hal

package
v0.0.22 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 29, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package hal provides hardware access (BLE, serial, camera) via a platform-specific backend. On Linux/Windows the backend is compiled in. On macOS it loads a Swift dylib via purego. On Android it uses gomobile.

Address conventions:

  • BLE addresses are normalized to lowercase at the HAL boundary. The format is platform-dependent: colon-separated MAC on Linux and Android (e.g. "aa:bb:cc:dd:ee:ff"), UUID on macOS.
  • Platform backends that require a different case (e.g. Android needs uppercase MACs) must convert internally.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func OpenSerial

func OpenSerial(path string, baudRate int) (io.ReadWriteCloser, error)

OpenSerial opens a serial port and returns an io.ReadWriteCloser.

func WatchBLE

func WatchBLE(serviceUUIDs []string, cb func(devices []BLEDevice)) (stop func())

WatchBLE starts BLE scanning. The callback receives periodic snapshots.

func WatchSerial

func WatchSerial(cb func(ports []SerialPort)) (stop func())

WatchSerial starts serial port discovery. The callback receives full snapshots whenever the port list changes. Returns a stop function.

Types

type BLEConnection

type BLEConnection interface {
	WriteCharacteristic(uuid string, data []byte) error
	ReadCharacteristic(uuid string) ([]byte, error)
	Subscribe(uuid string) (<-chan []byte, error)
	Unsubscribe(uuid string) error
	Disconnected() <-chan struct{}
	io.Closer
}

BLEConnection exposes GATT semantics for BLE devices.

func ConnectBLE

func ConnectBLE(address string) (BLEConnection, error)

ConnectBLE establishes a GATT connection and returns a BLEConnection.

type BLEDevice

type BLEDevice struct {
	Address      string
	Name         string
	ServiceUUIDs []string
	RSSI         int
}

BLEDevice describes a discovered BLE peripheral.

type CameraDevice

type CameraDevice struct {
	ID     string
	Name   string
	Facing string
}

CameraDevice describes a discovered camera.

type GATTService

type GATTService struct {
	UUID                string
	CharacteristicUUIDs []string
}

GATTService describes a discovered GATT service.

type Platform

type Platform struct {
	// Serial discovery. Callback receives JSON-encoded []SerialPort snapshots.
	// Returns a stop function.
	SerialWatch func(cb func(ports []SerialPort)) (stop func())

	// Serial I/O. Handle-based: Open returns a handle, Read/Write/Close use it.
	SerialOpen func(path string, baudRate int) (handle int64, err error)
	// SerialRead returns up to maxLen bytes. Some platforms (Android) may
	// return more than maxLen; the caller (serialHandle) buffers the excess.
	SerialRead  func(handle int64, maxLen int) ([]byte, error)
	SerialWrite func(handle int64, data []byte) (int, error)
	SerialClose func(handle int64) error

	// BLE discovery. Callback receives periodic snapshots of visible devices.
	BLEWatch func(serviceUUIDs []string, cb func(devices []BLEDevice)) (stop func())

	// BLE connection. Handle-based.
	BLEConnect     func(address string) (handle int64, err error)
	BLEDisconnect  func(handle int64) error
	BLERead        func(handle int64, charUUID string) ([]byte, error)
	BLEWrite       func(handle int64, charUUID string, data []byte) error
	BLESubscribe   func(handle int64, charUUID string, cb func(data []byte)) error
	BLEUnsubscribe func(handle int64, charUUID string) error

	// BLE service discovery (called after Connect, returns discovered services).
	BLEServices func(handle int64) ([]GATTService, error)

	// BLE disconnect notification. Registers a callback that fires when
	// a connected peripheral disconnects unexpectedly (remote-initiated).
	BLEOnDisconnect func(handle int64, cb func())

	// ReadSensors returns a snapshot of all available no-permission sensors.
	ReadSensors func() []SensorReading
}

Platform defines the set of hardware access functions provided by each OS. Each platform (native, darwin/purego, android/gomobile) fills in this struct. Functions left nil are treated as unsupported on that platform.

var P Platform

P is the active platform implementation. Set by platform-specific init().

type SensorReading

type SensorReading struct {
	ID    uint32
	Label string
	Kind  int32
	Unit  int32
	Value float64
}

SensorReading is a single sensor measurement from the local hardware. Kind and Unit values correspond to the MetricKind / MetricUnit proto enums.

func ReadSensors

func ReadSensors() []SensorReading

ReadSensors returns a snapshot of all available no-permission sensors. Returns nil if the platform does not support sensor reading.

type SerialPort

type SerialPort struct {
	// Key is the platform-provided stable identifier for this device.
	// When set, it is used as the entity key instead of deriving one
	// from VendorID/SerialNumber/Name. The platform should set this
	// to whatever identifier remains constant across permission
	// changes and USB re-enumeration.
	Key              string
	Path             string
	StablePath       string
	Name             string
	VendorID         uint32
	ProductID        uint32
	SerialNumber     string
	ManufacturerName string
	ProductName      string
}

SerialPort describes a discovered serial port.

type StreamAdapter

type StreamAdapter struct {
	// contains filtered or unexported fields
}

StreamAdapter flattens a BLEConnection into an io.ReadWriteCloser using fixed characteristic UUIDs for read (subscribe) and write.

func NewStreamAdapter

func NewStreamAdapter(conn BLEConnection, writeUUID, readUUID string) (*StreamAdapter, error)

NewStreamAdapter subscribes to readUUID for incoming data and writes to writeUUID.

func (*StreamAdapter) Close

func (s *StreamAdapter) Close() error

func (*StreamAdapter) Read

func (s *StreamAdapter) Read(p []byte) (int, error)

func (*StreamAdapter) Write

func (s *StreamAdapter) Write(p []byte) (int, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL