From 38c3d412fe6715e404d8e9f8c4cf3fbfc56b7092 Mon Sep 17 00:00:00 2001 From: Mateusz Lubecki Date: Sat, 19 Mar 2022 15:14:38 +0100 Subject: [PATCH] http client rx done callback - counting content lenght --- system/include/http_client/http_client.h | 6 ++++ .../http_client/http_client_rx_callback.h | 3 ++ .../src/http_client/http_client_rx_callback.c | 35 ++++++++++++++++++- 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/system/include/http_client/http_client.h b/system/include/http_client/http_client.h index 794b7fa..5b11819 100644 --- a/system/include/http_client/http_client.h +++ b/system/include/http_client/http_client.h @@ -35,6 +35,12 @@ extern int16_t http_client_http_code; */ extern uint16_t http_client_content_lenght; +/** + * Maximum content lenght which should be received by the client. Please bear in mind that THIS NOT include + * HTTP headers lenght + */ +extern uint16_t http_client_max_content_ln; + /** * Temporary buffer for processing */ diff --git a/system/include/http_client/http_client_rx_callback.h b/system/include/http_client/http_client_rx_callback.h index 3e260f9..2f5c8f4 100644 --- a/system/include/http_client/http_client_rx_callback.h +++ b/system/include/http_client/http_client_rx_callback.h @@ -10,6 +10,9 @@ #include +extern uint16_t http_client_content_start_index; + +void http_client_rx_done_callback_init(); uint8_t http_client_rx_done_callback(uint8_t current_data, const uint8_t * const rx_buffer, uint16_t rx_bytes_counter); #endif /* INCLUDE_HTTP_CLIENT_HTTP_CLIENT_RX_CALLBACK_H_ */ diff --git a/system/src/http_client/http_client_rx_callback.c b/system/src/http_client/http_client_rx_callback.c index aeb3e92..9b80901 100644 --- a/system/src/http_client/http_client_rx_callback.c +++ b/system/src/http_client/http_client_rx_callback.c @@ -36,6 +36,13 @@ typedef enum http_client_header_field { // set to one if we are still parsing HTTP response header static uint8_t http_client_response_header_processing = 1; + +// amount of bytes (octets) of a content of the HTTP response received so far +static uint16_t http_client_content_received_so_far = 0; + +// an index where the first byte of response occurs +uint16_t http_client_content_start_index = 0; + /** * This function is responsible for checking what HTTP header has been received */ @@ -59,6 +66,17 @@ static http_client_header_field_t http_client_check_what_field_it_is(char * buff return out; } +/** + * This function resets all variables used by rx done termination callback back to theirs default values + */ +void http_client_rx_done_callback_init() { + http_client_response_header_processing = 1; + http_client_content_received_so_far = 0; + http_client_content_start_index = 0; + http_client_header_index= 0; + memset (http_client_header_buffer, 0x0, HEADER_BUFFER_LN); +} + /** * This function is used as a termination callback for serial I/O with GPRS module. * It ends transmission in one of three cases @@ -125,7 +143,7 @@ uint8_t http_client_rx_done_callback(uint8_t current_data, const uint8_t * const case HEADER_CONTENT_LN: { // content ln may vary, so we need to copy data starting from the begining not the end - strncpy(local_buffer, http_client_header_buffer + http_client_header_index + 16, 6); + strncpy(local_buffer, http_client_header_buffer + 16, 6); // convert to integer value http_client_content_lenght = atoi(local_buffer); @@ -177,6 +195,21 @@ uint8_t http_client_rx_done_callback(uint8_t current_data, const uint8_t * const if ((char)current_data == '\n') { // everything what is now in 'http_client_header_buffer' is the size of chunk in hex!! http_client_content_lenght = strtol(http_client_header_buffer, 0, 16); + + // data starts from the next byte + http_client_content_start_index = rx_bytes_counter + 1; + } + } + else { + // chunk size is known so count data + http_client_content_received_so_far++; + + // check if all bytes defined by chunk size or 'HEADER_CONTENT_LN' have been received + if (http_client_content_received_so_far >= http_client_content_lenght) { + out = 1; + } + else if (http_client_max_content_ln != 0 && (http_client_content_received_so_far > http_client_max_content_ln)) { + out = 1; } } }