diff options
| -rw-r--r-- | lock/Scan.Makefile (renamed from lock/Frnt.Makefile) | 4 | ||||
| -rw-r--r-- | lock/Servo.Makefile (renamed from lock/Back.Makefile) | 0 | ||||
| -rw-r--r-- | lock/frnt.c | 89 | ||||
| -rw-r--r-- | lock/scan.c | 36 | ||||
| -rw-r--r-- | lock/servo.c (renamed from lock/back.c) | 0 | ||||
| -rw-r--r-- | lock/util.c | 31 | ||||
| -rw-r--r-- | lock/util.h | 13 |
7 files changed, 74 insertions, 99 deletions
diff --git a/lock/Frnt.Makefile b/lock/Scan.Makefile index f57494d..eedf079 100644 --- a/lock/Frnt.Makefile +++ b/lock/Scan.Makefile @@ -1,8 +1,8 @@ CC = avr-gcc MCU = atmega328p -TARGET = front +TARGET = scan -SRC = frnt.c uart.c nrfm.c util.c +SRC = scan.c uart.c nrfm.c util.c OBJ = $(SRC:.c=.o) CFLAGS = -std=gnu99 diff --git a/lock/Back.Makefile b/lock/Servo.Makefile index 51ede95..51ede95 100644 --- a/lock/Back.Makefile +++ b/lock/Servo.Makefile diff --git a/lock/frnt.c b/lock/frnt.c deleted file mode 100644 index 4ad2d11..0000000 --- a/lock/frnt.c +++ /dev/null @@ -1,89 +0,0 @@ -/* Door front, connected to the fingerprint scanner */ - -#include <stdint.h> -#include <stdlib.h> - -#include <avr/interrupt.h> -#include <util/delay.h> - -#include "nrfm.h" -#include "uart.h" -#include "util.h" - -#define RX_PIN PD7 -#define RX_DDR DDRD -#define RX_PORT PORTD -#define RX_PCIE PCIE2 -#define RX_PCINT PCINT23 -#define RX_PCMSK PCMSK2 -#define RX_PCINTVEC PCINT2_vect - -// todo: atomic var -static volatile int rxdr = 0; - -static inline void await_reply(void) -{ - int i; - - radio_listen(); - for (i = 0; i < 500 && rxdr == 0; i += 100) - _delay_ms(100); -} - -int main(void) -{ - uint8_t n; - uint8_t rxaddr[ADDRLEN] = { 194, 178, 82 }; - uint8_t txaddr[ADDRLEN] = { 194, 178, 83 }; - - char buf[WDLEN + 1], key[WDLEN + 1], msg[WDLEN + 1]; - - RX_DDR &= ~(1 << RX_PIN); - RX_PORT |= (1 << RX_PIN); - PCICR |= (1 << RX_PCIE); - RX_PCMSK |= (1 << RX_PCINT); - - uart_init(); - radio_init(rxaddr); - radio_print_config(); - - sei(); - - for (;;) { - _delay_ms(2000); /* todo: fingerprint check */ - xor(KEY, SYN, msg, WDLEN); - radio_sendto(txaddr, msg, WDLEN); - await_reply(); - if (rxdr) { - uart_write_line("reply received"); - n = radio_recv(buf, WDLEN); - buf[n] = '\0'; - if (n > 0) { - rxdr = 0; - xor(KEY, buf, key, WDLEN); - xor(key, MSG, msg, WDLEN); - radio_sendto(txaddr, msg, WDLEN); - await_reply(); - if (rxdr) { - n = radio_recv(buf, WDLEN); - buf[n] = '\0'; - rxdr = 0; - xor(key, buf, msg, WDLEN); - uart_write_line(msg); - } else { - // power down - uart_write_line("ERROR: no reply"); - } - } - } else { - // power down - uart_write_line("ERROR: no session key"); - } - } - return 0; -} - -ISR(RX_PCINTVEC) -{ - rxdr = 1; -} diff --git a/lock/scan.c b/lock/scan.c new file mode 100644 index 0000000..e375111 --- /dev/null +++ b/lock/scan.c @@ -0,0 +1,36 @@ +/* Lock front, connected to the fingerprint scanner */ + +#include <stdint.h> +#include <stdlib.h> + +#include <avr/interrupt.h> +#include <util/delay.h> + +#include "nrfm.h" +#include "uart.h" +#include "util.h" + +int main(void) +{ + uint8_t n; + uint8_t rxaddr[ADDRLEN] = { 194, 178, 82 }; + + char buf[WDLEN + 1], msg[WDLEN + 1]; + + /* timer for keygen */ + TCCR1A |= (1 << WGM11) | (1 << COM1A1); + TCCR1B |= (1 << WGM13) | (1 << CS11); + ICR1 = 20000; + + uart_init(); + radio_init(rxaddr); + radio_print_config(); + + sei(); + + for (;;) { + } + + return 0; +} + diff --git a/lock/back.c b/lock/servo.c index dd4938a..dd4938a 100644 --- a/lock/back.c +++ b/lock/servo.c diff --git a/lock/util.c b/lock/util.c index 95bef77..bc62438 100644 --- a/lock/util.c +++ b/lock/util.c @@ -1,5 +1,35 @@ +#include <avr/io.h> +#include <avr/interrupt.h> + #include "util.h" +static char tab[] = { + '0', '8', '3', '6', 'a', 'Z', '$', '4', 'v', 'R', '@', + 'E', '1', 'o', '#', ')', '2', '5', 'q', ';', '.', 'I', + 'c', '7', '9', '*', 'L', 'V', '&', 'k', 'K', '!', 'm', + 'N', '(', 'O', 'Q', 'A', '>', 'T', 't', '?', 'S', 'h', + 'w', '/', 'n', 'W', 'l', 'M', 'e', 'H', 'j', 'g', '[', + 'P', 'f', ':', 'B', ']', 'Y', '^', 'F', '%', 'C', 'x' +}; + +static uint16_t tablen = sizeof(tab) / sizeof(tab[0]); + +void keygen(char *buf, uint8_t n) +{ + int i, imax; + uint8_t sreg; + uint16_t idx; + + sreg = SREG; + cli(); + idx = TCNT1; + SREG = sreg; + + for (i = 0, imax = n - 1; i < imax; i++, idx++) + buf[i] = tab[(idx % tablen)]; + buf[imax] = '\0'; +} + void xor(const char *k, const char *s, char *d, uint8_t n) { int i; @@ -7,3 +37,4 @@ void xor(const char *k, const char *s, char *d, uint8_t n) for (i = 0; i < n; i++) d[i] = s[i] ^ k[i]; } + diff --git a/lock/util.h b/lock/util.h index c3449c8..54dc66b 100644 --- a/lock/util.h +++ b/lock/util.h @@ -3,16 +3,13 @@ #include <stdint.h> -#define WDLEN 32 +#define KEY "dM>}jdb,6gsnC$J^K 8(I5vyPemPs%;K" +#define LOCK "R,I7l^E4j]KyLR9'*Q{Jd'zu.~!84}Ij" +#define UNLOCK "R,I7l^E4j]KyLR9'*Q{Jd'zu.~!84}Ik" -#define KEY "dM>}jdb,6gsnC$J^K 8(I5vyPemPs%;K" -//#define SYN "43iqr5$NB8SpN?Z/52{iVl>o|i!.'dsT" -//#define LOCK "R,I7l^E4j]KyLR9'*Q{Jd'zu.~!84}Ij" -//#define UNLOCK "R,I7l^E4j]KyLR9'*Q{Jd'zu.~!84}Ik" +#define WDLEN 32 -#define SYN "SYNSYNSYNSYNSYNSYNSYNSYNSYNSYNSY" -#define ACK "ACKACKACKACKACKACKACKACKACKACKAC" -#define MSG "MSGMSGMSGMSGMSGMSGMSGMSGMSGMSGMS" +void keygen(char *buf, uint8_t n); void xor(const char *k, const char *s, char *d, uint8_t n); |
