Lines Matching +full:max +full:- +full:current +full:- +full:300

2  * Copyright (c) 2016-2017 Linaro Limited
6 * Copyright (c) 2021 G-Technologies Sdn. Bhd.
9 * SPDX-License-Identifier: Apache-2.0
43 #define URL_BUFFER_SIZE 300
47 #define HAWKBIT_RECV_TIMEOUT (300 * MSEC_PER_SEC)
48 #define HAWKBIT_SET_SERVER_TIMEOUT K_MSEC(300)
52 #define HTTP_HEADER_CONTENT_TYPE_JSON "application/json;charset=UTF-8"
228 JSON_OBJ_DESCR_OBJECT_NAMED(struct hawkbit_dep_res_links, "download-http", download_http,
230 JSON_OBJ_DESCR_OBJECT_NAMED(struct hawkbit_dep_res_links, "md5sum-http", md5sum_http,
275 return -EINVAL; in hawkbit_settings_set()
290 return -EINVAL; in hawkbit_settings_set()
304 return -EINVAL; in hawkbit_settings_set()
327 return -EINVAL; in hawkbit_settings_set()
347 return -ENOENT; in hawkbit_settings_set()
375 if (cb->event == event && cb->handler) { in hawkbit_event_raise()
376 cb->handler(cb, event); in hawkbit_event_raise()
387 if (cb == NULL || cb->handler == NULL) { in hawkbit_event_add_callback()
388 return -EINVAL; in hawkbit_event_add_callback()
393 sys_slist_prepend(&event_callbacks, &cb->node); in hawkbit_event_add_callback()
403 if (cb == NULL || cb->handler == NULL) { in hawkbit_event_remove_callback()
404 return -EINVAL; in hawkbit_event_remove_callback()
409 if (!sys_slist_find_and_remove(&event_callbacks, &cb->node)) { in hawkbit_event_remove_callback()
410 ret = -EINVAL; in hawkbit_event_remove_callback()
421 int ret = -1; in start_http_client()
435 while (resolve_attempts--) { in start_http_client()
449 *hb_sock = zsock_socket(addr->ai_family, SOCK_STREAM, protocol); in start_http_client()
472 if (zsock_connect(*hb_sock, addr->ai_addr, addr->ai_addrlen) < 0) { in start_http_client()
504 return -1; in hawkbit_time2sec()
556 return -EIO; in hawkbit_device_acid_update()
571 return -EAGAIN; in hawkbit_reset_action_id()
591 const char *sleep = hawkbit_res->config.polling.sleep; in hawkbit_update_sleep()
628 helper = strstr(res->_links.cancelAction.href, "cancelAction/"); in hawkbit_find_cancel_action_id()
631 LOG_ERR("Missing %s/ in href %s", "cancelAction", res->_links.cancelAction.href); in hawkbit_find_cancel_action_id()
632 return -EINVAL; in hawkbit_find_cancel_action_id()
640 return -EINVAL; in hawkbit_find_cancel_action_id()
650 id = strtol(res->id, NULL, 10); in hawkbit_deployment_get_action_id()
653 return -EINVAL; in hawkbit_deployment_get_action_id()
673 num_chunks = res->deployment.num_chunks; in hawkbit_parse_deployment()
676 return -ENOSPC; in hawkbit_parse_deployment()
679 chunk = &res->deployment.chunks[0]; in hawkbit_parse_deployment()
680 if (strcmp("bApp", chunk->part)) { in hawkbit_parse_deployment()
681 LOG_ERR("Only part 'bApp' is supported; got %s", chunk->part); in hawkbit_parse_deployment()
682 return -EINVAL; in hawkbit_parse_deployment()
685 num_artifacts = chunk->num_artifacts; in hawkbit_parse_deployment()
688 return -EINVAL; in hawkbit_parse_deployment()
691 artifact = &chunk->artifacts[0]; in hawkbit_parse_deployment()
692 if (hex2bin(artifact->hashes.sha256, SHA256_HASH_SIZE << 1, hb_context->dl.file_hash, in hawkbit_parse_deployment()
693 sizeof(hb_context->dl.file_hash)) != SHA256_HASH_SIZE) { in hawkbit_parse_deployment()
694 return -EINVAL; in hawkbit_parse_deployment()
697 hb_context->dl.file_size = artifact->size; in hawkbit_parse_deployment()
699 if (hb_context->dl.file_size > SLOT1_SIZE) { in hawkbit_parse_deployment()
700 LOG_ERR("Artifact file size too big (got %d, max is %d)", hb_context->dl.file_size, in hawkbit_parse_deployment()
702 return -ENOSPC; in hawkbit_parse_deployment()
706 * Find the download-http href. in hawkbit_parse_deployment()
708 href = artifact->_links.download_http.href; in hawkbit_parse_deployment()
710 LOG_ERR("Missing expected %s href", "download-http"); in hawkbit_parse_deployment()
711 return -EINVAL; in hawkbit_parse_deployment()
719 return -EINVAL; in hawkbit_parse_deployment()
727 struct hawkbit_dep_res_chunk *c = &d->deployment.chunks[0]; in hawkbit_dump_deployment()
728 struct hawkbit_dep_res_arts *a = &c->artifacts[0]; in hawkbit_dump_deployment()
729 struct hawkbit_dep_res_links *l = &a->_links; in hawkbit_dump_deployment()
731 LOG_DBG("%s=%s", "id", d->id); in hawkbit_dump_deployment()
732 LOG_DBG("%s=%s", "download", d->deployment.download); in hawkbit_dump_deployment()
733 LOG_DBG("%s=%s", "update", d->deployment.update); in hawkbit_dump_deployment()
734 LOG_DBG("chunks[0].%s=%s", "part", c->part); in hawkbit_dump_deployment()
735 LOG_DBG("chunks[0].%s=%s", "name", c->name); in hawkbit_dump_deployment()
736 LOG_DBG("chunks[0].%s=%s", "version", c->version); in hawkbit_dump_deployment()
737 LOG_DBG("chunks[0].artifacts[0].%s=%s", "filename", a->filename); in hawkbit_dump_deployment()
738 LOG_DBG("chunks[0].artifacts[0].%s=%s", "hashes.sha1", a->hashes.sha1); in hawkbit_dump_deployment()
739 LOG_DBG("chunks[0].artifacts[0].%s=%s", "hashes.md5", a->hashes.md5); in hawkbit_dump_deployment()
740 LOG_DBG("chunks[0].artifacts[0].%s=%s", "hashes.sha256", a->hashes.sha256); in hawkbit_dump_deployment()
741 LOG_DBG("chunks[0].size=%d", a->size); in hawkbit_dump_deployment()
742 LOG_DBG("%s=%s", "download-http", l->download_http.href); in hawkbit_dump_deployment()
743 LOG_DBG("%s=%s", "md5sum-http", l->md5sum_http.href); in hawkbit_dump_deployment()
751 return -EINVAL; in hawkbit_set_custom_data_cb()
758 return -ENOTSUP; in hawkbit_set_custom_data_cb()
776 if (config->server_addr != NULL) { in hawkbit_set_config()
777 strncpy(hb_cfg.server_addr, config->server_addr, in hawkbit_set_config()
781 if (config->server_port != 0) { in hawkbit_set_config()
783 config->server_port); in hawkbit_set_config()
787 if (config->auth_token != NULL) { in hawkbit_set_config()
788 strncpy(hb_cfg.ddi_security_token, config->auth_token, in hawkbit_set_config()
795 if (config->tls_tag != 0) { in hawkbit_set_config()
796 hb_cfg.tls_tag = config->tls_tag; in hawkbit_set_config()
804 return -EAGAIN; in hawkbit_set_config()
844 LOG_DBG("Current action_id: %d", hb_cfg.action_id); in hawkbit_init()
847 LOG_INF("Current image is%s confirmed", image_ok ? "" : " not"); in hawkbit_init()
851 LOG_ERR("Failed to confirm current image: %d", ret); in hawkbit_init()
855 LOG_DBG("Marked current image as OK"); in hawkbit_init()
874 if (rsp->http_status_code != 200) { in response_cb()
875 LOG_ERR("HTTP request denied: %d", rsp->http_status_code); in response_cb()
876 if (rsp->http_status_code == 401 || rsp->http_status_code == 403) { in response_cb()
877 hb_context->code_status = HAWKBIT_PERMISSION_ERROR; in response_cb()
879 hb_context->code_status = HAWKBIT_METADATA_ERROR; in response_cb()
884 switch (hb_context->type) { in response_cb()
887 if (hb_context->dl.http_content_size == 0) { in response_cb()
888 hb_context->dl.http_content_size = rsp->content_length; in response_cb()
891 if (rsp->body_found) { in response_cb()
892 body_data = rsp->body_frag_start; in response_cb()
893 body_len = rsp->body_frag_len; in response_cb()
895 if ((hb_context->dl.downloaded_size + body_len) > in response_cb()
896 hb_context->response_data_size) { in response_cb()
897 hb_context->response_data_size = in response_cb()
898 hb_context->dl.downloaded_size + body_len; in response_cb()
899 rsp_tmp = k_realloc(hb_context->response_data, in response_cb()
900 hb_context->response_data_size); in response_cb()
903 hb_context->code_status = HAWKBIT_ALLOC_ERROR; in response_cb()
907 hb_context->response_data = rsp_tmp; in response_cb()
909 strncpy(hb_context->response_data + hb_context->dl.downloaded_size, in response_cb()
911 hb_context->dl.downloaded_size += body_len; in response_cb()
915 if (hb_context->dl.http_content_size != hb_context->dl.downloaded_size) { in response_cb()
917 hb_context->dl.http_content_size, in response_cb()
918 hb_context->dl.downloaded_size); in response_cb()
919 hb_context->code_status = HAWKBIT_METADATA_ERROR; in response_cb()
923 hb_context->response_data[hb_context->dl.downloaded_size] = '\0'; in response_cb()
924 memset(&hb_context->results, 0, sizeof(hb_context->results)); in response_cb()
925 if (hb_context->type == HAWKBIT_PROBE) { in response_cb()
927 hb_context->response_data, hb_context->dl.downloaded_size, in response_cb()
929 &hb_context->results.base); in response_cb()
932 hb_context->code_status = HAWKBIT_METADATA_ERROR; in response_cb()
936 hb_context->response_data, hb_context->dl.downloaded_size, in response_cb()
938 &hb_context->results.dep); in response_cb()
941 hb_context->code_status = HAWKBIT_METADATA_ERROR; in response_cb()
949 if (hb_context->dl.http_content_size == 0) { in response_cb()
950 hb_context->dl.http_content_size = rsp->content_length; in response_cb()
953 if (rsp->body_found) { in response_cb()
954 body_data = rsp->body_frag_start; in response_cb()
955 body_len = rsp->body_frag_len; in response_cb()
957 ret = flash_img_buffered_write(&hb_context->flash_ctx, body_data, body_len, in response_cb()
961 hb_context->code_status = HAWKBIT_DOWNLOAD_ERROR; in response_cb()
966 hb_context->dl.downloaded_size = flash_img_bytes_written(&hb_context->flash_ctx); in response_cb()
969 hb_context->dl.downloaded_size * 100 / hb_context->dl.http_content_size; in response_cb()
971 if (downloaded > hb_context->dl.download_progress) { in response_cb()
972 hb_context->dl.download_progress = downloaded; in response_cb()
973 LOG_DBG("Downloaded: %d%% ", hb_context->dl.download_progress); in response_cb()
977 hb_context->final_data_received = true; in response_cb()
996 char header[DDI_SECURITY_TOKEN_SIZE + sizeof(AUTH_HEADER_START) + sizeof(HTTP_CRLF) - 1]; in send_request()
1015 hb_context->final_data_received = false; in send_request()
1016 hb_context->type = type; in send_request()
1065 hb_context->dl.http_content_size = 0; in send_request()
1066 hb_context->dl.downloaded_size = 0; in send_request()
1074 ret = http_client_req(hb_context->sock, &http_req, HAWKBIT_RECV_TIMEOUT, hb_context); in send_request()
1077 hb_context->code_status = HAWKBIT_NETWORKING_ERROR; in send_request()
1081 if (IN_RANGE(hb_context->code_status, HAWKBIT_NETWORKING_ERROR, HAWKBIT_ALLOC_ERROR)) { in send_request()
1150 LOG_ERR("Current image is not confirmed"); in s_start()
1156 if (!hawkbit_get_device_identity(s->device_id, DEVICE_ID_HEX_MAX_SIZE)) { in s_start()
1173 if (!start_http_client(&s->hb_context.sock)) { in s_http_start()
1174 s->hb_context.code_status = HAWKBIT_NETWORKING_ERROR; in s_http_start()
1178 s->hb_context.response_data_size = RESPONSE_BUFFER_SIZE; in s_http_start()
1180 s->hb_context.response_data = k_calloc(s->hb_context.response_data_size, sizeof(uint8_t)); in s_http_start()
1181 if (s->hb_context.response_data == NULL) { in s_http_start()
1182 cleanup_connection(&s->hb_context.sock); in s_http_start()
1183 s->hb_context.code_status = HAWKBIT_ALLOC_ERROR; in s_http_start()
1192 cleanup_connection(&s->hb_context.sock); in s_http_end()
1193 k_free(s->hb_context.response_data); in s_http_end()
1207 snprintk(url_buffer, sizeof(url_buffer), "%s/%s-%s", HAWKBIT_JSON_URL, CONFIG_BOARD, in s_probe()
1208 s->device_id); in s_probe()
1210 if (!send_request(&s->hb_context, HAWKBIT_PROBE, url_buffer, NULL)) { in s_probe()
1216 if (s->hb_context.results.base.config.polling.sleep) { in s_probe()
1218 LOG_DBG("config.polling.sleep=%s", s->hb_context.results.base.config.polling.sleep); in s_probe()
1219 hawkbit_update_sleep(&s->hb_context.results.base); in s_probe()
1222 if (s->hb_context.results.base._links.cancelAction.href) { in s_probe()
1224 s->hb_context.results.base._links.cancelAction.href); in s_probe()
1226 } else if (s->hb_context.results.base._links.configData.href) { in s_probe()
1228 s->hb_context.results.base._links.configData.href); in s_probe()
1230 } else if (s->hb_context.results.base._links.deploymentBase.href) { in s_probe()
1232 s->hb_context.results.base._links.deploymentBase.href); in s_probe()
1235 s->hb_context.code_status = HAWKBIT_NO_UPDATE; in s_probe()
1255 cancel_base = hawkbit_get_url(s->hb_context.results.base._links.cancelAction.href); in s_cancel()
1258 s->hb_context.code_status = HAWKBIT_METADATA_ERROR; in s_cancel()
1265 ret = hawkbit_find_cancel_action_id(&s->hb_context.results.base, &cancel_action_id); in s_cancel()
1268 s->hb_context.code_status = HAWKBIT_METADATA_ERROR; in s_cancel()
1282 s->hb_context.code_status = HAWKBIT_METADATA_ERROR; in s_cancel()
1287 if (!send_request(&s->hb_context, HAWKBIT_CANCEL, url_buffer, status_buffer)) { in s_cancel()
1314 url_buffer = hawkbit_get_url(s->hb_context.results.base._links.configData.href); in s_config_device()
1317 s->hb_context.code_status = HAWKBIT_METADATA_ERROR; in s_config_device()
1322 ret = hawkbit_config_device_data_cb_handler(s->device_id, status_buffer, in s_config_device()
1326 s->hb_context.code_status = HAWKBIT_METADATA_ERROR; in s_config_device()
1331 if (!send_request(&s->hb_context, HAWKBIT_CONFIG_DEVICE, url_buffer, status_buffer)) { in s_config_device()
1350 url_buffer = hawkbit_get_url(s->hb_context.results.base._links.deploymentBase.href); in s_probe_deployment_base()
1353 s->hb_context.code_status = HAWKBIT_METADATA_ERROR; in s_probe_deployment_base()
1358 if (!send_request(&s->hb_context, HAWKBIT_PROBE_DEPLOYMENT_BASE, url_buffer, NULL)) { in s_probe_deployment_base()
1364 hawkbit_dump_deployment(&s->hb_context.results.dep); in s_probe_deployment_base()
1366 ret = hawkbit_deployment_get_action_id(&s->hb_context.results.dep, in s_probe_deployment_base()
1367 &s->hb_context.json_action_id); in s_probe_deployment_base()
1369 s->hb_context.code_status = HAWKBIT_METADATA_ERROR; in s_probe_deployment_base()
1374 if (hb_cfg.action_id == s->hb_context.json_action_id) { in s_probe_deployment_base()
1398 snprintk(url_buffer, sizeof(url_buffer), "%s/%s-%s/%s/%d/%s", HAWKBIT_JSON_URL, in s_report()
1399 CONFIG_BOARD, s->device_id, "deploymentBase", s->hb_context.json_action_id, in s_report()
1404 s->hb_context.json_action_id); in s_report()
1410 s->hb_context.code_status = HAWKBIT_METADATA_ERROR; in s_report()
1415 if (!send_request(&s->hb_context, HAWKBIT_REPORT, url_buffer, status_buffer)) { in s_report()
1447 ret = hawkbit_parse_deployment(&s->hb_context, &s->hb_context.results.dep, &url_buffer); in s_download()
1450 s->hb_context.code_status = HAWKBIT_METADATA_ERROR; in s_download()
1455 flash_img_init(&s->hb_context.flash_ctx); in s_download()
1460 flash_area_ptr = s->hb_context.flash_ctx.flash_area; in s_download()
1462 if (!send_request(&s->hb_context, HAWKBIT_DOWNLOAD, url_buffer, NULL)) { in s_download()
1469 if (!s->hb_context.final_data_received) { in s_download()
1471 s->hb_context.code_status = HAWKBIT_DOWNLOAD_ERROR; in s_download()
1477 fic.match = s->hb_context.dl.file_hash; in s_download()
1478 fic.clen = s->hb_context.dl.downloaded_size; in s_download()
1479 if (flash_img_check(&s->hb_context.flash_ctx, &fic, flash_area_ptr->fa_id)) { in s_download()
1481 s->hb_context.code_status = HAWKBIT_DOWNLOAD_ERROR; in s_download()
1489 s->hb_context.code_status = HAWKBIT_DOWNLOAD_ERROR; in s_download()
1495 s->hb_context.code_status = HAWKBIT_UPDATE_INSTALLED; in s_download()
1496 hawkbit_device_acid_update(s->hb_context.json_action_id); in s_download()
1507 if (IN_RANGE(s->hb_context.code_status, HAWKBIT_NETWORKING_ERROR, in s_terminate()
1510 switch (s->hb_context.code_status) { in s_terminate()
1536 smf_set_terminate(SMF_CTX(s), s->hb_context.code_status); in s_terminate()