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 23 #include "app_data_change_listener.h" 24 #include "app_device_status_change_listener.h" 25 #include "app_types.h" 26 #include "session.h" 27 #include "softbus_bus_center.h" 28 #include "condition_lock.h" 29 30 namespace OHOS { 31 namespace ObjectStore { 32 class SoftBusAdapter { 33 public: 34 SoftBusAdapter(); 35 ~SoftBusAdapter(); 36 static std::shared_ptr<SoftBusAdapter> GetInstance(); 37 38 // add DeviceChangeListener to watch device change; 39 Status StartWatchDeviceChange(const AppDeviceStatusChangeListener *observer, const PipeInfo &pipeInfo); 40 // stop DeviceChangeListener to watch device change; 41 Status StopWatchDeviceChange(const AppDeviceStatusChangeListener *observer, const PipeInfo &pipeInfo); 42 void NotifyAll(const DeviceInfo &deviceInfo, const DeviceChangeType &type); 43 DeviceInfo GetLocalDevice(); 44 std::vector<DeviceInfo> GetDeviceList() const; 45 // get local device node information; 46 DeviceInfo GetLocalBasicInfo() const; 47 // get all remote connected device's node information; 48 std::vector<DeviceInfo> GetRemoteNodesBasicInfo() const; 49 static std::string ToBeAnonymous(const std::string &name); 50 51 // add DataChangeListener to watch data change; 52 Status StartWatchDataChange(const AppDataChangeListener *observer, const PipeInfo &pipeInfo); 53 54 // stop DataChangeListener to watch data change; 55 Status StopWatchDataChange(const AppDataChangeListener *observer, const PipeInfo &pipeInfo); 56 57 // Send data to other device, function will be called back after sent to notify send result. 58 Status SendData( 59 const PipeInfo &pipeInfo, const DeviceId &deviceId, const uint8_t *ptr, int size, const MessageInfo &info); 60 61 bool IsSameStartedOnPeer(const struct PipeInfo &pipeInfo, const struct DeviceId &peer); 62 63 void SetMessageTransFlag(const PipeInfo &pipeInfo, bool flag); 64 65 int CreateSessionServerAdapter(const std::string &sessionName); 66 67 int RemoveSessionServerAdapter(const std::string &sessionName) const; 68 69 void UpdateRelationship(const std::string &networkid, const DeviceChangeType &type); 70 71 void InsertSession(const std::string &sessionName); 72 73 void DeleteSession(const std::string &sessionName); 74 75 void NotifyDataListeners(const uint8_t *ptr, const int size, const std::string &deviceId, const PipeInfo &pipeInfo); 76 77 std::string ToNodeID(const std::string &nodeId) const; 78 79 int32_t GetSessionStatus(int32_t sessionId); 80 81 void OnSessionOpen(int32_t sessionId, int32_t status); 82 83 void OnSessionClose(int32_t sessionId); 84 85 private: 86 std::shared_ptr<ConditionLock<int32_t>> GetSemaphore (int32_t sessinId); 87 mutable std::mutex networkMutex_{}; 88 mutable std::map<std::string, std::string> networkId2Uuid_{}; 89 DeviceInfo localInfo_{}; 90 static std::shared_ptr<SoftBusAdapter> instance_; 91 std::mutex deviceChangeMutex_; 92 std::set<const AppDeviceStatusChangeListener *> listeners_{}; 93 std::mutex dataChangeMutex_{}; 94 std::map<std::string, const AppDataChangeListener *> dataChangeListeners_{}; 95 std::mutex busSessionMutex_{}; 96 std::map<std::string, bool> busSessionMap_{}; 97 bool flag_ = true; // only for br flag 98 ISessionListener sessionListener_{}; 99 std::mutex statusMutex_ {}; 100 std::map<int32_t, std::shared_ptr<ConditionLock<int32_t>>> sessionsStatus_; 101 }; 102 } // namespace ObjectStore 103 } // namespace OHOS 104 #endif /* DISTRIBUTEDDATAFWK_SRC_SOFTBUS_ADAPTER_H */