client

package
v2.5.0 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2024 License: Apache-2.0 Imports: 57 Imported by: 5

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateSDKIdentityCertificate

func GenerateSDKIdentityCertificate(ctx context.Context, sign SignFunc, sdkDeviceID string) (tls.Certificate, []*x509.Certificate, error)

func GenerateSDKManufacturerCertificate

func GenerateSDKManufacturerCertificate(ctx context.Context, sign SignFunc, id string) (tls.Certificate, []*x509.Certificate, error)

func TokenFromOutgoingMD

func TokenFromOutgoingMD(ctx context.Context) (string, error)

TokenFromOutgoingMD extracts token stored by CtxWithToken.

Types

type ApplicationCallback

type ApplicationCallback = interface {
	GetRootCertificateAuthorities() ([]*x509.Certificate, error)
	GetManufacturerCertificateAuthorities() ([]*x509.Certificate, error)
	GetManufacturerCertificate() (tls.Certificate, error)
}

type Client

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

Client uses the underlying OCF local client.

func NewClient

func NewClient(
	app ApplicationCallback,
	deviceOwner DeviceOwner,
	opt ...ClientOptionFunc,
) (*Client, error)

NewClient constructs a new local client.

func NewClientFromConfig

func NewClientFromConfig(cfg *Config, app ApplicationCallback, logger core.Logger) (*Client, error)

NewClientFromConfig constructs a new local client from the proto configuration.

func (*Client) Close

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

Close clears all connections and spawned goroutines by client.

func (*Client) CoreClient

func (c *Client) CoreClient() *core.Client

func (*Client) CreateResource

func (c *Client) CreateResource(
	ctx context.Context,
	deviceID string,
	href string,
	request interface{},
	response interface{},
	opts ...CreateOption,
) error

CreateResource creates the resource from the device. In the absence of a cached device, it is found through multicast and stored with an expiration time.

func (*Client) DeleteDevice

func (c *Client) DeleteDevice(ctx context.Context, deviceID string) bool

DeleteDevice deletes the device from the cache.

func (*Client) DeleteDevices

func (c *Client) DeleteDevices(ctx context.Context, deviceIDFilter []string) []string

DeleteDevices deletes devices from the cache. If deviceIDFilter is empty, all devices are deleted.

func (*Client) DeleteResource

func (c *Client) DeleteResource(
	ctx context.Context,
	deviceID string,
	href string,
	response interface{},
	opts ...DeleteOption,
) error

DeleteResource deletes the resource from the device. In the absence of a cached device, it is found through multicast and stored with an expiration time.

func (*Client) DisownDevice

func (c *Client) DisownDevice(ctx context.Context, deviceID string, opts ...CommonCommandOption) error

DisownDevice disowns a device. For unsecure device it calls factory reset. For secure device it disowns.

func (*Client) FactoryReset

func (c *Client) FactoryReset(ctx context.Context, deviceID string, opts ...CommonCommandOption) error

FactoryReset factory resets the device.

func (*Client) GetAllDeviceIDsFoundByIP

func (c *Client) GetAllDeviceIDsFoundByIP() map[string]string

func (*Client) GetDevice

func (c *Client) GetDevice(ctx context.Context, deviceID string, opts ...GetDeviceOption,
) (*core.Device, schema.ResourceLinks, error)

GetDevice gets the device from the cache or via multicast or via IP address if was previously stored by GetDeviceByIP and updates device in the cache.

func (*Client) GetDeviceByIP

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

GetDeviceByIP gets device by IP and store it to cache without expiration. To delete device, call DeleteDevices with the deviceID.

func (*Client) GetDeviceByMulticast

func (c *Client) GetDeviceByMulticast(ctx context.Context, deviceID string, opts ...GetDeviceOption) (*core.Device, schema.ResourceLinks, error)

GetDeviceByMulticast gets device by multicast and store it to cache with expiration. When the device expiration time has expired, the device will be removed from cache. The device expiration time is prolonged by using the device.

func (*Client) GetDeviceDetailsByIP

func (c *Client) GetDeviceDetailsByIP(ctx context.Context, ip string, opts ...GetDeviceByIPOption) (DeviceDetails, error)

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

func (*Client) GetDeviceDetailsByMulticast

func (c *Client) GetDeviceDetailsByMulticast(ctx context.Context, deviceID string, opts ...GetDeviceOption) (DeviceDetails, error)

func (*Client) GetDevicesByIP added in v2.3.0

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

