1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
|
/**************************************************************************/
/*!
@file BLEUart.cpp
@author hathach (tinyusb.org)
@section LICENSE
Software License Agreement (BSD License)
Copyright (c) 2018, Adafruit Industries (adafruit.com)
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holders nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**************************************************************************/
#include "bluefruit.h"
#include "utility/TimeoutTimer.h"
/* UART Serivce: 6E400001-B5A3-F393-E0A9-E50E24DCCA9E
* UART RXD : 6E400002-B5A3-F393-E0A9-E50E24DCCA9E
* UART TXD : 6E400003-B5A3-F393-E0A9-E50E24DCCA9E
*/
const uint8_t BLEUART_UUID_SERVICE[] =
{
0x9E, 0xCA, 0xDC, 0x24, 0x0E, 0xE5, 0xA9, 0xE0,
0x93, 0xF3, 0xA3, 0xB5, 0x01, 0x00, 0x40, 0x6E
};
const uint8_t BLEUART_UUID_CHR_RXD[] =
{
0x9E, 0xCA, 0xDC, 0x24, 0x0E, 0xE5, 0xA9, 0xE0,
0x93, 0xF3, 0xA3, 0xB5, 0x02, 0x00, 0x40, 0x6E
};
const uint8_t BLEUART_UUID_CHR_TXD[] =
{
0x9E, 0xCA, 0xDC, 0x24, 0x0E, 0xE5, 0xA9, 0xE0,
0x93, 0xF3, 0xA3, 0xB5, 0x03, 0x00, 0x40, 0x6E
};
/**
* Constructor
*/
BLEUart::BLEUart(uint16_t fifo_depth)
: BLEService(BLEUART_UUID_SERVICE), _txd(BLEUART_UUID_CHR_TXD), _rxd(BLEUART_UUID_CHR_RXD)
{
_rx_fifo = NULL;
_rx_cb = NULL;
_rx_fifo_depth = fifo_depth;
_tx_fifo = NULL;
_tx_buffered = 0;
_buffered_th = NULL;
}
/**
* Destructor
*/
BLEUart::~BLEUart()
{
if ( _tx_fifo ) delete _tx_fifo;
}
/**
* Callback when received new data
* @param chr
* @param data
* @param len
* @param offset
*/
void bleuart_rxd_cb(BLECharacteristic& chr, uint8_t* data, uint16_t len, uint16_t offset)
{
(void) offset;
BLEUart& svc = (BLEUart&) chr.parentService();
svc._rx_fifo->write(data, len);
#if CFG_DEBUG >= 2
LOG_LV2("BLEUART", "RX: ");
PRINT_BUFFER(data, len);
#endif
// invoke user callback
if ( svc._rx_cb ) svc._rx_cb();
}
/**
* Timer callback periodically to send TX packet (if enabled).
* @param timer
*/
void bleuart_txd_buffered_hdlr(TimerHandle_t timer)
{
BLEUart* svc = (BLEUart*) pvTimerGetTimerID(timer);
// skip if null (unlikely)
if ( !svc->_tx_fifo ) return;
// flush tx data
(void) svc->flush_tx_buffered();
}
void bleuart_txd_cccd_cb(BLECharacteristic& chr, uint16_t value)
{
BLEUart& svc = (BLEUart&) chr.parentService();
if ( svc._buffered_th == NULL) return;
// Enable TXD timer if configured
if (value & BLE_GATT_HVX_NOTIFICATION)
{
xTimerStart(svc._buffered_th, 0); // if started --> timer got reset
}else
{
xTimerStop(svc._buffered_th, 0);
}
}
void BLEUart::setRxCallback( rx_callback_t fp)
{
_rx_cb = fp;
}
/**
* Enable packet buffered for TXD
* Note: packet is sent right away if it reach MTU bytes
* @param enable true or false
*/
void BLEUart::bufferTXD(uint8_t enable)
{
_tx_buffered = enable;
if ( enable )
{
// enable cccd callback to start timer when enabled
_txd.setCccdWriteCallback(bleuart_txd_cccd_cb);
// Create FIFO for TX TODO Larger MTU Size
if ( _tx_fifo == NULL )
{
_tx_fifo = new Adafruit_FIFO(1);
_tx_fifo->begin( Bluefruit.Gap.getMaxMtuByConnCfg(CONN_CFG_PERIPHERAL) );
}
}else
{
_txd.setCccdWriteCallback(NULL);
if ( _tx_fifo ) delete _tx_fifo;
}
}
err_t BLEUart::begin(void)
{
_rx_fifo = new Adafruit_FIFO(1);
_rx_fifo->begin(_rx_fifo_depth);
// Invoke base class begin()
VERIFY_STATUS( BLEService::begin() );
uint16_t max_mtu = Bluefruit.Gap.getMaxMtuByConnCfg(CONN_CFG_PERIPHERAL);
// Add TXD Characteristic
_txd.setProperties(CHR_PROPS_NOTIFY);
// TODO enable encryption when bonding is enabled
_txd.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS);
_txd.setMaxLen( max_mtu );
_txd.setUserDescriptor("TXD");
VERIFY_STATUS( _txd.begin() );
// Add RXD Characteristic
_rxd.setProperties(CHR_PROPS_WRITE | CHR_PROPS_WRITE_WO_RESP);
_rxd.setWriteCallback(bleuart_rxd_cb);
// TODO enable encryption when bonding is enabled
_rxd.setPermission(SECMODE_NO_ACCESS, SECMODE_OPEN);
_rxd.setMaxLen( max_mtu );
_rxd.setUserDescriptor("RXD");
VERIFY_STATUS(_rxd.begin());
return ERROR_NONE;
}
bool BLEUart::notifyEnabled(void)
{
return _txd.notifyEnabled();
}
void BLEUart::_disconnect_cb(void)
{
if (_buffered_th)
{
xTimerDelete(_buffered_th, 0);
_buffered_th = NULL;
if (_tx_fifo) _tx_fifo->clear();
}
}
void BLEUart::_connect_cb (void)
{
if ( _tx_buffered )
{
// create TXD timer TODO take connInterval into account
// ((5*ms2tick(Bluefruit.connInterval())) / 4) / 2
_buffered_th = xTimerCreate(NULL, ms2tick(10), true, this, bleuart_txd_buffered_hdlr);
// Start the timer
xTimerStart(_buffered_th, 0);
}
}
/*------------------------------------------------------------------*/
/* STREAM API
*------------------------------------------------------------------*/
int BLEUart::read (void)
{
uint8_t ch;
return read(&ch, 1) ? (int) ch : EOF;
}
int BLEUart::read (uint8_t * buf, size_t size)
{
return _rx_fifo->read(buf, size);
}
size_t BLEUart::write (uint8_t b)
{
return write(&b, 1);
}
size_t BLEUart::write (const uint8_t *content, size_t len)
{
// notify right away if txd buffered is not enabled
if ( !(_tx_buffered && _tx_fifo) )
{
return _txd.notify(content, len) ? len : 0;
}else
{
// skip if not enabled
if ( !notifyEnabled() ) return 0;
uint16_t written = _tx_fifo->write(content, len);
// TODO multiple prph connections
// Not up to GATT MTU, notify will be sent later by TXD timer handler
if ( _tx_fifo->count() < (Bluefruit.Gap.getMTU( Bluefruit.connHandle() ) - 3) )
{
return len;
}
else
{
// TX fifo has enough data, send notify right away
VERIFY( flush_tx_buffered(), 0);
// still more data left, send them all
if ( written < len )
{
VERIFY( _txd.notify(content+written, len-written), written);
}
return len;
}
}
}
int BLEUart::available (void)
{
return _rx_fifo->count();
}
int BLEUart::peek (void)
{
uint8_t ch;
return _rx_fifo->peek(&ch) ? (int) ch : EOF;
}
void BLEUart::flush (void)
{
_rx_fifo->clear();
}
bool BLEUart::flush_tx_buffered(void)
{
uint16_t max_hvx = Bluefruit.Gap.getMTU( Bluefruit.connHandle() ) - 3;
uint8_t* ff_data = (uint8_t*) rtos_malloc( max_hvx );
if (!ff_data) return false;
uint16_t len = _tx_fifo->read(ff_data, max_hvx);
bool result = true;
if ( len )
{
result = _txd.notify(ff_data, len);
}
rtos_free(ff_data);
return result;
}
|