1 /*
2  * Copyright 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17 
18 #include <cstdint>
19 #include <functional>
20 #include <future>
21 #include <memory>
22 #include <string>
23 
24 #include "module.h"
25 
26 namespace bluetooth {
27 namespace shim {
28 
29 using ConnectionClosedCallback = std::function<void(uint16_t cid, int error_code)>;
30 using ConnectionCompleteCallback =
31     std::function<void(std::string string_address, uint16_t psm, uint16_t cid, uint16_t remote_cid, bool is_connected)>;
32 using ReadDataReadyCallback = std::function<void(uint16_t cid, std::vector<const uint8_t> data)>;
33 
34 using RegisterServicePromise = std::promise<uint16_t>;
35 using UnregisterServicePromise = std::promise<void>;
36 using CreateConnectionPromise = std::promise<uint16_t>;
37 
38 class L2cap : public bluetooth::Module {
39  public:
40   void RegisterClassicService(
41       uint16_t psm,
42       bool use_ertm,
43       uint16_t mtu,
44       ConnectionCompleteCallback on_complete,
45       RegisterServicePromise register_promise);
46   void UnregisterClassicService(uint16_t psm, UnregisterServicePromise unregister_promise);
47 
48   void CreateClassicConnection(
49       uint16_t psm,
50       const std::string address_string,
51       ConnectionCompleteCallback on_complete,
52       CreateConnectionPromise create_promise);
53   void CloseClassicConnection(uint16_t cid);
54 
55   void SetReadDataReadyCallback(uint16_t cid, ReadDataReadyCallback on_data_ready);
56   void SetConnectionClosedCallback(uint16_t cid, ConnectionClosedCallback on_closed);
57 
58   void Write(uint16_t cid, const uint8_t* data, size_t len);
59 
60   void SendLoopbackResponse(std::function<void()>);
61 
62   L2cap() = default;
63   ~L2cap() = default;
64 
65   static const ModuleFactory Factory;
66 
67  protected:
68   void ListDependencies(ModuleList* list) override;  // Module
69   void Start() override;                             // Module
70   void Stop() override;                              // Module
71   std::string ToString() const override;             // Module
72 
73  private:
74   struct impl;
75   std::unique_ptr<impl> pimpl_;
76   DISALLOW_COPY_AND_ASSIGN(L2cap);
77 };
78 
79 }  // namespace shim
80 }  // namespace bluetooth
81