3 function getConfig(configname)
5 local uci=require("uci")
14 logging = cur.get(config,"logging","enabled")
16 mqtt_host = cur.get(config,"mqtt","host")
17 mqtt_port = cur.get(config,"mqtt","port")
18 mqtt_id = cur.get(config,"mqtt","id")
19 mqtt_topic = cur.get(config,"mqtt","topic")
21 mqtt_user = cur.get(config,"mqtt","user")
22 mqtt_passwd = cur.get(config,"mqtt","password")
24 if mqtt_host and not mqtt_id then
28 if mqtt_host and not mqtt_port then
32 if mqtt_host and not mqtt_topic then
33 mqtt_topic = 'beaconmon/{type}/{details}'
38 function capture(cmd, raw)
39 local f = assert(io.popen(cmd, 'r'))
40 local s = assert(f:read('*a'))
42 if raw then return s end
43 s = string.gsub(s, '^%s+', '')
44 s = string.gsub(s, '%s+$', '')
45 s = string.gsub(s, '[\n\r]+', ' ')
49 function mqtt_encode(str)
51 str = string.gsub (str, "\n", "")
52 str = string.gsub (str, ":", "-")
53 str = string.gsub (str, "/", "-")
54 str = string.gsub (str, " ", "_")
59 function printLog(str)
61 capture("logger -t beaconmon "..str)
67 function run_command(cmd)
69 local file = assert(io.popen(cmd, 'r'))
70 local output = file:read('*all')
77 run_command("/bin/kill `/usr/bin/pgrep hcidump`")
78 run_command("/bin/kill `/usr/bin/pgrep hcitool`")
79 f = assert(io.popen ("/usr/bin/hcidump --raw"))
80 run_command("/usr/bin/hciconfig hci0 down")
81 run_command("/usr/bin/hciconfig hci0 up")
82 f_null = assert(io.popen ("/usr/bin/hcitool lescan --duplicates"))
89 if type(o) == 'table' then
91 for k,v in pairs(o) do
92 if type(k) ~= 'number' then k = '"'..k..'"' end
93 s = s .. '['..k..'] = ' .. dump(v) .. ','
94 -- s = s .. dump(v) .. ','
103 return (s:gsub("^%s*(.-)%s*$", "%1"))
106 function mqtt_pub(path,value)
107 printLog("Pub "..path.." "..value)
108 return mqtt_client:publish(path,value)
111 function process_packet(packet)
127 if packet:len()>1 then
129 while packet:len()>2 do
130 bytes[idx]=trim(packet:sub(1,3))
136 if bytes[1]=='04' and bytes[2]=='3E' then
139 mac=bytes[13]..':'..bytes[12]..':'..bytes[11]..':'..bytes[10]..':'..bytes[9]..':'..bytes[8]
141 power=tonumber("0x"..bytes[len-1])-256
142 tx=tonumber("0x"..bytes[len])-256
145 paysublen=tonumber('0x'..bytes[j])
146 if bytes[j+1]=="FF" and bytes[j+2]=="4C" and bytes[j+3]=="00" and bytes[j+4]=="02" and bytes[j+5]=="15" then
147 -- Standard UUID iBeacon
149 local uuid1=bytes[j+6]..bytes[j+7]..bytes[j+8]..bytes[j+9]
150 local uuid2=bytes[j+10]..bytes[j+11]
151 local uuid3=bytes[j+12]..bytes[j+13]
152 local uuid4=bytes[j+14]..bytes[j+15]
153 local uuid5=bytes[j+16]..bytes[j+17]..bytes[j+18]..bytes[j+19]..bytes[j+20]..bytes[j+21]
154 uuid=string.lower(uuid1..'-'..uuid2..'-'..uuid3..'-'..uuid4..'-'..uuid5)
155 major=bytes[j+23]..bytes[j+22]
156 minor=bytes[j+25]..bytes[j+24]
160 if type=="ibeacon" then
161 printLog(string.format("{type:'ibeacon',mac:'%s',uuid:'%s',major:'%s',minor:'%s',power:%d,tx:%d}",mac,uuid,major,minor,power,tx))
162 details=uuid..'/'..major..'/'..minor
168 if not (type=="unknown") then
169 mqtt_path=string.gsub(mqtt_topic,"{(.-)}",
172 return mqtt_encode(type)
173 elseif name=="details" then
174 return mqtt_encode(details)
176 return '{'..name..'}'
180 if not pcall(mqtt_pub,mqtt_path,tx) then
181 printLog('Reconnecting MQTT...')
182 mqtt_client:connect(mqtt_id)
195 for line in inp:lines() do
200 if lchr=="<" or lchr==">" then
201 line = trim(line:sub(2))
204 if not (lchr == " ") then
205 process_packet(packet)
212 packet = packet .. " " .. line
213 process_packet(packet)
216 packet = packet .. " " .. line
226 MQTT = require "paho.mqtt"
227 mqtt_client = MQTT.client.create(mqtt_host, mqtt_port)
229 mqtt_client:auth(mqtt_user, mqtt_passwd)
231 mqtt_client:connect(mqtt_id)