Crates.io | flem |
lib.rs | flem |
version | 0.6.2 |
source | src |
created_at | 2023-07-07 16:38:54.85159 |
updated_at | 2023-11-29 21:15:56.574042 |
description | Flexible, Light-weight, Embedded Messaging Protocol |
homepage | |
repository | https://github.com/BridgeSource/flem-rs.git |
max_upload_size | |
id | 910930 |
size | 81,039 |
FLEM stands for Flexible, Light-weight, Embedded Messaging and is a Little Endian messaging protocol intended for use in communicating with embedded systems targets over numerous types of buses.
Channel
trait. This trait requires features = ["std"]. It servers as a set of traits that can be used
to implement different hardware or to emulate a device. It uses the std
library for threading and mpsc
channels.Channel
trait.At its core, FLEM has packets composed of a header, and a data payload. The header is 10 bytes and consists of:
data
buffer.
Can be 0 to u16::MAX.A packet should be reset before reuse. There are 2 methods provided to do this:
reset()
- Performs a full reset of the packet, by zeroing out all bytes
including zeroing out the data bytes.lazy_reset()
- Resets the Checksum, Request, Response, and Length bytes.After data is added, the packet should be packed where the header, checksum, request, response, length are all configured and the checksum computed on the following bytes:
In Rust, a few convenience functions are provided to lazy_reset
the packet,
add data, set the header bytes, and perform the pack operation:
pack_data
- Adds data to a packet with a Response byte of SUCCESS
and the
Request byte set by the user.pack_error
- Adds data to a packet with Request and Response bytes specified
by the user. If no data needs to be transmitted, use an empty data array &[]
.Note: If no data is to be transmitted, set data to an empty data array
&[]
.
The header is a value of 0x5555 and represents a set of bytes that can be scanned quickly to determine the start of a packet. This may be expanded in the future to allow for other Header byte patterns.
A CRC-16 (IBM) checksum that can be used to ensure the data was transmitted and received without error. The checksum calculation does not include the header or the checksum bytes; ensure they are either zero or skipped if implemented in another language.
Typically, a host sends a 2-byte request to a client. A request doesn't need to have any data payload, in which case a simple request packet is 10 bytes. A response packet should always echo the request to ensure the partner device can double check and route the response correctly.
Note, the client is not required to respond. The client responses help ensure commands were sent and processed successful (or not). It will also help validate that the hardware transmission of data is performing as intended via the checksum.
Requests are mostly left up to the user to implement, though there is one pre-defined:
DataId
struct that
indicates a version, serial number, name, or some other information (30 bytes,
char) and a u16 indicating the partners max packet size. This requires that
the client / host use packet sizes of at least 30 bytes. Smaller Ids can be
used, or not responded to, but it is up to the user to implement.Our company has a separate project that has all of the responses and requests for each project in a different Rust sub-module. Typically, each project has something like:
pub mod host_requests {
pub const READ_CONFIG: u16 = ...;
pub const WRITE_CONFIG: u16 = ...;
}
pub mod client_requests {
pub const INTERRUPT: u16 = ...;
pub const DATA_ACQUIRED: u16 = ...;
}
Responses are 2-byte codes that indicate the status of the partner device, if needed.
There are some reserved non-event responses:
Two bytes indicating the amount of data to expect in the packets data field. This can be 0 to u16::MAX, though typically it would be something smaller.
The packet data payload. Can be 0 to u16::MAX bytes.
FLEM offers a DataInterface
trait that can be implemented on a struct that
allow the user to encode and decode the struct into a FLEM packet. In general,
check that the struct can fit in the packet when encoding, and that the packet
has enough data to fit into the struct when decoding.
Once this check is done, the data can be moved into or out of the struct, making
sure that the encoding and decoding order is consistent. See examples/traits.rs
for an example. There are convenience functions in src/buffer.rs
that can be
used when decoding, with unit tests for these functions in tests/tests.rs
.
See examples/example.rs
for a host to client request and a client to host
response.