4 * Copyright (c) 2015 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Anupam Roy <anupam.r@samsung.com>
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:
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 /*******************************************************************************
25 * Description: GATT Profile Bluetooth Interface
27 *******************************************************************************/
29 #include <hardware/bluetooth.h>
30 #include <hardware/bt_gatt.h>
38 #include "bt-hal-log.h"
39 #include "bt-hal-msg.h"
40 #include "bt-hal-utils.h"
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"
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
54 #define BT_HAL_ADV_DATA_MAX_SIZE 31
55 #define BT_HAL_GATTC_READ_VALUE_TYPE_VALUE 0x0000 /* Attribute value itself */
62 uint8_t data[BT_HAL_ADV_DATA_MAX_SIZE * 2];
64 } bt_hal_le_adv_info_t;
66 /****************************************** Global Variables ******************************************/
67 const btgatt_callbacks_t *bt_gatt_callbacks = NULL;
69 extern btgatt_client_interface_t btgatt_client_interface;
70 extern btgatt_server_interface_t btgatt_server_interface;
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 /*****************************************************************************************************/
89 static bool interface_ready(void)
91 return bt_gatt_callbacks != NULL;
94 static void __bt_hal_handle_server_instance_initialized(void *buf, uint16_t len)
96 struct hal_ev_server_instance_registered *ev = buf;
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);
102 static void __bt_hal_handle_multi_adv_data_set(void *buf, uint16_t len)
104 struct hal_ev_multi_adv_data_set *ev = buf;
106 if (bt_gatt_callbacks->server->multi_adv_data_cb)
107 bt_gatt_callbacks->server->multi_adv_data_cb(ev->server_instance, ev->status);
110 static void __bt_hal_handle_multi_adv_data_update(void *buf, uint16_t len)
112 struct hal_ev_multi_adv_update *ev = buf;
114 if (bt_gatt_callbacks->server->multi_adv_update_cb)
115 bt_gatt_callbacks->server->multi_adv_update_cb(ev->server_instance, ev->status);
118 static void __bt_hal_handle_multi_adv_enable(void *buf, uint16_t len)
120 struct hal_ev_multi_adv_enable *ev = buf;
122 if (bt_gatt_callbacks->server->multi_adv_enable_cb)
123 bt_gatt_callbacks->server->multi_adv_enable_cb(ev->server_instance, ev->status);
126 static void __bt_hal_handle_multi_adv_disable(void *buf, uint16_t len)
128 struct hal_ev_multi_adv_disable *ev = buf;
130 if (bt_gatt_callbacks->server->multi_adv_disable_cb)
131 bt_gatt_callbacks->server->multi_adv_disable_cb(ev->server_instance, ev->status);
134 /* Same callback for both Enable and DIsable */
135 static void __bt_hal_handle_legacy_adv_status(void *buf, uint16_t len)
137 struct hal_ev_legacy_adv_status *ev = buf;
139 if (bt_gatt_callbacks->server->listen_cb)
140 bt_gatt_callbacks->server->listen_cb(ev->status, ev->server_instance);
143 static void __bt_hal_handle_service_added(void *buf, uint16_t len)
145 struct hal_ev_gatt_service_added *ev = buf;
146 btgatt_srvc_id_t srvc_id;
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));
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);
158 static void __bt_hal_handle_char_added(void *buf, uint16_t len)
160 struct hal_ev_gatt_char_added *ev = buf;
163 memcpy(uuid.uu, ev->char_uuid, sizeof(ev->char_uuid));
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);
170 static void __bt_hal_handle_desc_added(void *buf, uint16_t len)
172 struct hal_ev_gatt_desc_added *ev = buf;
175 memcpy(uuid.uu, ev->desc_uuid, sizeof(ev->desc_uuid));
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);
182 static void __bt_hal_handle_service_started(void *buf, uint16_t len)
184 struct hal_ev_gatt_service_started *ev = buf;
186 if (bt_gatt_callbacks->server->service_started_cb)
187 bt_gatt_callbacks->server->service_started_cb(ev->status, ev->server_instance,
191 static void __bt_hal_handle_service_deleted(void *buf, uint16_t len)
193 struct hal_ev_gatt_service_deleted *ev = buf;
195 if (bt_gatt_callbacks->server->service_deleted_cb)
196 bt_gatt_callbacks->server->service_deleted_cb(ev->status, ev->server_instance,
200 static void __bt_hal_handle_gatt_server_connected(void *buf, uint16_t len)
202 struct hal_ev_gatt_server_connected *ev = buf;
205 memcpy(bd_addr.address, ev->bdaddr, 6);
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);
212 static void __bt_hal_handle_gatt_server_notification_changed(void *buf, uint16_t len)
214 struct hal_ev_gatt_server_notifcation_change *ev = buf;
217 memcpy(bd_addr.address, ev->bdaddr, 6);
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);
224 static void __bt_hal_handle_gatt_server_read_requested(void *buf, uint16_t len)
226 struct hal_ev_gatt_server_read_req *ev = buf;
229 memcpy(bd_addr.address, ev->bdaddr, 6);
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);
236 static void __bt_hal_handle_gatt_server_write_requested(void *buf, uint16_t len)
238 struct hal_ev_gatt_server_write_req *ev = buf;
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);
248 static void __bt_hal_handle_gatt_server_acquire_write_requested(void *buf, uint16_t len)
250 struct hal_ev_gatt_server_acquire_write_res *ev = buf;
252 DBG("acquire write requested event recived");
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);
258 static void __bt_hal_handle_gatt_server_indicate_confirmed(void *buf, uint16_t len)
260 struct hal_ev_gatt_server_indicate_cfm *ev = buf;
262 if (bt_gatt_callbacks->server->indication_confirmation_cb)
263 bt_gatt_callbacks->server->indication_confirmation_cb(ev->conn_id, ev->trans_id,
264 ev->att_handle, (bt_bdaddr_t *) ev->bdaddr);
268 static void __bt_hal_gatt_events(int message, void *buf, uint16_t len)
271 /* Check if GATT interface is Ready */
272 if (!interface_ready())
276 case HAL_EV_SERVER_INSTANCE_INITIALIZED: {
277 __bt_hal_handle_server_instance_initialized(buf, len);
280 case HAL_EV_MULTI_ADV_ENABLE: {
281 __bt_hal_handle_multi_adv_enable(buf, len);
284 case HAL_EV_MULTI_ADV_DISABLE: {
285 __bt_hal_handle_multi_adv_disable(buf, len);
288 case HAL_EV_MULTI_ADV_DATA_SET: {
289 __bt_hal_handle_multi_adv_data_set(buf, len);
292 case HAL_EV_MULTI_ADV_UPDATE: {
293 __bt_hal_handle_multi_adv_data_update(buf, len);
296 case HAL_EV_LEGACY_ADV_ENABLE: {
297 __bt_hal_handle_legacy_adv_status(buf, len);
300 case HAL_EV_GATT_CLIENT_REGISTERED: {
301 __bt_handle_gatt_client_registered(buf, len);
304 case HAL_EV_GATT_CLIENT_SCAN_RESULT: {
305 __bt_hal_handle_gatt_client_scan_result(buf, len);
308 case HAL_EV_GATT_SERVICE_ADDED: {
309 __bt_hal_handle_service_added(buf, len);
312 case HAL_EV_GATT_CHAR_ADDED: {
313 __bt_hal_handle_char_added(buf, len);
316 case HAL_EV_GATT_DESC_ADDED: {
317 __bt_hal_handle_desc_added(buf, len);
320 case HAL_EV_GATT_SERVICE_STARTED: {
321 __bt_hal_handle_service_started(buf, len);
324 case HAL_EV_GATT_SERVICE_DELETED: {
325 __bt_hal_handle_service_deleted(buf, len);
328 case HAL_EV_GATT_SERVER_CONNECTED: {
329 __bt_hal_handle_gatt_server_connected(buf, len);
332 case HAL_EV_GATT_NOTIFICATION_CHANGE: {
333 __bt_hal_handle_gatt_server_notification_changed(buf, len);
336 case HAL_EV_GATT_READ_REQUESTED: {
337 __bt_hal_handle_gatt_server_read_requested(buf, len);
340 case HAL_EV_GATT_WRITE_REQUESTED: {
341 __bt_hal_handle_gatt_server_write_requested(buf, len);
344 case HAL_EV_GATT_INDICATE_CFM: {
345 __bt_hal_handle_gatt_server_indicate_confirmed(buf, len);
348 case HAL_EV_GATT_CLIENT_CONNECTED: {
349 __bt_handle_gatt_client_connected(buf, len);
352 case HAL_EV_GATT_CLIENT_DISCONNECTED: {
353 __bt_handle_gatt_client_disconnected(buf, len);
356 case HAL_EV_GATT_CLIENT_SEARCH_RESULT: {
357 __bt_handle_gatt_client_search_result(buf, len);
360 case HAL_EV_GATT_CLIENT_SEARCH_COMPLETE: {
361 __bt_handle_gatt_client_search_complete(buf, len);
364 case HAL_EV_GATT_CLIENT_CHARAC_SEARCH_RESULT: {
365 __bt_handle_gatt_client_search_char_result(buf, len);
368 case HAL_EV_GATT_CLIENT_DESC_SEARCH_RESULT: {
369 __bt_handle_gatt_client_search_desc_result(buf, len);
372 case HAL_EV_GATT_CLIENT_READ_CHARAC: {
373 __bt_handle_gatt_client_read_charac(buf, len);
376 case HAL_EV_GATT_CLIENT_READ_DESC: {
377 __bt_handle_gatt_client_read_desc(buf, len);
380 case HAL_EV_GATT_CLIENT_WRITE_CHARAC: {
381 __bt_handle_gatt_client_write_char(buf, len);
384 case HAL_EV_GATT_CLIENT_WRITE_DESC: {
385 __bt_handle_gatt_client_write_desc(buf, len);
388 case HAL_EV_GATT_CLIENT_WATCH_NOTIFICATION: {
389 __bt_handle_gatt_client_watch_notification(buf, len);
392 case HAL_EV_GATT_SERVER_ACQUIRE_WRITE_RES:{
393 __bt_hal_handle_gatt_server_acquire_write_requested(buf, len);
398 DBG("Event Currently not handled!!");
404 /************************************* GATT CLIENT EVENTS ****************************************/
405 static void __bt_handle_gatt_client_registered(void *buf, uint16_t len)
407 struct hal_ev_gatt_client_registered *ev = buf;
410 memcpy(uuid.uu, ev->app_uuid, 16);
411 if (bt_gatt_callbacks->client->register_client_cb)
412 bt_gatt_callbacks->client->register_client_cb(
413 ev->status, ev->client_if, &uuid);
416 static void __bt_hal_send_le_scan_result_event(bt_hal_le_adv_info_t *adv_info)
420 str2bt_bdaddr_t(adv_info->addr, &bd_addr);
421 if (bt_gatt_callbacks->client->scan_result_cb)
422 bt_gatt_callbacks->client->scan_result_cb(
423 &bd_addr, adv_info->rssi, adv_info->data);
426 static void __bt_handle_gatt_client_connected(void *buf, uint16_t len)
428 struct hal_ev_gatt_client_connected *ev = buf;
431 memcpy(bd_addr.address, ev->bdaddr, 6);
432 if (bt_gatt_callbacks->client->open_cb)
433 bt_gatt_callbacks->client->open_cb(ev->conn_id,
434 ev->status, ev->client_if, &bd_addr);
437 static void __bt_handle_gatt_client_disconnected(void *buf, uint16_t len)
439 struct hal_ev_gatt_client_connected *ev = buf;
442 memcpy(bd_addr.address, ev->bdaddr, 6);
443 if (bt_gatt_callbacks->client->close_cb)
444 bt_gatt_callbacks->client->close_cb(ev->conn_id,
445 ev->status, ev->client_if, &bd_addr);
448 static void __bt_handle_gatt_client_search_result(void *buf, uint16_t len)
450 struct hal_ev_gatt_client_search_result *ev = buf;
451 btgatt_srvc_id_t gatt_srvc_id;
453 gatt_srvc_id.is_primary = ev->is_primary;
454 gatt_srvc_id.id.inst_id = ev->inst_id;
455 memcpy(gatt_srvc_id.id.uuid.uu, ev->uuid, 16);
457 if (bt_gatt_callbacks->client->search_result_cb)
458 bt_gatt_callbacks->client->search_result_cb(ev->conn_id,
462 static void __bt_handle_gatt_client_search_complete(void *buf, uint16_t len)
464 struct hal_ev_gatt_client_search_complete *ev = buf;
466 if (bt_gatt_callbacks->client->search_complete_cb)
467 bt_gatt_callbacks->client->search_complete_cb(ev->conn_id,
471 static void __bt_handle_gatt_client_search_char_result(void *buf, uint16_t len)
473 struct hal_ev_gatt_client_char_search_result *ev = buf;
474 btgatt_srvc_id_t gatt_srvc_id;
475 btgatt_gatt_id_t gatt_char_id;
477 gatt_srvc_id.is_primary = ev->is_primary;
478 gatt_srvc_id.id.inst_id = ev->inst_id;
479 memcpy(gatt_srvc_id.id.uuid.uu, ev->svc_uuid, 16);
481 if (BT_STATUS_SUCCESS == ev->status) {
482 memcpy(gatt_char_id.uuid.uu, ev->char_uuid, 16);
483 gatt_char_id.inst_id = ev->inst_id;
486 if (bt_gatt_callbacks->client->get_characteristic_cb)
487 bt_gatt_callbacks->client->get_characteristic_cb(ev->conn_id,
488 ev->status, &gatt_srvc_id, &gatt_char_id, ev->char_prop);
491 static void __bt_handle_gatt_client_search_desc_result(void *buf, uint16_t len)
493 struct hal_ev_gatt_client_desc_search_result *ev = buf;
494 btgatt_srvc_id_t gatt_srvc_id;
495 btgatt_gatt_id_t gatt_char_id;
496 btgatt_gatt_id_t gatt_desc_id;
499 gatt_srvc_id.is_primary = ev->is_primary;
500 gatt_srvc_id.id.inst_id = ev->inst_id;
501 memcpy(gatt_srvc_id.id.uuid.uu, ev->svc_uuid, 16);
503 memcpy(gatt_char_id.uuid.uu, ev->char_uuid, 16);
504 gatt_char_id.inst_id = ev->inst_id;
506 if (BT_STATUS_SUCCESS == ev->status) {
507 memcpy(gatt_desc_id.uuid.uu, ev->desc_uuid, 16);
508 gatt_desc_id.inst_id = ev->inst_id;
511 if (bt_gatt_callbacks->client->get_descriptor_cb)
512 bt_gatt_callbacks->client->get_descriptor_cb(ev->conn_id,
513 ev->status, &gatt_srvc_id, &gatt_char_id, &gatt_desc_id);
516 static void __bt_handle_gatt_client_read_charac(void *buf, uint16_t len)
518 struct hal_ev_gatt_client_read_data *ev = buf;
519 btgatt_read_params_t char_read_parm;
521 char_read_parm.srvc_id.is_primary = ev->is_primary;
522 char_read_parm.srvc_id.id.inst_id = ev->inst_id;
523 memcpy(char_read_parm.srvc_id.id.uuid.uu, ev->svc_uuid, 16);
525 char_read_parm.char_id.inst_id = ev->inst_id;
526 memcpy(char_read_parm.char_id.uuid.uu, ev->char_uuid, 16);
528 char_read_parm.value.len = ev->len;
530 memcpy(char_read_parm.value.value, ev->value, ev->len);
531 char_read_parm.value_type = BT_HAL_GATTC_READ_VALUE_TYPE_VALUE;
534 if (bt_gatt_callbacks->client->read_characteristic_cb)
535 bt_gatt_callbacks->client->read_characteristic_cb(ev->conn_id,
536 ev->status, &char_read_parm);
539 static void __bt_handle_gatt_client_read_desc(void *buf, uint16_t len)
541 struct hal_ev_gatt_client_read_data *ev = buf;
542 btgatt_read_params_t desc_read_parm;
544 desc_read_parm.srvc_id.is_primary = ev->is_primary;
545 desc_read_parm.srvc_id.id.inst_id = ev->inst_id;
546 memcpy(desc_read_parm.srvc_id.id.uuid.uu, ev->svc_uuid, 16);
548 desc_read_parm.char_id.inst_id = ev->inst_id;
549 memcpy(desc_read_parm.char_id.uuid.uu, ev->char_uuid, 16);
551 desc_read_parm.descr_id.inst_id = ev->inst_id;
552 memcpy(desc_read_parm.descr_id.uuid.uu, ev->desc_uuid, 16);
554 desc_read_parm.value.len = ev->len;
556 memcpy(desc_read_parm.value.value, ev->value, ev->len);
557 desc_read_parm.value_type = BT_HAL_GATTC_READ_VALUE_TYPE_VALUE;
560 if (bt_gatt_callbacks->client->read_descriptor_cb)
561 bt_gatt_callbacks->client->read_descriptor_cb(ev->conn_id,
562 ev->status, &desc_read_parm);
565 static void __bt_handle_gatt_client_write_char(void *buf, uint16_t len)
567 struct hal_ev_gatt_client_write_result *ev = buf;
568 btgatt_write_params_t char_write_parm;
570 char_write_parm.srvc_id.is_primary = ev->is_primary;
571 char_write_parm.srvc_id.id.inst_id = ev->inst_id;
572 memcpy(char_write_parm.srvc_id.id.uuid.uu, ev->svc_uuid, 16);
574 char_write_parm.char_id.inst_id = ev->inst_id;
575 memcpy(char_write_parm.char_id.uuid.uu, ev->char_uuid, 16);
577 if (bt_gatt_callbacks->client->write_characteristic_cb)
578 bt_gatt_callbacks->client->write_characteristic_cb(ev->conn_id,
579 ev->status, &char_write_parm);
582 static void __bt_handle_gatt_client_write_desc(void *buf, uint16_t len)
584 struct hal_ev_gatt_client_write_result *ev = buf;
585 btgatt_write_params_t desc_write_parm;
587 desc_write_parm.srvc_id.is_primary = ev->is_primary;
588 desc_write_parm.srvc_id.id.inst_id = ev->inst_id;
589 memcpy(desc_write_parm.srvc_id.id.uuid.uu, ev->svc_uuid, 16);
591 desc_write_parm.char_id.inst_id = ev->inst_id;
592 memcpy(desc_write_parm.char_id.uuid.uu, ev->char_uuid, 16);
594 desc_write_parm.descr_id.inst_id = ev->inst_id;
595 memcpy(desc_write_parm.descr_id.uuid.uu, ev->desc_uuid, 16);
597 if (bt_gatt_callbacks->client->write_descriptor_cb)
598 bt_gatt_callbacks->client->write_descriptor_cb(ev->conn_id,
599 ev->status, &desc_write_parm);
602 static void __bt_handle_gatt_client_watch_notification(void *buf, uint16_t len)
604 struct hal_ev_gatt_client_watch_notification *ev = buf;
605 btgatt_srvc_id_t gatt_srvc_id;
606 btgatt_gatt_id_t gatt_char_id;
608 gatt_srvc_id.is_primary = ev->is_primary;
609 gatt_srvc_id.id.inst_id = ev->inst_id;
610 memcpy(gatt_srvc_id.id.uuid.uu, ev->svc_uuid, 16);
612 memcpy(gatt_char_id.uuid.uu, ev->char_uuid, 16);
613 gatt_char_id.inst_id = ev->inst_id;
615 if (bt_gatt_callbacks->client->register_for_notification_cb)
616 bt_gatt_callbacks->client->register_for_notification_cb(ev->client_if,
617 ev->registered, ev->status, &gatt_srvc_id, &gatt_char_id);
620 static bt_hal_le_adv_info_t *__bt_hal_get_adv_ind_info(char *addr)
627 for (l = adv_ind_list; NULL != l; l = g_slist_next(l)) {
628 bt_hal_le_adv_info_t *adv_info = l->data;
633 if (!g_strcmp0(adv_info->addr, addr))
640 static int __bt_hal_add_adv_ind_info(bt_hal_le_adv_info_t *adv_info)
645 if (__bt_hal_get_adv_ind_info(adv_info->addr) != NULL) {
646 DBG("Adv info already present");
650 adv_ind_list = g_slist_append(adv_ind_list, adv_info);
654 static gboolean __bt_hal_adv_scan_req_timeout_cb(gpointer user_data)
656 bt_hal_le_adv_info_t *adv_info = user_data;
658 __bt_hal_send_le_scan_result_event(adv_info);
659 adv_ind_list = g_slist_remove(adv_ind_list, adv_info);
664 static void __bt_hal_handle_gatt_client_scan_result(void *buf, uint16_t len)
668 struct hal_ev_gatt_client_scan_result *ev = buf;
669 bt_hal_le_adv_info_t *adv_info;
672 memcpy(bd_addr.address, ev->bd_addr, 6);
673 bt_bdaddr_t2str(&bd_addr, address);
674 data_len = (ev->len < BT_HAL_ADV_DATA_MAX_SIZE) ? ev->len : BT_HAL_ADV_DATA_MAX_SIZE;
675 if (data_len == 0 && ev->adv_type != BT_LE_ADV_SCAN_RSP) {
676 ERR("Unexpected: Data len is 0");
680 if (ev->adv_type == BT_LE_ADV_SCAN_RSP) { /* SCAN_RSP */
681 adv_info = __bt_hal_get_adv_ind_info(address);
683 /* Copy scan response data in data field and send event */
684 memcpy(&(adv_info->data[adv_info->data_len]), ev->adv_data, data_len);
685 __bt_hal_send_le_scan_result_event(adv_info);
686 adv_ind_list = g_slist_remove(adv_ind_list, adv_info);
687 if (adv_info->timer_id)
688 g_source_remove(adv_info->timer_id);
695 adv_info = g_malloc0(sizeof(bt_hal_le_adv_info_t));
697 ERR("Not enough memory");
701 g_strlcpy(adv_info->addr, address, 18);
702 adv_info->addr_type = ev->addr_type;
703 adv_info->adv_type = ev->adv_type;
704 adv_info->rssi = ev->rssi;
705 adv_info->data_len = data_len;
706 memcpy(adv_info->data, ev->adv_data, data_len);
709 if (_bt_hal_gatt_client_get_le_scan_type() == BT_GATTC_LE_SCAN_TYPE_PASSIVE) {
710 __bt_hal_send_le_scan_result_event(adv_info);
715 if (__bt_hal_add_adv_ind_info(adv_info))
716 adv_info->timer_id = g_timeout_add(1000,
717 __bt_hal_adv_scan_req_timeout_cb,
721 /*******************************************************************************
723 ** Function gatt_init
725 ** Description Initializes the GATT interface
727 ** Returns bt_status_t
729 *******************************************************************************/
730 static bt_status_t gatt_init(const btgatt_callbacks_t* callbacks)
732 bt_gatt_callbacks = callbacks;
733 DBG("Register A2DP Src events callback function");
734 _bt_hal_register_gatt_le_dbus_handler_cb(__bt_hal_gatt_events);
735 _bt_hal_register_gatt_server_handler_cb(__bt_hal_gatt_events);
736 _bt_hal_register_gatt_client_handler_cb(__bt_hal_gatt_events);
737 _bt_hal_register_event_handler_cb(HAL_GATT, __bt_hal_gatt_events);
739 return BT_STATUS_SUCCESS;
742 /*******************************************************************************
744 ** Function gatt_cleanup
746 ** Description Closes the GATT interface
750 *******************************************************************************/
751 static void gatt_cleanup(void)
753 _bt_hal_unregister_gatt_le_dbus_handler_cb();
754 _bt_hal_unregister_gatt_client_handler_cb();
755 _bt_hal_unregister_gatt_server_handler_cb();
756 _bt_hal_unregister_event_handler_cb(HAL_GATT);
758 if (bt_gatt_callbacks)
759 bt_gatt_callbacks = NULL;
762 static const btgatt_interface_t btgatt_interface = {
763 sizeof(btgatt_interface),
768 &btgatt_client_interface,
769 &btgatt_server_interface,
772 /*******************************************************************************
774 ** Function bt_get_gatt_interface
776 ** Description Get the gatt callback interface
778 ** Returns btgatt_interface_t
780 *******************************************************************************/
781 const btgatt_interface_t *bt_get_gatt_interface()
783 return &btgatt_interface;