blob: d1315dc897965e80b6c8e70d53c3986012c84a97 [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>
17 RasDataParser::getResolution(const libhei::Signature&)
18{
19 // TODO: Default to level 2 support callout until fully implemented.
20 return std::make_shared<ProcedureCalloutResolution>(
21 ProcedureCallout::NEXTLVL, Callout::HIGH);
22}
23
24//------------------------------------------------------------------------------
25
26void RasDataParser::initDataFiles()
27{
28 iv_dataFiles.clear(); // initially empty
29
30 // Get the RAS data schema files from the package `schema` subdirectory.
31 fs::path schemaDir{PACKAGE_DIR "schema"};
Zane Shelleyee54c992021-08-08 17:46:48 -050032 auto schemaRegex = R"(ras-data-schema-v[0-9]{2}\.json)";
33 std::vector<fs::path> schemaPaths;
34 util::findFiles(schemaDir, schemaRegex, schemaPaths);
Zane Shelleya9b44342021-08-08 17:15:52 -050035
Zane Shelleyee54c992021-08-08 17:46:48 -050036 // Parse each of the schema files.
37 std::map<unsigned int, nlohmann::json> schemaFiles;
38 for (const auto& path : schemaPaths)
39 {
40 // Trace each data file for debug.
41 trace::inf("File found: path=%s", path.string().c_str());
Zane Shelleya9b44342021-08-08 17:15:52 -050042
Zane Shelleyee54c992021-08-08 17:46:48 -050043 // Open the file.
44 std::ifstream file{path};
45 assert(file.good()); // The file must be readable.
Zane Shelleya9b44342021-08-08 17:15:52 -050046
Zane Shelleyee54c992021-08-08 17:46:48 -050047 // Parse the JSON.
48 auto schema = nlohmann::json::parse(file);
Zane Shelleya9b44342021-08-08 17:15:52 -050049
Zane Shelleyee54c992021-08-08 17:46:48 -050050 // Get the schema version.
51 auto version = schema.at("version").get<unsigned int>();
52
53 // Keep track of the schemas.
54 auto ret = schemaFiles.emplace(version, schema);
55 assert(ret.second); // Should not have duplicate entries
56 }
57
58 // Get the RAS data files from the package `data` subdirectory.
Zane Shelleya9b44342021-08-08 17:15:52 -050059 fs::path dataDir{PACKAGE_DIR "ras-data"};
60 std::vector<fs::path> dataPaths;
61 util::findFiles(dataDir, R"(.*\.json)", dataPaths);
62
63 // Parse each of the data files.
64 for (const auto& path : dataPaths)
65 {
66 // Trace each data file for debug.
67 trace::inf("File found: path=%s", path.string().c_str());
68
69 // Open the file.
70 std::ifstream file{path};
71 assert(file.good()); // The file must be readable.
72
73 // Parse the JSON.
74 const auto data = nlohmann::json::parse(file);
75
Zane Shelleyee54c992021-08-08 17:46:48 -050076 // Get the data version.
77 auto version = data.at("version").get<unsigned int>();
78
79 // Get the schema for this file.
80 auto schema = schemaFiles.at(version);
81
Zane Shelleya9b44342021-08-08 17:15:52 -050082 // Validate the data against the schema.
83 assert(util::validateJson(schema, data));
84
85 // Get the chip model/EC level from the data. The value is currently
86 // stored as a string representation of the hex value. So it will have
87 // to be converted to an integer.
88 libhei::ChipType_t chipType =
89 std::stoul(data.at("model_ec").get<std::string>(), 0, 16);
90
91 // So far, so good. Add the entry.
92 auto ret = iv_dataFiles.emplace(chipType, data);
93 assert(ret.second); // Should not have duplicate entries
94 }
95}
96
97//------------------------------------------------------------------------------
98
99} // namespace analyzer