Merge branch 'group-manager'
[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;
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;
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     }
243 }
244
245 JNIEXPORT void JNICALL
246 Java_org_iotivity_ca_CaInterface_caManagerSetAutoConnectionDeviceInfo(JNIEnv *env,
247                                                                       jclass clazz,
248                                                                       jstring jaddress)
249 {
250     LOGI("CaManager_setAutoConnectionDeviceInfo");
251
252     const char* address = (*env)->GetStringUTFChars(env, jaddress, NULL);
253     if (!address)
254     {
255         LOGE("address is null");
256         return;
257     }
258
259     CASetAutoConnectionDeviceInfo(address);
260 }
261
262 JNIEXPORT void JNICALL
263 Java_org_iotivity_ca_CaInterface_caManagerUnsetAutoConnectionDeviceInfo(JNIEnv *env,
264                                                                         jclass clazz,
265                                                                         jstring jaddress)
266 {
267     LOGI("CaManager_unsetAutoConnectionDeviceInfo");
268
269     const char* address = (*env)->GetStringUTFChars(env, jaddress, NULL);
270     if (!address)
271     {
272         LOGE("address is null");
273         return;
274     }
275
276     CAUnsetAutoConnectionDeviceInfo(address);
277 }
278
279 JNIEXPORT void JNICALL
280 Java_org_iotivity_ca_CaInterface_caBtPairingInitialize(JNIEnv *env, jclass clazz,
281                                                        jobject context, jobject listener)
282 {
283     LOGI("caBtPairingInitialize");
284     (void)clazz;
285
286     CAUtilClientInitialize(env, g_jvm, context);
287
288     g_foundDeviceListenerObject = (*env)->NewGlobalRef(env, listener);
289     CAUtilSetFoundDeviceListener(g_foundDeviceListenerObject);
290 }
291
292 JNIEXPORT void JNICALL
293 Java_org_iotivity_ca_CaInterface_caBtPairingTerminate(JNIEnv *env, jclass clazz)
294 {
295     LOGI("caBtPairingTerminate");
296     (void)clazz;
297
298     if (g_foundDeviceListenerObject)
299     {
300         (*env)->DeleteGlobalRef(env, g_foundDeviceListenerObject);
301     }
302 }
303
304 JNIEXPORT void JNICALL
305 Java_org_iotivity_ca_CaInterface_caBtPairingStartScan(JNIEnv *env, jclass clazz)
306 {
307     LOGI("caBtPairingStartScan");
308     (void)clazz;
309     CAUtilStartScan(env);
310 }
311
312 JNIEXPORT void JNICALL
313 Java_org_iotivity_ca_CaInterface_caBtPairingStopScan(JNIEnv *env, jclass clazz)
314 {
315     LOGI("caBtPairingStopScan");
316     (void)clazz;
317     CAUtilStopScan(env);
318 }
319
320 JNIEXPORT void JNICALL
321 Java_org_iotivity_ca_CaInterface_caBtPairingCreateBond(JNIEnv *env, jclass clazz, jobject device)
322 {
323     LOGI("caBtPairingCreateBond");
324     (void)clazz;
325     CAUtilCreateBond(env, device);
326 }