1 /******************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 ******************************************************************/
24 #include <android/log.h>
26 #include "caleserver.h"
27 #include "caleutils.h"
28 #include "caleinterface.h"
29 #include "caadapterutils.h"
32 #include "oic_malloc.h"
33 #include "cathreadpool.h"
35 #include "uarraylist.h"
36 #include "org_iotivity_ca_CaLeServerInterface.h"
38 #define TAG PCF("CA_LE_SERVER")
40 static JavaVM *g_jvm = NULL;
41 static jobject g_context = NULL;
42 static jobject g_bluetoothGattServer = NULL;
43 static jobject g_bluetoothGattServerCallback = NULL;
44 static jobject g_leAdvertiseCallback = NULL;
46 static CAPacketReceiveCallback g_packetReceiveCallback = NULL;
47 static CABLEErrorHandleCallback g_serverErrorCallback;
49 static u_arraylist_t *g_connectedDeviceList = NULL;
50 static ca_thread_pool_t g_threadPoolHandle = NULL;
52 static bool g_isStartServer = false;
53 static bool g_isInitializedServer = false;
55 static CABLEDataReceivedCallback g_CABLEServerDataReceivedCallback = NULL;
56 static ca_mutex g_bleReqRespCbMutex = NULL;
57 static ca_mutex g_bleClientBDAddressMutex = NULL;
58 static ca_mutex g_connectedDeviceListMutex = NULL;
60 void CALEServerJNISetContext()
62 OIC_LOG(DEBUG, TAG, "CALEServerJNISetContext");
63 g_context = (jobject) CANativeJNIGetContext();
66 void CALeServerJniInit()
68 OIC_LOG(DEBUG, TAG, "CALeServerJniInit");
69 g_jvm = (JavaVM*) CANativeJNIGetJavaVM();
72 CAResult_t CALEServerCreateJniInterfaceObject()
74 OIC_LOG(DEBUG, TAG, "CALEServerCreateJniInterfaceObject");
78 OIC_LOG(ERROR, TAG, "g_context is null");
79 return CA_STATUS_FAILED;
84 OIC_LOG(ERROR, TAG, "g_jvm is null");
85 return CA_STATUS_FAILED;
88 bool isAttached = false;
90 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
93 OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
94 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
98 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
99 return CA_STATUS_FAILED;
104 jclass jni_LEInterface = (*env)->FindClass(env, "org/iotivity/ca/CaLeServerInterface");
105 if (!jni_LEInterface)
107 OIC_LOG(ERROR, TAG, "Could not get CaLeServerInterface class");
111 jmethodID LeInterfaceConstructorMethod = (*env)->GetMethodID(env, jni_LEInterface, "<init>",
113 if (!LeInterfaceConstructorMethod)
115 OIC_LOG(ERROR, TAG, "Could not get CaLeServerInterface constructor method");
119 (*env)->NewObject(env, jni_LEInterface, LeInterfaceConstructorMethod, g_context);
120 OIC_LOG(DEBUG, TAG, "Create instance for CaLeServerInterface");
124 (*g_jvm)->DetachCurrentThread(g_jvm);
133 (*g_jvm)->DetachCurrentThread(g_jvm);
136 return CA_STATUS_FAILED;
139 jobject CALEServerSetResponseData(JNIEnv *env, jbyteArray responseData)
141 OIC_LOG(DEBUG, TAG, "IN - CALEServerSetResponseData");
142 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
143 VERIFY_NON_NULL_RET(responseData, TAG, "responseData is null", NULL);
145 if (!g_bluetoothGattServer)
147 OIC_LOG(ERROR, TAG, "Check BluetoothGattServer status");
151 if (!CALEIsEnableBTAdapter(env))
153 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
157 OIC_LOG(DEBUG, TAG, "CALEServerSetResponseData");
159 jclass jni_cid_bluetoothGattServer = (*env)->FindClass(env,
160 "android/bluetooth/BluetoothGattServer");
161 if (!jni_cid_bluetoothGattServer)
163 OIC_LOG(ERROR, TAG, "jni_cid_bluetoothGattServer is null");
167 jclass jni_cid_bluetoothGattService = (*env)->FindClass(env, "android/bluetooth/"
168 "BluetoothGattService");
169 if (!jni_cid_bluetoothGattService)
171 OIC_LOG(ERROR, TAG, "jni_cid_bluetoothGattService is null");
175 jclass jni_cid_bluetoothGattCharacteristic = (*env)->FindClass(env, "android/bluetooth/"
176 "BluetoothGattCharacteristic");
177 if (!jni_cid_bluetoothGattCharacteristic)
179 OIC_LOG(ERROR, TAG, "jni_cid_bluetoothGattCharacteristic is null");
183 jmethodID jni_mid_getService = (*env)->GetMethodID(env, jni_cid_bluetoothGattServer,
185 "(Ljava/util/UUID;)Landroid/bluetooth/"
186 "BluetoothGattService;");
187 if (!jni_mid_getService)
189 OIC_LOG(ERROR, TAG, "jni_mid_getService is null");
193 jobject jni_obj_serviceUUID = CALEGetUuidFromString(env, OIC_GATT_SERVICE_UUID);
194 if (!jni_obj_serviceUUID)
196 OIC_LOG(ERROR, TAG, "jni_obj_serviceUUID is null");
200 jobject jni_obj_bluetoothGattService = (*env)->CallObjectMethod(env, g_bluetoothGattServer,
202 jni_obj_serviceUUID);
203 if (!jni_obj_bluetoothGattService)
205 OIC_LOG(ERROR, TAG, "jni_obj_bluetoothGattService is null");
209 jmethodID jni_mid_getCharacteristic = (*env)->GetMethodID(env, jni_cid_bluetoothGattService,
212 "Landroid/bluetooth/"
213 "BluetoothGattCharacteristic;");
214 if (!jni_mid_getCharacteristic)
216 OIC_LOG(ERROR, TAG, "jni_mid_getCharacteristic is null");
220 jobject jni_obj_responseUUID = CALEGetUuidFromString(env,
221 OIC_GATT_CHARACTERISTIC_RESPONSE_UUID);
222 if (!jni_obj_responseUUID)
224 OIC_LOG(ERROR, TAG, "jni_obj_responseUUID is null");
228 jobject jni_obj_bluetoothGattCharacteristic = (*env)->CallObjectMethod(
229 env, jni_obj_bluetoothGattService, jni_mid_getCharacteristic, jni_obj_responseUUID);
230 if (!jni_obj_bluetoothGattCharacteristic)
232 OIC_LOG(ERROR, TAG, "jni_obj_bluetoothGattCharacteristic is null");
236 jmethodID jni_mid_setValue = (*env)->GetMethodID(env, jni_cid_bluetoothGattCharacteristic,
237 "setValue", "([B)Z");
238 if (!jni_mid_setValue)
240 OIC_LOG(ERROR, TAG, "jni_mid_setValue is null");
244 jboolean jni_boolean_setValue = (*env)->CallBooleanMethod(env,
245 jni_obj_bluetoothGattCharacteristic,
246 jni_mid_setValue, responseData);
247 if (JNI_FALSE == jni_boolean_setValue)
249 OIC_LOG(ERROR, TAG, "Fail to set response data");
252 OIC_LOG(DEBUG, TAG, "OUT - CALEServerSetResponseData");
253 return jni_obj_bluetoothGattCharacteristic;
256 CAResult_t CALEServerSendResponseData(JNIEnv *env, jobject device, jobject responseData)
258 OIC_LOG(DEBUG, TAG, "IN - CALEServerSendResponseData");
259 VERIFY_NON_NULL(responseData, TAG, "responseData is null");
260 VERIFY_NON_NULL(device, TAG, "device is null");
261 VERIFY_NON_NULL(env, TAG, "env is null");
263 if (!CALEIsEnableBTAdapter(env))
265 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
266 return CA_ADAPTER_NOT_ENABLED;
269 jclass jni_cid_bluetoothGattServer = (*env)->FindClass(env,
270 "android/bluetooth/BluetoothGattServer");
271 if (!jni_cid_bluetoothGattServer)
273 OIC_LOG(ERROR, TAG, "jni_cid_bluetoothGattServer is null");
274 return CA_STATUS_FAILED;
277 jmethodID jni_mid_notifyCharacteristicChanged = (*env)->GetMethodID(
278 env, jni_cid_bluetoothGattServer, "notifyCharacteristicChanged",
279 "(Landroid/bluetooth/BluetoothDevice;"
280 "Landroid/bluetooth/BluetoothGattCharacteristic;Z)Z");
281 if (!jni_mid_notifyCharacteristicChanged)
283 OIC_LOG(ERROR, TAG, "jni_mid_notifyCharacteristicChanged is null");
284 return CA_STATUS_FAILED;
287 jboolean jni_boolean_notifyCharacteristicChanged = (*env)->CallBooleanMethod(
288 env, g_bluetoothGattServer, jni_mid_notifyCharacteristicChanged, device, responseData,
290 if (JNI_FALSE == jni_boolean_notifyCharacteristicChanged)
292 OIC_LOG(ERROR, TAG, "Fail to notify characteristic");
293 return CA_SEND_FAILED;
296 OIC_LOG(DEBUG, TAG, "OUT - CALEServerSendResponseData");
300 CAResult_t CALEServerSendResponse(JNIEnv *env, jobject device, jint requestId, jint status,
301 jint offset, jbyteArray value)
303 OIC_LOG(DEBUG, TAG, "IN - CALEServerSendResponse");
304 VERIFY_NON_NULL(env, TAG, "env is null");
305 VERIFY_NON_NULL(device, TAG, "device is null");
306 VERIFY_NON_NULL(value, TAG, "value is null");
308 OIC_LOG(DEBUG, TAG, "CALEServerSendResponse");
310 if (!CALEIsEnableBTAdapter(env))
312 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
313 return CA_ADAPTER_NOT_ENABLED;
316 jclass jni_cid_bluetoothGattServer = (*env)->FindClass(env,
317 "android/bluetooth/BluetoothGattServer");
318 if (!jni_cid_bluetoothGattServer)
320 OIC_LOG(ERROR, TAG, "jni_cid_bluetoothGattServer is null");
321 return CA_STATUS_FAILED;
324 jmethodID jni_mid_sendResponse = (*env)->GetMethodID(env, jni_cid_bluetoothGattServer,
326 "(Landroid/bluetooth/BluetoothDevice;"
328 if (!jni_mid_sendResponse)
330 OIC_LOG(ERROR, TAG, "jni_mid_sendResponse is null");
331 return CA_STATUS_FAILED;
334 jboolean jni_boolean_sendResponse = (*env)->CallBooleanMethod(env, g_bluetoothGattServer,
335 jni_mid_sendResponse, device,
336 requestId, status, offset,
338 if (JNI_FALSE == jni_boolean_sendResponse)
340 OIC_LOG(ERROR, TAG, "Fail to send response for gatt characteristic write request");
341 return CA_SEND_FAILED;
344 OIC_LOG(DEBUG, TAG, "OUT - CALEServerSendResponse");
348 CAResult_t CALEStartAdvertise()
352 OIC_LOG(ERROR, TAG, "g_jvm is null");
353 return CA_STATUS_FAILED;
356 bool isAttached = false;
358 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
361 OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
362 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
366 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
367 return CA_STATUS_FAILED;
373 CAResult_t ret = CALEServerStartAdvertise(env, g_leAdvertiseCallback);
374 if (CA_STATUS_OK != ret)
376 OIC_LOG(ERROR, TAG, "CALEServerStartAdvertise has failed");
381 (*g_jvm)->DetachCurrentThread(g_jvm);
386 CAResult_t CALEServerStartAdvertise(JNIEnv *env, jobject advertiseCallback)
388 OIC_LOG(DEBUG, TAG, "IN - CALEServerStartAdvertise");
389 VERIFY_NON_NULL(env, TAG, "env is null");
390 VERIFY_NON_NULL(advertiseCallback, TAG, "advertiseCallback is null");
392 if (!CALEIsEnableBTAdapter(env))
394 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
395 return CA_ADAPTER_NOT_ENABLED;
398 jclass jni_cid_AdvertiseSettings = (*env)->FindClass(env,
399 "android/bluetooth/le/"
400 "AdvertiseSettings$Builder");
401 if (!jni_cid_AdvertiseSettings)
403 OIC_LOG(ERROR, TAG, "jni_cid_AdvertiseSettings is null");
404 return CA_STATUS_FAILED;
407 jmethodID jni_mid_AdvertiseSettings = (*env)->GetMethodID(env, jni_cid_AdvertiseSettings,
409 if (!jni_mid_AdvertiseSettings)
411 OIC_LOG(ERROR, TAG, "jni_mid_AdvertiseSettings is null");
412 return CA_STATUS_FAILED;
415 jobject jni_AdvertiseSettings = (*env)->NewObject(env, jni_cid_AdvertiseSettings,
416 jni_mid_AdvertiseSettings);
417 if (!jni_AdvertiseSettings)
419 OIC_LOG(ERROR, TAG, "jni_AdvertiseSettings is null");
420 return CA_STATUS_FAILED;
423 jmethodID jni_mid_setAdvertiseMode = (*env)->GetMethodID(env, jni_cid_AdvertiseSettings,
425 "(I)Landroid/bluetooth/le/"
426 "AdvertiseSettings$Builder;");
427 if (!jni_mid_setAdvertiseMode)
429 OIC_LOG(ERROR, TAG, "jni_mid_setAdvertiseMode is null");
430 return CA_STATUS_FAILED;
433 // 0: Low power, 1: Balanced
434 jobject jni_obj_setAdvertiseMode = (*env)->CallObjectMethod(env, jni_AdvertiseSettings,
435 jni_mid_setAdvertiseMode, 0);
436 if (!jni_obj_setAdvertiseMode)
438 OIC_LOG(ERROR, TAG, "jni_obj_setAdvertiseMode is null");
439 return CA_STATUS_FAILED;
442 jmethodID jni_mid_setConnectable = (*env)->GetMethodID(env, jni_cid_AdvertiseSettings,
444 "(Z)Landroid/bluetooth/le/"
445 "AdvertiseSettings$Builder;");
446 if (!jni_mid_setConnectable)
448 OIC_LOG(ERROR, TAG, "jni_mid_setConnectable is null");
449 return CA_STATUS_FAILED;
452 jobject jni_obj_setConnectable = (*env)->CallObjectMethod(env, jni_AdvertiseSettings,
453 jni_mid_setConnectable, JNI_TRUE);
454 if (!jni_obj_setConnectable)
456 OIC_LOG(ERROR, TAG, "jni_obj_setConnectable is null");
457 return CA_STATUS_FAILED;
460 jmethodID jni_mid_setTimeout = (*env)->GetMethodID(env, jni_cid_AdvertiseSettings, "setTimeout",
461 "(I)Landroid/bluetooth/le/"
462 "AdvertiseSettings$Builder;");
463 if (!jni_mid_setTimeout)
465 OIC_LOG(ERROR, TAG, "jni_mid_setTimeout is null");
466 return CA_STATUS_FAILED;
469 //A value of 0 will disable the time limit
470 jobject jni_obj_setTimeout = (*env)->CallObjectMethod(env, jni_AdvertiseSettings,
471 jni_mid_setTimeout, 0);
472 if (!jni_obj_setTimeout)
474 OIC_LOG(ERROR, TAG, "jni_obj_setTimeout is null");
475 return CA_STATUS_FAILED;
478 jclass jni_cid_AdvertiseDataBuilder = (*env)->FindClass(env,
479 "android/bluetooth/le/"
480 "AdvertiseData$Builder");
481 if (!jni_cid_AdvertiseDataBuilder)
483 OIC_LOG(ERROR, TAG, "jni_cid_AdvertiseDataBuilder is null");
484 return CA_STATUS_FAILED;
487 jmethodID jni_mid_AdvertiseDataBuilder = (*env)->GetMethodID(env, jni_cid_AdvertiseDataBuilder,
489 if (!jni_mid_AdvertiseDataBuilder)
491 OIC_LOG(ERROR, TAG, "jni_mid_AdvertiseDataBuilder is null");
492 return CA_STATUS_FAILED;
495 jobject jni_AdvertiseDataBuilder = (*env)->NewObject(env, jni_cid_AdvertiseDataBuilder,
496 jni_mid_AdvertiseDataBuilder);
497 if (!jni_AdvertiseDataBuilder)
499 OIC_LOG(ERROR, TAG, "jni_AdvertiseDataBuilder is null");
500 return CA_STATUS_FAILED;
503 jobject jni_obj_serviceUUID = CALEGetUuidFromString(env, OIC_GATT_SERVICE_UUID);
504 if (!jni_obj_serviceUUID)
506 OIC_LOG(ERROR, TAG, "jni_obj_serviceUUID is null");
507 return CA_STATUS_FAILED;
510 jobject jni_ParcelUuid = CALEGetParcelUuid(env, jni_obj_serviceUUID);
513 OIC_LOG(ERROR, TAG, "jni_ParcelUuid is null");
514 return CA_STATUS_FAILED;
517 jmethodID jni_mid_addServiceUuid = (*env)->GetMethodID(env, jni_cid_AdvertiseDataBuilder,
519 "(Landroid/os/ParcelUuid;)Landroid/"
520 "bluetooth/le/AdvertiseData$Builder;");
521 if (!jni_mid_addServiceUuid)
523 OIC_LOG(ERROR, TAG, "jni_mid_addServiceUuid is null");
524 return CA_STATUS_FAILED;
527 jobject jni_obj_addServiceUuid = (*env)->CallObjectMethod(env, jni_AdvertiseDataBuilder,
528 jni_mid_addServiceUuid,
530 if (!jni_obj_addServiceUuid)
532 OIC_LOG(ERROR, TAG, "jni_obj_addServiceUuid is null");
533 return CA_STATUS_FAILED;
536 jclass jni_cid_BTAdapter = (*env)->FindClass(env, "android/bluetooth/BluetoothAdapter");
537 if (!jni_cid_BTAdapter)
539 OIC_LOG(ERROR, TAG, "jni_cid_BTAdapter is null");
540 return CA_STATUS_FAILED;
543 jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env, jni_cid_BTAdapter,
545 "()Landroid/bluetooth/"
546 "BluetoothAdapter;");
547 if (!jni_mid_getDefaultAdapter)
549 OIC_LOG(ERROR, TAG, "jni_mid_getDefaultAdapter is null");
550 return CA_STATUS_FAILED;
553 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter,
554 jni_mid_getDefaultAdapter);
555 if (!jni_obj_BTAdapter)
557 OIC_LOG(ERROR, TAG, "jni_obj_BTAdapter is null");
558 return CA_STATUS_FAILED;
561 jmethodID jni_mid_getBluetoothLeAdvertiser = (*env)->GetMethodID(env, jni_cid_BTAdapter,
562 "getBluetoothLeAdvertiser",
563 "()Landroid/bluetooth/le/"
564 "BluetoothLeAdvertiser;");
565 if (!jni_mid_getBluetoothLeAdvertiser)
567 OIC_LOG(ERROR, TAG, "jni_mid_getBluetoothLeAdvertiser is null");
568 return CA_STATUS_FAILED;
571 jobject jni_obj_getBluetoothLeAdvertiser = (*env)->CallObjectMethod(
572 env, jni_obj_BTAdapter, jni_mid_getBluetoothLeAdvertiser);
573 if (!jni_obj_getBluetoothLeAdvertiser)
575 OIC_LOG(ERROR, TAG, "jni_obj_getBluetoothLeAdvertiser is null");
576 return CA_STATUS_FAILED;
579 jmethodID jni_mid_build_LeAdvertiseSettings = (*env)->GetMethodID(env,
580 jni_cid_AdvertiseSettings,
582 "()Landroid/bluetooth/le/"
583 "AdvertiseSettings;");
584 if (!jni_mid_build_LeAdvertiseSettings)
586 OIC_LOG(ERROR, TAG, "jni_mid_build_LeAdvertiseSettings is null");
587 return CA_STATUS_FAILED;
590 jobject jni_obj_build_LeAdvertiseSettings = (*env)->CallObjectMethod(
591 env, jni_AdvertiseSettings, jni_mid_build_LeAdvertiseSettings);
592 if (!jni_obj_build_LeAdvertiseSettings)
594 OIC_LOG(ERROR, TAG, "jni_obj_build_LeAdvertiseSettings is null");
595 return CA_STATUS_FAILED;
598 jmethodID jni_mid_build_LeAdvertiseData = (*env)->GetMethodID(env, jni_cid_AdvertiseDataBuilder,
600 "()Landroid/bluetooth/le/"
602 if (!jni_mid_build_LeAdvertiseData)
604 OIC_LOG(ERROR, TAG, "jni_mid_build_LeAdvertiseData is null");
605 return CA_STATUS_FAILED;
608 jobject jni_obj_build_LeAdvertiseData = (*env)->CallObjectMethod(env, jni_AdvertiseDataBuilder,
609 jni_mid_build_LeAdvertiseData);
610 if (!jni_obj_build_LeAdvertiseData)
612 OIC_LOG(ERROR, TAG, "jni_obj_build_LeAdvertiseData is null");
613 return CA_STATUS_FAILED;
616 jclass jni_cid_leAdvertiser = (*env)->FindClass(env,
617 "android/bluetooth/le/BluetoothLeAdvertiser");
618 if (!jni_cid_leAdvertiser)
620 OIC_LOG(ERROR, TAG, "jni_cid_leAdvertiser is null");
621 return CA_STATUS_FAILED;
624 jmethodID jni_mid_startAdvertising = (*env)->GetMethodID(env, jni_cid_leAdvertiser,
626 "(Landroid/bluetooth/le/"
627 "AdvertiseSettings;Landroid/bluetooth/"
628 "le/AdvertiseData;Landroid/bluetooth/"
629 "le/AdvertiseCallback;)V");
630 if (!jni_mid_startAdvertising)
632 OIC_LOG(ERROR, TAG, "jni_mid_startAdvertising is null");
633 return CA_STATUS_FAILED;
636 (*env)->CallVoidMethod(env, jni_obj_getBluetoothLeAdvertiser, jni_mid_startAdvertising,
637 jni_obj_build_LeAdvertiseSettings, jni_obj_build_LeAdvertiseData,
640 if ((*env)->ExceptionCheck(env))
642 OIC_LOG(ERROR, TAG, "StartAdvertising has failed");
643 (*env)->ExceptionDescribe(env);
644 (*env)->ExceptionClear(env);
645 return CA_STATUS_FAILED;
648 OIC_LOG(DEBUG, TAG, "Advertising started!!");
650 OIC_LOG(DEBUG, TAG, "OUT - CALEServerStartAdvertise");
654 CAResult_t CALEServerStopAdvertise(JNIEnv *env, jobject advertiseCallback)
656 OIC_LOG(DEBUG, TAG, "LEServerStopAdvertise");
657 VERIFY_NON_NULL(env, TAG, "env is null");
658 VERIFY_NON_NULL(advertiseCallback, TAG, "advertiseCallback is null");
660 if (!CALEIsEnableBTAdapter(env))
662 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
663 return CA_ADAPTER_NOT_ENABLED;
666 jclass jni_cid_BTAdapter = (*env)->FindClass(env, "android/bluetooth/BluetoothAdapter");
667 if (!jni_cid_BTAdapter)
669 OIC_LOG(ERROR, TAG, "jni_cid_BTAdapter is null");
670 return CA_STATUS_FAILED;
673 jclass jni_cid_leAdvertiser = (*env)->FindClass(env,
674 "android/bluetooth/le/BluetoothLeAdvertiser");
675 if (!jni_cid_leAdvertiser)
677 OIC_LOG(ERROR, TAG, "jni_cid_leAdvertiser is null");
678 return CA_STATUS_FAILED;
681 jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env, jni_cid_BTAdapter,
683 "()Landroid/bluetooth/"
684 "BluetoothAdapter;");
685 if (!jni_mid_getDefaultAdapter)
687 OIC_LOG(ERROR, TAG, "jni_mid_getDefaultAdapter is null");
688 return CA_STATUS_FAILED;
691 jmethodID jni_mid_getBTLeAdvertiser = (*env)->GetMethodID(env, jni_cid_BTAdapter,
692 "getBluetoothLeAdvertiser",
693 "()Landroid/bluetooth/le/"
694 "BluetoothLeAdvertiser;");
695 if (!jni_mid_getBTLeAdvertiser)
697 OIC_LOG(ERROR, TAG, "jni_mid_getBTLeAdvertiser is null");
698 return CA_STATUS_FAILED;
701 jmethodID jni_mid_stopAdvertising = (*env)->GetMethodID(env, jni_cid_leAdvertiser,
703 "(Landroid/bluetooth/le/"
704 "AdvertiseCallback;)V");
705 if (!jni_mid_stopAdvertising)
707 OIC_LOG(ERROR, TAG, "jni_mid_stopAdvertising is null");
708 return CA_STATUS_FAILED;
711 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter,
712 jni_mid_getDefaultAdapter);
713 if (!jni_obj_BTAdapter)
715 OIC_LOG(ERROR, TAG, "jni_obj_BTAdapter is null");
716 return CA_STATUS_FAILED;
719 jobject jni_obj_getBluetoothLeAdvertiser = (*env)->CallObjectMethod(env, jni_obj_BTAdapter,
720 jni_mid_getBTLeAdvertiser);
721 if (!jni_obj_getBluetoothLeAdvertiser)
723 OIC_LOG(ERROR, TAG, "jni_obj_getBluetoothLeAdvertiser is null");
724 return CA_STATUS_FAILED;
727 (*env)->CallVoidMethod(env, jni_obj_getBluetoothLeAdvertiser, jni_mid_stopAdvertising,
729 if ((*env)->ExceptionCheck(env))
731 OIC_LOG(ERROR, TAG, "getBluetoothLeAdvertiser has failed");
732 (*env)->ExceptionDescribe(env);
733 (*env)->ExceptionClear(env);
734 return CA_STATUS_FAILED;
737 OIC_LOG(DEBUG, TAG, "Advertising stopped!!");
741 CAResult_t CALEServerStartGattServer(JNIEnv *env, jobject gattServerCallback)
743 OIC_LOG(DEBUG, TAG, "IN - CALEServerStartGattServer");
744 VERIFY_NON_NULL(env, TAG, "env is null");
745 VERIFY_NON_NULL(gattServerCallback, TAG, "gattServerCallback is null");
747 if (!CALEIsEnableBTAdapter(env))
749 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
750 return CA_ADAPTER_NOT_ENABLED;
755 OIC_LOG(DEBUG, TAG, "Gatt server already started");
758 g_bluetoothGattServerCallback = (*env)->NewGlobalRef(env, gattServerCallback);
761 jobject bluetoothGattServer = CALEServerOpenGattServer(env);
762 if (!bluetoothGattServer)
764 OIC_LOG(ERROR, TAG, "bluetoothGattServer is null");
765 return CA_STATUS_FAILED;
768 g_bluetoothGattServer = (*env)->NewGlobalRef(env, bluetoothGattServer);
769 if (!g_bluetoothGattServer)
771 OIC_LOG(ERROR, TAG, "g_bluetoothGattServer is null");
772 return CA_STATUS_FAILED;
775 // create gatt service
776 jobject bluetoothGattService = CALEServerCreateGattService(env);
777 if (!bluetoothGattService)
779 OIC_LOG(ERROR, TAG, "bluetoothGattService is null");
780 return CA_STATUS_FAILED;
784 CAResult_t res = CALEServerAddGattService(env, g_bluetoothGattServer,
785 bluetoothGattService);
786 if (CA_STATUS_OK != res)
788 OIC_LOG(ERROR, TAG, "CALEServerAddGattService has failed");
793 jobject CALEServerOpenGattServer(JNIEnv *env)
795 OIC_LOG(DEBUG, TAG, "IN - CALEServerOpenGattServer");
796 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
798 if (!CALEIsEnableBTAdapter(env))
800 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
804 jclass jni_cid_context = (*env)->FindClass(env, "android/content/Context");
805 if (!jni_cid_context)
807 OIC_LOG(ERROR, TAG, "jni_cid_context is null");
811 jclass jni_cid_bluetoothManager = (*env)->FindClass(env, "android/bluetooth/BluetoothManager");
812 if (!jni_cid_bluetoothManager)
814 OIC_LOG(ERROR, TAG, "jni_cid_bluetoothManager is null");
818 jfieldID jni_fid_bluetoothService = (*env)->GetStaticFieldID(env, jni_cid_context,
820 "Ljava/lang/String;");
821 if (!jni_fid_bluetoothService)
823 OIC_LOG(ERROR, TAG, "jni_fid_bluetoothService is null");
827 jmethodID jni_mid_getSystemService = (*env)->GetMethodID(env, jni_cid_context,
829 "(Ljava/lang/String;)"
830 "Ljava/lang/Object;");
831 if (!jni_mid_getSystemService)
833 OIC_LOG(ERROR, TAG, "jni_mid_getSystemService is null");
837 jmethodID jni_mid_openGattServer = (*env)->GetMethodID(env, jni_cid_bluetoothManager,
839 "(Landroid/content/Context;"
840 "Landroid/bluetooth/"
841 "BluetoothGattServerCallback;)"
842 "Landroid/bluetooth/"
843 "BluetoothGattServer;");
844 if (!jni_mid_openGattServer)
846 OIC_LOG(ERROR, TAG, "jni_mid_openGattServer is null");
850 jobject jni_obj_bluetoothService = (*env)->GetStaticObjectField(env, jni_cid_context,
851 jni_fid_bluetoothService);
852 if (!jni_obj_bluetoothService)
854 OIC_LOG(ERROR, TAG, "jni_obj_bluetoothService is null");
858 jobject jni_obj_bluetoothManager = (*env)->CallObjectMethod(env, g_context,
859 jni_mid_getSystemService,
860 jni_obj_bluetoothService);
861 if (!jni_obj_bluetoothManager)
863 OIC_LOG(ERROR, TAG, "jni_obj_bluetoothManager is null");
867 jobject jni_obj_bluetoothGattServer = (*env)->CallObjectMethod(env, jni_obj_bluetoothManager,
868 jni_mid_openGattServer,
870 g_bluetoothGattServerCallback);
871 if (!jni_obj_bluetoothGattServer)
873 OIC_LOG(ERROR, TAG, "jni_obj_bluetoothGattServer is null");
877 OIC_LOG(DEBUG, TAG, "OUT - CALEServerOpenGattServer");
878 return jni_obj_bluetoothGattServer;
881 jobject CALEServerCreateGattService(JNIEnv *env)
883 OIC_LOG(DEBUG, TAG, "IN - CALEServerCreateGattService");
884 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
886 if (!CALEIsEnableBTAdapter(env))
888 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
892 jclass jni_cid_bluetoothGattService = (*env)->FindClass(env, "android/bluetooth/"
893 "BluetoothGattService");
894 if (!jni_cid_bluetoothGattService)
896 OIC_LOG(ERROR, TAG, "jni_cid_bluetoothGattService is null");
900 jclass jni_cid_bluetoothGattCharacteristic = (*env)->FindClass(env, "android/bluetooth/"
901 "BluetoothGattCharacteristic");
902 if (!jni_cid_bluetoothGattCharacteristic)
904 OIC_LOG(ERROR, TAG, "jni_cid_bluetoothGattCharacteristic is null");
908 jfieldID jni_fid_serviceType = (*env)->GetStaticFieldID(env, jni_cid_bluetoothGattService,
909 "SERVICE_TYPE_PRIMARY", "I");
910 if (!jni_fid_serviceType)
912 OIC_LOG(ERROR, TAG, "jni_fid_serviceType is null");
916 jfieldID jni_fid_readProperties = (*env)->GetStaticFieldID(env,
917 jni_cid_bluetoothGattCharacteristic,
918 "PROPERTY_NOTIFY", "I");
919 if (!jni_fid_readProperties)
921 OIC_LOG(ERROR, TAG, "jni_fid_readProperties is null");
925 jfieldID jni_fid_writeProperties = (*env)->GetStaticFieldID(env,
926 jni_cid_bluetoothGattCharacteristic,
927 "PROPERTY_WRITE_NO_RESPONSE", "I");
928 if (!jni_fid_writeProperties)
930 OIC_LOG(ERROR, TAG, "jni_fid_writeProperties is null");
934 jfieldID jni_fid_readPermissions = (*env)->GetStaticFieldID(env,
935 jni_cid_bluetoothGattCharacteristic,
936 "PERMISSION_READ", "I");
937 if (!jni_fid_readPermissions)
939 OIC_LOG(ERROR, TAG, "jni_fid_readPermissions is null");
943 jfieldID jni_fid_writePermissions = (*env)->GetStaticFieldID(
944 env, jni_cid_bluetoothGattCharacteristic, "PERMISSION_WRITE", "I");
945 if (!jni_fid_writePermissions)
947 OIC_LOG(ERROR, TAG, "jni_fid_writePermissions is null");
951 jmethodID jni_mid_bluetoothGattService = (*env)->GetMethodID(env, jni_cid_bluetoothGattService,
952 "<init>", "(Ljava/util/UUID;I)V");
953 if (!jni_mid_bluetoothGattService)
955 OIC_LOG(ERROR, TAG, "jni_mid_bluetoothGattService is null");
959 jmethodID jni_mid_addCharacteristic = (*env)->GetMethodID(env, jni_cid_bluetoothGattService,
961 "(Landroid/bluetooth/"
962 "BluetoothGattCharacteristic;)Z");
963 if (!jni_mid_addCharacteristic)
965 OIC_LOG(ERROR, TAG, "jni_mid_addCharacteristic is null");
969 jmethodID jni_mid_bluetoothGattCharacteristic = (*env)->GetMethodID(
970 env, jni_cid_bluetoothGattCharacteristic, "<init>", "(Ljava/util/UUID;II)V");
971 if (!jni_mid_bluetoothGattCharacteristic)
973 OIC_LOG(ERROR, TAG, "jni_mid_bluetoothGattCharacteristic is null");
977 jobject jni_obj_serviceUUID = CALEGetUuidFromString(env, OIC_GATT_SERVICE_UUID);
978 if (!jni_obj_serviceUUID)
980 OIC_LOG(ERROR, TAG, "jni_obj_serviceUUID is null");
984 jint jni_int_serviceType = (*env)->GetStaticIntField(env, jni_cid_bluetoothGattService,
985 jni_fid_serviceType);
986 jobject jni_bluetoothGattService = (*env)->NewObject(env, jni_cid_bluetoothGattService,
987 jni_mid_bluetoothGattService,
988 jni_obj_serviceUUID, jni_int_serviceType);
989 if (!jni_bluetoothGattService)
991 OIC_LOG(ERROR, TAG, "jni_bluetoothGattService is null");
995 jobject jni_obj_readUuid = CALEGetUuidFromString(env, OIC_GATT_CHARACTERISTIC_RESPONSE_UUID);
996 if (!jni_obj_readUuid)
998 OIC_LOG(ERROR, TAG, "jni_obj_readUuid is null");
1002 jint jni_int_readProperties = (*env)->GetStaticIntField(env,
1003 jni_cid_bluetoothGattCharacteristic,
1004 jni_fid_readProperties);
1006 jint jni_int_readPermissions = (*env)->GetStaticIntField(env,
1007 jni_cid_bluetoothGattCharacteristic,
1008 jni_fid_readPermissions);
1010 jint jni_int_writePermissions = (*env)->GetStaticIntField(env,
1011 jni_cid_bluetoothGattCharacteristic,
1012 jni_fid_writePermissions);
1014 jobject jni_readCharacteristic = (*env)->NewObject(env, jni_cid_bluetoothGattCharacteristic,
1015 jni_mid_bluetoothGattCharacteristic,
1016 jni_obj_readUuid, jni_int_readProperties,
1017 jni_int_readPermissions|
1018 jni_int_writePermissions);
1019 if (!jni_readCharacteristic)
1021 OIC_LOG(ERROR, TAG, "jni_readCharacteristic is null");
1025 jboolean jni_boolean_addReadCharacteristic = (*env)->CallBooleanMethod(
1026 env, jni_bluetoothGattService, jni_mid_addCharacteristic, jni_readCharacteristic);
1027 if (!jni_boolean_addReadCharacteristic)
1029 OIC_LOG(ERROR, TAG, "jni_boolean_addReadCharacteristic is null");
1033 jobject jni_obj_writeUuid = CALEGetUuidFromString(env, OIC_GATT_CHARACTERISTIC_REQUEST_UUID);
1034 if (!jni_obj_writeUuid)
1036 OIC_LOG(ERROR, TAG, "jni_obj_bluetoothGattServer is null");
1040 jint jni_int_writeProperties = (*env)->GetStaticIntField(env,
1041 jni_cid_bluetoothGattCharacteristic,
1042 jni_fid_writeProperties);
1044 jobject jni_writeCharacteristic = (*env)->NewObject(env, jni_cid_bluetoothGattCharacteristic,
1045 jni_mid_bluetoothGattCharacteristic,
1046 jni_obj_writeUuid, jni_int_writeProperties,
1047 jni_int_writePermissions);
1048 if (!jni_writeCharacteristic)
1050 OIC_LOG(ERROR, TAG, "jni_writeCharacteristic is null");
1054 jboolean jni_boolean_addWriteCharacteristic = (*env)->CallBooleanMethod(
1055 env, jni_bluetoothGattService, jni_mid_addCharacteristic, jni_writeCharacteristic);
1056 if (JNI_FALSE == jni_boolean_addWriteCharacteristic)
1058 OIC_LOG(ERROR, TAG, "Fail to add jni_boolean_addReadCharacteristic");
1062 OIC_LOG(DEBUG, TAG, "OUT - CALEServerCreateGattService");
1063 return jni_bluetoothGattService;
1066 CAResult_t CALEServerAddDescriptor(JNIEnv *env, jobject characteristic)
1068 OIC_LOG(DEBUG, TAG, "CALEServerAddDescriptor");
1069 VERIFY_NON_NULL(env, TAG, "env is null");
1070 VERIFY_NON_NULL(characteristic, TAG, "characteristic is null");
1072 jclass jni_cid_bluetoothGattDescriptor = (*env)->FindClass(env, "android/bluetooth/"
1073 "BluetoothGattDescriptor");
1074 if (!jni_cid_bluetoothGattDescriptor)
1076 OIC_LOG(ERROR, TAG, "jni_cid_bluetoothGattDescriptor is null");
1077 return CA_STATUS_FAILED;
1080 jmethodID jni_mid_bluetoothGattDescriptor = (*env)->GetMethodID(env,
1081 jni_cid_bluetoothGattDescriptor,
1083 "(Ljava/util/UUID;I)V");
1084 if (!jni_mid_bluetoothGattDescriptor)
1086 OIC_LOG(ERROR, TAG, "jni_mid_bluetoothGattDescriptor is null");
1087 return CA_STATUS_FAILED;
1090 jfieldID jni_fid_readPermissions = (*env)->GetStaticFieldID(env,
1091 jni_cid_bluetoothGattDescriptor,
1092 "PERMISSION_READ", "I");
1093 if (!jni_fid_readPermissions)
1095 OIC_LOG(ERROR, TAG, "jni_fid_readPermissions is null");
1096 return CA_STATUS_FAILED;
1099 jobject jni_obj_readUuid = CALEGetUuidFromString(env, OIC_GATT_CHARACTERISTIC_CONFIG_UUID);
1100 if (!jni_obj_readUuid)
1102 OIC_LOG(ERROR, TAG, "jni_obj_readUuid is null");
1103 return CA_STATUS_FAILED;
1106 jint jni_int_readPermissions = (*env)->GetStaticIntField(env, jni_cid_bluetoothGattDescriptor,
1107 jni_fid_readPermissions);
1109 OIC_LOG(DEBUG, TAG, "initialize new Descriptor");
1111 jobject jni_readDescriptor = (*env)->NewObject(env, jni_cid_bluetoothGattDescriptor,
1112 jni_mid_bluetoothGattDescriptor,
1113 jni_obj_readUuid, jni_int_readPermissions);
1114 if (!jni_readDescriptor)
1116 OIC_LOG(ERROR, TAG, "jni_readDescriptor is null");
1117 return CA_STATUS_FAILED;
1120 jclass jni_cid_GattCharacteristic = (*env)->FindClass(env, "android/bluetooth/"
1121 "BluetoothGattCharacteristic");
1122 if (!jni_cid_GattCharacteristic)
1124 OIC_LOG(ERROR, TAG, "jni_cid_GattCharacteristic is null");
1125 return CA_STATUS_FAILED;
1128 jmethodID jni_mid_addDescriptor = (*env)->GetMethodID(env, jni_cid_GattCharacteristic,
1130 "(Landroid/bluetooth/"
1131 "BluetoothGattDescriptor;)Z");
1132 if (!jni_mid_addDescriptor)
1134 OIC_LOG(ERROR, TAG, "jni_mid_addDescriptor is null");
1135 return CA_STATUS_FAILED;
1138 jboolean jni_boolean_addDescriptor = (*env)->CallBooleanMethod(env, characteristic,
1139 jni_mid_addDescriptor,
1140 jni_readDescriptor);
1142 if (JNI_FALSE == jni_boolean_addDescriptor)
1144 OIC_LOG(ERROR, TAG, "addDescriptor has failed");
1145 return CA_STATUS_FAILED;
1149 OIC_LOG(DEBUG, TAG, "addDescriptor success");
1151 return CA_STATUS_OK;
1154 CAResult_t CALEServerAddGattService(JNIEnv *env, jobject bluetoothGattServer,
1155 jobject bluetoothGattService)
1157 OIC_LOG(DEBUG, TAG, "IN - CALEServerAddGattService");
1158 VERIFY_NON_NULL(env, TAG, "env is null");
1159 VERIFY_NON_NULL(bluetoothGattServer, TAG, "bluetoothGattServer is null");
1160 VERIFY_NON_NULL(bluetoothGattService, TAG, "bluetoothGattService is null");
1162 if (!CALEIsEnableBTAdapter(env))
1164 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1165 return CA_ADAPTER_NOT_ENABLED;
1168 jclass jni_cid_bluetoothGattServer = (*env)->FindClass(env,
1169 "android/bluetooth/BluetoothGattServer");
1170 if (!jni_cid_bluetoothGattServer)
1172 OIC_LOG(ERROR, TAG, "jni_cid_bluetoothGattServer is null");
1173 return CA_STATUS_FAILED;
1176 jmethodID jni_mid_addService = (*env)->GetMethodID(env, jni_cid_bluetoothGattServer,
1178 "(Landroid/bluetooth/BluetoothGattService;)"
1180 if (!jni_mid_addService)
1182 OIC_LOG(ERROR, TAG, "jni_mid_addService is null");
1183 return CA_STATUS_FAILED;
1186 jboolean jni_boolean_addService = (*env)->CallBooleanMethod(env, bluetoothGattServer,
1188 bluetoothGattService);
1190 if (JNI_FALSE == jni_boolean_addService)
1192 OIC_LOG(ERROR, TAG, "Fail to add GATT service");
1193 return CA_STATUS_FAILED;
1196 OIC_LOG(DEBUG, TAG, "OUT - CALEServerAddGattService");
1197 return CA_STATUS_OK;
1200 CAResult_t CALEServerConnect(JNIEnv *env, jobject bluetoothDevice)
1202 OIC_LOG(DEBUG, TAG, "IN - CALEServerConnect");
1203 VERIFY_NON_NULL(env, TAG, "env is null");
1204 VERIFY_NON_NULL(bluetoothDevice, TAG, "bluetoothDevice is null");
1206 if (!CALEIsEnableBTAdapter(env))
1208 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1209 return CA_ADAPTER_NOT_ENABLED;
1212 jclass jni_cid_bluetoothGattServer = (*env)->FindClass(env,
1213 "android/bluetooth/BluetoothGattServer");
1214 if (!jni_cid_bluetoothGattServer)
1216 OIC_LOG(ERROR, TAG, "jni_cid_bluetoothGattServer is null");
1217 return CA_STATUS_FAILED;
1220 jmethodID jni_mid_connect = (*env)->GetMethodID(env, jni_cid_bluetoothGattServer, "connect",
1221 "(Landroid/bluetooth/BluetoothDevice;Z)Z");
1222 if (!jni_mid_connect)
1224 OIC_LOG(ERROR, TAG, "jni_mid_connect is null");
1225 return CA_STATUS_FAILED;
1228 jboolean jni_boolean_connect = (*env)->CallBooleanMethod(env, g_bluetoothGattServer,
1229 jni_mid_connect, bluetoothDevice,
1231 if (JNI_FALSE == jni_boolean_connect)
1233 OIC_LOG(ERROR, TAG, "Fail to connect");
1234 return CA_STATUS_FAILED;
1237 OIC_LOG(DEBUG, TAG, "OUT - CALEServerConnect");
1238 return CA_STATUS_OK;
1241 CAResult_t CALEServerDisconnectAllDevices(JNIEnv *env)
1243 OIC_LOG(DEBUG, TAG, "IN - CALEServerDisconnectAllDevices");
1244 VERIFY_NON_NULL(env, TAG, "env is null");
1246 ca_mutex_lock(g_connectedDeviceListMutex);
1247 if (!g_connectedDeviceList)
1249 OIC_LOG(ERROR, TAG, "g_connectedDeviceList is null");
1250 ca_mutex_unlock(g_connectedDeviceListMutex);
1251 return CA_STATUS_FAILED;
1254 uint32_t length = u_arraylist_length(g_connectedDeviceList);
1255 for (uint32_t index = 0; index < length; index++)
1257 jobject jarrayObj = (jobject) u_arraylist_get(g_connectedDeviceList, index);
1260 OIC_LOG_V(ERROR, TAG, "object[%d] is null", index);
1264 // disconnect for device obj
1265 CAResult_t res = CALEServerDisconnect(env, jarrayObj);
1266 if (CA_STATUS_OK != res)
1268 OIC_LOG_V(ERROR, TAG, "Disconnect for this device[%d] has failed", index);
1273 ca_mutex_unlock(g_connectedDeviceListMutex);
1274 OIC_LOG(DEBUG, TAG, "OUT - CALEServerDisconnectAllDevices");
1275 return CA_STATUS_OK;
1278 CAResult_t CALEServerDisconnect(JNIEnv *env, jobject bluetoothDevice)
1280 OIC_LOG(DEBUG, TAG, "IN - CALEServerDisconnect");
1281 VERIFY_NON_NULL(env, TAG, "env is null");
1282 VERIFY_NON_NULL(bluetoothDevice, TAG, "bluetoothDevice is null");
1284 if (!CALEIsEnableBTAdapter(env))
1286 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1287 return CA_ADAPTER_NOT_ENABLED;
1290 jclass jni_cid_bluetoothGattServer = (*env)->FindClass(env,
1291 "android/bluetooth/BluetoothGattServer");
1292 if (!jni_cid_bluetoothGattServer)
1294 OIC_LOG(ERROR, TAG, "jni_cid_bluetoothGattServer is null");
1295 return CA_STATUS_FAILED;
1298 jmethodID jni_mid_cancelConnection = (*env)->GetMethodID(env, jni_cid_bluetoothGattServer,
1300 "(Landroid/bluetooth/BluetoothDevice;)"
1302 if (!jni_mid_cancelConnection)
1304 OIC_LOG(ERROR, TAG, "jni_mid_cancelConnection is null");
1305 return CA_STATUS_FAILED;
1308 (*env)->CallVoidMethod(env, g_bluetoothGattServer, jni_mid_cancelConnection, bluetoothDevice);
1310 if ((*env)->ExceptionCheck(env))
1312 OIC_LOG(ERROR, TAG, "cancelConnection has failed");
1313 (*env)->ExceptionDescribe(env);
1314 (*env)->ExceptionClear(env);
1315 return CA_STATUS_FAILED;
1318 OIC_LOG(DEBUG, TAG, "OUT - CALEServerDisconnect");
1319 return CA_STATUS_OK;
1322 CAResult_t CALEServerGattClose(JNIEnv *env, jobject bluetoothGattServer)
1325 OIC_LOG(DEBUG, TAG, "GattServer Close");
1326 VERIFY_NON_NULL(bluetoothGattServer, TAG, "bluetoothGattServer is null");
1327 VERIFY_NON_NULL(env, TAG, "env is null");
1329 // get BluetoothGatt class
1330 OIC_LOG(DEBUG, TAG, "get BluetoothGatt class");
1331 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, "android/bluetooth/BluetoothGattServer");
1332 if (!jni_cid_BluetoothGatt)
1334 OIC_LOG(ERROR, TAG, "jni_cid_BluetoothGatt is null");
1335 return CA_STATUS_FAILED;
1338 jmethodID jni_mid_closeGatt = (*env)->GetMethodID(env, jni_cid_BluetoothGatt, "close", "()V");
1339 if (!jni_mid_closeGatt)
1341 OIC_LOG(ERROR, TAG, "jni_mid_closeGatt is null");
1342 return CA_STATUS_OK;
1345 // call disconnect gatt method
1346 OIC_LOG(DEBUG, TAG, "request to close GATT");
1347 (*env)->CallVoidMethod(env, bluetoothGattServer, jni_mid_closeGatt);
1349 if ((*env)->ExceptionCheck(env))
1351 OIC_LOG(ERROR, TAG, "closeGATT has failed");
1352 (*env)->ExceptionDescribe(env);
1353 (*env)->ExceptionClear(env);
1354 return CA_STATUS_FAILED;
1357 return CA_STATUS_OK;
1360 CAResult_t CALEServerSend(JNIEnv *env, jobject bluetoothDevice, jbyteArray responseData)
1362 OIC_LOG(DEBUG, TAG, "IN - CALEServerSend");
1363 VERIFY_NON_NULL(env, TAG, "env is null");
1364 VERIFY_NON_NULL(bluetoothDevice, TAG, "bluetoothDevice is null");
1365 VERIFY_NON_NULL(responseData, TAG, "responseData is null");
1367 if (!CALEIsEnableBTAdapter(env))
1369 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1370 return CA_ADAPTER_NOT_ENABLED;
1373 jobject responseChar = CALEServerSetResponseData(env, responseData);
1376 OIC_LOG(ERROR, TAG, "responseChar is null");
1377 return CA_STATUS_FAILED;
1380 CAResult_t result = CALEServerSendResponseData(env, bluetoothDevice, responseChar);
1381 if (CA_STATUS_OK != result)
1383 OIC_LOG(ERROR, TAG, "Fail to send response data");
1387 OIC_LOG(DEBUG, TAG, "OUT - CALEServerSend");
1391 CAResult_t CALEServerInitialize(ca_thread_pool_t handle)
1393 OIC_LOG(DEBUG, TAG, "IN - CALEServerInitialize");
1395 CALeServerJniInit();
1399 OIC_LOG(ERROR, TAG, "g_jvm is null");
1400 return CA_STATUS_FAILED;
1403 bool isAttached = false;
1405 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
1408 OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
1409 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
1413 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
1414 return CA_STATUS_FAILED;
1419 CAResult_t ret = CALECheckPlatformVersion(env, 21);
1420 if (CA_STATUS_OK != ret)
1422 OIC_LOG(ERROR, TAG, "it is not supported");
1426 (*g_jvm)->DetachCurrentThread(g_jvm);
1431 g_threadPoolHandle = handle;
1433 ret = CALEServerInitMutexVaraibles();
1434 if (CA_STATUS_OK != ret)
1436 OIC_LOG(ERROR, TAG, "CALEServerInitMutexVaraibles has failed");
1440 (*g_jvm)->DetachCurrentThread(g_jvm);
1442 return CA_STATUS_FAILED;
1445 CALEServerJNISetContext();
1446 CALEServerCreateCachedDeviceList();
1448 ret = CALEServerCreateJniInterfaceObject();
1449 if (CA_STATUS_OK != ret)
1451 OIC_LOG(ERROR, TAG, "CALEServerCreateJniInterfaceObject has failed");
1455 (*g_jvm)->DetachCurrentThread(g_jvm);
1457 return CA_STATUS_FAILED;
1462 (*g_jvm)->DetachCurrentThread(g_jvm);
1465 g_isInitializedServer = true;
1466 OIC_LOG(DEBUG, TAG, "OUT - CALEServerInitialize");
1467 return CA_STATUS_OK;
1470 void CALEServerTerminate()
1472 OIC_LOG(DEBUG, TAG, "IN - CALEServerTerminate");
1476 OIC_LOG(ERROR, TAG, "g_jvm is null");
1480 bool isAttached = false;
1482 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
1485 OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
1486 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
1490 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
1496 CAResult_t ret = CALEServerGattClose(env, g_bluetoothGattServer);
1497 if (CA_STATUS_OK != ret)
1499 OIC_LOG(ERROR, TAG, "CALEServerGattClose has failed");
1502 ret = CALEServerStopMulticastServer(0);
1503 if (CA_STATUS_OK != ret)
1505 OIC_LOG(ERROR, TAG, "CALEServerStopMulticastServer has failed");
1508 ret = CALEServerDisconnectAllDevices(env);
1509 if (CA_STATUS_OK != ret)
1511 OIC_LOG(ERROR, TAG, "CALEServerDisconnectAllDevices has failed");
1514 ret = CALEServerRemoveAllDevices(env);
1515 if (CA_STATUS_OK != ret)
1517 OIC_LOG(ERROR, TAG, "CALEServerRemoveAllDevices has failed");
1520 if (g_leAdvertiseCallback)
1522 (*env)->DeleteGlobalRef(env, g_leAdvertiseCallback);
1525 if (g_bluetoothGattServer)
1527 (*env)->DeleteGlobalRef(env, g_bluetoothGattServer);
1530 if (g_bluetoothGattServerCallback)
1532 (*env)->DeleteGlobalRef(env, g_bluetoothGattServerCallback);
1535 CALEServerTerminateMutexVaraibles();
1536 CALEServerTerminateConditionVaraibles();
1538 g_isStartServer = false;
1539 g_isInitializedServer = false;
1543 (*g_jvm)->DetachCurrentThread(g_jvm);
1546 OIC_LOG(DEBUG, TAG, "OUT - CALEServerTerminate");
1549 CAResult_t CALEServerSendUnicastMessage(const char* address, const uint8_t* data, uint32_t dataLen)
1551 OIC_LOG_V(DEBUG, TAG, "CALEServerSendUnicastMessage(%s, %p)", address, data);
1552 VERIFY_NON_NULL(address, TAG, "address is null");
1553 VERIFY_NON_NULL(data, TAG, "data is null");
1557 OIC_LOG(ERROR, TAG, "g_jvm is null");
1558 return CA_STATUS_FAILED;
1561 bool isAttached = false;
1563 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
1566 OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
1567 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
1571 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
1572 return CA_STATUS_FAILED;
1577 CAResult_t ret = CALEServerSendUnicastMessageImpl(env, address, data, dataLen);
1578 if (CA_STATUS_OK != ret)
1580 OIC_LOG(ERROR, TAG, "CALEServerSendUnicastMessageImpl has failed");
1585 (*g_jvm)->DetachCurrentThread(g_jvm);
1591 CAResult_t CALEServerSendMulticastMessage(const uint8_t* data, uint32_t dataLen)
1593 OIC_LOG_V(DEBUG, TAG, "CALEServerSendMulticastMessage(%p)", data);
1594 VERIFY_NON_NULL(data, TAG, "data is null");
1598 OIC_LOG(ERROR, TAG, "g_jvm is null");
1599 return CA_STATUS_FAILED;
1602 bool isAttached = false;
1604 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
1607 OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
1608 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
1612 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
1613 return CA_STATUS_FAILED;
1618 CAResult_t ret = CALEServerSendMulticastMessageImpl(env, data, dataLen);
1619 if (CA_STATUS_OK != ret)
1621 OIC_LOG(ERROR, TAG, "CALEServerSendMulticastMessageImpl has failed");
1626 (*g_jvm)->DetachCurrentThread(g_jvm);
1632 CAResult_t CALEServerStartMulticastServer()
1634 OIC_LOG(DEBUG, TAG, "IN - CALEServerStartMulticastServer");
1636 if (!g_isInitializedServer)
1638 OIC_LOG(INFO, TAG, "server is not initialized");
1639 return CA_STATUS_FAILED;
1642 if (g_isStartServer)
1644 OIC_LOG(INFO, TAG, "server is already started..it will be skipped");
1645 return CA_STATUS_FAILED;
1650 OIC_LOG(ERROR, TAG, "g_jvm is null");
1651 return CA_STATUS_FAILED;
1654 bool isAttached = false;
1656 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
1659 OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
1660 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
1664 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
1665 return CA_STATUS_FAILED;
1670 g_isStartServer = true;
1672 // start gatt server
1673 CAResult_t ret = CALEServerStartGattServer(env, g_bluetoothGattServerCallback);
1674 if (CA_STATUS_OK != ret)
1676 OIC_LOG(ERROR, TAG, "Fail to start gatt server");
1681 ret = CALEServerStartAdvertise(env, g_leAdvertiseCallback);
1682 if (CA_STATUS_OK != ret)
1684 OIC_LOG(ERROR, TAG, "CALEServerStartAdvertise has failed");
1689 (*g_jvm)->DetachCurrentThread(g_jvm);
1692 OIC_LOG(DEBUG, TAG, "OUT - CALEServerStartMulticastServer");
1696 CAResult_t CALEServerStopMulticastServer()
1698 OIC_LOG(DEBUG, TAG, "IN - CALEServerStopMulticastServer");
1700 if (false == g_isStartServer)
1702 OIC_LOG(INFO, TAG, "server is already stopped..it will be skipped");
1703 return CA_STATUS_FAILED;
1708 OIC_LOG(ERROR, TAG, "g_jvm is null");
1709 return CA_STATUS_FAILED;
1712 bool isAttached = false;
1714 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
1717 OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
1718 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
1722 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
1723 return CA_STATUS_FAILED;
1728 CAResult_t ret = CALEServerStopAdvertise(env, g_leAdvertiseCallback);
1729 if (CA_STATUS_OK != ret)
1731 OIC_LOG(ERROR, TAG, "CALEServerStopAdvertise has failed");
1734 g_isStartServer = false;
1738 (*g_jvm)->DetachCurrentThread(g_jvm);
1741 OIC_LOG(DEBUG, TAG, "OUT - CALEServerStopMulticastServer");
1745 void CALEServerSetCallback(CAPacketReceiveCallback callback)
1747 OIC_LOG(DEBUG, TAG, "CALEServerSetCallback");
1748 g_packetReceiveCallback = callback;
1751 CAResult_t CALEServerSendUnicastMessageImpl(JNIEnv *env, const char* address, const uint8_t* data,
1754 OIC_LOG_V(DEBUG, TAG, "CALEServerSendUnicastMessageImpl, address: %s, data: %p",
1756 VERIFY_NON_NULL(env, TAG, "env is null");
1757 VERIFY_NON_NULL(address, TAG, "address is null");
1758 VERIFY_NON_NULL(data, TAG, "data is null");
1760 if (!g_connectedDeviceList)
1762 OIC_LOG(ERROR, TAG, "g_connectedDeviceList is null");
1763 return CA_STATUS_FAILED;
1766 jobject jni_obj_bluetoothDevice = NULL;
1767 uint32_t length = u_arraylist_length(g_connectedDeviceList);
1768 for (uint32_t index = 0; index < length; index++)
1770 OIC_LOG(DEBUG, TAG, "check device address");
1771 jobject jarrayObj = (jobject) u_arraylist_get(g_connectedDeviceList, index);
1774 OIC_LOG(ERROR, TAG, "jarrayObj is null");
1775 return CA_STATUS_FAILED;
1778 jstring jni_setAddress = CALEGetAddressFromBTDevice(env, jarrayObj);
1779 if (!jni_setAddress)
1781 OIC_LOG(ERROR, TAG, "jni_setAddress is null");
1782 return CA_STATUS_FAILED;
1784 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
1787 OIC_LOG(ERROR, TAG, "setAddress is null");
1788 return CA_STATUS_FAILED;
1791 OIC_LOG_V(DEBUG, TAG, "setAddress : %s", setAddress);
1792 OIC_LOG_V(DEBUG, TAG, "address : %s", address);
1794 if (!strcmp(setAddress, address))
1796 OIC_LOG(DEBUG, TAG, "found the device");
1797 jni_obj_bluetoothDevice = jarrayObj;
1798 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
1801 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
1804 if (jni_obj_bluetoothDevice)
1806 jbyteArray jni_bytearr_data = (*env)->NewByteArray(env, dataLen);
1807 (*env)->SetByteArrayRegion(env, jni_bytearr_data, 0, dataLen, (jbyte*) data);
1809 CAResult_t res = CALEServerSend(env, jni_obj_bluetoothDevice, jni_bytearr_data);
1810 if (CA_STATUS_OK != res)
1812 OIC_LOG(ERROR, TAG, "send has failed");
1813 return CA_SEND_FAILED;
1818 OIC_LOG(ERROR, TAG, "There are no device to send in the list");
1819 return CA_STATUS_FAILED;
1822 return CA_STATUS_OK;
1825 CAResult_t CALEServerSendMulticastMessageImpl(JNIEnv *env, const uint8_t *data, uint32_t dataLen)
1827 OIC_LOG_V(DEBUG, TAG, "CALEServerSendMulticastMessageImpl, send to, data: %s", data);
1828 VERIFY_NON_NULL(env, TAG, "env is null");
1829 VERIFY_NON_NULL(data, TAG, "data is null");
1831 if (!g_connectedDeviceList)
1833 OIC_LOG(ERROR, TAG, "g_connectedDeviceList is null");
1834 return CA_STATUS_FAILED;
1837 uint32_t length = u_arraylist_length(g_connectedDeviceList);
1838 for (uint32_t index = 0; index < length; index++)
1840 jobject jarrayObj = (jobject) u_arraylist_get(g_connectedDeviceList, index);
1843 OIC_LOG(ERROR, TAG, "jarrayObj is null");
1844 return CA_STATUS_FAILED;
1847 // send data for all device
1848 jbyteArray jni_bytearr_data = (*env)->NewByteArray(env, dataLen);
1849 (*env)->SetByteArrayRegion(env, jni_bytearr_data, 0, dataLen, (jbyte*) data);
1850 CAResult_t res = CALEServerSend(env, jarrayObj, jni_bytearr_data);
1851 if (CA_STATUS_OK != res)
1853 OIC_LOG(ERROR, TAG, "send has failed");
1854 return CA_SEND_FAILED;
1858 return CA_STATUS_OK;
1861 void CALEServerCreateCachedDeviceList()
1863 ca_mutex_lock(g_connectedDeviceListMutex);
1864 // create new object array
1865 if (!g_connectedDeviceList)
1867 OIC_LOG(DEBUG, TAG, "Create device list");
1868 g_connectedDeviceList = u_arraylist_create();
1870 ca_mutex_unlock(g_connectedDeviceListMutex);
1873 bool CALEServerIsDeviceInList(JNIEnv *env, const char* remoteAddress)
1875 VERIFY_NON_NULL_RET(env, TAG, "env is null", false);
1876 VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", false);
1878 if (!g_connectedDeviceList)
1880 OIC_LOG(ERROR, TAG, "list is null");
1884 uint32_t length = u_arraylist_length(g_connectedDeviceList);
1885 for (uint32_t index = 0; index < length; index++)
1887 jobject jarrayObj = (jobject) u_arraylist_get(g_connectedDeviceList, index);
1891 OIC_LOG(ERROR, TAG, "jarrayObj is null");
1895 jstring jni_setAddress = CALEGetAddressFromBTDevice(env, jarrayObj);
1896 if (!jni_setAddress)
1898 OIC_LOG(ERROR, TAG, "jni_setAddress is null");
1902 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
1905 OIC_LOG(ERROR, TAG, "setAddress is null");
1909 if (!strcmp(remoteAddress, setAddress))
1911 OIC_LOG(ERROR, TAG, "the device is already set");
1912 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
1917 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
1922 OIC_LOG(DEBUG, TAG, "there are no device in the list");
1926 CAResult_t CALEServerAddDeviceToList(JNIEnv *env, jobject device)
1928 OIC_LOG(DEBUG, TAG, "IN - CALEServerAddDeviceToList");
1929 VERIFY_NON_NULL(device, TAG, "device is null");
1930 VERIFY_NON_NULL(env, TAG, "env is null");
1932 ca_mutex_lock(g_connectedDeviceListMutex);
1934 if (!g_connectedDeviceList)
1936 OIC_LOG(ERROR, TAG, "list is null");
1937 ca_mutex_unlock(g_connectedDeviceListMutex);
1938 return CA_STATUS_FAILED;
1941 jstring jni_remoteAddress = CALEGetAddressFromBTDevice(env, device);
1942 if (!jni_remoteAddress)
1944 OIC_LOG(ERROR, TAG, "jni_remoteAddress is null");
1945 ca_mutex_unlock(g_connectedDeviceListMutex);
1946 return CA_STATUS_FAILED;
1949 const char* remoteAddress = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);
1952 OIC_LOG(ERROR, TAG, "remoteAddress is null");
1953 ca_mutex_unlock(g_connectedDeviceListMutex);
1954 return CA_STATUS_FAILED;
1957 if (false == CALEServerIsDeviceInList(env, remoteAddress))
1959 jobject jni_obj_device = (*env)->NewGlobalRef(env, device);
1960 u_arraylist_add(g_connectedDeviceList, jni_obj_device);
1961 OIC_LOG_V(DEBUG, TAG, "Set the object to ArrayList as Element : %s", remoteAddress);
1964 (*env)->ReleaseStringUTFChars(env, jni_remoteAddress, remoteAddress);
1965 ca_mutex_unlock(g_connectedDeviceListMutex);
1966 OIC_LOG(DEBUG, TAG, "OUT - CALEServerAddDeviceToList");
1967 return CA_STATUS_OK;
1970 CAResult_t CALEServerRemoveAllDevices(JNIEnv *env)
1972 OIC_LOG(DEBUG, TAG, "IN - CALEServerRemoveAllDevices");
1973 VERIFY_NON_NULL(env, TAG, "env is null");
1975 ca_mutex_lock(g_connectedDeviceListMutex);
1976 if (!g_connectedDeviceList)
1978 OIC_LOG(ERROR, TAG, "g_connectedDeviceList is null");
1979 ca_mutex_unlock(g_connectedDeviceListMutex);
1980 return CA_STATUS_FAILED;
1983 uint32_t length = u_arraylist_length(g_connectedDeviceList);
1984 for (uint32_t index = 0; index < length; index++)
1986 jobject jarrayObj = (jobject) u_arraylist_get(g_connectedDeviceList, index);
1989 (*env)->DeleteGlobalRef(env, jarrayObj);
1993 OICFree(g_connectedDeviceList);
1994 g_connectedDeviceList = NULL;
1995 ca_mutex_unlock(g_connectedDeviceListMutex);
1997 OIC_LOG(DEBUG, TAG, "OUT - CALEServerRemoveAllDevices");
1998 return CA_STATUS_OK;
2001 CAResult_t CALEServerRemoveDevice(JNIEnv *env, jstring address)
2003 OIC_LOG(DEBUG, TAG, "IN CALEServerRemoveDevice");
2004 VERIFY_NON_NULL(env, TAG, "env is null");
2005 VERIFY_NON_NULL(address, TAG, "address is null");
2007 ca_mutex_lock(g_connectedDeviceListMutex);
2008 if (!g_connectedDeviceList)
2010 OIC_LOG(ERROR, TAG, "no deviceList");
2011 ca_mutex_unlock(g_connectedDeviceListMutex);
2012 return CA_STATUS_FAILED;
2015 uint32_t length = u_arraylist_length(g_connectedDeviceList);
2016 for (uint32_t index = 0; index < length; index++)
2018 jobject jarrayObj = (jobject) u_arraylist_get(g_connectedDeviceList, index);
2022 jstring jni_setAddress = CALEGetAddressFromBTDevice(env, jarrayObj);
2023 if (!jni_setAddress)
2025 OIC_LOG(ERROR, TAG, "wrong device address");
2028 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
2031 OIC_LOG(ERROR, TAG, "setAddress is null");
2035 const char* remoteAddress = (*env)->GetStringUTFChars(env, address, NULL);
2038 OIC_LOG(ERROR, TAG, "remoteAddress is null");
2039 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2043 if (!strcmp(setAddress, remoteAddress))
2045 OIC_LOG_V(DEBUG, TAG, "device address : %s", remoteAddress);
2047 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2048 (*env)->ReleaseStringUTFChars(env, address, remoteAddress);
2049 (*env)->DeleteGlobalRef(env, jarrayObj);
2051 if (NULL == u_arraylist_remove(g_connectedDeviceList, index))
2053 OIC_LOG(ERROR, TAG, "List removal failed.");
2054 ca_mutex_unlock(g_connectedDeviceListMutex);
2055 return CA_STATUS_FAILED;
2057 ca_mutex_unlock(g_connectedDeviceListMutex);
2058 return CA_STATUS_OK;
2060 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2061 (*env)->ReleaseStringUTFChars(env, address, remoteAddress);
2065 ca_mutex_unlock(g_connectedDeviceListMutex);
2067 OIC_LOG(DEBUG, TAG, "there are no device in the device list");
2069 OIC_LOG(DEBUG, TAG, "IN CALEServerRemoveDevice");
2070 return CA_STATUS_FAILED;
2073 JNIEXPORT void JNICALL
2074 Java_org_iotivity_ca_CaLeServerInterface_caLeRegisterGattServerCallback(JNIEnv *env, jobject obj,
2077 OIC_LOG(DEBUG, TAG, "Register Le Gatt Server Callback");
2078 VERIFY_NON_NULL_VOID(env, TAG, "env");
2079 VERIFY_NON_NULL_VOID(obj, TAG, "obj");
2080 VERIFY_NON_NULL_VOID(callback, TAG, "callback");
2082 g_bluetoothGattServerCallback = (*env)->NewGlobalRef(env, callback);
2085 JNIEXPORT void JNICALL
2086 Java_org_iotivity_ca_CaLeServerInterface_caLeRegisterBluetoothLeAdvertiseCallback(JNIEnv *env,
2090 OIC_LOG(DEBUG, TAG, "Register Le Advertise Callback");
2091 VERIFY_NON_NULL_VOID(env, TAG, "env");
2092 VERIFY_NON_NULL_VOID(obj, TAG, "obj");
2093 VERIFY_NON_NULL_VOID(callback, TAG, "callback");
2095 g_leAdvertiseCallback = (*env)->NewGlobalRef(env, callback);
2098 JNIEXPORT void JNICALL
2099 Java_org_iotivity_ca_CaLeServerInterface_caLeGattServerConnectionStateChangeCallback(
2100 JNIEnv *env, jobject obj, jobject device, jint status, jint newState)
2102 OIC_LOG(DEBUG, TAG, " Gatt Server ConnectionStateChange Callback");
2103 OIC_LOG_V(DEBUG, TAG, "New connection State: %d", newState);
2105 VERIFY_NON_NULL_VOID(env, TAG, "env");
2106 VERIFY_NON_NULL_VOID(obj, TAG, "obj");
2107 VERIFY_NON_NULL_VOID(device, TAG, "device");
2109 jclass jni_cid_bluetoothProfile = (*env)->FindClass(env, "android/bluetooth/BluetoothProfile");
2110 if (!jni_cid_bluetoothProfile)
2112 OIC_LOG(ERROR, TAG, "jni_cid_bluetoothProfile is null");
2116 jfieldID jni_fid_state_connected = (*env)->GetStaticFieldID(env, jni_cid_bluetoothProfile,
2117 "STATE_CONNECTED", "I");
2118 if(!jni_fid_state_connected)
2120 OIC_LOG(ERROR, TAG, "jni_fid_state_connected is null");
2124 jfieldID jni_fid_state_disconnected = (*env)->GetStaticFieldID(env, jni_cid_bluetoothProfile,
2125 "STATE_DISCONNECTED", "I");
2126 if(!jni_fid_state_disconnected)
2128 OIC_LOG(ERROR, TAG, "jni_fid_state_disconnected is null");
2133 jint jni_int_state_connected = (*env)->GetStaticIntField(env, jni_cid_bluetoothProfile,
2134 jni_fid_state_connected);
2136 // STATE_DISCONNECTED
2137 jint jni_int_state_disconnected = (*env)->GetStaticIntField(env, jni_cid_bluetoothProfile,
2138 jni_fid_state_disconnected);
2140 if (newState == jni_int_state_connected)
2143 OIC_LOG(DEBUG, TAG, "LE CONNECTED");
2145 jstring jni_remoteAddress = CALEGetAddressFromBTDevice(env, device);
2146 if (!jni_remoteAddress)
2148 OIC_LOG(ERROR, TAG, "jni_remoteAddress is null");
2152 const char* remoteAddress = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);
2155 OIC_LOG(ERROR, TAG, "remoteAddress is null");
2159 if (false == CALEServerIsDeviceInList(env, remoteAddress))
2161 OIC_LOG(DEBUG, TAG, "add connected device to cache");
2162 CALEServerAddDeviceToList(env, device);
2164 (*env)->ReleaseStringUTFChars(env, jni_remoteAddress, remoteAddress);
2166 else if (newState == jni_int_state_disconnected)
2168 OIC_LOG(DEBUG, TAG, "LE DISCONNECTED");
2170 jstring jni_remoteAddress = CALEGetAddressFromBTDevice(env, device);
2171 CAResult_t ret = CALEServerRemoveDevice(env, jni_remoteAddress);
2172 if (CA_STATUS_OK != ret)
2174 OIC_LOG(ERROR, TAG, "CALEServerRemoveDevice has failed");
2178 ret = CALEServerStartAdvertise(env, g_leAdvertiseCallback);
2179 if (CA_STATUS_OK != ret)
2181 OIC_LOG(ERROR, TAG, "CALEServerStartAdvertise has failed");
2186 OIC_LOG_V(DEBUG, TAG, "LE Connection state is [newState : %d, status %d]", newState,
2191 JNIEXPORT void JNICALL
2192 Java_org_iotivity_ca_CaLeServerInterface_caLeGattServerServiceAddedCallback(JNIEnv *env,
2195 jobject gattService)
2197 VERIFY_NON_NULL_VOID(env, TAG, "env");
2198 VERIFY_NON_NULL_VOID(obj, TAG, "obj");
2199 VERIFY_NON_NULL_VOID(gattService, TAG, "gattService");
2201 OIC_LOG_V(DEBUG, TAG, "Gatt Service Added Callback(%d)", status);
2204 JNIEXPORT void JNICALL
2205 Java_org_iotivity_ca_CaLeServerInterface_caLeGattServerCharacteristicReadRequestCallback(
2206 JNIEnv *env, jobject obj, jobject device, jbyteArray data)
2208 OIC_LOG(DEBUG, TAG, " Gatt Server Characteristic Read Request Callback");
2209 VERIFY_NON_NULL_VOID(env, TAG, "env");
2210 VERIFY_NON_NULL_VOID(obj, TAG, "obj");
2211 VERIFY_NON_NULL_VOID(device, TAG, "device");
2212 VERIFY_NON_NULL_VOID(data, TAG, "data");
2215 JNIEXPORT void JNICALL
2216 Java_org_iotivity_ca_CaLeServerInterface_caLeGattServerCharacteristicWriteRequestCallback(
2217 JNIEnv *env, jobject obj, jobject device, jbyteArray data)
2219 OIC_LOG_V(DEBUG, TAG, "Gatt Server Characteristic Write Request Callback");
2220 VERIFY_NON_NULL_VOID(env, TAG, "env");
2221 VERIFY_NON_NULL_VOID(obj, TAG, "obj");
2222 VERIFY_NON_NULL_VOID(device, TAG, "device");
2223 VERIFY_NON_NULL_VOID(data, TAG, "data");
2225 // get Byte Array and covert to uint8_t*
2226 jint length = (*env)->GetArrayLength(env, data);
2229 jbyte *jni_byte_requestData = (jbyte *) (*env)->GetByteArrayElements(env, data, &isCopy);
2231 uint8_t* requestData = NULL;
2232 requestData = OICMalloc(length);
2235 OIC_LOG(ERROR, TAG, "requestData is null");
2239 memcpy(requestData, jni_byte_requestData, length);
2240 (*env)->ReleaseByteArrayElements(env, data, jni_byte_requestData, JNI_ABORT);
2242 jstring jni_address = CALEGetAddressFromBTDevice(env, device);
2245 OIC_LOG(ERROR, TAG, "jni_address is null");
2246 OICFree(requestData);
2250 const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
2253 OIC_LOG(ERROR, TAG, "address is null");
2254 OICFree(requestData);
2258 OIC_LOG_V(DEBUG, TAG, "remote device address : %s, %p, %d", address, requestData, length);
2260 ca_mutex_lock(g_bleClientBDAddressMutex);
2261 uint32_t sentLength = 0;
2262 g_CABLEServerDataReceivedCallback(address, requestData, length,
2264 ca_mutex_unlock(g_bleClientBDAddressMutex);
2266 (*env)->ReleaseStringUTFChars(env, jni_address, address);
2269 JNIEXPORT void JNICALL
2270 Java_org_iotivity_ca_CaLeServerInterface_caLeGattServerNotificationSentCallback(JNIEnv *env,
2275 VERIFY_NON_NULL_VOID(env, TAG, "env");
2276 VERIFY_NON_NULL_VOID(obj, TAG, "obj");
2277 VERIFY_NON_NULL_VOID(device, TAG, "device");
2279 OIC_LOG_V(DEBUG, TAG, "Gatt Server Notification Sent Callback(%d)",
2283 JNIEXPORT void JNICALL
2284 Java_org_iotivity_ca_CaLeServerInterface_caLeAdvertiseStartSuccessCallback(JNIEnv *env,
2286 jobject settingsInEffect)
2288 VERIFY_NON_NULL_VOID(env, TAG, "env");
2289 VERIFY_NON_NULL_VOID(obj, TAG, "obj");
2290 VERIFY_NON_NULL_VOID(settingsInEffect, TAG, "settingsInEffect");
2292 OIC_LOG(DEBUG, TAG, "LE Advertise Start Success Callback");
2295 JNIEXPORT void JNICALL
2296 Java_org_iotivity_ca_CaLeServerInterface_caLeAdvertiseStartFailureCallback(JNIEnv *env,
2300 VERIFY_NON_NULL_VOID(env, TAG, "env");
2301 VERIFY_NON_NULL_VOID(obj, TAG, "obj");
2303 OIC_LOG_V(INFO, TAG, "LE Advertise Start Failure Callback(%d)", errorCode);
2310 CAResult_t CAStartLEGattServer()
2312 CAResult_t ret = CALEServerInitMutexVaraibles();
2313 if (CA_STATUS_OK != ret)
2315 OIC_LOG(ERROR, TAG, "CALEServerInitMutexVaraibles has failed!");
2316 CALEServerTerminateMutexVaraibles();
2317 return CA_SERVER_NOT_STARTED;
2320 ret = CALEServerInitConditionVaraibles();
2321 if (CA_STATUS_OK != ret)
2323 OIC_LOG(ERROR, TAG, "CALEServerInitConditionVaraibles has failed!");
2324 CALEServerTerminateConditionVaraibles();
2325 return CA_SERVER_NOT_STARTED;
2328 // start gatt service
2329 CALEServerStartMulticastServer();
2331 return CA_STATUS_OK;
2334 CAResult_t CAStopLEGattServer()
2336 OIC_LOG(DEBUG, TAG, "this method is not supported");
2337 return CA_STATUS_OK;
2340 void CATerminateLEGattServer()
2342 OIC_LOG(DEBUG, TAG, "Terminat Gatt Server");
2343 CALEServerTerminate();
2346 void CASetLEReqRespServerCallback(CABLEDataReceivedCallback callback)
2348 ca_mutex_lock(g_bleReqRespCbMutex);
2349 g_CABLEServerDataReceivedCallback = callback;
2350 ca_mutex_unlock(g_bleReqRespCbMutex);
2353 void CASetBLEServerErrorHandleCallback(CABLEErrorHandleCallback callback)
2355 g_serverErrorCallback = callback;
2358 CAResult_t CAUpdateCharacteristicsToGattClient(const char *address,
2359 const uint8_t *charValue,
2360 uint32_t charValueLen)
2362 CAResult_t result = CA_SEND_FAILED;
2363 VERIFY_NON_NULL(address, TAG, "env is null");
2364 VERIFY_NON_NULL(charValue, TAG, "device is null");
2368 result = CALEServerSendUnicastMessage(address, charValue, charValueLen);
2374 CAResult_t CAUpdateCharacteristicsToAllGattClients(const uint8_t *charValue,
2375 uint32_t charValueLen)
2377 VERIFY_NON_NULL(charValue, TAG, "device is null");
2379 CAResult_t result = CALEServerSendMulticastMessage(charValue, charValueLen);
2384 void CASetLEServerThreadPoolHandle(ca_thread_pool_t handle)
2386 CALEServerInitialize(handle);
2389 CAResult_t CALEServerInitMutexVaraibles()
2391 if (NULL == g_bleReqRespCbMutex)
2393 g_bleReqRespCbMutex = ca_mutex_new();
2394 if (NULL == g_bleReqRespCbMutex)
2396 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
2397 return CA_STATUS_FAILED;
2401 if (NULL == g_bleClientBDAddressMutex)
2403 g_bleClientBDAddressMutex = ca_mutex_new();
2404 if (NULL == g_bleClientBDAddressMutex)
2406 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
2407 return CA_STATUS_FAILED;
2411 if (NULL == g_connectedDeviceListMutex)
2413 g_connectedDeviceListMutex = ca_mutex_new();
2414 if (NULL == g_connectedDeviceListMutex)
2416 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
2417 return CA_STATUS_FAILED;
2421 return CA_STATUS_OK;
2424 CAResult_t CALEServerInitConditionVaraibles()
2426 OIC_LOG(DEBUG, TAG, "this method is not supported");
2427 return CA_STATUS_OK;
2430 void CALEServerTerminateMutexVaraibles()
2432 ca_mutex_free(g_bleReqRespCbMutex);
2433 g_bleReqRespCbMutex = NULL;
2435 ca_mutex_free(g_bleClientBDAddressMutex);
2436 g_bleClientBDAddressMutex = NULL;
2438 ca_mutex_free(g_connectedDeviceListMutex);
2439 g_connectedDeviceListMutex = NULL;
2442 void CALEServerTerminateConditionVaraibles()
2444 OIC_LOG(DEBUG, TAG, "this method is not supported");