Skip to content

Commit

Permalink
libosdp: Unify StatusReport query and event structs
Browse files Browse the repository at this point in the history
Commit caadc3e "libosdp: Rework status command and event handling"
introduced the same struct body into command and event which causes an
overhead to maintain. This patch unifies those structs and calls them
"StatusReport" which is shared between the event and command that are
used by the application.

Signed-off-by: Siddharth Chandrasekaran <[email protected]>
  • Loading branch information
sidcha committed Jan 28, 2024
1 parent 29cfbaf commit 6f2dbc9
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 135 deletions.
123 changes: 45 additions & 78 deletions include/osdp.h
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,49 @@ typedef struct {
*/
typedef void osdp_t;

enum osdp_status_report_type {
/**
* @brief Status report of the inputs attached the PD
*/
OSDP_STATUS_REPORT_INPUT,
/**
* @brief Status report of the output attached the PD
*/
OSDP_STATUS_REPORT_OUTPUT,
/**
* @brief Local tamper and power status report
*
* Bit-0: tamper
* Bit-1: power
*/
OSDP_STATUS_REPORT_LOCAL,
/**
* @brief Remote tamper and power status report
*
* Bit-0: tamper
* Bit-1: power
*/
OSDP_STATUS_REPORT_REMOTE,
};

/**
* @brief Status report structure. Used by OSDP_CMD_STATUS and OSDP_EVENT_STATUS.
* In case of command, it is used to send a query to the PD while in the case of
* events, the PR responds back with this structure.
*
* This can is used by the PD to indicate various status change reports. Upto
* a maximum of 32 statuses can be reported using this API.
*
* @param type The kind of event to report see `enum osdp_event_status_type_e`
* @param nr_entries Number of valid bits in `status`
* @param status Status bit mask
*/
struct osdp_status_report {
enum osdp_status_report_type type;
int nr_entries;
uint32_t mask;
};

/* ------------------------------- */
/* OSDP Commands */
/* ------------------------------- */
Expand Down Expand Up @@ -514,38 +557,6 @@ struct osdp_cmd_file_tx {
uint32_t flags;
};

enum osdp_command_status_query_e {
/**
* @brief Query for local status of the PD such as tamper and power
*/
OSDP_CMD_STATUS_QUERY_LOCAL,
/**
* @brief Query status of the inputs attached the PD
*/
OSDP_CMD_STATUS_QUERY_INPUT,
/**
* @brief Query status of the ouputs attached the PD
*/
OSDP_CMD_STATUS_QUERY_OUTPUT,
/**
* @brief Query status of the remote readers attached to the PD
*/
OSDP_CMD_STATUS_QUERY_REMOTE,
/**
* @brief Status query max value
*/
OSDP_CMD_STATUS_QUERY_SENTINEL
};

/**
* @brief Status query command.
*/
struct osdp_cmd_status {
enum osdp_command_status_query_e type;
int nr_entries;
uint32_t mask;
};

/**
* @brief OSDP application exposed commands
*/
Expand Down Expand Up @@ -585,7 +596,7 @@ struct osdp_cmd {
struct osdp_cmd_keyset keyset;
struct osdp_cmd_mfg mfg;
struct osdp_cmd_file_tx file_tx;
struct osdp_cmd_status status;
struct osdp_status_report status;
};
};

Expand Down Expand Up @@ -668,50 +679,6 @@ struct osdp_event_mfgrep {
uint8_t data[OSDP_EVENT_MFGREP_MAX_DATALEN];
};

/**
* @brief Status event types
*/
enum osdp_event_status_type_e {
/**
* @brief Input status event
*/
OSDP_EVENT_STATUS_TYPE_INPUT,
/**
* @brief Output status event
*/
OSDP_EVENT_STATUS_TYPE_OUTPUT,
/**
* @brief Remote tamper and power status event
*
* Bit-0: tamper
* Bit-1: power
*/
OSDP_EVENT_STATUS_TYPE_REMOTE,
/**
* @brief Local tamper and power status event
*
* Bit-0: tamper
* Bit-1: power
*/
OSDP_EVENT_STATUS_TYPE_LOCAL,
};

/**
* @brief OSDP Event input/output status
*
* This event is used by the PD to indicate various status change reports. Upto
* a maximum of 32 statuses can be reported using this API.
*
* @param type The kind of event to report see `enum osdp_event_status_type_e`
* @param nr_entries Number of valid bits in `status`
* @param status Status bit mask
*/
struct osdp_event_status {
enum osdp_event_status_type_e type;
int nr_entries;
uint32_t mask;
};

