9#include "cdc_msg/MessageTransfer.h"
15#include "freertos/FreeRTOS.h"
16#include "freertos/task.h"
21static const char*
TAG =
"BleMenu";
92 uint16_t index,
int x,
int y,
int w,
int h,
93 bool selected,
void* userCtx) {
98 if (!gfx || !item.
userData)
return false;
102 gfx->setFont(
nullptr);
104 int baseline = y + 6;
109 char nameDisplay[36];
110 strncpy(nameDisplay, dev->name,
sizeof(nameDisplay) - 1);
111 nameDisplay[
sizeof(nameDisplay) - 1] =
'\0';
112 gfx->setCursor(x + 22, baseline);
117 snprintf(rssiBuf,
sizeof(rssiBuf),
"%d", dev->rssi);
120 gfx->getTextBounds(rssiBuf, 0, 0, &rx1, &ry1, &rw, &rh);
121 gfx->setCursor(x + w - rw - 4, baseline);
147 bool enabled = ble && ble->isEnabled();
159 auto& msg = cdc::msg::MessageTransfer::instance();
161 msg.isBeaconEnabled() ?
ui::tr(
"core.msg_beacon_on") :
ui::tr(
"core.msg_beacon_off"),
162 static_cast<uint8_t
>(msg.isBeaconActive() ?
'*' : 0),
false,
nullptr};
222 auto& msg = cdc::msg::MessageTransfer::instance();
223 msg.setBeaconEnabled(!msg.isBeaconEnabled());
231 ble->clearAllBonds();
242 IView* view = item.getView();
246 }
else if (item.onSelect) {
263 if (ble->isEnabled()) {
287 auto append = [&](
const char* fmt, ...) {
294 size_t w =
static_cast<size_t>(written);
300 append(
"BLE: %s\n", ble->isEnabled() ?
"ON" :
"OFF");
303 if (ble->getMacAddress(mac)) {
304 append(
"MAC: %02X:%02X:%02X:%02X:%02X:%02X\n",
305 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
308 if (ble->isConnected()) {
309 append(
"\n%s\n",
ui::tr(
"core.ble_connected_to"));
310 append(
"%s: %d dBm\n",
ui::tr(
"core.ble_signal"), ble->getRssi());
312 append(
"\n%s\n",
ui::tr(
"core.ble_not_connected"));
315 append(
"\nName: %s\n", ble->getDeviceName());
369 snprintf(out, n,
"%02X:%02X:%02X:%02X:%02X:%02X",
414 const uint8_t* data, uint16_t len) {
417 if (data && len > 0) {
419 n = len < cap ? len : cap;
430 ble->cancelConnect();
453 LOG_I(
TAG,
"Resolving [%u] %02X:%02X:%02X:%02X:%02X:%02X (addrType %u)",
455 dev.mac[2], dev.mac[1], dev.mac[0], dev.addrType);
456 if (!ble->connect(dev.mac, dev.addrType)) {
470 LOG_W(
TAG,
"Name resolve not started (ble=%d enabled=%d count=%u)",
497 ble->cancelConnect();
531 LOG_W(
TAG,
"discoverServiceByUuid failed");
534 }
else if (deadline) {
552 }
else if (deadline) {
572 }
else if (deadline) {
602 const char*
getName()
const override {
return "BleScanView"; }
610 if (!ble || !ble->isEnabled()) {
619 uint32_t startMs = esp_timer_get_time() / 1000;
620 while (!ble->isScanComplete()) {
621 vTaskDelay(pdMS_TO_TICKS(100));
649 static char title[32];
669 "%02X:%02X:%02X:%02X:%02X:%02X %s",
670 b.addr[5], b.addr[4], b.addr[3], b.addr[2], b.addr[1], b.addr[0],
671 b.addrType == 0 ?
"pub" :
"rnd");
673 static_cast<uint8_t
>(b.connected ?
'*' : 0),
712 if (!ble || !ble->isEnabled()) {
char name[cdc::hal::ISecureElement::RMEM_NAME_LEN]
CDC Log: logging over TinyUSB CDC and UART.
#define LOG_W(tag, fmt,...)
#define LOG_I(tag, fmt,...)
static ModuleRegistry & instance()
Returns the singleton module registry instance.
static constexpr uint8_t MAX_SCAN_RESULTS
static constexpr ListenerToken INVALID_LISTENER
static constexpr uint8_t MAX_BONDED_DEVICES
Discoverable/pairing-mode screen.
ListView for the BLE scan results that drives async name resolution while visible and cancels it when...
void onEnter(void *context) override
const char * getName() const override
void onTick(uint32_t nowMs) override
virtual void onResume()=0
virtual void onEnter(void *context=nullptr)=0
static ViewStack & instance()
Returns singleton view-stack instance.
void push(IView *view, void *context=nullptr)
IBluetoothController * getBluetoothControllerInstance()
Returns singleton Bluetooth stub when NimBLE is unavailable.
void printText(Gdey029T94 *gfx, const char *text)
Draws CP437 text with the built-in 6x8 glyph font, byte-for-byte.
Centralized key-code constants for cdc_views.
const char * tr(const char *key)
Look up a translation by string key.
static constexpr BleListenerToken kInvalidListener
void askConfirm(const char *message, ConfirmView::ConfirmCallback onYes, void *userData=nullptr)
static void startBluetoothScan()
Starts BLE scan and opens result list.
static constexpr uint8_t BLE_MAX_BONDS
static char s_bleStatusBuf[BLE_STATUS_BUF_SIZE]
static void sortBleScanResults()
Sorts BLE scan results by RSSI descending.
static constexpr uint8_t BT_MENU_MAX_ITEMS
static void rebuildPairedList()
Rebuilds the paired-device list from the current bond store.
static void bleResolveStop()
static void rOnDisconnect(uint16_t connHandle, int)
static BleListenerToken s_tokResSvc
static uint16_t s_resolveNameHandle
static void bleResolveCleanupToSettle(uint32_t nowMs)
Tears down the current attempt and waits briefly for a clean stack.
static constexpr uint32_t BLE_RESOLVE_DEVICE_TIMEOUT_MS
static BleListenerToken s_tokResRead
static volatile bool s_evtConnected
InfoView * showInfo(const char *title, const char *text, const char *hint=nullptr)
Shows a shared info view instance and pushes it onto the view stack.
static constexpr uint32_t BLE_SCAN_TIMEOUT_MS
static void showBluetoothStatus()
Shows BLE status and adapter information.
static volatile uint16_t s_evtConnHandle
static char s_pairedLabels[BLE_MAX_BONDS][24]
void rebuildToolsMenu()
Rebuilds tools menu entries including dynamic module tools.
static volatile uint16_t s_evtNameHandle
static void rOnServiceDiscovered(uint16_t connHandle, const hal::IBluetoothController::DiscoveredService *svc, bool complete)
static void bleResolveStart()
static constexpr size_t BLE_STATUS_BUF_SIZE
PSRAM-backed text buffer used for BLE status details.
static ListView * s_bluetoothMenu
Bluetooth menu and scan state.
static char s_resolveNameBuf[32]
hal::IBluetoothController::ListenerToken BleListenerToken
static constexpr uint16_t BLE_GAP_SVC_UUID
static constexpr uint32_t BLE_RESOLVE_SETTLE_TIMEOUT_MS
static hal::BleScanResult s_bleScanResults[BLE_MAX_SCAN_RESULTS]
static void rOnConnect(uint16_t connHandle)
static uint8_t s_pairedCount
static uint8_t s_bluetoothModuleCount
static ListView * s_bleScanView
BLE scan result list state.
static volatile bool s_evtDiscovered
static void bleResolveMac(const hal::BleScanResult &d, char *out, size_t n)
Formats a scan result's MAC the same way as the no-name fallback.
static bool renderBleRow(Gdey029T94 *gfx, const ListItem &item, uint16_t index, int x, int y, int w, int h, bool selected, void *userCtx)
Renders one BLE scan row with signal bars and RSSI text.
static uint16_t s_resolveConn
static void onPairedSelect(uint16_t index, void *userData)
Asks for confirmation to forget the selected paired device.
static ListItem s_pairedItems[BLE_MAX_BONDS]
static constexpr uint8_t BLE_MAX_SCAN_RESULTS
static bool bleDeviceNeedsName(uint8_t i)
True when the row still shows the MAC fallback (no name yet).
static BleResolvePhase s_resolvePhase
static bool s_resolveActive
static void onBluetoothMenuSelect(uint16_t index, void *userData)
Handles Bluetooth menu selection.
static void bleResolveClearEvents()
static volatile bool s_evtGotName
static ListView * s_pairedView
Paired (bonded) device list state.
static uint32_t s_resolveSettleStartMs
static volatile bool s_evtDisconnected
static void toggleBluetoothEnable()
Toggles BLE controller enabled state.
void drawSignalBars(Gdey029T94 *gfx, int x, int y, int8_t rssi, bool inverted)
Draws RSSI signal bars using the shared lock-screen visual style.
static void showPairedDevices()
Shows the list of paired (bonded) devices.
void showToastSuccess(const char *message, uint16_t durationMs=1500)
Shows a success toast message.
static uint32_t s_resolveDeviceStartMs
static constexpr uint16_t BLE_DEV_NAME_UUID
static ListItem s_bleScanItems[BLE_MAX_SCAN_RESULTS]
void showBluetoothMenu()
Shows top-level Bluetooth menu.
static BleListenerToken s_tokResDisc
static void onForgetConfirm(void *)
Confirmed-forget handler: unpairs the selected device and refreshes.
static void bleResolveTick(uint32_t nowMs)
static hal::BleBondInfo s_pairedBonds[BLE_MAX_BONDS]
static ListItem s_bluetoothItems[BT_MENU_MAX_ITEMS]
void rebuildBluetoothMenu()
Rebuilds Bluetooth menu entries and dynamic module items.
void showToastInfo(const char *message, uint16_t durationMs=1500)
Shows an informational toast message.
void showToastError(const char *message, uint16_t durationMs=1500)
Shows an error toast message.
static BleListenerToken s_tokResConn
static uint8_t s_resolveIndex
BluetoothMenuIdx
Bluetooth fixed menu indices and capacity limits.
static void rOnCharRead(uint16_t connHandle, uint16_t, const uint8_t *data, uint16_t len)
static uint8_t s_bleScanCount
static void bleResolveBeginNext(uint32_t nowMs)
Starts resolving the next device that still shows its MAC.
static hal::BleBondInfo s_forgetTarget
static core::ModuleMenuItem s_bluetoothModuleItems[12]
static BleUuid from16(uint16_t v)
DiscoveredCharacteristic characteristics[MAX_DISCOVERED_CHARS]
uint8_t numCharacteristics