knx/examples/knxPython/knxmodule.cpp

223 lines
4.9 KiB
C++
Raw Normal View History

2018-12-22 01:55:08 +01:00
#include <pybind11/pybind11.h>
#include <pybind11/stl_bind.h>
#include <pybind11/functional.h>
2019-06-29 10:30:58 +02:00
#include <pybind11/stl.h>
2024-08-30 20:47:43 +02:00
2018-12-22 01:55:08 +01:00
namespace py = pybind11;
2018-12-11 22:42:13 +01:00
#include <Python.h>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
2018-12-22 01:55:08 +01:00
#include <thread>
#include <stdint.h>
#include <vector>
2019-06-29 10:30:58 +02:00
#include <algorithm>
2018-12-11 22:42:13 +01:00
#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>
2024-08-30 20:47:43 +02:00
#define LOGGER Logger::logger("knxmodule")
2024-08-28 20:12:21 +02:00
using namespace Knx;
2018-12-11 22:42:13 +01:00
2019-06-29 00:48:57 +02:00
LinuxPlatform* platform = 0;
Bau57B0* bau = 0;
2018-12-11 22:42:13 +01:00
2019-01-21 23:31:53 +01:00
bool running = false;
2018-12-22 01:55:08 +01:00
static void loop()
2018-12-11 22:42:13 +01:00
{
2019-01-21 23:31:53 +01:00
while (running)
2018-12-11 22:42:13 +01:00
{
2019-06-29 00:48:57 +02:00
bau->loop();
delayMicroseconds(100);
2018-12-11 22:42:13 +01:00
}
}
2018-12-22 01:55:08 +01:00
static std::thread workerThread;
2019-06-29 10:37:33 +02:00
static std::vector<std::string> argsVector;
2019-06-29 10:30:58 +02:00
static std::vector<const char*> argv;
2018-12-11 22:42:13 +01:00
2019-06-29 10:30:58 +02:00
struct StdStringCStrFunctor
2019-06-29 00:50:08 +02:00
{
2024-08-30 20:47:43 +02:00
const char* operator() (const std::string& str)
{
return str.c_str();
}
2019-06-29 10:30:58 +02:00
};
2024-08-30 20:47:43 +02:00
static void init()
2019-06-29 10:30:58 +02:00
{
2024-08-30 20:47:43 +02:00
Logger::logLevel("knxmodule", Logger::Info);
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);
2024-08-30 20:47:43 +02:00
/*
// copy args so we control the livetime of the char*
argsVector = args;
2019-06-29 10:30:58 +02:00
2024-08-30 20:47:43 +02:00
for (int i = 0; i < args.size(); i++)
printf("%s\n", args[i].c_str());
2019-06-29 10:30:58 +02:00
2024-08-30 20:47:43 +02:00
argv = std::vector<const char*>(argsVector.size());
std::transform(argsVector.begin(), argsVector.end(), argv.begin(), StdStringCStrFunctor());
*/
2020-10-28 21:44:01 +01:00
platform = new LinuxPlatform();
platform->cmdLineArgs(argv.size(), const_cast<char**>(argv.data()));
2019-06-29 16:40:29 +02:00
bau = new Bau57B0(*platform);
2024-08-30 20:47:43 +02:00
2019-06-29 00:50:08 +02:00
}
static void Destroy()
{
2019-06-29 16:40:29 +02:00
delete platform;
delete bau;
platform = 0;
bau = 0;
}
static void ReadMemory()
{
if (!bau)
2024-08-30 20:47:43 +02:00
init();
2019-06-29 16:40:29 +02:00
bau->readMemory();
2019-06-29 00:50:08 +02:00
}
2019-01-21 23:31:53 +01:00
2018-12-22 01:55:08 +01:00
static void Start()
2018-12-11 22:42:13 +01:00
{
2019-01-21 23:31:53 +01:00
if (running)
2018-12-22 01:55:08 +01:00
return;
2024-08-30 20:47:43 +02:00
2019-06-29 16:40:29 +02:00
if (!bau)
2024-08-30 20:47:43 +02:00
init();
2019-06-29 00:48:57 +02:00
2019-01-21 23:31:53 +01:00
running = true;
2024-08-30 20:47:43 +02:00
2019-06-29 00:48:57 +02:00
bau->enabled(true);
2018-12-11 22:42:13 +01:00
2018-12-22 01:55:08 +01:00
workerThread = std::thread(loop);
workerThread.detach();
2018-12-11 22:42:13 +01:00
}
2019-01-21 23:31:53 +01:00
static void Stop()
{
if (!running)
return;
2024-08-30 20:47:43 +02:00
2019-01-21 23:31:53 +01:00
running = false;
2019-06-29 00:48:57 +02:00
bau->writeMemory();
bau->enabled(false);
2024-08-30 20:47:43 +02:00
2019-01-21 23:31:53 +01:00
workerThread.join();
}
2018-12-22 01:55:08 +01:00
static bool ProgramMode(bool value)
2018-12-11 22:42:13 +01:00
{
2019-06-29 16:40:29 +02:00
if (!bau)
2024-08-30 20:47:43 +02:00
init();
2019-06-29 00:50:08 +02:00
2024-08-30 20:47:43 +02:00
LOGGER.info("ProgramMode %d", value);
2019-06-29 00:48:57 +02:00
bau->deviceObject().progMode(value);
return bau->deviceObject().progMode();
2018-12-11 22:42:13 +01:00
}
2018-12-22 01:55:08 +01:00
static bool ProgramMode()
2018-12-11 22:42:13 +01:00
{
2019-06-29 16:40:29 +02:00
if (!bau)
2024-08-30 20:47:43 +02:00
init();
2019-06-29 00:50:08 +02:00
2019-06-29 00:48:57 +02:00
return bau->deviceObject().progMode();
2018-12-22 01:55:08 +01:00
}
2019-01-17 21:36:45 +01:00
static bool Configured()
{
2019-06-29 16:40:29 +02:00
if (!bau)
2024-08-30 20:47:43 +02:00
init();
2019-06-29 00:50:08 +02:00
2019-06-29 00:48:57 +02:00
return bau->configured();
2019-01-17 21:36:45 +01:00
}
2018-12-22 01:55:08 +01:00
2024-08-30 20:47:43 +02:00
PYBIND11_MODULE(knx, m)
{
m.doc() = "wrapper for knx device lib"; // optional module docstring
2018-12-22 01:55:08 +01:00
m.def("Start", &Start, "Start knx handling thread.");
2019-06-29 10:30:58 +02:00
m.def("Stop", &Stop, "Stop knx handling thread.");
2024-08-30 20:47:43 +02:00
m.def("Destroy", &Destroy, "Free object allocated objects.");
m.def("ProgramMode", (bool(*)())&ProgramMode, "get programing mode active.");
2018-12-22 01:55:08 +01:00
m.def("ProgramMode", (bool(*)(bool))&ProgramMode, "Activate / deactivate programing mode.");
2024-08-30 20:47:43 +02:00
m.def("Configured", (bool(*)())&Configured, "get configured status.");
2019-06-29 16:40:29 +02:00
m.def("ReadMemory", &ReadMemory, "read memory from flash file");
2024-08-30 20:47:43 +02:00
m.def("FlashFilePath", []()
{
if (!platform)
init();
return platform->flashFilePath();
});
m.def("FlashFilePath", [](std::string path)
{
if (!platform)
init();
platform->flashFilePath(path);
});
m.def("GetGroupObject", [](uint16_t goNr)
{
LOGGER.info("GetGroupObject arg %d", goNr);
LOGGER.info("GetGroupObject entrycount %d", bau->groupObjectTable().entryCount());
if (!bau)
init();
if (goNr > bau->groupObjectTable().entryCount())
return (GroupObject*)nullptr;
return &bau->groupObjectTable().get(goNr);
}, py::return_value_policy::reference);
m.def("Callback", [](GroupObjectUpdatedHandler handler)
{
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());
});
2024-08-30 20:47:43 +02:00
py::class_<GroupObject>(m, "GroupObject", py::dynamic_attr())
2024-08-30 20:47:43 +02:00
.def(py::init())
.def("asap", &GroupObject::asap)
.def("size", &GroupObject::valueSize)
.def_property("value",
[](GroupObject & go)
{
return py::bytes((const char*)go.valueRef(), go.valueSize());
},
[](GroupObject & go, py::bytes bytesValue)
{
const auto value = static_cast<std::string>(bytesValue);
if (value.length() != go.valueSize())
throw std::length_error("bytesValue");
auto valueRef = go.valueRef();
memcpy(valueRef, value.c_str(), go.valueSize());
go.objectWritten();
});
2019-06-29 10:30:58 +02:00
}