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 ******************************************************************/
23 #include "jni_things_manager_jvm.h"
24 #include "jni_group_manager.h"
25 #include "jni_things_configuration.h"
26 #include "jni_things_maintenance.h"
27 #include "jni_things_manager_util.h"
28 #include "JniOcResource.h"
30 #define TM_ERROR_JNI_FOUND_CLASS_FAILED -2005
33 * This class provides functions for initializing the Java class path and Java class.
45 const char *szClassPath;
50 JClassMap(const char *path)
58 * This class provides functions for initializing the Java Class path
59 * and Java Class Object.
71 const char *szClassPath;
76 JObjectMap(const char *path)
83 static JClassMap gJClassMapArray[] =
85 JClassMap(TM_SERVICE_GROUP_MANAGER_CLASS_PATH),
86 JClassMap(TM_SERVICE_THINGS_CONFIGURATION_CLASS_PATH),
87 JClassMap(TM_SERVICE_THINGS_MAINTENANCE_CLASS_PATH),
88 JClassMap(TM_SERVICE_OCRESOURCE_PATH),
89 JClassMap(TM_SERVICE_OCREPRESENTATION_PATH),
90 JClassMap(TM_SERVICE_HEADER_OPTION_PATH),
91 JClassMap(TM_SERVICE_ACTIONSET_PATH),
92 JClassMap(TM_SERVICE_CAPABILITY_PATH),
93 JClassMap(TM_SERVICE_ACTION_PATH),
94 JClassMap(TM_SERVICE_PLATFORM_CLASS_PATH),
95 JClassMap(TM_SERVICE_PLATFORM_CONFIG_CLASS_PATH),
96 JClassMap(TM_SERVICE_OCRESOURCEHANDLE_PATH)
99 static JObjectMap gJObjectMapArray[] =
101 JObjectMap(TM_SERVICE_THINGS_CONFIGURATION_CLASS_PATH),
102 JObjectMap(TM_SERVICE_THINGS_MAINTENANCE_CLASS_PATH)
105 static JNINativeMethod gGroupManagerMethodTable[] =
107 { "nativeFindCandidateResource", "(Ljava/util/Vector;I)I", (void *) JNIGroupManagerFindCandidateResource},
108 { "nativeSubscribeCollectionPresence", "(Lorg/iotivity/base/OcResource;)I", (void *) JNIGroupManagerSubscribeCollectionPresence},
109 { "nativeBindResourceToGroup", "(Lorg/iotivity/base/OcResource;Lorg/iotivity/base/OcResourceHandle;)Lorg/iotivity/base/OcResourceHandle;", (void *) JNIGroupManagerBindResourceToGroup},
110 { "nativeAddActionSet", "(Lorg/iotivity/base/OcResource;Lorg/iotivity/service/tm/ActionSet;)I", (void *) JNIGroupManagerAddActionSet},
111 { "nativeExecuteActionSet", "(Lorg/iotivity/base/OcResource;Ljava/lang/String;)I", (void *) JNIGroupManagerExecuteActionSet},
112 { "nativeExecuteActionSetWithDelay", "(Lorg/iotivity/base/OcResource;Ljava/lang/String;J)I", (void *) JNIGroupManagerExecuteActionSetWithDelay},
113 { "nativeCancelActionSet", "(Lorg/iotivity/base/OcResource;Ljava/lang/String;)I", (void *) JNIGroupManagerCancelActionSet},
114 { "nativeGetActionSet", "(Lorg/iotivity/base/OcResource;Ljava/lang/String;)I", (void *) JNIGroupManagerGetActionSet},
115 { "nativeDeleteActionSet", "(Lorg/iotivity/base/OcResource;Ljava/lang/String;)I", (void *) JNIGroupManagerDeleteActionSet}
118 static JNINativeMethod gThingsConfigurationMethodTable[] =
120 { "nativeUpdateConfigurations", "(Lorg/iotivity/base/OcResource;Ljava/util/Map;)I", (void *) JNIThingsConfigurationUpdateConfigurations},
121 { "nativeGetConfigurations", "(Lorg/iotivity/base/OcResource;Ljava/util/Vector;)I", (void *) JNIThingsConfigurationGetConfigurations},
122 { "nativeGetListOfSupportedConfigurationUnits", "()Ljava/lang/String;", (void *) JNIThingsConfigurationGetListOfSupportedConfigurationUnits},
123 { "nativeDoBootstrap", "()I", (void *) JNIThingsConfigurationDoBootstrap},
126 static JNINativeMethod gThingsMaintenanceMethodTable[] =
128 { "nativeReboot", "(Lorg/iotivity/base/OcResource;)I", (void *) JNIThingsMaintenanceReboot},
129 { "nativeFactoryReset", "(Lorg/iotivity/base/OcResource;)I", (void *) JNIThingsMaintenanceFactoryReset},
130 { "nativeGetListOfSupportedMaintenanceUnits", "()Ljava/lang/String;", (void *) JNIThingsMaintenanceGetListOfSupportedConfigurationUnits}
134 static int gGroupManagerMethodTableSize = sizeof(gGroupManagerMethodTable) / sizeof(
135 gGroupManagerMethodTable[0]);
137 static int gThingsConfigurationMethodTableSize = sizeof(gThingsConfigurationMethodTable) / sizeof(
138 gThingsConfigurationMethodTable[0]);
140 static int gThingsMaintenanceMethodTableSize = sizeof(gThingsMaintenanceMethodTable) / sizeof(
141 gThingsMaintenanceMethodTable[0]);
144 int InitializeJClassMapArray(JNIEnv *env)
146 LOGI("InitializeJClassMapArray: Enter");
148 unsigned int nLen = sizeof(gJClassMapArray) / sizeof(JClassMap);
150 for (unsigned int i = 0; i < nLen; i++)
152 jclass classRef = env->FindClass(gJClassMapArray[i].szClassPath);
153 if (NULL == classRef)
155 LOGE("FindClass failed for [%s]", gJClassMapArray[i].szClassPath);
158 gJClassMapArray[i].classRef = (jclass)env->NewGlobalRef(classRef);
159 env->DeleteLocalRef(classRef);
162 LOGI("InitializeJClassMapArray: Exit");
166 jclass GetJClass(const char *szClassPath)
168 unsigned int nLen = sizeof(gJClassMapArray) / sizeof(JClassMap);
170 jclass classRef = NULL;
172 for (unsigned int i = 0; i < nLen; i++)
174 if (0 == strcmp(gJClassMapArray[i].szClassPath, szClassPath))
176 classRef = gJClassMapArray[i].classRef;
184 void DeleteClassMapArray(JNIEnv *env)
186 LOGI("DeleteClassMapArray: Enter");
188 unsigned int nLen = sizeof(gJClassMapArray) / sizeof(JClassMap);
189 for (unsigned int i = 0; i < nLen; i++)
191 if (NULL != gJClassMapArray[i].classRef)
193 env->DeleteGlobalRef(gJClassMapArray[i].classRef);
194 gJClassMapArray[i].classRef = NULL;
198 LOGI("DeleteClassMapArray: Exit");
201 int InitializeJObjectMapArray(JNIEnv *env)
203 LOGI("InitializeJObjectMapArray: Enter");
205 unsigned int nLen = sizeof(gJObjectMapArray) / sizeof(JObjectMap);
207 for (unsigned int i = 0; i < nLen; i++)
209 jclass classRef = env->FindClass(gJObjectMapArray[i].szClassPath);
210 if (NULL == classRef)
212 LOGE("InitializeJObjectMapArray: FindClass failed for [%s]", gJObjectMapArray[i].szClassPath);
216 std::string methodSignature = "()L";
217 methodSignature.append(gJObjectMapArray[i].szClassPath);
218 methodSignature.append(";");
220 // Get the object form "getInstance"
221 jmethodID methodid = env->GetStaticMethodID(classRef, "getInstance", methodSignature.c_str());
222 if (NULL == methodid)
224 LOGE("InitializeJObjectMapArray: GetStaticMethodID failed for [%s]",
225 gJObjectMapArray[i].szClassPath);
229 // Get the singleton object
230 jobject objectRef = (jobject)env->CallStaticObjectMethod(classRef, methodid);
231 if (NULL == objectRef)
233 LOGE("InitializeJObjectMapArray: CallStaticObjectMethod failed for [%s]",
234 gJObjectMapArray[i].szClassPath);
238 gJObjectMapArray[i].object = (jobject)env->NewGlobalRef(objectRef);
239 env->DeleteLocalRef(classRef);
242 LOGI("InitializeJObjectMapArray: Exit");
246 jobject GetJObjectInstance(const char *szClassPath)
248 unsigned int nLen = sizeof(gJObjectMapArray) / sizeof(JObjectMap);
250 jobject object = NULL;
252 for (unsigned int i = 0; i < nLen; i++)
254 if (0 == strcmp(gJObjectMapArray[i].szClassPath, szClassPath))
256 object = gJObjectMapArray[i].object;
264 void DeleteObjectMapArray(JNIEnv *env)
266 LOGI("DeleteObjectMapArray: Enter");
268 unsigned int nLen = sizeof(gJObjectMapArray) / sizeof(JObjectMap);
269 for (unsigned int i = 0; i < nLen; i++)
271 if (NULL != gJObjectMapArray[i].object)
273 env->DeleteGlobalRef(gJObjectMapArray[i].object);
274 gJObjectMapArray[i].object = NULL;
278 LOGI("DeleteObjectMapArray: Exit");
281 JavaVM *ThingsManagerJVM::m_jvm = NULL;
282 std::mutex ThingsManagerJVM::m_currentThreadMutex;
283 JNIEnv *ThingsManagerJVM::getEnv()
285 std::unique_lock<std::mutex> scoped_lock(m_currentThreadMutex);
289 LOGE("Failed to get JVM");
294 jint ret = m_jvm->GetEnv((void **)&env, JNI_CURRENT_VERSION);
300 if (0 > m_jvm->AttachCurrentThread(&env, NULL))
302 LOGE("Failed to attach current thread to env");
307 LOGE("JNI version not supported");
309 LOGE("Failed to get the environment");
314 void ThingsManagerJVM::releaseEnv()
316 std::unique_lock<std::mutex> scoped_lock(m_currentThreadMutex);
320 LOGE("Failed to release JVM");
324 m_jvm->DetachCurrentThread();
330 JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved)
332 LOGD("JNI_OnLoad: Enter");
336 LOGE("JNI_OnLoad: vm is invalid");
341 if (JNI_OK != vm->GetEnv((void **) &env, JNI_CURRENT_VERSION))
343 LOGE("JNI_OnLoad: Version check is failed!");
347 if (0 != InitializeJClassMapArray(env))
349 LOGE("JNI_OnLoad: Initialize JClass Array failed!");
353 if (0 != InitializeJObjectMapArray(env))
355 LOGE("JNI_OnLoad: Initialize JObject Array failed!");
360 jclass groupManagerClassRef = GetJClass(TM_SERVICE_GROUP_MANAGER_CLASS_PATH);
362 if (NULL == groupManagerClassRef)
364 LOGE("JNI_OnLoad: GetJClass gThingsManagerClass failed !");
368 env->RegisterNatives(groupManagerClassRef, gGroupManagerMethodTable,
369 gGroupManagerMethodTableSize);
371 //Things Configuration
372 jclass thingsConfigurationClassRef = GetJClass(TM_SERVICE_THINGS_CONFIGURATION_CLASS_PATH);
374 if (NULL == thingsConfigurationClassRef)
376 LOGE("JNI_OnLoad: GetJClass gThingsManagerClass failed !");
380 env->RegisterNatives(thingsConfigurationClassRef, gThingsConfigurationMethodTable,
381 gThingsConfigurationMethodTableSize);
384 jclass thingsMaintenanceClassRef = GetJClass(TM_SERVICE_THINGS_MAINTENANCE_CLASS_PATH);
386 if (NULL == thingsMaintenanceClassRef)
388 LOGE("JNI_OnLoad: GetJClass gThingsManagerClass failed !");
392 env->RegisterNatives(thingsMaintenanceClassRef, gThingsMaintenanceMethodTable,
393 gThingsMaintenanceMethodTableSize);
395 ThingsManagerJVM::m_jvm = vm;
397 LOGI("JNI_OnLoad: Exit");
398 return JNI_CURRENT_VERSION;
401 JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved)
403 LOGD("JNI_OnUnload: Enter");
406 if (JNI_OK != vm->GetEnv((void **)&env, JNI_CURRENT_VERSION))
408 LOGE("JNI_OnLoad: Version check is failed!");
412 // delete all class references
413 DeleteClassMapArray(env);
415 // delete all jobject
416 DeleteObjectMapArray(env);
418 LOGD("JNI_OnUnload: Exit");