summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Franke <nobody@nowhere.ws>2012-09-02 22:00:16 (GMT)
committerChristian Franke <nobody@nowhere.ws>2012-09-02 22:03:29 (GMT)
commiteee52b591f31a10895bcaae69ecbcc8a8920df79 (patch)
tree82808ef023dcae38b16697b892bb434465f9ff0e
parent97140abf540f41e97ae59beff8a3c106010641aa (diff)
add a masterpin which allows to change the pin
-rw-r--r--Makefile4
-rw-r--r--kbc.c98
2 files changed, 97 insertions, 5 deletions
diff --git a/Makefile b/Makefile
index a44356e..7d24dc8 100644
--- a/Makefile
+++ b/Makefile
@@ -8,6 +8,10 @@ CFLAGS=-Wall -Wextra -Wno-unused-parameter -pedantic -std=c99
ifdef PIN
DPIN=-DPIN=\"$(PIN)\"
endif
+
+ifdef MASTER_PIN
+DPIN+=-DMASTER_PIN=\"$(MASTER_PIN)\"
+endif
ACFLAGS=-g -mmcu=$(MCU) $(CFLAGS) $(DPIN) -Os -mcall-prologues
# ALDFLAGS=-L/usr/avr/lib/avr4 -L/usr/lib/binutils/avr/2.18
LD=gcc
diff --git a/kbc.c b/kbc.c
index f343012..a1f70b8 100644
--- a/kbc.c
+++ b/kbc.c
@@ -12,6 +12,10 @@
#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)
@@ -54,6 +58,8 @@ enum state {
STATE_CONFIG,
STATE_IDLE,
STATE_IDLEBLINK,
+ STATE_PASSWORD,
+ STATE_PASSWORD2,
STATE_INPUT,
STATE_ACCEPT,
STATE_REJECT,
@@ -341,6 +347,7 @@ static uint8_t wait_byte(void)
#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
@@ -357,8 +364,10 @@ static uint8_t close_try = 0;
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)
{
@@ -438,6 +447,15 @@ static void state_enter(void)
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;
@@ -532,12 +550,13 @@ const PROGMEM uint8_t kbc[] = {
};
#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;
@@ -573,8 +592,66 @@ static void handle_keypress(uint8_t data)
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;
@@ -622,7 +699,16 @@ static void handle_keypress(uint8_t data)
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)
@@ -640,6 +726,8 @@ ISR(USART_RX_vect)
break;
case STATE_IDLE:
case STATE_IDLEBLINK:
+ case STATE_PASSWORD:
+ case STATE_PASSWORD2:
case STATE_INPUT:
handle_keypress(data);
default: