Coapum
A modern, ergonomic CoAP (Constrained Application Protocol) library for Rust with support for DTLS, observers, and asynchronous handlers.
Features
- ๐ Async/await support - Built on Tokio for high-performance async networking
- ๐ก๏ธ DTLS security - Full DTLS 1.2 support with PSK authentication
- ๐ฏ Ergonomic routing - Express-like routing with automatic parameter extraction
- ๐๏ธ Observer pattern - CoAP observe support with persistent storage backends
- ๐ฆ Multiple payload formats - JSON, CBOR, and raw byte support
- ๐ง Type-safe extractors - Automatic request parsing with compile-time guarantees
- ๐๏ธ Pluggable storage - Memory and Sled database backends for observers
- ๐งช Comprehensive testing - High test coverage with benchmarks
Quick Start
Add Coapum to your Cargo.toml:
[]
= "0.2.0"
# For standalone SenML usage
= "0.1.0"
Basic Server
use ;
use ;
// Handler with automatic JSON deserialization and path parameter extraction
async
// Observer handler for device state notifications
async
async
Secure DTLS Server
use ;
use ;
async
Client Example
use ;
use UdpSocket;
use Arc;
async
Core Concepts
Routing
Coapum provides an ergonomic routing system inspired by web frameworks:
let router = new
.get // GET with path parameter
.post // POST with JSON body
.put // PUT with path + body
.delete // DELETE
.observe // Observer pattern
.build;
Extractors
Coapum automatically extracts data from requests using type-safe extractors:
Path<T>- Extract path parametersJson<T>- Parse JSON payloadCbor<T>- Parse CBOR payloadSenML- Parse SenML (Sensor Measurement Lists) payloadBytes- Raw byte payloadRaw- Raw payload dataState<T>- Access shared application stateIdentity- Client identity from DTLSObserveFlag- CoAP observe optionSource- Request source information
async
// SenML handler example
async
Observer Pattern
CoAP's observe mechanism is fully supported with persistent storage:
// Register observer endpoint
.observe
// Get handler - returns current value
async
// Notify handler - called when sending updates to observers
async
SenML Support
Coapum includes built-in support for Sensor Measurement Lists (SenML) RFC 8428:
use SenML;
use SenMLBuilder;
// Handler accepting SenML sensor data
async
SenML supports multiple formats:
- JSON - Standard SenML JSON format
- CBOR - Compact binary format for IoT devices
- XML - Legacy XML format (with
xmlfeature)
Storage Backends
Choose from multiple observer storage backends:
// In-memory (for testing/development)
let observer = new;
// Persistent storage with Sled
let observer = new.unwrap;
Configuration
Server Configuration
use Config;
let config = Config ;
DTLS Configuration
use ;
let dtls_config = Config ;
Feature Flags
[]
= { = "0.2.0", = ["sled-observer"] }
= { = "0.1.0", = ["json", "cbor", "xml"] }
Coapum Features
sled-observer- Enable Sled database backend for observers (default)
SenML Features
json- JSON serialization support (default)cbor- CBOR serialization support (default)xml- XML serialization supportvalidation- Input validation support
Examples
The examples/ directory contains complete examples:
cbor_server.rs- CBOR payload handling with device state managementcbor_client.rs- CBOR client implementationraw_server.rs- Raw payload handlingraw_client.rs- Raw client implementationsenml_example.rs- Advanced SenML payload handling with time-series datasenml_simple.rs- Simple SenML payload handling demonstrationconcurrency.rs- Concurrent request handlingdynamic_client_management.rs- Dynamic client management exampleexternal_state_updates.rs- External state update handling
Run an example:
# Start CBOR server
# In another terminal, run client
Testing
Run the test suite:
# Run all tests
# Run with logging
RUST_LOG=debug
# Run specific test module
Benchmarks
# Run router benchmarks
Code Coverage
Install grcov and generate coverage reports:
# Generate coverage data
CARGO_INCREMENTAL=0 RUSTFLAGS='-Cinstrument-coverage' \
LLVM_PROFILE_FILE='cargo-test-%p-%m.profraw'
# Generate HTML report
# Generate LCOV report
Architecture
Coapum is built with the following principles:
- Async-first: Built on Tokio for high-performance async I/O
- Type safety: Extensive use of Rust's type system to prevent runtime errors
- Ergonomics: API design inspired by modern web frameworks
- Modularity: Pluggable components for storage, security, and serialization
- Performance: Zero-copy parsing and efficient routing algorithms
Key Components
- Router: Route matching and handler dispatch
- Extractors: Type-safe request data extraction
- Handlers: Function-based request handling
- Observers: CoAP observe pattern implementation
- DTLS: Secure transport layer
- Config: Server and security configuration
Contributing
We welcome contributions! Please see CONTRIBUTING.md for guidelines.
Development Setup
# Clone the repository
# Run tests
# Run clippy for linting
# Format code
License
This project is licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Acknowledgments
- Built on the excellent coap-lite library
- DTLS implementation from webrtc-rs
- Routing powered by route-recognizer
- Storage backend using sled
For more information, see the API documentation.