Browse Source

Дополнительные проверки при работе с пакетом от ESP

Откат в обработчике serial
pull/5/head
Sergey 4 years ago
parent
commit
b261e886ef
  1. 8
      Marlin/src/core/serial.h
  2. 2
      Marlin/src/gcode/gcode.cpp
  3. 133
      Marlin/src/module/mks_wifi/mks_wifi.cpp
  4. 8
      Marlin/src/module/mks_wifi/mks_wifi.h

8
Marlin/src/core/serial.h

@ -57,7 +57,7 @@ extern uint8_t marlin_debug_flags;
#define SERIAL_OUT(WHAT, V...) (void)CAT(MYSERIAL,SERIAL_CATCHALL).WHAT(V)
#else
#define SERIAL_OUT(WHAT, V...) do{ \
if (!serial_port_index || serial_port_index == SERIAL_BOTH || serial_port_index == -1) (void)MYSERIAL0.WHAT(V); \
if (!serial_port_index || serial_port_index == SERIAL_BOTH) (void)MYSERIAL0.WHAT(V); \
if ( serial_port_index) (void)MYSERIAL1.WHAT(V); \
}while(0)
#endif
@ -75,7 +75,7 @@ extern uint8_t marlin_debug_flags;
#if ENABLED(MKS_WIFI)
#define SERIAL_ECHO(x) do{ \
if (!serial_port_index || serial_port_index == SERIAL_BOTH || serial_port_index == -1 ) SERIAL_OUT(print, x); \
if (!serial_port_index || serial_port_index == SERIAL_BOTH ) SERIAL_OUT(print, x); \
if ( serial_port_index) mks_wifi_print(x); \
}while(0)
#else
@ -86,7 +86,7 @@ extern uint8_t marlin_debug_flags;
#if ENABLED(MKS_WIFI)
#define SERIAL_ECHOLN(x) do{ \
if (!serial_port_index || serial_port_index == SERIAL_BOTH || serial_port_index == -1) SERIAL_OUT(println, x); \
if (!serial_port_index || serial_port_index == SERIAL_BOTH ) SERIAL_OUT(println, x); \
if ( serial_port_index) mks_wifi_println(x); \
}while(0)
#else
@ -122,7 +122,7 @@ extern uint8_t marlin_debug_flags;
#if ENABLED(MKS_WIFI)
#define SERIAL_CHAR(V...) do{ \
if (!serial_port_index || serial_port_index == SERIAL_BOTH || serial_port_index == -1) _CHAR_N(NUM_ARGS(V),V); \
if (!serial_port_index || serial_port_index == SERIAL_BOTH ) _CHAR_N(NUM_ARGS(V),V); \
if ( serial_port_index) mks_wifi_print_var(NUM_ARGS(V),V); \
}while(0)
#else

2
Marlin/src/gcode/gcode.cpp

