starnix_core/power/
wakeup_count.rs

1// Copyright 2023 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use crate::task::CurrentTask;
6use crate::vfs::FsNodeOps;
7use crate::vfs::pseudo::simple_file::{
8    BytesFile, BytesFileOps, parse_unsigned_file, serialize_for_file,
9};
10use starnix_uapi::error;
11use starnix_uapi::errors::Errno;
12use std::borrow::Cow;
13
14/// This file allows user space to put the system into a sleep state while taking into account the
15/// concurrent arrival of wakeup events.
16/// * Reading from it returns the current number of registered wakeup events and it blocks if some
17/// wakeup events are being processed when the file is read from.
18/// * Writing to it will only succeed if the current number of wakeup events is equal to the written
19/// value and, if successful, will make the kernel abort a subsequent transition to a sleep state
20/// if any wakeup events are reported after the write has returned.
21pub struct PowerWakeupCountFile;
22
23impl PowerWakeupCountFile {
24    pub fn new_node() -> impl FsNodeOps {
25        BytesFile::new_node(Self {})
26    }
27}
28
29impl BytesFileOps for PowerWakeupCountFile {
30    fn write(&self, current_task: &CurrentTask, data: Vec<u8>) -> Result<(), Errno> {
31        let expected_count: u64 = parse_unsigned_file(&data)?;
32        let real_count = current_task.kernel().suspend_resume_manager.suspend_stats().wakeup_count;
33        if expected_count != real_count {
34            return error!(EINVAL);
35        }
36        Ok(())
37    }
38
39    fn read(&self, current_task: &CurrentTask) -> Result<Cow<'_, [u8]>, Errno> {
40        let wakeup_count =
41            current_task.kernel().suspend_resume_manager.suspend_stats().wakeup_count;
42        Ok(serialize_for_file(wakeup_count).into())
43    }
44}