2 * //******************************************************************
4 * // Copyright 2015 Intel Corporation.
6 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
8 * // Licensed under the Apache License, Version 2.0 (the "License");
9 * // you may not use this file except in compliance with the License.
10 * // You may obtain a copy of the License at
12 * // http://www.apache.org/licenses/LICENSE-2.0
14 * // Unless required by applicable law or agreed to in writing, software
15 * // distributed under the License is distributed on an "AS IS" BASIS,
16 * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * // See the License for the specific language governing permissions and
18 * // limitations under the License.
20 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
24 #include <android/log.h>
26 #include "cainterface.h"
27 #include "JniCaInterface.h"
28 #include "cautilinterface.h"
31 #define LOG_TAG "JNI_CA_INTERFACE"
32 #define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
33 #define LOGE(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
35 static jobject g_listenerObject = NULL;
36 static JavaVM *g_jvm = NULL;
38 JNIEXPORT jint JNI_OnLoad(JavaVM *jvm, void *reserved)
40 LOGI("CaInterface_initialize");
42 CANativeJNISetJavaVM(jvm);
44 return JNI_VERSION_1_6;
47 void JNI_OnUnload(JavaVM *jvm, void *reserved)
52 JNIEXPORT void JNICALL
53 Java_org_iotivity_ca_CaInterface_initialize
54 (JNIEnv *env, jclass clazz, jobject activity, jobject context)
56 LOGI("CaInterface_initialize");
58 CANativeSetActivity(env, activity);
59 CANativeJNISetContext(env, context);
62 void CAManagerConnectionStateChangedCB(CATransportAdapter_t adapter,
63 const char *remote_address,
66 LOGI("Callback - CAManagerConnectionStateChangedCB : type(%d), address(%s), connected(%d)",
67 adapter, remote_address, connected);
69 if (!g_listenerObject)
71 LOGE("g_listener is NULL, cannot have callback");
75 bool isAttached = false;
77 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
80 LOGI("AttachCurrentThread will be called for JNIEnv pointer");
81 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
85 LOGE("AttachCurrentThread has failed");
91 jclass jni_cls_listener = (*env)->GetObjectClass(env, g_listenerObject);
92 if (!jni_cls_listener)
94 LOGE("could not get jni_cls_listener");
98 jmethodID jni_mid_listener = (*env)->GetMethodID(env, jni_cls_listener,
99 "onConnectionStateChanged",
100 "(Lorg/iotivity/base/OcConnectivityType;"
101 "Ljava/lang/String;Z)V");
102 if (!jni_mid_listener)
104 LOGE("could not get Method ID");
108 jstring jni_address = (*env)->NewStringUTF(env, remote_address);
111 LOGE("jni_address is null");
115 jclass jni_cls_enum = (*env)->FindClass(env, "org/iotivity/base/OcConnectivityType");
118 LOGE("could not get jni_cls_enum");
122 jmethodID jni_mid_enum = (*env)->GetStaticMethodID(env, jni_cls_enum, "getInstance",
123 "(I)Lorg/iotivity/base/OcConnectivityType;");
126 LOGE("could not get Method ID (getInstance)");
130 jobject jni_adaptertype = (*env)->CallStaticObjectMethod(env, jni_cls_enum,
131 jni_mid_enum, adapter);
132 (*env)->CallVoidMethod(env, g_listenerObject, jni_mid_listener,
133 jni_adaptertype, jni_address,
134 (jboolean)connected);
139 (*g_jvm)->DetachCurrentThread(g_jvm);
142 LOGI("OUT - CAManagerConnectionStateChangedCB");
145 void CAManagerAdapterStateChangedCB(CATransportAdapter_t adapter, bool enabled)
147 LOGI("Callback - CAManagerAdapterStateChangedCB : type(%d), enabled(%d)",
150 if (!g_listenerObject)
152 LOGE("g_listener is NULL, cannot have callback");
156 bool isAttached = false;
158 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
161 LOGI("AttachCurrentThread will be called for JNIEnv pointer");
162 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
166 LOGE("AttachCurrentThread has failed");
172 jclass jni_cls_listener = (*env)->GetObjectClass(env, g_listenerObject);
173 if (!jni_cls_listener)
175 LOGE("could not get jni_cls_listener");
179 jmethodID jni_mid_listener = (*env)->GetMethodID(env, jni_cls_listener,
180 "onAdapterStateChanged",
181 "(Lorg/iotivity/base/OcConnectivityType;Z)V");
182 if (!jni_mid_listener)
184 LOGE("could not get Method ID");
188 jclass jni_cls_enum = (*env)->FindClass(env, "org/iotivity/base/OcConnectivityType");
191 LOGE("could not get jni_cls_enum");
195 jmethodID jni_mid_enum = (*env)->GetStaticMethodID(env, jni_cls_enum, "getInstance",
196 "(I)Lorg/iotivity/base/OcConnectivityType;");
199 LOGE("could not get Method ID (getInstance)");
203 jobject jni_adaptertype = (*env)->CallStaticObjectMethod(env, jni_cls_enum,
204 jni_mid_enum, adapter);
206 (*env)->CallVoidMethod(env, g_listenerObject, jni_mid_listener,
207 jni_adaptertype, (jboolean)enabled);
212 (*g_jvm)->DetachCurrentThread(g_jvm);
214 LOGI("OUT - CAManagerAdapterStateChangedCB");
217 JNIEXPORT void JNICALL
218 Java_org_iotivity_ca_CaInterface_caManagerInitialize(JNIEnv *env, jclass clazz,
219 jobject context, jobject listener)
221 LOGI("CaManagere_initialize");
223 g_listenerObject = (*env)->NewGlobalRef(env, listener);
225 CARegisterNetworkMonitorHandler(CAManagerAdapterStateChangedCB,
226 CAManagerConnectionStateChangedCB);
229 JNIEXPORT void JNICALL
230 Java_org_iotivity_ca_CaInterface_caManagerTerminate(JNIEnv *env, jclass clazz)
232 LOGI("CaManager_terminate");
234 CAUtilClientTerminate(env);
236 if (g_listenerObject)
238 (*env)->DeleteGlobalRef(env, g_listenerObject);
242 JNIEXPORT void JNICALL
243 Java_org_iotivity_ca_CaInterface_caManagerSetAutoConnectionDeviceInfo(JNIEnv *env,
247 LOGI("CaManager_setAutoConnectionDeviceInfo");
249 const char* address = (*env)->GetStringUTFChars(env, jaddress, NULL);
252 LOGE("address is null");
256 CASetAutoConnectionDeviceInfo(address);
259 JNIEXPORT void JNICALL
260 Java_org_iotivity_ca_CaInterface_caManagerUnsetAutoConnectionDeviceInfo(JNIEnv *env,
264 LOGI("CaManager_unsetAutoConnectionDeviceInfo");
266 const char* address = (*env)->GetStringUTFChars(env, jaddress, NULL);
269 LOGE("address is null");
273 CAUnsetAutoConnectionDeviceInfo(address);