• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 #include <algorithm>
17 
18 #include "anonymous_string.h"
19 #include "constants.h"
20 #include "dh_context.h"
21 #include "dh_utils_tool.h"
22 #include "distributed_hardware_errno.h"
23 #include "distributed_hardware_log.h"
24 
25 namespace OHOS {
26 namespace DistributedHardware {
27 IMPLEMENT_SINGLE_INSTANCE(DHContext);
DHContext()28 DHContext::DHContext()
29 {
30     DHLOGI("Ctor DHContext");
31     std::shared_ptr<AppExecFwk::EventRunner> runner = AppExecFwk::EventRunner::Create(true);
32     eventHandler_ = std::make_shared<DHContext::CommonEventHandler>(runner);
33     RegisterPowerStateLinstener();
34 }
35 
~DHContext()36 DHContext::~DHContext()
37 {
38     DHLOGI("Dtor DHContext");
39 }
40 
RegisterPowerStateLinstener()41 void DHContext::RegisterPowerStateLinstener()
42 {
43     sptr<PowerMgr::IPowerStateCallback> powerStateCallback_(new DHFWKPowerStateCallback());
44     if (powerStateCallback_ == nullptr) {
45         DHLOGE("DHFWK subscribe create power state callback Create Error");
46         return;
47     }
48 
49     bool ret = PowerMgr::PowerMgrClient::GetInstance().RegisterPowerStateCallback(powerStateCallback_);
50     if (!ret) {
51         DHLOGE("DHFWK register power state callback failed");
52     } else {
53         DHLOGE("DHFWK register power state callback success");
54     }
55 }
56 
OnPowerStateChanged(PowerMgr::PowerState state)57 void DHContext::DHFWKPowerStateCallback::OnPowerStateChanged(PowerMgr::PowerState state)
58 {
59     DHLOGI("DHFWK OnPowerStateChanged state: %u", static_cast<uint32_t>(state));
60     if (state == PowerMgr::PowerState::SLEEP || state == PowerMgr::PowerState::HIBERNATE ||
61         state == PowerMgr::PowerState::SHUTDOWN) {
62         DHLOGI("DHFWK set in sleeping");
63         DHContext::GetInstance().SetIsSleeping(true);
64         return;
65     }
66 
67     DHLOGI("DHFWK set NOT in sleeping");
68     DHContext::GetInstance().SetIsSleeping(false);
69 }
70 
IsSleeping()71 bool DHContext::IsSleeping()
72 {
73     return isSleeping_;
74 }
75 
SetIsSleeping(bool isSleeping)76 void DHContext::SetIsSleeping(bool isSleeping)
77 {
78     isSleeping_ = isSleeping;
79 }
80 
CommonEventHandler(const std::shared_ptr<AppExecFwk::EventRunner> & runner)81 DHContext::CommonEventHandler::CommonEventHandler(const std::shared_ptr<AppExecFwk::EventRunner> &runner)
82     : AppExecFwk::EventHandler(runner)
83 {
84     DHLOGI("Ctor CommonEventHandler");
85 }
86 
GetEventHandler()87 std::shared_ptr<DHContext::CommonEventHandler> DHContext::GetEventHandler()
88 {
89     return eventHandler_;
90 }
91 
PostTask(const Callback & callback,const std::string & name,int64_t delayTime)92 bool DHContext::CommonEventHandler::PostTask(const Callback &callback, const std::string &name, int64_t delayTime)
93 {
94     return AppExecFwk::EventHandler::PostTask(callback, name, delayTime);
95 }
96 
RemoveTask(const std::string & name)97 void DHContext::CommonEventHandler::RemoveTask(const std::string &name)
98 {
99     AppExecFwk::EventHandler::RemoveTask(name);
100 }
101 
GetDeviceInfo()102 const DeviceInfo& DHContext::GetDeviceInfo()
103 {
104     std::lock_guard<std::mutex> lock(devMutex_);
105     if (!devInfo_.uuid.empty()) {
106         return devInfo_;
107     }
108     devInfo_ = GetLocalDeviceInfo();
109     return devInfo_;
110 }
111 
AddOnlineDevice(const std::string & uuid,const std::string & networkId)112 void DHContext::AddOnlineDevice(const std::string &uuid, const std::string &networkId)
113 {
114     std::unique_lock<std::shared_mutex> lock(onlineDevMutex_);
115     if (onlineDeviceMap_.size() > MAX_ONLINE_DEVICE_SIZE || deviceIdUUIDMap_.size() > MAX_ONLINE_DEVICE_SIZE) {
116         DHLOGE("OnlineDeviceMap or deviceIdUUIDMap is over size!");
117         return;
118     }
119     onlineDeviceMap_[uuid] = networkId;
120     deviceIdUUIDMap_[GetDeviceIdByUUID(uuid)] = uuid;
121 }
122 
RemoveOnlineDevice(const std::string & uuid)123 void DHContext::RemoveOnlineDevice(const std::string &uuid)
124 {
125     std::unique_lock<std::shared_mutex> lock(onlineDevMutex_);
126     auto iter = onlineDeviceMap_.find(uuid);
127     if (iter != onlineDeviceMap_.end()) {
128         onlineDeviceMap_.erase(iter);
129         deviceIdUUIDMap_.erase(GetDeviceIdByUUID(uuid));
130     }
131 }
132 
IsDeviceOnline(const std::string & uuid)133 bool DHContext::IsDeviceOnline(const std::string &uuid)
134 {
135     std::shared_lock<std::shared_mutex> lock(onlineDevMutex_);
136     return onlineDeviceMap_.find(uuid) != onlineDeviceMap_.end();
137 }
138 
GetOnlineCount()139 size_t DHContext::GetOnlineCount()
140 {
141     std::shared_lock<std::shared_mutex> lock(onlineDevMutex_);
142     return onlineDeviceMap_.size();
143 }
144 
GetNetworkIdByUUID(const std::string & uuid)145 std::string DHContext::GetNetworkIdByUUID(const std::string &uuid)
146 {
147     std::unique_lock<std::shared_mutex> lock(onlineDevMutex_);
148     if (onlineDeviceMap_.find(uuid) == onlineDeviceMap_.end()) {
149         DHLOGE("Can not find networkId, uuid: %s", GetAnonyString(uuid).c_str());
150         return "";
151     }
152     return onlineDeviceMap_[uuid];
153 }
154 
GetUUIDByNetworkId(const std::string & networkId)155 std::string DHContext::GetUUIDByNetworkId(const std::string &networkId)
156 {
157     std::unique_lock<std::shared_mutex> lock(onlineDevMutex_);
158     auto iter = std::find_if(onlineDeviceMap_.begin(), onlineDeviceMap_.end(),
159         [networkId](const auto &item) {return networkId.compare(item.second) == 0; });
160     if (iter == onlineDeviceMap_.end()) {
161         DHLOGE("Can not find uuid, networkId: %s", GetAnonyString(networkId).c_str());
162         return "";
163     }
164     return iter->first;
165 }
166 
GetUUIDByDeviceId(const std::string & deviceId)167 std::string DHContext::GetUUIDByDeviceId(const std::string &deviceId)
168 {
169     std::unique_lock<std::shared_mutex> lock(onlineDevMutex_);
170     if (deviceIdUUIDMap_.find(deviceId) == deviceIdUUIDMap_.end()) {
171         DHLOGE("Can not find uuid, deviceId: %s", GetAnonyString(deviceId).c_str());
172         return "";
173     }
174     return deviceIdUUIDMap_[deviceId];
175 }
176 } // namespace DistributedHardware
177 } // namespace OHOS
178