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 static DeviceManagerAdapter &GetInstance(); 47 static constexpr const char *CLOUD_DEVICE_UUID = "cloudDeviceUuid"; 48 static constexpr const char *CLOUD_DEVICE_UDID = "cloudDeviceUdid"; 49 50 void Init(std::shared_ptr<ExecutorPool> executors); 51 Status StartWatchDeviceChange(const AppDeviceChangeListener *observer, const PipeInfo &pipeInfo); 52 Status StopWatchDeviceChange(const AppDeviceChangeListener *observer, const PipeInfo &pipeInfo); 53 DeviceInfo GetLocalDevice(); 54 std::vector<DeviceInfo> GetRemoteDevices(); 55 std::vector<DeviceInfo> GetOnlineDevices(); 56 bool IsDeviceReady(const std::string &id); 57 size_t GetOnlineSize(); 58 DeviceInfo GetDeviceInfo(const std::string &id); 59 std::string GetUuidByNetworkId(const std::string &networkId); 60 std::string GetUdidByNetworkId(const std::string &networkId); 61 std::string CalcClientUuid(const std::string &appId, const std::string &uuid); 62 std::string ToUUID(const std::string &id); 63 std::string ToUDID(const std::string &id); 64 static std::vector<std::string> ToUUID(const std::vector<std::string> &devices); 65 static std::vector<std::string> ToUUID(std::vector<DeviceInfo> devices); 66 std::string ToNetworkID(const std::string &id); 67 void NotifyReadyEvent(const std::string &uuid); 68 bool IsNetworkAvailable(); 69 friend class DataMgrDmStateCall; 70 friend class NetConnCallbackObserver; 71 72 private: 73 DeviceManagerAdapter(); 74 ~DeviceManagerAdapter(); 75 std::function<void()> RegDevCallback(); 76 bool RegOnNetworkChange(); 77 bool SetNetAvailable(bool isNetAvailable); 78 bool GetDeviceInfo(const DmDeviceInfo &dmInfo, DeviceInfo &dvInfo); 79 void SaveDeviceInfo(const DeviceInfo &deviceInfo, const AppDistributedKv::DeviceChangeType &type); 80 void InitDeviceInfo(bool onlyCache = true); 81 DeviceInfo GetLocalDeviceInfo(); 82 DeviceInfo GetDeviceInfoFromCache(const std::string &id); 83 void Online(const DmDeviceInfo &info); 84 void Offline(const DmDeviceInfo &info); 85 void OnChanged(const DmDeviceInfo &info); 86 void OnReady(const DmDeviceInfo &info); 87 void TimeOut(const std::string uuid); 88 std::vector<const AppDeviceChangeListener *> GetObservers(); 89 90 std::mutex devInfoMutex_ {}; 91 DeviceInfo localInfo_ {}; 92 const DmDeviceInfo cloudDmInfo; 93 ConcurrentMap<const AppDeviceChangeListener *, const AppDeviceChangeListener *> observers_ {}; 94 LRUBucket<std::string, DeviceInfo> deviceInfos_ {64}; 95 static constexpr size_t TIME_TASK_CAPACITY = 50; 96 static constexpr int32_t SYNC_TIMEOUT = 60 * 1000; // ms 97 ConcurrentMap<std::string, std::string> syncTask_ {}; 98 std::shared_ptr<ExecutorPool> executors_; 99 mutable std::shared_mutex mutex_; 100 static constexpr int32_t EFFECTIVE_DURATION = 30 * 1000; // ms 101 Time expireTime_ = std::chrono::steady_clock::now(); 102 bool isNetAvailable_ = false; 103 ConcurrentMap<std::string, std::pair<DeviceState, DeviceInfo>> readyDevices_; 104 }; 105 } // namespace DistributedData 106 } // namespace OHOS 107 #endif // DISTRIBUTEDDATAMGR_DATAMGR_DEVICE_MANAGER_ADAPTER_H