Matt Spinler | d08dbe2 | 2017-04-11 13:52:54 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | """ |
| 4 | This script reads in fan definition and zone definition YAML |
| 5 | files and generates a set of structures for use by the fan control code. |
| 6 | """ |
| 7 | |
| 8 | import os |
| 9 | import sys |
| 10 | import yaml |
| 11 | from argparse import ArgumentParser |
| 12 | from mako.template import Template |
| 13 | |
Matt Spinler | ee7f642 | 2017-05-09 11:03:14 -0500 | [diff] [blame] | 14 | #Note: Condition is a TODO (openbmc/openbmc#1500) |
Matt Spinler | d08dbe2 | 2017-04-11 13:52:54 -0500 | [diff] [blame] | 15 | tmpl = '''/* This is a generated file. */ |
| 16 | #include "manager.hpp" |
Matthew Barth | ba102b3 | 2017-05-16 16:13:56 -0500 | [diff] [blame] | 17 | #include "functor.hpp" |
| 18 | #include "actions.hpp" |
Matthew Barth | 7e527c1 | 2017-05-17 10:14:15 -0500 | [diff] [blame] | 19 | #include "handlers.hpp" |
Matt Spinler | d08dbe2 | 2017-04-11 13:52:54 -0500 | [diff] [blame] | 20 | |
| 21 | using namespace phosphor::fan::control; |
| 22 | |
Matt Spinler | ee7f642 | 2017-05-09 11:03:14 -0500 | [diff] [blame] | 23 | const unsigned int Manager::_powerOnDelay{${mgr_data['power_on_delay']}}; |
| 24 | |
Matt Spinler | d08dbe2 | 2017-04-11 13:52:54 -0500 | [diff] [blame] | 25 | const std::vector<ZoneGroup> Manager::_zoneLayouts |
| 26 | { |
| 27 | %for zone_group in zones: |
Matthew Barth | 9c72667 | 2017-05-18 13:44:46 -0500 | [diff] [blame] | 28 | ZoneGroup{ |
| 29 | std::vector<Condition>{}, |
| 30 | std::vector<ZoneDefinition>{ |
| 31 | %for zone in zone_group['zones']: |
| 32 | ZoneDefinition{ |
| 33 | ${zone['num']}, |
| 34 | ${zone['full_speed']}, |
| 35 | std::vector<FanDefinition>{ |
| 36 | %for fan in zone['fans']: |
| 37 | FanDefinition{ |
| 38 | "${fan['name']}", |
| 39 | std::vector<std::string>{ |
| 40 | %for sensor in fan['sensors']: |
| 41 | "${sensor}", |
| 42 | %endfor |
| 43 | } |
| 44 | }, |
| 45 | %endfor |
| 46 | }, |
| 47 | std::vector<SetSpeedEvent>{ |
| 48 | %for event in zone['events']: |
| 49 | SetSpeedEvent{ |
| 50 | Group{ |
| 51 | %for member in event['group']: |
| 52 | { |
| 53 | "${member['name']}", |
| 54 | {"${member['interface']}", |
| 55 | "${member['property']}"} |
| 56 | }, |
| 57 | %endfor |
| 58 | }, |
| 59 | make_action(action::${event['action']['name']}( |
| 60 | %for i, p in enumerate(event['action']['parameters']): |
| 61 | %if (i+1) != len(event['action']['parameters']): |
| 62 | static_cast<${p['type']}>(${p['value']}), |
| 63 | %else: |
| 64 | static_cast<${p['type']}>(${p['value']}) |
| 65 | %endif |
| 66 | %endfor |
| 67 | )), |
| 68 | std::vector<PropertyChange>{ |
| 69 | %for s in event['signal']: |
| 70 | PropertyChange{ |
| 71 | "interface='org.freedesktop.DBus.Properties'," |
| 72 | "member='PropertiesChanged'," |
| 73 | "type='signal'," |
| 74 | "path='${s['path']}'", |
| 75 | make_handler(propertySignal<${s['type']}>( |
| 76 | "${s['interface']}", |
| 77 | "${s['property']}", |
| 78 | handler::setProperty<${s['type']}>( |
| 79 | "${s['member']}", |
| 80 | "${s['property']}" |
| 81 | ) |
| 82 | )) |
| 83 | }, |
| 84 | %endfor |
| 85 | } |
| 86 | }, |
| 87 | %endfor |
| 88 | } |
| 89 | }, |
| 90 | %endfor |
| 91 | } |
Matt Spinler | 78498c9 | 2017-04-11 13:59:46 -0500 | [diff] [blame] | 92 | }, |
Matt Spinler | d08dbe2 | 2017-04-11 13:52:54 -0500 | [diff] [blame] | 93 | %endfor |
| 94 | }; |
| 95 | ''' |
| 96 | |
Matt Spinler | 78498c9 | 2017-04-11 13:59:46 -0500 | [diff] [blame] | 97 | |
Matthew Barth | d4d0f08 | 2017-05-16 13:51:10 -0500 | [diff] [blame] | 98 | def getEventsInZone(zone_num, events_data): |
| 99 | """ |
| 100 | Constructs the event entries defined for each zone using the events yaml |
| 101 | provided. |
| 102 | """ |
| 103 | events = [] |
Matthew Barth | ba102b3 | 2017-05-16 16:13:56 -0500 | [diff] [blame] | 104 | |
Matthew Barth | d4d0f08 | 2017-05-16 13:51:10 -0500 | [diff] [blame] | 105 | if 'events' in events_data: |
| 106 | for e in events_data['events']: |
| 107 | for z in e['zone_conditions']: |
| 108 | if zone_num not in z['zones']: |
| 109 | continue |
| 110 | |
| 111 | event = {} |
| 112 | # Add set speed event group |
| 113 | group = [] |
| 114 | groups = next(g for g in events_data['groups'] |
| 115 | if g['name'] == e['group']) |
| 116 | for member in groups['members']: |
| 117 | members = {} |
Matthew Barth | 7e527c1 | 2017-05-17 10:14:15 -0500 | [diff] [blame] | 118 | members['type'] = groups['type'] |
Matthew Barth | d4d0f08 | 2017-05-16 13:51:10 -0500 | [diff] [blame] | 119 | members['name'] = ("/xyz/openbmc_project/" + |
| 120 | groups['type'] + |
| 121 | member) |
| 122 | members['interface'] = e['interface'] |
| 123 | members['property'] = e['property']['name'] |
| 124 | group.append(members) |
| 125 | event['group'] = group |
Matthew Barth | ba102b3 | 2017-05-16 16:13:56 -0500 | [diff] [blame] | 126 | |
| 127 | # Add set speed action and function parameters |
| 128 | action = {} |
| 129 | actions = next(a for a in events_data['actions'] |
Matthew Barth | 9c72667 | 2017-05-18 13:44:46 -0500 | [diff] [blame] | 130 | if a['name'] == e['action']['name']) |
Matthew Barth | ba102b3 | 2017-05-16 16:13:56 -0500 | [diff] [blame] | 131 | action['name'] = actions['name'] |
| 132 | params = [] |
| 133 | for p in actions['parameters']: |
| 134 | param = {} |
| 135 | if type(e['action'][p]) is not dict: |
| 136 | if p == 'property': |
| 137 | param['value'] = str(e['action'][p]).lower() |
| 138 | param['type'] = str(e['property']['type']).lower() |
| 139 | else: |
| 140 | # Default type to 'size_t' when not given |
| 141 | param['value'] = str(e['action'][p]).lower() |
| 142 | param['type'] = 'size_t' |
| 143 | params.append(param) |
| 144 | else: |
| 145 | param['value'] = str(e['action'][p]['value']).lower() |
| 146 | param['type'] = str(e['action'][p]['type']).lower() |
| 147 | params.append(param) |
| 148 | action['parameters'] = params |
| 149 | event['action'] = action |
| 150 | |
Matthew Barth | 7e527c1 | 2017-05-17 10:14:15 -0500 | [diff] [blame] | 151 | # Add property change signal handler |
| 152 | signal = [] |
| 153 | for path in group: |
| 154 | signals = {} |
| 155 | signals['path'] = path['name'] |
| 156 | signals['interface'] = e['interface'] |
| 157 | signals['property'] = e['property']['name'] |
| 158 | signals['type'] = e['property']['type'] |
| 159 | signals['member'] = path['name'] |
| 160 | signal.append(signals) |
| 161 | event['signal'] = signal |
| 162 | |
Matthew Barth | d4d0f08 | 2017-05-16 13:51:10 -0500 | [diff] [blame] | 163 | events.append(event) |
| 164 | |
| 165 | return events |
| 166 | |
| 167 | |
Matt Spinler | 78498c9 | 2017-04-11 13:59:46 -0500 | [diff] [blame] | 168 | def getFansInZone(zone_num, profiles, fan_data): |
| 169 | """ |
| 170 | Parses the fan definition YAML files to find the fans |
| 171 | that match both the zone passed in and one of the |
| 172 | cooling profiles. |
| 173 | """ |
| 174 | |
| 175 | fans = [] |
| 176 | |
| 177 | for f in fan_data['fans']: |
| 178 | |
| 179 | if zone_num != f['cooling_zone']: |
| 180 | continue |
| 181 | |
| 182 | #'cooling_profile' is optional (use 'all' instead) |
| 183 | if f.get('cooling_profile') is None: |
| 184 | profile = "all" |
| 185 | else: |
| 186 | profile = f['cooling_profile'] |
| 187 | |
| 188 | if profile not in profiles: |
| 189 | continue |
| 190 | |
| 191 | fan = {} |
| 192 | fan['name'] = f['inventory'] |
| 193 | fan['sensors'] = f['sensors'] |
| 194 | fans.append(fan) |
| 195 | |
| 196 | return fans |
| 197 | |
| 198 | |
Matthew Barth | d4d0f08 | 2017-05-16 13:51:10 -0500 | [diff] [blame] | 199 | def buildZoneData(zone_data, fan_data, events_data): |
Matt Spinler | 78498c9 | 2017-04-11 13:59:46 -0500 | [diff] [blame] | 200 | """ |
| 201 | Combines the zone definition YAML and fan |
| 202 | definition YAML to create a data structure defining |
| 203 | the fan cooling zones. |
| 204 | """ |
| 205 | |
| 206 | zone_groups = [] |
| 207 | |
| 208 | for group in zone_data: |
| 209 | conditions = [] |
| 210 | for c in group['zone_conditions']: |
| 211 | conditions.append(c['name']) |
| 212 | |
| 213 | zone_group = {} |
| 214 | zone_group['conditions'] = conditions |
| 215 | |
| 216 | zones = [] |
| 217 | for z in group['zones']: |
| 218 | zone = {} |
| 219 | |
| 220 | #'zone' is required |
| 221 | if (not 'zone' in z) or (z['zone'] is None): |
| 222 | sys.exit("Missing fan zone number in " + zone_yaml) |
| 223 | |
| 224 | zone['num'] = z['zone'] |
| 225 | |
| 226 | zone['full_speed'] = z['full_speed'] |
| 227 | |
| 228 | #'cooling_profiles' is optional (use 'all' instead) |
| 229 | if (not 'cooling_profiles' in z) or \ |
| 230 | (z['cooling_profiles'] is None): |
| 231 | profiles = ["all"] |
| 232 | else: |
| 233 | profiles = z['cooling_profiles'] |
| 234 | |
| 235 | fans = getFansInZone(z['zone'], profiles, fan_data) |
Matthew Barth | d4d0f08 | 2017-05-16 13:51:10 -0500 | [diff] [blame] | 236 | events = getEventsInZone(z['zone'], events_data) |
Matt Spinler | 78498c9 | 2017-04-11 13:59:46 -0500 | [diff] [blame] | 237 | |
| 238 | if len(fans) == 0: |
| 239 | sys.exit("Didn't find any fans in zone " + str(zone['num'])) |
| 240 | |
| 241 | zone['fans'] = fans |
Matthew Barth | d4d0f08 | 2017-05-16 13:51:10 -0500 | [diff] [blame] | 242 | zone['events'] = events |
Matt Spinler | 78498c9 | 2017-04-11 13:59:46 -0500 | [diff] [blame] | 243 | zones.append(zone) |
| 244 | |
| 245 | zone_group['zones'] = zones |
| 246 | zone_groups.append(zone_group) |
| 247 | |
| 248 | return zone_groups |
| 249 | |
| 250 | |
Matt Spinler | d08dbe2 | 2017-04-11 13:52:54 -0500 | [diff] [blame] | 251 | if __name__ == '__main__': |
| 252 | parser = ArgumentParser( |
| 253 | description="Phosphor fan zone definition parser") |
| 254 | |
| 255 | parser.add_argument('-z', '--zone_yaml', dest='zone_yaml', |
| 256 | default="example/zones.yaml", |
| 257 | help='fan zone definitional yaml') |
| 258 | parser.add_argument('-f', '--fan_yaml', dest='fan_yaml', |
| 259 | default="example/fans.yaml", |
| 260 | help='fan definitional yaml') |
Matthew Barth | d4d0f08 | 2017-05-16 13:51:10 -0500 | [diff] [blame] | 261 | parser.add_argument('-e', '--events_yaml', dest='events_yaml', |
| 262 | help='events to set speeds yaml') |
Matt Spinler | d08dbe2 | 2017-04-11 13:52:54 -0500 | [diff] [blame] | 263 | parser.add_argument('-o', '--output_dir', dest='output_dir', |
| 264 | default=".", |
| 265 | help='output directory') |
| 266 | args = parser.parse_args() |
| 267 | |
| 268 | if not args.zone_yaml or not args.fan_yaml: |
| 269 | parser.print_usage() |
| 270 | sys.exit(-1) |
| 271 | |
| 272 | with open(args.zone_yaml, 'r') as zone_input: |
| 273 | zone_data = yaml.safe_load(zone_input) or {} |
| 274 | |
| 275 | with open(args.fan_yaml, 'r') as fan_input: |
| 276 | fan_data = yaml.safe_load(fan_input) or {} |
| 277 | |
Matthew Barth | d4d0f08 | 2017-05-16 13:51:10 -0500 | [diff] [blame] | 278 | events_data = {} |
| 279 | if args.events_yaml: |
| 280 | with open(args.events_yaml, 'r') as events_input: |
| 281 | events_data = yaml.safe_load(events_input) or {} |
| 282 | |
Matt Spinler | ee7f642 | 2017-05-09 11:03:14 -0500 | [diff] [blame] | 283 | zone_config = buildZoneData(zone_data.get('zone_configuration', {}), |
Matthew Barth | d4d0f08 | 2017-05-16 13:51:10 -0500 | [diff] [blame] | 284 | fan_data, events_data) |
Matt Spinler | ee7f642 | 2017-05-09 11:03:14 -0500 | [diff] [blame] | 285 | |
| 286 | manager_config = zone_data.get('manager_configuration', {}) |
| 287 | |
| 288 | if manager_config.get('power_on_delay') is None: |
| 289 | manager_config['power_on_delay'] = 0 |
Matt Spinler | d08dbe2 | 2017-04-11 13:52:54 -0500 | [diff] [blame] | 290 | |
| 291 | output_file = os.path.join(args.output_dir, "fan_zone_defs.cpp") |
| 292 | with open(output_file, 'w') as output: |
Matt Spinler | ee7f642 | 2017-05-09 11:03:14 -0500 | [diff] [blame] | 293 | output.write(Template(tmpl).render(zones=zone_config, |
| 294 | mgr_data=manager_config)) |