Skip to content

Commit dfeec3a

Browse files
committed
pbio/drv/bluetooth_btstack: Dynamically test local version.
The EV3 is known to have different Bluetooth chips, so we need to determine the version at runtime before we send the initialization script.
1 parent f773eac commit dfeec3a

4 files changed

Lines changed: 41 additions & 0 deletions

File tree

lib/pbio/drv/bluetooth/bluetooth_btstack.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,30 @@ static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packe
223223
#endif // PBDRV_CONFIG_BLUETOOTH_BTSTACK_LE
224224

225225
switch (hci_event_packet_get_type(packet)) {
226+
case HCI_EVENT_COMMAND_COMPLETE: {
227+
const uint8_t *rp = hci_event_command_complete_get_return_parameters(packet);
228+
switch (hci_event_command_complete_get_command_opcode(packet)) {
229+
case HCI_OPCODE_HCI_READ_LOCAL_VERSION_INFORMATION: {
230+
uint16_t lmp_pal_subversion = pbio_get_uint16_le(&rp[7]);
231+
pbdrv_bluetooth_btstack_set_chipset(lmp_pal_subversion);
232+
233+
#if DEBUG
234+
// Show version in ev3dev format.
235+
uint16_t chip = (lmp_pal_subversion & 0x7C00) >> 10;
236+
uint16_t min_ver = (lmp_pal_subversion & 0x007F);
237+
uint16_t maj_ver = (lmp_pal_subversion & 0x0380) >> 7;
238+
if (lmp_pal_subversion & 0x8000) {
239+
maj_ver |= 0x0008;
240+
}
241+
DEBUG_PRINT("LMP %04x: TIInit_%d.%d.%d.bts\n", lmp_pal_subversion, chip, maj_ver, min_ver);
242+
#endif
243+
break;
244+
}
245+
default:
246+
break;
247+
}
248+
break;
249+
}
226250
#if PBDRV_CONFIG_BLUETOOTH_BTSTACK_LE
227251
case GATT_EVENT_SERVICE_QUERY_RESULT: {
228252
// Service discovery not used.

lib/pbio/drv/bluetooth/bluetooth_btstack.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@
1212

1313
void pbdrv_bluetooth_btstack_run_loop_trigger(void);
1414

15+
/**
16+
* Hook called when BTstack reads the local version information.
17+
*
18+
* This is called _after_ hci_set_chipset but _before_ the init script is sent
19+
* over the wire, so this can be used to dynamically select the init script.
20+
*/
21+
void pbdrv_bluetooth_btstack_set_chipset(uint16_t lmp_pal_subversion);
22+
1523
typedef struct {
1624
const hci_transport_t *(*transport_instance)(void);
1725
const void *(*transport_config)(void);

lib/pbio/drv/bluetooth/bluetooth_btstack_stm32_hal.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222

2323
#include <pbdrv/gpio.h>
2424

25+
void pbdrv_bluetooth_btstack_set_chipset(uint16_t lmp_pal_subversion) {
26+
// All platforms supported by this abstraction use a cc2560x with the same
27+
// init script, so nothing to do here.
28+
}
29+
2530
static int btstack_control_gpio_on(void) {
2631
const pbdrv_bluetooth_btstack_stm32_platform_data_t *pdata =
2732
&pbdrv_bluetooth_btstack_stm32_platform_data;

lib/pbio/test/drv/test_bluetooth_btstack.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,10 @@ static void handle_data_source(btstack_data_source_t *ds, btstack_data_source_c
770770
}
771771
}
772772

773+
void pbdrv_bluetooth_btstack_set_chipset(uint16_t lmp_pal_subversion) {
774+
}
775+
776+
773777
static pbio_error_t test_btstack_run_loop_contiki_poll(pbio_os_state_t *state, void *context) {
774778
static btstack_data_source_t data_source;
775779
static uint32_t callback_count;

0 commit comments

Comments
 (0)