GetDevicesByIP gets devices by IP and store it to cache without expiration. To delete device, call DeleteDevices with the deviceID.

func (*Client) GetDevicesDetails added in v2.0.2

func (c *Client) GetDevicesDetails(
	ctx context.Context,
	opts ...GetDevicesOption,
) (map[string]DeviceDetails, error)

GetDevicesDetails gets devices by multicast and devices found by ip. Each device are stored or refreshed in cache. When the device expiration time has expired, the device will be removed from cache. The device expiration time is prolonged by using the device.

func (*Client) GetDevicesWithHandler

func (c *Client) GetDevicesWithHandler(ctx context.Context, handler core.DeviceMulticastHandler, opts ...GetDevicesWithHandlerOption) error

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

func (*Client) GetIdentityCACerts

func (c *Client) GetIdentityCACerts() ([]*x509.Certificate, error)

func (*Client) GetIdentityCertificate

func (c *Client) GetIdentityCertificate() (tls.Certificate, error)

GetIdentityCertificate returns certificate for connection

func (*Client) GetResource

func (c *Client) GetResource(
	ctx context.Context,
	deviceID string,
	href string,
	response interface{},
	opts ...GetOption,
) error

GetResource returns the device resource. In the absence of a cached device, it is found through multicast and stored with an expiration time.

func (*Client) Initialization

func (c *Client) Initialization(ctx context.Context) (err error)

Initialization initializes the client.

func (*Client) ObserveDeviceResources

func (c *Client) ObserveDeviceResources(ctx context.Context, deviceID string, handler DeviceResourcesObservationHandler) (string, error)

ObserveDeviceResources starts observing links in the device. In the absence of a cached device, it is found through multicast and stored with an expiration time.

func (*Client) ObserveDevices

func (c *Client) ObserveDevices(handler DevicesObservationHandler, opts ...ObserveDevicesOption) (string, error)

ObserveDevices method starts observing devices via multicast and added by IP in poll interval configured in observerPollingInterval.

func (*Client) ObserveResource

func (c *Client) ObserveResource(
	ctx context.Context,
	deviceID string,
	href string,
	handler core.ObservationHandler,
	opts ...ObserveOption,
) (observationID string, _ error)

ObserveResource method starts observing the resource of the device. In the absence of a cached device, it is found through multicast and stored with an expiration time.

func (*Client) OffboardDevice

func (c *Client) OffboardDevice(ctx context.Context, deviceID string, opts ...CommonCommandOption) error

OffboardDevice disconnects from the cloud and removes cloud configuration at the device. In the absence of a cached device, it is found through multicast and stored with an expiration time.

func (*Client) OnboardDevice

func (c *Client) OnboardDevice(
	ctx context.Context,
	deviceID, authorizationProvider, cloudURL, authCode, cloudID string,
	opts ...CommonCommandOption,
) error

OnboardDevice connects device to the cloud. In the absence of a cached device, it is found through multicast and stored with an expiration time.

func (*Client) OwnDevice

func (c *Client) OwnDevice(ctx context.Context, deviceID string, opts ...OwnOption) (string, error)

OwnDevice transfer ownership to the client and setup time at the device. In the absence of a cached device, it is found through multicast and stored with an expiration time.

func (*Client) Reboot

func (c *Client) Reboot(ctx context.Context, deviceID string, opts ...CommonCommandOption) error

Reboot reboots the device.

func (*Client) StopObservingDeviceResources

func (c *Client) StopObservingDeviceResources(ctx context.Context, observationID string) (bool, error)

StopObservingDeviceResources stops observing links in the device. In the absence of a cached device, it is found through multicast and stored with an expiration time.

func (*Client) StopObservingDevices

func (c *Client) StopObservingDevices(observationID string) bool

StopObservingDevices method stops observing devices.

func (*Client) StopObservingResource

func (c *Client) StopObservingResource(ctx context.Context, observationID string) (bool, error)

StopObservingResource method stops observing the resource of the device. In the absence of a cached device, it is found through multicast and stored with an expiration time.

func (*Client) UpdateResource

func (c *Client) UpdateResource(
	ctx context.Context,
	deviceID string,
	href string,
	request interface{},
	response interface{},
	opts ...UpdateOption,
) error

UpdateResource updates the device resource. In the absence of a cached device, it is found through multicast and stored with an expiration time.

type ClientConfig added in v2.1.0

