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