void MiniBeeV2::routeMsg(char type, char *msg, uint8_t size) { uint8_t len; char * ser; if ( loopback ){ char * loopbackMsg = (char *)malloc(sizeof(char)* (size + 2 ) ); loopbackMsg[0] = type; loopbackMsg[1] = size; for ( i=0; i<size; i++ ){ loopbackMsg[i+2] = msg[i]; } send( N_INFO, loopbackMsg, size + 2 ); free( loopbackMsg ); } switch(type) { case S_ANN: if ( remoteConfig ){ sendSerialNumber(); status = WAITFORHOST; // send( N_INFO, "waitforhost", 11 ); } break; case S_QUIT: if ( remoteConfig ){ status = WAITFORHOST; //do something to stop doing anything } // send( N_INFO, "waitforhost", 11 ); break; case S_ID: if ( remoteConfig ){ if ( checkIDMsg( msg[0] ) ){ len = strlen(serial); ser = (char *)malloc(sizeof(char)* (len + 1 ) ); for(i = 0;i < len;i++) { ser[i] = msg[i+1]; } ser[len] = '\0'; if(strcmp(ser, serial) == 0){ node_id = msg[len+1]; //writeConfig(msg); if ( size == (len+3) ){ config_id = msg[len+2]; status = WAITFORCONFIG; char configInfo[2]; configInfo[0] = node_id; configInfo[1] = config_id; send( N_WAIT, configInfo, 2 ); // send( N_INFO, "waitforconfig", 13 ); } else if ( size == (len+2) ) { readConfig(); status = SENSING; // send( N_INFO, "sensing", 7 ); } // } else { // // send( N_INFO, "wrong serial number", 19 ); // send( N_INFO, ser, len ); } free(ser); } } break; case S_CONFIG: if ( remoteConfig ){ // check if right config_id: if ( checkConfMsg( msg[0] ) ){ if ( msg[1] == config_id ){ writeConfig( msg ); readConfig(); // readConfigMsg( msg ); if ( hasInput ){ status = SENSING; } else if ( hasOutput ){ status = ACTING; } // send( N_INFO, "sensing", 7 ); } } } break; case S_RUN: if ( checkNodeMsg( msg[0], msg[1] ) ){ setRunning( msg[2] ); } break; case S_LOOP: if ( checkNodeMsg( msg[0], msg[1] ) ){ setLoopback( msg[2] ); } break; /* case S_PWM: if ( checkNodeMsg( msg[0], msg[1] ) ){ for( i=0; i<6; i++){ pwm_values[i] = msg[2+i]; } setPWM(); } break; case S_DIGI: if ( checkNodeMsg( msg[0], msg[1] ) ){ for( i=0; i< (size-2); i++){ digital_values[i] = msg[2+i]; } setDigital(); } break; */ case S_OUT: if ( checkNodeMsg( msg[0], msg[1] ) ){ setOutputValues( msg, 2 ); setOutput(); } break; case S_CUSTOM: if ( checkNodeMsg( msg[0], msg[1] ) ){ this->customMsgFunc( msg ); } break; case N_DATA: if ( checkNotNodeMsg( msg[0] ) ){ this->dataMsgFunc( msg ); } break; // default: // break; } }
// Waits for input from the user, also printing new output when appropriate void getInput(char* szInput, short nMaxLength) { time_t timeout; char chKeyPressed; short nPosition = 0; static OutputData odMessage; // Decrease nMaxLength by one, since this function later doesn't count the null zero nMaxLength--; if(szInput == NULL || nMaxLength < 1) return; timeout=time(NULL); while (1) { // Check for lost carrier, time expired, etc. (Note that in the OpenDoors implementation, these functions // are blank, since OpenDoors does this type of checking automatically) checkCarrier(); checkTimeLeft(); inactiveCheck(timeout); chKeyPressed = 0; // While no input on screen, continue to display output for this node. while ( nPosition == 0 && chKeyPressed == 0 ) { checkCarrier(); checkTimeLeft(); inactiveCheck(timeout, 0); // Check for inter-node messages in synchronet if ( checkNodeMsg() ) fixStatline(); // Only respond to keyboard if there's a valid prompt on screen. if ( isValidPrompt() ) chKeyPressed = inputKey(); else Sleep(50); // Display all output waiting in mailslot, if any. if ( getOutput() ) fixStatline(); } // If no key pressed this cycle, try getting the key again. if ( chKeyPressed == 0 && isValidPrompt() ) chKeyPressed = inputKey(); // If a key was pressed if ( chKeyPressed != 0 ) { timeout=time(NULL); // Fix CR/LF problem, and ignore otherwise invalid characters if ( chKeyPressed == 10 || chKeyPressed < 0 ) continue; // Enter key hit: Add ending \0 to string and return. if(chKeyPressed == '\r' || chKeyPressed == '\n') { szInput[nPosition] = '\0'; newline(); bPromptOnScreen = false; return; } // At this point, user has definitely hit a non-enter key. If this is the // first key hit of a string, and there's no prompt on screen yet, display // it first before displaying the keystroke. if ( nPosition == 0 && !bPromptOnScreen ) fixStatline(); // Backspace key if( chKeyPressed == 8 && nPosition > 0 ) { backspace(1); --nPosition; continue; } // Any normal key if(chKeyPressed >= 32 && nPosition < nMaxLength && (unsigned char)chKeyPressed <= 127) { // Display the key and add it to the input buffer. printChar(chKeyPressed); szInput[nPosition++] = chKeyPressed; // If in hotkey mode, send the input immediately. if ( !bCommandMode ) { szInput[nPosition] = '\0'; newline(); bPromptOnScreen = false; return; } } } } }