type ClientConfig struct {
	CoreOptions []core.OptionFunc
	// CacheExpiration is a time after which the device entry in cache is invalidated.
	CacheExpiration time.Duration
	// Observer is a configuration of the devices observation.
	Observer ObserverConfig
	// UseDeviceIDInQuery if true, deviceID is used also in query. Set this option if you use bridged devices.
	UseDeviceIDInQuery bool
}

type ClientOptionFunc added in v2.1.0

type ClientOptionFunc func(ClientConfig) ClientConfig

func WithCacheExpiration added in v2.1.0

func WithCacheExpiration(cacheExpiration time.Duration) ClientOptionFunc

func WithDialDTLS added in v2.1.0

func WithDialDTLS(dial core.DialDTLS) ClientOptionFunc

func WithDialTCP added in v2.1.0

func WithDialTCP(dial core.DialTCP) ClientOptionFunc

func WithDialTLS added in v2.1.0

func WithDialTLS(dial core.DialTLS) ClientOptionFunc

func WithDialUDP added in v2.1.0

func WithDialUDP(dial core.DialUDP) ClientOptionFunc

func WithLogger added in v2.1.0

func WithLogger(logger core.Logger) ClientOptionFunc

func WithObserverConfig added in v2.1.0

func WithObserverConfig(observerConfig ObserverConfig) ClientOptionFunc

WithObserverConfig sets the observer config.

func WithTLS added in v2.1.0

func WithTLS(tlsConfig *core.TLSConfig) ClientOptionFunc

func WithUseDeviceIDInQuery added in v2.3.0

func WithUseDeviceIDInQuery(useDeviceIDInQuery bool) ClientOptionFunc

WithUseDeviceIDInQuery sets the observer config.

type CodecOption

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

func WithCodec

func WithCodec(codec coap.Codec) CodecOption

type CommonCommandOption

type CommonCommandOption = interface {
	// contains filtered or unexported methods
}

CommonCommandOption option definition.

type Config

type Config struct {
	DeviceCacheExpirationSeconds   int64
	ObserverPollingIntervalSeconds uint64 // 0 means 3 seconds
	ObserverFailureThreshold       uint8  // 0 means 3

	KeepAliveConnectionTimeoutSeconds uint64 // 0 means keepalive is disabled
	MaxMessageSize                    uint32
	DisablePeerTCPSignalMessageCSMs   bool
	DefaultTransferDurationSeconds    uint64 // 0 means 15 seconds

	UseDeviceIDInQuery bool // if true, deviceID is used also in query. Set this option if you use bridged devices.

	// specify one of:
	DeviceOwnershipSDK     *DeviceOwnershipSDKConfig     `yaml:",omitempty"`
	DeviceOwnershipBackend *DeviceOwnershipBackendConfig `yaml:",omitempty"`
}

type CreateOption

type CreateOption = interface {
	// contains filtered or unexported methods
}

CreateOption option definition.

type DeleteOption

type DeleteOption = interface {
	// contains filtered or unexported methods
}

DeleteOption option definition.

type DeviceCache

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

func NewDeviceCache

func NewDeviceCache(deviceExpiration, pollInterval time.Duration, logger core.Logger) *DeviceCache

NewDeviceCache creates a new cache for devices. - deviceExpiration: default expiration time for the device in the cache, 0 means infinite. The device expiration is refreshed by getting or updating the device. - pollInterval: pool interval for cleaning expired devices from the cache - logger: logger for logging

func (*DeviceCache) Close

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

func (*DeviceCache) GetDevice

func (c *DeviceCache) GetDevice(deviceID string) (*core.Device, bool)

func (*DeviceCache) GetDeviceByFoundIP

func (c *DeviceCache) GetDeviceByFoundIP(ip string) []*core.Device

func (*DeviceCache) GetDeviceExpiration

func (c *DeviceCache) GetDeviceExpiration(deviceID string) (time.Time, bool)

func (*DeviceCache) GetDevicesFoundByIP

func (c *DeviceCache) GetDevicesFoundByIP() map[string]string

func (*DeviceCache) LoadAndDeleteDevice

func (c *DeviceCache) LoadAndDeleteDevice(deviceID string) (*core.Device, bool)

LoadAndDeleteDevice loads the device from the cache and deletes it from the cache. To cleanup the device you have to call device.Close.

func (*DeviceCache) LoadAndDeleteDevices

func (c *DeviceCache) LoadAndDeleteDevices(deviceIDFilter []string) []*core.Device

func (*DeviceCache) TryToChangeDeviceExpirationToDefault

func (c *DeviceCache) TryToChangeDeviceExpirationToDefault(deviceID string) bool

