-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.js
81 lines (72 loc) · 1.76 KB
/
data.js
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
// creates the Slimp3 Client Protocol header
function createHeader() {
return Buffer.alloc(18)
}
// Writes the first byte of the header
function writeHeaderType(buf, t) {
buf.write(t, 0, 1, 'ascii')
}
// creates a discovery response packet
function createDiscovery() {
var buf = createHeader()
writeHeaderType(buf, 'D')
return buf
}
// creates a hello packet
function createHello() {
var buffer = createHeader()
writeHeaderType(buffer, 'h')
return buffer
}
// creates a rune command
function createCode(rune) {
var buf = Buffer.alloc(2)
buf.writeUInt8(3, 0)
buf.writeUInt8(rune.charCodeAt(0), 1)
return buf
}
// creates a reset command
function createResetCode() {
var buf = Buffer.alloc(2)
buf.writeUInt8(2, 0)
buf.writeUInt8(2, 1)
return buf
}
// creates a write string packet
function createWrite(text) {
var buf = createHeader()
writeHeaderType(buf, 'l')
var codes = [createResetCode()]
for (var r in text) {
codes.push(createCode(text[r]))
}
return Buffer.concat([buf].concat(codes))
}
// builds a packet
function build(options) {
switch (options.type) {
case 'discovery':
return createDiscovery()
case 'hello':
return createHello()
case 'write':
return createWrite(options.text)
}
}
// reads packet fields
function read(buffer) {
const type = buffer.slice(0,1).toString()
const mac = buffer.slice(12,17).toString('hex')
switch (type) {
case 'd':
return {type:type, mac: mac, client: buffer.readUInt8(2), firmware: buffer.readUInt8(3)}
case 'h':
return {type:type, mac: mac}
case 'i':
return {type:type, mac: mac, time: buffer.readUInt32BE(2), code: buffer.readUInt32BE(8)}
default:
return {type:'unknown', mac: mac}
}
}
exports.build = build
exports.read = read