diff options
author | lonkaars <loek@pipeframe.xyz> | 2022-10-28 12:14:50 +0200 |
---|---|---|
committer | lonkaars <loek@pipeframe.xyz> | 2022-10-28 12:14:50 +0200 |
commit | 4cd465332087e4ab12709f28fae55df10e1a1154 (patch) | |
tree | e00f602321d589acdd6a6f2ae162c5d891902882 | |
parent | f37af779bb836faa7571b7d47036f36f71319aca (diff) |
add todo.md, fixed segfault in protocol.c, and fix response sending
-rw-r--r-- | shared/protocol.c | 5 | ||||
-rw-r--r-- | stm32f091/consts.h | 2 | ||||
-rw-r--r-- | stm32f091/protocol.c | 13 | ||||
-rw-r--r-- | stm32f091/server.c | 18 | ||||
-rw-r--r-- | stm32f091/todo.md | 10 |
5 files changed, 31 insertions, 17 deletions
diff --git a/shared/protocol.c b/shared/protocol.c index fcc84a4..e17a728 100644 --- a/shared/protocol.c +++ b/shared/protocol.c @@ -99,10 +99,11 @@ ws_s_protocol_req_parser_state* ws_protocol_req_parser_alloc() { void ws_protocol_req_cmd_init(ws_s_protocol_req_parser_state* state) { state->target = malloc(sizeof(ws_s_protocol_parsed_req_cmd) + sizeof(char*) * state->arg_len); - for (unsigned int i = 0; i < state->arg_len; i++) + unsigned int args = WS_MIN(state->arg_len, WS_PROTOCOL_CMD_MAX_ARGUMENTS); + for (unsigned int i = 0; i < args; i++) state->target->argv[i] = malloc(sizeof(char) * (state->args_len[i] + 1)); - state->target->argc = state->arg_len; + state->target->argc = args; unsigned int head = 0; for (unsigned int i = 0; i < state->arg_len; i++) { diff --git a/stm32f091/consts.h b/stm32f091/consts.h index 3d2ef5a..955ceea 100644 --- a/stm32f091/consts.h +++ b/stm32f091/consts.h @@ -6,7 +6,7 @@ #define WS_SERVER_MAX_CHANNELS 4 #define WS_DMA_RX_BUFFER_SIZE 100 -#define WS_DMA_TX_BUFFER_SIZE 1024 +#define WS_DMA_TX_BUFFER_SIZE 100 #define WS_PINOUT_I2C_SDA_PIN GPIO_PIN_9 #define WS_PINOUT_I2C_SDA_PORT GPIOB diff --git a/stm32f091/protocol.c b/stm32f091/protocol.c index 3a8d78b..21e6527 100644 --- a/stm32f091/protocol.c +++ b/stm32f091/protocol.c @@ -13,19 +13,21 @@ void ws_protocol_res_last_records(ws_s_protocol_parsed_req_cmd* parsed_cmd, ws_s if (!send) { response->success = WS_PROTOCOL_CMD_RETURN_OK; - if (sscanf(parsed_cmd->argv[1], "%u", &record_amount) < 1) response->success = WS_PROTOCOL_CMD_RETURN_ERROR; - record_amount = WS_MIN(record_amount, ws_backlog_get_record_count()); response->csh = true; response->msg = ws_bin_s_alloc(0); - response->msg->bytes = strlen(response_header) + response_line_size * record_amount; + response->msg->bytes = 0; + record_amount = WS_MIN(record_amount, ws_backlog_get_record_count()); + if (sscanf(parsed_cmd->argv[1], "%u", &record_amount) < 1) response->success = WS_PROTOCOL_CMD_RETURN_ERROR; + else response->msg->bytes = strlen(response_header) + response_line_size * record_amount; ws_server_req_respond_start(0, response->msg->bytes + ws_protocol_get_header_size(response)); } else { + if (response->success == WS_PROTOCOL_CMD_RETURN_ERROR) return; ws_protocol_send_data(response_header, strlen(response_header)); - char line[response_line_size + 1]; // +1 for null terminator -> sprintf + char line[response_line_size]; for (unsigned int i = 0; i < record_amount; i++) { ws_s_backlog_record* record = ws_backlog_get_last_record(i); sprintf(line, "%04x,%02x,%02x,%02x\n", record->id, record->sens_temperature, record->sens_humidity, record->sens_atm_pressure); - ws_protocol_send_data(line, response_line_size); + ws_protocol_send_data(line, response_line_size - 1); // remove string terminator } ws_protocol_send_data("\r\n", 2); // test } @@ -33,5 +35,4 @@ void ws_protocol_res_last_records(ws_s_protocol_parsed_req_cmd* parsed_cmd, ws_s void ws_protocol_send_data(const char* data, unsigned int length) { ws_server_buffer_send_append((uint8_t*) data, length); - // HAL_UART_Transmit(&huart1, (uint8_t*) data, length, HAL_MAX_DELAY); } diff --git a/stm32f091/server.c b/stm32f091/server.c index 8001380..1419da8 100644 --- a/stm32f091/server.c +++ b/stm32f091/server.c @@ -20,7 +20,7 @@ ws_s_server_parser g_ws_server_parser = { }; static ws_s_protocol_req_parser_state* g_ws_protocol_parsers[WS_SERVER_MAX_CHANNELS] = {0}; -static unsigned int g_ws_esp8266_dma_tx_buffer_index = 0; +static unsigned int g_ws_esp8266_dma_tx_buffer_size = 0; void ws_server_req_parse_byte(unsigned int channel, uint8_t byte, bool ignore) { if (ignore) return; @@ -126,7 +126,7 @@ void ws_server_req_incoming(uint8_t* data, size_t size) { break; } case WS_SERVER_LM_CIPSEND_LISTENING: { - if (next_few_bytes_are("SEND OK")) { + if (next_few_bytes_are("SEND OK") || next_few_bytes_are("ERROR")) { ws_server_req_respond_end(0); // g_ws_server_parser.mode = WS_SERVER_LM_IDLE; } @@ -146,23 +146,25 @@ void ws_server_send(uint8_t* data, size_t size) { void ws_server_buffer_send_append(uint8_t* data, size_t size) { // TODO: buffer overrun protection // while (!__HAL_DMA_GET_FLAG(&hdma_usart1_tx, DMA_FLAG_TC2)); // make sure buffer isn't used - strncpy((char*) &g_ws_esp8266_dma_tx_buffer[g_ws_esp8266_dma_tx_buffer_index], (char*) data, size); // append string - g_ws_esp8266_dma_tx_buffer_index += size; // shift head + strncpy((char*) &g_ws_esp8266_dma_tx_buffer[g_ws_esp8266_dma_tx_buffer_size], (char*) data, size); // append string + g_ws_esp8266_dma_tx_buffer_size += size; // shift head } +// TODO: refactor this void ws_server_buffer_send_finish() { #ifdef WS_DBG_PRINT_ESP_OVER_USART2 uint8_t green[] = { 0x1b, 0x5b, 0x33, 0x32, 0x6d }; HAL_UART_Transmit(&huart2, green, sizeof(green), 100); - HAL_UART_Transmit(&huart2, g_ws_esp8266_dma_tx_buffer, strlen((char*) g_ws_esp8266_dma_tx_buffer), 100); + HAL_UART_Transmit(&huart2, g_ws_esp8266_dma_tx_buffer, g_ws_esp8266_dma_tx_buffer_size, 100); #endif - HAL_UART_Transmit_DMA(&huart1, g_ws_esp8266_dma_tx_buffer, strlen((char*) g_ws_esp8266_dma_tx_buffer)); + HAL_UART_Transmit_DMA(&huart1, g_ws_esp8266_dma_tx_buffer, g_ws_esp8266_dma_tx_buffer_size); __HAL_UART_ENABLE_IT(&huart1, UART_IT_TXE); - g_ws_esp8266_dma_tx_buffer_index = 0; + g_ws_esp8266_dma_tx_buffer_size = 0; while (!__HAL_DMA_GET_FLAG(&hdma_usart1_tx, DMA_FLAG_TC2)); } +// TODO: refactor this void ws_server_req_respond_start(unsigned int channel, size_t size) { char* cmd = NULL; size_t len = asiprintf(&cmd, "AT+CIPSEND=%d,%d\r\n", channel, size); @@ -171,6 +173,7 @@ void ws_server_req_respond_start(unsigned int channel, size_t size) { while (!__HAL_DMA_GET_FLAG(&hdma_usart1_tx, DMA_FLAG_TC2)); } +// TODO: refactor this void ws_server_req_respond_end(unsigned int channel) { char* cmd = NULL; size_t len = asiprintf(&cmd, "AT+CIPCLOSE=%d\r\n", channel); @@ -178,4 +181,3 @@ void ws_server_req_respond_end(unsigned int channel) { ws_esp8266_send((uint8_t*) cmd, len); while (!__HAL_DMA_GET_FLAG(&hdma_usart1_tx, DMA_FLAG_TC2)); } - diff --git a/stm32f091/todo.md b/stm32f091/todo.md new file mode 100644 index 0000000..767d745 --- /dev/null +++ b/stm32f091/todo.md @@ -0,0 +1,10 @@ +- handle errors from `AT+CIPSEND`, these look like this: + ``` + > AT0,CONNECT + + > +IPD,0,15:last-records 5<0a> + < AT+CIPSEND=0,125 + > AT+CIPSEND=0,125 + + > ERROR + ``` |