Crate embedded_jsonrpc

Source
Expand description

§JSON-RPC for Embedded Systems

This crate provides a JSON-RPC server implementation for embedded systems.

§Features

  • #![no_std] Support: Fully compatible with environments lacking a standard library.
  • Predictable Memory Usage: Zero dynamic allocation with statically sized buffers.
  • Async: Non-blocking I/O with embedded-io-async.
  • Client Compatibility: Uses LSP style framing for JSON-RPC messages.
  • Error Handling: Adheres to JSON-RPC standards with robust error reporting.

§Example Usage

§Create an RPC Server

use embedded_jsonrpc::{RpcError, RpcResponse, RpcServer, RpcHandler, JSONRPC_VERSION, DEFAULT_STACK_SIZE};
use embedded_jsonrpc::stackfuture::StackFuture;

struct MyHandler;

impl RpcHandler for MyHandler {
   fn handle<'a>(&self, id: Option<u64>, _request_json: &'a [u8], response_json: &'a mut [u8]) -> StackFuture<'a, Result<usize, RpcError>, DEFAULT_STACK_SIZE> {
      StackFuture::from(async move {
         let response: RpcResponse<'static, ()> = RpcResponse {
           jsonrpc: JSONRPC_VERSION,
           error: None,
           result: None,
           id,
         };
        Ok(serde_json_core::to_slice(&response, response_json).unwrap())
     })
  }
}

let mut server: RpcServer<'_> = RpcServer::new();
server.register_method("echo", &MyHandler);

§Serve Requests

let mut stream: YourAsyncStream = YourAsyncStream::new();
server.serve(&mut stream).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§

  • This crate defines a StackFuture wrapper around futures that stores the wrapped future in space provided by the caller. This can be used to emulate dyn async traits without requiring heap allocation.

Structs§

Enums§

Constants§

Traits§