driver_tools/subcommands/disable/
mod.rs

1// Copyright 2023 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::DisableCommand;
9use fidl_fuchsia_driver_development as fdd;
10use std::io::Write;
11use zx_status::Status;
12
13pub async fn disable(
14    cmd: DisableCommand,
15    writer: &mut dyn Write,
16    driver_development_proxy: fdd::ManagerProxy,
17) -> Result<()> {
18    writeln!(writer, "Disabling {} and restarting driver hosts with rematching enabled.", cmd.url)?;
19
20    let result = driver_development_proxy.disable_driver(&cmd.url, None).await?;
21    match result {
22        Ok(_) => {
23            writeln!(writer, "Disabled driver successfully.")?;
24        }
25        Err(e) => {
26            if e == Status::NOT_FOUND.into_raw() {
27                writeln!(writer, "No drivers affected in this disable operation.")?;
28            } else {
29                writeln!(writer, "Unexpected error from disable: {}", e)?;
30            }
31        }
32    }
33
34    let rebind_result = driver_development_proxy.rebind_composites_with_driver(&cmd.url).await?;
35    match rebind_result {
36        Ok(count) => {
37            if count > 0 {
38                writeln!(writer, "Rebound {count} composites successfully.")?;
39            } else {
40                writeln!(writer, "No composites affected in this operation.")?;
41            }
42        }
43        Err(e) => {
44            writeln!(writer, "Unexpected error from rebind: {}", e)?;
45        }
46    }
47
48    let restart_result = driver_development_proxy
49        .restart_driver_hosts(
50            cmd.url.as_str(),
51            fdd::RestartRematchFlags::REQUESTED | fdd::RestartRematchFlags::COMPOSITE_SPEC,
52        )
53        .await?;
54
55    match restart_result {
56        Ok(count) => {
57            if count > 0 {
58                writeln!(
59                    writer,
60                    "Successfully restarted. Rematched {} driver hosts that had the disabled driver.",
61                    count
62                )?;
63            } else {
64                writeln!(writer, "{}", "Successfully restarted.")?;
65            }
66        }
67        Err(err) => {
68            return Err(format_err!(
69                "Failed to restart existing drivers: {:?}",
70                Status::from_raw(err)
71            ));
72        }
73    }
74
75    Ok(())
76}