?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 MRF24WB0M Driver WiFi Console
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: WFConsole.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 <string.h>
54 #include <stdio.h>
55 #include <stdarg.h>
56 #include <ctype.h>
57  
58 #include "TCPIP Stack/TCPIP.h"
59 #include "TCPIP Stack/WFConsole.h"
60  
61 #if defined ( WF_CONSOLE )
62  
63 //============================================================================
64 // Constants
65 //============================================================================
66  
67 // This define determines the number of command lines that are stored in the history
68 // buffer. Setting this define to 0 will remove the command line history feature by
69 // eliminating history code and buffers, thereby saving memory.
70 #define kWFNumHistoryEntries (1)
71  
72 // states for WFConsoleProcess() state machine
73 enum
74 {
75 kSTWaitForChar,
76 kSTWaitForEscSeqSecondChar,
77 kSTWaitForEscSeqThirdChar
78 };
79  
80 // ASCII control keys
81  
82  
83 #define kWFTab ('\t')
84 #define kWFNewLine ('\n')
85 #define kWFBackspace ('\b')
86 #define kWFCR ('\r')
87 #define kWFSpace (' ')
88 #define kWFEscape (0x1b)
89 #define kWFDelete (0x7f) /* not supported on hyperterminal unless in VT100 mode */
90 #define kWFEnter (0x0d)
91 #define kWFCtrl_B (0x02)
92 #define kWFCtrl_C (0x03)
93 #define kWFCtrl_D (0x04)
94 #define kWFCtrl_E (0x05) /* used to turns off console echo */
95 #define kWFCtrl_H (0x08)
96 #define kWFCtrl_I (0x09)
97 #define kWFCtrl_O (0x0f)
98 #define kWFCtrl_X (0x18)
99  
100 // total number of commands to and from Host Bridge
101 #define kWFNumCmdsToHostBridge (sizeof(MsgsToHB) / sizeof(MSGS))
102 #define kWFNumCmdsFromHostBridge (sizeof(MsgsFromHB) / sizeof(MSGS))
103  
104 #define kWFNextHistory (0)
105 #define kWFPrevHistory (1)
106  
107 #define kWFMaxInputEscapeSequence (5)
108  
109 //============================================================================
110 // Macros
111 //============================================================================
112 #define SET_RX_STATE(state) g_ConsoleContext.rxState = state
113 #define GET_RX_STATE() g_ConsoleContext.rxState
114 #define SET_CURSOR(index) g_ConsoleContext.cursorIndex = index
115 #define GET_CURSOR() g_ConsoleContext.cursorIndex
116  
117 #define GET_LEN_RX_CMD_STRING() ( strlen( (char *) g_ConsoleContext.rxBuf))
118  
119 //============================================================================
120 // TypeDefs
121 //============================================================================
122  
123 #if (kWFNumHistoryEntries > 0)
124 typedef struct history_struct
125 {
126 INT8 buf[kWFNumHistoryEntries][kConsoleMaxMsgSize + 1]; // N lines of history
127 UINT8 index;
128 BOOL seeded;
129 INT8 recallIndex;
130 } tWFHistory;
131 #endif
132  
133 //============================================================================
134 // Local Globals
135 //============================================================================
136 #if (kWFNumHistoryEntries > 0)
137 static tWFHistory history;
138 #endif
139  
140  
141 static ROM INT8 gCmdLinePrompt[] = "> ";
142 static UINT8 gCmdLinePromptLength = 2;
143 static INT8 gTmpCmdLine[kConsoleMaxMsgSize];
144  
145  
146 //============================================================================
147 // Constant Local Globals
148 //============================================================================
149  
150 // VT100 escapse sequences that are output to the terminal
151 static ROM INT8 cursorLeftEscapeSequence[] = {kWFEscape, '[', '1', 'D', 0x00};
152 static ROM INT8 cursorRightEscapeSequence[] = {kWFEscape, '[', '1', 'C', 0x00};
153 static ROM INT8 cursorHomeEscapeSequence[] = {kWFEscape, '[', 'f', 0x00};
154 static ROM INT8 eraseToEndOfLineEscapeSequence[] = {kWFEscape, '[', 'K', 0x00};
155 static ROM INT8 saveCursorPositionEscapeSequence[] = {kWFEscape, '7', 0x00};
156 static ROM INT8 restoreCursorPositionSequence[] = {kWFEscape, '8', 0x00};
157 static ROM INT8 eraseEntireLineEscapeSequence[] = {kWFEscape, '[', '2', 'K', 0x00};
158 static ROM INT8 eraseEntireScreenEscapeSequence[] = {kWFEscape, '[', '2', 'J', 0x00};
159 static ROM INT8 underlineModeEscapeSequence[] = {kWFEscape, '[', '4', 'm', 0x00};
160 static ROM INT8 normalModeEscapeSequence[] = {kWFEscape, '[', 'm', 0x00};
161 static ROM INT8 highlightModeEscapeSequence[] = {kWFEscape, '[', '1', 'm', 0x00};
162 static ROM INT8 inverseVideoEscapeSequence[] = {kWFEscape, '[', '7', 'm', 0x00};
163  
164 // VT100 escape sequences that are input from the terminal
165 // (note, if we ever use a longer sequence, update kWFMaxInputEscapeSequence)
166 static ROM INT8 upArrowEscapeSequence[] = {kWFEscape, 0x5b, 'A', 0x00};
167 static ROM INT8 downArrowEscapeSequence[] = {kWFEscape, 0x5b, 'B', 0x00};
168 static ROM INT8 rightArrowEscapeSequence[] = {kWFEscape, 0x5b, 'C', 0x00};
169 static ROM INT8 leftArrowEscapeSequence[] = {kWFEscape, 0x5b, 'D', 0x00};
170 static ROM INT8 homeKeyEscapeSequence[] = {kWFEscape, 0x5b, 'H', 0x00};
171 static ROM INT8 endKeyEscapeSequence[] = {kWFEscape, 0x5b, 'K', 0x00};
172  
173  
174  
175 //============================================================================
176 // Globals
177 //============================================================================
178 #ifdef __18CXX
179 #pragma udata wfcli_section
180 #endif
181  
182 tConsoleContext g_ConsoleContext;
183  
184 #ifdef __18CXX
185 #pragma udata
186 #endif
187  
188 //============================================================================
189 // Local Function Prototypes
190 //============================================================================
191 #if 0 /* add back if needed */
192 static BOOL isCtrlCharacter(INT8 c);
193 static void NormalMode(void);
194 static void UnderlineMode(void);
195 #endif
196  
197 static BOOL isPrintableCharacter(INT8 c);
198 static BOOL isCmdLineOnlyWhitespace(void);
199 static void InsertCharacter(INT8 c);
200 static void Delete(void);
201 static void Backspace(void);
202 static void EchoCharacter(INT8 new_char);
203  
204 #if 0 /* add back if you need this feature */
205 static void EraseEntireScreen(void);
206 #endif
207  
208 static void EraseEntireLine(void);
209 static void CursorRight(void);
210 static void CursorRight_N(UINT8 n);
211 static void CursorLeft(void);
212 static void CursorLeft_N(UINT8 n);
213 static void CursorHome(void);
214 static void CursorEnd(void);
215  
216  
217 static void OutputLine(INT8 lineChar, UINT8 count);
218 static void Enter(void);
219 static void ProcessEscapeSequence(INT8 *p_escape_sequence);
220 static void OutputCommandPrompt(void);
221  
222 #if (kWFNumHistoryEntries > 0)
223 static void InitHistory(void);
224 static void AddCmdToHistory(void);
225 static void DisplayHistoryEntry(UINT8 action);
226 #endif
227  
228  
229  
230  
231 /*****************************************************************************
232 * FUNCTION: WFConsoleInit
233 *
234 * RETURNS: None
235 *
236 * PARAMS:
237 *
238 * NOTES: Initialization for console thread
239 *
240 *****************************************************************************/
241 void WFConsoleInit(void)
242 {
243  
244 SET_RX_STATE(kSTWaitForChar);
245 SET_CURSOR(0);
246  
247 // zero out command line buffer
248 memset(g_ConsoleContext.rxBuf, 0x00, sizeof(g_ConsoleContext.rxBuf));
249  
250 g_ConsoleContext.bStateMachineLoop = TRUE;
251 g_ConsoleContext.firstChar = FALSE;
252 g_ConsoleContext.appConsoleMsgRx = FALSE;
253 SET_ECHO_ON();
254 #if (kWFNumHistoryEntries > 0)
255 InitHistory();
256 #endif
257  
258 g_ConsoleContext.echoOn = TRUE;
259  
260 }
261  
262  
263 /*****************************************************************************
264 * FUNCTION: WFConsoleProcess
265 *
266 * RETURNS: None
267 *
268 * PARAMS: None
269 *
270 * NOTES: State machine called from main loop of app. Handles serial input.
271 *
272 *****************************************************************************/
273 void WFConsoleProcess(void)
274 {
275 //UINT8 *pStart = &(cmdline[0]);
276 UINT16 rc;
277 INT8 c;
278 static INT8 escape_sequence[kWFMaxInputEscapeSequence];
279 static INT8 esc_seq_index;
280  
281 // if this state machine has been disabled
282 if (g_ConsoleContext.bStateMachineLoop == FALSE)
283 {
284 return;
285 }
286  
287  
288 // if a command was entered that is application=specific
289 if (g_ConsoleContext.appConsoleMsgRx == TRUE)
290 {
291 return; // wait until app done before processing further characters
292 }
293  
294 // if no character(s) received
295 if ( (rc = DataRdyUART() ) == 0u )
296 {
297 return;
298 }
299  
300 // get the character
301 c = (INT8) ReadUART();
302  
303 // if this is the very first character received by this state machine
304 if (g_ConsoleContext.firstChar == FALSE)
305 {
306 Output_Monitor_Hdr();
307 g_ConsoleContext.firstChar = TRUE;
308 }
309  
310 switch( GET_RX_STATE() )
311 {
312 //------------------------------------------
313 case kSTWaitForChar:
314 //------------------------------------------
315 // if a 'normal' printable character
316 if (isPrintableCharacter(c))
317 {
318 InsertCharacter(c);
319 }
320 // else if Delete key
321 else if (c == kWFDelete)
322 {
323 Delete();
324 }
325 // else if Backspace key
326 else if (c == (INT8)kWFBackspace)
327 {
328 Backspace();
329 }
330 // else if Enter key
331 else if (c == kWFEnter)
332 {
333 Enter();
334 }
335 // else if Escape key
336 else if (c == kWFEscape)
337 {
338 /* zero out escape buffer, init with ESC */
339 memset(escape_sequence, 0x00, sizeof(escape_sequence));
340 escape_sequence[0] = kWFEscape;
341 esc_seq_index = 1;
342 SET_RX_STATE(kSTWaitForEscSeqSecondChar);
343 }
344 // else if Ctrl C
345 else if (c == kWFCtrl_C)
346 {
347 OutputCommandPrompt();
348 }
349 else {
350 // Enter();
351 }
352 break;
353  
354 //------------------------------------------
355 case kSTWaitForEscSeqSecondChar:
356 //------------------------------------------
357 /* if an arrow key, home, or end key (which is all that this state machine handles) */
358 if (c == 0x5b)
359 {
360 escape_sequence[1] = c;
361 SET_RX_STATE(kSTWaitForEscSeqThirdChar);
362 }
363 // else if user pressed escape followed by any printable character
364 else if (isPrintableCharacter(c))
365 {
366 InsertCharacter(c);
367 SET_RX_STATE(kSTWaitForChar);
368 }
369 // start this command line over
370 else // anything else
371 {
372 OutputCommandPrompt();
373 SET_RX_STATE(kSTWaitForChar);
374 }
375 break;
376  
377 //------------------------------------------
378 case kSTWaitForEscSeqThirdChar:
379 //------------------------------------------
380 escape_sequence[2] = c;
381 ProcessEscapeSequence(escape_sequence);
382 SET_RX_STATE(kSTWaitForChar);
383 break;
384  
385  
386 } // end switch
387 }
388  
389 /*****************************************************************************
390 * FUNCTION: WFConsoleProcessEpilogue
391 *
392 * RETURNS: None
393 *
394 * PARAMS: None
395 *
396 * NOTES: Check if there is a left console msg, and release it if found.
397 *
398 *****************************************************************************/
399 void WFConsoleProcessEpilogue(void)
400 {
401 if (WFConsoleIsConsoleMsgReceived())
402 {
403 if ( memcmppgm2ram(ARGV[0], "help", 4) != 0 )
404 {
405 WFConsolePrintRomStr("Unknown cmd: ", FALSE);
406 WFConsolePrintRamStr(ARGV[0], TRUE);
407 }
408  
409 WFConsoleReleaseConsoleMsg();
410 }
411 }
412  
413 INT8 ** WFConsoleGetCmdLineArgv(void)
414 {
415 return g_ConsoleContext.argv;
416 }
417  
418 UINT8 WFConsoleGetCmdLineArgc(void)
419 {
420 return g_ConsoleContext.argc;
421 }
422  
423 /*****************************************************************************
424 * FUNCTION: ProcessEscapeSequence
425 *
426 * RETURNS: None
427 *
428 * PARAMS: pEscapeSequence -- escape sequence string to be processed.
429 *
430 * NOTES: Processes an escape sequence received by the state machine
431 *
432 *****************************************************************************/
433 static void ProcessEscapeSequence(INT8 *pEscapeSequence)
434 {
435  
436 /* if a Left Arrow Key */
437 if (strcmppgm2ram( (const char *) pEscapeSequence, (ROM FAR char*) leftArrowEscapeSequence) == 0)
438 {
439 CursorLeft();
440 }
441 /* else if Right Arrow Key */
442 else if (strcmppgm2ram( (const char *) pEscapeSequence, (ROM FAR char*) rightArrowEscapeSequence) == 0)
443 {
444 CursorRight();
445 }
446 #if (kWFNumHistoryEntries > 0)
447 /* else if Up Arrow Key */
448 else if (strcmppgm2ram( (const char *) pEscapeSequence, (ROM FAR char*) upArrowEscapeSequence) == 0)
449 {
450  
451 DisplayHistoryEntry(kWFPrevHistory);
452 }
453 /* else if Down Arrow Key */
454 else if (strcmppgm2ram( (const char *) pEscapeSequence, (ROM FAR char*) downArrowEscapeSequence) == 0)
455 {
456 DisplayHistoryEntry(kWFNextHistory);
457 }
458 #endif
459 /* else if Home Key */
460 else if (strcmppgm2ram( (const char *) pEscapeSequence, (ROM FAR char*) homeKeyEscapeSequence) == 0)
461 {
462 CursorHome();
463 }
464 /* else if End Key */
465 else if (strcmppgm2ram( (const char *) pEscapeSequence, (ROM FAR char*) endKeyEscapeSequence) == 0)
466 {
467 CursorEnd();
468 }
469 }
470  
471 /*= Output_Monitor_Hdr =======================================================
472 Purpose: After clearing screen, outputs to terminal the header block of text.
473  
474 Inputs: None
475  
476 Returns: None
477 ============================================================================*/
478 void Output_Monitor_Hdr(void)
479 {
480 // KS:
481 // EraseEntireScreen();
482  
483 putrsUART("\n\r");
484 OutputLine('=', 79);
485 putrsUART("* WiFi Host Interface Monitor\n\r");
486 putrsUART("* (c) 2008, 2009, 2010 -- Microchip Technology, Inc.\n\r");
487 putrsUART("*\n\r* Type 'help' to get a list of commands.\n\r");
488 OutputLine('=', 79);
489 OutputCommandPrompt();
490  
491 }
492  
493 /*= OutputLine ===============================================================
494 Purpose: Outputs a line of the specified character
495  
496 Inputs: lineChar -- character the comprises the line
497 count -- number of characters in the line
498  
499 Returns: None
500 ============================================================================*/
501 static void OutputLine(INT8 lineChar, UINT8 count)
502 {
503 #if defined( STACK_USE_UART )
504 UINT8 i;
505  
506 for (i = 0; i < count; ++i)
507 {
508 while(BusyUART());
509 putcUART(lineChar);
510 }
511 putrsUART("\n\r");
512 #endif
513 }
514  
515  
516 /*= is_Printable_Character ===================================================
517 Purpose: Determines if the input character can be output to the screen
518  
519 Inputs: c -- char to test
520  
521 Returns: True if printable, else False
522 ============================================================================*/
523 static BOOL isPrintableCharacter(INT8 c)
524 {
525 if ( ((isalpha(c)) ||
526 (isdigit(c)) ||
527 (isspace(c)) ||
528 (ispunct(c)))
529  
530 &&
531  
532 (c != (INT8)kWFEnter) && (c != (INT8)kWFTab)
533  
534 )
535 {
536 return TRUE;
537 }
538 else
539 {
540 return FALSE;
541 }
542 }
543  
544 /*= InsertCharacter =========================================================
545 Purpose: Inserts and echoes an printable character into the command line at the
546 cursor location.
547  
548 Inputs: c -- char to insert
549  
550 Returns: none
551 ============================================================================*/
552 static void InsertCharacter(INT8 c)
553 {
554 UINT8 len;
555  
556 UINT8 i;
557 UINT8 orig_cursor_index = GET_CURSOR();
558 UINT8 count;
559  
560 /* throw away characters if exceeded cmd line length */
561 if (GET_LEN_RX_CMD_STRING() >= sizeof(g_ConsoleContext.rxBuf)-1)
562 {
563 return;
564 }
565  
566 len = GET_LEN_RX_CMD_STRING() + gCmdLinePromptLength;
567  
568 /* if inserting a character at end of cmd line */
569 if (GET_CURSOR() == len)
570 {
571 g_ConsoleContext.rxBuf[GET_CURSOR() - gCmdLinePromptLength] = c;
572 SET_CURSOR(GET_CURSOR() + 1);
573 EchoCharacter(c);
574 }
575 /* inserting a character somewhere before the end of command line */
576 else
577 {
578 /* Null out tmp cmd line */
579 memset(gTmpCmdLine, 0x00, sizeof(gTmpCmdLine));
580  
581 /* copy up to the point of insertion */
582 strncpy( (char *) gTmpCmdLine, (const char *) g_ConsoleContext.rxBuf, GET_CURSOR() - gCmdLinePromptLength);
583  
584 /* insert the new character */
585 gTmpCmdLine[GET_CURSOR() - gCmdLinePromptLength] = c;
586  
587 /* copy the chars after the new character */
588 strncpy( (char *) &gTmpCmdLine[GET_CURSOR() - gCmdLinePromptLength + 1],
589 (const char *) &g_ConsoleContext.rxBuf[GET_CURSOR() - gCmdLinePromptLength],
590 len - GET_CURSOR());
591  
592 /* put the first part of new string in the cmd line buffer */
593 strcpy( (char *) g_ConsoleContext.rxBuf, (const char *) gTmpCmdLine);
594  
595 /* erase entire line, put the cursor at index 0 */
596 EraseEntireLine();
597  
598 /* output the prompt */
599 putrsUART( (ROM FAR char *) gCmdLinePrompt);
600  
601 /* Output the updated command line */
602 putsUART( (char *) &g_ConsoleContext.rxBuf[0]);
603  
604 /* move the cursor to the next insert location */
605 count = (len + 1) - orig_cursor_index - 1;
606 for (i = 0; i < count; ++i)
607 {
608 putrsUART( (ROM FAR char *) cursorLeftEscapeSequence);
609 }
610  
611 SET_CURSOR(orig_cursor_index + 1);
612 }
613 }
614  
615 /*= Delete ==================================================================
616 Purpose: Deletes the character at the cursor index
617  
618 Inputs: none
619  
620 Returns: none
621 ============================================================================*/
622  
623 static void Delete(void)
624 {
625 unsigned int num_chars;
626 unsigned int orig_index = GET_CURSOR();
627  
628 /* if cursor is not at the end of the line */
629 if (GET_CURSOR() != GET_LEN_RX_CMD_STRING() + gCmdLinePromptLength)
630 {
631 /* Null out tmp cmd line */
632 memset(gTmpCmdLine, 0x00, sizeof(gTmpCmdLine));
633  
634 /* get characters before the deleted key */
635 num_chars = GET_CURSOR() - gCmdLinePromptLength;
636 strncpy( (char *) gTmpCmdLine, (const char *) g_ConsoleContext.rxBuf, num_chars);
637  
638 /* append characters after the deleted char (if there are any) */
639 if (strlen( (char *) g_ConsoleContext.rxBuf) - 1 > 0u)
640 {
641 strcpy( (char *) &gTmpCmdLine[num_chars], (const char *) &g_ConsoleContext.rxBuf[num_chars + 1]);
642 }
643  
644 EraseEntireLine(); /* leaves g_ConsoleContext.cursorIndex at 0 */
645 putrsUART( (ROM FAR char *) gCmdLinePrompt);
646  
647 strcpy( (char *) g_ConsoleContext.rxBuf, (const char *) gTmpCmdLine);
648  
649  
650 putsUART( (char *) g_ConsoleContext.rxBuf );
651 SET_CURSOR(gCmdLinePromptLength + GET_LEN_RX_CMD_STRING());
652 CursorHome(); /* to first character after prompt */
653  
654  
655 /* move cursor to point of delete */
656 CursorRight_N(orig_index - gCmdLinePromptLength);
657 }
658 }
659  
660 /*= EchoCharacter ===========================================================
661 Purpose: Echoes a character to the terminal.
662  
663 Inputs: new_char -- character to echo
664  
665 Returns: none
666 ============================================================================*/
667 static void EchoCharacter(INT8 c)
668 {
669 if (IS_ECHO_ON())
670 {
671 /* output cr then lf for lf */
672 if (c == (INT8)'\n')
673 {
674 putcUART('\r');
675 }
676  
677 putcUART(c);
678 }
679 }
680  
681 /*= Enter ====================================================================
682 Purpose: Enter key processing
683  
684 Inputs: None
685  
686 Returns: none
687 ============================================================================*/
688 static void Enter(void)
689 {
690 BOOL cmd_flag = FALSE;
691  
692  
693 if ( IS_ECHO_ON() )
694 {
695 /* if the command line has any characters in it and it is not just white space */
696 if ( (GET_LEN_RX_CMD_STRING() > 0u) && (!isCmdLineOnlyWhitespace() ) )
697 {
698 #if (kWFNumHistoryEntries > 0)
699 AddCmdToHistory();
700 #endif
701 cmd_flag = TRUE;
702 }
703 }
704 // else talking to PC app, presume it only sends valid commands
705 else
706 {
707 cmd_flag = TRUE;
708 }
709  
710 // Process command
711 if (cmd_flag == TRUE)
712 {
713 process_cmd();
714 }
715  
716 // if we got an app-specific command,
717 if (g_ConsoleContext.appConsoleMsgRx == FALSE)
718 {
719 /* linefeed and output prompt */
720 OutputCommandPrompt();
721 }
722  
723 // don't output command prompt, which also clears rx buf, until app processes it's command
724  
725 }
726  
727 #if (kWFNumHistoryEntries > 0)
728  
729 /*****************************************************************************
730 * FUNCTION: InitHistory
731 *
732 * RETURNS: None
733 *
734 * PARAMS: None
735 *
736 * NOTES: Initialize command line history states.
737 *
738 *****************************************************************************/
739 static void InitHistory(void)
740 {
741 history.index = 0;
742 history.seeded = FALSE;
743 history.recallIndex = 0;
744 }
745  
746 /*****************************************************************************
747 * FUNCTION: AddCmdToHistory
748 *
749 * RETURNS: None
750 *
751 * PARAMS: None
752 *
753 * NOTES: Adds latest command to history buffer
754 *
755 *****************************************************************************/
756 static void AddCmdToHistory(void)
757 {
758 // zero out current command at this location
759 memset((void *)&history.buf[history.index], 0x00, sizeof(history.buf[history.index]));
760  
761 // copy new command to buffer
762 memcpy( (void *) &history.buf[history.index], (void *) g_ConsoleContext.rxBuf, strlen( (char *) g_ConsoleContext.rxBuf));
763  
764 // bump index to next line in history buffer
765 history.index = (history.index + 1) % kWFNumHistoryEntries;
766  
767 // put the recall index one command in advance of the command we just added
768 history.recallIndex = history.index;
769  
770 // at least one entry in history buffer
771 history.seeded = TRUE;
772 }
773  
774 /*****************************************************************************
775 * FUNCTION: DisplayHistoryEntry
776 *
777 * RETURNS: None
778 *
779 * PARAMS: action -- PREV_HISTORY or NEXT_HISTORY
780 *
781 * NOTES: In response to the user pressing up or down arrow key, display
782 * corresponding entry in history buffer.
783 *
784 *****************************************************************************/
785 static void DisplayHistoryEntry(UINT8 action)
786 {
787  
788 BOOL foundEntry = FALSE;
789  
790 // if nothing in history buffer
791 if (history.seeded == FALSE)
792 {
793 return;
794 }
795  
796 if (action == (UINT8)kWFPrevHistory)
797 {
798 --history.recallIndex;
799 if (history.recallIndex < 0)
800 {
801 history.recallIndex = kWFNumHistoryEntries - 1;
802 }
803  
804 /* search until found a history entry or searched all entries */
805 while (foundEntry == FALSE)
806 {
807 /* if found a history entry */
808 if (history.buf[history.recallIndex][0] != 0)
809 {
810 foundEntry = TRUE;
811 }
812 else
813 {
814 --history.recallIndex;
815 if (history.recallIndex < 0)
816 {
817 history.recallIndex = kWFNumHistoryEntries - 1;
818 }
819 }
820 }
821 }
822 else /* action == kWFNextHistory */
823 {
824 history.recallIndex = (history.recallIndex + 1) % kWFNumHistoryEntries;
825  
826 /* search until found a history entry or searched all entries */
827 while (foundEntry == FALSE)
828 {
829 /* if found a history entry */
830 if (history.buf[history.recallIndex][0] != 0)
831 {
832 foundEntry = TRUE;
833 }
834 else
835 {
836 history.recallIndex = (history.recallIndex + 1) % kWFNumHistoryEntries;
837 }
838 }
839 }
840  
841 if (foundEntry)
842 {
843 // erase line on screen and output command from history
844 EraseEntireLine(); /* leaves Cursor_Index at 0 */
845 putrsUART( (ROM FAR char *) gCmdLinePrompt );
846 putsUART( (char *) history.buf[history.recallIndex]);
847  
848 // copy history command to console buffer (so they match) and put cursor
849 // at end of line
850 memset(g_ConsoleContext.rxBuf, 0x00, GET_LEN_RX_CMD_STRING() );
851 strcpy( (char *) g_ConsoleContext.rxBuf, (const char *) history.buf[history.recallIndex]);
852 SET_CURSOR(gCmdLinePromptLength + strlen( (char *) history.buf[history.recallIndex]));
853 }
854  
855 }
856 #endif /* (kWFNumHistoryEntries > 0) */
857  
858  
859 /*= Backspace ================================================================
860 Purpose: Performs a backspace operation on the command line
861  
862 Inputs: none
863  
864 Returns: none
865 ============================================================================*/
866 static void Backspace(void)
867 {
868 UINT8 num_chars;
869 UINT8 orig_index = GET_CURSOR();
870  
871 /* if cursor is not at the left-most position */
872 if (GET_CURSOR() != gCmdLinePromptLength)
873 {
874 /* Null out tmp cmd line */
875 memset(gTmpCmdLine, 0x00, sizeof(gTmpCmdLine));
876  
877 /* get characters before the backspace */
878 num_chars = GET_CURSOR() - gCmdLinePromptLength - 1;
879 strncpy( (char *) gTmpCmdLine, (const char *) g_ConsoleContext.rxBuf, num_chars);
880  
881 /* append characters after the deleted char (if there are any) */
882 if ( (GET_LEN_RX_CMD_STRING() - 1) > 0u)
883 {
884 strcpy( (char *) &gTmpCmdLine[num_chars], (const char *) &g_ConsoleContext.rxBuf[num_chars + 1]);
885 }
886  
887 EraseEntireLine(); /* leaves g_ConsoleContext.cursorIndex at 0 */
888  
889 strcpy( (char *) g_ConsoleContext.rxBuf, (const char *) gTmpCmdLine);
890  
891 putrsUART( (ROM FAR char *) gCmdLinePrompt);
892 putsUART( (char *) g_ConsoleContext.rxBuf);
893 SET_CURSOR(gCmdLinePromptLength + GET_LEN_RX_CMD_STRING());
894  
895 CursorHome(); /* to first character after prompt */
896  
897  
898 /* move cursor to point of backspace */
899 CursorRight_N(orig_index - 1 - gCmdLinePromptLength);
900 }
901 }
902  
903  
904  
905  
906 static void EraseEntireLine()
907 {
908 // int i;
909 putrsUART( (ROM FAR char*) eraseEntireLineEscapeSequence);
910 CursorLeft_N(GET_CURSOR());
911 SET_CURSOR(0);
912 }
913  
914 #if 0 /* add back if you want this feature */
915 static void EraseEntireScreen()
916 {
917 putrsUART( (ROM FAR char*) eraseEntireScreenEscapeSequence);
918 }
919 #endif
920  
921 static void OutputCommandPrompt(void)
922 {
923 if ( IS_ECHO_ON() )
924 {
925 putrsUART("\n\r");
926 putrsUART((ROM FAR char*) gCmdLinePrompt);
927 }
928 SET_CURSOR(gCmdLinePromptLength);
929 memset(g_ConsoleContext.rxBuf, 0x00, sizeof(g_ConsoleContext.rxBuf));
930  
931 }
932  
933 /*= CursorRight =============================================================
934 Purpose: Moves the cursor right by one character
935  
936 Inputs: none
937  
938 Returns: none
939 ============================================================================*/
940 void CursorRight(void)
941 {
942 /* if cursor is not already at the right-most position */
943 if (GET_CURSOR() < GET_LEN_RX_CMD_STRING() + gCmdLinePromptLength)
944 {
945 SET_CURSOR( GET_CURSOR() + 1);
946 putrsUART( (ROM FAR char*) cursorRightEscapeSequence);
947 }
948 }
949  
950  
951 /*= CursorRight_N ==============================================================
952 Purpose: Moves the cursor left N characters to the right
953  
954 Inputs: n -- number of characters to move the cursor to the left
955  
956 Note: This sequence only takes a single digit of length, so may need to
957 do the move in steps
958  
959  
960 Returns: none
961 ============================================================================*/
962 void CursorRight_N(UINT8 n)
963 {
964 INT8 sequence_string[sizeof(cursorRightEscapeSequence) + 2]; /* null and extra digit */
965  
966 // ASSERT(n <= (strlen(g_ConsoleContext.buf) + CMD_LINE_PROMPT_LENGTH));
967  
968 if (n > 0u)
969 {
970 SET_CURSOR( GET_CURSOR() + n );
971 sequence_string[0] = cursorRightEscapeSequence[0]; /* ESC */
972 sequence_string[1] = cursorRightEscapeSequence[1]; /* '[' */
973  
974 if (n < 10u)
975 {
976 sequence_string[2] = n + '0'; /* ascii digit */
977 sequence_string[3] = cursorRightEscapeSequence[3]; /* 'C' */
978 sequence_string[4] = '\0';
979 }
980 else
981 {
982 sequence_string[2] = (n / 10) + '0'; /* first ascii digit */
983 sequence_string[3] = (n % 10) + '0'; /* second ascii digit */
984 sequence_string[4] = cursorRightEscapeSequence[3]; /* 'C' */
985 sequence_string[5] = '\0';
986  
987 }
988  
989 putsUART( (char *) sequence_string);
990 }
991 }
992  
993 /*= CursorLeft ==============================================================
994 Purpose: Moves the cursor left by one character
995  
996 Inputs: none
997  
998 Returns: none
999 ============================================================================*/
1000 void CursorLeft(void)
1001 {
1002 /* if cursor is not already at the left-most position */
1003 if (GET_CURSOR() > strlenpgm( (ROM FAR char *) gCmdLinePrompt))
1004 {
1005 SET_CURSOR( GET_CURSOR() - 1);
1006 putrsUART( (ROM FAR char *) cursorLeftEscapeSequence);
1007 }
1008 }
1009  
1010  
1011 /*= CursorLeft_N ==============================================================
1012 Purpose: Moves the cursor left N characters to the left
1013  
1014 Inputs: n -- number of characters to move the cursor to the left
1015  
1016 Note: This sequence only takes a single digit of length, so may need to
1017 do the move in steps
1018  
1019  
1020 Returns: none
1021 ============================================================================*/
1022 void CursorLeft_N(UINT8 n)
1023 {
1024 INT8 sequence_string[sizeof(cursorLeftEscapeSequence) + 2]; /* null and extra digit */
1025  
1026 // ASSERT(n <= g_ConsoleContext.cursorIndex + CMD_LINE_PROMPT_LENGTH);
1027  
1028 if (n > 0u)
1029 {
1030 SET_CURSOR( GET_CURSOR() - n );
1031  
1032 sequence_string[0] = cursorLeftEscapeSequence[0]; /* ESC */
1033 sequence_string[1] = cursorLeftEscapeSequence[1]; /* '[' */
1034  
1035 if (n < 10u)
1036 {
1037 sequence_string[2] = n + '0'; /* ascii digit */
1038 sequence_string[3] = cursorLeftEscapeSequence[3]; /* 'D' */
1039 sequence_string[4] = '\0';
1040 }
1041 else
1042 {
1043 sequence_string[2] = (n / 10) + '0'; /* first ascii digit */
1044 sequence_string[3] = (n % 10) + '0'; /* second ascii digit */
1045 sequence_string[4] = cursorLeftEscapeSequence[3]; /* 'D' */
1046 sequence_string[5] = '\0';
1047  
1048 }
1049  
1050 putsUART( (char *) sequence_string);
1051 }
1052 }
1053  
1054  
1055 /*= CursorHome ==============================================================
1056 Purpose: Moves the cursor to the left-most position of the command line (just
1057 in front of the prompt).
1058  
1059 Inputs: none
1060  
1061 Returns: none
1062 ============================================================================*/
1063 static void CursorHome(void)
1064 {
1065 /* if cursor not at start of command line */
1066 if (GET_CURSOR() != gCmdLinePromptLength)
1067 {
1068 /* move it to start of command line */
1069 CursorLeft_N(GET_CURSOR() - gCmdLinePromptLength);
1070 }
1071 }
1072  
1073 static void CursorEnd(void)
1074 {
1075 UINT8 len;
1076  
1077 if ( (GET_LEN_RX_CMD_STRING() + gCmdLinePromptLength) != GET_CURSOR())
1078 {
1079 len = GET_LEN_RX_CMD_STRING() - GET_CURSOR() + gCmdLinePromptLength;
1080 CursorRight_N(len);
1081 }
1082 }
1083  
1084  
1085 static BOOL isCmdLineOnlyWhitespace(void)
1086 {
1087 UINT8 i;
1088 UINT8 len = GET_LEN_RX_CMD_STRING();
1089  
1090 for (i = 0; i < len; ++i)
1091 {
1092 if ( !isspace(g_ConsoleContext.rxBuf[i]) )
1093 {
1094 return FALSE;
1095 }
1096 }
1097  
1098 return TRUE;
1099 }
1100  
1101 #if 0 /* Add back if you need this func */
1102  
1103 static void UnderlineMode(void)
1104 {
1105 putrsUART(inverseVideoEscapeSequence);
1106 }
1107  
1108 static void NormalMode(void)
1109 {
1110 putrsUART(normalModeEscapeSequence);
1111 }
1112  
1113  
1114 /*****************************************************************************
1115 * FUNCTION: isCtrlCharacter
1116 *
1117 * RETURNS: TRUE if input is a ctrl character, else FALSE
1118 * REG_OK_16_BIT_REG -- valid 16-bit register
1119 * REG_UNKNOWN -- unknown register ID
1120 * REG_VAL_TOO_LARGE -- reg value to large for an 8-bit register
1121 *
1122 * PARAMS: None
1123 *
1124 * NOTES: Called by do_writereg_cmd and do_readreg_cmd to verify if accessing
1125 * a legal register. In the case of write, function verifies that
1126 * write value doesn't overflow an 8-bit register.
1127 *
1128 *****************************************************************************/
1129 static BOOL isCtrlCharacter(INT8 c)
1130 {
1131 if (isprint(c))
1132 {
1133 return FALSE;
1134 }
1135 else
1136 {
1137 return TRUE;
1138 }
1139 }
1140 #endif
1141  
1142 void WFConsoleSetMsgFlag(void)
1143 {
1144 g_ConsoleContext.appConsoleMsgRx = TRUE;
1145 }
1146  
1147 void WFConsoleReleaseConsoleMsg(void)
1148 {
1149 /* linefeed and output prompt */
1150 OutputCommandPrompt();
1151  
1152 g_ConsoleContext.appConsoleMsgRx = FALSE;
1153 }
1154  
1155 BOOL WFConsoleIsConsoleMsgReceived(void)
1156 {
1157  
1158 return g_ConsoleContext.appConsoleMsgRx;
1159 }
1160  
1161 void WFConsolePrintInteger(UINT32 val, char mode)
1162 {
1163 switch (mode)
1164 {
1165 case 'c':
1166 sprintf( (char *) g_ConsoleContext.txBuf, "%c", (int)val);
1167 break;
1168 case 'x':
1169 sprintf( (char *) g_ConsoleContext.txBuf, "%x", (unsigned int)val);
1170 break;
1171 case 'u':
1172 sprintf( (char *) g_ConsoleContext.txBuf, "%u", (unsigned int)val);
1173 break;
1174 case 'd':
1175 default:
1176 sprintf( (char *) g_ConsoleContext.txBuf, "%d", (int)val);
1177 }
1178  
1179 putsUART( (char*) g_ConsoleContext.txBuf);
1180 }
1181  
1182 void WFConsolePrintHex(UINT32 val, UINT8 width)
1183 {
1184 switch (width)
1185 {
1186 case 2:
1187 sprintf( (char *) g_ConsoleContext.txBuf, "%02x", (unsigned int)val);
1188 break;
1189 case 4:
1190 sprintf( (char *) g_ConsoleContext.txBuf, "%04x", (unsigned int)val);
1191 break;
1192 case 8:
1193 sprintf( (char *) g_ConsoleContext.txBuf, "%08lx", (unsigned long)val);
1194 break;
1195 default:
1196 sprintf( (char *) g_ConsoleContext.txBuf, "%x", (unsigned int)val);
1197 }
1198  
1199 putsUART( (char*) g_ConsoleContext.txBuf);
1200 }
1201  
1202 #endif /* WF_CONSOLE */
{BLAME END}
{FOOTER START}

Powered by WebSVN v2.8.3