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