From 4bc5a192d390a7a5b93bffec66c6e5c68dd2d6d7 Mon Sep 17 00:00:00 2001 From: Kevin Dewald Date: Tue, 12 Nov 2024 20:30:30 -0800 Subject: [PATCH] Exposed device path during pairing. Exposed bonded property. --- examples/simplebluez/incoming/incoming.cpp | 11 +++++-- examples/simplebluez/pair/pair.cpp | 29 ++++++++++--------- simplebluez/include/simplebluez/Agent.h | 14 ++++----- simplebluez/include/simplebluez/Device.h | 1 + .../include/simplebluez/interfaces/Agent1.h | 14 ++++----- .../include/simplebluez/interfaces/Device1.h | 1 + simplebluez/src/Agent.cpp | 16 +++++----- simplebluez/src/Device.cpp | 2 ++ simplebluez/src/interfaces/Agent1.cpp | 17 ++++++----- simplebluez/src/interfaces/Device1.cpp | 9 ++++++ 10 files changed, 68 insertions(+), 46 deletions(-) diff --git a/examples/simplebluez/incoming/incoming.cpp b/examples/simplebluez/incoming/incoming.cpp index 03afe2bd..a63bfa80 100644 --- a/examples/simplebluez/incoming/incoming.cpp +++ b/examples/simplebluez/incoming/incoming.cpp @@ -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; }); @@ -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. @@ -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. diff --git a/examples/simplebluez/pair/pair.cpp b/examples/simplebluez/pair/pair.cpp index 12efee1c..dcf167fa 100644 --- a/examples/simplebluez/pair/pair.cpp +++ b/examples/simplebluez/pair/pair.cpp @@ -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; }); diff --git a/simplebluez/include/simplebluez/Agent.h b/simplebluez/include/simplebluez/Agent.h index 64526605..33a9cb61 100644 --- a/simplebluez/include/simplebluez/Agent.h +++ b/simplebluez/include/simplebluez/Agent.h @@ -25,25 +25,25 @@ class Agent : public SimpleDBus::Proxy { void set_capabilities(Capabilities capabilities); // ----- METHODS ----- - void set_on_request_pin_code(std::function callback); + void set_on_request_pin_code(std::function callback); void clear_on_request_pin_code(); - void set_on_display_pin_code(std::function callback); + void set_on_display_pin_code(std::function callback); void clear_on_display_pin_code(); - void set_on_request_passkey(std::function callback); + void set_on_request_passkey(std::function callback); void clear_on_request_passkey(); - void set_on_display_passkey(std::function callback); + void set_on_display_passkey(std::function callback); void clear_on_display_passkey(); - void set_on_request_confirmation(std::function callback); + void set_on_request_confirmation(std::function callback); void clear_on_request_confirmation(); - void set_on_request_authorization(std::function callback); + void set_on_request_authorization(std::function callback); void clear_on_request_authorization(); - void set_on_authorize_service(std::function callback); + void set_on_authorize_service(std::function callback); void clear_on_authorize_service(); void set_on_release(std::function callback); diff --git a/simplebluez/include/simplebluez/Device.h b/simplebluez/include/simplebluez/Device.h index c31ccfe8..b1ccef97 100644 --- a/simplebluez/include/simplebluez/Device.h +++ b/simplebluez/include/simplebluez/Device.h @@ -33,6 +33,7 @@ class Device : public SimpleDBus::Proxy { std::map service_data(); bool paired(bool refresh = true); + bool bonded(bool refresh = true); bool connected(bool refresh = true); bool services_resolved(bool refresh = true); diff --git a/simplebluez/include/simplebluez/interfaces/Agent1.h b/simplebluez/include/simplebluez/interfaces/Agent1.h index 811e0a48..e30b1d5d 100644 --- a/simplebluez/include/simplebluez/interfaces/Agent1.h +++ b/simplebluez/include/simplebluez/interfaces/Agent1.h @@ -34,7 +34,7 @@ class Agent1 : public SimpleDBus::Interface { * @note: Invalid values will cause a rejection of the request * be returned. */ - kvn::safe_callback OnRequestPinCode; + kvn::safe_callback OnRequestPinCode; /** * @brief This method gets called when the service daemon @@ -44,7 +44,7 @@ class Agent1 : public SimpleDBus::Interface { * * @return false if the request should be rejected. */ - kvn::safe_callback OnDisplayPinCode; + kvn::safe_callback OnDisplayPinCode; /** * @brief This method gets called when the service daemon @@ -55,7 +55,7 @@ class Agent1 : public SimpleDBus::Interface { * @note: Invalid values will cause a rejection of the request * be returned. */ - kvn::safe_callback OnRequestPasskey; + kvn::safe_callback OnRequestPasskey; /** * @brief This method gets called when the service daemon @@ -63,7 +63,7 @@ class Agent1 : public SimpleDBus::Interface { * The entered parameter indicates the number of already * typed keys on the remote side. */ - kvn::safe_callback OnDisplayPasskey; + kvn::safe_callback OnDisplayPasskey; /** * @brief This method gets called when the service daemon @@ -71,7 +71,7 @@ class Agent1 : public SimpleDBus::Interface { * * @return false if the request should be rejected. */ - kvn::safe_callback OnRequestConfirmation; + kvn::safe_callback OnRequestConfirmation; /** * @brief This method gets called to request the user to @@ -82,7 +82,7 @@ class Agent1 : public SimpleDBus::Interface { * * @return false if the request should be rejected. */ - kvn::safe_callback OnRequestAuthorization; + kvn::safe_callback OnRequestAuthorization; /** * @brief This method gets called when the service daemon @@ -90,7 +90,7 @@ class Agent1 : public SimpleDBus::Interface { * * @return false if the request should be rejected. */ - kvn::safe_callback OnAuthorizeService; + kvn::safe_callback OnAuthorizeService; /** * @brief This method gets called when the service daemon diff --git a/simplebluez/include/simplebluez/interfaces/Device1.h b/simplebluez/include/simplebluez/interfaces/Device1.h index cbf60c97..daf4bc62 100644 --- a/simplebluez/include/simplebluez/interfaces/Device1.h +++ b/simplebluez/include/simplebluez/interfaces/Device1.h @@ -32,6 +32,7 @@ class Device1 : public SimpleDBus::Interface { std::map ManufacturerData(bool refresh = true); std::map ServiceData(bool refresh = true); bool Paired(bool refresh = true); + bool Bonded(bool refresh = true); bool Connected(bool refresh = true); bool ServicesResolved(bool refresh = true); diff --git a/simplebluez/src/Agent.cpp b/simplebluez/src/Agent.cpp index b18d4648..50a07a18 100644 --- a/simplebluez/src/Agent.cpp +++ b/simplebluez/src/Agent.cpp @@ -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 callback) { +void Agent::set_on_request_pin_code(std::function callback) { agent1()->OnRequestPinCode.load(callback); } void Agent::clear_on_request_pin_code() { agent1()->OnRequestPinCode.unload(); } -void Agent::set_on_display_pin_code(std::function callback) { +void Agent::set_on_display_pin_code(std::function callback) { agent1()->OnDisplayPinCode.load(callback); } void Agent::clear_on_display_pin_code() { agent1()->OnDisplayPinCode.unload(); } -void Agent::set_on_request_passkey(std::function callback) { agent1()->OnRequestPasskey.load(callback); } +void Agent::set_on_request_passkey(std::function callback) { + agent1()->OnRequestPasskey.load(callback); +} void Agent::clear_on_request_passkey() { agent1()->OnRequestPasskey.unload(); } -void Agent::set_on_display_passkey(std::function callback) { +void Agent::set_on_display_passkey(std::function callback) { agent1()->OnDisplayPasskey.load(callback); } void Agent::clear_on_display_passkey() { agent1()->OnDisplayPasskey.unload(); } -void Agent::set_on_request_confirmation(std::function callback) { +void Agent::set_on_request_confirmation(std::function callback) { agent1()->OnRequestConfirmation.load(callback); } void Agent::clear_on_request_confirmation() { agent1()->OnRequestConfirmation.unload(); } -void Agent::set_on_request_authorization(std::function callback) { +void Agent::set_on_request_authorization(std::function callback) { agent1()->OnRequestAuthorization.load(callback); } void Agent::clear_on_request_authorization() { agent1()->OnRequestAuthorization.unload(); } -void Agent::set_on_authorize_service(std::function callback) { +void Agent::set_on_authorize_service(std::function callback) { agent1()->OnAuthorizeService.load(callback); } diff --git a/simplebluez/src/Device.cpp b/simplebluez/src/Device.cpp index d9d56bde..5f348ee0 100644 --- a/simplebluez/src/Device.cpp +++ b/simplebluez/src/Device.cpp @@ -90,6 +90,8 @@ std::map 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); } diff --git a/simplebluez/src/interfaces/Agent1.cpp b/simplebluez/src/interfaces/Agent1.cpp index fb9774eb..0cc5ab3c 100644 --- a/simplebluez/src/interfaces/Agent1.cpp +++ b/simplebluez/src/interfaces/Agent1.cpp @@ -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()) { @@ -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) { @@ -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) { @@ -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") { @@ -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) { @@ -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) { @@ -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) { diff --git a/simplebluez/src/interfaces/Device1.cpp b/simplebluez/src/interfaces/Device1.cpp index 4e234006..751bc2f4 100644 --- a/simplebluez/src/interfaces/Device1.cpp +++ b/simplebluez/src/interfaces/Device1.cpp @@ -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");