bacnet-server 0.15.0

BACnet server: APDU dispatch and service handlers
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
//! Lighting Output's fades, ramps and egress timers on the server's
//! monotonic task (#1384), in paused time: a write that starts one wakes the
//! task, which samples Tracking_Value for COV as it moves, ends the fade or
//! ramp on time, and reports the new Present_Value when an egress runs out.
//!
//! Table 13-1 reports a Lighting Output's Present_Value and Status_Flags, so
//! a SubscribeCOV hears a fade once, when the target goes in; a
//! SubscribeCOVProperty of Tracking_Value hears it move.

use super::cov_notifications_tests::recording_transport;
use super::cov_wire_test_support::real;
use super::*;
use crate::server::test_transport::{SendLog, TestTransport};
use bacnet_objects::device::{DeviceConfig, DeviceObject};
use bacnet_objects::lighting::LightingOutputObject;
use bacnet_objects::traits::BACnetObject;
use bacnet_services::cov::COVNotificationRequest;

const PV: PropertyIdentifier = PropertyIdentifier::PRESENT_VALUE;
const TV: PropertyIdentifier = PropertyIdentifier::TRACKING_VALUE;
const LC: PropertyIdentifier = PropertyIdentifier::LIGHTING_COMMAND;

/// The whole-object subscriber's process identifier.
const OBJECT: u32 = 1;
/// The Tracking_Value subscriber's process identifier.
const TRACKING: u32 = 2;

async fn start(
    configure: impl FnOnce(&mut LightingOutputObject),
) -> (BACnetServer<TestTransport>, ObjectIdentifier, SendLog) {
    let (transport, sent) = recording_transport();
    let mut object = LightingOutputObject::new(1, "LO-1").unwrap();
    configure(&mut object);
    let oid = object.object_identifier();
    let mut db = ObjectDatabase::new();
    db.add(Box::new(
        DeviceObject::new(DeviceConfig {
            identity: bacnet_objects::DeviceIdentity::new(1384, 0)
                .expect("valid Device identity")
                .with_name("Lighting-engine-device"),
            ..DeviceConfig::default()
        })
        .unwrap(),
    ))
    .unwrap();
    db.add(Box::new(object)).unwrap();
    let config = ServerConfig {
        device: bacnet_objects::DeviceIdentity::new(1384, 0).unwrap(),
        enable_event_enrollment: false,
        ..ServerConfig::default()
    };
    let server = BACnetServer::start(config, db, transport).await.unwrap();
    settle().await;
    (server, oid, sent)
}

async fn settle() {
    for _ in 0..16 {
        tokio::task::yield_now().await;
    }
}

/// Move paused time on by `milliseconds` and let the server catch up.
async fn advance(milliseconds: u64) {
    tokio::time::advance(Duration::from_millis(milliseconds)).await;
    settle().await;
}

/// Subscribe to the whole object, and to Tracking_Value with `increment`.
async fn subscribe(
    server: &BACnetServer<TestTransport>,
    oid: ObjectIdentifier,
    increment: Option<f32>,
) {
    let mut table = server.cov_table.write().await;
    for (process, property, cov_increment) in
        [(OBJECT, None, None), (TRACKING, Some(TV), increment)]
    {
        table
            .subscribe(CovSubscription {
                subscriber_mac: MacAddr::from_slice(&[127, 0, 0, 1, 0xBA, process as u8]),
                subscriber_network: None,
                subscriber_process_identifier: process,
                monitored_object_identifier: oid,
                issue_confirmed_notifications: false,
                expires_at: None,
                last_notified_observation: None,
                monitored_property: property,
                monitored_property_array_index: None,
                cov_increment,
                notification_kind: CovNotificationKind::Single,
                timestamped: false,
            })
            .unwrap();
    }
}

async fn write(
    server: &BACnetServer<TestTransport>,
    oid: ObjectIdentifier,
    property: PropertyIdentifier,
    value: PropertyValue,
    priority: Option<u8>,
) {
    server
        .write_local(
            &oid,
            property,
            None,
            value,
            priority,
            crate::LocalCommandSource::ServerDevice,
        )
        .await
        .unwrap();
}

/// A Lighting_Command write's value: the command's octets.
fn command(octets: &[u8]) -> PropertyValue {
    PropertyValue::ApplicationData(octets.to_vec())
}

