mbus-ffi 0.13.0

Native C FFI and browser WASM bindings for modbus-rs client APIs, with optional generated server bindings
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
use core::ffi::c_void;

use crate::c::error::MbusStatusCode;

// ── Opaque Model Types ───────────────────────────────────────────────────────

#[cfg(feature = "coils")]
use super::models::coils::MbusCoils;
#[cfg(feature = "discrete-inputs")]
use super::models::discrete_inputs::MbusDiscreteInputs;
#[cfg(feature = "fifo")]
use super::models::fifo::MbusFifoQueue;
#[cfg(feature = "holding-registers")]
use super::models::registers::MbusHoldingRegisters;
#[cfg(feature = "input-registers")]
use super::models::registers::MbusInputRegisters;

// ── File-record response view type ───────────────────────────────────────────

/// A single sub-request result returned in the `on_read_file_record` callback.
///
/// `data` points into Rust-owned memory and is **valid only during the callback
/// invocation**. Copy the slice if you need to retain it.
#[cfg(feature = "file-record")]
#[repr(C)]
pub struct MbusFileRecordResult {
    /// Record number for this sub-request.
    pub record_number: u16,
    /// Pointer to the register data for this sub-request chunk.
    pub data: *const u16,
    /// Number of u16 words in `data`.
    pub data_len: u16,
}

// ── Callback context structs ─────────────────────────────────────────────────

// ── Coil callbacks (feature "coils") ─────────────────────────────────────────

#[cfg(feature = "coils")]
#[repr(C)]
/// Context passed to the read-coils callback.
pub struct MbusReadCoilsCtx {
    /// Transaction ID.
    pub txn_id: u16,
    /// Unit / slave ID.
    pub unit_id: u8,
    /// Opaque pointer to the coils data (valid during this callback only).
    pub coils: *const MbusCoils,
    /// User-provided opaque pointer.
    pub userdata: *mut c_void,
}

#[cfg(feature = "coils")]
#[repr(C)]
/// Context passed to the write-single-coil callback.
pub struct MbusWriteSingleCoilCtx {
    /// Transaction ID.
    pub txn_id: u16,
    /// Unit / slave ID.
    pub unit_id: u8,
    /// Coil address.
    pub address: u16,
    /// Written value (1 = ON, 0 = OFF).
    pub value: u8,
    /// User-provided opaque pointer.
    pub userdata: *mut c_void,
}

#[cfg(feature = "coils")]
#[repr(C)]
/// Context passed to the write-multiple-coils callback.
pub struct MbusWriteMultipleCoilsCtx {
    /// Transaction ID.
    pub txn_id: u16,
    /// Unit / slave ID.
    pub unit_id: u8,
    /// Starting coil address.
    pub address: u16,
    /// Number of coils written.
    pub quantity: u16,
    /// User-provided opaque pointer.
    pub userdata: *mut c_void,
}

// ── Register callbacks (feature "registers") ─────────────────────────────────

#[cfg(feature = "holding-registers")]
#[repr(C)]
/// Context passed to the read-holding-registers callback.
pub struct MbusReadHoldingRegistersCtx {
    /// Transaction ID.
    pub txn_id: u16,
    /// Unit / slave ID.
    pub unit_id: u8,
    /// Opaque pointer to registers data.
    pub registers: *const MbusHoldingRegisters,
    /// User-provided opaque pointer.
    pub userdata: *mut c_void,
}

#[cfg(feature = "input-registers")]
#[repr(C)]
/// Context passed to the read-input-registers callback.
pub struct MbusReadInputRegistersCtx {
    /// Transaction ID.
    pub txn_id: u16,
    /// Unit / slave ID.
    pub unit_id: u8,
    /// Opaque pointer to registers data.
    pub registers: *const MbusInputRegisters,
    /// User-provided opaque pointer.
    pub userdata: *mut c_void,
}

#[cfg(feature = "holding-registers")]
#[repr(C)]
/// Context passed to the read-write-multiple-registers callback.
pub struct MbusReadWriteMultipleRegistersCtx {
    /// Transaction ID.
    pub txn_id: u16,
    /// Unit / slave ID.
    pub unit_id: u8,
    /// Opaque pointer to registers data.
    pub registers: *const MbusHoldingRegisters,
    /// User-provided opaque pointer.
    pub userdata: *mut c_void,
}

