embedded-bacnet 0.2.0

A bacnet library for embedded systems (no_std)
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
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
use core::fmt::Display;

use crate::{
    application_protocol::{
        confirmed::{ComplexAck, ComplexAckService, ConfirmedServiceChoice},
        primitives::data_value::ApplicationDataValue,
    },
    common::{
        daily_schedule::WeeklySchedule,
        error::Error,
        helper::{
            decode_context_object_id, decode_context_property_id, decode_unsigned,
            encode_closing_tag, encode_context_enumerated, encode_context_object_id,
            encode_context_unsigned, encode_opening_tag, get_tagged_body, get_tagged_body_for_tag,
        },
        io::{Reader, Writer},
        object_id::{ObjectId, ObjectType},
        property_id::PropertyId,
        spec::{ErrorClass, ErrorCode, BACNET_ARRAY_ALL},
        tag::{ApplicationTagNumber, Tag, TagNumber},
    },
    network_protocol::data_link::DataLink,
};

#[derive(Debug, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct ReadPropertyMultipleAck<'a> {
    pub objects_with_results: &'a [ObjectWithResults<'a>],
    buf: &'a [u8],
}

impl<'a> IntoIterator for &'_ ReadPropertyMultipleAck<'a> {
    type Item = Result<ObjectWithResults<'a>, Error>;

    type IntoIter = ObjectWithResultsIter<'a>;

    fn into_iter(self) -> Self::IntoIter {
        ObjectWithResultsIter {
            buf: self.buf,
            reader: Reader::new_with_len(self.buf.len()),
        }
    }
}

impl<'a> TryFrom<DataLink<'a>> for ReadPropertyMultipleAck<'a> {
    type Error = Error;

    fn try_from(value: DataLink<'a>) -> Result<Self, Self::Error> {
        let ack: ComplexAck = value.try_into()?;
        match ack.service {
            ComplexAckService::ReadPropertyMultiple(ack) => Ok(ack),
            _ => Err(Error::ConvertDataLink(
                "apdu message is not a ComplexAckService ReadPropertyMultipleAck",
            )),
        }
    }
}

#[derive(Debug, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct ObjectWithResults<'a> {
    pub object_id: ObjectId,
    pub property_results: PropertyResultList<'a>,
}

impl<'a> ObjectWithResults<'a> {
    pub fn encode(&self, writer: &mut Writer) {
        encode_context_object_id(writer, 0, &self.object_id);
        encode_opening_tag(writer, 1);
        self.property_results.encode(writer);
        encode_closing_tag(writer, 1);
    }

    pub fn decode(reader: &mut Reader, buf: &'a [u8]) -> Result<Self, Error> {
        let object_id =
            decode_context_object_id(reader, buf, 0, "ObjectWithResults decode object_id")?;
        let buf =
            get_tagged_body_for_tag(reader, buf, 1, "ObjectWithResults decode list of results")?;

        let property_results = PropertyResultList {
            object_id,
            buf,
            property_results: &[],
        };

        Ok(ObjectWithResults {
            object_id,
            property_results,
        })
    }
}

impl<'a> IntoIterator for &'_ PropertyResultList<'a> {
    type Item = Result<PropertyResult<'a>, Error>;
    type IntoIter = PropertyResultIter<'a>;

    fn into_iter(self) -> Self::IntoIter {
        PropertyResultIter {
            buf: self.buf,
            reader: Reader::new_with_len(self.buf.len()),
            object_id: self.object_id,
        }
    }
}

impl<'a> Iterator for PropertyResultIter<'a> {
    type Item = Result<PropertyResult<'a>, Error>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.reader.eof() {
            return None;
        }

        Some(PropertyResult::decode(
            &mut self.reader,
            self.buf,
            &self.object_id,
        ))
    }
}

fn read_error(reader: &mut Reader, buf: &[u8]) -> Result<PropertyAccessError, Error> {
    // error class enumerated
    let tag = Tag::decode_expected(
        reader,
        buf,
        TagNumber::Application(ApplicationTagNumber::Enumerated),
        "read_error error_class",
    )?;
    let value = decode_unsigned(tag.value, reader, buf)? as u32;
    let error_class = value
        .try_into()
        .map_err(|x| Error::InvalidVariant(("ErrorClass", x)))?;

    // error code enumerated
    let tag = Tag::decode_expected(
        reader,
        buf,
        TagNumber::Application(ApplicationTagNumber::Enumerated),
        "read_error error code",
    )?;
    let value = decode_unsigned(tag.value, reader, buf)? as u32;
    let error_code = value
        .try_into()
        .map_err(|x| Error::InvalidVariant(("ErrorCode", x)))?;

    Ok(PropertyAccessError {
        error_class,
        error_code,
    })
}

#[derive(Debug, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct PropertyResultList<'a> {
    pub property_results: &'a [PropertyResult<'a>],
    object_id: ObjectId,
    buf: &'a [u8],
}

#[derive(Debug, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct PropertyResultIter<'a> {
    object_id: ObjectId,
    reader: Reader,
    buf: &'a [u8],
}

