JNIEXPORT void JNICALL Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_addAttributeString (JNIEnv *env, jobject jobject, jstring jKey, jstring jValue) { if (!jKey) { throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid Attribute name!"); return; } SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, jobject); if (!resource) { throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!"); return; } std::string key = env->GetStringUTFChars(jKey, NULL); std::string value = env->GetStringUTFChars(jValue, NULL); SimulatorResourceModel::Attribute att; att.setName(key); att.setValue(value); resource->addAttribute(att); }
bool simulatorResourceAttributeToCpp(JNIEnv *env, jobject jAttribute, SimulatorResourceModel::Attribute &attribute) { if (!jAttribute) return false; static jfieldID nameFID = env->GetFieldID(gSimulatorClassRefs.simulatorResourceAttributeCls, "mName", "Ljava/lang/String;"); static jfieldID valueFID = env->GetFieldID(gSimulatorClassRefs.simulatorResourceAttributeCls, "mValue", "Lorg/oic/simulator/AttributeValue;"); static jfieldID propertyFID = env->GetFieldID(gSimulatorClassRefs.simulatorResourceAttributeCls, "mProperty", "Lorg/oic/simulator/AttributeProperty;"); jstring jAttributeName = (jstring) env->GetObjectField(jAttribute, nameFID); jobject jAttributeValue = env->GetObjectField(jAttribute, valueFID); jobject jAttributeProperty = env->GetObjectField(jAttribute, propertyFID); if (!jAttributeName || !jAttributeValue) return false; JniString attrName(env, jAttributeName); SimulatorResourceModel::ValueVariant value = JniAttributeValue::toCpp(env, jAttributeValue); attribute.setName(attrName.get()); attribute.setValue(value); if (jAttributeProperty) { SimulatorResourceModel::AttributeProperty property = JniAttributeProperty::toCpp(env, jAttributeProperty); attribute.setProperty(property); } return true; }
jobject simulatorResourceAttributeToJava(JNIEnv *env, SimulatorResourceModel::Attribute &attribute) { static jmethodID simulatorResAttributeCtor = env->GetMethodID( gSimulatorClassRefs.simulatorResourceAttributeCls, "<init>", "(Ljava/lang/String;Lorg/oic/simulator/AttributeValue;Lorg/oic/simulator/AttributeProperty;)V"); jstring jAttrName = env->NewStringUTF(attribute.getName().c_str()); jobject jAttributeValue = JniAttributeValue::toJava(env, attribute); jobject jAttributeProperty = JniAttributeProperty::toJava(env, attribute.getProperty()); return env->NewObject(gSimulatorClassRefs.simulatorResourceAttributeCls, simulatorResAttributeCtor, jAttrName, jAttributeValue, jAttributeProperty); }
SimulatorResult ResponseModel::verifyResponse(const OC::OCRepresentation &rep) { for (auto & ocAttribute : rep) { SimulatorResourceModel::Attribute attribute; if (false == m_repSchema->getAttribute(ocAttribute.attrname(), attribute)) { return SIMULATOR_UKNOWN_PROPERTY; } switch (attribute.getValueType()) { case SimulatorResourceModel::Attribute::ValueType::INTEGER : // Integer { SimulatorResult result = validateAttributeInteger(attribute, ocAttribute); if (SIMULATOR_OK != result) { return result; } } break; case SimulatorResourceModel::Attribute::ValueType::DOUBLE : // Double { SimulatorResult result = validateAttributeDouble(attribute, ocAttribute); if (SIMULATOR_OK != result) { return result; } } break; case SimulatorResourceModel::Attribute::ValueType::STRING : // String { SimulatorResult result = validateAttributeString(attribute, ocAttribute); if (SIMULATOR_OK != result) { return result; } } break; } } return SIMULATOR_OK; }
SimulatorResult ResponseModel::validateAttributeInteger( SimulatorResourceModel::Attribute &attrSchema, const OC::OCRepresentation::AttributeItem &ocAttribute) { // Check the value type if (OC::AttributeType::Integer != ocAttribute.type()) { return SIMULATOR_TYPE_MISMATCH; } // Check value if it is in range int min, max, value; attrSchema.getRange(min, max); value = ocAttribute.getValue<int>(); if (value < min || value > max) { return SIMULATOR_BAD_VALUE; } return SIMULATOR_OK; }
static jobject toJava(JNIEnv *env, SimulatorResourceModel::Attribute &attribute) { auto value = attribute.getValue(); return toJava(env, value); }