blob: 965625ba81e3c23e6edf6ae7ebe6b4d5c41d0841 [file] [log] [blame]
Matt Spinler85be2e72017-04-28 15:16:48 -05001#!/usr/bin/env python
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 = '''/* This is a generated file. */
20#include "fan_defs.hpp"
21#include "types.hpp"
Matt Spinler35108a72017-09-28 13:02:32 -050022#include "groups.hpp"
Matt Spinler85be2e72017-04-28 15:16:48 -050023
24using namespace phosphor::fan::monitor;
Matt Spinler35108a72017-09-28 13:02:32 -050025using namespace phosphor::fan::trust;
Matt Spinler85be2e72017-04-28 15:16:48 -050026
27const std::vector<FanDefinition> fanDefinitions
28{
Matt Spinler35108a72017-09-28 13:02:32 -050029%for fan_data in data.get('fans', {}):
Matt Spinler85be2e72017-04-28 15:16:48 -050030 FanDefinition{"${fan_data['inventory']}",
31 ${fan_data['allowed_out_of_range_time']},
32 ${fan_data['deviation']},
33 ${fan_data['num_sensors_nonfunc_for_fan_nonfunc']},
34 std::vector<SensorDefinition>{
35 %for sensor in fan_data['sensors']:
36 <%
37 #has_target is a bool, and we need a true instead of True
38 has_target = str(sensor['has_target']).lower()
Lei YU80f271b2018-01-31 15:24:46 +080039 target_interface = sensor.get(
40 'target_interface',
41 'xyz.openbmc_project.Control.FanSpeed')
Lei YU8e5d1972018-01-26 17:14:00 +080042 factor = sensor.get('factor', 1)
43 offset = sensor.get('offset', 0)
Matt Spinler85be2e72017-04-28 15:16:48 -050044 %> \
Lei YU8e5d1972018-01-26 17:14:00 +080045 SensorDefinition{"${sensor['name']}",
46 ${has_target},
Lei YU80f271b2018-01-31 15:24:46 +080047 "${target_interface}",
Lei YU8e5d1972018-01-26 17:14:00 +080048 ${factor},
49 ${offset}},
Matt Spinler85be2e72017-04-28 15:16:48 -050050 %endfor
51 },
52 },
53%endfor
54};
Matt Spinler35108a72017-09-28 13:02:32 -050055
56##Function to generate the group creation lambda.
57##If a group were to ever need a different constructor,
58##it could be handled here.
59<%def name="get_lambda_contents(group)">
60 std::vector<std::string> names{
61 %for sensor in group['sensors']:
62 "${sensor['name']}",
63 %endfor
64 };
65 return std::make_unique<${group['class']}>(names);
66</%def>
67const std::vector<CreateGroupFunction> trustGroups
68{
69%for group in data.get('sensor_trust_groups', {}):
70 {
71 []()
72 {\
73${get_lambda_contents(group)}\
74 }
75 },
76%endfor
77};
Matt Spinler85be2e72017-04-28 15:16:48 -050078'''
79
80
81if __name__ == '__main__':
82 parser = ArgumentParser(
83 description="Phosphor fan monitor definition parser")
84
85 parser.add_argument('-m', '--monitor_yaml', dest='monitor_yaml',
86 default="example/monitor.yaml",
87 help='fan monitor definitional yaml')
88 parser.add_argument('-o', '--output_dir', dest='output_dir',
89 default=".",
90 help='output directory')
91 args = parser.parse_args()
92
93 if not args.monitor_yaml:
94 parser.print_usage()
95 sys.exit(-1)
96
97 with open(args.monitor_yaml, 'r') as monitor_input:
98 monitor_data = yaml.safe_load(monitor_input) or {}
99
100 #Do some minor input validation
Matt Spinler35108a72017-09-28 13:02:32 -0500101 for fan in monitor_data.get('fans', {}):
Matt Spinler85be2e72017-04-28 15:16:48 -0500102 if ((fan['deviation'] < 0) or (fan['deviation'] > 100)):
103 sys.exit("Invalid deviation value " + str(fan['deviation']))
104
105 output_file = os.path.join(args.output_dir, "fan_monitor_defs.cpp")
106 with open(output_file, 'w') as output:
107 output.write(Template(tmpl).render(data=monitor_data))