• 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     DeviceInfo GetLocalBasicInfo();
55     std::string ToUUID(const std::string &id);
56     std::string ToUDID(const std::string &id);
57     static std::vector<std::string> ToUUID(const std::vector<std::string> &devices);
58     static std::vector<std::string> ToUUID(std::vector<DeviceInfo> devices);
59     std::string ToNetworkID(const std::string &id);
60     void NotifyReadyEvent(const std::string &uuid);
61     friend class DataMgrDmStateCall;
62 
63 private:
64     DeviceManagerAdapter();
65     ~DeviceManagerAdapter();
66     std::function<void()> RegDevCallback();
67     bool GetDeviceInfo(const DmDeviceInfo &dmInfo, DeviceInfo &dvInfo);
68     void SaveDeviceInfo(const DeviceInfo &deviceInfo, const AppDistributedKv::DeviceChangeType &type);
69     void UpdateDeviceInfo();
70     DeviceInfo GetDeviceInfoFromCache(const std::string &id);
71     bool Execute(KvStoreTask &&task);
72     void Online(const DmDeviceInfo &info);
73     void Offline(const DmDeviceInfo &info);
74     void OnChanged(const DmDeviceInfo &info);
75     void OnReady(const DmDeviceInfo &info);
76     void TimeOut(const std::string uuid);
77     std::vector<const AppDeviceChangeListener *> GetObservers();
78 
79     std::mutex devInfoMutex_ {};
80     DeviceInfo localInfo_ {};
81     ConcurrentMap<const AppDeviceChangeListener *, const AppDeviceChangeListener *> observers_ {};
82     LRUBucket<std::string, DeviceInfo> deviceInfos_ {64};
83     static constexpr int POOL_SIZE = 1;
84     std::shared_ptr<KvStoreThreadPool> threadPool_;
85     KvScheduler scheduler_ {1};
86     static constexpr int32_t SYNC_TIMEOUT = 10 * 1000; // ms
87     ConcurrentMap<std::string, std::string> syncTask_ {};
88 };
89 }  // namespace DistributedData
90 }  // namespace OHOS
91 #endif // DISTRIBUTEDDATAMGR_DATAMGR_DEVICE_MANAGER_ADAPTER_H
92