discovery

package
v0.0.0-...-02ded7e Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Package discovery implements DNS-SD (mDNS) discovery for Matter nodes.

This package provides:

  • Service advertising for commissionable, operational, and commissioner nodes
  • Service resolution to discover other Matter nodes on the network
  • TXT record encoding/decoding for Matter-specific attributes

Spec References:

  • Section 4.3: Discovery
  • Section 4.3.1: Commissionable Node Discovery (_matterc._udp)
  • Section 4.3.2: Operational Discovery (_matter._tcp)
  • Section 4.3.3: Commissioner Discovery (_matterd._udp)

Index

Constants

View Source
const (
	// ServiceCommissionable is the DNS-SD service type for commissionable nodes.
	ServiceCommissionable = "_matterc._udp"

	// ServiceOperational is the DNS-SD service type for operational nodes.
	ServiceOperational = "_matter._tcp"

	// ServiceCommissioner is the DNS-SD service type for commissioners.
	ServiceCommissioner = "_matterd._udp"

	// DefaultDomain is the default mDNS domain.
	DefaultDomain = "local."
)

DNS-SD service type strings.

View Source
const (
	// TXTKeyDiscriminator is the discriminator key (12-bit value).
	TXTKeyDiscriminator = "D"

	// TXTKeyCommissioningMode is the commissioning mode key.
	TXTKeyCommissioningMode = "CM"

	// TXTKeyVendorProduct is the vendor/product ID key (format: "VID+PID").
	TXTKeyVendorProduct = "VP"

	// TXTKeyDeviceType is the device type key.
	TXTKeyDeviceType = "DT"

	// TXTKeyDeviceName is the device name key (max 32 chars).
	TXTKeyDeviceName = "DN"

	// TXTKeyIdleInterval is the sleepy idle interval key (milliseconds).
	TXTKeyIdleInterval = "SII"

	// TXTKeyActiveInterval is the sleepy active interval key (milliseconds).
	TXTKeyActiveInterval = "SAI"

	// TXTKeyTCPSupported indicates TCP support.
	TXTKeyTCPSupported = "T"

	// TXTKeyICDMode is the ICD operating mode key.
	TXTKeyICDMode = "ICD"

	// TXTKeyPairingHint is the pairing hint bitmap.
	TXTKeyPairingHint = "PH"

	// TXTKeyPairingInstructions is the pairing instructions key.
	TXTKeyPairingInstructions = "PI"

	// TXTKeyCommissionerPasscode indicates commissioner passcode support.
	TXTKeyCommissionerPasscode = "CP"

	// TXTKeyJointFabric indicates Joint Fabric capabilities.
	TXTKeyJointFabric = "JF"
)

TXT record key constants from Spec Section 4.3.1.4 and 4.3.2.5.

View Source
const CommissioningModeSubtype = "_CM"

CommissioningModeSubtype returns the subtype filter for nodes in commissioning mode.

View Source
const DefaultBrowseTimeout = 10 * time.Second

DefaultBrowseTimeout is the default timeout for browse operations.

View Source
const DefaultLookupTimeout = 5 * time.Second

DefaultLookupTimeout is the default timeout for lookup operations.

View Source
const DefaultPort = 5540

DefaultPort is the default Matter port.

View Source
const MaxDeviceNameLength = 32

MaxDeviceNameLength is the maximum length of the device name. Spec Section 4.3.1.9

View Source
const MaxDiscriminator = 0xFFF

MaxDiscriminator is the maximum valid discriminator value (12 bits).

Variables

