Imported Upstream version 0.9.1
[platform/upstream/iotivity.git] / service / things-manager / sdk / java / jni / tm / src / jni_things_manager.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 #include "jni_things_manager.h"
21
22 #include "JniOcResource.h"
23 #include "JniOcResourceHandle.h"
24 #include "ThingsManager.h"
25 #include "ActionSet.h"
26 #include "jni_things_manager_jvm.h"
27 #include "jni_things_manager_util.h"
28 #include "jni_things_manager_callbacks.h"
29 #include "jni_action_set.h"
30
31 using namespace OC;
32 using namespace OIC;
33
34 /**
35  * @var g_ThingsManager
36  * @brief ThingsManager static object
37  */
38 static ThingsManager g_ThingsManager;
39
40 jobject ocResourceHandleToJava(JNIEnv *env, jlong resourceHandle);
41
42 JNIEXPORT jint JNICALL JNIThingsManagerFindCandidateResource(JNIEnv *env, jobject interfaceObject,
43         jobject jResourceTypes, jint waitSec)
44 {
45     LOGI("JNIThingsManagerFindCandidateResource: Enter");
46
47     if (!jResourceTypes)
48     {
49         LOGE("JNIThingsManagerFindCandidateResource: jResourceTypes is NULL!");
50         return OC_STACK_INVALID_PARAM;
51     }
52
53     OCStackResult ocResult = OC_STACK_ERROR;
54     try
55     {
56         ocResult = g_ThingsManager.findCandidateResources(convertStringVector(env, jResourceTypes),
57                    &ThingsManagerCallbacks::onFoundCandidateResource, (int)waitSec);
58
59         if (OC_STACK_OK != ocResult)
60         {
61             LOGE("JNIThingsManagerFindCandidateResource: findCandidateResources failed!");
62             return ocResult;
63         }
64     }
65     catch (InitializeException &e)
66     {
67         LOGE("JNIThingsManagerFindCandidateResource: Exception occurred! %s, %d", e.reason().c_str(),
68              e.code());
69         return ocResult;
70     }
71
72     LOGI("JNIThingsManagerFindCandidateResource: Exit");
73     return ocResult;
74 }
75
76 JNIEXPORT jint JNICALL JNIThingsManagerSubscribeCollectionPresence(JNIEnv *env,
77         jobject interfaceObject,
78         jobject jResource)
79 {
80     LOGI("JNIThingsManagerSubscribeCollectionPresence: Enter");
81
82     if (!jResource)
83     {
84         LOGE("JNIThingsManagerSubscribeCollectionPresence: jResource is NULL!");
85         return OC_STACK_INVALID_PARAM;
86     }
87
88     OCStackResult ocResult = OC_STACK_ERROR;
89
90     JniOcResource *jniOcResource = JniOcResource::getJniOcResourcePtr(env, jResource);
91     if (NULL == jniOcResource)
92     {
93         LOGE("JNIThingsManagerSubscribeCollectionPresence: Failed to get jni OcResource!");
94         return ocResult;
95     }
96
97     std::shared_ptr<OCResource> ocResource = jniOcResource->getOCResource();
98     if (NULL == ocResource.get())
99     {
100         LOGE("JNIThingsManagerSubscribeCollectionPresence: Failed to get OCResource object!");
101         return ocResult;
102     }
103
104     ocResult = g_ThingsManager.subscribeCollectionPresence(ocResource,
105                &ThingsManagerCallbacks::onSubscribePresence);
106     if (OC_STACK_OK != ocResult)
107     {
108         LOGE("JNIThingsManagerSubscribeCollectionPresence: subscribeCollectionPresence failed!");
109         return ocResult;
110     }
111
112     LOGI("JNIThingsManagerSubscribeCollectionPresence: Exit");
113     return ocResult;
114 }
115
116 JNIEXPORT jobject JNICALL JNIThingsManagerBindResourceToGroup(JNIEnv *env, jobject interfaceObject,
117         jobject jResource, jobject jCollectionHandle)
118 {
119     LOGI("JNIThingsManagerBindResourceToGroup: Enter");
120
121     if (!jResource || !jCollectionHandle)
122     {
123         LOGE("JNIThingsManagerBindResourceToGroup: Invalid parameter!");
124         return NULL;
125     }
126
127     std::shared_ptr<OCResource> ocResource;
128     JniOcResource *jniOcResource = JniOcResource::getJniOcResourcePtr(env, jResource);
129     if (jniOcResource)
130     {
131         ocResource = jniOcResource->getOCResource();
132     }
133
134     if (NULL == ocResource.get())
135     {
136         LOGE("JNIThingsManagerBindResourceToGroup: Failed to get OCResource object!");
137         return NULL;
138     }
139
140     JniOcResourceHandle *jniOcCollectionHandle = JniOcResourceHandle::getJniOcResourceHandlePtr(env,
141             jCollectionHandle);
142     if (NULL == jniOcCollectionHandle)
143     {
144         LOGE("JNIThingsManagerBindResourceToGroup: collection handle is null!");
145         return NULL;
146     }
147
148     jobject jResourceHandle = NULL;
149     try
150     {
151         OCResourceHandle ocChildHandle = NULL;
152         OCResourceHandle ocCollectionHandle = jniOcCollectionHandle->getOCResourceHandle();
153         OCStackResult ocResult = g_ThingsManager.bindResourceToGroup(ocChildHandle, ocResource, ocCollectionHandle);
154         if (OC_STACK_OK != ocResult)
155         {
156             LOGE("JNIThingsManagerBindResourceToGroup: bindResourceToGroup failed!");
157             return NULL;
158         }
159
160         // Convert OCResourceHandle to java type
161         JniOcResourceHandle* jniHandle = new JniOcResourceHandle(ocChildHandle);
162         jlong handle = reinterpret_cast<jlong>(jniHandle);
163         jResourceHandle = env->NewObject(g_cls_OcResourceHandle, g_mid_OcResourceHandle_N_ctor, handle);
164         if (env->ExceptionCheck())
165         {
166             LOGE("JNIThingsManagerBindResourceToGroup: Failed to create OcResourceHandle");
167             delete jniHandle;
168             return NULL;
169         }
170     }
171     catch (InitializeException &e)
172     {
173         LOGE("JNIThingsManagerBindResourceToGroup: Exception occurred! %s, %d", e.reason().c_str(),
174              e.code());
175         return NULL;
176     }
177
178     LOGI("JNIThingsManagerBindResourceToGroup: Exit");
179     return jResourceHandle;
180 }
181
182 JNIEXPORT jint JNICALL JNIThingsManagerFindGroup(JNIEnv *env, jobject interfaceObject,
183         jobject jResourceTypes)
184 {
185     LOGI("JNIThingsManagerFindGroup: Enter");
186
187     if (!jResourceTypes)
188     {
189         LOGE("JNIThingsManagerFindGroup: jResourceTypes is NULL!");
190         return OC_STACK_INVALID_PARAM;
191     }
192
193     OCStackResult ocResult = OC_STACK_ERROR;
194     try
195     {
196         ocResult = g_ThingsManager.findGroup((convertStringVector(env, jResourceTypes)),
197                                              &ThingsManagerCallbacks::onFoundGroup);
198         if (OC_STACK_OK != ocResult)
199         {
200             LOGE("JNIThingsManagerFindGroup: findGroup failed!");
201             return ocResult;
202         }
203     }
204     catch (InitializeException &e)
205     {
206         LOGE("JNIThingsManagerFindGroup: Exception occurred! %s, %d", e.reason().c_str(),
207             e.code());
208         return ocResult;
209     }
210     LOGI("JNIThingsManagerFindGroup: Exit");
211     return ocResult;
212 }
213
214 JNIEXPORT jint JNICALL JNIThingsManagerCreateGroup(JNIEnv *env, jobject interfaceObject,
215         jstring jResourceType)
216 {
217     LOGI("JNIThingsManagerCreateGroup: Enter");
218
219     if (!jResourceType)
220     {
221         LOGE("JNIThingsManagerCreateGroup: jResourceType is NULL!");
222         return OC_STACK_INVALID_PARAM;
223     }
224
225     OCStackResult ocResult = OC_STACK_ERROR;
226
227     const char *resourceTypePointer = env->GetStringUTFChars(jResourceType, NULL);
228     if (NULL == resourceTypePointer)
229     {
230         LOGE("JNIThingsManagerCreateGroup: Failed to convert jstring to char string!");
231         return OC_STACK_NO_MEMORY;
232     }
233
234     std::string resourceType(resourceTypePointer);
235     try
236     {
237         ocResult =  g_ThingsManager.createGroup(resourceType);
238         if (OC_STACK_OK != ocResult)
239         {
240             LOGE("JNIThingsManagerCreateGroup: CreateGroup failed!");
241         }
242     }
243     catch (InitializeException &e)
244     {
245         LOGE("JNIThingsManagerCreateGroup: Exception occurred! %s, %d", e.reason().c_str(),
246             e.code());
247     }
248     env->ReleaseStringUTFChars(jResourceType, resourceTypePointer);
249     LOGI("JNIThingsManagerCreateGroup: Exit");
250     return ocResult;
251 }
252
253 JNIEXPORT jint JNICALL JNIThingsManagerJoinGroupString(JNIEnv *env, jobject interfaceObject,
254         jstring jResourceType, jobject jResourceHandle)
255 {
256     LOGI("JNIThingsManagerJoinGroupString: Enter");
257
258     if ((!jResourceType) || (!jResourceHandle))
259     {
260         LOGE("JNIThingsManagerJoinGroupString: jResourceType or jResourceHandle is NULL!");
261         return OC_STACK_INVALID_PARAM;
262     }
263
264     OCStackResult ocResult = OC_STACK_ERROR;
265
266     const char *resourceTypePointer = env->GetStringUTFChars(jResourceType, NULL);
267     if (NULL == resourceTypePointer)
268     {
269         LOGE("JNIThingsManagerJoinGroupString: Failed to convert jstring to char string!");
270         return OC_STACK_NO_MEMORY;
271     }
272
273     std::string resourceType(resourceTypePointer);
274
275     JniOcResourceHandle *jniOcResourceHandle = JniOcResourceHandle::getJniOcResourceHandlePtr(env,
276             jResourceHandle);
277     try
278     {
279         OCResourceHandle ocResourceHandle = jniOcResourceHandle->getOCResourceHandle();
280         ocResult = g_ThingsManager.joinGroup(resourceType, ocResourceHandle);
281         if (OC_STACK_OK != ocResult)
282         {
283             LOGE("JNIThingsManagerJoinGroupString: joinGroup failed!");
284         }
285     }
286     catch (InitializeException &e)
287     {
288         LOGE("JNIThingsManagerJoinGroupString: Exception occurred! %s, %d", e.reason().c_str(),
289              e.code());
290     }
291     env->ReleaseStringUTFChars(jResourceType, resourceTypePointer);
292     LOGI("JNIThingsManagerJoinGroupString: Exit");
293     return ocResult;
294 }
295
296 JNIEXPORT jint JNICALL JNIThingsManagerJoinGroupObject(JNIEnv *env, jobject interfaceObject,
297         jobject jResource, jobject jResourceHandle)
298 {
299     LOGI("JNIThingsManagerJoinGroupObject: Enter");
300
301     if ((!jResource) || (!jResourceHandle))
302     {
303         LOGE("JNIThingsManagerJoinGroupObject: jResource or jResourceHandle is NULL!");
304         return OC_STACK_INVALID_PARAM;
305     }
306
307     OCStackResult ocResult = OC_STACK_ERROR;
308
309     std::shared_ptr<OCResource> ocResource;
310     JniOcResource *jniOcResource = JniOcResource::getJniOcResourcePtr(env, jResource);
311     if (jniOcResource)
312     {
313         ocResource = jniOcResource->getOCResource();
314     }
315
316     if (NULL == ocResource.get())
317     {
318         LOGE("JNIThingsManagerJoinGroupObject: Failed to get OCResource object!");
319         return ocResult;
320     }
321
322     JniOcResourceHandle *jniOcResourceHandle = JniOcResourceHandle::getJniOcResourceHandlePtr(env,
323             jResourceHandle);
324
325     try
326     {
327         OCResourceHandle resHandle = jniOcResourceHandle->getOCResourceHandle();
328
329         ocResult = g_ThingsManager.joinGroup(ocResource, resHandle);
330         if (OC_STACK_OK != ocResult)
331         {
332             LOGE("JNIThingsManagerJoinGroupObject: joinGroup failed!");
333             return ocResult;
334         }
335     }
336     catch (InitializeException &e)
337     {
338         LOGE("JNIThingsManagerJoinGroupObject: Exception occurred! %s, %d", e.reason().c_str(),
339              e.code());
340         return ocResult;
341     }
342     LOGI("JNIThingsManagerJoinGroupObject: Exit");
343     return ocResult;
344 }
345
346 JNIEXPORT jint JNICALL JNIThingsManagerLeaveGroup(JNIEnv *env, jobject interfaceObject,
347         jstring jResourceType,
348         jobject jResourceHandle)
349 {
350     LOGI("JNIThingsManagerLeaveGroup: Enter");
351
352     if ((!jResourceType) || (!jResourceHandle))
353     {
354         LOGE("JNIThingsManagerLeaveGroup: jResourceType or jResourceHandle is NULL!");
355         return OC_STACK_INVALID_PARAM;
356     }
357
358     OCStackResult ocResult = OC_STACK_ERROR;
359
360     const char *resourceTypePointer = env->GetStringUTFChars(jResourceType, NULL);
361     if (NULL == resourceTypePointer)
362     {
363         LOGE("JNIThingsManagerLeaveGroup: Failed to convert jstring to char string!");
364         return OC_STACK_NO_MEMORY;
365     }
366
367     std::string resourceType(resourceTypePointer);
368     JniOcResourceHandle *jniOcResourceHandle = JniOcResourceHandle::getJniOcResourceHandlePtr(env,
369             jResourceHandle);
370
371     try
372     {
373         OCResourceHandle ocResourceHandle = jniOcResourceHandle->getOCResourceHandle();
374
375         ocResult = g_ThingsManager.leaveGroup(resourceType, ocResourceHandle);
376         if (OC_STACK_OK != ocResult)
377         {
378             LOGE("JNIThingsManagerLeaveGroup: leaveGroup failed!");
379         }
380     }
381     catch (InitializeException &e)
382     {
383         LOGE("JNIThingsManagerLeaveGroup: Exception occurred! %s, %d", e.reason().c_str(),
384             e.code());
385     }
386     env->ReleaseStringUTFChars(jResourceType, resourceTypePointer);
387     LOGI("JNIThingsManagerLeaveGroup: Exit");
388     return ocResult;
389 }
390
391 JNIEXPORT jint JNICALL JNIThingsManagerLeaveGroupForResource(JNIEnv *env, jobject interfaceObject,
392         jobject jResource,
393         jstring jResourceType,
394         jobject jResourceHandle)
395 {
396     LOGI("JNIThingsManagerLeaveGroupForResource: Enter");
397
398     if ((!jResource) || (!jResourceType) || (!jResourceHandle))
399     {
400         LOGE("JNIThingsManagerLeaveGroupForResource: jResourceType or jResourceHandle is NULL!");
401         return OC_STACK_INVALID_PARAM;
402     }
403
404     OCStackResult ocResult = OC_STACK_ERROR;
405
406     JniOcResource *jniOcResource = JniOcResource::getJniOcResourcePtr(env, jResource);
407     if (NULL == jniOcResource)
408     {
409         LOGE("JNIThingsManagerLeaveGroupForResource: Failed to get jni OcResource!");
410         return ocResult;
411     }
412
413     std::shared_ptr<OCResource> ocResource = jniOcResource->getOCResource();
414     if (NULL == ocResource.get())
415     {
416         LOGE("JNIThingsManagerLeaveGroupForResource: Failed to get OCResource object!");
417         return ocResult;
418     }
419
420     const char *resourceTypePointer = env->GetStringUTFChars(jResourceType, NULL);
421     if (NULL == resourceTypePointer)
422     {
423         LOGE("JNIThingsManagerLeaveGroupForResource: Failed to convert jstring to char string!");
424         return OC_STACK_NO_MEMORY;
425     }
426
427     std::string resourceType(resourceTypePointer);
428     JniOcResourceHandle *jniOcResourceHandle = JniOcResourceHandle::getJniOcResourceHandlePtr(env,
429             jResourceHandle);
430
431     try
432     {
433         OCResourceHandle ocResourceHandle = jniOcResourceHandle->getOCResourceHandle();
434
435         ocResult = g_ThingsManager.leaveGroup(ocResource, resourceType, ocResourceHandle);
436         if (OC_STACK_OK != ocResult)
437         {
438             LOGE("JNIThingsManagerLeaveGroupForResource: leaveGroup failed!");
439         }
440     }
441     catch (InitializeException &e)
442     {
443         LOGE("JNIThingsManagerLeaveGroupForResource: Exception occurred! %s, %d", e.reason().c_str(),
444              e.code());
445     }
446
447     env->ReleaseStringUTFChars(jResourceType, resourceTypePointer);
448     LOGI("JNIThingsManagerLeaveGroupForResource: Exit");
449     return ocResult;
450 }
451
452 JNIEXPORT void JNICALL JNIThingsManagerDeleteGroup(JNIEnv *env, jobject interfaceObject,
453         jstring jcollectionResourceType)
454 {
455     LOGI("JNIThingsManagerDeleteGroup: Enter");
456
457     if (!jcollectionResourceType)
458     {
459         LOGE("JNIThingsManagerDeleteGroup: jcollectionResourceType is NULL!");
460         return;
461     }
462
463     const char *collectionResourceTypePointer = env->GetStringUTFChars(jcollectionResourceType, NULL);
464     if (NULL == collectionResourceTypePointer)
465     {
466         LOGE("JNIThingsManagerDeleteGroup: Failed to convert jstring to char string!");
467         return;
468     }
469
470     std::string collectionResourceType(collectionResourceTypePointer);
471     try
472     {
473         g_ThingsManager.deleteGroup(collectionResourceType);
474     }
475     catch (InitializeException &e)
476     {
477         LOGE("JNIThingsManagerDeleteGroup: Exception occurred! %s, %d", e.reason().c_str(),
478             e.code());
479     }
480
481     env->ReleaseStringUTFChars(jcollectionResourceType, collectionResourceTypePointer);
482     LOGI("JNIThingsManagerDeleteGroup: Exit");
483 }
484
485 JNIEXPORT jobject JNICALL JNIThingsManagerGetGroupList(JNIEnv *env, jobject interfaceObject)
486 {
487     LOGI("JNIThingsManagerGetGroupList: Enter");
488
489     std::map< std::string, OCResourceHandle> groupListMap;
490     jobject jGroupListMap;
491
492     groupListMap = g_ThingsManager.getGroupList();
493     if (groupListMap.empty())
494     {
495         LOGD("getGroupList Map is empty");
496         return NULL;
497     }
498
499     jclass clazz = env->FindClass("java/util/HashMap");
500     jmethodID init = env->GetMethodID(clazz, "<init>", "()V");
501     jGroupListMap = env->NewObject(clazz, init);
502     jmethodID putMethod = env->GetMethodID(clazz, "put",
503                                            "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
504
505     for (auto it = groupListMap.begin(); it != groupListMap.end(); ++it)
506     {
507         jstring key = (*env).NewStringUTF( (*it).first.c_str() );
508         JniOcResourceHandle *jniOcResourceHandle = new JniOcResourceHandle(((*it).second));
509         jobject value = ocResourceHandleToJava(env, reinterpret_cast<jlong>(jniOcResourceHandle));
510         env->CallObjectMethod(jGroupListMap, putMethod, key, value);
511     }
512
513     LOGI("JNIThingsManagerGetGroupList: Exit");
514     return jGroupListMap;
515 }
516
517 JNIEXPORT jint JNICALL JNIThingsManagerUpdateConfigurations(JNIEnv *env, jobject interfaceObject,
518         jobject resource, jobject configurations)
519 {
520     LOGI("JNIThingsManagerUpdateConfigurations: Enter");
521
522     if ((!resource) || (!configurations))
523     {
524         LOGE("JNIThingsManagerUpdateConfigurations: resource or configurations is NULL!");
525         return OC_STACK_INVALID_PARAM;
526     }
527
528     OCStackResult ocResult = OC_STACK_ERROR;
529
530     std::shared_ptr<OCResource> ocResource;
531     JniOcResource *jniOcResource = JniOcResource::getJniOcResourcePtr(env, resource);
532     if (jniOcResource)
533     {
534         ocResource = jniOcResource->getOCResource();
535     }
536
537     if (NULL == ocResource.get())
538     {
539         LOGE("JNIThingsManagerUpdateConfigurations: Failed to get OCResource object!");
540         return ocResult;
541     }
542
543     std::map<std::string, std::string> configurationsMap;
544     configurationsMap = convertStringMap(env, configurations);
545     ocResult =  g_ThingsManager.updateConfigurations(ocResource, configurationsMap,
546                 &ThingsManagerCallbacks::onUpdateConfigurationsResponse);
547     if (OC_STACK_OK != ocResult)
548     {
549         LOGE("JNIThingsManagerUpdateConfigurations: updateConfigurations failed!");
550         return ocResult;
551     }
552
553     LOGI("JNIThingsManagerUpdateConfigurations: Exit");
554     return ocResult;
555 }
556
557 JNIEXPORT jint JNICALL JNIThingsManagerGetConfigurations(JNIEnv *env, jobject interfaceObject,
558         jobject resource, jobject configurations)
559 {
560     LOGI("JNIThingsManagerGetConfigurations: Enter");
561
562     if ((!resource) || (!configurations))
563     {
564         LOGE("JNIThingsManagerGetConfigurations: resource or configurations is NULL!");
565         return OC_STACK_INVALID_PARAM;
566     }
567
568     OCStackResult ocResult = OC_STACK_ERROR;
569
570     std::shared_ptr<OCResource> ocResource;
571     JniOcResource *jniOcResource = JniOcResource::getJniOcResourcePtr(env, resource);
572     if (jniOcResource)
573     {
574         ocResource = jniOcResource->getOCResource();
575     }
576
577     if (NULL == ocResource.get())
578     {
579         LOGE("JNIThingsManagerGetConfigurations: Failed to get OCResource object!");
580         return ocResult;
581     }
582
583     ocResult = g_ThingsManager.getConfigurations(ocResource,
584                (convertStringVector(env, configurations)),
585                &ThingsManagerCallbacks::onGetConfigurationsResponse);
586     if (OC_STACK_OK != ocResult)
587     {
588         LOGE("JNIThingsManagerGetConfigurations: getConfigurations failed!");
589         return ocResult;
590     }
591
592     LOGI("JNIThingsManagerGetConfigurations: Exit");
593     return ocResult;
594 }
595
596 JNIEXPORT jstring JNICALL JNIThingsManagerGetListOfSupportedConfigurationUnits(JNIEnv *env,
597         jobject interfaceObject)
598 {
599     LOGI("JNIThingsManagerGetListOfSupportedConfigurationUnits: Enter");
600
601     std::string configListString = g_ThingsManager.getListOfSupportedConfigurationUnits();
602     jstring jConfigListString =  env->NewStringUTF(configListString.c_str());
603
604     LOGI("JNIThingsManagerGetListOfSupportedConfigurationUnits: Exit");
605     return jConfigListString;
606 }
607
608 JNIEXPORT jint JNICALL JNIThingsManagerDoBootstrap(JNIEnv *env, jobject interfaceObject)
609 {
610     LOGI("JNIThingsManagerDoBootstrap: Enter");
611
612     OCStackResult ocResult = OC_STACK_ERROR;
613     try
614     {
615         ocResult  = g_ThingsManager.doBootstrap(&ThingsManagerCallbacks::onBootStrapResponse);
616         if (OC_STACK_OK != ocResult)
617         {
618             LOGE("JNIThingsManagerDoBootstrap: doBootstrap failed!");
619             return ocResult;
620         }
621     }
622     catch (InitializeException &e)
623     {
624         LOGE("JNIThingsManagerDoBootstrap: Exception occurred! %s, %d", e.reason().c_str(),
625             e.code());
626         return ocResult;
627     }
628
629     LOGI("JNIThingsManagerDoBootstrap: Exit");
630     return ocResult;
631 }
632
633 JNIEXPORT jint JNICALL JNIThingsManagerReboot(JNIEnv *env, jobject interfaceObject,
634         jobject resource)
635 {
636     LOGI("JNIThingsManagerReboot: Enter");
637
638     if (!resource)
639     {
640         LOGE("JNIThingsManagerReboot: resource is NULL!");
641         return OC_STACK_INVALID_PARAM;
642     }
643
644     OCStackResult ocResult = OC_STACK_ERROR;
645
646     std::shared_ptr<OCResource> ocResource;
647     JniOcResource *jniOcResource = JniOcResource::getJniOcResourcePtr(env, resource);
648     if (jniOcResource)
649     {
650         ocResource = jniOcResource->getOCResource();
651     }
652
653     if (NULL == ocResource.get())
654     {
655         LOGE("JNIThingsManagerReboot: Failed to get OCResource object!");
656         return ocResult;
657     }
658
659     ocResult = g_ThingsManager.reboot(ocResource, &ThingsManagerCallbacks::onRebootResponse);
660     if (OC_STACK_OK != ocResult)
661     {
662         LOGE("JNIThingsManagerReboot: reboot failed!");
663         return ocResult;
664     }
665
666     LOGI("JNIThingsManagerReboot: Exit");
667     return ocResult;
668 }
669
670 JNIEXPORT jint JNICALL JNIThingsManagerFactoryReset(JNIEnv *env, jobject interfaceObject,
671         jobject resource)
672 {
673     LOGI("JNIThingsManagerFactoryReset: Enter");
674
675     if (!resource)
676     {
677         LOGE("JNIThingsManagerFactoryReset: resource is NULL!");
678         return OC_STACK_INVALID_PARAM;
679     }
680
681     OCStackResult ocResult = OC_STACK_ERROR;
682
683     std::shared_ptr<OCResource> ocResource;
684     JniOcResource *jniOcResource = JniOcResource::getJniOcResourcePtr(env, resource);
685     if (jniOcResource)
686     {
687         ocResource = jniOcResource->getOCResource();
688     }
689
690     if (NULL == ocResource.get())
691     {
692         LOGE("JNIThingsManagerFactoryReset: Failed to get OCResource object!");
693         return ocResult;
694     }
695
696     ocResult = g_ThingsManager.factoryReset(ocResource,
697                                             &ThingsManagerCallbacks::onFactoryResetResponse);
698     if (OC_STACK_OK != ocResult)
699     {
700         LOGE("JNIThingsManagerFactoryReset: factoryReset failed!");
701         return ocResult;
702     }
703
704     LOGI("JNIThingsManagerFactoryReset: Exit");
705     return ocResult;
706 }
707
708 JNIEXPORT jint JNICALL JNIThingsManagerAddActionSet(JNIEnv *env, jobject interfaceObject,
709         jobject resource,
710         jobject newActionSet)
711 {
712     LOGI("JNIThingsManagerAddActionSet: Entry");
713
714     if ((!resource) || (!newActionSet))
715     {
716         LOGE("JNIThingsManagerAddActionSet: resource or newActionSet is NULL!");
717         return OC_STACK_INVALID_PARAM;
718     }
719
720     OCStackResult ocResult = OC_STACK_ERROR;
721
722     std::shared_ptr<OCResource> ocResource;
723     JniOcResource *jniOcResource = JniOcResource::getJniOcResourcePtr(env, resource);
724     if (jniOcResource)
725     {
726         ocResource = jniOcResource->getOCResource();
727     }
728
729     if (NULL == ocResource.get())
730     {
731         LOGE("JNIThingsManageraAdActionSet: Failed to get OCResource object!");
732         return ocResult;
733     }
734
735     JniActionSet *jActionSet = new JniActionSet(env, newActionSet);
736     ActionSet *pActionSet = jActionSet->getActionSet(env, newActionSet);
737     if (NULL == pActionSet)
738     {
739         LOGE("JNIThingsManageraAdActionSet: Failed to convert ActionSet!");
740         return ocResult;
741     }
742
743     ocResult = g_ThingsManager.addActionSet(ocResource, pActionSet,
744                                             &ThingsManagerCallbacks::onPutResponse);
745     if (OC_STACK_OK != ocResult)
746     {
747         LOGE("JNIThingsManagerAddActionSet: addActionSet is failed!");
748     }
749
750     delete pActionSet;
751     LOGI("JNIThingsManagerAddActionSet: Exit");
752     return ocResult;
753 }
754
755 JNIEXPORT jint JNICALL JNIThingsManagerExecuteActionSet(JNIEnv *env, jobject interfaceObject,
756         jobject resource, jstring jActionSetName)
757 {
758     LOGI("JNIThingsManagerExecuteActionSet: Entry");
759
760     if ((!resource) || (!jActionSetName))
761     {
762         LOGE("JNIThingsManagerExecuteActionSet: resource or jActionSetName is NULL!");
763         return OC_STACK_INVALID_PARAM;
764     }
765
766     OCStackResult ocResult = OC_STACK_ERROR;
767
768     std::shared_ptr<OCResource> ocResource;
769     JniOcResource *jniOcResource = JniOcResource::getJniOcResourcePtr(env, resource);
770     if (jniOcResource)
771     {
772         ocResource = jniOcResource->getOCResource();
773     }
774
775     if (NULL == ocResource.get())
776     {
777         LOGE("JNIThingsManagerExecuteActionSet: Failed to get OCResource object!");
778         return ocResult;
779     }
780
781     const char *actionSetNamePointer = env->GetStringUTFChars(jActionSetName, 0);
782     if (NULL == actionSetNamePointer)
783     {
784         LOGE("JNIThingsManagerExecuteActionSet: Failed to convert jstring to char string!");
785         return OC_STACK_NO_MEMORY;
786     }
787
788     std::string actionSetName(actionSetNamePointer);
789
790     ocResult = g_ThingsManager.executeActionSet(ocResource, actionSetName,
791                &ThingsManagerCallbacks::onPostResponse);
792     if (OC_STACK_OK != ocResult)
793     {
794         LOGE("JNIThingsManagerExecuteActionSet: executeActionSet is failed!");
795     }
796
797     env->ReleaseStringUTFChars(jActionSetName, actionSetNamePointer);
798     LOGI("JNIThingsManagerExecuteActionSet: Exit");
799     return ocResult;
800 }
801
802 JNIEXPORT jint JNICALL JNIThingsManagerExecuteActionSetWithDelay(JNIEnv *env,
803         jobject interfaceObject,
804         jobject resource, jstring jActionSetName, jlong delay)
805 {
806     LOGI("JNIThingsManagerExecuteActionSet: Entry");
807
808     if ((!resource) || (!jActionSetName))
809     {
810         LOGE("JNIThingsManagerExecuteActionSet: resource or jActionSetName is NULL!");
811         return OC_STACK_INVALID_PARAM;
812     }
813
814     OCStackResult ocResult = OC_STACK_ERROR;
815
816     std::shared_ptr<OCResource> ocResource;
817     JniOcResource *jniOcResource = JniOcResource::getJniOcResourcePtr(env, resource);
818     if (jniOcResource)
819     {
820         ocResource = jniOcResource->getOCResource();
821     }
822
823     if (NULL == ocResource.get())
824     {
825         LOGE("JNIThingsManagerExecuteActionSet: Failed to get OCResource object!");
826         return ocResult;
827     }
828
829     const char *actionSetNamePointer = env->GetStringUTFChars(jActionSetName, 0);
830     if (NULL == actionSetNamePointer)
831     {
832         LOGE("JNIThingsManagerExecuteActionSet: Failed to convert jstring to char string!");
833         return OC_STACK_NO_MEMORY;
834     }
835
836     std::string actionSetName(actionSetNamePointer);
837     if (0 == delay)
838     {
839         ocResult = g_ThingsManager.executeActionSet(ocResource, actionSetName,
840                    &ThingsManagerCallbacks::onPostResponse);
841     }
842     else
843     {
844         ocResult = g_ThingsManager.executeActionSet(ocResource, actionSetName,
845                    (long int)delay,
846                    &ThingsManagerCallbacks::onPostResponse);
847     }
848     if (OC_STACK_OK != ocResult)
849     {
850         LOGE("JNIThingsManagerExecuteActionSet: executeActionSet is failed!");
851     }
852
853     env->ReleaseStringUTFChars(jActionSetName, actionSetNamePointer);
854     LOGI("JNIThingsManagerExecuteActionSet: Exit");
855     return ocResult;
856 }
857
858 JNIEXPORT jint JNICALL JNIThingsManagerCancelActionSet(JNIEnv *env, jobject interfaceObject,
859         jobject resource, jstring jActionSetName)
860 {
861     LOGI("JNIThingsManagerCancelActionSet: Entry");
862
863     if ((!resource) || (!jActionSetName))
864     {
865         LOGE("JNIThingsManagerCancelActionSet: resource or jActionSetName is NULL!");
866         return OC_STACK_INVALID_PARAM;
867     }
868
869     OCStackResult ocResult = OC_STACK_ERROR;
870
871     std::shared_ptr<OCResource> ocResource;
872     JniOcResource *jniOcResource = JniOcResource::getJniOcResourcePtr(env, resource);
873     if (jniOcResource)
874     {
875         ocResource = jniOcResource->getOCResource();
876     }
877
878     if (NULL == ocResource.get())
879     {
880         LOGE("JNIThingsManagerCancelActionSet: Failed to get OCResource object!");
881         return ocResult;
882     }
883
884     const char *actionSetNamePointer = env->GetStringUTFChars(jActionSetName, 0);
885     if (NULL == actionSetNamePointer)
886     {
887         LOGE("JNIThingsManagerCancelActionSet: Failed to get character sequence from jstring!");
888         return OC_STACK_NO_MEMORY;
889     }
890
891     std::string actionSetName(actionSetNamePointer);
892
893     ocResult = g_ThingsManager.cancelActionSet(ocResource, actionSetName,
894                &ThingsManagerCallbacks::onPostResponse);
895     if (OC_STACK_OK != ocResult)
896     {
897         LOGE("JNIThingsManagerCancelActionSet: cancelActionSet is failed!");
898     }
899
900     env->ReleaseStringUTFChars(jActionSetName, actionSetNamePointer);
901     LOGI("JNIThingsManagerCancelActionSet: Exit");
902     return ocResult;
903 }
904
905 JNIEXPORT jint JNICALL JNIThingsManagerGetActionSet(JNIEnv *env, jobject interfaceObject,
906         jobject resource,
907         jstring jActionSetName)
908 {
909     LOGI("JNIThingsManagerGetActionSet: Entry");
910
911     if ((!resource) || (!jActionSetName))
912     {
913         LOGE("JNIThingsManagerGetActionSet: resource or jActionSetName is NULL!");
914         return OC_STACK_INVALID_PARAM;
915     }
916
917     OCStackResult ocResult = OC_STACK_ERROR;
918
919     std::shared_ptr<OCResource> ocResource;
920     JniOcResource *jniOcResource = JniOcResource::getJniOcResourcePtr(env, resource);
921     if (jniOcResource)
922     {
923         ocResource = jniOcResource->getOCResource();
924     }
925
926     if (NULL == ocResource.get())
927     {
928         LOGE("JNIThingsManagerGetActionSet: Failed to get OCResource object!");
929         return ocResult;
930     }
931
932     const char *actionSetNamePointer = env->GetStringUTFChars(jActionSetName, 0);
933     std::string actionSetName(actionSetNamePointer);
934
935     ocResult = g_ThingsManager.getActionSet(ocResource, actionSetName,
936                                             &ThingsManagerCallbacks::onGetResponse);
937     if (OC_STACK_OK != ocResult)
938     {
939         LOGE("JNIThingsManagerGetActionSet: getActionSet is failed!");
940     }
941
942     env->ReleaseStringUTFChars(jActionSetName, actionSetNamePointer);
943     LOGI("JNIThingsManagerGetActionSet: Exit");
944     return ocResult;
945 }
946
947 JNIEXPORT jint JNICALL JNIThingsManagerDeleteActionSet(JNIEnv *env, jobject interfaceObject,
948         jobject resource,
949         jstring jActionSetName)
950 {
951     LOGI("JNIThingsManagerDeleteActionSet: Entry");
952
953     if ((!resource) || (!jActionSetName))
954     {
955         LOGE("JNIThingsManagerDeleteActionSet: resource or jActionSetName is NULL!");
956         return OC_STACK_INVALID_PARAM;
957     }
958
959     OCStackResult ocResult = OC_STACK_ERROR;
960
961     std::shared_ptr<OCResource> ocResource;
962     JniOcResource *jniOcResource = JniOcResource::getJniOcResourcePtr(env, resource);
963     if (jniOcResource)
964     {
965         ocResource = jniOcResource->getOCResource();
966     }
967
968     if (NULL == ocResource.get())
969     {
970         LOGE("JNIThingsManagerDeleteActionSet: Failed to get OCResource object!");
971         return ocResult;
972     }
973
974     const char *actionSetNamePointer = env->GetStringUTFChars(jActionSetName, 0);
975     std::string actionSetName(actionSetNamePointer);
976
977     ocResult = g_ThingsManager.deleteActionSet(ocResource, actionSetName,
978                &ThingsManagerCallbacks::onPutResponse);
979     if (OC_STACK_OK != ocResult)
980     {
981         LOGE("JNIThingsManagerDeleteActionSet: deleteActionSet is failed!");
982     }
983
984     env->ReleaseStringUTFChars(jActionSetName, actionSetNamePointer);
985     LOGI("JNIThingsManagerDeleteActionSet: Exit");
986     return ocResult;
987 }
988
989 jobject ocResourceHandleToJava(JNIEnv *env, jlong resourceHandle)
990 {
991     jclass resourceClass = GetJClass(TM_SERVICE_OCRESOURCEHANDLE_PATH);
992     if (NULL == resourceClass)
993     {
994         LOGE("ocResourceHandleToJava : failed to find OCResourceHandle java class!");
995         return NULL;
996     }
997
998     jmethodID constructor = env->GetMethodID(resourceClass, "<init>", "(J)V");
999     if (NULL == constructor)
1000     {
1001         LOGE("ocResourceHandleToJava: Failed to get constructor method!");
1002         return NULL;
1003     }
1004
1005     jobject resourceObj = (jobject) env->NewObject(resourceClass, constructor, resourceHandle);
1006     if (NULL == resourceObj)
1007     {
1008         LOGE("ocResourceHandleToJava: Failed to create OCResouceHandle java object!");
1009         return NULL;
1010     }
1011
1012     return resourceObj;
1013 }