CDC Badge OS
Firmware for the CDC Badge v1.0 hardware security key
Loading...
Searching...
No Matches
BleUartService.h
Go to the documentation of this file.
1#pragma once
2
4#include <atomic>
5#include <cstdint>
6#include <cstddef>
7#include <functional>
8
9namespace cdc::mod_ble_serial {
10
19class BleUartService {
20public:
25 bool init();
26
30 void deinit();
31
35 bool isInitialized() const { return initialized_; }
36
37 // === TX (Badge -> Phone) ===
38
45 size_t send(const uint8_t* data, size_t len);
46
52 size_t send(const char* str);
53
57 bool txReady() const;
58
59 // === RX (Phone -> Badge) ===
60
65 size_t available() const;
66
71 int getchar();
72
79 size_t read(uint8_t* buf, size_t maxLen);
80
81 // === Connection State ===
82
86 bool isConnected() const;
87
88 // === Callbacks (invoked by API) ===
89
93 void onRxData(const uint8_t* data, size_t len);
94
98 void onConnectionChange(bool connected);
99
100 // === Application Callbacks ===
101
102 using ConnectCallback = std::function<void()>;
103 using DisconnectCallback = std::function<void()>;
104
105 void setOnConnect(ConnectCallback cb) { onConnect_ = cb; }
106 void setOnDisconnect(DisconnectCallback cb) { onDisconnect_ = cb; }
107
108 // === Singleton ===
109 static BleUartService& instance();
110
111private:
112 BleUartService() = default;
113
114 bool initialized_ = false;
115
116 // GATT handle for TX characteristic (populated by registerGattService)
117 uint16_t txCharHandle_ = 0;
118
119 // Callbacks
120 ConnectCallback onConnect_;
121 DisconnectCallback onDisconnect_;
122
123 // RX ring buffer with atomic indices for safe interaction between the
124 // BLE host task (writer) and the application task (reader).
125 static constexpr size_t RX_BUFFER_SIZE = 1024;
126 uint8_t rxBuffer_[RX_BUFFER_SIZE] = {};
127 std::atomic<size_t> rxHead_{0};
128 std::atomic<size_t> rxTail_{0};
129
130 // TX state
131 volatile bool txCongested_ = false;
132 volatile bool txInProgress_ = false; // Recursion guard
133
134 // BLE callback tokens for safe removal on deinit.
139};
140
141} // namespace cdc::mod_ble_serial
static constexpr ListenerToken INVALID_LISTENER
void onConnectionChange(bool connected)
Handles BLE connection state changes.
void setOnConnect(ConnectCallback cb)
void deinit()
Deinitializes BLE UART service runtime state.
int getchar()
Reads one byte from RX ring buffer.
size_t available() const
Returns number of buffered RX bytes.
std::function< void()> ConnectCallback
size_t read(uint8_t *buf, size_t maxLen)
Reads up to maxLen bytes from RX ring buffer.
bool isConnected() const
Returns whether BLE link is currently connected.
void onRxData(const uint8_t *data, size_t len)
Appends received BLE UART data into RX ring buffer.
std::function< void()> DisconnectCallback
bool txReady() const
Returns whether TX path is currently ready.
size_t send(const uint8_t *data, size_t len)
Sends binary payload to connected BLE peer via notifications.
void setOnDisconnect(DisconnectCallback cb)
bool init()
Initializes Nordic UART Service over BLE GATT.
static BleUartService & instance()
Returns singleton BLE UART service instance.