-
Notifications
You must be signed in to change notification settings - Fork 0
/
hci_pscan.rb
76 lines (68 loc) · 1.6 KB
/
hci_pscan.rb
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
# Path to hciconfig executable
EXEC = "/usr/sbin/hciconfig"
# Look for keywords in results
SSTR = ["UP", "DOWN", "RUNNING", "PSCAN", "ISCAN"]
# Interval
INTC = 10
# Check if running as root
raise "Must run as root" if not Process.uid == 0
puts "Daemon starting..."
begin
while true
# Fetch result
result = `#{EXEC}`
array = result.split("\t").map{|ln| ln.rstrip}
#p array
# Parse result
hcimap = Hash.new()
dev = nil
for ln in array
if match = ln.match(/^(hci\d+)/)
dev = match.captures[0]
if not hcimap.has_key? dev
hcimap[dev] = nil
else
STDERR.puts "Warning: Duplicate HCI device identifiers"
end
else
next if not dev
if ln =~ /^[A-Z\s]+$/ and ln =~ /\b(UP|DOWN)\b/
if not hcimap[dev]
hcimap[dev] = Hash.new()
for sstr in SSTR
hcimap[dev][sstr] = ln =~ /\b#{sstr}\b/
end
else
STDERR.puts "Warning: Duplicate HCI device state for #{dev}"
end
end
end
end
#p hcimap
dev = nil
# Check state of each HCI device
hcimap.each do |dev, states|
if states.has_key? "DOWN" and states["DOWN"]
puts "HCI device #{dev} is DOWN. Will bring it up."
`hciconfig #{dev} up`
elsif states.has_key? "UP" and states["UP"]
if states.has_key? "PSCAN" and states["PSCAN"]
nil
else
puts "HCI device #{dev} is UP without PSCAN. Will enable it."
if states.has_key? "ISCAN" and states["ISCAN"]
`hciconfig #{dev} piscan`
else
`hciconfig #{dev} pscan`
end
end
end
end
# Sleep
sleep INTC
end
rescue SystemExit, Interrupt
puts
ensure
puts "Daemon terminated."
end