Merge branch 'master' into notification-service
[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     if (g_jni_mid_enum)
252     {
253         (*env)->DeleteGlobalRef(env, g_jni_mid_enum);
254         g_jni_mid_enum = NULL;
255     }
256 }
257
258 JNIEXPORT void JNICALL
259 Java_org_iotivity_ca_CaInterface_caManagerSetAutoConnectionDeviceInfo(JNIEnv *env,
260                                                                       jclass clazz,
261                                                                       jstring jaddress)
262 {
263     LOGI("CaManager_setAutoConnectionDeviceInfo");
264
265     const char* address = (*env)->GetStringUTFChars(env, jaddress, NULL);
266     if (!address)
267     {
268         LOGE("address is null");
269         return;
270     }
271
272     CASetAutoConnectionDeviceInfo(address);
273
274     (*env)->ReleaseStringUTFChars(env, jaddress, address);
275 }
276
277 JNIEXPORT void JNICALL
278 Java_org_iotivity_ca_CaInterface_caManagerUnsetAutoConnectionDeviceInfo(JNIEnv *env,
279                                                                         jclass clazz,
280                                                                         jstring jaddress)
281 {
282     LOGI("CaManager_unsetAutoConnectionDeviceInfo");
283
284     const char* address = (*env)->GetStringUTFChars(env, jaddress, NULL);
285     if (!address)
286     {
287         LOGE("address is null");
288         return;
289     }
290
291     CAUnsetAutoConnectionDeviceInfo(address);
292
293     (*env)->ReleaseStringUTFChars(env, jaddress, address);
294 }
295
296 JNIEXPORT void JNICALL
297 Java_org_iotivity_ca_CaInterface_caBtPairingInitialize(JNIEnv *env, jclass clazz,
298                                                        jobject context, jobject listener)
299 {
300     LOGI("caBtPairingInitialize");
301     (void)clazz;
302
303     CAUtilClientInitialize(env, g_jvm, context);
304
305     g_foundDeviceListenerObject = (*env)->NewGlobalRef(env, listener);
306     CAUtilSetFoundDeviceListener(g_foundDeviceListenerObject);
307 }
308
309 JNIEXPORT void JNICALL
310 Java_org_iotivity_ca_CaInterface_caBtPairingTerminate(JNIEnv *env, jclass clazz)
311 {
312     LOGI("caBtPairingTerminate");
313     (void)clazz;
314
315     if (g_foundDeviceListenerObject)
316     {
317         (*env)->DeleteGlobalRef(env, g_foundDeviceListenerObject);
318     }
319 }
320
321 JNIEXPORT void JNICALL
322 Java_org_iotivity_ca_CaInterface_caBtPairingStartScan(JNIEnv *env, jclass clazz)
323 {
324     LOGI("caBtPairingStartScan");
325     (void)clazz;
326     CAUtilStartScan(env);
327 }
328
329 JNIEXPORT void JNICALL
330 Java_org_iotivity_ca_CaInterface_caBtPairingStopScan(JNIEnv *env, jclass clazz)
331 {
332     LOGI("caBtPairingStopScan");
333     (void)clazz;
334     CAUtilStopScan(env);
335 }
336
337 JNIEXPORT void JNICALL
338 Java_org_iotivity_ca_CaInterface_caBtPairingCreateBond(JNIEnv *env, jclass clazz, jobject device)
339 {
340     LOGI("caBtPairingCreateBond");
341     (void)clazz;
342     CAUtilCreateBond(env, device);
343 }
344
345 JNIEXPORT void JNICALL
346 Java_org_iotivity_ca_CaInterface_setLeScanIntervalTimeImpl(JNIEnv *env, jclass clazz,
347                                                            jint intervalTime, jint workignCount)
348 {
349     LOGI("setLeScanIntervalTimeImpl");
350     (void)env;
351     (void)clazz;
352     CAUtilSetLEScanInterval(intervalTime, workignCount);
353 }
354