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