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