Skip to main content

routing/
config.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 crate::RoutingError;
6use crate::bedrock::dict_ext::DictExt;
7use crate::bedrock::request_metadata;
8use crate::component_instance::ComponentInstanceInterface;
9use cm_rust::{Availability, FidlIntoNative};
10use router_error::Explain;
11use runtime_capabilities::Data;
12use std::sync::Arc;
13use zx_status as zx;
14
15/// Get a specific configuration use declaration from the structured config key value.
16pub fn get_use_config_from_key<'a>(
17    key: &str,
18    decl: &'a cm_rust::ComponentDecl,
19) -> Option<&'a cm_rust::UseConfigurationDecl> {
20    decl.uses.iter().find_map(|use_| match use_ {
21        cm_rust::UseDecl::Config(c) => (c.target_name == key).then_some(&**c),
22        _ => None,
23    })
24}
25
26/// Routes the config value referenced in `use_config` from `component`. Returns the default value
27/// if the capability is not available (i.e. routed from void), or if `use_config` has transitional
28/// availability and routing fails with an error that maps to `NOT_FOUND`.
29pub async fn route_config_value<C>(
30    use_config: &cm_rust::UseConfigurationDecl,
31    component: &Arc<C>,
32) -> Result<Option<cm_rust::ConfigValue>, router_error::RouterError>
33where
34    C: ComponentInstanceInterface + 'static,
35{
36    let component_sandbox =
37        component.component_sandbox().await.map_err(|e| RoutingError::from(e))?;
38    let capability =
39        match component_sandbox.program_input.config().get_capability(&use_config.target_name) {
40            Some(c) => c,
41            None => {
42                return Err(RoutingError::BedrockNotPresentInDictionary {
43                    name: use_config.target_name.to_string(),
44                    moniker: component.moniker().clone().into(),
45                }
46                .into());
47            }
48        };
49    let runtime_capabilities::Capability::DataRouter(router) = capability else {
50        return Err(RoutingError::BedrockWrongCapabilityType {
51            actual: format!("{:?}", capability),
52            expected: "Router".to_string(),
53            moniker: component.moniker().clone().into(),
54        }
55        .into());
56    };
57    let request = request_metadata::config_metadata(use_config.availability);
58    let data = match router.route(request, component.as_weak().into()).await {
59        Ok(Some(d)) => d,
60        Ok(None) => return Ok(use_config.default.clone()),
61        Err(e)
62            if use_config.availability == Availability::Transitional
63                && e.as_zx_status() == zx::Status::NOT_FOUND =>
64        {
65            return Ok(use_config.default.clone());
66        }
67        Err(e) => return Err(e),
68    };
69    let Data::Bytes(bytes) = data else {
70        return Err(RoutingError::BedrockWrongCapabilityType {
71            actual: format!("{:?}", data),
72            expected: "Data::bytes".to_string(),
73            moniker: component.moniker().clone().into(),
74        }
75        .into());
76    };
77    let config_value: fidl_fuchsia_component_decl::ConfigValue = match fidl::unpersist(&bytes) {
78        Ok(v) => v,
79        Err(_) => {
80            return Err(RoutingError::BedrockWrongCapabilityType {
81                actual: "{unknown}".into(),
82                expected: "fuchsia.component.decl.ConfigValue".into(),
83                moniker: component.moniker().clone().into(),
84            }
85            .into());
86        }
87    };
88
89    Ok(Some(config_value.fidl_into_native()))
90}