/* * Function: commandShell(char *command) * Parameters: None * Returns: None * Description: This functions behaves like shell answering all user commands */ void Server::commandShell(char *command) { CommandID command_id = getCommandID(command); switch(command_id) { case COMMAND_HELP: // handle help command command_help(); break; case COMMAND_CREATOR: // handle CREATOR command command_creator(); break; case COMMAND_DISPLAY: // handle DISPLAY command command_display(); break; case COMMAND_LIST: // handle LIST command command_list(); break; default: printf("Please enter a valid command \n"); printf("Type Help - to display supported commands \n"); break; } }
/** * Liest ein Kommando ein, ist blockierend! * Greift auf cmd_functions.read() zurueck * Achtung, die Payload wird nicht mitgelesen! */ int8_t command_read(void) { int16_t bytesRcvd; int8_t start = 0; // Start des Kommandos int8_t i; command_t * command; // Pointer zum Casten der empfangegen Daten uint8_t buffer[RCVBUFSIZE]; // Puffer #ifdef PC #if BYTE_ORDER == BIG_ENDIAN uint16_t store; // Puffer fuer die Endian-Konvertierung #endif #endif // PC #ifdef MCU uint16_t old_ticks; // alte Systemzeit #endif buffer[0] = 0; // Sicherheitshalber mit sauberem Puffer anfangen /* Daten holen, maximal soviele, wie ein Kommando lang ist */ const uint8_t n = sizeof(command_t); bytesRcvd = cmd_functions.read(buffer, n); if (bytesRcvd < 0) { LOG_ERROR("command_read(): read() returned %d", bytesRcvd); return -1; } #ifdef DEBUG_COMMAND_NOISY LOG_DEBUG("%d read", bytesRcvd); LOG_DEBUG("%x %x %x", buffer[0], buffer[1], buffer[2]); #endif /* Suche nach dem Beginn des Frames */ while ((start < bytesRcvd) && (buffer[start] != CMD_STARTCODE)) { LOG_DEBUG("falscher Startcode: 0x%02x (should be 0x%02x), start=%d bytesRcvd=%d", buffer[start], CMD_STARTCODE, start, bytesRcvd); start++; } /* Wenn keine STARTCODE gefunden ==> Daten verwerfen */ if (buffer[start] != CMD_STARTCODE) { #ifdef DEBUG_COMMAND_NOISY LOG_DEBUG("kein Startcode"); #endif return -2; } #ifdef DEBUG_COMMAND_NOISY LOG_DEBUG("Start @%d", start); #endif // haben wir noch genug Platz im Puffer, um das Packet ferig zu lesen? if ((RCVBUFSIZE - (uint8_t) (start)) < n) { LOG_DEBUG("not enough space"); return -3; // nein? ==> verwerfen } i = (int8_t) ((int8_t) n - (bytesRcvd - start)); if (i > 0) { // Fehlen noch Daten ? LOG_DEBUG("Start @ %d es fehlen %d Bytes ", start, i); #ifdef MCU old_ticks = TIMER_GET_TICKCOUNT_16; // Systemzeit erfassen #endif /* So lange Daten lesen, bis das Packet vollstaendig ist, oder der Timeout zuschlaegt */ while (i > 0) { #ifdef MCU if (timer_ms_passed_16(&old_ticks, COMMAND_TIMEOUT)) { /* Timeout ueberschritten */ LOG_DEBUG("Timeout beim Nachlesen"); return -4; // ==> Abbruch } #endif // MCU LOG_DEBUG("%d Bytes missing", i); i = (int8_t) cmd_functions.read(buffer + bytesRcvd, i); LOG_DEBUG("%d read", i); bytesRcvd = bytesRcvd + i; i = (int8_t) ((int8_t) n - (bytesRcvd - start)); } } #ifdef DEBUG_COMMAND_NOISY LOG_DEBUG("%d/%d read/start", bytesRcvd, start); LOG_DEBUG("%x %x %x", buffer[start], buffer[start+1], buffer[start+2]); #endif command = (command_t *) (buffer + start); // Cast in command_t #ifdef DEBUG_COMMAND_NOISY LOG_DEBUG("start: %c ", command->startCode); // command_display(command); #endif // validate (Startcode ist bereits ok, sonst waeren wir nicht hier) if (command->CRC == CMD_STOPCODE) { #ifdef DEBUG_COMMAND_NOISY LOG_DEBUG("Command is valid"); #endif #ifdef CRC_CHECK if (! cmd_functions.crc_check(command)) { LOG_ERROR("CRC ungueltig:"); #ifdef DEBUG_COMMAND command_display(command); #endif memcpy(&received_command, command, sizeof(command_t)); return -20; } else { #ifdef DEBUG_COMMAND_NOISY LOG_DEBUG("CRC korrekt"); #endif // DEBUG_COMMAND_NOISY } #endif // CRC_CHECK #ifdef CHECK_CMD_ADDRESS /* Ist das Paket ueberhaupt fuer uns? */ if ((command->to != CMD_BROADCAST) && (command->to != CMD_IGNORE_ADDR) && (command->to != get_bot_address()) && (command->request.command != CMD_WELCOME)) { LOG_DEBUG("Fehler: Paket To= %d statt %u", command->to, get_bot_address()); #ifdef LOG_AVAILABLE command_display(command); #endif return -10; } #endif // CHECK_CMD_ADDRESS // Transfer memcpy(&received_command, command, sizeof(command_t)); #ifdef PC #if BYTE_ORDER == BIG_ENDIAN /* Umwandeln der 16 bit Werte in Big Endian */ store = received_command.data_l; received_command.data_l = store << 8; received_command.data_l |= (store >> 8) & 0xff; store = received_command.data_r; received_command.data_r = store << 8; received_command.data_r |= (store >> 8) & 0xff; #endif // BYTE_ORDER == BIG_ENDIAN #endif // PC #ifdef DEBUG_COMMAND_NOISY LOG_DEBUG("Command received:"); command_display(&received_command); #endif return 0; } else { // Command not valid