lib60870 0.4.0

Safe Rust bindings to lib60870-C, an IEC 60870-5-101/104 protocol implementation
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
//! IEC 60870-5-104 server (slave) implementation.
//!
//! The server accepts connections from clients (masters) and can send
//! spontaneous data and respond to commands.

use std::ffi::CString;
use std::os::raw::c_void;
use std::ptr::NonNull;
use std::sync::Arc;

use crate::asdu::Asdu;
use crate::sys;
use crate::time::Timestamp;
use crate::types::{CauseOfTransmission, PeerConnectionEvent, Quality, ServerMode};

/// Callback for connection requests.
///
/// Return `true` to accept the connection, `false` to reject.
pub type ConnectionRequestHandler = Box<dyn Fn(&str) -> bool + Send + Sync>;

/// Callback for connection events.
pub type ConnectionEventHandler = Box<dyn Fn(PeerConnectionEvent) + Send + Sync>;

/// Callback for interrogation commands.
///
/// The ASDU is cloned before being passed to the callback.
/// Return `true` if handled, `false` otherwise.
pub type InterrogationHandler = Box<dyn Fn(&MasterConnection, Asdu, u8) -> bool + Send + Sync>;

/// Callback for clock synchronization commands.
///
/// The ASDU is cloned before being passed to the callback.
/// Return `true` if handled, `false` otherwise.
pub type ClockSyncHandler = Box<dyn Fn(&MasterConnection, Asdu, &Timestamp) -> bool + Send + Sync>;

/// Callback for received ASDUs (commands).
///
/// The ASDU is cloned before being passed to the callback.
/// Return `true` if handled, `false` otherwise.
pub type AsduHandler = Box<dyn Fn(&MasterConnection, Asdu) -> bool + Send + Sync>;

/// A connection from a master (client) to this server.
///
/// This type represents a borrowed reference to an active client connection.
/// It is passed to server callbacks and can be used to send responses.
///
/// # Lifetime
///
/// **Important:** This type is only valid within the callback scope. The underlying
/// connection is owned by the C library and may be closed at any time after the
/// callback returns.
///
/// - ✅ Safe: Use within callbacks to send responses
/// - ❌ Unsafe: Store and use after callback returns
///
/// The type intentionally has no `Clone` implementation to prevent accidental storage.
#[repr(transparent)]
pub struct MasterConnection(sys::IMasterConnection);

impl MasterConnection {
    /// Create from raw pointer (for callbacks).
    pub(crate) unsafe fn from_ptr(ptr: sys::IMasterConnection) -> Self {
        Self(ptr)
    }

    /// Get the raw pointer for FFI interop.
    ///
    /// # Safety
    ///
    /// The returned pointer is only valid within the current callback scope.
    /// Do not store or use after the callback returns, as the connection
    /// may be closed by the C library at any time.
    pub fn as_ptr(&self) -> sys::IMasterConnection {
        self.0
    }

    /// Send an ASDU to this connection.
    pub fn send_asdu(&self, asdu: &Asdu) {
        unsafe {
            sys::IMasterConnection_sendASDU(self.0, asdu.as_ptr());
        }
    }

    /// Send an activation confirmation (ACT_CON).
    ///
    /// `negative` should be `true` for negative confirmation.
    pub fn send_act_con(&self, asdu: &Asdu, negative: bool) {
        unsafe {
            sys::IMasterConnection_sendACT_CON(self.0, asdu.as_ptr(), negative);
        }
    }

    /// Send an activation termination (ACT_TERM).
    pub fn send_act_term(&self, asdu: &Asdu) {
        unsafe {
            sys::IMasterConnection_sendACT_TERM(self.0, asdu.as_ptr());
        }
    }

    /// Get the application layer parameters for this connection.
    #[allow(dead_code)]
    pub(crate) fn app_layer_params(&self) -> sys::CS101_AppLayerParameters {
        unsafe { sys::IMasterConnection_getApplicationLayerParameters(self.0) }
    }
}

/// Internal state for callbacks.
struct CallbackState {
    connection_request_handler: Option<ConnectionRequestHandler>,
    connection_event_handler: Option<ConnectionEventHandler>,
    interrogation_handler: Option<InterrogationHandler>,
    clock_sync_handler: Option<ClockSyncHandler>,
    asdu_handler: Option<AsduHandler>,
}

/// Builder for creating a server.
pub struct ServerBuilder {
    max_low_priority_queue: i32,
    max_high_priority_queue: i32,
    local_address: String,
    local_port: i32,
    server_mode: ServerMode,
}