impl<'a> PropertyResultList<'a> {
    pub fn new(property_results: &'a [PropertyResult<'a>]) -> Self {
        Self {
            property_results,
            object_id: ObjectId::new(ObjectType::Invalid, 0),
            buf: &[],
        }
    }

    pub fn encode(&self, writer: &mut Writer) {
        for item in self.property_results {
            item.encode(writer);
        }
    }
}

#[derive(Debug, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct PropertyResult<'a> {
    pub id: PropertyId,
    pub value: PropertyValue<'a>,
}

impl<'a> PropertyResult<'a> {
    const PROPERTY_ID_TAG: u8 = 2;

    pub fn encode(&self, writer: &mut Writer) {
        encode_context_unsigned(writer, Self::PROPERTY_ID_TAG, self.id as u32);
        self.value.encode(writer);
    }

    pub fn decode(reader: &mut Reader, buf: &'a [u8], object_id: &ObjectId) -> Result<Self, Error> {
        let property_id = decode_context_property_id(
            reader,
            buf,
            Self::PROPERTY_ID_TAG,
            "PropertyResultList next property_id",
        )?;

        let value = PropertyValue::decode(reader, buf, object_id, &property_id)?;

        Ok(PropertyResult {
            id: property_id,
            value,
        })
    }
}

#[derive(Debug, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum PropertyValue<'a> {
    PropValue(ApplicationDataValue<'a>),
    PropError(PropertyAccessError),
    // TODO: figure out if we need these
    PropDescription(&'a str),
    PropObjectName(&'a str),
}

impl<'a> PropertyValue<'a> {
    const PROPERTY_VALUE_TAG: u8 = 4;
    const PROPERTY_ERROR_TAG: u8 = 5;

    pub fn encode(&self, writer: &mut Writer) {
        match self {
            Self::PropValue(val) => {
                encode_opening_tag(writer, Self::PROPERTY_VALUE_TAG);
                val.encode(writer);
                encode_closing_tag(writer, Self::PROPERTY_VALUE_TAG);
            }
            Self::PropError(_) => todo!(),
            Self::PropObjectName(_) => todo!(),
            Self::PropDescription(_) => todo!(),
        }
    }

    pub fn decode(
        reader: &mut Reader,
        buf: &'a [u8],
        object_id: &ObjectId,
        property_id: &PropertyId,
    ) -> Result<Self, Error> {
        let (buf, tag_number) = get_tagged_body(reader, buf)?;
        let mut reader = Reader {
            index: 0,
            end: buf.len(),
        };

        let property_value = match tag_number {
            Self::PROPERTY_VALUE_TAG => {
                match &property_id {
                    PropertyId::PropEventTimeStamps => {
                        // ignore for now
                        PropertyValue::PropValue(ApplicationDataValue::Boolean(false))
                    }
                    PropertyId::PropWeeklySchedule => {
                        let weekly_schedule = WeeklySchedule::decode(&mut reader, buf)?;
                        PropertyValue::PropValue(ApplicationDataValue::WeeklySchedule(
                            weekly_schedule,
                        ))
                    }
                    property_id => {
                        let tag = Tag::decode(&mut reader, buf)?;
                        let value = ApplicationDataValue::decode(
                            &tag,
                            object_id,
                            property_id,
                            &mut reader,
                            buf,
                        )?;
                        PropertyValue::PropValue(value)
                    }
                }
            }
            Self::PROPERTY_ERROR_TAG => {
                // property read error
                let error = read_error(&mut reader, buf)?;
                PropertyValue::PropError(error)
            }
            x => {
                return Err(Error::TagNotSupported((
                    "PropertyResultList next",
                    TagNumber::ContextSpecificOpening(x),
                )));
            }
        };

        Ok(property_value)
    }
}

#[derive(Debug, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct PropertyAccessError {
    pub error_class: ErrorClass,
    pub error_code: ErrorCode,
}

impl<'a> Display for PropertyValue<'a> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match &self {
            Self::PropValue(x) => write!(f, "{}", x),
            _ => write!(f, "property value unprintable",),
        }
    }
}

impl<'a> ReadPropertyMultipleAck<'a> {
    pub fn new(objects_with_results: &'a [ObjectWithResults<'a>]) -> Self {
        Self {
            objects_with_results,
            buf: &[],
        }
    }

    pub fn new_from_buf(buf: &'a [u8]) -> Self {
        Self {
            buf,
            objects_with_results: &[],
        }
    }

    pub fn encode(&self, writer: &mut Writer) {
        writer.push(ConfirmedServiceChoice::ReadPropMultiple as u8);
        for item in self.objects_with_results {
            item.encode(writer);
        }
    }
}

pub struct ObjectWithResultsIter<'a> {
    buf: &'a [u8],
    reader: Reader,
}

impl<'a> Iterator for ObjectWithResultsIter<'a> {
    type Item = Result<ObjectWithResults<'a>, Error>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.reader.eof() {
            return None;
        }

        let object_with_results = ObjectWithResults::decode(&mut self.reader, self.buf);
        Some(object_with_results)
    }
}

