diff --git a/.gitignore b/.gitignore index e7550bf9..aec1d546 100644 --- a/.gitignore +++ b/.gitignore @@ -12,7 +12,8 @@ Makefile.in *.o *.dirstamp /src/ebusd/ebusd -/src/ebusctl/ebusctl +/src/tools/ebusctl +/src/tools/ebusfeed /src/lib/utils/libutils.a /src/lib/ebus/libebus.a /src/lib/ebus/test/test_bus diff --git a/Makefile.am b/Makefile.am index d4638cc4..d2178823 100644 --- a/Makefile.am +++ b/Makefile.am @@ -3,7 +3,7 @@ SUBDIRS = docs \ src/lib/ebus \ src/lib/ebus/test \ src/ebusd \ - src/ebusctl + src/tools distclean-local: -rm -rf autom4te.cache diff --git a/configure.ac b/configure.ac index e49a1449..3ca3cfd4 100644 --- a/configure.ac +++ b/configure.ac @@ -29,7 +29,7 @@ AC_CONFIG_FILES([Makefile src/lib/ebus/Makefile src/lib/ebus/test/Makefile src/ebusd/Makefile - src/ebusctl/Makefile]) + src/tools/Makefile]) AC_CHECK_PROGS([HAVE_DOXYGEN], [doxygen]) if test -z "$HAVE_DOXYGEN"; diff --git a/src/ebusctl/ebusctl.cpp b/src/ebusctl/ebusctl.cpp deleted file mode 100644 index da642266..00000000 --- a/src/ebusctl/ebusctl.cpp +++ /dev/null @@ -1,136 +0,0 @@ -/* - * Copyright (C) Roland Jax 2012-2014 - * - * This file is part of ebusd. - * - * ebusd is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * ebusd is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with ebusd. If not, see http://www.gnu.org/licenses/. - */ - -#ifdef HAVE_CONFIG_H -#include -#endif - -#include "appl.h" -#include "port.h" -#include "decode.h" -#include "tcpsocket.h" -#include -#include -#include -#include - -using namespace std; - -Appl& A = Appl::Instance(true); - -void define_args() -{ - A.setVersion("ebusctl is part of """PACKAGE_STRING""); - - A.addText(" 'help' show server commands\n\n" - " 'feed' sends a dump file to a local serial device (pts)\n" - " (hint: socat -d -d pty,raw,echo=0 pty,raw,echo=0)\n\n" - "Options:\n"); - - A.addOption("device", "d", OptVal("/dev/ttyUSB60"), dt_string, ot_mandatory, - "virtual serial device (/dev/ttyUSB60)"); - - A.addOption("file", "f", OptVal("/tmp/ebus_dump.bin"),dt_string, ot_mandatory, - "dump file name (/tmp/ebus_dump.bin)"); - - A.addOption("time", "t", OptVal(10000), dt_long, ot_mandatory, - "delay between 2 bytes in 'us' (10000)\n"); - - A.addOption("server", "s", OptVal("localhost"), dt_string, ot_mandatory, - "name or ip (localhost)"); - - A.addOption("port", "p", OptVal(8888), dt_int, ot_mandatory, - "port (8888)\n"); -} - -int main(int argc, char* argv[]) -{ - // define arguments and application variables - define_args(); - - // parse arguments - A.parseArgs(argc, argv); - - if (strcasecmp(A.getArg(0).c_str(), "feed") == 0) { - string dev(A.getOptVal("device")); - Port port(dev, true); - - port.open(); - if(port.isOpen() == true) { - cout << "openPort successful." << endl; - - fstream file(A.getOptVal("file"), ios::in | ios::binary); - - if (file.is_open() == true) { - - while (file.eof() == false) { - unsigned char byte = file.get(); - cout << hex << setw(2) << setfill('0') - << static_cast(byte) << endl; - - port.send(&byte, 1); - usleep(A.getOptVal("time")); - } - - file.close(); - } - else - cout << "error opening file " << A.getOptVal("file") << endl; - - port.close(); - if(port.isOpen() == false) - cout << "closePort successful." << endl; - } else - cout << "error opening device " << A.getOptVal("device") << endl; - } - else { - - TCPClient* client = new TCPClient(); - TCPSocket* socket = client->connect(A.getOptVal("server"), A.getOptVal("port")); - - if (socket != NULL) { - // build message - string message(A.getArg(0)); - for (int i = 1; i < A.numArgs(); i++) { - message += " "; - message += A.getArg(i); - } - - socket->send(message.c_str(), message.size()); - - char data[1024]; - size_t datalen; - - datalen = socket->recv(data, sizeof(data)-1); - data[datalen] = '\0'; - - cout << data; - - delete socket; - } - else - cout << "error connecting to " << A.getOptVal("server") - << ":" << A.getOptVal("port") << endl; - - delete client; - } - - return 0; -} - diff --git a/src/lib/utils/appl.cpp b/src/lib/utils/appl.cpp index f56d8c1a..c5fad1f7 100644 --- a/src/lib/utils/appl.cpp +++ b/src/lib/utils/appl.cpp @@ -24,9 +24,9 @@ using namespace std; -Appl& Appl::Instance(const bool command) +Appl& Appl::Instance(const bool command, const bool argument) { - static Appl instance(command); + static Appl instance(command, argument); return instance; } @@ -114,10 +114,13 @@ void Appl::parseArgs(int argc, char* argv[]) i++; continue; } - m_arguments.push_back(_argv[i]); + if (m_command.size() == 0) + m_command = _argv[i]; + else + m_arguments.push_back(_argv[i]); } - if (m_arguments.size() == 0) { + if (m_command.size() == 0) { cerr << endl << "command needed" << endl; printHelp(); } @@ -130,7 +133,7 @@ bool Appl::checkOption(const string& option, const string& value) if (strcmp(option.c_str(), "settings") == 0) printSettings(); - if (strcmp(option.c_str(), "v") == 0 || strcmp(option.c_str(), "version") == 0) + if (strcmp(option.c_str(), "version") == 0) printVersion(); if (strcmp(option.c_str(), "h") == 0 || strcmp(option.c_str(), "help") == 0) @@ -194,7 +197,10 @@ void Appl::printHelp() << m_argv[0].substr(m_argv[0].find_last_of("/\\") + 1) << " [OPTIONS...]" ; if (m_needCommand == true) - cerr << " COMMAND {ARGS...}" << endl << endl; + if (m_withArgument == true) + cerr << " COMMAND {ARGS...}" << endl << endl; + else + cerr << " COMMAND" << endl << endl; else cerr << endl << endl; @@ -210,7 +216,7 @@ void Appl::printHelp() } } - cerr << " | --settings\n-v | --version\n-h | --help" << endl << endl; + cerr << endl << " | --settings\n | --version\n-h | --help" << endl << endl; exit(EXIT_SUCCESS); } diff --git a/src/lib/utils/appl.h b/src/lib/utils/appl.h index 8e988deb..0a2a01c9 100644 --- a/src/lib/utils/appl.h +++ b/src/lib/utils/appl.h @@ -117,9 +117,10 @@ public: /** * @brief create an instance and return the reference. * @param command is true if an command is needed. + * @param argument is true if command could have an argument. * @return the reference to instance. */ - static Appl& Instance(const bool command=false); + static Appl& Instance(const bool command=false, const bool argument=false); /** * @brief destructor. @@ -176,24 +177,50 @@ public: int numArgs() const { return m_arguments.size(); } /** - * @brief returns the string of an interested argument (0 = command). + * @brief returns the string of given command. + * @return the command string. + */ + string getCommand() const { return m_command; } + + /** + * @brief returns the string of an interested argument. * @param num number of interested argument. - * @return string value. + * @return the argument string. */ string getArg(const int num) const { return m_arguments[num]; } private: /** private constructor - singleton pattern */ - Appl(const bool command) : m_needCommand(command) {} + /** + * @brief private construtor. + * @param command is true if an command is needed. + * @param argument is true if command could have an argument. + */ + Appl(const bool command, const bool argument) + : m_needCommand(command), m_withArgument(argument) {} + + /** + * @brief private copy construtor. + * @param reference to an instance. + */ Appl(const Appl&); + + /** + * @brief private = operator. + * @param reference to an instance. + */ Appl& operator=(const Appl&); /** application options */ vector m_opts; + + /** application options iterator */ vector::const_iterator o_it; /** map option - value */ map m_optvals; + + /** map option - value iterator */ map::iterator ov_it; /** given arguments */ @@ -205,7 +232,13 @@ private: /** true if the application need a command */ bool m_needCommand; - /** arguments (argument 0 = command) string */ + /** true if the needed command could have an argument */ + bool m_withArgument; + + /** command (argument 0 = command) string */ + string m_command; + + /** arguments (argument >= 1) string */ vector m_arguments; /** diff --git a/src/ebusctl/Makefile.am b/src/tools/Makefile.am similarity index 63% rename from src/ebusctl/Makefile.am rename to src/tools/Makefile.am index 5b5088ce..5c0a025a 100644 --- a/src/ebusctl/Makefile.am +++ b/src/tools/Makefile.am @@ -4,13 +4,19 @@ AM_CXXFLAGS = -fpic \ -I$(top_srcdir)/src/lib/utils \ -I$(top_srcdir)/src/lib/ebus -bin_PROGRAMS = ebusctl +bin_PROGRAMS = ebusctl \ + ebusfeed ebusctl_SOURCES = ebusctl.cpp ebusctl_LDADD = $(top_srcdir)/src/lib/utils/libutils.a \ $(top_srcdir)/src/lib/ebus/libebus.a +ebusfeed_SOURCES = ebusfeed.cpp + +ebusfeed_LDADD = $(top_srcdir)/src/lib/utils/libutils.a \ + $(top_srcdir)/src/lib/ebus/libebus.a + distclean-local: -rm -f Makefile.in -rm -rf .libs diff --git a/src/tools/ebusctl.cpp b/src/tools/ebusctl.cpp new file mode 100644 index 00000000..47ea5d06 --- /dev/null +++ b/src/tools/ebusctl.cpp @@ -0,0 +1,92 @@ +/* + * Copyright (C) Roland Jax 2012-2014 + * + * This file is part of ebusd. + * + * ebusd is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * ebusd is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with ebusd. If not, see http://www.gnu.org/licenses/. + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include "appl.h" +#include "port.h" +#include "decode.h" +#include "tcpsocket.h" +#include +#include +#include +#include + +using namespace std; + +Appl& A = Appl::Instance(true, true); + +void define_args() +{ + A.setVersion("ebusctl is part of """PACKAGE_STRING""); + + A.addText(" 'ebusctl' is a tcp socket client for ebusd.\n\n" + "Command: 'help' show available ebusd commands.\n\n" + "Options:\n"); + + A.addOption("server", "s", OptVal("localhost"), dt_string, ot_mandatory, + "name or ip (localhost)"); + + A.addOption("port", "p", OptVal(8888), dt_int, ot_mandatory, + "port (8888)"); + +} + +int main(int argc, char* argv[]) +{ + // define arguments and application variables + define_args(); + + // parse arguments + A.parseArgs(argc, argv); + + TCPClient* client = new TCPClient(); + TCPSocket* socket = client->connect(A.getOptVal("server"), A.getOptVal("port")); + + if (socket != NULL) { + // build message + string message(A.getCommand()); + for (int i = 0; i < A.numArgs(); i++) { + message += " "; + message += A.getArg(i); + } + + socket->send(message.c_str(), message.size()); + + char data[1024]; + size_t datalen; + + datalen = socket->recv(data, sizeof(data)-1); + data[datalen] = '\0'; + + cout << data; + + delete socket; + } + else + cout << "error connecting to " << A.getOptVal("server") + << ":" << A.getOptVal("port") << endl; + + delete client; + + return 0; +} + diff --git a/src/tools/ebusfeed.cpp b/src/tools/ebusfeed.cpp new file mode 100644 index 00000000..009d8980 --- /dev/null +++ b/src/tools/ebusfeed.cpp @@ -0,0 +1,101 @@ +/* + * Copyright (C) Roland Jax 2012-2014 + * + * This file is part of ebusd. + * + * ebusd is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * ebusd is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with ebusd. If not, see http://www.gnu.org/licenses/. + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include "appl.h" +#include "port.h" +#include "decode.h" +#include "tcpsocket.h" +#include +#include +#include +#include + +using namespace std; + +Appl& A = Appl::Instance(true); + +void define_args() +{ + A.setVersion("ebusfeed is part of """PACKAGE_STRING""); + + A.addText(" 'ebusfeed' sends a raw dump file to a local serial device (pts)\n\n" + " Usage: 1. 'socat -d -d pty,raw,echo=0 pty,raw,echo=0'\n" + " 2. create symbol links to appropriate devices\n" + " for example: 'ln -s /dev/pts/2 /dev/ttyUSB60'\n" + " 'ln -s /dev/pts/3 /dev/ttyUSB20'\n" + " 3. start ebusd: 'ebusd -f -d /dev/ttyUSB20'\n" + " 4. start ebusfeed: 'ebusfeed /path/to/raw_file'\n\n" + "Command: '/path/to/raw_file'\n\n" + "Options:\n"); + + A.addOption("device", "d", OptVal("/dev/ttyUSB60"), dt_string, ot_mandatory, + "virtual serial device (/dev/ttyUSB60)"); + + A.addOption("time", "t", OptVal(10000), dt_long, ot_mandatory, + "delay between 2 bytes in 'us' (10000)"); + +} + +int main(int argc, char* argv[]) +{ + // define arguments and application variables + define_args(); + + // parse arguments + A.parseArgs(argc, argv); + + string dev(A.getOptVal("device")); + Port port(dev, true); + + port.open(); + if(port.isOpen() == true) { + cout << "openPort successful." << endl; + + fstream file(A.getCommand().c_str(), ios::in | ios::binary); + + if (file.is_open() == true) { + + while (file.eof() == false) { + unsigned char byte = file.get(); + cout << hex << setw(2) << setfill('0') + << static_cast(byte) << endl; + + port.send(&byte, 1); + usleep(A.getOptVal("time")); + } + + file.close(); + } + else + cout << "error opening file " << A.getOptVal("file") << endl; + + port.close(); + if(port.isOpen() == false) + cout << "closePort successful." << endl; + } else + cout << "error opening device " << A.getOptVal("device") << endl; + + + return 0; +} +