1 /*
2 * Copyright (c) 2021-2023 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 "bus_center_manager.h"
17
18 #include <stdint.h>
19 #include <stdlib.h>
20
21 #include "bus_center_decision_center.h"
22 #include "bus_center_event.h"
23 #include "lnn_async_callback_utils.h"
24 #include "lnn_coap_discovery_impl.h"
25 #include "lnn_discovery_manager.h"
26 #include "lnn_event_monitor.h"
27 #include "lnn_lane_hub.h"
28 #include "lnn_log.h"
29 #include "lnn_meta_node_interface.h"
30 #include "lnn_net_builder.h"
31 #include "lnn_net_ledger.h"
32 #include "lnn_network_manager.h"
33 #include "lnn_ohos_account_adapter.h"
34 #include "softbus_adapter_xcollie.h"
35 #include "softbus_def.h"
36 #include "softbus_errcode.h"
37 #include "softbus_feature_config.h"
38 #include "softbus_utils.h"
39
40 #define WATCHDOG_TASK_NAME "LNN_WATCHDOG_TASK"
41 #define WATCHDOG_INTERVAL_TIME 10000
42 #define WATCHDOG_DELAY_TIME 5000
43 #define DEFAULT_DELAY_LEN 1500
44 #define RETRY_MAX 10
45
InitNodeAddrAllocator(void)46 int32_t __attribute__((weak)) InitNodeAddrAllocator(void)
47 {
48 return SOFTBUS_OK;
49 }
DeinitNodeAddrAllocator(void)50 void __attribute__((weak)) DeinitNodeAddrAllocator(void) {}
51
RouteLSInit(void)52 int32_t __attribute__((weak)) RouteLSInit(void)
53 {
54 return SOFTBUS_OK;
55 }
RouteLSDeinit(void)56 void __attribute__((weak)) RouteLSDeinit(void) {}
57
58 typedef int32_t (*LnnInitDelayImpl)(void);
59
60 typedef enum {
61 INIT_LOCAL_LEDGER_DELAY_TYPE = 0,
62 INIT_EVENT_MONITER_DELAY_TYPE,
63 INIT_NETWORK_MANAGER_DELAY_TYPE,
64 INIT_NETBUILDER_DELAY_TYPE,
65 INIT_LANEHUB_DELAY_TYPE,
66 INIT_DELAY_MAX_TYPE,
67 } InitDelayType;
68
69 typedef struct {
70 LnnInitDelayImpl implInit;
71 bool isInit;
72 } InitDelayImpl;
73
74 typedef struct {
75 InitDelayImpl initDelayImpl[INIT_DELAY_MAX_TYPE];
76 int32_t delayLen;
77 } LnnLocalConfigInit;
78
WatchdogProcess(void)79 static void WatchdogProcess(void)
80 {
81 if (GetWatchdogFlag()) {
82 LNN_LOGI(LNN_STATE, "softbus net_builder thread running normally");
83 return;
84 }
85 LNN_LOGW(LNN_STATE, "softbus net_builder thread exception");
86 }
87
88 static LnnLocalConfigInit g_lnnLocalConfigInit = {
89 .initDelayImpl = {
90 [INIT_LOCAL_LEDGER_DELAY_TYPE] = {
91 .implInit = LnnInitNetLedgerDelay,
92 .isInit = false,
93 },
94 [INIT_EVENT_MONITER_DELAY_TYPE] = {
95 .implInit = LnnInitEventMoniterDelay,
96 .isInit = false,
97 },
98 [INIT_NETWORK_MANAGER_DELAY_TYPE] = {
99 .implInit = LnnInitNetworkManagerDelay,
100 .isInit = false,
101 },
102 [INIT_NETBUILDER_DELAY_TYPE] = {
103 .implInit = LnnInitNetBuilderDelay,
104 .isInit = false,
105 },
106 [INIT_LANEHUB_DELAY_TYPE] = {
107 .implInit = LnnInitLaneHubDelay,
108 .isInit = false,
109 },
110 },
111 };
112
ReadDelayConfig(void)113 static void ReadDelayConfig(void)
114 {
115 if (SoftbusGetConfig(SOFTBUS_INT_LNN_UDID_INIT_DELAY_LEN,
116 (unsigned char *)&g_lnnLocalConfigInit.delayLen, sizeof(g_lnnLocalConfigInit.delayLen)) != SOFTBUS_OK) {
117 LNN_LOGE(LNN_STATE, "get lnn delay init len fail, use default value");
118 g_lnnLocalConfigInit.delayLen = DEFAULT_DELAY_LEN;
119 }
120 LNN_LOGI(LNN_STATE, "lnn delay init len=%{public}u", g_lnnLocalConfigInit.delayLen);
121 }
122
BusCenterServerDelayInit(void * para)123 static void BusCenterServerDelayInit(void *para)
124 {
125 (void)para;
126 static int32_t retry = 0;
127 if (retry > RETRY_MAX) {
128 LNN_LOGE(LNN_STATE, "try exceeds max times");
129 return;
130 }
131 int32_t ret = SOFTBUS_OK;
132 uint32_t i;
133 for (i = 0; i < INIT_DELAY_MAX_TYPE; ++i) {
134 if (g_lnnLocalConfigInit.initDelayImpl[i].implInit == NULL) {
135 continue;
136 }
137 /* initialize the lane hub module after successfully initializing the local ledger. */
138 if (i == INIT_LANEHUB_DELAY_TYPE &&
139 !g_lnnLocalConfigInit.initDelayImpl[INIT_LOCAL_LEDGER_DELAY_TYPE].isInit) {
140 continue;
141 }
142 if (!g_lnnLocalConfigInit.initDelayImpl[i].isInit &&
143 g_lnnLocalConfigInit.initDelayImpl[i].implInit() != SOFTBUS_OK) {
144 LNN_LOGE(LNN_STATE, "init delay impl failed. i=%{public}u", i);
145 ret = SOFTBUS_ERR;
146 } else {
147 g_lnnLocalConfigInit.initDelayImpl[i].isInit = true;
148 }
149 }
150 LnnCoapConnectInit();
151 if (ret != SOFTBUS_OK) {
152 retry++;
153 SoftBusLooper *looper = GetLooper(LOOP_TYPE_DEFAULT);
154 ret = LnnAsyncCallbackDelayHelper(looper, BusCenterServerDelayInit, NULL, g_lnnLocalConfigInit.delayLen);
155 if (ret != SOFTBUS_OK) {
156 LNN_LOGE(LNN_STATE, "LnnAsyncCallbackDelayHelper fail");
157 }
158 }
159 }
160
StartDelayInit(void)161 static int32_t StartDelayInit(void)
162 {
163 ReadDelayConfig();
164 int32_t ret = LnnAsyncCallbackDelayHelper(GetLooper(LOOP_TYPE_DEFAULT), BusCenterServerDelayInit,
165 NULL, g_lnnLocalConfigInit.delayLen);
166 if (ret != SOFTBUS_OK) {
167 LNN_LOGE(LNN_INIT, "call LnnAsyncCallbackDelayHelper fail");
168 }
169 return ret;
170 }
171
BusCenterServerInitFirstStep(void)172 static int32_t BusCenterServerInitFirstStep(void)
173 {
174 if (LnnInitLnnLooper() != SOFTBUS_OK) {
175 LNN_LOGE(LNN_INIT, "init lnn looper fail");
176 return SOFTBUS_ERR;
177 }
178 if (LnnInitNetLedger() != SOFTBUS_OK) {
179 return SOFTBUS_ERR;
180 }
181 if (LnnInitBusCenterEvent() != SOFTBUS_OK) {
182 LNN_LOGE(LNN_INIT, "init bus center event fail");
183 return SOFTBUS_ERR;
184 }
185 if (LnnInitEventMonitor() != SOFTBUS_OK) {
186 LNN_LOGE(LNN_INIT, "init event monitor fail");
187 return SOFTBUS_ERR;
188 }
189 if (LnnInitDiscoveryManager() != SOFTBUS_OK) {
190 LNN_LOGE(LNN_INIT, "init discovery manager fail");
191 return SOFTBUS_ERR;
192 }
193 if (LnnInitNetworkManager() != SOFTBUS_OK) {
194 LNN_LOGE(LNN_INIT, "init lnn network manager fail");
195 return SOFTBUS_ERR;
196 }
197 if (LnnInitNetBuilder() != SOFTBUS_OK) {
198 LNN_LOGE(LNN_INIT, "init net builder fail");
199 return SOFTBUS_ERR;
200 }
201 if (LnnInitMetaNode() != SOFTBUS_OK) {
202 LNN_LOGE(LNN_INIT, "init meta node fail");
203 return SOFTBUS_ERR;
204 }
205 if (IsActiveOsAccountUnlocked()) {
206 LNN_LOGI(LNN_INIT, "user unlocked try load local deviceinfo");
207 RestoreLocalDeviceInfo();
208 }
209 return SOFTBUS_OK;
210 }
211
BusCenterServerInitSecondStep(void)212 static int32_t BusCenterServerInitSecondStep(void)
213 {
214 SoftBusRunPeriodicalTask(WATCHDOG_TASK_NAME, WatchdogProcess, WATCHDOG_INTERVAL_TIME, WATCHDOG_DELAY_TIME);
215 if (LnnInitLaneHub() != SOFTBUS_OK) {
216 LNN_LOGE(LNN_INIT, "init lane hub fail");
217 return SOFTBUS_ERR;
218 }
219 if (InitNodeAddrAllocator() != SOFTBUS_OK) {
220 LNN_LOGE(LNN_INIT, "init nodeAddr fail");
221 return SOFTBUS_ERR;
222 }
223 if (RouteLSInit() != SOFTBUS_OK) {
224 LNN_LOGE(LNN_INIT, "init route fail");
225 return SOFTBUS_ERR;
226 }
227 if (StartDelayInit() != SOFTBUS_OK) {
228 LNN_LOGE(LNN_INIT, "start delay init fail");
229 return SOFTBUS_ERR;
230 }
231 if (InitDecisionCenter() != SOFTBUS_OK) {
232 LNN_LOGE(LNN_INIT, "initDecisionCenter fail");
233 return SOFTBUS_ERR;
234 }
235 return SOFTBUS_OK;
236 }
237
LnnInitLnnLooper(void)238 int32_t LnnInitLnnLooper(void)
239 {
240 SoftBusLooper *looper = CreateNewLooper("Lnn_Lp");
241 if (!looper) {
242 LNN_LOGE(LNN_LANE, "init laneLooper fail");
243 return SOFTBUS_ERR;
244 }
245 SetLooper(LOOP_TYPE_LNN, looper);
246 LNN_LOGI(LNN_LANE, "init laneLooper success");
247 return SOFTBUS_OK;
248 }
249
LnnDeinitLnnLooper(void)250 void LnnDeinitLnnLooper(void)
251 {
252 SoftBusLooper *looper = GetLooper(LOOP_TYPE_LNN);
253 if (looper != NULL) {
254 DestroyLooper(looper);
255 SetLooper(LOOP_TYPE_LNN, NULL);
256 }
257 }
258
BusCenterServerInit(void)259 int32_t BusCenterServerInit(void)
260 {
261 if (BusCenterServerInitFirstStep() != SOFTBUS_OK) {
262 return SOFTBUS_ERR;
263 }
264 if (BusCenterServerInitSecondStep() != SOFTBUS_OK) {
265 return SOFTBUS_ERR;
266 }
267 LNN_LOGI(LNN_INIT, "bus center server init ok");
268 return SOFTBUS_OK;
269 }
270
BusCenterServerDeinit(void)271 void BusCenterServerDeinit(void)
272 {
273 RouteLSDeinit();
274 DeinitNodeAddrAllocator();
275 LnnDeinitLaneHub();
276 LnnDeinitNetBuilder();
277 LnnDeinitNetworkManager();
278 LnnDeinitEventMonitor();
279 LnnDeinitBusCenterEvent();
280 LnnDeinitNetLedger();
281 DeinitDecisionCenter();
282 LnnDeinitMetaNode();
283 LnnCoapConnectDeinit();
284 LnnDeinitLnnLooper();
285 LNN_LOGI(LNN_INIT, "bus center server deinit");
286 }
287