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