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 CABLEServerDataReceivedCallback 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_bluetoothGattService)
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_cid_bluetoothGattService)
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 CALEServerStartAdvertise(JNIEnv *env, jobject advertiseCallback)
350 OIC_LOG(DEBUG, TAG, "IN - CALEServerStartAdvertise");
351 VERIFY_NON_NULL(env, TAG, "env is null");
352 VERIFY_NON_NULL(advertiseCallback, TAG, "advertiseCallback is null");
354 if (!CALEIsEnableBTAdapter(env))
356 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
357 return CA_ADAPTER_NOT_ENABLED;
360 jclass jni_cid_AdvertiseSettings = (*env)->FindClass(env,
361 "android/bluetooth/le/"
362 "AdvertiseSettings$Builder");
363 if (!jni_cid_AdvertiseSettings)
365 OIC_LOG(ERROR, TAG, "jni_cid_AdvertiseSettings is null");
366 return CA_STATUS_FAILED;
369 jmethodID jni_mid_AdvertiseSettings = (*env)->GetMethodID(env, jni_cid_AdvertiseSettings,
371 if (!jni_mid_AdvertiseSettings)
373 OIC_LOG(ERROR, TAG, "jni_mid_AdvertiseSettings is null");
374 return CA_STATUS_FAILED;
377 jobject jni_AdvertiseSettings = (*env)->NewObject(env, jni_cid_AdvertiseSettings,
378 jni_mid_AdvertiseSettings);
379 if (!jni_AdvertiseSettings)
381 OIC_LOG(ERROR, TAG, "jni_AdvertiseSettings is null");
382 return CA_STATUS_FAILED;
385 jmethodID jni_mid_setAdvertiseMode = (*env)->GetMethodID(env, jni_cid_AdvertiseSettings,
387 "(I)Landroid/bluetooth/le/"
388 "AdvertiseSettings$Builder;");
389 if (!jni_mid_setAdvertiseMode)
391 OIC_LOG(ERROR, TAG, "jni_mid_setAdvertiseMode is null");
392 return CA_STATUS_FAILED;
395 // 0: Low power, 1: Balanced
396 jobject jni_obj_setAdvertiseMode = (*env)->CallObjectMethod(env, jni_AdvertiseSettings,
397 jni_mid_setAdvertiseMode, 0);
398 if (!jni_obj_setAdvertiseMode)
400 OIC_LOG(ERROR, TAG, "jni_obj_setAdvertiseMode is null");
401 return CA_STATUS_FAILED;
404 jmethodID jni_mid_setConnectable = (*env)->GetMethodID(env, jni_cid_AdvertiseSettings,
406 "(Z)Landroid/bluetooth/le/"
407 "AdvertiseSettings$Builder;");
408 if (!jni_mid_setConnectable)
410 OIC_LOG(ERROR, TAG, "jni_mid_setConnectable is null");
411 return CA_STATUS_FAILED;
414 jobject jni_obj_setConnectable = (*env)->CallObjectMethod(env, jni_AdvertiseSettings,
415 jni_mid_setConnectable, JNI_TRUE);
416 if (!jni_obj_setConnectable)
418 OIC_LOG(ERROR, TAG, "jni_obj_setConnectable is null");
419 return CA_STATUS_FAILED;
422 jmethodID jni_mid_setTimeout = (*env)->GetMethodID(env, jni_cid_AdvertiseSettings, "setTimeout",
423 "(I)Landroid/bluetooth/le/"
424 "AdvertiseSettings$Builder;");
425 if (!jni_mid_setTimeout)
427 OIC_LOG(ERROR, TAG, "jni_mid_setTimeout is null");
428 return CA_STATUS_FAILED;
431 //A value of 0 will disable the time limit
432 jobject jni_obj_setTimeout = (*env)->CallObjectMethod(env, jni_AdvertiseSettings,
433 jni_mid_setTimeout, 0);
434 if (!jni_obj_setTimeout)
436 OIC_LOG(ERROR, TAG, "jni_obj_setTimeout is null");
437 return CA_STATUS_FAILED;
440 jclass jni_cid_AdvertiseDataBuilder = (*env)->FindClass(env,
441 "android/bluetooth/le/"
442 "AdvertiseData$Builder");
443 if (!jni_cid_AdvertiseDataBuilder)
445 OIC_LOG(ERROR, TAG, "jni_cid_AdvertiseDataBuilder is null");
446 return CA_STATUS_FAILED;
449 jmethodID jni_mid_AdvertiseDataBuilder = (*env)->GetMethodID(env, jni_cid_AdvertiseDataBuilder,
451 if (!jni_mid_AdvertiseDataBuilder)
453 OIC_LOG(ERROR, TAG, "jni_mid_AdvertiseDataBuilder is null");
454 return CA_STATUS_FAILED;
457 jobject jni_AdvertiseDataBuilder = (*env)->NewObject(env, jni_cid_AdvertiseDataBuilder,
458 jni_mid_AdvertiseDataBuilder);
459 if (!jni_AdvertiseDataBuilder)
461 OIC_LOG(ERROR, TAG, "jni_AdvertiseDataBuilder is null");
462 return CA_STATUS_FAILED;
465 jobject jni_obj_serviceUUID = CALEGetUuidFromString(env, OIC_GATT_SERVICE_UUID);
466 if (!jni_obj_serviceUUID)
468 OIC_LOG(ERROR, TAG, "jni_obj_serviceUUID is null");
469 return CA_STATUS_FAILED;
472 jobject jni_ParcelUuid = CALEGetParcelUuid(env, jni_obj_serviceUUID);
475 OIC_LOG(ERROR, TAG, "jni_ParcelUuid is null");
476 return CA_STATUS_FAILED;
479 jmethodID jni_mid_addServiceUuid = (*env)->GetMethodID(env, jni_cid_AdvertiseDataBuilder,
481 "(Landroid/os/ParcelUuid;)Landroid/"
482 "bluetooth/le/AdvertiseData$Builder;");
483 if (!jni_mid_addServiceUuid)
485 OIC_LOG(ERROR, TAG, "jni_mid_addServiceUuid is null");
486 return CA_STATUS_FAILED;
489 jobject jni_obj_addServiceUuid = (*env)->CallObjectMethod(env, jni_AdvertiseDataBuilder,
490 jni_mid_addServiceUuid,
492 if (!jni_obj_addServiceUuid)
494 OIC_LOG(ERROR, TAG, "jni_obj_addServiceUuid is null");
495 return CA_STATUS_FAILED;
498 jclass jni_cid_BTAdapter = (*env)->FindClass(env, "android/bluetooth/BluetoothAdapter");
499 if (!jni_cid_BTAdapter)
501 OIC_LOG(ERROR, TAG, "jni_cid_BTAdapter is null");
502 return CA_STATUS_FAILED;
505 jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env, jni_cid_BTAdapter,
507 "()Landroid/bluetooth/"
508 "BluetoothAdapter;");
509 if (!jni_mid_getDefaultAdapter)
511 OIC_LOG(ERROR, TAG, "jni_mid_getDefaultAdapter is null");
512 return CA_STATUS_FAILED;
515 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter,
516 jni_mid_getDefaultAdapter);
517 if (!jni_obj_BTAdapter)
519 OIC_LOG(ERROR, TAG, "jni_obj_BTAdapter is null");
520 return CA_STATUS_FAILED;
523 jmethodID jni_mid_getBluetoothLeAdvertiser = (*env)->GetMethodID(env, jni_cid_BTAdapter,
524 "getBluetoothLeAdvertiser",
525 "()Landroid/bluetooth/le/"
526 "BluetoothLeAdvertiser;");
527 if (!jni_mid_getBluetoothLeAdvertiser)
529 OIC_LOG(ERROR, TAG, "jni_mid_getBluetoothLeAdvertiser is null");
530 return CA_STATUS_FAILED;
533 jobject jni_obj_getBluetoothLeAdvertiser = (*env)->CallObjectMethod(
534 env, jni_obj_BTAdapter, jni_mid_getBluetoothLeAdvertiser);
535 if (!jni_obj_getBluetoothLeAdvertiser)
537 OIC_LOG(ERROR, TAG, "jni_obj_getBluetoothLeAdvertiser is null");
538 return CA_STATUS_FAILED;
541 jmethodID jni_mid_build_LeAdvertiseSettings = (*env)->GetMethodID(env,
542 jni_cid_AdvertiseSettings,
544 "()Landroid/bluetooth/le/"
545 "AdvertiseSettings;");
546 if (!jni_mid_build_LeAdvertiseSettings)
548 OIC_LOG(ERROR, TAG, "jni_mid_build_LeAdvertiseSettings is null");
549 return CA_STATUS_FAILED;
552 jobject jni_obj_build_LeAdvertiseSettings = (*env)->CallObjectMethod(
553 env, jni_AdvertiseSettings, jni_mid_build_LeAdvertiseSettings);
554 if (!jni_obj_build_LeAdvertiseSettings)
556 OIC_LOG(ERROR, TAG, "jni_obj_build_LeAdvertiseSettings is null");
557 return CA_STATUS_FAILED;
560 jmethodID jni_mid_build_LeAdvertiseData = (*env)->GetMethodID(env, jni_cid_AdvertiseDataBuilder,
562 "()Landroid/bluetooth/le/"
564 if (!jni_mid_build_LeAdvertiseData)
566 OIC_LOG(ERROR, TAG, "jni_mid_build_LeAdvertiseData is null");
567 return CA_STATUS_FAILED;
570 jobject jni_obj_build_LeAdvertiseData = (*env)->CallObjectMethod(env, jni_AdvertiseDataBuilder,
571 jni_mid_build_LeAdvertiseData);
572 if (!jni_obj_build_LeAdvertiseData)
574 OIC_LOG(ERROR, TAG, "jni_obj_build_LeAdvertiseData is null");
575 return CA_STATUS_FAILED;
578 jclass jni_cid_leAdvertiser = (*env)->FindClass(env,
579 "android/bluetooth/le/BluetoothLeAdvertiser");
580 if (!jni_cid_leAdvertiser)
582 OIC_LOG(ERROR, TAG, "jni_cid_leAdvertiser is null");
583 return CA_STATUS_FAILED;
586 jmethodID jni_mid_startAdvertising = (*env)->GetMethodID(env, jni_cid_leAdvertiser,
588 "(Landroid/bluetooth/le/"
589 "AdvertiseSettings;Landroid/bluetooth/"
590 "le/AdvertiseData;Landroid/bluetooth/"
591 "le/AdvertiseCallback;)V");
592 if (!jni_mid_startAdvertising)
594 OIC_LOG(ERROR, TAG, "jni_mid_startAdvertising is null");
595 return CA_STATUS_FAILED;
598 (*env)->CallVoidMethod(env, jni_obj_getBluetoothLeAdvertiser, jni_mid_startAdvertising,
599 jni_obj_build_LeAdvertiseSettings, jni_obj_build_LeAdvertiseData,
602 if ((*env)->ExceptionCheck(env))
604 OIC_LOG(ERROR, TAG, "StartAdvertising has failed");
605 (*env)->ExceptionDescribe(env);
606 (*env)->ExceptionClear(env);
607 return CA_STATUS_FAILED;
610 OIC_LOG(DEBUG, TAG, "Advertising started!!");
612 OIC_LOG(DEBUG, TAG, "OUT - CALEServerStartAdvertise");
616 CAResult_t CALEServerStopAdvertise(JNIEnv *env, jobject advertiseCallback)
618 OIC_LOG(DEBUG, TAG, "IN - LEServerStopAdvertise");
619 VERIFY_NON_NULL(env, TAG, "env is null");
620 VERIFY_NON_NULL(advertiseCallback, TAG, "advertiseCallback is null");
622 if (!CALEIsEnableBTAdapter(env))
624 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
625 return CA_ADAPTER_NOT_ENABLED;
628 jclass jni_cid_BTAdapter = (*env)->FindClass(env, "android/bluetooth/BluetoothAdapter");
629 if (!jni_cid_BTAdapter)
631 OIC_LOG(ERROR, TAG, "jni_cid_BTAdapter is null");
632 return CA_STATUS_FAILED;
635 jclass jni_cid_leAdvertiser = (*env)->FindClass(env,
636 "android/bluetooth/le/BluetoothLeAdvertiser");
637 if (!jni_cid_leAdvertiser)
639 OIC_LOG(ERROR, TAG, "jni_cid_leAdvertiser is null");
640 return CA_STATUS_FAILED;
643 jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env, jni_cid_BTAdapter,
645 "()Landroid/bluetooth/"
646 "BluetoothAdapter;");
647 if (!jni_cid_leAdvertiser)
649 OIC_LOG(ERROR, TAG, "jni_cid_leAdvertiser is null");
650 return CA_STATUS_FAILED;
653 jmethodID jni_mid_getBTLeAdvertiser = (*env)->GetMethodID(env, jni_cid_BTAdapter,
654 "getBluetoothLeAdvertiser",
655 "()Landroid/bluetooth/le/"
656 "BluetoothLeAdvertiser;");
657 if (!jni_mid_getBTLeAdvertiser)
659 OIC_LOG(ERROR, TAG, "jni_mid_getBTLeAdvertiser is null");
660 return CA_STATUS_FAILED;
663 jmethodID jni_mid_stopAdvertising = (*env)->GetMethodID(env, jni_cid_leAdvertiser,
665 "(Landroid/bluetooth/le/"
666 "AdvertiseCallback;)V");
667 if (!jni_mid_stopAdvertising)
669 OIC_LOG(ERROR, TAG, "jni_mid_stopAdvertising is null");
670 return CA_STATUS_FAILED;
673 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter,
674 jni_mid_getDefaultAdapter);
675 if (!jni_obj_BTAdapter)
677 OIC_LOG(ERROR, TAG, "jni_obj_BTAdapter is null");
678 return CA_STATUS_FAILED;
681 jobject jni_obj_getBluetoothLeAdvertiser = (*env)->CallObjectMethod(env, jni_obj_BTAdapter,
682 jni_mid_getBTLeAdvertiser);
683 if (!jni_obj_getBluetoothLeAdvertiser)
685 OIC_LOG(ERROR, TAG, "jni_obj_getBluetoothLeAdvertiser is null");
686 return CA_STATUS_FAILED;
689 (*env)->CallVoidMethod(env, jni_obj_getBluetoothLeAdvertiser, jni_mid_stopAdvertising,
691 if ((*env)->ExceptionCheck(env))
693 OIC_LOG(ERROR, TAG, "getBluetoothLeAdvertiser has failed");
694 (*env)->ExceptionDescribe(env);
695 (*env)->ExceptionClear(env);
696 return CA_STATUS_FAILED;
699 OIC_LOG(DEBUG, TAG, "Advertising stopped!!");
701 OIC_LOG(DEBUG, TAG, "OUT - LEServerStopAdvertise");
705 CAResult_t CALEServerStartGattServer(JNIEnv *env, jobject gattServerCallback)
707 OIC_LOG(DEBUG, TAG, "IN - CALEServerStartGattServer");
708 VERIFY_NON_NULL(env, TAG, "env is null");
709 VERIFY_NON_NULL(gattServerCallback, TAG, "gattServerCallback is null");
711 if (!CALEIsEnableBTAdapter(env))
713 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
714 return CA_ADAPTER_NOT_ENABLED;
719 OIC_LOG(DEBUG, TAG, "Gatt server already started");
722 g_bluetoothGattServerCallback = (*env)->NewGlobalRef(env, gattServerCallback);
725 jobject bluetoothGattServer = CALEServerOpenGattServer(env);
726 if (!bluetoothGattServer)
728 OIC_LOG(ERROR, TAG, "bluetoothGattServer is null");
729 return CA_STATUS_FAILED;
732 g_bluetoothGattServer = (*env)->NewGlobalRef(env, bluetoothGattServer);
733 if (!g_bluetoothGattServer)
735 OIC_LOG(ERROR, TAG, "g_bluetoothGattServer is null");
736 return CA_STATUS_FAILED;
739 // create gatt service
740 jobject bluetoothGattService = CALEServerCreateGattService(env);
741 if (!bluetoothGattService)
743 OIC_LOG(ERROR, TAG, "bluetoothGattService is null");
744 return CA_STATUS_FAILED;
748 CAResult_t res = CALEServerAddGattService(env, g_bluetoothGattServer,
749 bluetoothGattService);
750 if (CA_STATUS_OK != res)
752 OIC_LOG(ERROR, TAG, "CALEServerAddGattService has failed");
757 jobject CALEServerOpenGattServer(JNIEnv *env)
759 OIC_LOG(DEBUG, TAG, "IN - CALEServerOpenGattServer");
760 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
762 if (!CALEIsEnableBTAdapter(env))
764 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
768 jclass jni_cid_context = (*env)->FindClass(env, "android/content/Context");
769 if (!jni_cid_context)
771 OIC_LOG(ERROR, TAG, "jni_cid_context is null");
775 jclass jni_cid_bluetoothManager = (*env)->FindClass(env, "android/bluetooth/BluetoothManager");
776 if (!jni_cid_bluetoothManager)
778 OIC_LOG(ERROR, TAG, "jni_cid_bluetoothManager is null");
782 jfieldID jni_fid_bluetoothService = (*env)->GetStaticFieldID(env, jni_cid_context,
784 "Ljava/lang/String;");
785 if (!jni_fid_bluetoothService)
787 OIC_LOG(ERROR, TAG, "jni_fid_bluetoothService is null");
791 jmethodID jni_mid_getSystemService = (*env)->GetMethodID(env, jni_cid_context,
793 "(Ljava/lang/String;)"
794 "Ljava/lang/Object;");
795 if (!jni_mid_getSystemService)
797 OIC_LOG(ERROR, TAG, "jni_mid_getSystemService is null");
801 jmethodID jni_mid_openGattServer = (*env)->GetMethodID(env, jni_cid_bluetoothManager,
803 "(Landroid/content/Context;"
804 "Landroid/bluetooth/"
805 "BluetoothGattServerCallback;)"
806 "Landroid/bluetooth/"
807 "BluetoothGattServer;");
808 if (!jni_mid_openGattServer)
810 OIC_LOG(ERROR, TAG, "jni_mid_openGattServer is null");
814 jobject jni_obj_bluetoothService = (*env)->GetStaticObjectField(env, jni_cid_context,
815 jni_fid_bluetoothService);
816 if (!jni_obj_bluetoothService)
818 OIC_LOG(ERROR, TAG, "jni_obj_bluetoothService is null");
822 jobject jni_obj_bluetoothManager = (*env)->CallObjectMethod(env, g_context,
823 jni_mid_getSystemService,
824 jni_obj_bluetoothService);
825 if (!jni_obj_bluetoothManager)
827 OIC_LOG(ERROR, TAG, "jni_obj_bluetoothManager is null");
831 jobject jni_obj_bluetoothGattServer = (*env)->CallObjectMethod(env, jni_obj_bluetoothManager,
832 jni_mid_openGattServer,
834 g_bluetoothGattServerCallback);
835 if (!jni_obj_bluetoothGattServer)
837 OIC_LOG(ERROR, TAG, "jni_obj_bluetoothGattServer is null");
841 OIC_LOG(DEBUG, TAG, "OUT - CALEServerOpenGattServer");
842 return jni_obj_bluetoothGattServer;
845 jobject CALEServerCreateGattService(JNIEnv *env)
847 OIC_LOG(DEBUG, TAG, "IN - CALEServerCreateGattService");
848 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
850 if (!CALEIsEnableBTAdapter(env))
852 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
856 jclass jni_cid_bluetoothGattService = (*env)->FindClass(env, "android/bluetooth/"
857 "BluetoothGattService");
858 if (!jni_cid_bluetoothGattService)
860 OIC_LOG(ERROR, TAG, "jni_cid_bluetoothGattService is null");
864 jclass jni_cid_bluetoothGattCharacteristic = (*env)->FindClass(env, "android/bluetooth/"
865 "BluetoothGattCharacteristic");
866 if (!jni_cid_bluetoothGattCharacteristic)
868 OIC_LOG(ERROR, TAG, "jni_cid_bluetoothGattCharacteristic is null");
872 jfieldID jni_fid_serviceType = (*env)->GetStaticFieldID(env, jni_cid_bluetoothGattService,
873 "SERVICE_TYPE_PRIMARY", "I");
874 if (!jni_fid_serviceType)
876 OIC_LOG(ERROR, TAG, "jni_fid_serviceType is null");
880 jfieldID jni_fid_readProperties = (*env)->GetStaticFieldID(env,
881 jni_cid_bluetoothGattCharacteristic,
882 "PROPERTY_READ", "I");
883 if (!jni_fid_readProperties)
885 OIC_LOG(ERROR, TAG, "jni_fid_readProperties is null");
889 #ifdef USE_PROPERTY_WRITE_RESPONSE
890 jfieldID jni_fid_writeProperties = (*env)->GetStaticFieldID(env,
891 jni_cid_bluetoothGattCharacteristic,
892 "PROPERTY_WRITE", "I");
894 jfieldID jni_fid_writeProperties = (*env)->GetStaticFieldID(env,
895 jni_cid_bluetoothGattCharacteristic,
896 "PROPERTY_WRITE_NO_RESPONSE", "I");
899 if (!jni_fid_writeProperties)
901 OIC_LOG(ERROR, TAG, "jni_fid_writeProperties is null");
905 jfieldID jni_fid_readPermissions = (*env)->GetStaticFieldID(env,
906 jni_cid_bluetoothGattCharacteristic,
907 "PERMISSION_READ", "I");
908 if (!jni_fid_readPermissions)
910 OIC_LOG(ERROR, TAG, "jni_fid_readPermissions is null");
914 jfieldID jni_fid_writePermissions = (*env)->GetStaticFieldID(
915 env, jni_cid_bluetoothGattCharacteristic, "PERMISSION_WRITE", "I");
916 if (!jni_fid_writePermissions)
918 OIC_LOG(ERROR, TAG, "jni_fid_writePermissions is null");
922 jmethodID jni_mid_bluetoothGattService = (*env)->GetMethodID(env, jni_cid_bluetoothGattService,
923 "<init>", "(Ljava/util/UUID;I)V");
924 if (!jni_mid_bluetoothGattService)
926 OIC_LOG(ERROR, TAG, "jni_mid_bluetoothGattService is null");
930 jmethodID jni_mid_addCharacteristic = (*env)->GetMethodID(env, jni_cid_bluetoothGattService,
932 "(Landroid/bluetooth/"
933 "BluetoothGattCharacteristic;)Z");
934 if (!jni_mid_addCharacteristic)
936 OIC_LOG(ERROR, TAG, "jni_mid_addCharacteristic is null");
940 jmethodID jni_mid_bluetoothGattCharacteristic = (*env)->GetMethodID(
941 env, jni_cid_bluetoothGattCharacteristic, "<init>", "(Ljava/util/UUID;II)V");
942 if (!jni_mid_bluetoothGattCharacteristic)
944 OIC_LOG(ERROR, TAG, "jni_mid_bluetoothGattCharacteristic is null");
948 jobject jni_obj_serviceUUID = CALEGetUuidFromString(env, OIC_GATT_SERVICE_UUID);
949 if (!jni_obj_serviceUUID)
951 OIC_LOG(ERROR, TAG, "jni_obj_serviceUUID is null");
955 jint jni_int_serviceType = (*env)->GetStaticIntField(env, jni_cid_bluetoothGattService,
956 jni_fid_serviceType);
957 jobject jni_bluetoothGattService = (*env)->NewObject(env, jni_cid_bluetoothGattService,
958 jni_mid_bluetoothGattService,
959 jni_obj_serviceUUID, jni_int_serviceType);
960 if (!jni_bluetoothGattService)
962 OIC_LOG(ERROR, TAG, "jni_bluetoothGattService is null");
966 jobject jni_obj_readUuid = CALEGetUuidFromString(env, OIC_GATT_CHARACTERISTIC_RESPONSE_UUID);
967 if (!jni_obj_readUuid)
969 OIC_LOG(ERROR, TAG, "jni_obj_readUuid is null");
973 jint jni_int_readProperties = (*env)->GetStaticIntField(env,
974 jni_cid_bluetoothGattCharacteristic,
975 jni_fid_readProperties);
977 jint jni_int_readPermissions = (*env)->GetStaticIntField(env,
978 jni_cid_bluetoothGattCharacteristic,
979 jni_fid_readPermissions);
981 jobject jni_readCharacteristic = (*env)->NewObject(env, jni_cid_bluetoothGattCharacteristic,
982 jni_mid_bluetoothGattCharacteristic,
983 jni_obj_readUuid, jni_int_readProperties,
984 jni_int_readPermissions);
985 if (!jni_readCharacteristic)
987 OIC_LOG(ERROR, TAG, "jni_readCharacteristic is null");
991 CAResult_t res = CALEServerAddDescriptor(env, jni_readCharacteristic);
992 if (CA_STATUS_OK != res)
994 OIC_LOG(ERROR, TAG, "CALEServerAddDescriptor has failed");
998 jboolean jni_boolean_addReadCharacteristic = (*env)->CallBooleanMethod(
999 env, jni_bluetoothGattService, jni_mid_addCharacteristic, jni_readCharacteristic);
1000 if (!jni_boolean_addReadCharacteristic)
1002 OIC_LOG(ERROR, TAG, "jni_boolean_addReadCharacteristic is null");
1006 jobject jni_obj_writeUuid = CALEGetUuidFromString(env, OIC_GATT_CHARACTERISTIC_REQUEST_UUID);
1007 if (!jni_obj_writeUuid)
1009 OIC_LOG(ERROR, TAG, "jni_obj_bluetoothGattServer is null");
1013 jint jni_int_writeProperties = (*env)->GetStaticIntField(env,
1014 jni_cid_bluetoothGattCharacteristic,
1015 jni_fid_writeProperties);
1017 jint jni_int_writePermissions = (*env)->GetStaticIntField(env,
1018 jni_cid_bluetoothGattCharacteristic,
1019 jni_fid_writePermissions);
1021 jobject jni_writeCharacteristic = (*env)->NewObject(env, jni_cid_bluetoothGattCharacteristic,
1022 jni_mid_bluetoothGattCharacteristic,
1023 jni_obj_writeUuid, jni_int_writeProperties,
1024 jni_int_writePermissions);
1025 if (!jni_writeCharacteristic)
1027 OIC_LOG(ERROR, TAG, "jni_writeCharacteristic is null");
1031 jboolean jni_boolean_addWriteCharacteristic = (*env)->CallBooleanMethod(
1032 env, jni_bluetoothGattService, jni_mid_addCharacteristic, jni_writeCharacteristic);
1033 if (JNI_FALSE == jni_boolean_addWriteCharacteristic)
1035 OIC_LOG(ERROR, TAG, "Fail to add jni_boolean_addReadCharacteristic");
1039 OIC_LOG(DEBUG, TAG, "OUT - CALEServerCreateGattService");
1040 return jni_bluetoothGattService;
1043 CAResult_t CALEServerAddDescriptor(JNIEnv *env, jobject characteristic)
1045 OIC_LOG(DEBUG, TAG, "IN - CALEServerAddDescriptor");
1046 VERIFY_NON_NULL(env, TAG, "env is null");
1047 VERIFY_NON_NULL(characteristic, TAG, "characteristic is null");
1049 jclass jni_cid_bluetoothGattDescriptor = (*env)->FindClass(env, "android/bluetooth/"
1050 "BluetoothGattDescriptor");
1051 if (!jni_cid_bluetoothGattDescriptor)
1053 OIC_LOG(ERROR, TAG, "jni_cid_bluetoothGattDescriptor is null");
1054 return CA_STATUS_FAILED;
1057 jmethodID jni_mid_bluetoothGattDescriptor = (*env)->GetMethodID(env,
1058 jni_cid_bluetoothGattDescriptor,
1060 "(Ljava/util/UUID;I)V");
1061 if (!jni_mid_bluetoothGattDescriptor)
1063 OIC_LOG(ERROR, TAG, "jni_mid_bluetoothGattDescriptor is null");
1064 return CA_STATUS_FAILED;
1067 jfieldID jni_fid_readPermissions = (*env)->GetStaticFieldID(env,
1068 jni_cid_bluetoothGattDescriptor,
1069 "PERMISSION_READ", "I");
1070 if (!jni_fid_readPermissions)
1072 OIC_LOG(ERROR, TAG, "jni_fid_readPermissions is null");
1073 return CA_STATUS_FAILED;
1076 jobject jni_obj_readUuid = CALEGetUuidFromString(env, OIC_GATT_CHARACTERISTIC_CONFIG_UUID);
1077 if (!jni_obj_readUuid)
1079 OIC_LOG(ERROR, TAG, "jni_obj_readUuid is null");
1080 return CA_STATUS_FAILED;
1083 jint jni_int_readPermissions = (*env)->GetStaticIntField(env, jni_cid_bluetoothGattDescriptor,
1084 jni_fid_readPermissions);
1086 OIC_LOG(DEBUG, TAG, "initialize new Descriptor");
1088 jobject jni_readDescriptor = (*env)->NewObject(env, jni_cid_bluetoothGattDescriptor,
1089 jni_mid_bluetoothGattDescriptor,
1090 jni_obj_readUuid, jni_int_readPermissions);
1091 if (!jni_readDescriptor)
1093 OIC_LOG(ERROR, TAG, "jni_readDescriptor is null");
1094 return CA_STATUS_FAILED;
1097 jclass jni_cid_GattCharacteristic = (*env)->FindClass(env, "android/bluetooth/"
1098 "BluetoothGattCharacteristic");
1099 if (!jni_cid_GattCharacteristic)
1101 OIC_LOG(ERROR, TAG, "jni_cid_GattCharacteristic is null");
1102 return CA_STATUS_FAILED;
1105 jmethodID jni_mid_addDescriptor = (*env)->GetMethodID(env, jni_cid_GattCharacteristic,
1107 "(Landroid/bluetooth/"
1108 "BluetoothGattDescriptor;)Z");
1109 if (!jni_mid_addDescriptor)
1111 OIC_LOG(ERROR, TAG, "jni_mid_addDescriptor is null");
1112 return CA_STATUS_FAILED;
1115 jboolean jni_boolean_addDescriptor = (*env)->CallBooleanMethod(env, characteristic,
1116 jni_mid_addDescriptor,
1117 jni_readDescriptor);
1119 if (JNI_FALSE == jni_boolean_addDescriptor)
1121 OIC_LOG(ERROR, TAG, "addDescriptor has failed");
1122 return CA_STATUS_FAILED;
1126 OIC_LOG(DEBUG, TAG, "addDescriptor success");
1129 OIC_LOG(DEBUG, TAG, "OUT - CALEServerAddDescriptor");
1130 return CA_STATUS_OK;
1133 CAResult_t CALEServerAddGattService(JNIEnv *env, jobject bluetoothGattServer,
1134 jobject bluetoothGattService)
1136 OIC_LOG(DEBUG, TAG, "IN - CALEServerAddGattService");
1137 VERIFY_NON_NULL(env, TAG, "env is null");
1138 VERIFY_NON_NULL(bluetoothGattServer, TAG, "bluetoothGattServer is null");
1139 VERIFY_NON_NULL(bluetoothGattService, TAG, "bluetoothGattService is null");
1141 if (!CALEIsEnableBTAdapter(env))
1143 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1144 return CA_ADAPTER_NOT_ENABLED;
1147 jclass jni_cid_bluetoothGattServer = (*env)->FindClass(env,
1148 "android/bluetooth/BluetoothGattServer");
1149 if (!jni_cid_bluetoothGattServer)
1151 OIC_LOG(ERROR, TAG, "jni_cid_bluetoothGattServer is null");
1152 return CA_STATUS_FAILED;
1155 jmethodID jni_mid_addService = (*env)->GetMethodID(env, jni_cid_bluetoothGattServer,
1157 "(Landroid/bluetooth/BluetoothGattService;)"
1159 if (!jni_mid_addService)
1161 OIC_LOG(ERROR, TAG, "jni_mid_addService is null");
1162 return CA_STATUS_FAILED;
1165 jboolean jni_boolean_addService = (*env)->CallBooleanMethod(env, bluetoothGattServer,
1167 bluetoothGattService);
1169 if (JNI_FALSE == jni_boolean_addService)
1171 OIC_LOG(ERROR, TAG, "Fail to add GATT service");
1172 return CA_STATUS_FAILED;
1175 OIC_LOG(DEBUG, TAG, "OUT - CALEServerAddGattService");
1176 return CA_STATUS_OK;
1179 CAResult_t CALEServerConnect(JNIEnv *env, jobject bluetoothDevice)
1181 OIC_LOG(DEBUG, TAG, "IN - CALEServerConnect");
1182 VERIFY_NON_NULL(env, TAG, "env is null");
1183 VERIFY_NON_NULL(bluetoothDevice, TAG, "bluetoothDevice is null");
1185 if (!CALEIsEnableBTAdapter(env))
1187 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1188 return CA_ADAPTER_NOT_ENABLED;
1191 jclass jni_cid_bluetoothGattServer = (*env)->FindClass(env,
1192 "android/bluetooth/BluetoothGattServer");
1193 if (!jni_cid_bluetoothGattServer)
1195 OIC_LOG(ERROR, TAG, "jni_cid_bluetoothGattServer is null");
1196 return CA_STATUS_FAILED;
1199 jmethodID jni_mid_connect = (*env)->GetMethodID(env, jni_cid_bluetoothGattServer, "connect",
1200 "(Landroid/bluetooth/BluetoothDevice;Z)Z");
1201 if (!jni_mid_connect)
1203 OIC_LOG(ERROR, TAG, "jni_mid_connect is null");
1204 return CA_STATUS_FAILED;
1207 jboolean jni_boolean_connect = (*env)->CallBooleanMethod(env, g_bluetoothGattServer,
1208 jni_mid_connect, bluetoothDevice,
1210 if (JNI_FALSE == jni_boolean_connect)
1212 OIC_LOG(ERROR, TAG, "Fail to connect");
1213 return CA_STATUS_FAILED;
1216 OIC_LOG(DEBUG, TAG, "OUT - CALEServerConnect");
1217 return CA_STATUS_OK;
1220 CAResult_t CALEServerDisconnectAllDevices(JNIEnv *env)
1222 OIC_LOG(DEBUG, TAG, "IN - CALEServerDisconnectAllDevices");
1223 VERIFY_NON_NULL(env, TAG, "env is null");
1225 ca_mutex_lock(g_connectedDeviceListMutex);
1226 if (!g_connectedDeviceList)
1228 OIC_LOG(ERROR, TAG, "g_connectedDeviceList is null");
1229 ca_mutex_unlock(g_connectedDeviceListMutex);
1230 return CA_STATUS_FAILED;
1233 uint32_t length = u_arraylist_length(g_connectedDeviceList);
1234 for (uint32_t index = 0; index < length; index++)
1236 jobject jarrayObj = (jobject) u_arraylist_get(g_connectedDeviceList, index);
1239 OIC_LOG_V(ERROR, TAG, "object[%d] is null", index);
1243 // disconnect for device obj
1244 CAResult_t res = CALEServerDisconnect(env, jarrayObj);
1245 if (CA_STATUS_OK != res)
1247 OIC_LOG_V(ERROR, TAG, "Disconnect for this device[%d] has failed", index);
1252 ca_mutex_unlock(g_connectedDeviceListMutex);
1253 OIC_LOG(DEBUG, TAG, "OUT - CALEServerDisconnectAllDevices");
1254 return CA_STATUS_OK;
1257 CAResult_t CALEServerDisconnect(JNIEnv *env, jobject bluetoothDevice)
1259 OIC_LOG(DEBUG, TAG, "IN - CALEServerDisconnect");
1260 VERIFY_NON_NULL(env, TAG, "env is null");
1261 VERIFY_NON_NULL(bluetoothDevice, TAG, "bluetoothDevice is null");
1263 if (!CALEIsEnableBTAdapter(env))
1265 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1266 return CA_ADAPTER_NOT_ENABLED;
1269 jclass jni_cid_bluetoothGattServer = (*env)->FindClass(env,
1270 "android/bluetooth/BluetoothGattServer");
1271 if (!jni_cid_bluetoothGattServer)
1273 OIC_LOG(ERROR, TAG, "jni_cid_bluetoothGattServer is null");
1274 return CA_STATUS_FAILED;
1277 jmethodID jni_mid_cancelConnection = (*env)->GetMethodID(env, jni_cid_bluetoothGattServer,
1279 "(Landroid/bluetooth/BluetoothDevice;)"
1281 if (!jni_mid_cancelConnection)
1283 OIC_LOG(ERROR, TAG, "jni_mid_cancelConnection is null");
1284 return CA_STATUS_FAILED;
1287 (*env)->CallVoidMethod(env, g_bluetoothGattServer, jni_mid_cancelConnection, bluetoothDevice);
1289 if ((*env)->ExceptionCheck(env))
1291 OIC_LOG(ERROR, TAG, "cancelConnection has failed");
1292 (*env)->ExceptionDescribe(env);
1293 (*env)->ExceptionClear(env);
1294 return CA_STATUS_FAILED;
1297 OIC_LOG(DEBUG, TAG, "OUT - CALEServerDisconnect");
1298 return CA_STATUS_OK;
1301 CAResult_t CALEServerGattClose(JNIEnv *env, jobject bluetoothGattServer)
1304 OIC_LOG(DEBUG, TAG, "GattServer Close");
1305 VERIFY_NON_NULL(bluetoothGattServer, TAG, "bluetoothGattServer is null");
1306 VERIFY_NON_NULL(env, TAG, "env is null");
1308 // get BluetoothGatt class
1309 OIC_LOG(DEBUG, TAG, "get BluetoothGatt class");
1310 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, "android/bluetooth/BluetoothGattServer");
1311 if (!jni_cid_BluetoothGatt)
1313 OIC_LOG(ERROR, TAG, "jni_cid_BluetoothGatt is null");
1314 return CA_STATUS_FAILED;
1317 jmethodID jni_mid_closeGatt = (*env)->GetMethodID(env, jni_cid_BluetoothGatt, "close", "()V");
1318 if (!jni_mid_closeGatt)
1320 OIC_LOG(ERROR, TAG, "jni_mid_closeGatt is null");
1321 return CA_STATUS_OK;
1324 // call disconnect gatt method
1325 OIC_LOG(DEBUG, TAG, "request to close GATT");
1326 (*env)->CallVoidMethod(env, bluetoothGattServer, jni_mid_closeGatt);
1328 if ((*env)->ExceptionCheck(env))
1330 OIC_LOG(ERROR, TAG, "closeGATT has failed");
1331 (*env)->ExceptionDescribe(env);
1332 (*env)->ExceptionClear(env);
1333 return CA_STATUS_FAILED;
1336 return CA_STATUS_OK;
1339 CAResult_t CALEServerSend(JNIEnv *env, jobject bluetoothDevice, jbyteArray responseData)
1341 OIC_LOG(DEBUG, TAG, "IN - CALEServerSend");
1342 VERIFY_NON_NULL(env, TAG, "env is null");
1343 VERIFY_NON_NULL(bluetoothDevice, TAG, "bluetoothDevice is null");
1344 VERIFY_NON_NULL(responseData, TAG, "responseData is null");
1346 if (!CALEIsEnableBTAdapter(env))
1348 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1349 return CA_ADAPTER_NOT_ENABLED;
1352 jobject responseChar = CALEServerSetResponseData(env, responseData);
1355 OIC_LOG(ERROR, TAG, "responseChar is null");
1356 return CA_STATUS_FAILED;
1359 CAResult_t result = CALEServerSendResponseData(env, bluetoothDevice, responseChar);
1360 if (CA_STATUS_OK != result)
1362 OIC_LOG(ERROR, TAG, "Fail to send response data");
1366 OIC_LOG(DEBUG, TAG, "OUT - CALEServerSend");
1370 CAResult_t CALEServerInitialize(ca_thread_pool_t handle)
1372 OIC_LOG(DEBUG, TAG, "IN - CALEServerInitialize");
1374 CALeServerJniInit();
1378 OIC_LOG(ERROR, TAG, "g_jvm is null");
1379 return CA_STATUS_FAILED;
1382 bool isAttached = false;
1384 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
1387 OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
1388 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
1392 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
1393 return CA_STATUS_FAILED;
1398 CAResult_t ret = CALECheckPlatformVersion(env, 21);
1399 if (CA_STATUS_OK != ret)
1401 OIC_LOG(ERROR, TAG, "it is not supported");
1405 (*g_jvm)->DetachCurrentThread(g_jvm);
1410 g_threadPoolHandle = handle;
1412 ret = CALEServerInitMutexVaraibles();
1413 if (CA_STATUS_OK != ret)
1415 OIC_LOG(ERROR, TAG, "CALEServerInitMutexVaraibles has failed");
1419 (*g_jvm)->DetachCurrentThread(g_jvm);
1421 return CA_STATUS_FAILED;
1424 CALEServerJNISetContext();
1425 CALEServerCreateCachedDeviceList();
1427 ret = CALEServerCreateJniInterfaceObject();
1428 if (CA_STATUS_OK != ret)
1430 OIC_LOG(ERROR, TAG, "CALEServerCreateJniInterfaceObject has failed");
1434 (*g_jvm)->DetachCurrentThread(g_jvm);
1436 return CA_STATUS_FAILED;
1441 (*g_jvm)->DetachCurrentThread(g_jvm);
1444 g_isInitializedServer = true;
1445 OIC_LOG(DEBUG, TAG, "OUT - CALEServerInitialize");
1446 return CA_STATUS_OK;
1449 void CALEServerTerminate()
1451 OIC_LOG(DEBUG, TAG, "IN - CALEServerTerminate");
1455 OIC_LOG(ERROR, TAG, "g_jvm is null");
1459 bool isAttached = false;
1461 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
1464 OIC_LOG(ERROR, TAG, "Could not get JNIEnv pointer");
1465 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
1469 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
1475 CAResult_t ret = CALEServerStopMulticastServer(0);
1476 if (CA_STATUS_OK != ret)
1478 OIC_LOG(ERROR, TAG, "CALEServerStopMulticastServer has failed");
1481 ret = CALEServerDisconnectAllDevices(env);
1482 if (CA_STATUS_OK != ret)
1484 OIC_LOG(ERROR, TAG, "CALEServerDisconnectAllDevices has failed");
1487 ret = CALEServerRemoveAllDevices(env);
1488 if (CA_STATUS_OK != ret)
1490 OIC_LOG(ERROR, TAG, "CALEServerRemoveAllDevices has failed");
1493 if (g_leAdvertiseCallback)
1495 (*env)->DeleteGlobalRef(env, g_leAdvertiseCallback);
1498 if (g_bluetoothGattServer)
1500 (*env)->DeleteGlobalRef(env, g_bluetoothGattServer);
1503 if (g_bluetoothGattServerCallback)
1505 (*env)->DeleteGlobalRef(env, g_bluetoothGattServerCallback);
1508 CALEServerTerminateMutexVaraibles();
1509 CALEServerTerminateConditionVaraibles();
1511 g_isStartServer = false;
1512 g_isInitializedServer = false;
1516 (*g_jvm)->DetachCurrentThread(g_jvm);
1519 OIC_LOG(DEBUG, TAG, "OUT - CALEServerTerminate");
1522 CAResult_t CALEServerSendUnicastMessage(const char* address, const char* data, uint32_t dataLen)
1524 OIC_LOG_V(DEBUG, TAG, "IN - CALEServerSendUnicastMessage(%s, %s)", address, data);
1525 VERIFY_NON_NULL(address, TAG, "address is null");
1526 VERIFY_NON_NULL(data, TAG, "data is null");
1530 OIC_LOG(ERROR, TAG, "g_jvm is null");
1531 return CA_STATUS_FAILED;
1534 bool isAttached = false;
1536 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
1539 OIC_LOG(ERROR, TAG, "Could not get JNIEnv pointer");
1540 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
1544 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
1545 return CA_STATUS_FAILED;
1550 CAResult_t ret = CALEServerSendUnicastMessageImpl(env, address, data, dataLen);
1551 if (CA_STATUS_OK != ret)
1553 OIC_LOG(ERROR, TAG, "CALEServerSendUnicastMessageImpl has failed");
1558 (*g_jvm)->DetachCurrentThread(g_jvm);
1561 OIC_LOG(DEBUG, TAG, "OUT - CALEServerSendUnicastMessage");
1565 CAResult_t CALEServerSendMulticastMessage(const char* data, uint32_t dataLen)
1567 OIC_LOG_V(DEBUG, TAG, "IN - CALEServerSendMulticastMessage(%s)", data);
1568 VERIFY_NON_NULL(data, TAG, "data is null");
1572 OIC_LOG(ERROR, TAG, "g_jvm is null");
1573 return CA_STATUS_FAILED;
1576 bool isAttached = false;
1578 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
1581 OIC_LOG(ERROR, TAG, "Could not get JNIEnv pointer");
1582 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
1586 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
1587 return CA_STATUS_FAILED;
1592 CAResult_t ret = CALEServerSendMulticastMessageImpl(env, data, dataLen);
1593 if (CA_STATUS_OK != ret)
1595 OIC_LOG(ERROR, TAG, "CALEServerSendMulticastMessageImpl has failed");
1600 (*g_jvm)->DetachCurrentThread(g_jvm);
1603 OIC_LOG(DEBUG, TAG, "OUT - CALEServerSendMulticastMessage");
1607 CAResult_t CALEServerStartMulticastServer()
1609 OIC_LOG(DEBUG, TAG, "IN - CALEServerStartMulticastServer");
1611 if (!g_isInitializedServer)
1613 OIC_LOG(INFO, TAG, "server is not initialized");
1614 return CA_STATUS_FAILED;
1617 if (g_isStartServer)
1619 OIC_LOG(INFO, TAG, "server is already started..it will be skipped");
1620 return CA_STATUS_FAILED;
1625 OIC_LOG(ERROR, TAG, "g_jvm is null");
1626 return CA_STATUS_FAILED;
1629 bool isAttached = false;
1631 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
1634 OIC_LOG(ERROR, TAG, "Could not get JNIEnv pointer");
1635 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
1639 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
1640 return CA_STATUS_FAILED;
1645 g_isStartServer = true;
1647 // start gatt server
1648 CAResult_t ret = CALEServerStartGattServer(env, g_bluetoothGattServerCallback);
1649 if (CA_STATUS_OK != ret)
1651 OIC_LOG(ERROR, TAG, "Fail to start gatt server");
1656 ret = CALEServerStartAdvertise(env, g_leAdvertiseCallback);
1657 if (CA_STATUS_OK != ret)
1659 OIC_LOG(ERROR, TAG, "CALEServerStartAdvertise has failed");
1664 (*g_jvm)->DetachCurrentThread(g_jvm);
1667 OIC_LOG(DEBUG, TAG, "OUT - CALEServerStartMulticastServer");
1671 CAResult_t CALEServerStopMulticastServer()
1673 OIC_LOG(DEBUG, TAG, "IN - CALEServerStopMulticastServer");
1675 if (false == g_isStartServer)
1677 OIC_LOG(INFO, TAG, "server is already stopped..it will be skipped");
1678 return CA_STATUS_FAILED;
1683 OIC_LOG(ERROR, TAG, "g_jvm is null");
1684 return CA_STATUS_FAILED;
1687 bool isAttached = false;
1689 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
1692 OIC_LOG(ERROR, TAG, "Could not get JNIEnv pointer");
1693 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
1697 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
1698 return CA_STATUS_FAILED;
1703 CAResult_t ret = CALEServerStopAdvertise(env, g_leAdvertiseCallback);
1704 if (CA_STATUS_OK != ret)
1706 OIC_LOG(ERROR, TAG, "CALEServerStopAdvertise has failed");
1709 g_isStartServer = false;
1713 (*g_jvm)->DetachCurrentThread(g_jvm);
1716 OIC_LOG(DEBUG, TAG, "OUT - CALEServerStopMulticastServer");
1720 void CALEServerSetCallback(CAPacketReceiveCallback callback)
1722 OIC_LOG(DEBUG, TAG, "CALEServerSetCallback");
1723 g_packetReceiveCallback = callback;
1726 CAResult_t CALEServerSendUnicastMessageImpl(JNIEnv *env, const char* address, const char* data,
1729 OIC_LOG_V(DEBUG, TAG, "IN - CALEServerSendUnicastMessageImpl, address: %s, data: %s",
1731 VERIFY_NON_NULL(env, TAG, "env is null");
1732 VERIFY_NON_NULL(address, TAG, "address is null");
1733 VERIFY_NON_NULL(data, TAG, "data is null");
1735 if (!g_connectedDeviceList)
1737 OIC_LOG(ERROR, TAG, "g_connectedDeviceList is null");
1738 return CA_STATUS_FAILED;
1741 jobject jni_obj_bluetoothDevice = NULL;
1742 uint32_t length = u_arraylist_length(g_connectedDeviceList);
1743 for (uint32_t index = 0; index < length; index++)
1745 OIC_LOG(DEBUG, TAG, "check device address");
1746 jobject jarrayObj = (jobject) u_arraylist_get(g_connectedDeviceList, index);
1749 OIC_LOG(ERROR, TAG, "jarrayObj is null");
1750 return CA_STATUS_FAILED;
1753 jstring jni_setAddress = CALEGetAddressFromBTDevice(env, jarrayObj);
1754 if (!jni_setAddress)
1756 OIC_LOG(ERROR, TAG, "jni_setAddress is null");
1757 return CA_STATUS_FAILED;
1759 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
1762 OIC_LOG(ERROR, TAG, "setAddress is null");
1763 return CA_STATUS_FAILED;
1766 OIC_LOG_V(DEBUG, TAG, "setAddress : %s", setAddress);
1767 OIC_LOG_V(DEBUG, TAG, "address : %s", address);
1769 if (!strcmp(setAddress, address))
1771 OIC_LOG(DEBUG, TAG, "found the device");
1772 jni_obj_bluetoothDevice = jarrayObj;
1773 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
1776 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
1779 if (jni_obj_bluetoothDevice)
1781 jbyteArray jni_bytearr_data = (*env)->NewByteArray(env, dataLen);
1782 (*env)->SetByteArrayRegion(env, jni_bytearr_data, 0, dataLen, (jbyte*) data);
1784 CAResult_t res = CALEServerSend(env, jni_obj_bluetoothDevice, jni_bytearr_data);
1785 if (CA_STATUS_OK != res)
1787 OIC_LOG(ERROR, TAG, "send has failed");
1788 return CA_SEND_FAILED;
1793 OIC_LOG(ERROR, TAG, "There are no device to send in the list");
1794 return CA_STATUS_FAILED;
1797 OIC_LOG(DEBUG, TAG, "OUT - CALEServerSendUnicastMessageImpl");
1798 return CA_STATUS_OK;
1801 CAResult_t CALEServerSendMulticastMessageImpl(JNIEnv *env, const char *data, uint32_t dataLen)
1803 OIC_LOG_V(DEBUG, TAG, "IN - CALEServerSendMulticastMessageImpl, send to, data: %s", data);
1804 VERIFY_NON_NULL(env, TAG, "env is null");
1805 VERIFY_NON_NULL(data, TAG, "data is null");
1807 if (!g_connectedDeviceList)
1809 OIC_LOG(ERROR, TAG, "g_connectedDeviceList is null");
1810 return CA_STATUS_FAILED;
1813 uint32_t length = u_arraylist_length(g_connectedDeviceList);
1814 for (uint32_t index = 0; index < length; index++)
1816 jobject jarrayObj = (jobject) u_arraylist_get(g_connectedDeviceList, index);
1819 OIC_LOG(ERROR, TAG, "jarrayObj is null");
1820 return CA_STATUS_FAILED;
1823 // send data for all device
1824 jbyteArray jni_bytearr_data = (*env)->NewByteArray(env, dataLen);
1825 (*env)->SetByteArrayRegion(env, jni_bytearr_data, 0, dataLen, (jbyte*) data);
1826 CAResult_t res = CALEServerSend(env, jarrayObj, jni_bytearr_data);
1827 if (CA_STATUS_OK != res)
1829 OIC_LOG(ERROR, TAG, "send has failed");
1830 return CA_SEND_FAILED;
1834 OIC_LOG(DEBUG, TAG, "OUT - CALEServerSendMulticastMessageImpl");
1835 return CA_STATUS_OK;
1838 void CALEServerCreateCachedDeviceList()
1840 OIC_LOG(DEBUG, TAG, "IN - CALEServerCreateCachedDeviceList");
1842 ca_mutex_lock(g_connectedDeviceListMutex);
1843 // create new object array
1844 if (!g_connectedDeviceList)
1846 OIC_LOG(DEBUG, TAG, "Create device list");
1847 g_connectedDeviceList = u_arraylist_create();
1849 ca_mutex_unlock(g_connectedDeviceListMutex);
1851 OIC_LOG(DEBUG, TAG, "OUT - CALEServerCreateCachedDeviceList");
1854 bool CALEServerIsDeviceInList(JNIEnv *env, const char* remoteAddress)
1856 OIC_LOG(DEBUG, TAG, "IN - CALEServerIsDeviceInList");
1857 VERIFY_NON_NULL_RET(env, TAG, "env is null", false);
1858 VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", false);
1860 if (!g_connectedDeviceList)
1862 OIC_LOG(ERROR, TAG, "list is null");
1866 uint32_t length = u_arraylist_length(g_connectedDeviceList);
1867 for (uint32_t index = 0; index < length; index++)
1869 jobject jarrayObj = (jobject) u_arraylist_get(g_connectedDeviceList, index);
1873 OIC_LOG(ERROR, TAG, "jarrayObj is null");
1877 jstring jni_setAddress = CALEGetAddressFromBTDevice(env, jarrayObj);
1878 if (!jni_setAddress)
1880 OIC_LOG(ERROR, TAG, "jni_setAddress is null");
1884 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
1887 OIC_LOG(ERROR, TAG, "setAddress is null");
1891 if (!strcmp(remoteAddress, setAddress))
1893 OIC_LOG(ERROR, TAG, "the device is already set");
1894 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
1899 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
1904 OIC_LOG(DEBUG, TAG, "there are no device in the list");
1905 OIC_LOG(DEBUG, TAG, "OUT - CALEServerCreateCachedDeviceList");
1909 CAResult_t CALEServerAddDeviceToList(JNIEnv *env, jobject device)
1911 OIC_LOG(DEBUG, TAG, "IN - CALEServerAddDeviceToList");
1912 VERIFY_NON_NULL(device, TAG, "device is null");
1913 VERIFY_NON_NULL(env, TAG, "env is null");
1915 ca_mutex_lock(g_connectedDeviceListMutex);
1917 if (!g_connectedDeviceList)
1919 OIC_LOG(ERROR, TAG, "list is null");
1920 ca_mutex_unlock(g_connectedDeviceListMutex);
1921 return CA_STATUS_FAILED;
1924 jstring jni_remoteAddress = CALEGetAddressFromBTDevice(env, device);
1925 if (!jni_remoteAddress)
1927 OIC_LOG(ERROR, TAG, "jni_remoteAddress is null");
1928 ca_mutex_unlock(g_connectedDeviceListMutex);
1929 return CA_STATUS_FAILED;
1932 const char* remoteAddress = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);
1935 OIC_LOG(ERROR, TAG, "remoteAddress is null");
1936 ca_mutex_unlock(g_connectedDeviceListMutex);
1937 return CA_STATUS_FAILED;
1940 if (false == CALEServerIsDeviceInList(env, remoteAddress))
1942 jobject jni_obj_device = (*env)->NewGlobalRef(env, device);
1943 u_arraylist_add(g_connectedDeviceList, jni_obj_device);
1944 OIC_LOG_V(DEBUG, TAG, "Set the object to ArrayList as Element : %s", remoteAddress);
1947 (*env)->ReleaseStringUTFChars(env, jni_remoteAddress, remoteAddress);
1948 ca_mutex_unlock(g_connectedDeviceListMutex);
1949 OIC_LOG(DEBUG, TAG, "OUT - CALEServerAddDeviceToList");
1950 return CA_STATUS_OK;
1953 CAResult_t CALEServerRemoveAllDevices(JNIEnv *env)
1955 OIC_LOG(DEBUG, TAG, "IN - CALEServerRemoveAllDevices");
1956 VERIFY_NON_NULL(env, TAG, "env is null");
1958 ca_mutex_lock(g_connectedDeviceListMutex);
1959 if (!g_connectedDeviceList)
1961 OIC_LOG(ERROR, TAG, "g_connectedDeviceList is null");
1962 ca_mutex_unlock(g_connectedDeviceListMutex);
1963 return CA_STATUS_FAILED;
1966 uint32_t length = u_arraylist_length(g_connectedDeviceList);
1967 for (uint32_t index = 0; index < length; index++)
1969 jobject jarrayObj = (jobject) u_arraylist_get(g_connectedDeviceList, index);
1972 (*env)->DeleteGlobalRef(env, jarrayObj);
1976 OICFree(g_connectedDeviceList);
1977 g_connectedDeviceList = NULL;
1978 ca_mutex_unlock(g_connectedDeviceListMutex);
1980 OIC_LOG(DEBUG, TAG, "OUT - CALEServerRemoveAllDevices");
1981 return CA_STATUS_OK;
1984 CAResult_t CALEServerRemoveDevice(JNIEnv *env, jstring address)
1986 OIC_LOG(DEBUG, TAG, "IN CALEServerRemoveDevice");
1987 VERIFY_NON_NULL(env, TAG, "env is null");
1988 VERIFY_NON_NULL(address, TAG, "address is null");
1990 ca_mutex_lock(g_connectedDeviceListMutex);
1991 if (!g_connectedDeviceList)
1993 OIC_LOG(ERROR, TAG, "no deviceList");
1994 ca_mutex_unlock(g_connectedDeviceListMutex);
1995 return CA_STATUS_FAILED;
1998 uint32_t length = u_arraylist_length(g_connectedDeviceList);
1999 for (uint32_t index = 0; index < length; index++)
2001 jobject jarrayObj = (jobject) u_arraylist_get(g_connectedDeviceList, index);
2005 jstring jni_setAddress = CALEGetAddressFromBTDevice(env, jarrayObj);
2006 if (!jni_setAddress)
2008 OIC_LOG(ERROR, TAG, "wrong device address");
2011 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
2014 OIC_LOG(ERROR, TAG, "setAddress is null");
2018 const char* remoteAddress = (*env)->GetStringUTFChars(env, address, NULL);
2021 OIC_LOG(ERROR, TAG, "remoteAddress is null");
2022 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2026 if (!strcmp(setAddress, remoteAddress))
2028 OIC_LOG_V(DEBUG, TAG, "device address : %s", remoteAddress);
2030 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2031 (*env)->ReleaseStringUTFChars(env, address, remoteAddress);
2032 (*env)->DeleteGlobalRef(env, jarrayObj);
2034 CAResult_t res = CALEServerReorderinglist(index);
2035 if (CA_STATUS_OK != res)
2037 OIC_LOG(ERROR, TAG, "CALEServerReorderinglist has failed");
2038 ca_mutex_unlock(g_connectedDeviceListMutex);
2041 ca_mutex_unlock(g_connectedDeviceListMutex);
2042 return CA_STATUS_OK;
2044 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2045 (*env)->ReleaseStringUTFChars(env, address, remoteAddress);
2049 ca_mutex_unlock(g_connectedDeviceListMutex);
2051 OIC_LOG(DEBUG, TAG, "there are no device in the device list");
2053 OIC_LOG(DEBUG, TAG, "IN CALEServerRemoveDevice");
2054 return CA_STATUS_FAILED;
2057 CAResult_t CALEServerReorderinglist(uint32_t index)
2059 if (!g_connectedDeviceList)
2061 OIC_LOG(ERROR, TAG, "g_connectedDeviceList is null");
2062 return CA_STATUS_FAILED;
2065 if (index >= g_connectedDeviceList->length)
2067 OIC_LOG(ERROR, TAG, "index is not available");
2068 return CA_STATUS_FAILED;
2071 if (index < g_connectedDeviceList->length - 1)
2073 memmove(&g_connectedDeviceList->data[index], &g_connectedDeviceList->data[index + 1],
2074 (g_connectedDeviceList->length - index - 1) * sizeof(void *));
2077 g_connectedDeviceList->size--;
2078 g_connectedDeviceList->length--;
2080 return CA_STATUS_OK;
2083 JNIEXPORT void JNICALL
2084 Java_org_iotivity_ca_CaLeServerInterface_caLeRegisterGattServerCallback(JNIEnv *env, jobject obj,
2087 OIC_LOG(DEBUG, TAG, "CaLeServerInterface - Register Le Gatt Server Callback");
2088 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
2089 VERIFY_NON_NULL_VOID(callback, TAG, "callback is null");
2091 g_bluetoothGattServerCallback = (*env)->NewGlobalRef(env, callback);
2094 JNIEXPORT void JNICALL
2095 Java_org_iotivity_ca_CaLeServerInterface_caLeRegisterBluetoothLeAdvertiseCallback(JNIEnv *env,
2099 OIC_LOG(DEBUG, TAG, "CaLeServerInterface - Register Le Advertise Callback");
2100 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
2101 VERIFY_NON_NULL_VOID(callback, TAG, "callback is null");
2103 g_leAdvertiseCallback = (*env)->NewGlobalRef(env, callback);
2106 JNIEXPORT void JNICALL
2107 Java_org_iotivity_ca_CaLeServerInterface_caLeGattServerConnectionStateChangeCallback(
2108 JNIEnv *env, jobject obj, jobject device, jint status, jint newState)
2110 OIC_LOG(DEBUG, TAG, "CaLeServerInterface - Gatt Server ConnectionStateChange Callback");
2111 OIC_LOG_V(DEBUG, TAG, "New connection State: %d", newState);
2113 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
2114 VERIFY_NON_NULL_VOID(device, TAG, "device is null");
2116 jclass jni_cid_bluetoothProfile = (*env)->FindClass(env, "android/bluetooth/BluetoothProfile");
2117 if (!jni_cid_bluetoothProfile)
2119 OIC_LOG(ERROR, TAG, "jni_cid_bluetoothProfile is null");
2123 jfieldID jni_fid_state_connected = (*env)->GetStaticFieldID(env, jni_cid_bluetoothProfile,
2124 "STATE_CONNECTED", "I");
2125 if(!jni_fid_state_connected)
2127 OIC_LOG(ERROR, TAG, "jni_fid_state_connected is null");
2131 jfieldID jni_fid_state_disconnected = (*env)->GetStaticFieldID(env, jni_cid_bluetoothProfile,
2132 "STATE_DISCONNECTED", "I");
2133 if(!jni_fid_state_disconnected)
2135 OIC_LOG(ERROR, TAG, "jni_fid_state_disconnected is null");
2140 jint jni_int_state_connected = (*env)->GetStaticIntField(env, jni_cid_bluetoothProfile,
2141 jni_fid_state_connected);
2143 // STATE_DISCONNECTED
2144 jint jni_int_state_disconnected = (*env)->GetStaticIntField(env, jni_cid_bluetoothProfile,
2145 jni_fid_state_disconnected);
2147 if (newState == jni_int_state_connected)
2150 OIC_LOG(DEBUG, TAG, "LE CONNECTED");
2152 jstring jni_remoteAddress = CALEGetAddressFromBTDevice(env, device);
2153 if (!jni_remoteAddress)
2155 OIC_LOG(ERROR, TAG, "jni_remoteAddress is null");
2159 const char* remoteAddress = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);
2162 OIC_LOG(ERROR, TAG, "remoteAddress is null");
2166 if (false == CALEServerIsDeviceInList(env, remoteAddress))
2168 OIC_LOG(DEBUG, TAG, "add connected device to cache");
2169 CALEServerAddDeviceToList(env, device);
2171 (*env)->ReleaseStringUTFChars(env, jni_remoteAddress, remoteAddress);
2173 else if (newState == jni_int_state_disconnected)
2175 OIC_LOG(DEBUG, TAG, "LE DISCONNECTED");
2176 CAResult_t res = CALEServerGattClose(env, g_bluetoothGattServer);
2177 if (CA_STATUS_OK != res)
2179 OIC_LOG(ERROR, TAG, "CALEServerGattClose has failed");
2184 OIC_LOG_V(DEBUG, TAG, "LE Connection state is [newState : %d, status %d]", newState,
2189 JNIEXPORT void JNICALL
2190 Java_org_iotivity_ca_CaLeServerInterface_caLeGattServerServiceAddedCallback(JNIEnv *env,
2193 jobject gattService)
2195 OIC_LOG_V(DEBUG, TAG, "CaLeServerInterface - Gatt Service Added Callback(%d)", status);
2198 JNIEXPORT void JNICALL
2199 Java_org_iotivity_ca_CaLeServerInterface_caLeGattServerCharacteristicReadRequestCallback(
2200 JNIEnv *env, jobject obj, jobject device, jint requestId, jint offset,
2201 jobject characteristic, jbyteArray data)
2203 OIC_LOG(DEBUG, TAG, "CaLeServerInterface - Gatt Server Characteristic Read Request Callback");
2204 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
2205 VERIFY_NON_NULL_VOID(device, TAG, "device is null");
2207 #ifdef USE_PROPERTY_WRITE_RESPONSE
2208 CALEServerSendResponse(env, device, requestId, 0, offset, NULL);
2213 JNIEXPORT void JNICALL
2214 Java_org_iotivity_ca_CaLeServerInterface_caLeGattServerCharacteristicWriteRequestCallback(
2215 JNIEnv *env, jobject obj, jobject device, jint requestId, jobject characteristic,
2216 jbyteArray data, jboolean preparedWrite, jboolean responseNeeded, jint offset,
2219 OIC_LOG(DEBUG, TAG, "CaLeServerInterface - Gatt Server Characteristic Write Request Callback");
2220 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
2221 VERIFY_NON_NULL_VOID(device, TAG, "device is null");
2222 VERIFY_NON_NULL_VOID(value, TAG, "value is null");
2223 VERIFY_NON_NULL_VOID(data, TAG, "data is null");
2225 #ifdef USE_PROPERTY_WRITE_RESPONSE
2226 CALEServerSendResponse(env, device, requestId, 0, offset, value);
2229 // get Byte Array and covert to char*
2230 jint length = (*env)->GetArrayLength(env, data);
2233 jbyte *jni_byte_requestData = (jbyte *) (*env)->GetByteArrayElements(env, data, &isCopy);
2235 char* requestData = NULL;
2236 requestData = (char*) OICMalloc(sizeof(char) * length + 1);
2239 OIC_LOG(ERROR, TAG, "requestData is null");
2243 memcpy(requestData, (const char*) jni_byte_requestData, length);
2244 requestData[length] = '\0';
2245 (*env)->ReleaseByteArrayElements(env, data, jni_byte_requestData, JNI_ABORT);
2247 jstring jni_address = CALEGetAddressFromBTDevice(env, device);
2250 OIC_LOG(ERROR, TAG, "jni_address is null");
2251 OICFree(requestData);
2255 const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
2258 OIC_LOG(ERROR, TAG, "address is null");
2259 OICFree(requestData);
2263 OIC_LOG_V(DEBUG, TAG, "remote device address : %s, %s, %d", address, requestData, length);
2265 ca_mutex_lock(g_bleClientBDAddressMutex);
2266 uint32_t sentLength = 0;
2267 g_CABLEServerDataReceivedCallback(address, OIC_GATT_SERVICE_UUID, requestData, length,
2269 ca_mutex_unlock(g_bleClientBDAddressMutex);
2271 (*env)->ReleaseStringUTFChars(env, jni_address, address);
2274 JNIEXPORT void JNICALL
2275 Java_org_iotivity_ca_CaLeServerInterface_caLeGattServerDescriptorReadRequestCallback(
2276 JNIEnv *env, jobject obj, jobject device, jint requestId, jint offset, jobject descriptor)
2278 OIC_LOG(DEBUG, TAG, "CaLeServerInterface_CALeGattServerDescriptorReadRequestCallback");
2281 JNIEXPORT void JNICALL
2282 Java_org_iotivity_ca_CaLeServerInterface_caLeGattServerDescriptorWriteRequestCallback(
2283 JNIEnv *env, jobject obj, jobject device, jint requestId, jobject descriptor,
2284 jboolean preparedWrite, jboolean responseNeeded, jint offset, jbyteArray value)
2286 OIC_LOG(DEBUG, TAG, "CaLeServerInterface_CALeGattServerDescriptorWriteRequestCallback");
2289 JNIEXPORT void JNICALL
2290 Java_org_iotivity_ca_CaLeServerInterface_caLeGattServerExecuteWriteCallback(JNIEnv *env,
2296 OIC_LOG(DEBUG, TAG, "CaLeServerInterface_CALeGattServerExecuteWriteCallback");
2297 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
2298 VERIFY_NON_NULL_VOID(device, TAG, "device is null");
2300 // CALEServerSendResponse(env, device, requestId, 0, 0, NULL);
2303 JNIEXPORT void JNICALL
2304 Java_org_iotivity_ca_CaLeServerInterface_caLeGattServerNotificationSentCallback(JNIEnv *env,
2309 OIC_LOG(DEBUG, TAG, "CaLeServerInterface - Gatt Server Notification Sent Callback");
2312 JNIEXPORT void JNICALL
2313 Java_org_iotivity_ca_CaLeServerInterface_caLeAdvertiseStartSuccessCallback(
2314 JNIEnv *env, jobject obj, jobject settingsInEffect)
2316 OIC_LOG(DEBUG, TAG, "CaLeServerInterface - LE Advertise Start Success Callback");
2319 JNIEXPORT void JNICALL
2320 Java_org_iotivity_ca_CaLeServerInterface_caLeAdvertiseStartFailureCallback(JNIEnv *env,
2324 OIC_LOG_V(ERROR, TAG, "CaLeServerInterface - LE Advertise Start Failure Callback(%)",
2332 CAResult_t CAStartLEGattServer()
2334 OIC_LOG(DEBUG, TAG, "IN");
2336 CAResult_t ret = CALEServerInitMutexVaraibles();
2337 if (CA_STATUS_OK != ret)
2339 OIC_LOG(ERROR, TAG, "CALEServerInitMutexVaraibles has failed!");
2340 CALEServerTerminateMutexVaraibles();
2341 return CA_SERVER_NOT_STARTED;
2344 ret = CALEServerInitConditionVaraibles();
2345 if (CA_STATUS_OK != ret)
2347 OIC_LOG(ERROR, TAG, "CALEServerInitConditionVaraibles has failed!");
2348 CALEServerTerminateConditionVaraibles();
2349 return CA_SERVER_NOT_STARTED;
2352 // start gatt service
2353 CALEServerStartMulticastServer();
2355 OIC_LOG(DEBUG, TAG, "OUT");
2356 return CA_STATUS_OK;
2359 CAResult_t CAStopLEGattServer()
2361 OIC_LOG(DEBUG, TAG, "IN");
2363 OIC_LOG(DEBUG, TAG, "OUT");
2364 return CA_STATUS_OK;
2367 void CATerminateLEGattServer()
2369 OIC_LOG(DEBUG, TAG, "IN");
2371 OIC_LOG(DEBUG, TAG, "Terminat Gatt Server");
2372 CALEServerTerminate();
2374 OIC_LOG(DEBUG, TAG, "OUT");
2377 void CASetLEReqRespServerCallback(CABLEServerDataReceivedCallback callback)
2379 OIC_LOG(DEBUG, TAG, "IN");
2381 ca_mutex_lock(g_bleReqRespCbMutex);
2382 g_CABLEServerDataReceivedCallback = callback;
2383 ca_mutex_unlock(g_bleReqRespCbMutex);
2385 OIC_LOG(DEBUG, TAG, "OUT");
2388 void CASetBLEServerErrorHandleCallback(CABLEErrorHandleCallback callback)
2390 g_serverErrorCallback = callback;
2393 CAResult_t CAUpdateCharacteristicsToGattClient(const char* address, const char *charValue,
2394 const uint32_t charValueLen)
2396 CAResult_t result = CA_SEND_FAILED;
2397 OIC_LOG(DEBUG, TAG, "IN");
2398 VERIFY_NON_NULL(address, TAG, "env is null");
2399 VERIFY_NON_NULL(charValue, TAG, "device is null");
2403 OIC_LOG(DEBUG, TAG, "CALEServerSendUnicastData");
2404 result = CALEServerSendUnicastMessage(address, charValue, charValueLen);
2407 OIC_LOG(DEBUG, TAG, "OUT");
2412 CAResult_t CAUpdateCharacteristicsToAllGattClients(const char *charValue,
2413 uint32_t charValueLen)
2415 OIC_LOG(DEBUG, TAG, "IN");
2416 VERIFY_NON_NULL(charValue, TAG, "device is null");
2418 OIC_LOG(DEBUG, TAG, "CALEServerSendMulticastMessage");
2419 CAResult_t result = CALEServerSendMulticastMessage(charValue, charValueLen);
2421 OIC_LOG(DEBUG, TAG, "OUT");
2425 void CASetLEServerThreadPoolHandle(ca_thread_pool_t handle)
2427 OIC_LOG(DEBUG, TAG, "IN");
2429 CALEServerInitialize(handle);
2431 OIC_LOG(DEBUG, TAG, "OUT");
2434 CAResult_t CALEServerInitMutexVaraibles()
2436 OIC_LOG(DEBUG, TAG, "IN");
2437 if (NULL == g_bleReqRespCbMutex)
2439 g_bleReqRespCbMutex = ca_mutex_new();
2440 if (NULL == g_bleReqRespCbMutex)
2442 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
2443 return CA_STATUS_FAILED;
2447 if (NULL == g_bleClientBDAddressMutex)
2449 g_bleClientBDAddressMutex = ca_mutex_new();
2450 if (NULL == g_bleClientBDAddressMutex)
2452 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
2453 return CA_STATUS_FAILED;
2457 if (NULL == g_connectedDeviceListMutex)
2459 g_connectedDeviceListMutex = ca_mutex_new();
2460 if (NULL == g_connectedDeviceListMutex)
2462 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
2463 return CA_STATUS_FAILED;
2467 OIC_LOG(DEBUG, TAG, "OUT");
2468 return CA_STATUS_OK;
2471 CAResult_t CALEServerInitConditionVaraibles()
2473 OIC_LOG(DEBUG, TAG, "this method is not supported");
2474 return CA_STATUS_OK;
2477 void CALEServerTerminateMutexVaraibles()
2479 OIC_LOG(DEBUG, TAG, "IN");
2481 ca_mutex_free(g_bleReqRespCbMutex);
2482 g_bleReqRespCbMutex = NULL;
2484 ca_mutex_free(g_bleClientBDAddressMutex);
2485 g_bleClientBDAddressMutex = NULL;
2487 ca_mutex_free(g_connectedDeviceListMutex);
2488 g_connectedDeviceListMutex = NULL;
2490 OIC_LOG(DEBUG, TAG, "OUT");
2493 void CALEServerTerminateConditionVaraibles()
2495 OIC_LOG(DEBUG, TAG, "this method is not supported");