OCTagsPayload* OCCopyTagsResources(const char *deviceName, const unsigned char *id, uint64_t ttl) { OCTagsPayload *tags = (OCTagsPayload *)OICCalloc(1, sizeof(OCTagsPayload)); if (!tags) { return NULL; } if (deviceName) { tags->n.deviceName = OICStrdup(deviceName); if (!tags->n.deviceName) { goto memory_allocation_failed; } } if (id) { OICStrcpy((char*)tags->di.id, MAX_IDENTITY_SIZE, (char *)id); } tags->ttl = ttl; return tags; memory_allocation_failed: OIC_LOG(ERROR, TAG, "Memory allocation failed."); OCFreeTagsResource(tags); return NULL; }
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); }
static CborError OCTagsCborToPayload(CborValue *tagsMap, OCTagsPayload **tagsPayload) { CborError cborFindResult = CborErrorOutOfMemory; OCTagsPayload *tags = (OCTagsPayload *)OICCalloc(1, sizeof(OCTagsPayload)); VERIFY_PARAM_NON_NULL(TAG, tags, "Failed allocating tags"); cborFindResult = FindStringInMap(tagsMap, OC_RSRVD_DEVICE_NAME, &tags->n.deviceName); VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding deviceName"); { char *deviceId = NULL; cborFindResult = FindStringInMap(tagsMap, OC_RSRVD_DEVICE_ID, &deviceId); if (deviceId) { memcpy(tags->di.id, deviceId, strlen(deviceId)); OICFree(deviceId); } VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding deviceId"); } cborFindResult = FindIntInMap(tagsMap, OC_RSRVD_DEVICE_TTL, &tags->ttl); VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding ttl"); *tagsPayload = tags; return cborFindResult; exit: OCFreeTagsResource(tags); return cborFindResult; }
OCTagsPayload* OCCopyTagsResources(const char *deviceName, const unsigned char *id, const char *baseURI, uint8_t bitmap, uint16_t port, uint8_t ins, const char *rts,const char *drel, uint32_t ttl) { OCTagsPayload *tags = (OCTagsPayload *)OICCalloc(1, sizeof(OCTagsPayload)); if (!tags) { return NULL; } if (deviceName) { tags->n.deviceName = OICStrdup(deviceName); if (!tags->n.deviceName) { goto memory_allocation_failed; } } if (id) { OICStrcpy((char*)tags->di.id, MAX_IDENTITY_SIZE, (char *)id); if (!tags->di.id) { goto memory_allocation_failed; } } if (baseURI) { tags->baseURI = OICStrdup(baseURI); if (!tags->baseURI) { goto memory_allocation_failed; } } tags->bitmap = bitmap; tags->port = port; tags->ins = ins; if (rts) { tags->rts = OICStrdup(rts); if (!tags->rts) { goto memory_allocation_failed; } } if (drel) { tags->drel = OICStrdup(drel); if (!tags->drel) { goto memory_allocation_failed; } } tags->ttl = ttl; return tags; memory_allocation_failed: OC_LOG(ERROR, TAG, "Memory allocation failed."); OCFreeTagsResource(tags); return NULL; }
static CborError OCTagsCborToPayload(CborValue *tagsMap, OCTagsPayload **tagsPayload) { CborError cborFindResult = CborErrorOutOfMemory; OCTagsPayload *tags = (OCTagsPayload *)OICCalloc(1, sizeof(OCTagsPayload)); VERIFY_PARAM_NON_NULL(TAG, tags, "Failed allocating tags"); if (cbor_value_is_map(tagsMap)) { cborFindResult = FindStringInMap(tagsMap, OC_RSRVD_DEVICE_NAME, &tags->n.deviceName); VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding deviceName"); cborFindResult = FindStringInMap(tagsMap, OC_RSRVD_DREL, &tags->drel); VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding drel"); cborFindResult = FindStringInMap(tagsMap, OC_RSRVD_RTS, &tags->rts); VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding rts"); cborFindResult = FindStringInMap(tagsMap, OC_RSRVD_BASE_URI, &tags->baseURI); VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding baseURI"); char *deviceId = NULL; cborFindResult = FindStringInMap(tagsMap, OC_RSRVD_DEVICE_ID, &deviceId); if (deviceId) { memcpy(tags->di.id, deviceId, strlen(deviceId)); OICFree(deviceId); } VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding deviceId"); { uint64_t value = 0; cborFindResult = FindIntInMap(tagsMap, OC_RSRVD_HOSTING_PORT, &value); VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding port"); tags->port = value; value = 0; cborFindResult = FindIntInMap(tagsMap, OC_RSRVD_BITMAP, &value); VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding bitmap"); tags->bitmap = value; value = 0; cborFindResult = FindIntInMap(tagsMap, OC_RSRVD_INS, &value); VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding ins"); tags->ins = value; value = 0; cborFindResult = FindIntInMap(tagsMap, OC_RSRVD_TTL, &value); tags->ttl = value; } VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding ttl"); cborFindResult = cbor_value_advance(tagsMap); VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed advancing bitmap"); } *tagsPayload = tags; return cborFindResult; exit: OCFreeTagsResource(tags); return cborFindResult; }
void OCFreeCollectionResource(OCResourceCollectionPayload *payload) { if (!payload) { return; } if (payload->tags) { OCFreeTagsResource(payload->tags); } if (payload->setLinks) { OCFreeLinksResource(payload->setLinks); } OICFree(payload); }
OCStackResult OCTagsCborToPayload(CborValue *tagsMap, OCTagsPayload **tagsPayload) { OCTagsPayload *tags = (OCTagsPayload *)OICCalloc(1, sizeof(OCTagsPayload)); if (!tags) { return OC_STACK_NO_MEMORY; } if (cbor_value_is_map(tagsMap)) { if (CborNoError != FindStringInMap(tagsMap, OC_RSRVD_DEVICE_NAME, &tags->n.deviceName)) { OCFreeTagsResource(tags); return OC_STACK_ERROR; } if (CborNoError != FindStringInMap(tagsMap, OC_RSRVD_DREL, &tags->drel)) { OCFreeTagsResource(tags); return OC_STACK_ERROR; } if (CborNoError != FindStringInMap(tagsMap, OC_RSRVD_RTS, &tags->rts)) { OCFreeTagsResource(tags); return OC_STACK_ERROR; } if (CborNoError != FindStringInMap(tagsMap, OC_RSRVD_BASE_URI, &tags->baseURI)) { OCFreeTagsResource(tags); return OC_STACK_ERROR; } char *id = NULL; if (CborNoError != FindStringInMap(tagsMap, OC_RSRVD_DEVICE_ID, &id)) { OCFreeTagsResource(tags); return OC_STACK_ERROR; } if (id) { OICStrcpy((char*)tags->di.id, MAX_IDENTITY_SIZE, id); tags->di.id_length = MAX_IDENTITY_SIZE; OICFree(id); } uint64_t temp; if (CborNoError != FindIntInMap(tagsMap, OC_RSRVD_HOSTING_PORT, &temp)) { OCFreeTagsResource(tags); return OC_STACK_ERROR; } tags->port = (uint16_t) temp; if (CborNoError != FindIntInMap(tagsMap, OC_RSRVD_BITMAP, &temp)) { OCFreeTagsResource(tags); return OC_STACK_ERROR; } tags->bitmap = (uint8_t) temp; if (CborNoError != FindIntInMap(tagsMap, OC_RSRVD_INS, &temp)) { OCFreeTagsResource(tags); return OC_STACK_ERROR; } tags->ins = (uint8_t) temp; if (CborNoError != FindIntInMap(tagsMap, OC_RSRVD_TTL, &temp)) { OCFreeTagsResource(tags); return OC_STACK_ERROR; } tags->ttl = (uint32_t) temp; if (CborNoError != cbor_value_advance(tagsMap)) { OCFreeTagsResource(tags); return OC_STACK_ERROR; } } *tagsPayload = tags; 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; }
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; }