修改蓝牙
This commit is contained in:
parent
dca184ea3d
commit
a1666d018f
|
|
@ -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,17 +228,20 @@ 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)
|
|
||||||
{
|
|
||||||
g_ble_error.recovery_target = RECOVERY_TARGET_RESTART_SEQ;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if(i == table_size)
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
|
@ -251,20 +249,7 @@ void BLE_StateMachine_Handler(void)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
|
|
@ -395,54 +384,51 @@ void BLE_StateMachine_Handler(void)
|
||||||
|
|
||||||
}
|
}
|
||||||
if(target_seq != NULL)
|
if(target_seq != NULL)
|
||||||
{
|
{
|
||||||
// 如果 set_executor 尚未启动,则初始化
|
// 如果 set_executor 尚未启动,则初始化
|
||||||
if(set_executor.sequence == NULL)
|
if(set_executor.sequence == NULL)
|
||||||
{
|
{
|
||||||
set_executor.sequence = target_seq;
|
set_executor.sequence = target_seq;
|
||||||
set_executor.cmd_index = 0;
|
set_executor.cmd_index = 0;
|
||||||
set_executor.retry_cnt = 0;
|
set_executor.retry_cnt = 0;
|
||||||
set_executor.state = EXEC_STATE_IDLE;
|
set_executor.state = EXEC_STATE_IDLE;
|
||||||
}
|
}
|
||||||
ExecutorResult_t res = CmdExecutor_Process(&set_executor);
|
ExecutorResult_t res = CmdExecutor_Process(&set_executor);
|
||||||
if(res == EXECUTOR_DONE)
|
if(res == EXECUTOR_DONE)
|
||||||
{
|
{
|
||||||
// 恢复命令执行成功,将恢复级别清零,返回原状态
|
// 恢复命令执行成功,将恢复级别清零,返回原状态
|
||||||
// 注意:恢复级别 recovery_level 是在 BLE_ERROR 中定义的静态变量,这里无法直接访问。
|
// 注意:恢复级别 recovery_level 是在 BLE_ERROR 中定义的静态变量,这里无法直接访问。
|
||||||
// 解决方案:可以在 BLE_ERROR 中定义一个全局或外部变量,或者通过某种方式传递。
|
// 解决方案:可以在 BLE_ERROR 中定义一个全局或外部变量,或者通过某种方式传递。
|
||||||
// 简单起见,我们可以在 BLE_ERROR 中重置 recovery_level,但这里无法做到。
|
// 简单起见,我们可以在 BLE_ERROR 中重置 recovery_level,但这里无法做到。
|
||||||
// 一个常见的做法是在 BLE_ERROR 中判断恢复成功与否,但由于这里返回原状态,我们可以在返回前设置一个标志,让 BLE_ERROR 下次进入时重置级别。
|
// 一个常见的做法是在 BLE_ERROR 中判断恢复成功与否,但由于这里返回原状态,我们可以在返回前设置一个标志,让 BLE_ERROR 下次进入时重置级别。
|
||||||
// 但考虑到恢复级别只在 BLE_ERROR 中管理,我们可以这样处理:当 BLE_SET 成功返回原状态后,原状态会继续执行,如果再次出错会重新进入 BLE_ERROR,此时 recovery_level 仍然存在,但这是合理的(因为已经成功过,应该重置级别?)
|
// 但考虑到恢复级别只在 BLE_ERROR 中管理,我们可以这样处理:当 BLE_SET 成功返回原状态后,原状态会继续执行,如果再次出错会重新进入 BLE_ERROR,此时 recovery_level 仍然存在,但这是合理的(因为已经成功过,应该重置级别?)
|
||||||
// 实际上,一次恢复成功意味着系统已经正常,应该重置级别。所以我们需要在 BLE_SET 成功时通知 BLE_ERROR 重置级别。
|
// 实际上,一次恢复成功意味着系统已经正常,应该重置级别。所以我们需要在 BLE_SET 成功时通知 BLE_ERROR 重置级别。
|
||||||
// 由于 recovery_level 是 BLE_ERROR 内部的静态变量,无法直接从外部修改。我们可以将 recovery_level 定义为全局变量,或者通过函数接口。
|
// 由于 recovery_level 是 BLE_ERROR 内部的静态变量,无法直接从外部修改。我们可以将 recovery_level 定义为全局变量,或者通过函数接口。
|
||||||
// 这里为了简单,我们假设 recovery_level 是全局变量(例如在文件顶部定义),并在 BLE_SET 成功时清零。
|
// 这里为了简单,我们假设 recovery_level 是全局变量(例如在文件顶部定义),并在 BLE_SET 成功时清零。
|
||||||
// 请根据实际情况调整。下面假设有一个全局变量 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));
|
|
||||||
curr_state = g_ble_error.origin_state;
|
|
||||||
}
|
|
||||||
else if(res == EXECUTOR_ERROR)
|
|
||||||
{
|
|
||||||
// 恢复命令也失败,将 set_executor 中的错误信息复制到 g_ble_error(保留 origin_state)
|
|
||||||
g_ble_error.type = set_executor.error_type;
|
|
||||||
g_ble_error.error_code = set_executor.error_code;
|
|
||||||
g_ble_error.cmd_index = set_executor.error_cmd_index;
|
|
||||||
g_ble_error.timestamp = HAL_GetTick();
|
|
||||||
// 注意:不要覆盖 origin_state
|
|
||||||
memset(&set_executor, 0, sizeof(set_executor));
|
|
||||||
// 再次进入 BLE_ERROR,恢复级别将在那里递增
|
|
||||||
curr_state = BLE_ERROR;
|
|
||||||
}
|
|
||||||
// 若返回 BUSY,则继续等待,不做状态切换
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
memset(&set_executor, 0, sizeof(set_executor));
|
||||||
|
curr_state = g_ble_error.origin_state;
|
||||||
|
}
|
||||||
|
else if(res == EXECUTOR_ERROR)
|
||||||
|
{
|
||||||
|
// 恢复命令也失败,将 set_executor 中的错误信息复制到 g_ble_error(保留 origin_state)
|
||||||
|
g_ble_error.type = set_executor.error_type;
|
||||||
|
g_ble_error.error_code = set_executor.error_code;
|
||||||
|
g_ble_error.cmd_index = set_executor.error_cmd_index;
|
||||||
|
g_ble_error.timestamp = HAL_GetTick();
|
||||||
|
// 注意:不要覆盖 origin_state
|
||||||
|
memset(&set_executor, 0, sizeof(set_executor));
|
||||||
|
// 再次进入 BLE_ERROR,恢复级别将在那里递增
|
||||||
|
curr_state = BLE_ERROR;
|
||||||
|
}
|
||||||
|
// 若返回 BUSY,则继续等待,不做状态切换
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
@ -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
Loading…
Reference in New Issue