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 DISTRIBUTEDDATAFWK_SRC_SOFTBUS_ADAPTER_H 17 #define DISTRIBUTEDDATAFWK_SRC_SOFTBUS_ADAPTER_H 18 19 #include "app_data_change_listener.h" 20 #include "app_device_status_change_listener.h" 21 #include "concurrent_map.h" 22 #include "socket.h" 23 #include "task_scheduler.h" 24 25 namespace OHOS { 26 namespace ObjectStore { 27 class SoftBusAdapter { 28 public: 29 SoftBusAdapter(); 30 ~SoftBusAdapter(); 31 static std::shared_ptr<SoftBusAdapter> GetInstance(); 32 33 // add DeviceChangeListener to watch device change; 34 Status StartWatchDeviceChange(const AppDeviceStatusChangeListener *observer, const PipeInfo &pipeInfo); 35 // stop DeviceChangeListener to watch device change; 36 Status StopWatchDeviceChange(const AppDeviceStatusChangeListener *observer, const PipeInfo &pipeInfo); 37 void NotifyAll(const DeviceInfo &deviceInfo, const DeviceChangeType &type); 38 DeviceInfo GetLocalDevice(); 39 std::vector<DeviceInfo> GetDeviceList() const; 40 // get local device node information; 41 DeviceInfo GetLocalBasicInfo() const; 42 // get all remote connected device's node information; 43 std::vector<DeviceInfo> GetRemoteNodesBasicInfo() const; 44 static std::string ToBeAnonymous(const std::string &name); 45 46 // add DataChangeListener to watch data change; 47 Status StartWatchDataChange(const AppDataChangeListener *observer, const PipeInfo &pipeInfo); 48 49 // stop DataChangeListener to watch data change; 50 Status StopWatchDataChange(const AppDataChangeListener *observer, const PipeInfo &pipeInfo); 51 52 // Send data to other device, function will be called back after sent to notify send result. 53 Status SendData(const PipeInfo &pipeInfo, const DeviceId &deviceId, const DataInfo &dataInfo, uint32_t totalLength, 54 const MessageInfo &info); 55 56 bool IsSameStartedOnPeer(const struct PipeInfo &pipeInfo, const struct DeviceId &peer); 57 58 int CreateSessionServerAdapter(const std::string &sessionName); 59 60 int RemoveSessionServerAdapter(const std::string &sessionName) const; 61 62 void UpdateRelationship(const std::string &networkId, const DeviceChangeType &type); 63 64 void NotifyDataListeners(const uint8_t *ptr, const int size, const std::string &deviceId, const PipeInfo &pipeInfo); 65 66 void OnClientShutdown(int32_t socket); 67 68 void OnBind(int32_t socket, PeerSocketInfo info); 69 70 void OnServerShutdown(int32_t socket); 71 72 bool GetPeerSocketInfo(int32_t socket, PeerSocketInfo &info); 73 74 std::string ToNodeID(const std::string &nodeId) const; 75 76 private: 77 struct BytesMsg { 78 uint8_t *ptr; 79 uint32_t size; 80 }; 81 static constexpr uint32_t VECTOR_SIZE_THRESHOLD = 100; 82 static constexpr uint32_t QOS_COUNT = 3; 83 static constexpr QosTV Qos[QOS_COUNT] = { 84 { .qos = QOS_TYPE_MIN_BW, .value = 90 * 1024 * 1024 }, 85 { .qos = QOS_TYPE_MAX_LATENCY, .value = 10000 }, 86 { .qos = QOS_TYPE_MIN_LATENCY, .value = 2000 } }; 87 Status CacheData(const std::string &deviceId, const DataInfo &dataInfo); 88 void DoSend(); 89 int GetSocket(const PipeInfo &pipeInfo, const DeviceId &deviceId); 90 int CreateClientSocket(const PipeInfo &pipeInfo, const DeviceId &deviceId); 91 std::string GetSocketName(const std::string &socketName); 92 mutable std::mutex networkMutex_{}; 93 mutable std::map<std::string, std::string> networkId2Uuid_{}; 94 DeviceInfo localInfo_{}; 95 static std::shared_ptr<SoftBusAdapter> instance_; 96 std::mutex deviceChangeMutex_; 97 std::set<const AppDeviceStatusChangeListener *> listeners_{}; 98 std::mutex dataChangeMutex_{}; 99 std::map<std::string, const AppDataChangeListener *> dataChangeListeners_{}; 100 std::mutex sendDataMutex_{}; 101 std::mutex socketLock_; 102 std::mutex deviceDataLock_; 103 std::map<std::string, std::vector<BytesMsg>> dataCaches_; 104 std::shared_ptr<TaskScheduler> taskQueue_; 105 std::mutex localDeviceLock_{}; 106 std::map<std::string, int> sockets_; 107 int32_t socketServer_{0}; 108 ConcurrentMap<int32_t, PeerSocketInfo> peerSocketInfos_; 109 ISocketListener clientListener_{}; 110 ISocketListener serverListener_{}; 111 std::string appId_; 112 bool isSystemApp_ = false; 113 std::mutex bundleInfoMutex_{}; 114 }; 115 116 class AppDataListenerWrap { 117 public: 118 static void SetDataHandler(SoftBusAdapter *handler); 119 static void OnClientShutdown(int32_t socket, ShutdownReason reason); 120 static void OnClientBytesReceived(int32_t socket, const void *data, uint32_t dataLen); 121 122 static void OnServerBind(int32_t socket, PeerSocketInfo info); 123 static void OnServerShutdown(int32_t socket, ShutdownReason reason); 124 static void OnServerBytesReceived(int32_t socket, const void *data, uint32_t dataLen); 125 126 public: 127 // notifiy all listeners when received message 128 static void NotifyDataListeners( 129 const uint8_t *ptr, const int size, const std::string &deviceId, const PipeInfo &pipeInfo); 130 static SoftBusAdapter *softBusAdapter_; 131 }; 132 } // namespace ObjectStore 133 } // namespace OHOS 134 #endif /* DISTRIBUTEDDATAFWK_SRC_SOFTBUS_ADAPTER_H */ 135