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; }
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 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; }