1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-01-18 12:52:44 +01:00

sys/suit: move suit_storage_helper() to handlers_command_seq.c

This commit is contained in:
Benjamin Valentin 2022-05-02 23:30:16 +02:00
parent 3e531801e6
commit 1662f0fa46
3 changed files with 74 additions and 85 deletions

View File

@ -325,22 +325,6 @@ static inline bool suit_component_check_flag(suit_component_t *component,
int suit_component_name_to_string(const suit_manifest_t *manifest,
const suit_component_t *component,
char separator, char *buf, size_t buf_len);
/**
* @brief Helper function for writing bytes on flash a specified offset
*
* @param[in] arg ptr to the SUIT manifest
* @param[in] offset offset to write to on flash
* @param[in] buf bytes to write
* @param[in] len length of bytes to write
* @param[in] more whether more data is coming
*
* @return 0 on success
* @return <0 on error
*/
int suit_storage_helper(void *arg, size_t offset, uint8_t *buf, size_t len,
int more);
#ifdef __cplusplus
}
#endif

View File

@ -41,8 +41,15 @@
#endif
#include "suit/transport/mock.h"
#if defined(MODULE_PROGRESS_BAR)
#include "progress_bar.h"
#endif
#include "log.h"
#define ENABLE_DEBUG 0
#include "debug.h"
static int _get_component_size(suit_manifest_t *manifest,
suit_component_t *comp,
uint32_t *img_size)
@ -312,6 +319,72 @@ static int _start_storage(suit_manifest_t *manifest, suit_component_t *comp)
return suit_storage_start(comp->storage_backend, manifest, img_size);
}
static inline void _print_download_progress(suit_manifest_t *manifest,
size_t offset, size_t len,
size_t image_size)
{
(void)manifest;
(void)offset;
(void)len;
DEBUG("_suit_flashwrite(): writing %u bytes at pos %u\n", len, offset);
#if defined(MODULE_PROGRESS_BAR)
if (image_size != 0) {
char _suffix[7] = { 0 };
uint8_t _progress = 100 * (offset + len) / image_size;
sprintf(_suffix, " %3d%%", _progress);
progress_bar_print("Fetching firmware ", _suffix, _progress);
if (_progress == 100) {
puts("");
}
}
#else
(void) image_size;
#endif
}
static int _storage_helper(void *arg, size_t offset, uint8_t *buf, size_t len,
int more)
{
suit_manifest_t *manifest = (suit_manifest_t *)arg;
uint32_t image_size;
nanocbor_value_t param_size;
size_t total = offset + len;
suit_component_t *comp = &manifest->components[manifest->component_current];
suit_param_ref_t *ref_size = &comp->param_size;
/* Grab the total image size from the manifest */
if ((suit_param_ref_to_cbor(manifest, ref_size, &param_size) == 0) ||
(nanocbor_get_uint32(&param_size, &image_size) < 0)) {
/* Early exit if the total image size can't be determined */
return -1;
}
if (image_size < offset + len) {
/* Extra newline at the start to compensate for the progress bar */
LOG_ERROR(
"\n_suit_coap(): Image beyond size, offset + len=%u, "
"image_size=%u\n", (unsigned)(total), (unsigned)image_size);
return -1;
}
if (!more && image_size != total) {
LOG_INFO("Incorrect size received, got %u, expected %u\n",
(unsigned)total, (unsigned)image_size);
return -1;
}
_print_download_progress(manifest, offset, len, image_size);
int res = suit_storage_write(comp->storage_backend, manifest, buf, offset, len);
if (!more) {
LOG_INFO("Finalizing payload store\n");
/* Finalize the write if no more data available */
res = suit_storage_finish(comp->storage_backend, manifest);
}
return res;
}
static int _dtv_fetch(suit_manifest_t *manifest, int key,
nanocbor_value_t *_it)
{
@ -360,7 +433,7 @@ static int _dtv_fetch(suit_manifest_t *manifest, int key,
#ifdef MODULE_SUIT_TRANSPORT_COAP
else if (strncmp(manifest->urlbuf, "coap://", 7) == 0) {
res = nanocoap_get_blockwise_url(manifest->urlbuf, CONFIG_SUIT_COAP_BLOCKSIZE,
suit_storage_helper,
_storage_helper,
manifest);
}
#endif

View File

@ -77,31 +77,6 @@ static char _stack[SUIT_COAP_STACKSIZE];
static char _url[SUIT_URL_MAX];
static uint8_t _manifest_buf[SUIT_MANIFEST_BUFSIZE];
#ifdef MODULE_SUIT
static inline void _print_download_progress(suit_manifest_t *manifest,
size_t offset, size_t len,
size_t image_size)
{
(void)manifest;
(void)offset;
(void)len;
DEBUG("_suit_flashwrite(): writing %u bytes at pos %u\n", len, offset);
#if defined(MODULE_PROGRESS_BAR)
if (image_size != 0) {
char _suffix[7] = { 0 };
uint8_t _progress = 100 * (offset + len) / image_size;
sprintf(_suffix, " %3d%%", _progress);
progress_bar_print("Fetching firmware ", _suffix, _progress);
if (_progress == 100) {
puts("");
}
}
#else
(void) image_size;
#endif
}
#endif
static kernel_pid_t _suit_coap_pid;
static void _suit_handle_url(const char *url, coap_blksize_t blksize)
@ -149,49 +124,6 @@ static void _suit_handle_url(const char *url, coap_blksize_t blksize)
}
}
int suit_storage_helper(void *arg, size_t offset, uint8_t *buf, size_t len,
int more)
{
suit_manifest_t *manifest = (suit_manifest_t *)arg;
uint32_t image_size;
nanocbor_value_t param_size;
size_t total = offset + len;
suit_component_t *comp = &manifest->components[manifest->component_current];
suit_param_ref_t *ref_size = &comp->param_size;
/* Grab the total image size from the manifest */
if ((suit_param_ref_to_cbor(manifest, ref_size, &param_size) == 0) ||
(nanocbor_get_uint32(&param_size, &image_size) < 0)) {
/* Early exit if the total image size can't be determined */
return -1;
}
if (image_size < offset + len) {
/* Extra newline at the start to compensate for the progress bar */
LOG_ERROR(
"\n_suit_coap(): Image beyond size, offset + len=%u, "
"image_size=%u\n", (unsigned)(total), (unsigned)image_size);
return -1;
}
if (!more && image_size != total) {
LOG_INFO("Incorrect size received, got %u, expected %u\n",
(unsigned)total, (unsigned)image_size);
return -1;
}
_print_download_progress(manifest, offset, len, image_size);
int res = suit_storage_write(comp->storage_backend, manifest, buf, offset, len);
if (!more) {
LOG_INFO("Finalizing payload store\n");
/* Finalize the write if no more data available */
res = suit_storage_finish(comp->storage_backend, manifest);
}
return res;
}
static void *_suit_coap_thread(void *arg)
{
(void)arg;