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_read_requested(void *buf, uint16_t len)
214 struct hal_ev_gatt_server_read_req *ev = buf;
217 memcpy(bd_addr.address, ev->bdaddr, 6);
219 if (bt_gatt_callbacks->server->request_read_cb)
220 bt_gatt_callbacks->server->request_read_cb(ev->conn_id, ev->trans_id, &bd_addr,
221 ev->att_handle, ev->offset, ev->is_long);
224 static void __bt_hal_handle_gatt_server_write_requested(void *buf, uint16_t len)
226 struct hal_ev_gatt_server_write_req *ev = buf;
228 if (bt_gatt_callbacks->server->request_write_cb)
229 bt_gatt_callbacks->server->request_write_cb(ev->conn_id, ev->trans_id,
230 (bt_bdaddr_t *) ev->bdaddr,
231 ev->att_handle, ev->offset,
232 ev->length, ev->need_rsp,
233 ev->is_prep, ev->value);
236 static void __bt_hal_handle_gatt_server_indicate_confirmed(void *buf, uint16_t len)
238 struct hal_ev_gatt_server_indicate_cfm *ev = buf;
241 if (bt_gatt_callbacks->server->indication_confirmation_cb)
242 bt_gatt_callbacks->server->indication_confirmation_cb(ev->conn_id, ev->trans_id,
243 ev->att_handle, (bt_bdaddr_t *) ev->bdaddr);
247 static void __bt_hal_gatt_events(int message, void *buf, uint16_t len)
250 /* Check if GATT interface is Ready */
251 if (!interface_ready())
255 case HAL_EV_SERVER_INSTANCE_INITIALIZED: {
256 __bt_hal_handle_server_instance_initialized(buf, len);
259 case HAL_EV_MULTI_ADV_ENABLE: {
260 __bt_hal_handle_multi_adv_enable(buf, len);
263 case HAL_EV_MULTI_ADV_DISABLE: {
264 __bt_hal_handle_multi_adv_disable(buf, len);
267 case HAL_EV_MULTI_ADV_DATA_SET: {
268 __bt_hal_handle_multi_adv_data_set(buf, len);
271 case HAL_EV_MULTI_ADV_UPDATE: {
272 __bt_hal_handle_multi_adv_data_update(buf, len);
275 case HAL_EV_LEGACY_ADV_ENABLE: {
276 __bt_hal_handle_legacy_adv_status(buf, len);
279 case HAL_EV_GATT_CLIENT_REGISTERED: {
280 __bt_handle_gatt_client_registered(buf, len);
283 case HAL_EV_GATT_CLIENT_SCAN_RESULT: {
284 __bt_hal_handle_gatt_client_scan_result(buf, len);
287 case HAL_EV_GATT_SERVICE_ADDED: {
288 __bt_hal_handle_service_added(buf, len);
291 case HAL_EV_GATT_CHAR_ADDED: {
292 __bt_hal_handle_char_added(buf, len);
295 case HAL_EV_GATT_DESC_ADDED: {
296 __bt_hal_handle_desc_added(buf, len);
299 case HAL_EV_GATT_SERVICE_STARTED: {
300 __bt_hal_handle_service_started(buf, len);
303 case HAL_EV_GATT_SERVICE_DELETED: {
304 __bt_hal_handle_service_deleted(buf, len);
307 case HAL_EV_GATT_SERVER_CONNECTED: {
308 __bt_hal_handle_gatt_server_connected(buf, len);
311 case HAL_EV_GATT_READ_REQUESTED: {
312 __bt_hal_handle_gatt_server_read_requested(buf, len);
315 case HAL_EV_GATT_WRITE_REQUESTED: {
316 __bt_hal_handle_gatt_server_write_requested(buf, len);
319 case HAL_EV_GATT_INDICATE_CFM: {
320 __bt_hal_handle_gatt_server_indicate_confirmed(buf, len);
323 case HAL_EV_GATT_CLIENT_CONNECTED: {
324 __bt_handle_gatt_client_connected(buf, len);
327 case HAL_EV_GATT_CLIENT_DISCONNECTED: {
328 __bt_handle_gatt_client_disconnected(buf, len);
331 case HAL_EV_GATT_CLIENT_SEARCH_RESULT: {
332 __bt_handle_gatt_client_search_result(buf, len);
335 case HAL_EV_GATT_CLIENT_SEARCH_COMPLETE: {
336 __bt_handle_gatt_client_search_complete(buf, len);
339 case HAL_EV_GATT_CLIENT_CHARAC_SEARCH_RESULT: {
340 __bt_handle_gatt_client_search_char_result(buf, len);
343 case HAL_EV_GATT_CLIENT_DESC_SEARCH_RESULT: {
344 __bt_handle_gatt_client_search_desc_result(buf, len);
347 case HAL_EV_GATT_CLIENT_READ_CHARAC: {
348 __bt_handle_gatt_client_read_charac(buf, len);
351 case HAL_EV_GATT_CLIENT_READ_DESC: {
352 __bt_handle_gatt_client_read_desc(buf, len);
355 case HAL_EV_GATT_CLIENT_WRITE_CHARAC: {
356 __bt_handle_gatt_client_write_char(buf, len);
359 case HAL_EV_GATT_CLIENT_WRITE_DESC: {
360 __bt_handle_gatt_client_write_desc(buf, len);
363 case HAL_EV_GATT_CLIENT_WATCH_NOTIFICATION: {
364 __bt_handle_gatt_client_watch_notification(buf, len);
368 DBG("Event Currently not handled!!");
374 /************************************* GATT CLIENT EVENTS ****************************************/
375 static void __bt_handle_gatt_client_registered(void *buf, uint16_t len)
377 struct hal_ev_gatt_client_registered *ev = buf;
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);
386 static void __bt_hal_send_le_scan_result_event(bt_hal_le_adv_info_t *adv_info)
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);
396 static void __bt_handle_gatt_client_connected(void *buf, uint16_t len)
398 struct hal_ev_gatt_client_connected *ev = buf;
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);
407 static void __bt_handle_gatt_client_disconnected(void *buf, uint16_t len)
409 struct hal_ev_gatt_client_connected *ev = buf;
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);
418 static void __bt_handle_gatt_client_search_result(void *buf, uint16_t len)
420 struct hal_ev_gatt_client_search_result *ev = buf;
421 btgatt_srvc_id_t gatt_srvc_id;
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);
427 if (bt_gatt_callbacks->client->search_result_cb)
428 bt_gatt_callbacks->client->search_result_cb(ev->conn_id,
432 static void __bt_handle_gatt_client_search_complete(void *buf, uint16_t len)
434 struct hal_ev_gatt_client_search_complete *ev = buf;
436 if (bt_gatt_callbacks->client->search_complete_cb)
437 bt_gatt_callbacks->client->search_complete_cb(ev->conn_id,
441 static void __bt_handle_gatt_client_search_char_result(void *buf, uint16_t len)
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;
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);
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;
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);
461 static void __bt_handle_gatt_client_search_desc_result(void *buf, uint16_t len)
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;
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);
473 memcpy(gatt_char_id.uuid.uu, ev->char_uuid, 16);
474 gatt_char_id.inst_id = ev->inst_id;
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;
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);
486 static void __bt_handle_gatt_client_read_charac(void *buf, uint16_t len)
488 struct hal_ev_gatt_client_read_data *ev = buf;
489 btgatt_read_params_t char_read_parm;
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);
495 char_read_parm.char_id.inst_id = ev->inst_id;
496 memcpy(char_read_parm.char_id.uuid.uu, ev->char_uuid, 16);
498 char_read_parm.value.len = ev->len;
500 memcpy(char_read_parm.value.value, ev->value, ev->len);
501 char_read_parm.value_type = BT_HAL_GATTC_READ_VALUE_TYPE_VALUE;
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);
509 static void __bt_handle_gatt_client_read_desc(void *buf, uint16_t len)
511 struct hal_ev_gatt_client_read_data *ev = buf;
512 btgatt_read_params_t desc_read_parm;
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);
518 desc_read_parm.char_id.inst_id = ev->inst_id;
519 memcpy(desc_read_parm.char_id.uuid.uu, ev->char_uuid, 16);
521 desc_read_parm.descr_id.inst_id = ev->inst_id;
522 memcpy(desc_read_parm.descr_id.uuid.uu, ev->desc_uuid, 16);
524 desc_read_parm.value.len = ev->len;
526 memcpy(desc_read_parm.value.value, ev->value, ev->len);
527 desc_read_parm.value_type = BT_HAL_GATTC_READ_VALUE_TYPE_VALUE;
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);
535 static void __bt_handle_gatt_client_write_char(void *buf, uint16_t len)
537 struct hal_ev_gatt_client_write_result *ev = buf;
538 btgatt_write_params_t char_write_parm;
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);
544 char_write_parm.char_id.inst_id = ev->inst_id;
545 memcpy(char_write_parm.char_id.uuid.uu, ev->char_uuid, 16);
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);
552 static void __bt_handle_gatt_client_write_desc(void *buf, uint16_t len)
554 struct hal_ev_gatt_client_write_result *ev = buf;
555 btgatt_write_params_t desc_write_parm;
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);
561 desc_write_parm.char_id.inst_id = ev->inst_id;
562 memcpy(desc_write_parm.char_id.uuid.uu, ev->char_uuid, 16);
564 desc_write_parm.descr_id.inst_id = ev->inst_id;
565 memcpy(desc_write_parm.descr_id.uuid.uu, ev->desc_uuid, 16);
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);
572 static void __bt_handle_gatt_client_watch_notification(void *buf, uint16_t len)
574 struct hal_ev_gatt_client_watch_notification *ev = buf;
575 btgatt_srvc_id_t gatt_srvc_id;
576 btgatt_gatt_id_t gatt_char_id;
578 gatt_srvc_id.is_primary = ev->is_primary;
579 gatt_srvc_id.id.inst_id = ev->inst_id;
580 memcpy(gatt_srvc_id.id.uuid.uu, ev->svc_uuid, 16);
582 memcpy(gatt_char_id.uuid.uu, ev->char_uuid, 16);
583 gatt_char_id.inst_id = ev->inst_id;
585 if (bt_gatt_callbacks->client->register_for_notification_cb)
586 bt_gatt_callbacks->client->register_for_notification_cb(ev->client_if,
587 ev->registered, ev->status, &gatt_srvc_id, &gatt_char_id);
590 static bt_hal_le_adv_info_t *__bt_hal_get_adv_ind_info(char *addr)
597 for (l = adv_ind_list; NULL != l; l = g_slist_next(l)) {
598 bt_hal_le_adv_info_t *adv_info = l->data;
603 if (!g_strcmp0(adv_info->addr, addr))
610 static int __bt_hal_add_adv_ind_info(bt_hal_le_adv_info_t *adv_info)
615 if (__bt_hal_get_adv_ind_info(adv_info->addr) != NULL) {
616 DBG("Adv info already present");
620 adv_ind_list = g_slist_append(adv_ind_list, adv_info);
624 static gboolean __bt_hal_adv_scan_req_timeout_cb(gpointer user_data)
626 bt_hal_le_adv_info_t *adv_info = user_data;
628 __bt_hal_send_le_scan_result_event(adv_info);
629 adv_ind_list = g_slist_remove(adv_ind_list, adv_info);
634 static void __bt_hal_handle_gatt_client_scan_result(void *buf, uint16_t len)
638 struct hal_ev_gatt_client_scan_result *ev = buf;
639 bt_hal_le_adv_info_t *adv_info;
642 memcpy(bd_addr.address, ev->bd_addr, 6);
643 bt_bdaddr_t2str(&bd_addr, address);
644 data_len = (ev->len < BT_HAL_ADV_DATA_MAX_SIZE) ? ev->len : BT_HAL_ADV_DATA_MAX_SIZE;
645 if (data_len == 0 && ev->adv_type != BT_LE_ADV_SCAN_RSP) {
646 ERR("Unexpected: Data len is 0");
650 if (ev->adv_type == BT_LE_ADV_SCAN_RSP) { /* SCAN_RSP */
651 adv_info = __bt_hal_get_adv_ind_info(address);
653 /* Copy scan response data in data field and send event */
654 memcpy(&(adv_info->data[adv_info->data_len]), ev->adv_data, data_len);
655 __bt_hal_send_le_scan_result_event(adv_info);
656 adv_ind_list = g_slist_remove(adv_ind_list, adv_info);
657 if (adv_info->timer_id)
658 g_source_remove(adv_info->timer_id);
665 adv_info = g_malloc0(sizeof(bt_hal_le_adv_info_t));
667 ERR("Not enough memory");
671 g_strlcpy(adv_info->addr, address, 18);
672 adv_info->addr_type = ev->addr_type;
673 adv_info->adv_type = ev->adv_type;
674 adv_info->rssi = ev->rssi;
675 adv_info->data_len = data_len;
676 memcpy(adv_info->data, ev->adv_data, data_len);
679 if (_bt_hal_gatt_client_get_le_scan_type() == BT_GATTC_LE_SCAN_TYPE_PASSIVE) {
680 __bt_hal_send_le_scan_result_event(adv_info);
685 if (__bt_hal_add_adv_ind_info(adv_info))
686 adv_info->timer_id = g_timeout_add(1000,
687 __bt_hal_adv_scan_req_timeout_cb,
691 /*******************************************************************************
693 ** Function gatt_init
695 ** Description Initializes the GATT interface
697 ** Returns bt_status_t
699 *******************************************************************************/
700 static bt_status_t gatt_init(const btgatt_callbacks_t* callbacks)
702 bt_gatt_callbacks = callbacks;
703 DBG("Register A2DP Src events callback function");
704 _bt_hal_register_gatt_le_dbus_handler_cb(__bt_hal_gatt_events);
705 _bt_hal_register_gatt_server_handler_cb(__bt_hal_gatt_events);
706 _bt_hal_register_gatt_client_handler_cb(__bt_hal_gatt_events);
707 _bt_hal_register_event_handler_cb(HAL_GATT, __bt_hal_gatt_events);
709 return BT_STATUS_SUCCESS;
712 /*******************************************************************************
714 ** Function gatt_cleanup
716 ** Description Closes the GATT interface
720 *******************************************************************************/
721 static void gatt_cleanup(void)
723 _bt_hal_unregister_gatt_le_dbus_handler_cb();
724 _bt_hal_unregister_gatt_client_handler_cb();
725 _bt_hal_unregister_gatt_server_handler_cb();
726 _bt_hal_unregister_event_handler_cb(HAL_GATT);
728 if (bt_gatt_callbacks)
729 bt_gatt_callbacks = NULL;
732 static const btgatt_interface_t btgatt_interface = {
733 sizeof(btgatt_interface),
738 &btgatt_client_interface,
739 &btgatt_server_interface,
742 /*******************************************************************************
744 ** Function bt_get_gatt_interface
746 ** Description Get the gatt callback interface
748 ** Returns btgatt_interface_t
750 *******************************************************************************/
751 const btgatt_interface_t *bt_get_gatt_interface()
753 return &btgatt_interface;