Documentation
¶
Index ¶
- Constants
- Variables
- func CRC16(data []byte) uint16
- func CRC16Recursive(byteVal byte, remainder uint16) uint16
- type Frame
- type FramingState
- type Parity
- type SerialTransportOptions
- type StopBits
- type TMMsg
- type TMTransport
- type TMType
- type Telemetry
- func (t *Telemetry) Attach(topic string, variable interface{})
- func (t *Telemetry) GetAvailableTopics() []string
- func (t *Telemetry) GetValue(topic string) interface{}
- func (t *Telemetry) PrintHashTable()
- func (t *Telemetry) Publish(topic string, msgType TMType, payload []byte) error
- func (t *Telemetry) Run(ctx context.Context) error
- func (t *Telemetry) SetMessageHandler(callback func(TMMsg))
- func (t *Telemetry) Subscribe(topic string, callback func(TMMsg))
- func (t *Telemetry) TryUpdateHashTable(msg TMMsg)
- func (t *Telemetry) UpdateTelemetry(stopChan chan struct{})
- type USBDevice
- type USBTelemetrySource
- type USBTelemetrySourceOptions
Constants ¶
const ( SOF = 0xF7 EOF = 0x7F ESC = 0x7D )
Frame handling
const ( IncomingBufferSize = 128 OutgoingBufferSize = 128 TopicBufferSize = 64 )
Telemetry Core
const ( VendorID = "0403" // Default vendor for FTDI devices ProductID = "6015" // Default product for FTDI devices )
Variables ¶
var ( MaxRetries = 1000 // Maximum number of retries for connections RetryDelay = 10 * time.Second // Delay between retry attempts )
Functions ¶
func CRC16Recursive ¶
CRC16Recursive performs the CRC-CCITT (0x1021) computation for a single byte.
Types ¶
type Frame ¶
type Frame struct {
// IncomingBuffer stores the bytes of the incoming frame.
IncomingBuffer []byte
// OutgoingBuffer stores the bytes of the outgoing frame.
OutgoingBuffer []byte
// IncomingState represents the current state of the frame parsing.
IncomingState FramingState
// OnFrame is called when a complete frame is received.
OnFrame func(data []byte)
// OnError is called when an error occurs during frame parsing.
OnError func(err error)
}
Frame represents the framing protocol for handling incoming and outgoing data.
func (*Frame) AppendByte ¶
AppendByte appends a byte to the outgoing buffer, escaping it if necessary.
func (*Frame) AppendUint16 ¶
AppendUint16 appends a 16-bit unsigned integer to the outgoing buffer, escaping it if necessary.
func (*Frame) BeginFrame ¶
func (f *Frame) BeginFrame()
BeginFrame initializes the outgoing buffer with the Start of Frame (SOF) byte.
type Parity ¶ added in v0.4.0
type Parity int
Parity selects the serial parity bit. The zero value is NoParity.
type SerialTransportOptions ¶ added in v0.4.0
type SerialTransportOptions struct {
BaudRate int
DataBits int
Parity Parity
StopBits StopBits
ReadTimeout time.Duration
WriteTimeout time.Duration
}
SerialTransportOptions configure a port without device-specific assumptions. Zero values select 9600 baud, 8N1 and one-second read/write timeouts. Timeouts must be finite and positive so Close can safely join pending I/O operations.
type StopBits ¶ added in v0.4.0
type StopBits int
StopBits selects the number of serial stop bits. The zero value is OneStopBit.
type TMTransport ¶
type TMTransport struct {
Read func([]byte) (int, error)
Write func([]byte) (int, error)
ProductID string
VendorID string
Manufacturer string
Product string
SerialNumber string
PortName string
// Close releases the transport and joins pending I/O operations.
Close func() error
}
func GetTransport ¶
func GetTransport(options ...interface{}) (*TMTransport, error)
GetTransport scans for available USB ports and returns the first one that matches the specified VendorID and ProductID. All parameters are optional: - vendorId (default: VendorID constant "0403") - productId (default: ProductID constant "6015") - maxRetries (default: MaxRetries variable) - retryDelay (default: RetryDelay variable)
func GetTransportContext ¶ added in v0.4.0
func GetTransportContext(ctx context.Context, options ...interface{}) (*TMTransport, error)
GetTransportContext performs legacy USB discovery with cancellable retries. A count of one performs a single attempt without a retry delay.
func OpenSerialTransport ¶ added in v0.4.0
func OpenSerialTransport(name string, options SerialTransportOptions) (*TMTransport, error)
OpenSerialTransport opens exactly the named port without discovery or retries. Serial ports are supported on Linux, Windows and macOS. Linux and macOS do not lock the port for exclusive use; Windows serial drivers only allow one open handle per port. Close is idempotent and joins pending I/O operations.
type Telemetry ¶
type Telemetry struct {
// Frame handles the framing protocol for incoming and outgoing messages.
Frame *Frame
// HashTable stores the values of attached variables by topic.
HashTable map[string]interface{}
// TopicCallbacks stores the callbacks for specific topics.
TopicCallbacks map[string]func(TMMsg)
// GeneralCallback is called for any received message if no specific topic callback is registered.
GeneralCallback func(TMMsg)
// Transport handles the read and write operations for the telemetry system.
Transport *TMTransport
// Mutex ensures thread-safe access to the telemetry system's data structures.
Mutex sync.Mutex
// ReceivedTopics keeps track of all received topics.
ReceivedTopics map[string]bool
// contains filtered or unexported fields
}
Telemetry represents the core telemetry system, handling frame parsing, topic-based messaging, and CRC validation.
func NewTelemetry ¶
func NewTelemetry(transport *TMTransport) *Telemetry
NewTelemetry creates a new telemetry instance with the provided transport.
func (*Telemetry) GetAvailableTopics ¶
GetAvailableTopics returns a list of all received topics.
func (*Telemetry) PrintHashTable ¶
func (t *Telemetry) PrintHashTable()
PrintHashTable prints the current values stored in the telemetry's hash table.
func (*Telemetry) Run ¶ added in v0.4.0
Run receives telemetry until cancellation or a transport error. It never discovers devices or reconnects. It owns and closes the current transport on return, including on cancellation. Close must unblock Read; without Close, reads must have a timeout so cancellation can be observed.
Only one reader may run per Telemetry. Configure Frame before starting Run; do not feed Frame concurrently with Run. Publish and subscription methods may be called concurrently. Use a new Telemetry instance for a new connection.
func (*Telemetry) SetMessageHandler ¶ added in v0.4.0
SetMessageHandler receives every valid decoded message, including unchanged values, after change subscriptions. It does not change Subscribe behavior. Handlers run synchronously outside the mutex and should return promptly.
func (*Telemetry) Subscribe ¶
Subscribe registers a callback for a specific topic. If the topic is an empty string, the callback is called for any received message.
func (*Telemetry) TryUpdateHashTable ¶
TryUpdateHashTable updates the hash table with the received message and calls the appropriate callbacks.
func (*Telemetry) UpdateTelemetry ¶
func (t *Telemetry) UpdateTelemetry(stopChan chan struct{})
UpdateTelemetry starts listening for incoming messages and processes them. New applications should use Run and own their reconnect policy.
type USBDevice ¶ added in v0.4.0
type USBDevice struct {
Name string
SerialNumber string
Product string // USB product string descriptor.
Path string
Attached time.Time // When the device appeared; zero if unknown.
}
USBDevice identifies one enumerated USB serial port: a tty on Linux, a COM port on Windows or a callout device (/dev/cu.*) on macOS. Path is a sysfs path, a device instance ID or an I/O Registry entry ID; it distinguishes device nodes reused after unplugging, except on Windows where the instance ID stays the same. SerialNumber permits port renumbering; Windows reports it upper-cased.
type USBTelemetrySource ¶ added in v0.4.0
type USBTelemetrySource struct {
// contains filtered or unexported fields
}
USBTelemetrySource watches USB serial devices and owns a single telemetry session. Start performs one discovery scan; a healthy watcher then wakes it on device changes. After errors or disconnection it also retries indefinitely with capped exponential backoff. When several devices match, the most recently attached one is used. Stop cancels and joins all its work.
Options are copied at construction. Callbacks run synchronously on the supervisor; return promptly and do not call lifecycle methods from them.
func NewUSBTelemetrySource ¶ added in v0.4.0
func NewUSBTelemetrySource(options USBTelemetrySourceOptions) *USBTelemetrySource
func (*USBTelemetrySource) IsRunning ¶ added in v0.4.0
func (s *USBTelemetrySource) IsRunning() bool
func (*USBTelemetrySource) Publish ¶ added in v0.4.2
func (s *USBTelemetrySource) Publish(ctx context.Context, msg TMMsg) error
Publish sends once on the current connected session. It never retries a command. Do not call it from supervisor callbacks. Cancellation after delivery begins cannot retract the write; a timeout must not be treated as proof of non-delivery.
func (*USBTelemetrySource) Restart ¶ added in v0.4.0
func (s *USBTelemetrySource) Restart() error
func (*USBTelemetrySource) Start ¶ added in v0.4.0
func (s *USBTelemetrySource) Start() error
func (*USBTelemetrySource) Stop ¶ added in v0.4.0
func (s *USBTelemetrySource) Stop() error
type USBTelemetrySourceOptions ¶ added in v0.4.0
type USBTelemetrySourceOptions struct {
Name string
VendorID string
ProductID string
SerialNumber string // Optional selector when adapters share VID/PID.
Product string // Optional USB product string selector; a trailing % matches a prefix.
Products []string // Alternative product selectors; a nonempty list overrides Product.
Serial SerialTransportOptions
ConnectionTimeout time.Duration // Includes opening and identifying.
ReconnectInitialDelay time.Duration
ReconnectMaxDelay time.Duration
// Handshake, when non-nil, is published after starting the session reader.
Handshake *TMMsg
// Identify returns true once this is the expected accessory, false while
// waiting for more descriptors, or an error to reject it. If nil, the first
// valid telemetry message establishes connectivity.
Identify func(TMMsg) (bool, error)
OnConnected func(USBDevice)
OnDisconnected func(error) // Also called at startup (nil) and shutdown.
OnMessage func(TMMsg) // Includes unchanged values and handshake metadata.
}
USBTelemetrySourceOptions describes an accessory without imposing a protocol handshake, project name, USB identity, or application status representation.