1#!/usr/bin/env python3 2 3import os 4import sys 5import yaml 6from argparse import ArgumentParser 7from mako.template import Template 8 9""" 10This script generates the data structures for the 11phosphor-fan-monitor application. 12 13A future improvement is to get the fan inventory names 14from a separate file, so just that file could be generated 15from the MRW. 16""" 17 18 19tmpl = '''\ 20<%! 21def 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'): 26std::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. */ 41#include "fan_defs.hpp" 42#include "types.hpp" 43#include "groups.hpp" 44#include "conditions.hpp" 45 46using namespace phosphor::fan::monitor; 47using namespace phosphor::fan::trust; 48 49const std::vector<FanDefinition> fanDefinitions 50{ 51%for fan_data in data.get('fans', {}): 52 FanDefinition{"${fan_data['inventory']}", 53 ${fan_data.get('method', {})}, 54 ${fan_data.get('functional_delay', 0)}, 55 ${fan_data.get('allowed_out_of_range_time', {})}, 56 ${fan_data['deviation']}, 57 ${fan_data['num_sensors_nonfunc_for_fan_nonfunc']}, 58 0, // Monitor start delay - not used in YAML configs 59 0, // Count interval - not used in YAML configs 60 std::nullopt, // nonfuncRotorErrorDelay - also not used here 61 std::nullopt, // fanMissingErrorDelay - also not used here 62 std::vector<SensorDefinition>{ 63 %for sensor in fan_data['sensors']: 64 <% 65 #has_target is a bool, and we need a true instead of True 66 has_target = str(sensor['has_target']).lower() 67 target_interface = sensor.get( 68 'target_interface', 69 'xyz.openbmc_project.Control.FanSpeed') 70 factor = sensor.get('factor', 1) 71 offset = sensor.get('offset', 0) 72 threshold = sensor.get('threshold', 1) 73 ignore_above_max = str(sensor.get( 74 'ignore_above_max', False)).lower() 75 %> \ 76 SensorDefinition{"${sensor['name']}", 77 ${has_target}, 78 "${target_interface}", 79 ${factor}, 80 ${offset}, 81 ${threshold}, 82 ${ignore_above_max}}, 83 %endfor 84 }, 85 %if ('condition' in fan_data) and \ 86 (fan_data['condition'] is not None): 87 make_condition(condition::${fan_data['condition']['name']}(\ 88 ${indent(getCondParams(cond=fan_data['condition']), 5)}\ 89 )) 90 %else: 91 {}, 92 false // set_func_on_present. Hardcoded to false. 93 %endif 94 }, 95%endfor 96}; 97 98##Function to generate the group creation lambda. 99##If a group were to ever need a different constructor, 100##it could be handled here. 101<%def name="get_lambda_contents(group)"> 102 std::vector<GroupDefinition> group{ 103 %for member in group['group']: 104 <% 105 in_trust = str(member.get('in_trust', "true")).lower() 106 %> 107 GroupDefinition{"${member['name']}", ${in_trust}}, 108 %endfor 109 }; 110 return std::make_unique<${group['class']}>(group); 111</%def> 112const std::vector<CreateGroupFunction> trustGroups 113{ 114%for group in data.get('sensor_trust_groups', {}): 115 { 116 []() 117 {\ 118${get_lambda_contents(group)}\ 119 } 120 }, 121%endfor 122}; 123''' 124 125 126if __name__ == '__main__': 127 parser = ArgumentParser( 128 description="Phosphor fan monitor definition parser") 129 130 parser.add_argument('-m', '--monitor_yaml', dest='monitor_yaml', 131 default="example/monitor.yaml", 132 help='fan monitor definitional yaml') 133 parser.add_argument('-o', '--output_dir', dest='output_dir', 134 default=".", 135 help='output directory') 136 args = parser.parse_args() 137 138 if not args.monitor_yaml: 139 parser.print_usage() 140 sys.exit(1) 141 142 with open(args.monitor_yaml, 'r') as monitor_input: 143 monitor_data = yaml.safe_load(monitor_input) or {} 144 145 #Do some minor input validation 146 for fan in monitor_data.get('fans', {}): 147 if ((fan['deviation'] < 0) or (fan['deviation'] > 100)): 148 sys.exit("Invalid deviation value " + str(fan['deviation'])) 149 150 output_file = os.path.join(args.output_dir, "fan_monitor_defs.cpp") 151 with open(output_file, 'w') as output: 152 output.write(Template(tmpl).render(data=monitor_data)) 153