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_event.h"
22 #include "lnn_async_callback_utils.h"
23 #include "lnn_discovery_manager.h"
24 #include "lnn_event_monitor.h"
25 #include "lnn_lane_hub.h"
26 #include "lnn_network_manager.h"
27 #include "lnn_net_builder.h"
28 #include "lnn_net_ledger.h"
29 #include "lnn_decision_center.h"
30 #include "softbus_adapter_xcollie.h"
31 #include "softbus_def.h"
32 #include "softbus_errcode.h"
33 #include "softbus_feature_config.h"
34 #include "softbus_log.h"
35 #include "softbus_utils.h"
36
37 #define WATCHDOG_TASK_NAME "LNN_WATCHDOG_TASK"
38 #define WATCHDOG_INTERVAL_TIME 10000
39 #define WATCHDOG_DELAY_TIME 5000
40 #define DEFAULT_DELAY_LEN 1000
41 #define RETRY_MAX 10
42
InitNodeAddrAllocator(void)43 int32_t __attribute__((weak)) InitNodeAddrAllocator(void)
44 {
45 return SOFTBUS_OK;
46 }
DeinitNodeAddrAllocator(void)47 void __attribute__((weak)) DeinitNodeAddrAllocator(void) {}
48
RouteLSInit(void)49 int32_t __attribute__((weak)) RouteLSInit(void)
50 {
51 return SOFTBUS_OK;
52 }
RouteLSDeinit(void)53 void __attribute__((weak)) RouteLSDeinit(void) {}
54
55 typedef int32_t (*LnnInitDelayImpl)(void);
56
57 typedef enum {
58 INIT_LOCAL_LEDGER_DELAY_TYPE = 0,
59 INIT_NETWORK_MANAGER_DELAY_TYPE,
60 INIT_NETBUILDER_DELAY_TYPE,
61 INIT_LANEHUB_DELAY_TYPE,
62 INIT_DELAY_MAX_TYPE,
63 } InitDelayType;
64
65 typedef struct {
66 LnnInitDelayImpl implInit;
67 bool isInit;
68 } InitDelayImpl;
69
70 typedef struct {
71 InitDelayImpl initDelayImpl[INIT_DELAY_MAX_TYPE];
72 int32_t delayLen;
73 } LnnLocalConfigInit;
74
WatchdogProcess(void)75 static void WatchdogProcess(void)
76 {
77 if (GetWatchdogFlag()) {
78 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "softbus net_builder thread running normally.");
79 return;
80 }
81 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_WARN, "softbus net_builder thread exception.");
82 }
83
84 static LnnLocalConfigInit g_lnnLocalConfigInit = {
85 .initDelayImpl = {
86 [INIT_LOCAL_LEDGER_DELAY_TYPE] = {
87 .implInit = LnnInitNetLedgerDelay,
88 .isInit = false,
89 },
90 [INIT_NETWORK_MANAGER_DELAY_TYPE] = {
91 .implInit = LnnInitNetworkManagerDelay,
92 .isInit = false,
93 },
94 [INIT_NETBUILDER_DELAY_TYPE] = {
95 .implInit = LnnInitNetBuilderDelay,
96 .isInit = false,
97 },
98 [INIT_LANEHUB_DELAY_TYPE] = {
99 .implInit = LnnInitLaneHubDelay,
100 .isInit = false,
101 },
102 },
103 };
104
ReadDelayConfig(void)105 static void ReadDelayConfig(void)
106 {
107 if (SoftbusGetConfig(SOFTBUS_INT_LNN_UDID_INIT_DELAY_LEN,
108 (unsigned char *)&g_lnnLocalConfigInit.delayLen, sizeof(g_lnnLocalConfigInit.delayLen)) != SOFTBUS_OK) {
109 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "get lnn delay init len fail, use default value");
110 g_lnnLocalConfigInit.delayLen = DEFAULT_DELAY_LEN;
111 }
112 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "lnn delay init len is %u", g_lnnLocalConfigInit.delayLen);
113 }
114
BusCenterServerDelayInit(void * para)115 static void BusCenterServerDelayInit(void *para)
116 {
117 (void)para;
118 static int32_t retry = 0;
119 if (retry > RETRY_MAX) {
120 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "try BusCenterServerDelayInit max times");
121 return;
122 }
123 int32_t ret = SOFTBUS_OK;
124 uint32_t i;
125 for (i = 0; i < INIT_DELAY_MAX_TYPE; ++i) {
126 if (g_lnnLocalConfigInit.initDelayImpl[i].implInit == NULL) {
127 continue;
128 }
129 /* initialize the lane hub module after successfully initializing the local ledger. */
130 if (i == INIT_LANEHUB_DELAY_TYPE &&
131 !g_lnnLocalConfigInit.initDelayImpl[INIT_LOCAL_LEDGER_DELAY_TYPE].isInit) {
132 continue;
133 }
134 if (!g_lnnLocalConfigInit.initDelayImpl[i].isInit &&
135 g_lnnLocalConfigInit.initDelayImpl[i].implInit() != SOFTBUS_OK) {
136 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "init delay impl(%u) failed", i);
137 ret = SOFTBUS_ERR;
138 } else {
139 g_lnnLocalConfigInit.initDelayImpl[i].isInit = true;
140 }
141 }
142 if (ret != SOFTBUS_OK) {
143 retry++;
144 SoftBusLooper *looper = GetLooper(LOOP_TYPE_DEFAULT);
145 ret = LnnAsyncCallbackDelayHelper(looper, BusCenterServerDelayInit, NULL, g_lnnLocalConfigInit.delayLen);
146 if (ret != SOFTBUS_OK) {
147 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "BusCenterServerDelayInit LnnAsyncCallbackDelayHelper fail");
148 }
149 }
150 }
151
StartDelayInit(void)152 static int32_t StartDelayInit(void)
153 {
154 ReadDelayConfig();
155 int32_t ret = LnnAsyncCallbackDelayHelper(GetLooper(LOOP_TYPE_DEFAULT), BusCenterServerDelayInit,
156 NULL, g_lnnLocalConfigInit.delayLen);
157 if (ret != SOFTBUS_OK) {
158 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "StartDelayInit LnnAsyncCallbackDelayHelper fail");
159 }
160 return ret;
161 }
162
BusCenterServerInit(void)163 NO_SANITIZE("cfi") int32_t BusCenterServerInit(void)
164 {
165 if (LnnInitNetLedger() != SOFTBUS_OK) {
166 return SOFTBUS_ERR;
167 }
168 if (LnnInitDecisionCenter(DC_VERSION_1_0) != SOFTBUS_OK) {
169 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "start decision center init fail!");
170 return SOFTBUS_ERR;
171 }
172 if (LnnInitBusCenterEvent() != SOFTBUS_OK) {
173 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "init bus center event failed");
174 return SOFTBUS_ERR;
175 }
176 if (LnnInitEventMonitor() != SOFTBUS_OK) {
177 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "init event monitor fail");
178 return SOFTBUS_ERR;
179 }
180 if (LnnInitDiscoveryManager() != SOFTBUS_OK) {
181 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "init discovery manager fail");
182 return SOFTBUS_ERR;
183 }
184 if (LnnInitNetworkManager() != SOFTBUS_OK) {
185 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "init lnn network manager fail!");
186 return SOFTBUS_ERR;
187 }
188 if (LnnInitNetBuilder() != SOFTBUS_OK) {
189 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "init net builder fail!");
190 return SOFTBUS_ERR;
191 }
192 SoftBusRunPeriodicalTask(WATCHDOG_TASK_NAME, WatchdogProcess, WATCHDOG_INTERVAL_TIME, WATCHDOG_DELAY_TIME);
193 if (LnnInitLaneHub() != SOFTBUS_OK) {
194 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "init lane hub fail!");
195 return SOFTBUS_ERR;
196 }
197 if (InitNodeAddrAllocator() != SOFTBUS_OK) {
198 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "init nodeAddr failed.");
199 return SOFTBUS_ERR;
200 }
201 if (RouteLSInit() != SOFTBUS_OK) {
202 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "init route failed.");
203 return SOFTBUS_ERR;
204 }
205 if (StartDelayInit() != SOFTBUS_OK) {
206 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "start delay init fail!");
207 return SOFTBUS_ERR;
208 }
209 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "bus center server init ok");
210 return SOFTBUS_OK;
211 }
212
BusCenterServerDeinit(void)213 NO_SANITIZE("cfi") void BusCenterServerDeinit(void)
214 {
215 RouteLSDeinit();
216 DeinitNodeAddrAllocator();
217 LnnDeinitLaneHub();
218 LnnDeinitNetBuilder();
219 LnnDeinitNetworkManager();
220 LnnDeinitEventMonitor();
221 LnnDeinitBusCenterEvent();
222 DeinitNodeAddrAllocator();
223 LnnDeinitDecisionCenter();
224 LnnDeinitNetLedger();
225 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "bus center server deinit");
226 }
227