#[derive(Debug, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct ReadPropertyMultiple<'a> {
    _array_index: u32, // use BACNET_ARRAY_ALL for all
    objects: &'a [ReadPropertyMultipleObject<'a>],
    buf: &'a [u8],
}

#[derive(Debug, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct PropertyIdList<'a> {
    pub property_ids: &'a [PropertyId],
    buf: &'a [u8],
}

impl<'a> IntoIterator for &'_ PropertyIdList<'a> {
    type Item = Result<PropertyId, Error>;

    type IntoIter = PropertyIdIter<'a>;

    fn into_iter(self) -> Self::IntoIter {
        PropertyIdIter {
            buf: self.buf,
            reader: Reader::new_with_len(self.buf.len()),
        }
    }
}

pub struct PropertyIdIter<'a> {
    reader: Reader,
    buf: &'a [u8],
}

impl<'a> Iterator for PropertyIdIter<'a> {
    type Item = Result<PropertyId, Error>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.reader.eof() {
            None
        } else {
            match decode_context_property_id(
                &mut self.reader,
                self.buf,
                0,
                "PropertyIdList next property_id",
            ) {
                Ok(property_id) => Some(Ok(property_id)),
                Err(e) => Some(Err(e)),
            }
        }
    }
}

impl<'a> PropertyIdList<'a> {
    pub fn new(property_ids: &'a [PropertyId]) -> Self {
        Self {
            property_ids,
            buf: &[],
        }
    }

    pub fn encode(&self, writer: &mut Writer) {
        encode_opening_tag(writer, 1);

        for property_id in self.property_ids {
            // property_id
            encode_context_enumerated(writer, 0, property_id);

            // array_index
            //if self.array_index != BACNET_ARRAY_ALL {
            //    encode_context_unsigned(writer, 1, self.array_index);
            //}
        }

        encode_closing_tag(writer, 1);
    }
}

#[derive(Debug, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct ReadPropertyMultipleObject<'a> {
    pub object_id: ObjectId, // e.g ObjectDevice:20088
    pub property_ids: PropertyIdList<'a>,
}

impl<'a> ReadPropertyMultipleObject<'a> {
    pub fn new(object_id: ObjectId, property_ids: &'a [PropertyId]) -> Self {
        let property_ids = PropertyIdList::new(property_ids);
        Self {
            object_id,
            property_ids,
        }
    }

    pub fn encode(&self, writer: &mut Writer) {
        // object_id
        encode_context_object_id(writer, 0, &self.object_id);

        encode_opening_tag(writer, 1);

        for property_id in self.property_ids.property_ids {
            // property_id
            encode_context_enumerated(writer, 0, property_id);

            // array_index
            //if self.array_index != BACNET_ARRAY_ALL {
            //    encode_context_unsigned(writer, 1, self.array_index);
            //}
        }

        encode_closing_tag(writer, 1);
    }

    pub fn decode(reader: &mut Reader, buf: &'a [u8]) -> Result<Self, Error> {
        let object_id =
            decode_context_object_id(reader, buf, 0, "ReadPropertyMultiple next object_id")?;

        let buf =
            get_tagged_body_for_tag(reader, buf, 1, "ReadPropertyMultiple next list of results")?;
        let property_ids = PropertyIdList {
            property_ids: &[],
            buf,
        };

        Ok(ReadPropertyMultipleObject {
            object_id,
            property_ids,
        })
    }
}

impl<'a> ReadPropertyMultiple<'a> {
    pub fn new(objects: &'a [ReadPropertyMultipleObject]) -> Self {
        Self {
            objects,
            _array_index: BACNET_ARRAY_ALL,
            buf: &[],
        }
    }

    pub fn new_from_buf(buf: &'a [u8]) -> Self {
        Self {
            objects: &[],
            _array_index: BACNET_ARRAY_ALL,
            buf,
        }
    }

    pub fn encode(&self, writer: &mut Writer) {
        for object in self.objects {
            object.encode(writer)
        }
    }

    pub fn decode(reader: &mut Reader, buf: &'a [u8]) -> Self {
        let buf = &buf[reader.index..reader.end];
        Self {
            buf,
            _array_index: BACNET_ARRAY_ALL,
            objects: &[],
        }
    }
}

impl<'a> IntoIterator for &'_ ReadPropertyMultiple<'a> {
    type Item = Result<ReadPropertyMultipleObject<'a>, Error>;

    type IntoIter = ReadPropertyMultipleIter<'a>;

    fn into_iter(self) -> Self::IntoIter {
        ReadPropertyMultipleIter {
            buf: self.buf,
            reader: Reader::new_with_len(self.buf.len()),
        }
    }
}

pub struct ReadPropertyMultipleIter<'a> {
    buf: &'a [u8],
    reader: Reader,
}

impl<'a> Iterator for ReadPropertyMultipleIter<'a> {
    type Item = Result<ReadPropertyMultipleObject<'a>, Error>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.reader.eof() {
            return None;
        }

        let object_with_property_ids =
            ReadPropertyMultipleObject::decode(&mut self.reader, self.buf);
        Some(object_with_property_ids)
    }
}