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