Commit e0a04b6a authored by Wenxi XU's avatar Wenxi XU
Browse files

添加CRC16校验功能,优化数据帧处理逻辑,增强错误日志记录

parent a17ffe57
Loading
Loading
Loading
Loading
+181 −60
Original line number Diff line number Diff line
@@ -12,6 +12,45 @@ LOG_MODULE_REGISTER(dual_protocol, LOG_LEVEL_INF);
K_HEAP_DEFINE(dual_protocol_heap, 4096);

#define HEART_BEAT_DELAY 200
#define CRC16_SIZE       2

// CRC16-CCITT计算函数
static uint16_t crc16_ccitt(uint8_t *data, size_t length)
{
	uint16_t crc = 0xFFFF;
	for (size_t i = 0; i < length; i++) {
		crc ^= (uint16_t)data[i] << 8;
		for (int j = 0; j < 8; j++) {
			if (crc & 0x8000) {
				crc = (crc << 1) ^ 0x1021;
			} else {
				crc <<= 1;
			}
		}
	}
	return crc;
}

// 添加CRC16到缓冲区末尾
static void add_crc16(uint8_t *buf, size_t data_len)
{
	uint16_t crc = crc16_ccitt(buf, data_len);
	GET_16BITS(buf, data_len) = crc;
}

// 验证CRC16
static bool verify_crc16(uint8_t *buf, size_t total_len)
{
	if (total_len < CRC16_SIZE) {
		return false;
	}

	size_t data_len = total_len - CRC16_SIZE;
	uint16_t received_crc = GET_16BITS(buf, data_len);
	uint16_t calculated_crc = crc16_ccitt(buf, data_len);

	return received_crc == calculated_crc;
}

