• 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 "lnn_discovery_manager.h"
17 
18 #include <string.h>
19 
20 #include "lnn_coap_discovery_impl.h"
21 #include "lnn_log.h"
22 #include "lnn_net_builder.h"
23 #include "softbus_errcode.h"
24 #include "softbus_hisysevt_bus_center.h"
25 
26 static void DeviceFound(const ConnectionAddr *addr, const LnnDfxDeviceInfoReport *infoReport);
27 
28 typedef enum {
29     LNN_DISC_IMPL_TYPE_COAP,
30     LNN_DISC_IMPL_TYPE_MAX,
31 } LnnDiscoveryImplType;
32 
33 typedef struct {
34     int32_t (*InitDiscoveryImpl)(LnnDiscoveryImplCallback *callback);
35     int32_t (*StartPublishImpl)(void);
36     int32_t (*StopPublishImpl)(void);
37     int32_t (*StartDiscoveryImpl)(void);
38     int32_t (*StopDiscoveryImpl)(void);
39 } DiscoveryImpl;
40 
41 static DiscoveryImpl g_discoveryImpl[LNN_DISC_IMPL_TYPE_MAX] = {
42     [LNN_DISC_IMPL_TYPE_COAP] = {
43         .InitDiscoveryImpl = LnnInitCoapDiscovery,
44         .StartPublishImpl = LnnStartCoapPublish,
45         .StopPublishImpl = LnnStopCoapPublish,
46         .StartDiscoveryImpl = LnnStartCoapDiscovery,
47         .StopDiscoveryImpl = LnnStopCoapDiscovery,
48     },
49 };
50 
51 static LnnDiscoveryImplCallback g_discoveryCallback = {
52     .onDeviceFound = DeviceFound,
53 };
54 
ReportDeviceFoundResultEvt(void)55 static void ReportDeviceFoundResultEvt(void)
56 {
57     LNN_LOGD(LNN_BUILDER, "report device found result evt enter");
58     if (SoftBusRecordDiscoveryResult(DEVICE_FOUND, NULL) != SOFTBUS_OK) {
59         LNN_LOGE(LNN_BUILDER, "report device found result fail");
60     }
61 }
62 
DeviceFound(const ConnectionAddr * addr,const LnnDfxDeviceInfoReport * infoReport)63 static void DeviceFound(const ConnectionAddr *addr, const LnnDfxDeviceInfoReport *infoReport)
64 {
65     if (addr == NULL) {
66         LNN_LOGE(LNN_BUILDER, "device addr is null\n");
67         return;
68     }
69     ReportDeviceFoundResultEvt();
70     if (LnnNotifyDiscoveryDevice(addr, infoReport, true) != SOFTBUS_OK) {
71         LNN_LOGE(LNN_BUILDER, "notify device found failed\n");
72     }
73 }
74 
LnnInitDiscoveryManager(void)75 int32_t LnnInitDiscoveryManager(void)
76 {
77     uint32_t i;
78 
79     for (i = 0; i < LNN_DISC_IMPL_TYPE_MAX; ++i) {
80         if (g_discoveryImpl[i].InitDiscoveryImpl == NULL) {
81             continue;
82         }
83         if (g_discoveryImpl[i].InitDiscoveryImpl(&g_discoveryCallback) != SOFTBUS_OK) {
84             LNN_LOGE(LNN_BUILDER, "init discovery impl failed. i=%{public}d", i);
85             return SOFTBUS_ERR;
86         }
87     }
88     return SOFTBUS_OK;
89 }
90 
LnnStartPublish(void)91 int32_t LnnStartPublish(void)
92 {
93     uint32_t i;
94 
95     for (i = 0; i < LNN_DISC_IMPL_TYPE_MAX; ++i) {
96         if (g_discoveryImpl[i].StartPublishImpl == NULL) {
97             LNN_LOGE(LNN_BUILDER, "not support start publish. i=%{public}d", i);
98             continue;
99         }
100         if (g_discoveryImpl[i].StartPublishImpl() != SOFTBUS_OK) {
101             LNN_LOGE(LNN_BUILDER, "start publish impl failed. i=%{public}d", i);
102             return SOFTBUS_ERR;
103         }
104     }
105     return SOFTBUS_OK;
106 }
107 
LnnStopPublish(void)108 void LnnStopPublish(void)
109 {
110     uint32_t i;
111 
112     for (i = 0; i < LNN_DISC_IMPL_TYPE_MAX; ++i) {
113         if (g_discoveryImpl[i].StopPublishImpl == NULL) {
114             LNN_LOGE(LNN_BUILDER, "not support stop publish. i=%{public}d", i);
115             continue;
116         }
117         if (g_discoveryImpl[i].StopPublishImpl() != SOFTBUS_OK) {
118             LNN_LOGE(LNN_BUILDER, "stop publish impl failed. i=%{public}d", i);
119         }
120     }
121 }
122 
ReportStartDiscoveryResultEvt(void)123 static void ReportStartDiscoveryResultEvt(void)
124 {
125     LNN_LOGI(LNN_BUILDER, "report start discovery result evt enter");
126     if (SoftBusRecordDiscoveryResult(START_DISCOVERY, NULL) != SOFTBUS_OK) {
127         LNN_LOGE(LNN_BUILDER, "report start discovery result fail");
128     }
129 }
130 
LnnStartDiscovery(void)131 int32_t LnnStartDiscovery(void)
132 {
133     uint32_t i;
134 
135     for (i = 0; i < LNN_DISC_IMPL_TYPE_MAX; ++i) {
136         if (g_discoveryImpl[i].StartDiscoveryImpl == NULL) {
137             LNN_LOGE(LNN_BUILDER, "not support start discovery. i=%{public}d", i);
138             continue;
139         }
140         if (g_discoveryImpl[i].StartDiscoveryImpl() != SOFTBUS_OK) {
141             LNN_LOGE(LNN_BUILDER, "start discovery impl failed. i=%{public}d", i);
142             return SOFTBUS_ERR;
143         }
144     }
145     ReportStartDiscoveryResultEvt();
146     return SOFTBUS_OK;
147 }
148 
LnnStopDiscovery(void)149 void LnnStopDiscovery(void)
150 {
151     uint32_t i;
152 
153     for (i = 0; i < LNN_DISC_IMPL_TYPE_MAX; ++i) {
154         if (g_discoveryImpl[i].StopDiscoveryImpl == NULL) {
155             LNN_LOGE(LNN_BUILDER, "not support stop discovery. i=%{public}d", i);
156             continue;
157         }
158         if (g_discoveryImpl[i].StopDiscoveryImpl() != SOFTBUS_OK) {
159             LNN_LOGE(LNN_BUILDER, "stop discovery impl failed. i=%{public}d", i);
160         }
161     }
162 }
163