Skip to content

Commit 77ca576

Browse files
committed
pytest: Fix crash due to missing baud rate handling in python layers
Signed-off-by: Siddharth Chandrasekaran <sidcha.dev@gmail.com>
1 parent 61fbe31 commit 77ca576

4 files changed

Lines changed: 30 additions & 3 deletions

File tree

python/osdp/helpers.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ def __init__(self, version: int, model: int, vendor_code: int,
1818

1919
class PDInfo:
2020
def __init__(self, address: int, channel: Channel, scbk: bytes=None,
21-
name: str=None, flags=[], id: PdId=None):
21+
name: str=None, flags=[], id: PdId=None, baud_rate: int=9600):
2222
self.address = address
2323
self.flags = flags
2424
self.scbk = scbk
25+
self.baud_rate = baud_rate
2526
if name:
2627
self.name = name
2728
else:
@@ -41,6 +42,7 @@ def get_flags(self) -> int:
4142
def get(self) -> dict:
4243
return {
4344
'name': self.name,
45+
'baud_rate': self.baud_rate,
4446
'address': self.address,
4547
'flags': self.get_flags(),
4648
'scbk': self.scbk,

python/osdp_sys/cp.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ static void pyosdp_cp_tp_dealloc(pyosdp_cp_t *self)
472472
"@return None"
473473
static int pyosdp_cp_tp_init(pyosdp_cp_t *self, PyObject *args, PyObject *kwargs)
474474
{
475-
int i, len;
475+
int i, len, baud_rate;
476476
uint8_t *scbk = NULL;
477477
PyObject *py_info_list, *py_info, *channel;
478478
static char *kwlist[] = { "", NULL };
@@ -529,6 +529,17 @@ static int pyosdp_cp_tp_init(pyosdp_cp_t *self, PyObject *args, PyObject *kwargs
529529
goto error;
530530
}
531531

532+
if (pyosdp_dict_get_int(py_info, "baud_rate", &baud_rate)) {
533+
PyErr_Clear();
534+
baud_rate = 9600;
535+
}
536+
if (baud_rate <= 0) {
537+
PyErr_Format(PyExc_ValueError,
538+
"Invalid baud_rate in pd_info at index %d", i);
539+
goto error;
540+
}
541+
info->baud_rate = baud_rate;
542+
532543
if (i == 0) {
533544
channel = PyDict_GetItemString(py_info, "channel");
534545
if (channel == NULL) {

python/osdp_sys/pd.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ static int pyosdp_add_pd_cap(PyObject *obj, osdp_pd_info_t *info)
345345
"@return None"
346346
static int pyosdp_pd_tp_init(pyosdp_pd_t *self, PyObject *args, PyObject *kwargs)
347347
{
348-
int scbk_length;
348+
int scbk_length, baud_rate;
349349
osdp_t *ctx;
350350
osdp_pd_info_t info = { 0 };
351351
struct osdp_channel osdp_channel = { 0 };
@@ -379,6 +379,16 @@ static int pyosdp_pd_tp_init(pyosdp_pd_t *self, PyObject *args, PyObject *kwargs
379379
if (pyosdp_dict_get_int(py_info, "flags", &info.flags))
380380
goto error;
381381

382+
if (pyosdp_dict_get_int(py_info, "baud_rate", &baud_rate)) {
383+
PyErr_Clear();
384+
baud_rate = 9600;
385+
}
386+
if (baud_rate <= 0) {
387+
PyErr_SetString(PyExc_ValueError, "Invalid baud_rate");
388+
goto error;
389+
}
390+
info.baud_rate = baud_rate;
391+
382392
channel = PyDict_GetItemString(py_info, "channel");
383393
if (channel == NULL) {
384394
PyErr_Format(PyExc_KeyError, "channel object missing");

tests/pytest/test_status.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,7 @@ def test_metrics_access_and_reset():
142142
next_pd_metrics = pd.get_metrics()
143143
assert sum(next_cp_metrics.values()) <= sum(cp_metrics.values())
144144
assert sum(next_pd_metrics.values()) <= sum(pd_metrics.values())
145+
146+
def test_pd_info_default_baud_rate():
147+
info = PDInfo(101, f1, scbk=key).get()
148+
assert info["baud_rate"] == 9600

0 commit comments

Comments
 (0)