Newer
Older
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
/****************************************************************************
* drivers/wireless/bluetooth/btuart.c
* UART based Bluetooth driver
*
* Copyright (C) 2018 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Ported from the Intel/Zephyr arduino101_firmware_source-v1.tar package
* where the code was released with a compatible 3-clause BSD license:
*
* Copyright (c) 2016, Intel Corporation
* 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 holder 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 AND CONTRIBUTORS "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 OR CONTRIBUTORS 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.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stddef.h>
#include <assert.h>
#include <errno.h>
#include <nuttx/kmalloc.h>
#include <nuttx/wireless/bt_core.h>
#include <nuttx/wireless/bt_hci.h>
#include <nuttx/wireless/bt_driver.h>
#include <nuttx/wireless/bt_uart.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define H4_HEADER_SIZE 1
#define H4_CMD 0x01
#define H4_ACL 0x02
#define H4_SCO 0x03
#define H4_EVT 0x04
/****************************************************************************
* Private Types
****************************************************************************/
struct btuart_upperhalf_s
{
/* This structure must appear first in the structure so that this structure
* is cast compatible with struct bt_driver_s.
*/
struct bt_driver_s dev;
/* The cached lower half interface */
FAR const struct btuart_lowerhalf_s *lower;
};
/****************************************************************************
* Private Functions
****************************************************************************/
static ssize_t btuart_read(FAR struct btuart_upperhalf_s *upper,
FAR uint8_t *buffer, size_t buflen,
size_t minread)
{
FAR const struct btuart_lowerhalf_s *lower;
ssize_t nread;
ssize_t ntotal = 0;
DEBUGASSERT(upper != NULL && upper->lower != NULL);
lower = upper->lower;
DEBUGASSERT(lower->read != NULL);
while (buflen > 0)
{
nread = lower->read(lower, buffer, buflen);
if (nread == 0)
{
wlinfo("Got zero bytes from UART\n");
if (ntotal < minread)
{
continue;
}
break;
}
else if (nread < 0)
{
return nread;
}
wlinfo("read %ld remaining %lu\n",
(long)nread, (unsigned long)(buflen - nread));
buflen -= nread;
ntotal += nread;
buffer += nread;
}
return ntotal;
}
static FAR struct bt_buf_s *btuart_evt_recv(FAR struct btuart_upperhalf_s *upper,
FAR int *remaining)
{
FAR struct bt_buf_s *buf;
struct bt_hci_evt_hdr_s hdr;
ssize_t nread;
/* We can ignore the return value since we pass buflen == minread */
nread = btuart_read(upper, (FAR uint8_t *)&hdr,
sizeof(struct bt_hci_evt_hdr_s),
sizeof(struct bt_hci_evt_hdr_s));
if (nread < 0)
{
return NULL;
}
*remaining = hdr.len;
Gregory Nutt
committed
buf = bt_buf_alloc(BT_EVT, NULL, 0);
if (buf != NULL)
{
memcpy(bt_buf_extend(buf, sizeof(struct bt_hci_evt_hdr_s)), &hdr,
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
sizeof(struct bt_hci_evt_hdr_s));
}
else
{
wlerr("ERROR: No available event buffers!\n");
}
wlinfo("len %u\n", hdr.len);
return buf;
}
static FAR struct bt_buf_s *btuart_acl_recv(FAR struct btuart_upperhalf_s *upper,
FAR int *remaining)
{
FAR struct bt_buf_s *buf;
struct bt_hci_acl_hdr_s hdr;
ssize_t nread;
/* We can ignore the return value since we pass buflen == minread */
nread = btuart_read(upper, (FAR uint8_t *)&hdr,
sizeof(struct bt_hci_acl_hdr_s),
sizeof(struct bt_hci_acl_hdr_s));
if (nread < 0)
{
return NULL;
}
Gregory Nutt
committed
buf = bt_buf_alloc(BT_ACL_IN, NULL, 0);
if (buf)
{
memcpy(bt_buf_extend(buf, sizeof(struct bt_hci_acl_hdr_s)), &hdr,
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
sizeof(struct bt_hci_acl_hdr_s));
}
else
{
wlerr("ERROR: No available ACL buffers!\n");
}
*remaining = BT_LE162HOST(hdr.len);
wlinfo("len %u\n", *remaining);
return buf;
}
static void btuart_interrupt(FAR const struct btuart_lowerhalf_s *lower,
FAR void *arg)
{
FAR struct btuart_upperhalf_s *upper;
static FAR struct bt_buf_s *buf;
static int remaining;
ssize_t nread;
DEBUGASSERT(lower != NULL && lower->rxdrain != NULL && arg != NULL);
upper = (FAR struct btuart_upperhalf_s *)arg;
/* Beginning of a new packet */
while (remaining > 0)
{
uint8_t type;
/* Get packet type */
nread = btuart_read(upper, &type, 1, 0);
if (nread != sizeof(type))
{
wlwarn("WARNING: Unable to read H4 packet type\n");
continue;
}
switch (type)
{
case H4_EVT:
buf = btuart_evt_recv(upper, &remaining);
break;
case H4_ACL:
buf = btuart_acl_recv(upper, &remaining);
break;
default:
wlerr("ERROR: Unknown H4 type %u\n", type);
return;
}
if (buf != NULL && remaining > bt_buf_tailroom(buf))
{
wlerr("ERROR: Not enough space in buffer\n");
goto failed;
}
wlinfo("Need to get %u bytes\n", remaining);
}
if (buf == NULL)
{
nread = lower->rxdrain(lower);
wlwarn("WARNING: Discarded %ld bytes\n", (long)nread);
remaining -= nread;
}
nread = btuart_read(upper, bt_buf_tail(buf), remaining, 0);
buf->len += nread;
remaining -= nread;
wlinfo("Received %ld bytes\n", (long)nread);
if (remaining == 0)
{
wlinfo("Full packet received\n");
/* Pass buffer to the stack */
buf = NULL;
}
return;
failed:
remaining = 0;
buf = NULL;
return;
}
static int btuart_send(FAR const struct bt_driver_s *dev,
FAR struct bt_buf_s *buf)
{
FAR struct btuart_upperhalf_s *upper;
FAR const struct btuart_lowerhalf_s *lower;
FAR uint8_t *type;
ssize_t nwritten;
upper = (FAR struct btuart_upperhalf_s *)dev;
DEBUGASSERT(upper != NULL && upper->lower != NULL);
lower = upper->lower;
if (bt_buf_headroom(buf) < H4_HEADER_SIZE)
{
wlerr("Not enough headroom in buffer\n");
return -EINVAL;
}
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
switch (buf->type)
{
case BT_CMD:
*type = H4_CMD;
break;
case BT_ACL_OUT:
*type = H4_ACL;
break;
case BT_EVT:
*type = H4_EVT;
break;
default:
wlerr("Unknown buf type %u\n", buf->type);
return -EINVAL;
}
nwritten = lower->write(lower, buf->data, buf->len);
if (nwritten == buf->len)
{
return OK;
}
if (nwritten < 0)
{
return (int)nwritten;
}
return -EIO;
}
static int btuart_open(FAR const struct bt_driver_s *dev)
{
FAR struct btuart_upperhalf_s *upper;
FAR const struct btuart_lowerhalf_s *lower;
upper = (FAR struct btuart_upperhalf_s *)dev;
DEBUGASSERT(upper != NULL && upper->lower != NULL);
lower = upper->lower;
/* Disable Tx and Rx interrupts */
lower->txenable(lower, false);
lower->rxenable(lower, false);
/* Drain any cached Rx data */
(void)lower->rxdrain(lower);
/* Enable Rx interrupts */
lower->rxenable(lower, true);
return OK;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: btuart_register
*
* Description:
* Create the UART-based Bluetooth device and register it with the
* Bluetooth stack.
*
* Input Parameters:
* lower - an instance of the lower half driver interface
*
* Returned Value:
* Zero is returned on success; a negated errno value is returned on any
* failure.
*
****************************************************************************/
int btuart_register(FAR const struct btuart_lowerhalf_s *lower)
{
FAR struct btuart_upperhalf_s *upper;
int ret;
DEBUGASSERT(lower != NULL);
/* Allocate a new instance of the upper half driver state structure */
upper = (FAR struct btuart_upperhalf_s *)
kmm_zalloc(sizeof(struct btuart_upperhalf_s));
if (upper == NULL)
{
wlerr("ERROR: Failed to allocate upper-half state\n");
return -ENOMEM;
}
/* Initialize the upper half driver state */
upper->dev.head_reserve = H4_HEADER_SIZE;
upper->dev.open = btuart_open;
upper->dev.send = btuart_send;
upper->lower = lower;
/* Attach the interrupt handler */
lower->attach(lower, btuart_interrupt, upper);
/* And register the driver with the Bluetooth stack */
ret = bt_driver_register(&upper->dev);
if (ret < 0)
{
kmm_free(upper);
}
return ret;
}