Add APIs of cloud provisioning for Android and JNI layer
[platform/upstream/iotivity.git] / service / easy-setup / mediator / richsdk / android / 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 LOGI(...) __android_log_print(ANDROID_LOG_INFO, ESTAG, __VA_ARGS__)
39 #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, ESTAG, __VA_ARGS__)
40 #define 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_CloudProvisioningStatus;
47
48 extern jmethodID g_mid_RemoteEnrollee_ctor;
49 extern jmethodID g_mid_ESException_ctor;
50 extern jmethodID g_mid_CloudProvisioningStatus_ctor;
51
52 typedef void(*RemoveListenerCallback)(JNIEnv *env, jobject jListener);
53
54 /**
55  * @brief Get the native handle field
56  */
57 static jfieldID ESGetHandleField(JNIEnv *env, jobject jobj)
58 {
59     jclass cls = env->GetObjectClass(jobj);
60     return env->GetFieldID(cls, "m_nativeHandle", "J");
61 }
62
63 /**
64  * @brief Get the native handle
65  */
66 template <typename T>
67 static T *ESGetHandle(JNIEnv *env, jobject jobj)
68 {
69     jlong handle = env->GetLongField(jobj, ESGetHandleField(env, jobj));
70     return reinterpret_cast<T *>(handle);
71 }
72
73 /**
74  * @brief Set the native handle
75  */
76 template <typename T>
77 static void ESSetHandle(JNIEnv *env, jobject jobj, T *type)
78 {
79     jlong handle = reinterpret_cast<jlong>(type);
80
81     env->SetLongField(jobj, ESGetHandleField(env, jobj), handle);
82 }
83
84 /**
85  * @brief Get the JNI Environment
86  */
87 static JNIEnv *GetESJNIEnv(jint &ret)
88 {
89     JNIEnv *env = NULL;
90
91     ret = g_jvm->GetEnv((void **)&env, JNI_CURRENT_VERSION);
92     switch (ret)
93     {
94         case JNI_OK:
95             return env;
96         case JNI_EDETACHED:
97             if (g_jvm->AttachCurrentThread(&env, NULL) < 0)
98             {
99                 LOGE("Failed to get the environment");
100                 return nullptr;
101             }
102             else
103             {
104                 return env;
105             }
106
107         case JNI_EVERSION:
108             LOGE("JNI version not supported");
109         default:
110             LOGE("Failed to get the environment");
111             return nullptr;
112     }
113 }
114 #endif // __JNI_ES_JVM_H