core

package
v2.5.3 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2024 License: Apache-2.0 Imports: 54 Imported by: 9

Documentation

Index

Constants

View Source
const (
	DiscoveryAddressUDP4Local      = "224.0.1.187:5683"
	DiscoveryAddressUDP6LinkLocal  = "[ff02::158]:5683"
	DiscoveryAddressUDP6RealmLocal = "[ff03::158]:5683"
	DiscoveryAddressUDP6SiteLocal  = "[ff05::158]:5683"
)

See the section 10.4 on the line 2482 of the Core specification: https://openconnectivity.org/specs/OCF_Core_Specification_v2.0.0.pdf https://iotivity.org/documentation/linux/programmers-guide

View Source
const (
	// DiscoverAllDevices discovers owned and disowned devices.
	DiscoverAllDevices = DiscoverOwnershipStatus(0)
	// DiscoverOwnedDevices discovers owned devices,
	DiscoverOwnedDevices = DiscoverOwnershipStatus(1)
	// DiscoverDisownedDevices discovers disowned devices,
	DiscoverDisownedDevices = DiscoverOwnershipStatus(2)
)
View Source
const ANY_DEVICE = "anydevice"

Variables

View Source
var ErrCannotDetermineDeviceID = errors.New("cannot determine device ID")

Functions

func Discover

func Discover(
	ctx context.Context,
	conn []*DiscoveryClient,
	href string,
	handler DiscoveryHandler,
	options ...coap.OptionFunc,
) error

Discover discovers devices using a CoAP multicast request via UDP. It waits for device responses until the context is canceled. Device resources can be queried in DiscoveryHandler. An empty typeFilter queries all resource types. Note: Iotivity 1.3 which responds with BadRequest if more than 1 resource type is queried.

func DiscoverDeviceOwnership

func DiscoverDeviceOwnership(
	ctx context.Context,
	conn []*DiscoveryClient,
	status DiscoverOwnershipStatus,
	handler DiscoverDeviceOwnershipHandler,
) error

DiscoverDeviceOwnership discovers devices using a CoAP multicast request via UDP. It waits for device responses until the context is canceled.

func DiscoverDevices

func DiscoverDevices(
	ctx context.Context,
	conn []*DiscoveryClient,
	handler DiscoverDevicesHandler,
	options ...coap.OptionFunc,
) error

DiscoverDevices discovers devices using a CoAP multicast request via UDP. It waits for device responses until the context is canceled. Device resources can be queried in DiscoveryHandler. An empty typeFilter queries all resource types. Note: Iotivity 1.3 which responds with BadRequest if more than 1 resource type is queried.

func GetResourceLink(links schema.ResourceLinks, href string) (schema.ResourceLink, error)

func MakeAborted

func MakeAborted(e error) error

func MakeAlreadyExists

func MakeAlreadyExists(e error) error

func MakeCanceled

func MakeCanceled(e error) error

func MakeDataLoss

func MakeDataLoss(e error) error

func MakeDeadlineExceeded

func MakeDeadlineExceeded(e error) error

func MakeFailedPrecondition

func MakeFailedPrecondition(e error) error

func MakeInternal

func MakeInternal(e error) error

func MakeInternalStr

func MakeInternalStr(str string, e error) error

func MakeInvalidArgument

func MakeInvalidArgument(e error) error

func MakeNotFound

func MakeNotFound(e error) error

func MakeOutOfRange

func MakeOutOfRange(e error) error

func MakePermissionDenied

func MakePermissionDenied(e error) error

func MakeResourceExhausted

func MakeResourceExhausted(e error) error

func MakeSdkError

func MakeSdkError(code codes.Code, err error) error

func MakeUnauthenticated

func MakeUnauthenticated(e error) error

func MakeUnavailable

func MakeUnavailable(e error) error

func MakeUnimplemented

func MakeUnimplemented(e error) error

func MakeUnknown

func MakeUnknown(e error) error

Types

type ActionAfterOwnFunc

type ActionAfterOwnFunc = func(ctx context.Context, client *coap.ClientCloseHandler) error

