Skip to content

Commit

Permalink
PD: Change app notification of incoming commands to polling to simpli…
Browse files Browse the repository at this point in the history
…fy API

Signed-off-by: Siddharth Chandrasekaran <[email protected]>
  • Loading branch information
sidcha committed Jun 28, 2020
1 parent aeb3fd3 commit ac125fa
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 147 deletions.
10 changes: 1 addition & 9 deletions include/osdp.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,7 @@ int osdp_cp_send_cmd_keyset(osdp_t *ctx, struct osdp_cmd_keyset *p);
osdp_t *osdp_pd_setup(osdp_pd_info_t * info, uint8_t *scbk);
void osdp_pd_teardown(osdp_t *ctx);
void osdp_pd_refresh(osdp_t *ctx);

/* --- PD application command call backs --- */

void osdp_pd_set_callback_cmd_led(osdp_t *ctx, int (*cb) (struct osdp_cmd_led *p));
void osdp_pd_set_callback_cmd_buzzer(osdp_t *ctx, int (*cb) (struct osdp_cmd_buzzer *p));
void osdp_pd_set_callback_cmd_output(osdp_t *ctx, int (*cb) (struct osdp_cmd_output *p));
void osdp_pd_set_callback_cmd_text(osdp_t *ctx, int (*cb) (struct osdp_cmd_text *p));
void osdp_pd_set_callback_cmd_comset(osdp_t *ctx, int (*cb) (struct osdp_cmd_comset *p));
void osdp_pd_set_callback_cmd_keyset(osdp_t *ctx, int (*cb) (struct osdp_cmd_keyset *p));
int osdp_pd_get_cmd(osdp_t *ctx, struct osdp_cmd *cmd);

/* ============================= Common Methods ============================= */

Expand Down
10 changes: 10 additions & 0 deletions include/osdp_cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@

#include <stdint.h>

enum osdp_cmd_e {
OSDP_CMD_OUTPUT = 1,
OSDP_CMD_LED,
OSDP_CMD_BUZZER,
OSDP_CMD_TEXT,
OSDP_CMD_KEYSET,
OSDP_CMD_COMSET,
OSDP_CMD_SENTINEL
};

/* CMD_OUT */
struct osdp_cmd_output {
uint8_t output_no;
Expand Down
34 changes: 26 additions & 8 deletions osdpctl/cmd_start.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,25 @@ int pd_cmd_comset_handler(struct osdp_cmd_comset *p)
return 0;
}

int pd_cmd_handler(struct osdp_cmd *cmd)
{
switch(cmd->id) {
case OSDP_CMD_OUTPUT:
return pd_cmd_output_handler(&cmd->output);
case OSDP_CMD_LED:
return pd_cmd_led_handler(&cmd->led);
case OSDP_CMD_BUZZER:
return pd_cmd_buzzer_handler(&cmd->buzzer);
case OSDP_CMD_TEXT:
return pd_cmd_text_handler(&cmd->text);
case OSDP_CMD_COMSET:
return pd_cmd_comset_handler(&cmd->comset);
case OSDP_CMD_KEYSET:
return pd_cmd_keyset_handler(&cmd->keyset);
}
return -1;
}

int cp_keypress_handler(int pd, uint8_t key)
{
printf("CP: PD[%d]: keypressed: 0x%02x\n", pd, key);
Expand Down Expand Up @@ -205,6 +224,7 @@ int cmd_handler_start(int argc, char *argv[], void *data)
struct config_pd_s *pd;
uint8_t *scbk, scbk_buf[16];
struct config_s *c = data;
struct osdp_cmd cmd;

if (argc < 1) {
printf ("Error: must pass a config file\n");
Expand Down Expand Up @@ -265,21 +285,19 @@ int cmd_handler_start(int argc, char *argv[], void *data)
printf("Failed to setup PD context\n");
return -1;
}
osdp_pd_set_callback_cmd_led(c->pd_ctx, pd_cmd_led_handler);
osdp_pd_set_callback_cmd_buzzer(c->pd_ctx, pd_cmd_buzzer_handler);
osdp_pd_set_callback_cmd_output(c->pd_ctx, pd_cmd_output_handler);
osdp_pd_set_callback_cmd_text(c->pd_ctx, pd_cmd_text_handler);
osdp_pd_set_callback_cmd_comset(c->pd_ctx, pd_cmd_comset_handler);
osdp_pd_set_callback_cmd_keyset(c->pd_ctx, pd_cmd_keyset_handler);
}

free(info_arr);

