WIP enroll device
This commit is contained in:
@ -6,13 +6,14 @@
|
||||
#include "storage.h"
|
||||
#include "http_client.h"
|
||||
#include "constants.h"
|
||||
#include "crypto.h"
|
||||
#include "cJSON.h"
|
||||
|
||||
#include "esp_log.h"
|
||||
|
||||
static const char *TAG = "secure_api";
|
||||
|
||||
static char *process_secure_request(const char *uri)
|
||||
static char *process_secure_request(const char *uri, const char *body)
|
||||
{
|
||||
char *url = calloc(1, 255);
|
||||
assert(url);
|
||||
@ -46,7 +47,7 @@ enum DevEnrollmentStatus secure_api_get_device_enrollment_status()
|
||||
sprintf(uri, "/devices_api/mgmt/enrollment_status?id=");
|
||||
assert(storage_get_dev_name(uri + strlen(uri)) > 0);
|
||||
|
||||
char *res = process_secure_request(uri);
|
||||
char *res = process_secure_request(uri, NULL);
|
||||
|
||||
free(uri);
|
||||
|
||||
@ -89,4 +90,54 @@ fail:
|
||||
free(res);
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate device information. Pointer to be released by caller
|
||||
*/
|
||||
static cJSON *genDevInfo()
|
||||
{
|
||||
cJSON *json = cJSON_CreateObject();
|
||||
if (!json)
|
||||
return NULL;
|
||||
cJSON_AddStringToObject(json, "reference", DEV_REFERENCE);
|
||||
cJSON_AddStringToObject(json, "version", DEV_VERSION);
|
||||
cJSON_AddNumberToObject(json, "max_relays", DEV_MAX_RELAYS);
|
||||
return json;
|
||||
}
|
||||
|
||||
int secure_api_enroll_device()
|
||||
{
|
||||
char *csr = crypto_get_csr();
|
||||
if (!csr)
|
||||
{
|
||||
ESP_LOGE(TAG, "Failed to get CSR!");
|
||||
return 1;
|
||||
}
|
||||
|
||||
cJSON *obj = cJSON_CreateObject();
|
||||
if (!obj)
|
||||
{
|
||||
ESP_LOGE(TAG, "Failed allocate memory to store JSON object!");
|
||||
return 1;
|
||||
}
|
||||
|
||||
cJSON_AddItemToObject(obj, "info", genDevInfo());
|
||||
cJSON_AddStringToObject(obj, "csr", csr);
|
||||
|
||||
char *body = cJSON_PrintUnformatted(obj);
|
||||
if (!body)
|
||||
{
|
||||
ESP_LOGE(TAG, "Failed to generate JSON body!");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// TODO : perform request
|
||||
printf("res = %s\n", body);
|
||||
|
||||
cJSON_Delete(obj);
|
||||
free(body);
|
||||
free(csr);
|
||||
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user