blob: 48b0428a5351641e65b0192f45ab6cfdaf260b6c [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"};
32 auto schemaRegex = R"(ras-data-schema\.json)";
33 std::vector<fs::path> schmemaPaths;
34 util::findFiles(schemaDir, schemaRegex, schmemaPaths);
35 assert(1 == schmemaPaths.size()); // Should be one, and only one, file.
36
37 // Trace the file for debug.
38 trace::inf("File found: path=%s", schmemaPaths.front().string().c_str());
39
40 // Open the file.
41 std::ifstream schemaFile{schmemaPaths.front()};
42 assert(schemaFile.good()); // The file must be readable.
43
44 // Parse the JSON.
45 auto schema = nlohmann::json::parse(schemaFile);
46
47 // Get the RAS data files from the package `ras-data` subdirectory.
48 fs::path dataDir{PACKAGE_DIR "ras-data"};
49 std::vector<fs::path> dataPaths;
50 util::findFiles(dataDir, R"(.*\.json)", dataPaths);
51
52 // Parse each of the data files.
53 for (const auto& path : dataPaths)
54 {
55 // Trace each data file for debug.
56 trace::inf("File found: path=%s", path.string().c_str());
57
58 // Open the file.
59 std::ifstream file{path};
60 assert(file.good()); // The file must be readable.
61
62 // Parse the JSON.
63 const auto data = nlohmann::json::parse(file);
64
65 // Validate the data against the schema.
66 assert(util::validateJson(schema, data));
67
68 // Get the chip model/EC level from the data. The value is currently
69 // stored as a string representation of the hex value. So it will have
70 // to be converted to an integer.
71 libhei::ChipType_t chipType =
72 std::stoul(data.at("model_ec").get<std::string>(), 0, 16);
73
74 // So far, so good. Add the entry.
75 auto ret = iv_dataFiles.emplace(chipType, data);
76 assert(ret.second); // Should not have duplicate entries
77 }
78}
79
80//------------------------------------------------------------------------------
81
82} // namespace analyzer