183 lines
5.0 KiB
C
183 lines
5.0 KiB
C
#include "public.h"
|
||
#include "esp_log.h"
|
||
#include "stream.h"
|
||
|
||
static const char *TAG = "RECV";
|
||
|
||
typedef struct
|
||
{
|
||
uint8_t Funcode;//功能码
|
||
uint8_t Cmdcode;//命令码
|
||
uint16_t waitcount;
|
||
void (* const codefunc)(uint8_t *buffer); //对应code执行的命令
|
||
}DECODERCMD;
|
||
|
||
void Connection_test(uint8_t *buffer);
|
||
void Transfer_file(uint8_t *buffer);
|
||
|
||
const DECODERCMD Cmd_Receiver[] = {
|
||
{0x01,0x01,0x05,Connection_test},
|
||
{0x02,0x01,0x03,Transfer_file},
|
||
{0,0,0,NULL},
|
||
};
|
||
|
||
void HandleCmd(const DECODERCMD* pCmd,uint8_t ch)
|
||
{
|
||
uint8_t i,j, result;
|
||
uint8_t PrnCmd[16];
|
||
|
||
result = 0;
|
||
for(i = 0; i < 256 ; i++)
|
||
{
|
||
if (pCmd[i].Funcode == 0) {
|
||
break;
|
||
} else if(pCmd[i].Funcode == ch) {
|
||
|
||
if (pCmd[i].waitcount > 0)
|
||
{
|
||
if (WaitPrnBufBytes(&uart_interface_data,pCmd[i].waitcount, 1000, PrnCmd) != pCmd[i].waitcount)
|
||
{
|
||
result = 0;
|
||
}
|
||
}
|
||
if (pCmd[i].codefunc != NULL)
|
||
{
|
||
pCmd[i].codefunc(&PrnCmd[0]);
|
||
}
|
||
}
|
||
|
||
}
|
||
|
||
|
||
}
|
||
|
||
// CRC16 (Modbus) calculation function
|
||
uint16_t crc16_modbus(const uint8_t *data, uint16_t len) {
|
||
uint16_t crc = 0xFFFF;
|
||
for (int i = 0; i < len; i++) {
|
||
crc ^= data[i];
|
||
for (int j = 0; j < 8; j++) {
|
||
if (crc & 0x0001)
|
||
crc = (crc >> 1) ^ 0xA001;
|
||
else
|
||
crc = crc >> 1;
|
||
}
|
||
}
|
||
return crc;
|
||
}
|
||
|
||
/*
|
||
|
||
| 帧头(2字节) | 功能码(1字节) | 命令码(1字节) |长度(2字节) | 数据(N字节) | CRC16(2字节) |
|
||
例:0xAA 0x55 + 0x01 + 0x01 + 0x0008 + 数据内容 + CRC校验
|
||
*/
|
||
|
||
void Connection_test(uint8_t *buffer)
|
||
{
|
||
ESP_LOGI(TAG,"Connection_test");
|
||
if (buffer[0] == 0x01) {
|
||
|
||
}
|
||
|
||
}
|
||
|
||
/*
|
||
Funcode(0x02)
|
||
Cmdcode:(0x01)----文件名
|
||
Cmdcode:(0x02)----文件数据
|
||
*/
|
||
|
||
void Transfer_file(uint8_t *buffer)
|
||
{
|
||
uint16_t data_len = 0;
|
||
uint16_t crc_calc = 0xffff;
|
||
uint16_t crc_received = 0;
|
||
static FILE* file;
|
||
char* data_buff = NULL;
|
||
char file_name[64];
|
||
ESP_LOGI(TAG,"Transfer_file");
|
||
data_len = buffer[1] << 8 | buffer[2];
|
||
ESP_LOGI(TAG,"data_len:%d",data_len);
|
||
data_buff = malloc(data_len +10);
|
||
if(buffer[0] == 0x01) {//文件名
|
||
if (WaitPrnBufBytes(&uart_interface_data,data_len,1000,(uint8_t*)&data_buff[0]) == data_len)
|
||
{
|
||
crc_received = data_buff[data_len-1]<<8 | data_buff[data_len-2];
|
||
data_buff[data_len-2] = 0;
|
||
data_buff[data_len-1] = 0;
|
||
|
||
crc_calc = crc16_modbus((uint8_t*)&data_buff[0],data_len-2);
|
||
if (crc_calc != crc_received)
|
||
{
|
||
return;
|
||
}
|
||
sprintf(&file_name[0],"/spiffs/%s",data_buff);
|
||
ESP_LOGI(TAG,"file name:%s",file_name);
|
||
file = fopen(file_name, "ab");
|
||
if (file == NULL) {
|
||
printf("Failed to open file for writing\n");
|
||
return;
|
||
}
|
||
// fclose(file);
|
||
}
|
||
|
||
} else if(buffer[0] == 0x02) {//文件内容
|
||
// 包数 | (0xff - 包数)| 数据 | crc
|
||
// 01 | 7f | data | crc
|
||
if (WaitPrnBufBytes(&uart_interface_data,data_len,1000,(uint8_t*)&data_buff[0]) == data_len)
|
||
{
|
||
crc_received = data_buff[data_len-1]<<8 | data_buff[data_len-2];
|
||
data_buff[data_len-2] = 0;
|
||
data_buff[data_len-1] = 0;
|
||
//
|
||
if (data_buff[0] != 0xff)
|
||
{
|
||
size_t written = fwrite(&data_buff[2], 1, (data_len - 4), file);
|
||
if (written != (data_len - 4)) {
|
||
ESP_LOGE("SPIFFS", "fwrite failed, written=%d", written);
|
||
} else {
|
||
ESP_LOGI("SPIFFS", "Write success: %d bytes", written);
|
||
}
|
||
fclose(file);
|
||
}else{
|
||
// fclose(file);
|
||
}
|
||
crc_calc = crc16_modbus((uint8_t*)&data_buff[0],data_len-2);
|
||
}
|
||
}
|
||
ESP_LOGI(TAG,"crc:%X----%X",crc_calc,crc_received);
|
||
free(data_buff);
|
||
}
|
||
|
||
|
||
void Read_CMD_task(void *pvParameter)
|
||
{
|
||
uint8_t ch;
|
||
uint8_t data[24];
|
||
stream_data_t* stream_data;
|
||
stream_data = &uart_interface_data;
|
||
ESP_LOGI(TAG, "Read_CMD_task...");
|
||
while (1)
|
||
{
|
||
if (ISframeStreamEmpty(&uart_interface_data) != ESP_OK)
|
||
{
|
||
|
||
frameStreamRead(&uart_interface_data,&ch,1);
|
||
ESP_LOGI(TAG, "Read:%X",ch);
|
||
if(ch == 0xAA)
|
||
{
|
||
if (WaitPrnBufBytes(&uart_interface_data,2,1000,&data[0]) == 2)
|
||
{
|
||
if (data[0] == 0x55) {
|
||
ESP_LOGI(TAG, "---Read:%X %X",data[0],data[1]);
|
||
HandleCmd(Cmd_Receiver,data[1]);
|
||
}
|
||
}
|
||
}
|
||
// HandleRecChar(ch);
|
||
// ESP_LOG_BUFFER_HEXDUMP("Recv str: ", ch, 1, ESP_LOG_INFO);
|
||
}
|
||
vTaskDelay(10/ portTICK_PERIOD_MS);
|
||
}
|
||
|
||
} |