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"
25 #include "simulator_jni_utils.h"
26 #include "simulator_logger.h"
27 #include "simulator_jni_utils.h"
29 extern SimulatorClassRefs gSimulatorClassRefs;
31 JniSimulatorResource::JniSimulatorResource(SimulatorResourceServerSP &resource)
32 : m_sharedResource(resource) {}
34 SimulatorResourceServerSP JniSimulatorResource::getJniSimulatorResourceSP(JNIEnv *env,
37 JniSimulatorResource *resource = GetHandle<JniSimulatorResource>(env, thiz);
38 if (env->ExceptionCheck())
42 return resource->m_sharedResource;
45 jobject JniSimulatorResource::toJava(JNIEnv *env, jlong resource)
47 jobject resourceObj = (jobject) env->NewObject(gSimulatorClassRefs.classSimulatorResource,
48 gSimulatorClassRefs.classSimulatorResourceCtor, resource);
49 if (NULL == resourceObj)
56 void JniSimulatorResource::setResourceInfo(JNIEnv *env, jobject jobj)
61 std::string uri = m_sharedResource->getURI();
62 std::string resourceType = m_sharedResource->getResourceType();
63 std::string name = m_sharedResource->getName();
64 std::string interfaceType = m_sharedResource->getInterfaceType();
66 jfieldID fieldID = env->GetFieldID(gSimulatorClassRefs.classSimulatorResource, "resourceURI",
67 "Ljava/lang/String;");
68 jstring jUri = env->NewStringUTF(uri.c_str());
69 env->SetObjectField(jobj, fieldID, jUri);
71 fieldID = env->GetFieldID(gSimulatorClassRefs.classSimulatorResource, "resourceName",
72 "Ljava/lang/String;");
73 jstring jName = env->NewStringUTF(name.c_str());
74 env->SetObjectField(jobj, fieldID, jName);
76 fieldID = env->GetFieldID(gSimulatorClassRefs.classSimulatorResource, "resourceType",
77 "Ljava/lang/String;");
78 jstring jResourceType = env->NewStringUTF(resourceType.c_str());
79 env->SetObjectField(jobj, fieldID, jResourceType);
81 fieldID = env->GetFieldID(gSimulatorClassRefs.classSimulatorResource, "interfaceType",
82 "Ljava/lang/String;");
83 jstring jInterfaceType = env->NewStringUTF(interfaceType.c_str());
84 env->SetObjectField(jobj, fieldID, jInterfaceType);
86 env->DeleteLocalRef(jUri);
87 env->DeleteLocalRef(jName);
88 env->DeleteLocalRef(jResourceType);
89 env->DeleteLocalRef(jInterfaceType);
92 void onAutomationComplete(jweak jlistenerRef, const std::string &uri,
93 const int automationID)
95 JNIEnv *env = getEnv();
99 jobject autoCompleteListener = env->NewLocalRef(jlistenerRef);
100 if (!autoCompleteListener)
106 jclass autoCompleteCls = env->GetObjectClass(autoCompleteListener);
107 if (!autoCompleteCls)
113 jmethodID autoCompleteMId = env->GetMethodID(autoCompleteCls, "onAutomationComplete",
114 "(Ljava/lang/String;I)V");
115 if (!autoCompleteMId)
121 jstring jUri = env->NewStringUTF(uri.c_str());
123 env->CallVoidMethod(autoCompleteListener, autoCompleteMId, jUri, automationID);
124 if ((env)->ExceptionCheck())
130 env->DeleteLocalRef(jUri);
135 JNIEXPORT jobject JNICALL
136 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_getModel
137 (JNIEnv *env, jobject object)
139 SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object);
142 throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
146 SimulatorResourceModel resModel = resource->getModel();
147 JSimulatorResourceModel *model = new JSimulatorResourceModel(resModel);
148 jobject jModel = JSimulatorResourceModel::toJava(env, reinterpret_cast<jlong>(model));
152 JNIEXPORT void JNICALL
153 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_addAttributeInteger
154 (JNIEnv *env, jobject jobject, jstring jKey, jint jValue)
158 throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid Attribute name!");
162 SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env,
166 throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
170 std::string str = env->GetStringUTFChars(jKey, NULL);
171 SimulatorResourceModel::Attribute att;
173 att.setValue(static_cast<int>(jValue));
174 resource->addAttribute(att);
177 JNIEXPORT void JNICALL
178 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_addAttributeDouble
179 (JNIEnv *env, jobject jobject, jstring jKey, jdouble jValue)
183 throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid Attribute name!");
187 SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env,
191 throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
195 std::string str = env->GetStringUTFChars(jKey, NULL);
196 SimulatorResourceModel::Attribute att;
198 att.setValue(static_cast<double>(jValue));
199 resource->addAttribute(att);
202 JNIEXPORT void JNICALL
203 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_addAttributeBoolean
204 (JNIEnv *env, jobject jobject, jstring jKey, jboolean jValue)
208 throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid Attribute name!");
212 SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env,
216 throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
220 std::string str = env->GetStringUTFChars(jKey, NULL);
221 SimulatorResourceModel::Attribute att;
223 att.setValue(static_cast<bool>(jValue));
224 resource->addAttribute(att);
227 JNIEXPORT void JNICALL
228 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_addAttributeString
229 (JNIEnv *env, jobject jobject, jstring jKey, jstring jValue)
233 throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid Attribute name!");
237 SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env,
241 throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
245 std::string key = env->GetStringUTFChars(jKey, NULL);
246 std::string value = env->GetStringUTFChars(jValue, NULL);
247 SimulatorResourceModel::Attribute att;
250 resource->addAttribute(att);
253 JNIEXPORT void JNICALL
254 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_updateAttributeInteger
255 (JNIEnv *env, jobject jobject, jstring jKey, jint jValue)
259 throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid Attribute name!");
263 SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env,
267 throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
271 std::string str = env->GetStringUTFChars(jKey, NULL);
272 resource->updateAttributeValue(str, static_cast<int>(jValue));
275 JNIEXPORT void JNICALL
276 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_updateAttributeDouble
277 (JNIEnv *env, jobject jobject, jstring jKey, jdouble jValue)
281 throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid Attribute name!");
285 SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env,
289 throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
293 std::string str = env->GetStringUTFChars(jKey, NULL);
294 resource->updateAttributeValue(str, static_cast<double>(jValue));
297 JNIEXPORT void JNICALL
298 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_updateAttributeBoolean
299 (JNIEnv *env, jobject jobject, jstring jKey, jboolean jValue)
303 throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid Attribute name!");
307 SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env,
311 throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
315 std::string str = env->GetStringUTFChars(jKey, NULL);
316 resource->updateAttributeValue(str, static_cast<bool>(jValue));
319 JNIEXPORT void JNICALL
320 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_updateAttributeString
321 (JNIEnv *env, jobject jobject, jstring jKey, jstring jValue)
325 throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid Attribute name!");
329 SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env,
333 throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
337 std::string key = env->GetStringUTFChars(jKey, NULL);
338 std::string value = env->GetStringUTFChars(jValue, NULL);
340 resource->updateAttributeValue(key, value);
343 JNIEXPORT void JNICALL
344 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_updateAttributeFromAllowedValues
345 (JNIEnv *env, jobject object, jstring attrName, jint index)
347 if (!attrName || index < 0)
349 throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid parameter!");
353 SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object);
356 throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
360 const char *attrNameCStr = env->GetStringUTFChars(attrName, NULL);
363 throwSimulatorException(env, SIMULATOR_ERROR, "String error!");
367 resource->updateAttributeValue(attrNameCStr, static_cast<int>(index));
368 env->ReleaseStringUTFChars(attrName, attrNameCStr);
371 JNIEXPORT void JNICALL
372 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_setRange
373 (JNIEnv *env, jobject object, jstring attrName, jint min, jint max)
377 throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid parameter!");
381 SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object);
384 throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
388 const char *attrNameCStr = env->GetStringUTFChars(attrName, NULL);
391 throwSimulatorException(env, SIMULATOR_ERROR, "String error!");
395 resource->setRange(attrNameCStr, static_cast<int>(min), static_cast<int>(max));
396 env->ReleaseStringUTFChars(attrName, attrNameCStr);
399 JNIEXPORT void JNICALL
400 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_setAllowedValuesInteger
401 (JNIEnv *env, jobject object, jstring jKey, jobject jAllowedValues)
403 if (!jKey || !jAllowedValues)
405 throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid parameter!");
409 SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object);
412 throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
416 const char *keyCStr = env->GetStringUTFChars(jKey, NULL);
419 throwSimulatorException(env, SIMULATOR_ERROR, "String error!");
423 resource->setAllowedValues(keyCStr, convertIntegerVector(env, jAllowedValues));
424 env->ReleaseStringUTFChars(jKey, keyCStr);
427 JNIEXPORT void JNICALL
428 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_setAllowedValuesDouble
429 (JNIEnv *env, jobject object, jstring jKey, jobject jAllowedValues)
431 if (!jKey || !jAllowedValues)
433 throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid parameter!");
437 SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object);
440 throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
444 const char *keyCStr = env->GetStringUTFChars(jKey, NULL);
447 throwSimulatorException(env, SIMULATOR_ERROR, "String error!");
451 resource->setAllowedValues(keyCStr, convertDoubleVector(env, jAllowedValues));
452 env->ReleaseStringUTFChars(jKey, keyCStr);
455 JNIEXPORT void JNICALL
456 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_setAllowedValuesString
457 (JNIEnv *env, jobject object, jstring jKey, jobject jAllowedValues)
459 if (!jKey || !jAllowedValues)
461 throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid parameter!");
465 SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object);
468 throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
472 const char *keyCStr = env->GetStringUTFChars(jKey, NULL);
475 throwSimulatorException(env, SIMULATOR_ERROR, "String error!");
479 resource->setAllowedValues(keyCStr, convertStringVector(env, jAllowedValues));
480 env->ReleaseStringUTFChars(jKey, keyCStr);
483 JNIEXPORT jint JNICALL
484 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_startResourceAutomation
485 (JNIEnv *env, jobject object, jint automationType, jobject listener)
487 SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object);
490 throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
491 return SIMULATOR_BAD_OBJECT;
496 throwInvalidArgsException(env, SIMULATOR_INVALID_CALLBACK,
497 "Start Resource Automation failed! Callback not set");
498 return SIMULATOR_INVALID_CALLBACK;
501 jweak jlistenerRef = env->NewWeakGlobalRef(listener);
502 updateCompleteCallback callback = [jlistenerRef](const std::string & uri, const int automationID)
504 onAutomationComplete(jlistenerRef, uri, automationID);
507 AutomationType type = AutomationType::NORMAL;
508 if (1 == automationType)
510 type = AutomationType::RECURRENT;
513 int automationId = -1;
517 automationId = resource->startUpdateAutomation(type, callback);
519 catch (InvalidArgsException &e)
521 throwInvalidArgsException(env, e.code(), e.what());
523 catch (SimulatorException &e)
525 throwSimulatorException(env, e.code(), e.what());
529 throwSimulatorException(env, SIMULATOR_ERROR, "Unknown Exception");
534 JNIEXPORT jint JNICALL
535 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_startAttributeAutomation
536 (JNIEnv *env, jobject object, jstring attrName, jint automationType, jobject listener)
538 SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object);
541 throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
542 return SIMULATOR_BAD_OBJECT;
547 throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid Attribute name!");
548 return SIMULATOR_INVALID_PARAM;
553 throwInvalidArgsException(env, SIMULATOR_INVALID_CALLBACK,
554 "Start Attribute Automation failed! Callback not set");
555 return SIMULATOR_INVALID_CALLBACK;
558 jweak jlistenerRef = env->NewWeakGlobalRef(listener);
559 updateCompleteCallback callback = [jlistenerRef](const std::string & uri, const int automationID)
561 onAutomationComplete(jlistenerRef, uri, automationID);
564 const char *attrNamePtr = env->GetStringUTFChars(attrName, NULL);
566 AutomationType type = AutomationType::NORMAL;
567 if (1 == automationType)
569 type = AutomationType::RECURRENT;
572 int automationId = -1;
575 automationId = resource->startUpdateAutomation(attrNamePtr, type, callback);
577 catch (InvalidArgsException &e)
579 throwInvalidArgsException(env, e.code(), e.what());
582 catch (SimulatorException &e)
584 throwSimulatorException(env, e.code(), e.what());
589 throwSimulatorException(env, SIMULATOR_ERROR, "Unknown Exception");
592 env->ReleaseStringUTFChars(attrName, attrNamePtr);
596 JNIEXPORT void JNICALL
597 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_stopAutomation
598 (JNIEnv *env, jobject object, jint automationId)
600 SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object);
603 throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
609 resource->stopUpdateAutomation(automationId);
611 catch (SimulatorException &e)
613 throwSimulatorException(env, e.code(), e.what());
617 throwSimulatorException(env, SIMULATOR_ERROR, "Unknown Exception");
620 SIM_LOG(ILogger::INFO, "Automation has been forcibly stopped.")
623 JNIEXPORT void JNICALL
624 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_removeAttribute
625 (JNIEnv *env, jobject jobject, jstring jKey)
629 throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid Attribute name!");
633 SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env,
637 throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
641 std::string str = env->GetStringUTFChars(jKey, NULL);
642 resource->removeAttribute(str);
645 JNIEXPORT jobjectArray JNICALL
646 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_getObserversList
647 (JNIEnv *env, jobject object)
649 SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object);
652 throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
656 std::vector<ObserverInfo> observersList;
657 observersList = resource->getObserversList();
659 // Construct the object array and send it java layer
660 jobjectArray jobserversArray = env->NewObjectArray(observersList.size(),
661 gSimulatorClassRefs.classObserverInfo, NULL);
664 for (size_t i = 0; i < observersList.size(); i++)
666 jstring jaddress = env->NewStringUTF(observersList[i].address.c_str());
667 jobject jobserver = (jobject) env->NewObject(gSimulatorClassRefs.classObserverInfo,
668 gSimulatorClassRefs.classObserverInfoCtor, observersList[i].id,
669 jaddress, observersList[i].port);
671 env->SetObjectArrayElement(jobserversArray, i, jobserver);
672 env->DeleteLocalRef(jaddress);
676 return jobserversArray;
679 void onObserverChange(jweak jlistenerRef, const std::string &uri,
680 ObservationStatus state, const ObserverInfo &observerInfo)
682 JNIEnv *env = getEnv();
686 jobject observerChangeListener = env->NewLocalRef(jlistenerRef);
687 if (!observerChangeListener)
693 jclass observerChangeCls = env->GetObjectClass(observerChangeListener);
694 if (!observerChangeCls)
700 jmethodID observerChangeMId = env->GetMethodID(observerChangeCls, "onObserverChanged",
701 "(Ljava/lang/String;ILorg/oic/simulator/serviceprovider/ObserverInfo;)V");
702 if (!observerChangeMId)
709 jstring jUri = env->NewStringUTF(uri.c_str());
712 jint jstate = (state == ObservationStatus::OBSERVE_REGISTER) ? 0 : 1;
714 // Construct the java object of observerinfo
715 jstring jaddress = env->NewStringUTF(observerInfo.address.c_str());
716 jobject jobserver = (jobject) env->NewObject(gSimulatorClassRefs.classObserverInfo,
717 gSimulatorClassRefs.classObserverInfoCtor, observerInfo.id,
718 jaddress, observerInfo.port);
720 env->CallVoidMethod(observerChangeListener, observerChangeMId, jUri, jstate, jobserver);
721 env->DeleteLocalRef(jaddress);
722 if ((env)->ExceptionCheck())
728 env->DeleteLocalRef(jUri);
733 JNIEXPORT void JNICALL
734 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_setObserverCallback
735 (JNIEnv *env, jobject object, jobject jcallback)
739 throwInvalidArgsException(env, SIMULATOR_INVALID_CALLBACK, "Callback not set");
743 jweak jlistenerRef = env->NewWeakGlobalRef(jcallback);
744 SimulatorResourceServer::ObserverCB callback = [jlistenerRef](const std::string & uri,
745 ObservationStatus state, const ObserverInfo & observerInfo)
747 onObserverChange(jlistenerRef, uri, state, observerInfo);
750 SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object);
753 throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
757 resource->setObserverCallback(callback);
760 JNIEXPORT void JNICALL
761 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_notifyObserver
762 (JNIEnv *env, jobject object, jint jId)
764 SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object);
767 throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
773 resource->notify(jId);
775 catch (SimulatorException &e)
777 throwSimulatorException(env, e.code(), e.what());
781 throwSimulatorException(env, SIMULATOR_ERROR, "Unknown Exception");
785 JNIEXPORT void JNICALL
786 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_notifyAllObservers
787 (JNIEnv *env, jobject object)
789 SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object);
792 throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
798 resource->notifyAll();
800 catch (SimulatorException &e)
802 throwSimulatorException(env, e.code(), e.what());
806 throwSimulatorException(env, SIMULATOR_ERROR, "Unknown Exception");
810 JNIEXPORT void JNICALL Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_dispose
811 (JNIEnv *env, jobject thiz)
813 JniSimulatorResource *resource = GetHandle<JniSimulatorResource>(env, thiz);