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
16 #include "lnn_p2p_info.h"
17
18 #include <securec.h>
19
20 #include "anonymizer.h"
21 #include "auth_device_common_key.h"
22 #include "bus_center_manager.h"
23 #include "lnn_async_callback_utils.h"
24 #include "lnn_distributed_net_ledger.h"
25 #include "lnn_feature_capability.h"
26 #include "lnn_local_net_ledger.h"
27 #include "lnn_log.h"
28 #include "lnn_secure_storage.h"
29 #include "lnn_sync_info_manager.h"
30 #include "softbus_adapter_crypto.h"
31 #include "softbus_adapter_json.h"
32 #include "softbus_adapter_mem.h"
33 #include "softbus_def.h"
34 #include "softbus_errcode.h"
35 #include "softbus_json_utils.h"
36 #include "wifi_direct_manager.h"
37
38 #define JSON_KEY_P2P_ROLE "P2P_ROLE"
39 #define JSON_KEY_WIFI_CFG "WIFI_CFG"
40 #define JSON_KEY_CHAN_LIST_5G "CHAN_LIST_5G"
41 #define JSON_KEY_STA_FREQUENCY "STA_FREQUENCY"
42 #define JSON_KEY_P2P_MAC "P2P_MAC"
43 #define JSON_KEY_GO_MAC "GO_MAC"
44 #define JSON_KEY_WIFIDIRECT_ADDR "WIFIDIRECT_ADDR"
45
LnnGetP2pInfoMsg(const P2pInfo * info)46 static char *LnnGetP2pInfoMsg(const P2pInfo *info)
47 {
48 cJSON *json = cJSON_CreateObject();
49 if (json == NULL) {
50 LNN_LOGE(LNN_BUILDER, "create p2p info json fail");
51 return NULL;
52 }
53 if (!AddNumberToJsonObject(json, JSON_KEY_P2P_ROLE, info->p2pRole)) {
54 LNN_LOGE(LNN_BUILDER, "add p2p role fail");
55 cJSON_Delete(json);
56 return NULL;
57 }
58 if (!AddStringToJsonObject(json, JSON_KEY_WIFI_CFG, info->wifiCfg)) {
59 LNN_LOGE(LNN_BUILDER, "add wifi cfg fail");
60 cJSON_Delete(json);
61 return NULL;
62 }
63 if (!AddStringToJsonObject(json, JSON_KEY_CHAN_LIST_5G, info->chanList5g)) {
64 LNN_LOGE(LNN_BUILDER, "add chan list 5g fail");
65 cJSON_Delete(json);
66 return NULL;
67 }
68 if (!AddNumberToJsonObject(json, JSON_KEY_STA_FREQUENCY, info->staFrequency)) {
69 LNN_LOGE(LNN_BUILDER, "add sta frequency fail");
70 cJSON_Delete(json);
71 return NULL;
72 }
73 if (!AddStringToJsonObject(json, JSON_KEY_P2P_MAC, info->p2pMac)) {
74 LNN_LOGE(LNN_BUILDER, "add p2p mac fail");
75 cJSON_Delete(json);
76 return NULL;
77 }
78 if (!AddStringToJsonObject(json, JSON_KEY_GO_MAC, info->goMac)) {
79 LNN_LOGE(LNN_BUILDER, "add go mac fail");
80 cJSON_Delete(json);
81 return NULL;
82 }
83 char *msg = cJSON_PrintUnformatted(json);
84 if (msg == NULL) {
85 LNN_LOGE(LNN_BUILDER, "unformat p2p info fail");
86 }
87 cJSON_Delete(json);
88 return msg;
89 }
90
LnnGetWifiDirectAddrMsg(const NodeInfo * info)91 static char *LnnGetWifiDirectAddrMsg(const NodeInfo *info)
92 {
93 cJSON *json = cJSON_CreateObject();
94 if (json == NULL) {
95 LNN_LOGE(LNN_BUILDER, "create wifidirect addr json fail");
96 return NULL;
97 }
98 if (!AddStringToJsonObject(json, JSON_KEY_WIFIDIRECT_ADDR, info->wifiDirectAddr)) {
99 LNN_LOGE(LNN_BUILDER, "add wifidirect addr fail");
100 cJSON_Delete(json);
101 return NULL;
102 }
103 char *msg = cJSON_PrintUnformatted(json);
104 if (msg == NULL) {
105 LNN_LOGE(LNN_BUILDER, "unformat wifidirect addr fail");
106 }
107 cJSON_Delete(json);
108 return msg;
109 }
110
LnnParseP2pInfoMsg(const char * msg,P2pInfo * info,uint32_t len)111 static int32_t LnnParseP2pInfoMsg(const char *msg, P2pInfo *info, uint32_t len)
112 {
113 JsonObj *json = JSON_Parse((char *)msg, len);
114 if (json == NULL) {
115 LNN_LOGE(LNN_BUILDER, "parse p2p info json fail");
116 return SOFTBUS_PARSE_JSON_ERR;
117 }
118 if (!JSON_GetInt32FromOject(json, JSON_KEY_P2P_ROLE, &info->p2pRole)) {
119 LNN_LOGE(LNN_BUILDER, "p2p role not found");
120 JSON_Delete(json);
121 return SOFTBUS_ERR;
122 }
123 if (!JSON_GetStringFromOject(json, JSON_KEY_WIFI_CFG, info->wifiCfg, sizeof(info->wifiCfg))) {
124 LNN_LOGE(LNN_BUILDER, "wifi cfg not found");
125 JSON_Delete(json);
126 return SOFTBUS_ERR;
127 }
128 if (!JSON_GetStringFromOject(json, JSON_KEY_CHAN_LIST_5G, info->chanList5g, sizeof(info->chanList5g))) {
129 LNN_LOGE(LNN_BUILDER, "chan list 5g not found");
130 JSON_Delete(json);
131 return SOFTBUS_ERR;
132 }
133 if (!JSON_GetInt32FromOject(json, JSON_KEY_STA_FREQUENCY, &info->staFrequency)) {
134 LNN_LOGE(LNN_BUILDER, "sta frequency not found");
135 JSON_Delete(json);
136 return SOFTBUS_ERR;
137 }
138 if (!JSON_GetStringFromOject(json, JSON_KEY_P2P_MAC, info->p2pMac, sizeof(info->p2pMac))) {
139 LNN_LOGE(LNN_BUILDER, "p2p mac not found");
140 JSON_Delete(json);
141 return SOFTBUS_ERR;
142 }
143 if (!JSON_GetStringFromOject(json, JSON_KEY_GO_MAC, info->goMac, sizeof(info->goMac))) {
144 LNN_LOGE(LNN_BUILDER, "go mac not found");
145 JSON_Delete(json);
146 return SOFTBUS_ERR;
147 }
148 JSON_Delete(json);
149 return SOFTBUS_OK;
150 }
151
LnnParseWifiDirectAddrMsg(const char * msg,char * wifiDirectAddr,uint32_t len)152 static int32_t LnnParseWifiDirectAddrMsg(const char *msg, char *wifiDirectAddr, uint32_t len)
153 {
154 JsonObj *json = JSON_Parse((char *)msg, len);
155 if (json == NULL) {
156 LNN_LOGE(LNN_BUILDER, "parse wifidirect addr json fail");
157 return SOFTBUS_PARSE_JSON_ERR;
158 }
159 if (!JSON_GetStringFromOject(json, JSON_KEY_WIFIDIRECT_ADDR, wifiDirectAddr, MAC_LEN)) {
160 LNN_LOGE(LNN_BUILDER, "wifidirect addr not found");
161 JSON_Delete(json);
162 return SOFTBUS_ERR;
163 }
164 JSON_Delete(json);
165 return SOFTBUS_OK;
166 }
167
IsNeedSyncP2pInfo(const NodeInfo * localInfo,const NodeBasicInfo * info)168 static bool IsNeedSyncP2pInfo(const NodeInfo *localInfo, const NodeBasicInfo *info)
169 {
170 int32_t osType = 0;
171 // rk need to sync
172 if (!IsFeatureSupport(localInfo->feature, BIT_WIFI_DIRECT_TLV_NEGOTIATION)) {
173 return true;
174 }
175 if (LnnGetOsTypeByNetworkId(info->networkId, &osType) != SOFTBUS_OK) {
176 LNN_LOGE(LNN_BUILDER, "get remote osType fail");
177 }
178 if (osType != OH_OS_TYPE) {
179 LNN_LOGE(LNN_BUILDER, "remote osType is %{public}d, need sync p2pinfo", osType);
180 return true;
181 }
182 return false;
183 }
184
ProcessSyncP2pInfo(void * para)185 static void ProcessSyncP2pInfo(void *para)
186 {
187 (void)para;
188 int32_t i;
189 int32_t infoNum = 0;
190 uint32_t len;
191 NodeBasicInfo *info = NULL;
192 if (LnnGetAllOnlineAndMetaNodeInfo(&info, &infoNum) != SOFTBUS_OK) {
193 LNN_LOGE(LNN_BUILDER, "get all online node info fail");
194 return;
195 }
196 if (infoNum == 0) {
197 LNN_LOGI(LNN_BUILDER, "online device num is 0, not need to sync p2p info");
198 return;
199 }
200
201 const NodeInfo *localInfo = LnnGetLocalNodeInfo();
202 if (localInfo == NULL) {
203 SoftBusFree(info);
204 return;
205 }
206 char *msg = LnnGetP2pInfoMsg(&localInfo->p2pInfo);
207 if (msg == NULL) {
208 LNN_LOGE(LNN_BUILDER, "get p2p info msg fail");
209 SoftBusFree(info);
210 return;
211 }
212 len = strlen(msg) + 1; /* add 1 for '\0' */
213 for (i = 0; i < infoNum; i++) {
214 if (LnnIsLSANode(&info[i])) {
215 continue;
216 }
217 if (IsNeedSyncP2pInfo(localInfo, &info[i]) &&
218 LnnSendSyncInfoMsg(LNN_INFO_TYPE_P2P_INFO, info[i].networkId, (uint8_t *)msg, len, NULL) != SOFTBUS_OK) {
219 char *anonyDeviceName = NULL;
220 Anonymize(info[i].deviceName, &anonyDeviceName);
221 LNN_LOGE(LNN_BUILDER, "sync p2p info fail. deviceName=%{public}s", anonyDeviceName);
222 AnonymizeFree(anonyDeviceName);
223 }
224 }
225 cJSON_free(msg);
226 SoftBusFree(info);
227 LNN_LOGI(LNN_BUILDER, "sync p2p info done");
228 }
229
ProcessSyncWifiDirectAddr(void * para)230 static void ProcessSyncWifiDirectAddr(void *para)
231 {
232 (void)para;
233 int32_t i;
234 int32_t infoNum = 0;
235 uint32_t len;
236 NodeBasicInfo *info = NULL;
237 if (LnnGetAllOnlineAndMetaNodeInfo(&info, &infoNum) != SOFTBUS_OK) {
238 LNN_LOGE(LNN_BUILDER, "get all online node info fail");
239 return;
240 }
241 if (infoNum == 0) {
242 LNN_LOGI(LNN_BUILDER, "online device num is 0, not need to sync wifidirect addr");
243 return;
244 }
245 const NodeInfo *localInfo = LnnGetLocalNodeInfo();
246 if (localInfo == NULL) {
247 SoftBusFree(info);
248 return;
249 }
250 char *msg = LnnGetWifiDirectAddrMsg(localInfo);
251 if (msg == NULL) {
252 LNN_LOGE(LNN_BUILDER, "get wifidirect addr msg fail");
253 SoftBusFree(info);
254 return;
255 }
256 len = strlen(msg) + 1; /* add 1 for '\0' */
257 for (i = 0; i < infoNum; i++) {
258 if (LnnIsLSANode(&info[i])) {
259 continue;
260 }
261 int32_t osType = 0;
262 if (LnnGetOsTypeByNetworkId(info[i].networkId, &osType) != SOFTBUS_OK) {
263 LNN_LOGE(LNN_BUILDER, "get remote osType fail");
264 }
265 if (osType != OH_OS_TYPE &&
266 LnnSendSyncInfoMsg(LNN_INFO_TYPE_WIFI_DIRECT, info[i].networkId, (uint8_t *)msg, len, NULL)
267 != SOFTBUS_OK) {
268 char *anonyNetworkId = NULL;
269 Anonymize(info[i].networkId, &anonyNetworkId);
270 LNN_LOGE(LNN_BUILDER, "sync wifidirect addr fail. anonyNetworkId=%{public}s", anonyNetworkId);
271 AnonymizeFree(anonyNetworkId);
272 }
273 }
274 cJSON_free(msg);
275 SoftBusFree(info);
276 LNN_LOGI(LNN_BUILDER, "sync wifidirect addr done");
277 }
278
OnReceiveP2pSyncInfoMsg(LnnSyncInfoType type,const char * networkId,const uint8_t * msg,uint32_t len)279 static void OnReceiveP2pSyncInfoMsg(LnnSyncInfoType type, const char *networkId, const uint8_t *msg, uint32_t len)
280 {
281 LNN_LOGI(LNN_BUILDER, "Recv p2p info, type=%{public}d, len=%{public}d", type, len);
282 if (type != LNN_INFO_TYPE_P2P_INFO) {
283 return;
284 }
285 if (msg == NULL || len == 0) {
286 return;
287 }
288 P2pInfo p2pInfo = {0};
289 if (LnnParseP2pInfoMsg((const char *)msg, &p2pInfo, len) != SOFTBUS_OK) {
290 LNN_LOGE(LNN_BUILDER, "parse p2p info fail");
291 return;
292 }
293 if (!LnnSetDLP2pInfo(networkId, &p2pInfo)) {
294 LNN_LOGE(LNN_BUILDER, "set p2p info fail");
295 }
296 }
297
OnReceiveWifiDirectSyncAddr(LnnSyncInfoType type,const char * networkId,const uint8_t * msg,uint32_t len)298 static void OnReceiveWifiDirectSyncAddr(LnnSyncInfoType type, const char *networkId, const uint8_t *msg, uint32_t len)
299 {
300 LNN_LOGI(LNN_BUILDER, "Recv wifidirect addr, type=%{public}d, len=%{public}d", type, len);
301 if (type != LNN_INFO_TYPE_WIFI_DIRECT) {
302 LNN_LOGI(LNN_BUILDER, "lnnsyncinfo type is null");
303 return;
304 }
305 if (msg == NULL || len == 0) {
306 LNN_LOGI(LNN_BUILDER, "invalid null msg or len");
307 return;
308 }
309 char wifiDirectAddr[MAC_LEN] = { 0 };
310 if (LnnParseWifiDirectAddrMsg((const char *)msg, wifiDirectAddr, len) != SOFTBUS_OK) {
311 LNN_LOGE(LNN_BUILDER, "parse wifidirect addr fail");
312 return;
313 }
314 if (!LnnSetDLWifiDirectAddr(networkId, wifiDirectAddr)) {
315 LNN_LOGE(LNN_BUILDER, "set wifidirect addr fail");
316 }
317 }
318
LnnSyncP2pInfo(void)319 int32_t LnnSyncP2pInfo(void)
320 {
321 int32_t rc = LnnAsyncCallbackHelper(GetLooper(LOOP_TYPE_DEFAULT), ProcessSyncP2pInfo, NULL);
322 if (rc != SOFTBUS_OK) {
323 LNN_LOGE(LNN_BUILDER, "async p2p info fail, rc=%{public}d", rc);
324 return SOFTBUS_ERR;
325 }
326 return SOFTBUS_OK;
327 }
328
LnnSyncWifiDirectAddr(void)329 int32_t LnnSyncWifiDirectAddr(void)
330 {
331 int32_t rc = LnnAsyncCallbackHelper(GetLooper(LOOP_TYPE_DEFAULT), ProcessSyncWifiDirectAddr, NULL);
332 if (rc != SOFTBUS_OK) {
333 LNN_LOGE(LNN_BUILDER, "async wifidirect addr fail, rc=%{public}d", rc);
334 return SOFTBUS_ERR;
335 }
336 return SOFTBUS_OK;
337 }
338
LnnInitLocalP2pInfo(NodeInfo * info)339 int32_t LnnInitLocalP2pInfo(NodeInfo *info)
340 {
341 if (info == NULL) {
342 return SOFTBUS_INVALID_PARAM;
343 }
344 bool isSupportBle = info->netCapacity & (1 << BIT_BLE);
345 bool isSupportP2p = info->netCapacity & (1 << BIT_WIFI_P2P);
346 if (LnnSetP2pRole(info, WIFI_DIRECT_ROLE_NONE) != SOFTBUS_OK ||
347 LnnSetP2pMac(info, "") != SOFTBUS_OK ||
348 LnnSetP2pGoMac(info, "") != SOFTBUS_OK ||
349 LnnSetWifiDirectAddr(info, "") != SOFTBUS_OK) {
350 LNN_LOGE(LNN_BUILDER, "init p2p info fail");
351 return SOFTBUS_ERR;
352 }
353 info->isBleP2p = (isSupportBle && isSupportP2p);
354 return SOFTBUS_OK;
355 }
356
LnnInitP2p(void)357 int32_t LnnInitP2p(void)
358 {
359 if (LnnInitPtk() != SOFTBUS_OK) {
360 LNN_LOGE(LNN_INIT, "init ptk fail");
361 }
362 return LnnRegSyncInfoHandler(LNN_INFO_TYPE_P2P_INFO, OnReceiveP2pSyncInfoMsg);
363 }
364
LnnDeinitP2p(void)365 void LnnDeinitP2p(void)
366 {
367 LnnDeinitPtk();
368 (void)LnnUnregSyncInfoHandler(LNN_INFO_TYPE_P2P_INFO, OnReceiveP2pSyncInfoMsg);
369 }
370
LnnInitWifiDirect(void)371 int32_t LnnInitWifiDirect(void)
372 {
373 return LnnRegSyncInfoHandler(LNN_INFO_TYPE_WIFI_DIRECT, OnReceiveWifiDirectSyncAddr);
374 }
375
LnnDeinitWifiDirect(void)376 void LnnDeinitWifiDirect(void)
377 {
378 (void)LnnUnregSyncInfoHandler(LNN_INFO_TYPE_WIFI_DIRECT, OnReceiveWifiDirectSyncAddr);
379 }
380