Removed SECURED define dependency from JNI.
[platform/upstream/iotivity.git] / android / android_api / base / jni / JniCaInterface.c
1 /*
2 * //******************************************************************
3 * //
4 * // Copyright 2015 Intel Corporation.
5 * //
6 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 * //
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
11 * //
12 * //      http://www.apache.org/licenses/LICENSE-2.0
13 * //
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.
19 * //
20 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 */
22
23 #include <jni.h>
24 #include <android/log.h>
25 #include <stdio.h>
26 #include "cainterface.h"
27 #include "JniCaInterface.h"
28 #include "cautilinterface.h"
29 #include "cacommon.h"
30
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__)
34
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;
40
41 JNIEXPORT jint JNI_OnLoad(JavaVM *jvm, void *reserved)
42 {
43     LOGI("CaInterface_initialize");
44     g_jvm = jvm;
45     CANativeJNISetJavaVM(jvm);
46
47     return JNI_VERSION_1_6;
48 }
49
50 void JNI_OnUnload(JavaVM *jvm, void *reserved)
51 {
52     return;
53 }
54
55 JNIEXPORT void JNICALL
56 Java_org_iotivity_ca_CaInterface_initialize
57 (JNIEnv *env, jclass clazz, jobject activity, jobject context)
58 {
59     LOGI("CaInterface_initialize");
60
61     CANativeSetActivity(env, activity);
62     CANativeJNISetContext(env, context);
63 }
64
65 void CAManagerConnectionStateChangedCB(const CAEndpoint_t *info,
66                                        bool connected)
67 {
68     if (!info)
69     {
70         LOGE("info is NULL");
71         return;
72     }
73
74     if (!g_listenerObject)
75     {
76         LOGE("g_listener is NULL, cannot have callback");
77         return;
78     }
79
80     LOGI("Callback - CAManagerConnectionStateChangedCB : type(%d), address(%s), connected(%d)",
81          info->adapter, info->addr, connected);
82
83     bool isAttached = false;
84     JNIEnv* env = NULL;
85     jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
86     if (JNI_OK != res)
87     {
88         LOGI("AttachCurrentThread will be called for JNIEnv pointer");
89         res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
90
91         if (JNI_OK != res)
92         {
93             LOGE("AttachCurrentThread has failed");
94             return;
95         }
96         isAttached = true;
97     }
98
99     jclass jni_cls_listener = (*env)->GetObjectClass(env, g_listenerObject);
100     if (!jni_cls_listener)
101     {
102         LOGE("could not get jni_cls_listener");
103         goto exit_error;
104     }
105
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)
111     {
112         LOGE("could not get Method ID");
113         goto exit_error;
114     }
115
116     jstring jni_address = (*env)->NewStringUTF(env, info->addr);
117     if (!jni_address)
118     {
119         LOGE("jni_address is null");
120         goto exit_error;
121     }
122
123     if (g_jni_cls_enum && g_jni_mid_enum)
124     {
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);
130     }
131
132 exit_error:
133     if (isAttached)
134     {
135         (*g_jvm)->DetachCurrentThread(g_jvm);
136     }
137
138     LOGI("OUT - CAManagerConnectionStateChangedCB");
139 }
140
141 void CAManagerAdapterStateChangedCB(CATransportAdapter_t adapter, bool enabled)
142 {
143     LOGI("Callback - CAManagerAdapterStateChangedCB : type(%d), enabled(%d)",
144          adapter, enabled);
145
146     if (!g_listenerObject)
147     {
148         LOGE("g_listener is NULL, cannot have callback");
149         return;
150     }
151
152     bool isAttached = false;
153     JNIEnv* env = NULL;
154     jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
155     if (JNI_OK != res)
156     {
157         LOGI("AttachCurrentThread will be called for JNIEnv pointer");
158         res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
159
160         if (JNI_OK != res)
161         {
162             LOGE("AttachCurrentThread has failed");
163             return;
164         }
165         isAttached = true;
166     }
167
168     jclass jni_cls_listener = (*env)->GetObjectClass(env, g_listenerObject);
169     if (!jni_cls_listener)
170     {
171         LOGE("could not get jni_cls_listener");
172         goto exit_error;
173     }
174
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)
179     {
180         LOGE("could not get Method ID");
181         goto exit_error;
182     }
183
184     if (g_jni_cls_enum && g_jni_mid_enum)
185     {
186         jobject jni_adaptertype = (*env)->CallStaticObjectMethod(env, g_jni_cls_enum,
187                                                                  g_jni_mid_enum, adapter);
188
189         (*env)->CallVoidMethod(env, g_listenerObject, jni_mid_listener,
190                                jni_adaptertype, (jboolean)enabled);
191     }
192
193 exit_error:
194     if (isAttached)
195     {
196         (*g_jvm)->DetachCurrentThread(g_jvm);
197     }
198     LOGI("OUT -  CAManagerAdapterStateChangedCB");
199 }
200
201 JNIEXPORT void JNICALL
202 Java_org_iotivity_ca_CaInterface_caManagerInitialize(JNIEnv *env, jclass clazz,
203                                                      jobject context, jobject listener)
204 {
205     LOGI("CaManagere_initialize");
206
207     CAUtilClientInitialize(env, g_jvm, context);
208
209     if (listener)
210     {
211         g_listenerObject = (*env)->NewGlobalRef(env, listener);
212     }
213
214     if (g_listenerObject)
215     {
216         jclass cls = (*env)->FindClass(env, "org/iotivity/base/OcConnectivityType");
217         if (cls)
218         {
219             g_jni_cls_enum = (jclass)(*env)->NewGlobalRef(env, cls);
220         }
221
222         if (g_jni_cls_enum)
223         {
224             g_jni_mid_enum = (*env)->GetStaticMethodID(env, g_jni_cls_enum, "getInstance",
225                                                    "(I)Lorg/iotivity/base/OcConnectivityType;");
226         }
227     }
228     CARegisterNetworkMonitorHandler(CAManagerAdapterStateChangedCB,
229                                     CAManagerConnectionStateChangedCB);
230 }
231
232 JNIEXPORT void JNICALL
233 Java_org_iotivity_ca_CaInterface_caManagerTerminate(JNIEnv *env, jclass clazz)
234 {
235     LOGI("CaManager_terminate");
236
237     CAUtilClientTerminate(env);
238
239     if (g_listenerObject)
240     {
241         (*env)->DeleteGlobalRef(env, g_listenerObject);
242         g_listenerObject = NULL;
243     }
244
245     if (g_jni_cls_enum)
246     {
247         (*env)->DeleteGlobalRef(env, g_jni_cls_enum);
248         g_jni_cls_enum = NULL;
249     }
250 }
251
252 JNIEXPORT void JNICALL
253 Java_org_iotivity_ca_CaInterface_caManagerSetAutoConnectionDeviceInfo(JNIEnv *env,
254                                                                       jclass clazz,
255                                                                       jstring jaddress)
256 {
257     LOGI("CaManager_setAutoConnectionDeviceInfo");
258     if (!jaddress)
259     {
260         LOGE("jaddress is null");
261         return;
262     }
263
264     const char* address = (*env)->GetStringUTFChars(env, jaddress, NULL);
265     if (!address)
266     {
267         LOGE("address is null");
268         return;
269     }
270
271     CASetAutoConnectionDeviceInfo(address);
272
273     (*env)->ReleaseStringUTFChars(env, jaddress, address);
274 }
275
276 JNIEXPORT void JNICALL
277 Java_org_iotivity_ca_CaInterface_caManagerUnsetAutoConnectionDeviceInfo(JNIEnv *env,
278                                                                         jclass clazz,
279                                                                         jstring jaddress)
280 {
281     LOGI("CaManager_unsetAutoConnectionDeviceInfo");
282     if (!jaddress)
283     {
284         LOGE("jaddress is null");
285         return;
286     }
287
288     const char* address = (*env)->GetStringUTFChars(env, jaddress, NULL);
289     if (!address)
290     {
291         LOGE("address is null");
292         return;
293     }
294
295     CAUnsetAutoConnectionDeviceInfo(address);
296
297     (*env)->ReleaseStringUTFChars(env, jaddress, address);
298 }
299
300 JNIEXPORT void JNICALL
301 Java_org_iotivity_ca_CaInterface_caBtPairingInitialize(JNIEnv *env, jclass clazz,
302                                                        jobject context, jobject listener)
303 {
304     LOGI("caBtPairingInitialize");
305     (void)clazz;
306
307     CAUtilClientInitialize(env, g_jvm, context);
308
309     g_foundDeviceListenerObject = (*env)->NewGlobalRef(env, listener);
310     CAUtilSetFoundDeviceListener(g_foundDeviceListenerObject);
311 }
312
313 JNIEXPORT void JNICALL
314 Java_org_iotivity_ca_CaInterface_caBtPairingTerminate(JNIEnv *env, jclass clazz)
315 {
316     LOGI("caBtPairingTerminate");
317     (void)clazz;
318
319     if (g_foundDeviceListenerObject)
320     {
321         (*env)->DeleteGlobalRef(env, g_foundDeviceListenerObject);
322     }
323 }
324
325 JNIEXPORT void JNICALL
326 Java_org_iotivity_ca_CaInterface_caBtPairingStartScan(JNIEnv *env, jclass clazz)
327 {
328     LOGI("caBtPairingStartScan");
329     (void)clazz;
330     CAUtilStartScan(env);
331 }
332
333 JNIEXPORT void JNICALL
334 Java_org_iotivity_ca_CaInterface_caBtPairingStopScan(JNIEnv *env, jclass clazz)
335 {
336     LOGI("caBtPairingStopScan");
337     (void)clazz;
338     CAUtilStopScan(env);
339 }
340
341 JNIEXPORT void JNICALL
342 Java_org_iotivity_ca_CaInterface_caBtPairingCreateBond(JNIEnv *env, jclass clazz, jobject device)
343 {
344     LOGI("caBtPairingCreateBond");
345     (void)clazz;
346     CAUtilCreateBond(env, device);
347 }
348
349 JNIEXPORT void JNICALL
350 Java_org_iotivity_ca_CaInterface_setLeScanIntervalTimeImpl(JNIEnv *env, jclass clazz,
351                                                            jint intervalTime, jint workignCount)
352 {
353     LOGI("setLeScanIntervalTimeImpl");
354     (void)env;
355     (void)clazz;
356     CAUtilSetLEScanInterval(intervalTime, workignCount);
357 }
358
359 JNIEXPORT jint JNICALL Java_org_iotivity_ca_CaInterface_setCipherSuiteImpl
360   (JNIEnv *env, jclass clazz, jint cipherSuite, jint adapter)
361 {
362     LOGI("setCipherSuiteImpl");
363     (void)env;
364     (void)clazz;
365     CAResult_t ret = CASelectCipherSuite(cipherSuite, (CATransportAdapter_t) adapter);
366     if (CA_STATUS_OK != ret)
367     {
368         LOGE("CASelectCipherSuite has failed");
369     }
370     return ret;
371 }
372