blob: eac764c2923164181e0374029cb272ff14a6e58e [file] [log] [blame]
Vishwanatha Subbanna4f43f5e2020-03-09 03:56:57 -05001#!/usr/bin/env python3
Vishwanatha Subbannaccbdf672017-01-23 14:39:06 +05302import argparse
Patrick Williams6d254ee2022-12-06 10:52:53 -06003import os
4
5import yaml
Vishwanatha Subbanna32bb6f82017-03-27 18:15:52 +05306from inflection import underscore
Vishwanatha Subbannab21fda72016-10-17 17:46:37 +05307
Alexander Hansen5a9f6512024-07-30 15:20:49 +02008
9def check_led_priority(led_name, value, priority_dict):
10
11 if led_name in priority_dict:
12 if value != priority_dict[led_name]:
13 # Priority for a particular LED needs to stay SAME
14 # across all groups
15 ofile.close()
16 os.remove(ofile.name)
17 raise ValueError(
18 "Priority for [" + led_name + "] is NOT same across all groups"
19 )
20 else:
21 priority_dict[led_name] = value
22
23 return 0
24
25
26def generate_file_single_led(ifile, led_name, list_dict, priority_dict, ofile):
27
28 value = list_dict.get("Priority")
29
30 check_led_priority(led_name, value, priority_dict)
31
32 action = "phosphor::led::Layout::Action::" + str(
33 list_dict.get("Action", "Off")
34 )
35 dutyOn = str(list_dict.get("DutyOn", 50))
36 period = str(list_dict.get("Period", 0))
37 priority = "phosphor::led::Layout::Action::" + str(
38 list_dict.get("Priority", "Blink")
39 )
40
41 ofile.write(' {"' + underscore(led_name) + '",')
42 ofile.write(action + ",")
43 ofile.write(dutyOn + ",")
44 ofile.write(period + ",")
45 ofile.write(priority + ",")
46
47 ofile.write("},\n")
48
49 return 0
50
51
52def generate_file_single_group(ifile, group, priority_dict, ofile):
53 # This section generates an std::unordered_map of LedGroupNames to
54 # std::set of LEDs containing the name and properties
55 led_dict = ifile[group]
56
57 group_priority = 0
58 has_group_priority = "Priority" in led_dict
59
60 if has_group_priority:
61 group_priority = led_dict["Priority"]
62 # we do not want to enumerate this as a led group
63 del led_dict["Priority"]
64
65 ofile.write(
66 ' {"'
67 + "/xyz/openbmc_project/led/groups/"
68 + underscore(group)
69 + '"'
70 + ",{ "
71 + str(group_priority)
72 + ",\n"
73 + "{\n"
74 )
75
76 for led_name, list_dict in list(led_dict.items()):
77 generate_file_single_led(
78 ifile, led_name, list_dict, priority_dict, ofile
79 )
80
81 ofile.write(" }}},\n")
82
83 return 0
84
85
86def generate_file(ifile, ofile):
87 # Dictionary having [Name:Priority]
88 priority_dict = {}
89
90 ofile.write("/* !!! WARNING: This is a GENERATED Code..")
91 ofile.write("Please do NOT Edit !!! */\n\n")
92
93 ofile.write("static const phosphor::led::GroupMap")
94 ofile.write(" systemLedMap = {\n\n")
95
96 for group in list(ifile.keys()):
97 generate_file_single_group(ifile, group, priority_dict, ofile)
98 ofile.write("};\n")
99
100 return 0
101
102
Patrick Williamsfccfac92022-03-17 05:14:46 -0500103if __name__ == "__main__":
Vishwanatha Subbannabb8fe0b2016-11-12 18:29:38 +0530104 script_dir = os.path.dirname(os.path.realpath(__file__))
Vishwanatha Subbannaccbdf672017-01-23 14:39:06 +0530105 parser = argparse.ArgumentParser()
Brad Bishop9a6220c2017-02-08 13:21:31 -0500106 parser.add_argument(
Patrick Williamsfccfac92022-03-17 05:14:46 -0500107 "-f", "--filename", default="led.yaml", help="Input File Name"
108 )
Brad Bishop9a6220c2017-02-08 13:21:31 -0500109 parser.add_argument(
Alexander Hansen30b60d42024-07-31 09:45:05 +0200110 "-l",
111 "--output-filename",
112 dest="outputfilename",
113 default="led-gen.hpp",
114 help="Output File Name",
115 )
116 parser.add_argument(
Patrick Williamsfccfac92022-03-17 05:14:46 -0500117 "-i",
118 "--input-dir",
119 dest="inputdir",
Brad Bishop9a6220c2017-02-08 13:21:31 -0500120 default=script_dir,
Patrick Williamsfccfac92022-03-17 05:14:46 -0500121 help="Input directory",
122 )
Brad Bishop513f58e2017-02-08 13:25:17 -0500123 parser.add_argument(
Patrick Williamsfccfac92022-03-17 05:14:46 -0500124 "-o",
125 "--output-dir",
126 dest="outputdir",
127 default=".",
128 help="Output directory.",
129 )
Brad Bishop513f58e2017-02-08 13:25:17 -0500130
Vishwanatha Subbannaccbdf672017-01-23 14:39:06 +0530131 args = parser.parse_args()
132
133 # Default to the one that is in the current.
Brad Bishop9a6220c2017-02-08 13:21:31 -0500134 yaml_dir = script_dir
Patrick Williamsfccfac92022-03-17 05:14:46 -0500135 yaml_file = os.path.join(yaml_dir, "led.yaml")
Vishwanatha Subbannaccbdf672017-01-23 14:39:06 +0530136
Brad Bishop513f58e2017-02-08 13:25:17 -0500137 if args.inputdir:
138 yaml_dir = args.inputdir
Vishwanatha Subbannaccbdf672017-01-23 14:39:06 +0530139
140 if args.filename:
141 yaml_file = os.path.join(yaml_dir, args.filename)
142
Patrick Williamsfccfac92022-03-17 05:14:46 -0500143 with open(yaml_file, "r") as f:
Vishwanatha Subbannab21fda72016-10-17 17:46:37 +0530144 ifile = yaml.safe_load(f)
145
Alexander Hansen30b60d42024-07-31 09:45:05 +0200146 with open(os.path.join(args.outputdir, args.outputfilename), "w") as ofile:
Alexander Hansen5a9f6512024-07-30 15:20:49 +0200147 generate_file(ifile, ofile)