协议修改为小端规则,并添加新命令

This commit is contained in:
OPTOC
2025-09-18 16:49:25 +08:00
parent 689a4f8285
commit f4c8fc30b4
2 changed files with 21 additions and 10 deletions

View File

@@ -38,14 +38,14 @@ int resend_encode(resend_device_t *resend_device, void *out, uint8_t cmd, void*
// retry_cnt // retry_cnt
c[4] = resend_device->tx_frame.retry_cnt; c[4] = resend_device->tx_frame.retry_cnt;
// len big endian // len big endian
c[5] = (uint8_t)((len >> 8) & 0xFF); c[5] = (uint8_t)((len) & 0xFF);
c[6] = (uint8_t)(len & 0xFF); c[6] = (uint8_t)((len >> 8) & 0xFF);
// payload // payload
if (len > 0 && data) memcpy(&c[7], data, len); if (len > 0 && data) memcpy(&c[7], data, len);
// compute crc over seq..payload (i.e. bytes p+1 .. p+5+len) // compute crc over seq..payload (i.e. bytes p+1 .. p+5+len)
uint16_t crc = fmav_crc_calculate(&c[2], 5 + len); uint16_t crc = fmav_crc_calculate(&c[2], 5 + len);
c[7 + len] = (uint8_t)((crc >> 8) & 0xFF); c[7 + len] = (uint8_t)(crc & 0xFF);
c[7 + len + 1] = (uint8_t)(crc & 0xFF); c[7 + len + 1] = (uint8_t)((crc >> 8) & 0xFF);
return (int)(len + RESEND_MAX_PAYLOAD); return (int)(len + RESEND_MAX_PAYLOAD);
} }
@@ -91,12 +91,12 @@ int resend_decode(resend_device_t *resend_device, uint8_t c)
break; break;
case RESEND_LEN_1: case RESEND_LEN_1:
resend_device->rx_frame.len = 0; resend_device->rx_frame.len = 0;
resend_device->rx_frame.len = (uint16_t)(c << 8); resend_device->rx_frame.len |= c;
fmav_crc_accumulate(&frame_crc, c); fmav_crc_accumulate(&frame_crc, c);
resend_device->status.c_state = RESEND_LEN_2; resend_device->status.c_state = RESEND_LEN_2;
break; break;
case RESEND_LEN_2: case RESEND_LEN_2:
resend_device->rx_frame.len |= c; resend_device->rx_frame.len |= (uint16_t)(c << 8);;
fmav_crc_accumulate(&frame_crc, c); fmav_crc_accumulate(&frame_crc, c);
if(resend_device->rx_frame.len > 0) if(resend_device->rx_frame.len > 0)
{ {
@@ -118,14 +118,14 @@ int resend_decode(resend_device_t *resend_device, uint8_t c)
} }
break; break;
case RESEND_CRC_1: case RESEND_CRC_1:
if((uint8_t)(frame_crc >> 8 & 0xFF) == c){ if((uint8_t)(frame_crc) == c){
resend_device->status.c_state = RESEND_CRC_2; resend_device->status.c_state = RESEND_CRC_2;
}else{ }else{
resend_device->status.c_state = RESEND_IDLE; resend_device->status.c_state = RESEND_IDLE;
} }
break; break;
case RESEND_CRC_2: case RESEND_CRC_2:
if((uint8_t)(frame_crc) == c){ if((uint8_t)(frame_crc >> 8 & 0xFF) == c){
resend_device->status.c_state = RESEND_IDLE; resend_device->status.c_state = RESEND_IDLE;
resend_device->rx_frame.crc = frame_crc; resend_device->rx_frame.crc = frame_crc;
if(resend_device->rx_frame.cmd != RESEND_CMD_DATA && resend_device->rx_frame.cmd != RESEND_CMD_ACK && resend_device->rx_frame.retry_cnt == 0) if(resend_device->rx_frame.cmd != RESEND_CMD_DATA && resend_device->rx_frame.cmd != RESEND_CMD_ACK && resend_device->rx_frame.retry_cnt == 0)
@@ -242,10 +242,17 @@ int resend_parse_data(resend_device_t *resend_device)
printf("ACK\n"); printf("ACK\n");
resend_device->status.ack_flag = 0; resend_device->status.ack_flag = 0;
break; break;
case RESEND_CMD_GET_STATUS:
resend_send_cmd(resend_device, RESEND_CMD_ACK, 0);
resend_device->handle_flag = RESEND_CMD_GET_STATUS;
break;
case RESEND_CMD_STATUS: case RESEND_CMD_STATUS:
resend_set(resend_device); resend_set(resend_device);
resend_send_cmd(resend_device, RESEND_CMD_ACK, 0); resend_send_cmd(resend_device, RESEND_CMD_ACK, 0);
break; break;
case RESEND_CMD_SET_STATUS:
resend_send_cmd(resend_device, RESEND_CMD_ACK, 0);
break;
case RESEND_CMD_DATA_ACK: case RESEND_CMD_DATA_ACK:
for (size_t i = 0; i < resend_device->rx_frame.len; i++) for (size_t i = 0; i < resend_device->rx_frame.len; i++)
{ {

View File

@@ -25,9 +25,11 @@ typedef enum
{ {
RESEND_CMD_DATA = 0x01, RESEND_CMD_DATA = 0x01,
RESEND_CMD_ACK = 0x02, RESEND_CMD_ACK = 0x02,
RESEND_CMD_STATUS = 0x03, RESEND_CMD_GET_STATUS = 0x03,
RESEND_CMD_DATA_ACK = 0x04, RESEND_CMD_STATUS = 0x04,
RESEND_CMD_SET_STATUS = 0x05,
RESEND_CMD_DATA_ACK = 0x06,
RESEND_CMD_OTA_START = 0x011, RESEND_CMD_OTA_START = 0x011,
RESEND_CMD_OTA_DATA = 0x012, RESEND_CMD_OTA_DATA = 0x012,
@@ -74,6 +76,8 @@ typedef struct
resend_frame_t tx_frame; // 发送帧 resend_frame_t tx_frame; // 发送帧
resend_status_t status; // 帧状态 resend_status_t status; // 帧状态
uint8_t handle_flag; // 接收请求帧处理标志
int (*send)(void* data, uint16_t len, int timeout); // 发送数据 int (*send)(void* data, uint16_t len, int timeout); // 发送数据
int (*recv)(void* data, uint16_t len, int timeout); // 接收数据 int (*recv)(void* data, uint16_t len, int timeout); // 接收数据
int (*get_length )(void); // 获取数据长度 int (*get_length )(void); // 获取数据长度