-
Notifications
You must be signed in to change notification settings - Fork 1
/
slipMuxReader.go
56 lines (42 loc) · 1.1 KB
/
slipMuxReader.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
package coap
import (
"io"
"strings"
"github.com/lobaro/slip"
)
var SlipMuxLogDiagnostic bool
type SlipMuxReader struct {
r *slip.SlipMuxReader
}
func NewSlipMuxReader(reader io.Reader) *SlipMuxReader {
return &SlipMuxReader{r: slip.NewSlipMuxReader(reader)}
}
type SlipMuxWriter struct {
w *slip.SlipMuxWriter
}
func NewSlipMuxWriter(writer io.Writer) *SlipMuxWriter {
return &SlipMuxWriter{w: slip.NewSlipMuxWriter(writer)}
}
func (r *SlipMuxReader) ReadPacket() ([]byte, bool, error) {
for {
packet, frame, err := r.r.ReadPacket()
if frame == slip.FRAME_DIAGNOSTIC {
if SlipMuxLogDiagnostic {
log.WithField("message", strings.TrimSpace(string(packet))).Debug("SlipMux Diagnostic")
}
continue
}
if frame == slip.FRAME_COAP {
return packet, false, err
}
if err != nil {
return packet, false, err
}
// silently ignore unhandled packets.
log.WithError(err).WithField("packet", packet).WithField("frameType", frame).Info("Unknown SlipMux packet")
continue
}
}
func (w *SlipMuxWriter) WritePacket(p []byte) (err error) {
return w.w.WritePacket(slip.FRAME_COAP, p)
}