coap-lite

Latest version Documentation License

A lightweight, #![no_std] CoAP message manipulation crate, ideal for embedded environments.

It's based on the improved low-level message handling code from the [coap] crate, made to work in bare metal environments.

Overview

This crate provides several types that can be used to build, modify and encode/decode CoAP messages to/from their byte representation.

It does require allocation, so you might have to set a global allocator depending on your target.

Usage

```rust use coap_lite::{ CoapOption, MessageClass, MessageType, Packet, RequestType, ResponseType, };

let mut request = Packet::new(); request.header.messageid = 23839; request.header.code = MessageClass::Request(RequestType::Get); request.settoken(vec![0, 0, 57, 116]); request.addoption(CoapOption::UriHost, b"localhost".tovec()); request.addoption(CoapOption::UriPath, b"tv1".tovec()); asserteq!( [ 0x44, 0x01, 0x5D, 0x1F, 0x00, 0x00, 0x39, 0x74, 0x39, 0x6C, 0x6F, 0x63, 0x61, 0x6C, 0x68, 0x6F, 0x73, 0x74, 0x83, 0x74, 0x76, 0x31, ], request.tobytes().unwrap()[..] );

let response = Packet::frombytes(&[ 0x64, 0x45, 0x5D, 0x1F, 0x00, 0x00, 0x39, 0x74, 0xFF, 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64, 0x21, ]) .unwrap(); asserteq!(23839, response.header.messageid); asserteq!( MessageClass::Response(ResponseType::Content), response.header.code ); asserteq!(MessageType::Acknowledgement, response.header.gettype()); asserteq!([0, 0, 57, 116], response.gettoken()[..]); assert_eq!(b"Hello World!", &response.payload[..]); ```

License

Licensed under either of

at your option.

This is a modification of the coap crate, its license is in LICENSE-3RD-PARTY.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.