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!");
239 throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Attribute value cannot be null!");
243 SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env,
247 throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
251 std::string key = env->GetStringUTFChars(jKey, NULL);
252 std::string value = env->GetStringUTFChars(jValue, NULL);
253 SimulatorResourceModel::Attribute att;
256 resource->addAttribute(att);
259 JNIEXPORT void JNICALL
260 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_updateAttributeInteger
261 (JNIEnv *env, jobject jobject, jstring jKey, jint jValue)
265 throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid Attribute name!");
269 SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env,
273 throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
277 std::string str = env->GetStringUTFChars(jKey, NULL);
278 resource->updateAttributeValue(str, static_cast<int>(jValue));
281 JNIEXPORT void JNICALL
282 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_updateAttributeDouble
283 (JNIEnv *env, jobject jobject, jstring jKey, jdouble jValue)
287 throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid Attribute name!");
291 SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env,
295 throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
299 std::string str = env->GetStringUTFChars(jKey, NULL);
300 resource->updateAttributeValue(str, static_cast<double>(jValue));
303 JNIEXPORT void JNICALL
304 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_updateAttributeBoolean
305 (JNIEnv *env, jobject jobject, jstring jKey, jboolean jValue)
309 throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid Attribute name!");
313 SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env,
317 throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
321 std::string str = env->GetStringUTFChars(jKey, NULL);
322 resource->updateAttributeValue(str, static_cast<bool>(jValue));
325 JNIEXPORT void JNICALL
326 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_updateAttributeString
327 (JNIEnv *env, jobject jobject, jstring jKey, jstring jValue)
331 throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid Attribute name!");
335 SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env,
339 throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
343 std::string key = env->GetStringUTFChars(jKey, NULL);
344 std::string value = env->GetStringUTFChars(jValue, NULL);
346 resource->updateAttributeValue(key, value);
349 JNIEXPORT void JNICALL
350 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_updateAttributeFromAllowedValues
351 (JNIEnv *env, jobject object, jstring attrName, jint index)
353 if (!attrName || index < 0)
355 throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid parameter!");
359 SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object);
362 throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
366 const char *attrNameCStr = env->GetStringUTFChars(attrName, NULL);
369 throwSimulatorException(env, SIMULATOR_ERROR, "String error!");
373 resource->updateAttributeValue(attrNameCStr, static_cast<int>(index));
374 env->ReleaseStringUTFChars(attrName, attrNameCStr);
377 JNIEXPORT void JNICALL
378 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_setRange
379 (JNIEnv *env, jobject object, jstring attrName, jint min, jint max)
383 throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid parameter!");
387 SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object);
390 throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
394 const char *attrNameCStr = env->GetStringUTFChars(attrName, NULL);
397 throwSimulatorException(env, SIMULATOR_ERROR, "String error!");
401 resource->setRange(attrNameCStr, static_cast<int>(min), static_cast<int>(max));
402 env->ReleaseStringUTFChars(attrName, attrNameCStr);
405 JNIEXPORT void JNICALL
406 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_setAllowedValuesInteger
407 (JNIEnv *env, jobject object, jstring jKey, jobject jAllowedValues)
409 if (!jKey || !jAllowedValues)
411 throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid parameter!");
415 SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object);
418 throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
422 const char *keyCStr = env->GetStringUTFChars(jKey, NULL);
425 throwSimulatorException(env, SIMULATOR_ERROR, "String error!");
429 resource->setAllowedValues(keyCStr, convertIntegerVector(env, jAllowedValues));
430 env->ReleaseStringUTFChars(jKey, keyCStr);
433 JNIEXPORT void JNICALL
434 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_setAllowedValuesDouble
435 (JNIEnv *env, jobject object, jstring jKey, jobject jAllowedValues)
437 if (!jKey || !jAllowedValues)
439 throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid parameter!");
443 SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object);
446 throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
450 const char *keyCStr = env->GetStringUTFChars(jKey, NULL);
453 throwSimulatorException(env, SIMULATOR_ERROR, "String error!");
457 resource->setAllowedValues(keyCStr, convertDoubleVector(env, jAllowedValues));
458 env->ReleaseStringUTFChars(jKey, keyCStr);
461 JNIEXPORT void JNICALL
462 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_setAllowedValuesString
463 (JNIEnv *env, jobject object, jstring jKey, jobject jAllowedValues)
465 if (!jKey || !jAllowedValues)
467 throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid parameter!");
471 SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object);
474 throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
478 const char *keyCStr = env->GetStringUTFChars(jKey, NULL);
481 throwSimulatorException(env, SIMULATOR_ERROR, "String error!");
485 resource->setAllowedValues(keyCStr, convertStringVector(env, jAllowedValues));
486 env->ReleaseStringUTFChars(jKey, keyCStr);
489 JNIEXPORT jint JNICALL
490 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_startResourceAutomation
491 (JNIEnv *env, jobject object, jint automationType, jint updateInterval, jobject listener)
493 SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object);
496 throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
497 return SIMULATOR_BAD_OBJECT;
502 throwInvalidArgsException(env, SIMULATOR_INVALID_CALLBACK,
503 "Start Resource Automation failed! Callback not set");
504 return SIMULATOR_INVALID_CALLBACK;
507 jweak jlistenerRef = env->NewWeakGlobalRef(listener);
508 updateCompleteCallback callback = [jlistenerRef](const std::string & uri, const int automationID)
510 onAutomationComplete(jlistenerRef, uri, automationID);
513 AutomationType type = AutomationType::NORMAL;
514 if (1 == automationType)
516 type = AutomationType::RECURRENT;
519 int automationId = -1;
523 automationId = resource->startUpdateAutomation(type, updateInterval, callback);
525 catch (InvalidArgsException &e)
527 throwInvalidArgsException(env, e.code(), e.what());
529 catch (SimulatorException &e)
531 throwSimulatorException(env, e.code(), e.what());
535 throwSimulatorException(env, SIMULATOR_ERROR, "Unknown Exception");
540 JNIEXPORT jint JNICALL
541 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_startAttributeAutomation
542 (JNIEnv *env, jobject object, jstring attrName, jint automationType, jint updateInterval, jobject listener)
544 SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object);
547 throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
548 return SIMULATOR_BAD_OBJECT;
553 throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid Attribute name!");
554 return SIMULATOR_INVALID_PARAM;
559 throwInvalidArgsException(env, SIMULATOR_INVALID_CALLBACK,
560 "Start Attribute Automation failed! Callback not set");
561 return SIMULATOR_INVALID_CALLBACK;
564 jweak jlistenerRef = env->NewWeakGlobalRef(listener);
565 updateCompleteCallback callback = [jlistenerRef](const std::string & uri, const int automationID)
567 onAutomationComplete(jlistenerRef, uri, automationID);
570 const char *attrNamePtr = env->GetStringUTFChars(attrName, NULL);
572 AutomationType type = AutomationType::NORMAL;
573 if (1 == automationType)
575 type = AutomationType::RECURRENT;
578 int automationId = -1;
581 automationId = resource->startUpdateAutomation(attrNamePtr, type, updateInterval, callback);
583 catch (InvalidArgsException &e)
585 throwInvalidArgsException(env, e.code(), e.what());
588 catch (SimulatorException &e)
590 throwSimulatorException(env, e.code(), e.what());
595 throwSimulatorException(env, SIMULATOR_ERROR, "Unknown Exception");
598 env->ReleaseStringUTFChars(attrName, attrNamePtr);
602 JNIEXPORT void JNICALL
603 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_stopAutomation
604 (JNIEnv *env, jobject object, jint automationId)
606 SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object);
609 throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
615 resource->stopUpdateAutomation(automationId);
617 catch (SimulatorException &e)
619 throwSimulatorException(env, e.code(), e.what());
623 throwSimulatorException(env, SIMULATOR_ERROR, "Unknown Exception");
626 SIM_LOG(ILogger::INFO, "Automation has been forcibly stopped.")
629 JNIEXPORT void JNICALL
630 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_removeAttribute
631 (JNIEnv *env, jobject jobject, jstring jKey)
635 throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid Attribute name!");
639 SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env,
643 throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
647 std::string str = env->GetStringUTFChars(jKey, NULL);
650 resource->removeAttribute(str);
652 catch (InvalidArgsException &e)
654 throwInvalidArgsException(env, e.code(), e.what());
657 catch (SimulatorException &e)
659 throwSimulatorException(env, e.code(), e.what());
664 throwSimulatorException(env, SIMULATOR_ERROR, "Unknown Exception");
669 JNIEXPORT jobjectArray JNICALL
670 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_getObserversList
671 (JNIEnv *env, jobject object)
673 SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object);
676 throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
680 std::vector<ObserverInfo> observersList;
681 observersList = resource->getObserversList();
683 // Construct the object array and send it java layer
684 jobjectArray jobserversArray = env->NewObjectArray(observersList.size(),
685 gSimulatorClassRefs.classObserverInfo, NULL);
688 for (size_t i = 0; i < observersList.size(); i++)
690 jstring jaddress = env->NewStringUTF(observersList[i].address.c_str());
691 jobject jobserver = (jobject) env->NewObject(gSimulatorClassRefs.classObserverInfo,
692 gSimulatorClassRefs.classObserverInfoCtor, observersList[i].id,
693 jaddress, observersList[i].port);
695 env->SetObjectArrayElement(jobserversArray, i, jobserver);
696 env->DeleteLocalRef(jaddress);
700 return jobserversArray;
703 void onObserverChange(jweak jlistenerRef, const std::string &uri,
704 ObservationStatus state, const ObserverInfo &observerInfo)
706 JNIEnv *env = getEnv();
710 jobject observerChangeListener = env->NewLocalRef(jlistenerRef);
711 if (!observerChangeListener)
717 jclass observerChangeCls = env->GetObjectClass(observerChangeListener);
718 if (!observerChangeCls)
724 jmethodID observerChangeMId = env->GetMethodID(observerChangeCls, "onObserverChanged",
725 "(Ljava/lang/String;ILorg/oic/simulator/serviceprovider/ObserverInfo;)V");
726 if (!observerChangeMId)
733 jstring jUri = env->NewStringUTF(uri.c_str());
736 jint jstate = (state == ObservationStatus::OBSERVE_REGISTER) ? 0 : 1;
738 // Construct the java object of observerinfo
739 jstring jaddress = env->NewStringUTF(observerInfo.address.c_str());
740 jobject jobserver = (jobject) env->NewObject(gSimulatorClassRefs.classObserverInfo,
741 gSimulatorClassRefs.classObserverInfoCtor, observerInfo.id,
742 jaddress, observerInfo.port);
744 env->CallVoidMethod(observerChangeListener, observerChangeMId, jUri, jstate, jobserver);
745 env->DeleteLocalRef(jaddress);
746 if ((env)->ExceptionCheck())
752 env->DeleteLocalRef(jUri);
757 JNIEXPORT void JNICALL
758 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_setObserverCallback
759 (JNIEnv *env, jobject object, jobject jcallback)
763 throwInvalidArgsException(env, SIMULATOR_INVALID_CALLBACK, "Callback not set");
767 jweak jlistenerRef = env->NewWeakGlobalRef(jcallback);
768 SimulatorResourceServer::ObserverCB callback = [jlistenerRef](const std::string & uri,
769 ObservationStatus state, const ObserverInfo & observerInfo)
771 onObserverChange(jlistenerRef, uri, state, observerInfo);
774 SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object);
777 throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
781 resource->setObserverCallback(callback);
784 JNIEXPORT void JNICALL
785 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_notifyObserver
786 (JNIEnv *env, jobject object, jint jId)
788 SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object);
791 throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
797 resource->notify(jId);
799 catch (SimulatorException &e)
801 throwSimulatorException(env, e.code(), e.what());
805 throwSimulatorException(env, SIMULATOR_ERROR, "Unknown Exception");
809 JNIEXPORT void JNICALL
810 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_notifyAllObservers
811 (JNIEnv *env, jobject object)
813 SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object);
816 throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
822 resource->notifyAll();
824 catch (SimulatorException &e)
826 throwSimulatorException(env, e.code(), e.what());
830 throwSimulatorException(env, SIMULATOR_ERROR, "Unknown Exception");
834 JNIEXPORT void JNICALL Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_dispose
835 (JNIEnv *env, jobject thiz)
837 JniSimulatorResource *resource = GetHandle<JniSimulatorResource>(env, thiz);