rep->setValue(key, value);
}
+/*
+* Class: org_iotivity_base_OcRepresentation
+* Method: setValueByteArray
+* Signature: (Ljava/lang/String;[B)V
+*/
+JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setValueByteArray
+(JNIEnv *env, jobject thiz, jstring jKey, jbyteArray jValue)
+{
+ LOGD("OcRepresentation_setValueByteArray");
+ if (!jKey)
+ {
+ ThrowOcException(OC_STACK_INVALID_PARAM, "key cannot be null");
+ return;
+ }
+
+ const jsize len = env->GetArrayLength(jValue);
+ jbyte* bytes = env->GetByteArrayElements(jValue, nullptr);
+
+ std::vector<uint8_t> value;
+ for (jsize i = 0; i < len; ++i)
+ {
+ value.push_back(static_cast<uint8_t>(bytes[i]));
+ }
+ env->ReleaseByteArrayElements(jValue, bytes, JNI_ABORT);
+
+ OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
+ if (!rep) return;
+
+ std::string key = env->GetStringUTFChars(jKey, nullptr);
+ rep->setValue(key, value);
+}
+
/*
* Class: org_iotivity_base_OcRepresentation
* Method: addChild
}
return repArr;
}
+ jobject operator()(const std::vector<uint8_t>& val) const
+ {
+ size_t len = val.size();
+ jbyteArray jByteArray = env->NewByteArray(len);
+ if (!jByteArray) return nullptr;
+ const uint8_t* bytes = &val[0];
+ env->SetByteArrayRegion(jByteArray, 0, len, reinterpret_cast<const jbyte*>(bytes));
+ return jByteArray;
+ }
// Nested sequences:
jobject operator()(const std::vector<std::vector<int>>& val) const
JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setValueRepresentation3DArray
(JNIEnv *, jobject, jstring, jobjectArray);
+ /*
+ * Class: org_iotivity_base_OcRepresentation
+ * Method: setValueByteArray
+ * Signature: (Ljava/lang/String;[B)V
+ */
+ JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setValueByteArray
+ (JNIEnv *, jobject, jstring, jbyteArray);
+
/*
* Class: org_iotivity_base_OcRepresentation
* Method: addChild
rep.setValue(representationK, representationArrVEmpty);
OcRepresentation[] representationArrVEmptyA = rep.getValue(representationK);
assertEquals(representationArrVEmpty.length, representationArrVEmptyA.length);
+
+ //byte
+ String byteK = "byteK";
+ byte[] byteArrV = {1, 2, 3, 4, 0, 5, 6, 7};
+ rep.setValue(byteK, byteArrV);
+ byte[] byteArrVa = rep.getValue(byteK);
+ assertTrue(Arrays.equals(byteArrV, byteArrVa));
+
+ byte[] byteArrVEmpty = {};
+ rep.setValue(byteK, byteArrVEmpty);
+ byte[] byteArrVEmptyA = rep.getValue(byteK);
+ assertTrue(Arrays.equals(byteArrVEmpty, byteArrVEmptyA));
}
public void testAttributeAccessBy2DType() throws OcException {
this.setValueRepresentation3DArray(key, value);
}
+ public void setValue(String key, byte[] value) throws OcException {
+ this.setValueByteArray(key, value);
+ }
+
private native void setValueInteger(String key, int value) throws OcException;
private native void setValueDouble(String key, double value) throws OcException;
private native void setValueRepresentation3DArray(String key, OcRepresentation[][][] value) throws OcException;
+ private native void setValueByteArray(String key, byte[] value) throws OcException;
+
/**
* @deprecated use {@link #getValue(String key)} instead.
*/
std::vector<std::vector<std::vector<std::string>>>,
std::vector<std::vector<OC::OCRepresentation>>,
- std::vector<std::vector<std::vector<OC::OCRepresentation>>>
+ std::vector<std::vector<std::vector<OC::OCRepresentation>>>,
+
+ // used for binary data type
+ std::vector<uint8_t>
> AttributeValue;
enum class AttributeType
Boolean,
String,
OCRepresentation,
- Vector
+ Vector,
+ Binary
};
template<typename T>
constexpr static AttributeType type = AttributeType::OCRepresentation;
};
+ template<>
+ struct AttributeTypeConvert<std::vector<uint8_t>>
+ {
+ constexpr static AttributeType type = AttributeType::Binary;
+ };
+
std::ostream& operator << (std::ostream& os, const AttributeType at);
}
#endif // OC_ATTRIBUTEVALUE_H_
m_values[str] = val;
}
+ // using R-value(or universal ref depending) to move string and vector<uint8_t>
+ template <typename T>
+ void setValue(const std::string& str, T&& val)
+ {
+ m_values[str] = std::forward<T>(val);
+ }
+
/**
* Retrieve the attribute value associated with the supplied name
*
case AttributeType::Vector:
getPayloadArray(root, val);
break;
+ case AttributeType::Binary:
+ OCRepPayloadSetPropByteString(root, val.attrname().c_str(),
+ OCByteString{const_cast<uint8_t*>(val.getValue<std::vector<uint8_t>>().data()),
+ val.getValue<std::vector<uint8_t>>().size()});
+ break;
default:
throw std::logic_error(std::string("Getpayload: Not Implemented") +
std::to_string((int)val.type()));
case OCREP_PROP_ARRAY:
setPayloadArray(val);
break;
+ case OCREP_PROP_BYTE_STRING:
+ setValue(val->name,
+ std::vector<uint8_t>
+ (val->ocByteStr.bytes, val->ocByteStr.bytes + val->ocByteStr.len)
+ );
+ break;
default:
throw std::logic_error(std::string("Not Implemented!") +
std::to_string((int)val->type));
case AttributeType::Vector:
os << "Vector";
break;
+ case AttributeType::Binary:
+ os<< "Binary";
}
return os;
}
};
template<typename T>
- struct type_info<T, typename std::enable_if<is_vector<T>::value>::type>
+ struct type_info<
+ T,
+ typename std::enable_if<
+ is_vector<T>::value &&
+ !std::is_same<uint8_t, typename T::value_type>::value
+ >::type
+ >
{
typedef T type;
typedef typename type_info<typename T::value_type>::base_type base_type;
type_info<typename T::value_type>::depth;
};
+ // special case for binary data, which is a std::vector<uint8_t>
+ template<>
+ struct type_info<std::vector<uint8_t>, void>
+ {
+ typedef std::vector<uint8_t> type;
+ typedef std::vector<uint8_t> base_type;
+ constexpr static AttributeType enum_type = AttributeType::Binary;
+ constexpr static AttributeType enum_base_type = AttributeType::Binary;
+ constexpr static size_t depth = 0;
+ };
+
+
struct type_introspection_visitor : boost::static_visitor<>
{
AttributeType type;
subRep.setValue("DoubleAttr", 3.333);
subRep.setValue("BoolAttr", true);
subRep.setValue("StringAttr", std::string("String attr"));
+ std::vector<uint8_t> bin_data {5,3,4,5,6,0,34,2,4,5,6,3};
+ subRep.setValue("BinaryAttr", bin_data);
startRep.setValue("Sub", subRep);
OC::MessageContainer mc1;
EXPECT_EQ(3.333, newSubRep.getValue<double>("DoubleAttr"));
EXPECT_EQ(true, newSubRep.getValue<bool>("BoolAttr"));
EXPECT_STREQ("String attr", newSubRep.getValue<std::string>("StringAttr").c_str());
+ EXPECT_EQ(bin_data,
+ newSubRep.getValue<std::vector<uint8_t>>("BinaryAttr"));
OCPayloadDestroy(cparsed);
}