Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion bt-embedded/drivers/generic.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ static void read_local_features_cb(BteHci *hci,
initialization_done(bte_hci_get_client(hci));
}

static void on_bd_addr_done(BteHci *hci, const BteHciReadBdAddrReply *reply,
void *userdata)
{
STOP_ON_FAILURE(hci, reply);

/* hci_dev itself saves the result */
bte_hci_read_local_features(hci, read_local_features_cb, userdata);
}

static void on_buffer_size_done(BteHci *hci,
const BteHciReadBufferSizeReply *reply,
void *userdata)
Expand All @@ -47,7 +56,7 @@ static void on_buffer_size_done(BteHci *hci,
dev->sco_max_packets = reply->sco_max_packets;
dev->info_flags |= BTE_HCI_INFO_GOT_BUFFER_SIZE;

bte_hci_read_local_features(hci, read_local_features_cb, userdata);
bte_hci_read_bd_addr(hci, on_bd_addr_done, userdata);
}

static void on_reset_done(BteHci *hci, const BteHciReply *reply,
Expand Down
4 changes: 1 addition & 3 deletions bt-embedded/drivers/wii.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,10 @@ static void on_read_local_features(BteHci *hci,
static void on_bd_addr_done(BteHci *hci, const BteHciReadBdAddrReply *reply,
void *userdata)
{
BteHciDev *dev = userdata;

BTE_DEBUG("");
STOP_ON_FAILURE(hci, reply);

dev->address = reply->address;
/* hci_dev itself saves the result */
bte_hci_read_local_features(hci, on_read_local_features, userdata);
}

Expand Down
15 changes: 15 additions & 0 deletions bt-embedded/hci.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,16 @@ BteHciFeatures bte_hci_get_supported_features(BteHci *)
return _bte_hci_dev.supported_features;
}

bool bte_hci_get_bd_address(BteHci *hci, BteBdAddr *address)
{
if (_bte_hci_dev.info_flags & BTE_HCI_INFO_GOT_BD_ADDR) {
*address = _bte_hci_dev.address;
return true;
} else {
return false;
}
}

uint16_t bte_hci_get_acl_mtu(BteHci *hci)
{
return _bte_hci_dev.acl_mtu;
Expand Down Expand Up @@ -1840,6 +1850,11 @@ static void read_bd_addr_cb(
BteHciReadBdAddrReply reply;
reply.status = buffer->data[HCI_CMD_REPLY_POS_STATUS];
memcpy(&reply.address, data, sizeof(reply.address));
BteHciDev *dev = &_bte_hci_dev;
if (!(dev->info_flags & BTE_HCI_INFO_GOT_BD_ADDR)) {
dev->address = reply.address;
dev->info_flags |= BTE_HCI_INFO_GOT_BD_ADDR;
}
BteHciReadBdAddrCb callback = client_cb;
callback(hci, &reply, userdata);
}
Expand Down
13 changes: 13 additions & 0 deletions bt-embedded/hci.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,19 @@ typedef uint64_t BteHciFeatures;
*/
BteHciFeatures bte_hci_get_supported_features(BteHci *hci);

/**
* @brief Get the local host's BD address
*
* Return the host's bluetooth device address. If this function is called
* before the device has been initialized, it will return false and no address
* will be copied into \a address.
*
* @param hci The HCI handle
* @param address Pointer to where the address will be stored
* @return \c true if the address was read, \c false otherwise
*/
bool bte_hci_get_bd_address(BteHci *hci, BteBdAddr *address);

/**
* @brief Get the ACL MTU
*
Expand Down
1 change: 1 addition & 0 deletions bt-embedded/internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ typedef enum {
typedef enum {
BTE_HCI_INFO_GOT_FEATURES = 1 << 0,
BTE_HCI_INFO_GOT_BUFFER_SIZE = 1 << 1,
BTE_HCI_INFO_GOT_BD_ADDR = 1 << 2,
} BteHciInfo;

typedef struct bte_acl_t BteAcl;
Expand Down
17 changes: 5 additions & 12 deletions example/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,17 +117,6 @@ static void inquiry_cb(BteHci *hci, const BteHciInquiryReply *reply, void *)
query_next_name(hci);
}

static void read_bd_addr_cb(BteHci *hci, const BteHciReadBdAddrReply *reply,
void *userdata)
{
static int count = 0;
const uint8_t *b = reply->address.bytes;
printf("%s: (%d), %02x:%02x:%02x:%02x:%02x:%02x\n", __func__, count, b[0],
b[1], b[2], b[3], b[4], b[5]);
count++;
// bte_hci_read_bd_addr(hci, read_bd_addr_cb);
}

static void read_stored_link_key_cb(BteHci *hci,
const BteHciReadStoredLinkKeyReply *reply,
void *userdata)
Expand All @@ -148,10 +137,14 @@ static void initialized_cb(BteHci *hci, bool success, void *)
printf("ACL MTU=%d, max packets=%d\n",
bte_hci_get_acl_mtu(hci),
bte_hci_get_acl_max_packets(hci));
// bte_hci_read_bd_addr(hci, read_bd_addr_cb, NULL);
bte_hci_periodic_inquiry(hci, 4, 5, BTE_LAP_GIAC, 3, 0,
inquiry_status_cb, inquiry_cb, NULL);
//bte_hci_read_stored_link_key(hci, NULL, read_stored_link_key_cb, NULL);
BteBdAddr address;
bte_hci_get_bd_address(hci, &address);
const uint8_t *b = address.bytes;
printf("%s: %02x:%02x:%02x:%02x:%02x:%02x\n", __func__, b[0],
b[1], b[2], b[3], b[4], b[5]);
}

int main(int argc, char **argv)
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ add_executable(test_commands
test_cpp_api.cpp
test_data_matcher.cpp
test_events.cpp
test_getters.cpp
test_hci_dev.cpp
test_l2cap_config.cpp
test_l2cap_connect.cpp
Expand Down
4 changes: 4 additions & 0 deletions tests/bte_cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,10 @@ class Client {
bte_hci_on_initialized(m_hci, &Hci::Callbacks::initialized, this);
}

bool getBdAddr(BteBdAddr *address) const {
return bte_hci_get_bd_address(m_hci, address);
}

using DoneCb = std::function<void(const BteHciReply &)>;
void nop(const DoneCb &cb) {
bte_hci_nop(m_hci, wrap<TAG>(cb), this);
Expand Down
36 changes: 36 additions & 0 deletions tests/test_getters.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include "helpers.h"
#include "mock_backend.h"
#include "type_utils.h"

#include "bt-embedded/client.h"
#include "bt-embedded/bte.h"
#include "bt-embedded/hci.h"
#include "bt-embedded/internals.h"
#include <gtest/gtest.h>
#include <iostream>

TEST(Getters, BdAddress) {
MockBackend backend;
Bte::Client client;
auto &hci = client.hci();

BteBdAddr address;
bool ok = hci.getBdAddr(&address);
ASSERT_FALSE(ok);

/* Read the BD address manually */
hci.readBdAddr([&](const BteHciReadBdAddrReply &) {});
BteBdAddr expectedAddress = {1, 2, 3, 4, 5, 6};
Buffer reply = Buffer{
HCI_COMMAND_COMPLETE, 4 + 6,
1, // packets
0x9, 0x10,
0, // status
} + expectedAddress;
backend.sendEvent(reply);
bte_handle_events();

ok = hci.getBdAddr(&address);
ASSERT_TRUE(ok);
ASSERT_EQ(address, expectedAddress);
}
Loading