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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
use super::*;
use crate::cov::MultipleRefusal;
use crate::runtime_clock;

fn cov_property_error(code: ErrorCode) -> Error {
    Error::protocol(ErrorClass::PROPERTY, code)
}

/// A refusal of one COV reference of a SubscribeCOVPropertyMultiple request,
/// which names the monitored object and the reference's property and array
/// index: the first-failed-subscription form of its error (Clause 13.16.2).
fn subscription_error(
    error: Error,
    monitored: ObjectIdentifier,
    reference: &bacnet_types::constructed::PropertyReference,
) -> Error {
    match error {
        Error::Remote(RemoteError::Error { class, code })
        | Error::Structured { class, code, .. } => Error::with_detail(
            class,
            code,
            Some(ErrorDetail::FirstFailedSubscription(
                BACnetObjectPropertyReference {
                    object_identifier: monitored,
                    property_identifier: reference.property_identifier.to_raw(),
                    property_array_index: reference.property_array_index,
                },
            )),
        ),
        other => other,
    }
}

fn validate_cov_property(
    object: &dyn bacnet_objects::traits::BACnetObject,
    property: PropertyIdentifier,
    array_index: Option<u32>,
) -> Result<(), Error> {
    let value = object
        .read_property(property, array_index)
        .map_err(|_| cov_property_error(ErrorCode::UNKNOWN_PROPERTY))?;
    if !object.supports_cov_property(property) {
        return Err(cov_property_error(ErrorCode::NOT_COV_PROPERTY));
    }
    crate::cov::prepare::validate_sample(object, property, array_index, &value)?;
    Ok(())
}

/// Handle a SubscribeCOV request.
///
/// Absent optional fields indicate a cancellation. Otherwise creates or updates
/// a subscription. Returns an error if the monitored object does not exist.
pub fn handle_subscribe_cov(
    table: &mut CovSubscriptionTable,
    db: &ObjectDatabase,
    source_mac: &[u8],
    service_data: &[u8],
) -> Result<(), Error> {
    handle_subscribe_cov_with_initial(table, db, source_mac, service_data).map(|_| ())
}

pub(crate) fn handle_subscribe_cov_with_initial(
    table: &mut CovSubscriptionTable,
    db: &ObjectDatabase,
    source_mac: &[u8],
    service_data: &[u8],
) -> Result<Vec<CovSubscriptionSnapshot>, Error> {
    handle_subscribe_cov_with_initial_endpoint(table, db, source_mac, None, service_data)
}

pub(crate) fn handle_subscribe_cov_with_initial_endpoint(
    table: &mut CovSubscriptionTable,
    db: &ObjectDatabase,
    source_mac: &[u8],
    source_network: Option<&NpduAddress>,
    service_data: &[u8],
) -> Result<Vec<CovSubscriptionSnapshot>, Error> {
    let request = SubscribeCOVRequest::decode(service_data).map_err(Error::into_request_reject)?;

    // Service consistency is distinct from structural parsing. Validate before
    // lookup, expiry purge or subscription replacement/initial notification.
    if request.lifetime.is_some() && request.issue_confirmed_notifications.is_none() {
        return Err(Error::reject(RejectReason::INCONSISTENT_PARAMETERS));
    }

    if request.is_cancellation() {
        table.unsubscribe(&CovSubscriptionKey::Object {
            recipient: crate::cov::CovRecipient::from_endpoint(source_mac, source_network),
            process_id: request.subscriber_process_identifier,
            object: request.monitored_object_identifier,
        });
        return Ok(Vec::new());
    }

    match db.get(&request.monitored_object_identifier) {
        None => {
            return Err(Error::protocol(
                ErrorClass::OBJECT,
                ErrorCode::UNKNOWN_OBJECT,
            ));
        }
        Some(obj) if !obj.supports_cov() => {
            return Err(Error::protocol(
                ErrorClass::OBJECT,
                ErrorCode::OPTIONAL_FUNCTIONALITY_NOT_SUPPORTED,
            ));
        }
        _ => {}
    }

    let expires_at = request.lifetime.and_then(|secs| {
        if secs == 0 {
            None
        } else {
            Some(runtime_clock::now() + Duration::from_secs(secs as u64))
        }
    });

    let subscription = CovSubscription {
        subscriber_mac: MacAddr::from_slice(source_mac),
        subscriber_network: source_network.cloned(),
        subscriber_process_identifier: request.subscriber_process_identifier,
        monitored_object_identifier: request.monitored_object_identifier,
        issue_confirmed_notifications: request.issue_confirmed_notifications.unwrap_or(false),
        expires_at,
        last_notified_observation: None,
        monitored_property: None,
        monitored_property_array_index: None,
        cov_increment: None,
        notification_kind: CovNotificationKind::Single,
        timestamped: false,
    };
    Ok(vec![table.subscribe(subscription)?])
}

