Fixed unused-param warning in JNI code
[iotivity.git] / java / jni / JniCaInterface.c
1 //******************************************************************
2 //
3 // Copyright 2015 Intel Corporation.
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 #include <jni.h>
21 #include "experimental/logger.h"
22 #include <stdio.h>
23 #include "caadapterutils.h"
24 #include "cainterface.h"
25 #include "JniCaInterface.h"
26 #include "cautilinterface.h"
27 #include "cacommon.h"
28
29 #define  LOG_TAG   "JNI_CA_INTERFACE"
30 #define  LOGI(...) OIC_LOG_V(OC_LOG_INFO, LOG_TAG, __VA_ARGS__)
31 #define  LOGE(...) OIC_LOG_V(OC_LOG_ERROR, LOG_TAG, __VA_ARGS__)
32
33 static jobject g_foundDeviceListenerObject = NULL;
34 static jobject g_listenerObject = NULL;
35 static JavaVM *g_jvm = NULL;
36 static jclass g_jni_cls_enum = NULL;
37 static jmethodID g_jni_mid_enum = NULL;
38
39 JNIEXPORT jint JNICALL
40 JNI_OnLoad(JavaVM *jvm, void *reserved)
41 {
42     OC_UNUSED(reserved);
43     LOGI("CaInterface_initialize");
44     g_jvm = jvm;
45     CANativeJNISetJavaVM(jvm);
46
47     return JNI_VERSION_1_6;
48 }
49
50 JNIEXPORT void JNICALL
51 JNI_OnUnload(JavaVM *jvm, void *reserved)
52 {
53     OC_UNUSED(jvm);
54     OC_UNUSED(reserved);
55     return;
56 }
57
58 #ifdef __ANDROID__
59 JNIEXPORT void JNICALL
60 Java_org_iotivity_ca_CaInterface_initialize
61 (JNIEnv *env, jclass clazz, jobject activity, jobject context)
62 {
63     OC_UNUSED(clazz);
64     LOGI("CaInterface_initialize");
65
66     CANativeSetActivity(env, activity);
67     CANativeJNISetContext(env, context);
68 }
69 #else
70 JNIEXPORT void JNICALL
71 Java_org_iotivity_ca_CaInterface_initialize(JNIEnv *env, jclass clazz)
72 {
73     OC_UNUSED(env);
74     OC_UNUSED(clazz);
75     LOGI("CaInterface_initialize");
76 }
77 #endif
78
79 void CAManagerConnectionStateChangedCB(const CAEndpoint_t *info,
80                                        bool connected)
81 {
82     if (!info)
83     {
84         LOGE("info is NULL");
85         return;
86     }
87
88     if (!g_listenerObject)
89     {
90         LOGE("g_listener is NULL, cannot have callback");
91         return;
92     }
93
94     LOGI("Callback - CAManagerConnectionStateChangedCB : type(%d), address(%s), connected(%d)",
95          info->adapter, info->addr, connected);
96
97     bool isAttached = false;
98     JNIEnv* env = NULL;
99     jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
100     if (JNI_OK != res)
101     {
102         LOGI("AttachCurrentThread will be called for JNIEnv pointer");
103 #ifdef __ANDROID__
104         res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
105 #else
106         res = (*g_jvm)->AttachCurrentThread(g_jvm, (void**) &env, NULL);
107 #endif
108
109         if (JNI_OK != res)
110         {
111             LOGE("AttachCurrentThread has failed");
112             return;
113         }
114         isAttached = true;
115     }
116
117     jclass jni_cls_listener = (*env)->GetObjectClass(env, g_listenerObject);
118     if (!jni_cls_listener)
119     {
120         LOGE("could not get jni_cls_listener");
121         goto exit_error;
122     }
123
124     jmethodID jni_mid_listener = (*env)->GetMethodID(env, jni_cls_listener,
125                                                      "onConnectionStateChanged",
126                                                      "(Lorg/iotivity/base/OcConnectivityType;"
127                                                      "Ljava/lang/String;Z)V");
128     if (!jni_mid_listener)
129     {
130         LOGE("could not get Method ID");
131         goto exit_error;
132     }
133
134     jstring jni_address = (*env)->NewStringUTF(env, info->addr);
135     if (!jni_address)
136     {
137         LOGE("jni_address is null");
138         goto exit_error;
139     }
140
141     if (g_jni_cls_enum && g_jni_mid_enum)
142     {
143         jobject jni_adaptertype = (*env)->CallStaticObjectMethod(env, g_jni_cls_enum,
144                                                                  g_jni_mid_enum, info->adapter);
145         (*env)->CallVoidMethod(env, g_listenerObject, jni_mid_listener,
146                                jni_adaptertype, jni_address,
147                                (jboolean)connected);
148     }
149
150 exit_error:
151     if (isAttached)
152     {
153         (*g_jvm)->DetachCurrentThread(g_jvm);
154     }
155
156     LOGI("OUT - CAManagerConnectionStateChangedCB");
157 }
158
159 void CAManagerAdapterStateChangedCB(CATransportAdapter_t adapter, bool enabled)
160 {
161     LOGI("Callback - CAManagerAdapterStateChangedCB : type(%d), enabled(%d)",
162          adapter, enabled);
163
164     if (!g_listenerObject)
165     {
166         LOGE("g_listener is NULL, cannot have callback");
167         return;
168     }
169
170     bool isAttached = false;
171     JNIEnv* env = NULL;
172     jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
173     if (JNI_OK != res)
174     {
175         LOGI("AttachCurrentThread will be called for JNIEnv pointer");
176 #ifdef __ANDROID__
177         res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
178 #else
179         res = (*g_jvm)->AttachCurrentThread(g_jvm, (void**) &env, NULL);
180 #endif
181
182         if (JNI_OK != res)
183         {
184             LOGE("AttachCurrentThread has failed");
185             return;
186         }
187         isAttached = true;
188     }
189
190     jclass jni_cls_listener = (*env)->GetObjectClass(env, g_listenerObject);
191     if (!jni_cls_listener)
192     {
193         LOGE("could not get jni_cls_listener");
194         goto exit_error;
195     }
196
197     jmethodID jni_mid_listener = (*env)->GetMethodID(env, jni_cls_listener,
198                                                      "onAdapterStateChanged",
199                                                      "(Lorg/iotivity/base/OcConnectivityType;Z)V");
200     if (!jni_mid_listener)
201     {
202         LOGE("could not get Method ID");
203         goto exit_error;
204     }
205
206     if (g_jni_cls_enum && g_jni_mid_enum)
207     {
208         jobject jni_adaptertype = (*env)->CallStaticObjectMethod(env, g_jni_cls_enum,
209                                                                  g_jni_mid_enum, adapter);
210
211         (*env)->CallVoidMethod(env, g_listenerObject, jni_mid_listener,
212                                jni_adaptertype, (jboolean)enabled);
213     }
214
215 exit_error:
216     if (isAttached)
217     {
218         (*g_jvm)->DetachCurrentThread(g_jvm);
219     }
220     LOGI("OUT -  CAManagerAdapterStateChangedCB");
221 }
222
223 #ifdef __ANDROID__
224 JNIEXPORT void JNICALL
225 Java_org_iotivity_ca_CaInterface_caManagerInitialize(JNIEnv *env, jclass clazz,
226                                                      jobject context, jobject listener)
227 {
228     OC_UNUSED(clazz);
229     LOGI("CaManager_initialize");
230
231     CAUtilClientInitialize(env, g_jvm, context);
232
233     if (listener)
234     {
235         g_listenerObject = (*env)->NewGlobalRef(env, listener);
236     }
237
238     if (g_listenerObject)
239     {
240         jclass cls = (*env)->FindClass(env, "org/iotivity/base/OcConnectivityType");
241         if (cls)
242         {
243             g_jni_cls_enum = (jclass)(*env)->NewGlobalRef(env, cls);
244         }
245
246         if (g_jni_cls_enum)
247         {
248             g_jni_mid_enum = (*env)->GetStaticMethodID(env, g_jni_cls_enum, "getInstance",
249                                                    "(I)Lorg/iotivity/base/OcConnectivityType;");
250         }
251     }
252     CAResult_t res = CARegisterNetworkMonitorHandler(CAManagerAdapterStateChangedCB,
253                                                      CAManagerConnectionStateChangedCB);
254     if (CA_STATUS_OK != res)
255     {
256         LOGE("CARegisterNetworkMonitorHandler has failed");
257     }
258 }
259 #else
260 JNIEXPORT void JNICALL
261 Java_org_iotivity_ca_CaInterface_caManagerInitialize(JNIEnv *env, jclass clazz,
262                                                      jobject listener)
263 {
264     OC_UNUSED(clazz);
265     LOGI("CaManagere_initialize");
266
267     g_listenerObject = (*env)->NewGlobalRef(env, listener);
268
269     CARegisterNetworkMonitorHandler(CAManagerAdapterStateChangedCB,
270                                     CAManagerConnectionStateChangedCB);
271 }
272 #endif
273
274 JNIEXPORT void JNICALL
275 Java_org_iotivity_ca_CaInterface_caManagerTerminate(JNIEnv *env, jclass clazz)
276 {
277     OC_UNUSED(clazz);
278     LOGI("CaManager_terminate");
279
280     CAUtilClientTerminate(env);
281
282     if (g_listenerObject)
283     {
284         (*env)->DeleteGlobalRef(env, g_listenerObject);
285         g_listenerObject = NULL;
286     }
287
288     if (g_jni_cls_enum)
289     {
290         (*env)->DeleteGlobalRef(env, g_jni_cls_enum);
291         g_jni_cls_enum = NULL;
292     }
293
294     CAResult_t res = CAUnregisterNetworkMonitorHandler(CAManagerAdapterStateChangedCB,
295                                                        CAManagerConnectionStateChangedCB);
296     if (CA_STATUS_OK != res)
297     {
298         LOGE("CAUnregisterNetworkMonitorHandler has failed");
299     }
300 }
301
302 JNIEXPORT void JNICALL
303 Java_org_iotivity_ca_CaInterface_caManagerSetAutoConnectionDeviceInfo(JNIEnv *env,
304                                                                       jclass clazz,
305                                                                       jstring jaddress)
306 {
307     OC_UNUSED(clazz);
308     LOGI("CaManager_setAutoConnectionDeviceInfo");
309     if (!jaddress)
310     {
311         LOGE("jaddress is null");
312         return;
313     }
314
315     const char* address = (*env)->GetStringUTFChars(env, jaddress, NULL);
316     if (!address)
317     {
318         LOGE("address is null");
319         return;
320     }
321
322     CASetAutoConnectionDeviceInfo(address);
323
324     (*env)->ReleaseStringUTFChars(env, jaddress, address);
325 }
326
327 JNIEXPORT void JNICALL
328 Java_org_iotivity_ca_CaInterface_caManagerUnsetAutoConnectionDeviceInfo(JNIEnv *env,
329                                                                         jclass clazz,
330                                                                         jstring jaddress)
331 {
332     OC_UNUSED(clazz);
333     LOGI("CaManager_unsetAutoConnectionDeviceInfo");
334     if (!jaddress)
335     {
336         LOGE("jaddress is null");
337         return;
338     }
339
340     const char* address = (*env)->GetStringUTFChars(env, jaddress, NULL);
341     if (!address)
342     {
343         LOGE("address is null");
344         return;
345     }
346
347     CAUnsetAutoConnectionDeviceInfo(address);
348
349     (*env)->ReleaseStringUTFChars(env, jaddress, address);
350 }
351
352 #ifdef __ANDROID__
353 JNIEXPORT void JNICALL
354 Java_org_iotivity_ca_CaInterface_caBtPairingInitialize(JNIEnv *env, jclass clazz,
355                                                        jobject context, jobject listener)
356 {
357     LOGI("caBtPairingInitialize");
358     (void)clazz;
359
360     CAUtilClientInitialize(env, g_jvm, context);
361
362     g_foundDeviceListenerObject = (*env)->NewGlobalRef(env, listener);
363     CAUtilSetFoundDeviceListener(g_foundDeviceListenerObject);
364 }
365 #else
366 JNIEXPORT void JNICALL
367 Java_org_iotivity_ca_CaInterface_caBtPairingInitialize(JNIEnv *env, jclass clazz,
368                                                        jobject listener)
369 {
370     LOGI("caBtPairingInitialize");
371     (void)clazz;
372
373     CAUtilClientInitialize(env, g_jvm);
374
375     g_foundDeviceListenerObject = (*env)->NewGlobalRef(env, listener);
376     CAUtilSetFoundDeviceListener(g_foundDeviceListenerObject);
377 }
378 #endif
379
380 JNIEXPORT void JNICALL
381 Java_org_iotivity_ca_CaInterface_caBtPairingTerminate(JNIEnv *env, jclass clazz)
382 {
383     LOGI("caBtPairingTerminate");
384     (void)clazz;
385
386     if (g_foundDeviceListenerObject)
387     {
388         (*env)->DeleteGlobalRef(env, g_foundDeviceListenerObject);
389     }
390 }
391
392 JNIEXPORT void JNICALL
393 Java_org_iotivity_ca_CaInterface_caBtPairingStartScan(JNIEnv *env, jclass clazz)
394 {
395     LOGI("caBtPairingStartScan");
396     (void)clazz;
397     CAUtilStartScan(env);
398 }
399
400 JNIEXPORT void JNICALL
401 Java_org_iotivity_ca_CaInterface_caBtPairingStopScan(JNIEnv *env, jclass clazz)
402 {
403     LOGI("caBtPairingStopScan");
404     (void)clazz;
405     CAUtilStopScan(env);
406 }
407
408 JNIEXPORT void JNICALL
409 Java_org_iotivity_ca_CaInterface_caBtPairingCreateBond(JNIEnv *env, jclass clazz, jobject device)
410 {
411     LOGI("caBtPairingCreateBond");
412     (void)clazz;
413     CAUtilCreateBond(env, device);
414 }
415
416 JNIEXPORT void JNICALL
417 Java_org_iotivity_ca_CaInterface_setLeScanIntervalTimeImpl(JNIEnv *env, jclass clazz,
418                                                            jint intervalTime, jint workignCount)
419 {
420     LOGI("setLeScanIntervalTimeImpl");
421     (void)env;
422     (void)clazz;
423     CAUtilSetLEScanInterval(intervalTime, workignCount);
424 }
425
426 JNIEXPORT void JNICALL
427 Java_org_iotivity_ca_CaInterface_stopLeScanImpl(JNIEnv *env, jclass clazz)
428 {
429     LOGI("stopLeScan");
430     (void)env;
431     (void)clazz;
432     CAUtilStopLEScan();
433 }
434
435 JNIEXPORT void JNICALL
436 Java_org_iotivity_ca_CaInterface_startLeAdvertisingImpl(JNIEnv *env, jclass clazz)
437 {
438     LOGI("startLeAdvertising");
439     (void)env;
440     (void)clazz;
441     CAUtilStartLEAdvertising();
442 }
443
444 JNIEXPORT void JNICALL
445 Java_org_iotivity_ca_CaInterface_stopLeAdvertisingImpl(JNIEnv *env, jclass clazz)
446 {
447     LOGI("stopLeAdvertising");
448     (void)env;
449     (void)clazz;
450     CAUtilStopLEAdvertising();
451 }
452
453 JNIEXPORT void JNICALL
454 Java_org_iotivity_ca_CaInterface_setBTConfigureImpl(JNIEnv *env, jclass clazz, jint flag)
455 {
456     LOGI("setConfigureImpl");
457     (void)env;
458     (void)clazz;
459     CAUtilConfig_t configs = {(CATransportBTFlags_t)flag, CA_USER_PREF_CLOUD};
460     CAUtilSetBTConfigure(configs);
461 }
462
463 JNIEXPORT jint JNICALL Java_org_iotivity_ca_CaInterface_setCipherSuiteImpl
464   (JNIEnv *env, jclass clazz, jint cipherSuite, jint adapter)
465 {
466     LOGI("setCipherSuiteImpl");
467     (void)env;
468     (void)clazz;
469     CAResult_t ret = CASelectCipherSuite((uint16_t)cipherSuite, (CATransportAdapter_t) adapter);
470     if (CA_STATUS_OK != ret)
471     {
472         LOGE("CASelectCipherSuite has failed");
473     }
474     return ret;
475 }
476
477 JNIEXPORT void JNICALL
478 Java_org_iotivity_ca_CaInterface_caManagerSetConnectionUserConfig(JNIEnv *env, jclass clazz,
479                                                                   jint connPriority)
480 {
481     (void)env;
482     (void)clazz;
483 #if defined(TCP_ADAPTER) && defined(WITH_CLOUD)
484     LOGI("CaManager_SetConnecitonUserConfig connPriority: %d", connPriority);
485     CAResult_t ret = CAUtilCMSetConnectionUserConfig(connPriority);
486     if (CA_STATUS_OK != ret)
487     {
488         LOGE("CASetIpConnSwitchConfig has failed");
489     }
490 #else
491     LOGI("[CaManager_SetConnecitonUserConfig] stack doesn't support TCP and CLOUD");
492     (void)connPriority;
493 #endif
494 }
495