5 function getConfig(configname)
7 local uci=require("uci")
16 logging = cur.get(config,"logging","enabled")
18 mqtt_host = cur.get(config,"mqtt","host")
19 mqtt_port = cur.get(config,"mqtt","port")
20 mqtt_id = cur.get(config,"mqtt","id")
21 mqtt_topic = cur.get(config,"mqtt","topic")
23 mqtt_user = cur.get(config,"mqtt","user")
24 mqtt_passwd = cur.get(config,"mqtt","password")
26 if mqtt_host and not mqtt_id then
27 socket = require("socket")
28 posix = require("posix")
29 hostname = socket.dns.gethostname()
31 mqtt_id="beaconmon-"..hostname.."-"..pid
34 if mqtt_host and not mqtt_port then
38 if mqtt_host and not mqtt_topic then
39 mqtt_topic = 'beaconmon/{type}/{details}'
44 function capture(cmd, raw)
45 local f = assert(io.popen(cmd, 'r'))
46 local s = assert(f:read('*a'))
48 if raw then return s end
49 s = string.gsub(s, '^%s+', '')
50 s = string.gsub(s, '%s+$', '')
51 s = string.gsub(s, '[\n\r]+', ' ')
55 function mqtt_encode(str)
57 str = string.gsub (str, "\n", "")
58 str = string.gsub (str, ":", "-")
59 str = string.gsub (str, "/", "-")
60 str = string.gsub (str, " ", "_")
65 function printLog(str)
66 if logging=="yes" then
67 capture("logger -t beaconmon \""..str.."\"")
73 function run_command(cmd)
75 local file = assert(io.popen(cmd, 'r'))
76 local output = file:read('*all')
83 run_command("/bin/kill `/usr/bin/pgrep btmon`")
84 run_command("/bin/kill `/usr/bin/pgrep hcitool`")
85 f = assert(io.popen ("/usr/bin/stdbuf -o0 /usr/bin/btmon"))
86 run_command("/usr/bin/hciconfig hci0 down")
87 run_command("/usr/bin/hciconfig hci0 up")
88 f_null = assert(io.popen ("/usr/bin/hcitool lescan --duplicates --passive"))
99 return (s:gsub("^%s*(.-)%s*$", "%1"))
102 function mqtt_pub(path,value)
103 res=mqtt_client:publish(path,value)
104 printLog("Pub "..path.." returned "..res);
108 function process_packet(packet)
116 mac = packet['Address']
117 uuid = packet['uuid']
118 type = packet['Type']
119 name = packet['Name (complete)']
121 if type=='iBeacon' then
135 if not (type=="unknown") then
136 mqtt_path=string.gsub(mqtt_topic,"{(.-)}",
139 return mqtt_encode(type)
140 elseif name=="details" then
141 return mqtt_encode(details)
143 return '{'..name..'}'
147 if not pcall(mqtt_pub,mqtt_path,dump(packet)) then
148 printLog('Reconnecting MQTT...')
149 mqtt_client:connect(mqtt_id)
156 function starts(String,Start)
157 return string.sub(String,1,string.len(Start))==Start
160 function split(inputstr, sep)
165 for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
189 value=trim(table.concat(t,':',2))
190 if name=="Address" then
191 value=split(value)[1]
194 elseif #t==1 and name then
195 if not(packet[name..'.list']) then
196 packet[name..'.list']={}
198 table.insert(packet[name..'.list'],(trim(t[1])))
202 if starts(str,'> HCI Event: LE Meta Event (0x3e)') then
205 elseif starts(str,'RSSI:') then
207 process_packet(packet)
217 io.stdout:setvbuf('no')
218 io.stdin:setvbuf('no')
223 MQTT = require "mosquitto"
224 mqtt_client = MQTT.new(mqtt_id)
226 mqtt_client:login_set(mqtt_user, mqtt_passwd)
228 mqtt_client:connect(mqtt_host,mqtt_port)