Skip to content

Commit

Permalink
-remove hack for 0 terminating string in groupobjects
Browse files Browse the repository at this point in the history
-fix logging on linux
-add parameters to python bindings
  • Loading branch information
thelsing committed Sep 11, 2024
1 parent b687cd2 commit d1bdc6e
Show file tree
Hide file tree
Showing 17 changed files with 77 additions and 54 deletions.
8 changes: 4 additions & 4 deletions examples/knx-demo/knx-demo.ino
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,13 @@ void setup()
goMin.dataPointType(DPT_Value_Temp);
goMax.dataPointType(DPT_Value_Temp);

Serial.print("Timeout: ");
Serial.print("Startverzögerung s: ");
Serial.println(knx.paramByte(0));
Serial.print("Zykl. senden: ");
Serial.print("Aenderung senden (*0.1K): ");
Serial.println(knx.paramByte(1));
Serial.print("Min/Max senden: ");
Serial.print("Zykl. senden min: ");
Serial.println(knx.paramByte(2));
Serial.print("Aenderung senden: ");
Serial.print("Min/Max senden: ");
Serial.println(knx.paramByte(3));
Serial.print("Abgleich: ");
Serial.println(knx.paramByte(4));
Expand Down
11 changes: 6 additions & 5 deletions examples/knx-linux/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ void setup()
srand((unsigned int)time(NULL));

Logger::logLevel("App", Logger::Info);
Logger::logLevel("ApplicationLayer", Logger::Info);
Logger::logLevel("TableObject", Logger::Info);
Logger::logLevel("Memory", Logger::Info);
knx.readMemory();

if (knx.individualAddress() == 0xFFFF)
Expand All @@ -115,11 +116,11 @@ void setup()
GO_MAX.dataPointType(Dpt(9, 1));
GO_MAX.valueNoSend(-273.0);
GO_RESET.dataPointType(Dpt(1, 15));
LOGGER.info("Timeout: %d", knx.paramWord(0));
LOGGER.info("Zykl. senden: %d", knx.paramByte(2));
LOGGER.info("Startverzögerung s: %d", knx.paramByte(0));
LOGGER.info("Aenderung senden (*0.1K): %d", knx.paramByte(1));
LOGGER.info("Zykl. senden min: %d", knx.paramByte(2));
LOGGER.info("Min/Max senden: %d", knx.paramByte(3));
LOGGER.info("Aenderung senden: %d", knx.paramByte(4));
LOGGER.info("Abgleich %d", knx.paramByte(5));
LOGGER.info("Abgleich %d", knx.paramInt(4));
}
else
LOGGER.info("not configured");
Expand Down
20 changes: 15 additions & 5 deletions examples/knxPython/knxmodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ namespace py = pybind11;
#include <vector>
#include <algorithm>

#include "knx/bits.h"
#include "knx/platform/linux_platform.h"
#include "knx/ip/bau57B0.h"
#include "knx/interface_object/group_object_table_object.h"
#include "knx/util/logger.h"
#include <knx/bits.h>
#include <knx/platform/linux_platform.h>
#include <knx/ip/bau57B0.h>
#include <knx/interface_object/group_object_table_object.h>
#include <knx/util/logger.h>

#define LOGGER Logger::logger("knxmodule")

Expand Down Expand Up @@ -56,6 +56,8 @@ static void init()
Logger::logLevel("ApplicationLayer", Logger::Info);
Logger::logLevel("BauSystemBDevice", Logger::Info);
Logger::logLevel("GroupObject", Logger::Info);
Logger::logLevel("TableObject", Logger::Info);
Logger::logLevel("Memory", Logger::Info);

