Skip to content

Commit 28c29bb

Browse files
committed
if no touch sensor pin defined, fallback to bootsel button; added cmd * to delete all 2FA secrets from EEPROM;
1 parent 78a3aae commit 28c29bb

File tree

4 files changed

+126
-22
lines changed

4 files changed

+126
-22
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include "BootselButton.h"
2+
3+
void BootselButton::loop(uint8_t brightness) {
4+
if (BOOTSEL) {
5+
longTouchCancelledHandled = false;
6+
if (!longTouchEndHandled && brightness == 0) {
7+
onLongTouchEnd();
8+
longTouchEndHandled = true;
9+
}
10+
if (!isTouched) {
11+
isTouched = true;
12+
touchStartTime = millis();
13+
longTouchDetected = false;
14+
15+
while (BOOTSEL) {
16+
if ((millis() - touchStartTime) > LONG_PRESS_DURATION) {
17+
longTouchDetected = true;
18+
break;
19+
}
20+
delay(10);
21+
}
22+
if (longTouchDetected && !longTouchStartHandled) {
23+
onLongTouchStart();
24+
longTouchStartHandled = true;
25+
singleTouchHandled = true;
26+
27+
} else if (!longTouchDetected && !singleTouchHandled) {
28+
onSingleTouch();
29+
singleTouchHandled = true;
30+
delay(100); // delay to prevent repeated calls
31+
}
32+
}
33+
} else {
34+
// when the touch is released
35+
if (longTouchDetected && !longTouchCancelledHandled && !longTouchEndHandled && longTouchStartHandled) {
36+
onLongTouchCancelled();
37+
longTouchCancelledHandled = true;
38+
}
39+
// reset flags
40+
isTouched = false;
41+
singleTouchHandled = false;
42+
longTouchStartHandled = false;
43+
longTouchEndHandled = false;
44+
}
45+
delay(10);
46+
}

turtlpass-firmware/BootselButton.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#ifndef BOOTSEL_BUTTON_H
2+
#define BOOTSEL_BUTTON_H
3+
4+
#include <Arduino.h>
5+
#include "LedManager.h"
6+
7+
#define LONG_PRESS_DURATION 500
8+
#define SHORT_DEBOUNCE_DURATION 100
9+
10+
typedef void (*OnSingleTouchCallback)();
11+
typedef void (*OnLongTouchStartCallback)();
12+
typedef void (*OnLongTouchEndCallback)();
13+
typedef void (*OnLongTouchCancelledCallback)();
14+
15+
class BootselButton {
16+
public:
17+
BootselButton(OnSingleTouchCallback singleTouch, OnLongTouchStartCallback longTouchStart, OnLongTouchEndCallback longTouchEnd, OnLongTouchCancelledCallback longTouchCancelled)
18+
: onSingleTouch(singleTouch), onLongTouchStart(longTouchStart), onLongTouchEnd(longTouchEnd), onLongTouchCancelled(longTouchCancelled) {
19+
}
20+
void loop(uint8_t brightness);
21+
22+
private:
23+
OnSingleTouchCallback onSingleTouch;
24+
OnLongTouchStartCallback onLongTouchStart;
25+
OnLongTouchEndCallback onLongTouchEnd;
26+
OnLongTouchCancelledCallback onLongTouchCancelled;
27+
bool isTouched = false;
28+
bool singleTouchHandled = false;
29+
bool longTouchDetected = false;
30+
bool longTouchStartHandled = false;
31+
bool longTouchEndHandled = false;
32+
bool longTouchCancelledHandled = false;
33+
unsigned long touchStartTime = 0;
34+
};
35+
36+
#endif // BOOTSEL_BUTTON_H

turtlpass-firmware/TTP223.h

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,11 @@
77
*
88
* This class handles the touch sensor events of the Mode (D)efault.
99
*/
10-
#ifndef TTP223_H
11-
#define TTP223_H
10+
#ifndef TTP_223_H
11+
#define TTP_223_H
1212

1313
#include "LedManager.h"
1414

15-
#if defined(__TURTLPASS_PIN_TTP223__)
16-
#define PIN_TTP223_SENSOR __TURTLPASS_PIN_TTP223__
17-
#else
18-
#define PIN_TTP223_SENSOR 2
19-
#endif
20-
2115
#define LONG_PRESS_DURATION 500
2216
#define SHORT_DEBOUNCE_DURATION 100
2317

@@ -50,4 +44,4 @@ class TTP223 {
5044
unsigned long touchStartTime = 0;
5145
};
5246

53-
#endif // TTP223_H
47+
#endif // TTP_223_H

turtlpass-firmware/turtlpass-firmware.ino

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,14 @@
3838
#include "Kdf.h"
3939
#include "OtpManager.h"
4040
#include "LedManager.h"
41-
#include "Aes256.h"
4241
#include "EncryptionManager.h"
4342
#include "Info.h"
43+
44+
#if defined(__TURTLPASS_PIN_TTP223__)
4445
#include "TTP223.h"
46+
#else
47+
#include "BootselButton.h"
48+
#endif
4549

