修复了WS连接稳定性问题

This commit is contained in:
CSBOOK\44744
2026-06-27 20:29:09 +08:00
parent b829cd8ebc
commit caa4a7eded
16 changed files with 2197 additions and 248 deletions
@@ -29,9 +29,11 @@ static float s_current_angle = 0.0f;
static int s_rotate_speed_us = DOMAIN_DEFAULT_ROTATE_SPEED_US;
static bool s_initialized;
static SemaphoreHandle_t s_state_mutex;
static SemaphoreHandle_t s_rotation_mutex;
static portMUX_TYPE s_state_mutex_init_lock = portMUX_INITIALIZER_UNLOCKED;
#if ( configSUPPORT_STATIC_ALLOCATION == 1 )
static StaticSemaphore_t s_state_mutex_buf;
static StaticSemaphore_t s_rotation_mutex_buf;
#endif
static const domain_direction_axis_config_t s_default_axis_config = {
@@ -94,6 +96,55 @@ static bool ensure_state_mutex(void)
return (s_state_mutex != NULL);
}
static bool ensure_rotation_mutex(void)
{
if (s_rotation_mutex != NULL) {
return true;
}
#if ( configSUPPORT_STATIC_ALLOCATION == 1 )
portENTER_CRITICAL(&s_state_mutex_init_lock);
if (s_rotation_mutex == NULL) {
s_rotation_mutex = xSemaphoreCreateMutexStatic(&s_rotation_mutex_buf);
}
portEXIT_CRITICAL(&s_state_mutex_init_lock);
#else
SemaphoreHandle_t created_mutex = xSemaphoreCreateMutex();
portENTER_CRITICAL(&s_state_mutex_init_lock);
if (s_rotation_mutex == NULL) {
s_rotation_mutex = created_mutex;
created_mutex = NULL;
}
portEXIT_CRITICAL(&s_state_mutex_init_lock);
if (created_mutex != NULL) {
vSemaphoreDelete(created_mutex);
}
#endif
return (s_rotation_mutex != NULL);
}
static bool domain_direction_lock_rotation(int timeout_ms)
{
if (!ensure_rotation_mutex()) {
return false;
}
int effective_timeout_ms = timeout_ms > 0 ? timeout_ms : DOMAIN_ROTATE_TIMEOUT_MS;
TickType_t timeout_ticks = pdMS_TO_TICKS(effective_timeout_ms);
if (timeout_ticks == 0) {
timeout_ticks = 1;
}
return (xSemaphoreTake(s_rotation_mutex, timeout_ticks) == pdTRUE);
}
static void domain_direction_unlock_rotation(void)
{
if (s_rotation_mutex != NULL) {
(void)xSemaphoreGive(s_rotation_mutex);
}
}
bool domain_direction_lock_state(int timeout_ms)
{
if (!ensure_state_mutex()) {
@@ -253,37 +304,62 @@ esp_err_t domain_rotate_to_angle_with_timeout(float target_angle, int timeout_ms
TickType_t start_tick = xTaskGetTickCount();
int effective_timeout_ms = timeout_ms > 0 ? timeout_ms : DOMAIN_ROTATE_TIMEOUT_MS;
if (!domain_direction_lock_state(effective_timeout_ms)) {
if (!domain_direction_lock_rotation(effective_timeout_ms)) {
return ESP_ERR_TIMEOUT;
}
int state_timeout_ms = remaining_timeout_ms(start_tick, effective_timeout_ms);
if (state_timeout_ms <= 0) {
domain_direction_unlock_rotation();
return ESP_ERR_TIMEOUT;
}
if (!domain_direction_lock_state(state_timeout_ms)) {
domain_direction_unlock_rotation();
return ESP_ERR_TIMEOUT;
}
if (!s_initialized) {
domain_direction_unlock_state();
domain_direction_unlock_rotation();
return ESP_ERR_INVALID_STATE;
}
float normalized_target = domain_angle_normalize(target_angle);
float rotation = domain_angle_shortest_rotation(s_current_angle, normalized_target);
float current_angle = s_current_angle;
int rotate_speed_us = s_rotate_speed_us;
float rotation = domain_angle_shortest_rotation(current_angle, normalized_target);
if (rotation == 0.0f) {
domain_direction_unlock_state();
domain_direction_unlock_rotation();
return ESP_OK;
}
domain_direction_unlock_state();
int motor_timeout_ms = remaining_timeout_ms(start_tick, effective_timeout_ms);
if (motor_timeout_ms <= 0) {
domain_direction_unlock_state();
domain_direction_unlock_rotation();
return ESP_ERR_TIMEOUT;
}
esp_err_t motor_err = domain_runtime_stepper_rotate_angle_with_accel_timeout(
rotation,
s_rotate_speed_us,
rotate_speed_us,
motor_timeout_ms);
if (motor_err != ESP_OK) {
domain_direction_unlock_state();
domain_direction_unlock_rotation();
return motor_err;
}
s_current_angle = domain_angle_normalize(s_current_angle + rotation);
state_timeout_ms = remaining_timeout_ms(start_tick, effective_timeout_ms);
if (state_timeout_ms <= 0) {
domain_direction_unlock_rotation();
return ESP_ERR_TIMEOUT;
}
if (!domain_direction_lock_state(state_timeout_ms)) {
domain_direction_unlock_rotation();
return ESP_ERR_TIMEOUT;
}
s_current_angle = domain_angle_normalize(current_angle + rotation);
domain_direction_unlock_state();
domain_direction_unlock_rotation();
return ESP_OK;
}
@@ -139,7 +139,7 @@ static const domain_expression_catalog_entry_t s_expression_catalog[] = {
.priority = 100,
.interrupt_current = true,
.return_to_idle = true,
.asset_dir = "server_error",
.asset_dir = "jichubiaoqing",
.frame_order = s_server_error_frame_order,
.frame_order_count = sizeof(s_server_error_frame_order) / sizeof(s_server_error_frame_order[0]),
.repeat_count = 5,
@@ -867,43 +867,51 @@ static esp_err_t load_legacy_auth_state(void *out, size_t out_len, size_t *out_l
return ESP_ERR_INVALID_SIZE;
}
domain_auth_state_legacy_blob_t state = {0};
domain_auth_state_legacy_blob_t *state = calloc(1, sizeof(*state));
if (state == NULL) {
return ESP_ERR_NO_MEM;
}
bool found_any = false;
size_t jwt_len = 0U;
esp_err_t err = platform_storage_get_string(DOMAIN_AUTH_STATE_STORE_NAMESPACE,
DOMAIN_AUTH_LEGACY_STATE_JWT_KEY,
state.jwt,
sizeof(state.jwt),
state->jwt,
sizeof(state->jwt),
&jwt_len);
if (err == ESP_OK) {
found_any = true;
state.jwt_valid = jwt_len > 1U && state.jwt[0] != '\0';
state->jwt_valid = jwt_len > 1U && state->jwt[0] != '\0';
} else if (err != ESP_ERR_NOT_FOUND) {
free(state);
return err;
}
err = storage_get_optional_u64(DOMAIN_AUTH_STATE_STORE_NAMESPACE,
DOMAIN_AUTH_LEGACY_STATE_EXP_KEY,
&state.exp_epoch_s,
&state->exp_epoch_s,
&found_any);
if (err != ESP_OK) {
free(state);
return err;
}
err = storage_get_optional_u32(DOMAIN_AUTH_STATE_STORE_NAMESPACE,
DOMAIN_AUTH_LEGACY_STATE_SCOPE_KEY,
&state.scope_mask,
&state->scope_mask,
&found_any);
if (err != ESP_OK) {
free(state);
return err;
}
err = storage_get_optional_u32(DOMAIN_AUTH_STATE_STORE_NAMESPACE,
DOMAIN_AUTH_LEGACY_STATE_KEY_VERSION_KEY,
&state.key_version,
&state->key_version,
&found_any);
if (err != ESP_OK) {
free(state);
return err;
}
@@ -913,34 +921,39 @@ static esp_err_t load_legacy_auth_state(void *out, size_t out_len, size_t *out_l
&wait_factory,
&found_any);
if (err != ESP_OK) {
free(state);
return err;
}
state.wait_factory_persisted = wait_factory != 0U;
state->wait_factory_persisted = wait_factory != 0U;
err = storage_get_optional_u32(DOMAIN_AUTH_STATE_STORE_NAMESPACE,
DOMAIN_AUTH_LEGACY_STATE_FAIL_COUNT_KEY,
&state.fail_count,
&state->fail_count,
&found_any);
if (err != ESP_OK) {
free(state);
return err;
}
err = storage_get_optional_u64(DOMAIN_AUTH_STATE_STORE_NAMESPACE,
DOMAIN_AUTH_LEGACY_STATE_NEXT_RETRY_KEY,
&state.next_retry_epoch_s,
&state->next_retry_epoch_s,
&found_any);
if (err != ESP_OK) {
free(state);
return err;
}
if (!found_any) {
free(state);
return ESP_ERR_NOT_FOUND;
}
memcpy(out, &state, sizeof(state));
memcpy(out, state, sizeof(*state));
if (out_len_actual != NULL) {
*out_len_actual = sizeof(state);
*out_len_actual = sizeof(*state);
}
free(state);
return ESP_OK;
}