mbus_ffi/lib.rs
1//! WASM and Native C/C++ bindings for the Modbus workspace.
2//!
3//! Provides both WASM browser bindings and native C FFI bindings for the modbus-rs stack.
4//!
5//! Browser-facing APIs (feature `wasm`, `wasm32` target only):
6//! - `WasmModbusClient` for WebSocket/TCP gateway usage
7//! - `WasmSerialModbusClient` for Web Serial (RTU/ASCII)
8//! - `request_serial_port` and `WasmSerialPortHandle` helpers
9//! - `WasmServerBindingPlan` and `WasmServerTransportKind` (server binding design surface)
10//!
11//! Native C FFI — client (feature `c`, non-wasm targets):
12//! - `mbus_tcp_client_new` / `mbus_serial_client_new` — create clients, return an ID
13//! - `mbus_tcp_poll` / `mbus_serial_poll` — drive the request/response state machine
14//! - Per-operation request functions (coils, registers, discrete inputs, etc.)
15//! - Response data delivered via C function-pointer callbacks in `MbusCallbacks`
16//!
17//! Native C FFI — server (feature `c-server`, non-wasm targets):
18//! There are two server integration approaches:
19//!
20//! **Approach 1 — hand-written handlers** (`c_server_demo`):
21//! Wire FC callbacks directly in `main.c` via `mbus_tcp_server_new`.
22//! No code generation required.
23//!
24//! **Approach 2 — YAML-driven codegen** (`c_server_demo_yaml`):
25//! Declare the device register/coil map in a YAML file. `build.rs` reads
26//! `MBUS_SERVER_APP_CONFIG` at compile time and generates a type-safe Rust
27//! dispatcher into `$OUT_DIR` (never tracked in git). `gen-server-app` produces
28//! the matching C header (`mbus_server_app.h`).
29//!
30//! ```text
31//! MBUS_SERVER_APP_CONFIG=/path/to/server_app.yaml cargo build -p mbus-ffi --features c-server
32//! ```
33#![cfg_attr(all(not(doc), not(has_unwind), not(target_arch = "wasm32")), no_std)]
34#![warn(missing_docs)]
35
36#[cfg(all(not(doc), not(has_unwind), not(target_arch = "wasm32")))]
37use core::panic::PanicInfo;
38
39#[cfg(all(not(doc), not(has_unwind), not(target_arch = "wasm32")))]
40#[panic_handler]
41fn panic_handler(_info: &PanicInfo) -> ! {
42 loop {}
43}
44
45// Pool configuration generated by build.rs from MBUS_MAX_TCP_CLIENTS and MBUS_MAX_SERIAL_CLIENTS env vars.
46include!(concat!(env!("OUT_DIR"), "/pool_config.rs"));
47
48#[cfg(all(
49 target_arch = "wasm32",
50 any(feature = "wasm-client", feature = "wasm-server")
51))]
52mod wasm;
53
54#[cfg(all(target_arch = "wasm32", feature = "wasm-client"))]
55pub use wasm::{WasmModbusClient, WasmWsTransport};
56
57#[cfg(all(target_arch = "wasm32", feature = "wasm-client"))]
58pub use wasm::{
59 ModbusErrorCode, WasmAsciiTransport, WasmRtuTransport, WasmSerialModbusClient,
60 WasmSerialPortHandle, get_modbus_error_code, request_serial_port,
61};
62
63#[cfg(all(target_arch = "wasm32", feature = "wasm-server"))]
64pub use wasm::{
65 WasmSerialServer, WasmSerialServerOptions, WasmServerTransportKind, WasmTcpServer,
66 WasmTcpServerOptions,
67};
68
69#[allow(missing_docs)]
70#[cfg(any(
71 feature = "c-client",
72 feature = "c-server",
73 feature = "c-gateway",
74 feature = "dotnet",
75 feature = "go",
76))]
77pub mod c;
78
79#[cfg(any(feature = "python-client", feature = "python-server"))]
80#[allow(missing_docs)]
81pub mod python;
82
83#[cfg(all(feature = "dotnet", not(target_arch = "wasm32")))]
84#[allow(missing_docs)]
85pub mod dotnet;
86
87#[cfg(all(feature = "go", not(target_arch = "wasm32")))]
88#[allow(missing_docs)]
89pub mod go;
90
91#[cfg(all(
92 any(
93 feature = "nodejs-client",
94 feature = "nodejs-server",
95 feature = "nodejs-gateway"
96 ),
97 not(target_arch = "wasm32")
98))]
99#[allow(missing_docs)]
100/// cbindgen:ignore
101pub mod nodejs;