• 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 DISTRIBUTEDDATAMGR_DATAMGR_DEVICE_MANAGER_ADAPTER_H
17 #define DISTRIBUTEDDATAMGR_DATAMGR_DEVICE_MANAGER_ADAPTER_H
18 
19 #include <set>
20 #include <string>
21 #include <vector>
22 #include "app_device_change_listener.h"
23 #include "commu_types.h"
24 #include "concurrent_map.h"
25 #include "device_manager.h"
26 #include "device_manager_callback.h"
27 #include "device_manager/device_manager_delegate.h"
28 #include "dm_device_info.h"
29 #include "executor_pool.h"
30 #include "lru_bucket.h"
31 
32 namespace OHOS {
33 namespace DistributedData {
34 class API_EXPORT DeviceManagerAdapter : public DeviceManagerDelegate {
35 public:
36     enum DeviceState {
37         DEVICE_ONLINE,
38         DEVICE_ONREADY,
39         DEVICE_BUTT
40     };
41     using DmDeviceInfo =  OHOS::DistributedHardware::DmDeviceInfo;
42     using DeviceInfo = OHOS::AppDistributedKv::DeviceInfo;
43     using PipeInfo = OHOS::AppDistributedKv::PipeInfo;
44     using AppDeviceChangeListener = OHOS::AppDistributedKv::AppDeviceChangeListener;
45     using Status = OHOS::DistributedKv::Status;
46     using Time = std::chrono::steady_clock::time_point;
47     using AccessCaller = OHOS::AppDistributedKv::AccessCaller;
48     using AccessCallee = OHOS::AppDistributedKv::AccessCallee;
49     static DeviceManagerAdapter &GetInstance();
50     static constexpr const char *CLOUD_DEVICE_UUID = "cloudDeviceUuid";
51     static constexpr const char *CLOUD_DEVICE_UDID = "cloudDeviceUdid";
52 
53     void Init(std::shared_ptr<ExecutorPool> executors);
54     Status StartWatchDeviceChange(const AppDeviceChangeListener *observer, const PipeInfo &pipeInfo);
55     Status StopWatchDeviceChange(const AppDeviceChangeListener *observer, const PipeInfo &pipeInfo);
56     DeviceInfo GetLocalDevice() override;
57     std::vector<DeviceInfo> GetRemoteDevices();
58     std::vector<DeviceInfo> GetOnlineDevices();
59     bool IsDeviceReady(const std::string &id);
60     bool IsOHOSType(const std::string &id);
61     std::string GetEncryptedUuidByNetworkId(const std::string &networkId);
62     size_t GetOnlineSize();
63     DeviceInfo GetDeviceInfo(const std::string &id);
64     std::string GetUuidByNetworkId(const std::string &networkId);
65     std::string GetUdidByNetworkId(const std::string &networkId);
66     std::string CalcClientUuid(const std::string &appId, const std::string &uuid);
67     std::string ToUUID(const std::string &id);
68     std::string ToUDID(const std::string &id);
69     static std::vector<std::string> ToUUID(const std::vector<std::string> &devices);
70     static std::vector<std::string> ToUUID(std::vector<DeviceInfo> devices);
71     std::string ToNetworkID(const std::string &id);
72     void NotifyReadyEvent(const std::string &uuid);
73     int32_t GetAuthType(const std::string& id);
74     bool IsSameAccount(const std::string &id);
75     bool IsSameAccount(const AccessCaller &accCaller, const AccessCallee &accCallee);
76     bool CheckAccessControl(const AccessCaller &accCaller, const AccessCallee &accCallee);
77     void Offline(const DmDeviceInfo &info);
78     void OnReady(const DmDeviceInfo &info);
79     friend class DataMgrDmStateCall;
80     friend class NetConnCallbackObserver;
81 
82 private:
83     DeviceManagerAdapter();
84     ~DeviceManagerAdapter();
85     std::function<void()> RegDevCallback();
86     bool GetDeviceInfo(const DmDeviceInfo &dmInfo, DeviceInfo &dvInfo);
87     void SaveDeviceInfo(const DeviceInfo &deviceInfo, const AppDistributedKv::DeviceChangeType &type);
88     void InitDeviceInfo(bool onlyCache = true);
89     DeviceInfo GetLocalDeviceInfo();
90     DeviceInfo GetDeviceInfoFromCache(const std::string &id);
91     void Online(const DmDeviceInfo &info);
92     void OnChanged(const DmDeviceInfo &info);
93     std::vector<const AppDeviceChangeListener *> GetObservers();
94     void ResetLocalDeviceInfo();
GetTimeStamp()95     static inline uint64_t GetTimeStamp()
96     {
97         return std::chrono::duration_cast<std::chrono::milliseconds>(
98             std::chrono::steady_clock::now().time_since_epoch())
99             .count();
100     }
101 
102     std::mutex devInfoMutex_ {};
103     DeviceInfo localInfo_ {};
104     const DmDeviceInfo cloudDmInfo;
105     ConcurrentMap<const AppDeviceChangeListener *, const AppDeviceChangeListener *> observers_ {};
106     LRUBucket<std::string, DeviceInfo> deviceInfos_ {64};
107     LRUBucket<std::string, DeviceInfo> otherDeviceInfos_ {64};
108     static constexpr size_t TIME_TASK_CAPACITY = 50;
109     static constexpr int32_t SYNC_TIMEOUT = 60 * 1000; // ms
110     static constexpr int32_t OH_OS_TYPE = 10;
111     ConcurrentMap<std::string, std::string> syncTask_ {};
112     std::shared_ptr<ExecutorPool> executors_;
113     ConcurrentMap<std::string, std::pair<DeviceState, DeviceInfo>> readyDevices_;
114 };
115 }  // namespace DistributedData
116 }  // namespace OHOS
117 #endif // DISTRIBUTEDDATAMGR_DATAMGR_DEVICE_MANAGER_ADAPTER_H