Skip to content

Commit

Permalink
libosdp: Add pd_offset checks
Browse files Browse the repository at this point in the history
  • Loading branch information
sidcha committed Dec 15, 2019
1 parent e1f482f commit 73e4a0f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/osdp_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ struct osdp_secure_channel {

struct osdp_pd {
void *__parent;
int offset;
uint32_t flags;

/* OSDP specified data */
Expand Down Expand Up @@ -216,7 +217,7 @@ struct osdp_cp {
int state;

struct osdp_pd *current_pd; /* current operational pd's pointer */
int pd_offset; /* current pd's offset into ctx->pd */
int pd_offset; /* current pd's offset into ctx->pd */
};

struct osdp {
Expand Down
24 changes: 20 additions & 4 deletions src/osdp_cp.c
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ int cp_decode_response(struct osdp_pd *p, uint8_t *buf, int len)
if (ctx->notifier.keypress) {
for (i = 0; i < t1; i++) {
t2 = buf[pos + i]; /* key */
ctx->notifier.keypress(p->address, t2);
ctx->notifier.keypress(p->offset, t2);
}
}
ret = 0;
Expand All @@ -326,7 +326,7 @@ int cp_decode_response(struct osdp_pd *p, uint8_t *buf, int len)
t2 = buf[pos++]; /* length */
len |= buf[pos++] << 8;
if (ctx->notifier.cardread) {
ctx->notifier.cardread(p->address, t1, buf + pos, t2);
ctx->notifier.cardread(p->offset, t1, buf + pos, t2);
}
ret = 0;
break;
Expand All @@ -335,7 +335,7 @@ int cp_decode_response(struct osdp_pd *p, uint8_t *buf, int len)
pos++; /* skip one byte -- TODO: discuss about reader direction */
t1 = buf[pos++]; /* Key length */
if (ctx->notifier.cardread) {
ctx->notifier.cardread(p->address, OSDP_CARD_FMT_ASCII,
ctx->notifier.cardread(p->offset, OSDP_CARD_FMT_ASCII,
buf + pos, t1);
}
ret = 0;
Expand Down Expand Up @@ -761,6 +761,7 @@ osdp_cp_t *osdp_cp_setup(int num_pd, osdp_pd_info_t *info, uint8_t *master_key)
for (i = 0; i < num_pd; i++) {
osdp_pd_info_t *p = info + i;
pd = to_pd(ctx, i);
pd->offset = i;
pd->queue = calloc(1, sizeof(struct cmd_queue));
if (pd->queue == NULL) {
LOG_E(TAG "failed to alloc pd->cmd_queue");
Expand Down Expand Up @@ -815,7 +816,7 @@ void osdp_cp_refresh(osdp_cp_t *ctx)

for (i = 0; i < to_cp(ctx)->num_pd; i++) {
set_current_pd(ctx, i);
osdp_log_ctx_set(to_current_pd(ctx)->address);
osdp_log_ctx_set(i);
cp_state_update(to_current_pd(ctx));
}
}
Expand All @@ -841,6 +842,9 @@ int osdp_cp_send_cmd_output(osdp_cp_t *ctx, int pd, struct osdp_cmd_output *p)
uint8_t cmd_buf[sizeof(struct osdp_data) + sizeof(union osdp_cmd)];
struct osdp_data *cmd = (struct osdp_data *)cmd_buf;

if (ctx == NULL || pd >= to_cp(ctx)->num_pd)
return -1;

cmd->id = CMD_OUT;
cmd->len = sizeof(struct osdp_data) + sizeof(struct osdp_cmd_output);
memcpy(cmd->data, p, sizeof(struct osdp_cmd_output));
Expand All @@ -857,6 +861,9 @@ int osdp_cp_send_cmd_led(osdp_cp_t *ctx, int pd, struct osdp_cmd_led *p)
uint8_t cmd_buf[sizeof(struct osdp_data) + sizeof(union osdp_cmd)];
struct osdp_data *cmd = (struct osdp_data *)cmd_buf;

if (ctx == NULL || pd >= to_cp(ctx)->num_pd)
return -1;

cmd->id = CMD_LED;
cmd->len = sizeof(struct osdp_data) + sizeof(struct osdp_cmd_led);
memcpy(cmd->data, p, sizeof(struct osdp_cmd_led));
Expand All @@ -873,6 +880,9 @@ int osdp_cp_send_cmd_buzzer(osdp_cp_t *ctx, int pd, struct osdp_cmd_buzzer *p)
uint8_t cmd_buf[sizeof(struct osdp_data) + sizeof(union osdp_cmd)];
struct osdp_data *cmd = (struct osdp_data *)cmd_buf;

if (ctx == NULL || pd >= to_cp(ctx)->num_pd)
return -1;

cmd->id = CMD_BUZ;
cmd->len = sizeof(struct osdp_data) + sizeof(struct osdp_cmd_buzzer);
memcpy(cmd->data, p, sizeof(struct osdp_cmd_buzzer));
Expand All @@ -889,6 +899,9 @@ int osdp_cp_send_cmd_text(osdp_cp_t *ctx, int pd, struct osdp_cmd_text *p)
uint8_t cmd_buf[sizeof(struct osdp_data) + sizeof(union osdp_cmd)];
struct osdp_data *cmd = (struct osdp_data *)cmd_buf;

if (ctx == NULL || pd >= to_cp(ctx)->num_pd)
return -1;

cmd->id = CMD_TEXT;
cmd->len = sizeof(struct osdp_data) + sizeof(struct osdp_cmd_text);
memcpy(cmd->data, p, sizeof(struct osdp_cmd_text));
Expand All @@ -905,6 +918,9 @@ int osdp_cp_send_cmd_comset(osdp_cp_t *ctx, int pd, struct osdp_cmd_comset *p)
uint8_t cmd_buf[sizeof(struct osdp_data) + sizeof(union osdp_cmd)];
struct osdp_data *cmd = (struct osdp_data *)cmd_buf;

if (ctx == NULL || pd >= to_cp(ctx)->num_pd)
return -1;

cmd->id = CMD_COMSET;
cmd->len = sizeof(struct osdp_data) + sizeof(struct osdp_cmd_comset);
memcpy(cmd->data, p, sizeof(struct osdp_cmd_comset));
Expand Down
2 changes: 1 addition & 1 deletion src/osdp_pd.c
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,7 @@ osdp_pd_t *osdp_pd_setup(osdp_pd_info_t * p, uint8_t *scbk)
pd = to_pd(ctx, 0);

node_set_parent(pd, ctx);
pd->offset = 0;
pd->baud_rate = p->baud_rate;
pd->address = p->address;
pd->seq_number = -1;
Expand All @@ -538,7 +539,6 @@ osdp_pd_t *osdp_pd_setup(osdp_pd_info_t * p, uint8_t *scbk)

set_flag(pd, PD_FLAG_PD_MODE); /* used to understand operational mode */

osdp_log_ctx_set(pd->address);
LOG_I(TAG "setup complete");
return (osdp_pd_t *) ctx;

Expand Down

0 comments on commit 73e4a0f

Please sign in to comment.