-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserial.go
172 lines (147 loc) · 5.24 KB
/
serial.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
// This file is part of arduino-cloud-cli.
//
// Copyright (C) 2021 ARDUINO SA (http://www.arduino.cc/)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package serial
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"time"
"github.com/howeyc/crc16"
"go.bug.st/serial"
)
// Serial is a wrapper of serial port interface that
// features specific functions to send provisioning
// commands through the serial port to an arduino device.
type Serial struct {
port serial.Port
}
// NewSerial instantiate and returns a Serial instance.
// The Serial Connect method should be called before using
// its send/receive functions.
func NewSerial() *Serial {
s := &Serial{}
return s
}
// Connect tries to connect Serial to a specific serial port.
func (s *Serial) Connect(address string) error {
mode := &serial.Mode{
BaudRate: 57600,
}
port, err := serial.Open(address, mode)
if err != nil {
err = fmt.Errorf("%s: %w", "connecting to serial port", err)
return err
}
s.port = port
s.port.SetReadTimeout(time.Millisecond * 2500)
return nil
}
// Send allows to send a provisioning command to a connected arduino device.
func (s *Serial) Send(cmd Command, payload []byte) error {
payload = append([]byte{byte(cmd)}, payload...)
msg := encode(Cmd, payload)
_, err := s.port.Write(msg)
if err != nil {
err = fmt.Errorf("%s: %w", "sending message through serial", err)
return err
}
return nil
}
// SendReceive allows to send a provisioning command to a connected arduino device.
// Then, it waits for a response from the device and, if any, returns it.
// If no response is received after 2 seconds, an error is returned.
func (s *Serial) SendReceive(cmd Command, payload []byte) ([]byte, error) {
err := s.Send(cmd, payload)
if err != nil {
return nil, err
}
return s.receive()
}
// Close should be used when the Serial connection isn't used anymore.
// After that, Serial could Connect again to any port.
func (s *Serial) Close() error {
return s.port.Close()
}
// receive allows to wait for a response from an arduino device under provisioning.
// Its timeout is set to 2 seconds. It returns an error if the response is not valid
// or if the timeout expires.
// TODO: consider refactoring using a more explicit procedure:
// start := s.Read(buff, MsgStartLength)
// payloadLen := s.Read(buff, payloadFieldLen)
func (s *Serial) receive() ([]byte, error) {
buff := make([]byte, 1000)
var resp []byte
received := 0
payloadLen := 0
// Wait to receive the entire packet that is long as the preamble (from msgStart to payload length field)
// plus the actual payload length plus the length of the ending sequence.
for received < (payloadLenField+payloadLenFieldLen)+payloadLen+len(msgEnd) {
n, err := s.port.Read(buff)
if err != nil {
err = fmt.Errorf("%s: %w", "receiving from serial", err)
return nil, err
}
if n == 0 {
break
}
received += n
resp = append(resp, buff[:n]...)
// Update the payload length as soon as it is received.
if payloadLen == 0 && received >= (payloadLenField+payloadLenFieldLen) {
payloadLen = int(binary.BigEndian.Uint16(resp[payloadLenField:(payloadLenField + payloadLenFieldLen)]))
// TODO: return error if payloadLen is too large.
}
}
if received == 0 {
err := errors.New("receiving from serial: timeout, nothing received")
return nil, err
}
// TODO: check if msgStart is present
if !bytes.Equal(resp[received-len(msgEnd):], msgEnd[:]) {
err := errors.New("receiving from serial: end of message (0xAA, 0x55) not found")
return nil, err
}
payload := resp[payloadField : payloadField+payloadLen-crcFieldLen]
ch := crc16.Checksum(payload, crc16.CCITTTable)
// crc is contained in the last bytes of the payload
cp := binary.BigEndian.Uint16(resp[payloadField+payloadLen-crcFieldLen : payloadField+payloadLen])
if ch != cp {
err := errors.New("receiving from serial: signature of received message is not valid")
return nil, err
}
return payload, nil
}
// encode is internally used to create a valid provisioning packet.
func encode(mType MsgType, msg []byte) []byte {
// Insert the preamble sequence followed by the message type
packet := append(msgStart[:], byte(mType))
// Append the packet length
bLen := make([]byte, payloadLenFieldLen)
binary.BigEndian.PutUint16(bLen, (uint16(len(msg) + crcFieldLen)))
packet = append(packet, bLen...)
// Append the message payload
packet = append(packet, msg...)
// Calculate and append the message signature
ch := crc16.Checksum(msg, crc16.CCITTTable)
checksum := make([]byte, crcFieldLen)
binary.BigEndian.PutUint16(checksum, ch)
packet = append(packet, checksum...)
// Append final byte sequence
packet = append(packet, msgEnd[:]...)
return packet
}