-
Notifications
You must be signed in to change notification settings - Fork 6
/
firmware.go
84 lines (73 loc) · 1.54 KB
/
firmware.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
package lifxlan
import (
"bytes"
"context"
"encoding/binary"
"net"
)
// RawStateHostFirmwarePayload defines the struct to be used for encoding and
// decoding.
//
// https://lan.developer.lifx.com/docs/information-messages#statehostfirmware---packet-15
type RawStateHostFirmwarePayload struct {
_ uint64 // build
_ [8]byte // reserved
VersionMinor uint16
VersionMajor uint16
}
// ToFirmware converts RawStateHostFirmwarePayload into FirmwareUpgrade
// with empty Features.
func (raw RawStateHostFirmwarePayload) ToFirmware() FirmwareUpgrade {
return FirmwareUpgrade{
Major: raw.VersionMajor,
Minor: raw.VersionMinor,
}
}
func (d *device) Firmware() *FirmwareUpgrade {
return &d.firmware
}
func (d *device) GetFirmware(ctx context.Context, conn net.Conn) error {
if ctx.Err() != nil {
return ctx.Err()
}
if conn == nil {
newConn, err := d.Dial()
if err != nil {
return err
}
defer newConn.Close()
conn = newConn
if ctx.Err() != nil {
return ctx.Err()
}
}
seq, err := d.Send(
ctx,
conn,
0, // flags
GetHostFirmware,
nil, // payload
)
if err != nil {
return err
}
for {
resp, err := ReadNextResponse(ctx, conn)
if err != nil {
return err
}
if resp.Sequence != seq || resp.Source != d.Source() {
continue
}
if resp.Message != StateHostFirmware {
continue
}
var raw RawStateHostFirmwarePayload
r := bytes.NewReader(resp.Payload)
if err := binary.Read(r, binary.LittleEndian, &raw); err != nil {
return err
}
d.firmware = raw.ToFirmware()
return nil
}
}