type ActionDuringOwnFunc

type ActionDuringOwnFunc = func(ctx context.Context, client *coap.ClientCloseHandler) (string, error)

type CertificateSigner

type CertificateSigner = interface {
	// csr is encoded by PEM and returns PEM
	Sign(ctx context.Context, csr []byte) ([]byte, error)
}

type Client

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

Client an OCF local client.

func NewClient

func NewClient(opts ...OptionFunc) *Client

func (*Client) GetDeviceByIP

func (c *Client) GetDeviceByIP(ctx context.Context, ip string) (*Device, error)

GetDeviceByIP gets the device directly via IP address and multicast listen port 5683.

func (*Client) GetDeviceByMulticast

func (c *Client) GetDeviceByMulticast(ctx context.Context, deviceID string, discoveryConfiguration DiscoveryConfiguration) (*Device, error)

GetDeviceByMulticast performs a multicast and returns a device object if the device responds.

func (*Client) GetDevicesByIP added in v2.3.0

func (c *Client) GetDevicesByIP(ctx context.Context, ip string) ([]*Device, error)

GetDevicesByIP gets the devices directly via IP address and multicast listen port 5683.

func (*Client) GetDevicesByMulticast

func (c *Client) GetDevicesByMulticast(ctx context.Context, discoveryConfiguration DiscoveryConfiguration, handler DeviceMulticastHandler) error

GetDevicesByMulticast discovers devices using a CoAP multicast request via UDP. Device resources can be queried in DeviceHandler using device.Client,

func (*Client) GetOwnerships

func (c *Client) GetOwnerships(
	ctx context.Context,
	discoveryConfiguration DiscoveryConfiguration,
	status DiscoverOwnershipStatus,
	handler OwnershipHandler,
) error

GetOwnerships discovers device's ownerships using a CoAP multicast request via UDP. It waits for device responses until the context is canceled.

func (*Client) GetSdkOwnerID

func (c *Client) GetSdkOwnerID() (string, error)

GetSdkOwnerID returns sdk ownerID from sdk identity certificate.

type Config

type Config struct {
	TLSConfig *TLSConfig
	Logger    Logger
	DialDTLS  DialDTLS
	DialTLS   DialTLS
	DialTCP   DialTCP
	DialUDP   DialUDP
}

type Device

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

func NewDevice

func NewDevice(
	cfg DeviceConfiguration,
	deviceID string,
	deviceTypes []string,
	getEndpoints func() schema.Endpoints,
) *Device

func (*Device) Close

func (d *Device) Close(ctx context.Context) error

Close closes open connections to the device.

func (*Device) DeleteResource

func (d *Device) DeleteResource(
	ctx context.Context,
	link schema.ResourceLink,
	response interface{},
	options ...coap.OptionFunc,
) error

func (*Device) DeleteResourceWithCodec

func (d *Device) DeleteResourceWithCodec(
	ctx context.Context,
	link schema.ResourceLink,
	codec coap.Codec,
	response interface{},
	options ...coap.OptionFunc,
) error

func (*Device) DeviceID

func (d *Device) DeviceID() string

func (*Device) DeviceTypes

func (d *Device) DeviceTypes() []string

func (*Device) Disown

func (d *Device) Disown(
	ctx context.Context,
	links schema.ResourceLinks,
	options ...coap.OptionFunc,
) error

Disown removes ownership of device

func (*Device) FactoryReset

func (d *Device) FactoryReset(
	ctx context.Context,
	links schema.ResourceLinks,
	options ...coap.OptionFunc,
) error

func (*Device) FoundByIP

func (d *Device) FoundByIP() string

func (*Device) GetEndpoints

func (d *Device) GetEndpoints() schema.Endpoints

func (*Device) GetOwnership

func (d *Device) GetOwnership(ctx context.Context, links schema.ResourceLinks, options ...coap.OptionFunc) (doxm.Doxm, error)

GetOwnership gets device's ownership resource.

func (*Device) GetResource