/*
// copy args so we control the livetime of the char*
Expand Down Expand Up @@ -186,6 +188,14 @@ PYBIND11_MODULE(knx, m)
{
GroupObject::classCallback(handler);
});
m.def("Parameters", []()
{
uint8_t* data = bau->parameters().data();
if (data == nullptr)
return py::bytes();

return py::bytes((const char*)data, bau->parameters().dataSize());
});

py::class_<GroupObject>(m, "GroupObject", py::dynamic_attr())
.def(py::init())
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ build-backend = "scikit_build_core.build"

[project]
name = "knxPython"
version = "0.1.5"
version = "1.5.0"
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ set(SOURCES

set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -Wall -Wno-unknown-pragmas -g -O0")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall -Wno-unknown-pragmas -g -O0")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -g -O0")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -DKNX_FLASH_SIZE=0x20000")

add_library(knx ${SOURCES})
target_include_directories(knx PUBLIC .)
Expand Down
1 change: 1 addition & 0 deletions src/knx/bau/bau_systemB_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace Knx
void loop() override;
bool configured() override;
GroupObjectTableObject& groupObjectTable();


protected:
ApplicationLayer& applicationLayer() override;
Expand Down
2 changes: 2 additions & 0 deletions src/knx/group_object/dpt.h
Original file line number Diff line number Diff line change
Expand Up @@ -372,5 +372,7 @@ namespace Knx
unsigned short index;
bool operator==(const Dpt& other) const;
bool operator!=(const Dpt& other) const;


};
}
10 changes: 5 additions & 5 deletions src/knx/group_object/dptconvert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -629,16 +629,16 @@ namespace Knx
int busValueToString(const uint8_t* payload, size_t payload_length, const Dpt& datatype, KNXValue& value)
{
ASSERT_PAYLOAD(14);

char strValue[15];
strValue[14] = '\0';
for (int n = 0; n < 14; ++n)
{
auto value = signed8FromPayload(payload, n);

if (!datatype.subGroup && (value & 0x80))
strValue[n] = signed8FromPayload(payload, n);
if (!datatype.subGroup && (strValue[n] & 0x80))
return false;
}

value = (const char*) payload;
value = strValue;
return true;
}

Expand Down
14 changes: 0 additions & 14 deletions src/knx/group_object/group_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,20 +202,6 @@ namespace Knx
return asapValueSize(code);
}

size_t GroupObject::sizeInMemory() const
{
uint8_t code = lowByte(ntohs(_table->_tableData[_asap]));
size_t result = asapValueSize(code);

if (code == 0)
return 1;

if (code == 14)
return 14 + 1;

return result;
}

GroupObjectUpdatedHandler GroupObject::classCallback()
{
return _updateHandlerStatic;
Expand Down
5 changes: 0 additions & 5 deletions src/knx/group_object/group_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,6 @@ namespace Knx
* will return 0.
*/
size_t sizeInTelegram();
/**
* returns the size of the group object in the heap memory of the group object. The function returns the same value as goSize(),
* exept fot the 14 byte string type to reserve one byte of a \0 terminator character.
*/
size_t sizeInMemory() const;
/**
* returns the pointer to the value of the group object. This can be used if a datapoint type is not supported or if you want do
* your own conversion.
Expand Down
2 changes: 2 additions & 0 deletions src/knx/interface_object/application_program_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ namespace Knx
uint16_t getWord(uint32_t addr);
uint32_t getInt(uint32_t addr);
double getFloat(uint32_t addr, ParameterFloatEncodings encoding);
using TableObject::data;
using TableObject::dataSize;
const char* name() override
{
return "ApplicationProgram";
Expand Down
7 changes: 3 additions & 4 deletions src/knx/interface_object/group_object_table_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace Knx

GroupObject& GroupObjectTableObject::get(uint16_t asap)
{
if(asap == 0 || asap > entryCount())
if (asap == 0 || asap > entryCount())
LOGGER.warning("get: %d is no valid GroupObject. Asap must be > 0 and <= %d", asap, entryCount());

return _groupObjects[asap - 1];
Expand Down Expand Up @@ -122,9 +122,8 @@ namespace Knx
go._table = this;

go._dataLength = go.goSize();
size_t sizeInMemory = go.sizeInMemory();
go._data = new uint8_t[sizeInMemory];
memset(go._data, 0, sizeInMemory);
go._data = new uint8_t[go._dataLength];
memset(go._data, 0, go._dataLength);

if (go.valueReadOnInit())
go.requestObjectRead();
Expand Down
5 changes: 5 additions & 0 deletions src/knx/interface_object/table_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,11 @@ namespace Knx
return _data;
}

uint32_t TableObject::dataSize()
{
return _size;
}

void TableObject::errorCode(ErrorCode errorCode)
{
uint8_t data = errorCode;
Expand Down
6 changes: 6 additions & 0 deletions src/knx/interface_object/table_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ namespace Knx
* must not be written at nor freed.
*/
uint8_t* data();

/**
* returns the size in bytes of the internal data of the interface object.
*/
uint32_t dataSize();

/**
* Set the reason for a state change failure.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/knx/platform/linux_platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ namespace Knx
fsync(_fd);
}

#define FLASHSIZE 0x10000
#define FLASHSIZE 0x20000
void LinuxPlatform::doMemoryMapping()
{
fs::path filePath = _flashFilePath;
Expand Down
14 changes: 10 additions & 4 deletions src/knx/util/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@

namespace Knx
{
Map<const char*, Logger::LogType, 64> Logger::_loggers;
Map<loggername_t, Logger::LogType, 64> Logger::_loggers;
Logger Logger::_logger;

Logger& Logger::logger(const char* name)
Logger& Logger::logger(loggername_t name)
{
_logger.name(name);
return _logger;
}

void Logger::logLevel(const char* name, LogType level)
void Logger::logLevel(loggername_t name, LogType level)
{
_loggers.insertOrAssign(name, level);
}
Expand Down Expand Up @@ -67,6 +67,12 @@ namespace Knx
va_end(objects);
#endif
}
#ifdef __linux__
void print(std::string msg)
{
print(msg.c_str());
}
#endif

bool Logger::log(LogType type)
{
Expand All @@ -86,7 +92,7 @@ namespace Knx

if (*level > type)
return false;

print(millis());
print(" ");
print(_name);
Expand Down
20 changes: 15 additions & 5 deletions src/knx/util/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,18 @@
#include <stdarg.h>
#include "simple_map.h"

#ifdef __linux
#include <string>
#define loggername_t std::string
#else
#define loggername_t const char*
#endif


namespace Knx
{


class IPrintable
{
public:
Expand All @@ -23,8 +33,8 @@ namespace Knx
{
public:
enum LogType { Info, Warning, Error, Critical, Exception, Disabled};
static Logger& logger(const char* name);
static void logLevel(const char* name, LogType level);
static Logger& logger(const loggername_t name);
static void logLevel(const loggername_t name, LogType level);
void info(const char* message, IPrintable& object)
{
if (!log(LogType::Info))
Expand Down Expand Up @@ -79,14 +89,14 @@ namespace Knx
Logger() {}
bool log(LogType type);
void log(LogType type, const char* format, va_list args);
void name(const char* value)
void name(loggername_t value)
{
_name = value;
}
private:
const char* enum_name(LogType type);
const char* _name = "";
static Map<const char*, LogType, 64> _loggers;
loggername_t _name = "";
static Map<loggername_t, LogType, 64> _loggers;
static Logger _logger;
};
}

0 comments on commit d1bdc6e

Please sign in to comment.