2143ee61826158f37494860674664266dc9053fa
[platform/upstream/iotivity.git] / service / simulator / java / jni / simulator_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_jni.h"
22 #include "simulator_resource_jni_util.h"
23 #include "simulator_common_jni.h"
24 #include "simulator_resource_model_jni.h"
25
26 extern SimulatorClassRefs gSimulatorClassRefs;
27
28 JniSimulatorResource::JniSimulatorResource(SimulatorResourcePtr &resource)
29     : m_sharedResource(resource) {}
30
31 SimulatorResourcePtr JniSimulatorResource::getJniSimulatorResourcePtr(JNIEnv *env, jobject thiz)
32 {
33     JniSimulatorResource *resource = GetHandle<JniSimulatorResource>(env, thiz);
34     if (env->ExceptionCheck())
35     {
36         return NULL;
37     }
38     return resource->m_sharedResource;
39 }
40
41 jobject JniSimulatorResource::toJava(JNIEnv *env, jlong resource)
42 {
43     jobject resourceObj = (jobject) env->NewObject(gSimulatorClassRefs.classSimulatorResource,
44                           gSimulatorClassRefs.classSimulatorResourceCtor, resource);
45     if (NULL == resourceObj)
46     {
47         return NULL;
48     }
49     return resourceObj;
50 }
51
52 void JniSimulatorResource::setUri(JNIEnv *env, jobject jobj, const std::string &uri)
53 {
54     if (!env || !jobj)
55         return;
56
57     jstring jURI = env->NewStringUTF(uri.c_str());
58     if (!jURI)
59         return;
60
61     env->CallVoidMethod(jobj, gSimulatorClassRefs.classSimulatorResourceSetURI, jURI);
62     env->DeleteLocalRef(jURI);
63 }
64
65 void JniSimulatorResource::setResourceType(JNIEnv *env, jobject jobj,
66         const std::string &resourceType)
67 {
68     if (!env || !jobj)
69         return;
70
71     jstring jResourceType = env->NewStringUTF(resourceType.c_str());
72     if (!jResourceType)
73         return;
74
75     env->CallVoidMethod(jobj, gSimulatorClassRefs.classSimulatorResourceSetResourceType, jResourceType);
76     env->DeleteLocalRef(jResourceType);
77 }
78
79 void JniSimulatorResource::setResourceName(JNIEnv *env, jobject jobj, const std::string &name)
80 {
81     if (!env || !jobj)
82         return;
83
84     jstring jName = env->NewStringUTF(name.c_str());
85     if (!jName)
86         return;
87
88     env->CallVoidMethod(jobj, gSimulatorClassRefs.classSimulatorResourceSetName, jName);
89     env->DeleteLocalRef(jName);
90 }
91
92 JNIEXPORT jobject JNICALL
93 Java_org_iotivity_simulator_SimulatorResourceServer_getModel
94 (JNIEnv *env, jobject object)
95 {
96     SimulatorResourcePtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, object);
97     if (nullptr == resource.get())
98     {
99         std::cout << "getModel: Resource is NULL";
100         return nullptr;
101     }
102
103     SimulatorResourceModel resModel = resource->getModel();
104     JniSimulatorResourceModel *model = new JniSimulatorResourceModel(resModel);
105     jobject jModel = JniSimulatorResourceModel::toJava(env, reinterpret_cast<jlong>(model));
106     return jModel;
107 }
108
109 JNIEXPORT void JNICALL
110 Java_org_iotivity_simulator_SimulatorResourceServer_updateAttributeFromAllowedValues
111 (JNIEnv *env, jobject object, jstring attrName, jint index)
112 {
113     SimulatorResourcePtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, object);
114     if (nullptr == resource.get())
115     {
116         std::cout << "updateAttributeFromAllowedValues: Resource is NULL";
117         return;
118     }
119
120     const char *attrNamePtr = env->GetStringUTFChars(attrName, NULL);
121     if (!attrNamePtr)
122     {
123         std::cout << "updateAttributeFromAllowedValues: Failed to convert jstring to char string!";
124         return;
125     }
126
127     resource->updateAttributeFromAllowedValues(attrNamePtr, static_cast<int>(index));
128     env->ReleaseStringUTFChars(attrName, attrNamePtr);
129 }
130
131 JNIEXPORT void JNICALL
132 Java_org_iotivity_simulator_SimulatorResourceServer_setRange
133 (JNIEnv *env, jobject object, jstring attrName, jint min, jint max)
134 {
135     SimulatorResourcePtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, object);
136     if (nullptr == resource.get())
137     {
138         std::cout << "setRange: Resource is NULL";
139         return;
140     }
141
142     const char *attrNamePtr = env->GetStringUTFChars(attrName, NULL);
143     if (!attrNamePtr)
144     {
145         std::cout << "setRange: Failed to convert jstring to char string!";
146         return;
147     }
148
149     resource->setRange(attrNamePtr, static_cast<int>(min), static_cast<int>(max));
150     env->ReleaseStringUTFChars(attrName, attrNamePtr);
151 }
152
153 JNIEXPORT void JNICALL
154 Java_org_iotivity_simulator_SimulatorResourceServer_setInterfaceType
155 (JNIEnv *env, jobject jobject, const std::string &interfaceType)
156 {
157     jstring jInterfaceType = env->NewStringUTF(interfaceType.c_str());
158     if (!jInterfaceType)
159     {
160         std::cout << "setInterfaceType: InterfaceType is NULL";
161         return;
162     }
163
164     env->CallVoidMethod(jobject, gSimulatorClassRefs.classSimulatorResourceSetInterfaceType,
165                         jInterfaceType);
166     env->DeleteLocalRef(jInterfaceType);
167 }
168
169 JNIEXPORT void JNICALL Java_org_iotivity_simulator_SimulatorResourceServer_addAttributeInteger
170 (JNIEnv *env, jobject jobject, jstring jKey, jint jValue)
171 {
172     if (!jKey)
173     {
174         std::cout << "addAttributeInteger: AttributeName is Empty";
175         return;
176     }
177
178     SimulatorResourcePtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, jobject);
179     if (nullptr == resource.get())
180     {
181         std::cout << "addAttributeInteger: Resource is NULL";
182         return;
183     }
184
185     std::string str = env->GetStringUTFChars(jKey, NULL);
186     resource->addAttribute(str, static_cast<int>(jValue));
187 }
188
189 JNIEXPORT void JNICALL Java_org_iotivity_simulator_SimulatorResourceServer_addAttributeDouble
190 (JNIEnv *env, jobject jobject, jstring jKey, jdouble jValue)
191 {
192     if (!jKey)
193     {
194         std::cout << "addAttributeDouble: AttributeName is Empty";
195         return;
196     }
197
198     SimulatorResourcePtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, jobject);
199     if (nullptr == resource.get())
200     {
201         std::cout << "addAttributeDouble: Resource is NULL";
202         return;
203     }
204
205     std::string str = env->GetStringUTFChars(jKey, NULL);
206     resource->addAttribute(str, static_cast<double>(jValue));
207 }
208
209 JNIEXPORT void JNICALL Java_org_iotivity_simulator_SimulatorResourceServer_addAttributeBoolean
210 (JNIEnv *env, jobject jobject, jstring jKey, jboolean jValue)
211 {
212     if (!jKey)
213     {
214         std::cout << "addAttributeBoolean: AttributeName is Empty";
215         return;
216     }
217
218     SimulatorResourcePtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, jobject);
219     if (nullptr == resource.get())
220     {
221         std::cout << "addAttributeBoolean: Resource is NULL";
222         return;
223     }
224
225     std::string str = env->GetStringUTFChars(jKey, NULL);
226     resource->addAttribute(str, static_cast<bool>(jValue));
227 }
228
229 JNIEXPORT void JNICALL Java_org_iotivity_simulator_SimulatorResourceServer_addAttributeStringN
230 (JNIEnv *env, jobject jobject, jstring jKey, jstring jValue)
231 {
232     if (!jKey)
233     {
234         std::cout << "addAttributeStringN: AttributeName is Empty";
235         return;
236     }
237
238     SimulatorResourcePtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, jobject);
239     if (nullptr == resource.get())
240     {
241         std::cout << "addAttributeStringN: Resource is NULL";
242         return;
243     }
244
245     std::string key = env->GetStringUTFChars(jKey, NULL);
246     std::string value = env->GetStringUTFChars(jValue, NULL);
247
248     resource->addAttribute(key, value);
249 }
250
251 JNIEXPORT void JNICALL Java_org_iotivity_simulator_SimulatorResourceServer_updateAttributeInteger
252 (JNIEnv *env, jobject jobject, jstring jKey, jint jValue)
253 {
254     if (!jKey)
255     {
256         std::cout << "updateAttributeInteger: AttributeName is Empty";
257         return;
258     }
259
260     SimulatorResourcePtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, jobject);
261     if (nullptr == resource.get())
262     {
263         std::cout << "updateAttributeInteger: Resource is NULL";
264         return;
265     }
266
267     std::string str = env->GetStringUTFChars(jKey, NULL);
268     resource->updateAttribute(str, static_cast<int>(jValue));
269 }
270
271 JNIEXPORT void JNICALL Java_org_iotivity_simulator_SimulatorResourceServer_updateAttributeDouble
272 (JNIEnv *env, jobject jobject, jstring jKey, jdouble jValue)
273 {
274     if (!jKey)
275     {
276         std::cout << "updateAttributeDouble: AttributeName is Empty";
277         return;
278     }
279
280     SimulatorResourcePtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, jobject);
281     if (nullptr == resource.get())
282     {
283         std::cout << "updateAttributeDouble: Resource is NULL";
284         return;
285     }
286
287     std::string str = env->GetStringUTFChars(jKey, NULL);
288     resource->updateAttribute(str, static_cast<double>(jValue));
289 }
290
291 JNIEXPORT void JNICALL Java_org_iotivity_simulator_SimulatorResourceServer_updateAttributeBoolean
292 (JNIEnv *env, jobject jobject, jstring jKey, jboolean jValue)
293 {
294     if (!jKey)
295     {
296         std::cout << "updateAttributeBoolean: AttributeName is Empty";
297         return;
298     }
299
300     SimulatorResourcePtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, jobject);
301     if (nullptr == resource.get())
302     {
303         std::cout << "updateAttributeBoolean: Resource is NULL";
304         return;
305     }
306
307     std::string str = env->GetStringUTFChars(jKey, NULL);
308     resource->updateAttribute(str, static_cast<bool>(jValue));
309 }
310
311 JNIEXPORT void JNICALL Java_org_iotivity_simulator_SimulatorResourceServer_updateAttributeStringN
312 (JNIEnv *env, jobject jobject, jstring jKey, jstring jValue)
313 {
314     if (!jKey)
315     {
316         std::cout << "updateAttributeStringN: AttributeName is Empty";
317         return;
318     }
319
320     SimulatorResourcePtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, jobject);
321     if (nullptr == resource.get())
322     {
323         std::cout << "updateAttributeStringN: Resource is NULL";
324         return;
325     }
326
327     std::string key = env->GetStringUTFChars(jKey, NULL);
328     std::string value = env->GetStringUTFChars(jValue, NULL);
329
330     resource->updateAttribute(key, value);
331 }
332
333 JNIEXPORT void JNICALL Java_org_iotivity_simulator_SimulatorResourceServer_setAllowedValuesInteger
334 (JNIEnv *env, jobject object, jstring jKey, jobject jAllowedValues)
335 {
336     if (!jKey)
337     {
338         std::cout << "setAllowedValuesInteger: AttributeName is Empty";
339         return;
340     }
341
342     SimulatorResourcePtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, object);
343     if (nullptr == resource.get())
344     {
345         std::cout << "setAllowedValuesInteger: Resource is NULL";
346         return;
347     }
348
349     std::string str = env->GetStringUTFChars(jKey, NULL);
350     resource->setAllowedValues(str, convertIntegerVector(env, jAllowedValues));
351 }
352
353 JNIEXPORT void JNICALL Java_org_iotivity_simulator_SimulatorResourceServer_setAllowedValuesDouble
354 (JNIEnv *env, jobject object, jstring jKey, jobject jAllowedValues)
355 {
356     if (!jKey)
357     {
358         std::cout << "setAllowedValuesDouble: AttributeName is Empty";
359         return;
360     }
361
362     SimulatorResourcePtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, object);
363     if (nullptr == resource.get())
364     {
365         std::cout << "setAllowedValuesDouble: Resource is NULL";
366         return;
367     }
368
369     std::string str = env->GetStringUTFChars(jKey, NULL);
370     resource->setAllowedValues(str, convertDoubleVector(env, jAllowedValues));
371 }
372
373 JNIEXPORT void JNICALL Java_org_iotivity_simulator_SimulatorResourceServer_setAllowedValuesStringN
374 (JNIEnv *env, jobject object, jstring jKey, jobject jAllowedValues)
375 {
376     if (!jKey)
377     {
378         std::cout << "setAllowedValuesStringN: AttributeName is Empty";
379         return;
380     }
381
382     SimulatorResourcePtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, object);
383     if (nullptr == resource.get())
384     {
385         std::cout << "setAllowedValuesStringN: Resource is NULL";
386         return;
387     }
388
389     std::string str = env->GetStringUTFChars(jKey, NULL);
390     resource->setAllowedValues(str, convertStringVector(env, jAllowedValues));
391 }
392
393 JNIEXPORT jint JNICALL
394 Java_org_iotivity_simulator_SimulatorResourceServer_startResourceAutomation
395 (JNIEnv *env, jobject object)
396 {
397     SimulatorResourcePtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, object);
398     if (nullptr == resource.get())
399     {
400         return -1;
401     }
402
403     int automationId;
404     if (SIMULATOR_SUCCESS != resource->startUpdateAutomation(AutomationType::NORMAL, automationId))
405         return -1;
406
407     return automationId;
408 }
409
410 JNIEXPORT jint JNICALL
411 Java_org_iotivity_simulator_SimulatorResourceServer_startAttributeAutomation
412 (JNIEnv *env, jobject object, jstring attrName)
413 {
414     SimulatorResourcePtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, object);
415     if (nullptr == resource.get())
416     {
417         return -1;
418     }
419
420     const char *attrNamePtr = env->GetStringUTFChars(attrName, NULL);
421
422     int automationId = -1;
423     resource->startUpdateAutomation(AutomationType::NORMAL, automationId);
424
425     env->ReleaseStringUTFChars(attrName, attrNamePtr);
426     return automationId;
427 }
428
429 JNIEXPORT void JNICALL
430 Java_org_iotivity_simulator_SimulatorResourceServer_startAutomation
431 (JNIEnv *env, jobject object, jint automationId)
432 {
433     SimulatorResourcePtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, object);
434     if (nullptr == resource.get())
435     {
436         return;
437     }
438
439     resource->stopUpdateAutomation(automationId);
440 }
441
442 JNIEXPORT void JNICALL Java_org_iotivity_simulator_SimulatorResourceServer_removeAttribute
443 (JNIEnv *env, jobject jobject, jstring jKey)
444 {
445     if (!jKey)
446     {
447         std::cout << "removeAttribute: AttributeName is Empty";
448         return;
449     }
450
451     SimulatorResourcePtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, jobject);
452     if (nullptr == resource.get())
453     {
454         std::cout << "removeAttribute: Resource is NULL";
455         return;
456     }
457
458     std::string str = env->GetStringUTFChars(jKey, NULL);
459     resource->removeAttribute(str);
460 }
461
462 JNIEXPORT void JNICALL Java_org_iotivity_simulator_SimulatorResourceServer_dispose
463 (JNIEnv *env, jobject thiz)
464 {
465     JniSimulatorResource *resource = GetHandle<JniSimulatorResource>(env, thiz);
466     delete resource;
467 }
468