allow to trace the serial communication with a separate writer

master
Florian Thienel 3 years ago
parent db7c48f22d
commit 2af6ef6e27

@ -13,6 +13,14 @@ const (
atSendingQueueTimeout = 500 * time.Millisecond atSendingQueueTimeout = 500 * time.Millisecond
) )
// NewWithTrace creates a new COM instance that traces all communications to a second writer.
func NewWithTrace(device io.ReadWriter, tracer io.Writer) *COM {
result := New(device)
result.tracer = tracer
return result
}
// New creates a new COM instance using the given io.ReadWriter to communicate with the radio's PEI.
func New(device io.ReadWriter) *COM { func New(device io.ReadWriter) *COM {
lines := readLoop(device) lines := readLoop(device)
commands := make(chan command) commands := make(chan command)
@ -23,8 +31,8 @@ func New(device io.ReadWriter) *COM {
} }
go func() { go func() {
// log.Print("entering COM loop") result.trace("****\n* SESSION START\n****\n")
// defer log.Print("exiting COM loop") defer result.trace("****\n* SESSION END\n****\n")
defer close(result.closed) defer close(result.closed)
var commandCancelled <-chan struct{} var commandCancelled <-chan struct{}
@ -39,7 +47,7 @@ func New(device io.ReadWriter) *COM {
if !valid { if !valid {
return return
} }
// log.Printf("rx: %s", line) result.tracef("rx: %s\nhex: %X\n--\n", line, line)
switch { switch {
case activeIndication != nil: case activeIndication != nil:
@ -78,7 +86,7 @@ func New(device io.ReadWriter) *COM {
if (lastbyte != 0x1a) && (lastbyte != 0x1b) { if (lastbyte != 0x1a) && (lastbyte != 0x1b) {
txbytes = append(txbytes, 0x0d, 0x0a) txbytes = append(txbytes, 0x0d, 0x0a)
} }
// log.Printf("tx: %v", txbytes) result.tracef("tx: %s\nhex: %X\n--\n", txbytes, txbytes)
device.Write(txbytes) device.Write(txbytes)
commandCancelled = cmd.cancelled commandCancelled = cmd.cancelled
activeCommand = &cmd activeCommand = &cmd
@ -91,9 +99,11 @@ func New(device io.ReadWriter) *COM {
return result return result
} }
// COM allows to communicate with a radio's PEI using AT commands.
type COM struct { type COM struct {
commands chan<- command commands chan<- command
closed chan struct{} closed chan struct{}
tracer io.Writer
indications map[string]indicationConfig indications map[string]indicationConfig
} }
@ -101,9 +111,6 @@ type COM struct {
func readLoop(r io.Reader) <-chan string { func readLoop(r io.Reader) <-chan string {
lines := make(chan string, 1) lines := make(chan string, 1)
go func() { go func() {
// log.Print("entering read loop")
// defer log.Print("exiting read loop")
buf := make([]byte, readBufferSize) buf := make([]byte, readBufferSize)
currentLine := make([]byte, 0, readBufferSize) currentLine := make([]byte, 0, readBufferSize)
for { for {
@ -115,7 +122,6 @@ func readLoop(r io.Reader) <-chan string {
close(lines) close(lines)
return return
} else if err != nil { } else if err != nil {
// log.Printf("read error: %v", err)
if len(currentLine) > 0 { if len(currentLine) > 0 {
lines <- string(currentLine) lines <- string(currentLine)
} }
@ -178,7 +184,6 @@ func (c *COM) ClearSyntaxErrors(ctx context.Context) error {
return nil return nil
} }
if err.Error() == "+CME ERROR: 35" { if err.Error() == "+CME ERROR: 35" {
// log.Printf(".")
time.Sleep(200) time.Sleep(200)
} else { } else {
return err return err
@ -224,6 +229,20 @@ func (c *COM) ATs(ctx context.Context, requests ...string) error {
return nil return nil
} }
func (c *COM) trace(args ...interface{}) {
if c.tracer == nil {
return
}
fmt.Fprint(c.tracer, args...)
}
func (c *COM) tracef(format string, args ...interface{}) {
if c.tracer == nil {
return
}
fmt.Fprintf(c.tracer, format, args...)
}
type indicationConfig struct { type indicationConfig struct {
prefix string prefix string
trailingLines int trailingLines int

Loading…
Cancel
Save