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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
// Copyright 2022 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use super::types::Rgbc;
use crate::input_device::{
    self, Handled, InputDeviceBinding, InputDeviceDescriptor, InputDeviceStatus, InputEvent,
};
use crate::metrics;
use anyhow::{format_err, Error};
use async_trait::async_trait;
use fidl_fuchsia_input_report::{InputDeviceProxy, InputReport, SensorDescriptor, SensorType};
use fuchsia_inspect::health::Reporter;
use fuchsia_zircon as zx;
use futures::channel::mpsc::{UnboundedReceiver, UnboundedSender};
use metrics_registry::*;

#[derive(Clone, Debug)]
pub struct LightSensorEvent {
    pub(crate) device_proxy: InputDeviceProxy,
    pub(crate) rgbc: Rgbc<u16>,
}

impl PartialEq for LightSensorEvent {
    fn eq(&self, other: &Self) -> bool {
        self.rgbc == other.rgbc
    }
}

impl Eq for LightSensorEvent {}

impl LightSensorEvent {
    pub fn record_inspect(&self, node: &fuchsia_inspect::Node) {
        node.record_uint("red", u64::from(self.rgbc.red));
        node.record_uint("green", u64::from(self.rgbc.green));
        node.record_uint("blue", u64::from(self.rgbc.blue));
        node.record_uint("clear", u64::from(self.rgbc.clear));
    }
}

/// A [`LightSensorBinding`] represents a connection to a light sensor input device.
///
/// TODO more details
pub(crate) struct LightSensorBinding {
    /// The channel to stream InputEvents to.
    event_sender: UnboundedSender<input_device::InputEvent>,

    /// Holds information about this device.
    device_descriptor: LightSensorDeviceDescriptor,
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct LightSensorDeviceDescriptor {
    /// The vendor id of the connected light sensor input device.
    pub(crate) vendor_id: u32,

    /// The product id of the connected light sensor input device.
    pub(crate) product_id: u32,

    /// The device id of the connected light sensor input device.
    pub(crate) device_id: u32,

    /// Layout of the color channels in the sensor report.
    pub(crate) sensor_layout: Rgbc<usize>,
}

#[async_trait]
impl InputDeviceBinding for LightSensorBinding {
    fn input_event_sender(&self) -> UnboundedSender<InputEvent> {
        self.event_sender.clone()
    }

    fn get_device_descriptor(&self) -> InputDeviceDescriptor {
        InputDeviceDescriptor::LightSensor(self.device_descriptor.clone())
    }
}

impl LightSensorBinding {
    /// Creates a new [`InputDeviceBinding`] from the `device_proxy`.
    ///
    /// The binding will start listening for input reports immediately and send new InputEvents
    /// to the device binding owner over `input_event_sender`.
    ///
    /// # Parameters
    /// - `device_proxy`: The proxy to bind the new [`InputDeviceBinding`] to.
    /// - `device_id`: The unique identifier of this device.
    /// - `input_event_sender`: The channel to send new InputEvents to.
    /// - `device_node`: The inspect node for this device binding
    /// - `metrics_logger`: The metrics logger.
    ///
    /// # Errors
    /// If there was an error binding to the proxy.
    pub(crate) async fn new(
        device_proxy: InputDeviceProxy,
        device_id: u32,
        input_event_sender: UnboundedSender<input_device::InputEvent>,
        device_node: fuchsia_inspect::Node,
        metrics_logger: metrics::MetricsLogger,
    ) -> Result<Self, Error> {
        let (device_binding, mut inspect_status) = Self::bind_device(
            &device_proxy,
            device_id,
            input_event_sender,
            device_node,
            metrics_logger.clone(),
        )
        .await?;
        inspect_status.health_node.set_ok();
        input_device::initialize_report_stream(
            device_proxy.clone(),
            device_binding.get_device_descriptor(),
            device_binding.input_event_sender(),
            inspect_status,
            metrics_logger,
            move |report,
                  previous_report,
                  device_descriptor,
                  input_event_sender,
                  inspect_status,
                  metrics_logger| {
                Self::process_reports(
                    report,
                    previous_report,
                    device_descriptor,
                    input_event_sender,
                    device_proxy.clone(),
                    inspect_status,
                    metrics_logger,
                )
            },
        );

        Ok(device_binding)
    }

