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