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 #ifndef SOFT_BUS_MANAGER_H 17 #define SOFT_BUS_MANAGER_H 18 19 #include <functional> 20 #include <cinttypes> 21 #include <memory> 22 #include <string> 23 #include <thread> 24 25 #include "accesstoken_log.h" 26 #include "device_manager.h" 27 #include "remote_command_executor.h" 28 #include "session.h" 29 #include "soft_bus_device_connection_listener.h" 30 #include "soft_bus_session_listener.h" 31 32 namespace OHOS { 33 namespace Security { 34 namespace AccessToken { 35 class SoftBusManager final { 36 public: 37 virtual ~SoftBusManager(); 38 39 /** 40 * @brief Get instance of SoftBusManager 41 * 42 * @return SoftBusManager's instance. 43 * @since 1.0 44 * @version 1.0 45 */ 46 static SoftBusManager &GetInstance(); 47 48 /** 49 * @brief Bind soft bus service. 50 * 51 * @since 1.0 52 * @version 1.0 53 */ 54 void Initialize(); 55 56 /** 57 * @brief Unbind soft bus service when DPMS has been destroyed. 58 * 59 * @since 1.0 60 * @version 1.0 61 */ 62 void Destroy(); 63 64 /** 65 * @brief Open session with the peer device sychronized. 66 * 67 * @param deviceUdid The udid of peer device. 68 * @return Session id if open successfully, otherwise return -1(Constant::FAILURE). 69 * @since 1.0 70 * @version 1.0 71 */ 72 int OpenSession(const std::string &deviceUdid); 73 74 /** 75 * @brief Close session with the peer device. 76 * 77 * @param session The session id need to close. 78 * @return 0 if close successfully, otherwise return -1(Constant::FAILURE). 79 * @since 1.0 80 * @version 1.0 81 */ 82 int CloseSession(int sessionId); 83 84 /** 85 * @brief Get UUID(networkId) by deviceNodeId. 86 * 87 * @param deviceNodeId The valid networkId or deviceId(UDID) or deviceUuid. 88 * @return uuid if deviceManager is ready, empty string otherwise. 89 * @since 1.0 90 * @version 1.0 91 */ 92 std::string GetUniversallyUniqueIdByNodeId(const std::string &deviceNodeId); 93 94 /** 95 * @brief Get deviceId(UDID) by deviceNodeId. 96 * 97 * @param deviceNodeId The valid networkId or deviceId(UDID) or deviceUuid. 98 * @return udid if deviceManager work correctly, empty string otherwise. 99 * @since 1.0 100 * @version 1.0 101 */ 102 std::string GetUniqueDeviceIdByNodeId(const std::string &deviceNodeId); 103 104 public: 105 static const std::string SESSION_NAME; 106 107 private: 108 SoftBusManager(); 109 int DeviceInit(); 110 int SessionInit(); 111 112 /** 113 * @brief Fulfill local device info 114 * 115 * @return 0 if operate successfully, otherwise return -1(Constant::FAILURE). 116 * @since 1.0 117 * @version 1.0 118 */ 119 int FulfillLocalDeviceInfo(); 120 121 /** 122 * @brief add all trusted device info. 123 * 124 * @since 1.0 125 * @version 1.0 126 */ 127 int AddTrustedDeviceInfo(); 128 129 std::string GetUuidByNodeId(const std::string &nodeId) const; 130 std::string GetUdidByNodeId(const std::string &nodeId) const; 131 132 const static std::string TOKEN_SYNC_PACKAGE_NAME; 133 134 // soft bus session server opened flag 135 bool isSoftBusServiceBindSuccess_; 136 std::atomic_bool inited_; 137 138 // init mutex 139 std::mutex mutex_; 140 141 // fulfill thread mutex 142 std::mutex fulfillMutex_; 143 }; 144 } // namespace AccessToken 145 } // namespace Security 146 } // namespace OHOS 147 #endif // SOFT_BUS_MANAGER_H 148