• 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 
23 #include "app_data_change_listener.h"
24 #include "app_device_status_change_listener.h"
25 #include "app_types.h"
26 #include "session.h"
27 #include "softbus_bus_center.h"
28 #include "condition_lock.h"
29 
30 namespace OHOS {
31 namespace ObjectStore {
32 class SoftBusAdapter {
33 public:
34     SoftBusAdapter();
35     ~SoftBusAdapter();
36     static std::shared_ptr<SoftBusAdapter> GetInstance();
37 
38     // add DeviceChangeListener to watch device change;
39     Status StartWatchDeviceChange(const AppDeviceStatusChangeListener *observer, const PipeInfo &pipeInfo);
40     // stop DeviceChangeListener to watch device change;
41     Status StopWatchDeviceChange(const AppDeviceStatusChangeListener *observer, const PipeInfo &pipeInfo);
42     void NotifyAll(const DeviceInfo &deviceInfo, const DeviceChangeType &type);
43     DeviceInfo GetLocalDevice();
44     std::vector<DeviceInfo> GetDeviceList() const;
45     std::string GetUdidByNodeId(const std::string &nodeId) const;
46     // get local device node information;
47     DeviceInfo GetLocalBasicInfo() const;
48     // get all remote connected device's node information;
49     std::vector<DeviceInfo> GetRemoteNodesBasicInfo() const;
50     static std::string ToBeAnonymous(const std::string &name);
51 
52     // add DataChangeListener to watch data change;
53     Status StartWatchDataChange(const AppDataChangeListener *observer, const PipeInfo &pipeInfo);
54 
55     // stop DataChangeListener to watch data change;
56     Status StopWatchDataChange(const AppDataChangeListener *observer, const PipeInfo &pipeInfo);
57 
58     // Send data to other device, function will be called back after sent to notify send result.
59     Status SendData(
60         const PipeInfo &pipeInfo, const DeviceId &deviceId, const uint8_t *ptr, int size, const MessageInfo &info);
61 
62     bool IsSameStartedOnPeer(const struct PipeInfo &pipeInfo, const struct DeviceId &peer);
63 
64     void SetMessageTransFlag(const PipeInfo &pipeInfo, bool flag);
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 InsertSession(const std::string &sessionName);
73 
74     void DeleteSession(const std::string &sessionName);
75 
76     void NotifyDataListeners(const uint8_t *ptr, const int size, const std::string &deviceId, const PipeInfo &pipeInfo);
77 
78     std::string ToNodeID(const std::string &nodeId) const;
79 
80     int32_t GetSessionStatus(int32_t sessionId);
81 
82     void OnSessionOpen(int32_t sessionId, int32_t status);
83 
84     void OnSessionClose(int32_t sessionId);
85 
86 private:
87     std::shared_ptr<ConditionLock<int32_t>> GetSemaphore (int32_t sessinId);
88     mutable std::mutex networkMutex_{};
89     mutable std::map<std::string, std::string> networkId2Udid_{};
90     DeviceInfo localInfo_{};
91     static std::shared_ptr<SoftBusAdapter> instance_;
92     std::mutex deviceChangeMutex_;
93     std::set<const AppDeviceStatusChangeListener *> listeners_{};
94     std::mutex dataChangeMutex_{};
95     std::map<std::string, const AppDataChangeListener *> dataChangeListeners_{};
96     std::mutex busSessionMutex_{};
97     std::map<std::string, bool> busSessionMap_{};
98     bool flag_ = true; // only for br flag
99     ISessionListener sessionListener_{};
100     std::mutex statusMutex_ {};
101     std::map<int32_t, std::shared_ptr<ConditionLock<int32_t>>> sessionsStatus_;
102 };
103 } // namespace ObjectStore
104 } // namespace OHOS
105 #endif /* DISTRIBUTEDDATAFWK_SRC_SOFTBUS_ADAPTER_H */