Skip to content

Commit

Permalink
Merge pull request #445 from tock/hotp-screen
Browse files Browse the repository at this point in the history
HOTP Tutorial: Add screen support
  • Loading branch information
alevy authored Jun 21, 2024
2 parents 2dfc653 + 89a9b2f commit a574756
Show file tree
Hide file tree
Showing 12 changed files with 96 additions and 0 deletions.
3 changes: 3 additions & 0 deletions examples/tutorials/hotp/hotp_milestone_one/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ TOCK_USERLAND_BASE_DIR = ../../../../
# Which files to compile.
C_SRCS := $(wildcard *.c)

# Include U8G2 graphics library.
EXTERN_LIBS += $(TOCK_USERLAND_BASE_DIR)/u8g2

# Include userland master makefile. Contains rules and flags for actually
# building the application.
include $(TOCK_USERLAND_BASE_DIR)/AppMakefile.mk
5 changes: 5 additions & 0 deletions examples/tutorials/hotp/hotp_milestone_one/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

// Local includes
#include "base32.h"
#include "screen.h"
#include "step0.h"


Expand Down Expand Up @@ -108,6 +109,8 @@ int main(void) {
// Configure a default HOTP secret
program_default_secret(&stored_key);

display_hotp_keys(&stored_key, 1);

// Main loop. Waits for button presses
while (true) {
// Yield until a button is pressed
Expand All @@ -123,10 +126,12 @@ int main(void) {
// Handle long presses (program new secret)
if (new_val) {
program_new_secret(&stored_key);
display_hotp_keys(&stored_key, 1);

// Handle short presses on already configured keys (output next code)
} else if (stored_key.len > 0) {
get_next_code(&stored_key, KEY_DIGITS);
display_hotp_keys(&stored_key, 1);

// Error for short press on a non-configured key
} else if (stored_key.len == 0) {
Expand Down
63 changes: 63 additions & 0 deletions examples/tutorials/hotp/hotp_milestone_one/screen.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <u8g2-tock.h>
#include <u8g2.h>

#include "screen.h"
#include "step0.h"

u8g2_t u8g2;
bool inited = false;

int display_hotp_keys(hotp_key_t* hotp_key, int num_keys) {
// Only init if not previously init-ed.
if (!inited) {
int ret = u8g2_tock_init(&u8g2);
if (ret != 0) {
return -1;
}
inited = true;
}

// int height = u8g2_GetDisplayHeight(&u8g2);
int width = u8g2_GetDisplayWidth(&u8g2);

u8g2_SetFont(&u8g2, u8g2_font_profont17_tr);
u8g2_SetFontPosTop(&u8g2);

u8g2_ClearBuffer(&u8g2);

// Draw title.
u8g2_DrawStr(&u8g2, 0, 0, "HOTP App");

// Draw line below title.
int title_height = u8g2_GetMaxCharHeight(&u8g2);
u8g2_DrawHLine(&u8g2, 0, title_height, width);

// Draw for each key.
u8g2_SetFont(&u8g2, u8g2_font_helvR08_tr);
int lines_start = title_height + 1;
int offset = u8g2_GetMaxCharHeight(&u8g2) + 1;

for (int i = 0; i < num_keys; i++) {
char buf[100];

int y_pos = lines_start + (offset * i);

if (hotp_key[i].len == 0) {
snprintf(buf, 100, "Key %i: (unused)", i);
} else {
snprintf(buf, 100, "Key %i: %li", i, (uint32_t) hotp_key[i].counter);
}
u8g2_DrawStr(&u8g2, 0, y_pos, buf);
}

// Write to display.
u8g2_SendBuffer(&u8g2);

return 0;

}
5 changes: 5 additions & 0 deletions examples/tutorials/hotp/hotp_milestone_one/screen.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#include "step0.h"

int display_hotp_keys(hotp_key_t* hotp_key, int num_keys);
3 changes: 3 additions & 0 deletions examples/tutorials/hotp/hotp_milestone_three/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ C_SRCS := $(wildcard *.c)
# Make sure we have storage permissions.
ELF2TAB_ARGS += --write_id 0x4016 --read_ids 0x4016 --access_ids 0x4016

# Include U8G2 graphics library.
EXTERN_LIBS += $(TOCK_USERLAND_BASE_DIR)/u8g2

# Include userland master makefile. Contains rules and flags for actually
# building the application.
include $(TOCK_USERLAND_BASE_DIR)/AppMakefile.mk
5 changes: 5 additions & 0 deletions examples/tutorials/hotp/hotp_milestone_three/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

// Local includes
#include "base32.h"
#include "screen.h"
#include "step0.h"
#include "step1.h"
#include "step2.h"
Expand Down Expand Up @@ -65,6 +66,8 @@ int main(void) {
program_default_secret(&stored_keys[0]);
}

display_hotp_keys(stored_keys, NUM_KEYS);

// Main loop. Waits for button presses
while (true) {
// Yield until a button is pressed
Expand All @@ -81,11 +84,13 @@ int main(void) {
if (new_val) {
program_new_secret(&stored_keys[btn_num]);
save_key(&stored_keys[btn_num], btn_num);
display_hotp_keys(stored_keys, NUM_KEYS);

} else if (btn_num < NUM_KEYS && stored_keys[btn_num].len > 0) {
// Handle short presses on already configured keys (output next code).
get_next_code(&stored_keys[btn_num], key_digits[btn_num]);
save_key(&stored_keys[btn_num], btn_num);
display_hotp_keys(stored_keys, NUM_KEYS);

} else if (stored_keys[btn_num].len == 0) {
// Error for short press on a non-configured key.
Expand Down
1 change: 1 addition & 0 deletions examples/tutorials/hotp/hotp_milestone_three/screen.c
1 change: 1 addition & 0 deletions examples/tutorials/hotp/hotp_milestone_three/screen.h
3 changes: 3 additions & 0 deletions examples/tutorials/hotp/hotp_milestone_two/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ C_SRCS := $(wildcard *.c)
# Make sure we have storage permissions.
ELF2TAB_ARGS += --write_id 0x4016 --read_ids 0x4016 --access_ids 0x4016

# Include U8G2 graphics library.
EXTERN_LIBS += $(TOCK_USERLAND_BASE_DIR)/u8g2

# Include userland master makefile. Contains rules and flags for actually
# building the application.
include $(TOCK_USERLAND_BASE_DIR)/AppMakefile.mk
5 changes: 5 additions & 0 deletions examples/tutorials/hotp/hotp_milestone_two/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

// Local includes
#include "base32.h"
#include "screen.h"
#include "step0.h"
#include "step1.h"

Expand Down Expand Up @@ -108,6 +109,8 @@ int main(void) {
program_default_secret(&stored_key);
}

display_hotp_keys(&stored_key, 1);

// Main loop. Waits for button presses
while (true) {
// Yield until a button is pressed
Expand All @@ -124,11 +127,13 @@ int main(void) {
if (new_val) {
program_new_secret(&stored_key);
save_key(&stored_key, 0);
display_hotp_keys(&stored_key, 1);

// Handle short presses on already configured keys (output next code)
} else if (stored_key.len > 0) {
get_next_code(&stored_key, KEY_DIGITS);
save_key(&stored_key, 0);
display_hotp_keys(&stored_key, 1);

// Error for short press on a non-configured key
} else if (stored_key.len == 0) {
Expand Down
1 change: 1 addition & 0 deletions examples/tutorials/hotp/hotp_milestone_two/screen.c
1 change: 1 addition & 0 deletions examples/tutorials/hotp/hotp_milestone_two/screen.h

0 comments on commit a574756

Please sign in to comment.