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 Status SendData(const PipeInfo &pipeInfo, const DeviceId &deviceId, const DataInfo &dataInfo, uint32_t length, 57 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); 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 int32_t Broadcast(const PipeInfo &pipeInfo, uint16_t mask); 78 void OnBroadcast(const DeviceId &device, uint16_t mask); 79 int32_t ListenBroadcastMsg(const PipeInfo &pipeInfo, std::function<void(const std::string &, uint16_t)> 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 Task GetCloseSessionTask(); 93 static constexpr Time INVALID_NEXT = std::chrono::steady_clock::time_point::max(); 94 static constexpr uint32_t QOS_COUNT = 3; 95 static constexpr QosTV Qos[QOS_COUNT] = { { .qos = QOS_TYPE_MIN_BW, .value = 64 * 1024 }, 96 { .qos = QOS_TYPE_MAX_LATENCY, .value = 15000 }, { .qos = QOS_TYPE_MIN_LATENCY, .value = 1600 } }; 97 static std::shared_ptr<SoftBusAdapter> instance_; 98 ConcurrentMap<std::string, const AppDataChangeListener *> dataChangeListeners_{}; 99 ConcurrentMap<std::string, std::vector<std::shared_ptr<SoftBusClient>>> connects_; 100 bool flag_ = true; // only for br flag 101 std::function<void(const std::string &, uint16_t)> onBroadcast_; 102 std::mutex taskMutex_; 103 ExecutorPool::TaskId taskId_ = ExecutorPool::INVALID_TASK_ID; 104 Time next_ = INVALID_NEXT; 105 106 ConcurrentMap<int32_t, ServerSocketInfo> peerSocketInfos_; 107 ISocketListener clientListener_{}; 108 ISocketListener serverListener_{}; 109 int32_t socket_ = 0; 110 }; 111 } // namespace AppDistributedKv 112 } // namespace OHOS 113 #endif /* DISTRIBUTEDDATAFWK_SRC_SOFTBUS_ADAPTER_H */