|
| 1 | +//! @file |
| 2 | +//! |
| 3 | +//! Copyright (c) Memfault, Inc. |
| 4 | +//! See License.txt for details |
| 5 | +//! |
| 6 | +//! Implements sMemfaultDataSourceImpl API specified in data_packetizer_source.h to serialize a |
| 7 | +//! custom data recording such that it can be published to the Memfault cloud. |
| 8 | + |
| 9 | +#include "memfault/core/custom_data_recording.h" |
| 10 | +#include "memfault_custom_data_recording_private.h" |
| 11 | + |
| 12 | +#include <string.h> |
| 13 | +#include <stddef.h> |
| 14 | +#include <stdint.h> |
| 15 | + |
| 16 | +#include "memfault/config.h" |
| 17 | +#include "memfault/core/data_packetizer_source.h" |
| 18 | +#include "memfault/core/debug_log.h" |
| 19 | +#include "memfault/core/math.h" |
| 20 | +#include "memfault/core/sdk_assert.h" |
| 21 | +#include "memfault/core/serializer_helper.h" |
| 22 | +#include "memfault/core/serializer_key_ids.h" |
| 23 | +#include "memfault/util/cbor.h" |
| 24 | + |
| 25 | +#if MEMFAULT_CDR_ENABLE |
| 26 | + |
| 27 | +// Holds CDR metadata -- we pre-serialize in prv_has_cdr to avoid an egregious amount of calls to |
| 28 | +// read out the metadata when a small chunk size is used by a platform |
| 29 | +typedef struct { |
| 30 | + size_t length; |
| 31 | + uint8_t data[MEMFAULT_CDR_MAX_ENCODED_METADATA_LEN]; |
| 32 | +} sMemfaultCdrEncodedMetadata; |
| 33 | + |
| 34 | +static const sMemfaultCdrSourceImpl *s_cdr_sources[MEMFAULT_CDR_MAX_DATA_SOURCES]; |
| 35 | + |
| 36 | +typedef struct { |
| 37 | + sMemfaultCborEncoder encoder; |
| 38 | + const sMemfaultCdrSourceImpl *active_source; |
| 39 | + size_t total_encode_len; |
| 40 | + sMemfaultCdrMetadata active_metadata; |
| 41 | + sMemfaultCdrEncodedMetadata encoded_metadata; |
| 42 | +} sMfltCdrSourceCtx; |
| 43 | + |
| 44 | +static sMfltCdrSourceCtx s_memfault_cdr_source_ctx; |
| 45 | + |
| 46 | +static bool prv_encode_cdr_metadata(sMemfaultCborEncoder *encoder, sMfltCdrSourceCtx *cdr_ctx) { |
| 47 | + const sMemfaultCdrMetadata *metadata = &cdr_ctx->active_metadata; |
| 48 | + |
| 49 | + if (!memfault_serializer_helper_encode_metadata_with_time( |
| 50 | + encoder, kMemfaultEventType_Cdr, &metadata->start_time)) { |
| 51 | + return false; |
| 52 | + } |
| 53 | + if (!memfault_cbor_encode_unsigned_integer(encoder, kMemfaultEventKey_EventInfo)) { |
| 54 | + return false; |
| 55 | + } |
| 56 | + |
| 57 | + const size_t cdr_num_pairs = |
| 58 | + 1 /* mime types array */ + |
| 59 | + 1 /* duration ms */ + |
| 60 | + 1 /* collection reason */ + |
| 61 | + 1 /* recording itself */; |
| 62 | + |
| 63 | + memfault_cbor_encode_dictionary_begin(encoder, cdr_num_pairs); |
| 64 | + |
| 65 | + if (!memfault_serializer_helper_encode_uint32_kv_pair( |
| 66 | + encoder, kMemfaultCdrInfoKey_DurationMs, metadata->duration_ms)) { |
| 67 | + return false; |
| 68 | + } |
| 69 | + |
| 70 | + if (!memfault_cbor_encode_unsigned_integer(encoder, kMemfaultCdrInfoKey_Mimetypes) || |
| 71 | + !memfault_cbor_encode_array_begin(encoder, metadata->num_mimetypes)) { |
| 72 | + return false; |
| 73 | + } |
| 74 | + |
| 75 | + for (size_t i = 0; i < metadata->num_mimetypes; i++) { |
| 76 | + if (!memfault_cbor_encode_string(encoder, metadata->mimetypes[i])) { |
| 77 | + return false; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + if (!memfault_cbor_encode_unsigned_integer(encoder, kMemfaultCdrInfoKey_Reason) || |
| 82 | + !memfault_cbor_encode_string(encoder, metadata->collection_reason)) { |
| 83 | + return false; |
| 84 | + } |
| 85 | + |
| 86 | + if (!memfault_cbor_encode_unsigned_integer(encoder, kMemfaultCdrInfoKey_Data)) { |
| 87 | + return false; |
| 88 | + } |
| 89 | + |
| 90 | + if (!memfault_cbor_encode_byte_string_begin(encoder, metadata->data_size_bytes)) { |
| 91 | + return false; |
| 92 | + } |
| 93 | + |
| 94 | + // Note: at this point all that's left to encode is the binary blob itself as a byte string |
| 95 | + return true; |
| 96 | +} |
| 97 | + |
| 98 | +static void prv_try_get_cdr_source_with_data(sMfltCdrSourceCtx *ctx) { |
| 99 | + if (ctx->active_source != NULL) { |
| 100 | + // a source is already active |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + for (size_t i = 0; i < MEMFAULT_ARRAY_SIZE(s_cdr_sources); i++) { |
| 105 | + const sMemfaultCdrSourceImpl *source = s_cdr_sources[i]; |
| 106 | + if (source == NULL) { |
| 107 | + continue; |
| 108 | + } |
| 109 | + |
| 110 | + if (source->has_cdr_cb(&ctx->active_metadata)) { |
| 111 | + ctx->active_source = source; |
| 112 | + return; |
| 113 | + } |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +static void prv_fill_header_cb(void *ctx, uint32_t offset, const void *buf, size_t buf_len) { |
| 118 | + uint8_t *header_buf = (uint8_t *)ctx; |
| 119 | + memcpy(&header_buf[offset], buf, buf_len); |
| 120 | +} |
| 121 | + |
| 122 | +static bool prv_has_cdr(size_t *total_size) { |
| 123 | + sMfltCdrSourceCtx *cdr_ctx = &s_memfault_cdr_source_ctx; |
| 124 | + |
| 125 | + prv_try_get_cdr_source_with_data(cdr_ctx); |
| 126 | + if (cdr_ctx->active_source == NULL) { |
| 127 | + return false; |
| 128 | + } |
| 129 | + |
| 130 | + sMemfaultCborEncoder encoder; |
| 131 | + memfault_cbor_encoder_init(&encoder, prv_fill_header_cb, cdr_ctx->encoded_metadata.data, |
| 132 | + sizeof(cdr_ctx->encoded_metadata.data)); |
| 133 | + |
| 134 | + if (!prv_encode_cdr_metadata(&encoder, cdr_ctx)) { |
| 135 | + MEMFAULT_LOG_ERROR("Not enough storage to serialized CDR, increase MEMFAULT_CDR_MAX_ENCODED_METADATA_LEN"); |
| 136 | + return false; |
| 137 | + } |
| 138 | + |
| 139 | + cdr_ctx->encoded_metadata.length = memfault_cbor_encoder_deinit(&encoder); |
| 140 | + cdr_ctx->total_encode_len = cdr_ctx->encoded_metadata.length + cdr_ctx->active_metadata.data_size_bytes; |
| 141 | + *total_size = cdr_ctx->total_encode_len; |
| 142 | + return true; |
| 143 | +} |
| 144 | + |
| 145 | +static bool prv_cdr_read(uint32_t offset, void *buf, size_t buf_len) { |
| 146 | + sMfltCdrSourceCtx *cdr_ctx = &s_memfault_cdr_source_ctx; |
| 147 | + if (cdr_ctx->active_source == NULL) { |
| 148 | + return false; |
| 149 | + } |
| 150 | + |
| 151 | + if ((offset + buf_len) > cdr_ctx->total_encode_len) { |
| 152 | + return false; |
| 153 | + } |
| 154 | + |
| 155 | + uint8_t *bufp = (uint8_t *)buf; |
| 156 | + if (offset < cdr_ctx->encoded_metadata.length) { |
| 157 | + const size_t metadata_bytes_to_copy = MEMFAULT_MIN( |
| 158 | + buf_len, cdr_ctx->encoded_metadata.length - offset); |
| 159 | + memcpy(bufp, &cdr_ctx->encoded_metadata.data[offset], metadata_bytes_to_copy); |
| 160 | + buf_len -= metadata_bytes_to_copy; |
| 161 | + |
| 162 | + if (buf_len == 0) { |
| 163 | + return true; |
| 164 | + } |
| 165 | + |
| 166 | + offset = 0; |
| 167 | + bufp += metadata_bytes_to_copy; |
| 168 | + } else { |
| 169 | + offset -= cdr_ctx->encoded_metadata.length; |
| 170 | + } |
| 171 | + |
| 172 | + if (!cdr_ctx->active_source->read_data_cb(offset, bufp, buf_len)) { |
| 173 | + return false; |
| 174 | + } |
| 175 | + |
| 176 | + return true; |
| 177 | +} |
| 178 | + |
| 179 | +static void prv_cdr_mark_sent(void) { |
| 180 | + sMfltCdrSourceCtx *cdr_ctx = &s_memfault_cdr_source_ctx; |
| 181 | + if (cdr_ctx->active_source == NULL) { |
| 182 | + return; |
| 183 | + } |
| 184 | + |
| 185 | + cdr_ctx->active_source->mark_cdr_read_cb(); |
| 186 | + |
| 187 | + *cdr_ctx = (sMfltCdrSourceCtx) { 0 }; |
| 188 | +} |
| 189 | + |
| 190 | +bool memfault_cdr_register_source(const sMemfaultCdrSourceImpl *impl) { |
| 191 | + // it is a configuration error if all the required dependencies are not implemented! |
| 192 | + MEMFAULT_SDK_ASSERT( |
| 193 | + (impl != NULL) && |
| 194 | + (impl->has_cdr_cb != NULL) && |
| 195 | + (impl->read_data_cb != NULL) && |
| 196 | + (impl->mark_cdr_read_cb != NULL)); |
| 197 | + |
| 198 | + for (size_t i = 0; i < MEMFAULT_ARRAY_SIZE(s_cdr_sources); i++) { |
| 199 | + if (s_cdr_sources[i] == NULL) { |
| 200 | + s_cdr_sources[i] = impl; |
| 201 | + return true; |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + MEMFAULT_LOG_ERROR("Memfault Cdr Register is full, %d entries", (int)MEMFAULT_ARRAY_SIZE(s_cdr_sources)); |
| 206 | + return false; |
| 207 | +} |
| 208 | + |
| 209 | +void memfault_cdr_source_reset(void) { |
| 210 | + memset(s_cdr_sources, 0x0, sizeof(s_cdr_sources)); |
| 211 | + s_memfault_cdr_source_ctx = (sMfltCdrSourceCtx) { 0x0 }; |
| 212 | +} |
| 213 | + |
| 214 | +//! Expose a data source for use by the Memfault Packetizer |
| 215 | +const sMemfaultDataSourceImpl g_memfault_cdr_source = { |
| 216 | + .has_more_msgs_cb = prv_has_cdr, |
| 217 | + .read_msg_cb = prv_cdr_read, |
| 218 | + .mark_msg_read_cb = prv_cdr_mark_sent, |
| 219 | +}; |
| 220 | + |
| 221 | +#endif /* MEMFAULT_CDR_ENABLE */ |
0 commit comments