mabi-bacnet 1.5.0

Mabinogion - BACnet/IP simulator
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
//! COV (Change of Value) subscription and notification services.

use std::net::SocketAddr;
use std::time::{Duration, Instant};

use dashmap::DashMap;
use tokio::sync::mpsc;

use crate::apdu::encoding::ApduEncoder;
use crate::object::property::{BACnetValue, PropertyId};
use crate::object::types::ObjectId;

/// COV subscription.
#[derive(Debug, Clone)]
pub struct CovSubscription {
    /// Subscriber address.
    pub subscriber_address: SocketAddr,
    /// Subscriber process identifier.
    pub subscriber_process_id: u32,
    /// Monitored object identifier.
    pub monitored_object: ObjectId,
    /// Whether to send confirmed notifications.
    pub confirmed_notifications: bool,
    /// Subscription lifetime (None = infinite).
    pub lifetime: Option<Duration>,
    /// COV increment for analog objects.
    pub cov_increment: Option<f32>,
    /// When the subscription was created.
    pub created_at: Instant,
    /// Last notification time.
    pub last_notification: Option<Instant>,
}

impl CovSubscription {
    /// Create a new subscription.
    pub fn new(
        subscriber_address: SocketAddr,
        subscriber_process_id: u32,
        monitored_object: ObjectId,
        confirmed: bool,
        lifetime: Option<Duration>,
    ) -> Self {
        Self {
            subscriber_address,
            subscriber_process_id,
            monitored_object,
            confirmed_notifications: confirmed,
            lifetime,
            cov_increment: None,
            created_at: Instant::now(),
            last_notification: None,
        }
    }

    /// Check if the subscription has expired.
    pub fn is_expired(&self) -> bool {
        if let Some(lifetime) = self.lifetime {
            self.created_at.elapsed() > lifetime
        } else {
            false
        }
    }

    /// Get remaining lifetime in seconds.
    pub fn remaining_lifetime(&self) -> Option<u32> {
        self.lifetime.map(|lifetime| {
            let elapsed = self.created_at.elapsed();
            if elapsed > lifetime {
                0
            } else {
                (lifetime - elapsed).as_secs() as u32
            }
        })
    }
}

/// Key for subscription lookup.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct SubscriptionKey {
    subscriber_address: SocketAddr,
    subscriber_process_id: u32,
    monitored_object: ObjectId,
}

/// COV notification to be sent.
#[derive(Debug, Clone)]
pub struct CovNotification {
    /// Destination address.
    pub destination: SocketAddr,
    /// Subscriber process ID.
    pub subscriber_process_id: u32,
    /// Initiating device identifier.
    pub initiating_device: ObjectId,
    /// Monitored object identifier.
    pub monitored_object: ObjectId,
    /// Time remaining on subscription.
    pub time_remaining: u32,
    /// List of property values.
    pub list_of_values: Vec<(PropertyId, BACnetValue)>,
    /// Whether this requires confirmation.
    pub confirmed: bool,
}

impl CovNotification {
    /// Encode the COV notification service data (shared between confirmed and unconfirmed).
    fn encode_service_data(&self) -> Vec<u8> {
        let mut encoder = ApduEncoder::new();

        // Context tag 0: Subscriber Process Identifier
        encoder.encode_context_unsigned(0, self.subscriber_process_id);

        // Context tag 1: Initiating Device Identifier
        encoder.encode_context_object_identifier(1, self.initiating_device);

        // Context tag 2: Monitored Object Identifier
        encoder.encode_context_object_identifier(2, self.monitored_object);

        // Context tag 3: Time Remaining
        encoder.encode_context_unsigned(3, self.time_remaining);

        // Context tag 4: List of Values (opening)
        encoder.encode_opening_tag(4);
        for (property_id, value) in &self.list_of_values {
            // Property Identifier
            encoder.encode_context_enumerated(0, *property_id as u32);
            // Property Value (opening tag 2)
            encoder.encode_opening_tag(2);
            encoder.encode_value(value);
            encoder.encode_closing_tag(2);
        }
        encoder.encode_closing_tag(4);

        encoder.into_bytes()
    }

    /// Encode as unconfirmed COV notification APDU service data.
    pub fn encode_unconfirmed(&self) -> Vec<u8> {
        self.encode_service_data()
    }

