blob: 1464ba7758924b9c341a69c27ae1dfa470cc56f6 [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
Patrick Williamsfccfac92022-03-17 05:14:46 -05007if __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(
Patrick Williamsfccfac92022-03-17 05:14:46 -050011 "-f", "--filename", default="led.yaml", help="Input File Name"
12 )
Brad Bishop9a6220c2017-02-08 13:21:31 -050013 parser.add_argument(
Patrick Williamsfccfac92022-03-17 05:14:46 -050014 "-i",
15 "--input-dir",
16 dest="inputdir",
Brad Bishop9a6220c2017-02-08 13:21:31 -050017 default=script_dir,
Patrick Williamsfccfac92022-03-17 05:14:46 -050018 help="Input directory",
19 )
Brad Bishop513f58e2017-02-08 13:25:17 -050020 parser.add_argument(
Patrick Williamsfccfac92022-03-17 05:14:46 -050021 "-o",
22 "--output-dir",
23 dest="outputdir",
24 default=".",
25 help="Output directory.",
26 )
Brad Bishop513f58e2017-02-08 13:25:17 -050027
Vishwanatha Subbannaccbdf672017-01-23 14:39:06 +053028 args = parser.parse_args()
29
30 # Default to the one that is in the current.
Brad Bishop9a6220c2017-02-08 13:21:31 -050031 yaml_dir = script_dir
Patrick Williamsfccfac92022-03-17 05:14:46 -050032 yaml_file = os.path.join(yaml_dir, "led.yaml")
Vishwanatha Subbannaccbdf672017-01-23 14:39:06 +053033
Brad Bishop513f58e2017-02-08 13:25:17 -050034 if args.inputdir:
35 yaml_dir = args.inputdir
Vishwanatha Subbannaccbdf672017-01-23 14:39:06 +053036
37 if args.filename:
38 yaml_file = os.path.join(yaml_dir, args.filename)
39
Patrick Williamsfccfac92022-03-17 05:14:46 -050040 with open(yaml_file, "r") as f:
Vishwanatha Subbannab21fda72016-10-17 17:46:37 +053041 ifile = yaml.safe_load(f)
42
Vishwanatha Subbanna1da8eb72017-06-15 13:46:05 +053043 # Dictionary having [Name:Priority]
44 priority_dict = {}
45
Patrick Williamsfccfac92022-03-17 05:14:46 -050046 with open(os.path.join(args.outputdir, "led-gen.hpp"), "w") as ofile:
47 ofile.write("/* !!! WARNING: This is a GENERATED Code..")
48 ofile.write("Please do NOT Edit !!! */\n\n")
Vishwanatha Subbannab21fda72016-10-17 17:46:37 +053049
Patrick Williamsfccfac92022-03-17 05:14:46 -050050 ofile.write("static const std::unordered_map<std::string,")
51 ofile.write(" std::set<phosphor::led::Layout::LedAction>>")
52 ofile.write(" systemLedMap = {\n\n")
Vishwanatha Subbannae0cf8fe2020-03-03 02:38:21 -060053 for group in list(ifile.keys()):
Patrick Williamsf2044032022-03-17 05:12:30 -050054 # This section generates an std::unordered_map of LedGroupNames to
55 # std::set of LEDs containing the name and properties
Vishwanatha Subbanna1da8eb72017-06-15 13:46:05 +053056 led_dict = ifile[group]
Brad Bishop9a6220c2017-02-08 13:21:31 -050057 ofile.write(
Patrick Williamsfccfac92022-03-17 05:14:46 -050058 ' {"'
59 + "/xyz/openbmc_project/led/groups/"
60 + underscore(group)
61 + '",{\n'
62 )
Vishwanatha Subbannab21fda72016-10-17 17:46:37 +053063
Vishwanatha Subbannad53de6f2017-03-30 11:21:37 +053064 # Some LED groups could be empty
Vishwanatha Subbanna1da8eb72017-06-15 13:46:05 +053065 if not led_dict:
Patrick Williamsfccfac92022-03-17 05:14:46 -050066 ofile.write(" }},\n")
Vishwanatha Subbannad53de6f2017-03-30 11:21:37 +053067 continue
68
Vishwanatha Subbannae0cf8fe2020-03-03 02:38:21 -060069 for led_name, list_dict in list(led_dict.items()):
Patrick Williamsfccfac92022-03-17 05:14:46 -050070 value = list_dict.get("Priority")
Vishwanatha Subbanna1da8eb72017-06-15 13:46:05 +053071 if led_name in priority_dict:
72 if value != priority_dict[led_name]:
73 # Priority for a particular LED needs to stay SAME
74 # across all groups
75 ofile.close()
76 os.remove(ofile.name)
Patrick Williamsfccfac92022-03-17 05:14:46 -050077 raise ValueError(
78 "Priority for ["
79 + led_name
80 + "] is NOT same across all groups"
81 )
Vishwanatha Subbanna1da8eb72017-06-15 13:46:05 +053082 else:
83 priority_dict[led_name] = value
84
Patrick Williamsfccfac92022-03-17 05:14:46 -050085 ofile.write(' {"' + underscore(led_name) + '",')
86 ofile.write(
87 "phosphor::led::Layout::Action::"
88 + str(list_dict.get("Action", "Off"))
89 + ","
90 )
91 ofile.write(str(list_dict.get("DutyOn", 50)) + ",")
92 ofile.write(str(list_dict.get("Period", 0)) + ",")
93 priority = str(list_dict.get("Priority", "Blink"))
94 ofile.write("phosphor::led::Layout::Action::" + priority + ",")
95 ofile.write("},\n")
96 ofile.write(" }},\n")
97 ofile.write("};\n")