1use crate::dpt::DptEncode;
47use crate::error::{KnxError, Result};
48
49#[derive(Debug, Clone, Copy, PartialEq, Eq)]
51pub enum Dpt3 {
52 Dimming,
54 Blind,
56}
57
58#[derive(Debug, Clone, Copy, PartialEq, Eq)]
62#[repr(u8)]
63pub enum StepCode {
64 Break = 0,
66 Intervals1 = 1,
68 Intervals2 = 2,
70 Intervals4 = 3,
72 Intervals8 = 4,
74 Intervals16 = 5,
76 Intervals32 = 6,
78 Intervals64 = 7,
80}
81
82#[derive(Debug, Clone, Copy, PartialEq, Eq)]
84pub struct ControlCommand {
85 pub control: bool,
87 pub stepcode: StepCode,
89}
90
91impl Dpt3 {
92 pub fn decode(&self, data: &[u8]) -> Result<ControlCommand> {
117 if data.is_empty() {
118 return Err(KnxError::invalid_dpt_data());
119 }
120
121 let byte = data[0];
122 let control = (byte & 0x08) != 0;
123 let stepcode_value = byte & 0x07;
124
125 let stepcode = StepCode::from_u8(stepcode_value)?;
126
127 Ok(ControlCommand { control, stepcode })
128 }
129
130 pub const fn identifier(&self) -> &'static str {
132 match self {
133 Dpt3::Dimming => "3.007",
134 Dpt3::Blind => "3.008",
135 }
136 }
137
138 pub const fn control_labels(&self) -> (&'static str, &'static str) {
142 match self {
143 Dpt3::Dimming => ("decrease", "increase"),
144 Dpt3::Blind => ("up", "down"),
145 }
146 }
147}
148
149impl StepCode {
150 pub fn from_u8(value: u8) -> Result<Self> {
156 match value {
157 0 => Ok(StepCode::Break),
158 1 => Ok(StepCode::Intervals1),
159 2 => Ok(StepCode::Intervals2),
160 3 => Ok(StepCode::Intervals4),
161 4 => Ok(StepCode::Intervals8),
162 5 => Ok(StepCode::Intervals16),
163 6 => Ok(StepCode::Intervals32),
164 7 => Ok(StepCode::Intervals64),
165 _ => Err(KnxError::invalid_dpt_data()),
166 }
167 }
168
169 pub const fn intervals(&self) -> u8 {
173 match self {
174 StepCode::Break => 0,
175 StepCode::Intervals1 => 1,
176 StepCode::Intervals2 => 2,
177 StepCode::Intervals4 => 4,
178 StepCode::Intervals8 => 8,
179 StepCode::Intervals16 => 16,
180 StepCode::Intervals32 => 32,
181 StepCode::Intervals64 => 64,
182 }
183 }
184}
185
186impl DptEncode<(bool, StepCode)> for Dpt3 {
187 fn encode(&self, value: (bool, StepCode), buf: &mut [u8]) -> Result<usize> {
188 if buf.is_empty() {
189 return Err(KnxError::buffer_too_small());
190 }
191
192 let control_bit = if value.0 { 0x08 } else { 0x00 };
193 let step_bits = value.1 as u8 & 0x07;
194 buf[0] = control_bit | step_bits;
195 Ok(1)
196 }
197}
198
199#[cfg(test)]
200mod tests {
201 use super::*;
202
203 #[test]
208 fn test_encode_break() {
209 let mut buf = [0u8; 1];
210
211 let len = Dpt3::Dimming
213 .encode((false, StepCode::Break), &mut buf)
214 .unwrap();
215 assert_eq!(len, 1);
216 assert_eq!(buf[0], 0x00);
217
218 let len = Dpt3::Dimming
220 .encode((true, StepCode::Break), &mut buf)
221 .unwrap();
222 assert_eq!(len, 1);
223 assert_eq!(buf[0], 0x08);
224 }
225
226 #[test]
227 fn test_encode_dimming_decrease() {
228 let mut buf = [0u8; 1];
229
230 let len = Dpt3::Dimming
233 .encode((false, StepCode::Intervals1), &mut buf)
234 .unwrap();
235 assert_eq!(len, 1);
236 assert_eq!(buf[0], 0x01);
237
238 let len = Dpt3::Dimming
241 .encode((false, StepCode::Intervals4), &mut buf)
242 .unwrap();
243 assert_eq!(len, 1);
244 assert_eq!(buf[0], 0x03);
245 }
246
247 #[test]
248 fn test_encode_dimming_increase() {
249 let mut buf = [0u8; 1];
250
251 let len = Dpt3::Dimming
254 .encode((true, StepCode::Intervals1), &mut buf)
255 .unwrap();
256 assert_eq!(len, 1);
257 assert_eq!(buf[0], 0x09);
258
259 let len = Dpt3::Dimming
262 .encode((true, StepCode::Intervals4), &mut buf)
263 .unwrap();
264 assert_eq!(len, 1);
265 assert_eq!(buf[0], 0x0B);
266 }
267
268 #[test]
269 fn test_encode_blind_up() {
270 let mut buf = [0u8; 1];
271
272 let len = Dpt3::Blind
275 .encode((false, StepCode::Intervals2), &mut buf)
276 .unwrap();
277 assert_eq!(len, 1);
278 assert_eq!(buf[0], 0x02);
279 }
280
281 #[test]
282 fn test_encode_blind_down() {
283 let mut buf = [0u8; 1];
284
285 let len = Dpt3::Blind
288 .encode((true, StepCode::Intervals8), &mut buf)
289 .unwrap();
290 assert_eq!(len, 1);
291 assert_eq!(buf[0], 0x0C);
292 }
293
294 #[test]
295 fn test_encode_all_stepcodes() {
296 let mut buf = [0u8; 1];
297 let stepcodes = [
298 (StepCode::Break, 0),
299 (StepCode::Intervals1, 1),
300 (StepCode::Intervals2, 2),
301 (StepCode::Intervals4, 3),
302 (StepCode::Intervals8, 4),
303 (StepCode::Intervals16, 5),
304 (StepCode::Intervals32, 6),
305 (StepCode::Intervals64, 7),
306 ];
307
308 for (stepcode, expected_bits) in &stepcodes {
309 let len = Dpt3::Dimming.encode((false, *stepcode), &mut buf).unwrap();
311 assert_eq!(len, 1);
312 assert_eq!(buf[0], *expected_bits);
313
314 let len = Dpt3::Dimming.encode((true, *stepcode), &mut buf).unwrap();
316 assert_eq!(len, 1);
317 assert_eq!(buf[0], 0x08 | expected_bits);
318 }
319 }
320
321 #[test]
326 fn test_decode_break() {
327 let cmd = Dpt3::Dimming.decode(&[0x00]).unwrap();
328 assert_eq!(cmd.control, false);
329 assert_eq!(cmd.stepcode, StepCode::Break);
330
331 let cmd = Dpt3::Dimming.decode(&[0x08]).unwrap();
332 assert_eq!(cmd.control, true);
333 assert_eq!(cmd.stepcode, StepCode::Break);
334 }
335
336 #[test]
337 fn test_decode_dimming_decrease() {
338 let cmd = Dpt3::Dimming.decode(&[0x01]).unwrap();
340 assert_eq!(cmd.control, false);
341 assert_eq!(cmd.stepcode, StepCode::Intervals1);
342
343 let cmd = Dpt3::Dimming.decode(&[0x03]).unwrap();
345 assert_eq!(cmd.control, false);
346 assert_eq!(cmd.stepcode, StepCode::Intervals4);
347 }
348
349 #[test]
350 fn test_decode_dimming_increase() {
351 let cmd = Dpt3::Dimming.decode(&[0x09]).unwrap();
353 assert_eq!(cmd.control, true);
354 assert_eq!(cmd.stepcode, StepCode::Intervals1);
355
356 let cmd = Dpt3::Dimming.decode(&[0x0B]).unwrap();
358 assert_eq!(cmd.control, true);
359 assert_eq!(cmd.stepcode, StepCode::Intervals4);
360 }
361
362 #[test]
363 fn test_decode_blind_up() {
364 let cmd = Dpt3::Blind.decode(&[0x02]).unwrap();
365 assert_eq!(cmd.control, false);
366 assert_eq!(cmd.stepcode, StepCode::Intervals2);
367 }
368
369 #[test]
370 fn test_decode_blind_down() {
371 let cmd = Dpt3::Blind.decode(&[0x0C]).unwrap();
372 assert_eq!(cmd.control, true);
373 assert_eq!(cmd.stepcode, StepCode::Intervals8);
374 }
375
376 #[test]
377 fn test_decode_all_combinations() {
378 for byte in 0x00..=0x0F {
380 let cmd = Dpt3::Dimming.decode(&[byte]).unwrap();
381
382 let expected_control = (byte & 0x08) != 0;
383 let expected_stepcode = StepCode::from_u8(byte & 0x07).unwrap();
384
385 assert_eq!(cmd.control, expected_control);
386 assert_eq!(cmd.stepcode, expected_stepcode);
387 }
388 }
389
390 #[test]
391 fn test_decode_empty_data() {
392 let result = Dpt3::Dimming.decode(&[]);
393 assert!(result.is_err());
394 assert!(matches!(result.unwrap_err(), KnxError::Dpt(_)));
395 }
396
397 #[test]
398 fn test_decode_ignores_upper_bits() {
399 let cmd = Dpt3::Dimming.decode(&[0xFF]).unwrap();
401 assert_eq!(cmd.control, true); assert_eq!(cmd.stepcode, StepCode::Intervals64); let cmd = Dpt3::Dimming.decode(&[0xF0]).unwrap();
405 assert_eq!(cmd.control, false); assert_eq!(cmd.stepcode, StepCode::Break); }
408
409 #[test]
414 fn test_round_trip() {
415 let mut buf = [0u8; 1];
416 let test_cases = [
417 (false, StepCode::Break),
418 (true, StepCode::Break),
419 (false, StepCode::Intervals1),
420 (true, StepCode::Intervals1),
421 (false, StepCode::Intervals4),
422 (true, StepCode::Intervals4),
423 (false, StepCode::Intervals64),
424 (true, StepCode::Intervals64),
425 ];
426
427 for (control, stepcode) in &test_cases {
428 let len = Dpt3::Dimming
430 .encode((*control, *stepcode), &mut buf)
431 .unwrap();
432 assert_eq!(len, 1);
433
434 let cmd = Dpt3::Dimming.decode(&buf[..len]).unwrap();
436
437 assert_eq!(cmd.control, *control);
439 assert_eq!(cmd.stepcode, *stepcode);
440 }
441 }
442
443 #[test]
448 fn test_stepcode_from_u8_valid() {
449 assert_eq!(StepCode::from_u8(0).unwrap(), StepCode::Break);
450 assert_eq!(StepCode::from_u8(1).unwrap(), StepCode::Intervals1);
451 assert_eq!(StepCode::from_u8(2).unwrap(), StepCode::Intervals2);
452 assert_eq!(StepCode::from_u8(3).unwrap(), StepCode::Intervals4);
453 assert_eq!(StepCode::from_u8(4).unwrap(), StepCode::Intervals8);
454 assert_eq!(StepCode::from_u8(5).unwrap(), StepCode::Intervals16);
455 assert_eq!(StepCode::from_u8(6).unwrap(), StepCode::Intervals32);
456 assert_eq!(StepCode::from_u8(7).unwrap(), StepCode::Intervals64);
457 }
458
459 #[test]
460 fn test_stepcode_from_u8_invalid() {
461 let result = StepCode::from_u8(8);
462 assert!(result.is_err());
463 assert!(matches!(result.unwrap_err(), KnxError::Dpt(_)));
464 }
465
466 #[test]
467 fn test_stepcode_intervals() {
468 assert_eq!(StepCode::Break.intervals(), 0);
469 assert_eq!(StepCode::Intervals1.intervals(), 1);
470 assert_eq!(StepCode::Intervals2.intervals(), 2);
471 assert_eq!(StepCode::Intervals4.intervals(), 4);
472 assert_eq!(StepCode::Intervals8.intervals(), 8);
473 assert_eq!(StepCode::Intervals16.intervals(), 16);
474 assert_eq!(StepCode::Intervals32.intervals(), 32);
475 assert_eq!(StepCode::Intervals64.intervals(), 64);
476 }
477
478 #[test]
483 fn test_identifier() {
484 assert_eq!(Dpt3::Dimming.identifier(), "3.007");
485 assert_eq!(Dpt3::Blind.identifier(), "3.008");
486 }
487
488 #[test]
489 fn test_control_labels() {
490 assert_eq!(Dpt3::Dimming.control_labels(), ("decrease", "increase"));
491 assert_eq!(Dpt3::Blind.control_labels(), ("up", "down"));
492 }
493
494 #[test]
499 fn test_semantic_dimming() {
500 let mut buf = [0u8; 1];
501
502 let len = Dpt3::Dimming
504 .encode((true, StepCode::Intervals1), &mut buf)
505 .unwrap();
506 assert_eq!(len, 1);
507 let cmd = Dpt3::Dimming.decode(&buf[..len]).unwrap();
508 assert_eq!(cmd.control, true); assert_eq!(cmd.stepcode.intervals(), 1);
510
511 let len = Dpt3::Dimming
513 .encode((false, StepCode::Break), &mut buf)
514 .unwrap();
515 assert_eq!(len, 1);
516 let cmd = Dpt3::Dimming.decode(&buf[..len]).unwrap();
517 assert_eq!(cmd.stepcode, StepCode::Break);
518 }
519
520 #[test]
521 fn test_semantic_blind() {
522 let mut buf = [0u8; 1];
523
524 let len = Dpt3::Blind
526 .encode((true, StepCode::Intervals8), &mut buf)
527 .unwrap();
528 assert_eq!(len, 1);
529 let cmd = Dpt3::Blind.decode(&buf[..len]).unwrap();
530 assert_eq!(cmd.control, true); assert_eq!(cmd.stepcode.intervals(), 8);
532
533 let len = Dpt3::Blind
535 .encode((false, StepCode::Break), &mut buf)
536 .unwrap();
537 assert_eq!(len, 1);
538 let cmd = Dpt3::Blind.decode(&buf[..len]).unwrap();
539 assert_eq!(cmd.stepcode, StepCode::Break);
540 }
541
542 #[test]
547 fn test_trait_encode_basic() {
548 let mut buf = [0u8; 1];
549
550 let len = Dpt3::Dimming
552 .encode((true, StepCode::Intervals4), &mut buf)
553 .unwrap();
554 assert_eq!(len, 1);
555 assert_eq!(buf[0], 0x0B);
556
557 let len = Dpt3::Dimming
559 .encode((false, StepCode::Break), &mut buf)
560 .unwrap();
561 assert_eq!(len, 1);
562 assert_eq!(buf[0], 0x00);
563 }
564
565 #[test]
566 fn test_trait_encode_buffer_too_small() {
567 let mut buf = [0u8; 0];
568 let result = Dpt3::Dimming.encode((true, StepCode::Intervals1), &mut buf);
569 assert!(result.is_err());
570 assert!(matches!(result.unwrap_err(), KnxError::Transport(_)));
571 }
572
573 #[test]
574 fn test_trait_encode_all_combinations() {
575 let mut buf = [0u8; 2];
576
577 for control in [false, true] {
578 for stepcode in [
579 StepCode::Break,
580 StepCode::Intervals1,
581 StepCode::Intervals2,
582 StepCode::Intervals4,
583 StepCode::Intervals8,
584 StepCode::Intervals16,
585 StepCode::Intervals32,
586 StepCode::Intervals64,
587 ] {
588 let len = Dpt3::Dimming.encode((control, stepcode), &mut buf).unwrap();
589 assert_eq!(len, 1);
590
591 let control_bit = if control { 0x08 } else { 0x00 };
593 let step_bits = stepcode as u8 & 0x07;
594 let expected = control_bit | step_bits;
595 assert_eq!(buf[0], expected);
596 }
597 }
598 }
599
600 #[test]
601 fn test_trait_encode_round_trip() {
602 let mut buf = [0u8; 1];
603 let test_cases = [
604 (false, StepCode::Break),
605 (true, StepCode::Break),
606 (false, StepCode::Intervals1),
607 (true, StepCode::Intervals4),
608 (false, StepCode::Intervals64),
609 (true, StepCode::Intervals64),
610 ];
611
612 for (control, stepcode) in &test_cases {
613 let len = Dpt3::Dimming
614 .encode((*control, *stepcode), &mut buf)
615 .unwrap();
616 assert_eq!(len, 1);
617
618 let cmd = Dpt3::Dimming.decode(&buf[..len]).unwrap();
619 assert_eq!(cmd.control, *control);
620 assert_eq!(cmd.stepcode, *stepcode);
621 }
622 }
623}