Go to the documentation of this file.00001
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include <stdlib.h>
00032
00033 #include "tm_reader.h"
00034 #include "serial_reader_imp.h"
00035
00036 static char hexchars[] = "0123456789ABCDEF";
00037
00038
00039 TMR_Status
00040 TMR_hexToBytes(const char *hex, uint8_t *bytes, uint32_t size,
00041 uint32_t *convertLen)
00042 {
00043 char c;
00044 int i, len;
00045 uint8_t val[2];
00046
00047 if (hex[0] == '0' && (hex[1] == 'x' || hex[1] == 'X'))
00048 {
00049 hex += 2;
00050 }
00051
00052 len = 0;
00053 while (size > 0 && *hex != '\0')
00054 {
00055 for (i = 0; i < 2; i++)
00056 {
00057 c = *hex++;
00058 if (c >= '0' && c <= '9')
00059 val[i] = c - '0';
00060 else if (c >= 'a' && c <= 'f')
00061 val[i] = 10 + c - 'a';
00062 else if (c >= 'A' && c <= 'F')
00063 val[i] = 10 + c - 'A';
00064 else
00065 return TMR_ERROR_INVALID;
00066 }
00067 *bytes = (val[0] << 4) | val[1];
00068
00069 len++;
00070 bytes++;
00071 size--;
00072 }
00073
00074 if (convertLen != NULL)
00075 {
00076 *convertLen = len;
00077 }
00078
00079 return TMR_SUCCESS;
00080 }
00081
00082 void
00083 TMR_bytesToHex(const uint8_t *bytes, uint32_t size, char *hex)
00084 {
00085
00086 while (size--)
00087 {
00088 *hex++ = hexchars[*bytes >> 4];
00089 *hex++ = hexchars[*bytes & 15];
00090 bytes++;
00091 }
00092 *hex = '\0';
00093 }
00094
00102 void
00103 TMR_hexDottedQuad(const uint8_t bytes[4], char buf[12])
00104 {
00105 int i;
00106
00107 for (i = 0; i < 4 ; i++)
00108 {
00109 *buf++ = hexchars[*bytes >> 4];
00110 *buf++ = hexchars[*bytes & 15];
00111 *buf++ = '.';
00112 bytes++;
00113 }
00114 *--buf = '\0';
00115 }
00116
00124 TMR_Status
00125 TMR_hexDottedQuadToUint32(const char bytes[12], uint32_t *result)
00126 {
00127 TMR_Status retVal;
00128 uint32_t tmpResult;
00129 uint8_t byteVal;
00130 int i,j;
00131
00132 tmpResult=0;
00133 for (i = 0, j = 0; i < 12 ; i+=3,++j)
00134 {
00135 retVal = TMR_hexToBytes(&bytes[i], &byteVal, 1, NULL);
00136 if (TMR_SUCCESS == retVal)
00137 {
00138 tmpResult |= (byteVal << (8 * (3-j)));
00139 }
00140 else
00141 {
00142 return retVal;
00143 }
00144 }
00145
00146 if (NULL != result)
00147 {
00148 *result = tmpResult;
00149 }
00150 return retVal;
00151 }