struct tx_msg_bck {
	uint32_t ret;
@@ -32,13 +71,14 @@ K_THREAD_STACK_DEFINE(ares_usb_stack, 2048);

struct net_buf *alloc_and_add_data(struct AresProtocol *protocol, uint8_t *data, size_t len)
{
	struct dual_protocol_data *pdata = protocol->priv_data;
	if (protocol->interface->api->alloc_buf_with_data) {
		return protocol->interface->api->alloc_buf_with_data(protocol->interface, data,
								     len);
	} else {
		struct net_buf *buf = protocol->interface->api->alloc_buf(protocol->interface);
		if (buf == NULL) {
			LOG_ERR("Failed to allocate buffer.");
			LOG_ERR("%s Failed to allocate buffer.", pdata->name);
			return NULL;
		}
		net_buf_add_mem(buf, data, len);
@@ -63,16 +103,23 @@ static void error_handle(struct AresProtocol *protocol, uint16_t req_id, uint16_
	GET_8BITS(data->error_frame_buf, ERROR_ID_IDX) = req_id;
	GET_16BITS(data->error_frame_buf, ERROR_CODE_IDX) = error;
	if (error != HEART_BEAT) {
		LOG_ERR("Error code: %x, Request ID: %x", error, req_id);
		LOG_ERR("%s Error code: %x, Request ID: %x", data->name, error, req_id);
	} else {
		data->last_heart_beat = k_uptime_get_32();
	}

	size_t frame_len = ERROR_FRAME_LENGTH;
	if (data->crc_enabled) {
		add_crc16(data->error_frame_buf, ERROR_FRAME_LENGTH);
		frame_len += CRC16_SIZE;
	}

	struct net_buf *buf = protocol->interface->api->alloc_buf(protocol->interface);
	net_buf_add_mem(buf, data->error_frame_buf, ERROR_FRAME_LENGTH);
	if (buf == NULL) {
		LOG_ERR("Failed to allocate buffer for error frame.");
		LOG_ERR("%s Failed to allocate buffer for error frame.", data->name);
		return;
	}
	net_buf_add_mem(buf, data->error_frame_buf, frame_len);
	int err = protocol->interface->api->send(protocol->interface, buf);
	if (err != 0) {
		// LOG_ERR("Failed to send error frame. %d", err);
@@ -93,17 +140,28 @@ static bool proceed_tx_bck(struct AresProtocol *protocol, uint8_t ID)
	struct tx_msg_bck *item;
	while ((k_msgq_get(&data->func_tx_bckup_msgq, &item, K_NO_WAIT)) == 0) {
		if (item->id == ID && !found) {
			struct dual_protocol_data *pdata = protocol->priv_data;
			size_t frame_len = REPL_FRAME_LENGTH;
			if (pdata->crc_enabled) {
				frame_len += CRC16_SIZE;
			}

			struct net_buf *buf =
				protocol->interface->api->alloc_buf(protocol->interface);

			if (buf == NULL) {
				LOG_ERR("Failed to allocate buffer for FUNC frame.");
				LOG_ERR("%s Failed to allocate buffer for FUNC frame.", data->name);
				return false;
			}

			net_buf_add(buf, REPL_FRAME_LENGTH);
			net_buf_add(buf, frame_len);

			MAKE_REPL_FRAME(ID, item->ret, buf->data);

			if (pdata->crc_enabled) {
				add_crc16(buf->data, REPL_FRAME_LENGTH);
			}

			protocol->interface->api->send(protocol->interface, buf);

			found = true;
@@ -169,7 +227,7 @@ static void usb_trans_heart_beat(struct k_timer *timer)
		}
	} else {
		if ((k_uptime_get_32() - data->last_receive) <= 10 * HEART_BEAT_DELAY) {
			LOG_INF("Connection established.");
			LOG_INF("%s Connection established.", data->name);
			data->online = true;
		}
	}
@@ -180,20 +238,21 @@ static void usb_trans_heart_beat(struct k_timer *timer)

static void parse_sync(struct AresProtocol *protocol, uint8_t *buf, size_t len)
{
	struct dual_protocol_data *data = protocol->priv_data;
	LOG_HEXDUMP_DBG(buf, len, "SYNC frame received");

	uint16_t ID = GET_16BITS(buf, SYNC_ID_IDX);
	uint8_t *data = buf + SYNC_DATA_IDX;
	uint8_t *pdata = buf + SYNC_DATA_IDX;
	size_t data_len = len - SYNC_FRAME_LENGTH_OFFSET;

	sync_table_t *pack = find_pack(protocol->priv_data, ID);
	if (pack == NULL) {
		LOG_ERR("Cannot find corresponding ID from sync table: 0x%x", ID);
		LOG_ERR("%s Cannot find corresponding ID from sync table: 0x%x", data->name, ID);
		return;
	}

	if (pack->len + SYNC_FRAME_LENGTH_OFFSET != len) {
		LOG_ERR("SYNC frame length mismatch: %d vs %d",
		LOG_ERR("%s SYNC frame length mismatch: %d vs %d", data->name,
			pack->len + SYNC_FRAME_LENGTH_OFFSET, len);
		error_handle(protocol, pack->ID, UNKNOWN_TAIL);
		return;
@@ -203,7 +262,7 @@ static void parse_sync(struct AresProtocol *protocol, uint8_t *buf, size_t len)
		pack->cb(SYNC_PACK_STATUS_WRITE);
	}

	memcpy(pack->data, data, data_len);
	memcpy(pack->data, pdata, data_len);

	if (pack->cb != NULL) {
		pack->cb(SYNC_PACK_STATUS_DONE);
@@ -222,7 +281,7 @@ static void parse_repl(struct AresProtocol *protocol, uint8_t *buf, size_t len)
	if (data->func_ret_cb != NULL) {
		data->func_ret_cb(ID, req_id, ret);
	}
	LOG_DBG("REPL frame received: ID %x, req_id %x, ret %x", ID, req_id, ret);
	LOG_DBG("%s REPL frame received: ID %x, req_id %x, ret %x", data->name, ID, req_id, ret);
	return;
}

@@ -249,7 +308,7 @@ static void parse_func(struct AresProtocol *protocol, func_table_t *map)

	uint8_t *repl_frame = map->buf;
	if (k_mutex_lock(&map->mutex, K_NO_WAIT) != 0) {
		LOG_DBG("Failed to lock FUNC frame mutex.");
		LOG_DBG("%s Failed to lock FUNC frame mutex.", data->name);
		return;
	}
	GET_16BITS(repl_frame, REPL_HEAD_IDX) = REPL_FRAME_HEAD;
@@ -257,12 +316,18 @@ static void parse_func(struct AresProtocol *protocol, func_table_t *map)
	GET_32BITS(repl_frame, REPL_RET_IDX) = (uint32_t)ret;
	GET_8BITS(repl_frame, REPL_REQ_ID_IDX) = map->req_id;

	size_t frame_len = REPL_FRAME_LENGTH;
	if (data->crc_enabled) {
		add_crc16(repl_frame, REPL_FRAME_LENGTH);
		frame_len += CRC16_SIZE;
	}

	int err = 0;
	struct net_buf *buf = alloc_and_add_data(protocol, repl_frame, REPL_FRAME_LENGTH);
	struct net_buf *buf = alloc_and_add_data(protocol, repl_frame, frame_len);
	err = send_try_lock(protocol, buf, &map->mutex);

	if (err != 0) {
		LOG_ERR("Failed to send REPLY frame.");
		LOG_ERR("%s Failed to send REPLY frame.", data->name);
		return;
	}
	return;
@@ -270,17 +335,19 @@ static void parse_func(struct AresProtocol *protocol, func_table_t *map)

static void parse_error(struct AresProtocol *protocol, uint8_t data[], int8_t len)
{
	struct dual_protocol_data *pdata = protocol->priv_data;
	uint8_t req_id = GET_8BITS(data, ERROR_ID_IDX);
	if (req_id == HEARTBEAT_ID) {
		LOG_DBG("Heartbeat frame received.");
		LOG_DBG("%s Heartbeat frame received.", pdata->name);
		return;
	}
	if (!proceed_tx_bck(protocol, req_id)) {
		sync_table_t *pack = find_pack(protocol->priv_data,
					       sys_be16_to_cpu(GET_16BITS(data, ERROR_ID_IDX)));
		if (pack == NULL) {
			LOG_ERR("Cannot find corresponding ID from error "
				"frame.");
			LOG_ERR("%s Cannot find corresponding ID from error "
				"frame.",
				pdata->name);
			error_handle(protocol, req_id, CLEANED_PACK);
			return;
		}
@@ -289,7 +356,8 @@ static void parse_error(struct AresProtocol *protocol, uint8_t data[], int8_t le
		}
		dual_sync_flush(protocol, pack);
	}
	LOG_INF("Error frame received: id =%d; code=%d", req_id, GET_16BITS(data, ERROR_CODE_IDX));
	LOG_INF("%s Error frame received: id =%d; code=%d", pdata->name, req_id,
		GET_16BITS(data, ERROR_CODE_IDX));
	return;
}

@@ -297,11 +365,11 @@ int dual_ret_cb_set(struct AresProtocol *protocol, dual_func_ret_cb_t cb)
{
	struct dual_protocol_data *data = protocol->priv_data;
	if (cb == NULL) {
		LOG_ERR("Callback function is NULL.");
		LOG_ERR("%s Callback function is NULL.", data->name);
		return -EINVAL;
	}
	if (data->func_ret_cb != NULL) {
		LOG_ERR("Callback function already set.");
		LOG_ERR("%s Callback function already set.", data->name);
		return -EALREADY;
	}
	data->func_ret_cb = cb;
@@ -315,16 +383,20 @@ int dual_sync_flush(struct AresProtocol *protocol, sync_table_t *pack)
		return -EUNATCH;
	}
	if (pack == NULL) {
		LOG_ERR("Sync pack is NULL.");
		LOG_ERR("%s Sync pack is NULL.", data->name);
		return -EINVAL;
	}
	memcpy(pack->buf + SYNC_DATA_IDX, pack->data, pack->len);

	// LOG_HEXDUMP_DBG(pack->buf, pack->len + SYNC_FRAME_LENGTH_OFFSET, "Sync data to
	// send:");
	size_t frame_len = pack->len + SYNC_FRAME_LENGTH_OFFSET;
	if (data->crc_enabled) {
		add_crc16(pack->buf, pack->len + SYNC_FRAME_LENGTH_OFFSET);
		frame_len += CRC16_SIZE;
	}

	struct net_buf *buf =
		alloc_and_add_data(protocol, pack->buf, pack->len + SYNC_FRAME_LENGTH_OFFSET);
	// LOG_HEXDUMP_DBG(pack->buf, frame_len, "Sync data to send:");

	struct net_buf *buf = alloc_and_add_data(protocol, pack->buf, frame_len);
	int ret = send_try_lock(protocol, buf, &pack->mutex);

	if (ret != 0) {
@@ -341,12 +413,17 @@ int dual_func_call(struct AresProtocol *protocol, uint16_t id, uint32_t arg1, ui
	if (!data->online) {
		return -EUNATCH;
	}
	size_t frame_len = FUNC_FRAME_LENGTH;
	if (data->crc_enabled) {
		frame_len += CRC16_SIZE;
	}

	struct net_buf *buf = protocol->interface->api->alloc_buf(protocol->interface);
	if (buf == NULL) {
		LOG_ERR("Failed to allocate buffer for FUNC frame.");
		LOG_ERR("%s Failed to allocate buffer for FUNC frame.", data->name);
		return -ENOMEM;
	}
	net_buf_add(buf, FUNC_FRAME_LENGTH);
	net_buf_add(buf, frame_len);
	GET_16BITS(buf->data, FUNC_HEAD_IDX) = FUNC_FRAME_HEAD;
	GET_16BITS(buf->data, FUNC_ID_IDX) = id;
	GET_32BITS(buf->data, FUNC_ARG1_IDX) = arg1;
@@ -354,9 +431,14 @@ int dual_func_call(struct AresProtocol *protocol, uint16_t id, uint32_t arg1, ui
	GET_32BITS(buf->data, FUNC_ARG3_IDX) = arg3;
	uint16_t req_id = k_uptime_get_32();
	GET_16BITS(buf->data, FUNC_REQ_IDX) = req_id;

	if (data->crc_enabled) {
		add_crc16(buf->data, FUNC_FRAME_LENGTH);
	}

	int err = protocol->interface->api->send(protocol->interface, buf);
	if (err != 0) {
		LOG_ERR("Failed to send FUNC frame.");
		LOG_ERR("%s Failed to send FUNC frame.", data->name);
		return -EBUSY;
	}
	return req_id;
@@ -367,7 +449,7 @@ sync_table_t *dual_sync_add(struct AresProtocol *protocol, uint16_t ID, uint8_t
{
	struct dual_protocol_data *data = protocol->priv_data;
	if (data->sync_cnt >= MAX_PACK_COUNT) {
		LOG_ERR("Sync pack count exceeds maximum.");
		LOG_ERR("%s Sync pack count exceeds maximum.", data->name);
		return NULL;
	}
	data->sync_table[data->sync_cnt].interface = protocol->interface;
@@ -375,11 +457,18 @@ sync_table_t *dual_sync_add(struct AresProtocol *protocol, uint16_t ID, uint8_t
	data->sync_table[data->sync_cnt].ID = ID;
	data->sync_table[data->sync_cnt].len = len;
	data->sync_table[data->sync_cnt].cb = cb;
	data->sync_table[data->sync_cnt].buf = k_heap_aligned_alloc(
		&dual_protocol_heap, 4, len + SYNC_FRAME_LENGTH_OFFSET, K_NO_WAIT);
	memset(data->sync_table[data->sync_cnt].buf, 0, len + SYNC_FRAME_LENGTH_OFFSET);

	size_t buf_size = len + SYNC_FRAME_LENGTH_OFFSET;
	if (data->crc_enabled) {
		buf_size += CRC16_SIZE;
	}

	data->sync_table[data->sync_cnt].buf =
		k_heap_aligned_alloc(&dual_protocol_heap, 4, buf_size, K_NO_WAIT);
	memset(data->sync_table[data->sync_cnt].buf, 0, buf_size);
	if (data->sync_table[data->sync_cnt].buf == NULL) {
		LOG_ERR("Failed to allocate memory for SYNC frame. Count: %d", data->sync_cnt);
		LOG_ERR("%s Failed to allocate memory for SYNC frame. Count: %d", data->name,
			data->sync_cnt);
		return NULL;
	}
	GET_16BITS(data->sync_table[data->sync_cnt].buf, SYNC_HEAD_IDX) = SYNC_FRAME_HEAD;
@@ -388,7 +477,7 @@ sync_table_t *dual_sync_add(struct AresProtocol *protocol, uint16_t ID, uint8_t
	if (cb) {
		cb(SYNC_PACK_STATUS_READ);
	}
	LOG_INF("Sync pack added: ID %x, data %p, len %d", ID, (void *)data, len);
	LOG_INF("%s Sync pack added: ID %x, data %p, len %d", data->name, ID, (void *)data, len);
	dual_sync_flush(protocol, &data->sync_table[data->sync_cnt - 1]);

	return &data->sync_table[data->sync_cnt - 1];
@@ -396,13 +485,13 @@ sync_table_t *dual_sync_add(struct AresProtocol *protocol, uint16_t ID, uint8_t

void dual_func_add(struct AresProtocol *protocol, uint16_t id, dual_trans_func_t cb)
{
	struct dual_protocol_data *data = protocol->priv_data;
	if (cb == NULL) {
		LOG_ERR("Callback function is NULL.");
		LOG_ERR("%s Callback function is NULL.", data->name);
		return;
	}
	struct dual_protocol_data *data = protocol->priv_data;
	if (data->func_cnt >= MAX_PACK_COUNT) {
		LOG_ERR("Func count exceeds maximum.");
		LOG_ERR("%s Func count exceeds maximum.", data->name);
		return;
	}
	data->func_table[data->func_cnt].id = id;
@@ -420,7 +509,7 @@ void dual_func_remove(struct AresProtocol *protocol, uint16_t header)
			return;
		}
	}
	LOG_ERR("Cannot find corresponding ID from func table.");
	LOG_ERR("%s Cannot find corresponding ID from func table.", data->name);
	return;
}

@@ -444,26 +533,43 @@ static enum frame_type get_frame_type(uint16_t header)
// 根据帧类型和数据确定帧长度
static uint16_t get_frame_length(struct AresProtocol *protocol, enum frame_type type, uint8_t *buf)
{
	struct dual_protocol_data *data = protocol->priv_data;
	uint16_t base_length = 0;

	switch (type) {
	case FRAME_TYPE_FUNC:
		return FUNC_FRAME_LENGTH;
		base_length = FUNC_FRAME_LENGTH;
		break;
	case FRAME_TYPE_ERROR:
		return ERROR_FRAME_LENGTH;
		base_length = ERROR_FRAME_LENGTH;
		break;
	case FRAME_TYPE_REPL:
		return REPL_FRAME_LENGTH;
		base_length = REPL_FRAME_LENGTH;
		break;
	case FRAME_TYPE_SYNC:
		if (buf != NULL) {
			// 需要根据sync_table中的信息确定长度
			uint16_t sync_id = GET_16BITS(buf, SYNC_ID_IDX);
			sync_table_t *pack = find_pack(protocol->priv_data, sync_id);
			if (pack != NULL) {
				return pack->len + SYNC_FRAME_LENGTH_OFFSET;
			}
				base_length = pack->len + SYNC_FRAME_LENGTH_OFFSET;
			} else {
				return 0; // 无法确定长度
			}
		} else {
			return 0; // 无法确定长度
		}
		break;
	default:
		return 0;
	}

	// 如果启用CRC,增加CRC16长度
	if (data->crc_enabled) {
		base_length += CRC16_SIZE;
	}

	return base_length;
}

// 重置状态机
@@ -483,21 +589,35 @@ static void process_complete_frame(struct AresProtocol *protocol)
	uint8_t *buf = data->rx_buffer;
	uint16_t len = data->rx_buffer_pos;

	LOG_DBG("Processing complete frame: type %d, length %d", data->current_frame_type, len);
	LOG_DBG("%s Processing complete frame: type %d, length %d", data->name,
		data->current_frame_type, len);

	// 如果启用CRC,先验证CRC
	if (data->crc_enabled) {
		if (!verify_crc16(buf, len)) {
			LOG_ERR("%s CRC16 verification failed for frame type %d", data->name,
				data->current_frame_type);
			reset_parser_state(data);
			return;
		}
		// CRC验证通过,调整帧长度(去除CRC部分)
		len -= CRC16_SIZE;
	}

	switch (data->current_frame_type) {
	case FRAME_TYPE_FUNC:
		if (len != FUNC_FRAME_LENGTH) {
			LOG_ERR("FUNC frame length mismatch: %d vs %d", FUNC_FRAME_LENGTH, len);
			LOG_ERR("%s FUNC frame length mismatch: %d vs %d", data->name,
				FUNC_FRAME_LENGTH, len);
			error_handle(protocol, GET_16BITS(buf, FUNC_ID_IDX), UNKNOWN_TAIL);
			break;
		}
		func_table_t *map = find_func(data, GET_16BITS(buf, FUNC_ID_IDX));
		LOG_HEXDUMP_DBG(buf, len, "FUNC frame received");
		LOG_DBG("FUNC frame received: ID %x", GET_16BITS(buf, FUNC_ID_IDX));
		LOG_DBG("%s FUNC frame received: ID %x", data->name, GET_16BITS(buf, FUNC_ID_IDX));
		if (map == NULL) {
			LOG_ERR("Cannot find corresponding ID 0x%x in func mappings.",
				GET_16BITS(buf, FUNC_ID_IDX));
			LOG_ERR("%s Cannot find corresponding ID 0x%x in func mappings.",
				data->name, GET_16BITS(buf, FUNC_ID_IDX));
			LOG_HEXDUMP_DBG(buf, len, "FUNC frame");
			// error_handle(protocol, GET_16BITS(buf, FUNC_ID_IDX), UNKNOWN_FUNC);
			break;
@@ -510,11 +630,12 @@ static void process_complete_frame(struct AresProtocol *protocol)
		break;
	case FRAME_TYPE_SYNC:
		parse_sync(protocol, buf, len);
		LOG_DBG("SYNC frame received: ID %x", GET_16BITS(buf, SYNC_ID_IDX));
		LOG_DBG("%s SYNC frame received: ID %x", data->name, GET_16BITS(buf, SYNC_ID_IDX));
		break;
	case FRAME_TYPE_ERROR:
		if (len != ERROR_FRAME_LENGTH) {
			LOG_ERR("ERROR frame length mismatch: %d vs %d", ERROR_FRAME_LENGTH, len);
			LOG_ERR("%s ERROR frame length mismatch: %d vs %d", data->name,
				ERROR_FRAME_LENGTH, len);
			LOG_HEXDUMP_ERR(buf, len, "ERROR frame");
			error_handle(protocol, GET_16BITS(buf, ERROR_ID_IDX), UNKNOWN_TAIL);
			break;
@@ -525,7 +646,7 @@ static void process_complete_frame(struct AresProtocol *protocol)
		parse_repl(protocol, buf, len);
		break;
	default:
		LOG_ERR("Unknown frame type: %d", data->current_frame_type);
		LOG_ERR("%s Unknown frame type: %d", data->name, data->current_frame_type);
		LOG_HEXDUMP_ERR(buf, len, "Unknown frame");
		break;
	}
@@ -564,7 +685,7 @@ parse:
			data->expected_frame_length =
				get_frame_length(protocol, data->current_frame_type, NULL);
			if (data->expected_frame_length == 0) {
				LOG_ERR("Cannot determine frame length for type %d",
				LOG_ERR("%s Cannot determine frame length for type %d", data->name,
					data->current_frame_type);
				reset_parser_state(data);
				break;
@@ -584,7 +705,7 @@ parse:
	case PARSER_STATE_RECEIVING_DATA:
		// 接收数据字节
		if (data->rx_buffer_pos >= sizeof(data->rx_buffer)) {
			LOG_ERR("RX buffer overflow");
			LOG_ERR("%s RX buffer overflow", data->name);
			reset_parser_state(data);
			break;
		}
@@ -596,8 +717,8 @@ parse:
			data->expected_frame_length = get_frame_length(
				protocol, data->current_frame_type, data->rx_buffer);
			if (data->expected_frame_length == 0) {
				LOG_ERR("Cannot determine SYNC frame length with ID: 0x%x",
					GET_16BITS(data->rx_buffer, SYNC_ID_IDX));
				LOG_ERR("%s Cannot determine SYNC frame length with ID: 0x%x",
					data->name, GET_16BITS(data->rx_buffer, SYNC_ID_IDX));
				reset_parser_state(data);
			}
		}
@@ -612,7 +733,7 @@ parse:

	case PARSER_STATE_FRAME_COMPLETE:
		// 这种状态不应该出现,重置状态机
		LOG_ERR("Unexpected byte in FRAME_COMPLETE state");
		LOG_ERR("%s Unexpected byte in FRAME_COMPLETE state", data->name);
		reset_parser_state(data);
		process_byte(protocol, byte); // 重新处理这个字节
		break;
@@ -643,8 +764,8 @@ void ares_dual_protocol_handle_byte(struct AresProtocol *protocol, uint8_t byte)

int ares_dual_protocol_init(struct AresProtocol *protocol)
{
	LOG_INF("Dual protocol init");
	struct dual_protocol_data *data = protocol->priv_data;
	LOG_INF("%s Dual protocol init", data->name);
	k_mutex_init(&data->err_frame_mutex);
	k_timer_init(&data->heart_beat_timer, usb_trans_heart_beat, NULL);
	k_timer_user_data_set(&data->heart_beat_timer, protocol);
@@ -677,13 +798,13 @@ void ares_dual_protocol_event(struct AresProtocol *protocol, enum AresProtocolEv
	// LOG_INF("event");
	struct dual_protocol_data *data = protocol->priv_data;
	if (event == ARES_PROTOCOL_EVENT_CONNECTED) {
		LOG_INF("Connection established due to event.");
		LOG_INF("%s Connection established due to event.", data->name);
		k_msleep(600);
		data->online = true;
		// 重置状态机状态
		reset_parser_state(data);
	} else if (event == ARES_PROTOCOL_EVENT_DISCONNECTED) {
		LOG_INF("Connection lost due to event.");
		LOG_INF("%s Connection lost due to event.", data->name);
		data->online = false;
		// 重置状态机状态
		reset_parser_state(data);
+36 −4
Original line number Diff line number Diff line
@@ -128,7 +128,7 @@ struct id_mapping {
	uint16_t req_id;

	struct k_mutex mutex;
	__aligned(4) uint8_t buf[REPL_FRAME_LENGTH];
	__aligned(4) uint8_t buf[REPL_FRAME_LENGTH + 2]; // +2 for potential CRC16
};

#define MAX_PACK_COUNT 8
@@ -153,7 +153,7 @@ struct dual_protocol_data {
	dual_func_ret_cb_t func_ret_cb;

	uint8_t func_tx_bckup_cnt;
	__aligned(4) uint8_t error_frame_buf[ERROR_FRAME_LENGTH];
	__aligned(4) uint8_t error_frame_buf[ERROR_FRAME_LENGTH + 2]; // +2 for potential CRC16
	
	// 状态机相关字段
	enum parser_state state;
@@ -162,6 +162,8 @@ struct dual_protocol_data {
	uint16_t rx_buffer_pos;  // 当前接收位置
	uint16_t expected_frame_length;  // 期望的帧长度
	uint16_t header_value;           // 帧头值

	bool crc_enabled;
};

int dual_ret_cb_set(struct AresProtocol *protocol, dual_func_ret_cb_t cb);
@@ -217,6 +219,36 @@ int dual_sync_flush(struct AresProtocol *protocol, sync_table_t *pack);
		.rx_buffer_pos = 0,                                                                \
		.expected_frame_length = 0,                                                        \
		.header_value = 0,                                                                 \
		.crc_enabled = false,                                                        \
	};                                                                                         \
	struct AresProtocol Protocol_name = {                                                      \
		.name = #Protocol_name,                                                            \
		.api = &Protocol_name##_api,                                                       \
		.priv_data = &Protocol_name##_data,                                                \
	};

	#define DUAL_PROPOSE_PROTOCOL_DEFINE_CRC(Protocol_name)                                                \
	struct AresProtocolAPI Protocol_name##_api = {                                             \
		.handle = ares_dual_protocol_handle,                                               \
		.handle_byte = ares_dual_protocol_handle_byte,                                     \
		.event = ares_dual_protocol_event,                                                 \
		.init = ares_dual_protocol_init,                                                   \
	};                                                                                         \
	struct dual_protocol_data Protocol_name##_data = {                                         \
		.name = #Protocol_name,                                                            \
		.heart_beat_timer = {0},                                                           \
		.err_frame_mutex = {0},                                                            \
		.func_cnt = 0,                                                                     \
		.sync_cnt = 0,                                                                     \
		.func_tx_bckup_msgq = {0},                                                         \
		.online = false,                                                                   \
		.func_tx_bckup_cnt = 0,                                                            \
		.state = PARSER_STATE_IDLE,                                                        \
		.current_frame_type = FRAME_TYPE_UNKNOWN,                                          \
		.rx_buffer_pos = 0,                                                                \
		.expected_frame_length = 0,                                                        \
		.header_value = 0,                                                                 \
		.crc_enabled = true,                                                        \
	};                                                                                         \
	struct AresProtocol Protocol_name = {                                                      \
		.name = #Protocol_name,                                                            \