Skip to content

Commit 9dc0c7c

Browse files
committed
hci: reject connections if no handler accepts them
1 parent 4b430dc commit 9dc0c7c

2 files changed

Lines changed: 51 additions & 8 deletions

File tree

bt-embedded/hci.c

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -392,13 +392,15 @@ static void conn_complete_event_cb(BteBuffer *buffer)
392392
void *userdata = pc->userdata;
393393
_bte_hci_dev_free_command(pc);
394394

395-
create_connection_cb(hci, &reply, userdata);
395+
if (create_connection_cb) {
396+
create_connection_cb(hci, &reply, userdata);
397+
}
396398
}
397399

398400
static void create_connection_status_cb(
399401
BteHci *hci, uint8_t status, BteHciPendingCommand *pc)
400402
{
401-
if (status != 0) goto error;
403+
if (status != 0 || !hci) goto error;
402404

403405
struct _bte_hci_tmpdata_create_connection_t *tmpdata =
404406
&hci->last_async_cmd_data.create_connection;
@@ -590,11 +592,13 @@ void bte_hci_reject_connection(BteHci *hci,
590592
create_connection_status_cb, status_cb, userdata);
591593
if (UNLIKELY(!b)) return;
592594

593-
/* In the status callback we read this and setup the event matcher */
594-
struct _bte_hci_tmpdata_create_connection_t *tmpdata =
595-
&hci->last_async_cmd_data.create_connection;
596-
memcpy(&tmpdata->address, address, sizeof(*address));
597-
tmpdata->client_cb = callback;
595+
if (hci) {
596+
/* In the status callback we read this and setup the event matcher */
597+
struct _bte_hci_tmpdata_create_connection_t *tmpdata =
598+
&hci->last_async_cmd_data.create_connection;
599+
memcpy(&tmpdata->address, address, sizeof(*address));
600+
tmpdata->client_cb = callback;
601+
}
598602

599603
uint8_t *data = b->data + HCI_CMD_HDR_LEN;
600604
memcpy(data, address, sizeof(*address));
@@ -617,7 +621,13 @@ static bool client_handle_connection_request(BteHci *hci, void *cb_data)
617621
static void connection_request_event_cb(BteBuffer *buffer)
618622
{
619623
uint8_t *data = buffer->data + HCI_CMD_EVENT_POS_DATA;
620-
_bte_hci_dev_foreach_hci_client(client_handle_connection_request, data);
624+
bool handled =
625+
_bte_hci_dev_foreach_hci_client(client_handle_connection_request, data);
626+
if (!handled) {
627+
const BteBdAddr *address = (void*)data;
628+
bte_hci_reject_connection(NULL, address, BTE_HCI_HOST_REJECTED_BD_ADDR,
629+
NULL, NULL, NULL);
630+
}
621631
}
622632

623633
void bte_hci_on_connection_request(BteHci *hci, BteHciConnectionRequestCb callback)

tests/test_events.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,39 @@ TEST(Events, testConnectionRequested)
135135
ASSERT_EQ(calls, expectedCalls);
136136
}
137137

138+
TEST(Events, testConnectionRequestedIgnored)
139+
{
140+
MockBackend backend;
141+
Bte::Client client0, client1;
142+
auto &hci0 = client0.hci();
143+
auto &hci1 = client1.hci();
144+
145+
hci0.onConnectionRequest([&](const BteBdAddr &address,
146+
const BteClassOfDevice &cod,
147+
uint8_t link_type) {
148+
return false;
149+
});
150+
hci1.onConnectionRequest([&](const BteBdAddr &address,
151+
const BteClassOfDevice &cod,
152+
uint8_t link_type) {
153+
return false;
154+
});
155+
156+
/* Emit the ConnectionRequest event */
157+
BteBdAddr address = {1, 2, 3, 4, 5, 6};
158+
BteClassOfDevice cod = {7, 8, 9};
159+
uint8_t link_type = 2;
160+
backend.sendEvent(
161+
Buffer{ HCI_CONNECTION_REQUEST, 6 + 3 + 1 } + address + cod +
162+
Buffer{link_type});
163+
bte_handle_events();
164+
165+
/* We should have sent a rejection */
166+
Buffer expectedCommand = Buffer{0xa, 0x4, 6 + 1} + address +
167+
Buffer{BTE_HCI_HOST_REJECTED_BD_ADDR};
168+
ASSERT_EQ(backend.lastCommand(), expectedCommand);
169+
}
170+
138171
TEST(Events, testLinkKeyRequested)
139172
{
140173
MockBackend backend;

0 commit comments

Comments
 (0)