settings/light/
light_controller.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
// Copyright 2020 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::base::{SettingInfo, SettingType};
use crate::config::default_settings::DefaultSetting;
use crate::handler::base::Request;
use crate::handler::setting_handler::persist::{controller as data_controller, ClientProxy};
use crate::handler::setting_handler::{
    controller, ControllerError, ControllerStateResult, SettingHandlerResult,
};
use crate::input::MediaButtons;
use crate::light::light_hardware_configuration::DisableConditions;
use crate::light::types::{LightGroup, LightInfo, LightState, LightType, LightValue};
use crate::service_context::ExternalServiceProxy;
use crate::{call_async, LightHardwareConfiguration};
use async_trait::async_trait;
use fidl_fuchsia_hardware_light::{Info, LightMarker, LightProxy};
use fidl_fuchsia_settings_storage::LightGroups;
use futures::lock::Mutex;
use settings_storage::fidl_storage::{FidlStorage, FidlStorageConvertible};
use settings_storage::storage_factory::{NoneT, StorageAccess};
use std::collections::hash_map::Entry;
use std::collections::HashMap;
use std::rc::Rc;

/// Used as the argument field in a ControllerError::InvalidArgument to signal the FIDL handler to
/// signal that a LightError::INVALID_NAME should be returned to the client.
pub(crate) const ARG_NAME: &str = "name";

/// Hardware path used to connect to light devices.
pub(crate) const DEVICE_PATH: &str = "/dev/class/light/*";

impl FidlStorageConvertible for LightInfo {
    type Storable = LightGroups;
    type Loader = NoneT;
    const KEY: &'static str = "light_info";

    #[allow(clippy::redundant_closure)]
    fn to_storable(self) -> Self::Storable {
        LightGroups {
            groups: self
                .light_groups
                .into_values()
                .map(|group| fidl_fuchsia_settings::LightGroup::from(group))
                .collect(),
        }
    }

    fn from_storable(storable: Self::Storable) -> Self {
        // Unwrap ok since validation would ensure non-None name before writing to storage.
        let light_groups = storable
            .groups
            .into_iter()
            .map(|group| (group.name.clone().unwrap(), group.into()))
            .collect();
        Self { light_groups }
    }
}

impl From<LightInfo> for SettingInfo {
    fn from(info: LightInfo) -> SettingInfo {
        SettingInfo::Light(info)
    }
}

pub struct LightController {
    /// Provides access to common resources and functionality for controllers.
    client: ClientProxy,

    /// Proxy for interacting with light hardware.
    light_proxy: ExternalServiceProxy<LightProxy>,

    /// Hardware configuration that determines what lights to return to the client.
    ///
    /// If present, overrides the lights from the underlying fuchsia.hardware.light API.
    light_hardware_config: Option<LightHardwareConfiguration>,

    /// Cache of data that includes hardware values. The data stored on disk does not persist the
    /// hardware values, so restoring does not bring the values back into memory. The data needs to
    /// be cached at this layer so we don't lose track of them.
    data_cache: Rc<Mutex<Option<LightInfo>>>,
}

impl StorageAccess for LightController {
    type Storage = FidlStorage;
    type Data = LightInfo;
    const STORAGE_KEY: &'static str = LightInfo::KEY;
}

#[async_trait(?Send)]
impl data_controller::CreateWithAsync for LightController {
    type Data = Rc<Mutex<DefaultSetting<LightHardwareConfiguration, &'static str>>>;
    async fn create_with(client: ClientProxy, data: Self::Data) -> Result<Self, ControllerError> {
        let light_hardware_config = data.lock().await.load_default_value().map_err(|_| {
            ControllerError::InitFailure("Invalid default light hardware config".into())
        })?;

        LightController::create_with_config(client, light_hardware_config).await
    }
}

