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 */
61 uint8_t adv_data[BT_HAL_ADV_DATA_MAX_SIZE];
63 uint8_t scan_rsp_data[BT_HAL_ADV_DATA_MAX_SIZE];
64 int scan_rsp_data_len;
66 } bt_hal_le_adv_info_t;
68 /****************************************** Global Variables ******************************************/
69 const btgatt_callbacks_t *bt_gatt_callbacks = NULL;
71 extern btgatt_client_interface_t btgatt_client_interface;
72 extern btgatt_server_interface_t btgatt_server_interface;
74 static GSList *adv_ind_list = NULL;
75 /****************************************** Forward Declarations *************************************/
76 static void __bt_handle_gatt_client_registered(void *buf, uint16_t len);
77 static void __bt_hal_handle_gatt_client_scan_result(void *buf, uint16_t len);
78 static void __bt_handle_gatt_client_connected(void *buf, uint16_t len);
79 static void __bt_handle_gatt_client_disconnected(void *buf, uint16_t len);
80 static void __bt_handle_gatt_client_search_result(void *buf, uint16_t len);
81 static void __bt_handle_gatt_client_search_complete(void *buf, uint16_t len);
82 static void __bt_handle_gatt_client_search_char_result(void *buf, uint16_t len);
83 static void __bt_handle_gatt_client_search_desc_result(void *buf, uint16_t len);
84 static void __bt_handle_gatt_client_read_charac(void *buf, uint16_t len);
85 static void __bt_handle_gatt_client_read_desc(void *buf, uint16_t len);
86 static void __bt_handle_gatt_client_write_char(void *buf, uint16_t len);
87 static void __bt_handle_gatt_client_write_desc(void *buf, uint16_t len);
88 static void __bt_handle_gatt_client_watch_notification(void *buf, uint16_t len);
89 static void __bt_handle_gatt_client_changed_value(void *buf, uint16_t len);
90 static void __bt_hal_handle_gatt_client_service_changed(void *buf, uint16_t len);
91 static void __bt_hal_handle_gatt_client_mtu_exchange_completed(void *buf, uint16_t len);
92 /*****************************************************************************************************/
94 static bool interface_ready(void)
96 return bt_gatt_callbacks != NULL;
99 static void __bt_hal_handle_server_instance_initialized(void *buf, uint16_t len)
101 struct hal_ev_server_instance_registered *ev = buf;
103 if (bt_gatt_callbacks->server->register_server_cb)
104 bt_gatt_callbacks->server->register_server_cb(ev->status, ev->server_instance, (bt_uuid_t*)&ev->app_uuid);
107 static void __bt_hal_handle_multi_adv_data_set(void *buf, uint16_t len)
109 struct hal_ev_multi_adv_data_set *ev = buf;
111 if (bt_gatt_callbacks->server->multi_adv_data_cb)
112 bt_gatt_callbacks->server->multi_adv_data_cb(ev->server_instance, ev->status);
115 static void __bt_hal_handle_multi_adv_data_update(void *buf, uint16_t len)
117 struct hal_ev_multi_adv_update *ev = buf;
119 if (bt_gatt_callbacks->server->multi_adv_update_cb)
120 bt_gatt_callbacks->server->multi_adv_update_cb(ev->server_instance, ev->status);
123 static void __bt_hal_handle_multi_adv_enable(void *buf, uint16_t len)
125 struct hal_ev_multi_adv_enable *ev = buf;
127 if (bt_gatt_callbacks->server->multi_adv_enable_cb)
128 bt_gatt_callbacks->server->multi_adv_enable_cb(ev->server_instance, ev->status);
131 static void __bt_hal_handle_multi_adv_disable(void *buf, uint16_t len)
133 struct hal_ev_multi_adv_disable *ev = buf;
135 if (bt_gatt_callbacks->server->multi_adv_disable_cb)
136 bt_gatt_callbacks->server->multi_adv_disable_cb(ev->server_instance, ev->status);
139 /* Same callback for both Enable and DIsable */
140 static void __bt_hal_handle_legacy_adv_status(void *buf, uint16_t len)
142 struct hal_ev_legacy_adv_status *ev = buf;
144 if (bt_gatt_callbacks->server->listen_cb)
145 bt_gatt_callbacks->server->listen_cb(ev->status, ev->server_instance);
148 static void __bt_hal_handle_service_added(void *buf, uint16_t len)
150 struct hal_ev_gatt_service_added *ev = buf;
151 btgatt_srvc_id_t srvc_id;
153 memset(&srvc_id, 0x00, sizeof(btgatt_srvc_id_t));
154 srvc_id.is_primary = ev->is_primary;
155 srvc_id.id.inst_id = ev->server_instance;
156 memcpy(srvc_id.id.uuid.uu, ev->svc_uuid, sizeof(ev->svc_uuid));
158 if (bt_gatt_callbacks->server->service_added_cb)
159 bt_gatt_callbacks->server->service_added_cb(ev->status, ev->server_instance,
160 &srvc_id, ev->service_handle);
163 static void __bt_hal_handle_char_added(void *buf, uint16_t len)
165 struct hal_ev_gatt_char_added *ev = buf;
168 memcpy(uuid.uu, ev->char_uuid, sizeof(ev->char_uuid));
170 if (bt_gatt_callbacks->server->characteristic_added_cb)
171 bt_gatt_callbacks->server->characteristic_added_cb(ev->status, ev->server_instance,
172 &uuid, ev->service_handle, ev->char_handle);
175 static void __bt_hal_handle_desc_added(void *buf, uint16_t len)
177 struct hal_ev_gatt_desc_added *ev = buf;
180 memcpy(uuid.uu, ev->desc_uuid, sizeof(ev->desc_uuid));
182 if (bt_gatt_callbacks->server->descriptor_added_cb)
183 bt_gatt_callbacks->server->descriptor_added_cb(ev->status, ev->server_instance,
184 &uuid, ev->service_handle, ev->desc_handle);
187 static void __bt_hal_handle_service_started(void *buf, uint16_t len)
189 struct hal_ev_gatt_service_started *ev = buf;
191 if (bt_gatt_callbacks->server->service_started_cb)
192 bt_gatt_callbacks->server->service_started_cb(ev->status, ev->server_instance,
196 static void __bt_hal_handle_service_deleted(void *buf, uint16_t len)
198 struct hal_ev_gatt_service_deleted *ev = buf;
200 if (bt_gatt_callbacks->server->service_deleted_cb)
201 bt_gatt_callbacks->server->service_deleted_cb(ev->status, ev->server_instance,
205 static void __bt_hal_handle_gatt_server_connected(void *buf, uint16_t len)
207 struct hal_ev_gatt_server_connected *ev = buf;
210 memcpy(bd_addr.address, ev->bdaddr, 6);
212 if (bt_gatt_callbacks->server->connection_cb)
213 bt_gatt_callbacks->server->connection_cb(ev->conn_id, ev->server_instance,
214 ev->connected, &bd_addr);
217 static void __bt_hal_handle_gatt_server_notification_changed(void *buf, uint16_t len)
219 struct hal_ev_gatt_server_notifcation_change *ev = buf;
222 memcpy(bd_addr.address, ev->bdaddr, 6);
224 if (bt_gatt_callbacks->server->notif_enabled_cb)
225 bt_gatt_callbacks->server->notif_enabled_cb(ev->conn_id, ev->trans_id,
226 ev->att_handle, ev->notify, &bd_addr);
229 static void __bt_hal_handle_gatt_server_read_requested(void *buf, uint16_t len)
231 struct hal_ev_gatt_server_read_req *ev = buf;
234 memcpy(bd_addr.address, ev->bdaddr, 6);
236 if (bt_gatt_callbacks->server->request_read_cb)
237 bt_gatt_callbacks->server->request_read_cb(ev->conn_id, ev->trans_id, &bd_addr,
238 ev->att_handle, ev->offset, ev->is_long);
241 static void __bt_hal_handle_gatt_server_write_requested(void *buf, uint16_t len)
243 struct hal_ev_gatt_server_write_req *ev = buf;
245 if (bt_gatt_callbacks->server->request_write_cb)
246 bt_gatt_callbacks->server->request_write_cb(ev->conn_id, ev->trans_id,
247 (bt_bdaddr_t *) ev->bdaddr,
248 ev->att_handle, ev->offset,
249 ev->length, ev->need_rsp,
250 ev->is_prep, ev->value);
253 static void __bt_hal_handle_gatt_server_acquire_write_requested(void *buf, uint16_t len)
255 struct hal_ev_gatt_server_acquire_write_res *ev = buf;
257 DBG("acquire write requested event recived");
259 if (bt_gatt_callbacks->server->request_acquire_write_cb)
260 bt_gatt_callbacks->server->request_acquire_write_cb(ev->mtu, ev->conn_id, ev->trans_id, ev->char_handl, (bt_bdaddr_t *)ev->bdaddr);
263 static void __bt_hal_handle_gatt_server_acquire_notify_requested(void *buf, uint16_t len)
265 struct hal_ev_gatt_server_acquire_notify *ev = buf;
267 DBG("acquire notify requested event recived");
269 if (bt_gatt_callbacks->server->request_acquire_notify_cb)
270 bt_gatt_callbacks->server->request_acquire_notify_cb(ev->mtu, ev->conn_id, ev->trans_id, ev->char_handl);
273 static void __bt_hal_handle_gatt_server_indicate_confirmed(void *buf, uint16_t len)
275 struct hal_ev_gatt_server_indicate_cfm *ev = buf;
277 if (bt_gatt_callbacks->server->indication_confirmation_cb)
278 bt_gatt_callbacks->server->indication_confirmation_cb(ev->conn_id, ev->trans_id,
279 ev->att_handle, (bt_bdaddr_t *) ev->bdaddr);
283 static void __bt_hal_handle_gatt_server_mtu_changed(void *buf, uint16_t len)
285 struct hal_ev_gatt_server_mtu_changed *ev = buf;
287 DBG("GATT Server MTU changed event recived");
289 if (bt_gatt_callbacks->server->mtu_changed_cb)
290 bt_gatt_callbacks->server->mtu_changed_cb(ev->conn_id, ev->mtu);
293 static void __bt_hal_gatt_events(int message, void *buf, uint16_t len)
295 DBG("GATT event 0x%x", message);
296 /* Check if GATT interface is Ready */
297 if (!interface_ready())
301 case HAL_EV_SERVER_INSTANCE_INITIALIZED: {
302 __bt_hal_handle_server_instance_initialized(buf, len);
305 case HAL_EV_MULTI_ADV_ENABLE: {
306 __bt_hal_handle_multi_adv_enable(buf, len);
309 case HAL_EV_MULTI_ADV_DISABLE: {
310 __bt_hal_handle_multi_adv_disable(buf, len);
313 case HAL_EV_MULTI_ADV_DATA_SET: {
314 __bt_hal_handle_multi_adv_data_set(buf, len);
317 case HAL_EV_MULTI_ADV_UPDATE: {
318 __bt_hal_handle_multi_adv_data_update(buf, len);
321 case HAL_EV_LEGACY_ADV_ENABLE: {
322 __bt_hal_handle_legacy_adv_status(buf, len);
325 case HAL_EV_GATT_CLIENT_REGISTERED: {
326 __bt_handle_gatt_client_registered(buf, len);
329 case HAL_EV_GATT_CLIENT_SCAN_RESULT: {
330 __bt_hal_handle_gatt_client_scan_result(buf, len);
333 case HAL_EV_GATT_SERVICE_ADDED: {
334 __bt_hal_handle_service_added(buf, len);
337 case HAL_EV_GATT_CHAR_ADDED: {
338 __bt_hal_handle_char_added(buf, len);
341 case HAL_EV_GATT_DESC_ADDED: {
342 __bt_hal_handle_desc_added(buf, len);
345 case HAL_EV_GATT_SERVICE_STARTED: {
346 __bt_hal_handle_service_started(buf, len);
349 case HAL_EV_GATT_SERVICE_DELETED: {
350 __bt_hal_handle_service_deleted(buf, len);
353 case HAL_EV_GATT_SERVER_CONNECTED: {
354 __bt_hal_handle_gatt_server_connected(buf, len);
357 case HAL_EV_GATT_NOTIFICATION_CHANGE: {
358 __bt_hal_handle_gatt_server_notification_changed(buf, len);
361 case HAL_EV_GATT_READ_REQUESTED: {
362 __bt_hal_handle_gatt_server_read_requested(buf, len);
365 case HAL_EV_GATT_WRITE_REQUESTED: {
366 __bt_hal_handle_gatt_server_write_requested(buf, len);
369 case HAL_EV_GATT_INDICATE_CFM: {
370 __bt_hal_handle_gatt_server_indicate_confirmed(buf, len);
373 case HAL_EV_GATT_CLIENT_CONNECTED: {
374 __bt_handle_gatt_client_connected(buf, len);
377 case HAL_EV_GATT_CLIENT_DISCONNECTED: {
378 __bt_handle_gatt_client_disconnected(buf, len);
381 case HAL_EV_GATT_CLIENT_SEARCH_RESULT: {
382 __bt_handle_gatt_client_search_result(buf, len);
385 case HAL_EV_GATT_CLIENT_SEARCH_COMPLETE: {
386 __bt_handle_gatt_client_search_complete(buf, len);
389 case HAL_EV_GATT_CLIENT_CHARAC_SEARCH_RESULT: {
390 __bt_handle_gatt_client_search_char_result(buf, len);
393 case HAL_EV_GATT_CLIENT_DESC_SEARCH_RESULT: {
394 __bt_handle_gatt_client_search_desc_result(buf, len);
397 case HAL_EV_GATT_CLIENT_READ_CHARAC: {
398 __bt_handle_gatt_client_read_charac(buf, len);
401 case HAL_EV_GATT_CLIENT_READ_DESC: {
402 __bt_handle_gatt_client_read_desc(buf, len);
405 case HAL_EV_GATT_CLIENT_WRITE_CHARAC: {
406 __bt_handle_gatt_client_write_char(buf, len);
409 case HAL_EV_GATT_CLIENT_WRITE_DESC: {
410 __bt_handle_gatt_client_write_desc(buf, len);
413 case HAL_EV_GATT_CLIENT_WATCH_NOTIFICATION: {
414 __bt_handle_gatt_client_watch_notification(buf, len);
417 case HAL_EV_GATT_CLIENT_NOTIFY_CHANGED_VALUE: {
418 __bt_handle_gatt_client_changed_value(buf, len);
421 case HAL_EV_GATT_CLIENT_SERVICE_CHANGED: {
422 __bt_hal_handle_gatt_client_service_changed(buf, len);
425 case HAL_EV_GATT_SERVER_ACQUIRE_WRITE_RES:{
426 __bt_hal_handle_gatt_server_acquire_write_requested(buf, len);
429 case HAL_EV_GATT_SERVER_ACQUIRE_NOTIFY_RES:{
430 __bt_hal_handle_gatt_server_acquire_notify_requested(buf, len);
433 case HAL_EV_GATT_SERVER_MTU_CHANGED:{
434 __bt_hal_handle_gatt_server_mtu_changed(buf, len);
438 case HAL_EV_GATT_CLIENT_MTU_EXCHANGE_COMPLETED:{
439 __bt_hal_handle_gatt_client_mtu_exchange_completed(buf, len);
443 DBG("Event Currently not handled!!");
448 /************************************* GATT CLIENT EVENTS ****************************************/
449 static void __bt_handle_gatt_client_registered(void *buf, uint16_t len)
451 struct hal_ev_gatt_client_registered *ev = buf;
454 memcpy(uuid.uu, ev->app_uuid, 16);
455 if (bt_gatt_callbacks->client->register_client_cb)
456 bt_gatt_callbacks->client->register_client_cb(
457 ev->status, ev->client_if, &uuid);
460 static void __bt_hal_send_le_scan_result_event(bt_hal_le_adv_info_t *adv_info)
464 str2bt_bdaddr_t(adv_info->addr, &bd_addr);
465 if (bt_gatt_callbacks->client->scan_result_cb)
466 bt_gatt_callbacks->client->scan_result_cb(
467 &bd_addr, adv_info->addr_type, adv_info->rssi,
468 adv_info->adv_data, adv_info->adv_data_len,
469 adv_info->scan_rsp_data, adv_info->scan_rsp_data_len);
472 static void __bt_handle_gatt_client_connected(void *buf, uint16_t len)
474 struct hal_ev_gatt_client_connected *ev = buf;
477 memcpy(bd_addr.address, ev->bdaddr, 6);
478 if (bt_gatt_callbacks->client->open_cb)
479 bt_gatt_callbacks->client->open_cb(ev->conn_id,
480 ev->status, ev->client_if, &bd_addr);
483 static void __bt_handle_gatt_client_disconnected(void *buf, uint16_t len)
485 struct hal_ev_gatt_client_connected *ev = buf;
488 memcpy(bd_addr.address, ev->bdaddr, 6);
489 if (bt_gatt_callbacks->client->close_cb)
490 bt_gatt_callbacks->client->close_cb(ev->conn_id,
491 ev->status, ev->client_if, &bd_addr);
494 static void __bt_handle_gatt_client_search_result(void *buf, uint16_t len)
496 struct hal_ev_gatt_client_search_result *ev = buf;
497 btgatt_srvc_id_t gatt_srvc_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->uuid, 16);
503 if (bt_gatt_callbacks->client->search_result_cb)
504 bt_gatt_callbacks->client->search_result_cb(ev->conn_id,
508 static void __bt_handle_gatt_client_search_complete(void *buf, uint16_t len)
510 struct hal_ev_gatt_client_search_complete *ev = buf;
512 if (bt_gatt_callbacks->client->search_complete_cb)
513 bt_gatt_callbacks->client->search_complete_cb(ev->conn_id,
517 static void __bt_handle_gatt_client_search_char_result(void *buf, uint16_t len)
519 struct hal_ev_gatt_client_char_search_result *ev = buf;
520 btgatt_srvc_id_t gatt_srvc_id;
521 btgatt_gatt_id_t gatt_char_id;
523 gatt_srvc_id.is_primary = ev->is_primary;
524 gatt_srvc_id.id.inst_id = ev->inst_id;
525 memcpy(gatt_srvc_id.id.uuid.uu, ev->svc_uuid, 16);
527 if (BT_STATUS_SUCCESS == ev->status) {
528 memcpy(gatt_char_id.uuid.uu, ev->char_uuid, 16);
529 gatt_char_id.inst_id = ev->inst_id;
532 if (bt_gatt_callbacks->client->get_characteristic_cb)
533 bt_gatt_callbacks->client->get_characteristic_cb(ev->conn_id,
534 ev->status, &gatt_srvc_id, &gatt_char_id, ev->char_prop);
537 static void __bt_handle_gatt_client_search_desc_result(void *buf, uint16_t len)
539 struct hal_ev_gatt_client_desc_search_result *ev = buf;
540 btgatt_srvc_id_t gatt_srvc_id;
541 btgatt_gatt_id_t gatt_char_id;
542 btgatt_gatt_id_t gatt_desc_id;
545 gatt_srvc_id.is_primary = ev->is_primary;
546 gatt_srvc_id.id.inst_id = ev->inst_id;
547 memcpy(gatt_srvc_id.id.uuid.uu, ev->svc_uuid, 16);
549 memcpy(gatt_char_id.uuid.uu, ev->char_uuid, 16);
550 gatt_char_id.inst_id = ev->inst_id;
552 if (BT_STATUS_SUCCESS == ev->status) {
553 memcpy(gatt_desc_id.uuid.uu, ev->desc_uuid, 16);
554 gatt_desc_id.inst_id = ev->inst_id;
557 if (bt_gatt_callbacks->client->get_descriptor_cb)
558 bt_gatt_callbacks->client->get_descriptor_cb(ev->conn_id,
559 ev->status, &gatt_srvc_id, &gatt_char_id, &gatt_desc_id);
562 static void __bt_handle_gatt_client_read_charac(void *buf, uint16_t len)
564 struct hal_ev_gatt_client_read_data *ev = buf;
565 btgatt_read_params_t char_read_parm;
567 char_read_parm.srvc_id.is_primary = ev->is_primary;
568 char_read_parm.srvc_id.id.inst_id = ev->inst_id;
569 memcpy(char_read_parm.srvc_id.id.uuid.uu, ev->svc_uuid, 16);
571 char_read_parm.char_id.inst_id = ev->inst_id;
572 memcpy(char_read_parm.char_id.uuid.uu, ev->char_uuid, 16);
574 char_read_parm.value.len = ev->len;
576 memcpy(char_read_parm.value.value, ev->value, ev->len);
577 char_read_parm.value_type = BT_HAL_GATTC_READ_VALUE_TYPE_VALUE;
580 if (bt_gatt_callbacks->client->read_characteristic_cb)
581 bt_gatt_callbacks->client->read_characteristic_cb(ev->conn_id,
582 ev->status, &char_read_parm);
585 static void __bt_handle_gatt_client_read_desc(void *buf, uint16_t len)
587 struct hal_ev_gatt_client_read_data *ev = buf;
588 btgatt_read_params_t desc_read_parm;
590 desc_read_parm.srvc_id.is_primary = ev->is_primary;
591 desc_read_parm.srvc_id.id.inst_id = ev->inst_id;
592 memcpy(desc_read_parm.srvc_id.id.uuid.uu, ev->svc_uuid, 16);
594 desc_read_parm.char_id.inst_id = ev->inst_id;
595 memcpy(desc_read_parm.char_id.uuid.uu, ev->char_uuid, 16);
597 desc_read_parm.descr_id.inst_id = ev->inst_id;
598 memcpy(desc_read_parm.descr_id.uuid.uu, ev->desc_uuid, 16);
600 desc_read_parm.value.len = ev->len;
602 memcpy(desc_read_parm.value.value, ev->value, ev->len);
603 desc_read_parm.value_type = BT_HAL_GATTC_READ_VALUE_TYPE_VALUE;
606 if (bt_gatt_callbacks->client->read_descriptor_cb)
607 bt_gatt_callbacks->client->read_descriptor_cb(ev->conn_id,
608 ev->status, &desc_read_parm);
611 static void __bt_handle_gatt_client_write_char(void *buf, uint16_t len)
613 struct hal_ev_gatt_client_write_result *ev = buf;
614 btgatt_write_params_t char_write_parm;
616 char_write_parm.srvc_id.is_primary = ev->is_primary;
617 char_write_parm.srvc_id.id.inst_id = ev->inst_id;
618 memcpy(char_write_parm.srvc_id.id.uuid.uu, ev->svc_uuid, 16);
620 char_write_parm.char_id.inst_id = ev->inst_id;
621 memcpy(char_write_parm.char_id.uuid.uu, ev->char_uuid, 16);
623 if (bt_gatt_callbacks->client->write_characteristic_cb)
624 bt_gatt_callbacks->client->write_characteristic_cb(ev->conn_id,
625 ev->status, &char_write_parm);
628 static void __bt_handle_gatt_client_write_desc(void *buf, uint16_t len)
630 struct hal_ev_gatt_client_write_result *ev = buf;
631 btgatt_write_params_t desc_write_parm;
633 desc_write_parm.srvc_id.is_primary = ev->is_primary;
634 desc_write_parm.srvc_id.id.inst_id = ev->inst_id;
635 memcpy(desc_write_parm.srvc_id.id.uuid.uu, ev->svc_uuid, 16);
637 desc_write_parm.char_id.inst_id = ev->inst_id;
638 memcpy(desc_write_parm.char_id.uuid.uu, ev->char_uuid, 16);
640 desc_write_parm.descr_id.inst_id = ev->inst_id;
641 memcpy(desc_write_parm.descr_id.uuid.uu, ev->desc_uuid, 16);
643 if (bt_gatt_callbacks->client->write_descriptor_cb)
644 bt_gatt_callbacks->client->write_descriptor_cb(ev->conn_id,
645 ev->status, &desc_write_parm);
648 static void __bt_handle_gatt_client_watch_notification(void *buf, uint16_t len)
650 struct hal_ev_gatt_client_watch_notification *ev = buf;
651 btgatt_srvc_id_t gatt_srvc_id;
652 btgatt_gatt_id_t gatt_char_id;
654 gatt_srvc_id.is_primary = ev->is_primary;
655 gatt_srvc_id.id.inst_id = ev->inst_id;
656 memcpy(gatt_srvc_id.id.uuid.uu, ev->svc_uuid, 16);
658 memcpy(gatt_char_id.uuid.uu, ev->char_uuid, 16);
659 gatt_char_id.inst_id = ev->inst_id;
661 if (bt_gatt_callbacks->client->register_for_notification_cb)
662 bt_gatt_callbacks->client->register_for_notification_cb(ev->conn_id,
663 ev->registered, ev->status, &gatt_srvc_id, &gatt_char_id);
666 static void __bt_handle_gatt_client_changed_value(void *buf, uint16_t len)
668 struct hal_ev_gatt_client_notify_changed_value *ev = buf;
669 btgatt_notify_params_t changd_value_parm;
671 changd_value_parm.srvc_id.is_primary = ev->is_primary;
672 changd_value_parm.srvc_id.id.inst_id = ev->inst_id;
673 memcpy(changd_value_parm.srvc_id.id.uuid.uu, ev->svc_uuid, 16);
675 changd_value_parm.char_id.inst_id = ev->inst_id;
676 memcpy(changd_value_parm.char_id.uuid.uu, ev->char_uuid, 16);
677 changd_value_parm.is_notify = ev->is_notify;
679 memcpy(changd_value_parm.bda.address, ev->bdaddr, 6);
681 changd_value_parm.len = ev->len;
683 memcpy(changd_value_parm.value, ev->value, ev->len);
685 if (bt_gatt_callbacks->client->notify_cb)
686 bt_gatt_callbacks->client->notify_cb(ev->conn_id, &changd_value_parm);
689 static void __bt_hal_handle_gatt_client_service_changed(void *buf, uint16_t len)
691 struct hal_ev_gatt_client_service_changed *ev = buf;
693 DBG("GATT Client service changed event received");
695 if (bt_gatt_callbacks->client->service_changed_cb)
696 bt_gatt_callbacks->client->service_changed_cb((bt_bdaddr_t *)ev->bdaddr,
697 ev->change_type, ev->uuid, ev->conn_id, ev->inst_id);
700 static bt_hal_le_adv_info_t *__bt_hal_get_adv_ind_info(char *addr)
707 for (l = adv_ind_list; NULL != l; l = g_slist_next(l)) {
708 bt_hal_le_adv_info_t *adv_info = l->data;
713 if (!g_strcmp0(adv_info->addr, addr))
720 static int __bt_hal_add_adv_ind_info(bt_hal_le_adv_info_t *adv_info)
725 if (__bt_hal_get_adv_ind_info(adv_info->addr) != NULL) {
726 DBG("Adv info already present");
730 adv_ind_list = g_slist_append(adv_ind_list, adv_info);
734 static void __bt_hal_free_adv_info_list(gpointer data)
736 bt_hal_le_adv_info_t *adv_info = data;
738 if (adv_info->timer_id)
739 g_source_remove(adv_info->timer_id);
740 adv_info->timer_id = 0;
745 void bt_hal_release_pending_adv_ind_list(void)
747 g_slist_free_full(adv_ind_list, __bt_hal_free_adv_info_list);
751 static gboolean __bt_hal_adv_scan_req_timeout_cb(gpointer user_data)
753 bt_hal_le_adv_info_t *adv_info = user_data;
755 __bt_hal_send_le_scan_result_event(adv_info);
756 adv_ind_list = g_slist_remove(adv_ind_list, adv_info);
761 static void __bt_hal_handle_gatt_client_scan_result(void *buf, uint16_t len)
765 struct hal_ev_gatt_client_scan_result *ev = buf;
766 bt_hal_le_adv_info_t *adv_info;
769 memcpy(bd_addr.address, ev->bd_addr, 6);
770 bt_bdaddr_t2str(&bd_addr, address);
771 data_len = (ev->len < BT_HAL_ADV_DATA_MAX_SIZE) ? ev->len : BT_HAL_ADV_DATA_MAX_SIZE;
772 if (data_len == 0 && ev->adv_type != BT_LE_ADV_SCAN_RSP) {
773 ERR("Unexpected: Data len is 0");
777 if (ev->adv_type == BT_LE_ADV_SCAN_RSP) { /* SCAN_RSP */
778 adv_info = __bt_hal_get_adv_ind_info(address);
780 /* Copy scan response data in data field and send event */
781 adv_info->scan_rsp_data_len = data_len;
782 memcpy(adv_info->scan_rsp_data, ev->adv_data, data_len);
783 __bt_hal_send_le_scan_result_event(adv_info);
784 adv_ind_list = g_slist_remove(adv_ind_list, adv_info);
785 if (adv_info->timer_id)
786 g_source_remove(adv_info->timer_id);
793 adv_info = g_malloc0(sizeof(bt_hal_le_adv_info_t));
795 ERR("Not enough memory");
800 g_strlcpy(adv_info->addr, address, 18);
801 adv_info->addr_type = ev->addr_type;
802 adv_info->adv_type = ev->adv_type;
803 adv_info->rssi = ev->rssi;
804 adv_info->adv_data_len = data_len;
805 memcpy(adv_info->adv_data, ev->adv_data, data_len);
808 if (_bt_hal_gatt_client_get_le_scan_type() == BT_GATTC_LE_SCAN_TYPE_PASSIVE) {
809 __bt_hal_send_le_scan_result_event(adv_info);
815 if (adv_info->adv_type == BT_LE_ADV_DIRECT_IND ||
816 adv_info->adv_type == BT_LE_ADV_NONCONN_IND) {
817 __bt_hal_send_le_scan_result_event(adv_info);
818 } else if (adv_info->adv_type == BT_LE_ADV_IND ||
819 adv_info->adv_type == BT_LE_ADV_SCAN_IND) {
821 if (__bt_hal_add_adv_ind_info(adv_info) == 0)
822 adv_info->timer_id = g_timeout_add(1000,
823 __bt_hal_adv_scan_req_timeout_cb,
828 static void __bt_hal_handle_gatt_client_mtu_exchange_completed(void *buf, uint16_t len)
830 struct hal_ev_gatt_client_mtu_exchange_completed *ev = buf;
832 if (bt_gatt_callbacks->client->configure_mtu_cb)
833 bt_gatt_callbacks->client->configure_mtu_cb(ev->conn_id, ev->status, ev->mtu);
837 /*******************************************************************************
839 ** Function gatt_init
841 ** Description Initializes the GATT interface
843 ** Returns bt_status_t
845 *******************************************************************************/
846 static bt_status_t gatt_init(const btgatt_callbacks_t* callbacks)
848 bt_gatt_callbacks = callbacks;
850 DBG("Register A2DP Src events callback function");
851 _bt_hal_register_gatt_le_dbus_handler_cb(__bt_hal_gatt_events);
852 _bt_hal_register_gatt_server_handler_cb(__bt_hal_gatt_events);
853 _bt_hal_register_gatt_client_handler_cb(__bt_hal_gatt_events);
854 _bt_hal_register_event_handler_cb(HAL_GATT, __bt_hal_gatt_events);
856 _bt_hal_gatt_server_init();
857 _bt_hal_gatt_client_init();
859 return BT_STATUS_SUCCESS;
862 /*******************************************************************************
864 ** Function gatt_cleanup
866 ** Description Closes the GATT interface
870 *******************************************************************************/
871 static void gatt_cleanup(void)
873 _bt_hal_unregister_gatt_le_dbus_handler_cb();
874 _bt_hal_unregister_gatt_client_handler_cb();
875 _bt_hal_unregister_gatt_server_handler_cb();
876 _bt_hal_unregister_event_handler_cb(HAL_GATT);
878 if (bt_gatt_callbacks)
879 bt_gatt_callbacks = NULL;
882 static const btgatt_interface_t btgatt_interface = {
883 sizeof(btgatt_interface),
888 &btgatt_client_interface,
889 &btgatt_server_interface,
892 /*******************************************************************************
894 ** Function bt_get_gatt_interface
896 ** Description Get the gatt callback interface
898 ** Returns btgatt_interface_t
900 *******************************************************************************/
901 const btgatt_interface_t *bt_get_gatt_interface()
903 return &btgatt_interface;