!984 heartbeat sync code

Merge pull request !984 from jeosif/master
This commit is contained in:
openharmony_ci 2022-02-22 11:21:42 +00:00 committed by Gitee
commit b76e21dd1b
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
18 changed files with 615 additions and 593 deletions

View File

@ -29,7 +29,7 @@ declare_args() {
dsoftbus_standard_feature_lnn_net = true
dsoftbus_standard_feature_lnn_time_sync = false
enable_lnn_heartbeat = false
dsoftbus_standard_feature_lnn_heartbeat = false
dsoftbus_standard_feature_lnn_meta_node = false
dsoftbus_standard_feature_qos = false

View File

@ -29,7 +29,7 @@ declare_args() {
dsoftbus_standard_feature_lnn_net = true
dsoftbus_standard_feature_lnn_time_sync = true
enable_lnn_heartbeat = false
dsoftbus_standard_feature_lnn_heartbeat = false
dsoftbus_standard_feature_lnn_meta_node = false
dsoftbus_standard_feature_qos = false

View File

@ -29,7 +29,7 @@ declare_args() {
dsoftbus_standard_feature_lnn_net = true
dsoftbus_standard_feature_lnn_time_sync = true
enable_lnn_heartbeat = true
dsoftbus_standard_feature_lnn_heartbeat = true
dsoftbus_standard_feature_lnn_meta_node = true
dsoftbus_standard_feature_qos = true

View File

@ -23,8 +23,8 @@ extern "C" {
#endif
int32_t LnnInitBleHeartbeat(LnnHeartbeatImplCallback *callback);
int32_t LnnOnceBleBeatBegin(void);
int32_t LnnOnceBleBeatEnd(void);
int32_t LnnOnceBleHbBegin(void);
int32_t LnnOnceBleHbEnd(void);
int32_t LnnStopBleHeartbeat(void);
int32_t LnnDeinitBleHeartbeat(void);

View File

@ -27,42 +27,42 @@ extern "C" {
#define SHORT_USRID_HASH_HEX_LEN 4
typedef enum {
STATE_NONE_BEAT_INDEX = 0,
STATE_BEAT_NORMAL_NODE_INDEX,
STATE_BEAT_MASTER_NODE_INDEX,
STATE_BEAT_INDEX_MAX,
STATE_HB_NONE_INDEX = 0,
STATE_HB_NORMAL_NODE_INDEX,
STATE_HB_MASTER_NODE_INDEX,
STATE_HB_INDEX_MAX,
} LnnHeartbeatState;
typedef enum {
EVENT_BEAT_ENTER = 0,
EVENT_BEAT_START,
EVENT_BEAT_ONCE_ENTER,
EVENT_BEAT_DEVICE_LOST,
EVENT_BEAT_AS_MASTER_NODE,
EVENT_BEAT_AS_NORMAL_NODE = 5,
EVENT_BEAT_MONITOR_DEV,
EVENT_BEAT_REPEAT_CYCLE,
EVENT_BEAT_ONCE_OUT,
EVENT_BEAT_STOP,
EVENT_BEAT_TIMEOUT = 10,
EVENT_BEAT_EXIT,
EVENT_BEAT_MAX,
EVENT_HB_ENTER = 0,
EVENT_HB_START,
EVENT_HB_ONCE_BEGIN,
EVENT_HB_DEVICE_LOST,
EVENT_HB_AS_MASTER_NODE,
EVENT_HB_AS_NORMAL_NODE = 5,
EVENT_HB_CHECK_DEV,
EVENT_HB_REPEAT_CYCLE,
EVENT_HB_ONCE_END,
EVENT_HB_STOP,
EVENT_HB_TIMEOUT = 10,
EVENT_HB_EXIT,
EVENT_HB_MAX,
} LnnHeartbeatEventType;
int32_t LnnPostMsgToBeatFsm(int32_t eventType, void *obj);
int32_t LnnPostDelayMsgToBeatFsm(int32_t eventType, void *obj, uint64_t delayMillis);
int32_t LnnRemoveBeatFsmMsg(int32_t eventType, uint64_t para, void *obj);
int32_t LnnPostMsgToHbFsm(int32_t eventType, void *obj);
int32_t LnnPostDelayMsgToHbFsm(int32_t eventType, void *obj, uint64_t delayMillis);
int32_t LnnRemoveHbFsmMsg(int32_t eventType, uint64_t para, void *obj);
int32_t LnnHeartbeatRelayBeat(ConnectionAddrType type);
int32_t LnnHeartbeatMonitorDevInfo(ConnectionAddrType type, uint64_t delayMillis);
int32_t LnnHeartbeatAsNormalNode(void);
int32_t LnnHeartbeatNodeOffline(const char *networkId, ConnectionAddrType addrType, uint64_t delayMillis);
int32_t LnnHbRelayToMaster(ConnectionAddrType type);
int32_t LnnHbCheckDevStatus(ConnectionAddrType type, uint64_t delayMillis);
int32_t LnnHbAsNormalNode(void);
int32_t LnnHbProcessDeviceLost(const char *networkId, ConnectionAddrType addrType, uint64_t delayMillis);
int32_t LnnHeartbeatFsmStart(int32_t beatStateIndex, uint64_t delayMillis);
int32_t LnnHeartbeatFsmStop(uint64_t delayMillis);
int32_t LnnHbFsmStart(int32_t stateIndex, uint64_t delayMillis);
int32_t LnnHbFsmStop(uint64_t delayMillis);
int32_t LnnHeartbeatFsmInit(void);
void LnnHeartbeatFsmDeinit(void);
int32_t LnnHbFsmInit(void);
void LnnHbFsmDeinit(void);
#ifdef __cplusplus
}

View File

@ -23,12 +23,12 @@
extern "C" {
#endif
#define BEAT_SHA_HASH_LEN 32
#define BEAT_USER_ID_LEN 65
#define HB_SHA_HASH_LEN 32
#define HB_USER_ID_LEN 65
typedef enum {
LNN_BEAT_IMPL_TYPE_BLE = 0,
LNN_BEAT_IMPL_TYPE_MAX,
HB_IMPL_TYPE_BLE = 0,
HB_IMPL_TYPE_MAX,
} LnnHeartbeatImplType;
typedef struct {
@ -37,14 +37,14 @@ typedef struct {
int32_t (*onUpdateDev)(DeviceInfo *device, int32_t weight, int32_t localMasterWeight);
} LnnHeartbeatImplCallback;
int32_t LnnHeartbeatMgrInit(void);
int32_t LnnHeartbeatMgrStart(void);
int32_t LnnHeartbeatMgrStopAdv(void);
int32_t LnnHeartbeatMgrStop(void);
void LnnHeartbeatMgrDeinit(void);
int32_t LnnHbMgrInit(void);
int32_t LnnHbMgrOneCycleBegin(void);
int32_t LnnHbMgrOneCycleEnd(void);
int32_t LnnHbMgrStop(void);
void LnnHbMgrDeinit(void);
void LnnDumpBeatMgrUpdateList(void);
void LnnDumpBeatOnlineNodeList(void);
void LnnDumpHbMgrUpdateList(void);
void LnnDumpHbOnlineNodeList(void);
#ifdef __cplusplus
}

View File

@ -24,12 +24,12 @@
extern "C" {
#endif
#define HEARTBEAT_TIME_FACTOR (1000LL)
#define HEARTBEAT_ENABLE_DELAY_LEN (20 * HEARTBEAT_TIME_FACTOR)
#define HEARTBEAT_TOCK_TIME_LEN (10 * HEARTBEAT_TIME_FACTOR)
#define HEARTBEAT_MONITOR_DELAY_LEN (10 * HEARTBEAT_TIME_FACTOR + HEARTBEAT_TOCK_TIME_LEN)
#define HEARTBEAT_MANAGER_TIMEOUT_LEN (5 * HEARTBEAT_TIME_FACTOR + HEARTBEAT_TOCK_TIME_LEN)
#define HEARTBEAT_UPDATE_TIME_PRECISION (HEARTBEAT_ENABLE_DELAY_LEN - HEARTBEAT_TOCK_TIME_LEN)
#define HB_TIME_FACTOR (1000LL)
#define HB_ONE_CYCLE_LEN (10 * HB_TIME_FACTOR)
#define HB_ONE_CYCLE_TIMEOUT_LEN (5 * HB_TIME_FACTOR + HB_ONE_CYCLE_LEN)
#define HB_CHECK_DELAY_LEN (10 * HB_TIME_FACTOR + HB_ONE_CYCLE_LEN)
#define HB_ENABLE_DELAY_LEN (20 * HB_TIME_FACTOR)
#define HB_UPDATE_INTERVAL_LEN (HB_ENABLE_DELAY_LEN - HB_ONE_CYCLE_LEN)
typedef enum {
/**< Heartbeat cycle ( in sec ). */
@ -76,7 +76,7 @@ int32_t LnnOfflineTimingByHeartbeat(const char *networkId, ConnectionAddrType ad
int32_t LnnNotifyMasterNodeChanged(const char *masterUdid, int32_t weight);
int32_t LnnStartHeartbeatDelay(void);
void LnnStopHeartbeat(void);
void LnnStopHeartbeatNow(void);
int32_t LnnInitHeartbeat(void);
void LnnDeinitHeartbeat(void);

View File

@ -25,13 +25,13 @@ int32_t LnnInitBleHeartbeat(LnnHeartbeatImplCallback *callback)
return SOFTBUS_OK;
}
int32_t LnnOnceBleBeatBegin(void)
int32_t LnnOnceBleHbBegin(void)
{
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "ble heartbeat stub impl beat once");
return SOFTBUS_NOT_IMPLEMENT;
}
int32_t LnnOnceBleBeatEnd(void)
int32_t LnnOnceBleHbEnd(void)
{
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "ble heartbeat stub impl beat end");
return SOFTBUS_NOT_IMPLEMENT;

View File

@ -50,71 +50,71 @@ typedef struct {
static SoftBusHandler g_beatHandler = {0};
static int32_t g_curentState = -1;
static int32_t OnTryAsMasterNode(const SoftBusMessage *msg);
static int32_t OnBeatRepeatCycle(const SoftBusMessage *msg);
static int32_t onBeatMasterNodeEnter(const SoftBusMessage *msg);
static int32_t onBeatMasterNodeExit(const SoftBusMessage *msg);
static int32_t OnCheckDeviceStatus(const SoftBusMessage *msg);
static int32_t OnDetectDeviceLost(const SoftBusMessage *msg);
static int32_t OnElectAsMasterNode(const SoftBusMessage *msg);
static int32_t OnElectAsNormalNode(const SoftBusMessage *msg);
static int32_t OnStartHeartbeat(const SoftBusMessage *msg);
static int32_t OnBeatOnceEnter(const SoftBusMessage *msg);
static int32_t OnBeatOnceOut(const SoftBusMessage *msg);
static int32_t OnStopHeartbeat(const SoftBusMessage *msg);
static int32_t OnBeatDeviceLost(const SoftBusMessage *msg);
static int32_t OnMonitorDeviceStatus(const SoftBusMessage *msg);
static int32_t OnBeatTimeOut(const SoftBusMessage *msg);
static int32_t OnHeartbeatStart(const SoftBusMessage *msg);
static int32_t OnHeartbeatStop(const SoftBusMessage *msg);
static int32_t OnMasterStateEnter(const SoftBusMessage *msg);
static int32_t OnMasterStateExit(const SoftBusMessage *msg);
static int32_t OnOneCycleBegin(const SoftBusMessage *msg);
static int32_t OnOneCycleEnd(const SoftBusMessage *msg);
static int32_t OnOneCycleTimeout(const SoftBusMessage *msg);
static int32_t OnRepeatCycle(const SoftBusMessage *msg);
static int32_t OnTryAsMasterNode(const SoftBusMessage *msg);
static LnnHeartbeatEventHandler g_noneBeatStateHandler[] = {
{EVENT_BEAT_ENTER, OnStopHeartbeat},
{EVENT_BEAT_START, OnStartHeartbeat},
{EVENT_BEAT_EXIT, NULL}
static LnnHeartbeatEventHandler g_noneHbStateHandler[] = {
{EVENT_HB_ENTER, OnHeartbeatStop},
{EVENT_HB_START, OnHeartbeatStart},
{EVENT_HB_EXIT, NULL}
};
static LnnHeartbeatEventHandler g_beatNormalNodeStateHandler[] = {
{EVENT_BEAT_ENTER, OnTryAsMasterNode},
{EVENT_BEAT_START, OnStartHeartbeat},
{EVENT_BEAT_ONCE_ENTER, OnBeatOnceEnter},
{EVENT_BEAT_DEVICE_LOST, OnBeatDeviceLost},
{EVENT_BEAT_MONITOR_DEV, OnMonitorDeviceStatus},
{EVENT_BEAT_AS_MASTER_NODE, OnElectAsMasterNode},
{EVENT_BEAT_AS_NORMAL_NODE, OnElectAsNormalNode},
{EVENT_BEAT_ONCE_OUT, OnBeatOnceOut},
{EVENT_BEAT_STOP, OnStopHeartbeat},
{EVENT_BEAT_TIMEOUT, OnBeatTimeOut},
{EVENT_BEAT_EXIT, NULL}
static LnnHeartbeatEventHandler g_normalNodeStateHandler[] = {
{EVENT_HB_ENTER, OnTryAsMasterNode},
{EVENT_HB_START, OnHeartbeatStart},
{EVENT_HB_ONCE_BEGIN, OnOneCycleBegin},
{EVENT_HB_DEVICE_LOST, OnDetectDeviceLost},
{EVENT_HB_CHECK_DEV, OnCheckDeviceStatus},
{EVENT_HB_AS_MASTER_NODE, OnElectAsMasterNode},
{EVENT_HB_AS_NORMAL_NODE, OnElectAsNormalNode},
{EVENT_HB_ONCE_END, OnOneCycleEnd},
{EVENT_HB_STOP, OnHeartbeatStop},
{EVENT_HB_TIMEOUT, OnOneCycleTimeout},
{EVENT_HB_EXIT, NULL}
};
static LnnHeartbeatEventHandler g_beatMasterNodeStateHandler[] = {
{EVENT_BEAT_ENTER, onBeatMasterNodeEnter},
{EVENT_BEAT_START, OnStartHeartbeat},
{EVENT_BEAT_ONCE_ENTER, OnBeatOnceEnter},
{EVENT_BEAT_DEVICE_LOST, OnBeatDeviceLost},
{EVENT_BEAT_MONITOR_DEV, OnMonitorDeviceStatus},
{EVENT_BEAT_REPEAT_CYCLE, OnBeatRepeatCycle},
{EVENT_BEAT_AS_MASTER_NODE, OnElectAsMasterNode},
{EVENT_BEAT_AS_NORMAL_NODE, OnElectAsNormalNode},
{EVENT_BEAT_ONCE_OUT, OnBeatOnceOut},
{EVENT_BEAT_STOP, OnStopHeartbeat},
{EVENT_BEAT_TIMEOUT, OnBeatTimeOut},
{EVENT_BEAT_EXIT, onBeatMasterNodeExit}
static LnnHeartbeatEventHandler g_masterNodeStateHandler[] = {
{EVENT_HB_ENTER, OnMasterStateEnter},
{EVENT_HB_START, OnHeartbeatStart},
{EVENT_HB_ONCE_BEGIN, OnOneCycleBegin},
{EVENT_HB_DEVICE_LOST, OnDetectDeviceLost},
{EVENT_HB_CHECK_DEV, OnCheckDeviceStatus},
{EVENT_HB_REPEAT_CYCLE, OnRepeatCycle},
{EVENT_HB_AS_MASTER_NODE, OnElectAsMasterNode},
{EVENT_HB_AS_NORMAL_NODE, OnElectAsNormalNode},
{EVENT_HB_ONCE_END, OnOneCycleEnd},
{EVENT_HB_STOP, OnHeartbeatStop},
{EVENT_HB_TIMEOUT, OnOneCycleTimeout},
{EVENT_HB_EXIT, OnMasterStateExit}
};
static LnnHeartbeatStateHandler g_beatStatHandler[] = {
[STATE_NONE_BEAT_INDEX] = {
.eventNum = sizeof(g_noneBeatStateHandler) / sizeof(LnnHeartbeatEventHandler),
.eventHandler = g_noneBeatStateHandler,
[STATE_HB_NONE_INDEX] = {
.eventNum = sizeof(g_noneHbStateHandler) / sizeof(LnnHeartbeatEventHandler),
.eventHandler = g_noneHbStateHandler,
},
[STATE_BEAT_NORMAL_NODE_INDEX] = {
.eventNum = sizeof(g_beatNormalNodeStateHandler) / sizeof(LnnHeartbeatEventHandler),
.eventHandler = g_beatNormalNodeStateHandler,
[STATE_HB_NORMAL_NODE_INDEX] = {
.eventNum = sizeof(g_normalNodeStateHandler) / sizeof(LnnHeartbeatEventHandler),
.eventHandler = g_normalNodeStateHandler,
},
[STATE_BEAT_MASTER_NODE_INDEX] = {
.eventNum = sizeof(g_beatMasterNodeStateHandler) / sizeof(LnnHeartbeatEventHandler),
.eventHandler = g_beatMasterNodeStateHandler,
[STATE_HB_MASTER_NODE_INDEX] = {
.eventNum = sizeof(g_masterNodeStateHandler) / sizeof(LnnHeartbeatEventHandler),
.eventHandler = g_masterNodeStateHandler,
}
};
static void FreeBeatHandlerMsg(SoftBusMessage *msg)
static void FreeHbHandlerMsg(SoftBusMessage *msg)
{
if (msg != NULL) {
if (msg->obj != NULL) {
@ -124,108 +124,106 @@ static void FreeBeatHandlerMsg(SoftBusMessage *msg)
}
}
static SoftBusMessage *CreateBeatHandlerMsg(int32_t what, uint64_t arg1, uint64_t arg2, void *obj)
static SoftBusMessage *CreateHbHandlerMsg(int32_t what, uint64_t arg1, uint64_t arg2, void *obj)
{
SoftBusMessage *msg = (SoftBusMessage *)SoftBusCalloc(sizeof(SoftBusMessage));
if (msg == NULL) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "beat create handler msg fail");
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB create msg calloc fail, what=%d", what);
return NULL;
}
msg->what = what;
msg->arg1 = arg1;
msg->arg2 = arg2;
msg->handler = &g_beatHandler;
msg->FreeMessage = FreeBeatHandlerMsg;
msg->FreeMessage = FreeHbHandlerMsg;
msg->obj = obj;
return msg;
}
static void HeartbeatFsmTransactState(int32_t fromState, int32_t toState, const SoftBusMessage *msg)
static void HbFsmTransactState(int32_t fromState, int32_t toState, const SoftBusMessage *msg)
{
LnnHeartbeatEventHandler *eventHandler = g_beatStatHandler[fromState].eventHandler;
int32_t eventNum = g_beatStatHandler[fromState].eventNum;
if (eventHandler[eventNum - 1].eventType != EVENT_BEAT_EXIT) {
LnnHeartbeatEventHandler *eventHandler = g_beatStatHandler[fromState].eventHandler;
if (eventHandler[eventNum - 1].eventType != EVENT_HB_EXIT) {
return;
}
if ((eventHandler[eventNum - 1].handler != NULL) && (eventHandler[eventNum - 1].handler(msg) < 0)) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "beat FSM process exit err");
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB FSM process exit err, stateIndex=%d", fromState);
return;
}
eventHandler = g_beatStatHandler[toState].eventHandler;
if (eventHandler[0].eventType != EVENT_BEAT_ENTER) {
if (eventHandler[0].eventType != EVENT_HB_ENTER) {
return;
}
if ((eventHandler[0].handler != NULL) && (eventHandler[EVENT_BEAT_ENTER].handler(msg) < 0)) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "beat FSM process enter err");
if ((eventHandler[0].handler != NULL) && (eventHandler[EVENT_HB_ENTER].handler(msg) < 0)) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB FSM process enter err, stateIndex=%d", toState);
return;
}
}
static void HeartbeatMsgHandler(SoftBusMessage *msg)
static void HbMsgHandler(SoftBusMessage *msg)
{
if (msg == NULL) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "beat msg handler invalid param");
return;
}
if (g_curentState < 0 || g_curentState >= STATE_BEAT_INDEX_MAX) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "beat unknow state or not init");
return;
}
int32_t actIdx, eventNum, eventType, nextStatus, ret;
int32_t actIdx, ret;
int32_t eventType = msg->what;
int32_t nextStatus = g_curentState;
int32_t eventNum = g_beatStatHandler[g_curentState].eventNum;
if (msg == NULL) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB msg handler get invalid param");
return;
}
if (g_curentState < 0 || g_curentState >= STATE_HB_INDEX_MAX) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB unknow state or not init yet");
return;
}
eventType = msg->what;
nextStatus = g_curentState;
eventNum = g_beatStatHandler[g_curentState].eventNum;
LnnHeartbeatEventHandler *eventHandler = g_beatStatHandler[g_curentState].eventHandler;
for (actIdx = 0; actIdx < eventNum; ++actIdx) {
if (eventHandler[actIdx].eventType == eventType) {
ret = (eventHandler[actIdx].handler)(msg);
if (ret < 0) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "beat FSM process msg(%d) fail, ret=%d", eventType, ret);
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB FSM process msg(%d) fail, ret=%d", eventType, ret);
return;
}
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "beat FSM process msg(%d) done, ret=%d, nowstatus=%d",
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "HB FSM process msg(%d) done, ret=%d, nowstatus=%d",
eventType, ret, g_curentState);
nextStatus = ret;
break;
}
}
if (actIdx == eventNum) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "no handler what=%d in status=%d", eventType, g_curentState);
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_WARN, "HB no handler what=%d in status=%d", eventType, g_curentState);
}
if (nextStatus != g_curentState) {
HeartbeatFsmTransactState(g_curentState, nextStatus, msg);
HbFsmTransactState(g_curentState, nextStatus, msg);
}
g_curentState = nextStatus;
}
static int32_t PostMsgToBeatHandler(int32_t what, uint64_t arg1, uint64_t arg2, void *obj)
static int32_t PostMsgToHbHandler(int32_t what, uint64_t arg1, uint64_t arg2, void *obj)
{
SoftBusMessage *msg = CreateBeatHandlerMsg(what, arg1, arg2, obj);
SoftBusMessage *msg = CreateHbHandlerMsg(what, arg1, arg2, obj);
if (msg == NULL) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "create softbus beat message fail");
return SOFTBUS_ERR;
}
g_beatHandler.looper->PostMessage(g_beatHandler.looper, msg);
return SOFTBUS_OK;
}
static int32_t PostDelayMsgToBeatHandler(int32_t what, uint64_t arg1, uint64_t arg2, void *obj, uint64_t delayMillis)
static int32_t PostDelayMsgToHbHandler(int32_t what, uint64_t arg1, uint64_t arg2, void *obj, uint64_t delayMillis)
{
SoftBusMessage *msg = CreateBeatHandlerMsg(what, arg1, arg2, obj);
SoftBusMessage *msg = CreateHbHandlerMsg(what, arg1, arg2, obj);
if (msg == NULL) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "create softbus beat delay message fail");
return SOFTBUS_ERR;
}
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "PostDelayMsgToBeatHandler what = %d, delayMillis = %d msec",
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "HB post delay msg, what=%d delayMillis=%llu msec",
what, delayMillis);
g_beatHandler.looper->PostMessageDelay(g_beatHandler.looper, msg, delayMillis);
return SOFTBUS_OK;
}
/* remove message when return 0, else return 1 */
static int32_t RemoveBeatMsgFunc(const SoftBusMessage *msg, void *args)
static int32_t RemoveHbMsgFunc(const SoftBusMessage *msg, void *args)
{
if (msg == NULL || args == NULL) {
return 1;
@ -244,98 +242,105 @@ static int32_t RemoveBeatMsgFunc(const SoftBusMessage *msg, void *args)
return 1;
}
int32_t LnnPostMsgToBeatFsm(int32_t eventType, void *obj)
int32_t LnnPostMsgToHbFsm(int32_t eventType, void *obj)
{
if (eventType < 0 || eventType >= EVENT_BEAT_MAX) {
if (eventType < 0 || eventType >= EVENT_HB_MAX) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB post msg get invalid param, what=%d", eventType);
return SOFTBUS_INVALID_PARAM;
}
return PostMsgToBeatHandler(eventType, 0, 0, obj);
return PostMsgToHbHandler(eventType, 0, 0, obj);
}
int32_t LnnPostDelayMsgToBeatFsm(int32_t eventType, void *obj, uint64_t delayMillis)
int32_t LnnPostDelayMsgToHbFsm(int32_t eventType, void *obj, uint64_t delayMillis)
{
if (eventType < 0 || eventType >= EVENT_BEAT_MAX || delayMillis < 0) {
if (eventType < 0 || eventType >= EVENT_HB_MAX || delayMillis < 0) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB post delay msg get invalid param, what=%d", eventType);
return SOFTBUS_INVALID_PARAM;
}
return PostDelayMsgToBeatHandler(eventType, 0, 0, obj, delayMillis);
return PostDelayMsgToHbHandler(eventType, 0, 0, obj, delayMillis);
}
int32_t LnnRemoveBeatFsmMsg(int32_t eventType, uint64_t para, void *obj)
int32_t LnnRemoveHbFsmMsg(int32_t eventType, uint64_t para, void *obj)
{
if (eventType < 0 || eventType >= EVENT_BEAT_MAX) {
if (eventType < 0 || eventType >= EVENT_HB_MAX) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB remove msg get invalid param, what=%d", eventType);
return SOFTBUS_INVALID_PARAM;
}
SoftBusMessage *msg = CreateBeatHandlerMsg(eventType, 0, para, obj);
SoftBusMessage *msg = CreateHbHandlerMsg(eventType, 0, para, obj);
if (msg == NULL) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "create softbus beat message fail");
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB remove msg create msg fail, what=%d", eventType);
return SOFTBUS_ERR;
}
g_beatHandler.looper->RemoveMessageCustom(g_beatHandler.looper, &g_beatHandler, RemoveBeatMsgFunc, msg);
g_beatHandler.looper->RemoveMessageCustom(g_beatHandler.looper, &g_beatHandler, RemoveHbMsgFunc, msg);
return SOFTBUS_OK;
}
int32_t LnnHeartbeatMonitorDevInfo(ConnectionAddrType type, uint64_t delayMillis)
int32_t LnnHbCheckDevStatus(ConnectionAddrType type, uint64_t delayMillis)
{
return PostDelayMsgToBeatHandler(EVENT_BEAT_MONITOR_DEV, 0, (uint64_t)type, NULL, delayMillis);
return PostDelayMsgToHbHandler(EVENT_HB_CHECK_DEV, 0, (uint64_t)type, NULL, delayMillis);
}
int32_t LnnHeartbeatAsNormalNode(void)
int32_t LnnHbAsNormalNode(void)
{
return PostMsgToBeatHandler(EVENT_BEAT_AS_NORMAL_NODE, 0, 0, NULL);
return PostMsgToHbHandler(EVENT_HB_AS_NORMAL_NODE, 0, 0, NULL);
}
int32_t LnnHeartbeatRelayBeat(ConnectionAddrType type)
int32_t LnnHbRelayToMaster(ConnectionAddrType type)
{
(void)LnnHeartbeatAsNormalNode();
return PostMsgToBeatHandler(EVENT_BEAT_ONCE_ENTER, 0, (uint64_t)type, NULL);
if (LnnHbAsNormalNode() != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB perform as normal node fail");
return SOFTBUS_ERR;
}
return PostMsgToHbHandler(EVENT_HB_ONCE_BEGIN, 0, (uint64_t)type, NULL);
}
int32_t LnnHeartbeatNodeOffline(const char *networkId, ConnectionAddrType addrType, uint64_t delayMillis)
int32_t LnnHbProcessDeviceLost(const char *networkId, ConnectionAddrType addrType, uint64_t delayMillis)
{
if (networkId == NULL || addrType > CONNECTION_ADDR_MAX || delayMillis < 0) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "device networkId is null");
return SOFTBUS_ERR;
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB detect dev lost get invalid param");
return SOFTBUS_INVALID_PARAM;
}
char *tempId = (char *)SoftBusCalloc(NETWORK_ID_BUF_LEN);
if (tempId == NULL) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "beat malloc networkId err");
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB networkId malloc err");
return SOFTBUS_MALLOC_ERR;
}
if (strncpy_s(tempId, NETWORK_ID_BUF_LEN, networkId, strlen(networkId)) != EOK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "beat strncpy_s err");
if (strcpy_s(tempId, NETWORK_ID_BUF_LEN, networkId) != EOK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB networkId strcpy_s err");
SoftBusFree(tempId);
return SOFTBUS_ERR;
}
return PostDelayMsgToBeatHandler(EVENT_BEAT_DEVICE_LOST, 0, (uint64_t)addrType, (void *)tempId, delayMillis);
return PostDelayMsgToHbHandler(EVENT_HB_DEVICE_LOST, 0, (uint64_t)addrType, (void *)tempId, delayMillis);
}
int32_t LnnHeartbeatFsmStart(int32_t beatStateIndex, uint64_t delayMillis)
int32_t LnnHbFsmStart(int32_t stateIndex, uint64_t delayMillis)
{
if (beatStateIndex < 0 || beatStateIndex >= STATE_BEAT_INDEX_MAX) {
if (stateIndex < 0 || stateIndex >= STATE_HB_INDEX_MAX) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB fsm start get invalid param");
return SOFTBUS_INVALID_PARAM;
}
return PostDelayMsgToBeatHandler(EVENT_BEAT_START, (uint64_t)beatStateIndex, 0, NULL, delayMillis);
return PostDelayMsgToHbHandler(EVENT_HB_START, (uint64_t)stateIndex, 0, NULL, delayMillis);
}
int32_t LnnHeartbeatFsmStop(uint64_t delayMillis)
int32_t LnnHbFsmStop(uint64_t delayMillis)
{
return PostDelayMsgToBeatHandler(EVENT_BEAT_STOP, 0, 0, NULL, delayMillis);
return PostDelayMsgToHbHandler(EVENT_HB_STOP, 0, 0, NULL, delayMillis);
}
int32_t LnnHeartbeatFsmInit(void)
int32_t LnnHbFsmInit(void)
{
g_curentState = STATE_NONE_BEAT_INDEX;
g_curentState = STATE_HB_NONE_INDEX;
g_beatHandler.name = "heartbeat_handler";
g_beatHandler.HandleMessage = HeartbeatMsgHandler;
g_beatHandler.HandleMessage = HbMsgHandler;
g_beatHandler.looper = GetLooper(LOOP_TYPE_DEFAULT);
if (g_beatHandler.looper == NULL) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "beat get looper fail");
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB get looper fail");
return SOFTBUS_ERR;
}
return SOFTBUS_OK;
}
void LnnHeartbeatFsmDeinit(void)
void LnnHbFsmDeinit(void)
{
g_curentState = -1;
}
@ -343,150 +348,175 @@ void LnnHeartbeatFsmDeinit(void)
static int32_t OnTryAsMasterNode(const SoftBusMessage *msg)
{
(void)msg;
LnnDumpBeatMgrUpdateList();
LnnDumpBeatOnlineNodeList();
if (g_curentState == STATE_BEAT_MASTER_NODE_INDEX) {
return STATE_BEAT_MASTER_NODE_INDEX;
}
if (LnnRemoveBeatFsmMsg(EVENT_BEAT_AS_MASTER_NODE, 0, NULL) != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "beat remove master beat timing error");
return SOFTBUS_ERR;
}
if (LnnRemoveBeatFsmMsg(EVENT_BEAT_REPEAT_CYCLE, 0, NULL) != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "beat remove master beat timing error");
return SOFTBUS_ERR;
}
GearMode gearMode;
LnnDumpHbMgrUpdateList();
LnnDumpHbOnlineNodeList();
if (g_curentState == STATE_HB_MASTER_NODE_INDEX) {
return STATE_HB_MASTER_NODE_INDEX;
}
if (LnnRemoveHbFsmMsg(EVENT_HB_AS_MASTER_NODE, 0, NULL) != SOFTBUS_OK) {
return SOFTBUS_ERR;
}
if (LnnRemoveHbFsmMsg(EVENT_HB_REPEAT_CYCLE, 0, NULL) != SOFTBUS_OK) {
return SOFTBUS_ERR;
}
if (LnnGetHeartbeatGearMode(&gearMode) != SOFTBUS_OK) {
return SOFTBUS_ERR;
}
uint64_t delayMillis = (uint64_t)gearMode.modeCycle * HEARTBEAT_TIME_FACTOR + HEARTBEAT_ENABLE_DELAY_LEN;
LnnPostDelayMsgToBeatFsm(EVENT_BEAT_AS_MASTER_NODE, NULL, delayMillis);
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "beat as normal node");
return STATE_BEAT_NORMAL_NODE_INDEX;
uint64_t delayMillis = (uint64_t)gearMode.modeCycle * HB_TIME_FACTOR + HB_ENABLE_DELAY_LEN;
if (LnnPostDelayMsgToHbFsm(EVENT_HB_AS_MASTER_NODE, NULL, delayMillis) != SOFTBUS_OK) {
return SOFTBUS_ERR;
}
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "heartbeat(HB) try as master node in %llu mecs", delayMillis);
return STATE_HB_NORMAL_NODE_INDEX;
}
static int32_t onBeatMasterNodeEnter(const SoftBusMessage *msg)
static int32_t OnMasterStateEnter(const SoftBusMessage *msg)
{
(void)msg;
char udid[UDID_BUF_LEN] = {0};
if (LnnGetLocalStrInfo(STRING_KEY_DEV_UDID, udid, UDID_BUF_LEN) != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "get local udid err");
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB get local udid err");
return SOFTBUS_ERR;
}
LnnSetLocalStrInfo(STRING_KEY_MASTER_NODE_UDID, udid);
LnnSetLocalNumInfo(NUM_KEY_MASTER_NODE_WEIGHT, LnnGetLocalWeight());
if (LnnRemoveBeatFsmMsg(EVENT_BEAT_REPEAT_CYCLE, 0, NULL) != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "beat remove master beat timing err");
if (LnnSetLocalStrInfo(STRING_KEY_MASTER_NODE_UDID, udid) != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB set local master node udid err");
return SOFTBUS_ERR;
}
LnnPostDelayMsgToBeatFsm(EVENT_BEAT_REPEAT_CYCLE, NULL, HEARTBEAT_ENABLE_DELAY_LEN);
return STATE_BEAT_MASTER_NODE_INDEX;
if (LnnSetLocalNumInfo(NUM_KEY_MASTER_NODE_WEIGHT, LnnGetLocalWeight()) != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB set local master node weight err");
return SOFTBUS_ERR;
}
if (LnnRemoveHbFsmMsg(EVENT_HB_REPEAT_CYCLE, 0, NULL) != SOFTBUS_OK) {
return SOFTBUS_ERR;
}
if (LnnPostDelayMsgToHbFsm(EVENT_HB_REPEAT_CYCLE, NULL, HB_ENABLE_DELAY_LEN) != SOFTBUS_OK) {
return SOFTBUS_ERR;
}
return STATE_HB_MASTER_NODE_INDEX;
}
static int32_t onBeatMasterNodeExit(const SoftBusMessage *msg)
static int32_t OnMasterStateExit(const SoftBusMessage *msg)
{
(void)msg;
if (LnnRemoveBeatFsmMsg(EVENT_BEAT_REPEAT_CYCLE, 0, NULL) != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "beat remove master beat timing err");
if (LnnRemoveHbFsmMsg(EVENT_HB_REPEAT_CYCLE, 0, NULL) != SOFTBUS_OK) {
return SOFTBUS_ERR;
}
return STATE_BEAT_MASTER_NODE_INDEX;
return STATE_HB_MASTER_NODE_INDEX;
}
static int32_t OnBeatRepeatCycle(const SoftBusMessage *msg)
static int32_t OnRepeatCycle(const SoftBusMessage *msg)
{
(void)msg;
LnnDumpBeatMgrUpdateList();
LnnDumpBeatOnlineNodeList();
PostMsgToBeatHandler(EVENT_BEAT_ONCE_ENTER, 0, CONNECTION_ADDR_MAX, NULL);
LnnPostDelayMsgToBeatFsm(EVENT_BEAT_TIMEOUT, NULL, HEARTBEAT_MANAGER_TIMEOUT_LEN);
GearMode gearMode;
LnnDumpHbMgrUpdateList();
LnnDumpHbOnlineNodeList();
if (PostMsgToHbHandler(EVENT_HB_ONCE_BEGIN, 0, CONNECTION_ADDR_MAX, NULL) != SOFTBUS_OK) {
return SOFTBUS_ERR;
}
if (LnnPostDelayMsgToHbFsm(EVENT_HB_TIMEOUT, NULL, HB_ONE_CYCLE_TIMEOUT_LEN) != SOFTBUS_OK) {
return SOFTBUS_ERR;
}
if (LnnGetHeartbeatGearMode(&gearMode) != SOFTBUS_OK) {
return SOFTBUS_ERR;
}
uint64_t delayMillis = (uint64_t)gearMode.modeCycle * HEARTBEAT_TIME_FACTOR;
LnnPostDelayMsgToBeatFsm(EVENT_BEAT_REPEAT_CYCLE, NULL, delayMillis);
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "beat as master node");
return STATE_BEAT_MASTER_NODE_INDEX;
uint64_t delayMillis = (uint64_t)gearMode.modeCycle * HB_TIME_FACTOR;
if (LnnPostDelayMsgToHbFsm(EVENT_HB_REPEAT_CYCLE, NULL, delayMillis) != SOFTBUS_OK) {
return SOFTBUS_ERR;
}
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "heartbeat(HB) perform as master node");
return STATE_HB_MASTER_NODE_INDEX;
}
static int32_t OnElectAsMasterNode(const SoftBusMessage *msg)
{
(void)msg;
g_curentState = STATE_NONE_BEAT_INDEX;
if (LnnRemoveBeatFsmMsg(EVENT_BEAT_AS_MASTER_NODE, 0, NULL) != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "beat remove master beat trans err");
g_curentState = STATE_HB_NONE_INDEX;
if (LnnRemoveHbFsmMsg(EVENT_HB_AS_MASTER_NODE, 0, NULL) != SOFTBUS_OK) {
return SOFTBUS_ERR;
}
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "beat elect as master node");
return STATE_BEAT_MASTER_NODE_INDEX;
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "heartbeat(HB) elect as master node");
return STATE_HB_MASTER_NODE_INDEX;
}
static int32_t OnElectAsNormalNode(const SoftBusMessage *msg)
{
(void)msg;
g_curentState = STATE_NONE_BEAT_INDEX;
if (LnnRemoveBeatFsmMsg(EVENT_BEAT_AS_MASTER_NODE, 0, NULL) != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "beat remove master beat trans err");
g_curentState = STATE_HB_NONE_INDEX;
if (LnnRemoveHbFsmMsg(EVENT_HB_AS_MASTER_NODE, 0, NULL) != SOFTBUS_OK) {
return SOFTBUS_ERR;
}
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "beat elect as normal node");
return STATE_BEAT_NORMAL_NODE_INDEX;
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "heartbeat(HB) elect as normal node");
return STATE_HB_NORMAL_NODE_INDEX;
}
static int32_t OnBeatOnceEnter(const SoftBusMessage *msg)
static int32_t OnOneCycleBegin(const SoftBusMessage *msg)
{
if (LnnHeartbeatMgrStart() != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "start beat adv or scan fail");
(void)LnnRemoveBeatFsmMsg(EVENT_BEAT_TIMEOUT, 0, NULL);
if (LnnHbMgrOneCycleBegin() != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB start mgr to perform one cycle fail");
(void)LnnRemoveHbFsmMsg(EVENT_HB_TIMEOUT, 0, NULL);
return SOFTBUS_ERR;
}
LnnPostDelayMsgToBeatFsm(EVENT_BEAT_ONCE_OUT, NULL, HEARTBEAT_TOCK_TIME_LEN);
LnnHeartbeatMonitorDevInfo((ConnectionAddrType)msg->arg2, HEARTBEAT_MONITOR_DELAY_LEN);
return g_curentState;
}
static int32_t OnBeatOnceOut(const SoftBusMessage *msg)
{
(void)msg;
if (LnnHeartbeatMgrStopAdv() != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "stop once beat adv fail");
if (LnnPostDelayMsgToHbFsm(EVENT_HB_ONCE_END, NULL, HB_ONE_CYCLE_LEN) != SOFTBUS_OK) {
return SOFTBUS_ERR;
}
if (LnnHbCheckDevStatus((ConnectionAddrType)msg->arg2, HB_CHECK_DELAY_LEN) != SOFTBUS_OK) {
return SOFTBUS_ERR;
}
return g_curentState;
}
static int32_t OnStartHeartbeat(const SoftBusMessage *msg)
{
g_curentState = STATE_NONE_BEAT_INDEX;
int32_t beatStateIndex = (int32_t)msg->arg1;
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "beat start in status: %d", beatStateIndex);
return beatStateIndex;
}
static int32_t OnStopHeartbeat(const SoftBusMessage *msg)
static int32_t OnOneCycleEnd(const SoftBusMessage *msg)
{
(void)msg;
if (LnnHeartbeatMgrStop() != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "beat manager stop fail");
if (LnnHbMgrOneCycleEnd() != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "stop once HB adv fail");
return SOFTBUS_ERR;
}
return STATE_NONE_BEAT_INDEX;
}
static int32_t OnBeatTimeOut(const SoftBusMessage *msg)
{
(void)msg;
(void)LnnRemoveBeatFsmMsg(EVENT_BEAT_ONCE_ENTER, 0, NULL);
(void)LnnRemoveBeatFsmMsg(EVENT_BEAT_ONCE_OUT, 0, NULL);
return g_curentState;
}
static bool BeatCheckActiveConn(ConnectionAddrType addrType, const char *networkId)
static int32_t OnHeartbeatStart(const SoftBusMessage *msg)
{
g_curentState = STATE_HB_NONE_INDEX;
int32_t stateIndex = (int32_t)msg->arg1;
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "heartbeat(HB) start in status: %d", stateIndex);
return stateIndex;
}
static int32_t OnHeartbeatStop(const SoftBusMessage *msg)
{
(void)msg;
if (LnnHbMgrStop() != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB stop manager fail");
return SOFTBUS_ERR;
}
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "heartbeat(HB) process stop.");
return STATE_HB_NONE_INDEX;
}
static int32_t OnOneCycleTimeout(const SoftBusMessage *msg)
{
(void)msg;
if (LnnRemoveHbFsmMsg(EVENT_HB_ONCE_BEGIN, 0, NULL) != SOFTBUS_OK) {
return SOFTBUS_ERR;
}
if (LnnRemoveHbFsmMsg(EVENT_HB_ONCE_END, 0, NULL) != SOFTBUS_OK) {
return SOFTBUS_ERR;
}
return g_curentState;
}
static bool HbCheckActiveConn(ConnectionAddrType addrType, const char *networkId)
{
ConnectOption option = {0};
NodeInfo *nodeInfo = NULL;
@ -502,17 +532,17 @@ static bool BeatCheckActiveConn(ConnectionAddrType addrType, const char *network
case CONNECTION_ADDR_BLE:
nodeInfo = LnnGetNodeInfoById(networkId, CATEGORY_NETWORK_ID);
if (nodeInfo == NULL) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "beat not find node, no need to notify lost");
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "HB not find node, no need to notify lost");
return true;
}
mac = LnnGetBtMac(nodeInfo);
if (mac == NULL) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "beat get bt mac err");
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB get bt mac err");
return true;
}
option.type = CONNECT_BR;
if (strncpy_s(option.info.brOption.brMac, BT_MAC_LEN, mac, strlen(mac)) != EOK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "beat strncpy_s bt mac err");
if (strcpy_s(option.info.brOption.brMac, BT_MAC_LEN, mac) != EOK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB strcpy_s bt mac err");
return true;
}
break;
@ -522,79 +552,82 @@ static bool BeatCheckActiveConn(ConnectionAddrType addrType, const char *network
return CheckActiveConnection(&option);
}
static int32_t OnBeatDeviceLost(const SoftBusMessage *msg)
static int32_t OnDetectDeviceLost(const SoftBusMessage *msg)
{
ConnectionAddrType addrType = (ConnectionAddrType)msg->arg2;
const char *networkId = (const char *)msg->obj;
if (networkId == NULL) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "beat device networkId is null");
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB device networkId is null");
return SOFTBUS_ERR;
}
if (BeatCheckActiveConn(addrType, networkId)) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_WARN, "beat cannot offline dev, set new offline check begin");
if (HbCheckActiveConn(addrType, networkId)) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_WARN, "HB cannot offline dev, set new offline check begin");
if (LnnOfflineTimingByHeartbeat(networkId, addrType) != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "beat set new offline check err");
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB set new offline check err");
return SOFTBUS_ERR;
}
return SOFTBUS_OK;
return g_curentState;
}
if (addrType == CONNECTION_ADDR_MAX) {
/* heartbeat dont support medium type except ble now, so only offline ble devices */
if (LnnRequestLeaveSpecific(networkId, CONNECTION_ADDR_BLE) != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "beat notify device lost fail");
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB notify device lost fail");
return SOFTBUS_ERR;
}
return g_curentState;
}
if (LnnRequestLeaveSpecific(networkId, addrType) != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "beat notify device lost fail");
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB notify device lost fail");
return SOFTBUS_ERR;
}
return g_curentState;
}
static int32_t OnMonitorDeviceStatus(const SoftBusMessage *msg)
static int32_t OnCheckDeviceStatus(const SoftBusMessage *msg)
{
NodeBasicInfo *info = NULL;
int32_t infoNum, i;
GearMode gearMode;
SoftBusSysTime times;
uint64_t nowTime, oldTimeStamp, offlineMillis;
NodeBasicInfo *info = NULL;
DiscoveryType discType = LnnGetDiscoveryType(msg->arg2);
if (LnnGetAllOnlineNodeInfo(&info, &infoNum) != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "beat get node info fail");
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB get node info fail");
return SOFTBUS_ERR;
}
if (info == NULL || infoNum == 0) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "beat no online node");
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "HB no online node");
return g_curentState;
}
GearMode gearMode;
SoftBusSysTime times;
SoftBusGetTime(&times);
uint64_t oldTimeStamp;
if (LnnGetHeartbeatGearMode(&gearMode) != SOFTBUS_OK) {
return SOFTBUS_ERR;
}
uint64_t offlineMillis = (uint64_t)gearMode.modeCycle * HEARTBEAT_TIME_FACTOR + HEARTBEAT_ENABLE_DELAY_LEN;
uint64_t nowTime = (uint64_t)times.sec * HEARTBEAT_TIME_FACTOR + (uint64_t)times.usec / HEARTBEAT_TIME_FACTOR;
offlineMillis = (uint64_t)gearMode.modeCycle * HB_TIME_FACTOR + HB_ENABLE_DELAY_LEN;
nowTime = (uint64_t)times.sec * HB_TIME_FACTOR + (uint64_t)times.usec / HB_TIME_FACTOR;
for (i = 0; i < infoNum; i++) {
NodeInfo *nodeInfo = LnnGetNodeInfoById(info[i].networkId, CATEGORY_NETWORK_ID);
if (nodeInfo == NULL || (msg->arg2 != CONNECTION_ADDR_MAX && !LnnHasDiscoveryType(nodeInfo, discType))) {
continue;
}
if (LnnGetDistributedHeartbeatTimestamp(info[i].networkId, &oldTimeStamp) != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "beat get timeStamp err, networkId:%s", info[i].networkId);
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB get timeStamp err, nodeInfo i=%d", i);
continue;
}
if ((nowTime - oldTimeStamp) > offlineMillis) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "beat Notify networkId:%s offline, timestamp:%llu, now:%llu",
info[i].networkId, oldTimeStamp, nowTime);
if (LnnRemoveBeatFsmMsg(EVENT_BEAT_DEVICE_LOST, msg->arg2, info[i].networkId) != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "beat remove offline timing err");
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "HB notify nodeInfo i=%d offline, timestamp:%llu, now:%llu",
i, oldTimeStamp, nowTime);
if (LnnRemoveHbFsmMsg(EVENT_HB_DEVICE_LOST, msg->arg2, info[i].networkId) != SOFTBUS_OK) {
SoftBusFree(info);
return SOFTBUS_ERR;
}
if (LnnHbProcessDeviceLost(info[i].networkId, (ConnectionAddrType)msg->arg2, 0) != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB process dev lost err, nodeInfo i=%d", i);
SoftBusFree(info);
return SOFTBUS_ERR;
}
LnnHeartbeatNodeOffline(info[i].networkId, (ConnectionAddrType)msg->arg2, 0);
}
}
SoftBusFree(info);

