Example #1
0
OCStackResult OCDiscoveryCollectionPayloadAddResource(OCDiscoveryPayload *payload, OCTagsPayload *tags, OCLinksPayload *links)
{
    OCResourceCollectionPayload* res = OCCopyCollectionResource(tags, links);
    if (res == NULL)
    {
        return OC_STACK_NO_MEMORY;
    }
    if(!payload->collectionResources)
    {
        payload->collectionResources = res;
    }
    else
    {
        OCResourceCollectionPayload *p = payload->collectionResources;
        while(p->next)
        {
            p = p->next;
        }
        p->next = res;
    }
    return OC_STACK_OK;
}
Example #2
0
OCStackResult OCRDCborToPayload(const CborValue *cborPayload, OCPayload **outPayload)
{
    CborValue *rdCBORPayload = (CborValue *)cborPayload;
    CborError cborFindResult;

    OCRDPayload *rdPayload = OCRDPayloadCreate();
    if (!rdPayload)
    {
        goto no_memory;
    }

    if (cbor_value_is_array(rdCBORPayload))
    {
        OCLinksPayload *linksPayload = NULL;
        OCTagsPayload *tagsPayload = NULL;

        while (cbor_value_is_container(rdCBORPayload))
        {
            // enter tags map
            CborValue tags;
            cborFindResult = cbor_value_enter_container(rdCBORPayload, &tags);
            if (cborFindResult != CborNoError)
            {
                goto cbor_error;
            }
            if (OC_STACK_OK != OCTagsCborToPayload(&tags, &tagsPayload))
            {
                OCFreeTagsResource(tagsPayload);
                goto cbor_error;
            }
            OCTagsLog(DEBUG, tagsPayload);
            if (OC_STACK_OK != OCLinksCborToPayload(&tags, &linksPayload))
            {
                OCFreeLinksResource(linksPayload);
                OCFreeTagsResource(tagsPayload);
                goto cbor_error;
            }
            OCLinksLog(DEBUG, linksPayload);
            // Move from tags payload to links array.
            if (CborNoError != cbor_value_advance(rdCBORPayload))
            {
                OC_LOG(DEBUG, TAG, "Failed advancing from tags payload to links.");
                OCFreeLinksResource(linksPayload);
                OCFreeTagsResource(tagsPayload);
                goto cbor_error;
            }
        }
        rdPayload->rdPublish = OCCopyCollectionResource(tagsPayload, linksPayload);
        if (!rdPayload->rdPublish)
        {
            goto cbor_error;
        }
    }
    else if (cbor_value_is_map(rdCBORPayload))
    {
        char *name = NULL;
        if (CborNoError != FindStringInMap(rdCBORPayload, OC_RSRVD_DEVICE_NAME, &name))
        {
            goto cbor_error;
        }
        char *id = NULL;
        if (CborNoError != FindStringInMap(rdCBORPayload, OC_RSRVD_DEVICE_ID, &id))
        {
            goto cbor_error;
        }
        uint64_t biasFactor = 0;
        if (CborNoError != FindIntInMap(rdCBORPayload, OC_RSRVD_RD_DISCOVERY_SEL, &biasFactor))
        {
            goto cbor_error;
        }
        rdPayload->rdDiscovery = OCRDDiscoveryPayloadCreate(name, id, (uint8_t)biasFactor);
        if (!rdPayload->rdDiscovery)
        {
            goto no_memory;
        }
        OICFree(id);
        OICFree(name);
        cborFindResult =  cbor_value_advance(rdCBORPayload);
        if (CborNoError != cborFindResult)
        {
            goto cbor_error;
        }
    }
    OC_LOG_PAYLOAD(DEBUG, (OCPayload *) rdPayload);
    *outPayload = (OCPayload *)rdPayload;
    return OC_STACK_OK;
no_memory:
    OC_LOG(ERROR, TAG, "Failed allocating memory.");
    OCRDPayloadDestroy(rdPayload);
    return OC_STACK_NO_MEMORY;

cbor_error:
    OCRDPayloadDestroy(rdPayload);
    return OC_STACK_ERROR;
}
Example #3
0
OCRDPayload *OCRDPublishPayloadCreate(OCResourceHandle resourceHandles[], uint8_t nHandles,
                                      uint64_t ttl)
{
    OCTagsPayload *tagsPayload = NULL;
    OCLinksPayload *linksPayload = NULL;
    OCStringLL *rt = NULL;
    OCStringLL *itf = NULL;
    OCStringLL *mt = NULL;
    OCResourceProperty p = OC_RES_PROP_NONE;
    uint8_t ins = 0;

    OCRDPayload *rdPayload = OCRDPayloadCreate();
    if (!rdPayload)
    {
        return NULL;
    }

    const unsigned char *id = (const unsigned char *) OCGetServerInstanceIDString();
    tagsPayload = OCCopyTagsResources(NULL, id, ttl);
    if (!tagsPayload)
    {
        goto exit;
    }

    for (uint8_t j = 0; j < nHandles; j++)
    {
        OCResourceHandle handle = resourceHandles[j];
        if (handle)
        {
            rt = NULL;
            itf = NULL;
            mt = NULL;
            ins = 0;
            const char *uri = OCGetResourceUri(handle);
            uint8_t numElement = 0;
            if (OC_STACK_OK == OCGetNumberOfResourceTypes(handle, &numElement))
            {
                OCStackResult res = CreateStringLL(numElement, handle, OCGetResourceTypeName, &rt);
                if (OC_STACK_OK != res || !rt)
                {
                    goto exit;
                }
            }

            if (OC_STACK_OK == OCGetNumberOfResourceInterfaces(handle, &numElement))
            {
                OCStackResult res = CreateStringLL(numElement, handle, OCGetResourceInterfaceName,
                                                   &itf);
                if (OC_STACK_OK != res || !itf)
                {
                    goto exit;
                }
            }

            p = OCGetResourceProperties(handle);
            p = (OCResourceProperty) ((p & OC_DISCOVERABLE) | (p & OC_OBSERVABLE));

            OCStackResult res = OCGetResourceIns(handle, &ins);
            if (OC_STACK_OK != res)
            {
                goto exit;
            }

            mt = (OCStringLL *)OICCalloc(1, sizeof(OCStringLL));
            if (!mt)
            {
                goto exit;
            }
            mt->value = OICStrdup(DEFAULT_MESSAGE_TYPE);
            if (!mt->value)
            {
                goto exit;
            }

            if (!linksPayload)
            {
                linksPayload = OCCopyLinksResources(uri, NULL, rt, itf, p, NULL,
                                                    NULL, ins, ttl, mt);;
                if (!linksPayload)
                {
                    goto exit;
                }
            }
            else
            {
                OCLinksPayload *temp = linksPayload;
                while (temp->next)
                {
                    temp = temp->next;
                }
                temp->next = OCCopyLinksResources(uri, NULL, rt, itf, p, NULL,
                                                  NULL, ins, ttl, mt);
                if (!temp->next)
                {
                    goto exit;
                }
            }
            OCFreeOCStringLL(rt);
            OCFreeOCStringLL(itf);
            OCFreeOCStringLL(mt);
        }
    }

    rdPayload->rdPublish = OCCopyCollectionResource(tagsPayload, linksPayload);
    if (!rdPayload->rdPublish)
    {
        goto exit;
    }

    return rdPayload;

exit:
    if (rt)
    {
        OCFreeOCStringLL(rt);
    }
    if (itf)
    {
        OCFreeOCStringLL(itf);
    }
    if (mt)
    {
        OCFreeOCStringLL(mt);
    }
    if (tagsPayload)
    {
        OCFreeTagsResource(tagsPayload);
    }
    if (linksPayload)
    {
        OCFreeLinksResource(linksPayload);
    }
    OCRDPayloadDestroy(rdPayload);
    return NULL;
}
OCStackResult OCRDCborToPayload(const CborValue *cborPayload, OCPayload **outPayload)
{
    CborValue *rdCBORPayload = (CborValue *)cborPayload;
    OCStackResult ret = OC_STACK_NO_MEMORY;
    CborError cborFindResult;

    OCRDPayload *rdPayload = OCRDPayloadCreate();
    VERIFY_PARAM_NON_NULL(TAG, rdPayload, "Failed allocating rdPayload");

    ret = OC_STACK_MALFORMED_RESPONSE;

    if (cbor_value_is_array(rdCBORPayload))
    {
        OCLinksPayload *linksPayload = NULL;
        OCTagsPayload *tagsPayload = NULL;

        while (cbor_value_is_container(rdCBORPayload))
        {
            // enter tags map
            CborValue tags;
            cborFindResult = cbor_value_enter_container(rdCBORPayload, &tags);
            VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed entering tags container.");

            cborFindResult= OCTagsCborToPayload(&tags, &tagsPayload);
            VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed parsing tags payload.");
            OCTagsLog(DEBUG, tagsPayload);

            cborFindResult = OCLinksCborToPayload(&tags, &linksPayload);
            VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed parsing links payload.");
            OCLinksLog(DEBUG, linksPayload);

            // Move from tags payload to links array.
            cborFindResult = cbor_value_advance(rdCBORPayload);
            VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed advancing rdCborPayload.");
        }
        rdPayload->rdPublish = OCCopyCollectionResource(tagsPayload, linksPayload);
        VERIFY_PARAM_NON_NULL(TAG, rdPayload->rdPublish, "Failed allocating rdPayload->rdPublish");
    }
    else if (cbor_value_is_map(rdCBORPayload))
    {
        rdPayload->rdDiscovery = (OCRDDiscoveryPayload *)OICCalloc(1, sizeof(OCRDDiscoveryPayload));
        VERIFY_PARAM_NON_NULL(TAG, rdPayload->rdDiscovery, "Failed allocating discoveryPayload");

        cborFindResult = FindStringInMap(rdCBORPayload, OC_RSRVD_DEVICE_NAME,
                &rdPayload->rdDiscovery->n.deviceName);
        VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding OC_RSRVD_DEVICE_NAME.");
        char *deviceId = NULL;
        cborFindResult = FindStringInMap(rdCBORPayload, OC_RSRVD_DEVICE_ID, &deviceId);
        if (deviceId)
        {
            memcpy(rdPayload->rdDiscovery->di.id, deviceId, strlen(deviceId));
            OICFree(deviceId);
        }
        VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding OC_RSRVD_DEVICE_ID.");

        {
            uint64_t value = 0;
            cborFindResult = FindIntInMap(rdCBORPayload, OC_RSRVD_RD_DISCOVERY_SEL, &value);
            VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding OC_RSRVD_RD_DISCOVERY_SEL.");
            rdPayload->rdDiscovery->sel = value;
        }

        cborFindResult =  cbor_value_advance(rdCBORPayload);
        VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed advancing rdCBORPayload.");
    }
    OIC_LOG_PAYLOAD(DEBUG, (OCPayload *) rdPayload);
    *outPayload = (OCPayload *)rdPayload;
    return OC_STACK_OK;

exit:
    OCRDPayloadDestroy(rdPayload);
    return ret;
}
Example #5
0
OCStackResult OCRDCheckPublishedResource(const char *interfaceType, const char *resourceType,
        OCResourceCollectionPayload **payload)
{
    // ResourceType and InterfaceType if both are NULL it will return. If either is
    // not null it will continue execution.
    if (!resourceType && !interfaceType)
    {
        OC_LOG(DEBUG, TAG, "Missing resource type and interace type.");
        return OC_STACK_INVALID_PARAM;
    }

    OC_LOG(DEBUG, TAG, "Check Resource in RD");
    if (g_rdStorage && g_rdStorage->publishedResource)
    {
        for (OCRDStorePublishResources *pResource = g_rdStorage;
                pResource; pResource = pResource->next)
        {
            if (pResource->publishedResource->setLinks)
            {
                for (OCLinksPayload *tLinks = pResource->publishedResource->setLinks; tLinks; tLinks = tLinks->next)
                {
                    // If either rt or itf are NULL, it should skip remaining code execution.
                    if (!tLinks->rt || !tLinks->itf)
                    {
                        OC_LOG(DEBUG, TAG, "Either resource type and interface type are missing.");
                        continue;
                    }
                    if (resourceType)
                    {
                        OCStringLL *temp = tLinks->rt;
                        while(temp)
                        {
                            OC_LOG_V(DEBUG, TAG, "Resource Type: %s %s", resourceType, temp->value);
                            if (strcmp(resourceType, temp->value) == 0)
                            {
                                OCTagsPayload *tag = pResource->publishedResource->tags;
                                OCTagsPayload *tags = OCCopyTagsResources(tag->n.deviceName, tag->di.id, tag->baseURI,
                                    tag->bitmap, tag->port, tag->ins, tag->rts, tag->drel, tag->ttl);
                                if (!tags)
                                {
                                    return OC_STACK_NO_MEMORY;
                                }
                                OCLinksPayload *links = OCCopyLinksResources(tLinks->href, tLinks->rt, tLinks->itf,
                                    tLinks->rel, tLinks->obs, tLinks->title, tLinks->uri, tLinks->ins, tLinks->mt);
                                if (!links)
                                {
                                    OCFreeTagsResource(tags);
                                    return OC_STACK_NO_MEMORY;
                                }
                                *payload = OCCopyCollectionResource(tags, links);
                                if (!*payload)
                                {
                                    OCFreeTagsResource(tags);
                                    OCFreeLinksResource(links);
                                    return OC_STACK_NO_MEMORY;
                                }
                                return OC_STACK_OK;
                            }
                            temp = temp->next;
                        }
                    }
                    if (interfaceType)
                    {
                        OCStringLL *temp = tLinks->itf;
                        while (temp)
                        {
                            OC_LOG_V(DEBUG, TAG, "Interface Type: %s %s", interfaceType, temp->value);
                            if (strcmp(interfaceType, temp->value) == 0)
                            {
                                OCTagsPayload *tag = pResource->publishedResource->tags;
                                OCTagsPayload *tags = OCCopyTagsResources(tag->n.deviceName, tag->di.id, tag->baseURI,
                                    tag->bitmap, tag->port, tag->ins, tag->rts, tag->drel, tag->ttl);
                                if (!tags)
                                {
                                    return OC_STACK_NO_MEMORY;
                                }
                                OCLinksPayload *links = OCCopyLinksResources(tLinks->uri, tLinks->rt, tLinks->itf,
                                    tLinks->rel, tLinks->obs, tLinks->title, tLinks->uri, tLinks->ins, tLinks->mt);
                                if (!links)
                                {
                                    OCFreeTagsResource(tags);
                                    return OC_STACK_NO_MEMORY;
                                }
                                *payload = OCCopyCollectionResource(tags, links);
                                if (!*payload)
                                {
                                    OCFreeTagsResource(tags);
                                    OCFreeLinksResource(links);
                                    return OC_STACK_NO_MEMORY;
                                }
                                return OC_STACK_OK;
                            }
                            temp = temp->next;
                        }
                    }
                }
            }
        }
    }
    return OC_STACK_ERROR;
}