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 #include <map> 19 #include <memory> 20 #include <set> 21 #include <tuple> 22 #include <vector> 23 24 #include "app_data_change_listener.h" 25 #include "app_device_status_change_listener.h" 26 #include "app_types.h" 27 #include "concurrent_map.h" 28 #include "session.h" 29 #include "softbus_bus_center.h" 30 #include "task_scheduler.h" 31 32 namespace OHOS { 33 namespace ObjectStore { 34 class SoftBusAdapter { 35 public: 36 SoftBusAdapter(); 37 ~SoftBusAdapter(); 38 static std::shared_ptr<SoftBusAdapter> GetInstance(); 39 40 // add DeviceChangeListener to watch device change; 41 Status StartWatchDeviceChange(const AppDeviceStatusChangeListener *observer, const PipeInfo &pipeInfo); 42 // stop DeviceChangeListener to watch device change; 43 Status StopWatchDeviceChange(const AppDeviceStatusChangeListener *observer, const PipeInfo &pipeInfo); 44 void NotifyAll(const DeviceInfo &deviceInfo, const DeviceChangeType &type); 45 DeviceInfo GetLocalDevice(); 46 std::vector<DeviceInfo> GetDeviceList() const; 47 // get local device node information; 48 DeviceInfo GetLocalBasicInfo() const; 49 // get all remote connected device's node information; 50 std::vector<DeviceInfo> GetRemoteNodesBasicInfo() const; 51 static std::string ToBeAnonymous(const std::string &name); 52 53 // add DataChangeListener to watch data change; 54 Status StartWatchDataChange(const AppDataChangeListener *observer, const PipeInfo &pipeInfo); 55 56 // stop DataChangeListener to watch data change; 57 Status StopWatchDataChange(const AppDataChangeListener *observer, const PipeInfo &pipeInfo); 58 59 // Send data to other device, function will be called back after sent to notify send result. 60 Status SendData(const PipeInfo &pipeInfo, const DeviceId &deviceId, const DataInfo &dataInfo, uint32_t totalLength, 61 const MessageInfo &info); 62 63 bool IsSameStartedOnPeer(const struct PipeInfo &pipeInfo, const struct DeviceId &peer); 64 65 void SetMessageTransFlag(const PipeInfo &pipeInfo, bool flag); 66 67 int CreateSessionServerAdapter(const std::string &sessionName); 68 69 int RemoveSessionServerAdapter(const std::string &sessionName) const; 70 71 void UpdateRelationship(const std::string &networkid, const DeviceChangeType &type); 72 73 void InsertSession(const std::string &sessionName); 74 75 void DeleteSession(const std::string &sessionName); 76 77 void NotifyDataListeners(const uint8_t *ptr, const int size, const std::string &deviceId, const PipeInfo &pipeInfo); 78 79 std::string ToNodeID(const std::string &nodeId) const; 80 81 void OnSessionOpen(int32_t sessionId, int32_t status); 82 83 void OnSessionClose(int32_t sessionId); 84 85 private: 86 struct BytesMsg { 87 uint8_t *ptr; 88 uint32_t size; 89 }; 90 static constexpr uint32_t P2P_SIZE_THRESHOLD = 0x10000u; // 64KB 91 static constexpr uint32_t VECTOR_SIZE_THRESHOLD = 100; 92 static constexpr float SWITCH_DELAY_FACTOR = 0.6f; 93 Status CacheData(const std::string &deviceId, const DataInfo &dataInfo); 94 uint32_t GetSize(const std::string &deviceId); 95 RouteType GetRouteType(int sessionId); 96 RecOperate CalcRecOperate(uint32_t dataSize, RouteType currentRouteType, bool &isP2P) const; 97 void CacheSession(int32_t sessionId); 98 void DoSend(); 99 int GetDeviceId(int sessionId, std::string &deviceId); 100 int GetSessionId(const std::string &deviceId); 101 SessionAttribute GetSessionAttribute(bool isP2P); 102 mutable std::mutex networkMutex_{}; 103 mutable std::map<std::string, std::string> networkId2Uuid_{}; 104 DeviceInfo localInfo_{}; 105 static std::shared_ptr<SoftBusAdapter> instance_; 106 std::mutex deviceChangeMutex_; 107 std::set<const AppDeviceStatusChangeListener *> listeners_{}; 108 std::mutex dataChangeMutex_{}; 109 std::map<std::string, const AppDataChangeListener *> dataChangeListeners_{}; 110 std::mutex busSessionMutex_{}; 111 std::map<std::string, bool> busSessionMap_{}; 112 bool flag_ = true; // only for br flag 113 ISessionListener sessionListener_{}; 114 std::mutex statusMutex_{}; 115 std::mutex sendDataMutex_{}; 116 std::mutex mapLock_; 117 std::mutex deviceSessionLock_; 118 std::map<std::string, int> sessionIds_; 119 std::mutex deviceDataLock_; 120 std::map<std::string, std::vector<BytesMsg>> dataCaches_; 121 std::shared_ptr<TaskScheduler> taskQueue_; 122 }; 123 124 class AppDataListenerWrap { 125 public: 126 static void SetDataHandler(SoftBusAdapter *handler); 127 static int OnSessionOpened(int sessionId, int result); 128 static void OnSessionClosed(int sessionId); 129 static void OnMessageReceived(int sessionId, const void *data, unsigned int dataLen); 130 static void OnBytesReceived(int sessionId, const void *data, unsigned int dataLen); 131 132 public: 133 // notifiy all listeners when received message 134 static void NotifyDataListeners( 135 const uint8_t *ptr, const int size, const std::string &deviceId, const PipeInfo &pipeInfo); 136 static SoftBusAdapter *softBusAdapter_; 137 }; 138 } // namespace ObjectStore 139 } // namespace OHOS 140 #endif /* DISTRIBUTEDDATAFWK_SRC_SOFTBUS_ADAPTER_H */