• 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 
62 //LCOV_EXCL_START
PutAllTrustedDevices()63 void DpInitedCallback::PutAllTrustedDevices()
64 {
65     LOGE("In.");
66     std::vector<DmDeviceInfo> dmDeviceInfos;
67     int32_t ret = SoftbusCache::GetInstance().GetDeviceInfoFromCache(dmDeviceInfos);
68     if (ret != DM_OK) {
69         LOGE("GetDeviceInfoFromCache fail:%{public}d", ret);
70         return;
71     }
72     if (dmDeviceInfos.empty()) {
73         LOGW("dmDeviceInfos is empty");
74         return;
75     }
76     char localUdidTemp[DEVICE_UUID_LENGTH];
77     GetDevUdid(localUdidTemp, DEVICE_UUID_LENGTH);
78     std::string localUdid = reinterpret_cast<char *>(localUdidTemp);
79     if (localUdid.empty()) {
80         LOGE("localUdid is empty");
81         return;
82     }
83     std::vector<DistributedDeviceProfile::TrustedDeviceInfo> deviceInfos;
84     std::unordered_map<std::string, DmAuthForm> authFormMap =
85         DeviceProfileConnector::GetInstance().GetAppTrustDeviceList("", localUdid);
86     for (const auto& item : dmDeviceInfos) {
87         DistributedDeviceProfile::TrustedDeviceInfo trustedDeviceInfo;
88         if (ConvertToTrustedDeviceInfo(authFormMap, item, trustedDeviceInfo)) {
89             deviceInfos.push_back(trustedDeviceInfo);
90         }
91     }
92     if (deviceInfos.empty()) {
93         LOGW("deviceInfos is empty");
94         return;
95     }
96     ret = DeviceProfileConnector::GetInstance().PutAllTrustedDevices(deviceInfos);
97     LOGI("ret:%{public}d", ret);
98 }
99 //LCOV_EXCL_STOP
100 
ConvertToTrustedDeviceInfo(const std::unordered_map<std::string,DmAuthForm> & authFormMap,const DmDeviceInfo & deviceInfo,DistributedDeviceProfile::TrustedDeviceInfo & trustedDeviceInfo)101 bool DpInitedCallback::ConvertToTrustedDeviceInfo(const std::unordered_map<std::string, DmAuthForm> &authFormMap,
102     const DmDeviceInfo &deviceInfo, DistributedDeviceProfile::TrustedDeviceInfo &trustedDeviceInfo)
103 {
104     trustedDeviceInfo.SetNetworkId(deviceInfo.networkId);
105     trustedDeviceInfo.SetDeviceTypeId(deviceInfo.deviceTypeId);
106 
107     if (deviceInfo.extraData.empty()) {
108         LOGE("extraData is empty, networkId:%{public}s", GetAnonyString(deviceInfo.networkId).c_str());
109         return false;
110     }
111     JsonObject extraJson(deviceInfo.extraData);
112     if (extraJson.IsDiscarded()) {
113         LOGE("extraData parse failed, networkId:%{public}s", GetAnonyString(deviceInfo.networkId).c_str());
114         return false;
115     }
116     if (IsString(extraJson, PARAM_KEY_OS_VERSION)) {
117         trustedDeviceInfo.SetOsVersion(extraJson[PARAM_KEY_OS_VERSION].Get<std::string>());
118     } else {
119         LOGE("osVersion parse failed, networkId:%{public}s", GetAnonyString(deviceInfo.networkId).c_str());
120         return false;
121     }
122     if (IsInt32(extraJson, PARAM_KEY_OS_TYPE)) {
123         trustedDeviceInfo.SetOsType(extraJson[PARAM_KEY_OS_TYPE].Get<int32_t>());
124     } else {
125         LOGE("osType parse failed, networkId:%{public}s", GetAnonyString(deviceInfo.networkId).c_str());
126         return false;
127     }
128 
129     std::string udid = "";
130     if (SoftbusCache::GetInstance().GetUdidFromCache(deviceInfo.networkId, udid) != DM_OK) {
131         LOGE("udid parse failed, networkId:%{public}s", GetAnonyString(deviceInfo.networkId).c_str());
132         return false;
133     }
134     trustedDeviceInfo.SetUdid(udid);
135 
136     std::string uuid = "";
137     if (SoftbusCache::GetInstance().GetUuidFromCache(deviceInfo.networkId, uuid) != DM_OK) {
138         LOGE("uuid parse failed, networkId:%{public}s", GetAnonyString(deviceInfo.networkId).c_str());
139         return false;
140     }
141     trustedDeviceInfo.SetUuid(uuid);
142 
143     auto it = authFormMap.find(udid);
144     if (it == authFormMap.end()) {
145         LOGE("authForm not exist, udid:%{public}s", GetAnonyString(udid).c_str());
146         return false;
147     }
148     trustedDeviceInfo.SetAuthForm(static_cast<int32_t>(it->second));
149     return true;
150 }
151 } // namespace DistributedHardware
152 } // namespace OHOS