Example #1
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);
}
Example #2
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);
}
Example #3
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;
}
Example #4
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;
}
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;
}
Example #6
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;
}
Example #7
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;
}
Example #8
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);
}
Example #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;
}
Example #10
0
static int64_t OCConvertDevicePayload(OCDevicePayload *payload, uint8_t *outPayload,
        size_t *size)
{
    if (!payload)
    {
        return CborUnknownError;
    }
    int64_t err = CborNoError;
    CborEncoder encoder;

    cbor_encoder_init(&encoder, outPayload, *size, 0);
    CborEncoder repMap;
    err |= cbor_encoder_create_map(&encoder, &repMap, CborIndefiniteLength);
    VERIFY_CBOR_SUCCESS(TAG, err, "Failed creating device map");

    // Resource Type
    if (payload->types)
    {
        OIC_LOG(INFO, TAG, "Payload has types");
        err |= OCStringLLJoin(&repMap, OC_RSRVD_RESOURCE_TYPE, payload->types);
        VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding resource type tag/value.");
    }

    if (payload->interfaces)
    {
        OIC_LOG(INFO, TAG, "Payload has interface");
        err |= OCStringLLJoin(&repMap, OC_RSRVD_INTERFACE, payload->interfaces);
        VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding interface type tag/value.");
    }

    // Device ID
    err |= AddTextStringToMap(&repMap, OC_RSRVD_DEVICE_ID, sizeof(OC_RSRVD_DEVICE_ID) - 1 , payload->sid);
    VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding device id");

    // Device Name
    err |= ConditionalAddTextStringToMap(&repMap, OC_RSRVD_DEVICE_NAME,
            sizeof(OC_RSRVD_DEVICE_NAME) - 1, payload->deviceName);
    VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding device name");

    // Device Spec Version
    err |= ConditionalAddTextStringToMap(&repMap, OC_RSRVD_SPEC_VERSION,
            sizeof(OC_RSRVD_SPEC_VERSION) - 1, payload->specVersion);
    VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding data spec version");

    // Device data Model Version
    err |= ConditionalAddTextStringToMap(&repMap, OC_RSRVD_DATA_MODEL_VERSION,
            sizeof(OC_RSRVD_DATA_MODEL_VERSION) - 1, payload->dataModelVersion);
    VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding data model version");

    err |= cbor_encoder_close_container(&encoder, &repMap);
    VERIFY_CBOR_SUCCESS(TAG, err, "Failed closing device map");

exit:
    return checkError(err, &encoder, outPayload, size);
}
Example #11
0
static int64_t OCConvertRepMap(CborEncoder *map, const OCRepPayload *payload)
{
    int64_t err = CborNoError;
    CborEncoder repMap;
    err |= cbor_encoder_create_map(map, &repMap, CborIndefiniteLength);
    VERIFY_CBOR_SUCCESS(TAG, err, "Failed creating rep map");
    err |= OCConvertSingleRepPayload(&repMap, payload);
    VERIFY_CBOR_SUCCESS(TAG, err, "Failed converting single rep payload");
    err |= cbor_encoder_close_container(map, &repMap);
    VERIFY_CBOR_SUCCESS(TAG, err, "Failed closing rep map");
exit:
    return err;
}
Example #12
0
bool cnc_daemon_finish_encode_response_and_send(uint8_t * data, CborEncoder *encoder, CborEncoder * container)
{
    if (data == NULL)
    {
        KLOG_ERR(&log_handle, LOG_COMPONENT_NAME, "%s called with a NULL pointer\n", __func__);
        return false;
    }

    cbor_encoder_close_container(encoder, container);
    size_t data_len = cbor_encoder_get_buffer_size(encoder, data);

    KLOG_INFO(&log_handle, LOG_COMPONENT_NAME, "Encoded response buffer size: %lu\n", data_len);

    return cnc_daemon_send_buffer(data, data_len);
}
Example #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;
}
Example #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;
}
static int64_t OCTagsPayloadToCbor(OCTagsPayload *tags, CborEncoder *setMap)
{
    CborEncoder tagsMap;
    int64_t cborEncoderResult = CborNoError;
    cborEncoderResult |= cbor_encoder_create_map(setMap, &tagsMap, CborIndefiniteLength);
    VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to create tags map");

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

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

    cborEncoderResult |= ConditionalAddTextStringToMap(&tagsMap, OC_RSRVD_RTS, tags->rts);
    VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_RTS in tags map");

    cborEncoderResult |= ConditionalAddTextStringToMap(&tagsMap, OC_RSRVD_DREL, tags->drel);
    VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_DREL in tags map");

    cborEncoderResult |= ConditionalAddTextStringToMap(&tagsMap, OC_RSRVD_BASE_URI, tags->baseURI);
    VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_BASE_URI in tags map");

    {
        uint64_t value = tags->bitmap;
        cborEncoderResult |= ConditionalAddIntToMap(&tagsMap, OC_RSRVD_BITMAP, &value);
        VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_BITMAP in tags map");

        value = tags->port;
        cborEncoderResult |= ConditionalAddIntToMap(&tagsMap, OC_RSRVD_HOSTING_PORT, &value);
        VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_HOSTING_PORT in tags map");

        value = tags->ins;
        cborEncoderResult |= ConditionalAddIntToMap(&tagsMap, OC_RSRVD_INS, &value);
        VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_INS in tags map");

        value = tags->ttl;
        cborEncoderResult |= ConditionalAddIntToMap(&tagsMap, OC_RSRVD_TTL, &value);
        VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_TTL in tags map");
    }

    cborEncoderResult |= cbor_encoder_close_container(setMap, &tagsMap);
    VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to close container");

exit:
    return cborEncoderResult;
}
Example #16
0
static int64_t OCConvertPresencePayload(OCPresencePayload *payload, uint8_t *outPayload,
        size_t *size)
{
    int64_t err = CborNoError;
    CborEncoder encoder;

    cbor_encoder_init(&encoder, outPayload, *size, 0);
    CborEncoder map;
    err |= cbor_encoder_create_map(&encoder, &map, CborIndefiniteLength);
    VERIFY_CBOR_SUCCESS(TAG, err, "Failed creating presence map");

    // Sequence Number
    err |= cbor_encode_text_string(&map, OC_RSRVD_NONCE, sizeof(OC_RSRVD_NONCE) - 1);
    VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding nonce tag to presence map");
    err |= cbor_encode_uint(&map, payload->sequenceNumber);
    VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding nonce value to presence map");

    // Max Age
    err |= cbor_encode_text_string(&map, OC_RSRVD_TTL, sizeof(OC_RSRVD_TTL) - 1);
    VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding ttl tag to presence map");
    err |= cbor_encode_uint(&map, payload->maxAge);
    VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding ttl value to presence map");

    // Trigger
    err |= cbor_encode_text_string(&map, OC_RSRVD_TRIGGER, sizeof(OC_RSRVD_TRIGGER) - 1);
    VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding trigger tag to presence map");
    err |= cbor_encode_simple_value(&map, payload->trigger);
    VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding trigger value to presence map");

    // Resource type name
    if (payload->trigger != OC_PRESENCE_TRIGGER_DELETE)
    {
        err |= ConditionalAddTextStringToMap(&map, OC_RSRVD_RESOURCE_TYPE,
                sizeof(OC_RSRVD_RESOURCE_TYPE) - 1, payload->resourceType);
        VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding resource type to presence map");
    }

    // Close Map
    err |= cbor_encoder_close_container(&encoder, &map);
    VERIFY_CBOR_SUCCESS(TAG, err, "Failed closing presence map");

exit:
    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;
}
Example #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);
}
/**
 *
 * Closes the CBOR container (array or map) provided by \a containerEncoder and
 * updates the CBOR stream provided by \a encoder. Both parameters must be the
 * same as were passed to cbor_encoder_create_array() or
 * cbor_encoder_create_map().
 *
 * Unlike cbor_encoder_close_container(), this function checks that the number
 * of items (or pair of items, in the case of a map) was correct. If the number
 * of items inserted does not match the length originally passed to
 * cbor_encoder_create_array() or cbor_encoder_create_map(), this function
 * returns either CborErrorTooFewItems or CborErrorTooManyItems.
 *
 * \sa cbor_encoder_create_array(), cbor_encoder_create_map()
 */
