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 "dh_context.h"
19
20 #include "dh_utils_tool.h"
21 #include "distributed_hardware_errno.h"
22 #include "distributed_hardware_log.h"
23
24 namespace OHOS {
25 namespace DistributedHardware {
26 IMPLEMENT_SINGLE_INSTANCE(DHContext);
DHContext()27 DHContext::DHContext() : eventBus_(std::make_shared<EventBus>()) {}
28
~DHContext()29 DHContext::~DHContext()
30 {
31 if (eventBus_ != nullptr) {
32 eventBus_.reset();
33 eventBus_ = nullptr;
34 }
35 }
36
GetEventBus()37 std::shared_ptr<EventBus> DHContext::GetEventBus()
38 {
39 return eventBus_;
40 }
41
GetDeviceInfo()42 const DeviceInfo& DHContext::GetDeviceInfo()
43 {
44 std::lock_guard<std::mutex> lock(devMutex_);
45 if (!devInfo_.uuid.empty()) {
46 return devInfo_;
47 }
48 devInfo_ = GetLocalDeviceInfo();
49 return devInfo_;
50 }
51
AddOnlineDevice(const std::string & uuid,const std::string & networkId)52 void DHContext::AddOnlineDevice(const std::string &uuid, const std::string &networkId)
53 {
54 std::unique_lock<std::shared_mutex> lock(onlineDevMutex_);
55 if (!uuid.empty() && !networkId.empty()) {
56 onlineDeviceMap_[uuid] = networkId;
57 deviceIdUUIDMap_[GetDeviceIdByUUID(uuid)] = uuid;
58 }
59 }
60
RemoveOnlineDevice(const std::string & uuid)61 void DHContext::RemoveOnlineDevice(const std::string &uuid)
62 {
63 std::unique_lock<std::shared_mutex> lock(onlineDevMutex_);
64 auto iter = onlineDeviceMap_.find(uuid);
65 if (iter != onlineDeviceMap_.end()) {
66 onlineDeviceMap_.erase(iter);
67 deviceIdUUIDMap_.erase(GetDeviceIdByUUID(uuid));
68 }
69 }
70
IsDeviceOnline(const std::string & uuid)71 bool DHContext::IsDeviceOnline(const std::string &uuid)
72 {
73 std::shared_lock<std::shared_mutex> lock(onlineDevMutex_);
74 return onlineDeviceMap_.find(uuid) != onlineDeviceMap_.end();
75 }
76
GetOnlineCount()77 size_t DHContext::GetOnlineCount()
78 {
79 std::shared_lock<std::shared_mutex> lock(onlineDevMutex_);
80 return onlineDeviceMap_.size();
81 }
82
GetNetworkIdByUUID(const std::string & uuid)83 std::string DHContext::GetNetworkIdByUUID(const std::string &uuid)
84 {
85 std::unique_lock<std::shared_mutex> lock(onlineDevMutex_);
86 if (onlineDeviceMap_.find(uuid) == onlineDeviceMap_.end()) {
87 DHLOGE("Can not find networkId, uuid: %s", GetAnonyString(uuid).c_str());
88 return "";
89 }
90 return onlineDeviceMap_[uuid];
91 }
92
GetUUIDByNetworkId(const std::string & networkId)93 std::string DHContext::GetUUIDByNetworkId(const std::string &networkId) {
94 std::unique_lock<std::shared_mutex> lock(onlineDevMutex_);
95 auto iter = std::find_if(onlineDeviceMap_.begin(), onlineDeviceMap_.end(),
96 [networkId](const auto &item) {return networkId.compare(item.second) == 0; });
97 if (iter == onlineDeviceMap_.end()) {
98 DHLOGE("Can not find uuid, networkId: %s", GetAnonyString(networkId).c_str());
99 return "";
100 }
101 return iter->first;
102 }
103
GetUUIDByDeviceId(const std::string & deviceId)104 std::string DHContext::GetUUIDByDeviceId(const std::string &deviceId)
105 {
106 std::unique_lock<std::shared_mutex> lock(onlineDevMutex_);
107 if (deviceIdUUIDMap_.find(deviceId) == deviceIdUUIDMap_.end()) {
108 DHLOGE("Can not find uuid, deviceId: %s", GetAnonyString(deviceId).c_str());
109 return "";
110 }
111 return deviceIdUUIDMap_[deviceId];
112 }
113 }
114 }
115