diff options
author | lonkaars <loek@pipeframe.xyz> | 2022-10-27 16:50:01 +0200 |
---|---|---|
committer | lonkaars <loek@pipeframe.xyz> | 2022-10-27 16:50:01 +0200 |
commit | 12704850c9f4cf7410ecf85116da9d23fc2ce6d6 (patch) | |
tree | e4bf351d74b2292276a007793d14c3cc57a95963 /stm32f091 | |
parent | ea748e6c6e268ec46924ac4e3b9436e094668541 (diff) |
server to protocol code
Diffstat (limited to 'stm32f091')
-rw-r--r-- | stm32f091/consts.h | 1 | ||||
-rw-r--r-- | stm32f091/server.c | 73 | ||||
-rw-r--r-- | stm32f091/server.h | 34 | ||||
-rw-r--r-- | stm32f091/test.c | 1 |
4 files changed, 101 insertions, 8 deletions
diff --git a/stm32f091/consts.h b/stm32f091/consts.h index b3bc2e2..0a8acec 100644 --- a/stm32f091/consts.h +++ b/stm32f091/consts.h @@ -3,6 +3,7 @@ #include "wifi.h" #define WS_SERVER_PORT "80" +#define WS_SERVER_MAX_CHANNELS 2 #define WS_DMA_RX_BUFFER_SIZE 100 #define WS_DMA_TX_BUFFER_SIZE 100 diff --git a/stm32f091/server.c b/stm32f091/server.c index aa2ff8b..a06727c 100644 --- a/stm32f091/server.c +++ b/stm32f091/server.c @@ -3,17 +3,46 @@ #include <stdlib.h> #include <string.h> +#include "../shared/protocol.h" #include "esp8266.h" #include "server.h" #include "setup.h" #include "consts.h" static ws_s_server_parser g_ws_server_parser = { - .counter = 0, .last_response = WS_SERVER_RC_NONE, .mode = WS_SERVER_LM_IDLE, + + .current_channel = 0, + .channel_data_length = 0, + .channel_data_counter = 0, + .channel_listen_mode = WS_SERVER_CL_DATA_LENGTH, }; +static ws_s_protocol_req_parser_state* g_ws_protocol_parsers[WS_SERVER_MAX_CHANNELS] = {0}; + + +void ws_server_req_parse_byte(unsigned int channel, uint8_t byte, bool ignore) { + if (ignore) return; + if (channel >= WS_SERVER_MAX_CHANNELS) return; + + if (g_ws_protocol_parsers[channel] == NULL) { + g_ws_protocol_parsers[channel] = ws_protocol_req_parser_alloc(); + } + + ws_protocol_parse_req_byte(g_ws_protocol_parsers[channel], byte); +} + +void ws_server_req_finish(unsigned int channel, bool ignore) { + if (ignore) return; + if (channel >= WS_SERVER_MAX_CHANNELS) return; + + if (g_ws_protocol_parsers[channel] != NULL) { + ws_protocol_req_parser_free(g_ws_protocol_parsers[channel]); + g_ws_protocol_parsers[channel] = NULL; + } +} + // TODO: next_few_bytes_are assumes that the complete search string is in the // buffer, so won't work for buffer cutoffs #define next_few_bytes_are(code) (i + sizeof(code) - 1 < size && strncmp((char*)&data[i], code, sizeof(code) - 1) == 0) @@ -37,7 +66,7 @@ void ws_server_req_incoming(uint8_t* data, size_t size) { if (next_few_bytes_are("OK")) { code_got = true; g_ws_server_parser.last_response = WS_SERVER_RC_OK; - } else if (next_few_bytes_are("ERROR")) { + } else if (next_few_bytes_are("ERROR") || next_few_bytes_are("FAIL")) { code_got = true; g_ws_server_parser.last_response = WS_SERVER_RC_ERR; } else if (next_few_bytes_are("busy p...")) { @@ -55,14 +84,46 @@ void ws_server_req_incoming(uint8_t* data, size_t size) { break; } case WS_SERVER_LM_IPD_LISTENING: { - // +IPD,0,15:last-records 5\n + switch (g_ws_server_parser.channel_listen_mode) { + case WS_SERVER_CL_CHANNEL_ID: { + if (byte == ',') { + g_ws_server_parser.channel_listen_mode = WS_SERVER_CL_DATA_LENGTH; + break; + } + g_ws_server_parser.current_channel *= 10; + g_ws_server_parser.current_channel += byte - '0'; // ascii to int + break; + } + case WS_SERVER_CL_DATA_LENGTH: { + if (byte == ':') { + g_ws_server_parser.channel_listen_mode = WS_SERVER_CL_DATA_READ; + if (g_ws_server_parser.channel_data_length > WS_PROTOCOL_CMD_BUFFER_LEN) + g_ws_server_parser.channel_data_ignore = true; + break; + } + g_ws_server_parser.current_channel *= 10; + g_ws_server_parser.current_channel += byte - '0'; // ascii to int + break; + } + case WS_SERVER_CL_DATA_READ: { + ws_server_req_parse_byte(g_ws_server_parser.current_channel, byte, g_ws_server_parser.channel_data_ignore); + g_ws_server_parser.channel_data_counter++; + if (g_ws_server_parser.channel_data_counter == g_ws_server_parser.channel_data_length) { + g_ws_server_parser.current_channel = 0; + g_ws_server_parser.channel_data_counter = 0; + g_ws_server_parser.channel_data_length = 0; + g_ws_server_parser.channel_listen_mode = WS_SERVER_CL_CHANNEL_ID; + ws_server_req_finish(g_ws_server_parser.current_channel, g_ws_server_parser.channel_data_ignore); + } + break; + } + default: {} + } break; } default: {} } } - - return; } void ws_server_send(uint8_t* data, size_t size) { @@ -72,8 +133,8 @@ void ws_server_send(uint8_t* data, size_t size) { } void ws_server_req_respond(unsigned int channel, uint8_t* data, size_t size) { - uint8_t Tx_close[]="AT+CIPCLOSE=0\r\n"; uint8_t Tx_send[]="AT+CIPSEND=0,LEN:"; + uint8_t Tx_close[]="AT+CIPCLOSE=0\r\n"; return; } diff --git a/stm32f091/server.h b/stm32f091/server.h index c5698c0..44882e4 100644 --- a/stm32f091/server.h +++ b/stm32f091/server.h @@ -12,6 +12,12 @@ typedef enum { } ws_e_server_listen_mode; typedef enum { + WS_SERVER_CL_CHANNEL_ID, /** @brief listen channel id */ + WS_SERVER_CL_DATA_LENGTH, /** @brief listen for data byte length */ + WS_SERVER_CL_DATA_READ, /** @brief listen for data and pipe to ws_protocol_parse_req_byte */ +} ws_e_channel_listen_mode; + +typedef enum { WS_SERVER_RC_NONE = -1, WS_SERVER_RC_BUSY, WS_SERVER_RC_ERR, @@ -19,9 +25,13 @@ typedef enum { } ws_e_server_response_code; typedef struct { - int counter; ws_e_server_listen_mode mode; ws_e_server_response_code last_response; + unsigned int current_channel; + unsigned int channel_data_length; + unsigned int channel_data_counter; + ws_e_channel_listen_mode channel_listen_mode; + bool channel_data_ignore; } ws_s_server_parser; /** @@ -42,3 +52,25 @@ void ws_server_req_respond(unsigned int channel, uint8_t* data, size_t size); /** @brief send data to esp, waiting until server returns to idle mode */ void ws_server_send(uint8_t* data, size_t size); +/** + * @brief parse byte from channel + * + * automatically creates parser struct and passes data onto protocol parser + * functions + * + * @param channel request channel + * @param byte data byte + * @param ignore ignore mode + */ +void ws_server_req_parse_byte(unsigned int channel, uint8_t byte, bool ignore); + +/** + * @brief close connection + * + * deallocates any parser struct that were automatically created in + * ws_server_req_parse_byte + * + * @param channel request channel + * @param ignore ignore mode + */ +void ws_server_req_finish(unsigned int channel, bool ignore); diff --git a/stm32f091/test.c b/stm32f091/test.c index c5e43da..ae48ef7 100644 --- a/stm32f091/test.c +++ b/stm32f091/test.c @@ -6,7 +6,6 @@ #include "setup.h" void ws_test_write_task() { - // uint8_t data[] = "AT+CIPMUX=1+CWMODE=1+CWJAP=\"" WS_ESP8266_WLAN_SSID "\",\"" WS_ESP8266_WLAN_PASSWD "\"\r\n"; uint8_t cmd1[] = "AT+CIPMUX=1\r\n"; uint8_t cmd2[] = "AT+CWMODE=1\r\n"; uint8_t cmd3[] = "AT+CWJAP=\"" WS_ESP8266_WLAN_SSID "\",\"" WS_ESP8266_WLAN_PASSWD "\"\r\n"; |