• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "disc_manager.h"
17 #include "common_list.h"
18 #include "disc_ble_dispatcher.h"
19 #include "disc_coap.h"
20 #include "securec.h"
21 #include "softbus_adapter_mem.h"
22 #include "softbus_adapter_thread.h"
23 #include "softbus_def.h"
24 #include "softbus_error_code.h"
25 #include "softbus_log.h"
26 #include "softbus_hisysevt_discreporter.h"
27 #include "softbus_utils.h"
28 
29 #define SEC_TIME_PARAM 1000LL
30 
31 static bool g_firstDiscFlag = false;
32 static bool g_onDeviceFound = false;
33 static int64_t g_currentTime = 0;
34 
35 static bool g_isInited = false;
36 
37 static SoftBusList *g_publishInfoList = NULL;
38 static SoftBusList *g_discoveryInfoList = NULL;
39 
40 static DiscoveryFuncInterface *g_discCoapInterface = NULL;
41 static DiscoveryFuncInterface *g_discBleInterface = NULL;
42 
43 static DiscInnerCallback g_discMgrMediumCb;
44 
45 static ListNode g_capabilityList[CAPABILITY_MAX_BITNUM];
46 
47 static const char *g_discModuleMap[] = {
48     "MODULE_LNN",
49     "MODULE_CONN",
50 };
51 
52 typedef enum {
53     MIN_SERVICE = 0,
54     PUBLISH_SERVICE = MIN_SERVICE,
55     PUBLISH_INNER_SERVICE = 1,
56     SUBSCRIBE_SERVICE = 2,
57     SUBSCRIBE_INNER_SERVICE = 3,
58     MAX_SERVICE = SUBSCRIBE_INNER_SERVICE,
59 } ServiceType;
60 
61 typedef union {
62     PublishOption publishOption;
63     SubscribeOption subscribeOption;
64 } InnerOption;
65 
66 typedef union  {
67     IServerDiscInnerCallback serverCb;
68     DiscInnerCallback innerCb;
69 } InnerCallback;
70 
71 typedef struct {
72     ListNode node;
73     char packageName[PKG_NAME_SIZE_MAX];
74     InnerCallback callback;
75     uint32_t infoNum;
76     ListNode InfoList;
77 } DiscItem;
78 
79 typedef struct {
80     ListNode node;
81     int32_t id;
82     DiscoverMode mode;
83     ExchangeMedium medium;
84     InnerOption option;
85     ListNode capNode;
86     DiscItem *item;
87 } DiscInfo;
88 
89 typedef struct {
90     ListNode node;
91     int32_t id;
92     char *pkgName;
93 } IdContainer;
94 
BitmapSet(uint32_t * bitMap,uint32_t pos)95 static void BitmapSet(uint32_t *bitMap, uint32_t pos)
96 {
97     *bitMap |= 1U << pos;
98 }
99 
IsBitmapSet(const uint32_t * bitMap,uint32_t pos)100 static bool IsBitmapSet(const uint32_t *bitMap, uint32_t pos)
101 {
102     return ((1U << pos) & (*bitMap)) ? true : false;
103 }
104 
CallSpecificInterfaceFunc(const InnerOption * option,const DiscoveryFuncInterface * interface,const DiscoverMode mode,InterfaceFuncType type)105 static int32_t CallSpecificInterfaceFunc(const InnerOption *option, const DiscoveryFuncInterface *interface,
106                                          const DiscoverMode mode, InterfaceFuncType type)
107 {
108     DISC_CHECK_AND_RETURN_RET_LOG(interface != NULL, SOFTBUS_DISCOVER_MANAGER_INNERFUNCTION_FAIL,
109                                   "interface is null");
110     switch (type) {
111         case PUBLISH_FUNC:
112             return ((mode == DISCOVER_MODE_ACTIVE) ? (interface->Publish(&(option->publishOption))) :
113                 (interface->StartScan(&(option->publishOption))));
114         case UNPUBLISH_FUNC:
115             return ((mode == DISCOVER_MODE_ACTIVE) ? (interface->Unpublish(&(option->publishOption))) :
116                 (interface->StopScan(&(option->publishOption))));
117         case STARTDISCOVERTY_FUNC:
118             return ((mode == DISCOVER_MODE_ACTIVE) ? (interface->StartAdvertise(&(option->subscribeOption))) :
119                 (interface->Subscribe(&(option->subscribeOption))));
120         case STOPDISCOVERY_FUNC:
121             return ((mode == DISCOVER_MODE_ACTIVE) ? (interface->StopAdvertise(&(option->subscribeOption))) :
122                 (interface->Unsubscribe(&(option->subscribeOption))));
123         default:
124             return SOFTBUS_DISCOVER_MANAGER_INNERFUNCTION_FAIL;
125     }
126 }
127 
GetCurrentTimeUs(void)128 static int64_t GetCurrentTimeUs(void)
129 {
130     SoftBusSysTime time;
131     SoftBusGetTime(&time);
132     return time.sec * SEC_TIME_PARAM + (time.usec / SEC_TIME_PARAM);
133 }
134 
StartFirstDiscoveryTimeRecord(void)135 static void StartFirstDiscoveryTimeRecord(void)
136 {
137     if (g_firstDiscFlag == false) {
138         g_currentTime = GetCurrentTimeUs();
139         g_firstDiscFlag = true;
140         g_onDeviceFound = false;
141         DLOGI("First discovery time record start");
142     }
143 }
144 
FinishFirstDiscoveryTimeRecord(void)145 static void FinishFirstDiscoveryTimeRecord(void)
146 {
147     if (g_onDeviceFound == false) {
148         int64_t lastTime = g_currentTime;
149         g_currentTime = GetCurrentTimeUs();
150         g_currentTime -= lastTime;
151         g_onDeviceFound = true;
152         g_firstDiscFlag = false;
153         DLOGI("First discovery time record finish");
154     }
155 }
156 
CallInterfaceByMedium(const DiscInfo * info,const InterfaceFuncType type)157 static int32_t CallInterfaceByMedium(const DiscInfo *info, const InterfaceFuncType type)
158 {
159     SoftbusRecordDiscScanTimes(info->medium);
160     StartFirstDiscoveryTimeRecord();
161 
162     switch (info->medium) {
163         case COAP:
164             return CallSpecificInterfaceFunc(&(info->option), g_discCoapInterface, info->mode, type);
165         case BLE:
166             return CallSpecificInterfaceFunc(&(info->option), g_discBleInterface, info->mode, type);
167         case AUTO: {
168             int coapRes = CallSpecificInterfaceFunc(&(info->option), g_discCoapInterface, info->mode, type);
169             int bleRes = CallSpecificInterfaceFunc(&(info->option), g_discBleInterface, info->mode, type);
170             DISC_CHECK_AND_RETURN_RET_LOG(coapRes == SOFTBUS_OK || bleRes == SOFTBUS_OK,
171                                           SOFTBUS_DISCOVER_MANAGER_INNERFUNCTION_FAIL, "all medium failed");
172             return SOFTBUS_OK;
173         }
174         default:
175             return SOFTBUS_DISCOVER_MANAGER_INNERFUNCTION_FAIL;
176     }
177 }
178 
TransferStringCapToBitmap(const char * capability)179 static int32_t TransferStringCapToBitmap(const char *capability)
180 {
181     DISC_CHECK_AND_RETURN_RET_LOG(capability != NULL, SOFTBUS_DISCOVER_MANAGER_CAPABILITY_INVALID,
182                                   "capability is null");
183 
184     for (uint32_t i = 0; i < sizeof(g_capabilityMap) / sizeof(g_capabilityMap[0]); i++) {
185         if (strcmp(capability, g_capabilityMap[i].capability) == 0) {
186             DLOGI("capability=%s", capability);
187             return g_capabilityMap[i].bitmap;
188         }
189     }
190 
191     return SOFTBUS_DISCOVER_MANAGER_CAPABILITY_INVALID;
192 }
193 
AddDiscInfoToCapabilityList(DiscInfo * info,const ServiceType type)194 static void AddDiscInfoToCapabilityList(DiscInfo *info, const ServiceType type)
195 {
196     DISC_CHECK_AND_RETURN_LOG(type == SUBSCRIBE_SERVICE || type == SUBSCRIBE_INNER_SERVICE, "no need to add");
197 
198     for (uint32_t tmp = 0; tmp < CAPABILITY_MAX_BITNUM; tmp++) {
199         if (IsBitmapSet(&(info->option.subscribeOption.capabilityBitmap[0]), tmp) == true) {
200             if (type == SUBSCRIBE_SERVICE) {
201                 ListTailInsert(&(g_capabilityList[tmp]), &(info->capNode));
202             } else {
203                 ListNodeInsert(&(g_capabilityList[tmp]), &(info->capNode));
204             }
205             break;
206         }
207     }
208 }
209 
RemoveDiscInfoFromCapabilityList(DiscInfo * info,const ServiceType type)210 static void RemoveDiscInfoFromCapabilityList(DiscInfo *info, const ServiceType type)
211 {
212     DISC_CHECK_AND_RETURN_LOG(type == SUBSCRIBE_SERVICE || type == SUBSCRIBE_INNER_SERVICE, "no need to delete");
213     ListDelete(&(info->capNode));
214 }
215 
FreeDiscInfo(DiscInfo * info,const ServiceType type)216 static void FreeDiscInfo(DiscInfo *info, const ServiceType type)
217 {
218     if ((type == PUBLISH_SERVICE) || (type == PUBLISH_INNER_SERVICE)) {
219         SoftBusFree(info->option.publishOption.capabilityData);
220     }
221 
222     if ((type == SUBSCRIBE_SERVICE) || (type == SUBSCRIBE_INNER_SERVICE)) {
223         SoftBusFree(info->option.subscribeOption.capabilityData);
224     }
225     SoftBusFree(info);
226 }
227 
IsInnerModule(const DiscInfo * infoNode)228 static bool IsInnerModule(const DiscInfo *infoNode)
229 {
230     for (uint32_t i = 0; i < MODULE_MAX; i++) {
231         DLOGI("%s", infoNode->item->packageName);
232         if (strcmp(infoNode->item->packageName, g_discModuleMap[i]) == 0) {
233             DLOGI("true");
234             return true;
235         }
236     }
237     DLOGI("false");
238     return false;
239 }
240 
InnerDeviceFound(const DiscInfo * infoNode,const DeviceInfo * device,const InnerDeviceInfoAddtions * additions)241 static void InnerDeviceFound(const DiscInfo *infoNode, const DeviceInfo *device,
242                              const InnerDeviceInfoAddtions *additions)
243 {
244     if (IsInnerModule(infoNode) == false) {
245         (void)infoNode->item->callback.serverCb.OnServerDeviceFound(infoNode->item->packageName, device, additions);
246         return;
247     }
248 
249     if (GetCallLnnStatus()) {
250         FinishFirstDiscoveryTimeRecord();
251         (void)SoftbusRecordFirstDiscTime(infoNode->medium, (uint32_t)g_currentTime);
252         if (infoNode->item->callback.innerCb.OnDeviceFound != NULL) {
253             infoNode->item->callback.innerCb.OnDeviceFound(device, additions);
254         }
255     }
256 }
257 
DiscOnDeviceFound(const DeviceInfo * device,const InnerDeviceInfoAddtions * additions)258 static void DiscOnDeviceFound(const DeviceInfo *device, const InnerDeviceInfoAddtions *additions)
259 {
260     DISC_CHECK_AND_RETURN_LOG(device != NULL, "device is null");
261     DISC_CHECK_AND_RETURN_LOG(additions != NULL, "additions is null");
262 
263     DLOGI("capabilityBitmap=%d, medium=%d", device->capabilityBitmap[0], additions->medium);
264     for (uint32_t tmp = 0; tmp < CAPABILITY_MAX_BITNUM; tmp++) {
265         if (IsBitmapSet((uint32_t *)device->capabilityBitmap, tmp) == false) {
266             continue;
267         }
268 
269         if (SoftBusMutexLock(&(g_discoveryInfoList->lock)) != 0) {
270             DLOGE("lock failed");
271             return;
272         }
273         DiscInfo *infoNode = NULL;
274         LIST_FOR_EACH_ENTRY(infoNode, &(g_capabilityList[tmp]), DiscInfo, capNode) {
275             DLOGI("find callback id=%d", infoNode->id);
276             InnerDeviceFound(infoNode, device, additions);
277         }
278         (void)SoftBusMutexUnlock(&(g_discoveryInfoList->lock));
279     }
280 }
281 
CheckPublishInfo(const PublishInfo * info)282 static int32_t CheckPublishInfo(const PublishInfo *info)
283 {
284     DISC_CHECK_AND_RETURN_RET_LOG(info->mode == DISCOVER_MODE_PASSIVE || info->mode == DISCOVER_MODE_ACTIVE,
285                                   SOFTBUS_INVALID_PARAM, "mode is invalid");
286     DISC_CHECK_AND_RETURN_RET_LOG(info->medium >= AUTO && info->medium <= COAP,
287                                   SOFTBUS_DISCOVER_MANAGER_INVALID_MEDIUM, "mode is invalid");
288     DISC_CHECK_AND_RETURN_RET_LOG(info->freq >= LOW && info->freq <= SUPER_HIGH,
289                                   SOFTBUS_INVALID_PARAM, "freq is invalid");
290     DISC_CHECK_AND_RETURN_RET_LOG((info->capabilityData == NULL && info->dataLen == 0) ||
291                                   (info->capabilityData != NULL && info->dataLen <= MAX_CAPABILITYDATA_LEN),
292                                   SOFTBUS_INVALID_PARAM, "data and length invalid");
293     return SOFTBUS_OK;
294 }
295 
CheckSubscribeInfo(const SubscribeInfo * info)296 static int32_t CheckSubscribeInfo(const SubscribeInfo *info)
297 {
298     DISC_CHECK_AND_RETURN_RET_LOG(info->mode == DISCOVER_MODE_PASSIVE || info->mode == DISCOVER_MODE_ACTIVE,
299                                   SOFTBUS_INVALID_PARAM, "mode is invalid");
300     DISC_CHECK_AND_RETURN_RET_LOG(info->medium >= AUTO && info->medium <= COAP,
301                                   SOFTBUS_DISCOVER_MANAGER_INVALID_MEDIUM, "mode is invalid");
302     DISC_CHECK_AND_RETURN_RET_LOG(info->freq >= LOW && info->freq <= SUPER_HIGH,
303                                   SOFTBUS_INVALID_PARAM, "freq is invalid");
304     DISC_CHECK_AND_RETURN_RET_LOG((info->capabilityData == NULL && info->dataLen == 0) ||
305                                   (info->capabilityData != NULL && info->dataLen <= MAX_CAPABILITYDATA_LEN),
306                                   SOFTBUS_INVALID_PARAM, "data and length invalid");
307     return SOFTBUS_OK;
308 }
309 
SetDiscItemCallback(DiscItem * itemNode,const InnerCallback * cb,const ServiceType type)310 static void SetDiscItemCallback(DiscItem *itemNode, const InnerCallback *cb, const ServiceType type)
311 {
312     if ((type != SUBSCRIBE_INNER_SERVICE) && (type != SUBSCRIBE_SERVICE)) {
313         return;
314     }
315     if (type == SUBSCRIBE_SERVICE) {
316         itemNode->callback.serverCb.OnServerDeviceFound = cb->serverCb.OnServerDeviceFound;
317         return;
318     }
319     if ((itemNode->callback.innerCb.OnDeviceFound != NULL) && (cb->innerCb.OnDeviceFound == NULL)) {
320         return;
321     }
322     itemNode->callback.innerCb.OnDeviceFound = cb->innerCb.OnDeviceFound;
323 }
324 
CreateDiscItem(SoftBusList * serviceList,const char * packageName,const InnerCallback * cb,const ServiceType type)325 static DiscItem *CreateDiscItem(SoftBusList *serviceList, const char *packageName, const InnerCallback *cb,
326                                 const ServiceType type)
327 {
328     DiscItem *itemNode = (DiscItem *)SoftBusCalloc(sizeof(DiscItem));
329     DISC_CHECK_AND_RETURN_RET_LOG(itemNode != NULL, NULL, "calloc item node failed");
330 
331     if (strcpy_s(itemNode->packageName, PKG_NAME_SIZE_MAX, packageName) != EOK) {
332         SoftBusFree(itemNode);
333         return NULL;
334     }
335 
336     if ((type == PUBLISH_INNER_SERVICE) || (type == SUBSCRIBE_INNER_SERVICE)) {
337         ListNodeInsert(&(serviceList->list), &(itemNode->node));
338     } else if ((type == PUBLISH_SERVICE) || (type == SUBSCRIBE_SERVICE)) {
339         ListTailInsert(&(serviceList->list), &(itemNode->node));
340     }
341 
342     SetDiscItemCallback(itemNode, cb, type);
343 
344     serviceList->cnt++;
345     ListInit(&(itemNode->InfoList));
346     return itemNode;
347 }
348 
CreateDiscInfoForPublish(const PublishInfo * info)349 static DiscInfo *CreateDiscInfoForPublish(const PublishInfo *info)
350 {
351     DiscInfo *infoNode = (DiscInfo *)SoftBusCalloc(sizeof(DiscInfo));
352     DISC_CHECK_AND_RETURN_RET_LOG(infoNode != NULL, NULL, "calloc info node failed");
353 
354     ListInit(&(infoNode->node));
355     ListInit(&(infoNode->capNode));
356 
357     infoNode->id = info->publishId;
358     infoNode->medium = info->medium;
359     infoNode->mode = info->mode;
360 
361     PublishOption *option = &infoNode->option.publishOption;
362     option->freq = info->freq;
363     option->ranging = info->ranging;
364     option->dataLen = info->dataLen;
365 
366     if (info->dataLen != 0) {
367         option->capabilityData = (uint8_t *)SoftBusCalloc(info->dataLen);
368         if (option->capabilityData == NULL) {
369             DLOGE("alloc capability data failed");
370             SoftBusFree(infoNode);
371             return NULL;
372         }
373         (void)memcpy_s(option->capabilityData, info->dataLen, info->capabilityData, info->dataLen);
374     }
375 
376     int32_t bitmap = TransferStringCapToBitmap(info->capability);
377     if (bitmap < 0) {
378         DLOGE("capability not found");
379         FreeDiscInfo(infoNode, PUBLISH_SERVICE);
380         return NULL;
381     }
382     BitmapSet(option->capabilityBitmap, (uint32_t)bitmap);
383 
384     return infoNode;
385 }
386 
CreateDiscInfoForSubscribe(const SubscribeInfo * info)387 static DiscInfo *CreateDiscInfoForSubscribe(const SubscribeInfo *info)
388 {
389     DiscInfo *infoNode = (DiscInfo *)SoftBusCalloc(sizeof(DiscInfo));
390     DISC_CHECK_AND_RETURN_RET_LOG(infoNode != NULL, NULL, "alloc info node failed");
391 
392     ListInit(&(infoNode->node));
393     ListInit(&(infoNode->capNode));
394 
395     infoNode->id = info->subscribeId;
396     infoNode->medium = info->medium;
397     infoNode->mode = info->mode;
398 
399     SubscribeOption *option = &infoNode->option.subscribeOption;
400     option->freq = info->freq;
401     option->dataLen = info->dataLen;
402     option->isSameAccount = info->isSameAccount;
403     option->isWakeRemote = info->isWakeRemote;
404 
405     if (info->dataLen != 0) {
406         option->capabilityData = (uint8_t *)SoftBusCalloc(info->dataLen);
407         if (option->capabilityData == NULL) {
408             DLOGE("alloc capability data failed");
409             SoftBusFree(infoNode);
410             return NULL;
411         }
412         (void)memcpy_s(option->capabilityData, info->dataLen, info->capabilityData, info->dataLen);
413     }
414 
415     int32_t bimap = TransferStringCapToBitmap(info->capability);
416     if (bimap < 0) {
417         DLOGE("capability not found");
418         FreeDiscInfo(infoNode, SUBSCRIBE_SERVICE);
419         return NULL;
420     }
421     BitmapSet(option->capabilityBitmap, (uint32_t)bimap);
422 
423     return infoNode;
424 }
425 
AddDiscInfoToList(SoftBusList * serviceList,const char * packageName,const InnerCallback * cb,DiscInfo * info,ServiceType type)426 static int32_t AddDiscInfoToList(SoftBusList *serviceList, const char *packageName, const InnerCallback *cb,
427                                  DiscInfo *info, ServiceType type)
428 {
429     if (SoftBusMutexLock(&(serviceList->lock)) != 0) {
430         DLOGE("lock failed");
431         return SOFTBUS_LOCK_ERR;
432     }
433 
434     DiscItem *itemNode = NULL;
435     bool exist = false;
436     LIST_FOR_EACH_ENTRY(itemNode, &(serviceList->list), DiscItem, node) {
437         if (strcmp(itemNode->packageName, packageName) != 0) {
438             continue;
439         }
440 
441         DiscInfo *infoNode = NULL;
442         LIST_FOR_EACH_ENTRY(infoNode, &(itemNode->InfoList), DiscInfo, node) {
443             if (infoNode->id == info->id) {
444                 DLOGI("id already existed");
445                 (void)SoftBusMutexUnlock(&(serviceList->lock));
446                 return SOFTBUS_DISCOVER_MANAGER_DUPLICATE_PARAM;
447             }
448         }
449 
450         SetDiscItemCallback(itemNode, cb, type);
451         exist = true;
452         itemNode->infoNum++;
453         info->item = itemNode;
454         ListTailInsert(&(itemNode->InfoList), &(info->node));
455         AddDiscInfoToCapabilityList(info, type);
456         break;
457     }
458 
459     if (exist == false) {
460         itemNode = CreateDiscItem(serviceList, packageName, cb, type);
461         if (itemNode == NULL) {
462             DLOGE("itemNode create failed");
463             (void)SoftBusMutexUnlock(&(serviceList->lock));
464             return SOFTBUS_DISCOVER_MANAGER_ITEM_NOT_CREATE;
465         }
466 
467         itemNode->infoNum++;
468         info->item = itemNode;
469         ListTailInsert(&(itemNode->InfoList), &(info->node));
470         AddDiscInfoToCapabilityList(info, type);
471     }
472 
473     (void)SoftBusMutexUnlock(&(serviceList->lock));
474     return SOFTBUS_OK;
475 }
476 
AddDiscInfoToPublishList(const char * packageName,const InnerCallback * cb,DiscInfo * info,ServiceType type)477 static int32_t AddDiscInfoToPublishList(const char *packageName, const InnerCallback *cb, DiscInfo *info,
478                                         ServiceType type)
479 {
480     return AddDiscInfoToList(g_publishInfoList, packageName, cb, info, type);
481 }
482 
AddDiscInfoToDiscoveryList(const char * packageName,const InnerCallback * cb,DiscInfo * info,ServiceType type)483 static int32_t AddDiscInfoToDiscoveryList(const char *packageName, const InnerCallback *cb, DiscInfo *info,
484                                           ServiceType type)
485 {
486     return AddDiscInfoToList(g_discoveryInfoList, packageName, cb, info, type);
487 }
488 
RemoveInfoFromList(SoftBusList * serviceList,const char * packageName,const int32_t id,const ServiceType type)489 static DiscInfo *RemoveInfoFromList(SoftBusList *serviceList, const char *packageName, const int32_t id,
490                                     const ServiceType type)
491 {
492     if (SoftBusMutexLock(&(serviceList->lock)) != 0) {
493         DLOGE("lock failed");
494         return NULL;
495     }
496 
497     bool isIdExist = false;
498     DiscItem *itemNode = NULL;
499     DiscInfo *infoNode = NULL;
500 
501     LIST_FOR_EACH_ENTRY(itemNode, &(serviceList->list), DiscItem, node) {
502         if (strcmp(itemNode->packageName, packageName) != 0) {
503             continue;
504         }
505 
506         if (itemNode->infoNum == 0) {
507             serviceList->cnt--;
508             ListDelete(&(itemNode->node));
509             SoftBusFree(itemNode);
510             (void)SoftBusMutexUnlock(&(serviceList->lock));
511             return NULL;
512         }
513 
514         LIST_FOR_EACH_ENTRY(infoNode, &(itemNode->InfoList), DiscInfo, node) {
515             if (infoNode->id != id) {
516                 continue;
517             }
518             isIdExist = true;
519             itemNode->infoNum--;
520             RemoveDiscInfoFromCapabilityList(infoNode, type);
521             ListDelete(&(infoNode->node));
522 
523             if (itemNode->infoNum == 0) {
524                 serviceList->cnt--;
525                 ListDelete(&(itemNode->node));
526                 SoftBusFree(itemNode);
527             }
528             break;
529         }
530         break;
531     }
532 
533     (void)SoftBusMutexUnlock(&(serviceList->lock));
534 
535     if (isIdExist == false) {
536         DLOGI("can not find publishId");
537         return NULL;
538     }
539     return infoNode;
540 }
541 
RemoveInfoFromPublishList(const char * packageName,const int32_t id,const ServiceType type)542 static DiscInfo *RemoveInfoFromPublishList(const char *packageName, const int32_t id, const ServiceType type)
543 {
544     return RemoveInfoFromList(g_publishInfoList, packageName, id, type);
545 }
546 
RemoveInfoFromDiscoveryList(const char * packageName,const int32_t id,const ServiceType type)547 static DiscInfo *RemoveInfoFromDiscoveryList(const char *packageName, const int32_t id, const ServiceType type)
548 {
549     return RemoveInfoFromList(g_discoveryInfoList, packageName, id, type);
550 }
551 
InnerPublishService(const char * packageName,DiscInfo * info,const ServiceType type)552 static int32_t InnerPublishService(const char *packageName, DiscInfo *info, const ServiceType type)
553 {
554     int32_t ret = AddDiscInfoToPublishList(packageName, NULL, info, type);
555     DISC_CHECK_AND_RETURN_RET_LOG(ret == SOFTBUS_OK, ret, "add info to list failed");
556 
557     ret = CallInterfaceByMedium(info, PUBLISH_FUNC);
558     if (ret != SOFTBUS_OK) {
559         DLOGE("DiscInterfaceByMedium failed");
560         ListDelete(&(info->node));
561         info->item->infoNum--;
562     }
563 
564     return ret;
565 }
566 
InnerUnPublishService(const char * packageName,int32_t publishId,const ServiceType type)567 static int32_t InnerUnPublishService(const char *packageName, int32_t publishId, const ServiceType type)
568 {
569     DiscInfo *infoNode = RemoveInfoFromPublishList(packageName, publishId, type);
570     DISC_CHECK_AND_RETURN_RET_LOG(infoNode != NULL, SOFTBUS_DISCOVER_MANAGER_INFO_NOT_DELETE,
571                                   "delete info from list failed");
572 
573     int32_t ret = CallInterfaceByMedium(infoNode, UNPUBLISH_FUNC);
574     DISC_CHECK_AND_RETURN_RET_LOG(ret == SOFTBUS_OK, ret, "DiscInterfaceByMedium failed");
575 
576     FreeDiscInfo(infoNode, type);
577     return SOFTBUS_OK;
578 }
579 
InnerStartDiscovery(const char * packageName,DiscInfo * info,const IServerDiscInnerCallback * cb,const ServiceType type)580 static int32_t InnerStartDiscovery(const char *packageName, DiscInfo *info, const IServerDiscInnerCallback *cb,
581                                    const ServiceType type)
582 {
583     InnerCallback callback;
584     callback.serverCb.OnServerDeviceFound = NULL;
585     if (cb != NULL) {
586         callback.serverCb.OnServerDeviceFound = cb->OnServerDeviceFound;
587     }
588 
589     int32_t ret = AddDiscInfoToDiscoveryList(packageName, &callback, info, type);
590     DISC_CHECK_AND_RETURN_RET_LOG(ret == SOFTBUS_OK, ret, "add info to list failed");
591 
592     (void)SoftBusReportDiscStartupEvt(packageName);
593 
594     ret = CallInterfaceByMedium(info, STARTDISCOVERTY_FUNC);
595     if (ret != SOFTBUS_OK) {
596         DLOGE("DiscInterfaceByMedium failed");
597         RemoveDiscInfoFromCapabilityList(info, type);
598         ListDelete(&(info->node));
599         info->item->infoNum--;
600     }
601     return ret;
602 }
603 
InnerStopDiscovery(const char * packageName,int32_t subscribeId,const ServiceType type)604 static int32_t InnerStopDiscovery(const char *packageName, int32_t subscribeId, const ServiceType type)
605 {
606     DiscInfo *infoNode = RemoveInfoFromDiscoveryList(packageName, subscribeId, type);
607     DISC_CHECK_AND_RETURN_RET_LOG(infoNode != NULL, SOFTBUS_DISCOVER_MANAGER_INFO_NOT_DELETE,
608                                   "delete info from list failed");
609 
610     int32_t ret = CallInterfaceByMedium(infoNode, STOPDISCOVERY_FUNC);
611     DISC_CHECK_AND_RETURN_RET_LOG(ret == SOFTBUS_OK, ret, "DiscInterfaceByMedium failed");
612 
613     FreeDiscInfo(infoNode, type);
614     return SOFTBUS_OK;
615 }
616 
TransferModuleIdToPackageName(DiscModule moduleId)617 static const char* TransferModuleIdToPackageName(DiscModule moduleId)
618 {
619     return g_discModuleMap[moduleId - 1];
620 }
621 
InnerSetDiscoveryCallback(const char * packageName,const DiscInnerCallback * cb)622 static int32_t InnerSetDiscoveryCallback(const char *packageName, const DiscInnerCallback *cb)
623 {
624     if (SoftBusMutexLock(&(g_discoveryInfoList->lock)) != 0) {
625         DLOGE("lock failed");
626         return SOFTBUS_LOCK_ERR;
627     }
628 
629     bool isIdExist = false;
630     DiscItem *itemNode = NULL;
631     InnerCallback callback;
632     LIST_FOR_EACH_ENTRY(itemNode, &(g_discoveryInfoList->list), DiscItem, node) {
633         if (strcmp(itemNode->packageName, packageName) != 0) {
634             continue;
635         }
636         itemNode->callback.innerCb.OnDeviceFound = cb->OnDeviceFound;
637         isIdExist = true;
638         break;
639     }
640     if (isIdExist == false) {
641         callback.innerCb.OnDeviceFound = cb->OnDeviceFound;
642         itemNode = CreateDiscItem(g_discoveryInfoList, packageName, &callback, SUBSCRIBE_INNER_SERVICE);
643         if (itemNode == NULL) {
644             DLOGE("itemNode create failed");
645             (void)SoftBusMutexUnlock(&(g_discoveryInfoList->lock));
646             return SOFTBUS_DISCOVER_MANAGER_ITEM_NOT_CREATE;
647         }
648     }
649     (void)SoftBusMutexUnlock(&(g_discoveryInfoList->lock));
650     return SOFTBUS_OK;
651 }
652 
DiscSetDiscoverCallback(DiscModule moduleId,const DiscInnerCallback * callback)653 int32_t DiscSetDiscoverCallback(DiscModule moduleId, const DiscInnerCallback *callback)
654 {
655     DISC_CHECK_AND_RETURN_RET_LOG(moduleId >= MODULE_MIN && moduleId <= MODULE_MAX && callback != NULL,
656                                   SOFTBUS_INVALID_PARAM, "invalid parameters");
657     DISC_CHECK_AND_RETURN_RET_LOG(g_isInited == true, SOFTBUS_DISCOVER_MANAGER_NOT_INIT, "manager is not inited");
658     return InnerSetDiscoveryCallback(TransferModuleIdToPackageName(moduleId), callback);
659 }
660 
DiscPublish(DiscModule moduleId,const PublishInfo * info)661 int32_t DiscPublish(DiscModule moduleId, const PublishInfo *info)
662 {
663     DISC_CHECK_AND_RETURN_RET_LOG(moduleId >= MODULE_MIN && moduleId <= MODULE_MAX && info != NULL,
664                                   SOFTBUS_INVALID_PARAM, "invalid parameters");
665     DISC_CHECK_AND_RETURN_RET_LOG(info->mode == DISCOVER_MODE_ACTIVE, SOFTBUS_INVALID_PARAM, "mode is not active");
666     DISC_CHECK_AND_RETURN_RET_LOG(CheckPublishInfo(info) == SOFTBUS_OK, SOFTBUS_INVALID_PARAM, "invalid info");
667     DISC_CHECK_AND_RETURN_RET_LOG(g_isInited == true, SOFTBUS_DISCOVER_MANAGER_NOT_INIT, "manager is not inited");
668 
669     DiscInfo *infoNode = CreateDiscInfoForPublish(info);
670     DISC_CHECK_AND_RETURN_RET_LOG(infoNode != NULL, SOFTBUS_DISCOVER_MANAGER_INFO_NOT_CREATE, "create info failed");
671 
672     int32_t ret = InnerPublishService(TransferModuleIdToPackageName(moduleId), infoNode, PUBLISH_INNER_SERVICE);
673     if (ret != SOFTBUS_OK) {
674         FreeDiscInfo(infoNode, PUBLISH_INNER_SERVICE);
675     }
676     return ret;
677 }
678 
DiscStartScan(DiscModule moduleId,const PublishInfo * info)679 int32_t DiscStartScan(DiscModule moduleId, const PublishInfo *info)
680 {
681     DISC_CHECK_AND_RETURN_RET_LOG(moduleId >= MODULE_MIN && moduleId <= MODULE_MAX && info != NULL,
682                                   SOFTBUS_INVALID_PARAM, "invalid parameters");
683     DISC_CHECK_AND_RETURN_RET_LOG(info->mode == DISCOVER_MODE_PASSIVE, SOFTBUS_INVALID_PARAM, "mode is not passive");
684     DISC_CHECK_AND_RETURN_RET_LOG(CheckPublishInfo(info) == SOFTBUS_OK, SOFTBUS_INVALID_PARAM, "invalid info");
685     DISC_CHECK_AND_RETURN_RET_LOG(g_isInited == true, SOFTBUS_DISCOVER_MANAGER_NOT_INIT, "manager is not inited");
686 
687     DiscInfo *infoNode = CreateDiscInfoForPublish(info);
688     DISC_CHECK_AND_RETURN_RET_LOG(infoNode != NULL, SOFTBUS_DISCOVER_MANAGER_INFO_NOT_CREATE, "create info failed");
689 
690     int32_t ret = InnerPublishService(TransferModuleIdToPackageName(moduleId), infoNode, PUBLISH_INNER_SERVICE);
691     if (ret != SOFTBUS_OK) {
692         FreeDiscInfo(infoNode, PUBLISH_INNER_SERVICE);
693     }
694     return ret;
695 }
696 
DiscUnpublish(DiscModule moduleId,int32_t publishId)697 int32_t DiscUnpublish(DiscModule moduleId, int32_t publishId)
698 {
699     DISC_CHECK_AND_RETURN_RET_LOG(moduleId >= MODULE_MIN && moduleId <= MODULE_MAX,
700                                   SOFTBUS_INVALID_PARAM, "invalid moduleId");
701     DISC_CHECK_AND_RETURN_RET_LOG(g_isInited == true, SOFTBUS_DISCOVER_MANAGER_NOT_INIT, "manager is not inited");
702 
703     return InnerUnPublishService(TransferModuleIdToPackageName(moduleId), publishId, PUBLISH_INNER_SERVICE);
704 }
705 
DiscStartAdvertise(DiscModule moduleId,const SubscribeInfo * info)706 int32_t DiscStartAdvertise(DiscModule moduleId, const SubscribeInfo *info)
707 {
708     DISC_CHECK_AND_RETURN_RET_LOG(moduleId >= MODULE_MIN && moduleId <= MODULE_MAX && info != NULL,
709                                   SOFTBUS_INVALID_PARAM, "invalid parameters");
710     DISC_CHECK_AND_RETURN_RET_LOG(info->mode == DISCOVER_MODE_ACTIVE, SOFTBUS_INVALID_PARAM, "mode is not active");
711     DISC_CHECK_AND_RETURN_RET_LOG(CheckSubscribeInfo(info) == SOFTBUS_OK, SOFTBUS_INVALID_PARAM, "invalid info");
712     DISC_CHECK_AND_RETURN_RET_LOG(g_isInited == true, SOFTBUS_DISCOVER_MANAGER_NOT_INIT, "manager is not inited");
713 
714     DiscInfo *infoNode = CreateDiscInfoForSubscribe(info);
715     DISC_CHECK_AND_RETURN_RET_LOG(infoNode != NULL, SOFTBUS_DISCOVER_MANAGER_INFO_NOT_CREATE, "create info failed");
716 
717     int32_t ret = InnerStartDiscovery(TransferModuleIdToPackageName(moduleId), infoNode, NULL, SUBSCRIBE_INNER_SERVICE);
718     if (ret != SOFTBUS_OK) {
719         FreeDiscInfo(infoNode, SUBSCRIBE_INNER_SERVICE);
720     }
721     return ret;
722 }
723 
DiscSubscribe(DiscModule moduleId,const SubscribeInfo * info)724 int32_t DiscSubscribe(DiscModule moduleId, const SubscribeInfo *info)
725 {
726     DISC_CHECK_AND_RETURN_RET_LOG(moduleId >= MODULE_MIN && moduleId <= MODULE_MAX && info != NULL,
727                                   SOFTBUS_INVALID_PARAM, "invalid parameters");
728     DISC_CHECK_AND_RETURN_RET_LOG(info->mode == DISCOVER_MODE_PASSIVE, SOFTBUS_INVALID_PARAM, "mode is not passive");
729     DISC_CHECK_AND_RETURN_RET_LOG(CheckSubscribeInfo(info) == SOFTBUS_OK, SOFTBUS_INVALID_PARAM, "invalid info");
730     DISC_CHECK_AND_RETURN_RET_LOG(g_isInited == true, SOFTBUS_DISCOVER_MANAGER_NOT_INIT, "manager is not inited");
731 
732     DiscInfo *infoNode = CreateDiscInfoForSubscribe(info);
733     DISC_CHECK_AND_RETURN_RET_LOG(infoNode != NULL, SOFTBUS_DISCOVER_MANAGER_INFO_NOT_CREATE, "create info failed");
734 
735     int32_t ret = InnerStartDiscovery(TransferModuleIdToPackageName(moduleId), infoNode, NULL, SUBSCRIBE_INNER_SERVICE);
736     if (ret != SOFTBUS_OK) {
737         FreeDiscInfo(infoNode, SUBSCRIBE_INNER_SERVICE);
738     }
739     return ret;
740 }
741 
DiscStopAdvertise(DiscModule moduleId,int32_t subscribeId)742 int32_t DiscStopAdvertise(DiscModule moduleId, int32_t subscribeId)
743 {
744     DISC_CHECK_AND_RETURN_RET_LOG(moduleId >= MODULE_MIN && moduleId <= MODULE_MAX,
745                                   SOFTBUS_INVALID_PARAM, "invalid moduleId");
746     DISC_CHECK_AND_RETURN_RET_LOG(g_isInited == true, SOFTBUS_DISCOVER_MANAGER_NOT_INIT, "manager is not inited");
747 
748     return InnerStopDiscovery(TransferModuleIdToPackageName(moduleId), subscribeId, SUBSCRIBE_INNER_SERVICE);
749 }
750 
DiscPublishService(const char * packageName,const PublishInfo * info)751 int32_t DiscPublishService(const char *packageName, const PublishInfo *info)
752 {
753     DISC_CHECK_AND_RETURN_RET_LOG(packageName != NULL && info != NULL, SOFTBUS_INVALID_PARAM, "invalid parameters");
754     DISC_CHECK_AND_RETURN_RET_LOG(strlen(packageName) < PKG_NAME_SIZE_MAX,
755                                   SOFTBUS_INVALID_PARAM, "package name too long");
756     DISC_CHECK_AND_RETURN_RET_LOG(CheckPublishInfo(info) == SOFTBUS_OK, SOFTBUS_INVALID_PARAM, "invalid info");
757     DISC_CHECK_AND_RETURN_RET_LOG(g_isInited == true, SOFTBUS_DISCOVER_MANAGER_NOT_INIT, "manager is not inited");
758 
759     DiscInfo *infoNode = CreateDiscInfoForPublish(info);
760     DISC_CHECK_AND_RETURN_RET_LOG(infoNode != NULL, SOFTBUS_DISCOVER_MANAGER_INFO_NOT_CREATE, "create info failed");
761 
762     int32_t ret = InnerPublishService(packageName, infoNode, PUBLISH_SERVICE);
763     if (ret != SOFTBUS_OK) {
764         FreeDiscInfo(infoNode, PUBLISH_SERVICE);
765     }
766     return ret;
767 }
768 
DiscUnPublishService(const char * packageName,int32_t publishId)769 int32_t DiscUnPublishService(const char *packageName, int32_t publishId)
770 {
771     DISC_CHECK_AND_RETURN_RET_LOG(packageName != NULL && strlen(packageName) < PKG_NAME_SIZE_MAX,
772                                   SOFTBUS_INVALID_PARAM, "invalid parameters");
773     DISC_CHECK_AND_RETURN_RET_LOG(g_isInited == true, SOFTBUS_DISCOVER_MANAGER_NOT_INIT, "manager is not inited");
774 
775     return InnerUnPublishService(packageName, publishId, PUBLISH_SERVICE);
776 }
777 
DiscStartDiscovery(const char * packageName,const SubscribeInfo * info,const IServerDiscInnerCallback * cb)778 int32_t DiscStartDiscovery(const char *packageName, const SubscribeInfo *info, const IServerDiscInnerCallback *cb)
779 {
780     DISC_CHECK_AND_RETURN_RET_LOG(packageName != NULL && strlen(packageName) < PKG_NAME_SIZE_MAX,
781                                   SOFTBUS_INVALID_PARAM, "invalid package name");
782     DISC_CHECK_AND_RETURN_RET_LOG(info != NULL && cb != NULL, SOFTBUS_INVALID_PARAM, "invalid parameters");
783     DISC_CHECK_AND_RETURN_RET_LOG(g_isInited == true, SOFTBUS_DISCOVER_MANAGER_NOT_INIT, "manager is not inited");
784     DISC_CHECK_AND_RETURN_RET_LOG(CheckSubscribeInfo(info) == SOFTBUS_OK, SOFTBUS_INVALID_PARAM, "invalid info");
785 
786     DiscInfo *infoNode = CreateDiscInfoForSubscribe(info);
787     DISC_CHECK_AND_RETURN_RET_LOG(infoNode != NULL, SOFTBUS_DISCOVER_MANAGER_INFO_NOT_CREATE, "create info failed");
788 
789     int32_t ret = InnerStartDiscovery(packageName, infoNode, cb, SUBSCRIBE_SERVICE);
790     if (ret != SOFTBUS_OK) {
791         FreeDiscInfo(infoNode, SUBSCRIBE_SERVICE);
792     }
793     return ret;
794 }
795 
DiscStopDiscovery(const char * packageName,int32_t subscribeId)796 int32_t DiscStopDiscovery(const char *packageName, int32_t subscribeId)
797 {
798     DISC_CHECK_AND_RETURN_RET_LOG(packageName != NULL && strlen(packageName) < PKG_NAME_SIZE_MAX,
799                                   SOFTBUS_INVALID_PARAM, "invalid parameters");
800     DISC_CHECK_AND_RETURN_RET_LOG(g_isInited == true, SOFTBUS_DISCOVER_MANAGER_NOT_INIT, "manager is not inited");
801 
802     return InnerStopDiscovery(packageName, subscribeId, SUBSCRIBE_SERVICE);
803 }
804 
DiscLinkStatusChanged(LinkStatus status,ExchangeMedium medium)805 void DiscLinkStatusChanged(LinkStatus status, ExchangeMedium medium)
806 {
807     if (medium == COAP) {
808         if (g_discCoapInterface != NULL) {
809             g_discCoapInterface->LinkStatusChanged(status);
810         }
811     } else {
812         DLOGE("not support medium=%d", medium);
813     }
814 }
815 
DiscDeviceInfoChanged(InfoTypeChanged type)816 void DiscDeviceInfoChanged(InfoTypeChanged type)
817 {
818     DLOGI("type=%d", type);
819     if (g_discBleInterface != NULL && g_discBleInterface->UpdateLocalDeviceInfo != NULL) {
820         g_discBleInterface->UpdateLocalDeviceInfo(type);
821     }
822     if (g_discCoapInterface != NULL && g_discCoapInterface->UpdateLocalDeviceInfo != NULL) {
823         g_discCoapInterface->UpdateLocalDeviceInfo(type);
824     }
825 }
826 
CreateIdContainer(int32_t id,const char * pkgName)827 static IdContainer* CreateIdContainer(int32_t id, const char *pkgName)
828 {
829     IdContainer *container = SoftBusCalloc(sizeof(IdContainer));
830     if (container == NULL) {
831         return NULL;
832     }
833 
834     ListInit(&container->node);
835     container->id = id;
836 
837     uint32_t nameLen = strlen(pkgName) + 1;
838     container->pkgName = SoftBusCalloc(nameLen);
839     if (container->pkgName == NULL) {
840         SoftBusFree(container);
841         return NULL;
842     }
843 
844     if (strcpy_s(container->pkgName, nameLen, pkgName) != EOK) {
845         SoftBusFree(container);
846         return NULL;
847     }
848 
849     return container;
850 }
851 
DestroyIdContainer(IdContainer * container)852 static void DestroyIdContainer(IdContainer* container)
853 {
854     SoftBusFree(container->pkgName);
855     SoftBusFree(container);
856 }
857 
CleanupPublishDiscovery(ListNode * ids,ServiceType type)858 static void CleanupPublishDiscovery(ListNode *ids, ServiceType type)
859 {
860     IdContainer *it = NULL;
861     int32_t ret = SOFTBUS_ERR;
862 
863     LIST_FOR_EACH_ENTRY(it, ids, IdContainer, node) {
864         if (type == PUBLISH_SERVICE) {
865             ret = DiscUnPublishService(it->pkgName, it->id);
866             DLOGE("clean publish pkgName=%s id=%d ret=%d", it->pkgName, it->id, ret);
867             return;
868         } else if (type == SUBSCRIBE_SERVICE) {
869             ret = DiscStopDiscovery(it->pkgName, it->id);
870             DLOGE("clean subscribe pkgName=%s id=%d ret=%d", it->pkgName, it->id, ret);
871         }
872     }
873 }
874 
RemoveDiscInfoByPackageName(SoftBusList * itemList,const ServiceType type,const char * pkgName)875 static void RemoveDiscInfoByPackageName(SoftBusList *itemList, const ServiceType type, const char *pkgName)
876 {
877     ListNode ids;
878     ListInit(&ids);
879 
880     if (SoftBusMutexLock(&itemList->lock) != 0) {
881         DLOGE("lock failed");
882         return;
883     }
884 
885     DiscItem *itemNode = NULL;
886     IdContainer *container = NULL;
887     LIST_FOR_EACH_ENTRY(itemNode, &itemList->list, DiscItem, node) {
888         if (pkgName != NULL) {
889             if (strcmp(itemNode->packageName, pkgName) != 0) {
890                 continue;
891             }
892         }
893 
894         DiscInfo *infoNode = NULL;
895         LIST_FOR_EACH_ENTRY(infoNode, &itemNode->InfoList, DiscInfo, node) {
896             container = CreateIdContainer(infoNode->id, itemNode->packageName);
897             if (container == NULL) {
898                 DLOGE("CreateIdContainer failed");
899                 (void)SoftBusMutexUnlock(&itemList->lock);
900                 goto CLEANUP;
901             }
902             ListTailInsert(&ids, &container->node);
903         }
904     }
905 
906     (void)SoftBusMutexUnlock(&itemList->lock);
907     CleanupPublishDiscovery(&ids, type);
908 
909 CLEANUP:
910     while (!IsListEmpty(&ids)) {
911         container = LIST_ENTRY(ids.next, IdContainer, node);
912         ListDelete(&container->node);
913         DestroyIdContainer(container);
914     }
915 }
916 
RemoveAllDiscInfoForPublish(void)917 static void RemoveAllDiscInfoForPublish(void)
918 {
919     RemoveDiscInfoByPackageName(g_publishInfoList, PUBLISH_SERVICE, NULL);
920     DestroySoftBusList(g_publishInfoList);
921     g_publishInfoList = NULL;
922 }
923 
RemoveAllDiscInfoForDiscovery(void)924 static void RemoveAllDiscInfoForDiscovery(void)
925 {
926     RemoveDiscInfoByPackageName(g_discoveryInfoList, SUBSCRIBE_SERVICE, NULL);
927     DestroySoftBusList(g_discoveryInfoList);
928     g_discoveryInfoList = NULL;
929 }
930 
RemoveDiscInfoForPublish(const char * pkgName)931 static void RemoveDiscInfoForPublish(const char *pkgName)
932 {
933     RemoveDiscInfoByPackageName(g_publishInfoList, PUBLISH_SERVICE, pkgName);
934 }
935 
RemoveDiscInfoForDiscovery(const char * pkgName)936 static void RemoveDiscInfoForDiscovery(const char *pkgName)
937 {
938     RemoveDiscInfoByPackageName(g_discoveryInfoList, SUBSCRIBE_SERVICE, pkgName);
939 }
940 
DiscMgrDeathCallback(const char * pkgName)941 void DiscMgrDeathCallback(const char *pkgName)
942 {
943     DISC_CHECK_AND_RETURN_LOG(pkgName != NULL, "pkgName is null");
944     DISC_CHECK_AND_RETURN_LOG(g_isInited == true, "disc manager is not inited");
945 
946     DLOGI("%s is dead", pkgName);
947     RemoveDiscInfoForPublish(pkgName);
948     RemoveDiscInfoForDiscovery(pkgName);
949 }
950 
DiscMgrInit(void)951 int32_t DiscMgrInit(void)
952 {
953     DISC_CHECK_AND_RETURN_RET_LOG(g_isInited == false, SOFTBUS_OK, "already inited");
954 
955     g_discMgrMediumCb.OnDeviceFound = DiscOnDeviceFound;
956 
957     g_discCoapInterface = DiscCoapInit(&g_discMgrMediumCb);
958     g_discBleInterface = DiscBleInit(&g_discMgrMediumCb);
959     DISC_CHECK_AND_RETURN_RET_LOG(g_discBleInterface != NULL || g_discCoapInterface != NULL,
960                                   SOFTBUS_ERR, "ble and coap both init failed");
961 
962     g_publishInfoList = CreateSoftBusList();
963     DISC_CHECK_AND_RETURN_RET_LOG(g_publishInfoList != NULL, SOFTBUS_ERR, "init publish info list failed");
964     g_discoveryInfoList = CreateSoftBusList();
965     DISC_CHECK_AND_RETURN_RET_LOG(g_discoveryInfoList != NULL, SOFTBUS_ERR, "init discovery info list failed");
966 
967     for (int32_t i = 0; i < CAPABILITY_MAX_BITNUM; i++) {
968         ListInit(&g_capabilityList[i]);
969     }
970 
971     g_isInited = true;
972     return SOFTBUS_OK;
973 }
974 
DiscMgrDeinit(void)975 void DiscMgrDeinit(void)
976 {
977     DISC_CHECK_AND_RETURN_LOG(g_isInited == true, "disc manager is not inited");
978 
979     RemoveAllDiscInfoForPublish();
980     RemoveAllDiscInfoForDiscovery();
981 
982     g_discCoapInterface = NULL;
983     g_discBleInterface = NULL;
984 
985     DiscCoapDeinit();
986     DiscBleDeinit();
987 
988     g_isInited = false;
989     DLOGI("disc manager deinit success");
990 }