brainwires-hardware 0.10.0

Hardware I/O for the Brainwires Agent Framework — audio, GPIO, Bluetooth, and network
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
/// Matter IM Read and Report interactions.
///
/// Implements TLV encode/decode for:
/// - `ReadRequest` (opcode 0x02) — ask for one or more attributes.
/// - `ReportData`  (opcode 0x05) — server reply carrying attribute values.
///
/// TLV layout (Matter spec §8.6):
///
/// ReadRequest
/// ```text
/// struct {
///   tag 0: list of AttributePath  // attribute_requests
///   tag 3: bool                   // fabric_filtered
/// }
/// ```
///
/// ReportData
/// ```text
/// struct {
///   tag 0: uint32?                // subscription_id (present if subscribed)
///   tag 1: list of {              // attribute_reports
///     tag 1: struct {             //   AttributeDataIB
///       tag 0: AttributePath
///       tag 1: <raw TLV>          //   attribute value
///     }
///   }
///   tag 4: bool                   // suppress_response
/// }
/// ```
use super::super::clusters::{
    AttributePath, tlv, tlv_bool, tlv_uint32, wrap_list_tagged, wrap_struct, wrap_struct_tagged,
};
use super::super::error::{MatterError, MatterResult};

// ── ReadRequest ───────────────────────────────────────────────────────────────

/// Request to read one or more attributes (opcode 0x02).
#[derive(Debug, Clone)]
pub struct ReadRequest {
    /// The attribute paths to read.  Use [`AttributePath::wildcard`] to read all.
    pub attribute_requests: Vec<AttributePath>,
    /// When `true`, only return attributes visible to the accessing fabric.
    pub fabric_filtered: bool,
}

impl ReadRequest {
    /// Construct a new read request for the given paths.
    pub fn new(paths: Vec<AttributePath>) -> Self {
        Self {
            attribute_requests: paths,
            fabric_filtered: false,
        }
    }

    /// TLV-encode the `ReadRequest`.
    ///
    /// Layout:
    /// ```text
    /// struct {
    ///   tag 0: list { AttributePath... }
    ///   tag 3: bool  (fabric_filtered)
    /// }
    /// ```
    pub fn encode(&self) -> Vec<u8> {
        // Encode attribute path list (tag 0)
        let mut paths_inner = Vec::new();
        for path in &self.attribute_requests {
            // Each path is an anonymous-tagged struct inside the list.
            let path_bytes = path.encode();
            paths_inner.extend_from_slice(&path_bytes);
        }
        let paths_list = wrap_list_tagged(0, &paths_inner);

        // fabric_filtered (tag 3)
        let ff = tlv_bool(3, self.fabric_filtered);

        let mut inner = paths_list;
        inner.extend_from_slice(&ff);
        wrap_struct(&inner)
    }

