@@ -213,7 +213,7 @@ static int cp_build_command(struct osdp_pd *pd, uint8_t *buf, int max_len)
213
213
break ;
214
214
#ifdef CONFIG_OSDP_SC_ENABLED
215
215
case CMD_KEYSET :
216
- if (!ISSET_FLAG (pd , PD_FLAG_SC_ACTIVE )) {
216
+ if (!sc_is_active (pd )) {
217
217
LOG_ERR ("Cannot perform KEYSET without SC!" );
218
218
return OSDP_CP_ERR_GENERIC ;
219
219
}
@@ -262,7 +262,7 @@ static int cp_build_command(struct osdp_pd *pd, uint8_t *buf, int max_len)
262
262
}
263
263
264
264
#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 )) {
266
266
/**
267
267
* When SC active and current cmd is not a handshake (<= SCS_14)
268
268
* 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)
283
283
static int cp_decode_response (struct osdp_pd * pd , uint8_t * buf , int len )
284
284
{
285
285
uint32_t temp32 ;
286
- struct osdp_cp * cp = TO_CTX (pd )-> cp ;
286
+ struct osdp * ctx = pd_to_osdp (pd );
287
287
int i , ret = OSDP_CP_ERR_GENERIC , pos = 0 , t1 , t2 ;
288
288
289
289
if (len < 1 ) {
@@ -404,10 +404,10 @@ static int cp_decode_response(struct osdp_pd *pd, uint8_t *buf, int len)
404
404
if ((len - REPLY_KEYPPAD_DATA_LEN ) != t1 ) {
405
405
break ;
406
406
}
407
- if (cp -> notifier .keypress ) {
407
+ if (ctx -> notifier .keypress ) {
408
408
for (i = 0 ; i < t1 ; i ++ ) {
409
409
t2 = buf [pos + i ]; /* key data */
410
- cp -> notifier .keypress (pd -> idx , t2 );
410
+ ctx -> notifier .keypress (pd -> idx , t2 );
411
411
}
412
412
}
413
413
ret = OSDP_CP_ERR_NONE ;
@@ -423,8 +423,8 @@ static int cp_decode_response(struct osdp_pd *pd, uint8_t *buf, int len)
423
423
if ((len - REPLY_RAW_DATA_LEN ) != t2 ) {
424
424
break ;
425
425
}
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 );
428
428
}
429
429
ret = OSDP_CP_ERR_NONE ;
430
430
break ;
@@ -438,8 +438,8 @@ static int cp_decode_response(struct osdp_pd *pd, uint8_t *buf, int len)
438
438
if ((len - REPLY_FMT_DATA_LEN ) != t1 ) {
439
439
break ;
440
440
}
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 ,
443
443
buf + pos , t1 );
444
444
}
445
445
ret = OSDP_CP_ERR_NONE ;
@@ -465,7 +465,7 @@ static int cp_decode_response(struct osdp_pd *pd, uint8_t *buf, int len)
465
465
for (i = 0 ; i < 16 ; i ++ ) {
466
466
pd -> sc .pd_cryptogram [i ] = buf [pos ++ ];
467
467
}
468
- osdp_compute_session_keys (TO_CTX ( pd ) );
468
+ osdp_compute_session_keys (pd );
469
469
if (osdp_verify_pd_cryptogram (pd ) != 0 ) {
470
470
LOG_ERR ("Failed to verify PD cryptogram" );
471
471
return OSDP_CP_ERR_GENERIC ;
@@ -479,7 +479,7 @@ static int cp_decode_response(struct osdp_pd *pd, uint8_t *buf, int len)
479
479
for (i = 0 ; i < 16 ; i ++ ) {
480
480
pd -> sc .r_mac [i ] = buf [pos ++ ];
481
481
}
482
- SET_FLAG (pd , PD_FLAG_SC_ACTIVE );
482
+ sc_activate (pd );
483
483
ret = OSDP_CP_ERR_NONE ;
484
484
break ;
485
485
#endif /* CONFIG_OSDP_SC_ENABLED */
@@ -592,7 +592,7 @@ static void cp_flush_command_queue(struct osdp_pd *pd)
592
592
593
593
static inline void cp_set_offline (struct osdp_pd * pd )
594
594
{
595
- CLEAR_FLAG (pd , PD_FLAG_SC_ACTIVE );
595
+ sc_deactivate (pd );
596
596
pd -> state = OSDP_CP_STATE_OFFLINE ;
597
597
pd -> tstamp = osdp_millis_now ();
598
598
}
@@ -609,6 +609,14 @@ static inline void cp_set_state(struct osdp_pd *pd, enum osdp_cp_state_e state)
609
609
CLEAR_FLAG (pd , PD_FLAG_AWAIT_RESP );
610
610
}
611
611
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
+
612
620
/**
613
621
* Note: This method must not dequeue cmd unless it reaches an invalid state.
614
622
*/
@@ -729,9 +737,7 @@ static int state_update(struct osdp_pd *pd)
729
737
switch (pd -> state ) {
730
738
case OSDP_CP_STATE_ONLINE :
731
739
#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 )) {
735
741
LOG_INF ("Retry SC after retry timeout" );
736
742
cp_set_state (pd , OSDP_CP_STATE_SC_INIT );
737
743
break ;
@@ -773,7 +779,7 @@ static int state_update(struct osdp_pd *pd)
773
779
cp_set_offline (pd );
774
780
}
775
781
#ifdef CONFIG_OSDP_SC_ENABLED
776
- if (ISSET_FLAG (pd , PD_FLAG_SC_CAPABLE )) {
782
+ if (sc_is_capable (pd )) {
777
783
CLEAR_FLAG (pd , PD_FLAG_SC_SCBKD_DONE );
778
784
CLEAR_FLAG (pd , PD_FLAG_SC_USE_SCBKD );
779
785
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)
870
876
}
871
877
872
878
for (i = 0 ; i < NUM_PD (ctx ); i ++ ) {
873
- pd = TO_PD (ctx , i );
879
+ pd = osdp_to_pd (ctx , i );
874
880
p = osdp_cmd_alloc (pd );
875
881
if (p == NULL ) {
876
882
return -1 ;
@@ -915,7 +921,7 @@ int osdp_cp_set_callback_key_press(int (*cb)(int address, uint8_t key))
915
921
{
916
922
struct osdp * ctx = osdp_get_ctx ();
917
923
918
- ctx -> cp -> notifier .keypress = cb ;
924
+ ctx -> notifier .keypress = cb ;
919
925
920
926
return 0 ;
921
927
}
@@ -925,7 +931,7 @@ int osdp_cp_set_callback_card_read(
925
931
{
926
932
struct osdp * ctx = osdp_get_ctx ();
927
933
928
- TO_CP ( ctx ) -> notifier .cardread = cb ;
934
+ ctx -> notifier .cardread = cb ;
929
935
930
936
return 0 ;
931
937
}
@@ -940,7 +946,7 @@ int osdp_cp_send_command(int pd, struct osdp_cmd *cmd)
940
946
LOG_ERR ("Invalid PD number" );
941
947
return -1 ;
942
948
}
943
- if (TO_PD (ctx , pd )-> state != OSDP_CP_STATE_ONLINE ) {
949
+ if (osdp_to_pd (ctx , pd )-> state != OSDP_CP_STATE_ONLINE ) {
944
950
LOG_WRN ("PD not online" );
945
951
return -1 ;
946
952
}
@@ -970,12 +976,12 @@ int osdp_cp_send_command(int pd, struct osdp_cmd *cmd)
970
976
return -1 ;
971
977
}
972
978
973
- p = osdp_cmd_alloc (TO_PD (ctx , pd ));
979
+ p = osdp_cmd_alloc (osdp_to_pd (ctx , pd ));
974
980
if (p == NULL ) {
975
981
return -1 ;
976
982
}
977
983
memcpy (p , cmd , sizeof (struct osdp_cmd ));
978
984
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 );
980
986
return 0 ;
981
987
}
0 commit comments