Skip to content

Commit

Permalink
drv/bluetooth: use address type when connecting
Browse files Browse the repository at this point in the history
The LEGO Mario hub uses a random BLE address, so we need to store this
information from the advertising data and use it when connecting.

We can also drop the Mario attribute handle hack since Mario seems to
be working without it now (this may have been fixed earlier in a4a85dd
or it could be due to this change but wasn't tested until now).
  • Loading branch information
dlech committed Aug 27, 2021
1 parent 51bdd09 commit 98fa91c
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 20 deletions.
12 changes: 2 additions & 10 deletions lib/pbio/drv/bluetooth/bluetooth_stm32_bluenrg.c
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ static PT_THREAD(scan_and_connect_task(struct pt *pt, pbio_task_t *task)) {
}

// save the Bluetooth address for later
// addr_type = subevt->bdaddr_type;
context->bdaddr_type = subevt->bdaddr_type;
memcpy(context->bdaddr, subevt->bdaddr, 6);

break;
Expand Down Expand Up @@ -482,9 +482,7 @@ static PT_THREAD(scan_and_connect_task(struct pt *pt, pbio_task_t *task)) {
assert(!remote_handle);

PT_WAIT_WHILE(pt, write_xfer_size);
// REVISIT: might want to store address type from advertising data and pass
// it in here instead of assuming public address type
aci_gap_create_connection_begin(0x0060, 0x0030, PUBLIC_ADDR, context->bdaddr,
aci_gap_create_connection_begin(0x0060, 0x0030, context->bdaddr_type, context->bdaddr,
PUBLIC_ADDR, 0x0010 >> 1, 0x0030 >> 1, 4, 720 / 10, 0x0010, 0x0030);
PT_WAIT_UNTIL(pt, hci_command_status);
context->status = aci_gap_create_connection_end();
Expand Down Expand Up @@ -530,12 +528,6 @@ static PT_THREAD(scan_and_connect_task(struct pt *pt, pbio_task_t *task)) {
});
});

// HACK: Characteristics of LEGO Mario are not properly found by aci_gatt_disc_charac_by_uuid_begin().
// remote_lwp3_char_handle for mario is hard coded for now
if (context->hub_kind == LWP3_HUB_KIND_MARIO) {
remote_lwp3_char_handle = 0x0011;
}

// enable notifications

static const uint16_t enable = 0x0001;
Expand Down
12 changes: 2 additions & 10 deletions lib/pbio/drv/bluetooth/bluetooth_stm32_cc2640.c
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ static PT_THREAD(scan_and_connect_task(struct pt *pt, pbio_task_t *task)) {
}

// save the Bluetooth address for later
// addr_type = read_buf[10];
context->bdaddr_type = read_buf[10];
memcpy(context->bdaddr, &read_buf[11], 6);

break;
Expand Down Expand Up @@ -510,9 +510,7 @@ static PT_THREAD(scan_and_connect_task(struct pt *pt, pbio_task_t *task)) {
assert(remote_handle == NO_CONNECTION);

PT_WAIT_WHILE(pt, write_xfer_size);
// REVISIT: might want to store address type from advertising data and pass
// it in here instead of assuming public address type
GAP_EstablishLinkReq(0, 0, ADDRTYPE_PUBLIC, context->bdaddr);
GAP_EstablishLinkReq(0, 0, context->bdaddr_type, context->bdaddr);
PT_WAIT_UNTIL(pt, hci_command_status);

context->status = read_buf[8]; // debug
Expand Down Expand Up @@ -577,12 +575,6 @@ static PT_THREAD(scan_and_connect_task(struct pt *pt, pbio_task_t *task)) {
});
});

// HACK: Characteristics of LEGO Mario are not properly found by GATT_DiscCharsByUUID().
// remote_lwp3_char_handle for mario is hard coded for now
if (context->hub_kind == LWP3_HUB_KIND_MARIO) {
remote_lwp3_char_handle = 0x0012;
}

// enable notifications

retry:
Expand Down
1 change: 1 addition & 0 deletions lib/pbio/include/pbdrv/bluetooth.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ void pbdrv_bluetooth_set_notification_handler(pbdrv_bluetooth_receive_handler_t
typedef struct {
lwp3_hub_kind_t hub_kind;
uint8_t status;
uint8_t bdaddr_type;
uint8_t bdaddr[6];
char name[20];
} pbdrv_bluetooth_scan_and_connect_context_t;
Expand Down

0 comments on commit 98fa91c

Please sign in to comment.