    /// Decode a `ReadRequest` from TLV bytes.
    pub fn decode(bytes: &[u8]) -> MatterResult<Self> {
        if bytes.is_empty() || bytes[0] != tlv::TYPE_STRUCTURE {
            return Err(MatterError::Transport(
                "ReadRequest: expected structure".into(),
            ));
        }
        let mut attribute_requests = Vec::new();
        let mut fabric_filtered = false;

        let mut i = 1;
        while i < bytes.len() {
            if bytes[i] == tlv::TYPE_END_OF_CONTAINER {
                break;
            }
            if i + 1 >= bytes.len() {
                return Err(MatterError::Transport("ReadRequest: truncated".into()));
            }
            let ctrl = bytes[i];
            let tag = bytes[i + 1];
            i += 2;
            let type_bits = ctrl & 0x1F;

            match (tag, type_bits) {
                (0, t) if t == tlv::TYPE_LIST => {
                    // Read list of AttributePaths
                    while i < bytes.len() && bytes[i] != tlv::TYPE_END_OF_CONTAINER {
                        // Each path is a structure
                        let start = i;
                        if bytes[i] != tlv::TYPE_STRUCTURE {
                            return Err(MatterError::Transport(
                                "ReadRequest: expected path struct".into(),
                            ));
                        }
                        // Scan to matching END_OF_CONTAINER
                        i += 1;
                        let mut depth = 1u32;
                        while i < bytes.len() && depth > 0 {
                            if bytes[i] == tlv::TYPE_END_OF_CONTAINER {
                                depth -= 1;
                            } else if bytes[i] == tlv::TYPE_STRUCTURE
                                || bytes[i] == tlv::TYPE_LIST
                                || bytes[i] == (tlv::TAG_CONTEXT_1 | tlv::TYPE_STRUCTURE)
                                || bytes[i] == (tlv::TAG_CONTEXT_1 | tlv::TYPE_LIST)
                            {
                                depth += 1;
                            }
                            i += 1;
                        }
                        let path = AttributePath::decode(&bytes[start..i]).ok_or_else(|| {
                            MatterError::Transport("ReadRequest: bad AttributePath".into())
                        })?;
                        attribute_requests.push(path);
                    }
                    if i < bytes.len() {
                        i += 1;
                    } // consume END_OF_CONTAINER for list
                }
                (3, t) if t == tlv::TYPE_BOOL_TRUE || t == tlv::TYPE_BOOL_FALSE => {
                    fabric_filtered = t == tlv::TYPE_BOOL_TRUE;
                }
                _ => {
                    return Err(MatterError::Transport(format!(
                        "ReadRequest: unexpected field tag={tag} ctrl={ctrl:#04x}"
                    )));
                }
            }
        }
        Ok(Self {
            attribute_requests,
            fabric_filtered,
        })
    }
}

// ── AttributeData ─────────────────────────────────────────────────────────────

/// An attribute path + its raw TLV-encoded value.
#[derive(Debug, Clone)]
pub struct AttributeData {
    /// Path identifying which attribute this data belongs to.
    pub path: AttributePath,
    /// Raw TLV-encoded attribute value (opaque bytes).
    pub data: Vec<u8>,
}

impl AttributeData {
    /// TLV-encode as an AttributeDataIB struct.
    ///
    /// Layout:
    /// ```text
    /// struct {
    ///   tag 0: AttributePath (struct)
    ///   tag 1: <raw data bytes>
    /// }
    /// ```
    pub fn encode(&self) -> Vec<u8> {
        let path_tagged =
            wrap_struct_tagged(0, &self.path.encode()[1..self.path.encode().len() - 1]);
        // tag 1: raw data — wrap data bytes as a context-tagged anonymous blob
        // We treat data as a pre-encoded TLV octet — embed verbatim with tag 1 prefix
        let data_tagged = {
            let mut v = Vec::new();
            // Wrap data in a context-tagged structure shell (tag 1)
            v.push(tlv::TAG_CONTEXT_1 | tlv::TYPE_STRUCTURE);
            v.push(1u8);
            v.extend_from_slice(&self.data);
            v.push(tlv::TYPE_END_OF_CONTAINER);
            v
        };
        let mut inner = path_tagged;
        inner.extend_from_slice(&data_tagged);
        wrap_struct(&inner)
    }