/// Handle a SubscribeCOVProperty request.
///
/// Like SubscribeCOV but subscribes to changes on a specific property.
pub fn handle_subscribe_cov_property(
    table: &mut CovSubscriptionTable,
    db: &ObjectDatabase,
    source_mac: &[u8],
    service_data: &[u8],
) -> Result<(), Error> {
    handle_subscribe_cov_property_with_initial(table, db, source_mac, service_data).map(|_| ())
}

pub(crate) fn handle_subscribe_cov_property_with_initial(
    table: &mut CovSubscriptionTable,
    db: &ObjectDatabase,
    source_mac: &[u8],
    service_data: &[u8],
) -> Result<Vec<CovSubscriptionSnapshot>, Error> {
    handle_subscribe_cov_property_with_initial_endpoint(table, db, source_mac, None, service_data)
}

pub(crate) fn handle_subscribe_cov_property_with_initial_endpoint(
    table: &mut CovSubscriptionTable,
    db: &ObjectDatabase,
    source_mac: &[u8],
    source_network: Option<&NpduAddress>,
    service_data: &[u8],
) -> Result<Vec<CovSubscriptionSnapshot>, Error> {
    use bacnet_services::cov::SubscribeCOVPropertyRequest;

    let request =
        SubscribeCOVPropertyRequest::decode(service_data).map_err(Error::into_request_reject)?;

    // Keep service validity separate from structural decoding so malformed
    // pairing and zero lifetime retain their distinct formal responses.
    let lifetime = match (request.issue_confirmed_notifications, request.lifetime) {
        (None, None) => None,
        (Some(_), Some(0)) => {
            return Err(Error::protocol(
                ErrorClass::SERVICES,
                ErrorCode::VALUE_OUT_OF_RANGE,
            ));
        }
        (Some(_), Some(lifetime)) => Some(lifetime),
        _ => {
            return Err(Error::reject(RejectReason::INCONSISTENT_PARAMETERS));
        }
    };

    if request.is_cancellation() {
        table.unsubscribe(&CovSubscriptionKey::Property {
            recipient: crate::cov::CovRecipient::from_endpoint(source_mac, source_network),
            process_id: request.subscriber_process_identifier,
            object: request.monitored_object_identifier,
            property: request.monitored_property_identifier,
            index: request.monitored_property_array_index,
        });
        return Ok(Vec::new());
    }

    let object = db
        .get(&request.monitored_object_identifier)
        .ok_or(Error::protocol(
            ErrorClass::OBJECT,
            ErrorCode::UNKNOWN_OBJECT,
        ))?;

    // An object can take property subscriptions without supporting
    // SubscribeCOV (Averaging, which Table 13-1 doesn't list).
    if !object.supports_subscribe_cov_property() {
        return Err(Error::protocol(
            ErrorClass::OBJECT,
            ErrorCode::OPTIONAL_FUNCTIONALITY_NOT_SUPPORTED,
        ));
    }

    validate_cov_property(
        object,
        request.monitored_property_identifier,
        request.monitored_property_array_index,
    )?;

    let expires_at =
        lifetime.map(|secs| runtime_clock::now() + Duration::from_secs(u64::from(secs)));

    let subscription = CovSubscription {
        subscriber_mac: MacAddr::from_slice(source_mac),
        subscriber_network: source_network.cloned(),
        subscriber_process_identifier: request.subscriber_process_identifier,
        monitored_object_identifier: request.monitored_object_identifier,
        issue_confirmed_notifications: request.issue_confirmed_notifications.unwrap_or(false),
        expires_at,
        last_notified_observation: None,
        monitored_property: Some(request.monitored_property_identifier),
        monitored_property_array_index: request.monitored_property_array_index,
        cov_increment: request.cov_increment,
        notification_kind: CovNotificationKind::Single,
        timestamped: false,
    };
    Ok(vec![table.subscribe(subscription)?])
}
/// Handle a SubscribeCOVPropertyMultiple request.
///
/// Creates individual COV subscriptions for each property in each object
/// referenced by the request, in request order. A failed reference ends the
/// request with an error naming it; the references before it stay subscribed
/// (Clause 13.16.2).
pub fn handle_subscribe_cov_property_multiple(
    table: &mut CovSubscriptionTable,
    db: &ObjectDatabase,
    source_mac: &[u8],
    service_data: &[u8],
) -> Result<(), Error> {
    handle_subscribe_cov_property_multiple_with_initial(table, db, source_mac, service_data)
        .map(|_| ())
        .map_err(|refusal| refusal.error)
}

