Ejemplo n.º 1
0
static int64_t OCConvertArray(CborEncoder *parent, const OCRepPayloadValueArray *valArray)
{
    int64_t err = CborNoError;
    CborEncoder array;
    err |= cbor_encoder_create_array(parent, &array, valArray->dimensions[0]);
    VERIFY_CBOR_SUCCESS(TAG, err, "Failed creating rep array");

    for (size_t i = 0; i < valArray->dimensions[0]; ++i)
    {
        if (0 != valArray->dimensions[1])
        {
            CborEncoder array2;
            err |= cbor_encoder_create_array(&array, &array2, valArray->dimensions[1]);
            VERIFY_CBOR_SUCCESS(TAG, err, "Failed creating rep array2");

            for (size_t j = 0; j < valArray->dimensions[1]; ++j)
            {
                if (0 != valArray->dimensions[2])
                {
                    CborEncoder array3;
                    err |= cbor_encoder_create_array(&array2, &array3, valArray->dimensions[2]);
                    VERIFY_CBOR_SUCCESS(TAG, err, "Failed creating rep array3");

                    for(size_t k = 0; k < valArray->dimensions[2]; ++k)
                    {
                        err |= OCConvertArrayItem(&array3, valArray,
                            j * valArray->dimensions[2] +
                            i * valArray->dimensions[2] * valArray->dimensions[1] +
                            k);
                        VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding rep array3 value");
                    }
                    err |= cbor_encoder_close_container(&array2, &array3);
                    VERIFY_CBOR_SUCCESS(TAG, err, "Failed closing rep array3");
                }
                else
                {
                    err |= OCConvertArrayItem(&array2, valArray, i * valArray->dimensions[1] + j);
                    VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding rep array2 value");
                }
            }
            err |= cbor_encoder_close_container(&array, &array2);
            VERIFY_CBOR_SUCCESS(TAG, err, "Failed closing rep array2");
        }
        else
        {
            err |= OCConvertArrayItem(&array, valArray, i);
            VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding rep array value");
        }
    }
    err |= cbor_encoder_close_container(parent, &array);
    VERIFY_CBOR_SUCCESS(TAG, err, "Failed closing rep array");

exit:
    return err;
}
void writeInputEventHandler(CborEncoder *enc0, InputEventHandler inputEventHandler)
{
    if (inputEventHandler.selector == 0) {
        CborEncoder enc1; CborEncoder* enc = &enc1; cbor_encoder_create_array(enc0, enc, 1);
        cbor_encode_uint(enc, (uint64_t)inputEventHandler.selector);
        cbor_encoder_close_container_checked(enc0, enc);
    };
    if (inputEventHandler.selector == 1) {
        CborEncoder enc1; CborEncoder* enc = &enc1; cbor_encoder_create_array(enc0, enc, 2);
        cbor_encode_uint(enc, (uint64_t)inputEventHandler.selector);
        /* TBD */        cbor_encoder_close_container_checked(enc0, enc);
    };
}
Ejemplo n.º 3
0
void writeGeometry(CborEncoder *enc0, Geometry geometry)
{
    if (geometry.selector == 0) {
        CborEncoder enc1; CborEncoder* enc = &enc1; cbor_encoder_create_array(enc0, enc, 2);
        cbor_encode_uint(enc, (uint64_t)geometry.selector);
        writeShape(enc, geometry.data.ShapeGeometry.value0);
        cbor_encoder_close_container_checked(enc0, enc);
    };
    if (geometry.selector == 1) {
        CborEncoder enc1; CborEncoder* enc = &enc1; cbor_encoder_create_array(enc0, enc, 2);
        cbor_encode_uint(enc, (uint64_t)geometry.selector);
        cbor_encode_text_stringz(enc, geometry.data.ResourceGeometry.value0.c_str());
        cbor_encoder_close_container_checked(enc0, enc);
    };
}
Ejemplo n.º 4
0
static int64_t AddStringLLToMap(CborEncoder *map, char *tag, const size_t size, OCStringLL *value)
{
    CborEncoder array;
    CborError cborEncoderResult;
    cborEncoderResult = cbor_encode_text_string(map, tag, size);
    if (CborNoError != cborEncoderResult)
    {
        return cborEncoderResult;
    }
    cborEncoderResult = cbor_encoder_create_array(map, &array, CborIndefiniteLength);
    if (CborNoError != cborEncoderResult)
    {
        return cborEncoderResult;
    }
    OCStringLL *strType = value;
    while (strType)
    {
        cborEncoderResult = cbor_encode_text_string(&array, strType->value, strlen(strType->value));
        if (CborNoError != cborEncoderResult)
        {
            return cborEncoderResult;
        }
        strType = strType->next;
    }
    cborEncoderResult = cbor_encoder_close_container(map, &array);
    if (CborNoError != cborEncoderResult)
    {
        return cborEncoderResult;
    }
    return cborEncoderResult;
}
Ejemplo n.º 5
0
static int64_t OCConvertSecurityPayload(OCSecurityPayload* payload, uint8_t* outPayload,
        size_t* size)
{
    CborEncoder encoder;
    int64_t err = 0;

    cbor_encoder_init(&encoder, outPayload, *size, 0);

    CborEncoder rootArray;
    err = err | cbor_encoder_create_array(&encoder, &rootArray, 1);
    CborEncoder map;

    err = err | cbor_encoder_create_map(&rootArray, &map, CborIndefiniteLength);

    if(payload->securityData)
    {
        err = err | AddTextStringToMap(&map, OC_RSRVD_REPRESENTATION,
                sizeof(OC_RSRVD_REPRESENTATION) - 1,
                payload->securityData);
    }

    err = err | cbor_encoder_close_container(&rootArray, &map);

    err = err | cbor_encoder_close_container(&encoder, &rootArray);
    return checkError(err, &encoder, outPayload, size);
}
Ejemplo n.º 6
0
static int64_t OCConvertArray(CborEncoder* parent, const OCRepPayloadValueArray* valArray)
{
    CborEncoder array;
    int64_t err = 0;

    err = err | cbor_encoder_create_array(parent, &array, valArray->dimensions[0]);

    for (size_t i = 0; i < valArray->dimensions[0];++i)
    {
        if (valArray->dimensions[1] != 0)
        {
            CborEncoder array2;
            err = err | cbor_encoder_create_array(&array, &array2, valArray->dimensions[1]);

            for (size_t j = 0; j < valArray->dimensions[1]; ++j)
            {
                if (valArray->dimensions[2] != 0)
                {
                    CborEncoder array3;
                    err = err | cbor_encoder_create_array(&array2, &array3,
                            valArray->dimensions[2]);

                    for(size_t k = 0; k < valArray->dimensions[2]; ++k)
                    {
                        OCConvertArrayItem(&array3, valArray,
                            j * valArray->dimensions[2] +
                            i * valArray->dimensions[2] * valArray->dimensions[1] +
                            k);
                    }
                    err = err | cbor_encoder_close_container(&array2, &array3);
                }
                else
                {
                    OCConvertArrayItem(&array2, valArray,
                            i * valArray->dimensions[1] + j);
                }
            }
            err = err | cbor_encoder_close_container(&array, &array2);
        }
        else
        {
            OCConvertArrayItem(&array, valArray, i);
        }
    }
    err = err | cbor_encoder_close_container(parent, &array);
    return err;
}
Ejemplo n.º 7
0
void writeVec3(CborEncoder *enc0, Vec3 vec3)
{
    CborEncoder enc1; CborEncoder* enc = &enc1; cbor_encoder_create_array(enc0, enc, 3);
    cbor_encode_float(enc, vec3.x);
    cbor_encode_float(enc, vec3.y);
    cbor_encode_float(enc, vec3.z);
    cbor_encoder_close_container_checked(enc0, enc);
}
Ejemplo n.º 8
0
static int64_t OCConvertDevicePayload(OCDevicePayload* payload, uint8_t* outPayload,
        size_t* size)
{
    CborEncoder encoder = {0};
    int64_t err = 0;

    cbor_encoder_init(&encoder, outPayload, *size, 0);
    CborEncoder rootArray;
    err = err | cbor_encoder_create_array(&encoder, &rootArray, 1);

    {
        CborEncoder map;
        err = err | cbor_encoder_create_map(&rootArray, &map, CborIndefiniteLength);

        // uri
        err = err | ConditionalAddTextStringToMap(&map, OC_RSRVD_HREF, sizeof(OC_RSRVD_HREF) - 1,
                payload->uri);

        // Rep Map
        {
            CborEncoder repMap;
            err = err | cbor_encode_text_string(&map, OC_RSRVD_REPRESENTATION,
                    sizeof(OC_RSRVD_REPRESENTATION) - 1);
            err = err | cbor_encoder_create_map(&map, &repMap, CborIndefiniteLength);

            // Device ID
            err = err | cbor_encode_text_string(&repMap, OC_RSRVD_DEVICE_ID,
                    sizeof(OC_RSRVD_DEVICE_ID) - 1);
            err = err | cbor_encode_byte_string(&repMap, payload->sid, UUID_SIZE);

            // Device Name
            err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_DEVICE_NAME,
                    sizeof(OC_RSRVD_DEVICE_NAME) - 1,
                    payload->deviceName);

            // Device Spec Version
            err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_SPEC_VERSION,
                    sizeof(OC_RSRVD_SPEC_VERSION) - 1,
                    payload->specVersion);

            // Device data Model Version
            err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_DATA_MODEL_VERSION,
                    sizeof(OC_RSRVD_DATA_MODEL_VERSION) - 1,
                    payload->dataModelVersion);

            err = err | cbor_encoder_close_container(&map, &repMap);
        }

        // Close Map
        err = err | cbor_encoder_close_container(&rootArray, &map);
    }

    // Close main array
    err = err | cbor_encoder_close_container(&encoder, &rootArray);

    return checkError(err, &encoder, outPayload, size);
}
Ejemplo n.º 9
0
/**
 * Log encode entries
 * @param log structure, the encoder, timestamp, index
 * @return 0 on success; non-zero on failure
 */
