4 * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd.
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 #include "bt-service-common.h"
24 #include "bt-service-core-adapter.h"
25 #include "bt-service-event-receiver.h"
26 #include "bt-request-handler.h"
27 #include "bluetooth-api.h"
29 #include "bluetooth-api.h"
30 #include "bt-internal-types.h"
31 #include "bt-service-util.h"
32 #include "bt-service-common.h"
33 #include "bt-service-event.h"
35 #include "bt-internal-types.h"
36 #include "bt-service-gatt.h"
37 #include "bt-service-gatt.h"
38 #include "bt-service-core-device.h"
39 #include "bt-service-core-adapter-le.h"
40 #ifdef TIZEN_GATT_CLIENT
41 #include "bluetooth-gatt-client-api.h"
44 #include <oal-hardware.h>
45 #include <oal-manager.h>
46 #include <oal-event.h>
47 #include <oal-adapter-mgr.h>
48 #include <oal-device-mgr.h>
51 #define BT_GATT_TRANSPORT_LE 0
52 #define BT_GATT_TRANSPORT_BR_EDR 1
53 #define BT_GATT_TRANSPORT_LE_BR_EDR 2
54 #define BT_UUID_STRING_MAX 64
55 #define BT_SENDER_MAX_LENGTH 50
56 #define MAX_APPS_SUPPORTED 11 /* Slot 0 is not used */
57 #define BT_DEFAULT_ATT_MTU 23
59 #ifdef TIZEN_GATT_CLIENT
66 #define UUID_MAX_LEN 50
69 #define BDADDR_ANY (&(bluetooth_device_address_t) {{0, 0, 0, 0, 0, 0} })
71 #ifdef TIZEN_GATT_CLIENT
72 char uuid_list[NUM_UUID][BT_UUID_STRING_MAX] = {"0000b00b-0000-0000-f065-080080fa49b5", /* Used by BLEAPP */
73 "0000b00b-1111-1111-0123-456789ab0cd2", /* Used by BLEAPP */
74 "0000b00b-2222-1111-0123-456789ab0cd2",
75 "0000b00b-3333-1111-0123-456789ab0cd2",
76 "0000b00b-4444-1111-0123-456789ab0cd2",
77 "0000b00b-5555-1111-0123-456789ab0cd2",
78 "0000b00b-6666-1111-0123-456789ab0cd2",
79 "0000b00b-7777-1111-0123-456789ab0cd2",
80 "0000b00b-8888-1111-0123-456789ab0cd2",
81 "0000b00b-9999-1111-0123-456789ab0cd2",
82 "0000b00b-aaaa-1111-0123-456789ab0cd2",
83 "0000b00b-bbbb-1111-0123-456789ab0cd2",
84 "0000b00b-cccc-1111-0123-456789ab0cd2",
85 "0000b00b-dddd-1111-0123-456789ab0cd2",
86 "0000b00b-eeee-1111-0123-456789ab0cd2",
87 "0000b00b-ffff-1111-0123-456789ab0cd2",
88 "0000b00c-0000-1111-0123-456789ab0cd2",
89 "0000b00c-1111-1111-0123-456789ab0cd2",
90 "0000b00c-2222-1111-0123-456789ab0cd2",
91 "0000b00c-3333-1111-0123-456789ab0cd2"};
93 char uuid_list[NUM_UUID][BT_UUID_STRING_MAX] = {"0000b00b-0000-0000-f065-080080fa49b5", /* Used by BLEAPP */
94 "0000b00b-1111-1111-0123-456789ab0cd2", /* Used by BLEAPP */
95 "0000b00b-2222-1111-0123-456789ab0cd2",
96 "0000b00b-3333-1111-0123-456789ab0cd2",
97 "0000b00b-4444-1111-0123-456789ab0cd2",
98 "0000b00b-5555-1111-0123-456789ab0cd2",
99 "0000b00b-6666-1111-0123-456789ab0cd2",
100 "0000b00b-7777-1111-0123-456789ab0cd2",
101 "0000b00b-8888-1111-0123-456789ab0cd2",
102 "0000b00b-9999-1111-0123-456789ab0cd2"};
105 #ifdef TIZEN_GATT_CLIENT
106 /* Reserved GATT client Instance UUID. This is used only internally by bt-service */
107 #define DEFAULT_GATT_CLIENT_UUID "0000a00a-1111-1111-0123-456789abcdef"
109 static int gatt_default_client = -1;
112 gboolean is_registered;
113 bluetooth_device_address_t addr;
114 unsigned char svc_uuid[BLUETOOTH_UUID_HEX_MAX_LEN];
116 unsigned char char_uuid[BLUETOOTH_UUID_HEX_MAX_LEN];
118 } bt_gatt_notif_reg_info_t;
120 struct gatt_out_conn_info_t {
121 int client_id; /* This value unique identifies a GATT Client instance */
122 char *addr; /* Remote GATT Server address */
125 /* Linked List of outgoing gatt connection list
126 Note: This is valid only for local GATT client */
127 static GSList *outgoing_gatt_conn_list = NULL;
129 /* GATT Server Info(Local Client) List Structure */
130 struct gatt_server_info_t {
131 int connection_id; /* This value will uniquely identify a GATT client-server connection */
132 int client_id; /* This value unique identifies a GATT Client instance */
133 char *addr; /* Remote GATT Server address */
136 struct gatt_client_info_t {
137 int connection_id; /* This value will uniquely identify a GATT client-server connection */
138 int instance_id; /* This value unique identifies a GATT server instance */
139 char *addr; /* Remote GATT client address */
143 * Remove this feature if code is verified
144 * Remove gatt_client/server_info_t and use gatt_conn_info_t
145 * Remove gatt_client/server_info_list and use gatt_conn_info_list
147 //#define __INTEGRATE_GATT_INFO__ // TODO: this feature can be used if easy setup scenario is fully supported and the name need to be changed to avoid confusion with existing GATT flags
148 #ifndef __INTEGRATE_GATT_INFO__
149 /* Linked List of connected Remote GATT Servers */
150 static GSList *gatt_server_info_list = NULL;
151 /* Linked List of connected Remote GATT clients */
152 static GSList *gatt_client_info_list = NULL;
154 /* GATT Connection Info List Structure */
155 struct gatt_conn_info_t {
156 char *addr; /* Remote GATT address */
157 int connection_id; /* This value will uniquely identify a GATT client-server connection */
158 int client_id; /* This value unique identifies a GATT Client instance */
159 int instance_id; /* This value unique identifies a GATT Server instance */
162 /* Linked List of connected Remote GATT info */
163 static GSList *gatt_conn_info_list = NULL;
164 #define gatt_server_info_t gatt_conn_info_t
165 #define gatt_client_info_t gatt_conn_info_t
166 #define gatt_server_info_list gatt_conn_info_list
167 #define gatt_client_info_list gatt_conn_info_list
172 unsigned char uuid[BLUETOOTH_UUID_HEX_MAX_LEN]; /* If any service added */
173 } bt_gatt_svc_changed_info_t;
178 // bluetooth_device_address_t address; /* Remote BLE Device Address */
179 GSList *services; /* List of all services of above remote device */
180 int count; /* Number of services browsed from remote device */
181 bt_gatt_svc_changed_info_t info;
182 } bt_gatt_service_info_list_t;
185 unsigned char uuid[BLUETOOTH_UUID_HEX_MAX_LEN];
189 GSList *included_svcs;
190 gboolean is_removed; /* 0 => Service is added, 1=> removed */
191 } bt_gatt_service_info_t;
194 unsigned char uuid[BLUETOOTH_UUID_HEX_MAX_LEN];
198 unsigned char val[BLUETOOTH_GATT_ATT_DATA_LENGTH_MAX];
200 } bt_gatt_char_info_t;
203 unsigned char uuid[BLUETOOTH_UUID_HEX_MAX_LEN];
206 unsigned char val[BLUETOOTH_GATT_ATT_DATA_LENGTH_MAX];
207 } bt_gatt_descriptor_info_t;
210 unsigned char uuid[BLUETOOTH_UUID_HEX_MAX_LEN];
212 } bt_gatt_included_service_info_t;
214 static GSList *list_gatt_info = NULL;
217 /* App Information structure */
220 char sender[BT_SENDER_MAX_LENGTH];
221 char uuid[BT_UUID_STRING_MAX];
224 bluetooth_advertising_data_t adv_data; /* Will store adv data for specific slot */
226 bluetooth_scan_resp_data_t scan_rsp; /* Will store scan rsp data for specific slot */
228 gboolean is_initialized;
229 GSList *service_handles;
230 #ifdef TIZEN_GATT_CLIENT
231 int client_id; /* GATT Client instance ID */
232 bluetooth_device_address_t address; /* Remote BLE Device Address */
233 gboolean is_watcher_enabled;
235 } bt_service_app_info_t;
237 /* GATT Server Request Info Structure */
238 struct gatt_server_req_info {
239 int connection_id; /* This value will uniquely identify a GATT client-server connection */
240 int request_id; /* This is an unique transaction ID assigned against each request by stack */
241 int attribute_handle; /* GATT server attribute handle */
242 int offset; /* GATT server attribute offset on which request is invoked by GATT client */
243 bluetooth_gatt_att_request_type_e request_type; /* Read or Write request */
244 char *addr; /* Remote GATT client address */
247 /* GATT Indicate confirm result */
248 struct gatt_indicate_cfm_result_info_t {
249 int result; /* Result of event */
250 char *addr; /* Remote GATT client address */
251 int att_hdl; /* Characteristic Attribute handle */
252 int completed; /* 1 if last event, otheriwse 0 */
255 /* Request Search Utility method */
256 static struct gatt_server_req_info *__bt_gatt_server_find_request_info(int request_id,
257 bluetooth_gatt_att_request_type_e req_type);
259 static int __bt_gatt_send_indication_to_all_connected_clients(bluetooth_gatt_att_data_t *data,
260 bluetooth_gatt_server_indication_params_t *param);
262 static void __bt_remove_all_service_handles(bt_service_app_info_t *app_info);
264 static void __bt_free_service_info(bt_gatt_service_info_t *service_info);
266 static int __bt_unregister_gatt_client_instance(int client_if);
268 static void __bt_service_reset_gatt_data(void);
270 #ifdef TIZEN_GATT_CLIENT
271 static void __bt_handle_client_instance_registered(event_gattc_register_t *data);
272 static void __bt_handle_client_connected(event_gattc_conn_t *event_data);
273 static void __bt_handle_client_disconnected(event_gattc_conn_t *event_data);
274 static void __bt_handle_client_service_search_completed(event_gattc_conn_status_t *event_data);
275 static void __bt_handle_client_service_search_result(event_gattc_service_result_t *event_data);
276 static void __bt_handle_client_characteristic_search_result(
277 event_gattc_characteristic_result_t *event_data);
278 static void __bt_handle_client_descriptor_search_result(event_gattc_descriptor_result_t *event_data);
279 static void __bt_handle_client_characteristic_read_data(event_gattc_read_data *event_data);
280 static void __bt_handle_client_descriptor_read_data(event_gattc_read_data *event_data);
281 static void __bt_handle_client_characteristic_write_data(event_gattc_write_data *event_data);
282 static void __bt_handle_client_descriptor_write_data(event_gattc_write_data *event_data);
283 static void __bt_hanlde_le_device_disconnection(event_dev_conn_status_t *event_data);
284 static void __bt_handle_client_notification_registered(event_gattc_regdereg_notify_t *event_data,
285 gboolean is_registered);
286 static void __bt_handle_client_notification_data(event_gattc_notify_data *event_data);
287 static void __bt_handle_client_service_changed_ind(event_gattc_service_changed_data *event_data);
288 static void __bt_handle_client_mtu_exchange_completed(event_gattc_mtu_configured_t *event_data);
291 static int __bt_unregister_gatt_server_instance(int server_instance);
292 static void __bt_cleanup_remote_services(struct gatt_server_info_t *conn_info);
295 struct gatt_mtu_info_t {
296 char *addr; /* Remote GATT Server address */
300 static GSList *gatt_mtu_info_list = NULL;
302 static struct gatt_mtu_info_t *__bt_find_mtu_gatt_device(char *address);
303 static void __bt_remove_mtu_gatt_device(char *address);
304 static void __bt_add_mtu_gatt_device(char *address);
305 static void __bt_update_mtu_gatt_device(char *address, int mtu);
307 /* Linked List of GATT requests from Remote GATT Clients */
308 static GSList *gatt_server_requests = NULL;
310 /* Number of clients to be notified to */
311 static int num_indicate_clients;
313 /* List of applications */
314 static bt_service_app_info_t numapps[MAX_APPS_SUPPORTED];
316 static void __bt_gatt_handle_pending_request_info(int result,
317 int service_function, void *data, unsigned int size);
319 static void __bt_handle_server_instance_registered(event_gatts_register_t *data);
321 static void __bt_gatt_event_handler(int event_type, gpointer event_data);
328 void _bt_check_adv_app_termination(const char *name)
330 bt_service_app_info_t *app = NULL;
332 int apps[MAX_APPS_SUPPORTED] = { 0, };
334 ret_if(NULL == name);
336 memset(&apps, -1, sizeof(apps));
338 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
341 /* Search for a app which has same sender and stop adv is running */
342 if (strcasecmp(app->sender, name) == 0 && app->is_initialized == TRUE) {
343 BT_DBG("numapps[%d] Match found, name: %s", k, name);
345 /* TODO 2: Need to manage app info as list, not array.
346 This loop always run for MAX count if any apps are terminated.
349 /* Save instances of all apps that need to be unregistered */
350 if (app->instance_id != -1) {
352 /* Unregister all service handles with stack */
353 __bt_remove_all_service_handles(app);
355 /* If Advertising is enabled, stop it */
356 if (app->adv_handle != 0) {
357 BT_INFO("Stop advertising on instance ID [%d]", app->instance_id);
358 /* Disable adv if running */
359 BT_INFO("Disable Advertising Adv Handle [%d] sender [%s]",
360 app->adv_handle, name);
361 _bt_set_advertising(app->sender, app->adv_handle, FALSE, FALSE);
364 apps[app->instance_id] = BT_GATT_SERVER; /* App holds a GATT server Instance */
365 } else if (app->client_id != -1) {
367 apps[app->client_id] = BT_GATT_CLIENT; /* App holds a GATT client Instance */
372 /* Unregister all apps one by one */
373 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
374 if (apps[k] == BT_GATT_SERVER) {
375 BT_INFO("Unregister server app[%d]", k);
376 /* Unregister server instance */
377 __bt_unregister_gatt_server_instance(k);
378 } else if (apps[k] == BT_GATT_CLIENT) {
379 BT_INFO("Unregister client app[%d]", k);
380 /* Unregister client instance */
381 __bt_unregister_gatt_client_instance(k);
386 static int __bt_gatt_send_indication_to_all_connected_clients(bluetooth_gatt_att_data_t *data,
387 bluetooth_gatt_server_indication_params_t *param)
390 int ret = OAL_STATUS_SUCCESS;
391 int result = OAL_STATUS_INTERNAL_ERROR;
393 BT_INFO("Current total number of connected clients [%d]", g_slist_length(gatt_client_info_list));
394 for (l = gatt_client_info_list; l != NULL; l = l->next) {
395 struct gatt_client_info_t *info = l->data;
398 BT_INFO("GATT Remote client address [%s] connection Id [%d]", info->addr, info->connection_id);
400 ret = gatts_send_indication(param->instance_id, param->atrribute_handle,
401 info->connection_id, data->length,
402 param->need_confirmation, (char *)(&data->data[0]));
404 BT_INFO("Send Indication to GATT client [%s] result: [%d]", info->addr, ret);
405 if (ret == OAL_STATUS_SUCCESS) {
406 BT_INFO("Send Indication sent successfully to GATT client [%s]", info->addr);
408 num_indicate_clients++;
412 BT_INFO("Indication sending done for total number of clients [%d]", num_indicate_clients);
416 static struct gatt_server_req_info *__bt_gatt_server_find_request_info(int request_id,
417 bluetooth_gatt_att_request_type_e req_type)
421 for (l = gatt_server_requests; l != NULL; l = l->next) {
422 struct gatt_server_req_info *req_info = l->data;
424 if (req_info && req_info->request_id == request_id && req_info->request_type == req_type) {
425 BT_DBG("GATT Server request info found Req ID [%d] handle [%d] conn ID [%d]",
426 req_info->request_id, req_info->attribute_handle, req_info->connection_id);
430 BT_ERR("Gatt Request not found");
434 void _bt_get_adv_handle_from_instance(int server_inst, int *adv_handle)
438 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
439 if (numapps[k].is_initialized == 1 && numapps[k].instance_id == server_inst) {
440 *adv_handle = numapps[k].adv_handle;
446 #ifdef TIZEN_GATT_CLIENT
447 char * _bt_gatt_get_default_gatt_client_uuid(void)
449 return g_strdup(DEFAULT_GATT_CLIENT_UUID);
453 static void __bt_register_default_gatt_client()
458 uuid_str = _bt_gatt_get_default_gatt_client_uuid();
459 _bt_string_to_uuid(uuid_str, (service_uuid_t*)&uuid);
461 BT_INFO("Register Default GATT client uuid [%s]", uuid_str);
463 if (OAL_STATUS_SUCCESS != gattc_register(&uuid)) /* for only Smart Control */
464 BT_ERR("gattc register failed");
470 int _bt_gatt_init(void)
472 const char *stack_name = NULL;
477 result = gatt_enable();
478 if (result != OAL_STATUS_SUCCESS) {
479 BT_ERR("gatt Init failed");
480 return _bt_convert_oal_status_to_bt_error(result);
483 /* Register gatt event handler */
484 _bt_service_register_event_handler_callback(BT_GATT_MODULE, __bt_gatt_event_handler);
486 __bt_service_reset_gatt_data();
488 stack_name = oal_get_stack_name();
490 if (stack_name && g_strcmp0(stack_name, "bluez") == 0) {
491 /*In the platform, defacult gatt client should be registered */
492 __bt_register_default_gatt_client();
496 return BLUETOOTH_ERROR_NONE;
499 static void __bt_service_reset_gatt_data(void)
503 BT_INFO("Rest numapp");
506 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
507 numapps[k].is_initialized = 0;
508 numapps[k].instance_id = -1;
509 numapps[k].adv_handle = 0;
510 numapps[k].adv_instance = -1;
511 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
512 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
513 memset(numapps[k].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
514 memset(numapps[k].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
515 numapps[k].adv_data_len = 0;
516 numapps[k].scan_rsp_len = 0;
519 numapps[k].client_id = -1;
520 memset(numapps[k].address.addr, 0x00, BLUETOOTH_ADDRESS_LENGTH);
521 numapps[k].is_watcher_enabled = FALSE;
525 void _bt_gatt_deinit(void)
527 BT_INFO("GATT deinit");
529 /* Un-register the default gatt client before */
530 __bt_unregister_gatt_client_instance(gatt_default_client);
532 if (OAL_STATUS_SUCCESS != gatt_disable())
533 BT_ERR("gatt deinit failed");
535 /* Un-register gatt event handler */
536 _bt_service_unregister_event_handler_callback(BT_GATT_MODULE);
538 __bt_service_reset_gatt_data();
541 void _bt_update_adv_handle(const char *sender, int adv_handle)
544 BT_DBG("Sender [%s] Adv handle [%d]", sender, adv_handle);
545 bt_service_app_info_t *info = NULL;
547 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
549 /* Do not update client instance */
550 if (info->instance_id == -1)
552 /* Search for a app which has same sender and adv handle as 0 */
553 if (!g_strcmp0(info->sender, sender) && info->adv_handle == 0)
554 info->adv_handle = adv_handle;
558 int _bt_is_sender_gatt_server_with_no_adv(const char *sender, int adv_handle)
561 BT_DBG("Sender [%s] Adv handle [%d]", sender, adv_handle);
562 bt_service_app_info_t *info = NULL;
564 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
566 /* Search for a app which has same sender and adv handle as 0
567 It is possible that same sender but different adv handle */
568 if (!g_strcmp0(info->sender, sender) && info->adv_handle == 0) {
569 //info->adv_handle = adv_handle;
570 return info->instance_id;
576 int _bt_get_allocated_server_instance(const char *sender, int adv_handle, gboolean use_reserved_slot)
579 BT_DBG("Sender [%s] Adv handle [%d]", sender, adv_handle);
580 bt_service_app_info_t *info = NULL;
582 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
585 /* Exact matching of Adv handle + sender combination */
586 if (!g_strcmp0(info->sender, sender) && info->adv_handle == adv_handle)
587 return info->instance_id;
589 if (!g_strcmp0(info->sender, sender) && info->adv_handle == -1)
590 return info->instance_id;
596 char * _bt_get_sender_and_handle(int server_instance, int *adv_handle)
599 bt_service_app_info_t *info = NULL;
601 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
604 if (info->instance_id == server_instance && info->adv_handle != 0) {
605 *adv_handle = info->adv_handle;
606 BT_DBG("Server instance [%d] Adv handle [%d] Sender [%s]", server_instance, *adv_handle, info->sender);
607 return g_strdup(info->sender);
613 void _bt_set_new_adv_data(bluetooth_advertising_data_t *adv, int len, int instance)
617 bt_service_app_info_t *info = NULL;
618 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
621 if (info->instance_id == instance) {
622 memcpy(info->adv_data.data, &adv->data, len);
628 void _bt_set_new_scan_rsp_data(bluetooth_scan_resp_data_t *scan, int len, int instance)
632 bt_service_app_info_t *info = NULL;
633 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
636 if (info->instance_id == instance) {
637 memcpy(info->scan_rsp.data, &scan->data, len);
643 void _bt_get_previous_adv_data(bluetooth_advertising_data_t *adv, int *len, int instance)
646 bt_service_app_info_t *info = NULL;
648 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
651 if (info->instance_id == instance) {
652 memcpy(&adv->data, info->adv_data.data, info->adv_data_len);
653 *len = info->adv_data_len;
659 void _bt_get_previous_scan_rsp_data(bluetooth_scan_resp_data_t *scan, int *len, int instance)
663 bt_service_app_info_t *info = NULL;
665 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
668 if (info->instance_id == instance) {
669 memcpy(&scan->data, info->scan_rsp.data, info->scan_rsp_len);
670 *len = info->scan_rsp_len;
676 static int __bt_unregister_gatt_client_instance(int client_if)
678 int ret = OAL_STATUS_SUCCESS;
681 BT_INFO("DeAllocate client instance ID [%d]", client_if);
683 /* Reset data: instance_id parameter could be either for GATT Server or for GATT client */
684 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
685 if (numapps[k].client_id == client_if) {
686 BT_INFO("This is a GATT client app, unregister: Slot [%d] vacant", k);
687 numapps[k].client_id = -1;
688 numapps[k].is_initialized = FALSE;
689 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
690 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
691 memset(&numapps[k].address.addr, 0x00, sizeof(bluetooth_device_address_t));
693 /* Its a GATT Client Instance */
694 ret = gattc_deregister(client_if);
695 if (ret != OAL_STATUS_SUCCESS) {
696 BT_ERR("DeAllocate GATT Client instance with stack Fail ret: %d", ret);
697 return _bt_convert_oal_status_to_bt_error(ret);
702 return BLUETOOTH_ERROR_NONE;
705 static int __bt_unregister_gatt_server_instance(int server_instance)
707 int ret = OAL_STATUS_SUCCESS;
710 /* Unregister the server instance */
711 ret = gatts_unregister(server_instance);
712 if (ret != OAL_STATUS_SUCCESS) {
713 BT_ERR("DeAllocate server instance with stack Fail ret: %d", ret);
714 return _bt_convert_oal_status_to_bt_error(ret);
716 BT_INFO("DeAllocated server instance with stack successful..");
719 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
720 if (numapps[k].instance_id == server_instance) {
721 numapps[k].is_initialized = 0;
722 numapps[k].instance_id = -1;
723 numapps[k].adv_handle = 0;
724 numapps[k].adv_instance = -1;
725 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
726 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
727 memset(numapps[k].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
728 memset(numapps[k].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
729 numapps[k].adv_data_len = 0;
730 numapps[k].scan_rsp_len = 0;
734 BT_DBG("Going8 to reset numapp block num [%d]", k);
735 return BLUETOOTH_ERROR_NONE;
738 static void __bt_remove_all_service_handles(bt_service_app_info_t *app_info)
742 int ret = OAL_STATUS_SUCCESS;
745 if (app_info == NULL)
748 count = g_slist_length(app_info->service_handles);
749 BT_INFO("Before handle removal: current count [%d]", count);
751 for (l = app_info->service_handles; l != NULL;) {
756 BT_INFO("Server Handle to be Removed [%d] Instance ID [%d]", *handle, app_info->instance_id);
757 if (_bt_gatt_server_stop_service(app_info->sender, *handle, app_info->instance_id) != BLUETOOTH_ERROR_NONE)
760 ret = gatts_delete_service(app_info->instance_id, *handle);
761 if (ret != OAL_STATUS_SUCCESS) {
762 BT_ERR("ret: %d", ret);
765 app_info->service_handles = g_slist_remove(app_info->service_handles, handle);
768 count = g_slist_length(app_info->service_handles);
769 BT_INFO("After deleting current count [%d]", count);
775 int _bt_unregister_server_instance(const char *sender, int adv_handle)
777 BT_INFO("Unregister Allocated server instance request Sender [%s] Adv handle [%d]", sender, adv_handle);
778 int result = BLUETOOTH_ERROR_NONE;
779 int apps[MAX_APPS_SUPPORTED];
782 bt_service_app_info_t *info = NULL;
784 memset(&apps, -1, sizeof(apps));
786 if (adv_handle == 0) {
787 BT_DBG("Its a direct GATT Server app request to unregister");
788 /* Unregister server instance for each app with same sender (case: GATT Server with multiple adv handle) */
790 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
793 /* Exact matching of sender */
794 if (!g_strcmp0(info->sender, sender)) {
795 BT_INFO("Unregister GATT server instance [%d]", info->instance_id);
796 /* Unregister all service handles with stack */
797 __bt_remove_all_service_handles(info);
799 /* Disable adv if running */
800 BT_INFO("Disable Advertising Adv Handle [%d] sender [%s]",
801 info->adv_handle, sender);
802 _bt_set_advertising(sender, info->adv_handle, FALSE, FALSE);
804 /* Save all instances which need to be unregistered */
805 apps[info->instance_id] = 1;
809 BT_DBG("Its an Internal unregister request by adv application");
810 server_instance = _bt_get_allocated_server_instance(sender, adv_handle, FALSE);
811 BT_DBG("Its an Internal unregister request by adv application: Adv disabled srv instance [%d]", server_instance);
812 if (server_instance == -1) {
813 BT_ERR("No allocated server instance to be removed");
814 return BLUETOOTH_ERROR_INVALID_PARAM;
817 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
819 if (info->instance_id == server_instance) {
820 if (info->service_handles == NULL) {
821 BT_INFO("There are no Service handles with this app, safe to unregister");
822 /* Unregister server instance only if this sender does not have any gatt services in it */
823 result = __bt_unregister_gatt_server_instance(server_instance);
825 info->adv_handle = 0;
826 memset(info->adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
827 info->adv_data_len = 0;
828 memset(info->scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
829 info->scan_rsp_len = 0;
836 /* Unregister all apps one by one */
837 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
839 BT_INFO("Unregister app[%d]", k);
840 /* Unregister server instance */
841 __bt_unregister_gatt_server_instance(k);
848 int _bt_register_server_instance(const char *sender, int adv_handle)
850 int ret = OAL_STATUS_SUCCESS;
851 char *uuid_string = NULL;
856 BT_INFO("Check on which instance Server instance can be initialized....");
857 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
858 if (numapps[k].is_initialized == 1 || strlen(numapps[k].uuid) > 0) {
859 BT_DBG("Instance ID [%d] is already in use..Check next slot", numapps[k].instance_id);
862 BT_DBG("Time to register GATT Server..UUID to be used is [%s] slot [%d]", uuid_list[slot-1], slot);
868 BT_ERR("No Slot if free for GATT Server registration..");
869 return BLUETOOTH_ERROR_REGISTRATION_FAILED;
872 uuid_string = g_malloc0(BT_UUID_STRING_MAX);
873 _bt_string_to_uuid(uuid_list[slot-1], (service_uuid_t*)&uuid);
874 g_strlcpy(uuid_string, uuid_list[slot-1], BT_UUID_STRING_MAX);
875 BT_INFO("Copied UUID string [%s] slot [%d]", uuid_string, slot);
877 /* Register GATT Server */
878 ret = gatts_register(&uuid);
879 if (ret != OAL_STATUS_SUCCESS) {
880 BT_ERR("ret: %d", ret);
882 return _bt_convert_oal_status_to_bt_error(ret);
884 BT_DBG("GATT Server registration call successfully accepted by OAL..wait for Instance Initialized event from OAL..");
885 /* Return & wait for GATT Server Instance Initialization event */
886 memset(numapps[slot].sender, 0x00, sizeof(numapps[slot].sender));
887 memset(numapps[slot].uuid, 0x00, sizeof(numapps[slot].uuid));
889 g_strlcpy(numapps[slot].sender, sender, sizeof(numapps[slot].sender));
890 g_strlcpy(numapps[slot].uuid, uuid_string, sizeof(numapps[slot].uuid));
892 numapps[slot].is_initialized = 0; /* Set initialization from app registered callback */
893 numapps[slot].adv_handle = adv_handle;
896 return BLUETOOTH_ERROR_NONE;
900 static void __bt_gatt_get_pending_request_info(int service_function,
904 invocation_info_t *req_info = NULL;
906 for (l = _bt_get_invocation_list(); l != NULL; l = g_slist_next(l)) {
908 if (req_info == NULL)
911 if (req_info->service_function == service_function) {
912 *sender = req_info->sender;
918 static void __bt_gatt_handle_pending_request_info(int result,
919 int service_function, void *data, unsigned int size)
923 invocation_info_t *req_info = NULL;
924 ret_if(data == NULL);
926 for (l = _bt_get_invocation_list(); l != NULL; ) {
929 if (req_info == NULL || req_info->service_function != service_function)
932 switch (service_function) {
934 case BT_GATT_SERVER_REGISTER: {
935 bt_service_app_info_t *param = (bt_service_app_info_t*)data;
937 if (!g_strcmp0(req_info->sender, param->sender)) {
938 BT_DBG("GATT Server app found [%s]", req_info->sender);
940 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
941 g_array_append_vals(out_param, ¶m->instance_id, sizeof(int));
942 _bt_service_method_return(req_info->context, out_param, result);
944 _bt_free_info_from_invocation_list(req_info);
945 g_array_free(out_param, TRUE);
949 case BT_GATT_SERVER_START_SERVICE:
950 case BT_GATT_SERVER_DELETE_SERVICE: {
951 bt_service_app_info_t *param = (bt_service_app_info_t*)data;
953 int *saved_instance_id = (int*)req_info->user_data;
954 if (!g_strcmp0(req_info->sender, param->sender) && param->instance_id == *saved_instance_id) {
955 BT_DBG("GATT Server app found [%s] Instance ID [%d] Reply DBUS",
956 req_info->sender, *saved_instance_id);
958 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
959 g_array_append_vals(out_param, &saved_instance_id, sizeof(int));
960 _bt_service_method_return(req_info->context, out_param, result);
962 _bt_free_info_from_invocation_list(req_info);
963 g_array_free(out_param, TRUE);
967 case BT_GATT_SERVER_ADD_SERVICE:
968 case BT_GATT_SERVER_ADD_DESCRIPTOR:
969 case BT_GATT_SERVER_ADD_CHARACTERISTIC: {
970 int *handle = (int*)data;
971 BT_DBG("Characteristic added: Handle [%d]", *handle);
972 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
974 g_array_append_vals(out_param, handle, sizeof(int));
975 _bt_service_method_return(req_info->context, out_param, result);
977 _bt_free_info_from_invocation_list(req_info);
978 g_array_free(out_param, TRUE);
983 case BT_DISCONNECT_LE: {
984 #ifdef TIZEN_GATT_CLIENT
985 char *addr = (char*)req_info->user_data;
986 bluetooth_device_address_t address;
988 if (!g_strcmp0(addr, (char*)data)) {
989 BT_INFO("GATT Client connect-disconnect call pending for app [%s] addr [%s]",
990 req_info->sender, addr + 12);
991 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
992 _bt_convert_addr_string_to_type(address.addr, addr);
994 g_array_append_vals(out_param, (bluetooth_device_address_t*)&address,
995 sizeof(bluetooth_device_address_t));
996 _bt_service_method_return(req_info->context, out_param, result);
998 _bt_free_info_from_invocation_list(req_info);
999 g_array_free(out_param, TRUE);
1002 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1004 g_array_append_vals(out_param, (bluetooth_device_address_t*)data,
1005 sizeof(bluetooth_device_address_t));
1006 _bt_service_method_return(req_info->context, out_param, result);
1008 _bt_free_info_from_invocation_list(req_info);
1010 g_array_free(out_param, TRUE);
1014 #ifdef TIZEN_GATT_CLIENT
1015 case BT_GATT_CLIENT_REGISTER: {
1016 bt_service_app_info_t *param = (bt_service_app_info_t*)data;
1018 if (!g_strcmp0(req_info->sender, param->sender)) {
1019 BT_DBG("GATT Client app found [%s] created client ID [%d]",
1020 req_info->sender, param->client_id);
1022 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1023 g_array_append_vals(out_param, ¶m->client_id, sizeof(int));
1024 _bt_service_method_return(req_info->context, out_param, result);
1026 _bt_free_info_from_invocation_list(req_info);
1027 g_array_free(out_param, TRUE);
1031 case BT_GATT_GET_PRIMARY_SERVICES: {
1032 bt_services_browse_info_t *param = (bt_services_browse_info_t*)data;
1033 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1034 _bt_convert_addr_type_to_string(address,
1035 (unsigned char *)(¶m->device_addr.addr));
1037 /* Match address to determine same request */
1038 if (!g_strcmp0((char*)req_info->user_data, address)) {
1039 BT_DBG("GATT Client app found [%s] Remote address [%s]",
1040 req_info->sender, address);
1042 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1043 g_array_append_vals(out_param, param,
1044 sizeof(bt_services_browse_info_t));
1046 //g_array_append_vals(out_param, ¶m->client_id, sizeof(int));
1047 _bt_service_method_return(req_info->context, out_param, result);
1049 _bt_free_info_from_invocation_list(req_info);
1050 g_array_free(out_param, TRUE);
1055 case BT_GATT_GET_SERVICE_PROPERTIES: {
1056 bt_char_browse_info_t param;
1057 memcpy((void*)¶m, data, sizeof(bt_char_browse_info_t));
1058 //bt_char_browse_info_t *param = (bt_char_browse_info_t*)data;
1060 bluetooth_gatt_client_svc_prop_info_t *prop = (bluetooth_gatt_client_svc_prop_info_t*)req_info->user_data;
1062 /* Match both address and service properties to determine same request */
1063 if (!memcmp(param.device_addr.addr,
1064 prop->device_address.addr,
1065 sizeof(bluetooth_device_address_t)) &&
1066 !memcmp(param.svc_uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1067 param.svc_inst_id == prop->svc.instance_id) {
1068 BT_DBG("Returning Service properties");
1070 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1071 g_array_append_vals(out_param, ¶m, sizeof(bt_char_browse_info_t));
1072 _bt_service_method_return(req_info->context, out_param, result);
1074 _bt_free_info_from_invocation_list(req_info);
1075 g_array_free(out_param, TRUE);
1079 case BT_GATT_GET_CHARACTERISTIC_PROPERTIES: {
1080 bt_descriptor_browse_info_t *param = (bt_descriptor_browse_info_t*)data;
1082 bluetooth_gatt_client_char_prop_info_t *prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
1084 /* Match both address, service properties &char properties to determine same request */
1085 if (!memcmp(param->device_addr.addr,
1086 prop->device_address.addr,
1087 sizeof(bluetooth_device_address_t)) &&
1088 !memcmp(param->svc_uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1089 param->svc_inst_id == prop->svc.instance_id &&
1090 !memcmp(param->char_uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1091 param->char_inst_id == prop->characteristic.instance_id) {
1092 BT_DBG("Returning Characteristic properties");
1093 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1094 g_array_append_vals(out_param, param, sizeof(bt_descriptor_browse_info_t));
1095 _bt_service_method_return(req_info->context, out_param, result);
1097 _bt_free_info_from_invocation_list(req_info);
1098 g_array_free(out_param, TRUE);
1102 case BT_GATT_WATCH_CHARACTERISTIC: {
1103 bt_gatt_notif_reg_info_t *param = (bt_gatt_notif_reg_info_t*)data;
1104 bluetooth_gatt_client_char_prop_info_t *prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
1106 /* Match both address, service properties &char properties to determine same request */
1107 if (!memcmp(param->addr.addr,
1108 prop->device_address.addr,
1109 sizeof(bluetooth_device_address_t)) &&
1110 !memcmp(param->svc_uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1111 param->svc_inst == prop->svc.instance_id &&
1112 !memcmp(param->char_uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1113 param->char_inst == prop->characteristic.instance_id) {
1114 BT_INFO("Characteristic Watch Successful: Is registered [%d]",
1115 param->is_registered);
1116 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1117 g_array_append_vals(out_param, param, sizeof(bt_gatt_notif_reg_info_t));
1118 _bt_service_method_return(req_info->context, out_param, result);
1119 _bt_free_info_from_invocation_list(req_info);
1120 g_array_free(out_param, TRUE);
1124 case BT_GATT_READ_CHARACTERISTIC:
1125 case BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE: {
1126 bluetooth_gatt_client_char_prop_info_t *param = (bluetooth_gatt_client_char_prop_info_t*)data;
1128 char *addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
1129 bluetooth_gatt_client_char_prop_info_t *prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
1130 _bt_convert_addr_type_to_string(addr, prop->device_address.addr);
1132 if (!memcmp(param, prop, sizeof(bluetooth_gatt_client_char_prop_info_t))) {
1133 BT_DBG("Gatt Char read or write request matched for address [%s]", addr);
1134 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1135 g_array_append_vals(out_param, param, sizeof(bluetooth_gatt_client_char_prop_info_t));
1136 _bt_service_method_return(req_info->context, out_param, result);
1138 _bt_free_info_from_invocation_list(req_info);
1139 g_array_free(out_param, TRUE);
1144 case BT_GATT_READ_DESCRIPTOR_VALUE:
1145 case BT_GATT_WRITE_DESCRIPTOR_VALUE: {
1146 bluetooth_gatt_client_desc_prop_info_t *param = (bluetooth_gatt_client_desc_prop_info_t*)data;
1148 char *addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
1149 bluetooth_gatt_client_desc_prop_info_t *prop = (bluetooth_gatt_client_desc_prop_info_t*)req_info->user_data;
1150 _bt_convert_addr_type_to_string(addr, prop->device_address.addr);
1152 if (!memcmp(param, prop, sizeof(bluetooth_gatt_client_desc_prop_info_t))) {
1153 BT_DBG("Descriptor read or write request matched for address [%s]", addr);
1154 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1155 g_array_append_vals(out_param, param, sizeof(bluetooth_gatt_client_desc_prop_info_t));
1156 _bt_service_method_return(req_info->context, out_param, result);
1158 _bt_free_info_from_invocation_list(req_info);
1159 g_array_free(out_param, TRUE);
1164 case BT_REQ_ATT_MTU: {
1165 char *addr = (char*)req_info->user_data;
1166 bluetooth_device_address_t address;
1168 if (!g_strcmp0(addr, (char*)data)) {
1169 BT_DBG("GATT Client BT_REQ_ATT_MTU call pending for app [%s] addr [%s]",
1170 req_info->sender, addr);
1171 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1172 _bt_convert_addr_string_to_type(address.addr, addr);
1174 g_array_append_vals(out_param, (bluetooth_device_address_t*)&address,
1175 sizeof(bluetooth_device_address_t));
1176 _bt_service_method_return(req_info->context, out_param, result);
1178 _bt_free_info_from_invocation_list(req_info);
1179 g_array_free(out_param, TRUE);
1189 static void __bt_handle_server_instance_registered(event_gatts_register_t *data)
1191 bt_service_app_info_t *info = NULL;
1193 char *uuid_string = g_malloc0(BT_UUID_STRING_MAX);
1195 _bt_uuid_to_string(&(data->server_uuid), uuid_string);
1196 BT_INFO("Instance ID is Intialized [%d] UUID initialized [%s]", data->server_inst, uuid_string);
1198 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1199 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1202 if (g_strcmp0(info->uuid, uuid_string) == 0) {
1203 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1204 info->is_initialized = TRUE;
1205 info->instance_id = data->server_inst;
1206 info->adv_instance = data->server_inst;
1207 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE, BT_GATT_SERVER_REGISTER,
1208 (void*)info, sizeof(bt_service_app_info_t));
1212 g_free(uuid_string);
1215 static void __bt_handle_gatt_server_service_added(event_gatts_srvc_prm_t *event)
1217 int result = BLUETOOTH_ERROR_NONE;
1221 bt_service_app_info_t *info = NULL;
1224 char *uuid_str = g_malloc0(BT_UUID_STRING_MAX);
1225 _bt_uuid_to_string(&(event->gatt_srvc_id.id.uuid), uuid_str);
1226 BT_INFO("GATT Added Service UUID: [%s] Server Ins [%d] Service hdl [%d] Is Primary [%d]\n",
1227 uuid_str, event->gatt_srvc_stat.server_inst, svc_handle, event->gatt_srvc_id.is_prmry);
1229 if (event->gatt_srvc_stat.status != OAL_STATUS_SUCCESS) {
1230 result = BLUETOOTH_ERROR_INTERNAL;
1231 svc_handle = 0; /* Service handle set to 0 indicates.
1232 0 is reserved by BT SIG, cant be used by app*/
1234 svc_handle = event->gatt_srvc_stat.servic_hndl;
1236 BT_INFO("GATT Added Service Status [%d] Server Ins [%d] Service hdl [%d] Is Primary [%d]\n",
1237 event->gatt_srvc_stat.status, event->gatt_srvc_stat.server_inst, svc_handle, event->gatt_srvc_id.is_prmry);
1239 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1240 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1243 if (info->instance_id == event->gatt_srvc_stat.server_inst) {
1244 BT_INFO("numapps[%d] Found GATT Server.. UUID [%s], sender [%s]", k, info->uuid, info->sender);
1245 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_ADD_SERVICE,
1246 (int*)&svc_handle, sizeof(int));
1248 /* Add Service Handle */
1249 if (svc_handle > 0) {
1250 handle = g_malloc0(sizeof(int));
1251 *handle = svc_handle;
1252 numapps[k].service_handles = g_slist_append(numapps[k].service_handles, handle);
1253 count = g_slist_length(numapps[k].service_handles);
1254 BT_INFO("Added Service handle [%d] to list..current count [%d]", svc_handle, count);
1263 static void __bt_handle_gatt_server_characteristic_added(event_gatts_srvc_charctr_t *event)
1265 int result = BLUETOOTH_ERROR_NONE;
1267 bt_service_app_info_t *info = NULL;
1269 char *uuid_str = g_malloc0(BT_UUID_STRING_MAX);
1271 BT_INFO("GATT Server Char added status [%d]", event->gatt_srvc_stat.status);
1272 BT_INFO("GATT Server_inst: [%d]", event->gatt_srvc_stat.server_inst);
1273 BT_INFO("GATT Service Handle: [%d]", event->gatt_srvc_stat.servic_hndl);
1274 BT_INFO("GATT Add characteristic Status: [%d]", event->gatt_srvc_stat.status);
1275 BT_INFO("GATT Service characteristic Handle: [%d]", event->charctr_hndl);
1277 if (event->gatt_srvc_stat.status != OAL_STATUS_SUCCESS) {
1278 result = BLUETOOTH_ERROR_INTERNAL;
1279 char_handle = 0; /* characteristic handle set to 0 indicates.
1280 0 is reserved by BT SIG, cant be used by app*/
1282 char_handle = event->charctr_hndl;
1285 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1286 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1289 if (info->instance_id == event->gatt_srvc_stat.server_inst) {
1290 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1291 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_ADD_CHARACTERISTIC,
1292 (int*)&char_handle, sizeof(int));
1298 _bt_uuid_to_string(&(event->charctr_uuid), uuid_str);
1299 BT_INFO("GATT Added Characteristic: UUID: [%s]", uuid_str);
1304 static void __bt_handle_gatt_server_descriptor_added(event_gatts_srvc_descr_t* event)
1306 int result = BLUETOOTH_ERROR_NONE;
1308 bt_service_app_info_t *info = NULL;
1310 char *uuid_str = g_malloc0(BT_UUID_STRING_MAX);
1312 BT_INFO("GATT Server Descriptor added status [%d]", event->gatt_srvc_stat.status);
1313 BT_INFO("GATT Server_inst: [%d]", event->gatt_srvc_stat.server_inst);
1314 BT_INFO("GATT Service Handle: [%d]", event->gatt_srvc_stat.servic_hndl);
1315 BT_INFO("GATT Add Descriptor Status: [%d]", event->gatt_srvc_stat.status);
1316 BT_INFO("GATT Service Descriptor Handle: [%d]", event->descrptr_hndl);
1318 if (event->gatt_srvc_stat.status != OAL_STATUS_SUCCESS) {
1319 result = BLUETOOTH_ERROR_INTERNAL;
1320 desc_handle = 0; /* Service handle set to 0 indicates.
1321 0 is reserved by BT SIG, cant be used by app*/
1323 desc_handle = event->descrptr_hndl;
1325 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1326 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1329 if (info->instance_id == event->gatt_srvc_stat.server_inst) {
1330 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1331 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_ADD_DESCRIPTOR,
1332 (int*)&desc_handle, sizeof(int));
1337 _bt_uuid_to_string(&(event->descrptr_uuid), uuid_str);
1338 BT_INFO("GATT Added Descriptor: UUID: [%s]", uuid_str);
1343 static void __bt_handle_gatt_server_service_started(event_gatts_srvc_t *event)
1345 bt_service_app_info_t *info = NULL;
1346 int result = BLUETOOTH_ERROR_NONE;
1348 BT_INFO("GATT Server Service Started..");
1351 BT_INFO("GATT Server_inst: [%d]", event->server_inst);
1352 BT_INFO("GATT Service Handle: [%d]", event->servic_hndl);
1353 BT_INFO("GATT Service Started Status: [%d]", event->status);
1355 if (event->status != OAL_STATUS_SUCCESS) {
1356 BT_ERR("GATT Server Service Start Failed Err: [%d]", event->status);
1357 result = BLUETOOTH_ERROR_INTERNAL;
1360 /* Check if the just registered Instance ID belongs to requester */
1361 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1364 if (info->instance_id == event->server_inst) {
1365 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1366 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_START_SERVICE,
1367 (void*)info, sizeof(bt_service_app_info_t));
1373 static void __bt_handle_gatt_server_service_stopped(event_gatts_srvc_t *event)
1375 int result = BLUETOOTH_ERROR_NONE;
1376 bt_service_app_info_t *info = NULL;
1378 BT_INFO("GATT Server Service Stopped..");
1380 BT_INFO("GATT Server_inst: [%d]", event->server_inst);
1381 BT_INFO("GATT Service Handle: [%d]", event->servic_hndl);
1382 BT_INFO("GATT Service Stopped Status: [%d]", event->status);
1384 if (event->status != OAL_STATUS_SUCCESS) {
1385 BT_ERR("GATT Server Service Stop Failed Err: [%d]", event->status);
1386 result = BLUETOOTH_ERROR_INTERNAL;
1389 /* Check if the just registered Instance ID belongs to requester */
1390 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1393 if (info->instance_id == event->server_inst) {
1394 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1395 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_STOP_SERVICE,
1396 (void*)info, sizeof(bt_service_app_info_t));
1402 static void __bt_handle_gatt_server_service_deleted(event_gatts_srvc_t *event)
1404 int result = BLUETOOTH_ERROR_NONE;
1405 bt_service_app_info_t *info = NULL;
1407 BT_INFO("GATT Server Service Deleted..");
1409 BT_INFO("GATT Server_inst: [%d]", event->server_inst);
1410 BT_INFO("GATT Service Handle: [%d]", event->servic_hndl);
1411 BT_INFO("GATT Service Deleted Status: [%d]", event->status);
1413 if (event->status != OAL_STATUS_SUCCESS) {
1414 BT_ERR("GATT Server Service Delete Failed Err: [%d]", event->status);
1415 result = BLUETOOTH_ERROR_INTERNAL;
1418 /* Check if the just registered Instance ID belongs to requester */
1419 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1422 if (info->instance_id == event->server_inst) {
1423 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1424 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_DELETE_SERVICE,
1425 (void*)info, sizeof(bt_service_app_info_t));
1431 struct gatt_client_info_t *_bt_find_remote_gatt_client_info(char *address)
1434 struct gatt_client_info_t *info = NULL;
1435 for (l = gatt_client_info_list; l != NULL; l = g_slist_next(l)) {
1436 info = (struct gatt_client_info_t*)l->data;
1440 if (!g_strcmp0(info->addr, address)) {
1441 BT_DBG("Remote GATT client found addr[%s]", info->addr);
1448 static struct gatt_client_info_t *__bt_find_remote_gatt_client_info_from_conn_id(int conn_id)
1451 struct gatt_client_info_t *info = NULL;
1453 for (l = gatt_client_info_list; l != NULL; l = g_slist_next(l)) {
1454 info = (struct gatt_client_info_t*)l->data;
1458 if (info->connection_id == conn_id) {
1459 BT_INFO("Remote GATT client found addr[%s]", info->addr);
1466 #ifdef TIZEN_GATT_CLIENT
1467 struct gatt_server_info_t *_bt_find_remote_gatt_server_info(char *address)
1470 struct gatt_server_info_t *info = NULL;
1471 for (l = gatt_server_info_list; l != NULL; l = g_slist_next(l)) {
1472 info = (struct gatt_server_info_t*)l->data;
1476 if (!g_strcmp0(info->addr, address)) {
1477 BT_DBG("Remote GATT Server found addr[%s] connection_id %d", info->addr, info->connection_id);
1484 static struct gatt_out_conn_info_t* __bt_find_gatt_outgoing_conn_info(char *address)
1487 struct gatt_out_conn_info_t *info = NULL;
1488 for (l = outgoing_gatt_conn_list; l != NULL; l = g_slist_next(l)) {
1489 info = (struct gatt_out_conn_info_t*)l->data;
1493 if (!g_strcmp0(info->addr, address)) {
1494 BT_INFO("Outgoing connection info found addr[%s]", info->addr + 12);
1502 #ifdef TIZEN_GATT_CLIENT
1503 static void __bt_handle_gatt_server_connection_state(event_gatts_conn_t *event)
1505 int result = BLUETOOTH_ERROR_NONE;
1506 struct gatt_client_info_t *client_info = NULL;
1507 #ifndef __INTEGRATE_GATT_INFO__
1508 struct gatt_server_info_t *server_info = NULL;
1510 bluetooth_device_address_t dev_addr;
1511 GVariant *param = NULL;
1514 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1516 memcpy(dev_addr.addr, event->address.addr, 6);
1518 /* REPLY dbus Context to application which called BT_CONNECT_LE. There is status
1520 _bt_convert_addr_type_to_string(address,
1521 (unsigned char *)dev_addr.addr);
1523 if (event->status != OAL_STATUS_SUCCESS)
1524 result = BLUETOOTH_ERROR_INTERNAL;
1526 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
1527 address, BT_ADDRESS_STRING_SIZE);
1529 BT_INFO("GATT Server Connedted: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1530 address + 12, event->server_inst, event->conn_id);
1533 /* Check if device is already in connected list */
1534 client_info = _bt_find_remote_gatt_client_info(address);
1537 BT_DBG("Conn Info absent: But no need to Send Local GATT Server Connected event to apps");
1539 param = g_variant_new("(is)", result, address);
1541 /* Send event to application */
1542 _bt_send_event(BT_DEVICE_EVENT,
1543 BLUETOOTH_EVENT_GATT_SERVER_CONNECTED, /* Local device is GATT server */
1546 /* Save client connection info */
1547 client_info = g_new0(struct gatt_client_info_t, 1);
1548 client_info->addr = g_strdup(address);
1549 BT_INFO("Added GATT client addr[%s]", client_info->addr + 12);
1550 client_info->connection_id = event->conn_id;
1551 #ifdef __INTEGRATE_GATT_INFO__
1552 client_info->client_id = -1;
1554 client_info->instance_id = event->server_inst;
1555 gatt_client_info_list = g_slist_append(gatt_client_info_list, client_info);
1556 BT_INFO("Total num of connected Remote GATT Clients [%d]", g_slist_length(gatt_client_info_list));
1558 #ifndef __INTEGRATE_GATT_INFO__
1559 /* Save server connection info */
1560 server_info = g_new0(struct gatt_server_info_t, 1);
1561 server_info->addr = g_strdup(address);
1562 server_info->client_id = -1;
1563 BT_INFO("Added GATT server addr[%s]", server_info->addr + 12);
1564 server_info->connection_id = event->conn_id;
1565 gatt_server_info_list = g_slist_append(gatt_server_info_list, server_info);
1566 BT_INFO("Total num of connected Remote GATT Servers [%d]", g_slist_length(gatt_server_info_list));
1569 ret = gattc_add_connection_info((bt_address_t *)&dev_addr, event->conn_id, event->server_inst);
1570 if (ret != OAL_STATUS_SUCCESS) {
1571 BT_ERR("gattc register server instance failed");
1575 __bt_add_mtu_gatt_device(address);
1580 /* GATT Server Dis connected */
1581 static void __bt_handle_gatt_server_disconnection_state(event_gatts_conn_t *event)
1583 int result = BLUETOOTH_ERROR_NONE;
1584 struct gatt_client_info_t *client_info = NULL;
1585 #ifndef __INTEGRATE_GATT_INFO__
1586 struct gatt_server_info_t *server_info = NULL;
1588 bluetooth_device_address_t dev_addr;
1589 GVariant *param = NULL;
1590 char address[BT_ADDRESS_STRING_SIZE];
1592 memcpy(dev_addr.addr, event->address.addr, 6);
1594 /* REPLY dbus Context to application which called BT_DISCONNECT_LE. There is status
1596 _bt_convert_addr_type_to_string(address,
1597 (unsigned char *)dev_addr.addr);
1599 if (event->status != OAL_STATUS_SUCCESS)
1600 result = BLUETOOTH_ERROR_INTERNAL;
1602 if (NULL == _bt_get_request_info_data(BT_DISCONNECT_LE, address)) {
1603 if (NULL != _bt_get_request_info_data(BT_CONNECT_LE, address)) {
1604 result = BLUETOOTH_ERROR_INTERNAL;
1606 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
1607 address, BT_ADDRESS_STRING_SIZE);
1609 BT_ERR("Failed to connect Local GATT Server Remote Client addr[%s]", address + 12);
1613 __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE,
1614 address, BT_ADDRESS_STRING_SIZE);
1617 BT_INFO("Local GATT Server DisConnected: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1618 address + 12, event->server_inst, event->conn_id);
1619 /* Remove Connection info */
1620 client_info = _bt_find_remote_gatt_client_info(address);
1622 BT_DBG("No need to Send Local GATT Server Disconnected event to apps, just remove remote client info");
1624 param = g_variant_new("(is)", result, address);
1625 /* Send event to application */
1626 _bt_send_event(BT_DEVICE_EVENT,
1627 BLUETOOTH_EVENT_GATT_SERVER_DISCONNECTED, /* Local device is GATT server */
1630 #ifndef __INTEGRATE_GATT_INFO__
1631 /* Remove server info from list */
1632 server_info = _bt_find_remote_gatt_server_info(address);
1634 gatt_server_info_list = g_slist_remove(gatt_server_info_list, server_info);
1636 /* Remove all services from info list_gatt_info */
1637 __bt_cleanup_remote_services(server_info);
1639 BT_INFO("Can not find conn info, already removed!");
1642 /* Remove all services from info list_gatt_info */
1643 __bt_cleanup_remote_services(client_info);
1646 /* Remove client info from List */
1647 gatt_client_info_list = g_slist_remove(gatt_client_info_list, client_info);
1648 BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
1649 g_free(client_info->addr);
1650 g_free(client_info);
1653 __bt_remove_mtu_gatt_device(address);
1657 static void __bt_handle_gatt_server_connection_state(event_gatts_conn_t *event)
1659 int result = BLUETOOTH_ERROR_NONE;
1660 struct gatt_client_info_t *conn_info = NULL;
1661 bt_service_app_info_t *info = NULL;
1662 bluetooth_device_address_t dev_addr;
1663 GVariant *param = NULL;
1666 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1668 memcpy(dev_addr.addr, event->address.addr, 6);
1671 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1672 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1675 if (info->instance_id == event->server_inst) {
1676 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1677 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
1678 (bluetooth_device_address_t*)&dev_addr, sizeof(bluetooth_device_address_t));
1684 /* Send event to BT-API */
1685 _bt_convert_addr_type_to_string(address,
1686 (unsigned char *)dev_addr.addr);
1688 BT_INFO("GATT Server Connedted: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1689 address, event->server_inst, event->conn_id);
1692 /* Check if device is already in connected list */
1693 conn_info = _bt_find_remote_gatt_client_info(address);
1696 param = g_variant_new("(is)", result, address);
1697 /* Send event to application */
1698 _bt_send_event(BT_GATT_SERVER_EVENT,
1699 BLUETOOTH_EVENT_GATT_CONNECTED,
1702 /* Save Connection info */
1703 conn_info = g_new0(struct gatt_client_info_t, 1);
1704 conn_info->addr = g_strdup(address);
1705 BT_INFO("Added GATT client addr[%s]", conn_info->addr);
1706 conn_info->connection_id = event->conn_id;
1707 conn_info->instance_id = event->server_inst;
1708 gatt_client_info_list = g_slist_append(gatt_client_info_list, conn_info);
1709 BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
1715 /* GATT Server Dis connected */
1716 static void __bt_handle_gatt_server_disconnection_state(event_gatts_conn_t *event)
1718 int result = BLUETOOTH_ERROR_NONE;
1719 struct gatt_client_info_t *conn_info = NULL;
1720 bluetooth_device_address_t dev_addr;
1721 bt_service_app_info_t *info = NULL;
1722 GVariant *param = NULL;
1725 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1727 memcpy(dev_addr.addr, event->address.addr, 6);
1729 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1730 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1733 if (info->instance_id == event->server_inst) {
1734 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1735 __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE,
1736 (bluetooth_device_address_t*)&dev_addr, sizeof(bluetooth_device_address_t));
1742 /* Send event to BT-API */
1743 _bt_convert_addr_type_to_string(address,
1744 (unsigned char *)dev_addr.addr);
1746 BT_INFO("GATT Server DisConnected: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1747 address, event->server_inst, event->conn_id);
1749 param = g_variant_new("(is)", result, address);
1750 /* Send event to application */
1751 _bt_send_event(BT_GATT_SERVER_EVENT,
1752 BLUETOOTH_EVENT_GATT_DISCONNECTED,
1755 /* Remove Connection info */
1756 conn_info = _bt_find_remote_gatt_client_info(address);
1758 BT_INFO("Remove GATT client info from List..");
1759 /* Remove info from List */
1760 gatt_client_info_list = g_slist_remove(gatt_client_info_list, conn_info);
1761 BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
1762 g_free(conn_info->addr);
1771 static void __bt_handle_gatt_server_acquire_write_requested(event_gatts_srvc_acquire_attr_t *event)
1773 GVariant *param = NULL;
1774 int result = BLUETOOTH_ERROR_NONE;
1775 struct gatt_server_req_info *req_info = NULL;
1776 bluetooth_device_address_t dev_addr;
1777 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1779 BT_INFO("GATT Server ACQUIRE Write Req");
1780 BT_DBG(" conn id %d, trans id %d, attr andle %d", event->attr_trans.conn_id,
1781 event->attr_trans.trans_id, event->attr_trans.attr_handle);
1783 //address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1784 memcpy(dev_addr.addr, event->address.addr, 6);
1786 req_info = g_new0(struct gatt_server_req_info, 1);
1787 req_info->request_id = event->attr_trans.trans_id;
1788 req_info->attribute_handle = event->attr_trans.attr_handle;
1789 req_info->connection_id = event->attr_trans.conn_id;
1790 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_ACQUIRE_WRITE;
1791 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1793 _bt_convert_addr_type_to_string(address,
1794 (unsigned char *)dev_addr.addr);
1796 param = g_variant_new("(iiiiiis)", result,
1797 event->attr_trans.conn_id,
1798 event->attr_trans.trans_id,
1799 event->attr_trans.attr_handle,
1800 event->mtu, event->attr_trans.offset, address);
1801 BT_DBG("remote address : [%s]", address);
1803 _bt_send_event(BT_GATT_SERVER_EVENT,
1804 BLUETOOTH_EVENT_GATT_SERVER_ACQUIRE_WRITE,
1809 static void __bt_handle_gatt_server_acquire_notify_requested(event_gatts_srvc_acquire_attr_t *event)
1811 GVariant *param = NULL;
1812 int result = BLUETOOTH_ERROR_NONE;
1813 struct gatt_server_req_info *req_info = NULL;
1814 bluetooth_device_address_t dev_addr;
1815 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1817 BT_INFO("GATT Server ACQUIRE Notify Req");
1818 BT_DBG("conn id %d, trans id %d, attr handle %d, req address %s",
1819 event->attr_trans.conn_id, event->attr_trans.trans_id,
1820 event->attr_trans.attr_handle, address);
1822 memcpy(dev_addr.addr, event->address.addr, 6);
1823 _bt_convert_addr_type_to_string(address,
1824 (unsigned char *)dev_addr.addr);
1825 BT_DBG("remote address : [%s]", address);
1827 req_info = g_new0(struct gatt_server_req_info, 1);
1828 req_info->request_id = event->attr_trans.trans_id;
1829 req_info->attribute_handle = event->attr_trans.attr_handle;
1830 req_info->connection_id = event->attr_trans.conn_id;
1831 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_ACQUIRE_NOTIFY;
1832 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1834 param = g_variant_new("(iiiiiis)", result,
1835 event->attr_trans.conn_id,
1836 event->attr_trans.trans_id,
1837 event->attr_trans.attr_handle,
1838 event->mtu, event->attr_trans.offset,
1841 _bt_send_event(BT_GATT_SERVER_EVENT,
1842 BLUETOOTH_EVENT_GATT_SERVER_ACQUIRE_NOTIFY,
1846 static void __bt_handle_gatt_server_write_requested(event_gatts_srvc_write_attr_t *event)
1849 bluetooth_device_address_t dev_addr;
1850 GVariant *param = NULL;
1851 int result = BLUETOOTH_ERROR_NONE;
1853 gboolean is_prepare_write;
1855 GVariant *data = NULL;
1857 struct gatt_server_req_info *req_info = NULL;
1858 BT_INFO("GATT Server Write Requested");
1860 memcpy(dev_addr.addr, event->address.addr, 6);
1862 BT_INFO("GATT Server Write Req Connection ID: [%d]", event->attr_trans.conn_id);
1863 BT_INFO("GATT Server Write Req Transaction ID:[%d]", event->attr_trans.trans_id);
1864 BT_INFO("GATT Server Write Req Attribute Handle: [%d]", event->attr_trans.attr_handle);
1865 BT_INFO("GATT Server Write Req Attribute Offset: [%d]", event->attr_trans.offset);
1866 BT_INFO("GATT Server Write Req value length [%d]", event->length);
1867 BT_INFO("GATT Server Write Req needs response: [%d]", event->need_rsp);
1868 BT_INFO("GATT Server Write Req Is Prep: [%d]", event->is_prep);
1870 if (event->length <= 0) {
1871 BT_INFO("GATT Server write requested, but length of attr value is 0");
1875 need_resp = event->need_rsp;
1876 is_prepare_write = event->is_prep;
1878 write_val = g_memdup(&event->value[0], event->length);
1880 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1881 _bt_convert_addr_type_to_string(address,
1882 (unsigned char *)dev_addr.addr);
1884 BT_INFO("GATT Server Write Request from remote client [%s]", address);
1886 if (event->length > 0) {
1887 for (i = 0; i < event->length; i++)
1888 BT_DBG("Data[%d] = [0x%x]", i, event->value[i]);
1890 /* Save Write Request Info */
1891 req_info = g_new0(struct gatt_server_req_info, 1);
1892 req_info->request_id = event->attr_trans.trans_id;
1893 req_info->attribute_handle = event->attr_trans.attr_handle;
1894 req_info->connection_id = event->attr_trans.conn_id;
1895 req_info->addr = address;
1896 req_info->offset = event->attr_trans.offset;
1897 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_WRITE;
1898 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1900 data = g_variant_new_from_data(
1901 G_VARIANT_TYPE_BYTESTRING,
1906 param = g_variant_new("(iiiiiibbsn@ay)", result,
1907 event->attr_trans.conn_id,
1908 event->attr_trans.trans_id,
1909 event->attr_trans.attr_handle,
1910 event->attr_trans.offset,
1918 _bt_send_event(BT_GATT_SERVER_EVENT,
1919 BLUETOOTH_EVENT_GATT_SERVER_VALUE_CHANGED,
1925 static void __bt_handle_gatt_server_read_requested(event_gatts_srvc_read_attr_t *event)
1927 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1928 bluetooth_device_address_t dev_addr;
1929 int result = BLUETOOTH_ERROR_NONE;
1930 struct gatt_server_req_info *req_info = NULL;
1931 GVariant *param = NULL;
1934 memcpy(dev_addr.addr, event->address.addr, 6);
1935 _bt_convert_addr_type_to_string(address,
1936 (unsigned char *)dev_addr.addr);
1938 BT_DBG("conn_id %d, trans id %d, attr handle %d, offset %d, is_long %d, addr %s",
1939 event->attr_trans.conn_id, event->attr_trans.trans_id,
1940 event->attr_trans.attr_handle, event->attr_trans.offset,
1941 event->is_long, address);
1943 is_long = event->is_long;
1945 /* Save Read Request Info */
1946 req_info = g_new0(struct gatt_server_req_info, 1);
1947 req_info->request_id = event->attr_trans.trans_id;
1948 req_info->attribute_handle = event->attr_trans.attr_handle;
1949 req_info->connection_id = event->attr_trans.conn_id;
1950 req_info->addr = address;
1951 req_info->offset = event->attr_trans.offset;
1952 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_READ;
1953 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1955 /* Send event to BT-API */
1956 param = g_variant_new("(iiiiibs)", result,
1957 event->attr_trans.conn_id,
1958 event->attr_trans.trans_id,
1959 event->attr_trans.attr_handle,
1960 event->attr_trans.offset,
1964 _bt_send_event(BT_GATT_SERVER_EVENT,
1965 BLUETOOTH_EVENT_GATT_SERVER_READ_REQUESTED,
1969 static void __bt_handle_gatt_server_indicate_confirmed(event_gatts_ind_cnfrm_t *event)
1971 bluetooth_device_address_t dev_addr;
1972 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1973 int cur_connected_clients;
1974 static int recvd = 0;
1975 gboolean completed = 0;
1976 GVariant *param = NULL;
1978 /* OAL event does provide error, so MW assumes event will never contain wrong data,
1979 incase of any issues, check with OAL */
1980 int result = BLUETOOTH_ERROR_NONE;
1982 memcpy(dev_addr.addr, event->address.addr, 6);
1983 _bt_convert_addr_type_to_string(address,
1984 (unsigned char *)dev_addr.addr);
1986 BT_INFO("Indication sent to GATT client [%s] conn_ ID [%d] transaction ID [%d] Att handle [%d]",
1987 address, event->conn_id, event->trans_id, event->attr_handle);
1990 cur_connected_clients = g_slist_length(gatt_client_info_list);
1991 BT_INFO("Number of connected clients during sending Indication [%d] & current connected count [%d]",
1992 num_indicate_clients, cur_connected_clients);
1995 if (recvd == num_indicate_clients) {
1996 BT_INFO("Gatt indication confirm event for last GATT client.. [%s]", address);
1997 completed = 1; /* Last event */
1998 recvd = 0; /* Reset */
1999 num_indicate_clients = 0;
2002 param = g_variant_new("(isib)",
2008 /* Send event to BT-API */
2009 _bt_send_event(BT_GATT_SERVER_EVENT,
2010 BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_COMPLETED,
2013 BT_INFO("Received Indication confirm for client number [%d]", recvd);
2017 /* Tizen Platform Specific */
2018 static void __bt_handle_gatt_server_notification_changed(event_gatts_notif_t *event)
2020 bluetooth_device_address_t dev_addr;
2021 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2022 GVariant *param = NULL;
2025 /* OAL event does provide error, so MW assumes event will never contain wrong data,
2026 incase of any issues, check with OAL */
2027 int result = BLUETOOTH_ERROR_NONE;
2029 memcpy(dev_addr.addr, event->address.addr, 6);
2030 _bt_convert_addr_type_to_string(address,
2031 (unsigned char *)dev_addr.addr);
2033 BT_INFO("notification_changed [%s] conn_ ID [%d] transaction ID [%d] Att handle [%d] Notify[%d]",
2034 address, event->conn_id, event->trans_id, event->attr_handle, event->notify);
2036 /* Set Notifcation status */
2037 notify = event->notify;
2039 param = g_variant_new("(isib)",
2045 /* Send event to BT-API */
2046 _bt_send_event(BT_GATT_SERVER_EVENT,
2047 BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_STATE_CHANGED,
2053 static void __bt_handle_gatt_mtu_changed_event(event_gatts_mtu_changed_t *event)
2055 int result = BLUETOOTH_ERROR_NONE;
2056 struct gatt_client_info_t *conn_info = NULL;
2057 GVariant *param = NULL;
2060 conn_info = __bt_find_remote_gatt_client_info_from_conn_id(event->conn_id);
2061 if (conn_info == NULL) {
2062 BT_ERR("Cant find connection Information");
2065 BT_INFO("Got connection Info GATT client [%s] MTU Size [%d]",
2066 conn_info->addr, event->mtu_size);
2068 __bt_update_mtu_gatt_device(conn_info->addr, event->mtu_size);
2070 param = g_variant_new("(isqy)",
2076 /* Send event to BT-API */
2077 _bt_send_event(BT_GATT_SERVER_EVENT,
2078 BLUETOOTH_EVENT_GATT_ATT_MTU_CHANGED,
2082 static void __bt_gatt_event_handler(int event_type, gpointer event_data)
2084 switch (event_type) {
2085 case OAL_EVENT_BLE_SERVER_INSTANCE_INITIALISED: {
2086 BT_INFO("OAL Event: Server Instance Registered");
2087 /* GATT Server Registered event is handled in MAIN thread context */
2088 __bt_handle_server_instance_registered((event_gatts_register_t *)event_data);
2091 case OAL_EVENT_GATTS_SERVICE_ADDED: {
2092 BT_INFO("OAL Event: GATT Service added");
2093 __bt_handle_gatt_server_service_added((event_gatts_srvc_prm_t *)event_data);
2096 case OAL_EVENT_GATTS_CHARACTERISTIC_ADDED: {
2097 BT_INFO("OAL Event: GATT characteristic added");
2098 __bt_handle_gatt_server_characteristic_added((event_gatts_srvc_charctr_t *)event_data);
2101 case OAL_EVENT_GATTS_DESCRIPTOR_ADDED: {
2102 BT_INFO("OAL Event: GATT descriptor added");
2103 __bt_handle_gatt_server_descriptor_added((event_gatts_srvc_descr_t *)event_data);
2106 case OAL_EVENT_GATTS_SERVICE_STARTED: {
2107 BT_INFO("OAL Event: GATT Service started");
2108 __bt_handle_gatt_server_service_started((event_gatts_srvc_t *)event_data);
2111 case OAL_EVENT_GATTS_SERVICE_STOPED: {
2112 BT_INFO("OAL Event: GATT Service stopped");
2113 __bt_handle_gatt_server_service_stopped((event_gatts_srvc_t *)event_data);
2116 case OAL_EVENT_GATTS_SERVICE_DELETED: {
2117 BT_INFO("OAL Event: GATT Service deleted");
2118 __bt_handle_gatt_server_service_deleted((event_gatts_srvc_t *) event_data);
2121 case OAL_EVENT_GATTS_CONNECTION_COMPLETED: {
2122 BT_INFO("OAL Event: GATT Server Connected");
2123 __bt_handle_gatt_server_connection_state((event_gatts_conn_t *)event_data);
2126 case OAL_EVENT_GATTS_DISCONNECTION_COMPLETED: {
2127 BT_INFO("OAL Event: GATT Server Disconnected");
2128 __bt_handle_gatt_server_disconnection_state((event_gatts_conn_t *)event_data);
2131 case OAL_EVENT_GATTS_REQUEST_READ: {
2132 BT_DBG("OAL Event: GATT Server Read Request");
2133 __bt_handle_gatt_server_read_requested((event_gatts_srvc_read_attr_t *)event_data);
2136 case OAL_EVENT_GATTS_REQUEST_WRITE: {
2137 BT_DBG("OAL Event: GATT Server Write Request");
2138 __bt_handle_gatt_server_write_requested((event_gatts_srvc_write_attr_t *)event_data);
2141 case OAL_EVENT_GATTS_REQUEST_ACQUIRE_WRITE: {
2142 BT_INFO("OAL Event: GATT Server Acquire Write Request");
2143 __bt_handle_gatt_server_acquire_write_requested((event_gatts_srvc_acquire_attr_t*)event_data);
2146 case OAL_EVENT_GATTS_REQUEST_ACQUIRE_NOTIFY: {
2147 BT_INFO("OAL Event: GATT ServerAcquire Notify Request");
2148 __bt_handle_gatt_server_acquire_notify_requested((event_gatts_srvc_acquire_attr_t*)event_data);
2151 case OAL_EVENT_GATTS_IND_CONFIRM: {
2152 BT_INFO("OAL Event: GATT Server Indication confirmed");
2153 __bt_handle_gatt_server_indicate_confirmed((event_gatts_ind_cnfrm_t *)event_data);
2156 case OAL_EVENT_GATTS_NOTIFICATION: { /* Tizen Platform Specific */
2157 BT_INFO("OAL Event: GATT Server DisConnected");
2158 __bt_handle_gatt_server_notification_changed((event_gatts_notif_t *)event_data);
2161 case OAL_EVENT_GATTS_MTU_CHANGED: {
2162 BT_INFO("OAL Event: GATT Server MTU changed event callback");
2163 __bt_handle_gatt_mtu_changed_event((event_gatts_mtu_changed_t *)event_data);
2166 #ifdef TIZEN_GATT_CLIENT
2167 case OAL_EVENT_GATTC_REGISTRATION: {
2168 BT_INFO("OAL Event: GATT Client instance Registered");
2169 __bt_handle_client_instance_registered((event_gattc_register_t *) event_data);
2172 case OAL_EVENT_GATTC_CONNECTION_COMPLETED: {
2173 BT_INFO("OAL Event: GATT Client Connected");
2174 __bt_handle_client_connected((event_gattc_conn_t *) event_data);
2177 case OAL_EVENT_GATTC_DISCONNECTION_COMPLETED: {
2178 BT_INFO("OAL Event: GATT Client DisConnected");
2179 __bt_handle_client_disconnected((event_gattc_conn_t *) event_data);
2182 case OAL_EVENT_GATTC_SERVICE_SEARCH_RESULT: {
2183 BT_DBG("OAL Event: GATT Client Service Search Result");
2184 __bt_handle_client_service_search_result((event_gattc_service_result_t *) event_data);
2187 case OAL_EVENT_GATTC_SERVICE_SEARCH_DONE: {
2188 BT_INFO("OAL Event: GATT Client Service Completed");
2189 __bt_handle_client_service_search_completed((event_gattc_conn_status_t *) event_data);
2192 case OAL_EVENT_GATTC_CHARAC_SERACH_RESULT: {
2193 BT_DBG("OAL Event: GATT Client Characteristic Search Result");
2194 __bt_handle_client_characteristic_search_result((event_gattc_characteristic_result_t *) event_data);
2197 case OAL_EVENT_GATTC_DESC_SERACH_RESULT: {
2198 BT_DBG("OAL Event: GATT Client Descriptor Search Result");
2199 __bt_handle_client_descriptor_search_result((event_gattc_descriptor_result_t *) event_data);
2202 case OAL_EVENT_GATTC_READ_CHARAC: {
2203 BT_DBG("OAL Event: GATT Client Characteristic Read Data");
2204 __bt_handle_client_characteristic_read_data((event_gattc_read_data *) event_data);
2207 case OAL_EVENT_GATTC_READ_DESCR: {
2208 BT_DBG("OAL Event: GATT Client Descriptor Read Data");
2209 __bt_handle_client_descriptor_read_data((event_gattc_read_data *) event_data);
2212 case OAL_EVENT_GATTC_WRITE_CHARAC: {
2213 BT_DBG("OAL Event: GATT Client Characteristic Write Data");
2214 __bt_handle_client_characteristic_write_data((event_gattc_write_data *) event_data);
2217 case OAL_EVENT_GATTC_WRITE_DESCR: {
2218 BT_DBG("OAL Event: GATT Client Descriptor Write Data");
2219 __bt_handle_client_descriptor_write_data((event_gattc_write_data *) event_data);
2222 case OAL_EVENT_DEVICE_LE_DISCONNECTED: {
2223 BT_INFO("OAL Event: LE device disconnected");
2224 __bt_hanlde_le_device_disconnection((event_dev_conn_status_t *)event_data);
2227 case OAL_EVENT_GATTC_NOTIFICATION_REGISTERED: {
2228 BT_INFO("OAL Event: GATT Client Notification Registered");
2229 __bt_handle_client_notification_registered((event_gattc_regdereg_notify_t *) event_data, TRUE);
2232 case OAL_EVENT_GATTC_NOTIFICATION_DEREGISTERED: {
2233 BT_INFO("OAL Event: GATT Client Notification Registered");
2234 __bt_handle_client_notification_registered((event_gattc_regdereg_notify_t *) event_data, FALSE);
2237 case OAL_EVENT_GATTC_NOTIFY_DATA: {
2238 BT_DBG("OAL Event: GATT Client Notification Data");
2239 __bt_handle_client_notification_data((event_gattc_notify_data *) event_data);
2242 case OAL_EVENT_GATTC_SERVICE_CHANGED_IND: {
2243 BT_INFO("OAL Event: GATT Client service changed indication");
2244 __bt_handle_client_service_changed_ind((event_gattc_service_changed_data *)event_data);
2247 case OAL_EVENT_GATTC_MTU_EXCHANGE_COMPLETED: {
2248 BT_INFO("OAL Event: GATT Client MTU Exchange Complete");
2249 __bt_handle_client_mtu_exchange_completed((event_gattc_mtu_configured_t *) event_data);
2254 BT_DBG("Unhandled OAL event = 0x%x", event_type);
2259 int _bt_gatt_server_add_service(char *sender, int service_type,
2260 int num_handles, char *svc_uuid, int instance_id)
2262 BT_CHECK_PARAMETER(svc_uuid, return);
2263 BT_CHECK_PARAMETER(sender, return);
2264 int ret = OAL_STATUS_SUCCESS;
2266 oal_gatt_srvc_id_t svc_data;
2268 svc_data.is_prmry = service_type;
2269 svc_data.id.inst_id = instance_id;
2271 BT_INFO("Service UUID [%s] Num handles [%d] Instance ID [%d]", svc_uuid, num_handles, instance_id);
2272 _bt_string_to_uuid(svc_uuid, (service_uuid_t*)&svc_data.id.uuid);
2274 ret = gatts_add_service(instance_id, &svc_data, num_handles);
2275 if (ret != OAL_STATUS_SUCCESS) {
2276 BT_ERR("ret: %d", ret);
2277 return _bt_convert_oal_status_to_bt_error(ret);
2280 return BLUETOOTH_ERROR_NONE;
2284 int _bt_gatt_server_add_included_service(char *sender, int instance_id,
2285 int service_handle, int included_svc_handle)
2287 BT_CHECK_PARAMETER(sender, return);
2288 int ret = OAL_STATUS_SUCCESS;
2290 ret = gatts_add_included_services(instance_id, service_handle, included_svc_handle);
2291 if (ret != OAL_STATUS_SUCCESS) {
2292 BT_ERR("ret: %d", ret);
2293 return _bt_convert_oal_status_to_bt_error(ret);
2295 return BLUETOOTH_ERROR_NONE;
2298 int _bt_gatt_server_add_characteristic(char *sender, char *char_uuid,
2299 bluetooth_gatt_server_attribute_params_t *param)
2301 BT_CHECK_PARAMETER(char_uuid, return);
2302 BT_CHECK_PARAMETER(sender, return);
2303 BT_CHECK_PARAMETER(param, return);
2304 int ret = OAL_STATUS_SUCCESS;
2306 oal_uuid_t uuid = {{0} };
2308 BT_INFO("Char UUID [%s] Instance ID [%d]", char_uuid, param->instance_id);
2309 _bt_string_to_uuid(char_uuid, (service_uuid_t*)&uuid);
2311 BT_INFO("Char permission From API [0x%x]", param->permissions);
2313 ret = gatts_add_characteristics(param->instance_id, param->service_handle, &uuid,
2314 param->properties, (int)param->permissions);
2315 if (ret != OAL_STATUS_SUCCESS) {
2316 BT_ERR("ret: %d", ret);
2317 return _bt_convert_oal_status_to_bt_error(ret);
2319 return BLUETOOTH_ERROR_NONE;
2322 int _bt_gatt_server_add_descriptor(char *sender, char *desc_uuid,
2323 bt_gatt_permission_t *param, int service_handle, int instance_id)
2325 BT_CHECK_PARAMETER(desc_uuid, return);
2326 BT_CHECK_PARAMETER(sender, return);
2327 BT_CHECK_PARAMETER(param, return);
2328 int ret = OAL_STATUS_SUCCESS;
2330 oal_uuid_t uuid = {{0} };
2332 BT_INFO("Descriptor UUID [%s] Instance ID [%d] Service handle [%d]",
2333 desc_uuid, service_handle, instance_id);
2335 _bt_string_to_uuid(desc_uuid, (service_uuid_t*)&uuid);
2337 BT_INFO("Descriptor permission From API [0x%x]", *param);
2338 ret = gatts_add_descriptor(instance_id, service_handle, &uuid, (int)*param);
2340 if (ret != OAL_STATUS_SUCCESS) {
2341 BT_ERR("ret: %d", ret);
2342 return _bt_convert_oal_status_to_bt_error(ret);
2344 return BLUETOOTH_ERROR_NONE;
2347 int _bt_gatt_server_start_service(char *sender, int service_handle, int instance_id)
2349 BT_CHECK_PARAMETER(sender, return);
2350 int ret = OAL_STATUS_SUCCESS;
2352 ret = gatts_start_service(instance_id, service_handle, BT_GATT_TRANSPORT_LE);
2353 if (ret != OAL_STATUS_SUCCESS) {
2354 BT_ERR("ret: %d", ret);
2355 return _bt_convert_oal_status_to_bt_error(ret);
2357 return BLUETOOTH_ERROR_NONE;
2360 int _bt_gatt_server_stop_service(char *sender, int service_handle, int instance_id)
2362 BT_CHECK_PARAMETER(sender, return);
2363 int ret = OAL_STATUS_SUCCESS;
2365 ret = gatts_stop_service(instance_id, service_handle);
2366 if (ret != OAL_STATUS_SUCCESS) {
2367 BT_ERR("ret: %d", ret);
2368 return _bt_convert_oal_status_to_bt_error(ret);
2370 return BLUETOOTH_ERROR_NONE;
2373 int _bt_gatt_server_delete_service(char *sender, int service_handle, int instance_id)
2375 BT_CHECK_PARAMETER(sender, return);
2376 int ret = OAL_STATUS_SUCCESS;
2380 bt_service_app_info_t *info = NULL;
2382 ret = gatts_delete_service(instance_id, service_handle);
2383 if (ret != OAL_STATUS_SUCCESS) {
2384 BT_ERR("ret: %d", ret);
2385 return _bt_convert_oal_status_to_bt_error(ret);
2388 /* Remove the Service Handle */
2389 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
2391 if (info->instance_id == instance_id) {
2392 for (l = info->service_handles; l != NULL; ) {
2394 l = g_slist_next(l);
2395 if (handle && *handle == service_handle) {
2396 BT_INFO("Remove Service handle [%d]", *handle);
2397 info->service_handles = g_slist_remove(info->service_handles, handle);
2406 return BLUETOOTH_ERROR_NONE;
2409 int _bt_gatt_server_send_response(char *sender, bluetooth_gatt_att_data_t *data,
2410 bluetooth_gatt_server_response_params_t *param)
2412 BT_CHECK_PARAMETER(sender, return);
2413 BT_CHECK_PARAMETER(data, return);
2414 BT_CHECK_PARAMETER(param, return);
2415 struct gatt_server_req_info *req_info = NULL;
2416 int ret = OAL_STATUS_SUCCESS;
2418 oal_gatt_response_t response;
2420 BT_DBG("GATT Server Response: Req Type [%d] req_id [%d] status [%d] auth_req [%d] offset[%d] data len[%d]",
2421 param->req_type, param->request_id,
2422 param->response_status, param->auth_req,
2423 data->offset, data->length);
2425 /* Search for matching Request in List */
2426 req_info = __bt_gatt_server_find_request_info(param->request_id, param->req_type);
2428 BT_ERR("GATT Server Req Info not found for current response..return Error");
2429 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
2432 memset(&response, 0x00, sizeof(oal_gatt_response_t));
2434 response.handle = req_info->attribute_handle;
2435 response.attr_value.auth_req = param->auth_req;
2436 response.attr_value.handle = req_info->attribute_handle;
2437 response.attr_value.offset = data->offset;
2438 response.attr_value.len = data->length;
2439 memcpy(&response.attr_value.value, &data->data, data->length);
2442 ret = gatts_send_response(req_info->connection_id, param->request_id,
2443 param->response_status, &response);
2445 if (ret != OAL_STATUS_SUCCESS) {
2446 BT_ERR("ret: %d", ret);
2447 return _bt_convert_oal_status_to_bt_error(ret);
2450 /* Remove GATT server request from list */
2451 gatt_server_requests = g_slist_remove(gatt_server_requests, req_info);
2452 g_free(req_info->addr);
2454 return BLUETOOTH_ERROR_NONE;
2457 int _bt_gatt_server_acquire_send_response(char *sender, bluetooth_gatt_server_acquire_response_params_t *param , void *fd_list)
2459 BT_CHECK_PARAMETER(sender, return);
2460 BT_CHECK_PARAMETER(param, return);
2461 struct gatt_server_req_info *req_info = NULL;
2462 int ret = OAL_STATUS_SUCCESS;
2465 BT_INFO("GATT acquire Server Response: Req Type [%d] req_id [%d] fd [%d] mtu[%d]",
2466 param->req_type, param->request_id,
2470 /* Search for matching Request in List */
2471 req_info = __bt_gatt_server_find_request_info(param->request_id, param->req_type);
2473 BT_ERR("GATT acquire Server Req Info not found for current response..return Error");
2474 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
2477 ret = gatt_send_response_acquire(req_info->connection_id, param->request_id, 0, param->fd, param->mtu, fd_list);
2479 if (ret != OAL_STATUS_SUCCESS) {
2480 BT_ERR("ret: %d", ret);
2481 return _bt_convert_oal_status_to_bt_error(ret);
2484 /* Remove GATT server request from list */
2485 gatt_server_requests = g_slist_remove(gatt_server_requests, req_info);
2486 g_free(req_info->addr);
2488 return BLUETOOTH_ERROR_NONE;
2493 int _bt_gatt_server_send_indication(char *sender, bluetooth_device_address_t *dev_addr,
2494 bluetooth_gatt_att_data_t *data,
2495 bluetooth_gatt_server_indication_params_t *param)
2497 BT_CHECK_PARAMETER(sender, return);
2498 BT_CHECK_PARAMETER(data, return);
2499 BT_CHECK_PARAMETER(param, return);
2501 gboolean all_send = FALSE;
2502 int ret = OAL_STATUS_SUCCESS;
2503 struct gatt_client_info_t *conn;
2505 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2506 _bt_convert_addr_type_to_string(address, dev_addr->addr);
2508 if (memcmp(dev_addr->addr, BDADDR_ANY, 6) == 0) {
2509 BT_INFO("GATT Server: Send Indication to all connected GATT clients..");
2512 BT_INFO("GATT Server: Send Indication to connected GATT client addr [%s]", address);
2515 /* Attempt to send Notification/Indication to all Connected GATT clients */
2517 ret = __bt_gatt_send_indication_to_all_connected_clients(data, param);
2518 if (ret != OAL_STATUS_SUCCESS) {
2519 BT_ERR("ret: %d", ret);
2521 return _bt_convert_oal_status_to_bt_error(ret);
2525 conn = _bt_find_remote_gatt_client_info(address);
2527 ret = gatts_send_indication(param->instance_id, param->atrribute_handle,
2528 conn->connection_id, data->length,
2529 param->need_confirmation, (char *)(&data->data[0]));
2531 if (ret != OAL_STATUS_SUCCESS) {
2532 BT_ERR("ret: %d", ret);
2533 BT_INFO("Indication failed to send to Remote GATT Client [%s]", address);
2535 return _bt_convert_oal_status_to_bt_error(ret);
2537 BT_INFO("Indication sent to Remote GATT Client [%s] wait for Notification completed event from OAL", address);
2539 num_indicate_clients = 1;
2540 return BLUETOOTH_ERROR_NONE;
2542 BT_ERR("Remote GATT client [%s] is not connected..Cant send Indication!!", address);
2544 return BLUETOOTH_ERROR_NOT_CONNECTED;
2547 return BLUETOOTH_ERROR_NONE;
2550 int _bt_gatt_server_update_attribute_value(char *sender, int instance_id,
2551 bluetooth_gatt_server_update_value_t *param)
2553 BT_CHECK_PARAMETER(sender, return);
2554 BT_CHECK_PARAMETER(param, return);
2555 int ret = OAL_STATUS_SUCCESS;
2557 oal_gatt_value_t value;
2558 BT_DBG("GATT Server Update value: Instance ID [%d] attr handle [%d] Value len [%d]",
2559 instance_id, param->attribute_handle, param->length);
2561 memset(&value, 0x00, sizeof(oal_gatt_value_t));
2563 value.handle = param->attribute_handle;
2564 value.len = param->length;
2565 memcpy(&value.value, ¶m->data.data, param->length);
2567 ret = gatts_update_att_value(instance_id, &value);
2569 if (ret != OAL_STATUS_SUCCESS) {
2570 BT_ERR("ret: %d", ret);
2571 return _bt_convert_oal_status_to_bt_error(ret);
2574 return BLUETOOTH_ERROR_NONE;
2577 int _bt_request_att_mtu(bluetooth_device_address_t *device_address,
2580 struct gatt_server_info_t *conn_info = NULL;
2582 int ret = OAL_STATUS_SUCCESS;
2584 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
2585 _bt_convert_addr_type_to_string(addr, device_address->addr);
2587 /* Check if remote GATT Server is connected or not */
2588 conn_info = _bt_find_remote_gatt_server_info(addr);
2589 if (conn_info == NULL) {
2590 BT_ERR("GATT Server is not yet connected..");
2592 return BLUETOOTH_ERROR_NOT_CONNECTED;
2595 ret = gattc_configure_mtu(conn_info->connection_id, mtu);
2596 if (ret != OAL_STATUS_SUCCESS) {
2597 BT_ERR("ret: %d", ret);
2599 return _bt_convert_oal_status_to_bt_error(ret);
2603 return BLUETOOTH_ERROR_NONE;
2606 int _bt_get_att_mtu(bluetooth_device_address_t *address,
2609 BT_CHECK_PARAMETER(address, return);
2610 BT_CHECK_PARAMETER(mtu, return);
2611 struct gatt_client_info_t *client_info = NULL;
2612 char addr[BT_ADDRESS_STRING_SIZE] = { 0 };
2613 int ret = OAL_STATUS_SUCCESS;
2616 _bt_convert_addr_type_to_string(addr, address->addr);
2618 BT_INFO("Get current MTU size for the remote client:DevAddress:[%s]", addr);
2620 client_info = _bt_find_remote_gatt_client_info(addr);
2622 BT_INFO("GATT Client [%s] is connected, conn Id [%d] Instance ID [%d]",
2623 client_info->addr, client_info->connection_id, client_info->instance_id);
2625 ret = gatts_get_att_mtu(client_info->connection_id, &stack_mtu);
2626 if (ret != OAL_STATUS_SUCCESS) {
2627 BT_ERR("ret: %d", ret);
2628 return _bt_convert_oal_status_to_bt_error(ret);
2631 struct gatt_server_info_t *server_info = NULL;
2632 BT_ERR("GATT Client [%s] is not yet connected..", addr);
2633 server_info = _bt_find_remote_gatt_server_info(addr);
2635 BT_INFO("GATT Server [%s] is connected, conn Id [%d] Client ID [%d]",
2636 server_info->addr, server_info->connection_id, server_info->client_id);
2638 ret = gattc_get_att_mtu(server_info->connection_id, &stack_mtu);
2639 if (ret != OAL_STATUS_SUCCESS) {
2640 BT_ERR("ret: %d", ret);
2641 return _bt_convert_oal_status_to_bt_error(ret);
2644 BT_ERR("GATT Server [%s] is not yet connected..", addr);
2645 return BLUETOOTH_ERROR_NOT_CONNECTED;
2649 BT_INFO("ATT MTU received from OAL [%d]", stack_mtu);
2650 *mtu = (unsigned int)stack_mtu;
2653 BT_ERR("MTU value is zero, GATT Client [%s] is not yet connected..", addr);
2654 return BLUETOOTH_ERROR_NOT_CONNECTED;
2657 return BLUETOOTH_ERROR_NONE;
2660 #ifdef TIZEN_GATT_CLIENT
2661 /* GATT Client utility static functions */
2662 static bt_gatt_service_info_list_t *__bt_get_service_info_list(int conn_id)
2665 bt_gatt_service_info_list_t *info = NULL;
2667 for (l = list_gatt_info; l != NULL; l = g_slist_next(l)) {
2668 info = (bt_gatt_service_info_list_t *)l->data;
2672 if (info->conn_id == conn_id)
2679 static bt_gatt_service_info_t *__bt_find_matching_service(
2680 bt_gatt_service_info_list_t *svc_list, oal_gatt_srvc_id_t *svc)
2683 bt_gatt_service_info_t *info = NULL;
2685 for (l = svc_list->services; l != NULL; l = g_slist_next(l)) {
2686 info = (bt_gatt_service_info_t *)l->data;
2690 /* Match UUID and instance ID */
2691 if (!memcmp(&svc->id.uuid.uuid, &info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
2692 && (svc->id.inst_id == info->inst_id)) {
2699 static bt_gatt_char_info_t *__bt_find_matching_charc(
2700 bt_gatt_service_info_t *svc_info, oal_gatt_id_t *charc)
2703 bt_gatt_char_info_t *info = NULL;
2705 for (l = svc_info->chars; l != NULL; l = g_slist_next(l)) {
2706 info = (bt_gatt_char_info_t *)l->data;
2710 /* Match UUID and instance ID */
2711 if (!memcmp(&charc->uuid.uuid, &info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
2712 && (charc->inst_id == info->inst_id)) {
2719 static bt_gatt_descriptor_info_t *__bt_find_matching_desc(
2720 bt_gatt_char_info_t *char_info, oal_gatt_id_t *desc)
2723 bt_gatt_descriptor_info_t *info = NULL;
2725 for (l = char_info->descs; l != NULL; l = g_slist_next(l)) {
2726 info = (bt_gatt_descriptor_info_t *)l->data;
2730 /* Match UUID and instance ID */
2731 if (!memcmp(&desc->uuid, &info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
2732 && (desc->inst_id == info->inst_id)) {
2740 static struct gatt_server_info_t *__bt_find_remote_gatt_server_info_from_conn_id(int conn_id)
2743 struct gatt_server_info_t *info = NULL;
2745 for (l = gatt_server_info_list; l != NULL; l = g_slist_next(l)) {
2746 info = (struct gatt_server_info_t*)l->data;
2750 if (info->connection_id == conn_id)
2756 static bt_gatt_service_info_t* __bt_find_removed_service(bt_gatt_service_info_list_t *svc_list)
2759 bt_gatt_service_info_t *info = NULL;
2761 for (l = svc_list->services; l != NULL; l = g_slist_next(l)) {
2762 info = (bt_gatt_service_info_t*)l->data;
2766 /* Service is marked a removed */
2767 if (info->is_removed == 1)
2773 static void __bt_remove_service_info_from_list(bt_gatt_service_info_t *svc_info)
2779 bt_gatt_char_info_t *charc = NULL;
2780 bt_gatt_included_service_info_t *incl = NULL;
2781 bt_gatt_descriptor_info_t *desc = NULL;
2783 /* Remove all Characteristic and Descriptors within characteristic */
2784 for (l = svc_info->chars; l != NULL;) {
2785 charc = (bt_gatt_char_info_t*)l->data;
2786 l = g_slist_next(l); /* Incase if l is removed, saving next to l */
2791 /* Inside Characteristic */
2792 for (l1 = charc->descs; l1 != NULL;) {
2794 desc = (bt_gatt_descriptor_info_t*)l1->data;
2795 l1 = g_slist_next(l1);
2800 /* Remove Descriptor */
2801 charc->descs = g_slist_remove(charc->descs, desc);
2804 /* Remove Characteristic */
2805 svc_info->chars = g_slist_remove(svc_info->chars, charc);
2809 /* Remove all Included Services */
2810 for (l2 = svc_info->included_svcs; l2 != NULL;) {
2811 incl = (bt_gatt_included_service_info_t*)l2->data;
2812 l2 = g_slist_next(l2); /* Incase if l is removed, saving next to l */
2817 /* Remove included service */
2818 svc_info->included_svcs = g_slist_remove(svc_info->included_svcs, incl);
2825 static void __bt_build_service_browse_info(int conn_id,
2826 bt_services_browse_info_t* info)
2829 bt_gatt_service_info_list_t *svc_info_list;
2830 bt_gatt_service_info_t *svc_info;
2832 service_uuid_t uuid;
2833 struct gatt_server_info_t *conn_info = NULL;
2835 char uuid_string[BLUETOOTH_UUID_STRING_MAX];
2837 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
2838 if (conn_info == NULL) {
2839 BT_ERR("Cant find connection Information");
2843 _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
2845 svc_info_list = __bt_get_service_info_list(conn_id);
2849 info->count = g_slist_length(svc_info_list->services);
2850 BT_DBG("Total services present in the svc info list for this conn id [%d] is [%d]",
2851 conn_id, info->count);
2853 for (l = svc_info_list->services; l != NULL; l = g_slist_next(l)) {
2854 svc_info = (bt_gatt_service_info_t*)l->data;
2855 if (svc_info == NULL)
2858 memcpy(&uuid.uuid, &svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2859 _bt_uuid_to_string(&uuid, uuid_string);
2861 BT_INFO("[%d] %s [%s]", count, uuid_string, _bt_convert_uuid_to_string(uuid_string));
2863 /* Fill UUID of service */
2864 g_strlcpy(info->uuids[count], uuid_string,
2865 BLUETOOTH_UUID_STRING_MAX);
2867 /* Fill instance ID of service */
2868 info->inst_id[count] = svc_info->inst_id;
2870 /* Fill primary service or not info */
2871 info->primary[count] = svc_info->is_primary;
2873 /* Increment count of services browsed */
2878 static void __bt_build_char_browse_info(int conn_id,
2879 bt_gatt_service_info_t *svc_info,
2880 bt_char_browse_info_t* info)
2883 bt_gatt_char_info_t *char_info;
2884 service_uuid_t uuid;
2886 struct gatt_server_info_t *conn_info = NULL;
2888 char uuid_string[BLUETOOTH_UUID_STRING_MAX];
2890 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
2891 if (conn_info == NULL) {
2892 BT_ERR("Cant find connection Information");
2896 /* Fill default data, this will be required even in case of failure */
2897 _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
2898 memcpy(&info->svc_uuid, svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2899 info->svc_inst_id = svc_info->inst_id;
2901 if (!svc_info->chars) {
2902 BT_ERR("No Chars browsed for address [%s]", conn_info->addr);
2906 info->count = g_slist_length(svc_info->chars);
2908 for (l = svc_info->chars; l != NULL; l = g_slist_next(l)) {
2909 char_info = (bt_gatt_char_info_t*)l->data;
2910 if (char_info == NULL)
2913 memcpy(&uuid.uuid, &char_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2914 _bt_uuid_to_string(&uuid, uuid_string);
2916 /* Fill UUID of characteristic */
2917 g_strlcpy(info->uuids[count], uuid_string,
2918 BLUETOOTH_UUID_STRING_MAX);
2920 /* Fill instance ID of characteristic */
2921 info->inst_id[count] = char_info->inst_id;
2923 /* Fill property of characteristic */
2924 info->props[count] = char_info->props;
2926 /* Increment count of services browsed */
2929 BT_DBG("Total characteristics browsed [%d]", count);
2932 static void __bt_build_descriptor_browse_info(int conn_id,
2933 bt_gatt_service_info_t *svc_info,
2934 bt_gatt_char_info_t *char_info,
2935 bt_descriptor_browse_info_t* info)
2938 bt_gatt_descriptor_info_t *desc_info;
2940 service_uuid_t uuid;
2941 struct gatt_server_info_t *conn_info = NULL;
2943 char uuid_string[BLUETOOTH_UUID_STRING_MAX];
2945 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
2947 /* Fill default data, this will be required even in case of failure */
2948 _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
2949 memcpy(&info->svc_uuid, svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2950 info->svc_inst_id = svc_info->inst_id;
2951 memcpy(&info->char_uuid, char_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2952 info->char_inst_id = char_info->inst_id;
2954 /* Fill property of the parent characteristic of this descriptor */
2955 info->char_props_map = char_info->props;
2957 info->count = g_slist_length(char_info->descs);
2959 if (!char_info->descs) {
2960 BT_ERR("No Descriptors browsed for address [%s]", conn_info->addr + 12);
2964 for (l = char_info->descs; l != NULL; l = g_slist_next(l)) {
2965 desc_info = (bt_gatt_descriptor_info_t*)l->data;
2966 if (desc_info == NULL)
2969 memcpy(&uuid.uuid, &desc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2970 _bt_uuid_to_string(&uuid, uuid_string);
2972 /* Fill UUID of Descriptor */
2973 g_strlcpy(info->uuids[count], uuid_string,
2974 BLUETOOTH_UUID_STRING_MAX);
2976 /* Fill instance ID of Descriptor */
2977 info->inst_id[count] = desc_info->inst_id;
2980 /* Increment count of Descriptor browsed */
2984 BT_INFO("Total descriptors browsed [%d]", count);
2987 static void __bt_free_service_info(bt_gatt_service_info_t *svc)
2989 GSList *ll, *lll, *llll;
2990 bt_gatt_char_info_t *chr = NULL;
2991 bt_gatt_descriptor_info_t *desc = NULL;
2992 bt_gatt_included_service_info_t *incl_svc = NULL;
2994 BT_DBG("Service info Is Prim[%d] Inst ID [%d]", svc->is_primary, svc->inst_id);
2995 /* Delete all chars and its descriptors */
2996 for (ll = svc->chars; ll != NULL; ) {
2997 chr = (bt_gatt_char_info_t *)ll->data;
2998 ll = g_slist_next(ll);
3002 for (lll = chr->descs; lll != NULL; ) {
3003 desc = (bt_gatt_descriptor_info_t *)lll->data;
3004 lll = g_slist_next(lll);
3007 chr->descs = g_slist_remove(chr->descs, desc);
3010 svc->chars = g_slist_remove(svc->chars, chr);
3014 /* Delete all included services */
3015 for (llll = svc->included_svcs; llll != NULL; ) {
3016 incl_svc = (bt_gatt_included_service_info_t *)llll->data;
3017 llll = g_slist_next(llll);
3018 if (incl_svc == NULL)
3020 svc->included_svcs = g_slist_remove(svc->included_svcs, incl_svc);
3025 static void __bt_cleanup_remote_services(struct gatt_server_info_t *conn_info)
3027 bt_gatt_service_info_list_t *svc_info_list = NULL;
3028 bt_gatt_service_info_t *svc = NULL;
3032 BT_ERR("conn_info is NULL");
3036 svc_info_list = __bt_get_service_info_list(conn_info->connection_id);
3037 if (!svc_info_list) {
3038 BT_INFO("Could not find Svc Info list for the connection ID [%d]",
3039 conn_info->connection_id);
3043 BT_INFO("Start Cleanup of all services. Num Services [%d]", g_slist_length(svc_info_list->services));
3044 for (l = svc_info_list->services; l != NULL; ) {
3045 svc = (bt_gatt_service_info_t *)l->data;
3046 l = g_slist_next(l);
3050 __bt_free_service_info(svc);
3051 svc_info_list->services = g_slist_remove(svc_info_list->services, svc);
3055 list_gatt_info = g_slist_remove(list_gatt_info, svc_info_list);
3056 g_free(svc_info_list);
3059 int _bt_register_gatt_client_instance(const char *sender,
3060 bluetooth_device_address_t *address)
3062 int ret = OAL_STATUS_SUCCESS;
3063 char *uuid_string = NULL;
3068 /* App should ensure that it should not send */
3069 BT_INFO("Check on which instance GATT Client instance can be initialized....");
3070 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
3071 if (numapps[k].is_initialized == 1) {
3072 BT_DBG("Instance ID [%d] is already in use..Check next slot",
3073 numapps[k].instance_id);
3076 BT_DBG("Time to register GATT client instancer..UUID to be used is [%s] slot [%d]",
3077 uuid_list[slot-1], slot);
3083 BT_ERR("No Slot if free for GATT Client registration..");
3084 return BLUETOOTH_ERROR_REGISTRATION_FAILED;
3087 uuid_string = g_malloc0(BT_UUID_STRING_MAX);
3088 _bt_string_to_uuid(uuid_list[slot-1], (service_uuid_t*)&uuid);
3089 g_strlcpy(uuid_string, uuid_list[slot-1], BT_UUID_STRING_MAX);
3090 BT_INFO("Copied UUID string [%s] slot [%d]", uuid_string, slot);
3092 /* Register GATT Client */
3093 ret = gattc_register(&uuid);
3094 if (ret != OAL_STATUS_SUCCESS) {
3095 BT_ERR("ret: %d", ret);
3096 g_free(uuid_string);
3097 return _bt_convert_oal_status_to_bt_error(ret);
3100 BT_DBG("GATT Client registration call successfully accepted by OAL..wait for Instance Initialized event from OAL..");
3102 /* Return & wait for GATT Client Instance Initialization event */
3103 memset(numapps[slot].sender, 0x00, sizeof(numapps[slot].sender));
3104 memset(numapps[slot].uuid, 0x00, sizeof(numapps[slot].uuid));
3106 g_strlcpy(numapps[slot].sender, sender, sizeof(numapps[slot].sender));
3107 g_strlcpy(numapps[slot].uuid, uuid_string, sizeof(numapps[slot].uuid));
3109 /* Address is saved here. When event comes, sender + address are matched for replying pending
3110 request. It is impossible for same sender to have requests with two same addresses */
3111 memcpy(&numapps[slot].address.addr, address->addr, sizeof(bluetooth_device_address_t));
3113 numapps[slot].is_initialized = TRUE; /* Set initialization to true here itself */
3115 g_free(uuid_string);
3116 return BLUETOOTH_ERROR_NONE;
3122 /* GATT client events */
3123 static void __bt_handle_client_instance_registered(event_gattc_register_t *data)
3125 bt_service_app_info_t *info = NULL;
3127 char *uuid_string = g_malloc0(BT_UUID_STRING_MAX);
3129 _bt_uuid_to_string(&(data->client_uuid), uuid_string);
3130 BT_INFO("Client ID is Initialized [%d] UUID initialized [%s]", data->client_if, uuid_string);
3132 /* Platform GATT client framwork does not use Default GATT client instance
3133 This GATT client instance is never deregistred in the lifetime of bt-service */
3134 if (g_strcmp0(uuid_string, DEFAULT_GATT_CLIENT_UUID) == 0) {
3135 BT_INFO("Default client Instance Registered");
3136 gatt_default_client = data->client_if;
3137 g_free(uuid_string);
3141 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
3144 if (g_strcmp0(info->uuid, uuid_string) == 0) {
3145 BT_INFO("Found GATT client.. sender [%s] Slot [%d] occupied", info->sender, k);
3146 info->is_initialized = TRUE;
3147 info->client_id = data->client_if;
3148 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE, BT_GATT_CLIENT_REGISTER,
3149 (void*)info, sizeof(bt_service_app_info_t));
3153 g_free(uuid_string);
3156 static void __bt_handle_client_connected(event_gattc_conn_t *event_data)
3158 int result = BLUETOOTH_ERROR_NONE;
3159 struct gatt_server_info_t *conn_info = NULL;
3160 struct gatt_out_conn_info_t *out_conn_info = NULL;
3162 GVariant *param = NULL;
3164 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
3165 _bt_convert_addr_type_to_string(address,
3166 (unsigned char *)event_data->address.addr);
3168 if (event_data->status != OAL_STATUS_SUCCESS)
3169 result = BLUETOOTH_ERROR_INTERNAL;
3171 /* DBUS Return fo BT_CONNECT_LE for all the apps */
3172 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE, address,
3173 BT_ADDRESS_STRING_SIZE);
3175 BT_INFO("Local GATT Client Connected: Remote addr[%s] Client Interface [%d] Connection ID [%d] status[%d]",
3176 address, event_data->client_if, event_data->conn_id, event_data->status);
3178 if (result == BLUETOOTH_ERROR_NONE) {
3179 /* Check if device is already in connected list */
3180 conn_info = _bt_find_remote_gatt_server_info(address);
3183 /* Send event to BT-API */
3184 param = g_variant_new("(is)", result, address);
3185 _bt_send_event(BT_DEVICE_EVENT,
3186 BLUETOOTH_EVENT_GATT_CLIENT_CONNECTED, /* Local device is GATT client */
3189 /* Save Connection info */
3190 conn_info = g_new0(struct gatt_server_info_t, 1);
3191 conn_info->addr = g_strdup(address);
3192 conn_info->client_id = event_data->client_if;
3193 #ifdef __INTEGRATE_GATT_INFO__
3194 conn_info->instance_id = -1;
3196 conn_info->connection_id = event_data->conn_id;
3197 gatt_server_info_list = g_slist_append(gatt_server_info_list, conn_info);
3198 BT_DBG("Total num of connected Remote GATT server devices [%d]",
3199 g_slist_length(gatt_server_info_list));
3202 BT_INFO("Do a Internal refresh");
3203 if (OAL_STATUS_SUCCESS != gattc_refresh(conn_info->client_id, &event_data->address))
3204 BT_ERR("GATT database refresh failed!!");
3206 BT_INFO("GATT database refresh Success!!");
3209 BT_ERR("Local GATT Client connected event for addr[%s], but device is in connected list already", address);
3211 __bt_add_mtu_gatt_device(address);
3213 BT_ERR("GATT Client Connection failed!!");
3215 /* If outgoing connection Info is present, then remove it */
3216 out_conn_info = __bt_find_gatt_outgoing_conn_info(address);
3217 if (out_conn_info) {
3218 BT_ERR("Outgoing Client connect request was sent");
3219 outgoing_gatt_conn_list = g_slist_remove(outgoing_gatt_conn_list, out_conn_info);
3220 g_free(out_conn_info->addr);
3221 g_free(out_conn_info);
3226 static void __bt_handle_client_disconnected(event_gattc_conn_t *event_data)
3228 int result = BLUETOOTH_ERROR_NONE;
3230 struct gatt_server_info_t *conn_info = NULL;
3231 struct gatt_out_conn_info_t *out_conn_info = NULL;
3233 GVariant *param = NULL;
3235 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
3236 _bt_convert_addr_type_to_string(address,
3237 (unsigned char *)event_data->address.addr);
3239 if (event_data->status != OAL_STATUS_SUCCESS)
3240 result = BLUETOOTH_ERROR_INTERNAL;
3242 if (NULL == _bt_get_request_info_data(BT_DISCONNECT_LE, address)) {
3243 if (NULL != _bt_get_request_info_data(BT_CONNECT_LE, address)) {
3244 result = BLUETOOTH_ERROR_INTERNAL;
3245 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
3246 address, BT_ADDRESS_STRING_SIZE);
3247 BT_ERR("Failed to connect Local GATT Remote addr[%s]", address);
3252 /* DBUS Return for BT_DISCONNECT_LE for all the apps */
3253 __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE, address,
3254 BT_ADDRESS_STRING_SIZE);
3256 BT_INFO("Local GATT Client DisConnected: Remote addr[%s] Client Interface [%d] Connection ID [%d] status [%d]",
3257 address + 12, event_data->client_if, event_data->conn_id, event_data->status);
3259 /* Remove Connection info */
3260 conn_info = _bt_find_remote_gatt_server_info(address);
3263 param = g_variant_new("(is)", result, address);
3264 /* Send event to application */
3265 _bt_send_event(BT_DEVICE_EVENT,
3266 BLUETOOTH_EVENT_GATT_CLIENT_DISCONNECTED,
3269 BT_INFO("Remove GATT server info from List..");
3270 /* Remove all services from info list_gatt_info */
3271 __bt_cleanup_remote_services(conn_info);
3273 /* Remove info from List */
3274 gatt_server_info_list = g_slist_remove(gatt_server_info_list, conn_info);
3275 BT_INFO("Total num of connected GATT servers [%d]", g_slist_length(gatt_server_info_list));
3276 g_free(conn_info->addr);
3279 BT_INFO("Can not find conn info, already removed!");
3281 __bt_remove_mtu_gatt_device(address);
3283 /* If outgoing connection Info is present, then remove it */
3284 out_conn_info = __bt_find_gatt_outgoing_conn_info(address);
3285 if (out_conn_info) {
3286 BT_ERR("Client Disconnected event, but outgoing connect request was sent");
3287 outgoing_gatt_conn_list = g_slist_remove(outgoing_gatt_conn_list, out_conn_info);
3288 g_free(out_conn_info->addr);
3289 g_free(out_conn_info);
3295 static void __bt_handle_client_service_search_result(
3296 event_gattc_service_result_t *event_data)
3298 /* Pre: status is never fail from OAL */
3300 /* Find service list from address */
3301 bt_gatt_service_info_list_t *svc_info_list;
3302 bt_gatt_service_info_t *svc_info;
3304 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3305 if (!svc_info_list) {
3306 BT_DBG("Service info list not present for connection ID %d, means first time browse", event_data->conn_status.conn_id);
3307 /* Means for this conn_id, no services are ever browsed, first time,
3308 create service info list for this conn_id */
3309 svc_info_list = g_malloc0(sizeof(bt_gatt_service_info_list_t));
3310 svc_info_list->conn_id = event_data->conn_status.conn_id;
3311 list_gatt_info = g_slist_append(list_gatt_info, svc_info_list);
3314 /* send list and current service's uuid and instance id to find it */
3315 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3316 /* If not found, check if service changed, if yes, means this is a new service added
3317 in remote GATT device, update uuid info in svc info list structure, to be used when
3318 search is completed */
3320 if (svc_info_list->info.is_changed) {
3321 BT_DBG("Service Changed indication already found for connection ID %d", event_data->conn_status.conn_id);
3322 memcpy(svc_info_list->info.uuid, event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3324 /* Create and add new service in service list */
3325 svc_info = g_malloc0(sizeof(bt_gatt_service_info_t));
3326 memcpy(svc_info->uuid, event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3327 svc_info->inst_id = event_data->srvc_id.id.inst_id;
3328 svc_info->is_primary = event_data->srvc_id.is_prmry;
3329 svc_info_list->services = g_slist_append(svc_info_list->services, svc_info);
3330 BT_DBG("Service created and added in Svc info list or connection ID %d", event_data->conn_status.conn_id);
3332 /* If returned matching service info, then just update service_rmeoved value inside it to 0 */
3333 svc_info->is_removed = 0;
3337 static void __bt_handle_client_service_search_completed(
3338 event_gattc_conn_status_t *event_data)
3340 struct gatt_server_info_t *conn_info = NULL;
3341 bt_gatt_service_info_list_t *svc_info_list;
3342 bt_gatt_service_info_t *svc_info;
3343 bt_services_browse_info_t browse_info;
3344 unsigned char uuid_empty[BLUETOOTH_UUID_HEX_MAX_LEN];
3346 memset(&uuid_empty, 0x00, BLUETOOTH_UUID_HEX_MAX_LEN);
3347 memset(&browse_info, 0x00, sizeof(bt_services_browse_info_t));
3348 BT_INFO("Primary Services browsing completed status[%d] conn ID [%d]",
3349 event_data->status, event_data->conn_id);
3351 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(event_data->conn_id);
3353 svc_info_list = __bt_get_service_info_list(event_data->conn_id);
3354 if (!svc_info_list) {
3355 BT_ERR("No services browsed ever for addr [%s]", conn_info->addr);
3357 /* Just build response and return ERROR */
3358 __bt_build_service_browse_info(event_data->conn_id, &browse_info);
3360 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_INTERNAL,
3361 BT_GATT_GET_PRIMARY_SERVICES, &browse_info,
3362 sizeof(bt_services_browse_info_t));
3366 /* If fail, then send event with error */
3367 if (event_data->status != OAL_STATUS_SUCCESS) {
3368 /* Just build response and return ERROR */
3369 __bt_build_service_browse_info(event_data->conn_id, &browse_info);
3371 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_INTERNAL,
3372 BT_GATT_GET_PRIMARY_SERVICES, &browse_info,
3373 sizeof(bt_services_browse_info_t));
3377 /* If success, then find service info list from address */
3379 /* If svc_changed == 1 and uuid valid, means a new service is added*/
3380 if (svc_info_list->info.is_changed && !memcmp(uuid_empty, svc_info_list->info.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)) {
3381 /* TODO: Send event -Service added with instance ID and UUID of newly added service */
3382 BT_INFO("new service added");
3384 BT_INFO("TODO new service added");
3387 /* If svc_changed == 1 and uuid invalid, then a service is removed */
3388 if (svc_info_list->info.is_changed && memcmp(uuid_empty, svc_info_list->info.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)) {
3389 /* Scan through the service info list to find service with is_removed = 1*/
3390 svc_info = __bt_find_removed_service(svc_info_list);
3392 /* TODO Send event - Service removed with instance ID and UUID of just rmeoved service */
3394 /* Remove that service info from service info list */
3395 svc_info_list->services = g_slist_remove(svc_info_list->services, svc_info);
3397 /* Delete that service completely from svc_info list*/
3398 __bt_remove_service_info_from_list(svc_info);
3402 /* Reset svc_changed = 0, and reset UUID = all 0's */
3403 svc_info_list->info.is_changed = 0;
3404 memset(&svc_info_list->info.uuid, 0x00, BLUETOOTH_UUID_HEX_MAX_LEN);
3406 /* Build Reply and send to service browse primary services request of pending apps */
3407 __bt_build_service_browse_info(event_data->conn_id, &browse_info);
3409 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE,
3410 BT_GATT_GET_PRIMARY_SERVICES, &browse_info,
3411 sizeof(bt_services_browse_info_t));
3415 static void __bt_handle_client_characteristic_search_result(
3416 event_gattc_characteristic_result_t *event_data)
3418 bt_gatt_service_info_list_t *svc_info_list;
3419 bt_gatt_service_info_t *svc_info;
3420 bt_gatt_char_info_t *char_info;
3421 bt_char_browse_info_t browse_info;
3423 memset(&browse_info, 0x00, sizeof(bt_char_browse_info_t));
3426 if (event_data->conn_status.status == OAL_STATUS_SUCCESS) {
3427 /* Find service info list from address */
3428 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3429 if (svc_info_list == NULL) {
3430 BT_ERR("svc_info_list is NULL");
3434 /* Find matching service info from svc info list */
3435 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3436 if (svc_info == NULL) {
3437 BT_ERR("svc_info is NULL");
3441 /* Find Matching char from service info in event */
3442 char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
3443 /* If not found, then add new characteristic and return */
3445 BT_DBG("Add new characteristic");
3446 char_info = g_malloc0(sizeof(bt_gatt_char_info_t));
3447 memcpy(char_info->uuid, event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3449 char_info->inst_id = event_data->char_id.inst_id;
3450 char_info->props = event_data->char_prop;
3451 svc_info->chars = g_slist_append(svc_info->chars, char_info);
3453 /* If found, then return */
3454 BT_DBG("update char property as Characteristic browsed is already present");
3455 char_info->props |= event_data->char_prop;
3458 /* If Not success: Means Charc browse is completed */
3459 /* Find char list from service in event */
3460 /* Find service list from address */
3461 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3462 if (svc_info_list == NULL) {
3463 BT_ERR("svc_info_list is NULL");
3467 /* Find service info from service in event */
3468 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3469 if (svc_info == NULL) {
3470 BT_ERR("svc_info is NULL");
3474 /* Build char list from service in event */
3475 __bt_build_char_browse_info(event_data->conn_status.conn_id,
3476 svc_info, &browse_info);
3478 /* Create response and return by sending event*/
3479 /* Build Reply and send to service browse All Included services request of pending apps */
3480 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE,
3481 BT_GATT_GET_SERVICE_PROPERTIES,
3483 sizeof(bt_char_browse_info_t));
3487 static void __bt_handle_client_descriptor_search_result(
3488 event_gattc_descriptor_result_t *event_data)
3490 bt_gatt_service_info_list_t *svc_info_list;
3491 bt_gatt_service_info_t *svc_info;
3492 bt_gatt_char_info_t *char_info;
3493 bt_gatt_descriptor_info_t *desc_info;
3494 bt_descriptor_browse_info_t browse_info;
3496 BT_DBG("descriptor search result status [%d]", event_data->conn_status.status);
3498 memset(&browse_info, 0x00, sizeof(bt_descriptor_browse_info_t));
3501 if (event_data->conn_status.status == OAL_STATUS_SUCCESS) {
3502 /* Find service list from address */
3503 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3504 if (svc_info_list == NULL) {
3505 BT_ERR("svc_info_list is NULL");
3509 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3510 if (svc_info == NULL) {
3511 BT_ERR("svc_info is NULL");
3515 char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
3516 if (char_info == NULL) {
3517 BT_ERR("char_info is NULL");
3521 desc_info = __bt_find_matching_desc(char_info, &event_data->descr_id);
3522 /* If not found, add new descriptor and return */
3524 desc_info = g_malloc0(sizeof(bt_gatt_descriptor_info_t));
3525 memcpy(desc_info->uuid, event_data->descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3527 desc_info->inst_id = event_data->descr_id.inst_id;
3528 char_info->descs = g_slist_append(char_info->descs, desc_info);
3531 /* If found, then return */
3532 BT_DBG("Descriptor browsed is already presesnt");
3535 /* If Not success */
3536 /* Find service list from address */
3537 /* Find included service list from service in event */
3538 /* Create response and return by sending event*/
3539 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3540 if (svc_info_list == NULL) {
3541 BT_ERR("svc_info_list is NULL");
3545 /* Find service info from service in event */
3546 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3547 if (svc_info == NULL) {
3548 BT_ERR("svc_info is NULL");
3552 /* Find char info from char in event */
3553 char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
3554 if (char_info == NULL) {
3555 BT_ERR("char_info is NULL");
3559 /* Build descriptor list from char in event */
3560 __bt_build_descriptor_browse_info(event_data->conn_status.conn_id,
3561 svc_info, char_info, &browse_info);
3564 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE,
3565 BT_GATT_GET_CHARACTERISTIC_PROPERTIES,
3567 sizeof(bt_descriptor_browse_info_t));
3571 static void __bt_handle_client_characteristic_read_data(
3572 event_gattc_read_data *event_data)
3574 int result = BLUETOOTH_ERROR_NONE;
3575 struct gatt_server_info_t *conn_info = NULL;
3576 bluetooth_gatt_client_char_prop_info_t read_info;
3578 /* Read Information data structures */
3579 GVariant *param = NULL;
3580 GVariant *data = NULL;
3581 GVariant *data_svc_uuid = NULL;
3582 GVariant *data_char_uuid = NULL;
3583 char *read_val = NULL;
3584 char *svc_uuid = NULL;
3585 char *char_uuid = NULL;
3588 //memset(&read_info, 0x00, sizeof(bt_gatt_handle_property_t));
3589 memset(&read_info, 0x00, sizeof(bluetooth_gatt_client_char_prop_info_t));
3591 /* Extract Address from conn_id of event data */
3592 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3593 event_data->uuid_status.conn_status.conn_id);
3595 BT_INFO("Characteristic Read result from addr [%s] status [%d]",
3596 conn_info->addr, event_data->uuid_status.conn_status.status);
3598 /* Fill char in buffer */
3599 memcpy(&read_info.characteristic.uuid,
3600 event_data->uuid_status.char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3601 read_info.characteristic.instance_id = event_data->uuid_status.char_id.inst_id;
3603 /* Fill Service in buffer */
3604 memcpy(&read_info.svc.uuid,
3605 event_data->uuid_status.srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3606 read_info.svc.instance_id = event_data->uuid_status.srvc_id.id.inst_id;
3608 /* Fill remote device address */
3609 _bt_convert_addr_string_to_type(read_info.device_address.addr, conn_info->addr);
3611 /* Fill data and reply to all apps waiting for Read result on the same characteristic
3612 Note: Even in case of failure, address, handles and result code should be returned */
3613 if (event_data->uuid_status.conn_status.status != OAL_STATUS_SUCCESS)
3614 result = BLUETOOTH_ERROR_INTERNAL;
3616 if (event_data->data_len > 0) {
3618 // for (i = 0; i < event_data->data_len; i++)
3619 // BT_INFO("Data[%d] = [0x%x]", i, event_data->data[i]);
3622 read_val = g_memdup(&event_data->data[0], event_data->data_len);
3624 data = g_variant_new_from_data(
3625 G_VARIANT_TYPE_BYTESTRING,
3627 event_data->data_len,
3630 BT_ERR("Characteristic Read success, but no data!!!");
3632 data = g_variant_new_from_data(
3633 G_VARIANT_TYPE_BYTESTRING,
3640 svc_uuid = g_memdup(&event_data->uuid_status.srvc_id.id.uuid.uuid[0], uuid_len);
3642 data_svc_uuid = g_variant_new_from_data(
3643 G_VARIANT_TYPE_BYTESTRING,
3649 char_uuid = g_memdup(&event_data->uuid_status.char_id.uuid.uuid[0], uuid_len);
3651 data_char_uuid = g_variant_new_from_data(
3652 G_VARIANT_TYPE_BYTESTRING,
3657 param = g_variant_new("(isn@ayin@ayin@ay)", result,
3661 event_data->uuid_status.srvc_id.id.inst_id,
3664 event_data->uuid_status.char_id.inst_id,
3665 event_data->data_len,
3669 char *sender = NULL;
3670 __bt_gatt_get_pending_request_info(BT_GATT_READ_CHARACTERISTIC, &sender);
3671 _bt_send_event_to_dest(sender, BT_GATT_CLIENT_EVENT,
3672 BLUETOOTH_EVENT_GATT_READ_CHAR,
3675 /* Send DBUS return */
3676 __bt_gatt_handle_pending_request_info(result,
3677 BT_GATT_READ_CHARACTERISTIC,
3679 sizeof(bluetooth_gatt_client_char_prop_info_t));
3690 static void __bt_handle_client_descriptor_read_data(
3691 event_gattc_read_data *event_data)
3693 int result = BLUETOOTH_ERROR_NONE;
3694 struct gatt_server_info_t *conn_info = NULL;
3695 bluetooth_gatt_client_desc_prop_info_t read_info;
3697 /* Read Information data structures */
3698 GVariant *param = NULL;
3699 GVariant *data = NULL;
3700 GVariant *data_svc_uuid = NULL;
3701 GVariant *data_char_uuid = NULL;
3702 GVariant *data_desc_uuid = NULL;
3703 char *read_val = NULL;
3704 char *svc_uuid = NULL;
3705 char *char_uuid = NULL;
3706 char *desc_uuid = NULL;
3710 memset(&read_info, 0x00, sizeof(bluetooth_gatt_client_desc_prop_info_t));
3712 /* Extract Address from conn_id of event data */
3713 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3714 event_data->uuid_status.conn_status.conn_id);
3716 BT_DBG("Descriptor Read result from addr [%s] status [%d]",
3717 conn_info->addr, event_data->uuid_status.conn_status.status);
3719 /* Fill descriptor informations in buffer */
3720 memcpy(&read_info.descriptor.uuid,
3721 event_data->uuid_status.descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3722 read_info.descriptor.instance_id = event_data->uuid_status.descr_id.inst_id;
3724 /* Fill Characteristic informations in buffer */
3725 memcpy(&read_info.characteristic.uuid,
3726 event_data->uuid_status.char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3727 read_info.characteristic.instance_id = event_data->uuid_status.char_id.inst_id;
3729 /* Fill Service informations in buffer */
3730 memcpy(&read_info.svc.uuid,
3731 event_data->uuid_status.srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3732 read_info.svc.instance_id = event_data->uuid_status.srvc_id.id.inst_id;
3734 /* Fill remote device address */
3735 _bt_convert_addr_string_to_type(read_info.device_address.addr, conn_info->addr);
3737 /* Fill data and reply to all apps waiting for Read result on the same characteristic */
3738 if (event_data->uuid_status.conn_status.status != OAL_STATUS_SUCCESS)
3739 result = BLUETOOTH_ERROR_INTERNAL;
3741 if (event_data->data_len > 0) {
3743 for (i = 0; i < event_data->data_len; i++)
3744 BT_DBG("Data[%d] = [0x%x]", i, event_data->data[i]);
3747 read_val = g_memdup(&event_data->data[0], event_data->data_len);
3749 data = g_variant_new_from_data(
3750 G_VARIANT_TYPE_BYTESTRING,
3752 event_data->data_len,
3755 BT_INFO("Descriptor Read success, but no data!!!");
3757 data = g_variant_new_from_data(
3758 G_VARIANT_TYPE_BYTESTRING,
3764 svc_uuid = g_memdup(&event_data->uuid_status.srvc_id.id.uuid.uuid[0], uuid_len);
3766 data_svc_uuid = g_variant_new_from_data(
3767 G_VARIANT_TYPE_BYTESTRING,
3773 char_uuid = g_memdup(&event_data->uuid_status.char_id.uuid.uuid[0], uuid_len);
3775 data_char_uuid = g_variant_new_from_data(
3776 G_VARIANT_TYPE_BYTESTRING,
3782 desc_uuid = g_memdup(&event_data->uuid_status.descr_id.uuid.uuid[0], uuid_len);
3784 data_desc_uuid = g_variant_new_from_data(
3785 G_VARIANT_TYPE_BYTESTRING,
3790 param = g_variant_new("(isn@ayin@ayin@ayin@ay)", result,
3794 event_data->uuid_status.srvc_id.id.inst_id,
3797 event_data->uuid_status.char_id.inst_id,
3800 event_data->uuid_status.descr_id.inst_id,
3801 event_data->data_len,
3805 char *sender = NULL;
3806 __bt_gatt_get_pending_request_info(BT_GATT_READ_DESCRIPTOR_VALUE, &sender);
3807 _bt_send_event_to_dest(sender, BT_GATT_CLIENT_EVENT,
3808 BLUETOOTH_EVENT_GATT_READ_DESC,
3812 /* Send DBUS return */
3813 __bt_gatt_handle_pending_request_info(result,
3814 BT_GATT_READ_DESCRIPTOR_VALUE,
3816 sizeof(bluetooth_gatt_client_desc_prop_info_t));
3827 static void __bt_handle_client_characteristic_write_data(
3828 event_gattc_write_data *event_data)
3830 int result = BLUETOOTH_ERROR_NONE;
3831 struct gatt_server_info_t *conn_info = NULL;
3832 bluetooth_gatt_client_char_prop_info_t write_info;
3834 /* Read Information data structures */
3835 GVariant *param = NULL;
3836 GVariant *data_svc_uuid = NULL;
3837 GVariant *data_char_uuid = NULL;
3838 char *svc_uuid = NULL;
3839 char *char_uuid = NULL;
3842 memset(&write_info, 0x00, sizeof(bluetooth_gatt_client_char_prop_info_t));
3844 /* Extract Address from conn_id of event data */
3845 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3846 event_data->conn_status.conn_id);
3848 BT_DBG("Characteristic Write callback from addr [%s] status [%d]",
3849 conn_info->addr, event_data->conn_status.status);
3851 /* Fill char in buffer */
3852 memcpy(&write_info.characteristic.uuid,
3853 event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3854 write_info.characteristic.instance_id = event_data->char_id.inst_id;
3856 /* Fill Service in buffer */
3857 memcpy(&write_info.svc.uuid,
3858 event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3859 write_info.svc.instance_id = event_data->srvc_id.id.inst_id;
3861 /* Fill remote device address */
3862 _bt_convert_addr_string_to_type(write_info.device_address.addr, conn_info->addr);
3864 if (event_data->conn_status.status != OAL_STATUS_SUCCESS) {
3865 result = BLUETOOTH_ERROR_INTERNAL;
3871 svc_uuid = g_memdup(&event_data->srvc_id.id.uuid.uuid[0], uuid_len);
3873 data_svc_uuid = g_variant_new_from_data(
3874 G_VARIANT_TYPE_BYTESTRING,
3880 char_uuid = g_memdup(&event_data->char_id.uuid.uuid[0], uuid_len);
3882 data_char_uuid = g_variant_new_from_data(
3883 G_VARIANT_TYPE_BYTESTRING,
3888 param = g_variant_new("(isn@ayin@ayi)", result,
3892 event_data->srvc_id.id.inst_id,
3895 event_data->char_id.inst_id);
3898 char *sender = NULL;
3899 __bt_gatt_get_pending_request_info(BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE, &sender);
3900 _bt_send_event_to_dest(sender, BT_GATT_CLIENT_EVENT,
3901 BLUETOOTH_EVENT_GATT_WRITE_CHAR,
3911 /* Send DBUS return */
3912 __bt_gatt_handle_pending_request_info(result,
3913 BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE,
3915 sizeof(bluetooth_gatt_client_char_prop_info_t));
3919 static void __bt_handle_client_descriptor_write_data(
3920 event_gattc_write_data *event_data)
3922 int result = BLUETOOTH_ERROR_NONE;
3923 struct gatt_server_info_t *conn_info = NULL;
3924 bluetooth_gatt_client_desc_prop_info_t write_info;
3926 /* Write Information data structures */
3927 GVariant *param = NULL;
3928 GVariant *data_svc_uuid = NULL;
3929 GVariant *data_char_uuid = NULL;
3930 GVariant *data_desc_uuid = NULL;
3931 char *svc_uuid = NULL;
3932 char *char_uuid = NULL;
3933 char *desc_uuid = NULL;
3936 memset(&write_info, 0x00, sizeof(bluetooth_gatt_client_desc_prop_info_t));
3938 /* Extract Address from conn_id of event data */
3939 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3940 event_data->conn_status.conn_id);
3942 if (NULL == conn_info) {
3943 BT_ERR("Failed to get the conn info for conn_id [%d]", event_data->conn_status.conn_id);
3947 BT_DBG("Descriptor Write callback from addr [%s] status [%d]",
3948 conn_info->addr, event_data->conn_status.status);
3950 /* Fill descriptor informations in buffer */
3951 memcpy(&write_info.descriptor.uuid,
3952 event_data->descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3953 write_info.descriptor.instance_id = event_data->descr_id.inst_id;
3955 /* Fill Characteristic informations in buffer */
3956 memcpy(&write_info.characteristic.uuid,
3957 event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3958 write_info.characteristic.instance_id = event_data->char_id.inst_id;
3960 /* Fill Service informations in buffer */
3961 memcpy(&write_info.svc.uuid,
3962 event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3963 write_info.svc.instance_id = event_data->srvc_id.id.inst_id;
3965 /* Fill remote device address */
3966 _bt_convert_addr_string_to_type(write_info.device_address.addr, conn_info->addr);
3968 if (event_data->conn_status.status != OAL_STATUS_SUCCESS) {
3969 result = BLUETOOTH_ERROR_INTERNAL;
3975 svc_uuid = g_memdup(&event_data->srvc_id.id.uuid.uuid[0], uuid_len);
3977 data_svc_uuid = g_variant_new_from_data(
3978 G_VARIANT_TYPE_BYTESTRING,
3984 char_uuid = g_memdup(&event_data->char_id.uuid.uuid[0], uuid_len);
3986 data_char_uuid = g_variant_new_from_data(
3987 G_VARIANT_TYPE_BYTESTRING,
3993 desc_uuid = g_memdup(&event_data->descr_id.uuid.uuid[0], uuid_len);
3995 data_desc_uuid = g_variant_new_from_data(
3996 G_VARIANT_TYPE_BYTESTRING,
4001 param = g_variant_new("(isn@ayin@ayin@ayi)", result,
4005 event_data->srvc_id.id.inst_id,
4008 event_data->char_id.inst_id,
4011 event_data->descr_id.inst_id);
4014 char *sender = NULL;
4015 __bt_gatt_get_pending_request_info(BT_GATT_WRITE_DESCRIPTOR_VALUE, &sender);
4016 _bt_send_event_to_dest(sender, BT_GATT_CLIENT_EVENT,
4017 BLUETOOTH_EVENT_GATT_WRITE_DESC,
4028 /* Send DBUS return */
4029 __bt_gatt_handle_pending_request_info(result,
4030 BT_GATT_WRITE_DESCRIPTOR_VALUE,
4032 sizeof(bluetooth_gatt_client_desc_prop_info_t));
4035 static void __bt_hanlde_le_device_disconnection(event_dev_conn_status_t *event_data)
4037 int result = BLUETOOTH_ERROR_INTERNAL;
4038 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
4040 _bt_convert_addr_type_to_string(address, (unsigned char *)event_data->address.addr);
4042 /* DBUS Return with fail of pending BT_CONNECT_LE for all the apps */
4043 BT_INFO("Local GATT Client disconnected: Remote addr[%s] ", address + 12);
4045 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE, address,
4046 BT_ADDRESS_STRING_SIZE);
4050 static void __bt_handle_client_notification_registered(
4051 event_gattc_regdereg_notify_t *event_data,
4052 gboolean is_registered)
4054 int result = BLUETOOTH_ERROR_NONE;
4055 struct gatt_server_info_t *conn_info = NULL;
4056 bt_gatt_notif_reg_info_t notif_info;
4059 memset(¬if_info, 0x00, sizeof(bt_gatt_notif_reg_info_t));
4061 BT_INFO("Client Interface [%d] status [%d]",
4062 event_data->conn_id,
4063 event_data->status);
4065 /* Extract Address from conn_id of event data */
4066 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(event_data->conn_id);
4068 BT_INFO("Connection Info is not present, return");
4071 BT_INFO("Notification Registered for addr [%s]", conn_info->addr);
4073 /* Fill svc informations in buffer */
4074 memcpy(¬if_info.svc_uuid,
4075 event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4076 notif_info.svc_inst = event_data->srvc_id.id.inst_id;
4078 /* Fill char in buffer */
4079 memcpy(¬if_info.char_uuid,
4080 event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4081 notif_info.char_inst = event_data->char_id.inst_id;
4083 /* Fill remote device address */
4084 _bt_convert_addr_string_to_type(notif_info.addr.addr, conn_info->addr);
4086 notif_info.is_registered = is_registered;
4088 if (event_data->status != OAL_STATUS_SUCCESS)
4089 result = BLUETOOTH_ERROR_INTERNAL;
4091 /* Send DBUS Return for BT_GATT_WATCH_CHARACTERISTIC */
4092 __bt_gatt_handle_pending_request_info(result,
4093 BT_GATT_WATCH_CHARACTERISTIC,
4095 sizeof(bt_gatt_notif_reg_info_t));
4098 static void __bt_handle_client_notification_data(event_gattc_notify_data *event_data)
4100 /* No status in this event from OAL */
4101 int result = BLUETOOTH_ERROR_NONE;
4103 /* Read Information data structures */
4104 GVariant *param = NULL;
4105 GVariant *data = NULL;
4106 GVariant *data_svc_uuid = NULL;
4107 GVariant *data_char_uuid = NULL;
4108 char *read_val = NULL;
4109 char *svc_uuid = NULL;
4110 char *char_uuid = NULL;
4116 BT_INFO("Notifcation of charc data changed");
4118 if (event_data->data_len > 0) {
4120 for (i = 0; i < event_data->data_len; i++)
4121 BT_INFO("Data[%d] = [0x%x]", i, event_data->data[i]);
4124 addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
4125 _bt_convert_addr_type_to_string(addr,
4126 (unsigned char *)&(event_data->address.addr));
4129 read_val = g_memdup(&event_data->data[0], event_data->data_len);
4131 data = g_variant_new_from_data(
4132 G_VARIANT_TYPE_BYTESTRING,
4134 event_data->data_len,
4137 svc_uuid = g_memdup(&event_data->srvc_id.id.uuid.uuid[0], uuid_len);
4139 data_svc_uuid = g_variant_new_from_data(
4140 G_VARIANT_TYPE_BYTESTRING,
4146 char_uuid = g_memdup(&event_data->char_id.uuid.uuid[0], uuid_len);
4148 data_char_uuid = g_variant_new_from_data(
4149 G_VARIANT_TYPE_BYTESTRING,
4155 param = g_variant_new("(isn@ayin@ayin@ay)", result,
4159 event_data->srvc_id.id.inst_id,
4162 event_data->char_id.inst_id,
4163 event_data->data_len,
4167 _bt_send_event(BT_GATT_CLIENT_EVENT,
4168 BLUETOOTH_EVENT_GATT_CHAR_VAL_CHANGED,
4171 BT_ERR("No Data!!");
4184 static void __bt_handle_client_service_changed_ind(event_gattc_service_changed_data *event_data)
4186 bt_gatt_service_info_list_t *svc_info_list;
4188 bt_gatt_service_info_t *svc_info;
4189 GVariant *param = NULL;
4190 char *address_str = NULL;
4191 char *uuid_str = NULL;
4193 svc_info_list = __bt_get_service_info_list(event_data->conn_id);
4194 if (svc_info_list == NULL) {
4195 BT_ERR("svc_info_list is NULL");
4199 if (event_data->change_type) {
4200 /* Add service UUID in list */
4201 svc_info = g_malloc0(sizeof(bt_gatt_service_info_t));
4202 memcpy(svc_info->uuid, event_data->uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4203 svc_info->inst_id = event_data->inst_id;
4204 svc_info->is_primary = 1; // TODO: Need to check is_primary is required or not
4205 svc_info_list->services = g_slist_append(svc_info_list->services, svc_info);
4206 BT_DBG("Service created and added in Svc info list or connection ID %d", event_data->conn_id);
4208 /* Remove service UUID in list */
4209 for (l = svc_info_list->services; l != NULL; l = g_slist_next(l)) {
4210 svc_info = (bt_gatt_service_info_t *)l->data;
4211 if (svc_info == NULL)
4214 if (!memcmp(svc_info->uuid, event_data->uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)) {
4215 svc_info_list->services = g_slist_remove(svc_info_list->services, svc_info);
4216 __bt_free_service_info(svc_info);
4222 address_str = g_malloc0(BT_ADDRESS_STRING_SIZE);
4223 uuid_str = g_malloc0(BT_UUID_STRING_MAX);
4224 _bt_convert_addr_type_to_string(address_str, event_data->address.addr);
4225 _bt_uuid_to_string(&event_data->uuid, uuid_str);
4227 param = g_variant_new("(iiss)", event_data->inst_id, event_data->change_type, address_str, uuid_str);
4229 _bt_send_event(BT_GATT_CLIENT_EVENT,
4230 BLUETOOTH_EVENT_GATT_CLIENT_SERVICE_CHANGED,
4232 g_free(address_str);
4236 gboolean _bt_is_remote_gatt_device_connected(bluetooth_device_address_t *address)
4239 struct gatt_server_info_t *conn_info = NULL;
4240 gboolean connected = FALSE;
4242 addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
4243 _bt_convert_addr_type_to_string(addr,
4244 (unsigned char *)&(address->addr));
4246 BT_DBG("Check GATT connection status of [%s]", addr);
4247 /* Check if device is already in connected list */
4248 conn_info = _bt_find_remote_gatt_server_info(addr);
4250 BT_DBG("Remote GATT Server device [%s] is Connected", conn_info->addr);
4253 struct gatt_client_info_t *client_info = NULL;
4255 BT_DBG("Remote GATT Server Device [%s] is not Connected", addr);
4257 /* Check if device is already in connected list */
4258 client_info = _bt_find_remote_gatt_client_info(addr);
4260 BT_DBG("Remote Client device [%s] is Connected", client_info->addr);
4263 BT_DBG("Remote GATT Client Device [%s] is not Connected", addr);
4271 void _bt_handle_invocation_context(int function_name, void *data)
4273 switch (function_name) {
4275 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE, BT_CONNECT_LE,
4276 (char *)data, BT_ADDRESS_STRING_SIZE);
4283 int _bt_connect_le_device(bluetooth_device_address_t *address,
4284 int auto_connect, int client_id)
4286 struct gatt_server_info_t *conn_info = NULL;
4287 struct gatt_out_conn_info_t *out_conn_info = NULL;
4289 invocation_info_t *req_info = NULL;
4290 int ret = OAL_STATUS_SUCCESS;
4292 char *remote_address = NULL;
4294 BT_CHECK_PARAMETER(address, return);
4296 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4297 _bt_convert_addr_type_to_string(addr, address->addr);
4298 BT_DBG("GATT Client connect request for address [%s] client instance [%d]",
4302 /* Check if Remote Device is already under connection progress */
4303 req_info = _bt_get_request_info_data_from_function_name(BT_CONNECT_LE);
4305 remote_address = (char*)req_info->user_data;
4306 if (remote_address && !strcasecmp(remote_address, addr)) {/* Address matched */
4307 BT_DBG("Already Connection ongoing for same remote GATT Server address [%s]", remote_address);
4308 /* Return and wait for events to be sent to all apps */
4310 return BLUETOOTH_ERROR_IN_PROGRESS;
4314 /* Check if remote GATT Server is connected or not */
4315 conn_info = _bt_find_remote_gatt_server_info(addr);
4317 BT_ERR("GATT Server is already connected..");
4319 return BLUETOOTH_ERROR_ALREADY_CONNECT;
4322 /* TODO Check Requirement of holding Advertisement before initiating LE connect */
4324 /* Check if app sent 0 client id for connection, in such case, use default gatt client ID */
4325 if (client_id == 0) {
4326 /* GATT CLient connect request sent by an app without any client instance [0] */
4327 BT_DBG("Assign default GATT client id [%d]", gatt_default_client);
4328 client_id = gatt_default_client;
4331 BT_INFO("Connect using CLient ID [%d]", client_id);
4332 ret = gattc_connect(client_id, (bt_address_t*)(address), auto_connect);
4334 if (ret != OAL_STATUS_SUCCESS) {
4335 BT_ERR("gattc_connect is failed. ret: %d", ret);
4337 return _bt_convert_oal_status_to_bt_error(ret);
4340 /* Mark this as outgoing connection */
4341 out_conn_info = g_new0(struct gatt_out_conn_info_t, 1);
4342 out_conn_info->addr = g_strdup(addr);
4343 out_conn_info->client_id = client_id;
4344 BT_INFO("Added outgoing connection info addr[%s]", out_conn_info->addr + 12);
4345 outgoing_gatt_conn_list = g_slist_append(outgoing_gatt_conn_list, out_conn_info);
4348 return BLUETOOTH_ERROR_NONE;
4351 int _bt_gatt_get_primary_services(char *address)
4353 BT_CHECK_PARAMETER(address, return);
4354 struct gatt_server_info_t *conn_info = NULL;
4355 invocation_info_t *req_info = NULL;
4356 int ret = OAL_STATUS_SUCCESS;
4358 /* Check if any app is already browsing primary services on the same remote GATT Server */
4359 req_info = _bt_get_request_info_data(BT_GATT_GET_PRIMARY_SERVICES, address);
4361 BT_INFO("Already Primary Service Browsing ongoing for same rmeote GATT Server");
4362 /* Return and wait for events to be sent to all apps */
4363 return BLUETOOTH_ERROR_NONE;
4366 /* Check if remote GATT Server is connected or not */
4367 conn_info = _bt_find_remote_gatt_server_info(address);
4369 BT_ERR("GATT Server is not yet connected..");
4370 return BLUETOOTH_ERROR_NOT_CONNECTED;
4373 BT_INFO("Get all services. GATT Server [%s] is connected, conn Id [%d]",
4374 conn_info->addr + 12, conn_info->connection_id);
4376 /* Send Primary Service Browsing request to stack */
4377 ret = gattc_search_service(conn_info->connection_id, NULL);
4378 if (ret != OAL_STATUS_SUCCESS) {
4379 BT_ERR("ret: %d", ret);
4380 return _bt_convert_oal_status_to_bt_error(ret);
4382 return BLUETOOTH_ERROR_NONE;
4385 int _bt_gatt_get_all_characteristic(bluetooth_gatt_client_svc_prop_info_t *svc)
4387 BT_CHECK_PARAMETER(svc, return);
4388 struct gatt_server_info_t *conn_info = NULL;
4389 invocation_info_t *req_info = NULL;
4390 bluetooth_gatt_client_svc_prop_info_t *prop;
4391 oal_gatt_srvc_id_t srvc_id;
4392 int ret = OAL_STATUS_SUCCESS;
4395 /* Check if any app is already browsing characteristics of the same service on the same remote GATT Server */
4396 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_GET_SERVICE_PROPERTIES);
4398 prop = (bluetooth_gatt_client_svc_prop_info_t*)req_info->user_data;
4399 if (prop && !memcmp(svc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t))
4400 && !memcmp(prop->svc.uuid, svc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
4401 && prop->svc.instance_id == svc->svc.instance_id) {
4402 BT_INFO("Already Properties browsing for Primary Service ongoing for same remote GATT Server");
4403 /* Return and wait for events to be sent to all apps */
4404 return BLUETOOTH_ERROR_NONE;
4408 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4409 _bt_convert_addr_type_to_string(addr, svc->device_address.addr);
4411 /* Check if remote GATT Server is connected or not */
4412 conn_info = _bt_find_remote_gatt_server_info(addr);
4414 BT_ERR("GATT Server is not yet connected..");
4416 return BLUETOOTH_ERROR_NOT_CONNECTED;
4419 BT_DBG("Get all charc. GATT Server [%s] is connected, conn Id [%d]",
4420 conn_info->addr, conn_info->connection_id);
4422 srvc_id.is_prmry = TRUE;
4423 srvc_id.id.inst_id = svc->svc.instance_id;
4424 memcpy(srvc_id.id.uuid.uuid, svc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4426 /* Search All Characteristic */
4427 ret = gattc_get_characteristic(conn_info->connection_id, &srvc_id, NULL);
4428 if (ret != OAL_STATUS_SUCCESS) {
4429 BT_ERR("ret: %d", ret);
4431 return _bt_convert_oal_status_to_bt_error(ret);
4434 return BLUETOOTH_ERROR_NONE;
4437 int _bt_gatt_get_all_characteristic_properties(
4438 bluetooth_gatt_client_char_prop_info_t *chr)
4440 struct gatt_server_info_t *conn_info = NULL;
4441 invocation_info_t *req_info = NULL;
4442 bluetooth_gatt_client_char_prop_info_t *prop;
4443 oal_gatt_srvc_id_t srvc_id;
4444 oal_gatt_id_t char_id;
4445 int ret = OAL_STATUS_SUCCESS;
4448 BT_CHECK_PARAMETER(chr, return);
4450 /* Check if any app is already browsing descriptors of the same char of
4451 particular service on the same remote GATT Server */
4452 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_GET_CHARACTERISTIC_PROPERTIES);
4454 prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
4455 if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4456 && !memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4457 && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4458 && !memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4459 && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
4460 BT_INFO("Already Properties browsing for Characteristic ongoing for same remote GATT Server");
4461 /* Return and wait for events to be sent to all apps */
4462 return BLUETOOTH_ERROR_NONE;
4466 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4467 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4469 /* Check if remote GATT Server is connected or not */
4470 conn_info = _bt_find_remote_gatt_server_info(addr);
4472 BT_ERR("GATT Server is not yet connected..");
4474 return BLUETOOTH_ERROR_NOT_CONNECTED;
4477 BT_DBG("Get all desc. GATT Server [%s] is connected, conn Id [%d]",
4478 conn_info->addr, conn_info->connection_id);
4480 srvc_id.is_prmry = TRUE;
4481 srvc_id.id.inst_id = chr->svc.instance_id;
4482 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4484 char_id.inst_id = chr->characteristic.instance_id;
4485 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4487 /* Search All Descriptors */
4488 ret = gattc_get_descriptor(conn_info->connection_id, &srvc_id, &char_id, NULL);
4489 if (ret != OAL_STATUS_SUCCESS) {
4490 BT_ERR("ret: %d", ret);
4492 return _bt_convert_oal_status_to_bt_error(ret);
4495 return BLUETOOTH_ERROR_NONE;
4498 int _bt_gatt_read_characteristic_value(
4499 bluetooth_gatt_client_char_prop_info_t *chr)
4501 struct gatt_server_info_t *conn_info = NULL;
4502 invocation_info_t *req_info = NULL;
4503 bluetooth_gatt_client_char_prop_info_t *prop;
4504 oal_gatt_srvc_id_t srvc_id;
4505 oal_gatt_id_t char_id;
4506 int ret = OAL_STATUS_SUCCESS;
4509 BT_CHECK_PARAMETER(chr, return);
4511 /* Check if any app is already Reading characteristic of the same char of
4512 particular service on the same remote GATT Server */
4513 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_READ_CHARACTERISTIC);
4515 prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
4516 if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4517 && !memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4518 && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4519 && !memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4520 && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
4521 BT_INFO("Already Characteristic value Read operation in progress for same remote GATT Server");
4522 /* Return and wait for events to be sent to all apps */
4523 return BLUETOOTH_ERROR_NONE;
4527 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4528 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4530 /* Check if remote GATT Server is connected or not */
4531 conn_info = _bt_find_remote_gatt_server_info(addr);
4533 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4534 conn_info->addr, conn_info->connection_id);
4536 BT_ERR("GATT Server is not yet connected..");
4538 return BLUETOOTH_ERROR_NOT_CONNECTED;
4541 srvc_id.is_prmry = TRUE;
4542 srvc_id.id.inst_id = chr->svc.instance_id;
4543 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4545 char_id.inst_id = chr->characteristic.instance_id;
4546 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4548 /* Search All Descriptors */
4549 ret = gattc_read_characteristic(conn_info->connection_id, &srvc_id, &char_id, OAL_GATT_AUTH_REQ_NONE);
4550 if (ret != OAL_STATUS_SUCCESS) {
4551 BT_ERR("ret: %d", ret);
4553 return _bt_convert_oal_status_to_bt_error(ret);
4556 return BLUETOOTH_ERROR_NONE;
4559 int _bt_gatt_read_descriptor_value(
4560 bluetooth_gatt_client_desc_prop_info_t *desc)
4562 struct gatt_server_info_t *conn_info = NULL;
4563 invocation_info_t *req_info = NULL;
4564 bluetooth_gatt_client_desc_prop_info_t *prop;
4565 oal_gatt_srvc_id_t srvc_id;
4566 oal_gatt_id_t char_id;
4567 oal_gatt_id_t desc_id;
4568 int ret = OAL_STATUS_SUCCESS;
4571 BT_CHECK_PARAMETER(desc, return);
4573 /* Check if any app is already Reading descriptors of the same char of
4574 particular service on the same remote GATT Server */
4575 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_READ_DESCRIPTOR_VALUE);
4577 prop = (bluetooth_gatt_client_desc_prop_info_t*)req_info->user_data;
4578 if (prop && !memcmp(desc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4579 && !memcmp(desc->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4580 && desc->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4581 && !memcmp(desc->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4582 && desc->characteristic.instance_id == prop->characteristic.instance_id /* Characteristic Instance ID matched */
4583 && !memcmp(desc->descriptor.uuid, prop->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Descriptor UUID matched */
4584 && desc->descriptor.instance_id == prop->descriptor.instance_id) { /* Descriptor Instance ID matched */
4585 BT_INFO("Already Descriptor value Read operation in progress for same remote GATT Server");
4586 /* Return and wait for events to be sent to all apps */
4587 return BLUETOOTH_ERROR_NONE;
4591 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4592 _bt_convert_addr_type_to_string(addr, desc->device_address.addr);
4594 /* Check if remote GATT Server is connected or not */
4595 conn_info = _bt_find_remote_gatt_server_info(addr);
4597 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4598 conn_info->addr, conn_info->connection_id);
4600 BT_ERR("GATT Server is not yet connected..");
4602 return BLUETOOTH_ERROR_NOT_CONNECTED;
4605 srvc_id.is_prmry = TRUE;
4606 srvc_id.id.inst_id = desc->svc.instance_id;
4607 memcpy(srvc_id.id.uuid.uuid, desc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4609 char_id.inst_id = desc->characteristic.instance_id;
4610 memcpy(char_id.uuid.uuid, desc->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4612 desc_id.inst_id = desc->descriptor.instance_id;
4613 memcpy(desc_id.uuid.uuid, desc->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4615 /* Search All Descriptors */
4616 ret = gattc_read_descriptor(conn_info->connection_id, &srvc_id, &char_id,
4617 &desc_id, OAL_GATT_AUTH_REQ_NONE);
4618 if (ret != OAL_STATUS_SUCCESS) {
4619 BT_ERR("ret: %d", ret);
4621 return _bt_convert_oal_status_to_bt_error(ret);
4624 return BLUETOOTH_ERROR_NONE;
4628 int _bt_gatt_acquire_notify(bluetooth_gatt_client_char_prop_info_t *chr, int *fd, int *mtu)
4630 struct gatt_server_info_t *conn_info = NULL;
4631 oal_gatt_srvc_id_t srvc_id;
4632 oal_gatt_id_t char_id;
4633 int ret = OAL_STATUS_SUCCESS;
4636 BT_CHECK_PARAMETER(chr, return);
4638 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4639 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4641 /* Check if remote GATT Server is connected or not */
4642 conn_info = _bt_find_remote_gatt_server_info(addr);
4644 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4645 conn_info->addr, conn_info->connection_id);
4647 BT_ERR("GATT Server is not yet connected..");
4649 return BLUETOOTH_ERROR_NOT_CONNECTED;
4652 srvc_id.is_prmry = TRUE;
4653 srvc_id.id.inst_id = chr->svc.instance_id;
4654 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4656 char_id.inst_id = chr->characteristic.instance_id;
4657 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4659 ret = gattc_acquire_notify(conn_info->connection_id, &srvc_id, &char_id, fd, mtu);
4661 if (ret != OAL_STATUS_SUCCESS) {
4662 BT_ERR("ret: %d", ret);
4664 return _bt_convert_oal_status_to_bt_error(ret);
4666 BT_INFO("GATT characterstics FD [%d] mtu[%d]", *fd, *mtu);
4668 return BLUETOOTH_ERROR_NONE;
4672 int _bt_gatt_acquire_write(bluetooth_gatt_client_char_prop_info_t *chr, int *fd, int *mtu)
4675 struct gatt_server_info_t *conn_info = NULL;
4676 oal_gatt_srvc_id_t srvc_id;
4677 oal_gatt_id_t char_id;
4678 int ret = OAL_STATUS_SUCCESS;
4681 BT_CHECK_PARAMETER(chr, return);
4683 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4684 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4686 /* Check if remote GATT Server is connected or not */
4687 conn_info = _bt_find_remote_gatt_server_info(addr);
4689 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4690 conn_info->addr, conn_info->connection_id);
4692 BT_ERR("GATT Server is not yet connected..");
4694 return BLUETOOTH_ERROR_NOT_CONNECTED;
4697 srvc_id.is_prmry = TRUE;
4698 srvc_id.id.inst_id = chr->svc.instance_id;
4699 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4701 char_id.inst_id = chr->characteristic.instance_id;
4702 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4704 ret = gattc_acquire_write(conn_info->connection_id, &srvc_id, &char_id,
4705 OAL_GATT_AUTH_REQ_NONE, fd, mtu);
4706 if (ret != OAL_STATUS_SUCCESS) {
4707 BT_ERR("ret: %d", ret);
4709 return _bt_convert_oal_status_to_bt_error(ret);
4711 BT_INFO("GATT characterstics FD [%d] mtu [%d]", *fd, *mtu);
4713 return BLUETOOTH_ERROR_NONE;
4718 /* Write Characteristic */
4719 int _bt_gatt_write_characteristic_value_by_type(
4720 bluetooth_gatt_client_char_prop_info_t *chr,
4721 bluetooth_gatt_att_data_t *data,
4722 bluetooth_gatt_write_type_e write_type)
4724 struct gatt_server_info_t *conn_info = NULL;
4725 invocation_info_t *req_info = NULL;
4726 bluetooth_gatt_client_char_prop_info_t *prop;
4727 oal_gatt_srvc_id_t srvc_id;
4728 oal_gatt_id_t char_id;
4729 int ret = OAL_STATUS_SUCCESS;
4732 BT_CHECK_PARAMETER(chr, return);
4733 BT_CHECK_PARAMETER(data, return);
4735 /* Check if any app is already writing same char of
4736 particular service on the same remote GATT Server */
4737 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE);
4739 prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
4740 if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4741 && !memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4742 && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4743 && !memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4744 && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
4745 BT_INFO("Already Characteristic Write Value operation in progress for same remote GATT Server");
4746 /* Return and wait for events to be sent to all apps */
4747 return BLUETOOTH_ERROR_NONE;
4751 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4752 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4754 /* Check if remote GATT Server is connected or not */
4755 conn_info = _bt_find_remote_gatt_server_info(addr);
4757 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4758 conn_info->addr, conn_info->connection_id);
4760 BT_ERR("GATT Server is not yet connected..");
4762 return BLUETOOTH_ERROR_NOT_CONNECTED;
4765 srvc_id.is_prmry = TRUE;
4766 srvc_id.id.inst_id = chr->svc.instance_id;
4767 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4769 char_id.inst_id = chr->characteristic.instance_id;
4770 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4772 /* Write CHar value */
4773 ret = gattc_write_characteristic(conn_info->connection_id,
4775 (oal_gatt_write_type_t)write_type, data->length,
4776 OAL_GATT_AUTH_REQ_NONE, (char *)(&data->data[0]));
4777 if (ret != OAL_STATUS_SUCCESS) {
4778 BT_ERR("ret: %d", ret);
4780 return _bt_convert_oal_status_to_bt_error(ret);
4783 return BLUETOOTH_ERROR_NONE;
4786 /* Write Descriptor */
4787 int _bt_gatt_write_descriptor_value_by_type(
4788 bluetooth_gatt_client_desc_prop_info_t *desc,
4789 bluetooth_gatt_att_data_t *data,
4790 bluetooth_gatt_write_type_e write_type)
4792 struct gatt_server_info_t *conn_info = NULL;
4793 invocation_info_t *req_info = NULL;
4794 bluetooth_gatt_client_desc_prop_info_t *prop;
4795 oal_gatt_srvc_id_t srvc_id;
4796 oal_gatt_id_t char_id;
4797 oal_gatt_id_t desc_id;
4798 int ret = OAL_STATUS_SUCCESS;
4802 BT_CHECK_PARAMETER(desc, return);
4803 BT_CHECK_PARAMETER(data, return);
4807 /* Check if any app is already writing on same Descriptor of the same char of
4808 particular service on the same remote GATT Server */
4809 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_WRITE_DESCRIPTOR_VALUE);
4811 prop = (bluetooth_gatt_client_desc_prop_info_t*)req_info->user_data;
4812 if (prop && !memcmp(desc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4813 && !memcmp(desc->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4814 && desc->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4815 && !memcmp(desc->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4816 && desc->characteristic.instance_id == prop->characteristic.instance_id /* Characteristic Instance ID matched */
4817 && !memcmp(desc->descriptor.uuid, prop->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Descriptor UUID matched */
4818 && desc->descriptor.instance_id == prop->descriptor.instance_id) { /* Descriptor Instance ID matched */
4819 BT_INFO("Already Descriptor value Write operation in progress for same remote GATT Server");
4820 /* Return and wait for events to be sent to all apps */
4821 return BLUETOOTH_ERROR_NONE;
4825 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4826 _bt_convert_addr_type_to_string(addr, desc->device_address.addr);
4828 /* Check if remote GATT Server is connected or not */
4829 conn_info = _bt_find_remote_gatt_server_info(addr);
4831 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4832 conn_info->addr, conn_info->connection_id);
4834 BT_ERR("GATT Server is not yet connected..");
4836 return BLUETOOTH_ERROR_NOT_CONNECTED;
4839 srvc_id.is_prmry = TRUE;
4840 srvc_id.id.inst_id = desc->svc.instance_id;
4841 memcpy(srvc_id.id.uuid.uuid, desc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4843 char_id.inst_id = desc->characteristic.instance_id;
4844 memcpy(char_id.uuid.uuid, desc->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4846 desc_id.inst_id = desc->descriptor.instance_id;
4847 memcpy(desc_id.uuid.uuid, desc->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4850 BT_INFO("Connection ID [%d] write type [%d] data length [%d]", conn_info->connection_id, write_type, data->length);
4851 for (k = 0; k < data->length; k++)
4852 BT_INFO("Data[%d] [0x%x]", k, data->data[k]);
4854 ret = gattc_write_descriptor(conn_info->connection_id,
4855 &srvc_id, &char_id, &desc_id,
4856 (oal_gatt_write_type_t)write_type, data->length,
4857 OAL_GATT_AUTH_REQ_NONE, (char *)(&data->data[0]));
4858 if (ret != OAL_STATUS_SUCCESS) {
4859 BT_ERR("ret: %d", ret);
4861 return _bt_convert_oal_status_to_bt_error(ret);
4864 return BLUETOOTH_ERROR_NONE;
4867 int _bt_gatt_watch_characteristic(
4868 bluetooth_gatt_client_char_prop_info_t *chr,
4872 struct gatt_server_info_t *conn_info = NULL;
4873 oal_gatt_srvc_id_t srvc_id;
4874 oal_gatt_id_t char_id;
4875 int ret = OAL_STATUS_SUCCESS;
4878 BT_CHECK_PARAMETER(chr, return);
4880 BT_INFO("Client ID [%d] Is Notify [%d]", client_id, is_notify);
4882 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4883 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4885 /* Check if remote GATT Server is connected or not */
4886 conn_info = _bt_find_remote_gatt_server_info(addr);
4888 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4889 conn_info->addr, conn_info->connection_id);
4891 BT_ERR("GATT Server is not yet connected..");
4893 return BLUETOOTH_ERROR_NOT_CONNECTED;
4895 srvc_id.is_prmry = TRUE;
4896 srvc_id.id.inst_id = chr->svc.instance_id;
4897 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4899 char_id.inst_id = chr->characteristic.instance_id;
4900 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4902 /* Register or unregister Notification characteristic */
4904 ret = gattc_register_for_notification(conn_info->connection_id,
4905 (bt_address_t*)&(chr->device_address),
4906 &srvc_id, &char_id);
4908 ret = gattc_deregister_for_notification(conn_info->connection_id,
4909 (bt_address_t*)&(chr->device_address),
4910 &srvc_id, &char_id);
4912 BT_INFO("Result[%d]", ret);
4913 if (ret != OAL_STATUS_SUCCESS) {
4914 BT_ERR("ret: %d", ret);
4916 return _bt_convert_oal_status_to_bt_error(ret);
4919 return BLUETOOTH_ERROR_NONE;
4923 int _bt_disconnect_le_device(bluetooth_device_address_t *address,
4926 struct gatt_server_info_t *conn_info = NULL;
4927 struct gatt_client_info_t *rem_client_conn_info = NULL;
4928 invocation_info_t *req_info = NULL;
4929 int ret = OAL_STATUS_SUCCESS;
4931 char *remote_address = NULL;
4933 BT_CHECK_PARAMETER(address, return);
4935 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4936 _bt_convert_addr_type_to_string(addr, address->addr);
4937 BT_INFO("GATT Client Disconnect request for address [%s]", addr + 12);
4939 /* Check if Remote Device is already under connection progress */
4940 req_info = _bt_get_request_info_data_from_function_name(BT_DISCONNECT_LE);
4942 remote_address = (char*)req_info->user_data;
4943 if (remote_address && !strcasecmp(remote_address, addr)) {/* Address matched */
4944 BT_DBG("Already DisConnection ongoing for same remote GATT Server address [%s]", remote_address);
4945 /* Return success and wait for events to be sent to all apps */
4947 return BLUETOOTH_ERROR_IN_PROGRESS;
4950 /* Check if remote GATT Server is connected or not */
4951 conn_info = _bt_find_remote_gatt_server_info(addr);
4953 /* Check if app sent 0 client id for Disconnection, in such case, use default gatt client ID */
4954 if (client_id == 0) {
4955 BT_INFO("GATT CLient Disconnect request sent by an app without any client instance [%d]",
4957 BT_INFO("Assign default GATT client id [%d]", gatt_default_client);
4958 client_id = gatt_default_client;
4961 BT_INFO("Disconnect remote gatt server using CLient ID [%d] Connection ID [%d]", client_id, conn_info->connection_id);
4962 ret = gattc_disconnect(client_id, (bt_address_t*)(address),
4963 conn_info->connection_id);
4965 /* check if remote client is connected */
4966 rem_client_conn_info = _bt_find_remote_gatt_client_info(addr);
4968 if (!rem_client_conn_info || client_id != 0) {
4969 BT_ERR("GATT device is not connected..");
4971 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
4974 BT_INFO("Disconnect remote gatt client ");
4976 ret = gatts_disconnect(rem_client_conn_info->instance_id,
4977 (bt_address_t*)(address), rem_client_conn_info->connection_id);
4980 if (ret != OAL_STATUS_SUCCESS) {
4981 BT_ERR("ret: %d", ret);
4983 return _bt_convert_oal_status_to_bt_error(ret);
4986 return BLUETOOTH_ERROR_NONE;
4989 int _bt_gatt_watch_service_changed_indication(const char *sender,
4990 bluetooth_device_address_t *address,
4991 gboolean is_enabled)
4994 bt_service_app_info_t *info = NULL;
4996 BT_INFO("%s Servic changed Indication watcher for app [%s]",
4997 is_enabled ? "Enable":"Disable", sender);
4999 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
5002 if (g_strcmp0(sender, info->sender) == 0 &&
5003 memcmp(info->address.addr, address->addr,
5004 sizeof(bluetooth_device_address_t)) == 0) {
5005 BT_DBG("Found GATT client App.. [%s], sender [%s]", info->uuid, info->sender);
5006 info->is_watcher_enabled = is_enabled;
5010 return BLUETOOTH_ERROR_NONE;
5013 int _bt_unregister_gatt_client_instance(const char *sender, int client_id)
5015 bt_service_app_info_t *info = NULL;
5018 BT_DBG("Unregister Allocated GATT Client instance [%s] Client ID [%d]", sender, client_id);
5020 /* Unregister CLient instance associated with address X. It is possible that another app still
5021 has client_id valid for same remote address */
5022 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
5025 /* Exact matching of sender */
5026 if (!g_strcmp0(info->sender, sender) && info->client_id == client_id) { /* Check for only valid GATT client Instance */
5027 numapps[k].client_id = -1;
5028 numapps[k].is_initialized = FALSE;
5029 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
5030 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
5031 memset(&numapps[k].address.addr, 0x00, sizeof(bluetooth_device_address_t));
5033 /* Its a GATT Client Instance */
5034 ret = gattc_deregister(client_id);
5035 if (ret != OAL_STATUS_SUCCESS) {
5036 BT_ERR("DeAllocate GATT Client instance with stack Fail ret: %d", ret);
5037 return _bt_convert_oal_status_to_bt_error(ret);
5039 return BLUETOOTH_ERROR_NONE;
5044 return BLUETOOTH_ERROR_NOT_FOUND;
5047 static void __bt_handle_client_mtu_exchange_completed(event_gattc_mtu_configured_t *event_data)
5049 int result = BLUETOOTH_ERROR_NONE;
5050 struct gatt_server_info_t *conn_info = NULL;
5051 GVariant *param = NULL;
5055 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(event_data->conn_id);
5056 if (conn_info == NULL) {
5057 BT_ERR("Cant find connection Information");
5061 BT_DBG("GATT Client: MTU Configured from addr [%s] status [%d] MTU size [%d]",
5062 conn_info->addr, event_data->status, event_data->mtu);
5064 if (event_data->status != OAL_STATUS_SUCCESS)
5065 result = BLUETOOTH_ERROR_INTERNAL;
5067 /* DBUS Return fo BT_REQ_ATT_MTU for all the apps */
5068 __bt_gatt_handle_pending_request_info(result, BT_REQ_ATT_MTU, conn_info->addr,
5069 BT_ADDRESS_STRING_SIZE);
5071 if (result == BLUETOOTH_ERROR_NONE) {
5072 mtu = event_data->mtu;
5073 param = g_variant_new("(isqy)",
5079 /* Send event to BT-API */
5080 _bt_send_event(BT_DEVICE_EVENT,
5081 BLUETOOTH_EVENT_GATT_ATT_MTU_CHANGED,
5084 /* Update the MTU for current connection */
5085 __bt_update_mtu_gatt_device(conn_info->addr, event_data->mtu);
5089 static struct gatt_mtu_info_t *__bt_find_mtu_gatt_device(char *address)
5092 struct gatt_mtu_info_t *info = NULL;
5094 for (l = gatt_mtu_info_list; l != NULL; l = g_slist_next(l)) {
5095 info = (struct gatt_mtu_info_t*)l->data;
5099 if (!g_strcmp0(info->addr, address)) {
5100 BT_DBG("Remote GATT device found addr[%s]", info->addr);
5105 BT_DBG("Not found Remote GATT device addr[%s]", address);
5109 static void __bt_remove_mtu_gatt_device(char *address)
5111 struct gatt_mtu_info_t *dev_info = NULL;
5113 dev_info = __bt_find_mtu_gatt_device(address);
5116 BT_DBG("removing the gatt device from mtu list");
5117 gatt_mtu_info_list = g_slist_remove(gatt_mtu_info_list, dev_info);
5118 g_free(dev_info->addr);
5123 static void __bt_add_mtu_gatt_device(char *address)
5125 struct gatt_mtu_info_t *dev_info = NULL;
5127 dev_info = __bt_find_mtu_gatt_device(address);
5130 BT_DBG("adding the gatt device in mtu list");
5131 dev_info = g_new0(struct gatt_mtu_info_t, 1);
5132 dev_info->addr = g_strdup(address);
5133 dev_info->att_mtu = BT_DEFAULT_ATT_MTU;
5134 gatt_mtu_info_list = g_slist_append(gatt_mtu_info_list, dev_info);
5138 static void __bt_update_mtu_gatt_device(char *address, int mtu)
5140 struct gatt_mtu_info_t *dev_info = NULL;
5142 dev_info = __bt_find_mtu_gatt_device(address);
5145 dev_info->att_mtu = mtu;
5149 int _bt_gatt_get_data_batching_available_packets(
5150 guint *available_packets)
5152 int ret = OAL_STATUS_SUCCESS;
5154 BT_CHECK_PARAMETER(available_packets, return);
5156 ret = gatt_get_data_batching_available_packets(available_packets);
5157 if (ret != OAL_STATUS_SUCCESS) {
5158 BT_ERR("ret: %d", ret);
5159 return _bt_convert_oal_status_to_bt_error(ret);
5162 return BLUETOOTH_ERROR_NONE;
5165 int _bt_gatt_enable_data_batching(bluetooth_device_address_t *address,
5166 int packet_threshold, int timeout)
5168 int ret = OAL_STATUS_SUCCESS;
5169 char remote_address[BT_ADDRESS_STRING_SIZE] = { 0 };
5171 BT_CHECK_PARAMETER(address, return);
5173 _bt_convert_addr_type_to_string(remote_address, address->addr);
5174 BT_INFO("Enable GATT data batching. address[%s] packet_threshold[%d] timeout[%d]",
5175 remote_address, packet_threshold, timeout);
5177 ret = gatt_enable_data_batching((bt_address_t*)(address), packet_threshold, timeout);
5179 if (ret != OAL_STATUS_SUCCESS) {
5180 BT_ERR("ret: %d", ret);
5181 return _bt_convert_oal_status_to_bt_error(ret);
5184 return BLUETOOTH_ERROR_NONE;
5187 int _bt_gatt_disable_data_batching(bluetooth_device_address_t *address)
5189 int ret = OAL_STATUS_SUCCESS;
5190 char remote_address[BT_ADDRESS_STRING_SIZE] = { 0 };
5192 BT_CHECK_PARAMETER(address, return);
5194 _bt_convert_addr_type_to_string(remote_address, address->addr);
5195 BT_INFO("Disable GATT data batching. address[%s]", remote_address);
5197 ret = gatt_disable_data_batching((bt_address_t*)(address));
5199 if (ret != OAL_STATUS_SUCCESS) {
5200 BT_ERR("ret: %d", ret);
5201 return _bt_convert_oal_status_to_bt_error(ret);
5204 return BLUETOOTH_ERROR_NONE;