/// The first value each COV notification sent since the last call carried,
/// with its subscriber process, as encoded, ordered by process.
fn reports(sent: &SendLog) -> Vec<(u32, PropertyIdentifier, Vec<u8>)> {
    let mut reports: Vec<_> = sent
        .take()
        .into_iter()
        .filter_map(|frame| match frame.apdu() {
            Apdu::UnconfirmedRequest(request)
                if request.service_choice
                    == UnconfirmedServiceChoice::UNCONFIRMED_COV_NOTIFICATION =>
            {
                let report = COVNotificationRequest::decode(&request.service_request).unwrap();
                let first = &report.list_of_values[0];
                Some((
                    report.subscriber_process_identifier,
                    first.property_identifier,
                    first.value.clone(),
                ))
            }
            _ => None,
        })
        .collect();
    // Subscribers are reported in no set order.
    reports.sort_by_key(|report| report.0);
    reports
}

async fn read(
    server: &BACnetServer<TestTransport>,
    oid: ObjectIdentifier,
    property: PropertyIdentifier,
) -> PropertyValue {
    server
        .database()
        .read()
        .await
        .get(&oid)
        .unwrap()
        .read_property(property, None)
        .unwrap()
}

#[tokio::test(start_paused = true)]
async fn a_fade_shorter_than_the_routine_pass_is_sampled_and_ended_on_time() {
    let (mut server, oid, sent) = start(|_| {}).await;
    // Past the task's first pass; its next routine one is at 2 s.
    advance(1_200).await;
    subscribe(&server, oid, None).await;
    // FADE_TO 90.0 % (0x42B40000) over 300 ms (0x012C) at priority 8.
    let fade = [
        0x09, 0x01, 0x1C, 0x42, 0xB4, 0x00, 0x00, 0x4A, 0x01, 0x2C, 0x59, 0x08,
    ];
    write(&server, oid, LC, command(&fade), None).await;
    // The write reports the new Present_Value, and a first Tracking_Value.
    assert_eq!(
        reports(&sent),
        [(OBJECT, PV, real(90.0)), (TRACKING, TV, real(0.0))]
    );
    // A 1 percent step is 3.3 ms of this fade, so each 100 ms grid point
    // samples, and the last is the end.
    for expected in [30.0, 60.0, 90.0] {
        advance(100).await;
        assert_eq!(reports(&sent), [(TRACKING, TV, real(expected))]);
    }
    assert_eq!(
        read(&server, oid, PropertyIdentifier::IN_PROGRESS).await,
        PropertyValue::Enumerated(0)
    );
    let deadline = server
        .database()
        .read()
        .await
        .get(&oid)
        .unwrap()
        .next_monotonic_deadline_internal();
    assert_eq!(deadline, None);
    advance(5_000).await;
    assert_eq!(reports(&sent), []);
    server.stop().await.unwrap();
}

#[tokio::test(start_paused = true)]
async fn a_ramp_reports_tracking_value_by_increment_and_present_value_once() {
    let (mut server, oid, sent) = start(|object| {
        object
            .write_property(
                PropertyIdentifier::COV_INCREMENT,
                None,
                PropertyValue::Real(25.0),
                None,
            )
            .unwrap();
    })
    .await;
    subscribe(&server, oid, Some(50.0)).await;
    // RAMP_TO 100.0 % (0x42C80000) at 50.0 %/s (0x42480000): 2 s.
    let ramp = [
        0x09, 0x02, 0x1C, 0x42, 0xC8, 0x00, 0x00, 0x2C, 0x42, 0x48, 0x00, 0x00,
    ];
    write(&server, oid, LC, command(&ramp), None).await;
    assert_eq!(
        reports(&sent),
        [(OBJECT, PV, real(100.0)), (TRACKING, TV, real(0.0))]
    );
    // COV_Increment 25 samples Tracking_Value every quarter, 0.5 s; the
    // subscription's own increment of 50 reports every other sample.
    let mut heard = Vec::new();
    for _ in 0..4 {
        advance(500).await;
        heard.push(reports(&sent));
    }
    assert_eq!(
        heard,
        [
            vec![],
            vec![(TRACKING, TV, real(50.0))],
            vec![],
            vec![(TRACKING, TV, real(100.0))],
        ]
    );
    assert_eq!(read(&server, oid, TV).await, PropertyValue::Real(100.0));
    server.stop().await.unwrap();
}

