summaryrefslogtreecommitdiff
path: root/src/main.cc
diff options
context:
space:
mode:
authorJoel Klinghed <the_jk@spawned.biz>2025-10-19 20:59:07 +0200
committerJoel Klinghed <the_jk@spawned.biz>2025-10-20 09:15:50 +0200
commit99527bdf3a12433936d2ab74c78284f59aaaaeed (patch)
tree026bb1b48d65c21910166329aacc594fd84ebf28 /src/main.cc
parentc0e24cc461cb60cde2791cd89d59a1ff1be0f6b2 (diff)
bt & main: Add more properties for adapter
Allow controller to be set in discoverable state.
Diffstat (limited to 'src/main.cc')
-rw-r--r--src/main.cc79
1 files changed, 69 insertions, 10 deletions
diff --git a/src/main.cc b/src/main.cc
index 494c49c..4555473 100644
--- a/src/main.cc
+++ b/src/main.cc
@@ -54,6 +54,14 @@ class Signaler {
Signaler() = default;
};
+std::optional<bool> match_bool(std::string_view str) {
+ if (str == "true")
+ return true;
+ if (str == "false")
+ return false;
+ return std::nullopt;
+}
+
class HttpServerDelegate : public http::Server::Delegate {
public:
HttpServerDelegate(Api& api, Signaler& signaler)
@@ -64,22 +72,21 @@ class HttpServerDelegate : public http::Server::Delegate {
std::unique_ptr<http::Response> handle(
http::Request const& request) override {
- if (request.method() != "GET") {
- return http::Response::status(http::StatusCode::kMethodNotAllowed);
- }
-
if (request.path().starts_with("/api/v1/")) {
auto path = request.path().substr(8);
if (path == "status") {
- json_writer_->clear();
- json_writer_->start_object();
- json_writer_->key("status");
- json_writer_->value("OK");
- json_writer_->end_object();
- return http::Response::content(json_tmp_, *json_mimetype_);
+ if (request.method() != "GET") {
+ return http::Response::status(http::StatusCode::kMethodNotAllowed);
+ }
+
+ return status_ok();
}
if (path == "controller") {
+ if (request.method() != "GET") {
+ return http::Response::status(http::StatusCode::kMethodNotAllowed);
+ }
+
auto* adapter = api_.adapter();
json_writer_->clear();
json_writer_->start_object();
@@ -87,6 +94,11 @@ class HttpServerDelegate : public http::Server::Delegate {
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();
@@ -94,7 +106,24 @@ class HttpServerDelegate : public http::Server::Delegate {
return http::Response::content(json_tmp_, *json_mimetype_);
}
+ if (path == "controller/discoverable") {
+ if (request.method() != "POST") {
+ return http::Response::status(http::StatusCode::kMethodNotAllowed);
+ }
+ std::optional<bool> value = match_bool(request.body());
+ auto* adapter = api_.adapter();
+ if (adapter && value.has_value()) {
+ adapter->set_discoverable(value.value());
+ return status_ok();
+ }
+ return status_error("Bad state");
+ }
+
if (path == "events") {
+ if (request.method() != "GET") {
+ return http::Response::status(http::StatusCode::kMethodNotAllowed);
+ }
+
auto resp = signaler_.handle(request);
if (resp)
return resp;
@@ -106,6 +135,26 @@ class HttpServerDelegate : public http::Server::Delegate {
}
private:
+ std::unique_ptr<http::Response> status_ok() {
+ json_writer_->clear();
+ json_writer_->start_object();
+ json_writer_->key("status");
+ json_writer_->value("OK");
+ json_writer_->end_object();
+ return http::Response::content(json_tmp_, *json_mimetype_);
+ }
+
+ std::unique_ptr<http::Response> status_error(std::string_view message) {
+ json_writer_->clear();
+ json_writer_->start_object();
+ json_writer_->key("status");
+ json_writer_->value("error");
+ json_writer_->key("message");
+ json_writer_->value(message);
+ json_writer_->end_object();
+ return http::Response::content(json_tmp_, *json_mimetype_);
+ }
+
Api& api_;
Signaler& signaler_;
std::unique_ptr<json::Writer> json_writer_;
@@ -131,6 +180,16 @@ class BluetoothManagerDelegate : public bt::Manager::Delegate, public Api {
if (adapter) {
logger_.info(std::format("New adapter: {} [{}]", adapter->name(),
adapter->address()));
+
+ // Assuming pairable doesn't have a timeout - make it so.
+ if (adapter_->pairable_timeout_seconds() > 0) {
+ adapter_->set_pairable_timeout_seconds(0);
+ }
+
+ // Assuming discoverable has one
+ if (adapter_->discoverable_timeout_seconds() == 0) {
+ adapter_->set_discoverable_timeout_seconds(180);
+ }
} else {
logger_.info("No adapter");
}