Skip to main content

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