From 3a0e9d63aaba9ea35da06a9fa917fd4e14e46b2a Mon Sep 17 00:00:00 2001 From: Mateusz Lubecki Date: Mon, 14 Feb 2022 21:41:36 +0100 Subject: [PATCH] daily --- src/main.c | 2 +- system/include/drivers/serial.h | 1 + system/include/gsm/sim800c.h | 2 +- system/src/drivers/l4/serial_stm32l4x.c | 17 ++++ system/src/gsm/sim800c.c | 101 +++++++++++++++++++++--- system/src/gsm/sim800c_gprs.c | 4 +- 6 files changed, 114 insertions(+), 13 deletions(-) diff --git a/src/main.c b/src/main.c index 182cd8f..b9d240d 100644 --- a/src/main.c +++ b/src/main.c @@ -986,7 +986,7 @@ int main(int argc, char* argv[]){ // receive callback for communicatio with the modem gsm_sim800_rx_done_event_handler(main_gsm_srl_ctx_ptr, &main_gsm_state); - main_gsm_srl_ctx_ptr->srl_rx_state = SRL_RX_IDLE; + srl_reset(main_gsm_srl_ctx_ptr); } if (main_gsm_srl_ctx_ptr->srl_tx_state == SRL_TX_IDLE) { diff --git a/system/include/drivers/serial.h b/system/include/drivers/serial.h index eb43374..7022e29 100644 --- a/system/include/drivers/serial.h +++ b/system/include/drivers/serial.h @@ -186,6 +186,7 @@ extern "C" { void srl_init(srl_context_t *ctx, USART_TypeDef *port, uint8_t *rx_buffer, uint16_t rx_buffer_size, uint8_t *tx_buffer, uint16_t tx_buffer_size, uint32_t baudrate, uint8_t stop_bits); +void srl_reset(srl_context_t *ctx); void srl_close(srl_context_t *ctx); uint8_t srl_send_data(srl_context_t *ctx, const uint8_t* data, uint8_t mode, uint16_t leng, uint8_t internal_external); uint8_t srl_start_tx(srl_context_t *ctx, short leng); diff --git a/system/include/gsm/sim800c.h b/system/include/gsm/sim800c.h index 3edc4b1..0b7498b 100644 --- a/system/include/gsm/sim800c.h +++ b/system/include/gsm/sim800c.h @@ -20,7 +20,7 @@ extern float gsm_sim800_bcch_frequency; extern char gsm_sim800_cellid[5]; extern char gsm_sim800_lac[5]; -inline void replace_non_printable_with_space(char * str) { +inline void gsm_sim800_replace_non_printable_with_space(char * str) { for (int i = 0; *(str + i) != 0 ; i++) { char current = *(str + i); diff --git a/system/src/drivers/l4/serial_stm32l4x.c b/system/src/drivers/l4/serial_stm32l4x.c index 3960d19..efe6152 100644 --- a/system/src/drivers/l4/serial_stm32l4x.c +++ b/system/src/drivers/l4/serial_stm32l4x.c @@ -110,6 +110,23 @@ void srl_init( ctx->srl_rx_timeout_waiting_enable = 0; } +void srl_reset(srl_context_t *ctx) { + ctx->srl_rx_state = SRL_RX_IDLE; + ctx->srl_tx_state = SRL_TX_IDLE; + + ctx->srl_rx_error_reason = SRL_ERR_NONE; + + ctx->srl_rx_timeout_calc_started = 0; + ctx->total_idle_counter = 0; + ctx->total_overrun_counter = 0; + + ctx->srl_rx_start_time = 0; + ctx->srl_rx_waiting_start_time = 0; + + ctx->srl_rx_timeout_enable = 0; + ctx->srl_rx_timeout_waiting_enable = 0; +} + void srl_close(srl_context_t *ctx) { LL_USART_DeInit(ctx->port); diff --git a/system/src/gsm/sim800c.c b/system/src/gsm/sim800c.c index c5ac9b7..5c5c25c 100644 --- a/system/src/gsm/sim800c.c +++ b/system/src/gsm/sim800c.c @@ -29,12 +29,29 @@ static const char * CPIN = "+CPIN:\0"; static const char * CPIN_READY = "READY"; static const char * CPIN_SIMPIN = "SIMPIN"; static const char * REGISTERED_NETWORK = "+COPS:\0"; +static const char * INCOMING_CALL = "RING\0"; +#define INCOMING_CALL_LN 4 +static const char * UVP_PDOWN = "UNDER-VOLTAGE POWER DOWN\0"; +#define UVP_PDOWN_LN 24 +static const char * UVP_WARNING = "UNDER-VOLTAGE WARNNING\0"; +#define UVP_WARNING_LN 22 +static const char * OVP_PDWON = "OVER-VOLTAGE POWER DOWN\0"; +#define IVP_PDWON_LN 23 +static const char * OVP_WARNING = "OVER-VOLTAGE WARNNING\0"; +#define OVP_WARNING_LN 21 +static const char * CALL_RDY = "Call Ready\0"; +#define CALL_RDY_LN 10 +static const char * SMS_RDY = "SMS Ready\0"; +#define SMS_RDY_LN 9 uint32_t gsm_time_of_last_command_send_to_module = 0; // let's the library know if gsm module echoes every AT command send through serial port static uint8_t gsm_at_comm_echo = 1; +// how many newlines +volatile static int8_t gsm_terminating_newline_counter = 1; + // used to receive echo and response separately static uint8_t gsm_receive_newline_counter = 0; @@ -62,6 +79,53 @@ char gsm_sim800_cellid[5] = {0, 0, 0, 0, 0}; char gsm_sim800_lac[5] = {0, 0, 0, 0, 0}; +static void gsm_sim800_check_for_async_messages(uint8_t * ptr, uint16_t size, uint16_t * offset) { + + int comparision_result = 123; + + comparision_result = strncmp(INCOMING_CALL, ptr, INCOMING_CALL_LN); +} + +static uint32_t gsm_sim800_check_for_extra_newlines(uint8_t * ptr, uint16_t size) { + + // this bitmask stores positions of first four lines of text in input buffer + // the position value is set to 0xFF if a position of the newline is beyond 255 offset + uint32_t output_bitmask = 0; + + int8_t newlines = 0; + + int i = 0; + + char current, previous; + + current = (char)*ptr; + + for (i = 0; (i < size) && *(ptr + i) != 0; i++) { + + previous = current; + + current = (char)*(ptr + i); + + if (previous == '\n' && current >= 0x20 && current < 0x7F) { + + output_bitmask |= (uint8_t)(i << (8 * newlines)); + + newlines++; + } + + + if (newlines > 3) { + break; + } + } + + if (*(ptr + i - 1) == '\n' || *(ptr + i) == '\n') { + output_bitmask |= (uint8_t)i << (8 * newlines); + + } + + return output_bitmask; +} uint8_t gsm_sim800_get_waiting_for_command_response(void) { return gsm_waiting_for_command_response; @@ -341,15 +405,15 @@ uint8_t gsm_sim800_rx_terminating_callback(uint8_t current_data, const uint8_t * char before = '\0'; - int8_t terminating_newline_counter = 1; - // special case for CENG request if (gsm_at_command_sent_last == ENGINEERING_GET) { - terminating_newline_counter = 4; + gsm_terminating_newline_counter = 4; } - - if (gsm_at_command_sent_last == GET_CONNECTION_STATUS) { - terminating_newline_counter = 3; + else if (gsm_at_command_sent_last == GET_CONNECTION_STATUS) { + gsm_terminating_newline_counter = 4; + } + else { + gsm_terminating_newline_counter = 1; } if (rx_bytes_counter > 0) { @@ -368,7 +432,7 @@ uint8_t gsm_sim800_rx_terminating_callback(uint8_t current_data, const uint8_t * } // if an echo is enabled and second newline has been received - if (gsm_at_comm_echo == 1 && gsm_receive_newline_counter > terminating_newline_counter && gsm_response_start_idx > 0) { + if (gsm_at_comm_echo == 1 && gsm_receive_newline_counter > gsm_terminating_newline_counter && gsm_response_start_idx > 0) { gsm_receive_newline_counter = 0; @@ -385,8 +449,27 @@ void gsm_sim800_rx_done_event_handler(srl_context_t * srl_context, gsm_sim800_st int comparision_result = 123; + uint8_t second_line, third_line, fourth_line; + + uint32_t newlines = 0; + gsm_waiting_for_command_response = 0; + // check how many lines of text + newlines = gsm_sim800_check_for_extra_newlines(srl_context->srl_rx_buf_pointer + gsm_response_start_idx, srl_context->srl_rx_buf_ln); + + // if a library expects only single line of response + if (gsm_terminating_newline_counter == 1) { + // if more than one line of response has been received + second_line = (newlines & 0x0000FF00) >> 8; + third_line = (newlines & 0x00FF0000) >> 16; + fourth_line = (newlines & 0xFF000000) >> 24; + + if (second_line != 0) { + gsm_sim800_check_for_async_messages(srl_context->srl_rx_buf_pointer + gsm_response_start_idx + second_line, srl_context->srl_rx_buf_ln, & gsm_response_start_idx); + } + } + // if the library expects to receive a handshake from gsm module if (*state == SIM800_HANDSHAKING) { comparision_result = strcmp(OK, (const char *)(srl_context->srl_rx_buf_pointer + gsm_response_start_idx)); @@ -417,7 +500,7 @@ void gsm_sim800_rx_done_event_handler(srl_context_t * srl_context, gsm_sim800_st if (comparision_result == 0) { strncpy(gsm_sim800_sim_status, (const char *)(srl_context->srl_rx_buf_pointer + gsm_response_start_idx + 7), 10); - replace_non_printable_with_space(gsm_sim800_sim_status); + gsm_sim800_replace_non_printable_with_space(gsm_sim800_sim_status); } } @@ -427,7 +510,7 @@ void gsm_sim800_rx_done_event_handler(srl_context_t * srl_context, gsm_sim800_st if (comparision_result == 0) { strncpy(gsm_sim800_registered_network, (const char *)(srl_context->srl_rx_buf_pointer + gsm_response_start_idx + 12), 16); - replace_non_printable_with_space(gsm_sim800_registered_network); + gsm_sim800_replace_non_printable_with_space(gsm_sim800_registered_network); } } else if (gsm_at_command_sent_last == GET_SIGNAL_LEVEL) { diff --git a/system/src/gsm/sim800c_gprs.c b/system/src/gsm/sim800c_gprs.c index 211f3a4..de4a77c 100644 --- a/system/src/gsm/sim800c_gprs.c +++ b/system/src/gsm/sim800c_gprs.c @@ -106,7 +106,7 @@ void sim800_gprs_response_callback(srl_context_t * srl_context, gsm_sim800_state strncpy(gsm_sim800_ip_address, (const char *)(srl_context->srl_rx_buf_pointer + gsm_response_start_idx), 18); - replace_non_printable_with_space(gsm_sim800_ip_address); + gsm_sim800_replace_non_printable_with_space(gsm_sim800_ip_address); } else if (gsm_at_command_sent_last == GET_CONNECTION_STATUS ) { @@ -124,7 +124,7 @@ void sim800_gprs_response_callback(srl_context_t * srl_context, gsm_sim800_state if (comparision_result == 0) { memcpy(gsm_sim800_connection_status_str, srl_context->srl_rx_buf_pointer + i + 7, stringln - i - 7); - replace_non_printable_with_space(gsm_sim800_connection_status_str); + gsm_sim800_replace_non_printable_with_space(gsm_sim800_connection_status_str); } } }