/**
* @brief OSDP PD Events
*/
Expand Down Expand Up @@ -752,7 +719,7 @@ struct osdp_event {
struct osdp_event_keypress keypress;
struct osdp_event_cardread cardread;
struct osdp_event_mfgrep mfgrep;
struct osdp_event_status status;
struct osdp_status_report status;
};
};

Expand Down
2 changes: 1 addition & 1 deletion python/osdp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from .key_store import KeyStore
from .constants import (
LibFlag, Command, CommandLEDColor, CommandFileTxFlags,
Event, CardFormat, Capability, LogLevel, CommandStatusType, EventStatusType
Event, CardFormat, Capability, LogLevel, StatusReportType
)
from .helpers import PDInfo, PDCapabilities

Expand Down
16 changes: 5 additions & 11 deletions python/osdp/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ class LogLevel:
Info = osdp_sys.LOG_INFO
Debug = osdp_sys.LOG_DEBUG

class CommandStatusType:
Local = osdp_sys.CMD_STATUS_QUERY_LOCAL
Input = osdp_sys.CMD_STATUS_QUERY_INPUT
Output = osdp_sys.CMD_STATUS_QUERY_OUTPUT
Remote = osdp_sys.CMD_STATUS_QUERY_REMOTE
class StatusReportType:
Local = osdp_sys.STATUS_REPORT_LOCAL
Input = osdp_sys.STATUS_REPORT_INPUT
Output = osdp_sys.STATUS_REPORT_OUTPUT
Remote = osdp_sys.STATUS_REPORT_REMOTE

class Command:
Output = osdp_sys.CMD_OUTPUT
Expand All @@ -48,12 +48,6 @@ class CommandLEDColor:
class CommandFileTxFlags:
Cancel = osdp_sys.CMD_FILE_TX_FLAG_CANCEL

class EventStatusType:
Input = osdp_sys.EVENT_STATUS_TYPE_INPUT
Output = osdp_sys.EVENT_STATUS_TYPE_OUTPUT
Remote = osdp_sys.EVENT_STATUS_TYPE_REMOTE
Local = osdp_sys.EVENT_STATUS_TYPE_LOCAL