#[cfg(feature = "holding-registers")]
#[repr(C)]
/// Context passed to the write-single-register callback.
pub struct MbusWriteSingleRegisterCtx {
    /// Transaction ID.
    pub txn_id: u16,
    /// Unit / slave ID.
    pub unit_id: u8,
    /// Register address.
    pub address: u16,
    /// Written value.
    pub value: u16,
    /// User-provided opaque pointer.
    pub userdata: *mut c_void,
}

#[cfg(feature = "holding-registers")]
#[repr(C)]
/// Context passed to the write-multiple-registers callback.
pub struct MbusWriteMultipleRegistersCtx {
    /// Transaction ID.
    pub txn_id: u16,
    /// Unit / slave ID.
    pub unit_id: u8,
    /// Starting register address.
    pub address: u16,
    /// Number of registers written.
    pub quantity: u16,
    /// User-provided opaque pointer.
    pub userdata: *mut c_void,
}

#[cfg(feature = "holding-registers")]
#[repr(C)]
/// Context passed to the mask-write-register callback.
pub struct MbusMaskWriteRegisterCtx {
    /// Transaction ID.
    pub txn_id: u16,
    /// Unit / slave ID.
    pub unit_id: u8,
    /// User-provided opaque pointer.
    pub userdata: *mut c_void,
}

// ── Discrete input callbacks (feature "discrete-inputs") ──────────────────────

#[cfg(feature = "discrete-inputs")]
#[repr(C)]
/// Context passed to the read-discrete-inputs callback.
pub struct MbusReadDiscreteInputsCtx {
    /// Transaction ID.
    pub txn_id: u16,
    /// Unit / slave ID.
    pub unit_id: u8,
    /// Opaque pointer to discrete inputs data.
    pub discrete_inputs: *const MbusDiscreteInputs,
    /// User-provided opaque pointer.
    pub userdata: *mut c_void,
}

// ── FIFO callbacks (feature "fifo") ───────────────────────────────────────────

#[cfg(feature = "fifo")]
#[repr(C)]
/// Context passed to the read-fifo-queue callback.
pub struct MbusReadFifoQueueCtx {
    /// Transaction ID.
    pub txn_id: u16,
    /// Unit / slave ID.
    pub unit_id: u8,
    /// Opaque pointer to FIFO queue data.
    pub fifo_queue: *const MbusFifoQueue,
    /// User-provided opaque pointer.
    pub userdata: *mut c_void,
}

// ── File-record callbacks (feature "file-record") ─────────────────────────────

#[cfg(feature = "file-record")]
#[repr(C)]
/// Context passed to the read-file-record callback.
pub struct MbusReadFileRecordCtx {
    /// Transaction ID.
    pub txn_id: u16,
    /// Unit / slave ID.
    pub unit_id: u8,
    /// Pointer to array of sub-request results.
    pub results: *const MbusFileRecordResult,
    /// Number of sub-request entries in `results`.
    pub count: u16,
    /// User-provided opaque pointer.
    pub userdata: *mut c_void,
}

#[cfg(feature = "file-record")]
#[repr(C)]
/// Context passed to the write-file-record callback.
pub struct MbusWriteFileRecordCtx {
    /// Transaction ID.
    pub txn_id: u16,
    /// Unit / slave ID.
    pub unit_id: u8,
    /// User-provided opaque pointer.
    pub userdata: *mut c_void,
}

// ── Diagnostics callbacks (feature "diagnostics") ────────────────────────────

#[cfg(feature = "diagnostics")]
#[repr(C)]
/// Context passed to the read-exception-status callback.
pub struct MbusReadExceptionStatusCtx {
    /// Transaction ID.
    pub txn_id: u16,
    /// Unit / slave ID.
    pub unit_id: u8,
    /// Exception status byte.
    pub status: u8,
    /// User-provided opaque pointer.
    pub userdata: *mut c_void,
}

#[cfg(feature = "diagnostics")]
#[repr(C)]
/// Context passed to the diagnostics callback.
pub struct MbusDiagnosticsCtx {
    /// Transaction ID.
    pub txn_id: u16,
    /// Unit / slave ID.
    pub unit_id: u8,
    /// Diagnostic sub-function code.
    pub sub_fn: u16,
    /// Pointer to diagnostic data words.
    pub data: *const u16,
    /// Number of u16 words in `data`.
    pub data_len: u16,
    /// User-provided opaque pointer.
    pub userdata: *mut c_void,
}

