From 0f047ffea0167314b8aaaa141ada02002991d303 Mon Sep 17 00:00:00 2001 From: Gaetan Perrot Date: Wed, 28 Aug 2024 20:58:35 +0900 Subject: [PATCH] WIP --- examples/python_bindings_example_client.py | 4 +- examples/python_bindings_example_server.py | 8 ++- src/bindings/python/pycsp.c | 69 ++++++++++++++++++---- 3 files changed, 67 insertions(+), 14 deletions(-) diff --git a/examples/python_bindings_example_client.py b/examples/python_bindings_example_client.py index a006e8cb8..fac50b4a8 100644 --- a/examples/python_bindings_example_client.py +++ b/examples/python_bindings_example_client.py @@ -44,7 +44,9 @@ def get_options(): if options.can: # add CAN interface - libcsp.can_socketcan_init(options.can, options.address) + iface = libcsp.pycsp_add_iface() + libcsp.can_socketcan_init(options.can, options.address, iface) + libcsp.iface_set_is_default(iface,1) if options.zmq: # add ZMQ interface - (address, host) diff --git a/examples/python_bindings_example_server.py b/examples/python_bindings_example_server.py index 4769d586d..50a35df9e 100644 --- a/examples/python_bindings_example_server.py +++ b/examples/python_bindings_example_server.py @@ -116,18 +116,24 @@ def csp_server(): if options.zmq: # add ZMQ interface - (address, host) # creates publish and subrcribe endpoints from the host + iface = libcsp.add_iface() libcsp.zmqhub_init(options.address, options.zmq) + libcsp.iface_set_is_default(iface,1) if options.kiss: libcsp.kiss_init(options.kiss, options.address) if options.can: # add CAN interface - libcsp.can_socketcan_init(options.can, options.address) + iface = libcsp.pycsp_add_iface() + libcsp.can_socketcan_init(options.can, options.address, iface) + libcsp.iface_set_is_default(iface,1) if options.routing_table: # Examples: "0/0 CAN, 8 KISS, 10 I2C 10", same as "0/0 CAN, 8/5 KISS, 10/5 I2C 10" + iface = libcsp.pycsp_add_iface() libcsp.rtable_load(options.routing_table) + libcsp.iface_set_is_default(iface,1) # Parameters: {priority} - 0 (critical), 1 (high), 2 (norm), 3 (low) ---- default=2 # Start the router task - creates routing thread diff --git a/src/bindings/python/pycsp.c b/src/bindings/python/pycsp.c index 1fc7d441b..4a83bb91d 100644 --- a/src/bindings/python/pycsp.c +++ b/src/bindings/python/pycsp.c @@ -13,6 +13,7 @@ #define SOCKET_CAPSULE "csp_socket_t" #define CONNECTION_CAPSULE "csp_conn_t" #define PACKET_CAPSULE "csp_packet_t" +#define IFACE_CAPSULE "csp_iface_t" static PyObject * Error = NULL; @@ -841,16 +842,17 @@ static PyObject * pycsp_cmp_clock_get(PyObject * self, PyObject * args) { static PyObject * pycsp_zmqhub_init(PyObject * self, PyObject * args) { uint16_t addr; char * host; - csp_iface_t * default_iface = NULL; - if (!PyArg_ParseTuple(args, "Hs", &addr, &host)) { + PyObject * iface_capsule = NULL; + if (!PyArg_ParseTuple(args, "Hs|O", &addr, &host, &iface_capsule)) { return NULL; // TypeError is thrown } - int res = csp_zmqhub_init(addr, host, 0, &default_iface); + csp_iface_t * iface = get_capsule_pointer(iface_capsule, IFACE_CAPSULE, true); + + int res = csp_zmqhub_init(addr, host, 0, &iface); if (res != CSP_ERR_NONE) { return PyErr_Error("csp_zmqhub_init()", res); } - default_iface->is_default = 1; Py_RETURN_NONE; } @@ -861,16 +863,17 @@ static PyObject * pycsp_can_socketcan_init(PyObject * self, PyObject * args) { int bitrate = 1000000; int promisc = 0; uint16_t addr = 0; - csp_iface_t * default_iface = NULL; - if (!PyArg_ParseTuple(args, "s|Hii", &ifc, &addr, &bitrate, &promisc)) { + PyObject * iface_capsule = NULL; + if (!PyArg_ParseTuple(args, "s|HOii", &ifc, &addr, &iface_capsule, &bitrate, &promisc)) { return NULL; } - int res = csp_can_socketcan_open_and_add_interface(ifc, CSP_IF_CAN_DEFAULT_NAME, addr, bitrate, promisc, &default_iface); + csp_iface_t * iface = get_capsule_pointer(iface_capsule, IFACE_CAPSULE, true); + + int res = csp_can_socketcan_open_and_add_interface(ifc, CSP_IF_CAN_DEFAULT_NAME, addr, bitrate, promisc, &iface); if (res != CSP_ERR_NONE) { return PyErr_Error("csp_can_socketcan_open_and_add_interface()", res); } - default_iface->is_default = 1; Py_RETURN_NONE; } @@ -881,17 +884,18 @@ static PyObject * pycsp_kiss_init(PyObject * self, PyObject * args) { uint32_t mtu = 512; uint16_t addr; const char * if_name = CSP_IF_KISS_DEFAULT_NAME; - csp_iface_t * default_iface = NULL; - if (!PyArg_ParseTuple(args, "sH|IIs", &device, &addr, &baudrate, &mtu, &if_name)) { + PyObject * iface_capsule = NULL; + if (!PyArg_ParseTuple(args, "sH|IIsO", &device, &addr, &iface_capsule, &baudrate, &mtu, &if_name)) { return NULL; // TypeError is thrown } + + csp_iface_t * iface = get_capsule_pointer(iface_capsule, IFACE_CAPSULE, true); csp_usart_conf_t conf = {.device = device, .baudrate = baudrate}; - int res = csp_usart_open_and_add_kiss_interface(&conf, if_name, addr, &default_iface); + int res = csp_usart_open_and_add_kiss_interface(&conf, if_name, addr, &iface); if (res != CSP_ERR_NONE) { return PyErr_Error("csp_usart_open_and_add_kiss_interface()", res); } - default_iface->is_default = 1; Py_RETURN_NONE; } @@ -943,6 +947,43 @@ static PyObject * pycsp_print_interfaces(PyObject * self, PyObject * args) { Py_RETURN_NONE; } +static PyObject * pycsp_iface_set_is_default(PyObject * self, PyObject * args) { + PyObject * iface_capsule = NULL; + int is_default; + + if (!PyArg_ParseTuple(args, "Oi", &iface_capsule, &is_default)) { + return NULL; // TypeError is thrown + } + + csp_iface_t * iface = get_capsule_pointer(iface_capsule, IFACE_CAPSULE, true); + if (iface == NULL) { + return NULL; // Handle error if iface is NULL + } + + iface->is_default = (uint8_t)is_default; + + Py_RETURN_NONE; +} + +static PyObject * pycsp_add_iface(PyObject * self, PyObject * args) { + csp_iface_t * iface = PyMem_RawCalloc(1, sizeof(*iface)); + if (iface == NULL) { + return PyErr_NoMemory(); + } + + uint16_t addr; + uint16_t netmask; + const char * name; + uint8_t is_default; + + if (!PyArg_ParseTuple(args, "|HHSb", &addr, &netmask, &name, &is_default)) { + PyMem_RawFree(iface); + return NULL; // TypeError is thrown + } + + return PyCapsule_New(iface, "csp_iface_t", NULL); +} + static PyMethodDef methods[] = { /* csp/csp.h */ @@ -1014,6 +1055,10 @@ static PyMethodDef methods[] = { {"print_connections", pycsp_print_connections, METH_NOARGS, ""}, {"print_interfaces", pycsp_print_interfaces, METH_NOARGS, ""}, + /* csp/csp_interface.h */ + {"add_iface", pycsp_add_iface, METH_VARARGS, "Setup iface"}, + {"iface_set_is_default", pycsp_iface_set_is_default, METH_VARARGS, "Set is_default flag in csp_iface_t"}, + /* sentinel */ {NULL, NULL, 0, NULL}};