Parse sync response from server
This commit is contained in:
		@@ -148,16 +148,16 @@ int secure_api_enroll_device()
 | 
			
		||||
    free(csr);
 | 
			
		||||
 | 
			
		||||
    char *body = cJSON_PrintUnformatted(obj);
 | 
			
		||||
    cJSON_Delete(obj);
 | 
			
		||||
 | 
			
		||||
    if (!body)
 | 
			
		||||
    {
 | 
			
		||||
        ESP_LOGE(TAG, "Failed to generate JSON body!");
 | 
			
		||||
        cJSON_Delete(obj);
 | 
			
		||||
        return 1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    char *res = process_secure_request("/devices_api/mgmt/enroll", body);
 | 
			
		||||
 | 
			
		||||
    cJSON_Delete(obj);
 | 
			
		||||
    free(body);
 | 
			
		||||
 | 
			
		||||
    if (res == NULL)
 | 
			
		||||
@@ -195,7 +195,7 @@ char *secure_api_get_dev_certificate()
 | 
			
		||||
    return res;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void *secure_api_sync_device()
 | 
			
		||||
sync_response *secure_api_sync_device()
 | 
			
		||||
{
 | 
			
		||||
    cJSON *obj = cJSON_CreateObject();
 | 
			
		||||
    if (!obj)
 | 
			
		||||
@@ -215,10 +215,53 @@ void *secure_api_sync_device()
 | 
			
		||||
        return NULL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    printf("JWT: %s\n", encoded_req);
 | 
			
		||||
    // Prepare request body
 | 
			
		||||
    cJSON *json_body = cJSON_CreateObject();
 | 
			
		||||
    if (!json_body)
 | 
			
		||||
    {
 | 
			
		||||
        ESP_LOGE(TAG, "Failed to allocated memory to store sync request body!");
 | 
			
		||||
        free(encoded_req);
 | 
			
		||||
        return NULL;
 | 
			
		||||
    }
 | 
			
		||||
    cJSON_AddStringToObject(json_body, "payload", encoded_req);
 | 
			
		||||
    free(encoded_req);
 | 
			
		||||
 | 
			
		||||
    // TODO : replace
 | 
			
		||||
    printf("here implement sync device logic\n");
 | 
			
		||||
    return NULL;
 | 
			
		||||
    char *body = cJSON_PrintUnformatted(json_body);
 | 
			
		||||
    cJSON_Delete(json_body);
 | 
			
		||||
 | 
			
		||||
    if (!body)
 | 
			
		||||
    {
 | 
			
		||||
        ESP_LOGE(TAG, "Failed to allocated memory to store encoded sync request body!");
 | 
			
		||||
        return NULL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Send request
 | 
			
		||||
    char *res = process_secure_request("/devices_api/mgmt/sync", body);
 | 
			
		||||
    if (res == NULL)
 | 
			
		||||
    {
 | 
			
		||||
        ESP_LOGE(TAG, "Sync request failed!");
 | 
			
		||||
        return NULL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Parse response
 | 
			
		||||
    cJSON *states = cJSON_Parse(res);
 | 
			
		||||
    free(res);
 | 
			
		||||
 | 
			
		||||
    if (!states)
 | 
			
		||||
    {
 | 
			
		||||
        ESP_LOGE(TAG, "Failed to decode sync response from server!");
 | 
			
		||||
        return NULL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    sync_response *sync_res = sync_response_parse(states);
 | 
			
		||||
 | 
			
		||||
    cJSON_Delete(states);
 | 
			
		||||
 | 
			
		||||
    if (!sync_res)
 | 
			
		||||
    {
 | 
			
		||||
        ESP_LOGE(TAG, "Failed to parse sync response from server!");
 | 
			
		||||
        return NULL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return sync_res;
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user