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