coap

package
v0.46.0 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: Apache-2.0 Imports: 17 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Mainflux CoAP Adapter

Mainflux CoAP adapter provides an CoAP API for sending messages through the platform.

Configuration

The service is configured using the environment variables presented in the following table. Note that any unset variables will be replaced with their default values.

Variable Description Default
MF_COAP_ADAPTER_PORT Service listening port 5683
MF_COAP_ADAPTER_PLAIN_ENABLED Flag that enables the plain CoAP listener on MF_COAP_ADAPTER_PORT true
MF_COAP_ADAPTER_DTLS_PORT DTLS (coaps) listening port 5684
MF_COAP_ADAPTER_DTLS_IDLE_TIMEOUT Time after which an idle DTLS session is closed 5m
MF_BROKER_URL Message broker instance URL nats://localhost:4222
MF_COAP_ADAPTER_LOG_LEVEL Service log level error
MF_COAP_ADAPTER_CLIENT_TLS Flag that indicates if TLS should be turned on false
MF_COAP_ADAPTER_CA_CERTS Path to trusted CAs in PEM format
MF_JAEGER_URL Jaeger server URL for distributed tracing. Leave empty to disable tracing.
MF_THINGS_AUTH_GRPC_URL Things service Auth gRPC URL localhost:8183
MF_THINGS_AUTH_GRPC_TIMEOUT Things service Auth gRPC request timeout in seconds 1s

Deployment

The service itself is distributed as Docker container. Check the coap-adapter service section in docker-compose to see how service is deployed.

Running this service outside of container requires working instance of the message broker service. To start the service outside of the container, execute the following shell script:

# Download the latest version of the service
git clone https://github.com/MainfluxLabs/mainflux

cd mainflux

# compile the http
make coap

# Copy binary to bin
make install

# set the environment variables and run the service
MF_BROKER_URL=[Message broker instance URL] \
MF_COAP_ADAPTER_PORT=[Service HTTP port] \
MF_COAP_ADAPTER_PLAIN_ENABLED=[Flag that enables the plain CoAP listener] \
MF_COAP_ADAPTER_DTLS_PORT=[DTLS listening port] \
MF_COAP_ADAPTER_DTLS_IDLE_TIMEOUT=[Time after which an idle DTLS session is closed] \
MF_COAP_ADAPTER_LOG_LEVEL=[Service log level] \
MF_COAP_ADAPTER_CLIENT_TLS=[Flag that indicates if TLS should be turned on] \
MF_COAP_ADAPTER_CA_CERTS=[Path to trusted CAs in PEM format] \
MF_JAEGER_URL=[Jaeger server URL] \
MF_THINGS_AUTH_GRPC_URL=[Things service Auth gRPC URL] \
MF_THINGS_AUTH_GRPC_TIMEOUT=[Things service Auth gRPC request timeout in seconds] \
$GOBIN/mainfluxlabs-coap

Usage

DTLS