    /// Binds the provided input device to a new instance of `LightSensorBinding`.
    ///
    /// # Parameters
    /// - `device`: The device to use to initialize the binding.
    /// - `device_id`: The device ID being bound.
    /// - `input_event_sender`: The channel to send new InputEvents to.
    /// - `device_node`: The inspect node for this device binding
    ///
    /// # Errors
    /// If the device descriptor could not be retrieved, or the descriptor could not be parsed
    /// correctly.
    async fn bind_device(
        device: &InputDeviceProxy,
        device_id: u32,
        input_event_sender: UnboundedSender<input_device::InputEvent>,
        device_node: fuchsia_inspect::Node,
        metrics_logger: metrics::MetricsLogger,
    ) -> Result<(Self, InputDeviceStatus), Error> {
        let mut input_device_status = InputDeviceStatus::new(device_node);
        let descriptor = match device.get_descriptor().await {
            Ok(descriptor) => descriptor,
            Err(_) => {
                input_device_status.health_node.set_unhealthy("Could not get device descriptor.");
                return Err(format_err!("Could not get descriptor for device_id: {}", device_id));
            }
        };
        let device_info = descriptor.device_info.ok_or_else(|| {
            input_device_status.health_node.set_unhealthy("Empty device_info in descriptor.");
            // Logging in addition to returning an error, as in some test
            // setups the error may never be displayed to the user.
            metrics_logger.log_error(
                InputPipelineErrorMetricDimensionEvent::LightEmptyDeviceInfo,
                std::format!("DRIVER BUG: empty device_info for device_id: {}", device_id),
            );
            format_err!("empty device info for device_id: {}", device_id)
        })?;
        match descriptor.sensor {
            Some(SensorDescriptor { input: Some(input_descriptors), .. }) => {
                let sensor_layout = input_descriptors
                    .into_iter()
                    .filter_map(|input_descriptor| {
                        input_descriptor.values.and_then(|values| {
                            let mut red_value = None;
                            let mut green_value = None;
                            let mut blue_value = None;
                            let mut clear_value = None;
                            for (i, value) in values.iter().enumerate() {
                                let old = match value.type_ {
                                    SensorType::LightRed => {
                                        std::mem::replace(&mut red_value, Some(i))
                                    }
                                    SensorType::LightGreen => {
                                        std::mem::replace(&mut green_value, Some(i))
                                    }
                                    SensorType::LightBlue => {
                                        std::mem::replace(&mut blue_value, Some(i))
                                    }
                                    SensorType::LightIlluminance => {
                                        std::mem::replace(&mut clear_value, Some(i))
                                    }
                                    type_ => {
                                        tracing::warn!(
                                            "unexpected sensor type {type_:?} found on light \
                                                sensor device"
                                        );
                                        None
                                    }
                                };
                                if old.is_some() {
                                    tracing::warn!(
                                        "existing index for light sensor {:?} replaced",
                                        value.type_
                                    );
                                }
                            }

                            red_value.and_then(|red| {
                                green_value.and_then(|green| {
                                    blue_value.and_then(|blue| {
                                        clear_value.map(|clear| Rgbc { red, green, blue, clear })
                                    })
                                })
                            })
                        })
                    })
                    .next()
                    .ok_or_else(|| {
                        input_device_status.health_node.set_unhealthy("Missing light sensor data.");
                        format_err!("missing sensor data in device")
                    })?;
                Ok((
                    LightSensorBinding {
                        event_sender: input_event_sender,
                        device_descriptor: LightSensorDeviceDescriptor {
                            vendor_id: device_info.vendor_id,
                            product_id: device_info.product_id,
                            device_id,
                            sensor_layout,
                        },
                    },
                    input_device_status,
                ))
            }
            device_descriptor => {
                input_device_status
                    .health_node
                    .set_unhealthy("Light Sensor Device Descriptor failed to parse.");
                Err(format_err!(
                    "Light Sensor Device Descriptor failed to parse: \n {:?}",
                    device_descriptor
                ))
            }
        }
    }

    /// Parses an [`InputReport`] into one or more [`InputEvent`]s.
    ///
    /// The [`InputEvent`]s are sent to the device binding owner via [`input_event_sender`].
    ///
    /// # Parameters
    /// `report`: The incoming [`InputReport`].
    /// `previous_report`: The previous [`InputReport`] seen for the same device.
    /// `device_descriptor`: The descriptor for the input device generating the input reports.
    /// `input_event_sender`: The sender for the device binding's input event stream.
    ///
    /// # Returns
    /// An [`InputReport`] which will be passed to the next call to [`process_reports`], as
    /// [`previous_report`]. If `None`, the next call's [`previous_report`] will be `None`.
    /// A [`UnboundedReceiver<InputEvent>`] which will poll asynchronously generated events to be
    /// recorded by `inspect_status` in `input_device::initialize_report_stream()`. If device
    /// binding does not generate InputEvents asynchronously, this will be `None`.
    fn process_reports(
        report: InputReport,
        previous_report: Option<InputReport>,
        device_descriptor: &input_device::InputDeviceDescriptor,
        input_event_sender: &mut UnboundedSender<input_device::InputEvent>,
        device_proxy: InputDeviceProxy,
        inspect_status: &InputDeviceStatus,
        metrics_logger: &metrics::MetricsLogger,
    ) -> (Option<InputReport>, Option<UnboundedReceiver<InputEvent>>) {
        inspect_status.count_received_report(&report);
        let light_sensor_descriptor =
            if let input_device::InputDeviceDescriptor::LightSensor(ref light_sensor_descriptor) =
                device_descriptor
            {
                light_sensor_descriptor
            } else {
                unreachable!()
            };

        // Input devices can have multiple types so ensure `report` is a KeyboardInputReport.
        let sensor = match &report.sensor {
            None => {
                inspect_status.count_filtered_report();
                return (previous_report, None);
            }
            Some(sensor) => sensor,
        };

        let values = match &sensor.values {
            None => {
                inspect_status.count_filtered_report();
                return (None, None);
            }
            Some(values) => values,
        };

        let event = input_device::InputEvent {
            device_event: input_device::InputDeviceEvent::LightSensor(LightSensorEvent {
                device_proxy,
                rgbc: Rgbc {
                    red: values[light_sensor_descriptor.sensor_layout.red] as u16,
                    green: values[light_sensor_descriptor.sensor_layout.green] as u16,
                    blue: values[light_sensor_descriptor.sensor_layout.blue] as u16,
                    clear: values[light_sensor_descriptor.sensor_layout.clear] as u16,
                },
            }),
            device_descriptor: device_descriptor.clone(),
            event_time: zx::Time::get_monotonic(),
            handled: Handled::No,
            trace_id: None,
        };

        if let Err(e) = input_event_sender.unbounded_send(event.clone()) {
            metrics_logger.log_error(
                InputPipelineErrorMetricDimensionEvent::LightFailedToSendEvent,
                std::format!("Failed to send LightSensorEvent with error: {e:?}"),
            );
        } else {
            inspect_status.count_generated_event(event);
        }

        (Some(report), None)
    }
}