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 onLockedAccept_ = nullptr;
29 lockedAcceptUd_ = nullptr;
30 dirty_ = true;
31}
32
33void BlePairingPromptView::onEnter(void* context) {
34 (void)context;
35 dirty_ = true;
36}
37
38void BlePairingPromptView::onTick(uint32_t nowMs) {
39 if (responded_) return;
40 if (enteredAtMs_ == 0) {
41 enteredAtMs_ = nowMs;
42 return;
43 }
44 if (nowMs - enteredAtMs_ >= timeoutMs_) {
45 LOG_W(TAG, "Pairing prompt timed out, rejecting");
46 respond(false);
47 }
48}
49
51 if (responded_) return InputResult::CONSUMED;
52
53 if (key == KEY_YES) {
54 if (onLockedAccept_) {
55 // Locked: do not pair yet. Dismiss the prompt and hand off to the
56 // PIN-unlock flow; the pairing is accepted only once the PIN succeeds.
57 responded_ = true;
59 onLockedAccept_(lockedAcceptUd_);
60 } else {
61 respond(true);
62 }
64 }
65 if (key == KEY_NO) {
66 respond(false);
68 }
70}
71
72void BlePairingPromptView::respond(bool accept) {
73 responded_ = true;
75 if (ble) {
76 ble->respondToNumericComparison(connHandle_, accept);
77 }
78 LOG_I(TAG, "Pairing %s by user", accept ? "accepted" : "rejected");
80}
81
82void BlePairingPromptView::render(bool partial) {
83 (void)partial;
84
86 if (!display) return;
87 auto* gfx = static_cast<Gdey029T94*>(display->getNativeHandle());
88 if (!gfx) return;
89
90 constexpr int BOX_W = 240;
91 constexpr int BOX_H = 96;
92 const int boxX = (display->getWidth() - BOX_W) / 2;
93 const int boxY = (display->getHeight() - BOX_H) / 2;
94
95 render::drawDialogFrame(gfx, boxX, boxY, BOX_W, BOX_H);
96
97 gfx->setTextColor(EPD_BLACK);
98 gfx->setTextSize(1);
99
100 gfx->setCursor(boxX + 14, boxY + 14);
101 gfx->print("BLE Pairing Request");
102
103 char codeBuf[16];
104 snprintf(codeBuf, sizeof(codeBuf), "%03lu %03lu",
105 (unsigned long)(passkey_ / 1000), (unsigned long)(passkey_ % 1000));
106
107 gfx->setTextSize(2);
108 gfx->setCursor(boxX + 48, boxY + 36);
109 gfx->print(codeBuf);
110
111 gfx->setTextSize(1);
112 gfx->setCursor(boxX + 14, boxY + BOX_H - 16);
113 gfx->print("[Y] Accept [N] Reject");
114
115 clearDirty();
116}
117
118} // 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:198
static ViewStack & instance()
Returns singleton view-stack instance.
Definition ViewStack.cpp:53
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:11
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