• 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 #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 "condition_lock.h"
29 #include "session.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     void SetMessageTransFlag(const PipeInfo &pipeInfo, bool flag);
67 
68     int CreateSessionServerAdapter(const std::string &sessionName);
69 
70     int RemoveSessionServerAdapter(const std::string &sessionName) const;
71 
72     void UpdateRelationship(const std::string &networkid, const DeviceChangeType &type);
73 
74     void InsertSession(const std::string &sessionName);
75 
76     void DeleteSession(const std::string &sessionName);
77 
78     void NotifyDataListeners(const uint8_t *ptr, const int size, const std::string &deviceId, const PipeInfo &pipeInfo);
79 
80     std::string ToNodeID(const std::string &nodeId) const;
81 
82     void OnSessionOpen(int32_t sessionId, int32_t status);
83 
84     void OnSessionClose(int32_t sessionId);
85 
86 private:
87     struct BytesMsg {
88         uint8_t *ptr;
89         uint32_t size;
90     };
91     static constexpr uint32_t P2P_SIZE_THRESHOLD = 0x10000u;    // 64KB
92     static constexpr uint32_t VECTOR_SIZE_THRESHOLD = 100;
93     static constexpr float SWITCH_DELAY_FACTOR = 0.6f;
94     Status CacheData(const std::string &deviceId, const DataInfo &dataInfo);
95     uint32_t GetSize(const std::string &deviceId);
96     RouteType GetRouteType(int sessionId);
97     RecOperate CalcRecOperate(uint32_t dataSize, RouteType currentRouteType, bool &isP2P) const;
98     void CacheSession(int32_t sessionId);
99     void DoSend();
100     int GetDeviceId(int sessionId, std::string &deviceId);
101     int GetSessionId(const std::string &deviceId);
102     SessionAttribute GetSessionAttribute(bool isP2P);
103     mutable std::mutex networkMutex_{};
104     mutable std::map<std::string, std::string> networkId2Uuid_{};
105     DeviceInfo localInfo_{};
106     static std::shared_ptr<SoftBusAdapter> instance_;
107     std::mutex deviceChangeMutex_;
108     std::set<const AppDeviceStatusChangeListener *> listeners_{};
109     std::mutex dataChangeMutex_{};
110     std::map<std::string, const AppDataChangeListener *> dataChangeListeners_{};
111     std::mutex busSessionMutex_{};
112     std::map<std::string, bool> busSessionMap_{};
113     bool flag_ = true; // only for br flag
114     ISessionListener sessionListener_{};
115     std::mutex statusMutex_{};
116     std::mutex sendDataMutex_{};
117     std::mutex mapLock_;
118     std::mutex deviceSessionLock_;
119     std::map<std::string, int> sessionIds_;
120     std::mutex deviceDataLock_;
121     std::map<std::string, std::vector<BytesMsg>> dataCaches_;
122     std::shared_ptr<TaskScheduler> taskQueue_;
123 };
124 
125 class AppDataListenerWrap {
126 public:
127     static void SetDataHandler(SoftBusAdapter *handler);
128     static int OnSessionOpened(int sessionId, int result);
129     static void OnSessionClosed(int sessionId);
130     static void OnMessageReceived(int sessionId, const void *data, unsigned int dataLen);
131     static void OnBytesReceived(int sessionId, const void *data, unsigned int dataLen);
132 
133 public:
134     // notifiy all listeners when received message
135     static void NotifyDataListeners(
136         const uint8_t *ptr, const int size, const std::string &deviceId, const PipeInfo &pipeInfo);
137     static SoftBusAdapter *softBusAdapter_;
138 };
139 } // namespace ObjectStore
140 } // namespace OHOS
141 #endif /* DISTRIBUTEDDATAFWK_SRC_SOFTBUS_ADAPTER_H */