LCSF Stack Rust is a Rust implementation of the LCSF (Light Command Set Format).
This adds a software layer to easily encode/decode custom command sets protocols based on LCSF to your project.
LCSF is a specification to easily create and deploy custom command sets.
For more information on the project, check the official LCSF documentation here.
WIP Install as source code or crate
Then, to interface with your project:
- Create a custom protocol either by hand or by using the LCSF Generator (recommended).
- Instantiate a
LcsfCore
object with the desired parameters, example of how to use this object can be found in this repo'smain.rs
.
The lcsf_lib
itself is composed of 4 files:
lcsf_transcoder
: Serialize/DeserializeLcsfRawMsg
objects to and frombyte array
.lcsf_validator
: Validate/EncodeLcsfRawMsg
intoLcsfValidCmd
following a protocol descriptor objectLcsfProtDesc
.lcsf_error
: Handle the processing/creation of the built-in LCSF Error Protocol. For more information on the error protocol, check the LCSF documentation.lcsf_core
: The core file that links all the other parts together into a simple to useLcsfCore
object.
If you use LCSF Generator, you will get two more files per protocol:
lcsf_protocol_<name>
: An lcsf abstraction layer that will convert anLcsfValidCmd
into a protocol specific, easier to use,CmdPayload
. Also contains your protocol'sLcsfProtDesc
.protocol_<name>
: A skeleton of application file to process the received commands you need to fill. Contains aninit_core
function to give anLcsfCore
the protocol's details.
LcsfCore
is the main object that is used to process lcsf messages. It has 5 methods:
new
: Create anLcsfCore
object.update_err_cb
: Change the function called when an lcsf error protocol message is received.add_protocol
: Add your custom protocolLcsfProtDesc
, allowing the core to process messages from this protocol.receive_buff
: Process an incoming lcsf message as a byte array.send_cmd
: Process an outgoing command.receive_raw
: Deserialize a lcsf message, if you want to skip protocol handlingsend_raw
: Serialize aLcsfRawMsg
, if you want to skip protocol handling
When instantiating your LcsfCore
object with new()
you have to feed three parameters:
mode: LcsfModeEnum
, indicates the lcsf representation to use, either Small or Normal. For more information on lcsf representation, check the LCSF documentation.send_cb: fn pointer
, a function pointer to receive the encoded lcsf frame and send them wherever they're needed.do_gen_err: bool
, indicates if the module will generate an lcsf error protocol message when decoding an incoming message fails.
At creation, your LcsfCore
can only handle the default lcsf error protocol. You need to give it the LcsfProtDesc
and corresponding callback with the add_protocol()
method. This is easily done by calling your protocol's init_core
function.
Once your LcsfCore
is init, here's how receiving and sending commands work in a nutshell:
When receiving an lcsf message as an array of bytes from whatever network link you're using, the application shall send it to an LcsfCore
object that will do the processing:
-
- The buffer is deserialized into an
LcsfRawMsg
by thelcsf_transcoder module
- The buffer is deserialized into an
-
- The
LcsfRawMsg
is validated into anLcsfValidCmd
by thelcsf_validator
module, using the availableLcsfProtDesc
- The
-
- The
LcsfValidCmd
is passed along to the corresponding protocol callback
- The
-
- The protocol callback asks its specific
lcsf_protocol
module to turn theLcsfValidCmd
into a specificCmdPayload
than can be easily used by your application
- The protocol callback asks its specific
- err) The
lcsf_error
module is called if an error occurred during transcoding and validation or if the packet itself is anlcsf error protocol
message
When sending a protocol specific CmdPayload
, your application should use the protocol
module to do the processing:
-
- The
CmdPayload
will be turned into aLcsfValidCmd
by thelcsf_protocol
module
- The
-
- The
LcsfValidCmd
is sent to theLcsfCore
- The
-
- The
LcsfValidCmd
is turned into aLcsfRawMsg
by thelcsf_validator
module, using the correspondingLcsfProtDesc
- The
-
- The
LcsfRawMsg
is serialized into a byte array by thelcsf_transcoder module
- The
-
- The lcsf message is sent to the send callback for processing
Since LCSF is based on nested structures, the stack use recursive functions.
Recursivity can be frowned upon, which is why the stack is made to limit the issue:
- The number of calls is directly linked to the number of sub-attribute layers in a protocol, that means the user has direct control.
- The stack is linear in its recursivity (one call will only lead to a maximum of one other call).
If you want to run the project as is use cargo run
To run the test suite use cargo test
To generate the doc use cargo doc --no-deps
, you can access the documentation at target/doc/help.html
.
WIP do some benchmarking