|
11 | 11 | #include <esp_rmaker_utils.h>
|
12 | 12 |
|
13 | 13 | #include <cbor.h>
|
| 14 | +#include "esp_insights_cbor_decoder.h" |
14 | 15 |
|
15 | 16 | static const char *TAG = "insight_cbor_dec";
|
16 | 17 |
|
@@ -281,3 +282,101 @@ esp_err_t esp_insights_cbor_decode_dump(const uint8_t *buffer, int len)
|
281 | 282 |
|
282 | 283 | return ESP_OK;
|
283 | 284 | }
|
| 285 | + |
| 286 | +bool esp_insights_cbor_decoder_at_end(cbor_parse_ctx_t *ctx) |
| 287 | +{ |
| 288 | + return cbor_value_at_end(&ctx->it[ctx->curr_itr]); |
| 289 | +} |
| 290 | + |
| 291 | +esp_err_t esp_insights_cbor_decoder_advance(cbor_parse_ctx_t *ctx) |
| 292 | +{ |
| 293 | + if (CborNoError == cbor_value_advance(&ctx->it[ctx->curr_itr])) { |
| 294 | + return ESP_OK; |
| 295 | + } |
| 296 | + return ESP_FAIL; |
| 297 | +} |
| 298 | + |
| 299 | +CborType esp_insights_cbor_decode_get_value_type(cbor_parse_ctx_t *ctx) |
| 300 | +{ |
| 301 | + return cbor_value_get_type(&ctx->it[ctx->curr_itr]); |
| 302 | +} |
| 303 | + |
| 304 | +char *esp_insights_cbor_decoder_get_string(CborValue *val) |
| 305 | +{ |
| 306 | + CborError ret = CborNoError; |
| 307 | + char *buf = NULL; |
| 308 | + size_t n; |
| 309 | + if (cbor_value_get_type(val) != CborTextStringType) { |
| 310 | + return NULL; |
| 311 | + } |
| 312 | + ret = cbor_value_dup_text_string(val, &buf, &n, val); |
| 313 | + if (ret == CborNoError) { |
| 314 | + return (char *) buf; |
| 315 | + } |
| 316 | + return NULL; |
| 317 | +} |
| 318 | + |
| 319 | +esp_err_t esp_insights_cbor_decoder_enter_container(cbor_parse_ctx_t *ctx) |
| 320 | +{ |
| 321 | + CborError ret = CborNoError; |
| 322 | + int curr_itr = ctx->curr_itr; |
| 323 | + if (curr_itr >= INS_CBOR_MAX_DEPTH) { |
| 324 | + ESP_LOGE(TAG, "Cannot parse depth more than %d", INS_CBOR_MAX_DEPTH); |
| 325 | + return ESP_FAIL; |
| 326 | + } |
| 327 | + ret = cbor_value_enter_container(&ctx->it[curr_itr], &ctx->it[curr_itr + 1]); |
| 328 | + if (ret != CborNoError) { |
| 329 | + ESP_LOGE(TAG, "error entering container"); |
| 330 | + return ESP_FAIL; |
| 331 | + } |
| 332 | + if (esp_insights_cbor_decoder_at_end(ctx)) { |
| 333 | + return ESP_FAIL; |
| 334 | + } |
| 335 | + ctx->curr_itr++; |
| 336 | + |
| 337 | + return ESP_OK; |
| 338 | +} |
| 339 | + |
| 340 | +esp_err_t esp_insights_cbor_decoder_exit_container(cbor_parse_ctx_t *ctx) |
| 341 | +{ |
| 342 | + CborError ret = CborNoError; |
| 343 | + if (ctx->curr_itr <= 0) { |
| 344 | + ESP_LOGE(TAG, "cannot exit, already at top"); |
| 345 | + return ESP_FAIL; |
| 346 | + } |
| 347 | + ctx->curr_itr--; |
| 348 | + int curr_itr = ctx->curr_itr; |
| 349 | + ret = cbor_value_leave_container(&ctx->it[curr_itr], &ctx->it[curr_itr + 1]); |
| 350 | + if (ret != CborNoError) { |
| 351 | + ESP_LOGE(TAG, "error leaving container"); |
| 352 | + return ESP_FAIL; |
| 353 | + } |
| 354 | + return ESP_OK; |
| 355 | +} |
| 356 | + |
| 357 | +esp_err_t esp_insights_cbor_decoder_done(cbor_parse_ctx_t *ctx) |
| 358 | +{ |
| 359 | + if (ctx) { |
| 360 | + free(ctx); |
| 361 | + } |
| 362 | + return ESP_OK; |
| 363 | +} |
| 364 | + |
| 365 | +cbor_parse_ctx_t *esp_insights_cbor_decoder_start(const uint8_t *buffer, int len) |
| 366 | +{ |
| 367 | + if (!buffer || len == 0) { |
| 368 | + return NULL; |
| 369 | + } |
| 370 | + cbor_parse_ctx_t *ctx = calloc(1, sizeof(cbor_parse_ctx_t)); |
| 371 | + if (!ctx) { |
| 372 | + ESP_LOGE(TAG, "failed to allocate cbor ctx"); |
| 373 | + return NULL; |
| 374 | + } |
| 375 | + CborParser root_parser = ctx->root_parser; |
| 376 | + CborValue *it = &ctx->it[0]; |
| 377 | + if (cbor_parser_init(buffer, len, 0, &root_parser, it) != CborNoError) { |
| 378 | + ESP_LOGE(TAG, "Error initializing cbor parser"); |
| 379 | + return NULL; |
| 380 | + } |
| 381 | + return ctx; |
| 382 | +} |
0 commit comments