Skip to content

Commit

Permalink
Exposed device path during pairing. Exposed bonded property.
Browse files Browse the repository at this point in the history
  • Loading branch information
kdewald committed Nov 13, 2024
1 parent a7f1c50 commit 4bc5a19
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 46 deletions.
11 changes: 8 additions & 3 deletions examples/simplebluez/incoming/incoming.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ int main(int argc, char* argv[]) {
std::cout << "Cancel called" << std::endl;
});

agent->set_on_display_passkey([agent](uint32_t passkey, uint16_t entered) {
std::cout << "DisplayPasskey called with passkey: " << passkey << std::endl;
agent->set_on_display_passkey([agent](const std::string& device_path, uint32_t passkey, uint16_t entered) {
std::cout << "DisplayPasskey called with passkey: " << passkey << " for device: " << device_path << std::endl;
});


Expand Down Expand Up @@ -81,7 +81,7 @@ int main(int argc, char* argv[]) {

auto characteristic0 = service0->create_characteristic();
characteristic0->uuid("12345678-AAAA-5678-1234-567812345678");
characteristic0->flags({"read", "notify", "write"});
characteristic0->flags({"secure-read", "secure-write", "secure-notify"});
// NOTE: Setting an initial value is not required, as this value doesn't get sent
// to the central until it attempts to read or notify.

Expand Down Expand Up @@ -121,6 +121,11 @@ int main(int argc, char* argv[]) {
if (!advertisement->active()) {
adapter->register_advertisement(advertisement);
std::cout << "Advertising on " << adapter->identifier() << " [" << adapter->address() << "]" << std::endl;

auto paired_devices = adapter->device_paired_get();
for (auto& device : paired_devices) {
std::cout << "Paired device: " << device->name() << " [" << device->address() << "]" << std::endl;
}
}

// TODO: Handle connection events.
Expand Down
29 changes: 15 additions & 14 deletions examples/simplebluez/pair/pair.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,37 +35,38 @@ int main(int argc, char* argv[]) {
agent->set_capabilities(SimpleBluez::Agent::Capabilities::KeyboardDisplay);

// Configure all callback handlers for the agent, as part of this example.
agent->set_on_request_pin_code([&]() {
std::cout << "Agent::RequestPinCode" << std::endl;
agent->set_on_request_pin_code([&](const std::string& device_path) {
std::cout << "Agent::RequestPinCode for device: " << device_path << std::endl;
return "123456";
});

agent->set_on_display_pin_code([&](const std::string& pin_code) {
std::cout << "Agent::DisplayPinCode: " << pin_code << std::endl;
agent->set_on_display_pin_code([&](const std::string& device_path, const std::string& pin_code) {
std::cout << "Agent::DisplayPinCode for device: " << device_path << " with pin_code: " << pin_code << std::endl;
return true;
});

agent->set_on_request_passkey([&]() {
std::cout << "Agent::RequestPasskey" << std::endl;
agent->set_on_request_passkey([&](const std::string& device_path) {
std::cout << "Agent::RequestPasskey for device: " << device_path << std::endl;
return 123456;
});

agent->set_on_display_passkey([&](uint32_t passkey, uint16_t entered) {
std::cout << "Agent::DisplayPasskey: " << passkey << " (" << entered << " entered)" << std::endl;
agent->set_on_display_passkey([&](const std::string& device_path, uint32_t passkey, uint16_t entered) {
std::cout << "Agent::DisplayPasskey for device: " << device_path << " with passkey: " << passkey << " (" << entered << " entered)" << std::endl;
return true;
});

agent->set_on_request_confirmation([&](uint32_t passkey) {
std::cout << "Agent::RequestConfirmation: " << passkey << std::endl;
agent->set_on_request_confirmation([&](const std::string& device_path, uint32_t passkey) {
std::cout << "Agent::RequestConfirmation for device: " << device_path << " with passkey: " << passkey << std::endl;
return true;
});

agent->set_on_request_authorization([&]() {
std::cout << "Agent::RequestAuthorization" << std::endl;
agent->set_on_request_authorization([&](const std::string& device_path) {
std::cout << "Agent::RequestAuthorization for device: " << device_path << std::endl;
return true;
});

agent->set_on_authorize_service([&](const std::string& uuid) {
std::cout << "Agent::AuthorizeService: " << uuid << std::endl;
agent->set_on_authorize_service([&](const std::string& device_path, const std::string& uuid) {
std::cout << "Agent::AuthorizeService for device: " << device_path << " with uuid: " << uuid << std::endl;
return true;
});

Expand Down
14 changes: 7 additions & 7 deletions simplebluez/include/simplebluez/Agent.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,25 @@ class Agent : public SimpleDBus::Proxy {
void set_capabilities(Capabilities capabilities);

// ----- METHODS -----
void set_on_request_pin_code(std::function<std::string()> callback);
void set_on_request_pin_code(std::function<std::string(const std::string& device_path)> callback);
void clear_on_request_pin_code();

void set_on_display_pin_code(std::function<bool(const std::string&)> callback);
void set_on_display_pin_code(std::function<bool(const std::string& device_path, const std::string& pin_code)> callback);
void clear_on_display_pin_code();

void set_on_request_passkey(std::function<uint32_t()> callback);
void set_on_request_passkey(std::function<uint32_t(const std::string& device_path)> callback);
void clear_on_request_passkey();

void set_on_display_passkey(std::function<void(uint32_t, uint16_t)> callback);
void set_on_display_passkey(std::function<void(const std::string& device_path, uint32_t passkey, uint16_t entered)> callback);
void clear_on_display_passkey();

void set_on_request_confirmation(std::function<bool(uint32_t)> callback);
void set_on_request_confirmation(std::function<bool(const std::string& device_path, uint32_t passkey)> callback);
void clear_on_request_confirmation();

void set_on_request_authorization(std::function<bool()> callback);
void set_on_request_authorization(std::function<bool(const std::string& device_path)> callback);
void clear_on_request_authorization();

void set_on_authorize_service(std::function<bool(const std::string&)> callback);
void set_on_authorize_service(std::function<bool(const std::string& device_path, const std::string& uuid)> callback);
void clear_on_authorize_service();

void set_on_release(std::function<void()> callback);
Expand Down
1 change: 1 addition & 0 deletions simplebluez/include/simplebluez/Device.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Device : public SimpleDBus::Proxy {
std::map<std::string, ByteArray> service_data();

bool paired(bool refresh = true);
bool bonded(bool refresh = true);
bool connected(bool refresh = true);
bool services_resolved(bool refresh = true);

Expand Down
14 changes: 7 additions & 7 deletions simplebluez/include/simplebluez/interfaces/Agent1.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Agent1 : public SimpleDBus::Interface {
* @note: Invalid values will cause a rejection of the request
* be returned.
*/
kvn::safe_callback<std::string()> OnRequestPinCode;
kvn::safe_callback<std::string(const std::string& device_path)> OnRequestPinCode;

/**
* @brief This method gets called when the service daemon
Expand All @@ -44,7 +44,7 @@ class Agent1 : public SimpleDBus::Interface {
*
* @return false if the request should be rejected.
*/
kvn::safe_callback<bool(const std::string&)> OnDisplayPinCode;
kvn::safe_callback<bool(const std::string& device_path, const std::string& pin_code)> OnDisplayPinCode;

/**
* @brief This method gets called when the service daemon
Expand All @@ -55,23 +55,23 @@ class Agent1 : public SimpleDBus::Interface {
* @note: Invalid values will cause a rejection of the request
* be returned.
*/
kvn::safe_callback<int32_t()> OnRequestPasskey;
kvn::safe_callback<int32_t(const std::string& device_path)> OnRequestPasskey;

/**
* @brief This method gets called when the service daemon
* needs to display a passkey for an authentication.
* The entered parameter indicates the number of already
* typed keys on the remote side.
*/
kvn::safe_callback<void(uint32_t, uint16_t)> OnDisplayPasskey;
kvn::safe_callback<void(const std::string& device_path, uint32_t passkey, uint16_t entered)> OnDisplayPasskey;

/**
* @brief This method gets called when the service daemon
* needs to confirm a passkey for an authentication.
*
* @return false if the request should be rejected.
*/
kvn::safe_callback<bool(uint32_t)> OnRequestConfirmation;
kvn::safe_callback<bool(const std::string& device_path, uint32_t passkey)> OnRequestConfirmation;

/**
* @brief This method gets called to request the user to
Expand All @@ -82,15 +82,15 @@ class Agent1 : public SimpleDBus::Interface {
*
* @return false if the request should be rejected.
*/
kvn::safe_callback<bool()> OnRequestAuthorization;
kvn::safe_callback<bool(const std::string& device_path)> OnRequestAuthorization;

/**
* @brief This method gets called when the service daemon
* needs to authorize a connection/service request.
*
* @return false if the request should be rejected.
*/
kvn::safe_callback<bool(const std::string&)> OnAuthorizeService;
kvn::safe_callback<bool(const std::string& device_path, const std::string& uuid)> OnAuthorizeService;

/**
* @brief This method gets called when the service daemon
Expand Down
1 change: 1 addition & 0 deletions simplebluez/include/simplebluez/interfaces/Device1.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class Device1 : public SimpleDBus::Interface {
std::map<uint16_t, ByteArray> ManufacturerData(bool refresh = true);
std::map<std::string, ByteArray> ServiceData(bool refresh = true);
bool Paired(bool refresh = true);
bool Bonded(bool refresh = true);
bool Connected(bool refresh = true);
bool ServicesResolved(bool refresh = true);

Expand Down
16 changes: 9 additions & 7 deletions simplebluez/src/Agent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,41 +32,43 @@ std::string Agent::capabilities() const {

void Agent::set_capabilities(Agent::Capabilities capabilities) { _capabilities = capabilities; }

void Agent::set_on_request_pin_code(std::function<std::string()> callback) {
void Agent::set_on_request_pin_code(std::function<std::string(const std::string& device_path)> callback) {
agent1()->OnRequestPinCode.load(callback);
}

void Agent::clear_on_request_pin_code() { agent1()->OnRequestPinCode.unload(); }

void Agent::set_on_display_pin_code(std::function<bool(const std::string&)> callback) {
void Agent::set_on_display_pin_code(std::function<bool(const std::string& device_path, const std::string& pin_code)> callback) {
agent1()->OnDisplayPinCode.load(callback);
}

void Agent::clear_on_display_pin_code() { agent1()->OnDisplayPinCode.unload(); }

void Agent::set_on_request_passkey(std::function<uint32_t()> callback) { agent1()->OnRequestPasskey.load(callback); }
void Agent::set_on_request_passkey(std::function<uint32_t(const std::string& device_path)> callback) {
agent1()->OnRequestPasskey.load(callback);
}

void Agent::clear_on_request_passkey() { agent1()->OnRequestPasskey.unload(); }

void Agent::set_on_display_passkey(std::function<void(uint32_t, uint16_t)> callback) {
void Agent::set_on_display_passkey(std::function<void(const std::string& device_path, uint32_t passkey, uint16_t entered)> callback) {
agent1()->OnDisplayPasskey.load(callback);
}

void Agent::clear_on_display_passkey() { agent1()->OnDisplayPasskey.unload(); }

void Agent::set_on_request_confirmation(std::function<bool(uint32_t)> callback) {
void Agent::set_on_request_confirmation(std::function<bool(const std::string& device_path, uint32_t passkey)> callback) {
agent1()->OnRequestConfirmation.load(callback);
}

void Agent::clear_on_request_confirmation() { agent1()->OnRequestConfirmation.unload(); }

void Agent::set_on_request_authorization(std::function<bool()> callback) {
void Agent::set_on_request_authorization(std::function<bool(const std::string& device_path)> callback) {
agent1()->OnRequestAuthorization.load(callback);
}

void Agent::clear_on_request_authorization() { agent1()->OnRequestAuthorization.unload(); }

void Agent::set_on_authorize_service(std::function<bool(const std::string&)> callback) {
void Agent::set_on_authorize_service(std::function<bool(const std::string& device_path, const std::string& uuid)> callback) {
agent1()->OnAuthorizeService.load(callback);
}

Expand Down
2 changes: 2 additions & 0 deletions simplebluez/src/Device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ std::map<std::string, ByteArray> Device::service_data() { return device1()->Serv

bool Device::paired(bool refresh) { return device1()->Paired(refresh); }

bool Device::bonded(bool refresh) { return device1()->Bonded(refresh); }

bool Device::connected(bool refresh) { return device1()->Connected(refresh); }

bool Device::services_resolved(bool refresh) { return device1()->ServicesResolved(refresh); }
Expand Down
17 changes: 9 additions & 8 deletions simplebluez/src/interfaces/Agent1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ void Agent1::message_handle(SimpleDBus::Message& msg) {

} else if (msg.get_member() == "RequestPinCode") {
// std::cout << "Agent1::message_handle() RequestPinCode" << std::endl;
SimpleDBus::Holder arg_device = msg.extract();

std::string pin_code = "abc123";
if (OnRequestPinCode) {
pin_code = OnRequestPinCode();
pin_code = OnRequestPinCode(arg_device.get_string());
}

if (!pin_code.empty()) {
Expand All @@ -34,10 +35,10 @@ void Agent1::message_handle(SimpleDBus::Message& msg) {

} else if (msg.get_member() == "RequestPasskey") {
// std::cout << "Agent1::message_handle() RequestPasskey" << std::endl;

SimpleDBus::Holder arg_device = msg.extract();
int32_t passkey = 123456;
if (OnRequestPasskey) {
passkey = OnRequestPasskey();
passkey = OnRequestPasskey(arg_device.get_string());
}

if (passkey >= 0 && passkey <= 999999) {
Expand All @@ -56,7 +57,7 @@ void Agent1::message_handle(SimpleDBus::Message& msg) {

bool success = true;
if (OnDisplayPinCode) {
success = OnDisplayPinCode(arg_pin_code.get_string());
success = OnDisplayPinCode(arg_device.get_string(), arg_pin_code.get_string());
}

if (!success) {
Expand All @@ -73,7 +74,7 @@ void Agent1::message_handle(SimpleDBus::Message& msg) {
SimpleDBus::Holder arg_entered = msg.extract();

if (OnDisplayPasskey) {
OnDisplayPasskey(arg_passkey.get_uint32(), arg_entered.get_uint16());
OnDisplayPasskey(arg_device.get_string(), arg_passkey.get_uint32(), arg_entered.get_uint16());
}

} else if (msg.get_member() == "RequestConfirmation") {
Expand All @@ -84,7 +85,7 @@ void Agent1::message_handle(SimpleDBus::Message& msg) {

bool success = true;
if (OnRequestConfirmation) {
success = OnRequestConfirmation(arg_passkey.get_uint32());
success = OnRequestConfirmation(arg_device.get_string(), arg_passkey.get_uint32());
}

if (!success) {
Expand All @@ -98,7 +99,7 @@ void Agent1::message_handle(SimpleDBus::Message& msg) {

bool success = true;
if (OnRequestAuthorization) {
success = OnRequestAuthorization();
success = OnRequestAuthorization(arg_device.get_string());
}

if (!success) {
Expand All @@ -115,7 +116,7 @@ void Agent1::message_handle(SimpleDBus::Message& msg) {

bool success = true;
if (OnAuthorizeService) {
success = OnAuthorizeService(arg_uuid.get_string());
success = OnAuthorizeService(arg_device.get_string(), arg_uuid.get_string());
}

if (!success) {
Expand Down
9 changes: 9 additions & 0 deletions simplebluez/src/interfaces/Device1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@ bool Device1::Paired(bool refresh) {
return _properties["Paired"].get_boolean();
}

bool Device1::Bonded(bool refresh) {
if (refresh) {
property_refresh("Bonded");
}

std::scoped_lock lock(_property_update_mutex);
return _properties["Bonded"].get_boolean();
}

bool Device1::Connected(bool refresh) {
if (refresh) {
property_refresh("Connected");
Expand Down

0 comments on commit 4bc5a19

Please sign in to comment.