Beispiel #1
0
void OCDiscoveryCollectionPayloadDestroy(OCDiscoveryPayload* payload)
{
    if(!payload)
    {
        return;
    }

    OCFreeCollectionResource(payload->collectionResources);
    OICFree(payload);
}
Beispiel #2
0
void OCFreeCollectionResource(OCResourceCollectionPayload *payload)
{
    if (!payload)
    {
        return;
    }
    if (payload->tags)
    {
        OCFreeTagsResource(payload->tags);
    }
    if (payload->setLinks)
    {
        OCFreeLinksResource(payload->setLinks);
    }
    OCFreeCollectionResource(payload->next);
    OICFree(payload);
}
OCStackResult OCRDStorePublishedResources(const OCResourceCollectionPayload *payload, const OCDevAddr *address)
{
    OCResourceCollectionPayload *storeResource = (OCResourceCollectionPayload *)OICCalloc(1, sizeof(OCResourceCollectionPayload));
    if (!storeResource)
    {
        OC_LOG(ERROR, TAG, "Failed allocating memory for OCRDStorePublishResources.");
        return OC_STACK_NO_MEMORY;
    }

    OC_LOG_V(DEBUG, TAG, "Storing Resources for %s:%u", address->addr, address->port);

    char rdPubAddr[MAX_ADDR_STR_SIZE];
    snprintf(rdPubAddr, MAX_ADDR_STR_SIZE, "%s:%d", address->addr, address->port);

    OCTagsPayload *tags = payload->tags;
    storeResource->tags = OCCopyTagsResources(tags->n.deviceName, tags->di.id, rdPubAddr,
        tags->bitmap, address->port, tags->ins, tags->rts, tags->drel, tags->ttl);
    if (!storeResource->tags)
    {
        OC_LOG(ERROR, TAG, "Failed allocating memory for tags.");
        OCFreeCollectionResource(storeResource);
        return OC_STACK_NO_MEMORY;
    }

    for (OCLinksPayload *links = payload->setLinks; links; links = links->next)
    {
        if (!storeResource->setLinks)
        {
            storeResource->setLinks = OCCopyLinksResources(links->href, links->rt, links->itf,
                links->rel, links->obs, links->title, links->uri, links->ins, links->mt);
            if (!storeResource->setLinks)
            {
                OC_LOG(ERROR, TAG, "Failed allocating memory for links.");
                OCFreeCollectionResource(storeResource);
                return OC_STACK_NO_MEMORY;
            }
        }
        else
        {
            OCLinksPayload *temp = storeResource->setLinks;
            while (temp->next)
            {
                temp = temp->next;
            }
            temp->next = OCCopyLinksResources(links->href, links->rt, links->itf, links->rel,
                links->obs, links->title, links->uri, links->ins, links->mt);
            if (!temp->next)
            {
                OC_LOG(ERROR, TAG, "Failed allocating memory for links.");
                OCFreeCollectionResource(storeResource);
                return OC_STACK_NO_MEMORY;
            }
        }

    }
    storeResource->next = NULL;
    OCRDStorePublishResources *resources = (OCRDStorePublishResources *)OICCalloc(1, sizeof(OCRDStorePublishResources));
    if (!resources)
    {
        OCFreeCollectionResource(storeResource);
        return OC_STACK_NO_MEMORY;
    }
    resources->publishedResource = storeResource;

    pthread_mutex_lock(&storageMutex);
    if (g_rdStorage)
    {
        OCRDStorePublishResources *temp = g_rdStorage;
        while (temp->next)
        {
            temp = temp->next;
        }
        temp->next = resources;
    }
    else
    {
        g_rdStorage = resources;
    }
    pthread_mutex_unlock(&storageMutex);

    printStoragedResources(g_rdStorage);
    return OC_STACK_OK;
}