Vishwanatha Subbanna | 4f43f5e | 2020-03-09 03:56:57 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Vishwanatha Subbanna | ccbdf67 | 2017-01-23 14:39:06 +0530 | [diff] [blame] | 2 | import argparse |
Patrick Williams | 6d254ee | 2022-12-06 10:52:53 -0600 | [diff] [blame] | 3 | import os |
| 4 | |
| 5 | import yaml |
Vishwanatha Subbanna | 32bb6f8 | 2017-03-27 18:15:52 +0530 | [diff] [blame] | 6 | from inflection import underscore |
Vishwanatha Subbanna | b21fda7 | 2016-10-17 17:46:37 +0530 | [diff] [blame] | 7 | |
Alexander Hansen | 5a9f651 | 2024-07-30 15:20:49 +0200 | [diff] [blame] | 8 | |
Alexander Hansen | 638d148 | 2024-08-21 17:39:57 +0200 | [diff] [blame] | 9 | def 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 | |
| 22 | def check_led_priority(led_name, group, value, priority_dict): |
Alexander Hansen | 5a9f651 | 2024-07-30 15:20:49 +0200 | [diff] [blame] | 23 | |
| 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 Hansen | 638d148 | 2024-08-21 17:39:57 +0200 | [diff] [blame] | 28 | config_error( |
| 29 | ofile, |
| 30 | led_name, |
| 31 | group, |
| 32 | "Priority is NOT same across all groups", |
Alexander Hansen | 5a9f651 | 2024-07-30 15:20:49 +0200 | [diff] [blame] | 33 | ) |
| 34 | else: |
| 35 | priority_dict[led_name] = value |
| 36 | |
| 37 | return 0 |
| 38 | |
| 39 | |
Alexander Hansen | 638d148 | 2024-08-21 17:39:57 +0200 | [diff] [blame] | 40 | def led_action_literal(action): |
| 41 | if action == "": |
| 42 | return "std::nullopt" |
Alexander Hansen | 5a9f651 | 2024-07-30 15:20:49 +0200 | [diff] [blame] | 43 | |
Alexander Hansen | 638d148 | 2024-08-21 17:39:57 +0200 | [diff] [blame] | 44 | return "phosphor::led::Layout::Action::" + str(action) |
Alexander Hansen | 5a9f651 | 2024-07-30 15:20:49 +0200 | [diff] [blame] | 45 | |
Alexander Hansen | 5a9f651 | 2024-07-30 15:20:49 +0200 | [diff] [blame] | 46 | |
Alexander Hansen | 638d148 | 2024-08-21 17:39:57 +0200 | [diff] [blame] | 47 | def 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 Hansen | 5a9f651 | 2024-07-30 15:20:49 +0200 | [diff] [blame] | 72 | dutyOn = str(list_dict.get("DutyOn", 50)) |
| 73 | period = str(list_dict.get("Period", 0)) |
Alexander Hansen | 638d148 | 2024-08-21 17:39:57 +0200 | [diff] [blame] | 74 | priority = led_action_literal(led_priority) |
Alexander Hansen | 5a9f651 | 2024-07-30 15:20:49 +0200 | [diff] [blame] | 75 | |
| 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 | |
| 87 | def 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 |
Jason M. Bills | f2669fa | 2024-09-05 13:29:16 -0700 | [diff] [blame] | 93 | has_group_priority = led_dict and "Priority" in led_dict |
Alexander Hansen | 5a9f651 | 2024-07-30 15:20:49 +0200 | [diff] [blame] | 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 | |
Jason M. Bills | f2669fa | 2024-09-05 13:29:16 -0700 | [diff] [blame] | 111 | # Some LED groups could be empty |
| 112 | if not led_dict: |
| 113 | led_dict = {} |
| 114 | |
Alexander Hansen | 5a9f651 | 2024-07-30 15:20:49 +0200 | [diff] [blame] | 115 | for led_name, list_dict in list(led_dict.items()): |
| 116 | generate_file_single_led( |
Alexander Hansen | 638d148 | 2024-08-21 17:39:57 +0200 | [diff] [blame] | 117 | ifile, |
| 118 | led_name, |
| 119 | list_dict, |
| 120 | priority_dict, |
| 121 | ofile, |
| 122 | has_group_priority, |
| 123 | group, |
Alexander Hansen | 5a9f651 | 2024-07-30 15:20:49 +0200 | [diff] [blame] | 124 | ) |
| 125 | |
| 126 | ofile.write(" }}},\n") |
| 127 | |
| 128 | return 0 |
| 129 | |
| 130 | |
| 131 | def generate_file(ifile, ofile): |
| 132 | # Dictionary having [Name:Priority] |
| 133 | priority_dict = {} |
| 134 | |
| 135 | ofile.write("/* !!! WARNING: This is a GENERATED Code..") |
| 136 | ofile.write("Please do NOT Edit !!! */\n\n") |
| 137 | |
| 138 | ofile.write("static const phosphor::led::GroupMap") |
| 139 | ofile.write(" systemLedMap = {\n\n") |
| 140 | |
| 141 | for group in list(ifile.keys()): |
| 142 | generate_file_single_group(ifile, group, priority_dict, ofile) |
| 143 | ofile.write("};\n") |
| 144 | |
| 145 | return 0 |
| 146 | |
| 147 | |
Patrick Williams | fccfac9 | 2022-03-17 05:14:46 -0500 | [diff] [blame] | 148 | if __name__ == "__main__": |
Vishwanatha Subbanna | bb8fe0b | 2016-11-12 18:29:38 +0530 | [diff] [blame] | 149 | script_dir = os.path.dirname(os.path.realpath(__file__)) |
Vishwanatha Subbanna | ccbdf67 | 2017-01-23 14:39:06 +0530 | [diff] [blame] | 150 | parser = argparse.ArgumentParser() |
Brad Bishop | 9a6220c | 2017-02-08 13:21:31 -0500 | [diff] [blame] | 151 | parser.add_argument( |
Patrick Williams | fccfac9 | 2022-03-17 05:14:46 -0500 | [diff] [blame] | 152 | "-f", "--filename", default="led.yaml", help="Input File Name" |
| 153 | ) |
Brad Bishop | 9a6220c | 2017-02-08 13:21:31 -0500 | [diff] [blame] | 154 | parser.add_argument( |
Alexander Hansen | 30b60d4 | 2024-07-31 09:45:05 +0200 | [diff] [blame] | 155 | "-l", |
| 156 | "--output-filename", |
| 157 | dest="outputfilename", |
| 158 | default="led-gen.hpp", |
| 159 | help="Output File Name", |
| 160 | ) |
| 161 | parser.add_argument( |
Patrick Williams | fccfac9 | 2022-03-17 05:14:46 -0500 | [diff] [blame] | 162 | "-i", |
| 163 | "--input-dir", |
| 164 | dest="inputdir", |
Brad Bishop | 9a6220c | 2017-02-08 13:21:31 -0500 | [diff] [blame] | 165 | default=script_dir, |
Patrick Williams | fccfac9 | 2022-03-17 05:14:46 -0500 | [diff] [blame] | 166 | help="Input directory", |
| 167 | ) |
Brad Bishop | 513f58e | 2017-02-08 13:25:17 -0500 | [diff] [blame] | 168 | parser.add_argument( |
Patrick Williams | fccfac9 | 2022-03-17 05:14:46 -0500 | [diff] [blame] | 169 | "-o", |
| 170 | "--output-dir", |
| 171 | dest="outputdir", |
| 172 | default=".", |
| 173 | help="Output directory.", |
| 174 | ) |
Brad Bishop | 513f58e | 2017-02-08 13:25:17 -0500 | [diff] [blame] | 175 | |
Vishwanatha Subbanna | ccbdf67 | 2017-01-23 14:39:06 +0530 | [diff] [blame] | 176 | args = parser.parse_args() |
| 177 | |
| 178 | # Default to the one that is in the current. |
Brad Bishop | 9a6220c | 2017-02-08 13:21:31 -0500 | [diff] [blame] | 179 | yaml_dir = script_dir |
Patrick Williams | fccfac9 | 2022-03-17 05:14:46 -0500 | [diff] [blame] | 180 | yaml_file = os.path.join(yaml_dir, "led.yaml") |
Vishwanatha Subbanna | ccbdf67 | 2017-01-23 14:39:06 +0530 | [diff] [blame] | 181 | |
Brad Bishop | 513f58e | 2017-02-08 13:25:17 -0500 | [diff] [blame] | 182 | if args.inputdir: |
| 183 | yaml_dir = args.inputdir |
Vishwanatha Subbanna | ccbdf67 | 2017-01-23 14:39:06 +0530 | [diff] [blame] | 184 | |
| 185 | if args.filename: |
| 186 | yaml_file = os.path.join(yaml_dir, args.filename) |
| 187 | |
Patrick Williams | fccfac9 | 2022-03-17 05:14:46 -0500 | [diff] [blame] | 188 | with open(yaml_file, "r") as f: |
Vishwanatha Subbanna | b21fda7 | 2016-10-17 17:46:37 +0530 | [diff] [blame] | 189 | ifile = yaml.safe_load(f) |
| 190 | |
Alexander Hansen | 30b60d4 | 2024-07-31 09:45:05 +0200 | [diff] [blame] | 191 | with open(os.path.join(args.outputdir, args.outputfilename), "w") as ofile: |
Alexander Hansen | 5a9f651 | 2024-07-30 15:20:49 +0200 | [diff] [blame] | 192 | generate_file(ifile, ofile) |