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 #include <vector>
23 
24 #include "app_data_change_listener.h"
25 #include "app_device_status_change_listener.h"
26 #include "app_types.h"
27 #include "concurrent_map.h"
28 #include "session.h"
29 #include "socket.h"
30 #include "softbus_bus_center.h"
31 #include "task_scheduler.h"
32 
33 namespace OHOS {
34 namespace ObjectStore {
35 class SoftBusAdapter {
36 public:
37     SoftBusAdapter();
38     ~SoftBusAdapter();
39     static std::shared_ptr<SoftBusAdapter> GetInstance();
40 
41     // add DeviceChangeListener to watch device change;
42     Status StartWatchDeviceChange(const AppDeviceStatusChangeListener *observer, const PipeInfo &pipeInfo);
43     // stop DeviceChangeListener to watch device change;
44     Status StopWatchDeviceChange(const AppDeviceStatusChangeListener *observer, const PipeInfo &pipeInfo);
45     void NotifyAll(const DeviceInfo &deviceInfo, const DeviceChangeType &type);
46     DeviceInfo GetLocalDevice();
47     std::vector<DeviceInfo> GetDeviceList() const;
48     // get local device node information;
49     DeviceInfo GetLocalBasicInfo() const;
50     // get all remote connected device's node information;
51     std::vector<DeviceInfo> GetRemoteNodesBasicInfo() const;
52     static std::string ToBeAnonymous(const std::string &name);
53 
54     // add DataChangeListener to watch data change;
55     Status StartWatchDataChange(const AppDataChangeListener *observer, const PipeInfo &pipeInfo);
56 
57     // stop DataChangeListener to watch data change;
58     Status StopWatchDataChange(const AppDataChangeListener *observer, const PipeInfo &pipeInfo);
59 
60     // Send data to other device, function will be called back after sent to notify send result.
61     Status SendData(const PipeInfo &pipeInfo, const DeviceId &deviceId, const DataInfo &dataInfo, uint32_t totalLength,
62         const MessageInfo &info);
63 
64     bool IsSameStartedOnPeer(const struct PipeInfo &pipeInfo, const struct DeviceId &peer);
65 
66     int CreateSessionServerAdapter(const std::string &sessionName);
67 
68     int RemoveSessionServerAdapter(const std::string &sessionName) const;
69 
70     void UpdateRelationship(const std::string &networkId, const DeviceChangeType &type);
71 
72     void NotifyDataListeners(const uint8_t *ptr, const int size, const std::string &deviceId, const PipeInfo &pipeInfo);
73 
74     void OnClientShutdown(int32_t socket);
75 
76     void OnBind(int32_t socket, PeerSocketInfo info);
77 
78     void OnServerShutdown(int32_t socket);
79 
80     bool GetPeerSocketInfo(int32_t socket, PeerSocketInfo &info);
81 
82     std::string ToNodeID(const std::string &nodeId) const;
83 
84 private:
85     struct BytesMsg {
86         uint8_t *ptr;
87         uint32_t size;
88     };
89     static constexpr uint32_t VECTOR_SIZE_THRESHOLD = 100;
90     static constexpr uint32_t QOS_COUNT = 3;
91     static constexpr QosTV Qos[QOS_COUNT] = {
92         { .qos = QOS_TYPE_MIN_BW, .value = 90 * 1024 * 1024 },
93         { .qos = QOS_TYPE_MAX_LATENCY, .value = 10000 },
94         { .qos = QOS_TYPE_MIN_LATENCY, .value = 2000 } };
95     Status CacheData(const std::string &deviceId, const DataInfo &dataInfo);
96     void DoSend();
97     int GetSocket(const PipeInfo &pipeInfo, const DeviceId &deviceId);
98     int CreateClientSocket(const PipeInfo &pipeInfo, const DeviceId &deviceId);
99     std::string GetSocketName(const std::string &socketName);
100     mutable std::mutex networkMutex_{};
101     mutable std::map<std::string, std::string> networkId2Uuid_{};
102     DeviceInfo localInfo_{};
103     static std::shared_ptr<SoftBusAdapter> instance_;
104     std::mutex deviceChangeMutex_;
105     std::set<const AppDeviceStatusChangeListener *> listeners_{};
106     std::mutex dataChangeMutex_{};
107     std::map<std::string, const AppDataChangeListener *> dataChangeListeners_{};
108     std::mutex sendDataMutex_{};
109     std::mutex socketLock_;
110     std::mutex deviceDataLock_;
111     std::map<std::string, std::vector<BytesMsg>> dataCaches_;
112     std::shared_ptr<TaskScheduler> taskQueue_;
113     std::mutex localDeviceLock_{};
114     std::map<std::string, int> sockets_;
115     int32_t socketServer_{0};
116     ConcurrentMap<int32_t, PeerSocketInfo> peerSocketInfos_;
117     ISocketListener clientListener_{};
118     ISocketListener serverListener_{};
119     std::string appId_;
120     bool isSystemApp_ = false;
121     std::mutex bundleInfoMutex_{};
122 };
123 
124 class AppDataListenerWrap {
125 public:
126     static void SetDataHandler(SoftBusAdapter *handler);
127     static void OnClientShutdown(int32_t socket, ShutdownReason reason);
128     static void OnClientBytesReceived(int32_t socket, const void *data, uint32_t dataLen);
129 
130     static void OnServerBind(int32_t socket, PeerSocketInfo info);
131     static void OnServerShutdown(int32_t socket, ShutdownReason reason);
132     static void OnServerBytesReceived(int32_t socket, const void *data, uint32_t dataLen);
133 
134 public:
135     // notifiy all listeners when received message
136     static void NotifyDataListeners(
137         const uint8_t *ptr, const int size, const std::string &deviceId, const PipeInfo &pipeInfo);
138     static SoftBusAdapter *softBusAdapter_;
139 };
140 } // namespace ObjectStore
141 } // namespace OHOS
142 #endif /* DISTRIBUTEDDATAFWK_SRC_SOFTBUS_ADAPTER_H */
143