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_server_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(SimulatorResourceServerPtr &resource)
29 : m_sharedResource(resource) {}
31 SimulatorResourceServerPtr JniSimulatorResource::getJniSimulatorResourcePtr(JNIEnv *env,
34 JniSimulatorResource *resource = GetHandle<JniSimulatorResource>(env, thiz);
35 if (env->ExceptionCheck())
39 return resource->m_sharedResource;
42 jobject JniSimulatorResource::toJava(JNIEnv *env, jlong resource)
44 jobject resourceObj = (jobject) env->NewObject(gSimulatorClassRefs.classSimulatorResource,
45 gSimulatorClassRefs.classSimulatorResourceCtor, resource);
46 if (NULL == resourceObj)
53 void JniSimulatorResource::setResourceInfo(JNIEnv *env, jobject jobj)
58 std::string uri = m_sharedResource->getURI();
59 std::string resourceType = m_sharedResource->getResourceType();
60 std::string name = m_sharedResource->getName();
61 std::string interfaceType = m_sharedResource->getInterfaceType();
63 jstring jURI = env->NewStringUTF(uri.c_str());
66 env->CallVoidMethod(jobj, gSimulatorClassRefs.classSimulatorResourceSetURI, jURI);
67 env->DeleteLocalRef(jURI);
70 jstring jResourceType = env->NewStringUTF(resourceType.c_str());
73 env->CallVoidMethod(jobj, gSimulatorClassRefs.classSimulatorResourceSetResourceType, jResourceType);
74 env->DeleteLocalRef(jResourceType);
77 jstring jName = env->NewStringUTF(name.c_str());
80 env->CallVoidMethod(jobj, gSimulatorClassRefs.classSimulatorResourceSetName, jName);
81 env->DeleteLocalRef(jName);
84 jstring jInterfaceType = env->NewStringUTF(interfaceType.c_str());
87 env->CallVoidMethod(jobj, gSimulatorClassRefs.classSimulatorResourceSetInterfaceType,
89 env->DeleteLocalRef(jInterfaceType);
93 void onAutomationComplete(jweak jlistenerRef, const std::string &uri,
94 const int automationID)
96 std::cout << "onAutomationComplete JNI entry" << std::endl;
97 JNIEnv *env = getEnv();
101 jobject autoCompleteListener = env->NewLocalRef(jlistenerRef);
102 if (!autoCompleteListener)
108 jclass autoCompleteCls = env->GetObjectClass(autoCompleteListener);
109 if (!autoCompleteCls)
115 jmethodID autoCompleteMId = env->GetMethodID(autoCompleteCls, "onAutomationComplete",
116 "(Ljava/lang/String;I)V");
117 if (!autoCompleteMId)
123 jstring jUri = env->NewStringUTF(uri.c_str());
125 env->CallVoidMethod(autoCompleteListener, autoCompleteMId, jUri, automationID);
126 if ((env)->ExceptionCheck())
132 env->DeleteLocalRef(jUri);
137 JNIEXPORT jobject JNICALL
138 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_getModel
139 (JNIEnv *env, jobject object)
141 SimulatorResourceServerPtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, object);
142 if (nullptr == resource.get())
144 std::cout << "getModel: Resource is NULL";
148 SimulatorResourceModel resModel = resource->getModel();
149 JniSimulatorResourceModel *model = new JniSimulatorResourceModel(resModel);
150 jobject jModel = JniSimulatorResourceModel::toJava(env, reinterpret_cast<jlong>(model));
154 JNIEXPORT void JNICALL
155 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_updateAttributeFromAllowedValues
156 (JNIEnv *env, jobject object, jstring attrName, jint index)
158 SimulatorResourceServerPtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, object);
159 if (nullptr == resource.get())
161 std::cout << "updateAttributeFromAllowedValues: Resource is NULL";
165 const char *attrNamePtr = env->GetStringUTFChars(attrName, NULL);
168 std::cout << "updateAttributeFromAllowedValues: Failed to convert jstring to char string!";
172 resource->updateAttributeFromAllowedValues(attrNamePtr, static_cast<int>(index));
173 env->ReleaseStringUTFChars(attrName, attrNamePtr);
176 JNIEXPORT void JNICALL
177 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_setRange
178 (JNIEnv *env, jobject object, jstring attrName, jint min, jint max)
180 SimulatorResourceServerPtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, object);
181 if (nullptr == resource.get())
183 std::cout << "setRange: Resource is NULL";
187 const char *attrNamePtr = env->GetStringUTFChars(attrName, NULL);
190 std::cout << "setRange: Failed to convert jstring to char string!";
194 resource->setRange(attrNamePtr, static_cast<int>(min), static_cast<int>(max));
195 env->ReleaseStringUTFChars(attrName, attrNamePtr);
198 JNIEXPORT void JNICALL
199 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_setInterfaceType
200 (JNIEnv *env, jobject jobject, const std::string &interfaceType)
202 jstring jInterfaceType = env->NewStringUTF(interfaceType.c_str());
205 std::cout << "setInterfaceType: InterfaceType is NULL";
209 env->CallVoidMethod(jobject, gSimulatorClassRefs.classSimulatorResourceSetInterfaceType,
211 env->DeleteLocalRef(jInterfaceType);
214 JNIEXPORT void JNICALL
215 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_addAttributeInteger
216 (JNIEnv *env, jobject jobject, jstring jKey, jint jValue)
220 std::cout << "addAttributeInteger: AttributeName is Empty";
224 SimulatorResourceServerPtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env,
226 if (nullptr == resource.get())
228 std::cout << "addAttributeInteger: Resource is NULL";
232 std::string str = env->GetStringUTFChars(jKey, NULL);
233 resource->addAttribute(str, static_cast<int>(jValue));
236 JNIEXPORT void JNICALL
237 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_addAttributeDouble
238 (JNIEnv *env, jobject jobject, jstring jKey, jdouble jValue)
242 std::cout << "addAttributeDouble: AttributeName is Empty";
246 SimulatorResourceServerPtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env,
248 if (nullptr == resource.get())
250 std::cout << "addAttributeDouble: Resource is NULL";
254 std::string str = env->GetStringUTFChars(jKey, NULL);
255 resource->addAttribute(str, static_cast<double>(jValue));
258 JNIEXPORT void JNICALL
259 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_addAttributeBoolean
260 (JNIEnv *env, jobject jobject, jstring jKey, jboolean jValue)
264 std::cout << "addAttributeBoolean: AttributeName is Empty";
268 SimulatorResourceServerPtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env,
270 if (nullptr == resource.get())
272 std::cout << "addAttributeBoolean: Resource is NULL";
276 std::string str = env->GetStringUTFChars(jKey, NULL);
277 resource->addAttribute(str, static_cast<bool>(jValue));
280 JNIEXPORT void JNICALL
281 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_addAttributeStringN
282 (JNIEnv *env, jobject jobject, jstring jKey, jstring jValue)
286 std::cout << "addAttributeStringN: AttributeName is Empty";
290 SimulatorResourceServerPtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env,
292 if (nullptr == resource.get())
294 std::cout << "addAttributeStringN: Resource is NULL";
298 std::string key = env->GetStringUTFChars(jKey, NULL);
299 std::string value = env->GetStringUTFChars(jValue, NULL);
301 resource->addAttribute(key, value);
304 JNIEXPORT void JNICALL
305 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_updateAttributeInteger
306 (JNIEnv *env, jobject jobject, jstring jKey, jint jValue)
310 std::cout << "updateAttributeInteger: AttributeName is Empty";
314 SimulatorResourceServerPtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env,
316 if (nullptr == resource.get())
318 std::cout << "updateAttributeInteger: Resource is NULL";
322 std::string str = env->GetStringUTFChars(jKey, NULL);
323 resource->updateAttribute(str, static_cast<int>(jValue));
326 JNIEXPORT void JNICALL
327 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_updateAttributeDouble
328 (JNIEnv *env, jobject jobject, jstring jKey, jdouble jValue)
332 std::cout << "updateAttributeDouble: AttributeName is Empty";
336 SimulatorResourceServerPtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env,
338 if (nullptr == resource.get())
340 std::cout << "updateAttributeDouble: Resource is NULL";
344 std::string str = env->GetStringUTFChars(jKey, NULL);
345 resource->updateAttribute(str, static_cast<double>(jValue));
348 JNIEXPORT void JNICALL
349 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_updateAttributeBoolean
350 (JNIEnv *env, jobject jobject, jstring jKey, jboolean jValue)
354 std::cout << "updateAttributeBoolean: AttributeName is Empty";
358 SimulatorResourceServerPtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env,
360 if (nullptr == resource.get())
362 std::cout << "updateAttributeBoolean: Resource is NULL";
366 std::string str = env->GetStringUTFChars(jKey, NULL);
367 resource->updateAttribute(str, static_cast<bool>(jValue));
370 JNIEXPORT void JNICALL
371 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_updateAttributeStringN
372 (JNIEnv *env, jobject jobject, jstring jKey, jstring jValue)
376 std::cout << "updateAttributeStringN: AttributeName is Empty";
380 SimulatorResourceServerPtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env,
382 if (nullptr == resource.get())
384 std::cout << "updateAttributeStringN: Resource is NULL";
388 std::string key = env->GetStringUTFChars(jKey, NULL);
389 std::string value = env->GetStringUTFChars(jValue, NULL);
391 resource->updateAttribute(key, value);
394 JNIEXPORT void JNICALL
395 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_setAllowedValuesInteger
396 (JNIEnv *env, jobject object, jstring jKey, jobject jAllowedValues)
400 std::cout << "setAllowedValuesInteger: AttributeName is Empty";
404 SimulatorResourceServerPtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, object);
405 if (nullptr == resource.get())
407 std::cout << "setAllowedValuesInteger: Resource is NULL";
411 std::string str = env->GetStringUTFChars(jKey, NULL);
412 resource->setAllowedValues(str, convertIntegerVector(env, jAllowedValues));
415 JNIEXPORT void JNICALL
416 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_setAllowedValuesDouble
417 (JNIEnv *env, jobject object, jstring jKey, jobject jAllowedValues)
421 std::cout << "setAllowedValuesDouble: AttributeName is Empty";
425 SimulatorResourceServerPtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, object);
426 if (nullptr == resource.get())
428 std::cout << "setAllowedValuesDouble: Resource is NULL";
432 std::string str = env->GetStringUTFChars(jKey, NULL);
433 resource->setAllowedValues(str, convertDoubleVector(env, jAllowedValues));
436 JNIEXPORT void JNICALL
437 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_setAllowedValuesStringN
438 (JNIEnv *env, jobject object, jstring jKey, jobject jAllowedValues)
442 std::cout << "setAllowedValuesStringN: AttributeName is Empty";
446 SimulatorResourceServerPtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, object);
447 if (nullptr == resource.get())
449 std::cout << "setAllowedValuesStringN: Resource is NULL";
453 std::string str = env->GetStringUTFChars(jKey, NULL);
454 resource->setAllowedValues(str, convertStringVector(env, jAllowedValues));
457 JNIEXPORT jint JNICALL
458 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_startResourceAutomation
459 (JNIEnv *env, jobject object, jint automationType, jobject listener)
461 SimulatorResourceServerPtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, object);
462 if (nullptr == resource.get())
472 jweak jlistenerRef = env->NewWeakGlobalRef(listener);
473 updateCompleteCallback callback = [jlistenerRef](const std::string & uri, const int automationID)
475 onAutomationComplete(jlistenerRef, uri, automationID);
478 AutomationType type = AutomationType::NORMAL;
479 if (1 == automationType)
481 type = AutomationType::RECURRENT;
485 if (SIMULATOR_SUCCESS != resource->startUpdateAutomation(type, callback,
492 JNIEXPORT jint JNICALL
493 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_startAttributeAutomation
494 (JNIEnv *env, jobject object, jstring attrName, jint automationType, jobject listener)
496 std::cout << "starAttributeAutomation JNI" << std::endl;
497 SimulatorResourceServerPtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, object);
498 if (nullptr == resource.get())
513 jweak jlistenerRef = env->NewWeakGlobalRef(listener);
514 updateCompleteCallback callback = [jlistenerRef](const std::string & uri, const int automationID)
516 onAutomationComplete(jlistenerRef, uri, automationID);
519 const char *attrNamePtr = env->GetStringUTFChars(attrName, NULL);
521 AutomationType type = AutomationType::NORMAL;
522 if (1 == automationType)
524 type = AutomationType::RECURRENT;
527 int automationId = -1;
528 resource->startUpdateAutomation(attrNamePtr, type, callback, automationId);
530 env->ReleaseStringUTFChars(attrName, attrNamePtr);
534 JNIEXPORT void JNICALL
535 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_stopAutomation
536 (JNIEnv *env, jobject object, jint automationId)
538 SimulatorResourceServerPtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, object);
539 if (nullptr == resource.get())
544 resource->stopUpdateAutomation(automationId);
547 JNIEXPORT void JNICALL
548 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_removeAttribute
549 (JNIEnv *env, jobject jobject, jstring jKey)
553 std::cout << "removeAttribute: AttributeName is Empty";
557 SimulatorResourceServerPtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env,
559 if (nullptr == resource.get())
561 std::cout << "removeAttribute: Resource is NULL";
565 std::string str = env->GetStringUTFChars(jKey, NULL);
566 resource->removeAttribute(str);
569 JNIEXPORT void JNICALL Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_dispose
570 (JNIEnv *env, jobject thiz)
572 JniSimulatorResource *resource = GetHandle<JniSimulatorResource>(env, thiz);