summaryrefslogtreecommitdiff
path: root/src/main.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.cc')
-rw-r--r--src/main.cc95
1 files changed, 93 insertions, 2 deletions
diff --git a/src/main.cc b/src/main.cc
index 54f3d0a..6883f30 100644
--- a/src/main.cc
+++ b/src/main.cc
@@ -1,15 +1,21 @@
#include "args.hh"
+#include "bt.hh"
#include "cfg.hh"
#include "config.h"
#include "http.hh"
#include "logger.hh"
#include "looper.hh"
+#include "signals.hh"
#include <cerrno>
+#include <cstdint>
#include <cstring>
#include <format>
+#include <functional>
#include <iostream>
#include <memory>
+#include <optional>
+#include <string>
#include <unistd.h>
#include <utility>
#include <vector>
@@ -40,12 +46,97 @@ class HttpServerDelegate : public http::Server::Delegate {
}
};
+class BluetoothManagerDelegate : public bt::Manager::Delegate {
+ public:
+ explicit BluetoothManagerDelegate(logger::Logger& logger) : logger_(logger) {}
+
+ void new_adapter(bt::Adapter* adapter) override {
+ if (adapter) {
+ logger_.info(std::format("New adapter: {} [{}]", adapter->name(),
+ adapter->address()));
+ } else {
+ logger_.info("No adapter");
+ }
+ }
+
+ void added_device(bt::Device& device) override {
+ logger_.info(
+ std::format("New device: {} [{}]", device.name(), device.address()));
+ }
+
+ void removed_device(std::string const& address) override {
+ logger_.info(std::format("Remove device: [{}]", address));
+ }
+
+ void agent_request_pincode(
+ bt::Device& device,
+ std::function<void(std::optional<std::string>)> callback) override {
+ logger_.info(std::format("Device request pincode: {}", device.name()));
+ callback(std::nullopt);
+ }
+
+ void agent_display_pincode(bt::Device& device,
+ std::string const& pincode) override {
+ logger_.info(std::format("Device pincode: {} {}", device.name(), pincode));
+ }
+
+ void agent_request_passkey(
+ bt::Device& device,
+ std::function<void(std::optional<uint32_t>)> callback) override {
+ logger_.info(std::format("Device request passkey: {}", device.name()));
+ callback(std::nullopt);
+ }
+
+ void agent_display_passkey(bt::Device& device, uint32_t passkey,
+ uint16_t /* entered */) override {
+ logger_.info(std::format("Device passkey: {} {}", device.name(), passkey));
+ }
+
+ void agent_request_confirmation(bt::Device& device, uint32_t passkey,
+ std::function<void(bool)> callback) override {
+ logger_.info(std::format("Device request confirmation: {} {}",
+ device.name(), passkey));
+ callback(false);
+ }
+
+ void agent_request_authorization(
+ bt::Device& device, std::function<void(bool)> callback) override {
+ logger_.info(
+ std::format("Device request authorization: {}", device.name()));
+ callback(false);
+ }
+
+ void agent_authorize_service(bt::Device& device, std::string const& uuid,
+ std::function<void(bool)> callback) override {
+ logger_.info(std::format("Device request authorize service: {} {}",
+ device.name(), uuid));
+ callback(false);
+ }
+
+ void agent_cancel() override {}
+
+ private:
+ logger::Logger& logger_;
+};
+
bool run(logger::Logger& logger, cfg::Config const& cfg,
std::unique_ptr<http::OpenPort> port) {
auto looper = looper::create();
- HttpServerDelegate delegate;
+ HttpServerDelegate http_delegate;
auto server =
- http::create_server(logger, cfg, *looper, std::move(port), delegate);
+ http::create_server(logger, cfg, *looper, std::move(port), http_delegate);
+ BluetoothManagerDelegate bt_delegate(logger);
+ auto manager = bt::create_manager(logger, cfg, *looper, bt_delegate);
+ auto sigint_handler = signals::Handler::create(
+ *looper, signals::Signal::INT, [&looper, &logger]() {
+ logger.info("Received SIGINT, quitting...");
+ looper->quit();
+ });
+ auto sigterm_handler = signals::Handler::create(
+ *looper, signals::Signal::TERM, [&looper, &logger]() {
+ logger.info("Received SIGTERM, quitting...");
+ looper->quit();
+ });
return looper->run(logger);
}