summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSadeep Madurange <sadeep@asciimx.com>2025-04-07 17:21:29 +0800
committerSadeep Madurange <sadeep@asciimx.com>2025-04-07 17:21:29 +0800
commit6a2b5fefeb8931ca886bcf99a8f983b13e545515 (patch)
treeb792d15fa60c6ff256e78800ed2ce58aab9a709d
parent6b9f251d75fde8064280973fd7ba968008581d73 (diff)
downloadsmart-home-6a2b5fefeb8931ca886bcf99a8f983b13e545515.tar.gz
2-way radio comm.
-rw-r--r--lock/back.c56
-rw-r--r--lock/frnt.c11
-rw-r--r--lock/util.h3
3 files changed, 51 insertions, 19 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;
}
diff --git a/lock/frnt.c b/lock/frnt.c
index cef60f3..88ee614 100644
--- a/lock/frnt.c
+++ b/lock/frnt.c
@@ -1,7 +1,8 @@
-/* Door front, connected to the fingerprint scanner */
+/* Door front, connected to the fingerprint scanner */
#include <stdint.h>
-#include <string.h>
+
+#include <avr/interrupt.h>
#include <util/delay.h>
#include "nrfm.h"
@@ -21,6 +22,8 @@ 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);
@@ -28,7 +31,6 @@ static inline void await_reply(void)
int main(void)
{
- uint8_t i;
uint8_t rxaddr[ADDRLEN] = { 194, 178, 82 };
uint8_t txaddr[ADDRLEN] = { 194, 178, 83 };
@@ -43,6 +45,8 @@ int main(void)
radio_init(rxaddr);
radio_print_config();
+ sei();
+
for (;;) {
_delay_ms(2000); /* todo: fingerprint check */
xor(KEY, SYN, msg, WDLEN);
@@ -53,6 +57,7 @@ int main(void)
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();
diff --git a/lock/util.h b/lock/util.h
index 3f6d7a2..64f6ad5 100644
--- a/lock/util.h
+++ b/lock/util.h
@@ -6,8 +6,7 @@
#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 LOCKED "R,I7l^E4j]KyLR9'*Q{Jd'zu.~!84}Ik"
-#define UNLOCKED "R,I7l^E4j]KyLR9'*Q{Jd'zu.~!84}Il"
+#define UNLOCK "R,I7l^E4j]KyLR9'*Q{Jd'zu.~!84}Ik"
void xor(const char *k, const char *s, char *d, uint8_t n);