Merge branch 'master' into extended-easysetup
[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
39 JNIEXPORT jint JNI_OnLoad(JavaVM *jvm, void *reserved)
40 {
41     LOGI("CaInterface_initialize");
42     g_jvm = jvm;
43     CANativeJNISetJavaVM(jvm);
44
45     return JNI_VERSION_1_6;
46 }
47
48 void JNI_OnUnload(JavaVM *jvm, void *reserved)
49 {
50     return;
51 }
52
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
63 void CAManagerConnectionStateChangedCB(const CAEndpoint_t *info,
64                                        bool connected)
65 {
66     if (!info)
67     {
68         LOGE("info is NULL");
69         return;
70     }
71
72     if (!g_listenerObject)
73     {
74         LOGE("g_listener is NULL, cannot have callback");
75         return;
76     }
77
78     LOGI("Callback - CAManagerConnectionStateChangedCB : type(%d), address(%s), connected(%d)",
79          info->adapter, info->addr, connected);
80
81     bool isAttached = false;
82     JNIEnv* env = NULL;
83     jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
84     if (JNI_OK != res)
85     {
86         LOGI("AttachCurrentThread will be called for JNIEnv pointer");
87         res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
88
89         if (JNI_OK != res)
90         {
91             LOGE("AttachCurrentThread has failed");
92             return;
93         }
94         isAttached = true;
95     }
96
97     jclass jni_cls_listener = (*env)->GetObjectClass(env, g_listenerObject);
98     if (!jni_cls_listener)
99     {
100         LOGE("could not get jni_cls_listener");
101         goto exit_error;
102     }
103
104     jmethodID jni_mid_listener = (*env)->GetMethodID(env, jni_cls_listener,
105                                                      "onConnectionStateChanged",
106                                                      "(Lorg/iotivity/base/OcConnectivityType;"
107                                                      "Ljava/lang/String;Z)V");
108     if (!jni_mid_listener)
109     {
110         LOGE("could not get Method ID");
111         goto exit_error;
112     }
113
114     jstring jni_address = (*env)->NewStringUTF(env, info->addr);
115     if (!jni_address)
116     {
117         LOGE("jni_address is null");
118         goto exit_error;
119     }
120
121     jclass jni_cls_enum = (*env)->FindClass(env, "org/iotivity/base/OcConnectivityType");
122     if (!jni_cls_enum)
123     {
124         LOGE("could not get jni_cls_enum");
125         goto exit_error;
126     }
127
128     jmethodID jni_mid_enum = (*env)->GetStaticMethodID(env, jni_cls_enum, "getInstance",
129                                                        "(I)Lorg/iotivity/base/OcConnectivityType;");
130     if (!jni_mid_enum)
131     {
132         LOGE("could not get Method ID (getInstance)");
133         goto exit_error;
134     }
135
136     jobject jni_adaptertype = (*env)->CallStaticObjectMethod(env, jni_cls_enum,
137                                                              jni_mid_enum, info->adapter);
138     (*env)->CallVoidMethod(env, g_listenerObject, jni_mid_listener,
139                            jni_adaptertype, jni_address,
140                            (jboolean)connected);
141
142 exit_error:
143     if (isAttached)
144     {
145         (*g_jvm)->DetachCurrentThread(g_jvm);
146     }
147
148     LOGI("OUT - CAManagerConnectionStateChangedCB");
149 }
150
151 void CAManagerAdapterStateChangedCB(CATransportAdapter_t adapter, bool enabled)
152 {
153     LOGI("Callback - CAManagerAdapterStateChangedCB : type(%d), enabled(%d)",
154          adapter, enabled);
155
156     if (!g_listenerObject)
157     {
158         LOGE("g_listener is NULL, cannot have callback");
159         return;
160     }
161
162     bool isAttached = false;
163     JNIEnv* env = NULL;
164     jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
165     if (JNI_OK != res)
166     {
167         LOGI("AttachCurrentThread will be called for JNIEnv pointer");
168         res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
169
170         if (JNI_OK != res)
171         {
172             LOGE("AttachCurrentThread has failed");
173             return;
174         }
175         isAttached = true;
176     }
177
178     jclass jni_cls_listener = (*env)->GetObjectClass(env, g_listenerObject);
179     if (!jni_cls_listener)
180     {
181         LOGE("could not get jni_cls_listener");
182         goto exit_error;
183     }
184
185     jmethodID jni_mid_listener = (*env)->GetMethodID(env, jni_cls_listener,
186                                                      "onAdapterStateChanged",
187                                                      "(Lorg/iotivity/base/OcConnectivityType;Z)V");
188     if (!jni_mid_listener)
189     {
190         LOGE("could not get Method ID");
191         goto exit_error;
192     }
193
194     jclass jni_cls_enum = (*env)->FindClass(env, "org/iotivity/base/OcConnectivityType");
195     if (!jni_cls_enum)
196     {
197         LOGE("could not get jni_cls_enum");
198         goto exit_error;
199     }
200
201     jmethodID jni_mid_enum = (*env)->GetStaticMethodID(env, jni_cls_enum, "getInstance",
202                                                        "(I)Lorg/iotivity/base/OcConnectivityType;");
203     if (!jni_mid_enum)
204     {
205         LOGE("could not get Method ID (getInstance)");
206         goto exit_error;
207     }
208
209     jobject jni_adaptertype = (*env)->CallStaticObjectMethod(env, jni_cls_enum,
210                                                              jni_mid_enum, adapter);
211
212     (*env)->CallVoidMethod(env, g_listenerObject, jni_mid_listener,
213                            jni_adaptertype, (jboolean)enabled);
214
215 exit_error:
216     if (isAttached)
217     {
218         (*g_jvm)->DetachCurrentThread(g_jvm);
219     }
220     LOGI("OUT -  CAManagerAdapterStateChangedCB");
221 }
222
223 JNIEXPORT void JNICALL
224 Java_org_iotivity_ca_CaInterface_caManagerInitialize(JNIEnv *env, jclass clazz,
225                                                      jobject context, jobject listener)
226 {
227     LOGI("CaManagere_initialize");
228
229     CAUtilClientInitialize(env, g_jvm, context);
230
231     g_listenerObject = (*env)->NewGlobalRef(env, listener);
232
233     CARegisterNetworkMonitorHandler(CAManagerAdapterStateChangedCB,
234                                     CAManagerConnectionStateChangedCB);
235 }
236
237 JNIEXPORT void JNICALL
238 Java_org_iotivity_ca_CaInterface_caManagerTerminate(JNIEnv *env, jclass clazz)
239 {
240     LOGI("CaManager_terminate");
241
242     CAUtilClientTerminate(env);
243
244     if (g_listenerObject)
245     {
246         (*env)->DeleteGlobalRef(env, g_listenerObject);
247         g_listenerObject = NULL;
248     }
249 }
250
251 JNIEXPORT void JNICALL
252 Java_org_iotivity_ca_CaInterface_caManagerSetAutoConnectionDeviceInfo(JNIEnv *env,
253                                                                       jclass clazz,
254                                                                       jstring jaddress)
255 {
256     LOGI("CaManager_setAutoConnectionDeviceInfo");
257
258     const char* address = (*env)->GetStringUTFChars(env, jaddress, NULL);
259     if (!address)
260     {
261         LOGE("address is null");
262         return;
263     }
264
265     CASetAutoConnectionDeviceInfo(address);
266
267     (*env)->ReleaseStringUTFChars(env, jaddress, address);
268 }
269
270 JNIEXPORT void JNICALL
271 Java_org_iotivity_ca_CaInterface_caManagerUnsetAutoConnectionDeviceInfo(JNIEnv *env,
272                                                                         jclass clazz,
273                                                                         jstring jaddress)
274 {
275     LOGI("CaManager_unsetAutoConnectionDeviceInfo");
276
277     const char* address = (*env)->GetStringUTFChars(env, jaddress, NULL);
278     if (!address)
279     {
280         LOGE("address is null");
281         return;
282     }
283
284     CAUnsetAutoConnectionDeviceInfo(address);
285
286     (*env)->ReleaseStringUTFChars(env, jaddress, address);
287 }
288
289 JNIEXPORT void JNICALL
290 Java_org_iotivity_ca_CaInterface_caBtPairingInitialize(JNIEnv *env, jclass clazz,
291                                                        jobject context, jobject listener)
292 {
293     LOGI("caBtPairingInitialize");
294     (void)clazz;
295
296     CAUtilClientInitialize(env, g_jvm, context);
297
298     g_foundDeviceListenerObject = (*env)->NewGlobalRef(env, listener);
299     CAUtilSetFoundDeviceListener(g_foundDeviceListenerObject);
300 }
301
302 JNIEXPORT void JNICALL
303 Java_org_iotivity_ca_CaInterface_caBtPairingTerminate(JNIEnv *env, jclass clazz)
304 {
305     LOGI("caBtPairingTerminate");
306     (void)clazz;
307
308     if (g_foundDeviceListenerObject)
309     {
310         (*env)->DeleteGlobalRef(env, g_foundDeviceListenerObject);
311     }
312 }
313
314 JNIEXPORT void JNICALL
315 Java_org_iotivity_ca_CaInterface_caBtPairingStartScan(JNIEnv *env, jclass clazz)
316 {
317     LOGI("caBtPairingStartScan");
318     (void)clazz;
319     CAUtilStartScan(env);
320 }
321
322 JNIEXPORT void JNICALL
323 Java_org_iotivity_ca_CaInterface_caBtPairingStopScan(JNIEnv *env, jclass clazz)
324 {
325     LOGI("caBtPairingStopScan");
326     (void)clazz;
327     CAUtilStopScan(env);
328 }
329
330 JNIEXPORT void JNICALL
331 Java_org_iotivity_ca_CaInterface_caBtPairingCreateBond(JNIEnv *env, jclass clazz, jobject device)
332 {
333     LOGI("caBtPairingCreateBond");
334     (void)clazz;
335     CAUtilCreateBond(env, device);
336 }
337
338 JNIEXPORT void JNICALL
339 Java_org_iotivity_ca_CaInterface_setLeScanIntervalTimeImpl(JNIEnv *env, jclass clazz,
340                                                            jint intervalTime, jint workignCount)
341 {
342     LOGI("setLeScanIntervalTimeImpl");
343     (void)env;
344     (void)clazz;
345     CAUtilSetLEScanInterval(intervalTime, workignCount);
346 }
347