Line No. | Rev | Author | Line |
---|---|---|---|
1 | 32 | kaklik | /****************************************************************************** |
2 | |||
3 | MRF24WB0M Driver ifconfig |
||
4 | Module for Microchip TCP/IP Stack |
||
5 | -Provides access to MRF24WB0M WiFi controller |
||
6 | -Reference: MRF24WB0M Data sheet, IEEE 802.11 Standard |
||
7 | |||
8 | ******************************************************************************* |
||
9 | FileName: WFConsoleIfconfig.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 | // Includes |
||
52 | //============================================================================ |
||
53 | #include <ctype.h> |
||
54 | #include <stdio.h> |
||
55 | #include <string.h> |
||
56 | |||
57 | #include "TCPIP Stack/TCPIP.h" |
||
58 | #include "TCPIP Stack/WFConsole.h" |
||
59 | |||
60 | #if defined( WF_CONSOLE_IFCFGUTIL ) |
||
61 | |||
62 | #include "TCPIP Stack/WFConsoleIfconfig.h" |
||
63 | #include "TCPIP Stack/WFConsoleMsgs.h" |
||
64 | #include "TCPIP Stack/WFConsoleMsgHandler.h" |
||
65 | |||
66 | //============================================================================ |
||
67 | // Constants |
||
68 | //============================================================================ |
||
69 | |||
70 | //============================================================================ |
||
71 | // Globals |
||
72 | //============================================================================ |
||
73 | |||
74 | //============================================================================ |
||
75 | // Local Function Prototypes |
||
76 | //============================================================================ |
||
77 | static void IfconfigDisplayStatus(void); |
||
78 | static BOOL isIPAddress(INT8 *p_string, UINT8 *p_Address); |
||
79 | static BOOL isMacAddress(INT8 *p_string, UINT8 *p_Address); |
||
80 | #if defined(STACK_USE_DHCP_CLIENT) |
||
81 | static void setDHCPState(BOOL enable); |
||
82 | #endif |
||
83 | static void missingValue(void); |
||
84 | static void notHandledParam(UINT8 index); |
||
85 | |||
86 | void LCDDisplayIPValue(IP_ADDR IPVal) |
||
87 | { |
||
88 | #ifdef USE_LCD |
||
89 | |||
90 | BYTE IPDigit[4]; |
||
91 | BYTE i; |
||
92 | BYTE j; |
||
93 | BYTE LCDPos=16; |
||
94 | |||
95 | for(i = 0; i < sizeof(IP_ADDR); i++) |
||
96 | { |
||
97 | uitoa((WORD)IPVal.v[i], IPDigit); |
||
98 | |||
99 | for(j = 0; j < strlen((char*)IPDigit); j++) |
||
100 | { |
||
101 | LCDText[LCDPos++] = IPDigit[j]; |
||
102 | } |
||
103 | if(i == sizeof(IP_ADDR)-1) |
||
104 | break; |
||
105 | LCDText[LCDPos++] = '.'; |
||
106 | } |
||
107 | |||
108 | |||
109 | if(LCDPos < 32u) |
||
110 | LCDText[LCDPos] = 0; |
||
111 | LCDUpdate(); |
||
112 | |||
113 | #endif |
||
114 | } |
||
115 | |||
116 | /***************************************************************************** |
||
117 | * FUNCTION: do_ifconfig_cmd |
||
118 | * |
||
119 | * RETURNS: None |
||
120 | * |
||
121 | * PARAMS: None |
||
122 | * |
||
123 | * NOTES: Responds to the user invoking ifconfig |
||
124 | *****************************************************************************/ |
||
125 | void do_ifconfig_cmd(void) |
||
126 | { |
||
127 | UINT8 address[6]; |
||
128 | UINT8 conState, cpId; |
||
129 | |||
130 | // if user only typed in ifconfig with no other parameters |
||
131 | if (ARGC == 1u) |
||
132 | { |
||
133 | IfconfigDisplayStatus(); |
||
134 | } |
||
135 | #if defined(WF_CM_DEBUG) |
||
136 | else if ( (ARGC == 2u) && !strcmp((char *) ARGV[1], "info") ) |
||
137 | { |
||
138 | UINT8 i; |
||
139 | tWFCMInfoFSMStats cm_stats; |
||
140 | |||
141 | WF_CMInfoGetFSMStats(&cm_stats); |
||
142 | for (i = 0; i < 12; i++) |
||
143 | { |
||
144 | sprintf( (char *) g_ConsoleContext.txBuf, |
||
145 | "[%02X]: %02X%02X %02X%02X", |
||
146 | i, |
||
147 | cm_stats.byte[i*4 + 0], |
||
148 | cm_stats.byte[i*4 + 1], |
||
149 | cm_stats.byte[i*4 + 2], |
||
150 | cm_stats.byte[i*4 + 3] |
||
151 | ); |
||
152 | WFConsolePrintRamStr( (char *) g_ConsoleContext.txBuf , TRUE); |
||
153 | } |
||
154 | } |
||
155 | else if ( (ARGC == 2u) && !strcmp((char *) ARGV[1], "scan") ) |
||
156 | { |
||
157 | WF_Scan(1); // scan, using CP 1 |
||
158 | } |
||
159 | else if ( (ARGC == 2u) && !strcmp((char *) ARGV[1], "scanget") ) //"scangetresult" |
||
160 | { |
||
161 | tWFScanResult pScanResult[1]; |
||
162 | |||
163 | WF_ScanGetResult(0, pScanResult); |
||
164 | } |
||
165 | else if ( (ARGC == 2u) && !strcmp((char *) ARGV[1], "cpgete") ) //"cpgetelements" |
||
166 | { |
||
167 | tWFCPElements pCPElements[1]; |
||
168 | |||
169 | WF_CPGetElements(1, pCPElements); |
||
170 | } |
||
171 | #endif |
||
172 | // else if 2 arguments and the second arg is IP address |
||
173 | else if ( (ARGC == 2u) && (isIPAddress(ARGV[1], address)) ) |
||
174 | { |
||
175 | #if defined(STACK_USE_DHCP_CLIENT) |
||
176 | if (DHCPIsEnabled(0)) |
||
177 | { |
||
178 | WFConsolePrintRomStr( |
||
179 | "Static IP address should not be set with DHCP enabled", TRUE); |
||
180 | return; |
||
181 | } |
||
182 | #endif |
||
183 | |||
184 | AppConfig.MyIPAddr.v[0] = address[0]; |
||
185 | AppConfig.MyIPAddr.v[1] = address[1]; |
||
186 | AppConfig.MyIPAddr.v[2] = address[2]; |
||
187 | AppConfig.MyIPAddr.v[3] = address[3]; |
||
188 | |||
189 | /* Microchip DHCP client clobbers static ip on every iteration of loop, even if dhcp is turned off*/ |
||
190 | AppConfig.DefaultIPAddr.v[0] = address[0]; |
||
191 | AppConfig.DefaultIPAddr.v[1] = address[1]; |
||
192 | AppConfig.DefaultIPAddr.v[2] = address[2]; |
||
193 | AppConfig.DefaultIPAddr.v[3] = address[3]; |
||
194 | |||
195 | LCDDisplayIPValue(AppConfig.MyIPAddr); |
||
196 | } |
||
197 | // else if 2 args and second arg is MAC address |
||
198 | else if ( (ARGC == 2u) && isMacAddress(ARGV[1], address)) |
||
199 | { |
||
200 | /* Can only set MAC address in idle state */ |
||
201 | WF_CMGetConnectionState(&conState, &cpId); |
||
202 | if ( conState != WF_CSTATE_NOT_CONNECTED ) |
||
203 | { |
||
204 | WFConsolePrintRomStr("HW MAC address can only be set in idle mode", TRUE); |
||
205 | return; |
||
206 | } |
||
207 | |||
208 | WF_SetMacAddress( address ); |
||
209 | } |
||
210 | else if ( (2u <= ARGC) && (strcmppgm2ram((char *)ARGV[1], (ROM FAR char *)"netmask") == 0) ) |
||
211 | { |
||
212 | if (ARGC != 3u) |
||
213 | { |
||
214 | missingValue(); |
||
215 | return; |
||
216 | } |
||
217 | |||
218 | #if defined(STACK_USE_DHCP_CLIENT) |
||
219 | if ( DHCPIsEnabled(0) ) |
||
220 | { |
||
221 | WFConsolePrintRomStr( |
||
222 | "The Netmask should not be set with DHCP enabled", TRUE); |
||
223 | return; |
||
224 | } |
||
225 | #endif |
||
226 | |||
227 | if ( !isIPAddress(ARGV[2], address) ) |
||
228 | { |
||
229 | WFConsolePrintRomStr("Invalid netmask value", TRUE); |
||
230 | return; |
||
231 | } |
||
232 | |||
233 | AppConfig.MyMask.v[0] = address[0]; |
||
234 | AppConfig.MyMask.v[1] = address[1]; |
||
235 | AppConfig.MyMask.v[2] = address[2]; |
||
236 | AppConfig.MyMask.v[3] = address[3]; |
||
237 | |||
238 | /* Microchip DHCP client clobbers static netmask on every iteration of loop, even if dhcp is turned off*/ |
||
239 | AppConfig.DefaultMask.v[0] = address[0]; |
||
240 | AppConfig.DefaultMask.v[1] = address[1]; |
||
241 | AppConfig.DefaultMask.v[2] = address[2]; |
||
242 | AppConfig.DefaultMask.v[3] = address[3]; |
||
243 | } |
||
244 | else if ( (2u <= ARGC) && (strcmppgm2ram((char *)ARGV[1], (ROM FAR char *)"gateway") == 0) ) |
||
245 | { |
||
246 | if (ARGC != 3u) |
||
247 | { |
||
248 | missingValue(); |
||
249 | return; |
||
250 | } |
||
251 | |||
252 | if ( !isIPAddress(ARGV[2], address) ) |
||
253 | { |
||
254 | WFConsolePrintRomStr("Invalid gateway value", TRUE); |
||
255 | return; |
||
256 | } |
||
257 | |||
258 | AppConfig.MyGateway.v[0] = address[0]; |
||
259 | AppConfig.MyGateway.v[1] = address[1]; |
||
260 | AppConfig.MyGateway.v[2] = address[2]; |
||
261 | AppConfig.MyGateway.v[3] = address[3]; |
||
262 | } |
||
263 | else if ( (2u <= ARGC) && (strcmppgm2ram((char*)ARGV[1], "auto-dhcp") == 0) ) |
||
264 | { |
||
265 | if (ARGC != 3u) |
||
266 | { |
||
267 | missingValue(); |
||
268 | return; |
||
269 | } |
||
270 | |||
271 | #if defined(STACK_USE_DHCP_CLIENT) |
||
272 | if (strcmppgm2ram((char*)ARGV[2], "start") == 0) |
||
273 | { |
||
274 | setDHCPState(TRUE); |
||
275 | } |
||
276 | else if (strcmppgm2ram((char*)ARGV[2], "stop") == 0) |
||
277 | { |
||
278 | setDHCPState(FALSE); |
||
279 | } |
||
280 | else |
||
281 | #endif |
||
282 | { |
||
283 | WFConsolePrintRomStr(" Invalid dhcp param", TRUE); |
||
284 | return; |
||
285 | } |
||
286 | } |
||
287 | else |
||
288 | { |
||
289 | notHandledParam(1); |
||
290 | } |
||
291 | } |
||
292 | |||
293 | static void missingValue(void) |
||
294 | { |
||
295 | WFConsolePrintRomStr( |
||
296 | "Missing value after last parameter", TRUE); |
||
297 | } |
||
298 | |||
299 | static void notHandledParam(UINT8 index) |
||
300 | { |
||
301 | WFConsolePrintRomStr("Param ", FALSE); |
||
302 | WFConsolePrintInteger(index, 'd'); |
||
303 | WFConsolePrintRomStr(" not handled", TRUE); |
||
304 | } |
||
305 | |||
306 | /***************************************************************************** |
||
307 | * FUNCTION: isIPAddress |
||
308 | * |
||
309 | * RETURNS: True if valid IP address, else False |
||
310 | * |
||
311 | * PARAMS: p_string -- string to check |
||
312 | * p_Address -- Array where IP values will be written |
||
313 | * |
||
314 | * NOTES: Determines if the input string is a valid dot-notation IP address. |
||
315 | * If it is, then returns an array of 4 bytes for each of the values. |
||
316 | * IP address |
||
317 | *****************************************************************************/ |
||
318 | |||
319 | static BOOL isIPAddress(INT8 *p_string, UINT8 *p_Address) |
||
320 | { |
||
321 | UINT8 digIndex = 0; |
||
322 | UINT8 bufIndex = 0; |
||
323 | UINT8 dotCount = 0; |
||
324 | INT8 buf[4]; |
||
325 | UINT8 i; |
||
326 | UINT16 tmp; |
||
327 | |||
328 | memset(buf, 0x00, sizeof(buf)); |
||
329 | for (i = 0; i < strlen((char *)p_string); ++i) |
||
330 | { |
||
331 | // if gathering digits |
||
332 | if (isdigit(p_string[i])) |
||
333 | { |
||
334 | // store digit in buf, fail if user has more than 3 digits |
||
335 | buf[bufIndex++] = p_string[i]; |
||
336 | if (bufIndex > 3u) |
||
337 | { |
||
338 | return FALSE; |
||
339 | } |
||
340 | } |
||
341 | // else encountered a dot |
||
342 | else if (p_string[i] == (INT8)'.') |
||
343 | { |
||
344 | // keep track of dots and fail if we encounter too many of them |
||
345 | ++dotCount; |
||
346 | if (dotCount > 3u) |
||
347 | { |
||
348 | return FALSE; |
||
349 | } |
||
350 | |||
351 | // convert the number we just pulled from the input string, fail if not a number |
||
352 | if (!ConvertASCIIUnsignedDecimalToBinary(buf, &tmp)) |
||
353 | { |
||
354 | return FALSE; |
||
355 | } |
||
356 | // else a valid number |
||
357 | else |
||
358 | { |
||
359 | // fail if greater than 255 |
||
360 | if ( tmp > 255u) |
||
361 | { |
||
362 | return FALSE; |
||
363 | } |
||
364 | |||
365 | p_Address[digIndex] = (UINT8) (tmp & 0xFF); |
||
366 | |||
367 | // get ready for next number |
||
368 | memset(buf, 0x00, sizeof(buf)); |
||
369 | bufIndex = 0; |
||
370 | ++digIndex; |
||
371 | } |
||
372 | } |
||
373 | // else got a character that is neither number nor dot |
||
374 | else |
||
375 | { |
||
376 | return FALSE; |
||
377 | } |
||
378 | |||
379 | } |
||
380 | |||
381 | // fail if more than 3 dots |
||
382 | if (dotCount != 3u) |
||
383 | { |
||
384 | return FALSE; |
||
385 | } |
||
386 | |||
387 | // if made it here then make sure we have the last number |
||
388 | if (buf[0] == 0) |
||
389 | { |
||
390 | return FALSE; |
||
391 | } |
||
392 | |||
393 | // convert last number to binary, fail if we can't |
||
394 | if (!ConvertASCIIUnsignedDecimalToBinary(buf, &tmp)) |
||
395 | { |
||
396 | return FALSE; |
||
397 | } |
||
398 | |||
399 | p_Address[digIndex] = (UINT8) (tmp & 0xFF); |
||
400 | |||
401 | // IP digits will be in p_Address[] |
||
402 | return TRUE; |
||
403 | } |
||
404 | |||
405 | |||
406 | /***************************************************************************** |
||
407 | * FUNCTION: isMacAddress |
||
408 | * |
||
409 | * RETURNS: True if valid MAC address, else False |
||
410 | * |
||
411 | * PARAMS: p_string -- string to check |
||
412 | * p_Address -- Array where MAC values will be written |
||
413 | * |
||
414 | * NOTES: Determines if the input string is a valid MAC address. |
||
415 | * If it is, then returns an array of 6 bytes for each of the values. |
||
416 | * MAC address must be in hex in the format xx:xx:xx:xx:xx:xx |
||
417 | *****************************************************************************/ |
||
418 | static BOOL isMacAddress(INT8 *p_string, UINT8 *p_Address) |
||
419 | { |
||
420 | UINT8 i; |
||
421 | UINT16 tmp; |
||
422 | |||
423 | if (strlen((char *)p_string) != 17u) |
||
424 | { |
||
425 | return FALSE; |
||
426 | } |
||
427 | |||
428 | // ensure the ':' is in the right place, and if so, set them to 0 |
||
429 | for (i = 2; i < 17u; i += 3) |
||
430 | { |
||
431 | if (p_string[i] == (INT8)':') |
||
432 | { |
||
433 | p_string[i] = '\0'; |
||
434 | } |
||
435 | else |
||
436 | { |
||
437 | return FALSE; |
||
438 | } |
||
439 | } |
||
440 | |||
441 | // now extract each hex number string |
||
442 | for (i = 0; i < 6u; ++i) |
||
443 | { |
||
444 | if (!ConvertASCIIHexToBinary(&p_string[i * 3], &tmp)) |
||
445 | { |
||
446 | return FALSE; |
||
447 | } |
||
448 | |||
449 | p_Address[i] = (UINT8) (tmp & 0xFF); |
||
450 | |||
451 | } |
||
452 | |||
453 | return TRUE; |
||
454 | } |
||
455 | |||
456 | /***************************************************************************** |
||
457 | * FUNCTION: IfconfigDisplayStatus |
||
458 | * |
||
459 | * RETURNS: None |
||
460 | * |
||
461 | * PARAMS: None |
||
462 | * |
||
463 | * NOTES: Responds to the user invoking ifconfig with no parameters |
||
464 | *****************************************************************************/ |
||
465 | static void IfconfigDisplayStatus(void) |
||
466 | { |
||
467 | UINT8 p_mac[6]; |
||
468 | |||
469 | sprintf( (char *) g_ConsoleContext.txBuf, |
||
470 | "\tIP addr: %d.%d.%d.%d", AppConfig.MyIPAddr.v[0], |
||
471 | AppConfig.MyIPAddr.v[1], |
||
472 | AppConfig.MyIPAddr.v[2], |
||
473 | AppConfig.MyIPAddr.v[3] ); |
||
474 | WFConsolePrintRamStr( (char *) g_ConsoleContext.txBuf , TRUE); |
||
475 | |||
476 | |||
477 | WF_GetMacAddress(p_mac); |
||
478 | sprintf( (char *) g_ConsoleContext.txBuf, |
||
479 | "\tMAC addr: %02X:%02X:%02X:%02X:%02X:%02X", p_mac[0], p_mac[1], |
||
480 | p_mac[2], p_mac[3], |
||
481 | p_mac[4], p_mac[5]); |
||
482 | WFConsolePrintRamStr( (char *) g_ConsoleContext.txBuf , TRUE); |
||
483 | |||
484 | |||
485 | |||
486 | sprintf( (char *) g_ConsoleContext.txBuf, |
||
487 | "\tNetmask: %d.%d.%d.%d", AppConfig.MyMask.v[0], |
||
488 | AppConfig.MyMask.v[1], |
||
489 | AppConfig.MyMask.v[2], |
||
490 | AppConfig.MyMask.v[3] ); |
||
491 | WFConsolePrintRamStr( (char *) g_ConsoleContext.txBuf , TRUE); |
||
492 | |||
493 | |||
494 | sprintf( (char *) g_ConsoleContext.txBuf, |
||
495 | "\tGateway: %d.%d.%d.%d", AppConfig.MyGateway.v[0], |
||
496 | AppConfig.MyGateway.v[1], |
||
497 | AppConfig.MyGateway.v[2], |
||
498 | AppConfig.MyGateway.v[3] ); |
||
499 | WFConsolePrintRamStr( (char *) g_ConsoleContext.txBuf , TRUE); |
||
500 | |||
501 | #if defined(STACK_USE_DHCP_CLIENT) |
||
502 | if ( DHCPIsEnabled(0) ) |
||
503 | WFConsolePrintRomStr("\tDHCP: Started", TRUE); |
||
504 | else |
||
505 | WFConsolePrintRomStr("\tDHCP: Stopped", TRUE); |
||
506 | #endif |
||
507 | } |
||
508 | |||
509 | #if defined(STACK_USE_DHCP_CLIENT) |
||
510 | /***************************************************************************** |
||
511 | * FUNCTION: setDHCPState |
||
512 | * |
||
513 | * RETURNS: None |
||
514 | * |
||
515 | * PARAMS: enable -- a boolean indicating whether to enable DHCP or not |
||
516 | * |
||
517 | * NOTES: Enable or disable DHCP operation |
||
518 | *****************************************************************************/ |
||
519 | static void setDHCPState(BOOL enable) |
||
520 | { |
||
521 | if ( enable ) |
||
522 | { |
||
523 | AppConfig.Flags.bIsDHCPEnabled = TRUE; |
||
524 | DHCPEnable(0); |
||
525 | } |
||
526 | else |
||
527 | { |
||
528 | AppConfig.Flags.bIsDHCPEnabled = FALSE; |
||
529 | DHCPDisable(0); |
||
530 | } |
||
531 | } |
||
532 | #endif |
||
533 | |||
534 | #endif /* WF_CONSOLE_IFCFGUTIL */ |
||
535 | |||
536 |
Powered by WebSVN v2.8.3