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(CATransportAdapter_t adapter,
64 const char *remote_address,
67 LOGI("Callback - CAManagerConnectionStateChangedCB : type(%d), address(%s), connected(%d)",
68 adapter, remote_address, connected);
70 if (!g_listenerObject)
72 LOGE("g_listener is NULL, cannot have callback");
76 bool isAttached = false;
78 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
81 LOGI("AttachCurrentThread will be called for JNIEnv pointer");
82 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
86 LOGE("AttachCurrentThread has failed");
92 jclass jni_cls_listener = (*env)->GetObjectClass(env, g_listenerObject);
93 if (!jni_cls_listener)
95 LOGE("could not get jni_cls_listener");
99 jmethodID jni_mid_listener = (*env)->GetMethodID(env, jni_cls_listener,
100 "onConnectionStateChanged",
101 "(Lorg/iotivity/base/OcConnectivityType;"
102 "Ljava/lang/String;Z)V");
103 if (!jni_mid_listener)
105 LOGE("could not get Method ID");
109 jstring jni_address = (*env)->NewStringUTF(env, remote_address);
112 LOGE("jni_address is null");
116 jclass jni_cls_enum = (*env)->FindClass(env, "org/iotivity/base/OcConnectivityType");
119 LOGE("could not get jni_cls_enum");
123 jmethodID jni_mid_enum = (*env)->GetStaticMethodID(env, jni_cls_enum, "getInstance",
124 "(I)Lorg/iotivity/base/OcConnectivityType;");
127 LOGE("could not get Method ID (getInstance)");
131 jobject jni_adaptertype = (*env)->CallStaticObjectMethod(env, jni_cls_enum,
132 jni_mid_enum, adapter);
133 (*env)->CallVoidMethod(env, g_listenerObject, jni_mid_listener,
134 jni_adaptertype, jni_address,
135 (jboolean)connected);
140 (*g_jvm)->DetachCurrentThread(g_jvm);
143 LOGI("OUT - CAManagerConnectionStateChangedCB");
146 void CAManagerAdapterStateChangedCB(CATransportAdapter_t adapter, bool enabled)
148 LOGI("Callback - CAManagerAdapterStateChangedCB : type(%d), enabled(%d)",
151 if (!g_listenerObject)
153 LOGE("g_listener is NULL, cannot have callback");
157 bool isAttached = false;
159 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
162 LOGI("AttachCurrentThread will be called for JNIEnv pointer");
163 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
167 LOGE("AttachCurrentThread has failed");
173 jclass jni_cls_listener = (*env)->GetObjectClass(env, g_listenerObject);
174 if (!jni_cls_listener)
176 LOGE("could not get jni_cls_listener");
180 jmethodID jni_mid_listener = (*env)->GetMethodID(env, jni_cls_listener,
181 "onAdapterStateChanged",
182 "(Lorg/iotivity/base/OcConnectivityType;Z)V");
183 if (!jni_mid_listener)
185 LOGE("could not get Method ID");
189 jclass jni_cls_enum = (*env)->FindClass(env, "org/iotivity/base/OcConnectivityType");
192 LOGE("could not get jni_cls_enum");
196 jmethodID jni_mid_enum = (*env)->GetStaticMethodID(env, jni_cls_enum, "getInstance",
197 "(I)Lorg/iotivity/base/OcConnectivityType;");
200 LOGE("could not get Method ID (getInstance)");
204 jobject jni_adaptertype = (*env)->CallStaticObjectMethod(env, jni_cls_enum,
205 jni_mid_enum, adapter);
207 (*env)->CallVoidMethod(env, g_listenerObject, jni_mid_listener,
208 jni_adaptertype, (jboolean)enabled);
213 (*g_jvm)->DetachCurrentThread(g_jvm);
215 LOGI("OUT - CAManagerAdapterStateChangedCB");
218 JNIEXPORT void JNICALL
219 Java_org_iotivity_ca_CaInterface_caManagerInitialize(JNIEnv *env, jclass clazz,
220 jobject context, jobject listener)
222 LOGI("CaManagere_initialize");
224 CAUtilClientInitialize(env, g_jvm, context);
226 g_listenerObject = (*env)->NewGlobalRef(env, listener);
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;
246 JNIEXPORT void JNICALL
247 Java_org_iotivity_ca_CaInterface_caManagerSetAutoConnectionDeviceInfo(JNIEnv *env,
251 LOGI("CaManager_setAutoConnectionDeviceInfo");
253 const char* address = (*env)->GetStringUTFChars(env, jaddress, NULL);
256 LOGE("address is null");
260 CASetAutoConnectionDeviceInfo(address);
262 (*env)->ReleaseStringUTFChars(env, jaddress, address);
265 JNIEXPORT void JNICALL
266 Java_org_iotivity_ca_CaInterface_caManagerUnsetAutoConnectionDeviceInfo(JNIEnv *env,
270 LOGI("CaManager_unsetAutoConnectionDeviceInfo");
272 const char* address = (*env)->GetStringUTFChars(env, jaddress, NULL);
275 LOGE("address is null");
279 CAUnsetAutoConnectionDeviceInfo(address);
281 (*env)->ReleaseStringUTFChars(env, jaddress, address);
284 JNIEXPORT void JNICALL
285 Java_org_iotivity_ca_CaInterface_caBtPairingInitialize(JNIEnv *env, jclass clazz,
286 jobject context, jobject listener)
288 LOGI("caBtPairingInitialize");
291 CAUtilClientInitialize(env, g_jvm, context);
293 g_foundDeviceListenerObject = (*env)->NewGlobalRef(env, listener);
294 CAUtilSetFoundDeviceListener(g_foundDeviceListenerObject);
297 JNIEXPORT void JNICALL
298 Java_org_iotivity_ca_CaInterface_caBtPairingTerminate(JNIEnv *env, jclass clazz)
300 LOGI("caBtPairingTerminate");
303 if (g_foundDeviceListenerObject)
305 (*env)->DeleteGlobalRef(env, g_foundDeviceListenerObject);
309 JNIEXPORT void JNICALL
310 Java_org_iotivity_ca_CaInterface_caBtPairingStartScan(JNIEnv *env, jclass clazz)
312 LOGI("caBtPairingStartScan");
314 CAUtilStartScan(env);
317 JNIEXPORT void JNICALL
318 Java_org_iotivity_ca_CaInterface_caBtPairingStopScan(JNIEnv *env, jclass clazz)
320 LOGI("caBtPairingStopScan");
325 JNIEXPORT void JNICALL
326 Java_org_iotivity_ca_CaInterface_caBtPairingCreateBond(JNIEnv *env, jclass clazz, jobject device)
328 LOGI("caBtPairingCreateBond");
330 CAUtilCreateBond(env, device);
333 JNIEXPORT void JNICALL
334 Java_org_iotivity_ca_CaInterface_setLeScanIntervalTimeImpl(JNIEnv *env, jclass clazz,
335 jint intervalTime, jint workignCount)
337 LOGI("setLeScanIntervalTimeImpl");
340 CAUtilSetLEScanInterval(intervalTime, workignCount);