#[async_trait(?Send)]
impl controller::Handle for LightController {
    async fn handle(&self, request: Request) -> Option<SettingHandlerResult> {
        match request {
            Request::Restore => {
                Some(self.restore().await.map(|light_info| Some(SettingInfo::Light(light_info))))
            }
            Request::OnButton(MediaButtons { mic_mute: Some(mic_mute), .. }) => {
                Some(self.on_mic_mute(mic_mute).await)
            }
            Request::SetLightGroupValue(name, state) => {
                // Validate state contains valid float numbers.
                for light_state in &state {
                    if !light_state.is_finite() {
                        return Some(Err(ControllerError::InvalidArgument(
                            SettingType::Light,
                            "state".into(),
                            format!("{light_state:?}").into(),
                        )));
                    }
                }
                Some(self.set(name, state).await)
            }
            Request::Get => {
                // Read all light values from underlying fuchsia.hardware.light before returning a
                // value to ensure we have the latest light state.
                // TODO(https://fxbug.dev/42134045): remove once all clients are migrated.
                Some(self.restore().await.map(|light_info| Some(SettingInfo::Light(light_info))))
            }
            _ => None,
        }
    }
}

/// Controller for processing requests surrounding the Light protocol.
impl LightController {
    /// Alternate constructor that allows specifying a configuration.
    pub(crate) async fn create_with_config(
        client: ClientProxy,
        light_hardware_config: Option<LightHardwareConfiguration>,
    ) -> Result<Self, ControllerError> {
        let light_proxy = client
            .get_service_context()
            .connect_device_path::<LightMarker>(DEVICE_PATH)
            .await
            .map_err(|e| {
                ControllerError::InitFailure(
                    format!("failed to connect to fuchsia.hardware.light with error: {e:?}").into(),
                )
            })?;

        Ok(LightController {
            client,
            light_proxy,
            light_hardware_config,
            data_cache: Rc::new(Mutex::new(None)),
        })
    }

    async fn set(&self, name: String, state: Vec<LightState>) -> SettingHandlerResult {
        let id = fuchsia_trace::Id::new();
        let mut light_info = self.data_cache.lock().await;
        // TODO(https://fxbug.dev/42058901) Deduplicate the code here and in mic_mute if possible.
        if light_info.is_none() {
            drop(light_info);
            let _ = self.restore().await?;
            light_info = self.data_cache.lock().await;
        }

        let current = light_info
            .as_mut()
            .ok_or_else(|| ControllerError::UnexpectedError("missing data cache".into()))?;
        let mut entry = match current.light_groups.entry(name.clone()) {
            Entry::Vacant(_) => {
                // Reject sets if the light name is not known.
                return Err(ControllerError::InvalidArgument(
                    SettingType::Light,
                    ARG_NAME.into(),
                    name.into(),
                ));
            }
            Entry::Occupied(entry) => entry,
        };

        let group = entry.get_mut();

        if state.len() != group.lights.len() {
            // If the number of light states provided doesn't match the number of lights,
            // return an error.
            return Err(ControllerError::InvalidArgument(
                SettingType::Light,
                "state".into(),
                format!("{state:?}").into(),
            ));
        }

        if !state.iter().filter_map(|state| state.value.clone()).all(|value| {
            match group.light_type {
                LightType::Brightness => matches!(value, LightValue::Brightness(_)),
                LightType::Rgb => matches!(value, LightValue::Rgb(_)),
                LightType::Simple => matches!(value, LightValue::Simple(_)),
            }
        }) {
            // If not all the light values match the light type of this light group, return an
            // error.
            return Err(ControllerError::InvalidArgument(
                SettingType::Light,
                "state".into(),
                format!("{state:?}").into(),
            ));
        }

        // After the main validations, write the state to the hardware.
        self.write_light_group_to_hardware(group, &state).await?;

        let _ = self.client.write_setting(current.clone().into(), id).await?;
        Ok(Some(current.clone().into()))
    }

