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 = 0x0509;
28
29pub const REVISION: u16 = 1;
32
33pub const PICS: &str = "KEYPADINPUT";
35
36pub mod feature {
41 pub const NAVIGATION_KEY_CODES: u32 = 1 << 0;
45 pub const LOCATION_KEYS: u32 = 1 << 1;
49 pub const NUMBER_KEYS: u32 = 1 << 2;
53}
54
55pub const FEATURES: &[Feature] = &[
57 Feature {
58 bit: 0,
59 code: "NV",
60 name: "NavigationKeyCodes",
61 conform: Conform::OPTIONAL,
62 },
63 Feature {
64 bit: 1,
65 code: "LK",
66 name: "LocationKeys",
67 conform: Conform::OPTIONAL,
68 },
69 Feature {
70 bit: 2,
71 code: "NK",
72 name: "NumberKeys",
73 conform: Conform::OPTIONAL,
74 },
75];
76
77pub mod command {
79 use crate::im::CommandId;
80
81 pub const SEND_KEY: CommandId = 0x00;
83 pub const SEND_KEY_RESPONSE: CommandId = 0x01;
85}
86
87#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
89#[repr(u8)]
90pub enum CecKeyCodeEnum {
91 Select = 0,
93 Up = 1,
95 Down = 2,
97 Left = 3,
99 Right = 4,
101 RightUp = 5,
103 RightDown = 6,
105 LeftUp = 7,
107 LeftDown = 8,
109 RootMenu = 9,
111 SetupMenu = 10,
113 ContentsMenu = 11,
115 FavoriteMenu = 12,
117 Exit = 13,
119 MediaTopMenu = 16,
121 MediaContextSensitiveMenu = 17,
123 NumberEntryMode = 29,
125 Number11 = 30,
127 Number12 = 31,
129 Number0OrNumber10 = 32,
131 Numbers1 = 33,
133 Numbers2 = 34,
135 Numbers3 = 35,
137 Numbers4 = 36,
139 Numbers5 = 37,
141 Numbers6 = 38,
143 Numbers7 = 39,
145 Numbers8 = 40,
147 Numbers9 = 41,
149 Dot = 42,
151 Enter = 43,
153 Clear = 44,
155 NextFavorite = 47,
157 ChannelUp = 48,
159 ChannelDown = 49,
161 PreviousChannel = 50,
163 SoundSelect = 51,
165 InputSelect = 52,
167 DisplayInformation = 53,
169 Help = 54,
171 PageUp = 55,
173 PageDown = 56,
175 Power = 64,
177 VolumeUp = 65,
179 VolumeDown = 66,
181 Mute = 67,
183 Play = 68,
185 Stop = 69,
187 Pause = 70,
189 Record = 71,
191 Rewind = 72,
193 FastForward = 73,
195 Eject = 74,
197 Forward = 75,
199 Backward = 76,
201 StopRecord = 77,
203 PauseRecord = 78,
205 Reserved = 79,
207 Angle = 80,
209 SubPicture = 81,
211 VideoOnDemand = 82,
213 ElectronicProgramGuide = 83,
215 TimerProgramming = 84,
217 InitialConfiguration = 85,
219 SelectBroadcastType = 86,
221 SelectSoundPresentation = 87,
223 PlayFunction = 96,
225 PausePlayFunction = 97,
227 RecordFunction = 98,
229 PauseRecordFunction = 99,
231 StopFunction = 100,
233 MuteFunction = 101,
235 RestoreVolumeFunction = 102,
237 TuneFunction = 103,
239 SelectMediaFunction = 104,
241 SelectAvInputFunction = 105,
243 SelectAudioInputFunction = 106,
245 PowerToggleFunction = 107,
247 PowerOffFunction = 108,
249 PowerOnFunction = 109,
251 F1Blue = 113,
253 F2Red = 114,
255 F3Green = 115,
257 F4Yellow = 116,
259 F5 = 117,
261 Data = 118,
263}
264
265impl CecKeyCodeEnum {
266 #[must_use]
268 pub const fn from_value(value: u8) -> Option<Self> {
269 match value {
270 0 => Some(Self::Select),
271 1 => Some(Self::Up),
272 2 => Some(Self::Down),
273 3 => Some(Self::Left),
274 4 => Some(Self::Right),
275 5 => Some(Self::RightUp),
276 6 => Some(Self::RightDown),
277 7 => Some(Self::LeftUp),
278 8 => Some(Self::LeftDown),
279 9 => Some(Self::RootMenu),
280 10 => Some(Self::SetupMenu),
281 11 => Some(Self::ContentsMenu),
282 12 => Some(Self::FavoriteMenu),
283 13 => Some(Self::Exit),
284 16 => Some(Self::MediaTopMenu),
285 17 => Some(Self::MediaContextSensitiveMenu),
286 29 => Some(Self::NumberEntryMode),
287 30 => Some(Self::Number11),
288 31 => Some(Self::Number12),
289 32 => Some(Self::Number0OrNumber10),
290 33 => Some(Self::Numbers1),
291 34 => Some(Self::Numbers2),
292 35 => Some(Self::Numbers3),
293 36 => Some(Self::Numbers4),
294 37 => Some(Self::Numbers5),
295 38 => Some(Self::Numbers6),
296 39 => Some(Self::Numbers7),
297 40 => Some(Self::Numbers8),
298 41 => Some(Self::Numbers9),
299 42 => Some(Self::Dot),
300 43 => Some(Self::Enter),
301 44 => Some(Self::Clear),
302 47 => Some(Self::NextFavorite),
303 48 => Some(Self::ChannelUp),
304 49 => Some(Self::ChannelDown),
305 50 => Some(Self::PreviousChannel),
306 51 => Some(Self::SoundSelect),
307 52 => Some(Self::InputSelect),
308 53 => Some(Self::DisplayInformation),
309 54 => Some(Self::Help),
310 55 => Some(Self::PageUp),
311 56 => Some(Self::PageDown),
312 64 => Some(Self::Power),
313 65 => Some(Self::VolumeUp),
314 66 => Some(Self::VolumeDown),
315 67 => Some(Self::Mute),
316 68 => Some(Self::Play),
317 69 => Some(Self::Stop),
318 70 => Some(Self::Pause),
319 71 => Some(Self::Record),
320 72 => Some(Self::Rewind),
321 73 => Some(Self::FastForward),
322 74 => Some(Self::Eject),
323 75 => Some(Self::Forward),
324 76 => Some(Self::Backward),
325 77 => Some(Self::StopRecord),
326 78 => Some(Self::PauseRecord),
327 79 => Some(Self::Reserved),
328 80 => Some(Self::Angle),
329 81 => Some(Self::SubPicture),
330 82 => Some(Self::VideoOnDemand),
331 83 => Some(Self::ElectronicProgramGuide),
332 84 => Some(Self::TimerProgramming),
333 85 => Some(Self::InitialConfiguration),
334 86 => Some(Self::SelectBroadcastType),
335 87 => Some(Self::SelectSoundPresentation),
336 96 => Some(Self::PlayFunction),
337 97 => Some(Self::PausePlayFunction),
338 98 => Some(Self::RecordFunction),
339 99 => Some(Self::PauseRecordFunction),
340 100 => Some(Self::StopFunction),
341 101 => Some(Self::MuteFunction),
342 102 => Some(Self::RestoreVolumeFunction),
343 103 => Some(Self::TuneFunction),
344 104 => Some(Self::SelectMediaFunction),
345 105 => Some(Self::SelectAvInputFunction),
346 106 => Some(Self::SelectAudioInputFunction),
347 107 => Some(Self::PowerToggleFunction),
348 108 => Some(Self::PowerOffFunction),
349 109 => Some(Self::PowerOnFunction),
350 113 => Some(Self::F1Blue),
351 114 => Some(Self::F2Red),
352 115 => Some(Self::F3Green),
353 116 => Some(Self::F4Yellow),
354 117 => Some(Self::F5),
355 118 => Some(Self::Data),
356 _ => None,
357 }
358 }
359
360 #[must_use]
362 pub const fn value(self) -> u8 {
363 self as u8
364 }
365}
366
367impl ToTlv for CecKeyCodeEnum {
368 fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
369 self.value().to_tlv(w, tag)
370 }
371}
372
373impl<'a> FromTlv<'a> for CecKeyCodeEnum {
374 fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
375 let value = <u8 as FromTlv<'a>>::from_tlv(reader, element)?;
376 Self::from_value(value).ok_or(crate::Error::new(crate::ErrorCode::TlvOutOfRange))
377 }
378}
379
380#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
382#[repr(u8)]
383pub enum StatusEnum {
384 Success = 0,
386 UnsupportedKey = 1,
388 InvalidKeyInCurrentState = 2,
390}
391
392impl StatusEnum {
393 #[must_use]
395 pub const fn from_value(value: u8) -> Option<Self> {
396 match value {
397 0 => Some(Self::Success),
398 1 => Some(Self::UnsupportedKey),
399 2 => Some(Self::InvalidKeyInCurrentState),
400 _ => None,
401 }
402 }
403
404 #[must_use]
406 pub const fn value(self) -> u8 {
407 self as u8
408 }
409}
410
411impl ToTlv for StatusEnum {
412 fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
413 self.value().to_tlv(w, tag)
414 }
415}
416
417impl<'a> FromTlv<'a> for StatusEnum {
418 fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
419 let value = <u8 as FromTlv<'a>>::from_tlv(reader, element)?;
420 Self::from_value(value).ok_or(crate::Error::new(crate::ErrorCode::TlvOutOfRange))
421 }
422}
423
424#[derive(Debug, Clone, Copy, PartialEq)]
426pub struct SendKeyFields {
427 pub key_code: CecKeyCodeEnum,
429}
430
431impl ToTlv for SendKeyFields {
432 fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
433 w.start_structure(tag)?;
434 self.key_code.to_tlv(w, Tag::Context(0))?;
435 w.end_container()
436 }
437}
438
439impl<'a> FromTlv<'a> for SendKeyFields {
440 fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
441 if element.value.container() != Some(ContainerKind::Structure) {
442 return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
443 }
444 let mut key_code = None;
445 loop {
446 let Some(field) = reader.next_element()? else {
447 return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
448 };
449 if field.value == Value::EndOfContainer {
450 break;
451 }
452 match field.tag.context() {
453 Some(0) => key_code = Some(<CecKeyCodeEnum>::from_tlv(reader, &field)?),
454 _ => reader.skip_value(&field)?,
458 }
459 }
460 Ok(Self {
461 key_code: key_code.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
462 })
463 }
464}
465
466#[derive(Debug, Clone, Copy, PartialEq)]
468pub struct SendKeyResponseFields {
469 pub status: StatusEnum,
471}
472
473impl ToTlv for SendKeyResponseFields {
474 fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
475 w.start_structure(tag)?;
476 self.status.to_tlv(w, Tag::Context(0))?;
477 w.end_container()
478 }
479}
480
481impl<'a> FromTlv<'a> for SendKeyResponseFields {
482 fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
483 if element.value.container() != Some(ContainerKind::Structure) {
484 return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
485 }
486 let mut status = None;
487 loop {
488 let Some(field) = reader.next_element()? else {
489 return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
490 };
491 if field.value == Value::EndOfContainer {
492 break;
493 }
494 match field.tag.context() {
495 Some(0) => status = Some(<StatusEnum>::from_tlv(reader, &field)?),
496 _ => reader.skip_value(&field)?,
500 }
501 }
502 Ok(Self {
503 status: status.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
504 })
505 }
506}
507
508pub const ATTRIBUTES: &[Attribute] = &[];
510
511pub const COMMANDS: &[Command] = &[
513 Command {
514 id: 0x00,
515 name: "SendKey",
516 to_server: true,
517 response: Some(0x01),
518 access: Access::invoke(Privilege::Operate),
519 conform: Conform::MANDATORY,
520 },
521 Command {
522 id: 0x01,
523 name: "SendKeyResponse",
524 to_server: false,
525 response: None,
526 access: Access::invoke(Privilege::Operate),
527 conform: Conform::MANDATORY,
528 },
529];
530
531pub const EVENTS: &[Event] = &[];
533
534pub const CLUSTER: Cluster = Cluster {
536 id: ID,
537 name: "Keypad Input",
538 revision: REVISION,
539 pics: "KEYPADINPUT",
540 derived_from: None,
541 features: FEATURES,
542 attributes: ATTRIBUTES,
543 commands: COMMANDS,
544 events: EVENTS,
545};