static void execute_console_cmd(int argc, A_CHAR *argv[]) { A_BOOL found = FALSE; CONSOLE_COMMAND *cmd; cmd = Console_info->command_list; while (cmd != NULL) { if (A_STRCMP(cmd->name,(const char *)argv[0]) == 0) { if (0 != A_STRNCMP(cmd->name, "iwconfig ", 8)) { CONSOLE_OUTPUT("\n"); } cmd->execute(argc,argv); found = TRUE; break; } cmd = cmd->next; } if (!found) { CONSOLE_OUTPUT("\n\n** cmd : %s - not found\n",argv[0]); } }
LOCAL int _do_memory_op(void *pAddress, A_BOOL read, A_UINT32 write_value, MEMORY_ACCESS_WIDTH width) { A_UINT32 read_value = 0; int incr = sizeof(A_UINT32); switch (width) { case MEMORY_ACCESS_BYTE : if (read) { read_value = *(volatile A_UINT8 *)pAddress; } else { *(volatile A_UINT8 *)pAddress = (A_UINT8)write_value; } incr = sizeof(A_UINT8); break; case MEMORY_ACCESS_SHORT : if (read) { read_value = *(volatile A_UINT16 *)pAddress; } else { *(volatile A_UINT16 *)pAddress = (A_UINT16)write_value; } incr = sizeof(A_UINT16); break; case MEMORY_ACCESS_WORD : if (read) { read_value = *(volatile A_UINT32 *)pAddress; } else { *(volatile A_UINT32 *)pAddress = (A_UINT32)write_value; } break; default : break; } if (read) { switch (width) { case MEMORY_ACCESS_BYTE : CONSOLE_OUTPUT("0x%X : 0x%2.2X\n",(A_UINT32)pAddress,read_value); break; case MEMORY_ACCESS_SHORT : CONSOLE_OUTPUT("0x%X : 0x%4.4X\n",(A_UINT32)pAddress,read_value); break; case MEMORY_ACCESS_WORD : CONSOLE_OUTPUT("0x%X : 0x%8.8X\n",(A_UINT32)pAddress,read_value); break; default : break; } } return incr; }
static int print_help(int argc, A_CHAR *argv[]) { CONSOLE_COMMAND *cmd; cmd = Console_info->command_list; CONSOLE_OUTPUT("\nCommand List:\n\n"); while (cmd != NULL) { CONSOLE_OUTPUT("%s - %s\n",cmd->name,cmd->description); cmd = cmd->next; } return 0; }
LOCAL void _show_console_history(A_BOOL Up) { int len; A_CHAR *history; int i; /* find the next non-empty command-line history buffer, the direction of * searching is indicated */ for (i = 0; i < Console_info->config.max_history; i++) { history = get_console_history_buf(Console_info->history_read_index); len = A_STRLEN(history); if (len > 0) { A_STRCPY(Console_info->current_line,history); Console_info->chars_avail = len; Console_info->cursor_pos = Console_info->chars_avail; refresh_console_line(); move_next_history_read_index(Up); break; } /* keep looking until we wrap back around */ move_next_history_read_index(Up); } if (i == Console_info->config.max_history) { CONSOLE_OUTPUT(ASCII_BELL); } }
LOCAL A_BOOL _insert_console_char(A_CHAR character) { A_BOOL refresh = FALSE; if (Console_info->chars_avail >= Console_info->config.max_cmdline_length) { return FALSE; } if (Console_info->cursor_pos < Console_info->chars_avail) { /* move chars */ A_MEMMOVE(&Console_info->current_line[Console_info->cursor_pos + 1], &Console_info->current_line[Console_info->cursor_pos], Console_info->chars_avail - Console_info->cursor_pos); refresh = TRUE; } Console_info->current_line[Console_info->cursor_pos] = character; Console_info->chars_avail++; Console_info->cursor_pos++; Console_info->current_line[Console_info->chars_avail] = '\0'; if (refresh) { refresh_console_line(); } else { CONSOLE_OUTPUT("%c",character); } return TRUE; }
LOCAL void _refresh_console_line(void) { int backup; CONSOLE_OUTPUT(ANSI_VT100_CLEAR_LINE); /* back up max-line worth of characters which will reset the cursor to the front of the line */ CONSOLE_OUTPUT("\x1b[%dD",Console_info->config.max_cmdline_length); CONSOLE_OUTPUT(CONSOLE_PROMPT); CONSOLE_OUTPUT("%s", (char *)Console_info->current_line); /* the cursor will be parked at the end of the current line, back it up to reflect the current * cursor position */ backup = Console_info->chars_avail - Console_info->cursor_pos; if (backup > 0) { CONSOLE_OUTPUT("\x1b[%dD",backup); } }
static int mem_write(int argc, A_CHAR *argv[]) { MEMORY_ACCESS_OPTIONS options; A_MEMZERO(&options,sizeof(options)); if (get_mem_access_options(argc,argv,&options) < 0) { return -1; } if (!(options.flags & WRITE_VALUE_OPTION)) { CONSOLE_OUTPUT("Missing Write Data\n"); return -1; } do_memory_op((void *)options.address,FALSE,options.write_val,options.width); return 0; }
LOCAL int _get_mem_access_options(int argc, A_CHAR *argv[], MEMORY_ACCESS_OPTIONS *options) { int i; options->width = MEMORY_ACCESS_WORD; options->count = 1; for (i = 1; i < argc; i++) { if ((A_STRCMP(argv[i],"-a") == 0) && ((i + 1) < argc)) { options->address = A_ATOUL(argv[i + 1]); options->flags |= ADDRESS_OPTION; } if (A_STRCMP(argv[i],"-b") == 0) { options->width = MEMORY_ACCESS_BYTE; } if (A_STRCMP(argv[i],"-h") == 0) { options->width = MEMORY_ACCESS_SHORT; } if (A_STRCMP(argv[i],"-w") == 0) { options->width = MEMORY_ACCESS_WORD; } if ((A_STRCMP(argv[i],"-c") == 0) && ((i + 1) < argc)) { options->count = A_ATOUL(argv[i + 1]); } if ((A_STRCMP(argv[i],"-d") == 0) && ((i + 1) < argc)) { options->write_val = A_ATOUL(argv[i + 1]); options->flags |= WRITE_VALUE_OPTION; } } if (!(options->flags & ADDRESS_OPTION)) { CONSOLE_OUTPUT("Missing Address\n"); return -1; } return 0; }
LOCAL void console_mod_start(void) { SERIAL_PORT_PROPERTIES port_properties; SER_PORT_UART_CONFIG port_config; A_STATUS status; #ifdef CONFIG_CLI console_register_commands(Default_console_commands,NUM_DEFAULT_CONSOLE_CMDS); #else console_mod_register_commands(Default_console_commands,NUM_DEFAULT_CONSOLE_CMDS); #endif /* open the logical port */ Console_info->port = SERP_Open(Console_info->config.logical_port_name, NULL, console_serial_event_callback, &port_properties); A_MEMZERO(&port_config,sizeof(port_config)); port_config.Baud = Console_info->config.baud; port_config.DataBits = 8; port_config.StopBits = 1; /* configure port */ status = SERP_Ioctl(Console_info->port, SER_PORT_IOCTL_CONFIG_UART_PORT, &port_config, sizeof(port_config)); A_ASSERT(A_SUCCESS(status)); /* set receiver threshold to 1 character */ SERP_Ioctl(Console_info->port,SER_PORT_IOCTL_SET_RX_THRESH,NULL,1); if (Console_info->config.flags & CONSOLE_CONFIG_FLAGS_ALLOW_RX_SUSPEND) { Console_info->flags |= CONSOLE_FLAGS_ALLOW_RX_SUSPEND; } if (Console_info->config.flags & CONSOLE_CONFIG_FLAGS_USE_HW_RX_WAKE) { /* try using hardware for RX wake */ Console_info->flags |= CONSOLE_FLAGS_USE_HW_RX_SUSP; } if ((UART_SERP_UART0_INSTANCE_ID == Console_info->config.phys_uart_id) || (Console_info->flags & CONSOLE_FLAGS_ALLOW_RX_SUSPEND)) { console_suspend_rx(0,NULL); /* install uart control commands */ #ifdef CONFIG_CLI console_register_commands(&Console_uart_control,1); #else console_mod_register_commands(&Console_uart_control,1); //ychen #endif } #ifndef USE_STANDALONE_CONSOLE _A_OS_INDIRECTION_TABLE->cmnos.serial._serial_putc = console_putc_replacement; #endif _A_OS_INDIRECTION_TABLE->cmnos.serial._serial_getc = console_getc_replacement; _A_OS_INDIRECTION_TABLE->cmnos.serial._serial_restore_funcs = console_restore_replacement; CONSOLE_OUTPUT("\nCLI:"); Console_info->flags = 0; CONSOLE_OUTPUT(CONSOLE_PROMPT_LINE); }
void _console_process_char(void) { A_UINT8 character; int length; int bytes_avail; A_BOOL extended = FALSE; while (TRUE) { length = 1; bytes_avail = SERP_Read(Console_info->port,(A_UINT8 *)&character,&length); if (length == 0) { break; } //CONSOLE_OUTPUT("%2.2X\n",character); if (Console_info->flags & CONSOLE_FLAGS_RX_SUSPENDED) { if (character == ASCII_CARRIAGE_RETURN) { /* activate the console when the first enter key is pressed */ Console_info->flags &= ~CONSOLE_FLAGS_RX_SUSPENDED; if (!(Console_info->flags & CONSOLE_FLAGS_USE_HW_RX_SUSP)) { /* switch to fast polling in the port */ SERP_Ioctl(Console_info->port, SER_PORT_HW_IOCTL_SET_POLL_INTERVAL, NULL, CONSOLE_FAST_POLL_INTERVAL_MS); } } else { continue; } } if (Console_info->current_escape_pos > 0) { /* in an escape sequence */ Console_info->escape_buffer[Console_info->current_escape_pos] = character; Console_info->current_escape_pos++; if (Console_info->current_escape_pos >= MIN_ESCAPE_BYTES) { if (Console_info->escape_buffer[1] == '[') { switch (Console_info->escape_buffer[2]) { case 'A' : /* cursor up */ show_console_history(TRUE); break; case 'B' : /* cursor down */ show_console_history(FALSE); break; case 'C' : /* cursor FORWARD */ if (!move_console_cursor(CURSOR_FORWARD)){ CONSOLE_OUTPUT(ASCII_BELL); } else { CONSOLE_OUTPUT(ANSI_VT100_CURSOR_FWD); } break; case 'D' : /* cursor BACKWARDS */ if (!move_console_cursor(CURSOR_BACKWARD)) { CONSOLE_OUTPUT(ASCII_BELL); } else { CONSOLE_OUTPUT(ANSI_VT100_CURSOR_BACK); } break; case '4' : if (Console_info->current_escape_pos <= MIN_ESCAPE_BYTES) { extended = TRUE; } else { if (Console_info->escape_buffer[3] == 0x7e) { /* delete key */ if (!process_console_delete()) { CONSOLE_OUTPUT(ASCII_BELL); } } } break; default: break; } } if (!extended) { Console_info->current_escape_pos = 0; } } continue; } if ((character >= 0x20) && (character <= 0x7E)) { if (!insert_console_char(character)) { CONSOLE_OUTPUT(ASCII_BELL); } continue; } switch (character) { case ASCII_CARRIAGE_RETURN : if (Console_info->config.flags & CONSOLE_CONFIG_FLAGS_AWAKE_FOR_READ) { A_POWER_SYSSLEEP_CONTROL(1); /* Permit sleep while processing cmd */ } process_console_line(); if (Console_info->config.flags & CONSOLE_CONFIG_FLAGS_AWAKE_FOR_READ) { A_POWER_SYSSLEEP_CONTROL(0); /* disallow sleep while waiting for keystrokes */ } if (Console_info->flags & CONSOLE_FLAGS_ALLOW_RX_SUSPEND) { schedule_console_idle_recv_timer(); } break; case ASCII_BACK_SPACE : if (!process_console_backspace()) { CONSOLE_OUTPUT(ASCII_BELL); } break; case ASCII_ESCAPE : Console_info->escape_buffer[0] = character; Console_info->current_escape_pos = 1; break; default : break; } } }
LOCAL void _process_console_line(void) { A_CHAR *history; int argc; A_CHAR *ptr; A_BOOL found_start; A_BOOL found_end; A_BOOL prompt_flg = 0; if (Console_info->chars_avail) { Console_info->current_line[Console_info->chars_avail] = '\0'; /* save current line to history buffer */ history = get_console_history_buf(Console_info->history_write_index); A_STRCPY(history,Console_info->current_line); if ((0 == A_STRNCMP(history, "iwconfig ", 8)) || (0 == A_STRNCMP(history, "wmiconfig --connect", 19)) || (0 == A_STRNCMP(history, "wmiconfig --disc", 16))) { prompt_flg = 1; } Console_info->history_write_index++; if (Console_info->history_write_index >= Console_info->config.max_history) { Console_info->history_write_index = 0; } argc = 0; ptr = Console_info->current_line; found_start = FALSE; found_end = TRUE; A_MEMZERO(&Console_info->argv_buffers,sizeof(Console_info->argv_buffers)); while ((*ptr != '\0') && (argc < CONSOLE_DEF_MAX_ARGC)) { if (!found_start) { if (*ptr != ' ') { found_start = TRUE; found_end = FALSE; Console_info->argv_buffers[argc] = ptr; } } else { if (*ptr == ' ') { *ptr = '\0'; argc++; found_start = FALSE; found_end = TRUE; } } ptr++; } if ((argc < CONSOLE_DEF_MAX_ARGC) && found_start && !found_end) { argc++; } if (argc) { execute_console_cmd(argc,Console_info->argv_buffers); } } Console_info->chars_avail = 0; Console_info->cursor_pos = 0; if (prompt_flg == 1) { return; } if (!(Console_info->flags & CONSOLE_FLAGS_RX_SUSPENDED)) { CONSOLE_OUTPUT(CONSOLE_PROMPT_LINE); } }
int my_protocol_callback(struct libwebsocket_context *context, struct libwebsocket *wsi, enum libwebsocket_callback_reasons reason, void *user, void *in, size_t len) { unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 512 + LWS_SEND_BUFFER_POST_PADDING]; switch (reason) { case LWS_CALLBACK_ESTABLISHED: CONSOLE_OUTPUT("New Connection"); break; case LWS_CALLBACK_SERVER_WRITEABLE: break; case LWS_CALLBACK_RECEIVE: { /* message from client received. check if length of received data is in allowed limits. two bytes are reserved for length information */ if (len > RECVBUFFER_SIZE - 2) { throw Clock::ClockException(std::string("Too large data callback from Websocket interface (") + std::to_string(len) + ") bytes"); } MutexGuard lock(WebsocketAmbassador::getMutex()); RecvBuffer& recvBuf = WebsocketAmbassador::getRecvBuffer(); /* check if an instant response is required, i.e. do we find a special character as the first byte */ char firstByte; memcpy(&firstByte, static_cast<char*>(in), 1); if (firstByte == TIMEREQUEST_CHAR) { /* respond with current time shown on the clock */ SendBuffer &sendBuf = WebsocketAmbassador::getSendBuffer(); int sendDataSize = 0; memcpy(&sendDataSize, &sendBuf[0], sizeof(sendBuf[0])); for (int i=0; i <= sendDataSize; i++) { buf[LWS_SEND_BUFFER_PRE_PADDING + i ] = sendBuf[i+1]; } libwebsocket_write(wsi, &buf[LWS_SEND_BUFFER_PRE_PADDING], sendDataSize, LWS_WRITE_TEXT); } else { /* store length of received data to first two elements of buffer */ std::stringstream ss; ss << std::setfill('0') << std::setw(2) << len << std::endl; std::string lenStr = ss.str(); lenStr.copy(&recvBuf[USERREQUEST_SIZE_POSITION], USERREQUEST_SIZE_LENGTH); /* copy received data to buffer after size information for further processing */ memcpy(&recvBuf[USERREQUEST_SIZE_POSITION + USERREQUEST_SIZE_LENGTH], in, len); } } case LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION: break; default: break; } return 0; }