1 /* 2 * Copyright (c) 2021 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 OHOS_DEVICE_PROFILE_DEVICE_MANAGER_H 17 #define OHOS_DEVICE_PROFILE_DEVICE_MANAGER_H 18 19 #include <mutex> 20 #include <list> 21 #include <string> 22 23 #include "event_handler.h" 24 #include "single_instance.h" 25 #include "device_info.h" 26 #include "device_manager.h" 27 28 namespace OHOS { 29 namespace DeviceProfile { 30 enum class DeviceIdType : uint8_t { 31 NETWORKID = 0, 32 UDID = 1, 33 UUID = 2 34 }; 35 36 class DpDeviceManager { 37 DECLARE_SINGLE_INSTANCE(DpDeviceManager); 38 39 public: 40 bool Init(); 41 void GetLocalDeviceUdid(std::string& udid); 42 bool GetUdidByNetworkId(const std::string& networkId, std::string& udid); 43 bool GetUuidByNetworkId(const std::string& networkId, std::string& uuid); 44 bool DisconnectDeviceManager(); 45 bool ConnectDeviceManager(); 46 bool TransformDeviceId(const std::string& fromDeviceId, std::string& toDeviceId, 47 DeviceIdType toType); 48 void GetDeviceIdList(std::list<std::string>& deviceIdList); 49 void GetDeviceList(std::list<std::shared_ptr<DeviceInfo>>& deviceList); 50 void RemoveDeviceIdsByUdid(const std::string& udid); 51 52 private: 53 bool WaitForDnetworkReady(); 54 void OnNodeOnline(const std::shared_ptr<DeviceInfo> deviceInfo); 55 void OnNodeOffline(const std::string& deviceId); 56 void AddLocalDeviceIds(); 57 void AddDeviceIds(const std::string& networkId); 58 void RemoveDeviceIds(const std::string& networkId); 59 void RemoveExpiredDeviceIds(const std::string& networkId); 60 void RecoverDevicesIfNeeded(); 61 62 private: 63 std::mutex deviceLock_; 64 std::shared_ptr<AppExecFwk::EventHandler> devMgrHandler_; 65 std::shared_ptr<DistributedHardware::DeviceStateCallback> stateCallback_; 66 std::shared_ptr<DistributedHardware::DmInitCallback> initCallback_; 67 std::map<std::string, std::shared_ptr<DeviceInfo>> remoteDeviceInfoMap_; 68 std::list<std::vector<std::string>> deviceIdsList_; 69 70 class DeviceInitCallBack : public DistributedHardware::DmInitCallback { 71 void OnRemoteDied() override; 72 }; 73 74 class DpDeviceStateCallback : public DistributedHardware::DeviceStateCallback { 75 void OnDeviceOnline(const DistributedHardware::DmDeviceInfo &deviceInfo) override; 76 void OnDeviceOffline(const DistributedHardware::DmDeviceInfo &deviceInfo) override; 77 void OnDeviceChanged(const DistributedHardware::DmDeviceInfo &deviceInfo) override; 78 void OnDeviceReady(const DistributedHardware::DmDeviceInfo &deviceInfo) override; 79 }; 80 }; 81 } // namespace DeviceProfile 82 } // namespace OHOS 83 #endif // OHOS_DEVICE_PROFILE_DEVICE_MANAGER_H 84