added CA interface to monitoring network status for android ble.
[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_listenerObject = NULL;
36 static JavaVM *g_jvm = NULL;
37
38 JNIEXPORT jint JNI_OnLoad(JavaVM *jvm, void *reserved)
39 {
40     LOGI("CaInterface_initialize");
41     g_jvm = jvm;
42     CANativeJNISetJavaVM(jvm);
43
44     return JNI_VERSION_1_6;
45 }
46
47 void JNI_OnUnload(JavaVM *jvm, void *reserved)
48 {
49     return;
50 }
51
52 JNIEXPORT void JNICALL
53 Java_org_iotivity_ca_CaInterface_initialize
54 (JNIEnv *env, jclass clazz, jobject activity, jobject context)
55 {
56     LOGI("CaInterface_initialize");
57
58     CANativeSetActivity(env, activity);
59     CANativeJNISetContext(env, context);
60 }
61
62 void CAManagerConnectionStateChangedCB(CATransportAdapter_t adapter,
63                                        const char *remote_address,
64                                        bool connected)
65 {
66     LOGI("Callback - CAManagerConnectionStateChangedCB : type(%d), address(%s), connected(%d)",
67          adapter, remote_address, connected);
68
69     if (!g_listenerObject)
70     {
71         LOGE("g_listener is NULL, cannot have callback");
72         return;
73     }
74
75     bool isAttached = false;
76     JNIEnv* env;
77     jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
78     if (JNI_OK != res)
79     {
80         LOGI("AttachCurrentThread will be called for JNIEnv pointer");
81         res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
82
83         if (JNI_OK != res)
84         {
85             LOGE("AttachCurrentThread has failed");
86             return;
87         }
88         isAttached = true;
89     }
90
91     jclass jni_cls_listener = (*env)->GetObjectClass(env, g_listenerObject);
92     if (!jni_cls_listener)
93     {
94         LOGE("could not get jni_cls_listener");
95         goto exit_error;
96     }
97
98     jmethodID jni_mid_listener = (*env)->GetMethodID(env, jni_cls_listener,
99                                                      "onConnectionStateChanged",
100                                                      "(Lorg/iotivity/base/OcConnectivityType;"
101                                                      "Ljava/lang/String;Z)V");
102     if (!jni_mid_listener)
103     {
104         LOGE("could not get Method ID");
105         goto exit_error;
106     }
107
108     jstring jni_address = (*env)->NewStringUTF(env, remote_address);
109     if (!jni_address)
110     {
111         LOGE("jni_address is null");
112         goto exit_error;
113     }
114
115     jclass jni_cls_enum = (*env)->FindClass(env, "org/iotivity/base/OcConnectivityType");
116     if (!jni_cls_enum)
117     {
118         LOGE("could not get jni_cls_enum");
119         goto exit_error;
120     }
121
122     jmethodID jni_mid_enum = (*env)->GetStaticMethodID(env, jni_cls_enum, "getInstance",
123                                                        "(I)Lorg/iotivity/base/OcConnectivityType;");
124     if (!jni_mid_enum)
125     {
126         LOGE("could not get Method ID (getInstance)");
127         goto exit_error;
128     }
129
130     jobject jni_adaptertype = (*env)->CallStaticObjectMethod(env, jni_cls_enum,
131                                                              jni_mid_enum, adapter);
132     (*env)->CallVoidMethod(env, g_listenerObject, jni_mid_listener,
133                            jni_adaptertype, jni_address,
134                            (jboolean)connected);
135
136 exit_error:
137     if (isAttached)
138     {
139         (*g_jvm)->DetachCurrentThread(g_jvm);
140     }
141
142     LOGI("OUT - CAManagerConnectionStateChangedCB");
143 }
144
145 void CAManagerAdapterStateChangedCB(CATransportAdapter_t adapter, bool enabled)
146 {
147     LOGI("Callback - CAManagerAdapterStateChangedCB : type(%d), enabled(%d)",
148          adapter, enabled);
149
150     if (!g_listenerObject)
151     {
152         LOGE("g_listener is NULL, cannot have callback");
153         return;
154     }
155
156     bool isAttached = false;
157     JNIEnv* env;
158     jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
159     if (JNI_OK != res)
160     {
161         LOGI("AttachCurrentThread will be called for JNIEnv pointer");
162         res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
163
164         if (JNI_OK != res)
165         {
166             LOGE("AttachCurrentThread has failed");
167             return;
168         }
169         isAttached = true;
170     }
171
172     jclass jni_cls_listener = (*env)->GetObjectClass(env, g_listenerObject);
173     if (!jni_cls_listener)
174     {
175         LOGE("could not get jni_cls_listener");
176         goto exit_error;
177     }
178
179     jmethodID jni_mid_listener = (*env)->GetMethodID(env, jni_cls_listener,
180                                                      "onAdapterStateChanged",
181                                                      "(Lorg/iotivity/base/OcConnectivityType;Z)V");
182     if (!jni_mid_listener)
183     {
184         LOGE("could not get Method ID");
185         goto exit_error;
186     }
187
188     jclass jni_cls_enum = (*env)->FindClass(env, "org/iotivity/base/OcConnectivityType");
189     if (!jni_cls_enum)
190     {
191         LOGE("could not get jni_cls_enum");
192         goto exit_error;
193     }
194
195     jmethodID jni_mid_enum = (*env)->GetStaticMethodID(env, jni_cls_enum, "getInstance",
196                                                        "(I)Lorg/iotivity/base/OcConnectivityType;");
197     if (!jni_mid_enum)
198     {
199         LOGE("could not get Method ID (getInstance)");
200         goto exit_error;
201     }
202
203     jobject jni_adaptertype = (*env)->CallStaticObjectMethod(env, jni_cls_enum,
204                                                              jni_mid_enum, adapter);
205
206     (*env)->CallVoidMethod(env, g_listenerObject, jni_mid_listener,
207                            jni_adaptertype, (jboolean)enabled);
208
209 exit_error:
210     if (isAttached)
211     {
212         (*g_jvm)->DetachCurrentThread(g_jvm);
213     }
214     LOGI("OUT -  CAManagerAdapterStateChangedCB");
215 }
216
217 JNIEXPORT void JNICALL
218 Java_org_iotivity_ca_CaInterface_caManagerInitialize(JNIEnv *env, jclass clazz,
219                                                      jobject context, jobject listener)
220 {
221     LOGI("CaManagere_initialize");
222
223     g_listenerObject = (*env)->NewGlobalRef(env, listener);
224
225     CARegisterNetworkMonitorHandler(CAManagerAdapterStateChangedCB,
226                                     CAManagerConnectionStateChangedCB);
227 }
228
229 JNIEXPORT void JNICALL
230 Java_org_iotivity_ca_CaInterface_caManagerTerminate(JNIEnv *env, jclass clazz)
231 {
232     LOGI("CaManager_terminate");
233
234     CAUtilClientTerminate(env);
235
236     if (g_listenerObject)
237     {
238         (*env)->DeleteGlobalRef(env, g_listenerObject);
239     }
240 }
241
242 JNIEXPORT void JNICALL
243 Java_org_iotivity_ca_CaInterface_caManagerSetAutoConnectionDeviceInfo(JNIEnv *env,
244                                                                       jclass clazz,
245                                                                       jstring jaddress)
246 {
247     LOGI("CaManager_setAutoConnectionDeviceInfo");
248
249     const char* address = (*env)->GetStringUTFChars(env, jaddress, NULL);
250     if (!address)
251     {
252         LOGE("address is null");
253         return;
254     }
255
256     CASetAutoConnectionDeviceInfo(address);
257 }
258
259 JNIEXPORT void JNICALL
260 Java_org_iotivity_ca_CaInterface_caManagerUnsetAutoConnectionDeviceInfo(JNIEnv *env,
261                                                                         jclass clazz,
262                                                                         jstring jaddress)
263 {
264     LOGI("CaManager_unsetAutoConnectionDeviceInfo");
265
266     const char* address = (*env)->GetStringUTFChars(env, jaddress, NULL);
267     if (!address)
268     {
269         LOGE("address is null");
270         return;
271     }
272
273     CAUnsetAutoConnectionDeviceInfo(address);
274 }