• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 #include "dev_manager.h"
16 
17 #include <logger.h>
18 
19 #include "device_manager.h"
20 #include "softbus_adapter.h"
21 
22 namespace OHOS {
23 namespace ObjectStore {
24 using namespace OHOS::DistributedHardware;
25 using DevInfo = OHOS::DistributedHardware::DmDeviceInfo;
26 constexpr int32_t DM_OK = 0;
27 constexpr int32_t DM_ERROR = -1;
28 constexpr const char *PKG_NAME = "ohos.objectstore";
29 class DMStateCallback : public DeviceStateCallback {
30 public:
DMStateCallback(std::shared_ptr<SoftBusAdapter> softBusAdapter)31     explicit DMStateCallback(std::shared_ptr<SoftBusAdapter> softBusAdapter) : softBusAdapter_(softBusAdapter){};
32     void OnDeviceOnline(const DmDeviceInfo &deviceInfo) override;
33     void OnDeviceOffline(const DmDeviceInfo &deviceInfo) override;
34     void OnDeviceChanged(const DmDeviceInfo &deviceInfo) override;
35     void OnDeviceReady(const DmDeviceInfo &deviceInfo) override;
36 
37 private:
38     std::shared_ptr<SoftBusAdapter> softBusAdapter_;
39     void NotifyAll(const DmDeviceInfo &deviceInfo, DeviceChangeType type);
40 };
41 
OnDeviceOnline(const DmDeviceInfo & deviceInfo)42 void DMStateCallback::OnDeviceOnline(const DmDeviceInfo &deviceInfo)
43 {
44     std::string uuid = DevManager::GetInstance()->GetUuidByNodeId(std::string(deviceInfo.networkId));
45     LOG_INFO("[Online] id:%{public}s, name:%{public}s, typeId:%{public}d", SoftBusAdapter::ToBeAnonymous(uuid).c_str(),
46         deviceInfo.deviceName, deviceInfo.deviceTypeId);
47     NotifyAll(deviceInfo, DeviceChangeType::DEVICE_ONLINE);
48 }
49 
OnDeviceOffline(const DmDeviceInfo & deviceInfo)50 void DMStateCallback::OnDeviceOffline(const DmDeviceInfo &deviceInfo)
51 {
52     std::string uuid = DevManager::GetInstance()->GetUuidByNodeId(std::string(deviceInfo.networkId));
53     LOG_INFO("[Offline] id:%{public}s, name:%{public}s, typeId:%{public}d",
54         SoftBusAdapter::ToBeAnonymous(uuid).c_str(), deviceInfo.deviceName, deviceInfo.deviceTypeId);
55     NotifyAll(deviceInfo, DeviceChangeType::DEVICE_OFFLINE);
56 }
57 
OnDeviceChanged(const DmDeviceInfo & deviceInfo)58 void DMStateCallback::OnDeviceChanged(const DmDeviceInfo &deviceInfo)
59 {
60     std::string uuid = DevManager::GetInstance()->GetUuidByNodeId(std::string(deviceInfo.networkId));
61     LOG_INFO("[InfoChange] id:%{public}s, name:%{public}s", SoftBusAdapter::ToBeAnonymous(uuid).c_str(),
62         deviceInfo.deviceName);
63 }
64 
OnDeviceReady(const DmDeviceInfo & deviceInfo)65 void DMStateCallback::OnDeviceReady(const DmDeviceInfo &deviceInfo)
66 {
67 }
68 
69 class DmDeathCallback : public DmInitCallback {
70 public:
DmDeathCallback(DevManager & devManager)71     explicit DmDeathCallback(DevManager &devManager) : devManager_(devManager){};
72     void OnRemoteDied() override;
73 
74 private:
75     DevManager &devManager_;
76 };
77 
OnRemoteDied()78 void DmDeathCallback::OnRemoteDied()
79 {
80     LOG_INFO("dm device manager died, init it again");
81     devManager_.RegisterDevCallback();
82 }
83 
NotifyAll(const DmDeviceInfo & deviceInfo,DeviceChangeType type)84 void DMStateCallback::NotifyAll(const DmDeviceInfo &deviceInfo, DeviceChangeType type)
85 {
86     DeviceInfo di = { std::string(deviceInfo.networkId), std::string(deviceInfo.deviceName),
87         std::to_string(deviceInfo.deviceTypeId) };
88     softBusAdapter_->NotifyAll(di, type);
89 }
90 
DevManager()91 DevManager::DevManager()
92 {
93 }
94 
~DevManager()95 DevManager::~DevManager()
96 {
97 }
98 
Init()99 int32_t DevManager::Init()
100 {
101     auto &deviceManager = DeviceManager::GetInstance();
102     auto deviceInitCallback = std::make_shared<DmDeathCallback>(*this);
103     auto deviceStateCallback = std::make_shared<DMStateCallback>(SoftBusAdapter::GetInstance());
104     int32_t status = deviceManager.InitDeviceManager(PKG_NAME, deviceInitCallback);
105     if (status != DM_OK) {
106         return status;
107     }
108     status = deviceManager.RegisterDevStateCallback(PKG_NAME, "", deviceStateCallback);
109     return status;
110 }
111 
RegisterDevCallback()112 void DevManager::RegisterDevCallback()
113 {
114     int32_t status = Init();
115     if (status == DM_OK) {
116         return;
117     }
118     LOG_INFO("register device callback failed, status: %{public}d, try again.", status);
119     std::thread th = std::thread([this]() {
120         pthread_setname_np(pthread_self(), "Data_Object_InitDevManager");
121         constexpr int RETRY_TIMES = 300;
122         int i = 0;
123         int32_t status = DM_ERROR;
124         while (i++ < RETRY_TIMES) {
125             status = Init();
126             if (status == DM_OK) {
127                 break;
128             }
129             std::this_thread::sleep_for(std::chrono::milliseconds(100));
130         }
131         LOG_INFO("register device callback exit now: %{public}d times, status: %{public}d", i, status);
132     });
133     th.detach();
134 }
135 
GetUuidByNodeId(const std::string & nodeId) const136 std::string DevManager::GetUuidByNodeId(const std::string &nodeId) const
137 {
138     std::string uuid = "";
139     int32_t ret = DistributedHardware::DeviceManager::GetInstance().GetEncryptedUuidByNetworkId(
140         "ohos.objectstore", nodeId.c_str(), uuid);
141     if (ret != DM_OK) {
142         LOG_WARN("GetEncryptedUuidByNetworkId error, nodeId:%{public}s", SoftBusAdapter::ToBeAnonymous(nodeId).c_str());
143         return "";
144     }
145     return uuid;
146 }
147 
GetLocalDevice()148 const DevManager::DetailInfo &DevManager::GetLocalDevice()
149 {
150     std::lock_guard<decltype(mutex_)> lockGuard(mutex_);
151     if (!localInfo_.uuid.empty()) {
152         return localInfo_;
153     }
154     DevInfo info;
155     auto ret = DeviceManager::GetInstance().GetLocalDeviceInfo(PKG_NAME, info);
156     if (ret != DM_OK) {
157         LOG_ERROR("get local device info fail");
158         return invalidDetail_;
159     }
160     auto networkId = std::string(info.networkId);
161     std::string uuid;
162     DeviceManager::GetInstance().GetEncryptedUuidByNetworkId(PKG_NAME, networkId, uuid);
163     if (uuid.empty() || networkId.empty()) {
164         return invalidDetail_;
165     }
166     localInfo_.networkId = std::move(networkId);
167     localInfo_.uuid = std::move(uuid);
168     return localInfo_;
169 }
170 } // namespace ObjectStore
171 } // namespace OHOS