Rtos API example

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers lwip_def.c Source File

lwip_def.c

Go to the documentation of this file.
00001 /**
00002  * @file
00003  * Common functions used throughout the stack.
00004  *
00005  * These are reference implementations of the byte swapping functions.
00006  * Again with the aim of being simple, correct and fully portable.
00007  * Byte swapping is the second thing you would want to optimize. You will
00008  * need to port it to your architecture and in your cc.h:
00009  *
00010  * \#define lwip_htons(x) your_htons
00011  * \#define lwip_htonl(x) your_htonl
00012  *
00013  * Note lwip_ntohs() and lwip_ntohl() are merely references to the htonx counterparts.
00014  * 
00015  * If you \#define them to htons() and htonl(), you should
00016  * \#define LWIP_DONT_PROVIDE_BYTEORDER_FUNCTIONS to prevent lwIP from
00017  * defining htonx/ntohx compatibility macros.
00018 
00019  * @defgroup sys_nonstandard Non-standard functions
00020  * @ingroup sys_layer
00021  * lwIP provides default implementations for non-standard functions.
00022  * These can be mapped to OS functions to reduce code footprint if desired.
00023  * All defines related to this section must not be placed in lwipopts.h,
00024  * but in arch/cc.h!
00025  * These options cannot be \#defined in lwipopts.h since they are not options
00026  * of lwIP itself, but options of the lwIP port to your system.
00027  */
00028 
00029 /*
00030  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
00031  * All rights reserved.
00032  *
00033  * Redistribution and use in source and binary forms, with or without modification,
00034  * are permitted provided that the following conditions are met:
00035  *
00036  * 1. Redistributions of source code must retain the above copyright notice,
00037  *    this list of conditions and the following disclaimer.
00038  * 2. Redistributions in binary form must reproduce the above copyright notice,
00039  *    this list of conditions and the following disclaimer in the documentation
00040  *    and/or other materials provided with the distribution.
00041  * 3. The name of the author may not be used to endorse or promote products
00042  *    derived from this software without specific prior written permission.
00043  *
00044  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
00045  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00046  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
00047  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00048  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
00049  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00050  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00051  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
00052  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
00053  * OF SUCH DAMAGE.
00054  *
00055  * This file is part of the lwIP TCP/IP stack.
00056  *
00057  * Author: Simon Goldschmidt
00058  *
00059  */
00060 
00061 #include "lwip/opt.h"
00062 #include "lwip/def.h"
00063 
00064 #include <string.h>
00065 
00066 #if BYTE_ORDER == LITTLE_ENDIAN
00067 
00068 #if !defined(lwip_htons)
00069 /**
00070  * Convert an u16_t from host- to network byte order.
00071  *
00072  * @param n u16_t in host byte order
00073  * @return n in network byte order
00074  */
00075 u16_t
00076 lwip_htons(u16_t n)
00077 {
00078   return (u16_t)PP_HTONS(n);
00079 }
00080 #endif /* lwip_htons */
00081 
00082 #if !defined(lwip_htonl)
00083 /**
00084  * Convert an u32_t from host- to network byte order.
00085  *
00086  * @param n u32_t in host byte order
00087  * @return n in network byte order
00088  */
00089 u32_t
00090 lwip_htonl(u32_t n)
00091 {
00092   return (u32_t)PP_HTONL(n);
00093 }
00094 #endif /* lwip_htonl */
00095 
00096 #endif /* BYTE_ORDER == LITTLE_ENDIAN */
00097 
00098 #ifndef lwip_strnstr
00099 /**
00100  * @ingroup sys_nonstandard
00101  * lwIP default implementation for strnstr() non-standard function.
00102  * This can be \#defined to strnstr() depending on your platform port.
00103  */
00104 char*
00105 lwip_strnstr(const char* buffer, const char* token, size_t n)
00106 {
00107   const char* p;
00108   size_t tokenlen = strlen(token);
00109   if (tokenlen == 0) {
00110     return LWIP_CONST_CAST(char *, buffer);
00111   }
00112   for (p = buffer; *p && (p + tokenlen <= buffer + n); p++) {
00113     if ((*p == *token) && (strncmp(p, token, tokenlen) == 0)) {
00114       return LWIP_CONST_CAST(char *, p);
00115     }
00116   }
00117   return NULL;
00118 }
00119 #endif
00120 
00121 #ifndef lwip_stricmp
00122 /**
00123  * @ingroup sys_nonstandard
00124  * lwIP default implementation for stricmp() non-standard function.
00125  * This can be \#defined to stricmp() depending on your platform port.
00126  */
00127 int
00128 lwip_stricmp(const char* str1, const char* str2)
00129 {
00130   char c1, c2;
00131 
00132   do {
00133     c1 = *str1++;
00134     c2 = *str2++;
00135     if (c1 != c2) {
00136       char c1_upc = c1 | 0x20;
00137       if ((c1_upc >= 'a') && (c1_upc <= 'z')) {
00138         /* characters are not equal an one is in the alphabet range:
00139         downcase both chars and check again */
00140         char c2_upc = c2 | 0x20;
00141         if (c1_upc != c2_upc) {
00142           /* still not equal */
00143           /* don't care for < or > */
00144           return 1;
00145         }
00146       } else {
00147         /* characters are not equal but none is in the alphabet range */
00148         return 1;
00149       }
00150     }
00151   } while (c1 != 0);
00152   return 0;
00153 }
00154 #endif
00155 
00156 #ifndef lwip_strnicmp
00157 /**
00158  * @ingroup sys_nonstandard
00159  * lwIP default implementation for strnicmp() non-standard function.
00160  * This can be \#defined to strnicmp() depending on your platform port.
00161  */
00162 int
00163 lwip_strnicmp(const char* str1, const char* str2, size_t len)
00164 {
00165   char c1, c2;
00166 
00167   do {
00168     c1 = *str1++;
00169     c2 = *str2++;
00170     if (c1 != c2) {
00171       char c1_upc = c1 | 0x20;
00172       if ((c1_upc >= 'a') && (c1_upc <= 'z')) {
00173         /* characters are not equal an one is in the alphabet range:
00174         downcase both chars and check again */
00175         char c2_upc = c2 | 0x20;
00176         if (c1_upc != c2_upc) {
00177           /* still not equal */
00178           /* don't care for < or > */
00179           return 1;
00180         }
00181       } else {
00182         /* characters are not equal but none is in the alphabet range */
00183         return 1;
00184       }
00185     }
00186   } while (len-- && c1 != 0);
00187   return 0;
00188 }
00189 #endif
00190 
00191 #ifndef lwip_itoa
00192 /**
00193  * @ingroup sys_nonstandard
00194  * lwIP default implementation for itoa() non-standard function.
00195  * This can be \#defined to itoa() or snprintf(result, bufsize, "%d", number) depending on your platform port.
00196  */
00197 void
00198 lwip_itoa(char* result, size_t bufsize, int number)
00199 {
00200   const int base = 10;
00201   char* ptr = result, *ptr1 = result, tmp_char;
00202   int tmp_value;
00203   LWIP_UNUSED_ARG(bufsize);
00204 
00205   do {
00206     tmp_value = number;
00207     number /= base;
00208     *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz"[35 + (tmp_value - number * base)];
00209   } while(number);
00210 
00211    /* Apply negative sign */
00212   if (tmp_value < 0) {
00213      *ptr++ = '-';
00214   }
00215   *ptr-- = '\0';
00216   while(ptr1 < ptr) {
00217     tmp_char = *ptr;
00218     *ptr--= *ptr1;
00219     *ptr1++ = tmp_char;
00220   }
00221 }
00222 #endif