From f2650727e505b750386a4226404bd14d9c37a0c6 Mon Sep 17 00:00:00 2001 From: Siddharth Chandrasekaran Date: Wed, 14 Aug 2024 19:07:45 +0200 Subject: [PATCH] API: Add support for PD status change events. In addition to SC status change event introduced in eefa5f10f, add a new event for PD online/offline state changes. Now that we wait 8 seconds before putting a PD to offline, this data can be consumed safely by applications. Fixes: #136 Signed-off-by: Siddharth Chandrasekaran --- include/osdp.h | 6 ++++++ python/osdp/constants.py | 1 + python/osdp_sys/module.c | 4 +++- src/osdp_cp.c | 18 ++++++++++++++++++ 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/include/osdp.h b/include/osdp.h index 4fce8ef..e363b06 100644 --- a/include/osdp.h +++ b/include/osdp.h @@ -817,6 +817,12 @@ enum osdp_event_notification_type { * arg1: scbk type -- 0: scbk; 1: scbk-d */ OSDP_EVENT_NOTIFICATION_SC_STATUS, + /** + * PD state change + * + * arg0: status -- 0: offline; 1: online + */ + OSDP_EVENT_NOTIFICATION_PD_STATUS, }; /** diff --git a/python/osdp/constants.py b/python/osdp/constants.py index a9be948..4d0c583 100644 --- a/python/osdp/constants.py +++ b/python/osdp/constants.py @@ -56,6 +56,7 @@ class CommandFileTxFlags: class EventNotification: Command = osdp_sys.EVENT_NOTIFICATION_COMMAND SecureChannelStatus = osdp_sys.EVENT_NOTIFICATION_SC_STATUS + PeripheralDeviceStatus = osdp_sys.EVENT_NOTIFICATION_PD_STATUS class Event: CardRead = osdp_sys.EVENT_CARDREAD diff --git a/python/osdp_sys/module.c b/python/osdp_sys/module.c index cec0bb4..7d935ed 100644 --- a/python/osdp_sys/module.c +++ b/python/osdp_sys/module.c @@ -51,11 +51,13 @@ void pyosdp_add_module_constants(PyObject *module) ADD_CONST("STATUS_REPORT_OUTPUT", OSDP_STATUS_REPORT_OUTPUT) ADD_CONST("STATUS_REPORT_REMOTE", OSDP_STATUS_REPORT_REMOTE) - /* For `struct osdp_cmd_file_tx`::flags */ + /* For `struct osdp_cmd_file_tx::flags` */ ADD_CONST("CMD_FILE_TX_FLAG_CANCEL", OSDP_CMD_FILE_TX_FLAG_CANCEL); + /* For `struct osdp_event_notification::type` */ ADD_CONST("EVENT_NOTIFICATION_COMMAND", OSDP_EVENT_NOTIFICATION_COMMAND); ADD_CONST("EVENT_NOTIFICATION_SC_STATUS", OSDP_EVENT_NOTIFICATION_SC_STATUS); + ADD_CONST("EVENT_NOTIFICATION_PD_STATUS", OSDP_EVENT_NOTIFICATION_PD_STATUS); /* enum osdp_event_type */ ADD_CONST("EVENT_CARDREAD", OSDP_EVENT_CARDREAD); diff --git a/src/osdp_cp.c b/src/osdp_cp.c index d3c53e7..a4e180f 100644 --- a/src/osdp_cp.c +++ b/src/osdp_cp.c @@ -958,6 +958,22 @@ static int cp_get_online_command(struct osdp_pd *pd) return -1; } +static void notify_pd_status(struct osdp_pd *pd, bool is_online) +{ + struct osdp *ctx = pd_to_osdp(pd); + struct osdp_event evt; + + if (!ctx->event_callback || + !ISSET_FLAG(pd, OSDP_FLAG_ENABLE_NOTIFICATION)) { + return; + } + + evt.type = OSDP_EVENT_NOTIFICATION; + evt.notif.type = OSDP_EVENT_NOTIFICATION_PD_STATUS; + evt.notif.arg0 = is_online; + ctx->event_callback(ctx->event_callback_arg, pd->idx, &evt); +} + static void notify_sc_status(struct osdp_pd *pd) { struct osdp *ctx = pd_to_osdp(pd); @@ -1197,6 +1213,7 @@ static void cp_state_change(struct osdp_pd *pd, enum osdp_cp_state_e next) break; case OSDP_CP_STATE_ONLINE: LOG_INF("Online; %s SC", sc_is_active(pd) ? "With" : "Without"); + notify_pd_status(pd, true); break; case OSDP_CP_STATE_OFFLINE: pd->tstamp = osdp_millis_now(); @@ -1205,6 +1222,7 @@ static void cp_state_change(struct osdp_pd *pd, enum osdp_cp_state_e next) notify_sc_status(pd); LOG_ERR("Going offline for %d seconds; Was in '%s' state", pd->wait_ms / 1000, state_get_name(cur)); + notify_pd_status(pd, true); break; case OSDP_CP_STATE_SC_CHLNG: osdp_sc_setup(pd);