summaryrefslogtreecommitdiff
path: root/src/main.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.cc')
-rw-r--r--src/main.cc57
1 files changed, 44 insertions, 13 deletions
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> 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(