1 /* 2 * Copyright (c) 2021 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 <concurrent_map.h> 19 #include <condition_variable> 20 #include <functional> 21 #include <map> 22 #include <memory> 23 #include <mutex> 24 #include <set> 25 #include <tuple> 26 #include <vector> 27 28 #include "app_data_change_listener.h" 29 #include "app_device_change_listener.h" 30 #include "block_data.h" 31 #include "session.h" 32 #include "socket.h" 33 #include "softbus_bus_center.h" 34 #include "softbus_client.h" 35 namespace OHOS { 36 namespace AppDistributedKv { 37 class SoftBusAdapter : public AppDistributedKv::AppDeviceChangeListener { 38 public: 39 SoftBusAdapter(); 40 ~SoftBusAdapter(); 41 static std::shared_ptr<SoftBusAdapter> GetInstance(); 42 43 struct ServerSocketInfo { 44 std::string name; /**< Peer socket name */ 45 std::string networkId; /**< Peer network ID */ 46 std::string pkgName; /**< Peer package name */ 47 }; 48 49 // add DataChangeListener to watch data change; 50 Status StartWatchDataChange(const AppDataChangeListener *observer, const PipeInfo &pipeInfo); 51 52 // stop DataChangeListener to watch data change; 53 Status StopWatchDataChange(const AppDataChangeListener *observer, const PipeInfo &pipeInfo); 54 55 // Send data to other device, function will be called back after sent to notify send result. 56 std::pair<Status, int32_t> SendData(const PipeInfo &pipeInfo, const DeviceId &deviceId, 57 const DataInfo &dataInfo, uint32_t length, const MessageInfo &info); 58 59 bool IsSameStartedOnPeer(const struct PipeInfo &pipeInfo, const struct DeviceId &peer); 60 61 void SetMessageTransFlag(const PipeInfo &pipeInfo, bool flag); 62 63 int CreateSessionServerAdapter(const std::string &sessionName); 64 65 int RemoveSessionServerAdapter(const std::string &sessionName) const; 66 67 void NotifyDataListeners(const uint8_t *data, int size, const std::string &deviceId, const PipeInfo &pipeInfo); 68 69 std::string OnClientShutdown(int32_t socket, bool isForce = true); 70 71 void OnBind(int32_t socket, PeerSocketInfo info); 72 73 void OnServerShutdown(int32_t socket); 74 75 bool GetPeerSocketInfo(int32_t socket, ServerSocketInfo &info); 76 77 Status Broadcast(const PipeInfo &pipeInfo, const LevelInfo &levelInfo); 78 void OnBroadcast(const DeviceId &device, const LevelInfo &levelInfo); 79 int32_t ListenBroadcastMsg(const PipeInfo &pipeInfo, 80 std::function<void(const std::string &, const LevelInfo &)> listener); 81 82 uint32_t GetMtuSize(const DeviceId &deviceId); 83 uint32_t GetTimeout(const DeviceId &deviceId); 84 85 void OnDeviceChanged(const AppDistributedKv::DeviceInfo &info, 86 const AppDistributedKv::DeviceChangeType &type) const override; 87 private: 88 using Time = std::chrono::steady_clock::time_point; 89 using Duration = std::chrono::steady_clock::duration; 90 using Task = ExecutorPool::Task; 91 std::string DelConnect(int32_t socket, bool isForce); 92 void StartCloseSessionTask(const std::string &deviceId); 93 Task GetCloseSessionTask(); 94 bool CloseSession(const std::string &networkId); 95 void GetExpireTime(std::shared_ptr<SoftBusClient> &conn); 96 std::pair<Status, int32_t> OpenConnect(const std::shared_ptr<SoftBusClient> &conn, const DeviceId &deviceId); 97 static constexpr const char *PKG_NAME = "distributeddata-default"; 98 static constexpr Time INVALID_NEXT = std::chrono::steady_clock::time_point::max(); 99 static constexpr uint32_t QOS_COUNT = 3; 100 static constexpr QosTV Qos[QOS_COUNT] = { { .qos = QOS_TYPE_MIN_BW, .value = 64 * 1024 }, 101 { .qos = QOS_TYPE_MAX_LATENCY, .value = 15000 }, { .qos = QOS_TYPE_MIN_LATENCY, .value = 1600 } }; 102 static std::shared_ptr<SoftBusAdapter> instance_; 103 ConcurrentMap<std::string, const AppDataChangeListener *> dataChangeListeners_{}; 104 ConcurrentMap<std::string, std::vector<std::shared_ptr<SoftBusClient>>> connects_; 105 bool flag_ = true; // only for br flag 106 std::function<void(const std::string &, const LevelInfo &)> onBroadcast_; 107 std::mutex taskMutex_; 108 ExecutorPool::TaskId taskId_ = ExecutorPool::INVALID_TASK_ID; 109 Time next_ = INVALID_NEXT; 110 111 ConcurrentMap<int32_t, ServerSocketInfo> peerSocketInfos_; 112 ISocketListener clientListener_{}; 113 ISocketListener serverListener_{}; 114 int32_t socket_ = 0; 115 }; 116 } // namespace AppDistributedKv 117 } // namespace OHOS 118 #endif /* DISTRIBUTEDDATAFWK_SRC_SOFTBUS_ADAPTER_H */