Skip to content

Commit

Permalink
Attempted bugfix at non-gatt subpaths for Bluez devices. (#321)
Browse files Browse the repository at this point in the history
* Attempted bugfix at non-gatt subpaths for Bluez devices.

* Fixed examples, updated changelog
  • Loading branch information
kdewald authored Jul 4, 2024
1 parent ad0c934 commit 0fd7881
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 10 deletions.
2 changes: 1 addition & 1 deletion docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ The format is based on `Keep a Changelog`_, and this project adheres to `Semanti

**Fixed**

-
- (SimpleBluez) Fixed improper handling of non `org.Bluez.Service1` objects within a `org.bluez.Device1` object. *(Thanks Kober Engineering!)*


[0.7.X]
Expand Down
2 changes: 1 addition & 1 deletion examples/simpleble/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ option(SIMPLEBLE_LOCAL "Use local SimpleBLE" ON)
if (SIMPLEBLE_LOCAL)
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../../simpleble ${CMAKE_BINARY_DIR}/simpleble)
else()
cmake_policy(SET CMP0144 OLD)
cmake_policy(SET CMP0144 OLD) # NOTE: This broke on older versions of CMake
find_package(simpleble CONFIG REQUIRED)
endif()

Expand Down
9 changes: 7 additions & 2 deletions examples/simpleble/cpp/connect/connect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,19 @@ int main() {

std::vector<SimpleBLE::Peripheral> peripherals;

adapter.set_callback_on_scan_found([&](SimpleBLE::Peripheral peripheral) { peripherals.push_back(peripheral); });
adapter.set_callback_on_scan_found([&](SimpleBLE::Peripheral peripheral) {
std::cout << "Found device: " << peripheral.identifier() << " [" << peripheral.address() << "]" << std::endl;
if (peripheral.is_connectable()) {
peripherals.push_back(peripheral);
}
});

adapter.set_callback_on_scan_start([]() { std::cout << "Scan started." << std::endl; });
adapter.set_callback_on_scan_stop([]() { std::cout << "Scan stopped." << std::endl; });
// Scan for 5 seconds and return.
adapter.scan_for(5000);

std::cout << "The following devices were found:" << std::endl;
std::cout << "The following connectable devices were found:" << std::endl;
for (size_t i = 0; i < peripherals.size(); i++) {
std::cout << "[" << i << "] " << peripherals[i].identifier() << " [" << peripherals[i].address() << "]"
<< std::endl;
Expand Down
4 changes: 3 additions & 1 deletion examples/simpleble/cpp/notify/notify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ int main() {

adapter.set_callback_on_scan_found([&](SimpleBLE::Peripheral peripheral) {
std::cout << "Found device: " << peripheral.identifier() << " [" << peripheral.address() << "]" << std::endl;
peripherals.push_back(peripheral);
if (peripheral.is_connectable()) {
peripherals.push_back(peripheral);
}
});

adapter.set_callback_on_scan_start([]() { std::cout << "Scan started." << std::endl; });
Expand Down
4 changes: 3 additions & 1 deletion examples/simpleble/cpp/read/read.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ int main() {

adapter.set_callback_on_scan_found([&](SimpleBLE::Peripheral peripheral) {
std::cout << "Found device: " << peripheral.identifier() << " [" << peripheral.address() << "]" << std::endl;
peripherals.push_back(peripheral);
if (peripheral.is_connectable()) {
peripherals.push_back(peripheral);
}
});

adapter.set_callback_on_scan_start([]() { std::cout << "Scan started." << std::endl; });
Expand Down
4 changes: 3 additions & 1 deletion examples/simpleble/cpp/write/write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ int main() {

adapter.set_callback_on_scan_found([&](SimpleBLE::Peripheral peripheral) {
std::cout << "Found device: " << peripheral.identifier() << " [" << peripheral.address() << "]" << std::endl;
peripherals.push_back(peripheral);
if (peripheral.is_connectable()) {
peripherals.push_back(peripheral);
}
});

adapter.set_callback_on_scan_start([]() { std::cout << "Scan started." << std::endl; });
Expand Down
16 changes: 13 additions & 3 deletions simplebluez/src/Device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include <simplebluez/Exceptions.h>
#include <simplebluez/Service.h>

#include <simpledbus/base/Path.h>

using namespace SimpleBluez;

Device::Device(std::shared_ptr<SimpleDBus::Connection> conn, const std::string& bus_name, const std::string& path)
Expand All @@ -10,8 +12,14 @@ Device::Device(std::shared_ptr<SimpleDBus::Connection> conn, const std::string&
Device::~Device() {}

std::shared_ptr<SimpleDBus::Proxy> Device::path_create(const std::string& path) {
auto child = std::make_shared<Service>(_conn, _bus_name, path);
return std::static_pointer_cast<SimpleDBus::Proxy>(child);
const std::string next_child = SimpleDBus::Path::next_child_strip(_path, path);

if (next_child.find("service") == 0) {
auto child = std::make_shared<Service>(_conn, _bus_name, path);
return std::static_pointer_cast<SimpleDBus::Proxy>(child);
} else {
return std::make_shared<Proxy>(_conn, _bus_name, path);
}
}

std::shared_ptr<SimpleDBus::Interface> Device::interfaces_create(const std::string& interface_name) {
Expand All @@ -33,7 +41,9 @@ std::shared_ptr<Battery1> Device::battery1() {
return std::dynamic_pointer_cast<Battery1>(interface_get("org.bluez.Battery1"));
}

std::vector<std::shared_ptr<Service>> Device::services() { return children_casted<Service>(); }
std::vector<std::shared_ptr<Service>> Device::services() {
return children_casted_with_prefix<Service>("service");
}

std::shared_ptr<Service> Device::get_service(const std::string& uuid) {
auto services_all = services();
Expand Down
14 changes: 14 additions & 0 deletions simpledbus/include/simpledbus/advanced/Proxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <simpledbus/advanced/Interface.h>
#include <simpledbus/external/kvn_safe_callback.hpp>
#include <simpledbus/base/Path.h>

#include <memory>
#include <mutex>
Expand Down Expand Up @@ -63,6 +64,19 @@ class Proxy {
return result;
}

template <typename T>
std::vector<std::shared_ptr<T>> children_casted_with_prefix(const std::string& prefix) {
std::vector<std::shared_ptr<T>> result;
std::scoped_lock lock(_child_access_mutex);
for (auto& [path, child] : _children) {
const std::string next_child = SimpleDBus::Path::next_child_strip(_path, path);
if (next_child.find(prefix) == 0) {
result.push_back(std::dynamic_pointer_cast<T>(child));
}
}
return result;
}

protected:
bool _valid;
std::string _path;
Expand Down
1 change: 1 addition & 0 deletions simpledbus/include/simpledbus/base/Path.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Path {
static bool is_parent(const std::string& base, const std::string& path);

static std::string next_child(const std::string& base, const std::string& path);
static std::string next_child_strip(const std::string& base, const std::string& path);
};

} // namespace SimpleDBus
5 changes: 5 additions & 0 deletions simpledbus/src/base/Path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,9 @@ std::string Path::next_child(const std::string& base, const std::string& path) {
return fetch_elements(path, count_elements(base) + 1);
}

std::string Path::next_child_strip(const std::string& base, const std::string& path) {
const std::string child = next_child(base, path);
return child.substr(base.length() + 1);
}

} // namespace SimpleDBus

0 comments on commit 0fd7881

Please sign in to comment.