Пример #1
0
void readVec3(CborValue *it0, Vec3 *vec3)
{
    CborValue it1; CborValue* it = &it1; cbor_value_enter_container(it0, it);
    cbor_value_get_float(it, &(vec3->x)); cbor_value_advance_fixed(it);
    cbor_value_get_float(it, &(vec3->y)); cbor_value_advance_fixed(it);
    cbor_value_get_float(it, &(vec3->z)); cbor_value_advance_fixed(it);
    cbor_value_leave_container(it0, it);
}
Пример #2
0
CborError
sol_oic_decode_cbor_repr_map(CborValue *map, struct sol_vector *reprs)
{
    struct sol_oic_repr_field *repr;
    CborValue value;
    CborError err;
    size_t len;

    if (!cbor_value_is_map(map))
        return CborInvalidType;

    err = cbor_value_enter_container(map, &value);
    for (; cbor_value_is_valid(&value) && err == CborNoError;
        err |= cbor_value_advance(&value)) {
        repr = sol_vector_append(reprs);
        if (!repr)
            return CborErrorOutOfMemory;

        err |= cbor_value_dup_text_string(&value, (char **)&repr->key, &len, NULL);
        err |= cbor_value_advance(&value);

        switch (cbor_value_get_type(&value)) {
        case CborIntegerType:
            err |= cbor_value_get_int64(&value, &repr->v_int);
            repr->type = SOL_OIC_REPR_TYPE_INT;
            break;
        case CborTextStringType:
            err |= cbor_value_dup_text_string(&value, (char **)&repr->v_slice.data, &repr->v_slice.len, NULL);
            if (err != CborNoError)
                goto harmless;
            repr->type = SOL_OIC_REPR_TYPE_TEXT_STRING;
            break;
        case CborByteStringType:
            err |= cbor_value_dup_byte_string(&value, (uint8_t **)&repr->v_slice.data, &repr->v_slice.len, NULL);
            if (err != CborNoError)
                goto harmless;
            repr->type = SOL_OIC_REPR_TYPE_BYTE_STRING;
            break;
        case CborDoubleType:
            err |= cbor_value_get_double(&value, &repr->v_double);
            repr->type = SOL_OIC_REPR_TYPE_DOUBLE;
            break;
        case CborFloatType:
            err |= cbor_value_get_float(&value, &repr->v_float);
            repr->type = SOL_OIC_REPR_TYPE_FLOAT;
            break;
        case CborHalfFloatType:
            err |= cbor_value_get_half_float(&value, &repr->v_voidptr);
            repr->type = SOL_OIC_REPR_TYPE_HALF_FLOAT;
            break;
        case CborBooleanType:
            err |= cbor_value_get_boolean(&value, &repr->v_boolean);
            repr->type = SOL_OIC_REPR_TYPE_BOOLEAN;
            break;
        default:
            SOL_ERR("While parsing representation map, got unexpected type %d",
                cbor_value_get_type(&value));
            if (err == CborNoError)
                err = CborErrorUnknownType;

harmless:
            /* Initialize repr with harmless data so cleanup works. */
            repr->v_boolean = false;
            repr->type = SOL_OIC_REPR_TYPE_BOOLEAN;
        }
    }

    return err | cbor_value_leave_container(map, &value);
}
Пример #3
0
static int
cbor_internal_read_object(CborValue *root_value,
                          const struct cbor_attr_t *attrs,
                          const struct cbor_array_t *parent,
                          int offset)
{
    const struct cbor_attr_t *cursor, *best_match;
    char attrbuf[MYNEWT_VAL(CBORATTR_MAX_SIZE) + 1];
    void *lptr;
    CborValue cur_value;
    CborError err = 0;
    size_t len;
    CborType type = CborInvalidType;

    /* stuff fields with defaults in case they're omitted in the JSON input */
    for (cursor = attrs; cursor->attribute != NULL; cursor++) {
        if (!cursor->nodefault) {
            lptr = cbor_target_address(cursor, parent, offset);
            if (lptr != NULL) {
                switch (cursor->type) {
                case CborAttrIntegerType:
                    memcpy(lptr, &cursor->dflt.integer, sizeof(long long int));
                    break;
                case CborAttrUnsignedIntegerType:
                    memcpy(lptr, &cursor->dflt.integer,
                           sizeof(long long unsigned int));
                    break;
                case CborAttrBooleanType:
                    memcpy(lptr, &cursor->dflt.boolean, sizeof(bool));
                    break;
#if FLOAT_SUPPORT
                case CborAttrFloatType:
                    memcpy(lptr, &cursor->dflt.fval, sizeof(float));
                    break;
                case CborAttrDoubleType:
                    memcpy(lptr, &cursor->dflt.real, sizeof(double));
                    break;
#endif
                default:
                    break;
                }
            }
        }
    }

    if (cbor_value_is_map(root_value)) {
        err |= cbor_value_enter_container(root_value, &cur_value);
    } else {
        err |= CborErrorIllegalType;
        return err;
    }

    /* contains key value pairs */
    while (cbor_value_is_valid(&cur_value) && !err) {
        /* get the attribute */
        if (cbor_value_is_text_string(&cur_value)) {
            if (cbor_value_calculate_string_length(&cur_value, &len) == 0) {
                if (len > MYNEWT_VAL(CBORATTR_MAX_SIZE)) {
                    err |= CborErrorDataTooLarge;
                    break;
                }
                err |= cbor_value_copy_text_string(&cur_value, attrbuf, &len,
                                                     NULL);
            }

            /* at least get the type of the next value so we can match the
             * attribute name and type for a perfect match */
            err |= cbor_value_advance(&cur_value);
            if (cbor_value_is_valid(&cur_value)) {
                type = cbor_value_get_type(&cur_value);
            } else {
                err |= CborErrorIllegalType;
                break;
            }
        } else {
            attrbuf[0] = '\0';
            type = cbor_value_get_type(&cur_value);
        }

        /* find this attribute in our list */
        best_match = NULL;
        for (cursor = attrs; cursor->attribute != NULL; cursor++) {
            if (valid_attr_type(type, cursor->type)) {
                if (cursor->attribute == CBORATTR_ATTR_UNNAMED &&
                    attrbuf[0] == '\0') {
                    best_match = cursor;
                } else if (strlen(cursor->attribute) == len &&
                    !memcmp(cursor->attribute, attrbuf, len)) {
                    break;
                }
            }
        }
        if (!cursor->attribute && best_match) {
            cursor = best_match;
        }
        /* we found a match */
        if (cursor->attribute != NULL) {
            lptr = cbor_target_address(cursor, parent, offset);
            switch (cursor->type) {
            case CborAttrNullType:
                /* nothing to do */
                break;
            case CborAttrBooleanType:
                err |= cbor_value_get_boolean(&cur_value, lptr);
                break;
            case CborAttrIntegerType:
                err |= cbor_value_get_int64(&cur_value, lptr);
                break;
            case CborAttrUnsignedIntegerType:
                err |= cbor_value_get_uint64(&cur_value, lptr);
                break;
#if FLOAT_SUPPORT
            case CborAttrFloatType:
                err |= cbor_value_get_float(&cur_value, lptr);
                break;
            case CborAttrDoubleType:
                err |= cbor_value_get_double(&cur_value, lptr);
                break;
#endif
            case CborAttrByteStringType: {
                size_t len = cursor->len;
                err |= cbor_value_copy_byte_string(&cur_value, lptr,
                                                   &len, NULL);
                *cursor->addr.bytestring.len = len;
                break;
            }
            case CborAttrTextStringType: {
                size_t len = cursor->len;
                err |= cbor_value_copy_text_string(&cur_value, lptr,
                                                   &len, NULL);
                break;
            }
            case CborAttrArrayType:
                err |= cbor_read_array(&cur_value, &cursor->addr.array);
                continue;
            case CborAttrObjectType:
                err |= cbor_internal_read_object(&cur_value, cursor->addr.obj,
                                                 NULL, 0);
                continue;
            default:
                err |= CborErrorIllegalType;
            }
        }
        cbor_value_advance(&cur_value);
    }
    if (!err) {
        /* that should be it for this container */
        err |= cbor_value_leave_container(root_value, &cur_value);
    }
    return err;
}