func (d *Device) GetResource(
	ctx context.Context,
	link schema.ResourceLink,
	response interface{},
	options ...coap.OptionFunc,
) error

GetResource queries a device for a resource value in CBOR.

func (d *Device) GetResourceLinks(ctx context.Context, endpoints []schema.Endpoint, options ...coap.OptionFunc) (schema.ResourceLinks, error)

func (*Device) GetResourceWithCodec

func (d *Device) GetResourceWithCodec(
	ctx context.Context,
	link schema.ResourceLink,
	codec coap.Codec,
	response interface{},
	options ...coap.OptionFunc,
) error

GetResourceWithCodec queries a device for a resource value.

func (*Device) GetResources

func (d *Device) GetResources(links schema.ResourceLinks) *ResourceIterator

GetResources resolves URIs and returns an iterator for querying resources of given resource types.

func (*Device) GetSdkOwnerID

func (d *Device) GetSdkOwnerID() (string, error)

GetSdkOwnerID returns sdk ownerID

func (*Device) IsConnected

func (d *Device) IsConnected() bool

func (*Device) IsSecured

func (d *Device) IsSecured() bool

IsSecured returns if device serves secure ports.

func (*Device) ObserveResource

func (d *Device) ObserveResource(
	ctx context.Context,
	link schema.ResourceLink,
	handler ObservationHandler,
	options ...coap.OptionFunc,
) (observationID string, _ error)

func (*Device) ObserveResourceWithCodec

func (d *Device) ObserveResourceWithCodec(
	ctx context.Context,
	link schema.ResourceLink,
	codec coap.Codec,
	handler ObservationHandler,
	options ...coap.OptionFunc,
) (observationID string, _ error)

func (*Device) Own

func (d *Device) Own(
	ctx context.Context,
	links schema.ResourceLinks,
	otmClients []otm.Client,
	options ...OwnOption,
) error

Own set ownership of device. For owning, the first match in order of otmClients with the device will be used. Note: In case if the device fails before changing RFOTM the iotivity-stack invokes disown by itself. This can result in a state where the disown is invoked two times in a row. Once by the iotivity-stack and second time by device core.

func (*Device) Provision

func (d *Device) Provision(ctx context.Context, links schema.ResourceLinks, options ...coap.OptionFunc) (*ProvisioningClient, error)

func (*Device) Reboot

func (d *Device) Reboot(
	ctx context.Context,
	links schema.ResourceLinks,
	options ...coap.OptionFunc,
) error

func (*Device) SetDeviceID

func (d *Device) SetDeviceID(deviceID string)

func (*Device) SetEndpoints

func (d *Device) SetEndpoints(getEndpoints func() schema.Endpoints)

func (*Device) StopObservingResource

func (d *Device) StopObservingResource(
	ctx context.Context,
	observationID string,
) (bool, error)

func (*Device) UpdateBy

func (d *Device) UpdateBy(v *Device)

func (*Device) UpdateResource

func (d *Device) UpdateResource(
	ctx context.Context,
	link schema.ResourceLink,
	request interface{},
	response interface{},
	options ...coap.OptionFunc,
) error

func (*Device) UpdateResourceWithCodec

func (d *Device) UpdateResourceWithCodec(
	ctx context.Context,
	link schema.ResourceLink,
	codec coap.Codec,
	request interface{},
	response interface{},
	options ...coap.OptionFunc,
) error

type DeviceConfiguration

type DeviceConfiguration struct {
	DialDTLS   DialDTLS
	DialTLS    DialTLS
	DialUDP    DialUDP
	DialTCP    DialTCP
	Logger     Logger
	TLSConfig  *TLSConfig
	GetOwnerID func() (string, error)
}

type DeviceHandler

type DeviceHandler interface {
	// Handle gets a device connection and is responsible for closing it.
	Handle(ctx context.Context, device *Device, deviceLinks schema.ResourceLinks)
	// Error gets errors during discovery.
	Error(err error)
}

DeviceHandler conveys device connections and errors during discovery.

type DeviceMulticastHandler

