Пример #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);
}
Пример #2
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);
}
Пример #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;
}
Пример #4
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);
}
Пример #5
0
static int64_t ConditionalAddTextStringToMap(CborEncoder* map, const char* key, size_t keylen,
        const char* value)
{
    return value ? AddTextStringToMap(map, key, keylen, value) : 0;
}
Пример #6
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);
}
static int64_t ConditionalAddTextStringToMap(CborEncoder* map, const char* key, const char* value)
{
    return value ? AddTextStringToMap(map, key, value) : CborNoError;
}
Пример #8
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;
}
Пример #9
0
bool ConditionalAddTextStringToMap(CborEncoder* map, const char* key, size_t keylen,
        const char* value)
{
    return value ? AddTextStringToMap(map, key, keylen, value) : false;
}
Пример #10
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;
}
Пример #11
0
static OCStackResult OCConvertPlatformPayload(OCPlatformPayload* 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_PLATFORM);
    {
        CborEncoder map;
        err = err || cbor_encoder_create_map(&rootArray, &map, CborIndefiniteLength);

        // uri
        err = err || AddTextStringToMap(&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 || AddTextStringToMap(&repMap, OC_RSRVD_PLATFORM_ID,
                    sizeof(OC_RSRVD_PLATFORM_ID) - 1,
                    payload->info.platformID);

            // MFG Name
            err = err || AddTextStringToMap(&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);

    if(err)
    {
        OC_LOG_V(ERROR, TAG, "Convert Platform 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;
}
Пример #12
0
static OCStackResult OCConvertDevicePayload(OCDevicePayload* 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_DEVICE);

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

        // uri
        err = err || AddTextStringToMap(&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, 4);

            // 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 || AddTextStringToMap(&repMap, OC_RSRVD_DEVICE_NAME,
                    sizeof(OC_RSRVD_DEVICE_NAME) - 1,
                    payload->deviceName);

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

            // Device data Model Version
            err = err || AddTextStringToMap(&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);

    if(err)
    {
        OC_LOG_V(ERROR, TAG, "Convert Device 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;
}
Пример #13
0
static OCStackResult OCConvertDiscoveryPayload(OCDiscoveryPayload* 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;
    size_t resourceCount =  OCDiscoveryPayloadGetResourceCount(payload);

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

    CborEncoder rootArray;
    err = err || cbor_encoder_create_array(&encoder, &rootArray, 1 + resourceCount);
    err = err || cbor_encode_uint(&rootArray, PAYLOAD_TYPE_DISCOVERY);

    for(size_t i = 0; i < resourceCount; ++i)
    {
        CborEncoder map;
        OCResourcePayload* resource = OCDiscoveryPayloadGetResource(payload, i);
        err = err || cbor_encoder_create_map(&rootArray, &map, 3);
        // Uri
        err = err || AddTextStringToMap(&map, OC_RSRVD_HREF,
                sizeof(OC_RSRVD_HREF) - 1,
                resource->uri);

        // Server ID
        err = err || cbor_encode_text_string(&map, OC_RSRVD_SERVER_INSTANCE_ID,
                sizeof(OC_RSRVD_SERVER_INSTANCE_ID) - 1);
        err = err || cbor_encode_byte_string(&map, resource->sid, UUID_SIZE);
        // Prop Tag
        {
            CborEncoder propMap;
            err = err || cbor_encode_text_string(&map, OC_RSRVD_PROPERTY,
                    sizeof(OC_RSRVD_PROPERTY) -1 );
            err = err || cbor_encoder_create_map(&map, &propMap, 3);

            // Resource Type
            {
                CborEncoder rtArray;
                err = err || cbor_encode_text_string(&propMap, OC_RSRVD_RESOURCE_TYPE,
                    sizeof(OC_RSRVD_RESOURCE_TYPE) - 1);
                err = err || cbor_encoder_create_array(&propMap, &rtArray, CborIndefiniteLength);

                OCStringLL* rtPtr = resource->types;
                while(rtPtr)
                {
                    err = err || cbor_encode_text_string(&rtArray, rtPtr->value,
                            strlen(rtPtr->value));
                    rtPtr = rtPtr->next;
                }

                err = err || cbor_encoder_close_container(&propMap, &rtArray);
            }

            // Interface Types
            {
                CborEncoder ifArray;
                err = err || cbor_encode_text_string(&propMap, OC_RSRVD_INTERFACE,
                        sizeof(OC_RSRVD_INTERFACE) - 1);
                err = err || cbor_encoder_create_array(&propMap, &ifArray, CborIndefiniteLength);
                OCStringLL* ifPtr = resource->interfaces;

                while(ifPtr)
                {
                    err = err || cbor_encode_text_string(&ifArray, ifPtr->value,
                        strlen(ifPtr->value));
                    ifPtr= ifPtr->next;
                }

                err = err || cbor_encoder_close_container(&propMap, &ifArray);
            }
            // Policy
            {
                CborEncoder policyMap;
                err = err || cbor_encode_text_string(&propMap, OC_RSRVD_POLICY,
                        sizeof(OC_RSRVD_POLICY) - 1);
                err = err || cbor_encoder_create_map(&propMap, &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(&propMap, &policyMap);
            }
            // Close
            err = err || cbor_encoder_close_container(&map, &propMap);
        }
        // Close Item
        err = err || cbor_encoder_close_container(&rootArray, &map);
    }
    // Close main array
    err = err || cbor_encoder_close_container(&encoder, &rootArray);

    if(err)
    {
        OC_LOG_V(ERROR, TAG, "Convert Discovery 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;
}
Пример #14
0
static int64_t OCConvertDiscoveryPayload(OCDiscoveryPayload* payload, uint8_t* outPayload,
        size_t* size)
{
    CborEncoder encoder = {0};
    int64_t err = 0;
    size_t resourceCount =  OCDiscoveryPayloadGetResourceCount(payload);

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

    CborEncoder rootArray;
    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)
        {
            return OC_STACK_INVALID_PARAM;
        }

        err = err | cbor_encoder_create_map(&rootArray, &map, 3);
        // Uri
        err = err | AddTextStringToMap(&map, OC_RSRVD_HREF,
                sizeof(OC_RSRVD_HREF) - 1,
                resource->uri);

        // Server ID
        err = err | cbor_encode_text_string(&map, OC_RSRVD_SERVER_INSTANCE_ID,
                sizeof(OC_RSRVD_SERVER_INSTANCE_ID) - 1);
        err = err | cbor_encode_byte_string(&map, resource->sid, UUID_SIZE);
        // Prop Tag
        {
            CborEncoder propMap;
            err = err | cbor_encode_text_string(&map, OC_RSRVD_PROPERTY,
                    sizeof(OC_RSRVD_PROPERTY) -1 );
            err = err | cbor_encoder_create_map(&map, &propMap, 3);

            // Resource Type
            if (resource->types)
            {
                char* joinedTypes = OCStringLLJoin(resource->types);
                if (joinedTypes)
                {
                    err = err | cbor_encode_text_string(&propMap, OC_RSRVD_RESOURCE_TYPE,
                            sizeof(OC_RSRVD_RESOURCE_TYPE) - 1);
                    err = err | cbor_encode_text_string(&propMap, 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(&propMap, OC_RSRVD_INTERFACE,
                            sizeof(OC_RSRVD_INTERFACE) - 1);
                    err = err | cbor_encode_text_string(&propMap, joinedInterfaces,
                            strlen(joinedInterfaces));
                    OICFree(joinedInterfaces);
                }
                else
                {
                    return OC_STACK_NO_MEMORY;
                }
            }

            // Policy
            {
                CborEncoder policyMap;
                err = err | cbor_encode_text_string(&propMap, OC_RSRVD_POLICY,
                        sizeof(OC_RSRVD_POLICY) - 1);
                err = err | cbor_encoder_create_map(&propMap, &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(&propMap, &policyMap);
            }
            // Close
            err = err | cbor_encoder_close_container(&map, &propMap);
        }
        // Close Item
        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);
}