Use correct timeouts and log methods
This commit is contained in:
parent
72e921ec1b
commit
3a2578ae55
1 changed files with 29 additions and 19 deletions
|
@ -24,6 +24,11 @@ func main() {
|
||||||
flag.IntVar(&baudrate, "b", 115200, "baud rate")
|
flag.IntVar(&baudrate, "b", 115200, "baud rate")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
|
log.SetFormatter(&log.TextFormatter{
|
||||||
|
DisableColors: true,
|
||||||
|
FullTimestamp: true,
|
||||||
|
})
|
||||||
|
|
||||||
serialMode := &serial.Mode{
|
serialMode := &serial.Mode{
|
||||||
BaudRate: baudrate,
|
BaudRate: baudrate,
|
||||||
DataBits: 8,
|
DataBits: 8,
|
||||||
|
@ -33,18 +38,18 @@ func main() {
|
||||||
log.Infof("connecting to %s using %+v", address, serialMode)
|
log.Infof("connecting to %s using %+v", address, serialMode)
|
||||||
port, err := serial.Open(address, serialMode)
|
port, err := serial.Open(address, serialMode)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatalf("could not open serial port: %v", err)
|
||||||
}
|
}
|
||||||
log.Println("connected")
|
log.Info("connected")
|
||||||
|
|
||||||
count := 0
|
count := 0
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
err := port.Close()
|
err := port.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatalf("could not close port: %v", err)
|
||||||
}
|
}
|
||||||
log.Println("closed")
|
log.Info("closed")
|
||||||
}()
|
}()
|
||||||
|
|
||||||
readLoop:
|
readLoop:
|
||||||
|
@ -61,19 +66,19 @@ readLoop:
|
||||||
case command := <-commandChan:
|
case command := <-commandChan:
|
||||||
response, err := Process(command)
|
response, err := Process(command)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("ERROR %v\n", err)
|
log.Errorf("error processing command: %v", err)
|
||||||
} else {
|
break readLoop
|
||||||
_ = SendResponse(&readWriteCloser, response)
|
|
||||||
}
|
}
|
||||||
|
_ = SendResponse(&readWriteCloser, response)
|
||||||
case <-timeout:
|
case <-timeout:
|
||||||
log.Println("timeout in main loop")
|
log.Error("timeout in main loop")
|
||||||
break readLoop
|
break readLoop
|
||||||
case err := <-errChan:
|
case err := <-errChan:
|
||||||
log.Printf("ERROR %v\n", err)
|
log.Errorf("error from io goroutine %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
count++
|
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
|
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
|
return err
|
||||||
} else if ack[0] != 0x10 {
|
} else if ack[0] != 0x10 {
|
||||||
return errors.New(fmt.Sprintf("invalid ack byte 0x%02x", ack[0]))
|
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
|
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
|
return err
|
||||||
} else if ack[0] == 0x10 {
|
} else if ack[0] == 0x10 {
|
||||||
tryAgain = false
|
tryAgain = false
|
||||||
|
@ -119,14 +124,18 @@ func SendResponse(port *io.ReadWriteCloser, response *datastructures.SignerRespo
|
||||||
|
|
||||||
// Process the signer request
|
// Process the signer request
|
||||||
func Process(command datastructures.SignerRequest) (response *datastructures.SignerResponse, err error) {
|
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 {
|
switch command.Action {
|
||||||
case datastructures.ActionNul:
|
case datastructures.ActionNul:
|
||||||
response, err = handleNulAction(command)
|
response, err = handleNulAction(command)
|
||||||
return
|
return
|
||||||
default:
|
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
|
// Receive a request and generate a request data structure
|
||||||
func Receive(port *io.ReadWriteCloser, commandChan *chan datastructures.SignerRequest, errorChan *chan error) {
|
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 {
|
if err != nil {
|
||||||
*errorChan <- err
|
*errorChan <- err
|
||||||
return
|
return
|
||||||
|
@ -153,19 +162,19 @@ func Receive(port *io.ReadWriteCloser, commandChan *chan datastructures.SignerRe
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
lengthBytes, err := shared.ReceiveBytes(port, 3, 2)
|
lengthBytes, err := shared.ReceiveBytes(port, 3, 2*time.Second)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
*errorChan <- err
|
*errorChan <- err
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
blockLength := datastructures.Decode24BitLength(lengthBytes)
|
blockLength := datastructures.Decode24BitLength(lengthBytes)
|
||||||
blockData, err := shared.ReceiveBytes(port, blockLength, 5)
|
blockData, err := shared.ReceiveBytes(port, blockLength, 5*time.Second)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
*errorChan <- err
|
*errorChan <- err
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
checkSum, err := shared.ReceiveBytes(port, 1, 2)
|
checkSum, err := shared.ReceiveBytes(port, 1, 2*time.Second)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
*errorChan <- err
|
*errorChan <- err
|
||||||
return
|
return
|
||||||
|
@ -174,9 +183,10 @@ func Receive(port *io.ReadWriteCloser, commandChan *chan datastructures.SignerRe
|
||||||
command, err := datastructures.SignerRequestFromData(lengthBytes, blockData, checkSum[0])
|
command, err := datastructures.SignerRequestFromData(lengthBytes, blockData, checkSum[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
*errorChan <- err
|
*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 {
|
if err != nil {
|
||||||
*errorChan <- err
|
*errorChan <- err
|
||||||
return
|
return
|
||||||
|
|
Loading…
Reference in a new issue