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!");
607 resource->stopUpdateAutomation(automationId);
608 SIM_LOG(ILogger::INFO, "Automation has been forcibly stopped.")
611 JNIEXPORT void JNICALL
612 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_removeAttribute
613 (JNIEnv *env, jobject jobject, jstring jKey)
617 throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid Attribute name!");
621 SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env,
625 throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
629 std::string str = env->GetStringUTFChars(jKey, NULL);
630 resource->removeAttribute(str);
633 JNIEXPORT jobjectArray JNICALL
634 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_getObserversList
635 (JNIEnv *env, jobject object)
637 SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object);
640 throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
644 std::vector<ObserverInfo> observersList;
645 observersList = resource->getObserversList();
647 // Construct the object array and send it java layer
648 jobjectArray jobserversArray = env->NewObjectArray(observersList.size(),
649 gSimulatorClassRefs.classObserverInfo, NULL);
652 for (size_t i = 0; i < observersList.size(); i++)
654 jstring jaddress = env->NewStringUTF(observersList[i].address.c_str());
655 jobject jobserver = (jobject) env->NewObject(gSimulatorClassRefs.classObserverInfo,
656 gSimulatorClassRefs.classObserverInfoCtor, observersList[i].id,
657 jaddress, observersList[i].port);
659 env->SetObjectArrayElement(jobserversArray, i, jobserver);
660 env->DeleteLocalRef(jaddress);
664 return jobserversArray;
667 void onObserverChange(jweak jlistenerRef, const std::string &uri,
668 ObservationStatus state, const ObserverInfo &observerInfo)
670 JNIEnv *env = getEnv();
674 jobject observerChangeListener = env->NewLocalRef(jlistenerRef);
675 if (!observerChangeListener)
681 jclass observerChangeCls = env->GetObjectClass(observerChangeListener);
682 if (!observerChangeCls)
688 jmethodID observerChangeMId = env->GetMethodID(observerChangeCls, "onObserverChanged",
689 "(Ljava/lang/String;ILorg/oic/simulator/serviceprovider/ObserverInfo;)V");
690 if (!observerChangeMId)
697 jstring jUri = env->NewStringUTF(uri.c_str());
700 jint jstate = (state == ObservationStatus::OBSERVE_REGISTER) ? 0 : 1;
702 // Construct the java object of observerinfo
703 jstring jaddress = env->NewStringUTF(observerInfo.address.c_str());
704 jobject jobserver = (jobject) env->NewObject(gSimulatorClassRefs.classObserverInfo,
705 gSimulatorClassRefs.classObserverInfoCtor, observerInfo.id,
706 jaddress, observerInfo.port);
708 env->CallVoidMethod(observerChangeListener, observerChangeMId, jUri, jstate, jobserver);
709 env->DeleteLocalRef(jaddress);
710 if ((env)->ExceptionCheck())
716 env->DeleteLocalRef(jUri);
721 JNIEXPORT void JNICALL
722 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_setObserverCallback
723 (JNIEnv *env, jobject object, jobject jcallback)
727 throwInvalidArgsException(env, SIMULATOR_INVALID_CALLBACK, "Callback not set");
731 jweak jlistenerRef = env->NewWeakGlobalRef(jcallback);
732 SimulatorResourceServer::ObserverCB callback = [jlistenerRef](const std::string & uri,
733 ObservationStatus state, const ObserverInfo & observerInfo)
735 onObserverChange(jlistenerRef, uri, state, observerInfo);
738 SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object);
741 throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
745 resource->setObserverCallback(callback);
748 JNIEXPORT void JNICALL
749 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_notifyObserver
750 (JNIEnv *env, jobject object, jint jId)
752 SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object);
755 throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
761 resource->notify(jId);
763 catch (SimulatorException &e)
765 throwSimulatorException(env, e.code(), e.what());
769 throwSimulatorException(env, SIMULATOR_ERROR, "Unknown Exception");
773 JNIEXPORT void JNICALL
774 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_notifyAllObservers
775 (JNIEnv *env, jobject object)
777 SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object);
780 throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
786 resource->notifyAll();
788 catch (SimulatorException &e)
790 throwSimulatorException(env, e.code(), e.what());
794 throwSimulatorException(env, SIMULATOR_ERROR, "Unknown Exception");
798 JNIEXPORT void JNICALL Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_dispose
799 (JNIEnv *env, jobject thiz)
801 JniSimulatorResource *resource = GetHandle<JniSimulatorResource>(env, thiz);