lepresenced: V0.92: Restart hcitool on HCI Command (forum #75559).

git-svn-id: https://svn.fhem.de/fhem/trunk/fhem@21704 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
PatrickR 2020-04-16 19:19:22 +00:00
parent 7831b8fc26
commit 121c17086e
2 changed files with 33 additions and 22 deletions

Binary file not shown.

View File

@ -58,7 +58,7 @@ use constant DEFAULT_RSSI_THRESHOLD => 10;
use constant RSSI_WINDOW => 10; use constant RSSI_WINDOW => 10;
use constant ME => 'lepresenced'; use constant ME => 'lepresenced';
use constant VERSION => '0.9'; use constant VERSION => '0.92';
use constant PIDFILE => '/var/run/' . ME . '.pid'; use constant PIDFILE => '/var/run/' . ME . '.pid';
@ -75,6 +75,7 @@ my @clients = ();
my ($log_level, $log_target); my ($log_level, $log_target);
my $debug; my $debug;
my ($beacons_hcitool, $beacons_hcidump) : shared = (0, 0); my ($beacons_hcitool, $beacons_hcidump) : shared = (0, 0);
my $restart_hcitool :shared;
sub syslogw { sub syslogw {
return if (scalar(@_) < 2); return if (scalar(@_) < 2);
@ -207,9 +208,15 @@ sub update_device($$$) {
sub bluetooth_scan_thread($$) { sub bluetooth_scan_thread($$) {
my ($device, $legacy_mode) = @_; my ($device, $legacy_mode) = @_;
my $hcitool; my $hcitool;
$restart_hcitool = 0;
for(;;) { for(;;) {
($beacons_hcitool, $beacons_hcidump) = (0, 0);
my $pid = open($hcitool, "-|", "stdbuf -oL hcitool -i " . $device . " lescan --duplicates 2>&1") || die('Unable to start scanning. Please make sure hcitool and stdbuf are installed!'); my $pid = open($hcitool, "-|", "stdbuf -oL hcitool -i " . $device . " lescan --duplicates 2>&1") || die('Unable to start scanning. Please make sure hcitool and stdbuf are installed!');
while (<$hcitool>) { while (<$hcitool>) {
if ($restart_hcitool) {
$restart_hcitool = 0;
last();
}
chomp($_); chomp($_);
if ($_ eq 'LE Scan ...') { if ($_ eq 'LE Scan ...') {
syslogw(LOG_INFO, "Received '%s'.", $_); syslogw(LOG_INFO, "Received '%s'.", $_);
@ -249,7 +256,12 @@ sub bluetooth_dump_thread($) {
while (<$hcidump>) { while (<$hcidump>) {
chomp($_); chomp($_);
if ($_ =~ m/^>/) { if ($_ =~ m/^< HCI Command: / && $beacons_hcitool > 0) { # Ignore initial settings, i. e. before first beacon
# https://forum.fhem.de/index.php/topic,75559.msg1007719.html#msg1007719
syslogw(LOG_WARNING, "Received '%s', telling hcidump to restart...", $_);
$state = HCIDUMP_STATE_NONE;
$restart_hcitool = 1;
} elsif ($_ =~ m/^>/) {
if ($current_mac) { if ($current_mac) {
#printf("DEBUG: mac: %s, name: '%s', rssi: %s\n", $current_mac, $current_name, $current_rssi); #printf("DEBUG: mac: %s, name: '%s', rssi: %s\n", $current_mac, $current_name, $current_rssi);
@ -404,12 +416,12 @@ sub cleanup_task() {
openlog(ME, 'pid', LOG_USER); openlog(ME, 'pid', LOG_USER);
(my $device, my $daemonize, my $listen_address, my $listen_port, $log_level, $log_target, $debug, my $legacy_mode, my $rssi_threshold) = parse_options(); (my $device, my $daemonize, my $listen_address, my $listen_port, $log_level, $log_target, $debug, my $legacy_mode, my $rssi_threshold) = parse_options();
local $SIG{INT} = local $SIG{TERM} = local $SIG{HUP} = sub { local $SIG{INT} = local $SIG{TERM} = local $SIG{HUP} = sub {
syslogw(LOG_NOTICE, "Caught signal, cleaning up and exiting..."); syslogw(LOG_NOTICE, "Caught signal, cleaning up and exiting...");
unlink(PIDFILE) if (-e PIDFILE); unlink(PIDFILE) if (-e PIDFILE);
closelog(); closelog();
exit(1); exit(1);
}; };
syslogw(LOG_NOTICE, "Version %s started (device: %s, listen addr: %s, listen port: %s, daemonize: %i, legacy mode: %i, rssi threshold: %i, log level: %i, debug: %i).", syslogw(LOG_NOTICE, "Version %s started (device: %s, listen addr: %s, listen port: %s, daemonize: %i, legacy mode: %i, rssi threshold: %i, log level: %i, debug: %i).",
VERSION, $device, $listen_address, $listen_port, $daemonize, $legacy_mode, $rssi_threshold, $log_level, $debug); VERSION, $device, $listen_address, $listen_port, $daemonize, $legacy_mode, $rssi_threshold, $log_level, $debug);
@ -449,7 +461,6 @@ for(;;) {
syslogw(LOG_INFO, "Connection from %s:%s. Connected clients: %i.", $client_socket->peerhost(), $client_socket->peerport(), $select->count()-1); syslogw(LOG_INFO, "Connection from %s:%s. Connected clients: %i.", $client_socket->peerhost(), $client_socket->peerport(), $select->count()-1);
} else { } else {
sysread ($current_client, my $buf, INET_RECV_BUFFER); sysread ($current_client, my $buf, INET_RECV_BUFFER);
my $disconnect; my $disconnect;
if ($buf) { if ($buf) {
chomp($buf); chomp($buf);