1 /******************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
21 #include "simulator_resource_jni.h"
22 #include "simulator_resource_jni_util.h"
23 #include "simulator_common_jni.h"
24 #include "simulator_resource_model_jni.h"
26 extern SimulatorClassRefs gSimulatorClassRefs;
28 JniSimulatorResource::JniSimulatorResource(SimulatorResourcePtr &resource)
29 : m_sharedResource(resource) {}
31 SimulatorResourcePtr JniSimulatorResource::getJniSimulatorResourcePtr(JNIEnv *env, jobject thiz)
33 JniSimulatorResource *resource = GetHandle<JniSimulatorResource>(env, thiz);
34 if (env->ExceptionCheck())
38 return resource->m_sharedResource;
41 jobject JniSimulatorResource::toJava(JNIEnv *env, jlong resource)
43 jobject resourceObj = (jobject) env->NewObject(gSimulatorClassRefs.classSimulatorResource,
44 gSimulatorClassRefs.classSimulatorResourceCtor, resource);
45 if (NULL == resourceObj)
52 void JniSimulatorResource::setUri(JNIEnv *env, jobject jobj, const std::string &uri)
57 jstring jURI = env->NewStringUTF(uri.c_str());
61 env->CallVoidMethod(jobj, gSimulatorClassRefs.classSimulatorResourceSetURI, jURI);
62 env->DeleteLocalRef(jURI);
65 void JniSimulatorResource::setResourceType(JNIEnv *env, jobject jobj,
66 const std::string &resourceType)
71 jstring jResourceType = env->NewStringUTF(resourceType.c_str());
75 env->CallVoidMethod(jobj, gSimulatorClassRefs.classSimulatorResourceSetResourceType, jResourceType);
76 env->DeleteLocalRef(jResourceType);
79 void JniSimulatorResource::setResourceName(JNIEnv *env, jobject jobj, const std::string &name)
84 jstring jName = env->NewStringUTF(name.c_str());
88 env->CallVoidMethod(jobj, gSimulatorClassRefs.classSimulatorResourceSetName, jName);
89 env->DeleteLocalRef(jName);
92 JNIEXPORT jobject JNICALL
93 Java_org_iotivity_simulator_SimulatorResourceServer_getModel
94 (JNIEnv *env, jobject object)
96 SimulatorResourcePtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, object);
97 if (nullptr == resource.get())
99 std::cout << "getModel: Resource is NULL";
103 SimulatorResourceModel resModel = resource->getModel();
104 JniSimulatorResourceModel *model = new JniSimulatorResourceModel(resModel);
105 jobject jModel = JniSimulatorResourceModel::toJava(env, reinterpret_cast<jlong>(model));
109 JNIEXPORT void JNICALL
110 Java_org_iotivity_simulator_SimulatorResourceServer_updateAttributeFromAllowedValues
111 (JNIEnv *env, jobject object, jstring attrName, jint index)
113 SimulatorResourcePtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, object);
114 if (nullptr == resource.get())
116 std::cout << "updateAttributeFromAllowedValues: Resource is NULL";
120 const char *attrNamePtr = env->GetStringUTFChars(attrName, NULL);
123 std::cout << "updateAttributeFromAllowedValues: Failed to convert jstring to char string!";
127 resource->updateAttributeFromAllowedValues(attrNamePtr, static_cast<int>(index));
128 env->ReleaseStringUTFChars(attrName, attrNamePtr);
131 JNIEXPORT void JNICALL
132 Java_org_iotivity_simulator_SimulatorResourceServer_setRange
133 (JNIEnv *env, jobject object, jstring attrName, jint min, jint max)
135 SimulatorResourcePtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, object);
136 if (nullptr == resource.get())
138 std::cout << "setRange: Resource is NULL";
142 const char *attrNamePtr = env->GetStringUTFChars(attrName, NULL);
145 std::cout << "setRange: Failed to convert jstring to char string!";
149 resource->setRange(attrNamePtr, static_cast<int>(min), static_cast<int>(max));
150 env->ReleaseStringUTFChars(attrName, attrNamePtr);
153 JNIEXPORT void JNICALL
154 Java_org_iotivity_simulator_SimulatorResourceServer_setInterfaceType
155 (JNIEnv *env, jobject jobject, const std::string &interfaceType)
157 jstring jInterfaceType = env->NewStringUTF(interfaceType.c_str());
160 std::cout << "setInterfaceType: InterfaceType is NULL";
164 env->CallVoidMethod(jobject, gSimulatorClassRefs.classSimulatorResourceSetInterfaceType,
166 env->DeleteLocalRef(jInterfaceType);
169 JNIEXPORT void JNICALL Java_org_iotivity_simulator_SimulatorResourceServer_addAttributeInteger
170 (JNIEnv *env, jobject jobject, jstring jKey, jint jValue)
174 std::cout << "addAttributeInteger: AttributeName is Empty";
178 SimulatorResourcePtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, jobject);
179 if (nullptr == resource.get())
181 std::cout << "addAttributeInteger: Resource is NULL";
185 std::string str = env->GetStringUTFChars(jKey, NULL);
186 resource->addAttribute(str, static_cast<int>(jValue));
189 JNIEXPORT void JNICALL Java_org_iotivity_simulator_SimulatorResourceServer_addAttributeDouble
190 (JNIEnv *env, jobject jobject, jstring jKey, jdouble jValue)
194 std::cout << "addAttributeDouble: AttributeName is Empty";
198 SimulatorResourcePtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, jobject);
199 if (nullptr == resource.get())
201 std::cout << "addAttributeDouble: Resource is NULL";
205 std::string str = env->GetStringUTFChars(jKey, NULL);
206 resource->addAttribute(str, static_cast<double>(jValue));
209 JNIEXPORT void JNICALL Java_org_iotivity_simulator_SimulatorResourceServer_addAttributeBoolean
210 (JNIEnv *env, jobject jobject, jstring jKey, jboolean jValue)
214 std::cout << "addAttributeBoolean: AttributeName is Empty";
218 SimulatorResourcePtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, jobject);
219 if (nullptr == resource.get())
221 std::cout << "addAttributeBoolean: Resource is NULL";
225 std::string str = env->GetStringUTFChars(jKey, NULL);
226 resource->addAttribute(str, static_cast<bool>(jValue));
229 JNIEXPORT void JNICALL Java_org_iotivity_simulator_SimulatorResourceServer_addAttributeStringN
230 (JNIEnv *env, jobject jobject, jstring jKey, jstring jValue)
234 std::cout << "addAttributeStringN: AttributeName is Empty";
238 SimulatorResourcePtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, jobject);
239 if (nullptr == resource.get())
241 std::cout << "addAttributeStringN: Resource is NULL";
245 std::string key = env->GetStringUTFChars(jKey, NULL);
246 std::string value = env->GetStringUTFChars(jValue, NULL);
248 resource->addAttribute(key, value);
251 JNIEXPORT void JNICALL Java_org_iotivity_simulator_SimulatorResourceServer_updateAttributeInteger
252 (JNIEnv *env, jobject jobject, jstring jKey, jint jValue)
256 std::cout << "updateAttributeInteger: AttributeName is Empty";
260 SimulatorResourcePtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, jobject);
261 if (nullptr == resource.get())
263 std::cout << "updateAttributeInteger: Resource is NULL";
267 std::string str = env->GetStringUTFChars(jKey, NULL);
268 resource->updateAttribute(str, static_cast<int>(jValue));
271 JNIEXPORT void JNICALL Java_org_iotivity_simulator_SimulatorResourceServer_updateAttributeDouble
272 (JNIEnv *env, jobject jobject, jstring jKey, jdouble jValue)
276 std::cout << "updateAttributeDouble: AttributeName is Empty";
280 SimulatorResourcePtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, jobject);
281 if (nullptr == resource.get())
283 std::cout << "updateAttributeDouble: Resource is NULL";
287 std::string str = env->GetStringUTFChars(jKey, NULL);
288 resource->updateAttribute(str, static_cast<double>(jValue));
291 JNIEXPORT void JNICALL Java_org_iotivity_simulator_SimulatorResourceServer_updateAttributeBoolean
292 (JNIEnv *env, jobject jobject, jstring jKey, jboolean jValue)
296 std::cout << "updateAttributeBoolean: AttributeName is Empty";
300 SimulatorResourcePtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, jobject);
301 if (nullptr == resource.get())
303 std::cout << "updateAttributeBoolean: Resource is NULL";
307 std::string str = env->GetStringUTFChars(jKey, NULL);
308 resource->updateAttribute(str, static_cast<bool>(jValue));
311 JNIEXPORT void JNICALL Java_org_iotivity_simulator_SimulatorResourceServer_updateAttributeStringN
312 (JNIEnv *env, jobject jobject, jstring jKey, jstring jValue)
316 std::cout << "updateAttributeStringN: AttributeName is Empty";
320 SimulatorResourcePtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, jobject);
321 if (nullptr == resource.get())
323 std::cout << "updateAttributeStringN: Resource is NULL";
327 std::string key = env->GetStringUTFChars(jKey, NULL);
328 std::string value = env->GetStringUTFChars(jValue, NULL);
330 resource->updateAttribute(key, value);
333 JNIEXPORT void JNICALL Java_org_iotivity_simulator_SimulatorResourceServer_setAllowedValuesInteger
334 (JNIEnv *env, jobject object, jstring jKey, jobject jAllowedValues)
338 std::cout << "setAllowedValuesInteger: AttributeName is Empty";
342 SimulatorResourcePtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, object);
343 if (nullptr == resource.get())
345 std::cout << "setAllowedValuesInteger: Resource is NULL";
349 std::string str = env->GetStringUTFChars(jKey, NULL);
350 resource->setAllowedValues(str, convertIntegerVector(env, jAllowedValues));
353 JNIEXPORT void JNICALL Java_org_iotivity_simulator_SimulatorResourceServer_setAllowedValuesDouble
354 (JNIEnv *env, jobject object, jstring jKey, jobject jAllowedValues)
358 std::cout << "setAllowedValuesDouble: AttributeName is Empty";
362 SimulatorResourcePtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, object);
363 if (nullptr == resource.get())
365 std::cout << "setAllowedValuesDouble: Resource is NULL";
369 std::string str = env->GetStringUTFChars(jKey, NULL);
370 resource->setAllowedValues(str, convertDoubleVector(env, jAllowedValues));
373 JNIEXPORT void JNICALL Java_org_iotivity_simulator_SimulatorResourceServer_setAllowedValuesStringN
374 (JNIEnv *env, jobject object, jstring jKey, jobject jAllowedValues)
378 std::cout << "setAllowedValuesStringN: AttributeName is Empty";
382 SimulatorResourcePtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, object);
383 if (nullptr == resource.get())
385 std::cout << "setAllowedValuesStringN: Resource is NULL";
389 std::string str = env->GetStringUTFChars(jKey, NULL);
390 resource->setAllowedValues(str, convertStringVector(env, jAllowedValues));
393 JNIEXPORT jint JNICALL
394 Java_org_iotivity_simulator_SimulatorResourceServer_startResourceAutomation
395 (JNIEnv *env, jobject object)
397 SimulatorResourcePtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, object);
398 if (nullptr == resource.get())
404 if (SIMULATOR_SUCCESS != resource->startUpdateAutomation(AutomationType::NORMAL, automationId))
410 JNIEXPORT jint JNICALL
411 Java_org_iotivity_simulator_SimulatorResourceServer_startAttributeAutomation
412 (JNIEnv *env, jobject object, jstring attrName)
414 SimulatorResourcePtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, object);
415 if (nullptr == resource.get())
420 const char *attrNamePtr = env->GetStringUTFChars(attrName, NULL);
422 int automationId = -1;
423 resource->startUpdateAutomation(AutomationType::NORMAL, automationId);
425 env->ReleaseStringUTFChars(attrName, attrNamePtr);
429 JNIEXPORT void JNICALL
430 Java_org_iotivity_simulator_SimulatorResourceServer_startAutomation
431 (JNIEnv *env, jobject object, jint automationId)
433 SimulatorResourcePtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, object);
434 if (nullptr == resource.get())
439 resource->stopUpdateAutomation(automationId);
442 JNIEXPORT void JNICALL Java_org_iotivity_simulator_SimulatorResourceServer_removeAttribute
443 (JNIEnv *env, jobject jobject, jstring jKey)
447 std::cout << "removeAttribute: AttributeName is Empty";
451 SimulatorResourcePtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, jobject);
452 if (nullptr == resource.get())
454 std::cout << "removeAttribute: Resource is NULL";
458 std::string str = env->GetStringUTFChars(jKey, NULL);
459 resource->removeAttribute(str);
462 JNIEXPORT void JNICALL Java_org_iotivity_simulator_SimulatorResourceServer_dispose
463 (JNIEnv *env, jobject thiz)
465 JniSimulatorResource *resource = GetHandle<JniSimulatorResource>(env, thiz);