impl ServerBuilder {
    /// Create a new server builder with default settings.
    pub fn new() -> Self {
        Self {
            max_low_priority_queue: 10,
            max_high_priority_queue: 10,
            local_address: "0.0.0.0".to_string(),
            local_port: 2404,
            server_mode: ServerMode::SingleRedundancyGroup,
        }
    }

    /// Set the maximum queue sizes.
    pub fn queue_sizes(mut self, low_priority: i32, high_priority: i32) -> Self {
        self.max_low_priority_queue = low_priority;
        self.max_high_priority_queue = high_priority;
        self
    }

    /// Set the local address to bind to.
    pub fn local_address(mut self, address: &str) -> Self {
        self.local_address = address.to_string();
        self
    }

    /// Set the local port to listen on.
    pub fn local_port(mut self, port: u16) -> Self {
        self.local_port = port as i32;
        self
    }

    /// Set the server mode.
    pub fn server_mode(mut self, mode: ServerMode) -> Self {
        self.server_mode = mode;
        self
    }

    /// Build the server.
    pub fn build(self) -> Option<Server> {
        Server::new_with_config(
            self.max_low_priority_queue,
            self.max_high_priority_queue,
            &self.local_address,
            self.local_port,
            self.server_mode,
        )
    }
}

impl Default for ServerBuilder {
    fn default() -> Self {
        Self::new()
    }
}

/// An IEC 60870-5-104 server (slave).
///
/// # Example
///
/// ```no_run
/// use lib60870::server::{Server, ServerBuilder};
/// use lib60870::types::PeerConnectionEvent;
///
/// let mut server = ServerBuilder::new()
///     .local_address("0.0.0.0")
///     .local_port(2404)
///     .build()
///     .expect("Failed to create server");
///
/// server.set_connection_request_handler(|ip| {
///     println!("Connection request from {}", ip);
///     true // Accept all connections
/// });
///
/// server.set_connection_event_handler(|event| {
///     println!("Connection event: {:?}", event);
/// });
///
/// server.start();
///
/// // Server is running, enqueue data to send...
/// ```
pub struct Server {
    ptr: NonNull<sys::sCS104_Slave>,
    callback_state: Option<Arc<CallbackState>>,
}

unsafe impl Send for Server {}

impl Drop for Server {
    fn drop(&mut self) {
        unsafe {
            sys::CS104_Slave_stop(self.ptr.as_ptr());
            sys::CS104_Slave_destroy(self.ptr.as_ptr());
        }
    }
}

impl Server {
    /// Create a new server with default settings.
    pub fn new() -> Option<Self> {
        ServerBuilder::new().build()
    }

    fn new_with_config(
        max_low_priority_queue: i32,
        max_high_priority_queue: i32,
        local_address: &str,
        local_port: i32,
        server_mode: ServerMode,
    ) -> Option<Self> {
        let ptr =
            unsafe { sys::CS104_Slave_create(max_low_priority_queue, max_high_priority_queue) };
        let ptr = NonNull::new(ptr)?;

        let c_address = CString::new(local_address).ok()?;
        unsafe {
            sys::CS104_Slave_setLocalAddress(ptr.as_ptr(), c_address.as_ptr());
            sys::CS104_Slave_setLocalPort(ptr.as_ptr(), local_port);
            sys::CS104_Slave_setServerMode(ptr.as_ptr(), server_mode.as_raw());
        }

        Some(Self {
            ptr,
            callback_state: None,
        })
    }

    /// Get the raw pointer (for advanced use).
    pub fn as_ptr(&self) -> sys::CS104_Slave {
        self.ptr.as_ptr()
    }

    /// Get the application layer parameters.
    pub(crate) fn app_layer_params(&self) -> sys::CS101_AppLayerParameters {
        unsafe { sys::CS104_Slave_getAppLayerParameters(self.ptr.as_ptr()) }
    }

    /// Set the connection request handler.
    pub fn set_connection_request_handler<F>(&mut self, handler: F)
    where
        F: Fn(&str) -> bool + Send + Sync + 'static,
    {
        self.update_callback_state(|state| {
            state.connection_request_handler = Some(Box::new(handler));
        });
    }

    /// Set the connection event handler.
    pub fn set_connection_event_handler<F>(&mut self, handler: F)
    where
        F: Fn(PeerConnectionEvent) + Send + Sync + 'static,
    {
        self.update_callback_state(|state| {
            state.connection_event_handler = Some(Box::new(handler));
        });
    }

    /// Set the interrogation handler.
    pub fn set_interrogation_handler<F>(&mut self, handler: F)
    where
        F: Fn(&MasterConnection, Asdu, u8) -> bool + Send + Sync + 'static,
    {
        self.update_callback_state(|state| {
            state.interrogation_handler = Some(Box::new(handler));
        });
    }

