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