address working; improved pkt dump

This commit is contained in:
Dan Halbert 2020-07-08 23:15:48 -04:00
parent a5ab2829eb
commit f03045b97e
10 changed files with 148 additions and 1862 deletions

View File

@ -179,10 +179,10 @@ char default_ble_name[] = { 'C', 'I', 'R', 'C', 'U', 'I', 'T', 'P', 'Y', 0, 0, 0
// common_hal_bleio_adapter_set_name(self, (char*) default_ble_name);
// }
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) {
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) {
self->hci_uart = uart;
self->rts_pin = rts;
self->cts_pin = cts;
self->rts_digitalinout = rts;
self->cts_digitalinout = cts;
self->enabled = false;
}
@ -194,16 +194,6 @@ void common_hal_bleio_adapter_set_enabled(bleio_adapter_obj_t *self, bool enable
return;
}
if (enabled) {
common_hal_digitalio_digitalinout_construct(&self->rts_digitalinout, self->rts_pin);
common_hal_digitalio_digitalinout_construct(&self->cts_digitalinout, self->cts_pin);
hci_init(self);
} else {
common_hal_digitalio_digitalinout_deinit(&self->rts_digitalinout);
common_hal_digitalio_digitalinout_deinit(&self->cts_digitalinout);
}
//FIX enable/disable HCI adapter, but don't reset it, since we don't know how.
self->enabled = enabled;
}
@ -213,13 +203,13 @@ bool common_hal_bleio_adapter_get_enabled(bleio_adapter_obj_t *self) {
}
bleio_address_obj_t *common_hal_bleio_adapter_get_address(bleio_adapter_obj_t *self) {
bt_addr_le_t addr;
hci_read_bd_addr(&addr.a);
bt_addr_t addr;
check_hci_error(hci_read_bd_addr(&addr));
bleio_address_obj_t *address = m_new_obj(bleio_address_obj_t);
address->base.type = &bleio_address_type;
common_hal_bleio_address_construct(address, addr.a.val, addr.type);
common_hal_bleio_address_construct(address, addr.val, BT_ADDR_LE_PUBLIC);
return address;
}

View File

