/*
 * Read in cbor key/values from os_mbuf pointed by m, and fill them
 * into attrs.
 *
 * @param m		Pointer to os_mbuf containing cbor encoded data
 * @param off		Offset into mbuf where cbor data begins
 * @param len		Number of bytes to decode
 * @param attrs		Array of cbor objects to look for.
 *
 * @return		0 on success; non-zero on failure.
 */
int
cbor_read_mbuf_attrs(struct os_mbuf *m, uint16_t off, uint16_t len,
                     const struct cbor_attr_t *attrs)
{
    struct cbor_mbuf_reader cmr;
    struct CborParser parser;
    struct CborValue value;
    CborError err;

    cbor_mbuf_reader_init(&cmr, m, off);
    err = cbor_parser_init(&cmr.r, 0, &parser, &value);
    if (err != CborNoError) {
        return -1;
    }
    return cbor_read_object(&value, attrs);
}
/*
 * Read in cbor key/values from flat buffer pointed by data, and fill them
 * into attrs.
 *
 * @param data		Pointer to beginning of cbor encoded data
 * @param len		Number of bytes in the buffer
 * @param attrs		Array of cbor objects to look for.
 *
 * @return		0 on success; non-zero on failure.
 */
int
cbor_read_flat_attrs(const uint8_t *data, int len,
                     const struct cbor_attr_t *attrs)
{
    struct cbor_buf_reader reader;
    struct CborParser parser;
    struct CborValue value;
    CborError err;

    cbor_buf_reader_init(&reader, data, len);
    err = cbor_parser_init(&reader.r, 0, &parser, &value);
    if (err != CborNoError) {
        return -1;
    }
    return cbor_read_object(&value, attrs);
}
            .addr.uinteger = &off
        },
        [1] = { 0 },
    };
    int rc;
    int sz;
    const struct flash_area *fa;
    uint8_t data[IMGMGR_NMGR_MAX_MSG];
    struct coredump_header *hdr;
    CborError g_err = CborNoError;
    CborEncoder *penc = &cb->encoder;
    CborEncoder rsp;

    hdr = (struct coredump_header *)data;

    rc = cbor_read_object(&cb->it, dload_attr);
    if (rc || off == UINT_MAX) {
        rc = MGMT_ERR_EINVAL;
        goto err;
    }

    rc = flash_area_open(MYNEWT_VAL(COREDUMP_FLASH_AREA), &fa);
    if (rc) {
        rc = MGMT_ERR_EINVAL;
        goto err;
    }

    rc = flash_area_read(fa, 0, hdr, sizeof(*hdr));
    if (rc) {
        rc = MGMT_ERR_EINVAL;
        goto err_close;
{
    char tmp_str[64];
    const struct cbor_attr_t attr[2] = {
        [0] = {
            .attribute = "u",
            .type = CborAttrTextStringType,
            .addr.string = tmp_str,
            .len = sizeof(tmp_str)
        },
        [1] = {
            .attribute = NULL
        }
    };
    int rc;

    rc = cbor_read_object(&cb->it, attr);
    if (rc) {
        rc = MGMT_ERR_EINVAL;
    } else {
        /* XXX ugh - drop out of a loop allowing a test to run in an app */
        runtest_start = 0;
        /*
         * we should be doing something more like this where we call
         * a named test suite directly
         */
#ifdef NOTYET
        rc = start_test(tmp_str);
#endif
        if (rc) {
            rc = MGMT_ERR_EINVAL;
        }
            .attribute = "hash",
            .type = CborAttrByteStringType,
            .addr.bytestring.data = hash,
            .addr.bytestring.len = &hash_len,
            .len = sizeof(hash),
        },
        [1] = {
            .attribute = "confirm",
            .type = CborAttrBooleanType,
            .addr.boolean = &confirm,
            .dflt.boolean = false,
        },
        [2] = { 0 },
    };

    rc = cbor_read_object(&cb->it, write_attr);
    if (rc != 0) {
        rc = MGMT_ERR_EINVAL;
        goto err;
    }

    /* Validate arguments. */
    if ((hash_len == 0) && !confirm) {
        rc = MGMT_ERR_EINVAL;
        goto err;
    }
    if ((hash_len != 0) && confirm) {
        rc = MGMT_ERR_EINVAL;
        goto err;
    }
            .addr.uinteger = &off,
            .nodefault = true
        },
        [3] = { 0 },
    };
    struct image_version ver;
    struct image_header *hdr;
    int area_id;
    int best;
    int rc;
    int i;
    CborEncoder *penc = &cb->encoder;
    CborEncoder rsp;
    CborError g_err = CborNoError;

    rc = cbor_read_object(&cb->it, off_attr);
    if (rc || off == UINT_MAX) {
        rc = MGMT_ERR_EINVAL;
        goto err;
    }

    if (off == 0) {
        if (data_len < sizeof(struct image_header)) {
            /*
             * Image header is the first thing in the image.
             */
            rc = MGMT_ERR_EINVAL;
            goto err;
        }
        hdr = (struct image_header *)img_data;
        if (hdr->ih_magic != IMAGE_MAGIC) {