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__
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;
218 /* App Information structure */
221 char sender[BT_SENDER_MAX_LENGTH];
222 char uuid[BT_UUID_STRING_MAX];
225 bluetooth_advertising_data_t adv_data; /* Will store adv data for specific slot */
227 bluetooth_scan_resp_data_t scan_rsp; /* Will store scan rsp data for specific slot */
229 gboolean is_initialized;
230 GSList *service_handles;
231 #ifdef TIZEN_GATT_CLIENT
232 int client_id; /* GATT Client instance ID */
233 bluetooth_device_address_t address; /* Remote BLE Device Address */
234 gboolean is_watcher_enabled;
236 } bt_service_app_info_t;
238 /* GATT Server Request Info Structure */
239 struct gatt_server_req_info {
240 int connection_id; /* This value will uniquely identify a GATT client-server connection */
241 int request_id; /* This is an unique transaction ID assigned against each request by stack */
242 int attribute_handle; /* GATT server attribute handle */
243 int offset; /* GATT server attribute offset on which request is invoked by GATT client */
244 bluetooth_gatt_att_request_type_e request_type; /* Read or Write request */
245 char *addr; /* Remote GATT client address */
248 /* GATT Indicate confirm result */
249 struct gatt_indicate_cfm_result_info_t {
250 int result; /* Result of event */
251 char *addr; /* Remote GATT client address */
252 int att_hdl; /* Characteristic Attribute handle */
253 int completed; /* 1 if last event, otheriwse 0 */
256 /* Request Search Utility method */
257 static struct gatt_server_req_info *__bt_gatt_server_find_request_info(int request_id,
258 bluetooth_gatt_att_request_type_e req_type);
260 static int __bt_gatt_send_indication_to_all_connected_clients(bluetooth_gatt_att_data_t *data,
261 bluetooth_gatt_server_indication_params_t *param);
263 static void __bt_remove_all_service_handles(bt_service_app_info_t *app_info);
265 static void __bt_free_service_info(bt_gatt_service_info_t *service_info);
267 static int __bt_unregister_gatt_client_instance(int client_if);
269 static void __bt_service_reset_gatt_data(void);
271 #ifdef TIZEN_GATT_CLIENT
272 static void __bt_handle_client_instance_registered(event_gattc_register_t *data);
273 static void __bt_handle_client_connected(event_gattc_conn_t *event_data);
274 static void __bt_handle_client_disconnected(event_gattc_conn_t *event_data);
275 static void __bt_handle_client_service_search_completed(event_gattc_conn_status_t *event_data);
276 static void __bt_handle_client_service_search_result(event_gattc_service_result_t *event_data);
277 static void __bt_handle_client_characteristic_search_result(
278 event_gattc_characteristic_result_t *event_data);
279 static void __bt_handle_client_descriptor_search_result(event_gattc_descriptor_result_t *event_data);
280 static void __bt_handle_client_characteristic_read_data(event_gattc_read_data *event_data);
281 static void __bt_handle_client_descriptor_read_data(event_gattc_read_data *event_data);
282 static void __bt_handle_client_characteristic_write_data(event_gattc_write_data *event_data);
283 static void __bt_handle_client_descriptor_write_data(event_gattc_write_data *event_data);
284 static void __bt_hanlde_le_device_disconnection(event_dev_conn_status_t *event_data);
285 static void __bt_handle_client_notification_registered(event_gattc_regdereg_notify_t *event_data,
286 gboolean is_registered);
287 static void __bt_handle_client_notification_data(event_gattc_notify_data *event_data);
288 static void __bt_handle_client_service_changed_ind(event_gattc_service_changed_data *event_data);
289 static void __bt_handle_client_mtu_exchange_completed(event_gattc_mtu_configured_t *event_data);
292 static int __bt_unregister_gatt_server_instance(int server_instance);
293 static void __bt_cleanup_remote_services(struct gatt_server_info_t *conn_info);
296 struct gatt_mtu_info_t {
297 char *addr; /* Remote GATT Server address */
301 static GSList *gatt_mtu_info_list = NULL;
303 static struct gatt_mtu_info_t *__bt_find_mtu_gatt_device(char *address);
304 static void __bt_remove_mtu_gatt_device(char *address);
305 static void __bt_add_mtu_gatt_device(char *address);
306 static void __bt_update_mtu_gatt_device(char *address, int mtu);
308 /* Linked List of GATT requests from Remote GATT Clients */
309 static GSList *gatt_server_requests = NULL;
311 /* Number of clients to be notified to */
312 static int num_indicate_clients;
314 /* List of applications */
315 static bt_service_app_info_t numapps[MAX_APPS_SUPPORTED];
317 static void __bt_gatt_handle_pending_request_info(int result,
318 int service_function, void *data, unsigned int size);
320 static void __bt_handle_server_instance_registered(event_gatts_register_t *data);
322 static void __bt_gatt_event_handler(int event_type, gpointer event_data);
329 void _bt_check_adv_app_termination(const char *name)
331 bt_service_app_info_t *app = NULL;
333 int apps[MAX_APPS_SUPPORTED] = { 0, };
335 ret_if(NULL == name);
337 memset(&apps, -1, sizeof(apps));
339 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
342 /* Search for a app which has same sender and stop adv is running */
343 if (!strncasecmp(app->sender, name, strlen(name)) && app->is_initialized == TRUE) {
344 BT_DBG("Match found, name: %s", name);
346 /* TODO 2: Need to manage app info as list, not array.
347 This loop always run for MAX count if any apps are terminated.
350 /* Save instances of all apps that need to be unregistered */
351 if (app->instance_id != -1) {
353 /* Unregister all service handles with stack */
354 __bt_remove_all_service_handles(app);
356 /* If Advertising is enabled, stop it */
357 if (app->adv_handle != 0) {
358 BT_INFO("Stop advertising on instance ID [%d]", app->instance_id);
359 /* Disable adv if running */
360 BT_INFO("Disable Advertising Adv Handle [%d] sender [%s]",
361 app->adv_handle, name);
362 _bt_set_advertising(app->sender, app->adv_handle, FALSE, FALSE);
365 apps[app->instance_id] = BT_GATT_SERVER; /* App holds a GATT server Instance */
366 } else if (app->client_id != -1) {
368 apps[app->client_id] = BT_GATT_CLIENT; /* App holds a GATT client Instance */
373 /* Unregister all apps one by one */
374 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
375 if (apps[k] == BT_GATT_SERVER) {
376 BT_INFO("Unregister server app[%d]", k);
377 /* Unregister server instance */
378 __bt_unregister_gatt_server_instance(k);
379 } else if (apps[k] == BT_GATT_CLIENT) {
380 BT_INFO("Unregister client app[%d]", k);
381 /* Unregister client instance */
382 __bt_unregister_gatt_client_instance(k);
387 static int __bt_gatt_send_indication_to_all_connected_clients(bluetooth_gatt_att_data_t *data,
388 bluetooth_gatt_server_indication_params_t *param)
391 int ret = OAL_STATUS_SUCCESS;
392 int result = OAL_STATUS_INTERNAL_ERROR;
394 BT_INFO("Current total number of connected clients [%d]", g_slist_length(gatt_client_info_list));
395 for (l = gatt_client_info_list; l != NULL; l = l->next) {
396 struct gatt_client_info_t *info = l->data;
399 BT_INFO("GATT Remote client address [%s] connection Id [%d]", info->addr, info->connection_id);
401 ret = gatts_send_indication(param->instance_id, param->atrribute_handle,
402 info->connection_id, data->length,
403 param->need_confirmation, (char *)(&data->data[0]));
405 BT_INFO("Send Indication to GATT client [%s] result: [%d]", info->addr, ret);
406 if (ret == OAL_STATUS_SUCCESS) {
407 BT_INFO("Send Indication sent successfully to GATT client [%s]", info->addr);
409 num_indicate_clients++;
413 BT_INFO("Indication sending done for total number of clients [%d]", num_indicate_clients);
417 static struct gatt_server_req_info *__bt_gatt_server_find_request_info(int request_id,
418 bluetooth_gatt_att_request_type_e req_type)
422 for (l = gatt_server_requests; l != NULL; l = l->next) {
423 struct gatt_server_req_info *req_info = l->data;
425 if (req_info && req_info->request_id == request_id && req_info->request_type == req_type) {
426 BT_INFO("GATT Server request info found Req ID [%d] handle [%d] conn ID [%d]",
427 req_info->request_id, req_info->attribute_handle, req_info->connection_id);
431 BT_ERR("Gatt Request not found");
435 void _bt_get_adv_handle_from_instance(int server_inst, int *adv_handle)
439 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
440 if (numapps[k].is_initialized == 1 && numapps[k].instance_id == server_inst) {
441 *adv_handle = numapps[k].adv_handle;
447 #ifdef TIZEN_GATT_CLIENT
448 char * _bt_gatt_get_default_gatt_client_uuid(void)
450 return g_strdup(DEFAULT_GATT_CLIENT_UUID);
454 static void __bt_register_default_gatt_client()
459 uuid_str = _bt_gatt_get_default_gatt_client_uuid();
460 _bt_string_to_uuid(uuid_str, (service_uuid_t*)&uuid);
462 BT_INFO("Register Default GATT client uuid [%s]", uuid_str);
464 if (OAL_STATUS_SUCCESS != gattc_register(&uuid)) /* for only Smart Control */
465 BT_ERR("gattc register failed");
471 int _bt_gatt_init(void)
473 const char *stack_name = NULL;
477 if (OAL_STATUS_SUCCESS != gatt_enable()) {
478 BT_ERR("gatt Init failed");
479 return BLUETOOTH_ERROR_INTERNAL;
482 /* Register gatt event handler */
483 _bt_service_register_event_handler_callback(BT_GATT_MODULE, __bt_gatt_event_handler);
485 __bt_service_reset_gatt_data();
487 stack_name = oal_get_stack_name();
489 if (stack_name && g_strcmp0(stack_name, "bluez") == 0) {
490 /*In the platform, defacult gatt client should be registered */
491 __bt_register_default_gatt_client();
495 return BLUETOOTH_ERROR_NONE;
498 static void __bt_service_reset_gatt_data(void)
502 BT_INFO("Rest numapp");
505 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
506 numapps[k].is_initialized = 0;
507 numapps[k].instance_id = -1;
508 numapps[k].adv_handle = 0;
509 numapps[k].adv_instance = -1;
510 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
511 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
512 memset(numapps[k].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
513 memset(numapps[k].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
514 numapps[k].adv_data_len = 0;
515 numapps[k].scan_rsp_len = 0;
518 numapps[k].client_id = -1;
519 memset(numapps[k].address.addr, 0x00, BLUETOOTH_ADDRESS_LENGTH);
520 numapps[k].is_watcher_enabled = FALSE;
524 void _bt_gatt_deinit(void)
526 BT_INFO("GATT deinit");
528 /* Un-register the default gatt client before */
529 __bt_unregister_gatt_client_instance(gatt_default_client);
531 if (OAL_STATUS_SUCCESS != gatt_disable())
532 BT_ERR("gatt deinit failed");
534 /* Un-register gatt event handler */
535 _bt_service_unregister_event_handler_callback(BT_GATT_MODULE);
537 __bt_service_reset_gatt_data();
540 void _bt_update_adv_handle(const char *sender, int adv_handle)
543 BT_DBG("Sender [%s] Adv handle [%d]", sender, adv_handle);
544 bt_service_app_info_t *info = NULL;
546 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
548 /* Do not update client instance */
549 if (info->instance_id == -1)
551 /* Search for a app which has same sender and adv handle as 0 */
552 if (!g_strcmp0(info->sender, sender) && info->adv_handle == 0)
553 info->adv_handle = adv_handle;
557 int _bt_is_sender_gatt_server_with_no_adv(const char *sender, int adv_handle)
560 BT_DBG("Sender [%s] Adv handle [%d]", sender, adv_handle);
561 bt_service_app_info_t *info = NULL;
563 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
565 /* Search for a app which has same sender and adv handle as 0
566 It is possible that same sender but different adv handle */
567 if (!g_strcmp0(info->sender, sender) && info->adv_handle == 0) {
568 //info->adv_handle = adv_handle;
569 return info->instance_id;
575 int _bt_get_allocated_server_instance(const char *sender, int adv_handle, gboolean use_reserved_slot)
578 BT_DBG("Sender [%s] Adv handle [%d]", sender, adv_handle);
579 bt_service_app_info_t *info = NULL;
581 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
584 /* Exact matching of Adv handle + sender combination */
585 if (!g_strcmp0(info->sender, sender) && info->adv_handle == adv_handle)
586 return info->instance_id;
588 if (!g_strcmp0(info->sender, sender) && info->adv_handle == -1)
589 return info->instance_id;
595 char * _bt_get_sender_and_handle(int server_instance, int *adv_handle)
598 bt_service_app_info_t *info = NULL;
600 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
603 if (info->instance_id == server_instance && info->adv_handle != 0) {
604 *adv_handle = info->adv_handle;
605 BT_DBG("Server instance [%d] Adv handle [%d] Sender [%s]", server_instance, *adv_handle, info->sender);
606 return g_strdup(info->sender);
612 void _bt_set_new_adv_data(bluetooth_advertising_data_t *adv, int len, int instance)
616 bt_service_app_info_t *info = NULL;
617 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
620 if (info->instance_id == instance) {
621 memcpy(info->adv_data.data, &adv->data, len);
627 void _bt_set_new_scan_rsp_data(bluetooth_scan_resp_data_t *scan, int len, int instance)
631 bt_service_app_info_t *info = NULL;
632 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
635 if (info->instance_id == instance) {
636 memcpy(info->scan_rsp.data, &scan->data, len);
642 void _bt_get_previous_adv_data(bluetooth_advertising_data_t *adv, int *len, int instance)
645 bt_service_app_info_t *info = NULL;
647 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
650 if (info->instance_id == instance) {
651 memcpy(&adv->data, info->adv_data.data, info->adv_data_len);
652 *len = info->adv_data_len;
658 void _bt_get_previous_scan_rsp_data(bluetooth_scan_resp_data_t *scan, int *len, int instance)
662 bt_service_app_info_t *info = NULL;
664 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
667 if (info->instance_id == instance) {
668 memcpy(&scan->data, info->scan_rsp.data, info->scan_rsp_len);
669 *len = info->scan_rsp_len;
675 static int __bt_unregister_gatt_client_instance(int client_if)
677 int ret = OAL_STATUS_SUCCESS;
680 BT_INFO("DeAllocate client instance ID [%d]", client_if);
682 /* Reset data: instance_id parameter could be either for GATT Server or for GATT client */
683 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
684 if (numapps[k].client_id == client_if) {
685 BT_INFO("This is a GATT client app, unregister: Slot [%d] vacant", k);
686 numapps[k].client_id = -1;
687 numapps[k].is_initialized = FALSE;
688 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
689 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
690 memset(&numapps[k].address.addr, 0x00, sizeof(bluetooth_device_address_t));
692 /* Its a GATT Client Instance */
693 ret = gattc_deregister(client_if);
694 if (ret != OAL_STATUS_SUCCESS) {
695 BT_ERR("DeAllocate GATT Client instance with stack Fail ret: %d", ret);
696 return BLUETOOTH_ERROR_INTERNAL;
701 return BLUETOOTH_ERROR_NONE;
704 static int __bt_unregister_gatt_server_instance(int server_instance)
706 int ret = OAL_STATUS_SUCCESS;
709 /* Unregister the server instance */
710 ret = gatts_unregister(server_instance);
711 if (ret != OAL_STATUS_SUCCESS) {
712 BT_ERR("DeAllocate server instance with stack Fail ret: %d", ret);
713 return BLUETOOTH_ERROR_INTERNAL;
715 BT_INFO("DeAllocated server instance with stack successful..");
718 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
719 if (numapps[k].instance_id == server_instance) {
720 numapps[k].is_initialized = 0;
721 numapps[k].instance_id = -1;
722 numapps[k].adv_handle = 0;
723 numapps[k].adv_instance = -1;
724 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
725 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
726 memset(numapps[k].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
727 memset(numapps[k].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
728 numapps[k].adv_data_len = 0;
729 numapps[k].scan_rsp_len = 0;
733 BT_DBG("Going8 to reset numapp block num [%d]", k);
734 return BLUETOOTH_ERROR_NONE;
737 static void __bt_remove_all_service_handles(bt_service_app_info_t *app_info)
741 int ret = OAL_STATUS_SUCCESS;
744 if (app_info == NULL)
747 count = g_slist_length(app_info->service_handles);
748 BT_INFO("Before handle removal: current count [%d]", count);
750 for (l = app_info->service_handles; l != NULL;) {
755 BT_INFO("Server Handle to be Removed [%d] Instance ID [%d]", *handle, app_info->instance_id);
756 if (_bt_gatt_server_stop_service(app_info->sender, *handle, app_info->instance_id) != BLUETOOTH_ERROR_NONE)
759 ret = gatts_delete_service(app_info->instance_id, *handle);
760 if (ret != OAL_STATUS_SUCCESS) {
761 BT_ERR("ret: %d", ret);
764 app_info->service_handles = g_slist_remove(app_info->service_handles, handle);
767 count = g_slist_length(app_info->service_handles);
768 BT_INFO("After deleting current count [%d]", count);
774 int _bt_unregister_server_instance(const char *sender, int adv_handle)
776 BT_INFO("Unregister Allocated server instance request Sender [%s] Adv handle [%d]", sender, adv_handle);
777 int result = BLUETOOTH_ERROR_NONE;
778 int apps[MAX_APPS_SUPPORTED];
782 memset(&apps, -1, sizeof(apps));
784 if (adv_handle == 0) {
785 BT_DBG("Its a direct GATT Server app request to unregister");
786 /* Unregister server instance for each app with same sender (case: GATT Server with multiple adv handle) */
787 bt_service_app_info_t *info = NULL;
789 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
792 /* Exact matching of sender */
793 if (!g_strcmp0(info->sender, sender)) {
794 BT_INFO("Unregister GATT server instance [%d]", info->instance_id);
795 /* Unregister all service handles with stack */
796 __bt_remove_all_service_handles(info);
798 /* Disable adv if running */
799 BT_INFO("Disable Advertising Adv Handle [%d] sender [%s]",
800 info->adv_handle, sender);
801 _bt_set_advertising(sender, info->adv_handle, FALSE, FALSE);
803 /* Save all instances which need to be unregistered */
804 apps[info->instance_id] = 1;
808 BT_DBG("Its an Internal unregister request by adv application");
809 server_instance = _bt_get_allocated_server_instance(sender, adv_handle, FALSE);
810 BT_DBG("Its an Internal unregister request by adv application: Adv disabled srv instance [%d]", server_instance);
811 if (server_instance == -1) {
812 BT_ERR("No allocated server instance to be removed");
813 return BLUETOOTH_ERROR_INVALID_PARAM;
815 if (!numapps[server_instance].service_handles) {
816 BT_INFO("There are no Service handles with this app, safe to unregister");
817 /* Unregister server instance only if this sender does not have any gatt services in it */
818 result = __bt_unregister_gatt_server_instance(server_instance);
820 numapps[server_instance].adv_handle = 0;
821 memset(numapps[server_instance].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
822 numapps[server_instance].adv_data_len = 0;
823 memset(numapps[server_instance].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
824 numapps[server_instance].scan_rsp_len = 0;
829 /* Unregister all apps one by one */
830 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
832 BT_INFO("Unregister app[%d]", k);
833 /* Unregister server instance */
834 __bt_unregister_gatt_server_instance(k);
841 int _bt_register_server_instance(const char *sender, int adv_handle)
843 int ret = OAL_STATUS_SUCCESS;
844 char *uuid_string = NULL;
849 BT_INFO("Check on which instance Server instance can be initialized....");
850 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
851 if (numapps[k].is_initialized == 1) {
852 BT_DBG("Instance ID [%d] is already in use..Check next slot", numapps[k].instance_id);
855 BT_DBG("Time to register GATT Server..UUID to be used is [%s] slot [%d]", uuid_list[slot-1], slot);
861 BT_ERR("No Slot if free for GATT Server registration..");
862 return BLUETOOTH_ERROR_REGISTRATION_FAILED;
865 uuid_string = g_malloc0(BT_UUID_STRING_MAX);
866 _bt_string_to_uuid(uuid_list[slot-1], (service_uuid_t*)&uuid);
867 g_strlcpy(uuid_string, uuid_list[slot-1], BT_UUID_STRING_MAX);
868 BT_INFO("Copied UUID string [%s]", uuid_string);
870 /* Register GATT Server */
871 ret = gatts_register(&uuid);
872 if (ret != OAL_STATUS_SUCCESS) {
873 BT_ERR("ret: %d", ret);
875 return BLUETOOTH_ERROR_INTERNAL;
877 BT_DBG("GATT Server registration call successfully accepted by OAL..wait for Instance Initialized event from OAL..");
878 /* Return & wait for GATT Server Instance Initialization event */
879 memset(numapps[slot].sender, 0x00, sizeof(numapps[slot].sender));
880 memset(numapps[slot].uuid, 0x00, sizeof(numapps[slot].uuid));
882 g_strlcpy(numapps[slot].sender, sender, sizeof(numapps[slot].sender));
883 g_strlcpy(numapps[slot].uuid, uuid_string, sizeof(numapps[slot].uuid));
885 numapps[slot].is_initialized = 0; /* Set initialization from app registered callback */
886 numapps[slot].adv_handle = adv_handle;
889 return BLUETOOTH_ERROR_NONE;
893 static void __bt_gatt_handle_pending_request_info(int result,
894 int service_function, void *data, unsigned int size)
898 invocation_info_t *req_info = NULL;
899 ret_if(data == NULL);
901 for (l = _bt_get_invocation_list(); l != NULL; ) {
904 if (req_info == NULL || req_info->service_function != service_function)
907 switch (service_function) {
909 case BT_GATT_SERVER_REGISTER: {
910 bt_service_app_info_t *param = (bt_service_app_info_t*)data;
912 if (!g_strcmp0(req_info->sender, param->sender)) {
913 BT_DBG("GATT Server app found [%s]", req_info->sender);
915 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
916 g_array_append_vals(out_param, ¶m->instance_id, sizeof(int));
917 _bt_service_method_return(req_info->context, out_param, result);
919 g_free(req_info->user_data);
920 _bt_free_info_from_invocation_list(req_info);
921 g_array_free(out_param, TRUE);
925 case BT_GATT_SERVER_START_SERVICE:
926 case BT_GATT_SERVER_DELETE_SERVICE: {
927 bt_service_app_info_t *param = (bt_service_app_info_t*)data;
929 int *saved_instance_id = (int*)req_info->user_data;
930 if (!g_strcmp0(req_info->sender, param->sender) && param->instance_id == *saved_instance_id) {
931 BT_DBG("GATT Server app found [%s] Instance ID [%d] Reply DBUS",
932 req_info->sender, *saved_instance_id);
934 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
935 g_array_append_vals(out_param, &saved_instance_id, sizeof(int));
936 _bt_service_method_return(req_info->context, out_param, result);
938 g_free(req_info->user_data);
939 _bt_free_info_from_invocation_list(req_info);
940 g_array_free(out_param, TRUE);
944 case BT_GATT_SERVER_ADD_SERVICE:
945 case BT_GATT_SERVER_ADD_DESCRIPTOR:
946 case BT_GATT_SERVER_ADD_CHARACTERISTIC: {
947 int *handle = (int*)data;
948 BT_DBG("Characteristic added: Handle [%d]", *handle);
949 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
951 g_array_append_vals(out_param, handle, sizeof(int));
952 _bt_service_method_return(req_info->context, out_param, result);
954 g_free(req_info->user_data);
955 _bt_free_info_from_invocation_list(req_info);
956 g_array_free(out_param, TRUE);
961 case BT_DISCONNECT_LE: {
962 #ifdef TIZEN_GATT_CLIENT
963 char *addr = (char*)req_info->user_data;
964 bluetooth_device_address_t address;
966 if (!g_strcmp0(addr, (char*)data)) {
967 BT_INFO("GATT Client connect-disconnect call pending for app [%s] addr [%s]",
968 req_info->sender, addr);
969 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
970 _bt_convert_addr_string_to_type(address.addr, addr);
972 g_array_append_vals(out_param, (bluetooth_device_address_t*)&address,
973 sizeof(bluetooth_device_address_t));
974 _bt_service_method_return(req_info->context, out_param, result);
976 g_free(req_info->user_data);
977 _bt_free_info_from_invocation_list(req_info);
978 g_array_free(out_param, TRUE);
981 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
983 g_array_append_vals(out_param, (bluetooth_device_address_t*)data,
984 sizeof(bluetooth_device_address_t));
985 _bt_service_method_return(req_info->context, out_param, result);
987 g_free(req_info->user_data);
988 _bt_free_info_from_invocation_list(req_info);
990 g_array_free(out_param, TRUE);
994 #ifdef TIZEN_GATT_CLIENT
995 case BT_GATT_CLIENT_REGISTER: {
996 bt_service_app_info_t *param = (bt_service_app_info_t*)data;
998 if (!g_strcmp0(req_info->sender, param->sender)) {
999 BT_DBG("GATT Client app found [%s] created client ID [%d]",
1000 req_info->sender, param->client_id);
1002 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1003 g_array_append_vals(out_param, ¶m->client_id, sizeof(int));
1004 _bt_service_method_return(req_info->context, out_param, result);
1006 g_free(req_info->user_data);
1007 _bt_free_info_from_invocation_list(req_info);
1008 g_array_free(out_param, TRUE);
1012 case BT_GATT_GET_PRIMARY_SERVICES: {
1013 bt_services_browse_info_t *param = (bt_services_browse_info_t*)data;
1014 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1015 _bt_convert_addr_type_to_string(address,
1016 (unsigned char *)(¶m->device_addr.addr));
1018 /* Match address to determine same request */
1019 if (!g_strcmp0((char*)req_info->user_data, address)) {
1020 BT_DBG("GATT Client app found [%s] Remote address [%s]",
1021 req_info->sender, address);
1023 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1024 g_array_append_vals(out_param, param,
1025 sizeof(bt_services_browse_info_t));
1027 //g_array_append_vals(out_param, ¶m->client_id, sizeof(int));
1028 _bt_service_method_return(req_info->context, out_param, result);
1030 g_free(req_info->user_data);
1031 _bt_free_info_from_invocation_list(req_info);
1032 g_array_free(out_param, TRUE);
1037 case BT_GATT_GET_SERVICE_PROPERTIES: {
1038 bt_char_browse_info_t param;
1039 memcpy((void*)¶m, data, sizeof(bt_char_browse_info_t));
1040 //bt_char_browse_info_t *param = (bt_char_browse_info_t*)data;
1042 bluetooth_gatt_client_svc_prop_info_t *prop = (bluetooth_gatt_client_svc_prop_info_t*)req_info->user_data;
1044 /* Match both address and service properties to determine same request */
1045 if (!memcmp(param.device_addr.addr,
1046 prop->device_address.addr,
1047 sizeof(bluetooth_device_address_t)) &&
1048 !memcmp(param.svc_uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1049 param.svc_inst_id == prop->svc.instance_id) {
1050 BT_DBG("Returning Service properties");
1052 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1053 g_array_append_vals(out_param, ¶m, sizeof(bt_char_browse_info_t));
1054 _bt_service_method_return(req_info->context, out_param, result);
1056 g_free(req_info->user_data);
1057 _bt_free_info_from_invocation_list(req_info);
1058 g_array_free(out_param, TRUE);
1062 case BT_GATT_GET_CHARACTERISTIC_PROPERTIES: {
1063 bt_descriptor_browse_info_t *param = (bt_descriptor_browse_info_t*)data;
1065 bluetooth_gatt_client_char_prop_info_t *prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
1067 /* Match both address, service properties &char properties to determine same request */
1068 if (!memcmp(param->device_addr.addr,
1069 prop->device_address.addr,
1070 sizeof(bluetooth_device_address_t)) &&
1071 !memcmp(param->svc_uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1072 param->svc_inst_id == prop->svc.instance_id &&
1073 !memcmp(param->char_uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1074 param->char_inst_id == prop->characteristic.instance_id) {
1075 BT_DBG("Returning Characteristic properties");
1076 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1077 g_array_append_vals(out_param, param, sizeof(bt_descriptor_browse_info_t));
1078 _bt_service_method_return(req_info->context, out_param, result);
1080 g_free(req_info->user_data);
1081 _bt_free_info_from_invocation_list(req_info);
1082 g_array_free(out_param, TRUE);
1086 case BT_GATT_WATCH_CHARACTERISTIC: {
1087 bt_gatt_notif_reg_info_t *param = (bt_gatt_notif_reg_info_t*)data;
1088 bluetooth_gatt_client_char_prop_info_t *prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
1090 /* Match both address, service properties &char properties to determine same request */
1091 if (!memcmp(param->addr.addr,
1092 prop->device_address.addr,
1093 sizeof(bluetooth_device_address_t)) &&
1094 !memcmp(param->svc_uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1095 param->svc_inst == prop->svc.instance_id &&
1096 !memcmp(param->char_uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1097 param->char_inst == prop->characteristic.instance_id) {
1098 BT_INFO("Characteristic Watch Successful: Is registered [%d]",
1099 param->is_registered);
1100 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1101 g_array_append_vals(out_param, param, sizeof(bt_gatt_notif_reg_info_t));
1102 _bt_service_method_return(req_info->context, out_param, result);
1103 g_free(req_info->user_data);
1104 _bt_free_info_from_invocation_list(req_info);
1105 g_array_free(out_param, TRUE);
1109 case BT_GATT_READ_CHARACTERISTIC:
1110 case BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE: {
1111 bluetooth_gatt_client_char_prop_info_t *param = (bluetooth_gatt_client_char_prop_info_t*)data;
1113 char *addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
1114 bluetooth_gatt_client_char_prop_info_t *prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
1115 _bt_convert_addr_type_to_string(addr, prop->device_address.addr);
1117 if (!memcmp(param, prop, sizeof(bluetooth_gatt_client_char_prop_info_t))) {
1118 BT_DBG("Gatt Char read or write request matched for address [%s]", addr);
1119 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1120 g_array_append_vals(out_param, param, sizeof(bluetooth_gatt_client_char_prop_info_t));
1121 _bt_service_method_return(req_info->context, out_param, result);
1123 g_free(req_info->user_data);
1124 _bt_free_info_from_invocation_list(req_info);
1125 g_array_free(out_param, TRUE);
1130 case BT_GATT_READ_DESCRIPTOR_VALUE:
1131 case BT_GATT_WRITE_DESCRIPTOR_VALUE: {
1132 bluetooth_gatt_client_desc_prop_info_t *param = (bluetooth_gatt_client_desc_prop_info_t*)data;
1134 char *addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
1135 bluetooth_gatt_client_desc_prop_info_t *prop = (bluetooth_gatt_client_desc_prop_info_t*)req_info->user_data;
1136 _bt_convert_addr_type_to_string(addr, prop->device_address.addr);
1138 if (!memcmp(param, prop, sizeof(bluetooth_gatt_client_desc_prop_info_t))) {
1139 BT_DBG("Descriptor read or write request matched for address [%s]", addr);
1140 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1141 g_array_append_vals(out_param, param, sizeof(bluetooth_gatt_client_desc_prop_info_t));
1142 _bt_service_method_return(req_info->context, out_param, result);
1144 g_free(req_info->user_data);
1145 _bt_free_info_from_invocation_list(req_info);
1146 g_array_free(out_param, TRUE);
1151 case BT_REQ_ATT_MTU: {
1152 char *addr = (char*)req_info->user_data;
1153 bluetooth_device_address_t address;
1155 if (!g_strcmp0(addr, (char*)data)) {
1156 BT_DBG("GATT Client BT_REQ_ATT_MTU call pending for app [%s] addr [%s]",
1157 req_info->sender, addr);
1158 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1159 _bt_convert_addr_string_to_type(address.addr, addr);
1161 g_array_append_vals(out_param, (bluetooth_device_address_t*)&address,
1162 sizeof(bluetooth_device_address_t));
1163 _bt_service_method_return(req_info->context, out_param, result);
1165 g_free(req_info->user_data);
1166 _bt_free_info_from_invocation_list(req_info);
1167 g_array_free(out_param, TRUE);
1177 static void __bt_handle_server_instance_registered(event_gatts_register_t *data)
1179 bt_service_app_info_t *info = NULL;
1181 char *uuid_string = g_malloc0(BT_UUID_STRING_MAX);
1183 _bt_uuid_to_string(&(data->server_uuid), uuid_string);
1184 BT_INFO("Instance ID is Intialized [%d] UUID initialized [%s]", data->server_inst, uuid_string);
1186 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1187 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1190 if (g_strcmp0(info->uuid, uuid_string) == 0) {
1191 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1192 info->is_initialized = TRUE;
1193 info->instance_id = data->server_inst;
1194 info->adv_instance = data->server_inst;
1195 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE, BT_GATT_SERVER_REGISTER,
1196 (void*)info, sizeof(bt_service_app_info_t));
1200 g_free(uuid_string);
1203 static void __bt_handle_gatt_server_service_added(event_gatts_srvc_prm_t *event)
1205 int result = BLUETOOTH_ERROR_NONE;
1209 bt_service_app_info_t *info = NULL;
1212 char *uuid_str = g_malloc0(BT_UUID_STRING_MAX);
1213 _bt_uuid_to_string(&(event->gatt_srvc_id.id.uuid), uuid_str);
1214 BT_INFO("GATT Added Service UUID: [%s] Server Ins [%d] Service hdl [%d] Is Primary [%d]\n",
1215 uuid_str, event->gatt_srvc_stat.server_inst, svc_handle, event->gatt_srvc_id.is_prmry);
1217 if (event->gatt_srvc_stat.status != OAL_STATUS_SUCCESS) {
1218 result = BLUETOOTH_ERROR_INTERNAL;
1219 svc_handle = 0; /* Service handle set to 0 indicates.
1220 0 is reserved by BT SIG, cant be used by app*/
1222 svc_handle = event->gatt_srvc_stat.servic_hndl;
1224 BT_INFO("GATT Added Service Status [%d] Server Ins [%d] Service hdl [%d] Is Primary [%d]\n",
1225 event->gatt_srvc_stat.status, event->gatt_srvc_stat.server_inst, svc_handle, event->gatt_srvc_id.is_prmry);
1227 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1228 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1231 if (info->instance_id == event->gatt_srvc_stat.server_inst) {
1232 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1233 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_ADD_SERVICE,
1234 (int*)&svc_handle, sizeof(int));
1236 /* Add Service Handle */
1237 if (svc_handle > 0) {
1238 handle = g_malloc0(sizeof(int));
1239 *handle = svc_handle;
1240 numapps[event->gatt_srvc_stat.server_inst].service_handles = g_slist_append(numapps[event->gatt_srvc_stat.server_inst].service_handles, handle);
1241 count = g_slist_length(numapps[event->gatt_srvc_stat.server_inst].service_handles);
1242 BT_INFO("Added Service handle [%d] to list..current count [%d]", svc_handle, count);
1251 static void __bt_handle_gatt_server_characteristic_added(event_gatts_srvc_charctr_t *event)
1253 int result = BLUETOOTH_ERROR_NONE;
1255 bt_service_app_info_t *info = NULL;
1257 char *uuid_str = g_malloc0(BT_UUID_STRING_MAX);
1259 BT_INFO("GATT Server Char added status [%d]", event->gatt_srvc_stat.status);
1260 BT_INFO("GATT Server_inst: [%d]", event->gatt_srvc_stat.server_inst);
1261 BT_INFO("GATT Service Handle: [%d]", event->gatt_srvc_stat.servic_hndl);
1262 BT_INFO("GATT Add characteristic Status: [%d]", event->gatt_srvc_stat.status);
1263 BT_INFO("GATT Service characteristic Handle: [%d]", event->charctr_hndl);
1265 if (event->gatt_srvc_stat.status != OAL_STATUS_SUCCESS) {
1266 result = BLUETOOTH_ERROR_INTERNAL;
1267 char_handle = 0; /* characteristic handle set to 0 indicates.
1268 0 is reserved by BT SIG, cant be used by app*/
1270 char_handle = event->charctr_hndl;
1273 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1274 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1277 if (info->instance_id == event->gatt_srvc_stat.server_inst) {
1278 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1279 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_ADD_CHARACTERISTIC,
1280 (int*)&char_handle, sizeof(int));
1286 _bt_uuid_to_string(&(event->charctr_uuid), uuid_str);
1287 BT_INFO("GATT Added Characteristic: UUID: [%s]", uuid_str);
1292 static void __bt_handle_gatt_server_descriptor_added(event_gatts_srvc_descr_t* event)
1294 int result = BLUETOOTH_ERROR_NONE;
1296 bt_service_app_info_t *info = NULL;
1298 char *uuid_str = g_malloc0(BT_UUID_STRING_MAX);
1300 BT_INFO("GATT Server Descriptor added status [%d]", event->gatt_srvc_stat.status);
1301 BT_INFO("GATT Server_inst: [%d]", event->gatt_srvc_stat.server_inst);
1302 BT_INFO("GATT Service Handle: [%d]", event->gatt_srvc_stat.servic_hndl);
1303 BT_INFO("GATT Add Descriptor Status: [%d]", event->gatt_srvc_stat.status);
1304 BT_INFO("GATT Service Descriptor Handle: [%d]", event->descrptr_hndl);
1306 if (event->gatt_srvc_stat.status != OAL_STATUS_SUCCESS) {
1307 result = BLUETOOTH_ERROR_INTERNAL;
1308 desc_handle = 0; /* Service handle set to 0 indicates.
1309 0 is reserved by BT SIG, cant be used by app*/
1311 desc_handle = event->descrptr_hndl;
1313 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1314 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1317 if (info->instance_id == event->gatt_srvc_stat.server_inst) {
1318 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1319 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_ADD_DESCRIPTOR,
1320 (int*)&desc_handle, sizeof(int));
1325 _bt_uuid_to_string(&(event->descrptr_uuid), uuid_str);
1326 BT_INFO("GATT Added Descriptor: UUID: [%s]", uuid_str);
1331 static void __bt_handle_gatt_server_service_started(event_gatts_srvc_t *event)
1333 bt_service_app_info_t *info = NULL;
1334 int result = BLUETOOTH_ERROR_NONE;
1336 BT_INFO("GATT Server Service Started..");
1339 BT_INFO("GATT Server_inst: [%d]", event->server_inst);
1340 BT_INFO("GATT Service Handle: [%d]", event->servic_hndl);
1341 BT_INFO("GATT Service Started Status: [%d]", event->status);
1343 if (event->status != OAL_STATUS_SUCCESS) {
1344 BT_ERR("GATT Server Service Start Failed Err: [%d]", event->status);
1345 result = BLUETOOTH_ERROR_INTERNAL;
1348 /* Check if the just registered Instance ID belongs to requester */
1349 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1352 if (info->instance_id == event->server_inst) {
1353 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1354 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_START_SERVICE,
1355 (void*)info, sizeof(bt_service_app_info_t));
1361 static void __bt_handle_gatt_server_service_stopped(event_gatts_srvc_t *event)
1363 int result = BLUETOOTH_ERROR_NONE;
1364 bt_service_app_info_t *info = NULL;
1366 BT_INFO("GATT Server Service Stopped..");
1368 BT_INFO("GATT Server_inst: [%d]", event->server_inst);
1369 BT_INFO("GATT Service Handle: [%d]", event->servic_hndl);
1370 BT_INFO("GATT Service Stopped Status: [%d]", event->status);
1372 if (event->status != OAL_STATUS_SUCCESS) {
1373 BT_ERR("GATT Server Service Stop Failed Err: [%d]", event->status);
1374 result = BLUETOOTH_ERROR_INTERNAL;
1377 /* Check if the just registered Instance ID belongs to requester */
1378 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1381 if (info->instance_id == event->server_inst) {
1382 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1383 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_STOP_SERVICE,
1384 (void*)info, sizeof(bt_service_app_info_t));
1390 static void __bt_handle_gatt_server_service_deleted(event_gatts_srvc_t *event)
1392 int result = BLUETOOTH_ERROR_NONE;
1393 bt_service_app_info_t *info = NULL;
1395 BT_INFO("GATT Server Service Deleted..");
1397 BT_INFO("GATT Server_inst: [%d]", event->server_inst);
1398 BT_INFO("GATT Service Handle: [%d]", event->servic_hndl);
1399 BT_INFO("GATT Service Deleted Status: [%d]", event->status);
1401 if (event->status != OAL_STATUS_SUCCESS) {
1402 BT_ERR("GATT Server Service Delete Failed Err: [%d]", event->status);
1403 result = BLUETOOTH_ERROR_INTERNAL;
1406 /* Check if the just registered Instance ID belongs to requester */
1407 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1410 if (info->instance_id == event->server_inst) {
1411 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1412 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_DELETE_SERVICE,
1413 (void*)info, sizeof(bt_service_app_info_t));
1419 struct gatt_client_info_t *_bt_find_remote_gatt_client_info(char *address)
1422 struct gatt_client_info_t *info = NULL;
1423 for (l = gatt_client_info_list; l != NULL; l = g_slist_next(l)) {
1424 info = (struct gatt_client_info_t*)l->data;
1428 if (!g_strcmp0(info->addr, address)) {
1429 BT_DBG("Remote GATT client found addr[%s]", info->addr);
1436 static struct gatt_client_info_t *__bt_find_remote_gatt_client_info_from_conn_id(int conn_id)
1439 struct gatt_client_info_t *info = NULL;
1441 for (l = gatt_client_info_list; l != NULL; l = g_slist_next(l)) {
1442 info = (struct gatt_client_info_t*)l->data;
1446 if (info->connection_id == conn_id) {
1447 BT_INFO("Remote GATT client found addr[%s]", info->addr);
1454 #ifdef TIZEN_GATT_CLIENT
1455 struct gatt_server_info_t *_bt_find_remote_gatt_server_info(char *address)
1458 struct gatt_server_info_t *info = NULL;
1459 for (l = gatt_server_info_list; l != NULL; l = g_slist_next(l)) {
1460 info = (struct gatt_server_info_t*)l->data;
1464 if (!g_strcmp0(info->addr, address)) {
1465 BT_DBG("Remote GATT Server found addr[%s]", info->addr);
1472 static struct gatt_out_conn_info_t* __bt_find_gatt_outgoing_conn_info(char *address)
1475 struct gatt_out_conn_info_t *info = NULL;
1476 for (l = outgoing_gatt_conn_list; l != NULL; l = g_slist_next(l)) {
1477 info = (struct gatt_out_conn_info_t*)l->data;
1481 if (!g_strcmp0(info->addr, address)) {
1482 BT_INFO("Outgoing connection info found addr[%s]", info->addr);
1490 #ifdef TIZEN_GATT_CLIENT
1491 static void __bt_handle_gatt_server_connection_state(event_gatts_conn_t *event)
1493 int result = BLUETOOTH_ERROR_NONE;
1494 struct gatt_client_info_t *client_info = NULL;
1495 #ifndef __INTEGRATE_GATT_INFO__
1496 struct gatt_server_info_t *server_info = NULL;
1498 bluetooth_device_address_t dev_addr;
1499 GVariant *param = NULL;
1502 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1504 memcpy(dev_addr.addr, event->address.addr, 6);
1506 /* REPLY dbus Context to application which called BT_CONNECT_LE. There is status
1508 _bt_convert_addr_type_to_string(address,
1509 (unsigned char *)dev_addr.addr);
1511 if (event->status != OAL_STATUS_SUCCESS)
1512 result = BLUETOOTH_ERROR_INTERNAL;
1514 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
1515 address, BT_ADDRESS_STRING_SIZE);
1517 BT_INFO("GATT Server Connedted: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1518 address, event->server_inst, event->conn_id);
1521 /* Check if device is already in connected list */
1522 client_info = _bt_find_remote_gatt_client_info(address);
1525 BT_INFO("Conn Info absent: But no need to Send Local GATT Server Connected event to apps");
1527 param = g_variant_new("(is)", result, address);
1529 /* Send event to application */
1530 _bt_send_event(BT_DEVICE_EVENT,
1531 BLUETOOTH_EVENT_GATT_SERVER_CONNECTED, /* Local device is GATT server */
1534 /* Save client connection info */
1535 client_info = g_new0(struct gatt_client_info_t, 1);
1536 client_info->addr = g_strdup(address);
1537 BT_INFO("Added GATT client addr[%s]", client_info->addr);
1538 client_info->connection_id = event->conn_id;
1539 #ifdef __INTEGRATE_GATT_INFO__
1540 client_info->client_id = -1;
1542 client_info->instance_id = event->server_inst;
1543 gatt_client_info_list = g_slist_append(gatt_client_info_list, client_info);
1544 BT_INFO("Total num of connected Remote GATT Clients [%d]", g_slist_length(gatt_client_info_list));
1546 #ifndef __INTEGRATE_GATT_INFO__
1547 /* Save server connection info */
1548 server_info = g_new0(struct gatt_server_info_t, 1);
1549 server_info->addr = g_strdup(address);
1550 server_info->client_id = -1;
1551 BT_INFO("Added GATT server addr[%s]", server_info->addr);
1552 server_info->connection_id = event->conn_id;
1553 gatt_server_info_list = g_slist_append(gatt_server_info_list, server_info);
1554 BT_INFO("Total num of connected Remote GATT Servers [%d]", g_slist_length(gatt_server_info_list));
1557 ret = gattc_add_connection_info((bt_address_t *)&dev_addr, event->conn_id, event->server_inst);
1558 if (ret != OAL_STATUS_SUCCESS) {
1559 BT_ERR("gattc register server instance failed");
1563 __bt_add_mtu_gatt_device(address);
1568 /* GATT Server Dis connected */
1569 static void __bt_handle_gatt_server_disconnection_state(event_gatts_conn_t *event)
1571 int result = BLUETOOTH_ERROR_NONE;
1572 struct gatt_client_info_t *client_info = NULL;
1573 #ifndef __INTEGRATE_GATT_INFO__
1574 struct gatt_server_info_t *server_info = NULL;
1576 bluetooth_device_address_t dev_addr;
1577 GVariant *param = NULL;
1578 char address[BT_ADDRESS_STRING_SIZE];
1580 memcpy(dev_addr.addr, event->address.addr, 6);
1582 /* REPLY dbus Context to application which called BT_DISCONNECT_LE. There is status
1584 _bt_convert_addr_type_to_string(address,
1585 (unsigned char *)dev_addr.addr);
1587 if (event->status != OAL_STATUS_SUCCESS)
1588 result = BLUETOOTH_ERROR_INTERNAL;
1590 if (NULL == _bt_get_request_info_data(BT_DISCONNECT_LE, address)) {
1591 if (NULL != _bt_get_request_info_data(BT_CONNECT_LE, address)) {
1592 result = BLUETOOTH_ERROR_INTERNAL;
1594 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
1595 address, BT_ADDRESS_STRING_SIZE);
1597 BT_ERR("Failed to connect Local GATT Server Remote Client addr[%s]", address);
1601 __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE,
1602 address, BT_ADDRESS_STRING_SIZE);
1605 BT_INFO("Local GATT Server DisConnected: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1606 address, event->server_inst, event->conn_id);
1607 /* Remove Connection info */
1608 client_info = _bt_find_remote_gatt_client_info(address);
1610 BT_INFO("No need to Send Local GATT Server Disconnected event to apps, just remove remote client info");
1612 param = g_variant_new("(is)", result, address);
1613 /* Send event to application */
1614 _bt_send_event(BT_DEVICE_EVENT,
1615 BLUETOOTH_EVENT_GATT_SERVER_DISCONNECTED, /* Local device is GATT server */
1618 #ifndef __INTEGRATE_GATT_INFO__
1619 /* Remove server info from list */
1620 server_info = _bt_find_remote_gatt_server_info(address);
1622 gatt_server_info_list = g_slist_remove(gatt_server_info_list, server_info);
1624 BT_INFO("Can not find conn info, already removed!");
1626 /* Remove all services from info list_gatt_info */
1627 __bt_cleanup_remote_services(server_info);
1629 /* Remove all services from info list_gatt_info */
1630 __bt_cleanup_remote_services(client_info);
1633 /* Remove client info from List */
1634 gatt_client_info_list = g_slist_remove(gatt_client_info_list, client_info);
1635 BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
1636 g_free(client_info->addr);
1637 g_free(client_info);
1640 __bt_remove_mtu_gatt_device(address);
1644 static void __bt_handle_gatt_server_connection_state(event_gatts_conn_t *event)
1646 int result = BLUETOOTH_ERROR_NONE;
1647 struct gatt_client_info_t *conn_info = NULL;
1648 bt_service_app_info_t *info = NULL;
1649 bluetooth_device_address_t dev_addr;
1650 GVariant *param = NULL;
1653 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1655 memcpy(dev_addr.addr, event->address.addr, 6);
1658 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1659 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1662 if (info->instance_id == event->server_inst) {
1663 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1664 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
1665 (bluetooth_device_address_t*)&dev_addr, sizeof(bluetooth_device_address_t));
1671 /* Send event to BT-API */
1672 _bt_convert_addr_type_to_string(address,
1673 (unsigned char *)dev_addr.addr);
1675 BT_INFO("GATT Server Connedted: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1676 address, event->server_inst, event->conn_id);
1679 /* Check if device is already in connected list */
1680 conn_info = _bt_find_remote_gatt_client_info(address);
1683 param = g_variant_new("(is)", result, address);
1684 /* Send event to application */
1685 _bt_send_event(BT_GATT_SERVER_EVENT,
1686 BLUETOOTH_EVENT_GATT_CONNECTED,
1689 /* Save Connection info */
1690 conn_info = g_new0(struct gatt_client_info_t, 1);
1691 conn_info->addr = g_strdup(address);
1692 BT_INFO("Added GATT client addr[%s]", conn_info->addr);
1693 conn_info->connection_id = event->conn_id;
1694 conn_info->instance_id = event->server_inst;
1695 gatt_client_info_list = g_slist_append(gatt_client_info_list, conn_info);
1696 BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
1702 /* GATT Server Dis connected */
1703 static void __bt_handle_gatt_server_disconnection_state(event_gatts_conn_t *event)
1705 int result = BLUETOOTH_ERROR_NONE;
1706 struct gatt_client_info_t *conn_info = NULL;
1707 bluetooth_device_address_t dev_addr;
1708 bt_service_app_info_t *info = NULL;
1709 GVariant *param = NULL;
1712 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1714 memcpy(dev_addr.addr, event->address.addr, 6);
1716 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1717 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1720 if (info->instance_id == event->server_inst) {
1721 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1722 __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE,
1723 (bluetooth_device_address_t*)&dev_addr, sizeof(bluetooth_device_address_t));
1729 /* Send event to BT-API */
1730 _bt_convert_addr_type_to_string(address,
1731 (unsigned char *)dev_addr.addr);
1733 BT_INFO("GATT Server DisConnected: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1734 address, event->server_inst, event->conn_id);
1736 param = g_variant_new("(is)", result, address);
1737 /* Send event to application */
1738 _bt_send_event(BT_GATT_SERVER_EVENT,
1739 BLUETOOTH_EVENT_GATT_DISCONNECTED,
1742 /* Remove Connection info */
1743 conn_info = _bt_find_remote_gatt_client_info(address);
1745 BT_INFO("Remove GATT client info from List..");
1746 /* Remove info from List */
1747 gatt_client_info_list = g_slist_remove(gatt_client_info_list, conn_info);
1748 BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
1749 g_free(conn_info->addr);
1758 static void __bt_handle_gatt_server_acquire_write_requested(event_gatts_srvc_acquire_attr_t *event)
1760 GVariant *param = NULL;
1761 int result = BLUETOOTH_ERROR_NONE;
1762 struct gatt_server_req_info *req_info = NULL;
1763 bluetooth_device_address_t dev_addr;
1764 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1766 BT_INFO("GATT Server ACQUIRE Write Req Connection ID: [%d]", event->attr_trans.conn_id);
1767 BT_INFO("GATT Server ACQUIRE Write Req Transaction ID:[%d]", event->attr_trans.trans_id);
1768 BT_INFO("GATT Server ACQUIRE Write Req Attribute Handle: [%d]", event->attr_trans.attr_handle);
1770 //address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1771 memcpy(dev_addr.addr, event->address.addr, 6);
1773 req_info = g_new0(struct gatt_server_req_info, 1);
1774 req_info->request_id = event->attr_trans.trans_id;
1775 req_info->attribute_handle = event->attr_trans.attr_handle;
1776 req_info->connection_id = event->attr_trans.conn_id;
1777 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_ACQUIRE_WRITE;
1778 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1780 _bt_convert_addr_type_to_string(address,
1781 (unsigned char *)dev_addr.addr);
1783 param = g_variant_new("(iiiiiis)", result,
1784 event->attr_trans.conn_id,
1785 event->attr_trans.trans_id,
1786 event->attr_trans.attr_handle,
1787 event->mtu, event->attr_trans.offset, address);
1788 BT_INFO("GATT Server ACQUIRE Write Req Attribute remote address : [%s]", address);
1790 _bt_send_event(BT_GATT_SERVER_EVENT,
1791 BLUETOOTH_EVENT_GATT_SERVER_ACQUIRE_WRITE,
1796 static void __bt_handle_gatt_server_acquire_notify_requested(event_gatts_srvc_acquire_attr_t *event)
1798 GVariant *param = NULL;
1799 int result = BLUETOOTH_ERROR_NONE;
1800 struct gatt_server_req_info *req_info = NULL;
1801 bluetooth_device_address_t dev_addr;
1802 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1804 BT_INFO("GATT Server ACQUIRE Notify Req Connection ID: [%d]", event->attr_trans.conn_id);
1805 BT_INFO("GATT Server ACQUIRE Notify Req Transaction ID:[%d]", event->attr_trans.trans_id);
1806 BT_INFO("GATT Server ACQUIRE Notify Req Attribute Handle: [%d]", event->attr_trans.attr_handle);
1807 BT_INFO("GATT Server ACQUIRE notify Req address : [%s]", address);
1809 memcpy(dev_addr.addr, event->address.addr, 6);
1810 _bt_convert_addr_type_to_string(address,
1811 (unsigned char *)dev_addr.addr);
1812 BT_INFO("GATT Server ACQUIRE notify Req remote address : [%s]", address);
1814 req_info = g_new0(struct gatt_server_req_info, 1);
1815 req_info->request_id = event->attr_trans.trans_id;
1816 req_info->attribute_handle = event->attr_trans.attr_handle;
1817 req_info->connection_id = event->attr_trans.conn_id;
1818 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_ACQUIRE_NOTIFY;
1819 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1821 param = g_variant_new("(iiiiiis)", result,
1822 event->attr_trans.conn_id,
1823 event->attr_trans.trans_id,
1824 event->attr_trans.attr_handle,
1825 event->mtu, event->attr_trans.offset,
1828 BT_INFO("GATT Server ACQUIRE Notify Req Attribute : ");
1830 _bt_send_event(BT_GATT_SERVER_EVENT,
1831 BLUETOOTH_EVENT_GATT_SERVER_ACQUIRE_NOTIFY,
1835 static void __bt_handle_gatt_server_write_requested(event_gatts_srvc_write_attr_t *event)
1838 bluetooth_device_address_t dev_addr;
1839 GVariant *param = NULL;
1840 int result = BLUETOOTH_ERROR_NONE;
1842 gboolean is_prepare_write;
1844 GVariant *data = NULL;
1846 struct gatt_server_req_info *req_info = NULL;
1847 BT_INFO("GATT Server Write Requested");
1849 memcpy(dev_addr.addr, event->address.addr, 6);
1851 BT_INFO("GATT Server Write Req Connection ID: [%d]", event->attr_trans.conn_id);
1852 BT_INFO("GATT Server Write Req Transaction ID:[%d]", event->attr_trans.trans_id);
1853 BT_INFO("GATT Server Write Req Attribute Handle: [%d]", event->attr_trans.attr_handle);
1854 BT_INFO("GATT Server Write Req Attribute Offset: [%d]", event->attr_trans.offset);
1855 BT_INFO("GATT Server Write Req value length [%d]", event->length);
1856 BT_INFO("GATT Server Write Req needs response: [%d]", event->need_rsp);
1857 BT_INFO("GATT Server Write Req Is Prep: [%d]", event->is_prep);
1859 if (event->length <= 0) {
1860 BT_INFO("GATT Server write requested, but length of attr value is 0");
1864 need_resp = event->need_rsp;
1865 is_prepare_write = event->is_prep;
1867 write_val = g_memdup(&event->value[0], event->length);
1869 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1870 _bt_convert_addr_type_to_string(address,
1871 (unsigned char *)dev_addr.addr);
1873 BT_INFO("GATT Server Write Request from remote client [%s]", address);
1875 if (event->length > 0) {
1876 for (i = 0; i < event->length; i++)
1877 BT_DBG("Data[%d] = [0x%x]", i, event->value[i]);
1879 /* Save Write Request Info */
1880 req_info = g_new0(struct gatt_server_req_info, 1);
1881 req_info->request_id = event->attr_trans.trans_id;
1882 req_info->attribute_handle = event->attr_trans.attr_handle;
1883 req_info->connection_id = event->attr_trans.conn_id;
1884 req_info->addr = address;
1885 req_info->offset = event->attr_trans.offset;
1886 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_WRITE;
1887 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1889 data = g_variant_new_from_data(
1890 G_VARIANT_TYPE_BYTESTRING,
1895 param = g_variant_new("(iiiiiibbsn@ay)", result,
1896 event->attr_trans.conn_id,
1897 event->attr_trans.trans_id,
1898 event->attr_trans.attr_handle,
1899 event->attr_trans.offset,
1907 _bt_send_event(BT_GATT_SERVER_EVENT,
1908 BLUETOOTH_EVENT_GATT_SERVER_VALUE_CHANGED,
1914 static void __bt_handle_gatt_server_read_requested(event_gatts_srvc_read_attr_t *event)
1916 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1917 bluetooth_device_address_t dev_addr;
1918 int result = BLUETOOTH_ERROR_NONE;
1919 struct gatt_server_req_info *req_info = NULL;
1920 GVariant *param = NULL;
1922 BT_INFO("GATT Server Read Requested");
1924 memcpy(dev_addr.addr, event->address.addr, 6);
1926 BT_INFO("GATT Server-Client Connection ID: [%d]", event->attr_trans.conn_id);
1927 BT_INFO("GATT Server-Client Transaction ID: [%d]", event->attr_trans.trans_id);
1928 BT_INFO("GATT Server Attribute Handle: [%d]", event->attr_trans.attr_handle);
1929 BT_INFO("GATT Server Attribute Offset for read: [%d]", event->attr_trans.offset);
1930 BT_INFO("GATT Server Attribute is long: [%d]", event->is_long);
1932 is_long = event->is_long;
1934 /* Save Read Request Info */
1935 req_info = g_new0(struct gatt_server_req_info, 1);
1936 req_info->request_id = event->attr_trans.trans_id;
1937 req_info->attribute_handle = event->attr_trans.attr_handle;
1938 req_info->connection_id = event->attr_trans.conn_id;
1939 req_info->addr = address;
1940 req_info->offset = event->attr_trans.offset;
1941 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_READ;
1942 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1944 /* Send event to BT-API */
1945 _bt_convert_addr_type_to_string(address,
1946 (unsigned char *)dev_addr.addr);
1948 BT_INFO("GATT Server Read Request from remote client [%s]", address);
1950 param = g_variant_new("(iiiiibs)", result,
1951 event->attr_trans.conn_id,
1952 event->attr_trans.trans_id,
1953 event->attr_trans.attr_handle,
1954 event->attr_trans.offset,
1958 _bt_send_event(BT_GATT_SERVER_EVENT,
1959 BLUETOOTH_EVENT_GATT_SERVER_READ_REQUESTED,
1963 static void __bt_handle_gatt_server_indicate_confirmed(event_gatts_ind_cnfrm_t *event)
1965 bluetooth_device_address_t dev_addr;
1966 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1967 int cur_connected_clients;
1968 static int recvd = 0;
1969 gboolean completed = 0;
1970 GVariant *param = NULL;
1972 /* OAL event does provide error, so MW assumes event will never contain wrong data,
1973 incase of any issues, check with OAL */
1974 int result = BLUETOOTH_ERROR_NONE;
1976 memcpy(dev_addr.addr, event->address.addr, 6);
1977 _bt_convert_addr_type_to_string(address,
1978 (unsigned char *)dev_addr.addr);
1980 BT_INFO("Indication sent to GATT client [%s] conn_ ID [%d] transaction ID [%d] Att handle [%d]",
1981 address, event->conn_id, event->trans_id, event->attr_handle);
1984 cur_connected_clients = g_slist_length(gatt_client_info_list);
1985 BT_INFO("Number of connected clients during sending Indication [%d] & current connected count [%d]",
1986 num_indicate_clients, cur_connected_clients);
1989 if (recvd == num_indicate_clients) {
1990 BT_INFO("Gatt indication confirm event for last GATT client.. [%s]", address);
1991 completed = 1; /* Last event */
1992 recvd = 0; /* Reset */
1993 num_indicate_clients = 0;
1996 param = g_variant_new("(isib)",
2002 /* Send event to BT-API */
2003 _bt_send_event(BT_GATT_SERVER_EVENT,
2004 BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_COMPLETED,
2007 BT_INFO("Received Indication confirm for client number [%d]", recvd);
2011 /* Tizen Platform Specific */
2012 static void __bt_handle_gatt_server_notification_changed(event_gatts_notif_t *event)
2014 bluetooth_device_address_t dev_addr;
2015 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2016 GVariant *param = NULL;
2019 /* OAL event does provide error, so MW assumes event will never contain wrong data,
2020 incase of any issues, check with OAL */
2021 int result = BLUETOOTH_ERROR_NONE;
2023 memcpy(dev_addr.addr, event->address.addr, 6);
2024 _bt_convert_addr_type_to_string(address,
2025 (unsigned char *)dev_addr.addr);
2027 BT_INFO("notification_changed [%s] conn_ ID [%d] transaction ID [%d] Att handle [%d] Notify[%d]",
2028 address, event->conn_id, event->trans_id, event->attr_handle, event->notify);
2030 /* Set Notifcation status */
2031 notify = event->notify;
2033 param = g_variant_new("(isib)",
2039 /* Send event to BT-API */
2040 _bt_send_event(BT_GATT_SERVER_EVENT,
2041 BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_STATE_CHANGED,
2047 static void __bt_handle_gatt_mtu_changed_event(event_gatts_mtu_changed_t *event)
2049 int result = BLUETOOTH_ERROR_NONE;
2050 struct gatt_client_info_t *conn_info = NULL;
2051 GVariant *param = NULL;
2054 conn_info = __bt_find_remote_gatt_client_info_from_conn_id(event->conn_id);
2055 if (conn_info == NULL) {
2056 BT_ERR("Cant find connection Information");
2059 BT_INFO("Got connection Info GATT client [%s] MTU Size [%d]",
2060 conn_info->addr, event->mtu_size);
2062 __bt_update_mtu_gatt_device(conn_info->addr, event->mtu_size);
2064 param = g_variant_new("(isqy)",
2070 /* Send event to BT-API */
2071 _bt_send_event(BT_GATT_SERVER_EVENT,
2072 BLUETOOTH_EVENT_GATT_ATT_MTU_CHANGED,
2076 static void __bt_gatt_event_handler(int event_type, gpointer event_data)
2078 BT_INFO("OAL event = 0x%x, \n", event_type);
2079 switch (event_type) {
2080 case OAL_EVENT_BLE_SERVER_INSTANCE_INITIALISED: {
2081 BT_INFO("OAL Event: Server Instance Registered");
2082 /* GATT Server Registered event is handled in MAIN thread context */
2083 __bt_handle_server_instance_registered((event_gatts_register_t *)event_data);
2086 case OAL_EVENT_GATTS_SERVICE_ADDED: {
2087 BT_INFO("OAL Event: GATT Service added");
2088 __bt_handle_gatt_server_service_added((event_gatts_srvc_prm_t *)event_data);
2091 case OAL_EVENT_GATTS_CHARACTERISTIC_ADDED: {
2092 BT_INFO("OAL Event: GATT characteristic added");
2093 __bt_handle_gatt_server_characteristic_added((event_gatts_srvc_charctr_t *)event_data);
2096 case OAL_EVENT_GATTS_DESCRIPTOR_ADDED: {
2097 BT_INFO("OAL Event: GATT descriptor added");
2098 __bt_handle_gatt_server_descriptor_added((event_gatts_srvc_descr_t *)event_data);
2101 case OAL_EVENT_GATTS_SERVICE_STARTED: {
2102 BT_INFO("OAL Event: GATT Service started");
2103 __bt_handle_gatt_server_service_started((event_gatts_srvc_t *)event_data);
2106 case OAL_EVENT_GATTS_SERVICE_STOPED: {
2107 BT_INFO("OAL Event: GATT Service stopped");
2108 __bt_handle_gatt_server_service_stopped((event_gatts_srvc_t *)event_data);
2111 case OAL_EVENT_GATTS_SERVICE_DELETED: {
2112 BT_INFO("OAL Event: GATT Service deleted");
2113 __bt_handle_gatt_server_service_deleted((event_gatts_srvc_t *) event_data);
2116 case OAL_EVENT_GATTS_CONNECTION_COMPLETED: {
2117 BT_INFO("OAL Event: GATT Server Connected");
2118 __bt_handle_gatt_server_connection_state((event_gatts_conn_t *)event_data);
2121 case OAL_EVENT_GATTS_DISCONNECTION_COMPLETED: {
2122 BT_INFO("OAL Event: GATT Server Disconnected");
2123 __bt_handle_gatt_server_disconnection_state((event_gatts_conn_t *)event_data);
2126 case OAL_EVENT_GATTS_REQUEST_READ: {
2127 BT_INFO("OAL Event: GATT Server Read Request");
2128 __bt_handle_gatt_server_read_requested((event_gatts_srvc_read_attr_t *)event_data);
2131 case OAL_EVENT_GATTS_REQUEST_WRITE: {
2132 BT_INFO("OAL Event: GATT Server Write Request");
2133 __bt_handle_gatt_server_write_requested((event_gatts_srvc_write_attr_t *)event_data);
2136 case OAL_EVENT_GATTS_REQUEST_ACQUIRE_WRITE: {
2137 BT_INFO("OAL Event: GATT Server Acquire Write Request");
2138 __bt_handle_gatt_server_acquire_write_requested((event_gatts_srvc_acquire_attr_t*)event_data);
2141 case OAL_EVENT_GATTS_REQUEST_ACQUIRE_NOTIFY: {
2142 BT_INFO("OAL Event: GATT ServerAcquire Notify Request");
2143 __bt_handle_gatt_server_acquire_notify_requested((event_gatts_srvc_acquire_attr_t*)event_data);
2146 case OAL_EVENT_GATTS_IND_CONFIRM: {
2147 BT_INFO("OAL Event: GATT Server Indication confirmed");
2148 __bt_handle_gatt_server_indicate_confirmed((event_gatts_ind_cnfrm_t *)event_data);
2151 case OAL_EVENT_GATTS_NOTIFICATION: { /* Tizen Platform Specific */
2152 BT_INFO("OAL Event: GATT Server DisConnected");
2153 __bt_handle_gatt_server_notification_changed((event_gatts_notif_t *)event_data);
2156 case OAL_EVENT_GATTS_MTU_CHANGED: {
2157 BT_INFO("OAL Event: GATT Server MTU changed event callback");
2158 __bt_handle_gatt_mtu_changed_event((event_gatts_mtu_changed_t *)event_data);
2161 #ifdef TIZEN_GATT_CLIENT
2162 case OAL_EVENT_GATTC_REGISTRATION: {
2163 BT_INFO("OAL Event: GATT Client instance Registered");
2164 __bt_handle_client_instance_registered((event_gattc_register_t *) event_data);
2167 case OAL_EVENT_GATTC_CONNECTION_COMPLETED: {
2168 BT_INFO("OAL Event: GATT Client Connected");
2169 __bt_handle_client_connected((event_gattc_conn_t *) event_data);
2172 case OAL_EVENT_GATTC_DISCONNECTION_COMPLETED: {
2173 BT_INFO("OAL Event: GATT Client DisConnected");
2174 __bt_handle_client_disconnected((event_gattc_conn_t *) event_data);
2177 case OAL_EVENT_GATTC_SERVICE_SEARCH_RESULT: {
2178 BT_DBG("OAL Event: GATT Client Service Search Result");
2179 __bt_handle_client_service_search_result((event_gattc_service_result_t *) event_data);
2182 case OAL_EVENT_GATTC_SERVICE_SEARCH_DONE: {
2183 BT_INFO("OAL Event: GATT Client Service Completed");
2184 __bt_handle_client_service_search_completed((event_gattc_conn_status_t *) event_data);
2187 case OAL_EVENT_GATTC_CHARAC_SERACH_RESULT: {
2188 BT_DBG("OAL Event: GATT Client Characteristic Search Result");
2189 __bt_handle_client_characteristic_search_result((event_gattc_characteristic_result_t *) event_data);
2192 case OAL_EVENT_GATTC_DESC_SERACH_RESULT: {
2193 BT_DBG("OAL Event: GATT Client Descriptor Search Result");
2194 __bt_handle_client_descriptor_search_result((event_gattc_descriptor_result_t *) event_data);
2197 case OAL_EVENT_GATTC_READ_CHARAC: {
2198 BT_DBG("OAL Event: GATT Client Characteristic Read Data");
2199 __bt_handle_client_characteristic_read_data((event_gattc_read_data *) event_data);
2202 case OAL_EVENT_GATTC_READ_DESCR: {
2203 BT_DBG("OAL Event: GATT Client Descriptor Read Data");
2204 __bt_handle_client_descriptor_read_data((event_gattc_read_data *) event_data);
2207 case OAL_EVENT_GATTC_WRITE_CHARAC: {
2208 BT_DBG("OAL Event: GATT Client Characteristic Write Data");
2209 __bt_handle_client_characteristic_write_data((event_gattc_write_data *) event_data);
2212 case OAL_EVENT_GATTC_WRITE_DESCR: {
2213 BT_DBG("OAL Event: GATT Client Descriptor Write Data");
2214 __bt_handle_client_descriptor_write_data((event_gattc_write_data *) event_data);
2217 case OAL_EVENT_DEVICE_LE_DISCONNECTED: {
2218 BT_INFO("OAL Event: LE device disconnected");
2219 __bt_hanlde_le_device_disconnection((event_dev_conn_status_t *)event_data);
2222 case OAL_EVENT_GATTC_NOTIFICATION_REGISTERED: {
2223 BT_INFO("OAL Event: GATT Client Notification Registered");
2224 __bt_handle_client_notification_registered((event_gattc_regdereg_notify_t *) event_data, TRUE);
2227 case OAL_EVENT_GATTC_NOTIFICATION_DEREGISTERED: {
2228 BT_INFO("OAL Event: GATT Client Notification Registered");
2229 __bt_handle_client_notification_registered((event_gattc_regdereg_notify_t *) event_data, FALSE);
2232 case OAL_EVENT_GATTC_NOTIFY_DATA: {
2233 BT_DBG("OAL Event: GATT Client Notification Data");
2234 __bt_handle_client_notification_data((event_gattc_notify_data *) event_data);
2237 case OAL_EVENT_GATTC_SERVICE_CHANGED_IND: {
2238 BT_INFO("OAL Event: GATT Client service changed indication");
2239 __bt_handle_client_service_changed_ind((event_gattc_service_changed_data *)event_data);
2242 case OAL_EVENT_GATTC_MTU_EXCHANGE_COMPLETED: {
2243 BT_INFO("OAL Event: GATT Client MTU Exchange Complete");
2244 __bt_handle_client_mtu_exchange_completed((event_gattc_mtu_configured_t *) event_data);
2253 int _bt_gatt_server_add_service(char *sender, int service_type,
2254 int num_handles, char *svc_uuid, int instance_id)
2256 BT_CHECK_PARAMETER(svc_uuid, return);
2257 BT_CHECK_PARAMETER(sender, return);
2258 int ret = OAL_STATUS_SUCCESS;
2260 oal_gatt_srvc_id_t svc_data;
2262 svc_data.is_prmry = service_type;
2263 svc_data.id.inst_id = instance_id;
2265 BT_INFO("Service UUID [%s] Num handles [%d] Instance ID [%d]", svc_uuid, num_handles, instance_id);
2266 _bt_string_to_uuid(svc_uuid, (service_uuid_t*)&svc_data.id.uuid);
2268 ret = gatts_add_service(instance_id, &svc_data, num_handles);
2269 if (ret != OAL_STATUS_SUCCESS) {
2270 BT_ERR("ret: %d", ret);
2271 return BLUETOOTH_ERROR_INTERNAL;
2274 return BLUETOOTH_ERROR_NONE;
2278 int _bt_gatt_server_add_included_service(char *sender, int instance_id,
2279 int service_handle, int included_svc_handle)
2281 BT_CHECK_PARAMETER(sender, return);
2282 int ret = OAL_STATUS_SUCCESS;
2284 ret = gatts_add_included_services(instance_id, service_handle, included_svc_handle);
2285 if (ret != OAL_STATUS_SUCCESS) {
2286 BT_ERR("ret: %d", ret);
2287 return BLUETOOTH_ERROR_INTERNAL;
2289 return BLUETOOTH_ERROR_NONE;
2292 int _bt_gatt_server_add_characteristic(char *sender, char *char_uuid,
2293 bluetooth_gatt_server_attribute_params_t *param)
2295 BT_CHECK_PARAMETER(char_uuid, return);
2296 BT_CHECK_PARAMETER(sender, return);
2297 BT_CHECK_PARAMETER(param, return);
2298 int ret = OAL_STATUS_SUCCESS;
2300 oal_uuid_t uuid = {{0} };
2302 BT_INFO("Char UUID [%s] Instance ID [%d]", char_uuid, param->instance_id);
2303 _bt_string_to_uuid(char_uuid, (service_uuid_t*)&uuid);
2305 BT_INFO("Char permission From API [0x%x]", param->permissions);
2307 ret = gatts_add_characteristics(param->instance_id, param->service_handle, &uuid,
2308 param->properties, (int)param->permissions);
2309 if (ret != OAL_STATUS_SUCCESS) {
2310 BT_ERR("ret: %d", ret);
2311 return BLUETOOTH_ERROR_INTERNAL;
2313 return BLUETOOTH_ERROR_NONE;
2316 int _bt_gatt_server_add_descriptor(char *sender, char *desc_uuid,
2317 bt_gatt_permission_t *param, int service_handle, int instance_id)
2319 BT_CHECK_PARAMETER(desc_uuid, return);
2320 BT_CHECK_PARAMETER(sender, return);
2321 BT_CHECK_PARAMETER(param, return);
2322 int ret = OAL_STATUS_SUCCESS;
2324 oal_uuid_t uuid = {{0} };
2326 BT_INFO("Descriptor UUID [%s] Instance ID [%d] Service handle [%d]",
2327 desc_uuid, service_handle, instance_id);
2329 _bt_string_to_uuid(desc_uuid, (service_uuid_t*)&uuid);
2331 BT_INFO("Descriptor permission From API [0x%x]", *param);
2332 ret = gatts_add_descriptor(instance_id, service_handle, &uuid, (int)*param);
2334 if (ret != OAL_STATUS_SUCCESS) {
2335 BT_ERR("ret: %d", ret);
2336 return BLUETOOTH_ERROR_INTERNAL;
2338 return BLUETOOTH_ERROR_NONE;
2341 int _bt_gatt_server_start_service(char *sender, int service_handle, int instance_id)
2343 BT_CHECK_PARAMETER(sender, return);
2344 int ret = OAL_STATUS_SUCCESS;
2346 ret = gatts_start_service(instance_id, service_handle, BT_GATT_TRANSPORT_LE);
2347 if (ret != OAL_STATUS_SUCCESS) {
2348 BT_ERR("ret: %d", ret);
2349 return BLUETOOTH_ERROR_INTERNAL;
2351 return BLUETOOTH_ERROR_NONE;
2354 int _bt_gatt_server_stop_service(char *sender, int service_handle, int instance_id)
2356 BT_CHECK_PARAMETER(sender, return);
2357 int ret = OAL_STATUS_SUCCESS;
2359 ret = gatts_stop_service(instance_id, service_handle);
2360 if (ret != OAL_STATUS_SUCCESS) {
2361 BT_ERR("ret: %d", ret);
2362 return BLUETOOTH_ERROR_INTERNAL;
2364 return BLUETOOTH_ERROR_NONE;
2367 int _bt_gatt_server_delete_service(char *sender, int service_handle, int instance_id)
2369 BT_CHECK_PARAMETER(sender, return);
2370 int ret = OAL_STATUS_SUCCESS;
2374 ret = gatts_delete_service(instance_id, service_handle);
2375 if (ret != OAL_STATUS_SUCCESS) {
2376 BT_ERR("ret: %d", ret);
2377 return BLUETOOTH_ERROR_INTERNAL;
2380 /* Remove the Service Handle */
2381 for (l = numapps[instance_id].service_handles; l != NULL;) {
2383 l = g_slist_next(l);
2384 if (handle && *handle == service_handle) {
2385 BT_INFO("Remove Service handle [%d]", *handle);
2386 numapps[instance_id].service_handles = g_slist_remove(numapps[instance_id].service_handles, handle);
2392 return BLUETOOTH_ERROR_NONE;
2395 int _bt_gatt_server_send_response(char *sender, bluetooth_gatt_att_data_t *data,
2396 bluetooth_gatt_server_response_params_t *param)
2398 BT_CHECK_PARAMETER(sender, return);
2399 BT_CHECK_PARAMETER(data, return);
2400 BT_CHECK_PARAMETER(param, return);
2401 struct gatt_server_req_info *req_info = NULL;
2402 int ret = OAL_STATUS_SUCCESS;
2404 oal_gatt_response_t response;
2406 BT_INFO("GATT Server Response: Req Type [%d] req_id [%d] status [%d] auth_req [%d] offset[%d] data len[%d]",
2407 param->req_type, param->request_id,
2408 param->response_status, param->auth_req,
2409 data->offset, data->length);
2411 /* Search for matching Request in List */
2412 req_info = __bt_gatt_server_find_request_info(param->request_id, param->req_type);
2414 BT_ERR("GATT Server Req Info not found for current response..return Error");
2415 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
2418 memset(&response, 0x00, sizeof(oal_gatt_response_t));
2420 response.handle = req_info->attribute_handle;
2421 response.attr_value.auth_req = param->auth_req;
2422 response.attr_value.handle = req_info->attribute_handle;
2423 response.attr_value.offset = data->offset;
2424 response.attr_value.len = data->length;
2425 memcpy(&response.attr_value.value, &data->data, data->length);
2428 ret = gatts_send_response(req_info->connection_id, param->request_id,
2429 param->response_status, &response);
2431 if (ret != OAL_STATUS_SUCCESS) {
2432 BT_ERR("ret: %d", ret);
2433 return BLUETOOTH_ERROR_INTERNAL;
2436 BT_INFO("GATT Server Response successfully sent");
2437 /* Remove GATT server request from list */
2438 gatt_server_requests = g_slist_remove(gatt_server_requests, req_info);
2439 g_free(req_info->addr);
2441 return BLUETOOTH_ERROR_NONE;
2444 int _bt_gatt_server_acquire_send_response(char *sender, bluetooth_gatt_server_acquire_response_params_t *param , void *fd_list)
2446 BT_CHECK_PARAMETER(sender, return);
2447 BT_CHECK_PARAMETER(param, return);
2448 struct gatt_server_req_info *req_info = NULL;
2449 int ret = OAL_STATUS_SUCCESS;
2452 BT_INFO("GATT acquire Server Response: Req Type [%d] req_id [%d] fd [%d] mtu[%d]",
2453 param->req_type, param->request_id,
2457 /* Search for matching Request in List */
2458 req_info = __bt_gatt_server_find_request_info(param->request_id, param->req_type);
2460 BT_ERR("GATT acquire Server Req Info not found for current response..return Error");
2461 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
2464 ret = gatt_send_response_acquire(req_info->connection_id, param->request_id, 0, param->fd, param->mtu, fd_list);
2466 if (ret != OAL_STATUS_SUCCESS) {
2467 BT_ERR("ret: %d", ret);
2468 return BLUETOOTH_ERROR_INTERNAL;
2471 BT_INFO("GATT acquire Server Response successfully sent");
2472 /* Remove GATT server request from list */
2473 gatt_server_requests = g_slist_remove(gatt_server_requests, req_info);
2474 g_free(req_info->addr);
2476 return BLUETOOTH_ERROR_NONE;
2481 int _bt_gatt_server_send_indication(char *sender, bluetooth_device_address_t *dev_addr,
2482 bluetooth_gatt_att_data_t *data,
2483 bluetooth_gatt_server_indication_params_t *param)
2485 BT_CHECK_PARAMETER(sender, return);
2486 BT_CHECK_PARAMETER(data, return);
2487 BT_CHECK_PARAMETER(param, return);
2489 gboolean all_send = FALSE;
2490 int ret = OAL_STATUS_SUCCESS;
2491 struct gatt_client_info_t *conn;
2493 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2494 _bt_convert_addr_type_to_string(address, dev_addr->addr);
2496 if (memcmp(dev_addr->addr, BDADDR_ANY, 6) == 0) {
2497 BT_INFO("GATT Server: Send Indication to all connected GATT clients..");
2500 BT_INFO("GATT Server: Send Indication to connected GATT client addr [%s]", address);
2503 /* Attempt to send Notification/Indication to all Connected GATT clients */
2505 ret = __bt_gatt_send_indication_to_all_connected_clients(data, param);
2506 if (ret != OAL_STATUS_SUCCESS) {
2507 BT_ERR("ret: %d", ret);
2509 return BLUETOOTH_ERROR_INTERNAL;
2513 conn = _bt_find_remote_gatt_client_info(address);
2515 ret = gatts_send_indication(param->instance_id, param->atrribute_handle,
2516 conn->connection_id, data->length,
2517 param->need_confirmation, (char *)(&data->data[0]));
2519 if (ret != OAL_STATUS_SUCCESS) {
2520 BT_ERR("ret: %d", ret);
2521 BT_INFO("Indication failed to send to Remote GATT Client [%s]", address);
2523 return BLUETOOTH_ERROR_INTERNAL;
2525 BT_INFO("Indication sent to Remote GATT Client [%s] wait for Notification completed event from OAL", address);
2527 num_indicate_clients = 1;
2528 return BLUETOOTH_ERROR_NONE;
2530 BT_ERR("Remote GATT client [%s] is not connected..Cant send Indication!!", address);
2532 return BLUETOOTH_ERROR_NOT_CONNECTED;
2535 return BLUETOOTH_ERROR_NONE;
2538 int _bt_gatt_server_update_attribute_value(char *sender, int instance_id,
2539 bluetooth_gatt_server_update_value_t *param)
2541 BT_CHECK_PARAMETER(sender, return);
2542 BT_CHECK_PARAMETER(param, return);
2543 int ret = OAL_STATUS_SUCCESS;
2545 oal_gatt_value_t value;
2546 BT_INFO("GATT Server Update value: Instance ID [%d] attr handle [%d] Value len [%d]",
2547 instance_id, param->attribute_handle, param->length);
2550 memset(&value, 0x00, sizeof(oal_gatt_value_t));
2552 value.handle = param->attribute_handle;
2553 value.len = param->length;
2554 memcpy(&value.value, ¶m->data.data, param->length);
2556 ret = gatts_update_att_value(instance_id, &value);
2558 if (ret != OAL_STATUS_SUCCESS) {
2559 BT_ERR("ret: %d", ret);
2560 return BLUETOOTH_ERROR_INTERNAL;
2563 BT_INFO("GATT Server Update Attribute Value successfully");
2564 return BLUETOOTH_ERROR_NONE;
2567 int _bt_request_att_mtu(bluetooth_device_address_t *device_address,
2570 struct gatt_server_info_t *conn_info = NULL;
2572 int ret = OAL_STATUS_SUCCESS;
2574 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
2575 _bt_convert_addr_type_to_string(addr, device_address->addr);
2577 /* Check if remote GATT Server is connected or not */
2578 conn_info = _bt_find_remote_gatt_server_info(addr);
2580 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
2581 conn_info->addr, conn_info->connection_id);
2583 BT_ERR("GATT Server is not yet connected..");
2585 return BLUETOOTH_ERROR_NOT_CONNECTED;
2588 ret = gattc_configure_mtu(conn_info->connection_id, mtu);
2589 if (ret != OAL_STATUS_SUCCESS) {
2590 BT_ERR("ret: %d", ret);
2592 return BLUETOOTH_ERROR_INTERNAL;
2596 return BLUETOOTH_ERROR_NONE;
2599 int _bt_get_att_mtu(bluetooth_device_address_t *address,
2602 BT_CHECK_PARAMETER(address, return);
2603 BT_CHECK_PARAMETER(mtu, return);
2604 struct gatt_client_info_t *conn_info = NULL;
2605 char addr[BT_ADDRESS_STRING_SIZE] = { 0 };
2606 int ret = OAL_STATUS_SUCCESS;
2609 _bt_convert_addr_type_to_string(addr, address->addr);
2611 BT_INFO("Get current MTU size for the remote client:DevAddress:[%s]", addr);
2613 conn_info = _bt_find_remote_gatt_client_info(addr);
2615 BT_INFO("GATT Client [%s] is connected, conn Id [%d] Instance ID [%d]",
2616 conn_info->addr, conn_info->connection_id, conn_info->instance_id);
2618 BT_ERR("GATT Client [%s] is not yet connected..", addr);
2619 return BLUETOOTH_ERROR_NOT_CONNECTED;
2622 ret = gatts_get_att_mtu(conn_info->connection_id, &stack_mtu);
2623 if (ret != OAL_STATUS_SUCCESS) {
2624 BT_ERR("ret: %d", ret);
2625 return BLUETOOTH_ERROR_INTERNAL;
2628 BT_INFO("ATT MTU received from OAL [%d]", stack_mtu);
2629 *mtu = (unsigned int)stack_mtu;
2632 BT_ERR("MTU value is zero, GATT Client [%s] is not yet connected..", addr);
2633 return BLUETOOTH_ERROR_NOT_CONNECTED;
2636 return BLUETOOTH_ERROR_NONE;
2639 #ifdef TIZEN_GATT_CLIENT
2640 /* GATT Client utility static functions */
2641 static bt_gatt_service_info_list_t *__bt_get_service_info_list(int conn_id)
2644 bt_gatt_service_info_list_t *info = NULL;
2646 for (l = list_gatt_info; l != NULL; l = g_slist_next(l)) {
2647 info = (bt_gatt_service_info_list_t *)l->data;
2651 if (info->conn_id == conn_id)
2658 static bt_gatt_service_info_t *__bt_find_matching_service(
2659 bt_gatt_service_info_list_t *svc_list, oal_gatt_srvc_id_t *svc)
2662 bt_gatt_service_info_t *info = NULL;
2664 for (l = svc_list->services; l != NULL; l = g_slist_next(l)) {
2665 info = (bt_gatt_service_info_t *)l->data;
2669 /* Match UUID and instance ID */
2670 if (!memcmp(&svc->id.uuid.uuid, &info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
2671 && (svc->id.inst_id == info->inst_id)) {
2678 static bt_gatt_char_info_t *__bt_find_matching_charc(
2679 bt_gatt_service_info_t *svc_info, oal_gatt_id_t *charc)
2682 bt_gatt_char_info_t *info = NULL;
2684 for (l = svc_info->chars; l != NULL; l = g_slist_next(l)) {
2685 info = (bt_gatt_char_info_t *)l->data;
2689 /* Match UUID and instance ID */
2690 if (!memcmp(&charc->uuid.uuid, &info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
2691 && (charc->inst_id == info->inst_id)) {
2698 static bt_gatt_descriptor_info_t *__bt_find_matching_desc(
2699 bt_gatt_char_info_t *char_info, oal_gatt_id_t *desc)
2702 bt_gatt_descriptor_info_t *info = NULL;
2704 for (l = char_info->descs; l != NULL; l = g_slist_next(l)) {
2705 info = (bt_gatt_descriptor_info_t *)l->data;
2709 /* Match UUID and instance ID */
2710 if (!memcmp(&desc->uuid, &info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
2711 && (desc->inst_id == info->inst_id)) {
2719 static struct gatt_server_info_t *__bt_find_remote_gatt_server_info_from_conn_id(int conn_id)
2722 struct gatt_server_info_t *info = NULL;
2724 for (l = gatt_server_info_list; l != NULL; l = g_slist_next(l)) {
2725 info = (struct gatt_server_info_t*)l->data;
2729 if (info->connection_id == conn_id) {
2730 BT_INFO("Remote GATT server found addr[%s]", info->addr);
2737 static bt_gatt_service_info_t* __bt_find_removed_service(bt_gatt_service_info_list_t *svc_list)
2740 bt_gatt_service_info_t *info = NULL;
2742 for (l = svc_list->services; l != NULL; l = g_slist_next(l)) {
2743 info = (bt_gatt_service_info_t*)l->data;
2747 /* Service is marked a removed */
2748 if (info->is_removed == 1)
2754 static void __bt_remove_service_info_from_list(bt_gatt_service_info_t *svc_info)
2760 bt_gatt_char_info_t *charc = NULL;
2761 bt_gatt_included_service_info_t *incl = NULL;
2762 bt_gatt_descriptor_info_t *desc = NULL;
2764 /* Remove all Characteristic and Descriptors within characteristic */
2765 for (l = svc_info->chars; l != NULL;) {
2766 charc = (bt_gatt_char_info_t*)l->data;
2767 l = g_slist_next(l); /* Incase if l is removed, saving next to l */
2772 /* Inside Characteristic */
2773 for (l1 = charc->descs; l1 != NULL;) {
2775 desc = (bt_gatt_descriptor_info_t*)l1->data;
2776 l1 = g_slist_next(l1);
2781 /* Remove Descriptor */
2782 charc->descs = g_slist_remove(charc->descs, desc);
2785 /* Remove Characteristic */
2786 svc_info->chars = g_slist_remove(svc_info->chars, charc);
2790 /* Remove all Included Services */
2791 for (l2 = svc_info->included_svcs; l2 != NULL;) {
2792 incl = (bt_gatt_included_service_info_t*)l2->data;
2793 l2 = g_slist_next(l2); /* Incase if l is removed, saving next to l */
2798 /* Remove included service */
2799 svc_info->included_svcs = g_slist_remove(svc_info->included_svcs, incl);
2806 static void __bt_build_service_browse_info(int conn_id,
2807 bt_services_browse_info_t* info)
2810 bt_gatt_service_info_list_t *svc_info_list;
2811 bt_gatt_service_info_t *svc_info;
2813 service_uuid_t uuid;
2814 struct gatt_server_info_t *conn_info = NULL;
2816 char uuid_string[BLUETOOTH_UUID_STRING_MAX];
2818 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
2823 _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
2825 svc_info_list = __bt_get_service_info_list(conn_id);
2829 info->count = g_slist_length(svc_info_list->services);
2830 BT_DBG("Total services present in the svc info list for this conn id [%d] is [%d]",
2831 conn_id, info->count);
2833 for (l = svc_info_list->services; l != NULL; l = g_slist_next(l)) {
2834 svc_info = (bt_gatt_service_info_t*)l->data;
2835 if (svc_info == NULL)
2838 memcpy(&uuid.uuid, &svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2839 _bt_uuid_to_string(&uuid, uuid_string);
2841 BT_INFO("[%d] %s [%s]", count, uuid_string, _bt_convert_uuid_to_string(uuid_string));
2843 /* Fill UUID of service */
2844 g_strlcpy(info->uuids[count], uuid_string,
2845 BLUETOOTH_UUID_STRING_MAX);
2847 /* Fill instance ID of service */
2848 info->inst_id[count] = svc_info->inst_id;
2850 /* Fill primary service or not info */
2851 info->primary[count] = svc_info->is_primary;
2853 /* Increment count of services browsed */
2858 static void __bt_build_char_browse_info(int conn_id,
2859 bt_gatt_service_info_t *svc_info,
2860 bt_char_browse_info_t* info)
2863 bt_gatt_char_info_t *char_info;
2864 service_uuid_t uuid;
2866 struct gatt_server_info_t *conn_info = NULL;
2868 char uuid_string[BLUETOOTH_UUID_STRING_MAX];
2870 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
2874 /* Fill default data, this will be required even in case of failure */
2875 _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
2876 memcpy(&info->svc_uuid, svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2877 info->svc_inst_id = svc_info->inst_id;
2879 if (!svc_info->chars) {
2880 BT_ERR("No Chars browsed for address [%s]", conn_info->addr);
2884 info->count = g_slist_length(svc_info->chars);
2886 for (l = svc_info->chars; l != NULL; l = g_slist_next(l)) {
2887 char_info = (bt_gatt_char_info_t*)l->data;
2888 if (char_info == NULL)
2891 memcpy(&uuid.uuid, &char_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2892 _bt_uuid_to_string(&uuid, uuid_string);
2894 /* Fill UUID of characteristic */
2895 g_strlcpy(info->uuids[count], uuid_string,
2896 BLUETOOTH_UUID_STRING_MAX);
2898 /* Fill instance ID of characteristic */
2899 info->inst_id[count] = char_info->inst_id;
2901 /* Fill property of characteristic */
2902 info->props[count] = char_info->props;
2904 /* Increment count of services browsed */
2907 BT_DBG("Total characteristics browsed [%d]", count);
2910 static void __bt_build_descriptor_browse_info(int conn_id,
2911 bt_gatt_service_info_t *svc_info,
2912 bt_gatt_char_info_t *char_info,
2913 bt_descriptor_browse_info_t* info)
2916 bt_gatt_descriptor_info_t *desc_info;
2918 service_uuid_t uuid;
2919 struct gatt_server_info_t *conn_info = NULL;
2921 char uuid_string[BLUETOOTH_UUID_STRING_MAX];
2923 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
2925 /* Fill default data, this will be required even in case of failure */
2926 _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
2927 memcpy(&info->svc_uuid, svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2928 info->svc_inst_id = svc_info->inst_id;
2929 memcpy(&info->char_uuid, char_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2930 info->char_inst_id = char_info->inst_id;
2932 /* Fill property of the parent characteristic of this descriptor */
2933 info->char_props_map = char_info->props;
2935 info->count = g_slist_length(char_info->descs);
2937 if (!char_info->descs) {
2938 BT_ERR("No Descriptors browsed for address [%s]", conn_info->addr);
2942 for (l = char_info->descs; l != NULL; l = g_slist_next(l)) {
2943 desc_info = (bt_gatt_descriptor_info_t*)l->data;
2944 if (desc_info == NULL)
2947 memcpy(&uuid.uuid, &desc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2948 _bt_uuid_to_string(&uuid, uuid_string);
2950 /* Fill UUID of Descriptor */
2951 g_strlcpy(info->uuids[count], uuid_string,
2952 BLUETOOTH_UUID_STRING_MAX);
2954 /* Fill instance ID of Descriptor */
2955 info->inst_id[count] = desc_info->inst_id;
2958 /* Increment count of Descriptor browsed */
2962 BT_INFO("Total descriptors browsed [%d]", count);
2965 static void __bt_free_service_info(bt_gatt_service_info_t *svc)
2967 GSList *ll, *lll, *llll;
2968 bt_gatt_char_info_t *chr = NULL;
2969 bt_gatt_descriptor_info_t *desc = NULL;
2970 bt_gatt_included_service_info_t *incl_svc = NULL;
2972 BT_DBG("Service info Is Prim[%d] Inst ID [%d]", svc->is_primary, svc->inst_id);
2973 /* Delete all chars and its descriptors */
2974 for (ll = svc->chars; ll != NULL; ) {
2975 chr = (bt_gatt_char_info_t *)ll->data;
2976 ll = g_slist_next(ll);
2980 for (lll = chr->descs; lll != NULL; ) {
2981 desc = (bt_gatt_descriptor_info_t *)lll->data;
2982 lll = g_slist_next(lll);
2985 chr->descs = g_slist_remove(chr->descs, desc);
2988 svc->chars = g_slist_remove(svc->chars, chr);
2992 /* Delete all included services */
2993 for (llll = svc->included_svcs; llll != NULL; ) {
2994 incl_svc = (bt_gatt_included_service_info_t *)llll->data;
2995 llll = g_slist_next(llll);
2996 if (incl_svc == NULL)
2998 svc->included_svcs = g_slist_remove(svc->included_svcs, incl_svc);
3003 static void __bt_cleanup_remote_services(struct gatt_server_info_t *conn_info)
3005 bt_gatt_service_info_list_t *svc_info_list = NULL;
3006 bt_gatt_service_info_t *svc = NULL;
3009 svc_info_list = __bt_get_service_info_list(conn_info->connection_id);
3010 if (!svc_info_list) {
3011 BT_INFO("Could not find Svc Info list for the connection ID [%d]",
3012 conn_info->connection_id);
3016 BT_INFO("Start Cleanup of all services. Num Services [%d]", g_slist_length(svc_info_list->services));
3017 for (l = svc_info_list->services; l != NULL; ) {
3018 svc = (bt_gatt_service_info_t *)l->data;
3019 l = g_slist_next(l);
3023 __bt_free_service_info(svc);
3024 svc_info_list->services = g_slist_remove(svc_info_list->services, svc);
3028 list_gatt_info = g_slist_remove(list_gatt_info, svc_info_list);
3029 g_free(svc_info_list);
3032 int _bt_register_gatt_client_instance(const char *sender,
3033 bluetooth_device_address_t *address)
3035 int ret = OAL_STATUS_SUCCESS;
3036 char *uuid_string = NULL;
3041 /* App should ensure that it should not send */
3042 BT_INFO("Check on which instance GATT Client instance can be initialized....");
3043 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
3044 if (numapps[k].is_initialized == 1) {
3045 BT_INFO("Instance ID [%d] is already in use..Check next slot",
3046 numapps[k].instance_id);
3049 BT_INFO("Time to register GATT client instancer..UUID to be used is [%s] slot [%d]",
3050 uuid_list[slot-1], slot);
3056 BT_ERR("No Slot if free for GATT Client registration..");
3057 return BLUETOOTH_ERROR_REGISTRATION_FAILED;
3060 uuid_string = g_malloc0(BT_UUID_STRING_MAX);
3061 _bt_string_to_uuid(uuid_list[slot-1], (service_uuid_t*)&uuid);
3062 g_strlcpy(uuid_string, uuid_list[slot-1], BT_UUID_STRING_MAX);
3063 BT_INFO("Copied UUID string [%s]", uuid_string);
3065 /* Register GATT Client */
3066 ret = gattc_register(&uuid);
3067 if (ret != OAL_STATUS_SUCCESS) {
3068 BT_ERR("ret: %d", ret);
3069 g_free(uuid_string);
3070 return BLUETOOTH_ERROR_INTERNAL;
3073 BT_INFO("GATT Client registration call successfully accepted by OAL..wait for Instance Initialized event from OAL..");
3075 /* Return & wait for GATT Client Instance Initialization event */
3076 memset(numapps[slot].sender, 0x00, sizeof(numapps[slot].sender));
3077 memset(numapps[slot].uuid, 0x00, sizeof(numapps[slot].uuid));
3079 g_strlcpy(numapps[slot].sender, sender, sizeof(numapps[slot].sender));
3080 g_strlcpy(numapps[slot].uuid, uuid_string, sizeof(numapps[slot].uuid));
3082 /* Address is saved here. When event comes, sender + address are matched for replying pending
3083 request. It is impossible for same sender to have requests with two same addresses */
3084 memcpy(&numapps[slot].address.addr, address->addr, sizeof(bluetooth_device_address_t));
3086 BT_INFO("Slot [%d] occupied", slot);
3087 numapps[slot].is_initialized = TRUE; /* Set initialization to true here itself */
3089 g_free(uuid_string);
3090 return BLUETOOTH_ERROR_NONE;
3096 /* GATT client events */
3097 static void __bt_handle_client_instance_registered(event_gattc_register_t *data)
3099 bt_service_app_info_t *info = NULL;
3101 char *uuid_string = g_malloc0(BT_UUID_STRING_MAX);
3103 _bt_uuid_to_string(&(data->client_uuid), uuid_string);
3104 BT_INFO("CLient ID is Initialized [%d] UUID initialized [%s]", data->client_if, uuid_string);
3106 /* Platform GATT client framwork does not use Default GATT client instance
3107 This GATT client instance is never deregistred in the lifetime of bt-service */
3108 if (g_strcmp0(uuid_string, DEFAULT_GATT_CLIENT_UUID) == 0) {
3109 BT_INFO("Default client Instance Registered");
3110 gatt_default_client = data->client_if;
3111 g_free(uuid_string);
3115 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
3118 if (g_strcmp0(info->uuid, uuid_string) == 0) {
3119 BT_INFO("Found GATT client.. sender [%s] Slot [%d] occupied", info->sender, k);
3120 info->is_initialized = TRUE;
3121 info->client_id = data->client_if;
3122 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE, BT_GATT_CLIENT_REGISTER,
3123 (void*)info, sizeof(bt_service_app_info_t));
3127 g_free(uuid_string);
3130 static void __bt_handle_client_connected(event_gattc_conn_t *event_data)
3132 int result = BLUETOOTH_ERROR_NONE;
3133 struct gatt_server_info_t *conn_info = NULL;
3134 struct gatt_out_conn_info_t *out_conn_info = NULL;
3136 GVariant *param = NULL;
3138 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
3139 _bt_convert_addr_type_to_string(address,
3140 (unsigned char *)event_data->address.addr);
3142 if (event_data->status != OAL_STATUS_SUCCESS)
3143 result = BLUETOOTH_ERROR_INTERNAL;
3145 /* DBUS Return fo BT_CONNECT_LE for all the apps */
3146 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE, address,
3147 BT_ADDRESS_STRING_SIZE);
3149 BT_INFO("Local GATT Client Connected: Remote addr[%s] Client Interface [%d] Connection ID [%d] status[%d]",
3150 address, event_data->client_if, event_data->conn_id, event_data->status);
3152 if (result == BLUETOOTH_ERROR_NONE) {
3153 /* Check if device is already in connected list */
3154 conn_info = _bt_find_remote_gatt_server_info(address);
3157 /* Send event to BT-API */
3158 param = g_variant_new("(is)", result, address);
3159 _bt_send_event(BT_DEVICE_EVENT,
3160 BLUETOOTH_EVENT_GATT_CLIENT_CONNECTED, /* Local device is GATT client */
3163 /* Save Connection info */
3164 conn_info = g_new0(struct gatt_server_info_t, 1);
3165 conn_info->addr = g_strdup(address);
3166 conn_info->client_id = event_data->client_if;
3167 #ifdef __INTEGRATE_GATT_INFO__
3168 conn_info->instance_id = -1;
3170 conn_info->connection_id = event_data->conn_id;
3171 gatt_server_info_list = g_slist_append(gatt_server_info_list, conn_info);
3172 BT_DBG("Total num of connected Remote GATT server devices [%d]",
3173 g_slist_length(gatt_server_info_list));
3176 BT_INFO("Do a Internal refresh");
3177 if (OAL_STATUS_SUCCESS != gattc_refresh(conn_info->client_id, &event_data->address))
3178 BT_ERR("GATT database refresh failed!!");
3180 BT_INFO("GATT database refresh Success!!");
3183 BT_ERR("Local GATT Client connected event for addr[%s], but device is in connected list already", address);
3185 __bt_add_mtu_gatt_device(address);
3187 BT_ERR("GATT Client Connection failed!!");
3189 /* If outgoing connection Info is present, then remove it */
3190 out_conn_info = __bt_find_gatt_outgoing_conn_info(address);
3191 if (out_conn_info) {
3192 BT_ERR("Outgoing Client connect request was sent");
3193 outgoing_gatt_conn_list = g_slist_remove(outgoing_gatt_conn_list, out_conn_info);
3194 g_free(out_conn_info->addr);
3195 g_free(out_conn_info);
3200 static void __bt_handle_client_disconnected(event_gattc_conn_t *event_data)
3202 int result = BLUETOOTH_ERROR_NONE;
3204 struct gatt_server_info_t *conn_info = NULL;
3205 struct gatt_out_conn_info_t *out_conn_info = NULL;
3207 GVariant *param = NULL;
3209 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
3210 _bt_convert_addr_type_to_string(address,
3211 (unsigned char *)event_data->address.addr);
3213 if (event_data->status != OAL_STATUS_SUCCESS)
3214 result = BLUETOOTH_ERROR_INTERNAL;
3216 if (NULL == _bt_get_request_info_data(BT_DISCONNECT_LE, address)) {
3217 if (NULL != _bt_get_request_info_data(BT_CONNECT_LE, address)) {
3218 result = BLUETOOTH_ERROR_INTERNAL;
3219 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
3220 address, BT_ADDRESS_STRING_SIZE);
3221 BT_ERR("Failed to connect Local GATT Remote addr[%s]", address);
3226 /* DBUS Return for BT_DISCONNECT_LE for all the apps */
3227 __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE, address,
3228 BT_ADDRESS_STRING_SIZE);
3230 BT_INFO("Local GATT Client DisConnected: Remote addr[%s] Client Interface [%d] Connection ID [%d] status [%d]",
3231 address, event_data->client_if, event_data->conn_id, event_data->status);
3233 /* Remove Connection info */
3234 conn_info = _bt_find_remote_gatt_server_info(address);
3237 param = g_variant_new("(is)", result, address);
3238 /* Send event to application */
3239 _bt_send_event(BT_DEVICE_EVENT,
3240 BLUETOOTH_EVENT_GATT_CLIENT_DISCONNECTED,
3243 BT_INFO("Remove GATT server info from List..");
3244 /* Remove all services from info list_gatt_info */
3245 __bt_cleanup_remote_services(conn_info);
3247 /* Remove info from List */
3248 gatt_server_info_list = g_slist_remove(gatt_server_info_list, conn_info);
3249 BT_INFO("Total num of connected GATT servers [%d]", g_slist_length(gatt_server_info_list));
3250 g_free(conn_info->addr);
3253 BT_INFO("Can not find conn info, already removed!");
3255 __bt_remove_mtu_gatt_device(address);
3257 /* If outgoing connection Info is present, then remove it */
3258 out_conn_info = __bt_find_gatt_outgoing_conn_info(address);
3259 if (out_conn_info) {
3260 BT_ERR("Client Disconnected event, but outgoing connect request was sent");
3261 outgoing_gatt_conn_list = g_slist_remove(outgoing_gatt_conn_list, out_conn_info);
3262 g_free(out_conn_info->addr);
3263 g_free(out_conn_info);
3269 static void __bt_handle_client_service_search_result(
3270 event_gattc_service_result_t *event_data)
3272 /* Pre: status is never fail from OAL */
3274 /* Find service list from address */
3275 bt_gatt_service_info_list_t *svc_info_list;
3276 bt_gatt_service_info_t *svc_info;
3278 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3279 if (!svc_info_list) {
3280 BT_DBG("Service info list not present for connection ID %d, means first time browse", event_data->conn_status.conn_id);
3281 /* Means for this conn_id, no services are ever browsed, first time,
3282 create service info list for this conn_id */
3283 svc_info_list = g_malloc0(sizeof(bt_gatt_service_info_list_t));
3284 svc_info_list->conn_id = event_data->conn_status.conn_id;
3285 list_gatt_info = g_slist_append(list_gatt_info, svc_info_list);
3288 /* send list and current service's uuid and instance id to find it */
3289 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3292 /* If not found, check if service changed, if yes, means this is a new service added
3293 in remote GATT device, update uuid info in svc info list structure, to be used when
3294 search is completed */
3296 if (svc_info_list->info.is_changed) {
3297 BT_DBG("Service Changed indication already found for connection ID %d", event_data->conn_status.conn_id);
3298 memcpy(svc_info_list->info.uuid, event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3300 /* Create and add new service in service list */
3301 svc_info = g_malloc0(sizeof(bt_gatt_service_info_t));
3302 memcpy(svc_info->uuid, event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3303 svc_info->inst_id = event_data->srvc_id.id.inst_id;
3304 svc_info->is_primary = event_data->srvc_id.is_prmry;
3305 svc_info_list->services = g_slist_append(svc_info_list->services, svc_info);
3306 BT_DBG("Service created and added in Svc info list or connection ID %d", event_data->conn_status.conn_id);
3308 /* If returned matching service info, then just update service_rmeoved value inside it to 0 */
3309 svc_info->is_removed = 0;
3313 static void __bt_handle_client_service_search_completed(
3314 event_gattc_conn_status_t *event_data)
3316 struct gatt_server_info_t *conn_info = NULL;
3317 bt_gatt_service_info_list_t *svc_info_list;
3318 bt_gatt_service_info_t *svc_info;
3319 bt_services_browse_info_t browse_info;
3320 unsigned char uuid_empty[BLUETOOTH_UUID_HEX_MAX_LEN];
3322 memset(&uuid_empty, 0x00, BLUETOOTH_UUID_HEX_MAX_LEN);
3323 memset(&browse_info, 0x00, sizeof(bt_services_browse_info_t));
3324 BT_INFO("Primary Services browsing completed status[%d] conn ID [%d]",
3325 event_data->status, event_data->conn_id);
3327 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(event_data->conn_id);
3329 svc_info_list = __bt_get_service_info_list(event_data->conn_id);
3330 if (!svc_info_list) {
3331 BT_ERR("No services browsed ever for addr [%s]", conn_info->addr);
3333 /* Just build response and return ERROR */
3334 __bt_build_service_browse_info(event_data->conn_id, &browse_info);
3336 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_INTERNAL,
3337 BT_GATT_GET_PRIMARY_SERVICES, &browse_info,
3338 sizeof(bt_services_browse_info_t));
3342 /* If fail, then send event with error */
3343 if (event_data->status != OAL_STATUS_SUCCESS) {
3344 /* Just build response and return ERROR */
3345 __bt_build_service_browse_info(event_data->conn_id, &browse_info);
3347 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_INTERNAL,
3348 BT_GATT_GET_PRIMARY_SERVICES, &browse_info,
3349 sizeof(bt_services_browse_info_t));
3353 /* If success, then find service info list from address */
3355 /* If svc_changed == 1 and uuid valid, means a new service is added*/
3356 if (svc_info_list->info.is_changed && !memcmp(uuid_empty, svc_info_list->info.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)) {
3357 /* TODO: Send event -Service added with instance ID and UUID of newly added service */
3358 BT_INFO("new service added");
3360 BT_INFO("TODO new service added");
3363 /* If svc_changed == 1 and uuid invalid, then a service is removed */
3364 if (svc_info_list->info.is_changed && memcmp(uuid_empty, svc_info_list->info.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)) {
3365 /* Scan through the service info list to find service with is_removed = 1*/
3366 svc_info = __bt_find_removed_service(svc_info_list);
3368 /* TODO Send event - Service removed with instance ID and UUID of just rmeoved service */
3370 /* Remove that service info from service info list */
3371 svc_info_list->services = g_slist_remove(svc_info_list->services, svc_info);
3373 /* Delete that service completely from svc_info list*/
3374 __bt_remove_service_info_from_list(svc_info);
3378 /* Reset svc_changed = 0, and reset UUID = all 0's */
3379 svc_info_list->info.is_changed = 0;
3380 memset(&svc_info_list->info.uuid, 0x00, BLUETOOTH_UUID_HEX_MAX_LEN);
3382 /* Build Reply and send to service browse primary services request of pending apps */
3383 __bt_build_service_browse_info(event_data->conn_id, &browse_info);
3385 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE,
3386 BT_GATT_GET_PRIMARY_SERVICES, &browse_info,
3387 sizeof(bt_services_browse_info_t));
3391 static void __bt_handle_client_characteristic_search_result(
3392 event_gattc_characteristic_result_t *event_data)
3394 bt_gatt_service_info_list_t *svc_info_list;
3395 bt_gatt_service_info_t *svc_info;
3396 bt_gatt_char_info_t *char_info;
3397 bt_char_browse_info_t browse_info;
3399 memset(&browse_info, 0x00, sizeof(bt_char_browse_info_t));
3402 if (event_data->conn_status.status == OAL_STATUS_SUCCESS) {
3403 /* Find service info list from address */
3404 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3405 if (svc_info_list == NULL) {
3406 BT_ERR("svc_info_list is NULL");
3411 /* Find matching service info from svc info list */
3412 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3413 if (svc_info == NULL) {
3414 BT_ERR("svc_info is NULL");
3418 /* Find Matching char from service info in event */
3419 char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
3421 /* If not found, then add new characteristic and return */
3423 BT_DBG("Add new characteristic");
3424 char_info = g_malloc0(sizeof(bt_gatt_char_info_t));
3425 memcpy(char_info->uuid, event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3427 char_info->inst_id = event_data->char_id.inst_id;
3428 char_info->props = event_data->char_prop;
3429 svc_info->chars = g_slist_append(svc_info->chars, char_info);
3431 /* If found, then return */
3432 BT_DBG("update char property as Characteristic browsed is already present");
3433 char_info->props |= event_data->char_prop;
3436 /* If Not success: Means Charc browse is completed */
3437 /* Find char list from service in event */
3438 /* Find service list from address */
3439 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3440 if (svc_info_list == NULL) {
3441 BT_ERR("svc_info_list is NULL");
3445 /* Find service info from service in event */
3446 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3447 if (svc_info == NULL) {
3448 BT_ERR("svc_info is NULL");
3452 /* Build char list from service in event */
3453 __bt_build_char_browse_info(event_data->conn_status.conn_id,
3454 svc_info, &browse_info);
3456 /* Create response and return by sending event*/
3457 /* Build Reply and send to service browse All Included services request of pending apps */
3458 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE,
3459 BT_GATT_GET_SERVICE_PROPERTIES,
3461 sizeof(bt_char_browse_info_t));
3465 static void __bt_handle_client_descriptor_search_result(
3466 event_gattc_descriptor_result_t *event_data)
3468 bt_gatt_service_info_list_t *svc_info_list;
3469 bt_gatt_service_info_t *svc_info;
3470 bt_gatt_char_info_t *char_info;
3471 bt_gatt_descriptor_info_t *desc_info;
3472 bt_descriptor_browse_info_t browse_info;
3474 BT_DBG("descriptor search result status [%d]", event_data->conn_status.status);
3476 memset(&browse_info, 0x00, sizeof(bt_descriptor_browse_info_t));
3479 if (event_data->conn_status.status == OAL_STATUS_SUCCESS) {
3480 /* Find service list from address */
3481 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3482 if (svc_info_list == NULL) {
3483 BT_ERR("svc_info_list is NULL");
3487 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3488 if (svc_info == NULL) {
3489 BT_ERR("svc_info is NULL");
3493 char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
3494 if (char_info == NULL) {
3495 BT_ERR("char_info is NULL");
3499 desc_info = __bt_find_matching_desc(char_info, &event_data->descr_id);
3500 /* If not found, add new descriptor and return */
3502 desc_info = g_malloc0(sizeof(bt_gatt_descriptor_info_t));
3503 memcpy(desc_info->uuid, event_data->descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3505 desc_info->inst_id = event_data->descr_id.inst_id;
3506 char_info->descs = g_slist_append(char_info->descs, desc_info);
3509 /* If found, then return */
3510 BT_INFO("Characteristic browsed is already presesnt");
3513 /* If Not success */
3514 /* Find service list from address */
3515 /* Find included service list from service in event */
3516 /* Create response and return by sending event*/
3517 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3518 if (svc_info_list == NULL) {
3519 BT_ERR("svc_info_list is NULL");
3523 /* Find service info from service in event */
3524 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3525 if (svc_info == NULL) {
3526 BT_ERR("svc_info is NULL");
3530 /* Find char info from char in event */
3531 char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
3532 if (char_info == NULL) {
3533 BT_ERR("char_info is NULL");
3537 /* Build descriptor list from char in event */
3538 __bt_build_descriptor_browse_info(event_data->conn_status.conn_id,
3539 svc_info, char_info, &browse_info);
3542 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE,
3543 BT_GATT_GET_CHARACTERISTIC_PROPERTIES,
3545 sizeof(bt_descriptor_browse_info_t));
3549 static void __bt_handle_client_characteristic_read_data(
3550 event_gattc_read_data *event_data)
3552 int result = BLUETOOTH_ERROR_NONE;
3553 struct gatt_server_info_t *conn_info = NULL;
3554 bluetooth_gatt_client_char_prop_info_t read_info;
3556 /* Read Information data structures */
3557 GVariant *param = NULL;
3558 GVariant *data = NULL;
3559 GVariant *data_svc_uuid = NULL;
3560 GVariant *data_char_uuid = NULL;
3561 char *read_val = NULL;
3562 char *svc_uuid = NULL;
3563 char *char_uuid = NULL;
3566 //memset(&read_info, 0x00, sizeof(bt_gatt_handle_property_t));
3567 memset(&read_info, 0x00, sizeof(bluetooth_gatt_client_char_prop_info_t));
3569 /* Extract Address from conn_id of event data */
3570 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3571 event_data->uuid_status.conn_status.conn_id);
3573 BT_INFO("Characteristic Read result from addr [%s] status [%d]",
3574 conn_info->addr, event_data->uuid_status.conn_status.status);
3576 /* Fill char in buffer */
3577 memcpy(&read_info.characteristic.uuid,
3578 event_data->uuid_status.char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3579 read_info.characteristic.instance_id = event_data->uuid_status.char_id.inst_id;
3581 /* Fill Service in buffer */
3582 memcpy(&read_info.svc.uuid,
3583 event_data->uuid_status.srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3584 read_info.svc.instance_id = event_data->uuid_status.srvc_id.id.inst_id;
3586 /* Fill remote device address */
3587 _bt_convert_addr_string_to_type(read_info.device_address.addr, conn_info->addr);
3589 /* Fill data and reply to all apps waiting for Read result on the same characteristic
3590 Note: Even in case of failure, address, handles and result code should be returned */
3591 if (event_data->uuid_status.conn_status.status != OAL_STATUS_SUCCESS)
3592 result = BLUETOOTH_ERROR_INTERNAL;
3594 if (event_data->data_len > 0) {
3596 // for (i = 0; i < event_data->data_len; i++)
3597 // BT_INFO("Data[%d] = [0x%x]", i, event_data->data[i]);
3600 read_val = g_memdup(&event_data->data[0], event_data->data_len);
3602 data = g_variant_new_from_data(
3603 G_VARIANT_TYPE_BYTESTRING,
3605 event_data->data_len,
3608 BT_ERR("Characteristic Read success, but no data!!!");
3610 data = g_variant_new_from_data(
3611 G_VARIANT_TYPE_BYTESTRING,
3618 svc_uuid = g_memdup(&event_data->uuid_status.srvc_id.id.uuid.uuid[0], uuid_len);
3620 data_svc_uuid = g_variant_new_from_data(
3621 G_VARIANT_TYPE_BYTESTRING,
3627 char_uuid = g_memdup(&event_data->uuid_status.char_id.uuid.uuid[0], uuid_len);
3629 data_char_uuid = g_variant_new_from_data(
3630 G_VARIANT_TYPE_BYTESTRING,
3635 param = g_variant_new("(isn@ayin@ayin@ay)", result,
3639 event_data->uuid_status.srvc_id.id.inst_id,
3642 event_data->uuid_status.char_id.inst_id,
3643 event_data->data_len,
3647 _bt_send_event(BT_GATT_CLIENT_EVENT,
3648 BLUETOOTH_EVENT_GATT_READ_CHAR,
3651 /* Send DBUS return */
3652 __bt_gatt_handle_pending_request_info(result,
3653 BT_GATT_READ_CHARACTERISTIC,
3655 sizeof(bluetooth_gatt_client_char_prop_info_t));
3666 static void __bt_handle_client_descriptor_read_data(
3667 event_gattc_read_data *event_data)
3669 int result = BLUETOOTH_ERROR_NONE;
3670 struct gatt_server_info_t *conn_info = NULL;
3671 bluetooth_gatt_client_desc_prop_info_t read_info;
3673 /* Read Information data structures */
3674 GVariant *param = NULL;
3675 GVariant *data = NULL;
3676 GVariant *data_svc_uuid = NULL;
3677 GVariant *data_char_uuid = NULL;
3678 GVariant *data_desc_uuid = NULL;
3679 char *read_val = NULL;
3680 char *svc_uuid = NULL;
3681 char *char_uuid = NULL;
3682 char *desc_uuid = NULL;
3687 memset(&read_info, 0x00, sizeof(bluetooth_gatt_client_desc_prop_info_t));
3689 /* Extract Address from conn_id of event data */
3690 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3691 event_data->uuid_status.conn_status.conn_id);
3693 BT_INFO("Descriptor Read result from addr [%s] status [%d]",
3694 conn_info->addr, event_data->uuid_status.conn_status.status);
3696 /* Fill descriptor informations in buffer */
3697 memcpy(&read_info.descriptor.uuid,
3698 event_data->uuid_status.descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3699 read_info.descriptor.instance_id = event_data->uuid_status.descr_id.inst_id;
3701 /* Fill Characteristic informations in buffer */
3702 memcpy(&read_info.characteristic.uuid,
3703 event_data->uuid_status.char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3704 read_info.characteristic.instance_id = event_data->uuid_status.char_id.inst_id;
3706 /* Fill Service informations in buffer */
3707 memcpy(&read_info.svc.uuid,
3708 event_data->uuid_status.srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3709 read_info.svc.instance_id = event_data->uuid_status.srvc_id.id.inst_id;
3711 /* Fill remote device address */
3712 _bt_convert_addr_string_to_type(read_info.device_address.addr, conn_info->addr);
3714 /* Fill data and reply to all apps waiting for Read result on the same characteristic */
3715 if (event_data->uuid_status.conn_status.status != OAL_STATUS_SUCCESS)
3716 result = BLUETOOTH_ERROR_INTERNAL;
3718 BT_INFO("desc data len:%d", event_data->data_len);
3719 if (event_data->data_len > 0) {
3721 for (i = 0; i < event_data->data_len; i++)
3722 BT_DBG("Data[%d] = [0x%x]", i, event_data->data[i]);
3725 read_val = g_memdup(&event_data->data[0], event_data->data_len);
3727 data = g_variant_new_from_data(
3728 G_VARIANT_TYPE_BYTESTRING,
3730 event_data->data_len,
3733 BT_INFO("Descriptor Read success, but no data!!!");
3735 data = g_variant_new_from_data(
3736 G_VARIANT_TYPE_BYTESTRING,
3742 svc_uuid = g_memdup(&event_data->uuid_status.srvc_id.id.uuid.uuid[0], uuid_len);
3744 data_svc_uuid = g_variant_new_from_data(
3745 G_VARIANT_TYPE_BYTESTRING,
3751 char_uuid = g_memdup(&event_data->uuid_status.char_id.uuid.uuid[0], uuid_len);
3753 data_char_uuid = g_variant_new_from_data(
3754 G_VARIANT_TYPE_BYTESTRING,
3760 desc_uuid = g_memdup(&event_data->uuid_status.descr_id.uuid.uuid[0], uuid_len);
3762 data_desc_uuid = g_variant_new_from_data(
3763 G_VARIANT_TYPE_BYTESTRING,
3768 param = g_variant_new("(isn@ayin@ayin@ayin@ay)", result,
3772 event_data->uuid_status.srvc_id.id.inst_id,
3775 event_data->uuid_status.char_id.inst_id,
3778 event_data->uuid_status.descr_id.inst_id,
3779 event_data->data_len,
3783 _bt_send_event(BT_GATT_CLIENT_EVENT,
3784 BLUETOOTH_EVENT_GATT_READ_DESC,
3787 BT_INFO("Send DBUS rpely for GATT Read Descriptor");
3788 /* Send DBUS return */
3789 __bt_gatt_handle_pending_request_info(result,
3790 BT_GATT_READ_DESCRIPTOR_VALUE,
3792 sizeof(bluetooth_gatt_client_desc_prop_info_t));
3803 static void __bt_handle_client_characteristic_write_data(
3804 event_gattc_write_data *event_data)
3806 int result = BLUETOOTH_ERROR_NONE;
3807 struct gatt_server_info_t *conn_info = NULL;
3808 bluetooth_gatt_client_char_prop_info_t write_info;
3810 /* Read Information data structures */
3811 GVariant *param = NULL;
3812 GVariant *data_svc_uuid = NULL;
3813 GVariant *data_char_uuid = NULL;
3814 char *svc_uuid = NULL;
3815 char *char_uuid = NULL;
3819 memset(&write_info, 0x00, sizeof(bluetooth_gatt_client_char_prop_info_t));
3821 /* Extract Address from conn_id of event data */
3822 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3823 event_data->conn_status.conn_id);
3825 BT_INFO("Characteristic Write callback from addr [%s] status [%d]",
3826 conn_info->addr, event_data->conn_status.status);
3828 /* Fill char in buffer */
3829 memcpy(&write_info.characteristic.uuid,
3830 event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3831 write_info.characteristic.instance_id = event_data->char_id.inst_id;
3833 /* Fill Service in buffer */
3834 memcpy(&write_info.svc.uuid,
3835 event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3836 write_info.svc.instance_id = event_data->srvc_id.id.inst_id;
3838 /* Fill remote device address */
3839 _bt_convert_addr_string_to_type(write_info.device_address.addr, conn_info->addr);
3841 if (event_data->conn_status.status != OAL_STATUS_SUCCESS) {
3842 result = BLUETOOTH_ERROR_INTERNAL;
3848 svc_uuid = g_memdup(&event_data->srvc_id.id.uuid.uuid[0], uuid_len);
3850 data_svc_uuid = g_variant_new_from_data(
3851 G_VARIANT_TYPE_BYTESTRING,
3857 char_uuid = g_memdup(&event_data->char_id.uuid.uuid[0], uuid_len);
3859 data_char_uuid = g_variant_new_from_data(
3860 G_VARIANT_TYPE_BYTESTRING,
3865 param = g_variant_new("(isn@ayin@ayi)", result,
3869 event_data->srvc_id.id.inst_id,
3872 event_data->char_id.inst_id);
3875 _bt_send_event(BT_GATT_CLIENT_EVENT,
3876 BLUETOOTH_EVENT_GATT_WRITE_CHAR,
3885 /* Send DBUS return */
3886 __bt_gatt_handle_pending_request_info(result,
3887 BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE,
3889 sizeof(bluetooth_gatt_client_char_prop_info_t));
3893 static void __bt_handle_client_descriptor_write_data(
3894 event_gattc_write_data *event_data)
3896 int result = BLUETOOTH_ERROR_NONE;
3897 struct gatt_server_info_t *conn_info = NULL;
3898 bluetooth_gatt_client_desc_prop_info_t write_info;
3900 /* Write Information data structures */
3901 GVariant *param = NULL;
3902 GVariant *data_svc_uuid = NULL;
3903 GVariant *data_char_uuid = NULL;
3904 GVariant *data_desc_uuid = NULL;
3905 char *svc_uuid = NULL;
3906 char *char_uuid = NULL;
3907 char *desc_uuid = NULL;
3911 memset(&write_info, 0x00, sizeof(bluetooth_gatt_client_desc_prop_info_t));
3913 /* Extract Address from conn_id of event data */
3914 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3915 event_data->conn_status.conn_id);
3917 if (NULL == conn_info) {
3919 BT_INFO("Failed to get the conn info for conn_id [%d]", event_data->conn_status.conn_id);
3923 BT_INFO("Descriptor Write callback from addr [%s] status [%d]",
3924 conn_info->addr, event_data->conn_status.status);
3926 /* Fill descriptor informations in buffer */
3927 memcpy(&write_info.descriptor.uuid,
3928 event_data->descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3929 write_info.descriptor.instance_id = event_data->descr_id.inst_id;
3931 /* Fill Characteristic informations in buffer */
3932 memcpy(&write_info.characteristic.uuid,
3933 event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3934 write_info.characteristic.instance_id = event_data->char_id.inst_id;
3936 /* Fill Service informations in buffer */
3937 memcpy(&write_info.svc.uuid,
3938 event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3939 write_info.svc.instance_id = event_data->srvc_id.id.inst_id;
3941 /* Fill remote device address */
3942 _bt_convert_addr_string_to_type(write_info.device_address.addr, conn_info->addr);
3944 if (event_data->conn_status.status != OAL_STATUS_SUCCESS) {
3945 result = BLUETOOTH_ERROR_INTERNAL;
3951 svc_uuid = g_memdup(&event_data->srvc_id.id.uuid.uuid[0], uuid_len);
3953 data_svc_uuid = g_variant_new_from_data(
3954 G_VARIANT_TYPE_BYTESTRING,
3960 char_uuid = g_memdup(&event_data->char_id.uuid.uuid[0], uuid_len);
3962 data_char_uuid = g_variant_new_from_data(
3963 G_VARIANT_TYPE_BYTESTRING,
3969 desc_uuid = g_memdup(&event_data->descr_id.uuid.uuid[0], uuid_len);
3971 data_desc_uuid = g_variant_new_from_data(
3972 G_VARIANT_TYPE_BYTESTRING,
3977 param = g_variant_new("(isn@ayin@ayin@ayi)", result,
3981 event_data->srvc_id.id.inst_id,
3984 event_data->char_id.inst_id,
3987 event_data->descr_id.inst_id);
3990 _bt_send_event(BT_GATT_CLIENT_EVENT,
3991 BLUETOOTH_EVENT_GATT_WRITE_DESC,
4002 /* Send DBUS return */
4003 __bt_gatt_handle_pending_request_info(result,
4004 BT_GATT_WRITE_DESCRIPTOR_VALUE,
4006 sizeof(bluetooth_gatt_client_desc_prop_info_t));
4009 static void __bt_hanlde_le_device_disconnection(event_dev_conn_status_t *event_data)
4011 int result = BLUETOOTH_ERROR_INTERNAL;
4012 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
4014 _bt_convert_addr_type_to_string(address, (unsigned char *)event_data->address.addr);
4016 /* DBUS Return with fail of pending BT_CONNECT_LE for all the apps */
4017 BT_INFO("Local GATT Client disconnected: Remote addr[%s] ", address);
4019 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE, address,
4020 BT_ADDRESS_STRING_SIZE);
4024 static void __bt_handle_client_notification_registered(
4025 event_gattc_regdereg_notify_t *event_data,
4026 gboolean is_registered)
4028 int result = BLUETOOTH_ERROR_NONE;
4029 struct gatt_server_info_t *conn_info = NULL;
4030 bt_gatt_notif_reg_info_t notif_info;
4033 memset(¬if_info, 0x00, sizeof(bt_gatt_notif_reg_info_t));
4035 BT_INFO("Client Interface [%d] status [%d]",
4036 event_data->conn_id,
4037 event_data->status);
4039 /* Extract Address from conn_id of event data */
4040 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(event_data->conn_id);
4042 BT_INFO("Connection Info is not present, return");
4045 BT_INFO("Notification Registered for addr [%s]", conn_info->addr);
4047 /* Fill svc informations in buffer */
4048 memcpy(¬if_info.svc_uuid,
4049 event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4050 notif_info.svc_inst = event_data->srvc_id.id.inst_id;
4052 /* Fill char in buffer */
4053 memcpy(¬if_info.char_uuid,
4054 event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4055 notif_info.char_inst = event_data->char_id.inst_id;
4057 /* Fill remote device address */
4058 _bt_convert_addr_string_to_type(notif_info.addr.addr, conn_info->addr);
4060 notif_info.is_registered = is_registered;
4062 if (event_data->status != OAL_STATUS_SUCCESS)
4063 result = BLUETOOTH_ERROR_INTERNAL;
4065 /* Send DBUS Return for BT_GATT_WATCH_CHARACTERISTIC */
4066 __bt_gatt_handle_pending_request_info(result,
4067 BT_GATT_WATCH_CHARACTERISTIC,
4069 sizeof(bt_gatt_notif_reg_info_t));
4072 static void __bt_handle_client_notification_data(event_gattc_notify_data *event_data)
4074 /* No status in this event from OAL */
4075 int result = BLUETOOTH_ERROR_NONE;
4077 /* Read Information data structures */
4078 GVariant *param = NULL;
4079 GVariant *data = NULL;
4080 GVariant *data_svc_uuid = NULL;
4081 GVariant *data_char_uuid = NULL;
4082 char *read_val = NULL;
4083 char *svc_uuid = NULL;
4084 char *char_uuid = NULL;
4090 BT_INFO("Notifcation of charc data changed");
4092 if (event_data->data_len > 0) {
4094 for (i = 0; i < event_data->data_len; i++)
4095 BT_INFO("Data[%d] = [0x%x]", i, event_data->data[i]);
4098 addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
4099 _bt_convert_addr_type_to_string(addr,
4100 (unsigned char *)&(event_data->address.addr));
4103 read_val = g_memdup(&event_data->data[0], event_data->data_len);
4105 data = g_variant_new_from_data(
4106 G_VARIANT_TYPE_BYTESTRING,
4108 event_data->data_len,
4111 svc_uuid = g_memdup(&event_data->srvc_id.id.uuid.uuid[0], uuid_len);
4113 data_svc_uuid = g_variant_new_from_data(
4114 G_VARIANT_TYPE_BYTESTRING,
4120 char_uuid = g_memdup(&event_data->char_id.uuid.uuid[0], uuid_len);
4122 data_char_uuid = g_variant_new_from_data(
4123 G_VARIANT_TYPE_BYTESTRING,
4129 param = g_variant_new("(isn@ayin@ayin@ay)", result,
4133 event_data->srvc_id.id.inst_id,
4136 event_data->char_id.inst_id,
4137 event_data->data_len,
4141 _bt_send_event(BT_GATT_CLIENT_EVENT,
4142 BLUETOOTH_EVENT_GATT_CHAR_VAL_CHANGED,
4145 BT_ERR("No Data!!");
4158 static void __bt_handle_client_service_changed_ind(event_gattc_service_changed_data *event_data)
4160 bt_gatt_service_info_list_t *svc_info_list;
4162 bt_gatt_service_info_t *svc_info;
4163 GVariant *param = NULL;
4164 char *address_str = NULL;
4165 char *uuid_str = NULL;
4167 svc_info_list = __bt_get_service_info_list(event_data->conn_id);
4168 if (svc_info_list == NULL) {
4169 BT_ERR("svc_info_list is NULL");
4173 if (event_data->change_type) {
4174 /* Add service UUID in list */
4175 svc_info = g_malloc0(sizeof(bt_gatt_service_info_t));
4176 memcpy(svc_info->uuid, event_data->uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4177 svc_info->inst_id = event_data->inst_id;
4178 svc_info->is_primary = 1; // TODO: Need to check is_primary is required or not
4179 svc_info_list->services = g_slist_append(svc_info_list->services, svc_info);
4180 BT_DBG("Service created and added in Svc info list or connection ID %d", event_data->conn_id);
4182 /* Remove service UUID in list */
4183 for (l = svc_info_list->services; l != NULL; l = g_slist_next(l)) {
4184 svc_info = (bt_gatt_service_info_t *)l->data;
4185 if (svc_info == NULL)
4188 if (!memcmp(svc_info->uuid, event_data->uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)) {
4189 svc_info_list->services = g_slist_remove(svc_info_list->services, svc_info);
4190 __bt_free_service_info(svc_info);
4196 address_str = g_malloc0(BT_ADDRESS_STRING_SIZE);
4197 uuid_str = g_malloc0(BT_UUID_STRING_MAX);
4198 _bt_convert_addr_type_to_string(address_str, event_data->address.addr);
4199 _bt_uuid_to_string(&event_data->uuid, uuid_str);
4201 param = g_variant_new("(iiss)", event_data->inst_id, event_data->change_type, address_str, uuid_str);
4203 _bt_send_event(BT_GATT_CLIENT_EVENT,
4204 BLUETOOTH_EVENT_GATT_CLIENT_SERVICE_CHANGED,
4206 g_free(address_str);
4210 gboolean _bt_is_remote_gatt_device_connected(bluetooth_device_address_t *address)
4213 struct gatt_server_info_t *conn_info = NULL;
4214 gboolean connected = FALSE;
4216 addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
4217 _bt_convert_addr_type_to_string(addr,
4218 (unsigned char *)&(address->addr));
4220 BT_DBG("Check GATT connection status of [%s]", addr);
4221 /* Check if device is already in connected list */
4222 conn_info = _bt_find_remote_gatt_server_info(addr);
4225 BT_DBG("Remote GATT Server device [%s] is Connected", conn_info->addr);
4228 struct gatt_client_info_t *client_info = NULL;
4230 BT_INFO("Remote GATT Server Device [%s] is not Connected", addr);
4232 /* Check if device is already in connected list */
4233 client_info = _bt_find_remote_gatt_client_info(addr);
4236 BT_DBG("Remote Client device [%s] is Connected", client_info->addr);
4239 BT_INFO("Remote GATT Client Device [%s] is not Connected", addr);
4247 void _bt_handle_invocation_context(int function_name, void *data)
4249 switch (function_name) {
4251 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE, BT_CONNECT_LE,
4252 (char *)data, BT_ADDRESS_STRING_SIZE);
4259 int _bt_connect_le_device(bluetooth_device_address_t *address,
4260 int auto_connect, int client_id)
4262 struct gatt_server_info_t *conn_info = NULL;
4263 struct gatt_out_conn_info_t *out_conn_info = NULL;
4265 invocation_info_t *req_info = NULL;
4266 int ret = OAL_STATUS_SUCCESS;
4268 char *remote_address = NULL;
4270 BT_CHECK_PARAMETER(address, return);
4272 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4273 _bt_convert_addr_type_to_string(addr, address->addr);
4274 BT_DBG("GATT Client connect request for address [%s] client instance [%d]",
4278 /* Check if Remote Device is already under connection progress */
4279 req_info = _bt_get_request_info_data_from_function_name(BT_CONNECT_LE);
4281 remote_address = (char*)req_info->user_data;
4282 if (remote_address && !strcasecmp(remote_address, addr)) {/* Address matched */
4283 BT_DBG("Already Connection ongoing for same remote GATT Server address [%s]", remote_address);
4284 /* Return and wait for events to be sent to all apps */
4286 return BLUETOOTH_ERROR_IN_PROGRESS;
4290 /* Check if remote GATT Server is connected or not */
4291 conn_info = _bt_find_remote_gatt_server_info(addr);
4293 BT_ERR("GATT Server is already connected..");
4295 return BLUETOOTH_ERROR_ALREADY_CONNECT;
4298 /* TODO Check Requirement of holding Advertisement before initiating LE connect */
4300 /* Check if app sent 0 client id for connection, in such case, use default gatt client ID */
4301 if (client_id == 0) {
4302 /* GATT CLient connect request sent by an app without any client instance [0] */
4303 BT_DBG("Assign default GATT client id [%d]", gatt_default_client);
4304 client_id = gatt_default_client;
4307 BT_INFO("Connect using CLient ID [%d]", client_id);
4308 ret = gattc_connect(client_id, (bt_address_t*)(address), auto_connect);
4310 if (ret != OAL_STATUS_SUCCESS) {
4311 BT_ERR("gattc_connect is failed. ret: %d", ret);
4313 return BLUETOOTH_ERROR_INTERNAL;
4316 /* Mark this as outgoing connection */
4317 out_conn_info = g_new0(struct gatt_out_conn_info_t, 1);
4318 out_conn_info->addr = g_strdup(addr);
4319 out_conn_info->client_id = client_id;
4320 BT_INFO("Added outgoing connection info addr[%s]", out_conn_info->addr);
4321 outgoing_gatt_conn_list = g_slist_append(outgoing_gatt_conn_list, out_conn_info);
4324 return BLUETOOTH_ERROR_NONE;
4327 int _bt_gatt_get_primary_services(char *address)
4329 BT_CHECK_PARAMETER(address, return);
4330 struct gatt_server_info_t *conn_info = NULL;
4331 invocation_info_t *req_info = NULL;
4332 int ret = OAL_STATUS_SUCCESS;
4335 /* Check if any app is already browsing primary services on the same remote GATT Server */
4336 req_info = _bt_get_request_info_data(BT_GATT_GET_PRIMARY_SERVICES, address);
4338 BT_INFO("Already Primary Service Browsing ongoing for same rmeote GATT Server");
4339 /* Return and wait for events to be sent to all apps */
4340 return BLUETOOTH_ERROR_NONE;
4343 /* Check if remote GATT Server is connected or not */
4344 conn_info = _bt_find_remote_gatt_server_info(address);
4346 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4347 conn_info->addr, conn_info->connection_id);
4349 BT_ERR("GATT Server is not yet connected..");
4350 return BLUETOOTH_ERROR_NOT_CONNECTED;
4353 /* Send Primary Service Browsing request to stack */
4354 ret = gattc_search_service(conn_info->connection_id, NULL);
4355 if (ret != OAL_STATUS_SUCCESS) {
4356 BT_ERR("ret: %d", ret);
4357 return BLUETOOTH_ERROR_INTERNAL;
4359 return BLUETOOTH_ERROR_NONE;
4362 int _bt_gatt_get_all_characteristic(bluetooth_gatt_client_svc_prop_info_t *svc)
4364 BT_CHECK_PARAMETER(svc, return);
4365 struct gatt_server_info_t *conn_info = NULL;
4366 invocation_info_t *req_info = NULL;
4367 bluetooth_gatt_client_svc_prop_info_t *prop;
4368 oal_gatt_srvc_id_t srvc_id;
4369 int ret = OAL_STATUS_SUCCESS;
4372 /* Check if any app is already browsing characteristics of the same service on the same remote GATT Server */
4373 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_GET_SERVICE_PROPERTIES);
4375 prop = (bluetooth_gatt_client_svc_prop_info_t*)req_info->user_data;
4376 if (prop && !memcmp(svc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t))
4377 && !memcmp(prop->svc.uuid, svc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
4378 && prop->svc.instance_id == svc->svc.instance_id) {
4379 BT_INFO("Already Properties browsing for Primary Service ongoing for same remote GATT Server");
4380 /* Return and wait for events to be sent to all apps */
4381 return BLUETOOTH_ERROR_NONE;
4385 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4386 _bt_convert_addr_type_to_string(addr, svc->device_address.addr);
4388 /* Check if remote GATT Server is connected or not */
4389 conn_info = _bt_find_remote_gatt_server_info(addr);
4391 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4392 conn_info->addr, conn_info->connection_id);
4394 BT_ERR("GATT Server is not yet connected..");
4396 return BLUETOOTH_ERROR_NOT_CONNECTED;
4399 srvc_id.is_prmry = TRUE;
4400 srvc_id.id.inst_id = svc->svc.instance_id;
4401 memcpy(srvc_id.id.uuid.uuid, svc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4403 /* Search All Characteristic */
4404 ret = gattc_get_characteristic(conn_info->connection_id, &srvc_id, NULL);
4405 if (ret != OAL_STATUS_SUCCESS) {
4406 BT_ERR("ret: %d", ret);
4408 return BLUETOOTH_ERROR_INTERNAL;
4411 return BLUETOOTH_ERROR_NONE;
4414 int _bt_gatt_get_all_characteristic_properties(
4415 bluetooth_gatt_client_char_prop_info_t *chr)
4417 struct gatt_server_info_t *conn_info = NULL;
4418 invocation_info_t *req_info = NULL;
4419 bluetooth_gatt_client_char_prop_info_t *prop;
4420 oal_gatt_srvc_id_t srvc_id;
4421 oal_gatt_id_t char_id;
4422 int ret = OAL_STATUS_SUCCESS;
4425 BT_CHECK_PARAMETER(chr, return);
4427 /* Check if any app is already browsing descriptors of the same char of
4428 particular service on the same remote GATT Server */
4429 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_GET_CHARACTERISTIC_PROPERTIES);
4431 prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
4432 if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4433 && !memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4434 && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4435 && !memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4436 && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
4437 BT_INFO("Already Properties browsing for Characteristic ongoing for same remote GATT Server");
4438 /* Return and wait for events to be sent to all apps */
4439 return BLUETOOTH_ERROR_NONE;
4443 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4444 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4446 /* Check if remote GATT Server is connected or not */
4447 conn_info = _bt_find_remote_gatt_server_info(addr);
4449 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4450 conn_info->addr, conn_info->connection_id);
4452 BT_ERR("GATT Server is not yet connected..");
4454 return BLUETOOTH_ERROR_NOT_CONNECTED;
4457 srvc_id.is_prmry = TRUE;
4458 srvc_id.id.inst_id = chr->svc.instance_id;
4459 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4461 char_id.inst_id = chr->characteristic.instance_id;
4462 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4464 /* Search All Descriptors */
4465 ret = gattc_get_descriptor(conn_info->connection_id, &srvc_id, &char_id, NULL);
4466 if (ret != OAL_STATUS_SUCCESS) {
4467 BT_ERR("ret: %d", ret);
4469 return BLUETOOTH_ERROR_INTERNAL;
4472 return BLUETOOTH_ERROR_NONE;
4475 int _bt_gatt_read_characteristic_value(
4476 bluetooth_gatt_client_char_prop_info_t *chr)
4478 struct gatt_server_info_t *conn_info = NULL;
4479 invocation_info_t *req_info = NULL;
4480 bluetooth_gatt_client_char_prop_info_t *prop;
4481 oal_gatt_srvc_id_t srvc_id;
4482 oal_gatt_id_t char_id;
4483 int ret = OAL_STATUS_SUCCESS;
4486 BT_CHECK_PARAMETER(chr, return);
4488 /* Check if any app is already Reading characteristic of the same char of
4489 particular service on the same remote GATT Server */
4490 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_READ_CHARACTERISTIC);
4492 prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
4493 if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4494 && memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4495 && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4496 && memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4497 && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
4498 BT_INFO("Already Characteristic value Read operation in progress for same remote GATT Server");
4499 /* Return and wait for events to be sent to all apps */
4500 return BLUETOOTH_ERROR_NONE;
4504 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4505 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4507 /* Check if remote GATT Server is connected or not */
4508 conn_info = _bt_find_remote_gatt_server_info(addr);
4510 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4511 conn_info->addr, conn_info->connection_id);
4513 BT_ERR("GATT Server is not yet connected..");
4515 return BLUETOOTH_ERROR_NOT_CONNECTED;
4518 srvc_id.is_prmry = TRUE;
4519 srvc_id.id.inst_id = chr->svc.instance_id;
4520 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4522 char_id.inst_id = chr->characteristic.instance_id;
4523 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4525 /* Search All Descriptors */
4526 ret = gattc_read_characteristic(conn_info->connection_id, &srvc_id, &char_id, OAL_GATT_AUTH_REQ_NONE);
4527 if (ret != OAL_STATUS_SUCCESS) {
4528 BT_ERR("ret: %d", ret);
4530 return BLUETOOTH_ERROR_INTERNAL;
4533 return BLUETOOTH_ERROR_NONE;
4536 int _bt_gatt_read_descriptor_value(
4537 bluetooth_gatt_client_desc_prop_info_t *desc)
4539 struct gatt_server_info_t *conn_info = NULL;
4540 invocation_info_t *req_info = NULL;
4541 bluetooth_gatt_client_desc_prop_info_t *prop;
4542 oal_gatt_srvc_id_t srvc_id;
4543 oal_gatt_id_t char_id;
4544 oal_gatt_id_t desc_id;
4545 int ret = OAL_STATUS_SUCCESS;
4548 BT_CHECK_PARAMETER(desc, return);
4550 /* Check if any app is already Reading descriptors of the same char of
4551 particular service on the same remote GATT Server */
4552 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_READ_DESCRIPTOR_VALUE);
4554 prop = (bluetooth_gatt_client_desc_prop_info_t*)req_info->user_data;
4555 if (prop && !memcmp(desc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4556 && memcmp(desc->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4557 && desc->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4558 && memcmp(desc->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4559 && desc->characteristic.instance_id == prop->characteristic.instance_id /* Characteristic Instance ID matched */
4560 && memcmp(desc->descriptor.uuid, prop->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Descriptor UUID matched */
4561 && desc->descriptor.instance_id == prop->descriptor.instance_id) { /* Descriptor Instance ID matched */
4562 BT_INFO("Already Descriptor value Read operation in progress for same remote GATT Server");
4563 /* Return and wait for events to be sent to all apps */
4564 return BLUETOOTH_ERROR_NONE;
4568 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4569 _bt_convert_addr_type_to_string(addr, desc->device_address.addr);
4571 /* Check if remote GATT Server is connected or not */
4572 conn_info = _bt_find_remote_gatt_server_info(addr);
4574 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4575 conn_info->addr, conn_info->connection_id);
4577 BT_ERR("GATT Server is not yet connected..");
4579 return BLUETOOTH_ERROR_NOT_CONNECTED;
4582 srvc_id.is_prmry = TRUE;
4583 srvc_id.id.inst_id = desc->svc.instance_id;
4584 memcpy(srvc_id.id.uuid.uuid, desc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4586 char_id.inst_id = desc->characteristic.instance_id;
4587 memcpy(char_id.uuid.uuid, desc->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4589 desc_id.inst_id = desc->descriptor.instance_id;
4590 memcpy(desc_id.uuid.uuid, desc->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4592 /* Search All Descriptors */
4593 ret = gattc_read_descriptor(conn_info->connection_id, &srvc_id, &char_id,
4594 &desc_id, OAL_GATT_AUTH_REQ_NONE);
4595 if (ret != OAL_STATUS_SUCCESS) {
4596 BT_ERR("ret: %d", ret);
4598 return BLUETOOTH_ERROR_INTERNAL;
4601 return BLUETOOTH_ERROR_NONE;
4605 int _bt_gatt_acquire_notify(bluetooth_gatt_client_char_prop_info_t *chr, int *fd, int *mtu)
4607 struct gatt_server_info_t *conn_info = NULL;
4608 oal_gatt_srvc_id_t srvc_id;
4609 oal_gatt_id_t char_id;
4610 int ret = OAL_STATUS_SUCCESS;
4613 BT_CHECK_PARAMETER(chr, return);
4615 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4616 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4618 /* Check if remote GATT Server is connected or not */
4619 conn_info = _bt_find_remote_gatt_server_info(addr);
4621 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4622 conn_info->addr, conn_info->connection_id);
4624 BT_ERR("GATT Server is not yet connected..");
4626 return BLUETOOTH_ERROR_NOT_CONNECTED;
4629 srvc_id.is_prmry = TRUE;
4630 srvc_id.id.inst_id = chr->svc.instance_id;
4631 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4633 char_id.inst_id = chr->characteristic.instance_id;
4634 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4636 ret = gattc_acquire_notify(conn_info->connection_id, &srvc_id, &char_id, fd, mtu);
4638 if (ret != OAL_STATUS_SUCCESS) {
4639 BT_ERR("ret: %d", ret);
4641 return BLUETOOTH_ERROR_INTERNAL;
4643 BT_INFO("GATT characterstics FD [%d] mtu[%d]", *fd, *mtu);
4645 return BLUETOOTH_ERROR_NONE;
4649 int _bt_gatt_acquire_write(bluetooth_gatt_client_char_prop_info_t *chr, int *fd, int *mtu)
4652 struct gatt_server_info_t *conn_info = NULL;
4653 oal_gatt_srvc_id_t srvc_id;
4654 oal_gatt_id_t char_id;
4655 int ret = OAL_STATUS_SUCCESS;
4658 BT_CHECK_PARAMETER(chr, return);
4660 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4661 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4663 /* Check if remote GATT Server is connected or not */
4664 conn_info = _bt_find_remote_gatt_server_info(addr);
4666 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4667 conn_info->addr, conn_info->connection_id);
4669 BT_ERR("GATT Server is not yet connected..");
4671 return BLUETOOTH_ERROR_NOT_CONNECTED;
4674 srvc_id.is_prmry = TRUE;
4675 srvc_id.id.inst_id = chr->svc.instance_id;
4676 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4678 char_id.inst_id = chr->characteristic.instance_id;
4679 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4681 ret = gattc_acquire_write(conn_info->connection_id, &srvc_id, &char_id,
4682 OAL_GATT_AUTH_REQ_NONE, fd, mtu);
4683 if (ret != OAL_STATUS_SUCCESS) {
4684 BT_ERR("ret: %d", ret);
4686 return BLUETOOTH_ERROR_INTERNAL;
4688 BT_INFO("GATT characterstics FD [%d] mtu [%d]", *fd, *mtu);
4690 return BLUETOOTH_ERROR_NONE;
4695 /* Write Characteristic */
4696 int _bt_gatt_write_characteristic_value_by_type(
4697 bluetooth_gatt_client_char_prop_info_t *chr,
4698 bluetooth_gatt_att_data_t *data,
4699 bluetooth_gatt_write_type_e write_type)
4701 struct gatt_server_info_t *conn_info = NULL;
4702 invocation_info_t *req_info = NULL;
4703 bluetooth_gatt_client_char_prop_info_t *prop;
4704 oal_gatt_srvc_id_t srvc_id;
4705 oal_gatt_id_t char_id;
4706 int ret = OAL_STATUS_SUCCESS;
4710 BT_CHECK_PARAMETER(chr, return);
4711 BT_CHECK_PARAMETER(data, return);
4715 /* Check if any app is already writing same char of
4716 particular service on the same remote GATT Server */
4717 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE);
4719 prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
4720 if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4721 && memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4722 && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4723 && memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4724 && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
4725 BT_INFO("Already Characteristic Write Value operation in progress for same remote GATT Server");
4726 /* Return and wait for events to be sent to all apps */
4727 return BLUETOOTH_ERROR_NONE;
4731 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4732 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4734 /* Check if remote GATT Server is connected or not */
4735 conn_info = _bt_find_remote_gatt_server_info(addr);
4737 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4738 conn_info->addr, conn_info->connection_id);
4740 BT_ERR("GATT Server is not yet connected..");
4742 return BLUETOOTH_ERROR_NOT_CONNECTED;
4745 srvc_id.is_prmry = TRUE;
4746 srvc_id.id.inst_id = chr->svc.instance_id;
4747 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4749 char_id.inst_id = chr->characteristic.instance_id;
4750 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4752 /* Write CHar value */
4754 BT_INFO("Connection ID [%d] write type [%d] data length [%d]", conn_info->connection_id, write_type, data->length);
4755 for (k = 0; k < data->length; k++)
4756 BT_INFO("Data[%d] [0x%x]", k, data->data[k]);
4758 ret = gattc_write_characteristic(conn_info->connection_id,
4760 (oal_gatt_write_type_t)write_type, data->length,
4761 OAL_GATT_AUTH_REQ_NONE, (char *)(&data->data[0]));
4762 if (ret != OAL_STATUS_SUCCESS) {
4763 BT_ERR("ret: %d", ret);
4765 return BLUETOOTH_ERROR_INTERNAL;
4768 return BLUETOOTH_ERROR_NONE;
4771 /* Write Descriptor */
4772 int _bt_gatt_write_descriptor_value_by_type(
4773 bluetooth_gatt_client_desc_prop_info_t *desc,
4774 bluetooth_gatt_att_data_t *data,
4775 bluetooth_gatt_write_type_e write_type)
4777 struct gatt_server_info_t *conn_info = NULL;
4778 invocation_info_t *req_info = NULL;
4779 bluetooth_gatt_client_desc_prop_info_t *prop;
4780 oal_gatt_srvc_id_t srvc_id;
4781 oal_gatt_id_t char_id;
4782 oal_gatt_id_t desc_id;
4783 int ret = OAL_STATUS_SUCCESS;
4787 BT_CHECK_PARAMETER(desc, return);
4788 BT_CHECK_PARAMETER(data, return);
4792 /* Check if any app is already writing on same Descriptor of the same char of
4793 particular service on the same remote GATT Server */
4794 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_WRITE_DESCRIPTOR_VALUE);
4796 prop = (bluetooth_gatt_client_desc_prop_info_t*)req_info->user_data;
4797 if (prop && !memcmp(desc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4798 && memcmp(desc->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4799 && desc->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4800 && memcmp(desc->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4801 && desc->characteristic.instance_id == prop->characteristic.instance_id /* Characteristic Instance ID matched */
4802 && memcmp(desc->descriptor.uuid, prop->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Descriptor UUID matched */
4803 && desc->descriptor.instance_id == prop->descriptor.instance_id) { /* Descriptor Instance ID matched */
4804 BT_INFO("Already Descriptor value Write operation in progress for same remote GATT Server");
4805 /* Return and wait for events to be sent to all apps */
4806 return BLUETOOTH_ERROR_NONE;
4810 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4811 _bt_convert_addr_type_to_string(addr, desc->device_address.addr);
4813 /* Check if remote GATT Server is connected or not */
4814 conn_info = _bt_find_remote_gatt_server_info(addr);
4816 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4817 conn_info->addr, conn_info->connection_id);
4819 BT_ERR("GATT Server is not yet connected..");
4821 return BLUETOOTH_ERROR_NOT_CONNECTED;
4824 srvc_id.is_prmry = TRUE;
4825 srvc_id.id.inst_id = desc->svc.instance_id;
4826 memcpy(srvc_id.id.uuid.uuid, desc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4828 char_id.inst_id = desc->characteristic.instance_id;
4829 memcpy(char_id.uuid.uuid, desc->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4831 desc_id.inst_id = desc->descriptor.instance_id;
4832 memcpy(desc_id.uuid.uuid, desc->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4835 BT_INFO("Connection ID [%d] write type [%d] data length [%d]", conn_info->connection_id, write_type, data->length);
4836 for (k = 0; k < data->length; k++)
4837 BT_INFO("Data[%d] [0x%x]", k, data->data[k]);
4839 ret = gattc_write_descriptor(conn_info->connection_id,
4840 &srvc_id, &char_id, &desc_id,
4841 (oal_gatt_write_type_t)write_type, data->length,
4842 OAL_GATT_AUTH_REQ_NONE, (char *)(&data->data[0]));
4843 if (ret != OAL_STATUS_SUCCESS) {
4844 BT_ERR("ret: %d", ret);
4846 return BLUETOOTH_ERROR_INTERNAL;
4849 return BLUETOOTH_ERROR_NONE;
4852 int _bt_gatt_watch_characteristic(
4853 bluetooth_gatt_client_char_prop_info_t *chr,
4857 struct gatt_server_info_t *conn_info = NULL;
4858 oal_gatt_srvc_id_t srvc_id;
4859 oal_gatt_id_t char_id;
4860 int ret = OAL_STATUS_SUCCESS;
4863 BT_CHECK_PARAMETER(chr, return);
4865 BT_INFO("Client ID [%d] Is Notify [%d]", client_id, is_notify);
4867 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4868 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4870 /* Check if remote GATT Server is connected or not */
4871 conn_info = _bt_find_remote_gatt_server_info(addr);
4873 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4874 conn_info->addr, conn_info->connection_id);
4876 BT_ERR("GATT Server is not yet connected..");
4878 return BLUETOOTH_ERROR_NOT_CONNECTED;
4880 srvc_id.is_prmry = TRUE;
4881 srvc_id.id.inst_id = chr->svc.instance_id;
4882 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4884 char_id.inst_id = chr->characteristic.instance_id;
4885 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4887 /* Register or unregister Notification characteristic */
4889 ret = gattc_register_for_notification(conn_info->connection_id,
4890 (bt_address_t*)&(chr->device_address),
4891 &srvc_id, &char_id);
4893 ret = gattc_deregister_for_notification(conn_info->connection_id,
4894 (bt_address_t*)&(chr->device_address),
4895 &srvc_id, &char_id);
4897 BT_INFO("Result[%d]", ret);
4898 if (ret != OAL_STATUS_SUCCESS) {
4899 BT_ERR("ret: %d", ret);
4901 return BLUETOOTH_ERROR_INTERNAL;
4904 return BLUETOOTH_ERROR_NONE;
4908 int _bt_disconnect_le_device(bluetooth_device_address_t *address,
4911 struct gatt_server_info_t *conn_info = NULL;
4912 struct gatt_client_info_t *rem_client_conn_info = NULL;
4913 invocation_info_t *req_info = NULL;
4914 int ret = OAL_STATUS_SUCCESS;
4916 char *remote_address = NULL;
4918 BT_CHECK_PARAMETER(address, return);
4920 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4921 _bt_convert_addr_type_to_string(addr, address->addr);
4922 BT_INFO("GATT Client Disconnect request for address [%s]", addr);
4924 /* Check if Remote Device is already under connection progress */
4925 req_info = _bt_get_request_info_data_from_function_name(BT_DISCONNECT_LE);
4927 remote_address = (char*)req_info->user_data;
4928 if (remote_address && !strcasecmp(remote_address, addr)) {/* Address matched */
4929 BT_DBG("Already DisConnection ongoing for same remote GATT Server address [%s]", remote_address);
4930 /* Return success and wait for events to be sent to all apps */
4932 return BLUETOOTH_ERROR_IN_PROGRESS;
4935 /* Check if remote GATT Server is connected or not */
4936 conn_info = _bt_find_remote_gatt_server_info(addr);
4938 /* Check if app sent 0 client id for Disconnection, in such case, use default gatt client ID */
4939 if (client_id == 0) {
4940 BT_INFO("GATT CLient Disconnect request sent by an app without any client instance [%d]",
4942 BT_INFO("Assign default GATT client id [%d]", gatt_default_client);
4943 client_id = gatt_default_client;
4946 BT_INFO("Disconnect remote gatt server using CLient ID [%d] Connection ID [%d]", client_id, conn_info->connection_id);
4947 ret = gattc_disconnect(client_id, (bt_address_t*)(address),
4948 conn_info->connection_id);
4950 /* check if remote client is connected */
4951 rem_client_conn_info = _bt_find_remote_gatt_client_info(addr);
4953 if (!rem_client_conn_info || client_id != 0) {
4954 BT_ERR("GATT device is not connected..");
4956 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
4959 BT_INFO("Disconnect remote gatt client ");
4961 ret = gatts_disconnect(rem_client_conn_info->instance_id,
4962 (bt_address_t*)(address), rem_client_conn_info->connection_id);
4965 if (ret != OAL_STATUS_SUCCESS) {
4966 BT_ERR("ret: %d", ret);
4968 return BLUETOOTH_ERROR_INTERNAL;
4971 return BLUETOOTH_ERROR_NONE;
4974 int _bt_gatt_watch_service_changed_indication(const char *sender,
4975 bluetooth_device_address_t *address,
4976 gboolean is_enabled)
4979 bt_service_app_info_t *info = NULL;
4981 BT_INFO("%s Servic changed Indication watcher for app [%s]",
4982 is_enabled ? "Enable":"Disable", sender);
4984 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
4987 if (g_strcmp0(sender, info->sender) == 0 &&
4988 memcmp(info->address.addr, address->addr,
4989 sizeof(bluetooth_device_address_t)) == 0) {
4990 BT_DBG("Found GATT client App.. [%s], sender [%s]", info->uuid, info->sender);
4991 info->is_watcher_enabled = is_enabled;
4995 return BLUETOOTH_ERROR_NONE;
4998 int _bt_unregister_gatt_client_instance(const char *sender, int client_id)
5000 bt_service_app_info_t *info = NULL;
5003 BT_DBG("Unregister Allocated GATT Client instance [%s] Client ID [%d]", sender, client_id);
5005 /* Unregister CLient instance associated with address X. It is possible that another app still
5006 has client_id valid for same remote address */
5007 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
5010 /* Exact matching of sender */
5011 if (!g_strcmp0(info->sender, sender) && info->client_id == client_id) { /* Check for only valid GATT client Instance */
5012 numapps[k].client_id = -1;
5013 numapps[k].is_initialized = FALSE;
5014 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
5015 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
5016 memset(&numapps[k].address.addr, 0x00, sizeof(bluetooth_device_address_t));
5018 /* Its a GATT Client Instance */
5019 ret = gattc_deregister(client_id);
5020 if (ret != OAL_STATUS_SUCCESS) {
5021 BT_ERR("DeAllocate GATT Client instance with stack Fail ret: %d", ret);
5022 return BLUETOOTH_ERROR_INTERNAL;
5024 return BLUETOOTH_ERROR_NONE;
5029 return BLUETOOTH_ERROR_NOT_FOUND;
5032 static void __bt_handle_client_mtu_exchange_completed(event_gattc_mtu_configured_t *event_data)
5034 int result = BLUETOOTH_ERROR_NONE;
5035 struct gatt_server_info_t *conn_info = NULL;
5036 GVariant *param = NULL;
5040 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(event_data->conn_id);
5041 if (conn_info == NULL) {
5042 BT_ERR("Cant find connection Information");
5046 BT_INFO("GATT Client: MTU Configured from addr [%s] status [%d] MTU size [%d]",
5047 conn_info->addr, event_data->status, event_data->mtu);
5049 if (event_data->status != OAL_STATUS_SUCCESS)
5050 result = BLUETOOTH_ERROR_INTERNAL;
5052 BT_INFO("reply BT_REQ_ATT_MTU ");
5054 /* DBUS Return fo BT_REQ_ATT_MTU for all the apps */
5055 __bt_gatt_handle_pending_request_info(result, BT_REQ_ATT_MTU, conn_info->addr,
5056 BT_ADDRESS_STRING_SIZE);
5058 if (result == BLUETOOTH_ERROR_NONE) {
5059 BT_INFO("sending mtu event");
5061 mtu = event_data->mtu;
5062 param = g_variant_new("(isqy)",
5068 /* Send event to BT-API */
5069 _bt_send_event(BT_DEVICE_EVENT,
5070 BLUETOOTH_EVENT_GATT_ATT_MTU_CHANGED,
5073 /* Update the MTU for current connection */
5074 BT_INFO("Updating the MTU for current connection");
5075 __bt_update_mtu_gatt_device(conn_info->addr, event_data->mtu);
5079 static struct gatt_mtu_info_t *__bt_find_mtu_gatt_device(char *address)
5082 struct gatt_mtu_info_t *info = NULL;
5086 for (l = gatt_mtu_info_list; l != NULL; l = g_slist_next(l)) {
5087 info = (struct gatt_mtu_info_t*)l->data;
5091 if (!g_strcmp0(info->addr, address)) {
5092 BT_INFO("Remote GATT device found addr[%s]", info->addr);
5097 BT_INFO("Not found Remote GATT device addr[%s]", address);
5101 static void __bt_remove_mtu_gatt_device(char *address)
5103 struct gatt_mtu_info_t *dev_info = NULL;
5107 dev_info = __bt_find_mtu_gatt_device(address);
5110 BT_INFO("removing the gatt device from mtu list");
5111 gatt_mtu_info_list = g_slist_remove(gatt_mtu_info_list, dev_info);
5112 g_free(dev_info->addr);
5117 static void __bt_add_mtu_gatt_device(char *address)
5119 struct gatt_mtu_info_t *dev_info = NULL;
5123 dev_info = __bt_find_mtu_gatt_device(address);
5126 BT_INFO("adding the gatt device in mtu list");
5127 dev_info = g_new0(struct gatt_mtu_info_t, 1);
5128 dev_info->addr = g_strdup(address);
5129 dev_info->att_mtu = BT_DEFAULT_ATT_MTU;
5130 gatt_mtu_info_list = g_slist_append(gatt_mtu_info_list, dev_info);
5134 static void __bt_update_mtu_gatt_device(char *address, int mtu)
5136 struct gatt_mtu_info_t *dev_info = NULL;
5140 dev_info = __bt_find_mtu_gatt_device(address);
5143 BT_INFO("Udating the mtu");
5144 dev_info->att_mtu = mtu;
5148 int _bt_gatt_get_data_batching_available_packets(
5149 guint *available_packets)
5151 int ret = OAL_STATUS_SUCCESS;
5153 BT_CHECK_PARAMETER(available_packets, return);
5155 ret = gatt_get_data_batching_available_packets(available_packets);
5156 if (ret != OAL_STATUS_SUCCESS) {
5157 BT_ERR("ret: %d", ret);
5158 if (ret == OAL_STATUS_NOT_SUPPORT)
5159 return BLUETOOTH_ERROR_NOT_SUPPORT;
5160 return BLUETOOTH_ERROR_INTERNAL;
5163 return BLUETOOTH_ERROR_NONE;
5166 int _bt_gatt_enable_data_batching(bluetooth_device_address_t *address,
5167 int packet_threshold, int timeout)
5169 int ret = OAL_STATUS_SUCCESS;
5170 char remote_address[BT_ADDRESS_STRING_SIZE] = { 0 };
5172 BT_CHECK_PARAMETER(address, return);
5174 _bt_convert_addr_type_to_string(remote_address, address->addr);
5175 BT_INFO("Enable GATT data batching. address[%s] packet_threshold[%d] timeout[%d]",
5176 remote_address, packet_threshold, timeout);
5178 ret = gatt_enable_data_batching((bt_address_t*)(address), packet_threshold, timeout);
5180 if (ret != OAL_STATUS_SUCCESS) {
5181 BT_ERR("ret: %d", ret);
5182 if (ret == OAL_STATUS_INVALID_PARAM)
5183 return BLUETOOTH_ERROR_INVALID_PARAM;
5184 else if (ret == OAL_STATUS_NOT_SUPPORT)
5185 return BLUETOOTH_ERROR_NOT_SUPPORT;
5186 return BLUETOOTH_ERROR_INTERNAL;
5189 return BLUETOOTH_ERROR_NONE;
5192 int _bt_gatt_disable_data_batching(bluetooth_device_address_t *address)
5194 int ret = OAL_STATUS_SUCCESS;
5195 char remote_address[BT_ADDRESS_STRING_SIZE] = { 0 };
5197 BT_CHECK_PARAMETER(address, return);
5199 _bt_convert_addr_type_to_string(remote_address, address->addr);
5200 BT_INFO("Disable GATT data batching. address[%s]", remote_address);
5202 ret = gatt_disable_data_batching((bt_address_t*)(address));
5204 if (ret != OAL_STATUS_SUCCESS) {
5205 BT_ERR("ret: %d", ret);
5206 if (ret == OAL_STATUS_NOT_SUPPORT)
5207 return BLUETOOTH_ERROR_NOT_SUPPORT;
5208 return BLUETOOTH_ERROR_INTERNAL;
5211 return BLUETOOTH_ERROR_NONE;