device

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package device manages KDE Connect devices, their connections, state, and routing.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ReconnectBackoff

func ReconnectBackoff(attempt int, maxDuration time.Duration) time.Duration

ReconnectBackoff calculates exponential backoff duration based on attempts. Caps out at maxDuration.

func SaveDevices

func SaveDevices(path string, devices []DeviceInfo) error

SaveDevices writes the list of known devices to the given JSON file path.

Types

type Device

type Device struct {
	Type string

	IncomingCaps []string
	OutgoingCaps []string

	CertFP string

	BatteryCharge int
	IsCharging    bool
	// contains filtered or unexported fields
}

Device represents an active KDE Connect remote device.

func NewDevice

func NewDevice(id, name, dtype string, logger *zap.Logger) *Device

NewDevice creates a new disconnected device instance.

func (*Device) Connect

func (d *Device) Connect(ctx context.Context, conn *transport.Conn, dispatch func(context.Context, *Device, *protocol.Packet), onConnect func(*Device), onDisconnect func(*Device))

Connect establishes a connection for the device and starts the reader and writer loops.

func (*Device) Disconnect

func (d *Device) Disconnect()

Disconnect terminates the connection and stops the loops.

func (*Device) GetBattery

func (d *Device) GetBattery() (int, bool)

GetBattery returns the current battery state of the device.

func (*Device) HasCapability

func (d *Device) HasCapability(cap string) bool

HasCapability checks if the device has a particular capability (incoming or outgoing).

func (*Device) ID

func (d *Device) ID() string

func (*Device) IsConnected

func (d *Device) IsConnected() bool

IsConnected returns whether the device currently has an active connection.

func (*Device) LastSeen

func (d *Device) LastSeen() time.Time

func (*Device) Name

func (d *Device) Name() string

func (*Device) PeerCert

func (d *Device) PeerCert() *x509.Certificate

PeerCert returns the validated certificate presented by the remote device. Returns nil if not connected or no certificate was presented.

func (*Device) RemoteIP

func (d *Device) RemoteIP() net.IP

RemoteIP returns the IP address of the connected peer, if available.

func (*Device) Send

func (d *Device) Send(p *protocol.Packet) error

Send attempts to enqueue a packet for delivery to the device. If the send channel is full, the packet is dropped to avoid blocking.

func (*Device) SetBus

func (d *Device) SetBus(bus *events.Bus)

SetBus sets the event bus for the device.

func (*Device) SetLastSeen

func (d *Device) SetLastSeen(t time.Time)

func (*Device) SetName

func (d *Device) SetName(n string)

func (*Device) SetState

func (d *Device) SetState(s PairingState)

func (*Device) State

func (d *Device) State() PairingState

func (*Device) UpdateBattery

func (d *Device) UpdateBattery(charge int, charging bool)

UpdateBattery updates the battery state of the device.

type DeviceInfo

type DeviceInfo struct {
	ID        string       `json:"id"`
	Name      string       `json:"name"`
	Type      string       `json:"type"`
	State     PairingState `json:"state"`
	CertFP    string       `json:"cert_fp"`
	LastSeen  time.Time    `json:"last_seen"`
	Connected bool         `json:"connected"`
}

DeviceInfo represents the persistent state of a device.

func LoadDevices

func LoadDevices(path string) ([]DeviceInfo, error)

LoadDevices loads known devices from the given JSON file path.

type PairingState

type PairingState int

PairingState represents the current pairing status with a device.

const (
	StateUnknown             PairingState = iota
	StatePairRequested                    // We sent a pair request, waiting for response
	StatePairRequestedByPeer              // Peer sent a pair request, waiting for user to accept
	StatePaired
	StateUnpaired
)

func (PairingState) String

func (s PairingState) String() string

type Registry

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

Registry manages the set of known devices in memory.

func NewRegistry

func NewRegistry(bus *events.Bus) *Registry

NewRegistry creates a new empty device registry.

func (*Registry) Add

func (r *Registry) Add(d *Device)

Add inserts or replaces a device in the registry.

func (*Registry) AllPairedDevicesConnected

func (r *Registry) AllPairedDevicesConnected() bool

AllPairedDevicesConnected returns true if there is at least one paired device and all paired devices are currently connected.

func (*Registry) Connected

func (r *Registry) Connected() []*Device

Connected returns only devices that currently have an active connection.

func (*Registry) Get

func (r *Registry) Get(id string) (*Device, bool)

Get finds a device by its ID.

func (*Registry) List

func (r *Registry) List() []*Device

List returns all devices currently in the registry.

func (*Registry) Remove

func (r *Registry) Remove(id string)

Remove deletes a device from the registry.

type Sender

type Sender interface {
	ID() string
	Name() string
	SetName(name string)
	State() PairingState
	SetState(state PairingState)

	Send(p *protocol.Packet) error
	IsConnected() bool
	RemoteIP() net.IP
	PeerCert() *x509.Certificate

	HasCapability(cap string) bool

	UpdateBattery(charge int, charging bool)
	GetBattery() (int, bool)
}

Sender represents a KDE Connect device capable of receiving packets. This interface allows plugins to interact with a device without knowing its internal implementation details.

Jump to

Keyboard shortcuts

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