pub(crate) fn handle_subscribe_cov_property_multiple_with_initial(
    table: &mut CovSubscriptionTable,
    db: &ObjectDatabase,
    source_mac: &[u8],
    service_data: &[u8],
) -> Result<Vec<CovSubscriptionSnapshot>, MultipleRefusal> {
    handle_subscribe_cov_property_multiple_with_initial_endpoint(
        table,
        db,
        source_mac,
        None,
        service_data,
    )
}

pub(crate) fn handle_subscribe_cov_property_multiple_with_initial_endpoint(
    table: &mut CovSubscriptionTable,
    db: &ObjectDatabase,
    source_mac: &[u8],
    source_network: Option<&NpduAddress>,
    service_data: &[u8],
) -> Result<Vec<CovSubscriptionSnapshot>, MultipleRefusal> {
    use bacnet_services::cov_multiple::SubscribeCOVPropertyMultipleRequest;

    let request = SubscribeCOVPropertyMultipleRequest::decode(service_data)
        .map_err(Error::into_request_reject)?;
    handle_subscribe_cov_property_multiple_request_endpoint(
        table,
        db,
        source_mac,
        source_network,
        None,
        request,
    )
}

/// `subscriber_max_apdu` is the max-APDU-length-accepted of the request's
/// header, when the caller has it; the context's notifications fit it.
///
/// A failure before any COV reference is processed (inconsistent or out of
/// range timing) keeps nothing and carries the bare class and code. Otherwise
/// the references are processed in request order until one fails, whether
/// its object, its property, its timestamping or the subscription caps refuse
/// it (#1059, #1102): the error names that reference, and the
/// [`MultipleRefusal`] carries the references kept before it, which are owed
/// their initial notifications like an accepted request's (#1058).
pub(crate) fn handle_subscribe_cov_property_multiple_request_endpoint(
    table: &mut CovSubscriptionTable,
    db: &ObjectDatabase,
    source_mac: &[u8],
    source_network: Option<&NpduAddress>,
    subscriber_max_apdu: Option<u16>,
    request: bacnet_services::cov_multiple::SubscribeCOVPropertyMultipleRequest,
) -> Result<Vec<CovSubscriptionSnapshot>, MultipleRefusal> {
    let confirmed = request.issue_confirmed_notifications;
    let route = SubscriberEndpoint::new(source_mac, source_network);
    let context = MultipleContextKey {
        recipient: crate::cov::CovRecipient::from_endpoint(source_mac, source_network),
        process_id: request.subscriber_process_identifier,
        confirmed,
    };
    let cancellation = match (request.lifetime, request.max_notification_delay) {
        (None, None) => true,
        (Some(_), Some(_)) => false,
        _ => {
            return Err(Error::reject(RejectReason::INCONSISTENT_PARAMETERS).into());
        }
    };

    if cancellation {
        if request.list_of_cov_subscription_specifications.is_empty() {
            table.unsubscribe_cov_multiple_context(&context);
        } else {
            for spec in &request.list_of_cov_subscription_specifications {
                for cov_ref in &spec.list_of_cov_references {
                    table.unsubscribe(&CovSubscriptionKey::Multiple {
                        context: context.clone(),
                        object: spec.monitored_object_identifier,
                        property: cov_ref.monitored_property.property_identifier,
                        index: cov_ref.monitored_property.property_array_index,
                    });
                }
            }
        }
        return Ok(Vec::new());
    }

    let timestamped = request
        .list_of_cov_subscription_specifications
        .iter()
        .flat_map(|spec| &spec.list_of_cov_references)
        .any(|cov_ref| cov_ref.timestamped);
    // One clock sample both admits timestamped references and stamps their
    // initial report, so a clock turning invalid in between cannot cost it.
    let admission_clock = timestamped
        .then(|| db.clock_frame())
        .flatten()
        .filter(|frame| frame.is_valid_actual_datetime());

    let lifetime = request.lifetime.expect("validated COV-multiple lifetime");
    let max_notification_delay = request
        .max_notification_delay
        .expect("validated COV-multiple max notification delay");
    if lifetime == 0
        || max_notification_delay > 3600
        || u64::from(max_notification_delay) >= u64::from(lifetime)
    {
        return Err(Error::protocol(ErrorClass::SERVICES, ErrorCode::VALUE_OUT_OF_RANGE).into());
    }
    let expires_at = runtime_clock::now() + Duration::from_secs(u64::from(lifetime));
    let subscriber_mac = MacAddr::from_slice(source_mac);
    // The references that pass validation, in request order, up to the first
    // that fails; `names` holds each one's object and property reference.
    let mut subscriptions = Vec::new();
    let mut names = Vec::new();
    let mut failure = None;

    'specs: for spec in &request.list_of_cov_subscription_specifications {
        let monitored = spec.monitored_object_identifier;
        let object = match db.get(&monitored) {
            Some(object) if object.supports_subscribe_cov_property() => object,
            found => {
                let code = match found {
                    None => ErrorCode::UNKNOWN_OBJECT,
                    Some(_) => ErrorCode::OPTIONAL_FUNCTIONALITY_NOT_SUPPORTED,
                };
                let error = Error::protocol(ErrorClass::OBJECT, code);
                // A refusal of the object is a refusal of its first COV
                // reference, the first one that could not be processed.
                failure = Some(match spec.list_of_cov_references.first() {
                    Some(first) => subscription_error(error, monitored, &first.monitored_property),
                    None => error,
                });
                break;
            }
        };

        for cov_ref in &spec.list_of_cov_references {
            let property_identifier = cov_ref.monitored_property.property_identifier;
            let property_array_index = cov_ref.monitored_property.property_array_index;

            // Timestamped is an option of each reference, so a missing clock
            // fails the reference that asks for timestamps, after its object
            // and property, rather than the request (Clause 13.16.2, #1102).
            // Neither error table has a row for it; SERVICES /
            // OPTIONAL_FUNCTIONALITY_NOT_SUPPORTED is Clause 18.5's code for
            // a parameter needing functionality the device lacks, the pair
            // DeviceCommunicationControl gives a clockless device (16.1.1.3).
            let refused = validate_cov_property(object, property_identifier, property_array_index)
                .and_then(|()| {
                    if cov_ref.timestamped && admission_clock.is_none() {
                        Err(Error::protocol(
                            ErrorClass::SERVICES,
                            ErrorCode::OPTIONAL_FUNCTIONALITY_NOT_SUPPORTED,
                        ))
                    } else {
                        Ok(())
                    }
                });
            if let Err(error) = refused {
                failure = Some(subscription_error(
                    error,
                    monitored,
                    &cov_ref.monitored_property,
                ));
                break 'specs;
            }

            subscriptions.push(CovSubscription {
                subscriber_mac: subscriber_mac.clone(),
                subscriber_network: source_network.cloned(),
                subscriber_process_identifier: request.subscriber_process_identifier,
                monitored_object_identifier: monitored,
                issue_confirmed_notifications: confirmed,
                expires_at: Some(expires_at),
                last_notified_observation: None,
                monitored_property: Some(property_identifier),
                monitored_property_array_index: property_array_index,
                cov_increment: cov_ref.cov_increment,
                notification_kind: CovNotificationKind::Multiple,
                timestamped: cov_ref.timestamped,
            });
            names.push((monitored, &cov_ref.monitored_property));
        }
    }

    // A failed first reference leaves nothing processed, so nothing changes;
    // not even an existing context's lifetime is renewed.
    let validated = subscriptions.len();
    if validated == 0
        && let Some(error) = failure
    {
        return Err(MultipleRefusal {
            error,
            refused: Some(0),
            committed: Vec::new(),
        });
    }
    let (accepted, refusal) = match table.subscribe_multiple(
        &context,
        &route,
        expires_at,
        max_notification_delay,
        subscriber_max_apdu,
        subscriptions,
    ) {
        Ok(accepted) => (accepted, failure.map(|error| (validated, error))),
        // A subscription cap stopped admission before the validated
        // references ran out. A context has no storage or limit of its own,
        // so a new one whose first reference finds no room is refused here
        // too, and the general context error is never sent (#1102).
        Err(MultipleRefusal {
            error,
            refused: Some(position),
            committed,
        }) => {
            let (monitored, reference) = names[position];
            let error = subscription_error(error, monitored, reference);
            (committed, Some((position, error)))
        }
        Err(whole) => return Err(whole),
    };
    if let Some(frame) = admission_clock {
        table.initial_timed_capture(&accepted, frame).run(db);
    }
    match refusal {
        None => Ok(accepted),
        Some((position, error)) => Err(MultipleRefusal {
            error,
            refused: Some(position),
            committed: accepted,
        }),
    }
}