0ec22533d1331ad252ae85d47d260a74faa7c608
[platform/core/connectivity/bluetooth-frwk.git] / bt-oal / bluez_hal / src / bt-hal-gatt.c
1 /*
2  * BLUETOOOTH HAL
3  *
4  * Copyright (c) 2015 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Anupam Roy <anupam.r@samsung.com>
7  *
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:
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  *
20  */
21
22
23 /*******************************************************************************
24  *
25  * Description: GATT Profile Bluetooth Interface
26  *
27  *******************************************************************************/
28
29 #include <hardware/bluetooth.h>
30 #include <hardware/bt_gatt.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <errno.h>
34 #include <string.h>
35 #include <dlog.h>
36
37 #include "bt-hal.h"
38 #include "bt-hal-log.h"
39 #include "bt-hal-msg.h"
40 #include "bt-hal-utils.h"
41
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"
46
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
53
54 #define BT_HAL_ADV_DATA_MAX_SIZE        31
55 #define BT_HAL_GATTC_READ_VALUE_TYPE_VALUE          0x0000  /* Attribute value itself */
56 typedef struct {
57         char addr[18];
58         uint8_t addr_type;
59         uint8_t adv_type;
60         int rssi;
61         uint8_t adv_data[BT_HAL_ADV_DATA_MAX_SIZE];
62         int adv_data_len;
63         uint8_t scan_rsp_data[BT_HAL_ADV_DATA_MAX_SIZE];
64         int scan_rsp_data_len;
65         guint timer_id;
66 } bt_hal_le_adv_info_t;
67
68 /****************************************** Global Variables ******************************************/
69 static const btgatt_callbacks_t *bt_gatt_callbacks = NULL;
70
71 extern btgatt_client_interface_t btgatt_client_interface;
72 extern btgatt_server_interface_t btgatt_server_interface;
73
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 /*****************************************************************************************************/
93
94 bool _bt_hal_gatt_interface_ready(void)
95 {
96         return bt_gatt_callbacks != NULL;
97 }
98
99 static void __bt_hal_handle_server_instance_initialized(void *buf, uint16_t len)
100 {
101         struct hal_ev_server_instance_registered *ev = buf;
102
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);
105 }
106
107 static void __bt_hal_handle_multi_adv_data_set(void *buf, uint16_t len)
108 {
109         struct hal_ev_multi_adv_data_set *ev = buf;
110
111         if (bt_gatt_callbacks->server->multi_adv_data_cb)
112                 bt_gatt_callbacks->server->multi_adv_data_cb(ev->server_instance, ev->status);
113 }
114
115 static void __bt_hal_handle_multi_adv_data_update(void *buf, uint16_t len)
116 {
117         struct hal_ev_multi_adv_update *ev = buf;
118
119         if (bt_gatt_callbacks->server->multi_adv_update_cb)
120                 bt_gatt_callbacks->server->multi_adv_update_cb(ev->server_instance, ev->status);
121 }
122
123 static void __bt_hal_handle_multi_adv_enable(void *buf, uint16_t len)
124 {
125         struct hal_ev_multi_adv_enable *ev = buf;
126
127         if (bt_gatt_callbacks->server->multi_adv_enable_cb)
128                 bt_gatt_callbacks->server->multi_adv_enable_cb(ev->server_instance, ev->status);
129 }
130
131 static void __bt_hal_handle_multi_adv_disable(void *buf, uint16_t len)
132 {
133         struct hal_ev_multi_adv_disable *ev = buf;
134
135         if (bt_gatt_callbacks->server->multi_adv_disable_cb)
136                 bt_gatt_callbacks->server->multi_adv_disable_cb(ev->server_instance, ev->status);
137 }
138
139 /* Same callback for both Enable and DIsable */
140 static void __bt_hal_handle_legacy_adv_status(void *buf, uint16_t len)
141 {
142         struct hal_ev_legacy_adv_status *ev = buf;
143
144         if (bt_gatt_callbacks->server->listen_cb)
145                 bt_gatt_callbacks->server->listen_cb(ev->status, ev->server_instance);
146 }
147
148 static void __bt_hal_handle_service_added(void *buf, uint16_t len)
149 {
150         struct hal_ev_gatt_service_added *ev = buf;
151         btgatt_srvc_id_t srvc_id;
152
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));
157
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);
161 }
162
163 static void __bt_hal_handle_char_added(void *buf, uint16_t len)
164 {
165         struct hal_ev_gatt_char_added *ev = buf;
166         bt_uuid_t uuid;
167
168         memcpy(uuid.uu, ev->char_uuid, sizeof(ev->char_uuid));
169
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);
173 }
174
175 static void __bt_hal_handle_desc_added(void *buf, uint16_t len)
176 {
177         struct hal_ev_gatt_desc_added *ev = buf;
178         bt_uuid_t uuid;
179
180         memcpy(uuid.uu, ev->desc_uuid, sizeof(ev->desc_uuid));
181
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);
185 }
186
187 static void __bt_hal_handle_service_started(void *buf, uint16_t len)
188 {
189         struct hal_ev_gatt_service_started *ev = buf;
190
191         if (bt_gatt_callbacks->server->service_started_cb)
192                 bt_gatt_callbacks->server->service_started_cb(ev->status, ev->server_instance,
193                                 ev->service_handle);
194 }
195
196 static void __bt_hal_handle_service_deleted(void *buf, uint16_t len)
197 {
198         struct hal_ev_gatt_service_deleted *ev = buf;
199
200         if (bt_gatt_callbacks->server->service_deleted_cb)
201                 bt_gatt_callbacks->server->service_deleted_cb(ev->status, ev->server_instance,
202                                 ev->service_handle);
203 }
204
205 static void __bt_hal_handle_gatt_server_connected(void *buf, uint16_t len)
206 {
207         struct hal_ev_gatt_server_connected *ev = buf;
208         bt_bdaddr_t bd_addr;
209
210         memcpy(bd_addr.address, ev->bdaddr, 6);
211
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);
215 }
216
217 static void __bt_hal_handle_gatt_server_notification_changed(void *buf, uint16_t len)
218 {
219         struct hal_ev_gatt_server_notifcation_change *ev = buf;
220         bt_bdaddr_t bd_addr;
221
222         memcpy(bd_addr.address, ev->bdaddr, 6);
223
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);
227 }
228
229 static void __bt_hal_handle_gatt_server_read_requested(void *buf, uint16_t len)
230 {
231         struct hal_ev_gatt_server_read_req *ev = buf;
232         bt_bdaddr_t bd_addr;
233
234         memcpy(bd_addr.address, ev->bdaddr, 6);
235
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);
239 }
240
241 static void __bt_hal_handle_gatt_server_write_requested(void *buf, uint16_t len)
242 {
243         struct hal_ev_gatt_server_write_req *ev = buf;
244
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);
251 }
252
253 static void __bt_hal_handle_gatt_server_acquire_write_requested(void *buf, uint16_t len)
254 {
255         struct hal_ev_gatt_server_acquire_write_res *ev = buf;
256
257         DBG("acquire write requested event recived");
258
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);
261 }
262
263 static void __bt_hal_handle_gatt_server_acquire_notify_requested(void *buf, uint16_t len)
264 {
265         struct hal_ev_gatt_server_acquire_notify *ev = buf;
266
267         DBG("acquire notify  requested event recived");
268
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, (bt_bdaddr_t *)ev->bdaddr);
271 }
272
273 static void __bt_hal_handle_gatt_server_indicate_confirmed(void *buf, uint16_t len)
274 {
275         struct hal_ev_gatt_server_indicate_cfm *ev = buf;
276
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);
280
281 }
282
283 static void __bt_hal_handle_gatt_server_mtu_changed(void *buf, uint16_t len)
284 {
285         struct hal_ev_gatt_server_mtu_changed *ev = buf;
286
287         DBG("GATT Server MTU changed event recived");
288
289         if (bt_gatt_callbacks->server->mtu_changed_cb)
290                 bt_gatt_callbacks->server->mtu_changed_cb(ev->conn_id, ev->mtu);
291 }
292
293 static void __bt_hal_gatt_events(int message, void *buf, uint16_t len)
294 {
295         DBG("GATT event 0x%x", message);
296         /* Check if GATT interface is Ready */
297         if (!_bt_hal_gatt_interface_ready())
298                 return;
299
300         switch (message) {
301         case HAL_EV_SERVER_INSTANCE_INITIALIZED: {
302                 __bt_hal_handle_server_instance_initialized(buf, len);
303                 break;
304         }
305         case HAL_EV_MULTI_ADV_ENABLE: {
306                 __bt_hal_handle_multi_adv_enable(buf, len);
307                 break;
308         }
309         case HAL_EV_MULTI_ADV_DISABLE: {
310                 __bt_hal_handle_multi_adv_disable(buf, len);
311                 break;
312         }
313         case HAL_EV_MULTI_ADV_DATA_SET: {
314                 __bt_hal_handle_multi_adv_data_set(buf, len);
315                 break;
316         }
317         case HAL_EV_MULTI_ADV_UPDATE: {
318                 __bt_hal_handle_multi_adv_data_update(buf, len);
319                 break;
320         }
321         case HAL_EV_LEGACY_ADV_ENABLE: {
322                 __bt_hal_handle_legacy_adv_status(buf, len);
323                 break;
324         }
325         case HAL_EV_GATT_CLIENT_REGISTERED: {
326                 __bt_handle_gatt_client_registered(buf, len);
327                 break;
328         }
329         case HAL_EV_GATT_CLIENT_SCAN_RESULT: {
330                 __bt_hal_handle_gatt_client_scan_result(buf, len);
331                 break;
332         }
333         case HAL_EV_GATT_SERVICE_ADDED: {
334                 __bt_hal_handle_service_added(buf, len);
335                 break;
336         }
337         case HAL_EV_GATT_CHAR_ADDED: {
338                 __bt_hal_handle_char_added(buf, len);
339                 break;
340         }
341         case HAL_EV_GATT_DESC_ADDED: {
342                 __bt_hal_handle_desc_added(buf, len);
343                 break;
344         }
345         case HAL_EV_GATT_SERVICE_STARTED: {
346                 __bt_hal_handle_service_started(buf, len);
347                 break;
348         }
349         case HAL_EV_GATT_SERVICE_DELETED: {
350                 __bt_hal_handle_service_deleted(buf, len);
351                 break;
352         }
353         case HAL_EV_GATT_SERVER_CONNECTED: {
354                 __bt_hal_handle_gatt_server_connected(buf, len);
355                 break;
356         }
357         case HAL_EV_GATT_NOTIFICATION_CHANGE: {
358                 __bt_hal_handle_gatt_server_notification_changed(buf, len);
359                 break;
360         }
361         case HAL_EV_GATT_READ_REQUESTED: {
362                 __bt_hal_handle_gatt_server_read_requested(buf, len);
363                 break;
364         }
365         case HAL_EV_GATT_WRITE_REQUESTED: {
366                 __bt_hal_handle_gatt_server_write_requested(buf, len);
367                 break;
368         }
369         case HAL_EV_GATT_INDICATE_CFM: {
370                 __bt_hal_handle_gatt_server_indicate_confirmed(buf, len);
371                 break;
372         }
373         case HAL_EV_GATT_CLIENT_CONNECTED: {
374                 __bt_handle_gatt_client_connected(buf, len);
375                 break;
376         }
377         case HAL_EV_GATT_CLIENT_DISCONNECTED: {
378                 __bt_handle_gatt_client_disconnected(buf, len);
379                 break;
380         }
381         case HAL_EV_GATT_CLIENT_SEARCH_RESULT: {
382                 __bt_handle_gatt_client_search_result(buf, len);
383                 break;
384         }
385         case HAL_EV_GATT_CLIENT_SEARCH_COMPLETE: {
386                 __bt_handle_gatt_client_search_complete(buf, len);
387                 break;
388         }
389         case HAL_EV_GATT_CLIENT_CHARAC_SEARCH_RESULT: {
390                 __bt_handle_gatt_client_search_char_result(buf, len);
391                 break;
392         }
393         case HAL_EV_GATT_CLIENT_DESC_SEARCH_RESULT: {
394                 __bt_handle_gatt_client_search_desc_result(buf, len);
395                 break;
396         }
397         case HAL_EV_GATT_CLIENT_READ_CHARAC: {
398                 __bt_handle_gatt_client_read_charac(buf, len);
399                 break;
400         }
401         case HAL_EV_GATT_CLIENT_READ_DESC: {
402                 __bt_handle_gatt_client_read_desc(buf, len);
403                 break;
404         }
405         case HAL_EV_GATT_CLIENT_WRITE_CHARAC: {
406                 __bt_handle_gatt_client_write_char(buf, len);
407                 break;
408         }
409         case HAL_EV_GATT_CLIENT_WRITE_DESC: {
410                 __bt_handle_gatt_client_write_desc(buf, len);
411                 break;
412         }
413         case HAL_EV_GATT_CLIENT_WATCH_NOTIFICATION: {
414                 __bt_handle_gatt_client_watch_notification(buf, len);
415                 break;
416         }
417         case HAL_EV_GATT_CLIENT_NOTIFY_CHANGED_VALUE: {
418                 __bt_handle_gatt_client_changed_value(buf, len);
419                 break;
420         }
421         case HAL_EV_GATT_CLIENT_SERVICE_CHANGED: {
422                 __bt_hal_handle_gatt_client_service_changed(buf, len);
423                 break;
424         }
425         case HAL_EV_GATT_SERVER_ACQUIRE_WRITE_RES:{
426                 __bt_hal_handle_gatt_server_acquire_write_requested(buf, len);
427                 break;
428         }
429         case HAL_EV_GATT_SERVER_ACQUIRE_NOTIFY_RES:{
430                 __bt_hal_handle_gatt_server_acquire_notify_requested(buf, len);
431                 break;
432         }
433         case HAL_EV_GATT_SERVER_MTU_CHANGED:{
434                 __bt_hal_handle_gatt_server_mtu_changed(buf, len);
435                 break;
436         }
437
438         case HAL_EV_GATT_CLIENT_MTU_EXCHANGE_COMPLETED:{
439                 __bt_hal_handle_gatt_client_mtu_exchange_completed(buf, len);
440                 break;
441         }
442         default:
443                 DBG("Event Currently not handled!!");
444                 break;
445         }
446 }
447
448 /************************************* GATT CLIENT EVENTS ****************************************/
449 static void __bt_handle_gatt_client_registered(void *buf, uint16_t len)
450 {
451         struct hal_ev_gatt_client_registered *ev = buf;
452         bt_uuid_t uuid;
453
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);
458 }
459
460 static void __bt_hal_send_le_scan_result_event(bt_hal_le_adv_info_t *adv_info)
461 {
462         bt_bdaddr_t bd_addr;
463
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);
470 }
471
472 static void __bt_handle_gatt_client_connected(void *buf, uint16_t len)
473 {
474         struct hal_ev_gatt_client_connected  *ev = buf;
475         bt_bdaddr_t bd_addr;
476
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);
481 }
482
483 static void __bt_handle_gatt_client_disconnected(void *buf, uint16_t len)
484 {
485         struct hal_ev_gatt_client_connected  *ev = buf;
486         bt_bdaddr_t bd_addr;
487
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);
492 }
493
494 static void __bt_handle_gatt_client_search_result(void *buf, uint16_t len)
495 {
496         struct hal_ev_gatt_client_search_result  *ev = buf;
497         btgatt_srvc_id_t gatt_srvc_id;
498
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);
502
503         if (bt_gatt_callbacks->client->search_result_cb)
504                 bt_gatt_callbacks->client->search_result_cb(ev->conn_id,
505                                                         &gatt_srvc_id);
506 }
507
508 static void __bt_handle_gatt_client_search_complete(void *buf, uint16_t len)
509 {
510         struct hal_ev_gatt_client_search_complete  *ev = buf;
511
512         if (bt_gatt_callbacks->client->search_complete_cb)
513                 bt_gatt_callbacks->client->search_complete_cb(ev->conn_id,
514                                                                 ev->status);
515 }
516
517 static void __bt_handle_gatt_client_search_char_result(void *buf, uint16_t len)
518 {
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;
522
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);
526
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;
530         }
531
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);
535 }
536
537 static void __bt_handle_gatt_client_search_desc_result(void *buf, uint16_t len)
538 {
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;
543
544
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);
548
549         memcpy(gatt_char_id.uuid.uu, ev->char_uuid, 16);
550         gatt_char_id.inst_id = ev->inst_id;
551
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;
555         }
556
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);
560 }
561
562 static void __bt_handle_gatt_client_read_charac(void *buf, uint16_t len)
563 {
564         struct hal_ev_gatt_client_read_data *ev = buf;
565         btgatt_read_params_t char_read_parm;
566
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);
570
571         char_read_parm.char_id.inst_id = ev->inst_id;
572         memcpy(char_read_parm.char_id.uuid.uu, ev->char_uuid, 16);
573
574         char_read_parm.value.len = ev->len;
575         if (ev->len > 0) {
576                 memcpy(char_read_parm.value.value, ev->value, ev->len);
577                 char_read_parm.value_type = BT_HAL_GATTC_READ_VALUE_TYPE_VALUE;
578         }
579
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);
583 }
584
585 static void __bt_handle_gatt_client_read_desc(void *buf, uint16_t len)
586 {
587         struct hal_ev_gatt_client_read_data *ev = buf;
588         btgatt_read_params_t desc_read_parm;
589
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);
593
594         desc_read_parm.char_id.inst_id = ev->inst_id;
595         memcpy(desc_read_parm.char_id.uuid.uu, ev->char_uuid, 16);
596
597         desc_read_parm.descr_id.inst_id = ev->inst_id;
598         memcpy(desc_read_parm.descr_id.uuid.uu, ev->desc_uuid, 16);
599
600         desc_read_parm.value.len = ev->len;
601         if (ev->len > 0) {
602                 memcpy(desc_read_parm.value.value, ev->value, ev->len);
603                 desc_read_parm.value_type = BT_HAL_GATTC_READ_VALUE_TYPE_VALUE;
604         }
605
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);
609 }
610
611 static void __bt_handle_gatt_client_write_char(void *buf, uint16_t len)
612 {
613         struct hal_ev_gatt_client_write_result *ev = buf;
614         btgatt_write_params_t char_write_parm;
615
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);
619
620         char_write_parm.char_id.inst_id = ev->inst_id;
621         memcpy(char_write_parm.char_id.uuid.uu, ev->char_uuid, 16);
622
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);
626 }
627
628 static void __bt_handle_gatt_client_write_desc(void *buf, uint16_t len)
629 {
630         struct hal_ev_gatt_client_write_result *ev = buf;
631         btgatt_write_params_t desc_write_parm;
632
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);
636
637         desc_write_parm.char_id.inst_id = ev->inst_id;
638         memcpy(desc_write_parm.char_id.uuid.uu, ev->char_uuid, 16);
639
640         desc_write_parm.descr_id.inst_id = ev->inst_id;
641         memcpy(desc_write_parm.descr_id.uuid.uu, ev->desc_uuid, 16);
642
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);
646 }
647
648 static void __bt_handle_gatt_client_watch_notification(void *buf, uint16_t len)
649 {
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;
653
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);
657
658         memcpy(gatt_char_id.uuid.uu, ev->char_uuid, 16);
659         gatt_char_id.inst_id = ev->inst_id;
660
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);
664 }
665
666 static void __bt_handle_gatt_client_changed_value(void *buf, uint16_t len)
667 {
668         struct hal_ev_gatt_client_notify_changed_value *ev = buf;
669         btgatt_notify_params_t changd_value_parm;
670
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);
674
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;
678
679         memcpy(changd_value_parm.bda.address, ev->bdaddr, 6);
680
681         changd_value_parm.len = ev->len;
682         if (ev->len > 0)
683                 memcpy(changd_value_parm.value, ev->value, ev->len);
684
685         if (bt_gatt_callbacks->client->notify_cb)
686                 bt_gatt_callbacks->client->notify_cb(ev->conn_id, &changd_value_parm);
687 }
688
689 static void __bt_hal_handle_gatt_client_service_changed(void *buf, uint16_t len)
690 {
691         struct hal_ev_gatt_client_service_changed *ev = buf;
692
693         DBG("GATT Client service changed event received");
694
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);
698 }
699
700 static bt_hal_le_adv_info_t *__bt_hal_get_adv_ind_info(char *addr)
701 {
702         GSList *l;
703
704         if (!addr)
705                 return NULL;
706
707         for (l = adv_ind_list; NULL != l; l = g_slist_next(l)) {
708                 bt_hal_le_adv_info_t *adv_info = l->data;
709
710                 if (!adv_info)
711                         continue;
712
713                 if (!g_strcmp0(adv_info->addr, addr))
714                         return adv_info;
715         }
716
717         return NULL;
718 }
719
720 static int __bt_hal_add_adv_ind_info(bt_hal_le_adv_info_t *adv_info)
721 {
722         if (!adv_info)
723                 return -1;
724
725         if (__bt_hal_get_adv_ind_info(adv_info->addr) != NULL) {
726                 DBG("Adv info already present");
727                 return -1;
728         }
729
730         adv_ind_list = g_slist_append(adv_ind_list, adv_info);
731         return 0;
732 }
733
734 static void __bt_hal_free_adv_info_list(gpointer data)
735 {
736         bt_hal_le_adv_info_t *adv_info = data;
737
738         if (adv_info->timer_id)
739                 g_source_remove(adv_info->timer_id);
740         adv_info->timer_id = 0;
741
742         g_free(adv_info);
743 }
744
745 void bt_hal_release_pending_adv_ind_list(void)
746 {
747         g_slist_free_full(adv_ind_list, __bt_hal_free_adv_info_list);
748         adv_ind_list = NULL;
749 }
750
751 static gboolean __bt_hal_adv_scan_req_timeout_cb(gpointer user_data)
752 {
753         bt_hal_le_adv_info_t *adv_info = user_data;
754
755         __bt_hal_send_le_scan_result_event(adv_info);
756         adv_ind_list = g_slist_remove(adv_ind_list, adv_info);
757         g_free(adv_info);
758         return FALSE;
759 }
760
761 static void __bt_hal_handle_gatt_client_scan_result(void *buf, uint16_t len)
762 {
763         bt_bdaddr_t bd_addr;
764         char address[18];
765         struct hal_ev_gatt_client_scan_result *ev = buf;
766         bt_hal_le_adv_info_t *adv_info;
767         int data_len = 0;
768
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");
774                 return;
775         }
776
777         if (ev->adv_type == BT_LE_ADV_SCAN_RSP) { /* SCAN_RSP */
778                 adv_info = __bt_hal_get_adv_ind_info(address);
779                 if (adv_info) {
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);
787                         g_free(adv_info);
788                 }
789                 return;
790         }
791
792         /* ADV_IND */
793         adv_info = g_malloc0(sizeof(bt_hal_le_adv_info_t));
794         if (!adv_info) {
795                 ERR("Not enough memory");
796                 g_free(adv_info);
797                 return;
798         }
799
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);
806
807 #ifdef TIZEN_BT_HAL
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);
810                 g_free(adv_info);
811                 return;
812         }
813 #endif
814
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                 g_free(adv_info);
819         } else if (adv_info->adv_type == BT_LE_ADV_IND ||
820                 adv_info->adv_type == BT_LE_ADV_SCAN_IND) {
821
822                 if (__bt_hal_add_adv_ind_info(adv_info) == 0)
823                         adv_info->timer_id = g_timeout_add(1000,
824                                         __bt_hal_adv_scan_req_timeout_cb,
825                                         (void *)adv_info);
826         }
827 }
828
829 static void __bt_hal_handle_gatt_client_mtu_exchange_completed(void *buf, uint16_t len)
830 {
831         struct hal_ev_gatt_client_mtu_exchange_completed  *ev = buf;
832
833         if (bt_gatt_callbacks->client->configure_mtu_cb)
834                 bt_gatt_callbacks->client->configure_mtu_cb(ev->conn_id, ev->status, ev->mtu);
835
836 }
837
838 /*******************************************************************************
839  **
840  ** Function            gatt_init
841  **
842  ** Description         Initializes the GATT interface
843  **
844  ** Returns             bt_status_t
845  **
846  *******************************************************************************/
847 static bt_status_t gatt_init(const btgatt_callbacks_t* callbacks)
848 {
849         bt_gatt_callbacks = callbacks;
850
851         DBG("Register A2DP Src events callback function");
852         _bt_hal_register_gatt_le_dbus_handler_cb(__bt_hal_gatt_events);
853         _bt_hal_register_gatt_server_handler_cb(__bt_hal_gatt_events);
854         _bt_hal_register_gatt_client_handler_cb(__bt_hal_gatt_events);
855         _bt_hal_register_event_handler_cb(HAL_GATT, __bt_hal_gatt_events);
856
857         _bt_hal_gatt_server_init();
858         _bt_hal_gatt_client_init();
859
860         return BT_STATUS_SUCCESS;
861 }
862
863 /*******************************************************************************
864  **
865  ** Function    gatt_cleanup
866  **
867  ** Description Closes the GATT interface
868  **
869  ** Returns     void
870  **
871  *******************************************************************************/
872 static void gatt_cleanup(void)
873 {
874         _bt_hal_unregister_gatt_le_dbus_handler_cb();
875         _bt_hal_unregister_gatt_client_handler_cb();
876         _bt_hal_unregister_gatt_server_handler_cb();
877         _bt_hal_unregister_event_handler_cb(HAL_GATT);
878
879         if (bt_gatt_callbacks)
880                 bt_gatt_callbacks = NULL;
881 }
882
883 static const btgatt_interface_t btgatt_interface = {
884         sizeof(btgatt_interface),
885
886         gatt_init,
887         gatt_cleanup,
888
889         &btgatt_client_interface,
890         &btgatt_server_interface,
891 };
892
893 /*******************************************************************************
894  **
895  ** Function            bt_get_gatt_interface
896  **
897  ** Description         Get the gatt callback interface
898  **
899  ** Returns             btgatt_interface_t
900  **
901  *******************************************************************************/
902 const btgatt_interface_t *bt_get_gatt_interface()
903 {
904         return &btgatt_interface;
905 }