summaryrefslogtreecommitdiff
path: root/riff/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'riff/main.cpp')
-rwxr-xr-xriff/main.cpp240
1 files changed, 240 insertions, 0 deletions
diff --git a/riff/main.cpp b/riff/main.cpp
new file mode 100755
index 0000000..076a65f
--- /dev/null
+++ b/riff/main.cpp
@@ -0,0 +1,240 @@
+/*
+ * main.cpp
+ *
+ * Copyright (C) ST-Ericsson SA 2011
+ * Authors: Srimanta Panda <srimanta.panda@stericsson.com>,
+ * Ola Borgelin <ola.borgelin@stericsson.com>,
+ * Karin Hedlund <karin.hedlund@stericsson.com>,
+ * Markus Andersson <markus.m.andersson@stericsson.com> for ST-Ericsson.
+ * License terms: 3-clause BSD license
+ *
+ */
+
+/*
+ * @addtogroup main
+ * @{
+ */
+#include "main.h"
+#include "CDAL.h"
+#include "DutManager.h"
+#include <iostream>
+#include <string>
+#include <vector>
+#include <utility>
+#include <cstring>
+#include <cstdlib>
+#include <cstdio>
+using namespace std;
+
+bool isDone = false;
+const string info = \
+ "\n \
+----------------------- riff - Raw Image File Flasher -------------------------\n \
+Version: 0.4.1\n \
+"
+ "Flash a device. Try `riff --help' for more information. \n \
+"
+ "-------------------------------------------------------------------------------";
+
+const string cmdHelpString = " \n \
+Usage: riff [OPTION]...[FILE]...\n\n \
+Available command line arguments are:\n\n \
+ -h, --help\t Display this help message.\n \
+ -m, --mode ACTION\t Select the mode.\n \
+ ACTION is `flash`, `erase` or `dump`\n \
+ -c, --config PATH\t Give path to configuration file.\n \
+ -f, --flashimage IMAGEPATH\t Give path to flashimage.\n \
+ -a, --address FLASHADDR\t Give a start address in hex.\n \
+ -l, --length HEXLENGTH Length of the dump [Byte] in hex.\n \
+ -d, --dumppath DUMPPATH File path on PC where to store dump.\n \
+ -v, --verbose\t Shows more detailed information.\n \
+ --version\t Shows version information.\n \
+";
+
+
+int main(int argc, char* argv[])
+{
+ signal(SIGINT, interrupt);
+
+ cout << info << endl;
+
+ handleCmdLineInput(argc, argv);
+
+ logger_ = new Logger("Application");
+ logger_->log(Logger::INFO, "Using config file %s", configFile);
+
+ //Parse the config file.
+ config_ = new Config(configFile);
+
+ // if flashimage path has not been set from commandline
+ //then use the path in the config file if it is set there.
+ if (!strlen(flashimage)) {
+ if (config_->valueExists("FLASH_IMG_PATH")) {
+ strcpy(flashimage, config_->getValue("FLASH_IMG_PATH"));
+ }
+ }
+
+ // Set the LCM path from ConfigFile, init the USB driver and set the callback function
+ SetLCMLibPath(config_->getValue("LCMLIBPATH"));
+ usb_init_driver(config_->getValue("VENDORID"), config_->getValue("PRODUCTID"));
+ usb_set_listen_callback(UsbDeviceEventCallback);
+
+
+ logger_->log(Logger::PROGRESS, "Listening on USB for device connection...");
+
+ while (!isDone) {
+ sleep(1);
+ }
+
+ usb_deinit_driver();
+
+ _exit(exitstatus);
+}
+
+void interrupt(int param)
+{
+ logger_->log(Logger::PROGRESS, "Program interrupted...");
+ exitstatus = 1;
+ isDone = true;
+}
+
+void UsbDeviceEventCallback(DeviceStatus_t status, DeviceEvent_t event, Device_t device)
+{
+ if (COMM_DEVICE_SUCCESS == status) {
+ const DUT* dut;
+
+ switch (event) {
+ case LIBUSB_DEVICE_CONNECTED:
+
+ logger_->log(Logger::INFO, "Device detected on USB@%u", comm_get_physical_address(device));
+ dut = DutManager::createDut(device);
+
+ if (0 != dut) {
+ logger_->log(Logger::PROGRESS, "Connected %s", dut->getId());
+ }
+
+ break;
+ case LIBUSB_DEVICE_DISCONNECTED: {
+ logger_->log(Logger::INFO, "Disconnect detected on USB@%u", comm_get_physical_address(device));
+ dut = DutManager::getDut(device);
+
+ if (0 != dut) {
+ exitstatus = dut->getErrorCode();
+ logger_->log(Logger::PROGRESS, "Disconnected %s", dut->getId());
+ DutManager::destroyDut(device);
+ }
+
+ isDone = true;
+ }
+ break;
+ default:
+ logger_->log(Logger::ERROR, "Unknown USB event %d", event);
+ break;
+ }
+ } else {
+ logger_->log(Logger::ERROR, "USB device error %d", status);
+ }
+}
+void handleCmdLineInput(int argc, char* argv[])
+{
+ string input;
+ bool configPathSet = false;
+
+ for (int i = 1; i != argc; ++i) {
+ input = argv[i];
+
+ if ("-h" == input || "--help" == input) {
+ cout << cmdHelpString << endl;
+ _exit(0);
+ }
+
+ else if ("-v" == input || "--verbose" == input) {
+ Logger::verbose_ = true;
+ }
+
+ else if ("--version" == input) {
+ _exit(0);
+ }
+
+ else if ("-m" == input || "--mode" == input) {
+ i = checkCmdLineArgument(argc, i, argv, mode);
+ }
+
+ else if ("-c" == input || "--config" == input) {
+ configPathSet = true;
+ i = checkCmdLineArgument(argc, i, argv, configFile);
+
+ }
+
+ else if ("-f" == input || "--flashimage" == input) {
+ i = checkCmdLineArgument(argc, i, argv, flashimage);
+ }
+
+ else if ("-a" == input || "--address" == input) {
+ i = checkCmdLineArgument(argc, i, argv, address);
+ }
+
+ else if ("-l" == input || "--length" == input) {
+ i = checkCmdLineArgument(argc, i, argv, length);
+ }
+
+ else if ("-d" == input || "--dumppath" == input) {
+ i = checkCmdLineArgument(argc, i, argv, dumpPath);
+ } else {
+ cout << "Unknown option: " << input << endl;
+ }
+ }
+
+ if (!configPathSet) {
+ FILE* userConfig;
+ char* home = getenv("HOME");
+ char homeConfigPath[50];
+ strcpy(homeConfigPath, home);
+ strcat(homeConfigPath, "/.riff/config");
+
+
+ userConfig = fopen(homeConfigPath, "rb");
+
+ if (userConfig != NULL) {
+ strcpy(configFile, homeConfigPath);
+ fclose(userConfig);
+ }
+ else
+ {
+ // It will check the default config in /usr/share folder otherwise
+ userConfig = fopen(configFile, "r");
+ if(userConfig == NULL)
+ {
+ cout << cmdHelpString << endl;
+ _exit(0);
+ }
+ }
+
+
+ }
+ if (*dumpPath == '\0') {
+ //Sets default dump path if not provided
+ char* home = getenv("HOME");
+ strcpy(dumpPath, home);
+ strcat(dumpPath, "/flashdump.bin");
+ }
+ SequenceFactory::setArguments(configFile, mode, flashimage, address, length, dumpPath);
+}
+
+int checkCmdLineArgument(int argc, int counter, char* argv[], char* var)
+{
+ counter++;
+
+ if (counter < argc)
+
+ {
+ strcpy(var, argv[counter]);
+ } else {
+ cout << "Please give a parameter to the option" << endl;
+ counter--;
+ }
+
+ return counter;
+}
+
+/* @} */