    /// Writes the given list of light states for a light group to the actual hardware.
    ///
    /// [LightState::None] elements in the vector are ignored and not written to the hardware.
    async fn write_light_group_to_hardware(
        &self,
        group: &mut LightGroup,
        state: &[LightState],
    ) -> ControllerStateResult {
        for (i, (light, hardware_index)) in
            state.iter().zip(group.hardware_index.iter()).enumerate()
        {
            let (set_result, method_name) = match light.clone().value {
                // No value provided for this index, just skip it and don't update the
                // stored value.
                None => continue,
                Some(LightValue::Brightness(brightness)) => (
                    call_async!(self.light_proxy =>
                        set_brightness_value(*hardware_index, brightness))
                    .await,
                    "set_brightness_value",
                ),
                Some(LightValue::Rgb(rgb)) => {
                    let value = rgb.clone().try_into().map_err(|_| {
                        ControllerError::InvalidArgument(
                            SettingType::Light,
                            "value".into(),
                            format!("{rgb:?}").into(),
                        )
                    })?;
                    (
                        call_async!(self.light_proxy =>
                            set_rgb_value(*hardware_index, & value))
                        .await,
                        "set_rgb_value",
                    )
                }
                Some(LightValue::Simple(on)) => (
                    call_async!(self.light_proxy => set_simple_value(*hardware_index, on)).await,
                    "set_simple_value",
                ),
            };
            set_result
                .map_err(|e| format!("{e:?}"))
                .and_then(|res| res.map_err(|e| format!("{e:?}")))
                .map_err(|e| {
                    ControllerError::ExternalFailure(
                        SettingType::Light,
                        "fuchsia.hardware.light".into(),
                        format!("{method_name} for light {hardware_index}").into(),
                        e.into(),
                    )
                })?;

            // Set was successful, save this light value.
            group.lights[i] = light.clone();
        }
        Ok(())
    }

    async fn on_mic_mute(&self, mic_mute: bool) -> SettingHandlerResult {
        let id = fuchsia_trace::Id::new();
        let mut light_info = self.data_cache.lock().await;
        if light_info.is_none() {
            drop(light_info);
            let _ = self.restore().await?;
            light_info = self.data_cache.lock().await;
        }

        let current = light_info
            .as_mut()
            .ok_or_else(|| ControllerError::UnexpectedError("missing data cache".into()))?;
        for light in current
            .light_groups
            .values_mut()
            .filter(|l| l.disable_conditions.contains(&DisableConditions::MicSwitch))
        {
            // This condition means that the LED is hard-wired to the mute switch and will only be
            // on when the mic is disabled.
            light.enabled = mic_mute;
        }

        let _ = self.client.write_setting(current.clone().into(), id).await?;
        Ok(Some(current.clone().into()))
    }

    async fn restore(&self) -> Result<LightInfo, ControllerError> {
        let light_info = if let Some(config) = self.light_hardware_config.clone() {
            // Configuration is specified, restore from the configuration.
            self.restore_from_configuration(config).await
        } else {
            // Read light info from hardware.
            self.restore_from_hardware().await
        }?;
        let mut guard = self.data_cache.lock().await;
        *guard = Some(light_info.clone());
        Ok(light_info)
    }

    /// Restores the light information from a pre-defined hardware configuration. Individual light
    /// states are read from the underlying fuchsia.hardware.Light API, but the structure of the
    /// light groups is determined by the given `config`.
    async fn restore_from_configuration(
        &self,
        config: LightHardwareConfiguration,
    ) -> Result<LightInfo, ControllerError> {
        let id = fuchsia_trace::Id::new();
        let current = self.client.read_setting::<LightInfo>(id).await;
        let mut light_groups: HashMap<String, LightGroup> = HashMap::new();
        for group_config in config.light_groups {
            let mut light_state: Vec<LightState> = Vec::new();

            // TODO(https://fxbug.dev/42134045): once all clients go through setui, restore state from hardware
            // only if not found in persistent storage.
            for light_index in group_config.hardware_index.iter() {
                light_state.push(
                    self.light_state_from_hardware_index(*light_index, group_config.light_type)
                        .await?,
                );
            }

            // Restore previous state.
            let enabled = current
                .light_groups
                .get(&group_config.name)
                .map(|found_group| found_group.enabled)
                .unwrap_or(true);

            let _ = light_groups.insert(
                group_config.name.clone(),
                LightGroup {
                    name: group_config.name,
                    enabled,
                    light_type: group_config.light_type,
                    lights: light_state,
                    hardware_index: group_config.hardware_index,
                    disable_conditions: group_config.disable_conditions,
                },
            );
        }

        Ok(LightInfo { light_groups })
    }

