bool things_get_byte_value(struct things_representation_s *rep, char *key, uint8_t **value, size_t *size) { OCByteString b_val = { NULL, 0 }; bool ret = OCRepPayloadGetPropByteString((OCRepPayload *) rep->payload, key, &b_val); if (true == ret) { (*size) = b_val.len; (*value) = (uint8_t *) things_malloc((*size) + 1); memcpy((*value), b_val.bytes, b_val.len); if (b_val.bytes != NULL) { things_free(b_val.bytes); b_val.bytes = NULL; } } return ret; }
TEST_F(CborByteStringTest, ByteStringConvertParseTest) { OCRepPayloadSetUri(payload_in, "/a/quake_sensor"); OCRepPayloadSetPropInt(payload_in, "scale", 4); uint8_t binval[] = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0x0, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF}; OCByteString quakedata_in = { binval, sizeof(binval)}; // Set ByteString in Payload EXPECT_EQ(true, OCRepPayloadSetPropByteString(payload_in, "quakedata", quakedata_in)); // Convert OCPayload to CBOR uint8_t *payload_cbor = NULL; size_t payload_cbor_size = 0; EXPECT_EQ(OC_STACK_OK, OCConvertPayload((OCPayload*) payload_in, &payload_cbor, &payload_cbor_size)); #ifdef CBOR_BIN_STRING_DEBUG FILE *fp = fopen("binstring.cbor", "wb+"); if (fp) { fwrite(payload_cbor, 1, payload_cbor_size, fp); fclose(fp); } #endif //CBOR_BIN_STRING_DEBUG // Parse CBOR back to OCPayload OCPayload* payload_out = NULL; EXPECT_EQ(OC_STACK_OK, OCParsePayload(&payload_out, PAYLOAD_TYPE_REPRESENTATION, payload_cbor, payload_cbor_size)); OCByteString quakedata_out = {NULL, 0}; ASSERT_EQ(true, OCRepPayloadGetPropByteString((OCRepPayload*)payload_out, "quakedata", &quakedata_out)); // Compare input and output data EXPECT_EQ(quakedata_in.len, quakedata_out.len); EXPECT_EQ(0, memcmp(quakedata_in.bytes, quakedata_out.bytes, quakedata_in.len)); // Cleanup OICFree(payload_cbor); OICFree(quakedata_out.bytes); OCPayloadDestroy((OCPayload*)payload_out); }
TEST_F(CborByteStringTest, ByteStringSetGetTest) { OCRepPayloadSetUri(payload_in, "/a/quake_sensor"); OCRepPayloadSetPropInt(payload_in, "scale", 4); uint8_t binval[] = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0x0, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF}; OCByteString quakedata_in = { binval, sizeof(binval)}; EXPECT_EQ(true, OCRepPayloadSetPropByteString(payload_in, "quakedata", quakedata_in)); OCByteString quakedata_out = { NULL, 0}; ASSERT_EQ(true, OCRepPayloadGetPropByteString(payload_in, "quakedata", &quakedata_out)); EXPECT_EQ(quakedata_in.len, quakedata_out.len); EXPECT_EQ(0, memcmp(quakedata_in.bytes, quakedata_out.bytes, quakedata_in.len)); // Cleanup OICFree(quakedata_out.bytes); }
static bool add_property_in_post_req_msg(st_things_set_request_message_s *req_msg, OCRepPayload *req_payload, things_attribute_info_s *prop) { RET_FALSE_IF_PARAM_IS_NULL(TAG, req_msg); RET_FALSE_IF_PARAM_IS_NULL(TAG, req_msg->rep); RET_FALSE_IF_PARAM_IS_NULL(TAG, req_msg->rep->payload); RET_FALSE_IF_PARAM_IS_NULL(TAG, req_payload); RET_FALSE_IF_PARAM_IS_NULL(TAG, prop); OCRepPayload *resp_payload = req_msg->rep->payload; THINGS_LOG_D(TAG, "Property Key is %s", prop->key); THINGS_LOG_D(TAG, "Property type is %d", prop->type); // Based on the property type, call appropriate methods to copy // the property from request payload to request representation. bool result = false; switch (prop->type) { case BOOL_ID: { bool value = false; if (OCRepPayloadGetPropBool(req_payload, prop->key, &value)) { result = OCRepPayloadSetPropBool(resp_payload, prop->key, value); if (!result) { THINGS_LOG_E(TAG, "Failed to set the boolean value of '%s' in request message.", prop->key); } } else { THINGS_LOG_E(TAG, "Failed to get the boolean value of '%s' for request message.", prop->key); } } break; case INT_ID: { int64_t value = 0; if (OCRepPayloadGetPropInt(req_payload, prop->key, &value)) { result = OCRepPayloadSetPropInt(resp_payload, prop->key, value); if (!result) { THINGS_LOG_E(TAG, "Failed to set the integer value of '%s' in request message", prop->key); } } else { THINGS_LOG_E(TAG, "Failed to get the integer value of '%s' for request message", prop->key); } } break; case DOUBLE_ID: { double value = 0.0; if (OCRepPayloadGetPropDouble(req_payload, prop->key, &value)) { result = OCRepPayloadSetPropDouble(resp_payload, prop->key, value); if (!result) { THINGS_LOG_E(TAG, "Failed to set the double value of '%s' in request message", prop->key); } } else { THINGS_LOG_E(TAG, "Failed to get the double value of '%s' for request message", prop->key); } } break; case STRING_ID: { char *value = NULL; if (OCRepPayloadGetPropString(req_payload, prop->key, &value)) { result = OCRepPayloadSetPropString(resp_payload, prop->key, value); if (!result) { THINGS_LOG_E(TAG, "Failed to set the string value of '%s' in request message", prop->key); } things_free(value); } else { THINGS_LOG_E(TAG, "Failed to get the string value of '%s' for request message", prop->key); } } break; case OBJECT_ID: { OCRepPayload *value = NULL; if (OCRepPayloadGetPropObject(req_payload, prop->key, &value)) { result = OCRepPayloadSetPropObject(resp_payload, prop->key, value); if (!result) { THINGS_LOG_E(TAG, "Failed to set the object value of '%s' in request message", prop->key); } OCRepPayloadDestroy(value); } else { THINGS_LOG_E(TAG, "Failed to get the object value of '%s' for request message", prop->key); } } break; case BYTE_ID: { OCByteString byte_value; if (OCRepPayloadGetPropByteString(req_payload, prop->key, &byte_value)) { result = OCRepPayloadSetPropByteString(resp_payload, prop->key, byte_value); if (!result) { THINGS_LOG_E(TAG, "Failed to set the byte string value of '%s' in request message", prop->key); } things_free(byte_value.bytes); } else { THINGS_LOG_E(TAG, "Failed to get the byte string value of '%s' for request message", prop->key); } } break; case INT_ARRAY_ID: { int64_t *value = NULL; size_t dimensions[MAX_REP_ARRAY_DEPTH] = { 0 }; if (OCRepPayloadGetIntArray(req_payload, prop->key, &value, dimensions)) { result = OCRepPayloadSetIntArray(resp_payload, prop->key, value, dimensions); if (!result) { THINGS_LOG_E(TAG, "Failed to set the integer array value of '%s' in request message", prop->key); } things_free(value); } else { THINGS_LOG_E(TAG, "Failed to get the integer array value of '%s' for request message", prop->key); } } break; case DOUBLE_ARRAY_ID: { double *value = NULL; size_t dimensions[MAX_REP_ARRAY_DEPTH] = { 0 }; if (OCRepPayloadGetDoubleArray(req_payload, prop->key, &value, dimensions)) { result = OCRepPayloadSetDoubleArray(resp_payload, prop->key, value, dimensions); if (!result) { THINGS_LOG_E(TAG, "Failed to set the double array value of '%s' in request message", prop->key); } things_free(value); } else { THINGS_LOG_E(TAG, "Failed to get the double array value of '%s' for request message", prop->key); } } break; case STRING_ARRAY_ID: { char **value = NULL; size_t dimensions[MAX_REP_ARRAY_DEPTH] = { 0 }; if (OCRepPayloadGetStringArray(req_payload, prop->key, &value, dimensions)) { result = OCRepPayloadSetStringArray(resp_payload, prop->key, value, dimensions); if (!result) { THINGS_LOG_E(TAG, "Failed to set the string array value of '%s' in request message", prop->key); } size_t len = calcDimTotal(dimensions); things_free_str_array(value, len); } else { THINGS_LOG_E(TAG, "Failed to get the string array value of '%s' for request message", prop->key); } } break; case OBJECT_ARRAY_ID: { OCRepPayload **value = NULL; size_t dimensions[MAX_REP_ARRAY_DEPTH] = { 0 }; if (OCRepPayloadGetPropObjectArray(req_payload, prop->key, &value, dimensions)) { result = OCRepPayloadSetPropObjectArray(resp_payload, prop->key, value, dimensions); if (!result) { THINGS_LOG_E(TAG, "Failed to set the object array value of '%s' in request message", prop->key); } size_t len = calcDimTotal(dimensions); for (size_t index = 0; index < len; index++) { OCRepPayloadDestroy(value[index]); } things_free(value); } else { THINGS_LOG_E(TAG, "Failed to get the object array value of '%s' for request message", prop->key); } } break; default: THINGS_LOG_E(TAG, "Invalid property type (%d).", prop->type); break; } return result; }