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 "constants.h"
19 #include "dh_context.h"
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 (onlineDeviceMap_.size() > MAX_ONLINE_DEVICE_SIZE || deviceIdUUIDMap_.size() > MAX_ONLINE_DEVICE_SIZE) {
56 DHLOGE("OnlineDeviceMap or deviceIdUUIDMap is over size!");
57 return;
58 }
59 onlineDeviceMap_[uuid] = networkId;
60 deviceIdUUIDMap_[GetDeviceIdByUUID(uuid)] = uuid;
61 }
62
RemoveOnlineDevice(const std::string & uuid)63 void DHContext::RemoveOnlineDevice(const std::string &uuid)
64 {
65 std::unique_lock<std::shared_mutex> lock(onlineDevMutex_);
66 auto iter = onlineDeviceMap_.find(uuid);
67 if (iter != onlineDeviceMap_.end()) {
68 onlineDeviceMap_.erase(iter);
69 deviceIdUUIDMap_.erase(GetDeviceIdByUUID(uuid));
70 }
71 }
72
IsDeviceOnline(const std::string & uuid)73 bool DHContext::IsDeviceOnline(const std::string &uuid)
74 {
75 std::shared_lock<std::shared_mutex> lock(onlineDevMutex_);
76 return onlineDeviceMap_.find(uuid) != onlineDeviceMap_.end();
77 }
78
GetOnlineCount()79 size_t DHContext::GetOnlineCount()
80 {
81 std::shared_lock<std::shared_mutex> lock(onlineDevMutex_);
82 return onlineDeviceMap_.size();
83 }
84
GetNetworkIdByUUID(const std::string & uuid)85 std::string DHContext::GetNetworkIdByUUID(const std::string &uuid)
86 {
87 std::unique_lock<std::shared_mutex> lock(onlineDevMutex_);
88 if (onlineDeviceMap_.find(uuid) == onlineDeviceMap_.end()) {
89 DHLOGE("Can not find networkId, uuid: %s", GetAnonyString(uuid).c_str());
90 return "";
91 }
92 return onlineDeviceMap_[uuid];
93 }
94
GetUUIDByNetworkId(const std::string & networkId)95 std::string DHContext::GetUUIDByNetworkId(const std::string &networkId)
96 {
97 std::unique_lock<std::shared_mutex> lock(onlineDevMutex_);
98 auto iter = std::find_if(onlineDeviceMap_.begin(), onlineDeviceMap_.end(),
99 [networkId](const auto &item) {return networkId.compare(item.second) == 0; });
100 if (iter == onlineDeviceMap_.end()) {
101 DHLOGE("Can not find uuid, networkId: %s", GetAnonyString(networkId).c_str());
102 return "";
103 }
104 return iter->first;
105 }
106
GetUUIDByDeviceId(const std::string & deviceId)107 std::string DHContext::GetUUIDByDeviceId(const std::string &deviceId)
108 {
109 std::unique_lock<std::shared_mutex> lock(onlineDevMutex_);
110 if (deviceIdUUIDMap_.find(deviceId) == deviceIdUUIDMap_.end()) {
111 DHLOGE("Can not find uuid, deviceId: %s", GetAnonyString(deviceId).c_str());
112 return "";
113 }
114 return deviceIdUUIDMap_[deviceId];
115 }
116 } // namespace DistributedHardware
117 } // namespace OHOS
118