View Source
var (
	// ErrClosed is returned when an operation is attempted on a closed component.
	ErrClosed = errors.New("discovery: closed")

	// ErrAlreadyStarted is returned when starting an already-started service.
	ErrAlreadyStarted = errors.New("discovery: already started")

	// ErrNotStarted is returned when stopping a service that was not started.
	ErrNotStarted = errors.New("discovery: not started")

	// ErrInvalidServiceType is returned for invalid or unknown service types.
	ErrInvalidServiceType = errors.New("discovery: invalid service type")

	// ErrInvalidDiscriminator is returned when the discriminator is out of range.
	// Valid range: 0-4095 (12 bits).
	ErrInvalidDiscriminator = errors.New("discovery: invalid discriminator (must be 0-4095)")

	// ErrInvalidDeviceName is returned when the device name exceeds the maximum length.
	// Maximum length: 32 characters.
	ErrInvalidDeviceName = errors.New("discovery: invalid device name (max 32 characters)")

	// ErrInvalidHostName is returned when the host name is empty or invalid.
	ErrInvalidHostName = errors.New("discovery: invalid host name")

	// ErrInvalidPort is returned when the port number is out of range.
	ErrInvalidPort = errors.New("discovery: invalid port (must be 1-65535)")

	// ErrNoAddresses is returned when no IP addresses are provided for advertising.
	ErrNoAddresses = errors.New("discovery: no IP addresses provided")

	// ErrServiceNotFound is returned when a requested service is not found.
	ErrServiceNotFound = errors.New("discovery: service not found")

	// ErrTimeout is returned when an operation times out.
	ErrTimeout = errors.New("discovery: operation timed out")

	// ErrInvalidInstanceName is returned when the instance name format is invalid.
	ErrInvalidInstanceName = errors.New("discovery: invalid instance name format")

	// ErrInvalidTXTRecord is returned when a TXT record has invalid format.
	ErrInvalidTXTRecord = errors.New("discovery: invalid TXT record format")
)

Package-level sentinel errors for discovery operations.

Functions

func DeviceTypeSubtype

func DeviceTypeSubtype(deviceType uint32) string

DeviceTypeSubtype returns the subtype filter for device type. Format: "_T<value>"

func FilterIPv4

func FilterIPv4(ips []net.IP) []net.IP

FilterIPv4 returns only IPv4 addresses from the slice.

func FilterIPv6

func FilterIPv6(ips []net.IP) []net.IP

FilterIPv6 returns only IPv6 addresses from the slice.

func GenerateCommissionableInstanceName

func GenerateCommissionableInstanceName() string

GenerateCommissionableInstanceName generates a random 64-bit instance name for commissionable node discovery. Format: 16 uppercase hex characters. Spec Section 4.3.1

func GetLocalAddresses

func GetLocalAddresses() ([]net.IP, error)

GetLocalAddresses returns all non-loopback IP addresses on the host.

func GetLocalIPv6Addresses

func GetLocalIPv6Addresses() ([]net.IP, error)

GetLocalIPv6Addresses returns all non-loopback IPv6 addresses on the host.

func LongDiscriminatorSubtype

func LongDiscriminatorSubtype(discriminator uint16) string

LongDiscriminatorSubtype returns the subtype filter for long discriminator. Format: "_L<value>"

func OperationalInstanceName

func OperationalInstanceName(compressedFabricID [8]byte, nodeID fabric.NodeID) string

OperationalInstanceName constructs the DNS-SD instance name for operational discovery. Format: "<CompressedFabricID>-<NodeID>" where each is 16 uppercase hex characters. Spec Section 4.3.2.1

func OperationalInstanceNameFromBytes

func OperationalInstanceNameFromBytes(compressedFabricID []byte, nodeID uint64) string

OperationalInstanceNameFromBytes creates an operational instance name from raw bytes.

func ParseOperationalInstanceName

func ParseOperationalInstanceName(instanceName string) ([8]byte, fabric.NodeID, error)

ParseOperationalInstanceName parses the instance name into compressed fabric ID and node ID. Returns an error if the format is invalid. Format must be exactly: 16 hex chars + "-" + 16 hex chars (33 chars total).

func ParseTXT

func ParseTXT(records []string) map[string]string

ParseTXT parses raw TXT record strings into a map.

func ShortDiscriminatorSubtype

func ShortDiscriminatorSubtype(shortDiscriminator uint8) string

ShortDiscriminatorSubtype returns the subtype filter for short discriminator. Format: "_S<value>"

