summaryrefslogtreecommitdiff
path: root/kbc.c
diff options
context:
space:
mode:
Diffstat (limited to 'kbc.c')
-rw-r--r--kbc.c98
1 files changed, 93 insertions, 5 deletions
diff --git a/kbc.c b/kbc.c
index f343012..a1f70b8 100644
--- a/kbc.c
+++ b/kbc.c
@@ -9,12 +9,16 @@
#include <util/delay.h>
#ifndef PIN
#error need to define a PIN
#endif
+#ifndef MASTER_PIN
+#error need to define a MASTER_PIN
+#endif
+
/* port B RJ45
* 0
* 1 BEEP (OC1A)
* 2 SS up:8
* 3 MOSI up:2 was: LOCK - output broken!
* 4 MISO up:4 UNLOCK
@@ -51,12 +55,14 @@ enum state {
STATE_FAILURE,
STATE_POWERUP,
STATE_INITRESET,
STATE_CONFIG,
STATE_IDLE,
STATE_IDLEBLINK,
+ STATE_PASSWORD,
+ STATE_PASSWORD2,
STATE_INPUT,
STATE_ACCEPT,
STATE_REJECT,
STATE_KEYMATIC_RECHECK,
STATE_KEYMATIC_RECLOSE,
#ifdef KILLSWITCH
@@ -338,12 +344,13 @@ static uint8_t wait_byte(void)
#define WAIT_IDLE 3
#define WAIT_IDLEBLINK 1 /* no full cycle */
#define CNTR_BLINK 6
#define CNTR_ERROR 61
#define CNTR_BEEP 10
+#define CNTR_BEEP_ACK 30
#define CNTR_BEEP_REJ 92
#define WAIT_INPUT 6
#define WAIT_ACCEPT 8
#define WAIT_REJECT 8
#define WAIT_KEYMATIC_RETRY 9
@@ -354,14 +361,16 @@ static uint8_t wait_byte(void)
static uint8_t cntr = 0;
static uint8_t close_try = 0;
#ifdef KILLSWITCH
static uint8_t error;
#endif
-static const EEMEM uint8_t passwd[sizeof(PIN) - 1] = PIN;
+static EEMEM uint8_t passwd[sizeof(PIN) - 1] = PIN;
+static EEMEM uint8_t mpasswd[sizeof(MASTER_PIN) - 1] = MASTER_PIN;
static uint8_t code[sizeof(PIN)];
+static uint8_t new_code[sizeof(PIN)];
static void state_enter(void)
{
switch (state) {
case STATE_NONE:
case STATE_FAILURE:
@@ -435,12 +444,21 @@ static void state_enter(void)
statecntr = WAIT_INPUT;
if (send_byte(0xed) != 0xfa)
break;
if (send_byte(KBLED_STATE) != 0xfa)
break;
break;
+ case STATE_PASSWORD:
+ case STATE_PASSWORD2:
+ toutstate = STATE_IDLE;
+ statecntr = WAIT_INPUT;
+ if (send_byte(0xed) != 0xfa)
+ break;
+ if (send_byte(KBLED_STATE | KBLED_OK) != 0xfa)
+ break;
+ break;
case STATE_ACCEPT:
toutstate = close_try ? STATE_KEYMATIC_RECHECK : STATE_IDLE;
statecntr = WAIT_ACCEPT;
if (send_byte(0xed) != 0xfa)
break;
if (send_byte(KBLED_OK) != 0xfa)
@@ -529,18 +547,19 @@ const PROGMEM uint8_t kbc[] = {
'_', '1', '_', '4', '7', '_', '_', '_', /* 68 - 6f */
'0', '_', '2', '5', '6', '8', ESC, ESC, /* 70 - 77 */
'_', '_', '3', '_', '_', '9' /* 78 - 7f */
};
#define NUMPAD_ENTER 0xda /* e0 5a */
+#define F12 0x07
static uint8_t pressed = 0, e0 = 0, release = 0;
static void handle_keypress(uint8_t data)
{
- uint8_t unlock, lock;
+ uint8_t unlock, lock, change_pass;
uint8_t ascii = '_';
uint8_t c;
if (release) {
pressed = 0;
release = 0;
@@ -570,14 +589,72 @@ static void handle_keypress(uint8_t data)
ascii = pgm_read_byte(kbc + data - KBC_BASE);
dbg_wr(0x03);
dbg_wr(0x80 | (ascii & 0xf));
dbg_wr(0x80 | (ascii >> 4));
- lock = ascii == ESC;
- unlock = ascii == ENT || data == NUMPAD_ENTER;
+ if (state != STATE_PASSWORD && state != STATE_PASSWORD2) {
+ lock = ascii == ESC;
+ unlock = ascii == ENT || data == NUMPAD_ENTER;
+ change_pass = data == F12;
+ } else {
+ lock = unlock = 0;
+ change_pass = ascii == ENT || data == NUMPAD_ENTER;
+ }
+
+ if (change_pass) {
+ uint8_t pos, ok = 1;
+ switch(state) {
+ default:
+ /* Check master password, change state to password input */
+ if (code[0])
+ ok = 0;
+
+ for (pos = 1; pos < sizeof(code); pos++)
+ if(code[pos] != eeprom_read_byte(mpasswd + pos - 1))
+ ok = 0;
+
+ if (ok) {
+ cntr = CNTR_BEEP_ACK;
+ nextstate = STATE_PASSWORD;
+ }
+ break;
+ case STATE_PASSWORD:
+ /* Check password and store it to buffer */
+ if (code[0])
+ ok = 0;
+
+ if (ok) {
+ memcpy(new_code, code, sizeof(code));
+ PORTB &= ~B_BEEP;
+ cntr = CNTR_BEEP_ACK;
+ nextstate = STATE_PASSWORD2;
+ }
+ break;
+ case STATE_PASSWORD2:
+ /* Compare password with buffer, write to eeprom */
+ if (memcmp(new_code, code, sizeof(code)))
+ ok = 0;
+ if (ok) {
+ eeprom_update_block(new_code + 1, passwd,
+ sizeof(new_code) - 1);
+ PORTB &= ~B_BEEP;
+ cntr = CNTR_BEEP_REJ;
+ nextstate = STATE_ACCEPT;
+ }
+ break;
+ }
+ if (!ok) {
+ PORTB &= ~B_BEEP;
+
+ cntr = CNTR_BEEP_REJ;
+ nextstate = STATE_REJECT;
+ }
+ memset(code, 0, sizeof(code));
+ return;
+ }
if (lock || unlock) {
uint8_t pos, ok = 1;
/* passwd: a b c d
* code: \0 a b c d
@@ -619,13 +696,22 @@ static void handle_keypress(uint8_t data)
for (c = 0; c < sizeof(code) - 1; c++)
code[c] = code[c + 1];
code[c] = ascii;
PORTB &= ~B_BEEP;
cntr = CNTR_BEEP;
- nextstate = STATE_INPUT;
+ switch (state) {
+ case STATE_PASSWORD:
+ nextstate = STATE_PASSWORD;
+ break;
+ case STATE_PASSWORD2:
+ nextstate = STATE_PASSWORD2;
+ break;
+ default:
+ nextstate = STATE_INPUT;
+ }
}
ISR(USART_RX_vect)
{
uint8_t data = UDR0;
@@ -637,12 +723,14 @@ ISR(USART_RX_vect)
case STATE_INITRESET:
if (data == 0xaa)
nextstate = STATE_CONFIG;
break;
case STATE_IDLE:
case STATE_IDLEBLINK:
+ case STATE_PASSWORD:
+ case STATE_PASSWORD2:
case STATE_INPUT:
handle_keypress(data);
default:
break;
}
}