static int64_t OCLinksPayloadToCbor(OCLinksPayload *rtPtr, CborEncoder *setMap) { CborEncoder linksArray; int64_t cborEncoderResult = CborNoError; cborEncoderResult |= cbor_encoder_create_array(setMap, &linksArray, CborIndefiniteLength); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to create Links array"); while (rtPtr) { CborEncoder linksMap; cborEncoderResult |= cbor_encoder_create_map(&linksArray, &linksMap, CborIndefiniteLength); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to create Links map"); cborEncoderResult |= ConditionalAddTextStringToMap(&linksMap, OC_RSRVD_HREF, rtPtr->href); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_HREF in Links map"); cborEncoderResult|= ConditionalAddTextStringToMap(&linksMap, OC_RSRVD_REL, rtPtr->rel); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_REL in Links map"); cborEncoderResult |= ConditionalAddTextStringToMap(&linksMap, OC_RSRVD_TITLE, rtPtr->title); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_TITLE in Links map"); cborEncoderResult |= ConditionalAddTextStringToMap(&linksMap, OC_RSRVD_URI, rtPtr->uri); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_URI in Links map"); cborEncoderResult |= AddStringLLToMap(&linksMap, OC_RSRVD_RESOURCE_TYPE, rtPtr->rt); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_RT in Links map"); cborEncoderResult |= AddStringLLToMap(&linksMap, OC_RSRVD_INTERFACE, rtPtr->itf); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_ITF in Links map"); cborEncoderResult |= AddStringLLToMap(&linksMap, OC_RSRVD_MEDIA_TYPE, rtPtr->mt); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_MT in Links map"); { uint64_t value = rtPtr->ins; cborEncoderResult |= ConditionalAddIntToMap(&linksMap, OC_RSRVD_INS, &value); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_INS in Links map"); } cborEncoderResult |= cbor_encoder_close_container(&linksArray, &linksMap); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed closing Links map"); rtPtr = rtPtr->next; } cborEncoderResult |= cbor_encoder_close_container(setMap, &linksArray); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed closing links array"); exit: return cborEncoderResult; }
OCStackResult OCLinksPayloadToCbor(OCLinksPayload *rtPtr, CborEncoder *setMap) { CborEncoder linksArray; CborError cborEncoderResult; cborEncoderResult = cbor_encoder_create_array(setMap, &linksArray, CborIndefiniteLength); if (CborNoError != cborEncoderResult) { OC_LOG(ERROR, TAG, "Failed creating LINKS array."); return OC_STACK_ERROR; } while (rtPtr) { CborEncoder linksMap; cborEncoderResult = cbor_encoder_create_map(&linksArray, &linksMap, CborIndefiniteLength); if (CborNoError != cborEncoderResult) { OC_LOG(ERROR, TAG, "Failed creating LINKS map."); return OC_STACK_ERROR; } if (CborNoError != ConditionalAddTextStringToMap(&linksMap, OC_RSRVD_HREF, sizeof(OC_RSRVD_HREF) - 1, rtPtr->href)) { OC_LOG(ERROR, TAG, "Failed adding OC_RSRVD_HREF in LINKS map."); return OC_STACK_ERROR; } if (CborNoError != ConditionalAddTextStringToMap(&linksMap, OC_RSRVD_REL, sizeof(OC_RSRVD_REL) - 1, rtPtr->rel)) { OC_LOG(ERROR, TAG, "Failed adding OC_RSRVD_REL in LINKS map."); return OC_STACK_ERROR; } if (CborNoError != ConditionalAddTextStringToMap(&linksMap, OC_RSRVD_TITLE, sizeof(OC_RSRVD_TITLE) - 1, rtPtr->title)) { OC_LOG(ERROR, TAG, "Failed adding OC_RSRVD_TITLE in LINKS map."); return OC_STACK_ERROR; } if (CborNoError != ConditionalAddTextStringToMap(&linksMap, OC_RSRVD_URI, sizeof(OC_RSRVD_URI) - 1, rtPtr->uri)) { OC_LOG(ERROR, TAG, "Failed adding OC_RSRVD_URI in LINKS map."); return OC_STACK_ERROR; } if (CborNoError != AddStringLLToMap(&linksMap, OC_RSRVD_RESOURCE_TYPE, sizeof(OC_RSRVD_RESOURCE_TYPE) - 1, rtPtr->rt)) { OC_LOG(ERROR, TAG, "Failed adding OC_RSRVD_RESOURCE_TYPE in LINKS map."); return OC_STACK_ERROR; } if (CborNoError != AddStringLLToMap(&linksMap, OC_RSRVD_INTERFACE, sizeof(OC_RSRVD_INTERFACE) - 1, rtPtr->itf)) { OC_LOG(ERROR, TAG, "Failed adding OC_RSRVD_INTERFACE in LINKS map."); return OC_STACK_ERROR; } if (CborNoError != AddStringLLToMap(&linksMap, OC_RSRVD_MEDIA_TYPE, sizeof(OC_RSRVD_MEDIA_TYPE) - 1, rtPtr->mt)) { OC_LOG(ERROR, TAG, "Failed adding OC_RSRVD_MEDIA_TYPE in LINKS map."); return OC_STACK_ERROR; } uint64_t temp = (uint64_t)rtPtr->ins; if (CborNoError != ConditionalAddIntToMap(&linksMap, OC_RSRVD_INS, sizeof(OC_RSRVD_INS) - 1, &temp)) { OC_LOG(ERROR, TAG, "Failed adding OC_RSRVD_INS in LINKS map."); return OC_STACK_ERROR; } cborEncoderResult = cbor_encoder_close_container(&linksArray, &linksMap); if (CborNoError != cborEncoderResult) { OC_LOG(ERROR, TAG, "Failed closing LINKS map."); return OC_STACK_ERROR; } rtPtr = rtPtr->next; } cborEncoderResult = cbor_encoder_close_container(setMap, &linksArray); if (CborNoError != cborEncoderResult) { OC_LOG(ERROR, TAG, "Failed closing LINKS array."); return OC_STACK_ERROR;; } return OC_STACK_OK; }
static int64_t OCLinksPayloadToCbor(OCLinksPayload *links, CborEncoder *setMap) { CborEncoder linksArray; int64_t cborEncoderResult = CborNoError; cborEncoderResult |= cbor_encode_text_string(setMap, OC_RSRVD_LINKS, sizeof(OC_RSRVD_LINKS) - 1); cborEncoderResult |= cbor_encoder_create_array(setMap, &linksArray, CborIndefiniteLength); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to create Links array"); while (links) { CborEncoder linksMap; cborEncoderResult |= cbor_encoder_create_map(&linksArray, &linksMap, CborIndefiniteLength); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to create links map"); cborEncoderResult |= ConditionalAddTextStringToMap(&linksMap, OC_RSRVD_HREF, links->href); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_HREF in links map"); cborEncoderResult |= ConditionalAddTextStringToMap(&linksMap, OC_RSRVD_REL, links->rel); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_REL in links map"); cborEncoderResult |= AddStringLLToMap(&linksMap, OC_RSRVD_RESOURCE_TYPE, links->rt); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_RT in links map"); cborEncoderResult |= AddStringLLToMap(&linksMap, OC_RSRVD_INTERFACE, links->itf); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_ITF in links map"); // Policy CborEncoder policyMap; cborEncoderResult |= cbor_encode_text_string(&linksMap, OC_RSRVD_POLICY, sizeof(OC_RSRVD_POLICY) - 1); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed adding policy tag to links map"); cborEncoderResult |= cbor_encoder_create_map(&linksMap, &policyMap, CborIndefiniteLength); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed adding policy map to links map"); // Bitmap cborEncoderResult |= cbor_encode_text_string(&policyMap, OC_RSRVD_BITMAP, sizeof(OC_RSRVD_BITMAP) - 1); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed adding bitmap tag to policy map"); cborEncoderResult |= cbor_encode_uint(&policyMap, links->p); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed adding bitmap value to policy map"); cborEncoderResult |= cbor_encoder_close_container(&linksMap, &policyMap); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed closing policy map"); cborEncoderResult |= ConditionalAddTextStringToMap(&linksMap, OC_RSRVD_TITLE, links->title); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_TITLE in links map"); cborEncoderResult |= ConditionalAddTextStringToMap(&linksMap, OC_RSRVD_URI, links->anchor); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_URI in links map"); cborEncoderResult |= ConditionalAddIntToMap(&linksMap, OC_RSRVD_INS, (uint64_t *) &links->ins); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_INS in links map"); cborEncoderResult |= ConditionalAddIntToMap(&linksMap, OC_RSRVD_TTL, &links->ttl); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_TTL in links map"); cborEncoderResult |= AddStringLLToMap(&linksMap, OC_RSRVD_MEDIA_TYPE, links->type); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_MT in links map"); // Finsihed encoding a resource, close the map. cborEncoderResult |= cbor_encoder_close_container(&linksArray, &linksMap); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed closing links map"); links = links->next; } cborEncoderResult |= cbor_encoder_close_container(setMap, &linksArray); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed closing links array"); exit: return cborEncoderResult; }