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 static void __bt_handle_gatt_client_changed_value(void *buf, uint16_t len);
88 /*****************************************************************************************************/
90 static bool interface_ready(void)
92 return bt_gatt_callbacks != NULL;
95 static void __bt_hal_handle_server_instance_initialized(void *buf, uint16_t len)
97 struct hal_ev_server_instance_registered *ev = buf;
99 if (bt_gatt_callbacks->server->register_server_cb)
100 bt_gatt_callbacks->server->register_server_cb(ev->status, ev->server_instance, (bt_uuid_t*)&ev->app_uuid);
103 static void __bt_hal_handle_multi_adv_data_set(void *buf, uint16_t len)
105 struct hal_ev_multi_adv_data_set *ev = buf;
107 if (bt_gatt_callbacks->server->multi_adv_data_cb)
108 bt_gatt_callbacks->server->multi_adv_data_cb(ev->server_instance, ev->status);
111 static void __bt_hal_handle_multi_adv_data_update(void *buf, uint16_t len)
113 struct hal_ev_multi_adv_update *ev = buf;
115 if (bt_gatt_callbacks->server->multi_adv_update_cb)
116 bt_gatt_callbacks->server->multi_adv_update_cb(ev->server_instance, ev->status);
119 static void __bt_hal_handle_multi_adv_enable(void *buf, uint16_t len)
121 struct hal_ev_multi_adv_enable *ev = buf;
123 if (bt_gatt_callbacks->server->multi_adv_enable_cb)
124 bt_gatt_callbacks->server->multi_adv_enable_cb(ev->server_instance, ev->status);
127 static void __bt_hal_handle_multi_adv_disable(void *buf, uint16_t len)
129 struct hal_ev_multi_adv_disable *ev = buf;
131 if (bt_gatt_callbacks->server->multi_adv_disable_cb)
132 bt_gatt_callbacks->server->multi_adv_disable_cb(ev->server_instance, ev->status);
135 /* Same callback for both Enable and DIsable */
136 static void __bt_hal_handle_legacy_adv_status(void *buf, uint16_t len)
138 struct hal_ev_legacy_adv_status *ev = buf;
140 if (bt_gatt_callbacks->server->listen_cb)
141 bt_gatt_callbacks->server->listen_cb(ev->status, ev->server_instance);
144 static void __bt_hal_handle_service_added(void *buf, uint16_t len)
146 struct hal_ev_gatt_service_added *ev = buf;
147 btgatt_srvc_id_t srvc_id;
149 memset(&srvc_id, 0x00, sizeof(btgatt_srvc_id_t));
150 srvc_id.is_primary = ev->is_primary;
151 srvc_id.id.inst_id = ev->server_instance;
152 memcpy(srvc_id.id.uuid.uu, ev->svc_uuid, sizeof(ev->svc_uuid));
154 if (bt_gatt_callbacks->server->service_added_cb)
155 bt_gatt_callbacks->server->service_added_cb(ev->status, ev->server_instance,
156 &srvc_id, ev->service_handle);
159 static void __bt_hal_handle_char_added(void *buf, uint16_t len)
161 struct hal_ev_gatt_char_added *ev = buf;
164 memcpy(uuid.uu, ev->char_uuid, sizeof(ev->char_uuid));
166 if (bt_gatt_callbacks->server->characteristic_added_cb)
167 bt_gatt_callbacks->server->characteristic_added_cb(ev->status, ev->server_instance,
168 &uuid, ev->service_handle, ev->char_handle);
171 static void __bt_hal_handle_desc_added(void *buf, uint16_t len)
173 struct hal_ev_gatt_desc_added *ev = buf;
176 memcpy(uuid.uu, ev->desc_uuid, sizeof(ev->desc_uuid));
178 if (bt_gatt_callbacks->server->descriptor_added_cb)
179 bt_gatt_callbacks->server->descriptor_added_cb(ev->status, ev->server_instance,
180 &uuid, ev->service_handle, ev->desc_handle);
183 static void __bt_hal_handle_service_started(void *buf, uint16_t len)
185 struct hal_ev_gatt_service_started *ev = buf;
187 if (bt_gatt_callbacks->server->service_started_cb)
188 bt_gatt_callbacks->server->service_started_cb(ev->status, ev->server_instance,
192 static void __bt_hal_handle_service_deleted(void *buf, uint16_t len)
194 struct hal_ev_gatt_service_deleted *ev = buf;
196 if (bt_gatt_callbacks->server->service_deleted_cb)
197 bt_gatt_callbacks->server->service_deleted_cb(ev->status, ev->server_instance,
201 static void __bt_hal_handle_gatt_server_connected(void *buf, uint16_t len)
203 struct hal_ev_gatt_server_connected *ev = buf;
206 memcpy(bd_addr.address, ev->bdaddr, 6);
208 if (bt_gatt_callbacks->server->connection_cb)
209 bt_gatt_callbacks->server->connection_cb(ev->conn_id, ev->server_instance,
210 ev->connected, &bd_addr);
213 static void __bt_hal_handle_gatt_server_notification_changed(void *buf, uint16_t len)
215 struct hal_ev_gatt_server_notifcation_change *ev = buf;
218 memcpy(bd_addr.address, ev->bdaddr, 6);
220 if (bt_gatt_callbacks->server->notif_enabled_cb)
221 bt_gatt_callbacks->server->notif_enabled_cb(ev->conn_id, ev->trans_id,
222 ev->att_handle, ev->notify, &bd_addr);
225 static void __bt_hal_handle_gatt_server_read_requested(void *buf, uint16_t len)
227 struct hal_ev_gatt_server_read_req *ev = buf;
230 memcpy(bd_addr.address, ev->bdaddr, 6);
232 if (bt_gatt_callbacks->server->request_read_cb)
233 bt_gatt_callbacks->server->request_read_cb(ev->conn_id, ev->trans_id, &bd_addr,
234 ev->att_handle, ev->offset, ev->is_long);
237 static void __bt_hal_handle_gatt_server_write_requested(void *buf, uint16_t len)
239 struct hal_ev_gatt_server_write_req *ev = buf;
241 if (bt_gatt_callbacks->server->request_write_cb)
242 bt_gatt_callbacks->server->request_write_cb(ev->conn_id, ev->trans_id,
243 (bt_bdaddr_t *) ev->bdaddr,
244 ev->att_handle, ev->offset,
245 ev->length, ev->need_rsp,
246 ev->is_prep, ev->value);
249 static void __bt_hal_handle_gatt_server_acquire_write_requested(void *buf, uint16_t len)
251 struct hal_ev_gatt_server_acquire_write_res *ev = buf;
253 DBG("acquire write requested event recived");
255 if (bt_gatt_callbacks->server->request_acquire_write_cb)
256 bt_gatt_callbacks->server->request_acquire_write_cb(ev->mtu, ev->conn_id, ev->trans_id, ev->char_handl, (bt_bdaddr_t *)ev->bdaddr);
259 static void __bt_hal_handle_gatt_server_acquire_notify_requested(void *buf, uint16_t len)
261 struct hal_ev_gatt_server_acquire_notify *ev = buf;
263 DBG("acquire notify requested event recived");
265 if (bt_gatt_callbacks->server->request_acquire_notify_cb)
266 bt_gatt_callbacks->server->request_acquire_notify_cb(ev->mtu, ev->conn_id, ev->trans_id, ev->char_handl);
269 static void __bt_hal_handle_gatt_server_indicate_confirmed(void *buf, uint16_t len)
271 struct hal_ev_gatt_server_indicate_cfm *ev = buf;
273 if (bt_gatt_callbacks->server->indication_confirmation_cb)
274 bt_gatt_callbacks->server->indication_confirmation_cb(ev->conn_id, ev->trans_id,
275 ev->att_handle, (bt_bdaddr_t *) ev->bdaddr);
279 static void __bt_hal_gatt_events(int message, void *buf, uint16_t len)
282 /* Check if GATT interface is Ready */
283 if (!interface_ready())
287 case HAL_EV_SERVER_INSTANCE_INITIALIZED: {
288 __bt_hal_handle_server_instance_initialized(buf, len);
291 case HAL_EV_MULTI_ADV_ENABLE: {
292 __bt_hal_handle_multi_adv_enable(buf, len);
295 case HAL_EV_MULTI_ADV_DISABLE: {
296 __bt_hal_handle_multi_adv_disable(buf, len);
299 case HAL_EV_MULTI_ADV_DATA_SET: {
300 __bt_hal_handle_multi_adv_data_set(buf, len);
303 case HAL_EV_MULTI_ADV_UPDATE: {
304 __bt_hal_handle_multi_adv_data_update(buf, len);
307 case HAL_EV_LEGACY_ADV_ENABLE: {
308 __bt_hal_handle_legacy_adv_status(buf, len);
311 case HAL_EV_GATT_CLIENT_REGISTERED: {
312 __bt_handle_gatt_client_registered(buf, len);
315 case HAL_EV_GATT_CLIENT_SCAN_RESULT: {
316 __bt_hal_handle_gatt_client_scan_result(buf, len);
319 case HAL_EV_GATT_SERVICE_ADDED: {
320 __bt_hal_handle_service_added(buf, len);
323 case HAL_EV_GATT_CHAR_ADDED: {
324 __bt_hal_handle_char_added(buf, len);
327 case HAL_EV_GATT_DESC_ADDED: {
328 __bt_hal_handle_desc_added(buf, len);
331 case HAL_EV_GATT_SERVICE_STARTED: {
332 __bt_hal_handle_service_started(buf, len);
335 case HAL_EV_GATT_SERVICE_DELETED: {
336 __bt_hal_handle_service_deleted(buf, len);
339 case HAL_EV_GATT_SERVER_CONNECTED: {
340 __bt_hal_handle_gatt_server_connected(buf, len);
343 case HAL_EV_GATT_NOTIFICATION_CHANGE: {
344 __bt_hal_handle_gatt_server_notification_changed(buf, len);
347 case HAL_EV_GATT_READ_REQUESTED: {
348 __bt_hal_handle_gatt_server_read_requested(buf, len);
351 case HAL_EV_GATT_WRITE_REQUESTED: {
352 __bt_hal_handle_gatt_server_write_requested(buf, len);
355 case HAL_EV_GATT_INDICATE_CFM: {
356 __bt_hal_handle_gatt_server_indicate_confirmed(buf, len);
359 case HAL_EV_GATT_CLIENT_CONNECTED: {
360 __bt_handle_gatt_client_connected(buf, len);
363 case HAL_EV_GATT_CLIENT_DISCONNECTED: {
364 __bt_handle_gatt_client_disconnected(buf, len);
367 case HAL_EV_GATT_CLIENT_SEARCH_RESULT: {
368 __bt_handle_gatt_client_search_result(buf, len);
371 case HAL_EV_GATT_CLIENT_SEARCH_COMPLETE: {
372 __bt_handle_gatt_client_search_complete(buf, len);
375 case HAL_EV_GATT_CLIENT_CHARAC_SEARCH_RESULT: {
376 __bt_handle_gatt_client_search_char_result(buf, len);
379 case HAL_EV_GATT_CLIENT_DESC_SEARCH_RESULT: {
380 __bt_handle_gatt_client_search_desc_result(buf, len);
383 case HAL_EV_GATT_CLIENT_READ_CHARAC: {
384 __bt_handle_gatt_client_read_charac(buf, len);
387 case HAL_EV_GATT_CLIENT_READ_DESC: {
388 __bt_handle_gatt_client_read_desc(buf, len);
391 case HAL_EV_GATT_CLIENT_WRITE_CHARAC: {
392 __bt_handle_gatt_client_write_char(buf, len);
395 case HAL_EV_GATT_CLIENT_WRITE_DESC: {
396 __bt_handle_gatt_client_write_desc(buf, len);
399 case HAL_EV_GATT_CLIENT_WATCH_NOTIFICATION: {
400 __bt_handle_gatt_client_watch_notification(buf, len);
403 case HAL_EV_GATT_CLIENT_NOTIFY_CHANGED_VALUE: {
404 __bt_handle_gatt_client_changed_value(buf, len);
407 case HAL_EV_GATT_SERVER_ACQUIRE_WRITE_RES:{
408 __bt_hal_handle_gatt_server_acquire_write_requested(buf, len);
411 case HAL_EV_GATT_SERVER_ACQUIRE_NOTIFY_RES:{
412 __bt_hal_handle_gatt_server_acquire_notify_requested(buf, len);
417 DBG("Event Currently not handled!!");
423 /************************************* GATT CLIENT EVENTS ****************************************/
424 static void __bt_handle_gatt_client_registered(void *buf, uint16_t len)
426 struct hal_ev_gatt_client_registered *ev = buf;
429 memcpy(uuid.uu, ev->app_uuid, 16);
430 if (bt_gatt_callbacks->client->register_client_cb)
431 bt_gatt_callbacks->client->register_client_cb(
432 ev->status, ev->client_if, &uuid);
435 static void __bt_hal_send_le_scan_result_event(bt_hal_le_adv_info_t *adv_info)
439 str2bt_bdaddr_t(adv_info->addr, &bd_addr);
440 if (bt_gatt_callbacks->client->scan_result_cb)
441 bt_gatt_callbacks->client->scan_result_cb(
442 &bd_addr, adv_info->rssi, adv_info->data);
445 static void __bt_handle_gatt_client_connected(void *buf, uint16_t len)
447 struct hal_ev_gatt_client_connected *ev = buf;
450 memcpy(bd_addr.address, ev->bdaddr, 6);
451 if (bt_gatt_callbacks->client->open_cb)
452 bt_gatt_callbacks->client->open_cb(ev->conn_id,
453 ev->status, ev->client_if, &bd_addr);
456 static void __bt_handle_gatt_client_disconnected(void *buf, uint16_t len)
458 struct hal_ev_gatt_client_connected *ev = buf;
461 memcpy(bd_addr.address, ev->bdaddr, 6);
462 if (bt_gatt_callbacks->client->close_cb)
463 bt_gatt_callbacks->client->close_cb(ev->conn_id,
464 ev->status, ev->client_if, &bd_addr);
467 static void __bt_handle_gatt_client_search_result(void *buf, uint16_t len)
469 struct hal_ev_gatt_client_search_result *ev = buf;
470 btgatt_srvc_id_t gatt_srvc_id;
472 gatt_srvc_id.is_primary = ev->is_primary;
473 gatt_srvc_id.id.inst_id = ev->inst_id;
474 memcpy(gatt_srvc_id.id.uuid.uu, ev->uuid, 16);
476 if (bt_gatt_callbacks->client->search_result_cb)
477 bt_gatt_callbacks->client->search_result_cb(ev->conn_id,
481 static void __bt_handle_gatt_client_search_complete(void *buf, uint16_t len)
483 struct hal_ev_gatt_client_search_complete *ev = buf;
485 if (bt_gatt_callbacks->client->search_complete_cb)
486 bt_gatt_callbacks->client->search_complete_cb(ev->conn_id,
490 static void __bt_handle_gatt_client_search_char_result(void *buf, uint16_t len)
492 struct hal_ev_gatt_client_char_search_result *ev = buf;
493 btgatt_srvc_id_t gatt_srvc_id;
494 btgatt_gatt_id_t gatt_char_id;
496 gatt_srvc_id.is_primary = ev->is_primary;
497 gatt_srvc_id.id.inst_id = ev->inst_id;
498 memcpy(gatt_srvc_id.id.uuid.uu, ev->svc_uuid, 16);
500 if (BT_STATUS_SUCCESS == ev->status) {
501 memcpy(gatt_char_id.uuid.uu, ev->char_uuid, 16);
502 gatt_char_id.inst_id = ev->inst_id;
505 if (bt_gatt_callbacks->client->get_characteristic_cb)
506 bt_gatt_callbacks->client->get_characteristic_cb(ev->conn_id,
507 ev->status, &gatt_srvc_id, &gatt_char_id, ev->char_prop);
510 static void __bt_handle_gatt_client_search_desc_result(void *buf, uint16_t len)
512 struct hal_ev_gatt_client_desc_search_result *ev = buf;
513 btgatt_srvc_id_t gatt_srvc_id;
514 btgatt_gatt_id_t gatt_char_id;
515 btgatt_gatt_id_t gatt_desc_id;
518 gatt_srvc_id.is_primary = ev->is_primary;
519 gatt_srvc_id.id.inst_id = ev->inst_id;
520 memcpy(gatt_srvc_id.id.uuid.uu, ev->svc_uuid, 16);
522 memcpy(gatt_char_id.uuid.uu, ev->char_uuid, 16);
523 gatt_char_id.inst_id = ev->inst_id;
525 if (BT_STATUS_SUCCESS == ev->status) {
526 memcpy(gatt_desc_id.uuid.uu, ev->desc_uuid, 16);
527 gatt_desc_id.inst_id = ev->inst_id;
530 if (bt_gatt_callbacks->client->get_descriptor_cb)
531 bt_gatt_callbacks->client->get_descriptor_cb(ev->conn_id,
532 ev->status, &gatt_srvc_id, &gatt_char_id, &gatt_desc_id);
535 static void __bt_handle_gatt_client_read_charac(void *buf, uint16_t len)
537 struct hal_ev_gatt_client_read_data *ev = buf;
538 btgatt_read_params_t char_read_parm;
540 char_read_parm.srvc_id.is_primary = ev->is_primary;
541 char_read_parm.srvc_id.id.inst_id = ev->inst_id;
542 memcpy(char_read_parm.srvc_id.id.uuid.uu, ev->svc_uuid, 16);
544 char_read_parm.char_id.inst_id = ev->inst_id;
545 memcpy(char_read_parm.char_id.uuid.uu, ev->char_uuid, 16);
547 char_read_parm.value.len = ev->len;
549 memcpy(char_read_parm.value.value, ev->value, ev->len);
550 char_read_parm.value_type = BT_HAL_GATTC_READ_VALUE_TYPE_VALUE;
553 if (bt_gatt_callbacks->client->read_characteristic_cb)
554 bt_gatt_callbacks->client->read_characteristic_cb(ev->conn_id,
555 ev->status, &char_read_parm);
558 static void __bt_handle_gatt_client_read_desc(void *buf, uint16_t len)
560 struct hal_ev_gatt_client_read_data *ev = buf;
561 btgatt_read_params_t desc_read_parm;
563 desc_read_parm.srvc_id.is_primary = ev->is_primary;
564 desc_read_parm.srvc_id.id.inst_id = ev->inst_id;
565 memcpy(desc_read_parm.srvc_id.id.uuid.uu, ev->svc_uuid, 16);
567 desc_read_parm.char_id.inst_id = ev->inst_id;
568 memcpy(desc_read_parm.char_id.uuid.uu, ev->char_uuid, 16);
570 desc_read_parm.descr_id.inst_id = ev->inst_id;
571 memcpy(desc_read_parm.descr_id.uuid.uu, ev->desc_uuid, 16);
573 desc_read_parm.value.len = ev->len;
575 memcpy(desc_read_parm.value.value, ev->value, ev->len);
576 desc_read_parm.value_type = BT_HAL_GATTC_READ_VALUE_TYPE_VALUE;
579 if (bt_gatt_callbacks->client->read_descriptor_cb)
580 bt_gatt_callbacks->client->read_descriptor_cb(ev->conn_id,
581 ev->status, &desc_read_parm);
584 static void __bt_handle_gatt_client_write_char(void *buf, uint16_t len)
586 struct hal_ev_gatt_client_write_result *ev = buf;
587 btgatt_write_params_t char_write_parm;
589 char_write_parm.srvc_id.is_primary = ev->is_primary;
590 char_write_parm.srvc_id.id.inst_id = ev->inst_id;
591 memcpy(char_write_parm.srvc_id.id.uuid.uu, ev->svc_uuid, 16);
593 char_write_parm.char_id.inst_id = ev->inst_id;
594 memcpy(char_write_parm.char_id.uuid.uu, ev->char_uuid, 16);
596 if (bt_gatt_callbacks->client->write_characteristic_cb)
597 bt_gatt_callbacks->client->write_characteristic_cb(ev->conn_id,
598 ev->status, &char_write_parm);
601 static void __bt_handle_gatt_client_write_desc(void *buf, uint16_t len)
603 struct hal_ev_gatt_client_write_result *ev = buf;
604 btgatt_write_params_t desc_write_parm;
606 desc_write_parm.srvc_id.is_primary = ev->is_primary;
607 desc_write_parm.srvc_id.id.inst_id = ev->inst_id;
608 memcpy(desc_write_parm.srvc_id.id.uuid.uu, ev->svc_uuid, 16);
610 desc_write_parm.char_id.inst_id = ev->inst_id;
611 memcpy(desc_write_parm.char_id.uuid.uu, ev->char_uuid, 16);
613 desc_write_parm.descr_id.inst_id = ev->inst_id;
614 memcpy(desc_write_parm.descr_id.uuid.uu, ev->desc_uuid, 16);
616 if (bt_gatt_callbacks->client->write_descriptor_cb)
617 bt_gatt_callbacks->client->write_descriptor_cb(ev->conn_id,
618 ev->status, &desc_write_parm);
621 static void __bt_handle_gatt_client_watch_notification(void *buf, uint16_t len)
623 struct hal_ev_gatt_client_watch_notification *ev = buf;
624 btgatt_srvc_id_t gatt_srvc_id;
625 btgatt_gatt_id_t gatt_char_id;
627 gatt_srvc_id.is_primary = ev->is_primary;
628 gatt_srvc_id.id.inst_id = ev->inst_id;
629 memcpy(gatt_srvc_id.id.uuid.uu, ev->svc_uuid, 16);
631 memcpy(gatt_char_id.uuid.uu, ev->char_uuid, 16);
632 gatt_char_id.inst_id = ev->inst_id;
634 if (bt_gatt_callbacks->client->register_for_notification_cb)
635 bt_gatt_callbacks->client->register_for_notification_cb(ev->client_if,
636 ev->registered, ev->status, &gatt_srvc_id, &gatt_char_id);
639 static void __bt_handle_gatt_client_changed_value(void *buf, uint16_t len)
641 struct hal_ev_gatt_client_notify_changed_value *ev = buf;
642 btgatt_notify_params_t changd_value_parm;
644 changd_value_parm.srvc_id.is_primary = ev->is_primary;
645 changd_value_parm.srvc_id.id.inst_id = ev->inst_id;
646 memcpy(changd_value_parm.srvc_id.id.uuid.uu, ev->svc_uuid, 16);
648 changd_value_parm.char_id.inst_id = ev->inst_id;
649 memcpy(changd_value_parm.char_id.uuid.uu, ev->char_uuid, 16);
650 changd_value_parm.is_notify = ev->is_notify;
652 memcpy(changd_value_parm.bda.address, ev->bdaddr, 6);
654 changd_value_parm.len = ev->len;
656 memcpy(changd_value_parm.value, ev->value, ev->len);
658 if (bt_gatt_callbacks->client->notify_cb)
659 bt_gatt_callbacks->client->notify_cb(ev->conn_id, &changd_value_parm);
662 static bt_hal_le_adv_info_t *__bt_hal_get_adv_ind_info(char *addr)
669 for (l = adv_ind_list; NULL != l; l = g_slist_next(l)) {
670 bt_hal_le_adv_info_t *adv_info = l->data;
675 if (!g_strcmp0(adv_info->addr, addr))
682 static int __bt_hal_add_adv_ind_info(bt_hal_le_adv_info_t *adv_info)
687 if (__bt_hal_get_adv_ind_info(adv_info->addr) != NULL) {
688 DBG("Adv info already present");
692 adv_ind_list = g_slist_append(adv_ind_list, adv_info);
696 static gboolean __bt_hal_adv_scan_req_timeout_cb(gpointer user_data)
698 bt_hal_le_adv_info_t *adv_info = user_data;
700 __bt_hal_send_le_scan_result_event(adv_info);
701 adv_ind_list = g_slist_remove(adv_ind_list, adv_info);
706 static void __bt_hal_handle_gatt_client_scan_result(void *buf, uint16_t len)
710 struct hal_ev_gatt_client_scan_result *ev = buf;
711 bt_hal_le_adv_info_t *adv_info;
714 memcpy(bd_addr.address, ev->bd_addr, 6);
715 bt_bdaddr_t2str(&bd_addr, address);
716 data_len = (ev->len < BT_HAL_ADV_DATA_MAX_SIZE) ? ev->len : BT_HAL_ADV_DATA_MAX_SIZE;
717 if (data_len == 0 && ev->adv_type != BT_LE_ADV_SCAN_RSP) {
718 ERR("Unexpected: Data len is 0");
722 if (ev->adv_type == BT_LE_ADV_SCAN_RSP) { /* SCAN_RSP */
723 adv_info = __bt_hal_get_adv_ind_info(address);
725 /* Copy scan response data in data field and send event */
726 memcpy(&(adv_info->data[adv_info->data_len]), ev->adv_data, data_len);
727 __bt_hal_send_le_scan_result_event(adv_info);
728 adv_ind_list = g_slist_remove(adv_ind_list, adv_info);
729 if (adv_info->timer_id)
730 g_source_remove(adv_info->timer_id);
737 adv_info = g_malloc0(sizeof(bt_hal_le_adv_info_t));
739 ERR("Not enough memory");
743 g_strlcpy(adv_info->addr, address, 18);
744 adv_info->addr_type = ev->addr_type;
745 adv_info->adv_type = ev->adv_type;
746 adv_info->rssi = ev->rssi;
747 adv_info->data_len = data_len;
748 memcpy(adv_info->data, ev->adv_data, data_len);
751 if (_bt_hal_gatt_client_get_le_scan_type() == BT_GATTC_LE_SCAN_TYPE_PASSIVE) {
752 __bt_hal_send_le_scan_result_event(adv_info);
757 if (__bt_hal_add_adv_ind_info(adv_info))
758 adv_info->timer_id = g_timeout_add(1000,
759 __bt_hal_adv_scan_req_timeout_cb,
763 /*******************************************************************************
765 ** Function gatt_init
767 ** Description Initializes the GATT interface
769 ** Returns bt_status_t
771 *******************************************************************************/
772 static bt_status_t gatt_init(const btgatt_callbacks_t* callbacks)
774 bt_gatt_callbacks = callbacks;
775 DBG("Register A2DP Src events callback function");
776 _bt_hal_register_gatt_le_dbus_handler_cb(__bt_hal_gatt_events);
777 _bt_hal_register_gatt_server_handler_cb(__bt_hal_gatt_events);
778 _bt_hal_register_gatt_client_handler_cb(__bt_hal_gatt_events);
779 _bt_hal_register_event_handler_cb(HAL_GATT, __bt_hal_gatt_events);
781 return BT_STATUS_SUCCESS;
784 /*******************************************************************************
786 ** Function gatt_cleanup
788 ** Description Closes the GATT interface
792 *******************************************************************************/
793 static void gatt_cleanup(void)
795 _bt_hal_unregister_gatt_le_dbus_handler_cb();
796 _bt_hal_unregister_gatt_client_handler_cb();
797 _bt_hal_unregister_gatt_server_handler_cb();
798 _bt_hal_unregister_event_handler_cb(HAL_GATT);
800 if (bt_gatt_callbacks)
801 bt_gatt_callbacks = NULL;
804 static const btgatt_interface_t btgatt_interface = {
805 sizeof(btgatt_interface),
810 &btgatt_client_interface,
811 &btgatt_server_interface,
814 /*******************************************************************************
816 ** Function bt_get_gatt_interface
818 ** Description Get the gatt callback interface
820 ** Returns btgatt_interface_t
822 *******************************************************************************/
823 const btgatt_interface_t *bt_get_gatt_interface()
825 return &btgatt_interface;