Expand description
§PK Command
A lightweight, reliable data transfer protocol designed for constrained channels (e.g., HID, Serial) between a Host and an Embedded Device.
§Key Features
- Reliability: Built-in ACK/Retransmission mechanism.
- Efficiency: Fixed-length headers and data slicing for small MTUs.
- Flexibility: Supports variable access (GET/SET) and remote method invocation (INVOK).
- no_std Support: Core logic is compatible with embedded systems without an OS. Note that
allocis still used in no_std environments. - Wait Mechanism: Keep-alive
AWAITpackets for long-running operations.
§Architecture
The protocol operates using Transaction Chains. A chain starts with a START
command and ends with ENDTR. Large payloads are automatically sliced into
SDATA packets.
§Command Format
Every command follows the fixed layout:
[MSG ID][OPERATION NAME] [OBJECT] [DATA].
MSG ID: A base-94 encoded unsigned integer (using ASCII characters from ‘!’ to ‘~’) that uniquely identifies the command within a transaction.OPERATION NAME: A 5-character ASCII string representing the command type. All the operations defined in the specification are defined in this library. (Seetypes::Operationfor details.)OBJECT: An optional 5-character ASCII string that provides additional context for the operation (e.g., variable name, method name).DATA: An optional binary payload that carries parameters or return values. It can be of arbitrary length and may contain any byte values.
See Command for a structured representation of commands and utilities for parsing and serialization.
Note that:
OBJECTis either omitted, or exactly 5 ASCII characters.DATA(payload) is arbitrary binary data.- The total length of a command is limited by the transportation layer’s MTU (e.g., 64 bytes for HID). (See
PkCommandConfig.) But the protocol handles slicing and reassembly of large payloads automatically, so you can work with large data without worrying about the underlying transport constraints.
§Example
use pk_command::{PkCommand, PkCommandConfig, PkHashmapMethod, PkHashmapVariable};
// 1. Setup configuration and accessors
let config = PkCommandConfig::default(64);
let vars = PkHashmapVariable::new(vec![]);
let methods = PkHashmapMethod::new(vec![]);
// 2. Initialize the state machine
let pk = PkCommand::<_, _, std::time::Instant>::new(config, vars, methods);
// 3. Basic loop driving the protocol
loop {
// Handle received bytes from your transport (HID/Serial/etc.)
if let Some(bytes) = transport.recv() {
pk.incoming_command(bytes);
}
// Process and get commands to send back
if let Some(cmd) = pk.poll() {
transport.send(cmd.to_bytes());
}
if pk.is_complete() {
break;
}
}§Feature flags
std: Enables features that require the Rust standard library. (Mainly the convenient wrappers likePkPromise,PkHashmapVariable,PkHashmapMethod) Enabled by default.embassy: Enables integration with the Embassy async framework. Flags below are also enabled when this is active:embassy-time: Enables the support for embassy-time crate, which provides timekeeping utilities for embedded environments.embassy-runtime: Enables the support for embassy-executor crate, which provides integration between the main state machine and Embassy async tasks.
tokio-runtime: Enables integration with the Tokio async runtime. Providestokio_adapterfor running async operations within method implementations. Requiresstdfeature.smol-runtime: Enables integration with the Smol async executor. Providessmol_adapterfor running async operations within method implementations. Requiresstdfeature.
Modules§
- embassy_
adapter embassy-runtime - Embassy runtime adapters.
- msg_id
std - Module for handling PK Command Message IDs.
- smol_
adapter stdandsmol-runtime - Smol runtime adapters.
- tokio_
adapter stdandtokio-runtime - Tokio runtime adapters.
- types
- Core data structures and types for PK Command.
Macros§
- embassy_
method_ accessor embassy-runtime - Helper macro for creating a
PkMethodAccessorbacked by Embassy tasks.
Structs§
- Embassy
Instant embassy-time - A
PkInstantadapter forembassy_time::Instant. - PkCommand
- The main state machine for handling the PK Command protocol.
- PkCommand
Config - Configuration for the
PkCommandstate machine. - PkHashmap
Method std - A wrapper for
std::collections::HashMapthat implements thePkMethodAccessortrait. - PkHashmap
Variable std - A wrapper for
std::collections::HashMapthat implements thePkVariableAccessortrait. - PkPromise
std - A simple implementation of
Pollablethat executes tasks in a background thread.
Traits§
- PkInstant
- Trait representing an instant in time.
- PkMethod
Accessor - Trait defining how to invoke methods by their string key.
- PkVariable
Accessor - Trait defining how to access (get/set) variables by their string key.
- Pollable
- A handle for a long-running operation that can be polled for completion.