Skip to content

Commit

Permalink
Fix sudo: command not found issue (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
jlipponen authored Feb 19, 2022
1 parent c21b21f commit 1939518
Showing 1 changed file with 24 additions and 11 deletions.
35 changes: 24 additions & 11 deletions ruuvitag_sensor/adapters/nix_hci.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand Down

0 comments on commit 1939518

Please sign in to comment.