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