void HIDBridge_::read(void) { // check for read timeout only if really needed if (!nhp_read.mode){ if (millis() - readTimeout > HIDBRIDGE_RX_TIMEOUT) { // reset reportID and NHP if we have a timeout reportID = 0; nhp_read.mode = NHP_RESET; err(HIDBRIDGE_ERR_READ_TO); } } // read as long as the Serial is available // but do not block forever rx_buffer_index_t i; for (i = 0; i < SERIAL_RX_BUFFER_SIZE; i++){ // read in new Serial byte int b = HIDBRIDGE_SERIAL.read(); if (b < 0) break; // process with NHP protocol if (readNHP(b, &nhp_read)) { // command indicates a new hidReport (command==reportID) or the end (command==0) if (nhp_read.mode == NHP_COMMAND) proceedCommand(); // NHP address contains control data or hid in/out report data else if (nhp_read.mode == NHP_ADDRESS) proceedAddress(); } // NHP reading error else if (nhp_read.errorLevel) { // ASCII if (b < 128) { // possible main mcu reset if (!b) err(HIDBRIDGE_ERR_MCU_RST); else { //TODO different errors? err(HIDBRIDGE_ERR_SERIALB); } } else err(HIDBRIDGE_ERR_SERIALB); // do not change rxReady state because of a possible full buffer // which causes NHP corruption err(HIDBRIDGE_ERR_NHP_ERR); } } // end of for reading loop // save new time if (i) readTimeout = millis(); }
int main(int argc, char **argv) { signal(SIGINT, handler); while (1) { std::string args; args = readline(); if (args.length() < 1) break; std::deque<std::string> tokens; tokens = split(args, '|', tokens); pids.resize(tokens.size()); std::deque<int *> ffd(tokens.size(), new int[2]); for (int i = 0; i < tokens.size() - 1; ++i) { pipe(ffd[i]); } for (int i = 0; i < tokens.size(); ++i) { int id; if ((id = fork()) > 0) { pids[i] = id; } else { if (i != 0) { //dup2(ffd[i - 1][1], ffd[i][0]); dup2(ffd[i - 1][1], STDIN_FILENO); close(ffd[i - 1][0]); } if (i != tokens.size() - 1) { dup2(ffd[i][1], STDOUT_FILENO); } proceedCommand(tokens[i]); close(ffd[i][1]); close(ffd[i][0]); } if (id < 0) return 0; } for (int i = 0, j; i < tokens.size(); ++i) { waitpid(pids[i], &j, 0); } for (int i = 0; i < tokens.size() - 1; ++i) { close(ffd[i][0]); close(ffd[i][1]); } pids.clear(); } }