Over DTLS (coaps://<host>:5684) a thing authenticates in the handshake with a pre-shared key:

  • PSK identity: thing ID
  • PSK: the first 32 hex characters of the SHA-256 hash of the thing key

The PSK is derived from the key because constrained DTLS stacks (mbedTLS, tinydtls, nRF91 modems) accept PSKs of at most 32 bytes, and the key is longer. Compute it with:

echo -n <thing_key> | sha256sum | cut -c1-32

A wrong key or an unknown thing ID fails the handshake. Requests over the session need no auth and type query parameters, and the key is never sent over the network. The adapter supports the TLS_PSK_WITH_AES_128_CCM_8 and TLS_PSK_WITH_AES_128_GCM_SHA256 cipher suites.

The adapter supports DTLS Connection IDs (RFC 9146). A client that negotiates one keeps its session when NAT changes its address or port, instead of repeating the handshake.

A session with no traffic from the thing for MF_COAP_ADAPTER_DTLS_IDLE_TIMEOUT is closed, together with its observations. A thing that only observes commands should send something more often, for example a message or a CoAP ping, which also keeps carrier NAT mappings alive.

PSK=$(echo -n <thing_key> | sha256sum | cut -c1-32)
coap-client-openssl -m post -e '{"v":1}' -u <thing_id> -k "$PSK" "coaps://localhost/things/<thing_id>/messages"
Plain CoAP

Plain CoAP sends everything in cleartext, including the thing key, so it should be disabled with MF_COAP_ADAPTER_PLAIN_ENABLED=false once things use DTLS.

Since CoAP protocol does not support Authorization header (option) and since options have a limited size, authentication is supplied in the form of URI Query parameters:

  • auth
    • Thing authentication key
  • type
    • Type of Thing key. One of: external, internal.
Publishing messages

To publish a message to a thing, optionally with a subtopic:

coap://localhost/things/<thing_id>/messages?auth=<thing_key>&type=<key_type>
coap://localhost/things/<thing_id>/messages/<subtopic>?auth=<thing_key>&type=<key_type>

The thing is determined by the auth key, not the path.

Sending commands

To send a command to a specific thing (M2M, authorized by thing key):

coap://localhost/things/<thing_id>/commands?auth=<thing_key>&type=<key_type>

To send a command to all things in a group:

coap://localhost/groups/<group_id>/commands?auth=<thing_key>&type=<key_type>

Both endpoints support an optional subtopic:

coap://localhost/things/<thing_id>/commands/<subtopic>?auth=<thing_key>&type=<key_type>
coap://localhost/groups/<group_id>/commands/<subtopic>?auth=<thing_key>&type=<key_type>

Commands use POST (same as publishing messages). The thing must have command permission over the target thing or group.

Documentation

Overview

Package coap contains the domain concept definitions needed to support Mainflux CoAP adapter service functionality. All constant values are taken from RFC, and could be adjusted based on specific use case.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client interface {
	// In CoAP terminology, Token similar to the Session ID.
	Token() string
	Handle(subject string, m protomfx.Message) error
	HandleCommand(subject string, cmd protomfx.Command) error
	Cancel() error
	Done() <-chan struct{}
}

Client wraps CoAP client.

func NewClient

func NewClient(c mux.Conn, tkn message.Token, l logger.Logger) Client

NewClient instantiates a new Observer.

type PubSub added in v0.42.0

PubSub specifies the minimal publish/subscribe capability the CoAP adapter needs.

type Service

type Service interface {
	// Publish Message
	Publish(ctx context.Context, key domain.ThingKey, msg protomfx.Message) error

	// Subscribe subscribes to profile with specified id, subtopic and adds subscription to
	// service map of subscriptions under given ID.
	Subscribe(ctx context.Context, key domain.ThingKey, subtopic string, c Client) error

	// Unsubscribe method is used to stop observing resource.
	Unsubscribe(ctx context.Context, key domain.ThingKey, subtopic, token string) error

	// RemoveObserver ends the observation of the given client, unless another client
	// has since observed the subtopic with the same token.
	RemoveObserver(subtopic string, c Client) error

	// SendCommandToThing publishes a command to the specified thing, authorized by thing key (M2M).
	SendCommandToThing(ctx context.Context, key domain.ThingKey, recipientID string, cmd protomfx.Command) error

	// SendCommandToGroup publishes a command to a group, authorized by thing key (M2M).
	SendCommandToGroup(ctx context.Context, key domain.ThingKey, groupID string, cmd protomfx.Command) error
}

Service specifies CoAP service API.

func New

func New(things domain.ThingsClient, pubsub PubSub) Service

New instantiates the CoAP adapter implementation.

Directories

Path Synopsis
Package api contains API-related concerns: endpoint definitions, middlewares and all resource representations.
Package api contains API-related concerns: endpoint definitions, middlewares and all resource representations.

Jump to

Keyboard shortcuts

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