Skip to content

Commit 83dfb94

Browse files
committed
API: Pass readonly OSDP context to event callback
1 parent e600d30 commit 83dfb94

11 files changed

Lines changed: 33 additions & 20 deletions

File tree

examples/cpp/cp_app.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ static struct osdp_channel cp_channel = {
5050
.close = nullptr,
5151
};
5252

53-
int event_handler(void *data, int pd, struct osdp_event *event) {
53+
int event_handler(void *data, const osdp_t *ctx, int pd, struct osdp_event *event) {
5454
(void)(data);
55+
(void)(ctx);
5556

5657
std::cout << "PD" << pd << " EVENT: " << event->type << std::endl;
5758
return 0;

examples/platformio/cp.ino

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,10 @@ void init_cp_info()
5858
cp_channel.send = serial1_send_func;
5959
}
6060

61-
int event_handler(void *data, int pd, struct osdp_event *event)
61+
int event_handler(void *data, const osdp_t *ctx, int pd, struct osdp_event *event)
6262
{
6363
(void)(data);
64+
(void)(ctx);
6465

6566
Serial.println("Received an event!");
6667
return 0;

include/osdp.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1025,14 +1025,17 @@ typedef int (*pd_command_callback_t)(void *arg, struct osdp_cmd *cmd);
10251025
*
10261026
* @param arg Opaque pointer provided by the application during callback
10271027
* registration.
1028+
* @param ctx OSDP context, can be used to retrieve additional
1029+
* PD information.
10281030
* @param pd PD offset (0-indexed) of this PD in `osdp_pd_info_t *` passed to
10291031
* osdp_cp_setup()
10301032
* @param ev pointer to osdp_event struct (filled by libosdp).
10311033
*
10321034
* @retval 0 on handling the event successfully.
10331035
* @retval -ve on errors.
10341036
*/
1035-
typedef int (*cp_event_callback_t)(void *arg, int pd, struct osdp_event *ev);
1037+
typedef int (*cp_event_callback_t)(void *arg, const osdp_t *ctx, int pd,
1038+
struct osdp_event *ev);
10361039

10371040
/**
10381041
* @brief Terminal status of a submitted command/event object.

python/osdp_sys/cp.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,10 @@ static PyObject *pyosdp_cp_sc_status(pyosdp_cp_t *self, PyObject *args)
6969
return Py_BuildValue("I", bitmask);
7070
}
7171

72-
int pyosdp_cp_event_cb(void *data, int address, struct osdp_event *event)
72+
int pyosdp_cp_event_cb(void *data, const osdp_t* ctx, int address, struct osdp_event *event)
7373
{
74+
(void)(ctx);
75+
7476
pyosdp_cp_t *self = data;
7577
PyObject *arglist, *result, *event_dict;
7678

src/osdp_cp.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ enum osdp_cp_error_e {
5858
static void cp_dispatch_event(struct osdp_pd *pd,
5959
const struct osdp_event *event)
6060
{
61-
struct osdp *ctx = pd_to_osdp(pd);
61+
const struct osdp *ctx = pd_to_osdp(pd);
6262

6363
if (ctx->event_callback) {
64-
ctx->event_callback(ctx->event_callback_arg, pd->idx,
64+
ctx->event_callback(ctx->event_callback_arg, ctx, pd->idx,
6565
(struct osdp_event *)event);
6666
osdp_metrics_report(pd, OSDP_METRIC_EVENT);
6767
}
@@ -966,7 +966,7 @@ static int cp_get_online_command(struct osdp_pd *pd)
966966

967967
static void notify_pd_status(struct osdp_pd *pd, bool is_online)
968968
{
969-
struct osdp *ctx = pd_to_osdp(pd);
969+
const struct osdp *ctx = pd_to_osdp(pd);
970970
struct osdp_event evt;
971971

972972
if (!ctx->event_callback || !is_notifications_enabled(pd)) {
@@ -976,13 +976,13 @@ static void notify_pd_status(struct osdp_pd *pd, bool is_online)
976976
evt.type = OSDP_EVENT_NOTIFICATION;
977977
evt.notif.type = OSDP_NOTIFICATION_PD_STATUS;
978978
evt.notif.arg0 = is_online;
979-
ctx->event_callback(ctx->event_callback_arg, pd->idx, &evt);
979+
ctx->event_callback(ctx->event_callback_arg, ctx, pd->idx, &evt);
980980
osdp_metrics_report(pd, OSDP_METRIC_EVENT);
981981
}
982982

983983
static void notify_sc_status(struct osdp_pd *pd)
984984
{
985-
struct osdp *ctx = pd_to_osdp(pd);
985+
const struct osdp *ctx = pd_to_osdp(pd);
986986
struct osdp_event evt;
987987

988988
if (!ctx->event_callback || !is_notifications_enabled(pd)) {
@@ -993,14 +993,14 @@ static void notify_sc_status(struct osdp_pd *pd)
993993
evt.notif.type = OSDP_NOTIFICATION_SC_STATUS;
994994
evt.notif.arg0 = sc_is_active(pd);
995995
evt.notif.arg1 = sc_use_scbkd(pd);
996-
ctx->event_callback(ctx->event_callback_arg, pd->idx, &evt);
996+
ctx->event_callback(ctx->event_callback_arg, ctx, pd->idx, &evt);
997997
osdp_metrics_report(pd, OSDP_METRIC_EVENT);
998998
}
999999

10001000
void osdp_file_tx_notify_done(struct osdp_pd *pd, int file_id,
10011001
enum osdp_file_tx_outcome outcome)
10021002
{
1003-
struct osdp *ctx = pd_to_osdp(pd);
1003+
const struct osdp *ctx = pd_to_osdp(pd);
10041004
struct osdp_event evt;
10051005

10061006
if (!ctx->event_callback || !is_notifications_enabled(pd)) {
@@ -1011,7 +1011,7 @@ void osdp_file_tx_notify_done(struct osdp_pd *pd, int file_id,
10111011
evt.notif.type = OSDP_NOTIFICATION_FILE_TX_DONE;
10121012
evt.notif.arg0 = file_id;
10131013
evt.notif.arg1 = outcome;
1014-
ctx->event_callback(ctx->event_callback_arg, pd->idx, &evt);
1014+
ctx->event_callback(ctx->event_callback_arg, ctx, pd->idx, &evt);
10151015
osdp_metrics_report(pd, OSDP_METRIC_EVENT);
10161016
}
10171017

@@ -1280,7 +1280,7 @@ static void notify_command_status(struct osdp_pd *pd, int status)
12801280
{
12811281
int app_cmd;
12821282
struct osdp_event evt;
1283-
struct osdp *ctx = pd_to_osdp(pd);
1283+
const struct osdp *ctx = pd_to_osdp(pd);
12841284

12851285
if (!ctx->event_callback || !is_notifications_enabled(pd)) {
12861286
return;
@@ -1317,7 +1317,7 @@ static void notify_command_status(struct osdp_pd *pd, int status)
13171317
evt.notif.arg0 = app_cmd;
13181318
evt.notif.arg1 = status ? 0 : -1;
13191319

1320-
ctx->event_callback(ctx->event_callback_arg, pd->idx, &evt);
1320+
ctx->event_callback(ctx->event_callback_arg, ctx, pd->idx, &evt);
13211321
osdp_metrics_report(pd, OSDP_METRIC_EVENT);
13221322
}
13231323

tests/unit-tests/test-async-fuzz.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,12 @@ struct async_test_data {
2727
bool event_generated;
2828
};
2929

30-
static int async_event_callback(void *data, int pd, struct osdp_event *event)
30+
static int async_event_callback(void *data, const osdp_t *ctx, int pd, struct osdp_event *event)
3131
{
3232
struct async_test_data *d = data;
3333

3434
ARG_UNUSED(pd);
35+
ARG_UNUSED(ctx);
3536
if (event->type == OSDP_EVENT_CARDREAD) {
3637
d->event_generated = true;
3738
}

tests/unit-tests/test-commands.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ struct test_command_ctx {
4040

4141
static struct test_command_ctx g_test_ctx = {0};
4242

43-
int test_commands_event_callback(void *arg, int pd, struct osdp_event *ev)
43+
int test_commands_event_callback(void *arg, const osdp_t* cp_ctx, int pd, struct osdp_event *ev)
4444
{
45+
ARG_UNUSED(cp_ctx);
4546
ARG_UNUSED(pd);
4647
struct test_command_ctx *ctx = arg;
4748

tests/unit-tests/test-events.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ struct test_event_ctx {
2828

2929
static struct test_event_ctx g_test_ctx = {0};
3030

31-
int test_events_event_callback(void *arg, int pd, struct osdp_event *ev)
31+
int test_events_event_callback(void *arg, const osdp_t *cp_ctx, int pd, struct osdp_event *ev)
3232
{
3333
ARG_UNUSED(pd);
34+
ARG_UNUSED(cp_ctx);
3435
struct test_event_ctx *ctx = arg;
3536

3637
ctx->event_seen = true;

tests/unit-tests/test-file.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,11 @@ struct file_tx_notification {
169169

170170
static struct file_tx_notification g_notif;
171171

172-
static int event_callback(void *arg, int pd, struct osdp_event *ev)
172+
static int event_callback(void *arg, const osdp_t *ctx, int pd, struct osdp_event *ev)
173173
{
174174
ARG_UNUSED(arg);
175175
ARG_UNUSED(pd);
176+
ARG_UNUSED(ctx);
176177

177178
if (ev->type != OSDP_EVENT_NOTIFICATION) {
178179
return 0;

tests/unit-tests/test-hotplug.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@ static struct test_hotplug_ctx g_test_ctx = {0};
3333

3434
static bool wait_for_pd_online(int timeout_sec);
3535

36-
int test_hotplug_event_callback(void *arg, int pd, struct osdp_event *ev)
36+
int test_hotplug_event_callback(void *arg, const osdp_t *cp_ctx, int pd, struct osdp_event *ev)
3737
{
3838
ARG_UNUSED(pd);
39+
ARG_UNUSED(cp_ctx);
3940
struct test_hotplug_ctx *ctx = arg;
4041

4142
ctx->event_seen = true;

0 commit comments

Comments
 (0)