Skip to main content

rustbac_client/
lib.rs

1//! High-level async BACnet client.
2//!
3//! [`BacnetClient`] wraps any [`DataLink`](rustbac_datalink::DataLink)
4//! transport and exposes ergonomic methods for common BACnet operations
5//! such as reading properties, discovering devices, and subscribing to
6//! change-of-value (COV) notifications.
7
8/// Alarm and event services (GetAlarmSummary, GetEventInformation, etc.).
9pub mod alarm;
10/// Core [`BacnetClient`] type and transport setup.
11pub mod client;
12/// Change-of-value (COV) notification types.
13pub mod cov;
14/// COV subscriptions with renewal and polling failover.
15pub mod cov_manager;
16/// Device and object discovery (Who-Is / I-Am / Who-Has).
17pub mod discovery;
18/// Client-level error type.
19pub mod error;
20/// Atomic file read/write operations.
21pub mod file;
22/// Long-running async notification listener.
23pub mod listener;
24/// Point type inference for BACnet objects.
25pub mod point;
26/// ReadRange results and related types.
27pub mod range;
28/// Schedule and Calendar convenience helpers.
29pub mod schedule;
30/// BACnet server/responder implementation.
31pub mod server;
32/// Lightweight simulated BACnet device.
33pub mod simulator;
34/// Per-device request throttling utility.
35pub mod throttle;
36/// Owned application-data values for client-side use.
37pub mod value;
38/// Device discovery walk — reads all objects and their properties.
39pub mod walk;
40
41pub use alarm::{
42    AlarmSummaryItem, EnrollmentSummaryItem, EventInformationItem, EventInformationResult,
43    EventNotification,
44};
45pub use client::{BacnetClient, ForeignDeviceRenewal};
46pub use cov::{CovNotification, CovPropertyValue};
47pub use cov_manager::{
48    CovManager, CovManagerBuilder, CovSubscriptionSpec, CovUpdate, UpdateSource,
49};
50pub use discovery::{DiscoveredDevice, DiscoveredObject};
51pub use error::ClientError;
52pub use file::{AtomicReadFileResult, AtomicWriteFileResult};
53pub use listener::{create_notification_listener, Notification, NotificationListener};
54pub use point::{PointClassification, PointDirection, PointKind};
55pub use range::{ClientBitString, ReadRangeResult};
56pub use rustbac_bacnet_sc::BacnetScTransport;
57pub use rustbac_core::services::acknowledge_alarm::{EventState, TimeStamp};
58pub use rustbac_core::services::device_management::{DeviceCommunicationState, ReinitializeState};
59pub use rustbac_datalink::bip::transport::{BroadcastDistributionEntry, ForeignDeviceTableEntry};
60pub use schedule::{CalendarEntry, DateRange, TimeValue};
61pub use server::{
62    encode_unconfirmed_cov_notification, BacnetServer, BacnetServiceError, CovSubscriptionManager,
63    ObjectStore, ObjectStoreHandler, ServiceHandler,
64};
65pub use simulator::SimulatedDevice;
66pub use throttle::DeviceThrottle;
67pub use value::ClientDataValue;
68pub use walk::{DeviceInfo, DeviceWalkResult, ObjectSummary};
69
70// Internal helpers used by simulator module.
71use rustbac_core::encoding::{primitives::decode_unsigned, reader::Reader, tag::Tag};
72use rustbac_core::types::ObjectId;
73
74fn decode_ctx_unsigned(r: &mut Reader<'_>) -> Result<u32, ClientError> {
75    match Tag::decode(r)? {
76        Tag::Context { len, .. } => Ok(decode_unsigned(r, len as usize)?),
77        _ => Err(ClientError::UnsupportedResponse),
78    }
79}
80
81fn decode_ctx_object_id(r: &mut Reader<'_>) -> Result<ObjectId, ClientError> {
82    Ok(ObjectId::from_raw(decode_ctx_unsigned(r)?))
83}
84
85fn data_value_to_client(value: rustbac_core::types::DataValue<'_>) -> ClientDataValue {
86    match value {
87        rustbac_core::types::DataValue::Null => ClientDataValue::Null,
88        rustbac_core::types::DataValue::Boolean(v) => ClientDataValue::Boolean(v),
89        rustbac_core::types::DataValue::Unsigned(v) => ClientDataValue::Unsigned(v),
90        rustbac_core::types::DataValue::Signed(v) => ClientDataValue::Signed(v),
91        rustbac_core::types::DataValue::Real(v) => ClientDataValue::Real(v),
92        rustbac_core::types::DataValue::Double(v) => ClientDataValue::Double(v),
93        rustbac_core::types::DataValue::OctetString(v) => ClientDataValue::OctetString(v.to_vec()),
94        rustbac_core::types::DataValue::CharacterString(v) => {
95            ClientDataValue::CharacterString(v.to_string())
96        }
97        rustbac_core::types::DataValue::BitString(v) => ClientDataValue::BitString {
98            unused_bits: v.unused_bits,
99            data: v.data.to_vec(),
100        },
101        rustbac_core::types::DataValue::Enumerated(v) => ClientDataValue::Enumerated(v),
102        rustbac_core::types::DataValue::Date(v) => ClientDataValue::Date(v),
103        rustbac_core::types::DataValue::Time(v) => ClientDataValue::Time(v),
104        rustbac_core::types::DataValue::ObjectId(v) => ClientDataValue::ObjectId(v),
105        rustbac_core::types::DataValue::Constructed { tag_num, values } => {
106            ClientDataValue::Constructed {
107                tag_num,
108                values: values.into_iter().map(data_value_to_client).collect(),
109            }
110        }
111    }
112}