• 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 "instant_statistics.h"
17 
18 #include "bus_center_manager.h"
19 #include "comm_log.h"
20 #include "communication_radar.h"
21 #include "data/link_manager.h"
22 #include "lnn_distributed_net_ledger.h"
23 #include "lnn_lane_interface.h"
24 #include "softbus_adapter_mem.h"
25 #include "softbus_conn_interface.h"
26 #include "softbus_error_code.h"
27 #include "legacy/softbus_hisysevt_transreporter.h"
28 #include "softbus_json_utils.h"
29 #include "softbus_proxychannel_manager.h"
30 #include "softbus_utils.h"
31 #include "trans_auth_manager.h"
32 #include "trans_channel_common.h"
33 #include "trans_event.h"
34 #include "trans_tcp_direct_sessionconn.h"
35 #include "trans_udp_channel_manager.h"
36 #include "wifi_statistic.h"
37 #include "bt_statistic.h"
38 #include "anonymizer.h"
39 
40 using namespace OHOS::SoftBus;
41 
42 static constexpr const uint32_t INST_MAX_REMOTE_NUM = 100;
43 static constexpr const int INST_MAX_CHANNEL_NUM_EACH = 100;
44 
45 static constexpr const uint32_t UNKNOWN_IP = 0;
46 static constexpr const uint32_t HML_IP = 1;
47 static constexpr const uint32_t P2P_IP = 2;
48 static constexpr const uint32_t WLAN_IP = 3;
49 
50 static constexpr const int INST_MINUTE_TIME = 60 * 1000;
51 static constexpr const int INST_DELAY_REGISTER = 0;
52 using InstAsyncCallbackFunc = void (*)(SoftBusMessage *);
53 
54 using InstantChannelInfo = struct {
55     ListNode node;
56     std::string socketName;
57     int32_t channelType;
58     int32_t appType;
59     int32_t laneLinkType;
60     int32_t connectType;
61     int32_t status;
62     bool serverSide;
63     int64_t startTime;
64 };
65 
66 using InstantRemoteInfo = struct {
67     ListNode node;
68     uint16_t deviceType;
69     std::string udid;
70     std::string uuid;
71     std::string hmlMac;
72     std::string p2pMac;
73     std::string hmlIp;
74     std::string p2pIp;
75     std::string wlanIp;
76     std::string bleMac;
77     std::string brMac;
78     int32_t p2pRole;
79     int32_t p2pFreq;
80     int32_t hmlFreq;
81     int32_t staFreq;
82     int32_t p2pLinkState;
83     int32_t hmlLinkState;
84     uint32_t discoveryType;
85     uint32_t netCapability;
86     int32_t channelNum;
87     ListNode channels;
88 };
89 
AnonymizeStr(const std::string & data)90 static std::string AnonymizeStr(const std::string &data)
91 {
92     if (data.empty()) {
93         return "";
94     }
95     char *temp = nullptr;
96     Anonymize(data.c_str(), &temp);
97     std::string result = AnonymizeWrapper(temp);
98     AnonymizeFree(temp);
99     return result;
100 }
101 
InstPackAndAnonymizeStringIfNotNull(cJSON * json,std::string & str,const char * key,bool isDeviceId)102 static void InstPackAndAnonymizeStringIfNotNull(cJSON *json, std::string &str, const char *key, bool isDeviceId)
103 {
104     if (json == NULL || str.empty() || key == NULL) {
105         return;
106     }
107     if (isDeviceId) {
108         (void)AddStringToJsonObject(json, key, AnonymizeStr(str).c_str());
109     } else {
110         (void)AddStringToJsonObject(json, key, AnonymizeStr(str).c_str());
111     }
112 }
113 
InstantIsParaMatch(const std::string & dst,const std::string & src)114 static bool InstantIsParaMatch(const std::string &dst, const std::string &src)
115 {
116     if (dst.empty() || src.empty() || dst.compare(src) != 0) {
117         return false;
118     }
119     return true;
120 }
121 
InstSetPeerDeviceIdForRemoteInfo(std::string & dst,const std::string & src)122 static int32_t InstSetPeerDeviceIdForRemoteInfo(std::string &dst, const std::string &src)
123 {
124     if (!dst.empty() || src.empty()) {
125         return SOFTBUS_INVALID_PARAM;
126     }
127     dst = src;
128     return SOFTBUS_OK;
129 }
130 
131 #ifdef DSOFTBUS_FEATURE_CONN_PV1
InstUpdateRemoteInfoByInnerLink(InstantRemoteInfo * remoteInfo,const InnerLinkBasicInfo & link,const std::string & remoteUuid)132 static void InstUpdateRemoteInfoByInnerLink(InstantRemoteInfo *remoteInfo,
133     const InnerLinkBasicInfo &link, const std::string &remoteUuid)
134 {
135     if (remoteInfo == NULL) {
136         COMM_LOGE(COMM_DFX, "invalid param");
137         return;
138     }
139     if (remoteUuid.empty()) {
140         return;
141     }
142     InstSetPeerDeviceIdForRemoteInfo(remoteInfo->uuid, remoteUuid);
143     InnerLink::LinkType linkType = link.linkType;
144     if (linkType != InnerLink::LinkType::P2P && linkType != InnerLink::LinkType::HML) {
145         return;
146     }
147     InnerLink::LinkState state = link.state;
148     if (linkType == InnerLink::LinkType::P2P) {
149         remoteInfo->p2pFreq = link.freq;
150         remoteInfo->p2pLinkState = (int32_t)state;
151         InstSetPeerDeviceIdForRemoteInfo(remoteInfo->p2pMac, link.remoteBaseMac);
152         InstSetPeerDeviceIdForRemoteInfo(remoteInfo->p2pIp, link.remoteIpv4);
153     } else if (linkType == InnerLink::LinkType::HML) {
154         remoteInfo->hmlFreq = link.freq;
155         remoteInfo->hmlLinkState = (int32_t)state;
156         InstSetPeerDeviceIdForRemoteInfo(remoteInfo->hmlMac, link.remoteBaseMac);
157         InstSetPeerDeviceIdForRemoteInfo(remoteInfo->hmlIp, link.remoteIpv4);
158     }
159 }
160 #endif
161 
InstCreateAndAddRemoteInfo(SoftBusList * remoteChannelInfoList,bool matched)162 static InstantRemoteInfo *InstCreateAndAddRemoteInfo(SoftBusList *remoteChannelInfoList, bool matched)
163 {
164     if (matched) {
165         return NULL;
166     }
167     if (remoteChannelInfoList->cnt >= INST_MAX_REMOTE_NUM) {
168         return NULL;
169     }
170     InstantRemoteInfo *rInfo = static_cast<InstantRemoteInfo *>(SoftBusCalloc(sizeof(InstantRemoteInfo)));
171     if (rInfo == NULL) {
172         COMM_LOGE(COMM_DFX, "malloc remote channel info fail");
173         return NULL;
174     }
175     ListInit(&rInfo->node);
176     ListInit(&rInfo->channels);
177     ListAdd(&remoteChannelInfoList->list, &rInfo->node);
178     remoteChannelInfoList->cnt++;
179     return rInfo;
180 }
181 
182 #ifdef DSOFTBUS_FEATURE_CONN_PV1
InstAddRemoteInfoByLinkManager(SoftBusList * remoteChannelInfoList)183 static void InstAddRemoteInfoByLinkManager(SoftBusList *remoteChannelInfoList)
184 {
185     std::vector<InnerLinkBasicInfo> links;
186     LinkManager::GetInstance().GetAllLinksBasicInfo(links);
187     for (const auto &link : links) {
188         std::string remoteUuid = link.remoteDeviceId;
189         if (remoteUuid.empty()) {
190             continue;
191         }
192         InstantRemoteInfo *rInfo = NULL;
193         bool matched = false;
194         LIST_FOR_EACH_ENTRY(rInfo, &remoteChannelInfoList->list, InstantRemoteInfo, node) {
195             if (InstantIsParaMatch(rInfo->uuid, remoteUuid)) {
196                 matched = true;
197                 InstUpdateRemoteInfoByInnerLink(rInfo, link, remoteUuid);
198                 break;
199             }
200         }
201         rInfo = InstCreateAndAddRemoteInfo(remoteChannelInfoList, matched);
202         if (rInfo == NULL) {
203             continue;
204         }
205         InstUpdateRemoteInfoByInnerLink(rInfo, link, remoteUuid);
206     }
207     links.clear();
208 }
209 #endif
210 
InstGetIpFromLinkTypeOrConnectType(const InstantRemoteInfo * remoteInfo,int32_t linkType,int32_t connectType)211 static int32_t InstGetIpFromLinkTypeOrConnectType(const InstantRemoteInfo *remoteInfo, int32_t linkType,
212     int32_t connectType)
213 {
214     if (remoteInfo == NULL) {
215         COMM_LOGE(COMM_DFX, "invalid param");
216         return UNKNOWN_IP;
217     }
218     if (linkType == LANE_WLAN_2P4G || linkType == LANE_WLAN_5G || connectType == CONNECT_TCP) {
219         return WLAN_IP;
220     }
221     if (linkType == LANE_P2P || linkType == LANE_P2P_REUSE || connectType == CONNECT_P2P ||
222         connectType == CONNECT_P2P_REUSE) {
223         return P2P_IP;
224     }
225     if (linkType == LANE_HML || connectType == CONNECT_HML || connectType == CONNECT_TRIGGER_HML) {
226         return HML_IP;
227     }
228     return UNKNOWN_IP;
229 }
230 
InstIsMatchSessionConn(const InstantRemoteInfo * rInfo,const SessionConn * conn)231 static bool InstIsMatchSessionConn(const InstantRemoteInfo *rInfo, const SessionConn *conn)
232 {
233     if (rInfo == NULL || conn == NULL) {
234         COMM_LOGE(COMM_DFX, "invalid param");
235         return false;
236     }
237     if (InstantIsParaMatch(rInfo->uuid, std::string(conn->appInfo.peerData.deviceId))) {
238         return true;
239     }
240     int32_t type = InstGetIpFromLinkTypeOrConnectType(rInfo, conn->appInfo.linkType, conn->appInfo.connectType);
241     switch (type) {
242         case HML_IP:
243             return InstantIsParaMatch(rInfo->hmlIp, conn->appInfo.peerData.addr);
244         case P2P_IP:
245             return InstantIsParaMatch(rInfo->p2pIp, conn->appInfo.peerData.addr);
246         case WLAN_IP:
247             return InstantIsParaMatch(rInfo->wlanIp, conn->appInfo.peerData.addr);
248         default:
249             return false;
250     }
251 }
252 
GetUdidByTcpChannelInfo(const TcpChannelInfo * conn)253 static std::string GetUdidByTcpChannelInfo(const TcpChannelInfo *conn)
254 {
255     if (conn->channelType == CHANNEL_TYPE_TCP_DIRECT || conn->channelType == CHANNEL_TYPE_PROXY ||
256         conn->channelType == CHANNEL_TYPE_UDP) {
257         char peerUdid[DEVICE_ID_SIZE_MAX] = { 0 };
258         GetRemoteUdidWithNetworkId(conn->peerDeviceId, peerUdid, sizeof(peerUdid));
259         return std::string(peerUdid);
260     } else if (conn->channelType == CHANNEL_TYPE_AUTH) {
261         return std::string(conn->peerDeviceId);
262     }
263     return "";
264 }
265 
InstIsMatchTcpChannel(const InstantRemoteInfo * rInfo,const TcpChannelInfo * conn)266 static bool InstIsMatchTcpChannel(const InstantRemoteInfo *rInfo, const TcpChannelInfo *conn)
267 {
268     if (rInfo == nullptr || conn == nullptr) {
269         COMM_LOGE(COMM_DFX, "invalid param");
270         return false;
271     }
272     if (InstantIsParaMatch(rInfo->uuid, GetUdidByTcpChannelInfo(conn))) {
273         return true;
274     }
275     int32_t type = InstGetIpFromLinkTypeOrConnectType(rInfo, conn->linkType, conn->connectType);
276     switch (type) {
277         case HML_IP:
278             return InstantIsParaMatch(rInfo->hmlIp, conn->peerIp);
279         case P2P_IP:
280             return InstantIsParaMatch(rInfo->p2pIp, conn->peerIp);
281         case WLAN_IP:
282             return InstantIsParaMatch(rInfo->wlanIp, conn->peerIp);
283         default:
284             return false;
285     }
286 }
287 
InstSetIpForRemoteInfo(InstantRemoteInfo * remoteInfo,const AppInfo * appInfo)288 static void InstSetIpForRemoteInfo(InstantRemoteInfo *remoteInfo, const AppInfo *appInfo)
289 {
290     if (remoteInfo == NULL || appInfo == NULL) {
291         COMM_LOGE(COMM_DFX, "invalid param");
292         return;
293     }
294     int32_t type = InstGetIpFromLinkTypeOrConnectType(remoteInfo, appInfo->linkType, appInfo->connectType);
295     switch (type) {
296         case HML_IP:
297             remoteInfo->hmlIp = appInfo->peerData.addr;
298             break;
299         case P2P_IP:
300             remoteInfo->p2pIp = appInfo->peerData.addr;
301             break;
302         case WLAN_IP:
303             remoteInfo->wlanIp = appInfo->peerData.addr;
304             break;
305         default:
306             break;
307     }
308 }
309 
InstSetUdidForRemoteInfoByUuid(InstantRemoteInfo * remoteInfo)310 static void InstSetUdidForRemoteInfoByUuid(InstantRemoteInfo *remoteInfo)
311 {
312     if (remoteInfo == NULL) {
313         COMM_LOGE(COMM_DFX, "invalid param");
314         return;
315     }
316     NodeInfo nodeInfo = { { 0 } };
317     if (LnnGetRemoteNodeInfoById(remoteInfo->uuid.c_str(), CATEGORY_UUID, &nodeInfo) == SOFTBUS_OK) {
318         (void)InstSetPeerDeviceIdForRemoteInfo(remoteInfo->udid, nodeInfo.deviceInfo.deviceUdid);
319     }
320 }
321 
InstSetUuidForRemoteInfoByUdid(InstantRemoteInfo * remoteInfo)322 static void InstSetUuidForRemoteInfoByUdid(InstantRemoteInfo *remoteInfo)
323 {
324     if (remoteInfo == NULL) {
325         COMM_LOGE(COMM_DFX, "invalid param");
326         return;
327     }
328     NodeInfo nodeInfo = { { 0 } };
329     if (LnnGetRemoteNodeInfoById(remoteInfo->udid.c_str(), CATEGORY_UDID, &nodeInfo) == SOFTBUS_OK) {
330         (void)InstSetPeerDeviceIdForRemoteInfo(remoteInfo->uuid, nodeInfo.uuid);
331     }
332 }
333 
UpdateRemoteInfoBySessionConn(InstantRemoteInfo * remoteInfo,const SessionConn * conn)334 static void UpdateRemoteInfoBySessionConn(InstantRemoteInfo *remoteInfo, const SessionConn *conn)
335 {
336     if (remoteInfo == NULL || conn == NULL) {
337         COMM_LOGE(COMM_DFX, "invalid param");
338         return;
339     }
340     InstSetIpForRemoteInfo(remoteInfo, &conn->appInfo);
341     if (InstSetPeerDeviceIdForRemoteInfo(remoteInfo->uuid, conn->appInfo.peerData.deviceId) == SOFTBUS_OK &&
342         remoteInfo->udid.empty()) {
343         InstSetUdidForRemoteInfoByUuid(remoteInfo);
344     }
345 }
346 
UpdateRemoteInfoByTcpChannelInfo(InstantRemoteInfo * remoteInfo,const TcpChannelInfo * conn)347 static void UpdateRemoteInfoByTcpChannelInfo(InstantRemoteInfo *remoteInfo, const TcpChannelInfo *conn)
348 {
349     if (remoteInfo == nullptr) {
350         COMM_LOGE(COMM_DFX, "param remote info is null");
351         return;
352     }
353     if (conn == nullptr) {
354         COMM_LOGE(COMM_DFX, "param conn is null");
355         return;
356     }
357     int32_t type = InstGetIpFromLinkTypeOrConnectType(remoteInfo, conn->linkType, conn->connectType);
358     switch (type) {
359         case HML_IP:
360             remoteInfo->hmlIp = std::string(conn->peerIp);
361             break;
362         case P2P_IP:
363             remoteInfo->p2pIp = std::string(conn->peerIp);
364             break;
365         case WLAN_IP:
366             remoteInfo->wlanIp = std::string(conn->peerIp);
367             break;
368         default:
369             break;
370     }
371     if (remoteInfo->udid.empty()) {
372         remoteInfo->udid = GetUdidByTcpChannelInfo(conn);
373     }
374 }
375 
InstCreateAndAddChannelInfo(InstantRemoteInfo * remoteInfo)376 static InstantChannelInfo *InstCreateAndAddChannelInfo(InstantRemoteInfo *remoteInfo)
377 {
378     if (remoteInfo == NULL) {
379         COMM_LOGE(COMM_DFX, "invalid param");
380         return NULL;
381     }
382     if (remoteInfo->channelNum >= INST_MAX_CHANNEL_NUM_EACH) {
383         return NULL;
384     }
385     InstantChannelInfo *channelInfo = static_cast<InstantChannelInfo *>(SoftBusCalloc(sizeof(InstantChannelInfo)));
386     if (channelInfo == NULL) {
387         COMM_LOGE(COMM_DFX, "channel info Calloc fail");
388         return NULL;
389     }
390     ListInit(&channelInfo->node);
391     ListAdd(&remoteInfo->channels, &channelInfo->node);
392     remoteInfo->channelNum++;
393     return channelInfo;
394 }
395 
InstAddSessionConnToRemoteInfo(InstantRemoteInfo * remoteInfo,SessionConn * conn)396 static void InstAddSessionConnToRemoteInfo(InstantRemoteInfo *remoteInfo, SessionConn *conn)
397 {
398     if (remoteInfo == NULL || conn == NULL) {
399         COMM_LOGE(COMM_DFX, "invalid param");
400         return;
401     }
402     InstantChannelInfo *channelInfo = InstCreateAndAddChannelInfo(remoteInfo);
403     if (channelInfo == NULL) {
404         return;
405     }
406     channelInfo->serverSide = conn->serverSide;
407     channelInfo->laneLinkType = conn->appInfo.linkType;
408     channelInfo->connectType = conn->appInfo.connectType;
409     channelInfo->startTime = GetSoftbusRecordTimeMillis() - conn->appInfo.timeStart;
410     channelInfo->status = static_cast<int32_t>(conn->status);
411     channelInfo->channelType = CHANNEL_TYPE_TCP_DIRECT;
412     channelInfo->socketName = conn->appInfo.myData.sessionName;
413     UpdateRemoteInfoBySessionConn(remoteInfo, conn);
414 }
415 
InstAddTcpChannelInfoToRemoteInfo(InstantRemoteInfo * remoteInfo,TcpChannelInfo * conn)416 static void InstAddTcpChannelInfoToRemoteInfo(InstantRemoteInfo *remoteInfo, TcpChannelInfo *conn)
417 {
418     if (remoteInfo == nullptr || conn == nullptr) {
419         COMM_LOGE(COMM_DFX, "invalid param");
420         return;
421     }
422     InstantChannelInfo *channelInfo = InstCreateAndAddChannelInfo(remoteInfo);
423     if (channelInfo == nullptr) {
424         return;
425     }
426     channelInfo->serverSide = conn->isServer;
427     channelInfo->laneLinkType = conn->linkType;
428     channelInfo->connectType = conn->connectType;
429     channelInfo->startTime = GetSoftbusRecordTimeMillis() - conn->timeStart;
430     channelInfo->channelType = CHANNEL_TYPE_TCP_DIRECT;
431     channelInfo->socketName = conn->peerSessionName;
432     UpdateRemoteInfoByTcpChannelInfo(remoteInfo, conn);
433 }
434 
InstUpdateRemoteInfoBySessionConn(SoftBusList * remoteChannelInfoList)435 static void InstUpdateRemoteInfoBySessionConn(SoftBusList *remoteChannelInfoList)
436 {
437     SessionConn *item = NULL;
438     SoftBusList *sessionList = GetSessionConnList();
439     if (sessionList == NULL || GetSessionConnLock() != SOFTBUS_OK) {
440         return;
441     }
442     LIST_FOR_EACH_ENTRY(item, &sessionList->list, SessionConn, node) {
443         InstantRemoteInfo *rInfo = NULL;
444         bool matched = false;
445         LIST_FOR_EACH_ENTRY(rInfo, &remoteChannelInfoList->list, InstantRemoteInfo, node) {
446             if (InstIsMatchSessionConn(rInfo, item)) {
447                 matched = true;
448                 InstAddSessionConnToRemoteInfo(rInfo, item);
449                 break;
450             }
451         }
452         rInfo = InstCreateAndAddRemoteInfo(remoteChannelInfoList, matched);
453         if (rInfo == NULL) {
454             continue;
455         }
456         InstAddSessionConnToRemoteInfo(rInfo, item);
457     }
458     ReleaseSessionConnLock();
459 }
460 
InstUpdateRemoteInfoByTcpChannel(SoftBusList * remoteChannelInfoList)461 static void InstUpdateRemoteInfoByTcpChannel(SoftBusList *remoteChannelInfoList)
462 {
463     TcpChannelInfo *item = NULL;
464     SoftBusList *tcpChannelInfoList = GetTcpChannelInfoList();
465     if (tcpChannelInfoList == NULL || GetTcpChannelInfoLock() != SOFTBUS_OK) {
466         return;
467     }
468     LIST_FOR_EACH_ENTRY(item, &tcpChannelInfoList->list, TcpChannelInfo, node) {
469         InstantRemoteInfo *rInfo = NULL;
470         bool matched = false;
471         LIST_FOR_EACH_ENTRY(rInfo, &remoteChannelInfoList->list, InstantRemoteInfo, node) {
472             if (InstIsMatchTcpChannel(rInfo, item)) {
473                 matched = true;
474                 InstAddTcpChannelInfoToRemoteInfo(rInfo, item);
475                 break;
476             }
477         }
478         rInfo = InstCreateAndAddRemoteInfo(remoteChannelInfoList, matched);
479         if (rInfo == NULL) {
480             continue;
481         }
482         InstAddTcpChannelInfoToRemoteInfo(rInfo, item);
483     }
484     ReleaseTcpChannelInfoLock();
485 }
486 
InstIsMatchUdpChannel(const InstantRemoteInfo * rInfo,const UdpChannelInfo * info)487 static bool InstIsMatchUdpChannel(const InstantRemoteInfo *rInfo, const UdpChannelInfo *info)
488 {
489     if (rInfo == NULL || info == NULL) {
490         COMM_LOGE(COMM_DFX, "invalid param");
491         return false;
492     }
493     if (InstantIsParaMatch(rInfo->uuid, info->info.peerData.deviceId)) {
494         return true;
495     }
496     int32_t type = InstGetIpFromLinkTypeOrConnectType(rInfo, info->info.linkType, info->info.connectType);
497     switch (type) {
498         case HML_IP:
499             return InstantIsParaMatch(rInfo->hmlIp, info->info.peerData.addr);
500         case P2P_IP:
501             return InstantIsParaMatch(rInfo->p2pIp, info->info.peerData.addr);
502         case WLAN_IP:
503             return InstantIsParaMatch(rInfo->wlanIp, info->info.peerData.addr);
504         default:
505             return false;
506     }
507 }
508 
UpdateRemoteInfoByUdpChannel(InstantRemoteInfo * remoteInfo,const UdpChannelInfo * info)509 static void UpdateRemoteInfoByUdpChannel(InstantRemoteInfo *remoteInfo, const UdpChannelInfo *info)
510 {
511     if (remoteInfo == NULL || info == NULL) {
512         COMM_LOGE(COMM_DFX, "invalid param");
513         return;
514     }
515     InstSetIpForRemoteInfo(remoteInfo, &info->info);
516     if (InstSetPeerDeviceIdForRemoteInfo(remoteInfo->uuid, info->info.peerData.deviceId) == SOFTBUS_OK &&
517         remoteInfo->udid.empty()) {
518         InstSetUdidForRemoteInfoByUuid(remoteInfo);
519     }
520 }
521 
InstAddUdpChannelToRemoteInfo(InstantRemoteInfo * remoteInfo,const UdpChannelInfo * info)522 static void InstAddUdpChannelToRemoteInfo(InstantRemoteInfo *remoteInfo, const UdpChannelInfo *info)
523 {
524     if (remoteInfo == NULL || info == NULL) {
525         COMM_LOGE(COMM_DFX, "invalid param");
526         return;
527     }
528     InstantChannelInfo *channelInfo = InstCreateAndAddChannelInfo(remoteInfo);
529     if (channelInfo == NULL) {
530         return;
531     }
532     channelInfo->serverSide = !info->info.isClient;
533     channelInfo->laneLinkType = info->info.linkType;
534     channelInfo->connectType = info->info.connectType;
535     channelInfo->startTime = GetSoftbusRecordTimeMillis() - info->info.timeStart;
536     channelInfo->status = info->status;
537     channelInfo->channelType = CHANNEL_TYPE_UDP;
538     channelInfo->socketName = info->info.myData.sessionName;
539     UpdateRemoteInfoByUdpChannel(remoteInfo, info);
540 }
541 
InstUpdateRemoteInfoByUdpChannel(SoftBusList * remoteChannelInfoList)542 static void InstUpdateRemoteInfoByUdpChannel(SoftBusList *remoteChannelInfoList)
543 {
544     UdpChannelInfo *item = NULL;
545     SoftBusList *sessionList = GetUdpChannelMgrHead();
546     if (sessionList == NULL || GetUdpChannelLock() != SOFTBUS_OK) {
547         return;
548     }
549     LIST_FOR_EACH_ENTRY(item, &sessionList->list, UdpChannelInfo, node) {
550         InstantRemoteInfo *rInfo = NULL;
551         bool matched = false;
552         LIST_FOR_EACH_ENTRY(rInfo, &remoteChannelInfoList->list, InstantRemoteInfo, node) {
553             if (InstIsMatchUdpChannel(rInfo, item)) {
554                 matched = true;
555                 InstAddUdpChannelToRemoteInfo(rInfo, item);
556                 break;
557             }
558         }
559         rInfo = InstCreateAndAddRemoteInfo(remoteChannelInfoList, matched);
560         if (rInfo == NULL) {
561             continue;
562         }
563         InstAddUdpChannelToRemoteInfo(rInfo, item);
564     }
565     ReleaseUdpChannelLock();
566 }
567 
InstIsMatchProxyChannel(const InstantRemoteInfo * rInfo,const ProxyChannelInfo * conn)568 static bool InstIsMatchProxyChannel(const InstantRemoteInfo *rInfo, const ProxyChannelInfo *conn)
569 {
570     if (rInfo == NULL || conn == NULL) {
571         COMM_LOGE(COMM_DFX, "invalid param");
572         return false;
573     }
574 
575     std::string deviceId = conn->appInfo.appType == APP_TYPE_AUTH ? rInfo->udid : rInfo->uuid;
576     return InstantIsParaMatch(deviceId, conn->appInfo.peerData.deviceId);
577 }
578 
UpdateRemoteInfoByProxyChannel(InstantRemoteInfo * remoteInfo,const ProxyChannelInfo * info)579 static void UpdateRemoteInfoByProxyChannel(InstantRemoteInfo *remoteInfo, const ProxyChannelInfo *info)
580 {
581     if (remoteInfo == NULL || info == NULL) {
582         COMM_LOGE(COMM_DFX, "invalid param");
583         return;
584     }
585     if (info->appInfo.appType == APP_TYPE_AUTH) {
586         if (InstSetPeerDeviceIdForRemoteInfo(remoteInfo->udid, info->appInfo.peerData.deviceId) == SOFTBUS_OK &&
587             remoteInfo->uuid.empty()) {
588             InstSetUuidForRemoteInfoByUdid(remoteInfo);
589         }
590     } else {
591         if (InstSetPeerDeviceIdForRemoteInfo(remoteInfo->uuid, info->appInfo.peerData.deviceId) == SOFTBUS_OK &&
592             remoteInfo->udid.empty()) {
593             InstSetUdidForRemoteInfoByUuid(remoteInfo);
594         }
595     }
596 }
597 
InstSetDeviceIdByConnId(InstantRemoteInfo * remoteInfo,InstantChannelInfo * channelInfo,uint32_t connId)598 static void InstSetDeviceIdByConnId(InstantRemoteInfo *remoteInfo, InstantChannelInfo *channelInfo, uint32_t connId)
599 {
600     if (remoteInfo == NULL || channelInfo == NULL) {
601         COMM_LOGE(COMM_DFX, "invalid param");
602         return;
603     }
604     ConnectionInfo info;
605     (void)memset_s(&info, sizeof(ConnectionInfo), 0, sizeof(ConnectionInfo));
606     if (ConnGetConnectionInfo(connId, &info) != SOFTBUS_OK) {
607         return;
608     }
609     if (info.type < CONNECT_TCP || info.type >= CONNECT_TYPE_MAX) {
610         return;
611     }
612     channelInfo->connectType = info.type;
613     if (info.type == CONNECT_TCP) {
614         (void)InstSetPeerDeviceIdForRemoteInfo(remoteInfo->wlanIp, std::string(info.socketInfo.addr));
615     } else if (info.type == CONNECT_BR) {
616         (void)InstSetPeerDeviceIdForRemoteInfo(remoteInfo->brMac, std::string(info.brInfo.brMac));
617     } else if (info.type == CONNECT_BLE || info.type == CONNECT_BLE_DIRECT) {
618         (void)InstSetPeerDeviceIdForRemoteInfo(remoteInfo->bleMac, std::string(info.bleInfo.bleMac));
619     }
620 }
621 
SetParamByProxyChannelInfo(InstantRemoteInfo * remoteInfo,InstantChannelInfo * channelInfo,const ProxyChannelInfo * info)622 static void SetParamByProxyChannelInfo(InstantRemoteInfo *remoteInfo, InstantChannelInfo *channelInfo,
623     const ProxyChannelInfo *info)
624 {
625     if (remoteInfo == NULL || channelInfo == NULL || info == NULL) {
626         COMM_LOGE(COMM_DFX, "invalid param");
627         return;
628     }
629     channelInfo->serverSide = info->isServer;
630     channelInfo->laneLinkType = info->appInfo.linkType;
631     channelInfo->connectType = info->appInfo.connectType;
632     channelInfo->startTime = GetSoftbusRecordTimeMillis() - info->appInfo.timeStart;
633     channelInfo->status = info->status;
634     channelInfo->appType = info->appInfo.appType;
635     channelInfo->channelType = CHANNEL_TYPE_PROXY;
636     channelInfo->socketName = info->appInfo.myData.sessionName;
637     UpdateRemoteInfoByProxyChannel(remoteInfo, info);
638     InstSetDeviceIdByConnId(remoteInfo, channelInfo, info->connId);
639 }
640 
InstAddProxyChannelToRemoteInfo(InstantRemoteInfo * remoteInfo,const ProxyChannelInfo * info)641 static void InstAddProxyChannelToRemoteInfo(InstantRemoteInfo *remoteInfo, const ProxyChannelInfo *info)
642 {
643     if (remoteInfo == NULL || info == NULL) {
644         COMM_LOGE(COMM_DFX, "invalid param");
645         return;
646     }
647     InstantChannelInfo *channelInfo = InstCreateAndAddChannelInfo(remoteInfo);
648     if (channelInfo == NULL) {
649         return;
650     }
651     SetParamByProxyChannelInfo(remoteInfo, channelInfo, info);
652 }
653 
InstUpdateRemoteInfoByProxyChannel(SoftBusList * remoteChannelInfoList)654 static void InstUpdateRemoteInfoByProxyChannel(SoftBusList *remoteChannelInfoList)
655 {
656     ProxyChannelInfo *item = NULL;
657     SoftBusList *sessionList = GetProxyChannelMgrHead();
658     if (sessionList == NULL || GetProxyChannelLock() != SOFTBUS_OK) {
659         return;
660     }
661     LIST_FOR_EACH_ENTRY(item, &sessionList->list, ProxyChannelInfo, node) {
662         InstantRemoteInfo *rInfo = NULL;
663         bool matched = false;
664         LIST_FOR_EACH_ENTRY(rInfo, &remoteChannelInfoList->list, InstantRemoteInfo, node) {
665             if (InstIsMatchProxyChannel(rInfo, item)) {
666                 matched = true;
667                 InstAddProxyChannelToRemoteInfo(rInfo, item);
668                 break;
669             }
670         }
671         rInfo = InstCreateAndAddRemoteInfo(remoteChannelInfoList, matched);
672         if (rInfo == NULL) {
673             continue;
674         }
675         InstAddProxyChannelToRemoteInfo(rInfo, item);
676     }
677     ReleaseProxyChannelLock();
678 }
679 
InstIsMatchAuthChannel(const InstantRemoteInfo * rInfo,const AuthChannelInfo * info)680 static bool InstIsMatchAuthChannel(const InstantRemoteInfo *rInfo, const AuthChannelInfo *info)
681 {
682     if (rInfo == NULL || info == NULL) {
683         COMM_LOGE(COMM_DFX, "invalid param");
684         return false;
685     }
686     return InstantIsParaMatch(rInfo->udid, info->appInfo.peerData.deviceId) ||
687         InstantIsParaMatch(rInfo->wlanIp, info->connOpt.socketOption.addr);
688 }
689 
InstUpdateRemoteInfoByAuthChannel(InstantRemoteInfo * remoteInfo,const AuthChannelInfo * info)690 static void InstUpdateRemoteInfoByAuthChannel(InstantRemoteInfo *remoteInfo, const AuthChannelInfo *info)
691 {
692     if (remoteInfo == NULL || info == NULL) {
693         COMM_LOGE(COMM_DFX, "invalid param");
694         return;
695     }
696     InstSetPeerDeviceIdForRemoteInfo(remoteInfo->udid, info->appInfo.peerData.deviceId);
697     InstSetPeerDeviceIdForRemoteInfo(remoteInfo->wlanIp, info->connOpt.socketOption.addr);
698 }
699 
InstSetParamByAuthChannelInfo(InstantRemoteInfo * remoteInfo,InstantChannelInfo * channelInfo,const AuthChannelInfo * info)700 static void InstSetParamByAuthChannelInfo(InstantRemoteInfo *remoteInfo, InstantChannelInfo *channelInfo,
701     const AuthChannelInfo *info)
702 {
703     if (remoteInfo == NULL || channelInfo == NULL || info == NULL) {
704         COMM_LOGE(COMM_DFX, "invalid param");
705         return;
706     }
707     channelInfo->serverSide = !info->isClient;
708     channelInfo->connectType = CONNECT_TCP;
709     channelInfo->startTime = GetSoftbusRecordTimeMillis() - info->appInfo.timeStart;
710     channelInfo->channelType = info->appInfo.channelType;
711     channelInfo->socketName = info->appInfo.myData.sessionName;
712     InstUpdateRemoteInfoByAuthChannel(remoteInfo, info);
713 }
714 
InstAddAuthChannelToRemoteInfo(InstantRemoteInfo * remoteInfo,const AuthChannelInfo * info)715 static void InstAddAuthChannelToRemoteInfo(InstantRemoteInfo *remoteInfo, const AuthChannelInfo *info)
716 {
717     if (remoteInfo == NULL || info == NULL) {
718         COMM_LOGE(COMM_DFX, "invalid param");
719         return;
720     }
721     InstantChannelInfo *channelInfo = InstCreateAndAddChannelInfo(remoteInfo);
722     if (channelInfo == NULL) {
723         return;
724     }
725     InstSetParamByAuthChannelInfo(remoteInfo, channelInfo, info);
726 }
727 
InstUpdateByAuthChannelList(SoftBusList * remoteChannelInfoList)728 static void InstUpdateByAuthChannelList(SoftBusList *remoteChannelInfoList)
729 {
730     AuthChannelInfo *item = NULL;
731     SoftBusList *sessionList = GetAuthChannelListHead();
732     if (sessionList == NULL || GetAuthChannelLock() != SOFTBUS_OK) {
733         return;
734     }
735     LIST_FOR_EACH_ENTRY(item, &sessionList->list, AuthChannelInfo, node) {
736         InstantRemoteInfo *rInfo = NULL;
737         bool matched = false;
738         LIST_FOR_EACH_ENTRY(rInfo, &remoteChannelInfoList->list, InstantRemoteInfo, node) {
739             if (InstIsMatchAuthChannel(rInfo, item)) {
740                 matched = true;
741                 InstAddAuthChannelToRemoteInfo(rInfo, item);
742                 break;
743             }
744         }
745         rInfo = InstCreateAndAddRemoteInfo(remoteChannelInfoList, matched);
746         if (rInfo == NULL) {
747             continue;
748         }
749         InstAddAuthChannelToRemoteInfo(rInfo, item);
750     }
751     ReleaseAuthChannelLock();
752 }
753 
InstReleaseRemoteChannelInfoList(SoftBusList * remoteChannelInfoList)754 static void InstReleaseRemoteChannelInfoList(SoftBusList *remoteChannelInfoList)
755 {
756     InstantRemoteInfo *item = NULL;
757     InstantRemoteInfo *next = NULL;
758     LIST_FOR_EACH_ENTRY_SAFE(item, next, &remoteChannelInfoList->list, InstantRemoteInfo, node) {
759         if (!IsListEmpty(&item->channels)) {
760             InstantChannelInfo *channelItem = NULL;
761             InstantChannelInfo *channelNext = NULL;
762             LIST_FOR_EACH_ENTRY_SAFE(channelItem, channelNext, &item->channels, InstantChannelInfo, node) {
763                 ListDelete(&channelItem->node);
764                 SoftBusFree(channelItem);
765             }
766         }
767         ListDelete(&item->node);
768         SoftBusFree(item);
769     }
770     remoteChannelInfoList->cnt = 0;
771     DestroySoftBusList(remoteChannelInfoList);
772 }
773 
InstPackRemoteBasicInfo(cJSON * upperJson,InstantRemoteInfo * remoteInfo)774 static void InstPackRemoteBasicInfo(cJSON *upperJson, InstantRemoteInfo *remoteInfo)
775 {
776     if (upperJson == NULL || remoteInfo == NULL) {
777         COMM_LOGE(COMM_DFX, "invalid param");
778         return;
779     }
780     cJSON *json = cJSON_CreateObject();
781     COMM_CHECK_AND_RETURN_LOGE(json != NULL, COMM_DFX, "cJSON_CreateObject fail");
782 
783     (void)AddNumber64ToJsonObject(json, "deviceType", remoteInfo->deviceType);
784     InstPackAndAnonymizeStringIfNotNull(json, remoteInfo->udid, "udid", true);
785     InstPackAndAnonymizeStringIfNotNull(json, remoteInfo->hmlMac, "hmlMac", false);
786     InstPackAndAnonymizeStringIfNotNull(json, remoteInfo->p2pMac, "p2pMac", false);
787     InstPackAndAnonymizeStringIfNotNull(json, remoteInfo->bleMac, "bleMac", false);
788     InstPackAndAnonymizeStringIfNotNull(json, remoteInfo->brMac, "brMac", false);
789     (void)AddNumberToJsonObject(json, "channelNum", remoteInfo->channelNum);
790     (void)AddNumberToJsonObject(json, "p2pRole", remoteInfo->p2pRole);
791     (void)AddNumberToJsonObject(json, "p2pFreq", remoteInfo->p2pFreq);
792     (void)AddNumberToJsonObject(json, "hmlFreq", remoteInfo->hmlFreq);
793     (void)AddNumberToJsonObject(json, "staFreq", remoteInfo->staFreq);
794     (void)AddNumberToJsonObject(json, "p2pLinkState", remoteInfo->p2pLinkState);
795     (void)AddNumberToJsonObject(json, "hmlLinkState", remoteInfo->hmlLinkState);
796     (void)AddNumber64ToJsonObject(json, "discoveryType", remoteInfo->discoveryType);
797     (void)AddNumber64ToJsonObject(json, "netCapability", remoteInfo->netCapability);
798 
799     char *str = cJSON_PrintUnformatted(json);
800     cJSON_Delete(json);
801     COMM_CHECK_AND_RETURN_LOGE(str != NULL, COMM_DFX, "cJSON_PrintUnformatted fail");
802     cJSON_AddItemToArray(upperJson, cJSON_CreateString(str));
803     cJSON_free(str);
804 }
805 
InstPackChannelInfo(cJSON * upperJson,InstantChannelInfo * channelInfo)806 static void InstPackChannelInfo(cJSON *upperJson, InstantChannelInfo *channelInfo)
807 {
808     if (upperJson == NULL || channelInfo == NULL) {
809         COMM_LOGE(COMM_DFX, "invalid param");
810         return;
811     }
812     cJSON *channelJson = cJSON_CreateObject();
813     COMM_CHECK_AND_RETURN_LOGE(channelJson != NULL, COMM_DFX, "cJSON_CreateObject fail");
814 
815     (void)AddStringToJsonObject(channelJson, "socketName", channelInfo->socketName.c_str());
816     (void)AddNumberToJsonObject(channelJson, "channelType", channelInfo->channelType);
817     (void)AddNumberToJsonObject(channelJson, "appType", channelInfo->appType);
818     (void)AddNumberToJsonObject(channelJson, "laneLinkType", channelInfo->laneLinkType);
819     (void)AddNumberToJsonObject(channelJson, "connectType", channelInfo->connectType);
820     (void)AddNumberToJsonObject(channelJson, "status", channelInfo->status);
821     (void)AddBoolToJsonObject(channelJson, "serverSide", channelInfo->serverSide);
822     (void)AddNumberToJsonObject(channelJson, "keepTime", channelInfo->startTime);
823 
824     char *str = cJSON_PrintUnformatted(channelJson);
825     cJSON_Delete(channelJson);
826     COMM_CHECK_AND_RETURN_LOGE(str != NULL, COMM_DFX, "cJSON_PrintUnformatted fail");
827     cJSON_AddItemToArray(upperJson, cJSON_CreateString(str));
828     cJSON_free(str);
829 }
830 
InstPackRemoteInfo(cJSON * json,SoftBusList * remoteChannelInfoList)831 static void InstPackRemoteInfo(cJSON *json, SoftBusList *remoteChannelInfoList)
832 {
833     if (json == NULL || remoteChannelInfoList == NULL) {
834         COMM_LOGE(COMM_DFX, "invalid param");
835         return;
836     }
837     InstantRemoteInfo *item = NULL;
838     LIST_FOR_EACH_ENTRY(item, &remoteChannelInfoList->list, InstantRemoteInfo, node) {
839         cJSON *deviceJson = cJSON_AddArrayToObject(json, "remoteInfo");
840         if (deviceJson == NULL) {
841             continue;
842         }
843         InstPackRemoteBasicInfo(deviceJson, item);
844         if (!IsListEmpty(&item->channels)) {
845             InstantChannelInfo *channelItem = NULL;
846             LIST_FOR_EACH_ENTRY(channelItem, &item->channels, InstantChannelInfo, node) {
847                 InstPackChannelInfo(deviceJson, channelItem);
848             }
849         }
850     }
851 }
852 
InstUpdateRemoteInfoByLnn(InstantRemoteInfo * remoteInfo,NodeBasicInfo * info)853 static void InstUpdateRemoteInfoByLnn(InstantRemoteInfo *remoteInfo, NodeBasicInfo *info)
854 {
855     if (remoteInfo == NULL || info == NULL) {
856         return;
857     }
858     NodeInfo nodeInfo = { { 0 } };
859     if (LnnGetRemoteNodeInfoById(info->networkId, CATEGORY_NETWORK_ID, &nodeInfo) == SOFTBUS_OK) {
860         remoteInfo->udid = std::string(nodeInfo.deviceInfo.deviceUdid);
861         remoteInfo->uuid = std::string(nodeInfo.uuid);
862         remoteInfo->wlanIp = std::string(nodeInfo.connectInfo.ifInfo[WLAN_IF].deviceIp);
863         remoteInfo->bleMac = std::string(nodeInfo.connectInfo.bleMacAddr);
864         remoteInfo->brMac = std::string(nodeInfo.connectInfo.macAddr);
865         remoteInfo->p2pRole = nodeInfo.p2pInfo.p2pRole;
866         remoteInfo->p2pIp = nodeInfo.p2pInfo.p2pIp;
867         remoteInfo->staFreq = nodeInfo.p2pInfo.staFrequency;
868         remoteInfo->discoveryType = nodeInfo.discoveryType;
869         remoteInfo->netCapability = nodeInfo.netCapacity;
870         remoteInfo->deviceType = nodeInfo.deviceInfo.deviceTypeId;
871     }
872 }
873 
InstAddRemoteInfoByLnn(SoftBusList * remoteChannelInfoList)874 static int32_t InstAddRemoteInfoByLnn(SoftBusList *remoteChannelInfoList)
875 {
876     int32_t infoNum;
877     NodeBasicInfo *info = NULL;
878     int32_t ret = LnnGetAllOnlineNodeInfo(&info, &infoNum);
879     if (ret != SOFTBUS_OK) {
880         return ret;
881     }
882     if (info == NULL || infoNum == 0) {
883         return SOFTBUS_OK;
884     }
885     for (int32_t i = 0; i < infoNum; ++i) {
886         InstantRemoteInfo *rInfo = InstCreateAndAddRemoteInfo(remoteChannelInfoList, false);
887         if (rInfo == NULL) {
888             continue;
889         }
890         InstUpdateRemoteInfoByLnn(rInfo, info + i);
891     }
892     SoftBusFree(info);
893     return SOFTBUS_OK;
894 }
895 
InstGetRemoteInfo(cJSON * json)896 static void InstGetRemoteInfo(cJSON *json)
897 {
898     if (json == NULL) {
899         return;
900     }
901     SoftBusList *remoteChannelInfoList = CreateSoftBusList();
902     if (remoteChannelInfoList == NULL) {
903         COMM_LOGE(COMM_DFX, "remoteChannelInfoList init fail");
904         return;
905     }
906     int32_t ret = InstAddRemoteInfoByLnn(remoteChannelInfoList);
907     if (ret != SOFTBUS_OK) {
908         (void)AddNumberToJsonObject(json, "Bus_center_fault", ret);
909     }
910     #ifdef DSOFTBUS_FEATURE_CONN_PV1
911     InstAddRemoteInfoByLinkManager(remoteChannelInfoList);
912     #endif
913     InstUpdateRemoteInfoBySessionConn(remoteChannelInfoList);
914     InstUpdateRemoteInfoByTcpChannel(remoteChannelInfoList);
915     InstUpdateRemoteInfoByUdpChannel(remoteChannelInfoList);
916     InstUpdateRemoteInfoByProxyChannel(remoteChannelInfoList);
917     InstUpdateByAuthChannelList(remoteChannelInfoList);
918     InstPackRemoteInfo(json, remoteChannelInfoList);
919 
920     InstReleaseRemoteChannelInfoList(remoteChannelInfoList);
921 }
922 
InstGetAllInfo(int32_t radarId,int32_t errorCode)923 static int32_t InstGetAllInfo(int32_t radarId, int32_t errorCode)
924 {
925     cJSON *json = cJSON_CreateObject();
926     COMM_CHECK_AND_RETURN_RET_LOGE(json != NULL, SOFTBUS_CREATE_JSON_ERR, COMM_DFX, "cJSON_CreateObject fail");
927     (void)AddNumberToJsonObject(json, "radarId", radarId);
928     (void)AddNumberToJsonObject(json, "errorCode", errorCode);
929     cJSON *remoteDevicesJson = cJSON_AddArrayToObject(json, "remoteDevices");
930     if (remoteDevicesJson != NULL) {
931         InstGetRemoteInfo(remoteDevicesJson);
932     }
933 
934     cJSON *wifiJson = cJSON_CreateObject();
935     Communication::Softbus::WifiStatistic::GetInstance().GetWifiStatisticInfo(wifiJson);
936     (void)cJSON_AddItemToObject(json, "WifiInfo", wifiJson);
937 
938     cJSON *btJson = cJSON_CreateObject();
939     Communication::Softbus::BtStatistic::GetInstance().GetBtStatisticInfo(btJson);
940     (void)cJSON_AddItemToObject(json, "BtInfo", btJson);
941 
942     char *info = cJSON_PrintUnformatted(json);
943     cJSON_Delete(json);
944     COMM_CHECK_AND_RETURN_RET_LOGE(info != NULL, SOFTBUS_PARSE_JSON_ERR, COMM_DFX, "cJSON_PrintUnformatted fail");
945 
946     TransEventExtra extra = {
947         .result = EVENT_STAGE_RESULT_OK,
948         .trafficStats = info
949     };
950     TRANS_EVENT(EVENT_SCENE_TRANS_CHANNEL_INSTANT, EVENT_STAGE_TRANS_COMMON_ONE, extra);
951     cJSON_free(info);
952     return SOFTBUS_OK;
953 }
954 
CreateHandler(SoftBusLooper * looper,InstAsyncCallbackFunc callback)955 static inline SoftBusHandler *CreateHandler(SoftBusLooper *looper, InstAsyncCallbackFunc callback)
956 {
957     static char handlerName[] = "Instant_statistics";
958     SoftBusHandler *handler = static_cast<SoftBusHandler *>(SoftBusMalloc(sizeof(SoftBusHandler)));
959     if (handler == NULL) {
960         COMM_LOGI(COMM_DFX, "create handler failed");
961         return NULL;
962     }
963     handler->looper = looper;
964     handler->name = handlerName;
965     handler->HandleMessage = callback;
966     return handler;
967 }
968 
FreeMessageFunc(SoftBusMessage * msg)969 static void FreeMessageFunc(SoftBusMessage *msg)
970 {
971     if (msg == NULL) {
972         return;
973     }
974     if (msg->handler != NULL) {
975         SoftBusFree(msg->handler);
976     }
977     SoftBusFree(msg);
978 }
979 
CreateMessage(SoftBusLooper * looper,InstAsyncCallbackFunc callback)980 static SoftBusMessage *CreateMessage(SoftBusLooper *looper, InstAsyncCallbackFunc callback)
981 {
982     SoftBusMessage *msg = static_cast<SoftBusMessage *>(SoftBusMalloc(sizeof(SoftBusMessage)));
983     if (msg == NULL) {
984         COMM_LOGI(COMM_DFX, "malloc softbus message failed");
985         return NULL;
986     }
987     SoftBusHandler *handler = CreateHandler(looper, callback);
988     msg->what = INST_DELAY_REGISTER;
989     msg->obj = NULL;
990     msg->handler = handler;
991     msg->FreeMessage = FreeMessageFunc;
992     return msg;
993 }
994 
InstantRegisterMsgDelay(SoftBusLooper * looper,InstAsyncCallbackFunc callback,uint64_t delayMillis)995 static int32_t InstantRegisterMsgDelay(SoftBusLooper *looper, InstAsyncCallbackFunc callback, uint64_t delayMillis)
996 {
997     if ((looper == NULL) || (callback == NULL)) {
998         return SOFTBUS_INVALID_PARAM;
999     }
1000 
1001     SoftBusMessage *message = CreateMessage(looper, callback);
1002     if (message == NULL) {
1003         return SOFTBUS_MEM_ERR;
1004     }
1005 
1006     looper->PostMessageDelay(looper, message, delayMillis);
1007     return SOFTBUS_OK;
1008 }
1009 
InstRegister(SoftBusMessage * msg)1010 void InstRegister(SoftBusMessage *msg)
1011 {
1012     if (msg == NULL) {
1013         (void)InstantRegisterMsgDelay(GetLooper(LOOP_TYPE_DEFAULT), InstRegister, INST_MINUTE_TIME);
1014     } else {
1015         struct RadarCallback callback = { 0 };
1016         callback.resourceNotificationCallback = InstGetAllInfo;
1017         (void)OHOS::CommunicationRadar::CommunicationRadar::GetInstance().RegisterRadarCallback(callback);
1018     }
1019 }
1020