Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
moonlight83340 committed Aug 28, 2024
1 parent 149a50a commit c77e97b
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 14 deletions.
4 changes: 3 additions & 1 deletion examples/python_bindings_example_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ def get_options():

if options.can:
# add CAN interface
libcsp.can_socketcan_init(options.can, options.address)
iface = libcsp.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)
Expand Down
8 changes: 7 additions & 1 deletion examples/python_bindings_example_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
69 changes: 57 additions & 12 deletions src/bindings/python/pycsp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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}};

Expand Down

0 comments on commit c77e97b

Please sign in to comment.