driver_tools/subcommands/register/
mod.rs

1// Copyright 2022 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
5pub mod args;
6
7use anyhow::{format_err, Result};
8use args::RegisterCommand;
9use std::io::Write;
10use zx_status::Status;
11use {fidl_fuchsia_driver_development as fdd, fidl_fuchsia_driver_registrar as fdr};
12
13pub async fn register(
14    cmd: RegisterCommand,
15    writer: &mut dyn Write,
16    driver_registrar_proxy: fdr::DriverRegistrarProxy,
17    driver_development_proxy: fdd::ManagerProxy,
18) -> Result<()> {
19    writeln!(
20        writer,
21        "Registering {}, restarting driver hosts, and attempting to bind to unbound nodes",
22        cmd.url
23    )?;
24    let register_result = driver_registrar_proxy.register(&cmd.url).await?;
25
26    match register_result {
27        Ok(_) => {}
28        Err(e) => {
29            return Err(format_err!("Failed to register driver: {}", e));
30        }
31    }
32
33    let mut existing = false;
34    let restart_result = driver_development_proxy
35        .restart_driver_hosts(cmd.url.as_str(), fdd::RestartRematchFlags::empty())
36        .await?;
37    match restart_result {
38        Ok(count) => {
39            if count > 0 {
40                existing = true;
41                writeln!(writer, "Successfully restarted {} driver hosts with the driver.", count)?;
42            }
43        }
44        Err(err) => {
45            return Err(format_err!(
46                "Failed to restart existing drivers: {:?}",
47                Status::from_raw(err)
48            ));
49        }
50    }
51
52    let bind_result = driver_development_proxy.bind_all_unbound_nodes2().await?;
53
54    match bind_result {
55        Ok(result) => {
56            if result.is_empty() {
57                if !existing {
58                    writeln!(
59                        writer,
60                        "{}\n{}",
61                        "There are no existing driver hosts with this driver.",
62                        "No new nodes were bound to the driver being registered.",
63                    )?;
64                }
65            } else {
66                writeln!(writer, "Successfully bound:")?;
67                for info in result {
68                    writeln!(
69                        writer,
70                        "Node '{}':\nDriver '{:#?}'\nComposite Specs '{:#?}'",
71                        info.node_name.unwrap_or_else(|| "<NA>".to_string()),
72                        info.driver_url,
73                        info.composite_parents,
74                    )?;
75                }
76            }
77        }
78        Err(err) => {
79            return Err(format_err!("Failed to bind nodes: {:?}", Status::from_raw(err)));
80        }
81    };
82    Ok(())
83}