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 #include <vector> 25 26 #include "device_manager.h" 27 #include "remote_command_executor.h" 28 #include "socket.h" 29 30 namespace OHOS { 31 namespace Security { 32 namespace AccessToken { 33 class SoftBusManager final { 34 public: 35 virtual ~SoftBusManager(); 36 37 /** 38 * @brief Get instance of SoftBusManager 39 * 40 * @return SoftBusManager's instance. 41 * @since 1.0 42 * @version 1.0 43 */ 44 static SoftBusManager &GetInstance(); 45 46 /** 47 * @brief Bind soft bus service. 48 * 49 * @since 1.0 50 * @version 1.0 51 */ 52 void Initialize(); 53 54 /** 55 * @brief Unbind soft bus service when DPMS has been destroyed. 56 * 57 * @since 1.0 58 * @version 1.0 59 */ 60 void Destroy(); 61 62 /** 63 * @brief Open session with the peer device sychronized. 64 * 65 * @param deviceUdid The udid of peer device. 66 * @return Session id if open successfully, otherwise return -1(Constant::FAILURE). 67 * @since 1.0 68 * @version 1.0 69 */ 70 int BindService(const std::string &deviceUdid); 71 72 /** 73 * @brief Close socket with the peer device. 74 * 75 * @param socketFd The socket id need to close. 76 * @return 0 if close successfully, otherwise return -1(Constant::FAILURE). 77 * @since 1.0 78 * @version 1.0 79 */ 80 int CloseSocket(int socketFd); 81 82 /** 83 * @brief Get UUID(networkId) by deviceNodeId. 84 * 85 * @param networkId The valid networkId. 86 * @return uuid if deviceManager is ready, empty string otherwise. 87 * @since 1.0 88 * @version 1.0 89 */ 90 std::string GetUniversallyUniqueIdByNodeId(const std::string &networkId); 91 92 /** 93 * @brief Get deviceId(UDID) by deviceNodeId. 94 * 95 * @param networkId The valid networkId. 96 * @return udid if deviceManager work correctly, empty string otherwise. 97 * @since 1.0 98 * @version 1.0 99 */ 100 std::string GetUniqueDeviceIdByNodeId(const std::string &networkId); 101 102 bool GetNetworkIdBySocket(const int32_t socket, std::string& networkId); 103 104 int32_t GetRepeatTimes(); 105 106 public: 107 static const std::string SESSION_NAME; 108 109 private: 110 SoftBusManager(); 111 int DeviceInit(); 112 bool CheckAndCopyStr(char* dest, uint32_t destLen, const std::string& src); 113 int32_t InitSocketAndListener(const std::string& networkId, ISocketListener& listener); 114 int32_t ServiceSocketInit(); 115 116 /** 117 * @brief Fulfill local device info 118 * 119 * @return 0 if operate successfully, otherwise return -1(Constant::FAILURE). 120 * @since 1.0 121 * @version 1.0 122 */ 123 int FulfillLocalDeviceInfo(); 124 125 /** 126 * @brief add all trusted device info. 127 * 128 * @since 1.0 129 * @version 1.0 130 */ 131 int AddTrustedDeviceInfo(); 132 133 void SetDefaultConfigValue(); 134 void GetConfigValue(); 135 136 // soft bus session server opened flag 137 bool isSoftBusServiceBindSuccess_; 138 std::atomic_bool inited_; 139 140 // init mutex 141 std::mutex mutex_; 142 143 // fulfill thread mutex 144 std::mutex fulfillMutex_; 145 146 // soft bus service socket fd 147 int32_t socketFd_ = -1; 148 149 // soft bus client socket with networkId map 150 std::mutex clientSocketMutex_; 151 std::map<int32_t, std::string> clientSocketMap_; 152 153 // remote request overtime repeat times 154 int32_t sendRequestRepeatTimes_ = 0; 155 }; 156 } // namespace AccessToken 157 } // namespace Security 158 } // namespace OHOS 159 #endif // SOFT_BUS_MANAGER_H 160