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_foundDeviceListenerObject = NULL;
36 static jobject g_listenerObject = NULL;
37 static JavaVM *g_jvm = NULL;
38 static jclass g_jni_cls_enum = NULL;
39 static jmethodID g_jni_mid_enum = NULL;
41 JNIEXPORT jint JNI_OnLoad(JavaVM *jvm, void *reserved)
43 LOGI("CaInterface_initialize");
45 CANativeJNISetJavaVM(jvm);
47 return JNI_VERSION_1_6;
50 void JNI_OnUnload(JavaVM *jvm, void *reserved)
55 JNIEXPORT void JNICALL
56 Java_org_iotivity_ca_CaInterface_initialize
57 (JNIEnv *env, jclass clazz, jobject activity, jobject context)
59 LOGI("CaInterface_initialize");
61 CANativeSetActivity(env, activity);
62 CANativeJNISetContext(env, context);
65 void CAManagerConnectionStateChangedCB(const CAEndpoint_t *info,
74 if (!g_listenerObject)
76 LOGE("g_listener is NULL, cannot have callback");
80 LOGI("Callback - CAManagerConnectionStateChangedCB : type(%d), address(%s), connected(%d)",
81 info->adapter, info->addr, connected);
83 bool isAttached = false;
85 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
88 LOGI("AttachCurrentThread will be called for JNIEnv pointer");
89 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
93 LOGE("AttachCurrentThread has failed");
99 jclass jni_cls_listener = (*env)->GetObjectClass(env, g_listenerObject);
100 if (!jni_cls_listener)
102 LOGE("could not get jni_cls_listener");
106 jmethodID jni_mid_listener = (*env)->GetMethodID(env, jni_cls_listener,
107 "onConnectionStateChanged",
108 "(Lorg/iotivity/base/OcConnectivityType;"
109 "Ljava/lang/String;Z)V");
110 if (!jni_mid_listener)
112 LOGE("could not get Method ID");
116 jstring jni_address = (*env)->NewStringUTF(env, info->addr);
119 LOGE("jni_address is null");
123 if (g_jni_cls_enum && g_jni_mid_enum)
125 jobject jni_adaptertype = (*env)->CallStaticObjectMethod(env, g_jni_cls_enum,
126 g_jni_mid_enum, info->adapter);
127 (*env)->CallVoidMethod(env, g_listenerObject, jni_mid_listener,
128 jni_adaptertype, jni_address,
129 (jboolean)connected);
135 (*g_jvm)->DetachCurrentThread(g_jvm);
138 LOGI("OUT - CAManagerConnectionStateChangedCB");
141 void CAManagerAdapterStateChangedCB(CATransportAdapter_t adapter, bool enabled)
143 LOGI("Callback - CAManagerAdapterStateChangedCB : type(%d), enabled(%d)",
146 if (!g_listenerObject)
148 LOGE("g_listener is NULL, cannot have callback");
152 bool isAttached = false;
154 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
157 LOGI("AttachCurrentThread will be called for JNIEnv pointer");
158 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
162 LOGE("AttachCurrentThread has failed");
168 jclass jni_cls_listener = (*env)->GetObjectClass(env, g_listenerObject);
169 if (!jni_cls_listener)
171 LOGE("could not get jni_cls_listener");
175 jmethodID jni_mid_listener = (*env)->GetMethodID(env, jni_cls_listener,
176 "onAdapterStateChanged",
177 "(Lorg/iotivity/base/OcConnectivityType;Z)V");
178 if (!jni_mid_listener)
180 LOGE("could not get Method ID");
184 if (g_jni_cls_enum && g_jni_mid_enum)
186 jobject jni_adaptertype = (*env)->CallStaticObjectMethod(env, g_jni_cls_enum,
187 g_jni_mid_enum, adapter);
189 (*env)->CallVoidMethod(env, g_listenerObject, jni_mid_listener,
190 jni_adaptertype, (jboolean)enabled);
196 (*g_jvm)->DetachCurrentThread(g_jvm);
198 LOGI("OUT - CAManagerAdapterStateChangedCB");
201 JNIEXPORT void JNICALL
202 Java_org_iotivity_ca_CaInterface_caManagerInitialize(JNIEnv *env, jclass clazz,
203 jobject context, jobject listener)
205 LOGI("CaManagere_initialize");
207 CAUtilClientInitialize(env, g_jvm, context);
211 g_listenerObject = (*env)->NewGlobalRef(env, listener);
214 if (g_listenerObject)
216 jclass cls = (*env)->FindClass(env, "org/iotivity/base/OcConnectivityType");
219 g_jni_cls_enum = (jclass)(*env)->NewGlobalRef(env, cls);
224 g_jni_mid_enum = (*env)->GetStaticMethodID(env, g_jni_cls_enum, "getInstance",
225 "(I)Lorg/iotivity/base/OcConnectivityType;");
228 CARegisterNetworkMonitorHandler(CAManagerAdapterStateChangedCB,
229 CAManagerConnectionStateChangedCB);
232 JNIEXPORT void JNICALL
233 Java_org_iotivity_ca_CaInterface_caManagerTerminate(JNIEnv *env, jclass clazz)
235 LOGI("CaManager_terminate");
237 CAUtilClientTerminate(env);
239 if (g_listenerObject)
241 (*env)->DeleteGlobalRef(env, g_listenerObject);
242 g_listenerObject = NULL;
247 (*env)->DeleteGlobalRef(env, g_jni_cls_enum);
248 g_jni_cls_enum = NULL;
253 (*env)->DeleteGlobalRef(env, g_jni_mid_enum);
254 g_jni_mid_enum = NULL;
258 JNIEXPORT void JNICALL
259 Java_org_iotivity_ca_CaInterface_caManagerSetAutoConnectionDeviceInfo(JNIEnv *env,
263 LOGI("CaManager_setAutoConnectionDeviceInfo");
265 const char* address = (*env)->GetStringUTFChars(env, jaddress, NULL);
268 LOGE("address is null");
272 CASetAutoConnectionDeviceInfo(address);
274 (*env)->ReleaseStringUTFChars(env, jaddress, address);
277 JNIEXPORT void JNICALL
278 Java_org_iotivity_ca_CaInterface_caManagerUnsetAutoConnectionDeviceInfo(JNIEnv *env,
282 LOGI("CaManager_unsetAutoConnectionDeviceInfo");
284 const char* address = (*env)->GetStringUTFChars(env, jaddress, NULL);
287 LOGE("address is null");
291 CAUnsetAutoConnectionDeviceInfo(address);
293 (*env)->ReleaseStringUTFChars(env, jaddress, address);
296 JNIEXPORT void JNICALL
297 Java_org_iotivity_ca_CaInterface_caBtPairingInitialize(JNIEnv *env, jclass clazz,
298 jobject context, jobject listener)
300 LOGI("caBtPairingInitialize");
303 CAUtilClientInitialize(env, g_jvm, context);
305 g_foundDeviceListenerObject = (*env)->NewGlobalRef(env, listener);
306 CAUtilSetFoundDeviceListener(g_foundDeviceListenerObject);
309 JNIEXPORT void JNICALL
310 Java_org_iotivity_ca_CaInterface_caBtPairingTerminate(JNIEnv *env, jclass clazz)
312 LOGI("caBtPairingTerminate");
315 if (g_foundDeviceListenerObject)
317 (*env)->DeleteGlobalRef(env, g_foundDeviceListenerObject);
321 JNIEXPORT void JNICALL
322 Java_org_iotivity_ca_CaInterface_caBtPairingStartScan(JNIEnv *env, jclass clazz)
324 LOGI("caBtPairingStartScan");
326 CAUtilStartScan(env);
329 JNIEXPORT void JNICALL
330 Java_org_iotivity_ca_CaInterface_caBtPairingStopScan(JNIEnv *env, jclass clazz)
332 LOGI("caBtPairingStopScan");
337 JNIEXPORT void JNICALL
338 Java_org_iotivity_ca_CaInterface_caBtPairingCreateBond(JNIEnv *env, jclass clazz, jobject device)
340 LOGI("caBtPairingCreateBond");
342 CAUtilCreateBond(env, device);
345 JNIEXPORT void JNICALL
346 Java_org_iotivity_ca_CaInterface_setLeScanIntervalTimeImpl(JNIEnv *env, jclass clazz,
347 jint intervalTime, jint workignCount)
349 LOGI("setLeScanIntervalTimeImpl");
352 CAUtilSetLEScanInterval(intervalTime, workignCount);