-
Notifications
You must be signed in to change notification settings - Fork 0
/
version.go
47 lines (38 loc) · 1.36 KB
/
version.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
// Copyright 2023 The FIT SDK for Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package proto
// Version is FIT Protocol Version
type Version byte
const (
ErrProtocolVersionNotSupported = errorString("protocol version not supported")
MajorVersionShift = 4
MajorVersionMask = 0x0F << MajorVersionShift
MinorVersionMask = 0x0F
V1 Version = 1 << MajorVersionShift // V1 is Version 1.0
V2 Version = 2 << MajorVersionShift // V2 is Version 2.0
Vmax = V2 // Vmax is an alias for the current latest version.
)
// CreateVersion creates version from major and minor value, it can only create version up < Vmax.
func CreateVersion(major, minor byte) (Version, bool) {
version := Version((major << MajorVersionShift) | minor)
if version > Vmax {
return 0, false
}
return version, true
}
// Validate checks whether given version is a valid version.
func Validate(version byte) error {
if VersionMajor(version) > VersionMajor(byte(Vmax)) {
return ErrProtocolVersionNotSupported
}
return nil
}
// VersionMajor returns major value of given version
func VersionMajor(version byte) byte {
return version >> MajorVersionShift
}
// VersionMinor returns minor value of given version
func VersionMinor(version byte) byte {
return version & MinorVersionMask
}