func SortIPsByPreference

func SortIPsByPreference(ips []net.IP) []net.IP

SortIPsByPreference sorts IP addresses by preference per Spec 4.3.2.6. Priority order (highest to lowest):

  1. Global Unicast Addresses (routable on internet)
  2. Unique Local Addresses (ULA, fc00::/7)
  3. Link-Local Addresses (fe80::/10)
  4. Other addresses

This sorting helps ensure better connectivity for cross-network communication.

func VendorIDSubtype

func VendorIDSubtype(vendorID fabric.VendorID) string

VendorIDSubtype returns the subtype filter for vendor ID. Format: "_V<value>"

Types

type Advertiser

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

Advertiser publishes DNS-SD services to the network.

func NewAdvertiser

func NewAdvertiser(config AdvertiserConfig) (*Advertiser, error)

NewAdvertiser creates a new Advertiser with the given configuration.

func (*Advertiser) Close

func (a *Advertiser) Close() error

Close stops all services and closes the advertiser.

func (*Advertiser) GetInstanceName

func (a *Advertiser) GetInstanceName(serviceType ServiceType) string

GetInstanceName returns the instance name for an active service. Returns empty string if the service is not active.

func (*Advertiser) IsAdvertising

func (a *Advertiser) IsAdvertising(serviceType ServiceType) bool

IsAdvertising returns true if the given service type is currently being advertised.

func (*Advertiser) StartCommissionable

func (a *Advertiser) StartCommissionable(txt CommissionableTXT) error

StartCommissionable begins advertising the commissionable node discovery service. Service type: _matterc._udp Spec Section 4.3.1

func (*Advertiser) StartCommissioner

func (a *Advertiser) StartCommissioner(txt CommissionerTXT) error

StartCommissioner begins advertising the commissioner discovery service. Service type: _matterd._udp Spec Section 4.3.3

func (*Advertiser) StartOperational

func (a *Advertiser) StartOperational(compressedFabricID [8]byte, nodeID fabric.NodeID, txt OperationalTXT) error

StartOperational begins advertising the operational discovery service. Service type: _matter._tcp Spec Section 4.3.2

func (*Advertiser) Stop

func (a *Advertiser) Stop(serviceType ServiceType) error

Stop stops advertising a specific service type.

func (*Advertiser) StopAll

func (a *Advertiser) StopAll()

StopAll stops all active service advertisements.

type AdvertiserConfig

type AdvertiserConfig struct {
	// HostName is the mDNS host name (e.g., from MAC address).
	// If empty, a random name will be generated.
	HostName string

	// Port is the Matter port to advertise (default: 5540).
	Port int

	// Interfaces specifies which network interfaces to advertise on.
	// If nil, all interfaces are used.
	Interfaces []net.Interface

	// ServerFactory is the factory for creating mDNS servers.
	// If nil, the default pion/mdns factory is used.
	ServerFactory MDNSServerFactory

	// LoggerFactory for creating loggers.
	LoggerFactory logging.LoggerFactory
}

AdvertiserConfig holds configuration for the Advertiser.

type AdvertiserWithContext

type AdvertiserWithContext struct {
	*Advertiser
	// contains filtered or unexported fields
}

AdvertiserWithContext wraps an Advertiser with context support.

func NewAdvertiserWithContext

func NewAdvertiserWithContext(ctx context.Context, config AdvertiserConfig) (*AdvertiserWithContext, error)

NewAdvertiserWithContext creates an Advertiser that can be cancelled via context.

func (*AdvertiserWithContext) Close

func (a *AdvertiserWithContext) Close() error

Close cancels the context and closes the advertiser.

type CommissionableTXT

