blob: 6386b6b01b5dbe311f4f7caeb70a9c9e8ab20aa3 [file] [log] [blame]
Zane Shelleya9b44342021-08-08 17:15:52 -05001#include <analyzer/ras-data/ras-data-parser.hpp>
2#include <util/data_file.hpp>
3#include <util/trace.hpp>
4
5#include <filesystem>
6#include <fstream>
7#include <string>
8
9namespace fs = std::filesystem;
10
11namespace analyzer
12{
13
14//------------------------------------------------------------------------------
15
16std::shared_ptr<Resolution>
Zane Shelley7698b302021-08-09 16:00:03 -050017 RasDataParser::getResolution(const libhei::Signature& i_signature)
Zane Shelleya9b44342021-08-08 17:15:52 -050018{
Zane Shelley7698b302021-08-09 16:00:03 -050019 const auto data = iv_dataFiles.at(i_signature.getChip().getType());
20
21 const auto action = parseSignature(data, i_signature);
22
23 return parseAction(data, action);
Zane Shelleya9b44342021-08-08 17:15:52 -050024}
25
26//------------------------------------------------------------------------------
27
28void RasDataParser::initDataFiles()
29{
30 iv_dataFiles.clear(); // initially empty
31
32 // Get the RAS data schema files from the package `schema` subdirectory.
33 fs::path schemaDir{PACKAGE_DIR "schema"};
Zane Shelleyee54c992021-08-08 17:46:48 -050034 auto schemaRegex = R"(ras-data-schema-v[0-9]{2}\.json)";
35 std::vector<fs::path> schemaPaths;
36 util::findFiles(schemaDir, schemaRegex, schemaPaths);
Zane Shelleya9b44342021-08-08 17:15:52 -050037
Zane Shelleyee54c992021-08-08 17:46:48 -050038 // Parse each of the schema files.
39 std::map<unsigned int, nlohmann::json> schemaFiles;
40 for (const auto& path : schemaPaths)
41 {
42 // Trace each data file for debug.
43 trace::inf("File found: path=%s", path.string().c_str());
Zane Shelleya9b44342021-08-08 17:15:52 -050044
Zane Shelleyee54c992021-08-08 17:46:48 -050045 // Open the file.
46 std::ifstream file{path};
47 assert(file.good()); // The file must be readable.
Zane Shelleya9b44342021-08-08 17:15:52 -050048
Zane Shelleyee54c992021-08-08 17:46:48 -050049 // Parse the JSON.
50 auto schema = nlohmann::json::parse(file);
Zane Shelleya9b44342021-08-08 17:15:52 -050051
Zane Shelleyee54c992021-08-08 17:46:48 -050052 // Get the schema version.
53 auto version = schema.at("version").get<unsigned int>();
54
55 // Keep track of the schemas.
56 auto ret = schemaFiles.emplace(version, schema);
57 assert(ret.second); // Should not have duplicate entries
58 }
59
60 // Get the RAS data files from the package `data` subdirectory.
Zane Shelleya9b44342021-08-08 17:15:52 -050061 fs::path dataDir{PACKAGE_DIR "ras-data"};
62 std::vector<fs::path> dataPaths;
63 util::findFiles(dataDir, R"(.*\.json)", dataPaths);
64
65 // Parse each of the data files.
66 for (const auto& path : dataPaths)
67 {
68 // Trace each data file for debug.
69 trace::inf("File found: path=%s", path.string().c_str());
70
71 // Open the file.
72 std::ifstream file{path};
73 assert(file.good()); // The file must be readable.
74
75 // Parse the JSON.
76 const auto data = nlohmann::json::parse(file);
77
Zane Shelleyee54c992021-08-08 17:46:48 -050078 // Get the data version.
79 auto version = data.at("version").get<unsigned int>();
80
81 // Get the schema for this file.
82 auto schema = schemaFiles.at(version);
83
Zane Shelleya9b44342021-08-08 17:15:52 -050084 // Validate the data against the schema.
85 assert(util::validateJson(schema, data));
86
87 // Get the chip model/EC level from the data. The value is currently
88 // stored as a string representation of the hex value. So it will have
89 // to be converted to an integer.
90 libhei::ChipType_t chipType =
91 std::stoul(data.at("model_ec").get<std::string>(), 0, 16);
92
93 // So far, so good. Add the entry.
94 auto ret = iv_dataFiles.emplace(chipType, data);
95 assert(ret.second); // Should not have duplicate entries
96 }
97}
98
99//------------------------------------------------------------------------------
100
Zane Shelley7698b302021-08-09 16:00:03 -0500101std::string RasDataParser::parseSignature(const nlohmann::json& i_data,
102 const libhei::Signature& i_signature)
103{
104 // Get the signature keys. All are hex (lower case) with no prefix.
105 char buf[5];
106 sprintf(buf, "%04x", i_signature.getId());
107 std::string id{buf};
108
109 sprintf(buf, "%02x", i_signature.getBit());
110 std::string bit{buf};
111
112 sprintf(buf, "%02x", i_signature.getInstance());
113 std::string inst{buf};
114
115 // Return the action.
116 return i_data.at("signatures").at(id).at(bit).at(inst).get<std::string>();
117}
118
119//------------------------------------------------------------------------------
120
121std::shared_ptr<Resolution>
122 RasDataParser::parseAction(const nlohmann::json& i_data,
123 const std::string& i_action)
124{
125 auto o_list = std::make_shared<ResolutionList>();
126
127 // This function will be called recursively and we want to prevent cyclic
128 // recursion.
129 static std::vector<std::string> stack;
130 assert(stack.end() == std::find(stack.begin(), stack.end(), i_action));
131 stack.push_back(i_action);
132
133 // Iterate the action list and apply the changes.
134 for (const auto& a : i_data.at("actions").at(i_action))
135 {
136 auto type = a.at("type").get<std::string>();
137
138 if ("action" == type)
139 {
140 auto name = a.at("name").get<std::string>();
141
142 o_list->push(parseAction(i_data, name));
143 }
144 else if ("callout_self" == type)
145 {
146 auto priority = a.at("priority").get<std::string>();
147 auto guard = a.at("guard").get<bool>();
148
149 // TODO
150 trace::inf("callout_self: priority=%s guard=%c", priority.c_str(),
151 guard ? 'T' : 'F');
152 }
153 else if ("callout_unit" == type)
154 {
155 auto name = a.at("name").get<std::string>();
156 auto priority = a.at("priority").get<std::string>();
157 auto guard = a.at("guard").get<bool>();
158
159 // TODO
160 trace::inf("callout_unit: name=%s priority=%s guard=%c",
161 name.c_str(), priority.c_str(), guard ? 'T' : 'F');
162 }
163 else if ("callout_connected" == type)
164 {
165 auto name = a.at("name").get<std::string>();
166 auto priority = a.at("priority").get<std::string>();
167 auto guard = a.at("guard").get<bool>();
168
169 // TODO
170 trace::inf("callout_connected: name=%s priority=%s guard=%c",
171 name.c_str(), priority.c_str(), guard ? 'T' : 'F');
172 }
173 else if ("callout_bus" == type)
174 {
175 auto name = a.at("name").get<std::string>();
176 // auto rx_priority = a.at("rx_priority").get<std::string>();
177 // auto tx_priority = a.at("tx_priority").get<std::string>();
178 auto guard = a.at("guard").get<bool>();
179
180 // TODO
181 trace::inf("callout_bus: name=%s guard=%c", name.c_str(),
182 guard ? 'T' : 'F');
183 }
184 else if ("callout_clock" == type)
185 {
186 auto position = a.at("position").get<unsigned int>();
187 auto priority = a.at("priority").get<std::string>();
188 auto guard = a.at("guard").get<bool>();
189
190 // TODO
191 trace::inf("callout_clock: position=%u priority=%s guard=%c",
192 position, priority.c_str(), guard ? 'T' : 'F');
193 }
194 else if ("callout_procedure" == type)
195 {
196 auto name = a.at("name").get<std::string>();
197 auto priority = a.at("priority").get<std::string>();
198
199 // clang-format off
200 static const std::map<std::string, ProcedureCallout::Type> m =
201 {
202 {"LEVEL2", ProcedureCallout::NEXTLVL},
203 };
204 // clang-format on
205
206 o_list->push(std::make_shared<ProcedureCalloutResolution>(
207 m.at(name), getPriority(priority)));
208 }
209 else if ("callout_part" == type)
210 {
211 auto name = a.at("name").get<std::string>();
212 auto priority = a.at("priority").get<std::string>();
213
214 // TODO
215 trace::inf("callout_part: name=%s priority=%s", name.c_str(),
216 priority.c_str());
217 }
218 else if ("plugin" == type)
219 {
220 auto name = a.at("name").get<std::string>();
221
222 // TODO
223 trace::inf("plugin: name=%s", name.c_str());
224 }
225 else
226 {
227 throw std::logic_error("Unsupported action type: " + type);
228 }
229 }
230
231 // Done with this action pop it off the stack.
232 stack.pop_back();
233
234 return o_list;
235}
236
237//------------------------------------------------------------------------------
238
239Callout::Priority RasDataParser::getPriority(const std::string& i_priority)
240{
241 // clang-format off
242 static const std::map<std::string, Callout::Priority> m =
243 {
244 {"HIGH", Callout::HIGH},
245 {"MED", Callout::MED},
246 {"MED_A", Callout::MED_A},
247 {"MED_B", Callout::MED_B},
248 {"MED_C", Callout::MED_C},
249 {"LOW", Callout::LOW},
250 };
251 // clang-format on
252
253 return m.at(i_priority);
254}
255
256//------------------------------------------------------------------------------
257
Zane Shelleya9b44342021-08-08 17:15:52 -0500258} // namespace analyzer