From 918eaadde6a11dae611d314777a664f57a0f8e1c Mon Sep 17 00:00:00 2001 From: Joel Klinghed Date: Mon, 20 Oct 2025 09:09:03 +0200 Subject: main: Include devices in controller list --- src/main.cc | 57 ++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 44 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/main.cc b/src/main.cc index b98d06a..f04e545 100644 --- a/src/main.cc +++ b/src/main.cc @@ -89,19 +89,7 @@ class HttpServerDelegate : public http::Server::Delegate { auto* adapter = api_.adapter(); json_writer_->clear(); - json_writer_->start_object(); - json_writer_->key("name"); - json_writer_->value(adapter ? adapter->name() : "unknown"); - json_writer_->key("pairable"); - json_writer_->value(adapter ? adapter->pairable() : false); - json_writer_->key("discoverable"); - json_writer_->value(adapter ? adapter->discoverable() : false); - json_writer_->key("discoverable_timeout_seconds"); - json_writer_->value(adapter ? adapter->discoverable_timeout_seconds() - : 0); - json_writer_->key("pairing"); - json_writer_->value(adapter ? adapter->pairing() : false); - json_writer_->end_object(); + write_adapter(adapter); return http::Response::content(json_tmp_, *json_mimetype_); } @@ -155,6 +143,43 @@ class HttpServerDelegate : public http::Server::Delegate { return http::Response::content(json_tmp_, *json_mimetype_); } + void write_adapter(bt::Adapter const* adapter) { + json_writer_->start_object(); + json_writer_->key("name"); + json_writer_->value(adapter ? adapter->name() : "unknown"); + json_writer_->key("pairable"); + json_writer_->value(adapter ? adapter->pairable() : false); + json_writer_->key("discoverable"); + json_writer_->value(adapter ? adapter->discoverable() : false); + json_writer_->key("discoverable_timeout_seconds"); + json_writer_->value(adapter ? adapter->discoverable_timeout_seconds() : 0); + json_writer_->key("pairing"); + json_writer_->value(adapter ? adapter->pairing() : false); + + json_writer_->key("devices"); + json_writer_->start_array(); + + if (adapter) { + for (auto* device : adapter->devices()) { + write_device(*device); + } + } + + json_writer_->end_array(); + json_writer_->end_object(); + } + + void write_device(bt::Device const& device) { + json_writer_->start_object(); + json_writer_->key("name"); + json_writer_->value(device.name()); + json_writer_->key("paired"); + json_writer_->value(device.paired()); + json_writer_->key("connected"); + json_writer_->value(device.connected()); + json_writer_->end_object(); + } + Api& api_; Signaler& signaler_; std::unique_ptr json_writer_; @@ -203,10 +228,16 @@ class BluetoothManagerDelegate : public bt::Manager::Delegate, public Api { void added_device(bt::Device& device) override { logger_.info( std::format("New device: {} [{}]", device.name(), device.address())); + + if (adapter_) + signaler_.send(kSignalUpdateAdapter); } void removed_device(std::string const& address) override { logger_.info(std::format("Remove device: [{}]", address)); + + if (adapter_) + signaler_.send(kSignalUpdateAdapter); } void agent_request_pincode( -- cgit v1.2.3-70-g09d2