修改蓝牙

This commit is contained in:
kkkjtr 2026-02-25 16:56:05 +08:00
parent dca184ea3d
commit a1666d018f
2 changed files with 109 additions and 125 deletions

View File

@ -21,6 +21,7 @@ static CmdExecutor_t init_executor;
static CmdExecutor_t first_connect_executor; static CmdExecutor_t first_connect_executor;
static CmdExecutor_t connect_executor; static CmdExecutor_t connect_executor;
static CmdExecutor_t set_executor; static CmdExecutor_t set_executor;
static uint8_t recovery_level = 0; // 放在文件开头
const char* ble_state_names[] = { const char* ble_state_names[] = {
@ -175,8 +176,13 @@ void BLE_StateMachine_Handler(void)
case BLE_ERROR : case BLE_ERROR :
{ {
static uint8_t recovery_level = 0;
const uint8_t MAX_RECOVERY_LEVEL = 3; const uint8_t MAX_RECOVERY_LEVEL = 3;
recovery_level++;
if (recovery_level > MAX_RECOVERY_LEVEL) {
DEBUG("恢复尝试已达上限(%d次进入等待", MAX_RECOVERY_LEVEL);
curr_state = BLE_WAITTING;
break;
}
switch(g_ble_error.type) switch(g_ble_error.type)
{ {
@ -194,31 +200,20 @@ void BLE_StateMachine_Handler(void)
break; break;
case ERR_TYPE_TIMEOUT_EXCEEDED: case ERR_TYPE_TIMEOUT_EXCEEDED:
{
g_ble_error.recovery_target = RECOVERY_TARGET_RESTART_SEQ; g_ble_error.recovery_target = RECOVERY_TARGET_RESTART_SEQ;
//curr_state = BLE_SET; //curr_state = BLE_SET;
//重新配置串口信息 //重新配置串口信息
}
break; break;
case ERR_TYPE_PARSE_FAILED: case ERR_TYPE_PARSE_FAILED:
{
// DEBUG("error message ,main state:%s, sub state:%s, cmd_index:%d, timestamp:%d", // DEBUG("error message ,main state:%s, sub state:%s, cmd_index:%d, timestamp:%d",
// ble_state_names[g_ble_error.main_state], ble_sub_state_names[g_ble_error.sub_state], g_ble_error.cmd_index, g_ble_error.timestamp); // ble_state_names[g_ble_error.main_state], ble_sub_state_names[g_ble_error.sub_state], g_ble_error.cmd_index, g_ble_error.timestamp);
g_ble_error.recovery_target = RECOVERY_TARGET_RESTART_SEQ; g_ble_error.recovery_target = RECOVERY_TARGET_RESTART_SEQ;
//重新配置串口信息 //重新配置串口信息
}
break; break;
case ERR_TYPE_MODULE_ERROR: case ERR_TYPE_MODULE_ERROR:
@ -233,18 +228,21 @@ void BLE_StateMachine_Handler(void)
DEBUG("error message ,main state:%s, sub state:%s, cmd_index:%d, timestamp:%d", DEBUG("error message ,main state:%s, sub state:%s, cmd_index:%d, timestamp:%d",
ble_state_names[g_ble_error.main_state], ble_sub_state_names[g_ble_error.sub_state], g_ble_error.cmd_index, g_ble_error.timestamp); ble_state_names[g_ble_error.main_state], ble_sub_state_names[g_ble_error.sub_state], g_ble_error.cmd_index, g_ble_error.timestamp);
//curr_state = BLE_SET; //curr_state = BLE_SET;
break;
}
} }
if(i == table_size) if(i == table_size)
{ {
g_ble_error.recovery_target = RECOVERY_TARGET_RESTART_SEQ; g_ble_error.recovery_target = RECOVERY_TARGET_RESTART_SEQ;
} }
}
}
break; break;
}
default: default:
g_ble_error.recovery_target = RECOVERY_TARGET_PANIC; g_ble_error.recovery_target = RECOVERY_TARGET_PANIC;
@ -253,19 +251,6 @@ void BLE_StateMachine_Handler(void)
} }
if(g_ble_error.recovery_target >= RECOVERY_TARGET_SW_RESET_MODULE)
{
recovery_level++;
if(recovery_level > MAX_RECOVERY_LEVEL)
{
// 超过最大恢复级别,无法恢复,进入等待
DEBUG("恢复级别 %d 超过阈值,进入等待", recovery_level);
curr_state = BLE_WAITTING;
break; // 跳出 BLE_ERROR
}
}
// 3. 执行恢复动作 // 3. 执行恢复动作
switch(g_ble_error.recovery_target) switch(g_ble_error.recovery_target)
{ {
@ -286,6 +271,7 @@ void BLE_StateMachine_Handler(void)
break; break;
} }
// 返回原状态,重新开始执行序列 // 返回原状态,重新开始执行序列
recovery_level = 0;
curr_state = g_ble_error.origin_state; curr_state = g_ble_error.origin_state;
break; break;
@ -307,10 +293,11 @@ void BLE_StateMachine_Handler(void)
default: default:
break; break;
} }
recovery_level = 0;
curr_state = g_ble_error.origin_state; curr_state = g_ble_error.origin_state;
break; break;
// case RECOVERY_TARGET_SW_RESET_MODULE: case RECOVERY_TARGET_SW_RESET_MODULE:
case RECOVERY_TARGET_FACTORY_RESET: case RECOVERY_TARGET_FACTORY_RESET:
// 需要通过 AT 命令执行,进入 BLE_SET 状态 // 需要通过 AT 命令执行,进入 BLE_SET 状态
// 注意origin_state 保持不变BLE_SET 执行完后会返回它 // 注意origin_state 保持不变BLE_SET 执行完后会返回它
@ -338,6 +325,7 @@ void BLE_StateMachine_Handler(void)
default: default:
break; break;
} }
recovery_level = 0;
curr_state = g_ble_error.origin_state; curr_state = g_ble_error.origin_state;
break; break;
@ -354,10 +342,10 @@ void BLE_StateMachine_Handler(void)
curr_state = BLE_WAITTING; curr_state = BLE_WAITTING;
break; break;
} }
break;
} }
break;
case BLE_SET : case BLE_SET :
@ -378,7 +366,7 @@ void BLE_StateMachine_Handler(void)
} }
break; break;
case RECOVERY_TARGET_FACTORY_RESET: case RECOVERY_TARGET_FACTORY_RESET:
//回复出厂设置 { //回复出厂设置
{ {
static const BleAtCmd_t factory_reset_seq[] = { static const BleAtCmd_t factory_reset_seq[] = {
{"AT+DEFAULT\r\n", "OK", 100, 3, NULL, NULL}, {"AT+DEFAULT\r\n", "OK", 100, 3, NULL, NULL},
@ -388,6 +376,7 @@ void BLE_StateMachine_Handler(void)
} }
break; break;
}
default: default:
//没有匹配目标则返回原状态 //没有匹配目标则返回原状态
curr_state = g_ble_error.origin_state; curr_state = g_ble_error.origin_state;
@ -419,7 +408,7 @@ void BLE_StateMachine_Handler(void)
// 请根据实际情况调整。下面假设有一个全局变量 uint8_t g_recovery_level; 并在 BLE_ERROR 中改为使用它。 // 请根据实际情况调整。下面假设有一个全局变量 uint8_t g_recovery_level; 并在 BLE_ERROR 中改为使用它。
// 如果没有,你可以将 recovery_level 定义在文件作用域static然后在 BLE_SET 中包含一个 extern 声明。 // 如果没有,你可以将 recovery_level 定义在文件作用域static然后在 BLE_SET 中包含一个 extern 声明。
// 这里我们暂时注释掉,你需要根据你的设计实现。 // 这里我们暂时注释掉,你需要根据你的设计实现。
// g_recovery_level = 0; // 如果使用全局变量 recovery_level = 0;
memset(&set_executor, 0, sizeof(set_executor)); memset(&set_executor, 0, sizeof(set_executor));
curr_state = g_ble_error.origin_state; curr_state = g_ble_error.origin_state;
@ -438,12 +427,9 @@ void BLE_StateMachine_Handler(void)
} }
// 若返回 BUSY则继续等待不做状态切换 // 若返回 BUSY则继续等待不做状态切换
} }
break;
} }
break; break;
case BLE_WAITTING: case BLE_WAITTING:
@ -474,7 +460,7 @@ void BLE_StateMachine_Handler(void)
} }
ExecutorResult_t CmdExecutor_Process(CmdExecutor_t* ex) ExecutorResult_t CmdExecutor_Process(CmdExecutor_t* ex) //发送流程执行器
{ {
switch(ex->state) switch(ex->state)
{ {
@ -512,8 +498,6 @@ ExecutorResult_t CmdExecutor_Process(CmdExecutor_t* ex)
} }
} }
else else
{ {

File diff suppressed because one or more lines are too long