blob: 7386508246591766c2bf7730e805a629d2288fc0 [file] [log] [blame]
Matthew Barthf24d7742020-03-17 16:12:15 -05001#!/usr/bin/env python3
Matt Spinler85be2e72017-04-28 15:16:48 -05002
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
Matthew Barth33618bc2018-05-03 10:55:11 -050019tmpl = '''\
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. */
Matt Spinler85be2e72017-04-28 15:16:48 -050041#include "fan_defs.hpp"
42#include "types.hpp"
Matt Spinler35108a72017-09-28 13:02:32 -050043#include "groups.hpp"
Matthew Barth33618bc2018-05-03 10:55:11 -050044#include "conditions.hpp"
Matt Spinler85be2e72017-04-28 15:16:48 -050045
46using namespace phosphor::fan::monitor;
Matt Spinler35108a72017-09-28 13:02:32 -050047using namespace phosphor::fan::trust;
Matt Spinler85be2e72017-04-28 15:16:48 -050048
49const std::vector<FanDefinition> fanDefinitions
50{
Matt Spinler35108a72017-09-28 13:02:32 -050051%for fan_data in data.get('fans', {}):
Matt Spinler85be2e72017-04-28 15:16:48 -050052 FanDefinition{"${fan_data['inventory']}",
Matthew Barth9396bcc2018-02-19 14:13:20 -060053 ${fan_data.get('functional_delay', 0)},
Matt Spinler85be2e72017-04-28 15:16:48 -050054 ${fan_data['allowed_out_of_range_time']},
55 ${fan_data['deviation']},
56 ${fan_data['num_sensors_nonfunc_for_fan_nonfunc']},
Matt Spinlerb0412d02020-10-12 16:53:52 -050057 0, // Monitor start delay - not used in YAML configs
Matt Spinlerf13b42e2020-10-26 15:29:49 -050058 std::nullopt, // nonfuncRotorErrorDelay - also not used here
Matt Spinler27f6b682020-10-27 08:43:37 -050059 std::nullopt, // fanMissingErrorDelay - also not used here
Matt Spinler85be2e72017-04-28 15:16:48 -050060 std::vector<SensorDefinition>{
61 %for sensor in fan_data['sensors']:
62 <%
63 #has_target is a bool, and we need a true instead of True
64 has_target = str(sensor['has_target']).lower()
Lei YU80f271b2018-01-31 15:24:46 +080065 target_interface = sensor.get(
66 'target_interface',
67 'xyz.openbmc_project.Control.FanSpeed')
Lei YU8e5d1972018-01-26 17:14:00 +080068 factor = sensor.get('factor', 1)
69 offset = sensor.get('offset', 0)
Matt Spinler85be2e72017-04-28 15:16:48 -050070 %> \
Lei YU8e5d1972018-01-26 17:14:00 +080071 SensorDefinition{"${sensor['name']}",
72 ${has_target},
Lei YU80f271b2018-01-31 15:24:46 +080073 "${target_interface}",
Lei YU8e5d1972018-01-26 17:14:00 +080074 ${factor},
75 ${offset}},
Matt Spinler85be2e72017-04-28 15:16:48 -050076 %endfor
77 },
Matthew Barth33618bc2018-05-03 10:55:11 -050078 %if ('condition' in fan_data) and \
79 (fan_data['condition'] is not None):
80 make_condition(condition::${fan_data['condition']['name']}(\
81 ${indent(getCondParams(cond=fan_data['condition']), 5)}\
82 ))
83 %else:
84 {}
85 %endif
Matt Spinler85be2e72017-04-28 15:16:48 -050086 },
87%endfor
88};
Matt Spinler35108a72017-09-28 13:02:32 -050089
90##Function to generate the group creation lambda.
91##If a group were to ever need a different constructor,
92##it could be handled here.
93<%def name="get_lambda_contents(group)">
Matthew Barth6f31d192018-01-30 13:06:27 -060094 std::vector<GroupDefinition> group{
95 %for member in group['group']:
96 <%
97 in_trust = str(member.get('in_trust', "true")).lower()
98 %>
99 GroupDefinition{"${member['name']}", ${in_trust}},
Matt Spinler35108a72017-09-28 13:02:32 -0500100 %endfor
101 };
Matthew Barth6f31d192018-01-30 13:06:27 -0600102 return std::make_unique<${group['class']}>(group);
Matt Spinler35108a72017-09-28 13:02:32 -0500103</%def>
104const std::vector<CreateGroupFunction> trustGroups
105{
106%for group in data.get('sensor_trust_groups', {}):
107 {
108 []()
109 {\
110${get_lambda_contents(group)}\
111 }
112 },
113%endfor
114};
Matt Spinler85be2e72017-04-28 15:16:48 -0500115'''
116
117
118if __name__ == '__main__':
119 parser = ArgumentParser(
120 description="Phosphor fan monitor definition parser")
121
122 parser.add_argument('-m', '--monitor_yaml', dest='monitor_yaml',
123 default="example/monitor.yaml",
124 help='fan monitor definitional yaml')
125 parser.add_argument('-o', '--output_dir', dest='output_dir',
126 default=".",
127 help='output directory')
128 args = parser.parse_args()
129
130 if not args.monitor_yaml:
131 parser.print_usage()
William A. Kennington III3e781062018-10-19 17:18:34 -0700132 sys.exit(1)
Matt Spinler85be2e72017-04-28 15:16:48 -0500133
134 with open(args.monitor_yaml, 'r') as monitor_input:
135 monitor_data = yaml.safe_load(monitor_input) or {}
136
137 #Do some minor input validation
Matt Spinler35108a72017-09-28 13:02:32 -0500138 for fan in monitor_data.get('fans', {}):
Matt Spinler85be2e72017-04-28 15:16:48 -0500139 if ((fan['deviation'] < 0) or (fan['deviation'] > 100)):
140 sys.exit("Invalid deviation value " + str(fan['deviation']))
141
142 output_file = os.path.join(args.output_dir, "fan_monitor_defs.cpp")
143 with open(output_file, 'w') as output:
144 output.write(Template(tmpl).render(data=monitor_data))