1 /* 2 * Copyright (C) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef MAP_MSE_INSTANCE_H 17 #define MAP_MSE_INSTANCE_H 18 19 #include <cstdint> 20 #include <list> 21 #include <string> 22 #include <unordered_map> 23 #include <vector> 24 #include "adapter_manager.h" 25 #include "bt_def.h" 26 #include "gap_if.h" 27 #include "map_mse_mnscli.h" 28 #include "map_mse_types.h" 29 #include "message.h" 30 #include "raw_address.h" 31 #include "rfcomm.h" 32 #include "sdp.h" 33 #include "stub/map_service.h" 34 #include "timer.h" 35 36 namespace OHOS { 37 namespace bluetooth { 38 struct MapAccountItem { 39 MessageType msgType_ = MessageType::NONE; 40 std::string id_ = ""; 41 std::string name_ = ""; 42 std::string pAuthority_ = ""; 43 std::string uci_ = ""; 44 std::string uciPrefix_ = ""; 45 std::string GetUciFullName(void); 46 }; 47 class MapObserver { 48 public: 49 // A destructor used to delete the Map Observer instance. 50 virtual ~MapObserver() = default; 51 52 // The function is called when MCE connect is incoming 53 virtual void OnIncomingConnect(const RawAddress &remoteAddr, const int masInstanceId) = 0; 54 55 // The function is called when MCE transport connect is successful 56 virtual void OnTransportConnected(const RawAddress &remoteAddr, const int masInstanceId) = 0; 57 58 // The function is called when MCE transport disconnect is incoming 59 virtual void OnTransportDisconnected(const RawAddress &remoteAddr, const int masInstanceId) = 0; 60 61 // The function is called when Connection State Changed 62 virtual void OnConnectionStateChanged( 63 const RawAddress &remoteAddr, const int masInstanceId, BTConnectState state) = 0; 64 65 // The function is called when MCE connect is incoming timeout 66 virtual void OnIncomingTimeout(const RawAddress &remoteAddr, const int masInstanceId) = 0; 67 68 // The function is called when MCE connect is incoming timeout 69 virtual void OnRejectConnection(const RawAddress &remoteAddr, const int masInstanceId) = 0; 70 71 // The function is called when MSE busy or not busy 72 virtual void OnBusy(const RawAddress &remoteAddr, bool isBusy) = 0; 73 }; 74 75 class MapMseInstance { 76 public: 77 explicit MapMseInstance(utility::Dispatcher &dispatcher, uint8_t masId, uint16_t l2capPsm, 78 const MapAccountItem &accountItem, bool smsSupport); 79 explicit MapMseInstance(utility::Dispatcher &dispatcher, uint8_t masId, uint16_t l2capPsm, bool smsSupport); 80 virtual ~MapMseInstance(); 81 int RegistMapObserver(MapObserver &observer); 82 void DeregisterObserver(MapObserver &observer); 83 void GrantPermission(const RawAddress &remoteAddr, bool allow, bool save = false); 84 int Enable(MapMseMnscli &mnsClient); 85 void Disable(void); 86 /// create SDP record 87 int CreateMasSdpRecord(void); 88 /// destroy SDP record 89 void DestroyMasSdpRecord(void); 90 /// regist GAP 91 int RegistGap(void); 92 /// unregist GAP 93 void UnRegistGap(void); 94 void SetConnectState(std::string, BTConnectState); 95 void DestroyIncomingConnect(std::string address); 96 uint64_t GetDatabaseIdentifier(void); 97 void UpdateDatabaseIdentifier(void); 98 uint64_t GetFolderVersionCounter(void); 99 void UpdateFolderVersionCounter(void); 100 uint64_t GetConversationVersionCounter(void); 101 void UpdateConversationVersionCounter(void); 102 uint8_t GetMasId(void); 103 bool IsConnected(const std::string &addr = ""); 104 int GetConnectState(void); 105 void GetConnectDevices(std::list<RawAddress> &devices); 106 void GetDeviceState(const std::string &addr, int &result); 107 void GetDevicesByStates(std::vector<RawAddress> &devices, const std::vector<int> &states); 108 void DisConnect(const std::string &addr = ""); 109 bool AcceptedNoCallBack(bool disable); 110 bool IsBusy(const std::string &addr); 111 112 private: 113 /** 114 * @brief call SDP function : Add ServiceClassIDList Attribute to record 115 * 116 * @return int 117 */ 118 int AddServiceClassIdList(void); 119 120 /** 121 * @brief call SDP function : Add ProtocolDescriptorList Attribute to record 122 * 123 * @return int 124 */ 125 int AddProtocolDescriptorList(void); 126 127 /** 128 * @brief call SDP function : Add ServiceName Attribute to record 129 * 130 * @return int 131 */ 132 int AddServiceName(void); 133 134 /** 135 * @brief Gets the default supported version 136 * 137 */ 138 void GetProfileVersion(void); 139 140 /** 141 * @brief call SDP function : Add BluetoothProfileDescriptorList Attribute to record 142 * 143 * @return int 144 */ 145 int AddBluetoothProfileDescriptorList(void); 146 147 /** 148 * @brief call SDP function : Add Attribute to record 149 * 150 * @return int 151 */ 152 int AddAttribute(void); 153 154 /** 155 * @brief call SDP function : Add Browse Group List to record 156 * 157 * @return int 158 */ 159 int AddBrowseGroupList(void); 160 161 /** 162 * @brief Register sdp service 163 * 164 * @return int 165 */ 166 int RegisterServiceRecord(void); 167 168 /** 169 * @brief Get device list according to address 170 * 171 */ 172 void GetDevicesNative(std::vector<RawAddress> &devices, const std::string &address); 173 struct NativeData { 174 public: 175 explicit NativeData(MapMseInstance &masInstance); 176 virtual ~NativeData() = default; 177 bool StartTimer(std::function<void()> callback, int ms); 178 void StopTimer(); 179 void IncomingStartTimer(const RawAddress &rawAddr); 180 void IncomingTimeout(const RawAddress &rawAddr); 181 void ProcessIncomingTimeout(const RawAddress &rawAddr); 182 bool IsAccepted = false; 183 bool IsBusy = false; 184 185 private: 186 std::unique_ptr<utility::Timer> timer_ = nullptr; 187 MapMseInstance &masInstance_; 188 BT_DISALLOW_COPY_AND_ASSIGN(NativeData); 189 }; 190 std::unordered_map<std::string, std::unique_ptr<NativeData>> nativeMap_ {}; 191 utility::Dispatcher &dispatcher_; 192 uint8_t masInstanceId_ = 0; 193 // SDP Register GoepL2capPsm value 194 uint16_t goepL2capPsm_ = 0; 195 MapAccountItem accountItem_ {}; 196 bool smsSupport_ = false; 197 uint8_t supportedMsgTypes_ = 0; 198 MapObserver *mapObserver_ = nullptr; 199 std::unordered_map<std::string, BTConnectState> stateMap_ {}; 200 std::recursive_mutex instanceMapMutex_ {}; 201 // SDP Register FRCOMM No 202 uint8_t rfcommNo_ = 0; 203 // SDP Register Service Name 204 std::string serviceName_ = ""; 205 // SDP handle 206 uint32_t sdpHandle_ = 0; 207 // SDP profile version 208 uint16_t profileVersion_ = 0; 209 // SDP map supported features 210 uint32_t supportedFeatures_ = 0; 211 std::atomic_uint64_t databaseIdentifier_ = ATOMIC_VAR_INIT(); 212 std::atomic_uint64_t folderVersionCounter_ = ATOMIC_VAR_INIT(); 213 std::atomic_uint64_t conversationVersionCounter_ = ATOMIC_VAR_INIT(); 214 bool shutDownEnable_ = false; 215 DECLARE_IMPL(); 216 BT_DISALLOW_COPY_AND_ASSIGN(MapMseInstance); 217 }; 218 } // namespace bluetooth 219 } // namespace OHOS 220 221 #endif // MAP_MSE_INSTANCE_H