-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbus-bt-listener.pl
executable file
·490 lines (384 loc) · 12.3 KB
/
dbus-bt-listener.pl
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
#!/usr/bin/env perl
use strict;
use warnings;
use Net::DBus qw(:typing);
use Net::DBus::Reactor;
use RRDs;
################################################################################
# configuration
use constant {
LOG_DATA_INTERVAL => 30,
RRD_PATH => '~/rrd/',
};
################################################################################
# logging
# unbuffer stdout
$|++;
sub log_with_timestamp {
my ($sec,$min,$hour,$mday,$mon,$year) = localtime(time);
printf "%04d-%02d-%02d %02d:%02d:%02d | %s\n",
$year+1900, $mon, $mday, $hour, $min, $sec,
join(' ', @_);
}
sub log_info {
# comment this to disable log level INFO (normal output)
log_with_timestamp @_;
}
sub log_other {
# comment this to disable log level OTHER (ignored BT devices)
# log_with_timestamp @_;
}
sub log_debug {
# comment this to disable log level DEBUG (debug output)
# log_with_timestamp @_;
}
sub log_dumper {
# comment this to disable log level DUMP (use Data::Dumper on all arguments)
# use Data::Dumper;
# log_with_timestamp Dumper(@_);
}
################################################################################
# DBus helpers
use constant DBUS_PROPERTIES_IF => 'org.freedesktop.DBus.Properties';
my $bus = Net::DBus->system;
my $service = $bus->get_service("org.bluez");
sub register_signal {
my ($dbus_object, $signal_name, $coderef) = @_;
my $signal_id = $dbus_object->connect_to_signal($signal_name, $coderef);
log_info " registered signal $signal_name#$signal_id";
return sub {
log_info " trying to unregister signal $signal_name#$signal_id";
$dbus_object->disconnect_from_signal($signal_name, $signal_id);
log_info " unregistered signal $signal_name#$signal_id";
}
}
sub unregister_signals {
$_->() foreach reverse @_;
}
sub dbus_get_properties_if {
my ($path) = @_;
my $object = $service->get_object($path);
return $object->as_interface(DBUS_PROPERTIES_IF);
}
################################################################################
# store data in RRDs
use constant RRD_DSS_CONFIG => [
# this is no hashref, as order is important for RRDs::update!
{
NAME => 'temp_c',
# LYWSD03MMC can do -9.9°C to 60°C, but give some leeway for other sensors
MIN => -5000,
MAX => 10000,
},
{
NAME => 'hum_pc',
MIN => 0,
MAX => 100,
},
{
NAME => 'batt_mv',
MIN => 0,
MAX => 5000,
},
{
NAME => 'batt_pc',
MIN => 0,
MAX => 200, # battery level goes > 100% sometimes O_o
},
{
NAME => 'rssi',
MIN => -256,
MAX => 0,
},
];
use constant {
RRD_HEARTBEAT => 300,
RRD_XFF => 0.5,
RRD_CFS => ['AVERAGE', 'MIN', 'MAX'],
RRD_STEPS_ROWS => ['1m:1d', '15m:2w', '1h:13M', '1d:20y'],
};
sub create_rrd {
my ($rrd_file) = @_;
my @dss = ();
foreach my $dss (@{RRD_DSS_CONFIG()}) {
push @dss,
sprintf "DS:%s:GAUGE:%d:%d:%d",
$dss->{NAME}, RRD_HEARTBEAT, $dss->{MIN}, $dss->{MAX};
}
my @rras = ();
foreach my $cf (@{RRD_CFS()}) {
foreach my $steps_rows (@{RRD_STEPS_ROWS()}) {
push @rras,
sprintf "RRA:%s:%s:%s",
$cf, RRD_XFF, $steps_rows;
}
}
my $step = 60;
my @cmdline = ($rrd_file, '-s', $step, @dss, @rras);
log_debug "RRDs::create(", @cmdline, ")";
RRDs::create(@cmdline);
log_info "created missing RRD file $rrd_file";
}
# RSSID and sensor data change at different times
# RSSID might be outdated, but this should not matter too much
# I don't want to handle two different RRDs because of this
# TODO: change this? keep this?
sub store_in_rrd {
my ($sensor) = @_;
my $rrd_file = sprintf "%s/bt-sensors-%s.rrd", RRD_PATH, $sensor->{NAME};
$rrd_file =~ s://+:/:g;
$rrd_file =~ s:^~:$ENV{HOME}:e;
create_rrd $rrd_file unless -e $rrd_file;
my @values = map { $_ // 'U' } (
$sensor->{TEMPERATURE_CELSIUS},
$sensor->{HUMIDITY_PERCENT},
$sensor->{BATTERY_MILLIVOLT},
$sensor->{BATTERY_PERCENT},
$sensor->{RSSI_DBM}
);
my @cmdline = ($rrd_file, join(':', ('N', @values)));
log_debug "write to rrd", $rrd_file, @cmdline;
RRDs::update(@cmdline);
}
################################################################################
# output sensor data
# FIXME: stupid name
my %known_sensors;
sub format_sensor_data {
my ($sensor_data) = @_;
my @values;
my $tc = $sensor_data->{TEMPERATURE_CELSIUS};
push @values, sprintf("T:%5.2f°C", $tc) if defined $tc;
my $hp = $sensor_data->{HUMIDITY_PERCENT};
push @values, sprintf("H:%5.2f%%", $hp) if defined $hp;
my $bm = $sensor_data->{BATTERY_MILLIVOLT};
push @values, sprintf("B:%4dmV", $bm) if defined $bm;
my $bp = $sensor_data->{BATTERY_PERCENT};
push @values, sprintf("B:%3d%%", $bp) if defined $bp;
my $rd = $sensor_data->{RSSI_DBM};
push @values, sprintf("S:%+2ddB/m", $rd) if defined $rd;
return join ' ', @values;
}
my $print_count = 0;
sub show_sensor_data {
if (++$print_count > LOG_DATA_INTERVAL) {
foreach my $sensor (map { $known_sensors{$_} } sort keys %known_sensors) {
log_info $sensor->{NAME}, format_sensor_data($sensor);
}
$print_count = 0;
}
}
################################################################################
# parse sensor data
use constant PARSERS => {
'0000181a-0000-1000-8000-00805f9b34fb' => \&parse_LYWSD03MMC_with_ATC_MiThermometer
};
# /~https://github.com/atc1441/ATC_MiThermometer#advertising-format-of-the-custom-firmware
sub parse_LYWSD03MMC_with_ATC_MiThermometer {
my ($service_data) = @_;
my (@raw) = @{$service_data};
return {
TEMPERATURE_CELSIUS => ($raw[7] * 256 + $raw[8]) / 100,
HUMIDITY_PERCENT => $raw[9] * 2.56,
BATTERY_PERCENT => $raw[10],
BATTERY_MILLIVOLT => $raw[11] * 256 + $raw[12],
FRAME_COUNTER => $raw[13],
}
}
################################################################################
# handle Bluetooth Devices, that is: our sensors (ignoring everything else)
# DBus interface for Devices
use constant BLUEZ_DEVICE => 'org.bluez.Device1';
my %known_devices;
sub get_device_name {
my ($properties) = @_;
return $properties->{Name} // '**null**';
}
sub get_device_service_data {
my ($properties) = @_;
return %{$properties->{ServiceData} // {}};
}
sub is_device_a_supported_sensor {
my ($properties) = @_;
my %service_data = get_device_service_data($properties);
# Fixme: use grep/map
foreach my $service_data_uuid (keys %service_data) {
return 1 if exists PARSERS->{$service_data_uuid}
}
return 0;
}
sub device_added {
my ($path, $properties) = @_;
my $name = get_device_name($properties);
if (!is_device_a_supported_sensor($properties)) {
log_other "ignoring added Device $name at $path";
return;
}
log_info "found Device $name at $path:";
log_dumper $properties;
$known_sensors{$path}->{NAME} = $name;
$known_devices{$path}->{NAME} = $name;
record_device_data($path, $properties);
my $properties_if = dbus_get_properties_if($path);
push @{$known_devices{$path}->{SIGNALS}},
register_signal(
$properties_if,
'PropertiesChanged',
sub {
my ($interface, $changed, $invalidated) = @_;
device_properties_changed($path, $changed);
});
}
sub device_removed {
my ($path) = @_;
my $device = $known_devices{$path};
unless (defined $device) {
log_other "ignoring removed Device at $path";
return;
}
my $name = $device->{NAME};
log_info "removed Device $name at $path:";
# FIXME: allow calling with Array AND ArrayRef
unregister_signals(@{$device->{SIGNALS}});
delete $known_devices{$path};
# FIXME remove sensor data, too?
}
sub record_device_data {
my ($path, $properties) = @_;
my $has_changed = 0;
my $rssi = $properties->{RSSI};
if (defined $rssi) {
$known_sensors{$path}->{RSSI_DBM} = $rssi;
# we don't bother to check if it actually has changed, just trust Bluez here
$has_changed = 1;
}
my %service_data = get_device_service_data($properties);
while (my ($service_data_uuid, $service_data) = each(%service_data)) {
my $parser = PARSERS->{$service_data_uuid};
next unless defined $parser;
# FIXME: extract to merge()
my %new_values = %{$parser->($service_data)};
while (my ($key, $value) = each(%new_values)) {
$known_sensors{$path}->{$key} = $value;
}
# only write on sensor updates
# TODO: write on both RSSI and sensor updates instead?
store_in_rrd($known_sensors{$path});
$has_changed = 1;
}
return $has_changed;
}
sub device_properties_changed {
my ($path, $changed) = @_;
if (record_device_data($path, $changed)) {
show_sensor_data();
}
}
################################################################################
# handle Bluetooth Adapters aka. "our network devices" (eg. Bluetooth USB stick)
# DBus interface for Adapters
use constant BLUEZ_ADAPTER => 'org.bluez.Adapter1';
sub adapter_set_to_power {
my ($path) = @_;
my $properties_if = dbus_get_properties_if($path);
$properties_if->Set(BLUEZ_ADAPTER, 'Powered', dbus_boolean(1));
log_info " set to power";
}
sub adapter_start_le_discovery {
my ($path) = @_;
my $object = $service->get_object($path);
my $adapter = $object->as_interface(BLUEZ_ADAPTER);
$adapter->SetDiscoveryFilter({
'Transport' => 'le',
'DuplicateData' => dbus_boolean(0),
});
# FIXME: catch discovery already in progress
$adapter->StartDiscovery;
log_info " discovery started";
}
sub has_changed_to_off {
my ($changed, $property) = @_;
# false if missing (undefined) or truthy value
# true if set and falsy value
return ! ($changed->{$property} // 1);
}
sub adapter_properties_changed {
my ($path, $changed) = @_;
use Data::Dumper;
print "adapter $path property change:\n" . Dumper($changed) . "\n";
if (has_changed_to_off($changed, 'Powered')) {
log_info("Adapter $path has lost power");
# FIXME: setting power this will crash if the adapter has been removed
adapter_set_to_power($path);
}
if (has_changed_to_off($changed, 'Discovering')) {
log_info("Adapter $path stopped discovering");
# FIXME: discovering will crash if the adapter has been removed
adapter_start_le_discovery($path);
}
}
my %known_adapters;
sub adapter_added {
my ($path, $properties) = @_;
log_info "found Adapter $path:";
my $properties_if = dbus_get_properties_if($path);
push @{$known_adapters{$path}->{SIGNALS}},
register_signal(
$properties_if,
'PropertiesChanged',
sub {
my ($interface, $changed, $invalidated) = @_;
adapter_properties_changed($path, $changed);
});
# act on current state and init adapter if needed
adapter_properties_changed($path, $properties);
}
sub adapter_removed {
my ($path) = @_;
log_info "removed Adapter $path";
unregister_signals(@{$known_devices{$path}->{SIGNALS}});
delete $known_devices{$path};
}
################################################################################
# handle Bluetooth Interfaces, eg. "org/bluez/hci0"
sub interfaces_added {
my ($path, $interfaces) = @_;
while (my ($interface, $properties) = each(%{$interfaces})) {
if ($interface eq BLUEZ_ADAPTER) {
adapter_added($path, $properties);
}
elsif ($interface eq BLUEZ_DEVICE) {
device_added($path, $properties);
}
}
}
sub interfaces_removed {
my ($path, $interfaces) = @_;
foreach my $interface (@{$interfaces}) {
if ($interface eq BLUEZ_ADAPTER) {
adapter_removed($path);
}
elsif ($interface eq BLUEZ_DEVICE) {
device_removed($path);
}
}
}
################################################################################
# main routine
log_info "register signals for interface changes:";
my $object_manager = $service->get_object("/");
my @global_signals;
push @global_signals, register_signal($object_manager, 'InterfacesAdded', \&interfaces_added);
push @global_signals, register_signal($object_manager, 'InterfacesRemoved', \&interfaces_removed);
# do an initial scan of existing adapters
my $managed_objects = $object_manager->GetManagedObjects;
while (my ($path, $interfaces) = each %{$managed_objects}) {
interfaces_added($path, $interfaces);
}
show_sensor_data();
# RUN IT IN THE MAIN LOOP
Net::DBus::Reactor->main->run;
# FIXME: how can we get here to clean up?
log_info "unregister all signals:";
unregister_signals(@{$known_devices{$_}->{SIGNALS}}) foreach keys %known_devices;
unregister_signals(@global_signals);