type DeviceMulticastHandler interface {
	// Handle gets a device connection and is responsible for closing it.
	Handle(ctx context.Context, device *Device)
	// Error gets errors during discovery.
	Error(err error)
}

DeviceMulticastHandler conveys device connections and errors during discovery.

type DialDTLS

type DialDTLS = func(ctx context.Context, addr string, dtlsCfg *dtls.Config, opts ...udp.Option) (*coap.ClientCloseHandler, error)

type DialTCP

type DialTCP = func(ctx context.Context, addr string, opts ...tcp.Option) (*coap.ClientCloseHandler, error)

type DialTLS

type DialTLS = func(ctx context.Context, addr string, tlsCfg *tls.Config, opts ...tcp.Option) (*coap.ClientCloseHandler, error)

type DialUDP

type DialUDP = func(ctx context.Context, addr string, opts ...udp.Option) (*coap.ClientCloseHandler, error)

type DiscoverDeviceCodec

type DiscoverDeviceCodec struct{}

func (DiscoverDeviceCodec) ContentFormat

func (c DiscoverDeviceCodec) ContentFormat() message.MediaType

ContentFormat propagates the CoAP media type.

func (DiscoverDeviceCodec) Decode

func (c DiscoverDeviceCodec) Decode(msg *pool.Message, v interface{}) error

Decode validates the content format and propagates the payload to v as *schema.ResourceLinks

func (DiscoverDeviceCodec) Encode

func (c DiscoverDeviceCodec) Encode(interface{}) ([]byte, error)

Encode propagates the payload without any conversions.

type DiscoverDeviceOwnershipHandler

type DiscoverDeviceOwnershipHandler interface {
	Handle(ctx context.Context, client *client.Conn, device doxm.Doxm)
	Error(err error)
}

DiscoverDeviceOwnershipHandler receives devices ownership info.

type DiscoverDevicesHandler

type DiscoverDevicesHandler interface {
	Handle(ctx context.Context, client *client.Conn, device schema.ResourceLinks)
	Error(err error)
}

DiscoverDevicesHandler receives device links and errors from the discovery multicast request.

type DiscoverOwnershipStatus

type DiscoverOwnershipStatus int

DiscoverOwnershipStatus type of discover ownership status.

type DiscoveryClient

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

func DialDiscoveryAddresses

func DialDiscoveryAddresses(ctx context.Context, cfg DiscoveryConfiguration, errFn func(error)) ([]*DiscoveryClient, error)

DialDiscoveryAddresses connects to discovery endpoints.

func (*DiscoveryClient) Close

func (d *DiscoveryClient) Close() error

func (*DiscoveryClient) PublishMsgWithContext

func (d *DiscoveryClient) PublishMsgWithContext(req *pool.Message, discoveryHandler DiscoveryHandler) error

type DiscoveryConfiguration

type DiscoveryConfiguration struct {
	MulticastHopLimit    int      // default: {224.0.1.187:5683, ff02::158]:5683} = 1, {[ff03::158]:5683, [ff05::158]:5683} = 255, https://openconnectivity.org/specs/OCF_Core_Specification.pdf 12.2.9
	MulticastAddressUDP4 []string // default: "[224.0.1.187:5683] (client.DiscoveryAddressUDP4), empty: don't use ipv4 multicast"
	MulticastAddressUDP6 []string // default: "[ff02::158]:5683", "[ff03::158]:5683", "[ff05::158]:5683]"] (client.DiscoveryAddressUDP6), empty: don't use ipv6 multicast"
	MulticastOptions     []coapNet.MulticastOption
}

DiscoveryConfiguration setup discovery configuration

func DefaultDiscoveryConfiguration

func DefaultDiscoveryConfiguration() DiscoveryConfiguration

type DiscoveryHandler

type DiscoveryHandler = func(conn *client.Conn, req *pool.Message)

type GetCertificateAuthoritiesFunc

type GetCertificateAuthoritiesFunc func() ([]*x509.Certificate, error)

