static int64_t OCConvertDevicePayload(OCDevicePayload* payload, uint8_t* outPayload, size_t* size) { CborEncoder encoder = {0}; int64_t err = 0; cbor_encoder_init(&encoder, outPayload, *size, 0); CborEncoder rootArray; err = err | cbor_encoder_create_array(&encoder, &rootArray, 1); { CborEncoder map; err = err | cbor_encoder_create_map(&rootArray, &map, CborIndefiniteLength); // uri err = err | ConditionalAddTextStringToMap(&map, OC_RSRVD_HREF, sizeof(OC_RSRVD_HREF) - 1, payload->uri); // 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); // Device ID err = err | cbor_encode_text_string(&repMap, OC_RSRVD_DEVICE_ID, sizeof(OC_RSRVD_DEVICE_ID) - 1); err = err | cbor_encode_byte_string(&repMap, payload->sid, UUID_SIZE); // Device Name err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_DEVICE_NAME, sizeof(OC_RSRVD_DEVICE_NAME) - 1, payload->deviceName); // Device Spec Version err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_SPEC_VERSION, sizeof(OC_RSRVD_SPEC_VERSION) - 1, payload->specVersion); // Device data Model Version err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_DATA_MODEL_VERSION, sizeof(OC_RSRVD_DATA_MODEL_VERSION) - 1, payload->dataModelVersion); err = err | cbor_encoder_close_container(&map, &repMap); } // Close Map 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); }
static int64_t OCConvertSecurityPayload(OCSecurityPayload* payload, uint8_t* outPayload, size_t* size) { CborEncoder encoder; int64_t err = 0; cbor_encoder_init(&encoder, outPayload, *size, 0); CborEncoder rootArray; err = err | cbor_encoder_create_array(&encoder, &rootArray, 1); CborEncoder map; err = err | cbor_encoder_create_map(&rootArray, &map, CborIndefiniteLength); if(payload->securityData) { err = err | AddTextStringToMap(&map, OC_RSRVD_REPRESENTATION, sizeof(OC_RSRVD_REPRESENTATION) - 1, payload->securityData); } err = err | cbor_encoder_close_container(&rootArray, &map); err = err | cbor_encoder_close_container(&encoder, &rootArray); return checkError(err, &encoder, outPayload, size); }
static OCStackResult OCConvertSecurityPayload(OCSecurityPayload* 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; cbor_encoder_init(&encoder, *outPayload, *size, 0); CborEncoder rootArray; err = err || cbor_encoder_create_array(&encoder, &rootArray, 2); err = err || cbor_encode_uint(&rootArray, PAYLOAD_TYPE_SECURITY); CborEncoder map; err = err || cbor_encoder_create_map(&rootArray, &map, CborIndefiniteLength); if(payload->securityData) { err = err || AddTextStringToMap(&map, OC_RSRVD_REPRESENTATION, sizeof(OC_RSRVD_REPRESENTATION) - 1, payload->securityData); } err = err || cbor_encoder_close_container(&rootArray, &map); err = err || cbor_encoder_close_container(&encoder, &rootArray); if(err) { OC_LOG_V(ERROR, TAG, "Convert Security Payload failed", err); OICFree(*outPayload); 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; }
static int64_t OCConvertDevicePayload(OCDevicePayload *payload, uint8_t *outPayload, size_t *size) { if (!payload) { return CborUnknownError; } int64_t err = CborNoError; CborEncoder encoder; cbor_encoder_init(&encoder, outPayload, *size, 0); CborEncoder repMap; err |= cbor_encoder_create_map(&encoder, &repMap, CborIndefiniteLength); VERIFY_CBOR_SUCCESS(TAG, err, "Failed creating device map"); // Resource Type if (payload->types) { OIC_LOG(INFO, TAG, "Payload has types"); err |= OCStringLLJoin(&repMap, OC_RSRVD_RESOURCE_TYPE, payload->types); VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding resource type tag/value."); } if (payload->interfaces) { OIC_LOG(INFO, TAG, "Payload has interface"); err |= OCStringLLJoin(&repMap, OC_RSRVD_INTERFACE, payload->interfaces); VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding interface type tag/value."); } // Device ID err |= AddTextStringToMap(&repMap, OC_RSRVD_DEVICE_ID, sizeof(OC_RSRVD_DEVICE_ID) - 1 , payload->sid); VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding device id"); // Device Name err |= ConditionalAddTextStringToMap(&repMap, OC_RSRVD_DEVICE_NAME, sizeof(OC_RSRVD_DEVICE_NAME) - 1, payload->deviceName); VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding device name"); // Device Spec Version err |= ConditionalAddTextStringToMap(&repMap, OC_RSRVD_SPEC_VERSION, sizeof(OC_RSRVD_SPEC_VERSION) - 1, payload->specVersion); VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding data spec version"); // Device data Model Version err |= ConditionalAddTextStringToMap(&repMap, OC_RSRVD_DATA_MODEL_VERSION, sizeof(OC_RSRVD_DATA_MODEL_VERSION) - 1, payload->dataModelVersion); VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding data model version"); err |= cbor_encoder_close_container(&encoder, &repMap); VERIFY_CBOR_SUCCESS(TAG, err, "Failed closing device map"); exit: return checkError(err, &encoder, outPayload, size); }
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; }
static int64_t OCConvertRepMap(CborEncoder *map, const OCRepPayload *payload) { int64_t err = CborNoError; CborEncoder repMap; err |= cbor_encoder_create_map(map, &repMap, CborIndefiniteLength); VERIFY_CBOR_SUCCESS(TAG, err, "Failed creating rep map"); err |= OCConvertSingleRepPayload(&repMap, payload); VERIFY_CBOR_SUCCESS(TAG, err, "Failed converting single rep payload"); err |= cbor_encoder_close_container(map, &repMap); VERIFY_CBOR_SUCCESS(TAG, err, "Failed closing rep map"); exit: return err; }
static int64_t OCTagsPayloadToCbor(OCTagsPayload *tags, CborEncoder *setMap) { CborEncoder tagsMap; int64_t cborEncoderResult = CborNoError; cborEncoderResult |= cbor_encoder_create_map(setMap, &tagsMap, CborIndefiniteLength); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to create tags map"); cborEncoderResult |= ConditionalAddTextStringToMap(&tagsMap, OC_RSRVD_DEVICE_NAME, tags->n.deviceName); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_DEVICE_NAME in tags map"); cborEncoderResult |= ConditionalAddTextStringToMap(&tagsMap, OC_RSRVD_DEVICE_ID, (char *)tags->di.id); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_DEVICE_ID in tags map"); cborEncoderResult |= ConditionalAddTextStringToMap(&tagsMap, OC_RSRVD_RTS, tags->rts); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_RTS in tags map"); cborEncoderResult |= ConditionalAddTextStringToMap(&tagsMap, OC_RSRVD_DREL, tags->drel); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_DREL in tags map"); cborEncoderResult |= ConditionalAddTextStringToMap(&tagsMap, OC_RSRVD_BASE_URI, tags->baseURI); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_BASE_URI in tags map"); { uint64_t value = tags->bitmap; cborEncoderResult |= ConditionalAddIntToMap(&tagsMap, OC_RSRVD_BITMAP, &value); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_BITMAP in tags map"); value = tags->port; cborEncoderResult |= ConditionalAddIntToMap(&tagsMap, OC_RSRVD_HOSTING_PORT, &value); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_HOSTING_PORT in tags map"); value = tags->ins; cborEncoderResult |= ConditionalAddIntToMap(&tagsMap, OC_RSRVD_INS, &value); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_INS in tags map"); value = tags->ttl; cborEncoderResult |= ConditionalAddIntToMap(&tagsMap, OC_RSRVD_TTL, &value); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_TTL in tags map"); } cborEncoderResult |= cbor_encoder_close_container(setMap, &tagsMap); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to close container"); exit: return cborEncoderResult; }
static int64_t OCConvertPresencePayload(OCPresencePayload *payload, uint8_t *outPayload, size_t *size) { int64_t err = CborNoError; CborEncoder encoder; cbor_encoder_init(&encoder, outPayload, *size, 0); CborEncoder map; err |= cbor_encoder_create_map(&encoder, &map, CborIndefiniteLength); VERIFY_CBOR_SUCCESS(TAG, err, "Failed creating presence map"); // Sequence Number err |= cbor_encode_text_string(&map, OC_RSRVD_NONCE, sizeof(OC_RSRVD_NONCE) - 1); VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding nonce tag to presence map"); err |= cbor_encode_uint(&map, payload->sequenceNumber); VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding nonce value to presence map"); // Max Age err |= cbor_encode_text_string(&map, OC_RSRVD_TTL, sizeof(OC_RSRVD_TTL) - 1); VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding ttl tag to presence map"); err |= cbor_encode_uint(&map, payload->maxAge); VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding ttl value to presence map"); // Trigger err |= cbor_encode_text_string(&map, OC_RSRVD_TRIGGER, sizeof(OC_RSRVD_TRIGGER) - 1); VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding trigger tag to presence map"); err |= cbor_encode_simple_value(&map, payload->trigger); VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding trigger value to presence map"); // Resource type name if (payload->trigger != OC_PRESENCE_TRIGGER_DELETE) { err |= ConditionalAddTextStringToMap(&map, OC_RSRVD_RESOURCE_TYPE, sizeof(OC_RSRVD_RESOURCE_TYPE) - 1, payload->resourceType); VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding resource type to presence map"); } // Close Map err |= cbor_encoder_close_container(&encoder, &map); VERIFY_CBOR_SUCCESS(TAG, err, "Failed closing presence map"); exit: return checkError(err, &encoder, outPayload, size); }
static int64_t OCConvertPresencePayload(OCPresencePayload* payload, uint8_t* outPayload, size_t* size) { CborEncoder encoder = {0}; int64_t err = 0; cbor_encoder_init(&encoder, outPayload, *size, 0); CborEncoder rootArray; err = err | cbor_encoder_create_array(&encoder, &rootArray, 1); CborEncoder map; err = err | cbor_encoder_create_map(&rootArray, &map, CborIndefiniteLength); // Sequence Number err = err | cbor_encode_text_string(&map, OC_RSRVD_NONCE, sizeof(OC_RSRVD_NONCE) - 1); err = err | cbor_encode_uint(&map, payload->sequenceNumber); // Max Age err = err | cbor_encode_text_string(&map, OC_RSRVD_TTL, sizeof(OC_RSRVD_TTL) - 1); err = err | cbor_encode_uint(&map, payload->maxAge); // Trigger const char* triggerStr = convertTriggerEnumToString(payload->trigger); err = err | AddTextStringToMap(&map, OC_RSRVD_TRIGGER, sizeof(OC_RSRVD_TRIGGER) - 1, triggerStr); // Resource type name if(payload->trigger != OC_PRESENCE_TRIGGER_DELETE) { err = err | ConditionalAddTextStringToMap(&map, OC_RSRVD_RESOURCE_TYPE, sizeof(OC_RSRVD_RESOURCE_TYPE) - 1, payload->resourceType); } // Close Map err = err | cbor_encoder_close_container(&rootArray, &map); err = err | cbor_encoder_close_container(&encoder, &rootArray); return checkError(err, &encoder, outPayload, size); }
bool cnc_daemon_start_encode_response(int message_type, CNCWrapper * wrapper) { CborEncoder encoder, container; CborError err; uint8_t data[MTU] = {0}; if (wrapper == NULL) { KLOG_ERR(&log_handle, LOG_COMPONENT_NAME, "%s called with a NULL pointer\n", __func__); return false; } cbor_encoder_init(&encoder, data, MTU, 0); err = cbor_encoder_create_map(&encoder, &container, 4); //TODO: Dynamically assign map size if (err) { KLOG_ERR(&log_handle, LOG_COMPONENT_NAME, "Unable to initialize cbor encoder, Error code: %i", err); return false; } if (err = cbor_encode_text_stringz(&container, "MSG_TYPE")) { KLOG_ERR(&log_handle, LOG_COMPONENT_NAME, "Unable to encode key \"MSG_TYPE\". Error code: %i\n", err); return false; } if (err = cbor_encode_int(&container, message_type)) { KLOG_ERR(&log_handle, LOG_COMPONENT_NAME, "Unable to encode value for key \"MSG_TYPE\" Error code: %i\n", err); return false; } switch (message_type) { case RESPONSE_TYPE_COMMAND_RESULT: return cnc_daemon_encode_response(data, wrapper, &encoder, &container); case RESPONSE_TYPE_PROCESSING_ERROR: return cnc_daemon_encode_processing_error(data, wrapper, &encoder, &container); } }
/** * Log encode function * @param log structure, the encoder, json_value, * timestamp, index * @return 0 on success; non-zero on failure */ static int log_encode(struct log *log, CborEncoder *cb, int64_t ts, uint32_t index) { int rc; CborEncoder logs; CborError g_err = CborNoError; g_err |= cbor_encoder_create_map(cb, &logs, CborIndefiniteLength); g_err |= cbor_encode_text_stringz(&logs, "name"); g_err |= cbor_encode_text_stringz(&logs, log->l_name); g_err |= cbor_encode_text_stringz(&logs, "type"); g_err |= cbor_encode_uint(&logs, log->l_log->log_type); rc = log_encode_entries(log, &logs, ts, index); g_err |= cbor_encoder_close_container(cb, &logs); if (g_err) { return MGMT_ERR_ENOMEM; } return rc; }
static int64_t OCConvertPlatformPayload(OCPlatformPayload *payload, uint8_t *outPayload, size_t *size) { int64_t err = CborNoError; CborEncoder encoder; cbor_encoder_init(&encoder, outPayload, *size, 0); CborEncoder repMap; err |= cbor_encoder_create_map(&encoder, &repMap, CborIndefiniteLength); VERIFY_CBOR_SUCCESS(TAG, err, "Failed creating platform map"); // Platform ID err |= ConditionalAddTextStringToMap(&repMap, OC_RSRVD_PLATFORM_ID, sizeof(OC_RSRVD_PLATFORM_ID) - 1, payload->info.platformID); VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding platform id"); // MFG Name err |= ConditionalAddTextStringToMap(&repMap, OC_RSRVD_MFG_NAME, sizeof(OC_RSRVD_MFG_NAME) - 1, payload->info.manufacturerName); VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding mfg name"); // MFG Url err |= ConditionalAddTextStringToMap(&repMap, OC_RSRVD_MFG_URL, sizeof(OC_RSRVD_MFG_URL) - 1, payload->info.manufacturerUrl); VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding mfg url"); // Model Num err |= ConditionalAddTextStringToMap(&repMap, OC_RSRVD_MODEL_NUM, sizeof(OC_RSRVD_MODEL_NUM) -1, payload->info.modelNumber); VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding model num"); // Date of Mfg err |= ConditionalAddTextStringToMap(&repMap, OC_RSRVD_MFG_DATE, sizeof(OC_RSRVD_MFG_DATE) - 1, payload->info.dateOfManufacture); VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding mfg date"); // Platform Version err |= ConditionalAddTextStringToMap(&repMap, OC_RSRVD_PLATFORM_VERSION, sizeof(OC_RSRVD_PLATFORM_VERSION) - 1, payload->info.platformVersion); VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding platform version"); // OS Version err |= ConditionalAddTextStringToMap(&repMap, OC_RSRVD_OS_VERSION, sizeof(OC_RSRVD_OS_VERSION) - 1, payload->info.operatingSystemVersion); VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding OS version"); // Hardware Version err |= ConditionalAddTextStringToMap(&repMap, OC_RSRVD_HARDWARE_VERSION, sizeof(OC_RSRVD_HARDWARE_VERSION) - 1, payload->info.hardwareVersion); VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding HW version"); // Firmware Version err |= ConditionalAddTextStringToMap(&repMap, OC_RSRVD_FIRMWARE_VERSION, sizeof(OC_RSRVD_FIRMWARE_VERSION) - 1, payload->info.firmwareVersion); VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding firmware version"); // Support URL err |= ConditionalAddTextStringToMap(&repMap, OC_RSRVD_SUPPORT_URL, sizeof(OC_RSRVD_SUPPORT_URL) - 1, payload->info.supportUrl); VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding support url"); // System Time err |= ConditionalAddTextStringToMap(&repMap, OC_RSRVD_SYSTEM_TIME, sizeof(OC_RSRVD_SYSTEM_TIME) - 1, payload->info.systemTime); VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding system time"); // Resource type if (payload->rt) { err |= OCStringLLJoin(&repMap, OC_RSRVD_RESOURCE_TYPE, payload->rt); VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding resource type."); } // Resource interfaces if (payload->interfaces) { err |= OCStringLLJoin(&repMap, OC_RSRVD_INTERFACE, payload->interfaces); VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding platform interface type."); } // Close Map err |= cbor_encoder_close_container(&encoder, &repMap); VERIFY_CBOR_SUCCESS(TAG, err, "Failed closing rep map"); exit: return checkError(err, &encoder, outPayload, size); }
static OCStackResult OCConvertDevicePayload(OCDevicePayload* 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; cbor_encoder_init(&encoder, *outPayload, *size, 0); CborEncoder rootArray; err = err || cbor_encoder_create_array(&encoder, &rootArray, 2); err = err || cbor_encode_uint(&rootArray, PAYLOAD_TYPE_DEVICE); { CborEncoder map; err = err || cbor_encoder_create_map(&rootArray, &map, 2); // uri err = err || AddTextStringToMap(&map, OC_RSRVD_HREF, sizeof(OC_RSRVD_HREF) - 1, payload->uri); // 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, 4); // Device ID err = err || cbor_encode_text_string(&repMap, OC_RSRVD_DEVICE_ID, sizeof(OC_RSRVD_DEVICE_ID) - 1); err = err || cbor_encode_byte_string(&repMap, payload->sid, UUID_SIZE); // Device Name err = err || AddTextStringToMap(&repMap, OC_RSRVD_DEVICE_NAME, sizeof(OC_RSRVD_DEVICE_NAME) - 1, payload->deviceName); // Device Spec Version err = err || AddTextStringToMap(&repMap, OC_RSRVD_SPEC_VERSION, sizeof(OC_RSRVD_SPEC_VERSION) - 1, payload->specVersion); // Device data Model Version err = err || AddTextStringToMap(&repMap, OC_RSRVD_DATA_MODEL_VERSION, sizeof(OC_RSRVD_DATA_MODEL_VERSION) - 1, payload->dataModelVersion); err = err || cbor_encoder_close_container(&map, &repMap); } // Close Map 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 Device 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; }
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); }
int64_t OCRDPayloadToCbor(const OCRDPayload *rdPayload, uint8_t *outPayload, size_t *size) { if (!outPayload || !size) { OC_LOG(ERROR, TAG, "Invalid parameters."); return OC_STACK_INVALID_PARAM; } CborEncoder encoder; int flags = 0; cbor_encoder_init(&encoder, outPayload, *size, flags); CborEncoder rootArray; CborError cborEncoderResult; cborEncoderResult = cbor_encoder_create_array(&encoder, &rootArray, CBOR_ROOT_ARRAY_LENGTH); if (CborNoError != cborEncoderResult) { OC_LOG(ERROR, TAG, "Failed creating cbor array."); goto cbor_error; } if (rdPayload->rdDiscovery) { CborEncoder map; cborEncoderResult = cbor_encoder_create_map(&rootArray, &map, CborIndefiniteLength); if (CborNoError != cborEncoderResult) { OC_LOG(ERROR, TAG, "Failed creating discovery map."); goto cbor_error; } if (CborNoError != ConditionalAddTextStringToMap(&map, OC_RSRVD_DEVICE_NAME, sizeof(OC_RSRVD_DEVICE_NAME) - 1, (char *)rdPayload->rdDiscovery->n.deviceName)) { OC_LOG(ERROR, TAG, "Failed setting OC_RSRVD_DEVICE_NAME."); goto cbor_error; } if (CborNoError != ConditionalAddTextStringToMap(&map, OC_RSRVD_DEVICE_ID, sizeof(OC_RSRVD_DEVICE_ID) - 1, (char *)rdPayload->rdDiscovery->di.id)) { OC_LOG(ERROR, TAG, "Failed setting OC_RSRVD_DEVICE_ID."); goto cbor_error; } uint64_t sel = (uint8_t) rdPayload->rdDiscovery->sel; if (CborNoError != ConditionalAddIntToMap(&map, OC_RSRVD_RD_DISCOVERY_SEL, sizeof(OC_RSRVD_RD_DISCOVERY_SEL) - 1, &sel)) { OC_LOG(ERROR, TAG, "Failed setting OC_RSRVD_RD_DISCOVERY_SEL."); goto cbor_error; } cborEncoderResult = cbor_encoder_close_container(&rootArray, &map); if (CborNoError != cborEncoderResult) { OC_LOG(ERROR, TAG, "Failed closing discovery map."); goto cbor_error; } } else if (rdPayload->rdPublish) { 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 *rdPublish = rdPayload->rdPublish; while (rdPublish) { if (OC_STACK_OK != OCTagsPayloadToCbor(rdPublish->tags, &colArray)) { OC_LOG(ERROR, TAG, "Failed creating tags payload."); goto cbor_error; } if (OC_STACK_OK != OCLinksPayloadToCbor(rdPublish->setLinks, &colArray)) { OC_LOG(ERROR, TAG, "Failed creating links payload."); goto cbor_error; } rdPublish = rdPublish->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 container. "); goto cbor_error; } *size = encoder.ptr - outPayload; return OC_STACK_OK; cbor_error: OICFree(outPayload); return OC_STACK_ERROR; }
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; }
OCStackResult OCTagsPayloadToCbor(OCTagsPayload *tags, CborEncoder *setMap) { CborEncoder tagsMap; CborError cborEncoderResult = cbor_encoder_create_map(setMap, &tagsMap, CborIndefiniteLength); if (CborNoError != cborEncoderResult) { OC_LOG(ERROR, TAG, "Failed creating TAGS map."); return OC_STACK_ERROR; } if (CborNoError != ConditionalAddTextStringToMap(&tagsMap, OC_RSRVD_DEVICE_NAME, sizeof(OC_RSRVD_DEVICE_NAME) - 1, (char *)tags->n.deviceName)) { OC_LOG(ERROR, TAG, "Failed adding OC_RSRVD_DEVICE_NAME in TAGS map."); return OC_STACK_ERROR; } if (CborNoError != ConditionalAddTextStringToMap(&tagsMap, OC_RSRVD_DEVICE_ID, sizeof(OC_RSRVD_DEVICE_ID) - 1, (char *)tags->di.id)) { OC_LOG(ERROR, TAG, "Failed adding OC_RSRVD_DEVICE_ID in TAGS map."); return OC_STACK_ERROR; } if (CborNoError != ConditionalAddTextStringToMap(&tagsMap, OC_RSRVD_RTS, sizeof(OC_RSRVD_RTS) - 1, (char *)tags->rts)) { OC_LOG(ERROR, TAG, "Failed adding OC_RSRVD_RTS in TAGS map."); return OC_STACK_ERROR; } if (CborNoError != ConditionalAddTextStringToMap(&tagsMap, OC_RSRVD_DREL, sizeof(OC_RSRVD_DREL) - 1, (char *)tags->drel)) { OC_LOG(ERROR, TAG, "Failed adding OC_RSRVD_DREL in TAGS map."); return OC_STACK_ERROR; } if (CborNoError != ConditionalAddTextStringToMap(&tagsMap, OC_RSRVD_BASE_URI, sizeof(OC_RSRVD_BASE_URI) - 1, (char *)tags->baseURI)) { OC_LOG(ERROR, TAG, "Failed adding OC_RSRVD_BASE_URI in TAGS map."); return OC_STACK_ERROR; } uint64_t temp = (uint64_t)tags->bitmap; if (CborNoError != ConditionalAddIntToMap(&tagsMap, OC_RSRVD_BITMAP, sizeof(OC_RSRVD_BITMAP) - 1, &temp)) { OC_LOG(ERROR, TAG, "Failed adding OC_RSRVD_BITMAP in TAGS map."); return OC_STACK_ERROR; } temp = (uint64_t)tags->port; if (CborNoError != ConditionalAddIntToMap(&tagsMap, OC_RSRVD_HOSTING_PORT, sizeof(OC_RSRVD_HOSTING_PORT) - 1, &temp)) { OC_LOG(ERROR, TAG, "Failed adding OC_RSRVD_HOSTING_PORT in TAGS map."); return OC_STACK_ERROR; } temp = (uint64_t)tags->ins; if (CborNoError != ConditionalAddIntToMap(&tagsMap, OC_RSRVD_INS, sizeof(OC_RSRVD_INS) - 1, &temp)) { OC_LOG(ERROR, TAG, "Failed adding OC_RSRVD_INS in TAGS map."); return OC_STACK_ERROR; } temp = (uint64_t)tags->ttl; if (CborNoError != ConditionalAddIntToMap(&tagsMap, OC_RSRVD_TTL, sizeof(OC_RSRVD_TTL) - 1, &temp)) { OC_LOG(ERROR, TAG, "Failed adding OC_RSRVD_TTL in TAGS map."); return OC_STACK_ERROR; } cborEncoderResult = cbor_encoder_close_container(setMap, &tagsMap); if (CborNoError != cborEncoderResult) { OC_LOG(ERROR, TAG, "Failed closing TAGS map."); return OC_STACK_ERROR; } return OC_STACK_OK; }
int64_t OCRDPayloadToCbor(const OCRDPayload *rdPayload, uint8_t *outPayload, size_t *size) { int64_t cborEncoderResult = CborErrorIO; int flags = 0; CborEncoder encoder; VERIFY_PARAM_NON_NULL(TAG, rdPayload, "Invalid input parameter rdPayload"); VERIFY_PARAM_NON_NULL(TAG, outPayload, "Invalid input parameter outPayload"); VERIFY_PARAM_NON_NULL(TAG, size, "Invalid input parameter size"); cbor_encoder_init(&encoder, outPayload, *size, flags); if (rdPayload->rdDiscovery) { CborEncoder map; cborEncoderResult |= cbor_encoder_create_map(&encoder, &map, CborIndefiniteLength); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to create discovery map"); cborEncoderResult |= ConditionalAddTextStringToMap(&map, OC_RSRVD_DEVICE_NAME, rdPayload->rdDiscovery->n.deviceName); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_DEVICE_NAME in map"); cborEncoderResult |= ConditionalAddTextStringToMap(&map, OC_RSRVD_DEVICE_ID, (char *)rdPayload->rdDiscovery->di.id); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_DEVICE_ID in map"); { uint64_t value = rdPayload->rdDiscovery->sel; cborEncoderResult |= ConditionalAddIntToMap(&map, OC_RSRVD_RD_DISCOVERY_SEL, &value); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add RD_DISCOVERY_SEL in map"); } cborEncoderResult |= cbor_encoder_close_container(&encoder, &map); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed closing discovery map"); } else if (rdPayload->rdPublish) { CborEncoder colArray; cborEncoderResult |= cbor_encoder_create_array(&encoder, &colArray, CborIndefiniteLength); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to create collection array"); OCResourceCollectionPayload *rdPublish = rdPayload->rdPublish; while (rdPublish) { cborEncoderResult |= OCTagsPayloadToCbor(rdPublish->tags, &colArray); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed adding tags payload"); cborEncoderResult |= OCLinksPayloadToCbor(rdPublish->setLinks, &colArray); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed adding setLinks payload"); rdPublish = rdPublish->next; } cborEncoderResult |= cbor_encoder_close_container(&encoder, &colArray); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed closing collection array"); } else { CborEncoder map; cborEncoderResult |= cbor_encoder_create_map(&encoder, &map, CborIndefiniteLength); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed entering discovery map"); cborEncoderResult |= cbor_encoder_close_container(&encoder, &map); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed closing discovery map"); } if (cborEncoderResult == CborErrorOutOfMemory) { *size += encoder.ptr - encoder.end; } else { *size = encoder.ptr - outPayload; } return cborEncoderResult; exit: OICFree(outPayload); return cborEncoderResult; }
static OCStackResult OCConvertPlatformPayload(OCPlatformPayload* 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; cbor_encoder_init(&encoder, *outPayload, *size, 0); CborEncoder rootArray; err = err || cbor_encoder_create_array(&encoder, &rootArray, 2); err = err || cbor_encode_uint(&rootArray, PAYLOAD_TYPE_PLATFORM); { CborEncoder map; err = err || cbor_encoder_create_map(&rootArray, &map, CborIndefiniteLength); // uri err = err || AddTextStringToMap(&map, OC_RSRVD_HREF, sizeof(OC_RSRVD_HREF) - 1, payload->uri); // 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); // Platform ID err = err || AddTextStringToMap(&repMap, OC_RSRVD_PLATFORM_ID, sizeof(OC_RSRVD_PLATFORM_ID) - 1, payload->info.platformID); // MFG Name err = err || AddTextStringToMap(&repMap, OC_RSRVD_MFG_NAME, sizeof(OC_RSRVD_MFG_NAME) - 1, payload->info.manufacturerName); // MFG Url err = err || ConditionalAddTextStringToMap(&repMap, OC_RSRVD_MFG_URL, sizeof(OC_RSRVD_MFG_URL) - 1, payload->info.manufacturerUrl); // Model Num err = err || ConditionalAddTextStringToMap(&repMap, OC_RSRVD_MODEL_NUM, sizeof(OC_RSRVD_MODEL_NUM) - 1, payload->info.modelNumber); // Date of Mfg err = err || ConditionalAddTextStringToMap(&repMap, OC_RSRVD_MFG_DATE, sizeof(OC_RSRVD_MFG_DATE) - 1, payload->info.dateOfManufacture); // Platform Version err = err || ConditionalAddTextStringToMap(&repMap, OC_RSRVD_PLATFORM_VERSION, sizeof(OC_RSRVD_PLATFORM_VERSION) - 1, payload->info.platformVersion); // OS Version err = err || ConditionalAddTextStringToMap(&repMap, OC_RSRVD_OS_VERSION, sizeof(OC_RSRVD_OS_VERSION) - 1, payload->info.operatingSystemVersion); // Hardware Version err = err || ConditionalAddTextStringToMap(&repMap, OC_RSRVD_HARDWARE_VERSION, sizeof(OC_RSRVD_HARDWARE_VERSION) - 1, payload->info.hardwareVersion); // Firmware Version err = err || ConditionalAddTextStringToMap(&repMap, OC_RSRVD_FIRMWARE_VERSION, sizeof(OC_RSRVD_FIRMWARE_VERSION) - 1, payload->info.firmwareVersion); // Support URL err = err || ConditionalAddTextStringToMap(&repMap, OC_RSRVD_SUPPORT_URL, sizeof(OC_RSRVD_SUPPORT_URL) - 1, payload->info.supportUrl); // System Time err = err || ConditionalAddTextStringToMap(&repMap, OC_RSRVD_SYSTEM_TIME, sizeof(OC_RSRVD_SYSTEM_TIME) - 1, payload->info.systemTime); err = err || cbor_encoder_close_container(&map, &repMap); } // Close Map 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 Platform 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; }
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 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; }
static OCStackResult OCConvertPresencePayload(OCPresencePayload* 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; cbor_encoder_init(&encoder, *outPayload, *size, 0); CborEncoder rootArray; err = err || cbor_encoder_create_array(&encoder, &rootArray, 2); err = err || cbor_encode_uint(&rootArray, PAYLOAD_TYPE_PRESENCE); CborEncoder map; err = err || cbor_encoder_create_map(&rootArray, &map, CborIndefiniteLength); // Sequence Number err = err || cbor_encode_text_string(&map, OC_RSRVD_NONCE, sizeof(OC_RSRVD_NONCE) - 1); err = err || cbor_encode_uint(&map, payload->sequenceNumber); // Max Age err = err || cbor_encode_text_string(&map, OC_RSRVD_TTL, sizeof(OC_RSRVD_TTL) - 1); err = err || cbor_encode_uint(&map, payload->maxAge); // Trigger const char* triggerStr = convertTriggerEnumToString(payload->trigger); err = err || AddTextStringToMap(&map, OC_RSRVD_TRIGGER, sizeof(OC_RSRVD_TRIGGER) - 1, triggerStr); // Resource type name if(payload->trigger != OC_PRESENCE_TRIGGER_DELETE) { err = err || ConditionalAddTextStringToMap(&map, OC_RSRVD_RESOURCE_TYPE, sizeof(OC_RSRVD_RESOURCE_TYPE) - 1, payload->resourceType); } // Close Map err = err || cbor_encoder_close_container(&rootArray, &map); err = err || cbor_encoder_close_container(&encoder, &rootArray); if(err) { OC_LOG_V(ERROR, TAG, "Convert Presence 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; }
/** * Updates the Secure Virtual Resource(s) into the Persistent Storage. * This function stores cbor-payload of each resource by appending resource name, * and empty payload implies deleting the value * * @param rsrcName - pointer of character string for the SVR name (e.g. "acl") * @param psPayload - pointer of the updated Secure Virtual Resource(s) * @param psSize - the updated size of Secure Virtual Resource(s) * * @return OCStackResult - result of updating Secure Virtual Resource(s) */ OCStackResult UpdateSecureResourceInPS(const char *rsrcName, const uint8_t *psPayload, size_t psSize) { OIC_LOG(DEBUG, TAG, "UpdateSecureResourceInPS IN"); if (!rsrcName) { return OC_STACK_INVALID_PARAM; } size_t dbSize = 0; size_t outSize = 0; uint8_t *dbData = NULL; uint8_t *outPayload = NULL; uint8_t *aclCbor = NULL; uint8_t *pstatCbor = NULL; uint8_t *doxmCbor = NULL; uint8_t *amaclCbor = NULL; uint8_t *svcCbor = NULL; uint8_t *credCbor = NULL; uint8_t *pconfCbor = NULL; int64_t cborEncoderResult = CborNoError; OCStackResult ret = GetSecureVirtualDatabaseFromPS(NULL, &dbData, &dbSize); if (dbData && dbSize) { size_t aclCborLen = 0; size_t pstatCborLen = 0; size_t doxmCborLen = 0; size_t amaclCborLen = 0; size_t svcCborLen = 0; size_t credCborLen = 0; size_t pconfCborLen = 0; // Gets each secure virtual resource from persistent storage // this local scoping intended, for destroying large cbor instances after use { CborParser parser; // will be initialized in |cbor_parser_init| CborValue cbor; // will be initialized in |cbor_parser_init| cbor_parser_init(dbData, dbSize, 0, &parser, &cbor); CborValue curVal = {0}; CborError cborFindResult = CborNoError; cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_ACL_NAME, &curVal); if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal)) { cborFindResult = cbor_value_dup_byte_string(&curVal, &aclCbor, &aclCborLen, NULL); VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding ACL Name Value."); } cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_PSTAT_NAME, &curVal); if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal)) { cborFindResult = cbor_value_dup_byte_string(&curVal, &pstatCbor, &pstatCborLen, NULL); VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding PSTAT Name Value."); } cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_DOXM_NAME, &curVal); if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal)) { cborFindResult = cbor_value_dup_byte_string(&curVal, &doxmCbor, &doxmCborLen, NULL); VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding DOXM Name Value."); } cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_AMACL_NAME, &curVal); if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal)) { cborFindResult = cbor_value_dup_byte_string(&curVal, &amaclCbor, &amaclCborLen, NULL); VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding AMACL Name Value."); } cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_SVC_NAME, &curVal); if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal)) { cborFindResult = cbor_value_dup_byte_string(&curVal, &svcCbor, &svcCborLen, NULL); VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding SVC Name Value."); } cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_CRED_NAME, &curVal); if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal)) { cborFindResult = cbor_value_dup_byte_string(&curVal, &credCbor, &credCborLen, NULL); VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding CRED Name Value."); } cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_PCONF_NAME, &curVal); if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal)) { cborFindResult = cbor_value_dup_byte_string(&curVal, &pconfCbor, &pconfCborLen, NULL); VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding PCONF Name Value."); } } // Updates the added |psPayload| with the existing secure virtual resource(s) // this local scoping intended, for destroying large cbor instances after use { size_t size = aclCborLen + pstatCborLen + doxmCborLen + amaclCborLen + svcCborLen + credCborLen + pconfCborLen + psSize + 255; // This added '255' is arbitrary value that is added to cover the name of the resource, map addition and ending outPayload = (uint8_t *) OICCalloc(1, size); VERIFY_NON_NULL(TAG, outPayload, ERROR); CborEncoder encoder; // will be initialized in |cbor_parser_init| cbor_encoder_init(&encoder, outPayload, size, 0); CborEncoder secRsrc; // will be initialized in |cbor_encoder_create_map| cborEncoderResult |= cbor_encoder_create_map(&encoder, &secRsrc, CborIndefiniteLength); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PS Map."); if (psPayload && psSize) { cborEncoderResult |= cbor_encode_text_string(&secRsrc, rsrcName, strlen(rsrcName)); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Value Tag"); cborEncoderResult |= cbor_encode_byte_string(&secRsrc, psPayload, psSize); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Value."); } if (strcmp(OIC_JSON_ACL_NAME, rsrcName) && aclCborLen) { cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_ACL_NAME, strlen(OIC_JSON_ACL_NAME)); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ACL Name."); cborEncoderResult |= cbor_encode_byte_string(&secRsrc, aclCbor, aclCborLen); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ACL Value."); } if (strcmp(OIC_JSON_PSTAT_NAME, rsrcName) && pstatCborLen) { cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_PSTAT_NAME, strlen(OIC_JSON_PSTAT_NAME)); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PSTAT Name."); cborEncoderResult |= cbor_encode_byte_string(&secRsrc, pstatCbor, pstatCborLen); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PSTAT Value."); } if (strcmp(OIC_JSON_DOXM_NAME, rsrcName) && doxmCborLen) { cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_DOXM_NAME, strlen(OIC_JSON_DOXM_NAME)); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Doxm Name."); cborEncoderResult |= cbor_encode_byte_string(&secRsrc, doxmCbor, doxmCborLen); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Doxm Value."); } if (strcmp(OIC_JSON_AMACL_NAME, rsrcName) && amaclCborLen) { cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_AMACL_NAME, strlen(OIC_JSON_AMACL_NAME)); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Amacl Name."); cborEncoderResult |= cbor_encode_byte_string(&secRsrc, amaclCbor, amaclCborLen); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Amacl Value."); } if (strcmp(OIC_JSON_SVC_NAME, rsrcName) && svcCborLen) { cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_SVC_NAME, strlen(OIC_JSON_SVC_NAME)); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding SVC Name."); cborEncoderResult |= cbor_encode_byte_string(&secRsrc, svcCbor, svcCborLen); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding SVC Value."); } if (strcmp(OIC_JSON_CRED_NAME, rsrcName) && credCborLen) { cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_CRED_NAME, strlen(OIC_JSON_CRED_NAME)); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Cred Name."); cborEncoderResult |= cbor_encode_byte_string(&secRsrc, credCbor, credCborLen); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Cred Value."); } if (strcmp(OIC_JSON_PCONF_NAME, rsrcName) && pconfCborLen) { cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_PCONF_NAME, strlen(OIC_JSON_PCONF_NAME)); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Pconf Name."); cborEncoderResult |= cbor_encode_byte_string(&secRsrc, pconfCbor, pconfCborLen); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Pconf Value."); } cborEncoderResult |= cbor_encoder_close_container(&encoder, &secRsrc); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing Array."); outSize = encoder.ptr - outPayload; } } else if (psPayload && psSize) { size_t size = psSize + 255; // This added '255' is arbitrary value that is added to cover the name of the resource, map addition and ending outPayload = (uint8_t *) OICCalloc(1, size); VERIFY_NON_NULL(TAG, outPayload, ERROR); CborEncoder encoder; // will be initialized in |cbor_parser_init| cbor_encoder_init(&encoder, outPayload, size, 0); CborEncoder secRsrc; // will be initialized in |cbor_encoder_create_map| cborEncoderResult |= cbor_encoder_create_map(&encoder, &secRsrc, CborIndefiniteLength); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PS Map."); cborEncoderResult |= cbor_encode_text_string(&secRsrc, rsrcName, strlen(rsrcName)); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Value Tag"); cborEncoderResult |= cbor_encode_byte_string(&secRsrc, psPayload, psSize); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Value."); cborEncoderResult |= cbor_encoder_close_container(&encoder, &secRsrc); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing Array."); outSize = encoder.ptr - outPayload; } if (outPayload && outSize) { OIC_LOG_V(DEBUG, TAG, "Writting in the file: %zu", outSize); OCPersistentStorage* ps = SRMGetPersistentStorageHandler(); if (ps) { FILE *fp = ps->open(SVR_DB_DAT_FILE_NAME, "wb"); if (fp) { size_t numberItems = ps->write(outPayload, 1, outSize, fp); if (outSize == numberItems) { OIC_LOG_V(DEBUG, TAG, "Written %zu bytes into SVR database file", outSize); ret = OC_STACK_OK; } else { OIC_LOG_V(ERROR, TAG, "Failed writing %zu in the database", numberItems); } ps->close(fp); } else { OIC_LOG(ERROR, TAG, "File open failed."); } } } OIC_LOG(DEBUG, TAG, "UpdateSecureResourceInPS OUT"); exit: OICFree(dbData); OICFree(outPayload); OICFree(aclCbor); OICFree(pstatCbor); OICFree(doxmCbor); OICFree(amaclCbor); OICFree(svcCbor); OICFree(credCbor); OICFree(pconfCbor); return ret; }
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 OCConvertPlatformPayload(OCPlatformPayload* payload, uint8_t* outPayload, size_t* size) { CborEncoder encoder = {0}; int64_t err = 0; cbor_encoder_init(&encoder, outPayload, *size, 0); CborEncoder rootArray; err = err | cbor_encoder_create_array(&encoder, &rootArray, 1); { CborEncoder map; err = err | cbor_encoder_create_map(&rootArray, &map, CborIndefiniteLength); // uri err = err | ConditionalAddTextStringToMap(&map, OC_RSRVD_HREF, sizeof(OC_RSRVD_HREF) - 1, payload->uri); // 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); // Platform ID err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_PLATFORM_ID, sizeof(OC_RSRVD_PLATFORM_ID) - 1, payload->info.platformID); // MFG Name err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_MFG_NAME, sizeof(OC_RSRVD_MFG_NAME) - 1, payload->info.manufacturerName); // MFG Url err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_MFG_URL, sizeof(OC_RSRVD_MFG_URL) - 1, payload->info.manufacturerUrl); // Model Num err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_MODEL_NUM, sizeof(OC_RSRVD_MODEL_NUM) - 1, payload->info.modelNumber); // Date of Mfg err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_MFG_DATE, sizeof(OC_RSRVD_MFG_DATE) - 1, payload->info.dateOfManufacture); // Platform Version err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_PLATFORM_VERSION, sizeof(OC_RSRVD_PLATFORM_VERSION) - 1, payload->info.platformVersion); // OS Version err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_OS_VERSION, sizeof(OC_RSRVD_OS_VERSION) - 1, payload->info.operatingSystemVersion); // Hardware Version err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_HARDWARE_VERSION, sizeof(OC_RSRVD_HARDWARE_VERSION) - 1, payload->info.hardwareVersion); // Firmware Version err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_FIRMWARE_VERSION, sizeof(OC_RSRVD_FIRMWARE_VERSION) - 1, payload->info.firmwareVersion); // Support URL err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_SUPPORT_URL, sizeof(OC_RSRVD_SUPPORT_URL) - 1, payload->info.supportUrl); // System Time err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_SYSTEM_TIME, sizeof(OC_RSRVD_SYSTEM_TIME) - 1, payload->info.systemTime); err = err | cbor_encoder_close_container(&map, &repMap); } // Close Map 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); }
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 int64_t OCConvertRepPayload(OCRepPayload *payload, uint8_t *outPayload, size_t *size) { CborEncoder encoder; int64_t err = CborNoError; cbor_encoder_init(&encoder, outPayload, *size, 0); size_t arrayCount = 0; for (OCRepPayload *temp = payload; temp; temp = temp->next) { arrayCount++; } CborEncoder rootArray; if (arrayCount > 1) { err |= cbor_encoder_create_array(&encoder, &rootArray, arrayCount); VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding rep root map"); } while (payload != NULL && (err == CborNoError)) { CborEncoder rootMap; err |= cbor_encoder_create_map(((arrayCount == 1)? &encoder: &rootArray), &rootMap, CborIndefiniteLength); VERIFY_CBOR_SUCCESS(TAG, err, "Failed creating root map"); // Only in case of collection href is included. if (arrayCount > 1 && payload->uri && strlen(payload->uri) > 0) { OIC_LOG(INFO, TAG, "Payload has uri"); err |= cbor_encode_text_string(&rootMap, OC_RSRVD_HREF, strlen(OC_RSRVD_HREF)); VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding rep href tag"); err |= cbor_encode_text_string(&rootMap, payload->uri, strlen(payload->uri)); VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding rep href value"); } if (payload->types) { OIC_LOG(INFO, TAG, "Payload has types"); err |= OCStringLLJoin(&rootMap, OC_RSRVD_RESOURCE_TYPE, payload->types); VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding resource type."); } if (payload->interfaces) { OIC_LOG(INFO, TAG, "Payload has interfaces"); err |= OCStringLLJoin(&rootMap, OC_RSRVD_INTERFACE, payload->interfaces); VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding platform interface type."); } err |= OCConvertSingleRepPayload(&rootMap, payload); VERIFY_CBOR_SUCCESS(TAG, err, "Failed setting rep payload"); // Close main array err |= cbor_encoder_close_container(((arrayCount == 1) ? &encoder: &rootArray), &rootMap); VERIFY_CBOR_SUCCESS(TAG, err, "Failed closing root map"); payload = payload->next; } if (arrayCount > 1) { err |= cbor_encoder_close_container(&encoder, &rootArray); VERIFY_CBOR_SUCCESS(TAG, err, "Failed closing root array"); } exit: return checkError(err, &encoder, outPayload, size); }
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 void ConvertJsonToCBOR(const char *jsonFileName, const char *cborFileName) { char *jsonStr = NULL; FILE *fp = NULL; FILE *fp1 = NULL; uint8_t *aclCbor = NULL; uint8_t *pstatCbor = NULL; uint8_t *doxmCbor = NULL; uint8_t *amaclCbor = NULL; uint8_t *svcCbor = NULL; uint8_t *credCbor = NULL; cJSON *jsonRoot = NULL; OCStackResult ret = OC_STACK_ERROR; size_t size = GetJSONFileSize(jsonFileName); if (0 == size) { OIC_LOG (ERROR, TAG, "Failed converting to JSON"); return; } jsonStr = (char *)OICMalloc(size + 1); VERIFY_NON_NULL(TAG, jsonStr, FATAL); fp = fopen(jsonFileName, "r"); if (fp) { size_t bytesRead = fread(jsonStr, 1, size, fp); jsonStr[bytesRead] = '\0'; OIC_LOG_V(DEBUG, TAG, "Read %zu bytes", bytesRead); fclose(fp); fp = NULL; } else { OIC_LOG (ERROR, TAG, "Unable to open JSON file!!"); goto exit; } jsonRoot = cJSON_Parse(jsonStr); cJSON *value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_ACL_NAME); //printf("ACL json : \n%s\n", cJSON_PrintUnformatted(value)); size_t aclCborSize = 0; if (NULL != value) { OicSecAcl_t *acl = JSONToAclBin(jsonStr); VERIFY_NON_NULL(TAG, acl, FATAL); ret = AclToCBORPayload(acl, &aclCbor, &aclCborSize); if(OC_STACK_OK != ret) { OIC_LOG (ERROR, TAG, "Failed converting Acl to Cbor Payload"); DeleteACLList(acl); goto exit; } printf("ACL Cbor Size: %zd\n", aclCborSize); DeleteACLList(acl); } value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_PSTAT_NAME); size_t pstatCborSize = 0; if (NULL != value) { OicSecPstat_t *pstat = JSONToPstatBin(jsonStr); VERIFY_NON_NULL(TAG, pstat, FATAL); ret = PstatToCBORPayload(pstat, &pstatCbor, &pstatCborSize); if(OC_STACK_OK != ret) { OIC_LOG (ERROR, TAG, "Failed converting Pstat to Cbor Payload"); DeletePstatBinData(pstat); goto exit; } printf("PSTAT Cbor Size: %zd\n", pstatCborSize); DeletePstatBinData(pstat); } value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_DOXM_NAME); size_t doxmCborSize = 0; if (NULL != value) { OicSecDoxm_t *doxm = JSONToDoxmBin(jsonStr); VERIFY_NON_NULL(TAG, doxm, FATAL); ret = DoxmToCBORPayload(doxm, &doxmCbor, &doxmCborSize); if(OC_STACK_OK != ret) { OIC_LOG (ERROR, TAG, "Failed converting Doxm to Cbor Payload"); DeleteDoxmBinData(doxm); goto exit; } printf("DOXM Cbor Size: %zd\n", doxmCborSize); DeleteDoxmBinData(doxm); } value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_AMACL_NAME); size_t amaclCborSize = 0; if (NULL != value) { OicSecAmacl_t *amacl = JSONToAmaclBin(jsonStr); VERIFY_NON_NULL(TAG, amacl, FATAL); ret = AmaclToCBORPayload(amacl, &amaclCbor, &amaclCborSize); if(OC_STACK_OK != ret) { OIC_LOG (ERROR, TAG, "Failed converting Amacl to Cbor Payload"); DeleteAmaclList(amacl); goto exit; } printf("AMACL Cbor Size: %zd\n", amaclCborSize); DeleteAmaclList(amacl); } value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_SVC_NAME); size_t svcCborSize = 0; if (NULL != value) { OicSecSvc_t *svc = JSONToSvcBin(jsonStr); VERIFY_NON_NULL(TAG, svc, FATAL); ret = SVCToCBORPayload(svc, &svcCbor, &svcCborSize); if(OC_STACK_OK != ret) { OIC_LOG (ERROR, TAG, "Failed converting Svc to Cbor Payload"); DeleteSVCList(svc); goto exit; } printf("SVC Cbor Size: %zd\n", svcCborSize); DeleteSVCList(svc); } value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_CRED_NAME); //printf("CRED json : \n%s\n", cJSON_PrintUnformatted(value)); size_t credCborSize = 0; int secureFlag = 0; if (NULL != value) { OicSecCred_t *cred = JSONToCredBin(jsonStr); VERIFY_NON_NULL(TAG, cred, FATAL); ret = CredToCBORPayload(cred, &credCbor, &credCborSize, secureFlag); if(OC_STACK_OK != ret) { OIC_LOG (ERROR, TAG, "Failed converting Cred to Cbor Payload"); DeleteCredList(cred); goto exit; } printf("CRED Cbor Size: %zd\n", credCborSize); DeleteCredList(cred); } CborEncoder encoder; size_t cborSize = aclCborSize + pstatCborSize + doxmCborSize + svcCborSize + credCborSize + amaclCborSize; printf("Total Cbor Size : %zd\n", cborSize); cborSize += 255; // buffer margin for adding map and byte string uint8_t *outPayload = (uint8_t *)OICCalloc(1, cborSize); VERIFY_NON_NULL(TAG, outPayload, ERROR); cbor_encoder_init(&encoder, outPayload, cborSize, 0); CborEncoder map; CborError cborEncoderResult = cbor_encoder_create_map(&encoder, &map, CborIndefiniteLength); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Creating Main Map."); if (aclCborSize > 0) { cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_ACL_NAME, strlen(OIC_JSON_ACL_NAME)); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ACL Name."); cborEncoderResult = cbor_encode_byte_string(&map, aclCbor, aclCborSize); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ACL Value."); } if (pstatCborSize > 0) { cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_PSTAT_NAME, strlen(OIC_JSON_PSTAT_NAME)); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PSTAT Name."); cborEncoderResult = cbor_encode_byte_string(&map, pstatCbor, pstatCborSize); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PSTAT Value."); } if (doxmCborSize > 0) { cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_DOXM_NAME, strlen(OIC_JSON_DOXM_NAME)); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding DOXM Name."); cborEncoderResult = cbor_encode_byte_string(&map, doxmCbor, doxmCborSize); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding DOXM Value."); } if (amaclCborSize > 0) { cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_AMACL_NAME, strlen(OIC_JSON_AMACL_NAME)); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding AMACL Name."); cborEncoderResult = cbor_encode_byte_string(&map, amaclCbor, amaclCborSize); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding AMACL Value."); } if (svcCborSize > 0) { cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_SVC_NAME, strlen(OIC_JSON_SVC_NAME)); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding SVC Name."); cborEncoderResult = cbor_encode_byte_string(&map, svcCbor, svcCborSize); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding SVC Value."); } if (credCborSize > 0) { cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_CRED_NAME, strlen(OIC_JSON_CRED_NAME)); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding CRED Name."); cborEncoderResult = cbor_encode_byte_string(&map, credCbor, credCborSize); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding CRED Value."); } cborEncoderResult = cbor_encoder_close_container(&encoder, &map); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing Container."); size_t s = encoder.ptr - outPayload; OIC_LOG_V(DEBUG, TAG, "Payload size %zu", s); fp1 = fopen(cborFileName, "w"); if (fp1) { size_t bytesWritten = fwrite(outPayload, 1, s, fp1); if (bytesWritten == s) { OIC_LOG_V(DEBUG, TAG, "Written %zu bytes", bytesWritten); } else { OIC_LOG_V(ERROR, TAG, "Failed writing %zu bytes", s); } fclose(fp1); fp1 = NULL; } exit: cJSON_Delete(jsonRoot); OICFree(aclCbor); OICFree(doxmCbor); OICFree(pstatCbor); OICFree(amaclCbor); OICFree(svcCbor); OICFree(credCbor); OICFree(jsonStr); return ; }
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; }