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;
39 JNIEXPORT jint JNI_OnLoad(JavaVM *jvm, void *reserved)
41 LOGI("CaInterface_initialize");
43 CANativeJNISetJavaVM(jvm);
45 return JNI_VERSION_1_6;
48 void JNI_OnUnload(JavaVM *jvm, void *reserved)
53 JNIEXPORT void JNICALL
54 Java_org_iotivity_ca_CaInterface_initialize
55 (JNIEnv *env, jclass clazz, jobject activity, jobject context)
57 LOGI("CaInterface_initialize");
59 CANativeSetActivity(env, activity);
60 CANativeJNISetContext(env, context);
63 void CAManagerConnectionStateChangedCB(const CAEndpoint_t *info,
72 if (!g_listenerObject)
74 LOGE("g_listener is NULL, cannot have callback");
78 LOGI("Callback - CAManagerConnectionStateChangedCB : type(%d), address(%s), connected(%d)",
79 info->adapter, info->addr, connected);
81 bool isAttached = false;
83 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
86 LOGI("AttachCurrentThread will be called for JNIEnv pointer");
87 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
91 LOGE("AttachCurrentThread has failed");
97 jclass jni_cls_listener = (*env)->GetObjectClass(env, g_listenerObject);
98 if (!jni_cls_listener)
100 LOGE("could not get jni_cls_listener");
104 jmethodID jni_mid_listener = (*env)->GetMethodID(env, jni_cls_listener,
105 "onConnectionStateChanged",
106 "(Lorg/iotivity/base/OcConnectivityType;"
107 "Ljava/lang/String;Z)V");
108 if (!jni_mid_listener)
110 LOGE("could not get Method ID");
114 jstring jni_address = (*env)->NewStringUTF(env, info->addr);
117 LOGE("jni_address is null");
121 jclass jni_cls_enum = (*env)->FindClass(env, "org/iotivity/base/OcConnectivityType");
124 LOGE("could not get jni_cls_enum");
128 jmethodID jni_mid_enum = (*env)->GetStaticMethodID(env, jni_cls_enum, "getInstance",
129 "(I)Lorg/iotivity/base/OcConnectivityType;");
132 LOGE("could not get Method ID (getInstance)");
136 jobject jni_adaptertype = (*env)->CallStaticObjectMethod(env, jni_cls_enum,
137 jni_mid_enum, info->adapter);
138 (*env)->CallVoidMethod(env, g_listenerObject, jni_mid_listener,
139 jni_adaptertype, jni_address,
140 (jboolean)connected);
145 (*g_jvm)->DetachCurrentThread(g_jvm);
148 LOGI("OUT - CAManagerConnectionStateChangedCB");
151 void CAManagerAdapterStateChangedCB(CATransportAdapter_t adapter, bool enabled)
153 LOGI("Callback - CAManagerAdapterStateChangedCB : type(%d), enabled(%d)",
156 if (!g_listenerObject)
158 LOGE("g_listener is NULL, cannot have callback");
162 bool isAttached = false;
164 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
167 LOGI("AttachCurrentThread will be called for JNIEnv pointer");
168 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
172 LOGE("AttachCurrentThread has failed");
178 jclass jni_cls_listener = (*env)->GetObjectClass(env, g_listenerObject);
179 if (!jni_cls_listener)
181 LOGE("could not get jni_cls_listener");
185 jmethodID jni_mid_listener = (*env)->GetMethodID(env, jni_cls_listener,
186 "onAdapterStateChanged",
187 "(Lorg/iotivity/base/OcConnectivityType;Z)V");
188 if (!jni_mid_listener)
190 LOGE("could not get Method ID");
194 jclass jni_cls_enum = (*env)->FindClass(env, "org/iotivity/base/OcConnectivityType");
197 LOGE("could not get jni_cls_enum");
201 jmethodID jni_mid_enum = (*env)->GetStaticMethodID(env, jni_cls_enum, "getInstance",
202 "(I)Lorg/iotivity/base/OcConnectivityType;");
205 LOGE("could not get Method ID (getInstance)");
209 jobject jni_adaptertype = (*env)->CallStaticObjectMethod(env, jni_cls_enum,
210 jni_mid_enum, adapter);
212 (*env)->CallVoidMethod(env, g_listenerObject, jni_mid_listener,
213 jni_adaptertype, (jboolean)enabled);
218 (*g_jvm)->DetachCurrentThread(g_jvm);
220 LOGI("OUT - CAManagerAdapterStateChangedCB");
223 JNIEXPORT void JNICALL
224 Java_org_iotivity_ca_CaInterface_caManagerInitialize(JNIEnv *env, jclass clazz,
225 jobject context, jobject listener)
227 LOGI("CaManagere_initialize");
229 CAUtilClientInitialize(env, g_jvm, context);
231 g_listenerObject = (*env)->NewGlobalRef(env, listener);
233 CARegisterNetworkMonitorHandler(CAManagerAdapterStateChangedCB,
234 CAManagerConnectionStateChangedCB);
237 JNIEXPORT void JNICALL
238 Java_org_iotivity_ca_CaInterface_caManagerTerminate(JNIEnv *env, jclass clazz)
240 LOGI("CaManager_terminate");
242 CAUtilClientTerminate(env);
244 if (g_listenerObject)
246 (*env)->DeleteGlobalRef(env, g_listenerObject);
247 g_listenerObject = NULL;
251 JNIEXPORT void JNICALL
252 Java_org_iotivity_ca_CaInterface_caManagerSetAutoConnectionDeviceInfo(JNIEnv *env,
256 LOGI("CaManager_setAutoConnectionDeviceInfo");
258 const char* address = (*env)->GetStringUTFChars(env, jaddress, NULL);
261 LOGE("address is null");
265 CASetAutoConnectionDeviceInfo(address);
267 (*env)->ReleaseStringUTFChars(env, jaddress, address);
270 JNIEXPORT void JNICALL
271 Java_org_iotivity_ca_CaInterface_caManagerUnsetAutoConnectionDeviceInfo(JNIEnv *env,
275 LOGI("CaManager_unsetAutoConnectionDeviceInfo");
277 const char* address = (*env)->GetStringUTFChars(env, jaddress, NULL);
280 LOGE("address is null");
284 CAUnsetAutoConnectionDeviceInfo(address);
286 (*env)->ReleaseStringUTFChars(env, jaddress, address);
289 JNIEXPORT void JNICALL
290 Java_org_iotivity_ca_CaInterface_caBtPairingInitialize(JNIEnv *env, jclass clazz,
291 jobject context, jobject listener)
293 LOGI("caBtPairingInitialize");
296 CAUtilClientInitialize(env, g_jvm, context);
298 g_foundDeviceListenerObject = (*env)->NewGlobalRef(env, listener);
299 CAUtilSetFoundDeviceListener(g_foundDeviceListenerObject);
302 JNIEXPORT void JNICALL
303 Java_org_iotivity_ca_CaInterface_caBtPairingTerminate(JNIEnv *env, jclass clazz)
305 LOGI("caBtPairingTerminate");
308 if (g_foundDeviceListenerObject)
310 (*env)->DeleteGlobalRef(env, g_foundDeviceListenerObject);
314 JNIEXPORT void JNICALL
315 Java_org_iotivity_ca_CaInterface_caBtPairingStartScan(JNIEnv *env, jclass clazz)
317 LOGI("caBtPairingStartScan");
319 CAUtilStartScan(env);
322 JNIEXPORT void JNICALL
323 Java_org_iotivity_ca_CaInterface_caBtPairingStopScan(JNIEnv *env, jclass clazz)
325 LOGI("caBtPairingStopScan");
330 JNIEXPORT void JNICALL
331 Java_org_iotivity_ca_CaInterface_caBtPairingCreateBond(JNIEnv *env, jclass clazz, jobject device)
333 LOGI("caBtPairingCreateBond");
335 CAUtilCreateBond(env, device);
338 JNIEXPORT void JNICALL
339 Java_org_iotivity_ca_CaInterface_setLeScanIntervalTimeImpl(JNIEnv *env, jclass clazz,
340 jint intervalTime, jint workignCount)
342 LOGI("setLeScanIntervalTimeImpl");
345 CAUtilSetLEScanInterval(intervalTime, workignCount);