summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSadeep Madurange <sadeep@asciimx.com>2025-04-27 13:16:46 +0800
committerSadeep Madurange <sadeep@asciimx.com>2025-04-27 13:16:46 +0800
commit775d4a84f36d4d5d0700e9c44dfa4114a0cc022c (patch)
tree754c9d00ed1d56806dc35070895d5bba845f40d1
parent4dc74efdd09b5244ef676aef4f5575b6d9e2694d (diff)
downloadsmart-home-775d4a84f36d4d5d0700e9c44dfa4114a0cc022c.tar.gz
Yay! working with session key encryption.
-rw-r--r--lock/bend.c19
-rw-r--r--lock/fend.c44
2 files changed, 41 insertions, 22 deletions
diff --git a/lock/bend.c b/lock/bend.c
index d7459eb..3957c55 100644
--- a/lock/bend.c
+++ b/lock/bend.c
@@ -36,7 +36,6 @@ static char tab[] = {
'P', 'f', ':', 'B', ']', 'Y', '^', 'F', '%', 'C', 'x'
};
-static uint8_t sync = 0;
static volatile uint8_t rxd = 0;
static uint16_t tablen = sizeof(tab) / sizeof(tab[0]);
@@ -129,10 +128,20 @@ int main(void)
radio_recv(buf, WDLEN);
rxd = 0;
xor(KEY, buf, msg, WDLEN);
- if (memcmp(msg, LOCK, WDLEN) == 0)
- lock();
- else if (memcmp(msg, UNLOCK, WDLEN) == 0)
- unlock();
+ if (memcmp(msg, SYN, WDLEN) == 0) {
+ keygen(key, WDLEN);
+ xor(KEY, key, buf, WDLEN);
+ radio_sendto(txaddr, buf, WDLEN);
+ } else {
+ xor(key, buf, msg, WDLEN);
+ if (memcmp(msg, LOCK, WDLEN) == 0) {
+ lock();
+ keydel(key, WDLEN);
+ } else if (memcmp(msg, UNLOCK, WDLEN) == 0) {
+ unlock();
+ keydel(key, WDLEN);
+ }
+ }
}
}
return 0;
diff --git a/lock/fend.c b/lock/fend.c
index cae6a94..966bfde 100644
--- a/lock/fend.c
+++ b/lock/fend.c
@@ -21,8 +21,8 @@
#define RX_PCMSK PCMSK2
#define RX_PCINTVEC PCINT2_vect
-uint8_t sync = 0;
static volatile uint8_t rxd = 0;
+static volatile uint8_t sync = 0;
static volatile uint8_t islock = 0;
static volatile uint8_t isunlock = 0;
@@ -45,6 +45,7 @@ static inline void init_btns(void)
int main(void)
{
+ uint8_t n;
uint8_t rxaddr[ADDRLEN] = { 194, 178, 82 };
uint8_t txaddr[ADDRLEN] = { 194, 178, 83 };
@@ -58,28 +59,33 @@ int main(void)
radio_print_config();
sei();
+ radio_listen();
for (;;) {
- if (islock) {
- xor(KEY, LOCK, buf, WDLEN);
+ if (!sync && (islock || isunlock)) {
+ xor(KEY, SYN, buf, WDLEN);
do {
sync = radio_sendto(txaddr, buf, WDLEN);
_delay_ms(50);
} while (!sync);
- sync = 0;
- islock = 0;
- uart_write_line("sent LOCK");
+ uart_write_line("sent SYN");
}
- if (isunlock) {
- xor(KEY, UNLOCK, buf, WDLEN);
- do {
- sync = radio_sendto(txaddr, buf, WDLEN);
- _delay_ms(50);
- } while (!sync);
- sync = 0;
- isunlock = 0;
- uart_write_line("sent UNLOCK");
+ if (rxd) {
+ n = radio_recv(buf, WDLEN);
+ rxd = 0;
+ if (sync && (islock || isunlock)) {
+ sync = 0;
+ xor(KEY, buf, key, WDLEN);
+ if (islock) {
+ islock = 0;
+ xor(key, LOCK, buf, WDLEN);
+ } else if (isunlock) {
+ isunlock = 0;
+ xor(key, UNLOCK, buf, WDLEN);
+ }
+ radio_sendto(txaddr, buf, WDLEN);
+ }
}
}
return 0;
@@ -92,12 +98,16 @@ ISR(RX_PCINTVEC)
ISR(INT0_vect)
{
- if (is_btn_pressed(PIND, LOCK_PIN))
+ if (is_btn_pressed(PIND, LOCK_PIN)) {
+ sync = 0;
islock = 1;
+ }
}
ISR(INT1_vect)
{
- if (is_btn_pressed(PIND, UNLOCK_PIN))
+ if (is_btn_pressed(PIND, UNLOCK_PIN)) {
+ sync = 0;
isunlock = 1;
+ }
}