1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
#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>
#ifndef VERSION
# define VERSION "unknown"
#endif
namespace {
class HttpServerDelegate : public http::Server::Delegate {
public:
HttpServerDelegate() = default;
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() == "/api/v1/status") {
return http::Response::content(
R"({ status: "OK" })",
*http::MimeType::create("application", "json"));
}
return http::Response::status(http::StatusCode::kNotFound);
}
};
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 http_delegate;
auto server =
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);
}
bool start_daemon(logger::Logger& logger, cfg::Config const& cfg,
std::unique_ptr<http::OpenPort> port, bool verbose) {
auto pid = fork();
if (pid == 0) {
// Child process
// Move out of current workind directory to not keep it alive
chdir("/");
// Create a new process group
setpgid(0, 0);
// Close stdin, stdout and stderr
close(0);
close(1);
close(2);
// Use syslog logger
auto logger2 = logger::syslog("bluetooth-jukebox", verbose);
_exit(run(*logger2, cfg, std::move(port)) ? 0 : 1);
} else if (pid == -1) {
// Error
logger.err(std::format("Unable to fork: {}", strerror(errno)));
return false;
} else {
// Parent process
logger.info(std::format("Forked into pid {}", pid));
return true;
}
}
} // namespace
int main(int argc, char** argv) {
auto args = Args::create();
auto opt_help = args->option('h', "help", "display this text and exit.");
auto opt_version = args->option('V', "version", "display version and exit.");
auto opt_config = args->option_argument(
'C', "config", "FILE", "load config from FILE instead of default.");
auto opt_verbose = args->option('v', "verbose", "be more verbose.");
auto opt_daemon = args->option('d', "daemon", "fork into background.");
if (!args->run(argc, argv)) {
args->print_error(std::cerr);
std::cerr << "Try 'bluetooth-jukebox --help' for more information.\n";
return 1;
}
if (opt_help->is_set()) {
std::cout << "Usage: bluetooth-jukebox [OPTION...]\n"
<< "\n";
args->print_help(std::cout);
return 0;
}
if (opt_version->is_set()) {
std::cout << "bluetooth-jukebox " << VERSION
<< " written by Joel Klinghed <the_jk@spawned.biz>.\n";
return 0;
}
bool verbose = opt_verbose->is_set();
bool daemon = opt_daemon->is_set();
std::unique_ptr<cfg::Config> cfg;
std::vector<std::string> errors;
if (opt_config->is_set()) {
cfg = cfg::load_one(opt_config->argument(), errors);
} else {
cfg = cfg::load_all("bluetooth-jukebox", errors);
}
auto logger = logger::stderr(verbose);
// Open the port here, in case we fork() later, to fail early.
auto port = http::open_port(cfg->get("http.bind").value_or("localhost:5555"),
*logger);
if (!port)
return 1;
if (daemon)
return start_daemon(*logger, *cfg, std::move(port), verbose) ? 0 : 1;
return run(*logger, *cfg, std::move(port)) ? 0 : 1;
}
|