@ -36,7 +36,6 @@
#include "shared-bindings/_bleio/ScanResults.h"
#include "shared-bindings/busio/UART.h"
#include "shared-bindings/digitalio/DigitalInOut.h"
#include "shared-bindings/microcontroller/Pin.h"
#ifndef BLEIO_TOTAL_CONNECTION_COUNT
#define BLEIO_TOTAL_CONNECTION_COUNT 5
@ -44,19 +43,17 @@
extern bleio_connection_internal_t bleio_connections[BLEIO_TOTAL_CONNECTION_COUNT];
typedef struct {
typedef struct _bleio_adapter_obj_t {
mp_obj_base_t base;
uint8_t* advertising_data;
uint8_t* scan_response_data;
uint8_t* current_advertising_data;
bleio_scanresults_obj_t* scan_results;
bleio_scanresults_obj_t *scan_results;
mp_obj_t name;
mp_obj_tuple_t *connection_objs;
busio_uart_obj_t* hci_uart;
const mcu_pin_obj_t* rts_pin;
const mcu_pin_obj_t* cts_pin;
digitalio_digitalinout_obj_t rts_digitalinout;
digitalio_digitalinout_obj_t cts_digitalinout;
digitalio_digitalinout_obj_t *rts_digitalinout;
digitalio_digitalinout_obj_t *cts_digitalinout;
bool enabled;
} bleio_adapter_obj_t;

View File

@ -356,7 +356,7 @@ void common_hal_bleio_connection_pair(bleio_connection_internal_t *self, bool bo
if (mp_hal_is_interrupted()) {
return;
}
check_sec_status(self->sec_status);
//FIX check_sec_status(self->sec_status);
}
mp_float_t common_hal_bleio_connection_get_connection_interval(bleio_connection_internal_t *self) {

View File

@ -38,25 +38,41 @@
#include "shared-bindings/_bleio/UUID.h"
#include "supervisor/shared/bluetooth.h"
#include "common-hal/_bleio/__init__.h"
void check_hci_error(hci_result_t result) {
switch (result) {
case HCI_OK:
return;
//FIX to check HCI error
// void check_nrf_error(uint32_t err_code) {
// if (err_code == NRF_SUCCESS) {
// return;
// }
// switch (err_code) {
// case NRF_ERROR_TIMEOUT:
// mp_raise_msg(&mp_type_TimeoutError, NULL);
// return;
// case BLE_ERROR_INVALID_CONN_HANDLE:
// mp_raise_bleio_ConnectionError(translate("Not connected"));
// return;
// default:
// mp_raise_bleio_BluetoothError(translate("Unknown soft device error: %04x"), err_code);
// break;
// }
// }
case HCI_NO_RESPONSE:
mp_raise_bleio_BluetoothError(translate("No HCI command response received"));
return;
case HCI_READ_TIMEOUT:
mp_raise_bleio_BluetoothError(translate("Timeout waiting for HCI response"));
return;
case HCI_WRITE_TIMEOUT:
mp_raise_bleio_BluetoothError(translate("Timeout waiting to write HCI request"));
return;
case HCI_READ_ERROR:
mp_raise_bleio_BluetoothError(translate("Error reading from HCI adapter"));
return;
case HCI_WRITE_ERROR:
mp_raise_bleio_BluetoothError(translate("Error writing to HCI adapter"));
return;
default:
// Should be an HCI status error, > 0.
if (result > 0) {
mp_raise_bleio_BluetoothError(translate("HCI status error: %02x"), result);
} else {
mp_raise_bleio_BluetoothError(translate("Unknown hci_result_t: %d"), result);
}
return;
}
}
// void check_gatt_status(uint16_t gatt_status) {
// if (gatt_status == BLE_GATT_STATUS_SUCCESS) {
@ -104,7 +120,6 @@ void bleio_reset() {
}
// The singleton _bleio.Adapter object, bound to _bleio.adapter
// It currently only has properties and no state
bleio_adapter_obj_t common_hal_bleio_adapter_obj = {
.base = {
.type = &bleio_adapter_type,

View File

@ -29,6 +29,8 @@
#include <stdbool.h>
#include "hci_api.h"
void bleio_reset(void);
typedef struct {
@ -49,7 +51,7 @@ typedef struct {
// These helpers raise the appropriate exceptions if the code doesn't equal success.
void check_nrf_error(uint32_t err_code);
void check_hci_error(hci_result_t result);
void check_gatt_status(uint16_t gatt_status);
void check_sec_status(uint8_t sec_status);

View File

@ -14,6 +14,8 @@
#include "hci_api.h"
#include "py/obj.h"
// Zephyr include files to define HCI communication values and structs.
#include "hci_include/hci.h"
#include "hci_include/hci_err.h"
@ -21,6 +23,8 @@
#include <string.h>
#include "supervisor/shared/tick.h"
#include "shared-bindings/_bleio/__init__.h"
#include "common-hal/_bleio/Adapter.h"
// HCI H4 protocol packet types: first byte in the packet.
#define H4_CMD 0x01
@ -31,13 +35,15 @@
//FIX replace
#define ATT_CID 0x0004
#define sizeof_field(TYPE, MEMBER) sizeof((((TYPE *)0)->MEMBER))
#define RX_BUFFER_SIZE (3 + 255)
#define ACL_PKT_BUFFER_SIZE (255)
#define CTS_TIMEOUT_MSECS (1000)
#define RESPONSE_TIMEOUT_MSECS (1000)
STATIC bleio_adapter_obj_t *adapter;
#define adapter (&common_hal_bleio_adapter_obj)
STATIC uint8_t rx_buffer[RX_BUFFER_SIZE];
STATIC size_t rx_idx;
@ -81,12 +87,55 @@ typedef struct __attribute__ ((packed)) {
} h4_hci_evt_hdr_t;
STATIC void dump_pkt(const char* prefix, uint8_t pkt_len, uint8_t pkt_data[]) {
STATIC void dump_cmd_pkt(bool tx, uint8_t pkt_len, uint8_t pkt_data[]) {
if (debug) {
mp_printf(&mp_plat_print, "%s", prefix);
for (uint8_t i = 0; i < pkt_len; i++) {
mp_printf(&mp_plat_print, "%02x", pkt_data[i]);
h4_hci_cmd_hdr_t *pkt = (h4_hci_cmd_hdr_t *) pkt_data;
mp_printf(&mp_plat_print,
"%s HCI COMMAND (%x) opcode: %04x, len: %d, data: ",
tx ? "TX->" : "RX<-",
pkt->pkt_type, pkt->opcode, pkt->param_len);
uint8_t i;
for (i = sizeof(h4_hci_cmd_hdr_t); i < pkt_len; i++) {
mp_printf(&mp_plat_print, "%02x ", pkt_data[i]);
}
if (i != pkt->param_len + sizeof(h4_hci_cmd_hdr_t)) {
mp_printf(&mp_plat_print, " LENGTH MISMATCH");
}
mp_printf(&mp_plat_print, "\n");
}
}
STATIC void dump_acl_pkt(bool tx, uint8_t pkt_len, uint8_t pkt_data[]) {
if (debug) {
h4_hci_acl_hdr_t *pkt = (h4_hci_acl_hdr_t *) pkt_data;
mp_printf(&mp_plat_print,
"%s HCI ACLDATA (%x) handle: %04x, total_data_len: %d, acl_data_len: %d, cid: %04x, data: ",
tx ? "TX->" : "RX<-",
pkt->pkt_type, pkt->handle, pkt->total_data_len, pkt->acl_data_len, pkt->cid);
uint8_t i;
for (i = sizeof(h4_hci_acl_hdr_t); i < pkt_len; i++) {
mp_printf(&mp_plat_print, "%02x ", pkt_data[i]);
}
if (i != pkt->acl_data_len + sizeof(h4_hci_acl_hdr_t)) {
mp_printf(&mp_plat_print, " LENGTH MISMATCH");
}
mp_printf(&mp_plat_print, "\n");
}
}
STATIC void dump_evt_pkt(bool tx, uint8_t pkt_len, uint8_t pkt_data[]) {
if (debug) {
h4_hci_evt_hdr_t *pkt = (h4_hci_evt_hdr_t *) pkt_data;
mp_printf(&mp_plat_print,
"%s HCI EVENT (%x) evt: %02x, param_len: %d, data: ",
tx ? "TX->" : "RX<-",
pkt->pkt_type, pkt->evt, pkt->param_len);
uint8_t i;
for (i = sizeof(h4_hci_evt_hdr_t); i < pkt_len; i++) {
mp_printf(&mp_plat_print, "%02x ", pkt_data[i]);
}
if (i != pkt->param_len + sizeof(h4_hci_evt_hdr_t)) {
mp_printf(&mp_plat_print, " LENGTH MISMATCH");
}
mp_printf(&mp_plat_print, "\n");
}
@ -184,9 +233,11 @@ STATIC void process_evt_pkt(size_t pkt_len, uint8_t pkt[])
cmd_response_received = true;
cmd_response_opcode = evt->cmd_complete.opcode;
cmd_response_status = evt->cc_status.status;
// All the bytes following status.
cmd_response_data = &evt_data[sizeof(struct cmd_complete_with_status)];
cmd_response_len = evt_hdr->param_len - sizeof(struct cmd_complete_with_status);
// All the bytes following cmd_complete, -including- the status byte, which is
// included in all the _bt_hci_rp_* structs.
cmd_response_data = &evt_data[sizeof_field(struct cmd_complete_with_status, cmd_complete)];
// Includes status byte.
cmd_response_len = evt_hdr->param_len - sizeof_field(struct cmd_complete_with_status, cmd_complete);
break;
}
@ -265,15 +316,14 @@ STATIC void process_evt_pkt(size_t pkt_len, uint8_t pkt[])
}
}
void hci_init(bleio_adapter_obj_t *adapter_in) {
adapter = adapter_in;
void hci_init(void) {
rx_idx = 0;
pending_pkt = 0;
}
hci_result_t hci_poll_for_incoming_pkt(void) {
// Assert RTS low to say we're ready to read data.
common_hal_digitalio_digitalinout_set_value(&adapter->rts_digitalinout, false);
common_hal_digitalio_digitalinout_set_value(adapter->rts_digitalinout, false);
int errcode = 0;
bool packet_is_complete = false;
@ -281,7 +331,7 @@ hci_result_t hci_poll_for_incoming_pkt(void) {
// Read bytes until we run out, or accumulate a complete packet.
while (common_hal_busio_uart_rx_characters_available(adapter->hci_uart)) {
common_hal_busio_uart_read(adapter->hci_uart, rx_buffer + rx_idx, 1, &errcode);
if (!errcode) {
if (errcode) {
return HCI_READ_ERROR;
}
rx_idx++;
@ -313,14 +363,15 @@ hci_result_t hci_poll_for_incoming_pkt(void) {
}
// Stop incoming data while processing packet.
common_hal_digitalio_digitalinout_set_value(&adapter->rts_digitalinout, true);
common_hal_digitalio_digitalinout_set_value(adapter->rts_digitalinout, true);
size_t pkt_len = rx_idx;
// Reset for next pack
rx_idx = 0;
switch (rx_buffer[0]) {
case H4_ACL:
if (debug) {
dump_pkt("HCI EVENT RX <- ", rx_idx, rx_buffer);
dump_acl_pkt(false, pkt_len, rx_buffer);
}
process_acl_data_pkt(pkt_len, rx_buffer);
@ -328,7 +379,7 @@ hci_result_t hci_poll_for_incoming_pkt(void) {
case H4_EVT:
if (debug) {
dump_pkt("HCI ACLDATA RX <- ", rx_idx, rx_buffer);
dump_evt_pkt(false, pkt_len, rx_buffer);
}
process_evt_pkt(pkt_len, rx_buffer);
@ -338,7 +389,7 @@ hci_result_t hci_poll_for_incoming_pkt(void) {
break;
}
common_hal_digitalio_digitalinout_set_value(&adapter->rts_digitalinout, true);
common_hal_digitalio_digitalinout_set_value(adapter->rts_digitalinout, true);
return HCI_OK;
}
@ -348,7 +399,8 @@ hci_result_t hci_poll_for_incoming_pkt(void) {
STATIC hci_result_t write_pkt(uint8_t *buffer, size_t len) {
// Wait for CTS to go low before writing to HCI adapter.
uint64_t start = supervisor_ticks_ms64();
while (common_hal_digitalio_digitalinout_get_value(&adapter->cts_digitalinout)) {
while (common_hal_digitalio_digitalinout_get_value(adapter->cts_digitalinout)) {
RUN_BACKGROUND_TASKS;
if (supervisor_ticks_ms64() - start > CTS_TIMEOUT_MSECS) {
return HCI_WRITE_TIMEOUT;
@ -377,7 +429,7 @@ STATIC hci_result_t send_command(uint16_t opcode, uint8_t params_len, void* para
memcpy(&tx_buffer[sizeof(h4_hci_cmd_hdr_t)], params, params_len);
if (debug) {
dump_pkt("HCI COMMAND TX -> ", sizeof(tx_buffer), tx_buffer);
dump_cmd_pkt(true, sizeof(tx_buffer), tx_buffer);
}
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
}
// data_len does not include cid.
const size_t cid_len = sizeof(((h4_hci_acl_hdr_t *)0)->cid);
const size_t cid_len = sizeof_field(h4_hci_acl_hdr_t, cid);
// buf_len is size of entire packet including header.
const size_t buf_len = sizeof(h4_hci_acl_hdr_t) + cid_len + data_len;
uint8_t tx_buffer[buf_len];
@ -441,7 +493,7 @@ STATIC int __attribute__((unused)) send_acl_pkt(uint16_t handle, uint8_t cid, vo
memcpy(&tx_buffer[sizeof(h4_hci_acl_hdr_t)], data, data_len);
if (debug) {
dump_pkt("HCI ACLDATA TX -> ", buf_len, tx_buffer);
dump_acl_pkt(true, buf_len, tx_buffer);
}
pending_pkt++;
@ -478,7 +530,7 @@ hci_result_t hci_read_bd_addr(bt_addr_t *addr) {
int result = send_command(BT_HCI_OP_READ_BD_ADDR, 0, NULL);
if (result == HCI_OK) {
struct bt_hci_rp_read_bd_addr *response = (struct bt_hci_rp_read_bd_addr *) cmd_response_data;
memcpy(addr->val, response->bdaddr.val, sizeof(bt_addr_t));
memcpy(addr->val, response->bdaddr.val, sizeof_field(bt_addr_t, val));
}
return result;

View File

@ -17,27 +17,27 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef MICROPY_INCLUDED_DEVICES_BLE_HCI_COMMON_HAL_BLEIO_HCI_H
#define MICROPY_INCLUDED_DEVICES_BLE_HCI_COMMON_HAL_BLEIO_HCI_H
#ifndef MICROPY_INCLUDED_DEVICES_BLE_HCI_COMMON_HAL_BLEIO_HCI_API_H
#define MICROPY_INCLUDED_DEVICES_BLE_HCI_COMMON_HAL_BLEIO_HCI_API_H
#include <stdint.h>
#include "common-hal/_bleio/hci_include/hci.h"
#include "common-hal/_bleio/Adapter.h"
// Incomplete forward declaration to get around mutually-dependent include files.
typedef struct _bleio_adapter_obj_t bleio_adapter_obj_t;
// An hci_result_t is one of the HCI_x values below,
// or is it > 0 and is an HCI command status value (see hci_include/hci_err.h)
typedef int hci_result_t;
#define HCI_OK (0)
#define HCI_NO_RESPONSE (-1)
#define HCI_ERR_RESPONSE (-2)
#define HCI_READ_TIMEOUT (-3)
#define HCI_WRITE_TIMEOUT (-4)
#define HCI_READ_ERROR (-5)
#define HCI_WRITE_ERROR (-6)
#define HCI_READ_TIMEOUT (-2)
#define HCI_WRITE_TIMEOUT (-3)
#define HCI_READ_ERROR (-4)
#define HCI_WRITE_ERROR (-5)
void hci_init(bleio_adapter_obj_t *adapter_in);
void hci_init(void);
hci_result_t hci_disconnect(uint16_t handle);
@ -66,4 +66,4 @@ hci_result_t hci_reset(void);
hci_result_t hci_set_evt_mask(uint64_t event_mask);
#endif // MICROPY_INCLUDED_DEVICES_BLE_HCI_COMMON_HAL_BLEIO_HCI_H
#endif // MICROPY_INCLUDED_DEVICES_BLE_HCI_COMMON_HAL_BLEIO_HCI_API_H

File diff suppressed because it is too large Load Diff

View File

@ -68,16 +68,16 @@
//| Use `_bleio.adapter` to access the sole instance available."""
//|
//| def hci_init(self, *, uart: busio.UART, cts: Pin, baudrate: int = 115200, buffer_size: int = 256):
//| def hci_uart_init(self, *, uart: busio.UART, rts: digitalio.DigitalInOut, cts: digitalio.DigitalInOut, baudrate: int = 115200, buffer_size: int = 256):
//| On boards that do not have native BLE, you can an use HCI co-processor.
//| Call `_bleio.adapter.hci_init()` passing it the uart and pins used to communicate
//| Call `_bleio.adapter.hci_uart_init()` passing it the uart and pins used to communicate
//| with the co-processor, such as an Adafruit AirLift.
//| The co-processor must have been reset and put into BLE mode beforehand
//| by the appropriate pin manipulation.
//| The `uart` object, and `rts` and `cs` pins are used to
//| The `uart`, `rts`, and `cts` objects are used to
//| communicate with the HCI co-processor in HCI mode.
//|
mp_obj_t bleio_adapter_hci_init(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
mp_obj_t bleio_adapter_hci_uart_init(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
#if CIRCUITPY_BLEIO_HCI
bleio_adapter_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
@ -95,18 +95,23 @@ mp_obj_t bleio_adapter_hci_init(mp_uint_t n_args, const mp_obj_t *pos_args, mp_m
if (!MP_OBJ_IS_TYPE(uart, &busio_uart_type)) {
mp_raise_ValueError(translate("Expected a UART"));
}
const mcu_pin_obj_t *rts = validate_obj_is_free_pin(args[ARG_rts].u_obj);
const mcu_pin_obj_t *cts = validate_obj_is_free_pin(args[ARG_cts].u_obj);
common_hal_bleio_adapter_hci_init(self, uart, rts, cts);
digitalio_digitalinout_obj_t *rts = args[ARG_rts].u_obj;
digitalio_digitalinout_obj_t *cts = args[ARG_cts].u_obj;
if (!MP_OBJ_IS_TYPE(rts, &digitalio_digitalinout_type) ||
!MP_OBJ_IS_TYPE(cts, &digitalio_digitalinout_type)) {
mp_raise_ValueError(translate("Expected a DigitalInOut"));
}
common_hal_bleio_adapter_hci_uart_init(self, uart, rts, cts);
common_hal_bleio_adapter_set_enabled(self, true);
return mp_const_none;
#else
mp_raise_RuntimeError(translate("hci_init not available"));
mp_raise_RuntimeError(translate("hci_uart_init not available"));
return mp_const_none;
#endif // CIRCUITPY_BLEIO_HCI
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(bleio_adapter_hci_init_obj, 1, bleio_adapter_hci_init);
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(bleio_adapter_hci_uart_init_obj, 1, bleio_adapter_hci_uart_init);
//|
//| enabled: Any = ...
@ -433,7 +438,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_adapter_erase_bonding_obj, bleio_adapter_
STATIC const mp_rom_map_elem_t bleio_adapter_locals_dict_table[] = {
#if CIRCUITPY_BLEIO_HCI
{ MP_ROM_QSTR(MP_QSTR_hci_init), MP_ROM_PTR(&bleio_adapter_hci_init_obj) },
{ MP_ROM_QSTR(MP_QSTR_hci_uart_init), MP_ROM_PTR(&bleio_adapter_hci_uart_init_obj) },
#endif
{ MP_ROM_QSTR(MP_QSTR_enabled), MP_ROM_PTR(&bleio_adapter_enabled_obj) },
{ MP_ROM_QSTR(MP_QSTR_address), MP_ROM_PTR(&bleio_adapter_address_obj) },

View File

@ -38,7 +38,7 @@
const mp_obj_type_t bleio_adapter_type;
#if CIRCUITPY_BLEIO_HCI
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);
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);
#endif // CIRCUITPY_BLEIO_HCI
bool common_hal_bleio_adapter_get_advertising(bleio_adapter_obj_t *self);