d34d339467ea00d115bbfe9f1057e816b6525566
[platform/core/connectivity/bluetooth-frwk.git] / bt-oal / bluez_hal / src / bt-hal-gatt.c
1 /*
2  * BLUETOOOTH HAL
3  *
4  * Copyright (c) 2015 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Anupam Roy <anupam.r@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at:
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22
23 /*******************************************************************************
24  *
25  * Description: GATT Profile Bluetooth Interface
26  *
27  *******************************************************************************/
28
29 #include <hardware/bluetooth.h>
30 #include <hardware/bt_gatt.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <errno.h>
34 #include <string.h>
35 #include <dlog.h>
36
37 #include "bt-hal.h"
38 #include "bt-hal-log.h"
39 #include "bt-hal-msg.h"
40 #include "bt-hal-utils.h"
41
42 #include "bt-hal-adapter-le.h"
43 #include "bt-hal-gatt-server.h"
44 #include "bt-hal-gatt-client.h"
45 #include "bt_gatt_types.h"
46
47 /* Advertising report event types */
48 #define BT_LE_ADV_IND   0x00
49 #define BT_LE_ADV_DIRECT_IND    0x01
50 #define BT_LE_ADV_SCAN_IND      0x02
51 #define BT_LE_ADV_NONCONN_IND   0x03
52 #define BT_LE_ADV_SCAN_RSP      0x04
53
54 #define BT_HAL_ADV_DATA_MAX_SIZE        31
55 #define BT_HAL_GATTC_READ_VALUE_TYPE_VALUE          0x0000  /* Attribute value itself */
56 typedef struct {
57         char addr[18];
58         uint8_t addr_type;
59         uint8_t adv_type;
60         int rssi;
61         int data_len;
62         uint8_t data[BT_HAL_ADV_DATA_MAX_SIZE * 2];
63         guint timer_id;
64 } bt_hal_le_adv_info_t;
65
66 /****************************************** Global Variables ******************************************/
67 const btgatt_callbacks_t *bt_gatt_callbacks = NULL;
68
69 extern btgatt_client_interface_t btgatt_client_interface;
70 extern btgatt_server_interface_t btgatt_server_interface;
71
72 static GSList *adv_ind_list = NULL;
73 /****************************************** Forward Declarations *************************************/
74 static void __bt_handle_gatt_client_registered(void *buf, uint16_t len);
75 static void __bt_hal_handle_gatt_client_scan_result(void *buf, uint16_t len);
76 static void __bt_handle_gatt_client_connected(void *buf, uint16_t len);
77 static void __bt_handle_gatt_client_disconnected(void *buf, uint16_t len);
78 static void __bt_handle_gatt_client_search_result(void *buf, uint16_t len);
79 static void __bt_handle_gatt_client_search_complete(void *buf, uint16_t len);
80 static void __bt_handle_gatt_client_search_char_result(void *buf, uint16_t len);
81 static void __bt_handle_gatt_client_search_desc_result(void *buf, uint16_t len);
82 static void __bt_handle_gatt_client_read_charac(void *buf, uint16_t len);
83 static void __bt_handle_gatt_client_read_desc(void *buf, uint16_t len);
84 static void __bt_handle_gatt_client_write_char(void *buf, uint16_t len);
85 static void __bt_handle_gatt_client_write_desc(void *buf, uint16_t len);
86 static void __bt_handle_gatt_client_watch_notification(void *buf, uint16_t len);
87 /*****************************************************************************************************/
88
89 static bool interface_ready(void)
90 {
91         return bt_gatt_callbacks != NULL;
92 }
93
94 static void __bt_hal_handle_server_instance_initialized(void *buf, uint16_t len)
95 {
96         struct hal_ev_server_instance_registered *ev = buf;
97
98         if (bt_gatt_callbacks->server->register_server_cb)
99                 bt_gatt_callbacks->server->register_server_cb(ev->status, ev->server_instance, (bt_uuid_t*)&ev->app_uuid);
100 }
101
102 static void __bt_hal_handle_multi_adv_data_set(void *buf, uint16_t len)
103 {
104         struct hal_ev_multi_adv_data_set *ev = buf;
105
106         if (bt_gatt_callbacks->server->multi_adv_data_cb)
107                 bt_gatt_callbacks->server->multi_adv_data_cb(ev->server_instance, ev->status);
108 }
109
110 static void __bt_hal_handle_multi_adv_data_update(void *buf, uint16_t len)
111 {
112         struct hal_ev_multi_adv_update *ev = buf;
113
114         if (bt_gatt_callbacks->server->multi_adv_update_cb)
115                 bt_gatt_callbacks->server->multi_adv_update_cb(ev->server_instance, ev->status);
116 }
117
118 static void __bt_hal_handle_multi_adv_enable(void *buf, uint16_t len)
119 {
120         struct hal_ev_multi_adv_enable *ev = buf;
121
122         if (bt_gatt_callbacks->server->multi_adv_enable_cb)
123                 bt_gatt_callbacks->server->multi_adv_enable_cb(ev->server_instance, ev->status);
124 }
125
126 static void __bt_hal_handle_multi_adv_disable(void *buf, uint16_t len)
127 {
128         struct hal_ev_multi_adv_disable *ev = buf;
129
130         if (bt_gatt_callbacks->server->multi_adv_disable_cb)
131                 bt_gatt_callbacks->server->multi_adv_disable_cb(ev->server_instance, ev->status);
132 }
133
134 /* Same callback for both Enable and DIsable */
135 static void __bt_hal_handle_legacy_adv_status(void *buf, uint16_t len)
136 {
137         struct hal_ev_legacy_adv_status *ev = buf;
138
139         if (bt_gatt_callbacks->server->listen_cb)
140                 bt_gatt_callbacks->server->listen_cb(ev->status, ev->server_instance);
141 }
142
143 static void __bt_hal_handle_service_added(void *buf, uint16_t len)
144 {
145         struct hal_ev_gatt_service_added *ev = buf;
146         btgatt_srvc_id_t srvc_id;
147
148         memset(&srvc_id, 0x00, sizeof(btgatt_srvc_id_t));
149         srvc_id.is_primary = ev->is_primary;
150         srvc_id.id.inst_id = ev->server_instance;
151         memcpy(srvc_id.id.uuid.uu, ev->svc_uuid, sizeof(ev->svc_uuid));
152
153         if (bt_gatt_callbacks->server->service_added_cb)
154                 bt_gatt_callbacks->server->service_added_cb(ev->status, ev->server_instance,
155                                 &srvc_id, ev->service_handle);
156 }
157
158 static void __bt_hal_handle_char_added(void *buf, uint16_t len)
159 {
160         struct hal_ev_gatt_char_added *ev = buf;
161         bt_uuid_t uuid;
162
163         memcpy(uuid.uu, ev->char_uuid, sizeof(ev->char_uuid));
164
165         if (bt_gatt_callbacks->server->characteristic_added_cb)
166                 bt_gatt_callbacks->server->characteristic_added_cb(ev->status, ev->server_instance,
167                                 &uuid, ev->service_handle, ev->char_handle);
168 }
169
170 static void __bt_hal_handle_desc_added(void *buf, uint16_t len)
171 {
172         struct hal_ev_gatt_desc_added *ev = buf;
173         bt_uuid_t uuid;
174
175         memcpy(uuid.uu, ev->desc_uuid, sizeof(ev->desc_uuid));
176
177         if (bt_gatt_callbacks->server->descriptor_added_cb)
178                 bt_gatt_callbacks->server->descriptor_added_cb(ev->status, ev->server_instance,
179                                 &uuid, ev->service_handle, ev->desc_handle);
180 }
181
182 static void __bt_hal_handle_service_started(void *buf, uint16_t len)
183 {
184         struct hal_ev_gatt_service_started *ev = buf;
185
186         if (bt_gatt_callbacks->server->service_started_cb)
187                 bt_gatt_callbacks->server->service_started_cb(ev->status, ev->server_instance,
188                                 ev->service_handle);
189 }
190
191 static void __bt_hal_handle_service_deleted(void *buf, uint16_t len)
192 {
193         struct hal_ev_gatt_service_deleted *ev = buf;
194
195         if (bt_gatt_callbacks->server->service_deleted_cb)
196                 bt_gatt_callbacks->server->service_deleted_cb(ev->status, ev->server_instance,
197                                 ev->service_handle);
198 }
199
200 static void __bt_hal_handle_gatt_server_connected(void *buf, uint16_t len)
201 {
202         struct hal_ev_gatt_server_connected *ev = buf;
203         bt_bdaddr_t bd_addr;
204
205         memcpy(bd_addr.address, ev->bdaddr, 6);
206
207         if (bt_gatt_callbacks->server->connection_cb)
208                 bt_gatt_callbacks->server->connection_cb(ev->conn_id, ev->server_instance,
209                                 ev->connected, &bd_addr);
210 }
211
212 static void __bt_hal_handle_gatt_server_notification_changed(void *buf, uint16_t len)
213 {
214         struct hal_ev_gatt_server_notifcation_change *ev = buf;
215         bt_bdaddr_t bd_addr;
216
217         memcpy(bd_addr.address, ev->bdaddr, 6);
218
219         if (bt_gatt_callbacks->server->notif_enabled_cb)
220                 bt_gatt_callbacks->server->notif_enabled_cb(ev->conn_id, ev->trans_id,
221                                 ev->att_handle, ev->notify, &bd_addr);
222 }
223
224 static void __bt_hal_handle_gatt_server_read_requested(void *buf, uint16_t len)
225 {
226         struct hal_ev_gatt_server_read_req *ev = buf;
227         bt_bdaddr_t bd_addr;
228
229         memcpy(bd_addr.address, ev->bdaddr, 6);
230
231         if (bt_gatt_callbacks->server->request_read_cb)
232                 bt_gatt_callbacks->server->request_read_cb(ev->conn_id, ev->trans_id, &bd_addr,
233                                         ev->att_handle, ev->offset, ev->is_long);
234 }
235
236 static void __bt_hal_handle_gatt_server_write_requested(void *buf, uint16_t len)
237 {
238         struct hal_ev_gatt_server_write_req *ev = buf;
239
240         if (bt_gatt_callbacks->server->request_write_cb)
241                 bt_gatt_callbacks->server->request_write_cb(ev->conn_id, ev->trans_id,
242                                 (bt_bdaddr_t *) ev->bdaddr,
243                                 ev->att_handle, ev->offset,
244                                 ev->length, ev->need_rsp,
245                                 ev->is_prep, ev->value);
246 }
247
248 static void __bt_hal_handle_gatt_server_acquire_write_requested(void *buf, uint16_t len)
249 {
250         struct hal_ev_gatt_server_acquire_write_res *ev = buf;
251
252         DBG("acquire write requested event recived");
253
254         if (bt_gatt_callbacks->server->request_acquire_write_cb)
255                 bt_gatt_callbacks->server->request_acquire_write_cb(ev->mtu, ev->conn_id, ev->trans_id, ev->char_handl, (bt_bdaddr_t *)ev->bdaddr);
256 }
257
258 static void __bt_hal_handle_gatt_server_indicate_confirmed(void *buf, uint16_t len)
259 {
260         struct hal_ev_gatt_server_indicate_cfm *ev = buf;
261
262         if (bt_gatt_callbacks->server->indication_confirmation_cb)
263                 bt_gatt_callbacks->server->indication_confirmation_cb(ev->conn_id, ev->trans_id,
264                                 ev->att_handle, (bt_bdaddr_t *) ev->bdaddr);
265
266 }
267
268 static void __bt_hal_gatt_events(int message, void *buf, uint16_t len)
269 {
270         DBG("+");
271         /* Check if GATT interface is Ready */
272         if (!interface_ready())
273                 return;
274
275         switch (message) {
276         case HAL_EV_SERVER_INSTANCE_INITIALIZED: {
277                 __bt_hal_handle_server_instance_initialized(buf, len);
278                 break;
279         }
280         case HAL_EV_MULTI_ADV_ENABLE: {
281                 __bt_hal_handle_multi_adv_enable(buf, len);
282                 break;
283         }
284         case HAL_EV_MULTI_ADV_DISABLE: {
285                 __bt_hal_handle_multi_adv_disable(buf, len);
286                 break;
287         }
288         case HAL_EV_MULTI_ADV_DATA_SET: {
289                 __bt_hal_handle_multi_adv_data_set(buf, len);
290                 break;
291         }
292         case HAL_EV_MULTI_ADV_UPDATE: {
293                 __bt_hal_handle_multi_adv_data_update(buf, len);
294                 break;
295         }
296         case HAL_EV_LEGACY_ADV_ENABLE: {
297                 __bt_hal_handle_legacy_adv_status(buf, len);
298                 break;
299         }
300         case HAL_EV_GATT_CLIENT_REGISTERED: {
301                 __bt_handle_gatt_client_registered(buf, len);
302                 break;
303         }
304         case HAL_EV_GATT_CLIENT_SCAN_RESULT: {
305                 __bt_hal_handle_gatt_client_scan_result(buf, len);
306                 break;
307         }
308         case HAL_EV_GATT_SERVICE_ADDED: {
309                 __bt_hal_handle_service_added(buf, len);
310                 break;
311         }
312         case HAL_EV_GATT_CHAR_ADDED: {
313                 __bt_hal_handle_char_added(buf, len);
314                 break;
315         }
316         case HAL_EV_GATT_DESC_ADDED: {
317                 __bt_hal_handle_desc_added(buf, len);
318                 break;
319         }
320         case HAL_EV_GATT_SERVICE_STARTED: {
321                 __bt_hal_handle_service_started(buf, len);
322                 break;
323         }
324         case HAL_EV_GATT_SERVICE_DELETED: {
325                 __bt_hal_handle_service_deleted(buf, len);
326                 break;
327         }
328         case HAL_EV_GATT_SERVER_CONNECTED: {
329                 __bt_hal_handle_gatt_server_connected(buf, len);
330                 break;
331         }
332         case HAL_EV_GATT_NOTIFICATION_CHANGE: {
333                 __bt_hal_handle_gatt_server_notification_changed(buf, len);
334                 break;
335         }
336         case HAL_EV_GATT_READ_REQUESTED: {
337                 __bt_hal_handle_gatt_server_read_requested(buf, len);
338                 break;
339         }
340         case HAL_EV_GATT_WRITE_REQUESTED: {
341                 __bt_hal_handle_gatt_server_write_requested(buf, len);
342                 break;
343         }
344         case HAL_EV_GATT_INDICATE_CFM: {
345                 __bt_hal_handle_gatt_server_indicate_confirmed(buf, len);
346                 break;
347         }
348         case HAL_EV_GATT_CLIENT_CONNECTED: {
349                 __bt_handle_gatt_client_connected(buf, len);
350                 break;
351         }
352         case HAL_EV_GATT_CLIENT_DISCONNECTED: {
353                 __bt_handle_gatt_client_disconnected(buf, len);
354                 break;
355         }
356         case HAL_EV_GATT_CLIENT_SEARCH_RESULT: {
357                 __bt_handle_gatt_client_search_result(buf, len);
358                 break;
359         }
360         case HAL_EV_GATT_CLIENT_SEARCH_COMPLETE: {
361                 __bt_handle_gatt_client_search_complete(buf, len);
362                 break;
363         }
364         case HAL_EV_GATT_CLIENT_CHARAC_SEARCH_RESULT: {
365                 __bt_handle_gatt_client_search_char_result(buf, len);
366                 break;
367         }
368         case HAL_EV_GATT_CLIENT_DESC_SEARCH_RESULT: {
369                 __bt_handle_gatt_client_search_desc_result(buf, len);
370                 break;
371         }
372         case HAL_EV_GATT_CLIENT_READ_CHARAC: {
373                 __bt_handle_gatt_client_read_charac(buf, len);
374                 break;
375         }
376         case HAL_EV_GATT_CLIENT_READ_DESC: {
377                 __bt_handle_gatt_client_read_desc(buf, len);
378                 break;
379         }
380         case HAL_EV_GATT_CLIENT_WRITE_CHARAC: {
381                 __bt_handle_gatt_client_write_char(buf, len);
382                 break;
383         }
384         case HAL_EV_GATT_CLIENT_WRITE_DESC: {
385                 __bt_handle_gatt_client_write_desc(buf, len);
386                 break;
387         }
388         case HAL_EV_GATT_CLIENT_WATCH_NOTIFICATION: {
389                 __bt_handle_gatt_client_watch_notification(buf, len);
390                 break;
391         }
392         case HAL_EV_GATT_SERVER_ACQUIRE_WRITE_RES:{
393                 __bt_hal_handle_gatt_server_acquire_write_requested(buf, len);
394                 break;
395         }
396
397         default:
398                 DBG("Event Currently not handled!!");
399                 break;
400         }
401         DBG("-");
402 }
403
404 /************************************* GATT CLIENT EVENTS ****************************************/
405 static void __bt_handle_gatt_client_registered(void *buf, uint16_t len)
406 {
407         struct hal_ev_gatt_client_registered *ev = buf;
408         bt_uuid_t uuid;
409
410         memcpy(uuid.uu, ev->app_uuid, 16);
411         if (bt_gatt_callbacks->client->register_client_cb)
412                 bt_gatt_callbacks->client->register_client_cb(
413                                 ev->status, ev->client_if, &uuid);
414 }
415
416 static void __bt_hal_send_le_scan_result_event(bt_hal_le_adv_info_t *adv_info)
417 {
418         bt_bdaddr_t bd_addr;
419
420         str2bt_bdaddr_t(adv_info->addr, &bd_addr);
421         if (bt_gatt_callbacks->client->scan_result_cb)
422                 bt_gatt_callbacks->client->scan_result_cb(
423                                 &bd_addr, adv_info->rssi, adv_info->data);
424 }
425
426 static void __bt_handle_gatt_client_connected(void *buf, uint16_t len)
427 {
428         struct hal_ev_gatt_client_connected  *ev = buf;
429         bt_bdaddr_t bd_addr;
430
431         memcpy(bd_addr.address, ev->bdaddr, 6);
432         if (bt_gatt_callbacks->client->open_cb)
433                 bt_gatt_callbacks->client->open_cb(ev->conn_id,
434                                 ev->status, ev->client_if, &bd_addr);
435 }
436
437 static void __bt_handle_gatt_client_disconnected(void *buf, uint16_t len)
438 {
439         struct hal_ev_gatt_client_connected  *ev = buf;
440         bt_bdaddr_t bd_addr;
441
442         memcpy(bd_addr.address, ev->bdaddr, 6);
443         if (bt_gatt_callbacks->client->close_cb)
444                 bt_gatt_callbacks->client->close_cb(ev->conn_id,
445                                 ev->status, ev->client_if, &bd_addr);
446 }
447
448 static void __bt_handle_gatt_client_search_result(void *buf, uint16_t len)
449 {
450         struct hal_ev_gatt_client_search_result  *ev = buf;
451         btgatt_srvc_id_t gatt_srvc_id;
452
453         gatt_srvc_id.is_primary = ev->is_primary;
454         gatt_srvc_id.id.inst_id = ev->inst_id;
455         memcpy(gatt_srvc_id.id.uuid.uu, ev->uuid, 16);
456
457         if (bt_gatt_callbacks->client->search_result_cb)
458                 bt_gatt_callbacks->client->search_result_cb(ev->conn_id,
459                                                         &gatt_srvc_id);
460 }
461
462 static void __bt_handle_gatt_client_search_complete(void *buf, uint16_t len)
463 {
464         struct hal_ev_gatt_client_search_complete  *ev = buf;
465
466         if (bt_gatt_callbacks->client->search_complete_cb)
467                 bt_gatt_callbacks->client->search_complete_cb(ev->conn_id,
468                                                                 ev->status);
469 }
470
471 static void __bt_handle_gatt_client_search_char_result(void *buf, uint16_t len)
472 {
473         struct hal_ev_gatt_client_char_search_result *ev = buf;
474         btgatt_srvc_id_t gatt_srvc_id;
475         btgatt_gatt_id_t gatt_char_id;
476
477         gatt_srvc_id.is_primary = ev->is_primary;
478         gatt_srvc_id.id.inst_id = ev->inst_id;
479         memcpy(gatt_srvc_id.id.uuid.uu, ev->svc_uuid, 16);
480
481         if (BT_STATUS_SUCCESS == ev->status) {
482                 memcpy(gatt_char_id.uuid.uu, ev->char_uuid, 16);
483                 gatt_char_id.inst_id = ev->inst_id;
484         }
485
486         if (bt_gatt_callbacks->client->get_characteristic_cb)
487                 bt_gatt_callbacks->client->get_characteristic_cb(ev->conn_id,
488                         ev->status, &gatt_srvc_id, &gatt_char_id, ev->char_prop);
489 }
490
491 static void __bt_handle_gatt_client_search_desc_result(void *buf, uint16_t len)
492 {
493         struct hal_ev_gatt_client_desc_search_result *ev = buf;
494         btgatt_srvc_id_t gatt_srvc_id;
495         btgatt_gatt_id_t gatt_char_id;
496         btgatt_gatt_id_t gatt_desc_id;
497
498
499         gatt_srvc_id.is_primary = ev->is_primary;
500         gatt_srvc_id.id.inst_id = ev->inst_id;
501         memcpy(gatt_srvc_id.id.uuid.uu, ev->svc_uuid, 16);
502
503         memcpy(gatt_char_id.uuid.uu, ev->char_uuid, 16);
504         gatt_char_id.inst_id = ev->inst_id;
505
506         if (BT_STATUS_SUCCESS == ev->status) {
507                 memcpy(gatt_desc_id.uuid.uu, ev->desc_uuid, 16);
508                 gatt_desc_id.inst_id = ev->inst_id;
509         }
510
511         if (bt_gatt_callbacks->client->get_descriptor_cb)
512                 bt_gatt_callbacks->client->get_descriptor_cb(ev->conn_id,
513                         ev->status, &gatt_srvc_id, &gatt_char_id, &gatt_desc_id);
514 }
515
516 static void __bt_handle_gatt_client_read_charac(void *buf, uint16_t len)
517 {
518         struct hal_ev_gatt_client_read_data *ev = buf;
519         btgatt_read_params_t char_read_parm;
520
521         char_read_parm.srvc_id.is_primary = ev->is_primary;
522         char_read_parm.srvc_id.id.inst_id = ev->inst_id;
523         memcpy(char_read_parm.srvc_id.id.uuid.uu, ev->svc_uuid, 16);
524
525         char_read_parm.char_id.inst_id = ev->inst_id;
526         memcpy(char_read_parm.char_id.uuid.uu, ev->char_uuid, 16);
527
528         char_read_parm.value.len = ev->len;
529         if (ev->len > 0) {
530                 memcpy(char_read_parm.value.value, ev->value, ev->len);
531                 char_read_parm.value_type = BT_HAL_GATTC_READ_VALUE_TYPE_VALUE;
532         }
533
534         if (bt_gatt_callbacks->client->read_characteristic_cb)
535                 bt_gatt_callbacks->client->read_characteristic_cb(ev->conn_id,
536                                         ev->status, &char_read_parm);
537 }
538
539 static void __bt_handle_gatt_client_read_desc(void *buf, uint16_t len)
540 {
541         struct hal_ev_gatt_client_read_data *ev = buf;
542         btgatt_read_params_t desc_read_parm;
543
544         desc_read_parm.srvc_id.is_primary = ev->is_primary;
545         desc_read_parm.srvc_id.id.inst_id = ev->inst_id;
546         memcpy(desc_read_parm.srvc_id.id.uuid.uu, ev->svc_uuid, 16);
547
548         desc_read_parm.char_id.inst_id = ev->inst_id;
549         memcpy(desc_read_parm.char_id.uuid.uu, ev->char_uuid, 16);
550
551         desc_read_parm.descr_id.inst_id = ev->inst_id;
552         memcpy(desc_read_parm.descr_id.uuid.uu, ev->desc_uuid, 16);
553
554         desc_read_parm.value.len = ev->len;
555         if (ev->len > 0) {
556                 memcpy(desc_read_parm.value.value, ev->value, ev->len);
557                 desc_read_parm.value_type = BT_HAL_GATTC_READ_VALUE_TYPE_VALUE;
558         }
559
560         if (bt_gatt_callbacks->client->read_descriptor_cb)
561                 bt_gatt_callbacks->client->read_descriptor_cb(ev->conn_id,
562                                         ev->status, &desc_read_parm);
563 }
564
565 static void __bt_handle_gatt_client_write_char(void *buf, uint16_t len)
566 {
567         struct hal_ev_gatt_client_write_result *ev = buf;
568         btgatt_write_params_t char_write_parm;
569
570         char_write_parm.srvc_id.is_primary = ev->is_primary;
571         char_write_parm.srvc_id.id.inst_id = ev->inst_id;
572         memcpy(char_write_parm.srvc_id.id.uuid.uu, ev->svc_uuid, 16);
573
574         char_write_parm.char_id.inst_id = ev->inst_id;
575         memcpy(char_write_parm.char_id.uuid.uu, ev->char_uuid, 16);
576
577         if (bt_gatt_callbacks->client->write_characteristic_cb)
578                 bt_gatt_callbacks->client->write_characteristic_cb(ev->conn_id,
579                                         ev->status, &char_write_parm);
580 }
581
582 static void __bt_handle_gatt_client_write_desc(void *buf, uint16_t len)
583 {
584         struct hal_ev_gatt_client_write_result *ev = buf;
585         btgatt_write_params_t desc_write_parm;
586
587         desc_write_parm.srvc_id.is_primary = ev->is_primary;
588         desc_write_parm.srvc_id.id.inst_id = ev->inst_id;
589         memcpy(desc_write_parm.srvc_id.id.uuid.uu, ev->svc_uuid, 16);
590
591         desc_write_parm.char_id.inst_id = ev->inst_id;
592         memcpy(desc_write_parm.char_id.uuid.uu, ev->char_uuid, 16);
593
594         desc_write_parm.descr_id.inst_id = ev->inst_id;
595         memcpy(desc_write_parm.descr_id.uuid.uu, ev->desc_uuid, 16);
596
597         if (bt_gatt_callbacks->client->write_descriptor_cb)
598                 bt_gatt_callbacks->client->write_descriptor_cb(ev->conn_id,
599                                         ev->status, &desc_write_parm);
600 }
601
602 static void __bt_handle_gatt_client_watch_notification(void *buf, uint16_t len)
603 {
604         struct hal_ev_gatt_client_watch_notification *ev = buf;
605         btgatt_srvc_id_t gatt_srvc_id;
606         btgatt_gatt_id_t gatt_char_id;
607
608         gatt_srvc_id.is_primary = ev->is_primary;
609         gatt_srvc_id.id.inst_id = ev->inst_id;
610         memcpy(gatt_srvc_id.id.uuid.uu, ev->svc_uuid, 16);
611
612         memcpy(gatt_char_id.uuid.uu, ev->char_uuid, 16);
613         gatt_char_id.inst_id = ev->inst_id;
614
615         if (bt_gatt_callbacks->client->register_for_notification_cb)
616                 bt_gatt_callbacks->client->register_for_notification_cb(ev->client_if,
617                                 ev->registered, ev->status, &gatt_srvc_id, &gatt_char_id);
618 }
619
620 static bt_hal_le_adv_info_t *__bt_hal_get_adv_ind_info(char *addr)
621 {
622         GSList *l;
623
624         if (!addr)
625                 return NULL;
626
627         for (l = adv_ind_list; NULL != l; l = g_slist_next(l)) {
628                 bt_hal_le_adv_info_t *adv_info = l->data;
629
630                 if (!adv_info)
631                         continue;
632
633                 if (!g_strcmp0(adv_info->addr, addr))
634                         return adv_info;
635         }
636
637         return NULL;
638 }
639
640 static int __bt_hal_add_adv_ind_info(bt_hal_le_adv_info_t *adv_info)
641 {
642         if (!adv_info)
643                 return -1;
644
645         if (__bt_hal_get_adv_ind_info(adv_info->addr) != NULL) {
646                 DBG("Adv info already present");
647                 return -1;
648         }
649
650         adv_ind_list = g_slist_append(adv_ind_list, adv_info);
651         return 0;
652 }
653
654 static gboolean __bt_hal_adv_scan_req_timeout_cb(gpointer user_data)
655 {
656         bt_hal_le_adv_info_t *adv_info = user_data;
657
658         __bt_hal_send_le_scan_result_event(adv_info);
659         adv_ind_list = g_slist_remove(adv_ind_list, adv_info);
660         g_free(adv_info);
661         return FALSE;
662 }
663
664 static void __bt_hal_handle_gatt_client_scan_result(void *buf, uint16_t len)
665 {
666         bt_bdaddr_t bd_addr;
667         char address[18];
668         struct hal_ev_gatt_client_scan_result *ev = buf;
669         bt_hal_le_adv_info_t *adv_info;
670         int data_len = 0;
671
672         memcpy(bd_addr.address, ev->bd_addr, 6);
673         bt_bdaddr_t2str(&bd_addr, address);
674         data_len = (ev->len < BT_HAL_ADV_DATA_MAX_SIZE) ? ev->len : BT_HAL_ADV_DATA_MAX_SIZE;
675         if (data_len == 0 && ev->adv_type != BT_LE_ADV_SCAN_RSP) {
676                 ERR("Unexpected: Data len is 0");
677                 return;
678         }
679
680         if (ev->adv_type == BT_LE_ADV_SCAN_RSP) { /* SCAN_RSP */
681                 adv_info = __bt_hal_get_adv_ind_info(address);
682                 if (adv_info) {
683                         /* Copy scan response data in data field and send event */
684                         memcpy(&(adv_info->data[adv_info->data_len]), ev->adv_data, data_len);
685                         __bt_hal_send_le_scan_result_event(adv_info);
686                         adv_ind_list = g_slist_remove(adv_ind_list, adv_info);
687                         if (adv_info->timer_id)
688                                 g_source_remove(adv_info->timer_id);
689                         g_free(adv_info);
690                 }
691                 return;
692         }
693
694         /* ADV_IND */
695         adv_info = g_malloc0(sizeof(bt_hal_le_adv_info_t));
696         if (!adv_info) {
697                 ERR("Not enough memory");
698                 return;
699         }
700
701         g_strlcpy(adv_info->addr, address, 18);
702         adv_info->addr_type = ev->addr_type;
703         adv_info->adv_type = ev->adv_type;
704         adv_info->rssi = ev->rssi;
705         adv_info->data_len = data_len;
706         memcpy(adv_info->data, ev->adv_data, data_len);
707
708 #ifdef TIZEN_BT_HAL
709         if (_bt_hal_gatt_client_get_le_scan_type() == BT_GATTC_LE_SCAN_TYPE_PASSIVE) {
710                 __bt_hal_send_le_scan_result_event(adv_info);
711                 g_free(adv_info);
712                 return;
713         }
714 #endif
715         if (__bt_hal_add_adv_ind_info(adv_info))
716                 adv_info->timer_id = g_timeout_add(1000,
717                                 __bt_hal_adv_scan_req_timeout_cb,
718                                 (void *)adv_info);
719 }
720
721 /*******************************************************************************
722  **
723  ** Function            gatt_init
724  **
725  ** Description         Initializes the GATT interface
726  **
727  ** Returns             bt_status_t
728  **
729  *******************************************************************************/
730 static bt_status_t gatt_init(const btgatt_callbacks_t* callbacks)
731 {
732         bt_gatt_callbacks = callbacks;
733         DBG("Register A2DP Src events callback function");
734         _bt_hal_register_gatt_le_dbus_handler_cb(__bt_hal_gatt_events);
735         _bt_hal_register_gatt_server_handler_cb(__bt_hal_gatt_events);
736         _bt_hal_register_gatt_client_handler_cb(__bt_hal_gatt_events);
737         _bt_hal_register_event_handler_cb(HAL_GATT, __bt_hal_gatt_events);
738
739         return BT_STATUS_SUCCESS;
740 }
741
742 /*******************************************************************************
743  **
744  ** Function    gatt_cleanup
745  **
746  ** Description Closes the GATT interface
747  **
748  ** Returns     void
749  **
750  *******************************************************************************/
751 static void gatt_cleanup(void)
752 {
753         _bt_hal_unregister_gatt_le_dbus_handler_cb();
754         _bt_hal_unregister_gatt_client_handler_cb();
755         _bt_hal_unregister_gatt_server_handler_cb();
756         _bt_hal_unregister_event_handler_cb(HAL_GATT);
757
758         if (bt_gatt_callbacks)
759                 bt_gatt_callbacks = NULL;
760 }
761
762 static const btgatt_interface_t btgatt_interface = {
763         sizeof(btgatt_interface),
764
765         gatt_init,
766         gatt_cleanup,
767
768         &btgatt_client_interface,
769         &btgatt_server_interface,
770 };
771
772 /*******************************************************************************
773  **
774  ** Function            bt_get_gatt_interface
775  **
776  ** Description         Get the gatt callback interface
777  **
778  ** Returns             btgatt_interface_t
779  **
780  *******************************************************************************/
781 const btgatt_interface_t *bt_get_gatt_interface()
782 {
783         return &btgatt_interface;
784 }