2022-08-03 12:38:36 +00:00
|
|
|
/*
|
|
|
|
Copyright 2022 CAcert Inc.
|
|
|
|
SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-11-29 08:57:23 +00:00
|
|
|
// Package serial provides a handler for the serial connection of the signer machine.
|
2022-11-28 16:39:48 +00:00
|
|
|
package serial
|
2022-08-03 12:38:36 +00:00
|
|
|
|
|
|
|
import (
|
2022-11-21 07:26:50 +00:00
|
|
|
"context"
|
2022-08-03 12:38:36 +00:00
|
|
|
"fmt"
|
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2022-08-03 12:38:36 +00:00
|
|
|
"github.com/tarm/serial"
|
|
|
|
|
2022-11-28 16:39:48 +00:00
|
|
|
"git.cacert.org/cacert-gosigner/internal/config"
|
2022-08-03 12:38:36 +00:00
|
|
|
"git.cacert.org/cacert-gosigner/pkg/protocol"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Handler struct {
|
2022-11-29 13:05:10 +00:00
|
|
|
serverHandler protocol.ServerHandler
|
|
|
|
framer protocol.Framer
|
|
|
|
config *serial.Config
|
|
|
|
port *serial.Port
|
|
|
|
logger *logrus.Logger
|
|
|
|
framesIn chan []byte
|
|
|
|
framesOut chan []byte
|
2022-08-03 12:38:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) setupConnection() error {
|
|
|
|
s, err := serial.OpenPort(h.config)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not open serial port: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
h.port = s
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) Close() error {
|
|
|
|
err := h.port.Close()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not close serial port: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
func (h *Handler) Run(ctx context.Context) error {
|
2022-11-29 10:45:59 +00:00
|
|
|
protocolErrors, framerErrors := make(chan error), make(chan error)
|
2022-11-21 07:26:50 +00:00
|
|
|
|
|
|
|
go func() {
|
2022-11-29 08:57:23 +00:00
|
|
|
err := h.framer.ReadFrames(h.port, h.framesIn)
|
|
|
|
|
2022-11-29 10:45:59 +00:00
|
|
|
framerErrors <- err
|
2022-11-29 08:57:23 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
err := h.framer.WriteFrames(h.port, h.framesOut)
|
2022-11-21 07:26:50 +00:00
|
|
|
|
2022-11-29 10:45:59 +00:00
|
|
|
framerErrors <- err
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
2022-11-29 13:05:10 +00:00
|
|
|
serverProtocol := protocol.NewServer(h.serverHandler, h.framesIn, h.framesOut, h.logger)
|
|
|
|
|
|
|
|
err := serverProtocol.Handle()
|
2022-11-29 10:45:59 +00:00
|
|
|
|
|
|
|
protocolErrors <- err
|
2022-11-21 07:26:50 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil
|
2022-11-29 10:45:59 +00:00
|
|
|
case err := <-framerErrors:
|
2022-11-21 07:26:50 +00:00
|
|
|
if err != nil {
|
2022-11-29 10:45:59 +00:00
|
|
|
return fmt.Errorf("error from framer: %w", err)
|
2022-11-21 07:26:50 +00:00
|
|
|
}
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
return nil
|
2022-11-29 10:45:59 +00:00
|
|
|
case err := <-protocolErrors:
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error from protocol handler: %w", err)
|
2022-11-21 07:26:50 +00:00
|
|
|
}
|
2022-11-29 10:45:59 +00:00
|
|
|
|
|
|
|
return nil
|
2022-11-21 07:26:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-29 10:45:59 +00:00
|
|
|
func New(
|
|
|
|
cfg *config.Serial,
|
|
|
|
logger *logrus.Logger,
|
|
|
|
protocolHandler protocol.ServerHandler,
|
|
|
|
) (*Handler, error) {
|
2022-11-21 07:26:50 +00:00
|
|
|
h := &Handler{
|
2022-11-29 13:05:10 +00:00
|
|
|
serverHandler: protocolHandler,
|
|
|
|
logger: logger,
|
|
|
|
framesIn: make(chan []byte),
|
|
|
|
framesOut: make(chan []byte),
|
|
|
|
framer: protocol.NewCOBSFramer(logger),
|
2022-11-21 07:26:50 +00:00
|
|
|
}
|
2022-08-03 12:38:36 +00:00
|
|
|
h.config = &serial.Config{Name: cfg.Device, Baud: cfg.Baud, ReadTimeout: cfg.Timeout}
|
|
|
|
|
|
|
|
err := h.setupConnection()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return h, nil
|
|
|
|
}
|