initial commit
This commit is contained in:
@@ -0,0 +1,154 @@
|
||||
/*
|
||||
* Copyright (C) Roland Jax 2014 <roland.jax@liwest.at>
|
||||
*
|
||||
* This file is part of ebus-daemon.
|
||||
*
|
||||
* ebus-daemon 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.
|
||||
*
|
||||
* ebus-daemon 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 ebus-daemon. If not, see http://www.gnu.org/licenses/.
|
||||
*/
|
||||
|
||||
#include "baseloop.h"
|
||||
#include "logger.h"
|
||||
#include "appl.h"
|
||||
#include "network.h"
|
||||
#include <algorithm>
|
||||
#include <sstream>
|
||||
#include <unistd.h>
|
||||
|
||||
extern LogInstance& L;
|
||||
extern Appl& A;
|
||||
|
||||
void BaseLoop::start()
|
||||
{
|
||||
for (;;) {
|
||||
// recv new message from client
|
||||
Message* message = m_queue.remove();
|
||||
std::string data = message->getData();
|
||||
|
||||
data.erase(std::remove(data.begin(), data.end(), '\r'), data.end());
|
||||
data.erase(std::remove(data.begin(), data.end(), '\n'), data.end());
|
||||
|
||||
L.log(bas, event, ">>> %s", data.c_str());
|
||||
|
||||
// decode message
|
||||
std::string result(decodeMessage(data));
|
||||
|
||||
L.log(bas, event, "<<< %s", result.c_str());
|
||||
|
||||
// send result to client
|
||||
result += '\n';
|
||||
Connection* connection = static_cast<Connection*>(message->getSource());
|
||||
connection->addResult(Message(result.c_str()));
|
||||
|
||||
delete message;
|
||||
}
|
||||
}
|
||||
|
||||
std::string BaseLoop::decodeMessage(const std::string& data)
|
||||
{
|
||||
std::ostringstream result;
|
||||
std::string value;
|
||||
int index;
|
||||
BusCommand* busCommand;
|
||||
|
||||
// prepare data
|
||||
std::string token;
|
||||
std::istringstream stream(data);
|
||||
std::vector<std::string> cmd;
|
||||
|
||||
// split stream
|
||||
while (std::getline(stream, token, ' ') != 0)
|
||||
cmd.push_back(token);
|
||||
|
||||
if (cmd.size() == 0)
|
||||
return "command missing";
|
||||
|
||||
switch (getCase(cmd[0])) {
|
||||
case notfound:
|
||||
result << "command not found";
|
||||
break;
|
||||
|
||||
case get:
|
||||
case set:
|
||||
if (cmd.size() < 3) {
|
||||
result << "format: type class cmd [sub]";
|
||||
break;
|
||||
}
|
||||
|
||||
index = m_commands->findCommand(data);
|
||||
|
||||
if (index >= 0) {
|
||||
|
||||
std::string type = m_commands->getType(index);
|
||||
std::string cmd(A.getParam<const char*>("p_address"));
|
||||
cmd += m_commands->getCommand(index);
|
||||
std::transform(cmd.begin(), cmd.end(), cmd.begin(), tolower);
|
||||
|
||||
L.log(bas, trace, " type: %s msg: %s", type.c_str(), cmd.c_str());
|
||||
// send BusCommand
|
||||
m_ebusloop->addBusCommand(new BusCommand(type, cmd));
|
||||
busCommand = m_ebusloop->getBusCommand();
|
||||
|
||||
// decode BusCommand
|
||||
result << busCommand->getResult().c_str();
|
||||
delete busCommand;
|
||||
|
||||
} else {
|
||||
result << "ebus command not found";
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case cyc:
|
||||
if (cmd.size() < 3) {
|
||||
result << "format: type class cmd [sub]";
|
||||
break;
|
||||
}
|
||||
|
||||
index = m_commands->findCommand(data);
|
||||
|
||||
if (index >= 0) {
|
||||
value = m_cycdata->getData(index);
|
||||
if (value != "") {
|
||||
// decode CYC Data
|
||||
result << value.c_str();
|
||||
} else {
|
||||
result << "no data stored";
|
||||
}
|
||||
} else {
|
||||
result << "ebus command not found";
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case dump:
|
||||
if (cmd[1] == "on") m_ebusloop->dump(true);
|
||||
if (cmd[1] == "off") m_ebusloop->dump(false);
|
||||
result << "done";
|
||||
break;
|
||||
|
||||
case log:
|
||||
if (cmd[1] == "error") L.getSink(0)->setLevel(error);
|
||||
if (cmd[1] == "event") L.getSink(0)->setLevel(event);
|
||||
if (cmd[1] == "trace") L.getSink(0)->setLevel(trace);
|
||||
if (cmd[1] == "debug") L.getSink(0)->setLevel(debug);
|
||||
result << "done";
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return result.str();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
/*
|
||||
* Copyright (C) Roland Jax 2014 <roland.jax@liwest.at>
|
||||
*
|
||||
* This file is part of ebus-daemon.
|
||||
*
|
||||
* ebus-daemon 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.
|
||||
*
|
||||
* ebus-daemon 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 ebus-daemon. If not, see http://www.gnu.org/licenses/.
|
||||
*/
|
||||
|
||||
#ifndef BASELOOP_H_
|
||||
#define BASELOOP_H_
|
||||
|
||||
#include "libebus.h"
|
||||
#include "ebusloop.h"
|
||||
#include "cycdata.h"
|
||||
#include "wqueue.h"
|
||||
#include <string>
|
||||
|
||||
using namespace libebus;
|
||||
|
||||
class Connection;
|
||||
|
||||
class Message
|
||||
{
|
||||
|
||||
public:
|
||||
Message(const std::string data, void* source = NULL) : m_data(data), m_source(source) {}
|
||||
Message(const Message& src) : m_data(src.m_data), m_source(src.m_source) {}
|
||||
|
||||
std::string getData() const { return m_data.c_str(); }
|
||||
void* getSource() const { return m_source; }
|
||||
|
||||
private:
|
||||
std::string m_data;
|
||||
void* m_source;
|
||||
|
||||
};
|
||||
|
||||
class BaseLoop
|
||||
{
|
||||
|
||||
public:
|
||||
BaseLoop(EBusLoop* ebusloop, CYCData* cycdata, Commands* commands)
|
||||
: m_ebusloop(ebusloop), m_cycdata(cycdata), m_commands(commands) {}
|
||||
|
||||
void start();
|
||||
|
||||
WQueue<Message*>* getQueue() { return &m_queue; }
|
||||
void addMessage(Message* message) { m_queue.add(message); }
|
||||
|
||||
private:
|
||||
EBusLoop* m_ebusloop;
|
||||
CYCData* m_cycdata;
|
||||
Commands* m_commands;
|
||||
WQueue<Message*> m_queue;
|
||||
|
||||
enum ClientCommand {
|
||||
get, // get ebus data
|
||||
set, // set ebus value
|
||||
cyc, // fetch cycle data
|
||||
dump, // change dump state
|
||||
log, // change log level
|
||||
|
||||
notfound
|
||||
};
|
||||
|
||||
ClientCommand getCase(const std::string& item)
|
||||
{
|
||||
if (item == "get") return get;
|
||||
if (item == "set") return set;
|
||||
if (item == "cyc") return cyc;
|
||||
if (item == "dump") return dump;
|
||||
if (item == "log") return log;
|
||||
|
||||
return notfound;
|
||||
}
|
||||
|
||||
std::string decodeMessage(const std::string& data);
|
||||
|
||||
};
|
||||
|
||||
#endif // BASELOOP_H_
|
||||
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
* Copyright (C) Roland Jax 2014 <roland.jax@liwest.at>
|
||||
*
|
||||
* This file is part of ebus-daemon.
|
||||
*
|
||||
* ebus-daemon 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.
|
||||
*
|
||||
* ebus-daemon 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 ebus-daemon. If not, see http://www.gnu.org/licenses/.
|
||||
*/
|
||||
|
||||
#include "cycdata.h"
|
||||
#include "logger.h"
|
||||
|
||||
extern LogInstance& L;
|
||||
|
||||
CYCData::~CYCData()
|
||||
{
|
||||
for (mapCI_t iter = m_cycDB.begin(); iter != m_cycDB.end(); ++iter)
|
||||
delete iter->second;
|
||||
}
|
||||
|
||||
void* CYCData::run()
|
||||
{
|
||||
bool skipfirst = false;
|
||||
|
||||
for (;;) {
|
||||
std::string data = m_ebusloop->getData();
|
||||
|
||||
if (skipfirst == true) {
|
||||
L.log(cyc, trace, "%s", data.c_str());
|
||||
|
||||
int index = m_commands->findData(data);
|
||||
|
||||
if (index >= 0) {
|
||||
std::string tmp;
|
||||
tmp += (*m_commands)[index][0];
|
||||
tmp += " ";
|
||||
tmp += (*m_commands)[index][1];
|
||||
tmp += " ";
|
||||
tmp += (*m_commands)[index][2];
|
||||
L.log(cyc, trace, " [%d] %s", index, tmp.c_str());
|
||||
storeData(index, data);
|
||||
}
|
||||
}
|
||||
|
||||
skipfirst = true;
|
||||
|
||||
if (m_stop == true)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void CYCData::storeData(int index, std::string data)
|
||||
{
|
||||
mapCI_t iter = m_cycDB.find(index);
|
||||
|
||||
if (iter != m_cycDB.end()) {
|
||||
iter->second->setData(data);
|
||||
L.log(cyc, debug, " [%d] %s -> replaced", index, data.c_str());
|
||||
} else {
|
||||
Command* cmd = new Command(index, (*m_commands)[index], data);
|
||||
m_cycDB.insert(pair_t(index, cmd));
|
||||
L.log(cyc, debug, " [%d] %s -> inserted", index, data.c_str());
|
||||
}
|
||||
|
||||
L.log(cyc, debug, " cycDB entries: %d", m_cycDB.size());
|
||||
}
|
||||
|
||||
std::string CYCData::getData(int index)
|
||||
{
|
||||
mapCI_t iter = m_cycDB.find(index);
|
||||
if (iter != m_cycDB.end())
|
||||
return iter->second->getData();
|
||||
else
|
||||
return "";
|
||||
}
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright (C) Roland Jax 2014 <roland.jax@liwest.at>
|
||||
*
|
||||
* This file is part of ebus-daemon.
|
||||
*
|
||||
* ebus-daemon 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.
|
||||
*
|
||||
* ebus-daemon 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 ebus-daemon. If not, see http://www.gnu.org/licenses/.
|
||||
*/
|
||||
|
||||
#ifndef CYCDATA_H_
|
||||
#define CYCDATA_H_
|
||||
|
||||
#include "libebus.h"
|
||||
#include "ebusloop.h"
|
||||
#include "thread.h"
|
||||
#include <string>
|
||||
|
||||
using namespace libebus;
|
||||
|
||||
typedef std::map<int, Command*> map_t;
|
||||
typedef map_t::const_iterator mapCI_t;
|
||||
typedef std::pair<int, Command*> pair_t;
|
||||
|
||||
class CYCData : public Thread
|
||||
{
|
||||
|
||||
public:
|
||||
CYCData(EBusLoop* ebusloop, Commands* commands)
|
||||
: m_ebusloop(ebusloop), m_commands(commands), m_stop(false) {}
|
||||
~CYCData();
|
||||
|
||||
void* run();
|
||||
void stop() { m_stop = true; }
|
||||
|
||||
std::string getData(int index);
|
||||
|
||||
private:
|
||||
EBusLoop* m_ebusloop;
|
||||
Commands* m_commands;
|
||||
map_t m_cycDB;
|
||||
bool m_stop;
|
||||
|
||||
void storeData(int index, std::string data);
|
||||
};
|
||||
|
||||
#endif // CYCDATA_H_
|
||||
@@ -0,0 +1,110 @@
|
||||
/*
|
||||
* Copyright (C) Roland Jax 2014 <roland.jax@liwest.at>
|
||||
*
|
||||
* This file is part of ebus-daemon.
|
||||
*
|
||||
* ebus-daemon 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.
|
||||
*
|
||||
* ebus-daemon 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 ebus-daemon. If not, see http://www.gnu.org/licenses/.
|
||||
*/
|
||||
|
||||
#include "ebusloop.h"
|
||||
#include "logger.h"
|
||||
#include "appl.h"
|
||||
#include <iostream>
|
||||
|
||||
extern LogInstance& L;
|
||||
extern Appl& A;
|
||||
|
||||
EBusLoop::EBusLoop() : m_stop(false)
|
||||
{
|
||||
m_deviceName = A.getParam<const char*>("p_device");
|
||||
|
||||
m_bus = new Bus(m_deviceName,
|
||||
A.getParam<bool>("p_nodevicecheck"),
|
||||
A.getParam<const char*>("p_dumpfile"),
|
||||
A.getParam<long>("p_dumpsize"),
|
||||
A.getParam<bool>("p_dump"));
|
||||
|
||||
m_bus->connect();
|
||||
|
||||
if (m_bus->isConnected() == false)
|
||||
L.log(bus, error, "can't open %s", m_deviceName.c_str());
|
||||
}
|
||||
|
||||
EBusLoop::~EBusLoop()
|
||||
{
|
||||
m_bus->disconnect();
|
||||
|
||||
if (m_bus->isConnected() == true)
|
||||
L.log(bus, error, "error during disconnect.");
|
||||
|
||||
delete m_bus;
|
||||
}
|
||||
|
||||
void* EBusLoop::run()
|
||||
{
|
||||
int busResult;
|
||||
bool busCommandActive = false;
|
||||
|
||||
for (;;) {
|
||||
if (m_bus->isConnected() == true) {
|
||||
|
||||
// work on bus
|
||||
busResult = m_bus->proceed();
|
||||
|
||||
// new cyc message arrived
|
||||
if (busResult == 2) {
|
||||
std::string data = m_bus->getCycData();
|
||||
//~ L.log(bus, trace, "%s", data.c_str());
|
||||
m_cycBuffer.add(data);
|
||||
}
|
||||
|
||||
// add new bus command to send
|
||||
if (busResult == 4 && busCommandActive == false && m_sendBuffer.size() != 0) {
|
||||
BusCommand* busCommand = m_sendBuffer.remove();
|
||||
m_bus->addCommand(busCommand);
|
||||
busCommandActive = true;
|
||||
}
|
||||
|
||||
// send bus command
|
||||
if (busResult == 1 && busCommandActive == true) {
|
||||
L.log(bus, trace, " getBus success");
|
||||
m_bus->sendCommand();
|
||||
BusCommand* busCommand = m_bus->recvCommand();
|
||||
m_recvBuffer.add(busCommand);
|
||||
busCommandActive = false;
|
||||
}
|
||||
|
||||
if (busResult == 0)
|
||||
L.log(bus, trace, " getBus failure");
|
||||
|
||||
if (busResult == -1)
|
||||
L.log(bus, event, " getBus error");
|
||||
|
||||
} else {
|
||||
sleep(10);
|
||||
m_bus->connect();
|
||||
|
||||
if (m_bus->isConnected() == false)
|
||||
L.log(bus, error, "can't open %s", m_deviceName.c_str());
|
||||
}
|
||||
|
||||
if (m_stop == true) {
|
||||
m_bus->disconnect();
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright (C) Roland Jax 2014 <roland.jax@liwest.at>
|
||||
*
|
||||
* This file is part of ebus-daemon.
|
||||
*
|
||||
* ebus-daemon 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.
|
||||
*
|
||||
* ebus-daemon 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 ebus-daemon. If not, see http://www.gnu.org/licenses/.
|
||||
*/
|
||||
|
||||
#ifndef EBUSLOOP_H_
|
||||
#define EBUSLOOP_H_
|
||||
|
||||
#include "libebus.h"
|
||||
#include "wqueue.h"
|
||||
#include "thread.h"
|
||||
|
||||
using namespace libebus;
|
||||
|
||||
|
||||
class EBusLoop : public Thread
|
||||
{
|
||||
|
||||
public:
|
||||
EBusLoop();
|
||||
~EBusLoop();
|
||||
|
||||
void* run();
|
||||
void stop() { m_stop = true; }
|
||||
|
||||
std::string getData() { return m_cycBuffer.remove(); }
|
||||
|
||||
void addBusCommand(BusCommand* busCommand) { m_sendBuffer.add(busCommand); }
|
||||
BusCommand* getBusCommand() { return m_recvBuffer.remove(); }
|
||||
|
||||
void dump(const bool dumpState) { m_bus->setDumpState(dumpState); }
|
||||
|
||||
private:
|
||||
std::string m_deviceName;
|
||||
Bus* m_bus;
|
||||
WQueue<std::string> m_cycBuffer;
|
||||
bool m_stop;
|
||||
WQueue<BusCommand*> m_sendBuffer;
|
||||
WQueue<BusCommand*> m_recvBuffer;
|
||||
|
||||
};
|
||||
|
||||
#endif // EBUSLOOP_H_
|
||||
+222
@@ -0,0 +1,222 @@
|
||||
/*
|
||||
* Copyright (C) Roland Jax 2014 <roland.jax@liwest.at>
|
||||
*
|
||||
* This file is part of ebus-daemon.
|
||||
*
|
||||
* ebus-daemon 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.
|
||||
*
|
||||
* ebus-daemon 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 ebus-daemon. If not, see http://www.gnu.org/licenses/.
|
||||
*/
|
||||
#include "libebus.h"
|
||||
#include "logger.h"
|
||||
#include "daemon.h"
|
||||
#include "appl.h"
|
||||
#include "network.h"
|
||||
#include "ebusloop.h"
|
||||
#include "cycdata.h"
|
||||
#include "baseloop.h"
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <csignal>
|
||||
#include <cstring>
|
||||
#include <cstdio>
|
||||
#include <sstream>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
using namespace libebus;
|
||||
|
||||
Appl& A = Appl::Instance();
|
||||
Daemon& D = Daemon::Instance();
|
||||
LogInstance& L = LogInstance::Instance();
|
||||
|
||||
Network* network;
|
||||
Commands* commands;
|
||||
EBusLoop* ebusloop;
|
||||
CYCData* cycdata;
|
||||
|
||||
void define_args()
|
||||
{
|
||||
A.addItem("p_address", Appl::Param("FF"), "a", "address",
|
||||
"\tebus device address (FF)",
|
||||
Appl::type_string, Appl::opt_mandatory);
|
||||
|
||||
A.addItem("p_device", Appl::Param("/dev/ttyUSB0"), "d", "device",
|
||||
"\tebus device (serial or network) (/dev/ttyUSB0)",
|
||||
Appl::type_string, Appl::opt_mandatory);
|
||||
|
||||
A.addItem("p_nodevicecheck", Appl::Param(false), "n", "nodevicecheck",
|
||||
"disable valid ebus device test\n", Appl::type_bool, Appl::opt_none);
|
||||
|
||||
A.addItem("p_ebusconfdir", Appl::Param("contrib/csv/vaillant"), "e", "ebusconfdir",
|
||||
"directory for ebus configuration (contrib/csv/vaillant)\n",
|
||||
Appl::type_string, Appl::opt_mandatory);
|
||||
|
||||
A.addItem("p_foreground", Appl::Param(false), "f", "foreground",
|
||||
"run in foreground\n", Appl::type_bool, Appl::opt_none);
|
||||
|
||||
A.addItem("p_port", Appl::Param(8888), "p", "port",
|
||||
"\tlisten port (8888)", Appl::type_int, Appl::opt_mandatory);
|
||||
|
||||
A.addItem("p_localhost", Appl::Param(false), "", "localhost",
|
||||
"listen localhost only\n", Appl::type_bool, Appl::opt_none);
|
||||
|
||||
A.addItem("p_area", Appl::Param(all), "", "logarea",
|
||||
"\tlogging area (bas=1, net=2, bus=4, cyc=8, all=15)",
|
||||
Appl::type_int, Appl::opt_mandatory);
|
||||
|
||||
A.addItem("p_level", Appl::Param(trace), "", "loglevel",
|
||||
"\tlogging level (error=1, event=2, trace=3, debug=4)\n",
|
||||
Appl::type_int, Appl::opt_mandatory);
|
||||
|
||||
A.addItem("p_dump", Appl::Param(false), "D", "dump",
|
||||
"\tenable dump", Appl::type_bool, Appl::opt_none);
|
||||
|
||||
A.addItem("p_dumpfile", Appl::Param("/tmp/dump_ebusd.bin"), "", "dumpfile",
|
||||
"\tdump file name (/tmp/dump_ebusd.bin)", Appl::type_string, Appl::opt_mandatory);
|
||||
|
||||
A.addItem("p_dumpsize", Appl::Param(100), "", "dumpsize",
|
||||
"\tmax size for dump file in kB (100)\n", Appl::type_long, Appl::opt_mandatory);
|
||||
|
||||
A.addItem("p_help", Appl::Param(false), "h", "help",
|
||||
"\tprint this message", Appl::type_bool, Appl::opt_none);
|
||||
}
|
||||
|
||||
void shutdown()
|
||||
{
|
||||
// free Network
|
||||
if (network != NULL)
|
||||
delete network;
|
||||
|
||||
// free CYCData
|
||||
if (cycdata != NULL) {
|
||||
cycdata->stop();
|
||||
delete cycdata;
|
||||
}
|
||||
|
||||
// free EBusLoop
|
||||
if (ebusloop != NULL) {
|
||||
ebusloop->stop();
|
||||
ebusloop->join();
|
||||
delete ebusloop;
|
||||
}
|
||||
|
||||
// free Commands DB
|
||||
if (commands != NULL)
|
||||
delete commands;
|
||||
|
||||
// reset all signal handlers to default
|
||||
signal(SIGHUP, SIG_DFL);
|
||||
signal(SIGINT, SIG_DFL);
|
||||
signal(SIGTERM, SIG_DFL);
|
||||
|
||||
// delete Daemon pid file
|
||||
if (D.status() == true)
|
||||
D.stop();
|
||||
|
||||
// stop Logger
|
||||
L.log(bas, event, "ebusd stopped");
|
||||
L.stop();
|
||||
L.join();
|
||||
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
void signal_handler(int sig)
|
||||
{
|
||||
switch (sig) {
|
||||
case SIGHUP:
|
||||
L.log(bas, event, "SIGHUP received");
|
||||
break;
|
||||
case SIGINT:
|
||||
L.log(bas, event, "SIGINT received");
|
||||
shutdown();
|
||||
break;
|
||||
case SIGTERM:
|
||||
L.log(bas, event, "SIGTERM received");
|
||||
shutdown();
|
||||
break;
|
||||
default:
|
||||
L.log(bas, event, "undefined signal %s", strsignal(sig));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
// define Arguments and Application variables
|
||||
define_args();
|
||||
|
||||
// parse Arguments
|
||||
if (A.parse(argc, argv) == false) {
|
||||
A.printArgs();
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// print Help
|
||||
if (A.getParam<bool>("p_help") == true) {
|
||||
A.printArgs();
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
// make me Daemon
|
||||
if (A.getParam<bool>("p_foreground") == true) {
|
||||
L += new LogConsole(A.getParam<int>("p_area"),
|
||||
static_cast<const Level>(A.getParam<int>("p_level")), "logConsole");
|
||||
} else {
|
||||
D.run("/var/run/ebusd.pid");
|
||||
L += new LogFile(A.getParam<int>("p_area"),
|
||||
static_cast<const Level>(A.getParam<int>("p_level")), "logFile", "/var/log/ebusd.log");
|
||||
}
|
||||
|
||||
// trap Signals that we expect to receive
|
||||
signal(SIGHUP, signal_handler);
|
||||
signal(SIGINT, signal_handler);
|
||||
signal(SIGTERM, signal_handler);
|
||||
|
||||
// start Logger
|
||||
L.start("logInstance");
|
||||
L.log(bas, event, "ebusd started");
|
||||
|
||||
// print Daemon status
|
||||
if (D.status() == true)
|
||||
L.log(bas, event, "change to daemon");
|
||||
|
||||
// create Commands DB
|
||||
commands = ConfigCommands(A.getParam<const char*>("p_ebusconfdir"), CSV).getCommands();
|
||||
L.log(bas, event, "commands DB with %d entries created", commands->size());
|
||||
|
||||
// create EBusLoop
|
||||
ebusloop = new EBusLoop();
|
||||
ebusloop->start("ebusloop");
|
||||
|
||||
// create CYCData
|
||||
cycdata = new CYCData(ebusloop, commands);
|
||||
cycdata->start("cycdata");
|
||||
|
||||
// create Network
|
||||
network = new Network(A.getParam<bool>("p_localhost"));
|
||||
|
||||
// create BaseLoop
|
||||
BaseLoop baseloop(ebusloop, cycdata, commands);
|
||||
|
||||
// start Network
|
||||
network->addQueue(baseloop.getQueue());
|
||||
network->start("netListener");
|
||||
|
||||
// start Baseloop
|
||||
baseloop.start();
|
||||
|
||||
shutdown();
|
||||
}
|
||||
|
||||
+220
@@ -0,0 +1,220 @@
|
||||
/*
|
||||
* Copyright (C) Roland Jax 2014 <roland.jax@liwest.at>
|
||||
*
|
||||
* This file is part of ebus-daemon.
|
||||
*
|
||||
* ebus-daemon 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.
|
||||
*
|
||||
* ebus-daemon 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 ebus-daemon. If not, see http://www.gnu.org/licenses/.
|
||||
*/
|
||||
|
||||
#include "network.h"
|
||||
#include "logger.h"
|
||||
#include "appl.h"
|
||||
#include <sstream>
|
||||
#include <cstring>
|
||||
#include <sys/select.h>
|
||||
|
||||
extern LogInstance& L;
|
||||
extern Appl& A;
|
||||
|
||||
int Connection::m_count = -1;
|
||||
|
||||
void Connection::addResult(Message message)
|
||||
{
|
||||
Message* tmp = new Message(Message(message));
|
||||
m_result.add(tmp);
|
||||
}
|
||||
|
||||
void* Connection::run()
|
||||
{
|
||||
m_running = true;
|
||||
|
||||
int maxfd;
|
||||
fd_set checkfds;
|
||||
struct timeval timeout;
|
||||
|
||||
FD_ZERO(&checkfds);
|
||||
FD_SET(m_notify.notifyFD(), &checkfds);
|
||||
FD_SET(m_socket->getFD(), &checkfds);
|
||||
|
||||
(m_notify.notifyFD() > m_socket->getFD()) ?
|
||||
(maxfd = m_notify.notifyFD()) : (maxfd = m_socket->getFD());
|
||||
|
||||
for (;;) {
|
||||
fd_set readfds;
|
||||
int ret;
|
||||
|
||||
// set select timeout 10 secs
|
||||
timeout.tv_sec = 10;
|
||||
timeout.tv_usec = 0;
|
||||
|
||||
// set readfds to inital checkfds
|
||||
readfds = checkfds;
|
||||
|
||||
ret = select(maxfd + 1, &readfds, NULL, NULL, &timeout);
|
||||
if (ret == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// new data from notify
|
||||
if (FD_ISSET(m_notify.notifyFD(), &readfds))
|
||||
break;
|
||||
|
||||
// new data from socket
|
||||
if (FD_ISSET(m_socket->getFD(), &readfds)) {
|
||||
char data[256];
|
||||
size_t datalen;
|
||||
|
||||
if (m_socket->isValid() == true)
|
||||
datalen = m_socket->recv(data, sizeof(data)-1);
|
||||
else
|
||||
break;
|
||||
|
||||
// removed closed socket
|
||||
if (datalen <= 0 || strncasecmp(data, "quit", 4) == 0)
|
||||
break;
|
||||
|
||||
// send data
|
||||
data[datalen] = '\0';
|
||||
m_data->add(new Message(data, this));
|
||||
|
||||
// wait for result
|
||||
L.log(net, debug, "[%08x] wait for result", getID());
|
||||
Message* message = m_result.remove();
|
||||
|
||||
L.log(net, debug, "[%08x] result added", getID());
|
||||
std::string result(message->getData());
|
||||
|
||||
if (m_socket->isValid() == true)
|
||||
m_socket->send(result.c_str(), result.size());
|
||||
else
|
||||
break;
|
||||
|
||||
delete message;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
delete m_socket;
|
||||
m_running = false;
|
||||
L.log(net, event, "[%08x] connection closed - active connections: %d", getID(), m_count);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Network::Network(const bool localhost) : m_listening(false), m_running(false)
|
||||
{
|
||||
// Start Listener
|
||||
if (localhost == true)
|
||||
m_Listener = new TCPListener(A.getParam<int>("p_port"), "127.0.0.1");
|
||||
else
|
||||
m_Listener = new TCPListener(A.getParam<int>("p_port"), "0.0.0.0");
|
||||
|
||||
if (m_Listener && m_Listener->start() == 0)
|
||||
m_listening = true;
|
||||
|
||||
}
|
||||
|
||||
Network::~Network()
|
||||
{
|
||||
while (m_connections.empty() == false) {
|
||||
Connection* connection = m_connections.back();
|
||||
m_connections.pop_back();
|
||||
connection->stop();
|
||||
connection->join();
|
||||
delete connection;
|
||||
}
|
||||
|
||||
if (m_running == true)
|
||||
stop();
|
||||
|
||||
delete m_Listener;
|
||||
}
|
||||
|
||||
void* Network::run()
|
||||
{
|
||||
if (m_listening == false)
|
||||
return NULL;
|
||||
|
||||
m_running = true;
|
||||
|
||||
int maxfd;
|
||||
fd_set checkfds;
|
||||
struct timeval timeout;
|
||||
|
||||
FD_ZERO(&checkfds);
|
||||
FD_SET(m_notify.notifyFD(), &checkfds);
|
||||
FD_SET(m_Listener->getFD(), &checkfds);
|
||||
|
||||
(m_notify.notifyFD() > m_Listener->getFD()) ?
|
||||
(maxfd = m_notify.notifyFD()) : (maxfd = m_Listener->getFD());
|
||||
|
||||
for (;;) {
|
||||
fd_set readfds;
|
||||
int ret;
|
||||
|
||||
// set select timeout 1 secs
|
||||
timeout.tv_sec = 1;
|
||||
timeout.tv_usec = 0;
|
||||
|
||||
// set readfds to inital checkfds
|
||||
readfds = checkfds;
|
||||
|
||||
ret = select(maxfd + 1, &readfds, NULL, NULL, &timeout);
|
||||
if (ret == 0) {
|
||||
cleanConnections();
|
||||
continue;
|
||||
}
|
||||
|
||||
// new data from notify
|
||||
if (FD_ISSET(m_notify.notifyFD(), &readfds)) {
|
||||
m_running = false;
|
||||
break;
|
||||
}
|
||||
|
||||
// new data from socket
|
||||
if (FD_ISSET(m_Listener->getFD(), &readfds)) {
|
||||
TCPSocket* socket = m_Listener->newSocket();
|
||||
if (socket == NULL)
|
||||
continue;
|
||||
|
||||
Connection* connection = new Connection(socket, m_queue);
|
||||
if (connection == NULL)
|
||||
continue;
|
||||
|
||||
connection->start("netConnection");
|
||||
m_connections.push_back(connection);
|
||||
L.log(net, event, "[%08x] connection opened %s", connection->getID(), socket->getIP().c_str());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void Network::cleanConnections()
|
||||
{
|
||||
std::list<Connection*>::iterator c_it;
|
||||
for (c_it = m_connections.begin(); c_it != m_connections.end(); c_it++) {
|
||||
if ((*c_it)->isRunning() == false) {
|
||||
Connection* connection = *c_it;
|
||||
c_it = m_connections.erase(c_it);
|
||||
delete connection;
|
||||
L.log(net, debug, "dead connection removed - %d", m_connections.size());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* Copyright (C) Roland Jax 2014 <roland.jax@liwest.at>
|
||||
*
|
||||
* This file is part of ebus-daemon.
|
||||
*
|
||||
* ebus-daemon 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.
|
||||
*
|
||||
* ebus-daemon 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 ebus-daemon. If not, see http://www.gnu.org/licenses/.
|
||||
*/
|
||||
|
||||
#ifndef NETWORK_H_
|
||||
#define NETWORK_H_
|
||||
|
||||
#include "tcpsocket.h"
|
||||
#include "wqueue.h"
|
||||
#include "thread.h"
|
||||
#include "notify.h"
|
||||
#include "baseloop.h"
|
||||
#include <unistd.h>
|
||||
#include <list>
|
||||
|
||||
class Connection : public Thread
|
||||
{
|
||||
|
||||
public:
|
||||
Connection(TCPSocket* socket, WQueue<Message*>* data)
|
||||
: m_socket(socket), m_data(data), m_running(false) { m_count++; }
|
||||
|
||||
~Connection() { m_count--; }
|
||||
|
||||
void addResult(Message message);
|
||||
|
||||
void* run();
|
||||
void stop() const { m_notify.notify(); }
|
||||
bool isRunning() const { return m_running; }
|
||||
|
||||
pthread_t getID() { return this->self(); }
|
||||
int numConnections() const { return m_count; }
|
||||
|
||||
private:
|
||||
TCPSocket* m_socket;
|
||||
WQueue<Message*>* m_data;
|
||||
WQueue<Message*> m_result;
|
||||
Notify m_notify;
|
||||
bool m_running;
|
||||
|
||||
static int m_count;
|
||||
|
||||
};
|
||||
|
||||
class Network : public Thread
|
||||
{
|
||||
|
||||
public:
|
||||
Network(const bool localhost);
|
||||
~Network();
|
||||
|
||||
void addQueue(WQueue<Message*>* queue) { m_queue = queue; }
|
||||
|
||||
void* run();
|
||||
void stop() const { m_notify.notify(); usleep(100000); }
|
||||
|
||||
private:
|
||||
std::list<Connection*> m_connections;
|
||||
WQueue<Message*>* m_queue;
|
||||
TCPListener* m_Listener;
|
||||
Notify m_notify;
|
||||
bool m_listening;
|
||||
bool m_running;
|
||||
|
||||
void cleanConnections();
|
||||
|
||||
};
|
||||
|
||||
#endif // NETWORK_H_
|
||||
Reference in New Issue
Block a user