diff options
| author | Sadeep Madurange <sadeep@asciimx.com> | 2025-04-07 17:21:29 +0800 |
|---|---|---|
| committer | Sadeep Madurange <sadeep@asciimx.com> | 2025-04-07 17:21:29 +0800 |
| commit | 6a2b5fefeb8931ca886bcf99a8f983b13e545515 (patch) | |
| tree | b792d15fa60c6ff256e78800ed2ce58aab9a709d /lock/back.c | |
| parent | 6b9f251d75fde8064280973fd7ba968008581d73 (diff) | |
| download | smart-home-6a2b5fefeb8931ca886bcf99a8f983b13e545515.tar.gz | |
2-way radio comm.
Diffstat (limited to 'lock/back.c')
| -rw-r--r-- | lock/back.c | 56 |
1 files changed, 42 insertions, 14 deletions
diff --git a/lock/back.c b/lock/back.c index c5dc35e..9da6688 100644 --- a/lock/back.c +++ b/lock/back.c @@ -1,11 +1,14 @@ +/* Door back, connected to the fingerprint scanner */ + #include <stdint.h> -#include <stdlib.h> #include <string.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 @@ -15,15 +18,26 @@ #define RX_PCMSK PCMSK2 #define RX_PCINTVEC PCINT2_vect -static int rxdr = 0; +// todo: atomic var +static volatile int rxdr = 0; + +static inline void await_reply(void) +{ + uint8_t i; + + radio_listen(); + for (i = 0; i < 500 && rxdr == 0; i += 100) + _delay_ms(100); +} int main(void) { - uint8_t n; - char s[2]; - char buf[MAXPDLEN + 1]; + uint8_t rxaddr[ADDRLEN] = { 194, 178, 83 }; + uint8_t txaddr[ADDRLEN] = { 194, 178, 82 }; - uint8_t rxaddr[] = { 194, 178, 83 }; + char buf[WDLEN + 1]; + char key[WDLEN + 1]; + char msg[WDLEN + 1]; RX_DDR &= ~(1 << RX_PIN); RX_PORT |= (1 << RX_PIN); @@ -39,22 +53,36 @@ int main(void) for (;;) { if (rxdr) { - n = radio_recv(buf, MAXPDLEN); + n = radio_recv(buf, WDLEN); buf[n] = '\0'; - uart_write("Received data: "); - uart_write(itoa(n, s, 10)); - uart_write_line(" bytes"); rxdr = 0; - if (n > 0) { - uart_write("INFO: "); - uart_write_line(buf); + xor(KEY, buf, msg, WDLEN); + if (strncmp(msg, SYN, WDLEN) == 0) { + xor(KEY, key, 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); + if (strncmp(msg, LOCK, WDLEN) == 0) { + uart_write_line("LOCKED"); + } else if (strncmp(msg, UNLOCK, WDLEN) == 0) { + uart_write_line("UNLOCKED"); + } else { + uart_write("ERROR: unknown message "); + uart_write_line(msg); + } + } + } else { + uart_write_line("ERROR: handshake failed"); } } else { uart_write_line("No IRQ"); _delay_ms(2000); } } - return 0; } |
