Skip to content

Commit f4e3f2b

Browse files
sidchacarlescufi
authored andcommitted
mgmt/osdp: Add inline methods for flag checks
The raw, flags check has become a bit excessive and has begun to affect code readability. Provide inline functions for those accesses that are frequent. Also, get rid of `struct osdp_cp` as it can be fully represented by `struct osdp` itself. Signed-off-by: Siddharth Chandrasekaran <[email protected]>
1 parent b31e708 commit f4e3f2b

File tree

7 files changed

+119
-96
lines changed

7 files changed

+119
-96
lines changed

subsys/mgmt/osdp/src/osdp.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ struct osdp_device {
4141
};
4242

4343
static struct osdp osdp_ctx;
44-
static struct osdp_cp osdp_cp_ctx;
4544
static struct osdp_pd osdp_pd_ctx[CONFIG_OSDP_NUM_CONNECTED_PD];
4645
static struct osdp_device osdp_device;
4746
static struct k_thread osdp_refresh_thread;
@@ -142,17 +141,15 @@ static struct osdp *osdp_build_ctx(struct osdp_channel *channel)
142141
}
143142
#endif
144143
ctx = &osdp_ctx;
145-
ctx->cp = &osdp_cp_ctx;
146-
ctx->cp->__parent = ctx;
147-
ctx->cp->num_pd = CONFIG_OSDP_NUM_CONNECTED_PD;
144+
ctx->num_pd = CONFIG_OSDP_NUM_CONNECTED_PD;
148145
ctx->pd = &osdp_pd_ctx[0];
149146
SET_CURRENT_PD(ctx, 0);
150147

