Chris Manton | 38d9596 | 2019-10-28 17:29:29 -0700 | [diff] [blame] | 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> |
Chris Manton | 56deae0 | 2019-11-04 21:11:18 -0800 | [diff] [blame] | 19 | #include <functional> |
Chris Manton | 38d9596 | 2019-10-28 17:29:29 -0700 | [diff] [blame] | 20 | #include <future> |
| 21 | #include <memory> |
| 22 | #include <string> |
| 23 | |
| 24 | #include "module.h" |
Chris Manton | 38d9596 | 2019-10-28 17:29:29 -0700 | [diff] [blame] | 25 | |
| 26 | namespace bluetooth { |
| 27 | namespace shim { |
| 28 | |
Jakub Pawlowski | c73a895 | 2020-02-16 01:03:04 +0100 | [diff] [blame] | 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, 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 { |
Chris Manton | 38d9596 | 2019-10-28 17:29:29 -0700 | [diff] [blame] | 39 | public: |
Chris Manton | 2e1a231 | 2020-01-21 11:34:00 -0800 | [diff] [blame] | 40 | void RegisterService(uint16_t psm, bool use_ertm, uint16_t mtu, ConnectionCompleteCallback on_complete, |
Jakub Pawlowski | c73a895 | 2020-02-16 01:03:04 +0100 | [diff] [blame] | 41 | RegisterServicePromise register_promise); |
| 42 | void UnregisterService(uint16_t psm, UnregisterServicePromise unregister_promise); |
Chris Manton | c6f8744 | 2019-11-08 15:19:33 -0800 | [diff] [blame] | 43 | |
Chris Manton | 2e1a231 | 2020-01-21 11:34:00 -0800 | [diff] [blame] | 44 | void CreateConnection(uint16_t psm, const std::string address_string, ConnectionCompleteCallback on_complete, |
Jakub Pawlowski | c73a895 | 2020-02-16 01:03:04 +0100 | [diff] [blame] | 45 | CreateConnectionPromise create_promise); |
| 46 | void CloseConnection(uint16_t cid); |
Chris Manton | 38d9596 | 2019-10-28 17:29:29 -0700 | [diff] [blame] | 47 | |
Jakub Pawlowski | c73a895 | 2020-02-16 01:03:04 +0100 | [diff] [blame] | 48 | void SetReadDataReadyCallback(uint16_t cid, ReadDataReadyCallback on_data_ready); |
| 49 | void SetConnectionClosedCallback(uint16_t cid, ConnectionClosedCallback on_closed); |
Chris Manton | 56deae0 | 2019-11-04 21:11:18 -0800 | [diff] [blame] | 50 | |
Jakub Pawlowski | c73a895 | 2020-02-16 01:03:04 +0100 | [diff] [blame] | 51 | void Write(uint16_t cid, const uint8_t* data, size_t len); |
Chris Manton | 38d9596 | 2019-10-28 17:29:29 -0700 | [diff] [blame] | 52 | |
Jakub Pawlowski | c73a895 | 2020-02-16 01:03:04 +0100 | [diff] [blame] | 53 | void SendLoopbackResponse(std::function<void()>); |
Chris Manton | 38d9596 | 2019-10-28 17:29:29 -0700 | [diff] [blame] | 54 | |
| 55 | L2cap() = default; |
| 56 | ~L2cap() = default; |
| 57 | |
| 58 | static const ModuleFactory Factory; |
| 59 | |
| 60 | protected: |
| 61 | void ListDependencies(ModuleList* list) override; // Module |
| 62 | void Start() override; // Module |
| 63 | void Stop() override; // Module |
Chris Manton | e21efea | 2020-01-16 09:53:17 -0800 | [diff] [blame] | 64 | std::string ToString() const override; // Module |
Chris Manton | 38d9596 | 2019-10-28 17:29:29 -0700 | [diff] [blame] | 65 | |
| 66 | private: |
| 67 | struct impl; |
| 68 | std::unique_ptr<impl> pimpl_; |
| 69 | DISALLOW_COPY_AND_ASSIGN(L2cap); |
| 70 | }; |
| 71 | |
| 72 | } // namespace shim |
| 73 | } // namespace bluetooth |