CborError cbor_encoder_close_container_checked(CborEncoder *encoder, const CborEncoder *containerEncoder)
{
    const uint8_t *ptr = encoder->data.ptr;
    CborError err = cbor_encoder_close_container(encoder, containerEncoder);
    if (containerEncoder->flags & CborIteratorFlag_UnknownLength || encoder->end == NULL)
        return err;

    /* check what the original length was */
    uint64_t actually_added;
    err = extract_number(&ptr, encoder->data.ptr, &actually_added);
    if (err)
        return err;

    if (containerEncoder->flags & CborIteratorFlag_ContainerIsMap) {
        if (actually_added > SIZE_MAX / 2)
            return CborErrorDataTooLarge;
        actually_added *= 2;
    }
    return actually_added == containerEncoder->added ? CborNoError :
           actually_added < containerEncoder->added ? CborErrorTooManyItems : CborErrorTooFewItems;
}
Example #20
0
/**
 * Log encode function
 * @param log structure, the encoder, json_value,
 *        timestamp, index
 * @return 0 on success; non-zero on failure
 */
static int
log_encode(struct log *log, CborEncoder *cb,
            int64_t ts, uint32_t index)
{
    int rc;
    CborEncoder logs;
    CborError g_err = CborNoError;

    g_err |= cbor_encoder_create_map(cb, &logs, CborIndefiniteLength);
    g_err |= cbor_encode_text_stringz(&logs, "name");
    g_err |= cbor_encode_text_stringz(&logs, log->l_name);

    g_err |= cbor_encode_text_stringz(&logs, "type");
    g_err |= cbor_encode_uint(&logs, log->l_log->log_type);

    rc = log_encode_entries(log, &logs, ts, index);
    g_err |= cbor_encoder_close_container(cb, &logs);
    if (g_err) {
        return MGMT_ERR_ENOMEM;
    }
    return rc;
}
Example #21
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;
}
Example #22
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);
}
Example #23
0
static int64_t OCConvertPlatformPayload(OCPlatformPayload *payload, uint8_t *outPayload,
        size_t *size)
{
    int64_t err = CborNoError;
    CborEncoder encoder;

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

    CborEncoder repMap;
    err |= cbor_encoder_create_map(&encoder, &repMap, CborIndefiniteLength);
    VERIFY_CBOR_SUCCESS(TAG, err, "Failed creating platform map");

    // Platform ID
    err |= ConditionalAddTextStringToMap(&repMap, OC_RSRVD_PLATFORM_ID,
            sizeof(OC_RSRVD_PLATFORM_ID) - 1, payload->info.platformID);
    VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding platform id");

    // MFG Name
    err |= ConditionalAddTextStringToMap(&repMap, OC_RSRVD_MFG_NAME,
        sizeof(OC_RSRVD_MFG_NAME) - 1, payload->info.manufacturerName);
    VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding mfg name");

    // MFG Url
    err |= ConditionalAddTextStringToMap(&repMap, OC_RSRVD_MFG_URL,
        sizeof(OC_RSRVD_MFG_URL) - 1, payload->info.manufacturerUrl);
    VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding mfg url");

    // Model Num
    err |= ConditionalAddTextStringToMap(&repMap, OC_RSRVD_MODEL_NUM,
            sizeof(OC_RSRVD_MODEL_NUM) -1, payload->info.modelNumber);
    VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding model num");

    // Date of Mfg
    err |= ConditionalAddTextStringToMap(&repMap, OC_RSRVD_MFG_DATE,
            sizeof(OC_RSRVD_MFG_DATE) - 1, payload->info.dateOfManufacture);
    VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding mfg date");

    // Platform Version
    err |= ConditionalAddTextStringToMap(&repMap, OC_RSRVD_PLATFORM_VERSION,
            sizeof(OC_RSRVD_PLATFORM_VERSION) - 1, payload->info.platformVersion);
    VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding platform version");

    // OS Version
    err |= ConditionalAddTextStringToMap(&repMap, OC_RSRVD_OS_VERSION,
            sizeof(OC_RSRVD_OS_VERSION) - 1, payload->info.operatingSystemVersion);
    VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding OS version");

    // Hardware Version
    err |= ConditionalAddTextStringToMap(&repMap, OC_RSRVD_HARDWARE_VERSION,
            sizeof(OC_RSRVD_HARDWARE_VERSION) - 1, payload->info.hardwareVersion);
    VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding HW version");

    // Firmware Version
    err |= ConditionalAddTextStringToMap(&repMap, OC_RSRVD_FIRMWARE_VERSION,
            sizeof(OC_RSRVD_FIRMWARE_VERSION) - 1, payload->info.firmwareVersion);
    VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding firmware version");

    // Support URL
    err |= ConditionalAddTextStringToMap(&repMap, OC_RSRVD_SUPPORT_URL,
            sizeof(OC_RSRVD_SUPPORT_URL) - 1, payload->info.supportUrl);
    VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding support url");

    // System Time
    err |= ConditionalAddTextStringToMap(&repMap, OC_RSRVD_SYSTEM_TIME,
            sizeof(OC_RSRVD_SYSTEM_TIME) - 1, payload->info.systemTime);
    VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding system time");

    // Resource type
    if (payload->rt)
    {
        err |= OCStringLLJoin(&repMap, OC_RSRVD_RESOURCE_TYPE, payload->rt);
        VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding resource type.");
    }

    // Resource interfaces
    if (payload->interfaces)
    {
        err |= OCStringLLJoin(&repMap, OC_RSRVD_INTERFACE, payload->interfaces);
        VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding platform interface type.");
    }

    // Close Map
    err |= cbor_encoder_close_container(&encoder, &repMap);
    VERIFY_CBOR_SUCCESS(TAG, err, "Failed closing rep map");

exit:
    return checkError(err, &encoder, outPayload, size);
}
Example #24
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);
}
Example #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;
}
Example #26
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;
}
Example #27
0
OCStackResult OCTagsPayloadToCbor(OCTagsPayload *tags, CborEncoder *setMap)
{
    CborEncoder tagsMap;
    CborError cborEncoderResult = cbor_encoder_create_map(setMap, &tagsMap, CborIndefiniteLength);
    if (CborNoError != cborEncoderResult)
    {
        OC_LOG(ERROR, TAG, "Failed creating TAGS map.");
        return OC_STACK_ERROR;
    }

    if (CborNoError != ConditionalAddTextStringToMap(&tagsMap, OC_RSRVD_DEVICE_NAME,
            sizeof(OC_RSRVD_DEVICE_NAME) - 1, (char *)tags->n.deviceName))
    {
        OC_LOG(ERROR, TAG, "Failed adding OC_RSRVD_DEVICE_NAME in TAGS map.");
        return OC_STACK_ERROR;
    }
    if (CborNoError != ConditionalAddTextStringToMap(&tagsMap, OC_RSRVD_DEVICE_ID,
            sizeof(OC_RSRVD_DEVICE_ID) - 1, (char *)tags->di.id))
    {
        OC_LOG(ERROR, TAG, "Failed adding OC_RSRVD_DEVICE_ID in TAGS map.");
        return OC_STACK_ERROR;
    }
    if (CborNoError != ConditionalAddTextStringToMap(&tagsMap, OC_RSRVD_RTS,
            sizeof(OC_RSRVD_RTS) - 1, (char *)tags->rts))
    {
        OC_LOG(ERROR, TAG, "Failed adding OC_RSRVD_RTS in TAGS map.");
        return OC_STACK_ERROR;
    }
    if (CborNoError != ConditionalAddTextStringToMap(&tagsMap, OC_RSRVD_DREL,
            sizeof(OC_RSRVD_DREL) - 1, (char *)tags->drel))
    {
        OC_LOG(ERROR, TAG, "Failed adding OC_RSRVD_DREL in TAGS map.");
        return OC_STACK_ERROR;
    }
    if (CborNoError != ConditionalAddTextStringToMap(&tagsMap, OC_RSRVD_BASE_URI,
            sizeof(OC_RSRVD_BASE_URI) - 1, (char *)tags->baseURI))
    {
        OC_LOG(ERROR, TAG, "Failed adding OC_RSRVD_BASE_URI in TAGS map.");
        return OC_STACK_ERROR;
    }
    uint64_t temp = (uint64_t)tags->bitmap;
    if (CborNoError != ConditionalAddIntToMap(&tagsMap, OC_RSRVD_BITMAP,
            sizeof(OC_RSRVD_BITMAP) - 1, &temp))
    {
        OC_LOG(ERROR, TAG, "Failed adding OC_RSRVD_BITMAP in TAGS map.");
        return OC_STACK_ERROR;
    }
    temp = (uint64_t)tags->port;
    if (CborNoError != ConditionalAddIntToMap(&tagsMap, OC_RSRVD_HOSTING_PORT,
            sizeof(OC_RSRVD_HOSTING_PORT) - 1, &temp))
    {
        OC_LOG(ERROR, TAG, "Failed adding OC_RSRVD_HOSTING_PORT in TAGS map.");
        return OC_STACK_ERROR;
    }
    temp = (uint64_t)tags->ins;
    if (CborNoError != ConditionalAddIntToMap(&tagsMap, OC_RSRVD_INS,
            sizeof(OC_RSRVD_INS) - 1, &temp))
    {
        OC_LOG(ERROR, TAG, "Failed adding OC_RSRVD_INS in TAGS map.");
        return OC_STACK_ERROR;
    }
    temp = (uint64_t)tags->ttl;
    if (CborNoError != ConditionalAddIntToMap(&tagsMap, OC_RSRVD_TTL,
            sizeof(OC_RSRVD_TTL) - 1, &temp))
    {
        OC_LOG(ERROR, TAG, "Failed adding OC_RSRVD_TTL in TAGS map.");
        return OC_STACK_ERROR;
    }
    cborEncoderResult = cbor_encoder_close_container(setMap, &tagsMap);
    if (CborNoError != cborEncoderResult)
    {
        OC_LOG(ERROR, TAG, "Failed closing TAGS map.");
        return OC_STACK_ERROR;
    }
    return OC_STACK_OK;
}
Example #28
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;
}
Example #29
0
static void ConvertJsonToCBOR(const char *jsonFileName, const char *cborFileName)
{
    char *jsonStr = NULL;
    FILE *fp = NULL;
    FILE *fp1 = NULL;
    uint8_t *aclCbor = NULL;
    uint8_t *pstatCbor = NULL;
    uint8_t *doxmCbor = NULL;
    uint8_t *amaclCbor = NULL;
    uint8_t *svcCbor = NULL;
    uint8_t *credCbor = NULL;
    cJSON *jsonRoot = NULL;
    OCStackResult ret = OC_STACK_ERROR;
    size_t size = GetJSONFileSize(jsonFileName);
    if (0 == size)
    {
        OIC_LOG (ERROR, TAG, "Failed converting to JSON");
        return;
    }

    jsonStr = (char *)OICMalloc(size + 1);
    VERIFY_NON_NULL(TAG, jsonStr, FATAL);

    fp = fopen(jsonFileName, "r");
    if (fp)
    {
        size_t bytesRead = fread(jsonStr, 1, size, fp);
        jsonStr[bytesRead] = '\0';

        OIC_LOG_V(DEBUG, TAG, "Read %zu bytes", bytesRead);
        fclose(fp);
        fp = NULL;
    }
    else
    {
        OIC_LOG (ERROR, TAG, "Unable to open JSON file!!");
        goto exit;
    }

    jsonRoot = cJSON_Parse(jsonStr);

    cJSON *value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_ACL_NAME);
    //printf("ACL json : \n%s\n", cJSON_PrintUnformatted(value));
    size_t aclCborSize = 0;
    if (NULL != value)
    {
        OicSecAcl_t *acl = JSONToAclBin(jsonStr);
        VERIFY_NON_NULL(TAG, acl, FATAL);
        ret = AclToCBORPayload(acl, &aclCbor, &aclCborSize);
        if(OC_STACK_OK != ret)
        {
            OIC_LOG (ERROR, TAG, "Failed converting Acl to Cbor Payload");
            DeleteACLList(acl);
            goto exit;
        }
        printf("ACL Cbor Size: %zd\n", aclCborSize);
        DeleteACLList(acl);
    }

    value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_PSTAT_NAME);
    size_t pstatCborSize = 0;
    if (NULL != value)
    {
        OicSecPstat_t *pstat = JSONToPstatBin(jsonStr);
        VERIFY_NON_NULL(TAG, pstat, FATAL);
        ret = PstatToCBORPayload(pstat, &pstatCbor, &pstatCborSize);
        if(OC_STACK_OK != ret)
        {
            OIC_LOG (ERROR, TAG, "Failed converting Pstat to Cbor Payload");
            DeletePstatBinData(pstat);
            goto exit;
        }
        printf("PSTAT Cbor Size: %zd\n", pstatCborSize);
        DeletePstatBinData(pstat);
    }
    value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_DOXM_NAME);
    size_t doxmCborSize = 0;
    if (NULL != value)
    {
        OicSecDoxm_t *doxm = JSONToDoxmBin(jsonStr);
        VERIFY_NON_NULL(TAG, doxm, FATAL);
        ret = DoxmToCBORPayload(doxm, &doxmCbor, &doxmCborSize);
        if(OC_STACK_OK != ret)
        {
            OIC_LOG (ERROR, TAG, "Failed converting Doxm to Cbor Payload");
            DeleteDoxmBinData(doxm);
            goto exit;
        }
        printf("DOXM Cbor Size: %zd\n", doxmCborSize);
        DeleteDoxmBinData(doxm);
    }
    value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_AMACL_NAME);
    size_t amaclCborSize = 0;
    if (NULL != value)
    {
        OicSecAmacl_t *amacl = JSONToAmaclBin(jsonStr);
        VERIFY_NON_NULL(TAG, amacl, FATAL);
        ret = AmaclToCBORPayload(amacl, &amaclCbor, &amaclCborSize);
        if(OC_STACK_OK != ret)
        {
            OIC_LOG (ERROR, TAG, "Failed converting Amacl to Cbor Payload");
            DeleteAmaclList(amacl);
            goto exit;
        }
        printf("AMACL Cbor Size: %zd\n", amaclCborSize);
        DeleteAmaclList(amacl);
    }
    value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_SVC_NAME);
    size_t svcCborSize = 0;
    if (NULL != value)
    {
        OicSecSvc_t *svc = JSONToSvcBin(jsonStr);
        VERIFY_NON_NULL(TAG, svc, FATAL);
        ret = SVCToCBORPayload(svc, &svcCbor, &svcCborSize);
        if(OC_STACK_OK != ret)
        {
            OIC_LOG (ERROR, TAG, "Failed converting Svc to Cbor Payload");
            DeleteSVCList(svc);
            goto exit;
        }
        printf("SVC Cbor Size: %zd\n", svcCborSize);
        DeleteSVCList(svc);
    }
    value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_CRED_NAME);
    //printf("CRED json : \n%s\n", cJSON_PrintUnformatted(value));
    size_t credCborSize = 0;
    int secureFlag = 0;
    if (NULL != value)
    {
        OicSecCred_t *cred = JSONToCredBin(jsonStr);
        VERIFY_NON_NULL(TAG, cred, FATAL);
        ret = CredToCBORPayload(cred, &credCbor, &credCborSize, secureFlag);
        if(OC_STACK_OK != ret)
        {
            OIC_LOG (ERROR, TAG, "Failed converting Cred to Cbor Payload");
            DeleteCredList(cred);
            goto exit;
        }
        printf("CRED Cbor Size: %zd\n", credCborSize);
        DeleteCredList(cred);
    }

    CborEncoder encoder;
    size_t cborSize = aclCborSize + pstatCborSize + doxmCborSize + svcCborSize + credCborSize + amaclCborSize;

    printf("Total Cbor Size : %zd\n", cborSize);
    cborSize += 255; // buffer margin for adding map and byte string
    uint8_t *outPayload = (uint8_t *)OICCalloc(1, cborSize);
    VERIFY_NON_NULL(TAG, outPayload, ERROR);
    cbor_encoder_init(&encoder, outPayload, cborSize, 0);
    CborEncoder map;
    CborError cborEncoderResult = cbor_encoder_create_map(&encoder, &map, CborIndefiniteLength);
    VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Creating Main Map.");
    if (aclCborSize > 0)
    {
        cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_ACL_NAME, strlen(OIC_JSON_ACL_NAME));
        VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ACL Name.");
        cborEncoderResult = cbor_encode_byte_string(&map, aclCbor, aclCborSize);
        VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ACL Value.");
    }

    if (pstatCborSize > 0)
    {
        cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_PSTAT_NAME, strlen(OIC_JSON_PSTAT_NAME));
        VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PSTAT Name.");
        cborEncoderResult = cbor_encode_byte_string(&map, pstatCbor, pstatCborSize);
        VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PSTAT Value.");
    }
    if (doxmCborSize > 0)
    {
        cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_DOXM_NAME, strlen(OIC_JSON_DOXM_NAME));
        VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding DOXM Name.");
        cborEncoderResult = cbor_encode_byte_string(&map, doxmCbor, doxmCborSize);
        VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding DOXM Value.");
    }
    if (amaclCborSize > 0)
    {
        cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_AMACL_NAME, strlen(OIC_JSON_AMACL_NAME));
        VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding AMACL Name.");
        cborEncoderResult = cbor_encode_byte_string(&map, amaclCbor, amaclCborSize);
        VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding AMACL Value.");
    }
    if (svcCborSize > 0)
    {
        cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_SVC_NAME, strlen(OIC_JSON_SVC_NAME));
        VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding SVC Name.");
        cborEncoderResult = cbor_encode_byte_string(&map, svcCbor, svcCborSize);
        VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding SVC Value.");
    }
    if (credCborSize > 0)
    {
        cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_CRED_NAME, strlen(OIC_JSON_CRED_NAME));
        VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding CRED Name.");
        cborEncoderResult = cbor_encode_byte_string(&map, credCbor, credCborSize);
        VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding CRED Value.");
    }

    cborEncoderResult = cbor_encoder_close_container(&encoder, &map);
    VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing Container.");

    size_t s = encoder.ptr - outPayload;
    OIC_LOG_V(DEBUG, TAG, "Payload size %zu", s);

    fp1 = fopen(cborFileName, "w");
    if (fp1)
    {
        size_t bytesWritten = fwrite(outPayload, 1, s, fp1);
        if (bytesWritten == s)
        {
            OIC_LOG_V(DEBUG, TAG, "Written %zu bytes", bytesWritten);
        }
        else
        {
            OIC_LOG_V(ERROR, TAG, "Failed writing %zu bytes", s);
        }
        fclose(fp1);
        fp1 = NULL;
    }
exit:

    cJSON_Delete(jsonRoot);
    OICFree(aclCbor);
    OICFree(doxmCbor);
    OICFree(pstatCbor);
    OICFree(amaclCbor);
    OICFree(svcCbor);
    OICFree(credCbor);
    OICFree(jsonStr);
    return ;
}
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;
}