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_DISTRIBUTED_DTBSCHEDMGR_DEVICE_INFO_INTERFACE_H
17 #define OHOS_DISTRIBUTED_DTBSCHEDMGR_DEVICE_INFO_INTERFACE_H
18 
19 #include <map>
20 #include <set>
21 #include <string>
22 
23 #include "adapter/dnetwork_adapter.h"
24 #include "deviceManager/dms_device_info.h"
25 #include "distributed_device_node_listener.h"
26 #include "event_handler.h"
27 #include "iremote_object.h"
28 #include "single_instance.h"
29 
30 namespace OHOS {
31 namespace DistributedSchedule {
32 class DnetServiceDeathRecipient : public IRemoteObject::DeathRecipient {
33 public:
34     DnetServiceDeathRecipient() = default;
35     ~DnetServiceDeathRecipient() override = default;
36     void OnRemoteDied(const wptr<IRemoteObject>& remote) override;
37 };
38 
39 class DtbschedmgrDeviceInfoStorage {
40     DECLARE_SINGLE_INSTANCE(DtbschedmgrDeviceInfoStorage);
41 
42 public:
43     bool Init();
44     void Stop();
45     bool GetLocalDeviceId(std::string& networkId);
46     bool GetLocalUdid(std::string& udid);
47     bool GetLocalUuid(std::string& uuid);
48     void DeviceOnlineNotify(const std::shared_ptr<DmsDeviceInfo> devInfo);
49     void DeviceOfflineNotify(const std::string& networkId);
50     void OnDeviceInfoChanged(const std::string& networkId);
51     std::string GetDeviceName(std::string netWorkId);
52     std::vector<std::string> GetNetworkIdList();
53 
54     /**
55      * get device info by device id
56      *
57      * @param networkId, string
58      * @return shared_ptr<DmsDeviceInfo>
59      */
60     std::shared_ptr<DmsDeviceInfo> GetDeviceInfoById(const std::string& networkId);
61 
62     /**
63      * get uuid by networkId
64      *
65      * @param networkId
66      */
67     std::string GetUuidByNetworkId(const std::string& networkId);
68 
69     /**
70      * get udid by networkId
71      *
72      * @param networkId
73      */
74     std::string GetUdidByNetworkId(const std::string& networkId);
75 
76     /**
77      * get networkId by uuid
78      *
79      * @param uuid
80      */
81     std::string GetNetworkIdByUuid(const std::string& uuid);
82 
83     /**
84      * GetDeviceIdSet get all of the device Id in same network
85      *
86      * @param networkIdSet Returns the device set.
87      */
88     void GetDeviceIdSet(std::set<std::string>& deviceIdSet);
89 
90     /**
91      * UpdateDeviceInfoStorage update device Info cache
92      */
93     bool UpdateDeviceInfoStorage();
94 
95 private:
96     bool InitNetworkIdManager(std::shared_ptr<DnetworkAdapter> dnetworkAdapter);
97     bool ConnectSoftbus();
98     std::shared_ptr<DmsDeviceInfo> FindDeviceInfoInStorage(const std::string& networkId);
99     void ClearAllDevices();
100     bool WaitForDnetworkReady();
101     void RegisterUuidNetworkIdMap(const std::string& networkId);
102     void UnregisterUuidNetworkIdMap(const std::string& networkId);
103     std::mutex deviceLock_;
104     std::shared_ptr<DistributedDeviceNodeListener> deviceNodeListener_;
105     std::map<std::string, std::shared_ptr<DmsDeviceInfo>> remoteDevices_;
106     std::string deviceId_;
107     std::map<std::string, std::string> uuidNetworkIdMap_;
108     std::mutex uuidNetworkIdLock_;
109     std::shared_ptr<AppExecFwk::EventHandler> initHandler_;
110     std::shared_ptr<AppExecFwk::EventHandler> networkIdMgrHandler_;
111 };
112 } // namespace DistributedSchedule
113 } // namespace OHOS
114 #endif // OHOS_DISTRIBUTED_DTBSCHEDMGR_DEVICE_INFO_INTERFACE_H
115