type CommissionableTXT struct {
	// Discriminator is the 12-bit discriminator (required).
	// Spec Section 4.3.1.5
	Discriminator uint16

	// CommissioningMode indicates the current commissioning mode.
	// Spec Section 4.3.1.3
	CommissioningMode CommissioningMode

	// VendorID is the vendor identifier (optional, from VP key).
	// Spec Section 4.3.1.6
	VendorID fabric.VendorID

	// ProductID is the product identifier (optional, from VP key).
	// Spec Section 4.3.1.6
	ProductID uint16

	// DeviceType is the primary device type identifier (optional).
	// Spec Section 4.3.1.8
	DeviceType uint32

	// DeviceName is the human-readable device name (optional, max 32 chars).
	// Spec Section 4.3.1.9
	DeviceName string

	// IdleInterval is the SESSION_IDLE_INTERVAL in milliseconds (optional).
	// Spec Section 4.3.1.10
	IdleInterval time.Duration

	// ActiveInterval is the SESSION_ACTIVE_INTERVAL in milliseconds (optional).
	// Spec Section 4.3.1.11
	ActiveInterval time.Duration

	// TCPSupported indicates whether the node supports TCP (optional).
	// Spec Section 4.3.4
	TCPSupported bool

	// ICDMode is the ICD operating mode (optional).
	// Spec Section 4.3.4
	ICDMode ICDMode
	ICDSet  bool // Whether ICD was explicitly set

	// PairingHint is the bitmap of pairing methods (optional).
	// Spec Section 4.3.1.12
	PairingHint uint16

	// PairingInstructions provides additional pairing info (optional).
	// Spec Section 4.3.1.14
	PairingInstructions string
}

CommissionableTXT holds TXT records for _matterc._udp (Spec 4.3.1.4).

func ParseCommissionableTXT

func ParseCommissionableTXT(records []string) (*CommissionableTXT, error)

ParseCommissionableTXT parses raw TXT records into CommissionableTXT.

func (*CommissionableTXT) Encode

func (c *CommissionableTXT) Encode() []string

Encode converts the TXT record to DNS-SD format strings.

func (*CommissionableTXT) ShortDiscriminator

func (c *CommissionableTXT) ShortDiscriminator() uint8

ShortDiscriminator returns the 4-bit short discriminator. Spec Section 4.3.1.5: short = (long >> 8) & 0xF

func (*CommissionableTXT) Validate

func (c *CommissionableTXT) Validate() error

Validate checks that the TXT record values are within spec limits.

type CommissionerTXT

type CommissionerTXT struct {
	// VendorID is the vendor identifier (optional).
	VendorID fabric.VendorID

	// ProductID is the product identifier (optional).
	ProductID uint16

	// DeviceType is the primary device type identifier (optional).
	DeviceType uint32

	// DeviceName is the human-readable device name (optional, max 32 chars).
	DeviceName string

	// CommissionerPasscode indicates whether commissioner passcode is supported.
	// Spec Section 4.3.3
	CommissionerPasscode bool
}

CommissionerTXT holds TXT records for _matterd._udp (Spec 4.3.3).

func ParseCommissionerTXT

func ParseCommissionerTXT(records []string) (*CommissionerTXT, error)

ParseCommissionerTXT parses raw TXT records into CommissionerTXT.

func (*CommissionerTXT) Encode

func (c *CommissionerTXT) Encode() []string

Encode converts the TXT record to DNS-SD format strings.

func (*CommissionerTXT) Validate

func (c *CommissionerTXT) Validate() error

Validate checks that the TXT record values are within spec limits.

type CommissioningMode

type CommissioningMode int

CommissioningMode indicates the commissioning state of a node. Spec Section 4.3.1.3

const (
	// CommissioningModeDisabled indicates the node is not currently in commissioning mode.
	// CM=0: Extended Discovery only, not accepting commissioning.
	CommissioningModeDisabled CommissioningMode = 0

	// CommissioningModeBasic indicates basic commissioning mode.
	// CM=1: Node is in commissioning mode (e.g., factory reset, first boot).
	CommissioningModeBasic CommissioningMode = 1

	// CommissioningModeEnhanced indicates enhanced commissioning mode.
	// CM=2: Administrator-opened commissioning window (OpenCommissioningWindow command).
	CommissioningModeEnhanced CommissioningMode = 2
)