static int
log_encode_entries(struct log *log, CborEncoder *cb,
                   int64_t ts, uint32_t index)
{
    int rc;
    struct encode_off encode_off;
    int rsp_len = 0;
    CborEncoder entries;
    CborError g_err = CborNoError;
    struct CborCntWriter cnt_writer;
    CborEncoder cnt_encoder;

    memset(&encode_off, 0, sizeof(encode_off));

    /* this code counts how long the message would be if we encoded
     * this outer structure using cbor. */
    cbor_cnt_writer_init(&cnt_writer);
    cbor_encoder_init(&cnt_encoder, &cnt_writer.enc, 0);
    g_err |= cbor_encode_text_stringz(&cnt_encoder, "entries");
    g_err |= cbor_encoder_create_array(&cnt_encoder, &entries,
                                       CborIndefiniteLength);
    g_err |= cbor_encoder_close_container(&cnt_encoder, &entries);
    rsp_len = cbor_encode_bytes_written(cb) +
              cbor_encode_bytes_written(&cnt_encoder);
    if (rsp_len > 400) {
        rc = OS_ENOMEM;
        goto err;
    }

    g_err |= cbor_encode_text_stringz(cb, "entries");
    g_err |= cbor_encoder_create_array(cb, &entries, CborIndefiniteLength);

    encode_off.eo_encoder  = &entries;
    encode_off.eo_index    = index;
    encode_off.eo_ts       = ts;
    encode_off.rsp_len = rsp_len;

    rc = log_walk(log, log_nmgr_encode_entry, &encode_off);

    g_err |= cbor_encoder_close_container(cb, &entries);

err:
    return rc;
}
Ejemplo n.º 10
0
static OCStackResult OCConvertSecurityPayload(OCSecurityPayload* payload, uint8_t** outPayload,
        size_t* size)
{
    *outPayload = (uint8_t*)OICCalloc(1, MAX_REQUEST_LENGTH);
    *size = MAX_REQUEST_LENGTH;

    if(!*outPayload)
    {
        return OC_STACK_NO_MEMORY;
    }

    CborEncoder encoder;
    bool err = false;

    cbor_encoder_init(&encoder, *outPayload, *size, 0);

    CborEncoder rootArray;
    err = err || cbor_encoder_create_array(&encoder, &rootArray, 2);
    err = err || cbor_encode_uint(&rootArray, PAYLOAD_TYPE_SECURITY);

    CborEncoder map;

    err = err || cbor_encoder_create_map(&rootArray, &map, CborIndefiniteLength);

    if(payload->securityData)
    {
        err = err || AddTextStringToMap(&map, OC_RSRVD_REPRESENTATION,
                sizeof(OC_RSRVD_REPRESENTATION) - 1,
                payload->securityData);
    }

    err = err || cbor_encoder_close_container(&rootArray, &map);

    err = err || cbor_encoder_close_container(&encoder, &rootArray);

    if(err)
    {
        OC_LOG_V(ERROR, TAG, "Convert Security Payload failed", err);
        OICFree(*outPayload);
        return OC_STACK_ERROR;
    }

    *size = encoder.ptr - *outPayload;
    uint8_t* tempPayload = (uint8_t*)OICRealloc(*outPayload, *size);

    if(!tempPayload)
    {
        OC_LOG_V(ERROR, TAG, PCF("Payload realloc failed!"));
        OICFree(*outPayload);
        return OC_STACK_ERROR;
    }

    *outPayload = tempPayload;
    return OC_STACK_OK;
}
Ejemplo n.º 11
0
void writeShape(CborEncoder *enc0, Shape shape)
{
    if (shape.selector == 0) {
        CborEncoder enc1; CborEncoder* enc = &enc1; cbor_encoder_create_array(enc0, enc, 1);
        cbor_encode_uint(enc, (uint64_t)shape.selector);
        cbor_encoder_close_container_checked(enc0, enc);
    };
    if (shape.selector == 1) {
        CborEncoder enc1; CborEncoder* enc = &enc1; cbor_encoder_create_array(enc0, enc, 1);
        cbor_encode_uint(enc, (uint64_t)shape.selector);
        cbor_encoder_close_container_checked(enc0, enc);
    };
    if (shape.selector == 2) {
        CborEncoder enc1; CborEncoder* enc = &enc1; cbor_encoder_create_array(enc0, enc, 1);
        cbor_encode_uint(enc, (uint64_t)shape.selector);
        cbor_encoder_close_container_checked(enc0, enc);
    };
    if (shape.selector == 3) {
        CborEncoder enc1; CborEncoder* enc = &enc1; cbor_encoder_create_array(enc0, enc, 1);
        cbor_encode_uint(enc, (uint64_t)shape.selector);
        cbor_encoder_close_container_checked(enc0, enc);
    };
    if (shape.selector == 4) {
        CborEncoder enc1; CborEncoder* enc = &enc1; cbor_encoder_create_array(enc0, enc, 1);
        cbor_encode_uint(enc, (uint64_t)shape.selector);
        cbor_encoder_close_container_checked(enc0, enc);
    };
    if (shape.selector == 5) {
        CborEncoder enc1; CborEncoder* enc = &enc1; cbor_encoder_create_array(enc0, enc, 1);
        cbor_encode_uint(enc, (uint64_t)shape.selector);
        cbor_encoder_close_container_checked(enc0, enc);
    };
}
static int64_t OCLinksPayloadToCbor(OCLinksPayload *rtPtr, CborEncoder *setMap)
{
    CborEncoder linksArray;
    int64_t cborEncoderResult = CborNoError;

    cborEncoderResult |= cbor_encoder_create_array(setMap, &linksArray, CborIndefiniteLength);
    VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to create Links array");

    while (rtPtr)
    {
        CborEncoder linksMap;
        cborEncoderResult |= cbor_encoder_create_map(&linksArray, &linksMap, CborIndefiniteLength);
        VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to create Links map");

        cborEncoderResult |= ConditionalAddTextStringToMap(&linksMap, OC_RSRVD_HREF, rtPtr->href);
        VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_HREF in Links map");

        cborEncoderResult|= ConditionalAddTextStringToMap(&linksMap, OC_RSRVD_REL, rtPtr->rel);
        VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_REL in Links map");

        cborEncoderResult |= ConditionalAddTextStringToMap(&linksMap, OC_RSRVD_TITLE, rtPtr->title);
        VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_TITLE in Links map");

        cborEncoderResult |= ConditionalAddTextStringToMap(&linksMap, OC_RSRVD_URI, rtPtr->uri);
        VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_URI in Links map");

        cborEncoderResult |= AddStringLLToMap(&linksMap, OC_RSRVD_RESOURCE_TYPE, rtPtr->rt);
        VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_RT in Links map");

        cborEncoderResult |= AddStringLLToMap(&linksMap, OC_RSRVD_INTERFACE, rtPtr->itf);
        VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_ITF in Links map");

        cborEncoderResult |= AddStringLLToMap(&linksMap, OC_RSRVD_MEDIA_TYPE, rtPtr->mt);
        VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_MT in Links map");

        {
            uint64_t value = rtPtr->ins;
            cborEncoderResult |= ConditionalAddIntToMap(&linksMap, OC_RSRVD_INS, &value);
            VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_INS in Links map");
        }

        cborEncoderResult |= cbor_encoder_close_container(&linksArray, &linksMap);
        VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed closing Links map");

        rtPtr = rtPtr->next;
    }
    cborEncoderResult |= cbor_encoder_close_container(setMap, &linksArray);
    VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed closing links array");

exit:
    return cborEncoderResult;
}
Ejemplo n.º 13
0
static bool OCConvertArray(CborEncoder* parent, const OCRepPayloadValueArray* valArray)
{
    CborEncoder array;
    bool err = false;

    err = err || cbor_encoder_create_array(parent, &array, CborIndefiniteLength);
    err = err || cbor_encode_uint(&array, valArray->type);
    for(int i = 0; i < MAX_REP_ARRAY_DEPTH; ++i)
    {
        err = err || cbor_encode_uint(&array, valArray->dimensions[i]);
    }

    size_t dimTotal = calcDimTotal(valArray->dimensions);

    for(size_t i = 0; i < dimTotal; ++i)
    {
        switch(valArray->type)
        {
            case OCREP_PROP_NULL:
                OC_LOG(ERROR, TAG, PCF("ConvertArray Invalid NULL"));
                err = CborUnknownError;
                break;
            case OCREP_PROP_INT:
                err = err || cbor_encode_int(&array, valArray->iArray[i]);
                break;
            case OCREP_PROP_DOUBLE:
                err = err || cbor_encode_double(&array, valArray->dArray[i]);
                break;
            case OCREP_PROP_BOOL:
                err = err || cbor_encode_boolean(&array, valArray->bArray[i]);
                break;
            case OCREP_PROP_STRING:
                err = err || cbor_encode_text_string(&array, valArray->strArray[i],
                        strlen(valArray->strArray[i]));
                break;
            case OCREP_PROP_OBJECT:
                err = OCConvertSingleRepPayload(&array, valArray->objArray[i]);
                break;
            case OCREP_PROP_ARRAY:
                OC_LOG(ERROR, TAG, PCF("ConvertArray Invalid child array"));
                err = CborUnknownError;
                break;
        }
    }

    err = err || cbor_encoder_close_container(parent, &array);
    return err;
}
Ejemplo n.º 14
0
static OCStackResult OCConvertRepPayload(OCRepPayload* payload, uint8_t** outPayload, size_t* size)
{
    *outPayload = (uint8_t*)OICCalloc(1, MAX_REQUEST_LENGTH);
    *size = MAX_REQUEST_LENGTH;

    if(!*outPayload)
    {
        return OC_STACK_NO_MEMORY;
    }

    CborEncoder encoder = {};
    bool err = false;

    cbor_encoder_init(&encoder, *outPayload, *size, 0);
    CborEncoder rootArray;
    err = err || cbor_encoder_create_array(&encoder, &rootArray, CborIndefiniteLength);
    err = err || cbor_encode_uint(&rootArray, PAYLOAD_TYPE_REPRESENTATION);

    while(payload != NULL && !err)
    {
        err = err || OCConvertSingleRepPayload(&rootArray, payload);
        payload = payload->next;
    }

    // Close main array
    err = err || cbor_encoder_close_container(&encoder, &rootArray);

    if(err)
    {
        OC_LOG_V(ERROR, TAG, "Convert Rep Payload failed with : %d", err);
        return OC_STACK_ERROR;
    }

    *size = encoder.ptr - *outPayload;
    uint8_t* tempPayload = (uint8_t*)OICRealloc(*outPayload, *size);

    if(!tempPayload)
    {
        OC_LOG_V(ERROR, TAG, PCF("Payload realloc failed!"));
        OICFree(*outPayload);
        return OC_STACK_ERROR;
    }

    *outPayload = tempPayload;

    return OC_STACK_OK;
}
void writeInputEventType(CborEncoder *enc0, InputEventType inputEventType)
{
    if (inputEventType.selector == 0) {
        CborEncoder enc1; CborEncoder* enc = &enc1; cbor_encoder_create_array(enc0, enc, 1);
        cbor_encode_uint(enc, (uint64_t)inputEventType.selector);
        cbor_encoder_close_container_checked(enc0, enc);
    };
    if (inputEventType.selector == 1) {
        CborEncoder enc1; CborEncoder* enc = &enc1; cbor_encoder_create_array(enc0, enc, 1);
        cbor_encode_uint(enc, (uint64_t)inputEventType.selector);
        cbor_encoder_close_container_checked(enc0, enc);
    };
    if (inputEventType.selector == 2) {
        CborEncoder enc1; CborEncoder* enc = &enc1; cbor_encoder_create_array(enc0, enc, 1);
        cbor_encode_uint(enc, (uint64_t)inputEventType.selector);
        cbor_encoder_close_container_checked(enc0, enc);
    };
    if (inputEventType.selector == 3) {
        CborEncoder enc1; CborEncoder* enc = &enc1; cbor_encoder_create_array(enc0, enc, 1);
        cbor_encode_uint(enc, (uint64_t)inputEventType.selector);
        cbor_encoder_close_container_checked(enc0, enc);
    };
    if (inputEventType.selector == 4) {
        CborEncoder enc1; CborEncoder* enc = &enc1; cbor_encoder_create_array(enc0, enc, 1);
        cbor_encode_uint(enc, (uint64_t)inputEventType.selector);
        cbor_encoder_close_container_checked(enc0, enc);
    };
    if (inputEventType.selector == 5) {
        CborEncoder enc1; CborEncoder* enc = &enc1; cbor_encoder_create_array(enc0, enc, 1);
        cbor_encode_uint(enc, (uint64_t)inputEventType.selector);
        cbor_encoder_close_container_checked(enc0, enc);
    };
    if (inputEventType.selector == 6) {
        CborEncoder enc1; CborEncoder* enc = &enc1; cbor_encoder_create_array(enc0, enc, 1);
        cbor_encode_uint(enc, (uint64_t)inputEventType.selector);
        cbor_encoder_close_container_checked(enc0, enc);
    };
    if (inputEventType.selector == 7) {
        CborEncoder enc1; CborEncoder* enc = &enc1; cbor_encoder_create_array(enc0, enc, 1);
        cbor_encode_uint(enc, (uint64_t)inputEventType.selector);
        cbor_encoder_close_container_checked(enc0, enc);
    };
}
Ejemplo n.º 16
0
static int64_t OCConvertPresencePayload(OCPresencePayload* payload,
        uint8_t* outPayload, size_t* size)
{
    CborEncoder encoder = {0};
    int64_t err = 0;

    cbor_encoder_init(&encoder, outPayload, *size, 0);
    CborEncoder rootArray;

    err = err | cbor_encoder_create_array(&encoder, &rootArray, 1);

    CborEncoder map;
    err = err | cbor_encoder_create_map(&rootArray, &map, CborIndefiniteLength);

    // Sequence Number
    err = err | cbor_encode_text_string(&map,
            OC_RSRVD_NONCE,
            sizeof(OC_RSRVD_NONCE) - 1);
    err = err | cbor_encode_uint(&map, payload->sequenceNumber);

    // Max Age
    err = err | cbor_encode_text_string(&map,
            OC_RSRVD_TTL,
            sizeof(OC_RSRVD_TTL) - 1);
    err = err | cbor_encode_uint(&map, payload->maxAge);

    // Trigger
    const char* triggerStr = convertTriggerEnumToString(payload->trigger);
    err = err | AddTextStringToMap(&map, OC_RSRVD_TRIGGER, sizeof(OC_RSRVD_TRIGGER) - 1,
            triggerStr);

    // Resource type name
    if(payload->trigger != OC_PRESENCE_TRIGGER_DELETE)
    {
        err = err | ConditionalAddTextStringToMap(&map, OC_RSRVD_RESOURCE_TYPE,
                sizeof(OC_RSRVD_RESOURCE_TYPE) - 1, payload->resourceType);
    }

    // Close Map
    err = err | cbor_encoder_close_container(&rootArray, &map);
    err = err | cbor_encoder_close_container(&encoder, &rootArray);

    return checkError(err, &encoder, outPayload, size);
}
static int64_t AddStringLLToMap(CborEncoder *map, const char *tag, const OCStringLL *strType)
{
    CborEncoder array;
    int64_t cborEncoderResult = CborNoError;
    cborEncoderResult |= cbor_encode_text_string(map, tag, strlen(tag));
    VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed encoding string tag name");
    cborEncoderResult |= cbor_encoder_create_array(map, &array, CborIndefiniteLength);
    VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed creating stringLL array");
    while (strType)
    {
        cborEncoderResult |= cbor_encode_text_string(&array, strType->value, strlen(strType->value));
        VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed encoding string value");
        strType = strType->next;
    }
    cborEncoderResult |= cbor_encoder_close_container(map, &array);
    VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed closing string array");
exit:
    return cborEncoderResult;
}
Ejemplo n.º 18
0
static int64_t OCConvertRepPayload(OCRepPayload* payload, uint8_t* outPayload, size_t* size)
{
    CborEncoder encoder = {0};
    int64_t err = 0;

    cbor_encoder_init(&encoder, outPayload, *size, 0);
    CborEncoder rootArray;
    err = err | cbor_encoder_create_array(&encoder, &rootArray, CborIndefiniteLength);

    while(payload != NULL && (err == 0 || err == CborErrorOutOfMemory))
    {
        err = err | OCConvertSingleRepPayload(&rootArray, payload);
        payload = payload->next;
    }

    // Close main array
    err = err | cbor_encoder_close_container(&encoder, &rootArray);

    return checkError(err, &encoder, outPayload, size);
}
Ejemplo n.º 19
0
static int64_t OCStringLLJoin(CborEncoder *map, char *type, OCStringLL *val)
{
    uint16_t count = 0;
    int64_t err = CborNoError;

    for (OCStringLL *temp = val; temp; temp = temp->next)
    {
        ++count;
    }
    if (count > 0)
    {
        CborEncoder array;
        err |= cbor_encode_text_string(map, type, strlen(type));
        err |= cbor_encoder_create_array(map, &array, count);
        while (val)
        {
            err |= cbor_encode_text_string(&array, val->value, strlen(val->value));
            val = val->next;
        }
        err |= cbor_encoder_close_container(map, &array);
    }

    return err;
}
Ejemplo n.º 20
0
static int64_t OCConvertRepPayload(OCRepPayload *payload, uint8_t *outPayload, size_t *size)
{
    CborEncoder encoder;
    int64_t err = CborNoError;

    cbor_encoder_init(&encoder, outPayload, *size, 0);

    size_t arrayCount = 0;
    for (OCRepPayload *temp = payload; temp; temp = temp->next)
    {
        arrayCount++;
    }
    CborEncoder rootArray;
    if (arrayCount > 1)
    {
        err |= cbor_encoder_create_array(&encoder, &rootArray, arrayCount);
        VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding rep root map");
    }

    while (payload != NULL && (err == CborNoError))
    {
        CborEncoder rootMap;
        err |= cbor_encoder_create_map(((arrayCount == 1)? &encoder: &rootArray),
                                            &rootMap, CborIndefiniteLength);
        VERIFY_CBOR_SUCCESS(TAG, err, "Failed creating root map");

        // Only in case of collection href is included.
        if (arrayCount > 1 && payload->uri && strlen(payload->uri) > 0)
        {
            OIC_LOG(INFO, TAG, "Payload has uri");
            err |= cbor_encode_text_string(&rootMap, OC_RSRVD_HREF, strlen(OC_RSRVD_HREF));
            VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding rep href tag");
            err |= cbor_encode_text_string(&rootMap, payload->uri, strlen(payload->uri));
            VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding rep href value");
        }
        if (payload->types)
        {
            OIC_LOG(INFO, TAG, "Payload has types");
            err |= OCStringLLJoin(&rootMap, OC_RSRVD_RESOURCE_TYPE, payload->types);
            VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding resource type.");
        }
        if (payload->interfaces)
        {
            OIC_LOG(INFO, TAG, "Payload has interfaces");
            err |= OCStringLLJoin(&rootMap, OC_RSRVD_INTERFACE, payload->interfaces);
            VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding platform interface type.");
        }

        err |= OCConvertSingleRepPayload(&rootMap, payload);
        VERIFY_CBOR_SUCCESS(TAG, err, "Failed setting rep payload");

        // Close main array
        err |= cbor_encoder_close_container(((arrayCount == 1) ? &encoder: &rootArray),
                &rootMap);
        VERIFY_CBOR_SUCCESS(TAG, err, "Failed closing root map");
        payload = payload->next;
    }
    if (arrayCount > 1)
    {
        err |= cbor_encoder_close_container(&encoder, &rootArray);
        VERIFY_CBOR_SUCCESS(TAG, err, "Failed closing root array");
    }

exit:
    return checkError(err, &encoder, outPayload, size);
}
Ejemplo n.º 21
0
static OCStackResult OCConvertPresencePayload(OCPresencePayload* payload,
        uint8_t** outPayload, size_t* size)
{
    *outPayload = (uint8_t*)OICCalloc(1, MAX_REQUEST_LENGTH);
    *size = MAX_REQUEST_LENGTH;

    if(!*outPayload)
    {
        return OC_STACK_NO_MEMORY;
    }

    CborEncoder encoder = {};
    bool err = false;

    cbor_encoder_init(&encoder, *outPayload, *size, 0);
    CborEncoder rootArray;

    err = err || cbor_encoder_create_array(&encoder, &rootArray, 2);
    err = err || cbor_encode_uint(&rootArray, PAYLOAD_TYPE_PRESENCE);


    CborEncoder map;
    err = err || cbor_encoder_create_map(&rootArray, &map, CborIndefiniteLength);

    // Sequence Number
    err = err || cbor_encode_text_string(&map,
            OC_RSRVD_NONCE,
            sizeof(OC_RSRVD_NONCE) - 1);
    err = err || cbor_encode_uint(&map, payload->sequenceNumber);

    // Max Age
    err = err || cbor_encode_text_string(&map,
            OC_RSRVD_TTL,
            sizeof(OC_RSRVD_TTL) - 1);
    err = err || cbor_encode_uint(&map, payload->maxAge);

    // Trigger
    const char* triggerStr = convertTriggerEnumToString(payload->trigger);
    err = err || AddTextStringToMap(&map, OC_RSRVD_TRIGGER, sizeof(OC_RSRVD_TRIGGER) - 1,
            triggerStr);

    // Resource type name
    if(payload->trigger != OC_PRESENCE_TRIGGER_DELETE)
    {
        err = err || ConditionalAddTextStringToMap(&map, OC_RSRVD_RESOURCE_TYPE,
                sizeof(OC_RSRVD_RESOURCE_TYPE) - 1, payload->resourceType);
    }

    // Close Map
    err = err || cbor_encoder_close_container(&rootArray, &map);
    err = err || cbor_encoder_close_container(&encoder, &rootArray);

    if(err)
    {
        OC_LOG_V(ERROR, TAG, "Convert Presence Payload failed with : %d", err);
        return OC_STACK_ERROR;
    }

    *size = encoder.ptr - *outPayload;
    uint8_t* tempPayload = (uint8_t*)OICRealloc(*outPayload, *size);

    if(!tempPayload)
    {
        OC_LOG_V(ERROR, TAG, PCF("Payload realloc failed!"));
        OICFree(*outPayload);
        return OC_STACK_ERROR;
    }

    *outPayload = tempPayload;

    return OC_STACK_OK;
}
Ejemplo n.º 22
0
static int64_t OCConvertDiscoveryPayload(OCDiscoveryPayload *payload, uint8_t *outPayload,
        size_t *size)
{
    CborEncoder encoder;
    int64_t err = CborNoError;

    cbor_encoder_init(&encoder, outPayload, *size, 0);

    if (payload->resources)
    {
        /*
        The format for the payload is "modelled" as JSON.

        [                                                       // rootArray
            {                                                   // rootMap
                "di" : UUID,                                    // device ID
                "rt": "oic.wk.res"
                "n":"MyDevice"
                "if":"oic.if.ll oic.if.baseline"
                "di": "0685B960-736F-46F7-BEC0-9E6CBD61ADC1",
                links :[                                        // linksArray contains maps of resources
                            {
                                href, rt, if, policy            // Resource 1
                            },
                            {
                                href, rt, if, policy            // Resource 2
                            },
                            .
                            .
                            .
                        ]
            }
        ]
        */
        // Open the main root array
        CborEncoder rootArray;
        size_t resourceCount =  OCDiscoveryPayloadGetResourceCount(payload);
        err |= cbor_encoder_create_array(&encoder, &rootArray, 1);
        VERIFY_CBOR_SUCCESS(TAG, err, "Failed creating discovery root array");

        // Open the root map in the root array
        CborEncoder rootMap;
        err |= cbor_encoder_create_map(&rootArray, &rootMap, CborIndefiniteLength);
        VERIFY_CBOR_SUCCESS(TAG, err, "Failed creating discovery map");

        // Insert Name
        err |= ConditionalAddTextStringToMap(&rootMap, OC_RSRVD_DEVICE_NAME,
                sizeof(OC_RSRVD_DEVICE_NAME) - 1, payload->name);
        VERIFY_CBOR_SUCCESS(TAG, err, "Failed setting name");

        // Insert Device ID into the root map
        err |= AddTextStringToMap(&rootMap, OC_RSRVD_DEVICE_ID, sizeof(OC_RSRVD_DEVICE_ID) - 1,
                payload->sid);
        VERIFY_CBOR_SUCCESS(TAG, err, "Failed setting device id");

        // Insert Resource Type
        err |= ConditionalAddTextStringToMap(&rootMap, OC_RSRVD_RESOURCE_TYPE,
                sizeof(OC_RSRVD_RESOURCE_TYPE) - 1, payload->type);
        VERIFY_CBOR_SUCCESS(TAG, err, "Failed setting RT");

        // Insert interfaces
        if (payload->interface)
        {
            err |= OCStringLLJoin(&rootMap, OC_RSRVD_INTERFACE, payload->interface);
            VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding interface types tag/value");
        }

        // Insert baseURI if present
        err |= ConditionalAddTextStringToMap(&rootMap, OC_RSRVD_BASE_URI,
                                            sizeof(OC_RSRVD_BASE_URI) - 1,
                                            payload->baseURI);
        VERIFY_CBOR_SUCCESS(TAG, err, "Failed setting baseURI");

        // Insert Links into the root map.
        CborEncoder linkArray;
        err |= cbor_encode_text_string(&rootMap, OC_RSRVD_LINKS, sizeof(OC_RSRVD_LINKS) - 1);
        VERIFY_CBOR_SUCCESS(TAG, err, "Failed setting links array tag");
        err |= cbor_encoder_create_array(&rootMap, &linkArray, resourceCount);
        VERIFY_CBOR_SUCCESS(TAG, err, "Failed setting links array");

        for(size_t i = 0; i < resourceCount; ++i)
        {
            CborEncoder linkMap;
            OCResourcePayload *resource = OCDiscoveryPayloadGetResource(payload, i);
            VERIFY_PARAM_NON_NULL(TAG, resource, "Failed retrieving resource");

            // resource map inside the links array.
            err |= cbor_encoder_create_map(&linkArray, &linkMap, LINKS_MAP_LEN);
            VERIFY_CBOR_SUCCESS(TAG, err, "Failed creating links map");

            // Below are insertions of the resource properties into the map.
            // Uri
            err |= AddTextStringToMap(&linkMap, OC_RSRVD_HREF, sizeof(OC_RSRVD_HREF) - 1,
                    resource->uri);
            VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding uri to links map");

            // Resource Type
            if (resource->types)
            {
                err |= OCStringLLJoin(&linkMap, OC_RSRVD_RESOURCE_TYPE, resource->types);
                VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding resourceType tag/value to links map");
            }
            // Interface Types
            if (resource->interfaces)
            {
                err |= OCStringLLJoin(&linkMap, OC_RSRVD_INTERFACE, resource->interfaces);
                VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding interfaces tag/value to links map");
            }

            // Policy
            CborEncoder policyMap;
            err |= cbor_encode_text_string(&linkMap, OC_RSRVD_POLICY, sizeof(OC_RSRVD_POLICY) - 1);
            VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding policy tag to links map");
            err |= cbor_encoder_create_map(&linkMap, &policyMap, CborIndefiniteLength);
            VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding policy map to links map");

            // Bitmap
            err |=  cbor_encode_text_string(&policyMap, OC_RSRVD_BITMAP, sizeof(OC_RSRVD_BITMAP) - 1);
            VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding bitmap tag to policy map");
            err |= cbor_encode_uint(&policyMap, resource->bitmap);
            VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding bitmap value to policy map");

            if (resource->secure)
            {
                err |= cbor_encode_text_string(&policyMap, OC_RSRVD_SECURE,
                        sizeof(OC_RSRVD_SECURE) - 1);
                VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding secure tag to policy map");
                err |= cbor_encode_boolean(&policyMap, OC_RESOURCE_SECURE);
                VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding secure value to policy map");
            }
            if ((resource->secure && resource->port != 0) || payload->baseURI)
            {
                err |= cbor_encode_text_string(&policyMap, OC_RSRVD_HOSTING_PORT,
                        sizeof(OC_RSRVD_HOSTING_PORT) - 1);
                VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding secure port tag");
                err |= cbor_encode_uint(&policyMap, resource->port);
                VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding secure port value");
            }

#ifdef TCP_ADAPTER
            err |= cbor_encode_text_string(&policyMap, OC_RSRVD_TCP_PORT,
                                           sizeof(OC_RSRVD_TCP_PORT) - 1);
            VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding tcp port tag");
            err |= cbor_encode_uint(&policyMap, resource->tcpPort);
            VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding tcp port value");
#endif

            err |= cbor_encoder_close_container(&linkMap, &policyMap);
            VERIFY_CBOR_SUCCESS(TAG, err, "Failed closing policy map");

            // Finsihed encoding a resource, close the map.
            err |= cbor_encoder_close_container(&linkArray, &linkMap);
            VERIFY_CBOR_SUCCESS(TAG, err, "Failed closing link map");
        }
        // Close links array inside the root map.
        err |= cbor_encoder_close_container(&rootMap, &linkArray);
        VERIFY_CBOR_SUCCESS(TAG, err, "Failed closing link array");
        // close root map inside the root array.
        err |= cbor_encoder_close_container(&rootArray, &rootMap);
        VERIFY_CBOR_SUCCESS(TAG, err, "Failed closing root map");
        // Close the final root array.
        err |= cbor_encoder_close_container(&encoder, &rootArray);
        VERIFY_CBOR_SUCCESS(TAG, err, "Failed closing root array");
    }

exit:
    return checkError(err, &encoder, outPayload, size);
}
Ejemplo n.º 23
0
static int64_t OCConvertDiscoveryPayload(OCDiscoveryPayload* payload, uint8_t* outPayload,
        size_t* size)
{
    CborEncoder encoder = {0};
    CborEncoder rootArray = {0};
    int64_t err = 0;

    cbor_encoder_init(&encoder, outPayload, *size, 0);

    if (payload->collectionResources)
    {
        CborError cborEncoderResult;
        cborEncoderResult = cbor_encoder_create_array(&encoder, &rootArray, DISCOVERY_CBOR_ARRAY_LEN);
        if (CborNoError != cborEncoderResult)
        {
            OC_LOG(ERROR, TAG, "Failed creating root array.");
            goto cbor_error;
        }

        CborEncoder colArray;
        cborEncoderResult = cbor_encoder_create_array(&rootArray, &colArray, CborIndefiniteLength);
        if (CborNoError != cborEncoderResult)
        {
            OC_LOG(ERROR, TAG, "Failed creating collection array.");
            goto cbor_error;
        }
        OCResourceCollectionPayload *colResources = payload->collectionResources;
        while (colResources)
        {
            if (OC_STACK_OK != OCTagsPayloadToCbor(colResources->tags, &colArray))
            {
                goto cbor_error;
            }
            if (OC_STACK_OK != OCLinksPayloadToCbor(colResources->setLinks, &colArray))
            {
                goto cbor_error;
            }
            colResources = colResources->next;
        }
        cborEncoderResult = cbor_encoder_close_container(&rootArray, &colArray);
        if (CborNoError != cborEncoderResult)
        {
            OC_LOG(ERROR, TAG, "Failed closing collection array.");
            goto cbor_error;
        }
        cborEncoderResult = cbor_encoder_close_container(&encoder, &rootArray);
        if (CborNoError != cborEncoderResult)
        {
            OC_LOG(ERROR, TAG, "Failed closing root array.");
            goto cbor_error;
        }
    }
    else if (payload->resources)
    {
        size_t resourceCount =  OCDiscoveryPayloadGetResourceCount(payload);
        err = err | cbor_encoder_create_array(&encoder, &rootArray, resourceCount);

        for(size_t i = 0; i < resourceCount; ++i)
        {
            CborEncoder map;
            OCResourcePayload* resource = OCDiscoveryPayloadGetResource(payload, i);
            if(!resource)
            {
                OICFree(outPayload);
                return OC_STACK_INVALID_PARAM;
            }

            err = err | cbor_encoder_create_map(&rootArray, &map, DISCOVERY_CBOR_RES_MAP_LEN);

            // Device ID
            err = err | cbor_encode_text_string(&map, OC_RSRVD_DEVICE_ID,
                    sizeof(OC_RSRVD_DEVICE_ID) - 1);
            err = err | cbor_encode_byte_string(&map, resource->sid, UUID_SIZE);

            {
                CborEncoder linkArray;
                err = err | cbor_encode_text_string(&map, OC_RSRVD_LINKS, sizeof(OC_RSRVD_LINKS) -1);
                err = err | cbor_encoder_create_array(&map, &linkArray, CborIndefiniteLength);

                // Link Map
                {
                    CborEncoder linkMap;
                    err = err | cbor_encoder_create_map(&linkArray, &linkMap, DISCOVERY_CBOR_LINKS_MAP_LEN);

                    // Uri
                    err = err | AddTextStringToMap(&linkMap, OC_RSRVD_HREF,
                            sizeof(OC_RSRVD_HREF) - 1,
                            resource->uri);
                    // Resource Type
                    if (resource->types)
                    {
                        char* joinedTypes = OCStringLLJoin(resource->types);
                        if (joinedTypes)
                        {
                            err = err | cbor_encode_text_string(&linkMap, OC_RSRVD_RESOURCE_TYPE,
                                    sizeof(OC_RSRVD_RESOURCE_TYPE) - 1);
                            err = err | cbor_encode_text_string(&linkMap, joinedTypes,
                                    strlen(joinedTypes));
                            OICFree(joinedTypes);
                        }
                        else
                        {
                            return OC_STACK_NO_MEMORY;
                        }
                    }
                    // Interface Types
                    if (resource->interfaces)
                    {
                        char* joinedInterfaces = OCStringLLJoin(resource->interfaces);
                        if (joinedInterfaces)
                        {
                            err = err | cbor_encode_text_string(&linkMap, OC_RSRVD_INTERFACE,
                                    sizeof(OC_RSRVD_INTERFACE) - 1);
                            err = err | cbor_encode_text_string(&linkMap, joinedInterfaces,
                                    strlen(joinedInterfaces));
                            OICFree(joinedInterfaces);
                        }
                        else
                        {
                            return OC_STACK_NO_MEMORY;
                        }
                    }
                    // Policy
                    {
                        CborEncoder policyMap;
                        err = err | cbor_encode_text_string(&linkMap, OC_RSRVD_POLICY,
                                sizeof(OC_RSRVD_POLICY) - 1);
                        err = err | cbor_encoder_create_map(&linkMap, &policyMap, CborIndefiniteLength);

                        // Bitmap
                        err = err | cbor_encode_text_string(&policyMap, OC_RSRVD_BITMAP,
                                sizeof(OC_RSRVD_BITMAP) - 1);
                        err = err | cbor_encode_uint(&policyMap, resource->bitmap);

                        if(resource->secure)
                        {
                            err = err | cbor_encode_text_string(&policyMap, OC_RSRVD_SECURE,
                                    sizeof(OC_RSRVD_SECURE) - 1);
                            err = err | cbor_encode_boolean(&policyMap, OC_RESOURCE_SECURE);

                            if(resource->port != 0)
                            {
                                err = err | cbor_encode_text_string(&policyMap, OC_RSRVD_HOSTING_PORT,
                                        sizeof(OC_RSRVD_HOSTING_PORT) - 1);
                                err = err | cbor_encode_uint(&policyMap, resource->port);
                            }
                        }

                        err = err | cbor_encoder_close_container(&linkMap, &policyMap);
                    }
                    // Close
                    err = err | cbor_encoder_close_container(&linkArray, &linkMap);
                }
                err = err | cbor_encoder_close_container(&map, &linkArray);
            }
            err = err | cbor_encoder_close_container(&rootArray, &map);
        }
        // Close main array
        err = err | cbor_encoder_close_container(&encoder, &rootArray);
    }

    return checkError(err, &encoder, outPayload, size);
cbor_error:
    OICFree(outPayload);
    return OC_STACK_ERROR;
}
Ejemplo n.º 24
0
OCStackResult OCLinksPayloadToCbor(OCLinksPayload *rtPtr, CborEncoder *setMap)
{
    CborEncoder linksArray;
    CborError cborEncoderResult;

    cborEncoderResult = cbor_encoder_create_array(setMap, &linksArray, CborIndefiniteLength);
    if (CborNoError != cborEncoderResult)
    {
        OC_LOG(ERROR, TAG, "Failed creating LINKS array.");
        return OC_STACK_ERROR;
    }
    while (rtPtr)
    {
        CborEncoder linksMap;
        cborEncoderResult = cbor_encoder_create_map(&linksArray, &linksMap,
                CborIndefiniteLength);
        if (CborNoError != cborEncoderResult)
        {
            OC_LOG(ERROR, TAG, "Failed creating LINKS map.");
            return OC_STACK_ERROR;
        }
        if (CborNoError != ConditionalAddTextStringToMap(&linksMap, OC_RSRVD_HREF,
                sizeof(OC_RSRVD_HREF) - 1, rtPtr->href))
        {
            OC_LOG(ERROR, TAG, "Failed adding OC_RSRVD_HREF in LINKS map.");
            return OC_STACK_ERROR;
        }
        if (CborNoError != ConditionalAddTextStringToMap(&linksMap, OC_RSRVD_REL,
                sizeof(OC_RSRVD_REL) - 1,  rtPtr->rel))
        {
            OC_LOG(ERROR, TAG, "Failed adding OC_RSRVD_REL in LINKS map.");
            return OC_STACK_ERROR;
        }
        if (CborNoError != ConditionalAddTextStringToMap(&linksMap, OC_RSRVD_TITLE,
                sizeof(OC_RSRVD_TITLE) - 1, rtPtr->title))
        {
            OC_LOG(ERROR, TAG, "Failed adding OC_RSRVD_TITLE in LINKS map.");
            return OC_STACK_ERROR;
        }
        if (CborNoError != ConditionalAddTextStringToMap(&linksMap, OC_RSRVD_URI,
                sizeof(OC_RSRVD_URI) - 1, rtPtr->uri))
        {
            OC_LOG(ERROR, TAG, "Failed adding OC_RSRVD_URI in LINKS map.");
            return OC_STACK_ERROR;
        }
        if (CborNoError != AddStringLLToMap(&linksMap, OC_RSRVD_RESOURCE_TYPE,
                sizeof(OC_RSRVD_RESOURCE_TYPE) - 1, rtPtr->rt))
        {
            OC_LOG(ERROR, TAG, "Failed adding OC_RSRVD_RESOURCE_TYPE in LINKS map.");
            return OC_STACK_ERROR;
        }
        if (CborNoError != AddStringLLToMap(&linksMap, OC_RSRVD_INTERFACE,
                sizeof(OC_RSRVD_INTERFACE) - 1, rtPtr->itf))
        {
            OC_LOG(ERROR, TAG, "Failed adding OC_RSRVD_INTERFACE in LINKS map.");
            return OC_STACK_ERROR;
        }
        if (CborNoError != AddStringLLToMap(&linksMap, OC_RSRVD_MEDIA_TYPE,
                sizeof(OC_RSRVD_MEDIA_TYPE) - 1, rtPtr->mt))
        {
            OC_LOG(ERROR, TAG, "Failed adding OC_RSRVD_MEDIA_TYPE in LINKS map.");
            return OC_STACK_ERROR;
        }
        uint64_t temp = (uint64_t)rtPtr->ins;
        if (CborNoError != ConditionalAddIntToMap(&linksMap, OC_RSRVD_INS,
            sizeof(OC_RSRVD_INS) - 1, &temp))
        {
            OC_LOG(ERROR, TAG, "Failed adding OC_RSRVD_INS in LINKS map.");
            return OC_STACK_ERROR;
        }
        cborEncoderResult = cbor_encoder_close_container(&linksArray, &linksMap);
        if (CborNoError != cborEncoderResult)
        {
            OC_LOG(ERROR, TAG, "Failed closing LINKS map.");
            return OC_STACK_ERROR;
        }
        rtPtr = rtPtr->next;
    }
    cborEncoderResult = cbor_encoder_close_container(setMap, &linksArray);
    if (CborNoError != cborEncoderResult)
    {
        OC_LOG(ERROR, TAG, "Failed closing LINKS array.");
        return OC_STACK_ERROR;;
    }
    return OC_STACK_OK;
}
Ejemplo n.º 25
0
int64_t OCRDPayloadToCbor(const OCRDPayload *rdPayload, uint8_t *outPayload, size_t *size)
{
    if (!outPayload || !size)
    {
        OC_LOG(ERROR, TAG, "Invalid parameters.");
        return OC_STACK_INVALID_PARAM;
    }

    CborEncoder encoder;
    int flags = 0;
    cbor_encoder_init(&encoder, outPayload, *size, flags);

    CborEncoder rootArray;
    CborError cborEncoderResult;
    cborEncoderResult = cbor_encoder_create_array(&encoder, &rootArray, CBOR_ROOT_ARRAY_LENGTH);
    if (CborNoError != cborEncoderResult)
    {
        OC_LOG(ERROR, TAG, "Failed creating cbor array.");
        goto cbor_error;
    }

    if (rdPayload->rdDiscovery)
    {
        CborEncoder map;
        cborEncoderResult = cbor_encoder_create_map(&rootArray, &map, CborIndefiniteLength);
        if (CborNoError != cborEncoderResult)
        {
            OC_LOG(ERROR, TAG, "Failed creating discovery map.");
            goto cbor_error;
        }
        if (CborNoError != ConditionalAddTextStringToMap(&map, OC_RSRVD_DEVICE_NAME,
                sizeof(OC_RSRVD_DEVICE_NAME) - 1, (char *)rdPayload->rdDiscovery->n.deviceName))
        {
            OC_LOG(ERROR, TAG, "Failed setting OC_RSRVD_DEVICE_NAME.");
            goto cbor_error;
        }
        if (CborNoError != ConditionalAddTextStringToMap(&map, OC_RSRVD_DEVICE_ID,
                sizeof(OC_RSRVD_DEVICE_ID) - 1, (char *)rdPayload->rdDiscovery->di.id))
        {
            OC_LOG(ERROR, TAG, "Failed setting OC_RSRVD_DEVICE_ID.");
            goto cbor_error;
        }
        uint64_t sel = (uint8_t) rdPayload->rdDiscovery->sel;
        if (CborNoError != ConditionalAddIntToMap(&map, OC_RSRVD_RD_DISCOVERY_SEL,
            sizeof(OC_RSRVD_RD_DISCOVERY_SEL) - 1, &sel))
        {
            OC_LOG(ERROR, TAG, "Failed setting OC_RSRVD_RD_DISCOVERY_SEL.");
            goto cbor_error;
        }
        cborEncoderResult = cbor_encoder_close_container(&rootArray, &map);
        if (CborNoError != cborEncoderResult)
        {
            OC_LOG(ERROR, TAG, "Failed closing discovery map.");
            goto cbor_error;
        }
    }
    else if (rdPayload->rdPublish)
    {
        CborEncoder colArray;
        cborEncoderResult = cbor_encoder_create_array(&rootArray, &colArray, CborIndefiniteLength);
        if (CborNoError != cborEncoderResult)
        {
            OC_LOG(ERROR, TAG, "Failed creating collection array.");
            goto cbor_error;
        }

        OCResourceCollectionPayload *rdPublish = rdPayload->rdPublish;
        while (rdPublish)
        {
            if (OC_STACK_OK != OCTagsPayloadToCbor(rdPublish->tags, &colArray))
            {
                OC_LOG(ERROR, TAG, "Failed creating tags payload.");
                goto cbor_error;
            }
            if (OC_STACK_OK != OCLinksPayloadToCbor(rdPublish->setLinks, &colArray))
            {
                OC_LOG(ERROR, TAG, "Failed creating links payload.");
                goto cbor_error;
            }
            rdPublish = rdPublish->next;
        }
        cborEncoderResult = cbor_encoder_close_container(&rootArray, &colArray);
        if (CborNoError != cborEncoderResult)
        {
            OC_LOG(ERROR, TAG, "Failed closing collection array.");
            goto cbor_error;
        }
    }
    cborEncoderResult = cbor_encoder_close_container(&encoder, &rootArray);
    if (CborNoError != cborEncoderResult)
    {
        OC_LOG(ERROR, TAG, "Failed closing root array container. ");
        goto cbor_error;
    }

    *size = encoder.ptr - outPayload;
    return OC_STACK_OK;

cbor_error:
    OICFree(outPayload);
    return OC_STACK_ERROR;
}
Ejemplo n.º 26
0
static int64_t OCConvertPlatformPayload(OCPlatformPayload* payload, uint8_t* outPayload,
        size_t* size)
{
    CborEncoder encoder = {0};
    int64_t err = 0;

    cbor_encoder_init(&encoder, outPayload, *size, 0);
    CborEncoder rootArray;
    err = err | cbor_encoder_create_array(&encoder, &rootArray, 1);
    {
        CborEncoder map;
        err = err | cbor_encoder_create_map(&rootArray, &map, CborIndefiniteLength);

        // uri
        err = err | ConditionalAddTextStringToMap(&map, OC_RSRVD_HREF, sizeof(OC_RSRVD_HREF) - 1,
                payload->uri);

        // Rep Map
        {
            CborEncoder repMap;
            err = err | cbor_encode_text_string(&map, OC_RSRVD_REPRESENTATION,
                    sizeof(OC_RSRVD_REPRESENTATION) - 1);
            err = err | cbor_encoder_create_map(&map, &repMap, CborIndefiniteLength);

            // Platform ID
            err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_PLATFORM_ID,
                    sizeof(OC_RSRVD_PLATFORM_ID) - 1,
                    payload->info.platformID);

            // MFG Name
            err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_MFG_NAME,
                    sizeof(OC_RSRVD_MFG_NAME) - 1,
                    payload->info.manufacturerName);

            // MFG Url
            err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_MFG_URL,
                    sizeof(OC_RSRVD_MFG_URL) - 1,
                    payload->info.manufacturerUrl);

            // Model Num
            err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_MODEL_NUM,
                    sizeof(OC_RSRVD_MODEL_NUM) - 1,
                    payload->info.modelNumber);

            // Date of Mfg
            err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_MFG_DATE,
                    sizeof(OC_RSRVD_MFG_DATE) - 1,
                    payload->info.dateOfManufacture);

            // Platform Version
            err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_PLATFORM_VERSION,
                    sizeof(OC_RSRVD_PLATFORM_VERSION) - 1,
                    payload->info.platformVersion);

            // OS Version
            err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_OS_VERSION,
                    sizeof(OC_RSRVD_OS_VERSION) - 1,
                    payload->info.operatingSystemVersion);

            // Hardware Version
            err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_HARDWARE_VERSION,
                    sizeof(OC_RSRVD_HARDWARE_VERSION) - 1,
                    payload->info.hardwareVersion);

            // Firmware Version
            err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_FIRMWARE_VERSION,
                    sizeof(OC_RSRVD_FIRMWARE_VERSION) - 1,
                    payload->info.firmwareVersion);

            // Support URL
            err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_SUPPORT_URL,
                    sizeof(OC_RSRVD_SUPPORT_URL) - 1,
                    payload->info.supportUrl);

            // System Time
            err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_SYSTEM_TIME,
                    sizeof(OC_RSRVD_SYSTEM_TIME) - 1,
                    payload->info.systemTime);
            err = err | cbor_encoder_close_container(&map, &repMap);
        }

        // Close Map
        err = err | cbor_encoder_close_container(&rootArray, &map);
    }

    // Close main array
    err = err | cbor_encoder_close_container(&encoder, &rootArray);

    return checkError(err, &encoder, outPayload, size);
}
int64_t OCRDPayloadToCbor(const OCRDPayload *rdPayload, uint8_t *outPayload, size_t *size)
{
    int64_t cborEncoderResult = CborErrorIO;
    int flags = 0;
    CborEncoder encoder;
    VERIFY_PARAM_NON_NULL(TAG, rdPayload, "Invalid input parameter rdPayload");
    VERIFY_PARAM_NON_NULL(TAG, outPayload, "Invalid input parameter outPayload");
    VERIFY_PARAM_NON_NULL(TAG, size, "Invalid input parameter size");

    cbor_encoder_init(&encoder, outPayload, *size, flags);

    if (rdPayload->rdDiscovery)
    {
        CborEncoder map;
        cborEncoderResult |= cbor_encoder_create_map(&encoder, &map, CborIndefiniteLength);
        VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to create discovery map");

        cborEncoderResult |= ConditionalAddTextStringToMap(&map, OC_RSRVD_DEVICE_NAME,
            rdPayload->rdDiscovery->n.deviceName);
        VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_DEVICE_NAME in map");

        cborEncoderResult |= ConditionalAddTextStringToMap(&map, OC_RSRVD_DEVICE_ID,
            (char *)rdPayload->rdDiscovery->di.id);
        VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_DEVICE_ID in map");

        {
            uint64_t value = rdPayload->rdDiscovery->sel;
            cborEncoderResult |= ConditionalAddIntToMap(&map, OC_RSRVD_RD_DISCOVERY_SEL, &value);
            VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add RD_DISCOVERY_SEL in map");
        }
        cborEncoderResult |= cbor_encoder_close_container(&encoder, &map);
        VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed closing discovery map");
    }
    else if (rdPayload->rdPublish)
    {
        CborEncoder colArray;
        cborEncoderResult |= cbor_encoder_create_array(&encoder, &colArray, CborIndefiniteLength);
        VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to create collection array");

        OCResourceCollectionPayload *rdPublish = rdPayload->rdPublish;
        while (rdPublish)
        {
            cborEncoderResult |= OCTagsPayloadToCbor(rdPublish->tags, &colArray);
            VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed adding tags payload");
            cborEncoderResult |= OCLinksPayloadToCbor(rdPublish->setLinks, &colArray);
            VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed adding setLinks payload");
            rdPublish = rdPublish->next;
        }
        cborEncoderResult |= cbor_encoder_close_container(&encoder, &colArray);
        VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed closing collection array");
    }
    else
    {
        CborEncoder map;
        cborEncoderResult |= cbor_encoder_create_map(&encoder, &map, CborIndefiniteLength);
        VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed entering discovery map");
        cborEncoderResult |= cbor_encoder_close_container(&encoder, &map);
        VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed closing discovery map");
    }

    if (cborEncoderResult == CborErrorOutOfMemory)
    {
        *size += encoder.ptr - encoder.end;
    }
    else
    {
        *size = encoder.ptr - outPayload;
    }

    return cborEncoderResult;

exit:
    OICFree(outPayload);
    return cborEncoderResult;
}
Ejemplo n.º 28
0
int
imgmgr_state_read(struct mgmt_cbuf *cb)
{
    int i;
    int rc;
    uint32_t flags;
    struct image_version ver;
    uint8_t hash[IMGMGR_HASH_LEN]; /* SHA256 hash */
    char vers_str[IMGMGR_NMGR_MAX_VER];
    int any_non_bootable;
    int split_status;
    uint8_t state_flags;
    CborError g_err = CborNoError;
    CborEncoder *penc = &cb->encoder;
    CborEncoder rsp, images, image;

    any_non_bootable = 0;

    g_err |= cbor_encoder_create_map(penc, &rsp, CborIndefiniteLength);
    g_err |= cbor_encode_text_stringz(&rsp, "images");

    g_err |= cbor_encoder_create_array(&rsp, &images, CborIndefiniteLength);
    for (i = 0; i < 2; i++) {
        rc = imgr_read_info(i, &ver, hash, &flags);
        if (rc != 0) {
            continue;
        }

        if (flags & IMAGE_F_NON_BOOTABLE) {
            any_non_bootable = 1;
        }

        state_flags = imgmgr_state_flags(i);

        g_err |= cbor_encoder_create_map(&images, &image, CborIndefiniteLength);
        g_err |= cbor_encode_text_stringz(&image, "slot");
        g_err |= cbor_encode_int(&image, i);

        g_err |= cbor_encode_text_stringz(&image, "version");
        imgr_ver_str(&ver, vers_str);
        g_err |= cbor_encode_text_stringz(&image, vers_str);

        g_err |= cbor_encode_text_stringz(&image, "hash");
        g_err |= cbor_encode_byte_string(&image, hash, IMGMGR_HASH_LEN);

        g_err |= cbor_encode_text_stringz(&image, "bootable");
        g_err |= cbor_encode_boolean(&image, !(flags & IMAGE_F_NON_BOOTABLE));

        g_err |= cbor_encode_text_stringz(&image, "pending");
        g_err |= cbor_encode_boolean(&image,
                                     state_flags & IMGMGR_STATE_F_PENDING);

        g_err |= cbor_encode_text_stringz(&image, "confirmed");
        g_err |= cbor_encode_boolean(&image,
                                     state_flags & IMGMGR_STATE_F_CONFIRMED);

        g_err |= cbor_encode_text_stringz(&image, "active");
        g_err |= cbor_encode_boolean(&image,
                                     state_flags & IMGMGR_STATE_F_ACTIVE);

        g_err |= cbor_encoder_close_container(&images, &image);
    }

    g_err |= cbor_encoder_close_container(&rsp, &images);

    if (any_non_bootable) {
        split_status = split_check_status();
    } else {
        split_status = SPLIT_STATUS_INVALID;
    }

    g_err |= cbor_encode_text_stringz(&rsp, "splitStatus");
    g_err |= cbor_encode_int(&rsp, split_status);

    g_err |= cbor_encoder_close_container(penc, &rsp);

    if (g_err) {
        return MGMT_ERR_ENOMEM;
    }
    return 0;
}
Ejemplo n.º 29
0
static int64_t OCLinksPayloadToCbor(OCLinksPayload *links, CborEncoder *setMap)
{
    CborEncoder linksArray;
    int64_t cborEncoderResult = CborNoError;

    cborEncoderResult |= cbor_encode_text_string(setMap, OC_RSRVD_LINKS,
        sizeof(OC_RSRVD_LINKS) - 1);

    cborEncoderResult |= cbor_encoder_create_array(setMap, &linksArray, CborIndefiniteLength);
    VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to create Links array");

    while (links)
    {
        CborEncoder linksMap;
        cborEncoderResult |= cbor_encoder_create_map(&linksArray, &linksMap, CborIndefiniteLength);
        VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to create links map");

        cborEncoderResult |= ConditionalAddTextStringToMap(&linksMap, OC_RSRVD_HREF, links->href);
        VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_HREF in links map");

        cborEncoderResult |= ConditionalAddTextStringToMap(&linksMap, OC_RSRVD_REL, links->rel);
        VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_REL in links map");

        cborEncoderResult |= AddStringLLToMap(&linksMap, OC_RSRVD_RESOURCE_TYPE, links->rt);
        VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_RT in links map");

        cborEncoderResult |= AddStringLLToMap(&linksMap, OC_RSRVD_INTERFACE, links->itf);
        VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_ITF in links map");

        // Policy
        CborEncoder policyMap;
        cborEncoderResult |= cbor_encode_text_string(&linksMap, OC_RSRVD_POLICY,
            sizeof(OC_RSRVD_POLICY) - 1);
        VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed adding policy tag to links map");
        cborEncoderResult |= cbor_encoder_create_map(&linksMap, &policyMap, CborIndefiniteLength);
        VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed adding policy map to links map");

        // Bitmap
        cborEncoderResult |= cbor_encode_text_string(&policyMap, OC_RSRVD_BITMAP,
            sizeof(OC_RSRVD_BITMAP) - 1);
        VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed adding bitmap tag to policy map");
        cborEncoderResult |= cbor_encode_uint(&policyMap, links->p);
        VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed adding bitmap value to policy map");

        cborEncoderResult |= cbor_encoder_close_container(&linksMap, &policyMap);
        VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed closing policy map");

        cborEncoderResult |= ConditionalAddTextStringToMap(&linksMap, OC_RSRVD_TITLE, links->title);
        VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_TITLE in links map");

        cborEncoderResult |= ConditionalAddTextStringToMap(&linksMap, OC_RSRVD_URI, links->anchor);
        VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_URI in links map");

        cborEncoderResult |= ConditionalAddIntToMap(&linksMap, OC_RSRVD_INS, (uint64_t *) &links->ins);
        VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_INS in links map");

        cborEncoderResult |= ConditionalAddIntToMap(&linksMap, OC_RSRVD_TTL, &links->ttl);
        VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_TTL in links map");

        cborEncoderResult |= AddStringLLToMap(&linksMap, OC_RSRVD_MEDIA_TYPE, links->type);
        VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_MT in links map");

        // Finsihed encoding a resource, close the map.
        cborEncoderResult |= cbor_encoder_close_container(&linksArray, &linksMap);
        VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed closing links map");

        links = links->next;
    }
    cborEncoderResult |= cbor_encoder_close_container(setMap, &linksArray);
    VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed closing links array");

exit:
    return cborEncoderResult;
}
Ejemplo n.º 30
0
CborError
sol_oic_encode_cbor_repr(struct sol_coap_packet *pkt,
    const char *href, const struct sol_vector *repr_vec)
{
    CborEncoder encoder, rep_map, array, map;
    CborError err;
    uint8_t *payload;
    uint16_t size;

    if (!repr_vec)
        return CborNoError;

    if (sol_coap_packet_get_payload(pkt, &payload, &size) < 0) {
        SOL_WRN("Could not get CoAP payload");
        return CborUnknownError;
    }

    cbor_encoder_init(&encoder, payload, size, 0);

    err = cbor_encoder_create_array(&encoder, &array, CborIndefiniteLength);
    err |= cbor_encode_uint(&array, SOL_OIC_PAYLOAD_REPRESENTATION);

    err |= cbor_encoder_create_map(&array, &map, CborIndefiniteLength);

    err |= cbor_encode_text_stringz(&map, SOL_OIC_KEY_HREF);
    err |= cbor_encode_text_stringz(&map, href);

    err |= cbor_encode_text_stringz(&map, SOL_OIC_KEY_REPRESENTATION);
    err |= cbor_encoder_create_map(&map, &rep_map, CborIndefiniteLength);

    if (repr_vec) {
        struct sol_oic_repr_field *repr;
        uint16_t idx;

        SOL_VECTOR_FOREACH_IDX (repr_vec, repr, idx) {
            if (err != CborNoError)
                break;

            err |= cbor_encode_text_stringz(&rep_map, repr->key);

            switch (repr->type) {
            case SOL_OIC_REPR_TYPE_UINT:
                err |= cbor_encode_uint(&rep_map, repr->v_uint);
                break;
            case SOL_OIC_REPR_TYPE_INT:
                err |= cbor_encode_int(&rep_map, repr->v_int);
                break;
            case SOL_OIC_REPR_TYPE_SIMPLE:
                err |= cbor_encode_simple_value(&rep_map, repr->v_simple);
                break;
            case SOL_OIC_REPR_TYPE_TEXT_STRING: {
                const char *p = repr->v_slice.data ? repr->v_slice.data : "";

                err |= cbor_encode_text_string(&rep_map, p, repr->v_slice.len);
                break;
            }
            case SOL_OIC_REPR_TYPE_BYTE_STRING: {
                const uint8_t *empty = (const uint8_t *)"";
                const uint8_t *p = repr->v_slice.data ? (const uint8_t *)repr->v_slice.data : empty;

                err |= cbor_encode_byte_string(&rep_map, p, repr->v_slice.len);
                break;
            }
            case SOL_OIC_REPR_TYPE_HALF_FLOAT:
                err |= cbor_encode_half_float(&rep_map, repr->v_voidptr);
                break;
            case SOL_OIC_REPR_TYPE_FLOAT:
                err |= cbor_encode_float(&rep_map, repr->v_float);
                break;
            case SOL_OIC_REPR_TYPE_DOUBLE:
                err |= cbor_encode_double(&rep_map, repr->v_double);
                break;
            case SOL_OIC_REPR_TYPE_BOOLEAN:
                err |= cbor_encode_boolean(&rep_map, repr->v_boolean);
                break;
            default:
                if (err == CborNoError)
                    err = CborErrorUnknownType;
            }
        }
    }

    err |= cbor_encoder_close_container(&map, &rep_map);

    err |= cbor_encoder_close_container(&array, &map);

    err |= cbor_encoder_close_container(&encoder, &array);

    if (err == CborNoError)
        sol_coap_packet_set_payload_used(pkt, encoder.ptr - payload);

    return err;
}