Use correct timeouts and log methods

This commit is contained in:
Jan Dittberner 2021-01-04 08:15:49 +01:00
parent 72e921ec1b
commit 3a2578ae55

View file

@ -24,6 +24,11 @@ func main() {
flag.IntVar(&baudrate, "b", 115200, "baud rate")
flag.Parse()
log.SetFormatter(&log.TextFormatter{
DisableColors: true,
FullTimestamp: true,
})
serialMode := &serial.Mode{
BaudRate: baudrate,
DataBits: 8,
@ -33,18 +38,18 @@ func main() {
log.Infof("connecting to %s using %+v", address, serialMode)
port, err := serial.Open(address, serialMode)
if err != nil {
log.Fatal(err)
log.Fatalf("could not open serial port: %v", err)
}
log.Println("connected")
log.Info("connected")
count := 0
defer func() {
err := port.Close()
if err != nil {
log.Fatal(err)
log.Fatalf("could not close port: %v", err)
}
log.Println("closed")
log.Info("closed")
}()
readLoop:
@ -61,19 +66,19 @@ readLoop:
case command := <-commandChan:
response, err := Process(command)
if err != nil {
log.Printf("ERROR %v\n", err)
} else {
_ = SendResponse(&readWriteCloser, response)
log.Errorf("error processing command: %v", err)
break readLoop
}
_ = SendResponse(&readWriteCloser, response)
case <-timeout:
log.Println("timeout in main loop")
log.Error("timeout in main loop")
break readLoop
case err := <-errChan:
log.Printf("ERROR %v\n", err)
log.Errorf("error from io goroutine %v", err)
}
count++
log.Printf("INFO %d requests processed. Waiting for next request ...\n", count)
log.Infof("%d requests processed. Waiting for next request ...", count)
}
}
@ -83,7 +88,7 @@ func SendResponse(port *io.ReadWriteCloser, response *datastructures.SignerRespo
return err
}
if ack, err := shared.ReceiveBytes(port, 1, 5); err != nil {
if ack, err := shared.ReceiveBytes(port, 1, 5*time.Second); err != nil {
return err
} else if ack[0] != 0x10 {
return errors.New(fmt.Sprintf("invalid ack byte 0x%02x", ack[0]))
@ -105,7 +110,7 @@ func SendResponse(port *io.ReadWriteCloser, response *datastructures.SignerRespo
return err
}
if ack, err := shared.ReceiveBytes(port, 1, 5); err != nil {
if ack, err := shared.ReceiveBytes(port, 1, 5*time.Second); err != nil {
return err
} else if ack[0] == 0x10 {
tryAgain = false
@ -119,14 +124,18 @@ func SendResponse(port *io.ReadWriteCloser, response *datastructures.SignerRespo
// Process the signer request
func Process(command datastructures.SignerRequest) (response *datastructures.SignerResponse, err error) {
log.Printf("INFO analyze %+v\n", command)
log.Infof("analyze %+v", command)
switch command.Action {
case datastructures.ActionNul:
response, err = handleNulAction(command)
return
default:
return nil, errors.New(fmt.Sprintf("unsupported Action 0x%02x", command.Action))
return nil, errors.New(fmt.Sprintf(
"unsupported Action 0x%02x %s",
int(command.Action),
command.Action,
))
}
}
@ -139,7 +148,7 @@ func handleNulAction(command datastructures.SignerRequest) (*datastructures.Sign
// Receive a request and generate a request data structure
func Receive(port *io.ReadWriteCloser, commandChan *chan datastructures.SignerRequest, errorChan *chan error) {
header, err := shared.ReceiveBytes(port, 1, 20)
header, err := shared.ReceiveBytes(port, 1, 20*time.Second)
if err != nil {
*errorChan <- err
return
@ -153,19 +162,19 @@ func Receive(port *io.ReadWriteCloser, commandChan *chan datastructures.SignerRe
return
}
lengthBytes, err := shared.ReceiveBytes(port, 3, 2)
lengthBytes, err := shared.ReceiveBytes(port, 3, 2*time.Second)
if err != nil {
*errorChan <- err
return
}
blockLength := datastructures.Decode24BitLength(lengthBytes)
blockData, err := shared.ReceiveBytes(port, blockLength, 5)
blockData, err := shared.ReceiveBytes(port, blockLength, 5*time.Second)
if err != nil {
*errorChan <- err
return
}
checkSum, err := shared.ReceiveBytes(port, 1, 2)
checkSum, err := shared.ReceiveBytes(port, 1, 2*time.Second)
if err != nil {
*errorChan <- err
return
@ -174,9 +183,10 @@ func Receive(port *io.ReadWriteCloser, commandChan *chan datastructures.SignerRe
command, err := datastructures.SignerRequestFromData(lengthBytes, blockData, checkSum[0])
if err != nil {
*errorChan <- err
return
}
trailer, err := shared.ReceiveBytes(port, len(shared.MagicTrailer), 2)
trailer, err := shared.ReceiveBytes(port, len(shared.MagicTrailer), 2*time.Second)
if err != nil {
*errorChan <- err
return