#[cfg(feature = "diagnostics")]
#[repr(C)]
/// Context passed to the comm-event-counter callback.
pub struct MbusCommEventCounterCtx {
    /// Transaction ID.
    pub txn_id: u16,
    /// Unit / slave ID.
    pub unit_id: u8,
    /// Status word.
    pub status: u16,
    /// Event count.
    pub event_count: u16,
    /// User-provided opaque pointer.
    pub userdata: *mut c_void,
}

#[cfg(feature = "diagnostics")]
#[repr(C)]
/// Context passed to the comm-event-log callback.
pub struct MbusCommEventLogCtx {
    /// Transaction ID.
    pub txn_id: u16,
    /// Unit / slave ID.
    pub unit_id: u8,
    /// Status word.
    pub status: u16,
    /// Event count.
    pub event_count: u16,
    /// Message count.
    pub message_count: u16,
    /// Pointer to event bytes.
    pub events: *const u8,
    /// Number of bytes in `events`.
    pub events_len: u16,
    /// User-provided opaque pointer.
    pub userdata: *mut c_void,
}

#[cfg(feature = "diagnostics")]
#[repr(C)]
/// Context passed to the report-server-id callback.
pub struct MbusReportServerIdCtx {
    /// Transaction ID.
    pub txn_id: u16,
    /// Unit / slave ID.
    pub unit_id: u8,
    /// Server ID byte.
    pub server_id: u8,
    /// Run indicator (0xFF = ON, 0x00 = OFF).
    pub run_indicator: u8,
    /// Pointer to additional device identifier data.
    pub device_identifier: *const u8,
    /// Length of `device_identifier` data.
    pub identifier_len: u16,
    /// User-provided opaque pointer.
    pub userdata: *mut c_void,
}

#[cfg(feature = "diagnostics")]
#[repr(C)]
/// Context passed to the read-device-id callback.
pub struct MbusReadDeviceIdCtx {
    /// Transaction ID.
    pub txn_id: u16,
    /// Unit / slave ID.
    pub unit_id: u8,
    /// Read Device ID code that was requested.
    pub read_device_id_code: u8,
    /// Conformity level reported by the device.
    pub conformity_level: u8,
    /// 1 if more objects follow, 0 if this is the last.
    pub more_follows: u8,
    /// Pointer to raw MEI response bytes.
    pub objects: *const u8,
    /// Number of bytes in `objects`.
    pub objects_len: u16,
    /// User-provided opaque pointer.
    pub userdata: *mut c_void,
}

// ── Error callback ────────────────────────────────────────────────────────────

/// Context passed to the request-failed callback.
#[repr(C)]
pub struct MbusRequestFailedCtx {
    /// Transaction ID of the failed request.
    pub txn_id: u16,
    /// Unit / slave ID the request was targeting.
    pub unit_id: u8,
    /// Error code describing the failure.
    pub error: MbusStatusCode,
    /// User-provided opaque pointer.
    pub userdata: *mut c_void,
}

// ── Time callback ─────────────────────────────────────────────────────────────

/// Called by Rust whenever current time in milliseconds is needed.
///
/// This is required for no_std compatibility: the C host must provide time.
pub type MbusCurrentMillisCb = unsafe extern "C" fn(userdata: *mut c_void) -> u64;

// ── MbusCallbacks ─────────────────────────────────────────────────────────────

/// Table of C function-pointer callbacks.
///
/// Set a field to `NULL` to ignore that response type. All non-null callbacks
/// will be invoked synchronously from within `mbus_tcp_poll()` /
/// `mbus_serial_poll()`.
///
/// `userdata` is passed verbatim to every callback invocation; use it to
/// thread your own application context through.
#[repr(C)]
pub struct MbusCallbacks {
    /// Opaque application context pointer threaded through every callback.
    pub userdata: *mut c_void,

    /// Required callback used for all timekeeping in the client state machine.
    ///
    /// If this callback is NULL, client construction fails.
    pub on_current_millis: Option<unsafe extern "C" fn(userdata: *mut c_void) -> u64>,

    // ── Coil callbacks (feature "coils") ──────────────────────────────────
    #[cfg(feature = "coils")]
    /// Called when a Read Coils response is received.
    pub on_read_coils: Option<unsafe extern "C" fn(ctx: *const MbusReadCoilsCtx)>,
    #[cfg(feature = "coils")]
    /// Called when a Write Single Coil response is received.
    pub on_write_single_coil: Option<unsafe extern "C" fn(ctx: *const MbusWriteSingleCoilCtx)>,
    #[cfg(feature = "coils")]
    /// Called when a Write Multiple Coils response is received.
    pub on_write_multiple_coils:
        Option<unsafe extern "C" fn(ctx: *const MbusWriteMultipleCoilsCtx)>,