CommissioningMode constants.

func (CommissioningMode) IsValid

func (c CommissioningMode) IsValid() bool

IsValid returns true if the commissioning mode is valid.

func (CommissioningMode) String

func (c CommissioningMode) String() string

String returns a human-readable string for the commissioning mode.

type ICDMode

type ICDMode int

ICDMode indicates the operating mode of an Intermittently Connected Device. Spec Section 4.3.4

const (
	// ICDModeSIT indicates Short Idle Time operating mode.
	// ICD=0: Device operates in Short Idle Time mode.
	ICDModeSIT ICDMode = 0

	// ICDModeLIT indicates Long Idle Time operating mode.
	// ICD=1: Device operates in Long Idle Time mode.
	ICDModeLIT ICDMode = 1
)

ICDMode constants.

func (ICDMode) IsValid

func (i ICDMode) IsValid() bool

IsValid returns true if the ICD mode is valid.

func (ICDMode) String

func (i ICDMode) String() string

String returns a human-readable string for the ICD mode.

type MDNSResolver

type MDNSResolver interface {
	// Browse browses for services of the given type. The service may be a
	// subtype browse name of the form "<subtype>._sub.<service>".
	Browse(ctx context.Context, service, domain string, entries chan<- *ServiceEntry) error

	// Lookup looks up a specific service instance.
	Lookup(ctx context.Context, instance, service, domain string, entries chan<- *ServiceEntry) error
}

MDNSResolver is the interface for mDNS service resolution. This allows for dependency injection in tests.

Implementations send entries until ctx is done and then return; the caller owns and closes the entries channel.

type MDNSServer

type MDNSServer interface {
	// Shutdown stops the server.
	Shutdown()
}

MDNSServer is the interface for mDNS service registration. This allows for dependency injection in tests.

type MDNSServerFactory

type MDNSServerFactory interface {
	// Register creates a new mDNS server for the given service.
	Register(reg ServiceRegistration) (MDNSServer, error)
}

MDNSServerFactory creates MDNSServer instances.

type Manager

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

Manager coordinates DNS-SD advertising and resolution for Matter.

func NewManager

func NewManager(config ManagerConfig) (*Manager, error)

NewManager creates a new discovery Manager with the given configuration.

func (*Manager) Advertiser

func (m *Manager) Advertiser() *Advertiser

Advertiser returns the underlying Advertiser for advanced usage.

func (*Manager) BrowseCommissionable

func (m *Manager) BrowseCommissionable(ctx context.Context) (<-chan ResolvedService, error)

BrowseCommissionable discovers commissionable nodes on the network. Spec Section 4.3.1

func (*Manager) BrowseCommissionableByDiscriminator

func (m *Manager) BrowseCommissionableByDiscriminator(ctx context.Context, discriminator uint16) (<-chan ResolvedService, error)

BrowseCommissionableByDiscriminator discovers commissionable nodes matching the discriminator.

func (*Manager) BrowseCommissionableByVendor

func (m *Manager) BrowseCommissionableByVendor(ctx context.Context, vendorID fabric.VendorID) (<-chan ResolvedService, error)

BrowseCommissionableByVendor discovers commissionable nodes from a specific vendor.

func (*Manager) BrowseCommissioner

func (m *Manager) BrowseCommissioner(ctx context.Context) (<-chan ResolvedService, error)

BrowseCommissioner discovers commissioners on the network. Spec Section 4.3.3

func (*Manager) BrowseOperational

func (m *Manager) BrowseOperational(ctx context.Context) (<-chan ResolvedService, error)

BrowseOperational discovers operational nodes on the network. Spec Section 4.3.2

func (*Manager) Close

func (m *Manager) Close() error

Close stops all services and releases resources.

func (*Manager) DiscoverCommissionableNode

func (m *Manager) DiscoverCommissionableNode(ctx context.Context, discriminator uint16) (*ResolvedService, error)

DiscoverCommissionableNode finds a commissionable node by discriminator. This is a convenience method that browses and returns the first match.

