-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.coffee
83 lines (73 loc) · 2.6 KB
/
index.coffee
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
76
77
78
79
80
81
82
83
# BLE-SCANNER
spawn = require('child_process').spawn
#class ex.BleScanner
class BleScanner
# singleton instance
instance = undefined
hciconfig = {}
hcidump = {}
hcitool = {}
# The class must be constructed with a callback function
# which will process all packets and it will be constructed
# by creating all hooks to bluez
# callback - function, that receives one data parameter
init = (hcidev, callback) ->
# bring up hci device
hciconfig = spawn 'hciconfig', [hcidev,'up']
# dump results from scan
hcidump = spawn 'hcidump',['-R']
hciconfig.on "exit", (code) ->
if code != 0
console.log "HCICONFIG: failed to bring up device "+hcidev
else
console.log "HCICONFIG: succesfully brought up device "+hcidev
# clear hci-processes
clearHciDump = spawn "killall", ["hcidump"]
clearHciTool = spawn "killall", ["hcitool"]
# reset singleton if tool exists
clearHciTool.on "exit", (code) ->
console.log "HCITOOL: cleared (code #{code})"
# start le scan
hcitool = spawn 'hcitool',['lescan']
hcitool.on "exit", (code) ->
if code == 1
console.log "HCITOOL: exited, already running? (code 1)"
else
console.log "HCITOOL: exited (code #{code})"
instance = undefined
# dump results from scan
clearHciDump.on "exit", (code) ->
console.log "HCIDUMP: cleared (code #{code})"
hcidump = spawn 'hcidump',['-R']
# exit handling
hcidump.on "exit", (code) ->
console.log "HCIDUMP: exited (code #{code})"
instance = undefined
# Set listener for hcidump
hcidump.stdout.on('data', (data) ->
# remove the first 2 bytes, they contain "> " from the dumptool
# convert to ascii to have the original RAW
data = (data.slice 2).toString('ascii').trim()
# filter packet dumps only
if data.split(" ")[0] == "04"
data = filterHciDump(data)
callback(data)
)
destroy : ->
try
hcidump.kill()
hcitool.kill()
finally
instance = undefined
constructor : (hcidev, callback) ->
instance = init(hcidev, callback) unless instance
instance
# Define helper to format BLE packet
filterHciDump = (output) ->
# strip line breaks from string
output = output.replace(/(\r\n|\n|\r)/gm,"");
# strip double spaces from string
output = output.replace(/\s+/g," ")
# split into hex array
output = output.split " "
module.exports = BleScanner