/
uid-frame-imp.go
219 lines (171 loc) · 5.3 KB
/
uid-frame-imp.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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package eddystone
import (
"fmt"
"github.com/google/uuid"
"math/big"
"strings"
)
/*⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺*\
▏ ▕
▏ Internal Constants ▕
▏ ▕
\*⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽*/
const (
uidFrameLen = 31 // TODO: confirm this is how it is reported...
uidNSpaceLen = 10
uidIdenLen = 6
)
const (
uidFormatString = "UidFrame{Type: 0x%x, TxPower: %d, UUID: %s}"
uidFormatJson = `{` +
`"type":"%s",` +
`"typeId": %d,` +
`"txPower": %d,` +
`"uuid": "%s"` +
`}`
)
const(
errUidBadLen = "invalid UID frame length; expected %d, got %d"
errUidBadType = "invalid UID frame type; expected 0x%x, got 0x%x"
)
/*⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺*\
▏ ▕
▏ Exported Functions ▕
▏ ▕
\*⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽*/
func IsUidPacket(b []byte) bool {
return uidValidatePayload(b) == nil
}
/*⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺*\
▏ ▕
▏ Internal Helpers ▕
▏ ▕
\*⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽*/
func newUidLengthError(act int) error {
return fmt.Errorf(errUidBadLen, uidFrameLen, act)
}
func newUidTypeError(act byte) error {
return fmt.Errorf(errUidBadType, FrameTypeUid.Id(), act)
}
// Checks the given byte slice to confirm that it at least
// looks like a UID packet by checking length and type
// indicator.
func uidValidatePayload(b []byte) error {
if len(b) != uidFrameLen {
return newUidLengthError(len(b))
}
if b[0] != FrameTypeUid.Id() {
return newUidTypeError(b[0])
}
return nil
}
/*⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺*\
▏ ▕
▏ Internal Implementation ▕
▏ ▕
\*⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽*/
type uidFrame struct {
power uint8
nSpace [uidNSpaceLen]byte
iden [uidIdenLen]byte
}
func (u uidFrame) String() string {
return fmt.Sprintf(uidFormatString, u.Type().Id(), u.TxPower(), u.Uuid())
}
func (u uidFrame) MarshalJSON() ([]byte, error) {
tpe := u.Type()
return []byte(fmt.Sprintf(
uidFormatJson,
tpe.Value(),
tpe.Id(),
u.TxPower(),
u.Uuid(),
)), nil
}
func (u uidFrame) Type() FrameType {
return FrameTypeUid
}
func (u *uidFrame) ToBytes() (out []byte) {
var off offset
out = make([]byte, uidFrameLen)
out[off.inc()] = u.Type().Id()
for i := 0; i < uidNSpaceLen; i++ {
out[off.inc()] = u.nSpace[i]
}
for i := 0; i < uidIdenLen; i++ {
out[off.inc()] = u.iden[i]
}
return
}
func (u *uidFrame) FromBytes(b []byte) error {
if e := uidValidatePayload(b); e != nil {
return e
}
var off offset
// Skip the type byte
_ = off.inc()
u.power = b[off.inc()]
for i := 0; i < uidNSpaceLen; i++ {
u.nSpace[i] = b[off.inc()]
}
for i := 0; i < uidIdenLen; i++ {
u.iden[i] = b[off.inc()]
}
return nil
}
func (u uidFrame) TxPower() int8 {
return int8(u.power)
}
func (u uidFrame) RangingData() int8 {
return u.TxPower()
}
func (u *uidFrame) NamespaceBytes() (out [uidNSpaceLen]byte) {
for i := range u.nSpace {
out[i] = u.nSpace[i]
}
return
}
func (u *uidFrame) NamespaceString() string {
var sb strings.Builder
for _, v := range u.nSpace {
byteToHex(v, &sb)
}
return sb.String()
}
func (u *uidFrame) NamespaceInt() *big.Int {
return new(big.Int).SetBytes(u.nSpace[:])
}
func (u *uidFrame) InstanceBytes() (out [6]byte) {
for i := range u.iden {
out[i] = u.iden[i]
}
return
}
func (u *uidFrame) InstanceString() string {
var sb strings.Builder
for _, v := range u.iden {
byteToHex(v, &sb)
}
return sb.String()
}
func (u *uidFrame) InstanceInt() (out uint64) {
for i, v := range u.iden {
out |= uint64(v) << uint(i*8)
}
return
}
func (u *uidFrame) Uuid() (o uuid.UUID) {
size := uidNSpaceLen + uidIdenLen
buf := make([]byte, size)
for _, v := range u.nSpace {
buf = append(buf, v)
}
for _, v := range u.iden {
buf = append(buf, v)
}
o, e := uuid.FromBytes(buf)
if e != nil {
panic(e)
}
return
}