/
tlm-frame-imp.go
185 lines (149 loc) · 5.05 KB
/
tlm-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
package eddystone
import (
"encoding/binary"
"fmt"
)
/*⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺*\
▏ ▕
▏ Internal Constants ▕
▏ ▕
\*⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽*/
const (
tlmSize = 14
tlmVoltageDisabled uint16 = 0x0000
tlmTempDisabled uint16 = 0x8000
)
const (
jsonTlmFormat = `{` +
`"type":"%s",` +
`"typeId":"%d",` +
`"voltage":"%d",` +
`"beaconTempC":%.8f,` +
`"advertisements":%d,` +
`"uptime":%.8f` +
`}`
tlmFormatString = "TlmFrame{Type: 0x%x, Version: 0x%x, Voltage: %d, " +
"Temp: %.8fC, AdCount: %d, Seconds: %.1f}"
)
const (
errTlmPacketSize = "invalid tlm packet size; expected %d bytes, got %d"
errTlmPacketType = "invalid tlm frame type id; expected 0x%x, got 0x%x"
)
/*⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺*\
▏ ▕
▏ Exported Functions ▕
▏ ▕
\*⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽*/
func IsTlmPacket(b []byte) bool {
return tlmValidatePayload(b) == nil
}
/*⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺*\
▏ ▕
▏ Internal Helpers ▕
▏ ▕
\*⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽*/
func newErrTlmPacketSize(exp, act int) error {
return fmt.Errorf(errTlmPacketSize, exp, act)
}
func newErrTlmPacketType(act byte) error {
return fmt.Errorf(errTlmPacketType, FrameTypeTlm.Id(), act)
}
func tlmValidatePayload(b []byte) error {
if len(b) != tlmSize {
return newErrTlmPacketSize(tlmSize, len(b))
}
if b[0] != FrameTypeTlm.Id() {
return newErrTlmPacketType(b[0])
}
return nil
}
/*⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺*\
▏ ▕
▏ Internal Implementation ▕
▏ ▕
\*⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽*/
type tlmFrame struct {
version byte
voltage uint16
temp uint16
adCount uint32
seconds uint32
}
func (t tlmFrame) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(
jsonTlmFormat,
FrameTypeTlm.Value(),
FrameTypeTlm.Id(),
t.BatteryVoltage(),
t.BeaconTemperatureInC(),
t.AdvertisementCount(),
t.ActiveTime(),
)), nil
}
func (t tlmFrame) String() string {
return fmt.Sprintf(
tlmFormatString,
t.Type().Id(),
t.TlmVersion(),
t.BatteryVoltage(),
t.BeaconTemperatureInC(),
t.AdvertisementCount(),
t.ActiveTime(),
)
}
func (t tlmFrame) Type() FrameType {
return FrameTypeTlm
}
func (t tlmFrame) ToBytes() (out []byte) {
return []byte{
t.Type().Id(),
t.version,
byte(t.voltage >> 8),
u16ToByte(t.voltage),
byte(t.temp >> 8),
u16ToByte(t.temp),
u32ToByte(t.adCount >> 24),
u32ToByte(t.adCount >> 16),
u32ToByte(t.adCount >> 8),
u32ToByte(t.adCount),
u32ToByte(t.seconds >> 24),
u32ToByte(t.seconds >> 16),
u32ToByte(t.seconds >> 8),
u32ToByte(t.seconds),
}
}
func (t *tlmFrame) FromBytes(b []byte) error {
if e := tlmValidatePayload(b); e != nil {
return e
}
t.version = b[1]
t.voltage = binary.BigEndian.Uint16(b[2:4])
t.temp = binary.BigEndian.Uint16(b[4:6])
t.adCount = binary.BigEndian.Uint32(b[6:10])
t.seconds = binary.BigEndian.Uint32(b[10:14])
return nil
}
func (t tlmFrame) TlmVersion() byte {
return t.version
}
func (t tlmFrame) BatteryVoltage() uint16 {
return t.voltage
}
func (t tlmFrame) BeaconTemperatureInC() float32 {
return floatFrom88(int16(t.temp))
}
func (t tlmFrame) BeaconTemperatureInF() float32 {
return cToF(t.BeaconTemperatureInC())
}
func (t tlmFrame) AdvertisementCount() uint32 {
return t.adCount
}
func (t tlmFrame) ActiveTime() float32 {
return float32(t.seconds) / float32(10)
}
func (t tlmFrame) SupportsBatteryVoltage() bool {
return t.voltage != tlmVoltageDisabled
}
func (t tlmFrame) SupportsTemperature() bool {
return t.temp != tlmTempDisabled
}