• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     std::pair<Status, int32_t> SendData(const PipeInfo &pipeInfo, const DeviceId &deviceId,
56         const DataInfo &dataInfo, uint32_t length, 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, bool isForce = true);
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     Status ReuseConnect(const PipeInfo &pipeInfo, const DeviceId &deviceId);
88 private:
89     using Time = std::chrono::steady_clock::time_point;
90     using Duration = std::chrono::steady_clock::duration;
91     using Task = ExecutorPool::Task;
92     std::string DelConnect(int32_t socket, bool isForce);
93     void StartCloseSessionTask(const std::string &deviceId);
94     Task GetCloseSessionTask();
95     bool CloseSession(const std::string &networkId);
96     void GetExpireTime(std::shared_ptr<SoftBusClient> &conn);
97     std::pair<Status, int32_t> OpenConnect(const std::shared_ptr<SoftBusClient> &conn, const DeviceId &deviceId);
98     std::shared_ptr<SoftBusClient> GetConnect(const PipeInfo &pipeInfo, const DeviceId &deviceId, uint32_t qosType);
99     static constexpr const char *PKG_NAME = "distributeddata-default";
100     static constexpr Time INVALID_NEXT = std::chrono::steady_clock::time_point::max();
101     static constexpr uint32_t QOS_COUNT = 3;
102     static constexpr QosTV Qos[QOS_COUNT] = { { .qos = QOS_TYPE_MIN_BW, .value = 64 * 1024 },
103         { .qos = QOS_TYPE_MAX_LATENCY, .value = 15000 }, { .qos = QOS_TYPE_MIN_LATENCY, .value = 1600 } };
104     static std::shared_ptr<SoftBusAdapter> instance_;
105     ConcurrentMap<std::string, const AppDataChangeListener *> dataChangeListeners_{};
106     ConcurrentMap<std::string, std::vector<std::shared_ptr<SoftBusClient>>> connects_;
107     bool flag_ = true; // only for br flag
108     std::function<void(const std::string &, const LevelInfo &)> onBroadcast_;
109     std::mutex taskMutex_;
110     ExecutorPool::TaskId taskId_ = ExecutorPool::INVALID_TASK_ID;
111     Time next_ = INVALID_NEXT;
112 
113     ConcurrentMap<int32_t, ServerSocketInfo> peerSocketInfos_;
114     ISocketListener clientListener_{};
115     ISocketListener serverListener_{};
116     int32_t socket_ = 0;
117 };
118 } // namespace AppDistributedKv
119 } // namespace OHOS
120 #endif /* DISTRIBUTEDDATAFWK_SRC_SOFTBUS_ADAPTER_H */