subsys/mgmt/hawkbit: format for readability

Formatted the code so that it is easier to read.

Signed-off-by: Yong Cong Sin <yongcong.sin@gmail.com>
This commit is contained in:
Yong Cong Sin 2022-02-26 16:11:52 +08:00 committed by Carles Cufí
parent 3209bc1a4a
commit ae587f8e50

View file

@ -104,46 +104,36 @@ static const struct json_obj_descr json_href_descr[] = {
}; };
static const struct json_obj_descr json_status_result_descr[] = { static const struct json_obj_descr json_status_result_descr[] = {
JSON_OBJ_DESCR_PRIM(struct hawkbit_status_result, finished, JSON_OBJ_DESCR_PRIM(struct hawkbit_status_result, finished, JSON_TOK_STRING),
JSON_TOK_STRING),
}; };
static const struct json_obj_descr json_status_descr[] = { static const struct json_obj_descr json_status_descr[] = {
JSON_OBJ_DESCR_PRIM(struct hawkbit_status, execution, JSON_TOK_STRING), JSON_OBJ_DESCR_PRIM(struct hawkbit_status, execution, JSON_TOK_STRING),
JSON_OBJ_DESCR_OBJECT(struct hawkbit_status, result, JSON_OBJ_DESCR_OBJECT(struct hawkbit_status, result, json_status_result_descr),
json_status_result_descr),
}; };
static const struct json_obj_descr json_ctl_res_sleep_descr[] = { static const struct json_obj_descr json_ctl_res_sleep_descr[] = {
JSON_OBJ_DESCR_PRIM(struct hawkbit_ctl_res_sleep, sleep, JSON_OBJ_DESCR_PRIM(struct hawkbit_ctl_res_sleep, sleep, JSON_TOK_STRING),
JSON_TOK_STRING),
}; };
static const struct json_obj_descr json_ctl_res_polling_descr[] = { static const struct json_obj_descr json_ctl_res_polling_descr[] = {
JSON_OBJ_DESCR_OBJECT(struct hawkbit_ctl_res_polling, polling, JSON_OBJ_DESCR_OBJECT(struct hawkbit_ctl_res_polling, polling, json_ctl_res_sleep_descr),
json_ctl_res_sleep_descr),
}; };
static const struct json_obj_descr json_ctl_res_links_descr[] = { static const struct json_obj_descr json_ctl_res_links_descr[] = {
JSON_OBJ_DESCR_OBJECT(struct hawkbit_ctl_res_links, deploymentBase, JSON_OBJ_DESCR_OBJECT(struct hawkbit_ctl_res_links, deploymentBase, json_href_descr),
json_href_descr), JSON_OBJ_DESCR_OBJECT(struct hawkbit_ctl_res_links, cancelAction, json_href_descr),
JSON_OBJ_DESCR_OBJECT(struct hawkbit_ctl_res_links, cancelAction, JSON_OBJ_DESCR_OBJECT(struct hawkbit_ctl_res_links, configData, json_href_descr),
json_href_descr),
JSON_OBJ_DESCR_OBJECT(struct hawkbit_ctl_res_links, configData,
json_href_descr),
}; };
static const struct json_obj_descr json_ctl_res_descr[] = { static const struct json_obj_descr json_ctl_res_descr[] = {
JSON_OBJ_DESCR_OBJECT(struct hawkbit_ctl_res, config, JSON_OBJ_DESCR_OBJECT(struct hawkbit_ctl_res, config, json_ctl_res_polling_descr),
json_ctl_res_polling_descr), JSON_OBJ_DESCR_OBJECT(struct hawkbit_ctl_res, _links, json_ctl_res_links_descr),
JSON_OBJ_DESCR_OBJECT(struct hawkbit_ctl_res, _links,
json_ctl_res_links_descr),
}; };
static const struct json_obj_descr json_cfg_data_descr[] = { static const struct json_obj_descr json_cfg_data_descr[] = {
JSON_OBJ_DESCR_PRIM(struct hawkbit_cfg_data, VIN, JSON_TOK_STRING), JSON_OBJ_DESCR_PRIM(struct hawkbit_cfg_data, VIN, JSON_TOK_STRING),
JSON_OBJ_DESCR_PRIM(struct hawkbit_cfg_data, hwRevision, JSON_OBJ_DESCR_PRIM(struct hawkbit_cfg_data, hwRevision, JSON_TOK_STRING),
JSON_TOK_STRING),
}; };
static const struct json_obj_descr json_cfg_descr[] = { static const struct json_obj_descr json_cfg_descr[] = {
@ -161,66 +151,50 @@ static const struct json_obj_descr json_close_descr[] = {
}; };
static const struct json_obj_descr json_dep_res_hashes_descr[] = { static const struct json_obj_descr json_dep_res_hashes_descr[] = {
JSON_OBJ_DESCR_PRIM(struct hawkbit_dep_res_hashes, sha1, JSON_OBJ_DESCR_PRIM(struct hawkbit_dep_res_hashes, sha1, JSON_TOK_STRING),
JSON_TOK_STRING), JSON_OBJ_DESCR_PRIM(struct hawkbit_dep_res_hashes, md5, JSON_TOK_STRING),
JSON_OBJ_DESCR_PRIM(struct hawkbit_dep_res_hashes, md5, JSON_OBJ_DESCR_PRIM(struct hawkbit_dep_res_hashes, sha256, JSON_TOK_STRING),
JSON_TOK_STRING),
JSON_OBJ_DESCR_PRIM(struct hawkbit_dep_res_hashes, sha256,
JSON_TOK_STRING),
}; };
static const struct json_obj_descr json_dep_res_links_descr[] = { static const struct json_obj_descr json_dep_res_links_descr[] = {
JSON_OBJ_DESCR_OBJECT_NAMED(struct hawkbit_dep_res_links, JSON_OBJ_DESCR_OBJECT_NAMED(struct hawkbit_dep_res_links, "download-http", download_http,
"download-http", download_http, json_href_descr),
JSON_OBJ_DESCR_OBJECT_NAMED(struct hawkbit_dep_res_links, "md5sum-http", md5sum_http,
json_href_descr), json_href_descr),
JSON_OBJ_DESCR_OBJECT_NAMED(struct hawkbit_dep_res_links, "md5sum-http",
md5sum_http, json_href_descr),
}; };
static const struct json_obj_descr json_dep_res_arts_descr[] = { static const struct json_obj_descr json_dep_res_arts_descr[] = {
JSON_OBJ_DESCR_PRIM(struct hawkbit_dep_res_arts, filename, JSON_OBJ_DESCR_PRIM(struct hawkbit_dep_res_arts, filename, JSON_TOK_STRING),
JSON_TOK_STRING), JSON_OBJ_DESCR_OBJECT(struct hawkbit_dep_res_arts, hashes, json_dep_res_hashes_descr),
JSON_OBJ_DESCR_OBJECT(struct hawkbit_dep_res_arts, hashes,
json_dep_res_hashes_descr),
JSON_OBJ_DESCR_PRIM(struct hawkbit_dep_res_arts, size, JSON_TOK_NUMBER), JSON_OBJ_DESCR_PRIM(struct hawkbit_dep_res_arts, size, JSON_TOK_NUMBER),
JSON_OBJ_DESCR_OBJECT(struct hawkbit_dep_res_arts, _links, JSON_OBJ_DESCR_OBJECT(struct hawkbit_dep_res_arts, _links, json_dep_res_links_descr),
json_dep_res_links_descr),
}; };
static const struct json_obj_descr json_dep_res_chunk_descr[] = { static const struct json_obj_descr json_dep_res_chunk_descr[] = {
JSON_OBJ_DESCR_PRIM(struct hawkbit_dep_res_chunk, part, JSON_OBJ_DESCR_PRIM(struct hawkbit_dep_res_chunk, part, JSON_TOK_STRING),
JSON_TOK_STRING), JSON_OBJ_DESCR_PRIM(struct hawkbit_dep_res_chunk, version, JSON_TOK_STRING),
JSON_OBJ_DESCR_PRIM(struct hawkbit_dep_res_chunk, version, JSON_OBJ_DESCR_PRIM(struct hawkbit_dep_res_chunk, name, JSON_TOK_STRING),
JSON_TOK_STRING),
JSON_OBJ_DESCR_PRIM(struct hawkbit_dep_res_chunk, name,
JSON_TOK_STRING),
JSON_OBJ_DESCR_OBJ_ARRAY(struct hawkbit_dep_res_chunk, artifacts, JSON_OBJ_DESCR_OBJ_ARRAY(struct hawkbit_dep_res_chunk, artifacts,
HAWKBIT_DEP_MAX_CHUNK_ARTS, num_artifacts, HAWKBIT_DEP_MAX_CHUNK_ARTS, num_artifacts, json_dep_res_arts_descr,
json_dep_res_arts_descr,
ARRAY_SIZE(json_dep_res_arts_descr)), ARRAY_SIZE(json_dep_res_arts_descr)),
}; };
static const struct json_obj_descr json_dep_res_deploy_descr[] = { static const struct json_obj_descr json_dep_res_deploy_descr[] = {
JSON_OBJ_DESCR_PRIM(struct hawkbit_dep_res_deploy, download, JSON_OBJ_DESCR_PRIM(struct hawkbit_dep_res_deploy, download, JSON_TOK_STRING),
JSON_TOK_STRING), JSON_OBJ_DESCR_PRIM(struct hawkbit_dep_res_deploy, update, JSON_TOK_STRING),
JSON_OBJ_DESCR_PRIM(struct hawkbit_dep_res_deploy, update, JSON_OBJ_DESCR_OBJ_ARRAY(struct hawkbit_dep_res_deploy, chunks, HAWKBIT_DEP_MAX_CHUNKS,
JSON_TOK_STRING), num_chunks, json_dep_res_chunk_descr,
JSON_OBJ_DESCR_OBJ_ARRAY(struct hawkbit_dep_res_deploy, chunks,
HAWKBIT_DEP_MAX_CHUNKS, num_chunks,
json_dep_res_chunk_descr,
ARRAY_SIZE(json_dep_res_chunk_descr)), ARRAY_SIZE(json_dep_res_chunk_descr)),
}; };
static const struct json_obj_descr json_dep_res_descr[] = { static const struct json_obj_descr json_dep_res_descr[] = {
JSON_OBJ_DESCR_PRIM(struct hawkbit_dep_res, id, JSON_TOK_STRING), JSON_OBJ_DESCR_PRIM(struct hawkbit_dep_res, id, JSON_TOK_STRING),
JSON_OBJ_DESCR_OBJECT(struct hawkbit_dep_res, deployment, JSON_OBJ_DESCR_OBJECT(struct hawkbit_dep_res, deployment, json_dep_res_deploy_descr),
json_dep_res_deploy_descr),
}; };
static const struct json_obj_descr json_dep_fbk_descr[] = { static const struct json_obj_descr json_dep_fbk_descr[] = {
JSON_OBJ_DESCR_PRIM(struct hawkbit_dep_fbk, id, JSON_TOK_STRING), JSON_OBJ_DESCR_PRIM(struct hawkbit_dep_fbk, id, JSON_TOK_STRING),
JSON_OBJ_DESCR_OBJECT(struct hawkbit_dep_fbk, status, JSON_OBJ_DESCR_OBJECT(struct hawkbit_dep_fbk, status, json_status_descr),
json_status_descr),
}; };
static bool start_http_client(void) static bool start_http_client(void)
@ -245,8 +219,7 @@ static bool start_http_client(void)
hints.ai_socktype = SOCK_STREAM; hints.ai_socktype = SOCK_STREAM;
while (resolve_attempts--) { while (resolve_attempts--) {
ret = getaddrinfo(CONFIG_HAWKBIT_SERVER, CONFIG_HAWKBIT_PORT, ret = getaddrinfo(CONFIG_HAWKBIT_SERVER, CONFIG_HAWKBIT_PORT, &hints, &addr);
&hints, &addr);
if (ret == 0) { if (ret == 0) {
break; break;
} }
@ -276,8 +249,7 @@ static bool start_http_client(void)
goto err_sock; goto err_sock;
} }
if (setsockopt(hb_context.sock, SOL_TLS, TLS_HOSTNAME, if (setsockopt(hb_context.sock, SOL_TLS, TLS_HOSTNAME, CONFIG_HAWKBIT_SERVER,
CONFIG_HAWKBIT_SERVER,
sizeof(CONFIG_HAWKBIT_SERVER)) < 0) { sizeof(CONFIG_HAWKBIT_SERVER)) < 0) {
goto err_sock; goto err_sock;
} }
@ -385,8 +357,7 @@ static void hawkbit_update_sleep(struct hawkbit_ctl_res *hawkbit_res)
LOG_ERR("Invalid poll sleep: %s", log_strdup(sleep)); LOG_ERR("Invalid poll sleep: %s", log_strdup(sleep));
} else { } else {
sleep_time = hawkbit_time2sec(sleep); sleep_time = hawkbit_time2sec(sleep);
if (sleep_time > 0 && if (sleep_time > 0 && poll_sleep != (MSEC_PER_SEC * sleep_time)) {
poll_sleep != (MSEC_PER_SEC * sleep_time)) {
LOG_DBG("New poll sleep %d seconds", sleep_time); LOG_DBG("New poll sleep %d seconds", sleep_time);
poll_sleep = sleep_time * MSEC_PER_SEC; poll_sleep = sleep_time * MSEC_PER_SEC;
} }
@ -396,8 +367,7 @@ static void hawkbit_update_sleep(struct hawkbit_ctl_res *hawkbit_res)
/* /*
* Find URL component for the device cancel operation and action id * Find URL component for the device cancel operation and action id
*/ */
static int hawkbit_find_cancelAction_base(struct hawkbit_ctl_res *res, static int hawkbit_find_cancelAction_base(struct hawkbit_ctl_res *res, char *cancel_base)
char *cancel_base)
{ {
size_t len; size_t len;
const char *href; const char *href;
@ -419,8 +389,8 @@ static int hawkbit_find_cancelAction_base(struct hawkbit_ctl_res *res,
len = strlen(helper); len = strlen(helper);
if (len > CANCEL_BASE_SIZE - 1) { if (len > CANCEL_BASE_SIZE - 1) {
/* Lack of memory is an application error */ /* Lack of memory is an application error */
LOG_ERR("cancelBase %s is too big (len %zu, max %zu)", log_strdup(helper), LOG_ERR("cancelBase %s is too big (len %zu, max %zu)", log_strdup(helper), len,
len, CANCEL_BASE_SIZE - 1); CANCEL_BASE_SIZE - 1);
return -ENOMEM; return -ENOMEM;
} }
@ -449,8 +419,7 @@ static int hawkbit_find_cancelAction_base(struct hawkbit_ctl_res *res,
* Find URL component for the device's deployment operations * Find URL component for the device's deployment operations
* resource * resource
*/ */
static int hawkbit_find_deployment_base(struct hawkbit_ctl_res *res, static int hawkbit_find_deployment_base(struct hawkbit_ctl_res *res, char *deployment_base)
char *deployment_base)
{ {
const char *href; const char *href;
const char *helper; const char *helper;
@ -472,8 +441,8 @@ static int hawkbit_find_deployment_base(struct hawkbit_ctl_res *res,
len = strlen(helper); len = strlen(helper);
if (len > DEPLOYMENT_BASE_SIZE - 1) { if (len > DEPLOYMENT_BASE_SIZE - 1) {
/* Lack of memory is an application error */ /* Lack of memory is an application error */
LOG_ERR("deploymentBase %s is too big (len %zu, max %zu)", LOG_ERR("deploymentBase %s is too big (len %zu, max %zu)", log_strdup(helper), len,
log_strdup(helper), len, DEPLOYMENT_BASE_SIZE - 1); DEPLOYMENT_BASE_SIZE - 1);
return -ENOMEM; return -ENOMEM;
} }
@ -485,8 +454,7 @@ static int hawkbit_find_deployment_base(struct hawkbit_ctl_res *res,
* Find URL component for this device's deployment operations * Find URL component for this device's deployment operations
* resource. * resource.
*/ */
static int hawkbit_parse_deployment(struct hawkbit_dep_res *res, static int hawkbit_parse_deployment(struct hawkbit_dep_res *res, int32_t *json_action_id,
int32_t *json_action_id,
char *download_http, int32_t *file_size) char *download_http, int32_t *file_size)
{ {
int32_t size; int32_t size;
@ -524,17 +492,15 @@ static int hawkbit_parse_deployment(struct hawkbit_dep_res *res,
} }
artifact = &chunk->artifacts[0]; artifact = &chunk->artifacts[0];
if (hex2bin(artifact->hashes.sha256, SHA256_HASH_SIZE << 1, if (hex2bin(artifact->hashes.sha256, SHA256_HASH_SIZE << 1, hb_context.dl.file_hash,
hb_context.dl.file_hash, sizeof(hb_context.dl.file_hash)) != sizeof(hb_context.dl.file_hash)) != SHA256_HASH_SIZE) {
SHA256_HASH_SIZE) {
return -EINVAL; return -EINVAL;
} }
size = artifact->size; size = artifact->size;
if (size > SLOT1_SIZE) { if (size > SLOT1_SIZE) {
LOG_ERR("Artifact file size too big (got %d, max is %d)", size, LOG_ERR("Artifact file size too big (got %d, max is %d)", size, SLOT1_SIZE);
SLOT1_SIZE);
return -ENOSPC; return -ENOSPC;
} }
@ -559,8 +525,8 @@ static int hawkbit_parse_deployment(struct hawkbit_dep_res *res,
LOG_ERR("Empty download-http"); LOG_ERR("Empty download-http");
return -EINVAL; return -EINVAL;
} else if (len > DOWNLOAD_HTTP_SIZE - 1) { } else if (len > DOWNLOAD_HTTP_SIZE - 1) {
LOG_ERR("download-http %s is too big (len: %zu, max: %zu)", LOG_ERR("download-http %s is too big (len: %zu, max: %zu)", log_strdup(helper), len,
log_strdup(helper), len, DOWNLOAD_HTTP_SIZE - 1); DOWNLOAD_HTTP_SIZE - 1);
return -ENOMEM; return -ENOMEM;
} }
@ -573,12 +539,9 @@ static int hawkbit_parse_deployment(struct hawkbit_dep_res *res,
static void hawkbit_dump_base(struct hawkbit_ctl_res *r) static void hawkbit_dump_base(struct hawkbit_ctl_res *r)
{ {
LOG_DBG("config.polling.sleep=%s", log_strdup(r->config.polling.sleep)); LOG_DBG("config.polling.sleep=%s", log_strdup(r->config.polling.sleep));
LOG_DBG("_links.deploymentBase.href=%s", LOG_DBG("_links.deploymentBase.href=%s", log_strdup(r->_links.deploymentBase.href));
log_strdup(r->_links.deploymentBase.href)); LOG_DBG("_links.configData.href=%s", log_strdup(r->_links.configData.href));
LOG_DBG("_links.configData.href=%s", LOG_DBG("_links.cancelAction.href=%s", log_strdup(r->_links.cancelAction.href));
log_strdup(r->_links.configData.href));
LOG_DBG("_links.cancelAction.href=%s",
log_strdup(r->_links.cancelAction.href));
} }
static void hawkbit_dump_deployment(struct hawkbit_dep_res *d) static void hawkbit_dump_deployment(struct hawkbit_dep_res *d)
@ -594,12 +557,9 @@ static void hawkbit_dump_deployment(struct hawkbit_dep_res *d)
LOG_DBG("chunks[0].name=%s", log_strdup(c->name)); LOG_DBG("chunks[0].name=%s", log_strdup(c->name));
LOG_DBG("chunks[0].version=%s", log_strdup(c->version)); LOG_DBG("chunks[0].version=%s", log_strdup(c->version));
LOG_DBG("chunks[0].artifacts[0].filename=%s", log_strdup(a->filename)); LOG_DBG("chunks[0].artifacts[0].filename=%s", log_strdup(a->filename));
LOG_DBG("chunks[0].artifacts[0].hashes.sha1=%s", LOG_DBG("chunks[0].artifacts[0].hashes.sha1=%s", log_strdup(a->hashes.sha1));
log_strdup(a->hashes.sha1)); LOG_DBG("chunks[0].artifacts[0].hashes.md5=%s", log_strdup(a->hashes.md5));
LOG_DBG("chunks[0].artifacts[0].hashes.md5=%s", LOG_DBG("chunks[0].artifacts[0].hashes.sha256=%s", log_strdup(a->hashes.sha256));
log_strdup(a->hashes.md5));
LOG_DBG("chunks[0].artifacts[0].hashes.sha256=%s",
log_strdup(a->hashes.sha256));
LOG_DBG("chunks[0].size=%d", a->size); LOG_DBG("chunks[0].size=%d", a->size);
LOG_DBG("download-http=%s", log_strdup(l->download_http.href)); LOG_DBG("download-http=%s", log_strdup(l->download_http.href));
LOG_DBG("md5sum =%s", log_strdup(l->md5sum_http.href)); LOG_DBG("md5sum =%s", log_strdup(l->md5sum_http.href));
@ -675,8 +635,7 @@ static int enum_for_http_req_string(char *userdata)
return 0; return 0;
} }
static void response_cb(struct http_response *rsp, static void response_cb(struct http_response *rsp, enum http_final_call final_data, void *userdata)
enum http_final_call final_data, void *userdata)
{ {
static size_t body_len; static size_t body_len;
int ret, type, downloaded; int ret, type, downloaded;
@ -697,19 +656,17 @@ static void response_cb(struct http_response *rsp,
if ((hb_context.dl.downloaded_size + body_len) > response_buffer_size) { if ((hb_context.dl.downloaded_size + body_len) > response_buffer_size) {
response_buffer_size <<= 1; response_buffer_size <<= 1;
rsp_tmp = realloc(hb_context.response_data, rsp_tmp = realloc(hb_context.response_data, response_buffer_size);
response_buffer_size);
if (rsp_tmp == NULL) { if (rsp_tmp == NULL) {
LOG_ERR("Failed to realloc memory"); LOG_ERR("Failed to realloc memory");
hb_context.code_status = hb_context.code_status = HAWKBIT_METADATA_ERROR;
HAWKBIT_METADATA_ERROR;
break; break;
} }
hb_context.response_data = rsp_tmp; hb_context.response_data = rsp_tmp;
} }
strncpy(hb_context.response_data + hb_context.dl.downloaded_size, strncpy(hb_context.response_data + hb_context.dl.downloaded_size, body_data,
body_data, body_len); body_len);
hb_context.dl.downloaded_size += body_len; hb_context.dl.downloaded_size += body_len;
} }
@ -724,10 +681,8 @@ static void response_cb(struct http_response *rsp,
hb_context.response_data[hb_context.dl.downloaded_size] = '\0'; hb_context.response_data[hb_context.dl.downloaded_size] = '\0';
ret = json_obj_parse(hb_context.response_data, ret = json_obj_parse(hb_context.response_data,
hb_context.dl.downloaded_size, hb_context.dl.downloaded_size, json_ctl_res_descr,
json_ctl_res_descr, ARRAY_SIZE(json_ctl_res_descr), &hawkbit_results.base);
ARRAY_SIZE(json_ctl_res_descr),
&hawkbit_results.base);
if (ret < 0) { if (ret < 0) {
LOG_ERR("JSON parse error (HAWKBIT_PROBE): %d", ret); LOG_ERR("JSON parse error (HAWKBIT_PROBE): %d", ret);
hb_context.code_status = HAWKBIT_METADATA_ERROR; hb_context.code_status = HAWKBIT_METADATA_ERROR;
@ -756,19 +711,17 @@ static void response_cb(struct http_response *rsp,
if ((hb_context.dl.downloaded_size + body_len) > response_buffer_size) { if ((hb_context.dl.downloaded_size + body_len) > response_buffer_size) {
response_buffer_size <<= 1; response_buffer_size <<= 1;
rsp_tmp = realloc(hb_context.response_data, rsp_tmp = realloc(hb_context.response_data, response_buffer_size);
response_buffer_size);
if (rsp_tmp == NULL) { if (rsp_tmp == NULL) {
LOG_ERR("Failed to realloc memory"); LOG_ERR("Failed to realloc memory");
hb_context.code_status = hb_context.code_status = HAWKBIT_METADATA_ERROR;
HAWKBIT_METADATA_ERROR;
break; break;
} }
hb_context.response_data = rsp_tmp; hb_context.response_data = rsp_tmp;
} }
strncpy(hb_context.response_data + hb_context.dl.downloaded_size, strncpy(hb_context.response_data + hb_context.dl.downloaded_size, body_data,
body_data, body_len); body_len);
hb_context.dl.downloaded_size += body_len; hb_context.dl.downloaded_size += body_len;
} }
@ -781,10 +734,8 @@ static void response_cb(struct http_response *rsp,
hb_context.response_data[hb_context.dl.downloaded_size] = '\0'; hb_context.response_data[hb_context.dl.downloaded_size] = '\0';
ret = json_obj_parse(hb_context.response_data, ret = json_obj_parse(hb_context.response_data,
hb_context.dl.downloaded_size, hb_context.dl.downloaded_size, json_dep_res_descr,
json_dep_res_descr, ARRAY_SIZE(json_dep_res_descr), &hawkbit_results.dep);
ARRAY_SIZE(json_dep_res_descr),
&hawkbit_results.dep);
if (ret < 0) { if (ret < 0) {
LOG_ERR("DeploymentBase JSON parse error: %d", ret); LOG_ERR("DeploymentBase JSON parse error: %d", ret);
hb_context.code_status = HAWKBIT_METADATA_ERROR; hb_context.code_status = HAWKBIT_METADATA_ERROR;
@ -802,8 +753,7 @@ static void response_cb(struct http_response *rsp,
body_data = rsp->body_frag_start; body_data = rsp->body_frag_start;
body_len = rsp->body_frag_len; body_len = rsp->body_frag_len;
ret = flash_img_buffered_write( ret = flash_img_buffered_write(&hb_context.flash_ctx, body_data, body_len,
&hb_context.flash_ctx, body_data, body_len,
final_data == HTTP_DATA_FINAL); final_data == HTTP_DATA_FINAL);
if (ret < 0) { if (ret < 0) {
LOG_ERR("Flash write error: %d", ret); LOG_ERR("Flash write error: %d", ret);
@ -812,16 +762,13 @@ static void response_cb(struct http_response *rsp,
} }
} }
hb_context.dl.downloaded_size = hb_context.dl.downloaded_size = flash_img_bytes_written(&hb_context.flash_ctx);
flash_img_bytes_written(&hb_context.flash_ctx);
downloaded = hb_context.dl.downloaded_size * 100 / downloaded = hb_context.dl.downloaded_size * 100 / hb_context.dl.http_content_size;
hb_context.dl.http_content_size;
if (downloaded > hb_context.dl.download_progress) { if (downloaded > hb_context.dl.download_progress) {
hb_context.dl.download_progress = downloaded; hb_context.dl.download_progress = downloaded;
LOG_DBG("Download percentage: %d%% ", LOG_DBG("Download percentage: %d%% ", hb_context.dl.download_progress);
hb_context.dl.download_progress);
} }
if (final_data == HTTP_DATA_FINAL) { if (final_data == HTTP_DATA_FINAL) {
@ -832,10 +779,8 @@ static void response_cb(struct http_response *rsp,
} }
} }
static bool send_request(enum http_method method, static bool send_request(enum http_method method, enum hawkbit_http_request type,
enum hawkbit_http_request type, enum hawkbit_status_fini finished, enum hawkbit_status_exec execution)
enum hawkbit_status_fini finished,
enum hawkbit_status_exec execution)
{ {
int ret = 0; int ret = 0;
@ -847,11 +792,11 @@ static bool send_request(enum http_method method,
const char *exec = hawkbit_status_execution(execution); const char *exec = hawkbit_status_execution(execution);
char device_id[DEVICE_ID_HEX_MAX_SIZE] = { 0 }; char device_id[DEVICE_ID_HEX_MAX_SIZE] = { 0 };
#ifndef CONFIG_HAWKBIT_DDI_NO_SECURITY #ifndef CONFIG_HAWKBIT_DDI_NO_SECURITY
static const char * const headers[] = { static const char *const headers[] = {
#ifdef CONFIG_HAWKBIT_DDI_GATEWAY_SECURITY #ifdef CONFIG_HAWKBIT_DDI_GATEWAY_SECURITY
"Authorization: GatewayToken "CONFIG_HAWKBIT_DDI_SECURITY_TOKEN"\r\n", "Authorization: GatewayToken " CONFIG_HAWKBIT_DDI_SECURITY_TOKEN "\r\n",
#else #else
"Authorization: TargetToken "CONFIG_HAWKBIT_DDI_SECURITY_TOKEN"\r\n", "Authorization: TargetToken " CONFIG_HAWKBIT_DDI_SECURITY_TOKEN "\r\n",
#endif /* CONFIG_HAWKBIT_DDI_GATEWAY_SECURITY */ #endif /* CONFIG_HAWKBIT_DDI_GATEWAY_SECURITY */
NULL NULL
}; };
@ -878,8 +823,8 @@ static bool send_request(enum http_method method,
switch (type) { switch (type) {
case HAWKBIT_PROBE: case HAWKBIT_PROBE:
ret = http_client_req(hb_context.sock, &hb_context.http_req, ret = http_client_req(hb_context.sock, &hb_context.http_req, HAWKBIT_RECV_TIMEOUT,
HAWKBIT_RECV_TIMEOUT, "HAWKBIT_PROBE"); "HAWKBIT_PROBE");
if (ret < 0) { if (ret < 0) {
LOG_ERR("Unable to send HTTP request (HAWKBIT_PROBE): %d", ret); LOG_ERR("Unable to send HTTP request (HAWKBIT_PROBE): %d", ret);
return false; return false;
@ -897,8 +842,7 @@ static bool send_request(enum http_method method,
cfg.status.execution = exec; cfg.status.execution = exec;
cfg.status.result.finished = fini; cfg.status.result.finished = fini;
ret = json_obj_encode_buf(json_cfg_descr, ret = json_obj_encode_buf(json_cfg_descr, ARRAY_SIZE(json_cfg_descr), &cfg,
ARRAY_SIZE(json_cfg_descr), &cfg,
hb_context.status_buffer, hb_context.status_buffer,
hb_context.status_buffer_size - 1); hb_context.status_buffer_size - 1);
if (ret) { if (ret) {
@ -906,14 +850,11 @@ static bool send_request(enum http_method method,
return false; return false;
} }
hb_context.http_req.content_type_value = hb_context.http_req.content_type_value = HTTP_HEADER_CONTENT_TYPE_JSON;
HTTP_HEADER_CONTENT_TYPE_JSON;
hb_context.http_req.payload = hb_context.status_buffer; hb_context.http_req.payload = hb_context.status_buffer;
hb_context.http_req.payload_len = hb_context.http_req.payload_len = strlen(hb_context.status_buffer);
strlen(hb_context.status_buffer);
ret = http_client_req(hb_context.sock, &hb_context.http_req, ret = http_client_req(hb_context.sock, &hb_context.http_req, HAWKBIT_RECV_TIMEOUT,
HAWKBIT_RECV_TIMEOUT,
"HAWKBIT_CONFIG_DEVICE"); "HAWKBIT_CONFIG_DEVICE");
if (ret < 0) { if (ret < 0) {
LOG_ERR("Unable to send HTTP request (HAWKBIT_CONFIG_DEVICE): %d", ret); LOG_ERR("Unable to send HTTP request (HAWKBIT_CONFIG_DEVICE): %d", ret);
@ -924,16 +865,14 @@ static bool send_request(enum http_method method,
case HAWKBIT_CLOSE: case HAWKBIT_CLOSE:
memset(&close, 0, sizeof(close)); memset(&close, 0, sizeof(close));
memset(&hb_context.status_buffer, 0, memset(&hb_context.status_buffer, 0, sizeof(hb_context.status_buffer));
sizeof(hb_context.status_buffer));
snprintk(acid, sizeof(acid), "%d", hb_context.action_id); snprintk(acid, sizeof(acid), "%d", hb_context.action_id);
close.id = acid; close.id = acid;
close.time = ""; close.time = "";
close.status.execution = exec; close.status.execution = exec;
close.status.result.finished = fini; close.status.result.finished = fini;
ret = json_obj_encode_buf(json_close_descr, ret = json_obj_encode_buf(json_close_descr, ARRAY_SIZE(json_close_descr), &close,
ARRAY_SIZE(json_close_descr), &close,
hb_context.status_buffer, hb_context.status_buffer,
hb_context.status_buffer_size - 1); hb_context.status_buffer_size - 1);
if (ret) { if (ret) {
@ -941,14 +880,12 @@ static bool send_request(enum http_method method,
return false; return false;
} }
hb_context.http_req.content_type_value = hb_context.http_req.content_type_value = HTTP_HEADER_CONTENT_TYPE_JSON;
HTTP_HEADER_CONTENT_TYPE_JSON;
hb_context.http_req.payload = hb_context.status_buffer; hb_context.http_req.payload = hb_context.status_buffer;
hb_context.http_req.payload_len = hb_context.http_req.payload_len = strlen(hb_context.status_buffer);
strlen(hb_context.status_buffer);
ret = http_client_req(hb_context.sock, &hb_context.http_req, ret = http_client_req(hb_context.sock, &hb_context.http_req, HAWKBIT_RECV_TIMEOUT,
HAWKBIT_RECV_TIMEOUT, "HAWKBIT_CLOSE"); "HAWKBIT_CLOSE");
if (ret < 0) { if (ret < 0) {
LOG_ERR("Unable to send HTTP request (HAWKBIT_CLOSE): %d", ret); LOG_ERR("Unable to send HTTP request (HAWKBIT_CLOSE): %d", ret);
return false; return false;
@ -958,8 +895,7 @@ static bool send_request(enum http_method method,
case HAWKBIT_PROBE_DEPLOYMENT_BASE: case HAWKBIT_PROBE_DEPLOYMENT_BASE:
hb_context.http_req.content_type_value = NULL; hb_context.http_req.content_type_value = NULL;
ret = http_client_req(hb_context.sock, &hb_context.http_req, ret = http_client_req(hb_context.sock, &hb_context.http_req, HAWKBIT_RECV_TIMEOUT,
HAWKBIT_RECV_TIMEOUT,
"HAWKBIT_PROBE_DEPLOYMENT_BASE"); "HAWKBIT_PROBE_DEPLOYMENT_BASE");
if (ret < 0) { if (ret < 0) {
LOG_ERR("Unable to send HTTP request (HAWKBIT_PROBE_DEPLOYMENT_BASE): %d", LOG_ERR("Unable to send HTTP request (HAWKBIT_PROBE_DEPLOYMENT_BASE): %d",
@ -974,8 +910,8 @@ static bool send_request(enum http_method method,
return -EINVAL; return -EINVAL;
} }
LOG_INF("Reporting deployment feedback %s (%s) for action %d", LOG_INF("Reporting deployment feedback %s (%s) for action %d", fini, exec,
fini, exec, hb_context.json_action_id); hb_context.json_action_id);
/* Build JSON */ /* Build JSON */
memset(&feedback, 0, sizeof(feedback)); memset(&feedback, 0, sizeof(feedback));
snprintk(acid, sizeof(acid), "%d", hb_context.json_action_id); snprintk(acid, sizeof(acid), "%d", hb_context.json_action_id);
@ -983,8 +919,7 @@ static bool send_request(enum http_method method,
feedback.status.result.finished = fini; feedback.status.result.finished = fini;
feedback.status.execution = exec; feedback.status.execution = exec;
ret = json_obj_encode_buf(json_dep_fbk_descr, ret = json_obj_encode_buf(json_dep_fbk_descr, ARRAY_SIZE(json_dep_fbk_descr),
ARRAY_SIZE(json_dep_fbk_descr),
&feedback, hb_context.status_buffer, &feedback, hb_context.status_buffer,
hb_context.status_buffer_size - 1); hb_context.status_buffer_size - 1);
if (ret) { if (ret) {
@ -992,14 +927,12 @@ static bool send_request(enum http_method method,
return ret; return ret;
} }
hb_context.http_req.content_type_value = hb_context.http_req.content_type_value = HTTP_HEADER_CONTENT_TYPE_JSON;
HTTP_HEADER_CONTENT_TYPE_JSON;
hb_context.http_req.payload = hb_context.status_buffer; hb_context.http_req.payload = hb_context.status_buffer;
hb_context.http_req.payload_len = hb_context.http_req.payload_len = strlen(hb_context.status_buffer);
strlen(hb_context.status_buffer);
ret = http_client_req(hb_context.sock, &hb_context.http_req, ret = http_client_req(hb_context.sock, &hb_context.http_req, HAWKBIT_RECV_TIMEOUT,
HAWKBIT_RECV_TIMEOUT, "HAWKBIT_REPORT"); "HAWKBIT_REPORT");
if (ret < 0) { if (ret < 0) {
LOG_ERR("Unable to send HTTP request (HAWKBIT_REPORT): %d", ret); LOG_ERR("Unable to send HTTP request (HAWKBIT_REPORT): %d", ret);
return false; return false;
@ -1008,8 +941,8 @@ static bool send_request(enum http_method method,
break; break;
case HAWKBIT_DOWNLOAD: case HAWKBIT_DOWNLOAD:
ret = http_client_req(hb_context.sock, &hb_context.http_req, ret = http_client_req(hb_context.sock, &hb_context.http_req, HAWKBIT_RECV_TIMEOUT,
HAWKBIT_RECV_TIMEOUT, "HAWKBIT_DOWNLOAD"); "HAWKBIT_DOWNLOAD");
if (ret < 0) { if (ret < 0) {
LOG_ERR("Unable to send HTTP request (HAWKBIT_DOWNLOAD): %d", ret); LOG_ERR("Unable to send HTTP request (HAWKBIT_DOWNLOAD): %d", ret);
return false; return false;
@ -1046,8 +979,7 @@ enum hawkbit_response hawkbit_probe(void)
goto error; goto error;
} }
if (!hawkbit_get_firmware_version(firmware_version, if (!hawkbit_get_firmware_version(firmware_version, BOOT_IMG_VER_STRLEN_MAX)) {
BOOT_IMG_VER_STRLEN_MAX)) {
hb_context.code_status = HAWKBIT_METADATA_ERROR; hb_context.code_status = HAWKBIT_METADATA_ERROR;
goto error; goto error;
} }
@ -1094,19 +1026,15 @@ enum hawkbit_response hawkbit_probe(void)
hawkbit_dump_base(&hawkbit_results.base); hawkbit_dump_base(&hawkbit_results.base);
if (hawkbit_results.base._links.cancelAction.href) { if (hawkbit_results.base._links.cancelAction.href) {
ret = hawkbit_find_cancelAction_base(&hawkbit_results.base, ret = hawkbit_find_cancelAction_base(&hawkbit_results.base, cancel_base);
cancel_base);
memset(hb_context.url_buffer, 0, sizeof(hb_context.url_buffer)); memset(hb_context.url_buffer, 0, sizeof(hb_context.url_buffer));
hb_context.dl.http_content_size = 0; hb_context.dl.http_content_size = 0;
hb_context.url_buffer_size = URL_BUFFER_SIZE; hb_context.url_buffer_size = URL_BUFFER_SIZE;
snprintk(hb_context.url_buffer, hb_context.url_buffer_size, snprintk(hb_context.url_buffer, hb_context.url_buffer_size, "%s/%s-%s/%s/feedback",
"%s/%s-%s/%s/feedback", HAWKBIT_JSON_URL, CONFIG_BOARD, HAWKBIT_JSON_URL, CONFIG_BOARD, device_id, cancel_base);
device_id, cancel_base); memset(&hawkbit_results.cancel, 0, sizeof(hawkbit_results.cancel));
memset(&hawkbit_results.cancel, 0,
sizeof(hawkbit_results.cancel));
if (!send_request(HTTP_POST, HAWKBIT_CLOSE, if (!send_request(HTTP_POST, HAWKBIT_CLOSE, HAWKBIT_STATUS_FINISHED_SUCCESS,
HAWKBIT_STATUS_FINISHED_SUCCESS,
HAWKBIT_STATUS_EXEC_CLOSED)) { HAWKBIT_STATUS_EXEC_CLOSED)) {
LOG_ERR("Send request failed (HAWKBIT_CLOSE)"); LOG_ERR("Send request failed (HAWKBIT_CLOSE)");
hb_context.code_status = HAWKBIT_NETWORKING_ERROR; hb_context.code_status = HAWKBIT_NETWORKING_ERROR;
@ -1121,12 +1049,10 @@ enum hawkbit_response hawkbit_probe(void)
memset(hb_context.url_buffer, 0, sizeof(hb_context.url_buffer)); memset(hb_context.url_buffer, 0, sizeof(hb_context.url_buffer));
hb_context.dl.http_content_size = 0; hb_context.dl.http_content_size = 0;
hb_context.url_buffer_size = URL_BUFFER_SIZE; hb_context.url_buffer_size = URL_BUFFER_SIZE;
snprintk(hb_context.url_buffer, hb_context.url_buffer_size, snprintk(hb_context.url_buffer, hb_context.url_buffer_size, "%s/%s-%s/configData",
"%s/%s-%s/configData", HAWKBIT_JSON_URL, CONFIG_BOARD, HAWKBIT_JSON_URL, CONFIG_BOARD, device_id);
device_id);
if (!send_request(HTTP_PUT, HAWKBIT_CONFIG_DEVICE, if (!send_request(HTTP_PUT, HAWKBIT_CONFIG_DEVICE, HAWKBIT_STATUS_FINISHED_SUCCESS,
HAWKBIT_STATUS_FINISHED_SUCCESS,
HAWKBIT_STATUS_EXEC_CLOSED)) { HAWKBIT_STATUS_EXEC_CLOSED)) {
LOG_ERR("Send request failed (HAWKBIT_CONFIG_DEVICE)"); LOG_ERR("Send request failed (HAWKBIT_CONFIG_DEVICE)");
hb_context.code_status = HAWKBIT_NETWORKING_ERROR; hb_context.code_status = HAWKBIT_NETWORKING_ERROR;
@ -1134,8 +1060,7 @@ enum hawkbit_response hawkbit_probe(void)
} }
} }
ret = hawkbit_find_deployment_base(&hawkbit_results.base, ret = hawkbit_find_deployment_base(&hawkbit_results.base, deployment_base);
deployment_base);
if (ret < 0) { if (ret < 0) {
hb_context.code_status = HAWKBIT_METADATA_ERROR; hb_context.code_status = HAWKBIT_METADATA_ERROR;
LOG_ERR("Unable to find URL for the device's deploymentBase: %d", ret); LOG_ERR("Unable to find URL for the device's deploymentBase: %d", ret);
@ -1151,14 +1076,12 @@ enum hawkbit_response hawkbit_probe(void)
hb_context.dl.http_content_size = 0; hb_context.dl.http_content_size = 0;
hb_context.dl.downloaded_size = 0; hb_context.dl.downloaded_size = 0;
hb_context.url_buffer_size = URL_BUFFER_SIZE; hb_context.url_buffer_size = URL_BUFFER_SIZE;
snprintk(hb_context.url_buffer, hb_context.url_buffer_size, snprintk(hb_context.url_buffer, hb_context.url_buffer_size, "%s/%s-%s/%s", HAWKBIT_JSON_URL,
"%s/%s-%s/%s", HAWKBIT_JSON_URL, CONFIG_BOARD, device_id, CONFIG_BOARD, device_id, deployment_base);
deployment_base);
memset(&hawkbit_results.dep, 0, sizeof(hawkbit_results.dep)); memset(&hawkbit_results.dep, 0, sizeof(hawkbit_results.dep));
memset(hb_context.response_data, 0, RESPONSE_BUFFER_SIZE); memset(hb_context.response_data, 0, RESPONSE_BUFFER_SIZE);
if (!send_request(HTTP_GET, HAWKBIT_PROBE_DEPLOYMENT_BASE, if (!send_request(HTTP_GET, HAWKBIT_PROBE_DEPLOYMENT_BASE, HAWKBIT_STATUS_FINISHED_NONE,
HAWKBIT_STATUS_FINISHED_NONE,
HAWKBIT_STATUS_EXEC_NONE)) { HAWKBIT_STATUS_EXEC_NONE)) {
LOG_ERR("Send request failed (HAWKBIT_PROBE_DEPLOYMENT_BASE)"); LOG_ERR("Send request failed (HAWKBIT_PROBE_DEPLOYMENT_BASE)");
hb_context.code_status = HAWKBIT_NETWORKING_ERROR; hb_context.code_status = HAWKBIT_NETWORKING_ERROR;
@ -1172,8 +1095,7 @@ enum hawkbit_response hawkbit_probe(void)
hawkbit_dump_deployment(&hawkbit_results.dep); hawkbit_dump_deployment(&hawkbit_results.dep);
hb_context.dl.http_content_size = 0; hb_context.dl.http_content_size = 0;
ret = hawkbit_parse_deployment(&hawkbit_results.dep, ret = hawkbit_parse_deployment(&hawkbit_results.dep, &hb_context.json_action_id,
&hb_context.json_action_id,
download_http, &file_size); download_http, &file_size);
if (ret < 0) { if (ret < 0) {
LOG_ERR("Unable to parse deployment base: %d", ret); LOG_ERR("Unable to parse deployment base: %d", ret);
@ -1183,18 +1105,15 @@ enum hawkbit_response hawkbit_probe(void)
nvs_read(&fs, ADDRESS_ID, &action_id, sizeof(action_id)); nvs_read(&fs, ADDRESS_ID, &action_id, sizeof(action_id));
if (action_id == (int32_t)hb_context.json_action_id) { if (action_id == (int32_t)hb_context.json_action_id) {
LOG_INF("Preventing repeated attempt to install %d", LOG_INF("Preventing repeated attempt to install %d", hb_context.json_action_id);
hb_context.json_action_id);
hb_context.dl.http_content_size = 0; hb_context.dl.http_content_size = 0;
memset(hb_context.url_buffer, 0, sizeof(hb_context.url_buffer)); memset(hb_context.url_buffer, 0, sizeof(hb_context.url_buffer));
hb_context.url_buffer_size = URL_BUFFER_SIZE; hb_context.url_buffer_size = URL_BUFFER_SIZE;
snprintk(hb_context.url_buffer, hb_context.url_buffer_size, snprintk(hb_context.url_buffer, hb_context.url_buffer_size,
"%s/%s-%s/deploymentBase/%d/feedback", "%s/%s-%s/deploymentBase/%d/feedback", HAWKBIT_JSON_URL, CONFIG_BOARD,
HAWKBIT_JSON_URL, CONFIG_BOARD, device_id, device_id, hb_context.json_action_id);
hb_context.json_action_id);
if (!send_request(HTTP_POST, HAWKBIT_REPORT, if (!send_request(HTTP_POST, HAWKBIT_REPORT, HAWKBIT_STATUS_FINISHED_SUCCESS,
HAWKBIT_STATUS_FINISHED_SUCCESS,
HAWKBIT_STATUS_EXEC_CLOSED)) { HAWKBIT_STATUS_EXEC_CLOSED)) {
LOG_ERR("Send request failed (HAWKBIT_REPORT)"); LOG_ERR("Send request failed (HAWKBIT_REPORT)");
hb_context.code_status = HAWKBIT_NETWORKING_ERROR; hb_context.code_status = HAWKBIT_NETWORKING_ERROR;
@ -1211,8 +1130,7 @@ enum hawkbit_response hawkbit_probe(void)
memset(hb_context.url_buffer, 0, sizeof(hb_context.url_buffer)); memset(hb_context.url_buffer, 0, sizeof(hb_context.url_buffer));
hb_context.url_buffer_size = URL_BUFFER_SIZE; hb_context.url_buffer_size = URL_BUFFER_SIZE;
snprintk(hb_context.url_buffer, hb_context.url_buffer_size, "%s", snprintk(hb_context.url_buffer, hb_context.url_buffer_size, "%s", download_http);
download_http);
flash_img_init(&hb_context.flash_ctx); flash_img_init(&hb_context.flash_ctx);