• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 "dm_adapter.h"
17 
18 #include "cJSON.h"
19 #include "device_manager.h"
20 #include "dm_constants.h"
21 
22 #include "device_profile_manager.h"
23 #include "distributed_device_profile_constants.h"
24 #include "distributed_device_profile_errors.h"
25 #include "distributed_device_profile_log.h"
26 #include "profile_cache.h"
27 #include "profile_utils.h"
28 
29 namespace OHOS {
30 namespace DistributedDeviceProfile {
31 namespace {
32     const std::string TAG = "DMAdapter";
33 }
34 
35 IMPLEMENT_SINGLE_INSTANCE(DMAdapter);
36 
Init()37 int32_t DMAdapter::Init()
38 {
39     HILOGI("call!");
40     {
41         std::lock_guard<std::mutex> autoLock(deviceStateCallbackMutex_);
42         if (dmInitCallback_ == nullptr) {
43             dmInitCallback_ = std::make_shared<DpDmInitCallback>();
44         }
45         if (deviceStateCallback_ == nullptr) {
46             deviceStateCallback_ = std::make_shared<DmDeviceStateCallback>();
47         }
48         int32_t errCode = DistributedHardware::DeviceManager::GetInstance().InitDeviceManager(
49             DP_PKG_NAME, dmInitCallback_);
50         if (errCode != DP_SUCCESS) {
51             HILOGE("InitDeviceManager errCode = %{public}d", errCode);
52             return errCode;
53         }
54         errCode = DistributedHardware::DeviceManager::GetInstance().RegisterDevStateCallback(
55             DP_PKG_NAME, "", deviceStateCallback_);
56         if (errCode != DP_SUCCESS) {
57             HILOGE("RegisterDevStateCallback errCode = %{public}d", errCode);
58             return errCode;
59         }
60     }
61     return DP_SUCCESS;
62 }
63 
UnInit()64 int32_t DMAdapter::UnInit()
65 {
66     HILOGI("call!");
67     int32_t errCode = DP_SUCCESS;
68     {
69         std::lock_guard<std::mutex> autoLock(deviceStateCallbackMutex_);
70         if (deviceStateCallback_ != nullptr) {
71             errCode = DistributedHardware::DeviceManager::GetInstance().UnRegisterDevStateCallback(DP_PKG_NAME);
72             HILOGI("UnRegisterDevStateCallback errCode = %{public}d", errCode);
73             deviceStateCallback_ = nullptr;
74         }
75         if (dmInitCallback_ != nullptr) {
76             errCode = DistributedHardware::DeviceManager::GetInstance().UnInitDeviceManager(DP_PKG_NAME);
77             HILOGI("UnInitDeviceManager errCode = %{public}d", errCode);
78             dmInitCallback_ = nullptr;
79         }
80     }
81     return DP_SUCCESS;
82 }
83 
ReInit()84 int32_t DMAdapter::ReInit()
85 {
86     HILOGI("call!");
87     UnInit();
88     return Init();
89 }
90 
GetUuidByNetworkId(const std::string & networkId,std::string & uuid)91 bool DMAdapter::GetUuidByNetworkId(const std::string& networkId, std::string& uuid)
92 {
93     HILOGI("networkId:%{public}s", ProfileUtils::GetAnonyString(networkId).c_str());
94     return ((DistributedHardware::DeviceManager::GetInstance().GetUuidByNetworkId(DP_PKG_NAME, networkId, uuid) == 0) ?
95         true : false);
96 }
97 
OnDeviceOnline(const DistributedHardware::DmDeviceInfo & deviceInfo)98 void DMAdapter::DmDeviceStateCallback::OnDeviceOnline(const DistributedHardware::DmDeviceInfo& deviceInfo)
99 {
100     HILOGI("networkId:%{public}s", ProfileUtils::GetAnonyString(deviceInfo.networkId).c_str());
101     TrustedDeviceInfo trustedDeviceInfo;
102     if (!ConvertToTrustedDeviceInfo(deviceInfo, trustedDeviceInfo)) {
103         HILOGE("ConvertToTrustedDeviceInfo fial, networkId:%{public}s",
104             ProfileUtils::GetAnonyString(deviceInfo.networkId).c_str());
105         return;
106     }
107     HILOGI("trustedDeviceInfo:%{public}s", trustedDeviceInfo.dump().c_str());
108     ProfileCache::GetInstance().OnNodeOnline(trustedDeviceInfo);
109     DeviceProfileManager::GetInstance().OnDeviceOnline(trustedDeviceInfo);
110 }
111 
OnDeviceOffline(const DistributedHardware::DmDeviceInfo & deviceInfo)112 void DMAdapter::DmDeviceStateCallback::OnDeviceOffline(const DistributedHardware::DmDeviceInfo& deviceInfo)
113 {
114     std::string networkId = deviceInfo.networkId;
115     HILOGI("networkId:%{public}s", ProfileUtils::GetAnonyString(networkId).c_str());
116     ProfileCache::GetInstance().OnNodeOffline(networkId);
117 }
118 
OnDeviceChanged(const DistributedHardware::DmDeviceInfo & deviceInfo)119 void DMAdapter::DmDeviceStateCallback::OnDeviceChanged(const DistributedHardware::DmDeviceInfo& deviceInfo)
120 {
121     HILOGD("networkId:%{public}s", ProfileUtils::GetAnonyString(deviceInfo.networkId).c_str());
122 }
123 
OnDeviceReady(const DistributedHardware::DmDeviceInfo & deviceInfo)124 void DMAdapter::DmDeviceStateCallback::OnDeviceReady(const DistributedHardware::DmDeviceInfo& deviceInfo)
125 {
126     HILOGD("networkId:%{public}s", ProfileUtils::GetAnonyString(deviceInfo.networkId).c_str());
127 }
128 
ConvertToTrustedDeviceInfo(const DistributedHardware::DmDeviceInfo & deviceInfo,TrustedDeviceInfo & trustedDeviceInfo)129 bool DMAdapter::DmDeviceStateCallback::ConvertToTrustedDeviceInfo(const DistributedHardware::DmDeviceInfo& deviceInfo,
130     TrustedDeviceInfo& trustedDeviceInfo)
131 {
132     trustedDeviceInfo.SetNetworkId(deviceInfo.networkId);
133     trustedDeviceInfo.SetAuthForm(static_cast<int32_t>(deviceInfo.authForm));
134     trustedDeviceInfo.SetDeviceTypeId(deviceInfo.deviceTypeId);
135 
136     if (deviceInfo.extraData.empty()) {
137         HILOGE("extraData is empty!");
138         return false;
139     }
140     cJSON* extraDataJson = cJSON_Parse(deviceInfo.extraData.c_str());
141     if (extraDataJson == NULL) {
142         HILOGE("extraData parse failed");
143         cJSON_Delete(extraDataJson);
144         return false;
145     }
146 
147     cJSON* osVersionJson = cJSON_GetObjectItem(extraDataJson, DistributedHardware::PARAM_KEY_OS_VERSION);
148     if (!cJSON_IsString(osVersionJson)) {
149         HILOGE("osVersion parse failed");
150         cJSON_Delete(extraDataJson);
151         return false;
152     }
153     trustedDeviceInfo.SetOsVersion(osVersionJson->valuestring);
154 
155     cJSON* osTypeJson = cJSON_GetObjectItem(extraDataJson, DistributedHardware::PARAM_KEY_OS_TYPE);
156     if (!cJSON_IsNumber(osTypeJson)) {
157         HILOGE("osType parse failed");
158         cJSON_Delete(extraDataJson);
159         return false;
160     }
161     trustedDeviceInfo.SetOsType(static_cast<int32_t>(osTypeJson->valueint));
162 
163     cJSON* udidJson = cJSON_GetObjectItem(extraDataJson, DistributedHardware::PARAM_KEY_UDID);
164     if (!cJSON_IsString(udidJson)) {
165         HILOGE("udid parse failed");
166         cJSON_Delete(extraDataJson);
167         return false;
168     }
169     trustedDeviceInfo.SetUdid(udidJson->valuestring);
170 
171     cJSON* uuidJson = cJSON_GetObjectItem(extraDataJson, DistributedHardware::PARAM_KEY_UUID);
172     if (!cJSON_IsString(uuidJson)) {
173         HILOGE("uuid parse failed");
174         cJSON_Delete(extraDataJson);
175         return false;
176     }
177     trustedDeviceInfo.SetUuid(uuidJson->valuestring);
178     cJSON_Delete(extraDataJson);
179     return true;
180 }
181 
OnRemoteDied()182 void DMAdapter::DpDmInitCallback::OnRemoteDied()
183 {
184     HILOGI("call!");
185     DMAdapter::GetInstance().ReInit();
186 }
187 } // namespace DistributedDeviceProfile
188 } // namespace OHOS