Esempio n. 1
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);
}
Esempio n. 2
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;
}
Esempio n. 3
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;
}
Esempio n. 4
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);
}