View File

@ -36,7 +36,7 @@
#include "softbus_log.h"
#include "softbus_utils.h"
#define BEAT_REQ_LIFETIME_IN_MEM (60 * 60 * HEARTBEAT_TIME_FACTOR)
#define BEAT_REQ_LIFETIME_IN_MEM (60 * 60 * HB_TIME_FACTOR)
typedef struct {
ListNode node;
@ -54,151 +54,153 @@ typedef struct {
int32_t (*deinit)(void);
} HeartbeatImpl;
static void BeatMgrRelayToMaster(const char *udidHash, ConnectionAddrType type);
static int32_t BeatMgrRecvHigherWight(const char *udidHash, int32_t weight, ConnectionAddrType type);
static int32_t BeatMgrUpdateDevInfo(DeviceInfo *device, int32_t weight, int32_t localMasterWeight);
static void HbMgrRelayToMaster(const char *udidHash, ConnectionAddrType type);
static int32_t HbMgrRecvHigherWeight(const char *udidHash, int32_t weight, ConnectionAddrType type);
static int32_t HbMgrUpdateDevInfo(DeviceInfo *device, int32_t weight, int32_t localMasterWeight);
static HeartbeatImpl g_heartbeatImpl[LNN_BEAT_IMPL_TYPE_MAX] = {
[LNN_BEAT_IMPL_TYPE_BLE] = {
static HeartbeatImpl g_hbImpl[HB_IMPL_TYPE_MAX] = {
[HB_IMPL_TYPE_BLE] = {
.init = LnnInitBleHeartbeat,
.onOnceBegin = LnnOnceBleBeatBegin,
.onOnceEnd = LnnOnceBleBeatEnd,
.onOnceBegin = LnnOnceBleHbBegin,
.onOnceEnd = LnnOnceBleHbEnd,
.stop = LnnStopBleHeartbeat,
.deinit = LnnDeinitBleHeartbeat,
},
};
static LnnHeartbeatImplCallback g_heartbeatCallback = {
.onRelay = BeatMgrRelayToMaster,
.onRecvHigherWeight = BeatMgrRecvHigherWight,
.onUpdateDev = BeatMgrUpdateDevInfo,
static LnnHeartbeatImplCallback g_hbCallback = {
.onRelay = HbMgrRelayToMaster,
.onRecvHigherWeight = HbMgrRecvHigherWeight,
.onUpdateDev = HbMgrUpdateDevInfo,
};
static SoftBusList *g_beatUpdateInfoList = NULL;
static SoftBusList *g_hbUpdateInfoList = NULL;
static int32_t FirstSetUpdateReqTime(DeviceInfo *device, int32_t weight, int32_t localMasterWeight,
uint64_t *updateTime)
static int32_t FirstSetUpdateReqTime(DeviceInfo *device, int32_t weight, int32_t localMasterWeight, uint64_t updateTime)
{
HeartbeatUpdateReq *item = NULL;
item = (HeartbeatUpdateReq *)SoftBusMalloc(sizeof(HeartbeatUpdateReq));
if (item == NULL) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "beat item malloc err");
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB item malloc err");
return SOFTBUS_MALLOC_ERR;
}
item->device = (DeviceInfo *)SoftBusCalloc(sizeof(DeviceInfo));
if (item->device == NULL) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "beat device malloc err");
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB calloc deviceInfo err");
SoftBusFree(item);
return SOFTBUS_MALLOC_ERR;
}
if (memcpy_s(item->device, sizeof(DeviceInfo), device, sizeof(DeviceInfo)) != EOK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "beat device memcpy_s err");
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB memcpy_s deviceInfo err");
SoftBusFree(item->device);
SoftBusFree(item);
return SOFTBUS_MEM_ERR;
}
item->lastUpdateTime = *updateTime;
item->lastUpdateTime = updateTime;
item->weight = weight;
item->localMasterWeight = localMasterWeight;
ListInit(&item->node);
ListAdd(&g_beatUpdateInfoList->list, &item->node);
g_beatUpdateInfoList->cnt++;
ListAdd(&g_hbUpdateInfoList->list, &item->node);
g_hbUpdateInfoList->cnt++;
return SOFTBUS_OK;
}
static int32_t SetUpdateReqTime(DeviceInfo *device, int32_t weight, int32_t localMasterWeight,
uint64_t *updateTime)
static int32_t SetUpdateReqTime(DeviceInfo *device, int32_t weight, int32_t localMasterWeight, uint64_t updateTime)
{
HeartbeatUpdateReq *item = NULL;
HeartbeatUpdateReq *nextItem = NULL;
if (SoftBusMutexLock(&g_beatUpdateInfoList->lock) != 0) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "lock BeatMgrUpdateList fail");
if (SoftBusMutexLock(&g_hbUpdateInfoList->lock) != 0) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB lock update info list fail");
return SOFTBUS_LOCK_ERR;
}
LIST_FOR_EACH_ENTRY_SAFE(item, nextItem, &g_beatUpdateInfoList->list, HeartbeatUpdateReq, node) {
LIST_FOR_EACH_ENTRY_SAFE(item, nextItem, &g_hbUpdateInfoList->list, HeartbeatUpdateReq, node) {
if (memcmp((void *)item->device->devId, (void *)device->devId, SHORT_UDID_HASH_LEN) == 0 &&
LnnGetDiscoveryType(item->device->addr[0].type) == LnnGetDiscoveryType(device->addr[0].type)) {
item->lastUpdateTime = *updateTime;
item->lastUpdateTime = updateTime;
item->weight = weight;
item->localMasterWeight = localMasterWeight;
(void)SoftBusMutexUnlock(&g_beatUpdateInfoList->lock);
(void)SoftBusMutexUnlock(&g_hbUpdateInfoList->lock);
return SOFTBUS_OK;
}
if ((*updateTime - item->lastUpdateTime) > BEAT_REQ_LIFETIME_IN_MEM) {
if ((updateTime - item->lastUpdateTime) > BEAT_REQ_LIFETIME_IN_MEM) {
ListDelete(&item->node);
SoftBusFree(item->device);
SoftBusFree(item);
}
}
FirstSetUpdateReqTime(device, weight, localMasterWeight, updateTime);
(void)SoftBusMutexUnlock(&g_beatUpdateInfoList->lock);
if (FirstSetUpdateReqTime(device, weight, localMasterWeight, updateTime) != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB first set update req time fail");
(void)SoftBusMutexUnlock(&g_hbUpdateInfoList->lock);
return SOFTBUS_ERR;
}
(void)SoftBusMutexUnlock(&g_hbUpdateInfoList->lock);
return SOFTBUS_OK;
}
static bool IsRepeatedUpdateReq(const char *udidHash, ConnectionAddrType type, uint64_t *nowTime)
static bool IsRepeatedUpdateReq(const char *udidHash, ConnectionAddrType type, uint64_t nowTime)
{
/* ignore repeated (udidHash & DiscoveryType) update callbacks within 10 seconds */
/* ignore repeated (udidHash & DiscoveryType) update request within 10 seconds */
HeartbeatUpdateReq *item = NULL;
if (SoftBusMutexLock(&g_beatUpdateInfoList->lock) != 0) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "lock BeatMgrUpdateList fail");
if (SoftBusMutexLock(&g_hbUpdateInfoList->lock) != 0) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB lock update info list fail");
return false;
}
LIST_FOR_EACH_ENTRY(item, &g_beatUpdateInfoList->list, HeartbeatUpdateReq, node) {
LIST_FOR_EACH_ENTRY(item, &g_hbUpdateInfoList->list, HeartbeatUpdateReq, node) {
if (memcmp((void *)item->device->devId, (void *)udidHash, DISC_MAX_DEVICE_ID_LEN) == 0 &&
LnnGetDiscoveryType(item->device->addr[0].type) == LnnGetDiscoveryType(type) &&
(*nowTime - item->lastUpdateTime < HEARTBEAT_UPDATE_TIME_PRECISION)) {
(void)SoftBusMutexUnlock(&g_beatUpdateInfoList->lock);
(nowTime - item->lastUpdateTime < HB_UPDATE_INTERVAL_LEN)) {
(void)SoftBusMutexUnlock(&g_hbUpdateInfoList->lock);
return true;
}
}
(void)SoftBusMutexUnlock(&g_beatUpdateInfoList->lock);
(void)SoftBusMutexUnlock(&g_hbUpdateInfoList->lock);
return false;
}
static int32_t GenHexStringHash(const unsigned char *str, int32_t len, char *hashStr)
{
int32_t ret;
unsigned char hashResult[HB_SHA_HASH_LEN] = {0};
if (str == NULL) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "beat GenHexStringHash invalid param");
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB gen str hash invalid param");
return SOFTBUS_INVALID_PARAM;
}
unsigned char hashResult[BEAT_SHA_HASH_LEN] = {0};
int32_t ret = SoftBusGenerateStrHash(str, strlen((char *)str), hashResult);
ret = SoftBusGenerateStrHash(str, strlen((char *)str), hashResult);
if (ret != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "beat GenerateStrHash fail");
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB gen str hash fail, ret=%d", ret);
return ret;
}
ret = ConvertBytesToHexString(hashStr, len + 1, hashResult, len / HEXIFY_UNIT_LEN);
if (ret != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "beat ConvertBytesToHexString fail");
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB convert bytes to str hash fail ret=%d", ret);
return ret;
}
return SOFTBUS_OK;
}
static NodeInfo *BeatGetMatchNode(const char *devId, const ConnectionAddrType type)
static NodeInfo *HbGetMatchNode(const char *devId, const ConnectionAddrType type)
{
NodeBasicInfo *info = NULL;
int32_t infoNum, i;
NodeBasicInfo *info = NULL;
char udidHash[SHORT_UDID_HASH_HEX_LEN + 1] = {0};
if (LnnGetAllOnlineNodeInfo(&info, &infoNum) != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "beat get node info fail");
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB get all online node info fail");
return NULL;
}
if (info == NULL || infoNum == 0) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "beat no online node");
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "HB none online node");
return NULL;
}
char udidHash[SHORT_UDID_HASH_HEX_LEN + 1] = {0};
DiscoveryType discType = LnnGetDiscoveryType(type);
for (i = 0; i < infoNum; i++) {
NodeInfo *nodeInfo = LnnGetNodeInfoById(info[i].networkId, CATEGORY_NETWORK_ID);
if (nodeInfo == NULL || !LnnHasDiscoveryType(nodeInfo, discType)) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_DBG, "beat node online not have discType:%d", discType);
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_DBG, "HB node online not have discType:%d", discType);
continue;
}
if (GenHexStringHash((const unsigned char *)nodeInfo->deviceInfo.deviceUdid,
@ -206,7 +208,7 @@ static NodeInfo *BeatGetMatchNode(const char *devId, const ConnectionAddrType ty
continue;
}
if (strncmp(udidHash, devId, SHORT_UDID_HASH_HEX_LEN) == 0) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_DBG, "beat node online udidHash:%s, devId:%s", udidHash, devId);
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_DBG, "HB node online udidHash:%s", udidHash);
SoftBusFree(info);
return nodeInfo;
}
@ -215,288 +217,308 @@ static NodeInfo *BeatGetMatchNode(const char *devId, const ConnectionAddrType ty
return NULL;
}
static int32_t BeatMgrDeviceFound(const DeviceInfo *device)
static int32_t HbMgrDiscoveryDevice(const DeviceInfo *device)
{
ConnectionAddr *addr = (ConnectionAddr *)device->addr;
if (addr == NULL) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "device addr is null");
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB discovery device addr is null");
return SOFTBUS_ERR;
}
if (LnnNotifyDiscoveryDevice(addr) != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "notify device found fail");
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB notify device found fail");
return SOFTBUS_ERR;
}
return SOFTBUS_OK;
}
static int32_t BeatProcessUpdateReq(const DeviceInfo *device, const uint64_t *updateTime)
static int32_t HbProcessUpdateReq(const DeviceInfo *device, const uint64_t updateTime)
{
NodeInfo *nodeInfo = BeatGetMatchNode(device->devId, device->addr[0].type);
NodeInfo *nodeInfo = HbGetMatchNode(device->devId, device->addr[0].type);
if (nodeInfo == NULL) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "beat find device [udidHash:%s, ConnectionAddrType:%02X]",
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "heartbeat(HB) find device udidHash:%s, ConnectionAddrType:%02X",
device->devId, device->addr[0].type);
(void)BeatMgrDeviceFound(device);
(void)HbMgrDiscoveryDevice(device);
return SOFTBUS_OK;
}
if (LnnSetDistributedHeartbeatTimestamp(nodeInfo->networkId, updateTime) != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "beat update timeStamp err, udidHash:%s", device->devId);
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB update timeStamp err, udidHash:%s", device->devId);
return SOFTBUS_ERR;
}
if (LnnRemoveBeatFsmMsg(EVENT_BEAT_DEVICE_LOST, (uint64_t)device->addr[0].type,
nodeInfo->networkId) != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "beat remove offline check err, udidHash:%s", device->devId);
if (LnnRemoveHbFsmMsg(EVENT_HB_DEVICE_LOST, (uint64_t)device->addr[0].type, nodeInfo->networkId) != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB remove offline check err, udidHash:%s", device->devId);
return SOFTBUS_ERR;
}
if (LnnOfflineTimingByHeartbeat(nodeInfo->networkId, device->addr[0].type) != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "beat set new offline check err, udidHash:%s", device->devId);
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB set new offline check err, udidHash:%s", device->devId);
return SOFTBUS_ERR;
}
return SOFTBUS_OK;
}
static int32_t BeatMgrUpdateDevInfo(DeviceInfo *device, int32_t weight, int32_t localMasterWeight)
static int32_t HbMgrUpdateDevInfo(DeviceInfo *device, int32_t weight, int32_t localMasterWeight)
{
SoftBusSysTime times;
SoftBusGetTime(&times);
uint64_t nowTime;
nowTime = (uint64_t)times.sec * HEARTBEAT_TIME_FACTOR + (uint64_t)times.usec / HEARTBEAT_TIME_FACTOR;
if (IsRepeatedUpdateReq(device->devId, device->addr[0].type, &nowTime) == true) {
return SOFTBUS_NETWORK_HEARTBEAT_REPEATED;
}
if (SetUpdateReqTime(device, weight, localMasterWeight, &nowTime) != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "beat updateMgrDev fail, udidHash:%s", device->devId);
if (device == NULL) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB mgr update deviceInfo get invalid param");
return SOFTBUS_ERR;
}
nowTime = (uint64_t)times.sec * HB_TIME_FACTOR + (uint64_t)times.usec / HB_TIME_FACTOR;
if (IsRepeatedUpdateReq(device->devId, device->addr[0].type, nowTime)) {
return SOFTBUS_NETWORK_HEARTBEAT_REPEATED;
}
if (SetUpdateReqTime(device, weight, localMasterWeight, nowTime) != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB update req time fail, udidHash:%s", device->devId);
return SOFTBUS_ERR;
}
char *deviceType = LnnConvertIdToDeviceType((uint16_t)device->devType);
if (deviceType == NULL) {
return SOFTBUS_ERR;
}
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_DBG, ">> BeatMgrUpdateDevInfo OnTock [udidHash:%s, devTypeHex:%02X,"
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_DBG, ">> heartbeat(HB) OnTock [udidHash:%s, devTypeHex:%02X,"
"devTypeStr:%s, ConnectionAddrType:%d, peerWeight:%d, localMasterWeight:%d, nowTime:%llu]", device->devId,
device->devType, deviceType, device->addr[0].type, weight, localMasterWeight, nowTime);
if (BeatProcessUpdateReq(device, &nowTime) != SOFTBUS_OK) {
return SOFTBUS_ERR;
}
return SOFTBUS_OK;
return HbProcessUpdateReq(device, nowTime);
}
static int32_t BeatMgrRecvHigherWight(const char *udidHash, int32_t weight, ConnectionAddrType type)
static int32_t HbMgrRecvHigherWeight(const char *udidHash, int32_t weight, ConnectionAddrType type)
{
char localMasterUdid[UDID_BUF_LEN] = {0};
NodeInfo *nodeInfo = BeatGetMatchNode(udidHash, type);
if (udidHash == NULL) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB mgr recv higher weight get invalid param");
return SOFTBUS_ERR;
}
NodeInfo *nodeInfo = HbGetMatchNode(udidHash, type);
if (nodeInfo == NULL) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "BeatMgrRecvHigherWight udidhash:%s is not online yet", udidHash);
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "HB recv higher weight udidhash:%s is not online yet", udidHash);
return SOFTBUS_OK;
}
if (LnnGetLocalStrInfo(STRING_KEY_MASTER_NODE_UDID, localMasterUdid, sizeof(localMasterUdid)) != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "beat get local master udid fail");
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB get local master udid fail");
return SOFTBUS_ERR;
}
if (strcmp(localMasterUdid, nodeInfo->deviceInfo.deviceUdid) != 0 &&
LnnNotifyMasterElect(nodeInfo->networkId, nodeInfo->deviceInfo.deviceUdid, weight) != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "beat set local master info fail");
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB set local master info fail");
return SOFTBUS_ERR;
}
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_DBG, "BeatMgrRecvHigherWight udidHash:%s, weight:%d", udidHash, weight);
return LnnHeartbeatAsNormalNode();
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_DBG, "HB recv higher weight udidHash:%s, weight:%d", udidHash, weight);
return SOFTBUS_OK;
}
static void BeatMgrRelayToMaster(const char *udidHash, ConnectionAddrType type)
static void HbMgrRelayToMaster(const char *udidHash, ConnectionAddrType type)
{
NodeInfo *nodeInfo = BeatGetMatchNode(udidHash, type);
if (nodeInfo == NULL) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "BeatMgrRelayToMaster udidhash:%s is not online yet", udidHash);
return;
}
char localUdid[UDID_BUF_LEN] = {0};
char localMasterUdid[UDID_BUF_LEN] = {0};
(void)LnnGetLocalStrInfo(STRING_KEY_DEV_UDID, localUdid, UDID_BUF_LEN);
(void)LnnGetLocalStrInfo(STRING_KEY_MASTER_NODE_UDID, localMasterUdid, UDID_BUF_LEN);
if (udidHash == NULL) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB mgr relay to master get invalid param");
return;
}
NodeInfo *nodeInfo = HbGetMatchNode(udidHash, type);
if (nodeInfo == NULL) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "HB relay to master udidhash:%s is not online yet", udidHash);
return;
}
if (LnnGetLocalStrInfo(STRING_KEY_DEV_UDID, localUdid, UDID_BUF_LEN) != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB relay to master get udid err, udidhash:%s", udidHash);
return;
}
if (LnnGetLocalStrInfo(STRING_KEY_MASTER_NODE_UDID, localMasterUdid, UDID_BUF_LEN) != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB relay to master get masterUdid err, udidhash:%s", udidHash);
return;
}
if (strcmp(localMasterUdid, localUdid) != 0) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "BeatMgrRelayToMaster process. localMasterUdid:%s, localUdid:%s",
localMasterUdid, localUdid);
(void)LnnHeartbeatRelayBeat(type);
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "HB relay to master process, udidhash:%s", udidHash);
(void)LnnHbRelayToMaster(type);
}
}
static void BeatInitUpdateList(void)
static int32_t HbInitUpdateList(void)
{
if (g_beatUpdateInfoList != NULL) {
return;
if (g_hbUpdateInfoList != NULL) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB init update list get invalid param");
return SOFTBUS_INVALID_PARAM;
}
g_beatUpdateInfoList = CreateSoftBusList();
if (g_beatUpdateInfoList == NULL) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "create BeatMgrUpdateList fail.");
return;
g_hbUpdateInfoList = CreateSoftBusList();
if (g_hbUpdateInfoList == NULL) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB create update info list fail");
return SOFTBUS_ERR;
}
g_beatUpdateInfoList->cnt = 0;
g_hbUpdateInfoList->cnt = 0;
return SOFTBUS_OK;
}
static void BeatDeinitUpdateList(void)
static void HbDeinitUpdateList(void)
{
if (g_beatUpdateInfoList == NULL) {
return;
}
if (SoftBusMutexLock(&g_beatUpdateInfoList->lock) != 0) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "lock BeatMgrUpdateList fail");
return;
}
HeartbeatUpdateReq *reqItem = NULL;
HeartbeatUpdateReq *nextreqItem = NULL;
LIST_FOR_EACH_ENTRY_SAFE(reqItem, nextreqItem, &g_beatUpdateInfoList->list, HeartbeatUpdateReq, node) {
if (g_hbUpdateInfoList == NULL) {
return;
}
if (SoftBusMutexLock(&g_hbUpdateInfoList->lock) != 0) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB lock update info list fail");
return;
}
LIST_FOR_EACH_ENTRY_SAFE(reqItem, nextreqItem, &g_hbUpdateInfoList->list, HeartbeatUpdateReq, node) {
ListDelete(&reqItem->node);
SoftBusFree(reqItem->device);
SoftBusFree(reqItem);
}
(void)SoftBusMutexUnlock(&g_beatUpdateInfoList->lock);
DestroySoftBusList(g_beatUpdateInfoList);
g_beatUpdateInfoList = NULL;
(void)SoftBusMutexUnlock(&g_hbUpdateInfoList->lock);
DestroySoftBusList(g_hbUpdateInfoList);
g_hbUpdateInfoList = NULL;
}
void LnnDumpBeatMgrUpdateList(void)
void LnnDumpHbMgrUpdateList(void)
{
#define HB_DUMP_UPDATE_INFO_MAX_NUM 10
int32_t dumpCount = 0;
char *deviceType = NULL;
HeartbeatUpdateReq *item = NULL;
if (SoftBusMutexLock(&g_beatUpdateInfoList->lock) != 0) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "lock BeatMgrUpdateList fail");
return;
}
if (IsListEmpty(&g_beatUpdateInfoList->list)) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_DBG, "LnnDumpBeatMgrUpdateList count:0");
(void)SoftBusMutexUnlock(&g_beatUpdateInfoList->lock);
return;
}
LIST_FOR_EACH_ENTRY(item, &g_beatUpdateInfoList->list, HeartbeatUpdateReq, node) {
char *deviceType = LnnConvertIdToDeviceType((uint16_t)item->device->devType);
if (deviceType == NULL) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "beat get deviceType fail");
(void)SoftBusMutexUnlock(&g_beatUpdateInfoList->lock);
return;
if (SoftBusMutexLock(&g_hbUpdateInfoList->lock) != 0) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB lock update info list fail");
return;
}
if (IsListEmpty(&g_hbUpdateInfoList->list)) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_DBG, "LnnDumpHbMgrUpdateList count=0");
(void)SoftBusMutexUnlock(&g_hbUpdateInfoList->lock);
return;
}
LIST_FOR_EACH_ENTRY(item, &g_hbUpdateInfoList->list, HeartbeatUpdateReq, node) {
dumpCount++;
if (dumpCount > HB_DUMP_UPDATE_INFO_MAX_NUM) {
continue;
}
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_DBG, "LnnDumpBeatMgrUpdateList count:%d [udidHash:%s, deviceType:%s,"
deviceType = LnnConvertIdToDeviceType((uint16_t)item->device->devType);
if (deviceType == NULL) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB get deviceType fail, devId:%s", item->device->devId);
continue;
}
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_DBG, "LnnDumpHbMgrUpdateList count:%d [udidHash:%s, deviceType:%s,"
"ConnectionAddrType:%02X, weight:%d, localMasterWeight:%d, lastUpdateTime:%llu]",
g_beatUpdateInfoList->cnt, item->device->devId, deviceType, item->device->addr[0].type, item->weight,
g_hbUpdateInfoList->cnt, item->device->devId, deviceType, item->device->addr[0].type, item->weight,
item->localMasterWeight, item->lastUpdateTime);
}
(void)SoftBusMutexUnlock(&g_beatUpdateInfoList->lock);
(void)SoftBusMutexUnlock(&g_hbUpdateInfoList->lock);
}
void LnnDumpBeatOnlineNodeList(void)
void LnnDumpHbOnlineNodeList(void)
{
#define HB_DUMP_ONLINE_NODE_MAX_NUM 5
int32_t infoNum, i;
uint64_t oldTimeStamp;
NodeBasicInfo *info = NULL;
if (LnnGetAllOnlineNodeInfo(&info, &infoNum) != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "beat get node info fail");
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB get node info fail");
return;
}
if (info == NULL || infoNum == 0) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "LnnDumpBeatOnlineNodeList count:0");
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "LnnDumpHbOnlineNodeList count=0");
return;
}
for (i = 0; i < infoNum; i++) {
NodeInfo *nodeInfo = LnnGetNodeInfoById(info[i].networkId, CATEGORY_NETWORK_ID);
if (nodeInfo == NULL) {
if (nodeInfo == NULL || i > HB_DUMP_ONLINE_NODE_MAX_NUM) {
continue;
}
if (LnnGetDistributedHeartbeatTimestamp(info[i].networkId, &oldTimeStamp) != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "beat get timeStamp err, networkId:%s", info[i].networkId);
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB get timeStamp err, nodeInfo i=%d", i);
continue;
}
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_DBG, "LnnDumpBeatOnlineNodeList count:%d [udid:%s, networkId:%s,"
"masterUdid:%s, masterWeight:%d, discoveryType:%d, oldTimeStamp:%llu]", infoNum,
nodeInfo->deviceInfo.deviceUdid, nodeInfo->networkId, nodeInfo->masterUdid, nodeInfo->masterWeight,
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_DBG, "LnnDumpHbOnlineNodeList count:%d [i:%d, deviceName:%s,"
"deviceTypeId:%d, masterWeight:%d, discoveryType:%d, oldTimeStamp:%llu]", infoNum, i,
nodeInfo->deviceInfo.deviceName, nodeInfo->deviceInfo.deviceTypeId, nodeInfo->masterWeight,
nodeInfo->discoveryType, oldTimeStamp);
}
SoftBusFree(info);
}
int32_t LnnHeartbeatMgrInit(void)
int32_t LnnHbMgrInit(void)
{
uint32_t i;
for (i = 0; i < LNN_BEAT_IMPL_TYPE_MAX; ++i) {
if (g_heartbeatImpl[i].init == NULL) {
for (i = 0; i < HB_IMPL_TYPE_MAX; ++i) {
if (g_hbImpl[i].init == NULL) {
continue;
}
if (g_heartbeatImpl[i].init(&g_heartbeatCallback) != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "init heartbeat impl(%d) fail", i);
if (g_hbImpl[i].init(&g_hbCallback) != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB init heartbeat impl(%d) fail", i);
return SOFTBUS_ERR;
}
}
BeatInitUpdateList();
if (HbInitUpdateList() != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB init update list fail");
return SOFTBUS_ERR;
}
return SOFTBUS_OK;
}
int32_t LnnHeartbeatMgrStart(void)
int32_t LnnHbMgrOneCycleBegin(void)
{
uint32_t i;
for (i = 0; i < LNN_BEAT_IMPL_TYPE_MAX; ++i) {
if (g_heartbeatImpl[i].onOnceBegin == NULL) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_WARN, "not support heartbeat(%d)", i);
for (i = 0; i < HB_IMPL_TYPE_MAX; ++i) {
if (g_hbImpl[i].onOnceBegin == NULL) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_WARN, "HB not support heartbeat(%d)", i);
continue;
}
if (g_heartbeatImpl[i].onOnceBegin() != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "start heartbeat impl(%d) fail", i);
if (g_hbImpl[i].onOnceBegin() != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB start heartbeat impl(%d) fail", i);
return SOFTBUS_ERR;
}
}
return SOFTBUS_OK;
}
int32_t LnnHeartbeatMgrStopAdv(void)
int32_t LnnHbMgrOneCycleEnd(void)
{
uint32_t i;
for (i = 0; i < LNN_BEAT_IMPL_TYPE_MAX; ++i) {
if (g_heartbeatImpl[i].onOnceEnd == NULL) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_WARN, "not support heartbeat(%d)", i);
for (i = 0; i < HB_IMPL_TYPE_MAX; ++i) {
if (g_hbImpl[i].onOnceEnd == NULL) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_WARN, "HB not support heartbeat(%d)", i);
continue;
}
if (g_heartbeatImpl[i].onOnceEnd() != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "once heartbeat impl(%d) fail", i);
if (g_hbImpl[i].onOnceEnd() != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB once heartbeat impl(%d) fail", i);
return SOFTBUS_ERR;
}
}
return SOFTBUS_OK;
}
int32_t LnnHeartbeatMgrStop(void)
int32_t LnnHbMgrStop(void)
{
uint32_t i;
for (i = 0; i < LNN_BEAT_IMPL_TYPE_MAX; ++i) {
if (g_heartbeatImpl[i].stop == NULL) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_WARN, "not support heartbeat(%d)", i);
for (i = 0; i < HB_IMPL_TYPE_MAX; ++i) {
if (g_hbImpl[i].stop == NULL) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_WARN, "HB not support heartbeat(%d)", i);
continue;
}
if (g_heartbeatImpl[i].stop() != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "stop heartbeat impl(%d) fail", i);
return SOFTBUS_ERR;
if (g_hbImpl[i].stop() != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB stop heartbeat impl(%d) fail", i);
continue;
}
}
return SOFTBUS_OK;
}
void LnnHeartbeatMgrDeinit(void)
void LnnHbMgrDeinit(void)
{
uint32_t i;
for (i = 0; i < LNN_BEAT_IMPL_TYPE_MAX; ++i) {
if (g_heartbeatImpl[i].deinit == NULL) {
for (i = 0; i < HB_IMPL_TYPE_MAX; ++i) {
if (g_hbImpl[i].deinit == NULL) {
continue;
}
if (g_heartbeatImpl[i].deinit() != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "deinit heartbeat impl(%d) fail", i);
return;
if (g_hbImpl[i].deinit() != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB deinit heartbeat impl(%d) fail", i);
continue;
}
}
BeatDeinitUpdateList();
HbDeinitUpdateList();
}

View File

@ -27,59 +27,54 @@
#include "softbus_permission.h"
#include "softbus_utils.h"
#define BEAT_INVALID_UID (-1)
#define HB_INVALID_UID (-1)
typedef struct {
int32_t callingUid;
GearMode gearMode;
HeartbeatPolicy beatPolicy[LNN_BEAT_IMPL_TYPE_MAX];
HeartbeatPolicy policy[HB_IMPL_TYPE_MAX];
SoftBusMutex lock;
} LnnHeartbeatStrategy;
static LnnHeartbeatStrategy g_strategy = {
.beatPolicy[LNN_BEAT_IMPL_TYPE_BLE] = {
.policy[HB_IMPL_TYPE_BLE] = {
.implPolicy = NULL,
},
};
static int32_t HeartbeatMonitorInit(void)
static int32_t HbMonitorInit(void)
{
if (SoftBusMutexInit(&g_strategy.lock, NULL) != 0) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "mutex init fail!");
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB monitor init mutex fail!");
return SOFTBUS_ERR;
}
g_strategy.gearMode.modeCycle = LOW_FREQ_CYCLE;
g_strategy.gearMode.modeDuration = LONG_DURATION;
g_strategy.gearMode.wakeupFlag = false;
g_strategy.callingUid = BEAT_INVALID_UID;
g_strategy.callingUid = HB_INVALID_UID;
return SOFTBUS_OK;
}
static int32_t HeartbeatMonitorDeinit(void)
static void HbMonitorDeinit(void)
{
uint8_t i;
for (i = 0; i < LNN_BEAT_IMPL_TYPE_MAX; i++) {
if (g_strategy.beatPolicy[i].implPolicy != NULL) {
SoftBusFree(g_strategy.beatPolicy[i].implPolicy);
g_strategy.beatPolicy[i].implPolicy = NULL;
for (i = 0; i < HB_IMPL_TYPE_MAX; i++) {
if (g_strategy.policy[i].implPolicy != NULL) {
SoftBusFree(g_strategy.policy[i].implPolicy);
g_strategy.policy[i].implPolicy = NULL;
}
}
if (SoftBusMutexDestroy(&g_strategy.lock) != 0) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "mutex deinit fail!");
return SOFTBUS_ERR;
}
return SOFTBUS_OK;
SoftBusMutexDestroy(&g_strategy.lock);
}
static int32_t ResetHeartbeatParam(int32_t callingUid, GearMode mode, const HeartbeatImplPolicy *implPolicy)
{
if (SoftBusMutexLock(&g_strategy.lock) != 0) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "lock mutex fail!");
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB reset param lock mutex fail!");
return SOFTBUS_ERR;
}
if (g_strategy.callingUid == BEAT_INVALID_UID || g_strategy.callingUid == callingUid) {
if (g_strategy.callingUid == HB_INVALID_UID || g_strategy.callingUid == callingUid) {
g_strategy.gearMode = mode;
} else {
if (g_strategy.gearMode.modeCycle <= mode.modeCycle) {
@ -93,41 +88,41 @@ static int32_t ResetHeartbeatParam(int32_t callingUid, GearMode mode, const Hear
if (implPolicy != NULL) {
HeartbeatImplPolicy *tmpImplPolicy = (HeartbeatImplPolicy *)SoftBusCalloc(sizeof(HeartbeatImplPolicy));
if (tmpImplPolicy == NULL) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "beat malloc err");
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB calloc tmpImplPolicy err");
(void)SoftBusMutexUnlock(&g_strategy.lock);
return SOFTBUS_MALLOC_ERR;
}
tmpImplPolicy->type = implPolicy->type;
tmpImplPolicy->info = implPolicy->info;
g_strategy.beatPolicy[implPolicy->type].implPolicy = tmpImplPolicy;
g_strategy.policy[implPolicy->type].implPolicy = tmpImplPolicy;
tmpImplPolicy = NULL;
}
SoftBusMutexUnlock(&g_strategy.lock);
(void)SoftBusMutexUnlock(&g_strategy.lock);
return SOFTBUS_OK;
}
int32_t ShiftLNNGear(const char *pkgName, int32_t callingUid, const char *targetNetworkId,
GearMode mode, const HeartbeatImplPolicy *implPolicy)
{
if (pkgName == NULL || callingUid <= BEAT_INVALID_UID) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "beat invalid param");
NodeInfo *nodeInfo = NULL;
if (pkgName == NULL || callingUid <= HB_INVALID_UID) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB shift gear get invalid param");
return SOFTBUS_INVALID_PARAM;
}
if (targetNetworkId != NULL) {
NodeInfo *nodeInfo = NULL;
nodeInfo = LnnGetNodeInfoById(targetNetworkId, CATEGORY_NETWORK_ID);
if (nodeInfo == NULL) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "beat get node info fail");
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB shift gear get node info fail");
return SOFTBUS_ERR;
}
if (!LnnIsNodeOnline(nodeInfo)) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "beat target networdid offline");
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB target networkId has offline");
return SOFTBUS_ERR;
}
}
if (ResetHeartbeatParam(callingUid, mode, implPolicy) != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "reset gear mode param fail");
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB reset gearMode param fail");
return SOFTBUS_ERR;
}
return SOFTBUS_OK;
@ -136,14 +131,15 @@ int32_t ShiftLNNGear(const char *pkgName, int32_t callingUid, const char *target
int32_t LnnGetHeartbeatGearMode(GearMode *mode)
{
if (mode == NULL) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB get gearMode invalid param!");
return SOFTBUS_INVALID_PARAM;
}
if (SoftBusMutexLock(&g_strategy.lock) != 0) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "beat lock mutex fail!");
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB get gearMode lock mutex fail!");
return SOFTBUS_LOCK_ERR;
}
*mode = g_strategy.gearMode;
SoftBusMutexUnlock(&g_strategy.lock);
(void)SoftBusMutexUnlock(&g_strategy.lock);
return SOFTBUS_OK;
}
@ -153,97 +149,105 @@ int32_t LnnGetHeartbeatImplPolicy(LnnHeartbeatImplType type, HeartbeatImplPolicy
return SOFTBUS_INVALID_PARAM;
}
if (SoftBusMutexLock(&g_strategy.lock) != 0) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "beat lock mutex fail!");
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB get impl policy lock mutex fail!");
return SOFTBUS_LOCK_ERR;
}
if (g_strategy.beatPolicy[type].implPolicy == NULL) {
SoftBusMutexUnlock(&g_strategy.lock);
if (g_strategy.policy[type].implPolicy == NULL) {
(void)SoftBusMutexUnlock(&g_strategy.lock);
return SOFTBUS_ERR;
}
*implPolicy = *g_strategy.beatPolicy[type].implPolicy;
SoftBusMutexUnlock(&g_strategy.lock);
*implPolicy = *g_strategy.policy[type].implPolicy;
(void)SoftBusMutexUnlock(&g_strategy.lock);
return SOFTBUS_OK;
}
int32_t LnnOfflineTimingByHeartbeat(const char *networkId, ConnectionAddrType addrType)
{
uint64_t delayMillis;
GearMode gearMode;
if (networkId == NULL || addrType != CONNECTION_ADDR_BLE) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "beat invalid param: %d", addrType);
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB offline timing get invalid param: %d", addrType);
return SOFTBUS_INVALID_PARAM;
}
GearMode gearMode;
if (LnnGetHeartbeatGearMode(&gearMode) != SOFTBUS_OK) {
return SOFTBUS_ERR;
}
uint64_t delayMillis = (uint64_t)gearMode.modeCycle * HEARTBEAT_TIME_FACTOR + HEARTBEAT_ENABLE_DELAY_LEN;
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_DBG, "beat start offline countdown");
return LnnHeartbeatNodeOffline(networkId, addrType, delayMillis);
delayMillis = (uint64_t)gearMode.modeCycle * HB_TIME_FACTOR + HB_ENABLE_DELAY_LEN;
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_DBG, "heartbeat(HB) start offline countdown");
if (LnnHbProcessDeviceLost(networkId, addrType, delayMillis) != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB process dev lost err");
return SOFTBUS_ERR;
}
return SOFTBUS_OK;
}
int32_t LnnNotifyMasterNodeChanged(const char *masterUdid, int32_t weight)
{
(void)weight;
char localUdid[UDID_BUF_LEN] = {0};
if (masterUdid == NULL) {
return SOFTBUS_INVALID_PARAM;
}
(void)LnnGetLocalStrInfo(STRING_KEY_DEV_UDID, localUdid, UDID_BUF_LEN);
if (strcmp(masterUdid, localUdid) == 0) {
return LnnPostMsgToBeatFsm(EVENT_BEAT_AS_MASTER_NODE, NULL);
if (LnnGetLocalStrInfo(STRING_KEY_DEV_UDID, localUdid, UDID_BUF_LEN) != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB notify master node changed get local udid err");
return SOFTBUS_ERR;
}
return LnnPostMsgToBeatFsm(EVENT_BEAT_AS_NORMAL_NODE, NULL);
if (strcmp(masterUdid, localUdid) == 0) {
return LnnPostMsgToHbFsm(EVENT_HB_AS_MASTER_NODE, NULL);
}
return LnnPostMsgToHbFsm(EVENT_HB_AS_NORMAL_NODE, NULL);
}
int32_t LnnStartHeartbeatDelay(void)
{
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "heartbeat fsm start");
(void)LnnRemoveBeatFsmMsg(EVENT_BEAT_START, 0, NULL);
(void)LnnRemoveBeatFsmMsg(EVENT_BEAT_STOP, 0, NULL);
if (LnnHeartbeatFsmStart(STATE_BEAT_MASTER_NODE_INDEX, 0) != SOFTBUS_OK) {
uint64_t delayMillis;
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "heartbeat(HB) process start.");
if (LnnRemoveHbFsmMsg(EVENT_HB_START, 0, NULL) != SOFTBUS_OK) {
return SOFTBUS_ERR;
}
uint64_t delayMillis = (uint64_t)g_strategy.gearMode.modeDuration * HEARTBEAT_TIME_FACTOR;
if (LnnHeartbeatFsmStop(delayMillis) != SOFTBUS_OK) {
if (LnnRemoveHbFsmMsg(EVENT_HB_STOP, 0, NULL) != SOFTBUS_OK) {
return SOFTBUS_ERR;
}
if (LnnHbFsmStart(STATE_HB_MASTER_NODE_INDEX, 0) != SOFTBUS_OK) {
return SOFTBUS_ERR;
}
delayMillis = (uint64_t)g_strategy.gearMode.modeDuration * HB_TIME_FACTOR;
if (LnnHbFsmStop(delayMillis) != SOFTBUS_OK) {
return SOFTBUS_ERR;
}
return SOFTBUS_OK;
}
void LnnStopHeartbeat(void)
void LnnStopHeartbeatNow(void)
{
(void)LnnHeartbeatFsmStop(0);
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "heartbeat(HB) process stop.");
(void)LnnHbFsmStop(0);
}
int32_t LnnInitHeartbeat(void)
{
if (LnnHeartbeatMgrInit() != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "beat manager init fail");
if (LnnHbMgrInit() != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB manager init fail");
return SOFTBUS_ERR;
}
if (LnnHeartbeatFsmInit() != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "beat fsm init fail");
if (LnnHbFsmInit() != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB fsm init fail");
return SOFTBUS_ERR;
}
if (HeartbeatMonitorInit() != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "beat monitor init fail!");
if (HbMonitorInit() != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB monitor init fail!");
return SOFTBUS_ERR;
}
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "beat init success");
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "heartbeat(HB) init success");
return SOFTBUS_OK;
}
void LnnDeinitHeartbeat(void)
{
LnnHeartbeatFsmDeinit();
if (HeartbeatMonitorDeinit() != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "beat monitor deinit fail");
}
LnnHeartbeatMgrDeinit();
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "lnn heartbeat deinit done");
LnnHbFsmDeinit();
HbMonitorDeinit();
LnnHbMgrDeinit();
}

