Subversion Repositories HomeAutomation

Rev

Details | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
363 johboh 1
/*********************************************************************
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
* This software is owned by Microchip Technology Inc. ("Microchip")
17
* and is supplied to you for use exclusively as described in the
18
* associated software agreement.  This software is protected by
19
* software and other intellectual property laws.  Any use in
20
* violation of the software license may subject the user to criminal
21
* sanctions as well as civil liability.  Copyright 2006 Microchip
22
* Technology Inc.  All rights reserved.
23
*
24
* This software is provided "AS IS."  MICROCHIP DISCLAIMS ALL
25
* WARRANTIES, EXPRESS, IMPLIED, STATUTORY OR OTHERWISE, NOT LIMITED
26
* TO MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
27
* INFRINGEMENT.  Microchip shall in no event be liable for special,
28
* incidental, or consequential damages.
29
*
30
*
31
* Author               Date    Comment
32
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
33
* Nilesh Rajbharti     5/17/01 Original        (Rev 1.0)
34
* Nilesh Rajbharti     2/9/02  Cleanup
35
* Nilesh Rajbharti     6/25/02 Rewritten CalcIPChecksum() to avoid
36
*                              multi-byte shift operation.
37
* Howard Schlunder      2/9/05 Added hexatob(), btohexa_high(), and
38
*                              btohexa_low()
39
********************************************************************/
40
 
41
#include "..\Include\Compiler.h"
42
#include "..\Include\Helpers.h"
43
#include "..\Include\MAC.h"
44
 
45
 
46
 
47
#if defined(__C30__) || defined(HI_TECH_C)
48
/*********************************************************************
49
* Function:     void itoa(unsigned int Value, char *Buffer)
50
*
51
* PreCondition: None
52
*
53
* Input:        Value: Unsigned integer to be converted
54
*               Buffer: Pointer to a location to write the string
55
*
56
* Output:       *Buffer: Receives the resulting string
57
*
58
* Side Effects: None
59
*
60
* Overview:     The function converts an unsigned integer (16 bits)
61
*               into a null terminated decimal string.
62
*
63
* Note:         None
64
********************************************************************/
65
void itoa(unsigned int Value, char *Buffer)
66
{
67
    unsigned char i;
68
    unsigned int Digit;
69
    unsigned int Divisor;
70
    enum {FALSE = 0, TRUE} Printed = FALSE;
71
 
72
    if(Value)
73
    {
74
        for(i = 0, Divisor = 10000; i < 5; i++)
75
        {
76
            Digit = Value/Divisor;
77
            if(Digit || Printed)
78
            {
79
                *Buffer++ = '0' + Digit;
80
                Value -= Digit*Divisor;
81
                Printed = TRUE;
82
            }
83
            Divisor /= 10;
84
        }
85
    }
86
    else
87
    {
88
        *Buffer++ = '0';
89
    }
90
 
91
    *Buffer = '\0';
92
}              
93
#endif
94
 
95
 
96
/*********************************************************************
97
* Function:        BYTE hexatob(WORD_VAL AsciiChars)
98
*
99
* PreCondition:    None
100
*
101
* Input:           Two ascii bytes; each ranged '0'-'9', 'A'-'F', or
102
*                       'a'-'f'
103
*
104
* Output:          The resulting packed byte: 0x00-0xFF
105
*
106
* Side Effects:    None
107
*
108
* Overview:        None
109
*
110
* Note:         None
111
********************************************************************/
112
BYTE hexatob(WORD_VAL AsciiChars)
113
{
114
    // Convert lowercase to uppercase
115
    if(AsciiChars.v[1] > 'F')
116
        AsciiChars.v[1] -= 'a'-'A';
117
    if(AsciiChars.v[0] > 'F')
118
        AsciiChars.v[0] -= 'a'-'A';
119
 
120
    // Convert 0-9, A-F to 0x0-0xF
121
    if(AsciiChars.v[1] > '9')
122
        AsciiChars.v[1] -= 'A' - 10;
123
    else
124
        AsciiChars.v[1] -= '0';
125
 
126
    if(AsciiChars.v[0] > '9')
127
        AsciiChars.v[0] -= 'A' - 10;
128
    else
129
        AsciiChars.v[0] -= '0';
130
 
131
    // Concatenate
132
    return (AsciiChars.v[1]<<4) |  AsciiChars.v[0];
133
}
134
 
