diff --git a/src/ebusd/datahandler.cpp b/src/ebusd/datahandler.cpp new file mode 100644 index 00000000..70052304 --- /dev/null +++ b/src/ebusd/datahandler.cpp @@ -0,0 +1,68 @@ +/* + * ebusd - daemon for communication with eBUS heating systems. + * Copyright (C) 2016-2017 John Baier + * + * This program 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. + * + * This program 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 this program. If not, see . + */ + +#include "datahandler.h" +#include +#ifdef HAVE_MQTT +# include "mqtthandler.h" +#endif + +using namespace std; + +/** the final @a argp_child structure. */ +static const struct argp_child g_last_argp_child = {NULL, 0, NULL, 0}; + +/** the list of @a argp_child structures. */ +static const struct argp_child* g_argp_children[ +#ifdef HAVE_MQTT + 1 +#endif + +1 +]; + +const struct argp_child* datahandler_getargs() +{ + size_t count = 0; +#ifdef HAVE_MQTT + g_argp_children[count++] = mqtthandler_getargs(); +#endif + if (count>0) { + g_argp_children[count] = &g_last_argp_child; + return g_argp_children[0]; + } + return NULL; +} + +bool datahandler_register(BusHandler* busHandler, vector& handlers) +{ + bool success = true; +#ifdef HAVE_MQTT + DataHandler* handler = mqtthandler_register(busHandler); + if (handler) { + handlers.push_back(handler); + } else { + success = false; + } +#endif + return success; +} + +void DataSink::notifyUpdate(Message* message) +{ + m_updatedMessages.push_back(message); +} diff --git a/src/ebusd/datahandler.h b/src/ebusd/datahandler.h new file mode 100644 index 00000000..d8d3b938 --- /dev/null +++ b/src/ebusd/datahandler.h @@ -0,0 +1,135 @@ +/* + * ebusd - daemon for communication with eBUS heating systems. + * Copyright (C) 2016-2017 John Baier + * + * This program 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. + * + * This program 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 this program. If not, see . + */ + +#ifndef DATAHANDLER_H_ +#define DATAHANDLER_H_ + +#include "bushandler.h" +#include "message.h" +#include +#ifdef HAVE_CONFIG_H +# include +#endif + +/** @file datahandler.h + * Classes and functions for implementing and registering generic data sinks + * and sources that allow listening to received data updates and sending on + * the bus. + */ + +using namespace std; + +class DataHandler; + +/** + * Helper function for getting the argp definition for all known @a DataHandler instances. + * @return a pointer to the argp_child structure, or NULL. + */ +const struct argp_child* datahandler_getargs(); + +/** + * Registration function that is called once during initialization. + * @param busHandler the @a BusHandler instance. + * @param handlers the @a vector to which new @a DataHandler instances shall be added. + * @return true if registration was successful. + */ +bool datahandler_register(BusHandler* busHandler, vector& handlers); + + +/** + * Base class for all kinds of data handlers. + */ +class DataHandler +{ +public: + /** + * Constructor. + */ + DataHandler() {} + + /** + * Destructor. + */ + virtual ~DataHandler() {} + + /** + * Called to start the @a DataHandler. + */ + virtual void start() = 0; + +}; + + +/** + * Base class for listening to data updates. + */ +class DataSink : virtual public DataHandler +{ +public: + + /** + * Constructor. + */ + DataSink() {} + + /** + * Destructor. + */ + virtual ~DataSink() {} + + /** + * Notify the sink of an updated @a Message. + * @param message the updated @a Message. + */ + virtual void notifyUpdate(Message* message); + +protected: + + /** a queue of updated @p Message instances. */ + deque m_updatedMessages; + +}; + + +/** + * Base class providing data to be sent on the bus. + */ +class DataSource : virtual public DataHandler +{ +public: + + /** + * Constructor. + * @param busHandler the @a BusHandler instance. + */ + DataSource(BusHandler* busHandler) + : m_busHandler(busHandler) {} + + /** + * Destructor. + */ + virtual ~DataSource() {} + +protected: + + /** the @a BusHandler instance. */ + BusHandler* m_busHandler; + +}; + +#endif // DATAHANDLER_H_ diff --git a/src/ebusd/main.cpp b/src/ebusd/main.cpp index b97ec3a1..af58902e 100644 --- a/src/ebusd/main.cpp +++ b/src/ebusd/main.cpp @@ -1,6 +1,6 @@ /* * ebusd - daemon for communication with eBUS heating systems. - * Copyright (C) 2014-2016 John Baier + * Copyright (C) 2014-2017 John Baier * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -17,7 +17,7 @@ */ #ifdef HAVE_CONFIG_H -#include +# include #endif #include "main.h" @@ -88,14 +88,14 @@ static struct options opt = { 8888, // port false, // localOnly 0, // httpPort - "/var/ebusd/html", // htmlPath + "/var/" PACKAGE "/html", // htmlPath PACKAGE_LOGFILE, // logFile false, // logRaw PACKAGE_LOGFILE, // logRawFile 100, // logRawSize false, // dump "/tmp/" PACKAGE "_dump.bin", // dumpFile - 100 // dumpSize + 100, // dumpSize }; /** the @a MessageMap instance, or NULL. */ @@ -381,7 +381,7 @@ error_t parse_opt(int key, char *arg, struct argp_state *state) case O_HTTPPT: // --httpport=0 opt->httpPort = (uint16_t)parseInt(arg, 10, 1, 65535, result); if (result != RESULT_OK) { - argp_error(state, "invalid port"); + argp_error(state, "invalid httpport"); return EINVAL; } break; @@ -420,7 +420,7 @@ error_t parse_opt(int key, char *arg, struct argp_state *state) break; case O_RAWFIL: // --lograwdatafile=/var/log/ebusd.log if (arg == NULL || arg[0] == 0 || strcmp("/", arg) == 0) { - argp_error(state, "invalid dumpfile"); + argp_error(state, "invalid lograwdatafile"); return EINVAL; } opt->logRawFile = arg; @@ -428,7 +428,7 @@ error_t parse_opt(int key, char *arg, struct argp_state *state) case O_RAWSIZ: // --lograwdatasize=100 opt->logRawSize = (unsigned int)parseInt(arg, 10, 1, 1000000, result); if (result != RESULT_OK) { - argp_error(state, "invalid dumpsize"); + argp_error(state, "invalid lograwdatasize"); return EINVAL; } break; @@ -916,7 +916,7 @@ result_t loadScanConfigFile(MessageMap* messages, unsigned char address, SymbolS continue; if (name.length()<3 || name.find_first_of('.')!=2) { // different from the scheme "ZZ." name = *it; - result = messages->readFromFile(name, opt.checkConfig, "", ident); + result = messages->readFromFile(name, opt.checkConfig); if (result==RESULT_OK) logNotice(lf_main, "read common config file %s", name.c_str()); else @@ -925,7 +925,7 @@ result_t loadScanConfigFile(MessageMap* messages, unsigned char address, SymbolS } } } - result = messages->readFromFile(best, opt.checkConfig); + result = messages->readFromFile(best, opt.checkConfig, "", ident); if (result!=RESULT_OK) { logError(lf_main, "error reading scan config file %s for ID \"%s\", SW%4.4d, HW%4.4d: %s", best.c_str(), ident.c_str(), sw, hw, getResultCode(result)); return result; @@ -945,7 +945,7 @@ result_t loadScanConfigFile(MessageMap* messages, unsigned char address, SymbolS */ int main(int argc, char* argv[]) { - struct argp argp = { argpoptions, parse_opt, NULL, argpdoc, NULL, NULL, NULL }; + struct argp argp = { argpoptions, parse_opt, NULL, argpdoc, datahandler_getargs(), NULL, NULL }; int arg_index = -1; setenv("ARGP_HELP_FMT", "no-dup-args-note", 0); if (argp_parse(&argp, argc, argv, ARGP_IN_ORDER, &arg_index, &opt) != 0) {