1// Copyright 2020 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.
45use crate::agent::AgentCreator;
6use serde::{Deserialize, Serialize};
7use std::collections::HashSet;
89/// The flags used to control behavior of controllers.
10#[derive(PartialEq, Debug, Eq, Hash, Copy, Clone, Serialize, Deserialize)]
11pub enum ControllerFlag {
12/// This flag controls whether an external service is in control of the
13 /// brightness configuration.
14ExternalBrightnessControl,
15}
1617#[derive(Clone, Debug, PartialEq)]
18pub enum Event {
19/// A load of a config file with the given information about the load.
20Load(ConfigLoadInfo),
21}
2223#[derive(Clone, Debug, PartialEq)]
24pub struct ConfigLoadInfo {
25/// The status of the load.
26pub status: ConfigLoadStatus,
27/// The contents of the loaded config file.
28pub contents: Option<String>,
29}
3031#[derive(Clone, Debug, PartialEq)]
32pub enum ConfigLoadStatus {
33/// Failed to parse the file.
34ParseFailure(String),
35/// Successfully loaded the file.
36Success,
37/// Falling back to default.
38UsingDefaults(String),
39}
4041impl From<ConfigLoadStatus> for String {
42fn from(status: ConfigLoadStatus) -> String {
43match status {
44 ConfigLoadStatus::ParseFailure(_) => "ParseFailure".to_string(),
45 ConfigLoadStatus::Success => "Success".to_string(),
46 ConfigLoadStatus::UsingDefaults(_) => "UsingDefaults".to_string(),
47 }
48 }
49}
5051/// Represents each agent that can be run.
52#[derive(Eq, PartialEq, Hash, Debug, Copy, Clone, Deserialize)]
53pub enum AgentType {
54/// Responsible for watching the camera3 mute status. If other clients
55 /// of the camera3 api modify the camera state, the agent should watch and
56 /// should coordinate that change with the internal camera state.
57CameraWatcher,
58/// Plays earcons in response to certain events. If MediaButtons is
59 /// enabled, then it will also handle some media buttons events.
60Earcons,
61/// Responsible for managing the connection to media buttons. It will
62 /// broadcast events to the controllers and agents.
63MediaButtons,
64/// Responsible for initializing all of the controllers.
65Restore,
66/// Responsible for logging external API calls to other components and
67 /// their responses to Inspect.
68InspectExternalApis,
69/// Responsible for logging all settings values of messages between the
70 /// proxy and setting handlers to Inspect.
71InspectSettingProxy,
72/// Responsible for logging the setting values in the setting proxy to Inspect.
73InspectSettingValues,
74/// Responsible for logging API usage counts to Inspect.
75InspectSettingTypeUsage,
76}
7778pub fn get_default_agent_types() -> HashSet<AgentType> {
79 [
80 AgentType::Restore,
81 AgentType::InspectExternalApis,
82 AgentType::InspectSettingProxy,
83 AgentType::InspectSettingValues,
84 AgentType::InspectSettingTypeUsage,
85 ]
86 .into_iter()
87 .collect()
88}
8990#[macro_export]
91macro_rules! create_agent {
92 ($component:ident, $create:expr) => {
93 AgentCreator {
94 debug_id: concat!(stringify!($component), "_agent"),
95 create: $crate::agent::CreationFunc::Static(|c| Box::pin($create(c))),
96 }
97 };
98}
99100impl From<AgentType> for AgentCreator {
101fn from(agent_type: AgentType) -> AgentCreator {
102use crate::agent::*;
103match agent_type {
104 AgentType::CameraWatcher => {
105create_agent!(camera_watcher, camera_watcher::CameraWatcherAgent::create)
106 }
107 AgentType::Earcons => create_agent!(earcons, earcons::agent::Agent::create),
108 AgentType::MediaButtons => {
109create_agent!(media_buttons, media_buttons::MediaButtonsAgent::create)
110 }
111 AgentType::Restore => create_agent!(restore_agent, restore_agent::RestoreAgent::create),
112 AgentType::InspectExternalApis => create_agent!(
113 external_apis,
114 inspect::external_apis::ExternalApiInspectAgent::create
115 ),
116 AgentType::InspectSettingProxy => create_agent!(
117 setting_proxy,
118 inspect::setting_proxy::SettingProxyInspectAgent::create
119 ),
120 AgentType::InspectSettingTypeUsage => create_agent!(
121 usage_counts,
122 inspect::usage_counts::SettingTypeUsageInspectAgent::create
123 ),
124 AgentType::InspectSettingValues => create_agent!(
125 setting_values,
126 inspect::setting_values::SettingValuesInspectAgent::create
127 ),
128 }
129 }
130}