135
/*********************************************************************
136
* Function:        BYTE btohexa_high(BYTE b)
137
*
138
* PreCondition:    None
139
*
140
* Input:           One byte ranged 0x00-0xFF
141
*
142
* Output:          An ascii byte (always uppercase) between '0'-'9'
143
*                   or 'A'-'F' that corresponds to the upper 4 bits of
144
*                   the input byte.
145
*                   ex: b = 0xAE, btohexa_high() returns 'A'
146
*
147
* Side Effects:    None
148
*
149
* Overview:        None
150
*
151
* Note:         None
152
********************************************************************/
153
BYTE btohexa_high(BYTE b)
154
{
155
    b >>= 4;
156
    return (b>0x9) ? b+'A'-10:b+'0';
157
}
158
 
159
/*********************************************************************
160
* Function:        BYTE btohexa_low(BYTE b)
161
*
162
* PreCondition:    None
163
*
164
* Input:           One byte ranged 0x00-0xFF
165
*
166
* Output:          An ascii byte (always uppercase) between '0'-'9'
167
*                   or 'A'-'F' that corresponds to the lower 4 bits of
168
*                   the input byte.
169
*                   ex: b = 0xAE, btohexa_low() returns 'E'
170
*
171
* Side Effects:    None
172
*
173
* Overview:        None
174
*
175
* Note:         None
176
********************************************************************/
177
BYTE btohexa_low(BYTE b)
178
{
179
    b &= 0x0F;
180
    return (b>9) ? b+'A'-10:b+'0';
181
}
182
 
183
 
184
WORD swaps(WORD v)
185
{
186
    WORD_VAL t;
187
    BYTE b;
188
 
189
    t.Val   = v;
190
    b       = t.v[1];
191
    t.v[1]  = t.v[0];
192
    t.v[0]  = b;
193
 
194
    return t.Val;
195
}
196
 
197
 
198
DWORD swapl(DWORD v)
199
{
200
    BYTE b;
201
    DWORD myV;
202
    DWORD_VAL *myP;
203
 
204
    myV     = v;
205
    myP     = (DWORD_VAL*)&myV;
206
 
207
    b       = myP->v[3];
208
    myP->v[3] = myP->v[0];
209
    myP->v[0] = b;
210
 
211
    b       = myP->v[2];
212
    myP->v[2] = myP->v[1];
213
    myP->v[1] = b;
214
 
215
    return myV;
216
 
217
}
218
 
219
 
220
WORD CalcIPChecksum(BYTE* buffer, WORD count)
221
{
222
    WORD i;
223
    WORD *val;
224
 
225
    union
226
    {
227
        DWORD Val;
228
        struct
229
        {
230
            WORD_VAL LSB;
231
            WORD_VAL MSB;
232
        } words;
233
    } tempSum, sum;
234
 
235
    sum.Val = 0;
236
 
237
    i = count >> 1;
238
    val = (WORD *)buffer;
239
 
240
    while( i-- )
241
        sum.Val += *val++;
242
 
243
    if ( count & 1 )
244
        sum.Val += *(BYTE *)val;
245
 
246
    tempSum.Val = sum.Val;
247
 
248
    while( (i = tempSum.words.MSB.Val) != 0u )
249
    {
250
        sum.words.MSB.Val = 0;
251
        sum.Val = (DWORD)sum.words.LSB.Val + (DWORD)i;
252
        tempSum.Val = sum.Val;
253
    }
254
 
255
    return (~sum.words.LSB.Val);
256
}
257
 
258
 
259
/*********************************************************************
260
* Function:        WORD CalcIPBufferChecksum(WORD len)
261
*
262
* PreCondition:    TCPInit() is already called     AND
263
*                  MAC buffer pointer set to starting of buffer
264
*
265
* Input:           len     - Total number of bytes to calculate
266
*                          checksum for.
267
*
268
* Output:          16-bit checksum as defined by rfc 793.
269
*
270
* Side Effects:    None
271
*
272
* Overview:        This function performs checksum calculation in
273
*                  MAC buffer itself.
274
*
275
* Note:            None
276
********************************************************************/
277
#if defined(NON_MCHP_MAC)
278
WORD CalcIPBufferChecksum(WORD len)
279
{
280
    BOOL lbMSB;
281
    WORD_VAL checkSum;
282
    BYTE Checkbyte;
283
 
284
    lbMSB = TRUE;
285
    checkSum.Val = 0;
286
 
287
    while( len-- )
288
    {
289
        Checkbyte = MACGet();
290
 
291
        if ( !lbMSB )
292
        {
293
            if ( (checkSum.v[0] = Checkbyte+checkSum.v[0]) < Checkbyte)
294
            {
295
                if ( ++checkSum.v[1] == 0 )
296
                    checkSum.v[0]++;
297
            }
298
        }
299
        else
300
        {
301
            if ( (checkSum.v[1] = Checkbyte+checkSum.v[1]) < Checkbyte)
302
            {
303
                if ( ++checkSum.v[0] == 0 )
304
                    checkSum.v[1]++;
305
            }
306
        }
307
 
308
        lbMSB = !lbMSB;
309
    }
310
 
311
    checkSum.v[1] = ~checkSum.v[1];
312
    checkSum.v[0] = ~checkSum.v[0];
313
    return checkSum.Val;
314
}
315
#endif
316
 