func (*Manager) IsAdvertising

func (m *Manager) IsAdvertising(serviceType ServiceType) bool

IsAdvertising returns true if the given service type is currently being advertised.

func (*Manager) LookupOperational

func (m *Manager) LookupOperational(ctx context.Context, compressedFabricID [8]byte, nodeID fabric.NodeID) (*ResolvedService, error)

LookupOperational looks up a specific operational node. This is the primary method for finding a known commissioned node before establishing a CASE session. Spec Section 4.3.2

func (*Manager) Resolver

func (m *Manager) Resolver() *Resolver

Resolver returns the underlying Resolver for advanced usage.

func (*Manager) StartCommissionable

func (m *Manager) StartCommissionable(txt CommissionableTXT) error

StartCommissionable begins advertising as a commissionable node. This should be called when the device enters commissioning mode. Spec Section 4.3.1

func (*Manager) StartCommissioner

func (m *Manager) StartCommissioner(txt CommissionerTXT) error

StartCommissioner begins advertising as a commissioner. This should be called when the device acts as a commissioner. Spec Section 4.3.3

func (*Manager) StartOperational

func (m *Manager) StartOperational(compressedFabricID [8]byte, nodeID fabric.NodeID, txt OperationalTXT) error

StartOperational begins advertising as an operational (commissioned) node. This should be called after the device is commissioned onto a fabric. Spec Section 4.3.2

func (*Manager) StopAdvertising

func (m *Manager) StopAdvertising(serviceType ServiceType) error

StopAdvertising stops advertising a specific service type.

func (*Manager) StopAllAdvertising

func (m *Manager) StopAllAdvertising()

StopAllAdvertising stops all active service advertisements.

type ManagerConfig

type ManagerConfig struct {
	// HostName is the mDNS host name (e.g., derived from MAC address).
	// If empty, a default will be generated.
	HostName string

	// Port is the Matter port to advertise (default: 5540).
	Port int

	// Interfaces specifies which network interfaces to use.
	// If nil, all interfaces are used.
	Interfaces []net.Interface

	// BrowseTimeout is the default timeout for browse operations.
	// If zero, DefaultBrowseTimeout is used.
	BrowseTimeout time.Duration

	// LookupTimeout is the default timeout for lookup operations.
	// If zero, DefaultLookupTimeout is used.
	LookupTimeout time.Duration

	// ServerFactory is the factory for creating mDNS servers (for testing).
	ServerFactory MDNSServerFactory

	// MDNSResolver is the mDNS resolver implementation (for testing).
	MDNSResolver MDNSResolver

	// LoggerFactory is the factory for creating loggers.
	// If nil, logging is disabled.
	LoggerFactory logging.LoggerFactory
}

ManagerConfig holds configuration for the discovery Manager.

type MockMDNSResolver

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

MockMDNSResolver provides a mock mDNS resolver for testing without real network I/O. It allows registering services and simulating discovery responses.

func NewMockMDNSResolver

func NewMockMDNSResolver() *MockMDNSResolver

NewMockMDNSResolver creates a new mock resolver.

func (*MockMDNSResolver) Browse

func (m *MockMDNSResolver) Browse(ctx context.Context, service, domain string, entries chan<- *ServiceEntry) error

Browse implements MDNSResolver.

func (*MockMDNSResolver) ClearServices

func (m *MockMDNSResolver) ClearServices()

ClearServices removes all registered services.

func (*MockMDNSResolver) Lookup

func (m *MockMDNSResolver) Lookup(ctx context.Context, instance, service, domain string, entries chan<- *ServiceEntry) error

Lookup implements MDNSResolver.

func (*MockMDNSResolver) RegisterService

func (m *MockMDNSResolver) RegisterService(service string, entry *ServiceEntry)

RegisterService registers a service that will be returned by Browse/Lookup.

type OperationalTXT

