console.h File Reference

Go to the source code of this file.

Typedefs

typedef cmd_state_t(* cmd_cb_t )(int argc, char *argv[], void *ctx)

Enumerations

enum  cmd_state_t { CMD_DONE, CMD_INPROGRESS }

Functions

int console_add_cmd (const char *str, cmd_cb_t cb, void *ctx)
char * console_gets (void)
void console_init (void)
void console_init_silent (void)
void console_poll (void)
int console_schedule_cmd (char *cmd, int interactive)


Typedef Documentation

typedef cmd_state_t(* cmd_cb_t)(int argc, char *argv[], void *ctx)

Definition at line 37 of file console.h.


Enumeration Type Documentation

Enumerator:
CMD_DONE 
CMD_INPROGRESS 

Definition at line 32 of file console.h.

00032              {
00033         CMD_DONE,
00034         CMD_INPROGRESS
00035 } cmd_state_t;


Function Documentation

int console_add_cmd ( const char *  str,
cmd_cb_t  cb,
void *  ctx 
)

Definition at line 91 of file console.c.

References ARRAY_SIZE, and cmd_list.

Referenced by wl_init_complete_cb().

00092 {
00093         U32 i;
00094         for (i = 0; i < ARRAY_SIZE(cmd_list); i++)
00095                 if (!cmd_list[i].cb)
00096                         break;
00097         
00098         if (i == ARRAY_SIZE(cmd_list))
00099                 return -1;
00100         
00101         cmd_list[i].str = str;
00102         cmd_list[i].cb = cb;
00103         cmd_list[i].ctx = ctx;
00104         return 0;
00105 }

char* console_gets ( void   ) 

Definition at line 60 of file console.c.

References AVR32_USART, and CMD_MAX_LEN.

Referenced by console_poll().

00061 {
00062         static char buf[CMD_MAX_LEN];
00063         static int pos = 0;
00064         int c;
00065         int status;
00066         
00067         for (;;) {
00068                 status = usart_read_char(&AVR32_USART, &c);
00069                 if (status == USART_RX_EMPTY)
00070                         return NULL;
00071                 
00072                 if (status == USART_RX_ERROR) {
00073                         AVR32_USART.cr = AVR32_USART_CR_RSTSTA_MASK;
00074                         return NULL;
00075                 }
00076                 
00077                 if (c == '\r') {
00078                         usart_putchar(&AVR32_USART, '\n');
00079                         buf[pos] = 0;
00080                         pos = 0;
00081                         return buf;
00082                 }
00083                 usart_putchar(&AVR32_USART, c);
00084                 buf[pos++] = c;
00085                 if (pos == sizeof(buf))
00086                         pos = 0;
00087         }
00088         return NULL;
00089 }

void console_init ( void   ) 

Definition at line 107 of file console.c.

References is_initialized, printk(), and TRUE.

Referenced by wl_init_complete_cb().

00108 {
00109         printk("\n$ ");
00110         is_initialized = TRUE;
00111 }

void console_init_silent ( void   ) 

Definition at line 113 of file console.c.

References is_initialized, and TRUE.

00113                                {
00114         is_initialized = TRUE;
00115 }

void console_poll ( void   ) 

Definition at line 191 of file console.c.

References console_gets(), and console_schedule_cmd().

Referenced by poll().

00192 {
00193         char *buf;
00194         buf = console_gets();
00195         console_schedule_cmd(buf, 1);
00196 }

int console_schedule_cmd ( char *  cmd,
int  interactive 
)

Definition at line 117 of file console.c.

References ARRAY_SIZE, cb, CMD_INPROGRESS, cmd_list, ctx, MAX_ARGS, printk(), and str.

Referenced by console_poll(), gui_connect_cb(), set_wep_key_cb(), and set_wpa_key_cb().

00117                                                      {
00118 #define MAX_ARGS 8
00119         static int argc, i;
00120         static char* argv[MAX_ARGS];
00121         static char *buf;
00122         static enum { INPUT, RUN } state = INPUT;
00123 
00124         switch (state) {
00125         case INPUT: {
00126                 char* token;
00127                 if (NULL == cmd) {
00128                         return 0;
00129                 }
00130                 buf = strdup(cmd);
00131                 if (!buf)
00132                         return 0;
00133                 if (!strlen(buf)) {
00134                         interactive ? printk("$ ") : 0;
00135                         free(buf);
00136                         return 0;
00137                 }
00138 #ifdef WIFI_DEBUG_ON
00139                 printk("%s : Scheduling command \"%s\"\n",
00140                        __func__,
00141                        buf);
00142 #endif
00143                 for (i = 0; i < ARRAY_SIZE(cmd_list); i++)
00144                         if(!strncmp(cmd_list[i].str, buf, 2))
00145                                 break;
00146 
00147                 if (ARRAY_SIZE(cmd_list) == 0) {
00148                         printk("No commands available. Is the WiFi card responding?\n");
00149                 }
00150                 if (i == ARRAY_SIZE(cmd_list)) {
00151                         if (interactive) {
00152                                 printk("available commands:\n");
00153                                 for (i = 0; i < ARRAY_SIZE(cmd_list); i++)
00154                                         if (cmd_list[i].cb)
00155                                                 printk("  %s\n", cmd_list[i].str);
00156                                 printk("$ ");
00157                         }
00158                         free(buf);
00159                         return 0;
00160                 }
00161                 
00162                 for (token = strtok(buf, " "); token != NULL;
00163                      token = strtok(NULL, " ")) {
00164                         argv[argc] = token;
00165                         argc++;
00166                         if (argc == MAX_ARGS)
00167                                 break;
00168                 }
00169                 
00170                 
00171                 state = RUN;
00172         } /* fall through */
00173                 
00174         case RUN: {
00175                 cmd_state_t s = cmd_list[i].cb(argc, argv, cmd_list[i].ctx);
00176                 if (s == CMD_INPROGRESS)
00177                         return 1;
00178 
00179                 interactive ? printk("$ ") : 0;
00180 
00181                 argc = 0;
00182                 free(buf);
00183                 state = INPUT;
00184         }
00185         }
00186 
00187         return 1;
00188 }


Generated on Fri Feb 19 02:24:07 2010 for AVR32 - H&D by  doxygen 1.5.5