diff --git a/configure.ac b/configure.ac index 7e160ef5..f763c326 100644 --- a/configure.ac +++ b/configure.ac @@ -30,6 +30,10 @@ AC_CHECK_FUNC([pselect], [AC_DEFINE(HAVE_PSELECT, [1], [Define to 1 if pselect() AC_CHECK_FUNC([ppoll], [AC_DEFINE(HAVE_PPOLL, [1], [Define to 1 if ppoll() is available.])]) AC_ARG_ENABLE(coverage, AS_HELP_STRING([--enable-coverage], [enable code coverage tracking]), [CXXFLAGS+=" -coverage -O0"], []) +AC_ARG_WITH(contrib, AS_HELP_STRING([--without-contrib], [disable inclusion of contributed sources]), [], [with_contrib=yes]) +if test "x$with_contrib" != "xno"; then + AC_DEFINE_UNQUOTED(HAVE_CONTRIB, [1], [Define to 1 for inclusion of contributed sources.]) +fi AC_ARG_WITH(argp-lib, AS_HELP_STRING([--with-argp-lib=PATH], [path to argp libraries]), [LDFLAGS+="-L$with_argp_lib"]) AC_ARG_WITH(argp-include, AS_HELP_STRING([--with-argp-include=PATH], [path to argp includes]), [CXXFLAGS+="-I$with_argp_include"]) AC_CHECK_FUNC([argp_parse], [have_argp=yes], AC_CHECK_LIB([argp], [argp_parse], [have_argp=yes; LIBS="-largp $LIBS"], [have_argp=no])) @@ -84,12 +88,17 @@ fi AC_CONFIG_SRCDIR([src/ebusd/main.cpp]) AC_CONFIG_HEADERS([config.h]) AC_CONFIG_FILES([Makefile - docs/Makefile - src/lib/utils/Makefile - src/lib/ebus/Makefile - src/lib/ebus/test/Makefile - src/ebusd/Makefile - src/tools/Makefile]) + docs/Makefile + src/lib/utils/Makefile + src/lib/ebus/Makefile + src/lib/ebus/test/Makefile + src/ebusd/Makefile + src/tools/Makefile]) +AM_CONDITIONAL([CONTRIB], [test "x$with_contrib" != "xno"]) +AM_COND_IF([CONTRIB], [AC_CONFIG_FILES([ + src/lib/ebus/contrib/Makefile + src/lib/ebus/contrib/test/Makefile + ])]) AC_DEFINE_UNQUOTED(PACKAGE_PIDFILE, LOCALSTATEDIR "/run/" PACKAGE ".pid", [The path and name of the PID file.]) AC_DEFINE_UNQUOTED(PACKAGE_LOGFILE, LOCALSTATEDIR "/log/" PACKAGE ".log", [The path and name of the log file.]) @@ -103,9 +112,9 @@ if test -z "$HAVE_DOXYGEN"; then fi AM_CONDITIONAL([HAVE_DOXYGEN], [test -n "$HAVE_DOXYGEN"]) - AM_COND_IF([HAVE_DOXYGEN], [AC_CONFIG_FILES([docs/Doxyfile])]) +AM_COND_IF([HAVE_DOXYGEN], [AC_CONFIG_FILES([docs/Doxyfile])]) -AM_INIT_AUTOMAKE([1.11 -Wall -Werror foreign]) +AM_INIT_AUTOMAKE([1.11 -Wall -Werror foreign subdir-objects]) m4_ifdef([AM_PROG_AR], [AM_PROG_AR]) AC_PROG_RANLIB diff --git a/src/lib/ebus/Makefile.am b/src/lib/ebus/Makefile.am index c0eb2b11..9f15bfb6 100644 --- a/src/lib/ebus/Makefile.am +++ b/src/lib/ebus/Makefile.am @@ -17,5 +17,10 @@ libebus_a_SOURCES = result.cpp \ message.cpp \ message.h +if CONTRIB +SUBDIRS = contrib +libebus_a_LIBADD = contrib/*.o +endif + distclean-local: -rm -f Makefile.in diff --git a/src/lib/ebus/contrib/contrib.h b/src/lib/ebus/contrib/contrib.h new file mode 100644 index 00000000..6bf8c7ee --- /dev/null +++ b/src/lib/ebus/contrib/contrib.h @@ -0,0 +1,35 @@ +/* + * ebusd - daemon for communication with eBUS heating systems. + * Copyright (C) 2016 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 LIBEBUS_CONTRIB_H_ +#define LIBEBUS_CONTRIB_H_ + +/** @file contrib.h + * Contributed sources that may be excluded from regular builds. + * configure switch: --without-contrib + */ + +using namespace std; + +/** + * Registration function that is called once during initialization. + * @return true if registration was successful. + */ +bool libebus_contrib_register(); + +#endif // LIBEBUS_CONTRIB_H_ diff --git a/src/lib/ebus/contrib/tem.cpp b/src/lib/ebus/contrib/tem.cpp new file mode 100644 index 00000000..d384c9da --- /dev/null +++ b/src/lib/ebus/contrib/tem.cpp @@ -0,0 +1,138 @@ +/* + * ebusd - daemon for communication with eBUS heating systems. + * Copyright (C) 2016 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 . + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include "datatype.h" +#include "tem.h" +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +void contrib_tem_register() { + DataTypeList::getInstance()->add(new TemParamDataType("TEM_P")); +} + +result_t TemParamDataType::derive(int divisor, unsigned char bitCount, NumberDataType* &derived) +{ + if (divisor == 0) { + divisor = 1; + } + if (divisor == 1 && bitCount == 16) { + derived = this; + return RESULT_OK; + } + return RESULT_ERR_INVALID_ARG; +} + +result_t TemParamDataType::readSymbols(SymbolString& input, const bool isMaster, + const unsigned char offset, const unsigned char length, + ostringstream& output, OutputFormat outputFormat) +{ + unsigned int value = 0; + + result_t result = readRawValue(input, offset, length, value); + if (result != RESULT_OK) { + return result; + } + + if (value == m_replacement) { + if (outputFormat & OF_JSON) { + output << "null"; + } else { + output << NULL_VALUE; + } + return RESULT_OK; + } + int grp = 0, num = 0; + if (isMaster) { + grp = (value & 0x1f); // grp in bits 0...5 + num = ((value >> 8) & 0x7f); // num in bits 8...13 + } else { + grp = ((value >> 7) & 0x1f); // grp in bits 7...11 + num = (value & 0x7f); // num in bits 0...6 + } + if (outputFormat & OF_JSON) { + output << '"'; + } + output << setfill('0') << setw(2) << dec << static_cast(grp) << '-' << setw(3) << static_cast(num); + if (outputFormat & OF_JSON) { + output << '"'; + } + output << setfill(' ') << setw(0); // reset + return RESULT_OK; +} + +result_t TemParamDataType::writeSymbols(istringstream& input, + const unsigned char offset, const unsigned char length, + SymbolString& output, const bool isMaster, unsigned char* usedLength) +{ + unsigned int value; + int grp, num; + string token; + + const char* str = input.str().c_str(); + if (strcasecmp(str, NULL_VALUE) == 0) { + value = m_replacement; // replacement value + } else { + if (input.eof() || !getline(input, token, '-')) { + return RESULT_ERR_EOF; // incomplete + } + str = token.c_str(); + if (str == NULL || *str == 0) { + return RESULT_ERR_EOF; // input too short + } + char* strEnd = NULL; + grp = (unsigned int)strtoul(str, &strEnd, 10); + if (strEnd == NULL || strEnd == str || *strEnd != 0) { + return RESULT_ERR_INVALID_NUM; // invalid value + } + if (input.eof() || !getline(input, token, '-')) { + return RESULT_ERR_EOF; // incomplete + } + str = token.c_str(); + if (str == NULL || *str == 0) { + return RESULT_ERR_EOF; // input too short + } + strEnd = NULL; + num = (unsigned int)strtoul(str, &strEnd, 10); + if (strEnd == NULL || strEnd == str || *strEnd != 0) { + return RESULT_ERR_INVALID_NUM; // invalid value + } + if (grp < 0 || grp > 0x1f || num < 0 || num > 0x7f) { + return RESULT_ERR_OUT_OF_RANGE; // value out of range + } + if (isMaster) { + value = grp | (num<<8); // grp in bits 0...5, num in bits 8...13 + } else { + value = (grp<<7) | num; // grp in bits 7...11, num in bits 0...6 + } + } + if (value < getMinValue() || value > getMaxValue()) { + return RESULT_ERR_OUT_OF_RANGE; // value out of range + } + return writeRawValue(value, offset, length, output, usedLength); +} diff --git a/src/lib/ebus/contrib/tem.h b/src/lib/ebus/contrib/tem.h new file mode 100644 index 00000000..789c7e4b --- /dev/null +++ b/src/lib/ebus/contrib/tem.h @@ -0,0 +1,74 @@ +/* + * ebusd - daemon for communication with eBUS heating systems. + * Copyright (C) 2016 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 LIBEBUS_CONTRIB_TEM_H_ +#define LIBEBUS_CONTRIB_TEM_H_ + +#include "symbol.h" +#include "result.h" +#include "datatype.h" +#include +#include +#include +#include +#include +#include +#include + +/** @file tem.h + * Contributed data types for TEM devices not part of regular releases. + */ + +using namespace std; + +/** + * A special variant of @a NumberDataType for TEM/Dungs ParamID in master/slave + * data. + */ +class TemParamDataType : public NumberDataType +{ +public: + + /** + * Constructs a new instance. + * @param id the type identifier. + */ + TemParamDataType(const string id) + : NumberDataType(id, 16, 0, 0xffff, 0, 0xffff, 0) {} + + // @copydoc + virtual result_t derive(int divisor, unsigned char bitCount, NumberDataType* &derived); + + // @copydoc + virtual result_t readSymbols(SymbolString& input, const bool isMaster, + const unsigned char offset, const unsigned char length, + ostringstream& output, OutputFormat outputFormat); + + // @copydoc + virtual result_t writeSymbols(istringstream& input, + const unsigned char offset, const unsigned char length, + SymbolString& output, const bool isMaster, unsigned char* usedLength); + +}; + +/** + * Registration function to be called once during initialization. + */ +void contrib_tem_register(); + +#endif // LIBEBUS_CONTRIB_TEM_H_ diff --git a/src/lib/ebus/contrib/test/Makefile.am b/src/lib/ebus/contrib/test/Makefile.am new file mode 100644 index 00000000..1af3b756 --- /dev/null +++ b/src/lib/ebus/contrib/test/Makefile.am @@ -0,0 +1,11 @@ +AM_CXXFLAGS = -Wno-unused-parameter \ + -isystem$(top_srcdir)/src/lib/ebus -isystem../ + +noinst_PROGRAMS = test_tem + +test_tem_SOURCES = test_tem.cpp +test_tem_LDADD = $(top_srcdir)/src/lib/ebus/libebus.a + +distclean-local: + -rm -f Makefile.in + -rm -rf .libs diff --git a/src/lib/ebus/contrib/test/test_tem.cpp b/src/lib/ebus/contrib/test/test_tem.cpp new file mode 100644 index 00000000..0682768d --- /dev/null +++ b/src/lib/ebus/contrib/test/test_tem.cpp @@ -0,0 +1,196 @@ +/* + * ebusd - daemon for communication with eBUS heating systems. + * Copyright (C) 2016 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 "tem.h" +#include "data.h" +#include +#include + +using namespace std; + +static bool error = false; + +void verify(bool expectFailMatch, string type, string input, + bool match, string expectStr, string gotStr) +{ + match = match && expectStr == gotStr; + if (expectFailMatch) { + if (match) { + cout << " failed " << type << " match >" << input + << "< error: unexpectedly succeeded" << endl; + error = true; + } else { + cout << " failed " << type << " match >" << input << "< OK" << endl; + } + } else if (match) { + cout << " " << type << " match >" << input << "< OK" << endl; + } else { + cout << " " << type << " match >" << input << "< error: got >" + << gotStr << "<, expected >" << expectStr << "<" << endl; + error = true; + } +} + +int main() +{ + DataType* type = DataTypeList::getInstance()->get("TEM_P"); + if (type == NULL) { + cout << "datatype not registered" << endl; + return 1; + } + + string checks[][5] = { + // entry: definition, decoded value, master data, slave data, flags + // definition: name,part,type[:len][,[divisor|values][,[unit][,[comment]]]] + {"x,,TEM_P", "04-033", "10fe0700020421", "00", ""}, + {"x,,TEM_P", "00-000", "10fe0700020000", "00", ""}, + {"x,,TEM_P", "31-127", "10fe0700021f7f", "00", ""}, + {"x,,TEM_P", "-", "10fe070002ffff", "00", ""}, + {"x,,TEM_P", "32-000", "10fe0700022000", "00", "Rw"}, + {"x,,TEM_P", "00-128", "10fe0700020080", "00", "Rw"}, + + {"x,,TEM_P", "04-033", "1015070000", "022102", ""}, + {"x,,TEM_P", "00-000", "1015070000", "020000", ""}, + {"x,,TEM_P", "31-127", "1015070000", "02ff0f", ""}, + {"x,,TEM_P", "-", "1015070000", "02ffff", ""}, + {"x,,TEM_P", "32-000", "1015070000", "022000", "Rw"}, + {"x,,TEM_P", "00-128", "1015070000", "020080", "Rw"}, + }; + DataFieldTemplates* templates = new DataFieldTemplates(); + DataField* fields = NULL; + for (size_t i = 0; i < sizeof(checks) / sizeof(checks[0]); i++) { + string check[5] = checks[i]; + istringstream isstr(check[0]); + string expectStr = check[1]; + SymbolString mstr(false); + result_t result = mstr.parseHex(check[2]); + if (result != RESULT_OK) { + cout << "\"" << check[0] << "\": parse \"" << check[2] << "\" error: " << getResultCode(result) << endl; + error = true; + continue; + } + SymbolString sstr(false); + result = sstr.parseHex(check[3]); + if (result != RESULT_OK) { + cout << "\"" << check[0] << "\": parse \"" << check[3] << "\" error: " << getResultCode(result) << endl; + error = true; + continue; + } + string flags = check[4]; + bool isSet = flags.find('s') != string::npos; + bool failedRead = flags.find('r') != string::npos; + bool failedReadMatch = flags.find('R') != string::npos; + bool failedWrite = flags.find('w') != string::npos; + bool failedWriteMatch = flags.find('W') != string::npos; + string item; + vector entries; + + while (getline(isstr, item, FIELD_SEPARATOR)) + entries.push_back(item); + + if (fields != NULL) { + delete fields; + fields = NULL; + } + vector::iterator it = entries.begin(); + result = DataField::create(it, entries.end(), templates, fields, isSet, false, (mstr[1]==BROADCAST || isMaster(mstr[1]))); + if (result != RESULT_OK) { + cout << "\"" << check[0] << "\": create error: " << getResultCode(result) << endl; + error = true; + continue; + } + if (fields == NULL) { + cout << "\"" << check[0] << "\": create error: NULL" << endl; + error = true; + continue; + } + if (it != entries.end()) { + cout << "\"" << check[0] << "\": create error: trailing input" << endl; + error = true; + continue; + } + cout << "\"" << check[0] << "\"=\""; + fields->dump(cout); + cout << "\": create OK" << endl; + + ostringstream output; + SymbolString writeMstr(false); + result = writeMstr.parseHex(mstr.getDataStr(true, false).substr(0, 10)); + if (result != RESULT_OK) { + cout << " parse \"" << mstr.getDataStr(true, false).substr(0, 10) << "\" error: " << getResultCode(result) << endl; + error = true; + } + SymbolString writeSstr(false); + result = writeSstr.parseHex(sstr.getDataStr(true, false).substr(0, 2)); + if (result != RESULT_OK) { + cout << " parse \"" << sstr.getDataStr(true, false).substr(0, 2) << "\" error: " << getResultCode(result) << endl; + error = true; + } + result = fields->read(pt_masterData, mstr, 0, output, 0, -1, false); + if (result >= RESULT_OK) { + result = fields->read(pt_slaveData, sstr, 0, output, 0, -1, !output.str().empty()); + } + if (failedRead) + if (result >= RESULT_OK) { + cout << " failed read " << fields->getName() << " >" << check[2] << " " << check[3] + << "< error: unexpectedly succeeded" << endl; + error = true; + } else { + cout << " failed read " << fields->getName() << " >" << check[2] << " " << check[3] + << "< OK" << endl; + } + else if (result < RESULT_OK) { + cout << " read " << fields->getName() << " >" << check[2] << " " << check[3] + << "< error: " << getResultCode(result) << endl; + error = true; + } + else { + bool match = strcasecmp(output.str().c_str(), expectStr.c_str()) == 0; + verify(failedReadMatch, "read", check[2], match, expectStr, output.str()); + } + + istringstream input(expectStr); + result = fields->write(input, pt_masterData, writeMstr, 0); + if (result >= RESULT_OK) + result = fields->write(input, pt_slaveData, writeSstr, 0); + if (failedWrite) { + if (result >= RESULT_OK) { + cout << " failed write " << fields->getName() << " >" + << expectStr << "< error: unexpectedly succeeded" << endl; + error = true; + } else { + cout << " failed write " << fields->getName() << " >" + << expectStr << "< OK" << endl; + } + } + else if (result < RESULT_OK) { + cout << " write " << fields->getName() << " >" << expectStr + << "< error: " << getResultCode(result) << endl; + error = true; + } else { + bool match = mstr == writeMstr && sstr == writeSstr; + verify(failedWriteMatch, "write", expectStr, match, mstr.getDataStr(true, false) + " " + sstr.getDataStr(true, false), writeMstr.getDataStr(true, false) + " " + writeSstr.getDataStr(true, false)); + } + delete fields; + fields = NULL; + } + + delete templates; + + return error ? 1 : 0; +} diff --git a/src/lib/ebus/datatype.cpp b/src/lib/ebus/datatype.cpp index 0d893184..9c72a932 100644 --- a/src/lib/ebus/datatype.cpp +++ b/src/lib/ebus/datatype.cpp @@ -29,6 +29,10 @@ #include #include +#ifdef HAVE_CONTRIB +#include "contrib/contrib.h" +#endif + using namespace std; unsigned int parseInt(const char* str, int base, const unsigned int minValue, const unsigned int maxValue, result_t& result, unsigned int* length) { @@ -963,6 +967,11 @@ result_t NumberDataType::writeSymbols(istringstream& input, DataTypeList DataTypeList::s_instance; +#ifdef HAVE_CONTRIB +bool DataTypeList::s_contrib_initialized = libebus_contrib_register(); +#endif + + DataTypeList::DataTypeList() { add(new StringDataType("STR", MAX_LEN*8, ADJ, ' ')); // >= 1 byte character string filled up with space diff --git a/src/lib/ebus/datatype.h b/src/lib/ebus/datatype.h index 446ed650..3917da1c 100644 --- a/src/lib/ebus/datatype.h +++ b/src/lib/ebus/datatype.h @@ -591,6 +591,11 @@ private: /** the singleton instance. */ static DataTypeList s_instance; +#ifdef HAVE_CONTRIB + /** true when contributed datatypes were successfully initialized. */ + static bool s_contrib_initialized; +#endif + }; #endif // LIBEBUS_DATATYPE_H_