command 'feed' moved from ebusctl into ebusfeed;tools directory created.

This commit is contained in:
Roland Jax
2014-11-24 15:52:31 +01:00
parent b07a6930fd
commit 972277d68d
9 changed files with 255 additions and 152 deletions
+2 -1
View File
@@ -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
+1 -1
View File
@@ -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
+1 -1
View File
@@ -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";
-136
View File
@@ -1,136 +0,0 @@
/*
* Copyright (C) Roland Jax 2012-2014 <ebusd@liwest.at>
*
* 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 <config.h>
#endif
#include "appl.h"
#include "port.h"
#include "decode.h"
#include "tcpsocket.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <unistd.h>
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<const char*>("device"));
Port port(dev, true);
port.open();
if(port.isOpen() == true) {
cout << "openPort successful." << endl;
fstream file(A.getOptVal<const char*>("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<unsigned>(byte) << endl;
port.send(&byte, 1);
usleep(A.getOptVal<long>("time"));
}
file.close();
}
else
cout << "error opening file " << A.getOptVal<const char*>("file") << endl;
port.close();
if(port.isOpen() == false)
cout << "closePort successful." << endl;
} else
cout << "error opening device " << A.getOptVal<const char*>("device") << endl;
}
else {
TCPClient* client = new TCPClient();
TCPSocket* socket = client->connect(A.getOptVal<const char*>("server"), A.getOptVal<int>("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<const char*>("server")
<< ":" << A.getOptVal<int>("port") << endl;
delete client;
}
return 0;
}
+13 -7
View File
@@ -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);
}
+38 -5
View File
@@ -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<opt_t> m_opts;
/** application options iterator */
vector<opt_t>::const_iterator o_it;
/** map option - value */
map<const char*, OptVal> m_optvals;
/** map option - value iterator */
map<const char*, OptVal>::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<string> m_arguments;
/**
@@ -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
+92
View File
@@ -0,0 +1,92 @@
/*
* Copyright (C) Roland Jax 2012-2014 <ebusd@liwest.at>
*
* 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 <config.h>
#endif
#include "appl.h"
#include "port.h"
#include "decode.h"
#include "tcpsocket.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <unistd.h>
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<const char*>("server"), A.getOptVal<int>("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<const char*>("server")
<< ":" << A.getOptVal<int>("port") << endl;
delete client;
return 0;
}
+101
View File
@@ -0,0 +1,101 @@
/*
* Copyright (C) Roland Jax 2012-2014 <ebusd@liwest.at>
*
* 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 <config.h>
#endif
#include "appl.h"
#include "port.h"
#include "decode.h"
#include "tcpsocket.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <unistd.h>
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<const char*>("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<unsigned>(byte) << endl;
port.send(&byte, 1);
usleep(A.getOptVal<long>("time"));
}
file.close();
}
else
cout << "error opening file " << A.getOptVal<const char*>("file") << endl;
port.close();
if(port.isOpen() == false)
cout << "closePort successful." << endl;
} else
cout << "error opening device " << A.getOptVal<const char*>("device") << endl;
return 0;
}