// internals void * DashelInterface::run(void *param) { DashelInterface * instance = ((DashelInterface*)param); while (1) { try { instance->dashelConnect(); printf("Connected to target: %s\n", instance->dashelParams); instance->isConnected = true; GetDescription getDescription; getDescription.serialize(instance->stream); instance->stream->flush(); break; } catch (Dashel::DashelException e) { fprintf(stderr, "Cannot connect to target: %s\n", instance->dashelParams); instance->isConnected = false; sleep(1000000L); // 1s } } while (instance->isRunning) instance->dashelRun(); return NULL; }
RangerAsebaBridge::RangerAsebaBridge(const char* target): targetStream(connect(target)) { // request a description of the target GetDescription getDescription; getDescription.serialize(targetStream); targetStream->flush(); // emit event to enable feedback and encoders vector<int> arg; arg.push_back(1); emit(ENABLE_ENCODERS, arg); emit(ENABLE_FEEDBACK, arg); }
//! Process a command, return the number of arguments eaten (not counting the command itself) int processCommand(Stream* stream, int argc, char *argv[]) { const char *cmd = argv[0]; int argEaten = 0; if (strcmp(cmd, "presence") == 0) { GetDescription message; message.serialize(stream); stream->flush(); } else if (strcmp(cmd, "usermsg") == 0) { // first arg is type, second is length if (argc < 2) errorMissingArgument(argv[0]); argEaten = argc; uint16 type = atoi(argv[1]); uint16 length = argc-2; UserMessage::DataVector data(length); for (size_t i = 0; i < length; i++) data[i] = atoi(argv[i+2]); UserMessage message(type, data); message.serialize(stream); stream->flush(); } else if (strcmp(cmd, "rdpage") == 0) { // first arg is dest, second is page number if (argc < 3) errorMissingArgument(argv[0]); argEaten = 2; CmdBootloaderInterface bootloader(stream, atoi(argv[1])); vector<uint8> data(bootloader.getPageSize()); if (bootloader.readPage(atoi(argv[2]), &data[0])) { ofstream file("page.bin"); if (file.good()) copy(data.begin(), data.end(), ostream_iterator<uint8>(file)); else errorOpenFile("page.bin"); } else errorReadPage(atoi(argv[2])); } else if(strcmp(cmd, "rdpageusb") == 0) { if (argc < 3) errorMissingArgument(argv[0]); argEaten = 2; CmdBootloaderInterface bootloader(stream, atoi(argv[1])); vector <uint8> data(2048); cout << "Page: " << atoi(argv[2]) << endl; if(bootloader.readPageSimple(atoi(argv[2]), &data[0])) { ofstream file("page.bin"); if(file.good()) copy(data.begin(),data.end(),ostream_iterator<uint8>(file)); else errorOpenFile("page.bin"); } else errorReadPage(atoi(argv[2])); } else if (strcmp(cmd, "whex") == 0) { bool reset = 0; // first arg is dest, second is file name if (argc < 3) errorMissingArgument(argv[0]); argEaten = 2; if (argc > 3 && !strcmp(argv[3], "reset")) { reset = 1; argEaten = 3; } // try to write hex file try { CmdBootloaderInterface bootloader(stream, atoi(argv[1])); bootloader.writeHex(argv[2], reset, false); } catch (HexFile::Error &e) { errorHexFile(e.toString()); } } else if (strcmp(cmd,"wusb") == 0) { bool reset = 0; if (argc < 3) errorMissingArgument(argv[0]); argEaten = 2; if(argc > 3 && !strcmp(argv[3], "reset")) { reset = 1; argEaten = 3; } try { CmdBootloaderInterface bootloader(stream, atoi(argv[1])); bootloader.writeHex(argv[2], reset, true); } catch (HexFile::Error &e) { errorHexFile(e.toString()); } } else if (strcmp(cmd, "rhex") == 0) { // first arg is source, second is file name if (argc < 3) errorMissingArgument(argv[0]); argEaten = 2; // try to read hex file try { CmdBootloaderInterface bootloader(stream, atoi(argv[1])); bootloader.readHex(argv[2]); } catch (HexFile::Error &e) { errorHexFile(e.toString()); } } else if (strcmp(cmd, "eb") == 0) { uint16 dest; if(argc < 2) errorMissingArgument(argv[0]); argEaten = 1; dest = atoi(argv[1]); BootloaderReset msg(dest); msg.serialize(stream); stream->flush(); // Wait ack from bootloader (mean we got out of it) // Or bootloader description (mean we just entered it) // WRONG; FIXME while (true) { Message *message = Message::receive(stream); // handle ack BootloaderAck *ackMessage = dynamic_cast<BootloaderAck *>(message); if (ackMessage && (ackMessage->source == dest)) { cout << "Device is now in user-code" << endl; delete message; break; } //BootloaderDescription * bMessage = dynamic_cast<BootloaderDescription *>(message); delete message; } } else if (strcmp(cmd, "sb") == 0) { uint16 dest; if(argc < 2) errorMissingArgument(argv[0]); argEaten = 1; dest = atoi(argv[1]); Reboot msg(dest); msg.serialize(stream); stream->flush(); } else if (strcmp(cmd, "sleep") == 0) { uint16 dest; if(argc < 2) errorMissingArgument(argv[0]); argEaten = 1; dest = atoi(argv[1]); Sleep msg(dest); msg.serialize(stream); stream->flush(); } else errorUnknownCommand(cmd); return argEaten; }