class Event:
CardRead = osdp_sys.EVENT_CARDREAD
KeyPress = osdp_sys.EVENT_KEYPRESS
Expand Down
4 changes: 2 additions & 2 deletions python/osdp_sys/data.c
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ static int pyosdp_make_dict_cmd_status(PyObject *obj, struct osdp_cmd *cmd)
static int pyosdp_make_struct_cmd_status(struct osdp_cmd *p, PyObject *dict)
{
int type, nr_entries, mask;
struct osdp_cmd_status *cmd = &p->status;
struct osdp_status_report *cmd = &p->status;

if (pyosdp_dict_get_int(dict, "type", &type))
return -1;
Expand Down Expand Up @@ -526,7 +526,7 @@ static int pyosdp_make_dict_event_status(PyObject *obj, struct osdp_event *event
static int pyosdp_make_struct_event_status(struct osdp_event *p, PyObject *dict)
{
int type, nr_entries, mask;
struct osdp_event_status *ev = &p->status;
struct osdp_status_report *ev = &p->status;

if (pyosdp_dict_get_int(dict, "type", &type))
return -1;
Expand Down
13 changes: 4 additions & 9 deletions python/osdp_sys/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ void pyosdp_add_module_constants(PyObject *module)
ADD_CONST("CMD_FILE_TX", OSDP_CMD_FILE_TX);
ADD_CONST("CMD_STATUS", OSDP_CMD_STATUS);

ADD_CONST("CMD_STATUS_QUERY_LOCAL", OSDP_CMD_STATUS_QUERY_LOCAL)
ADD_CONST("CMD_STATUS_QUERY_INPUT", OSDP_CMD_STATUS_QUERY_INPUT)
ADD_CONST("CMD_STATUS_QUERY_OUTPUT", OSDP_CMD_STATUS_QUERY_OUTPUT)
ADD_CONST("CMD_STATUS_QUERY_REMOTE", OSDP_CMD_STATUS_QUERY_REMOTE)
ADD_CONST("STATUS_REPORT_LOCAL", OSDP_STATUS_REPORT_LOCAL)
ADD_CONST("STATUS_REPORT_INPUT", OSDP_STATUS_REPORT_INPUT)
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 */
ADD_CONST("CMD_FILE_TX_FLAG_CANCEL", OSDP_CMD_FILE_TX_FLAG_CANCEL);
Expand All @@ -58,11 +58,6 @@ void pyosdp_add_module_constants(PyObject *module)
ADD_CONST("EVENT_MFGREP", OSDP_EVENT_MFGREP);
ADD_CONST("EVENT_STATUS", OSDP_EVENT_STATUS);

ADD_CONST("EVENT_STATUS_TYPE_INPUT", OSDP_EVENT_STATUS_TYPE_INPUT);
ADD_CONST("EVENT_STATUS_TYPE_OUTPUT", OSDP_EVENT_STATUS_TYPE_OUTPUT);
ADD_CONST("EVENT_STATUS_TYPE_REMOTE", OSDP_EVENT_STATUS_TYPE_REMOTE);
ADD_CONST("EVENT_STATUS_TYPE_LOCAL", OSDP_EVENT_STATUS_TYPE_LOCAL);

/* enum osdp_led_color_e */
ADD_CONST("LED_COLOR_NONE", OSDP_LED_COLOR_NONE);
ADD_CONST("LED_COLOR_RED", OSDP_LED_COLOR_RED);
Expand Down
16 changes: 8 additions & 8 deletions src/osdp_cp.c
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ static int cp_decode_response(struct osdp_pd *pd, uint8_t *buf, int len)
status_mask |= !!buf[pos++] << i;
}
event.type = OSDP_EVENT_STATUS;
event.status.type = OSDP_EVENT_STATUS_TYPE_OUTPUT;
event.status.type = OSDP_STATUS_REPORT_OUTPUT;
event.status.nr_entries = len;
event.status.mask = status_mask;
memcpy(pd->ephemeral_data, &event, sizeof(event));
Expand All @@ -522,7 +522,7 @@ static int cp_decode_response(struct osdp_pd *pd, uint8_t *buf, int len)
status_mask |= !!buf[pos++] << i;
}
event.type = OSDP_EVENT_STATUS;
event.status.type = OSDP_EVENT_STATUS_TYPE_INPUT;
event.status.type = OSDP_STATUS_REPORT_INPUT;
event.status.nr_entries = len;
event.status.mask = status_mask;
memcpy(pd->ephemeral_data, &event, sizeof(event));
Expand All @@ -535,7 +535,7 @@ static int cp_decode_response(struct osdp_pd *pd, uint8_t *buf, int len)
break;
}
event.type = OSDP_EVENT_STATUS;
event.status.type = OSDP_EVENT_STATUS_TYPE_LOCAL;
event.status.type = OSDP_STATUS_REPORT_LOCAL;
event.status.nr_entries = 2;
event.status.mask = !!buf[pos++];
event.status.mask = !!buf[pos++] << 1;
Expand All @@ -548,7 +548,7 @@ static int cp_decode_response(struct osdp_pd *pd, uint8_t *buf, int len)
break;
}
event.type = OSDP_EVENT_STATUS;
event.status.type = OSDP_EVENT_STATUS_TYPE_REMOTE;
event.status.type = OSDP_STATUS_REPORT_REMOTE;
event.status.nr_entries = 1;
event.status.mask = !!buf[pos++];
memcpy(pd->ephemeral_data, &event, sizeof(event));
Expand Down Expand Up @@ -789,10 +789,10 @@ int cp_translate_cmd(struct osdp_pd *pd, struct osdp_cmd *cmd)
case OSDP_CMD_MFG: return CMD_MFG;
case OSDP_CMD_STATUS:
switch (cmd->status.type) {
case OSDP_CMD_STATUS_QUERY_INPUT: return CMD_ISTAT;
case OSDP_CMD_STATUS_QUERY_OUTPUT: return CMD_OSTAT;
case OSDP_CMD_STATUS_QUERY_LOCAL: return CMD_LSTAT;
case OSDP_CMD_STATUS_QUERY_REMOTE: return CMD_RSTAT;
case OSDP_STATUS_REPORT_INPUT: return CMD_ISTAT;
case OSDP_STATUS_REPORT_OUTPUT: return CMD_OSTAT;
case OSDP_STATUS_REPORT_LOCAL: return CMD_LSTAT;
case OSDP_STATUS_REPORT_REMOTE: return CMD_RSTAT;
default: return -1;
}
case OSDP_CMD_KEYSET:
Expand Down
35 changes: 15 additions & 20 deletions src/osdp_pd.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,16 +153,16 @@ static int pd_translate_event(struct osdp_pd *pd, struct osdp_event *event)
break;
case OSDP_EVENT_STATUS:
switch(event->status.type) {
case OSDP_EVENT_STATUS_TYPE_INPUT:
case OSDP_STATUS_REPORT_INPUT:
reply_code = REPLY_ISTATR;
break;
case OSDP_EVENT_STATUS_TYPE_OUTPUT:
case OSDP_STATUS_REPORT_OUTPUT:
reply_code = REPLY_OSTATR;
break;
case OSDP_EVENT_STATUS_TYPE_LOCAL:
case OSDP_STATUS_REPORT_LOCAL:
reply_code = REPLY_LSTATR;
break;
case OSDP_EVENT_STATUS_TYPE_REMOTE:
case OSDP_STATUS_REPORT_REMOTE:
reply_code = REPLY_RSTATR;
break;
}
Expand Down Expand Up @@ -310,14 +310,13 @@ static int pd_decode_command(struct osdp_pd *pd, uint8_t *buf, int len)
break;
}
cmd.id = OSDP_CMD_STATUS;
cmd.status.type = OSDP_CMD_STATUS_QUERY_LOCAL;
cmd.status.type = OSDP_STATUS_REPORT_LOCAL;
if (do_command_callback(pd, &cmd)) {
break;
}
event = (struct osdp_event *)pd->ephemeral_data;
event->status.type = OSDP_EVENT_STATUS_TYPE_LOCAL;
event->status.nr_entries = cmd.status.nr_entries;
event->status.mask = cmd.status.mask;
event->type = OSDP_EVENT_STATUS;
memcpy(&event->status, &cmd.status, sizeof(cmd.status));
pd->reply_id = REPLY_LSTATR;
ret = OSDP_PD_ERR_NONE;
break;
Expand All @@ -330,15 +329,13 @@ static int pd_decode_command(struct osdp_pd *pd, uint8_t *buf, int len)
break;
}
cmd.id = OSDP_CMD_STATUS;
cmd.status.type = OSDP_CMD_STATUS_QUERY_INPUT;
cmd.status.type = OSDP_STATUS_REPORT_INPUT;
if (do_command_callback(pd, &cmd)) {
break;
}
event = (struct osdp_event *)pd->ephemeral_data;
event->type = OSDP_EVENT_STATUS;
event->status.type = OSDP_EVENT_STATUS_TYPE_INPUT;
event->status.nr_entries = cmd.status.nr_entries;
event->status.mask = cmd.status.mask;
memcpy(&event->status, &cmd.status, sizeof(cmd.status));
pd->reply_id = REPLY_ISTATR;
ret = OSDP_PD_ERR_NONE;
break;
Expand All @@ -351,14 +348,13 @@ static int pd_decode_command(struct osdp_pd *pd, uint8_t *buf, int len)
break;
}
cmd.id = OSDP_CMD_STATUS;
cmd.status.type = OSDP_CMD_STATUS_QUERY_OUTPUT;
cmd.status.type = OSDP_STATUS_REPORT_OUTPUT;
if (do_command_callback(pd, &cmd)) {
break;
}
event = (struct osdp_event *)pd->ephemeral_data;
event->status.type = OSDP_EVENT_STATUS_TYPE_OUTPUT;
event->status.nr_entries = cmd.status.nr_entries;
event->status.mask = cmd.status.mask;
event->type = OSDP_EVENT_STATUS;
memcpy(&event->status, &cmd.status, sizeof(cmd.status));
pd->reply_id = REPLY_OSTATR;
ret = OSDP_PD_ERR_NONE;
break;
Expand All @@ -367,14 +363,13 @@ static int pd_decode_command(struct osdp_pd *pd, uint8_t *buf, int len)
break;
}
cmd.id = OSDP_CMD_STATUS;
cmd.status.type = OSDP_CMD_STATUS_QUERY_REMOTE;
cmd.status.type = OSDP_STATUS_REPORT_REMOTE;
if (do_command_callback(pd, &cmd)) {
break;
}
event = (struct osdp_event *)pd->ephemeral_data;
event->status.type = OSDP_EVENT_STATUS_TYPE_REMOTE;
event->status.nr_entries = cmd.status.nr_entries;
event->status.mask = cmd.status.mask;
event->type = OSDP_EVENT_STATUS;
memcpy(&event->status, &cmd.status, sizeof(cmd.status));
pd->reply_id = REPLY_RSTATR;
ret = OSDP_PD_ERR_NONE;
break;
Expand Down
Loading

0 comments on commit 6f2dbc9

Please sign in to comment.