4650
enum InternalState {
4751
IDLE = 0,
@@ -57,14 +61,19 @@ InternalState internalState = IDLE;
5761
LedManager ledManager;
5862
Kdf kdf;
5963
OtpManager otpManager;
60-
Aes256 aes256;
6164
EncryptionManager encryption;
6265

63-
void onSingleTouch(); // TTP223 callback
64-
void onLongTouchStart(); // TTP223 callback
65-
void onLongTouchEnd(); // TTP223 callback
66-
void onLongTouchCancelled(); // TTP223 callback
67-
TTP223 ttp223(PIN_TTP223_SENSOR, onSingleTouch, onLongTouchStart, onLongTouchEnd, onLongTouchCancelled); // initialize TTP223 instance with the callback function
66+
void onSingleTouch(); // TTP223 callback
67+
void onLongTouchStart(); // TTP223 callback
68+
void onLongTouchEnd(); // TTP223 callback
69+
void onLongTouchCancelled(); // TTP223 callback
70+
71+
#if defined(__TURTLPASS_PIN_TTP223__)
72+
TTP223 ttp223(__TURTLPASS_PIN_TTP223__, onSingleTouch, onLongTouchStart, onLongTouchEnd, onLongTouchCancelled); // initialize TTP223 instance with the callback functions
73+
#else
74+
// if no touch sensor pin defined, fallback to bootsel button
75+
BootselButton bootselButton(onSingleTouch, onLongTouchStart, onLongTouchEnd, onLongTouchCancelled); // initialize BootselButton instance with the callback functions
76+
#endif
6877

6978
const uint8_t INPUT_BUFFER_SIZE = 255;
7079
const uint8_t MIN_INPUT_SIZE = 1;
@@ -116,6 +125,10 @@ void onSingleTouch() {
116125
{
117126
if (otpManager.getCurrentOtpCode() > 0) {
118127
typeOtpCode();
128+
} else {
129+
Serial.println("<OTP-ERROR>");
130+
ledManager.setOn();
131+
internalState = IDLE;
119132
}
120133
break;
121134
}
@@ -210,13 +223,13 @@ void handleCommand() {
210223
{
211224
size_t inputLength = strlen(input);
212225
if (inputLength <= MIN_INPUT_SIZE || inputLength > MAX_INPUT_SIZE + 1) {
213-
Serial.println("<INVALID-LENGTH>");
226+
Serial.println("<PASSWORD-INVALID-LENGTH>");
214227
internalState = IDLE;
215228
break;
216229
}
217230
for (size_t i = 1; i < inputLength; i++) {
218231
if (!isHexadecimalDigit(input[i])) {
219-
Serial.println("<INVALID-HEX>");
232+
Serial.println("<PASSWORD-INVALID-INPUT>");
220233
internalState = IDLE;
221234
break;
222235
}
@@ -259,7 +272,13 @@ void handleCommand() {
259272
}
260273
case '?':
261274
{
262-
bool result = otpManager.readAllSavedData();
275+
otpManager.readAllSavedData();
276+
internalState = IDLE;
277+
break;
278+
}
279+
case '*':
280+
{
281+
otpManager.factoryReset();
263282
internalState = IDLE;
264283
break;
265284
}
@@ -294,7 +313,7 @@ void handleCommand() {
294313
default:
295314
{
296315
ledManager.setOn();
297-
Serial.println("<INVALID-CMD>");
316+
Serial.println("<UNKNOWN>");
298317
internalState = IDLE;
299318
break;
300319
}
@@ -330,14 +349,14 @@ void handleEncryption(InternalState state) {
330349
switch (state) {
331350
case ENCRYPTING:
332351
// encrypt
333-
if (!encryption.encrypt(encryptionByteBuffer, bytesReadNow)) {
352+
if (!encryption.encrypt2serial(encryptionByteBuffer, bytesReadNow)) {
334353
onEncryptionEnd();
335354
return;
336355
}
337356
break;
338357
case DECRYPTING:
339358
// decrypt
340-
if (!encryption.decrypt(encryptionByteBuffer, bytesReadNow)) {
359+
if (!encryption.decrypt2serial(encryptionByteBuffer, bytesReadNow)) {
341360
onEncryptionEnd();
342361
return;
343362
}
@@ -412,12 +431,21 @@ void setup() {
412431
Serial.begin(115200);
413432
Keyboard.begin();
414433
otpManager.begin(SIZE_EEPROM);
434+
435+
#if defined(__TURTLPASS_PIN_TTP223__)
415436
ttp223.begin();
437+
#endif
416438
}
417439

418440
void loop() {
419441
otpManager.loop();
442+
443+
#if defined(__TURTLPASS_PIN_TTP223__)
420444
ttp223.loop(FastLED.getBrightness());
445+
#else
446+
bootselButton.loop(FastLED.getBrightness());
447+
#endif
448+
421449
readSerial();
422450
}
423451

0 commit comments

Comments
 (0)