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_acquire_notify_requested(void *buf, uint16_t len)
260 struct hal_ev_gatt_server_acquire_notify *ev = buf;
262 DBG("acquire notify requested event recived");
264 if (bt_gatt_callbacks->server->request_acquire_notify_cb)
265 bt_gatt_callbacks->server->request_acquire_notify_cb(ev->mtu, ev->conn_id, ev->trans_id, ev->char_handl);
268 static void __bt_hal_handle_gatt_server_indicate_confirmed(void *buf, uint16_t len)
270 struct hal_ev_gatt_server_indicate_cfm *ev = buf;
272 if (bt_gatt_callbacks->server->indication_confirmation_cb)
273 bt_gatt_callbacks->server->indication_confirmation_cb(ev->conn_id, ev->trans_id,
274 ev->att_handle, (bt_bdaddr_t *) ev->bdaddr);
278 static void __bt_hal_gatt_events(int message, void *buf, uint16_t len)
281 /* Check if GATT interface is Ready */
282 if (!interface_ready())
286 case HAL_EV_SERVER_INSTANCE_INITIALIZED: {
287 __bt_hal_handle_server_instance_initialized(buf, len);
290 case HAL_EV_MULTI_ADV_ENABLE: {
291 __bt_hal_handle_multi_adv_enable(buf, len);
294 case HAL_EV_MULTI_ADV_DISABLE: {
295 __bt_hal_handle_multi_adv_disable(buf, len);
298 case HAL_EV_MULTI_ADV_DATA_SET: {
299 __bt_hal_handle_multi_adv_data_set(buf, len);
302 case HAL_EV_MULTI_ADV_UPDATE: {
303 __bt_hal_handle_multi_adv_data_update(buf, len);
306 case HAL_EV_LEGACY_ADV_ENABLE: {
307 __bt_hal_handle_legacy_adv_status(buf, len);
310 case HAL_EV_GATT_CLIENT_REGISTERED: {
311 __bt_handle_gatt_client_registered(buf, len);
314 case HAL_EV_GATT_CLIENT_SCAN_RESULT: {
315 __bt_hal_handle_gatt_client_scan_result(buf, len);
318 case HAL_EV_GATT_SERVICE_ADDED: {
319 __bt_hal_handle_service_added(buf, len);
322 case HAL_EV_GATT_CHAR_ADDED: {
323 __bt_hal_handle_char_added(buf, len);
326 case HAL_EV_GATT_DESC_ADDED: {
327 __bt_hal_handle_desc_added(buf, len);
330 case HAL_EV_GATT_SERVICE_STARTED: {
331 __bt_hal_handle_service_started(buf, len);
334 case HAL_EV_GATT_SERVICE_DELETED: {
335 __bt_hal_handle_service_deleted(buf, len);
338 case HAL_EV_GATT_SERVER_CONNECTED: {
339 __bt_hal_handle_gatt_server_connected(buf, len);
342 case HAL_EV_GATT_NOTIFICATION_CHANGE: {
343 __bt_hal_handle_gatt_server_notification_changed(buf, len);
346 case HAL_EV_GATT_READ_REQUESTED: {
347 __bt_hal_handle_gatt_server_read_requested(buf, len);
350 case HAL_EV_GATT_WRITE_REQUESTED: {
351 __bt_hal_handle_gatt_server_write_requested(buf, len);
354 case HAL_EV_GATT_INDICATE_CFM: {
355 __bt_hal_handle_gatt_server_indicate_confirmed(buf, len);
358 case HAL_EV_GATT_CLIENT_CONNECTED: {
359 __bt_handle_gatt_client_connected(buf, len);
362 case HAL_EV_GATT_CLIENT_DISCONNECTED: {
363 __bt_handle_gatt_client_disconnected(buf, len);
366 case HAL_EV_GATT_CLIENT_SEARCH_RESULT: {
367 __bt_handle_gatt_client_search_result(buf, len);
370 case HAL_EV_GATT_CLIENT_SEARCH_COMPLETE: {
371 __bt_handle_gatt_client_search_complete(buf, len);
374 case HAL_EV_GATT_CLIENT_CHARAC_SEARCH_RESULT: {
375 __bt_handle_gatt_client_search_char_result(buf, len);
378 case HAL_EV_GATT_CLIENT_DESC_SEARCH_RESULT: {
379 __bt_handle_gatt_client_search_desc_result(buf, len);
382 case HAL_EV_GATT_CLIENT_READ_CHARAC: {
383 __bt_handle_gatt_client_read_charac(buf, len);
386 case HAL_EV_GATT_CLIENT_READ_DESC: {
387 __bt_handle_gatt_client_read_desc(buf, len);
390 case HAL_EV_GATT_CLIENT_WRITE_CHARAC: {
391 __bt_handle_gatt_client_write_char(buf, len);
394 case HAL_EV_GATT_CLIENT_WRITE_DESC: {
395 __bt_handle_gatt_client_write_desc(buf, len);
398 case HAL_EV_GATT_CLIENT_WATCH_NOTIFICATION: {
399 __bt_handle_gatt_client_watch_notification(buf, len);
402 case HAL_EV_GATT_SERVER_ACQUIRE_WRITE_RES:{
403 __bt_hal_handle_gatt_server_acquire_write_requested(buf, len);
406 case HAL_EV_GATT_SERVER_ACQUIRE_NOTIFY_RES:{
407 __bt_hal_handle_gatt_server_acquire_notify_requested(buf, len);
412 DBG("Event Currently not handled!!");
418 /************************************* GATT CLIENT EVENTS ****************************************/
419 static void __bt_handle_gatt_client_registered(void *buf, uint16_t len)
421 struct hal_ev_gatt_client_registered *ev = buf;
424 memcpy(uuid.uu, ev->app_uuid, 16);
425 if (bt_gatt_callbacks->client->register_client_cb)
426 bt_gatt_callbacks->client->register_client_cb(
427 ev->status, ev->client_if, &uuid);
430 static void __bt_hal_send_le_scan_result_event(bt_hal_le_adv_info_t *adv_info)
434 str2bt_bdaddr_t(adv_info->addr, &bd_addr);
435 if (bt_gatt_callbacks->client->scan_result_cb)
436 bt_gatt_callbacks->client->scan_result_cb(
437 &bd_addr, adv_info->rssi, adv_info->data);
440 static void __bt_handle_gatt_client_connected(void *buf, uint16_t len)
442 struct hal_ev_gatt_client_connected *ev = buf;
445 memcpy(bd_addr.address, ev->bdaddr, 6);
446 if (bt_gatt_callbacks->client->open_cb)
447 bt_gatt_callbacks->client->open_cb(ev->conn_id,
448 ev->status, ev->client_if, &bd_addr);
451 static void __bt_handle_gatt_client_disconnected(void *buf, uint16_t len)
453 struct hal_ev_gatt_client_connected *ev = buf;
456 memcpy(bd_addr.address, ev->bdaddr, 6);
457 if (bt_gatt_callbacks->client->close_cb)
458 bt_gatt_callbacks->client->close_cb(ev->conn_id,
459 ev->status, ev->client_if, &bd_addr);
462 static void __bt_handle_gatt_client_search_result(void *buf, uint16_t len)
464 struct hal_ev_gatt_client_search_result *ev = buf;
465 btgatt_srvc_id_t gatt_srvc_id;
467 gatt_srvc_id.is_primary = ev->is_primary;
468 gatt_srvc_id.id.inst_id = ev->inst_id;
469 memcpy(gatt_srvc_id.id.uuid.uu, ev->uuid, 16);
471 if (bt_gatt_callbacks->client->search_result_cb)
472 bt_gatt_callbacks->client->search_result_cb(ev->conn_id,
476 static void __bt_handle_gatt_client_search_complete(void *buf, uint16_t len)
478 struct hal_ev_gatt_client_search_complete *ev = buf;
480 if (bt_gatt_callbacks->client->search_complete_cb)
481 bt_gatt_callbacks->client->search_complete_cb(ev->conn_id,
485 static void __bt_handle_gatt_client_search_char_result(void *buf, uint16_t len)
487 struct hal_ev_gatt_client_char_search_result *ev = buf;
488 btgatt_srvc_id_t gatt_srvc_id;
489 btgatt_gatt_id_t gatt_char_id;
491 gatt_srvc_id.is_primary = ev->is_primary;
492 gatt_srvc_id.id.inst_id = ev->inst_id;
493 memcpy(gatt_srvc_id.id.uuid.uu, ev->svc_uuid, 16);
495 if (BT_STATUS_SUCCESS == ev->status) {
496 memcpy(gatt_char_id.uuid.uu, ev->char_uuid, 16);
497 gatt_char_id.inst_id = ev->inst_id;
500 if (bt_gatt_callbacks->client->get_characteristic_cb)
501 bt_gatt_callbacks->client->get_characteristic_cb(ev->conn_id,
502 ev->status, &gatt_srvc_id, &gatt_char_id, ev->char_prop);
505 static void __bt_handle_gatt_client_search_desc_result(void *buf, uint16_t len)
507 struct hal_ev_gatt_client_desc_search_result *ev = buf;
508 btgatt_srvc_id_t gatt_srvc_id;
509 btgatt_gatt_id_t gatt_char_id;
510 btgatt_gatt_id_t gatt_desc_id;
513 gatt_srvc_id.is_primary = ev->is_primary;
514 gatt_srvc_id.id.inst_id = ev->inst_id;
515 memcpy(gatt_srvc_id.id.uuid.uu, ev->svc_uuid, 16);
517 memcpy(gatt_char_id.uuid.uu, ev->char_uuid, 16);
518 gatt_char_id.inst_id = ev->inst_id;
520 if (BT_STATUS_SUCCESS == ev->status) {
521 memcpy(gatt_desc_id.uuid.uu, ev->desc_uuid, 16);
522 gatt_desc_id.inst_id = ev->inst_id;
525 if (bt_gatt_callbacks->client->get_descriptor_cb)
526 bt_gatt_callbacks->client->get_descriptor_cb(ev->conn_id,
527 ev->status, &gatt_srvc_id, &gatt_char_id, &gatt_desc_id);
530 static void __bt_handle_gatt_client_read_charac(void *buf, uint16_t len)
532 struct hal_ev_gatt_client_read_data *ev = buf;
533 btgatt_read_params_t char_read_parm;
535 char_read_parm.srvc_id.is_primary = ev->is_primary;
536 char_read_parm.srvc_id.id.inst_id = ev->inst_id;
537 memcpy(char_read_parm.srvc_id.id.uuid.uu, ev->svc_uuid, 16);
539 char_read_parm.char_id.inst_id = ev->inst_id;
540 memcpy(char_read_parm.char_id.uuid.uu, ev->char_uuid, 16);
542 char_read_parm.value.len = ev->len;
544 memcpy(char_read_parm.value.value, ev->value, ev->len);
545 char_read_parm.value_type = BT_HAL_GATTC_READ_VALUE_TYPE_VALUE;
548 if (bt_gatt_callbacks->client->read_characteristic_cb)
549 bt_gatt_callbacks->client->read_characteristic_cb(ev->conn_id,
550 ev->status, &char_read_parm);
553 static void __bt_handle_gatt_client_read_desc(void *buf, uint16_t len)
555 struct hal_ev_gatt_client_read_data *ev = buf;
556 btgatt_read_params_t desc_read_parm;
558 desc_read_parm.srvc_id.is_primary = ev->is_primary;
559 desc_read_parm.srvc_id.id.inst_id = ev->inst_id;
560 memcpy(desc_read_parm.srvc_id.id.uuid.uu, ev->svc_uuid, 16);
562 desc_read_parm.char_id.inst_id = ev->inst_id;
563 memcpy(desc_read_parm.char_id.uuid.uu, ev->char_uuid, 16);
565 desc_read_parm.descr_id.inst_id = ev->inst_id;
566 memcpy(desc_read_parm.descr_id.uuid.uu, ev->desc_uuid, 16);
568 desc_read_parm.value.len = ev->len;
570 memcpy(desc_read_parm.value.value, ev->value, ev->len);
571 desc_read_parm.value_type = BT_HAL_GATTC_READ_VALUE_TYPE_VALUE;
574 if (bt_gatt_callbacks->client->read_descriptor_cb)
575 bt_gatt_callbacks->client->read_descriptor_cb(ev->conn_id,
576 ev->status, &desc_read_parm);
579 static void __bt_handle_gatt_client_write_char(void *buf, uint16_t len)
581 struct hal_ev_gatt_client_write_result *ev = buf;
582 btgatt_write_params_t char_write_parm;
584 char_write_parm.srvc_id.is_primary = ev->is_primary;
585 char_write_parm.srvc_id.id.inst_id = ev->inst_id;
586 memcpy(char_write_parm.srvc_id.id.uuid.uu, ev->svc_uuid, 16);
588 char_write_parm.char_id.inst_id = ev->inst_id;
589 memcpy(char_write_parm.char_id.uuid.uu, ev->char_uuid, 16);
591 if (bt_gatt_callbacks->client->write_characteristic_cb)
592 bt_gatt_callbacks->client->write_characteristic_cb(ev->conn_id,
593 ev->status, &char_write_parm);
596 static void __bt_handle_gatt_client_write_desc(void *buf, uint16_t len)
598 struct hal_ev_gatt_client_write_result *ev = buf;
599 btgatt_write_params_t desc_write_parm;
601 desc_write_parm.srvc_id.is_primary = ev->is_primary;
602 desc_write_parm.srvc_id.id.inst_id = ev->inst_id;
603 memcpy(desc_write_parm.srvc_id.id.uuid.uu, ev->svc_uuid, 16);
605 desc_write_parm.char_id.inst_id = ev->inst_id;
606 memcpy(desc_write_parm.char_id.uuid.uu, ev->char_uuid, 16);
608 desc_write_parm.descr_id.inst_id = ev->inst_id;
609 memcpy(desc_write_parm.descr_id.uuid.uu, ev->desc_uuid, 16);
611 if (bt_gatt_callbacks->client->write_descriptor_cb)
612 bt_gatt_callbacks->client->write_descriptor_cb(ev->conn_id,
613 ev->status, &desc_write_parm);
616 static void __bt_handle_gatt_client_watch_notification(void *buf, uint16_t len)
618 struct hal_ev_gatt_client_watch_notification *ev = buf;
619 btgatt_srvc_id_t gatt_srvc_id;
620 btgatt_gatt_id_t gatt_char_id;
622 gatt_srvc_id.is_primary = ev->is_primary;
623 gatt_srvc_id.id.inst_id = ev->inst_id;
624 memcpy(gatt_srvc_id.id.uuid.uu, ev->svc_uuid, 16);
626 memcpy(gatt_char_id.uuid.uu, ev->char_uuid, 16);
627 gatt_char_id.inst_id = ev->inst_id;
629 if (bt_gatt_callbacks->client->register_for_notification_cb)
630 bt_gatt_callbacks->client->register_for_notification_cb(ev->client_if,
631 ev->registered, ev->status, &gatt_srvc_id, &gatt_char_id);
634 static bt_hal_le_adv_info_t *__bt_hal_get_adv_ind_info(char *addr)
641 for (l = adv_ind_list; NULL != l; l = g_slist_next(l)) {
642 bt_hal_le_adv_info_t *adv_info = l->data;
647 if (!g_strcmp0(adv_info->addr, addr))
654 static int __bt_hal_add_adv_ind_info(bt_hal_le_adv_info_t *adv_info)
659 if (__bt_hal_get_adv_ind_info(adv_info->addr) != NULL) {
660 DBG("Adv info already present");
664 adv_ind_list = g_slist_append(adv_ind_list, adv_info);
668 static gboolean __bt_hal_adv_scan_req_timeout_cb(gpointer user_data)
670 bt_hal_le_adv_info_t *adv_info = user_data;
672 __bt_hal_send_le_scan_result_event(adv_info);
673 adv_ind_list = g_slist_remove(adv_ind_list, adv_info);
678 static void __bt_hal_handle_gatt_client_scan_result(void *buf, uint16_t len)
682 struct hal_ev_gatt_client_scan_result *ev = buf;
683 bt_hal_le_adv_info_t *adv_info;
686 memcpy(bd_addr.address, ev->bd_addr, 6);
687 bt_bdaddr_t2str(&bd_addr, address);
688 data_len = (ev->len < BT_HAL_ADV_DATA_MAX_SIZE) ? ev->len : BT_HAL_ADV_DATA_MAX_SIZE;
689 if (data_len == 0 && ev->adv_type != BT_LE_ADV_SCAN_RSP) {
690 ERR("Unexpected: Data len is 0");
694 if (ev->adv_type == BT_LE_ADV_SCAN_RSP) { /* SCAN_RSP */
695 adv_info = __bt_hal_get_adv_ind_info(address);
697 /* Copy scan response data in data field and send event */
698 memcpy(&(adv_info->data[adv_info->data_len]), ev->adv_data, data_len);
699 __bt_hal_send_le_scan_result_event(adv_info);
700 adv_ind_list = g_slist_remove(adv_ind_list, adv_info);
701 if (adv_info->timer_id)
702 g_source_remove(adv_info->timer_id);
709 adv_info = g_malloc0(sizeof(bt_hal_le_adv_info_t));
711 ERR("Not enough memory");
715 g_strlcpy(adv_info->addr, address, 18);
716 adv_info->addr_type = ev->addr_type;
717 adv_info->adv_type = ev->adv_type;
718 adv_info->rssi = ev->rssi;
719 adv_info->data_len = data_len;
720 memcpy(adv_info->data, ev->adv_data, data_len);
723 if (_bt_hal_gatt_client_get_le_scan_type() == BT_GATTC_LE_SCAN_TYPE_PASSIVE) {
724 __bt_hal_send_le_scan_result_event(adv_info);
729 if (__bt_hal_add_adv_ind_info(adv_info))
730 adv_info->timer_id = g_timeout_add(1000,
731 __bt_hal_adv_scan_req_timeout_cb,
735 /*******************************************************************************
737 ** Function gatt_init
739 ** Description Initializes the GATT interface
741 ** Returns bt_status_t
743 *******************************************************************************/
744 static bt_status_t gatt_init(const btgatt_callbacks_t* callbacks)
746 bt_gatt_callbacks = callbacks;
747 DBG("Register A2DP Src events callback function");
748 _bt_hal_register_gatt_le_dbus_handler_cb(__bt_hal_gatt_events);
749 _bt_hal_register_gatt_server_handler_cb(__bt_hal_gatt_events);
750 _bt_hal_register_gatt_client_handler_cb(__bt_hal_gatt_events);
751 _bt_hal_register_event_handler_cb(HAL_GATT, __bt_hal_gatt_events);
753 return BT_STATUS_SUCCESS;
756 /*******************************************************************************
758 ** Function gatt_cleanup
760 ** Description Closes the GATT interface
764 *******************************************************************************/
765 static void gatt_cleanup(void)
767 _bt_hal_unregister_gatt_le_dbus_handler_cb();
768 _bt_hal_unregister_gatt_client_handler_cb();
769 _bt_hal_unregister_gatt_server_handler_cb();
770 _bt_hal_unregister_event_handler_cb(HAL_GATT);
772 if (bt_gatt_callbacks)
773 bt_gatt_callbacks = NULL;
776 static const btgatt_interface_t btgatt_interface = {
777 sizeof(btgatt_interface),
782 &btgatt_client_interface,
783 &btgatt_server_interface,
786 /*******************************************************************************
788 ** Function bt_get_gatt_interface
790 ** Description Get the gatt callback interface
792 ** Returns btgatt_interface_t
794 *******************************************************************************/
795 const btgatt_interface_t *bt_get_gatt_interface()
797 return &btgatt_interface;