    /// Encode as confirmed COV notification APDU.
    ///
    /// Returns the full APDU including the confirmed request header.
    /// `invoke_id` is the invoke ID for the confirmed transaction.
    pub fn encode_confirmed(&self, invoke_id: u8) -> Vec<u8> {
        let service_data = self.encode_service_data();
        let mut apdu = Vec::with_capacity(4 + service_data.len());
        // Confirmed Request header: PDU type 0 (ConfirmedRequest), no segmentation
        apdu.push(0x00); // PDU type 0, no segmentation, no more-follows
        apdu.push(0x05); // max-segments=0, max-apdu=5 (1476 bytes)
        apdu.push(invoke_id);
        apdu.push(crate::apdu::types::ConfirmedService::ConfirmedCovNotification as u8);
        apdu.extend_from_slice(&service_data);
        apdu
    }
}

/// COV subscription manager.
pub struct CovManager {
    /// Active subscriptions.
    subscriptions: DashMap<SubscriptionKey, CovSubscription>,
    /// Channel for outgoing notifications.
    notification_tx: mpsc::Sender<CovNotification>,
    /// Maximum number of subscriptions.
    max_subscriptions: usize,
    /// Device instance for notifications.
    device_instance: u32,
}

impl CovManager {
    /// Create a new COV manager.
    pub fn new(
        device_instance: u32,
        max_subscriptions: usize,
    ) -> (Self, mpsc::Receiver<CovNotification>) {
        let (tx, rx) = mpsc::channel(1000);

        (
            Self {
                subscriptions: DashMap::new(),
                notification_tx: tx,
                max_subscriptions,
                device_instance,
            },
            rx,
        )
    }

    /// Add or update a subscription.
    pub fn subscribe(&self, subscription: CovSubscription) -> Result<(), CovError> {
        // Check if we're at capacity
        if self.subscriptions.len() >= self.max_subscriptions {
            // Try to remove expired subscriptions first
            self.cleanup_expired();

            if self.subscriptions.len() >= self.max_subscriptions {
                return Err(CovError::MaxSubscriptionsReached);
            }
        }

        let key = SubscriptionKey {
            subscriber_address: subscription.subscriber_address,
            subscriber_process_id: subscription.subscriber_process_id,
            monitored_object: subscription.monitored_object,
        };

        self.subscriptions.insert(key, subscription);
        Ok(())
    }

    /// Cancel a subscription.
    pub fn unsubscribe(
        &self,
        subscriber_address: SocketAddr,
        subscriber_process_id: u32,
        monitored_object: ObjectId,
    ) -> bool {
        let key = SubscriptionKey {
            subscriber_address,
            subscriber_process_id,
            monitored_object,
        };

        self.subscriptions.remove(&key).is_some()
    }

    /// Get subscription count.
    pub fn subscription_count(&self) -> usize {
        self.subscriptions.len()
    }

    /// Get subscriptions for an object.
    pub fn subscriptions_for_object(&self, object_id: ObjectId) -> Vec<CovSubscription> {
        self.subscriptions
            .iter()
            .filter(|entry| entry.monitored_object == object_id)
            .map(|entry| entry.clone())
            .collect()
    }

    /// Notify value change for an object (called when object changes).
    pub async fn notify_change(&self, object_id: ObjectId, values: Vec<(PropertyId, BACnetValue)>) {
        let device_id = ObjectId::device(self.device_instance);

        for entry in self.subscriptions.iter() {
            let subscription = entry.value();

            if subscription.monitored_object != object_id || subscription.is_expired() {
                continue;
            }

            let notification = CovNotification {
                destination: subscription.subscriber_address,
                subscriber_process_id: subscription.subscriber_process_id,
                initiating_device: device_id,
                monitored_object: object_id,
                time_remaining: subscription.remaining_lifetime().unwrap_or(0),
                list_of_values: values.clone(),
                confirmed: subscription.confirmed_notifications,
            };

            let _ = self.notification_tx.send(notification).await;
        }
    }

    /// Clean up expired subscriptions.
    pub fn cleanup_expired(&self) {
        self.subscriptions.retain(|_, sub| !sub.is_expired());
    }

    /// Get all active subscriptions as a list.
    pub fn list_subscriptions(&self) -> Vec<CovSubscription> {
        self.subscriptions
            .iter()
            .map(|entry| entry.clone())
            .collect()
    }
}

/// COV errors.
#[derive(Debug, thiserror::Error)]
pub enum CovError {
    #[error("Maximum subscriptions reached")]
    MaxSubscriptionsReached,

    #[error("Subscription not found")]
    SubscriptionNotFound,

