00001
00028 #include <avr32/io.h>
00029 #include "compiler.h"
00030 #include "board.h"
00031 #include "power_clocks_lib.h"
00032 #include "gpio.h"
00033 #include "usart.h"
00034 #include "printf-stdarg.h"
00035 #include <string.h>
00036 #include "console.h"
00037
00038 struct {
00039 cmd_cb_t cb;
00040 const char* str;
00041 void* ctx;
00042 } cmd_list[16] = { { 0 } };
00043
00044 #define ARRAY_SIZE(a) sizeof(a) / sizeof(a[0])
00045
00046 #if BOARD == EVK1104 || BOARD == EVK1100 || BOARD == EVK1101
00047 #define AVR32_USART AVR32_USART1
00048 #elif BOARD == EVK1105
00049 #define AVR32_USART AVR32_USART0
00050 #else
00051 #error
00052 #endif
00053
00054 #ifndef CMD_MAX_LEN
00055 #define CMD_MAX_LEN 80
00056 #endif
00057
00058 static Bool is_initialized = FALSE;
00059
00060 char* console_gets()
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 }
00090
00091 int console_add_cmd(const char* str, cmd_cb_t cb, void* ctx)
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 }
00106
00107 void console_init(void)
00108 {
00109 printk("\n$ ");
00110 is_initialized = TRUE;
00111 }
00112
00113 void console_init_silent(void) {
00114 is_initialized = TRUE;
00115 }
00116
00117 int console_schedule_cmd(char *cmd, int interactive) {
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 }
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 }
00189
00190
00191 void console_poll(void)
00192 {
00193 char *buf;
00194 buf = console_gets();
00195 console_schedule_cmd(buf, 1);
00196 }