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 using DmDeviceInfo = OHOS::DistributedHardware::DmDeviceInfo; 36 using DeviceInfo = OHOS::AppDistributedKv::DeviceInfo; 37 using PipeInfo = OHOS::AppDistributedKv::PipeInfo; 38 using AppDeviceChangeListener = OHOS::AppDistributedKv::AppDeviceChangeListener; 39 using Status = OHOS::DistributedKv::Status; 40 static DeviceManagerAdapter &GetInstance(); 41 static constexpr const char *CLOUD_DEVICE_UUID = "cloudDeviceUuid"; 42 static constexpr const char *CLOUD_DEVICE_UDID = "cloudDeviceUdid"; 43 44 void Init(std::shared_ptr<ExecutorPool> executors); 45 Status StartWatchDeviceChange(const AppDeviceChangeListener *observer, const PipeInfo &pipeInfo); 46 Status StopWatchDeviceChange(const AppDeviceChangeListener *observer, const PipeInfo &pipeInfo); 47 DeviceInfo GetLocalDevice(); 48 std::vector<DeviceInfo> GetRemoteDevices(); 49 DeviceInfo GetDeviceInfo(const std::string &id); 50 std::string GetUuidByNetworkId(const std::string &networkId); 51 std::string GetUdidByNetworkId(const std::string &networkId); 52 std::string CalcClientUuid(const std::string &appId, const std::string &uuid); 53 std::string ToUUID(const std::string &id); 54 std::string ToUDID(const std::string &id); 55 static std::vector<std::string> ToUUID(const std::vector<std::string> &devices); 56 static std::vector<std::string> ToUUID(std::vector<DeviceInfo> devices); 57 std::string ToNetworkID(const std::string &id); 58 void NotifyReadyEvent(const std::string &uuid); 59 bool IsNetworkAvailable(); 60 friend class DataMgrDmStateCall; 61 friend class NetConnCallbackObserver; 62 63 private: 64 DeviceManagerAdapter(); 65 ~DeviceManagerAdapter(); 66 std::function<void()> RegDevCallback(); 67 bool RegOnNetworkChange(); 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 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 const DmDeviceInfo cloudDmInfo; 82 ConcurrentMap<const AppDeviceChangeListener *, const AppDeviceChangeListener *> observers_ {}; 83 LRUBucket<std::string, DeviceInfo> deviceInfos_ {64}; 84 static constexpr size_t TIME_TASK_CAPACITY = 50; 85 static constexpr int32_t SYNC_TIMEOUT = 10 * 1000; // ms 86 ConcurrentMap<std::string, std::string> syncTask_ {}; 87 std::shared_ptr<ExecutorPool> executors_; 88 bool isNetAvailable_ = false; 89 }; 90 } // namespace DistributedData 91 } // namespace OHOS 92 #endif // DISTRIBUTEDDATAMGR_DATAMGR_DEVICE_MANAGER_ADAPTER_H 93