From 014040f33f914a82031e15940924bc9c92236fec Mon Sep 17 00:00:00 2001 From: Sadeep Madurange Date: Tue, 8 Apr 2025 17:01:41 +0800 Subject: Fix await_reply() --- lock/Back.Makefile | 2 +- lock/Frnt.Makefile | 2 +- lock/back.c | 24 ++++++++++++------------ lock/frnt.c | 38 +++++++++++++++++++++----------------- lock/util.h | 10 +++++++--- 5 files changed, 42 insertions(+), 34 deletions(-) (limited to 'lock') diff --git a/lock/Back.Makefile b/lock/Back.Makefile index 9f2fe2b..51ede95 100644 --- a/lock/Back.Makefile +++ b/lock/Back.Makefile @@ -1,6 +1,6 @@ CC = avr-gcc MCU = atmega328p -TARGET = sender +TARGET = back SRC = back.c uart.c nrfm.c util.c OBJ = $(SRC:.c=.o) diff --git a/lock/Frnt.Makefile b/lock/Frnt.Makefile index cb957df..f57494d 100644 --- a/lock/Frnt.Makefile +++ b/lock/Frnt.Makefile @@ -1,6 +1,6 @@ CC = avr-gcc MCU = atmega328p -TARGET = app +TARGET = front SRC = frnt.c uart.c nrfm.c util.c OBJ = $(SRC:.c=.o) diff --git a/lock/back.c b/lock/back.c index f373d20..dd4938a 100644 --- a/lock/back.c +++ b/lock/back.c @@ -38,7 +38,7 @@ static uint8_t charslen = sizeof(chars) / sizeof(chars[0]); static inline void await_reply(void) { - uint8_t i; + int i; radio_listen(); for (i = 0; i < 500 && rxdr == 0; i += 100) @@ -74,9 +74,7 @@ int main(void) uint8_t rxaddr[ADDRLEN] = { 194, 178, 83 }; uint8_t txaddr[ADDRLEN] = { 194, 178, 82 }; - char buf[WDLEN + 1]; - char key[WDLEN + 1]; - char msg[WDLEN + 1]; + char buf[WDLEN + 1], key[WDLEN + 1], msg[WDLEN + 1]; RX_DDR &= ~(1 << RX_PIN); RX_PORT |= (1 << RX_PIN); @@ -96,7 +94,11 @@ int main(void) n = radio_recv(buf, WDLEN); buf[n] = '\0'; rxdr = 0; + xor(KEY, buf, msg, WDLEN); + uart_write("DEBUG: msg recv = "); + uart_write_line(msg); + if (strncmp(msg, SYN, WDLEN) == 0) { keygen(key, WDLEN); uart_write("DEBUG: session key = "); @@ -105,19 +107,17 @@ int main(void) 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); - } + uart_write("DEBUG: msg = "); + uart_write_line(msg); + + xor(key, ACK, msg, WDLEN); + radio_sendto(txaddr, msg, WDLEN); } } else { uart_write_line("ERROR: handshake failed"); diff --git a/lock/frnt.c b/lock/frnt.c index c3c36a7..4ad2d11 100644 --- a/lock/frnt.c +++ b/lock/frnt.c @@ -1,6 +1,7 @@ /* Door front, connected to the fingerprint scanner */ #include +#include #include #include @@ -22,7 +23,7 @@ static volatile int rxdr = 0; static inline void await_reply(void) { - uint8_t i; + int i; radio_listen(); for (i = 0; i < 500 && rxdr == 0; i += 100) @@ -35,7 +36,7 @@ int main(void) uint8_t rxaddr[ADDRLEN] = { 194, 178, 82 }; uint8_t txaddr[ADDRLEN] = { 194, 178, 83 }; - char key[WDLEN + 1], msg[WDLEN + 1]; + char buf[WDLEN + 1], key[WDLEN + 1], msg[WDLEN + 1]; RX_DDR &= ~(1 << RX_PIN); RX_PORT |= (1 << RX_PIN); @@ -54,22 +55,25 @@ int main(void) radio_sendto(txaddr, msg, WDLEN); await_reply(); if (rxdr) { - n = radio_recv(msg, WDLEN); - msg[n] = '\0'; - rxdr = 0; - xor(KEY, msg, key, WDLEN); - // check btn press - xor(key, LOCK, msg, WDLEN); - radio_sendto(txaddr, msg, WDLEN); - await_reply(); - if (rxdr) { - n = radio_recv(msg, WDLEN); - msg[n] = '\0'; + uart_write_line("reply received"); + n = radio_recv(buf, WDLEN); + buf[n] = '\0'; + if (n > 0) { rxdr = 0; - uart_write_line(msg); - } else { - // power down - uart_write_line("ERROR: no reply"); + 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 diff --git a/lock/util.h b/lock/util.h index 8be238a..c3449c8 100644 --- a/lock/util.h +++ b/lock/util.h @@ -6,9 +6,13 @@ #define WDLEN 32 #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 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 SYN "SYNSYNSYNSYNSYNSYNSYNSYNSYNSYNSY" +#define ACK "ACKACKACKACKACKACKACKACKACKACKAC" +#define MSG "MSGMSGMSGMSGMSGMSGMSGMSGMSGMSGMS" void xor(const char *k, const char *s, char *d, uint8_t n); -- cgit v1.2.3