blob: f03d3c08899f8a9f69171216719c4f17893abc46 [file] [log] [blame]
Vishwanatha Subbanna4f43f5e2020-03-09 03:56:57 -05001#!/usr/bin/env python3
Vishwanatha Subbannab21fda72016-10-17 17:46:37 +05302import yaml
Vishwanatha Subbannabb8fe0b2016-11-12 18:29:38 +05303import os
Vishwanatha Subbannaccbdf672017-01-23 14:39:06 +05304import argparse
Vishwanatha Subbanna32bb6f82017-03-27 18:15:52 +05305from inflection import underscore
Vishwanatha Subbannab21fda72016-10-17 17:46:37 +05306
7if __name__ == '__main__':
Vishwanatha Subbannabb8fe0b2016-11-12 18:29:38 +05308 script_dir = os.path.dirname(os.path.realpath(__file__))
Vishwanatha Subbannaccbdf672017-01-23 14:39:06 +05309 parser = argparse.ArgumentParser()
Brad Bishop9a6220c2017-02-08 13:21:31 -050010 parser.add_argument(
11 "-f", "--filename",
12 default='led.yaml',
13 help="Input File Name")
14 parser.add_argument(
Brad Bishop513f58e2017-02-08 13:25:17 -050015 "-i", "--input-dir",
16 dest='inputdir',
Brad Bishop9a6220c2017-02-08 13:21:31 -050017 default=script_dir,
18 help="Input directory")
Brad Bishop513f58e2017-02-08 13:25:17 -050019 parser.add_argument(
20 '-o', '--output-dir',
21 dest='outputdir',
22 default='.',
23 help='Output directory.')
24
Vishwanatha Subbannaccbdf672017-01-23 14:39:06 +053025 args = parser.parse_args()
26
27 # Default to the one that is in the current.
Brad Bishop9a6220c2017-02-08 13:21:31 -050028 yaml_dir = script_dir
Vishwanatha Subbannaccbdf672017-01-23 14:39:06 +053029 yaml_file = os.path.join(yaml_dir, 'led.yaml')
30
Brad Bishop513f58e2017-02-08 13:25:17 -050031 if args.inputdir:
32 yaml_dir = args.inputdir
Vishwanatha Subbannaccbdf672017-01-23 14:39:06 +053033
34 if args.filename:
35 yaml_file = os.path.join(yaml_dir, args.filename)
36
37 with open(yaml_file, 'r') as f:
Vishwanatha Subbannab21fda72016-10-17 17:46:37 +053038 ifile = yaml.safe_load(f)
39
Vishwanatha Subbanna1da8eb72017-06-15 13:46:05 +053040 # Dictionary having [Name:Priority]
41 priority_dict = {}
42
Brad Bishop513f58e2017-02-08 13:25:17 -050043 with open(os.path.join(args.outputdir, 'led-gen.hpp'), 'w') as ofile:
Vishwanatha Subbannab21fda72016-10-17 17:46:37 +053044 ofile.write('/* !!! WARNING: This is a GENERATED Code..')
45 ofile.write('Please do NOT Edit !!! */\n\n')
46
Patrick Williamsf2044032022-03-17 05:12:30 -050047 ofile.write('static const std::unordered_map<std::string,')
Vishwanatha Subbannaed490732016-12-20 15:59:29 +053048 ofile.write(' std::set<phosphor::led::Layout::LedAction>>')
49 ofile.write(' systemLedMap = {\n\n')
Vishwanatha Subbannae0cf8fe2020-03-03 02:38:21 -060050 for group in list(ifile.keys()):
Patrick Williamsf2044032022-03-17 05:12:30 -050051 # This section generates an std::unordered_map of LedGroupNames to
52 # std::set of LEDs containing the name and properties
Vishwanatha Subbanna1da8eb72017-06-15 13:46:05 +053053 led_dict = ifile[group]
Brad Bishop9a6220c2017-02-08 13:21:31 -050054 ofile.write(
55 ' {\"' +
Vishwanatha Subbannaa1c7f6a2017-02-02 14:51:42 +053056 "/xyz/openbmc_project/led/groups/" +
Vishwanatha Subbanna32bb6f82017-03-27 18:15:52 +053057 underscore(group) +
Brad Bishop9a6220c2017-02-08 13:21:31 -050058 '\",{\n')
Vishwanatha Subbannab21fda72016-10-17 17:46:37 +053059
Vishwanatha Subbannad53de6f2017-03-30 11:21:37 +053060 # Some LED groups could be empty
Vishwanatha Subbanna1da8eb72017-06-15 13:46:05 +053061 if not led_dict:
Vishwanatha Subbannad53de6f2017-03-30 11:21:37 +053062 ofile.write(' }},\n')
63 continue
64
Vishwanatha Subbannae0cf8fe2020-03-03 02:38:21 -060065 for led_name, list_dict in list(led_dict.items()):
Vishwanatha Subbanna1da8eb72017-06-15 13:46:05 +053066 value = list_dict.get('Priority')
67 if led_name in priority_dict:
68 if value != priority_dict[led_name]:
69 # Priority for a particular LED needs to stay SAME
70 # across all groups
71 ofile.close()
72 os.remove(ofile.name)
73 raise ValueError("Priority for [" +
74 led_name +
75 "] is NOT same across all groups")
76 else:
77 priority_dict[led_name] = value
78
79 ofile.write(' {\"' + underscore(led_name) + '\",')
Patrick Williamsed80e882022-03-17 05:03:51 -050080 ofile.write('phosphor::led::Layout::Action::' +
Patrick Williamsd27c68f2017-05-31 17:12:37 -050081 str(list_dict.get('Action', 'Off')) + ',')
Vishwanatha Subbanna4b000d82017-05-03 18:44:16 +053082 ofile.write(str(list_dict.get('DutyOn', 50)) + ',')
Patrick Williamsd27c68f2017-05-31 17:12:37 -050083 ofile.write(str(list_dict.get('Period', 0)) + ',')
Vishwanatha Subbanna4b000d82017-05-03 18:44:16 +053084 priority = str(list_dict.get('Priority', 'Blink'))
Patrick Williamsed80e882022-03-17 05:03:51 -050085 ofile.write('phosphor::led::Layout::Action::' + priority + ',')
Vishwanatha Subbanna8a50a502017-01-20 18:42:21 +053086 ofile.write('},\n')
Vishwanatha Subbannab21fda72016-10-17 17:46:37 +053087 ofile.write(' }},\n')
88 ofile.write('};\n')