forked from tormoder/fit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.go
311 lines (283 loc) · 5.33 KB
/
base.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// Package types provides the set of base types defined by the FIT protocol.
// Users of this package must validate base types parsed from a raw byte using
// the Known method before calling any other methods (except String).
package types
import (
"fmt"
"math"
)
const (
pkg = "types"
typeNumMask = 0x1F
multiByteFlag = 0x80
)
type Base byte
// Base types for fit data.
// The base 5 bits increase by 1 for each definition with all multi-byte number types having the MSB set.
const (
BaseEnum Base = 0x00
BaseSint8 Base = 0x01 // 2's complement format
BaseUint8 Base = 0x02
BaseSint16 Base = 0x83 // 2's complement format
BaseUint16 Base = 0x84
BaseSint32 Base = 0x85 // 2's complement format
BaseUint32 Base = 0x86
BaseString Base = 0x07 // Null terminated string encoded in UTF-8
BaseFloat32 Base = 0x88
BaseFloat64 Base = 0x89
BaseUint8z Base = 0x0A
BaseUint16z Base = 0x8B
BaseUint32z Base = 0x8C
BaseByte Base = 0x0D // Array of bytes. Field is invalid if all bytes are invalid
BaseSint64 Base = 0x8E // 2's complement format
BaseUint64 Base = 0x8F
BaseUint64z Base = 0x90
)
// Internal compressed representation of certain base types.
// With this, we can fit all base types in 5 bits as opposed to 8 bits.
// Base types should be decompressed before use.
const (
compressedSint16 byte = 0x03
compressedUint16 byte = 0x04
compressedSint32 byte = 0x05
compressedUint32 byte = 0x06
compressedFloat32 byte = 0x08
compressedFloat64 byte = 0x09
compressedUint16z byte = 0x0B
compressedUint32z byte = 0x0C
compressedSint64 byte = 0x0E
compressedUint64 byte = 0x0F
compressedUint64z byte = 0x10
)
func decompress(b byte) Base {
b = b & typeNumMask
switch b {
case compressedSint16:
return BaseSint16
case compressedUint16:
return BaseUint16
case compressedSint32:
return BaseSint32
case compressedUint32:
return BaseUint32
case compressedFloat32:
return BaseFloat32
case compressedFloat64:
return BaseFloat64
case compressedUint16z:
return BaseUint16z
case compressedUint32z:
return BaseUint32z
case compressedSint64:
return BaseSint64
case compressedUint64:
return BaseUint64
case compressedUint64z:
return BaseUint64z
default:
return Base(b)
}
}
func (t Base) index() byte {
return byte(t) & typeNumMask
}
func (t Base) multibyte() bool {
return (byte(t) & multiByteFlag) == multiByteFlag
}
func (t Base) Float() bool {
return !t.Integer() && t.Signed()
}
func (t Base) GoInvalidValue() string {
return binvalid[t.index()]
}
func (t Base) GoType() string {
return bgotype[t.index()]
}
func (t Base) Integer() bool {
return binteger[t.index()]
}
func (t Base) Known() bool {
return int(t.index()) < len(bname) && (t.multibyte() == (t.Size() > 1))
}
func (t Base) PkgString() string {
return pkg + "." + t.String()
}
func (t Base) Signed() bool {
return bsigned[t.index()]
}
func (t Base) Size() int {
return bsize[t.index()]
}
func (t Base) String() string {
if t.Known() {
return bname[t.index()]
}
return fmt.Sprintf("unknown (0x%X)", byte(t))
}
func (t Base) Invalid() interface{} {
if t.Known() {
return goinvalid[t.index()]
}
return fmt.Sprintf("unknown (0x%X)", byte(t))
}
var bsize = [...]int{
1,
1,
1,
2,
2,
4,
4,
1,
4,
8,
1,
2,
4,
1,
8,
8,
8,
}
var bname = [...]string{
"BaseEnum",
"BaseSint8",
"BaseUint8",
"BaseSint16",
"BaseUint16",
"BaseSint32",
"BaseUint32",
"BaseString",
"BaseFloat32",
"BaseFloat64",
"BaseUint8z",
"BaseUint16z",
"BaseUint32z",
"BaseByte",
"BaseSint64",
"BaseUint64",
"BaseUint64z",
}
var binteger = [...]bool{
false,
true,
true,
true,
true,
true,
true,
false,
false,
false,
true,
true,
true,
false,
true,
true,
true,
}
var bsigned = [...]bool{
false,
true,
false,
true,
false,
true,
false,
false,
true,
true,
false,
false,
false,
false,
true,
false,
false,
}
var bgotype = [...]string{
"byte",
"int8",
"uint8",
"int16",
"uint16",
"int32",
"uint32",
"string",
"float32",
"float64",
"uint8",
"uint16",
"uint32",
"byte",
"int64",
"uint64",
"uint64",
}
var binvalid = [...]string{
"0xFF",
"0x7F",
"0xFF",
"0x7FFF",
"0xFFFF",
"0x7FFFFFFF",
"0xFFFFFFFF",
"\"\"",
"0xFFFFFFFF",
"0xFFFFFFFFFFFFFFFF",
"0x00",
"0x0000",
"0x00000000",
"0xFF",
"0x7FFFFFFFFFFFFFFF",
"0xFFFFFFFFFFFFFFFF",
"0x0000000000000000",
}
var goinvalid = [...]interface{}{
byte(0xFF),
int8(0x7F),
uint8(0xFF),
int16(0x7FFF),
uint16(0xFFFF),
int32(0x7FFFFFFF),
uint32(0xFFFFFFFF),
string(""),
math.Float32frombits(0xFFFFFFFF),
math.Float64frombits(0xFFFFFFFFFFFFFFFF),
uint8(0x00),
uint16(0x0000),
uint32(0x00000000),
byte(0xFF),
int64(0x7FFFFFFFFFFFFFFF),
uint64(0xFFFFFFFFFFFFFFFF),
uint64(0x0000000000000000),
}
func BaseFromString(s string) (Base, error) {
t, found := baseStringToType[s]
if !found {
return 0xFF, fmt.Errorf("no base type found for string: %q", s)
}
return t, nil
}
var baseStringToType = map[string]Base{
"enum": BaseEnum,
"sint8": BaseSint8,
"uint8": BaseUint8,
"sint16": BaseSint16,
"uint16": BaseUint16,
"sint32": BaseSint32,
"uint32": BaseUint32,
"string": BaseString,
"float32": BaseFloat32,
"float64": BaseFloat64,
"uint8z": BaseUint8z,
"uint16z": BaseUint16z,
"uint32z": BaseUint32z,
"byte": BaseByte,
"sint64": BaseSint64,
"uint64": BaseUint64,
"uint64z": BaseUint64z,
// Typo in SDK 20.14:
"unit8": BaseUint8,
}