summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--door_lock/Makefile2
-rw-r--r--door_lock/cmd.c33
-rw-r--r--door_lock/cmd.h8
-rw-r--r--door_lock/servo.c29
4 files changed, 50 insertions, 22 deletions
diff --git a/door_lock/Makefile b/door_lock/Makefile
index 92b9945..018960a 100644
--- a/door_lock/Makefile
+++ b/door_lock/Makefile
@@ -2,7 +2,7 @@ CC = avr-gcc
MCU = atmega328p
TARGET = app
-SRC = servo.c serial.c
+SRC = servo.c cmd.c serial.c
OBJ = $(SRC:.c=.o)
CFLAGS = -std=gnu99
diff --git a/door_lock/cmd.c b/door_lock/cmd.c
new file mode 100644
index 0000000..ccfbafb
--- /dev/null
+++ b/door_lock/cmd.c
@@ -0,0 +1,33 @@
+#include <stdint.h>
+#include <string.h>
+
+#include "cmd.h"
+
+#define XORLEN 32
+#define KEY "dM>}jdb,6gsnC$J^K 8(I5vyPemPs%;K"
+#define ULOCK_CMD "43iqr5$NB8SpN?Z/52{iVl>o|i!.'dsT"
+
+static char ulock_crypt_cmd[XORLEN];
+
+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 is_ulock_cmd(const char *s)
+{
+ char buf[XORLEN + 1];
+
+ xor(s, buf, XORLEN);
+ buf[XORLEN] = 0;
+ return !strcmp(ULOCK_CMD, buf);
+}
+
+char * get_encrypted_ulock_cmd(void)
+{
+ xor(ULOCK_CMD, ulock_crypt_cmd, XORLEN);
+ return ulock_crypt_cmd;
+}
diff --git a/door_lock/cmd.h b/door_lock/cmd.h
new file mode 100644
index 0000000..de9d4b0
--- /dev/null
+++ b/door_lock/cmd.h
@@ -0,0 +1,8 @@
+#ifndef SA_CMD_H
+#define SA_CMD_H
+
+int is_ulock_cmd(const char *s);
+
+char * get_encrypted_ulock_cmd(void);
+
+#endif /* SA_CMD_H */
diff --git a/door_lock/servo.c b/door_lock/servo.c
index 45640df..5ba867f 100644
--- a/door_lock/servo.c
+++ b/door_lock/servo.c
@@ -4,6 +4,7 @@
#include <avr/interrupt.h>
#include <util/delay.h>
+#include "cmd.h"
#include "serial.h"
#define PWM_MIN 1200
@@ -14,10 +15,6 @@
#define LOCK_BTN PD6
#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)
{
DDRB |= 1 << SERVO_PIN;
@@ -41,18 +38,9 @@ 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];
+ char *ulock_cmd;
servo_init();
pcint2_init();
@@ -62,13 +50,12 @@ int main(void)
sei();
for(;;) {
- // encrypt
- xor(ULOCK_FLAG, s1, XORLEN);
-
- // decrypt
- xor(s1, s2, XORLEN);
- s2[XORLEN] = 0;
- serial_write_line(s2);
+ ulock_cmd = get_encrypted_ulock_cmd();
+
+ if (is_ulock_cmd(ulock_cmd))
+ serial_write_line("unlock door");
+ else
+ serial_write_line("do not unlock door");
_delay_ms(1000);
}