    /// Decode an `AttributeData` from TLV bytes produced by [`AttributeData::encode`].
    pub fn decode(bytes: &[u8]) -> Option<Self> {
        if bytes.is_empty() || bytes[0] != tlv::TYPE_STRUCTURE {
            return None;
        }
        let mut path: Option<AttributePath> = None;
        let mut data: Option<Vec<u8>> = None;
        let mut i = 1;

        while i < bytes.len() {
            if bytes[i] == tlv::TYPE_END_OF_CONTAINER {
                break;
            }
            if i + 1 >= bytes.len() {
                return None;
            }
            let ctrl = bytes[i];
            let tag = bytes[i + 1];
            i += 2;
            let type_bits = ctrl & 0x1F;

            match (tag, type_bits) {
                (0, t) if t == tlv::TYPE_STRUCTURE => {
                    // Reconstruct AttributePath — starts at (i-2), but we skipped past ctrl+tag.
                    // We need to read the inner struct body and wrap it.
                    let start = i;
                    let mut depth = 1u32;
                    while i < bytes.len() && depth > 0 {
                        if bytes[i] == tlv::TYPE_END_OF_CONTAINER {
                            depth -= 1;
                        } else if bytes[i] == tlv::TYPE_STRUCTURE {
                            depth += 1;
                        }
                        i += 1;
                    }
                    // Reconstruct: prepend TYPE_STRUCTURE, the body is bytes[start..i-1], then END
                    let mut path_bytes = vec![tlv::TYPE_STRUCTURE];
                    path_bytes.extend_from_slice(&bytes[start..i - 1]);
                    path_bytes.push(tlv::TYPE_END_OF_CONTAINER);
                    path = AttributePath::decode(&path_bytes);
                }
                (1, t) if t == tlv::TYPE_STRUCTURE => {
                    // Raw data is the contents of this struct
                    let start = i;
                    let mut depth = 1u32;
                    while i < bytes.len() && depth > 0 {
                        if bytes[i] == tlv::TYPE_END_OF_CONTAINER {
                            depth -= 1;
                        } else if bytes[i] == tlv::TYPE_STRUCTURE {
                            depth += 1;
                        }
                        i += 1;
                    }
                    data = Some(bytes[start..i - 1].to_vec());
                }
                _ => return None,
            }
        }
        Some(Self {
            path: path?,
            data: data.unwrap_or_default(),
        })
    }
}

// ── ReportData ────────────────────────────────────────────────────────────────

/// Attribute report from device → controller (opcode 0x05).
#[derive(Debug, Clone)]
pub struct ReportData {
    /// Present when this report is part of a subscription.
    pub subscription_id: Option<u32>,
    /// The reported attribute values.
    pub attribute_reports: Vec<AttributeData>,
    /// When `true`, the controller must not send a `StatusResponse`.
    pub suppress_response: bool,
}

impl ReportData {
    /// TLV-encode the `ReportData`.
    ///
    /// Layout:
    /// ```text
    /// struct {
    ///   tag 0: uint32?          (subscription_id)
    ///   tag 1: list { AttributeData... }
    ///   tag 4: bool             (suppress_response)
    /// }
    /// ```
    pub fn encode(&self) -> Vec<u8> {
        let mut inner = Vec::new();
        if let Some(sid) = self.subscription_id {
            inner.extend_from_slice(&tlv_uint32(0, sid));
        }
        let mut reports_inner = Vec::new();
        for attr in &self.attribute_reports {
            reports_inner.extend_from_slice(&attr.encode());
        }
        inner.extend_from_slice(&wrap_list_tagged(1, &reports_inner));
        inner.extend_from_slice(&tlv_bool(4, self.suppress_response));
        wrap_struct(&inner)
    }

