Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Add a simulator for the project #1

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,14 @@ serial-read: build
uno-upload-isp: ac-build
$(DOCKER_ARDUINO_CLI) bash -c "cd /usr/src/app/ArduinoISP && arduino-cli compile --fqbn arduino:avr:uno --libraries=../vendor/librares/ && arduino-cli upload --verbose --verify -p $(DEVICE_SERIAL) --fqbn arduino:avr:uno"

ac-compile: ac-build
$(DOCKER_ARDUINO_CLI) bash -c "cd /usr/src/app/ArduinoISP && arduino-cli compile --fqbn arduino:avr:uno --libraries=../vendor/librares/"

sim:
g++ -DMOCK test.cpp -o test && ./test

uno:
arduino-cli compile --fqbn arduino:avr:uno --libraries vendor/librares/ hsldz_totp_lock.ino

pro-mini-upload-lock: ac-build
$(DOCKER_ARDUINO_CLI) bash -c "cd /usr/src/app/hsldz_totp_lock && arduino-cli compile --fqbn arduino:avr:uno --libraries=../vendor/librares/ && arduino-cli upload --verbose --verify -p $(DEVICE_SERIAL) --fqbn arduino:avr:mini --programmer arduinoasisp"
61 changes: 45 additions & 16 deletions hsldz_totp_lock/hsldz_totp_lock.ino
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
#ifndef MOCK
#include "Keypad.h"
#include "sha1.h"
#include "TOTP.h"
#include <DS3231.h>
#include <Wire.h>
#include <DS3231.h>
#include <Eeprom24C32_64.h>
#include <Eeprom24C04_16.h>
#endif

#define EEPROM_ADDRESS 0x57
#define EEPROM_CODE 32

#ifndef MOCK
static Eeprom24C32_64 eeprom(EEPROM_ADDRESS);
#else
#define word int
#endif

#define BUZZER_PIN 10
#define LOCK_PIN 12
Expand All @@ -22,15 +32,13 @@

#define MORSE_SOUND_TIME 100
#define MORSE_PAUSE 15
#define MORSE_FREQ 500
#define MORSE_FREQ 500

#define EEPROM_ADDRESS 0x57
#define EEPROM_CODE 32
static Eeprom24C32_64 eeprom(EEPROM_ADDRESS);
const word EEPROM_MEMORY_SIZE = EEPROM_CODE /8 * 1024;
const byte EEPROM_PAGE_COUNTS = EEPROM_CODE;
const word EEPROM_BATCHES = EEPROM_MEMORY_SIZE / EEPROM_PAGE_COUNTS;

void playMaintenanceMelody(int melody[], int size);

// Keypad
const byte ROWS = 4;
Expand Down Expand Up @@ -99,7 +107,7 @@ void setup(){
pinMode(BUZZER_PIN, OUTPUT);
pinMode(LOCK_PIN, OUTPUT);
int size = sizeof(melodyMain) / sizeof(int);
playMaintenanceMelody(melodyMain, size);
//playMaintenanceMelody(melodyMain, size);
}