TryToChangeDeviceExpirationToDefault attempts to change the expiration time for the device in cache to default expiration.

func (*DeviceCache) UpdateOrStoreDevice

func (c *DeviceCache) UpdateOrStoreDevice(device *core.Device) (*core.Device, bool)

UpdateOrStoreDevice stores the device without timeout into the cache. The device can be removed from the cache only by invoking LoadAndDeleteDevice function and device.Close to cleanup connections. If a device with the same deviceID is already in the cache, the previous reference will be updated in the cache and it's expiration time will be set to infinite.

func (*DeviceCache) UpdateOrStoreDeviceWithExpiration

func (c *DeviceCache) UpdateOrStoreDeviceWithExpiration(device *core.Device) (*core.Device, bool)

UpdateOrStoreDeviceWithExpiration stores the device with the default timeout into the cache. If a device with the same deviceID is already in the cache the device will be updated and the expiration time will be reset only when the device has it set.

type DeviceDetails

type DeviceDetails struct {
	// ID of the device
	ID string
	// IP used to find this device
	FoundByIP string
	// Details result of function which can be set via option WithGetDetails(), by default it is nil.
	Details interface{}
	// IsSecured is secured.
	IsSecured bool
	// Ownership describes ownership of the device, for unsecure device it is nil.
	Ownership *doxm.Doxm
	// Resources list of the device resources.
	Resources schema.ResourceLinks
	// Resources list of the device endpoints.
	Endpoints []schema.Endpoint
	// Ownership status
	OwnershipStatus OwnershipStatus
}

DeviceDetails describes a device.

type DeviceOwner

type DeviceOwner interface {
	Initialization(ctx context.Context) error
	OwnDevice(ctx context.Context, deviceID string, otmTypes []OTMType, discoveryConfiguration core.DiscoveryConfiguration, own ownFunc, opts ...core.OwnOption) (string, error)

	GetIdentityCertificate() (tls.Certificate, error)
	GetIdentityCACerts() ([]*x509.Certificate, error)
}

func NewDeviceOwnerFromConfig

func NewDeviceOwnerFromConfig(cfg *Config, dialTLS core.DialTLS, dialDTLS core.DialDTLS, app ApplicationCallback) (DeviceOwner, error)

type DeviceOwnershipBackendConfig

type DeviceOwnershipBackendConfig struct {
	JWTClaimOwnerID string
	Sign            SignFunc
}

type DeviceOwnershipSDKConfig

type DeviceOwnershipSDKConfig struct {
	ID         string
	Cert       string
	CertKey    string
	ValidFrom  string // RFC3339, or now-1m, empty means now-1m
	CertExpiry *string

	CreateSignerFunc func(caCert []*x509.Certificate, caKey crypto.PrivateKey, validNotBefore time.Time, validNotAfter time.Time) core.CertificateSigner
}

type DeviceResourcesObservationHandler

type DeviceResourcesObservationHandler = interface {
	Handle(ctx context.Context, links schema.ResourceLinks)
	OnClose()
	Error(err error)
}
type DeviceWithLinks struct {
	Device *core.Device
	Links  schema.ResourceLinks
}

type DevicesObservationEvent

type DevicesObservationEvent struct {
	DeviceID string
	Event    DevicesObservationEvent_type
}

type DevicesObservationEvent_type

type DevicesObservationEvent_type uint8
const (
	DevicesObservationEvent_ONLINE  DevicesObservationEvent_type = 0
	DevicesObservationEvent_OFFLINE DevicesObservationEvent_type = 1
)

type DevicesObservationHandler

type DevicesObservationHandler = interface {
	Handle(ctx context.Context, event DevicesObservationEvent) error
	OnClose()
	Error(err error)
}

type DiscoveryConfigurationOption

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

func WithDiscoveryConfiguration

func WithDiscoveryConfiguration(cfg core.DiscoveryConfiguration) DiscoveryConfigurationOption

WithDiscoveryConfiguration allows to setup multicast request. By default it is send to ipv4 and ipv6.

type GetDetailsFunc

type GetDetailsFunc = func(context.Context, *core.Device, schema.ResourceLinks, ...func(message.Options) message.Options) (interface{}, error)

type GetDetailsOption

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

func WithGetDetails

func WithGetDetails(getDetails func(ctx context.Context, d *core.Device, links schema.ResourceLinks, optsArgs ...func(message.Options) message.Options) (interface{}, error)) GetDetailsOption

type GetDeviceByIPOption

type GetDeviceByIPOption = interface {
	// contains filtered or unexported methods
}

