@@ -60,24 +60,21 @@ const uint8_t BLEUART_UUID_CHR_TXD[] =
60
60
0x93 , 0xF3 , 0xA3 , 0xB5 , 0x03 , 0x00 , 0x40 , 0x6E
61
61
};
62
62
63
- /* *
64
- * Constructor
65
- */
63
+ // Constructor
66
64
BLEUart::BLEUart (uint16_t fifo_depth)
67
65
: BLEService(BLEUART_UUID_SERVICE), _txd(BLEUART_UUID_CHR_TXD), _rxd(BLEUART_UUID_CHR_RXD)
68
66
{
69
67
_rx_fifo = NULL ;
70
- _rx_cb = NULL ;
71
68
_rx_fifo_depth = fifo_depth;
72
69
70
+ _rx_cb = NULL ;
71
+ _notify_cb = NULL ;
72
+
73
73
_tx_fifo = NULL ;
74
74
_tx_buffered = false ;
75
- _buffered_th = NULL ;
76
75
}
77
76
78
- /* *
79
- * Destructor
80
- */
77
+ // Destructor
81
78
BLEUart::~BLEUart ()
82
79
{
83
80
if ( _tx_fifo ) delete _tx_fifo;
@@ -99,42 +96,24 @@ void BLEUart::bleuart_rxd_cb(uint16_t conn_hdl, BLECharacteristic* chr, uint8_t*
99
96
if ( svc._rx_cb ) svc._rx_cb (conn_hdl);
100
97
}
101
98
102
- /* *
103
- * Timer callback periodically to send TX packet (if enabled).
104
- * @param timer
105
- */
106
- void bleuart_txd_buffered_hdlr (TimerHandle_t timer)
107
- {
108
- BLEUart* svc = (BLEUart*) pvTimerGetTimerID (timer);
109
-
110
- // skip if null (unlikely)
111
- if ( !svc->_tx_fifo ) return ;
112
-
113
- // flush tx data
114
- (void ) svc->flush_txd ();
115
- }
116
-
117
99
void BLEUart::bleuart_txd_cccd_cb (uint16_t conn_hdl, BLECharacteristic* chr, uint16_t value)
118
100
{
119
101
BLEUart& svc = (BLEUart&) chr->parentService ();
120
102
121
- if ( svc._buffered_th == NULL ) return ;
122
-
123
- // Enable TXD timer if configured
124
- if (value & BLE_GATT_HVX_NOTIFICATION)
125
- {
126
- xTimerStart (svc._buffered_th , 0 ); // if started --> timer got reset
127
- }else
128
- {
129
- xTimerStop (svc._buffered_th , 0 );
130
- }
103
+ if ( svc._notify_cb ) svc._notify_cb (conn_hdl, value & BLE_GATT_HVX_NOTIFICATION);
131
104
}
132
105
133
106
void BLEUart::setRxCallback ( rx_callback_t fp)
134
107
{
135
108
_rx_cb = fp;
136
109
}
137
110
111
+ void BLEUart::setNotifyCallback (notify_callback_t fp)
112
+ {
113
+ _notify_cb = fp;
114
+ _txd.setCccdWriteCallback ( fp ? BLEUart::bleuart_txd_cccd_cb : NULL );
115
+ }
116
+
138
117
/* *
139
118
* Enable packet buffered for TXD
140
119
* Note: packet is sent right away if it reach MTU bytes
@@ -146,19 +125,14 @@ void BLEUart::bufferTXD(bool enable)
146
125
147
126
if ( enable )
148
127
{
149
- // enable cccd callback to start timer when enabled
150
- _txd.setCccdWriteCallback (BLEUart::bleuart_txd_cccd_cb);
151
-
152
- // Create FIFO for TX
128
+ // Create FIFO for TXD
153
129
if ( _tx_fifo == NULL )
154
130
{
155
131
_tx_fifo = new Adafruit_FIFO (1 );
156
132
_tx_fifo->begin ( Bluefruit.getMaxMtu (BLE_GAP_ROLE_PERIPH) );
157
133
}
158
134
}else
159
135
{
160
- _txd.setCccdWriteCallback (NULL );
161
-
162
136
if ( _tx_fifo ) delete _tx_fifo;
163
137
}
164
138
}
@@ -204,30 +178,6 @@ bool BLEUart::notifyEnabled(uint16_t conn_hdl)
204
178
return _txd.notifyEnabled (conn_hdl);
205
179
}
206
180
207
- void BLEUart::svc_disconnect_hdl (uint16_t conn_hdl)
208
- {
209
- if (_buffered_th)
210
- {
211
- xTimerDelete (_buffered_th, 0 );
212
- _buffered_th = NULL ;
213
-
214
- if (_tx_fifo) _tx_fifo->clear ();
215
- }
216
- }
217
-
218
- void BLEUart::svc_connect_hdl (uint16_t conn_hdl)
219
- {
220
- if ( _tx_buffered )
221
- {
222
- // create TXD timer TODO take connInterval into account
223
- // ((5*ms2tick(Bluefruit.connInterval())) / 4) / 2
224
- _buffered_th = xTimerCreate (NULL , ms2tick (10 ), true , this , bleuart_txd_buffered_hdlr);
225
-
226
- // Start the timer
227
- xTimerStart (_buffered_th, 0 );
228
- }
229
- }
230
-
231
181
/* ------------------------------------------------------------------*/
232
182
/* STREAM API
233
183
*------------------------------------------------------------------*/
@@ -281,7 +231,7 @@ size_t BLEUart::write (const uint8_t *content, size_t len, uint16_t conn_hdl)
281
231
else
282
232
{
283
233
// TX fifo has enough data, send notify right away
284
- VERIFY ( flush_txd (conn_hdl), 0 );
234
+ VERIFY ( flushTXD (conn_hdl), 0 );
285
235
286
236
// still more data left, send them all
287
237
if ( written < len )
@@ -310,7 +260,7 @@ void BLEUart::flush (void)
310
260
_rx_fifo->clear ();
311
261
}
312
262
313
- bool BLEUart::flush_txd (uint16_t conn_hdl)
263
+ bool BLEUart::flushTXD (uint16_t conn_hdl)
314
264
{
315
265
// use default conn handle if not passed
316
266
if ( conn_hdl == BLE_CONN_HANDLE_INVALID ) conn_hdl = Bluefruit.connHandle ();
0 commit comments