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-12-02 11:54:07 +00:00
|
|
|
"sync"
|
2022-08-03 12:38:36 +00:00
|
|
|
|
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-12-02 11:54:07 +00:00
|
|
|
const componentCount = 3
|
|
|
|
|
2022-11-29 10:45:59 +00:00
|
|
|
protocolErrors, framerErrors := make(chan error), make(chan error)
|
2022-11-21 07:26:50 +00:00
|
|
|
|
2022-12-02 11:54:07 +00:00
|
|
|
subCtx, cancel := context.WithCancel(ctx)
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
wg.Add(componentCount)
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
cancel()
|
2022-12-03 12:15:58 +00:00
|
|
|
h.logger.Info("context canceled, waiting for shutdown of components")
|
2022-12-02 11:54:07 +00:00
|
|
|
wg.Wait()
|
|
|
|
h.logger.Info("shutdown complete")
|
|
|
|
}()
|
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
go func() {
|
2022-12-02 11:54:07 +00:00
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
err := h.framer.ReadFrames(subCtx, h.port, h.framesIn)
|
2022-11-29 08:57:23 +00:00
|
|
|
|
2022-12-02 11:54:07 +00:00
|
|
|
h.logger.Info("frame reading stopped")
|
|
|
|
|
|
|
|
select {
|
|
|
|
case framerErrors <- err:
|
|
|
|
case <-subCtx.Done():
|
|
|
|
}
|
2022-11-29 08:57:23 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
2022-12-02 11:54:07 +00:00
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
err := h.framer.WriteFrames(subCtx, h.port, h.framesOut)
|
|
|
|
|
|
|
|
h.logger.Info("frame writing stopped")
|
2022-11-21 07:26:50 +00:00
|
|
|
|
2022-12-02 11:54:07 +00:00
|
|
|
select {
|
|
|
|
case framerErrors <- err:
|
|
|
|
case <-subCtx.Done():
|
|
|
|
}
|
2022-11-29 10:45:59 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
2022-12-02 11:54:07 +00:00
|
|
|
defer wg.Done()
|
|
|
|
|
2022-11-29 13:05:10 +00:00
|
|
|
serverProtocol := protocol.NewServer(h.serverHandler, h.framesIn, h.framesOut, h.logger)
|
|
|
|
|
2022-12-02 11:54:07 +00:00
|
|
|
err := serverProtocol.Handle(subCtx)
|
2022-11-29 10:45:59 +00:00
|
|
|
|
2022-12-02 11:54:07 +00:00
|
|
|
h.logger.Info("server protocol stopped")
|
|
|
|
|
|
|
|
select {
|
|
|
|
case protocolErrors <- err:
|
|
|
|
case <-subCtx.Done():
|
|
|
|
}
|
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,
|
2022-12-02 11:54:07 +00:00
|
|
|
framer protocol.Framer,
|
2022-11-29 10:45:59 +00:00
|
|
|
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),
|
2022-12-02 11:54:07 +00:00
|
|
|
framer: framer,
|
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}
|
|
|
|
|
2022-12-02 11:54:07 +00:00
|
|
|
err := h.setupConnection()
|
2022-08-03 12:38:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return h, nil
|
|
|
|
}
|