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