blob: 5724e6dea869ff793ef2d39f318aee5cd4cc7a33 [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
Alexander Hansen638d1482024-08-21 17:39:57 +02009def config_error(ofile, led_name, group_name, message):
10 ofile.close()
11 os.remove(ofile.name)
12 raise ValueError(
13 "Invalid Configuration for LED ["
14 + led_name
15 + "] in Group ["
16 + group_name
17 + "]: "
18 + message
19 )
20
21
22def check_led_priority(led_name, group, value, priority_dict):
Alexander Hansen5a9f6512024-07-30 15:20:49 +020023
24 if led_name in priority_dict:
25 if value != priority_dict[led_name]:
26 # Priority for a particular LED needs to stay SAME
27 # across all groups
Alexander Hansen638d1482024-08-21 17:39:57 +020028 config_error(
29 ofile,
30 led_name,
31 group,
32 "Priority is NOT same across all groups",
Alexander Hansen5a9f6512024-07-30 15:20:49 +020033 )
34 else:
35 priority_dict[led_name] = value
36
37 return 0
38
39
Alexander Hansen638d1482024-08-21 17:39:57 +020040def led_action_literal(action):
41 if action == "":
42 return "std::nullopt"
Alexander Hansen5a9f6512024-07-30 15:20:49 +020043
Alexander Hansen638d1482024-08-21 17:39:57 +020044 return "phosphor::led::Layout::Action::" + str(action)
Alexander Hansen5a9f6512024-07-30 15:20:49 +020045
Alexander Hansen5a9f6512024-07-30 15:20:49 +020046
Alexander Hansen638d1482024-08-21 17:39:57 +020047def generate_file_single_led(
48 ifile, led_name, list_dict, priority_dict, ofile, has_group_priority, group
49):
50
51 has_led_priority = "Priority" in list_dict
52
53 if has_group_priority and has_led_priority:
54 config_error(
55 ofile,
56 led_name,
57 group,
58 "cannot mix group priority with led priority",
59 )
60
61 if (not has_group_priority) and (not has_led_priority):
62 config_error(
63 ofile, led_name, group, "no group priority or led priority defined"
64 )
65
66 led_priority = list_dict.get("Priority", "")
67
68 if has_led_priority:
69 check_led_priority(led_name, group, led_priority, priority_dict)
70
71 action = led_action_literal(list_dict.get("Action", "Off"))
Alexander Hansen5a9f6512024-07-30 15:20:49 +020072 dutyOn = str(list_dict.get("DutyOn", 50))
73 period = str(list_dict.get("Period", 0))
Alexander Hansen638d1482024-08-21 17:39:57 +020074 priority = led_action_literal(led_priority)
Alexander Hansen5a9f6512024-07-30 15:20:49 +020075
76 ofile.write(' {"' + underscore(led_name) + '",')
77 ofile.write(action + ",")
78 ofile.write(dutyOn + ",")
79 ofile.write(period + ",")
80 ofile.write(priority + ",")
81
82 ofile.write("},\n")
83
84 return 0
85
86
87def generate_file_single_group(ifile, group, priority_dict, ofile):
88 # This section generates an std::unordered_map of LedGroupNames to
89 # std::set of LEDs containing the name and properties
90 led_dict = ifile[group]
91
92 group_priority = 0
93 has_group_priority = "Priority" in led_dict
94
95 if has_group_priority:
96 group_priority = led_dict["Priority"]
97 # we do not want to enumerate this as a led group
98 del led_dict["Priority"]
99
100 ofile.write(
101 ' {"'
102 + "/xyz/openbmc_project/led/groups/"
103 + underscore(group)
104 + '"'
105 + ",{ "
106 + str(group_priority)
107 + ",\n"
108 + "{\n"
109 )
110
111 for led_name, list_dict in list(led_dict.items()):
112 generate_file_single_led(
Alexander Hansen638d1482024-08-21 17:39:57 +0200113 ifile,
114 led_name,
115 list_dict,
116 priority_dict,
117 ofile,
118 has_group_priority,
119 group,
Alexander Hansen5a9f6512024-07-30 15:20:49 +0200120 )
121
122 ofile.write(" }}},\n")
123
124 return 0
125
126
127def generate_file(ifile, ofile):
128 # Dictionary having [Name:Priority]
129 priority_dict = {}
130
131 ofile.write("/* !!! WARNING: This is a GENERATED Code..")
132 ofile.write("Please do NOT Edit !!! */\n\n")
133
134 ofile.write("static const phosphor::led::GroupMap")
135 ofile.write(" systemLedMap = {\n\n")
136
137 for group in list(ifile.keys()):
138 generate_file_single_group(ifile, group, priority_dict, ofile)
139 ofile.write("};\n")
140
141 return 0
142
143
Patrick Williamsfccfac92022-03-17 05:14:46 -0500144if __name__ == "__main__":
Vishwanatha Subbannabb8fe0b2016-11-12 18:29:38 +0530145 script_dir = os.path.dirname(os.path.realpath(__file__))
Vishwanatha Subbannaccbdf672017-01-23 14:39:06 +0530146 parser = argparse.ArgumentParser()
Brad Bishop9a6220c2017-02-08 13:21:31 -0500147 parser.add_argument(
Patrick Williamsfccfac92022-03-17 05:14:46 -0500148 "-f", "--filename", default="led.yaml", help="Input File Name"
149 )
Brad Bishop9a6220c2017-02-08 13:21:31 -0500150 parser.add_argument(
Alexander Hansen30b60d42024-07-31 09:45:05 +0200151 "-l",
152 "--output-filename",
153 dest="outputfilename",
154 default="led-gen.hpp",
155 help="Output File Name",
156 )
157 parser.add_argument(
Patrick Williamsfccfac92022-03-17 05:14:46 -0500158 "-i",
159 "--input-dir",
160 dest="inputdir",
Brad Bishop9a6220c2017-02-08 13:21:31 -0500161 default=script_dir,
Patrick Williamsfccfac92022-03-17 05:14:46 -0500162 help="Input directory",
163 )
Brad Bishop513f58e2017-02-08 13:25:17 -0500164 parser.add_argument(
Patrick Williamsfccfac92022-03-17 05:14:46 -0500165 "-o",
166 "--output-dir",
167 dest="outputdir",
168 default=".",
169 help="Output directory.",
170 )
Brad Bishop513f58e2017-02-08 13:25:17 -0500171
Vishwanatha Subbannaccbdf672017-01-23 14:39:06 +0530172 args = parser.parse_args()
173
174 # Default to the one that is in the current.
Brad Bishop9a6220c2017-02-08 13:21:31 -0500175 yaml_dir = script_dir
Patrick Williamsfccfac92022-03-17 05:14:46 -0500176 yaml_file = os.path.join(yaml_dir, "led.yaml")
Vishwanatha Subbannaccbdf672017-01-23 14:39:06 +0530177
Brad Bishop513f58e2017-02-08 13:25:17 -0500178 if args.inputdir:
179 yaml_dir = args.inputdir
Vishwanatha Subbannaccbdf672017-01-23 14:39:06 +0530180
181 if args.filename:
182 yaml_file = os.path.join(yaml_dir, args.filename)
183
Patrick Williamsfccfac92022-03-17 05:14:46 -0500184 with open(yaml_file, "r") as f:
Vishwanatha Subbannab21fda72016-10-17 17:46:37 +0530185 ifile = yaml.safe_load(f)
186
Alexander Hansen30b60d42024-07-31 09:45:05 +0200187 with open(os.path.join(args.outputdir, args.outputfilename), "w") as ofile:
Alexander Hansen5a9f6512024-07-30 15:20:49 +0200188 generate_file(ifile, ofile)