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