type OperationalTXT struct {
	// IdleInterval is the SESSION_IDLE_INTERVAL in milliseconds (optional).
	IdleInterval time.Duration

	// ActiveInterval is the SESSION_ACTIVE_INTERVAL in milliseconds (optional).
	ActiveInterval time.Duration

	// TCPSupported indicates whether the node supports TCP (optional).
	TCPSupported bool

	// ICDMode is the ICD operating mode (optional).
	ICDMode ICDMode
	ICDSet  bool // Whether ICD was explicitly set
}

OperationalTXT holds TXT records for _matter._tcp (Spec 4.3.2.5).

func ParseOperationalTXT

func ParseOperationalTXT(records []string) (*OperationalTXT, error)

ParseOperationalTXT parses raw TXT records into OperationalTXT.

func (*OperationalTXT) Encode

func (o *OperationalTXT) Encode() []string

Encode converts the TXT record to DNS-SD format strings.

type ResolvedService

type ResolvedService struct {
	// ServiceType is the type of the discovered service.
	ServiceType ServiceType

	// InstanceName is the DNS-SD instance name.
	InstanceName string

	// HostName is the target host name.
	HostName string

	// Port is the service port.
	Port int

	// IPs contains the resolved IP addresses, sorted by preference.
	IPs []net.IP

	// Text contains the raw TXT record key-value pairs.
	Text map[string]string
}

ResolvedService contains information about a discovered DNS-SD service.

func (*ResolvedService) IPv4Addresses

func (r *ResolvedService) IPv4Addresses() []net.IP

IPv4Addresses returns only IPv4 addresses from the service.

func (*ResolvedService) IPv6Addresses

func (r *ResolvedService) IPv6Addresses() []net.IP

IPv6Addresses returns only IPv6 addresses from the service.

func (*ResolvedService) PreferredIP

func (r *ResolvedService) PreferredIP() net.IP

PreferredIP returns the most preferred IP address (first in the sorted list). Returns nil if no addresses are available.

type Resolver

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

Resolver discovers Matter services via DNS-SD.

func NewResolver

func NewResolver(config ResolverConfig) (*Resolver, error)

NewResolver creates a new Resolver with the given configuration.

func (*Resolver) BrowseCommissionable

func (r *Resolver) BrowseCommissionable(ctx context.Context) (<-chan ResolvedService, error)

BrowseCommissionable discovers commissionable nodes on the network. Returns a channel that receives discovered services until the context is cancelled or the browse timeout expires. Spec Section 4.3.1

func (*Resolver) BrowseCommissionableWithFilter

func (r *Resolver) BrowseCommissionableWithFilter(ctx context.Context, filter string) (<-chan ResolvedService, error)

BrowseCommissionableWithFilter discovers commissionable nodes matching the filter. The filter can be:

  • Short discriminator: "_S<value>" (e.g., "_S3")
  • Long discriminator: "_L<value>" (e.g., "_L840")
  • Vendor ID: "_V<value>" (e.g., "_V123")
  • Device Type: "_T<value>" (e.g., "_T81")
  • Commissioning Mode: "_CM"

func (*Resolver) BrowseCommissioner

func (r *Resolver) BrowseCommissioner(ctx context.Context) (<-chan ResolvedService, error)

BrowseCommissioner discovers commissioners on the network. Returns a channel that receives discovered services until the context is cancelled or the browse timeout expires. Spec Section 4.3.3

func (*Resolver) BrowseOperational

func (r *Resolver) BrowseOperational(ctx context.Context) (<-chan ResolvedService, error)

BrowseOperational discovers operational nodes on the network. Returns a channel that receives discovered services until the context is cancelled or the browse timeout expires. Spec Section 4.3.2

func (*Resolver) DiscoverCommissionableNode

func (r *Resolver) DiscoverCommissionableNode(ctx context.Context, discriminator uint16) (*ResolvedService, error)

DiscoverCommissionableNode is a convenience function to find a commissionable node by discriminator. It browses and filters by the long discriminator.

func (*Resolver) Lookup

func (r *Resolver) Lookup(ctx context.Context, serviceType ServiceType, instanceName string) (*ResolvedService, error)

