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_MNSCLI_H 17 #define MAP_MSE_MNSCLI_H 18 19 #include <cstdint> 20 #include <memory> 21 #include <mutex> 22 #include <string> 23 #include <unordered_map> 24 #include "../obex/obex_mp_client.h" 25 #include "dispatcher.h" 26 #include "gap_if.h" 27 #include "map_mse_types.h" 28 #include "raw_address.h" 29 #include "sdp.h" 30 31 namespace OHOS { 32 namespace bluetooth { 33 /** 34 * @brief Callback function of MnsObserver 35 * 36 */ 37 class MnsObserver { 38 public: 39 /** 40 * @brief A destructor used to delete the Mns Observer instance. 41 * 42 */ 43 virtual ~MnsObserver() = default; 44 45 /** 46 * @brief Called OnDisconnected 47 * 48 */ 49 virtual void OnDisconnected() = 0; 50 }; 51 52 class MapMseMnscli { 53 public: 54 explicit MapMseMnscli(utility::Dispatcher &dispatcher, MnsObserver &mnsObserver); 55 56 virtual ~MapMseMnscli(); 57 58 /** 59 * @brief The function start MNS Client in asynchronization mode 60 * 61 * @return int 62 */ 63 int Connect(const RawAddress &remoteAddr, int masId); 64 65 /** 66 * @brief The function shutdown MNS Client 67 * 68 */ 69 void DisConnect(const RawAddress &remoteAddr, int masId); 70 71 /** 72 * @brief If the MCE supports the Message Notification feature and the the MCE had 73 * function to notify the MCE on events affecting the messages listings within 74 * the MSE's folder structure 75 * MAP specification #5.2 SendEvent Function 76 * @param bda Bluetooth address 77 * @param report Reference to the event 78 * @param report_size Reference to the event size 79 * @return int 80 */ 81 int SendEvent(const RawAddress &bda, const std::string &report, size_t reportSize, uint8_t masInstanceId); 82 83 int SdpSearch(const RawAddress &remoteAddr); 84 85 void PostCallback(const RawAddress &remoteAddr, ObexClientConfig clientConfig, uint32_t remoteFeature); 86 87 void SaveClientConfig(const RawAddress &remoteAddr, ObexClientConfig clientConfig, uint32_t remoteFeature); 88 89 static void SdpSearchCallback(const BtAddr *addr, const SdpService *serviceAry, uint16_t serviceNum, void *context); 90 static void GetRfcommNo(const SdpService &serviceAry, uint8_t &rfcommNo); 91 static void GetVersionNumber(const SdpService &serviceAry, uint16_t &versionNumber); 92 static void GetPsmRemoteFeature(const SdpService &serviceAry, uint16_t &psm, uint32_t &remoteFeature); 93 uint32_t GetRemoteFeatures(const std::string &addr); 94 bool IsDisconnected(void) const; 95 void DeregisterL2capLPsm(void) const; 96 97 private: 98 static const uint8_t MNS_TARGET[16]; 99 static const std::string HEADER_TYPE_EVENT; 100 static const uint16_t SERVICE_CLASS_UUID = 0x1133; 101 static const uint16_t GOEP_L2CAP_LPSM = 0x101D; 102 std::recursive_mutex mnsMapMutex_ {}; 103 int RegistGap(const RawAddress &remoteAddr, bool isL2cap, uint16_t scn) const; 104 void UnRegistGap(const RawAddress &remoteAddr, bool isL2cap, uint16_t scn) const; 105 uint32_t GetConnectionId(const std::string &address) const; 106 utility::Dispatcher &dispatcher_; 107 MnsObserver &mnsObserver_; 108 std::unordered_map<std::string, std::unique_ptr<ObexMpClient>> obexClientMap_ {}; 109 std::unordered_map<std::string, int> masIds_ {}; 110 std::unordered_map<std::string, ObexClientConfig> obexConfigMap_ {}; 111 std::unordered_map<std::string, BTConnectState> mnsStateMap_ {}; 112 std::unordered_map<std::string, uint32_t> connectionIdMap_ {}; 113 std::unordered_map<std::string, uint32_t> remoteFeatures_ {}; 114 void RemoveOnConnected(std::string address); 115 class MseClientObserver : public ObexClientObserver { 116 public: 117 /** 118 * @brief Construct a new Connect Observer object 119 * 120 * @param mnsClient Reference to the Mns instance 121 */ 122 explicit MseClientObserver(MapMseMnscli &mnsClient); 123 124 /** 125 * @brief A destructor used to delete the Mns Client Observer object. 126 * 127 */ 128 ~MseClientObserver() override = default; 129 130 private: 131 /** 132 * @brief Called OnTransportFailed 133 * @param client ObexClient 134 * @param errCd error code 135 */ 136 void OnTransportFailed(ObexClient &client, int errCd) override; 137 /** 138 * @brief Called OnConnected 139 * 140 * @param client ObexClient 141 * @param resp The Response from Server 142 */ 143 void OnConnected(ObexClient &client, const ObexHeader &resp) override; 144 /** 145 * @brief Called OnConnectFailed 146 * 147 * @param client ObexClient 148 * @param resp The Response from Server 149 */ 150 void OnConnectFailed(ObexClient &client, const ObexHeader &resp) override; 151 /** 152 * @brief Called OnDisconnected 153 * 154 * @param client ObexClient 155 */ 156 void OnDisconnected(ObexClient &client) override; 157 /** 158 * @brief Called OnActionCompleted 159 * 160 * @param client ObexClient 161 * @param resp The Response from Server 162 */ 163 void OnActionCompleted(ObexClient &client, const ObexHeader &resp) override; 164 165 MapMseMnscli &mnsClient_; 166 BT_DISALLOW_COPY_AND_ASSIGN(MseClientObserver); 167 }; 168 std::unique_ptr<MseClientObserver> clientObserver_ = nullptr; 169 BT_DISALLOW_COPY_AND_ASSIGN(MapMseMnscli); 170 }; 171 } // namespace bluetooth 172 } // namespace OHOS 173 174 #endif // MAP_MSE_MNSCLI_H