GetDeviceByIPOption option definition.

type GetDeviceOption

type GetDeviceOption = interface {
	// contains filtered or unexported methods
}

GetDeviceOption option definition.

type GetDevicesOption

type GetDevicesOption = interface {
	// contains filtered or unexported methods
}

GetDevicesOption option definition.

type GetDevicesWithHandlerOption

type GetDevicesWithHandlerOption = interface {
	// contains filtered or unexported methods
}

type GetOption

type GetOption = interface {
	// contains filtered or unexported methods
}

GetOption option definition.

type OTMType

type OTMType int
const (
	OTMType_Manufacturer OTMType = 0
	OTMType_JustWorks    OTMType = 1
)

type ObserveDevicesOption

type ObserveDevicesOption = interface {
	// contains filtered or unexported methods
}

type ObserveOption

type ObserveOption = interface {
	// contains filtered or unexported methods
}

ObserveOption option definition.

type ObserverConfig added in v2.1.0

type ObserverConfig struct {
	// PollingInterval is a time between two consecutive observations.
	PollingInterval time.Duration
	// FailureThreshold is a number of consecutive observation failures after which the device is marked as offline.
	FailureThreshold uint8
}

ObserverConfig is a configuration of the devices observation.

type OwnOption

type OwnOption = interface {
	// contains filtered or unexported methods
}

OwnOption option definition.

func WithActionAfterOwn added in v2.0.1

func WithActionAfterOwn(actionAfterOwn func(ctx context.Context, client *coap.ClientCloseHandler) error) 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 func(ctx context.Context, client *coap.ClientCloseHandler) (string, error)) 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 WithOTM

func WithOTM(otmType OTMType) OwnOption

WithOTM allows to set ownership transfer method, by default it is manufacturer.

func WithOTMs

func WithOTMs(otmTypes []OTMType) OwnOption

WithOTMs allows to set ownership transfer methods, by default it is []OTMType{manufacturer}. For owning, the first match in order of OTMType with the device will be used.

func WithPresharedKey

func WithPresharedKey(presharedKey []byte) OwnOption

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

type OwnershipStatus

type OwnershipStatus string

OwnershipStatus describes ownership status of the device

const (
	// OwnershipStatus_ReadyToBeOwned the device is ready to be owned.
	OwnershipStatus_ReadyToBeOwned OwnershipStatus = "readytobeowned"
	// OwnershipStatus_Owned the device is owned.
	OwnershipStatus_Owned OwnershipStatus = "owned"
	// OwnershipStatus_OwnedByOther the device is owned by another user.
	OwnershipStatus_OwnedByOther OwnershipStatus = "ownedbyother"
	// OwnershipStatus_Unknown the device is unsecure or cannot obtain his status.
	OwnershipStatus_Unknown OwnershipStatus = "unknown"
)

type ResourceETagOption added in v2.2.1

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

func WithETag added in v2.2.1

func WithETag(etag []byte) ResourceETagOption

type ResourceInterfaceOption

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

func WithInterface

func WithInterface(resourceInterface string) ResourceInterfaceOption

WithInterface updates/gets resource with interface directly from a device.

type ResourceQueryOption added in v2.2.1

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

func WithDeviceID added in v2.3.0

func WithDeviceID(deviceID string) ResourceQueryOption

WithDeviceID updates/gets resource with deviceID also in a query parameter for the bridged devices. Note: it is not needed when client has been created with UseDeviceIDInQuery.

func WithQuery added in v2.2.1

func WithQuery(resourceQuery string) ResourceQueryOption

WithQuery updates/gets resource with a query directly from a device.

type ResourceTypesOption

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

func WithResourceTypes

func WithResourceTypes(resourceTypes ...string) ResourceTypesOption

type SignFunc

type SignFunc = func(ctx context.Context, csr []byte) (signedCsr []byte, err error)

type Signer

type Signer = interface {
	Sign()
}

type UpdateOption

type UpdateOption = interface {
	// contains filtered or unexported methods
}

UpdateOption option definition.

type UseDeviceIDInQueryOption added in v2.3.0

type UseDeviceIDInQueryOption struct {
	UseDeviceID bool
}

func WithUseDeviceID added in v2.3.0

func WithUseDeviceID(useDeviceID bool) UseDeviceIDInQueryOption

WithUseDeviceID appends deviceID to all get requests during discovery. Note: it is not needed when client has been created with UseDeviceIDInQuery.

Directories

Path Synopsis
otm

Jump to

Keyboard shortcuts

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