Lookup looks up a specific service instance by name.

func (*Resolver) LookupOperational

func (r *Resolver) LookupOperational(ctx context.Context, compressedFabricID [8]byte, nodeID fabric.NodeID) (*ResolvedService, error)

LookupOperational looks up a specific operational node by compressed fabric ID and node ID. This is the primary method for finding a known commissioned node. Spec Section 4.3.2

type ResolverConfig

type ResolverConfig struct {
	// MDNSResolver is the underlying mDNS resolver implementation.
	// If nil, the default pion/mdns resolver is used.
	MDNSResolver MDNSResolver

	// BrowseTimeout is the timeout for browse operations.
	// If zero, DefaultBrowseTimeout is used.
	BrowseTimeout time.Duration

	// LookupTimeout is the timeout for lookup operations.
	// If zero, DefaultLookupTimeout is used.
	LookupTimeout time.Duration
}

ResolverConfig holds configuration for the Resolver.

type ServiceEntry

type ServiceEntry struct {
	// Instance is the service instance name.
	Instance string

	// Service is the service type (e.g., "_matterc._udp"), never a subtype.
	Service string

	// Domain is the DNS-SD domain.
	Domain string

	// HostName is the SRV target host name.
	HostName string

	// Port is the SRV port.
	Port int

	// Text contains the TXT record "key=value" strings.
	Text []string

	// AddrIPv4 and AddrIPv6 contain the resolved host addresses.
	AddrIPv4 []net.IP
	AddrIPv6 []net.IP
}

ServiceEntry is a DNS-SD service instance reported by an MDNSResolver.

func MockCommissionableService

func MockCommissionableService(instanceName string, port int, ip net.IP, discriminator uint16) *ServiceEntry

MockCommissionableService creates a mock commissionable service entry for testing.

func MockOperationalService

func MockOperationalService(compressedFabricID [8]byte, nodeID uint64, port int, ip net.IP) *ServiceEntry

MockOperationalService creates a mock operational service entry for testing.

type ServiceRegistration

type ServiceRegistration struct {
	// Instance is the service instance name (e.g., "DD200C20D25AE5F7").
	Instance string

	// Service is the service type (e.g., "_matterc._udp").
	Service string

	// Subtypes are the DNS-SD subtypes the instance is also advertised under
	// (e.g., "_L840"). Spec Section 4.3.1.
	Subtypes []string

	// Domain is the DNS-SD domain (e.g., "local.").
	Domain string

	// HostName is the target host name for the SRV record.
	// If empty, the factory picks one.
	HostName string

	// Port is the service port.
	Port int

	// Text contains the TXT record "key=value" strings.
	Text []string

	// Interfaces restricts advertising to these interfaces.
	// If nil, all interfaces are used.
	Interfaces []net.Interface
}

ServiceRegistration describes a DNS-SD service instance to advertise.

type ServiceType

type ServiceType int

ServiceType identifies the type of DNS-SD service. Spec Section 4.3

const (
	// ServiceTypeUnknown represents an unknown or invalid service type.
	ServiceTypeUnknown ServiceType = iota

	// ServiceTypeCommissionable is for nodes ready to be commissioned.
	// Service type: _matterc._udp
	// Spec Section 4.3.1
	ServiceTypeCommissionable

	// ServiceTypeOperational is for commissioned nodes in normal operation.
	// Service type: _matter._tcp
	// Spec Section 4.3.2
	ServiceTypeOperational

	// ServiceTypeCommissioner is for commissioners advertising their presence.
	// Service type: _matterd._udp
	// Spec Section 4.3.3
	ServiceTypeCommissioner
)

ServiceType constants.

func (ServiceType) IsValid

func (s ServiceType) IsValid() bool

IsValid returns true if the service type is valid.

func (ServiceType) ServiceString

func (s ServiceType) ServiceString() string

ServiceString returns the DNS-SD service type string.

func (ServiceType) String

func (s ServiceType) String() string

String returns a human-readable string for the service type.

Jump to

Keyboard shortcuts

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