Skip to content

Commit

Permalink
Update pycsp.c
Browse files Browse the repository at this point in the history
  • Loading branch information
moonlight83340 committed Aug 29, 2024
1 parent 07bbf22 commit 0b5b8c5
Showing 1 changed file with 37 additions and 12 deletions.
49 changes: 37 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,24 @@ 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;
csp_iface_t * iface = 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);
if(iface_capsule != NULL){
iface = get_capsule_pointer(iface_capsule, IFACE_CAPSULE, false);
}

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;

if(iface_capsule == NULL){
iface->is_default = 1;
}

Py_RETURN_NONE;
}
Expand All @@ -861,16 +870,24 @@ 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;
csp_iface_t * iface = 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);
if(iface_capsule != NULL){
iface = get_capsule_pointer(iface_capsule, IFACE_CAPSULE, false);
}

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;

if(iface_capsule == NULL){
iface->is_default = 1;
}

Py_RETURN_NONE;
}
Expand All @@ -881,17 +898,25 @@ 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;
csp_iface_t * iface = NULL;
if (!PyArg_ParseTuple(args, "sH|OIIs", &device, &addr, &iface_capsule, &baudrate, &mtu, &if_name)) {
return NULL; // TypeError is thrown
}

if(iface_capsule != NULL){
iface = get_capsule_pointer(iface_capsule, IFACE_CAPSULE, false);
}

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;

if(iface_capsule == NULL){
iface->is_default = 1;
}

Py_RETURN_NONE;
}
Expand Down

0 comments on commit 0b5b8c5

Please sign in to comment.