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_resource_attribute_jni.h"
23 #include "simulator_exceptions_jni.h"
24 #include "simulator_utils_jni.h"
25 #include "jni_sharedobject_holder.h"
26 #include "jni_listener_holder.h"
27 #include "jni_string.h"
28 #include "jni_vector.h"
30 #include "simulator_single_resource.h"
32 #define VALIDATE_OBJECT(ENV, OBJECT) if (!OBJECT){ThrowBadObjectException(ENV, "No corresponsing native object!"); return;}
33 #define VALIDATE_OBJECT_RET(ENV, OBJECT, RET) if (!OBJECT){ThrowBadObjectException(ENV, "No corresponsing native object!"); return RET;}
35 static SimulatorSingleResourceSP simulatorSingleResourceToCpp(JNIEnv *env, jobject object)
37 JniSharedObjectHolder<SimulatorSingleResource> *jniResource =
38 getHandle<JniSharedObjectHolder<SimulatorSingleResource>>(env, object);
40 return jniResource->get();
44 static AutoUpdateType autoUpdateTypeToCpp(JNIEnv *env, jobject jType)
46 static jmethodID ordinalMID = env->GetMethodID(
47 gSimulatorClassRefs.autoUpdateTypeCls, "ordinal", "()I");
49 int ordinal = env->CallIntMethod(jType, ordinalMID);
50 return AutoUpdateType(ordinal);
53 static void onAutoUpdationComplete(jobject listener, const std::string &uri, const int id)
55 JNIEnv *env = GetEnv();
59 jclass listenerCls = env->GetObjectClass(listener);
60 jmethodID listenerMethod = env->GetMethodID(listenerCls, "onUpdateComplete",
61 "(Ljava/lang/String;I)V");
63 jstring jUri = env->NewStringUTF(uri.c_str());
64 env->CallVoidMethod(listener, listenerMethod, jUri, id);
72 JNIEXPORT jobject JNICALL
73 Java_org_oic_simulator_server_SimulatorSingleResource_nativeGetAttribute
74 (JNIEnv *env, jobject object, jstring jAttrName)
76 VALIDATE_INPUT_RET(env, !jAttrName, "Attribute name is null!", nullptr)
78 SimulatorSingleResourceSP singleResource = simulatorSingleResourceToCpp(env, object);
79 VALIDATE_OBJECT_RET(env, singleResource, nullptr)
81 JniString jniAttrName(env, jAttrName);
82 SimulatorResourceAttribute attribute;
83 if (singleResource->getAttribute(jniAttrName.get(), attribute))
84 return SimulatorResourceAttributeToJava(env, attribute);
88 JNIEXPORT jobject JNICALL
89 Java_org_oic_simulator_server_SimulatorSingleResource_nativeGetAttributes
90 (JNIEnv *env, jobject object)
92 SimulatorSingleResourceSP singleResource = simulatorSingleResourceToCpp(env, object);
93 VALIDATE_OBJECT_RET(env, singleResource, nullptr)
95 return SimulatorResourceAttributesToJava(env, singleResource->getAttributes());
98 JNIEXPORT jboolean JNICALL
99 Java_org_oic_simulator_server_SimulatorSingleResource_nativeAddAttribute
100 (JNIEnv *env, jobject object, jobject jResAttribute)
102 VALIDATE_INPUT_RET(env, !jResAttribute, "Resource attribute is null!", false)
104 SimulatorSingleResourceSP singleResource = simulatorSingleResourceToCpp(env, object);
105 VALIDATE_OBJECT_RET(env, singleResource, false)
109 SimulatorResourceAttribute attribute;
110 if (!SimulatorResourceAttributeToCpp(env, jResAttribute, attribute))
112 ThrowSimulatorException(env, SIMULATOR_ERROR,
113 "Failed to covnert SimulatorResourceAttribute java object!");
116 return singleResource->addAttribute(attribute);
118 catch (SimulatorException &e)
120 ThrowSimulatorException(env, e.code(), e.what());
126 JNIEXPORT jboolean JNICALL
127 Java_org_oic_simulator_server_SimulatorSingleResource_nativeUpdateAttribute
128 (JNIEnv *env, jobject object, jstring jAttrName, jobject jAttrValue)
130 VALIDATE_INPUT_RET(env, !jAttrName, "Attribute name is null!", false)
131 VALIDATE_INPUT_RET(env, !jAttrValue, "Attribute value is null!", false)
133 SimulatorSingleResourceSP singleResource = simulatorSingleResourceToCpp(env, object);
134 VALIDATE_OBJECT_RET(env, singleResource, false)
136 AttributeValueVariant value;
137 if (!AttributeValueToCpp(env, jAttrValue, value))
139 ThrowSimulatorException(env, SIMULATOR_ERROR,
140 "Failed to covnert AttributeValue java object!");
144 SimulatorResourceAttribute attribute(JniString(env, jAttrName).get());
145 attribute.setValue(value);
146 return singleResource->updateAttributeValue(attribute);
149 JNIEXPORT jboolean JNICALL
150 Java_org_oic_simulator_server_SimulatorSingleResource_nativeRemoveAttribute
151 (JNIEnv *env, jobject object, jstring jAttrName)
153 VALIDATE_INPUT_RET(env, !jAttrName, "Attribute name is null!", false)
155 SimulatorSingleResourceSP singleResource = simulatorSingleResourceToCpp(env, object);
156 VALIDATE_OBJECT_RET(env, singleResource, false)
160 JniString jniAttrName(env, jAttrName);
161 return singleResource->removeAttribute(jniAttrName.get());
163 catch (InvalidArgsException &e)
165 ThrowInvalidArgsException(env, e.code(), e.what());
171 JNIEXPORT jint JNICALL
172 Java_org_oic_simulator_server_SimulatorSingleResource_nativeStartResourceUpdation
173 (JNIEnv *env, jobject object, jobject jType, jint jInterval, jobject jListener)
175 VALIDATE_CALLBACK_RET(env, jListener, -1)
177 SimulatorSingleResourceSP singleResource = simulatorSingleResourceToCpp(env, object);
178 VALIDATE_OBJECT_RET(env, singleResource, -1)
180 jobject listenerRef = env->NewGlobalRef(jListener);
181 SimulatorSingleResource::AutoUpdateCompleteCallback callback = [listenerRef](
182 const std::string & uri, const int id)
184 onAutoUpdationComplete(listenerRef, uri, id);
189 AutoUpdateType autoUpdateType = autoUpdateTypeToCpp(env, jType);
190 return singleResource->startResourceUpdation(autoUpdateType, jInterval, callback);
192 catch (InvalidArgsException &e)
194 ThrowInvalidArgsException(env, e.code(), e.what());
196 catch (SimulatorException &e)
198 ThrowSimulatorException(env, e.code(), e.what());
204 JNIEXPORT jint JNICALL
205 Java_org_oic_simulator_server_SimulatorSingleResource_nativeStartAttributeUpdation
206 (JNIEnv *env, jobject object, jstring jAttrName, jobject jType, jint jInterval, jobject jListener)
208 VALIDATE_INPUT_RET(env, !jAttrName, "Attribute name is null!", -1)
209 VALIDATE_CALLBACK_RET(env, jListener, -1)
211 SimulatorSingleResourceSP singleResource = simulatorSingleResourceToCpp(env, object);
212 VALIDATE_OBJECT_RET(env, singleResource, -1)
214 jobject listenerRef = env->NewGlobalRef(jListener);
215 SimulatorSingleResource::AutoUpdateCompleteCallback callback =
216 [listenerRef](const std::string & uri, const int id)
218 onAutoUpdationComplete(listenerRef, uri, id);
223 JniString jniAttrName(env, jAttrName);
224 AutoUpdateType autoUpdateType = autoUpdateTypeToCpp(env, jType);
225 return singleResource->startAttributeUpdation(jniAttrName.get(), autoUpdateType,
226 jInterval, callback);
228 catch (InvalidArgsException &e)
230 ThrowInvalidArgsException(env, e.code(), e.what());
232 catch (SimulatorException &e)
234 ThrowSimulatorException(env, e.code(), e.what());
240 JNIEXPORT void JNICALL
241 Java_org_oic_simulator_server_SimulatorSingleResource_nativeStopUpdation
242 (JNIEnv *env, jobject object, jint id)
244 SimulatorSingleResourceSP singleResource = simulatorSingleResourceToCpp(env, object);
245 VALIDATE_OBJECT(env, singleResource)
247 singleResource->stopUpdation(id);
250 JNIEXPORT void JNICALL
251 Java_org_oic_simulator_server_SimulatorSingleResource_nativeDispose
252 (JNIEnv *env, jobject object)
254 JniSharedObjectHolder<SimulatorSingleResource> *resource =
255 getHandle<JniSharedObjectHolder<SimulatorSingleResource>>(env, object);