@@ -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)
283283static 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
593593static 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