00001
00017 #include "cli.h"
00018 #include "menu_cli.h"
00019
00020 #include <string.h>
00021
00022
00023 #define AUTO_COMPLETE 0x1b //get_char 0x5b
00024
00025 static const cli_str* acpi;
00026 static str_t tkn;
00027 static char msg_dest[MAX_TAM_SIZE_STRING];
00028
00029 extern cli_str const api_cli[];
00030 static const cli_str* menu_cli = api_cli;
00031
00032 char buff_cmd[MAX_COMMAND_SIZE];
00033 int buff_cmd_counter=0;
00034
00035
00045 int mount_token(char *data)
00046 {
00047 char* buff="\0";
00048 char* buff2="\0";
00049
00050 U08_t i,j;
00051
00052 memset(tkn.cmd_name, '\0',TAM_STRING);
00053 memset(tkn.value, '\0',TAM_STRING);
00054 memset(tkn.resp, '\0',TAM_STRING);
00055
00056 if((j = strlen(data))==0){
00057 DEBUG_ERROR(INVALID_DATA);
00058 return -1;
00059 }
00060 else {
00061 for(i=0;i<j;i++){
00062 data[i] = toupper(data[i]);
00063 if(data[i] == ' '){
00064 }
00065 }
00066 }
00067
00068 DEBUG_CLI(data);
00069 buff = strtok(data," ");
00070
00071
00072
00073 if(strcmp(buff, "GET")==0) tkn.tipo = GET_CMD;
00074 else if(strcmp(buff, "SET")==0) tkn.tipo = SET_CMD;
00075 else if(strcmp(buff, "HELP")==0) tkn.tipo = HELP_CMD;
00076 else if(strcmp(buff, "CHAT")==0) tkn.tipo = CHAT_CMD;
00077 else if(strcmp(buff, "FTP")==0) tkn.tipo = FTP_CMD;
00078 else return -1;
00079 buff2 = strtok(NULL, " ");
00080 if (tkn.tipo == CHAT_CMD || tkn.tipo == FTP_CMD){
00081 strncpy(tkn.value, buff2,j);
00082 strncpy(tkn.cmd_name, buff,strlen(buff));
00083 return 0;
00084 }
00085 strncpy(tkn.cmd_name, buff2,j);
00086 buff2 = strtok(NULL, " ");
00087 strcpy(tkn.value, buff2);
00088
00089
00090
00091
00092
00093 return 0;
00094 }
00095
00108 int CLI_Builder(char *data, char *dest){
00109
00110 acpi = menu_cli;
00111 const cli_str* mCmd;
00112 char *buff;
00113 int ret=0,i=0;
00114
00115 DEBUG_ENTER;
00116
00117 strcpy(msg_dest, " ");
00118
00119 if (strlen(data)<=2) return -1;
00120
00121 if(mount_token(data)<0){
00122 unknown_cmd(data);
00123 strcpy(dest, msg_dest);
00124 return -1;
00125 }
00126
00127 if(tkn.tipo == HELP_CMD){
00128 help_func();
00129 strcpy(dest, msg_dest);
00130 return ret;
00131 }
00132
00133 mCmd = acpi;
00134 for(i=0;;i++){
00135
00136
00137
00138 buff = mCmd->string;
00139
00140 if(buff == NULL){
00141 DEBUG_ERROR(COMMAND_NOT_FOUND);
00142 unknown_cmd(tkn.cmd_name);
00143 strcpy(dest, msg_dest);
00144 return -1;
00145 }
00146 if(!strcmp(buff, tkn.cmd_name)) break;
00147 mCmd = acpi++;
00148 }
00149
00150 DEBUG_CLI("Chamando função tratadora do comando.\n");
00151 ret = ExecCommand(mCmd);
00152 strcpy(dest, msg_dest);
00153
00154
00155 memb_free(msg_dest, msg_dest);
00156 memb_free(&tkn, tkn.resp);
00157 memb_free(&tkn, tkn.cmd_name);
00158 memb_free(&tkn, tkn.value);
00159 memb_free(&tkn, tkn.tipo);
00160 memb_free(&mCmd, buff);
00161
00162
00163 DEBUG_EXIT;
00164
00165 return ret;
00166 }
00167
00176 void cmdExec(int (*ApiFunc) (str_t *tp)) {
00177 break_t error;
00178 DEBUG_ENTER;
00179
00180
00181 error = (ApiFunc) (&tkn);
00182 DEBUG_CLI("OK.");
00183 if(error == RET_ERROR){
00184 DEBUG_ERROR(EXEC_COMMAND_ERROR);
00185 strcat(msg_dest,"COMMAND ERROR\n");
00186 }
00187 else {
00188 strcat(msg_dest, tkn.resp);
00189 strcat(msg_dest, "\n");
00190 }
00191 DEBUG_EXIT;
00192 }
00193
00204 int ExecCommand(const cli_str* acp){
00205
00206 strcpy(msg_dest, "\n");
00207 strcat(msg_dest, tkn.cmd_name);
00208 strcat(msg_dest, ": ");
00209 cmdExec(acp->pFunc);
00210
00211
00212
00213 return 0;
00214 }
00215
00225 void UARTTakeString(char *str, char channel)
00226 {
00227 int vd = 0xff;
00228 char tp[MAX_COMMAND_SIZE]="\0";
00229 char cond = '\n';
00230 do{
00231 vd = UARTReceiveByte(channel);
00232 uart_putc(vd);
00233 if (vd != cond) sprintf(tp, "%s%c", tp,vd);
00234 }while (vd != cond);
00235 strcpy(str, tp);
00236 }
00237
00244 void set_IpDefault(void){
00245 vectorAddrIPv4[0] = 192;
00246 vectorAddrIPv4[1] = 168;
00247 vectorAddrIPv4[2] = 1;
00248 vectorAddrIPv4[3] = 13;
00249 set_IP();
00250 }
00251
00258 void set_GatewayDefault(void){
00259 uip_GAT0 = 192;
00260 uip_GAT1 = 168;
00261 uip_GAT2 = 1;
00262 uip_GAT3 = 1;
00263 set_Gateway();
00264 }
00265
00272 void set_MaskDefault(void){
00273 uip_Mask0 = 255;
00274 uip_Mask1 = 255;
00275 uip_Mask2 = 255;
00276 uip_Mask3 = 0;
00277 set_Mask();
00278 }
00279
00280
00289 static void help_func(void){
00290
00291 acpi = menu_cli;
00292 const cli_str* mCmd;
00293 char buff[200]="\0";
00294 int i;
00295
00296 DEBUG_ENTER;
00297
00298 mCmd = acpi++;
00299 strcpy(msg_dest,"\nFuncoes Basicas do Módulo Principal\n");
00300 for(i=0;;i++){
00301
00302 if(mCmd->string == NULL) break;
00303 sprintf(buff, "\n%20s\t - \t%s",mCmd->string,mCmd->help);
00304 strncat(msg_dest, buff, strlen(buff));
00305
00306 mCmd = acpi++;
00307 }
00308
00309 DEBUG_EXIT;
00310 }
00311
00312
00319 static void unknown_cmd(char *str)
00320 {
00321 char buff[100];
00322 if(strlen(str) > 0) {
00323 sprintf(buff, "\nUnknown command: %s\n", str);
00324 strcpy(msg_dest, buff);
00325 }
00326 }
00327
00328
00345 int UARTTakeStringAsync(char *str, char channel)
00346 {
00347 char vd = 0xff, i;
00348
00349 char cond = '\n';
00350
00351 vd = UARTReceiveByte(channel);
00352
00353 if(buff_cmd_counter == 0){
00354
00355 if(vd == 0x01b){
00356 vd = UARTReceiveByte(channel);
00357 printf("*");
00358
00359 if(vd = 0x5b){
00360 uart_puts(buff_cmd);
00361 strcpy(str, buff_cmd);
00362 return 0;
00363 }
00364 }
00365
00366 for(i=0;i<MAX_COMMAND_SIZE;i++) buff_cmd[i]='\0';
00367 }
00368
00369
00370 if(vd == cond || vd == 0x0D){
00371 DEBUG_ENTER;
00372 sprintf(buff_cmd, "%s\0", buff_cmd,vd);
00373 strcpy(str, buff_cmd);
00374 buff_cmd_counter = 0;
00375 return 0;
00376 }
00377
00378
00379 else if (vd != cond){
00380 uart_putc(vd);
00381 sprintf(buff_cmd, "%s%c", buff_cmd,vd);
00382
00383 buff_cmd_counter++;
00384 return -1;
00385 }
00386
00387 return 0;
00388 }
00389