Merge branch 'upstream' into tizen
[platform/upstream/iotivity.git] / service / simulator / java / jni / simulator_single_resource_jni.cpp
1 /******************************************************************
2  *
3  * Copyright 2015 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  ******************************************************************/
20
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"
29
30 #include "simulator_single_resource.h"
31
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;}
34
35 static SimulatorSingleResourceSP simulatorSingleResourceToCpp(JNIEnv *env, jobject object)
36 {
37     JniSharedObjectHolder<SimulatorSingleResource> *jniResource =
38         getHandle<JniSharedObjectHolder<SimulatorSingleResource>>(env, object);
39     if (jniResource)
40         return jniResource->get();
41     return nullptr;
42 }
43
44 static AutoUpdateType autoUpdateTypeToCpp(JNIEnv *env, jobject jType)
45 {
46     static jmethodID ordinalMID = env->GetMethodID(
47                                       gSimulatorClassRefs.autoUpdateTypeCls, "ordinal", "()I");
48
49     int ordinal = env->CallIntMethod(jType, ordinalMID);
50     return AutoUpdateType(ordinal);
51 }
52
53 static void onAutoUpdationComplete(jobject listener, const std::string &uri, const int id)
54 {
55     JNIEnv *env = GetEnv();
56     if (!env)
57         return;
58
59     jclass listenerCls = env->GetObjectClass(listener);
60     jmethodID listenerMethod = env->GetMethodID(listenerCls, "onUpdateComplete",
61                                "(Ljava/lang/String;I)V");
62
63     jstring jUri = env->NewStringUTF(uri.c_str());
64     env->CallVoidMethod(listener, listenerMethod, jUri, id);
65     ReleaseEnv();
66 }
67
68 #ifdef __cplusplus
69 extern "C" {
70 #endif
71
72 JNIEXPORT jobject JNICALL
73 Java_org_oic_simulator_server_SimulatorSingleResource_nativeGetAttribute
74 (JNIEnv *env, jobject object, jstring jAttrName)
75 {
76     VALIDATE_INPUT_RET(env, !jAttrName, "Attribute name is null!", nullptr)
77
78     SimulatorSingleResourceSP singleResource = simulatorSingleResourceToCpp(env, object);
79     VALIDATE_OBJECT_RET(env, singleResource, nullptr)
80
81     JniString jniAttrName(env, jAttrName);
82     SimulatorResourceAttribute attribute;
83     if (singleResource->getAttribute(jniAttrName.get(), attribute))
84         return SimulatorResourceAttributeToJava(env, attribute);
85     return nullptr;
86 }
87
88 JNIEXPORT jobject JNICALL
89 Java_org_oic_simulator_server_SimulatorSingleResource_nativeGetAttributes
90 (JNIEnv *env, jobject object)
91 {
92     SimulatorSingleResourceSP singleResource = simulatorSingleResourceToCpp(env, object);
93     VALIDATE_OBJECT_RET(env, singleResource, nullptr)
94
95     return SimulatorResourceAttributesToJava(env, singleResource->getAttributes());
96 }
97
98 JNIEXPORT jboolean JNICALL
99 Java_org_oic_simulator_server_SimulatorSingleResource_nativeAddAttribute
100 (JNIEnv *env, jobject object, jobject jResAttribute)
101 {
102     VALIDATE_INPUT_RET(env, !jResAttribute, "Resource attribute is null!", false)
103
104     SimulatorSingleResourceSP singleResource = simulatorSingleResourceToCpp(env, object);
105     VALIDATE_OBJECT_RET(env, singleResource, false)
106
107     try
108     {
109         SimulatorResourceAttribute attribute;
110         if (!SimulatorResourceAttributeToCpp(env, jResAttribute, attribute))
111         {
112             ThrowSimulatorException(env, SIMULATOR_ERROR,
113                                     "Failed to covnert SimulatorResourceAttribute java object!");
114             return false;
115         }
116         return singleResource->addAttribute(attribute);
117     }
118     catch (SimulatorException &e)
119     {
120         ThrowSimulatorException(env, e.code(), e.what());
121     }
122
123     return false;
124 }
125
126 JNIEXPORT jboolean JNICALL
127 Java_org_oic_simulator_server_SimulatorSingleResource_nativeUpdateAttribute
128 (JNIEnv *env, jobject object, jstring jAttrName, jobject jAttrValue)
129 {
130     VALIDATE_INPUT_RET(env, !jAttrName, "Attribute name is null!", false)
131     VALIDATE_INPUT_RET(env, !jAttrValue, "Attribute value is null!", false)
132
133     SimulatorSingleResourceSP singleResource = simulatorSingleResourceToCpp(env, object);
134     VALIDATE_OBJECT_RET(env, singleResource, false)
135
136     AttributeValueVariant value;
137     if (!AttributeValueToCpp(env, jAttrValue, value))
138     {
139         ThrowSimulatorException(env, SIMULATOR_ERROR,
140                                 "Failed to covnert AttributeValue java object!");
141         return false;
142     }
143
144     SimulatorResourceAttribute attribute(JniString(env, jAttrName).get());
145     attribute.setValue(value);
146     return singleResource->updateAttributeValue(attribute);
147 }
148
149 JNIEXPORT jboolean JNICALL
150 Java_org_oic_simulator_server_SimulatorSingleResource_nativeRemoveAttribute
151 (JNIEnv *env, jobject object, jstring jAttrName)
152 {
153     VALIDATE_INPUT_RET(env, !jAttrName, "Attribute name is null!", false)
154
155     SimulatorSingleResourceSP singleResource = simulatorSingleResourceToCpp(env, object);
156     VALIDATE_OBJECT_RET(env, singleResource, false)
157
158     try
159     {
160         JniString jniAttrName(env, jAttrName);
161         return singleResource->removeAttribute(jniAttrName.get());
162     }
163     catch (InvalidArgsException &e)
164     {
165         ThrowInvalidArgsException(env, e.code(), e.what());
166     }
167
168     return false;
169 }
170
171 JNIEXPORT jint JNICALL
172 Java_org_oic_simulator_server_SimulatorSingleResource_nativeStartResourceUpdation
173 (JNIEnv *env, jobject object, jobject jType, jint jInterval, jobject jListener)
174 {
175     VALIDATE_CALLBACK_RET(env, jListener, -1)
176
177     SimulatorSingleResourceSP singleResource = simulatorSingleResourceToCpp(env, object);
178     VALIDATE_OBJECT_RET(env, singleResource, -1)
179
180     jobject listenerRef = env->NewGlobalRef(jListener);
181     SimulatorSingleResource::AutoUpdateCompleteCallback callback =  [listenerRef](
182                 const std::string & uri, const int id)
183     {
184         onAutoUpdationComplete(listenerRef, uri, id);
185     };
186
187     try
188     {
189         AutoUpdateType autoUpdateType = autoUpdateTypeToCpp(env, jType);
190         return singleResource->startResourceUpdation(autoUpdateType, jInterval, callback);
191     }
192     catch (InvalidArgsException &e)
193     {
194         ThrowInvalidArgsException(env, e.code(), e.what());
195     }
196     catch (SimulatorException &e)
197     {
198         ThrowSimulatorException(env, e.code(), e.what());
199     }
200
201     return -1;
202 }
203
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)
207 {
208     VALIDATE_INPUT_RET(env, !jAttrName, "Attribute name is null!", -1)
209     VALIDATE_CALLBACK_RET(env, jListener, -1)
210
211     SimulatorSingleResourceSP singleResource = simulatorSingleResourceToCpp(env, object);
212     VALIDATE_OBJECT_RET(env, singleResource, -1)
213
214     jobject listenerRef = env->NewGlobalRef(jListener);
215     SimulatorSingleResource::AutoUpdateCompleteCallback callback =
216         [listenerRef](const std::string & uri, const int id)
217     {
218         onAutoUpdationComplete(listenerRef, uri, id);
219     };
220
221     try
222     {
223         JniString jniAttrName(env, jAttrName);
224         AutoUpdateType autoUpdateType = autoUpdateTypeToCpp(env, jType);
225         return singleResource->startAttributeUpdation(jniAttrName.get(), autoUpdateType,
226                 jInterval, callback);
227     }
228     catch (InvalidArgsException &e)
229     {
230         ThrowInvalidArgsException(env, e.code(), e.what());
231     }
232     catch (SimulatorException &e)
233     {
234         ThrowSimulatorException(env, e.code(), e.what());
235     }
236
237     return -1;
238 }
239
240 JNIEXPORT void JNICALL
241 Java_org_oic_simulator_server_SimulatorSingleResource_nativeStopUpdation
242 (JNIEnv *env, jobject object, jint id)
243 {
244     SimulatorSingleResourceSP singleResource = simulatorSingleResourceToCpp(env, object);
245     VALIDATE_OBJECT(env, singleResource)
246
247     singleResource->stopUpdation(id);
248 }
249
250 JNIEXPORT void JNICALL
251 Java_org_oic_simulator_server_SimulatorSingleResource_nativeDispose
252 (JNIEnv *env, jobject object)
253 {
254     JniSharedObjectHolder<SimulatorSingleResource> *resource =
255         getHandle<JniSharedObjectHolder<SimulatorSingleResource>>(env, object);
256     delete resource;
257 }
258
259 #ifdef __cplusplus
260 }
261 #endif