Merge branch 'master' into windows-port
[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;
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         CALEClientSetScanFlag(false);
347
348         CANetworkStatus_t newStatus = CA_INTERFACE_DOWN;
349         g_bleDeviceStateChangedCallback(newStatus);
350     }
351 }
352
353 JNIEXPORT void JNICALL
354 Java_org_iotivity_ca_CaLeClientInterface_caLeBondStateChangedCallback(JNIEnv *env, jobject obj,
355                                                                       jstring jaddr)
356 {
357     OIC_LOG(DEBUG, TAG, "CaLeClientInterface - Bond State Changed");
358     VERIFY_NON_NULL_VOID(env, TAG, "env is null");
359     VERIFY_NON_NULL_VOID(obj, TAG, "obj is null");
360     VERIFY_NON_NULL_VOID(jaddr, TAG, "jaddr is null");
361
362     // geneally 'addr' parameter will be not ble address, if you didn't bond for BLE.
363     // below logics will be needed when ble pairing is set.
364
365     CAResult_t res = CALEClientDisconnectforAddress(env, jaddr);
366     if (CA_STATUS_OK != res)
367     {
368         OIC_LOG(ERROR, TAG, "CALEClientDisconnectforAddress has failed");
369     }
370
371     // remove obj for client
372     res = CALEClientRemoveGattObjForAddr(env, jaddr);
373     if (CA_STATUS_OK != res)
374     {
375         OIC_LOG(ERROR, TAG, "CANativeRemoveGattObjForAddr has failed");
376     }
377
378     res = CALEClientRemoveDeviceInScanDeviceList(env, jaddr);
379     if (CA_STATUS_OK != res)
380     {
381         OIC_LOG(ERROR, TAG, "CALEClientRemoveDeviceInScanDeviceList has failed");
382     }
383
384     // remove obej for server
385     res = CALEServerRemoveDevice(env, jaddr);
386     if (CA_STATUS_OK != res)
387     {
388         OIC_LOG(ERROR, TAG, "CALEServerRemoveDevice has failed");
389     }
390 }
391
392 JNIEXPORT void JNICALL
393 Java_org_iotivity_ca_CaLeClientInterface_caLeGattNWConnectionStateChangeCallback(JNIEnv *env,
394                                                                                  jobject obj,
395                                                                                  jobject gatt,
396                                                                                  jint status,
397                                                                                  jint newState)
398 {
399     OIC_LOG_V(DEBUG, TAG, "CALeGattNWConnectionStateChangeCallback - status %d, newstate %d",
400               status, newState);
401     VERIFY_NON_NULL_VOID(env, TAG, "env");
402     VERIFY_NON_NULL_VOID(obj, TAG, "obj");
403     VERIFY_NON_NULL_VOID(gatt, TAG, "gatt");
404
405     jint state_disconnected = CALEGetConstantsValue(env, CLASSPATH_BT_PROFILE,
406                                                     "STATE_DISCONNECTED");
407     if (state_disconnected == newState)
408     {
409         jstring jni_address = CALEClientGetAddressFromGattObj(env, gatt);
410         if (!jni_address)
411         {
412             OIC_LOG(ERROR, TAG, "jni_address is null");
413             return;
414         }
415
416         const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
417         if (!address)
418         {
419             OIC_LOG(ERROR, TAG, "address is null");
420             return;
421         }
422
423         if (CONNECTION_FAILED_TO_BE_EASTABLISHED != status)
424         {
425             if (g_bleConnectionStateChangedCallback)
426             {
427                 OIC_LOG_V(DEBUG, TAG, "LE Disconnected state is %d, %s", newState, address);
428                 g_bleConnectionStateChangedCallback(CA_ADAPTER_GATT_BTLE, address, false);
429             }
430         }
431
432         (*env)->ReleaseStringUTFChars(env, jni_address, address);
433     }
434 }
435
436 JNIEXPORT void JNICALL
437 Java_org_iotivity_ca_CaLeServerInterface_caLeGattServerNWConnectionStateChangeCallback(
438         JNIEnv *env, jobject obj, jobject device, jint status, jint newState)
439 {
440     OIC_LOG_V(DEBUG, TAG, "caLeGattServerNWConnectionStateChangeCallback - status %d, newstate %d",
441               status, newState);
442     VERIFY_NON_NULL_VOID(env, TAG, "env");
443     VERIFY_NON_NULL_VOID(obj, TAG, "obj");
444     VERIFY_NON_NULL_VOID(device, TAG, "device");
445
446     if (CONNECTION_FAILED_TO_BE_EASTABLISHED != status)
447     {
448         if (g_bleConnectionStateChangedCallback)
449         {
450             jstring jni_remoteAddress = CALEGetAddressFromBTDevice(env, device);
451             if (!jni_remoteAddress)
452             {
453                 OIC_LOG(ERROR, TAG, "jni_remoteAddress is null");
454                 return;
455             }
456
457             const char* address = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);
458             if (!address)
459             {
460                 OIC_LOG(ERROR, TAG, "address is null");
461                 return;
462             }
463
464             // STATE_DISCONNECTED
465             jint state_disconnected = CALEGetConstantsValue(env, CLASSPATH_BT_PROFILE,
466                                                             "STATE_DISCONNECTED");
467
468             // STATE_CONNECTED
469             jint state_connected = CALEGetConstantsValue(env, CLASSPATH_BT_PROFILE,
470                                                          "STATE_CONNECTED");
471
472             if (state_disconnected == newState)
473             {
474                 OIC_LOG_V(DEBUG, TAG, "LE Disconnected state is %d, %s", newState, address);
475                 g_bleConnectionStateChangedCallback(CA_ADAPTER_GATT_BTLE, address, false);
476             }
477             else if (state_connected == newState)
478             {
479                 OIC_LOG_V(DEBUG, TAG, "LE Connected state is %d, %s", newState, address);
480                 g_bleConnectionStateChangedCallback(CA_ADAPTER_GATT_BTLE, address, true);
481             }
482             else
483             {
484                 OIC_LOG_V(DEBUG, TAG, "Unknown state : %d, %s", newState, address);
485             }
486
487             (*env)->ReleaseStringUTFChars(env, jni_remoteAddress, address);
488         }
489     }
490 }
491
492 JNIEXPORT void JNICALL
493 Java_org_iotivity_ca_CaLeClientInterface_caLeGattNWServicesDiscoveredCallback(JNIEnv *env,
494                                                                               jobject obj,
495                                                                               jobject gatt,
496                                                                               jint status)
497 {
498     OIC_LOG_V(DEBUG, TAG, "caLeGattNWServicesDiscoveredCallback - status %d", status);
499     VERIFY_NON_NULL_VOID(env, TAG, "env");
500     VERIFY_NON_NULL_VOID(obj, TAG, "obj");
501     VERIFY_NON_NULL_VOID(gatt, TAG, "gatt");
502
503     if (GATT_SUCCESS == status)
504     {
505         jstring jni_address = CALEGetAddressFromGatt(env, gatt);
506         if (!jni_address)
507         {
508             OIC_LOG(ERROR, TAG, "jni_address is null");
509             return;
510         }
511
512         if (CA_STATUS_OK != CALEStateConnectedCallback(env, jni_address, JNI_FALSE))
513         {
514             OIC_LOG(ERROR, TAG, "CALEStateConnectedCallback has failed");
515         }
516
517         (*env)->DeleteLocalRef(env, jni_address);
518     }
519 }
520
521 JNIEXPORT void JNICALL
522 Java_org_iotivity_ca_CaLeClientInterface_caLeGattNWDescriptorWriteCallback(JNIEnv *env,
523                                                                            jobject obj,
524                                                                            jobject gatt,
525                                                                            jint status)
526 {
527     OIC_LOG_V(DEBUG, TAG, "caLeGattNWDescriptorWriteCallback - status %d", status);
528     VERIFY_NON_NULL_VOID(env, TAG, "env");
529     VERIFY_NON_NULL_VOID(obj, TAG, "obj");
530     VERIFY_NON_NULL_VOID(gatt, TAG, "gatt");
531
532     if (GATT_SUCCESS == status)
533     {
534         jstring jni_address = CALEGetAddressFromGatt(env, gatt);
535         if (!jni_address)
536         {
537             OIC_LOG(ERROR, TAG, "jni_address is null");
538             return;
539         }
540
541         if (CA_STATUS_OK != CALEStateConnectedCallback(env, jni_address, JNI_TRUE))
542         {
543             OIC_LOG(ERROR, TAG, "CALEStateConnectedCallback has failed");
544         }
545
546         (*env)->DeleteLocalRef(env, jni_address);
547     }
548 }