Skip to content

Commit

Permalink
Attempted bugfix at non-gatt subpaths for Bluez devices.
Browse files Browse the repository at this point in the history
  • Loading branch information
kdewald committed Jun 23, 2024
1 parent 608d225 commit 7e2fa6f
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 5 deletions.
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
6 changes: 5 additions & 1 deletion examples/simpleble/cpp/connect/connect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ int main() {
// 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++) {
if (!peripherals[i].is_connectable()) {
continue;
}

std::cout << "[" << i << "] " << peripherals[i].identifier() << " [" << peripherals[i].address() << "]"
<< 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 7e2fa6f

Please sign in to comment.