    /// Set the clock synchronization handler.
    pub fn set_clock_sync_handler<F>(&mut self, handler: F)
    where
        F: Fn(&MasterConnection, Asdu, &Timestamp) -> bool + Send + Sync + 'static,
    {
        self.update_callback_state(|state| {
            state.clock_sync_handler = Some(Box::new(handler));
        });
    }

    /// Set the ASDU handler for commands.
    pub fn set_asdu_handler<F>(&mut self, handler: F)
    where
        F: Fn(&MasterConnection, Asdu) -> bool + Send + Sync + 'static,
    {
        self.update_callback_state(|state| {
            state.asdu_handler = Some(Box::new(handler));
        });
    }

    fn update_callback_state<F>(&mut self, f: F)
    where
        F: FnOnce(&mut CallbackState),
    {
        // Create new state with existing handlers
        let mut new_state = CallbackState {
            connection_request_handler: None,
            connection_event_handler: None,
            interrogation_handler: None,
            clock_sync_handler: None,
            asdu_handler: None,
        };
        f(&mut new_state);

        let state = Arc::new(new_state);
        let state_ptr = Arc::as_ptr(&state) as *mut c_void;
        self.callback_state = Some(state);

        unsafe {
            sys::CS104_Slave_setConnectionRequestHandler(
                self.ptr.as_ptr(),
                Some(connection_request_trampoline),
                state_ptr,
            );
            sys::CS104_Slave_setConnectionEventHandler(
                self.ptr.as_ptr(),
                Some(connection_event_trampoline),
                state_ptr,
            );
            sys::CS104_Slave_setInterrogationHandler(
                self.ptr.as_ptr(),
                Some(interrogation_trampoline),
                state_ptr,
            );
            sys::CS104_Slave_setClockSyncHandler(
                self.ptr.as_ptr(),
                Some(clock_sync_trampoline),
                state_ptr,
            );
            sys::CS104_Slave_setASDUHandler(
                self.ptr.as_ptr(),
                Some(asdu_handler_trampoline),
                state_ptr,
            );
        }
    }

    /// Start the server.
    pub fn start(&self) {
        unsafe {
            sys::CS104_Slave_start(self.ptr.as_ptr());
        }
    }

    /// Stop the server.
    pub fn stop(&self) {
        unsafe {
            sys::CS104_Slave_stop(self.ptr.as_ptr());
        }
    }

    /// Check if the server is running.
    pub fn is_running(&self) -> bool {
        unsafe { sys::CS104_Slave_isRunning(self.ptr.as_ptr()) }
    }

    /// Enqueue an ASDU to be sent to all connected clients.
    ///
    /// This is used to send spontaneous data updates.
    pub fn enqueue_asdu(&self, asdu: &Asdu) {
        unsafe {
            sys::CS104_Slave_enqueueASDU(self.ptr.as_ptr(), asdu.as_ptr());
        }
    }

    /// Send a single-point information value.
    ///
    /// This is a convenience method that creates and enqueues an ASDU.
    pub fn send_single_point(
        &self,
        cot: CauseOfTransmission,
        ca: u16,
        ioa: u32,
        value: bool,
        quality: Quality,
    ) {
        unsafe {
            let al_params = self.app_layer_params();
            let asdu =
                sys::CS101_ASDU_create(al_params, false, cot.as_raw(), 0, ca as i32, false, false);
            if asdu.is_null() {
                return;
            }

            let io = sys::SinglePointInformation_create(
                std::ptr::null_mut(),
                ioa as i32,
                value,
                quality.bits(),
            );
            if !io.is_null() {
                sys::CS101_ASDU_addInformationObject(asdu, io as sys::InformationObject);
                sys::InformationObject_destroy(io as sys::InformationObject);
            }

            sys::CS104_Slave_enqueueASDU(self.ptr.as_ptr(), asdu);
            sys::CS101_ASDU_destroy(asdu);
        }
    }

    /// Send a measured scaled value.
    ///
    /// This is a convenience method that creates and enqueues an ASDU.
    pub fn send_measured_scaled(
        &self,
        cot: CauseOfTransmission,
        ca: u16,
        ioa: u32,
        value: i16,
        quality: Quality,
    ) {
        unsafe {
            let al_params = self.app_layer_params();
            let asdu =
                sys::CS101_ASDU_create(al_params, false, cot.as_raw(), 0, ca as i32, false, false);
            if asdu.is_null() {
                return;
            }

            let io = sys::MeasuredValueScaled_create(
                std::ptr::null_mut(),
                ioa as i32,
                value as i32,
                quality.bits(),
            );
            if !io.is_null() {
                sys::CS101_ASDU_addInformationObject(asdu, io as sys::InformationObject);
                sys::InformationObject_destroy(io as sys::InformationObject);
            }

            sys::CS104_Slave_enqueueASDU(self.ptr.as_ptr(), asdu);
            sys::CS101_ASDU_destroy(asdu);
        }
    }

