Skip to content

Commit

Permalink
card+PIN write emulation
Browse files Browse the repository at this point in the history
  • Loading branch information
twystd committed Oct 11, 2023
1 parent bcba783 commit b49e539
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 101 deletions.
3 changes: 2 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
- [x] controller passcodes
- [x] 8-bit burst mode write
- [x] 8-bit burst mode read
- [ ] card + PIN writer
- [x] card + PIN writer
- [ ] card + PIN read


Expand All @@ -26,6 +26,7 @@
- [ ] Buzzer
- (?) CLI auth
- HOTP
- (?) Replace CLI with API
- [ ] tweetnacl/TLS
- [ ] TCP/IP
- (?) ACL
Expand Down
2 changes: 2 additions & 0 deletions pico/core/include/cli.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ void cli_list_acl(txrx, void *);
void cli_clear_acl(txrx, void *);
void cli_write_acl(txrx, void *);
void cli_set_passcodes(char *, txrx, void *);

void cli_swipe(char *, txrx f, void *);
void keypad(char *, txrx, void *);
50 changes: 50 additions & 0 deletions pico/core/src/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,56 @@ void cli_set_passcodes(char *cmd, txrx f, void *context) {
}
}

/* Card swipe + (optional) keycode emulation.
* Extract the facility code, card number and (optionally) the keycode from
* the command and writes the card and keycode (if present) to the Wiegand
* interface.
*/
void cli_swipe(char *cmd, txrx f, void *context) {
uint32_t facility_code = FACILITY_CODE;
uint32_t card = 0;
char *token = strtok(cmd, " ,");
char *code;

if (token != NULL) {
int N = strlen(token);

if (N < 5) {
if (sscanf(cmd, "%0u", &card) < 1) {
return;
}
} else {
if (sscanf(&token[N - 5], "%05u", &card) < 1) {
return;
}

if (N == 6 && sscanf(token, "%01u", &facility_code) < 1) {
return;
} else if (N == 7 && sscanf(token, "%02u", &facility_code) < 1) {
return;
} else if (N == 8 && sscanf(token, "%03u", &facility_code) < 1) {
return;
} else if (N > 8) {
return;
}
}

if ((mode == WRITER) || (mode == EMULATOR)) {
if ((code = strtok(NULL, " ,")) == NULL) {
write_card(facility_code, card);
f(context, "CARD WRITE OK");
logd_log("CARD WRITE OK");
} else {
write_card(facility_code, card);
keypad(code, f, context);

f(context, "CARD+ WRITE OK");
logd_log("CARD+ WRITE OK");
}
}
}
}

/* Keypad emulation.
* Sends the keycode code as 4-bit burst mode Wiegand.
*
Expand Down
86 changes: 2 additions & 84 deletions pico/emulator/common/src/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ typedef void (*handler)(uint32_t, uint32_t, txrx, void *);

void help(txrx, void *);
void query(txrx, void *);
void swipe(char *cmd, txrx, void *);
void swipex(char *cmd, txrx, void *);

void on_press_button(txrx, void *);
void on_release_button(txrx, void *);
Expand Down Expand Up @@ -64,9 +62,7 @@ void execw(char *cmd, txrx f, void *context) {
} else if (strncasecmp(cmd, "release", 7) == 0) {
on_release_button(f, context);
} else if (strncasecmp(cmd, "card ", 5) == 0) {
swipe(&cmd[5], f, context);
} else if (strncasecmp(cmd, "card+ ", 6) == 0) {
swipex(&cmd[6], f, context);
cli_swipe(&cmd[5], f, context);
} else if (strncasecmp(cmd, "code ", 5) == 0) {
keypad(&cmd[1], f, context);
} else if (strncasecmp(cmd, "query", 5) == 0) {
Expand Down Expand Up @@ -95,8 +91,7 @@ void help(txrx f, void *context) {
f(context, "-----");
f(context, "Commands:");
f(context, "TIME yyyy-mm-dd hh:mm:ss Set date/time");
f(context, "CARD nnnnnn Write card to Wiegand-26 interface");
f(context, "CARD+ nnnnnn dddddd Write card+keycode to Wiegand-26 interface");
f(context, "CARD nnnnnn dddddd Write card + (optional) keycode to the Wiegand interface");
f(context, "CODE dddddd Enter keypad digits");
f(context, "QUERY Display last card read/write");
f(context, "OPEN Opens door contact relay");
Expand All @@ -109,83 +104,6 @@ void help(txrx f, void *context) {
f(context, "-----");
}

/* Card swipe emulation.
* Extract the facility code and card number and invokes the handler function.
*
*/
void swipe(char *cmd, txrx f, void *context) {
uint32_t facility_code = FACILITY_CODE;
uint32_t card = 0;
int N = strlen(cmd);
int rc;

if (N < 5) {
if ((rc = sscanf(cmd, "%0u", &card)) < 1) {
return;
}
} else {
if ((rc = sscanf(&cmd[N - 5], "%05u", &card)) < 1) {
return;
}

if (N == 6 && ((rc = sscanf(cmd, "%01u", &facility_code)) < 1)) {
return;
} else if (N == 7 && ((rc = sscanf(cmd, "%02u", &facility_code)) < 1)) {
return;
} else if (N == 8 && ((rc = sscanf(cmd, "%03u", &facility_code)) < 1)) {
return;
} else if (N > 8) {
return;
}
}

if ((mode == WRITER) || (mode == EMULATOR)) {
write_card(facility_code, card);
}

f(context, "CARD WRITE OK");
logd_log("CARD WRITE OK");
}

