void things_set_byte_value(struct things_representation_s *rep, char *key, uint8_t *value, size_t size) { OCByteString b_val = { value, size }; if (b_val.len < 1) { THINGS_LOG_E(TAG, "No byte value to Set"); } else { OCRepPayloadSetPropByteString(rep->payload, key, b_val); } }
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 int _icd_state_value_from_gvariant(OCRepPayload *repr, GVariantIter *iter) { int ret; char *key; GVariant *var; const char *str_value; OCRepPayload *repr_value; struct icd_state_list_s value_list = {0}; while (g_variant_iter_loop(iter, "{sv}", &key, &var)) { if (g_variant_is_of_type(var, G_VARIANT_TYPE_BOOLEAN)) { OCRepPayloadSetPropBool(repr, key, g_variant_get_boolean(var)); } else if (g_variant_is_of_type(var, G_VARIANT_TYPE_INT32)) { OCRepPayloadSetPropInt(repr, key, g_variant_get_int32(var)); } else if (g_variant_is_of_type(var, G_VARIANT_TYPE_DOUBLE)) { OCRepPayloadSetPropDouble(repr, key, g_variant_get_double(var)); } else if (g_variant_is_of_type(var, G_VARIANT_TYPE_STRING)) { str_value = g_variant_get_string(var, NULL); if (NULL == str_value) { ERR("g_variant_get_string() Fail"); _icd_payload_state_list_destroy(&value_list); return IOTCON_ERROR_OUT_OF_MEMORY; } if (IC_STR_EQUAL == strcmp(IC_STR_NULL, str_value)) OCRepPayloadSetNull(repr, key); else OCRepPayloadSetPropString(repr, key, str_value); } else if (g_variant_is_of_type(var, G_VARIANT_TYPE("ay"))) { OCByteString byte_value; byte_value.bytes = (uint8_t*)g_variant_get_data(var); byte_value.len = g_variant_get_size(var); OCRepPayloadSetPropByteString(repr, key, byte_value); } else if (g_variant_is_of_type(var, G_VARIANT_TYPE("a{sv}"))) { GVariantIter state_iter; repr_value = OCRepPayloadCreate(); g_variant_iter_init(&state_iter, var); ret = _icd_state_value_from_gvariant(repr_value, &state_iter); if (IOTCON_ERROR_NONE != ret) { ERR("_icd_state_value_from_gvariant() Fail(%d)", ret); _icd_payload_state_list_destroy(&value_list); OCRepPayloadDestroy(repr_value); return ret; } OCRepPayloadSetPropObjectAsOwner(repr, key, repr_value); } else if (g_variant_is_of_type(var, G_VARIANT_TYPE_ARRAY)) { memset(&value_list, 0, sizeof(struct icd_state_list_s)); ret = _icd_state_list_from_gvariant(var, &value_list, 0); if (IOTCON_ERROR_NONE != ret) { ERR("_icd_state_list_from_gvariant() Fail(%d)", ret); _icd_payload_state_list_destroy(&value_list); return ret; } ret = _icd_state_array_from_list(repr, &value_list, key); if (IOTCON_ERROR_NONE != ret) { ERR("_icd_state_array_from_list() Fail(%d)", ret); _icd_payload_state_list_destroy(&value_list); return ret; } } else { ERR("Invalid type(%s)", g_variant_get_type_string(var)); return IOTCON_ERROR_INVALID_TYPE; } } return IOTCON_ERROR_NONE; }
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; }