settings/agent/
camera_watcher.rs

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
// Copyright 2021 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 crate::agent::{
    AgentError, Context as AgentContext, Invocation, InvocationResult, Lifespan, Payload,
};
use crate::base::SettingType;
use crate::event::{camera_watcher, Event, Publisher};
use crate::handler::base::{Payload as HandlerPayload, Request};
use crate::input::common::connect_to_camera;
use crate::message::base::Audience;
use crate::service_context::ServiceContext;
use crate::{service, trace, trace_guard};
use fuchsia_async as fasync;
use std::collections::HashSet;
use std::rc::Rc;

/// Setting types that the camera watcher agent will send updates to, if they're
/// available on the device.
fn get_event_setting_types() -> HashSet<SettingType> {
    vec![SettingType::Input].into_iter().collect()
}

// TODO(https://fxbug.dev/42149412): Extract common template from agents.
pub(crate) struct CameraWatcherAgent {
    publisher: Publisher,
    messenger: service::message::Messenger,

    /// Settings to send camera watcher events to.
    recipient_settings: HashSet<SettingType>,
}

impl CameraWatcherAgent {
    pub(crate) async fn create(context: AgentContext) {
        let mut agent = CameraWatcherAgent {
            publisher: context.get_publisher(),
            messenger: context
                .create_messenger()
                .await
                .expect("messenger should be created for CameraWatchAgent"),
            recipient_settings: context
                .available_components
                .intersection(&get_event_setting_types())
                .cloned()
                .collect::<HashSet<SettingType>>(),
        };

        let mut receptor = context.receptor;
        fasync::Task::local(async move {
            let id = fuchsia_trace::Id::new();
            let guard = trace_guard!(id, c"camera watcher agent");
            while let Ok((payload, client)) = receptor.next_of::<Payload>().await {
                trace!(id, c"payload");
                if let Payload::Invocation(invocation) = payload {
                    let _ = client.reply(Payload::Complete(agent.handle(invocation).await).into());
                }
            }
            drop(guard);

            tracing::info!("Camera watcher agent done processing requests");
        })
        .detach()
    }

    async fn handle(&mut self, invocation: Invocation) -> InvocationResult {
        match invocation.lifespan {
            Lifespan::Initialization => Err(AgentError::UnhandledLifespan),
            Lifespan::Service => self.handle_service_lifespan(invocation.service_context).await,
        }
    }

    async fn handle_service_lifespan(
        &mut self,
        service_context: Rc<ServiceContext>,
    ) -> InvocationResult {
        match connect_to_camera(service_context).await {
            Ok(camera_device_client) => {
                let mut event_handler = EventHandler {
                    publisher: self.publisher.clone(),
                    messenger: self.messenger.clone(),
                    recipient_settings: self.recipient_settings.clone(),
                    sw_muted: false,
                };
                fasync::Task::local(async move {
                    let id = fuchsia_trace::Id::new();
                    // Here we don't care about hw_muted state because the input service would pick
                    // up mute changes directly from the switch. We care about sw changes because
                    // other clients of the camera3 service could change the sw mute state but not
                    // notify the settings service.
                    trace!(id, c"camera_watcher_agent_handler");
                    while let Ok((sw_muted, _hw_muted)) =
                        camera_device_client.watch_mute_state().await
                    {
                        trace!(id, c"event");
                        event_handler.handle_event(sw_muted);
                    }
                })
                .detach();

                Ok(())
            }
            Err(e) => {
                tracing::error!("Unable to watch camera device: {:?}", e);
                Err(AgentError::UnexpectedError)
            }
        }
    }
}

struct EventHandler {
    publisher: Publisher,
    messenger: service::message::Messenger,
    recipient_settings: HashSet<SettingType>,
    sw_muted: bool,
}

impl EventHandler {
    fn handle_event(&mut self, sw_muted: bool) {
        if self.sw_muted != sw_muted {
            self.sw_muted = sw_muted;
            self.send_event(sw_muted);
        }
    }