151148
for (i = 0; i < CONFIG_OSDP_NUM_CONNECTED_PD; i++) {
152-
pd = TO_PD(ctx, i);
149+
pd = osdp_to_pd(ctx, i);
153150
pd->idx = i;
154151
pd->seq_number = -1;
155-
pd->__parent = ctx;
152+
pd->osdp_ctx = ctx;
156153
pd->address = pd_adddres[i];
157154
pd->baud_rate = CONFIG_OSDP_UART_BAUD_RATE;
158155
memcpy(&pd->channel, channel, sizeof(struct osdp_channel));

subsys/mgmt/osdp/src/osdp_common.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ uint32_t osdp_get_sc_status_mask(void)
189189
struct osdp *ctx = osdp_get_ctx();
190190

191191
for (i = 0; i < NUM_PD(ctx); i++) {
192-
pd = TO_PD(ctx, i);
192+
pd = osdp_to_pd(ctx, i);
193193
if (ISSET_FLAG(pd, PD_FLAG_SC_ACTIVE)) {
194194
mask |= 1 << i;
195195
}

subsys/mgmt/osdp/src/osdp_common.h

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,15 @@
2424
#define BYTE_2(x) (uint8_t)(((x) >> 16) & 0xFF)
2525
#define BYTE_3(x) (uint8_t)(((x) >> 24) & 0xFF)
2626

27-
/* casting helpers */
28-
#define TO_OSDP(p) ((struct osdp *)p)
29-
#define TO_CP(p) (((struct osdp *)(p))->cp)
30-
#define TO_PD(p, i) (((struct osdp *)(p))->pd + i)
31-
#define TO_CTX(p) ((struct osdp *)p->__parent)
32-
33-
#define GET_CURRENT_PD(p) (TO_CP(p)->current_pd)
27+
#define GET_CURRENT_PD(p) ((p)->current_pd)
3428
#define SET_CURRENT_PD(p, i) \
3529
do { \
36-
TO_CP(p)->current_pd = TO_PD(p, i); \
37-
TO_CP(p)->pd_offset = i; \
30+
(p)->current_pd = osdp_to_pd(p, i); \
3831
} while (0)
3932
#define PD_MASK(ctx) \
40-
(uint32_t)((1 << (TO_CP(ctx)->num_pd)) - 1)
33+
(uint32_t)((1 << ((ctx)->num_pd)) - 1)
4134
#define AES_PAD_LEN(x) ((x + 16 - 1) & (~(16 - 1)))
42-
#define NUM_PD(ctx) (TO_CP(ctx)->num_pd)
35+
#define NUM_PD(ctx) ((ctx)->num_pd)
4336
#define OSDP_COMMAND_DATA_MAX_LEN sizeof(struct osdp_cmd)
4437

4538
/**
@@ -414,7 +407,7 @@ struct osdp_secure_channel {
414407
#endif
415408

416409
struct osdp_pd {
417-
void *__parent;
410+
void *osdp_ctx;
418411
int idx;
419412
uint32_t flags;
420413

@@ -449,23 +442,16 @@ struct osdp_pd {
449442
#endif
450443
};
451444

452-
struct osdp_cp {
453-
void *__parent;
454-
uint32_t flags;
455-
int num_pd;
456-
struct osdp_pd *current_pd; /* current operational pd's pointer */
457-
int pd_offset; /* current pd's offset into ctx->pd */
458-
struct osdp_notifiers notifier;
459-
};
460-
461445
struct osdp {
462446
int magic;
463447
uint32_t flags;
464-
struct osdp_cp *cp;
448+
int num_pd;
449+
struct osdp_pd *current_pd; /* current operational pd's pointer */
465450
struct osdp_pd *pd;
466451
#ifdef CONFIG_OSDP_SC_ENABLED
467452
uint8_t sc_master_key[16];
468453
#endif
454+
struct osdp_notifiers notifier;
469455
};
470456

471457
/* from osdp_phy.c */
@@ -503,7 +489,7 @@ void osdp_decrypt(uint8_t *key, uint8_t *iv, uint8_t *data, int len);
503489

504490
/* from osdp_sc.c */
505491
void osdp_compute_scbk(struct osdp_pd *pd, uint8_t *scbk);
506-
void osdp_compute_session_keys(struct osdp *ctx);
492+
void osdp_compute_session_keys(struct osdp_pd *pd);
507493
void osdp_compute_cp_cryptogram(struct osdp_pd *pd);
508494
int osdp_verify_cp_cryptogram(struct osdp_pd *pd);
509495
void osdp_compute_pd_cryptogram(struct osdp_pd *pd);
@@ -520,4 +506,44 @@ void osdp_fill_random(uint8_t *buf, int len);
520506
int osdp_setup(struct osdp *ctx, uint8_t *key);
521507
void osdp_update(struct osdp *ctx);
522508

509+
static inline struct osdp *pd_to_osdp(struct osdp_pd *pd)
510+
{
511+
return pd->osdp_ctx;
512+
}
513+
514+
static inline struct osdp_pd *osdp_to_pd(struct osdp *ctx, int pd_idx)
515+
{
516+
return ctx->pd + pd_idx;
517+
}
518+
519+
static inline bool is_pd_mode(struct osdp_pd *pd)
520+
{
521+
return ISSET_FLAG(pd, PD_FLAG_PD_MODE);
522+
}
523+
524+
static inline bool is_cp_mode(struct osdp_pd *pd)
525+
{
526+
return !ISSET_FLAG(pd, PD_FLAG_PD_MODE);
527+
}
528+
529+
static inline bool sc_is_capable(struct osdp_pd *pd)
530+
{
531+
return ISSET_FLAG(pd, PD_FLAG_SC_CAPABLE);
532+
}
533+
534+
static inline bool sc_is_active(struct osdp_pd *pd)
535+
{
536+
return ISSET_FLAG(pd, PD_FLAG_SC_ACTIVE);
537+
}
538+
539+
static inline void sc_activate(struct osdp_pd *pd)
540+
{
541+
SET_FLAG(pd, PD_FLAG_SC_ACTIVE);
542+
}
543+
544+
static inline void sc_deactivate(struct osdp_pd *pd)
545+
{
546+
CLEAR_FLAG(pd, PD_FLAG_SC_ACTIVE);
547+
}
548+
523549
#endif /* _OSDP_COMMON_H_ */

subsys/mgmt/osdp/src/osdp_cp.c

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ static int cp_build_command(struct osdp_pd *pd, uint8_t *buf, int max_len)
213213
break;
214214
#ifdef CONFIG_OSDP_SC_ENABLED
215215
case CMD_KEYSET:
216-
if (!ISSET_FLAG(pd, PD_FLAG_SC_ACTIVE)) {
216+
if (!sc_is_active(pd)) {
217217
LOG_ERR("Cannot perform KEYSET without SC!");
218218
return OSDP_CP_ERR_GENERIC;
219219
}
@@ -262,7 +262,7 @@ static int cp_build_command(struct osdp_pd *pd, uint8_t *buf, int max_len)
262262
}
263263

264264
#ifdef CONFIG_OSDP_SC_ENABLED
265-
if (smb && (smb[1] > SCS_14) && ISSET_FLAG(pd, PD_FLAG_SC_ACTIVE)) {
265+
if (smb && (smb[1] > SCS_14) && sc_is_active(pd)) {
266266
/**
267267
* When SC active and current cmd is not a handshake (<= SCS_14)
268268
* then we must set SCS type to 17 if this message has data
@@ -283,7 +283,7 @@ static int cp_build_command(struct osdp_pd *pd, uint8_t *buf, int max_len)
283283
static int cp_decode_response(struct osdp_pd *pd, uint8_t *buf, int len)
284284
{
285285
uint32_t temp32;
286-
struct osdp_cp *cp = TO_CTX(pd)->cp;
286+
struct osdp *ctx = pd_to_osdp(pd);
287287
int i, ret = OSDP_CP_ERR_GENERIC, pos = 0, t1, t2;
288288

289289
if (len < 1) {
@@ -404,10 +404,10 @@ static int cp_decode_response(struct osdp_pd *pd, uint8_t *buf, int len)
404404
if ((len - REPLY_KEYPPAD_DATA_LEN) != t1) {
405405
break;
406406
}
407-
if (cp->notifier.keypress) {
407+
if (ctx->notifier.keypress) {
408408
for (i = 0; i < t1; i++) {
409409
t2 = buf[pos + i]; /* key data */
410-
cp->notifier.keypress(pd->idx, t2);
410+
ctx->notifier.keypress(pd->idx, t2);
411411
}
412412
}
413413
ret = OSDP_CP_ERR_NONE;
@@ -423,8 +423,8 @@ static int cp_decode_response(struct osdp_pd *pd, uint8_t *buf, int len)
423423
if ((len - REPLY_RAW_DATA_LEN) != t2) {
424424
break;
425425
}
426-
if (cp->notifier.cardread) {
427-
cp->notifier.cardread(pd->idx, t1, buf + pos, t2);
426+
if (ctx->notifier.cardread) {
427+
ctx->notifier.cardread(pd->idx, t1, buf + pos, t2);
428428
}
429429
ret = OSDP_CP_ERR_NONE;
430430
break;
@@ -438,8 +438,8 @@ static int cp_decode_response(struct osdp_pd *pd, uint8_t *buf, int len)
438438
if ((len - REPLY_FMT_DATA_LEN) != t1) {
439439
break;
440440
}
441-
if (cp->notifier.cardread) {
442-
cp->notifier.cardread(pd->idx, OSDP_CARD_FMT_ASCII,
441+
if (ctx->notifier.cardread) {
442+
ctx->notifier.cardread(pd->idx, OSDP_CARD_FMT_ASCII,
443443
buf + pos, t1);
444444
}
445445
ret = OSDP_CP_ERR_NONE;
@@ -465,7 +465,7 @@ static int cp_decode_response(struct osdp_pd *pd, uint8_t *buf, int len)
465465
for (i = 0; i < 16; i++) {
466466
pd->sc.pd_cryptogram[i] = buf[pos++];
467467
}
468-
osdp_compute_session_keys(TO_CTX(pd));
468+
osdp_compute_session_keys(pd);
469469
if (osdp_verify_pd_cryptogram(pd) != 0) {
470470
LOG_ERR("Failed to verify PD cryptogram");
471471
return OSDP_CP_ERR_GENERIC;
@@ -479,7 +479,7 @@ static int cp_decode_response(struct osdp_pd *pd, uint8_t *buf, int len)
479479
for (i = 0; i < 16; i++) {
480480
pd->sc.r_mac[i] = buf[pos++];
481481
}
482-
SET_FLAG(pd, PD_FLAG_SC_ACTIVE);
482+
sc_activate(pd);
483483
ret = OSDP_CP_ERR_NONE;
484484
break;
485485
#endif /* CONFIG_OSDP_SC_ENABLED */
@@ -592,7 +592,7 @@ static void cp_flush_command_queue(struct osdp_pd *pd)
592592

593593
static inline void cp_set_offline(struct osdp_pd *pd)
594594
{
595-
CLEAR_FLAG(pd, PD_FLAG_SC_ACTIVE);
595+
sc_deactivate(pd);
596596
pd->state = OSDP_CP_STATE_OFFLINE;
597597
pd->tstamp = osdp_millis_now();
598598
}
@@ -609,6 +609,14 @@ static inline void cp_set_state(struct osdp_pd *pd, enum osdp_cp_state_e state)
609609
CLEAR_FLAG(pd, PD_FLAG_AWAIT_RESP);
610610
}
611611

612+
#ifdef CONFIG_OSDP_SC_ENABLED
613+
static inline bool cp_sc_should_retry(struct osdp_pd *pd)
614+
{
615+
return (sc_is_capable(pd) && !sc_is_active(pd) &&
616+
osdp_millis_since(pd->sc_tstamp) > OSDP_PD_SC_RETRY_MS);
617+
}
618+
#endif
619+
612620
/**
613621
* Note: This method must not dequeue cmd unless it reaches an invalid state.
614622
*/
@@ -729,9 +737,7 @@ static int state_update(struct osdp_pd *pd)
729737
switch (pd->state) {
730738
case OSDP_CP_STATE_ONLINE:
731739
#ifdef CONFIG_OSDP_SC_ENABLED
732-
if (ISSET_FLAG(pd, PD_FLAG_SC_ACTIVE) == false &&
733-
ISSET_FLAG(pd, PD_FLAG_SC_CAPABLE) == true &&
734-
osdp_millis_since(pd->sc_tstamp) > OSDP_PD_SC_RETRY_MS) {
740+
if (cp_sc_should_retry(pd)) {
735741
LOG_INF("Retry SC after retry timeout");
736742
cp_set_state(pd, OSDP_CP_STATE_SC_INIT);
737743
break;
@@ -773,7 +779,7 @@ static int state_update(struct osdp_pd *pd)
773779
cp_set_offline(pd);
774780
}
775781
#ifdef CONFIG_OSDP_SC_ENABLED
776-
if (ISSET_FLAG(pd, PD_FLAG_SC_CAPABLE)) {
782+
if (sc_is_capable(pd)) {
777783
CLEAR_FLAG(pd, PD_FLAG_SC_SCBKD_DONE);
778784
CLEAR_FLAG(pd, PD_FLAG_SC_USE_SCBKD);
779785
cp_set_state(pd, OSDP_CP_STATE_SC_INIT);
@@ -870,7 +876,7 @@ static int osdp_cp_send_command_keyset(struct osdp_cmd_keyset *cmd)
870876
}
871877

872878
for (i = 0; i < NUM_PD(ctx); i++) {
873-
pd = TO_PD(ctx, i);
879+
pd = osdp_to_pd(ctx, i);
874880
p = osdp_cmd_alloc(pd);
875881
if (p == NULL) {
876882
return -1;
@@ -915,7 +921,7 @@ int osdp_cp_set_callback_key_press(int (*cb)(int address, uint8_t key))
915921
{
916922
struct osdp *ctx = osdp_get_ctx();
917923

918-
ctx->cp->notifier.keypress = cb;
924+
ctx->notifier.keypress = cb;
919925

920926
return 0;
921927
}
@@ -925,7 +931,7 @@ int osdp_cp_set_callback_card_read(
925931
{
926932
struct osdp *ctx = osdp_get_ctx();
927933

928-
TO_CP(ctx)->notifier.cardread = cb;
934+
ctx->notifier.cardread = cb;
929935

930936
return 0;
931937
}
@@ -940,7 +946,7 @@ int osdp_cp_send_command(int pd, struct osdp_cmd *cmd)
940946
LOG_ERR("Invalid PD number");
941947
return -1;
942948
}
943-
if (TO_PD(ctx, pd)->state != OSDP_CP_STATE_ONLINE) {
949+
if (osdp_to_pd(ctx, pd)->state != OSDP_CP_STATE_ONLINE) {
944950
LOG_WRN("PD not online");
945951
return -1;
946952
}
@@ -970,12 +976,12 @@ int osdp_cp_send_command(int pd, struct osdp_cmd *cmd)
970976
return -1;
971977
}
972978

973-
p = osdp_cmd_alloc(TO_PD(ctx, pd));
979+
p = osdp_cmd_alloc(osdp_to_pd(ctx, pd));
974980
if (p == NULL) {
975981
return -1;
976982
}
977983
memcpy(p, cmd, sizeof(struct osdp_cmd));
978984
p->id = cmd_id; /* translate to internal */
979-
osdp_cmd_enqueue(TO_PD(ctx, pd), p);
985+
osdp_cmd_enqueue(osdp_to_pd(ctx, pd), p);
980986
return 0;
981987
}

0 commit comments

Comments
 (0)