    /// Restores the light information when no hardware configuration is specified by reading from
    /// the underlying fuchsia.hardware.Light API and turning each light into a [`LightGroup`].
    ///
    /// [`LightGroup`]: ../../light/types/struct.LightGroup.html
    async fn restore_from_hardware(&self) -> Result<LightInfo, ControllerError> {
        let num_lights = call_async!(self.light_proxy => get_num_lights()).await.map_err(|e| {
            ControllerError::ExternalFailure(
                SettingType::Light,
                "fuchsia.hardware.light".into(),
                "get_num_lights".into(),
                format!("{e:?}").into(),
            )
        })?;

        let id = fuchsia_trace::Id::new();
        let mut current = self.client.read_setting::<LightInfo>(id).await;
        for i in 0..num_lights {
            let info = call_async!(self.light_proxy => get_info(i))
                .await
                .map_err(|e| format!("{e:?}"))
                .and_then(|res| res.map_err(|e| format!("{e:?}")))
                .map_err(|e| {
                    ControllerError::ExternalFailure(
                        SettingType::Light,
                        "fuchsia.hardware.light".into(),
                        format!("get_info for light {i}").into(),
                        e.into(),
                    )
                })?;
            let (name, group) = self.light_info_to_group(i, info).await?;
            let _ = current.light_groups.insert(name, group);
        }

        Ok(current)
    }

    /// Converts an Info object from the fuchsia.hardware.Light API into a LightGroup, the internal
    /// representation used for our service.
    async fn light_info_to_group(
        &self,
        index: u32,
        info: Info,
    ) -> Result<(String, LightGroup), ControllerError> {
        let light_type: LightType = info.capability.into();

        let light_state = self.light_state_from_hardware_index(index, light_type).await?;

        Ok((
            info.name.clone(),
            LightGroup {
                name: info.name,
                // When there's no config, lights are assumed to be always enabled.
                enabled: true,
                light_type,
                lights: vec![light_state],
                hardware_index: vec![index],
                disable_conditions: vec![],
            },
        ))
    }

    /// Reads light state from the underlying fuchsia.hardware.Light API for the given hardware
    /// index and light type.
    async fn light_state_from_hardware_index(
        &self,
        index: u32,
        light_type: LightType,
    ) -> Result<LightState, ControllerError> {
        // Read the proper value depending on the light type.
        let value = match light_type {
            LightType::Brightness => {
                call_async!(self.light_proxy => get_current_brightness_value(index))
                    .await
                    .map_err(|e| format!("{e:?}"))
                    .and_then(|res| res.map_err(|e| format!("{e:?}")))
                    .map(LightValue::Brightness)
                    .map_err(|e| {
                        ControllerError::ExternalFailure(
                            SettingType::Light,
                            "fuchsia.hardware.light".into(),
                            format!("get_current_brightness_value for light {index}").into(),
                            e.into(),
                        )
                    })?
            }
            LightType::Rgb => call_async!(self.light_proxy => get_current_rgb_value(index))
                .await
                .map_err(|e| format!("{e:?}"))
                .and_then(|res| res.map_err(|e| format!("{e:?}")))
                .map(LightValue::from)
                .map_err(|e| {
                    ControllerError::ExternalFailure(
                        SettingType::Light,
                        "fuchsia.hardware.light".into(),
                        format!("get_current_rgb_value for light {index}").into(),
                        e.into(),
                    )
                })?,
            LightType::Simple => call_async!(self.light_proxy => get_current_simple_value(index))
                .await
                .map_err(|e| format!("{e:?}"))
                .and_then(|res| res.map_err(|e| format!("{e:?}")))
                .map(LightValue::Simple)
                .map_err(|e| {
                    ControllerError::ExternalFailure(
                        SettingType::Light,
                        "fuchsia.hardware.light".into(),
                        format!("get_current_simple_value for light {index}").into(),
                        e.into(),
                    )
                })?,
        };

        Ok(LightState { value: Some(value) })
    }
}

