/*! * Reads the command line options passed into the constructor. * * This method can return a return code to its caller, which will cause the * tool to exit immediately with that return code value. Normally, though, it * will return -1 to signal that the tool should continue to execute and * all options were processed successfully. * * The Options class is used to parse command line options. See * #k_optionsDefinition for the list of options and #k_usageText for the * descriptive help for each option. * * \retval -1 The options were processed successfully. Let the tool run normally. * \return A zero or positive result is a return code value that should be * returned from the tool as it exits immediately. */ int processOptions() { Options options(*m_argv, k_optionsDefinition); OptArgvIter iter(--m_argc, ++m_argv); // process command line options int optchar; const char * optarg; while (optchar = options(iter, optarg)) { switch (optchar) { case '?': printUsage(options); return 0; case 'v': printf("%s %s\n%s\n", k_toolName, k_version, k_copyright); return 0; case 'd': Log::getLogger()->setFilterLevel(Logger::DEBUG); break; case 'q': Log::getLogger()->setFilterLevel(Logger::WARNING); break; case 'V': m_isVerbose = true; break; case 'n': m_keyCount = strtol(optarg, NULL, 0); break; default: Log::log(Logger::ERROR, "error: unrecognized option\n\n"); printUsage(options); return 1; } } // handle positional args if (iter.index() < m_argc) { // Log::SetOutputLevel leveler(Logger::DEBUG); // Log::log("positional args:\n"); int i; for (i = iter.index(); i < m_argc; ++i) { // Log::log("%d: %s\n", i - iter.index(), m_argv[i]); m_positionalArgs.push_back(m_argv[i]); } } // all is well return -1; }
/*! * Core of the tool. Calls processOptions() to handle command line options * before performing the real work the tool does. */ int run() { try { // read command line options int result; if ((result = processOptions()) != -1) { return result; } // set verbose logging setVerboseLogging(); // make sure a file was provided if (m_positionalArgs.size() < 1) { throw std::runtime_error("no output file path was provided"); } // generate key files string_vector_t::const_iterator it = m_positionalArgs.begin(); for (; it != m_positionalArgs.end(); ++it) { generateKeyFile(*it); } } catch (std::exception & e) { Log::log(Logger::ERROR, "error: %s\n", e.what()); return 1; } catch (...) { Log::log(Logger::ERROR, "error: unexpected exception\n"); return 1; } return 0; }
bool BusPal::parse(const string_vector_t& params, BusPal::BusPalConfigData& config) { if (!params[0].compare(0, 3, "spi")) { config.function = BusPal::kBusPalFunction_SPI; if ((params.size() > 1)) { int32_t spiSpeed = atoi(params[1].c_str()); if ( spiSpeed <= 0 ) return false; config.spiSpeedKHz = spiSpeed; if (params.size() > 2) { config.spiPolarity = (BusPal::spi_clock_polarity_t)atoi(params[2].c_str()); if (params.size() > 3) { config.spiPhase = (BusPal::spi_clock_phase_t)atoi(params[3].c_str()); if (params.size() > 4) { if (!strcmp(params[4].c_str(), "lsb")) { config.spiDirection = BusPal::kSpiLsbFirst; } else if (!strcmp(params[4].c_str(), "msb")) { config.spiDirection = BusPal::kSpiMsbFirst; } } } } } } else if (!params[0].compare(0, 3, "i2c")) { config.function = BusPal::kBusPalFunction_I2C; if (params.size() > 1) { uint32_t i2cAddress = strtoul(params[1].c_str(), NULL, 16); if (i2cAddress > 0x7F) { i2cAddress &= 0x7F; Log::info("Only 7-bit i2c address is supported, so the effective value is 0x%x\n", i2cAddress); } config.i2cAddress = (uint8_t)i2cAddress; if (params.size() > 2) { int32_t i2cSpeed = atoi(params[2].c_str()); if (i2cSpeed <= 0) return false; config.i2cSpeedKHz = i2cSpeed; } } } else if (!params[0].compare(0, 3, "can")) { config.function = BusPal::kBusPalFunction_CAN; if ((params.size() > 1)) { uint32_t canSpeed = atoi(params[1].c_str()); if (canSpeed > 4) return false; config.canSpeed = canSpeed; if (params.size() > 2) { config.canTxid = strtoul(params[2].c_str(), NULL, 16) & 0x7ff; } if (params.size() > 3) { config.canRxid = strtoul(params[3].c_str(), NULL, 16) & 0x7ff; } } } else if (!params[0].compare(0, 4, "gpio")) { if (!params[1].compare(0, 6, "config")) { if (params.size() != 5) { return false; } config.function = kBusPalFunction_GPIO_CONFIG; config.gpioPort = (uint8_t) *params[2].c_str(); config.gpioPinNum = atoi(params[3].c_str()); config.gpioMux = atoi(params[4].c_str()); } else if (!params[1].compare(0, 3, "set")) { if (params.size() != 5) { return false; } config.function = kBusPalFunction_GPIO_SET; config.gpioPort = (uint8_t) *params[2].c_str(); config.gpioPinNum = atoi(params[3].c_str()); config.gpioLevel = atoi(params[4].c_str()); } else { return false; } } else if (!params[0].compare(0, 5, "clock")) { if (params.size() != 2) { return false; } config.function = kBusPalFunction_FPGA_CLOCK_SET; config.fpgaClockMhz = atoi(params[1].c_str()); } else { // Error: Invalid BusPal function. return false; } return true; }