Index: Utilities.c =================================================================== diff -u -rcd9e36f40c7b69d8fec47be5410ba7926844d06b -r5d0c8a999e953e1e4d644c4928b2e6ca4947e624 --- Utilities.c (.../Utilities.c) (revision cd9e36f40c7b69d8fec47be5410ba7926844d06b) +++ Utilities.c (.../Utilities.c) (revision 5d0c8a999e953e1e4d644c4928b2e6ca4947e624) @@ -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,35 +594,69 @@ * @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; - // Switch 0-9 - // swtich A-F - // default - // size should not be more than 8 bytes - - 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 ( C ) - 0x37 = 12 + 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; - } + + *convValuePtr = result; } - return result; + return status; } /*********************************************************************//** @@ -638,7 +670,7 @@ BOOL isCriticalDataSet( CRITICAL_DATA_T *data ) { BOOL result = FALSE; - CRITICAL_DATA_T d; + CRITICAL_DATA_T d; _disable_IRQ(); d = *data;