#[cfg(test)]
mod tests {
    use crate::handler::setting_handler::persist::ClientProxy;
    use crate::handler::setting_handler::ClientImpl;
    use crate::light::types::{LightInfo, LightState, LightType, LightValue};
    use crate::message::base::MessengerType;
    use crate::storage::{Payload as StoragePayload, StorageRequest, StorageResponse};
    use crate::tests::fakes::hardware_light_service::HardwareLightService;
    use crate::tests::fakes::service_registry::ServiceRegistry;
    use crate::{service, Address, LightController, ServiceContext, SettingType};
    use futures::lock::Mutex;
    use settings_storage::UpdateState;
    use std::rc::Rc;

    // Verify that a set call without a restore call succeeds. This can happen when the controller
    // is shutdown after inactivity and is brought up again to handle the set call.
    #[fuchsia::test(allow_stalls = false)]
    async fn test_set_before_restore() {
        let message_hub = service::MessageHub::create_hub();

        // Create the messenger that the client proxy uses to send messages.
        let (controller_messenger, _) = message_hub
            .create(MessengerType::Unbound)
            .await
            .expect("Unable to create agent messenger");

        // Create a fake hardware light service that responds to FIDL calls and add it to the
        // service registry so that FIDL calls are routed to this fake service.
        let service_registry = ServiceRegistry::create();
        let light_service_handle = Rc::new(Mutex::new(HardwareLightService::new()));
        service_registry.lock().await.register_service(light_service_handle.clone());

        let service_context =
            ServiceContext::new(Some(ServiceRegistry::serve(service_registry)), None);

        // Add a light to the fake service.
        light_service_handle
            .lock()
            .await
            .insert_light(0, "light_1".to_string(), LightType::Simple, LightValue::Simple(false))
            .await;

        // This isn't actually the signature for the notifier, but it's unused in this test, so just
        // provide the signature of its own messenger to the client proxy.
        let signature = controller_messenger.get_signature();

        let base_proxy = ClientImpl::for_test(
            Default::default(),
            controller_messenger,
            signature,
            Rc::new(service_context),
            SettingType::Light,
        );

        // Create a fake storage receptor used to receive and respond to storage messages.
        let (_, mut storage_receptor) = message_hub
            .create(MessengerType::Addressable(Address::Storage))
            .await
            .expect("Unable to create agent messenger");

        // Spawn a task that mimics the storage agent by responding to read/write calls.
        fuchsia_async::Task::local(async move {
            loop {
                if let Ok((payload, message_client)) = storage_receptor.next_payload().await {
                    if let Ok(StoragePayload::Request(storage_request)) =
                        StoragePayload::try_from(payload)
                    {
                        match storage_request {
                            StorageRequest::Read(_, _) => {
                                // Just respond with the default value as we're not testing storage.
                                let _ = message_client.reply(service::Payload::Storage(
                                    StoragePayload::Response(StorageResponse::Read(
                                        LightInfo::default().into(),
                                    )),
                                ));
                            }
                            StorageRequest::Write(_, _) => {
                                // Just respond with Unchanged as we're not testing storage.
                                let _ = message_client.reply(service::Payload::Storage(
                                    StoragePayload::Response(StorageResponse::Write(Ok(
                                        UpdateState::Unchanged,
                                    ))),
                                ));
                            }
                        }
                    }
                }
            }
        })
        .detach();

        let client_proxy = ClientProxy::new(Rc::new(base_proxy), SettingType::Light).await;

        // Create the light controller.
        let light_controller = LightController::create_with_config(client_proxy, None)
            .await
            .expect("Failed to create light controller");

        // Call set and verify it succeeds.
        let _ = light_controller
            .set("light_1".to_string(), vec![LightState { value: Some(LightValue::Simple(true)) }])
            .await
            .expect("Set call failed");

        // Verify the data cache is populated after the set call.
        let _ =
            light_controller.data_cache.lock().await.as_ref().expect("Data cache is not populated");
    }

