240 lines
7.2 KiB
C
240 lines
7.2 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 Report_transmission_progress(uint32_t file_size,uint32_t have_load)
|
||
{
|
||
float tran_schedule = 0;
|
||
uint8_t send_buff[512] = {0x55, 0xAA, 0x02, 0x04, 0x00, 0x00, 0x00, 0x00};
|
||
uint16_t data_len = 0;
|
||
// ESP_LOGI(TAG,"Report_transmission_progress");
|
||
tran_schedule = (float)have_load / file_size;
|
||
data_len = 1 + 2;
|
||
send_buff[4] = data_len & 0xff;
|
||
send_buff[5] = (data_len >> 8) & 0xff;
|
||
send_buff[6] = (uint8_t)(tran_schedule * 100);
|
||
usb_serial_jtag_write_bytes(&send_buff[0],(6 + data_len + 2),20 / portTICK_PERIOD_MS);
|
||
// ESP_LOGI(TAG,"buff size:%d %.2f %d",send_buff[6],tran_schedule,data_len);
|
||
}
|
||
|
||
|
||
uint8_t Device_SN[12];
|
||
uint8_t Device_MAC_WIFI[6];
|
||
uint8_t Device_MAC_BLE[6];
|
||
uint8_t Flash_ID[4];
|
||
uint32_t flash_id;
|
||
|
||
void Connection_test(uint8_t *buffer)
|
||
{
|
||
uint8_t send_buff[512] = {0x55, 0xAA, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00};
|
||
uint16_t data_len = 0;
|
||
ESP_LOGI(TAG,"Connection_test");
|
||
data_len = sizeof(Device_info_t) + 2;
|
||
send_buff[4] = data_len & 0xff;
|
||
send_buff[5] = (data_len >> 8) & 0xff;
|
||
list_files_in_spiffs(off_line_tool.directory, 10);
|
||
memcpy(&send_buff[6],&off_line_tool,sizeof(Device_info_t));
|
||
if (buffer[0] == 0x01) {
|
||
usb_serial_jtag_write_bytes(&send_buff[0],(6 + sizeof(Device_info_t) + 2),20 / portTICK_PERIOD_MS);
|
||
}
|
||
// ESP_LOGI(TAG,"buff size:%d %d",sizeof(Device_info_t),data_len);
|
||
}
|
||
|
||
|
||
|
||
/*
|
||
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 uint32_t file_size = 0;
|
||
static uint32_t have_load = 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;
|
||
}
|
||
|
||
file_size = data_buff[0] | data_buff[1] << 8 | data_buff[2] << 16 | data_buff[3] << 24;
|
||
sprintf(&file_name[0],"/spiffs/%s",&data_buff[4]);
|
||
ESP_LOGI(TAG,"file name:%s file size:%ld",file_name,file_size);
|
||
file = fopen(file_name, "ab");
|
||
if (file == NULL) {
|
||
printf("Failed to open file for writing\n");
|
||
return;
|
||
}
|
||
}
|
||
|
||
} else if(buffer[0] == 0x02) {//文件内容
|
||
// 包数 | (0xff - 包数)| 数据 | crc
|
||
// 01 | 00 | data | crc
|
||
if (WaitPrnBufBytes(&uart_interface_data,data_len,0xffff,(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;
|
||
have_load += data_len;
|
||
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);
|
||
}
|
||
Report_transmission_progress(file_size,have_load);
|
||
if(data_buff[0] == 0xff && data_buff[1] == 0xff)
|
||
{
|
||
have_load = 0;
|
||
fclose(file);
|
||
ESP_LOGI(TAG,"data_buff[0]:%X %X",data_buff[0],data_buff[1]);
|
||
}
|
||
// ESP_LOGI(TAG,"data_buff[0]:%X %X",data_buff[0],data_buff[1]);
|
||
crc_calc = crc16_modbus((uint8_t*)&data_buff[2],data_len-4);
|
||
}else{
|
||
ESP_LOGE(TAG,"fail:%d",data_len);
|
||
}
|
||
|
||
if(crc_calc != crc_received)
|
||
{
|
||
ESP_LOG_BUFFER_HEXDUMP("Recv str: ", &data_buff[0], 1024, ESP_LOG_INFO);
|
||
while (1)
|
||
{
|
||
vTaskDelay(100/ portTICK_PERIOD_MS);
|
||
}
|
||
|
||
}
|
||
} else if(buffer[0] == 0x03) {//删除所有文件
|
||
if (WaitPrnBufBytes(&uart_interface_data,2,0xffff,(uint8_t*)&data_buff[0]) == 2)
|
||
{
|
||
|
||
}
|
||
delete_all_file();
|
||
}
|
||
// 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,%ld,---%ld:%ld",ch,frameStreamRecLen(&uart_interface_data),uart_interface_data.read,uart_interface_data.write);
|
||
if(ch == 0xAA)
|
||
{
|
||
if (WaitPrnBufBytes(&uart_interface_data,2,0xffff,&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);
|
||
}else{
|
||
vTaskDelay(10/ portTICK_PERIOD_MS);
|
||
}
|
||
|
||
}
|
||
|
||
} |