From 73ab9cac33f127e7d926ecf092b45b2bb08b1e6c Mon Sep 17 00:00:00 2001 From: Mateusz Lubecki Date: Sun, 5 May 2024 20:07:14 +0200 Subject: [PATCH] storing message counter as a string of character to support situation when it may contain a string of character, not hex of dec base number --- include/rte_main.h | 1 - include/software_version.h | 2 +- src/aprsis.c | 9 +--- src/main.c | 6 +-- src/rte_main.c | 1 - system/include/aprs/message.h | 27 ++++++---- system/src/aprs/message.c | 97 ++++++++++++++++++++++++----------- 7 files changed, 90 insertions(+), 53 deletions(-) diff --git a/include/rte_main.h b/include/rte_main.h index 705202c..d7e3673 100644 --- a/include/rte_main.h +++ b/include/rte_main.h @@ -16,7 +16,6 @@ #define RTE_MAIN_WOKEN_UP_EXITED 4u extern message_t rte_main_received_message; -extern message_source_t rte_main_received_message_source; //!< Trigger preparing and sending ACK extern uint8_t rte_main_trigger_message_ack; diff --git a/include/software_version.h b/include/software_version.h index 92e5ffe..9523c7a 100644 --- a/include/software_version.h +++ b/include/software_version.h @@ -9,7 +9,7 @@ #define SOFTWARE_VERSION_H_ #define SW_VER "EB05" -#define SW_DATE "18042024" +#define SW_DATE "27042024" #define SW_KISS_PROTO "B" extern const char software_version_str[5]; diff --git a/src/aprsis.c b/src/aprsis.c index 1aa2a7f..87cdee6 100644 --- a/src/aprsis.c +++ b/src/aprsis.c @@ -263,16 +263,13 @@ STATIC void aprsis_receive_callback(srl_context_t* srl_context) { if (message_position != 0) { // prevent overwriting message received from radio channel if it hasn't been serviced yet - if (rte_main_received_message_source == MESSAGE_SOURCE_UNINITIALIZED) { + if (rte_main_received_message.source == MESSAGE_SOURCE_UNINITIALIZED) { // if decoding was successfull if (message_decode(buffer, message_ln, message_position, MESSAGE_SOURCE_APRSIS, &rte_main_received_message) == 0) { // check if it is for me if (message_is_for_me(aprsis_callsign, aprsis_ssid, &rte_main_received_message) == 0) { - // set a source of this message - rte_main_received_message_source = MESSAGE_SOURCE_APRSIS; - // trigger preparing ACK rte_main_trigger_message_ack = 1; } @@ -1127,12 +1124,10 @@ void aprsis_send_gsm_status(const char * callsign_with_ssid) { * @param message */ void aprsis_send_ack_for_message(const message_t * const message) { - aprsis_packet_tx_message_size = message_create_ack_for((uint8_t*)aprsis_packet_tx_buffer, APRSIS_TX_BUFFER_LN - 1, message, MESSAGE_SOURCE_APRSIS); + aprsis_packet_tx_message_size = message_create_ack_for((uint8_t*)aprsis_packet_tx_buffer, APRSIS_TX_BUFFER_LN - 1, message); gsm_sim800_tcpip_async_write((uint8_t *)aprsis_packet_tx_buffer, aprsis_packet_tx_message_size, aprsis_serial_port, aprsis_gsm_modem_state); - rte_main_received_message_source = MESSAGE_SOURCE_UNINITIALIZED; - } #ifdef UNIT_TEST diff --git a/src/main.c b/src/main.c index 365d365..939ee2e 100644 --- a/src/main.c +++ b/src/main.c @@ -1253,15 +1253,15 @@ int main(int argc, char* argv[]){ } if (rte_main_trigger_message_ack == 1) { - if (rte_main_received_message_source == MESSAGE_SOURCE_APRSIS && gsm_sim800_tcpip_tx_busy() == 0) { + if (rte_main_received_message.source == MESSAGE_SOURCE_APRSIS && gsm_sim800_tcpip_tx_busy() == 0) { rte_main_trigger_message_ack = 0; aprsis_send_ack_for_message(&rte_main_received_message); - rte_main_received_message_source == MESSAGE_SOURCE_UNINITIALIZED; + rte_main_received_message.source = MESSAGE_SOURCE_UNINITIALIZED; } - else if (rte_main_received_message_source == MESSAGE_SOURCE_RADIO) { + else if (rte_main_received_message.source == MESSAGE_SOURCE_RADIO) { } diff --git a/src/rte_main.c b/src/rte_main.c index 0101eba..e16f447 100644 --- a/src/rte_main.c +++ b/src/rte_main.c @@ -18,7 +18,6 @@ uint8_t rte_main_trigger_status = 0; uint8_t rte_main_trigger_wx_packet = 0; message_t rte_main_received_message; -message_source_t rte_main_received_message_source; #ifdef PARAMETEO //!< Trigger preparing and sending ACK diff --git a/system/include/aprs/message.h b/system/include/aprs/message.h index 88a1d3c..b26982c 100644 --- a/system/include/aprs/message.h +++ b/system/include/aprs/message.h @@ -12,15 +12,13 @@ #include "stdint.h" #include "./stored_configuration_nvm/config_data.h" -#define MESSAGE_MAX_LENGHT 67 +#define MESSAGE_MAX_LENGHT 67 -typedef struct message_t { - AX25Call from; - AX25Call to; - uint8_t content[MESSAGE_MAX_LENGHT]; - uint8_t number; -}message_t; +#define MESSAGE_NUMBER_STRING_BUFFER 5 ///!< include room of null terminator +/** + * +*/ typedef enum message_source_t { MESSAGE_SOURCE_UNINITIALIZED = 0x0, MESSAGE_SOURCE_APRSIS = 0x1, @@ -29,6 +27,18 @@ typedef enum message_source_t { MESSAGE_SOURCE_RADIO_HEXCNTR = 0x12 }message_source_t; +/** + * +*/ +typedef struct message_t { + AX25Call from; + AX25Call to; + uint8_t content[MESSAGE_MAX_LENGHT]; + uint8_t number; + uint8_t number_str[MESSAGE_NUMBER_STRING_BUFFER]; //!< Message sequence number but stored as string not decoded to integer + message_source_t source; +}message_t; + /** * Decode received data to look for an APRS message and put it into a structure * @param message pointer to data received from APRS-IS @@ -53,8 +63,7 @@ uint8_t message_is_for_me(const char * const callsign, const uint8_t ssid, const * @param out_buffer * @param out_buffer_ln * @param message - * @param src how this message has been received */ -int message_create_ack_for(uint8_t * out_buffer, const uint16_t out_buffer_ln, const message_t * const message, const message_source_t src); +int message_create_ack_for(uint8_t * out_buffer, const uint16_t out_buffer_ln, const message_t * const message); #endif /* INCLUDE_APRS_MESSAGE_H_ */ diff --git a/system/src/aprs/message.c b/system/src/aprs/message.c index 29ea1a0..adfeb5f 100644 --- a/system/src/aprs/message.c +++ b/system/src/aprs/message.c @@ -20,15 +20,17 @@ #define MESSAGE_SSID_CHARS_LN 2 -#define MESSAGE_ATOI_BUFFER 5 ///!< include room of null terminator - #define MESSAGE_SENDER_CALL_SSID_MAXLEN 9 #define MESSAGE_CURRENT_CHAR *(message + content_position + i) #define MESSAGE_CURRENT_SENDER_CHAR *(message + i) -#define MESSAGE_IS_DIGIT(c) (c >= '0' && c <= '9') +#define MESSAGE_IS_HEX_DIGIT(c) ((c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')) + +#define MESSAGE_IS_DIGIT(c) ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')) + +#define MESSAGE_IS_DIGIT_OR_LETTER(c) ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) #define MESSAGE_ACK_REMAINING_BUF (out_buffer_ln - current_pos) @@ -46,10 +48,6 @@ static const int8_t message_atoi_lookup[] = { 0, 1, 2, 3, 4, 5, 6, 7, -1, -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15}; -/** - * Table used -*/ -static char message_atoi_buffer[MESSAGE_ATOI_BUFFER]; /** * Convert string with message number to integer, with automatic detection of decimal or hex base. It operates @@ -61,22 +59,45 @@ static char message_atoi_buffer[MESSAGE_ATOI_BUFFER]; */ STATIC uint32_t message_atoi_message_counter(const uint8_t const * string, uint8_t string_ln, message_t * output) { + // sum to be returned uint32_t sum = 0u; + // temporary buffer int8_t conversion_table[6]; + // set to non zero if any digit from A fo F was found in input string int8_t is_hex = 0; + // checck if input string is located in legoit RAM memory if (variant_validate_is_within_ram(string) == 1 && string_ln <= 6) { - memset(conversion_table, 0x00, string_ln); - for (int8_t i = string_ln; i >= 0; i--) { - const uint8_t index = *(string + i - 1) - 0x30u; + // clear intermediate convertion buffer + memset(conversion_table, 0x00, 6); - conversion_table[string_ln - i] = message_atoi_lookup[index]; + // iterator over conversion_table array + int8_t conversion_it = string_ln; - if (*(string + i) > '9') { - is_hex = 1; + // iterate from the end of a string + for (int8_t string_it = string_ln; string_it >= 0; string_it--) { + + // currently processed character + const uint8_t current_char = *(string + string_it - 1); + + // check if current character is hex or dec base digit + if (MESSAGE_IS_DIGIT(current_char)) { + + // calculate index to lookup table basing on current character + const uint8_t index = current_char - 0x30u; + + // check lookup table for converting character with a digit to a number + conversion_table[string_ln - conversion_it] = message_atoi_lookup[index]; + + // decrement an interator + conversion_it--; + + if (MESSAGE_IS_HEX_DIGIT(current_char)) { + is_hex = 1; + } } } @@ -100,6 +121,15 @@ STATIC uint32_t message_atoi_message_counter(const uint8_t const * string, uint8 if (variant_validate_is_within_ram(output) == 1) { output->number = sum & 0xFFu; + + if (is_hex == 1) { + if (output->source == MESSAGE_SOURCE_APRSIS) { + output->source = MESSAGE_SOURCE_APRSIS_HEXCNTR; + } + else if (output->source == MESSAGE_SOURCE_RADIO) { + output->source = MESSAGE_SOURCE_RADIO_HEXCNTR; + } + } } return sum; @@ -131,7 +161,7 @@ uint8_t message_decode(const uint8_t * const message, const uint16_t message_ln, return result; } - memset(message_atoi_buffer, 0x00, MESSAGE_ATOI_BUFFER); + memset(output->number_str, 0x00, MESSAGE_NUMBER_STRING_BUFFER); // if start position of APRS message (position of recipient callsign) has not been provided if ((src == MESSAGE_SOURCE_APRSIS) && (content_position == 0)) { @@ -205,16 +235,16 @@ uint8_t message_decode(const uint8_t * const message, const uint16_t message_ln, // extract SSID for (; i < MESSAGE_RECIPIENT_FIELD_SIZE; i++) { // copy characters to aux buffer - message_atoi_buffer[result++] = MESSAGE_CURRENT_SENDER_CHAR; + output->number_str[result++] = MESSAGE_CURRENT_SENDER_CHAR; // check if there isn't enough characters - if (result == MESSAGE_ATOI_BUFFER) { + if (result == MESSAGE_NUMBER_STRING_BUFFER) { break; } } // convert SSID to int - output->from.ssid = atoi(message_atoi_buffer); + output->from.ssid = atoi(output->number_str); } // clear the iterator, it will be used across this function @@ -259,19 +289,19 @@ uint8_t message_decode(const uint8_t * const message, const uint16_t message_ln, // extract SSID for (; i < MESSAGE_RECIPIENT_FIELD_SIZE; i++) { // copy characters to aux buffer - message_atoi_buffer[result++] = MESSAGE_CURRENT_CHAR; + output->number_str[result++] = MESSAGE_CURRENT_CHAR; // check if there isn't enough characters - if (result == MESSAGE_ATOI_BUFFER) { + if (result == MESSAGE_NUMBER_STRING_BUFFER) { break; } } // convert SSID to int - output->to.ssid = atoi(message_atoi_buffer); + output->to.ssid = atoi(output->number_str); } - if (result != MESSAGE_ATOI_BUFFER && /* if SSID extraction was OK and end of a buffer hasn't been reached */ + if (result != MESSAGE_NUMBER_STRING_BUFFER && /* if SSID extraction was OK and end of a buffer hasn't been reached */ ( (i < MESSAGE_RECIPIENT_FIELD_SIZE) || /* if recipient and callsign has been read before ':' separating from message content */ ((i == MESSAGE_RECIPIENT_FIELD_SIZE) && (MESSAGE_CURRENT_CHAR == ':')) /* if a position of separating ':' was reached and ':' is there */ @@ -279,7 +309,7 @@ uint8_t message_decode(const uint8_t * const message, const uint16_t message_ln, ) { // reinitialize buffer before next usage - memset(message_atoi_buffer, 0x00, MESSAGE_ATOI_BUFFER); + memset(output->number_str, 0x00, MESSAGE_NUMBER_STRING_BUFFER); // check if the iterator is set now to position of ':' separating // recipient an the message itself @@ -310,21 +340,23 @@ uint8_t message_decode(const uint8_t * const message, const uint16_t message_ln, // now iterator is set (should be set) on a first digit of message counter // copy everything until first non digit character is found - while (MESSAGE_IS_DIGIT(MESSAGE_CURRENT_CHAR)) { - message_atoi_buffer[result++] = MESSAGE_CURRENT_CHAR; + while (MESSAGE_IS_DIGIT_OR_LETTER(MESSAGE_CURRENT_CHAR)) { + output->number_str[result++] = MESSAGE_CURRENT_CHAR; i++; // check if there isn't enough characters - if (result == MESSAGE_ATOI_BUFFER) { + if (result == MESSAGE_NUMBER_STRING_BUFFER) { break; } } - // convert message counter from string to int - output->number = atoi(message_atoi_buffer); + output->source = src; - if (result < MESSAGE_ATOI_BUFFER) { + // convert message counter from string to int + message_atoi_message_counter(output->number_str, result, output); + + if (result < MESSAGE_NUMBER_STRING_BUFFER) { // new we are done (??) result = 0; } @@ -366,16 +398,18 @@ uint8_t message_is_for_me(const char * const callsign, const uint8_t ssid, const * @param message * @param src how this message has been received */ -int message_create_ack_for(uint8_t * out_buffer, const uint16_t out_buffer_ln, const message_t * const message, const message_source_t src) +int message_create_ack_for(uint8_t * out_buffer, const uint16_t out_buffer_ln, const message_t * const message) { int current_pos = 0; uint8_t call_position = 0; + const message_source_t src = message->source; + // clear output buffer memset(out_buffer, 0x00, out_buffer_ln); - if (src == MESSAGE_SOURCE_APRSIS) { + if (src == MESSAGE_SOURCE_APRSIS || src == MESSAGE_SOURCE_APRSIS_HEXCNTR) { // put my callsign for (; call_position < 6; call_position++) { @@ -428,7 +462,8 @@ int message_create_ack_for(uint8_t * out_buffer, const uint16_t out_buffer_ln, c current_pos += 9; // then put 'ackXX' where X is message number - current_pos += snprintf(MESSAGE_ACK_CURRENT_POS, MESSAGE_ACK_REMAINING_BUF, ":ack%d\r\n", message->number); + current_pos += snprintf(MESSAGE_ACK_CURRENT_POS, MESSAGE_ACK_REMAINING_BUF, ":ack%s\r\n", message->number_str); + } else {