    /// Send a measured float value.
    pub fn send_measured_float(
        &self,
        cot: CauseOfTransmission,
        ca: u16,
        ioa: u32,
        value: f32,
        quality: Quality,
    ) {
        unsafe {
            let al_params = self.app_layer_params();
            let asdu =
                sys::CS101_ASDU_create(al_params, false, cot.as_raw(), 0, ca as i32, false, false);
            if asdu.is_null() {
                return;
            }

            let io = sys::MeasuredValueShort_create(
                std::ptr::null_mut(),
                ioa as i32,
                value,
                quality.bits(),
            );
            if !io.is_null() {
                sys::CS101_ASDU_addInformationObject(asdu, io as sys::InformationObject);
                sys::InformationObject_destroy(io as sys::InformationObject);
            }

            sys::CS104_Slave_enqueueASDU(self.ptr.as_ptr(), asdu);
            sys::CS101_ASDU_destroy(asdu);
        }
    }
}

// C callback trampolines

unsafe extern "C" fn connection_request_trampoline(
    parameter: *mut c_void,
    ip_address: *const std::os::raw::c_char,
) -> bool {
    if parameter.is_null() || ip_address.is_null() {
        return true;
    }
    let state = &*(parameter as *const CallbackState);
    if let Some(ref handler) = state.connection_request_handler {
        let ip = std::ffi::CStr::from_ptr(ip_address).to_str().unwrap_or("");
        handler(ip)
    } else {
        true
    }
}

unsafe extern "C" fn connection_event_trampoline(
    parameter: *mut c_void,
    _connection: sys::IMasterConnection,
    event: sys::CS104_PeerConnectionEvent,
) {
    if parameter.is_null() {
        return;
    }
    let state = &*(parameter as *const CallbackState);
    if let Some(ref handler) = state.connection_event_handler {
        if let Some(event) = PeerConnectionEvent::from_raw(event) {
            handler(event);
        }
    }
}

unsafe extern "C" fn interrogation_trampoline(
    parameter: *mut c_void,
    connection: sys::IMasterConnection,
    asdu: sys::CS101_ASDU,
    qoi: sys::QualifierOfInterrogation,
) -> bool {
    if parameter.is_null() || connection.is_null() || asdu.is_null() {
        return false;
    }
    let state = &*(parameter as *const CallbackState);
    if let Some(ref handler) = state.interrogation_handler {
        let conn = MasterConnection::from_ptr(connection);
        if let Some(owned_asdu) = Asdu::clone_from_ptr(asdu) {
            handler(&conn, owned_asdu, qoi)
        } else {
            false
        }
    } else {
        false
    }
}

unsafe extern "C" fn clock_sync_trampoline(
    parameter: *mut c_void,
    connection: sys::IMasterConnection,
    asdu: sys::CS101_ASDU,
    new_time: sys::CP56Time2a,
) -> bool {
    if parameter.is_null() || connection.is_null() || asdu.is_null() || new_time.is_null() {
        return false;
    }
    let state = &*(parameter as *const CallbackState);
    if let Some(ref handler) = state.clock_sync_handler {
        let conn = MasterConnection::from_ptr(connection);
        if let Some(owned_asdu) = Asdu::clone_from_ptr(asdu) {
            let time = Timestamp(*new_time);
            handler(&conn, owned_asdu, &time)
        } else {
            false
        }
    } else {
        false
    }
}

unsafe extern "C" fn asdu_handler_trampoline(
    parameter: *mut c_void,
    connection: sys::IMasterConnection,
    asdu: sys::CS101_ASDU,
) -> bool {
    if parameter.is_null() || connection.is_null() || asdu.is_null() {
        return false;
    }
    let state = &*(parameter as *const CallbackState);
    if let Some(ref handler) = state.asdu_handler {
        let conn = MasterConnection::from_ptr(connection);
        if let Some(owned_asdu) = Asdu::clone_from_ptr(asdu) {
            handler(&conn, owned_asdu)
        } else {
            false
        }
    } else {
        false
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_server_builder() {
        let server = ServerBuilder::new()
            .local_address("127.0.0.1")
            .local_port(2404)
            .queue_sizes(10, 10)
            .build();
        assert!(server.is_some());
    }

    #[test]
    fn test_server_create() {
        let server = Server::new();
        assert!(server.is_some());
    }
}