while (1) {
if (c->mode == CONFIG_MODE_CP)
if (c->mode == CONFIG_MODE_CP) {
osdp_cp_refresh(c->cp_ctx);
else
} else {
osdp_pd_refresh(c->pd_ctx);
if (osdp_pd_get_cmd(c->pd_ctx, &cmd) == 0) {
pd_cmd_handler(&cmd);
}
}
process_commands(c);
usleep(20 * 1000);
}
Expand Down
9 changes: 7 additions & 2 deletions sample/pd_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ int sample_pd_recv_func(void *data, uint8_t *buf, int len)
int main()
{
struct osdp_t *ctx;
struct osdp_cmd cmd;
struct pd_cap cap[] = {
{
.function_code = CAP_READER_LED_CONTROL,
Expand Down Expand Up @@ -73,9 +74,13 @@ int main()
}

while (1) {
// your application code.

osdp_pd_refresh(ctx);
if (osdp_pd_get_cmd(ctx, &cmd) == 0) {
// do something with command.
}

// your application code.
u sleep(1000);
}

return 0;
Expand Down
17 changes: 1 addition & 16 deletions src/include/osdp_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,15 +158,6 @@ struct osdp_cmd_queue {
struct osdp_cmd *back;
};

struct osdp_pd_cmd_callback {
int (*led) (struct osdp_cmd_led *p);
int (*buzzer) (struct osdp_cmd_buzzer *p);
int (*text) (struct osdp_cmd_text *p);
int (*output) (struct osdp_cmd_output *p);
int (*comset) (struct osdp_cmd_comset *p);
int (*keyset) (struct osdp_cmd_keyset *p);
};

struct osdp_secure_channel {
uint8_t scbk[16];
uint8_t s_enc[16];
Expand Down Expand Up @@ -207,11 +198,7 @@ struct osdp_pd {
struct osdp_channel channel;
struct osdp_secure_channel sc;

/* CP mode only data */
struct osdp_cmd_queue queue;

/* callbacks */
struct osdp_pd_cmd_callback cmd_cb;
};

struct osdp_cp {
Expand All @@ -221,8 +208,6 @@ struct osdp_cp {
int num_pd;
int state;

struct osdp_slab *cmd_slab;

struct osdp_pd *current_pd; /* current operational pd's pointer */
int pd_offset; /* current pd's offset into ctx->pd */
};
Expand All @@ -233,7 +218,7 @@ struct osdp {
struct osdp_cp_notifiers notifier;

uint8_t sc_master_key[16];

struct osdp_slab *cmd_slab;
struct osdp_cp *cp;
struct osdp_pd *pd;
};
Expand Down
22 changes: 11 additions & 11 deletions src/osdp_cp.c
Original file line number Diff line number Diff line change
Expand Up @@ -436,14 +436,14 @@ int cp_process_reply(struct osdp_pd *p)

inline void cp_free_command(struct osdp *ctx, struct osdp_cmd *cmd)
{
osdp_slab_free(to_cp(ctx)->cmd_slab, cmd);
osdp_slab_free(ctx->cmd_slab, cmd);
}

int cp_alloc_command(struct osdp *ctx, struct osdp_cmd **cmd)
{
void *p;

p = osdp_slab_alloc(to_cp(ctx)->cmd_slab);
p = osdp_slab_alloc(ctx->cmd_slab);
if (p == NULL) {
LOG_W(TAG "Failed to alloc cmd");
return -1;
Expand Down Expand Up @@ -744,6 +744,13 @@ osdp_t *osdp_cp_setup(int num_pd, osdp_pd_info_t *info, uint8_t *master_key)
if (master_key != NULL)
memcpy(ctx->sc_master_key, master_key, 16);

ctx->cmd_slab = osdp_slab_init(sizeof(struct osdp_cmd),
OSDP_CP_CMD_POOL_SIZE * num_pd);
if (ctx->cmd_slab == NULL) {
LOG_E(TAG "failed to alloc struct osdp_cp_cmd_slab");
goto malloc_err;
}

ctx->cp = calloc(1, sizeof(struct osdp_cp));
if (ctx->cp == NULL) {
LOG_E(TAG "failed to alloc struct osdp_cp");
Expand All @@ -753,13 +760,6 @@ osdp_t *osdp_cp_setup(int num_pd, osdp_pd_info_t *info, uint8_t *master_key)
node_set_parent(cp, ctx);
cp->num_pd = num_pd;

cp->cmd_slab = osdp_slab_init(sizeof(struct osdp_cmd),
OSDP_CP_CMD_POOL_SIZE);
if (cp->cmd_slab == NULL) {
LOG_E(TAG "failed to alloc struct osdp_cp_cmd_slab");
goto malloc_err;
}

ctx->pd = calloc(1, sizeof(struct osdp_pd) * num_pd);
if (ctx->pd == NULL) {
LOG_E(TAG "failed to alloc struct osdp_pd[]");
Expand Down Expand Up @@ -790,7 +790,7 @@ void osdp_cp_teardown(osdp_t *ctx)
{
if (ctx != NULL) {
safe_free(to_pd(ctx, 0));
osdp_slab_del(to_cp(ctx)->cmd_slab);
osdp_slab_del(to_osdp(ctx)->cmd_slab);
safe_free(to_cp(ctx));
safe_free(ctx);
}
Expand Down Expand Up @@ -938,7 +938,7 @@ int osdp_cp_send_cmd_keyset(osdp_t *ctx, struct osdp_cmd_keyset *p)
return 1;
}

if (osdp_slab_blocks(to_cp(ctx)->cmd_slab) < NUM_PD(ctx)) {
if (osdp_slab_blocks(to_osdp(ctx)->cmd_slab) < NUM_PD(ctx)) {
LOG_W(TAG "Out of slab space for keyset operation");
}

Expand Down
Loading

0 comments on commit ac125fa

Please sign in to comment.