    /// Decode a `ReportData` from TLV bytes.
    pub fn decode(bytes: &[u8]) -> MatterResult<Self> {
        if bytes.is_empty() || bytes[0] != tlv::TYPE_STRUCTURE {
            return Err(MatterError::Transport(
                "ReportData: expected structure".into(),
            ));
        }
        let mut subscription_id = None;
        let mut attribute_reports = Vec::new();
        let mut suppress_response = false;
        let mut i = 1;

        while i < bytes.len() {
            if bytes[i] == tlv::TYPE_END_OF_CONTAINER {
                break;
            }
            if i + 1 >= bytes.len() {
                return Err(MatterError::Transport("ReportData: truncated".into()));
            }
            let ctrl = bytes[i];
            let tag = bytes[i + 1];
            i += 2;
            let type_bits = ctrl & 0x1F;

            match (tag, type_bits) {
                (0, t) if t == tlv::TYPE_UNSIGNED_INT_4 => {
                    use super::super::clusters::read_u32_le;
                    let (v, next) = read_u32_le(bytes, i).ok_or_else(|| {
                        MatterError::Transport("ReportData: bad subscription_id".into())
                    })?;
                    subscription_id = Some(v);
                    i = next;
                }
                (1, t) if t == tlv::TYPE_LIST => {
                    // Read list of AttributeData structs
                    while i < bytes.len() && bytes[i] != tlv::TYPE_END_OF_CONTAINER {
                        if bytes[i] != tlv::TYPE_STRUCTURE {
                            return Err(MatterError::Transport(
                                "ReportData: expected AttributeData struct".into(),
                            ));
                        }
                        let start = i;
                        i += 1;
                        let mut depth = 1u32;
                        while i < bytes.len() && depth > 0 {
                            if bytes[i] == tlv::TYPE_END_OF_CONTAINER {
                                depth -= 1;
                            } else if bytes[i] == tlv::TYPE_STRUCTURE
                                || bytes[i] == (tlv::TAG_CONTEXT_1 | tlv::TYPE_STRUCTURE)
                            {
                                depth += 1;
                            }
                            i += 1;
                        }
                        let attr = AttributeData::decode(&bytes[start..i]).ok_or_else(|| {
                            MatterError::Transport("ReportData: bad AttributeData".into())
                        })?;
                        attribute_reports.push(attr);
                    }
                    if i < bytes.len() {
                        i += 1;
                    } // consume END_OF_CONTAINER
                }
                (4, t) if t == tlv::TYPE_BOOL_TRUE || t == tlv::TYPE_BOOL_FALSE => {
                    suppress_response = t == tlv::TYPE_BOOL_TRUE;
                }
                _ => {
                    return Err(MatterError::Transport(format!(
                        "ReportData: unexpected field tag={tag} ctrl={ctrl:#04x}"
                    )));
                }
            }
        }
        Ok(Self {
            subscription_id,
            attribute_reports,
            suppress_response,
        })
    }
}

// ── Tests ─────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::super::super::clusters::AttributePath;
    use super::*;

    #[test]
    fn read_request_single_path_roundtrip() {
        let path = AttributePath::specific(1, 0x0006, 0x0000);
        let req = ReadRequest::new(vec![path.clone()]);
        let encoded = req.encode();
        let decoded = ReadRequest::decode(&encoded).expect("decode failed");
        assert_eq!(decoded.attribute_requests.len(), 1);
        assert_eq!(decoded.attribute_requests[0], path);
        assert!(!decoded.fabric_filtered);
    }

    #[test]
    fn report_data_multiple_attributes_roundtrip() {
        let reports = vec![
            AttributeData {
                path: AttributePath::specific(1, 0x0006, 0x0000),
                data: vec![tlv::TYPE_BOOL_TRUE],
            },
            AttributeData {
                path: AttributePath::specific(1, 0x0008, 0x0000),
                data: vec![tlv::TAG_CONTEXT_1 | tlv::TYPE_UNSIGNED_INT_1, 0, 128],
            },
        ];
        let report = ReportData {
            subscription_id: Some(42),
            attribute_reports: reports,
            suppress_response: false,
        };
        let encoded = report.encode();
        let decoded = ReportData::decode(&encoded).expect("decode failed");
        assert_eq!(decoded.subscription_id, Some(42));
        assert_eq!(decoded.attribute_reports.len(), 2);
        assert_eq!(
            decoded.attribute_reports[0].path,
            AttributePath::specific(1, 0x0006, 0x0000)
        );
        assert_eq!(
            decoded.attribute_reports[1].path,
            AttributePath::specific(1, 0x0008, 0x0000)
        );
        assert!(!decoded.suppress_response);
    }
}