drivers_only_common/
lib.rs

1// Copyright 2024 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::HandleBased;
6use fidl::endpoints::{Proxy, create_endpoints, create_proxy};
7use fuchsia_component::client::connect_to_protocol;
8use realm_client::{InstalledNamespace, extend_namespace};
9use std::sync::Arc;
10use test_realm_helpers::constants::TESTCONTROLLER_DRIVER_TOPOLOGICAL_PATH;
11use test_realm_helpers::tracing::Tracing;
12use {fidl_test_wlan_realm as fidl_realm, fidl_test_wlan_testcontroller as fidl_testcontroller};
13
14pub mod sme_helpers;
15
16pub struct DriversOnlyTestRealm {
17    testcontroller_proxy: Option<fidl_testcontroller::TestControllerProxy>,
18    _tracing: Tracing,
19    _test_ns: Arc<InstalledNamespace>,
20}
21
22impl DriversOnlyTestRealm {
23    pub async fn new() -> Self {
24        let realm_factory = connect_to_protocol::<fidl_realm::RealmFactoryMarker>()
25            .expect("Could not connect to realm factory protocol");
26
27        let (dict_client, dict_server) = create_endpoints();
28        let (dev_topological, dev_topological_server) = create_proxy();
29        let (_dev_class, dev_class_server) = create_proxy();
30
31        let (pkg_client, pkg_server) = create_endpoints();
32        fuchsia_fs::directory::open_channel_in_namespace(
33            "/pkg",
34            fidl_fuchsia_io::PERM_READABLE | fidl_fuchsia_io::PERM_EXECUTABLE,
35            pkg_server,
36        )
37        .expect("Could not open /pkg");
38
39        let options = fidl_realm::RealmOptions {
40            topology: Some(fidl_realm::Topology::DriversOnly(fidl_realm::DriversOnly {
41                driver_config: Some(fidl_realm::DriverConfig {
42                    dev_topological: Some(dev_topological_server),
43                    dev_class: Some(dev_class_server),
44                    driver_test_realm_start_args: Some(fidl_fuchsia_driver_test::RealmArgs {
45                        pkg: Some(pkg_client),
46                        ..Default::default()
47                    }),
48                    ..Default::default()
49                }),
50                ..Default::default()
51            })),
52            ..Default::default()
53        };
54
55        realm_factory
56            .create_realm2(options, dict_server)
57            .await
58            .expect("FIDL error on create_realm")
59            .expect("create_realm returned an error");
60
61        let testcontroller_proxy = device_watcher::recursive_wait_and_open::<
62            fidl_testcontroller::TestControllerMarker,
63        >(
64            &dev_topological, TESTCONTROLLER_DRIVER_TOPOLOGICAL_PATH
65        )
66        .await
67        .expect("Could not open testcontroller_proxy");
68
69        let test_ns = Arc::new(
70            extend_namespace(realm_factory, dict_client).await.expect("Failed to extend ns"),
71        );
72        let tracing = Tracing::start_at(Arc::clone(&test_ns)).await.unwrap();
73
74        Self {
75            testcontroller_proxy: Some(testcontroller_proxy),
76            _tracing: tracing,
77            _test_ns: test_ns,
78        }
79    }
80
81    pub fn testcontroller_proxy(&self) -> &fidl_testcontroller::TestControllerProxy {
82        self.testcontroller_proxy.as_ref().unwrap()
83    }
84
85    pub fn take_sync_testcontroller_proxy(
86        &mut self,
87    ) -> fidl_testcontroller::TestControllerSynchronousProxy {
88        fidl_testcontroller::TestControllerSynchronousProxy::new(fidl::Channel::from_handle(
89            self.testcontroller_proxy
90                .take()
91                .unwrap()
92                .into_channel()
93                .expect("Failed to get fidl::AsyncChannel from proxy")
94                .into_zx_channel()
95                .into_handle(),
96        ))
97    }
98}