1ab32fe1cb925dafc3ed267779be1b15d3015dd9
[platform/upstream/iotivity.git] / service / things-manager / sdk / java / jni / tm / src / jni_group_synchronization.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 "jni_group_synchronization.h"
22 #include "JniOcResource.h"
23 #include "JniOcResourceHandle.h"
24 #include "GroupManager.h"
25 #include "ActionSet.h"
26 #include "jni_things_manager_jvm.h"
27 #include "jni_things_manager_util.h"
28 #include "jni_group_synchronization_callbacks.h"
29 #include "jni_action_set.h"
30
31 using namespace OC;
32 using namespace OIC;
33
34 /**
35  * GroupSynchronization static object
36  */
37 static GroupSynchronization g_GroupSynchronization;
38
39 JNIEXPORT jint JNICALL JNIGroupSynchronizationFindGroup(JNIEnv *env, jobject interfaceObject,
40         jobject jResourceTypes)
41 {
42     LOGI("JNIGroupSynchronizationFindGroup: Enter");
43
44     if (!jResourceTypes)
45     {
46         LOGE("JNIGroupSynchronizationFindGroup: jResourceTypes is NULL!");
47         return OC_STACK_INVALID_PARAM;
48     }
49
50     OCStackResult ocResult = OC_STACK_ERROR;
51     try
52     {
53         ocResult = g_GroupSynchronization.findGroup((convertStringVector(env, jResourceTypes)),
54                    &GroupSynchronizationCallbacks::onFoundGroup);
55         if (OC_STACK_OK != ocResult)
56         {
57             LOGE("JNIGroupSynchronizationFindGroup: findGroup failed!");
58             return ocResult;
59         }
60     }
61     catch (InitializeException &e)
62     {
63         LOGE("JNIGroupSynchronizationFindGroup: Exception occurred! %s, %d", e.reason().c_str(),
64              e.code());
65         return ocResult;
66     }
67     LOGI("JNIGroupSynchronizationFindGroup: Exit");
68     return ocResult;
69 }
70
71 JNIEXPORT jint JNICALL JNIGroupSynchronizationCreateGroup(JNIEnv *env, jobject interfaceObject,
72         jstring jResourceType)
73 {
74     LOGI("JNIGroupSynchronizationCreateGroup: Enter");
75
76     if (!jResourceType)
77     {
78         LOGE("JNIGroupSynchronizationCreateGroup: jResourceType is NULL!");
79         return OC_STACK_INVALID_PARAM;
80     }
81
82     OCStackResult ocResult = OC_STACK_ERROR;
83
84     const char *resourceTypePointer = env->GetStringUTFChars(jResourceType, NULL);
85     if (NULL == resourceTypePointer)
86     {
87         LOGE("JNIGroupSynchronizationCreateGroup: Failed to convert jstring to char string!");
88         return OC_STACK_NO_MEMORY;
89     }
90
91     std::string resourceType(resourceTypePointer);
92     try
93     {
94         ocResult =  g_GroupSynchronization.createGroup(resourceType);
95         if (OC_STACK_OK != ocResult)
96         {
97             LOGE("JNIGroupSynchronizationCreateGroup: CreateGroup failed!");
98         }
99     }
100     catch (InitializeException &e)
101     {
102         LOGE("JNIGroupSynchronizationCreateGroup: Exception occurred! %s, %d", e.reason().c_str(),
103              e.code());
104     }
105     env->ReleaseStringUTFChars(jResourceType, resourceTypePointer);
106     LOGI("JNIGroupSynchronizationCreateGroup: Exit");
107     return ocResult;
108 }
109
110 JNIEXPORT jint JNICALL JNIGroupSynchronizationJoinGroupString(JNIEnv *env, jobject interfaceObject,
111         jstring jResourceType, jobject jResourceHandle)
112 {
113     LOGI("JNIGroupSynchronizationJoinGroupString: Enter");
114
115     if ((!jResourceType) || (!jResourceHandle))
116     {
117         LOGE("JNIGroupSynchronizationJoinGroupString: jResourceType or jResourceHandle is NULL!");
118         return OC_STACK_INVALID_PARAM;
119     }
120
121     OCStackResult ocResult = OC_STACK_ERROR;
122
123     const char *resourceTypePointer = env->GetStringUTFChars(jResourceType, NULL);
124     if (NULL == resourceTypePointer)
125     {
126         LOGE("JNIGroupSynchronizationJoinGroupString: Failed to convert jstring to char string!");
127         return OC_STACK_NO_MEMORY;
128     }
129
130     std::string resourceType(resourceTypePointer);
131
132     JniOcResourceHandle *jniOcResourceHandle = JniOcResourceHandle::getJniOcResourceHandlePtr(env,
133             jResourceHandle);
134     try
135     {
136         OCResourceHandle ocResourceHandle = jniOcResourceHandle->getOCResourceHandle();
137         ocResult = g_GroupSynchronization.joinGroup(resourceType, ocResourceHandle);
138         if (OC_STACK_OK != ocResult)
139         {
140             LOGE("JNIGroupSynchronizationJoinGroupString: joinGroup failed!");
141         }
142     }
143     catch (InitializeException &e)
144     {
145         LOGE("JNIGroupSynchronizationJoinGroupString: Exception occurred! %s, %d", e.reason().c_str(),
146              e.code());
147     }
148     env->ReleaseStringUTFChars(jResourceType, resourceTypePointer);
149     LOGI("JNIGroupSynchronizationJoinGroupString: Exit");
150     return ocResult;
151 }
152
153 JNIEXPORT jint JNICALL JNIGroupSynchronizationJoinGroupObject(JNIEnv *env, jobject interfaceObject,
154         jobject jResource, jobject jResourceHandle)
155 {
156     LOGI("JNIGroupSynchronizationJoinGroupObject: Enter");
157
158     if ((!jResource) || (!jResourceHandle))
159     {
160         LOGE("JNIGroupSynchronizationJoinGroupObject: jResource or jResourceHandle is NULL!");
161         return OC_STACK_INVALID_PARAM;
162     }
163
164     OCStackResult ocResult = OC_STACK_ERROR;
165
166     std::shared_ptr<OCResource> ocResource;
167     JniOcResource *jniOcResource = JniOcResource::getJniOcResourcePtr(env, jResource);
168     if (jniOcResource)
169     {
170         ocResource = jniOcResource->getOCResource();
171     }
172
173     if (NULL == ocResource.get())
174     {
175         LOGE("JNIGroupSynchronizationJoinGroupObject: Failed to get OCResource object!");
176         return ocResult;
177     }
178
179     JniOcResourceHandle *jniOcResourceHandle = JniOcResourceHandle::getJniOcResourceHandlePtr(env,
180             jResourceHandle);
181
182     try
183     {
184         OCResourceHandle resHandle = jniOcResourceHandle->getOCResourceHandle();
185
186         ocResult = g_GroupSynchronization.joinGroup(ocResource, resHandle);
187         if (OC_STACK_OK != ocResult)
188         {
189             LOGE("JNIGroupSynchronizationJoinGroupObject: joinGroup failed!");
190             return ocResult;
191         }
192     }
193     catch (InitializeException &e)
194     {
195         LOGE("JNIGroupSynchronizationJoinGroupObject: Exception occurred! %s, %d", e.reason().c_str(),
196              e.code());
197         return ocResult;
198     }
199     LOGI("JNIGroupSynchronizationJoinGroupObject: Exit");
200     return ocResult;
201 }
202
203 JNIEXPORT jint JNICALL JNIGroupSynchronizationLeaveGroup(JNIEnv *env, jobject interfaceObject,
204         jstring jResourceType,
205         jobject jResourceHandle)
206 {
207     LOGI("JNIGroupSynchronizationLeaveGroup: Enter");
208
209     if ((!jResourceType) || (!jResourceHandle))
210     {
211         LOGE("JNIGroupSynchronizationLeaveGroup: jResourceType or jResourceHandle is NULL!");
212         return OC_STACK_INVALID_PARAM;
213     }
214
215     OCStackResult ocResult = OC_STACK_ERROR;
216
217     const char *resourceTypePointer = env->GetStringUTFChars(jResourceType, NULL);
218     if (NULL == resourceTypePointer)
219     {
220         LOGE("JNIGroupSynchronizationLeaveGroup: Failed to convert jstring to char string!");
221         return OC_STACK_NO_MEMORY;
222     }
223
224     std::string resourceType(resourceTypePointer);
225     JniOcResourceHandle *jniOcResourceHandle = JniOcResourceHandle::getJniOcResourceHandlePtr(env,
226             jResourceHandle);
227
228     try
229     {
230         OCResourceHandle ocResourceHandle = jniOcResourceHandle->getOCResourceHandle();
231
232         ocResult = g_GroupSynchronization.leaveGroup(resourceType, ocResourceHandle);
233         if (OC_STACK_OK != ocResult)
234         {
235             LOGE("JNIGroupSynchronizationLeaveGroup: leaveGroup failed!");
236         }
237     }
238     catch (InitializeException &e)
239     {
240         LOGE("JNIGroupSynchronizationLeaveGroup: Exception occurred! %s, %d", e.reason().c_str(),
241              e.code());
242     }
243     env->ReleaseStringUTFChars(jResourceType, resourceTypePointer);
244     LOGI("JNIGroupSynchronizationLeaveGroup: Exit");
245     return ocResult;
246 }
247
248 JNIEXPORT jint JNICALL JNIGroupSynchronizationLeaveGroupForResource(JNIEnv *env,
249         jobject interfaceObject,
250         jobject jResource,
251         jstring jResourceType,
252         jobject jResourceHandle)
253 {
254     LOGI("JNIGroupSynchronizationLeaveGroupForResource: Enter");
255
256     if ((!jResource) || (!jResourceType) || (!jResourceHandle))
257     {
258         LOGE("JNIGroupSynchronizationLeaveGroupForResource: jResourceType or jResourceHandle is NULL!");
259         return OC_STACK_INVALID_PARAM;
260     }
261
262     OCStackResult ocResult = OC_STACK_ERROR;
263
264     JniOcResource *jniOcResource = JniOcResource::getJniOcResourcePtr(env, jResource);
265     if (NULL == jniOcResource)
266     {
267         LOGE("JNIGroupSynchronizationLeaveGroupForResource: Failed to get jni OcResource!");
268         return ocResult;
269     }
270
271     std::shared_ptr<OCResource> ocResource = jniOcResource->getOCResource();
272     if (NULL == ocResource.get())
273     {
274         LOGE("JNIGroupSynchronizationLeaveGroupForResource: Failed to get OCResource object!");
275         return ocResult;
276     }
277
278     const char *resourceTypePointer = env->GetStringUTFChars(jResourceType, NULL);
279     if (NULL == resourceTypePointer)
280     {
281         LOGE("JNIGroupSynchronizationLeaveGroupForResource: Failed to convert jstring to char string!");
282         return OC_STACK_NO_MEMORY;
283     }
284
285     std::string resourceType(resourceTypePointer);
286     JniOcResourceHandle *jniOcResourceHandle = JniOcResourceHandle::getJniOcResourceHandlePtr(env,
287             jResourceHandle);
288
289     try
290     {
291         OCResourceHandle ocResourceHandle = jniOcResourceHandle->getOCResourceHandle();
292
293         ocResult = g_GroupSynchronization.leaveGroup(ocResource, resourceType, ocResourceHandle);
294         if (OC_STACK_OK != ocResult)
295         {
296             LOGE("JNIGroupSynchronizationLeaveGroupForResource: leaveGroup failed!");
297         }
298     }
299     catch (InitializeException &e)
300     {
301         LOGE("JNIGroupSynchronizationLeaveGroupForResource: Exception occurred! %s, %d", e.reason().c_str(),
302              e.code());
303     }
304
305     env->ReleaseStringUTFChars(jResourceType, resourceTypePointer);
306     LOGI("JNIGroupSynchronizationLeaveGroupForResource: Exit");
307     return ocResult;
308 }
309
310 JNIEXPORT void JNICALL JNIGroupSynchronizationDeleteGroup(JNIEnv *env, jobject interfaceObject,
311         jstring jcollectionResourceType)
312 {
313     LOGI("JNIGroupSynchronizationDeleteGroup: Enter");
314
315     if (!jcollectionResourceType)
316     {
317         LOGE("JNIGroupSynchronizationDeleteGroup: jcollectionResourceType is NULL!");
318         return;
319     }
320
321     const char *collectionResourceTypePointer = env->GetStringUTFChars(jcollectionResourceType, NULL);
322     if (NULL == collectionResourceTypePointer)
323     {
324         LOGE("JNIGroupSynchronizationDeleteGroup: Failed to convert jstring to char string!");
325         return;
326     }
327
328     std::string collectionResourceType(collectionResourceTypePointer);
329     try
330     {
331         g_GroupSynchronization.deleteGroup(collectionResourceType);
332     }
333     catch (InitializeException &e)
334     {
335         LOGE("JNIGroupSynchronizationDeleteGroup: Exception occurred! %s, %d", e.reason().c_str(),
336              e.code());
337     }
338
339     env->ReleaseStringUTFChars(jcollectionResourceType, collectionResourceTypePointer);
340     LOGI("JNIGroupSynchronizationDeleteGroup: Exit");
341 }
342
343 JNIEXPORT jobject JNICALL JNIGroupSynchronizationGetGroupList(JNIEnv *env, jobject interfaceObject)
344 {
345     LOGI("JNIGroupSynchronizationGetGroupList: Enter");
346
347     std::map< std::string, OCResourceHandle> groupListMap;
348     jobject jGroupListMap;
349
350     groupListMap = g_GroupSynchronization.getGroupList();
351     if (groupListMap.empty())
352     {
353         LOGD("getGroupList Map is empty");
354         return NULL;
355     }
356
357     jclass clazz = env->FindClass("java/util/HashMap");
358     jmethodID init = env->GetMethodID(clazz, "<init>", "()V");
359     jGroupListMap = env->NewObject(clazz, init);
360     jmethodID putMethod = env->GetMethodID(clazz, "put",
361                                            "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
362
363     for (auto it = groupListMap.begin(); it != groupListMap.end(); ++it)
364     {
365         jstring key = (*env).NewStringUTF( (*it).first.c_str() );
366         JniOcResourceHandle *jniOcResourceHandle = new JniOcResourceHandle(((*it).second));
367         jobject value = ocResourceHandleToJava(env, reinterpret_cast<jlong>(jniOcResourceHandle));
368         env->CallObjectMethod(jGroupListMap, putMethod, key, value);
369     }
370
371     LOGI("JNIGroupSynchronizationGetGroupList: Exit");
372     return jGroupListMap;
373 }
374
375