pass/
pass.rs

1/*!
2 * Simple example of Passthrough Mode.
3 *
4 * This does what the core would do if not in Passthrough Mode.
5 */
6use cec_linux::*;
7
8fn main() -> std::io::Result<()> {
9    let cec = CecDevice::open("/dev/cec0")?;
10
11    cec.set_mode(CecModeInitiator::Send, CecModeFollower::ExclusivePassthru)?;
12
13    let physical_addr = cec.get_phys()?;
14
15    loop {
16        let msg = cec.rec()?;
17        match msg.opcode() {
18            Some(Ok(CecOpcode::ActiveSource))
19            | Some(Ok(CecOpcode::RoutingInformation))
20            | Some(Ok(CecOpcode::SetStreamPath))
21                if physical_addr == msg.parameters() =>
22            {
23                // this is not done by the core
24                println!("THIS IS US {:?}", msg.opcode().unwrap().unwrap());
25                cec.transmit_data(
26                    CecLogicalAddress::Playback2,
27                    CecLogicalAddress::UnregisteredBroadcast,
28                    CecOpcode::ActiveSource,
29                    &physical_addr.to_bytes(),
30                )?;
31            }
32            Some(Ok(CecOpcode::ReportPhysicalAddr)) => {} //core is still taking care of that
33            Some(Ok(opcode)) if msg.destination() == CecLogicalAddress::UnregisteredBroadcast => {
34                //dont answer brodcasts
35                println!(
36                    "{:?}: {:?} {:x?}",
37                    msg.initiator(),
38                    opcode,
39                    msg.parameters()
40                );
41            }
42            Some(Ok(CecOpcode::GetCecVersion)) => {
43                cec.transmit_data(
44                    msg.destination(),
45                    msg.initiator(),
46                    CecOpcode::CecVersion,
47                    &[Version::V1_3A.into()],
48                )?;
49            }
50            Some(Ok(CecOpcode::GiveDeviceVendorId)) => {
51                cec.transmit_data(
52                    msg.destination(),
53                    msg.initiator(),
54                    CecOpcode::FeatureAbort,
55                    &[
56                        CecOpcode::GiveDeviceVendorId.into(),
57                        CecAbortReason::Unrecognized.into(),
58                    ],
59                )?; /*
60                    cec.transmit_data(
61                        msg.destination(),
62                        msg.initiator(),
63                        CecOpcode::DeviceVendorId,
64                    &[0,0,0])?;*/
65            }
66            Some(Ok(CecOpcode::Abort)) => {
67                cec.transmit_data(
68                    msg.destination(),
69                    msg.initiator(),
70                    CecOpcode::FeatureAbort,
71                    &[CecOpcode::Abort.into(), CecAbortReason::Other.into()],
72                )?;
73            }
74            Some(Ok(CecOpcode::GivePhysicalAddr)) => {
75                let l = cec.get_log()?;
76                let mut addr = Vec::with_capacity(3);
77                if let Some(log) = l.addresses().first() {
78                    addr.extend_from_slice(&physical_addr.to_bytes());
79                    addr.push((*log).into());
80
81                    cec.transmit_data(
82                        msg.destination(),
83                        msg.initiator(),
84                        CecOpcode::ReportPhysicalAddr,
85                        &addr,
86                    )?;
87                } //else no address yet?!?!?
88            }
89            Some(Ok(CecOpcode::GiveOsdName)) => {
90                cec.transmit_data(
91                    msg.destination(),
92                    msg.initiator(),
93                    CecOpcode::SetOsdName,
94                    b"pi4",
95                )?;
96            }
97            Some(Ok(CecOpcode::GiveDevicePowerStatus)) => {
98                cec.transmit_data(
99                    msg.destination(),
100                    msg.initiator(),
101                    CecOpcode::ReportPowerStatus,
102                    &[CecPowerStatus::On.into()],
103                )?;
104            }
105            Some(Ok(CecOpcode::GiveFeatures)) => {}
106            Some(Ok(opcode)) => {
107                println!(
108                    "{:?} -> {:?} : {:?} {:x?}",
109                    msg.initiator(),
110                    msg.destination(),
111                    opcode,
112                    msg.parameters()
113                );
114            }
115            _ => {
116                println!("{:?}", msg);
117            }
118        }
119    }
120}