1 /* 2 * Copyright (C) 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 /** 17 * @addtogroup Bluetooth 18 * @{ 19 * 20 * @brief Defines a bluetooth system that provides basic bluetooth connection and profile functions, 21 * including A2DP, AVRCP, BLE, GATT, HFP, MAP, PBAP, and SPP, etc. 22 * 23 */ 24 25 /** 26 * @file bluetooth_opp.h 27 * 28 * @brief Declares OPP role framework functions, including basic and observer functions. 29 * 30 */ 31 #ifndef BLUETOOTH_OPP_H 32 #define BLUETOOTH_OPP_H 33 34 #include <string> 35 #include <vector> 36 #include <memory> 37 38 #include "bluetooth_def.h" 39 #include "bluetooth_remote_device.h" 40 #include "bluetooth_types.h" 41 #include "bluetooth_no_destructor.h" 42 namespace OHOS { 43 namespace Bluetooth { 44 /** 45 * @brief Bluetooth Opp Transfer Information. 46 * 47 * @since 9 48 */ 49 class BLUETOOTH_API BluetoothOppTransferInformation { 50 public: 51 /** 52 * @brief A constructor used to create a <b>BluetoothOppTransferInformation</b> instance. 53 * 54 * @since 15 55 */ 56 BluetoothOppTransferInformation(); 57 58 /** 59 * @brief A destructor used to delete the <b>BluetoothOppTransferInformation</b> instance. 60 * 61 * @since 15 62 */ 63 ~BluetoothOppTransferInformation(); 64 65 /** 66 * @brief Get Id. 67 * 68 * @return Returns Id. 69 * @since 15 70 */ 71 int GetId() const; 72 73 /** 74 * @brief Get File Name. 75 * 76 * @return File Name. 77 * @since 15 78 */ 79 std::string GetFileName() const; 80 81 /** 82 * @brief Get File Path. 83 * 84 * @return Returns File Path. 85 * @since 15 86 */ 87 std::string GetFilePath() const; 88 89 /** 90 * @brief Get Mime Type. 91 * 92 * @since 15 93 */ 94 std::string GetMimeType() const; 95 96 /** 97 * @brief Get Device Name. 98 * 99 * @return Returns Device Name. 100 * @since 15 101 */ 102 std::string GetDeviceName() const; 103 104 /** 105 * @brief Get Device Address. 106 * 107 * @return Returns Device Address. 108 * @since 15 109 */ 110 std::string GetDeviceAddress() const; 111 112 /** 113 * @brief Get Direction. 114 * 115 * @return Returns Direction. 116 * @since 15 117 */ 118 int GetDirection() const; 119 120 /** 121 * @brief Get Status. 122 * 123 * @return Returns Status. 124 * @since 15 125 */ 126 int GetStatus() const; 127 128 /** 129 * @brief Get Result. 130 * 131 * @return Returns Result. 132 * @since 15 133 */ 134 int GetResult() const; 135 136 /** 137 * @brief Get Time Stamp. 138 * 139 * @return TimeStamp. 140 * @since 15 141 */ 142 uint64_t GetTimeStamp() const; 143 144 /** 145 * @brief Get Current Bytes. 146 * 147 * @return Returns Current Bytes. 148 * @since 15 149 */ 150 uint64_t GetCurrentBytes() const; 151 152 /** 153 * @brief Get Total Bytes. 154 * 155 * @return Returns TotalBytes. 156 * @since 15 157 */ 158 uint64_t GetTotalBytes() const; 159 160 /** 161 * @brief Get CurrentCount. 162 * 163 * @return Returns CurrentCount. 164 * @since 15 165 */ 166 int GetCurrentCount() const; 167 168 /** 169 * @brief Get TotalCount. 170 * 171 * @return Returns TotalCount. 172 * @since 15 173 */ 174 int GetTotalCount() const; 175 176 /** 177 * @brief Set Id. 178 * 179 * @param interval Id. 180 * @since 15 181 */ 182 void SetId(int id); 183 184 /** 185 * @brief Set File Name. 186 * 187 * @param interval File Name. 188 * @since 15 189 */ 190 void SetFileName(std::string fileName); 191 192 /** 193 * @brief Set File Path. 194 * 195 * @param interval File Path. 196 * @since 15 197 */ 198 void SetFilePath(std::string filePath); 199 200 /** 201 * @brief Set Mime Type. 202 * 203 * @param interval Mime Type. 204 * @since 15 205 */ 206 void SetMimeType(std::string mimeType); 207 208 /** 209 * @brief Set Device Name. 210 * 211 * @param interval Device Name. 212 * @since 15 213 */ 214 void SetDeviceName(std::string deviceName); 215 216 /** 217 * @brief Set Device Address. 218 * 219 * @param interval Device Address. 220 * @since 15 221 */ 222 void SetDeviceAddress(std::string deviceAddress); 223 224 /** 225 * @brief Set Direction. 226 * 227 * @param interval Direction. 228 * @since 15 229 */ 230 void SetDirection(int direction); 231 232 /** 233 * @brief Set Status. 234 * 235 * @param interval Status. 236 * @since 15 237 */ 238 void SetStatus(int status); 239 240 /** 241 * @brief Set Result. 242 * 243 * @param interval Result. 244 * @since 15 245 */ 246 void SetResult(int result); 247 248 /** 249 * @brief Set Time Stamp. 250 * 251 * @param interval Time Stamp. 252 * @since 15 253 */ 254 void SetTimeStamp(uint64_t timeStamp); 255 256 /** 257 * @brief Set Current Bytes. 258 * 259 * @param interval Current Bytes. 260 * @since 15 261 */ 262 void SetCurrentBytes(uint64_t currentBytes); 263 264 /** 265 * @brief Set Total Bytes. 266 * 267 * @param interval Total Bytes. 268 * @since 15 269 */ 270 void SetTotalBytes(uint64_t totalBytes); 271 272 /** 273 * @brief Set CurrentCount. 274 * 275 * @param interval CurrentCount. 276 * @since 15 277 */ 278 void SetCurrentCount(int currentCount); 279 280 /** 281 * @brief Set TotalCount. 282 * 283 * @param interval TotalCount. 284 * @since 15 285 */ 286 void SetTotalCount(int totalCount); 287 288 private: 289 int id_ = -1; 290 std::string fileName_; 291 std::string filePath_; 292 std::string mimeType_; 293 std::string deviceName_; 294 std::string deviceAddress_; 295 int direction_ = 0; 296 int status_ = 0; 297 int result_ = 0; 298 uint64_t timeStamp_ = 0; 299 uint64_t currentBytes_ = 0; 300 uint64_t totalBytes_ = 0; 301 int currentCount_ = 0; 302 int totalCount_ = 0; 303 }; 304 305 /** 306 * @brief Bluetooth Opp FileHolder. 307 * 308 * @since 15 309 */ 310 class BLUETOOTH_API BluetoothOppFileHolder { 311 public: 312 /** 313 * @brief A destructor used to create the <b>BluetoothOppFileHolder</b> instance. 314 * 315 * @since 15 316 */ 317 BluetoothOppFileHolder(const std::string &filePath, const int64_t &fileSize, const int32_t &fileFd); 318 319 /** 320 * @brief A destructor used to create the <b>BluetoothOppFileHolder</b> instance. 321 * 322 * @since 15 323 */ 324 BluetoothOppFileHolder(); 325 326 /** 327 * @brief A destructor used to delete the <b>BluetoothOppFileHolder</b> instance. 328 * 329 * @since 15 330 */ 331 ~BluetoothOppFileHolder(); 332 333 /** 334 * @brief Get File Path. 335 * 336 * @return filePath. 337 * @since 15 338 */ 339 std::string GetFilePath() const; 340 341 /** 342 * @brief Get File Size. 343 * 344 * @return fileSize. 345 * @since 15 346 */ 347 int64_t GetFileSize() const; 348 349 /** 350 * @brief Get File Fd. 351 * 352 * @return fileFd. 353 * @since 15 354 */ 355 int32_t GetFileFd() const; 356 357 /** 358 * @brief Set File Path. 359 * 360 * @return filePath. 361 * @since 15 362 */ 363 void SetFilePath(const std::string &filePath); 364 365 /** 366 * @brief Set File Fd. 367 * 368 * @return fileFd. 369 * @since 15 370 */ 371 void SetFileFd(const int32_t &fileFd); 372 373 /** 374 * @brief Set File Size. 375 * 376 * @return fileSize. 377 * @since 15 378 */ 379 void SetFileSize(const int64_t &fileSize); 380 381 private: 382 std::string filePath_ = ""; 383 int64_t fileSize_ = 0; 384 int32_t fileFd_ = -1; 385 }; 386 387 /** 388 * @brief Class for Opp Host observer functions. 389 * 390 */ 391 class OppObserver { 392 public: 393 /** 394 * @brief The observer function to notify receive incoming file changed. 395 * 396 * @param transferInformation transfer Information. 397 */ OnReceiveIncomingFileChanged(const BluetoothOppTransferInformation & transferInformation)398 virtual void OnReceiveIncomingFileChanged(const BluetoothOppTransferInformation &transferInformation) 399 {} 400 401 /** 402 * @brief The observer function to notify transfer state changed. 403 * 404 * @param transferInformation transfer Information. 405 */ OnTransferStateChanged(const BluetoothOppTransferInformation & transferInformation)406 virtual void OnTransferStateChanged(const BluetoothOppTransferInformation &transferInformation) 407 {} 408 409 /** 410 * @brief Destroy the OppObserver object. 411 * 412 */ ~OppObserver()413 virtual ~OppObserver() 414 {} 415 }; 416 417 /** 418 * @brief Class for Opp API. 419 * 420 */ 421 class BLUETOOTH_API Opp { 422 public: 423 /** 424 * @brief Get the instance of Opp object. 425 * 426 * @return Returns the pointer to the Opp instance. 427 */ 428 static Opp *GetProfile(); 429 430 /** 431 * @brief Send File. 432 * 433 * @return Return operate result to confirm whether the file is successfully sent. 434 */ 435 int32_t SendFile(std::string device, std::vector<BluetoothOppFileHolder> fileHolders, bool& result); 436 437 /** 438 * @brief Set Incoming File Confirmation. 439 * 440 * @return Return operate result to confirm whether the setting is successful. 441 */ 442 int32_t SetIncomingFileConfirmation(bool accept, int32_t fileFd); 443 444 /** 445 * @brief Get Current Transfer Information. 446 * 447 * @return Return operate result to getCurrentTransferInformation. 448 */ 449 int32_t GetCurrentTransferInformation(BluetoothOppTransferInformation &transferInformation); 450 451 /** 452 * @brief Cancel Transfer. 453 * 454 * @return Return operate result to confirm whether the cancellation is set successfully. 455 */ 456 int32_t CancelTransfer(bool& result); 457 458 /** 459 * @brief Set LastReceived FileUri. 460 * @param Set LastReceived FileUri. 461 * 462 * @return Return operate result to SetLastReceivedFileUri. 463 */ 464 int32_t SetLastReceivedFileUri(const std::string &uri); 465 466 /** 467 * @brief Get remote opp device list which are in the specified states. 468 * 469 * @param states List of remote device states. 470 * @param result the list of devices 471 * @return Returns operate result. 472 */ 473 int32_t GetDevicesByStates(const std::vector<int32_t> &states, std::vector<BluetoothRemoteDevice> &result) const; 474 475 /** 476 * @brief Get the connection state of the specified remote opp device. 477 * 478 * @param device Remote device object. 479 * @param result the connection state of the remote device 480 * @return Returns operate result. 481 */ 482 int32_t GetDeviceState(const BluetoothRemoteDevice &device, int32_t &result) const; 483 484 /** 485 * @brief Register Opp observer instance. 486 * 487 * @param observer Opp observer instance. 488 */ 489 void RegisterObserver(std::shared_ptr<OppObserver> observer); 490 491 /** 492 * @brief Deregister Opp observer instance. 493 * 494 * @param observer Opp observer instance. 495 */ 496 void DeregisterObserver(std::shared_ptr<OppObserver> observer); 497 498 /** 499 * @brief The external process calls the Opp profile interface before the Bluetooth process starts. At this 500 * time, it needs to monitor the start of the Bluetooth process, and then call this interface to initialize the 501 * Opp proflie. 502 */ 503 void Init(); 504 505 /** 506 * @brief Static Opp observer instance. 507 * 508 */ 509 static OppObserver *instance_; 510 511 private: 512 Opp(); 513 ~Opp(); 514 BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(Opp); 515 BLUETOOTH_DECLARE_IMPL(); 516 517 #ifdef DTFUZZ_TEST 518 friend class BluetoothNoDestructor<Opp>; 519 #endif 520 }; 521 } // namespace Bluetooth 522 } // namespace OHOS 523 #endif // BLUETOOTH_OPP_H