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 #include "power_mgr_client.h" 27 #include "power_state_callback_stub.h" 28 29 #include "device_type.h" 30 #include "event_handler.h" 31 #include "ipublisher_listener.h" 32 #include "single_instance.h" 33 34 namespace OHOS { 35 namespace DistributedHardware { 36 struct DeviceIdEntry { 37 std::string networkId; 38 std::string uuid; 39 // deviceId is uuid hash 40 std::string deviceId; 41 std::string udid; 42 std::string udidHash; 43 44 bool operator == (const DeviceIdEntry &other) const 45 { 46 return (networkId == other.networkId) && 47 (uuid == other.uuid) && 48 (deviceId == other.deviceId) && 49 (udid == other.udid) && 50 (udidHash == other.udidHash); 51 } 52 53 bool operator < (const DeviceIdEntry &other) const 54 { 55 return (networkId.compare(other.networkId) < 0) || 56 ((networkId.compare(other.networkId) == 0) && (uuid.compare(other.uuid) < 0)) || 57 ((networkId.compare(other.networkId) == 0) && (uuid.compare(other.uuid) == 0) && 58 (deviceId.compare(other.deviceId) < 0)) || 59 ((networkId.compare(other.networkId) == 0) && (uuid.compare(other.uuid) == 0) && 60 (deviceId.compare(other.deviceId) == 0) && (udid.compare(other.udid) < 0)) || 61 ((networkId.compare(other.networkId) == 0) && (uuid.compare(other.uuid) == 0) && 62 (deviceId.compare(other.deviceId) == 0) && (udid.compare(other.udid) == 0) && 63 (udidHash.compare(other.udidHash) < 0)); 64 } 65 }; 66 67 class DHContext { 68 DECLARE_SINGLE_INSTANCE_BASE(DHContext); 69 public: 70 DHContext(); 71 ~DHContext(); 72 const DeviceInfo& GetDeviceInfo(); 73 74 /* Save online device UUID and networkId when devices online */ 75 void AddOnlineDevice(const std::string &udid, const std::string &uuid, const std::string &networkId); 76 void RemoveOnlineDeviceIdEntryByNetworkId(const std::string &networkId); 77 bool IsDeviceOnline(const std::string &uuid); 78 size_t GetOnlineCount(); 79 std::string GetNetworkIdByUUID(const std::string &uuid); 80 std::string GetNetworkIdByUDID(const std::string &udid); 81 std::string GetUdidHashIdByUUID(const std::string &uuid); 82 std::string GetUUIDByNetworkId(const std::string &networkId); 83 std::string GetUDIDByNetworkId(const std::string &networkId); 84 void AddRealTimeOnlineDeviceNetworkId(const std::string &networkId); 85 void DeleteRealTimeOnlineDeviceNetworkId(const std::string &networkId); 86 size_t GetRealTimeOnlineDeviceCount(); 87 void GetOnlineDeviceUdidHash(std::vector<std::string> &udidHashVec); 88 void GetOnlineDeviceDeviceId(std::vector<std::string> &deviceIdVec); 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 bool IsSleeping(); 111 void SetIsSleeping(bool isSleeping); 112 uint32_t GetIsomerismConnectCount(); 113 void AddIsomerismConnectDev(const std::string &IsomerismDeviceId); 114 void DelIsomerismConnectDev(const std::string &IsomerismDeviceId); 115 116 private: 117 class DHFWKPowerStateCallback : public OHOS::PowerMgr::PowerStateCallbackStub { 118 public: 119 void OnPowerStateChanged(OHOS::PowerMgr::PowerState state) override; 120 }; 121 void RegisterPowerStateLinstener(); 122 123 private: 124 class DHFWKIsomerismListener : public IPublisherListener { 125 public: 126 DHFWKIsomerismListener(); 127 ~DHFWKIsomerismListener() override; 128 void OnMessage(const DHTopic topic, const std::string &message) override; 129 sptr<IRemoteObject> AsObject() override; 130 }; 131 void RegisDHFWKIsomerismListener(); 132 private: 133 DeviceInfo devInfo_ { "", "", "", "", "", "", 0 }; 134 std::mutex devMutex_; 135 136 std::set<DeviceIdEntry> devIdEntrySet_; 137 std::shared_mutex onlineDevMutex_; 138 139 std::set<std::string> realTimeOnLineNetworkIdSet_; 140 std::shared_mutex realTimeNetworkIdMutex_; 141 142 std::shared_ptr<DHContext::CommonEventHandler> eventHandler_; 143 /* true for system in sleeping, false for NOT in sleeping */ 144 std::atomic<bool> isSleeping_ = false; 145 146 std::unordered_set<std::string> connectedDevIds_; 147 148 std::shared_mutex connectDevMutex_; 149 }; 150 } // namespace DistributedHardware 151 } // namespace OHOS 152 #endif 153