tmr_utils.h
Go to the documentation of this file.
00001 #ifndef _TMR_UTILS_H
00002 #define _TMR_UTILS_H
00003 
00011 /*
00012  * Copyright (c) 2009 ThingMagic, Inc.
00013  *
00014  * Permission is hereby granted, free of charge, to any person obtaining a copy
00015  * of this software and associated documentation files (the "Software"), to deal
00016  * in the Software without restriction, including without limitation the rights
00017  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00018  * copies of the Software, and to permit persons to whom the Software is
00019  * furnished to do so, subject to the following conditions:
00020  *
00021  * The above copyright notice and this permission notice shall be included in
00022  * all copies or substantial portions of the Software.
00023  * 
00024  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00025  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00026  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00027  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00028  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00029  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00030  * THE SOFTWARE.
00031  */
00032 
00033 #include <stdint.h>
00034 #include <stddef.h>
00035 
00036 #ifdef  __cplusplus
00037 extern "C" {
00038 #endif
00039 
00040 /* Macros for working with values embedded in uint8_t arrays (msg) */
00041 /* Absolute-value get */
00042 #define GETU8AT(msg, i) ( \
00043   ((msg)[(i)])          )
00044 
00045 #define GETU16AT(msg, i) ( \
00046   ((uint16_t)((msg)[(i)  ]) <<  8)   | \
00047   ((msg)[(i)+1] <<  0)   )
00048 
00049 #define GETS16AT(msg, i) ( \
00050     ((int16_t)((msg)[(i)  ]) <<  8)   | \
00051     ((int16_t)((msg)[(i)+1]) <<  0)   )
00052 
00053 #define GETU24AT(msg, i) ( \
00054   ((uint32_t)((msg)[(i)  ]) <<  16)  | \
00055   ((uint32_t)((msg)[(i)+1]) <<   8)  | \
00056   ((msg)[(i)+2] <<   0)  )
00057 
00058 #define GETU32AT(msg, i) ( \
00059   ((uint32_t)((msg)[(i)  ]) <<  24)  | \
00060   ((uint32_t)((msg)[(i)+1]) <<  16)  | \
00061   ((uint32_t)((msg)[(i)+2]) <<   8)  | \
00062   ((msg)[(i)+3] <<   0)  )
00063 
00064 /* Get and update index to next position */
00065 #define GETU8(msg, i)   ((msg)[(i)++])
00066 #define GETU16(msg, i)  (i+=2, GETU16AT((msg), i-2))
00067 #define GETU24(msg, i)  (i+=3, GETU24AT((msg), i-3))
00068 #define GETU32(msg, i)  (i+=4, GETU32AT((msg), i-4))
00069 
00070 /* Set and update index to next position */
00071 #define SETU8(msg, i, u8val) do {      \
00072   (msg)[(i)++] = (u8val)      & 0xff;  \
00073 } while (0)
00074 
00075 #define SETU16(msg, i, u16val) do {    \
00076   uint16_t _tmp = (u16val);            \
00077   (msg)[(i)++] =(uint8_t) (_tmp >>  8) & 0xff;  \
00078   (msg)[(i)++] =(uint8_t)(_tmp >>  0) & 0xff;  \
00079 } while (0)
00080 
00081 #define SETS16(msg, i, s16val) do {    \
00082   int16_t _tmp = (s16val);            \
00083   (msg)[(i)++] =(int8_t) (_tmp >>  8) & 0xff;  \
00084   (msg)[(i)++] =(int8_t)(_tmp >>  0) & 0xff;  \
00085 } while (0)
00086 
00087 #define SETU32(msg, i, u32val) do {    \
00088   uint32_t _tmp = (u32val);            \
00089   (msg)[(i)++] = (uint8_t)(_tmp >> 24) & 0xff;  \
00090   (msg)[(i)++] = (uint8_t)(_tmp >> 16) & 0xff;  \
00091   (msg)[(i)++] = (uint8_t)(_tmp >>  8) & 0xff;  \
00092   (msg)[(i)++] = (uint8_t)(_tmp >>  0) & 0xff;  \
00093 } while (0)
00094 
00095 #define SETS32(msg, i, s32val) do {    \
00096   int32_t _tmp = (s32val);            \
00097   (msg)[(i)++] = (int8_t)(_tmp >> 24) & 0xff;  \
00098   (msg)[(i)++] = (int8_t)(_tmp >> 16) & 0xff;  \
00099   (msg)[(i)++] = (int8_t)(_tmp >>  8) & 0xff;  \
00100   (msg)[(i)++] = (int8_t)(_tmp >>  0) & 0xff;  \
00101 } while (0)
00102 
00103 /* Append a value to our list structures, which have both
00104  * a allocated-space value (max) and a length-of-underlying-list
00105  * value (len). Len can exceed max, which indicates to the caller
00106  * that there was not enough space in the passed-in list structure
00107  * to store the entire list.
00108  */
00109 #define LISTAPPEND(l, value) do {         \
00110   (l)->len++;                             \
00111   if ((l)->len <= (l)->max)               \
00112     (l)->list[(l)->len - 1] = (value);    \
00113 } while (0)
00114 
00115 
00116 /* Macros for working with large bitmasks made up of arrays of uint32_t */
00117 #define BITGET(array, number) (((array)[(number)/32] >> ((number)&31)) & 1)
00118 #define BITSET(array, number) ((array)[(number)/32] |= ((uint32_t)1 << ((number)&31)))
00119 #define BITCLR(array, number) ((array)[(number)/32] &= ~((uint32_t)1 << ((number)&31)))
00120 
00121 #define numberof(x) (sizeof((x))/sizeof((x)[0]))
00122 
00123 #ifndef TMR_USE_HOST_C_LIBRARY
00124 void *tm_memcpy(void *dest, const void *src, size_t n);
00125 char *tm_strcpy(char *dest, const char *src);
00126 char *tm_strchr(const char *s, int c);
00127 
00128 #undef memcpy
00129 #undef strcpy
00130 #undef strchr
00131 
00132 #define memcpy tm_memcpy
00133 #define strcpy tm_strcpy
00134 #define strchr tm_strchr
00135 #endif
00136 
00137 int tm_strcasecmp(const char *s1, const char *s2);
00138 #define strcasecmp tm_strcasecmp
00139 
00140 void tm_gettime_consistent(uint32_t *high, uint32_t *low);
00141 uint32_t tm_time_subtract(uint32_t end, uint32_t start);
00142 int tm_u8s_per_bits(int bitCount);
00143 void TMR_stringCopy(TMR_String *dest, const char *src, int len);
00144 uint64_t TMR_makeBitMask(int offset, int lenght);
00145 uint32_t TMR_byteArrayToInt(uint8_t data[], int offset);
00146 uint16_t TMR_byteArrayToShort(uint8_t data[], int offset);
00147 uint64_t TMR_byteArrayToLong(uint8_t data[], int offset);
00148 #ifdef __cplusplus
00149 }
00150 #endif
00151 
00152 #endif /* _TMR_UTILS_H */


thingmagic_rfid
Author(s): Brian Bingham
autogenerated on Thu May 16 2019 03:01:24