From 312ef609bcec991b838023431921d1846a1003d0 Mon Sep 17 00:00:00 2001 From: twystd Date: Tue, 17 Oct 2023 12:08:16 -0700 Subject: [PATCH] controller: card PIN timeout --- TODO.md | 3 ++- pico/controller/common/src/cli.c | 40 +++++++++++++++++++------------- pico/core/src/acl.c | 30 +++++++++++++++++++++++- pico/core/src/cli.c | 1 - 4 files changed, 55 insertions(+), 19 deletions(-) diff --git a/TODO.md b/TODO.md index 1578583..c70e3cf 100644 --- a/TODO.md +++ b/TODO.md @@ -13,8 +13,9 @@ - [x] 8-bit burst mode read - [x] card + PIN writer - [ ] card + PIN read - - [ ] timeout on keycode + - [x] timeout on keycode - [ ] _reference_ implementation + - [ ] rework ACL as struct (with cards, timer, etc) - [ ] FIXME in acl.c - [ ] Check mode == CONTROLLER in unlock, etc diff --git a/pico/controller/common/src/cli.c b/pico/controller/common/src/cli.c index 1f2cc3f..73f7a7d 100644 --- a/pico/controller/common/src/cli.c +++ b/pico/controller/common/src/cli.c @@ -18,7 +18,7 @@ typedef void (*handler)(uint32_t, uint32_t, txrx, void *); -void debug(txrx, void *); +void debug(txrx, void *, int); void help(txrx, void *); void query(txrx, void *); @@ -84,8 +84,10 @@ void execw(char *cmd, txrx f, void *context) { cli_acl_revoke(&cmd[7], f, context); } else if (strncasecmp(cmd, "passcodes", 9) == 0) { cli_set_passcodes(&cmd[9], f, context); - } else if (strncasecmp(cmd, "debug", 5) == 0) { - debug(f, context); + } else if (strncasecmp(cmd, "debugx", 7) == 0) { + debug(f, context, 1); + } else if (strncasecmp(cmd, "debugy", 7) == 0) { + debug(f, context, 2); } else { help(f, context); } @@ -96,26 +98,32 @@ void execw(char *cmd, txrx f, void *context) { /* -- DEBUG -- * */ -void debug(txrx f, void *context) { - int N = 6; - char *s; - - if ((s = calloc(N, 1)) != NULL) { - // ... card +void debug(txrx f, void *context, int action) { + // ... card + if (action == 1) { uint32_t v = MSG_CARD | (0x0C9C841 & 0x03ffffff); // 10058400 if (!queue_is_full(&queue)) { queue_try_add(&queue, &v); } - // ... keycode - snprintf(s, N, "%s", "12345"); - uint32_t msg = MSG_CODE | ((uint32_t)s & 0x0fffffff); // SRAM_BASE is 0x20000000 - if (queue_is_full(&queue) || !queue_try_add(&queue, &msg)) { - free(s); - } + f(context, ">> DEBUG CARD"); } - f(context, ">> DEBUG OK"); + // ... keycode + if (action == 2) { + int N = 6; + char *code; + + if ((code = calloc(N, 1)) != NULL) { + snprintf(code, N, "%s", "12345"); + uint32_t msg = MSG_CODE | ((uint32_t)code & 0x0fffffff); // SRAM_BASE is 0x20000000 + if (queue_is_full(&queue) || !queue_try_add(&queue, &msg)) { + free(code); + } else { + f(context, ">> DEBUG CODE"); + } + } + } } /* Displays the last read/write card, if any. diff --git a/pico/core/src/acl.c b/pico/core/src/acl.c index e9f4e3e..aaf09d6 100644 --- a/pico/core/src/acl.c +++ b/pico/core/src/acl.c @@ -6,16 +6,23 @@ #include #include +#include #include +#include #include #include #include CARD ACL[MAX_CARDS]; uint32_t PASSCODES[4] = {0, 0, 0, 0}; +alarm_id_t acl_PIN_alarm; + const uint32_t OVERRIDE = MASTER_PASSCODE; const int ACL_SIZE = sizeof(ACL) / sizeof(CARD); const int PASSCODES_SIZE = sizeof(PASSCODES) / sizeof(uint32_t); +const uint32_t PIN_TIMEOUT = 12500; // ms + +int64_t acl_PIN_timeout(alarm_id_t id, void *data); /* Initialises the ACL. * @@ -221,6 +228,11 @@ bool acl_revoke(uint32_t facility_code, uint32_t card) { * */ enum ACCESS acl_allowed(uint32_t facility_code, uint32_t card, const char *pin) { + if (acl_PIN_alarm > 0) { + cancel_alarm(acl_PIN_alarm); + acl_PIN_alarm = 0; + } + for (int i = 0; i < ACL_SIZE; i++) { const uint32_t card_number = ACL[i].card_number; const bool allowed = ACL[i].allowed; @@ -235,7 +247,9 @@ enum ACCESS acl_allowed(uint32_t facility_code, uint32_t card, const char *pin) } if (ACL[i].allowed && strncmp(ACL[i].PIN, "", CARD_PIN_SIZE) != 0 && strncmp(pin, "", CARD_PIN_SIZE) == 0) { - return NEEDS_PIN; + if ((acl_PIN_alarm = add_alarm_in_ms(PIN_TIMEOUT, acl_PIN_timeout, NULL, true)) >= 0) { + return NEEDS_PIN; + } } return DENIED; @@ -290,3 +304,17 @@ bool acl_passcode(const char *code) { return false; } + +int64_t acl_PIN_timeout(alarm_id_t id, void *data) { + char s[64]; + + acl_PIN_alarm = 0; + + last_card.access = DENIED; + led_blink(3); + + cardf(&last_card, s, sizeof(s), false); + logd_log(s); + + return 0; +} diff --git a/pico/core/src/cli.c b/pico/core/src/cli.c index 47ef5d4..c8deab2 100644 --- a/pico/core/src/cli.c +++ b/pico/core/src/cli.c @@ -247,7 +247,6 @@ void cli_acl_revoke(char *cmd, txrx f, void *context) { } } - /* Sets the override passcodes. * */