    fn send_event(&self, muted: bool) {
        self.publisher.send_event(Event::CameraUpdate(camera_watcher::Event::OnSWMuteState(muted)));
        let setting_request: Request = Request::OnCameraSWState(muted);

        // Send the event to all the interested setting types that are also available.
        for setting_type in self.recipient_settings.iter() {
            // Ignore the receptor result.
            let _ = self.messenger.message(
                HandlerPayload::Request(setting_request.clone()).into(),
                Audience::Address(service::Address::Handler(*setting_type)),
            );
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::event;
    use crate::message::base::{MessageEvent, MessengerType};
    use crate::message::receptor::Receptor;
    use crate::tests::fakes::service_registry::ServiceRegistry;
    use crate::tests::helpers::{
        create_messenger_and_publisher, create_messenger_and_publisher_from_hub,
        create_receptor_for_setting_type,
    };
    use assert_matches::assert_matches;
    use futures::StreamExt;

    // Tests that the initialization lifespan is not handled.
    #[fuchsia::test(allow_stalls = false)]
    async fn initialization_lifespan_is_unhandled() {
        // Setup messengers needed to construct the agent.
        let (messenger, publisher) = create_messenger_and_publisher().await;

        // Construct the agent.
        let mut agent =
            CameraWatcherAgent { publisher, messenger, recipient_settings: HashSet::new() };

        // Try to initiatate the initialization lifespan.
        let result = agent
            .handle(Invocation {
                lifespan: Lifespan::Initialization,
                service_context: Rc::new(ServiceContext::new(None, None)),
            })
            .await;

        assert!(matches!(result, Err(AgentError::UnhandledLifespan)));
    }

    // Tests that the agent cannot start without a camera3 service.
    #[fuchsia::test(allow_stalls = false)]
    async fn when_camera3_inaccessible_returns_err() {
        // Setup messengers needed to construct the agent.
        let (messenger, publisher) = create_messenger_and_publisher().await;

        // Construct the agent.
        let mut agent =
            CameraWatcherAgent { publisher, messenger, recipient_settings: HashSet::new() };

        let service_context = Rc::new(ServiceContext::new(
            // Create a service registry without a camera3 service interface.
            Some(ServiceRegistry::serve(ServiceRegistry::create())),
            None,
        ));

        // Try to initiate the Service lifespan without providing the camera3 fidl interface.
        let result =
            agent.handle(Invocation { lifespan: Lifespan::Service, service_context }).await;
        assert!(matches!(result, Err(AgentError::UnexpectedError)));
    }

    // Tests that events can be sent to the intended recipients.
    #[fuchsia::test(allow_stalls = false)]
    async fn event_handler_proxies_event() {
        let service_message_hub = service::MessageHub::create_hub();
        let (messenger, publisher) =
            create_messenger_and_publisher_from_hub(&service_message_hub).await;

        // Get the messenger's signature and the receptor for agents. We need
        // a different messenger below because a broadcast would not send a message
        // to itself. The signature is used to delete the original messenger for this
        // receptor.
        let event_receptor = service::build_event_listener(&service_message_hub).await;

        // Get the messenger's signature and the receptor for agents. We need
        // a different messenger below because a broadcast would not send a message
        // to itself. The signature is used to delete the original messenger for this
        // receptor.
        let handler_receptor: Receptor =
            create_receptor_for_setting_type(&service_message_hub, SettingType::Unknown).await;

        let mut event_handler = EventHandler {
            publisher,
            messenger,
            recipient_settings: vec![SettingType::Unknown].into_iter().collect(),
            sw_muted: false,
        };

        // Send the events.
        event_handler.handle_event(true);

        // Delete the messengers for the receptors we're selecting below. This
        // will allow the `select!` to eventually hit the `complete` case.
        service_message_hub.delete(handler_receptor.get_signature());
        service_message_hub.delete(event_receptor.get_signature());

        let mut agent_received_sw_mute = false;
        let mut handler_received_event = false;

        let fused_event = event_receptor.fuse();
        let fused_setting_handler = handler_receptor.fuse();
        futures::pin_mut!(fused_event, fused_setting_handler);

        // Loop over the select so we can handle the messages as they come in. When all messages
        // have been handled, due to the messengers being deleted above, the complete branch should
        // be hit to break out of the loop.
        loop {
            futures::select! {
                message = fused_event.select_next_some() => {
                    if let MessageEvent::Message(service::Payload::Event(event::Payload::Event(
                        event::Event::CameraUpdate(event)
                    )), _) = message
                    {
                        match event {
                            event::camera_watcher::Event::OnSWMuteState(muted) => {
                                assert!(muted);
                                agent_received_sw_mute = true;
                            }
                        }
                    }
                },
                message = fused_setting_handler.select_next_some() => {
                    if let MessageEvent::Message(
                        service::Payload::Setting(HandlerPayload::Request(
                            Request::OnCameraSWState(_muted))),
                        _,
                    ) = message
                    {
                        handler_received_event = true;
                    }
                }
                complete => break,
            }
        }

        assert!(agent_received_sw_mute);
        assert!(handler_received_event);
    }

    // Tests that events are not sent to unavailable settings.
    #[fuchsia::test(allow_stalls = false)]
    async fn event_handler_sends_no_events_if_no_settings_available() {
        let service_message_hub = service::MessageHub::create_hub();
        let (messenger, publisher) =
            create_messenger_and_publisher_from_hub(&service_message_hub).await;
        let handler_address = service::Address::Handler(SettingType::Unknown);
        let verification_request = Request::Get;

        // Get the messenger's signature and the receptor for agents. We need
        // a different messenger below because a broadcast would not send a message
        // to itself. The signature is used to delete the original messenger for this
        // receptor.
        let mut handler_receptor: Receptor = service_message_hub
            .create(MessengerType::Addressable(handler_address))
            .await
            .expect("Unable to create handler receptor")
            .1;

        // Declare all settings as unavailable so that no events are sent.
        let mut event_handler = EventHandler {
            publisher,
            messenger,
            recipient_settings: HashSet::new(),
            sw_muted: false,
        };

        // Send the events
        event_handler.handle_event(true);

        // Send an arbitrary request that should be the next payload received.
        let _ = service_message_hub
            .create(MessengerType::Unbound)
            .await
            .expect("Unable to create messenger")
            .0
            .message(
                HandlerPayload::Request(verification_request.clone()).into(),
                Audience::Address(handler_address),
            );

        // Delete the messengers for the receptors we're selecting below. This will allow the while
        // loop below to eventually finish.
        service_message_hub.delete(handler_receptor.get_signature());

        assert_matches!(
            handler_receptor.next_of::<HandlerPayload>().await,
            Ok((HandlerPayload::Request(request), _))
                if request == verification_request
        )
    }
}