GetCertificateAuthoritiesFunc returns certificate authorities to verify peers

type GetCertificateFunc

type GetCertificateFunc func() (tls.Certificate, error)

GetCertificateFunc returns certificate for connection

type Logger

type Logger = log.Logger

type ObservationHandler

type ObservationHandler interface {
	Handle(ctx context.Context, body coap.DecodeFunc)
	OnClose()
	Error(err error)
}

type OptionFunc

type OptionFunc func(Config) Config

func WithDialDTLS

func WithDialDTLS(dial DialDTLS) OptionFunc

func WithDialTCP

func WithDialTCP(dial DialTCP) OptionFunc

func WithDialTLS

func WithDialTLS(dial DialTLS) OptionFunc

func WithDialUDP

func WithDialUDP(dial DialUDP) OptionFunc

func WithLogger

func WithLogger(logger Logger) OptionFunc

func WithTLS

func WithTLS(tlsConfig *TLSConfig) OptionFunc

type OwnOption

type OwnOption = func(ownCfg) ownCfg

func WithActionAfterOwn

func WithActionAfterOwn(actionAfterOwn ActionAfterOwnFunc) OwnOption

WithActionAfterOwn allows initialize configuration at the device via DTLS connection with preshared key. For example setup time / NTP. if it returns error device will be disowned.

func WithActionDuringOwn

func WithActionDuringOwn(actionDuringOwn ActionDuringOwnFunc) OwnOption

WithActionDuringOwn allows to set deviceID of owned device and other staff over owner TLS. returns new deviceID, if it returns error device will be disowned.

func WithPresharedKey

func WithPresharedKey(psk []byte) OwnOption

WithPresharedKey allows to set preshared key for owner. It is not set, it will be randomized.

func WithSetupCertificates

func WithSetupCertificates(sign otm.SignFunc) OwnOption

WithSetupCertificates signs identity ceriticates and install root ca.

type OwnershipHandler

type OwnershipHandler interface {
	// Handle gets a device ownership.
	Handle(ctx context.Context, doxm doxm.Doxm)
	// Error gets errors during discovery.
	Error(err error)
}

OwnershipHandler conveys device ownership and errors during discovery.

type ProvisioningClient

type ProvisioningClient struct {
	*Device
	// contains filtered or unexported fields
}

func (*ProvisioningClient) AddCertificateAuthority

func (c *ProvisioningClient) AddCertificateAuthority(ctx context.Context, subject string, cert *x509.Certificate) error

func (*ProvisioningClient) AddCredentials

func (*ProvisioningClient) Close

func (c *ProvisioningClient) Close(ctx context.Context) error

func (*ProvisioningClient) SetAccessControl

func (c *ProvisioningClient) SetAccessControl(
	ctx context.Context,
	permission acl.Permission,
	subject acl.Subject,
	resources ...acl.Resource,
) error

SetAccessControl updates the acl resource. Usage: SetAccessControl(ctx, schema.AllPermissions, schema.TLSConnection, schema.AllResources)

func (*ProvisioningClient) SetCloudResource

type ResourceIterator

type ResourceIterator struct {
	Err error
	// contains filtered or unexported fields
}

ResourceIterator queries resource values.

func (*ResourceIterator) Next

func (it *ResourceIterator) Next(ctx context.Context, v interface{}) bool

Next queries the next resource value. Returns false when failed or having no more items. Check it.Err for errors. Usage:

for {
	var v MyStruct
	if !it.Next(ctx, &v) {
		break
	}
}
if it.Err != nil {
}

type SdkError

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

func (SdkError) Error

func (e SdkError) Error() string

func (SdkError) GetCode

func (e SdkError) GetCode() codes.Code

func (SdkError) Unwrap

func (e SdkError) Unwrap() error

type TLSConfig

type TLSConfig struct {
	// User for communication with owned devices and cloud
	GetCertificate            GetCertificateFunc
	GetCertificateAuthorities GetCertificateAuthoritiesFunc
}

Directories

Path Synopsis
otm

Jump to

Keyboard shortcuts

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