Skip to content

Commit

Permalink
API: Add support for PD status change events.
Browse files Browse the repository at this point in the history
In addition to SC status change event introduced in eefa5f1, 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 <[email protected]>
  • Loading branch information
sidcha committed Aug 15, 2024
1 parent e8205a0 commit f265072
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
6 changes: 6 additions & 0 deletions include/osdp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

/**
Expand Down
1 change: 1 addition & 0 deletions python/osdp/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion python/osdp_sys/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
18 changes: 18 additions & 0 deletions src/osdp_cp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand All @@ -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);
Expand Down

0 comments on commit f265072

Please sign in to comment.