1 /* 2 * Copyright (C) 2021 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 PBAP_PCE_OBEX_CLIENT_H 17 #define PBAP_PCE_OBEX_CLIENT_H 18 19 #include <cstdint> 20 #include <cstring> 21 #include <fstream> 22 #include <memory> 23 #include "../obex/obex_body.h" 24 #include "../obex/obex_client.h" 25 #include "../obex/obex_headers.h" 26 #include "../obex/obex_mp_client.h" 27 #include "message.h" 28 #include "pbap_pce_header_msg.h" 29 #include "pbap_pce_service.h" 30 31 namespace OHOS { 32 namespace bluetooth { 33 class ObexFileBodyObject : public ObexBodyObject { 34 public: 35 explicit ObexFileBodyObject(const std::string &file); 36 ObexFileBodyObject() = default; 37 ~ObexFileBodyObject() override = default; 38 size_t Read(uint8_t *buf, size_t bufLen) override; 39 size_t Write(const uint8_t *buf, size_t bufLen) override; 40 int Close() override; 41 42 private: 43 void OpenOutFile(const std::string &file); 44 std::ofstream ofs_ {}; 45 }; 46 47 /** 48 * @brief obex client 49 * wrap obex client 50 */ 51 class PbapPceObexClient { 52 public: 53 /** 54 * @brief constructor 55 * @details construct with ObexClientConfig 56 * @param config obex client config 57 * @return 58 */ 59 explicit PbapPceObexClient(const ObexClientConfig &config, PbapPceService &pceService); 60 61 /** 62 * @brief destructor 63 * @details constructor 64 */ 65 virtual ~PbapPceObexClient(); 66 67 /** 68 * @brief connect to obex client 69 * @details connect to obex client 70 * @return @c 0 success 71 * @c -1 failure 72 */ 73 int Connect(bool supported); 74 75 /** 76 * @brief connect to obex client again 77 * @details connect to obex client again 78 * @param challenge challenge header 79 * @param digest digest header 80 * @return @c 0 success 81 * @c -1 failure 82 */ 83 int Reconnect(ObexDigestChallenge &challenge, ObexDigestResponse &digest); 84 85 /** 86 * @brief send obex disconnect request 87 * @param withObexReq is send obex disconnect request 88 * @return int Request processing result:0:succeeded -1:failed 89 */ 90 int Disconnect(bool withObexReq = true) const; 91 92 /** 93 * @brief disconnect to obex client 94 * @details disconnect to obex client 95 * @param req challenge header 96 * @param reqMsgType request message type 97 * @return @c 0 success 98 * @c -1 failure 99 */ 100 int Get(const ObexHeader &req, int reqMsgType) const; 101 /** 102 * @brief set path 103 * @details set path 104 * @param flag go direction 105 * @param path relative path or absolute path 106 * @return @c 0 success 107 * @c -1 failure 108 */ 109 int SetPath(uint8_t flag, const std::u16string &path) const; 110 /** 111 * @brief abort downloading phonebook 112 * @details abort downloading phonebook 113 * @return @c 0 success 114 * @c -1 failure 115 */ 116 int Abort() const; 117 118 /** 119 * @brief GetClient 120 * @return pointer to ObexMpClient 121 */ 122 ObexMpClient *GetClient() const; 123 124 struct PhoneBookActionInfo { 125 int operationId_ = 0; // pce last operation id 126 int reqMsgType_ = 0; // pce last command id 127 std::u16string path_ = u""; // pce last command path 128 uint8_t flags_ = 0; // pce last flags 129 bool isAbort = false; 130 }; 131 /** 132 * @brief obex observer 133 * wrap obex observer 134 */ 135 class PceObexObserver : public ObexClientObserver { 136 public: 137 /** 138 * @brief constructor 139 * @details pceService pce service pointer 140 * @return 141 */ PceObexObserver(PbapPceService & pceService)142 explicit PceObexObserver(PbapPceService &pceService) : pceService_(pceService) 143 {} 144 145 /** 146 * @brief destructor 147 * @details constructor 148 */ ~PceObexObserver()149 virtual ~PceObexObserver() 150 {} 151 152 /** 153 * @brief Called OnTransportFailed 154 * @param client ObexClient 155 * @param errCd error code 156 */ 157 void OnTransportFailed(ObexClient &client, int errCd) override; 158 159 /** 160 * @brief Called OnConnected 161 * @details Called OnConnected 162 * @param client ObexClient 163 * @param resp The Response from Server 164 */ 165 void OnConnected(ObexClient &client, const ObexHeader &resp) override; 166 167 /** 168 * @brief Called OnConnectFailed 169 * @details Called OnConnectFailed 170 * @param client ObexClient 171 * @param resp The Response from Server 172 */ 173 void OnConnectFailed(ObexClient &client, const ObexHeader &resp) override; 174 175 /** 176 * @brief Called OnDisconnected 177 * @details Called OnDisconnected 178 * @param client ObexClient 179 */ 180 void OnDisconnected(ObexClient &client) override; 181 182 /** 183 * @brief Called OnActionCompleted 184 * @details Called OnActionCompleted 185 * @param client ObexClient 186 * @param resp The Response from Server 187 */ 188 void OnActionCompleted(ObexClient &client, const ObexHeader &resp) override; 189 190 /** 191 * @brief Called OnBusy 192 * 193 * @param client ObexClient 194 * @param isBusy true:become busy false:become not busy 195 */ 196 void OnBusy(ObexClient &client, bool isBusy) override; 197 198 /** 199 * @brief Get PhoneBook Action Info 200 * @details Get PhoneBook Action Info 201 */ 202 bool GetPhoneBookActionInfo( 203 int operationId, int &retReqMsgType, std::u16string &retPath, uint8_t &retFlags) const; 204 205 /** 206 * @brief Set PhoneBook Action Info 207 * @details Set PhoneBook Action Info when action starting 208 */ 209 void SetPhoneBookActionInfo(int operationId, int reqMsgType, const std::u16string &path = u"", 210 uint8_t flags = 0, bool isBusy = true); 211 212 PbapPceService &pceService_; // pce service pointer 213 std::atomic_bool isBusy_ {false}; // pce start downloading; 214 std::atomic_bool isAbort_ {false}; // pce abort downloading; 215 PhoneBookActionInfo phoneBookActionInfo_ {}; // action info 216 }; 217 218 /** 219 * @brief Set Busy 220 * @details Set Busy as proxy observer_ 221 */ 222 void SetBusy(bool isBusy) const; 223 224 /** 225 * @brief Is Busy 226 * @details Is Busy as proxy observer_ 227 */ 228 bool IsBusy() const; 229 230 /** 231 * @brief Set Abort 232 * @details Set Abort as proxy observer_ 233 */ 234 void SetAbort(bool isAbort) const; 235 236 private: 237 std::unique_ptr<PceObexObserver> observer_ {nullptr}; // obex observer 238 ObexClientConfig obexConfig_; // obex config 239 PbapPceService &pceService_; // pce service 240 std::unique_ptr<ObexMpClient> client_ {nullptr}; // obex client 241 242 // pce feature 243 static const uint32_t PBAP_PCE_FEATURES = PBAP_PCE_FEATURES_DOWNLOAD | 244 PBAP_PCE_FEATURES_BROWSING | 245 PBAP_PCE_FEATURES_DATABASE_IDENTIFIER | 246 PBAP_PCE_FEATURES_FOLDER_VERSION_COUNTERS | 247 PBAP_PCE_FEATURES_VCARD_SELECTING | 248 PBAP_PCE_FEATURES_ENHANCED_MISSED_CALLS | 249 PBAP_PCE_FEATURES_X_BT_UCI_VCARD_PROPERTY | 250 PBAP_PCE_FEATURES_X_BT_UID_VCARD_PROPERTY | 251 PBAP_PCE_FEATURES_CONTACT_REFERENCING | 252 PBAP_PCE_FEATURES_DEFAULT_CONTACT_IMAGE_FORMAT; 253 }; 254 } // namespace bluetooth 255 } // namespace OHOS 256 #endif // PBAP_PCE_OBEX_CLIENT_H 257