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
//! BACnet error types.
//!
//! Provides the top-level [`Error`] type used throughout the library,
//! covering protocol errors, encoding/decoding failures, transport issues,
//! and timeouts.
//!
//! Without `alloc` it keeps only the variants that own nothing: a decoder's
//! [`DecodeError`], an encoder's [`EncodeError`], a constructor's
//! [`RangeError`] and a segmenter's [`SegmentationError`] carry static
//! labels or numbers and build their text when displayed (#1646, #1802).
//! The variants holding a `String` or a `Box` need `alloc`.
#[cfg(all(feature = "alloc", not(feature = "std")))]
use alloc::{boxed::Box, string::String, vec::Vec};
use core::fmt;
use core::time::Duration;
#[cfg(feature = "alloc")]
use crate::constructed::BACnetObjectPropertyReference;
use crate::data_link::DataLink;
#[cfg(feature = "alloc")]
use crate::enums::PropertyIdentifier;
use crate::enums::{AbortReason, ErrorClass, ErrorCode, RejectReason};
mod decode;
mod encode;
mod remote;
pub use decode::{DecodeError, DecodingKind, RangeError};
pub use encode::{EncodeError, SegmentationError};
#[cfg(feature = "alloc")]
use remote::ProtocolText;
pub use remote::RemoteError;
/// [`Error::LogNotAdvancing`]'s text.
struct NotAdvancing {
requested: u64,
returned: Option<u64>,
}
impl fmt::Display for NotAdvancing {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let requested = self.requested;
match self.returned {
Some(returned) => write!(
f,
"asked for sequence number {requested}, the device answered from {returned}"
),
None => write!(
f,
"asked for sequence number {requested}, which the log's counts say it holds, \
the device answered with no records"
),
}
}
}
/// Top-level error type for the BACnet library.
///
/// It is `#[non_exhaustive]`, so a `match` on it outside this crate needs a
/// wildcard arm. Some variants exist only with a feature, and any crate in a
/// build can turn that on: `Error::Transport` needs `std`, and the variants
/// that hold a `String` or a `Box` need `alloc`. Without the attribute a
/// match written against a smaller build would stop compiling (#1604).
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
// Clippy fails if the attribute above goes.
#[deny(clippy::exhaustive_enums)]
pub enum Error {
/// A peer's Error, Reject or Abort PDU that turned a confirmed request
/// down (Clauses 20.1.5 to 20.1.7). An Error PDU whose Clause 21
/// production carries more than the class and code is
/// `Error::Structured` (with `alloc`) instead.
#[error("{0}")]
Remote(RemoteError),
/// BACnet error response whose Clause 21 production carries more than the
/// class and code: ChangeList-Error, CreateObject-Error,
/// WritePropertyMultiple-Error, SubscribeCOVPropertyMultiple-Error,
/// ConfirmedPrivateTransfer-Error or VTClose-Error. `detail` holds the
/// extra fields; a body whose optional fields are all absent is reported
/// as [`Error::Remote`]. Only with `alloc`.
#[cfg(feature = "alloc")]
#[error("{}, {detail}", ProtocolText { class: *.class, code: *.code })]
Structured {
/// Error class (Clause 18).
class: ErrorClass,
/// Error code (Clause 18).
code: ErrorCode,
/// The production's fields after the class and code. Boxed so the
/// rare structured error does not widen every `Result<_, Error>`.
detail: Box<ErrorDetail>,
},
/// A router reported that the active message was too long for a routed path.
#[error("message is too long for routed path to DNET {dnet}")]
RoutedPathTooLong {
/// Destination network rejected by the router.
dnet: u16,
},
/// The client cannot safely allocate state for another routed path.
#[error("routed path state capacity of {capacity} entries is exhausted")]
RoutedPathCapacityExceeded {
/// Maximum number of immediate-router/DNET path entries retained.
capacity: usize,
},
/// The endpoint's transport cannot carry the requested operation, such as
/// a BBMD management request through a data link other than BACnet/IP.
/// Nothing was sent.
#[error("operation requires {required}; this transport is {actual}")]
UnsupportedTransport {
/// Data link the operation needs.
required: DataLink,
/// Data link the endpoint's transport carries.
actual: DataLink,
},
/// A definite length refusal before any bytes of this send were written
/// or admitted to a transport's write queue. The limit may apply to the
/// NPDU or its full link envelope, including data attributes. An I/O
/// failure, including an uncertain partial write, must not use this.
#[error("send refused before transmission: {length} octets exceed the {limit}-octet limit")]
SendTooLong {
/// Length of the NPDU or encoded link frame that was refused.
length: usize,
/// The corresponding bound when this send was refused.
limit: usize,
},
/// Error encoding a PDU, with a formatted message. Only with `alloc`;
/// [`Error::Encode`] is the static form, which the codec crates return.
#[cfg(feature = "alloc")]
#[error("encoding error: {0}")]
Encoding(String),
/// A value an encoder or validator refused, with a static label
/// (#1802). The encoders and validators of `bacnet-encoding` and
/// `bacnet-services` report this form.
#[error("{0}")]
Encode(EncodeError),
/// Error decoding received data, with a static label and its numbers
/// (#1646, #1776). Every decoder reports this form.
#[error("{0}")]
Decoding(DecodeError),
/// Transport-level I/O error. Only with the `std` feature, which has
/// `std::io`.
#[cfg(feature = "std")]
#[error("transport error: {0}")]
Transport(#[from] std::io::Error),
/// Request timed out.
#[error("request timed out after {0:?}")]
Timeout(Duration),
/// A payload that can't be segmented, segments that can't be
/// reassembled, or a segmented request a peer can't take (#1802).
#[error("segmentation error: {0}")]
Segmentation(SegmentationError),
/// Buffer too short for the expected data.
#[error("buffer too short: need {need} bytes, have {have}")]
BufferTooShort {
/// Minimum bytes needed.
need: usize,
/// Bytes actually available.
have: usize,
},
/// Value out of valid range, with a formatted message. Only with
/// `alloc`; [`Error::Range`] is the static form.
#[cfg(feature = "alloc")]
#[error("value out of range: {0}")]
OutOfRange(String),
/// A value a checked constructor refused, with a static label (#1646),
/// such as an object type or instance
/// [`ObjectIdentifier::new`](crate::primitives::ObjectIdentifier::new)
/// can't encode.
#[error("{0}")]
Range(RangeError),
/// A well-formed ReadRange acknowledgement that breaks a rule the client
/// checks against the request it answers, so the client refused it. The
/// acknowledgement decoded; a malformed one is [`Error::Decoding`]
/// instead. Read again leniently to keep its items.
#[error("ReadRange acknowledgement refused: {0}")]
ReadRangeViolation(ReadRangeViolation),
/// A paged log read asked for the records from one sequence number and
/// the device answered with a page starting before it, so reading on
/// would repeat records instead of advancing; or answered with none
/// although its record counts say it holds that record. Some devices
/// clamp a reference past their sequence wrap back to the oldest record,
/// or number the record after the wrap 0; read such a log by position
/// instead.
#[error(
"log read is not advancing: {}",
NotAdvancing { requested: *.requested, returned: *.returned }
)]
LogNotAdvancing {
/// The sequence number the read asked for.
requested: u64,
/// What the device numbered the first record it sent;
/// `None` when it returned no records.
returned: Option<u64>,
},
}
impl From<RemoteError> for Error {
fn from(error: RemoteError) -> Self {
Self::Remote(error)
}
}
impl From<DecodeError> for Error {
fn from(error: DecodeError) -> Self {
Self::Decoding(error)
}
}
impl From<EncodeError> for Error {
fn from(error: EncodeError) -> Self {
Self::Encode(error)
}
}
impl From<SegmentationError> for Error {
fn from(error: SegmentationError) -> Self {
Self::Segmentation(error)
}
}
impl From<RangeError> for Error {
fn from(error: RangeError) -> Self {
Self::Range(error)
}
}
/// A rule a ReadRange acknowledgement can break against the request it
/// answers (Clause 15.8).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ReadRangeViolation {
/// The object identifier isn't the one the request named.
ObjectMismatch,
/// The property identifier isn't the one the request named.
PropertyMismatch,
/// The array index isn't the one the request named, or is present when
/// the request had none, or the reverse.
ArrayIndexMismatch,
/// A nonempty answer to a By-Sequence or By-Time read without the
/// sequence number of its first item (Clause 15.8.1.2.7).
MissingFirstSequenceNumber,
/// A nonempty answer to a By-Sequence or By-Time read that numbers its
/// first item 0, a number logs never assign: they count from 1 and skip
/// 0 when they wrap. Some devices do number the record after the wrap 0.
ZeroFirstSequenceNumber,
/// A first sequence number in the answer to a read that was by position
/// or had no range, which carries none (Clause 15.8.1.2.7).
UnexpectedFirstSequenceNumber,
/// MORE_ITEMS together with the flag that says the page reached the end
/// a ranged read moves toward: LAST_ITEM for a forward read, FIRST_ITEM
/// for a backward one. Items left out of a full page lie beyond that
/// end, so the two contradict each other (Clause 15.8.2). A read with no
/// range may keep either end, so for it only all three flags together
/// break the rule.
MoreItemsPastEnd,
/// More items than the request's count asked for.
ItemCountExceedsRequest,
}
impl ReadRangeViolation {
/// The rule's name in snake case, as the Python bindings report it.
pub fn name(self) -> &'static str {
match self {
Self::ObjectMismatch => "object_mismatch",
Self::PropertyMismatch => "property_mismatch",
Self::ArrayIndexMismatch => "array_index_mismatch",
Self::MissingFirstSequenceNumber => "missing_first_sequence_number",
Self::ZeroFirstSequenceNumber => "zero_first_sequence_number",
Self::UnexpectedFirstSequenceNumber => "unexpected_first_sequence_number",
Self::MoreItemsPastEnd => "more_items_past_end",
Self::ItemCountExceedsRequest => "item_count_exceeds_request",
}
}
}
impl core::fmt::Display for ReadRangeViolation {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str(match self {
Self::ObjectMismatch => "object identifier does not match the request",
Self::PropertyMismatch => "property identifier does not match the request",
Self::ArrayIndexMismatch => "array index does not match the request",
Self::MissingFirstSequenceNumber => {
"nonempty By Sequence/Time answer has no first sequence number"
}
Self::ZeroFirstSequenceNumber => {
"nonempty By Sequence/Time answer has first sequence number 0"
}
Self::UnexpectedFirstSequenceNumber => {
"answer to a read by position or without a range has a first sequence number"
}
Self::MoreItemsPastEnd => {
"MORE_ITEMS is set with the flag for the end a ranged read moves toward, \
or with both FIRST_ITEM and LAST_ITEM on a read without a range"
}
Self::ItemCountExceedsRequest => "more items than the request's count",
})
}
}
/// The fields a Clause 21 error production adds to the error class and code,
/// one variant per shape. The service the error answers names the production.
/// Only with `alloc`.
#[cfg(feature = "alloc")]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ErrorDetail {
/// ChangeList-Error (AddListElement, RemoveListElement; Clauses 15.1.1.3
/// and 15.2.1.3) and CreateObject-Error (Clause 15.3.1.3): the 1-based
/// position of the refused element in the request's List of Elements or
/// List of Initial Values, or 0 when the failure is not about one element.
/// An object refusing one element of a list-valued write uses it too, for
/// that element's position in the list it was given; the server's list
/// handler turns that into the request's position.
FirstFailedElementNumber(u32),
/// WritePropertyMultiple-Error (Clause 15.10.1.3): the object, property
/// and array index of the first write that failed.
FirstFailedWriteAttempt(BACnetObjectPropertyReference),
/// SubscribeCOVPropertyMultiple-Error (Clause 13.16.1.3) in its
/// first-failed-subscription form: the monitored object and the property
/// reference of the COV reference that failed. The other form, a general
/// error, has no detail.
FirstFailedSubscription(BACnetObjectPropertyReference),
/// ConfirmedPrivateTransfer-Error (Clause 16.2.1.3): the vendor and
/// service the error answers, and any vendor-defined error parameters.
PrivateTransfer {
/// Vendor identifier of the private service.
vendor_id: u32,
/// Vendor-defined service number.
service_number: u32,
/// Error parameters as encoded inside their context tag 3 frame,
/// opaque to the stack; `None` when absent.
error_parameters: Option<Vec<u8>>,
},
/// VTClose-Error (Clause 17.3.1.3) with its list present: the
/// requester's local identifiers of the VT sessions that could not be
/// closed. Without the list the error has no detail.
VtSessionIdentifiers(Vec<u8>),
}
#[cfg(feature = "alloc")]
impl core::fmt::Display for ErrorDetail {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
fn reference(
f: &mut core::fmt::Formatter<'_>,
what: &str,
reference: &BACnetObjectPropertyReference,
) -> core::fmt::Result {
write!(
f,
"{what} {} {}",
reference.object_identifier,
PropertyIdentifier::from_raw(reference.property_identifier)
)?;
match reference.property_array_index {
Some(index) => write!(f, "[{index}]"),
None => Ok(()),
}
}
match self {
Self::FirstFailedElementNumber(number) => write!(f, "first failed element {number}"),
Self::FirstFailedWriteAttempt(attempt) => {
reference(f, "first failed write attempt", attempt)
}
Self::FirstFailedSubscription(subscription) => {
reference(f, "first failed subscription", subscription)
}
Self::PrivateTransfer {
vendor_id,
service_number,
error_parameters,
} => {
write!(f, "vendor {vendor_id} service {service_number}")?;
match error_parameters {
Some(parameters) => {
write!(f, ", {} octets of error parameters", parameters.len())
}
None => Ok(()),
}
}
Self::VtSessionIdentifiers(sessions) => {
f.write_str("VT sessions not closed:")?;
for session in sessions {
write!(f, " {session}")?;
}
Ok(())
}
}
}
}
/// Convenience alias for `Result<T, Error>`.
pub type Result<T> = core::result::Result<T, Error>;
impl Error {
/// An Error PDU's [`Error::Remote`] with its class and code.
pub const fn protocol(class: ErrorClass, code: ErrorCode) -> Self {
Self::Remote(RemoteError::Error { class, code })
}
/// A Reject PDU's [`Error::Remote`] with its reason.
pub const fn reject(reason: RejectReason) -> Self {
Self::Remote(RemoteError::Reject { reason })
}
/// An Abort PDU's [`Error::Remote`] with its reason.
pub const fn abort(reason: AbortReason) -> Self {
Self::Remote(RemoteError::Abort { reason })
}
/// The error an Error PDU reports: [`Error::Structured`] when its body
/// carried a detail, [`Error::Remote`] otherwise. Only with `alloc`.
#[cfg(feature = "alloc")]
pub fn with_detail(class: ErrorClass, code: ErrorCode, detail: Option<ErrorDetail>) -> Self {
match detail {
Some(detail) => Self::Structured {
class,
code,
detail: Box::new(detail),
},
None => Self::protocol(class, code),
}
}
/// The peer's answer this error reports, without a structured error's
/// detail: the [`RemoteError`] of [`Error::Remote`], and an Error with
/// the class and code of `Error::Structured`. `None` for any other
/// error, which no peer sent.
pub fn remote(&self) -> Option<RemoteError> {
match self {
Self::Remote(error) => Some(*error),
#[cfg(feature = "alloc")]
Self::Structured { class, code, .. } => Some(RemoteError::Error {
class: *class,
code: *code,
}),
_ => None,
}
}
/// Create a [`DecodingKind::InvalidEncoding`] decoding error at the given
/// byte offset: contents whose encoding isn't valid for their datatype.
/// Build a [`DecodeError`] directly for a label that names a context or
/// a number.
pub fn decoding(offset: usize, label: &'static str) -> Self {
Self::decoding_kind(DecodingKind::InvalidEncoding, offset, label)
}
/// Create an [`Error::Decoding`] of `kind` at the given byte offset,
/// described by a static `label`.
pub fn decoding_kind(kind: DecodingKind, offset: usize, label: &'static str) -> Self {
Self::Decoding(DecodeError::new(kind, offset, label))
}
/// Create a [`DecodingKind::InvalidTag`] decoding error: a tag at
/// `offset` that doesn't fit there.
pub fn invalid_tag(offset: usize, label: &'static str) -> Self {
Self::decoding_kind(DecodingKind::InvalidTag, offset, label)
}
/// Create a [`DecodingKind::Missing`] decoding error: a required member
/// due at `offset` isn't there.
pub fn missing(offset: usize, label: &'static str) -> Self {
Self::decoding_kind(DecodingKind::Missing, offset, label)
}
/// Create a [`DecodingKind::Trailing`] decoding error: octets from
/// `offset` on that the value leaves unread.
pub fn trailing(offset: usize, label: &'static str) -> Self {
Self::decoding_kind(DecodingKind::Trailing, offset, label)
}
/// Create a [`DecodingKind::OutOfRange`] decoding error: a value at
/// `offset` too large for its field, or outside its range.
pub fn out_of_range(offset: usize, label: &'static str) -> Self {
Self::decoding_kind(DecodingKind::OutOfRange, offset, label)
}
/// Create a [`DecodingKind::Overflow`] decoding error: more items, or a
/// longer tag, than the decoder takes.
pub fn overflow(offset: usize, label: &'static str) -> Self {
Self::decoding_kind(DecodingKind::Overflow, offset, label)
}
/// This error, an inner reader's refusal, as a static [`DecodeError`] at
/// `offset`, for a decoder that reports it as its own fault there and
/// names its own context or item (#1776). A decoding error keeps its
/// kind, context, label, values and index; a range error is
/// [`DecodingKind::OutOfRange`] with its label and values; a short buffer
/// is [`DecodingKind::Missing`] with its sizes. Any other error, which a
/// decoder doesn't return, is an [`DecodingKind::InvalidEncoding`]
/// labelled `malformed`.
pub fn to_decode_error(&self, offset: usize) -> DecodeError {
let mut refused = match self {
Self::Decoding(refused) => *refused,
Self::Range(refused) => {
let mut range = DecodeError::new(DecodingKind::OutOfRange, offset, refused.label);
range.values = refused.values;
range
}
Self::BufferTooShort { need, have } => DecodeError::new(
DecodingKind::Missing,
offset,
"buffer too short: need {} bytes, have {}",
)
.with_values(*need as u64, *have as u64),
_ => DecodeError::new(DecodingKind::InvalidEncoding, offset, "malformed"),
};
refused.offset = offset;
refused
}
/// The offset and kind of an [`Error::Decoding`]; `None` for any other
/// error.
pub fn decode_fault(&self) -> Option<(usize, DecodingKind)> {
match self {
Self::Decoding(error) => Some((error.offset, error.kind)),
_ => None,
}
}
/// The Reject reason a responder answers a confirmed request with when
/// this error refused it while decoding its parameters (Clauses 18.9 and
/// 20.1.8): the reason itself for a Reject's [`Error::Remote`], the kind's reason
/// for a decoding error (the table on
/// [`DecodingKind::reject_reason`]), and MISSING_REQUIRED_PARAMETER for
/// [`Error::BufferTooShort`], a member cut short because the data ran out
/// before it was complete. `None` for any other error, which no decoder
/// of the request's parameters reports.
pub fn reject_reason(&self) -> Option<RejectReason> {
match self {
Self::Remote(RemoteError::Reject { reason }) => Some(*reason),
Self::BufferTooShort { .. } => Some(RejectReason::MISSING_REQUIRED_PARAMETER),
_ => self.decode_fault().map(|(_, kind)| kind.reject_reason()),
}
}
/// This error, met decoding a confirmed request's own parameters, as the
/// Reject's [`Error::Remote`] a responder answers with: the
/// [`reject_reason`](Self::reject_reason) when it is a syntax fault, the
/// error unchanged otherwise.
///
/// A responder converts only the request's decode with this. A decoding
/// error met later, once the service has started to run, is no reason to
/// reject the request (Clause 20.1.8), so it stays as it is.
pub fn into_request_reject(self) -> Self {
match self.reject_reason() {
Some(reason) => Self::reject(reason),
None => self,
}
}
/// Create an [`Error::Encode`] described by a static `label`. Build an
/// [`EncodeError`] directly for a label that names a context or a
/// number.
pub fn encode(label: &'static str) -> Self {
Self::Encode(EncodeError::new(label))
}
/// Create an [`Error::Range`] described by a static `label`. Build a
/// [`RangeError`] directly for a label with numbers.
pub fn range(label: &'static str) -> Self {
Self::Range(RangeError::new(label))
}
/// This error, met checking octets an encoder was handed to copy onto
/// the wire, such as a captured raw value, as the encoder's own
/// [`Error::Encode`] (#1802). A decoder's refusal keeps its offset (see
/// [`EncodeError::from_decode`]); a short buffer or another refusal keeps
/// the label and numbers [`to_decode_error`](Self::to_decode_error) gives
/// it, without an offset; an [`Error::Encode`] stays as it is.
pub fn into_encode_refusal(self) -> Self {
match self {
Self::Encode(_) => self,
Self::Decoding(refused) => Self::Encode(EncodeError::from_decode(&refused)),
other => Self::Encode(EncodeError::restating(&other.to_decode_error(0))),
}
}
/// Create a buffer-too-short error.
pub fn buffer_too_short(need: usize, have: usize) -> Self {
Self::BufferTooShort { need, have }
}
}
#[cfg(test)]
mod tests;