1 /*
2  * Copyright (c) 2023-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_radar_helper.h"
17 #include <errors.h>
18 #include <algorithm>
19 #include <sstream>
20 #include <iomanip>
21 #include <cJSON.h>
22 #include "dm_constants.h"
23 #include "dm_crypto.h"
24 #include "dm_log.h"
25 #include "parameter.h"
26 
27 namespace OHOS {
28 namespace DistributedHardware {
29 DM_IMPLEMENT_SINGLE_INSTANCE(DmRadarHelper);
ReportDiscoverRegCallback(struct RadarInfo & info)30 bool DmRadarHelper::ReportDiscoverRegCallback(struct RadarInfo &info)
31 {
32     return true;
33 }
34 
ReportDiscoverResCallback(struct RadarInfo & info)35 bool DmRadarHelper::ReportDiscoverResCallback(struct RadarInfo &info)
36 {
37     return true;
38 }
39 
ReportDiscoverUserRes(struct RadarInfo & info)40 bool DmRadarHelper::ReportDiscoverUserRes(struct RadarInfo &info)
41 {
42     return true;
43 }
44 
ReportAuthStart(const std::string & peerUdid,const std::string & pkgName)45 bool DmRadarHelper::ReportAuthStart(const std::string &peerUdid, const std::string &pkgName)
46 {
47     return true;
48 }
49 
ReportAuthOpenSession(struct RadarInfo & info)50 bool DmRadarHelper::ReportAuthOpenSession(struct RadarInfo &info)
51 {
52     return true;
53 }
54 
ReportAuthSessionOpenCb(struct RadarInfo & info)55 bool DmRadarHelper::ReportAuthSessionOpenCb(struct RadarInfo &info)
56 {
57     return true;
58 }
59 
ReportAuthSendRequest(struct RadarInfo & info)60 bool DmRadarHelper::ReportAuthSendRequest(struct RadarInfo &info)
61 {
62     return true;
63 }
64 
ReportAuthPullAuthBox(struct RadarInfo & info)65 bool DmRadarHelper::ReportAuthPullAuthBox(struct RadarInfo &info)
66 {
67     return true;
68 }
69 
ReportAuthConfirmBox(struct RadarInfo & info)70 bool DmRadarHelper::ReportAuthConfirmBox(struct RadarInfo &info)
71 {
72     return true;
73 }
74 
ReportAuthCreateGroup(struct RadarInfo & info)75 bool DmRadarHelper::ReportAuthCreateGroup(struct RadarInfo &info)
76 {
77     return true;
78 }
79 
ReportAuthCreateGroupCb(std::string funcName,int32_t stageRes)80 bool DmRadarHelper::ReportAuthCreateGroupCb(std::string funcName, int32_t stageRes)
81 {
82     return true;
83 }
84 
ReportAuthPullPinBox(struct RadarInfo & info)85 bool DmRadarHelper::ReportAuthPullPinBox(struct RadarInfo &info)
86 {
87     return true;
88 }
89 
ReportAuthInputPinBox(struct RadarInfo & info)90 bool DmRadarHelper::ReportAuthInputPinBox(struct RadarInfo &info)
91 {
92     return true;
93 }
94 
ReportAuthAddGroup(struct RadarInfo & info)95 bool DmRadarHelper::ReportAuthAddGroup(struct RadarInfo &info)
96 {
97     return true;
98 }
99 
ReportAuthAddGroupCb(std::string funcName,int32_t stageRes)100 bool DmRadarHelper::ReportAuthAddGroupCb(std::string funcName, int32_t stageRes)
101 {
102     return true;
103 }
104 
ReportNetworkOnline(struct RadarInfo & info)105 bool DmRadarHelper::ReportNetworkOnline(struct RadarInfo &info)
106 {
107     return true;
108 }
109 
ReportNetworkOffline(struct RadarInfo & info)110 bool DmRadarHelper::ReportNetworkOffline(struct RadarInfo &info)
111 {
112     return true;
113 }
114 
ReportDeleteTrustRelation(struct RadarInfo & info)115 bool DmRadarHelper::ReportDeleteTrustRelation(struct RadarInfo &info)
116 {
117     return true;
118 }
119 
ReportGetTrustDeviceList(struct RadarInfo & info)120 bool DmRadarHelper::ReportGetTrustDeviceList(struct RadarInfo &info)
121 {
122     return true;
123 }
124 
ReportCreatePinHolder(std::string hostName,int32_t channelId,std::string peerUdid,int32_t errCode,int32_t stageRes)125 void DmRadarHelper::ReportCreatePinHolder(std::string hostName,
126     int32_t channelId, std::string peerUdid, int32_t errCode, int32_t stageRes)
127 {
128     return;
129 }
130 
ReportDestroyPinHolder(std::string hostName,std::string peerUdid,int32_t errCode,int32_t stageRes)131 void DmRadarHelper::ReportDestroyPinHolder(std::string hostName,
132     std::string peerUdid, int32_t errCode, int32_t stageRes)
133 {
134     return;
135 }
136 
ReportSendOrReceiveHolderMsg(int32_t bizStage,std::string funcName,std::string peerUdid)137 void DmRadarHelper::ReportSendOrReceiveHolderMsg(int32_t bizStage, std::string funcName, std::string peerUdid)
138 {
139     return;
140 }
141 
ConvertHexToString(uint16_t hex)142 std::string DmRadarHelper::ConvertHexToString(uint16_t hex)
143 {
144     std::stringstream str;
145     int32_t with = 3;
146     str << std::hex << std::setw(with) << std::setfill('0') << hex;
147     std::string hexStr = str.str();
148     transform(hexStr.begin(), hexStr.end(), hexStr.begin(), ::toupper);
149     return hexStr;
150 }
151 
GetDeviceInfoList(std::vector<DmDeviceInfo> & deviceInfoList)152 std::string DmRadarHelper::GetDeviceInfoList(std::vector<DmDeviceInfo> &deviceInfoList)
153 {
154     cJSON* deviceInfoJson = cJSON_CreateArray();
155     for (size_t i = 0; i < deviceInfoList.size(); i++) {
156         cJSON* object = cJSON_CreateObject();
157         std::string udidHash = GetUdidHashByUdid(std::string(deviceInfoList[i].deviceId));
158         cJSON_AddStringToObject(object, "PEER_UDID", udidHash.c_str());
159         cJSON_AddStringToObject(object, "PEER_NET_ID", deviceInfoList[i].networkId);
160         std::string devType = ConvertHexToString(deviceInfoList[i].deviceTypeId);
161         cJSON_AddStringToObject(object, "PEER_DEV_TYPE", devType.c_str());
162         cJSON_AddStringToObject(object, "PEER_DEV_NAME", deviceInfoList[i].deviceName);
163         cJSON_AddItemToArray(deviceInfoJson, object);
164     }
165     return std::string(cJSON_PrintUnformatted(deviceInfoJson));
166 }
167 
GetUdidHashByUdid(std::string udid)168 std::string DmRadarHelper::GetUdidHashByUdid(std::string udid)
169 {
170     char udidHash[DM_MAX_DEVICE_ID_LEN] = {0};
171     if (Crypto::GetUdidHash(udid, reinterpret_cast<uint8_t *>(udidHash)) != DM_OK) {
172         return "";
173     }
174     return GetAnonyUdid(std::string(udidHash));
175 }
176 
GetAnonyUdid(std::string udid)177 std::string DmRadarHelper::GetAnonyUdid(std::string udid)
178 {
179     if (udid.empty() || udid.length() < INVALID_UDID_LENGTH) {
180         return "unknown";
181     }
182     return udid.substr(0, SUBSTR_UDID_LENGTH) + "**" + udid.substr(udid.length() - SUBSTR_UDID_LENGTH);
183 }
184 
GetAnonyLocalUdid()185 std::string DmRadarHelper::GetAnonyLocalUdid()
186 {
187     char localDeviceId[DEVICE_UUID_LENGTH] = {0};
188     GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH);
189     return GetAnonyUdid(std::string(localDeviceId));
190 }
191 
GetErrCode(int32_t errCode)192 int32_t DmRadarHelper::GetErrCode(int32_t errCode)
193 {
194     auto flag = MAP_ERROR_CODE.find(errCode);
195     if (flag == MAP_ERROR_CODE.end()) {
196         return errCode;
197     }
198     return flag->second;
199 }
200 
CreateDmRadarInstance()201 IDmRadarHelper *CreateDmRadarInstance()
202 {
203     return &DmRadarHelper::GetInstance();
204 }
205 } // namespace DistributedHardware
206 } // namespace OHOS
207