Decode enrollment status JSON response

This commit is contained in:
Pierre HUBERT 2024-08-18 20:33:26 +02:00
parent 3b5d2abcc0
commit 38197afd79
4 changed files with 40 additions and 9 deletions

View File

@ -32,6 +32,7 @@
"unsecure_api.h": "c", "unsecure_api.h": "c",
"esp_log.h": "c", "esp_log.h": "c",
"http_client.h": "c", "http_client.h": "c",
"string.h": "c" "string.h": "c",
"cjson.h": "c"
} }
} }

View File

@ -99,7 +99,7 @@ void app_main(void)
// Check current device enrollment status // Check current device enrollment status
ESP_LOGI(TAG, "Check enrollment status"); ESP_LOGI(TAG, "Check enrollment status");
int status = secure_api_get_device_enrollment_status(); int status = secure_api_get_device_enrollment_status();
printf("Current enrollment status: %d\n", status); ESP_LOGI(TAG, "Current enrollment status: %d\n", status);
system_sleep(120); system_sleep(120);

View File

@ -6,6 +6,7 @@
#include "storage.h" #include "storage.h"
#include "http_client.h" #include "http_client.h"
#include "constants.h" #include "constants.h"
#include "cJSON.h"
#include "esp_log.h" #include "esp_log.h"
@ -46,6 +47,7 @@ enum DevEnrollmentStatus secure_api_get_device_enrollment_status()
assert(storage_get_dev_name(uri + strlen(uri)) > 0); assert(storage_get_dev_name(uri + strlen(uri)) > 0);
char *res = process_secure_request(uri); char *res = process_secure_request(uri);
free(uri); free(uri);
if (res == NULL) if (res == NULL)
@ -54,9 +56,37 @@ enum DevEnrollmentStatus secure_api_get_device_enrollment_status()
return DevEnrollError; return DevEnrollError;
} }
// TODO : parse reponse enum DevEnrollmentStatus s = DevEnrollError;
printf("response = %s\n", res); cJSON *root = cJSON_Parse(res);
if (root == NULL)
{
ESP_LOGE(TAG, "Failed to decode JSON response from server!");
goto fail;
}
cJSON *status = cJSON_GetObjectItem(root, "status");
if (status == NULL)
{
ESP_LOGE(TAG, "Status missing in response from server!");
goto fail;
}
if (!strcmp(status->valuestring, "Unknown"))
s = DevEnrollUnknown;
else if (!strcmp(status->valuestring, "Pending"))
s = DevEnrollPending;
else if (!strcmp(status->valuestring, "Validated"))
s = DevEnrollValidated;
else
{
ESP_LOGE(TAG, "Unknown enrollment status: %s", status->valuestring);
goto fail;
}
fail:
cJSON_Delete(root);
free(res); free(res);
return DevEnrollError; return s;
} }

View File

@ -15,13 +15,13 @@ extern "C"
enum DevEnrollmentStatus enum DevEnrollmentStatus
{ {
/** An error occurred while retrieving device status */ /** An error occurred while retrieving device status */
DevEnrollError, DevEnrollError = 0,
/** Device is unknown by the backend */ /** Device is unknown by the backend */
DevEnrollUnknown, DevEnrollUnknown = 1,
/** Device hasn't been validated yet */ /** Device hasn't been validated yet */
DevEnrollPending, DevEnrollPending = 2,
/** Device has been validated by the backend */ /** Device has been validated by the backend */
DevEnrollValidated, DevEnrollValidated = 3,
}; };
/** /**