diff options
author | joeycastillo <joeycastillo@utexas.edu> | 2021-11-24 12:26:16 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-24 12:26:16 -0500 |
commit | 4a71def034590236b8a3527960cb2331876529a5 (patch) | |
tree | 482d9ed26504c9af16f80393accd34b6c8c40106 /movement/watch_faces | |
parent | 7817e6696e438c8af74342ef10b576975a0e5448 (diff) | |
parent | 1a1a862d7941f720df79fb62fa03211cf083e127 (diff) | |
download | Sensor-Watch-4a71def034590236b8a3527960cb2331876529a5.tar.gz Sensor-Watch-4a71def034590236b8a3527960cb2331876529a5.tar.bz2 Sensor-Watch-4a71def034590236b8a3527960cb2331876529a5.zip |
Merge pull request #23 from tahnok/totp
Add initial TOTP watch face impl
Diffstat (limited to 'movement/watch_faces')
-rw-r--r-- | movement/watch_faces/complications/totp_face.c | 81 | ||||
-rw-r--r-- | movement/watch_faces/complications/totp_face.h | 26 |
2 files changed, 107 insertions, 0 deletions
diff --git a/movement/watch_faces/complications/totp_face.c b/movement/watch_faces/complications/totp_face.c new file mode 100644 index 00000000..4aa1382a --- /dev/null +++ b/movement/watch_faces/complications/totp_face.c @@ -0,0 +1,81 @@ +/** + * TODO: + * - Add support for UTC offset in settings? + * - Support for multiple codes + */ +#include <stdlib.h> +#include <string.h> +#include "totp_face.h" +#include "watch.h" +#include "watch_utility.h" +#include "TOTP.h" + +// test key: JBSWY3DPEHPK3PXP +// Use https://cryptii.com/pipes/base32-to-hex to convert base32 to hex +// Use https://totp.danhersam.com/ to generate test codes for verification +static uint8_t hmacKey[] = {0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x21, 0xde, 0xad, 0xbe, 0xef}; // Secret key + + +static const uint8_t UTC_OFFSET = 5; // set to your current UTC offset +static const uint32_t TIMESTEP = 30; + +void totp_face_setup(movement_settings_t *settings, void ** context_ptr) { + (void) settings; + if (*context_ptr == NULL) *context_ptr = malloc(sizeof(totp_state_t)); + TOTP(hmacKey, sizeof(hmacKey), TIMESTEP); +} + +void totp_face_activate(movement_settings_t *settings, void *context) { + (void) settings; + memset(context, 0, sizeof(totp_state_t)); + totp_state_t *totp_state = (totp_state_t *)context; + totp_state->timestamp = watch_utility_date_time_to_unix_time(watch_rtc_get_date_time(), UTC_OFFSET); + totp_state->current_code = getCodeFromTimestamp(totp_state->timestamp); +} + +bool totp_face_loop(movement_event_t event, movement_settings_t *settings, void *context) { + (void) settings; + + totp_state_t *totp_state = (totp_state_t *)context; + char buf[14]; + uint8_t valid_for; + div_t result; + + switch (event.event_type) { + case EVENT_TICK: + totp_state->timestamp++; + // fall through + case EVENT_ACTIVATE: + result = div(totp_state->timestamp, TIMESTEP); + if (result.quot != totp_state->steps) { + totp_state->current_code = getCodeFromTimestamp(totp_state->timestamp); + totp_state->steps = result.quot; + } + valid_for = TIMESTEP - result.rem; + sprintf(buf, "2f%2d%06lu", valid_for, totp_state->current_code); + + watch_display_string(buf, 0); + break; + case EVENT_MODE_BUTTON_UP: + movement_move_to_next_face(); + break; + case EVENT_LIGHT_BUTTON_DOWN: + movement_illuminate_led(); + break; + case EVENT_TIMEOUT: + movement_move_to_face(0); + break; + case EVENT_ALARM_BUTTON_DOWN: + case EVENT_ALARM_BUTTON_UP: + case EVENT_ALARM_LONG_PRESS: + default: + break; + } + + return true; +} + +void totp_face_resign(movement_settings_t *settings, void *context) { + (void) settings; + (void) context; +} diff --git a/movement/watch_faces/complications/totp_face.h b/movement/watch_faces/complications/totp_face.h new file mode 100644 index 00000000..0527627a --- /dev/null +++ b/movement/watch_faces/complications/totp_face.h @@ -0,0 +1,26 @@ +#ifndef TOTP_FACE_H_ +#define TOTP_FACE_H_ + +#include "movement.h" + +typedef struct { + uint32_t timestamp; + uint8_t steps; + uint32_t current_code; + +} totp_state_t; + +void totp_face_setup(movement_settings_t *settings, void ** context_ptr); +void totp_face_activate(movement_settings_t *settings, void *context); +bool totp_face_loop(movement_event_t event, movement_settings_t *settings, void *context); +void totp_face_resign(movement_settings_t *settings, void *context); + +static const watch_face_t totp_face = { + totp_face_setup, + totp_face_activate, + totp_face_loop, + totp_face_resign, + NULL +}; + +#endif // TOTP_FACE_H_ |