Skip to main content

fake_pin/
lib.rs

1// Copyright 2026 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 fidl_fuchsia_driver_framework as fdf;
6use fidl_next::{Request, Responder};
7use fidl_next_fuchsia_hardware_pin::{self as fpin, PinStatesServerHandler};
8use fuchsia_async as fasync;
9use fuchsia_component::server::{ServiceFs, ServiceObjTrait};
10use fuchsia_sync::Mutex;
11use std::sync::Arc;
12
13struct FakePinStatesState {
14    current_state: String,
15}
16
17pub struct FakePinStates {
18    state: Arc<Mutex<FakePinStatesState>>,
19}
20
21impl Default for FakePinStates {
22    fn default() -> Self {
23        Self::new()
24    }
25}
26
27impl FakePinStates {
28    pub fn new() -> Self {
29        Self { state: Arc::new(Mutex::new(FakePinStatesState { current_state: String::new() })) }
30    }
31
32    pub fn current_state(&self) -> String {
33        self.state.lock().current_state.clone()
34    }
35
36    pub fn serve<O: ServiceObjTrait>(
37        &self,
38        service_fs: &mut ServiceFs<O>,
39        scope: fasync::ScopeHandle,
40        instance_name: &str,
41    ) -> fdf::Offer {
42        fdf_component::ServiceOffer::<fpin::PinStatesService>::new_next()
43            .add_default_named_next(
44                service_fs,
45                instance_name,
46                FakePinStatesService { state: self.state.clone(), scope },
47            )
48            .build_zircon_offer_next()
49    }
50}
51
52struct FakePinStatesService {
53    state: Arc<Mutex<FakePinStatesState>>,
54    scope: fasync::ScopeHandle,
55}
56
57impl fpin::PinStatesServiceHandler for FakePinStatesService {
58    fn device(&self, server_end: fidl_next::ServerEnd<fpin::PinStates>) {
59        server_end.spawn_on(FakePinStatesServer { state: self.state.clone() }, &self.scope);
60    }
61}
62
63struct FakePinStatesServer {
64    state: Arc<Mutex<FakePinStatesState>>,
65}
66
67impl PinStatesServerHandler for FakePinStatesServer {
68    async fn select_state(
69        &mut self,
70        request: Request<fpin::pin_states::SelectState>,
71        responder: Responder<fpin::pin_states::SelectState>,
72    ) {
73        self.state.lock().current_state = request.payload().name;
74        let _ = responder.respond(()).await;
75    }
76}