Expand description
§JSON-RPC for Embedded Systems
This crate provides a simple JSON-RPC server implementation for embedded systems.
§Features
- no_std Support: Compatible with
#![no_std]environments. - Zero Alloc: Uses statically allocated buffers and
heaplesscontainers for predictable memory usage. - Async: Non-blocking IO with
embedded-io-async. - Varint Framing: Efficient message framing and length-prefix encoding using protobuf style varints.
- Error Handling: Includes comprehensive error codes and descriptions based on the JSON-RPC specification.
§Core Components
§JSON-RPC Data Structures
RpcRequest: Represents a JSON-RPC request, including the method and optional parameters.RpcResponse: Represents a JSON-RPC response, including the result or error information.RpcErrorandRpcErrorCode: Encapsulate errors with standard codes and descriptions.
§RPC Server
RpcServer: Core server to manage method registration and handle incoming requests.- Handlers: Define functions for specific RPC methods using a type-safe
RpcHandler. serve: Processes incoming requests and sends responses over anembedded-io-asyncstream.
§Example Usage
§Create an RPC Server
use embedded_jsonrpc::{RpcServer, RpcResponse, RpcError, RpcErrorCode, JSONRPC_VERSION};
const MAX_HANDLERS: usize = 4;
const MAX_REQUEST_LEN: usize = 256;
const MAX_RESPONSE_LEN: usize = 256;
let mut server: RpcServer<'_, MAX_HANDLERS, MAX_RESPONSE_LEN> = RpcServer::new();
server.register_method("echo", |id, _request_json, response_json| {
let response = RpcResponse {
jsonrpc: JSONRPC_VERSION,
id,
error: None,
};
serde_json_core::to_slice(&response, response_json).unwrap()
});§Serve Requests
ⓘ
let mut stream: YourAsyncStream = YourAsyncStream::new();
let mut rpc_buffer = [0u8; MAX_REQUEST_LEN];
server.serve(&mut stream, &mut rpc_buffer).await.unwrap();§License
This crate is licensed under the Mozilla Public License 2.0 (MPL-2.0). See the LICENSE file for more details.
§References
Modules§
- Varint Encoding and Decoding Library
Structs§
- JSON-RPC Error structure
- JSON-RPC Request structure
- JSON-RPC Response structure
- RPC server
Enums§
- JSON-RPC Standard Error Codes
Constants§
- JSON-RPC Version Currently only supports version 2.0 https://www.jsonrpc.org/specification
Type Aliases§
- Type for RPC handler functions