#[tokio::test(start_paused = true)]
async fn an_egress_reports_present_value_when_it_runs_out() {
    let (mut server, oid, sent) = start(|object| {
        for (property, value) in [
            (
                PropertyIdentifier::BLINK_WARN_ENABLE,
                PropertyValue::Boolean(true),
            ),
            (PropertyIdentifier::EGRESS_TIME, PropertyValue::Unsigned(2)),
        ] {
            object.write_property(property, None, value, None).unwrap();
        }
    })
    .await;
    write(&server, oid, PV, PropertyValue::Real(80.0), Some(8)).await;
    subscribe(&server, oid, None).await;
    // Present_Value -3.0 is WARN_OFF: blink, hold for 2 s, then 0.0.
    write(&server, oid, PV, PropertyValue::Real(-3.0), Some(8)).await;
    assert_eq!(
        reports(&sent),
        [(OBJECT, PV, real(80.0)), (TRACKING, TV, real(80.0))]
    );
    assert_eq!(
        read(&server, oid, PropertyIdentifier::EGRESS_ACTIVE).await,
        PropertyValue::Boolean(true)
    );
    advance(1_999).await;
    assert_eq!(reports(&sent), []);
    advance(1).await;
    assert_eq!(
        reports(&sent),
        [(OBJECT, PV, real(0.0)), (TRACKING, TV, real(0.0))]
    );
    assert_eq!(
        read(&server, oid, PropertyIdentifier::EGRESS_ACTIVE).await,
        PropertyValue::Boolean(false)
    );
    server.stop().await.unwrap();
}

#[tokio::test(start_paused = true)]
async fn a_trim_change_fades_in_on_the_task_and_reports_trim_active() {
    let (mut server, oid, sent) = start(|object| {
        object.set_high_end_trim(Some(100.0)).unwrap();
        object.set_trim_fade_time(300).unwrap();
    })
    .await;
    subscribe(&server, oid, None).await;
    write(&server, oid, PV, PropertyValue::Real(90.0), Some(8)).await;
    assert_eq!(
        reports(&sent),
        [(OBJECT, PV, real(90.0)), (TRACKING, TV, real(90.0))]
    );
    // High_End_Trim 60 (4194335): the bound moves from 100 down to 60 over
    // 300 ms, and Tracking_Value follows it from 90 down at each 100 ms grid
    // point. Present_Value stays 90, so whole-object subscribers hear
    // nothing.
    write(
        &server,
        oid,
        PropertyIdentifier::HIGH_END_TRIM,
        PropertyValue::Real(60.0),
        None,
    )
    .await;
    assert_eq!(reports(&sent), []);
    // TRIM_ACTIVE is 5.
    assert_eq!(
        read(&server, oid, PropertyIdentifier::IN_PROGRESS).await,
        PropertyValue::Enumerated(5)
    );
    // 100 - 40/3 is a little over 86.66, so the first grid point holds
    // Tracking_Value at the bound; the end holds it at the trim.
    let mut heard = Vec::new();
    for _ in 0..3 {
        advance(100).await;
        heard.push(reports(&sent));
    }
    assert_eq!(
        heard,
        [
            vec![(TRACKING, TV, real(100.0 - 40.0 / 3.0))],
            vec![(TRACKING, TV, real(100.0 - 80.0 / 3.0))],
            vec![(TRACKING, TV, real(60.0))],
        ]
    );
    assert_eq!(
        read(&server, oid, PropertyIdentifier::IN_PROGRESS).await,
        PropertyValue::Enumerated(5)
    );
    let deadline = server
        .database()
        .read()
        .await
        .get(&oid)
        .unwrap()
        .next_monotonic_deadline_internal();
    assert_eq!(deadline, None);
    server.stop().await.unwrap();
}

/// Subscribe `process` to Tracking_Value alone, with `increment`, and return
/// the subscription's key.
async fn subscribe_tracking(
    server: &BACnetServer<TestTransport>,
    oid: ObjectIdentifier,
    process: u32,
    increment: Option<f32>,
) -> crate::cov::CovSubscriptionKey {
    server
        .cov_table
        .write()
        .await
        .subscribe(CovSubscription {
            subscriber_mac: MacAddr::from_slice(&[127, 0, 0, 1, 0xBA, process as u8]),
            subscriber_network: None,
            subscriber_process_identifier: process,
            monitored_object_identifier: oid,
            issue_confirmed_notifications: false,
            expires_at: None,
            last_notified_observation: None,
            monitored_property: Some(TV),
            monitored_property_array_index: None,
            cov_increment: increment,
            notification_kind: CovNotificationKind::Single,
            timestamped: false,
        })
        .unwrap()
        .key()
        .clone()
}

