1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Licensed under the Apache License, Version 2.0 or the MIT License.
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Copyright Tock Contributors 2022.

//! Provides userspace with access to air quality sensors.
//!
//! Usage
//! -----
//!
//! You need a device that provides the `hil::sensors::AirQualityDriver` trait.
//!
//! ```rust,ignore
//! # use kernel::static_init;
//!
//! let grant_cap = create_capability!(capabilities::MemoryAllocationCapability);
//! let grant_temperature = board_kernel.create_grant(&grant_cap);
//!
//! let temp = static_init!(
//!        capsules::temperature::AirQualitySensor<'static>,
//!        capsules::temperature::AirQualitySensor::new(si7021,
//!                                                 board_kernel.create_grant(&grant_cap)));
//!
//! kernel::hil::sensors::AirQualityDriver::set_client(si7021, temp);
//! ```

use core::cell::Cell;
use kernel::grant::{AllowRoCount, AllowRwCount, Grant, UpcallCount};
use kernel::hil;
use kernel::syscall::{CommandReturn, SyscallDriver};
use kernel::{ErrorCode, ProcessId};

/// Syscall driver number.
use capsules_core::driver;
pub const DRIVER_NUM: usize = driver::NUM::AirQuality as usize;

#[derive(Clone, Copy, PartialEq, Default)]
enum Operation {
    #[default]
    None,
    CO2,
    TVOC,
}

#[derive(Default)]
pub struct App {
    operation: Operation,
}

pub struct AirQualitySensor<'a> {
    driver: &'a dyn hil::sensors::AirQualityDriver<'a>,
    apps: Grant<App, UpcallCount<1>, AllowRoCount<0>, AllowRwCount<0>>,
    busy: Cell<bool>,
}

impl<'a> AirQualitySensor<'a> {
    pub fn new(
        driver: &'a dyn hil::sensors::AirQualityDriver<'a>,
        grant: Grant<App, UpcallCount<1>, AllowRoCount<0>, AllowRwCount<0>>,
    ) -> AirQualitySensor<'a> {
        AirQualitySensor {
            driver,
            apps: grant,
            busy: Cell::new(false),
        }
    }

    fn enqueue_command(&self, processid: ProcessId, op: Operation) -> CommandReturn {
        self.apps
            .enter(processid, |app, _| {
                if !self.busy.get() {
                    self.busy.set(true);
                    app.operation = op;

                    let rcode = match op {
                        Operation::None => Err(ErrorCode::FAIL),
                        Operation::CO2 => self.driver.read_co2(),
                        Operation::TVOC => self.driver.read_tvoc(),
                    };
                    let eres = ErrorCode::try_from(rcode);

                    match eres {
                        Ok(ecode) => CommandReturn::failure(ecode),
                        _ => CommandReturn::success(),
                    }
                } else {
                    CommandReturn::failure(ErrorCode::BUSY)
                }
            })
            .unwrap_or_else(|err| CommandReturn::failure(err.into()))
    }
}

impl hil::sensors::AirQualityClient for AirQualitySensor<'_> {
    fn environment_specified(&self, _result: Result<(), ErrorCode>) {
        unimplemented!();
    }

    fn co2_data_available(&self, value: Result<u32, ErrorCode>) {
        for cntr in self.apps.iter() {
            cntr.enter(|app, upcalls| {
                if app.operation == Operation::CO2 {
                    value
                        .map(|co2| {
                            self.busy.set(false);
                            app.operation = Operation::None;
                            upcalls.schedule_upcall(0, (co2 as usize, 0, 0)).ok();
                        })
                        .ok();
                }
            });
        }
    }

    fn tvoc_data_available(&self, value: Result<u32, ErrorCode>) {
        for cntr in self.apps.iter() {
            cntr.enter(|app, upcalls| {
                if app.operation == Operation::TVOC {
                    value
                        .map(|tvoc| {
                            self.busy.set(false);
                            app.operation = Operation::None;
                            upcalls.schedule_upcall(0, (tvoc as usize, 0, 0)).ok();
                        })
                        .ok();
                }
            });
        }
    }
}

impl SyscallDriver for AirQualitySensor<'_> {
    fn command(
        &self,
        command_num: usize,
        _: usize,
        _: usize,
        processid: ProcessId,
    ) -> CommandReturn {
        match command_num {
            // check whether the driver exists!!
            0 => CommandReturn::success(),

            // specify temperature and humidity (TODO)
            1 => CommandReturn::failure(ErrorCode::NOSUPPORT),

            // read CO2
            2 => self.enqueue_command(processid, Operation::CO2),

            // read TVOC
            3 => self.enqueue_command(processid, Operation::TVOC),

            _ => CommandReturn::failure(ErrorCode::NOSUPPORT),
        }
    }

    fn allocate_grant(&self, processid: ProcessId) -> Result<(), kernel::process::Error> {
        self.apps.enter(processid, |_, _| {})
    }
}