Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 43 additions & 33 deletions cecd/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ use crate::{ArcDevice, AsyncDevicePoller};

const LOG_ADDR_RETRIES: i32 = 20;
const WAKE_TRIES: i32 = 2;
const ACTIVE_SOURCE_DELAY: Duration = Duration::from_millis(100);
const WAKE_DELAY: Duration = Duration::from_millis(1000);
const RESUME_TRIES: i32 = 5;
const RESUME_DELAY: Duration = Duration::from_millis(1000);
const REPLY_RETRIES: i32 = 4;

type CallbackFut<'a> = Box<dyn Future<Output = Result<()>> + Send + 'a>;
Expand Down Expand Up @@ -465,6 +468,7 @@ impl DeviceTask {
async fn wake(&mut self) -> Result<()> {
self.device.lock().await.wake(false, false).await?;
self.awaiting_wake = true;
sleep(ACTIVE_SOURCE_DELAY).await;
for _ in 0..WAKE_TRIES {
let result = self.device.lock().await.set_active_source(None).await;
match result {
Expand Down Expand Up @@ -505,13 +509,45 @@ impl DeviceTask {
Ok(())
}

async fn recover_after_resume(&mut self) {
for attempt in 1..=RESUME_TRIES {
match self
.system
.lock()
.await
.configure_dev(self.device.clone(), self.connector.as_ref())
.await
{
Ok(connector) => {
self.connector = connector;
if self
.device
.lock()
.await
.poll_address(LogicalAddress::Tv)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this behave if we wake while not connected to the TV then connect later?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so I just tested this:

starting with pc on and tv on, then I remove the hdmi cable from the tv. sleep the pc, wake the pc, wait a few seconds then re-plug the hdmi back into tv. cecd recovered and did not remain stuck

22:28:06  gamescope: DP-1 disconnected
22:28:18  kernel: PM: suspend entry (deep)
22:28:32  kernel: PM: suspend exit
22:28:41  kernel: DP-1 re-registered
22:28:41  kernel: input: cecd DP-1 ...
22:28:44  kernel: cec-DP-1: message ... timed out
22:30:25  kernel: PM: suspend entry (deep)
22:30:34  kernel: PM: suspend exit

then after reconnect /dev/cec0/ appeared and cec-ctl reports

Adapter Name: DP-1
Physical Address: 3.0.0.0
Logical Address: 8

on subsequent pc suspends and wakes, it correctly sleeps and wakes the tv and sets correct input on those events.

this is on the ugreen adaptor path that this particular pr focuses on.

.await
.is_ok()
{
return;
}
}
Err(_) => {}
}
if attempt < RESUME_TRIES {
sleep(RESUME_DELAY).await;
}
}
warn!("Failed to recover device after resume ({RESUME_TRIES} attempts)");
}

async fn handle_system_message(&mut self, message: SystemMessage) -> Result<()> {
match message {
SystemMessage::Wake {
wake_tv,
from_standby,
} => {
if from_standby {
self.recover_after_resume().await;
let device = self.device.clone();
self.with_logical_address(Box::new(|_| {
Box::new(async move {
Expand Down Expand Up @@ -550,10 +586,10 @@ impl DeviceTask {
}
SystemMessage::Standby { standby_tv, force } => {
let device = self.device.lock().await;
let address = device.get_physical_address().await?;
if force || (self.active && standby_tv) {
device.standby(LogicalAddress::Tv).await?;
} else {
} else if self.active {
let address = device.get_physical_address().await?;
device
.tx_message(&Message::InactiveSource { address }, LogicalAddress::Tv)
.await?;
Expand Down Expand Up @@ -987,15 +1023,6 @@ mod test {
.await
.unwrap();
}
assert_eq!(
rx_message(&test.dev).await.unwrap(),
(
Message::InactiveSource {
address: PhysicalAddress::from(0x1000)
},
LogicalAddress::Tv
)
);
assert!(rx_message(&test.dev).await.is_none());
}

Expand Down Expand Up @@ -1798,7 +1825,7 @@ mod test {
}

#[tokio::test]
async fn test_wake_from_standby_deferred() {
async fn test_wake_from_standby_reconfigures() {
let test = setup_basic_test().await.unwrap();

let interface: InterfaceRef<CecDevice> = test
Expand Down Expand Up @@ -1832,27 +1859,6 @@ mod test {
.unwrap();
}

assert_eq!(rx_message(&test.dev).await, None);

{
let dev = interface.get_mut().await;
dev.device
.lock()
.await
.set_logical_addresses(&[LogicalAddressType::Playback])
.await
.unwrap();
assert_eq!(
dev.device
.lock()
.await
.get_logical_addresses()
.await
.unwrap(),
&[LogicalAddress::PlaybackDevice1]
);
}

assert_eq!(
rx_message(&test.dev).await.unwrap(),
(
Expand All @@ -1862,6 +1868,10 @@ mod test {
LogicalAddress::Broadcast
)
);
assert_eq!(
test.dev.lock().await.get_logical_addresses().await.unwrap(),
&[LogicalAddress::PlaybackDevice1]
);
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion cecd/src/testing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ impl AsyncDevice {
}

pub async fn poll_address(&self, _destination: LogicalAddress) -> Result<()> {
todo!();
Ok(())
}

pub async fn handle_status(&self, status: PollStatus) -> Result<Vec<PollResult>> {
Expand Down