1 /* *****************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 ******************************************************************/
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"
35 * GroupSynchronization static object
37 static GroupSynchronization g_GroupSynchronization;
39 JNIEXPORT jint JNICALL JNIGroupSynchronizationFindGroup(JNIEnv *env, jobject interfaceObject,
40 jobject jResourceTypes)
42 LOGI("JNIGroupSynchronizationFindGroup: Enter");
46 LOGE("JNIGroupSynchronizationFindGroup: jResourceTypes is NULL!");
47 return OC_STACK_INVALID_PARAM;
50 OCStackResult ocResult = OC_STACK_ERROR;
53 ocResult = g_GroupSynchronization.findGroup((convertStringVector(env, jResourceTypes)),
54 &GroupSynchronizationCallbacks::onFoundGroup);
55 if (OC_STACK_OK != ocResult)
57 LOGE("JNIGroupSynchronizationFindGroup: findGroup failed!");
61 catch (InitializeException &e)
63 LOGE("JNIGroupSynchronizationFindGroup: Exception occurred! %s, %d", e.reason().c_str(),
67 LOGI("JNIGroupSynchronizationFindGroup: Exit");
71 JNIEXPORT jint JNICALL JNIGroupSynchronizationCreateGroup(JNIEnv *env, jobject interfaceObject,
72 jstring jResourceType)
74 LOGI("JNIGroupSynchronizationCreateGroup: Enter");
78 LOGE("JNIGroupSynchronizationCreateGroup: jResourceType is NULL!");
79 return OC_STACK_INVALID_PARAM;
82 OCStackResult ocResult = OC_STACK_ERROR;
84 const char *resourceTypePointer = env->GetStringUTFChars(jResourceType, NULL);
85 if (NULL == resourceTypePointer)
87 LOGE("JNIGroupSynchronizationCreateGroup: Failed to convert jstring to char string!");
88 return OC_STACK_NO_MEMORY;
91 std::string resourceType(resourceTypePointer);
94 ocResult = g_GroupSynchronization.createGroup(resourceType);
95 if (OC_STACK_OK != ocResult)
97 LOGE("JNIGroupSynchronizationCreateGroup: CreateGroup failed!");
100 catch (InitializeException &e)
102 LOGE("JNIGroupSynchronizationCreateGroup: Exception occurred! %s, %d", e.reason().c_str(),
105 env->ReleaseStringUTFChars(jResourceType, resourceTypePointer);
106 LOGI("JNIGroupSynchronizationCreateGroup: Exit");
110 JNIEXPORT jint JNICALL JNIGroupSynchronizationJoinGroupString(JNIEnv *env, jobject interfaceObject,
111 jstring jResourceType, jobject jResourceHandle)
113 LOGI("JNIGroupSynchronizationJoinGroupString: Enter");
115 if ((!jResourceType) || (!jResourceHandle))
117 LOGE("JNIGroupSynchronizationJoinGroupString: jResourceType or jResourceHandle is NULL!");
118 return OC_STACK_INVALID_PARAM;
121 OCStackResult ocResult = OC_STACK_ERROR;
123 const char *resourceTypePointer = env->GetStringUTFChars(jResourceType, NULL);
124 if (NULL == resourceTypePointer)
126 LOGE("JNIGroupSynchronizationJoinGroupString: Failed to convert jstring to char string!");
127 return OC_STACK_NO_MEMORY;
130 std::string resourceType(resourceTypePointer);
132 JniOcResourceHandle *jniOcResourceHandle = JniOcResourceHandle::getJniOcResourceHandlePtr(env,
136 OCResourceHandle ocResourceHandle = jniOcResourceHandle->getOCResourceHandle();
137 ocResult = g_GroupSynchronization.joinGroup(resourceType, ocResourceHandle);
138 if (OC_STACK_OK != ocResult)
140 LOGE("JNIGroupSynchronizationJoinGroupString: joinGroup failed!");
143 catch (InitializeException &e)
145 LOGE("JNIGroupSynchronizationJoinGroupString: Exception occurred! %s, %d", e.reason().c_str(),
148 env->ReleaseStringUTFChars(jResourceType, resourceTypePointer);
149 LOGI("JNIGroupSynchronizationJoinGroupString: Exit");
153 JNIEXPORT jint JNICALL JNIGroupSynchronizationJoinGroupObject(JNIEnv *env, jobject interfaceObject,
154 jobject jResource, jobject jResourceHandle)
156 LOGI("JNIGroupSynchronizationJoinGroupObject: Enter");
158 if ((!jResource) || (!jResourceHandle))
160 LOGE("JNIGroupSynchronizationJoinGroupObject: jResource or jResourceHandle is NULL!");
161 return OC_STACK_INVALID_PARAM;
164 OCStackResult ocResult = OC_STACK_ERROR;
166 std::shared_ptr<OCResource> ocResource;
167 JniOcResource *jniOcResource = JniOcResource::getJniOcResourcePtr(env, jResource);
170 ocResource = jniOcResource->getOCResource();
173 if (NULL == ocResource.get())
175 LOGE("JNIGroupSynchronizationJoinGroupObject: Failed to get OCResource object!");
179 JniOcResourceHandle *jniOcResourceHandle = JniOcResourceHandle::getJniOcResourceHandlePtr(env,
184 OCResourceHandle resHandle = jniOcResourceHandle->getOCResourceHandle();
186 ocResult = g_GroupSynchronization.joinGroup(ocResource, resHandle);
187 if (OC_STACK_OK != ocResult)
189 LOGE("JNIGroupSynchronizationJoinGroupObject: joinGroup failed!");
193 catch (InitializeException &e)
195 LOGE("JNIGroupSynchronizationJoinGroupObject: Exception occurred! %s, %d", e.reason().c_str(),
199 LOGI("JNIGroupSynchronizationJoinGroupObject: Exit");
203 JNIEXPORT jint JNICALL JNIGroupSynchronizationLeaveGroup(JNIEnv *env, jobject interfaceObject,
204 jstring jResourceType,
205 jobject jResourceHandle)
207 LOGI("JNIGroupSynchronizationLeaveGroup: Enter");
209 if ((!jResourceType) || (!jResourceHandle))
211 LOGE("JNIGroupSynchronizationLeaveGroup: jResourceType or jResourceHandle is NULL!");
212 return OC_STACK_INVALID_PARAM;
215 OCStackResult ocResult = OC_STACK_ERROR;
217 const char *resourceTypePointer = env->GetStringUTFChars(jResourceType, NULL);
218 if (NULL == resourceTypePointer)
220 LOGE("JNIGroupSynchronizationLeaveGroup: Failed to convert jstring to char string!");
221 return OC_STACK_NO_MEMORY;
224 std::string resourceType(resourceTypePointer);
225 JniOcResourceHandle *jniOcResourceHandle = JniOcResourceHandle::getJniOcResourceHandlePtr(env,
230 OCResourceHandle ocResourceHandle = jniOcResourceHandle->getOCResourceHandle();
232 ocResult = g_GroupSynchronization.leaveGroup(resourceType, ocResourceHandle);
233 if (OC_STACK_OK != ocResult)
235 LOGE("JNIGroupSynchronizationLeaveGroup: leaveGroup failed!");
238 catch (InitializeException &e)
240 LOGE("JNIGroupSynchronizationLeaveGroup: Exception occurred! %s, %d", e.reason().c_str(),
243 env->ReleaseStringUTFChars(jResourceType, resourceTypePointer);
244 LOGI("JNIGroupSynchronizationLeaveGroup: Exit");
248 JNIEXPORT jint JNICALL JNIGroupSynchronizationLeaveGroupForResource(JNIEnv *env,
249 jobject interfaceObject,
251 jstring jResourceType,
252 jobject jResourceHandle)
254 LOGI("JNIGroupSynchronizationLeaveGroupForResource: Enter");
256 if ((!jResource) || (!jResourceType) || (!jResourceHandle))
258 LOGE("JNIGroupSynchronizationLeaveGroupForResource: jResourceType or jResourceHandle is NULL!");
259 return OC_STACK_INVALID_PARAM;
262 OCStackResult ocResult = OC_STACK_ERROR;
264 JniOcResource *jniOcResource = JniOcResource::getJniOcResourcePtr(env, jResource);
265 if (NULL == jniOcResource)
267 LOGE("JNIGroupSynchronizationLeaveGroupForResource: Failed to get jni OcResource!");
271 std::shared_ptr<OCResource> ocResource = jniOcResource->getOCResource();
272 if (NULL == ocResource.get())
274 LOGE("JNIGroupSynchronizationLeaveGroupForResource: Failed to get OCResource object!");
278 const char *resourceTypePointer = env->GetStringUTFChars(jResourceType, NULL);
279 if (NULL == resourceTypePointer)
281 LOGE("JNIGroupSynchronizationLeaveGroupForResource: Failed to convert jstring to char string!");
282 return OC_STACK_NO_MEMORY;
285 std::string resourceType(resourceTypePointer);
286 JniOcResourceHandle *jniOcResourceHandle = JniOcResourceHandle::getJniOcResourceHandlePtr(env,
291 OCResourceHandle ocResourceHandle = jniOcResourceHandle->getOCResourceHandle();
293 ocResult = g_GroupSynchronization.leaveGroup(ocResource, resourceType, ocResourceHandle);
294 if (OC_STACK_OK != ocResult)
296 LOGE("JNIGroupSynchronizationLeaveGroupForResource: leaveGroup failed!");
299 catch (InitializeException &e)
301 LOGE("JNIGroupSynchronizationLeaveGroupForResource: Exception occurred! %s, %d", e.reason().c_str(),
305 env->ReleaseStringUTFChars(jResourceType, resourceTypePointer);
306 LOGI("JNIGroupSynchronizationLeaveGroupForResource: Exit");
310 JNIEXPORT void JNICALL JNIGroupSynchronizationDeleteGroup(JNIEnv *env, jobject interfaceObject,
311 jstring jcollectionResourceType)
313 LOGI("JNIGroupSynchronizationDeleteGroup: Enter");
315 if (!jcollectionResourceType)
317 LOGE("JNIGroupSynchronizationDeleteGroup: jcollectionResourceType is NULL!");
321 const char *collectionResourceTypePointer = env->GetStringUTFChars(jcollectionResourceType, NULL);
322 if (NULL == collectionResourceTypePointer)
324 LOGE("JNIGroupSynchronizationDeleteGroup: Failed to convert jstring to char string!");
328 std::string collectionResourceType(collectionResourceTypePointer);
331 g_GroupSynchronization.deleteGroup(collectionResourceType);
333 catch (InitializeException &e)
335 LOGE("JNIGroupSynchronizationDeleteGroup: Exception occurred! %s, %d", e.reason().c_str(),
339 env->ReleaseStringUTFChars(jcollectionResourceType, collectionResourceTypePointer);
340 LOGI("JNIGroupSynchronizationDeleteGroup: Exit");
343 JNIEXPORT jobject JNICALL JNIGroupSynchronizationGetGroupList(JNIEnv *env, jobject interfaceObject)
345 LOGI("JNIGroupSynchronizationGetGroupList: Enter");
347 std::map< std::string, OCResourceHandle> groupListMap;
348 jobject jGroupListMap;
350 groupListMap = g_GroupSynchronization.getGroupList();
351 if (groupListMap.empty())
353 LOGD("getGroupList Map is empty");
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;");
363 for (auto it = groupListMap.begin(); it != groupListMap.end(); ++it)
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);
371 LOGI("JNIGroupSynchronizationGetGroupList: Exit");
372 return jGroupListMap;