?lang_form? ?lang_select? ?lang_submit? ?lang_endform?
{HEADER END}
{BLAME START}

library

?curdirlinks? -

Blame information for rev 32

Line No. Rev Author Line
1 32 kaklik /******************************************************************************
2  
3 MRF24W10C Driver iwpriv
4 Module for Microchip TCP/IP Stack
5 -Provides access to MRF24W10C WiFi controller
6 -Reference: MRF24W10C Data sheet, IEEE 802.11 Standard
7  
8 *******************************************************************************
9 FileName: WFConsoleIwpriv.c
10 Dependencies: TCP/IP Stack header files
11 Processor: PIC18, PIC24F, PIC24H, dsPIC30F, dsPIC33F, PIC32
12 Compiler: Microchip C32 v1.10b or higher
13 Microchip C30 v3.22 or higher
14 Microchip C18 v3.34 or higher
15 Company: Microchip Technology, Inc.
16  
17 Software License Agreement
18  
19 Copyright (C) 2002-2010 Microchip Technology Inc. All rights reserved.
20  
21 Microchip licenses to you the right to use, modify, copy, and distribute:
22 (i) the Software when embedded on a Microchip microcontroller or digital
23 signal controller product ("Device") which is integrated into
24 Licensee's product; or
25 (ii) ONLY the Software driver source files ENC28J60.c, ENC28J60.h,
26 ENCX24J600.c and ENCX24J600.h ported to a non-Microchip device used in
27 conjunction with a Microchip ethernet controller for the sole purpose
28 of interfacing with the ethernet controller.
29  
30 You should refer to the license agreement accompanying this Software for
31 additional information regarding your rights and obligations.
32  
33 THE SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
34 KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY
35 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
36 NON-INFRINGEMENT. IN NO EVENT SHALL MICROCHIP BE LIABLE FOR ANY INCIDENTAL,
37 SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST
38 OF PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR SERVICES, ANY CLAIMS BY
39 THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), ANY CLAIMS
40 FOR INDEMNITY OR CONTRIBUTION, OR OTHER SIMILAR COSTS, WHETHER ASSERTED ON
41 THE BASIS OF CONTRACT, TORT (INCLUDING NEGLIGENCE), BREACH OF WARRANTY, OR
42 OTHERWISE.
43  
44  
45 Author Date Comment
46 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
47 KH 27 Jan 2010 Updated for MRF24WB0M
48 ******************************************************************************/
49  
50  
51 //============================================================================
52 // Includes
53 //============================================================================
54 #include <ctype.h>
55 #include <stdio.h>
56 #include <string.h>
57  
58 #include "TCPIP Stack/TCPIP.h"
59 #include "TCPIP Stack/WFConsole.h"
60  
61 #if defined( WF_CONSOLE_IFCFGUTIL )
62  
63 #include "TCPIP Stack/WFConsoleMsgs.h"
64 #include "TCPIP Stack/WFConsoleMsgHandler.h"
65 #include "TCPIP Stack/WFConsoleIwconfig.h" // for using iwconfigCb
66 #include "TCPIP Stack/WFConsoleIwpriv.h"
67  
68 //============================================================================
69 // Constants
70 //============================================================================
71  
72 #define IWPRIV_WEB_KEY_NUM (4u)
73 #define IWPRIV_WEB_LONG_KEY_LEN (13u) // in byte
74 #define IWPRIV_WEB_SHORT_KEY_LEN (5u) // in byte
75  
76 //============================================================================
77 // Globals
78 //============================================================================
79  
80 static BOOL iwprivCbInitialized = FALSE;
81 static struct
82 {
83 // NOTE: cpId, ssid, and wepDefaultKeyId
84 // are refreshed at each iwpriv
85  
86 UINT8 cpId; // conn. profile ID
87 UINT8 ssid[WF_MAX_SSID_LENGTH+1];
88 UINT8 wepDefaultKeyId;
89  
90 // NOTE: securityType, securityKey and securityKeyLength are
91 // not refreshed at each iwpriv
92  
93 UINT8 securityType;
94 UINT8 securityKey[WF_MAX_SECURITY_KEY_LENGTH+1];
95 UINT8 securityKeyLength;
96 } iwprivCb;
97  
98 //============================================================================
99 // Local Function Prototypes
100 //============================================================================
101  
102 /*****************************************************************************
103 * FUNCTION: iwprivSetCb
104 *
105 * RETURNS: TRUE or FALSE
106 *
107 * PARAMS: None
108 *
109 * NOTES: Set the iwprivCb structure
110 *****************************************************************************/
111 static BOOL iwprivSetCb(void)
112 {
113 tWFCPElements cprof;
114 BOOL cpIdChanged = FALSE;
115  
116 if ( !iwprivCbInitialized ) // first time call of iwprivSetCb
117 {
118 memset(&iwprivCb, 0, sizeof(iwprivCb));
119 iwprivCbInitialized = TRUE;
120 }
121  
122 if ( !iwconfigSetCb() ) // first set iwconfigCb
123 return FALSE;
124  
125 if ( iwprivCb.cpId != iwconfigCb.cpId)
126 {
127 iwprivCb.cpId = iwconfigCb.cpId;
128 cpIdChanged = TRUE;
129 }
130  
131 WF_CPGetElements(iwprivCb.cpId, &cprof);
132  
133 // set refreshable part of iwprivCb
134 {
135 memcpy((void*)iwprivCb.ssid, (const void*)cprof.ssid, cprof.ssidLength);
136 iwprivCb.ssid[cprof.ssidLength] = '\0';
137  
138 iwprivCb.wepDefaultKeyId = cprof.wepDefaultKeyId;
139 }
140  
141 // set non-refreshable part of iwprivCb only when cpId has changed
142 if (cpIdChanged)
143 {
144 iwprivCb.securityType = cprof.securityType;
145 if (iwprivCb.securityType == WF_SECURITY_WPA_WITH_KEY || iwprivCb.securityType == WF_SECURITY_WPA2_WITH_KEY)
146 {
147 iwprivCb.securityType = WF_SECURITY_WPA_AUTO_WITH_KEY;
148 }
149 else if (iwprivCb.securityType == WF_SECURITY_WPA_WITH_PASS_PHRASE || iwprivCb.securityType == WF_SECURITY_WPA2_WITH_PASS_PHRASE)
150 {
151 iwprivCb.securityType = WF_SECURITY_WPA_AUTO_WITH_PASS_PHRASE;
152 }
153  
154 iwprivCb.securityKeyLength = 0;
155 }
156  
157 return TRUE;
158 }
159  
160 static void iwprivDisplayStatus(void)
161 {
162 UINT8 i, j;
163 UINT8* p;
164  
165 // security type
166 {
167 WFConsolePrintRomStr("Encryption: ", FALSE);
168  
169 switch (iwprivCb.securityType)
170 {
171 case WF_SECURITY_OPEN:
172 WFConsolePrintRomStr("none", TRUE);
173 break;
174 case WF_SECURITY_WEP_40:
175 case WF_SECURITY_WEP_104:
176 WFConsolePrintRomStr("wep", TRUE);
177 break;
178 case WF_SECURITY_WPA_WITH_KEY:
179 case WF_SECURITY_WPA2_WITH_KEY:
180 case WF_SECURITY_WPA_AUTO_WITH_KEY:
181 WFConsolePrintRomStr("wpa-psk", TRUE);
182 break;
183 case WF_SECURITY_WPA_WITH_PASS_PHRASE:
184 case WF_SECURITY_WPA2_WITH_PASS_PHRASE:
185 case WF_SECURITY_WPA_AUTO_WITH_PASS_PHRASE:
186 WFConsolePrintRomStr("wpa-phrase", TRUE);
187 break;
188 default:
189 WFConsolePrintRomStr("unknown", TRUE);
190 return;
191 }
192 }
193  
194 if ( iwprivCb.securityType == WF_SECURITY_WEP_40 || iwprivCb.securityType == WF_SECURITY_WEP_104 )
195 {
196 UINT8 webKeyLen;
197  
198 if ( iwprivCb.securityKeyLength == (IWPRIV_WEB_KEY_NUM * IWPRIV_WEB_LONG_KEY_LEN) )
199 {
200 webKeyLen = IWPRIV_WEB_LONG_KEY_LEN;
201 }
202 else if ( iwprivCb.securityKeyLength == (IWPRIV_WEB_KEY_NUM * IWPRIV_WEB_SHORT_KEY_LEN) )
203 {
204 webKeyLen = IWPRIV_WEB_SHORT_KEY_LEN;
205 }
206 else
207 {
208 WFConsolePrintRomStr(" Wep key: not yet set or unknown", TRUE);
209 return;
210 }
211  
212 p = iwprivCb.securityKey;
213 for( j=0; j < IWPRIV_WEB_KEY_NUM ; j++ )
214 {
215 if ( j == iwprivCb.wepDefaultKeyId )
216 WFConsolePrintRomStr(" *", FALSE);
217 else
218 WFConsolePrintRomStr(" ", FALSE);
219  
220 WFConsolePrintRomStr("Wep key[", FALSE);
221 WFConsolePrintInteger(j+1, FALSE);
222 WFConsolePrintRomStr("]: 0x", FALSE);
223  
224 for ( i=0; i < webKeyLen ; i++ )
225 {
226 sprintf( (char *) g_ConsoleContext.txBuf,
227 "%.2x", *p++);
228 WFConsolePrintRamStr( (char *) g_ConsoleContext.txBuf, FALSE);
229 }
230  
231 WFConsolePrintRomStr("", TRUE);
232 }
233 }
234 else if ( iwprivCb.securityType == WF_SECURITY_WPA_AUTO_WITH_KEY )
235 {
236 if ( iwprivCb.securityKeyLength != WF_WPA_KEY_LENGTH )
237 {
238 WFConsolePrintRomStr(" PSK: not yet set or unknown", TRUE);
239 return;
240 }
241  
242 putrsUART(" PSK: \"");
243  
244 p = iwprivCb.securityKey;
245 for( j=0; j < WF_WPA_KEY_LENGTH ; j++ )
246 {
247 sprintf( (char *) g_ConsoleContext.txBuf,
248 "%.2x", *p++);
249 WFConsolePrintRamStr( (char *) g_ConsoleContext.txBuf , FALSE );
250 }
251  
252 WFConsolePrintRomStr("", TRUE);
253 }
254 else if ( iwprivCb.securityType == WF_SECURITY_WPA_AUTO_WITH_PASS_PHRASE )
255 {
256 if ( iwprivCb.securityKeyLength == 0 )
257 {
258 WFConsolePrintRomStr(" Phrase: not yet set or unknown", TRUE);
259 return;
260 }
261  
262 WFConsolePrintRomStr(" Phrase: \"", FALSE);
263  
264 p = iwprivCb.securityKey;
265 for( j=0; j < iwprivCb.securityKeyLength ; j++ )
266 {
267 sprintf( (char *) g_ConsoleContext.txBuf,
268 "%c", *p++);
269 WFConsolePrintRamStr( (char *) g_ConsoleContext.txBuf, FALSE );
270 }
271  
272 WFConsolePrintRomStr("\"", TRUE);
273  
274 WFConsolePrintRomStr(" SSID: ", FALSE);
275 WFConsolePrintRamStr(iwprivCb.ssid, TRUE);
276 }
277 }
278  
279 static BOOL iwprivSetEnc(void)
280 {
281 UINT8 securityType;
282  
283 if (ARGC < 3u)
284 {
285 WFConsolePrintRomStr("Missing value for last parameter", TRUE);
286 return FALSE;
287 }
288  
289 if ( (3u <= ARGC) && (strcmppgm2ram((char*)ARGV[2], "none") == 0) )
290 {
291 securityType = WF_SECURITY_OPEN;
292 }
293 else if ( (3u <= ARGC) && (strcmppgm2ram((char*)ARGV[2], "wep") == 0) )
294 {
295 securityType = WF_SECURITY_WEP_40; // by default
296 }
297 else if ( (3u <= ARGC) && (strcmppgm2ram((char*)ARGV[2], "wpa-psk") == 0) )
298 {
299 securityType = WF_SECURITY_WPA_AUTO_WITH_KEY;
300  
301 }
302 else if ( (3u <= ARGC) && (strcmppgm2ram((char*)ARGV[2], "wpa-phrase") == 0) )
303 {
304 securityType = WF_SECURITY_WPA_AUTO_WITH_PASS_PHRASE;
305 }
306 else
307 {
308 WFConsolePrintRomStr("Unknown parameter", TRUE);
309 return FALSE;
310 }
311  
312 if ( iwprivCb.securityType != securityType ) // security type changed
313 { // reset the security context
314 memset(iwprivCb.securityKey, 0, sizeof(iwprivCb.securityKey));
315 iwprivCb.securityKeyLength = 0;
316 }
317  
318 iwprivCb.securityType = securityType; // save the security type
319  
320 if (iwprivCb.securityType == WF_SECURITY_OPEN)
321 {
322 WF_CPSetSecurity(iwprivCb.cpId, iwprivCb.securityType, 0, NULL, 0);
323 }
324  
325 return TRUE;
326 }
327  
328 static BOOL iwprivSetKey(void)
329 {
330 UINT8 webKey;
331  
332 if (iwprivCb.securityType != WF_SECURITY_WEP_40 && iwprivCb.securityType != WF_SECURITY_WEP_104)
333 {
334 WFConsolePrintRomStr("WEP encryption mode is not selected", TRUE);
335 return FALSE;
336 }
337  
338 if (ARGC < 3u)
339 {
340 WFConsolePrintRomStr("Missing value for last parameter", TRUE);
341 return FALSE;
342 }
343  
344 if ( (3u <= ARGC) && (strcmppgm2ram((char*)ARGV[2], "[1]") == 0) )
345 {
346 webKey = 0u;
347 }
348 else if ( (3u <= ARGC) && (strcmppgm2ram((char*)ARGV[2], "[2]") == 0) )
349 {
350 webKey = 1u;
351 }
352 else if ( (3u <= ARGC) && (strcmppgm2ram((char*)ARGV[2], "[3]") == 0) )
353 {
354 webKey = 2u;
355  
356 }
357 else if ( (3u <= ARGC) && (strcmppgm2ram((char*)ARGV[2], "[4]") == 0) )
358 {
359 webKey = 3u;
360 }
361 else
362 {
363 WFConsolePrintRomStr("Invalid WEP key index", TRUE);
364 return FALSE;
365 }
366  
367 if (4u <= ARGC)
368 {
369 if ( convertAsciiToHexInPlace(ARGV[3], IWPRIV_WEB_LONG_KEY_LEN) ) // for long web key
370 {
371 iwprivCb.securityType = WF_SECURITY_WEP_104;
372  
373 memcpy((void*)(iwprivCb.securityKey + webKey * IWPRIV_WEB_LONG_KEY_LEN), (const void*)ARGV[3], IWPRIV_WEB_LONG_KEY_LEN);
374 iwprivCb.securityKeyLength = IWPRIV_WEB_KEY_NUM * IWPRIV_WEB_LONG_KEY_LEN;
375  
376 WF_CPSetSecurity(iwprivCb.cpId, iwprivCb.securityType, webKey,
377 iwprivCb.securityKey + webKey * IWPRIV_WEB_LONG_KEY_LEN, IWPRIV_WEB_LONG_KEY_LEN);
378 }
379 else if ( convertAsciiToHexInPlace(ARGV[3], IWPRIV_WEB_SHORT_KEY_LEN) ) // for short web key
380 {
381 iwprivCb.securityType = WF_SECURITY_WEP_40;
382  
383 memcpy((void*)(iwprivCb.securityKey + webKey * IWPRIV_WEB_SHORT_KEY_LEN), (const void*)ARGV[3], IWPRIV_WEB_SHORT_KEY_LEN);
384 iwprivCb.securityKeyLength = IWPRIV_WEB_KEY_NUM * IWPRIV_WEB_SHORT_KEY_LEN;
385  
386 WF_CPSetSecurity(iwprivCb.cpId, iwprivCb.securityType, webKey,
387 iwprivCb.securityKey + webKey * IWPRIV_WEB_SHORT_KEY_LEN, IWPRIV_WEB_SHORT_KEY_LEN);
388 }
389 else
390 {
391 WFConsolePrintRomStr("64/128bit WEP key format not valid", TRUE);
392 return FALSE;
393 }
394 }
395 else // ARGC == 3u
396 {
397 WF_CPSetDefaultWepKeyIndex(iwprivCb.cpId, webKey);
398 }
399  
400 return TRUE;
401 }
402  
403 static BOOL iwprivSetPsk(void)
404 {
405 if ( iwprivCb.securityType != WF_SECURITY_WPA_AUTO_WITH_KEY )
406 {
407 WFConsolePrintRomStr("WPA-PSK encryption mode is not selected", TRUE);
408 return FALSE;
409 }
410  
411 if (ARGC < 3u)
412 {
413 WFConsolePrintRomStr("Missing value for last parameter", TRUE);
414 return FALSE;
415 }
416  
417 if ( convertAsciiToHexInPlace(ARGV[2], WF_WPA_KEY_LENGTH) )
418 {
419 memcpy((void*)iwprivCb.securityKey, (const void*)ARGV[2], WF_WPA_KEY_LENGTH);
420 iwprivCb.securityKeyLength = WF_WPA_KEY_LENGTH;
421 }
422 else
423 {
424 WFConsolePrintRomStr("WPA PSK must be exactly 32 bytes", TRUE);
425 return FALSE;
426 }
427  
428 WF_CPSetSecurity(iwprivCb.cpId, iwprivCb.securityType, 0, iwprivCb.securityKey, iwprivCb.securityKeyLength);
429  
430 return TRUE;
431 }
432  
433 static BOOL iwprivSetPhrase(void)
434 {
435 UINT8 j;
436 UINT8 securityType;
437 UINT8* phraseStart;
438 UINT8* phraseEnd;
439 UINT8 phraseLen;
440  
441 if ( iwprivCb.securityType == WF_SECURITY_WPA_AUTO_WITH_KEY || iwprivCb.securityType == WF_SECURITY_WPA_AUTO_WITH_PASS_PHRASE )
442 {
443 securityType = WF_SECURITY_WPA_AUTO_WITH_PASS_PHRASE;
444 }
445 else
446 {
447 WFConsolePrintRomStr("WPA-PSK or WPA-PHRASE encryption mode is not selected", TRUE);
448 return FALSE;
449 }
450  
451 if (ARGC < 3u)
452 {
453 WFConsolePrintRomStr("Missing value for last parameter", TRUE);
454 return FALSE;
455 }
456  
457 phraseStart = (UINT8*) ARGV[2];
458 if (*phraseStart == '\"') // cancatenate remaining args into one string
459 {
460 for (j = 2; j < (ARGC-1); j++)
461 {
462 UINT8 argLen = strlen((char*)ARGV[j]);
463 ARGV[j][argLen] = ' '; // replace '\0' with ' '
464 }
465  
466 // searching for an ending quote
467 phraseEnd = phraseStart + strlen((char *)phraseStart) - 1;
468 while (*phraseEnd != '\"')
469 phraseEnd--;
470  
471 // remove the double quotes
472 phraseStart++;
473 phraseEnd--;
474 }
475 else // a single word
476 {
477 phraseEnd = phraseStart + strlen((char *)phraseStart) - 1;
478 }
479  
480 phraseLen = phraseEnd - phraseStart + 1;
481 if (phraseLen < WF_MIN_WPA_PASS_PHRASE_LENGTH || WF_MAX_WPA_PASS_PHRASE_LENGTH < phraseLen)
482 {
483 WFConsolePrintRomStr("Phrase string must be at least 8 chars and no greater than 64", TRUE);
484 return FALSE;
485 }
486  
487 iwprivCb.securityType = securityType;
488  
489 memcpy((void*)iwprivCb.securityKey, (const void*)phraseStart, phraseLen);
490 iwprivCb.securityKey[phraseLen] = '\0'; // just for easy printing on the console
491 iwprivCb.securityKeyLength = phraseLen;
492  
493 WF_CPSetSecurity(iwprivCb.cpId, iwprivCb.securityType, 0,
494 iwprivCb.securityKey, iwprivCb.securityKeyLength);
495  
496 return TRUE;
497 }
498  
499 /*****************************************************************************
500 * FUNCTION: do_iwpriv_cmd
501 *
502 * RETURNS: None
503 *
504 * PARAMS: None
505 *
506 * NOTES: Responds to the user invoking ifconfig
507 *****************************************************************************/
508 void do_iwpriv_cmd(void)
509 {
510 if ( !iwprivSetCb() )
511 return;
512  
513 // if user only typed in iwpriv with no other parameters
514 if (ARGC == 1u)
515 {
516 iwprivDisplayStatus();
517 return;
518 }
519  
520 if ( !iwconfigCb.isIdle )
521 {
522 WFConsolePrintRomStr("Security context modification can be only done in the idle state", TRUE);
523 return;
524 }
525  
526 if ( (2u <= ARGC) && (strcmppgm2ram((char*)ARGV[1], "enc") == 0) )
527 {
528 if ( !iwprivSetEnc() )
529 return;
530 }
531 else if ( (2u <= ARGC) && (strcmppgm2ram((char*)ARGV[1], "key") == 0) )
532 {
533 if ( !iwprivSetKey() )
534 return;
535 }
536 else if ( (2u <= ARGC) && (strcmppgm2ram((char*)ARGV[1], "psk") == 0) )
537 {
538 if ( !iwprivSetPsk() )
539 return;
540 }
541 else if ( (2u <= ARGC) && (strcmppgm2ram((char*)ARGV[1], "phrase") == 0) )
542 {
543 if ( !iwprivSetPhrase() )
544 return;
545 }
546 else
547 {
548 WFConsolePrintRomStr("Unknown parameter", TRUE);
549 return;
550 }
551 }
552  
553 #endif /* WF_CONSOLE_IFCFGUTIL */
554  
555  
{BLAME END}
{FOOTER START}

Powered by WebSVN v2.8.3