4a65ed241bb8a0e1647722f8a9f4027eebe96235
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / bt_le_adapter / android / calenwmonitor.c
1 /******************************************************************
2  *
3  * Copyright 2014 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  ******************************************************************/
20
21 #include <jni.h>
22 #include <stdio.h>
23 #include <android/log.h>
24 #include "logger.h"
25 #include "calenwmonitor.h"
26 #include "caleclient.h"
27 #include "caleserver.h"
28 #include "caleutils.h"
29 #include "caleinterface.h"
30 #include "caadapterutils.h"
31
32 #include "camutex.h"
33
34 #include "org_iotivity_ca_CaLeClientInterface.h"
35 #include "org_iotivity_ca_CaLeServerInterface.h"
36
37 #define TAG PCF("OIC_CA_LE_MONITOR")
38
39 static const jint CONNECTION_FAILED_TO_BE_EASTABLISHED = 62;
40
41 static JavaVM *g_jvm;
42
43 /**
44  * @var g_bleDeviceStateChangedCallback
45  * @brief Maintains the callback to be notified on device state changed.
46  */
47 static CALEDeviceStateChangedCallback g_bleDeviceStateChangedCallback = NULL;
48
49 /**
50  * @var g_bleConnectionStateChangedCallback
51  * @brief Maintains the callback to be notified on device state changed.
52  */
53 static CALEConnectionStateChangedCallback g_bleConnectionStateChangedCallback = NULL;
54
55 /**
56  * @var g_bleDeviceStateChangedCbMutex
57  * @brief Mutex to synchronize access to the deviceStateChanged Callback when the state
58  *           of the LE adapter gets change.
59  */
60 static ca_mutex g_bleDeviceStateChangedCbMutex = NULL;
61
62 /**
63  * @var g_bleConnectionStateChangedCbMutex
64  * @brief Mutex to synchronize access to the LE ConnectionStateChanged Callback when the state
65  *           of the LE adapter gets change.
66  */
67 static ca_mutex g_bleConnectionStateChangedCbMutex = NULL;
68
69 //getting context
70 void CALENetworkMonitorJNISetContext()
71 {
72     OIC_LOG(DEBUG, TAG, "CALENetworkMonitorJNISetContext - it is not supported");
73 }
74
75 //getting jvm
76 void CALENetworkMonitorJniInit()
77 {
78     OIC_LOG(DEBUG, TAG, "CALENetworkMonitorJniInit");
79     g_jvm = CANativeJNIGetJavaVM();
80 }
81
82 void CALESetAdapterStateCallback(CALEDeviceStateChangedCallback callback)
83 {
84     OIC_LOG(DEBUG, TAG, "CALESetAdapterStateCallback");
85     g_bleDeviceStateChangedCallback = callback;
86 }
87
88 CAResult_t CAInitializeLEAdapter()
89 {
90     // Nothing to do.
91
92     return CA_STATUS_OK;
93 }
94
95 CAResult_t CAStartLEAdapter()
96 {
97     // Nothing to do.
98
99     return CA_STATUS_OK;
100 }
101
102 CAResult_t CAStopLEAdapter()
103 {
104     // Nothing to do.
105
106     return CA_STATUS_OK;
107 }
108
109 CAResult_t CAInitLENwkMonitorMutexVaraibles()
110 {
111     OIC_LOG(DEBUG, TAG, "IN");
112     if (NULL == g_bleDeviceStateChangedCbMutex)
113     {
114         g_bleDeviceStateChangedCbMutex = ca_mutex_new();
115         if (NULL == g_bleDeviceStateChangedCbMutex)
116         {
117             OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
118             return CA_STATUS_FAILED;
119         }
120     }
121
122     if (NULL == g_bleConnectionStateChangedCbMutex)
123     {
124         g_bleConnectionStateChangedCbMutex = ca_mutex_new();
125         if (NULL == g_bleConnectionStateChangedCbMutex)
126         {
127             OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
128             ca_mutex_free(g_bleDeviceStateChangedCbMutex);
129             return CA_STATUS_FAILED;
130         }
131     }
132
133     OIC_LOG(DEBUG, TAG, "OUT");
134     return CA_STATUS_OK;
135 }
136
137 void CATerminateLENwkMonitorMutexVaraibles()
138 {
139     OIC_LOG(DEBUG, TAG, "IN");
140
141     ca_mutex_free(g_bleDeviceStateChangedCbMutex);
142     g_bleDeviceStateChangedCbMutex = NULL;
143
144     ca_mutex_free(g_bleConnectionStateChangedCbMutex);
145     g_bleConnectionStateChangedCbMutex = NULL;
146
147     OIC_LOG(DEBUG, TAG, "OUT");
148 }
149
150 CAResult_t CAGetLEAdapterState()
151 {
152     OIC_LOG(DEBUG, TAG, "IN");
153
154     if (!g_jvm)
155     {
156         OIC_LOG(ERROR, TAG, "g_jvm is null");
157         return CA_STATUS_FAILED;
158     }
159
160     bool isAttached = false;
161     JNIEnv* env = NULL;
162     jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
163     if (JNI_OK != res)
164     {
165         OIC_LOG(DEBUG, TAG, "Could not get JNIEnv pointer");
166         res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
167
168         if (JNI_OK != res)
169         {
170             OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
171             return CA_STATUS_FAILED;
172         }
173         isAttached = true;
174     }
175
176     if (!CALEIsEnableBTAdapter(env))
177     {
178         OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
179         if (isAttached)
180         {
181             (*g_jvm)->DetachCurrentThread(g_jvm);
182         }
183         return CA_ADAPTER_NOT_ENABLED;
184     }
185
186     if (isAttached)
187     {
188         (*g_jvm)->DetachCurrentThread(g_jvm);
189     }
190
191     OIC_LOG(DEBUG, TAG, "OUT");
192     return CA_STATUS_OK;
193 }
194
195 CAResult_t CAInitializeLENetworkMonitor()
196 {
197     OIC_LOG(DEBUG, TAG, "IN");
198
199     CAResult_t res = CAInitLENwkMonitorMutexVaraibles();
200     if (CA_STATUS_OK != res)
201     {
202         OIC_LOG(ERROR, TAG, "CAInitLENwkMonitorMutexVaraibles has failed");
203         return CA_STATUS_FAILED;
204     }
205
206     CALENetworkMonitorJNISetContext();
207     CALENetworkMonitorJniInit();
208
209     OIC_LOG(DEBUG, TAG, "OUT");
210
211     return CA_STATUS_OK;
212
213 }
214
215 void CATerminateLENetworkMonitor()
216 {
217     OIC_LOG(DEBUG, TAG, "IN");
218
219     CATerminateLENwkMonitorMutexVaraibles();
220
221     OIC_LOG(DEBUG, TAG, "OUT");
222 }
223
224 CAResult_t CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCallback callback)
225 {
226     OIC_LOG(DEBUG, TAG, "IN");
227
228     OIC_LOG(DEBUG, TAG, "Setting CALEDeviceStateChangedCallback");
229
230     ca_mutex_lock(g_bleDeviceStateChangedCbMutex);
231     CALESetAdapterStateCallback(callback);
232     ca_mutex_unlock(g_bleDeviceStateChangedCbMutex);
233
234     OIC_LOG(DEBUG, TAG, "OUT");
235     return CA_STATUS_OK;
236 }
237
238 CAResult_t CAUnSetLEAdapterStateChangedCb()
239 {
240     OIC_LOG(DEBUG, TAG, "it is not required in this platform");
241     return CA_STATUS_OK;
242 }
243
244 CAResult_t CASetLENWConnectionStateChangedCb(CALEConnectionStateChangedCallback callback)
245 {
246     OIC_LOG(DEBUG, TAG, "IN");
247     ca_mutex_lock(g_bleConnectionStateChangedCbMutex);
248     g_bleConnectionStateChangedCallback = callback;
249     ca_mutex_unlock(g_bleConnectionStateChangedCbMutex);
250     OIC_LOG(DEBUG, TAG, "OUT");
251     return CA_STATUS_OK;
252 }
253
254 CAResult_t CAUnsetLENWConnectionStateChangedCb()
255 {
256     OIC_LOG(DEBUG, TAG, "IN");
257     ca_mutex_lock(g_bleConnectionStateChangedCbMutex);
258     g_bleConnectionStateChangedCallback = NULL;
259     ca_mutex_unlock(g_bleConnectionStateChangedCbMutex);
260     OIC_LOG(DEBUG, TAG, "OUT");
261     return CA_STATUS_OK;
262 }
263
264 static CAResult_t CALEStateConnectedCallback(JNIEnv *env, jstring jni_address,
265                                              jboolean isDescriptorFound)
266 {
267     VERIFY_NON_NULL(env, TAG, "env");
268     VERIFY_NON_NULL(jni_address, TAG, "jni_address");
269
270     if (CALEClientGetFlagFromState(env, jni_address, CA_LE_DESCRIPTOR_FOUND) == isDescriptorFound)
271     {
272         if (g_bleConnectionStateChangedCallback)
273         {
274             const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
275             if (!address)
276             {
277                 OIC_LOG(ERROR, TAG, "address is null");
278                 return CA_STATUS_FAILED;
279             }
280
281             g_bleConnectionStateChangedCallback(CA_ADAPTER_GATT_BTLE, address, true);
282             OIC_LOG(DEBUG, TAG, "BLE is connected");
283
284             (*env)->ReleaseStringUTFChars(env, jni_address, address);
285         }
286     }
287
288     return CA_STATUS_OK;
289 }
290
291 JNIEXPORT void JNICALL
292 Java_org_iotivity_ca_CaLeClientInterface_caLeStateChangedCallback(JNIEnv *env, jobject obj,
293                                                                    jint status)
294 {
295     VERIFY_NON_NULL_VOID(env, TAG, "env is null");
296     VERIFY_NON_NULL_VOID(obj, TAG, "obj is null");
297
298     OIC_LOG_V(DEBUG, TAG, "CaLeClientInterface - Network State Changed : status(%d)", status);
299
300     if (!g_bleDeviceStateChangedCallback)
301     {
302         OIC_LOG(ERROR, TAG, "gNetworkChangeCb is null");
303         return;
304     }
305
306     jint state_on = CALEGetConstantsValue(env, CLASSPATH_BT_ADAPTER, "STATE_ON");
307     jint state_off = CALEGetConstantsValue(env, CLASSPATH_BT_ADAPTER, "STATE_OFF");
308     jint state_turning_off = CALEGetConstantsValue(env, CLASSPATH_BT_ADAPTER, "STATE_TURNING_OFF");
309
310     if (state_on == status) // STATE_ON:12
311     {
312         CANetworkStatus_t newStatus = CA_INTERFACE_UP;
313         CALEClientCreateDeviceList();
314         CALEServerCreateCachedDeviceList();
315
316         g_bleDeviceStateChangedCallback(newStatus);
317     }
318     else if (state_turning_off == status) // BT_STATE_TURNING_OFF:13
319     {
320         // gatt Device list will be removed.
321         // so it is need to create list again when adapter is enabled.
322         CAStopLEGattClient();
323     }
324     else if (state_off == status) // STATE_OFF:10
325     {
326         // remove obj for client
327         CAResult_t res = CALEClientRemoveAllGattObjs(env);
328         if (CA_STATUS_OK != res)
329         {
330             OIC_LOG(ERROR, TAG, "CALEClientRemoveAllGattObjs has failed");
331         }
332
333         res = CALEClientResetDeviceStateForAll();
334         if (CA_STATUS_OK != res)
335         {
336             OIC_LOG(ERROR, TAG, "CALEClientResetDeviceStateForAll has failed");
337         }
338
339         // remove obj for server
340         res = CALEServerRemoveAllDevices(env);
341         if (CA_STATUS_OK != res)
342         {
343             OIC_LOG(ERROR, TAG, "CALEServerRemoveAllDevices has failed");
344         }
345
346         CANetworkStatus_t newStatus = CA_INTERFACE_DOWN;
347         g_bleDeviceStateChangedCallback(newStatus);
348     }
349 }
350
351 JNIEXPORT void JNICALL
352 Java_org_iotivity_ca_CaLeClientInterface_caLeBondStateChangedCallback(JNIEnv *env, jobject obj,
353                                                                       jstring jaddr)
354 {
355     OIC_LOG(DEBUG, TAG, "CaLeClientInterface - Bond State Changed");
356     VERIFY_NON_NULL_VOID(env, TAG, "env is null");
357     VERIFY_NON_NULL_VOID(obj, TAG, "obj is null");
358     VERIFY_NON_NULL_VOID(jaddr, TAG, "jaddr is null");
359
360     // geneally 'addr' parameter will be not ble address, if you didn't bond for BLE.
361     // below logics will be needed when ble pairing is set.
362
363     CAResult_t res = CALEClientDisconnectforAddress(env, jaddr);
364     if (CA_STATUS_OK != res)
365     {
366         OIC_LOG(ERROR, TAG, "CALEClientDisconnectforAddress has failed");
367     }
368
369     // remove obj for client
370     res = CALEClientRemoveGattObjForAddr(env, jaddr);
371     if (CA_STATUS_OK != res)
372     {
373         OIC_LOG(ERROR, TAG, "CANativeRemoveGattObjForAddr has failed");
374     }
375
376     res = CALEClientRemoveDeviceInScanDeviceList(env, jaddr);
377     if (CA_STATUS_OK != res)
378     {
379         OIC_LOG(ERROR, TAG, "CALEClientRemoveDeviceInScanDeviceList has failed");
380     }
381
382     // remove obej for server
383     res = CALEServerRemoveDevice(env, jaddr);
384     if (CA_STATUS_OK != res)
385     {
386         OIC_LOG(ERROR, TAG, "CALEServerRemoveDevice has failed");
387     }
388 }
389
390 JNIEXPORT void JNICALL
391 Java_org_iotivity_ca_CaLeClientInterface_caLeGattNWConnectionStateChangeCallback(JNIEnv *env,
392                                                                                  jobject obj,
393                                                                                  jobject gatt,
394                                                                                  jint status,
395                                                                                  jint newState)
396 {
397     OIC_LOG_V(DEBUG, TAG, "CALeGattNWConnectionStateChangeCallback - status %d, newstate %d",
398               status, newState);
399     VERIFY_NON_NULL_VOID(env, TAG, "env");
400     VERIFY_NON_NULL_VOID(obj, TAG, "obj");
401     VERIFY_NON_NULL_VOID(gatt, TAG, "gatt");
402
403     jint state_disconnected = CALEGetConstantsValue(env, CLASSPATH_BT_PROFILE,
404                                                     "STATE_DISCONNECTED");
405     if (state_disconnected == newState)
406     {
407         jstring jni_address = CALEClientGetAddressFromGattObj(env, gatt);
408         if (!jni_address)
409         {
410             OIC_LOG(ERROR, TAG, "jni_address is null");
411             return;
412         }
413
414         const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
415         if (!address)
416         {
417             OIC_LOG(ERROR, TAG, "address is null");
418             return;
419         }
420
421         if (CONNECTION_FAILED_TO_BE_EASTABLISHED != status)
422         {
423             if (g_bleConnectionStateChangedCallback)
424             {
425                 OIC_LOG_V(DEBUG, TAG, "LE Disconnected state is %d, %s", newState, address);
426                 g_bleConnectionStateChangedCallback(CA_ADAPTER_GATT_BTLE, address, false);
427             }
428         }
429
430         (*env)->ReleaseStringUTFChars(env, jni_address, address);
431     }
432 }
433
434 JNIEXPORT void JNICALL
435 Java_org_iotivity_ca_CaLeServerInterface_caLeGattServerNWConnectionStateChangeCallback(
436         JNIEnv *env, jobject obj, jobject device, jint status, jint newState)
437 {
438     OIC_LOG_V(DEBUG, TAG, "caLeGattServerNWConnectionStateChangeCallback - status %d, newstate %d",
439               status, newState);
440     VERIFY_NON_NULL_VOID(env, TAG, "env");
441     VERIFY_NON_NULL_VOID(obj, TAG, "obj");
442     VERIFY_NON_NULL_VOID(device, TAG, "device");
443
444     if (CONNECTION_FAILED_TO_BE_EASTABLISHED != status)
445     {
446         if (g_bleConnectionStateChangedCallback)
447         {
448             jstring jni_remoteAddress = CALEGetAddressFromBTDevice(env, device);
449             if (!jni_remoteAddress)
450             {
451                 OIC_LOG(ERROR, TAG, "jni_remoteAddress is null");
452                 return;
453             }
454
455             const char* address = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);
456             if (!address)
457             {
458                 OIC_LOG(ERROR, TAG, "address is null");
459                 return;
460             }
461
462             // STATE_DISCONNECTED
463             jint state_disconnected = CALEGetConstantsValue(env, CLASSPATH_BT_PROFILE,
464                                                             "STATE_DISCONNECTED");
465
466             // STATE_CONNECTED
467             jint state_connected = CALEGetConstantsValue(env, CLASSPATH_BT_PROFILE,
468                                                          "STATE_CONNECTED");
469
470             if (state_disconnected == newState)
471             {
472                 OIC_LOG_V(DEBUG, TAG, "LE Disconnected state is %d, %s", newState, address);
473                 g_bleConnectionStateChangedCallback(CA_ADAPTER_GATT_BTLE, address, false);
474             }
475             else if (state_connected == newState)
476             {
477                 OIC_LOG_V(DEBUG, TAG, "LE Connected state is %d, %s", newState, address);
478                 g_bleConnectionStateChangedCallback(CA_ADAPTER_GATT_BTLE, address, true);
479             }
480             else
481             {
482                 OIC_LOG_V(DEBUG, TAG, "Unknown state : %d, %s", newState, address);
483             }
484
485             (*env)->ReleaseStringUTFChars(env, jni_remoteAddress, address);
486         }
487     }
488 }
489
490 JNIEXPORT void JNICALL
491 Java_org_iotivity_ca_CaLeClientInterface_caLeGattNWServicesDiscoveredCallback(JNIEnv *env,
492                                                                               jobject obj,
493                                                                               jobject gatt,
494                                                                               jint status)
495 {
496     OIC_LOG_V(DEBUG, TAG, "caLeGattNWServicesDiscoveredCallback - status %d", status);
497     VERIFY_NON_NULL_VOID(env, TAG, "env");
498     VERIFY_NON_NULL_VOID(obj, TAG, "obj");
499     VERIFY_NON_NULL_VOID(gatt, TAG, "gatt");
500
501     if (GATT_SUCCESS == status)
502     {
503         jstring jni_address = CALEGetAddressFromGatt(env, gatt);
504         if (!jni_address)
505         {
506             OIC_LOG(ERROR, TAG, "jni_address is null");
507             return;
508         }
509
510         if (CA_STATUS_OK != CALEStateConnectedCallback(env, jni_address, JNI_FALSE))
511         {
512             OIC_LOG(ERROR, TAG, "CALEStateConnectedCallback has failed");
513         }
514
515         (*env)->DeleteLocalRef(env, jni_address);
516     }
517 }
518
519 JNIEXPORT void JNICALL
520 Java_org_iotivity_ca_CaLeClientInterface_caLeGattNWDescriptorWriteCallback(JNIEnv *env,
521                                                                            jobject obj,
522                                                                            jobject gatt,
523                                                                            jint status)
524 {
525     OIC_LOG_V(DEBUG, TAG, "caLeGattNWDescriptorWriteCallback - status %d", status);
526     VERIFY_NON_NULL_VOID(env, TAG, "env");
527     VERIFY_NON_NULL_VOID(obj, TAG, "obj");
528     VERIFY_NON_NULL_VOID(gatt, TAG, "gatt");
529
530     if (GATT_SUCCESS == status)
531     {
532         jstring jni_address = CALEGetAddressFromGatt(env, gatt);
533         if (!jni_address)
534         {
535             OIC_LOG(ERROR, TAG, "jni_address is null");
536             return;
537         }
538
539         if (CA_STATUS_OK != CALEStateConnectedCallback(env, jni_address, JNI_TRUE))
540         {
541             OIC_LOG(ERROR, TAG, "CALEStateConnectedCallback has failed");
542         }
543
544         (*env)->DeleteLocalRef(env, jni_address);
545     }
546 }