static int64_t OCConvertArrayItem(CborEncoder *array, const OCRepPayloadValueArray *valArray, size_t index) { int64_t err = CborNoError; switch (valArray->type) { case OCREP_PROP_NULL: OIC_LOG(ERROR, TAG, "ConvertArray Invalid NULL"); err = CborUnknownError; break; case OCREP_PROP_INT: err |= cbor_encode_int(array, valArray->iArray[index]); break; case OCREP_PROP_DOUBLE: err |= cbor_encode_double(array, valArray->dArray[index]); break; case OCREP_PROP_BOOL: err |= cbor_encode_boolean(array, valArray->bArray[index]); break; case OCREP_PROP_STRING: if (!valArray->strArray[index]) { err |= cbor_encode_null(array); } else { err |= cbor_encode_text_string(array, valArray->strArray[index], strlen(valArray->strArray[index])); } break; case OCREP_PROP_BYTE_STRING: if (!valArray->strArray[index]) { err |= cbor_encode_null(array); } else { err |= cbor_encode_byte_string(array, valArray->ocByteStrArray[index].bytes, valArray->ocByteStrArray[index].len); } break; case OCREP_PROP_OBJECT: if (!valArray->objArray[index]) { err |= cbor_encode_null(array); } else { err |= OCConvertRepMap(array, valArray->objArray[index]); } break; case OCREP_PROP_ARRAY: OIC_LOG(ERROR, TAG, "ConvertArray Invalid child array"); err = CborUnknownError; break; } return err; }
static bool OCConvertArray(CborEncoder* parent, const OCRepPayloadValueArray* valArray) { CborEncoder array; bool err = false; err = err || cbor_encoder_create_array(parent, &array, CborIndefiniteLength); err = err || cbor_encode_uint(&array, valArray->type); for(int i = 0; i < MAX_REP_ARRAY_DEPTH; ++i) { err = err || cbor_encode_uint(&array, valArray->dimensions[i]); } size_t dimTotal = calcDimTotal(valArray->dimensions); for(size_t i = 0; i < dimTotal; ++i) { switch(valArray->type) { case OCREP_PROP_NULL: OC_LOG(ERROR, TAG, PCF("ConvertArray Invalid NULL")); err = CborUnknownError; break; case OCREP_PROP_INT: err = err || cbor_encode_int(&array, valArray->iArray[i]); break; case OCREP_PROP_DOUBLE: err = err || cbor_encode_double(&array, valArray->dArray[i]); break; case OCREP_PROP_BOOL: err = err || cbor_encode_boolean(&array, valArray->bArray[i]); break; case OCREP_PROP_STRING: err = err || cbor_encode_text_string(&array, valArray->strArray[i], strlen(valArray->strArray[i])); break; case OCREP_PROP_OBJECT: err = OCConvertSingleRepPayload(&array, valArray->objArray[i]); break; case OCREP_PROP_ARRAY: OC_LOG(ERROR, TAG, PCF("ConvertArray Invalid child array")); err = CborUnknownError; break; } } err = err || cbor_encoder_close_container(parent, &array); return err; }
static int64_t OCConvertSingleRepPayload(CborEncoder *repMap, const OCRepPayload *payload) { int64_t err = CborNoError; OCRepPayloadValue *value = payload->values; while (value) { err |= cbor_encode_text_string(repMap, value->name, strlen(value->name)); VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding tag name"); switch (value->type) { case OCREP_PROP_NULL: err |= cbor_encode_null(repMap); break; case OCREP_PROP_INT: err |= cbor_encode_int(repMap, value->i); break; case OCREP_PROP_DOUBLE: err |= cbor_encode_double(repMap, value->d); break; case OCREP_PROP_BOOL: err |= cbor_encode_boolean(repMap, value->b); break; case OCREP_PROP_STRING: err |= cbor_encode_text_string(repMap, value->str, strlen(value->str)); break; case OCREP_PROP_BYTE_STRING: err |= cbor_encode_byte_string(repMap, value->ocByteStr.bytes, value->ocByteStr.len); break; case OCREP_PROP_OBJECT: err |= OCConvertRepMap(repMap, value->obj); break; case OCREP_PROP_ARRAY: err |= OCConvertArray(repMap, &value->arr); break; default: OIC_LOG_V(ERROR, TAG, "Invalid Prop type: %d", value->type); break; } VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding single rep value"); value = value->next; } exit: return err; }
static int64_t OCConvertDiscoveryPayload(OCDiscoveryPayload *payload, uint8_t *outPayload, size_t *size) { CborEncoder encoder; int64_t err = CborNoError; cbor_encoder_init(&encoder, outPayload, *size, 0); if (payload->resources) { /* The format for the payload is "modelled" as JSON. [ // rootArray { // rootMap "di" : UUID, // device ID "rt": "oic.wk.res" "n":"MyDevice" "if":"oic.if.ll oic.if.baseline" "di": "0685B960-736F-46F7-BEC0-9E6CBD61ADC1", links :[ // linksArray contains maps of resources { href, rt, if, policy // Resource 1 }, { href, rt, if, policy // Resource 2 }, . . . ] } ] */ // Open the main root array CborEncoder rootArray; size_t resourceCount = OCDiscoveryPayloadGetResourceCount(payload); err |= cbor_encoder_create_array(&encoder, &rootArray, 1); VERIFY_CBOR_SUCCESS(TAG, err, "Failed creating discovery root array"); // Open the root map in the root array CborEncoder rootMap; err |= cbor_encoder_create_map(&rootArray, &rootMap, CborIndefiniteLength); VERIFY_CBOR_SUCCESS(TAG, err, "Failed creating discovery map"); // Insert Name err |= ConditionalAddTextStringToMap(&rootMap, OC_RSRVD_DEVICE_NAME, sizeof(OC_RSRVD_DEVICE_NAME) - 1, payload->name); VERIFY_CBOR_SUCCESS(TAG, err, "Failed setting name"); // Insert Device ID into the root map err |= AddTextStringToMap(&rootMap, OC_RSRVD_DEVICE_ID, sizeof(OC_RSRVD_DEVICE_ID) - 1, payload->sid); VERIFY_CBOR_SUCCESS(TAG, err, "Failed setting device id"); // Insert Resource Type err |= ConditionalAddTextStringToMap(&rootMap, OC_RSRVD_RESOURCE_TYPE, sizeof(OC_RSRVD_RESOURCE_TYPE) - 1, payload->type); VERIFY_CBOR_SUCCESS(TAG, err, "Failed setting RT"); // Insert interfaces if (payload->interface) { err |= OCStringLLJoin(&rootMap, OC_RSRVD_INTERFACE, payload->interface); VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding interface types tag/value"); } // Insert baseURI if present err |= ConditionalAddTextStringToMap(&rootMap, OC_RSRVD_BASE_URI, sizeof(OC_RSRVD_BASE_URI) - 1, payload->baseURI); VERIFY_CBOR_SUCCESS(TAG, err, "Failed setting baseURI"); // Insert Links into the root map. CborEncoder linkArray; err |= cbor_encode_text_string(&rootMap, OC_RSRVD_LINKS, sizeof(OC_RSRVD_LINKS) - 1); VERIFY_CBOR_SUCCESS(TAG, err, "Failed setting links array tag"); err |= cbor_encoder_create_array(&rootMap, &linkArray, resourceCount); VERIFY_CBOR_SUCCESS(TAG, err, "Failed setting links array"); for(size_t i = 0; i < resourceCount; ++i) { CborEncoder linkMap; OCResourcePayload *resource = OCDiscoveryPayloadGetResource(payload, i); VERIFY_PARAM_NON_NULL(TAG, resource, "Failed retrieving resource"); // resource map inside the links array. err |= cbor_encoder_create_map(&linkArray, &linkMap, LINKS_MAP_LEN); VERIFY_CBOR_SUCCESS(TAG, err, "Failed creating links map"); // Below are insertions of the resource properties into the map. // Uri err |= AddTextStringToMap(&linkMap, OC_RSRVD_HREF, sizeof(OC_RSRVD_HREF) - 1, resource->uri); VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding uri to links map"); // Resource Type if (resource->types) { err |= OCStringLLJoin(&linkMap, OC_RSRVD_RESOURCE_TYPE, resource->types); VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding resourceType tag/value to links map"); } // Interface Types if (resource->interfaces) { err |= OCStringLLJoin(&linkMap, OC_RSRVD_INTERFACE, resource->interfaces); VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding interfaces tag/value to links map"); } // Policy CborEncoder policyMap; err |= cbor_encode_text_string(&linkMap, OC_RSRVD_POLICY, sizeof(OC_RSRVD_POLICY) - 1); VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding policy tag to links map"); err |= cbor_encoder_create_map(&linkMap, &policyMap, CborIndefiniteLength); VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding policy map to links map"); // Bitmap err |= cbor_encode_text_string(&policyMap, OC_RSRVD_BITMAP, sizeof(OC_RSRVD_BITMAP) - 1); VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding bitmap tag to policy map"); err |= cbor_encode_uint(&policyMap, resource->bitmap); VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding bitmap value to policy map"); if (resource->secure) { err |= cbor_encode_text_string(&policyMap, OC_RSRVD_SECURE, sizeof(OC_RSRVD_SECURE) - 1); VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding secure tag to policy map"); err |= cbor_encode_boolean(&policyMap, OC_RESOURCE_SECURE); VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding secure value to policy map"); } if ((resource->secure && resource->port != 0) || payload->baseURI) { err |= cbor_encode_text_string(&policyMap, OC_RSRVD_HOSTING_PORT, sizeof(OC_RSRVD_HOSTING_PORT) - 1); VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding secure port tag"); err |= cbor_encode_uint(&policyMap, resource->port); VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding secure port value"); } #ifdef TCP_ADAPTER err |= cbor_encode_text_string(&policyMap, OC_RSRVD_TCP_PORT, sizeof(OC_RSRVD_TCP_PORT) - 1); VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding tcp port tag"); err |= cbor_encode_uint(&policyMap, resource->tcpPort); VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding tcp port value"); #endif err |= cbor_encoder_close_container(&linkMap, &policyMap); VERIFY_CBOR_SUCCESS(TAG, err, "Failed closing policy map"); // Finsihed encoding a resource, close the map. err |= cbor_encoder_close_container(&linkArray, &linkMap); VERIFY_CBOR_SUCCESS(TAG, err, "Failed closing link map"); } // Close links array inside the root map. err |= cbor_encoder_close_container(&rootMap, &linkArray); VERIFY_CBOR_SUCCESS(TAG, err, "Failed closing link array"); // close root map inside the root array. err |= cbor_encoder_close_container(&rootArray, &rootMap); VERIFY_CBOR_SUCCESS(TAG, err, "Failed closing root map"); // Close the final root array. err |= cbor_encoder_close_container(&encoder, &rootArray); VERIFY_CBOR_SUCCESS(TAG, err, "Failed closing root array"); } exit: return checkError(err, &encoder, outPayload, size); }
CborError sol_oic_encode_cbor_repr(struct sol_coap_packet *pkt, const char *href, const struct sol_vector *repr_vec) { CborEncoder encoder, rep_map, array, map; CborError err; uint8_t *payload; uint16_t size; if (!repr_vec) return CborNoError; if (sol_coap_packet_get_payload(pkt, &payload, &size) < 0) { SOL_WRN("Could not get CoAP payload"); return CborUnknownError; } cbor_encoder_init(&encoder, payload, size, 0); err = cbor_encoder_create_array(&encoder, &array, CborIndefiniteLength); err |= cbor_encode_uint(&array, SOL_OIC_PAYLOAD_REPRESENTATION); err |= cbor_encoder_create_map(&array, &map, CborIndefiniteLength); err |= cbor_encode_text_stringz(&map, SOL_OIC_KEY_HREF); err |= cbor_encode_text_stringz(&map, href); err |= cbor_encode_text_stringz(&map, SOL_OIC_KEY_REPRESENTATION); err |= cbor_encoder_create_map(&map, &rep_map, CborIndefiniteLength); if (repr_vec) { struct sol_oic_repr_field *repr; uint16_t idx; SOL_VECTOR_FOREACH_IDX (repr_vec, repr, idx) { if (err != CborNoError) break; err |= cbor_encode_text_stringz(&rep_map, repr->key); switch (repr->type) { case SOL_OIC_REPR_TYPE_UINT: err |= cbor_encode_uint(&rep_map, repr->v_uint); break; case SOL_OIC_REPR_TYPE_INT: err |= cbor_encode_int(&rep_map, repr->v_int); break; case SOL_OIC_REPR_TYPE_SIMPLE: err |= cbor_encode_simple_value(&rep_map, repr->v_simple); break; case SOL_OIC_REPR_TYPE_TEXT_STRING: { const char *p = repr->v_slice.data ? repr->v_slice.data : ""; err |= cbor_encode_text_string(&rep_map, p, repr->v_slice.len); break; } case SOL_OIC_REPR_TYPE_BYTE_STRING: { const uint8_t *empty = (const uint8_t *)""; const uint8_t *p = repr->v_slice.data ? (const uint8_t *)repr->v_slice.data : empty; err |= cbor_encode_byte_string(&rep_map, p, repr->v_slice.len); break; } case SOL_OIC_REPR_TYPE_HALF_FLOAT: err |= cbor_encode_half_float(&rep_map, repr->v_voidptr); break; case SOL_OIC_REPR_TYPE_FLOAT: err |= cbor_encode_float(&rep_map, repr->v_float); break; case SOL_OIC_REPR_TYPE_DOUBLE: err |= cbor_encode_double(&rep_map, repr->v_double); break; case SOL_OIC_REPR_TYPE_BOOLEAN: err |= cbor_encode_boolean(&rep_map, repr->v_boolean); break; default: if (err == CborNoError) err = CborErrorUnknownType; } } } err |= cbor_encoder_close_container(&map, &rep_map); err |= cbor_encoder_close_container(&array, &map); err |= cbor_encoder_close_container(&encoder, &array); if (err == CborNoError) sol_coap_packet_set_payload_used(pkt, encoder.ptr - payload); return err; }
int imgmgr_state_read(struct mgmt_cbuf *cb) { int i; int rc; uint32_t flags; struct image_version ver; uint8_t hash[IMGMGR_HASH_LEN]; /* SHA256 hash */ char vers_str[IMGMGR_NMGR_MAX_VER]; int any_non_bootable; int split_status; uint8_t state_flags; CborError g_err = CborNoError; CborEncoder *penc = &cb->encoder; CborEncoder rsp, images, image; any_non_bootable = 0; g_err |= cbor_encoder_create_map(penc, &rsp, CborIndefiniteLength); g_err |= cbor_encode_text_stringz(&rsp, "images"); g_err |= cbor_encoder_create_array(&rsp, &images, CborIndefiniteLength); for (i = 0; i < 2; i++) { rc = imgr_read_info(i, &ver, hash, &flags); if (rc != 0) { continue; } if (flags & IMAGE_F_NON_BOOTABLE) { any_non_bootable = 1; } state_flags = imgmgr_state_flags(i); g_err |= cbor_encoder_create_map(&images, &image, CborIndefiniteLength); g_err |= cbor_encode_text_stringz(&image, "slot"); g_err |= cbor_encode_int(&image, i); g_err |= cbor_encode_text_stringz(&image, "version"); imgr_ver_str(&ver, vers_str); g_err |= cbor_encode_text_stringz(&image, vers_str); g_err |= cbor_encode_text_stringz(&image, "hash"); g_err |= cbor_encode_byte_string(&image, hash, IMGMGR_HASH_LEN); g_err |= cbor_encode_text_stringz(&image, "bootable"); g_err |= cbor_encode_boolean(&image, !(flags & IMAGE_F_NON_BOOTABLE)); g_err |= cbor_encode_text_stringz(&image, "pending"); g_err |= cbor_encode_boolean(&image, state_flags & IMGMGR_STATE_F_PENDING); g_err |= cbor_encode_text_stringz(&image, "confirmed"); g_err |= cbor_encode_boolean(&image, state_flags & IMGMGR_STATE_F_CONFIRMED); g_err |= cbor_encode_text_stringz(&image, "active"); g_err |= cbor_encode_boolean(&image, state_flags & IMGMGR_STATE_F_ACTIVE); g_err |= cbor_encoder_close_container(&images, &image); } g_err |= cbor_encoder_close_container(&rsp, &images); if (any_non_bootable) { split_status = split_check_status(); } else { split_status = SPLIT_STATUS_INVALID; } g_err |= cbor_encode_text_stringz(&rsp, "splitStatus"); g_err |= cbor_encode_int(&rsp, split_status); g_err |= cbor_encoder_close_container(penc, &rsp); if (g_err) { return MGMT_ERR_ENOMEM; } return 0; }
static int64_t OCConvertSingleRepPayload(CborEncoder* parent, const OCRepPayload* payload) { int64_t err = 0; CborEncoder map; err = err | cbor_encoder_create_map(parent, &map, CborIndefiniteLength); // Uri err = err | ConditionalAddTextStringToMap(&map, OC_RSRVD_HREF, sizeof(OC_RSRVD_HREF) - 1, payload->uri); // Prop Map // resource types, interfaces if(payload->types || payload->interfaces) { OC_LOG(INFO, TAG, "Payload has types or interfaces"); err = err | cbor_encode_text_string(&map, OC_RSRVD_PROPERTY, sizeof(OC_RSRVD_PROPERTY) - 1); CborEncoder propMap; err = err | cbor_encoder_create_map(&map, &propMap, CborIndefiniteLength); if (payload->types) { char* joinedTypes = OCStringLLJoin(payload->types); if (joinedTypes) { err = err | cbor_encode_text_string(&propMap, OC_RSRVD_RESOURCE_TYPE, sizeof(OC_RSRVD_RESOURCE_TYPE) - 1); err = err | cbor_encode_text_string(&propMap, joinedTypes, strlen(joinedTypes)); OICFree(joinedTypes); } else { return OC_STACK_NO_MEMORY; } } if (payload->interfaces) { char* joinedInterfaces = OCStringLLJoin(payload->interfaces); if (joinedInterfaces) { err = err | cbor_encode_text_string(&propMap, OC_RSRVD_INTERFACE, sizeof(OC_RSRVD_INTERFACE) - 1); err = err | cbor_encode_text_string(&propMap, joinedInterfaces, strlen(joinedInterfaces)); OICFree(joinedInterfaces); } else { return OC_STACK_NO_MEMORY; } } err = err | cbor_encoder_close_container(&map, &propMap); } // Rep Map { CborEncoder repMap; err = err | cbor_encode_text_string(&map, OC_RSRVD_REPRESENTATION, sizeof(OC_RSRVD_REPRESENTATION) - 1); err = err | cbor_encoder_create_map(&map, &repMap, CborIndefiniteLength); OCRepPayloadValue* value = payload->values; while(value) { err = err | cbor_encode_text_string(&repMap, value->name, strlen(value->name)); switch(value->type) { case OCREP_PROP_NULL: err = err | cbor_encode_null(&repMap); break; case OCREP_PROP_INT: err = err | cbor_encode_int(&repMap, value->i); break; case OCREP_PROP_DOUBLE: err = err | cbor_encode_double(&repMap, value->d); break; case OCREP_PROP_BOOL: err = err | cbor_encode_boolean(&repMap, value->b); break; case OCREP_PROP_STRING: err = err | cbor_encode_text_string(&repMap, value->str, strlen(value->str)); break; case OCREP_PROP_BYTE_STRING: err = err | cbor_encode_byte_string(&repMap, value->ocByteStr.bytes, value->ocByteStr.len); break; case OCREP_PROP_OBJECT: err = err | OCConvertSingleRepPayload(&repMap, value->obj); break; case OCREP_PROP_ARRAY: err = err | OCConvertArray(&repMap, &value->arr); break; default: OC_LOG_V(ERROR, TAG, "Invalid Prop type: %d", value->type); break; } value = value->next; } err = err | cbor_encoder_close_container(&map, &repMap); } // Close Map err = err | cbor_encoder_close_container(parent, &map); return err; }
static int64_t OCConvertDiscoveryPayload(OCDiscoveryPayload* payload, uint8_t* outPayload, size_t* size) { CborEncoder encoder = {0}; CborEncoder rootArray = {0}; int64_t err = 0; cbor_encoder_init(&encoder, outPayload, *size, 0); if (payload->collectionResources) { CborError cborEncoderResult; cborEncoderResult = cbor_encoder_create_array(&encoder, &rootArray, DISCOVERY_CBOR_ARRAY_LEN); if (CborNoError != cborEncoderResult) { OC_LOG(ERROR, TAG, "Failed creating root array."); goto cbor_error; } CborEncoder colArray; cborEncoderResult = cbor_encoder_create_array(&rootArray, &colArray, CborIndefiniteLength); if (CborNoError != cborEncoderResult) { OC_LOG(ERROR, TAG, "Failed creating collection array."); goto cbor_error; } OCResourceCollectionPayload *colResources = payload->collectionResources; while (colResources) { if (OC_STACK_OK != OCTagsPayloadToCbor(colResources->tags, &colArray)) { goto cbor_error; } if (OC_STACK_OK != OCLinksPayloadToCbor(colResources->setLinks, &colArray)) { goto cbor_error; } colResources = colResources->next; } cborEncoderResult = cbor_encoder_close_container(&rootArray, &colArray); if (CborNoError != cborEncoderResult) { OC_LOG(ERROR, TAG, "Failed closing collection array."); goto cbor_error; } cborEncoderResult = cbor_encoder_close_container(&encoder, &rootArray); if (CborNoError != cborEncoderResult) { OC_LOG(ERROR, TAG, "Failed closing root array."); goto cbor_error; } } else if (payload->resources) { size_t resourceCount = OCDiscoveryPayloadGetResourceCount(payload); err = err | cbor_encoder_create_array(&encoder, &rootArray, resourceCount); for(size_t i = 0; i < resourceCount; ++i) { CborEncoder map; OCResourcePayload* resource = OCDiscoveryPayloadGetResource(payload, i); if(!resource) { OICFree(outPayload); return OC_STACK_INVALID_PARAM; } err = err | cbor_encoder_create_map(&rootArray, &map, DISCOVERY_CBOR_RES_MAP_LEN); // Device ID err = err | cbor_encode_text_string(&map, OC_RSRVD_DEVICE_ID, sizeof(OC_RSRVD_DEVICE_ID) - 1); err = err | cbor_encode_byte_string(&map, resource->sid, UUID_SIZE); { CborEncoder linkArray; err = err | cbor_encode_text_string(&map, OC_RSRVD_LINKS, sizeof(OC_RSRVD_LINKS) -1); err = err | cbor_encoder_create_array(&map, &linkArray, CborIndefiniteLength); // Link Map { CborEncoder linkMap; err = err | cbor_encoder_create_map(&linkArray, &linkMap, DISCOVERY_CBOR_LINKS_MAP_LEN); // Uri err = err | AddTextStringToMap(&linkMap, OC_RSRVD_HREF, sizeof(OC_RSRVD_HREF) - 1, resource->uri); // Resource Type if (resource->types) { char* joinedTypes = OCStringLLJoin(resource->types); if (joinedTypes) { err = err | cbor_encode_text_string(&linkMap, OC_RSRVD_RESOURCE_TYPE, sizeof(OC_RSRVD_RESOURCE_TYPE) - 1); err = err | cbor_encode_text_string(&linkMap, joinedTypes, strlen(joinedTypes)); OICFree(joinedTypes); } else { return OC_STACK_NO_MEMORY; } } // Interface Types if (resource->interfaces) { char* joinedInterfaces = OCStringLLJoin(resource->interfaces); if (joinedInterfaces) { err = err | cbor_encode_text_string(&linkMap, OC_RSRVD_INTERFACE, sizeof(OC_RSRVD_INTERFACE) - 1); err = err | cbor_encode_text_string(&linkMap, joinedInterfaces, strlen(joinedInterfaces)); OICFree(joinedInterfaces); } else { return OC_STACK_NO_MEMORY; } } // Policy { CborEncoder policyMap; err = err | cbor_encode_text_string(&linkMap, OC_RSRVD_POLICY, sizeof(OC_RSRVD_POLICY) - 1); err = err | cbor_encoder_create_map(&linkMap, &policyMap, CborIndefiniteLength); // Bitmap err = err | cbor_encode_text_string(&policyMap, OC_RSRVD_BITMAP, sizeof(OC_RSRVD_BITMAP) - 1); err = err | cbor_encode_uint(&policyMap, resource->bitmap); if(resource->secure) { err = err | cbor_encode_text_string(&policyMap, OC_RSRVD_SECURE, sizeof(OC_RSRVD_SECURE) - 1); err = err | cbor_encode_boolean(&policyMap, OC_RESOURCE_SECURE); if(resource->port != 0) { err = err | cbor_encode_text_string(&policyMap, OC_RSRVD_HOSTING_PORT, sizeof(OC_RSRVD_HOSTING_PORT) - 1); err = err | cbor_encode_uint(&policyMap, resource->port); } } err = err | cbor_encoder_close_container(&linkMap, &policyMap); } // Close err = err | cbor_encoder_close_container(&linkArray, &linkMap); } err = err | cbor_encoder_close_container(&map, &linkArray); } err = err | cbor_encoder_close_container(&rootArray, &map); } // Close main array err = err | cbor_encoder_close_container(&encoder, &rootArray); } return checkError(err, &encoder, outPayload, size); cbor_error: OICFree(outPayload); return OC_STACK_ERROR; }
static bool OCConvertSingleRepPayload(CborEncoder* parent, const OCRepPayload* payload) { bool err = false; CborEncoder map; err = err || cbor_encoder_create_map(parent, &map, CborIndefiniteLength); // Uri err = err || ConditionalAddTextStringToMap(&map, OC_RSRVD_HREF, sizeof(OC_RSRVD_HREF) - 1, payload->uri); // Prop Map // resource types, interfaces if(payload->types || payload->interfaces) { OC_LOG_V(INFO, TAG, "Payload has types or interfaces"); err = err || cbor_encode_text_string(&map, OC_RSRVD_PROPERTY, sizeof(OC_RSRVD_PROPERTY) - 1); CborEncoder propMap; err = err || cbor_encoder_create_map(&map, &propMap, 2); CborEncoder curArray; if(payload->types) { err = err || cbor_encode_text_string(&propMap, OC_RSRVD_RESOURCE_TYPE, sizeof(OC_RSRVD_RESOURCE_TYPE) - 1); err = err || cbor_encoder_create_array(&propMap, &curArray, CborIndefiniteLength); OCStringLL* val = payload->types; while(val) { err = err || cbor_encode_text_string(&curArray, val->value, strlen(val->value)); val = val->next; } err = err || cbor_encoder_close_container(&propMap, &curArray); } if(payload->interfaces) { err = err || cbor_encode_text_string(&propMap, OC_RSRVD_INTERFACE, sizeof(OC_RSRVD_INTERFACE) - 1); err = err || cbor_encoder_create_array(&propMap, &curArray, CborIndefiniteLength); OCStringLL* val = payload->interfaces; while(val) { err = err || cbor_encode_text_string(&curArray, val->value, strlen(val->value)); val = val->next; } err = err || cbor_encoder_close_container(&propMap, &curArray); } err = err || cbor_encoder_close_container(&map, &propMap); } // Rep Map { CborEncoder repMap; err = err || cbor_encode_text_string(&map, OC_RSRVD_REPRESENTATION, sizeof(OC_RSRVD_REPRESENTATION) - 1); err = err || cbor_encoder_create_map(&map, &repMap, CborIndefiniteLength); OCRepPayloadValue* value = payload->values; while(value) { err = err || cbor_encode_text_string(&repMap, value->name, strlen(value->name)); switch(value->type) { case OCREP_PROP_NULL: err = err || cbor_encode_null(&repMap); break; case OCREP_PROP_INT: err = err || cbor_encode_int(&repMap, value->i); break; case OCREP_PROP_DOUBLE: err = err || cbor_encode_double(&repMap, value->d); break; case OCREP_PROP_BOOL: err = err || cbor_encode_boolean(&repMap, value->b); break; case OCREP_PROP_STRING: err = err || cbor_encode_text_string(&repMap, value->str, strlen(value->str)); break; case OCREP_PROP_OBJECT: err = err || OCConvertSingleRepPayload(&repMap, value->obj); break; case OCREP_PROP_ARRAY: err = err || OCConvertArray(&repMap, &value->arr); break; default: OC_LOG_V(ERROR, TAG, "Invalid Prop type: %d", value->type); break; } value = value->next; } err = err || cbor_encoder_close_container(&map, &repMap); } // Close Map err = err || cbor_encoder_close_container(parent, &map); return err; }
static OCStackResult OCConvertDiscoveryPayload(OCDiscoveryPayload* payload, uint8_t** outPayload, size_t* size) { *outPayload = (uint8_t*)OICCalloc(1, MAX_REQUEST_LENGTH); *size = MAX_REQUEST_LENGTH; if(!*outPayload) { return OC_STACK_NO_MEMORY; } CborEncoder encoder = {}; bool err = false; size_t resourceCount = OCDiscoveryPayloadGetResourceCount(payload); cbor_encoder_init(&encoder, *outPayload, *size, 0); CborEncoder rootArray; err = err || cbor_encoder_create_array(&encoder, &rootArray, 1 + resourceCount); err = err || cbor_encode_uint(&rootArray, PAYLOAD_TYPE_DISCOVERY); for(size_t i = 0; i < resourceCount; ++i) { CborEncoder map; OCResourcePayload* resource = OCDiscoveryPayloadGetResource(payload, i); err = err || cbor_encoder_create_map(&rootArray, &map, 3); // Uri err = err || AddTextStringToMap(&map, OC_RSRVD_HREF, sizeof(OC_RSRVD_HREF) - 1, resource->uri); // Server ID err = err || cbor_encode_text_string(&map, OC_RSRVD_SERVER_INSTANCE_ID, sizeof(OC_RSRVD_SERVER_INSTANCE_ID) - 1); err = err || cbor_encode_byte_string(&map, resource->sid, UUID_SIZE); // Prop Tag { CborEncoder propMap; err = err || cbor_encode_text_string(&map, OC_RSRVD_PROPERTY, sizeof(OC_RSRVD_PROPERTY) -1 ); err = err || cbor_encoder_create_map(&map, &propMap, 3); // Resource Type { CborEncoder rtArray; err = err || cbor_encode_text_string(&propMap, OC_RSRVD_RESOURCE_TYPE, sizeof(OC_RSRVD_RESOURCE_TYPE) - 1); err = err || cbor_encoder_create_array(&propMap, &rtArray, CborIndefiniteLength); OCStringLL* rtPtr = resource->types; while(rtPtr) { err = err || cbor_encode_text_string(&rtArray, rtPtr->value, strlen(rtPtr->value)); rtPtr = rtPtr->next; } err = err || cbor_encoder_close_container(&propMap, &rtArray); } // Interface Types { CborEncoder ifArray; err = err || cbor_encode_text_string(&propMap, OC_RSRVD_INTERFACE, sizeof(OC_RSRVD_INTERFACE) - 1); err = err || cbor_encoder_create_array(&propMap, &ifArray, CborIndefiniteLength); OCStringLL* ifPtr = resource->interfaces; while(ifPtr) { err = err || cbor_encode_text_string(&ifArray, ifPtr->value, strlen(ifPtr->value)); ifPtr= ifPtr->next; } err = err || cbor_encoder_close_container(&propMap, &ifArray); } // Policy { CborEncoder policyMap; err = err || cbor_encode_text_string(&propMap, OC_RSRVD_POLICY, sizeof(OC_RSRVD_POLICY) - 1); err = err || cbor_encoder_create_map(&propMap, &policyMap, CborIndefiniteLength); // Bitmap err = err || cbor_encode_text_string(&policyMap, OC_RSRVD_BITMAP, sizeof(OC_RSRVD_BITMAP) - 1); err = err || cbor_encode_uint(&policyMap, resource->bitmap); if(resource->secure) { err = err || cbor_encode_text_string(&policyMap, OC_RSRVD_SECURE, sizeof(OC_RSRVD_SECURE) - 1); err = err || cbor_encode_boolean(&policyMap, OC_RESOURCE_SECURE); if(resource->port != 0) { err = err || cbor_encode_text_string(&policyMap, OC_RSRVD_HOSTING_PORT, sizeof(OC_RSRVD_HOSTING_PORT) - 1); err = err || cbor_encode_uint(&policyMap, resource->port); } } err = err || cbor_encoder_close_container(&propMap, &policyMap); } // Close err = err || cbor_encoder_close_container(&map, &propMap); } // Close Item err = err || cbor_encoder_close_container(&rootArray, &map); } // Close main array err = err || cbor_encoder_close_container(&encoder, &rootArray); if(err) { OC_LOG_V(ERROR, TAG, "Convert Discovery Payload failed with : %d", err); return OC_STACK_ERROR; } *size = encoder.ptr - *outPayload; uint8_t* tempPayload = (uint8_t*)OICRealloc(*outPayload, *size); if(!tempPayload) { OC_LOG_V(ERROR, TAG, PCF("Payload realloc failed!")); OICFree(*outPayload); return OC_STACK_ERROR; } *outPayload = tempPayload; return OC_STACK_OK; }
OCStackResult PstatToCBORPayload(const OicSecPstat_t *pstat, uint8_t **payload, size_t *size) { if (NULL == pstat || NULL == payload || NULL != *payload || NULL == size) { return OC_STACK_INVALID_PARAM; } size_t cborLen = *size; if (0 == cborLen) { cborLen = CBOR_SIZE; } *payload = NULL; *size = 0; OCStackResult ret = OC_STACK_ERROR; CborEncoder encoder; CborEncoder pstatMap; char* strUuid = NULL; int64_t cborEncoderResult = CborNoError; uint8_t *outPayload = (uint8_t *)OICCalloc(1, cborLen); VERIFY_NON_NULL(TAG, outPayload, ERROR); cbor_encoder_init(&encoder, outPayload, cborLen, 0); cborEncoderResult = cbor_encoder_create_map(&encoder, &pstatMap, PSTAT_MAP_SIZE); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Pstat Map."); cborEncoderResult = cbor_encode_text_string(&pstatMap, OIC_JSON_ISOP_NAME, strlen(OIC_JSON_ISOP_NAME)); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ISOP Name Tag."); cborEncoderResult = cbor_encode_boolean(&pstatMap, pstat->isOp); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ISOP Name Value."); cborEncoderResult = cbor_encode_text_string(&pstatMap, OIC_JSON_DEVICE_ID_NAME, strlen(OIC_JSON_DEVICE_ID_NAME)); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Device Id Tag."); ret = ConvertUuidToStr(&pstat->deviceID, &strUuid); VERIFY_SUCCESS(TAG, OC_STACK_OK == ret , ERROR); cborEncoderResult = cbor_encode_text_string(&pstatMap, strUuid, strlen(strUuid)); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Device Id Value."); OICFree(strUuid); strUuid = NULL; cborEncoderResult = cbor_encode_text_string(&pstatMap, OIC_JSON_CM_NAME, strlen(OIC_JSON_CM_NAME)); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding CM Name Tag."); cborEncoderResult = cbor_encode_int(&pstatMap, pstat->cm); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding CM Name Value."); cborEncoderResult = cbor_encode_text_string(&pstatMap, OIC_JSON_TM_NAME, strlen(OIC_JSON_TM_NAME)); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding TM Name Tag."); cborEncoderResult = cbor_encode_int(&pstatMap, pstat->tm); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding TM Name Value."); cborEncoderResult = cbor_encode_text_string(&pstatMap, OIC_JSON_OM_NAME, strlen(OIC_JSON_OM_NAME)); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding OM Name Tag."); cborEncoderResult = cbor_encode_int(&pstatMap, pstat->om); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding OM Name Value."); cborEncoderResult = cbor_encode_text_string(&pstatMap, OIC_JSON_SM_NAME, strlen(OIC_JSON_SM_NAME)); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding SM Name Tag."); cborEncoderResult = cbor_encode_int(&pstatMap, pstat->sm[0]); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding SM Name Value."); cborEncoderResult = cbor_encode_text_string(&pstatMap, OIC_JSON_ROWNERID_NAME, strlen(OIC_JSON_ROWNERID_NAME)); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ROwner Id Tag."); ret = ConvertUuidToStr(&pstat->rownerID, &strUuid); VERIFY_SUCCESS(TAG, OC_STACK_OK == ret , ERROR); cborEncoderResult = cbor_encode_text_string(&pstatMap, strUuid, strlen(strUuid)); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ROwner Id Value."); OICFree(strUuid); strUuid = NULL; cborEncoderResult = cbor_encoder_close_container(&encoder, &pstatMap); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Closing PSTAT Map."); if (CborNoError == cborEncoderResult) { *size = encoder.ptr - outPayload; *payload = outPayload; ret = OC_STACK_OK; } exit: if ((CborErrorOutOfMemory == cborEncoderResult) && (cborLen < CBOR_MAX_SIZE)) { // reallocate and try again! OICFree(outPayload); // Since the allocated initial memory failed, double the memory. cborLen += encoder.ptr - encoder.end; cborEncoderResult = CborNoError; ret = PstatToCBORPayload(pstat, payload, &cborLen); if (OC_STACK_OK == ret) { *size = cborLen; } } if ((CborNoError != cborEncoderResult) || (OC_STACK_OK != ret)) { OICFree(outPayload); outPayload = NULL; *payload = NULL; *size = 0; ret = OC_STACK_ERROR; } return ret; }
static int64_t OCConvertDiscoveryPayload(OCDiscoveryPayload* payload, uint8_t* outPayload, size_t* size) { CborEncoder encoder = {0}; int64_t err = 0; size_t resourceCount = OCDiscoveryPayloadGetResourceCount(payload); cbor_encoder_init(&encoder, outPayload, *size, 0); CborEncoder rootArray; err = err | cbor_encoder_create_array(&encoder, &rootArray, resourceCount); for(size_t i = 0; i < resourceCount; ++i) { CborEncoder map; OCResourcePayload* resource = OCDiscoveryPayloadGetResource(payload, i); if(!resource) { return OC_STACK_INVALID_PARAM; } err = err | cbor_encoder_create_map(&rootArray, &map, 3); // Uri err = err | AddTextStringToMap(&map, OC_RSRVD_HREF, sizeof(OC_RSRVD_HREF) - 1, resource->uri); // Server ID err = err | cbor_encode_text_string(&map, OC_RSRVD_SERVER_INSTANCE_ID, sizeof(OC_RSRVD_SERVER_INSTANCE_ID) - 1); err = err | cbor_encode_byte_string(&map, resource->sid, UUID_SIZE); // Prop Tag { CborEncoder propMap; err = err | cbor_encode_text_string(&map, OC_RSRVD_PROPERTY, sizeof(OC_RSRVD_PROPERTY) -1 ); err = err | cbor_encoder_create_map(&map, &propMap, 3); // Resource Type if (resource->types) { char* joinedTypes = OCStringLLJoin(resource->types); if (joinedTypes) { err = err | cbor_encode_text_string(&propMap, OC_RSRVD_RESOURCE_TYPE, sizeof(OC_RSRVD_RESOURCE_TYPE) - 1); err = err | cbor_encode_text_string(&propMap, joinedTypes, strlen(joinedTypes)); OICFree(joinedTypes); } else { return OC_STACK_NO_MEMORY; } } // Interface Types if (resource->interfaces) { char* joinedInterfaces = OCStringLLJoin(resource->interfaces); if (joinedInterfaces) { err = err | cbor_encode_text_string(&propMap, OC_RSRVD_INTERFACE, sizeof(OC_RSRVD_INTERFACE) - 1); err = err | cbor_encode_text_string(&propMap, joinedInterfaces, strlen(joinedInterfaces)); OICFree(joinedInterfaces); } else { return OC_STACK_NO_MEMORY; } } // Policy { CborEncoder policyMap; err = err | cbor_encode_text_string(&propMap, OC_RSRVD_POLICY, sizeof(OC_RSRVD_POLICY) - 1); err = err | cbor_encoder_create_map(&propMap, &policyMap, CborIndefiniteLength); // Bitmap err = err | cbor_encode_text_string(&policyMap, OC_RSRVD_BITMAP, sizeof(OC_RSRVD_BITMAP) - 1); err = err | cbor_encode_uint(&policyMap, resource->bitmap); if(resource->secure) { err = err | cbor_encode_text_string(&policyMap, OC_RSRVD_SECURE, sizeof(OC_RSRVD_SECURE) - 1); err = err | cbor_encode_boolean(&policyMap, OC_RESOURCE_SECURE); if(resource->port != 0) { err = err | cbor_encode_text_string(&policyMap, OC_RSRVD_HOSTING_PORT, sizeof(OC_RSRVD_HOSTING_PORT) - 1); err = err | cbor_encode_uint(&policyMap, resource->port); } } err = err | cbor_encoder_close_container(&propMap, &policyMap); } // Close err = err | cbor_encoder_close_container(&map, &propMap); } // Close Item err = err | cbor_encoder_close_container(&rootArray, &map); } // Close main array err = err | cbor_encoder_close_container(&encoder, &rootArray); return checkError(err, &encoder, outPayload, size); }