@ -245,7 +245,7 @@ void GcodeSuite::dwell(millis_t time) {
void GcodeSuite::process_parsed_command(const bool no_ok/*=false*/) {
KEEPALIVE_STATE(IN_HANDLER);
//DEBUG("Cmd %c %d",parser.command_letter,parser.codenum);
DEBUG("Cmd %c %d",parser.command_letter,parser.codenum);
// Handle a known G, M, or T
switch (parser.command_letter) {

133
Marlin/src/module/mks_wifi/mks_wifi.cpp

@ -5,10 +5,10 @@
#include "mks_wifi_sd.h"
#include "mks_test_sdio.h"
volatile uint8_t *mks_in_buffer=shared_mem;
volatile uint8_t mks_in_buffer[MKS_IN_BUFF_SIZE];
uint8_t mks_out_buffer[MKS_OUT_BUFF_SIZE];
volatile uint8_t *esp_packet=shared_mem+ESP_PACKET_DATA_MAX_SIZE;
volatile uint8_t esp_packet[MKS_TOTAL_PACKET_SIZE];
void mks_wifi_init(void){
@ -56,7 +56,7 @@ void mks_wifi_set_param(void){
uint32_t key_len = strlen((const char *)MKS_WIFI_KEY);
memset(mks_out_buffer, 0, sizeof(MKS_OUT_BUFF_SIZE));
memset(mks_out_buffer, 0, MKS_OUT_BUFF_SIZE);
mks_out_buffer[0] = WIFI_MODE_STA;
@ -71,8 +71,10 @@ void mks_wifi_set_param(void){
esp_frame.data=mks_out_buffer;
packet_size=mks_wifi_build_packet((uint8_t *)esp_packet,&esp_frame);
//выпихнуть в uart
mks_wifi_send((uint8_t *)esp_packet, packet_size);
if(packet_size > 8){ //4 байта заголовка + 2 байта длины + хвост + название сети и пароль
//выпихнуть в uart
mks_wifi_send((uint8_t *)esp_packet, packet_size);
};
}
/*
@ -93,6 +95,12 @@ void mks_wifi_out_add(uint8_t *data, uint32_t size){
esp_frame.data=mks_out_buffer;
packet_size=mks_wifi_build_packet((uint8_t *)esp_packet,&esp_frame);
if(packet_size == 0){
ERROR("Build packet failed");
line_index=0;
memset(mks_out_buffer,0,MKS_OUT_BUFF_SIZE);
break;
}
//выпихнуть в uart
mks_wifi_send((uint8_t *)esp_packet, packet_size);
//очистить буфер
@ -108,6 +116,7 @@ void mks_wifi_out_add(uint8_t *data, uint32_t size){
ERROR("Max line size");
line_index=0;
memset(mks_out_buffer,0,MKS_OUT_BUFF_SIZE);
break;
}
}
}
@ -127,7 +136,7 @@ uint8_t mks_wifi_input(uint8_t data){
payload_size = ESP_PACKET_DATA_MAX_SIZE;
packet_start_flag=1;
packet_index=0;
memset((uint8_t*)mks_in_buffer,0,ESP_PACKET_DATA_MAX_SIZE);
memset((uint8_t*)mks_in_buffer,0,MKS_IN_BUFF_SIZE);
}
if(packet_start_flag){
@ -140,9 +149,26 @@ uint8_t mks_wifi_input(uint8_t data){
if(packet_index == 3){
payload_size = uint16_t(mks_in_buffer[3] << 8) | mks_in_buffer[2];
if(payload_size > ESP_PACKET_DATA_MAX_SIZE){
ERROR("Payload size too big");
packet_start_flag=0;
packet_index=0;
memset((uint8_t*)mks_in_buffer,0,MKS_IN_BUFF_SIZE);
return 1;
}
}
if( (packet_index >= (payload_size+4)) || (packet_index >= ESP_PACKET_DATA_MAX_SIZE) ){
if(mks_wifi_check_packet((uint8_t *)mks_in_buffer)){
ERROR("Packet check failed");
packet_start_flag=0;
packet_index=0;
return 1;
}
esp_frame.type = packet_type;
esp_frame.dataLen = payload_size;
esp_frame.data = (uint8_t*)&mks_in_buffer[4];
@ -166,7 +192,16 @@ uint8_t mks_wifi_input(uint8_t data){
(packet_index >= 4) &&
(packet_index < payload_size+5)
){
ret_val=0;
if(!check_char_allowed(data)){
ret_val=0;
}else{
ERROR("Char not allowed: %0X %c",data,data);
packet_start_flag=0;
packet_index=0;
return 1;
}
}
if(packet_start_flag){
@ -176,13 +211,64 @@ uint8_t mks_wifi_input(uint8_t data){
return ret_val;
}
/*
Проверяет, что символы из текстового диапазона
для G-code команд
*/
uint8_t check_char_allowed(char data){
if( data == 0x0a || data == 0x0d){
return 0;
}
if( (data >= 0x20) && (data <= 0x7E) ){
return 0;
}
return 1;
}
/*
Проверяет пакет на корректность:
наличие заголовка
наличие "хвоста"
длина пакета
*/
uint8_t mks_wifi_check_packet(uint8_t *in_data){
uint16_t payload_size;
uint16_t tail_index;
if(in_data[0] != ESP_PROTOC_HEAD){
ERROR("Packet head mismatch");
return 1;
}
payload_size = uint16_t(in_data[3] << 8) | in_data[2];
if(payload_size > ESP_PACKET_DATA_MAX_SIZE){
ERROR("Payload size mismatch");
return 1;
}
tail_index = payload_size + 4;
if(in_data[tail_index] != ESP_PROTOC_TAIL ){
ERROR("Packet tail mismatch");
return 1;
}
return 0;
}
void mks_wifi_parse_packet(ESP_PROTOC_FRAME *packet){
static uint8_t show_ip_once=0;
char str[50];
char str[100];
switch(packet->type){
case ESP_TYPE_NET:
memset(str,0,100);
if(packet->data[6] == ESP_NET_WIFI_CONNECTED){
if(show_ip_once==0){
show_ip_once=1;
@ -192,12 +278,16 @@ void mks_wifi_parse_packet(ESP_PROTOC_FRAME *packet){
SERIAL_ECHOLN((char*)str);
//Вывод имени сети
memcpy(str,&packet->data[9],packet->data[8]);
str[packet->data[8]]=0;
SERIAL_ECHO_START();
SERIAL_ECHO("WIFI: ");
SERIAL_ECHOLN((char*)str);
if(packet->data[8] < 100){
memcpy(str,&packet->data[9],packet->data[8]);
str[packet->data[8]]=0;
SERIAL_ECHO_START();
SERIAL_ECHO("WIFI: ");
SERIAL_ECHOLN((char*)str);
}else{
DEBUG("Network NAME too long");
}
}
DEBUG("[Net] connected, IP: %d.%d.%d.%d",packet->data[0],packet->data[1],packet->data[2],packet->data[3]);
}else if(packet->data[6] == ESP_NET_WIFI_EXCEPTION){
@ -249,7 +339,7 @@ void mks_wifi_print(const char *s){
}
void mks_wifi_print(int i){
char str[14];
char str[100];
sprintf(str,"%d",i);
mks_wifi_out_add((uint8_t *)str, strnlen((char *)str,ESP_PACKET_DATA_MAX_SIZE));
@ -261,16 +351,16 @@ void mks_wifi_println(const char *s){
}
void mks_wifi_println(float f){
char str[30];
char str[100];
sprintf(str,"%ld\n",(uint32_t)f);
mks_wifi_out_add((uint8_t *)str, strnlen((char *)str,ESP_PACKET_DATA_MAX_SIZE));
}
uint16_t mks_wifi_build_packet(uint8_t *packet, ESP_PROTOC_FRAME *esp_frame){
uint16_t packet_size;
uint16_t packet_size=0;
memset(packet,0,ESP_PACKET_DATA_MAX_SIZE);
memset(packet,0,MKS_TOTAL_PACKET_SIZE);
packet[0] = ESP_PROTOC_HEAD;
packet[1] = esp_frame->type;
@ -280,6 +370,11 @@ uint16_t mks_wifi_build_packet(uint8_t *packet, ESP_PROTOC_FRAME *esp_frame){
packet_size = esp_frame->dataLen + 4;
if(packet_size > MKS_TOTAL_PACKET_SIZE){
ERROR("ESP packet too big");
return 0;
}
if(esp_frame->type != ESP_TYPE_NET){
packet[packet_size++] = 0x0d;
packet[packet_size++] = 0x0a;
@ -294,7 +389,7 @@ uint16_t mks_wifi_build_packet(uint8_t *packet, ESP_PROTOC_FRAME *esp_frame){
void mks_wifi_send(uint8_t *packet, uint16_t size){
safe_delay(10);
//safe_delay(10);
for( uint32_t i=0; i < (uint32_t)(size+1); i++){
while(MYSERIAL1.availableForWrite()==0){
safe_delay(10);

8
Marlin/src/module/mks_wifi/mks_wifi.h

@ -8,7 +8,10 @@
#ifdef MKS_WIFI
#define MKS_OUT_BUFF_SIZE 256
#define MKS_OUT_BUFF_SIZE (ESP_PACKET_DATA_MAX_SIZE)
#define MKS_IN_BUFF_SIZE (ESP_PACKET_DATA_MAX_SIZE + 30)
#define MKS_TOTAL_PACKET_SIZE (ESP_PACKET_DATA_MAX_SIZE+10)
//#define SDIO_TEST_AT_STARTUP
//#define LIST_FILES_AT_STARTUP (uint8_t)1
@ -52,6 +55,9 @@ void mks_wifi_out_add(uint8_t *data, uint32_t size);
uint16_t mks_wifi_build_packet(uint8_t *packet, ESP_PROTOC_FRAME *esp_frame);
uint8_t mks_wifi_check_packet(uint8_t *in_data);
uint8_t check_char_allowed(char data);
void mks_wifi_send(uint8_t *packet, uint16_t size);
void mks_wifi_print_var(uint8_t count, ...);

Loading…
Cancel
Save