Index: Utilities.c =================================================================== diff -u -reb133bc728b96fda709740f61b5dc22eae9ac5c9 -r392eff0e71403f26987f6aa275c93ab3af489421 --- Utilities.c (.../Utilities.c) (revision eb133bc728b96fda709740f61b5dc22eae9ac5c9) +++ Utilities.c (.../Utilities.c) (revision 392eff0e71403f26987f6aa275c93ab3af489421) @@ -7,8 +7,8 @@ * * @file Utilities.c * -* @author (last) Michael Garthwaite -* @date (last) 22-Feb-2022 +* @author (last) Dara Navaei +* @date (last) 13-Aug-2022 * * @author (original) Sean * @date (original) 17-Feb-2020 @@ -26,13 +26,11 @@ // ********** private definitions ********** -#define INITIAL_CRC16_VAL 0xFFFF ///< Seed for 16-bit CRC function -#define INITIAL_CRC08_VAL 0x00 ///< Seed for 8-bit CRC function +#define INITIAL_CRC16_VAL 0xFFFF ///< Seed for 16-bit CRC function. +#define INITIAL_CRC08_VAL 0x00 ///< Seed for 8-bit CRC function. +#define HEX_LETTER_TO_NUMBER_CONV 0x37 ///< Hex letter (i.e. A) to number conversion. +#define STR_TO_HEX_CONV_MAX_BYTES 8 ///< String to hex conversion maximum allowed bytes. -#define ASCII_CODE_LETTER_A 0x41 ///< ASCII code in hex for letter A. -#define ASCII_CODE_NUMBER_ZERO 0x30 ///< ASCII code in hex for number zero. -#define ASCII_CODE_NUMBER_SEVEN 0x37 ///< ASCII code in hex for number seven. - #define SEMAPHORE_IN_USE_TIMEOUT_MS ( 10 * MS_PER_SECOND ) ///< Taken semaphore timeout in milliseconds. // ********** private data ********** @@ -596,30 +594,70 @@ * @details Inputs: none * @details Outputs: none * @param valuePtr pointer to hex string to convert + * @param convValuePtr pointer to the provided buffer to write the converted + * values back * @param size size of the hex string to convert - * @return converted value of hex string + * @return TRUE if the characters were accepted, otherwise FALSE *************************************************************************/ -U32 hexStrToDec( U08 const * const valuePtr, U08 size ) +BOOL hexStrToDec( U08 const * const valuePtr, U32* convValuePtr, U08 size ) { U08 ii; U08 value; - U32 result = 0; + U32 result = 0; + BOOL status = FALSE; - for ( ii = 0; ii < size; ++ii ) + if ( size <= STR_TO_HEX_CONV_MAX_BYTES ) { - if ( valuePtr[ii] < ASCII_CODE_LETTER_A ) + // If the provided size is less than the maximum allowed, loop through the list for conversion + for ( ii = 0; ii < size; ++ii ) { - value = ( valuePtr[ii] - ASCII_CODE_NUMBER_ZERO ); - result = ( result << 4 ) | value; + value = valuePtr[ ii ]; + + switch( value ) + { + case ASCII_CODE_NUMBER_ZERO: + case ASCII_CODE_NUMBER_ONE: + case ASCII_CODE_NUMBER_TWO: + case ASCII_CODE_NUMBER_THREE: + case ASCII_CODE_NUMBER_FOUR: + case ASCII_CODE_NUMBER_FIVE: + case ASCII_CODE_NUMBER_SIX: + case ASCII_CODE_NUMBER_SEVEN: + case ASCII_CODE_NUMBER_EIGHT: + case ASCII_CODE_NUMBER_NINE: + // If the hex string is a number, subtract the number from hex zero to get a number + // Shift the results by a nibble and add the calculated number. + // For instance, if the value = 0x36 - 0x30 = the value becomes 6 + value = ( value - ASCII_CODE_NUMBER_ZERO ); + result = ( result << SHIFT_BITS_BY_4 ) | value; + status = TRUE; + break; + + case ASCII_CODE_LETTER_A: + case ASCII_CODE_LETTER_B: + case ASCII_CODE_LETTER_C: + case ASCII_CODE_LETTER_D: + case ASCII_CODE_LETTER_E: + case ASCII_CODE_LETTER_F: + // If the value is from A-F for the hex values, subtract the value from 0x37. + // This converts the letters from A to F to 10 to 15. + // For instance if the read value is 0x43 ( Hex C ) - 0x37 = 12 (decimal) + value = ( value - HEX_LETTER_TO_NUMBER_CONV ); + result = ( result << SHIFT_BITS_BY_4 ) | value; + status = TRUE; + break; + + default: + // Do nothing since the character is not accepted. + break; + } } - else - { - value = ( valuePtr[ii] - ASCII_CODE_NUMBER_SEVEN ); - result = ( result << 4 ) | value; - } + + // Set the processed value to the provided buffer + *convValuePtr = result; } - return result; + return status; } /*********************************************************************//** @@ -633,7 +671,7 @@ BOOL isCriticalDataSet( CRITICAL_DATA_T *data ) { BOOL result = FALSE; - CRITICAL_DATA_T d; + CRITICAL_DATA_T d; _disable_IRQ(); d = *data;