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