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_HARDWARE_DHCONTEXT_H
17 #define OHOS_DISTRIBUTED_HARDWARE_DHCONTEXT_H
18 
19 #include <atomic>
20 #include <memory>
21 #include <set>
22 #include <string>
23 #include <unordered_set>
24 #include <shared_mutex>
25 
26 #ifdef POWER_MANAGER_ENABLE
27 #include "power_mgr_client.h"
28 #include "power_state_callback_stub.h"
29 #endif
30 
31 #include "device_type.h"
32 #include "event_handler.h"
33 #include "ipublisher_listener.h"
34 #include "single_instance.h"
35 
36 namespace OHOS {
37 namespace DistributedHardware {
38 struct DeviceIdEntry {
39     std::string networkId;
40     std::string uuid;
41     // deviceId is uuid hash
42     std::string deviceId;
43     std::string udid;
44     std::string udidHash;
45 
operator ==OHOS::DistributedHardware::DeviceIdEntry46     bool operator == (const DeviceIdEntry &other) const
47     {
48         return (networkId == other.networkId) &&
49             (uuid == other.uuid) &&
50             (deviceId == other.deviceId) &&
51             (udid == other.udid) &&
52             (udidHash == other.udidHash);
53     }
54 
operator <OHOS::DistributedHardware::DeviceIdEntry55     bool operator < (const DeviceIdEntry &other) const
56     {
57         return (networkId.compare(other.networkId) < 0) ||
58             ((networkId.compare(other.networkId) == 0) && (uuid.compare(other.uuid) < 0)) ||
59             ((networkId.compare(other.networkId) == 0) && (uuid.compare(other.uuid) == 0) &&
60             (deviceId.compare(other.deviceId) < 0)) ||
61             ((networkId.compare(other.networkId) == 0) && (uuid.compare(other.uuid) == 0) &&
62             (deviceId.compare(other.deviceId) == 0) && (udid.compare(other.udid) < 0)) ||
63             ((networkId.compare(other.networkId) == 0) && (uuid.compare(other.uuid) == 0) &&
64             (deviceId.compare(other.deviceId) == 0) && (udid.compare(other.udid) == 0) &&
65             (udidHash.compare(other.udidHash) < 0));
66     }
67 };
68 
69 class DHContext {
70 DECLARE_SINGLE_INSTANCE_BASE(DHContext);
71 public:
72     DHContext();
73     ~DHContext();
74     const DeviceInfo& GetDeviceInfo();
75 
76     /* Save online device UUID and networkId when devices online */
77     void AddOnlineDevice(const std::string &udid, const std::string &uuid, const std::string &networkId);
78     void RemoveOnlineDeviceIdEntryByNetworkId(const std::string &networkId);
79     bool IsDeviceOnline(const std::string &uuid);
80     size_t GetOnlineCount();
81     std::string GetNetworkIdByUUID(const std::string &uuid);
82     std::string GetNetworkIdByUDID(const std::string &udid);
83     std::string GetUdidHashIdByUUID(const std::string &uuid);
84     std::string GetUUIDByNetworkId(const std::string &networkId);
85     std::string GetUDIDByNetworkId(const std::string &networkId);
86     void AddRealTimeOnlineDeviceNetworkId(const std::string &networkId);
87     void DeleteRealTimeOnlineDeviceNetworkId(const std::string &networkId);
88     size_t GetRealTimeOnlineDeviceCount();
89     void GetOnlineDeviceUdidHash(std::vector<std::string> &udidHashVec);
90     void GetOnlineDeviceDeviceId(std::vector<std::string> &deviceIdVec);
91     /* DeviceId is which is hashed by sha256 */
92     std::string GetUUIDByDeviceId(const std::string &deviceId);
93     /**
94      * @brief Get the Network Id By Device Id object
95      *
96      * @param deviceId the device form uuid hash
97      * @return std::string the networkId for the deviceId
98      */
99     std::string GetNetworkIdByDeviceId(const std::string &deviceId);
100     std::string GetDeviceIdByDBGetPrefix(const std::string &prefix);
101 
102     class CommonEventHandler : public AppExecFwk::EventHandler {
103     public:
104         CommonEventHandler(const std::shared_ptr<AppExecFwk::EventRunner> runner);
105         ~CommonEventHandler() override = default;
106 
107         bool PostTask(const Callback &callback, const std::string &name = std::string(), int64_t delayTime = 0);
108         void RemoveTask(const std::string &name);
109     };
110     std::shared_ptr<DHContext::CommonEventHandler> GetEventHandler();
111 
112     bool IsSleeping();
113     void SetIsSleeping(bool isSleeping);
114     uint32_t GetIsomerismConnectCount();
115     void AddIsomerismConnectDev(const std::string &IsomerismDeviceId);
116     void DelIsomerismConnectDev(const std::string &IsomerismDeviceId);
117 
118 private:
119 #ifdef POWER_MANAGER_ENABLE
120     class DHFWKPowerStateCallback : public OHOS::PowerMgr::PowerStateCallbackStub {
121     public:
122         void OnPowerStateChanged(OHOS::PowerMgr::PowerState state) override;
123     };
124     #endif
125     void RegisterPowerStateLinstener();
126 
127 private:
128     class DHFWKIsomerismListener : public IPublisherListener {
129     public:
130         DHFWKIsomerismListener();
131         ~DHFWKIsomerismListener() override;
132         void OnMessage(const DHTopic topic, const std::string &message) override;
133         sptr<IRemoteObject> AsObject() override;
134     };
135     void RegisDHFWKIsomerismListener();
136 private:
137     DeviceInfo devInfo_ { "", "", "", "", "", "", 0 };
138     std::mutex devMutex_;
139 
140     std::set<DeviceIdEntry> devIdEntrySet_;
141     std::shared_mutex onlineDevMutex_;
142 
143     std::set<std::string> realTimeOnLineNetworkIdSet_;
144     std::shared_mutex realTimeNetworkIdMutex_;
145 
146     std::shared_ptr<DHContext::CommonEventHandler> eventHandler_;
147     /* true for system in sleeping, false for NOT in sleeping */
148     std::atomic<bool> isSleeping_ = false;
149 
150     std::unordered_set<std::string> connectedDevIds_;
151 
152     std::shared_mutex connectDevMutex_;
153     };
154 } // namespace DistributedHardware
155 } // namespace OHOS
156 #endif
157