tmr_utils.c
Go to the documentation of this file.
00001 
00008  /*
00009  * Copyright (c) 2009 ThingMagic, Inc.
00010  *
00011  * Permission is hereby granted, free of charge, to any person obtaining a copy
00012  * of this software and associated documentation files (the "Software"), to deal
00013  * in the Software without restriction, including without limitation the rights
00014  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00015  * copies of the Software, and to permit persons to whom the Software is
00016  * furnished to do so, subject to the following conditions:
00017  *
00018  * The above copyright notice and this permission notice shall be included in
00019  * all copies or substantial portions of the Software.
00020  * 
00021  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00022  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00023  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00024  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00025  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00026  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00027  * THE SOFTWARE.
00028  */
00029 
00030 #include <stddef.h>
00031 
00032 #include "osdep.h"
00033 #include "tmr_types.h"
00034 #include "tmr_utils.h"
00035 
00036 #ifndef TMR_USE_HOST_C_LIBRARY
00037 
00038 void *
00039 tm_memcpy(void *dest, const void *src, size_t n)
00040 {
00041   const uint8_t *u8src;
00042   uint8_t *u8dest;
00043 
00044   u8dest = dest;
00045   u8src = src;
00046 
00047   while (0 != n)
00048   {
00049     *u8dest = *u8src;
00050     u8dest++;
00051     u8src++;
00052     n--;
00053   }
00054 
00055   return dest;
00056 }
00057 
00058 char *
00059 tm_strcpy(char *dest, const char *src)
00060 {
00061   char *ptr, val;
00062 
00063   ptr = dest;
00064 
00065   do
00066   {
00067     val = *src;
00068     *ptr = val;
00069     ptr++;
00070     src++;
00071   } 
00072   while ('\0' != val);
00073 
00074   return dest;
00075 }
00076 
00077 char *
00078 tm_strchr(const char *s, int c)
00079 {
00080   const char *ptr;
00081 
00082   for (ptr = s; *ptr != '\0'; ptr++)
00083   {
00084     if (*ptr == c)
00085     {
00086       return (char *)ptr;
00087     }
00088   }
00089 
00090   return NULL;
00091 }
00092 
00093 #endif /* TMR_USE_HOST_C_LIBRARY */
00094 
00095 int
00096 tm_strcasecmp(const char *s1, const char *s2)
00097 {
00098   char v1, v2;
00099   int diff;
00100 
00101   do
00102   {
00103     v1 = *s1;
00104     if (v1 >= 'a' && v1 <= 'z')
00105       v1 -= 'a' - 'A';
00106     v2 = *s2;
00107     if (v2 >= 'a' && v2 <= 'z')
00108       v2 -= 'a' - 'A';
00109     s1++;
00110     s2++;
00111   }
00112   while (v1 == v2 && '\0' != v1 && '\0' != v2);
00113 
00114   diff = v1 - v2;
00115   if (diff < 0)
00116     return -1;
00117   else if (diff > 0)
00118     return 1;
00119   else
00120     return 0;
00121 }
00122 
00123 /* Get a high/low timestamp pair, being careful not to get one value
00124  * before a wraparound and the other one after, which would lead to
00125  * 50-day time jumps forward or backwards.  (high<<32)|low is in milliseconds.
00126  */
00127 void
00128 tm_gettime_consistent(uint32_t *high, uint32_t *low)
00129 {
00130   uint32_t tmpHigh;
00131 
00132   *high   = tmr_gettime_high();
00133   *low    = tmr_gettime_low();
00134   tmpHigh = tmr_gettime_high();
00135 
00136   if (tmpHigh != *high)
00137   {
00138     *high = tmpHigh;
00139     *low = tmr_gettime_low();
00140   }
00141 }
00142 
00143 /* Find the time difference from start to end, allowing for end having
00144  * wrapped around UINT32_MAX and back to zero.
00145  */
00146 uint32_t
00147 tm_time_subtract(uint32_t end, uint32_t start)
00148 {
00149   if (end >= start)
00150     return end - start;
00151   else
00152     return (UINT32_MAX - start) + end;
00153 }
00154 
00160 int tm_u8s_per_bits(int bitCount) 
00161 {
00162         return ((0<bitCount) ?((((bitCount)-1)>>3)+1) :0);
00163 }
00164 
00165 void
00166 TMR_stringCopy(TMR_String *dest, const char *src, int len)
00167 {
00168   if (dest->max - 1 < len)
00169   {
00170     len = dest->max - 1;
00171   }
00172   if (dest->max > 0)
00173   {
00174     tm_memcpy(dest->value, src, len);
00175     dest->value[len] = '\0';
00176   }
00177 }
00178 
00179 uint64_t
00180 TMR_makeBitMask(int offset, int length)
00181 {
00182   uint64_t mask;
00183 
00184   mask = 0;
00185   mask = ~mask;
00186   mask <<= length;
00187   mask = ~mask;
00188 
00189   mask <<= offset;
00190 
00191   return mask;
00192 }
00193 
00194 uint32_t
00195 TMR_byteArrayToInt(uint8_t data[], int offset)
00196 {
00197   uint32_t value = 0;
00198   int count;
00199 
00200   for( count = 0; count < 4; count++)
00201   {
00202     value <<= 8;
00203     value ^= (data[count + offset] & 0x000000FF);
00204   }
00205   return value;
00206 }
00207 
00208 uint16_t
00209 TMR_byteArrayToShort(uint8_t data[], int offset)
00210 {
00211   uint16_t value = 0;
00212   int hi, lo;
00213   hi = (uint16_t) (data[offset++]) << 8;
00214   lo = (uint16_t) (data[offset++]);
00215   value = (uint16_t)(hi | lo);
00216 
00217   return value;
00218 }
00219 
00220 uint64_t
00221 TMR_byteArrayToLong(uint8_t data[], int offset)
00222 {
00223   uint64_t value = 0;
00224   int i;
00225   for (i = 0; i < 8; i++)
00226   {
00227     value <<= 8;
00228     value ^= (uint64_t) (data[i + offset] & 0xff);
00229   }
00230   return value;
00231 }
00232 


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