1 /* 2 * Copyright (c) 2024 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 IAM_SOFT_BUS_MANAGER_H 17 #define IAM_SOFT_BUS_MANAGER_H 18 19 #include <cstdint> 20 #include <string> 21 #include <map> 22 #include <mutex> 23 24 #include "iam_check.h" 25 #include "iam_logger.h" 26 #include "iam_common_defines.h" 27 #include "soft_bus_client_socket.h" 28 #include "soft_bus_server_socket.h" 29 #include "system_ability_listener.h" 30 31 #define LOG_TAG "USER_AUTH_SA" 32 namespace OHOS { 33 namespace UserIam { 34 namespace UserAuth { 35 inline static const std::string USER_AUTH_PACKAGE_NAME = "ohos.useriam"; 36 class SoftBusManager { 37 public: 38 virtual ~SoftBusManager(); 39 static SoftBusManager &GetInstance(); 40 void Start(); 41 void Stop(); 42 43 ResultCode OpenConnection(const std::string &connectionName, const uint32_t tokenId, const std::string &networkId); 44 ResultCode CloseConnection(const std::string &connectionName); 45 46 ResultCode SendMessage(const std::string &connectionName, 47 const std::string &srcEndPoint, const std::string &destEndPoint, 48 const std::shared_ptr<Attributes> &attributes, MsgCallback &callback); 49 50 std::shared_ptr<BaseSocket> FindClientSocket(const std::string &connectionName); 51 std::shared_ptr<BaseSocket> GetServerSocket(); 52 std::shared_ptr<BaseSocket> FindSocketBySocketId(const int32_t socketId); 53 54 void OnBind(int32_t socketId, PeerSocketInfo info); 55 void OnShutdown(int32_t socketId, ShutdownReason reason); OnQos(int32_t socketId,QoSEvent eventId,const QosTV * qos,uint32_t qosCount)56 void OnQos(int32_t socketId, QoSEvent eventId, const QosTV *qos, uint32_t qosCount) {}; 57 void OnClientBytes(int32_t socketId, const void *data, uint32_t dataLen); 58 void OnServerBytes(int32_t socketId, const void *data, uint32_t dataLen); 59 void DoOpenConnection(const std::string &connectionName, const uint32_t tokenId, 60 const std::string &networkId); 61 ResultCode DoCloseConnection(const std::string &connectionName); 62 63 private: 64 SoftBusManager(); 65 ResultCode RegistDeviceManagerListener(); 66 ResultCode UnRegistDeviceManagerListener(); 67 ResultCode RegistSoftBusListener(); 68 ResultCode UnRegistSoftBusListener(); 69 70 ResultCode DeviceInit(); 71 void DeviceUnInit(); 72 ResultCode DoServiceSocketInit(); 73 void ServiceSocketInit(); 74 void ServiceSocketUnInit(); 75 76 ResultCode ServiceSocketListen(const int32_t socketId); 77 int32_t ClientSocketInit(const std::string &connectionName, const std::string &networkId); 78 ResultCode ClientSocketBind(const int32_t socketId); 79 bool CheckAndCopyStr(char *dest, uint32_t destLen, const std::string &src); 80 void AddConnection(const std::string &connectionName, std::shared_ptr<BaseSocket> &socket); 81 void DeleteConnection(const std::string &connectionName); 82 void AddSocket(const int32_t socketId, std::shared_ptr<BaseSocket> &socket); 83 void DeleteSocket(const int32_t socketId); 84 void SetServerSocket(std::shared_ptr<BaseSocket> &socket); 85 void ClearServerSocket(); 86 ResultCode DoOpenConnectionInner(const std::string &connectionName, const uint32_t tokenId, 87 const std::string &networkId); 88 89 std::recursive_mutex mutex_; 90 bool inited_ = false; 91 92 std::recursive_mutex ServerSocketMutex_; 93 std::shared_ptr<BaseSocket> serverSocket_ {nullptr}; 94 95 std::recursive_mutex socketMutex_; 96 std::map<int32_t, std::shared_ptr<BaseSocket>> socketMap_; 97 98 std::recursive_mutex connectionMutex_; 99 /* <ConnectionName, std::shared_ptr<BaseSocket>> */ 100 std::map<std::string, std::shared_ptr<BaseSocket>> clientSocketMap_; 101 102 std::recursive_mutex deviceManagerMutex_; 103 sptr<SystemAbilityListener> deviceManagerServiceListener_ {nullptr}; 104 105 std::recursive_mutex softBusMutex_; 106 sptr<SystemAbilityListener> softBusServiceListener_ {nullptr}; 107 }; 108 } // namespace UserAuth 109 } // namespace UserIam 110 } // namespace OHOS 111 #endif // IAM_SOFT_BUS_MANAGER_H