CDC Badge OS
Firmware for the CDC Badge v1.0 hardware security key
Loading...
Searching...
No Matches
BlePairingPromptView.cpp
Go to the documentation of this file.
1
5
10#include "cdc_hal/IDisplay.h"
11#include "cdc_ui/ViewStack.h"
12#include "cdc_log.h"
13
14#include <goodisplay/gdey029T94.h>
15#include <cstdio>
16
17static const char* TAG = "BLE_PAIR_UI";
18
19namespace cdc::ui {
20
21void BlePairingPromptView::prepare(uint16_t connHandle, uint32_t passkey,
22 uint32_t timeoutMs) {
23 connHandle_ = connHandle;
24 passkey_ = passkey;
25 timeoutMs_ = timeoutMs;
26 responded_ = false;
27 enteredAtMs_ = 0;
28 dirty_ = true;
29}
30
31void BlePairingPromptView::onEnter(void* context) {
32 (void)context;
33 dirty_ = true;
34}
35
36void BlePairingPromptView::onTick(uint32_t nowMs) {
37 if (responded_) return;
38 if (enteredAtMs_ == 0) {
39 enteredAtMs_ = nowMs;
40 return;
41 }
42 if (nowMs - enteredAtMs_ >= timeoutMs_) {
43 LOG_W(TAG, "Pairing prompt timed out, rejecting");
44 respond(false);
45 }
46}
47
49 if (responded_) return InputResult::CONSUMED;
50
51 if (key == KEY_YES) {
52 respond(true);
54 }
55 if (key == KEY_NO) {
56 respond(false);
58 }
60}
61
62void BlePairingPromptView::respond(bool accept) {
63 responded_ = true;
65 if (ble) {
66 ble->respondToNumericComparison(connHandle_, accept);
67 }
68 LOG_I(TAG, "Pairing %s by user", accept ? "accepted" : "rejected");
70}
71
72void BlePairingPromptView::render(bool partial) {
73 (void)partial;
74
76 if (!display) return;
77 auto* gfx = static_cast<Gdey029T94*>(display->getNativeHandle());
78 if (!gfx) return;
79
80 constexpr int BOX_W = 240;
81 constexpr int BOX_H = 96;
82 const int boxX = (display->getWidth() - BOX_W) / 2;
83 const int boxY = (display->getHeight() - BOX_H) / 2;
84
85 render::drawDialogFrame(gfx, boxX, boxY, BOX_W, BOX_H);
86
87 gfx->setTextColor(EPD_BLACK);
88 gfx->setTextSize(1);
89
90 gfx->setCursor(boxX + 14, boxY + 14);
91 gfx->print("BLE Pairing Request");
92
93 char codeBuf[16];
94 snprintf(codeBuf, sizeof(codeBuf), "%03lu %03lu",
95 (unsigned long)(passkey_ / 1000), (unsigned long)(passkey_ % 1000));
96
97 gfx->setTextSize(2);
98 gfx->setCursor(boxX + 48, boxY + 36);
99 gfx->print(codeBuf);
100
101 gfx->setTextSize(1);
102 gfx->setCursor(boxX + 14, boxY + BOX_H - 16);
103 gfx->print("[Y] Accept [N] Reject");
104
105 clearDirty();
106}
107
108} // namespace cdc::ui
static const char * TAG
CDC Log: logging over TinyUSB CDC and UART.
#define LOG_W(tag, fmt,...)
Definition cdc_log.h:146
#define LOG_I(tag, fmt,...)
Definition cdc_log.h:147
void onTick(uint32_t nowMs) override
void render(bool partial) override
void onEnter(void *context) override
void prepare(uint16_t connHandle, uint32_t passkey, uint32_t timeoutMs=30000)
InputResult onKey(char key) override
void clearDirty() override
Definition IView.h:187
static ViewStack & instance()
Returns singleton view-stack instance.
Definition ViewStack.cpp:34
IDisplay * getDisplayInstance()
Returns lazily created singleton display instance.
IBluetoothController * getBluetoothControllerInstance()
Returns singleton Bluetooth stub when NimBLE is unavailable.
void drawDialogFrame(Gdey029T94 *gfx, int x, int y, int w, int h)
Draws a framed dialog box with double border.
Centralized key-code constants for cdc_views.
Definition IModule.h:8
Gdey029T94 * display
InputResult
Definition IView.h:10
static constexpr char KEY_NO
Cancel / Back / Backspace.
Definition KeyCodes.h:44
static const char * TAG
static constexpr char KEY_YES
Confirm / OK / Save.
Definition KeyCodes.h:41