/// Tracking_Value subscribers finer than COV_Increment get denser samples
/// (#1510): the finest one sets the step, at most one sample per 100 ms grid
/// point, and each subscriber still hears only its own steps.
#[tokio::test(start_paused = true)]
async fn the_finest_tracking_value_subscriber_sets_the_sample_step() {
    let (mut server, oid, sent) = start(|object| {
        object
            .write_property(
                PropertyIdentifier::COV_INCREMENT,
                None,
                PropertyValue::Real(25.0),
                None,
            )
            .unwrap();
    })
    .await;
    // Increments of 1, 10 and 50 percent on processes 3, 4 and 5.
    for (process, increment) in [(3, 1.0), (4, 10.0), (5, 50.0)] {
        subscribe_tracking(&server, oid, process, Some(increment)).await;
    }
    // RAMP_TO 100.0 % at 50.0 %/s, 5 percent each 100 ms, over 2 s.
    let ramp = [
        0x09, 0x02, 0x1C, 0x42, 0xC8, 0x00, 0x00, 0x2C, 0x42, 0x48, 0x00, 0x00,
    ];
    write(&server, oid, LC, command(&ramp), None).await;
    let first: Vec<_> = [3, 4, 5].map(|p| (p, TV, real(0.0))).into();
    assert_eq!(reports(&sent), first);
    // A 1 percent step is 20 ms, so the grid bounds it: a sample every
    // 100 ms, not every quarter as COV_Increment 25 alone would give.
    for tenth in 1..=20u8 {
        advance(100).await;
        let level = f32::from(tenth) * 5.0;
        let mut expected = vec![(3, TV, real(level))];
        if tenth % 2 == 0 {
            expected.push((4, TV, real(level)));
        }
        if tenth % 10 == 0 {
            expected.push((5, TV, real(level)));
        }
        assert_eq!(reports(&sent), expected, "at {} ms", u32::from(tenth) * 100);
    }
    server.stop().await.unwrap();
}

/// A subscriber increment finer than COV_Increment but coarser than the ramp
/// moves in one 100 ms grid cell sets the step itself, and once that
/// subscriber goes the object's own step comes back (#1510).
#[tokio::test(start_paused = true)]
async fn an_unsubscribe_gives_back_the_objects_own_sample_step() {
    let (mut server, oid, sent) = start(|object| {
        object
            .write_property(
                PropertyIdentifier::COV_INCREMENT,
                None,
                PropertyValue::Real(25.0),
                None,
            )
            .unwrap();
    })
    .await;
    // Process 2 gives no increment, so it hears every sample; process 3
    // asks for 10 percent.
    subscribe_tracking(&server, oid, 2, None).await;
    let fine = subscribe_tracking(&server, oid, 3, Some(10.0)).await;
    // RAMP_TO 100.0 % at 50.0 %/s, 5 percent each 100 ms, over 2 s.
    let ramp = [
        0x09, 0x02, 0x1C, 0x42, 0xC8, 0x00, 0x00, 0x2C, 0x42, 0x48, 0x00, 0x00,
    ];
    write(&server, oid, LC, command(&ramp), None).await;
    assert_eq!(reports(&sent), [(2, TV, real(0.0)), (3, TV, real(0.0))]);
    // 10 percent is two grid cells of this ramp: a sample every 200 ms, not
    // at every grid point.
    let mut heard = Vec::new();
    for _ in 0..4 {
        advance(100).await;
        heard.push(reports(&sent));
    }
    let both = |level| vec![(2, TV, real(level)), (3, TV, real(level))];
    assert_eq!(heard, [vec![], both(10.0), vec![], both(20.0)]);
    // Without process 3, the sample already planned for 600 ms stands; after
    // it, COV_Increment's quarter, 500 ms, is the step again.
    server.cov_table.write().await.unsubscribe(&fine);
    for at in (500..=2_000).step_by(100) {
        advance(100).await;
        let expected = match at {
            600 => vec![(2, TV, real(30.0))],
            1_100 => vec![(2, TV, real(55.0))],
            1_600 => vec![(2, TV, real(80.0))],
            2_000 => vec![(2, TV, real(100.0))],
            _ => vec![],
        };
        assert_eq!(reports(&sent), expected, "at {at} ms");
    }
    server.stop().await.unwrap();
}