Skip to content

Commit f03045b

Browse files
committed
address working; improved pkt dump
1 parent a5ab282 commit f03045b

10 files changed

Lines changed: 148 additions & 1862 deletions

File tree

devices/ble_hci/common-hal/_bleio/Adapter.c

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,10 @@ char default_ble_name[] = { 'C', 'I', 'R', 'C', 'U', 'I', 'T', 'P', 'Y', 0, 0, 0
179179
// common_hal_bleio_adapter_set_name(self, (char*) default_ble_name);
180180
// }
181181

182-
void common_hal_bleio_adapter_hci_init(bleio_adapter_obj_t *self, busio_uart_obj_t *uart, const mcu_pin_obj_t *rts, const mcu_pin_obj_t *cts) {
182+
void common_hal_bleio_adapter_hci_uart_init(bleio_adapter_obj_t *self, busio_uart_obj_t *uart, digitalio_digitalinout_obj_t *rts, digitalio_digitalinout_obj_t *cts) {
183183
self->hci_uart = uart;
184-
self->rts_pin = rts;
185-
self->cts_pin = cts;
184+
self->rts_digitalinout = rts;
185+
self->cts_digitalinout = cts;
186186
self->enabled = false;
187187
}
188188

@@ -194,16 +194,6 @@ void common_hal_bleio_adapter_set_enabled(bleio_adapter_obj_t *self, bool enable
194194
return;
195195
}
196196

197-
if (enabled) {
198-
common_hal_digitalio_digitalinout_construct(&self->rts_digitalinout, self->rts_pin);
199-
common_hal_digitalio_digitalinout_construct(&self->cts_digitalinout, self->cts_pin);
200-
201-
hci_init(self);
202-
} else {
203-
common_hal_digitalio_digitalinout_deinit(&self->rts_digitalinout);
204-
common_hal_digitalio_digitalinout_deinit(&self->cts_digitalinout);
205-
}
206-
207197
//FIX enable/disable HCI adapter, but don't reset it, since we don't know how.
208198
self->enabled = enabled;
209199
}
@@ -213,13 +203,13 @@ bool common_hal_bleio_adapter_get_enabled(bleio_adapter_obj_t *self) {
213203
}
214204

215205
bleio_address_obj_t *common_hal_bleio_adapter_get_address(bleio_adapter_obj_t *self) {
216-
bt_addr_le_t addr;
217-
hci_read_bd_addr(&addr.a);
206+
bt_addr_t addr;
207+
check_hci_error(hci_read_bd_addr(&addr));
218208

219209
bleio_address_obj_t *address = m_new_obj(bleio_address_obj_t);
220210
address->base.type = &bleio_address_type;
221211

222-
common_hal_bleio_address_construct(address, addr.a.val, addr.type);
212+
common_hal_bleio_address_construct(address, addr.val, BT_ADDR_LE_PUBLIC);
223213
return address;
224214
}
225215

devices/ble_hci/common-hal/_bleio/Adapter.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,27 +36,24 @@
3636
#include "shared-bindings/_bleio/ScanResults.h"
3737
#include "shared-bindings/busio/UART.h"
3838
#include "shared-bindings/digitalio/DigitalInOut.h"
39-
#include "shared-bindings/microcontroller/Pin.h"
4039

4140
#ifndef BLEIO_TOTAL_CONNECTION_COUNT
4241
#define BLEIO_TOTAL_CONNECTION_COUNT 5
4342
#endif
4443

4544
extern bleio_connection_internal_t bleio_connections[BLEIO_TOTAL_CONNECTION_COUNT];
4645

47-
typedef struct {
46+
typedef struct _bleio_adapter_obj_t {
4847
mp_obj_base_t base;
4948
uint8_t* advertising_data;
5049
uint8_t* scan_response_data;
5150
uint8_t* current_advertising_data;
52-
bleio_scanresults_obj_t* scan_results;
51+
bleio_scanresults_obj_t *scan_results;
5352
mp_obj_t name;
5453
mp_obj_tuple_t *connection_objs;
5554
busio_uart_obj_t* hci_uart;
56-
const mcu_pin_obj_t* rts_pin;
57-
const mcu_pin_obj_t* cts_pin;
58-
digitalio_digitalinout_obj_t rts_digitalinout;
59-
digitalio_digitalinout_obj_t cts_digitalinout;
55+
digitalio_digitalinout_obj_t *rts_digitalinout;
56+
digitalio_digitalinout_obj_t *cts_digitalinout;
6057
bool enabled;
6158
} bleio_adapter_obj_t;
6259

devices/ble_hci/common-hal/_bleio/Connection.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ void common_hal_bleio_connection_pair(bleio_connection_internal_t *self, bool bo
356356
if (mp_hal_is_interrupted()) {
357357
return;
358358
}
359-
check_sec_status(self->sec_status);
359+
//FIX check_sec_status(self->sec_status);
360360
}
361361

362362
mp_float_t common_hal_bleio_connection_get_connection_interval(bleio_connection_internal_t *self) {

devices/ble_hci/common-hal/_bleio/__init__.c

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,25 +38,41 @@
3838
#include "shared-bindings/_bleio/UUID.h"
3939
#include "supervisor/shared/bluetooth.h"
4040

41-
#include "common-hal/_bleio/__init__.h"
42-
43-
//FIX to check HCI error
44-
// void check_nrf_error(uint32_t err_code) {
45-
// if (err_code == NRF_SUCCESS) {
46-
// return;
47-
// }
48-
// switch (err_code) {
49-
// case NRF_ERROR_TIMEOUT:
50-
// mp_raise_msg(&mp_type_TimeoutError, NULL);
51-
// return;
52-
// case BLE_ERROR_INVALID_CONN_HANDLE:
53-
// mp_raise_bleio_ConnectionError(translate("Not connected"));
54-
// return;
55-
// default:
56-
// mp_raise_bleio_BluetoothError(translate("Unknown soft device error: %04x"), err_code);
57-
// break;
58-
// }
59-
// }
41+
void check_hci_error(hci_result_t result) {
42+
switch (result) {
43+
case HCI_OK:
44+
return;
45+
46+
case HCI_NO_RESPONSE:
47+
mp_raise_bleio_BluetoothError(translate("No HCI command response received"));
48+
return;
49+
50+
case HCI_READ_TIMEOUT:
51+
mp_raise_bleio_BluetoothError(translate("Timeout waiting for HCI response"));
52+
return;
53+
54+
case HCI_WRITE_TIMEOUT:
55+
mp_raise_bleio_BluetoothError(translate("Timeout waiting to write HCI request"));
56+
return;
57+
58+
case HCI_READ_ERROR:
59+
mp_raise_bleio_BluetoothError(translate("Error reading from HCI adapter"));
60+
return;
61+
62+
case HCI_WRITE_ERROR:
63+
mp_raise_bleio_BluetoothError(translate("Error writing to HCI adapter"));
64+
return;
65+
66+
default:
67+
// Should be an HCI status error, > 0.
68+
if (result > 0) {
69+
mp_raise_bleio_BluetoothError(translate("HCI status error: %02x"), result);
70+
} else {
71+
mp_raise_bleio_BluetoothError(translate("Unknown hci_result_t: %d"), result);
72+
}
73+
return;
74+
}
75+
}
6076

6177
// void check_gatt_status(uint16_t gatt_status) {
6278
// if (gatt_status == BLE_GATT_STATUS_SUCCESS) {
@@ -104,7 +120,6 @@ void bleio_reset() {
104120
}
105121

106122
// The singleton _bleio.Adapter object, bound to _bleio.adapter
107-
// It currently only has properties and no state
108123
bleio_adapter_obj_t common_hal_bleio_adapter_obj = {
109124
.base = {
110125
.type = &bleio_adapter_type,

devices/ble_hci/common-hal/_bleio/__init__.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
#include <stdbool.h>
3131

32+
#include "hci_api.h"
33+
3234
void bleio_reset(void);
3335

3436
typedef struct {
@@ -49,7 +51,7 @@ typedef struct {
4951

5052

5153
// These helpers raise the appropriate exceptions if the code doesn't equal success.
52-
void check_nrf_error(uint32_t err_code);
54+
void check_hci_error(hci_result_t result);
5355
void check_gatt_status(uint16_t gatt_status);
5456
void check_sec_status(uint8_t sec_status);
5557

devices/ble_hci/common-hal/_bleio/hci_api.c

Lines changed: 73 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@
1414

1515
#include "hci_api.h"
1616

17+
#include "py/obj.h"
18+
1719
// Zephyr include files to define HCI communication values and structs.
1820
#include "hci_include/hci.h"
1921
#include "hci_include/hci_err.h"
2022

2123
#include <string.h>
2224

2325
#include "supervisor/shared/tick.h"
26+
#include "shared-bindings/_bleio/__init__.h"
27+
#include "common-hal/_bleio/Adapter.h"
2428

2529
// HCI H4 protocol packet types: first byte in the packet.
2630
#define H4_CMD 0x01
@@ -31,13 +35,15 @@
3135
//FIX replace
3236
#define ATT_CID 0x0004
3337

38+
#define sizeof_field(TYPE, MEMBER) sizeof((((TYPE *)0)->MEMBER))
39+
3440
#define RX_BUFFER_SIZE (3 + 255)
3541
#define ACL_PKT_BUFFER_SIZE (255)
3642

3743
#define CTS_TIMEOUT_MSECS (1000)
3844
#define RESPONSE_TIMEOUT_MSECS (1000)
3945

40-
STATIC bleio_adapter_obj_t *adapter;
46+
#define adapter (&common_hal_bleio_adapter_obj)
4147

4248
STATIC uint8_t rx_buffer[RX_BUFFER_SIZE];
4349
STATIC size_t rx_idx;
@@ -81,12 +87,55 @@ typedef struct __attribute__ ((packed)) {
8187
} h4_hci_evt_hdr_t;
8288

8389

90+
STATIC void dump_cmd_pkt(bool tx, uint8_t pkt_len, uint8_t pkt_data[]) {
91+
if (debug) {
92+
h4_hci_cmd_hdr_t *pkt = (h4_hci_cmd_hdr_t *) pkt_data;
93+
mp_printf(&mp_plat_print,
94+
"%s HCI COMMAND (%x) opcode: %04x, len: %d, data: ",
95+
tx ? "TX->" : "RX<-",
96+
pkt->pkt_type, pkt->opcode, pkt->param_len);
97+
uint8_t i;
98+
for (i = sizeof(h4_hci_cmd_hdr_t); i < pkt_len; i++) {
99+
mp_printf(&mp_plat_print, "%02x ", pkt_data[i]);
100+
}
101+
if (i != pkt->param_len + sizeof(h4_hci_cmd_hdr_t)) {
102+
mp_printf(&mp_plat_print, " LENGTH MISMATCH");
103+
}
104+
mp_printf(&mp_plat_print, "\n");
105+
}
106+
}
84107

85-
STATIC void dump_pkt(const char* prefix, uint8_t pkt_len, uint8_t pkt_data[]) {
108+
STATIC void dump_acl_pkt(bool tx, uint8_t pkt_len, uint8_t pkt_data[]) {
86109
if (debug) {
87-
mp_printf(&mp_plat_print, "%s", prefix);
88-
for (uint8_t i = 0; i < pkt_len; i++) {
89-
mp_printf(&mp_plat_print, "%02x", pkt_data[i]);
110+
h4_hci_acl_hdr_t *pkt = (h4_hci_acl_hdr_t *) pkt_data;
111+
mp_printf(&mp_plat_print,
112+
"%s HCI ACLDATA (%x) handle: %04x, total_data_len: %d, acl_data_len: %d, cid: %04x, data: ",
113+
tx ? "TX->" : "RX<-",
114+
pkt->pkt_type, pkt->handle, pkt->total_data_len, pkt->acl_data_len, pkt->cid);
115+
uint8_t i;
116+
for (i = sizeof(h4_hci_acl_hdr_t); i < pkt_len; i++) {
117+
mp_printf(&mp_plat_print, "%02x ", pkt_data[i]);
118+
}
119+
if (i != pkt->acl_data_len + sizeof(h4_hci_acl_hdr_t)) {
120+
mp_printf(&mp_plat_print, " LENGTH MISMATCH");
121+
}
122+
mp_printf(&mp_plat_print, "\n");
123+
}
124+
}
125+
126+
STATIC void dump_evt_pkt(bool tx, uint8_t pkt_len, uint8_t pkt_data[]) {
127+
if (debug) {
128+
h4_hci_evt_hdr_t *pkt = (h4_hci_evt_hdr_t *) pkt_data;
129+
mp_printf(&mp_plat_print,
130+
"%s HCI EVENT (%x) evt: %02x, param_len: %d, data: ",
131+
tx ? "TX->" : "RX<-",
132+
pkt->pkt_type, pkt->evt, pkt->param_len);
133+
uint8_t i;
134+
for (i = sizeof(h4_hci_evt_hdr_t); i < pkt_len; i++) {
135+
mp_printf(&mp_plat_print, "%02x ", pkt_data[i]);
136+
}
137+
if (i != pkt->param_len + sizeof(h4_hci_evt_hdr_t)) {
138+
mp_printf(&mp_plat_print, " LENGTH MISMATCH");
90139
}
91140
mp_printf(&mp_plat_print, "\n");
92141
}
@@ -184,9 +233,11 @@ STATIC void process_evt_pkt(size_t pkt_len, uint8_t pkt[])
184233
cmd_response_received = true;
185234
cmd_response_opcode = evt->cmd_complete.opcode;
186235
cmd_response_status = evt->cc_status.status;
187-
// All the bytes following status.
188-
cmd_response_data = &evt_data[sizeof(struct cmd_complete_with_status)];
189-
cmd_response_len = evt_hdr->param_len - sizeof(struct cmd_complete_with_status);
236+
// All the bytes following cmd_complete, -including- the status byte, which is
237+
// included in all the _bt_hci_rp_* structs.
238+
cmd_response_data = &evt_data[sizeof_field(struct cmd_complete_with_status, cmd_complete)];
239+
// Includes status byte.
240+
cmd_response_len = evt_hdr->param_len - sizeof_field(struct cmd_complete_with_status, cmd_complete);
190241

191242
break;
192243
}
@@ -265,23 +316,22 @@ STATIC void process_evt_pkt(size_t pkt_len, uint8_t pkt[])
265316
}
266317
}
267318

268-
void hci_init(bleio_adapter_obj_t *adapter_in) {
269-
adapter = adapter_in;
319+
void hci_init(void) {
270320
rx_idx = 0;
271321
pending_pkt = 0;
272322
}
273323

274324
hci_result_t hci_poll_for_incoming_pkt(void) {
275325
// Assert RTS low to say we're ready to read data.
276-
common_hal_digitalio_digitalinout_set_value(&adapter->rts_digitalinout, false);
326+
common_hal_digitalio_digitalinout_set_value(adapter->rts_digitalinout, false);
277327

278328
int errcode = 0;
279329
bool packet_is_complete = false;
280330

281331
// Read bytes until we run out, or accumulate a complete packet.
282332
while (common_hal_busio_uart_rx_characters_available(adapter->hci_uart)) {
283333
common_hal_busio_uart_read(adapter->hci_uart, rx_buffer + rx_idx, 1, &errcode);
284-
if (!errcode) {
334+
if (errcode) {
285335
return HCI_READ_ERROR;
286336
}
287337
rx_idx++;
@@ -313,22 +363,23 @@ hci_result_t hci_poll_for_incoming_pkt(void) {
313363
}
314364

315365
// Stop incoming data while processing packet.
316-
common_hal_digitalio_digitalinout_set_value(&adapter->rts_digitalinout, true);
366+
common_hal_digitalio_digitalinout_set_value(adapter->rts_digitalinout, true);
317367
size_t pkt_len = rx_idx;
368+
// Reset for next pack
318369
rx_idx = 0;
319370

320371
switch (rx_buffer[0]) {
321372
case H4_ACL:
322373
if (debug) {
323-
dump_pkt("HCI EVENT RX <- ", rx_idx, rx_buffer);
374+
dump_acl_pkt(false, pkt_len, rx_buffer);
324375
}
325376

326377
process_acl_data_pkt(pkt_len, rx_buffer);
327378
break;
328379

329380
case H4_EVT:
330381
if (debug) {
331-
dump_pkt("HCI ACLDATA RX <- ", rx_idx, rx_buffer);
382+
dump_evt_pkt(false, pkt_len, rx_buffer);
332383
}
333384

334385
process_evt_pkt(pkt_len, rx_buffer);
@@ -338,7 +389,7 @@ hci_result_t hci_poll_for_incoming_pkt(void) {
338389
break;
339390
}
340391

341-
common_hal_digitalio_digitalinout_set_value(&adapter->rts_digitalinout, true);
392+
common_hal_digitalio_digitalinout_set_value(adapter->rts_digitalinout, true);
342393

343394
return HCI_OK;
344395
}
@@ -348,7 +399,8 @@ hci_result_t hci_poll_for_incoming_pkt(void) {
348399
STATIC hci_result_t write_pkt(uint8_t *buffer, size_t len) {
349400
// Wait for CTS to go low before writing to HCI adapter.
350401
uint64_t start = supervisor_ticks_ms64();
351-
while (common_hal_digitalio_digitalinout_get_value(&adapter->cts_digitalinout)) {
402+
403+
while (common_hal_digitalio_digitalinout_get_value(adapter->cts_digitalinout)) {
352404
RUN_BACKGROUND_TASKS;
353405
if (supervisor_ticks_ms64() - start > CTS_TIMEOUT_MSECS) {
354406
return HCI_WRITE_TIMEOUT;
@@ -377,7 +429,7 @@ STATIC hci_result_t send_command(uint16_t opcode, uint8_t params_len, void* para
377429
memcpy(&tx_buffer[sizeof(h4_hci_cmd_hdr_t)], params, params_len);
378430

379431
if (debug) {
380-
dump_pkt("HCI COMMAND TX -> ", sizeof(tx_buffer), tx_buffer);
432+
dump_cmd_pkt(true, sizeof(tx_buffer), tx_buffer);
381433
}
382434

383435
int result = write_pkt(tx_buffer, sizeof(h4_hci_cmd_hdr_t) + params_len);
@@ -426,7 +478,7 @@ STATIC int __attribute__((unused)) send_acl_pkt(uint16_t handle, uint8_t cid, vo
426478
}
427479

428480
// data_len does not include cid.
429-
const size_t cid_len = sizeof(((h4_hci_acl_hdr_t *)0)->cid);
481+
const size_t cid_len = sizeof_field(h4_hci_acl_hdr_t, cid);
430482
// buf_len is size of entire packet including header.
431483
const size_t buf_len = sizeof(h4_hci_acl_hdr_t) + cid_len + data_len;
432484
uint8_t tx_buffer[buf_len];
@@ -441,7 +493,7 @@ STATIC int __attribute__((unused)) send_acl_pkt(uint16_t handle, uint8_t cid, vo
441493
memcpy(&tx_buffer[sizeof(h4_hci_acl_hdr_t)], data, data_len);
442494

443495
if (debug) {
444-
dump_pkt("HCI ACLDATA TX -> ", buf_len, tx_buffer);
496+
dump_acl_pkt(true, buf_len, tx_buffer);
445497
}
446498

447499
pending_pkt++;
@@ -478,7 +530,7 @@ hci_result_t hci_read_bd_addr(bt_addr_t *addr) {
478530
int result = send_command(BT_HCI_OP_READ_BD_ADDR, 0, NULL);
479531
if (result == HCI_OK) {
480532
struct bt_hci_rp_read_bd_addr *response = (struct bt_hci_rp_read_bd_addr *) cmd_response_data;
481-
memcpy(addr->val, response->bdaddr.val, sizeof(bt_addr_t));
533+
memcpy(addr->val, response->bdaddr.val, sizeof_field(bt_addr_t, val));
482534
}
483535

484536
return result;

0 commit comments

Comments
 (0)