Updating JNI modules of service provider
[platform/upstream/iotivity.git] / service / simulator / java / jni / simulator_resource_server_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_server_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(SimulatorResourceServerPtr &resource)
29     : m_sharedResource(resource) {}
30
31 SimulatorResourceServerPtr JniSimulatorResource::getJniSimulatorResourcePtr(JNIEnv *env,
32         jobject thiz)
33 {
34     JniSimulatorResource *resource = GetHandle<JniSimulatorResource>(env, thiz);
35     if (env->ExceptionCheck())
36     {
37         return NULL;
38     }
39     return resource->m_sharedResource;
40 }
41
42 jobject JniSimulatorResource::toJava(JNIEnv *env, jlong resource)
43 {
44     jobject resourceObj = (jobject) env->NewObject(gSimulatorClassRefs.classSimulatorResource,
45                           gSimulatorClassRefs.classSimulatorResourceCtor, resource);
46     if (NULL == resourceObj)
47     {
48         return NULL;
49     }
50     return resourceObj;
51 }
52
53 void JniSimulatorResource::setResourceInfo(JNIEnv *env, jobject jobj)
54 {
55     if (!env || !jobj)
56         return;
57
58     std::string uri = m_sharedResource->getURI();
59     std::string resourceType = m_sharedResource->getResourceType();
60     std::string name = m_sharedResource->getName();
61     std::string interfaceType = m_sharedResource->getInterfaceType();
62
63     jstring jURI = env->NewStringUTF(uri.c_str());
64     if (jURI)
65     {
66         env->CallVoidMethod(jobj, gSimulatorClassRefs.classSimulatorResourceSetURI, jURI);
67         env->DeleteLocalRef(jURI);
68     }
69
70     jstring jResourceType = env->NewStringUTF(resourceType.c_str());
71     if (jResourceType)
72     {
73         env->CallVoidMethod(jobj, gSimulatorClassRefs.classSimulatorResourceSetResourceType, jResourceType);
74         env->DeleteLocalRef(jResourceType);
75     }
76
77     jstring jName = env->NewStringUTF(name.c_str());
78     if (jName)
79     {
80         env->CallVoidMethod(jobj, gSimulatorClassRefs.classSimulatorResourceSetName, jName);
81         env->DeleteLocalRef(jName);
82     }
83
84     jstring jInterfaceType = env->NewStringUTF(interfaceType.c_str());
85     if (jInterfaceType)
86     {
87         env->CallVoidMethod(jobj, gSimulatorClassRefs.classSimulatorResourceSetInterfaceType,
88                             jInterfaceType);
89         env->DeleteLocalRef(jInterfaceType);
90     }
91 }
92
93 void onAutomationComplete(jweak jlistenerRef, const std::string &uri,
94                           const int automationID)
95 {
96     std::cout << "onAutomationComplete JNI entry" << std::endl;
97     JNIEnv *env = getEnv();
98     if (nullptr == env)
99         return;
100
101     jobject autoCompleteListener = env->NewLocalRef(jlistenerRef);
102     if (!autoCompleteListener)
103     {
104         releaseEnv();
105         return;
106     }
107
108     jclass autoCompleteCls = env->GetObjectClass(autoCompleteListener);
109     if (!autoCompleteCls)
110     {
111         releaseEnv();
112         return;
113     }
114
115     jmethodID autoCompleteMId = env->GetMethodID(autoCompleteCls, "onAutomationComplete",
116                                 "(Ljava/lang/String;I)V");
117     if (!autoCompleteMId)
118     {
119         releaseEnv();
120         return;
121     }
122
123     jstring jUri = env->NewStringUTF(uri.c_str());
124
125     env->CallVoidMethod(autoCompleteListener, autoCompleteMId, jUri, automationID);
126     if ((env)->ExceptionCheck())
127     {
128         releaseEnv();
129         return;
130     }
131
132     env->DeleteLocalRef(jUri);
133
134     releaseEnv();
135 }
136
137 JNIEXPORT jobject JNICALL
138 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_getModel
139 (JNIEnv *env, jobject object)
140 {
141     SimulatorResourceServerPtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, object);
142     if (nullptr == resource.get())
143     {
144         std::cout << "getModel: Resource is NULL";
145         return nullptr;
146     }
147
148     SimulatorResourceModel resModel = resource->getModel();
149     JniSimulatorResourceModel *model = new JniSimulatorResourceModel(resModel);
150     jobject jModel = JniSimulatorResourceModel::toJava(env, reinterpret_cast<jlong>(model));
151     return jModel;
152 }
153
154 JNIEXPORT void JNICALL
155 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_updateAttributeFromAllowedValues
156 (JNIEnv *env, jobject object, jstring attrName, jint index)
157 {
158     SimulatorResourceServerPtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, object);
159     if (nullptr == resource.get())
160     {
161         std::cout << "updateAttributeFromAllowedValues: Resource is NULL";
162         return;
163     }
164
165     const char *attrNamePtr = env->GetStringUTFChars(attrName, NULL);
166     if (!attrNamePtr)
167     {
168         std::cout << "updateAttributeFromAllowedValues: Failed to convert jstring to char string!";
169         return;
170     }
171
172     resource->updateAttributeFromAllowedValues(attrNamePtr, static_cast<int>(index));
173     env->ReleaseStringUTFChars(attrName, attrNamePtr);
174 }
175
176 JNIEXPORT void JNICALL
177 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_setRange
178 (JNIEnv *env, jobject object, jstring attrName, jint min, jint max)
179 {
180     SimulatorResourceServerPtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, object);
181     if (nullptr == resource.get())
182     {
183         std::cout << "setRange: Resource is NULL";
184         return;
185     }
186
187     const char *attrNamePtr = env->GetStringUTFChars(attrName, NULL);
188     if (!attrNamePtr)
189     {
190         std::cout << "setRange: Failed to convert jstring to char string!";
191         return;
192     }
193
194     resource->setRange(attrNamePtr, static_cast<int>(min), static_cast<int>(max));
195     env->ReleaseStringUTFChars(attrName, attrNamePtr);
196 }
197
198 JNIEXPORT void JNICALL
199 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_setInterfaceType
200 (JNIEnv *env, jobject jobject, const std::string &interfaceType)
201 {
202     jstring jInterfaceType = env->NewStringUTF(interfaceType.c_str());
203     if (!jInterfaceType)
204     {
205         std::cout << "setInterfaceType: InterfaceType is NULL";
206         return;
207     }
208
209     env->CallVoidMethod(jobject, gSimulatorClassRefs.classSimulatorResourceSetInterfaceType,
210                         jInterfaceType);
211     env->DeleteLocalRef(jInterfaceType);
212 }
213
214 JNIEXPORT void JNICALL
215 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_addAttributeInteger
216 (JNIEnv *env, jobject jobject, jstring jKey, jint jValue)
217 {
218     if (!jKey)
219     {
220         std::cout << "addAttributeInteger: AttributeName is Empty";
221         return;
222     }
223
224     SimulatorResourceServerPtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env,
225                                           jobject);
226     if (nullptr == resource.get())
227     {
228         std::cout << "addAttributeInteger: Resource is NULL";
229         return;
230     }
231
232     std::string str = env->GetStringUTFChars(jKey, NULL);
233     resource->addAttribute(str, static_cast<int>(jValue));
234 }
235
236 JNIEXPORT void JNICALL
237 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_addAttributeDouble
238 (JNIEnv *env, jobject jobject, jstring jKey, jdouble jValue)
239 {
240     if (!jKey)
241     {
242         std::cout << "addAttributeDouble: AttributeName is Empty";
243         return;
244     }
245
246     SimulatorResourceServerPtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env,
247                                           jobject);
248     if (nullptr == resource.get())
249     {
250         std::cout << "addAttributeDouble: Resource is NULL";
251         return;
252     }
253
254     std::string str = env->GetStringUTFChars(jKey, NULL);
255     resource->addAttribute(str, static_cast<double>(jValue));
256 }
257
258 JNIEXPORT void JNICALL
259 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_addAttributeBoolean
260 (JNIEnv *env, jobject jobject, jstring jKey, jboolean jValue)
261 {
262     if (!jKey)
263     {
264         std::cout << "addAttributeBoolean: AttributeName is Empty";
265         return;
266     }
267
268     SimulatorResourceServerPtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env,
269                                           jobject);
270     if (nullptr == resource.get())
271     {
272         std::cout << "addAttributeBoolean: Resource is NULL";
273         return;
274     }
275
276     std::string str = env->GetStringUTFChars(jKey, NULL);
277     resource->addAttribute(str, static_cast<bool>(jValue));
278 }
279
280 JNIEXPORT void JNICALL
281 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_addAttributeStringN
282 (JNIEnv *env, jobject jobject, jstring jKey, jstring jValue)
283 {
284     if (!jKey)
285     {
286         std::cout << "addAttributeStringN: AttributeName is Empty";
287         return;
288     }
289
290     SimulatorResourceServerPtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env,
291                                           jobject);
292     if (nullptr == resource.get())
293     {
294         std::cout << "addAttributeStringN: Resource is NULL";
295         return;
296     }
297
298     std::string key = env->GetStringUTFChars(jKey, NULL);
299     std::string value = env->GetStringUTFChars(jValue, NULL);
300
301     resource->addAttribute(key, value);
302 }
303
304 JNIEXPORT void JNICALL
305 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_updateAttributeInteger
306 (JNIEnv *env, jobject jobject, jstring jKey, jint jValue)
307 {
308     if (!jKey)
309     {
310         std::cout << "updateAttributeInteger: AttributeName is Empty";
311         return;
312     }
313
314     SimulatorResourceServerPtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env,
315                                           jobject);
316     if (nullptr == resource.get())
317     {
318         std::cout << "updateAttributeInteger: Resource is NULL";
319         return;
320     }
321
322     std::string str = env->GetStringUTFChars(jKey, NULL);
323     resource->updateAttribute(str, static_cast<int>(jValue));
324 }
325
326 JNIEXPORT void JNICALL
327 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_updateAttributeDouble
328 (JNIEnv *env, jobject jobject, jstring jKey, jdouble jValue)
329 {
330     if (!jKey)
331     {
332         std::cout << "updateAttributeDouble: AttributeName is Empty";
333         return;
334     }
335
336     SimulatorResourceServerPtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env,
337                                           jobject);
338     if (nullptr == resource.get())
339     {
340         std::cout << "updateAttributeDouble: Resource is NULL";
341         return;
342     }
343
344     std::string str = env->GetStringUTFChars(jKey, NULL);
345     resource->updateAttribute(str, static_cast<double>(jValue));
346 }
347
348 JNIEXPORT void JNICALL
349 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_updateAttributeBoolean
350 (JNIEnv *env, jobject jobject, jstring jKey, jboolean jValue)
351 {
352     if (!jKey)
353     {
354         std::cout << "updateAttributeBoolean: AttributeName is Empty";
355         return;
356     }
357
358     SimulatorResourceServerPtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env,
359                                           jobject);
360     if (nullptr == resource.get())
361     {
362         std::cout << "updateAttributeBoolean: Resource is NULL";
363         return;
364     }
365
366     std::string str = env->GetStringUTFChars(jKey, NULL);
367     resource->updateAttribute(str, static_cast<bool>(jValue));
368 }
369
370 JNIEXPORT void JNICALL
371 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_updateAttributeStringN
372 (JNIEnv *env, jobject jobject, jstring jKey, jstring jValue)
373 {
374     if (!jKey)
375     {
376         std::cout << "updateAttributeStringN: AttributeName is Empty";
377         return;
378     }
379
380     SimulatorResourceServerPtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env,
381                                           jobject);
382     if (nullptr == resource.get())
383     {
384         std::cout << "updateAttributeStringN: Resource is NULL";
385         return;
386     }
387
388     std::string key = env->GetStringUTFChars(jKey, NULL);
389     std::string value = env->GetStringUTFChars(jValue, NULL);
390
391     resource->updateAttribute(key, value);
392 }
393
394 JNIEXPORT void JNICALL
395 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_setAllowedValuesInteger
396 (JNIEnv *env, jobject object, jstring jKey, jobject jAllowedValues)
397 {
398     if (!jKey)
399     {
400         std::cout << "setAllowedValuesInteger: AttributeName is Empty";
401         return;
402     }
403
404     SimulatorResourceServerPtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, object);
405     if (nullptr == resource.get())
406     {
407         std::cout << "setAllowedValuesInteger: Resource is NULL";
408         return;
409     }
410
411     std::string str = env->GetStringUTFChars(jKey, NULL);
412     resource->setAllowedValues(str, convertIntegerVector(env, jAllowedValues));
413 }
414
415 JNIEXPORT void JNICALL
416 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_setAllowedValuesDouble
417 (JNIEnv *env, jobject object, jstring jKey, jobject jAllowedValues)
418 {
419     if (!jKey)
420     {
421         std::cout << "setAllowedValuesDouble: AttributeName is Empty";
422         return;
423     }
424
425     SimulatorResourceServerPtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, object);
426     if (nullptr == resource.get())
427     {
428         std::cout << "setAllowedValuesDouble: Resource is NULL";
429         return;
430     }
431
432     std::string str = env->GetStringUTFChars(jKey, NULL);
433     resource->setAllowedValues(str, convertDoubleVector(env, jAllowedValues));
434 }
435
436 JNIEXPORT void JNICALL
437 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_setAllowedValuesStringN
438 (JNIEnv *env, jobject object, jstring jKey, jobject jAllowedValues)
439 {
440     if (!jKey)
441     {
442         std::cout << "setAllowedValuesStringN: AttributeName is Empty";
443         return;
444     }
445
446     SimulatorResourceServerPtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, object);
447     if (nullptr == resource.get())
448     {
449         std::cout << "setAllowedValuesStringN: Resource is NULL";
450         return;
451     }
452
453     std::string str = env->GetStringUTFChars(jKey, NULL);
454     resource->setAllowedValues(str, convertStringVector(env, jAllowedValues));
455 }
456
457 JNIEXPORT jint JNICALL
458 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_startResourceAutomation
459 (JNIEnv *env, jobject object, jint automationType, jobject listener)
460 {
461     SimulatorResourceServerPtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, object);
462     if (nullptr == resource.get())
463     {
464         return -1;
465     }
466
467     if (!listener)
468     {
469         return -1;
470     }
471
472     jweak jlistenerRef = env->NewWeakGlobalRef(listener);
473     updateCompleteCallback callback =  [jlistenerRef](const std::string & uri, const int automationID)
474     {
475         onAutomationComplete(jlistenerRef, uri, automationID);
476     };
477
478     AutomationType type = AutomationType::NORMAL;
479     if (1 == automationType)
480     {
481         type = AutomationType::RECURRENT;
482     }
483
484     int automationId;
485     if (SIMULATOR_SUCCESS != resource->startUpdateAutomation(type, callback,
486             automationId))
487         return -1;
488
489     return automationId;
490 }
491
492 JNIEXPORT jint JNICALL
493 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_startAttributeAutomation
494 (JNIEnv *env, jobject object, jstring attrName, jint automationType, jobject listener)
495 {
496     std::cout << "starAttributeAutomation JNI" << std::endl;
497     SimulatorResourceServerPtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, object);
498     if (nullptr == resource.get())
499     {
500         return -1;
501     }
502
503     if (!attrName)
504     {
505         return -1;
506     }
507
508     if (!listener)
509     {
510         return -1;
511     }
512
513     jweak jlistenerRef = env->NewWeakGlobalRef(listener);
514     updateCompleteCallback callback =  [jlistenerRef](const std::string & uri, const int automationID)
515     {
516         onAutomationComplete(jlistenerRef, uri, automationID);
517     };
518
519     const char *attrNamePtr = env->GetStringUTFChars(attrName, NULL);
520
521     AutomationType type = AutomationType::NORMAL;
522     if (1 == automationType)
523     {
524         type = AutomationType::RECURRENT;
525     }
526
527     int automationId = -1;
528     resource->startUpdateAutomation(attrNamePtr, type, callback, automationId);
529
530     env->ReleaseStringUTFChars(attrName, attrNamePtr);
531     return automationId;
532 }
533
534 JNIEXPORT void JNICALL
535 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_stopAutomation
536 (JNIEnv *env, jobject object, jint automationId)
537 {
538     SimulatorResourceServerPtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, object);
539     if (nullptr == resource.get())
540     {
541         return;
542     }
543
544     resource->stopUpdateAutomation(automationId);
545 }
546
547 JNIEXPORT void JNICALL
548 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_removeAttribute
549 (JNIEnv *env, jobject jobject, jstring jKey)
550 {
551     if (!jKey)
552     {
553         std::cout << "removeAttribute: AttributeName is Empty";
554         return;
555     }
556
557     SimulatorResourceServerPtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env,
558                                           jobject);
559     if (nullptr == resource.get())
560     {
561         std::cout << "removeAttribute: Resource is NULL";
562         return;
563     }
564
565     std::string str = env->GetStringUTFChars(jKey, NULL);
566     resource->removeAttribute(str);
567 }
568
569 JNIEXPORT void JNICALL Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_dispose
570 (JNIEnv *env, jobject thiz)
571 {
572     JniSimulatorResource *resource = GetHandle<JniSimulatorResource>(env, thiz);
573     delete resource;
574 }
575