    // Verify that an on_mic_mute event without a restore call succeeds. This can happen when the
    // controller is shutdown after inactivity and is brought up again to handle the set call.
    #[fuchsia::test(allow_stalls = false)]
    async fn test_on_mic_mute_before_restore() {
        let message_hub = service::MessageHub::create_hub();

        // Create the messenger that the client proxy uses to send messages.
        let (controller_messenger, _) = message_hub
            .create(MessengerType::Unbound)
            .await
            .expect("Unable to create agent messenger");

        // Create a fake hardware light service that responds to FIDL calls and add it to the
        // service registry so that FIDL calls are routed to this fake service.
        let service_registry = ServiceRegistry::create();
        let light_service_handle = Rc::new(Mutex::new(HardwareLightService::new()));
        service_registry.lock().await.register_service(light_service_handle.clone());

        let service_context =
            ServiceContext::new(Some(ServiceRegistry::serve(service_registry)), None);

        // Add a light to the fake service.
        light_service_handle
            .lock()
            .await
            .insert_light(0, "light_1".to_string(), LightType::Simple, LightValue::Simple(false))
            .await;

        // This isn't actually the signature for the notifier, but it's unused in this test, so just
        // provide the signature of its own messenger to the client proxy.
        let signature = controller_messenger.get_signature();

        let base_proxy = ClientImpl::for_test(
            Default::default(),
            controller_messenger,
            signature,
            Rc::new(service_context),
            SettingType::Light,
        );

        // Create a fake storage receptor used to receive and respond to storage messages.
        let (_, mut storage_receptor) = message_hub
            .create(MessengerType::Addressable(Address::Storage))
            .await
            .expect("Unable to create agent messenger");

        // Spawn a task that mimics the storage agent by responding to read/write calls.
        fuchsia_async::Task::local(async move {
            loop {
                if let Ok((payload, message_client)) = storage_receptor.next_payload().await {
                    if let Ok(StoragePayload::Request(storage_request)) =
                        StoragePayload::try_from(payload)
                    {
                        match storage_request {
                            StorageRequest::Read(_, _) => {
                                // Just respond with the default value as we're not testing storage.
                                let _ = message_client.reply(service::Payload::Storage(
                                    StoragePayload::Response(StorageResponse::Read(
                                        LightInfo::default().into(),
                                    )),
                                ));
                            }
                            StorageRequest::Write(_, _) => {
                                // Just respond with Unchanged as we're not testing storage.
                                let _ = message_client.reply(service::Payload::Storage(
                                    StoragePayload::Response(StorageResponse::Write(Ok(
                                        UpdateState::Unchanged,
                                    ))),
                                ));
                            }
                        }
                    }
                }
            }
        })
        .detach();

        let client_proxy = ClientProxy::new(Rc::new(base_proxy), SettingType::Light).await;

        // Create the light controller.
        let light_controller = LightController::create_with_config(client_proxy, None)
            .await
            .expect("Failed to create light controller");

        // Call on_mic_mute and verify it succeeds.
        let _ = light_controller.on_mic_mute(false).await.expect("Set call failed");

        // Verify the data cache is populated after the set call.
        let _ =
            light_controller.data_cache.lock().await.as_ref().expect("Data cache is not populated");
    }
}