1 /*
2  * Copyright (c) 2021-2024 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     void GetTrustedDeviceList();
52 
53 private:
54     std::shared_ptr<DistributedHardware::DmInitCallback> GetInitCallback();
55     std::shared_ptr<DistributedHardware::DeviceStateCallback> GetStateCallback();
56     void GetDevMgrHandler();
57     bool WaitForDnetworkReady();
58     void OnNodeOnline(const std::shared_ptr<DeviceInfo> deviceInfo);
59     void OnNodeOffline(const std::string& networkId);
60     void AddLocalDeviceIds();
61     void AddDeviceIds(const std::string& networkId);
62     void RemoveDeviceIds(const std::string& networkId);
63     void RemoveExpiredDeviceIds(const std::string& networkId);
64     void RecoverDevicesIfNeeded();
65 
66 private:
67     std::mutex deviceLock_;
68     std::mutex initcallbackLock_;
69     std::mutex stateCallLock_;
70     std::mutex devMgrLock_;
71     std::shared_ptr<AppExecFwk::EventHandler> devMgrHandler_;
72     std::shared_ptr<DistributedHardware::DeviceStateCallback> stateCallback_;
73     std::shared_ptr<DistributedHardware::DmInitCallback> initCallback_;
74     std::map<std::string, std::shared_ptr<DeviceInfo>> remoteDeviceInfoMap_;
75     std::list<std::vector<std::string>> deviceIdsList_;
76 
77 class DeviceInitCallBack : public DistributedHardware::DmInitCallback {
78     void OnRemoteDied() override;
79 };
80 
81 class DpDeviceStateCallback : public DistributedHardware::DeviceStateCallback {
82     void OnDeviceOnline(const DistributedHardware::DmDeviceInfo &deviceInfo) override;
83     void OnDeviceOffline(const DistributedHardware::DmDeviceInfo &deviceInfo) override;
84     void OnDeviceChanged(const DistributedHardware::DmDeviceInfo &deviceInfo) override;
85     void OnDeviceReady(const DistributedHardware::DmDeviceInfo &deviceInfo) override;
86 };
87 };
88 } // namespace DeviceProfile
89 } // namespace OHOS
90 #endif // OHOS_DEVICE_PROFILE_DEVICE_MANAGER_H
91