From 628b1e1ddd6f65df0e52476d90ff4513dca6c83a Mon Sep 17 00:00:00 2001 From: Mateusz Lubecki Date: Tue, 25 Jan 2022 23:37:37 +0100 Subject: [PATCH] requesting registration status from sim800 --- src/main.c | 4 +- system/include/gsm/sim800c.h | 7 +++- system/src/gsm/sim800c.c | 73 +++++++++++++++++++++++++++++++++--- 3 files changed, 75 insertions(+), 9 deletions(-) diff --git a/src/main.c b/src/main.c index a708635..0054d45 100644 --- a/src/main.c +++ b/src/main.c @@ -979,7 +979,9 @@ int main(int argc, char* argv[]){ if (main_gsm_srl_ctx_ptr->srl_rx_state == SRL_RX_DONE || main_gsm_srl_ctx_ptr->srl_rx_state == SRL_RX_ERROR) { // receive callback for communicatio with the modem - //gsm_sim800_rx_done_callback(main_gsm_srl_ctx_ptr, &main_gsm_state); + 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; } } diff --git a/system/include/gsm/sim800c.h b/system/include/gsm/sim800c.h index c5cab18..4566caa 100644 --- a/system/include/gsm/sim800c.h +++ b/system/include/gsm/sim800c.h @@ -14,7 +14,9 @@ typedef enum gsm_sim800_state_t { SIM800_UNKNOWN, SIM800_POWERED_OFF, SIM800_NOT_YET_COMM, - SIM800_INITIALIZIG, + SIM800_HANDSHAKING, + SIM800_INITIALIZING, + SIM800_INITIALIZING_WAIT_RESPONSE, SIM800_ALIVE, SIM800_GPRS_CONNECTED }gsm_sim800_state_t; @@ -22,6 +24,7 @@ typedef enum gsm_sim800_state_t { void gsm_sim800_init(gsm_sim800_state_t * state, uint8_t enable_echo); void gsm_sim800_pool(srl_context_t * srl_context, gsm_sim800_state_t * state); -uint8_t gsm_sim800_rx_callback(uint8_t current_data, const uint8_t * const rx_buffer, uint16_t rx_bytes_counter); // callback used to detect echo +uint8_t gsm_sim800_rx_terminating_callback(uint8_t current_data, const uint8_t * const rx_buffer, uint16_t rx_bytes_counter); // callback used to detect echo +void gsm_sim800_rx_done_event_handler(srl_context_t * srl_context, gsm_sim800_state_t * state); #endif /* INCLUDE_GSM_SIM800C_H_ */ diff --git a/system/src/gsm/sim800c.c b/system/src/gsm/sim800c.c index 6a88a76..981ea81 100644 --- a/system/src/gsm/sim800c.c +++ b/system/src/gsm/sim800c.c @@ -11,8 +11,12 @@ #include -static const char * AUTOBAUD_STRING = "AT\r\0"; -static const char * OK = "OK\0"; +static const char * AUTOBAUD_STRING = "AT\r\0"; +static const char * GET_SIGNAL_LEVEL = "AT+CSQ\r\0"; +static const char * GET_NETWORK_REGISTRATION = "AT+CREG?\r\0"; + +static const char * OK = "OK\r\n\0"; +static const char * NETWORK_REGISTRATION = "+CREG\0"; uint32_t gsm_time_of_last_command_send_to_module = 0; @@ -25,6 +29,13 @@ uint8_t gsm_receive_newline_counter = 0; // first character of non-echo response from the module uint16_t gsm_response_start_idx = 0; +// +const char * gsm_at_command_sent_last = 0; + +uint8_t gsm_waiting_for_command_response = 0; + +uint8_t gsm_registration_status = 4; // unknown + void gsm_sim800_init(gsm_sim800_state_t * state, uint8_t enable_echo) { gsm_at_comm_echo = enable_echo; @@ -47,24 +58,45 @@ void gsm_sim800_pool(srl_context_t * srl_context, gsm_sim800_state_t * state) { srl_send_data(srl_context, (const uint8_t*) AUTOBAUD_STRING, SRL_MODE_ZERO, strlen(AUTOBAUD_STRING), SRL_INTERNAL); // switch the state - *state = SIM800_INITIALIZIG; + *state = SIM800_HANDSHAKING; // wait for the handshake to transmit srl_wait_for_tx_completion(srl_context); // start data reception - //srl_receive_data(srl_context, 8, SRL_NO_START_CHR, SRL_NO_STOP_CHR, SRL_ECHO_DISABLE, 0, 0); - srl_receive_data_with_callback(srl_context, gsm_sim800_rx_callback); + srl_receive_data_with_callback(srl_context, gsm_sim800_rx_terminating_callback); // record when the handshake has been sent gsm_time_of_last_command_send_to_module = main_get_master_time(); } + else if (*state == SIM800_INITIALIZING && gsm_waiting_for_command_response == 0) { + + // check what command has been sent + switch ((uint32_t)gsm_at_command_sent_last) { + case 0: { // no command has been send so far + + // ask for network registration status + srl_send_data(srl_context, (const uint8_t*) GET_NETWORK_REGISTRATION, SRL_MODE_ZERO, strlen(GET_NETWORK_REGISTRATION), SRL_INTERNAL); + + // wait for command completion + srl_wait_for_tx_completion(srl_context); + + gsm_at_command_sent_last = GET_NETWORK_REGISTRATION; + + gsm_waiting_for_command_response = 1; + + srl_receive_data_with_callback(srl_context, gsm_sim800_rx_terminating_callback); + + break; + } + } + } } /** * Callback to be called just after the reception is done */ -uint8_t gsm_sim800_rx_callback(uint8_t current_data, const uint8_t * const rx_buffer, uint16_t rx_bytes_counter) { +uint8_t gsm_sim800_rx_terminating_callback(uint8_t current_data, const uint8_t * const rx_buffer, uint16_t rx_bytes_counter) { char current = (char) current_data; @@ -90,9 +122,38 @@ uint8_t gsm_sim800_rx_callback(uint8_t current_data, const uint8_t * const rx_bu gsm_receive_newline_counter = 0; + gsm_waiting_for_command_response = 0; + return 1; } return 0; } + +void gsm_sim800_rx_done_event_handler(srl_context_t * srl_context, gsm_sim800_state_t * state) { + + int comparision_result = 123; + + gsm_waiting_for_command_response = 0; + + // 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)); + + // if 'OK' has been received from the module + if (comparision_result == 0) { + *state = SIM800_INITIALIZING; + } + else { + *state = SIM800_NOT_YET_COMM; + } + } + else if (*state == SIM800_INITIALIZING) { + comparision_result = strncmp(NETWORK_REGISTRATION, (const char *)(srl_context->srl_rx_buf_pointer + gsm_response_start_idx), 5); + + if (comparision_result == 0) { + gsm_registration_status = atoi((const char *)(srl_context->srl_rx_buf_pointer + gsm_response_start_idx + 9)); + } + } +}