317
/*********************************************************************
318
* Function:     char *strupr(char *s)
319
*
320
* PreCondition: None
321
*
322
* Input:        s: Pointer to a null terminated string to convert.
323
*
324
* Output:       char* return: Pointer to the initial string
325
*               *s: String is updated in to have all upper case
326
*                   characters
327
*
328
* Side Effects: None
329
*
330
* Overview:     The function sequentially converts all lower case
331
*               characters in the input s string to upper case
332
*               characters.  Non a-z characters are skipped.
333
*
334
* Note:         None
335
********************************************************************/
336
#if defined(__C30__) || defined(HI_TECH_C)
337
char *strupr(char *s)
338
{
339
    char c;
340
    char *t;
341
 
342
    t = s;
343
    while( (c = *t) )
344
    {
345
        if(c >= 'a' && c <= 'z')
346
        {
347
            *t -= ('a' - 'A');
348
        }
349
        t++;
350
    }
351
    return s;
352
}
353
#endif
354
 
355
 
356
void dwordToHex(DWORD c,char *str)
357
{
358
    BYTE b;
359
    str[0]='0';
360
    str[1]='x';
361
 
362
    b = c>>24;
363
    str[2]=btohexa_high(b);
364
    str[3]=btohexa_low(b);
365
 
366
    b = c>>16;
367
    str[4]=btohexa_high(b);
368
    str[5]=btohexa_low(b);
369
 
370
    b = c>>8;
371
    str[6]=btohexa_high(b);
372
    str[7]=btohexa_low(b);
373
 
374
    b = c;
375
    str[8]=btohexa_high(b);
376
    str[9]=btohexa_low(b);
377
 
378
    str[10]='\0';
379
}
380
 
381
void timestampToString(DWORD t, char *str)
382
{
383
        DWORD days=t/86400;
384
        DWORD day_sec=t%86400+7200;
385
 
386
        int year = days/365+1970;
387
 
388
        int dy = days%365; //left in year
389
        int month=0;
390
        int day=0;
391
 
392
 
393
        int daysOfMonth = 31;
394
        int i;
395
 
396
        int hh;
397
        int mm;
398
        int ss;
399
 
400
        int skottar = (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0));
401
 
402
        dy-=9; // corr!!
403
 
404
        for(i = 1; i <= 12; ++i)
405
        {
406
                if(dy <= daysOfMonth) { day=dy; break; }
407
                else { dy -= daysOfMonth; }
408
 
409
                if(daysOfMonth==31) daysOfMonth=30; else daysOfMonth=31;
410
                if (i==1) daysOfMonth=(skottar?29:28);
411
        }
412
 
413
        month=i;
414
 
415
        hh = day_sec/3600;
416
        mm = (day_sec%3600)/60;
417
        ss = (day_sec%3600)%60;
418
 
419
 
420
        str[0]=(year/1000)+48;
421
        str[1]=(year-(str[0]-48)*1000)/100+48;
422
        str[2]=(year-(str[0]-48)*1000-(str[1]-48)*100)/10+48;
423
        str[3]=(year-(str[0]-48)*1000-(str[1]-48)*100-(str[2]-48)*10)+48;
424
        str[4]='-';
425
        str[5]=month/10+48;
426
        str[6]=(month-(str[5]-48)*10)+48;
427
        str[7]='-';
428
        str[8]=day/10+48;
429
        str[9]=(day-(str[8]-48)*10)+48;
430
        str[10]=' ';
431
        str[11]=hh/10+48;
432
        str[12]=(hh-(str[11]-48)*10)+48;   
433
        str[13]=':';
434
        str[14]=mm/10+48;
435
        str[15]=(mm-(str[14]-48)*10)+48;
436
        str[16]=':';
437
        str[17]=ss/10+48;
438
        str[18]=(ss-(str[17]-48)*10)+48;
439
        str[19]='\0';
440
}