BLE Gatt Server Socket notify implementation.
[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_acquire_notify_requested(void *buf, uint16_t len)
259 {
260         struct hal_ev_gatt_server_acquire_notify *ev = buf;
261
262         DBG("acquire notify  requested event recived");
263
264         if (bt_gatt_callbacks->server->request_acquire_notify_cb)
265                 bt_gatt_callbacks->server->request_acquire_notify_cb(ev->mtu, ev->conn_id, ev->trans_id, ev->char_handl);
266 }
267
268 static void __bt_hal_handle_gatt_server_indicate_confirmed(void *buf, uint16_t len)
269 {
270         struct hal_ev_gatt_server_indicate_cfm *ev = buf;
271
272         if (bt_gatt_callbacks->server->indication_confirmation_cb)
273                 bt_gatt_callbacks->server->indication_confirmation_cb(ev->conn_id, ev->trans_id,
274                                 ev->att_handle, (bt_bdaddr_t *) ev->bdaddr);
275
276 }
277
278 static void __bt_hal_gatt_events(int message, void *buf, uint16_t len)
279 {
280         DBG("+");
281         /* Check if GATT interface is Ready */
282         if (!interface_ready())
283                 return;
284
285         switch (message) {
286         case HAL_EV_SERVER_INSTANCE_INITIALIZED: {
287                 __bt_hal_handle_server_instance_initialized(buf, len);
288                 break;
289         }
290         case HAL_EV_MULTI_ADV_ENABLE: {
291                 __bt_hal_handle_multi_adv_enable(buf, len);
292                 break;
293         }
294         case HAL_EV_MULTI_ADV_DISABLE: {
295                 __bt_hal_handle_multi_adv_disable(buf, len);
296                 break;
297         }
298         case HAL_EV_MULTI_ADV_DATA_SET: {
299                 __bt_hal_handle_multi_adv_data_set(buf, len);
300                 break;
301         }
302         case HAL_EV_MULTI_ADV_UPDATE: {
303                 __bt_hal_handle_multi_adv_data_update(buf, len);
304                 break;
305         }
306         case HAL_EV_LEGACY_ADV_ENABLE: {
307                 __bt_hal_handle_legacy_adv_status(buf, len);
308                 break;
309         }
310         case HAL_EV_GATT_CLIENT_REGISTERED: {
311                 __bt_handle_gatt_client_registered(buf, len);
312                 break;
313         }
314         case HAL_EV_GATT_CLIENT_SCAN_RESULT: {
315                 __bt_hal_handle_gatt_client_scan_result(buf, len);
316                 break;
317         }
318         case HAL_EV_GATT_SERVICE_ADDED: {
319                 __bt_hal_handle_service_added(buf, len);
320                 break;
321         }
322         case HAL_EV_GATT_CHAR_ADDED: {
323                 __bt_hal_handle_char_added(buf, len);
324                 break;
325         }
326         case HAL_EV_GATT_DESC_ADDED: {
327                 __bt_hal_handle_desc_added(buf, len);
328                 break;
329         }
330         case HAL_EV_GATT_SERVICE_STARTED: {
331                 __bt_hal_handle_service_started(buf, len);
332                 break;
333         }
334         case HAL_EV_GATT_SERVICE_DELETED: {
335                 __bt_hal_handle_service_deleted(buf, len);
336                 break;
337         }
338         case HAL_EV_GATT_SERVER_CONNECTED: {
339                 __bt_hal_handle_gatt_server_connected(buf, len);
340                 break;
341         }
342         case HAL_EV_GATT_NOTIFICATION_CHANGE: {
343                 __bt_hal_handle_gatt_server_notification_changed(buf, len);
344                 break;
345         }
346         case HAL_EV_GATT_READ_REQUESTED: {
347                 __bt_hal_handle_gatt_server_read_requested(buf, len);
348                 break;
349         }
350         case HAL_EV_GATT_WRITE_REQUESTED: {
351                 __bt_hal_handle_gatt_server_write_requested(buf, len);
352                 break;
353         }
354         case HAL_EV_GATT_INDICATE_CFM: {
355                 __bt_hal_handle_gatt_server_indicate_confirmed(buf, len);
356                 break;
357         }
358         case HAL_EV_GATT_CLIENT_CONNECTED: {
359                 __bt_handle_gatt_client_connected(buf, len);
360                 break;
361         }
362         case HAL_EV_GATT_CLIENT_DISCONNECTED: {
363                 __bt_handle_gatt_client_disconnected(buf, len);
364                 break;
365         }
366         case HAL_EV_GATT_CLIENT_SEARCH_RESULT: {
367                 __bt_handle_gatt_client_search_result(buf, len);
368                 break;
369         }
370         case HAL_EV_GATT_CLIENT_SEARCH_COMPLETE: {
371                 __bt_handle_gatt_client_search_complete(buf, len);
372                 break;
373         }
374         case HAL_EV_GATT_CLIENT_CHARAC_SEARCH_RESULT: {
375                 __bt_handle_gatt_client_search_char_result(buf, len);
376                 break;
377         }
378         case HAL_EV_GATT_CLIENT_DESC_SEARCH_RESULT: {
379                 __bt_handle_gatt_client_search_desc_result(buf, len);
380                 break;
381         }
382         case HAL_EV_GATT_CLIENT_READ_CHARAC: {
383                 __bt_handle_gatt_client_read_charac(buf, len);
384                 break;
385         }
386         case HAL_EV_GATT_CLIENT_READ_DESC: {
387                 __bt_handle_gatt_client_read_desc(buf, len);
388                 break;
389         }
390         case HAL_EV_GATT_CLIENT_WRITE_CHARAC: {
391                 __bt_handle_gatt_client_write_char(buf, len);
392                 break;
393         }
394         case HAL_EV_GATT_CLIENT_WRITE_DESC: {
395                 __bt_handle_gatt_client_write_desc(buf, len);
396                 break;
397         }
398         case HAL_EV_GATT_CLIENT_WATCH_NOTIFICATION: {
399                 __bt_handle_gatt_client_watch_notification(buf, len);
400                 break;
401         }
402         case HAL_EV_GATT_SERVER_ACQUIRE_WRITE_RES:{
403                 __bt_hal_handle_gatt_server_acquire_write_requested(buf, len);
404                 break;
405         }
406         case HAL_EV_GATT_SERVER_ACQUIRE_NOTIFY_RES:{
407                 __bt_hal_handle_gatt_server_acquire_notify_requested(buf, len);
408                 break;
409         }
410
411         default:
412                 DBG("Event Currently not handled!!");
413                 break;
414         }
415         DBG("-");
416 }
417
418 /************************************* GATT CLIENT EVENTS ****************************************/
419 static void __bt_handle_gatt_client_registered(void *buf, uint16_t len)
420 {
421         struct hal_ev_gatt_client_registered *ev = buf;
422         bt_uuid_t uuid;
423
424         memcpy(uuid.uu, ev->app_uuid, 16);
425         if (bt_gatt_callbacks->client->register_client_cb)
426                 bt_gatt_callbacks->client->register_client_cb(
427                                 ev->status, ev->client_if, &uuid);
428 }
429
430 static void __bt_hal_send_le_scan_result_event(bt_hal_le_adv_info_t *adv_info)
431 {
432         bt_bdaddr_t bd_addr;
433
434         str2bt_bdaddr_t(adv_info->addr, &bd_addr);
435         if (bt_gatt_callbacks->client->scan_result_cb)
436                 bt_gatt_callbacks->client->scan_result_cb(
437                                 &bd_addr, adv_info->rssi, adv_info->data);
438 }
439
440 static void __bt_handle_gatt_client_connected(void *buf, uint16_t len)
441 {
442         struct hal_ev_gatt_client_connected  *ev = buf;
443         bt_bdaddr_t bd_addr;
444
445         memcpy(bd_addr.address, ev->bdaddr, 6);
446         if (bt_gatt_callbacks->client->open_cb)
447                 bt_gatt_callbacks->client->open_cb(ev->conn_id,
448                                 ev->status, ev->client_if, &bd_addr);
449 }
450
451 static void __bt_handle_gatt_client_disconnected(void *buf, uint16_t len)
452 {
453         struct hal_ev_gatt_client_connected  *ev = buf;
454         bt_bdaddr_t bd_addr;
455
456         memcpy(bd_addr.address, ev->bdaddr, 6);
457         if (bt_gatt_callbacks->client->close_cb)
458                 bt_gatt_callbacks->client->close_cb(ev->conn_id,
459                                 ev->status, ev->client_if, &bd_addr);
460 }
461
462 static void __bt_handle_gatt_client_search_result(void *buf, uint16_t len)
463 {
464         struct hal_ev_gatt_client_search_result  *ev = buf;
465         btgatt_srvc_id_t gatt_srvc_id;
466
467         gatt_srvc_id.is_primary = ev->is_primary;
468         gatt_srvc_id.id.inst_id = ev->inst_id;
469         memcpy(gatt_srvc_id.id.uuid.uu, ev->uuid, 16);
470
471         if (bt_gatt_callbacks->client->search_result_cb)
472                 bt_gatt_callbacks->client->search_result_cb(ev->conn_id,
473                                                         &gatt_srvc_id);
474 }
475
476 static void __bt_handle_gatt_client_search_complete(void *buf, uint16_t len)
477 {
478         struct hal_ev_gatt_client_search_complete  *ev = buf;
479
480         if (bt_gatt_callbacks->client->search_complete_cb)
481                 bt_gatt_callbacks->client->search_complete_cb(ev->conn_id,
482                                                                 ev->status);
483 }
484
485 static void __bt_handle_gatt_client_search_char_result(void *buf, uint16_t len)
486 {
487         struct hal_ev_gatt_client_char_search_result *ev = buf;
488         btgatt_srvc_id_t gatt_srvc_id;
489         btgatt_gatt_id_t gatt_char_id;
490
491         gatt_srvc_id.is_primary = ev->is_primary;
492         gatt_srvc_id.id.inst_id = ev->inst_id;
493         memcpy(gatt_srvc_id.id.uuid.uu, ev->svc_uuid, 16);
494
495         if (BT_STATUS_SUCCESS == ev->status) {
496                 memcpy(gatt_char_id.uuid.uu, ev->char_uuid, 16);
497                 gatt_char_id.inst_id = ev->inst_id;
498         }
499
500         if (bt_gatt_callbacks->client->get_characteristic_cb)
501                 bt_gatt_callbacks->client->get_characteristic_cb(ev->conn_id,
502                         ev->status, &gatt_srvc_id, &gatt_char_id, ev->char_prop);
503 }
504
505 static void __bt_handle_gatt_client_search_desc_result(void *buf, uint16_t len)
506 {
507         struct hal_ev_gatt_client_desc_search_result *ev = buf;
508         btgatt_srvc_id_t gatt_srvc_id;
509         btgatt_gatt_id_t gatt_char_id;
510         btgatt_gatt_id_t gatt_desc_id;
511
512
513         gatt_srvc_id.is_primary = ev->is_primary;
514         gatt_srvc_id.id.inst_id = ev->inst_id;
515         memcpy(gatt_srvc_id.id.uuid.uu, ev->svc_uuid, 16);
516
517         memcpy(gatt_char_id.uuid.uu, ev->char_uuid, 16);
518         gatt_char_id.inst_id = ev->inst_id;
519
520         if (BT_STATUS_SUCCESS == ev->status) {
521                 memcpy(gatt_desc_id.uuid.uu, ev->desc_uuid, 16);
522                 gatt_desc_id.inst_id = ev->inst_id;
523         }
524
525         if (bt_gatt_callbacks->client->get_descriptor_cb)
526                 bt_gatt_callbacks->client->get_descriptor_cb(ev->conn_id,
527                         ev->status, &gatt_srvc_id, &gatt_char_id, &gatt_desc_id);
528 }
529
530 static void __bt_handle_gatt_client_read_charac(void *buf, uint16_t len)
531 {
532         struct hal_ev_gatt_client_read_data *ev = buf;
533         btgatt_read_params_t char_read_parm;
534
535         char_read_parm.srvc_id.is_primary = ev->is_primary;
536         char_read_parm.srvc_id.id.inst_id = ev->inst_id;
537         memcpy(char_read_parm.srvc_id.id.uuid.uu, ev->svc_uuid, 16);
538
539         char_read_parm.char_id.inst_id = ev->inst_id;
540         memcpy(char_read_parm.char_id.uuid.uu, ev->char_uuid, 16);
541
542         char_read_parm.value.len = ev->len;
543         if (ev->len > 0) {
544                 memcpy(char_read_parm.value.value, ev->value, ev->len);
545                 char_read_parm.value_type = BT_HAL_GATTC_READ_VALUE_TYPE_VALUE;
546         }
547
548         if (bt_gatt_callbacks->client->read_characteristic_cb)
549                 bt_gatt_callbacks->client->read_characteristic_cb(ev->conn_id,
550                                         ev->status, &char_read_parm);
551 }
552
553 static void __bt_handle_gatt_client_read_desc(void *buf, uint16_t len)
554 {
555         struct hal_ev_gatt_client_read_data *ev = buf;
556         btgatt_read_params_t desc_read_parm;
557
558         desc_read_parm.srvc_id.is_primary = ev->is_primary;
559         desc_read_parm.srvc_id.id.inst_id = ev->inst_id;
560         memcpy(desc_read_parm.srvc_id.id.uuid.uu, ev->svc_uuid, 16);
561
562         desc_read_parm.char_id.inst_id = ev->inst_id;
563         memcpy(desc_read_parm.char_id.uuid.uu, ev->char_uuid, 16);
564
565         desc_read_parm.descr_id.inst_id = ev->inst_id;
566         memcpy(desc_read_parm.descr_id.uuid.uu, ev->desc_uuid, 16);
567
568         desc_read_parm.value.len = ev->len;
569         if (ev->len > 0) {
570                 memcpy(desc_read_parm.value.value, ev->value, ev->len);
571                 desc_read_parm.value_type = BT_HAL_GATTC_READ_VALUE_TYPE_VALUE;
572         }
573
574         if (bt_gatt_callbacks->client->read_descriptor_cb)
575                 bt_gatt_callbacks->client->read_descriptor_cb(ev->conn_id,
576                                         ev->status, &desc_read_parm);
577 }
578
579 static void __bt_handle_gatt_client_write_char(void *buf, uint16_t len)
580 {
581         struct hal_ev_gatt_client_write_result *ev = buf;
582         btgatt_write_params_t char_write_parm;
583
584         char_write_parm.srvc_id.is_primary = ev->is_primary;
585         char_write_parm.srvc_id.id.inst_id = ev->inst_id;
586         memcpy(char_write_parm.srvc_id.id.uuid.uu, ev->svc_uuid, 16);
587
588         char_write_parm.char_id.inst_id = ev->inst_id;
589         memcpy(char_write_parm.char_id.uuid.uu, ev->char_uuid, 16);
590
591         if (bt_gatt_callbacks->client->write_characteristic_cb)
592                 bt_gatt_callbacks->client->write_characteristic_cb(ev->conn_id,
593                                         ev->status, &char_write_parm);
594 }
595
596 static void __bt_handle_gatt_client_write_desc(void *buf, uint16_t len)
597 {
598         struct hal_ev_gatt_client_write_result *ev = buf;
599         btgatt_write_params_t desc_write_parm;
600
601         desc_write_parm.srvc_id.is_primary = ev->is_primary;
602         desc_write_parm.srvc_id.id.inst_id = ev->inst_id;
603         memcpy(desc_write_parm.srvc_id.id.uuid.uu, ev->svc_uuid, 16);
604
605         desc_write_parm.char_id.inst_id = ev->inst_id;
606         memcpy(desc_write_parm.char_id.uuid.uu, ev->char_uuid, 16);
607
608         desc_write_parm.descr_id.inst_id = ev->inst_id;
609         memcpy(desc_write_parm.descr_id.uuid.uu, ev->desc_uuid, 16);
610
611         if (bt_gatt_callbacks->client->write_descriptor_cb)
612                 bt_gatt_callbacks->client->write_descriptor_cb(ev->conn_id,
613                                         ev->status, &desc_write_parm);
614 }
615
616 static void __bt_handle_gatt_client_watch_notification(void *buf, uint16_t len)
617 {
618         struct hal_ev_gatt_client_watch_notification *ev = buf;
619         btgatt_srvc_id_t gatt_srvc_id;
620         btgatt_gatt_id_t gatt_char_id;
621
622         gatt_srvc_id.is_primary = ev->is_primary;
623         gatt_srvc_id.id.inst_id = ev->inst_id;
624         memcpy(gatt_srvc_id.id.uuid.uu, ev->svc_uuid, 16);
625
626         memcpy(gatt_char_id.uuid.uu, ev->char_uuid, 16);
627         gatt_char_id.inst_id = ev->inst_id;
628
629         if (bt_gatt_callbacks->client->register_for_notification_cb)
630                 bt_gatt_callbacks->client->register_for_notification_cb(ev->client_if,
631                                 ev->registered, ev->status, &gatt_srvc_id, &gatt_char_id);
632 }
633
634 static bt_hal_le_adv_info_t *__bt_hal_get_adv_ind_info(char *addr)
635 {
636         GSList *l;
637
638         if (!addr)
639                 return NULL;
640
641         for (l = adv_ind_list; NULL != l; l = g_slist_next(l)) {
642                 bt_hal_le_adv_info_t *adv_info = l->data;
643
644                 if (!adv_info)
645                         continue;
646
647                 if (!g_strcmp0(adv_info->addr, addr))
648                         return adv_info;
649         }
650
651         return NULL;
652 }
653
654 static int __bt_hal_add_adv_ind_info(bt_hal_le_adv_info_t *adv_info)
655 {
656         if (!adv_info)
657                 return -1;
658
659         if (__bt_hal_get_adv_ind_info(adv_info->addr) != NULL) {
660                 DBG("Adv info already present");
661                 return -1;
662         }
663
664         adv_ind_list = g_slist_append(adv_ind_list, adv_info);
665         return 0;
666 }
667
668 static gboolean __bt_hal_adv_scan_req_timeout_cb(gpointer user_data)
669 {
670         bt_hal_le_adv_info_t *adv_info = user_data;
671
672         __bt_hal_send_le_scan_result_event(adv_info);
673         adv_ind_list = g_slist_remove(adv_ind_list, adv_info);
674         g_free(adv_info);
675         return FALSE;
676 }
677
678 static void __bt_hal_handle_gatt_client_scan_result(void *buf, uint16_t len)
679 {
680         bt_bdaddr_t bd_addr;
681         char address[18];
682         struct hal_ev_gatt_client_scan_result *ev = buf;
683         bt_hal_le_adv_info_t *adv_info;
684         int data_len = 0;
685
686         memcpy(bd_addr.address, ev->bd_addr, 6);
687         bt_bdaddr_t2str(&bd_addr, address);
688         data_len = (ev->len < BT_HAL_ADV_DATA_MAX_SIZE) ? ev->len : BT_HAL_ADV_DATA_MAX_SIZE;
689         if (data_len == 0 && ev->adv_type != BT_LE_ADV_SCAN_RSP) {
690                 ERR("Unexpected: Data len is 0");
691                 return;
692         }
693
694         if (ev->adv_type == BT_LE_ADV_SCAN_RSP) { /* SCAN_RSP */
695                 adv_info = __bt_hal_get_adv_ind_info(address);
696                 if (adv_info) {
697                         /* Copy scan response data in data field and send event */
698                         memcpy(&(adv_info->data[adv_info->data_len]), ev->adv_data, data_len);
699                         __bt_hal_send_le_scan_result_event(adv_info);
700                         adv_ind_list = g_slist_remove(adv_ind_list, adv_info);
701                         if (adv_info->timer_id)
702                                 g_source_remove(adv_info->timer_id);
703                         g_free(adv_info);
704                 }
705                 return;
706         }
707
708         /* ADV_IND */
709         adv_info = g_malloc0(sizeof(bt_hal_le_adv_info_t));
710         if (!adv_info) {
711                 ERR("Not enough memory");
712                 return;
713         }
714
715         g_strlcpy(adv_info->addr, address, 18);
716         adv_info->addr_type = ev->addr_type;
717         adv_info->adv_type = ev->adv_type;
718         adv_info->rssi = ev->rssi;
719         adv_info->data_len = data_len;
720         memcpy(adv_info->data, ev->adv_data, data_len);
721
722 #ifdef TIZEN_BT_HAL
723         if (_bt_hal_gatt_client_get_le_scan_type() == BT_GATTC_LE_SCAN_TYPE_PASSIVE) {
724                 __bt_hal_send_le_scan_result_event(adv_info);
725                 g_free(adv_info);
726                 return;
727         }
728 #endif
729         if (__bt_hal_add_adv_ind_info(adv_info))
730                 adv_info->timer_id = g_timeout_add(1000,
731                                 __bt_hal_adv_scan_req_timeout_cb,
732                                 (void *)adv_info);
733 }
734
735 /*******************************************************************************
736  **
737  ** Function            gatt_init
738  **
739  ** Description         Initializes the GATT interface
740  **
741  ** Returns             bt_status_t
742  **
743  *******************************************************************************/
744 static bt_status_t gatt_init(const btgatt_callbacks_t* callbacks)
745 {
746         bt_gatt_callbacks = callbacks;
747         DBG("Register A2DP Src events callback function");
748         _bt_hal_register_gatt_le_dbus_handler_cb(__bt_hal_gatt_events);
749         _bt_hal_register_gatt_server_handler_cb(__bt_hal_gatt_events);
750         _bt_hal_register_gatt_client_handler_cb(__bt_hal_gatt_events);
751         _bt_hal_register_event_handler_cb(HAL_GATT, __bt_hal_gatt_events);
752
753         return BT_STATUS_SUCCESS;
754 }
755
756 /*******************************************************************************
757  **
758  ** Function    gatt_cleanup
759  **
760  ** Description Closes the GATT interface
761  **
762  ** Returns     void
763  **
764  *******************************************************************************/
765 static void gatt_cleanup(void)
766 {
767         _bt_hal_unregister_gatt_le_dbus_handler_cb();
768         _bt_hal_unregister_gatt_client_handler_cb();
769         _bt_hal_unregister_gatt_server_handler_cb();
770         _bt_hal_unregister_event_handler_cb(HAL_GATT);
771
772         if (bt_gatt_callbacks)
773                 bt_gatt_callbacks = NULL;
774 }
775
776 static const btgatt_interface_t btgatt_interface = {
777         sizeof(btgatt_interface),
778
779         gatt_init,
780         gatt_cleanup,
781
782         &btgatt_client_interface,
783         &btgatt_server_interface,
784 };
785
786 /*******************************************************************************
787  **
788  ** Function            bt_get_gatt_interface
789  **
790  ** Description         Get the gatt callback interface
791  **
792  ** Returns             btgatt_interface_t
793  **
794  *******************************************************************************/
795 const btgatt_interface_t *bt_get_gatt_interface()
796 {
797         return &btgatt_interface;
798 }