void OCRDPayloadDestroy(OCRDPayload *payload) { if (!payload) { return; } if (payload->rdDiscovery) { if (payload->rdDiscovery->n.deviceName) { OICFree(payload->rdDiscovery->n.deviceName); } OICFree(payload->rdDiscovery); } if (payload->rdPublish) { OCResourceCollectionPayload *col = payload->rdPublish; if (col->setLinks) { OCFreeLinksResource(col->setLinks); } if (col->tags) { OCFreeTagsResource(col->tags); } OICFree(col); } OICFree(payload); }
void OCFreeLinksResource(OCLinksPayload *payload) { if (!payload) { return; } OICFree(payload->href); OCFreeOCStringLL(payload->rt); OCFreeOCStringLL(payload->itf); OICFree(payload->rel); OICFree(payload->title); OICFree(payload->uri); OCFreeOCStringLL(payload->mt); OCFreeLinksResource(payload->next); OICFree(payload); }
void OCFreeCollectionResource(OCResourceCollectionPayload *payload) { if (!payload) { return; } if (payload->tags) { OCFreeTagsResource(payload->tags); } if (payload->setLinks) { OCFreeLinksResource(payload->setLinks); } OICFree(payload); }
void OCFreeLinksResource(OCLinksPayload *links) { if (!links) { return; } OICFree(links->href); OICFree(links->rel); OCFreeOCStringLL(links->rt); OCFreeOCStringLL(links->itf); OICFree(links->title); OICFree(links->anchor); OCFreeOCStringLL(links->type); OCFreeLinksResource(links->next); OICFree(links); }
OCLinksPayload* OCCopyLinksResources(const char *href, OCStringLL *rt, OCStringLL *itf, const char *rel, bool obs, const char *title, const char *uri, uint8_t ins, OCStringLL *mt) { OCLinksPayload *links = (OCLinksPayload *)OICCalloc(1, sizeof(OCLinksPayload)); if (!links) { OC_LOG(ERROR, TAG, "Failed allocating memory."); return NULL; } if (href) { links->href = OICStrdup(href); if (!links->href) { goto memory_allocation_failed; } } if (rt) { links->rt = CloneOCStringLL(rt); if (!links->rt) { goto memory_allocation_failed; } } if (itf) { links->itf = CloneOCStringLL(itf); if (!links->itf) { goto memory_allocation_failed; } } if (rel) { links->rel = OICStrdup(rel); if (!links->rel) { goto memory_allocation_failed; } } links->obs = obs; if (title) { links->title = OICStrdup(title); if (!links->title) { goto memory_allocation_failed; } } if (uri) { links->uri = OICStrdup(uri); if (!links->uri) { goto memory_allocation_failed; } } links->ins = ins; if (mt) { links->mt = CloneOCStringLL(mt); if (!links->mt) { goto memory_allocation_failed; } } links->next = NULL; return links; memory_allocation_failed: OC_LOG(ERROR, TAG, "Memory allocation failed."); OCFreeLinksResource(links); return NULL; }
OCStackResult OCLinksCborToPayload(CborValue *linksArray, OCLinksPayload **linksPayload) { CborValue linksMap; CborError cborFindResult = cbor_value_enter_container(linksArray, &linksMap); if (CborNoError != cborFindResult) { OC_LOG(ERROR, TAG, "Failed enter links map"); return OC_STACK_ERROR; } while (cbor_value_is_map(&linksMap)) { OCLinksPayload *setLinks = (OCLinksPayload *)OICCalloc(1, sizeof(OCLinksPayload)); if (!setLinks) { OC_LOG(ERROR, TAG, "Failed allocating memory."); OCFreeLinksResource(*linksPayload); return OC_STACK_NO_MEMORY; } cborFindResult = FindStringInMap(&linksMap, OC_RSRVD_HREF, &setLinks->href); if (CborNoError != cborFindResult) { OCFreeLinksResource(*linksPayload); OCFreeLinksResource(setLinks); return OC_STACK_ERROR; } cborFindResult = FindStringInMap(&linksMap, OC_RSRVD_REL, &setLinks->rel); if (CborNoError != cborFindResult) { OCFreeLinksResource(*linksPayload); OCFreeLinksResource(setLinks); return OC_STACK_ERROR; } cborFindResult = FindStringInMap(&linksMap, OC_RSRVD_TITLE, &setLinks->title); if (CborNoError != cborFindResult) { OCFreeLinksResource(*linksPayload); OCFreeLinksResource(setLinks); return OC_STACK_ERROR; } cborFindResult = FindStringInMap(&linksMap, OC_RSRVD_URI, &setLinks->uri); if (CborNoError != cborFindResult) { OCFreeLinksResource(*linksPayload); OCFreeLinksResource(setLinks); return OC_STACK_ERROR; } cborFindResult = FindStringLLInMap(&linksMap, OC_RSRVD_RESOURCE_TYPE, &setLinks->rt); if (CborNoError != cborFindResult) { OCFreeLinksResource(*linksPayload); OCFreeLinksResource(setLinks); return OC_STACK_ERROR; } cborFindResult = FindStringLLInMap(&linksMap, OC_RSRVD_INTERFACE, &setLinks->itf); if (CborNoError != cborFindResult) { OCFreeLinksResource(*linksPayload); OCFreeLinksResource(setLinks); return OC_STACK_ERROR; } cborFindResult = FindStringLLInMap(&linksMap, OC_RSRVD_MEDIA_TYPE, &setLinks->mt); if (CborNoError != cborFindResult) { OCFreeLinksResource(*linksPayload); OCFreeLinksResource(setLinks); return OC_STACK_ERROR; } uint64_t temp; cborFindResult = FindIntInMap(&linksMap, OC_RSRVD_INS, &temp); if (CborNoError != cborFindResult) { OCFreeLinksResource(*linksPayload); OCFreeLinksResource(setLinks); return OC_STACK_ERROR; } setLinks->ins = (uint8_t) temp; if (!*linksPayload) { *linksPayload = setLinks; } else { OCLinksPayload *temp = *linksPayload; while (temp->next) { temp = temp->next; } temp->next = setLinks; } cborFindResult = cbor_value_advance(&linksMap); if (CborNoError != cborFindResult) { OC_LOG(ERROR, TAG, "Failed advancing links map"); OCFreeLinksResource(*linksPayload); OCFreeLinksResource(setLinks); return OC_STACK_ERROR; } } return OC_STACK_OK; }
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; }
static CborError OCLinksCborToPayload(CborValue *linksArray, OCLinksPayload **linksPayload) { CborValue linksMap; OCLinksPayload *setLinks = NULL; CborError cborFindResult = cbor_value_enter_container(linksArray, &linksMap); VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed entering links map container"); while (cbor_value_is_map(&linksMap)) { setLinks = (OCLinksPayload *)OICCalloc(1, sizeof(OCLinksPayload)); VERIFY_PARAM_NON_NULL(TAG, setLinks, "Failed allocating setLinks"); cborFindResult = FindStringInMap(&linksMap, OC_RSRVD_HREF, &setLinks->href); VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding href value"); cborFindResult = FindStringInMap(&linksMap, OC_RSRVD_REL, &setLinks->rel); VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding rel value"); cborFindResult = FindStringInMap(&linksMap, OC_RSRVD_TITLE, &setLinks->title); VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding title value"); cborFindResult = FindStringInMap(&linksMap, OC_RSRVD_URI, &setLinks->uri); VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding uri value"); cborFindResult = FindStringLLInMap(&linksMap, OC_RSRVD_RESOURCE_TYPE, &setLinks->rt); VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding rt value"); cborFindResult = FindStringLLInMap(&linksMap, OC_RSRVD_INTERFACE, &setLinks->itf); VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding itf value"); cborFindResult = FindStringLLInMap(&linksMap, OC_RSRVD_MEDIA_TYPE, &setLinks->mt); VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding mt value"); { uint64_t value = 0; cborFindResult = FindIntInMap(&linksMap, OC_RSRVD_INS, &value); VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding ins value"); setLinks->ins = value; } if (!*linksPayload) { *linksPayload = setLinks; } else { OCLinksPayload *temp = *linksPayload; while (temp->next) { temp = temp->next; } temp->next = setLinks; } cborFindResult = cbor_value_advance(&linksMap); VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed advancing links map"); } return cborFindResult; exit: OCFreeLinksResource(*linksPayload); OCFreeLinksResource(setLinks); return cborFindResult; }
OCLinksPayload* OCCopyLinksResources(const char *href, const char *rel, OCStringLL *rt, OCStringLL *itf, uint8_t p, const char *title, const char *anchor, uint8_t ins, uint64_t ttl, OCStringLL *mt) { OCLinksPayload *links = (OCLinksPayload *)OICCalloc(1, sizeof(OCLinksPayload)); if (!links) { OIC_LOG(ERROR, TAG, "Failed allocating memory."); return NULL; } if (href) { links->href = OICStrdup(href); if (!links->href) { goto memory_allocation_failed; } } if (rel) { links->rel = OICStrdup(rel); if (!links->rel) { goto memory_allocation_failed; } } if (rt) { links->rt = CloneOCStringLL(rt); if (!links->rt) { goto memory_allocation_failed; } } if (itf) { links->itf = CloneOCStringLL(itf); if (!links->itf) { goto memory_allocation_failed; } } links->p = p; if (title) { links->title = OICStrdup(title); if (!links->title) { goto memory_allocation_failed; } } if (anchor) { links->anchor = OICStrdup(anchor); if (!links->anchor) { goto memory_allocation_failed; } } links->ins = ins; links->ttl = ttl; if (mt) { links->type = CloneOCStringLL(mt); if (!links->type) { goto memory_allocation_failed; } } links->next = NULL; return links; memory_allocation_failed: OIC_LOG(ERROR, TAG, "Memory allocation failed."); OCFreeLinksResource(links); return NULL; }
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; }
static CborError OCLinksCborToPayload(CborValue *links, OCLinksPayload **linksPayload) { OCLinksPayload *setLinks = NULL; CborValue linksMap; CborValue linksArray; CborError cborFindResult = CborErrorOutOfMemory; cborFindResult = cbor_value_map_find_value(links, OC_RSRVD_LINKS, &linksArray); VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding links array"); cborFindResult = cbor_value_enter_container(&linksArray, &linksMap); VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed entering links map container"); while (cbor_value_is_map(&linksMap)) { setLinks = (OCLinksPayload *)OICCalloc(1, sizeof(OCLinksPayload)); VERIFY_PARAM_NON_NULL(TAG, setLinks, "Failed allocating setLinks"); cborFindResult = FindStringInMap(&linksMap, OC_RSRVD_HREF, &setLinks->href); VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding href value"); cborFindResult = FindStringInMap(&linksMap, OC_RSRVD_REL, &setLinks->rel); VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding rel value"); cborFindResult = FindStringLLInMap(&linksMap, OC_RSRVD_RESOURCE_TYPE, &setLinks->rt); VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding rt value"); cborFindResult = FindStringLLInMap(&linksMap, OC_RSRVD_INTERFACE, &setLinks->itf); VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding itf value"); // Policy CborValue policyMap; cborFindResult = cbor_value_map_find_value(&linksMap, OC_RSRVD_POLICY, &policyMap); VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "to find policy tag"); // Bitmap cborFindResult = FindIntInMap(&policyMap, OC_RSRVD_BITMAP, (uint64_t *) &setLinks->p); VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding bitmap value"); cborFindResult = FindStringInMap(&linksMap, OC_RSRVD_TITLE, &setLinks->title); VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding title value"); cborFindResult = FindStringInMap(&linksMap, OC_RSRVD_URI, &setLinks->anchor); VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding uri value"); cborFindResult = FindIntInMap(&linksMap, OC_RSRVD_INS, (uint64_t *) &setLinks->ins); VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding ins value"); cborFindResult = FindIntInMap(&linksMap, OC_RSRVD_TTL, &setLinks->ttl); VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding ttl"); cborFindResult = FindStringLLInMap(&linksMap, OC_RSRVD_MEDIA_TYPE, &setLinks->type); VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding mt value"); if (!*linksPayload) { *linksPayload = setLinks; } else { OCLinksPayload *temp = *linksPayload; while (temp->next) { temp = temp->next; } temp->next = setLinks; } cborFindResult = cbor_value_advance(&linksMap); VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed advancing links map"); } return cborFindResult; exit: OCFreeLinksResource(*linksPayload); OCFreeLinksResource(setLinks); return cborFindResult; }
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; }