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 PCE_STATE_MACHINE_H 17 #define PCE_STATE_MACHINE_H 18 #include <cstdint> 19 #include <cstring> 20 #include <memory> 21 #include "../obex/obex_client.h" 22 #include "../obex/obex_headers.h" 23 #include "base_observer_list.h" 24 #include "gap_if.h" 25 #include "interface_profile_pbap_pce.h" 26 #include "log.h" 27 #include "message.h" 28 #include "pbap_pce_def.h" 29 #include "pbap_pce_gap.h" 30 #include "pbap_pce_header_msg.h" 31 #include "pbap_pce_obex_client.h" 32 #include "pbap_pce_parameter.h" 33 #include "pbap_pce_sdp.h" 34 #include "raw_address.h" 35 #include "rfcomm.h" 36 #include "state_machine.h" 37 38 namespace OHOS { 39 namespace bluetooth { 40 /** 41 * @brief statemachine 42 * control all state 43 */ 44 class PbapPceStateMachine : public utility::StateMachine { 45 public: 46 /** 47 * @brief constructor 48 * @details constructor 49 * @param dev remote device 50 * @param pceService pce service 51 * @param pbapPceSdp sdp service 52 * @param observerMgrList pce observer list 53 * @return 54 */ 55 explicit PbapPceStateMachine(const RawAddress &dev, PbapPceService &pceService, PbapPceSdp &pbapPceSdp, 56 BaseObserverList<IPbapPceObserver> &observerMgrList); 57 58 /** 59 * @brief destructor 60 * @details destructor 61 */ 62 virtual ~PbapPceStateMachine(); 63 64 /** 65 * @brief get remote device 66 * @details get remote device 67 * @return RawAddress& 68 */ 69 const RawAddress &GetDevice() const; 70 71 /** 72 * @brief get pce service 73 * @details get pce service 74 * @return Reference to PbapPceService 75 */ 76 PbapPceService &GetPceService() const; 77 78 /** 79 * @brief get pce obex client 80 * @details get pce obex client 81 * @return Reference to PbapPceObexClient 82 */ 83 PbapPceObexClient &GetObexClient() const; 84 85 /** 86 * @brief Set Version Number 87 * @details Set Version Number 88 * @param versionNumber version Number 89 * @return 90 */ 91 void SetVersionNumber(uint16_t versionNumber); 92 93 /** 94 * @brief get version number 95 * @details get version number 96 * @return uint16_t 97 */ 98 uint16_t GetVersionNumber() const; 99 100 /** 101 * @brief Set supported repositories 102 * @details Set supported repositories 103 * @param supportedRes supported repositories 104 * @return 105 */ 106 void SetSupportedRes(uint8_t supportedRes); 107 108 /** 109 * @brief get supported repositories 110 * @details get supported repositories 111 * @return uint8_t 112 */ 113 uint8_t GetSupportedRes() const; 114 115 /** 116 * @brief Set supported features 117 * @details Set supported features 118 * @param supportedRes supported features 119 * @return 120 */ 121 void SetSupportedFeature(uint32_t supportedFeature); 122 123 /** 124 * @brief get supported features 125 * @details get supported features 126 * @return uint32_t 127 */ 128 uint32_t GetSupportedFeature() const; 129 130 /** 131 * @brief set password 132 * @param password password 133 * @details set password 134 */ 135 void SetPassword(const std::string &password); 136 137 /** 138 * @brief get password 139 * @details get password 140 * @return std::string 141 */ 142 const std::string &GetPassword() const; 143 144 /** 145 * @brief get rfcomm or psm 146 * @details get rfcomm or psm 147 * @return bool 148 */ 149 bool IsGoepL2capPSM() const; 150 151 /** 152 * @brief Set rfcomm or psm 153 * @details Set rfcomm or psm 154 * @param goepL2capPSM rfcomm or psm 155 * @return 156 */ 157 void SetGoepL2capPSM(bool goepL2capPSM); 158 159 /** 160 * @brief get GapSecChannel 161 * @details get GapSecChannel 162 * @return GapSecChannel 163 */ 164 const GapSecChannel GetGapSecChannel() const; 165 166 /** 167 * @brief Set GapSecChannel 168 * @details Set GapSecChannel 169 * @param gapSecChannel 170 * @return 171 */ 172 void SetGapSecChannel(const GapSecChannel &gapSecChannel); 173 174 /** 175 * @brief get feature flag 176 * @details get feature flag 177 * @return bool 178 */ 179 bool GetFeatureFlag() const; 180 181 /** 182 * @brief Set feature flag 183 * @details Set feature flag 184 * @param featureFlag 185 * @return 186 */ 187 void SetFeatureFlag(bool featureFlag); 188 189 /** 190 * @brief get sdp 191 * @details get sdp 192 * @return PbapPceSdp reference 193 */ 194 PbapPceSdp &GetSdp() const; 195 196 /** 197 * @brief get sdp 198 * @details get sdp 199 * @return PbapPceGap* 200 */ 201 PbapPceGap *GetGap() const; 202 203 /** 204 * @brief Set gap 205 * @details Set gap 206 * @param gap reference 207 * @return 208 */ 209 void SetGap(PbapPceGap &gap); 210 211 /** 212 * @brief Create Pce Obex Client 213 * @details Create Pce Obex Client 214 * @param obexConfig 215 * @return 216 */ 217 void CreatePceObexClient(const ObexClientConfig &obexConfig); 218 219 /** 220 * @brief set userId 221 * @details set userId 222 * @param userId userId 223 */ 224 void SetUserId(const std::string &userId); 225 226 /** 227 * @brief get userId 228 * @details get userId 229 * @return userId 230 */ 231 const std::string &GetUserId() const; 232 233 /** 234 * @brief IsRepositorySupported 235 * @details IsRepositorySupported 236 */ 237 bool IsRepositorySupported(const std::u16string &name) const; 238 239 /** 240 * @brief PullPhoneBook 241 * @details PullPhoneBook 242 */ 243 void PullPhoneBook(const IPbapPullPhoneBookParam ¶m); 244 245 /** 246 * @brief SetPhoneBook 247 * @details SetPhoneBook 248 */ 249 void SetPhoneBook(const std::u16string &name, int flag); 250 251 /** 252 * @brief PullvCardListing 253 * @details PullvCardListing 254 */ 255 void PullvCardListing(const IPbapPullvCardListingParam ¶m); 256 257 /** 258 * @brief PullvCardEntry 259 * @details PullvCardEntry 260 */ 261 void PullvCardEntry(const IPbapPullvCardEntryParam ¶m); 262 263 /** 264 * @brief IsBusy 265 * @details Is Busy 266 */ 267 bool IsBusy() const; 268 269 /** 270 * @brief SetBusy 271 * @details SetBusy 272 */ 273 bool SetBusy(bool isBusy); 274 275 /** 276 * @brief SetPowerStatusBusy 277 * @details SetPowerStatusBusy 278 */ 279 void SetPowerStatusBusy(bool isBusy); 280 281 /** 282 * @brief AbortDownloading 283 * @details AbortDownloading 284 */ 285 int AbortDownloading(); 286 287 /** 288 * @brief SetConnectId 289 * @details SetConnectId 290 */ 291 void SetConnectId(uint32_t connectId); 292 293 /** 294 * @brief send obex disconnect request 295 * @param withObexReq is send obex disconnect request 296 * @return int Request processing result:0:succeeded -1:failed 297 */ 298 int ReqDisconnect(bool withObexReq = true) const; 299 300 /** 301 * @brief Get current virtual phonebook path 302 * @details when SetPhoneBook action succeeded, save the path 303 * @return path 304 */ 305 const std::u16string &GetCurrentPath() const; 306 307 /** 308 * @brief SetCurrentPath 309 * @details when SetPhoneBook action succeeded, call this 310 * @param path 311 */ 312 void SetCurrentPath(const std::u16string &path); 313 314 /** 315 * @brief GetReqVcardFileName 316 * @details Get Requested Vcard FileName for local DB 317 */ 318 const std::u16string &GetReqVcardFileName() const; 319 320 /** 321 * @brief GetDownloadFileName 322 * @details Get Download FileName for ObexFileBodyObject 323 */ 324 std::string GetDownloadFileName() const; 325 326 /** 327 * @brief SetCurrentPath 328 * @details when SetPhoneBook action succeeded, call this 329 * @param path 330 */ 331 void SetReqVcardFileName(const std::u16string &reqVcardFileName); 332 333 public: 334 void HandlePhoneBookSizeActionCompleted(const utility::Message &msg); 335 void HandlePullPhoneBookActionCompleted(const utility::Message &msg); 336 void HandleSetPhoneBookActionCompleted(const utility::Message &msg); 337 void HandlePullvCardListingSizeActionCompleted(const utility::Message &msg); 338 void HandlePullvCardListingActionCompleted(const utility::Message &msg); 339 void HandlePullvCardEntryActionCompleted(const utility::Message &msg); 340 void HandleAbortDownloadingActionCompleted(const utility::Message &msg); 341 void TryReleasePbapMsg(const utility::Message &msg) const; 342 void RemoveBTMLogging() const; 343 void TransitTargetState(); 344 int GetTargetState() const; 345 void SetTargetState(int targetState); 346 347 private: 348 static bool SplitFilePath(const std::u16string &filePath, std::u16string &retPath, std::u16string &retFileName); 349 static std::string Replace(const std::string &str, const std::string &oldStr, const std::string &newStr); 350 static std::string U16ToStr(const std::u16string &str); 351 bool ObexHeaderToDataResult(const ObexHeader &resp, int actionType, IPbapPhoneBookData &retResult); 352 bool IsResponeOK(uint8_t respCode) const; 353 void InitIPbapPhoneBookData(IPbapPhoneBookData &retResult); 354 355 private: 356 RawAddress device_; // remote address 357 PbapPceService *pceService_ {nullptr}; // pce service 358 std::unique_ptr<PbapPceObexClient> pceObexClient_ {nullptr}; // obex client 359 360 uint16_t versionNumber_ = 0; // received from pse version number 361 uint8_t supportedRes_ = 0; // received from pse Supported Repositories 362 uint32_t supportedFeature_ = 0; // received from pse Supported feature 363 bool featureFlag_ = false; // true:can received feature; false:cannot received feature 364 365 GapSecChannel gapSecChannel_ {0}; // gap channel 366 bool isGoepL2capPSM_ = false; // rfcomm or psm 367 std::string password_ = "0000"; // user input password 368 std::string userId_ = ""; // default password 369 uint32_t connectId_ {0}; // session connectId from PceConnectedState state 370 371 PbapPceSdp &pbapPceSdp_; // sdp 372 std::unique_ptr<PbapPceGap> pbapPceGap_ {nullptr}; // gap 373 BaseObserverList<IPbapPceObserver> &observerMgrList_; // observerlist 374 std::atomic_bool powerStatusBusy_ {false}; // powerStatusBusy_ 375 std::u16string currentPath_ = u""; // current path in sever 376 std::u16string reqVcardFileName_ = u""; // requested vcard file name 377 std::atomic_int targetState_ {0}; // target State 378 BT_DISALLOW_COPY_AND_ASSIGN(PbapPceStateMachine); 379 }; 380 } // namespace bluetooth 381 } // namespace OHOS 382 383 #endif // PCE_STATE_MACHINE_H 384