cacert-gosigner/internal/serial/seriallink.go

162 lines
3.3 KiB
Go
Raw Normal View History

/*
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.
*/
// Package serial provides a handler for the serial connection of the signer machine.
package serial
import (
"context"
"fmt"
"sync"
"github.com/sirupsen/logrus"
"github.com/tarm/serial"
"git.cacert.org/cacert-gosigner/internal/config"
"git.cacert.org/cacert-gosigner/pkg/protocol"
)
type Handler struct {
serverHandler protocol.ServerHandler
framer protocol.Framer
config *serial.Config
port *serial.Port
logger *logrus.Logger
framesIn chan []byte
framesOut chan []byte
}
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
}
func (h *Handler) Run(ctx context.Context) error {
const componentCount = 3
protocolErrors, framerErrors := make(chan error), make(chan error)
subCtx, cancel := context.WithCancel(ctx)
wg := sync.WaitGroup{}
wg.Add(componentCount)
defer func() {
cancel()
h.logger.Info("context canceled waiting for shutdown of components")
wg.Wait()
h.logger.Info("shutdown complete")
}()
go func() {
defer wg.Done()
err := h.framer.ReadFrames(subCtx, h.port, h.framesIn)
h.logger.Info("frame reading stopped")
select {
case framerErrors <- err:
case <-subCtx.Done():
}
}()
go func() {
defer wg.Done()
err := h.framer.WriteFrames(subCtx, h.port, h.framesOut)
h.logger.Info("frame writing stopped")
select {
case framerErrors <- err:
case <-subCtx.Done():
}
}()
go func() {
defer wg.Done()
serverProtocol := protocol.NewServer(h.serverHandler, h.framesIn, h.framesOut, h.logger)
err := serverProtocol.Handle(subCtx)
h.logger.Info("server protocol stopped")
select {
case protocolErrors <- err:
case <-subCtx.Done():
}
}()
for {
select {
case <-ctx.Done():
return nil
case err := <-framerErrors:
if err != nil {
return fmt.Errorf("error from framer: %w", err)
}
return nil
case err := <-protocolErrors:
if err != nil {
return fmt.Errorf("error from protocol handler: %w", err)
}
return nil
}
}
}
func New(
cfg *config.Serial,
logger *logrus.Logger,
framer protocol.Framer,
protocolHandler protocol.ServerHandler,
) (*Handler, error) {
h := &Handler{
serverHandler: protocolHandler,
logger: logger,
framesIn: make(chan []byte),
framesOut: make(chan []byte),
framer: framer,
}
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
}