• 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 "dp_inited_callback.h"
17 
18 #include <pthread.h>
19 #include <thread>
20 #include <vector>
21 #include <unordered_map>
22 
23 #include "json_object.h"
24 #include "parameter.h"
25 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
26 #include "ffrt.h"
27 #endif
28 
29 #include "deviceprofile_connector.h"
30 #include "dm_anonymous.h"
31 #include "dm_constants.h"
32 #include "dm_log.h"
33 #include "dm_softbus_cache.h"
34 
35 namespace OHOS {
36 namespace DistributedHardware {
37 namespace {
38 const std::string PUT_ALL_TRUSTED_DEVICES_TASK = "PutAllTrustedDevicesTask";
39 }
40 
DpInitedCallback()41 DpInitedCallback::DpInitedCallback()
42 {}
43 
~DpInitedCallback()44 DpInitedCallback::~DpInitedCallback()
45 {}
46 
OnDpInited()47 int32_t DpInitedCallback::OnDpInited()
48 {
49     LOGE("In.");
50 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
51     ffrt::submit([=]() { PutAllTrustedDevices(); });
52 #else
53     std::thread putAllTrustedDevicesTask([=]() { PutAllTrustedDevices(); });
54     if (pthread_setname_np(putAllTrustedDevicesTask.native_handle().c_str(), PUT_ALL_TRUSTED_DEVICES_TASK) != DM_OK) {
55         LOGE("putAllTrustedDevicesTask setname failed.");
56     }
57     putAllTrustedDevicesTask.detach();
58 #endif
59     return DM_OK;
60 }
61 
PutAllTrustedDevices()62 void DpInitedCallback::PutAllTrustedDevices()
63 {
64     LOGE("In.");
65     std::vector<DmDeviceInfo> dmDeviceInfos;
66     int32_t ret = SoftbusCache::GetInstance().GetDeviceInfoFromCache(dmDeviceInfos);
67     if (ret != DM_OK) {
68         LOGE("GetDeviceInfoFromCache fail:%{public}d", ret);
69         return;
70     }
71     if (dmDeviceInfos.empty()) {
72         LOGW("dmDeviceInfos is empty");
73         return;
74     }
75     char localUdidTemp[DEVICE_UUID_LENGTH];
76     GetDevUdid(localUdidTemp, DEVICE_UUID_LENGTH);
77     std::string localUdid = reinterpret_cast<char *>(localUdidTemp);
78     if (localUdid.empty()) {
79         LOGE("localUdid is empty");
80         return;
81     }
82     std::vector<DistributedDeviceProfile::TrustedDeviceInfo> deviceInfos;
83     std::unordered_map<std::string, DmAuthForm> authFormMap =
84         DeviceProfileConnector::GetInstance().GetAppTrustDeviceList("", localUdid);
85     for (const auto& item : dmDeviceInfos) {
86         DistributedDeviceProfile::TrustedDeviceInfo trustedDeviceInfo;
87         if (ConvertToTrustedDeviceInfo(authFormMap, item, trustedDeviceInfo)) {
88             deviceInfos.push_back(trustedDeviceInfo);
89         }
90     }
91     if (deviceInfos.empty()) {
92         LOGW("deviceInfos is empty");
93         return;
94     }
95     ret = DeviceProfileConnector::GetInstance().PutAllTrustedDevices(deviceInfos);
96     LOGI("ret:%{public}d", ret);
97 }
98 
ConvertToTrustedDeviceInfo(const std::unordered_map<std::string,DmAuthForm> & authFormMap,const DmDeviceInfo & deviceInfo,DistributedDeviceProfile::TrustedDeviceInfo & trustedDeviceInfo)99 bool DpInitedCallback::ConvertToTrustedDeviceInfo(const std::unordered_map<std::string, DmAuthForm> &authFormMap,
100     const DmDeviceInfo &deviceInfo, DistributedDeviceProfile::TrustedDeviceInfo &trustedDeviceInfo)
101 {
102     trustedDeviceInfo.SetNetworkId(deviceInfo.networkId);
103     trustedDeviceInfo.SetDeviceTypeId(deviceInfo.deviceTypeId);
104 
105     if (deviceInfo.extraData.empty()) {
106         LOGE("extraData is empty, networkId:%{public}s", GetAnonyString(deviceInfo.networkId).c_str());
107         return false;
108     }
109     JsonObject extraJson(deviceInfo.extraData);
110     if (extraJson.IsDiscarded()) {
111         LOGE("extraData parse failed, networkId:%{public}s", GetAnonyString(deviceInfo.networkId).c_str());
112         return false;
113     }
114     if (IsString(extraJson, PARAM_KEY_OS_VERSION)) {
115         trustedDeviceInfo.SetOsVersion(extraJson[PARAM_KEY_OS_VERSION].Get<std::string>());
116     } else {
117         LOGE("osVersion parse failed, networkId:%{public}s", GetAnonyString(deviceInfo.networkId).c_str());
118         return false;
119     }
120     if (IsInt32(extraJson, PARAM_KEY_OS_TYPE)) {
121         trustedDeviceInfo.SetOsType(extraJson[PARAM_KEY_OS_TYPE].Get<int32_t>());
122     } else {
123         LOGE("osType parse failed, networkId:%{public}s", GetAnonyString(deviceInfo.networkId).c_str());
124         return false;
125     }
126 
127     std::string udid = "";
128     if (SoftbusCache::GetInstance().GetUdidFromCache(deviceInfo.networkId, udid) != DM_OK) {
129         LOGE("udid parse failed, networkId:%{public}s", GetAnonyString(deviceInfo.networkId).c_str());
130         return false;
131     }
132     trustedDeviceInfo.SetUdid(udid);
133 
134     std::string uuid = "";
135     if (SoftbusCache::GetInstance().GetUuidFromCache(deviceInfo.networkId, uuid) != DM_OK) {
136         LOGE("uuid parse failed, networkId:%{public}s", GetAnonyString(deviceInfo.networkId).c_str());
137         return false;
138     }
139     trustedDeviceInfo.SetUuid(uuid);
140 
141     auto it = authFormMap.find(udid);
142     if (it == authFormMap.end()) {
143         LOGE("authForm not exist, udid:%{public}s", GetAnonyString(udid).c_str());
144         return false;
145     }
146     trustedDeviceInfo.SetAuthForm(static_cast<int32_t>(it->second));
147     return true;
148 }
149 } // namespace DistributedHardware
150 } // namespace OHOS