1 /*
2 * Copyright (c) 2022 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_heartbeat_fsm.h"
17
18 #include <securec.h>
19 #include <string.h>
20
21 #include "anonymizer.h"
22 #include "bus_center_manager.h"
23 #include "g_enhance_lnn_func_pack.h"
24 #include "lnn_connection_addr_utils.h"
25 #include "lnn_deviceinfo_to_profile.h"
26 #include "lnn_distributed_net_ledger.h"
27 #include "lnn_feature_capability.h"
28 #include "lnn_heartbeat_ctrl.h"
29 #include "lnn_heartbeat_strategy.h"
30 #include "lnn_heartbeat_utils.h"
31 #include "lnn_log.h"
32 #include "lnn_net_builder.h"
33 #include "lnn_node_info.h"
34 #include "message_handler.h"
35
36 #include "softbus_adapter_mem.h"
37 #include "softbus_adapter_timer.h"
38 #include "softbus_error_code.h"
39 #include "legacy/softbus_hisysevt_bus_center.h"
40
41 #define TO_HEARTBEAT_FSM(ptr) CONTAINER_OF(ptr, LnnHeartbeatFsm, fsm)
42
43 typedef int32_t (*LnnHeartbeatEventHandler)(FsmStateMachine *, int32_t, void *);
44
45 typedef struct {
46 int32_t eventType;
47 LnnHeartbeatEventHandler eventHandler;
48 } LnnHeartbeatStateHandler;
49
50 typedef struct {
51 int32_t eventNum;
52 LnnHeartbeatStateHandler *stateHandler;
53 } LnnHeartbeatFsmHandler;
54
55 static int32_t OnCheckDevStatus(FsmStateMachine *fsm, int32_t msgType, void *para);
56 static int32_t OnTransHbFsmState(FsmStateMachine *fsm, int32_t msgType, void *para);
57 static int32_t OnStartHbProcess(FsmStateMachine *fsm, int32_t msgType, void *para);
58 static int32_t OnStopHbByType(FsmStateMachine *fsm, int32_t msgType, void *para);
59 static int32_t OnSendOneHbBegin(FsmStateMachine *fsm, int32_t msgType, void *para);
60 static int32_t OnSendOneHbEnd(FsmStateMachine *fsm, int32_t msgType, void *para);
61 static int32_t OnProcessSendOnce(FsmStateMachine *fsm, int32_t msgType, void *para);
62 static int32_t OnSetMediumParam(FsmStateMachine *fsm, int32_t msgType, void *para);
63 static int32_t OnUpdateSendInfo(FsmStateMachine *fsm, int32_t msgType, void *para);
64 static int32_t OnScreeOffCheckDevStatus(FsmStateMachine *fsm, int32_t msgType, void *para);
65 static int32_t OnReStartHbProcess(FsmStateMachine *fsm, int32_t msgType, void *para);
66 static int32_t OnCheckSleDevStatus(FsmStateMachine *fsm, int32_t msgType, void *para);
67
68 static LnnHeartbeatStateHandler g_hbNoneStateHandler[] = {
69 {EVENT_HB_CHECK_DEV_STATUS, OnCheckDevStatus},
70 {EVENT_HB_START_PROCESS, OnStartHbProcess},
71 {EVENT_HB_STOP_SPECIFIC, OnStopHbByType},
72 {EVENT_HB_AS_MASTER_NODE, OnTransHbFsmState},
73 {EVENT_HB_AS_NORMAL_NODE, OnTransHbFsmState},
74 {EVENT_HB_IN_NONE_STATE, OnTransHbFsmState},
75 {EVENT_HB_SCREEN_OFF_CHECK_STATUS, OnScreeOffCheckDevStatus},
76 };
77
78 static LnnHeartbeatStateHandler g_normalNodeHandler[] = {
79 {EVENT_HB_SEND_ONE_BEGIN, OnSendOneHbBegin},
80 {EVENT_HB_SEND_ONE_END, OnSendOneHbEnd},
81 {EVENT_HB_CHECK_DEV_STATUS, OnCheckDevStatus},
82 {EVENT_HB_PROCESS_SEND_ONCE, OnProcessSendOnce},
83 {EVENT_HB_AS_MASTER_NODE, OnTransHbFsmState},
84 {EVENT_HB_AS_NORMAL_NODE, OnTransHbFsmState},
85 {EVENT_HB_IN_NONE_STATE, OnTransHbFsmState},
86 {EVENT_HB_SET_MEDIUM_PARAM, OnSetMediumParam},
87 {EVENT_HB_UPDATE_SEND_INFO, OnUpdateSendInfo},
88 {EVENT_HB_STOP_SPECIFIC, OnStopHbByType},
89 {EVENT_HB_SCREEN_OFF_CHECK_STATUS, OnScreeOffCheckDevStatus},
90 {EVENT_HB_START_PROCESS, OnReStartHbProcess},
91 {EVENT_HB_CHECK_SLE_DEV_STATUS, OnCheckSleDevStatus},
92 };
93
94 static LnnHeartbeatStateHandler g_masterNodeHandler[] = {
95 {EVENT_HB_SEND_ONE_BEGIN, OnSendOneHbBegin},
96 {EVENT_HB_SEND_ONE_END, OnSendOneHbEnd},
97 {EVENT_HB_CHECK_DEV_STATUS, OnCheckDevStatus},
98 {EVENT_HB_PROCESS_SEND_ONCE, OnProcessSendOnce},
99 {EVENT_HB_AS_MASTER_NODE, OnTransHbFsmState},
100 {EVENT_HB_AS_NORMAL_NODE, OnTransHbFsmState},
101 {EVENT_HB_IN_NONE_STATE, OnTransHbFsmState},
102 {EVENT_HB_SET_MEDIUM_PARAM, OnSetMediumParam},
103 {EVENT_HB_UPDATE_SEND_INFO, OnUpdateSendInfo},
104 {EVENT_HB_STOP_SPECIFIC, OnStopHbByType},
105 {EVENT_HB_SCREEN_OFF_CHECK_STATUS, OnScreeOffCheckDevStatus},
106 {EVENT_HB_START_PROCESS, OnReStartHbProcess},
107 {EVENT_HB_CHECK_SLE_DEV_STATUS, OnCheckSleDevStatus},
108 };
109
110 static LnnHeartbeatFsmHandler g_hbFsmHandler[] = {
111 [STATE_HB_NONE_INDEX] = {
112 .eventNum = sizeof(g_hbNoneStateHandler) / sizeof(LnnHeartbeatStateHandler),
113 .stateHandler = g_hbNoneStateHandler,
114 },
115 [STATE_HB_NORMAL_NODE_INDEX] = {
116 .eventNum = sizeof(g_normalNodeHandler) / sizeof(LnnHeartbeatStateHandler),
117 .stateHandler = g_normalNodeHandler,
118 },
119 [STATE_HB_MASTER_NODE_INDEX] = {
120 .eventNum = sizeof(g_masterNodeHandler) / sizeof(LnnHeartbeatStateHandler),
121 .stateHandler = g_masterNodeHandler,
122 }
123 };
124
125 static void HbNoneStateEnter(FsmStateMachine *fsm);
126 static void HbNoneStateExit(FsmStateMachine *fsm);
127 static void HbNormalNodeStateEnter(FsmStateMachine *fsm);
128 static void HbMasterNodeStateEnter(FsmStateMachine *fsm);
129 static void HbMasterNodeStateExit(FsmStateMachine *fsm);
130 static bool HbFsmStateProcessFunc(FsmStateMachine *fsm, int32_t msgType, void *para);
131
132 static FsmState g_hbState[STATE_HB_INDEX_MAX] = {
133 [STATE_HB_NONE_INDEX] = {
134 .enter = HbNoneStateEnter,
135 .process = HbFsmStateProcessFunc,
136 .exit = HbNoneStateExit,
137 },
138 [STATE_HB_NORMAL_NODE_INDEX] = {
139 .enter = HbNormalNodeStateEnter,
140 .process = HbFsmStateProcessFunc,
141 .exit = NULL,
142 },
143 [STATE_HB_MASTER_NODE_INDEX] = {
144 .enter = HbMasterNodeStateEnter,
145 .process = HbFsmStateProcessFunc,
146 .exit = HbMasterNodeStateExit,
147 },
148 };
149
CheckHbFsmStateMsgArgs(const FsmStateMachine * fsm)150 static bool CheckHbFsmStateMsgArgs(const FsmStateMachine *fsm)
151 {
152 if (fsm == NULL) {
153 LNN_LOGW(LNN_HEART_BEAT, "fsm is null");
154 return false;
155 }
156 LnnHeartbeatFsm * hbFsm = TO_HEARTBEAT_FSM(fsm);
157 if (hbFsm == NULL) {
158 LNN_LOGE(LNN_HEART_BEAT, "hbFsm is null");
159 return false;
160 }
161 if (hbFsm->state < STATE_HB_INDEX_MIN || hbFsm->state >= STATE_HB_INDEX_MAX) {
162 LNN_LOGE(LNN_HEART_BEAT, "fsmId is in invalid. fsmId=%{public}d, state=%{public}d", hbFsm->id, hbFsm->state);
163 return false;
164 }
165 return true;
166 }
167
FreeUnhandledHbMessage(int32_t msgType,void * para)168 static void FreeUnhandledHbMessage(int32_t msgType, void *para)
169 {
170 LNN_LOGI(LNN_HEART_BEAT, "free unhandled msgType=%{public}d", msgType);
171 if (msgType == EVENT_HB_UPDATE_SEND_INFO) {
172 /* this event use pointer to transfer parameters */
173 return;
174 }
175 if (para != NULL) {
176 SoftBusFree(para);
177 }
178 }
179
HbFsmStateProcessFunc(FsmStateMachine * fsm,int32_t msgType,void * para)180 static bool HbFsmStateProcessFunc(FsmStateMachine *fsm, int32_t msgType, void *para)
181 {
182 int32_t i, eventNum, ret;
183 LnnHeartbeatStateHandler *stateHandler = NULL;
184
185 if (!CheckHbFsmStateMsgArgs(fsm) || msgType <= EVENT_HB_MIN || msgType >= EVENT_HB_MAX) {
186 FreeUnhandledHbMessage(msgType, para);
187 return false;
188 }
189 LnnHeartbeatFsm * hbFsm = TO_HEARTBEAT_FSM(fsm);
190 eventNum = g_hbFsmHandler[hbFsm->state].eventNum;
191 stateHandler = g_hbFsmHandler[hbFsm->state].stateHandler;
192
193 for (i = 0; i < eventNum; ++i) {
194 if (stateHandler[i].eventType != msgType) {
195 continue;
196 }
197 /* in this case, free the memory of para in eventHandler FUNC */
198 ret = (stateHandler[i].eventHandler)(fsm, msgType, para);
199 if (ret != SOFTBUS_OK) {
200 LNN_LOGE(LNN_HEART_BEAT, "FSM process hb fail, hbType=%{public}d, ret=%{public}d", msgType, ret);
201 return false;
202 }
203 LNN_LOGD(LNN_HEART_BEAT, "FSM process hb succ, hbType=%{public}d, state=%{public}d", msgType, hbFsm->state);
204 return true;
205 }
206 LNN_LOGD(LNN_HEART_BEAT, "no eventHandler in. hbType=%{public}d, state=%{public}d", msgType, hbFsm->state);
207 FreeUnhandledHbMessage(msgType, para);
208 return false;
209 }
210
CheckRemoveHbMsgParams(const SoftBusMessage * msg,void * args)211 static bool CheckRemoveHbMsgParams(const SoftBusMessage *msg, void *args)
212 {
213 if (msg == NULL || args == NULL) {
214 LNN_LOGE(LNN_HEART_BEAT, "msg or args is NULL");
215 return false;
216 }
217 FsmCtrlMsgObj *ctrlMsgObj = (FsmCtrlMsgObj *)msg->obj;
218 if (ctrlMsgObj == NULL || ctrlMsgObj->obj == NULL) {
219 LNN_LOGE(LNN_HEART_BEAT, "ctrlMsgObj or obj is NULL");
220 return false;
221 }
222 SoftBusMessage *delMsg = (SoftBusMessage *)args;
223 if (delMsg == NULL || delMsg->obj == NULL) {
224 LNN_LOGE(LNN_HEART_BEAT, "delMsg or obj is NULL");
225 return false;
226 }
227 return true;
228 }
229
RemoveCheckDevStatusMsg(FsmCtrlMsgObj * ctrlMsgObj,SoftBusMessage * delMsg)230 static int32_t RemoveCheckDevStatusMsg(FsmCtrlMsgObj *ctrlMsgObj, SoftBusMessage *delMsg)
231 {
232 LnnCheckDevStatusMsgPara *msgPara = (LnnCheckDevStatusMsgPara *)ctrlMsgObj->obj;
233 LnnCheckDevStatusMsgPara *delMsgPara = (LnnCheckDevStatusMsgPara *)delMsg->obj;
234
235 if (delMsgPara->hasNetworkId != msgPara->hasNetworkId) {
236 return SOFTBUS_NETWORK_HB_REMOVE_MSG_FAIL;
237 }
238 if (!delMsgPara->hasNetworkId && msgPara->hbType == delMsgPara->hbType) {
239 SoftBusFree(msgPara);
240 msgPara = NULL;
241 return SOFTBUS_OK;
242 }
243 if (delMsgPara->hasNetworkId && msgPara->hbType == delMsgPara->hbType &&
244 strcmp(msgPara->networkId, delMsgPara->networkId) == 0) {
245 SoftBusFree(msgPara);
246 msgPara = NULL;
247 return SOFTBUS_OK;
248 }
249 return SOFTBUS_NETWORK_HB_REMOVE_MSG_FAIL;
250 }
251
RemoveSendOnceMsg(FsmCtrlMsgObj * ctrlMsgObj,SoftBusMessage * delMsg)252 static int32_t RemoveSendOnceMsg(FsmCtrlMsgObj *ctrlMsgObj, SoftBusMessage *delMsg)
253 {
254 LnnProcessSendOnceMsgPara *msgPara = (LnnProcessSendOnceMsgPara *)ctrlMsgObj->obj;
255 LnnProcessSendOnceMsgPara *delMsgPara = (LnnProcessSendOnceMsgPara *)delMsg->obj;
256
257 if (((msgPara->hbType & delMsgPara->hbType) == delMsgPara->hbType) &&
258 msgPara->strategyType == delMsgPara->strategyType) {
259 SoftBusFree(msgPara);
260 msgPara = NULL;
261 return SOFTBUS_OK;
262 }
263 return SOFTBUS_NETWORK_HB_REMOVE_MSG_FAIL;
264 }
265
RemoveSendOneEndMsgStepOne(LnnHeartbeatSendEndData * msgPara,LnnRemoveSendEndMsgPara * delMsgPara)266 static int32_t RemoveSendOneEndMsgStepOne(LnnHeartbeatSendEndData *msgPara, LnnRemoveSendEndMsgPara *delMsgPara)
267 {
268 if (msgPara->isMsdpRange && delMsgPara != NULL) {
269 *delMsgPara->isRemoved = true;
270 SoftBusFree(msgPara);
271 msgPara = NULL;
272 return SOFTBUS_OK;
273 }
274 if (!msgPara->isMsdpRange && delMsgPara->isMsdpRange) {
275 *delMsgPara->isRemoved = false;
276 return SOFTBUS_NETWORK_HB_REMOVE_MSG_FAIL;
277 }
278 if (!msgPara->wakeupFlag && delMsgPara->wakeupFlag) {
279 *delMsgPara->isRemoved = true;
280 SoftBusFree(msgPara);
281 msgPara = NULL;
282 return SOFTBUS_OK;
283 }
284 if (msgPara->wakeupFlag && !delMsgPara->wakeupFlag) {
285 *delMsgPara->isRemoved = false;
286 return SOFTBUS_NETWORK_HB_REMOVE_MSG_FAIL;
287 }
288 if (msgPara->isRelay && (msgPara->hbType & HEARTBEAT_TYPE_BLE_V0) != 0) {
289 *delMsgPara->isRemoved = true;
290 SoftBusFree(msgPara);
291 msgPara = NULL;
292 return SOFTBUS_OK;
293 }
294 return SOFTBUS_NETWORK_HB_INVALID_MGR;
295 }
296
RemoveSendOneEndMsgStepTwo(LnnHeartbeatSendEndData * msgPara,LnnRemoveSendEndMsgPara * delMsgPara)297 static int32_t RemoveSendOneEndMsgStepTwo(LnnHeartbeatSendEndData *msgPara, LnnRemoveSendEndMsgPara *delMsgPara)
298 {
299 if (delMsgPara->isRelay && (delMsgPara->hbType & HEARTBEAT_TYPE_BLE_V0) != 0) {
300 *delMsgPara->isRemoved = false;
301 return SOFTBUS_NETWORK_HB_REMOVE_MSG_FAIL;
302 }
303 if ((msgPara->hbType & HEARTBEAT_TYPE_BLE_V1) != 0 && (delMsgPara->hbType & HEARTBEAT_TYPE_BLE_V0) != 0) {
304 *delMsgPara->isRemoved = true;
305 SoftBusFree(msgPara);
306 msgPara = NULL;
307 return SOFTBUS_OK;
308 }
309 if ((msgPara->hbType & HEARTBEAT_TYPE_BLE_V0) != 0 && (delMsgPara->hbType & HEARTBEAT_TYPE_BLE_V1) != 0) {
310 *delMsgPara->isRemoved = false;
311 return SOFTBUS_NETWORK_HB_REMOVE_MSG_FAIL;
312 }
313 if (msgPara->hbType == delMsgPara->hbType && (delMsgPara->hbType & HEARTBEAT_TYPE_BLE_V0) != 0) {
314 *delMsgPara->isRemoved = true;
315 SoftBusFree(msgPara);
316 msgPara = NULL;
317 return SOFTBUS_OK;
318 }
319 if (msgPara->hbType == delMsgPara->hbType && (delMsgPara->hbType & HEARTBEAT_TYPE_BLE_V1) != 0) {
320 *delMsgPara->isRemoved = false;
321 return SOFTBUS_NETWORK_HB_REMOVE_MSG_FAIL;
322 }
323 *delMsgPara->isRemoved = false;
324 return SOFTBUS_NETWORK_HB_REMOVE_MSG_FAIL;
325 }
326
RemoveSendOneEndMsg(FsmCtrlMsgObj * ctrlMsgObj,SoftBusMessage * delMsg)327 static int32_t RemoveSendOneEndMsg(FsmCtrlMsgObj *ctrlMsgObj, SoftBusMessage *delMsg)
328 {
329 LnnHeartbeatSendEndData *msgPara = (LnnHeartbeatSendEndData *)ctrlMsgObj->obj;
330 LnnRemoveSendEndMsgPara *delMsgPara = (LnnRemoveSendEndMsgPara *)delMsg->obj;
331 int32_t ret = RemoveSendOneEndMsgStepOne(msgPara, delMsgPara);
332 if (ret == SOFTBUS_NETWORK_HB_REMOVE_MSG_FAIL || ret == SOFTBUS_OK) {
333 return ret;
334 }
335 return RemoveSendOneEndMsgStepTwo(msgPara, delMsgPara);
336 }
337
RemoveScreenOffCheckStatus(FsmCtrlMsgObj * ctrlMsgObj,SoftBusMessage * delMsg)338 static int32_t RemoveScreenOffCheckStatus(FsmCtrlMsgObj *ctrlMsgObj, SoftBusMessage *delMsg)
339 {
340 LnnCheckDevStatusMsgPara *msgPara = (LnnCheckDevStatusMsgPara *)ctrlMsgObj->obj;
341 LnnCheckDevStatusMsgPara *delMsgPara = (LnnCheckDevStatusMsgPara *)delMsg->obj;
342
343 if (delMsgPara->hasNetworkId != msgPara->hasNetworkId) {
344 return SOFTBUS_NETWORK_HB_REMOVE_MSG_FAIL;
345 }
346 if (!delMsgPara->hasNetworkId && msgPara->hbType == delMsgPara->hbType) {
347 SoftBusFree(msgPara);
348 msgPara = NULL;
349 return SOFTBUS_OK;
350 }
351 if (delMsgPara->hasNetworkId && msgPara->hbType == delMsgPara->hbType &&
352 strcmp(msgPara->networkId, delMsgPara->networkId) == 0) {
353 SoftBusFree(msgPara);
354 msgPara = NULL;
355 return SOFTBUS_OK;
356 }
357 return SOFTBUS_NETWORK_HB_REMOVE_MSG_FAIL;
358 }
359
RemoveCheckSleDevStatusMsg(FsmCtrlMsgObj * ctrlMsgObj,SoftBusMessage * delMsg)360 static int32_t RemoveCheckSleDevStatusMsg(FsmCtrlMsgObj *ctrlMsgObj, SoftBusMessage *delMsg)
361 {
362 LnnCheckDevStatusMsgPara *msgPara = (LnnCheckDevStatusMsgPara *)ctrlMsgObj->obj;
363 LnnCheckDevStatusMsgPara *delMsgPara = (LnnCheckDevStatusMsgPara *)delMsg->obj;
364
365 if (delMsgPara->hasNetworkId != msgPara->hasNetworkId) {
366 return SOFTBUS_NETWORK_HB_REMOVE_MSG_FAIL;
367 }
368 if (!delMsgPara->hasNetworkId && msgPara->hbType == delMsgPara->hbType) {
369 return SOFTBUS_NETWORK_HB_REMOVE_MSG_FAIL;
370 }
371 if (delMsgPara->hasNetworkId && msgPara->hbType == delMsgPara->hbType &&
372 strcmp(msgPara->networkId, delMsgPara->networkId) == 0) {
373 SoftBusFree(msgPara);
374 msgPara = NULL;
375 return SOFTBUS_OK;
376 }
377 return SOFTBUS_NETWORK_HB_REMOVE_MSG_FAIL;
378 }
379
CustomFuncRemoveHbMsg(const SoftBusMessage * msg,void * args)380 static int32_t CustomFuncRemoveHbMsg(const SoftBusMessage *msg, void *args)
381 {
382 if (!CheckRemoveHbMsgParams(msg, args)) {
383 return SOFTBUS_NETWORK_HB_REMOVE_MSG_FAIL;
384 }
385
386 SoftBusMessage *delMsg = (SoftBusMessage *)args;
387 if (msg->what != delMsg->what || msg->arg1 != delMsg->arg1) {
388 return SOFTBUS_NETWORK_HB_REMOVE_MSG_FAIL;
389 }
390 FsmCtrlMsgObj *ctrlMsgObj = (FsmCtrlMsgObj *)msg->obj;
391 switch (delMsg->arg1) {
392 case EVENT_HB_CHECK_DEV_STATUS:
393 return RemoveCheckDevStatusMsg(ctrlMsgObj, delMsg);
394 case EVENT_HB_PROCESS_SEND_ONCE:
395 return RemoveSendOnceMsg(ctrlMsgObj, delMsg);
396 case EVENT_HB_SEND_ONE_END:
397 return RemoveSendOneEndMsg(ctrlMsgObj, delMsg);
398 case EVENT_HB_SCREEN_OFF_CHECK_STATUS:
399 return RemoveScreenOffCheckStatus(ctrlMsgObj, delMsg);
400 case EVENT_HB_CHECK_SLE_DEV_STATUS:
401 return RemoveCheckSleDevStatusMsg(ctrlMsgObj, delMsg);
402 default:
403 break;
404 }
405 return SOFTBUS_NETWORK_HB_REMOVE_MSG_FAIL;
406 }
407
RemoveHbMsgByCustObj(LnnHeartbeatFsm * hbFsm,LnnHeartbeatEventType evtType,void * obj)408 static void RemoveHbMsgByCustObj(LnnHeartbeatFsm *hbFsm, LnnHeartbeatEventType evtType, void *obj)
409 {
410 int32_t ret;
411
412 SoftBusMessage removeMsg = {
413 .what = FSM_CTRL_MSG_DATA,
414 .arg1 = evtType,
415 .obj = obj,
416 };
417 ret = LnnFsmRemoveMessageSpecific(&hbFsm->fsm, CustomFuncRemoveHbMsg, (void *)&removeMsg);
418 if (ret != SOFTBUS_OK) {
419 LNN_LOGE(LNN_HEART_BEAT, "remove offline fb fail. hbType=%{public}d, fsmId=%{public}d ", hbFsm->id, evtType);
420 }
421 }
422
LnnRemoveSendEndMsg(LnnHeartbeatFsm * hbFsm,LnnProcessSendOnceMsgPara * msg,bool wakeupFlag,bool * isRemoved)423 void LnnRemoveSendEndMsg(LnnHeartbeatFsm *hbFsm, LnnProcessSendOnceMsgPara *msg, bool wakeupFlag, bool *isRemoved)
424 {
425 if (hbFsm == NULL || isRemoved == NULL) {
426 LNN_LOGW(LNN_HEART_BEAT, "remove send end msg get invalid param");
427 return;
428 }
429
430 *isRemoved = true;
431 LnnRemoveSendEndMsgPara msgPara = {
432 .hbType = msg->hbType & (LnnIsLocalSupportBurstFeature() ? HEARTBEAT_TYPE_INVALID : ~HEARTBEAT_TYPE_BLE_V3),
433 .wakeupFlag = wakeupFlag,
434 .isRelay = msg->isRelay,
435 .isRemoved = isRemoved,
436 .isMsdpRange = msg->isMsdpRange,
437 };
438 RemoveHbMsgByCustObj(hbFsm, EVENT_HB_SEND_ONE_END, (void *)&msgPara);
439 msgPara.isRemoved = NULL;
440 }
441
LnnRemoveCheckDevStatusMsg(LnnHeartbeatFsm * hbFsm,LnnCheckDevStatusMsgPara * msgPara)442 void LnnRemoveCheckDevStatusMsg(LnnHeartbeatFsm *hbFsm, LnnCheckDevStatusMsgPara *msgPara)
443 {
444 if (hbFsm == NULL || msgPara == NULL) {
445 LNN_LOGW(LNN_HEART_BEAT, "remove check msg get invalid param");
446 return;
447 }
448 RemoveHbMsgByCustObj(hbFsm, EVENT_HB_CHECK_DEV_STATUS, (void *)msgPara);
449 }
450
LnnRemoveScreenOffCheckStatusMsg(LnnHeartbeatFsm * hbFsm,LnnCheckDevStatusMsgPara * msgPara)451 void LnnRemoveScreenOffCheckStatusMsg(LnnHeartbeatFsm *hbFsm, LnnCheckDevStatusMsgPara *msgPara)
452 {
453 if (hbFsm == NULL || msgPara == NULL) {
454 LNN_LOGW(LNN_HEART_BEAT, "remove check msg get invalid param");
455 return;
456 }
457 RemoveHbMsgByCustObj(hbFsm, EVENT_HB_SCREEN_OFF_CHECK_STATUS, (void *)msgPara);
458 }
459
LnnRemoveSleCheckStatusMsg(LnnHeartbeatFsm * hbFsm,LnnCheckDevStatusMsgPara * msgPara)460 void LnnRemoveSleCheckStatusMsg(LnnHeartbeatFsm *hbFsm, LnnCheckDevStatusMsgPara *msgPara)
461 {
462 if (hbFsm == NULL || msgPara == NULL) {
463 LNN_LOGW(LNN_HEART_BEAT, "remove check msg get invalid param");
464 return;
465 }
466 RemoveHbMsgByCustObj(hbFsm, EVENT_HB_CHECK_SLE_DEV_STATUS, (void *)msgPara);
467 }
468
LnnRemoveProcessSendOnceMsg(LnnHeartbeatFsm * hbFsm,LnnHeartbeatType hbType,LnnHeartbeatStrategyType strategyType)469 void LnnRemoveProcessSendOnceMsg(LnnHeartbeatFsm *hbFsm, LnnHeartbeatType hbType,
470 LnnHeartbeatStrategyType strategyType)
471 {
472 if (hbFsm == NULL) {
473 LNN_LOGW(LNN_HEART_BEAT, "remove process send once msg get invalid param");
474 return;
475 }
476 LnnProcessSendOnceMsgPara msgPara = {
477 .hbType = hbType,
478 .strategyType = strategyType,
479 };
480 RemoveHbMsgByCustObj(hbFsm, EVENT_HB_PROCESS_SEND_ONCE, (void *)&msgPara);
481 }
482
HbMasterNodeStateEnter(FsmStateMachine * fsm)483 static void HbMasterNodeStateEnter(FsmStateMachine *fsm)
484 {
485 if (!CheckHbFsmStateMsgArgs(fsm)) {
486 LNN_LOGW(LNN_HEART_BEAT, "msg args check fail");
487 return;
488 }
489 LnnHeartbeatFsm *hbFsm = TO_HEARTBEAT_FSM(fsm);
490 hbFsm->state = STATE_HB_MASTER_NODE_INDEX;
491 LnnProcessSendOnceMsgPara *msgPara = (LnnProcessSendOnceMsgPara *)SoftBusCalloc(sizeof(LnnProcessSendOnceMsgPara));
492 if (msgPara == NULL) {
493 LNN_LOGE(LNN_HEART_BEAT, "enter master node malloc err. fsmId=%{public}d", hbFsm->id);
494 return;
495 }
496 msgPara->hbType = hbFsm->hbType;
497 msgPara->strategyType = hbFsm->strategyType;
498 msgPara->isRelay = false;
499 msgPara->isSyncData = false;
500 msgPara->isDirectBoardcast = false;
501 msgPara->isMsdpRange = false;
502 LnnRemoveProcessSendOnceMsg(hbFsm, hbFsm->hbType, hbFsm->strategyType);
503 if (LnnFsmPostMessage(fsm, EVENT_HB_PROCESS_SEND_ONCE, (void *)msgPara) != SOFTBUS_OK) {
504 SoftBusFree(msgPara);
505 return;
506 }
507 LNN_LOGI(LNN_HEART_BEAT, "perform as master node. fsmId=%{public}d", hbFsm->id);
508 }
509
HbMasterNodeStateExit(FsmStateMachine * fsm)510 static void HbMasterNodeStateExit(FsmStateMachine *fsm)
511 {
512 if (!CheckHbFsmStateMsgArgs(fsm)) {
513 return;
514 }
515 LnnHeartbeatFsm *hbFsm = TO_HEARTBEAT_FSM(fsm);
516 LnnRemoveProcessSendOnceMsg(hbFsm, hbFsm->hbType, STRATEGY_HB_SEND_FIXED_PERIOD);
517 }
518
HbNormalNodeStateEnter(FsmStateMachine * fsm)519 static void HbNormalNodeStateEnter(FsmStateMachine *fsm)
520 {
521 LnnDumpHbMgrRecvList();
522 LnnDumpHbOnlineNodeList();
523 if (!CheckHbFsmStateMsgArgs(fsm)) {
524 return;
525 }
526 LnnHeartbeatFsm *hbFsm = TO_HEARTBEAT_FSM(fsm);
527 hbFsm->state = STATE_HB_NORMAL_NODE_INDEX;
528 LnnRemoveProcessSendOnceMsg(hbFsm, hbFsm->hbType, STRATEGY_HB_SEND_FIXED_PERIOD);
529 LNN_LOGI(LNN_HEART_BEAT, "perform as normal node. fsmId=%{public}d", hbFsm->id);
530 }
531
HbNoneStateEnter(FsmStateMachine * fsm)532 static void HbNoneStateEnter(FsmStateMachine *fsm)
533 {
534 if (!CheckHbFsmStateMsgArgs(fsm)) {
535 LNN_LOGW(LNN_HEART_BEAT, "msg args check fail");
536 return;
537 }
538 LnnHeartbeatFsm *hbFsm = TO_HEARTBEAT_FSM(fsm);
539 hbFsm->state = STATE_HB_NONE_INDEX;
540 LNN_LOGI(LNN_HEART_BEAT, "perform none state. fsmId=%{public}d", hbFsm->id);
541
542 if (LnnHbMediumMgrStop(&hbFsm->hbType) != SOFTBUS_OK) {
543 LNN_LOGE(LNN_HEART_BEAT, "stop medium manager fail");
544 return;
545 }
546 LnnFsmRemoveMessage(fsm, EVENT_HB_PROCESS_SEND_ONCE);
547 }
548
HbNoneStateExit(FsmStateMachine * fsm)549 static void HbNoneStateExit(FsmStateMachine *fsm)
550 {
551 if (!CheckHbFsmStateMsgArgs(fsm)) {
552 return;
553 }
554 LnnFsmPostMessage(fsm, EVENT_HB_UPDATE_SEND_INFO, (void *)(uintptr_t)UPDATE_HB_ACCOUNT_INFO);
555 LnnFsmPostMessage(fsm, EVENT_HB_UPDATE_SEND_INFO, (void *)(uintptr_t)UPDATE_HB_NETWORK_INFO);
556 }
557
OnProcessSendOnce(FsmStateMachine * fsm,int32_t msgType,void * para)558 static int32_t OnProcessSendOnce(FsmStateMachine *fsm, int32_t msgType, void *para)
559 {
560 (void)msgType;
561 int32_t ret = SOFTBUS_NETWORK_HEARTBEAT_SEND_ERR;
562 LnnHeartbeatFsm *hbFsm = NULL;
563 LnnHeartbeatStrategyManager strategyMgr = {0};
564
565 LnnDumpHbMgrRecvList();
566 LnnDumpHbOnlineNodeList();
567 LnnProcessSendOnceMsgPara *msgPara = (LnnProcessSendOnceMsgPara *)para;
568 if (msgPara == NULL) {
569 LNN_LOGW(LNN_HEART_BEAT, "process send once get invalid para");
570 return SOFTBUS_INVALID_PARAM;
571 }
572 do {
573 if (!CheckHbFsmStateMsgArgs(fsm)) {
574 LNN_LOGW(LNN_HEART_BEAT, "process send once get invalid fsm");
575 break;
576 }
577 hbFsm = TO_HEARTBEAT_FSM(fsm);
578 if (LnnGetHbStrategyManager(&strategyMgr, msgPara->hbType, msgPara->strategyType) != SOFTBUS_OK) {
579 LNN_LOGE(LNN_HEART_BEAT, "process send once get strategy fail");
580 break;
581 }
582 if (strategyMgr.onProcess != NULL) {
583 ret = strategyMgr.onProcess(hbFsm, para);
584 } else {
585 LNN_LOGD(LNN_HEART_BEAT, "process send once get NULL process FUNC");
586 break;
587 }
588 if (ret != SOFTBUS_OK) {
589 LNN_LOGE(LNN_HEART_BEAT, "process send once fail, hbType=%{public}d, strategyType=%{public}d, "
590 "ret=%{public}d", msgPara->hbType, msgPara->strategyType, ret);
591 break;
592 }
593 ret = SOFTBUS_OK;
594 } while (false);
595 SoftBusFree(msgPara);
596 return ret;
597 }
598
ReportSendBroadcastResultEvt(void)599 static void ReportSendBroadcastResultEvt(void)
600 {
601 if (SoftBusRecordDiscoveryResult(SEND_BROADCAST, NULL) != SOFTBUS_OK) {
602 LNN_LOGE(LNN_HEART_BEAT, "report send broadcast result fail");
603 }
604 }
605
OnSendOneHbBegin(FsmStateMachine * fsm,int32_t msgType,void * para)606 static int32_t OnSendOneHbBegin(FsmStateMachine *fsm, int32_t msgType, void *para)
607 {
608 (void)fsm;
609 (void)msgType;
610 int32_t ret = SOFTBUS_NETWORK_HB_SEND_BEGIN_FAILED;
611
612 LnnHeartbeatSendBeginData *custData = (LnnHeartbeatSendBeginData *)para;
613 if (custData == NULL) {
614 LNN_LOGW(LNN_HEART_BEAT, "send once begin get invalid param");
615 return SOFTBUS_INVALID_PARAM;
616 }
617 do {
618 if (LnnHbMediumMgrSendBegin(custData) != SOFTBUS_OK) {
619 LNN_LOGE(LNN_HEART_BEAT, "send once begin to manager fail");
620 LnnCheckDevStatusMsgPara checkMsg = {.hbType = custData->hbType, .hasNetworkId = false};
621 LnnRemoveCheckDevStatusMsg(TO_HEARTBEAT_FSM(fsm), &checkMsg);
622 break;
623 }
624 ret = SOFTBUS_OK;
625 ReportSendBroadcastResultEvt();
626 } while (false);
627 SoftBusFree(custData);
628 return ret;
629 }
630
OnSendOneHbEnd(FsmStateMachine * fsm,int32_t msgType,void * para)631 static int32_t OnSendOneHbEnd(FsmStateMachine *fsm, int32_t msgType, void *para)
632 {
633 (void)msgType;
634 int32_t ret = SOFTBUS_NETWORK_HB_SEND_END_FAILED;
635
636 LnnHeartbeatSendEndData *custData = (LnnHeartbeatSendEndData *)para;
637 if (custData == NULL) {
638 LNN_LOGW(LNN_HEART_BEAT, "send once end get invalid para");
639 return SOFTBUS_INVALID_PARAM;
640 }
641 do {
642 if (!CheckHbFsmStateMsgArgs(fsm)) {
643 LNN_LOGW(LNN_HEART_BEAT, "send once end get invalid fsm");
644 break;
645 }
646 if (LnnHbMediumMgrSendEnd(custData) != SOFTBUS_OK) {
647 LNN_LOGE(LNN_HEART_BEAT, "send once end to manager fail");
648 (void)LnnFsmRemoveMessage(fsm, EVENT_HB_SEND_ONE_END);
649 (void)LnnFsmRemoveMessage(fsm, EVENT_HB_CHECK_DEV_STATUS);
650 break;
651 }
652 ret = SOFTBUS_OK;
653 } while (false);
654 SoftBusFree(custData);
655 return ret;
656 }
657
OnStartHbProcess(FsmStateMachine * fsm,int32_t msgType,void * para)658 static int32_t OnStartHbProcess(FsmStateMachine *fsm, int32_t msgType, void *para)
659 {
660 (void)msgType;
661 (void)para;
662 if (!CheckHbFsmStateMsgArgs(fsm)) {
663 LNN_LOGW(LNN_HEART_BEAT, "start process get invalid fsm");
664 return SOFTBUS_INVALID_PARAM;
665 }
666 LnnHeartbeatFsm *hbFsm = TO_HEARTBEAT_FSM(fsm);
667 LnnFsmPostMessage(&hbFsm->fsm, EVENT_HB_AS_MASTER_NODE, NULL);
668 if (LnnIsHeartbeatEnable(HEARTBEAT_TYPE_BLE_V0)) {
669 /* Send once ble v0 heartbeat to recovery ble network. */
670 LnnStartHbByTypeAndStrategy(HEARTBEAT_TYPE_BLE_V0 | HEARTBEAT_TYPE_BLE_V3, STRATEGY_HB_SEND_SINGLE, false);
671 }
672 return SOFTBUS_OK;
673 }
674
OnReStartHbProcess(FsmStateMachine * fsm,int32_t msgType,void * para)675 static int32_t OnReStartHbProcess(FsmStateMachine *fsm, int32_t msgType, void *para)
676 {
677 (void)msgType;
678 (void)para;
679
680 if (!CheckHbFsmStateMsgArgs(fsm)) {
681 LNN_LOGW(LNN_HEART_BEAT, "start process get invalid fsm");
682 return SOFTBUS_INVALID_PARAM;
683 }
684 if (LnnIsHeartbeatEnable(HEARTBEAT_TYPE_BLE_V0)) {
685 LnnStartHbByTypeAndStrategy(HEARTBEAT_TYPE_BLE_V0 | HEARTBEAT_TYPE_BLE_V3, STRATEGY_HB_SEND_SINGLE, false);
686 }
687 return SOFTBUS_OK;
688 }
689
OnStopHbByType(FsmStateMachine * fsm,int32_t msgType,void * para)690 static int32_t OnStopHbByType(FsmStateMachine *fsm, int32_t msgType, void *para)
691 {
692 (void)msgType;
693 int32_t ret = SOFTBUS_NETWORK_HB_STOP_PROCESS_FAIL;
694
695 LnnHeartbeatType *hbType = (LnnHeartbeatType *)para;
696 if (hbType == NULL) {
697 LNN_LOGW(LNN_HEART_BEAT, "stop specific get invalid para");
698 return SOFTBUS_INVALID_PARAM;
699 }
700 do {
701 if (!CheckHbFsmStateMsgArgs(fsm)) {
702 LNN_LOGW(LNN_HEART_BEAT, "stop specific get invalid fsm");
703 break;
704 }
705 if (LnnHbMediumMgrStop(hbType) != SOFTBUS_OK) {
706 LNN_LOGE(LNN_HEART_BEAT, "stop specific manager fail");
707 break;
708 }
709 LnnHeartbeatFsm *hbFsm = TO_HEARTBEAT_FSM(fsm);
710 if ((*hbType & HEARTBEAT_TYPE_BLE_V0) != 0) {
711 LnnFsmRemoveMessage(&hbFsm->fsm, EVENT_HB_CHECK_DEV_STATUS);
712 LnnRemoveProcessSendOnceMsg(hbFsm, HEARTBEAT_TYPE_BLE_V0, STRATEGY_HB_SEND_SINGLE);
713 LnnRemoveProcessSendOnceMsg(hbFsm, HEARTBEAT_TYPE_BLE_V0, STRATEGY_HB_SEND_ADJUSTABLE_PERIOD);
714 LnnFsmRemoveMessage(&hbFsm->fsm, EVENT_HB_SEND_ONE_BEGIN);
715 LnnFsmRemoveMessage(&hbFsm->fsm, EVENT_HB_SEND_ONE_END);
716 }
717 if ((*hbType & HEARTBEAT_TYPE_SLE) != 0) {
718 LnnFsmRemoveMessage(&hbFsm->fsm, EVENT_HB_PROCESS_SEND_ONCE);
719 LnnRemoveProcessSendOnceMsg(hbFsm, HEARTBEAT_TYPE_SLE, STRATEGY_HB_SEND_FIXED_PERIOD);
720 }
721 ret = SOFTBUS_OK;
722 } while (false);
723 SoftBusFree(hbType);
724 return ret;
725 }
726
OnSetMediumParam(FsmStateMachine * fsm,int32_t msgType,void * para)727 static int32_t OnSetMediumParam(FsmStateMachine *fsm, int32_t msgType, void *para)
728 {
729 (void)fsm;
730 (void)msgType;
731 int32_t ret;
732
733 if (para == NULL) {
734 LNN_LOGE(LNN_HEART_BEAT, "set medium param get invalid param");
735 return SOFTBUS_INVALID_PARAM;
736 }
737 ret = LnnHbMediumMgrSetParam(para);
738 if (ret != SOFTBUS_OK) {
739 LNN_LOGE(LNN_HEART_BEAT, "set medium param process fail, ret=%{public}d", ret);
740 }
741 SoftBusFree(para);
742 return ret;
743 }
744
OnUpdateSendInfo(FsmStateMachine * fsm,int32_t msgType,void * para)745 static int32_t OnUpdateSendInfo(FsmStateMachine *fsm, int32_t msgType, void *para)
746 {
747 (void)fsm;
748 (void)msgType;
749
750 return LnnHbMediumMgrUpdateSendInfo((LnnHeartbeatUpdateInfoType)(uintptr_t)para);
751 }
752
TryAsMasterNodeNextLoop(FsmStateMachine * fsm)753 static void TryAsMasterNodeNextLoop(FsmStateMachine *fsm)
754 {
755 uint64_t delayMillis;
756 GearMode mode;
757 (void)memset_s(&mode, sizeof(GearMode), 0, sizeof(GearMode));
758 if (LnnGetGearModeBySpecificType(&mode, NULL, HEARTBEAT_TYPE_BLE_V1) != SOFTBUS_OK) {
759 LNN_LOGE(LNN_HEART_BEAT, "try as master node get gearmode fail");
760 return;
761 }
762 delayMillis = (uint64_t)mode.cycle * HB_TIME_FACTOR + HB_NOTIFY_MASTER_NODE_DELAY_LEN;
763 if (LnnFsmPostMessageDelay(fsm, EVENT_HB_AS_MASTER_NODE, NULL, delayMillis) != SOFTBUS_OK) {
764 LNN_LOGE(LNN_HEART_BEAT, "try as master node post msg fail");
765 return;
766 }
767 LNN_LOGI(LNN_HEART_BEAT, "try as master node in delay time. delayMillis=%{public}" PRIu64 " msec", delayMillis);
768 }
769
OnTransHbFsmState(FsmStateMachine * fsm,int32_t msgType,void * para)770 static int32_t OnTransHbFsmState(FsmStateMachine *fsm, int32_t msgType, void *para)
771 {
772 (void)para;
773 LnnHeartbeatState nextState;
774 LnnHeartbeatFsm *hbFsm = NULL;
775
776 if (!CheckHbFsmStateMsgArgs(fsm)) {
777 LNN_LOGW(LNN_HEART_BEAT, "args check fail");
778 return SOFTBUS_INVALID_PARAM;
779 }
780 switch (msgType) {
781 case EVENT_HB_AS_MASTER_NODE:
782 nextState = STATE_HB_MASTER_NODE_INDEX;
783 LnnFsmRemoveMessage(fsm, EVENT_HB_AS_NORMAL_NODE);
784 break;
785 case EVENT_HB_AS_NORMAL_NODE:
786 nextState = STATE_HB_NORMAL_NODE_INDEX;
787 LnnFsmRemoveMessage(fsm, EVENT_HB_AS_MASTER_NODE);
788 TryAsMasterNodeNextLoop(fsm);
789 break;
790 case EVENT_HB_IN_NONE_STATE:
791 nextState = STATE_HB_NONE_INDEX;
792 break;
793 default:
794 LNN_LOGE(LNN_HEART_BEAT, "process transact state get invalid msgType");
795 return SOFTBUS_NETWORK_HB_TRANSACT_PROCESS_FAIL;
796 }
797 hbFsm = TO_HEARTBEAT_FSM(fsm);
798 if (hbFsm->state == nextState) {
799 return SOFTBUS_OK;
800 }
801 if (LnnFsmTransactState(fsm, g_hbState + nextState) != SOFTBUS_OK) {
802 LNN_LOGE(LNN_HEART_BEAT, "process transact fsm state fail");
803 return SOFTBUS_NETWORK_HB_TRANSACT_PROCESS_FAIL;
804 }
805 return SOFTBUS_OK;
806 }
807
ProcOfflineWithoutSoftbus(const char * networkId,ConnectionAddrType addrType)808 static bool ProcOfflineWithoutSoftbus(const char *networkId, ConnectionAddrType addrType)
809 {
810 NodeInfo node;
811 (void)memset_s(&node, sizeof(NodeInfo), 0, sizeof(NodeInfo));
812 if (LnnGetRemoteNodeInfoById(networkId, CATEGORY_NETWORK_ID, &node) != SOFTBUS_OK) {
813 LNN_LOGW(LNN_HEART_BEAT, "can not find node");
814 return false;
815 }
816 LNN_LOGD(LNN_HEART_BEAT, "node deviceTypeId=%{public}d", node.deviceInfo.deviceTypeId);
817 if (node.deviceInfo.deviceTypeId == TYPE_PC_ID &&
818 strcmp(node.networkId, node.deviceInfo.deviceUdid) == 0) {
819 LNN_LOGI(LNN_HEART_BEAT, "remove node because lost heartbeat");
820 DeleteFromProfile(node.deviceInfo.deviceUdid);
821 LnnRemoveNode(node.deviceInfo.deviceUdid);
822 return true;
823 }
824 return false;
825 }
826
ProcessLostHeartbeat(const char * networkId,LnnHeartbeatType type,bool isWakeUp)827 static int32_t ProcessLostHeartbeat(const char *networkId, LnnHeartbeatType type, bool isWakeUp)
828 {
829 ConnectionAddrType addrType = LnnConvertHbTypeToConnAddrType(type);
830 char udidHash[HB_SHORT_UDID_HASH_HEX_LEN + 1] = {0};
831 char *anonyNetworkId = NULL;
832 LNN_CHECK_AND_RETURN_RET_LOGE(networkId != NULL, SOFTBUS_INVALID_PARAM, LNN_HEART_BEAT, "networkId is null");
833
834 if (ProcOfflineWithoutSoftbus(networkId, addrType)) {
835 LNN_LOGI(LNN_HEART_BEAT, "proc offline, that device online without softbus");
836 return SOFTBUS_OK;
837 }
838 if (!LnnGetOnlineStateById(networkId, CATEGORY_NETWORK_ID)) {
839 Anonymize(networkId, &anonyNetworkId);
840 LNN_LOGI(LNN_HEART_BEAT, "process dev lost is offline, networkId=%{public}s",
841 AnonymizeWrapper(anonyNetworkId));
842 AnonymizeFree(anonyNetworkId);
843 return SOFTBUS_OK;
844 }
845 if (LnnHasActiveConnection(networkId, addrType) || QueryControlPlaneNodeValidPacked(networkId) == SOFTBUS_OK) {
846 Anonymize(networkId, &anonyNetworkId);
847 LNN_LOGD(LNN_HEART_BEAT, "process dev lost in next period, networkId=%{public}s",
848 AnonymizeWrapper(anonyNetworkId));
849 AnonymizeFree(anonyNetworkId);
850 if (LnnOfflineTimingByHeartbeat(networkId, addrType) != SOFTBUS_OK) {
851 LNN_LOGE(LNN_HEART_BEAT, "process dev lost start new offline timing err");
852 return SOFTBUS_NETWORK_HB_START_STRATEGY_FAIL;
853 }
854 return SOFTBUS_OK;
855 }
856 if (LnnIsSupportBurstFeature(networkId) && !(isWakeUp || type == HEARTBEAT_TYPE_BLE_V0)) {
857 LNN_LOGI(LNN_HEART_BEAT, "is support burst and is not wakeup or V0, don't check");
858 return SOFTBUS_OK;
859 }
860 const char *udid = LnnConvertDLidToUdid(networkId, CATEGORY_NETWORK_ID);
861 (void)LnnGenerateHexStringHash((const unsigned char *)udid, udidHash, HB_SHORT_UDID_HASH_HEX_LEN);
862 char *anonyUdidHash = NULL;
863 Anonymize(udidHash, &anonyUdidHash);
864 Anonymize(networkId, &anonyNetworkId);
865 LNN_LOGI(LNN_HEART_BEAT, "process dev lost, udidHash=%{public}s, networkId=%{public}s",
866 AnonymizeWrapper(anonyUdidHash), AnonymizeWrapper(anonyNetworkId));
867 AnonymizeFree(anonyNetworkId);
868 AnonymizeFree(anonyUdidHash);
869 if (LnnRequestLeaveSpecific(networkId, addrType) != SOFTBUS_OK) {
870 LNN_LOGE(LNN_HEART_BEAT, "process dev lost send request to NetBuilder fail");
871 return SOFTBUS_NETWORK_REQ_LEAVE_LNN_FAIL;
872 }
873 return SOFTBUS_OK;
874 }
875
IsTimestampExceedLimit(uint64_t nowTime,uint64_t oldTimeStamp,LnnHeartbeatType hbType,uint64_t delayTime)876 static bool IsTimestampExceedLimit(uint64_t nowTime, uint64_t oldTimeStamp, LnnHeartbeatType hbType, uint64_t delayTime)
877 {
878 GearMode mode;
879 (void)memset_s(&mode, sizeof(GearMode), 0, sizeof(GearMode));
880 uint64_t offlineToleranceLen;
881
882 switch (hbType) {
883 case HEARTBEAT_TYPE_BLE_V0:
884 if ((nowTime - oldTimeStamp) <= delayTime) {
885 return false;
886 }
887 break;
888 case HEARTBEAT_TYPE_BLE_V1:
889 if (LnnGetGearModeBySpecificType(&mode, NULL, HEARTBEAT_TYPE_BLE_V1) != SOFTBUS_OK) {
890 LNN_LOGE(LNN_HEART_BEAT, "is timestamp exceed limit get Gearmode err");
891 return false;
892 }
893 /* BLE_V1 have priority over BLE_V0 */
894 offlineToleranceLen = (uint64_t)mode.cycle * HB_TIME_FACTOR + HB_CHECK_DELAY_LEN + HB_SEND_ONCE_LEN;
895 if (nowTime - oldTimeStamp <= offlineToleranceLen) {
896 return false;
897 }
898 break;
899 default:
900 break;
901 }
902 return true;
903 }
904
CheckDevStatusByNetworkId(LnnHeartbeatFsm * hbFsm,const char * networkId,LnnCheckDevStatusMsgPara * msgPara)905 static void CheckDevStatusByNetworkId(LnnHeartbeatFsm *hbFsm, const char *networkId, LnnCheckDevStatusMsgPara *msgPara)
906 {
907 uint64_t oldTimeStamp;
908 DiscoveryType discType;
909 char *anonyNetworkId = NULL;
910 LnnHeartbeatType hbType = msgPara->hbType;
911 NodeInfo nodeInfo;
912 SoftBusSysTime times = {0};
913 (void)memset_s(&nodeInfo, sizeof(NodeInfo), 0, sizeof(NodeInfo));
914 if (LnnGetRemoteNodeInfoById(networkId, CATEGORY_NETWORK_ID, &nodeInfo) != SOFTBUS_OK) {
915 LNN_LOGE(LNN_HEART_BEAT, "check dev status get nodeInfo fail");
916 return;
917 }
918 discType = LnnConvAddrTypeToDiscType(LnnConvertHbTypeToConnAddrType(hbType));
919 if (!LnnHasDiscoveryType(&nodeInfo, discType)) {
920 Anonymize(networkId, &anonyNetworkId);
921 LNN_LOGE(LNN_HEART_BEAT,
922 "check dev status doesn't have discType. networkId=%{public}s, discType=%{public}d",
923 AnonymizeWrapper(anonyNetworkId), discType);
924 AnonymizeFree(anonyNetworkId);
925 return;
926 }
927 if (LnnGetDLHeartbeatTimestamp(networkId, &oldTimeStamp) != SOFTBUS_OK) {
928 Anonymize(networkId, &anonyNetworkId);
929 LNN_LOGE(LNN_HEART_BEAT, "get timestamp err, networkId=%{public}s", AnonymizeWrapper(anonyNetworkId));
930 AnonymizeFree(anonyNetworkId);
931 return;
932 }
933 SoftBusGetTime(×);
934 uint64_t nowTime = (uint64_t)times.sec * HB_TIME_FACTOR + (uint64_t)times.usec / HB_TIME_FACTOR;
935 if (!IsTimestampExceedLimit(nowTime, oldTimeStamp, hbType, msgPara->checkDelay)) {
936 Anonymize(networkId, &anonyNetworkId);
937 LNN_LOGD(LNN_HEART_BEAT, "receive heartbeat in time, networkId=%{public}s, nowTime=%{public}" PRIu64 ", "
938 "oldTimeStamp=%{public}" PRIu64, AnonymizeWrapper(anonyNetworkId), nowTime, oldTimeStamp);
939 AnonymizeFree(anonyNetworkId);
940 return;
941 }
942 Anonymize(networkId, &anonyNetworkId);
943 LNN_LOGI(LNN_HEART_BEAT, "notify node lost heartbeat, networkId=%{public}s, oldTimeStamp=%{public}" PRIu64 ", "
944 "nowTime=%{public}" PRIu64, AnonymizeWrapper(anonyNetworkId), oldTimeStamp, nowTime);
945 if (LnnStopOfflineTimingStrategy(networkId, LnnConvertHbTypeToConnAddrType(hbType)) != SOFTBUS_OK) {
946 LNN_LOGE(LNN_HEART_BEAT, "check dev status stop offline timing fail");
947 AnonymizeFree(anonyNetworkId);
948 return;
949 }
950 if (ProcessLostHeartbeat(networkId, hbType, msgPara->isWakeUp) != SOFTBUS_OK) {
951 LNN_LOGE(LNN_HEART_BEAT, "process dev lost err, networkId=%{public}s", AnonymizeWrapper(anonyNetworkId));
952 }
953 AnonymizeFree(anonyNetworkId);
954 }
955
CheckDevStatusForScreenOff(LnnHeartbeatFsm * hbFsm,const char * networkId,LnnHeartbeatType hbType,uint64_t nowTime)956 static void CheckDevStatusForScreenOff(LnnHeartbeatFsm *hbFsm, const char *networkId,
957 LnnHeartbeatType hbType, uint64_t nowTime)
958 {
959 (void)hbFsm;
960 uint64_t oldTimeStamp;
961 char *anonyNetworkId = NULL;
962 if (LnnHasActiveConnection(networkId, LnnConvertHbTypeToConnAddrType(hbType)) ||
963 QueryControlPlaneNodeValidPacked(networkId) == SOFTBUS_OK) {
964 Anonymize(networkId, &anonyNetworkId);
965 LNN_LOGD(LNN_HEART_BEAT, "process screen off dev lost in next period, networkId=%{public}s",
966 AnonymizeWrapper(anonyNetworkId));
967 if (LnnStartScreenChangeOfflineTiming(networkId, LnnConvertHbTypeToConnAddrType(hbType)) != SOFTBUS_OK) {
968 LNN_LOGE(LNN_HEART_BEAT, "process screen off dev lost start new offline timing err");
969 }
970 AnonymizeFree(anonyNetworkId);
971 return;
972 }
973 if (LnnGetDLHeartbeatTimestamp(networkId, &oldTimeStamp) != SOFTBUS_OK) {
974 LNN_LOGE(LNN_HEART_BEAT, "hb check dev status get timestamp err");
975 return;
976 }
977 if ((nowTime - oldTimeStamp) <= (HB_OFFLINE_PERIOD * HB_OFFLINE_TIME)) {
978 LNN_LOGI(LNN_HEART_BEAT, "hb check dev status , receive heartbeat in 2 * period time");
979 if (GetScreenState() == SOFTBUS_SCREEN_OFF && LnnStartScreenChangeOfflineTiming(networkId,
980 LnnConvertHbTypeToConnAddrType(hbType)) != SOFTBUS_OK) {
981 LNN_LOGI(LNN_HEART_BEAT, "post next period screen off offline check msg");
982 }
983 return;
984 }
985 Anonymize(networkId, &anonyNetworkId);
986 LNN_LOGW(LNN_HEART_BEAT, "the screen has been closed for more than 2 cycles, will offline, networkId=%{public}s",
987 AnonymizeWrapper(anonyNetworkId));
988 if (!LnnGetOnlineStateById(networkId, CATEGORY_NETWORK_ID)) {
989 LNN_LOGI(LNN_HEART_BEAT, "process dev lost is offline, networkId=%{public}s",
990 AnonymizeWrapper(anonyNetworkId));
991 AnonymizeFree(anonyNetworkId);
992 return;
993 }
994 AnonymizeFree(anonyNetworkId);
995 if (LnnIsLocalSupportBurstFeature()) {
996 LNN_LOGI(LNN_HEART_BEAT, "local device support lp, no need offline");
997 return;
998 }
999 if (LnnRequestLeaveSpecific(networkId, LnnConvertHbTypeToConnAddrType(hbType)) != SOFTBUS_OK) {
1000 LNN_LOGE(LNN_HEART_BEAT, "process dev lost send request to NetBuilder fail");
1001 return;
1002 }
1003 }
1004
OnCheckDevStatus(FsmStateMachine * fsm,int32_t msgType,void * para)1005 static int32_t OnCheckDevStatus(FsmStateMachine *fsm, int32_t msgType, void *para)
1006 {
1007 (void)msgType;
1008 int32_t ret = SOFTBUS_NETWORK_HB_CHECK_DEV_STATUS_ERROR;
1009
1010 LnnCheckDevStatusMsgPara *msgPara = (LnnCheckDevStatusMsgPara *)para;
1011 if (msgPara == NULL) {
1012 LNN_LOGE(LNN_HEART_BEAT, "check dev status get invalid para");
1013 return SOFTBUS_INVALID_PARAM;
1014 }
1015 do {
1016 if (GetScreenState() == SOFTBUS_SCREEN_OFF) {
1017 ret = SOFTBUS_OK;
1018 LNN_LOGI(LNN_HEART_BEAT, "screen if off, dont need hb check");
1019 break;
1020 }
1021 if (!CheckHbFsmStateMsgArgs(fsm)) {
1022 LNN_LOGE(LNN_HEART_BEAT, "check dev status get invalid fsm");
1023 break;
1024 }
1025 LnnHeartbeatFsm *hbFsm = TO_HEARTBEAT_FSM(fsm);
1026 if (msgPara->hasNetworkId) {
1027 CheckDevStatusByNetworkId(hbFsm, msgPara->networkId, msgPara);
1028 ret = SOFTBUS_OK;
1029 break;
1030 }
1031
1032 int32_t i, infoNum;
1033 NodeBasicInfo *info = NULL;
1034 if (LnnGetAllOnlineNodeInfo(&info, &infoNum) != SOFTBUS_OK) {
1035 break;
1036 }
1037 if (info == NULL || infoNum == 0) {
1038 LNN_LOGD(LNN_HEART_BEAT, "check dev status get none online node");
1039 ret = SOFTBUS_OK;
1040 break;
1041 }
1042 for (i = 0; i < infoNum; ++i) {
1043 if (LnnIsLSANode(&info[i])) {
1044 continue;
1045 }
1046 CheckDevStatusByNetworkId(hbFsm, info[i].networkId, msgPara);
1047 }
1048 SoftBusFree(info);
1049 ret = SOFTBUS_OK;
1050 } while (false);
1051 SoftBusFree(msgPara);
1052 return ret;
1053 }
1054
OnScreeOffCheckDevStatus(FsmStateMachine * fsm,int32_t msgType,void * para)1055 static int32_t OnScreeOffCheckDevStatus(FsmStateMachine *fsm, int32_t msgType, void *para)
1056 {
1057 (void)msgType;
1058 int32_t ret = SOFTBUS_OK;
1059 uint64_t nowTime;
1060 SoftBusSysTime times = {0};
1061 LnnCheckDevStatusMsgPara *msgPara = (LnnCheckDevStatusMsgPara *)para;
1062 if (msgPara == NULL) {
1063 LNN_LOGE(LNN_HEART_BEAT, "check dev status get invalid para");
1064 return SOFTBUS_INVALID_PARAM;
1065 }
1066 do {
1067 SoftBusGetTime(×);
1068 nowTime = (uint64_t)times.sec * HB_TIME_FACTOR + (uint64_t)times.usec / HB_TIME_FACTOR;
1069 if (!CheckHbFsmStateMsgArgs(fsm)) {
1070 LNN_LOGE(LNN_HEART_BEAT, "check dev status get invalid fsm");
1071 ret = SOFTBUS_NETWORK_HB_CHECK_DEV_STATUS_ERROR;
1072 break;
1073 }
1074 LnnHeartbeatFsm *hbFsm = TO_HEARTBEAT_FSM(fsm);
1075 if (msgPara->hasNetworkId) {
1076 CheckDevStatusForScreenOff(hbFsm, msgPara->networkId, msgPara->hbType, nowTime);
1077 break;
1078 }
1079 int32_t infoNum;
1080 NodeBasicInfo *info = NULL;
1081 if (LnnGetAllOnlineNodeInfo(&info, &infoNum) != SOFTBUS_OK) {
1082 LNN_LOGE(LNN_HEART_BEAT, "check dev status get online node info fail");
1083 ret = SOFTBUS_NETWORK_GET_ALL_NODE_INFO_ERR;
1084 break;
1085 }
1086 if (info == NULL || infoNum == 0) {
1087 LNN_LOGI(LNN_HEART_BEAT, "check dev status get none online node");
1088 break;
1089 }
1090 for (int32_t i = 0; i < infoNum; ++i) {
1091 if (LnnIsLSANode(&info[i])) {
1092 continue;
1093 }
1094 CheckDevStatusForScreenOff(hbFsm, info[i].networkId, msgPara->hbType, nowTime);
1095 }
1096 SoftBusFree(info);
1097 } while (false);
1098 SoftBusFree(msgPara);
1099 return ret;
1100 }
1101
CheckSleDevStatus(LnnHeartbeatFsm * hbFsm,const char * networkId,LnnHeartbeatType hbType,uint64_t nowTime)1102 static void CheckSleDevStatus(LnnHeartbeatFsm *hbFsm, const char *networkId,
1103 LnnHeartbeatType hbType, uint64_t nowTime)
1104 {
1105 (void)hbFsm;
1106 uint64_t oldTimeStamp;
1107 char *anonyNetworkId = NULL;
1108 Anonymize(networkId, &anonyNetworkId);
1109 NodeInfo nodeInfo;
1110 (void)memset_s(&nodeInfo, sizeof(NodeInfo), 0, sizeof(NodeInfo));
1111 if (LnnGetRemoteNodeInfoById(networkId, CATEGORY_NETWORK_ID, &nodeInfo) != SOFTBUS_OK) {
1112 LNN_LOGE(LNN_HEART_BEAT, "get nodeInfo fail, networkId=%{public}s", AnonymizeWrapper(anonyNetworkId));
1113 AnonymizeFree(anonyNetworkId);
1114 return;
1115 }
1116 if (QueryControlPlaneNodeValidPacked(networkId) != SOFTBUS_OK) {
1117 LNN_LOGD(LNN_HEART_BEAT, "target dev not in spark group, networkId=%{public}s",
1118 AnonymizeWrapper(anonyNetworkId));
1119 AnonymizeFree(anonyNetworkId);
1120 return;
1121 }
1122 if (!LnnHasDiscoveryType(&nodeInfo, LnnConvAddrTypeToDiscType(CONNECTION_ADDR_BLE))) {
1123 LNN_LOGD(LNN_HEART_BEAT, "target dev ble not online, networkId=%{public}s",
1124 AnonymizeWrapper(anonyNetworkId));
1125 AnonymizeFree(anonyNetworkId);
1126 return;
1127 }
1128 if (LnnGetDLSleHbTimestamp(networkId, &oldTimeStamp) != SOFTBUS_OK) {
1129 LNN_LOGE(LNN_HEART_BEAT, "get sle time stamp fail, networkId=%{public}s",
1130 AnonymizeWrapper(anonyNetworkId));
1131 AnonymizeFree(anonyNetworkId);
1132 return;
1133 }
1134 if (nowTime - oldTimeStamp < HB_SLE_OFFLINE_TIME) {
1135 LNN_LOGE(LNN_HEART_BEAT, "sle check dev status in period time, networkId=%{public}s",
1136 AnonymizeWrapper(anonyNetworkId));
1137 AnonymizeFree(anonyNetworkId);
1138 (void)LnnStartSleOfflineTimingStrategy(networkId);
1139 return;
1140 }
1141 LNN_LOGI(LNN_HEART_BEAT, "notify node lost sle heartbeat, networkId=%{public}s,"
1142 "timestamp=%{public}" PRIu64 "->%{public}" PRIu64 "",
1143 AnonymizeWrapper(anonyNetworkId), oldTimeStamp, nowTime);
1144 if (LnnRequestLeaveSpecific(networkId, CONNECTION_ADDR_BLE) != SOFTBUS_OK) {
1145 LNN_LOGE(LNN_HEART_BEAT, "process dev lost sle hb, send request to NetBuilder fail, networkId=%{public}s",
1146 AnonymizeWrapper(anonyNetworkId));
1147 }
1148 AnonymizeFree(anonyNetworkId);
1149 }
1150
OnCheckSleDevStatus(FsmStateMachine * fsm,int32_t msgType,void * para)1151 static int32_t OnCheckSleDevStatus(FsmStateMachine *fsm, int32_t msgType, void *para)
1152 {
1153 (void)msgType;
1154 int32_t ret = SOFTBUS_OK;
1155 uint64_t nowTime;
1156 SoftBusSysTime times = {0};
1157 LnnCheckDevStatusMsgPara *msgPara = (LnnCheckDevStatusMsgPara *)para;
1158 if (msgPara == NULL) {
1159 LNN_LOGE(LNN_HEART_BEAT, "check sle dev status get invalid para");
1160 return SOFTBUS_INVALID_PARAM;
1161 }
1162 do {
1163 SoftBusGetTime(×);
1164 nowTime = (uint64_t)times.sec * HB_TIME_FACTOR + (uint64_t)times.usec / HB_TIME_FACTOR;
1165 if (!CheckHbFsmStateMsgArgs(fsm)) {
1166 LNN_LOGE(LNN_HEART_BEAT, "check sle dev status get invalid fsm");
1167 ret = SOFTBUS_NETWORK_HB_CHECK_DEV_STATUS_ERROR;
1168 break;
1169 }
1170 LnnHeartbeatFsm *hbFsm = TO_HEARTBEAT_FSM(fsm);
1171 if (!msgPara->hasNetworkId) {
1172 LNN_LOGE(LNN_HEART_BEAT, "check sle dev status no need handle");
1173 break;
1174 }
1175 CheckSleDevStatus(hbFsm, msgPara->networkId, msgPara->hbType, nowTime);
1176 } while (false);
1177 SoftBusFree(msgPara);
1178 return ret;
1179 }
1180
LnnDestroyHeartbeatFsm(LnnHeartbeatFsm * hbFsm)1181 void LnnDestroyHeartbeatFsm(LnnHeartbeatFsm *hbFsm)
1182 {
1183 if (hbFsm == NULL) {
1184 return;
1185 }
1186 // Destroy by LnnDeinitLnnLooper
1187 LNN_LOGI(LNN_HEART_BEAT, "destroy heartbeat fsmId=%{public}u", hbFsm->id);
1188 SoftBusFree(hbFsm);
1189 }
1190
DeinitHbFsmCallback(FsmStateMachine * fsm)1191 static void DeinitHbFsmCallback(FsmStateMachine *fsm)
1192 {
1193 LnnHeartbeatFsm *hbFsm = NULL;
1194
1195 LNN_LOGI(LNN_HEART_BEAT, "fsm deinit callback enter");
1196 if (!CheckHbFsmStateMsgArgs(fsm)) {
1197 LNN_LOGE(LNN_HEART_BEAT, "fsm deinit callback error");
1198 return;
1199 }
1200 hbFsm = TO_HEARTBEAT_FSM(fsm);
1201 LnnDestroyHeartbeatFsm(hbFsm);
1202 }
1203
InitHeartbeatFsm(LnnHeartbeatFsm * hbFsm)1204 static int32_t InitHeartbeatFsm(LnnHeartbeatFsm *hbFsm)
1205 {
1206 if (sprintf_s(hbFsm->fsmName, HB_FSM_NAME_LEN, "LnnHbFsm-%u", hbFsm->id) == -1) {
1207 LNN_LOGE(LNN_HEART_BEAT, "format fsm name fail");
1208 return SOFTBUS_SPRINTF_ERR;
1209 }
1210 SoftBusLooper *looper = GetLooper(LOOP_TYPE_LNN);
1211 if (looper == NULL) {
1212 LNN_LOGE(LNN_HEART_BEAT, "create looper fail");
1213 return SOFTBUS_LOOPER_ERR;
1214 }
1215 if (LnnFsmInit(&hbFsm->fsm, looper, hbFsm->fsmName, DeinitHbFsmCallback) != SOFTBUS_OK) {
1216 LNN_LOGE(LNN_HEART_BEAT, "init lnn fsm fail");
1217 return SOFTBUS_NETWORK_FSM_INIT_FAIL;
1218 }
1219 for (int32_t i = 0; i < STATE_HB_INDEX_MAX; ++i) {
1220 LnnFsmAddState(&hbFsm->fsm, &g_hbState[i]);
1221 }
1222 return SOFTBUS_OK;
1223 }
1224
LnnCreateHeartbeatFsm(void)1225 LnnHeartbeatFsm *LnnCreateHeartbeatFsm(void)
1226 {
1227 LnnHeartbeatFsm *hbFsm = NULL;
1228
1229 hbFsm = (LnnHeartbeatFsm *)SoftBusCalloc(sizeof(LnnHeartbeatFsm));
1230 if (hbFsm == NULL) {
1231 LNN_LOGE(LNN_HEART_BEAT, "malloc fsm fail");
1232 return NULL;
1233 }
1234 ListInit(&hbFsm->node);
1235 if (InitHeartbeatFsm(hbFsm) != SOFTBUS_OK) {
1236 LNN_LOGE(LNN_HEART_BEAT, "init fsm fail");
1237 LnnDestroyHeartbeatFsm(hbFsm);
1238 return NULL;
1239 }
1240 hbFsm->state = STATE_HB_NONE_INDEX;
1241 return hbFsm;
1242 }
1243
LnnStartHeartbeatFsm(LnnHeartbeatFsm * hbFsm)1244 int32_t LnnStartHeartbeatFsm(LnnHeartbeatFsm *hbFsm)
1245 {
1246 if (hbFsm == NULL) {
1247 LNN_LOGE(LNN_HEART_BEAT, "start fsm is null");
1248 return SOFTBUS_INVALID_PARAM;
1249 }
1250 if (LnnFsmStart(&hbFsm->fsm, g_hbState + STATE_HB_NONE_INDEX) != SOFTBUS_OK) {
1251 LNN_LOGE(LNN_HEART_BEAT, "start fsm failed. fsmId=%{public}u", hbFsm->id);
1252 return SOFTBUS_NETWORK_FSM_START_FAIL;
1253 }
1254 LNN_LOGI(LNN_HEART_BEAT, "fsm is starting. fsmId=%{public}u", hbFsm->id);
1255 return SOFTBUS_OK;
1256 }
1257
LnnStopHeartbeatFsm(LnnHeartbeatFsm * hbFsm)1258 int32_t LnnStopHeartbeatFsm(LnnHeartbeatFsm *hbFsm)
1259 {
1260 if (hbFsm == NULL) {
1261 LNN_LOGE(LNN_HEART_BEAT, "stop fsm is null");
1262 return SOFTBUS_INVALID_PARAM;
1263 }
1264 if (LnnFsmStop(&hbFsm->fsm) != SOFTBUS_OK) {
1265 LNN_LOGE(LNN_HEART_BEAT, "stop fsm failed. fsmId=%{public}u", hbFsm->id);
1266 return SOFTBUS_NETWORK_FSM_STOP_FAIL;
1267 }
1268 return LnnFsmDeinit(&hbFsm->fsm);
1269 }
1270
LnnPostNextSendOnceMsgToHbFsm(LnnHeartbeatFsm * hbFsm,const LnnProcessSendOnceMsgPara * para,uint64_t delayMillis)1271 int32_t LnnPostNextSendOnceMsgToHbFsm(LnnHeartbeatFsm *hbFsm, const LnnProcessSendOnceMsgPara *para,
1272 uint64_t delayMillis)
1273 {
1274 LnnProcessSendOnceMsgPara *dupPara = NULL;
1275
1276 if (hbFsm == NULL || para == NULL) {
1277 LNN_LOGE(LNN_HEART_BEAT, "post next loop msg get invalid param");
1278 return SOFTBUS_INVALID_PARAM;
1279 }
1280 dupPara = (LnnProcessSendOnceMsgPara *)SoftBusCalloc(sizeof(LnnProcessSendOnceMsgPara));
1281 if (dupPara == NULL) {
1282 LNN_LOGE(LNN_HEART_BEAT, "post next loop msg malloc dupPara fail");
1283 return SOFTBUS_MALLOC_ERR;
1284 }
1285 *dupPara = *para;
1286 if (LnnFsmPostMessageDelay(&hbFsm->fsm, EVENT_HB_PROCESS_SEND_ONCE, (void *)dupPara, delayMillis) != SOFTBUS_OK) {
1287 LNN_LOGE(LNN_HEART_BEAT, "post next loop msg to hbFsm fail");
1288 SoftBusFree(dupPara);
1289 return SOFTBUS_NETWORK_POST_MSG_FAIL;
1290 }
1291 LnnNotifyHBRepeat();
1292 LNN_LOGD(LNN_HEART_BEAT, "post next loop msg, delayMillis=%{public}" PRIu64, delayMillis);
1293 return SOFTBUS_OK;
1294 }
1295
LnnPostSendBeginMsgToHbFsm(LnnHeartbeatFsm * hbFsm,LnnHeartbeatType type,bool wakeupFlag,LnnProcessSendOnceMsgPara * msgPara,uint64_t delayMillis)1296 int32_t LnnPostSendBeginMsgToHbFsm(LnnHeartbeatFsm *hbFsm, LnnHeartbeatType type,
1297 bool wakeupFlag, LnnProcessSendOnceMsgPara *msgPara, uint64_t delayMillis)
1298 {
1299 LNN_LOGD(LNN_HEART_BEAT, "LnnPostSendBeginMsgToHbFsm enter hbType=%{public}d, isSyncData=%{public}d",
1300 type, msgPara->isSyncData);
1301 LnnHeartbeatSendBeginData *custData = NULL;
1302
1303 if (hbFsm == NULL) {
1304 LNN_LOGE(LNN_HEART_BEAT, "post send begin msg get invalid param");
1305 return SOFTBUS_INVALID_PARAM;
1306 }
1307 custData = (LnnHeartbeatSendBeginData *)SoftBusCalloc(sizeof(LnnHeartbeatSendBeginData));
1308 if (custData == NULL) {
1309 LNN_LOGE(LNN_HEART_BEAT, "new hbType obj msg malloc err");
1310 return SOFTBUS_MALLOC_ERR;
1311 }
1312 custData->hbType = type;
1313 custData->wakeupFlag = wakeupFlag;
1314 custData->isRelay = msgPara->isRelay;
1315 custData->isSyncData = msgPara->isSyncData;
1316 custData->isNeedRestart = msgPara->isNeedRestart;
1317 custData->hasScanRsp = msgPara->hasScanRsp;
1318 custData->isFirstBegin = msgPara->isFirstBegin;
1319 custData->isFast = msgPara->isFast;
1320 custData->isDirectBoardcast = msgPara->isDirectBoardcast;
1321 custData->isMsdpRange = msgPara->isMsdpRange;
1322 if (strcpy_s(custData->networkId, NETWORK_ID_BUF_LEN, msgPara->networkId) != EOK) {
1323 LNN_LOGE(LNN_HEART_BEAT, "cpy networkId fail");
1324 SoftBusFree(custData);
1325 return SOFTBUS_STRCPY_ERR;
1326 }
1327 if (LnnFsmPostMessageDelay(&hbFsm->fsm, EVENT_HB_SEND_ONE_BEGIN, (void *)custData, delayMillis) != SOFTBUS_OK) {
1328 LNN_LOGE(LNN_HEART_BEAT, "post send begin msg to hbFsm fail");
1329 SoftBusFree(custData);
1330 return SOFTBUS_NETWORK_POST_MSG_DELAY_FAIL;
1331 }
1332 return SOFTBUS_OK;
1333 }
1334
LnnPostSendEndMsgToHbFsm(LnnHeartbeatFsm * hbFsm,LnnHeartbeatSendEndData * custData,uint64_t delayMillis)1335 int32_t LnnPostSendEndMsgToHbFsm(LnnHeartbeatFsm *hbFsm, LnnHeartbeatSendEndData *custData,
1336 uint64_t delayMillis)
1337 {
1338 LnnHeartbeatSendEndData *dupData = NULL;
1339 if (hbFsm == NULL || custData == NULL) {
1340 LNN_LOGE(LNN_HEART_BEAT, "post send end msg get invalid param");
1341 return SOFTBUS_INVALID_PARAM;
1342 }
1343 LNN_LOGD(LNN_HEART_BEAT, "LnnPostSendEndMsgToHbFsm enter hbType=%{public}d", custData->hbType);
1344 dupData = (LnnHeartbeatSendEndData *)SoftBusCalloc(sizeof(LnnHeartbeatSendEndData));
1345 if (dupData == NULL) {
1346 LNN_LOGE(LNN_HEART_BEAT, "post send end msg malloc error");
1347 return SOFTBUS_MALLOC_ERR;
1348 }
1349 *dupData = *custData;
1350 if (LnnFsmPostMessageDelay(&hbFsm->fsm, EVENT_HB_SEND_ONE_END, (void *)dupData, delayMillis) != SOFTBUS_OK) {
1351 LNN_LOGE(LNN_HEART_BEAT, "post send end msg to hbFsm fail");
1352 SoftBusFree(dupData);
1353 return SOFTBUS_NETWORK_POST_MSG_DELAY_FAIL;
1354 }
1355 return SOFTBUS_OK;
1356 }
1357
LnnPostStartMsgToHbFsm(LnnHeartbeatFsm * hbFsm,uint64_t delayMillis)1358 int32_t LnnPostStartMsgToHbFsm(LnnHeartbeatFsm *hbFsm, uint64_t delayMillis)
1359 {
1360 if (hbFsm == NULL) {
1361 LNN_LOGE(LNN_HEART_BEAT, "post start msg get invalid param");
1362 return SOFTBUS_INVALID_PARAM;
1363 }
1364 return LnnFsmPostMessageDelay(&hbFsm->fsm, EVENT_HB_START_PROCESS, NULL, delayMillis);
1365 }
1366
LnnPostStopMsgToHbFsm(LnnHeartbeatFsm * hbFsm,LnnHeartbeatType type)1367 int32_t LnnPostStopMsgToHbFsm(LnnHeartbeatFsm *hbFsm, LnnHeartbeatType type)
1368 {
1369 LnnHeartbeatType *newType = NULL;
1370
1371 if (hbFsm == NULL) {
1372 LNN_LOGE(LNN_HEART_BEAT, "post stop msg get invalid param");
1373 return SOFTBUS_INVALID_PARAM;
1374 }
1375 newType = (LnnHeartbeatType *)SoftBusCalloc(sizeof(LnnHeartbeatType));
1376 if (newType == NULL) {
1377 LNN_LOGE(LNN_HEART_BEAT, "post stop msg malloc newType err");
1378 return SOFTBUS_MALLOC_ERR;
1379 }
1380 *newType = type;
1381 if (LnnFsmPostMessage(&hbFsm->fsm, EVENT_HB_STOP_SPECIFIC, (void *)newType) != SOFTBUS_OK) {
1382 LNN_LOGE(LNN_HEART_BEAT, "post stop msg to hbFsm fail");
1383 SoftBusFree(newType);
1384 return SOFTBUS_NETWORK_POST_MSG_FAIL;
1385 }
1386 return SOFTBUS_OK;
1387 }
1388
LnnPostTransStateMsgToHbFsm(LnnHeartbeatFsm * hbFsm,LnnHeartbeatEventType evtType)1389 int32_t LnnPostTransStateMsgToHbFsm(LnnHeartbeatFsm *hbFsm, LnnHeartbeatEventType evtType)
1390 {
1391 if (hbFsm == NULL) {
1392 LNN_LOGE(LNN_HEART_BEAT, "post trans state msg get invalid hbFsm");
1393 return SOFTBUS_INVALID_PARAM;
1394 }
1395 if (evtType != EVENT_HB_AS_MASTER_NODE && evtType != EVENT_HB_AS_NORMAL_NODE && evtType != EVENT_HB_IN_NONE_STATE) {
1396 LNN_LOGE(LNN_HEART_BEAT, "post trans state msg get invalid evtType");
1397 return SOFTBUS_INVALID_PARAM;
1398 }
1399 return LnnFsmPostMessage(&hbFsm->fsm, evtType, NULL);
1400 }
1401
LnnPostSetMediumParamMsgToHbFsm(LnnHeartbeatFsm * hbFsm,const LnnHeartbeatMediumParam * para)1402 int32_t LnnPostSetMediumParamMsgToHbFsm(LnnHeartbeatFsm *hbFsm, const LnnHeartbeatMediumParam *para)
1403 {
1404 LnnHeartbeatMediumParam *dupPara = NULL;
1405
1406 if (hbFsm == NULL || para == NULL) {
1407 LNN_LOGE(LNN_HEART_BEAT, "post set medium param msg get invalid param");
1408 return SOFTBUS_INVALID_PARAM;
1409 }
1410 dupPara = (LnnHeartbeatMediumParam *)SoftBusCalloc(sizeof(LnnHeartbeatMediumParam));
1411 if (dupPara == NULL) {
1412 LNN_LOGE(LNN_HEART_BEAT, "post set medium param msg malloc msgPara fail");
1413 return SOFTBUS_MALLOC_ERR;
1414 }
1415 *dupPara = *para;
1416 if (LnnFsmPostMessage(&hbFsm->fsm, EVENT_HB_SET_MEDIUM_PARAM, (void *)dupPara) != SOFTBUS_OK) {
1417 LNN_LOGE(LNN_HEART_BEAT, "post set medium param msg to hbFsm fail");
1418 SoftBusFree(dupPara);
1419 return SOFTBUS_NETWORK_POST_MSG_FAIL;
1420 }
1421 return SOFTBUS_OK;
1422 }
1423
LnnPostCheckDevStatusMsgToHbFsm(LnnHeartbeatFsm * hbFsm,const LnnCheckDevStatusMsgPara * para,uint64_t delayMillis)1424 int32_t LnnPostCheckDevStatusMsgToHbFsm(LnnHeartbeatFsm *hbFsm, const LnnCheckDevStatusMsgPara *para,
1425 uint64_t delayMillis)
1426 {
1427 LnnCheckDevStatusMsgPara *dupPara = NULL;
1428
1429 if (hbFsm == NULL) {
1430 LNN_LOGE(LNN_HEART_BEAT, "post check dev status msg get invalid param");
1431 return SOFTBUS_INVALID_PARAM;
1432 }
1433 if (para == NULL) {
1434 return LnnFsmPostMessageDelay(&hbFsm->fsm, EVENT_HB_CHECK_DEV_STATUS, NULL, delayMillis);
1435 }
1436 dupPara = (LnnCheckDevStatusMsgPara *)SoftBusCalloc(sizeof(LnnCheckDevStatusMsgPara));
1437 if (dupPara == NULL) {
1438 LNN_LOGE(LNN_HEART_BEAT, "post check dev status msg malloc msgPara fail");
1439 return SOFTBUS_MALLOC_ERR;
1440 }
1441 if (memcpy_s(dupPara, sizeof(LnnCheckDevStatusMsgPara), para, sizeof(LnnCheckDevStatusMsgPara)) != EOK) {
1442 LNN_LOGE(LNN_HEART_BEAT, "post check dev status msg memcpy_s msgPara fail");
1443 SoftBusFree(dupPara);
1444 return SOFTBUS_MEM_ERR;
1445 }
1446 dupPara->checkDelay = delayMillis;
1447 if (LnnFsmPostMessageDelay(&hbFsm->fsm, EVENT_HB_CHECK_DEV_STATUS, (void *)dupPara, delayMillis) != SOFTBUS_OK) {
1448 LNN_LOGE(LNN_HEART_BEAT, "post check dev status msg to hbFsm fail");
1449 SoftBusFree(dupPara);
1450 return SOFTBUS_NETWORK_POST_MSG_DELAY_FAIL;
1451 }
1452 return SOFTBUS_OK;
1453 }
1454
LnnPostScreenOffCheckDevMsgToHbFsm(LnnHeartbeatFsm * hbFsm,const LnnCheckDevStatusMsgPara * para,uint64_t delayMillis)1455 int32_t LnnPostScreenOffCheckDevMsgToHbFsm(LnnHeartbeatFsm *hbFsm,
1456 const LnnCheckDevStatusMsgPara *para, uint64_t delayMillis)
1457 {
1458 LnnCheckDevStatusMsgPara *dupPara = NULL;
1459
1460 if (hbFsm == NULL) {
1461 LNN_LOGE(LNN_HEART_BEAT, "post check dev status msg get invalid param");
1462 return SOFTBUS_INVALID_PARAM;
1463 }
1464 if (para == NULL) {
1465 return LnnFsmPostMessageDelay(&hbFsm->fsm, EVENT_HB_SCREEN_OFF_CHECK_STATUS, NULL, delayMillis);
1466 }
1467 dupPara = (LnnCheckDevStatusMsgPara *)SoftBusCalloc(sizeof(LnnCheckDevStatusMsgPara));
1468 if (dupPara == NULL) {
1469 LNN_LOGE(LNN_HEART_BEAT, "post check dev status msg malloc msgPara fail");
1470 return SOFTBUS_MALLOC_ERR;
1471 }
1472 if (memcpy_s(dupPara, sizeof(LnnCheckDevStatusMsgPara), para, sizeof(LnnCheckDevStatusMsgPara)) != EOK) {
1473 LNN_LOGE(LNN_HEART_BEAT, "post check dev status msg memcpy_s msgPara fail");
1474 SoftBusFree(dupPara);
1475 return SOFTBUS_MEM_ERR;
1476 }
1477 if (LnnFsmPostMessageDelay(&hbFsm->fsm, EVENT_HB_SCREEN_OFF_CHECK_STATUS,
1478 (void *)dupPara, delayMillis) != SOFTBUS_OK) {
1479 LNN_LOGE(LNN_HEART_BEAT, "post check dev status msg to hbFsm fail");
1480 SoftBusFree(dupPara);
1481 return SOFTBUS_NETWORK_POST_MSG_DELAY_FAIL;
1482 }
1483 return SOFTBUS_OK;
1484 }
1485
LnnPostUpdateSendInfoMsgToHbFsm(LnnHeartbeatFsm * hbFsm,LnnHeartbeatUpdateInfoType type)1486 int32_t LnnPostUpdateSendInfoMsgToHbFsm(LnnHeartbeatFsm *hbFsm, LnnHeartbeatUpdateInfoType type)
1487 {
1488 if (hbFsm == NULL || type <= UPDATE_HB_INFO_MIN || type >= UPDATE_HB_MAX_INFO) {
1489 LNN_LOGE(LNN_HEART_BEAT, "post update info msg get invalid param");
1490 return SOFTBUS_INVALID_PARAM;
1491 }
1492 return LnnFsmPostMessage(&hbFsm->fsm, EVENT_HB_UPDATE_SEND_INFO, (void *)(uintptr_t)type);
1493 }
1494
LnnPostSleCheckDevStatusMsgToHbFsm(LnnHeartbeatFsm * hbFsm,const LnnCheckDevStatusMsgPara * para,uint64_t delayMillis)1495 int32_t LnnPostSleCheckDevStatusMsgToHbFsm(
1496 LnnHeartbeatFsm *hbFsm, const LnnCheckDevStatusMsgPara *para, uint64_t delayMillis)
1497 {
1498 if (hbFsm == NULL || para == NULL) {
1499 LNN_LOGE(LNN_HEART_BEAT, "post sle check dev status msg get invalid param");
1500 return SOFTBUS_INVALID_PARAM;
1501 }
1502 LnnCheckDevStatusMsgPara *dupPara = (LnnCheckDevStatusMsgPara *)SoftBusCalloc(sizeof(LnnCheckDevStatusMsgPara));
1503 LNN_CHECK_AND_RETURN_RET_LOGE(dupPara != NULL, SOFTBUS_MALLOC_ERR, LNN_HEART_BEAT, "malloc fail");
1504 if (memcpy_s(dupPara, sizeof(LnnCheckDevStatusMsgPara), para, sizeof(LnnCheckDevStatusMsgPara)) != EOK) {
1505 LNN_LOGE(LNN_HEART_BEAT, "post sle check dev status msg memcpy_s msgPara fail");
1506 SoftBusFree(dupPara);
1507 return SOFTBUS_MEM_ERR;
1508 }
1509 if (LnnFsmPostMessageDelay(
1510 &hbFsm->fsm, EVENT_HB_CHECK_SLE_DEV_STATUS, (void *)dupPara, delayMillis) != SOFTBUS_OK) {
1511 LNN_LOGE(LNN_HEART_BEAT, "post sle check dev status msg to hbFsm fail");
1512 SoftBusFree(dupPara);
1513 return SOFTBUS_NETWORK_POST_MSG_DELAY_FAIL;
1514 }
1515 return SOFTBUS_OK;
1516 }
1517