Expand Down Expand Up @@ -170,6 +178,7 @@ byte getChecksum(byte arrayBytes[], byte count) {
//}


#ifndef MOCK
bool read_key_eeprom(int key_num, byte keyBytes[], byte key_len)
{
bool result = false;
Expand Down Expand Up @@ -203,11 +212,31 @@ bool read_key_eeprom(int key_num, byte keyBytes[], byte key_len)
// Serial.println("DONE");
return result;
}
#else
bool read_key_eeprom(int key_num, byte keyBytes[], byte key_len) {
return true;
}
#endif MOCK

String substr(String s, int from, int to) {
#ifdef MOCK
return s.substr(from, to);
#else
return s.substring(from, to);
#endif
}

int toInt(String s) {
#ifdef MOCK
return stoi(s);
#else
return s.toInt();
#endif
}

bool isTOTPCodeValid(String userInput) {
// Serial.print("userInput: "); Serial.println(userInput);
int keyNum = userInput.substring(0, 2).toInt();
int keyNum = toInt(substr(userInput, 0, 2));
Serial.print("keyNum: "); Serial.println(keyNum);
const int hmacKeySize = 20;
byte keyBytes[hmacKeySize] = { 0 };
Expand All @@ -219,7 +248,7 @@ bool isTOTPCodeValid(String userInput) {
DateTime now = RTC.now();
unsigned long currentUnixTimestamp = now.unixtime();
int timeDeviations[5] = {-60, -30, 0, 30, 60};
String userCode = userInput.substring(2, 8);
String userCode = substr(userInput, 2, 8);
Serial.print("Time: "); Serial.println(currentUnixTimestamp);
TOTP totp = TOTP(keyBytes, hmacKeySize);
for (int i = 0; i < 5; i++) {
Expand All @@ -239,7 +268,7 @@ bool isTOTPCodeValid(String userInput) {

bool isMaintenance(String userInput, String userInputPrev) {
// Serial.print("userInput: "); Serial.println(userInput);
int keyNum = userInput.substring(0, 2).toInt();
int keyNum = toInt(substr(userInput, 0, 2));
// Serial.print("keyNum: "); Serial.println(keyNum);
return (( keyNum == 0 ) && ( userInputPrev != ""));
}
Expand Down Expand Up @@ -267,17 +296,17 @@ void playMaintenanceMelody(int melody[], int size) {
// divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000 / 12;
buzz(melodyPin, melody[thisNote], noteDuration);
//buzz(melodyPin, melody[thisNote], noteDuration);
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
// stop the tone playing:
buzz(melodyPin, 0, noteDuration);
//buzz(melodyPin, 0, noteDuration);
}
}

bool write_key_eeprom(int key_num, byte keyBytes[], byte key_len, bool is_active) {
void write_key_eeprom(int key_num, byte keyBytes[], byte key_len, bool is_active) {
bool result = false;
const byte buffer_size = key_len + 2;
byte inputBytes[buffer_size + 1] = { 0 };
Expand Down Expand Up @@ -315,8 +344,8 @@ void makeMaintenance(String userInputPrev) {
int melodyMainSize = sizeof(melodyMain) / sizeof(int);

playMaintenanceMelody(melodyUnderworld, melodyUnderworldSize);
int command = userInputPrev.substring(2, 4).toInt();
Serial.print("raw command "); Serial.println(userInputPrev.substring(2, 4));
int command = toInt(substr(userInputPrev, 2, 4));
Serial.print("raw command "); Serial.println(substr(userInputPrev, 2, 4));
Serial.print("command "); Serial.println(command);

if (command == 0) {
Expand All @@ -328,8 +357,8 @@ void makeMaintenance(String userInputPrev) {
playMaintenanceMelody(melodyMain, melodyMainSize);
}
if (command == 1) {
int keyNum = userInputPrev.substring(4, 6).toInt();
int keyNumRepeat = userInputPrev.substring(6, 8).toInt();
int keyNum = toInt(substr(userInputPrev, 4, 6));
int keyNumRepeat = toInt(substr(userInputPrev, 6, 8));
if (keyNum == keyNumRepeat) {
bool result = disableKey(keyNum);
if (result) {
Expand Down
81 changes: 81 additions & 0 deletions test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#ifdef MOCK
#include <string>
#include <iostream>
#define makeKeymap(x) ((char*)x)
typedef std::string String;
typedef char byte;
typedef bool boolean;

const int LOW=1;
const int HIGH=1;
const int INPUT=1;
const int OUTPUT=1;

struct {
void initialize() {};
void writeBytes(int&, int, byte []) {};
void readBytes(int&, int, byte []) {};
} eeprom;

struct Keypad{
Keypad(char*, const byte [4], const byte [3], const byte&, const byte&){}
char getKey() { return ' '; }
};

struct DateTime {
unsigned long unixtime(){ return 1; }
};

struct RTClib{
DateTime now() {return DateTime(); }
};
struct {

template<class T> void println(T x){
std::cout << x << std::endl;
}
template<class T> void print(T x){}
void begin(int x){}

} Serial;
struct {
void begin() {}
} Wire;

struct TOTP{

char* buffer = (char*) "000004";

TOTP(const char [20], const int&){}
char* getCode(int x) { return buffer; }
};

void pinMode(int BUTTON_OPEN_PIN, int INPUT){}
void tone(int BUZZER_PIN, int FREQ_BUTTON_PRESS, int SOUND_TIME_BUTTON_PRESS){}
void digitalWrite(int LOCK_PIN, int HIGH){}
void delay(int a){}
void delayMicroseconds(int a){}
int digitalRead(int pin) { return 1; }

#include "hsldz_totp_lock/hsldz_totp_lock.ino"

#include <sstream>
#include <iomanip>
#include <iostream>

int main()
{
for(int i=0; i<1000000; i++) {
std::stringstream ss;
ss << std::setfill('0') << std::setw(6) << i;
std::string s = ss.str();
if(isTOTPCodeValid("01" + s))
{
std::cout << s << std::endl;
break;
}
}
setup();
// for(;;) loop();
}
#endif