| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- //go:build linux
- package thermaltransport
- import (
- "context"
- "errors"
- "fmt"
- "io"
- "os"
- "strings"
- "sync"
- "time"
- "golang.org/x/sys/unix"
- "hth/internal/protocol6400"
- "hth/internal/serialtransport"
- )
- type Client struct {
- mu sync.Mutex
- file *os.File
- path string
- profile string
- timeout time.Duration
- retries int
- parser *protocol6400.StreamParser
- locked bool
- }
- func Open(path, profile string, timeout time.Duration, retries int) (*Client, error) {
- file, err := os.OpenFile(path, os.O_RDWR|unix.O_NOCTTY|unix.O_CLOEXEC, 0)
- if err != nil {
- return nil, err
- }
- locked := false
- if !strings.HasPrefix(path, "/dev/pts/") {
- if err := serialtransport.FlockExclusive(int(file.Fd())); err != nil {
- file.Close()
- return nil, fmt.Errorf("exclusive serial ownership: %w", err)
- }
- locked = true
- if err := serialtransport.ConfigureBaud8N1(int(file.Fd()), protocol6400.HostBaud); err != nil {
- serialtransport.Funlock(int(file.Fd()))
- file.Close()
- return nil, err
- }
- } else if err := serialtransport.ConfigurePTYRaw(int(file.Fd())); err != nil {
- file.Close()
- return nil, err
- }
- return &Client{file: file, path: path, profile: profile, timeout: timeout, retries: retries, parser: protocol6400.NewStreamParser(10 * time.Millisecond), locked: locked}, nil
- }
- func (c *Client) Close() error {
- c.mu.Lock()
- defer c.mu.Unlock()
- if c.file == nil {
- return nil
- }
- if c.locked {
- _ = serialtransport.Funlock(int(c.file.Fd()))
- }
- err := c.file.Close()
- c.file = nil
- return err
- }
- func (c *Client) transactSequenceLocked(ctx context.Context, request []byte, responseTypes []byte) ([]protocol6400.Frame, error) {
- if c.file == nil {
- return nil, os.ErrClosed
- }
- if len(responseTypes) == 0 {
- return nil, errors.New("thermal transport: empty response sequence")
- }
- deadline := time.Now().Add(c.timeout)
- if value, ok := ctx.Deadline(); ok && value.Before(deadline) {
- deadline = value
- }
- _ = c.file.SetDeadline(deadline)
- c.parser.Reset()
- written, err := c.file.Write(request)
- if err != nil {
- return nil, err
- }
- if written != len(request) {
- return nil, io.ErrShortWrite
- }
- buffer := make([]byte, 256)
- frames := make([]protocol6400.Frame, 0, len(responseTypes))
- for {
- if err := ctx.Err(); err != nil {
- return nil, err
- }
- n, err := c.file.Read(buffer)
- now := time.Now()
- if n > 0 {
- for _, item := range c.parser.Push(buffer[:n], now) {
- if item.Err != nil {
- return nil, item.Err
- }
- if len(frames) >= len(responseTypes) || item.Frame.Type != responseTypes[len(frames)] {
- return nil, fmt.Errorf(
- "unexpected response 0x%02x at stage %d", item.Frame.Type, len(frames),
- )
- }
- frames = append(frames, item.Frame)
- }
- if len(frames) == len(responseTypes) {
- return frames, nil
- }
- }
- if err != nil {
- if errors.Is(err, os.ErrDeadlineExceeded) || errors.Is(err, context.DeadlineExceeded) {
- return nil, context.DeadlineExceeded
- }
- if errors.Is(err, io.EOF) && strings.HasPrefix(c.path, "/dev/pts/") {
- continue
- }
- return nil, err
- }
- }
- }
- func (c *Client) transactSequence(ctx context.Context, request []byte, responseTypes ...byte) ([]protocol6400.Frame, error) {
- c.mu.Lock()
- defer c.mu.Unlock()
- var last error
- for attempt := 0; attempt <= c.retries; attempt++ {
- frames, err := c.transactSequenceLocked(ctx, request, responseTypes)
- if err == nil {
- return frames, nil
- }
- last = err
- if ctx.Err() != nil {
- break
- }
- }
- return nil, last
- }
- func (c *Client) transact(ctx context.Context, request []byte, responseType byte) (protocol6400.Frame, error) {
- frames, err := c.transactSequence(ctx, request, responseType)
- if err != nil {
- return protocol6400.Frame{}, err
- }
- return frames[0], nil
- }
- func (c *Client) SetTarget(ctx context.Context, target byte) error {
- request, err := protocol6400.SetTargetRequest(target)
- if err != nil {
- return err
- }
- _, err = c.transact(ctx, request, protocol6400.RspSetTargetAck)
- return err
- }
- func (c *Client) Stop(ctx context.Context) error { return c.SetTarget(ctx, 0) }
- func (c *Client) GetSnapshot(ctx context.Context) (protocol6400.Snapshot, []byte, error) {
- frames, err := c.transactSequence(
- ctx,
- protocol6400.GetSnapshotRequest(),
- protocol6400.RspGetSnapshotAck,
- protocol6400.RspSnapshotData,
- )
- if err != nil {
- return protocol6400.Snapshot{}, nil, err
- }
- frame := frames[1]
- value, err := protocol6400.DecodeSnapshot(frame.Payload)
- return value, frame.Payload, err
- }
- func (c *Client) RunStimulus(ctx context.Context, value protocol6400.Stimulus) error {
- request, err := protocol6400.StimulusRequest(c.profile, value)
- if err != nil {
- return err
- }
- _, err = c.transact(ctx, request, protocol6400.RspStimulusAck)
- return err
- }
|