• 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 "task_scheduler.h"
29 #include "kv_store_task.h"
30 #include "kv_store_thread_pool.h"
31 #include "lru_bucket.h"
32 
33 namespace OHOS {
34 namespace DistributedData {
35 class API_EXPORT DeviceManagerAdapter {
36 public:
37     using DmDeviceInfo =  OHOS::DistributedHardware::DmDeviceInfo;
38     using KvStoreTask = OHOS::DistributedKv::KvStoreTask;
39     using KvStoreThreadPool = OHOS::DistributedKv::KvStoreThreadPool;
40     using KvScheduler = OHOS::TaskScheduler;
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     static DeviceManagerAdapter &GetInstance();
46     void Init();
47     Status StartWatchDeviceChange(const AppDeviceChangeListener *observer, const PipeInfo &pipeInfo);
48     Status StopWatchDeviceChange(const AppDeviceChangeListener *observer, const PipeInfo &pipeInfo);
49     DeviceInfo GetLocalDevice();
50     std::vector<DeviceInfo> GetRemoteDevices();
51     DeviceInfo GetDeviceInfo(const std::string &id);
52     std::string GetUuidByNetworkId(const std::string &networkId);
53     std::string GetUdidByNetworkId(const std::string &networkId);
54     std::string CalcClientUuid(const std::string &appId, const std::string &uuid);
55     DeviceInfo GetLocalBasicInfo();
56     std::string ToUUID(const std::string &id);
57     std::string ToUDID(const std::string &id);
58     static std::vector<std::string> ToUUID(const std::vector<std::string> &devices);
59     static std::vector<std::string> ToUUID(std::vector<DeviceInfo> devices);
60     std::string ToNetworkID(const std::string &id);
61     void NotifyReadyEvent(const std::string &uuid);
62     friend class DataMgrDmStateCall;
63 
64 private:
65     DeviceManagerAdapter();
66     ~DeviceManagerAdapter();
67     std::function<void()> RegDevCallback();
68     bool GetDeviceInfo(const DmDeviceInfo &dmInfo, DeviceInfo &dvInfo);
69     void SaveDeviceInfo(const DeviceInfo &deviceInfo, const AppDistributedKv::DeviceChangeType &type);
70     void UpdateDeviceInfo();
71     DeviceInfo GetDeviceInfoFromCache(const std::string &id);
72     bool Execute(KvStoreTask &&task);
73     void Online(const DmDeviceInfo &info);
74     void Offline(const DmDeviceInfo &info);
75     void OnChanged(const DmDeviceInfo &info);
76     void OnReady(const DmDeviceInfo &info);
77     void TimeOut(const std::string uuid);
78     std::vector<const AppDeviceChangeListener *> GetObservers();
79 
80     std::mutex devInfoMutex_ {};
81     DeviceInfo localInfo_ {};
82     ConcurrentMap<const AppDeviceChangeListener *, const AppDeviceChangeListener *> observers_ {};
83     LRUBucket<std::string, DeviceInfo> deviceInfos_ {64};
84     static constexpr int POOL_SIZE = 1;
85     std::shared_ptr<KvStoreThreadPool> threadPool_;
86     KvScheduler scheduler_ {1};
87     static constexpr int32_t SYNC_TIMEOUT = 10 * 1000; // ms
88     ConcurrentMap<std::string, std::string> syncTask_ {};
89 };
90 }  // namespace DistributedData
91 }  // namespace OHOS
92 #endif // DISTRIBUTEDDATAMGR_DATAMGR_DEVICE_MANAGER_ADAPTER_H
93