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_model_jni.h"
22 #include "simulator_exceptions_jni.h"
23 #include "simulator_utils_jni.h"
24 #include "jni_sharedobject_holder.h"
25 #include "jni_listener_holder.h"
26 #include "jni_string.h"
27 #include "jni_vector.h"
29 #include "simulator_single_resource.h"
31 #define VALIDATE_OBJECT(ENV, OBJECT) if (!OBJECT){throwBadObjectException(ENV, "No corresponsing native object!"); return;}
32 #define VALIDATE_OBJECT_RET(ENV, OBJECT, RET) if (!OBJECT){throwBadObjectException(ENV, "No corresponsing native object!"); return RET;}
34 SimulatorSingleResourceSP simulatorSingleResourceToCpp(JNIEnv *env, jobject object)
36 JniSharedObjectHolder<SimulatorSingleResource> *jniResource =
37 GetHandle<JniSharedObjectHolder<SimulatorSingleResource>>(env, object);
39 return jniResource->get();
43 static AutomationType AutomationTypeToCpp(JNIEnv *env, jobject jType)
45 static jmethodID ordinalMID = env->GetMethodID(
46 gSimulatorClassRefs.automationTypeCls, "ordinal", "()I");
48 int ordinal = env->CallIntMethod(jType, ordinalMID);
49 return AutomationType(ordinal);
52 static void onAutoUpdationComplete(jobject listener, const std::string &uri, const int id)
54 JNIEnv *env = getEnv();
58 jclass listenerCls = env->GetObjectClass(listener);
59 jmethodID listenerMethod = env->GetMethodID(listenerCls, "onUpdateComplete",
60 "(Ljava/lang/String;I)V");
62 jstring jUri = env->NewStringUTF(uri.c_str());
63 env->CallVoidMethod(listener, listenerMethod, jUri, id);
71 JNIEXPORT jobject JNICALL
72 Java_org_oic_simulator_server_SimulatorSingleResource_getAttribute
73 (JNIEnv *env, jobject object, jstring attrName)
75 VALIDATE_INPUT_RET(env, !attrName, "Attribute name is null!", nullptr)
77 SimulatorSingleResourceSP singleResource = simulatorSingleResourceToCpp(env, object);
78 VALIDATE_OBJECT_RET(env, singleResource, nullptr)
80 JniString jniAttrName(env, attrName);
81 SimulatorResourceModel::Attribute attribute;
82 if (singleResource->getAttribute(jniAttrName.get(), attribute))
83 return simulatorResourceAttributeToJava(env, attribute);
87 JNIEXPORT void JNICALL
88 Java_org_oic_simulator_server_SimulatorSingleResource_addAttribute
89 (JNIEnv *env, jobject object, jobject resAttribute)
91 VALIDATE_INPUT(env, !resAttribute, "Resource attribute is null!")
93 SimulatorSingleResourceSP singleResource = simulatorSingleResourceToCpp(env, object);
94 VALIDATE_OBJECT(env, singleResource)
98 SimulatorResourceModel::Attribute attribute;
99 if (!simulatorResourceAttributeToCpp(env, resAttribute, attribute))
101 throwSimulatorException(env, SIMULATOR_ERROR,
102 "Failed to covnert SimulatorResourceAttribute java object!");
106 singleResource->addAttribute(attribute);
108 catch (SimulatorException &e)
110 throwSimulatorException(env, e.code(), e.what());
114 JNIEXPORT void JNICALL
115 Java_org_oic_simulator_server_SimulatorSingleResource_updateAttribute
116 (JNIEnv *env, jobject object, jstring attrName, jobject attrValue)
118 VALIDATE_INPUT(env, !attrName, "Attribute name is null!")
119 VALIDATE_INPUT(env, !attrValue, "Attribute value is null!")
121 SimulatorSingleResourceSP singleResource = simulatorSingleResourceToCpp(env, object);
122 VALIDATE_OBJECT(env, singleResource)
124 SimulatorResourceModel::ValueVariant value;
125 if (!AttributeValueToCpp(env, attrValue, value))
127 throwSimulatorException(env, SIMULATOR_ERROR,
128 "Failed to covnert AttributeValue java object!");
132 SimulatorResourceModel::Attribute attribute(JniString(env, attrName).get());
133 attribute.setValue(value);
134 singleResource->updateAttributeValue(attribute);
137 JNIEXPORT void JNICALL
138 Java_org_oic_simulator_server_SimulatorSingleResource_removeAttribute
139 (JNIEnv *env, jobject object, jstring attrName)
141 VALIDATE_INPUT(env, !attrName, "Attribute name is null!")
143 SimulatorSingleResourceSP singleResource = simulatorSingleResourceToCpp(env, object);
144 VALIDATE_OBJECT(env, singleResource)
148 JniString jniAttrName(env, attrName);
149 singleResource->removeAttribute(jniAttrName.get());
151 catch (InvalidArgsException &e)
153 throwInvalidArgsException(env, e.code(), e.what());
157 JNIEXPORT jint JNICALL
158 Java_org_oic_simulator_server_SimulatorSingleResource_startResourceUpdation
159 (JNIEnv *env, jobject object, jint type, jint interval, jobject listener)
161 VALIDATE_CALLBACK_RET(env, listener, -1)
163 SimulatorSingleResourceSP singleResource = simulatorSingleResourceToCpp(env, object);
164 VALIDATE_OBJECT_RET(env, singleResource, -1)
166 jobject listenerRef = env->NewGlobalRef(listener);
167 updateCompleteCallback callback = [listenerRef](const std::string & uri, const int id)
169 onAutoUpdationComplete(listenerRef, uri, id);
174 int id = singleResource->startResourceUpdation((1 == type) ? AutomationType::RECURRENT :
175 AutomationType::NORMAL, interval, callback);
178 catch (InvalidArgsException &e)
180 throwInvalidArgsException(env, e.code(), e.what());
182 catch (SimulatorException &e)
184 throwSimulatorException(env, e.code(), e.what());
190 JNIEXPORT jint JNICALL
191 Java_org_oic_simulator_server_SimulatorSingleResource_startAttributeUpdation
192 (JNIEnv *env, jobject object, jstring attrName, jobject type, jint interval, jobject listener)
194 VALIDATE_INPUT_RET(env, !attrName, "Attribute name is null!", -1)
195 VALIDATE_CALLBACK_RET(env, listener, -1)
197 SimulatorSingleResourceSP singleResource = simulatorSingleResourceToCpp(env, object);
198 VALIDATE_OBJECT_RET(env, singleResource, -1)
200 jobject listenerRef = env->NewGlobalRef(listener);
201 updateCompleteCallback callback = [listenerRef](const std::string & uri, const int id)
203 onAutoUpdationComplete(listenerRef, uri, id);
206 JniString jniAttrName(env, attrName);
207 AutomationType automationType = AutomationTypeToCpp(env, type);
211 int id = singleResource->startAttributeUpdation(jniAttrName.get(), automationType,
215 catch (InvalidArgsException &e)
217 throwInvalidArgsException(env, e.code(), e.what());
219 catch (SimulatorException &e)
221 throwSimulatorException(env, e.code(), e.what());
227 JNIEXPORT void JNICALL
228 Java_org_oic_simulator_server_SimulatorSingleResource_stopUpdation
229 (JNIEnv *env, jobject object, jint id)
231 SimulatorSingleResourceSP singleResource = simulatorSingleResourceToCpp(env, object);
232 VALIDATE_OBJECT(env, singleResource)
234 singleResource->stopUpdation(id);
237 JNIEXPORT void JNICALL
238 Java_org_oic_simulator_server_SimulatorSingleResource_dispose
239 (JNIEnv *env, jobject object)
241 JniSharedObjectHolder<SimulatorSingleResource> *resource =
242 GetHandle<JniSharedObjectHolder<SimulatorSingleResource>>(env, object);