/[H8]/trunk/docs/Microchip TCP_IP stack/TCPIP Stack/Helpers.c
ViewVC logotype

Annotation of /trunk/docs/Microchip TCP_IP stack/TCPIP Stack/Helpers.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 15 - (hide annotations) (download)
Thu Apr 19 09:01:15 2007 UTC (17 years, 2 months ago) by hedin
File MIME type: text/plain
File size: 11797 byte(s)
added the TCP/IP stack, source code.
1 hedin 15 /*********************************************************************
2     *
3     * Helper Functions for Microchip TCP/IP Stack
4     *
5     *********************************************************************
6     * FileName: Helpers.C
7     * Dependencies: Compiler.h
8     * Helpers.h
9     * Processor: PIC18, PIC24F, PIC24H, dsPIC30F, dsPIC33F
10     * Complier: Microchip C18 v3.02 or higher
11     * Microchip C30 v2.01 or higher
12     * Company: Microchip Technology, Inc.
13     *
14     * Software License Agreement
15     *
16     * Copyright © 2002-2007 Microchip Technology Inc. All rights
17     * reserved.
18     *
19     * Microchip licenses to you the right to use, modify, copy, and
20     * distribute:
21     * (i) the Software when embedded on a Microchip microcontroller or
22     * digital signal controller product (“Device”) which is
23     * integrated into Licensee’s product; or
24     * (ii) ONLY the Software driver source files ENC28J60.c and
25     * ENC28J60.h ported to a non-Microchip device used in
26     * conjunction with a Microchip ethernet controller for the
27     * sole purpose of interfacing with the ethernet controller.
28     *
29     * You should refer to the license agreement accompanying this
30     * Software for additional information regarding your rights and
31     * obligations.
32     *
33     * THE SOFTWARE AND DOCUMENTATION ARE PROVIDED “AS IS” WITHOUT
34     * WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT
35     * LIMITATION, ANY WARRANTY OF MERCHANTABILITY, FITNESS FOR A
36     * PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL
37     * MICROCHIP BE LIABLE FOR ANY INCIDENTAL, SPECIAL, INDIRECT OR
38     * CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF
39     * PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR SERVICES, ANY CLAIMS
40     * BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE
41     * THEREOF), ANY CLAIMS FOR INDEMNITY OR CONTRIBUTION, OR OTHER
42     * SIMILAR COSTS, WHETHER ASSERTED ON THE BASIS OF CONTRACT, TORT
43     * (INCLUDING NEGLIGENCE), BREACH OF WARRANTY, OR OTHERWISE.
44     *
45     *
46     * Author Date Comment
47     *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
48     * Nilesh Rajbharti 5/17/01 Original (Rev 1.0)
49     * Nilesh Rajbharti 2/9/02 Cleanup
50     * Nilesh Rajbharti 6/25/02 Rewritten CalcIPChecksum() to avoid
51     * multi-byte shift operation.
52     * Howard Schlunder 2/9/05 Added hexatob(), btohexa_high(), and
53     * btohexa_low()
54     * Howard Schlunder 10/10/06 Optimized swapl()
55     ********************************************************************/
56     #define __HELPERS_C
57    
58     #include "TCPIP Stack/TCPIP.h"
59    
60    
61     #if defined(STACK_USE_HTTP_SERVER)
62     /*********************************************************************
63     * Function: void UnencodeURL(BYTE *URL)
64     *
65     * PreCondition: None
66     *
67     * Input: *URL: Pointer to a null terminated URL string
68     *
69     * Output: All escape sequences (ex: %20) are converted to
70     * liternal representations in the URL string.
71     *
72     * Side Effects: None
73     *
74     * Overview: None
75     *
76     * Note: None
77     ********************************************************************/
78     void UnencodeURL(BYTE *URL)
79     {
80     BYTE *Right, *Copy;
81     WORD_VAL Number;
82    
83     while(Right = (BYTE*)strchr((char*)URL, '%'))
84     {
85     // Make sure the string is long enough
86     if(Right[1] == '\0')
87     break;
88     if(Right[2] == '\0')
89     break;
90    
91     // Update the string in place
92     Number.v[0] = Right[2];
93     Number.v[1] = Right[1];
94     *Right++ = hexatob(Number);
95     URL = Right;
96    
97     // Remove two blank spots by shifting all remaining characters right two
98     Copy = Right + 2;
99     while(*Right++ = *Copy++);
100     }
101     }
102     #endif
103    
104    
105     /*********************************************************************
106     * Function: BOOL StringToIPAddress(BYTE *str, IP_ADDR *IPAddress)
107     *
108     * PreCondition: None
109     *
110     * Input: str: Pointer to a null terminated URL, hostname, or dotted decimal IP address string
111     * IPAddress: Pointer to a location to write the decoded IP address. Result is big endian.
112     *
113     * Output: TRUE if a IP address was successfully decoded
114     * FALSE if no IP address could be found or wrong format
115     *
116     * Side Effects: None
117     *
118     * Overview: None
119     *
120     * Note: None
121     ********************************************************************/
122     BOOL StringToIPAddress(BYTE *str, IP_ADDR *IPAddress)
123     {
124     BYTE i, j, k;
125     BYTE buffer[4];
126    
127     j = 0;
128     k = 0;
129    
130     while(i = *str++)
131     {
132     if(i == '.')
133     {
134     buffer[j] = '\0';
135     j = 0;
136     IPAddress->v[k++] = atoi((char*)buffer);
137     }
138     else if(i < '0' || i > '9')
139     {
140     return FALSE;
141     }
142     else
143     {
144     buffer[j++] = i;
145     if(k >= sizeof(IP_ADDR))
146     return FALSE;
147     }
148    
149     if(j >= sizeof(buffer))
150     return FALSE;
151     }
152    
153     buffer[j] = '\0';
154     IPAddress->v[k] = atoi((char*)buffer);
155     return (k == 3u);
156     }
157    
158    
159     //#if defined(__C30__) || defined(HI_TECH_C)
160     /*********************************************************************
161     * Function: void uitoa(WORD Value, BYTE *Buffer)
162     *
163     * PreCondition: None
164     *
165     * Input: Value: Unsigned integer to be converted
166     * Buffer: Pointer to a location to write the string
167     *
168     * Output: *Buffer: Receives the resulting string
169     *
170     * Side Effects: None
171     *
172     * Overview: The function converts an unsigned integer (16 bits)
173     * into a null terminated decimal string.
174     *
175     * Note: None
176     ********************************************************************/
177     void uitoa(WORD Value, BYTE *Buffer)
178     {
179     BYTE i;
180     WORD Digit;
181     WORD Divisor;
182     BOOL Printed = FALSE;
183    
184     if(Value)
185     {
186     for(i = 0, Divisor = 10000; i < 5; i++)
187     {
188     Digit = Value/Divisor;
189     if(Digit || Printed)
190     {
191     *Buffer++ = '0' + Digit;
192     Value -= Digit*Divisor;
193     Printed = TRUE;
194     }
195     Divisor /= 10;
196     }
197     }
198     else
199     {
200     *Buffer++ = '0';
201     }
202    
203     *Buffer = '\0';
204     }
205     //#endif
206    
207    
208     /*********************************************************************
209     * Function: BYTE hexatob(WORD_VAL AsciiChars)
210     *
211     * PreCondition: None
212     *
213     * Input: Two ascii bytes; each ranged '0'-'9', 'A'-'F', or
214     * 'a'-'f'
215     *
216     * Output: The resulting packed byte: 0x00-0xFF
217     *
218     * Side Effects: None
219     *
220     * Overview: None
221     *
222     * Note: None
223     ********************************************************************/
224     BYTE hexatob(WORD_VAL AsciiChars)
225     {
226     // Convert lowercase to uppercase
227     if(AsciiChars.v[1] > 'F')
228     AsciiChars.v[1] -= 'a'-'A';
229     if(AsciiChars.v[0] > 'F')
230     AsciiChars.v[0] -= 'a'-'A';
231    
232     // Convert 0-9, A-F to 0x0-0xF
233     if(AsciiChars.v[1] > '9')
234     AsciiChars.v[1] -= 'A' - 10;
235     else
236     AsciiChars.v[1] -= '0';
237    
238     if(AsciiChars.v[0] > '9')
239     AsciiChars.v[0] -= 'A' - 10;
240     else
241     AsciiChars.v[0] -= '0';
242    
243     // Concatenate
244     return (AsciiChars.v[1]<<4) | AsciiChars.v[0];
245     }
246    
247     /*********************************************************************
248     * Function: BYTE btohexa_high(BYTE b)
249     *
250     * PreCondition: None
251     *
252     * Input: One byte ranged 0x00-0xFF
253     *
254     * Output: An ascii byte (always uppercase) between '0'-'9'
255     * or 'A'-'F' that corresponds to the upper 4 bits of
256     * the input byte.
257     * ex: b = 0xAE, btohexa_high() returns 'A'
258     *
259     * Side Effects: None
260     *
261     * Overview: None
262     *
263     * Note: None
264     ********************************************************************/
265     BYTE btohexa_high(BYTE b)
266     {
267     b >>= 4;
268     return (b>0x9u) ? b+'A'-10:b+'0';
269     }
270    
271     /*********************************************************************
272     * Function: BYTE btohexa_low(BYTE b)
273     *
274     * PreCondition: None
275     *
276     * Input: One byte ranged 0x00-0xFF
277     *
278     * Output: An ascii byte (always uppercase) between '0'-'9'
279     * or 'A'-'F' that corresponds to the lower 4 bits of
280     * the input byte.
281     * ex: b = 0xAE, btohexa_low() returns 'E'
282     *
283     * Side Effects: None
284     *
285     * Overview: None
286     *
287     * Note: None
288     ********************************************************************/
289     BYTE btohexa_low(BYTE b)
290     {
291     b &= 0x0F;
292     return (b>9u) ? b+'A'-10:b+'0';
293     }
294    
295    
296     WORD swaps(WORD v)
297     {
298     WORD_VAL t;
299     BYTE b;
300    
301     t.Val = v;
302     b = t.v[1];
303     t.v[1] = t.v[0];
304     t.v[0] = b;
305    
306     return t.Val;
307     }
308    
309    
310     DWORD swapl(DWORD v)
311     {
312     // Swap bytes 0 and 3
313     ((DWORD_VAL*)&v)->v[0] ^= ((DWORD_VAL*)&v)->v[3];
314     ((DWORD_VAL*)&v)->v[3] ^= ((DWORD_VAL*)&v)->v[0];
315     ((DWORD_VAL*)&v)->v[0] ^= ((DWORD_VAL*)&v)->v[3];
316    
317     // Swap bytes 1 and 2
318     ((DWORD_VAL*)&v)->v[1] ^= ((DWORD_VAL*)&v)->v[2];
319     ((DWORD_VAL*)&v)->v[2] ^= ((DWORD_VAL*)&v)->v[1];
320     ((DWORD_VAL*)&v)->v[1] ^= ((DWORD_VAL*)&v)->v[2];
321    
322     return v;
323     }
324    
325    
326     WORD CalcIPChecksum(BYTE* buffer, WORD count)
327     {
328     WORD i;
329     WORD *val;
330     DWORD_VAL sum = {0x00000000ul};
331    
332     i = count >> 1;
333     val = (WORD*)buffer;
334    
335     // Calculate the sum of all words
336     while(i--)
337     sum.Val += (DWORD)*val++;
338    
339     // Add in the sum of the remaining byte, if present
340     if(((WORD_VAL*)&count)->bits.b0)
341     sum.Val += (DWORD)*(BYTE*)val;
342    
343     // Do an end-around carry (one's complement arrithmatic)
344     sum.Val = (DWORD)sum.w[0] + (DWORD)sum.w[1];
345    
346     // Do another end-around carry in case if the prior add
347     // caused a carry out
348     sum.w[0] += sum.w[1];
349    
350     // Return the resulting checksum
351     return ~sum.w[0];
352     }
353    
354    
355     /*********************************************************************
356     * Function: WORD CalcIPBufferChecksum(WORD len)
357     *
358     * PreCondition: TCPInit() is already called AND
359     * MAC buffer pointer set to starting of buffer
360     *
361     * Input: len - Total number of bytes to calculate
362     * checksum for.
363     *
364     * Output: 16-bit checksum as defined by rfc 793.
365     *
366     * Side Effects: None
367     *
368     * Overview: This function performs checksum calculation in
369     * MAC buffer itself.
370     *
371     * Note: None
372     ********************************************************************/
373     #if defined(NON_MCHP_MAC)
374     WORD CalcIPBufferChecksum(WORD len)
375     {
376     DWORD_VAL Checksum = {0x00000000ul};
377     WORD ChunkLen;
378     BYTE DataBuffer[20]; // Must be an even size
379     WORD *DataPtr;
380    
381     while(len)
382     {
383     // Obtain a chunk of data (less SPI overhead compared
384     // to requesting one byte at a time)
385     ChunkLen = len > sizeof(DataBuffer) ? sizeof(DataBuffer) : len;
386     MACGetArray(DataBuffer, ChunkLen);
387     len -= ChunkLen;
388    
389     // Take care of a last odd numbered data byte
390     if(((WORD_VAL*)&ChunkLen)->bits.b0)
391     {
392     DataBuffer[ChunkLen] = 0x00;
393     ChunkLen++;
394     }
395    
396     // Calculate the checksum over this chunk
397     DataPtr = (WORD*)&DataBuffer[0];
398     while(ChunkLen)
399     {
400     Checksum.Val += *DataPtr++;
401     ChunkLen -= 2;
402     }
403     }
404    
405     // Do an end-around carry (one's complement arrithmatic)
406     Checksum.Val = (DWORD)Checksum.w[0] + (DWORD)Checksum.w[1];
407    
408     // Do another end-around carry in case if the prior add
409     // caused a carry out
410     Checksum.w[0] += Checksum.w[1];
411    
412     // Return the resulting checksum
413     return ~Checksum.w[0];
414     }
415     #endif
416    
417     /*********************************************************************
418     * Function: char *strupr(char *s)
419     *
420     * PreCondition: None
421     *
422     * Input: s: Pointer to a null terminated string to convert.
423     *
424     * Output: char* return: Pointer to the initial string
425     * *s: String is updated in to have all upper case
426     * characters
427     *
428     * Side Effects: None
429     *
430     * Overview: The function sequentially converts all lower case
431     * characters in the input s string to upper case
432     * characters. Non a-z characters are skipped.
433     *
434     * Note: None
435     ********************************************************************/
436     #if defined(__C30__) || defined(HI_TECH_C)
437     char *strupr(char *s)
438     {
439     char c;
440     char *t;
441    
442     t = s;
443     while( (c = *t) )
444     {
445     if(c >= 'a' && c <= 'z')
446     {
447     *t -= ('a' - 'A');
448     }
449     t++;
450     }
451     return s;
452     }
453     #endif

  ViewVC Help
Powered by ViewVC 1.1.20