Merge remote-tracking branch 'origin/extended-easysetup'
[platform/upstream/iotivity.git] / service / easy-setup / mediator / richsdk / android / EasySetupCore / src / main / jni / JniJvm.h
1 /******************************************************************
2  *
3  * Copyright 2016 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 /**
22   * @file   JniJvm.h
23   *
24   * @brief  This file contains the essential declarations and functions required
25   *            for JNI implementation
26   */
27
28 #ifndef __JNI_ES_JVM_H
29 #define __JNI_ES_JVM_H
30
31 #include <jni.h>
32 #include <string>
33 #include <android/log.h>
34
35 #define ESTAG "ES-JNI"
36 #define JNI_CURRENT_VERSION JNI_VERSION_1_6
37
38 #define ES_LOGI(...) __android_log_print(ANDROID_LOG_INFO, ESTAG, __VA_ARGS__)
39 #define ES_LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, ESTAG, __VA_ARGS__)
40 #define ES_LOGE(...) __android_log_print(ANDROID_LOG_ERROR, ESTAG, __VA_ARGS__)
41
42 extern JavaVM *g_jvm;
43
44 extern jclass g_cls_RemoteEnrollee;
45 extern jclass g_cls_ESException;
46 extern jclass g_cls_EnrolleeStatus;
47 extern jclass g_cls_EnrolleeConf;
48 extern jclass g_cls_getEnrolleeStatus;
49 extern jclass g_cls_getConfigurationStatus;
50 extern jclass g_cls_SecurityProvisioningStatus;
51 extern jclass g_cls_DevicePropProvisioningStatus;
52 extern jclass g_cls_CloudPropProvisioningStatus;
53 extern jclass g_cls_Integer;
54 extern jclass g_cls_OcRepresentation;
55
56 extern jmethodID g_mid_RemoteEnrollee_ctor;
57 extern jmethodID g_mid_ESException_ctor;
58 extern jmethodID g_mid_EnrolleeStatus_ctor;
59 extern jmethodID g_mid_EnrolleeConf_ctor;
60 extern jmethodID g_mid_getEnrolleeStatus_ctor;
61 extern jmethodID g_mid_getConfigurationStatus_ctor;
62 extern jmethodID g_mid_SecurityProvisioningStatus_ctor;
63 extern jmethodID g_mid_DevicePropProvisioningStatus_ctor;
64 extern jmethodID g_mid_CloudPropProvisioningStatus_ctor;
65 extern jmethodID g_mid_Integer_ctor;
66 extern jmethodID g_mid_OcRepresentation_N_ctor_bool;
67
68 typedef void(*RemoveListenerCallback)(JNIEnv *env, jobject jListener);
69
70 /**
71  * @brief Get the native handle field
72  */
73 static jfieldID ESGetHandleField(JNIEnv *env, jobject jobj)
74 {
75     jclass cls = env->GetObjectClass(jobj);
76     return env->GetFieldID(cls, "m_nativeHandle", "J");
77 }
78
79 /**
80  * @brief Get the native handle
81  */
82 template <typename T>
83 static T *ESGetHandle(JNIEnv *env, jobject jobj)
84 {
85     jlong handle = env->GetLongField(jobj, ESGetHandleField(env, jobj));
86     return reinterpret_cast<T *>(handle);
87 }
88
89 /**
90  * @brief Set the native handle
91  */
92 template <typename T>
93 static void ESSetHandle(JNIEnv *env, jobject jobj, T *type)
94 {
95     jlong handle = reinterpret_cast<jlong>(type);
96
97     env->SetLongField(jobj, ESGetHandleField(env, jobj), handle);
98 }
99
100 /**
101  * @brief Get the JNI Environment
102  */
103 static JNIEnv *GetESJNIEnv(jint &ret)
104 {
105     JNIEnv *env = NULL;
106
107     ret = g_jvm->GetEnv((void **)&env, JNI_CURRENT_VERSION);
108     switch (ret)
109     {
110         case JNI_OK:
111             return env;
112         case JNI_EDETACHED:
113             if (g_jvm->AttachCurrentThread(&env, NULL) < 0)
114             {
115                 ES_LOGE("Failed to get the environment");
116                 return nullptr;
117             }
118             else
119             {
120                 return env;
121             }
122
123         case JNI_EVERSION:
124             ES_LOGE("JNI version not supported");
125         default:
126             ES_LOGE("Failed to get the environment");
127             return nullptr;
128     }
129 }
130 #endif // __JNI_ES_JVM_H