CDC Badge OS
Firmware for the CDC Badge v1.0 hardware security key
Loading...
Searching...
No Matches
PinEntryView.h
Go to the documentation of this file.
1#pragma once
2
3#include "cdc_ui/IView.h"
4#include <cstdint>
5
6namespace cdc::ui {
7
20class PinEntryView : public ViewBase {
21public:
22 static constexpr uint8_t MAX_PIN_LENGTH = 8;
23 static constexpr uint32_t LOCKOUT_DURATION_MS = 60000; // 1 minute lockout
24
30 using VerifyCallback = bool(*)(const char* pin);
31
35 using SuccessCallback = void(*)();
36
40 using CancelCallback = void(*)();
41
46 using FailureCallback = void(*)(bool lockedOut);
47
54 void init(const char* title, uint8_t maxPinLength = 6, uint8_t maxAttempts = 3);
55
59 void setOnVerify(VerifyCallback callback) { onVerify_ = callback; }
60
64 void setOnSuccess(SuccessCallback callback) { onSuccess_ = callback; }
65
69 void setOnCancel(CancelCallback callback) { onCancel_ = callback; }
70
74 void setOnFailure(FailureCallback callback) { onFailure_ = callback; }
75
79 void setShowMessages(bool enabled) { showMessages_ = enabled; }
80
84 const char* getPin() const { return buffer_; }
85
89 uint8_t getAttempts() const { return attempts_; }
90
94 bool isLockedOut() const { return lockedOut_; }
95
99 void clear();
100
104 void resetAttempts() { attempts_ = 0; lockedOut_ = false; }
105
110 void setMinLength(uint8_t minLen) { minLength_ = minLen; }
111
115 uint32_t getLockoutRemaining() const;
116
117 // IView implementation
118 void onEnter(void* context) override;
119 void onTick(uint32_t nowMs) override;
120 void render(bool partial) override;
121 InputResult onKey(char key) override;
122 const char* getName() const override { return "PinEntryView"; }
123 const char* getFooterHint() const override;
124
125private:
126 const char* title_ = nullptr;
127 char buffer_[MAX_PIN_LENGTH + 1] = {};
128 uint8_t length_ = 0;
129 uint8_t maxLength_ = 6;
130 uint8_t minLength_ = 4;
131 uint8_t attempts_ = 0;
132 uint8_t maxAttempts_ = 3;
133 bool lockedOut_ = false;
134 uint32_t lockoutStartMs_ = 0;
135
136 VerifyCallback onVerify_ = nullptr;
137 SuccessCallback onSuccess_ = nullptr;
138 CancelCallback onCancel_ = nullptr;
139 FailureCallback onFailure_ = nullptr;
140 bool showMessages_ = true;
141
142 void addDigit(char digit);
143 void backspace();
144 void verify();
145};
146
147// ============================================================================
148// Convenience Functions
149// ============================================================================
150
168PinEntryView* showPinEntry(const char* title,
171 uint8_t maxLength = 6,
172 uint8_t minLength = 4,
173 uint8_t maxAttempts = 3);
174
175} // namespace cdc::ui
bool isLockedOut() const
uint8_t getAttempts() const
void setShowMessages(bool enabled)
void init(const char *title, uint8_t maxPinLength=6, uint8_t maxAttempts=3)
Initializes PIN entry configuration and clears current input.
void setOnFailure(FailureCallback callback)
static constexpr uint8_t MAX_PIN_LENGTH
const char * getName() const override
void setOnVerify(VerifyCallback callback)
void(*)(bool lockedOut) FailureCallback
InputResult onKey(char key) override
Handles key input for PIN entry and actions.
void onEnter(void *context) override
Resets entry state when the view is entered.
void setMinLength(uint8_t minLen)
void setOnSuccess(SuccessCallback callback)
void setOnCancel(CancelCallback callback)
void onTick(uint32_t nowMs) override
Updates lockout state and periodic countdown refresh.
static constexpr uint32_t LOCKOUT_DURATION_MS
const char * getPin() const
const char * getFooterHint() const override
Returns localized footer hint text.
void clear()
Clears the internal PIN buffer.
uint32_t getLockoutRemaining() const
Returns remaining lockout time.
bool(*)(const char *pin) VerifyCallback
const char * title_
Definition IView.h:202
Centralized key-code constants for cdc_views.
Definition IModule.h:8
InputResult
Definition IView.h:10
PinEntryView * showPinEntry(const char *title, PinEntryView::VerifyCallback onVerify, PinEntryView::SuccessCallback onSuccess, uint8_t maxLength=6, uint8_t minLength=4, uint8_t maxAttempts=3)
Shows a shared PIN entry view instance.