View File

@ -62,7 +62,7 @@ int32_t LnnStartHeartbeatDelay(void)
return SOFTBUS_OK;
}
void LnnStopHeartbeat(void)
void LnnStopHeartbeatNow(void)
{
}

View File

@ -41,7 +41,7 @@ if (dsoftbus_standard_feature_lnn_net) {
bus_center_hub_src +=
[ "$core_lane_hub_path/time_sync/src/lnn_time_sync_manager_virtual.c" ]
}
if (enable_lnn_heartbeat) {
if (dsoftbus_standard_feature_lnn_heartbeat) {
bus_center_hub_src += [
"$core_lane_hub_path/heartbeat/src/lnn_heartbeat_strategy.c",
"$core_lane_hub_path/heartbeat/src/lnn_heartbeat_manager.c",
@ -76,6 +76,7 @@ if (dsoftbus_standard_feature_lnn_net &&
dsoftbus_standard_feature_lnn_time_sync && enhanced) {
bus_center_hub_deps += [ "$dsoftbus_root_path/dsoftbus_enhance/core/bus_center/lnn/lane_hub/time_sync:dsoftbus_time_sync_impl" ]
}
if (dsoftbus_standard_feature_lnn_net && enable_lnn_heartbeat && enhanced) {
if (dsoftbus_standard_feature_lnn_net &&
dsoftbus_standard_feature_lnn_heartbeat && enhanced) {
bus_center_hub_deps += [ "$dsoftbus_root_path/dsoftbus_enhance/core/bus_center/lnn/lane_hub/heartbeat:dsoftbus_heartbeat_impl" ]
}

View File

@ -445,8 +445,8 @@ static void ParsePeerConnInfo(LnnConntionInfo *connInfo)
{
SoftBusSysTime times;
SoftBusGetTime(&times);
connInfo->nodeInfo->heartbeatTimeStamp = (uint64_t)times.sec * HEARTBEAT_TIME_FACTOR +
(uint64_t)times.usec / HEARTBEAT_TIME_FACTOR;
connInfo->nodeInfo->heartbeatTimeStamp = (uint64_t)times.sec * HB_TIME_FACTOR +
(uint64_t)times.usec / HB_TIME_FACTOR;
connInfo->nodeInfo->discoveryType = 1 << (uint32_t)LnnGetDiscoveryType(connInfo->addr.type);
connInfo->nodeInfo->authSeqNum = connInfo->authId;
connInfo->nodeInfo->authChannelId = (int32_t)connInfo->authId;

View File

@ -1678,7 +1678,7 @@ int32_t LnnRequestLeaveSpecific(const char *networkId, ConnectionAddrType addrTy
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "malloc specific msg fail");
return SOFTBUS_MALLOC_ERR;
}
if (strncpy_s(para->networkId, NETWORK_ID_BUF_LEN, networkId, strlen(networkId)) != EOK) {
if (strcpy_s(para->networkId, NETWORK_ID_BUF_LEN, networkId) != EOK) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "copy networkId fail");
SoftBusFree(para);
return SOFTBUS_ERR;

View File

@ -64,7 +64,7 @@ int32_t LnnGetBasicInfoByUdid(const char *udid, NodeBasicInfo *basicInfo);
int32_t LnnGetLaneCount(int32_t laneId);
int32_t LnnSetLaneCount(int32_t laneId, int32_t num);
int32_t LnnGetDistributedHeartbeatTimestamp(const char *networkId, uint64_t *timestamp);
int32_t LnnSetDistributedHeartbeatTimestamp(const char *networkId, const uint64_t *timestamp);
int32_t LnnSetDistributedHeartbeatTimestamp(const char *networkId, const uint64_t timestamp);
bool LnnGetOnlineStateById(const char *id, IdCategory type);
int32_t LnnGetLnnRelation(const char *id, IdCategory type, uint8_t *relation, uint32_t len);

View File

@ -1188,7 +1188,7 @@ int32_t LnnGetDistributedHeartbeatTimestamp(const char *networkId, uint64_t *tim
return SOFTBUS_OK;
}
int32_t LnnSetDistributedHeartbeatTimestamp(const char *networkId, const uint64_t *timestamp)
int32_t LnnSetDistributedHeartbeatTimestamp(const char *networkId, const uint64_t timestamp)
{
if (SoftBusMutexLock(&g_distributedNetLedger.lock) != 0) {
SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "lock mutex fail!");
@ -1200,7 +1200,7 @@ int32_t LnnSetDistributedHeartbeatTimestamp(const char *networkId, const uint64_
(void)SoftBusMutexUnlock(&g_distributedNetLedger.lock);
return SOFTBUS_ERR;
}
nodeInfo->heartbeatTimeStamp = *timestamp;
nodeInfo->heartbeatTimeStamp = timestamp;
(void)SoftBusMutexUnlock(&g_distributedNetLedger.lock);
return SOFTBUS_OK;
}

View File

@ -19,7 +19,6 @@
#include "bus_center_info_key.h"
#include "lnn_distributed_net_ledger.h"
#include "lnn_exchange_device_info.h"
#include "lnn_heartbeat_strategy.h"
#include "lnn_lane_info.h"
#include "lnn_lane_manager.h"
#include "lnn_local_net_ledger.h"
@ -105,8 +104,6 @@ void LedgerLaneHubTest::SetUp()
ret = LnnInitLocalLedger();
EXPECT_TRUE(ret == SOFTBUS_OK);
(void)LnnInitLaneManager();
ret = LnnInitHeartbeat();
EXPECT_TRUE(ret == SOFTBUS_OK);
GTEST_LOG_(INFO) << "LaneHubTest start.";
}
@ -951,39 +948,4 @@ HWTEST_F(LedgerLaneHubTest, LANE_HUB_LOCALINFO_LANE_Test_001, TestSize.Level1)
LnnSetLaneCount(LNN_LINK_TYPE_WLAN_5G, -LANES_COUNT_MAX);
EXPECT_TRUE(LnnGetLaneCount(LNN_LINK_TYPE_WLAN_5G) == 0);
}
/*
* @tc.name: HEARTBEAT_ARGS_ADJUST_Test_001
* @tc.desc: heart beat parameter adjust test
* @tc.type: FUNC
* @tc.require: AR000FNSVF
*/
HWTEST_F(LedgerLaneHubTest, HEARTBEAT_PARAMS_ADJUST_Test_001, TestSize.Level1)
{
GearMode mode = {
.modeCycle = HIGH_FREQ_CYCLE,
.modeDuration = NORMAL_DURATION,
.wakeupFlag = false,
};
HeartbeatImplPolicy implPolicy = {
.type = LNN_BEAT_IMPL_TYPE_BLE,
.info.ble = {
.advMinInterval = 10,
}
};
EXPECT_FALSE(ShiftLNNGear(NULL, 0, NULL, mode, &implPolicy) == SOFTBUS_OK);
EXPECT_FALSE(ShiftLNNGear("", -1, NULL, mode, &implPolicy) == SOFTBUS_OK);
ConstructALLCapacityNode();
LnnAddOnlineNode(&g_nodeInfo[ALL_CAPACITY]);
EXPECT_TRUE(ShiftLNNGear("", 0, g_nodeInfo[ALL_CAPACITY].networkId, mode, &implPolicy) == SOFTBUS_OK);
GearMode tempMode;
EXPECT_TRUE(LnnGetHeartbeatGearMode(&tempMode) == SOFTBUS_OK);
EXPECT_TRUE(tempMode.modeCycle == HIGH_FREQ_CYCLE);
EXPECT_TRUE(tempMode.modeDuration == NORMAL_DURATION);
EXPECT_TRUE(tempMode.wakeupFlag == false);
HeartbeatImplPolicy tempImplPolicy;
EXPECT_TRUE(LnnGetHeartbeatImplPolicy(LNN_BEAT_IMPL_TYPE_BLE, &tempImplPolicy) == SOFTBUS_OK);
EXPECT_TRUE(tempImplPolicy.info.ble.advMinInterval == 10);
}
}