dbeed8cb5abec0ce4df2cdf63726bd79da45a75f
[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 static void __bt_handle_gatt_client_changed_value(void *buf, uint16_t len);
88 /*****************************************************************************************************/
89
90 static bool interface_ready(void)
91 {
92         return bt_gatt_callbacks != NULL;
93 }
94
95 static void __bt_hal_handle_server_instance_initialized(void *buf, uint16_t len)
96 {
97         struct hal_ev_server_instance_registered *ev = buf;
98
99         if (bt_gatt_callbacks->server->register_server_cb)
100                 bt_gatt_callbacks->server->register_server_cb(ev->status, ev->server_instance, (bt_uuid_t*)&ev->app_uuid);
101 }
102
103 static void __bt_hal_handle_multi_adv_data_set(void *buf, uint16_t len)
104 {
105         struct hal_ev_multi_adv_data_set *ev = buf;
106
107         if (bt_gatt_callbacks->server->multi_adv_data_cb)
108                 bt_gatt_callbacks->server->multi_adv_data_cb(ev->server_instance, ev->status);
109 }
110
111 static void __bt_hal_handle_multi_adv_data_update(void *buf, uint16_t len)
112 {
113         struct hal_ev_multi_adv_update *ev = buf;
114
115         if (bt_gatt_callbacks->server->multi_adv_update_cb)
116                 bt_gatt_callbacks->server->multi_adv_update_cb(ev->server_instance, ev->status);
117 }
118
119 static void __bt_hal_handle_multi_adv_enable(void *buf, uint16_t len)
120 {
121         struct hal_ev_multi_adv_enable *ev = buf;
122
123         if (bt_gatt_callbacks->server->multi_adv_enable_cb)
124                 bt_gatt_callbacks->server->multi_adv_enable_cb(ev->server_instance, ev->status);
125 }
126
127 static void __bt_hal_handle_multi_adv_disable(void *buf, uint16_t len)
128 {
129         struct hal_ev_multi_adv_disable *ev = buf;
130
131         if (bt_gatt_callbacks->server->multi_adv_disable_cb)
132                 bt_gatt_callbacks->server->multi_adv_disable_cb(ev->server_instance, ev->status);
133 }
134
135 /* Same callback for both Enable and DIsable */
136 static void __bt_hal_handle_legacy_adv_status(void *buf, uint16_t len)
137 {
138         struct hal_ev_legacy_adv_status *ev = buf;
139
140         if (bt_gatt_callbacks->server->listen_cb)
141                 bt_gatt_callbacks->server->listen_cb(ev->status, ev->server_instance);
142 }
143
144 static void __bt_hal_handle_service_added(void *buf, uint16_t len)
145 {
146         struct hal_ev_gatt_service_added *ev = buf;
147         btgatt_srvc_id_t srvc_id;
148
149         memset(&srvc_id, 0x00, sizeof(btgatt_srvc_id_t));
150         srvc_id.is_primary = ev->is_primary;
151         srvc_id.id.inst_id = ev->server_instance;
152         memcpy(srvc_id.id.uuid.uu, ev->svc_uuid, sizeof(ev->svc_uuid));
153
154         if (bt_gatt_callbacks->server->service_added_cb)
155                 bt_gatt_callbacks->server->service_added_cb(ev->status, ev->server_instance,
156                                 &srvc_id, ev->service_handle);
157 }
158
159 static void __bt_hal_handle_char_added(void *buf, uint16_t len)
160 {
161         struct hal_ev_gatt_char_added *ev = buf;
162         bt_uuid_t uuid;
163
164         memcpy(uuid.uu, ev->char_uuid, sizeof(ev->char_uuid));
165
166         if (bt_gatt_callbacks->server->characteristic_added_cb)
167                 bt_gatt_callbacks->server->characteristic_added_cb(ev->status, ev->server_instance,
168                                 &uuid, ev->service_handle, ev->char_handle);
169 }
170
171 static void __bt_hal_handle_desc_added(void *buf, uint16_t len)
172 {
173         struct hal_ev_gatt_desc_added *ev = buf;
174         bt_uuid_t uuid;
175
176         memcpy(uuid.uu, ev->desc_uuid, sizeof(ev->desc_uuid));
177
178         if (bt_gatt_callbacks->server->descriptor_added_cb)
179                 bt_gatt_callbacks->server->descriptor_added_cb(ev->status, ev->server_instance,
180                                 &uuid, ev->service_handle, ev->desc_handle);
181 }
182
183 static void __bt_hal_handle_service_started(void *buf, uint16_t len)
184 {
185         struct hal_ev_gatt_service_started *ev = buf;
186
187         if (bt_gatt_callbacks->server->service_started_cb)
188                 bt_gatt_callbacks->server->service_started_cb(ev->status, ev->server_instance,
189                                 ev->service_handle);
190 }
191
192 static void __bt_hal_handle_service_deleted(void *buf, uint16_t len)
193 {
194         struct hal_ev_gatt_service_deleted *ev = buf;
195
196         if (bt_gatt_callbacks->server->service_deleted_cb)
197                 bt_gatt_callbacks->server->service_deleted_cb(ev->status, ev->server_instance,
198                                 ev->service_handle);
199 }
200
201 static void __bt_hal_handle_gatt_server_connected(void *buf, uint16_t len)
202 {
203         struct hal_ev_gatt_server_connected *ev = buf;
204         bt_bdaddr_t bd_addr;
205
206         memcpy(bd_addr.address, ev->bdaddr, 6);
207
208         if (bt_gatt_callbacks->server->connection_cb)
209                 bt_gatt_callbacks->server->connection_cb(ev->conn_id, ev->server_instance,
210                                 ev->connected, &bd_addr);
211 }
212
213 static void __bt_hal_handle_gatt_server_notification_changed(void *buf, uint16_t len)
214 {
215         struct hal_ev_gatt_server_notifcation_change *ev = buf;
216         bt_bdaddr_t bd_addr;
217
218         memcpy(bd_addr.address, ev->bdaddr, 6);
219
220         if (bt_gatt_callbacks->server->notif_enabled_cb)
221                 bt_gatt_callbacks->server->notif_enabled_cb(ev->conn_id, ev->trans_id,
222                                 ev->att_handle, ev->notify, &bd_addr);
223 }
224
225 static void __bt_hal_handle_gatt_server_read_requested(void *buf, uint16_t len)
226 {
227         struct hal_ev_gatt_server_read_req *ev = buf;
228         bt_bdaddr_t bd_addr;
229
230         memcpy(bd_addr.address, ev->bdaddr, 6);
231
232         if (bt_gatt_callbacks->server->request_read_cb)
233                 bt_gatt_callbacks->server->request_read_cb(ev->conn_id, ev->trans_id, &bd_addr,
234                                         ev->att_handle, ev->offset, ev->is_long);
235 }
236
237 static void __bt_hal_handle_gatt_server_write_requested(void *buf, uint16_t len)
238 {
239         struct hal_ev_gatt_server_write_req *ev = buf;
240
241         if (bt_gatt_callbacks->server->request_write_cb)
242                 bt_gatt_callbacks->server->request_write_cb(ev->conn_id, ev->trans_id,
243                                 (bt_bdaddr_t *) ev->bdaddr,
244                                 ev->att_handle, ev->offset,
245                                 ev->length, ev->need_rsp,
246                                 ev->is_prep, ev->value);
247 }
248
249 static void __bt_hal_handle_gatt_server_acquire_write_requested(void *buf, uint16_t len)
250 {
251         struct hal_ev_gatt_server_acquire_write_res *ev = buf;
252
253         DBG("acquire write requested event recived");
254
255         if (bt_gatt_callbacks->server->request_acquire_write_cb)
256                 bt_gatt_callbacks->server->request_acquire_write_cb(ev->mtu, ev->conn_id, ev->trans_id, ev->char_handl, (bt_bdaddr_t *)ev->bdaddr);
257 }
258
259 static void __bt_hal_handle_gatt_server_acquire_notify_requested(void *buf, uint16_t len)
260 {
261         struct hal_ev_gatt_server_acquire_notify *ev = buf;
262
263         DBG("acquire notify  requested event recived");
264
265         if (bt_gatt_callbacks->server->request_acquire_notify_cb)
266                 bt_gatt_callbacks->server->request_acquire_notify_cb(ev->mtu, ev->conn_id, ev->trans_id, ev->char_handl);
267 }
268
269 static void __bt_hal_handle_gatt_server_indicate_confirmed(void *buf, uint16_t len)
270 {
271         struct hal_ev_gatt_server_indicate_cfm *ev = buf;
272
273         if (bt_gatt_callbacks->server->indication_confirmation_cb)
274                 bt_gatt_callbacks->server->indication_confirmation_cb(ev->conn_id, ev->trans_id,
275                                 ev->att_handle, (bt_bdaddr_t *) ev->bdaddr);
276
277 }
278
279 static void __bt_hal_gatt_events(int message, void *buf, uint16_t len)
280 {
281         DBG("+");
282         /* Check if GATT interface is Ready */
283         if (!interface_ready())
284                 return;
285
286         switch (message) {
287         case HAL_EV_SERVER_INSTANCE_INITIALIZED: {
288                 __bt_hal_handle_server_instance_initialized(buf, len);
289                 break;
290         }
291         case HAL_EV_MULTI_ADV_ENABLE: {
292                 __bt_hal_handle_multi_adv_enable(buf, len);
293                 break;
294         }
295         case HAL_EV_MULTI_ADV_DISABLE: {
296                 __bt_hal_handle_multi_adv_disable(buf, len);
297                 break;
298         }
299         case HAL_EV_MULTI_ADV_DATA_SET: {
300                 __bt_hal_handle_multi_adv_data_set(buf, len);
301                 break;
302         }
303         case HAL_EV_MULTI_ADV_UPDATE: {
304                 __bt_hal_handle_multi_adv_data_update(buf, len);
305                 break;
306         }
307         case HAL_EV_LEGACY_ADV_ENABLE: {
308                 __bt_hal_handle_legacy_adv_status(buf, len);
309                 break;
310         }
311         case HAL_EV_GATT_CLIENT_REGISTERED: {
312                 __bt_handle_gatt_client_registered(buf, len);
313                 break;
314         }
315         case HAL_EV_GATT_CLIENT_SCAN_RESULT: {
316                 __bt_hal_handle_gatt_client_scan_result(buf, len);
317                 break;
318         }
319         case HAL_EV_GATT_SERVICE_ADDED: {
320                 __bt_hal_handle_service_added(buf, len);
321                 break;
322         }
323         case HAL_EV_GATT_CHAR_ADDED: {
324                 __bt_hal_handle_char_added(buf, len);
325                 break;
326         }
327         case HAL_EV_GATT_DESC_ADDED: {
328                 __bt_hal_handle_desc_added(buf, len);
329                 break;
330         }
331         case HAL_EV_GATT_SERVICE_STARTED: {
332                 __bt_hal_handle_service_started(buf, len);
333                 break;
334         }
335         case HAL_EV_GATT_SERVICE_DELETED: {
336                 __bt_hal_handle_service_deleted(buf, len);
337                 break;
338         }
339         case HAL_EV_GATT_SERVER_CONNECTED: {
340                 __bt_hal_handle_gatt_server_connected(buf, len);
341                 break;
342         }
343         case HAL_EV_GATT_NOTIFICATION_CHANGE: {
344                 __bt_hal_handle_gatt_server_notification_changed(buf, len);
345                 break;
346         }
347         case HAL_EV_GATT_READ_REQUESTED: {
348                 __bt_hal_handle_gatt_server_read_requested(buf, len);
349                 break;
350         }
351         case HAL_EV_GATT_WRITE_REQUESTED: {
352                 __bt_hal_handle_gatt_server_write_requested(buf, len);
353                 break;
354         }
355         case HAL_EV_GATT_INDICATE_CFM: {
356                 __bt_hal_handle_gatt_server_indicate_confirmed(buf, len);
357                 break;
358         }
359         case HAL_EV_GATT_CLIENT_CONNECTED: {
360                 __bt_handle_gatt_client_connected(buf, len);
361                 break;
362         }
363         case HAL_EV_GATT_CLIENT_DISCONNECTED: {
364                 __bt_handle_gatt_client_disconnected(buf, len);
365                 break;
366         }
367         case HAL_EV_GATT_CLIENT_SEARCH_RESULT: {
368                 __bt_handle_gatt_client_search_result(buf, len);
369                 break;
370         }
371         case HAL_EV_GATT_CLIENT_SEARCH_COMPLETE: {
372                 __bt_handle_gatt_client_search_complete(buf, len);
373                 break;
374         }
375         case HAL_EV_GATT_CLIENT_CHARAC_SEARCH_RESULT: {
376                 __bt_handle_gatt_client_search_char_result(buf, len);
377                 break;
378         }
379         case HAL_EV_GATT_CLIENT_DESC_SEARCH_RESULT: {
380                 __bt_handle_gatt_client_search_desc_result(buf, len);
381                 break;
382         }
383         case HAL_EV_GATT_CLIENT_READ_CHARAC: {
384                 __bt_handle_gatt_client_read_charac(buf, len);
385                 break;
386         }
387         case HAL_EV_GATT_CLIENT_READ_DESC: {
388                 __bt_handle_gatt_client_read_desc(buf, len);
389                 break;
390         }
391         case HAL_EV_GATT_CLIENT_WRITE_CHARAC: {
392                 __bt_handle_gatt_client_write_char(buf, len);
393                 break;
394         }
395         case HAL_EV_GATT_CLIENT_WRITE_DESC: {
396                 __bt_handle_gatt_client_write_desc(buf, len);
397                 break;
398         }
399         case HAL_EV_GATT_CLIENT_WATCH_NOTIFICATION: {
400                 __bt_handle_gatt_client_watch_notification(buf, len);
401                 break;
402         }
403         case HAL_EV_GATT_CLIENT_NOTIFY_CHANGED_VALUE: {
404                 __bt_handle_gatt_client_changed_value(buf, len);
405                 break;
406         }
407         case HAL_EV_GATT_SERVER_ACQUIRE_WRITE_RES:{
408                 __bt_hal_handle_gatt_server_acquire_write_requested(buf, len);
409                 break;
410         }
411         case HAL_EV_GATT_SERVER_ACQUIRE_NOTIFY_RES:{
412                 __bt_hal_handle_gatt_server_acquire_notify_requested(buf, len);
413                 break;
414         }
415
416         default:
417                 DBG("Event Currently not handled!!");
418                 break;
419         }
420         DBG("-");
421 }
422
423 /************************************* GATT CLIENT EVENTS ****************************************/
424 static void __bt_handle_gatt_client_registered(void *buf, uint16_t len)
425 {
426         struct hal_ev_gatt_client_registered *ev = buf;
427         bt_uuid_t uuid;
428
429         memcpy(uuid.uu, ev->app_uuid, 16);
430         if (bt_gatt_callbacks->client->register_client_cb)
431                 bt_gatt_callbacks->client->register_client_cb(
432                                 ev->status, ev->client_if, &uuid);
433 }
434
435 static void __bt_hal_send_le_scan_result_event(bt_hal_le_adv_info_t *adv_info)
436 {
437         bt_bdaddr_t bd_addr;
438
439         str2bt_bdaddr_t(adv_info->addr, &bd_addr);
440         if (bt_gatt_callbacks->client->scan_result_cb)
441                 bt_gatt_callbacks->client->scan_result_cb(
442                                 &bd_addr, adv_info->rssi, adv_info->data);
443 }
444
445 static void __bt_handle_gatt_client_connected(void *buf, uint16_t len)
446 {
447         struct hal_ev_gatt_client_connected  *ev = buf;
448         bt_bdaddr_t bd_addr;
449
450         memcpy(bd_addr.address, ev->bdaddr, 6);
451         if (bt_gatt_callbacks->client->open_cb)
452                 bt_gatt_callbacks->client->open_cb(ev->conn_id,
453                                 ev->status, ev->client_if, &bd_addr);
454 }
455
456 static void __bt_handle_gatt_client_disconnected(void *buf, uint16_t len)
457 {
458         struct hal_ev_gatt_client_connected  *ev = buf;
459         bt_bdaddr_t bd_addr;
460
461         memcpy(bd_addr.address, ev->bdaddr, 6);
462         if (bt_gatt_callbacks->client->close_cb)
463                 bt_gatt_callbacks->client->close_cb(ev->conn_id,
464                                 ev->status, ev->client_if, &bd_addr);
465 }
466
467 static void __bt_handle_gatt_client_search_result(void *buf, uint16_t len)
468 {
469         struct hal_ev_gatt_client_search_result  *ev = buf;
470         btgatt_srvc_id_t gatt_srvc_id;
471
472         gatt_srvc_id.is_primary = ev->is_primary;
473         gatt_srvc_id.id.inst_id = ev->inst_id;
474         memcpy(gatt_srvc_id.id.uuid.uu, ev->uuid, 16);
475
476         if (bt_gatt_callbacks->client->search_result_cb)
477                 bt_gatt_callbacks->client->search_result_cb(ev->conn_id,
478                                                         &gatt_srvc_id);
479 }
480
481 static void __bt_handle_gatt_client_search_complete(void *buf, uint16_t len)
482 {
483         struct hal_ev_gatt_client_search_complete  *ev = buf;
484
485         if (bt_gatt_callbacks->client->search_complete_cb)
486                 bt_gatt_callbacks->client->search_complete_cb(ev->conn_id,
487                                                                 ev->status);
488 }
489
490 static void __bt_handle_gatt_client_search_char_result(void *buf, uint16_t len)
491 {
492         struct hal_ev_gatt_client_char_search_result *ev = buf;
493         btgatt_srvc_id_t gatt_srvc_id;
494         btgatt_gatt_id_t gatt_char_id;
495
496         gatt_srvc_id.is_primary = ev->is_primary;
497         gatt_srvc_id.id.inst_id = ev->inst_id;
498         memcpy(gatt_srvc_id.id.uuid.uu, ev->svc_uuid, 16);
499
500         if (BT_STATUS_SUCCESS == ev->status) {
501                 memcpy(gatt_char_id.uuid.uu, ev->char_uuid, 16);
502                 gatt_char_id.inst_id = ev->inst_id;
503         }
504
505         if (bt_gatt_callbacks->client->get_characteristic_cb)
506                 bt_gatt_callbacks->client->get_characteristic_cb(ev->conn_id,
507                         ev->status, &gatt_srvc_id, &gatt_char_id, ev->char_prop);
508 }
509
510 static void __bt_handle_gatt_client_search_desc_result(void *buf, uint16_t len)
511 {
512         struct hal_ev_gatt_client_desc_search_result *ev = buf;
513         btgatt_srvc_id_t gatt_srvc_id;
514         btgatt_gatt_id_t gatt_char_id;
515         btgatt_gatt_id_t gatt_desc_id;
516
517
518         gatt_srvc_id.is_primary = ev->is_primary;
519         gatt_srvc_id.id.inst_id = ev->inst_id;
520         memcpy(gatt_srvc_id.id.uuid.uu, ev->svc_uuid, 16);
521
522         memcpy(gatt_char_id.uuid.uu, ev->char_uuid, 16);
523         gatt_char_id.inst_id = ev->inst_id;
524
525         if (BT_STATUS_SUCCESS == ev->status) {
526                 memcpy(gatt_desc_id.uuid.uu, ev->desc_uuid, 16);
527                 gatt_desc_id.inst_id = ev->inst_id;
528         }
529
530         if (bt_gatt_callbacks->client->get_descriptor_cb)
531                 bt_gatt_callbacks->client->get_descriptor_cb(ev->conn_id,
532                         ev->status, &gatt_srvc_id, &gatt_char_id, &gatt_desc_id);
533 }
534
535 static void __bt_handle_gatt_client_read_charac(void *buf, uint16_t len)
536 {
537         struct hal_ev_gatt_client_read_data *ev = buf;
538         btgatt_read_params_t char_read_parm;
539
540         char_read_parm.srvc_id.is_primary = ev->is_primary;
541         char_read_parm.srvc_id.id.inst_id = ev->inst_id;
542         memcpy(char_read_parm.srvc_id.id.uuid.uu, ev->svc_uuid, 16);
543
544         char_read_parm.char_id.inst_id = ev->inst_id;
545         memcpy(char_read_parm.char_id.uuid.uu, ev->char_uuid, 16);
546
547         char_read_parm.value.len = ev->len;
548         if (ev->len > 0) {
549                 memcpy(char_read_parm.value.value, ev->value, ev->len);
550                 char_read_parm.value_type = BT_HAL_GATTC_READ_VALUE_TYPE_VALUE;
551         }
552
553         if (bt_gatt_callbacks->client->read_characteristic_cb)
554                 bt_gatt_callbacks->client->read_characteristic_cb(ev->conn_id,
555                                         ev->status, &char_read_parm);
556 }
557
558 static void __bt_handle_gatt_client_read_desc(void *buf, uint16_t len)
559 {
560         struct hal_ev_gatt_client_read_data *ev = buf;
561         btgatt_read_params_t desc_read_parm;
562
563         desc_read_parm.srvc_id.is_primary = ev->is_primary;
564         desc_read_parm.srvc_id.id.inst_id = ev->inst_id;
565         memcpy(desc_read_parm.srvc_id.id.uuid.uu, ev->svc_uuid, 16);
566
567         desc_read_parm.char_id.inst_id = ev->inst_id;
568         memcpy(desc_read_parm.char_id.uuid.uu, ev->char_uuid, 16);
569
570         desc_read_parm.descr_id.inst_id = ev->inst_id;
571         memcpy(desc_read_parm.descr_id.uuid.uu, ev->desc_uuid, 16);
572
573         desc_read_parm.value.len = ev->len;
574         if (ev->len > 0) {
575                 memcpy(desc_read_parm.value.value, ev->value, ev->len);
576                 desc_read_parm.value_type = BT_HAL_GATTC_READ_VALUE_TYPE_VALUE;
577         }
578
579         if (bt_gatt_callbacks->client->read_descriptor_cb)
580                 bt_gatt_callbacks->client->read_descriptor_cb(ev->conn_id,
581                                         ev->status, &desc_read_parm);
582 }
583
584 static void __bt_handle_gatt_client_write_char(void *buf, uint16_t len)
585 {
586         struct hal_ev_gatt_client_write_result *ev = buf;
587         btgatt_write_params_t char_write_parm;
588
589         char_write_parm.srvc_id.is_primary = ev->is_primary;
590         char_write_parm.srvc_id.id.inst_id = ev->inst_id;
591         memcpy(char_write_parm.srvc_id.id.uuid.uu, ev->svc_uuid, 16);
592
593         char_write_parm.char_id.inst_id = ev->inst_id;
594         memcpy(char_write_parm.char_id.uuid.uu, ev->char_uuid, 16);
595
596         if (bt_gatt_callbacks->client->write_characteristic_cb)
597                 bt_gatt_callbacks->client->write_characteristic_cb(ev->conn_id,
598                                         ev->status, &char_write_parm);
599 }
600
601 static void __bt_handle_gatt_client_write_desc(void *buf, uint16_t len)
602 {
603         struct hal_ev_gatt_client_write_result *ev = buf;
604         btgatt_write_params_t desc_write_parm;
605
606         desc_write_parm.srvc_id.is_primary = ev->is_primary;
607         desc_write_parm.srvc_id.id.inst_id = ev->inst_id;
608         memcpy(desc_write_parm.srvc_id.id.uuid.uu, ev->svc_uuid, 16);
609
610         desc_write_parm.char_id.inst_id = ev->inst_id;
611         memcpy(desc_write_parm.char_id.uuid.uu, ev->char_uuid, 16);
612
613         desc_write_parm.descr_id.inst_id = ev->inst_id;
614         memcpy(desc_write_parm.descr_id.uuid.uu, ev->desc_uuid, 16);
615
616         if (bt_gatt_callbacks->client->write_descriptor_cb)
617                 bt_gatt_callbacks->client->write_descriptor_cb(ev->conn_id,
618                                         ev->status, &desc_write_parm);
619 }
620
621 static void __bt_handle_gatt_client_watch_notification(void *buf, uint16_t len)
622 {
623         struct hal_ev_gatt_client_watch_notification *ev = buf;
624         btgatt_srvc_id_t gatt_srvc_id;
625         btgatt_gatt_id_t gatt_char_id;
626
627         gatt_srvc_id.is_primary = ev->is_primary;
628         gatt_srvc_id.id.inst_id = ev->inst_id;
629         memcpy(gatt_srvc_id.id.uuid.uu, ev->svc_uuid, 16);
630
631         memcpy(gatt_char_id.uuid.uu, ev->char_uuid, 16);
632         gatt_char_id.inst_id = ev->inst_id;
633
634         if (bt_gatt_callbacks->client->register_for_notification_cb)
635                 bt_gatt_callbacks->client->register_for_notification_cb(ev->client_if,
636                                 ev->registered, ev->status, &gatt_srvc_id, &gatt_char_id);
637 }
638
639 static void __bt_handle_gatt_client_changed_value(void *buf, uint16_t len)
640 {
641         struct hal_ev_gatt_client_notify_changed_value *ev = buf;
642         btgatt_notify_params_t changd_value_parm;
643
644         changd_value_parm.srvc_id.is_primary = ev->is_primary;
645         changd_value_parm.srvc_id.id.inst_id = ev->inst_id;
646         memcpy(changd_value_parm.srvc_id.id.uuid.uu, ev->svc_uuid, 16);
647
648         changd_value_parm.char_id.inst_id = ev->inst_id;
649         memcpy(changd_value_parm.char_id.uuid.uu, ev->char_uuid, 16);
650         changd_value_parm.is_notify = ev->is_notify;
651
652         memcpy(changd_value_parm.bda.address, ev->bdaddr, 6);
653
654         changd_value_parm.len = ev->len;
655         if (ev->len > 0)
656                 memcpy(changd_value_parm.value, ev->value, ev->len);
657
658         if (bt_gatt_callbacks->client->notify_cb)
659                 bt_gatt_callbacks->client->notify_cb(ev->conn_id, &changd_value_parm);
660 }
661
662 static bt_hal_le_adv_info_t *__bt_hal_get_adv_ind_info(char *addr)
663 {
664         GSList *l;
665
666         if (!addr)
667                 return NULL;
668
669         for (l = adv_ind_list; NULL != l; l = g_slist_next(l)) {
670                 bt_hal_le_adv_info_t *adv_info = l->data;
671
672                 if (!adv_info)
673                         continue;
674
675                 if (!g_strcmp0(adv_info->addr, addr))
676                         return adv_info;
677         }
678
679         return NULL;
680 }
681
682 static int __bt_hal_add_adv_ind_info(bt_hal_le_adv_info_t *adv_info)
683 {
684         if (!adv_info)
685                 return -1;
686
687         if (__bt_hal_get_adv_ind_info(adv_info->addr) != NULL) {
688                 DBG("Adv info already present");
689                 return -1;
690         }
691
692         adv_ind_list = g_slist_append(adv_ind_list, adv_info);
693         return 0;
694 }
695
696 static gboolean __bt_hal_adv_scan_req_timeout_cb(gpointer user_data)
697 {
698         bt_hal_le_adv_info_t *adv_info = user_data;
699
700         __bt_hal_send_le_scan_result_event(adv_info);
701         adv_ind_list = g_slist_remove(adv_ind_list, adv_info);
702         g_free(adv_info);
703         return FALSE;
704 }
705
706 static void __bt_hal_handle_gatt_client_scan_result(void *buf, uint16_t len)
707 {
708         bt_bdaddr_t bd_addr;
709         char address[18];
710         struct hal_ev_gatt_client_scan_result *ev = buf;
711         bt_hal_le_adv_info_t *adv_info;
712         int data_len = 0;
713
714         memcpy(bd_addr.address, ev->bd_addr, 6);
715         bt_bdaddr_t2str(&bd_addr, address);
716         data_len = (ev->len < BT_HAL_ADV_DATA_MAX_SIZE) ? ev->len : BT_HAL_ADV_DATA_MAX_SIZE;
717         if (data_len == 0 && ev->adv_type != BT_LE_ADV_SCAN_RSP) {
718                 ERR("Unexpected: Data len is 0");
719                 return;
720         }
721
722         if (ev->adv_type == BT_LE_ADV_SCAN_RSP) { /* SCAN_RSP */
723                 adv_info = __bt_hal_get_adv_ind_info(address);
724                 if (adv_info) {
725                         /* Copy scan response data in data field and send event */
726                         memcpy(&(adv_info->data[adv_info->data_len]), ev->adv_data, data_len);
727                         __bt_hal_send_le_scan_result_event(adv_info);
728                         adv_ind_list = g_slist_remove(adv_ind_list, adv_info);
729                         if (adv_info->timer_id)
730                                 g_source_remove(adv_info->timer_id);
731                         g_free(adv_info);
732                 }
733                 return;
734         }
735
736         /* ADV_IND */
737         adv_info = g_malloc0(sizeof(bt_hal_le_adv_info_t));
738         if (!adv_info) {
739                 ERR("Not enough memory");
740                 return;
741         }
742
743         g_strlcpy(adv_info->addr, address, 18);
744         adv_info->addr_type = ev->addr_type;
745         adv_info->adv_type = ev->adv_type;
746         adv_info->rssi = ev->rssi;
747         adv_info->data_len = data_len;
748         memcpy(adv_info->data, ev->adv_data, data_len);
749
750 #ifdef TIZEN_BT_HAL
751         if (_bt_hal_gatt_client_get_le_scan_type() == BT_GATTC_LE_SCAN_TYPE_PASSIVE) {
752                 __bt_hal_send_le_scan_result_event(adv_info);
753                 g_free(adv_info);
754                 return;
755         }
756 #endif
757         if (__bt_hal_add_adv_ind_info(adv_info))
758                 adv_info->timer_id = g_timeout_add(1000,
759                                 __bt_hal_adv_scan_req_timeout_cb,
760                                 (void *)adv_info);
761 }
762
763 /*******************************************************************************
764  **
765  ** Function            gatt_init
766  **
767  ** Description         Initializes the GATT interface
768  **
769  ** Returns             bt_status_t
770  **
771  *******************************************************************************/
772 static bt_status_t gatt_init(const btgatt_callbacks_t* callbacks)
773 {
774         bt_gatt_callbacks = callbacks;
775         DBG("Register A2DP Src events callback function");
776         _bt_hal_register_gatt_le_dbus_handler_cb(__bt_hal_gatt_events);
777         _bt_hal_register_gatt_server_handler_cb(__bt_hal_gatt_events);
778         _bt_hal_register_gatt_client_handler_cb(__bt_hal_gatt_events);
779         _bt_hal_register_event_handler_cb(HAL_GATT, __bt_hal_gatt_events);
780
781         return BT_STATUS_SUCCESS;
782 }
783
784 /*******************************************************************************
785  **
786  ** Function    gatt_cleanup
787  **
788  ** Description Closes the GATT interface
789  **
790  ** Returns     void
791  **
792  *******************************************************************************/
793 static void gatt_cleanup(void)
794 {
795         _bt_hal_unregister_gatt_le_dbus_handler_cb();
796         _bt_hal_unregister_gatt_client_handler_cb();
797         _bt_hal_unregister_gatt_server_handler_cb();
798         _bt_hal_unregister_event_handler_cb(HAL_GATT);
799
800         if (bt_gatt_callbacks)
801                 bt_gatt_callbacks = NULL;
802 }
803
804 static const btgatt_interface_t btgatt_interface = {
805         sizeof(btgatt_interface),
806
807         gatt_init,
808         gatt_cleanup,
809
810         &btgatt_client_interface,
811         &btgatt_server_interface,
812 };
813
814 /*******************************************************************************
815  **
816  ** Function            bt_get_gatt_interface
817  **
818  ** Description         Get the gatt callback interface
819  **
820  ** Returns             btgatt_interface_t
821  **
822  *******************************************************************************/
823 const btgatt_interface_t *bt_get_gatt_interface()
824 {
825         return &btgatt_interface;
826 }