    // ── Register callbacks (feature "registers") ──────────────────────────
    #[cfg(feature = "holding-registers")]
    /// Called when a Read Holding Registers response is received.
    pub on_read_holding_registers:
        Option<unsafe extern "C" fn(ctx: *const MbusReadHoldingRegistersCtx)>,
    #[cfg(feature = "input-registers")]
    /// Called when a Read Input Registers response is received.
    pub on_read_input_registers:
        Option<unsafe extern "C" fn(ctx: *const MbusReadInputRegistersCtx)>,
    #[cfg(feature = "holding-registers")]
    /// Called when a Read/Write Multiple Registers response is received.
    pub on_read_write_multiple_registers:
        Option<unsafe extern "C" fn(ctx: *const MbusReadWriteMultipleRegistersCtx)>,
    #[cfg(feature = "holding-registers")]
    /// Called when a Write Single Register response is received.
    pub on_write_single_register:
        Option<unsafe extern "C" fn(ctx: *const MbusWriteSingleRegisterCtx)>,
    #[cfg(feature = "holding-registers")]
    /// Called when a Write Multiple Registers response is received.
    pub on_write_multiple_registers:
        Option<unsafe extern "C" fn(ctx: *const MbusWriteMultipleRegistersCtx)>,
    #[cfg(feature = "holding-registers")]
    /// Called when a Mask Write Register response is received.
    pub on_mask_write_register: Option<unsafe extern "C" fn(ctx: *const MbusMaskWriteRegisterCtx)>,

    // ── Discrete input callbacks (feature "discrete-inputs") ──────────────
    #[cfg(feature = "discrete-inputs")]
    /// Called when a Read Discrete Inputs response is received.
    pub on_read_discrete_inputs:
        Option<unsafe extern "C" fn(ctx: *const MbusReadDiscreteInputsCtx)>,

    // ── FIFO callbacks (feature "fifo") ───────────────────────────────────
    #[cfg(feature = "fifo")]
    /// Called when a Read FIFO Queue response is received.
    pub on_read_fifo_queue: Option<unsafe extern "C" fn(ctx: *const MbusReadFifoQueueCtx)>,

    // ── File-record callbacks (feature "file-record") ─────────────────────
    #[cfg(feature = "file-record")]
    /// Called when a Read File Record response is received.
    pub on_read_file_record: Option<unsafe extern "C" fn(ctx: *const MbusReadFileRecordCtx)>,
    #[cfg(feature = "file-record")]
    /// Called when a Write File Record response is received.
    pub on_write_file_record: Option<unsafe extern "C" fn(ctx: *const MbusWriteFileRecordCtx)>,

    // ── Diagnostics callbacks (feature "diagnostics") ─────────────────────
    #[cfg(feature = "diagnostics")]
    /// Called when a Read Exception Status response is received.
    pub on_read_exception_status:
        Option<unsafe extern "C" fn(ctx: *const MbusReadExceptionStatusCtx)>,
    #[cfg(feature = "diagnostics")]
    /// Called when a Diagnostics response is received.
    pub on_diagnostics: Option<unsafe extern "C" fn(ctx: *const MbusDiagnosticsCtx)>,
    #[cfg(feature = "diagnostics")]
    /// Called when a Comm Event Counter response is received.
    pub on_comm_event_counter: Option<unsafe extern "C" fn(ctx: *const MbusCommEventCounterCtx)>,
    #[cfg(feature = "diagnostics")]
    /// Called when a Comm Event Log response is received.
    pub on_comm_event_log: Option<unsafe extern "C" fn(ctx: *const MbusCommEventLogCtx)>,
    #[cfg(feature = "diagnostics")]
    /// Called when a Report Server ID response is received.
    pub on_report_server_id: Option<unsafe extern "C" fn(ctx: *const MbusReportServerIdCtx)>,
    #[cfg(feature = "diagnostics")]
    /// Called when a Read Device Identification response is received.
    pub on_read_device_id: Option<unsafe extern "C" fn(ctx: *const MbusReadDeviceIdCtx)>,

    // ── Error callback ─────────────────────────────────────────────────────
    /// Called when a request fails (timeout, exception, etc.).
    pub on_request_failed: Option<unsafe extern "C" fn(ctx: *const MbusRequestFailedCtx)>,
}