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