diff options
| author | Sadeep Madurange <sadeep@asciimx.com> | 2024-11-06 11:51:41 +0800 |
|---|---|---|
| committer | Sadeep Madurange <sadeep@asciimx.com> | 2024-11-06 11:51:41 +0800 |
| commit | 50381c06244e944390de382f6bdd85cc8b9a3332 (patch) | |
| tree | 860e72841512a9611b5287cc97e79f268ab7133e | |
| parent | 0ac5b054a17b71cfeba80f1f2f24cb0beee9e13c (diff) | |
| download | smart-home-50381c06244e944390de382f6bdd85cc8b9a3332.tar.gz | |
XOR cypher.
| -rw-r--r-- | door_lock/Makefile | 2 | ||||
| -rw-r--r-- | door_lock/servo.c (renamed from door_lock/main.c) | 43 |
2 files changed, 37 insertions, 8 deletions
diff --git a/door_lock/Makefile b/door_lock/Makefile index c188507..92b9945 100644 --- a/door_lock/Makefile +++ b/door_lock/Makefile @@ -2,7 +2,7 @@ CC = avr-gcc MCU = atmega328p TARGET = app -SRC = main.c serial.c +SRC = servo.c serial.c OBJ = $(SRC:.c=.o) CFLAGS = -std=gnu99 diff --git a/door_lock/main.c b/door_lock/servo.c index d272a7f..45640df 100644 --- a/door_lock/main.c +++ b/door_lock/servo.c @@ -1,7 +1,10 @@ -#include <stddef.h> +#include <stdint.h> #include <avr/io.h> #include <avr/interrupt.h> +#include <util/delay.h> + +#include "serial.h" #define PWM_MIN 1200 #define PWM_MID 3000 @@ -9,7 +12,11 @@ #define SERVO_PIN PB1 #define LOCK_BTN PD6 -#define UNLOCK_BTN PD7 +#define ULOCK_BTN PD7 + +#define XORLEN 32 +#define KEY "dM>}jdb,6gsnC$J^K 8(I5vyPemPs%;K" +#define ULOCK_FLAG "43iqr5$NB8SpN?Z/52{iVl>o|i!.'dsT" static inline void servo_init(void) { @@ -19,8 +26,8 @@ static inline void servo_init(void) ICR1 = 40000; - DDRD &= ~((1 << LOCK_BTN) | (1 << UNLOCK_BTN)); - PORTD |= (1 << LOCK_BTN) | (1 << UNLOCK_BTN); + DDRD &= ~((1 << LOCK_BTN) | (1 << ULOCK_BTN)); + PORTD |= (1 << LOCK_BTN) | (1 << ULOCK_BTN); } static inline void pcint2_init(void) @@ -34,15 +41,37 @@ static inline uint8_t is_btn_pressed(uint8_t btn) return !((PIND >> btn) & 0x01); } +static inline void xor(const char *s, char *d, uint8_t n) +{ + int i; + + for (i = 0; i < n && s[i]; i++) + d[i] = s[i] ^ KEY[i]; +} + int main(void) { + char s1[XORLEN]; + char s2[XORLEN + 1]; + servo_init(); pcint2_init(); + serial_init(); + sei(); - for(;;) - ; + for(;;) { + // encrypt + xor(ULOCK_FLAG, s1, XORLEN); + + // decrypt + xor(s1, s2, XORLEN); + s2[XORLEN] = 0; + serial_write_line(s2); + + _delay_ms(1000); + } return 0; } @@ -52,6 +81,6 @@ ISR(PCINT2_vect) if (is_btn_pressed(LOCK_BTN)) OCR1A = PWM_MID; - if (is_btn_pressed(UNLOCK_BTN)) + if (is_btn_pressed(ULOCK_BTN)) OCR1A = PWM_MIN; } |
