Zane Shelley | a9b4434 | 2021-08-08 17:15:52 -0500 | [diff] [blame] | 1 | #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 | |
| 9 | namespace fs = std::filesystem; |
| 10 | |
| 11 | namespace analyzer |
| 12 | { |
| 13 | |
| 14 | //------------------------------------------------------------------------------ |
| 15 | |
| 16 | std::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 | |
| 26 | void 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 Shelley | ee54c99 | 2021-08-08 17:46:48 -0500 | [diff] [blame] | 32 | auto schemaRegex = R"(ras-data-schema-v[0-9]{2}\.json)"; |
| 33 | std::vector<fs::path> schemaPaths; |
| 34 | util::findFiles(schemaDir, schemaRegex, schemaPaths); |
Zane Shelley | a9b4434 | 2021-08-08 17:15:52 -0500 | [diff] [blame] | 35 | |
Zane Shelley | ee54c99 | 2021-08-08 17:46:48 -0500 | [diff] [blame] | 36 | // 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 Shelley | a9b4434 | 2021-08-08 17:15:52 -0500 | [diff] [blame] | 42 | |
Zane Shelley | ee54c99 | 2021-08-08 17:46:48 -0500 | [diff] [blame] | 43 | // Open the file. |
| 44 | std::ifstream file{path}; |
| 45 | assert(file.good()); // The file must be readable. |
Zane Shelley | a9b4434 | 2021-08-08 17:15:52 -0500 | [diff] [blame] | 46 | |
Zane Shelley | ee54c99 | 2021-08-08 17:46:48 -0500 | [diff] [blame] | 47 | // Parse the JSON. |
| 48 | auto schema = nlohmann::json::parse(file); |
Zane Shelley | a9b4434 | 2021-08-08 17:15:52 -0500 | [diff] [blame] | 49 | |
Zane Shelley | ee54c99 | 2021-08-08 17:46:48 -0500 | [diff] [blame] | 50 | // 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 Shelley | a9b4434 | 2021-08-08 17:15:52 -0500 | [diff] [blame] | 59 | 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 Shelley | ee54c99 | 2021-08-08 17:46:48 -0500 | [diff] [blame] | 76 | // 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 Shelley | a9b4434 | 2021-08-08 17:15:52 -0500 | [diff] [blame] | 82 | // 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 |