Skip to content

Commit

Permalink
controller: card PIN timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
twystd committed Oct 17, 2023
1 parent ed1ef56 commit 312ef60
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 19 deletions.
3 changes: 2 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
40 changes: 24 additions & 16 deletions pico/controller/common/src/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 *);

Expand Down Expand Up @@ -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);
}
Expand All @@ -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.
Expand Down
30 changes: 29 additions & 1 deletion pico/core/src/acl.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,23 @@
#include <hardware/rtc.h>

#include <acl.h>
#include <common.h>
#include <flash.h>
#include <led.h>
#include <logd.h>
#include <sdcard.h>
#include <wiegand.h>

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.
*
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
1 change: 0 additions & 1 deletion pico/core/src/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,6 @@ void cli_acl_revoke(char *cmd, txrx f, void *context) {
}
}


/* Sets the override passcodes.
*
*/
Expand Down

0 comments on commit 312ef60

Please sign in to comment.