blob: 61b4ededc3c9ff17af6227a78c804efdbedcef6a [file] [log] [blame]
Chris Manton38d95962019-10-28 17:29:29 -07001/*
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 Manton56deae02019-11-04 21:11:18 -080019#include <functional>
Chris Manton38d95962019-10-28 17:29:29 -070020#include <future>
21#include <memory>
22#include <string>
23
24#include "module.h"
Chris Manton38d95962019-10-28 17:29:29 -070025
26namespace bluetooth {
27namespace shim {
28
Jakub Pawlowskic73a8952020-02-16 01:03:04 +010029using ConnectionClosedCallback = std::function<void(uint16_t cid, int error_code)>;
30using ConnectionCompleteCallback =
31 std::function<void(std::string string_address, uint16_t psm, uint16_t cid, bool is_connected)>;
32using ReadDataReadyCallback = std::function<void(uint16_t cid, std::vector<const uint8_t> data)>;
33
34using RegisterServicePromise = std::promise<uint16_t>;
35using UnregisterServicePromise = std::promise<void>;
36using CreateConnectionPromise = std::promise<uint16_t>;
37
38class L2cap : public bluetooth::Module {
Chris Manton38d95962019-10-28 17:29:29 -070039 public:
Chris Manton2e1a2312020-01-21 11:34:00 -080040 void RegisterService(uint16_t psm, bool use_ertm, uint16_t mtu, ConnectionCompleteCallback on_complete,
Jakub Pawlowskic73a8952020-02-16 01:03:04 +010041 RegisterServicePromise register_promise);
42 void UnregisterService(uint16_t psm, UnregisterServicePromise unregister_promise);
Chris Mantonc6f87442019-11-08 15:19:33 -080043
Chris Manton2e1a2312020-01-21 11:34:00 -080044 void CreateConnection(uint16_t psm, const std::string address_string, ConnectionCompleteCallback on_complete,
Jakub Pawlowskic73a8952020-02-16 01:03:04 +010045 CreateConnectionPromise create_promise);
46 void CloseConnection(uint16_t cid);
Chris Manton38d95962019-10-28 17:29:29 -070047
Jakub Pawlowskic73a8952020-02-16 01:03:04 +010048 void SetReadDataReadyCallback(uint16_t cid, ReadDataReadyCallback on_data_ready);
49 void SetConnectionClosedCallback(uint16_t cid, ConnectionClosedCallback on_closed);
Chris Manton56deae02019-11-04 21:11:18 -080050
Jakub Pawlowskic73a8952020-02-16 01:03:04 +010051 void Write(uint16_t cid, const uint8_t* data, size_t len);
Chris Manton38d95962019-10-28 17:29:29 -070052
Jakub Pawlowskic73a8952020-02-16 01:03:04 +010053 void SendLoopbackResponse(std::function<void()>);
Chris Manton38d95962019-10-28 17:29:29 -070054
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 Mantone21efea2020-01-16 09:53:17 -080064 std::string ToString() const override; // Module
Chris Manton38d95962019-10-28 17:29:29 -070065
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