/* Card swipe + keycode emulation.
* Extract the facility code, card number keycode and invokes the handler function.
*
*/
void swipex(char *cmd, txrx f, void *context) {
// uint32_t facility_code = FACILITY_CODE;
// uint32_t card = 0;
// int N = strlen(cmd);
// int rc;

// if (N < 5) {
// if ((rc = sscanf(cmd, "%0u", &card)) < 1) {
// return;
// }
// } else {
// if ((rc = sscanf(&cmd[N - 5], "%05u", &card)) < 1) {
// return;
// }

// if (N == 6 && ((rc = sscanf(cmd, "%01u", &facility_code)) < 1)) {
// return;
// } else if (N == 7 && ((rc = sscanf(cmd, "%02u", &facility_code)) < 1)) {
// return;
// } else if (N == 8 && ((rc = sscanf(cmd, "%03u", &facility_code)) < 1)) {
// return;
// } else if (N > 8) {
// return;
// }
// }

if ((mode == WRITER) || (mode == EMULATOR)) {
write_card(100, 58401);
keypad("7531#", f, context);
}

f(context, "CARD+ WRITE OK");
logd_log("CARD+ WRITE OK");
}

/* Pushbutton emulation command handler.
* Opens/closes the pushbutton emulation relay (in reader mode only).
*
Expand Down
18 changes: 2 additions & 16 deletions pico/reference/common/src/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ void on_card_command(char *, handler, txrx, void *);

void on_press_button(txrx, void *);
void on_release_button(txrx, void *);
void write(uint32_t, uint32_t, txrx, void *);

void grant(uint32_t, uint32_t, txrx, void *);
void revoke(uint32_t, uint32_t, txrx, void *);
Expand Down Expand Up @@ -104,7 +103,7 @@ void execw(char *cmd, txrx f, void *context) {
} else if (strncasecmp(cmd, "format", 6) == 0) {
format(f, context);
} else if (strncasecmp(cmd, "card ", 5) == 0) {
on_card_command(&cmd[5], write, f, context);
cli_swipe(&cmd[5], f, context);
} else if (strncasecmp(cmd, "code ", 5) == 0) {
keypad(&cmd[1], f, context);
} else {
Expand All @@ -124,19 +123,6 @@ void query(txrx f, void *context) {
f(context, s);
}

/* Write card command.
* Extract the facility code and card number pushes it to the emulator queue.
*
*/
void write(uint32_t facility_code, uint32_t card, txrx f, void *context) {
if ((mode == WRITER) || (mode == EMULATOR)) {
write_card(facility_code, card);
}

f(context, "CARD WRITE OK");
logd_log("CARD WRITE OK");
}

/* Adds a card number to the ACL.
*
*/
Expand Down Expand Up @@ -299,7 +285,7 @@ void help(txrx f, void *context) {
f(context, "Commands:");
f(context, "TIME YYYY-MM-DD HH:mm:ss Set date/time");
f(context, "");
f(context, "CARD nnnnnn Write card to Wiegand-26 interface");
f(context, "CARD nnnnnn dddddd Write card + (optional) keycode to the Wiegand interface");
f(context, "CODE dddddd Enter keypad digits");
f(context, "OPEN Opens door contact relay");
f(context, "CLOSE Closes door contact relay");
Expand Down

0 comments on commit b49e539

Please sign in to comment.