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