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 17 #pragma once 18 19 #include <cstdint> 20 #include <set> 21 #include <unordered_map> 22 23 #include "main/shim/dumpsys.h" 24 #include "stack/include/l2c_api.h" 25 26 namespace bluetooth { 27 namespace shim { 28 namespace legacy { 29 30 static constexpr uint16_t kInitialClassicDynamicPsm = 0x1001; 31 static constexpr uint16_t kFinalClassicDynamicPsm = 0xfeff; 32 static constexpr uint16_t kInitialClassicVirtualPsm = kInitialClassicDynamicPsm; 33 static constexpr uint16_t kFinalClassicVirtualPsm = 0x8000; 34 static constexpr uint16_t kInitialLeDynamicPsm = 0x0080; 35 static constexpr uint16_t kFinalLeDynamicPsm = 0x00ff; 36 37 class PsmManager { 38 public: 39 bool IsPsmRegistered(uint16_t psm) const; 40 bool HasClient(uint16_t psm) const; 41 void RegisterPsm(uint16_t psm, const tL2CAP_APPL_INFO* callbacks); 42 void RegisterPsm(uint16_t psm); 43 void UnregisterPsm(uint16_t psm); 44 const tL2CAP_APPL_INFO* Callbacks(uint16_t psm); 45 46 private: 47 /** 48 * Mapping of psm to client callback. 49 * 50 * The current API allows a client may reserve a psm but not 51 * provide a callback which is reflected in a mapping of a 52 * valid psm key entry but a nullptr value. 53 * 54 * A valid client is indicated with a valid psm key entry and a 55 * non-nullptr value. 56 */ 57 std::unordered_map<uint16_t, const tL2CAP_APPL_INFO*> psm_to_callback_map_; 58 }; 59 60 class L2cap { 61 public: 62 uint16_t RegisterService(uint16_t psm, const tL2CAP_APPL_INFO* callbacks, 63 bool enable_snoop, tL2CAP_ERTM_INFO* p_ertm_info); 64 void UnregisterService(uint16_t psm); 65 66 uint16_t CreateConnection(uint16_t psm, const RawAddress& raw_address); 67 68 bool Write(uint16_t cid, BT_HDR* bt_hdr); 69 70 void OnLocalInitiatedConnectionCreated(std::string string_address, 71 uint16_t psm, uint16_t cid, 72 bool connected); 73 void OnRemoteInitiatedConnectionCreated(std::string string_addresss, 74 uint16_t psm, uint16_t cid); 75 76 uint16_t GetNextDynamicClassicPsm(); 77 uint16_t GetNextDynamicLePsm(); 78 79 uint16_t ConvertClientToRealPsm(uint16_t psm, 80 bool is_outgoing_only_connection); 81 uint16_t ConvertClientToRealPsm(uint16_t psm); 82 void RemoveClientPsm(uint16_t client_psm); 83 84 // Legacy API entry points 85 bool ConnectResponse(const RawAddress& raw_address, uint8_t signal_id, 86 uint16_t cid, uint16_t result, uint16_t status, 87 tL2CAP_ERTM_INFO* ertm_info); 88 bool ConfigRequest(uint16_t cid, const tL2CAP_CFG_INFO* config_info); 89 bool ConfigResponse(uint16_t cid, const tL2CAP_CFG_INFO* config_info); 90 bool DisconnectRequest(uint16_t cid); 91 bool DisconnectResponse(uint16_t cid); 92 93 L2cap(); 94 ~L2cap(); 95 96 PsmManager& Classic(); 97 PsmManager& Le(); 98 99 private: 100 uint16_t GetNextVirtualPsm(uint16_t real_psm); 101 void SetDownstreamCallbacks(uint16_t cid); 102 103 void Dump(int fd); 104 105 PsmManager classic_; 106 PsmManager le_; 107 108 bool ConnectionExists(uint16_t cid) const; 109 uint16_t CidToPsm(uint16_t cid) const; 110 111 uint16_t classic_dynamic_psm_; 112 uint16_t le_dynamic_psm_; 113 uint16_t classic_virtual_psm_; 114 115 std::unordered_map<uint16_t, 116 std::function<void(std::function<void(uint16_t c)>)>> 117 cid_to_postable_map_; 118 std::set<uint16_t> cid_closing_set_; 119 120 std::unordered_map<uint16_t, uint16_t> cid_to_psm_map_; 121 std::unordered_map<uint16_t, uint16_t> client_psm_to_real_psm_map_; 122 }; 123 124 } // namespace legacy 125 } // namespace shim 126 } // namespace bluetooth 127