[CA] [All] Klockwork , Prevent , Memory leaks fixed
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / bt_le_adapter / android / caleserver.c
1 #include <jni.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <android/log.h>
5 #include "caleserver.h"
6 #include "caleutils.h"
7 #include "logger.h"
8 #include "oic_malloc.h"
9 #include "uthreadpool.h"
10 #include "uarraylist.h"
11 #include "com_iotivity_jar_CALeInterface.h"
12
13 #define TAG PCF("CA_LE_SERVER")
14
15 #define  LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
16 #define  LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
17
18 /* Service UUID */
19 //static const char *OIC_GATT_SERVICE_UUID = "000018f3-0000-1000-8000-00805f9b34fb";
20 //static const char *OIC_GATT_CHARACTERISTIC_RESPONSE_UUID = "00002af5-0000-1000-8000-00805f9b34fb";  //read
21 //static const char *OIC_GATT_CHARACTERISTIC_REQUEST_UUID = "00002af6-0000-1000-8000-00805f9b34fb";  //write
22
23 static const char *OIC_GATT_SERVICE_UUID = "713d0000-503e-4c75-ba94-3148f18d941e";
24 static const char *OIC_GATT_CHARACTERISTIC_RESPONSE_UUID = "713d0002-503e-4c75-ba94-3148f18d941e"; //read
25 static const char *OIC_GATT_CHARACTERISTIC_REQUEST_UUID = "713d0003-503e-4c75-ba94-3148f18d941e"; //write
26
27 static JavaVM *g_jvm;
28 static jobject gContext;
29 static jobject gBluetoothGattServer;
30 static jobject gBluetoothGattServerCallback;
31 static jobject gLeAdvertiseCallback;
32
33 static CAPacketReceiveCallback gPacketReceiveCallback = NULL;
34 static u_arraylist_t *gConnectedDeviceList = NULL;
35 static u_thread_pool_t gThreadPoolHandle = NULL;
36
37 static jboolean gIsStartServer;
38
39 //getting context
40 void CALEServerJNISetContext(JNIEnv *env, jobject context)
41 {
42     OIC_LOG_V(DEBUG, TAG, "CALEServerJNISetContext");
43
44     if(context == NULL)
45         OIC_LOG_V(DEBUG, TAG, "context is null");
46
47     gContext = (*env)->NewGlobalRef(env, context);
48 }
49
50 //getting jvm
51 void CALeServerJniInit(JNIEnv *env, JavaVM *jvm)
52 {
53     OIC_LOG_V(DEBUG, TAG, "CALeServerJniInit");
54     g_jvm = jvm;
55 }
56
57 jobject CALEServerSetResponseData(JNIEnv *env, jbyteArray responseData)
58 {
59
60     OIC_LOG_V(DEBUG, TAG, "CALEServerSetResponseData");
61
62     jclass jni_cid_bluetoothGattServer = (*env)->FindClass(env,
63             "android/bluetooth/BluetoothGattServer");
64
65     jclass jni_cid_bluetoothGattService = (*env)->FindClass(env,
66             "android/bluetooth/BluetoothGattService");
67
68     jclass jni_cid_bluetoothGattCharacteristic = (*env)->FindClass(env,
69             "android/bluetooth/BluetoothGattCharacteristic");
70
71     jmethodID jni_mid_getService = (*env)->GetMethodID(env,
72             jni_cid_bluetoothGattServer, "getService",
73             "(Ljava/util/UUID;)Landroid/bluetooth/BluetoothGattService;");
74
75     jobject jni_obj_serviceUUID = CALEGetUuidFromString(env, OIC_GATT_SERVICE_UUID);
76
77     if (!gBluetoothGattServer)
78     {
79         OIC_LOG_V(DEBUG, TAG, "Check BluetoothGattServer status");
80         return NULL;
81     }
82     jobject jni_obj_bluetoothGattService = (*env)->CallObjectMethod(env,
83             gBluetoothGattServer, jni_mid_getService, jni_obj_serviceUUID);
84
85     jmethodID jni_mid_getCharacteristic =
86             (*env)->GetMethodID(env, jni_cid_bluetoothGattService,
87                     "getCharacteristic",
88                     "(Ljava/util/UUID;)Landroid/bluetooth/BluetoothGattCharacteristic;");
89
90     jobject jni_obj_responseUUID = CALEGetUuidFromString(env,
91             OIC_GATT_CHARACTERISTIC_RESPONSE_UUID);
92
93     jobject jni_obj_bluetoothGattCharacteristic = (*env)->CallObjectMethod(env,
94             jni_obj_bluetoothGattService, jni_mid_getCharacteristic,
95             jni_obj_responseUUID);
96
97     jmethodID jni_mid_setValue = (*env)->GetMethodID(env,
98             jni_cid_bluetoothGattCharacteristic, "setValue",
99             "([B)Z");
100
101     jboolean jni_boolean_setValue = (*env)->CallBooleanMethod(env,
102             jni_obj_bluetoothGattCharacteristic, jni_mid_setValue,
103             responseData);
104
105     if (jni_boolean_setValue == JNI_FALSE) {
106         OIC_LOG_V(DEBUG, TAG, "Fail to set response data");
107     }
108
109     return jni_obj_bluetoothGattCharacteristic;
110 }
111
112 jboolean CALEServerSendResponseData(JNIEnv *env, jobject device, jobject responseData)
113 {
114
115     OIC_LOG_V(DEBUG, TAG, "CALEServerSendResponseData");
116
117     jclass jni_cid_bluetoothGattServer = (*env)->FindClass(env,
118             "android/bluetooth/BluetoothGattServer");
119
120     jmethodID jni_mid_notifyCharacteristicChanged =
121             (*env)->GetMethodID(env, jni_cid_bluetoothGattServer,
122                     "notifyCharacteristicChanged",
123                     "(Landroid/bluetooth/BluetoothDevice;Landroid/bluetooth/BluetoothGattCharacteristic;Z)Z");
124
125     jboolean jni_boolean_notifyCharacteristicChanged =
126             (*env)->CallBooleanMethod(env, gBluetoothGattServer,
127                     jni_mid_notifyCharacteristicChanged, device, responseData,
128                     JNI_TRUE);
129
130     if (jni_boolean_notifyCharacteristicChanged == JNI_FALSE) {
131         OIC_LOG_V(DEBUG, TAG, "Fail to notify characteristic");
132     }
133
134     return jni_boolean_notifyCharacteristicChanged;
135 }
136
137 jboolean CALEServerSendResponse(JNIEnv *env, jobject device, jint requestId, jint status,
138         jint offset, jbyteArray value)
139 {
140
141     OIC_LOG_V(DEBUG, TAG, "CALEServerSendResponse");
142
143     jclass jni_cid_bluetoothGattServer = (*env)->FindClass(env,
144             "android/bluetooth/BluetoothGattServer");
145
146     jmethodID jni_mid_sendResponse = (*env)->GetMethodID(env,
147             jni_cid_bluetoothGattServer, "sendResponse",
148             "(Landroid/bluetooth/BluetoothDevice;III[B)Z");
149
150     jboolean jni_boolean_sendResponse = (*env)->CallBooleanMethod(env, gBluetoothGattServer,
151             jni_mid_sendResponse, device, requestId, status, offset, value);
152
153     if (jni_boolean_sendResponse == JNI_FALSE) {
154         OIC_LOG_V(DEBUG, TAG, "Fail to send response for gatt characteristic write request");
155     }
156
157     return jni_boolean_sendResponse;
158 }
159
160 void LEServerStartAdvertise(JNIEnv *env, jobject advertiseCallback)
161 {
162
163     OIC_LOG_V(DEBUG, TAG, "LEServerStartAdvertise");
164
165     jclass jni_cid_AdvertiseSettings = (*env)->FindClass(env,
166             "android/bluetooth/le/AdvertiseSettings$Builder");
167
168     jclass jni_cid_AdvertiseDataBuilder = (*env)->FindClass(env,
169             "android/bluetooth/le/AdvertiseData$Builder");
170
171     jclass jni_cid_BTAdapter = (*env)->FindClass(env,
172             "android/bluetooth/BluetoothAdapter");
173
174     jclass jni_cid_leAdvertiser = (*env)->FindClass(env,
175             "android/bluetooth/le/BluetoothLeAdvertiser");
176
177     jmethodID jni_mid_AdvertiseSettings = (*env)->GetMethodID(env,
178             jni_cid_AdvertiseSettings, "<init>", "()V");
179
180     jmethodID jni_mid_setAdvertiseMode = (*env)->GetMethodID(env,
181             jni_cid_AdvertiseSettings, "setAdvertiseMode",
182                 "(I)Landroid/bluetooth/le/AdvertiseSettings$Builder;");
183
184     jmethodID jni_mid_setConnectable = (*env)->GetMethodID(env,
185             jni_cid_AdvertiseSettings, "setConnectable",
186             "(Z)Landroid/bluetooth/le/AdvertiseSettings$Builder;");
187
188     jmethodID jni_mid_setTimeout = (*env)->GetMethodID(env,
189             jni_cid_AdvertiseSettings, "setTimeout",
190             "(I)Landroid/bluetooth/le/AdvertiseSettings$Builder;");
191
192     jmethodID jni_mid_AdvertiseDataBuilder = (*env)->GetMethodID(env,
193             jni_cid_AdvertiseDataBuilder, "<init>", "()V");
194
195     jmethodID jni_mid_addServiceUuid =
196             (*env)->GetMethodID(env, jni_cid_AdvertiseDataBuilder,
197                     "addServiceUuid",
198                     "(Landroid/os/ParcelUuid;)Landroid/bluetooth/le/AdvertiseData$Builder;");
199
200     jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env,
201             jni_cid_BTAdapter, "getDefaultAdapter",
202             "()Landroid/bluetooth/BluetoothAdapter;");
203
204     jmethodID jni_mid_getBluetoothLeAdvertiser = (*env)->GetMethodID(env,
205             jni_cid_BTAdapter, "getBluetoothLeAdvertiser",
206             "()Landroid/bluetooth/le/BluetoothLeAdvertiser;");
207
208     jmethodID jni_mid_build_LeAdvertiseSettings = (*env)->GetMethodID(env,
209             jni_cid_AdvertiseSettings, "build",
210             "()Landroid/bluetooth/le/AdvertiseSettings;");
211
212     jmethodID jni_mid_build_LeAdvertiseData = (*env)->GetMethodID(env,
213             jni_cid_AdvertiseDataBuilder, "build",
214             "()Landroid/bluetooth/le/AdvertiseData;");
215
216     jmethodID jni_mid_startAdvertising =
217             (*env)->GetMethodID(env, jni_cid_leAdvertiser, "startAdvertising",
218                     "(Landroid/bluetooth/le/AdvertiseSettings;Landroid/bluetooth/le/AdvertiseData;Landroid/bluetooth/le/AdvertiseCallback;)V");
219
220     jobject jni_AdvertiseSettings = (*env)->NewObject(env,
221             jni_cid_AdvertiseSettings, jni_mid_AdvertiseSettings);
222
223     jobject jni_obj_setAdvertiseMode = (*env)->CallObjectMethod(env,
224                 jni_AdvertiseSettings, jni_mid_setAdvertiseMode, 0); // 0: Low power, 1: Balanced
225
226     jobject jni_obj_setConnectable = (*env)->CallObjectMethod(env,
227             jni_AdvertiseSettings, jni_mid_setConnectable, JNI_TRUE);
228
229     jobject jni_obj_setTimeout = (*env)->CallObjectMethod(env,
230             jni_AdvertiseSettings, jni_mid_setTimeout, 0); //A value of 0 will disable the time limit
231
232     jobject jni_AdvertiseDataBuilder = (*env)->NewObject(env,
233             jni_cid_AdvertiseDataBuilder, jni_mid_AdvertiseDataBuilder);
234
235     jobject jni_obj_serviceUUID = CALEGetUuidFromString(env, OIC_GATT_SERVICE_UUID);
236
237     jobject jni_ParcelUuid = CALEGetParcelUuid(env, jni_obj_serviceUUID);
238
239     jobject jni_obj_addServiceUuid = (*env)->CallObjectMethod(env,
240             jni_AdvertiseDataBuilder, jni_mid_addServiceUuid, jni_ParcelUuid);
241
242     jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env,
243             jni_cid_BTAdapter, jni_mid_getDefaultAdapter);
244
245     jobject jni_obj_getBluetoothLeAdvertiser = (*env)->CallObjectMethod(env,
246             jni_obj_BTAdapter, jni_mid_getBluetoothLeAdvertiser);
247
248     jobject jni_obj_build_LeAdvertiseSettings = (*env)->CallObjectMethod(env,
249             jni_AdvertiseSettings, jni_mid_build_LeAdvertiseSettings);
250
251     jobject jni_obj_build_LeAdvertiseData = (*env)->CallObjectMethod(env,
252             jni_AdvertiseDataBuilder, jni_mid_build_LeAdvertiseData);
253
254     (*env)->CallVoidMethod(env, jni_obj_getBluetoothLeAdvertiser,
255             jni_mid_startAdvertising, jni_obj_build_LeAdvertiseSettings,
256             jni_obj_build_LeAdvertiseData, advertiseCallback);
257
258     OIC_LOG_V(DEBUG, TAG, "Advertising started!!");
259 }
260
261 void LEServerStopAdvertise(JNIEnv *env, jobject advertiseCallback)
262 {
263
264     OIC_LOG_V(DEBUG, TAG, "LEServerStopAdvertise");
265
266     jclass jni_cid_BTAdapter = (*env)->FindClass(env,
267             "android/bluetooth/BluetoothAdapter");
268
269     jclass jni_cid_leAdvertiser = (*env)->FindClass(env,
270             "android/bluetooth/le/BluetoothLeAdvertiser");
271
272     jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env,
273             jni_cid_BTAdapter, "getDefaultAdapter",
274             "()Landroid/bluetooth/BluetoothAdapter;");
275
276     jmethodID jni_mid_getBluetoothLeAdvertiser = (*env)->GetMethodID(env,
277             jni_cid_BTAdapter, "getBluetoothLeAdvertiser",
278             "()Landroid/bluetooth/le/BluetoothLeAdvertiser;");
279
280     jmethodID jni_mid_stopAdvertising =
281             (*env)->GetMethodID(env, jni_cid_leAdvertiser, "stopAdvertising",
282                     "(Landroid/bluetooth/le/AdvertiseCallback;)V");
283
284     jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env,
285             jni_cid_BTAdapter, jni_mid_getDefaultAdapter);
286
287     jobject jni_obj_getBluetoothLeAdvertiser = (*env)->CallObjectMethod(env,
288             jni_obj_BTAdapter, jni_mid_getBluetoothLeAdvertiser);
289
290     (*env)->CallVoidMethod(env, jni_obj_getBluetoothLeAdvertiser,
291             jni_mid_stopAdvertising, advertiseCallback);
292
293     OIC_LOG_V(DEBUG, TAG, "Advertising stopped!!");
294 }
295
296 jboolean CALEStartGattServer(JNIEnv *env, jobject gattServerCallback)
297 {
298
299     OIC_LOG_V(DEBUG, TAG, "CALEStartGattServer");
300
301     if(gIsStartServer)
302         OIC_LOG_V(DEBUG, TAG, "Gatt server already started");
303
304     gBluetoothGattServerCallback = (*env)->NewGlobalRef(env, gattServerCallback);
305
306     // open gatt server
307     jobject bluetoothGattServer = CALEServerOpenGattServer(env);
308     gBluetoothGattServer = (*env)->NewGlobalRef(env, bluetoothGattServer);
309
310     // create gatt service
311     jobject bluetoothGattService = CALEServerCreateGattService(env);
312
313     // add gatt service
314     return CALEServerAddGattService(env, bluetoothGattServer, bluetoothGattService);
315 }
316
317 jobject CALEServerOpenGattServer(JNIEnv *env)
318 {
319
320     OIC_LOG_V(DEBUG, TAG, "CALEServerOpenGattServer");
321
322     jclass jni_cid_context = (*env)->FindClass(env, "android/content/Context");
323
324     jclass jni_cid_bluetoothManager = (*env)->FindClass(env,
325             "android/bluetooth/BluetoothManager");
326
327     jfieldID jni_fid_bluetoothService = (*env)->GetStaticFieldID(env,
328             jni_cid_context, "BLUETOOTH_SERVICE", "Ljava/lang/String;");
329
330     jmethodID jni_mid_getSystemService = (*env)->GetMethodID(env,
331             jni_cid_context, "getSystemService",
332             "(Ljava/lang/String;)Ljava/lang/Object;");
333
334     jmethodID jni_mid_openGattServer =
335             (*env)->GetMethodID(env, jni_cid_bluetoothManager, "openGattServer",
336                     "(Landroid/content/Context;Landroid/bluetooth/BluetoothGattServerCallback;)Landroid/bluetooth/BluetoothGattServer;");
337
338     jobject jni_obj_bluetoothService = (*env)->GetStaticObjectField(env,
339             jni_cid_context, jni_fid_bluetoothService);
340
341     jobject jni_obj_bluetoothManager = (*env)->CallObjectMethod(env, gContext,
342             jni_mid_getSystemService, jni_obj_bluetoothService);
343
344     jobject jni_obj_bluetoothGattServer = (*env)->CallObjectMethod(env,
345             jni_obj_bluetoothManager, jni_mid_openGattServer, gContext,
346             gBluetoothGattServerCallback);
347
348     return jni_obj_bluetoothGattServer;
349 }
350
351 jobject CALEServerCreateGattService(JNIEnv *env)
352 {
353
354     OIC_LOG_V(DEBUG, TAG, "CALEServerCreateGattService");
355
356     jclass jni_cid_bluetoothGattService = (*env)->FindClass(env,
357             "android/bluetooth/BluetoothGattService");
358
359     jclass jni_cid_bluetoothGattCharacteristic = (*env)->FindClass(env,
360             "android/bluetooth/BluetoothGattCharacteristic");
361
362     jfieldID jni_fid_serviceType = (*env)->GetStaticFieldID(env,
363             jni_cid_bluetoothGattService, "SERVICE_TYPE_PRIMARY", "I");
364
365     jfieldID jni_fid_readProperties = (*env)->GetStaticFieldID(env,
366             jni_cid_bluetoothGattCharacteristic, "PROPERTY_READ", "I");
367
368     jfieldID jni_fid_writeProperties = (*env)->GetStaticFieldID(env,
369             jni_cid_bluetoothGattCharacteristic, "PROPERTY_WRITE", "I");
370
371     jfieldID jni_fid_readPermissions = (*env)->GetStaticFieldID(env,
372             jni_cid_bluetoothGattCharacteristic, "PERMISSION_READ", "I");
373
374     jfieldID jni_fid_writePermissions = (*env)->GetStaticFieldID(env,
375             jni_cid_bluetoothGattCharacteristic, "PERMISSION_WRITE", "I");
376
377     jmethodID jni_mid_bluetoothGattService = (*env)->GetMethodID(env,
378             jni_cid_bluetoothGattService, "<init>", "(Ljava/util/UUID;I)V");
379
380     jmethodID jni_mid_addCharacteristic = (*env)->GetMethodID(env,
381             jni_cid_bluetoothGattService, "addCharacteristic",
382             "(Landroid/bluetooth/BluetoothGattCharacteristic;)Z");
383
384     jmethodID jni_mid_bluetoothGattCharacteristic = (*env)->GetMethodID(env,
385             jni_cid_bluetoothGattCharacteristic, "<init>",
386             "(Ljava/util/UUID;II)V");
387
388     jobject jni_obj_serviceUUID = CALEGetUuidFromString(env, OIC_GATT_SERVICE_UUID);
389
390     jobject jni_obj_serviceType = (*env)->GetStaticObjectField(env,
391             jni_cid_bluetoothGattService, jni_fid_serviceType);
392
393     jobject jni_bluetoothGattService = (*env)->NewObject(env,
394             jni_cid_bluetoothGattService, jni_mid_bluetoothGattService,
395             jni_obj_serviceUUID, jni_obj_serviceType);
396
397     jobject jni_obj_readUuid = CALEGetUuidFromString(env,
398             OIC_GATT_CHARACTERISTIC_RESPONSE_UUID);
399
400     jint jni_int_readProperties = (*env)->GetStaticIntField(env,
401             jni_cid_bluetoothGattCharacteristic, jni_fid_readProperties);
402
403     jint jni_int_readPermissions = (*env)->GetStaticIntField(env,
404             jni_cid_bluetoothGattCharacteristic, jni_fid_readPermissions);
405
406     jobject jni_readCharacteristic = (*env)->NewObject(env,
407             jni_cid_bluetoothGattCharacteristic,
408             jni_mid_bluetoothGattCharacteristic, jni_obj_readUuid,
409             jni_int_readProperties, jni_int_readPermissions);
410
411     jboolean jni_boolean_addReadCharacteristic = (*env)->CallBooleanMethod(env,
412             jni_bluetoothGattService, jni_mid_addCharacteristic,
413             jni_readCharacteristic);
414
415     jobject jni_obj_writeUuid = CALEGetUuidFromString(env,
416             OIC_GATT_CHARACTERISTIC_REQUEST_UUID);
417
418     jint jni_int_writeProperties = (*env)->GetStaticIntField(env,
419             jni_cid_bluetoothGattCharacteristic, jni_fid_writeProperties);
420
421     jint jni_int_writePermissions = (*env)->GetStaticIntField(env,
422             jni_cid_bluetoothGattCharacteristic, jni_fid_writePermissions);
423
424     jobject jni_writeCharacteristic = (*env)->NewObject(env,
425             jni_cid_bluetoothGattCharacteristic,
426             jni_mid_bluetoothGattCharacteristic, jni_obj_writeUuid,
427             jni_int_writeProperties, jni_int_writePermissions);
428
429     jboolean jni_boolean_addWriteCharacteristic = (*env)->CallBooleanMethod(env,
430             jni_bluetoothGattService, jni_mid_addCharacteristic,
431             jni_writeCharacteristic);
432
433     if (jni_boolean_addWriteCharacteristic == JNI_FALSE)
434     {
435         OIC_LOG_V(DEBUG, TAG, "Fail to add jni_boolean_addReadCharacteristic");
436         return NULL;
437     }
438
439     return jni_bluetoothGattService;
440 }
441
442 jboolean CALEServerAddGattService(JNIEnv *env, jobject bluetoothGattServer,
443         jobject bluetoothGattService)
444 {
445
446     OIC_LOG_V(DEBUG, TAG, "CALEServerAddGattService");
447
448     jclass jni_cid_bluetoothGattServer = (*env)->FindClass(env,
449             "android/bluetooth/BluetoothGattServer");
450
451     jmethodID jni_mid_addService = (*env)->GetMethodID(env,
452             jni_cid_bluetoothGattServer, "addService",
453             "(Landroid/bluetooth/BluetoothGattService;)Z");
454
455     jboolean jni_boolean_addService = (*env)->CallBooleanMethod(env, bluetoothGattServer,
456             jni_mid_addService, bluetoothGattService);
457
458     if (jni_boolean_addService == JNI_FALSE)
459     {
460         OIC_LOG_V(DEBUG, TAG, "Fail to add gatt service");
461     }
462
463     return jni_boolean_addService;
464 }
465
466 jboolean CALEServerConnect(JNIEnv *env, jobject bluetoothDevice)
467 {
468
469     OIC_LOG_V(DEBUG, TAG, "CALEConnect");
470
471     jclass jni_cid_bluetoothGattServer = (*env)->FindClass(env,
472             "android/bluetooth/BluetoothGattServer");
473
474     jmethodID jni_mid_connect = (*env)->GetMethodID(env,
475             jni_cid_bluetoothGattServer, "connect",
476             "(Landroid/bluetooth/BluetoothDevice;Z)Z");
477
478     jboolean jni_boolean_connect = (*env)->CallBooleanMethod(env,
479             gBluetoothGattServer, jni_mid_connect, bluetoothDevice, JNI_TRUE);
480
481     if(jni_boolean_connect == JNI_FALSE) {
482         OIC_LOG_V(DEBUG, TAG, "Fail to connect");
483     }
484
485     return jni_boolean_connect;
486 }
487
488 void CALEServerDisconnect(JNIEnv *env, jobject bluetoothDevice)
489 {
490
491     OIC_LOG_V(DEBUG, TAG, "CALEDisconnect");
492
493     jclass jni_cid_bluetoothGattServer = (*env)->FindClass(env,
494             "android/bluetooth/BluetoothGattServer");
495
496     jmethodID jni_mid_cancelConnection = (*env)->GetMethodID(env,
497             jni_cid_bluetoothGattServer, "cancelConnection",
498             "(Landroid/bluetooth/BluetoothDevice;)V");
499
500     (*env)->CallVoidMethod(env, gBluetoothGattServer, jni_mid_cancelConnection,
501             bluetoothDevice);
502 }
503
504 jboolean CALEServerSend(JNIEnv *env, jobject bluetoothDevice, jbyteArray responseData)
505 {
506
507     OIC_LOG_V(DEBUG, TAG, "CALESend");
508
509     jobject responseChar = CALEServerSetResponseData(env, responseData);
510
511     jboolean result = CALEServerSendResponseData(env, bluetoothDevice, responseChar);
512
513     if(result == JNI_FALSE)
514     {
515         OIC_LOG_V(DEBUG, TAG, "Fail to send response data");
516     }
517
518     return result;
519 }
520
521 void CALeServerCreateJniInterfaceObject()
522 {
523     OIC_LOG_V(DEBUG, TAG, "CALeServerCreateJniInterfaceObject");
524
525     jboolean isAttached = FALSE;
526     JNIEnv* env;
527     jint res = (*g_jvm)->GetEnv(g_jvm, (void**)&env, JNI_VERSION_1_6);
528     if(res != JNI_OK)
529     {
530         OIC_LOG_V(DEBUG, TAG, "Could not get JNIEnv pointer");
531         res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
532
533         if(res != JNI_OK)
534         {
535             OIC_LOG_V(DEBUG, TAG, "AttachCurrentThread failed");
536             return;
537         }
538         isAttached = TRUE;
539     }
540
541     // initialize le server
542
543     if(isAttached)
544         (*g_jvm)->DetachCurrentThread(g_jvm);
545 }
546
547 void CALEServerInitialize(u_thread_pool_t handle)
548 {
549     OIC_LOG(DEBUG, TAG, "CALEServerInitialize");
550
551     gThreadPoolHandle = handle;
552
553     CALEServerCreateCachedDeviceList();
554 }
555
556 void CALEServerTerminate()
557 {
558     OIC_LOG(DEBUG, TAG, "CALEServerTerminate");
559
560     jboolean isAttached = FALSE;
561     JNIEnv* env;
562     jint res = (*g_jvm)->GetEnv(g_jvm, (void**)&env, JNI_VERSION_1_6);
563     if(res != JNI_OK)
564     {
565         OIC_LOG_V(DEBUG, TAG, "Could not get JNIEnv pointer");
566         res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
567
568         if(res != JNI_OK)
569         {
570             OIC_LOG_V(DEBUG, TAG, "AttachCurrentThread failed");
571             return;
572         }
573         isAttached = TRUE;
574     }
575
576     CALEServerStopMulticastServer(0);
577
578     CALEServerRemoveAllDevices(env);
579
580     if(gLeAdvertiseCallback)
581     {
582         (*env)->DeleteGlobalRef(env, gLeAdvertiseCallback);
583     }
584
585     if(gBluetoothGattServer)
586     {
587         (*env)->DeleteGlobalRef(env, gBluetoothGattServer);
588     }
589
590     if(gBluetoothGattServerCallback)
591     {
592         (*env)->DeleteGlobalRef(env, gBluetoothGattServerCallback);
593     }
594
595     if(gContext)
596     {
597         (*env)->DeleteGlobalRef(env, gContext);
598     }
599
600     gIsStartServer = FALSE;
601
602     if(isAttached)
603         (*g_jvm)->DetachCurrentThread(g_jvm);
604 }
605
606 int32_t CALEServerSendUnicastMessage(const char* address, const char* data, uint32_t dataLen)
607 {
608     OIC_LOG_V(DEBUG, TAG, "CALEServerSendUnicastMessage(%s, %s)", address, data);
609
610     jboolean isAttached = FALSE;
611     JNIEnv* env;
612     jint res = (*g_jvm)->GetEnv(g_jvm, (void**)&env, JNI_VERSION_1_6);
613     if(res != JNI_OK)
614     {
615         OIC_LOG_V(DEBUG, TAG, "Could not get JNIEnv pointer");
616         res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
617
618         if(res != JNI_OK)
619         {
620             OIC_LOG_V(DEBUG, TAG, "AttachCurrentThread failed");
621             return -1;
622         }
623         isAttached = TRUE;
624     }
625
626     CALEServerSendUnicastMessageImpl(env, address, data, dataLen);
627
628     if(isAttached)
629         (*g_jvm)->DetachCurrentThread(g_jvm);
630
631     return 0;
632 }
633
634 int32_t CALEServerSendMulticastMessage(const char* data, uint32_t dataLen)
635 {
636     OIC_LOG_V(DEBUG, TAG, "CALEServerSendMulticastMessage(%s)", data);
637
638     jboolean isAttached = FALSE;
639     JNIEnv* env;
640     jint res = (*g_jvm)->GetEnv(g_jvm, (void**)&env, JNI_VERSION_1_6);
641     if(res != JNI_OK)
642     {
643         OIC_LOG_V(DEBUG, TAG, "Could not get JNIEnv pointer");
644         res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
645
646         if(res != JNI_OK)
647         {
648             OIC_LOG_V(DEBUG, TAG, "AttachCurrentThread failed");
649             return -1;
650         }
651         isAttached = TRUE;
652     }
653
654     CALEServerSendMulticastMessageImpl(env, data, dataLen);
655
656     if(isAttached)
657         (*g_jvm)->DetachCurrentThread(g_jvm);
658
659     return 0;
660 }
661
662 int32_t CALEServerStartUnicastServer(const char* address)
663 {
664     OIC_LOG_V(DEBUG, TAG, "CALEServerStartUnicastServer(%s)", address);
665
666     return 0;
667 }
668
669 int32_t CALEServerStartMulticastServer()
670 {
671     OIC_LOG_V(DEBUG, TAG, "CALEServerStartMulticastServer");
672
673     if(gIsStartServer)
674     {
675         OIC_LOG_V(DEBUG, TAG, "server is already started..it will be skipped");
676         return 0;
677     }
678
679     jboolean isAttached = FALSE;
680     JNIEnv* env;
681     jint res = (*g_jvm)->GetEnv(g_jvm, (void**)&env, JNI_VERSION_1_6);
682     if(res != JNI_OK)
683     {
684         OIC_LOG_V(DEBUG, TAG, "Could not get JNIEnv pointer");
685         res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
686
687         if(res != JNI_OK)
688         {
689             OIC_LOG_V(DEBUG, TAG, "AttachCurrentThread failed");
690             return -1;
691         }
692         isAttached = TRUE;
693     }
694
695     gIsStartServer = TRUE;
696
697     // start gatt server
698     if( CALEStartGattServer(env, gBluetoothGattServerCallback) == JNI_FALSE) {
699         OIC_LOG_V(DEBUG, TAG, "Fail to start gatt server");
700         return -1;
701     }
702
703     // start advertise
704     LEServerStartAdvertise(env, gLeAdvertiseCallback);
705
706     if(isAttached)
707         (*g_jvm)->DetachCurrentThread(g_jvm);
708
709     return 0;
710 }
711
712 int32_t CALEServerStopUnicastServer(int32_t serverID)
713 {
714     OIC_LOG(DEBUG, TAG, "CALEServerStopUnicastServer");
715
716     return 0;
717 }
718
719 int32_t CALEServerStopMulticastServer(int32_t serverID)
720 {
721     OIC_LOG(DEBUG, TAG, "CALEServerStopMulticastServer");
722
723     if(gIsStartServer == FALSE)
724     {
725         OIC_LOG_V(DEBUG, TAG, "server is already stopped..it will be skipped");
726         return 0;
727     }
728
729     jboolean isAttached = FALSE;
730     JNIEnv* env;
731     jint res = (*g_jvm)->GetEnv(g_jvm, (void**)&env, JNI_VERSION_1_6);
732     if(res != JNI_OK)
733     {
734         OIC_LOG_V(DEBUG, TAG, "Could not get JNIEnv pointer");
735         res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
736
737         if(res != JNI_OK)
738         {
739             OIC_LOG_V(DEBUG, TAG, "AttachCurrentThread failed");
740             return -1;
741         }
742         isAttached = TRUE;
743     }
744
745     LEServerStopAdvertise(env, gLeAdvertiseCallback);
746
747     gIsStartServer = FALSE;
748
749     if(isAttached)
750         (*g_jvm)->DetachCurrentThread(g_jvm);
751
752     return 0;
753 }
754
755 void CALEServerSetCallback(CAPacketReceiveCallback callback)
756 {
757     OIC_LOG(DEBUG, TAG, "CALEServerSetCallback");
758     gPacketReceiveCallback = callback;
759 }
760
761 void CALEServerGetInterfaceInfo(CALocalConnectivity_t **info, uint32_t* size)
762 {
763     OIC_LOG(DEBUG, TAG, "CALEServerGetInterfaceInfo");
764     return;
765 }
766
767 void CALEServerGetLocalAddress(char* address)
768 {
769     OIC_LOG(DEBUG, TAG, "CALEServerGetLocalAddress");
770
771     jboolean isAttached = FALSE;
772     JNIEnv* env;
773     jint res = (*g_jvm)->GetEnv(g_jvm, (void**)&env, JNI_VERSION_1_6);
774     if(res != JNI_OK)
775     {
776         OIC_LOG_V(DEBUG, TAG, "Could not get JNIEnv pointer");
777         res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
778         if(res != JNI_OK)
779         {
780             OIC_LOG_V(DEBUG, TAG, "AttachCurrentThread failed");
781             return;
782         }
783         isAttached = TRUE;
784     }
785
786     jstring jni_address = CALEGetLocalDeviceAddress(env);
787     const char* localAddress = (*env)->GetStringUTFChars(env, jni_address, NULL);
788     memcpy(address, localAddress, strlen(localAddress));
789
790     OIC_LOG_V(DEBUG, TAG, "Local Address : %s", address);
791     if(isAttached)
792         (*g_jvm)->DetachCurrentThread(g_jvm);
793 }
794
795 int32_t CALEServerSendUnicastMessageImpl(JNIEnv *env, const char* address, const char* data,
796         uint32_t dataLen)
797 {
798     OIC_LOG_V(DEBUG, TAG, "CALEServerSendUnicastMessageImpl, address: %s, data: %s", address, data);
799
800     // 1. get device object with address from cache
801     // 2. connect to the gatt client device
802     // 3. write a characteristic for response
803     // 4. notify it
804     // 5. disconnect
805
806     jobject jni_obj_bluetoothDevice = NULL;
807
808     if(gConnectedDeviceList == NULL)
809     {
810         OIC_LOG(DEBUG, TAG, "gConnectedDeviceList is null");
811     }
812
813     if(gConnectedDeviceList)
814     {
815         jint index;
816         for (index = 0; index < u_arraylist_length(gConnectedDeviceList); index++)
817         {
818             OIC_LOG(DEBUG, TAG, "check device address");
819             jobject jarrayObj = (jobject) u_arraylist_get(gConnectedDeviceList, index);
820             if(!jarrayObj)
821             {
822                 OIC_LOG(DEBUG, TAG, "jarrayObj is null");
823                 return -1;
824             }
825
826             jstring jni_setAddress = CALEGetAddressFromBTDevice(env, jarrayObj);
827             if(!jni_setAddress)
828             {
829                 OIC_LOG(DEBUG, TAG, "jni_setAddress is null");
830                 return -1;
831             }
832             const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
833
834             if(jarrayObj == NULL) {
835                 OIC_LOG(DEBUG, TAG, "jarrayObj is null");
836             }
837
838             if(!strcmp(setAddress, address))
839             {
840                 OIC_LOG(DEBUG, TAG, "device address matched");
841                 jni_obj_bluetoothDevice = jarrayObj;
842                 break;
843             }
844             jni_obj_bluetoothDevice = jarrayObj;
845         }
846
847         if(jni_obj_bluetoothDevice)
848         {
849             jbyteArray jni_bytearr_data = (*env)->NewByteArray(env, dataLen);
850             (*env)->SetByteArrayRegion(env, jni_bytearr_data, 0, dataLen, (jbyte*)data);
851
852             CALEServerSend(env, jni_obj_bluetoothDevice, jni_bytearr_data);
853
854         } else {
855             OIC_LOG(DEBUG, TAG, "jni_obj_bluetoothDevice is null");
856         }
857     }
858     return 0;
859 }
860
861 int32_t CALEServerSendMulticastMessageImpl(JNIEnv *env, const char* data, uint32_t dataLen)
862 {
863     OIC_LOG_V(DEBUG, TAG, "CALEServerSendMulticastMessageImpl, send to, data: %s", data);
864
865     if(!gConnectedDeviceList)
866     {
867         OIC_LOG(DEBUG, TAG, "gConnectedDeviceList is null");
868         return 0;
869     }
870
871     // 1. get all the device objects from cache
872     // 2. connect to the gatt client devices
873     // 3. write a characteristic for response
874     // 4. notify it to every devices
875     // 5. disconnect
876
877     jint index;
878     for (index = 0; index < u_arraylist_length(gConnectedDeviceList); index++)
879     {
880         jobject jarrayObj = (jobject) u_arraylist_get(gConnectedDeviceList, index);
881         if(!jarrayObj)
882         {
883             OIC_LOG(DEBUG, TAG, "jarrayObj is null");
884             return -1;
885         }
886
887         CALEServerConnect(env, jarrayObj);
888
889         sleep(1);
890     }
891
892     return 0;
893 }
894
895 JNIEXPORT void JNICALL Java_com_iotivity_jar_CALeInterface_CARegisterLeGattServerCallback
896   (JNIEnv *env, jobject obj, jobject callback)
897 {
898     OIC_LOG_V(DEBUG, TAG, "CALeInterface - Register Le Gatt Server Callback");
899
900     gBluetoothGattServerCallback = (*env)->NewGlobalRef(env, callback);
901 }
902
903 JNIEXPORT void JNICALL Java_com_iotivity_jar_CALeInterface_CARegisterBluetoothLeAdvertiseCallback
904   (JNIEnv *env, jobject obj, jobject callback)
905 {
906     OIC_LOG_V(DEBUG, TAG, "CALeInterface - Register Le Advertise Callback");
907
908     gLeAdvertiseCallback = (*env)->NewGlobalRef(env, callback);
909 }
910
911 JNIEXPORT void JNICALL Java_com_iotivity_jar_CALeInterface_CALeGattServerConnectionStateChangeCallback
912   (JNIEnv *env, jobject obj, jobject device, jint status, jint newState)
913 {
914     OIC_LOG_V(DEBUG, TAG, "CALeInterface - Gatt Server ConnectionStateChange Callback");
915
916     OIC_LOG_V(DEBUG, TAG, "New connection State: %d", newState);
917
918     if (!device)
919     {
920         OIC_LOG(DEBUG, TAG, "device is null");
921         return;
922     }
923
924     jclass jni_cid_bluetoothProfile = (*env)->FindClass(env,
925             "android/bluetooth/BluetoothProfile");
926
927     jfieldID jni_fid_state_connected = (*env)->GetStaticFieldID(env,
928             jni_cid_bluetoothProfile, "STATE_CONNECTED", "I");
929
930     jfieldID jni_fid_state_disconnected = (*env)->GetStaticFieldID(env,
931                 jni_cid_bluetoothProfile, "STATE_DISCONNECTED", "I");
932
933     // STATE_CONNECTED
934     jint jni_int_state_connected = (*env)->GetStaticIntField(env,
935             jni_cid_bluetoothProfile, jni_fid_state_connected);
936
937     // STATE_DISCONNECTED
938     jint jni_int_state_disconnected = (*env)->GetStaticIntField(env,
939                 jni_cid_bluetoothProfile, jni_fid_state_disconnected);
940
941     if(newState == jni_int_state_connected)
942     {
943
944         OIC_LOG_V(DEBUG, TAG, "LE CONNECTED");
945
946         jstring jni_remoteAddress = CALEGetAddressFromBTDevice(env, device);
947         if (!jni_remoteAddress)
948         {
949             OIC_LOG(DEBUG, TAG, "jni_remoteAddress is null");
950             return;
951         }
952
953         const char* remoteAddress = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);
954
955         if (gConnectedDeviceList == NULL)
956         {
957             OIC_LOG_V(DEBUG, TAG, "gConnectedDeviceList is null");
958         }
959
960         if(CALEServerIsDeviceInList(env, remoteAddress) == JNI_FALSE)
961         {
962             OIC_LOG_V(DEBUG, TAG, "add connected device to cache");
963             CALEServerAddDeviceToList(env, device);
964         }
965
966     }
967     else if(newState == jni_int_state_disconnected)
968     {
969         OIC_LOG_V(DEBUG, TAG, "LE DISCONNECTED");
970     }
971 }
972
973 void CALEServerCreateCachedDeviceList()
974 {
975     OIC_LOG(DEBUG, TAG, "CALEServerCreateCachedDeviceList");
976
977     // create new object array
978     if (gConnectedDeviceList == NULL)
979     {
980         OIC_LOG_V(DEBUG, TAG, "Create device list");
981
982         gConnectedDeviceList = u_arraylist_create();
983     }
984 }
985
986 jboolean CALEServerIsDeviceInList(JNIEnv *env, const char* remoteAddress)
987 {
988     OIC_LOG(DEBUG, TAG, "CALEServerIsDeviceInList");
989
990     if(gConnectedDeviceList == NULL)
991         OIC_LOG(DEBUG, TAG, "list is null");
992
993     uint32_t len = u_arraylist_length(gConnectedDeviceList);
994
995     uint32_t index;
996     for (index = 0; index < u_arraylist_length(gConnectedDeviceList); index++)
997     {
998         jobject jarrayObj = (jobject) u_arraylist_get(gConnectedDeviceList, index);
999
1000         if (!jarrayObj)
1001         {
1002             OIC_LOG(DEBUG, TAG, "jarrayObj is null");
1003             return JNI_TRUE;
1004         }
1005
1006         jstring jni_setAddress = CALEGetAddressFromBTDevice(env, jarrayObj);
1007         if (!jni_setAddress)
1008         {
1009             OIC_LOG(DEBUG, TAG, "jni_setAddress is null");
1010             return JNI_TRUE;
1011         }
1012
1013         const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress,
1014                 NULL);
1015
1016         if (!strcmp(remoteAddress, setAddress))
1017         {
1018             OIC_LOG_V(DEBUG, TAG, "the device is already set");
1019             return JNI_TRUE;
1020         }
1021         else
1022         {
1023             continue;
1024         }
1025     }
1026
1027     OIC_LOG_V(DEBUG, TAG, "no device in list");
1028     return JNI_FALSE;
1029 }
1030
1031 void CALEServerAddDeviceToList(JNIEnv *env, jobject device)
1032 {
1033     if (device == NULL)
1034     {
1035         OIC_LOG(DEBUG, TAG, "device is null");
1036         return;
1037     }
1038
1039     if (gConnectedDeviceList == NULL)
1040     {
1041         OIC_LOG(DEBUG, TAG, "list is null");
1042         return;
1043     }
1044
1045     jstring jni_remoteAddress = CALEGetAddressFromBTDevice(env, device);
1046     if (!jni_remoteAddress)
1047     {
1048         OIC_LOG(DEBUG, TAG, "jni_remoteAddress is null");
1049         return;
1050     }
1051
1052     const char* remoteAddress = (*env)->GetStringUTFChars(env,
1053             jni_remoteAddress, NULL);
1054
1055     if (CALEServerIsDeviceInList(env, remoteAddress) == JNI_FALSE)
1056     {
1057         jobject gdevice = (*env)->NewGlobalRef(env, device);
1058         u_arraylist_add(gConnectedDeviceList, gdevice);
1059         OIC_LOG_V(DEBUG, TAG, "Set Object to Array as Element");
1060     }
1061 }
1062
1063 void CALEServerRemoveAllDevices(JNIEnv *env)
1064 {
1065     OIC_LOG_V(DEBUG, TAG, "CALEServerRemoveAllDevices");
1066
1067     if (!gConnectedDeviceList)
1068     {
1069         OIC_LOG(DEBUG, TAG, "no deviceList");
1070         return;
1071     }
1072
1073     uint32_t index;
1074     for (index = 0; index < u_arraylist_length(gConnectedDeviceList); index++)
1075     {
1076         jobject jarrayObj = (jobject) u_arraylist_get(gConnectedDeviceList, index);
1077
1078         if (jarrayObj)
1079         {
1080             (*env)->DeleteGlobalRef(env, jarrayObj);
1081         }
1082     }
1083
1084     OICFree(gConnectedDeviceList);
1085     gConnectedDeviceList = NULL;
1086     return;
1087 }
1088
1089 void CALEServerRemoveDevice(JNIEnv *env, jstring address)
1090 {
1091     OIC_LOG_V(DEBUG, TAG, "CALEServerRemoveDevice");
1092
1093     if (!gConnectedDeviceList)
1094     {
1095         OIC_LOG(DEBUG, TAG, "no deviceList");
1096         return;
1097     }
1098
1099     uint32_t index;
1100     for (index = 0; index < u_arraylist_length(gConnectedDeviceList); index++)
1101     {
1102         jobject jarrayObj = (jobject) u_arraylist_get(gConnectedDeviceList, index);
1103
1104         if (jarrayObj)
1105         {
1106             jstring jni_setAddress = CALEGetAddressFromBTDevice(env, jarrayObj);
1107             if (!jni_setAddress)
1108             {
1109                 OIC_LOG(DEBUG, TAG, "wrong device address");
1110                 continue;
1111             }
1112             const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress,
1113                     NULL);
1114             const char* remoteAddress = (*env)->GetStringUTFChars(env, address,
1115                     NULL);
1116
1117             if (!strcmp(setAddress, remoteAddress))
1118             {
1119                 OIC_LOG_V(DEBUG, TAG, "device address : %s", remoteAddress);
1120                 (*env)->DeleteGlobalRef(env, jarrayObj);
1121
1122                 CALEServerReorderinglist(index);
1123                 return;
1124             }
1125         }
1126     }
1127     OIC_LOG(DEBUG, TAG, "no target object");
1128     return;
1129 }
1130
1131 void CALEServerReorderinglist(uint32_t index)
1132 {
1133     if (index >= gConnectedDeviceList->length)
1134         return;
1135
1136     if (index < gConnectedDeviceList->length - 1)
1137     {
1138         memmove(&gConnectedDeviceList->data[index], &gConnectedDeviceList->data[index + 1],
1139                 (gConnectedDeviceList->length - index - 1) * sizeof(void *));
1140     }
1141
1142     gConnectedDeviceList->size--;
1143     gConnectedDeviceList->length--;
1144 }
1145
1146 JNIEXPORT void JNICALL
1147 Java_com_iotivity_jar_CALeInterface_CALeGattServerServiceAddedCallback
1148 (JNIEnv *env, jobject obj, jint status, jobject gattService)
1149 {
1150     OIC_LOG_V(DEBUG, TAG, "CALeInterface - Gatt Service Added Callback(%d)", status);
1151 }
1152
1153 JNIEXPORT void JNICALL
1154 Java_com_iotivity_jar_CALeInterface_CALeGattServerCharacteristicReadRequestCallback
1155 (JNIEnv *env, jobject obj, jobject device, jint requestId, jint offset, jobject characteristic, jbyteArray data)
1156 {
1157     OIC_LOG_V(DEBUG, TAG, "CALeInterface - Gatt Server Characteristic Read Request Callback");
1158
1159     CALEServerSendResponse(env, device, requestId, 0, offset, NULL);
1160 }
1161
1162 JNIEXPORT void JNICALL
1163 Java_com_iotivity_jar_CALeInterface_CALeGattServerCharacteristicWriteRequestCallback
1164 (JNIEnv *env, jobject obj, jobject device, jint requestId, jobject characteristic, jbyteArray data,
1165         jboolean preparedWrite, jboolean responseNeeded, jint offset, jbyteArray value)
1166 {
1167     OIC_LOG_V(DEBUG, TAG, "CALeInterface - Gatt Server Characteristic Write Request Callback");
1168
1169     CALEServerSendResponse(env, device, requestId, 0, offset, value);
1170
1171     if(data == NULL)
1172     {
1173         OIC_LOG_V(DEBUG, TAG, "Reqeust data is null");
1174         return;
1175     }
1176
1177     jboolean isCopy;
1178     char* requestData = (char*)(*env)->GetByteArrayElements(env, data, &isCopy);
1179
1180     jstring jni_address = CALEGetAddressFromBTDevice(env, device);
1181     const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
1182     OIC_LOG_V(DEBUG, TAG, "remote device address : %s", address);
1183
1184     gPacketReceiveCallback(address, requestData);
1185 }
1186
1187 JNIEXPORT void JNICALL
1188 Java_com_iotivity_jar_CALeInterface_CALeGattServerDescriptorReadRequestCallback
1189 (JNIEnv *env, jobject obj, jobject device, jint requestId, jint offset, jobject descriptor)
1190 {
1191     OIC_LOG_V(DEBUG, TAG, "CALeInterface_CALeGattServerDescriptorReadRequestCallback");
1192 }
1193
1194 JNIEXPORT void JNICALL
1195 Java_com_iotivity_jar_CALeInterface_CALeGattServerDescriptorWriteRequestCallback
1196 (JNIEnv *env, jobject obj, jobject device, jint requestId, jobject descriptor,
1197         jboolean preparedWrite, jboolean responseNeeded, jint offset, jbyteArray value)
1198 {
1199     OIC_LOG_V(DEBUG, TAG, "CALeInterface_CALeGattServerDescriptorWriteRequestCallback");
1200 }
1201
1202 JNIEXPORT void JNICALL
1203 Java_com_iotivity_jar_CALeInterface_CALeGattServerExecuteWriteCallback
1204 (JNIEnv *env, jobject obj, jobject device, jint requestId, jboolean execute)
1205 {
1206     OIC_LOG_V(DEBUG, TAG, "CALeInterface_CALeGattServerExecuteWriteCallback");
1207
1208     CALEServerSendResponse(env, device, requestId, 0, 0, NULL);
1209 }
1210
1211 JNIEXPORT void JNICALL
1212 Java_com_iotivity_jar_CALeInterface_CALeGattServerNotificationSentCallback
1213 (JNIEnv *env, jobject obj, jobject device, jint status)
1214 {
1215     OIC_LOG_V(DEBUG, TAG, "CALeInterface - Gatt Server Notification Sent Callback");
1216 }
1217
1218 JNIEXPORT void JNICALL
1219 Java_com_iotivity_jar_CALeInterface_CALeAdvertiseStartSuccessCallback
1220 (JNIEnv *env, jobject obj, jobject settingsInEffect)
1221 {
1222     OIC_LOG_V(DEBUG, TAG, "CALeInterface - LE Advertise Start Success Callback");
1223 }
1224
1225 JNIEXPORT void JNICALL
1226 Java_com_iotivity_jar_CALeInterface_CALeAdvertiseStartFailureCallback
1227 (JNIEnv *env, jobject obj, jint errorCode)
1228 {
1229     OIC_LOG_V(DEBUG, TAG, "CALeInterface - LE Advertise Start Failure Callback(%)", errorCode);
1230 }
1231