Branch data Line data Source code
1 : : #include "manifest_parser.hpp"
2 : : #include "version_helper.hpp"
3 : : #include "cJSON.h"
4 : :
5 : 3 : std::optional<OtaManifest> ManifestParser::parse(const std::string& json_content) const {
6 : 3 : cJSON* root = cJSON_Parse(json_content.c_str());
7 [ + + ]: 3 : if (root == nullptr) {
8 : 1 : return std::nullopt;
9 : : }
10 : :
11 : 2 : OtaManifest manifest;
12 : :
13 : 2 : cJSON* device_type = cJSON_GetObjectItem(root, "device_type");
14 : 2 : cJSON* version = cJSON_GetObjectItem(root, "version");
15 : 2 : cJSON* firmware_url = cJSON_GetObjectItem(root, "firmware_url");
16 : 2 : cJSON* firmware_size = cJSON_GetObjectItem(root, "firmware_size");
17 : 2 : cJSON* sha256_hex = cJSON_GetObjectItem(root, "sha256_hex");
18 : :
19 : 2 : bool success = false;
20 [ + + ]: 4 : if (cJSON_IsString(device_type) &&
21 [ + - ]: 3 : cJSON_IsString(version) &&
22 [ + - ]: 2 : cJSON_IsString(firmware_url) &&
23 [ + - + - ]: 4 : cJSON_IsNumber(firmware_size) &&
24 : 1 : cJSON_IsString(sha256_hex)) {
25 : :
26 : 1 : auto v = VersionHelper::parse(version->valuestring);
27 [ + - ]: 1 : if (v.has_value()) {
28 : 1 : manifest.device_type = device_type->valuestring;
29 : 1 : manifest.version = v.value();
30 : 1 : manifest.firmware_url = firmware_url->valuestring;
31 : 1 : manifest.firmware_size = static_cast<uint32_t>(firmware_size->valuedouble);
32 : 1 : manifest.sha256_hex = sha256_hex->valuestring;
33 : 1 : success = true;
34 : : }
35 : : }
36 : :
37 : 2 : cJSON_Delete(root);
38 [ + + ]: 3 : return success ? std::make_optional(manifest) : std::nullopt;
39 : 2 : }
|