-
Notifications
You must be signed in to change notification settings - Fork 0
/
beacon.go
46 lines (41 loc) · 859 Bytes
/
beacon.go
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
package csp
type Beacon struct {
ID byte
Name string
Description string
}
func NewBeacon(id byte, name, description string) *Beacon {
return &Beacon{
ID: id,
Name: name,
Description: description,
}
}
func NewBeaconFromMessage(message *Message) *Beacon {
return &Beacon{
ID: message.Payload[0],
Name: string(message.Payload[1:11]),
Description: string(message.Payload[11:31]),
}
}
func (b *Beacon) Message() *Message {
data := make([]byte, 1+10+20)
data[0] = b.ID
nameLen := len(b.Name)
for i := 0; i < 10; i++ {
if i < nameLen {
data[1+i] = b.Name[i]
} else {
data[1+i] = 0x20
}
}
descLen := len(b.Description)
for i := 0; i < 20; i++ {
if i < descLen {
data[11+i] = b.Description[i]
} else {
data[11+i] = 0x20
}
}
return NewBroadcast(CmdBeacon, data)
}