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