Matthew Barth | f24d774 | 2020-03-17 16:12:15 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Matt Spinler | 85be2e7 | 2017-04-28 15:16:48 -0500 | [diff] [blame] | 2 | |
| 3 | import os |
| 4 | import sys |
Matt Spinler | 85be2e7 | 2017-04-28 15:16:48 -0500 | [diff] [blame] | 5 | from argparse import ArgumentParser |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 6 | |
| 7 | import yaml |
Matt Spinler | 85be2e7 | 2017-04-28 15:16:48 -0500 | [diff] [blame] | 8 | from mako.template import Template |
| 9 | |
| 10 | """ |
| 11 | This script generates the data structures for the |
| 12 | phosphor-fan-monitor application. |
| 13 | |
| 14 | A future improvement is to get the fan inventory names |
| 15 | from a separate file, so just that file could be generated |
| 16 | from the MRW. |
| 17 | """ |
| 18 | |
| 19 | |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 20 | tmpl = """\ |
Matthew Barth | 33618bc | 2018-05-03 10:55:11 -0500 | [diff] [blame] | 21 | <%! |
| 22 | def indent(str, depth): |
| 23 | return ''.join(4*' '*depth+line for line in str.splitlines(True)) |
| 24 | %>\ |
| 25 | <%def name="getCondParams(cond)" buffered="True"> |
| 26 | %if (cond['name'] == 'propertiesMatch'): |
| 27 | std::vector<PropertyState>{ |
| 28 | %for i in cond['properties']: |
| 29 | PropertyState{ |
| 30 | { |
| 31 | "${i['object']}", |
| 32 | "${i['interface']}", |
| 33 | "${i['property']['name']}" |
| 34 | }, |
| 35 | static_cast<${i['property']['type']}>(${str(i['property']['value']).lower()}) |
| 36 | }, |
| 37 | %endfor |
| 38 | } |
| 39 | %endif |
| 40 | </%def>\ |
| 41 | /* This is a generated file. */ |
Matt Spinler | 85be2e7 | 2017-04-28 15:16:48 -0500 | [diff] [blame] | 42 | #include "fan_defs.hpp" |
| 43 | #include "types.hpp" |
Matt Spinler | 35108a7 | 2017-09-28 13:02:32 -0500 | [diff] [blame] | 44 | #include "groups.hpp" |
Matthew Barth | 33618bc | 2018-05-03 10:55:11 -0500 | [diff] [blame] | 45 | #include "conditions.hpp" |
Matt Spinler | 85be2e7 | 2017-04-28 15:16:48 -0500 | [diff] [blame] | 46 | |
| 47 | using namespace phosphor::fan::monitor; |
Matt Spinler | 35108a7 | 2017-09-28 13:02:32 -0500 | [diff] [blame] | 48 | using namespace phosphor::fan::trust; |
Matt Spinler | 85be2e7 | 2017-04-28 15:16:48 -0500 | [diff] [blame] | 49 | |
| 50 | const std::vector<FanDefinition> fanDefinitions |
| 51 | { |
Matt Spinler | 35108a7 | 2017-09-28 13:02:32 -0500 | [diff] [blame] | 52 | %for fan_data in data.get('fans', {}): |
Matt Spinler | 85be2e7 | 2017-04-28 15:16:48 -0500 | [diff] [blame] | 53 | FanDefinition{"${fan_data['inventory']}", |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 54 | ${fan_data.get('method', {})}, |
Matthew Barth | 9396bcc | 2018-02-19 14:13:20 -0600 | [diff] [blame] | 55 | ${fan_data.get('functional_delay', 0)}, |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 56 | ${fan_data.get('allowed_out_of_range_time', {})}, |
Matt Spinler | 85be2e7 | 2017-04-28 15:16:48 -0500 | [diff] [blame] | 57 | ${fan_data['deviation']}, |
Matt Spinler | f724c16 | 2023-05-10 11:14:37 -0500 | [diff] [blame] | 58 | ${fan_data.get('upper_deviation', fan_data['deviation'])}, |
Matt Spinler | 85be2e7 | 2017-04-28 15:16:48 -0500 | [diff] [blame] | 59 | ${fan_data['num_sensors_nonfunc_for_fan_nonfunc']}, |
Matt Spinler | b0412d0 | 2020-10-12 16:53:52 -0500 | [diff] [blame] | 60 | 0, // Monitor start delay - not used in YAML configs |
Matt Spinler | 623635c | 2021-03-29 13:13:59 -0500 | [diff] [blame] | 61 | 0, // Count interval - not used in YAML configs |
Matt Spinler | f13b42e | 2020-10-26 15:29:49 -0500 | [diff] [blame] | 62 | std::nullopt, // nonfuncRotorErrorDelay - also not used here |
Matt Spinler | 27f6b68 | 2020-10-27 08:43:37 -0500 | [diff] [blame] | 63 | std::nullopt, // fanMissingErrorDelay - also not used here |
Matt Spinler | 85be2e7 | 2017-04-28 15:16:48 -0500 | [diff] [blame] | 64 | std::vector<SensorDefinition>{ |
| 65 | %for sensor in fan_data['sensors']: |
| 66 | <% |
| 67 | #has_target is a bool, and we need a true instead of True |
| 68 | has_target = str(sensor['has_target']).lower() |
Lei YU | 80f271b | 2018-01-31 15:24:46 +0800 | [diff] [blame] | 69 | target_interface = sensor.get( |
| 70 | 'target_interface', |
| 71 | 'xyz.openbmc_project.Control.FanSpeed') |
Chau Ly | 27cc39f | 2022-09-20 08:16:56 +0000 | [diff] [blame] | 72 | target_path = sensor.get( |
| 73 | 'target_path', |
| 74 | '') |
Lei YU | 8e5d197 | 2018-01-26 17:14:00 +0800 | [diff] [blame] | 75 | factor = sensor.get('factor', 1) |
| 76 | offset = sensor.get('offset', 0) |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 77 | threshold = sensor.get('threshold', 1) |
Matthew Barth | 8a8aa44 | 2021-11-19 14:13:13 -0600 | [diff] [blame] | 78 | ignore_above_max = str(sensor.get( |
| 79 | 'ignore_above_max', False)).lower() |
Matt Spinler | 85be2e7 | 2017-04-28 15:16:48 -0500 | [diff] [blame] | 80 | %> \ |
Lei YU | 8e5d197 | 2018-01-26 17:14:00 +0800 | [diff] [blame] | 81 | SensorDefinition{"${sensor['name']}", |
| 82 | ${has_target}, |
Lei YU | 80f271b | 2018-01-31 15:24:46 +0800 | [diff] [blame] | 83 | "${target_interface}", |
Chau Ly | 27cc39f | 2022-09-20 08:16:56 +0000 | [diff] [blame] | 84 | "${target_path}", |
Lei YU | 8e5d197 | 2018-01-26 17:14:00 +0800 | [diff] [blame] | 85 | ${factor}, |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 86 | ${offset}, |
Matthew Barth | 8a8aa44 | 2021-11-19 14:13:13 -0600 | [diff] [blame] | 87 | ${threshold}, |
| 88 | ${ignore_above_max}}, |
Matt Spinler | 85be2e7 | 2017-04-28 15:16:48 -0500 | [diff] [blame] | 89 | %endfor |
| 90 | }, |
Matthew Barth | 33618bc | 2018-05-03 10:55:11 -0500 | [diff] [blame] | 91 | %if ('condition' in fan_data) and \ |
| 92 | (fan_data['condition'] is not None): |
| 93 | make_condition(condition::${fan_data['condition']['name']}(\ |
| 94 | ${indent(getCondParams(cond=fan_data['condition']), 5)}\ |
Matt Spinler | 086e816 | 2023-05-09 11:14:41 -0500 | [diff] [blame] | 95 | )), |
Matthew Barth | 33618bc | 2018-05-03 10:55:11 -0500 | [diff] [blame] | 96 | %else: |
Matt Spinler | a3584bd | 2021-03-29 15:48:30 -0500 | [diff] [blame] | 97 | {}, |
Matthew Barth | 33618bc | 2018-05-03 10:55:11 -0500 | [diff] [blame] | 98 | %endif |
Matt Spinler | 086e816 | 2023-05-09 11:14:41 -0500 | [diff] [blame] | 99 | false // set_func_on_present. Hardcoded to false. |
Matt Spinler | 85be2e7 | 2017-04-28 15:16:48 -0500 | [diff] [blame] | 100 | }, |
| 101 | %endfor |
| 102 | }; |
Matt Spinler | 35108a7 | 2017-09-28 13:02:32 -0500 | [diff] [blame] | 103 | |
| 104 | ##Function to generate the group creation lambda. |
| 105 | ##If a group were to ever need a different constructor, |
| 106 | ##it could be handled here. |
| 107 | <%def name="get_lambda_contents(group)"> |
Matthew Barth | 6f31d19 | 2018-01-30 13:06:27 -0600 | [diff] [blame] | 108 | std::vector<GroupDefinition> group{ |
| 109 | %for member in group['group']: |
| 110 | <% |
| 111 | in_trust = str(member.get('in_trust', "true")).lower() |
| 112 | %> |
| 113 | GroupDefinition{"${member['name']}", ${in_trust}}, |
Matt Spinler | 35108a7 | 2017-09-28 13:02:32 -0500 | [diff] [blame] | 114 | %endfor |
| 115 | }; |
Matthew Barth | 6f31d19 | 2018-01-30 13:06:27 -0600 | [diff] [blame] | 116 | return std::make_unique<${group['class']}>(group); |
Matt Spinler | 35108a7 | 2017-09-28 13:02:32 -0500 | [diff] [blame] | 117 | </%def> |
| 118 | const std::vector<CreateGroupFunction> trustGroups |
| 119 | { |
| 120 | %for group in data.get('sensor_trust_groups', {}): |
| 121 | { |
| 122 | []() |
| 123 | {\ |
| 124 | ${get_lambda_contents(group)}\ |
| 125 | } |
| 126 | }, |
| 127 | %endfor |
| 128 | }; |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 129 | """ |
Matt Spinler | 85be2e7 | 2017-04-28 15:16:48 -0500 | [diff] [blame] | 130 | |
| 131 | |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 132 | if __name__ == "__main__": |
Matt Spinler | 85be2e7 | 2017-04-28 15:16:48 -0500 | [diff] [blame] | 133 | parser = ArgumentParser( |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 134 | description="Phosphor fan monitor definition parser" |
| 135 | ) |
Matt Spinler | 85be2e7 | 2017-04-28 15:16:48 -0500 | [diff] [blame] | 136 | |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 137 | parser.add_argument( |
| 138 | "-m", |
| 139 | "--monitor_yaml", |
| 140 | dest="monitor_yaml", |
| 141 | default="example/monitor.yaml", |
| 142 | help="fan monitor definitional yaml", |
| 143 | ) |
| 144 | parser.add_argument( |
| 145 | "-o", |
| 146 | "--output_dir", |
| 147 | dest="output_dir", |
| 148 | default=".", |
| 149 | help="output directory", |
| 150 | ) |
Matt Spinler | 85be2e7 | 2017-04-28 15:16:48 -0500 | [diff] [blame] | 151 | args = parser.parse_args() |
| 152 | |
| 153 | if not args.monitor_yaml: |
| 154 | parser.print_usage() |
William A. Kennington III | 3e78106 | 2018-10-19 17:18:34 -0700 | [diff] [blame] | 155 | sys.exit(1) |
Matt Spinler | 85be2e7 | 2017-04-28 15:16:48 -0500 | [diff] [blame] | 156 | |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 157 | with open(args.monitor_yaml, "r") as monitor_input: |
Matt Spinler | 85be2e7 | 2017-04-28 15:16:48 -0500 | [diff] [blame] | 158 | monitor_data = yaml.safe_load(monitor_input) or {} |
| 159 | |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 160 | # Do some minor input validation |
| 161 | for fan in monitor_data.get("fans", {}): |
| 162 | if (fan["deviation"] < 0) or (fan["deviation"] > 100): |
| 163 | sys.exit("Invalid deviation value " + str(fan["deviation"])) |
Matt Spinler | 85be2e7 | 2017-04-28 15:16:48 -0500 | [diff] [blame] | 164 | |
| 165 | output_file = os.path.join(args.output_dir, "fan_monitor_defs.cpp") |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 166 | with open(output_file, "w") as output: |
Matt Spinler | 85be2e7 | 2017-04-28 15:16:48 -0500 | [diff] [blame] | 167 | output.write(Template(tmpl).render(data=monitor_data)) |