diff --git a/ruuvitag_sensor/adapters/nix_hci.py b/ruuvitag_sensor/adapters/nix_hci.py index c671f4f..1e06cf9 100644 --- a/ruuvitag_sensor/adapters/nix_hci.py +++ b/ruuvitag_sensor/adapters/nix_hci.py @@ -25,15 +25,18 @@ def start(bt_device=''): if not bt_device: bt_device = 'hci0' + is_root = os.getuid() == 0 + log.info('Start receiving broadcasts (device %s)', bt_device) DEVNULL = subprocess.DEVNULL if sys.version_info >= (3, 3) else open(os.devnull, 'wb') def reset_ble_adapter(): - log.info("FYI: Calling a process with sudo: hciconfig %s reset", bt_device) - return subprocess.call( - 'sudo hciconfig %s reset' % bt_device, - shell=True, - stdout=DEVNULL) + cmd = 'hciconfig %s reset' % bt_device + log.info("FYI: Calling a process{}: {}".format( + "" if is_root else " with sudo", cmd)) + + cmd = "sudo {}".format(cmd) if not is_root else cmd + return subprocess.call(cmd, shell=True, stdout=DEVNULL) def start_with_retry(func, try_count, interval, msg): retcode = func() @@ -53,12 +56,22 @@ def start_with_retry(func, try_count, interval, msg): log.info('Problem with hciconfig reset. Exit.') exit(1) - log.info("FYI: Spawning process with sudo: hcitool -i %s lescan2 --duplicates --passive", bt_device) - hcitool = ptyprocess.PtyProcess.spawn( - ['sudo', '-n', 'hcitool', '-i', bt_device, 'lescan2', '--duplicates', '--passive']) - log.info("FYI: Spawning process with sudo: hcidump -i %s --raw", bt_device) - hcidump = ptyprocess.PtyProcess.spawn( - ['sudo', '-n', 'hcidump', '-i', bt_device, '--raw']) + cmd = ['hcitool', '-i', bt_device, 'lescan2', '--duplicates', '--passive'] + log.info("FYI: Spawning process{}: {}".format( + "" if is_root else " with sudo", ' '.join(str(i) for i in cmd))) + + if not is_root: + cmd.insert(0, "sudo") + hcitool = ptyprocess.PtyProcess.spawn(cmd) + + cmd = ['hcidump', '-i', bt_device, '--raw'] + log.info("FYI: Spawning process{}: {}".format( + "" if is_root else " with sudo", ' '.join(str(i) for i in cmd))) + + if not is_root: + cmd.insert(0, "sudo") + hcidump = ptyprocess.PtyProcess.spawn(cmd) + return (hcitool, hcidump) @staticmethod