1#![allow(unused_imports)]
15
16use crate::dm::conformance::{Clause, Condition, Conform, Conformance};
17use crate::dm::meta::{EventPriority, Reporting};
18use crate::dm::spec::{Attribute, Cluster, Command, Event, Feature};
19use crate::dm::{Access, AccessQualities, AttributeQualities, Privilege};
20use crate::error::Result;
21use crate::im::{AttributeId, ClusterId, CommandId, EventId};
22use crate::tlv::{
23 ContainerKind, Element, FromTlv, Nullable, Tag, TlvList, TlvReader, TlvWriter, ToTlv, Value,
24};
25
26pub const ID: ClusterId = 0x0453;
28
29pub const REVISION: u16 = 1;
32
33pub const PICS: &str = "THNETDIR";
35
36pub const FEATURES: &[Feature] = &[];
38
39pub mod attribute {
41 use crate::im::AttributeId;
42
43 pub const PREFERRED_EXTENDED_PAN_ID: AttributeId = 0x00;
45 pub const THREAD_NETWORKS: AttributeId = 0x01;
47 pub const THREAD_NETWORK_TABLE_SIZE: AttributeId = 0x02;
49}
50
51pub mod command {
53 use crate::im::CommandId;
54
55 pub const ADD_NETWORK: CommandId = 0x00;
57 pub const REMOVE_NETWORK: CommandId = 0x01;
59 pub const GET_OPERATIONAL_DATASET: CommandId = 0x02;
61 pub const OPERATIONAL_DATASET_RESPONSE: CommandId = 0x03;
63}
64
65#[derive(Debug, Clone, Copy, PartialEq)]
67pub struct ThreadNetworkStruct<'a> {
68 pub extended_pan_id: &'a [u8],
70 pub network_name: &'a str,
72 pub channel: u16,
74 pub active_timestamp: u64,
76}
77
78impl<'a> ToTlv for ThreadNetworkStruct<'a> {
79 fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
80 w.start_structure(tag)?;
81 self.extended_pan_id.to_tlv(w, Tag::Context(0))?;
82 self.network_name.to_tlv(w, Tag::Context(1))?;
83 self.channel.to_tlv(w, Tag::Context(2))?;
84 self.active_timestamp.to_tlv(w, Tag::Context(3))?;
85 w.end_container()
86 }
87}
88
89impl<'a> FromTlv<'a> for ThreadNetworkStruct<'a> {
90 fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
91 if element.value.container() != Some(ContainerKind::Structure) {
92 return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
93 }
94 let mut extended_pan_id = None;
95 let mut network_name = None;
96 let mut channel = None;
97 let mut active_timestamp = None;
98 loop {
99 let Some(field) = reader.next_element()? else {
100 return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
101 };
102 if field.value == Value::EndOfContainer {
103 break;
104 }
105 match field.tag.context() {
106 Some(0) => extended_pan_id = Some(<&'a [u8]>::from_tlv(reader, &field)?),
107 Some(1) => network_name = Some(<&'a str>::from_tlv(reader, &field)?),
108 Some(2) => channel = Some(<u16>::from_tlv(reader, &field)?),
109 Some(3) => active_timestamp = Some(<u64>::from_tlv(reader, &field)?),
110 _ => reader.skip_value(&field)?,
114 }
115 }
116 Ok(Self {
117 extended_pan_id: extended_pan_id
118 .ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
119 network_name: network_name.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
120 channel: channel.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
121 active_timestamp: active_timestamp
122 .ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
123 })
124 }
125}
126
127#[derive(Debug, Clone, Copy, PartialEq)]
129pub struct AddNetworkFields<'a> {
130 pub operational_dataset: &'a [u8],
132}
133
134impl<'a> ToTlv for AddNetworkFields<'a> {
135 fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
136 w.start_structure(tag)?;
137 self.operational_dataset.to_tlv(w, Tag::Context(0))?;
138 w.end_container()
139 }
140}
141
142impl<'a> FromTlv<'a> for AddNetworkFields<'a> {
143 fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
144 if element.value.container() != Some(ContainerKind::Structure) {
145 return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
146 }
147 let mut operational_dataset = None;
148 loop {
149 let Some(field) = reader.next_element()? else {
150 return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
151 };
152 if field.value == Value::EndOfContainer {
153 break;
154 }
155 match field.tag.context() {
156 Some(0) => operational_dataset = Some(<&'a [u8]>::from_tlv(reader, &field)?),
157 _ => reader.skip_value(&field)?,
161 }
162 }
163 Ok(Self {
164 operational_dataset: operational_dataset
165 .ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
166 })
167 }
168}
169
170#[derive(Debug, Clone, Copy, PartialEq)]
172pub struct RemoveNetworkFields<'a> {
173 pub extended_pan_id: &'a [u8],
175}
176
177impl<'a> ToTlv for RemoveNetworkFields<'a> {
178 fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
179 w.start_structure(tag)?;
180 self.extended_pan_id.to_tlv(w, Tag::Context(0))?;
181 w.end_container()
182 }
183}
184
185impl<'a> FromTlv<'a> for RemoveNetworkFields<'a> {
186 fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
187 if element.value.container() != Some(ContainerKind::Structure) {
188 return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
189 }
190 let mut extended_pan_id = None;
191 loop {
192 let Some(field) = reader.next_element()? else {
193 return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
194 };
195 if field.value == Value::EndOfContainer {
196 break;
197 }
198 match field.tag.context() {
199 Some(0) => extended_pan_id = Some(<&'a [u8]>::from_tlv(reader, &field)?),
200 _ => reader.skip_value(&field)?,
204 }
205 }
206 Ok(Self {
207 extended_pan_id: extended_pan_id
208 .ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
209 })
210 }
211}
212
213#[derive(Debug, Clone, Copy, PartialEq)]
215pub struct GetOperationalDatasetFields<'a> {
216 pub extended_pan_id: &'a [u8],
218}
219
220impl<'a> ToTlv for GetOperationalDatasetFields<'a> {
221 fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
222 w.start_structure(tag)?;
223 self.extended_pan_id.to_tlv(w, Tag::Context(0))?;
224 w.end_container()
225 }
226}
227
228impl<'a> FromTlv<'a> for GetOperationalDatasetFields<'a> {
229 fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
230 if element.value.container() != Some(ContainerKind::Structure) {
231 return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
232 }
233 let mut extended_pan_id = None;
234 loop {
235 let Some(field) = reader.next_element()? else {
236 return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
237 };
238 if field.value == Value::EndOfContainer {
239 break;
240 }
241 match field.tag.context() {
242 Some(0) => extended_pan_id = Some(<&'a [u8]>::from_tlv(reader, &field)?),
243 _ => reader.skip_value(&field)?,
247 }
248 }
249 Ok(Self {
250 extended_pan_id: extended_pan_id
251 .ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
252 })
253 }
254}
255
256#[derive(Debug, Clone, Copy, PartialEq)]
258pub struct OperationalDatasetResponseFields<'a> {
259 pub operational_dataset: &'a [u8],
261}
262
263impl<'a> ToTlv for OperationalDatasetResponseFields<'a> {
264 fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
265 w.start_structure(tag)?;
266 self.operational_dataset.to_tlv(w, Tag::Context(0))?;
267 w.end_container()
268 }
269}
270
271impl<'a> FromTlv<'a> for OperationalDatasetResponseFields<'a> {
272 fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
273 if element.value.container() != Some(ContainerKind::Structure) {
274 return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
275 }
276 let mut operational_dataset = None;
277 loop {
278 let Some(field) = reader.next_element()? else {
279 return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
280 };
281 if field.value == Value::EndOfContainer {
282 break;
283 }
284 match field.tag.context() {
285 Some(0) => operational_dataset = Some(<&'a [u8]>::from_tlv(reader, &field)?),
286 _ => reader.skip_value(&field)?,
290 }
291 }
292 Ok(Self {
293 operational_dataset: operational_dataset
294 .ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
295 })
296 }
297}
298
299pub const ATTRIBUTES: &[Attribute] = &[
301 Attribute {
302 id: 0x0000,
303 name: "PreferredExtendedPanID",
304 kind: "octstr",
305 access: Access::read_write_with(Privilege::View, Privilege::Manage),
306 qualities: AttributeQualities::NULLABLE.union(AttributeQualities::NON_VOLATILE),
307 reporting: Reporting::OnChange,
308 conform: Conform::MANDATORY,
309 },
310 Attribute {
311 id: 0x0001,
312 name: "ThreadNetworks",
313 kind: "list[ThreadNetworkStruct]",
314 access: Access::read_only(Privilege::View),
315 qualities: AttributeQualities::NON_VOLATILE,
316 reporting: Reporting::OnChange,
317 conform: Conform::MANDATORY,
318 },
319 Attribute {
320 id: 0x0002,
321 name: "ThreadNetworkTableSize",
322 kind: "uint8",
323 access: Access::read_only(Privilege::View),
324 qualities: AttributeQualities::FIXED,
325 reporting: Reporting::OnChange,
326 conform: Conform::MANDATORY,
327 },
328];
329
330pub const COMMANDS: &[Command] = &[
332 Command {
333 id: 0x00,
334 name: "AddNetwork",
335 to_server: true,
336 response: None,
337 access: Access::invoke(Privilege::Manage).with_qualities(AccessQualities::TIMED),
338 conform: Conform::MANDATORY,
339 },
340 Command {
341 id: 0x01,
342 name: "RemoveNetwork",
343 to_server: true,
344 response: None,
345 access: Access::invoke(Privilege::Manage).with_qualities(AccessQualities::TIMED),
346 conform: Conform::MANDATORY,
347 },
348 Command {
349 id: 0x02,
350 name: "GetOperationalDataset",
351 to_server: true,
352 response: Some(0x03),
353 access: Access::invoke(Privilege::Manage),
354 conform: Conform::MANDATORY,
355 },
356 Command {
357 id: 0x03,
358 name: "OperationalDatasetResponse",
359 to_server: false,
360 response: None,
361 access: Access::invoke(Privilege::Operate),
362 conform: Conform::MANDATORY,
363 },
364];
365
366pub const EVENTS: &[Event] = &[];
368
369pub const CLUSTER: Cluster = Cluster {
371 id: ID,
372 name: "Thread Network Directory",
373 revision: REVISION,
374 pics: "THNETDIR",
375 derived_from: None,
376 features: FEATURES,
377 attributes: ATTRIBUTES,
378 commands: COMMANDS,
379 events: EVENTS,
380};