Merge branch 'master' of git://github.com/yuhu-/ebusd

Conflicts:
	src/ebusctl/ebusctl.cpp
	src/ebusd/Makefile.am
	src/ebusd/baseloop.cpp
	src/ebusd/baseloop.h
	src/ebusd/bushandler.cpp
	src/ebusd/bushandler.h
	src/lib/ebus/message.cpp
	src/lib/ebus/message.h
	src/lib/ebus/port.cpp
	src/lib/ebus/symbol.cpp
	src/lib/ebus/symbol.h
	src/lib/ebus/test/test_data.cpp
	src/lib/ebus/test/test_message.cpp
This commit is contained in:
john30
2014-12-07 15:19:45 +01:00
19 changed files with 433 additions and 592 deletions
-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 "data.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, false, NULL, false, "", 1);
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;
}
Regular → Executable
+2 -1
View File
@@ -16,7 +16,8 @@ ebusd_SOURCES = bushandler.cpp \
ebusd_LDADD = $(top_srcdir)/src/lib/utils/libutils.a \
$(top_srcdir)/src/lib/ebus/libebus.a \
-lpthread -lrt
-lpthread
-lrt
distclean-local:
-rm -f Makefile.in
+1 -1
View File
@@ -115,7 +115,7 @@ void Connection::run()
break;
// removed closed socket
if (datalen <= 0 || strcasecmp(data, "QUIT") == 0)
if (datalen <= 0 || strncasecmp(data, "QUIT", 4) == 0)
break;
// send data
+6 -7
View File
@@ -147,20 +147,19 @@ unsigned char Device::getByte()
result_t DeviceSerial::openDevice(const string deviceName, const bool noDeviceCheck)
{
m_noDeviceCheck = noDeviceCheck;
termios newSettings;
struct termios newSettings;
m_open = false;
// open file descriptor from serial device
// open file descriptor
m_fd = open(deviceName.c_str(), O_RDWR | O_NOCTTY);
if (m_fd < 0)
if (m_fd < 0 || isatty(m_fd) == 0)
return RESULT_ERR_NOTFOUND;
// save current settings of serial device
// save current settings
tcgetattr(m_fd, &m_oldSettings);
// create new settings
memset(&newSettings, '\0', sizeof(newSettings));
newSettings.c_cflag |= (B2400 | CS8 | CLOCAL | CREAD);
@@ -176,7 +175,7 @@ result_t DeviceSerial::openDevice(const string deviceName, const bool noDeviceCh
tcflush(m_fd, TCIFLUSH);
// activate new settings of serial device
tcsetattr(m_fd, TCSANOW, &newSettings);
tcsetattr(m_fd, TCSAFLUSH, &newSettings);
// set serial device into blocking mode
fcntl(m_fd, F_SETFL, fcntl(m_fd, F_GETFL) & ~O_NONBLOCK);
+3 -3
View File
@@ -110,13 +110,13 @@ public:
ssize_t sizeRecvBuffer() const { return m_recvBuffer.size(); }
protected:
/** if of file descriptor */
/** file descriptor from input device */
int m_fd;
/** state of device*/
/** true if device is opened */
bool m_open;
/** state of device check */
/** true if device check is disabled */
bool m_noDeviceCheck;
/** queue for received bytes */
+17 -19
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;
}
@@ -107,22 +107,17 @@ void Appl::parseArgs(int argc, char* argv[])
}
// check command
if (m_needCommand == true) {
for (int i = 1; i < argc; i++) {
for (int i = 1; i < argc; i++) {
if (_argv[i].rfind("-", 0) != string::npos) {
i++;
continue;
}
if (_argv[i].rfind("-", 0) != string::npos) {
i++;
continue;
}
if (m_command.size() == 0)
m_command = _argv[i];
else
m_arguments.push_back(_argv[i]);
}
if (m_arguments.size() == 0) {
cerr << endl << "command needed" << endl;
printHelp();
}
}
}
bool Appl::checkOption(const string& option, const string& value)
@@ -130,7 +125,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)
@@ -196,8 +191,11 @@ void Appl::printHelp()
cerr << endl << "Usage:" << endl << " "
<< m_argv[0].substr(m_argv[0].find_last_of("/\\") + 1) << " [OPTIONS...]" ;
if (m_needCommand == true)
cerr << " COMMAND {ARGS...}" << endl << endl;
if (m_withCommand == true)
if (m_withArgument == true)
cerr << " COMMAND {ARGS...}" << endl << endl;
else
cerr << " COMMAND" << endl << endl;
else
cerr << endl << endl;
@@ -213,7 +211,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);
}
+47 -9
View File
@@ -118,9 +118,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.
@@ -175,26 +176,57 @@ public:
* @return number of commands and arguments.
*/
int numArgs() const { return m_arguments.size(); }
/**
* @brief returns the string of an interested argument (0 = 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]; }
/**
* @brief returns the string of given command.
* @return the command string.
*/
string getCommand() const { return m_command; }
/**
* @brief returns the string of given command.
* @return the command string.
*/
bool missingCommand() const { return (m_command.size() == 0 ? true : false); }
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_withCommand(command), m_withArgument(argument) {}
/**
* @brief private copy construtor.
* @param reference to an instance.
*/
Appl(const Appl&);
/**
* @brief private = operator.
* @param reference to an instance.
* @return reference to 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 */
@@ -203,10 +235,16 @@ private:
/** application version string */
const char* m_version;
/** true if the application need a command */
bool m_needCommand;
/** true if the application could have a command */
bool m_withCommand;
/** arguments (argument 0 = command) string */
/** true if the command could have an argument */
bool m_withArgument;
/** command (argument 0 = command) string */
string m_command;
/** arguments (argument >= 1) string */
vector<string> m_arguments;
/**
+14 -1
View File
@@ -51,9 +51,22 @@ public:
bool status() { return m_status; }
private:
/** private constructor - singleton pattern */
/**
* @brief private construtor.
*/
Daemon() {}
/**
* @brief private copy construtor.
* @param reference to an instance.
*/
Daemon(const Daemon&);
/**
* @brief private = operator.
* @param reference to an instance.
* @return reference to instance.
*/
Daemon& operator=(const Daemon&);
/** status of process; true if we are a daemon */
+16 -1
View File
@@ -302,13 +302,28 @@ public:
virtual void stop();
private:
/** private constructor - singleton pattern */
/**
* @brief private construtor.
*/
Logger() {}
/**
* @brief private copy construtor.
* @param reference to an instance.
*/
Logger(const Logger&);
/**
* @brief private = operator.
* @param reference to an instance.
* @return reference to instance.
*/
Logger& operator=(const Logger&);
/** typedefs for a vector of type LogSink* */
typedef vector<LogSink*> sink_t;
/** typedefs for a vector of type LogSink* iterator */
typedef vector<LogSink*>::iterator sinkCI_t;
/** vector of available logging sinks */
+3 -1
View File
@@ -32,8 +32,10 @@ class TCPSocket
{
public:
/** grant access for friend classes */
/** grant access for friend class TCPClient */
friend class TCPClient;
/** grant access for friend class TCPServer */
friend class TCPServer;
/**
@@ -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
+195
View File
@@ -0,0 +1,195 @@
/*
* 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 "tcpsocket.h"
#include <iostream>
#include <cstdlib>
#include <sstream>
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)");
}
enum CommandType {
ct_open,
ct_exit,
ct_help,
ct_invalid
};
CommandType getCase(const string& item)
{
if (strcasecmp(item.c_str(), "OPEN") == 0) return ct_open;
if (strcasecmp(item.c_str(), "EXIT") == 0) return ct_exit;
if (strcasecmp(item.c_str(), "HELP") == 0) return ct_help;
return ct_invalid;
}
bool connect(const char* host, int port, bool once=true)
{
TCPClient* client = new TCPClient();
TCPSocket* socket = client->connect(host, port);
if (socket != NULL) {
do {
string message;
if (once == false) {
cout << host << ": ";
getline(cin, message);
}
else {
message = A.getCommand();
for (int i = 0; i < A.numArgs(); i++) {
message += " ";
message += A.getArg(i);
}
}
socket->send(message.c_str(), message.size());
if (strncasecmp(message.c_str(), "QUIT", 4) != 0 && strncasecmp(message.c_str(), "STOP", 4) != 0) {
char data[1024];
size_t datalen;
datalen = socket->recv(data, sizeof(data)-1);
data[datalen] = '\0';
cout << data;
}
else
break;
} while (once == false);
delete socket;
}
else
cout << "error connecting to " << host << ":" << port << endl;
delete client;
if (once == false)
return false;
return true;
}
int main(int argc, char* argv[])
{
// define arguments and application variables
define_args();
// parse arguments
A.parseArgs(argc, argv);
if (A.missingCommand() == true) {
cout << "interactive mode started." << endl;
bool running = true;
do {
string input, token;
vector<string> cmd;
cout << "$: ";
getline(cin, input);
// prepare input
istringstream stream(input);
while (getline(stream, token, ' ') != 0)
cmd.push_back(token);
if (cmd.size() == 0)
cout << "command missing" << endl;
switch (getCase(cmd[0])) {
case ct_invalid:
cout << "command not found" << endl;
break;
case ct_open:
{
bool ret = true;
cout << "connect to..." << endl;
if (cmd.size() == 1)
ret = connect(A.getOptVal<const char*>("server"), A.getOptVal<int>("port"), false);
else if (cmd.size() == 2)
ret = connect(cmd[1].c_str(), A.getOptVal<int>("port"), false);
else if (cmd.size() == 3)
ret = connect(cmd[1].c_str(), atoi(cmd[2].c_str()), false);
else
cout << "open [host [port]]" << endl;
running = ret;
}
break;
case ct_exit:
running = false;
break;
case ct_help:
cout << "commands:" << endl
<< " open - open connection to ebusd 'open [host [port]]'" << endl
<< " exit - exit ebusctl" << endl
<< " help - print this page" << endl;
break;
default:
break;
}
} while (running == true);
exit(EXIT_SUCCESS);
}
connect(A.getOptVal<const char*>("server"), A.getOptVal<int>("port"));
exit(EXIT_SUCCESS);
}
+104
View File
@@ -0,0 +1,104 @@
/*
* 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 <iostream>
#include <cstdlib>
#include <fstream>
#include <iomanip>
using namespace std;
Appl& A = Appl::Instance(true);
void define_args()
{
A.setVersion("ebusfeed is part of """PACKAGE_STRING"");
A.addText(" 'ebusfeed' sends a 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/ebus_dump.bin'\n\n"
"Command: '/path/to/ebus_dump.bin'\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);
if (A.missingCommand() == true) {
cout << "ebus dump file is required." << endl;
exit(EXIT_FAILURE);
}
string dev(A.getOptVal<const char*>("device"));
Port port(dev, true, false, NULL, false, "", 1);
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;
exit(EXIT_SUCCESS);
}