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
//! Object-owned trend polling schedule. The caller owns synchronization and timers.
use std::collections::{HashMap, HashSet};
use std::time::Duration;
use bacnet_encoding::constructed::encode_log_multiple_record;
use bacnet_encoding::primitives::encode_property_value;
use bacnet_types::constructed::{
BACnetDeviceObjectPropertyReference, BACnetLogMultipleRecord, BACnetLogRecord, LogData,
LogDatum, LogValue,
};
use bacnet_types::enums::{
ErrorClass, ErrorCode, LoggingType, ObjectType, PropertyIdentifier as P,
};
use bacnet_types::error::Error;
use bacnet_types::primitives::{Date, ObjectIdentifier, PropertyValue, Time};
use bytes::BytesMut;
use tracing::warn;
use super::{LocalDevice, ObjectDatabase};
use crate::device_reference::decode_reference;
use crate::log_buffer::ANY_VALUE_MAX_OCTETS;
use crate::traits::{BACnetObject, MonotonicClock};
use alignment::{Alignment, DAY};
mod alignment;
/// Local maximum idle/configuration reconciliation delay and failure backoff.
/// This is a scheduling policy, not a BACnet timing guarantee.
const RECONCILE: Duration = Duration::from_millis(100);
#[derive(PartialEq)]
struct Configuration {
mode: Mode,
/// Log_DeviceObjectProperty as read, compared whole for scheduling
/// ownership.
reference: PropertyValue,
/// What each acquisition reads: the one reference of a Trend Log, or every
/// element of a Trend Log Multiple in array order.
members: Vec<Member>,
}
/// When a log acquires.
#[derive(Debug, Clone, Copy, PartialEq)]
enum Mode {
/// POLLED: every `interval` hundredths, counted from each acquisition.
Polled { interval: u32 },
/// POLLED with Align_Intervals (Clauses 12.25.27 and 12.30.14): at each
/// local time of day `offset` hundredths past a multiple of `interval`,
/// which divides a day. `offset` is Interval_Offset modulo the interval
/// (12.25.28 and 12.30.15).
Aligned { interval: u32, offset: u32 },
/// TRIGGERED with Trigger TRUE: one acquisition now (Clauses 12.25.29
/// and 12.30.16).
Triggered,
}
/// One monitored reference of a log.
#[derive(Debug, Clone, Copy, PartialEq)]
enum Member {
/// A Trend Log Multiple element naming object or device instance 4194303,
/// which Clause 12.30.11 treats as empty, or the unset reference of a
/// triggered Trend Log, which reads the same way (#1417).
Unspecified,
/// A property to read, at one element when `index` is present.
Reference {
target: ObjectIdentifier,
property: P,
index: Option<u32>,
/// The reference's optional Device member.
device: Option<ObjectIdentifier>,
},
}
struct Schedule {
configuration: Configuration,
last_success: Option<Duration>,
/// Latest failed attempt, independent of the last accepted sample.
retry_completed: Option<Duration>,
/// An aligned log's plan, kept on the Device clock.
alignment: Alignment,
}
impl Schedule {
fn new(configuration: Configuration) -> Self {
Self {
configuration,
last_success: None,
retry_completed: None,
alignment: Alignment::default(),
}
}
fn remaining(&self, now: Duration) -> Duration {
if let Some(failed) = self.retry_completed {
return RECONCILE.saturating_sub(now.saturating_sub(failed));
}
match (self.configuration.mode, self.last_success) {
(Mode::Polled { interval }, Some(success)) => {
hundredths(interval).saturating_sub(now.saturating_sub(success))
}
(Mode::Aligned { interval, offset }, _) => self.alignment.wait(now, interval, offset),
_ => Duration::ZERO,
}
}
}
fn hundredths(value: u32) -> Duration {
Duration::from_millis(u64::from(value) * 10)
}
#[derive(Default)]
pub(super) struct TrendPollSchedule(HashMap<ObjectIdentifier, Schedule>);
impl TrendPollSchedule {
pub(super) fn retire(&mut self, oid: &ObjectIdentifier) {
self.0.remove(oid);
}
pub(super) fn clear(&mut self) {
self.0.clear();
}
}
impl ObjectDatabase {
/// Poll every due local Trend Log or Trend Log Multiple synchronously and
/// return the next wait.
///
/// Each acquisition reads every reference of the log: a Trend Log's one,
/// or each Log_DeviceObjectProperty element of a Trend Log Multiple, whose
/// record then carries one value per element in array order (#1203).
/// Only this database is read. A reference whose Device member names
/// another device (see [`ObjectDatabase::local_device`]) is never read
/// here, even when a same-numbered local object exists: its value is a
/// failure, PROPERTY / OPTIONAL_FUNCTIONALITY_NOT_SUPPORTED. A Trend Log
/// Multiple element naming instance 4194303 is empty and fails with
/// PROPERTY / NO_PROPERTY_SPECIFIED. A reference with an array index reads
/// that element (#1205). A local read that fails yields the error a
/// ReadProperty of it would report: OBJECT / UNKNOWN_OBJECT for a missing
/// object, PROPERTY / PROPERTY_IS_NOT_AN_ARRAY for an index on a property
/// that isn't an array, or the read's own error, such as INVALID_ARRAY_INDEX
/// for an index past the end (#1183).
///
/// Only POLLED logs with a nonzero Log_Interval and at least one reference
/// are polled. A TRIGGERED log whose Trigger reads TRUE makes one
/// acquisition, and the object clears Trigger once it accepts the record
/// (Clauses 12.25.29 and 12.30.16). A Trend Log Multiple without members
/// records no values for it, and a Trend Log without a reference records
/// PROPERTY / NO_PROPERTY_SPECIFIED, so Trigger never stays TRUE. With
/// Align_Intervals TRUE and a Log_Interval that divides a day, a POLLED
/// log acquires when the Device clock's time of day is Interval_Offset
/// (modulo the interval) past a multiple of the interval (Clauses
/// 12.25.27-28 and 12.30.14-15); the first acquisition waits for such a
/// boundary. Such a log is judged against the Device clock on every pass,
/// so a clock change moves its plan with it (see the `alignment` module).
///
/// Every pass also lets each Trend Log, Trend Log Multiple and Event Log
/// look at its Start_Time / Stop_Time window, with or without a
/// monotonic clock, so one that opens or closes is recorded within a pass
/// even when no record arrives. An Event Log takes its records from the
/// application and the device's notifications, never from this poller,
/// but this pass is the one regular visit every log gets.
///
/// The caller must hold exclusive database access for this whole call. The
/// bound monotonic clock drives scheduling; the shared Device clock
/// provides each actual acquisition timestamp. Without either clock an
/// attempt cannot succeed. A successful call to an object's insertion hook,
/// including its accepted disabled/count-only outcomes, starts the
/// configured interval.
///
/// Log_Interval is in hundredths. Deadlines follow actual completion, without
/// catch-up bursts. Invalid timestamps and insertion errors preserve the last
/// success and retry after 100 ms. The returned wait is at most 100 ms so a
/// timer-driven caller also reconciles configuration changes and idle logs.
/// Slow synchronous work can make another object due; a minimum 1 ms yield
/// avoids a zero-delay loop. These bounds are local policy, not real-time
/// guarantees. Clock, object reads and insertion hooks must remain bounded.
pub fn poll_trend_logs(&mut self) -> Duration {
let mut logs = self.find_by_type(ObjectType::TREND_LOG);
logs.extend(self.find_by_type(ObjectType::TREND_LOG_MULTIPLE));
let trend_logs = logs.len();
logs.extend(self.find_by_type(ObjectType::EVENT_LOG));
// Every log looks at its window, Event Logs included, whether or not
// a monotonic clock drives acquisition.
for oid in &logs {
if let Some(log) = self.get_mut(oid) {
log.refresh_log_window_internal();
}
}
logs.truncate(trend_logs);
let Some(monotonic) = self.monotonic_clock.clone() else {
return RECONCILE;
};
let mut eligible = HashSet::new();
let local = self.local_device();
for oid in logs {
// Exclusive access prevents structural change after selection.
let Some(configuration) = self.get(&oid).and_then(configuration) else {
continue;
};
eligible.insert(oid);
if self
.trend_poll
.0
.get(&oid)
.is_none_or(|entry| entry.configuration != configuration)
{
self.trend_poll.0.insert(oid, Schedule::new(configuration));
}
self.poll_one(oid, local, &*monotonic);
}
self.trend_poll.0.retain(|oid, _| eligible.contains(oid));
let now = monotonic();
let remaining = self
.trend_poll
.0
.values()
.map(|entry| entry.remaining(now))
.min()
.unwrap_or(RECONCILE)
.min(RECONCILE);
if remaining.is_zero() {
Duration::from_millis(1)
} else {
remaining
}
}
/// Acquire for `oid`'s schedule entry when it is due.
fn poll_one(&mut self, oid: ObjectIdentifier, local: LocalDevice, monotonic: &MonotonicClock) {
let now = monotonic();
let entry = &self.trend_poll.0[&oid];
let mode = entry.configuration.mode;
// An aligned log looks at the Device clock on every pass, so its plan
// follows a clock change; the others wait for their deadline. Both
// wait out the retry delay after a failed attempt.
let waiting = match mode {
Mode::Aligned { .. } => entry
.retry_completed
.is_some_and(|failed| now.saturating_sub(failed) < RECONCILE),
_ => !entry.remaining(now).is_zero(),
};
if waiting {
return;
}
let frame = self
.clock_frame()
.filter(|frame| frame.is_valid_actual_datetime());
let entry = self.trend_poll.0.get_mut(&oid).unwrap();
let mut boundary = None;
if let Mode::Aligned { interval, offset } = mode {
// Without a clock there is no boundary to find; look again later.
let Some(frame) = frame else {
entry.retry_completed = Some(now);
return;
};
entry.retry_completed = None;
boundary = entry.alignment.due(now, &frame, interval, offset);
if boundary.is_none() {
return;
}
}
let accepted = frame.is_some_and(|frame| {
let values: Vec<LogValue> = self.trend_poll.0[&oid]
.configuration
.members
.iter()
.map(|member| self.acquire(local, member))
.collect();
let object = self.get_mut(&oid).unwrap();
let inserted = if oid.object_type() == ObjectType::TREND_LOG_MULTIPLE {
object.add_trend_multiple_record(BACnetLogMultipleRecord {
date: frame.local_date,
time: frame.local_time,
log_data: LogData::Values(values),
})
} else {
object.add_trend_record(BACnetLogRecord {
date: frame.local_date,
time: frame.local_time,
// A Trend Log has exactly one reference.
log_datum: values
.into_iter()
.next()
.map_or(LogDatum::NullValue, LogDatum::from),
status_flags: None,
})
};
inserted
.inspect_err(|error| {
warn!(object = %oid, %error, "trend-log record insertion failed");
})
.is_ok()
});
let completed = monotonic();
if accepted && mode == Mode::Triggered {
// Served: the next Trigger starts a fresh entry.
self.trend_poll.0.remove(&oid);
return;
}
let entry = self.trend_poll.0.get_mut(&oid).unwrap();
if !accepted {
entry.retry_completed = Some(completed);
return;
}
entry.last_success = Some(completed);
entry.retry_completed = None;
if let Some(boundary) = boundary {
entry.alignment.serve(boundary);
}
}
/// One reference's value, or the failure that stopped its read
/// (Clause 12.25 Log_Buffer; Clause 12.30.19 for each member).
fn acquire(&self, local: LocalDevice, member: &Member) -> LogValue {
let Member::Reference {
target,
property,
index,
device,
} = *member
else {
return failure(ErrorClass::PROPERTY, ErrorCode::NO_PROPERTY_SPECIFIED);
};
if !local.is_local(device) {
// Reading another device's property is not supported here.
return failure(
ErrorClass::PROPERTY,
ErrorCode::OPTIONAL_FUNCTIONALITY_NOT_SUPPORTED,
);
}
let Some(target) = self.get(&target) else {
return failure(ErrorClass::OBJECT, ErrorCode::UNKNOWN_OBJECT);
};
// ReadProperty's gate, which the Averaging sampler shares: only an
// array has elements, whatever an object's read arm does with the
// index.
if index.is_some() && !target.is_array_property(property) {
return failure(ErrorClass::PROPERTY, ErrorCode::PROPERTY_IS_NOT_AN_ARRAY);
}
match target.read_property(property, index) {
Ok(value) => property_value_to_log_value(&value),
Err(Error::Protocol { class, code } | Error::Structured { class, code, .. }) => {
LogValue::Failure {
error_class: class,
error_code: code,
}
}
// An error with no BACnet class and code reports as ReadProperty
// would answer it.
Err(_) => failure(ErrorClass::SERVICES, ErrorCode::OTHER),
}
}
}
fn failure(class: ErrorClass, code: ErrorCode) -> LogValue {
LogValue::Failure {
error_class: u32::from(class.to_raw()),
error_code: u32::from(code.to_raw()),
}
}
/// What a log's properties ask of the poller now; `None` when nothing.
fn configuration(object: &dyn BACnetObject) -> Option<Configuration> {
let read = |property| object.read_property(property, None).ok();
let logging_type = match read(P::LOGGING_TYPE) {
Some(PropertyValue::Enumerated(value)) => LoggingType::from_raw(value),
_ => LoggingType::POLLED,
};
let mode = match logging_type {
// An object without a Trigger row is never triggered.
LoggingType::TRIGGERED => {
(read(P::TRIGGER) == Some(PropertyValue::Boolean(true))).then_some(Mode::Triggered)?
}
LoggingType::POLLED => {
let Some(PropertyValue::Unsigned(raw)) = read(P::LOG_INTERVAL) else {
return None;
};
let interval = u32::try_from(raw).ok().filter(|value| *value > 0)?;
let offset = match read(P::INTERVAL_OFFSET) {
Some(PropertyValue::Unsigned(offset)) => (offset % u64::from(interval)) as u32,
_ => 0,
};
let aligned = read(P::ALIGN_INTERVALS) == Some(PropertyValue::Boolean(true));
// Clause 12.30.14 aligns only an interval that goes evenly into
// one of its clock periods; each of those goes evenly into a day,
// so going evenly into a day is the same test.
if aligned && DAY.is_multiple_of(interval) {
Mode::Aligned { interval, offset }
} else {
Mode::Polled { interval }
}
}
_ => return None,
};
let reference = read(P::LOG_DEVICE_OBJECT_PROPERTY)?;
let members = if object.object_identifier().object_type() == ObjectType::TREND_LOG_MULTIPLE {
let PropertyValue::List(elements) = &reference else {
return None;
};
elements.iter().map(member).collect::<Option<Vec<_>>>()?
} else {
match member(&reference)? {
// A log with no reference polls nothing. A Trigger on one is
// still served, with a failure, so it doesn't stay TRUE.
Member::Unspecified if mode != Mode::Triggered => return None,
member => vec![member],
}
};
// A Trigger is served even with no members, so it never stays TRUE.
if members.is_empty() && mode != Mode::Triggered {
return None;
}
Some(Configuration {
mode,
reference,
members,
})
}
/// One reference as a read serves it: a single
/// BACnetDeviceObjectPropertyReference in its Clause 21 encoding (#1234).
/// Anything else leaves the log unpolled. An unset reference, its object or
/// Device at instance 4194303, names nothing: an empty Trend Log Multiple
/// element (Clause 12.30.11), or a Trend Log without a reference (#1417).
fn member(value: &PropertyValue) -> Option<Member> {
let reference: BACnetDeviceObjectPropertyReference = decode_reference(value).ok()?;
if reference.is_unset() {
return Some(Member::Unspecified);
}
Some(Member::Reference {
target: reference.object_identifier,
property: P::from_raw(reference.property_identifier),
index: reference.property_array_index,
device: reference.device_identifier,
})
}
/// A read value as the log stores it: one of the datatypes both record kinds
/// name, or else the any-value alternative holding the value's own encoding,
/// the bytes a ReadProperty of it would carry (Clauses 12.25.14 and
/// 12.30.19).
///
/// An any-value over [`ANY_VALUE_MAX_OCTETS`] logs PROPERTY /
/// VALUE_TOO_LONG instead, so every record stays small enough to page. A
/// value no record could carry (one that fails to encode, a bit string with
/// impossible padding, framed bytes whose tags don't balance) logs SERVICES
/// / OTHER, so the log never refuses what the poller hands it.
fn property_value_to_log_value(value: &PropertyValue) -> LogValue {
let value = match value {
PropertyValue::Real(v) => LogValue::RealValue(*v),
PropertyValue::Unsigned(v) => LogValue::UnsignedValue(*v),
PropertyValue::Signed(v) => LogValue::SignedValue(i64::from(*v)),
PropertyValue::Boolean(v) => LogValue::BooleanValue(*v),
PropertyValue::Enumerated(v) => LogValue::EnumValue(u64::from(*v)),
PropertyValue::BitString { unused_bits, data } => LogValue::BitstringValue {
unused_bits: *unused_bits,
data: data.clone(),
},
PropertyValue::Null => LogValue::NullValue,
other => {
let mut encoded = BytesMut::new();
if encode_property_value(&mut encoded, other).is_err() {
// A value the read could not have carried either.
return failure(ErrorClass::SERVICES, ErrorCode::OTHER);
}
if encoded.len() > ANY_VALUE_MAX_OCTETS {
return failure(ErrorClass::PROPERTY, ErrorCode::VALUE_TOO_LONG);
}
LogValue::AnyValue(encoded.to_vec())
}
};
if !matches!(
value,
LogValue::BitstringValue { .. } | LogValue::AnyValue(_)
) {
return value;
}
// Only these two alternatives can hold something no record carries;
// encoding a one-member record tells.
let probe = BACnetLogMultipleRecord {
date: Date {
year: 0,
month: 1,
day: 1,
day_of_week: 1,
},
time: Time {
hour: 0,
minute: 0,
second: 0,
hundredths: 0,
},
log_data: LogData::Values(vec![value.clone()]),
};
match encode_log_multiple_record(&probe, &mut BytesMut::new()) {
Ok(()) => value,
Err(_) => failure(ErrorClass::SERVICES, ErrorCode::OTHER),
}
}
#[cfg(test)]
mod tests;