    #[error("Object does not support COV")]
    NotCovProperty,
}

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

    #[test]
    fn test_subscription_expiry() {
        let sub = CovSubscription::new(
            "127.0.0.1:47808".parse().unwrap(),
            1,
            ObjectId::new(crate::object::types::ObjectType::AnalogInput, 1),
            false,
            Some(Duration::from_secs(0)), // Expired immediately
        );

        std::thread::sleep(Duration::from_millis(10));
        assert!(sub.is_expired());
    }

    #[test]
    fn test_subscription_no_expiry() {
        let sub = CovSubscription::new(
            "127.0.0.1:47808".parse().unwrap(),
            1,
            ObjectId::new(crate::object::types::ObjectType::AnalogInput, 1),
            false,
            None, // No expiry
        );

        assert!(!sub.is_expired());
    }

    #[tokio::test]
    async fn test_cov_manager() {
        let (manager, _rx) = CovManager::new(1234, 100);

        let sub = CovSubscription::new(
            "127.0.0.1:47808".parse().unwrap(),
            1,
            ObjectId::new(crate::object::types::ObjectType::AnalogInput, 1),
            false,
            Some(Duration::from_secs(300)),
        );

        manager.subscribe(sub).unwrap();
        assert_eq!(manager.subscription_count(), 1);

        manager.unsubscribe(
            "127.0.0.1:47808".parse().unwrap(),
            1,
            ObjectId::new(crate::object::types::ObjectType::AnalogInput, 1),
        );
        assert_eq!(manager.subscription_count(), 0);
    }

    #[test]
    fn test_subscription_remaining_lifetime() {
        let sub = CovSubscription::new(
            "127.0.0.1:47808".parse().unwrap(),
            1,
            ObjectId::new(crate::object::types::ObjectType::AnalogInput, 1),
            false,
            Some(Duration::from_secs(300)),
        );

        let remaining = sub.remaining_lifetime().unwrap();
        assert!(remaining > 0 && remaining <= 300);

        // Infinite lifetime returns None
        let sub_inf = CovSubscription::new(
            "127.0.0.1:47808".parse().unwrap(),
            1,
            ObjectId::new(crate::object::types::ObjectType::AnalogInput, 1),
            false,
            None,
        );
        assert!(sub_inf.remaining_lifetime().is_none());
    }

    #[test]
    fn test_subscription_cov_increment() {
        let mut sub = CovSubscription::new(
            "127.0.0.1:47808".parse().unwrap(),
            1,
            ObjectId::new(crate::object::types::ObjectType::AnalogInput, 1),
            false,
            None,
        );

        assert!(sub.cov_increment.is_none());
        sub.cov_increment = Some(1.5);
        assert_eq!(sub.cov_increment, Some(1.5));
    }

    #[test]
    fn test_encode_unconfirmed_notification() {
        let notification = CovNotification {
            destination: "10.0.0.1:47808".parse().unwrap(),
            subscriber_process_id: 42,
            initiating_device: ObjectId::device(1234),
            monitored_object: ObjectId::new(crate::object::types::ObjectType::AnalogInput, 1),
            time_remaining: 300,
            list_of_values: vec![
                (PropertyId::PresentValue, BACnetValue::Real(72.5)),
                (
                    PropertyId::StatusFlags,
                    BACnetValue::BitString(vec![false, false, false, false]),
                ),
            ],
            confirmed: false,
        };

        let data = notification.encode_unconfirmed();
        assert!(!data.is_empty());
        // Should start with context tag 0 (subscriber process id)
        assert_eq!(data[0] & 0x0F, 0x09); // context tag 0, length 1
    }

    #[test]
    fn test_encode_confirmed_notification() {
        let notification = CovNotification {
            destination: "10.0.0.1:47808".parse().unwrap(),
            subscriber_process_id: 1,
            initiating_device: ObjectId::device(1234),
            monitored_object: ObjectId::new(crate::object::types::ObjectType::AnalogInput, 1),
            time_remaining: 0,
            list_of_values: vec![(PropertyId::PresentValue, BACnetValue::Real(25.0))],
            confirmed: true,
        };

        let apdu = notification.encode_confirmed(7);
        // ConfirmedRequest header
        assert_eq!(apdu[0], 0x00); // PDU type 0, no segmentation
        assert_eq!(apdu[1], 0x05); // max-segments=0, max-apdu=5 (1476)
        assert_eq!(apdu[2], 7); // invoke_id
        assert_eq!(
            apdu[3],
            crate::apdu::types::ConfirmedService::ConfirmedCovNotification as u8
        );
        // Service data follows
        assert!(apdu.len() > 4);
    }

    #[tokio::test]
    async fn test_cov_manager_max_subscriptions() {
        let (manager, _rx) = CovManager::new(1234, 2);

        let sub1 = CovSubscription::new(
            "10.0.0.1:47808".parse().unwrap(),
            1,
            ObjectId::new(crate::object::types::ObjectType::AnalogInput, 1),
            false,
            None,
        );
        let sub2 = CovSubscription::new(
            "10.0.0.2:47808".parse().unwrap(),
            1,
            ObjectId::new(crate::object::types::ObjectType::AnalogInput, 1),
            false,
            None,
        );
        let sub3 = CovSubscription::new(
            "10.0.0.3:47808".parse().unwrap(),
            1,
            ObjectId::new(crate::object::types::ObjectType::AnalogInput, 1),
            false,
            None,
        );

        manager.subscribe(sub1).unwrap();
        manager.subscribe(sub2).unwrap();
        assert!(manager.subscribe(sub3).is_err());
    }

    #[tokio::test]
    async fn test_cov_manager_cleanup_expired() {
        let (manager, _rx) = CovManager::new(1234, 100);

        let sub = CovSubscription::new(
            "127.0.0.1:47808".parse().unwrap(),
            1,
            ObjectId::new(crate::object::types::ObjectType::AnalogInput, 1),
            false,
            Some(Duration::from_secs(0)), // Expires immediately
        );

        manager.subscribe(sub).unwrap();
        assert_eq!(manager.subscription_count(), 1);

        std::thread::sleep(Duration::from_millis(10));
        manager.cleanup_expired();
        assert_eq!(manager.subscription_count(), 0);
    }

    #[tokio::test]
    async fn test_cov_manager_subscriptions_for_object() {
        let (manager, _rx) = CovManager::new(1234, 100);
        let obj1 = ObjectId::new(crate::object::types::ObjectType::AnalogInput, 1);
        let obj2 = ObjectId::new(crate::object::types::ObjectType::AnalogInput, 2);

        manager
            .subscribe(CovSubscription::new(
                "10.0.0.1:47808".parse().unwrap(),
                1,
                obj1,
                false,
                None,
            ))
            .unwrap();
        manager
            .subscribe(CovSubscription::new(
                "10.0.0.2:47808".parse().unwrap(),
                1,
                obj1,
                true,
                None,
            ))
            .unwrap();
        manager
            .subscribe(CovSubscription::new(
                "10.0.0.3:47808".parse().unwrap(),
                1,
                obj2,
                false,
                None,
            ))
            .unwrap();

        let subs = manager.subscriptions_for_object(obj1);
        assert_eq!(subs.len(), 2);

        let subs2 = manager.subscriptions_for_object(obj2);
        assert_eq!(subs2.len(), 1);
    }

    #[tokio::test]
    async fn test_cov_notify_change() {
        let (manager, mut rx) = CovManager::new(1234, 100);
        let obj = ObjectId::new(crate::object::types::ObjectType::AnalogInput, 1);

        // Subscribe two subscribers, one confirmed, one unconfirmed
        manager
            .subscribe(CovSubscription::new(
                "10.0.0.1:47808".parse().unwrap(),
                1,
                obj,
                false,
                None,
            ))
            .unwrap();
        manager
            .subscribe(CovSubscription::new(
                "10.0.0.2:47808".parse().unwrap(),
                2,
                obj,
                true,
                None,
            ))
            .unwrap();

        // Trigger notification
        let values = vec![(PropertyId::PresentValue, BACnetValue::Real(42.0))];
        manager.notify_change(obj, values).await;

        // Should receive 2 notifications
        let n1 = rx.try_recv().unwrap();
        let n2 = rx.try_recv().unwrap();

        // One confirmed, one not
        let (confirmed_count, unconfirmed_count) =
            [n1, n2].iter().fold(
                (0, 0),
                |(c, u), n| {
                    if n.confirmed {
                        (c + 1, u)
                    } else {
                        (c, u + 1)
                    }
                },
            );
        assert_eq!(confirmed_count, 1);
        assert_eq!(unconfirmed_count, 1);
    }

    #[tokio::test]
    async fn test_cov_manager_list_subscriptions() {
        let (manager, _rx) = CovManager::new(1234, 100);
        let obj = ObjectId::new(crate::object::types::ObjectType::AnalogInput, 1);

        manager
            .subscribe(CovSubscription::new(
                "10.0.0.1:47808".parse().unwrap(),
                1,
                obj,
                false,
                None,
            ))
            .unwrap();
        manager
            .subscribe(CovSubscription::new(
                "10.0.0.2:47808".parse().unwrap(),
                2,
                obj,
                true,
                Some(Duration::from_secs(600)),
            ))
            .unwrap();

        let list = manager.list_subscriptions();
        assert_eq!(list.len(), 2);
    }
}