Created
June 26, 2024 12:45
-
-
Save noahp/f5b54c6b9035fb8f391b3d7d18a83c39 to your computer and use it in GitHub Desktop.
Golioth Memfault chunk upload snippet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <zephyr/logging/log.h> | |
LOG_MODULE_REGISTER(golioth_memfault, LOG_LEVEL_DBG); | |
#include <golioth/stream.h> | |
#include <memfault/components.h> | |
/* Callback invoked by the Golioth SDK to read the next block of Memfault | |
data to send to Goliot */ | |
static enum golioth_status read_memfault_chunk(uint32_t block_idx, | |
uint8_t *block_buffer, | |
size_t *block_size, | |
bool *is_last, | |
void *arg) | |
{ | |
eMemfaultPacketizerStatus mflt_status; | |
mflt_status = memfault_packetizer_get_next(block_buffer, block_size); | |
if (kMemfaultPacketizerStatus_NoMoreData == mflt_status) | |
{ | |
/* We should not hit this, but can recover */ | |
LOG_WRN("Unexpected end of Memfault data"); | |
*block_size = 0; | |
*is_last = true; | |
} | |
else if (kMemfaultPacketizerStatus_EndOfChunk == mflt_status) | |
{ | |
/* Last block */ | |
*is_last = true; | |
} | |
else if (kMemfaultPacketizerStatus_MoreDataForChunk == mflt_status) | |
{ | |
*is_last = false; | |
} | |
return GOLIOTH_OK; | |
} | |
int golioth_memfault_upload(struct golioth_client *client) | |
{ | |
/* Configure the Memfault SDK to read maximum sized chunks across | |
multiple calls */ | |
sPacketizerConfig cfg = { | |
.enable_multi_packet_chunk = true, | |
}; | |
sPacketizerMetadata metadata = {}; | |
bool data_available = memfault_packetizer_begin(&cfg, &metadata); | |
while (data_available) | |
{ | |
LOG_DBG("Uploading memfault data"); | |
/* TODO make this non-blocking */ | |
golioth_stream_set_blockwise_sync(client, | |
"mflt", | |
GOLIOTH_CONTENT_TYPE_OCTET_STREAM, | |
read_memfault_chunk, | |
NULL); | |
/* Check for additional chunks */ | |
data_available = memfault_packetizer_begin(&cfg, &metadata); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment