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 (strcasecmp(app->sender, name) == 0 && app->is_initialized == TRUE) {
344 BT_DBG("numapps[%d] Match found, name: %s", k, 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_DBG("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;
478 result = gatt_enable();
479 if (result != OAL_STATUS_SUCCESS) {
480 BT_ERR("gatt Init failed");
481 return _bt_convert_oal_status_to_bt_error(result);
484 /* Register gatt event handler */
485 _bt_service_register_event_handler_callback(BT_GATT_MODULE, __bt_gatt_event_handler);
487 __bt_service_reset_gatt_data();
489 stack_name = oal_get_stack_name();
491 if (stack_name && g_strcmp0(stack_name, "bluez") == 0) {
492 /*In the platform, defacult gatt client should be registered */
493 __bt_register_default_gatt_client();
497 return BLUETOOTH_ERROR_NONE;
500 static void __bt_service_reset_gatt_data(void)
504 BT_INFO("Rest numapp");
507 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
508 numapps[k].is_initialized = 0;
509 numapps[k].instance_id = -1;
510 numapps[k].adv_handle = 0;
511 numapps[k].adv_instance = -1;
512 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
513 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
514 memset(numapps[k].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
515 memset(numapps[k].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
516 numapps[k].adv_data_len = 0;
517 numapps[k].scan_rsp_len = 0;
520 numapps[k].client_id = -1;
521 memset(numapps[k].address.addr, 0x00, BLUETOOTH_ADDRESS_LENGTH);
522 numapps[k].is_watcher_enabled = FALSE;
526 void _bt_gatt_deinit(void)
528 BT_INFO("GATT deinit");
530 /* Un-register the default gatt client before */
531 __bt_unregister_gatt_client_instance(gatt_default_client);
533 if (OAL_STATUS_SUCCESS != gatt_disable())
534 BT_ERR("gatt deinit failed");
536 /* Un-register gatt event handler */
537 _bt_service_unregister_event_handler_callback(BT_GATT_MODULE);
539 __bt_service_reset_gatt_data();
542 void _bt_update_adv_handle(const char *sender, int adv_handle)
545 BT_DBG("Sender [%s] Adv handle [%d]", sender, adv_handle);
546 bt_service_app_info_t *info = NULL;
548 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
550 /* Do not update client instance */
551 if (info->instance_id == -1)
553 /* Search for a app which has same sender and adv handle as 0 */
554 if (!g_strcmp0(info->sender, sender) && info->adv_handle == 0)
555 info->adv_handle = adv_handle;
559 int _bt_is_sender_gatt_server_with_no_adv(const char *sender, int adv_handle)
562 BT_DBG("Sender [%s] Adv handle [%d]", sender, adv_handle);
563 bt_service_app_info_t *info = NULL;
565 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
567 /* Search for a app which has same sender and adv handle as 0
568 It is possible that same sender but different adv handle */
569 if (!g_strcmp0(info->sender, sender) && info->adv_handle == 0) {
570 //info->adv_handle = adv_handle;
571 return info->instance_id;
577 int _bt_get_allocated_server_instance(const char *sender, int adv_handle, gboolean use_reserved_slot)
580 BT_DBG("Sender [%s] Adv handle [%d]", sender, adv_handle);
581 bt_service_app_info_t *info = NULL;
583 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
586 /* Exact matching of Adv handle + sender combination */
587 if (!g_strcmp0(info->sender, sender) && info->adv_handle == adv_handle)
588 return info->instance_id;
590 if (!g_strcmp0(info->sender, sender) && info->adv_handle == -1)
591 return info->instance_id;
597 char * _bt_get_sender_and_handle(int server_instance, int *adv_handle)
600 bt_service_app_info_t *info = NULL;
602 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
605 if (info->instance_id == server_instance && info->adv_handle != 0) {
606 *adv_handle = info->adv_handle;
607 BT_DBG("Server instance [%d] Adv handle [%d] Sender [%s]", server_instance, *adv_handle, info->sender);
608 return g_strdup(info->sender);
614 void _bt_set_new_adv_data(bluetooth_advertising_data_t *adv, int len, int instance)
618 bt_service_app_info_t *info = NULL;
619 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
622 if (info->instance_id == instance) {
623 memcpy(info->adv_data.data, &adv->data, len);
629 void _bt_set_new_scan_rsp_data(bluetooth_scan_resp_data_t *scan, int len, int instance)
633 bt_service_app_info_t *info = NULL;
634 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
637 if (info->instance_id == instance) {
638 memcpy(info->scan_rsp.data, &scan->data, len);
644 void _bt_get_previous_adv_data(bluetooth_advertising_data_t *adv, int *len, int instance)
647 bt_service_app_info_t *info = NULL;
649 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
652 if (info->instance_id == instance) {
653 memcpy(&adv->data, info->adv_data.data, info->adv_data_len);
654 *len = info->adv_data_len;
660 void _bt_get_previous_scan_rsp_data(bluetooth_scan_resp_data_t *scan, int *len, int instance)
664 bt_service_app_info_t *info = NULL;
666 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
669 if (info->instance_id == instance) {
670 memcpy(&scan->data, info->scan_rsp.data, info->scan_rsp_len);
671 *len = info->scan_rsp_len;
677 static int __bt_unregister_gatt_client_instance(int client_if)
679 int ret = OAL_STATUS_SUCCESS;
682 BT_INFO("DeAllocate client instance ID [%d]", client_if);
684 /* Reset data: instance_id parameter could be either for GATT Server or for GATT client */
685 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
686 if (numapps[k].client_id == client_if) {
687 BT_INFO("This is a GATT client app, unregister: Slot [%d] vacant", k);
688 numapps[k].client_id = -1;
689 numapps[k].is_initialized = FALSE;
690 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
691 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
692 memset(&numapps[k].address.addr, 0x00, sizeof(bluetooth_device_address_t));
694 /* Its a GATT Client Instance */
695 ret = gattc_deregister(client_if);
696 if (ret != OAL_STATUS_SUCCESS) {
697 BT_ERR("DeAllocate GATT Client instance with stack Fail ret: %d", ret);
698 return _bt_convert_oal_status_to_bt_error(ret);
703 return BLUETOOTH_ERROR_NONE;
706 static int __bt_unregister_gatt_server_instance(int server_instance)
708 int ret = OAL_STATUS_SUCCESS;
711 /* Unregister the server instance */
712 ret = gatts_unregister(server_instance);
713 if (ret != OAL_STATUS_SUCCESS) {
714 BT_ERR("DeAllocate server instance with stack Fail ret: %d", ret);
715 return _bt_convert_oal_status_to_bt_error(ret);
717 BT_INFO("DeAllocated server instance with stack successful..");
720 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
721 if (numapps[k].instance_id == server_instance) {
722 numapps[k].is_initialized = 0;
723 numapps[k].instance_id = -1;
724 numapps[k].adv_handle = 0;
725 numapps[k].adv_instance = -1;
726 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
727 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
728 memset(numapps[k].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
729 memset(numapps[k].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
730 numapps[k].adv_data_len = 0;
731 numapps[k].scan_rsp_len = 0;
735 BT_DBG("Going8 to reset numapp block num [%d]", k);
736 return BLUETOOTH_ERROR_NONE;
739 static void __bt_remove_all_service_handles(bt_service_app_info_t *app_info)
743 int ret = OAL_STATUS_SUCCESS;
746 if (app_info == NULL)
749 count = g_slist_length(app_info->service_handles);
750 BT_INFO("Before handle removal: current count [%d]", count);
752 for (l = app_info->service_handles; l != NULL;) {
757 BT_INFO("Server Handle to be Removed [%d] Instance ID [%d]", *handle, app_info->instance_id);
758 if (_bt_gatt_server_stop_service(app_info->sender, *handle, app_info->instance_id) != BLUETOOTH_ERROR_NONE)
761 ret = gatts_delete_service(app_info->instance_id, *handle);
762 if (ret != OAL_STATUS_SUCCESS) {
763 BT_ERR("ret: %d", ret);
766 app_info->service_handles = g_slist_remove(app_info->service_handles, handle);
769 count = g_slist_length(app_info->service_handles);
770 BT_INFO("After deleting current count [%d]", count);
776 int _bt_unregister_server_instance(const char *sender, int adv_handle)
778 BT_INFO("Unregister Allocated server instance request Sender [%s] Adv handle [%d]", sender, adv_handle);
779 int result = BLUETOOTH_ERROR_NONE;
780 int apps[MAX_APPS_SUPPORTED];
784 memset(&apps, -1, sizeof(apps));
786 if (adv_handle == 0) {
787 BT_DBG("Its a direct GATT Server app request to unregister");
788 /* Unregister server instance for each app with same sender (case: GATT Server with multiple adv handle) */
789 bt_service_app_info_t *info = NULL;
791 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
794 /* Exact matching of sender */
795 if (!g_strcmp0(info->sender, sender)) {
796 BT_INFO("Unregister GATT server instance [%d]", info->instance_id);
797 /* Unregister all service handles with stack */
798 __bt_remove_all_service_handles(info);
800 /* Disable adv if running */
801 BT_INFO("Disable Advertising Adv Handle [%d] sender [%s]",
802 info->adv_handle, sender);
803 _bt_set_advertising(sender, info->adv_handle, FALSE, FALSE);
805 /* Save all instances which need to be unregistered */
806 apps[info->instance_id] = 1;
810 BT_DBG("Its an Internal unregister request by adv application");
811 server_instance = _bt_get_allocated_server_instance(sender, adv_handle, FALSE);
812 BT_DBG("Its an Internal unregister request by adv application: Adv disabled srv instance [%d]", server_instance);
813 if (server_instance == -1) {
814 BT_ERR("No allocated server instance to be removed");
815 return BLUETOOTH_ERROR_INVALID_PARAM;
817 if (!numapps[server_instance].service_handles) {
818 BT_INFO("There are no Service handles with this app, safe to unregister");
819 /* Unregister server instance only if this sender does not have any gatt services in it */
820 result = __bt_unregister_gatt_server_instance(server_instance);
822 numapps[server_instance].adv_handle = 0;
823 memset(numapps[server_instance].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
824 numapps[server_instance].adv_data_len = 0;
825 memset(numapps[server_instance].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
826 numapps[server_instance].scan_rsp_len = 0;
831 /* Unregister all apps one by one */
832 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
834 BT_INFO("Unregister app[%d]", k);
835 /* Unregister server instance */
836 __bt_unregister_gatt_server_instance(k);
843 int _bt_register_server_instance(const char *sender, int adv_handle)
845 int ret = OAL_STATUS_SUCCESS;
846 char *uuid_string = NULL;
851 BT_INFO("Check on which instance Server instance can be initialized....");
852 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
853 if (numapps[k].is_initialized == 1) {
854 BT_DBG("Instance ID [%d] is already in use..Check next slot", numapps[k].instance_id);
857 BT_DBG("Time to register GATT Server..UUID to be used is [%s] slot [%d]", uuid_list[slot-1], slot);
863 BT_ERR("No Slot if free for GATT Server registration..");
864 return BLUETOOTH_ERROR_REGISTRATION_FAILED;
867 uuid_string = g_malloc0(BT_UUID_STRING_MAX);
868 _bt_string_to_uuid(uuid_list[slot-1], (service_uuid_t*)&uuid);
869 g_strlcpy(uuid_string, uuid_list[slot-1], BT_UUID_STRING_MAX);
870 BT_INFO("Copied UUID string [%s] slot [%d]", uuid_string, slot);
872 /* Register GATT Server */
873 ret = gatts_register(&uuid);
874 if (ret != OAL_STATUS_SUCCESS) {
875 BT_ERR("ret: %d", ret);
877 return _bt_convert_oal_status_to_bt_error(ret);
879 BT_DBG("GATT Server registration call successfully accepted by OAL..wait for Instance Initialized event from OAL..");
880 /* Return & wait for GATT Server Instance Initialization event */
881 memset(numapps[slot].sender, 0x00, sizeof(numapps[slot].sender));
882 memset(numapps[slot].uuid, 0x00, sizeof(numapps[slot].uuid));
884 g_strlcpy(numapps[slot].sender, sender, sizeof(numapps[slot].sender));
885 g_strlcpy(numapps[slot].uuid, uuid_string, sizeof(numapps[slot].uuid));
887 numapps[slot].is_initialized = 0; /* Set initialization from app registered callback */
888 numapps[slot].adv_handle = adv_handle;
891 return BLUETOOTH_ERROR_NONE;
895 static void __bt_gatt_handle_pending_request_info(int result,
896 int service_function, void *data, unsigned int size)
900 invocation_info_t *req_info = NULL;
901 ret_if(data == NULL);
903 for (l = _bt_get_invocation_list(); l != NULL; ) {
906 if (req_info == NULL || req_info->service_function != service_function)
909 switch (service_function) {
911 case BT_GATT_SERVER_REGISTER: {
912 bt_service_app_info_t *param = (bt_service_app_info_t*)data;
914 if (!g_strcmp0(req_info->sender, param->sender)) {
915 BT_DBG("GATT Server app found [%s]", req_info->sender);
917 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
918 g_array_append_vals(out_param, ¶m->instance_id, sizeof(int));
919 _bt_service_method_return(req_info->context, out_param, result);
921 g_free(req_info->user_data);
922 _bt_free_info_from_invocation_list(req_info);
923 g_array_free(out_param, TRUE);
927 case BT_GATT_SERVER_START_SERVICE:
928 case BT_GATT_SERVER_DELETE_SERVICE: {
929 bt_service_app_info_t *param = (bt_service_app_info_t*)data;
931 int *saved_instance_id = (int*)req_info->user_data;
932 if (!g_strcmp0(req_info->sender, param->sender) && param->instance_id == *saved_instance_id) {
933 BT_DBG("GATT Server app found [%s] Instance ID [%d] Reply DBUS",
934 req_info->sender, *saved_instance_id);
936 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
937 g_array_append_vals(out_param, &saved_instance_id, sizeof(int));
938 _bt_service_method_return(req_info->context, out_param, result);
940 g_free(req_info->user_data);
941 _bt_free_info_from_invocation_list(req_info);
942 g_array_free(out_param, TRUE);
946 case BT_GATT_SERVER_ADD_SERVICE:
947 case BT_GATT_SERVER_ADD_DESCRIPTOR:
948 case BT_GATT_SERVER_ADD_CHARACTERISTIC: {
949 int *handle = (int*)data;
950 BT_DBG("Characteristic added: Handle [%d]", *handle);
951 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
953 g_array_append_vals(out_param, handle, sizeof(int));
954 _bt_service_method_return(req_info->context, out_param, result);
956 g_free(req_info->user_data);
957 _bt_free_info_from_invocation_list(req_info);
958 g_array_free(out_param, TRUE);
963 case BT_DISCONNECT_LE: {
964 #ifdef TIZEN_GATT_CLIENT
965 char *addr = (char*)req_info->user_data;
966 bluetooth_device_address_t address;
968 if (!g_strcmp0(addr, (char*)data)) {
969 BT_INFO("GATT Client connect-disconnect call pending for app [%s] addr [%s]",
970 req_info->sender, addr + 12);
971 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
972 _bt_convert_addr_string_to_type(address.addr, addr);
974 g_array_append_vals(out_param, (bluetooth_device_address_t*)&address,
975 sizeof(bluetooth_device_address_t));
976 _bt_service_method_return(req_info->context, out_param, result);
978 g_free(req_info->user_data);
979 _bt_free_info_from_invocation_list(req_info);
980 g_array_free(out_param, TRUE);
983 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
985 g_array_append_vals(out_param, (bluetooth_device_address_t*)data,
986 sizeof(bluetooth_device_address_t));
987 _bt_service_method_return(req_info->context, out_param, result);
989 g_free(req_info->user_data);
990 _bt_free_info_from_invocation_list(req_info);
992 g_array_free(out_param, TRUE);
996 #ifdef TIZEN_GATT_CLIENT
997 case BT_GATT_CLIENT_REGISTER: {
998 bt_service_app_info_t *param = (bt_service_app_info_t*)data;
1000 if (!g_strcmp0(req_info->sender, param->sender)) {
1001 BT_DBG("GATT Client app found [%s] created client ID [%d]",
1002 req_info->sender, param->client_id);
1004 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1005 g_array_append_vals(out_param, ¶m->client_id, sizeof(int));
1006 _bt_service_method_return(req_info->context, out_param, result);
1008 g_free(req_info->user_data);
1009 _bt_free_info_from_invocation_list(req_info);
1010 g_array_free(out_param, TRUE);
1014 case BT_GATT_GET_PRIMARY_SERVICES: {
1015 bt_services_browse_info_t *param = (bt_services_browse_info_t*)data;
1016 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1017 _bt_convert_addr_type_to_string(address,
1018 (unsigned char *)(¶m->device_addr.addr));
1020 /* Match address to determine same request */
1021 if (!g_strcmp0((char*)req_info->user_data, address)) {
1022 BT_DBG("GATT Client app found [%s] Remote address [%s]",
1023 req_info->sender, address);
1025 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1026 g_array_append_vals(out_param, param,
1027 sizeof(bt_services_browse_info_t));
1029 //g_array_append_vals(out_param, ¶m->client_id, sizeof(int));
1030 _bt_service_method_return(req_info->context, out_param, result);
1032 g_free(req_info->user_data);
1033 _bt_free_info_from_invocation_list(req_info);
1034 g_array_free(out_param, TRUE);
1039 case BT_GATT_GET_SERVICE_PROPERTIES: {
1040 bt_char_browse_info_t param;
1041 memcpy((void*)¶m, data, sizeof(bt_char_browse_info_t));
1042 //bt_char_browse_info_t *param = (bt_char_browse_info_t*)data;
1044 bluetooth_gatt_client_svc_prop_info_t *prop = (bluetooth_gatt_client_svc_prop_info_t*)req_info->user_data;
1046 /* Match both address and service properties to determine same request */
1047 if (!memcmp(param.device_addr.addr,
1048 prop->device_address.addr,
1049 sizeof(bluetooth_device_address_t)) &&
1050 !memcmp(param.svc_uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1051 param.svc_inst_id == prop->svc.instance_id) {
1052 BT_DBG("Returning Service properties");
1054 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1055 g_array_append_vals(out_param, ¶m, sizeof(bt_char_browse_info_t));
1056 _bt_service_method_return(req_info->context, out_param, result);
1058 g_free(req_info->user_data);
1059 _bt_free_info_from_invocation_list(req_info);
1060 g_array_free(out_param, TRUE);
1064 case BT_GATT_GET_CHARACTERISTIC_PROPERTIES: {
1065 bt_descriptor_browse_info_t *param = (bt_descriptor_browse_info_t*)data;
1067 bluetooth_gatt_client_char_prop_info_t *prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
1069 /* Match both address, service properties &char properties to determine same request */
1070 if (!memcmp(param->device_addr.addr,
1071 prop->device_address.addr,
1072 sizeof(bluetooth_device_address_t)) &&
1073 !memcmp(param->svc_uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1074 param->svc_inst_id == prop->svc.instance_id &&
1075 !memcmp(param->char_uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1076 param->char_inst_id == prop->characteristic.instance_id) {
1077 BT_DBG("Returning Characteristic properties");
1078 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1079 g_array_append_vals(out_param, param, sizeof(bt_descriptor_browse_info_t));
1080 _bt_service_method_return(req_info->context, out_param, result);
1082 g_free(req_info->user_data);
1083 _bt_free_info_from_invocation_list(req_info);
1084 g_array_free(out_param, TRUE);
1088 case BT_GATT_WATCH_CHARACTERISTIC: {
1089 bt_gatt_notif_reg_info_t *param = (bt_gatt_notif_reg_info_t*)data;
1090 bluetooth_gatt_client_char_prop_info_t *prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
1092 /* Match both address, service properties &char properties to determine same request */
1093 if (!memcmp(param->addr.addr,
1094 prop->device_address.addr,
1095 sizeof(bluetooth_device_address_t)) &&
1096 !memcmp(param->svc_uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1097 param->svc_inst == prop->svc.instance_id &&
1098 !memcmp(param->char_uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1099 param->char_inst == prop->characteristic.instance_id) {
1100 BT_INFO("Characteristic Watch Successful: Is registered [%d]",
1101 param->is_registered);
1102 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1103 g_array_append_vals(out_param, param, sizeof(bt_gatt_notif_reg_info_t));
1104 _bt_service_method_return(req_info->context, out_param, result);
1105 g_free(req_info->user_data);
1106 _bt_free_info_from_invocation_list(req_info);
1107 g_array_free(out_param, TRUE);
1111 case BT_GATT_READ_CHARACTERISTIC:
1112 case BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE: {
1113 bluetooth_gatt_client_char_prop_info_t *param = (bluetooth_gatt_client_char_prop_info_t*)data;
1115 char *addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
1116 bluetooth_gatt_client_char_prop_info_t *prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
1117 _bt_convert_addr_type_to_string(addr, prop->device_address.addr);
1119 if (!memcmp(param, prop, sizeof(bluetooth_gatt_client_char_prop_info_t))) {
1120 BT_DBG("Gatt Char read or write request matched for address [%s]", addr);
1121 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1122 g_array_append_vals(out_param, param, sizeof(bluetooth_gatt_client_char_prop_info_t));
1123 _bt_service_method_return(req_info->context, out_param, result);
1125 g_free(req_info->user_data);
1126 _bt_free_info_from_invocation_list(req_info);
1127 g_array_free(out_param, TRUE);
1132 case BT_GATT_READ_DESCRIPTOR_VALUE:
1133 case BT_GATT_WRITE_DESCRIPTOR_VALUE: {
1134 bluetooth_gatt_client_desc_prop_info_t *param = (bluetooth_gatt_client_desc_prop_info_t*)data;
1136 char *addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
1137 bluetooth_gatt_client_desc_prop_info_t *prop = (bluetooth_gatt_client_desc_prop_info_t*)req_info->user_data;
1138 _bt_convert_addr_type_to_string(addr, prop->device_address.addr);
1140 if (!memcmp(param, prop, sizeof(bluetooth_gatt_client_desc_prop_info_t))) {
1141 BT_DBG("Descriptor read or write request matched for address [%s]", addr);
1142 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1143 g_array_append_vals(out_param, param, sizeof(bluetooth_gatt_client_desc_prop_info_t));
1144 _bt_service_method_return(req_info->context, out_param, result);
1146 g_free(req_info->user_data);
1147 _bt_free_info_from_invocation_list(req_info);
1148 g_array_free(out_param, TRUE);
1153 case BT_REQ_ATT_MTU: {
1154 char *addr = (char*)req_info->user_data;
1155 bluetooth_device_address_t address;
1157 if (!g_strcmp0(addr, (char*)data)) {
1158 BT_DBG("GATT Client BT_REQ_ATT_MTU call pending for app [%s] addr [%s]",
1159 req_info->sender, addr);
1160 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1161 _bt_convert_addr_string_to_type(address.addr, addr);
1163 g_array_append_vals(out_param, (bluetooth_device_address_t*)&address,
1164 sizeof(bluetooth_device_address_t));
1165 _bt_service_method_return(req_info->context, out_param, result);
1167 g_free(req_info->user_data);
1168 _bt_free_info_from_invocation_list(req_info);
1169 g_array_free(out_param, TRUE);
1179 static void __bt_handle_server_instance_registered(event_gatts_register_t *data)
1181 bt_service_app_info_t *info = NULL;
1183 char *uuid_string = g_malloc0(BT_UUID_STRING_MAX);
1185 _bt_uuid_to_string(&(data->server_uuid), uuid_string);
1186 BT_INFO("Instance ID is Intialized [%d] UUID initialized [%s]", data->server_inst, uuid_string);
1188 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1189 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1192 if (g_strcmp0(info->uuid, uuid_string) == 0) {
1193 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1194 info->is_initialized = TRUE;
1195 info->instance_id = data->server_inst;
1196 info->adv_instance = data->server_inst;
1197 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE, BT_GATT_SERVER_REGISTER,
1198 (void*)info, sizeof(bt_service_app_info_t));
1202 g_free(uuid_string);
1205 static void __bt_handle_gatt_server_service_added(event_gatts_srvc_prm_t *event)
1207 int result = BLUETOOTH_ERROR_NONE;
1211 bt_service_app_info_t *info = NULL;
1214 char *uuid_str = g_malloc0(BT_UUID_STRING_MAX);
1215 _bt_uuid_to_string(&(event->gatt_srvc_id.id.uuid), uuid_str);
1216 BT_INFO("GATT Added Service UUID: [%s] Server Ins [%d] Service hdl [%d] Is Primary [%d]\n",
1217 uuid_str, event->gatt_srvc_stat.server_inst, svc_handle, event->gatt_srvc_id.is_prmry);
1219 if (event->gatt_srvc_stat.status != OAL_STATUS_SUCCESS) {
1220 result = BLUETOOTH_ERROR_INTERNAL;
1221 svc_handle = 0; /* Service handle set to 0 indicates.
1222 0 is reserved by BT SIG, cant be used by app*/
1224 svc_handle = event->gatt_srvc_stat.servic_hndl;
1226 BT_INFO("GATT Added Service Status [%d] Server Ins [%d] Service hdl [%d] Is Primary [%d]\n",
1227 event->gatt_srvc_stat.status, event->gatt_srvc_stat.server_inst, svc_handle, event->gatt_srvc_id.is_prmry);
1229 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1230 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1233 if (info->instance_id == event->gatt_srvc_stat.server_inst) {
1234 BT_INFO("numapps[%d] Found GATT Server.. UUID [%s], sender [%s]", k, info->uuid, info->sender);
1235 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_ADD_SERVICE,
1236 (int*)&svc_handle, sizeof(int));
1238 /* Add Service Handle */
1239 if (svc_handle > 0) {
1240 handle = g_malloc0(sizeof(int));
1241 *handle = svc_handle;
1242 numapps[k].service_handles = g_slist_append(numapps[k].service_handles, handle);
1243 count = g_slist_length(numapps[k].service_handles);
1244 BT_INFO("Added Service handle [%d] to list..current count [%d]", svc_handle, count);
1253 static void __bt_handle_gatt_server_characteristic_added(event_gatts_srvc_charctr_t *event)
1255 int result = BLUETOOTH_ERROR_NONE;
1257 bt_service_app_info_t *info = NULL;
1259 char *uuid_str = g_malloc0(BT_UUID_STRING_MAX);
1261 BT_INFO("GATT Server Char added status [%d]", event->gatt_srvc_stat.status);
1262 BT_INFO("GATT Server_inst: [%d]", event->gatt_srvc_stat.server_inst);
1263 BT_INFO("GATT Service Handle: [%d]", event->gatt_srvc_stat.servic_hndl);
1264 BT_INFO("GATT Add characteristic Status: [%d]", event->gatt_srvc_stat.status);
1265 BT_INFO("GATT Service characteristic Handle: [%d]", event->charctr_hndl);
1267 if (event->gatt_srvc_stat.status != OAL_STATUS_SUCCESS) {
1268 result = BLUETOOTH_ERROR_INTERNAL;
1269 char_handle = 0; /* characteristic handle set to 0 indicates.
1270 0 is reserved by BT SIG, cant be used by app*/
1272 char_handle = event->charctr_hndl;
1275 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1276 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1279 if (info->instance_id == event->gatt_srvc_stat.server_inst) {
1280 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1281 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_ADD_CHARACTERISTIC,
1282 (int*)&char_handle, sizeof(int));
1288 _bt_uuid_to_string(&(event->charctr_uuid), uuid_str);
1289 BT_INFO("GATT Added Characteristic: UUID: [%s]", uuid_str);
1294 static void __bt_handle_gatt_server_descriptor_added(event_gatts_srvc_descr_t* event)
1296 int result = BLUETOOTH_ERROR_NONE;
1298 bt_service_app_info_t *info = NULL;
1300 char *uuid_str = g_malloc0(BT_UUID_STRING_MAX);
1302 BT_INFO("GATT Server Descriptor added status [%d]", event->gatt_srvc_stat.status);
1303 BT_INFO("GATT Server_inst: [%d]", event->gatt_srvc_stat.server_inst);
1304 BT_INFO("GATT Service Handle: [%d]", event->gatt_srvc_stat.servic_hndl);
1305 BT_INFO("GATT Add Descriptor Status: [%d]", event->gatt_srvc_stat.status);
1306 BT_INFO("GATT Service Descriptor Handle: [%d]", event->descrptr_hndl);
1308 if (event->gatt_srvc_stat.status != OAL_STATUS_SUCCESS) {
1309 result = BLUETOOTH_ERROR_INTERNAL;
1310 desc_handle = 0; /* Service handle set to 0 indicates.
1311 0 is reserved by BT SIG, cant be used by app*/
1313 desc_handle = event->descrptr_hndl;
1315 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1316 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1319 if (info->instance_id == event->gatt_srvc_stat.server_inst) {
1320 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1321 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_ADD_DESCRIPTOR,
1322 (int*)&desc_handle, sizeof(int));
1327 _bt_uuid_to_string(&(event->descrptr_uuid), uuid_str);
1328 BT_INFO("GATT Added Descriptor: UUID: [%s]", uuid_str);
1333 static void __bt_handle_gatt_server_service_started(event_gatts_srvc_t *event)
1335 bt_service_app_info_t *info = NULL;
1336 int result = BLUETOOTH_ERROR_NONE;
1338 BT_INFO("GATT Server Service Started..");
1341 BT_INFO("GATT Server_inst: [%d]", event->server_inst);
1342 BT_INFO("GATT Service Handle: [%d]", event->servic_hndl);
1343 BT_INFO("GATT Service Started Status: [%d]", event->status);
1345 if (event->status != OAL_STATUS_SUCCESS) {
1346 BT_ERR("GATT Server Service Start Failed Err: [%d]", event->status);
1347 result = BLUETOOTH_ERROR_INTERNAL;
1350 /* Check if the just registered Instance ID belongs to requester */
1351 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1354 if (info->instance_id == event->server_inst) {
1355 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1356 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_START_SERVICE,
1357 (void*)info, sizeof(bt_service_app_info_t));
1363 static void __bt_handle_gatt_server_service_stopped(event_gatts_srvc_t *event)
1365 int result = BLUETOOTH_ERROR_NONE;
1366 bt_service_app_info_t *info = NULL;
1368 BT_INFO("GATT Server Service Stopped..");
1370 BT_INFO("GATT Server_inst: [%d]", event->server_inst);
1371 BT_INFO("GATT Service Handle: [%d]", event->servic_hndl);
1372 BT_INFO("GATT Service Stopped Status: [%d]", event->status);
1374 if (event->status != OAL_STATUS_SUCCESS) {
1375 BT_ERR("GATT Server Service Stop Failed Err: [%d]", event->status);
1376 result = BLUETOOTH_ERROR_INTERNAL;
1379 /* Check if the just registered Instance ID belongs to requester */
1380 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1383 if (info->instance_id == event->server_inst) {
1384 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1385 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_STOP_SERVICE,
1386 (void*)info, sizeof(bt_service_app_info_t));
1392 static void __bt_handle_gatt_server_service_deleted(event_gatts_srvc_t *event)
1394 int result = BLUETOOTH_ERROR_NONE;
1395 bt_service_app_info_t *info = NULL;
1397 BT_INFO("GATT Server Service Deleted..");
1399 BT_INFO("GATT Server_inst: [%d]", event->server_inst);
1400 BT_INFO("GATT Service Handle: [%d]", event->servic_hndl);
1401 BT_INFO("GATT Service Deleted Status: [%d]", event->status);
1403 if (event->status != OAL_STATUS_SUCCESS) {
1404 BT_ERR("GATT Server Service Delete Failed Err: [%d]", event->status);
1405 result = BLUETOOTH_ERROR_INTERNAL;
1408 /* Check if the just registered Instance ID belongs to requester */
1409 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1412 if (info->instance_id == event->server_inst) {
1413 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1414 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_DELETE_SERVICE,
1415 (void*)info, sizeof(bt_service_app_info_t));
1421 struct gatt_client_info_t *_bt_find_remote_gatt_client_info(char *address)
1424 struct gatt_client_info_t *info = NULL;
1425 for (l = gatt_client_info_list; l != NULL; l = g_slist_next(l)) {
1426 info = (struct gatt_client_info_t*)l->data;
1430 if (!g_strcmp0(info->addr, address)) {
1431 BT_DBG("Remote GATT client found addr[%s]", info->addr);
1438 static struct gatt_client_info_t *__bt_find_remote_gatt_client_info_from_conn_id(int conn_id)
1441 struct gatt_client_info_t *info = NULL;
1443 for (l = gatt_client_info_list; l != NULL; l = g_slist_next(l)) {
1444 info = (struct gatt_client_info_t*)l->data;
1448 if (info->connection_id == conn_id) {
1449 BT_INFO("Remote GATT client found addr[%s]", info->addr);
1456 #ifdef TIZEN_GATT_CLIENT
1457 struct gatt_server_info_t *_bt_find_remote_gatt_server_info(char *address)
1460 struct gatt_server_info_t *info = NULL;
1461 for (l = gatt_server_info_list; l != NULL; l = g_slist_next(l)) {
1462 info = (struct gatt_server_info_t*)l->data;
1466 if (!g_strcmp0(info->addr, address)) {
1467 BT_DBG("Remote GATT Server found addr[%s] connection_id %d", info->addr, info->connection_id);
1474 static struct gatt_out_conn_info_t* __bt_find_gatt_outgoing_conn_info(char *address)
1477 struct gatt_out_conn_info_t *info = NULL;
1478 for (l = outgoing_gatt_conn_list; l != NULL; l = g_slist_next(l)) {
1479 info = (struct gatt_out_conn_info_t*)l->data;
1483 if (!g_strcmp0(info->addr, address)) {
1484 BT_INFO("Outgoing connection info found addr[%s]", info->addr + 12);
1492 #ifdef TIZEN_GATT_CLIENT
1493 static void __bt_handle_gatt_server_connection_state(event_gatts_conn_t *event)
1495 int result = BLUETOOTH_ERROR_NONE;
1496 struct gatt_client_info_t *client_info = NULL;
1497 #ifndef __INTEGRATE_GATT_INFO__
1498 struct gatt_server_info_t *server_info = NULL;
1500 bluetooth_device_address_t dev_addr;
1501 GVariant *param = NULL;
1504 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1506 memcpy(dev_addr.addr, event->address.addr, 6);
1508 /* REPLY dbus Context to application which called BT_CONNECT_LE. There is status
1510 _bt_convert_addr_type_to_string(address,
1511 (unsigned char *)dev_addr.addr);
1513 if (event->status != OAL_STATUS_SUCCESS)
1514 result = BLUETOOTH_ERROR_INTERNAL;
1516 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
1517 address, BT_ADDRESS_STRING_SIZE);
1519 BT_INFO("GATT Server Connedted: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1520 address + 12, event->server_inst, event->conn_id);
1523 /* Check if device is already in connected list */
1524 client_info = _bt_find_remote_gatt_client_info(address);
1527 BT_DBG("Conn Info absent: But no need to Send Local GATT Server Connected event to apps");
1529 param = g_variant_new("(is)", result, address);
1531 /* Send event to application */
1532 _bt_send_event(BT_DEVICE_EVENT,
1533 BLUETOOTH_EVENT_GATT_SERVER_CONNECTED, /* Local device is GATT server */
1536 /* Save client connection info */
1537 client_info = g_new0(struct gatt_client_info_t, 1);
1538 client_info->addr = g_strdup(address);
1539 BT_INFO("Added GATT client addr[%s]", client_info->addr + 12);
1540 client_info->connection_id = event->conn_id;
1541 #ifdef __INTEGRATE_GATT_INFO__
1542 client_info->client_id = -1;
1544 client_info->instance_id = event->server_inst;
1545 gatt_client_info_list = g_slist_append(gatt_client_info_list, client_info);
1546 BT_INFO("Total num of connected Remote GATT Clients [%d]", g_slist_length(gatt_client_info_list));
1548 #ifndef __INTEGRATE_GATT_INFO__
1549 /* Save server connection info */
1550 server_info = g_new0(struct gatt_server_info_t, 1);
1551 server_info->addr = g_strdup(address);
1552 server_info->client_id = -1;
1553 BT_INFO("Added GATT server addr[%s]", server_info->addr + 12);
1554 server_info->connection_id = event->conn_id;
1555 gatt_server_info_list = g_slist_append(gatt_server_info_list, server_info);
1556 BT_INFO("Total num of connected Remote GATT Servers [%d]", g_slist_length(gatt_server_info_list));
1559 ret = gattc_add_connection_info((bt_address_t *)&dev_addr, event->conn_id, event->server_inst);
1560 if (ret != OAL_STATUS_SUCCESS) {
1561 BT_ERR("gattc register server instance failed");
1565 __bt_add_mtu_gatt_device(address);
1570 /* GATT Server Dis connected */
1571 static void __bt_handle_gatt_server_disconnection_state(event_gatts_conn_t *event)
1573 int result = BLUETOOTH_ERROR_NONE;
1574 struct gatt_client_info_t *client_info = NULL;
1575 #ifndef __INTEGRATE_GATT_INFO__
1576 struct gatt_server_info_t *server_info = NULL;
1578 bluetooth_device_address_t dev_addr;
1579 GVariant *param = NULL;
1580 char address[BT_ADDRESS_STRING_SIZE];
1582 memcpy(dev_addr.addr, event->address.addr, 6);
1584 /* REPLY dbus Context to application which called BT_DISCONNECT_LE. There is status
1586 _bt_convert_addr_type_to_string(address,
1587 (unsigned char *)dev_addr.addr);
1589 if (event->status != OAL_STATUS_SUCCESS)
1590 result = BLUETOOTH_ERROR_INTERNAL;
1592 if (NULL == _bt_get_request_info_data(BT_DISCONNECT_LE, address)) {
1593 if (NULL != _bt_get_request_info_data(BT_CONNECT_LE, address)) {
1594 result = BLUETOOTH_ERROR_INTERNAL;
1596 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
1597 address, BT_ADDRESS_STRING_SIZE);
1599 BT_ERR("Failed to connect Local GATT Server Remote Client addr[%s]", address + 12);
1603 __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE,
1604 address, BT_ADDRESS_STRING_SIZE);
1607 BT_INFO("Local GATT Server DisConnected: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1608 address + 12, event->server_inst, event->conn_id);
1609 /* Remove Connection info */
1610 client_info = _bt_find_remote_gatt_client_info(address);
1612 BT_DBG("No need to Send Local GATT Server Disconnected event to apps, just remove remote client info");
1614 param = g_variant_new("(is)", result, address);
1615 /* Send event to application */
1616 _bt_send_event(BT_DEVICE_EVENT,
1617 BLUETOOTH_EVENT_GATT_SERVER_DISCONNECTED, /* Local device is GATT server */
1620 #ifndef __INTEGRATE_GATT_INFO__
1621 /* Remove server info from list */
1622 server_info = _bt_find_remote_gatt_server_info(address);
1624 gatt_server_info_list = g_slist_remove(gatt_server_info_list, server_info);
1626 BT_INFO("Can not find conn info, already removed!");
1628 /* Remove all services from info list_gatt_info */
1629 __bt_cleanup_remote_services(server_info);
1631 /* Remove all services from info list_gatt_info */
1632 __bt_cleanup_remote_services(client_info);
1635 /* Remove client info from List */
1636 gatt_client_info_list = g_slist_remove(gatt_client_info_list, client_info);
1637 BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
1638 g_free(client_info->addr);
1639 g_free(client_info);
1642 __bt_remove_mtu_gatt_device(address);
1646 static void __bt_handle_gatt_server_connection_state(event_gatts_conn_t *event)
1648 int result = BLUETOOTH_ERROR_NONE;
1649 struct gatt_client_info_t *conn_info = NULL;
1650 bt_service_app_info_t *info = NULL;
1651 bluetooth_device_address_t dev_addr;
1652 GVariant *param = NULL;
1655 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1657 memcpy(dev_addr.addr, event->address.addr, 6);
1660 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1661 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1664 if (info->instance_id == event->server_inst) {
1665 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1666 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
1667 (bluetooth_device_address_t*)&dev_addr, sizeof(bluetooth_device_address_t));
1673 /* Send event to BT-API */
1674 _bt_convert_addr_type_to_string(address,
1675 (unsigned char *)dev_addr.addr);
1677 BT_INFO("GATT Server Connedted: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1678 address, event->server_inst, event->conn_id);
1681 /* Check if device is already in connected list */
1682 conn_info = _bt_find_remote_gatt_client_info(address);
1685 param = g_variant_new("(is)", result, address);
1686 /* Send event to application */
1687 _bt_send_event(BT_GATT_SERVER_EVENT,
1688 BLUETOOTH_EVENT_GATT_CONNECTED,
1691 /* Save Connection info */
1692 conn_info = g_new0(struct gatt_client_info_t, 1);
1693 conn_info->addr = g_strdup(address);
1694 BT_INFO("Added GATT client addr[%s]", conn_info->addr);
1695 conn_info->connection_id = event->conn_id;
1696 conn_info->instance_id = event->server_inst;
1697 gatt_client_info_list = g_slist_append(gatt_client_info_list, conn_info);
1698 BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
1704 /* GATT Server Dis connected */
1705 static void __bt_handle_gatt_server_disconnection_state(event_gatts_conn_t *event)
1707 int result = BLUETOOTH_ERROR_NONE;
1708 struct gatt_client_info_t *conn_info = NULL;
1709 bluetooth_device_address_t dev_addr;
1710 bt_service_app_info_t *info = NULL;
1711 GVariant *param = NULL;
1714 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1716 memcpy(dev_addr.addr, event->address.addr, 6);
1718 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1719 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1722 if (info->instance_id == event->server_inst) {
1723 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1724 __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE,
1725 (bluetooth_device_address_t*)&dev_addr, sizeof(bluetooth_device_address_t));
1731 /* Send event to BT-API */
1732 _bt_convert_addr_type_to_string(address,
1733 (unsigned char *)dev_addr.addr);
1735 BT_INFO("GATT Server DisConnected: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1736 address, event->server_inst, event->conn_id);
1738 param = g_variant_new("(is)", result, address);
1739 /* Send event to application */
1740 _bt_send_event(BT_GATT_SERVER_EVENT,
1741 BLUETOOTH_EVENT_GATT_DISCONNECTED,
1744 /* Remove Connection info */
1745 conn_info = _bt_find_remote_gatt_client_info(address);
1747 BT_INFO("Remove GATT client info from List..");
1748 /* Remove info from List */
1749 gatt_client_info_list = g_slist_remove(gatt_client_info_list, conn_info);
1750 BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
1751 g_free(conn_info->addr);
1760 static void __bt_handle_gatt_server_acquire_write_requested(event_gatts_srvc_acquire_attr_t *event)
1762 GVariant *param = NULL;
1763 int result = BLUETOOTH_ERROR_NONE;
1764 struct gatt_server_req_info *req_info = NULL;
1765 bluetooth_device_address_t dev_addr;
1766 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1768 BT_INFO("GATT Server ACQUIRE Write Req");
1769 BT_DBG(" conn id %d, trans id %d, attr andle %d", event->attr_trans.conn_id,
1770 event->attr_trans.trans_id, event->attr_trans.attr_handle);
1772 //address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1773 memcpy(dev_addr.addr, event->address.addr, 6);
1775 req_info = g_new0(struct gatt_server_req_info, 1);
1776 req_info->request_id = event->attr_trans.trans_id;
1777 req_info->attribute_handle = event->attr_trans.attr_handle;
1778 req_info->connection_id = event->attr_trans.conn_id;
1779 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_ACQUIRE_WRITE;
1780 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1782 _bt_convert_addr_type_to_string(address,
1783 (unsigned char *)dev_addr.addr);
1785 param = g_variant_new("(iiiiiis)", result,
1786 event->attr_trans.conn_id,
1787 event->attr_trans.trans_id,
1788 event->attr_trans.attr_handle,
1789 event->mtu, event->attr_trans.offset, address);
1790 BT_DBG("remote address : [%s]", address);
1792 _bt_send_event(BT_GATT_SERVER_EVENT,
1793 BLUETOOTH_EVENT_GATT_SERVER_ACQUIRE_WRITE,
1798 static void __bt_handle_gatt_server_acquire_notify_requested(event_gatts_srvc_acquire_attr_t *event)
1800 GVariant *param = NULL;
1801 int result = BLUETOOTH_ERROR_NONE;
1802 struct gatt_server_req_info *req_info = NULL;
1803 bluetooth_device_address_t dev_addr;
1804 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1806 BT_INFO("GATT Server ACQUIRE Notify Req");
1807 BT_DBG("conn id %d, trans id %d, attr handle %d, req address %s",
1808 event->attr_trans.conn_id, event->attr_trans.trans_id,
1809 event->attr_trans.attr_handle, address);
1811 memcpy(dev_addr.addr, event->address.addr, 6);
1812 _bt_convert_addr_type_to_string(address,
1813 (unsigned char *)dev_addr.addr);
1814 BT_DBG("remote address : [%s]", address);
1816 req_info = g_new0(struct gatt_server_req_info, 1);
1817 req_info->request_id = event->attr_trans.trans_id;
1818 req_info->attribute_handle = event->attr_trans.attr_handle;
1819 req_info->connection_id = event->attr_trans.conn_id;
1820 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_ACQUIRE_NOTIFY;
1821 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1823 param = g_variant_new("(iiiiiis)", result,
1824 event->attr_trans.conn_id,
1825 event->attr_trans.trans_id,
1826 event->attr_trans.attr_handle,
1827 event->mtu, event->attr_trans.offset,
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;
1923 memcpy(dev_addr.addr, event->address.addr, 6);
1924 _bt_convert_addr_type_to_string(address,
1925 (unsigned char *)dev_addr.addr);
1927 BT_DBG("conn_id %d, trans id %d, attr handle %d, offset %d, is_long %d, addr %s",
1928 event->attr_trans.conn_id, event->attr_trans.trans_id,
1929 event->attr_trans.attr_handle, event->attr_trans.offset,
1930 event->is_long, address);
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 param = g_variant_new("(iiiiibs)", result,
1946 event->attr_trans.conn_id,
1947 event->attr_trans.trans_id,
1948 event->attr_trans.attr_handle,
1949 event->attr_trans.offset,
1953 _bt_send_event(BT_GATT_SERVER_EVENT,
1954 BLUETOOTH_EVENT_GATT_SERVER_READ_REQUESTED,
1958 static void __bt_handle_gatt_server_indicate_confirmed(event_gatts_ind_cnfrm_t *event)
1960 bluetooth_device_address_t dev_addr;
1961 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1962 int cur_connected_clients;
1963 static int recvd = 0;
1964 gboolean completed = 0;
1965 GVariant *param = NULL;
1967 /* OAL event does provide error, so MW assumes event will never contain wrong data,
1968 incase of any issues, check with OAL */
1969 int result = BLUETOOTH_ERROR_NONE;
1971 memcpy(dev_addr.addr, event->address.addr, 6);
1972 _bt_convert_addr_type_to_string(address,
1973 (unsigned char *)dev_addr.addr);
1975 BT_INFO("Indication sent to GATT client [%s] conn_ ID [%d] transaction ID [%d] Att handle [%d]",
1976 address, event->conn_id, event->trans_id, event->attr_handle);
1979 cur_connected_clients = g_slist_length(gatt_client_info_list);
1980 BT_INFO("Number of connected clients during sending Indication [%d] & current connected count [%d]",
1981 num_indicate_clients, cur_connected_clients);
1984 if (recvd == num_indicate_clients) {
1985 BT_INFO("Gatt indication confirm event for last GATT client.. [%s]", address);
1986 completed = 1; /* Last event */
1987 recvd = 0; /* Reset */
1988 num_indicate_clients = 0;
1991 param = g_variant_new("(isib)",
1997 /* Send event to BT-API */
1998 _bt_send_event(BT_GATT_SERVER_EVENT,
1999 BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_COMPLETED,
2002 BT_INFO("Received Indication confirm for client number [%d]", recvd);
2006 /* Tizen Platform Specific */
2007 static void __bt_handle_gatt_server_notification_changed(event_gatts_notif_t *event)
2009 bluetooth_device_address_t dev_addr;
2010 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2011 GVariant *param = NULL;
2014 /* OAL event does provide error, so MW assumes event will never contain wrong data,
2015 incase of any issues, check with OAL */
2016 int result = BLUETOOTH_ERROR_NONE;
2018 memcpy(dev_addr.addr, event->address.addr, 6);
2019 _bt_convert_addr_type_to_string(address,
2020 (unsigned char *)dev_addr.addr);
2022 BT_INFO("notification_changed [%s] conn_ ID [%d] transaction ID [%d] Att handle [%d] Notify[%d]",
2023 address, event->conn_id, event->trans_id, event->attr_handle, event->notify);
2025 /* Set Notifcation status */
2026 notify = event->notify;
2028 param = g_variant_new("(isib)",
2034 /* Send event to BT-API */
2035 _bt_send_event(BT_GATT_SERVER_EVENT,
2036 BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_STATE_CHANGED,
2042 static void __bt_handle_gatt_mtu_changed_event(event_gatts_mtu_changed_t *event)
2044 int result = BLUETOOTH_ERROR_NONE;
2045 struct gatt_client_info_t *conn_info = NULL;
2046 GVariant *param = NULL;
2049 conn_info = __bt_find_remote_gatt_client_info_from_conn_id(event->conn_id);
2050 if (conn_info == NULL) {
2051 BT_ERR("Cant find connection Information");
2054 BT_INFO("Got connection Info GATT client [%s] MTU Size [%d]",
2055 conn_info->addr, event->mtu_size);
2057 __bt_update_mtu_gatt_device(conn_info->addr, event->mtu_size);
2059 param = g_variant_new("(isqy)",
2065 /* Send event to BT-API */
2066 _bt_send_event(BT_GATT_SERVER_EVENT,
2067 BLUETOOTH_EVENT_GATT_ATT_MTU_CHANGED,
2071 static void __bt_gatt_event_handler(int event_type, gpointer event_data)
2073 switch (event_type) {
2074 case OAL_EVENT_BLE_SERVER_INSTANCE_INITIALISED: {
2075 BT_INFO("OAL Event: Server Instance Registered");
2076 /* GATT Server Registered event is handled in MAIN thread context */
2077 __bt_handle_server_instance_registered((event_gatts_register_t *)event_data);
2080 case OAL_EVENT_GATTS_SERVICE_ADDED: {
2081 BT_INFO("OAL Event: GATT Service added");
2082 __bt_handle_gatt_server_service_added((event_gatts_srvc_prm_t *)event_data);
2085 case OAL_EVENT_GATTS_CHARACTERISTIC_ADDED: {
2086 BT_INFO("OAL Event: GATT characteristic added");
2087 __bt_handle_gatt_server_characteristic_added((event_gatts_srvc_charctr_t *)event_data);
2090 case OAL_EVENT_GATTS_DESCRIPTOR_ADDED: {
2091 BT_INFO("OAL Event: GATT descriptor added");
2092 __bt_handle_gatt_server_descriptor_added((event_gatts_srvc_descr_t *)event_data);
2095 case OAL_EVENT_GATTS_SERVICE_STARTED: {
2096 BT_INFO("OAL Event: GATT Service started");
2097 __bt_handle_gatt_server_service_started((event_gatts_srvc_t *)event_data);
2100 case OAL_EVENT_GATTS_SERVICE_STOPED: {
2101 BT_INFO("OAL Event: GATT Service stopped");
2102 __bt_handle_gatt_server_service_stopped((event_gatts_srvc_t *)event_data);
2105 case OAL_EVENT_GATTS_SERVICE_DELETED: {
2106 BT_INFO("OAL Event: GATT Service deleted");
2107 __bt_handle_gatt_server_service_deleted((event_gatts_srvc_t *) event_data);
2110 case OAL_EVENT_GATTS_CONNECTION_COMPLETED: {
2111 BT_INFO("OAL Event: GATT Server Connected");
2112 __bt_handle_gatt_server_connection_state((event_gatts_conn_t *)event_data);
2115 case OAL_EVENT_GATTS_DISCONNECTION_COMPLETED: {
2116 BT_INFO("OAL Event: GATT Server Disconnected");
2117 __bt_handle_gatt_server_disconnection_state((event_gatts_conn_t *)event_data);
2120 case OAL_EVENT_GATTS_REQUEST_READ: {
2121 BT_DBG("OAL Event: GATT Server Read Request");
2122 __bt_handle_gatt_server_read_requested((event_gatts_srvc_read_attr_t *)event_data);
2125 case OAL_EVENT_GATTS_REQUEST_WRITE: {
2126 BT_DBG("OAL Event: GATT Server Write Request");
2127 __bt_handle_gatt_server_write_requested((event_gatts_srvc_write_attr_t *)event_data);
2130 case OAL_EVENT_GATTS_REQUEST_ACQUIRE_WRITE: {
2131 BT_INFO("OAL Event: GATT Server Acquire Write Request");
2132 __bt_handle_gatt_server_acquire_write_requested((event_gatts_srvc_acquire_attr_t*)event_data);
2135 case OAL_EVENT_GATTS_REQUEST_ACQUIRE_NOTIFY: {
2136 BT_INFO("OAL Event: GATT ServerAcquire Notify Request");
2137 __bt_handle_gatt_server_acquire_notify_requested((event_gatts_srvc_acquire_attr_t*)event_data);
2140 case OAL_EVENT_GATTS_IND_CONFIRM: {
2141 BT_INFO("OAL Event: GATT Server Indication confirmed");
2142 __bt_handle_gatt_server_indicate_confirmed((event_gatts_ind_cnfrm_t *)event_data);
2145 case OAL_EVENT_GATTS_NOTIFICATION: { /* Tizen Platform Specific */
2146 BT_INFO("OAL Event: GATT Server DisConnected");
2147 __bt_handle_gatt_server_notification_changed((event_gatts_notif_t *)event_data);
2150 case OAL_EVENT_GATTS_MTU_CHANGED: {
2151 BT_INFO("OAL Event: GATT Server MTU changed event callback");
2152 __bt_handle_gatt_mtu_changed_event((event_gatts_mtu_changed_t *)event_data);
2155 #ifdef TIZEN_GATT_CLIENT
2156 case OAL_EVENT_GATTC_REGISTRATION: {
2157 BT_INFO("OAL Event: GATT Client instance Registered");
2158 __bt_handle_client_instance_registered((event_gattc_register_t *) event_data);
2161 case OAL_EVENT_GATTC_CONNECTION_COMPLETED: {
2162 BT_INFO("OAL Event: GATT Client Connected");
2163 __bt_handle_client_connected((event_gattc_conn_t *) event_data);
2166 case OAL_EVENT_GATTC_DISCONNECTION_COMPLETED: {
2167 BT_INFO("OAL Event: GATT Client DisConnected");
2168 __bt_handle_client_disconnected((event_gattc_conn_t *) event_data);
2171 case OAL_EVENT_GATTC_SERVICE_SEARCH_RESULT: {
2172 BT_DBG("OAL Event: GATT Client Service Search Result");
2173 __bt_handle_client_service_search_result((event_gattc_service_result_t *) event_data);
2176 case OAL_EVENT_GATTC_SERVICE_SEARCH_DONE: {
2177 BT_INFO("OAL Event: GATT Client Service Completed");
2178 __bt_handle_client_service_search_completed((event_gattc_conn_status_t *) event_data);
2181 case OAL_EVENT_GATTC_CHARAC_SERACH_RESULT: {
2182 BT_DBG("OAL Event: GATT Client Characteristic Search Result");
2183 __bt_handle_client_characteristic_search_result((event_gattc_characteristic_result_t *) event_data);
2186 case OAL_EVENT_GATTC_DESC_SERACH_RESULT: {
2187 BT_DBG("OAL Event: GATT Client Descriptor Search Result");
2188 __bt_handle_client_descriptor_search_result((event_gattc_descriptor_result_t *) event_data);
2191 case OAL_EVENT_GATTC_READ_CHARAC: {
2192 BT_DBG("OAL Event: GATT Client Characteristic Read Data");
2193 __bt_handle_client_characteristic_read_data((event_gattc_read_data *) event_data);
2196 case OAL_EVENT_GATTC_READ_DESCR: {
2197 BT_DBG("OAL Event: GATT Client Descriptor Read Data");
2198 __bt_handle_client_descriptor_read_data((event_gattc_read_data *) event_data);
2201 case OAL_EVENT_GATTC_WRITE_CHARAC: {
2202 BT_DBG("OAL Event: GATT Client Characteristic Write Data");
2203 __bt_handle_client_characteristic_write_data((event_gattc_write_data *) event_data);
2206 case OAL_EVENT_GATTC_WRITE_DESCR: {
2207 BT_DBG("OAL Event: GATT Client Descriptor Write Data");
2208 __bt_handle_client_descriptor_write_data((event_gattc_write_data *) event_data);
2211 case OAL_EVENT_DEVICE_LE_DISCONNECTED: {
2212 BT_INFO("OAL Event: LE device disconnected");
2213 __bt_hanlde_le_device_disconnection((event_dev_conn_status_t *)event_data);
2216 case OAL_EVENT_GATTC_NOTIFICATION_REGISTERED: {
2217 BT_INFO("OAL Event: GATT Client Notification Registered");
2218 __bt_handle_client_notification_registered((event_gattc_regdereg_notify_t *) event_data, TRUE);
2221 case OAL_EVENT_GATTC_NOTIFICATION_DEREGISTERED: {
2222 BT_INFO("OAL Event: GATT Client Notification Registered");
2223 __bt_handle_client_notification_registered((event_gattc_regdereg_notify_t *) event_data, FALSE);
2226 case OAL_EVENT_GATTC_NOTIFY_DATA: {
2227 BT_DBG("OAL Event: GATT Client Notification Data");
2228 __bt_handle_client_notification_data((event_gattc_notify_data *) event_data);
2231 case OAL_EVENT_GATTC_SERVICE_CHANGED_IND: {
2232 BT_INFO("OAL Event: GATT Client service changed indication");
2233 __bt_handle_client_service_changed_ind((event_gattc_service_changed_data *)event_data);
2236 case OAL_EVENT_GATTC_MTU_EXCHANGE_COMPLETED: {
2237 BT_INFO("OAL Event: GATT Client MTU Exchange Complete");
2238 __bt_handle_client_mtu_exchange_completed((event_gattc_mtu_configured_t *) event_data);
2243 BT_DBG("Unhandled OAL event = 0x%x", event_type);
2248 int _bt_gatt_server_add_service(char *sender, int service_type,
2249 int num_handles, char *svc_uuid, int instance_id)
2251 BT_CHECK_PARAMETER(svc_uuid, return);
2252 BT_CHECK_PARAMETER(sender, return);
2253 int ret = OAL_STATUS_SUCCESS;
2255 oal_gatt_srvc_id_t svc_data;
2257 svc_data.is_prmry = service_type;
2258 svc_data.id.inst_id = instance_id;
2260 BT_INFO("Service UUID [%s] Num handles [%d] Instance ID [%d]", svc_uuid, num_handles, instance_id);
2261 _bt_string_to_uuid(svc_uuid, (service_uuid_t*)&svc_data.id.uuid);
2263 ret = gatts_add_service(instance_id, &svc_data, num_handles);
2264 if (ret != OAL_STATUS_SUCCESS) {
2265 BT_ERR("ret: %d", ret);
2266 return _bt_convert_oal_status_to_bt_error(ret);
2269 return BLUETOOTH_ERROR_NONE;
2273 int _bt_gatt_server_add_included_service(char *sender, int instance_id,
2274 int service_handle, int included_svc_handle)
2276 BT_CHECK_PARAMETER(sender, return);
2277 int ret = OAL_STATUS_SUCCESS;
2279 ret = gatts_add_included_services(instance_id, service_handle, included_svc_handle);
2280 if (ret != OAL_STATUS_SUCCESS) {
2281 BT_ERR("ret: %d", ret);
2282 return _bt_convert_oal_status_to_bt_error(ret);
2284 return BLUETOOTH_ERROR_NONE;
2287 int _bt_gatt_server_add_characteristic(char *sender, char *char_uuid,
2288 bluetooth_gatt_server_attribute_params_t *param)
2290 BT_CHECK_PARAMETER(char_uuid, return);
2291 BT_CHECK_PARAMETER(sender, return);
2292 BT_CHECK_PARAMETER(param, return);
2293 int ret = OAL_STATUS_SUCCESS;
2295 oal_uuid_t uuid = {{0} };
2297 BT_INFO("Char UUID [%s] Instance ID [%d]", char_uuid, param->instance_id);
2298 _bt_string_to_uuid(char_uuid, (service_uuid_t*)&uuid);
2300 BT_INFO("Char permission From API [0x%x]", param->permissions);
2302 ret = gatts_add_characteristics(param->instance_id, param->service_handle, &uuid,
2303 param->properties, (int)param->permissions);
2304 if (ret != OAL_STATUS_SUCCESS) {
2305 BT_ERR("ret: %d", ret);
2306 return _bt_convert_oal_status_to_bt_error(ret);
2308 return BLUETOOTH_ERROR_NONE;
2311 int _bt_gatt_server_add_descriptor(char *sender, char *desc_uuid,
2312 bt_gatt_permission_t *param, int service_handle, int instance_id)
2314 BT_CHECK_PARAMETER(desc_uuid, return);
2315 BT_CHECK_PARAMETER(sender, return);
2316 BT_CHECK_PARAMETER(param, return);
2317 int ret = OAL_STATUS_SUCCESS;
2319 oal_uuid_t uuid = {{0} };
2321 BT_INFO("Descriptor UUID [%s] Instance ID [%d] Service handle [%d]",
2322 desc_uuid, service_handle, instance_id);
2324 _bt_string_to_uuid(desc_uuid, (service_uuid_t*)&uuid);
2326 BT_INFO("Descriptor permission From API [0x%x]", *param);
2327 ret = gatts_add_descriptor(instance_id, service_handle, &uuid, (int)*param);
2329 if (ret != OAL_STATUS_SUCCESS) {
2330 BT_ERR("ret: %d", ret);
2331 return _bt_convert_oal_status_to_bt_error(ret);
2333 return BLUETOOTH_ERROR_NONE;
2336 int _bt_gatt_server_start_service(char *sender, int service_handle, int instance_id)
2338 BT_CHECK_PARAMETER(sender, return);
2339 int ret = OAL_STATUS_SUCCESS;
2341 ret = gatts_start_service(instance_id, service_handle, BT_GATT_TRANSPORT_LE);
2342 if (ret != OAL_STATUS_SUCCESS) {
2343 BT_ERR("ret: %d", ret);
2344 return _bt_convert_oal_status_to_bt_error(ret);
2346 return BLUETOOTH_ERROR_NONE;
2349 int _bt_gatt_server_stop_service(char *sender, int service_handle, int instance_id)
2351 BT_CHECK_PARAMETER(sender, return);
2352 int ret = OAL_STATUS_SUCCESS;
2354 ret = gatts_stop_service(instance_id, service_handle);
2355 if (ret != OAL_STATUS_SUCCESS) {
2356 BT_ERR("ret: %d", ret);
2357 return _bt_convert_oal_status_to_bt_error(ret);
2359 return BLUETOOTH_ERROR_NONE;
2362 int _bt_gatt_server_delete_service(char *sender, int service_handle, int instance_id)
2364 BT_CHECK_PARAMETER(sender, return);
2365 int ret = OAL_STATUS_SUCCESS;
2369 ret = gatts_delete_service(instance_id, service_handle);
2370 if (ret != OAL_STATUS_SUCCESS) {
2371 BT_ERR("ret: %d", ret);
2372 return _bt_convert_oal_status_to_bt_error(ret);
2375 /* Remove the Service Handle */
2376 for (l = numapps[instance_id].service_handles; l != NULL;) {
2378 l = g_slist_next(l);
2379 if (handle && *handle == service_handle) {
2380 BT_INFO("Remove Service handle [%d]", *handle);
2381 numapps[instance_id].service_handles = g_slist_remove(numapps[instance_id].service_handles, handle);
2387 return BLUETOOTH_ERROR_NONE;
2390 int _bt_gatt_server_send_response(char *sender, bluetooth_gatt_att_data_t *data,
2391 bluetooth_gatt_server_response_params_t *param)
2393 BT_CHECK_PARAMETER(sender, return);
2394 BT_CHECK_PARAMETER(data, return);
2395 BT_CHECK_PARAMETER(param, return);
2396 struct gatt_server_req_info *req_info = NULL;
2397 int ret = OAL_STATUS_SUCCESS;
2399 oal_gatt_response_t response;
2401 BT_DBG("GATT Server Response: Req Type [%d] req_id [%d] status [%d] auth_req [%d] offset[%d] data len[%d]",
2402 param->req_type, param->request_id,
2403 param->response_status, param->auth_req,
2404 data->offset, data->length);
2406 /* Search for matching Request in List */
2407 req_info = __bt_gatt_server_find_request_info(param->request_id, param->req_type);
2409 BT_ERR("GATT Server Req Info not found for current response..return Error");
2410 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
2413 memset(&response, 0x00, sizeof(oal_gatt_response_t));
2415 response.handle = req_info->attribute_handle;
2416 response.attr_value.auth_req = param->auth_req;
2417 response.attr_value.handle = req_info->attribute_handle;
2418 response.attr_value.offset = data->offset;
2419 response.attr_value.len = data->length;
2420 memcpy(&response.attr_value.value, &data->data, data->length);
2423 ret = gatts_send_response(req_info->connection_id, param->request_id,
2424 param->response_status, &response);
2426 if (ret != OAL_STATUS_SUCCESS) {
2427 BT_ERR("ret: %d", ret);
2428 return _bt_convert_oal_status_to_bt_error(ret);
2431 /* Remove GATT server request from list */
2432 gatt_server_requests = g_slist_remove(gatt_server_requests, req_info);
2433 g_free(req_info->addr);
2435 return BLUETOOTH_ERROR_NONE;
2438 int _bt_gatt_server_acquire_send_response(char *sender, bluetooth_gatt_server_acquire_response_params_t *param , void *fd_list)
2440 BT_CHECK_PARAMETER(sender, return);
2441 BT_CHECK_PARAMETER(param, return);
2442 struct gatt_server_req_info *req_info = NULL;
2443 int ret = OAL_STATUS_SUCCESS;
2446 BT_INFO("GATT acquire Server Response: Req Type [%d] req_id [%d] fd [%d] mtu[%d]",
2447 param->req_type, param->request_id,
2451 /* Search for matching Request in List */
2452 req_info = __bt_gatt_server_find_request_info(param->request_id, param->req_type);
2454 BT_ERR("GATT acquire Server Req Info not found for current response..return Error");
2455 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
2458 ret = gatt_send_response_acquire(req_info->connection_id, param->request_id, 0, param->fd, param->mtu, fd_list);
2460 if (ret != OAL_STATUS_SUCCESS) {
2461 BT_ERR("ret: %d", ret);
2462 return _bt_convert_oal_status_to_bt_error(ret);
2465 /* Remove GATT server request from list */
2466 gatt_server_requests = g_slist_remove(gatt_server_requests, req_info);
2467 g_free(req_info->addr);
2469 return BLUETOOTH_ERROR_NONE;
2474 int _bt_gatt_server_send_indication(char *sender, bluetooth_device_address_t *dev_addr,
2475 bluetooth_gatt_att_data_t *data,
2476 bluetooth_gatt_server_indication_params_t *param)
2478 BT_CHECK_PARAMETER(sender, return);
2479 BT_CHECK_PARAMETER(data, return);
2480 BT_CHECK_PARAMETER(param, return);
2482 gboolean all_send = FALSE;
2483 int ret = OAL_STATUS_SUCCESS;
2484 struct gatt_client_info_t *conn;
2486 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2487 _bt_convert_addr_type_to_string(address, dev_addr->addr);
2489 if (memcmp(dev_addr->addr, BDADDR_ANY, 6) == 0) {
2490 BT_INFO("GATT Server: Send Indication to all connected GATT clients..");
2493 BT_INFO("GATT Server: Send Indication to connected GATT client addr [%s]", address);
2496 /* Attempt to send Notification/Indication to all Connected GATT clients */
2498 ret = __bt_gatt_send_indication_to_all_connected_clients(data, param);
2499 if (ret != OAL_STATUS_SUCCESS) {
2500 BT_ERR("ret: %d", ret);
2502 return _bt_convert_oal_status_to_bt_error(ret);
2506 conn = _bt_find_remote_gatt_client_info(address);
2508 ret = gatts_send_indication(param->instance_id, param->atrribute_handle,
2509 conn->connection_id, data->length,
2510 param->need_confirmation, (char *)(&data->data[0]));
2512 if (ret != OAL_STATUS_SUCCESS) {
2513 BT_ERR("ret: %d", ret);
2514 BT_INFO("Indication failed to send to Remote GATT Client [%s]", address);
2516 return _bt_convert_oal_status_to_bt_error(ret);
2518 BT_INFO("Indication sent to Remote GATT Client [%s] wait for Notification completed event from OAL", address);
2520 num_indicate_clients = 1;
2521 return BLUETOOTH_ERROR_NONE;
2523 BT_ERR("Remote GATT client [%s] is not connected..Cant send Indication!!", address);
2525 return BLUETOOTH_ERROR_NOT_CONNECTED;
2528 return BLUETOOTH_ERROR_NONE;
2531 int _bt_gatt_server_update_attribute_value(char *sender, int instance_id,
2532 bluetooth_gatt_server_update_value_t *param)
2534 BT_CHECK_PARAMETER(sender, return);
2535 BT_CHECK_PARAMETER(param, return);
2536 int ret = OAL_STATUS_SUCCESS;
2538 oal_gatt_value_t value;
2539 BT_DBG("GATT Server Update value: Instance ID [%d] attr handle [%d] Value len [%d]",
2540 instance_id, param->attribute_handle, param->length);
2542 memset(&value, 0x00, sizeof(oal_gatt_value_t));
2544 value.handle = param->attribute_handle;
2545 value.len = param->length;
2546 memcpy(&value.value, ¶m->data.data, param->length);
2548 ret = gatts_update_att_value(instance_id, &value);
2550 if (ret != OAL_STATUS_SUCCESS) {
2551 BT_ERR("ret: %d", ret);
2552 return _bt_convert_oal_status_to_bt_error(ret);
2555 return BLUETOOTH_ERROR_NONE;
2558 int _bt_request_att_mtu(bluetooth_device_address_t *device_address,
2561 struct gatt_server_info_t *conn_info = NULL;
2563 int ret = OAL_STATUS_SUCCESS;
2565 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
2566 _bt_convert_addr_type_to_string(addr, device_address->addr);
2568 /* Check if remote GATT Server is connected or not */
2569 conn_info = _bt_find_remote_gatt_server_info(addr);
2570 if (conn_info == NULL) {
2571 BT_ERR("GATT Server is not yet connected..");
2573 return BLUETOOTH_ERROR_NOT_CONNECTED;
2576 ret = gattc_configure_mtu(conn_info->connection_id, mtu);
2577 if (ret != OAL_STATUS_SUCCESS) {
2578 BT_ERR("ret: %d", ret);
2580 return _bt_convert_oal_status_to_bt_error(ret);
2584 return BLUETOOTH_ERROR_NONE;
2587 int _bt_get_att_mtu(bluetooth_device_address_t *address,
2590 BT_CHECK_PARAMETER(address, return);
2591 BT_CHECK_PARAMETER(mtu, return);
2592 struct gatt_client_info_t *conn_info = NULL;
2593 char addr[BT_ADDRESS_STRING_SIZE] = { 0 };
2594 int ret = OAL_STATUS_SUCCESS;
2597 _bt_convert_addr_type_to_string(addr, address->addr);
2599 BT_INFO("Get current MTU size for the remote client:DevAddress:[%s]", addr);
2601 conn_info = _bt_find_remote_gatt_client_info(addr);
2603 BT_INFO("GATT Client [%s] is connected, conn Id [%d] Instance ID [%d]",
2604 conn_info->addr, conn_info->connection_id, conn_info->instance_id);
2606 BT_ERR("GATT Client [%s] is not yet connected..", addr);
2607 return BLUETOOTH_ERROR_NOT_CONNECTED;
2610 if (conn_info->instance_id == -1) {
2611 ret = gattc_get_att_mtu(conn_info->connection_id, &stack_mtu);
2612 if (ret != OAL_STATUS_SUCCESS) {
2613 BT_ERR("ret: %d", ret);
2614 return _bt_convert_oal_status_to_bt_error(ret);
2617 ret = gatts_get_att_mtu(conn_info->connection_id, &stack_mtu);
2618 if (ret != OAL_STATUS_SUCCESS) {
2619 BT_ERR("ret: %d", ret);
2620 return _bt_convert_oal_status_to_bt_error(ret);
2624 BT_INFO("ATT MTU received from OAL [%d]", stack_mtu);
2625 *mtu = (unsigned int)stack_mtu;
2628 BT_ERR("MTU value is zero, GATT Client [%s] is not yet connected..", addr);
2629 return BLUETOOTH_ERROR_NOT_CONNECTED;
2632 return BLUETOOTH_ERROR_NONE;
2635 #ifdef TIZEN_GATT_CLIENT
2636 /* GATT Client utility static functions */
2637 static bt_gatt_service_info_list_t *__bt_get_service_info_list(int conn_id)
2640 bt_gatt_service_info_list_t *info = NULL;
2642 for (l = list_gatt_info; l != NULL; l = g_slist_next(l)) {
2643 info = (bt_gatt_service_info_list_t *)l->data;
2647 if (info->conn_id == conn_id)
2654 static bt_gatt_service_info_t *__bt_find_matching_service(
2655 bt_gatt_service_info_list_t *svc_list, oal_gatt_srvc_id_t *svc)
2658 bt_gatt_service_info_t *info = NULL;
2660 for (l = svc_list->services; l != NULL; l = g_slist_next(l)) {
2661 info = (bt_gatt_service_info_t *)l->data;
2665 /* Match UUID and instance ID */
2666 if (!memcmp(&svc->id.uuid.uuid, &info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
2667 && (svc->id.inst_id == info->inst_id)) {
2674 static bt_gatt_char_info_t *__bt_find_matching_charc(
2675 bt_gatt_service_info_t *svc_info, oal_gatt_id_t *charc)
2678 bt_gatt_char_info_t *info = NULL;
2680 for (l = svc_info->chars; l != NULL; l = g_slist_next(l)) {
2681 info = (bt_gatt_char_info_t *)l->data;
2685 /* Match UUID and instance ID */
2686 if (!memcmp(&charc->uuid.uuid, &info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
2687 && (charc->inst_id == info->inst_id)) {
2694 static bt_gatt_descriptor_info_t *__bt_find_matching_desc(
2695 bt_gatt_char_info_t *char_info, oal_gatt_id_t *desc)
2698 bt_gatt_descriptor_info_t *info = NULL;
2700 for (l = char_info->descs; l != NULL; l = g_slist_next(l)) {
2701 info = (bt_gatt_descriptor_info_t *)l->data;
2705 /* Match UUID and instance ID */
2706 if (!memcmp(&desc->uuid, &info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
2707 && (desc->inst_id == info->inst_id)) {
2715 static struct gatt_server_info_t *__bt_find_remote_gatt_server_info_from_conn_id(int conn_id)
2718 struct gatt_server_info_t *info = NULL;
2720 for (l = gatt_server_info_list; l != NULL; l = g_slist_next(l)) {
2721 info = (struct gatt_server_info_t*)l->data;
2725 if (info->connection_id == conn_id)
2731 static bt_gatt_service_info_t* __bt_find_removed_service(bt_gatt_service_info_list_t *svc_list)
2734 bt_gatt_service_info_t *info = NULL;
2736 for (l = svc_list->services; l != NULL; l = g_slist_next(l)) {
2737 info = (bt_gatt_service_info_t*)l->data;
2741 /* Service is marked a removed */
2742 if (info->is_removed == 1)
2748 static void __bt_remove_service_info_from_list(bt_gatt_service_info_t *svc_info)
2754 bt_gatt_char_info_t *charc = NULL;
2755 bt_gatt_included_service_info_t *incl = NULL;
2756 bt_gatt_descriptor_info_t *desc = NULL;
2758 /* Remove all Characteristic and Descriptors within characteristic */
2759 for (l = svc_info->chars; l != NULL;) {
2760 charc = (bt_gatt_char_info_t*)l->data;
2761 l = g_slist_next(l); /* Incase if l is removed, saving next to l */
2766 /* Inside Characteristic */
2767 for (l1 = charc->descs; l1 != NULL;) {
2769 desc = (bt_gatt_descriptor_info_t*)l1->data;
2770 l1 = g_slist_next(l1);
2775 /* Remove Descriptor */
2776 charc->descs = g_slist_remove(charc->descs, desc);
2779 /* Remove Characteristic */
2780 svc_info->chars = g_slist_remove(svc_info->chars, charc);
2784 /* Remove all Included Services */
2785 for (l2 = svc_info->included_svcs; l2 != NULL;) {
2786 incl = (bt_gatt_included_service_info_t*)l2->data;
2787 l2 = g_slist_next(l2); /* Incase if l is removed, saving next to l */
2792 /* Remove included service */
2793 svc_info->included_svcs = g_slist_remove(svc_info->included_svcs, incl);
2800 static void __bt_build_service_browse_info(int conn_id,
2801 bt_services_browse_info_t* info)
2804 bt_gatt_service_info_list_t *svc_info_list;
2805 bt_gatt_service_info_t *svc_info;
2807 service_uuid_t uuid;
2808 struct gatt_server_info_t *conn_info = NULL;
2810 char uuid_string[BLUETOOTH_UUID_STRING_MAX];
2812 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
2813 if (conn_info == NULL) {
2814 BT_ERR("Cant find connection Information");
2818 _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
2820 svc_info_list = __bt_get_service_info_list(conn_id);
2824 info->count = g_slist_length(svc_info_list->services);
2825 BT_DBG("Total services present in the svc info list for this conn id [%d] is [%d]",
2826 conn_id, info->count);
2828 for (l = svc_info_list->services; l != NULL; l = g_slist_next(l)) {
2829 svc_info = (bt_gatt_service_info_t*)l->data;
2830 if (svc_info == NULL)
2833 memcpy(&uuid.uuid, &svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2834 _bt_uuid_to_string(&uuid, uuid_string);
2836 BT_INFO("[%d] %s [%s]", count, uuid_string, _bt_convert_uuid_to_string(uuid_string));
2838 /* Fill UUID of service */
2839 g_strlcpy(info->uuids[count], uuid_string,
2840 BLUETOOTH_UUID_STRING_MAX);
2842 /* Fill instance ID of service */
2843 info->inst_id[count] = svc_info->inst_id;
2845 /* Fill primary service or not info */
2846 info->primary[count] = svc_info->is_primary;
2848 /* Increment count of services browsed */
2853 static void __bt_build_char_browse_info(int conn_id,
2854 bt_gatt_service_info_t *svc_info,
2855 bt_char_browse_info_t* info)
2858 bt_gatt_char_info_t *char_info;
2859 service_uuid_t uuid;
2861 struct gatt_server_info_t *conn_info = NULL;
2863 char uuid_string[BLUETOOTH_UUID_STRING_MAX];
2865 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
2866 if (conn_info == NULL) {
2867 BT_ERR("Cant find connection Information");
2871 /* Fill default data, this will be required even in case of failure */
2872 _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
2873 memcpy(&info->svc_uuid, svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2874 info->svc_inst_id = svc_info->inst_id;
2876 if (!svc_info->chars) {
2877 BT_ERR("No Chars browsed for address [%s]", conn_info->addr);
2881 info->count = g_slist_length(svc_info->chars);
2883 for (l = svc_info->chars; l != NULL; l = g_slist_next(l)) {
2884 char_info = (bt_gatt_char_info_t*)l->data;
2885 if (char_info == NULL)
2888 memcpy(&uuid.uuid, &char_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2889 _bt_uuid_to_string(&uuid, uuid_string);
2891 /* Fill UUID of characteristic */
2892 g_strlcpy(info->uuids[count], uuid_string,
2893 BLUETOOTH_UUID_STRING_MAX);
2895 /* Fill instance ID of characteristic */
2896 info->inst_id[count] = char_info->inst_id;
2898 /* Fill property of characteristic */
2899 info->props[count] = char_info->props;
2901 /* Increment count of services browsed */
2904 BT_DBG("Total characteristics browsed [%d]", count);
2907 static void __bt_build_descriptor_browse_info(int conn_id,
2908 bt_gatt_service_info_t *svc_info,
2909 bt_gatt_char_info_t *char_info,
2910 bt_descriptor_browse_info_t* info)
2913 bt_gatt_descriptor_info_t *desc_info;
2915 service_uuid_t uuid;
2916 struct gatt_server_info_t *conn_info = NULL;
2918 char uuid_string[BLUETOOTH_UUID_STRING_MAX];
2920 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
2922 /* Fill default data, this will be required even in case of failure */
2923 _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
2924 memcpy(&info->svc_uuid, svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2925 info->svc_inst_id = svc_info->inst_id;
2926 memcpy(&info->char_uuid, char_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2927 info->char_inst_id = char_info->inst_id;
2929 /* Fill property of the parent characteristic of this descriptor */
2930 info->char_props_map = char_info->props;
2932 info->count = g_slist_length(char_info->descs);
2934 if (!char_info->descs) {
2935 BT_ERR("No Descriptors browsed for address [%s]", conn_info->addr + 12);
2939 for (l = char_info->descs; l != NULL; l = g_slist_next(l)) {
2940 desc_info = (bt_gatt_descriptor_info_t*)l->data;
2941 if (desc_info == NULL)
2944 memcpy(&uuid.uuid, &desc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2945 _bt_uuid_to_string(&uuid, uuid_string);
2947 /* Fill UUID of Descriptor */
2948 g_strlcpy(info->uuids[count], uuid_string,
2949 BLUETOOTH_UUID_STRING_MAX);
2951 /* Fill instance ID of Descriptor */
2952 info->inst_id[count] = desc_info->inst_id;
2955 /* Increment count of Descriptor browsed */
2959 BT_INFO("Total descriptors browsed [%d]", count);
2962 static void __bt_free_service_info(bt_gatt_service_info_t *svc)
2964 GSList *ll, *lll, *llll;
2965 bt_gatt_char_info_t *chr = NULL;
2966 bt_gatt_descriptor_info_t *desc = NULL;
2967 bt_gatt_included_service_info_t *incl_svc = NULL;
2969 BT_DBG("Service info Is Prim[%d] Inst ID [%d]", svc->is_primary, svc->inst_id);
2970 /* Delete all chars and its descriptors */
2971 for (ll = svc->chars; ll != NULL; ) {
2972 chr = (bt_gatt_char_info_t *)ll->data;
2973 ll = g_slist_next(ll);
2977 for (lll = chr->descs; lll != NULL; ) {
2978 desc = (bt_gatt_descriptor_info_t *)lll->data;
2979 lll = g_slist_next(lll);
2982 chr->descs = g_slist_remove(chr->descs, desc);
2985 svc->chars = g_slist_remove(svc->chars, chr);
2989 /* Delete all included services */
2990 for (llll = svc->included_svcs; llll != NULL; ) {
2991 incl_svc = (bt_gatt_included_service_info_t *)llll->data;
2992 llll = g_slist_next(llll);
2993 if (incl_svc == NULL)
2995 svc->included_svcs = g_slist_remove(svc->included_svcs, incl_svc);
3000 static void __bt_cleanup_remote_services(struct gatt_server_info_t *conn_info)
3002 bt_gatt_service_info_list_t *svc_info_list = NULL;
3003 bt_gatt_service_info_t *svc = NULL;
3006 svc_info_list = __bt_get_service_info_list(conn_info->connection_id);
3007 if (!svc_info_list) {
3008 BT_INFO("Could not find Svc Info list for the connection ID [%d]",
3009 conn_info->connection_id);
3013 BT_INFO("Start Cleanup of all services. Num Services [%d]", g_slist_length(svc_info_list->services));
3014 for (l = svc_info_list->services; l != NULL; ) {
3015 svc = (bt_gatt_service_info_t *)l->data;
3016 l = g_slist_next(l);
3020 __bt_free_service_info(svc);
3021 svc_info_list->services = g_slist_remove(svc_info_list->services, svc);
3025 list_gatt_info = g_slist_remove(list_gatt_info, svc_info_list);
3026 g_free(svc_info_list);
3029 int _bt_register_gatt_client_instance(const char *sender,
3030 bluetooth_device_address_t *address)
3032 int ret = OAL_STATUS_SUCCESS;
3033 char *uuid_string = NULL;
3038 /* App should ensure that it should not send */
3039 BT_INFO("Check on which instance GATT Client instance can be initialized....");
3040 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
3041 if (numapps[k].is_initialized == 1) {
3042 BT_DBG("Instance ID [%d] is already in use..Check next slot",
3043 numapps[k].instance_id);
3046 BT_DBG("Time to register GATT client instancer..UUID to be used is [%s] slot [%d]",
3047 uuid_list[slot-1], slot);
3053 BT_ERR("No Slot if free for GATT Client registration..");
3054 return BLUETOOTH_ERROR_REGISTRATION_FAILED;
3057 uuid_string = g_malloc0(BT_UUID_STRING_MAX);
3058 _bt_string_to_uuid(uuid_list[slot-1], (service_uuid_t*)&uuid);
3059 g_strlcpy(uuid_string, uuid_list[slot-1], BT_UUID_STRING_MAX);
3060 BT_INFO("Copied UUID string [%s] slot [%d]", uuid_string, slot);
3062 /* Register GATT Client */
3063 ret = gattc_register(&uuid);
3064 if (ret != OAL_STATUS_SUCCESS) {
3065 BT_ERR("ret: %d", ret);
3066 g_free(uuid_string);
3067 return _bt_convert_oal_status_to_bt_error(ret);
3070 BT_DBG("GATT Client registration call successfully accepted by OAL..wait for Instance Initialized event from OAL..");
3072 /* Return & wait for GATT Client Instance Initialization event */
3073 memset(numapps[slot].sender, 0x00, sizeof(numapps[slot].sender));
3074 memset(numapps[slot].uuid, 0x00, sizeof(numapps[slot].uuid));
3076 g_strlcpy(numapps[slot].sender, sender, sizeof(numapps[slot].sender));
3077 g_strlcpy(numapps[slot].uuid, uuid_string, sizeof(numapps[slot].uuid));
3079 /* Address is saved here. When event comes, sender + address are matched for replying pending
3080 request. It is impossible for same sender to have requests with two same addresses */
3081 memcpy(&numapps[slot].address.addr, address->addr, sizeof(bluetooth_device_address_t));
3083 numapps[slot].is_initialized = TRUE; /* Set initialization to true here itself */
3085 g_free(uuid_string);
3086 return BLUETOOTH_ERROR_NONE;
3092 /* GATT client events */
3093 static void __bt_handle_client_instance_registered(event_gattc_register_t *data)
3095 bt_service_app_info_t *info = NULL;
3097 char *uuid_string = g_malloc0(BT_UUID_STRING_MAX);
3099 _bt_uuid_to_string(&(data->client_uuid), uuid_string);
3100 BT_INFO("Client ID is Initialized [%d] UUID initialized [%s]", data->client_if, uuid_string);
3102 /* Platform GATT client framwork does not use Default GATT client instance
3103 This GATT client instance is never deregistred in the lifetime of bt-service */
3104 if (g_strcmp0(uuid_string, DEFAULT_GATT_CLIENT_UUID) == 0) {
3105 BT_INFO("Default client Instance Registered");
3106 gatt_default_client = data->client_if;
3107 g_free(uuid_string);
3111 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
3114 if (g_strcmp0(info->uuid, uuid_string) == 0) {
3115 BT_INFO("Found GATT client.. sender [%s] Slot [%d] occupied", info->sender, k);
3116 info->is_initialized = TRUE;
3117 info->client_id = data->client_if;
3118 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE, BT_GATT_CLIENT_REGISTER,
3119 (void*)info, sizeof(bt_service_app_info_t));
3123 g_free(uuid_string);
3126 static void __bt_handle_client_connected(event_gattc_conn_t *event_data)
3128 int result = BLUETOOTH_ERROR_NONE;
3129 struct gatt_server_info_t *conn_info = NULL;
3130 struct gatt_out_conn_info_t *out_conn_info = NULL;
3132 GVariant *param = NULL;
3134 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
3135 _bt_convert_addr_type_to_string(address,
3136 (unsigned char *)event_data->address.addr);
3138 if (event_data->status != OAL_STATUS_SUCCESS)
3139 result = BLUETOOTH_ERROR_INTERNAL;
3141 /* DBUS Return fo BT_CONNECT_LE for all the apps */
3142 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE, address,
3143 BT_ADDRESS_STRING_SIZE);
3145 BT_INFO("Local GATT Client Connected: Remote addr[%s] Client Interface [%d] Connection ID [%d] status[%d]",
3146 address, event_data->client_if, event_data->conn_id, event_data->status);
3148 if (result == BLUETOOTH_ERROR_NONE) {
3149 /* Check if device is already in connected list */
3150 conn_info = _bt_find_remote_gatt_server_info(address);
3153 /* Send event to BT-API */
3154 param = g_variant_new("(is)", result, address);
3155 _bt_send_event(BT_DEVICE_EVENT,
3156 BLUETOOTH_EVENT_GATT_CLIENT_CONNECTED, /* Local device is GATT client */
3159 /* Save Connection info */
3160 conn_info = g_new0(struct gatt_server_info_t, 1);
3161 conn_info->addr = g_strdup(address);
3162 conn_info->client_id = event_data->client_if;
3163 #ifdef __INTEGRATE_GATT_INFO__
3164 conn_info->instance_id = -1;
3166 conn_info->connection_id = event_data->conn_id;
3167 gatt_server_info_list = g_slist_append(gatt_server_info_list, conn_info);
3168 BT_DBG("Total num of connected Remote GATT server devices [%d]",
3169 g_slist_length(gatt_server_info_list));
3172 BT_INFO("Do a Internal refresh");
3173 if (OAL_STATUS_SUCCESS != gattc_refresh(conn_info->client_id, &event_data->address))
3174 BT_ERR("GATT database refresh failed!!");
3176 BT_INFO("GATT database refresh Success!!");
3179 BT_ERR("Local GATT Client connected event for addr[%s], but device is in connected list already", address);
3181 __bt_add_mtu_gatt_device(address);
3183 BT_ERR("GATT Client Connection failed!!");
3185 /* If outgoing connection Info is present, then remove it */
3186 out_conn_info = __bt_find_gatt_outgoing_conn_info(address);
3187 if (out_conn_info) {
3188 BT_ERR("Outgoing Client connect request was sent");
3189 outgoing_gatt_conn_list = g_slist_remove(outgoing_gatt_conn_list, out_conn_info);
3190 g_free(out_conn_info->addr);
3191 g_free(out_conn_info);
3196 static void __bt_handle_client_disconnected(event_gattc_conn_t *event_data)
3198 int result = BLUETOOTH_ERROR_NONE;
3200 struct gatt_server_info_t *conn_info = NULL;
3201 struct gatt_out_conn_info_t *out_conn_info = NULL;
3203 GVariant *param = NULL;
3205 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
3206 _bt_convert_addr_type_to_string(address,
3207 (unsigned char *)event_data->address.addr);
3209 if (event_data->status != OAL_STATUS_SUCCESS)
3210 result = BLUETOOTH_ERROR_INTERNAL;
3212 if (NULL == _bt_get_request_info_data(BT_DISCONNECT_LE, address)) {
3213 if (NULL != _bt_get_request_info_data(BT_CONNECT_LE, address)) {
3214 result = BLUETOOTH_ERROR_INTERNAL;
3215 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
3216 address, BT_ADDRESS_STRING_SIZE);
3217 BT_ERR("Failed to connect Local GATT Remote addr[%s]", address);
3222 /* DBUS Return for BT_DISCONNECT_LE for all the apps */
3223 __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE, address,
3224 BT_ADDRESS_STRING_SIZE);
3226 BT_INFO("Local GATT Client DisConnected: Remote addr[%s] Client Interface [%d] Connection ID [%d] status [%d]",
3227 address + 12, event_data->client_if, event_data->conn_id, event_data->status);
3229 /* Remove Connection info */
3230 conn_info = _bt_find_remote_gatt_server_info(address);
3233 param = g_variant_new("(is)", result, address);
3234 /* Send event to application */
3235 _bt_send_event(BT_DEVICE_EVENT,
3236 BLUETOOTH_EVENT_GATT_CLIENT_DISCONNECTED,
3239 BT_INFO("Remove GATT server info from List..");
3240 /* Remove all services from info list_gatt_info */
3241 __bt_cleanup_remote_services(conn_info);
3243 /* Remove info from List */
3244 gatt_server_info_list = g_slist_remove(gatt_server_info_list, conn_info);
3245 BT_INFO("Total num of connected GATT servers [%d]", g_slist_length(gatt_server_info_list));
3246 g_free(conn_info->addr);
3249 BT_INFO("Can not find conn info, already removed!");
3251 __bt_remove_mtu_gatt_device(address);
3253 /* If outgoing connection Info is present, then remove it */
3254 out_conn_info = __bt_find_gatt_outgoing_conn_info(address);
3255 if (out_conn_info) {
3256 BT_ERR("Client Disconnected event, but outgoing connect request was sent");
3257 outgoing_gatt_conn_list = g_slist_remove(outgoing_gatt_conn_list, out_conn_info);
3258 g_free(out_conn_info->addr);
3259 g_free(out_conn_info);
3265 static void __bt_handle_client_service_search_result(
3266 event_gattc_service_result_t *event_data)
3268 /* Pre: status is never fail from OAL */
3270 /* Find service list from address */
3271 bt_gatt_service_info_list_t *svc_info_list;
3272 bt_gatt_service_info_t *svc_info;
3274 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3275 if (!svc_info_list) {
3276 BT_DBG("Service info list not present for connection ID %d, means first time browse", event_data->conn_status.conn_id);
3277 /* Means for this conn_id, no services are ever browsed, first time,
3278 create service info list for this conn_id */
3279 svc_info_list = g_malloc0(sizeof(bt_gatt_service_info_list_t));
3280 svc_info_list->conn_id = event_data->conn_status.conn_id;
3281 list_gatt_info = g_slist_append(list_gatt_info, svc_info_list);
3284 /* send list and current service's uuid and instance id to find it */
3285 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3286 /* If not found, check if service changed, if yes, means this is a new service added
3287 in remote GATT device, update uuid info in svc info list structure, to be used when
3288 search is completed */
3290 if (svc_info_list->info.is_changed) {
3291 BT_DBG("Service Changed indication already found for connection ID %d", event_data->conn_status.conn_id);
3292 memcpy(svc_info_list->info.uuid, event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3294 /* Create and add new service in service list */
3295 svc_info = g_malloc0(sizeof(bt_gatt_service_info_t));
3296 memcpy(svc_info->uuid, event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3297 svc_info->inst_id = event_data->srvc_id.id.inst_id;
3298 svc_info->is_primary = event_data->srvc_id.is_prmry;
3299 svc_info_list->services = g_slist_append(svc_info_list->services, svc_info);
3300 BT_DBG("Service created and added in Svc info list or connection ID %d", event_data->conn_status.conn_id);
3302 /* If returned matching service info, then just update service_rmeoved value inside it to 0 */
3303 svc_info->is_removed = 0;
3307 static void __bt_handle_client_service_search_completed(
3308 event_gattc_conn_status_t *event_data)
3310 struct gatt_server_info_t *conn_info = NULL;
3311 bt_gatt_service_info_list_t *svc_info_list;
3312 bt_gatt_service_info_t *svc_info;
3313 bt_services_browse_info_t browse_info;
3314 unsigned char uuid_empty[BLUETOOTH_UUID_HEX_MAX_LEN];
3316 memset(&uuid_empty, 0x00, BLUETOOTH_UUID_HEX_MAX_LEN);
3317 memset(&browse_info, 0x00, sizeof(bt_services_browse_info_t));
3318 BT_INFO("Primary Services browsing completed status[%d] conn ID [%d]",
3319 event_data->status, event_data->conn_id);
3321 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(event_data->conn_id);
3323 svc_info_list = __bt_get_service_info_list(event_data->conn_id);
3324 if (!svc_info_list) {
3325 BT_ERR("No services browsed ever for addr [%s]", conn_info->addr);
3327 /* Just build response and return ERROR */
3328 __bt_build_service_browse_info(event_data->conn_id, &browse_info);
3330 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_INTERNAL,
3331 BT_GATT_GET_PRIMARY_SERVICES, &browse_info,
3332 sizeof(bt_services_browse_info_t));
3336 /* If fail, then send event with error */
3337 if (event_data->status != OAL_STATUS_SUCCESS) {
3338 /* Just build response and return ERROR */
3339 __bt_build_service_browse_info(event_data->conn_id, &browse_info);
3341 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_INTERNAL,
3342 BT_GATT_GET_PRIMARY_SERVICES, &browse_info,
3343 sizeof(bt_services_browse_info_t));
3347 /* If success, then find service info list from address */
3349 /* If svc_changed == 1 and uuid valid, means a new service is added*/
3350 if (svc_info_list->info.is_changed && !memcmp(uuid_empty, svc_info_list->info.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)) {
3351 /* TODO: Send event -Service added with instance ID and UUID of newly added service */
3352 BT_INFO("new service added");
3354 BT_INFO("TODO new service added");
3357 /* If svc_changed == 1 and uuid invalid, then a service is removed */
3358 if (svc_info_list->info.is_changed && memcmp(uuid_empty, svc_info_list->info.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)) {
3359 /* Scan through the service info list to find service with is_removed = 1*/
3360 svc_info = __bt_find_removed_service(svc_info_list);
3362 /* TODO Send event - Service removed with instance ID and UUID of just rmeoved service */
3364 /* Remove that service info from service info list */
3365 svc_info_list->services = g_slist_remove(svc_info_list->services, svc_info);
3367 /* Delete that service completely from svc_info list*/
3368 __bt_remove_service_info_from_list(svc_info);
3372 /* Reset svc_changed = 0, and reset UUID = all 0's */
3373 svc_info_list->info.is_changed = 0;
3374 memset(&svc_info_list->info.uuid, 0x00, BLUETOOTH_UUID_HEX_MAX_LEN);
3376 /* Build Reply and send to service browse primary services request of pending apps */
3377 __bt_build_service_browse_info(event_data->conn_id, &browse_info);
3379 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE,
3380 BT_GATT_GET_PRIMARY_SERVICES, &browse_info,
3381 sizeof(bt_services_browse_info_t));
3385 static void __bt_handle_client_characteristic_search_result(
3386 event_gattc_characteristic_result_t *event_data)
3388 bt_gatt_service_info_list_t *svc_info_list;
3389 bt_gatt_service_info_t *svc_info;
3390 bt_gatt_char_info_t *char_info;
3391 bt_char_browse_info_t browse_info;
3393 memset(&browse_info, 0x00, sizeof(bt_char_browse_info_t));
3396 if (event_data->conn_status.status == OAL_STATUS_SUCCESS) {
3397 /* Find service info list from address */
3398 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3399 if (svc_info_list == NULL) {
3400 BT_ERR("svc_info_list is NULL");
3404 /* Find matching service info from svc info list */
3405 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3406 if (svc_info == NULL) {
3407 BT_ERR("svc_info is NULL");
3411 /* Find Matching char from service info in event */
3412 char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
3413 /* If not found, then add new characteristic and return */
3415 BT_DBG("Add new characteristic");
3416 char_info = g_malloc0(sizeof(bt_gatt_char_info_t));
3417 memcpy(char_info->uuid, event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3419 char_info->inst_id = event_data->char_id.inst_id;
3420 char_info->props = event_data->char_prop;
3421 svc_info->chars = g_slist_append(svc_info->chars, char_info);
3423 /* If found, then return */
3424 BT_DBG("update char property as Characteristic browsed is already present");
3425 char_info->props |= event_data->char_prop;
3428 /* If Not success: Means Charc browse is completed */
3429 /* Find char list from service in event */
3430 /* Find service list from address */
3431 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3432 if (svc_info_list == NULL) {
3433 BT_ERR("svc_info_list is NULL");
3437 /* Find service info from service in event */
3438 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3439 if (svc_info == NULL) {
3440 BT_ERR("svc_info is NULL");
3444 /* Build char list from service in event */
3445 __bt_build_char_browse_info(event_data->conn_status.conn_id,
3446 svc_info, &browse_info);
3448 /* Create response and return by sending event*/
3449 /* Build Reply and send to service browse All Included services request of pending apps */
3450 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE,
3451 BT_GATT_GET_SERVICE_PROPERTIES,
3453 sizeof(bt_char_browse_info_t));
3457 static void __bt_handle_client_descriptor_search_result(
3458 event_gattc_descriptor_result_t *event_data)
3460 bt_gatt_service_info_list_t *svc_info_list;
3461 bt_gatt_service_info_t *svc_info;
3462 bt_gatt_char_info_t *char_info;
3463 bt_gatt_descriptor_info_t *desc_info;
3464 bt_descriptor_browse_info_t browse_info;
3466 BT_DBG("descriptor search result status [%d]", event_data->conn_status.status);
3468 memset(&browse_info, 0x00, sizeof(bt_descriptor_browse_info_t));
3471 if (event_data->conn_status.status == OAL_STATUS_SUCCESS) {
3472 /* Find service list from address */
3473 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3474 if (svc_info_list == NULL) {
3475 BT_ERR("svc_info_list is NULL");
3479 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3480 if (svc_info == NULL) {
3481 BT_ERR("svc_info is NULL");
3485 char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
3486 if (char_info == NULL) {
3487 BT_ERR("char_info is NULL");
3491 desc_info = __bt_find_matching_desc(char_info, &event_data->descr_id);
3492 /* If not found, add new descriptor and return */
3494 desc_info = g_malloc0(sizeof(bt_gatt_descriptor_info_t));
3495 memcpy(desc_info->uuid, event_data->descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3497 desc_info->inst_id = event_data->descr_id.inst_id;
3498 char_info->descs = g_slist_append(char_info->descs, desc_info);
3501 /* If found, then return */
3502 BT_DBG("Descriptor browsed is already presesnt");
3505 /* If Not success */
3506 /* Find service list from address */
3507 /* Find included service list from service in event */
3508 /* Create response and return by sending event*/
3509 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3510 if (svc_info_list == NULL) {
3511 BT_ERR("svc_info_list is NULL");
3515 /* Find service info from service in event */
3516 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3517 if (svc_info == NULL) {
3518 BT_ERR("svc_info is NULL");
3522 /* Find char info from char in event */
3523 char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
3524 if (char_info == NULL) {
3525 BT_ERR("char_info is NULL");
3529 /* Build descriptor list from char in event */
3530 __bt_build_descriptor_browse_info(event_data->conn_status.conn_id,
3531 svc_info, char_info, &browse_info);
3534 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE,
3535 BT_GATT_GET_CHARACTERISTIC_PROPERTIES,
3537 sizeof(bt_descriptor_browse_info_t));
3541 static void __bt_handle_client_characteristic_read_data(
3542 event_gattc_read_data *event_data)
3544 int result = BLUETOOTH_ERROR_NONE;
3545 struct gatt_server_info_t *conn_info = NULL;
3546 bluetooth_gatt_client_char_prop_info_t read_info;
3548 /* Read Information data structures */
3549 GVariant *param = NULL;
3550 GVariant *data = NULL;
3551 GVariant *data_svc_uuid = NULL;
3552 GVariant *data_char_uuid = NULL;
3553 char *read_val = NULL;
3554 char *svc_uuid = NULL;
3555 char *char_uuid = NULL;
3558 //memset(&read_info, 0x00, sizeof(bt_gatt_handle_property_t));
3559 memset(&read_info, 0x00, sizeof(bluetooth_gatt_client_char_prop_info_t));
3561 /* Extract Address from conn_id of event data */
3562 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3563 event_data->uuid_status.conn_status.conn_id);
3565 BT_INFO("Characteristic Read result from addr [%s] status [%d]",
3566 conn_info->addr, event_data->uuid_status.conn_status.status);
3568 /* Fill char in buffer */
3569 memcpy(&read_info.characteristic.uuid,
3570 event_data->uuid_status.char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3571 read_info.characteristic.instance_id = event_data->uuid_status.char_id.inst_id;
3573 /* Fill Service in buffer */
3574 memcpy(&read_info.svc.uuid,
3575 event_data->uuid_status.srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3576 read_info.svc.instance_id = event_data->uuid_status.srvc_id.id.inst_id;
3578 /* Fill remote device address */
3579 _bt_convert_addr_string_to_type(read_info.device_address.addr, conn_info->addr);
3581 /* Fill data and reply to all apps waiting for Read result on the same characteristic
3582 Note: Even in case of failure, address, handles and result code should be returned */
3583 if (event_data->uuid_status.conn_status.status != OAL_STATUS_SUCCESS)
3584 result = BLUETOOTH_ERROR_INTERNAL;
3586 if (event_data->data_len > 0) {
3588 // for (i = 0; i < event_data->data_len; i++)
3589 // BT_INFO("Data[%d] = [0x%x]", i, event_data->data[i]);
3592 read_val = g_memdup(&event_data->data[0], event_data->data_len);
3594 data = g_variant_new_from_data(
3595 G_VARIANT_TYPE_BYTESTRING,
3597 event_data->data_len,
3600 BT_ERR("Characteristic Read success, but no data!!!");
3602 data = g_variant_new_from_data(
3603 G_VARIANT_TYPE_BYTESTRING,
3610 svc_uuid = g_memdup(&event_data->uuid_status.srvc_id.id.uuid.uuid[0], uuid_len);
3612 data_svc_uuid = g_variant_new_from_data(
3613 G_VARIANT_TYPE_BYTESTRING,
3619 char_uuid = g_memdup(&event_data->uuid_status.char_id.uuid.uuid[0], uuid_len);
3621 data_char_uuid = g_variant_new_from_data(
3622 G_VARIANT_TYPE_BYTESTRING,
3627 param = g_variant_new("(isn@ayin@ayin@ay)", result,
3631 event_data->uuid_status.srvc_id.id.inst_id,
3634 event_data->uuid_status.char_id.inst_id,
3635 event_data->data_len,
3639 _bt_send_event(BT_GATT_CLIENT_EVENT,
3640 BLUETOOTH_EVENT_GATT_READ_CHAR,
3643 /* Send DBUS return */
3644 __bt_gatt_handle_pending_request_info(result,
3645 BT_GATT_READ_CHARACTERISTIC,
3647 sizeof(bluetooth_gatt_client_char_prop_info_t));
3658 static void __bt_handle_client_descriptor_read_data(
3659 event_gattc_read_data *event_data)
3661 int result = BLUETOOTH_ERROR_NONE;
3662 struct gatt_server_info_t *conn_info = NULL;
3663 bluetooth_gatt_client_desc_prop_info_t read_info;
3665 /* Read Information data structures */
3666 GVariant *param = NULL;
3667 GVariant *data = NULL;
3668 GVariant *data_svc_uuid = NULL;
3669 GVariant *data_char_uuid = NULL;
3670 GVariant *data_desc_uuid = NULL;
3671 char *read_val = NULL;
3672 char *svc_uuid = NULL;
3673 char *char_uuid = NULL;
3674 char *desc_uuid = NULL;
3678 memset(&read_info, 0x00, sizeof(bluetooth_gatt_client_desc_prop_info_t));
3680 /* Extract Address from conn_id of event data */
3681 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3682 event_data->uuid_status.conn_status.conn_id);
3684 BT_INFO("Descriptor Read result from addr [%s] status [%d]",
3685 conn_info->addr, event_data->uuid_status.conn_status.status);
3687 /* Fill descriptor informations in buffer */
3688 memcpy(&read_info.descriptor.uuid,
3689 event_data->uuid_status.descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3690 read_info.descriptor.instance_id = event_data->uuid_status.descr_id.inst_id;
3692 /* Fill Characteristic informations in buffer */
3693 memcpy(&read_info.characteristic.uuid,
3694 event_data->uuid_status.char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3695 read_info.characteristic.instance_id = event_data->uuid_status.char_id.inst_id;
3697 /* Fill Service informations in buffer */
3698 memcpy(&read_info.svc.uuid,
3699 event_data->uuid_status.srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3700 read_info.svc.instance_id = event_data->uuid_status.srvc_id.id.inst_id;
3702 /* Fill remote device address */
3703 _bt_convert_addr_string_to_type(read_info.device_address.addr, conn_info->addr);
3705 /* Fill data and reply to all apps waiting for Read result on the same characteristic */
3706 if (event_data->uuid_status.conn_status.status != OAL_STATUS_SUCCESS)
3707 result = BLUETOOTH_ERROR_INTERNAL;
3709 BT_INFO("desc data len:%d", event_data->data_len);
3710 if (event_data->data_len > 0) {
3712 for (i = 0; i < event_data->data_len; i++)
3713 BT_DBG("Data[%d] = [0x%x]", i, event_data->data[i]);
3716 read_val = g_memdup(&event_data->data[0], event_data->data_len);
3718 data = g_variant_new_from_data(
3719 G_VARIANT_TYPE_BYTESTRING,
3721 event_data->data_len,
3724 BT_INFO("Descriptor Read success, but no data!!!");
3726 data = g_variant_new_from_data(
3727 G_VARIANT_TYPE_BYTESTRING,
3733 svc_uuid = g_memdup(&event_data->uuid_status.srvc_id.id.uuid.uuid[0], uuid_len);
3735 data_svc_uuid = g_variant_new_from_data(
3736 G_VARIANT_TYPE_BYTESTRING,
3742 char_uuid = g_memdup(&event_data->uuid_status.char_id.uuid.uuid[0], uuid_len);
3744 data_char_uuid = g_variant_new_from_data(
3745 G_VARIANT_TYPE_BYTESTRING,
3751 desc_uuid = g_memdup(&event_data->uuid_status.descr_id.uuid.uuid[0], uuid_len);
3753 data_desc_uuid = g_variant_new_from_data(
3754 G_VARIANT_TYPE_BYTESTRING,
3759 param = g_variant_new("(isn@ayin@ayin@ayin@ay)", result,
3763 event_data->uuid_status.srvc_id.id.inst_id,
3766 event_data->uuid_status.char_id.inst_id,
3769 event_data->uuid_status.descr_id.inst_id,
3770 event_data->data_len,
3774 _bt_send_event(BT_GATT_CLIENT_EVENT,
3775 BLUETOOTH_EVENT_GATT_READ_DESC,
3778 BT_INFO("Send DBUS rpely for GATT Read Descriptor");
3779 /* Send DBUS return */
3780 __bt_gatt_handle_pending_request_info(result,
3781 BT_GATT_READ_DESCRIPTOR_VALUE,
3783 sizeof(bluetooth_gatt_client_desc_prop_info_t));
3794 static void __bt_handle_client_characteristic_write_data(
3795 event_gattc_write_data *event_data)
3797 int result = BLUETOOTH_ERROR_NONE;
3798 struct gatt_server_info_t *conn_info = NULL;
3799 bluetooth_gatt_client_char_prop_info_t write_info;
3801 /* Read Information data structures */
3802 GVariant *param = NULL;
3803 GVariant *data_svc_uuid = NULL;
3804 GVariant *data_char_uuid = NULL;
3805 char *svc_uuid = NULL;
3806 char *char_uuid = NULL;
3809 memset(&write_info, 0x00, sizeof(bluetooth_gatt_client_char_prop_info_t));
3811 /* Extract Address from conn_id of event data */
3812 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3813 event_data->conn_status.conn_id);
3815 BT_DBG("Characteristic Write callback from addr [%s] status [%d]",
3816 conn_info->addr, event_data->conn_status.status);
3818 /* Fill char in buffer */
3819 memcpy(&write_info.characteristic.uuid,
3820 event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3821 write_info.characteristic.instance_id = event_data->char_id.inst_id;
3823 /* Fill Service in buffer */
3824 memcpy(&write_info.svc.uuid,
3825 event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3826 write_info.svc.instance_id = event_data->srvc_id.id.inst_id;
3828 /* Fill remote device address */
3829 _bt_convert_addr_string_to_type(write_info.device_address.addr, conn_info->addr);
3831 if (event_data->conn_status.status != OAL_STATUS_SUCCESS) {
3832 result = BLUETOOTH_ERROR_INTERNAL;
3838 svc_uuid = g_memdup(&event_data->srvc_id.id.uuid.uuid[0], uuid_len);
3840 data_svc_uuid = g_variant_new_from_data(
3841 G_VARIANT_TYPE_BYTESTRING,
3847 char_uuid = g_memdup(&event_data->char_id.uuid.uuid[0], uuid_len);
3849 data_char_uuid = g_variant_new_from_data(
3850 G_VARIANT_TYPE_BYTESTRING,
3855 param = g_variant_new("(isn@ayin@ayi)", result,
3859 event_data->srvc_id.id.inst_id,
3862 event_data->char_id.inst_id);
3865 _bt_send_event(BT_GATT_CLIENT_EVENT,
3866 BLUETOOTH_EVENT_GATT_WRITE_CHAR,
3875 /* Send DBUS return */
3876 __bt_gatt_handle_pending_request_info(result,
3877 BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE,
3879 sizeof(bluetooth_gatt_client_char_prop_info_t));
3883 static void __bt_handle_client_descriptor_write_data(
3884 event_gattc_write_data *event_data)
3886 int result = BLUETOOTH_ERROR_NONE;
3887 struct gatt_server_info_t *conn_info = NULL;
3888 bluetooth_gatt_client_desc_prop_info_t write_info;
3890 /* Write Information data structures */
3891 GVariant *param = NULL;
3892 GVariant *data_svc_uuid = NULL;
3893 GVariant *data_char_uuid = NULL;
3894 GVariant *data_desc_uuid = NULL;
3895 char *svc_uuid = NULL;
3896 char *char_uuid = NULL;
3897 char *desc_uuid = NULL;
3900 memset(&write_info, 0x00, sizeof(bluetooth_gatt_client_desc_prop_info_t));
3902 /* Extract Address from conn_id of event data */
3903 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3904 event_data->conn_status.conn_id);
3906 if (NULL == conn_info) {
3907 BT_ERR("Failed to get the conn info for conn_id [%d]", event_data->conn_status.conn_id);
3911 BT_INFO("Descriptor Write callback from addr [%s] status [%d]",
3912 conn_info->addr, event_data->conn_status.status);
3914 /* Fill descriptor informations in buffer */
3915 memcpy(&write_info.descriptor.uuid,
3916 event_data->descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3917 write_info.descriptor.instance_id = event_data->descr_id.inst_id;
3919 /* Fill Characteristic informations in buffer */
3920 memcpy(&write_info.characteristic.uuid,
3921 event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3922 write_info.characteristic.instance_id = event_data->char_id.inst_id;
3924 /* Fill Service informations in buffer */
3925 memcpy(&write_info.svc.uuid,
3926 event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3927 write_info.svc.instance_id = event_data->srvc_id.id.inst_id;
3929 /* Fill remote device address */
3930 _bt_convert_addr_string_to_type(write_info.device_address.addr, conn_info->addr);
3932 if (event_data->conn_status.status != OAL_STATUS_SUCCESS) {
3933 result = BLUETOOTH_ERROR_INTERNAL;
3939 svc_uuid = g_memdup(&event_data->srvc_id.id.uuid.uuid[0], uuid_len);
3941 data_svc_uuid = g_variant_new_from_data(
3942 G_VARIANT_TYPE_BYTESTRING,
3948 char_uuid = g_memdup(&event_data->char_id.uuid.uuid[0], uuid_len);
3950 data_char_uuid = g_variant_new_from_data(
3951 G_VARIANT_TYPE_BYTESTRING,
3957 desc_uuid = g_memdup(&event_data->descr_id.uuid.uuid[0], uuid_len);
3959 data_desc_uuid = g_variant_new_from_data(
3960 G_VARIANT_TYPE_BYTESTRING,
3965 param = g_variant_new("(isn@ayin@ayin@ayi)", result,
3969 event_data->srvc_id.id.inst_id,
3972 event_data->char_id.inst_id,
3975 event_data->descr_id.inst_id);
3978 _bt_send_event(BT_GATT_CLIENT_EVENT,
3979 BLUETOOTH_EVENT_GATT_WRITE_DESC,
3990 /* Send DBUS return */
3991 __bt_gatt_handle_pending_request_info(result,
3992 BT_GATT_WRITE_DESCRIPTOR_VALUE,
3994 sizeof(bluetooth_gatt_client_desc_prop_info_t));
3997 static void __bt_hanlde_le_device_disconnection(event_dev_conn_status_t *event_data)
3999 int result = BLUETOOTH_ERROR_INTERNAL;
4000 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
4002 _bt_convert_addr_type_to_string(address, (unsigned char *)event_data->address.addr);
4004 /* DBUS Return with fail of pending BT_CONNECT_LE for all the apps */
4005 BT_INFO("Local GATT Client disconnected: Remote addr[%s] ", address + 12);
4007 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE, address,
4008 BT_ADDRESS_STRING_SIZE);
4012 static void __bt_handle_client_notification_registered(
4013 event_gattc_regdereg_notify_t *event_data,
4014 gboolean is_registered)
4016 int result = BLUETOOTH_ERROR_NONE;
4017 struct gatt_server_info_t *conn_info = NULL;
4018 bt_gatt_notif_reg_info_t notif_info;
4021 memset(¬if_info, 0x00, sizeof(bt_gatt_notif_reg_info_t));
4023 BT_INFO("Client Interface [%d] status [%d]",
4024 event_data->conn_id,
4025 event_data->status);
4027 /* Extract Address from conn_id of event data */
4028 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(event_data->conn_id);
4030 BT_INFO("Connection Info is not present, return");
4033 BT_INFO("Notification Registered for addr [%s]", conn_info->addr);
4035 /* Fill svc informations in buffer */
4036 memcpy(¬if_info.svc_uuid,
4037 event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4038 notif_info.svc_inst = event_data->srvc_id.id.inst_id;
4040 /* Fill char in buffer */
4041 memcpy(¬if_info.char_uuid,
4042 event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4043 notif_info.char_inst = event_data->char_id.inst_id;
4045 /* Fill remote device address */
4046 _bt_convert_addr_string_to_type(notif_info.addr.addr, conn_info->addr);
4048 notif_info.is_registered = is_registered;
4050 if (event_data->status != OAL_STATUS_SUCCESS)
4051 result = BLUETOOTH_ERROR_INTERNAL;
4053 /* Send DBUS Return for BT_GATT_WATCH_CHARACTERISTIC */
4054 __bt_gatt_handle_pending_request_info(result,
4055 BT_GATT_WATCH_CHARACTERISTIC,
4057 sizeof(bt_gatt_notif_reg_info_t));
4060 static void __bt_handle_client_notification_data(event_gattc_notify_data *event_data)
4062 /* No status in this event from OAL */
4063 int result = BLUETOOTH_ERROR_NONE;
4065 /* Read Information data structures */
4066 GVariant *param = NULL;
4067 GVariant *data = NULL;
4068 GVariant *data_svc_uuid = NULL;
4069 GVariant *data_char_uuid = NULL;
4070 char *read_val = NULL;
4071 char *svc_uuid = NULL;
4072 char *char_uuid = NULL;
4078 BT_INFO("Notifcation of charc data changed");
4080 if (event_data->data_len > 0) {
4082 for (i = 0; i < event_data->data_len; i++)
4083 BT_INFO("Data[%d] = [0x%x]", i, event_data->data[i]);
4086 addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
4087 _bt_convert_addr_type_to_string(addr,
4088 (unsigned char *)&(event_data->address.addr));
4091 read_val = g_memdup(&event_data->data[0], event_data->data_len);
4093 data = g_variant_new_from_data(
4094 G_VARIANT_TYPE_BYTESTRING,
4096 event_data->data_len,
4099 svc_uuid = g_memdup(&event_data->srvc_id.id.uuid.uuid[0], uuid_len);
4101 data_svc_uuid = g_variant_new_from_data(
4102 G_VARIANT_TYPE_BYTESTRING,
4108 char_uuid = g_memdup(&event_data->char_id.uuid.uuid[0], uuid_len);
4110 data_char_uuid = g_variant_new_from_data(
4111 G_VARIANT_TYPE_BYTESTRING,
4117 param = g_variant_new("(isn@ayin@ayin@ay)", result,
4121 event_data->srvc_id.id.inst_id,
4124 event_data->char_id.inst_id,
4125 event_data->data_len,
4129 _bt_send_event(BT_GATT_CLIENT_EVENT,
4130 BLUETOOTH_EVENT_GATT_CHAR_VAL_CHANGED,
4133 BT_ERR("No Data!!");
4146 static void __bt_handle_client_service_changed_ind(event_gattc_service_changed_data *event_data)
4148 bt_gatt_service_info_list_t *svc_info_list;
4150 bt_gatt_service_info_t *svc_info;
4151 GVariant *param = NULL;
4152 char *address_str = NULL;
4153 char *uuid_str = NULL;
4155 svc_info_list = __bt_get_service_info_list(event_data->conn_id);
4156 if (svc_info_list == NULL) {
4157 BT_ERR("svc_info_list is NULL");
4161 if (event_data->change_type) {
4162 /* Add service UUID in list */
4163 svc_info = g_malloc0(sizeof(bt_gatt_service_info_t));
4164 memcpy(svc_info->uuid, event_data->uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4165 svc_info->inst_id = event_data->inst_id;
4166 svc_info->is_primary = 1; // TODO: Need to check is_primary is required or not
4167 svc_info_list->services = g_slist_append(svc_info_list->services, svc_info);
4168 BT_DBG("Service created and added in Svc info list or connection ID %d", event_data->conn_id);
4170 /* Remove service UUID in list */
4171 for (l = svc_info_list->services; l != NULL; l = g_slist_next(l)) {
4172 svc_info = (bt_gatt_service_info_t *)l->data;
4173 if (svc_info == NULL)
4176 if (!memcmp(svc_info->uuid, event_data->uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)) {
4177 svc_info_list->services = g_slist_remove(svc_info_list->services, svc_info);
4178 __bt_free_service_info(svc_info);
4184 address_str = g_malloc0(BT_ADDRESS_STRING_SIZE);
4185 uuid_str = g_malloc0(BT_UUID_STRING_MAX);
4186 _bt_convert_addr_type_to_string(address_str, event_data->address.addr);
4187 _bt_uuid_to_string(&event_data->uuid, uuid_str);
4189 param = g_variant_new("(iiss)", event_data->inst_id, event_data->change_type, address_str, uuid_str);
4191 _bt_send_event(BT_GATT_CLIENT_EVENT,
4192 BLUETOOTH_EVENT_GATT_CLIENT_SERVICE_CHANGED,
4194 g_free(address_str);
4198 gboolean _bt_is_remote_gatt_device_connected(bluetooth_device_address_t *address)
4201 struct gatt_server_info_t *conn_info = NULL;
4202 gboolean connected = FALSE;
4204 addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
4205 _bt_convert_addr_type_to_string(addr,
4206 (unsigned char *)&(address->addr));
4208 BT_DBG("Check GATT connection status of [%s]", addr);
4209 /* Check if device is already in connected list */
4210 conn_info = _bt_find_remote_gatt_server_info(addr);
4212 BT_DBG("Remote GATT Server device [%s] is Connected", conn_info->addr);
4215 struct gatt_client_info_t *client_info = NULL;
4217 BT_DBG("Remote GATT Server Device [%s] is not Connected", addr);
4219 /* Check if device is already in connected list */
4220 client_info = _bt_find_remote_gatt_client_info(addr);
4222 BT_DBG("Remote Client device [%s] is Connected", client_info->addr);
4225 BT_DBG("Remote GATT Client Device [%s] is not Connected", addr);
4233 void _bt_handle_invocation_context(int function_name, void *data)
4235 switch (function_name) {
4237 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE, BT_CONNECT_LE,
4238 (char *)data, BT_ADDRESS_STRING_SIZE);
4245 int _bt_connect_le_device(bluetooth_device_address_t *address,
4246 int auto_connect, int client_id)
4248 struct gatt_server_info_t *conn_info = NULL;
4249 struct gatt_out_conn_info_t *out_conn_info = NULL;
4251 invocation_info_t *req_info = NULL;
4252 int ret = OAL_STATUS_SUCCESS;
4254 char *remote_address = NULL;
4256 BT_CHECK_PARAMETER(address, return);
4258 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4259 _bt_convert_addr_type_to_string(addr, address->addr);
4260 BT_DBG("GATT Client connect request for address [%s] client instance [%d]",
4264 /* Check if Remote Device is already under connection progress */
4265 req_info = _bt_get_request_info_data_from_function_name(BT_CONNECT_LE);
4267 remote_address = (char*)req_info->user_data;
4268 if (remote_address && !strcasecmp(remote_address, addr)) {/* Address matched */
4269 BT_DBG("Already Connection ongoing for same remote GATT Server address [%s]", remote_address);
4270 /* Return and wait for events to be sent to all apps */
4272 return BLUETOOTH_ERROR_IN_PROGRESS;
4276 /* Check if remote GATT Server is connected or not */
4277 conn_info = _bt_find_remote_gatt_server_info(addr);
4279 BT_ERR("GATT Server is already connected..");
4281 return BLUETOOTH_ERROR_ALREADY_CONNECT;
4284 /* TODO Check Requirement of holding Advertisement before initiating LE connect */
4286 /* Check if app sent 0 client id for connection, in such case, use default gatt client ID */
4287 if (client_id == 0) {
4288 /* GATT CLient connect request sent by an app without any client instance [0] */
4289 BT_DBG("Assign default GATT client id [%d]", gatt_default_client);
4290 client_id = gatt_default_client;
4293 BT_INFO("Connect using CLient ID [%d]", client_id);
4294 ret = gattc_connect(client_id, (bt_address_t*)(address), auto_connect);
4296 if (ret != OAL_STATUS_SUCCESS) {
4297 BT_ERR("gattc_connect is failed. ret: %d", ret);
4299 return _bt_convert_oal_status_to_bt_error(ret);
4302 /* Mark this as outgoing connection */
4303 out_conn_info = g_new0(struct gatt_out_conn_info_t, 1);
4304 out_conn_info->addr = g_strdup(addr);
4305 out_conn_info->client_id = client_id;
4306 BT_INFO("Added outgoing connection info addr[%s]", out_conn_info->addr + 12);
4307 outgoing_gatt_conn_list = g_slist_append(outgoing_gatt_conn_list, out_conn_info);
4310 return BLUETOOTH_ERROR_NONE;
4313 int _bt_gatt_get_primary_services(char *address)
4315 BT_CHECK_PARAMETER(address, return);
4316 struct gatt_server_info_t *conn_info = NULL;
4317 invocation_info_t *req_info = NULL;
4318 int ret = OAL_STATUS_SUCCESS;
4320 /* Check if any app is already browsing primary services on the same remote GATT Server */
4321 req_info = _bt_get_request_info_data(BT_GATT_GET_PRIMARY_SERVICES, address);
4323 BT_INFO("Already Primary Service Browsing ongoing for same rmeote GATT Server");
4324 /* Return and wait for events to be sent to all apps */
4325 return BLUETOOTH_ERROR_NONE;
4328 /* Check if remote GATT Server is connected or not */
4329 conn_info = _bt_find_remote_gatt_server_info(address);
4331 BT_ERR("GATT Server is not yet connected..");
4332 return BLUETOOTH_ERROR_NOT_CONNECTED;
4335 BT_INFO("Get all services. GATT Server [%s] is connected, conn Id [%d]",
4336 conn_info->addr + 12, conn_info->connection_id);
4338 /* Send Primary Service Browsing request to stack */
4339 ret = gattc_search_service(conn_info->connection_id, NULL);
4340 if (ret != OAL_STATUS_SUCCESS) {
4341 BT_ERR("ret: %d", ret);
4342 return _bt_convert_oal_status_to_bt_error(ret);
4344 return BLUETOOTH_ERROR_NONE;
4347 int _bt_gatt_get_all_characteristic(bluetooth_gatt_client_svc_prop_info_t *svc)
4349 BT_CHECK_PARAMETER(svc, return);
4350 struct gatt_server_info_t *conn_info = NULL;
4351 invocation_info_t *req_info = NULL;
4352 bluetooth_gatt_client_svc_prop_info_t *prop;
4353 oal_gatt_srvc_id_t srvc_id;
4354 int ret = OAL_STATUS_SUCCESS;
4357 /* Check if any app is already browsing characteristics of the same service on the same remote GATT Server */
4358 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_GET_SERVICE_PROPERTIES);
4360 prop = (bluetooth_gatt_client_svc_prop_info_t*)req_info->user_data;
4361 if (prop && !memcmp(svc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t))
4362 && !memcmp(prop->svc.uuid, svc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
4363 && prop->svc.instance_id == svc->svc.instance_id) {
4364 BT_INFO("Already Properties browsing for Primary Service ongoing for same remote GATT Server");
4365 /* Return and wait for events to be sent to all apps */
4366 return BLUETOOTH_ERROR_NONE;
4370 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4371 _bt_convert_addr_type_to_string(addr, svc->device_address.addr);
4373 /* Check if remote GATT Server is connected or not */
4374 conn_info = _bt_find_remote_gatt_server_info(addr);
4376 BT_ERR("GATT Server is not yet connected..");
4378 return BLUETOOTH_ERROR_NOT_CONNECTED;
4381 BT_DBG("Get all charc. GATT Server [%s] is connected, conn Id [%d]",
4382 conn_info->addr, conn_info->connection_id);
4384 srvc_id.is_prmry = TRUE;
4385 srvc_id.id.inst_id = svc->svc.instance_id;
4386 memcpy(srvc_id.id.uuid.uuid, svc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4388 /* Search All Characteristic */
4389 ret = gattc_get_characteristic(conn_info->connection_id, &srvc_id, NULL);
4390 if (ret != OAL_STATUS_SUCCESS) {
4391 BT_ERR("ret: %d", ret);
4393 return _bt_convert_oal_status_to_bt_error(ret);
4396 return BLUETOOTH_ERROR_NONE;
4399 int _bt_gatt_get_all_characteristic_properties(
4400 bluetooth_gatt_client_char_prop_info_t *chr)
4402 struct gatt_server_info_t *conn_info = NULL;
4403 invocation_info_t *req_info = NULL;
4404 bluetooth_gatt_client_char_prop_info_t *prop;
4405 oal_gatt_srvc_id_t srvc_id;
4406 oal_gatt_id_t char_id;
4407 int ret = OAL_STATUS_SUCCESS;
4410 BT_CHECK_PARAMETER(chr, return);
4412 /* Check if any app is already browsing descriptors of the same char of
4413 particular service on the same remote GATT Server */
4414 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_GET_CHARACTERISTIC_PROPERTIES);
4416 prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
4417 if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4418 && !memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4419 && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4420 && !memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4421 && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
4422 BT_INFO("Already Properties browsing for Characteristic ongoing for same remote GATT Server");
4423 /* Return and wait for events to be sent to all apps */
4424 return BLUETOOTH_ERROR_NONE;
4428 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4429 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4431 /* Check if remote GATT Server is connected or not */
4432 conn_info = _bt_find_remote_gatt_server_info(addr);
4434 BT_ERR("GATT Server is not yet connected..");
4436 return BLUETOOTH_ERROR_NOT_CONNECTED;
4439 BT_DBG("Get all desc. GATT Server [%s] is connected, conn Id [%d]",
4440 conn_info->addr, conn_info->connection_id);
4442 srvc_id.is_prmry = TRUE;
4443 srvc_id.id.inst_id = chr->svc.instance_id;
4444 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4446 char_id.inst_id = chr->characteristic.instance_id;
4447 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4449 /* Search All Descriptors */
4450 ret = gattc_get_descriptor(conn_info->connection_id, &srvc_id, &char_id, NULL);
4451 if (ret != OAL_STATUS_SUCCESS) {
4452 BT_ERR("ret: %d", ret);
4454 return _bt_convert_oal_status_to_bt_error(ret);
4457 return BLUETOOTH_ERROR_NONE;
4460 int _bt_gatt_read_characteristic_value(
4461 bluetooth_gatt_client_char_prop_info_t *chr)
4463 struct gatt_server_info_t *conn_info = NULL;
4464 invocation_info_t *req_info = NULL;
4465 bluetooth_gatt_client_char_prop_info_t *prop;
4466 oal_gatt_srvc_id_t srvc_id;
4467 oal_gatt_id_t char_id;
4468 int ret = OAL_STATUS_SUCCESS;
4471 BT_CHECK_PARAMETER(chr, return);
4473 /* Check if any app is already Reading characteristic of the same char of
4474 particular service on the same remote GATT Server */
4475 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_READ_CHARACTERISTIC);
4477 prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
4478 if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4479 && !memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4480 && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4481 && !memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4482 && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
4483 BT_INFO("Already Characteristic value Read operation in progress for same remote GATT Server");
4484 /* Return and wait for events to be sent to all apps */
4485 return BLUETOOTH_ERROR_NONE;
4489 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4490 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4492 /* Check if remote GATT Server is connected or not */
4493 conn_info = _bt_find_remote_gatt_server_info(addr);
4495 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4496 conn_info->addr, conn_info->connection_id);
4498 BT_ERR("GATT Server is not yet connected..");
4500 return BLUETOOTH_ERROR_NOT_CONNECTED;
4503 srvc_id.is_prmry = TRUE;
4504 srvc_id.id.inst_id = chr->svc.instance_id;
4505 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4507 char_id.inst_id = chr->characteristic.instance_id;
4508 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4510 /* Search All Descriptors */
4511 ret = gattc_read_characteristic(conn_info->connection_id, &srvc_id, &char_id, OAL_GATT_AUTH_REQ_NONE);
4512 if (ret != OAL_STATUS_SUCCESS) {
4513 BT_ERR("ret: %d", ret);
4515 return _bt_convert_oal_status_to_bt_error(ret);
4518 return BLUETOOTH_ERROR_NONE;
4521 int _bt_gatt_read_descriptor_value(
4522 bluetooth_gatt_client_desc_prop_info_t *desc)
4524 struct gatt_server_info_t *conn_info = NULL;
4525 invocation_info_t *req_info = NULL;
4526 bluetooth_gatt_client_desc_prop_info_t *prop;
4527 oal_gatt_srvc_id_t srvc_id;
4528 oal_gatt_id_t char_id;
4529 oal_gatt_id_t desc_id;
4530 int ret = OAL_STATUS_SUCCESS;
4533 BT_CHECK_PARAMETER(desc, return);
4535 /* Check if any app is already Reading descriptors of the same char of
4536 particular service on the same remote GATT Server */
4537 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_READ_DESCRIPTOR_VALUE);
4539 prop = (bluetooth_gatt_client_desc_prop_info_t*)req_info->user_data;
4540 if (prop && !memcmp(desc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4541 && !memcmp(desc->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4542 && desc->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4543 && !memcmp(desc->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4544 && desc->characteristic.instance_id == prop->characteristic.instance_id /* Characteristic Instance ID matched */
4545 && !memcmp(desc->descriptor.uuid, prop->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Descriptor UUID matched */
4546 && desc->descriptor.instance_id == prop->descriptor.instance_id) { /* Descriptor Instance ID matched */
4547 BT_INFO("Already Descriptor value Read operation in progress for same remote GATT Server");
4548 /* Return and wait for events to be sent to all apps */
4549 return BLUETOOTH_ERROR_NONE;
4553 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4554 _bt_convert_addr_type_to_string(addr, desc->device_address.addr);
4556 /* Check if remote GATT Server is connected or not */
4557 conn_info = _bt_find_remote_gatt_server_info(addr);
4559 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4560 conn_info->addr, conn_info->connection_id);
4562 BT_ERR("GATT Server is not yet connected..");
4564 return BLUETOOTH_ERROR_NOT_CONNECTED;
4567 srvc_id.is_prmry = TRUE;
4568 srvc_id.id.inst_id = desc->svc.instance_id;
4569 memcpy(srvc_id.id.uuid.uuid, desc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4571 char_id.inst_id = desc->characteristic.instance_id;
4572 memcpy(char_id.uuid.uuid, desc->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4574 desc_id.inst_id = desc->descriptor.instance_id;
4575 memcpy(desc_id.uuid.uuid, desc->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4577 /* Search All Descriptors */
4578 ret = gattc_read_descriptor(conn_info->connection_id, &srvc_id, &char_id,
4579 &desc_id, OAL_GATT_AUTH_REQ_NONE);
4580 if (ret != OAL_STATUS_SUCCESS) {
4581 BT_ERR("ret: %d", ret);
4583 return _bt_convert_oal_status_to_bt_error(ret);
4586 return BLUETOOTH_ERROR_NONE;
4590 int _bt_gatt_acquire_notify(bluetooth_gatt_client_char_prop_info_t *chr, int *fd, int *mtu)
4592 struct gatt_server_info_t *conn_info = NULL;
4593 oal_gatt_srvc_id_t srvc_id;
4594 oal_gatt_id_t char_id;
4595 int ret = OAL_STATUS_SUCCESS;
4598 BT_CHECK_PARAMETER(chr, return);
4600 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4601 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4603 /* Check if remote GATT Server is connected or not */
4604 conn_info = _bt_find_remote_gatt_server_info(addr);
4606 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4607 conn_info->addr, conn_info->connection_id);
4609 BT_ERR("GATT Server is not yet connected..");
4611 return BLUETOOTH_ERROR_NOT_CONNECTED;
4614 srvc_id.is_prmry = TRUE;
4615 srvc_id.id.inst_id = chr->svc.instance_id;
4616 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4618 char_id.inst_id = chr->characteristic.instance_id;
4619 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4621 ret = gattc_acquire_notify(conn_info->connection_id, &srvc_id, &char_id, fd, mtu);
4623 if (ret != OAL_STATUS_SUCCESS) {
4624 BT_ERR("ret: %d", ret);
4626 return _bt_convert_oal_status_to_bt_error(ret);
4628 BT_INFO("GATT characterstics FD [%d] mtu[%d]", *fd, *mtu);
4630 return BLUETOOTH_ERROR_NONE;
4634 int _bt_gatt_acquire_write(bluetooth_gatt_client_char_prop_info_t *chr, int *fd, int *mtu)
4637 struct gatt_server_info_t *conn_info = NULL;
4638 oal_gatt_srvc_id_t srvc_id;
4639 oal_gatt_id_t char_id;
4640 int ret = OAL_STATUS_SUCCESS;
4643 BT_CHECK_PARAMETER(chr, return);
4645 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4646 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4648 /* Check if remote GATT Server is connected or not */
4649 conn_info = _bt_find_remote_gatt_server_info(addr);
4651 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4652 conn_info->addr, conn_info->connection_id);
4654 BT_ERR("GATT Server is not yet connected..");
4656 return BLUETOOTH_ERROR_NOT_CONNECTED;
4659 srvc_id.is_prmry = TRUE;
4660 srvc_id.id.inst_id = chr->svc.instance_id;
4661 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4663 char_id.inst_id = chr->characteristic.instance_id;
4664 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4666 ret = gattc_acquire_write(conn_info->connection_id, &srvc_id, &char_id,
4667 OAL_GATT_AUTH_REQ_NONE, fd, mtu);
4668 if (ret != OAL_STATUS_SUCCESS) {
4669 BT_ERR("ret: %d", ret);
4671 return _bt_convert_oal_status_to_bt_error(ret);
4673 BT_INFO("GATT characterstics FD [%d] mtu [%d]", *fd, *mtu);
4675 return BLUETOOTH_ERROR_NONE;
4680 /* Write Characteristic */
4681 int _bt_gatt_write_characteristic_value_by_type(
4682 bluetooth_gatt_client_char_prop_info_t *chr,
4683 bluetooth_gatt_att_data_t *data,
4684 bluetooth_gatt_write_type_e write_type)
4686 struct gatt_server_info_t *conn_info = NULL;
4687 invocation_info_t *req_info = NULL;
4688 bluetooth_gatt_client_char_prop_info_t *prop;
4689 oal_gatt_srvc_id_t srvc_id;
4690 oal_gatt_id_t char_id;
4691 int ret = OAL_STATUS_SUCCESS;
4694 BT_CHECK_PARAMETER(chr, return);
4695 BT_CHECK_PARAMETER(data, return);
4697 /* Check if any app is already writing same char of
4698 particular service on the same remote GATT Server */
4699 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE);
4701 prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
4702 if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4703 && !memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4704 && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4705 && !memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4706 && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
4707 BT_INFO("Already Characteristic Write Value operation in progress for same remote GATT Server");
4708 /* Return and wait for events to be sent to all apps */
4709 return BLUETOOTH_ERROR_NONE;
4713 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4714 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4716 /* Check if remote GATT Server is connected or not */
4717 conn_info = _bt_find_remote_gatt_server_info(addr);
4719 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4720 conn_info->addr, conn_info->connection_id);
4722 BT_ERR("GATT Server is not yet connected..");
4724 return BLUETOOTH_ERROR_NOT_CONNECTED;
4727 srvc_id.is_prmry = TRUE;
4728 srvc_id.id.inst_id = chr->svc.instance_id;
4729 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4731 char_id.inst_id = chr->characteristic.instance_id;
4732 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4734 /* Write CHar value */
4735 ret = gattc_write_characteristic(conn_info->connection_id,
4737 (oal_gatt_write_type_t)write_type, data->length,
4738 OAL_GATT_AUTH_REQ_NONE, (char *)(&data->data[0]));
4739 if (ret != OAL_STATUS_SUCCESS) {
4740 BT_ERR("ret: %d", ret);
4742 return _bt_convert_oal_status_to_bt_error(ret);
4745 return BLUETOOTH_ERROR_NONE;
4748 /* Write Descriptor */
4749 int _bt_gatt_write_descriptor_value_by_type(
4750 bluetooth_gatt_client_desc_prop_info_t *desc,
4751 bluetooth_gatt_att_data_t *data,
4752 bluetooth_gatt_write_type_e write_type)
4754 struct gatt_server_info_t *conn_info = NULL;
4755 invocation_info_t *req_info = NULL;
4756 bluetooth_gatt_client_desc_prop_info_t *prop;
4757 oal_gatt_srvc_id_t srvc_id;
4758 oal_gatt_id_t char_id;
4759 oal_gatt_id_t desc_id;
4760 int ret = OAL_STATUS_SUCCESS;
4764 BT_CHECK_PARAMETER(desc, return);
4765 BT_CHECK_PARAMETER(data, return);
4769 /* Check if any app is already writing on same Descriptor of the same char of
4770 particular service on the same remote GATT Server */
4771 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_WRITE_DESCRIPTOR_VALUE);
4773 prop = (bluetooth_gatt_client_desc_prop_info_t*)req_info->user_data;
4774 if (prop && !memcmp(desc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4775 && !memcmp(desc->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4776 && desc->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4777 && !memcmp(desc->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4778 && desc->characteristic.instance_id == prop->characteristic.instance_id /* Characteristic Instance ID matched */
4779 && !memcmp(desc->descriptor.uuid, prop->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Descriptor UUID matched */
4780 && desc->descriptor.instance_id == prop->descriptor.instance_id) { /* Descriptor Instance ID matched */
4781 BT_INFO("Already Descriptor value Write operation in progress for same remote GATT Server");
4782 /* Return and wait for events to be sent to all apps */
4783 return BLUETOOTH_ERROR_NONE;
4787 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4788 _bt_convert_addr_type_to_string(addr, desc->device_address.addr);
4790 /* Check if remote GATT Server is connected or not */
4791 conn_info = _bt_find_remote_gatt_server_info(addr);
4793 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4794 conn_info->addr, conn_info->connection_id);
4796 BT_ERR("GATT Server is not yet connected..");
4798 return BLUETOOTH_ERROR_NOT_CONNECTED;
4801 srvc_id.is_prmry = TRUE;
4802 srvc_id.id.inst_id = desc->svc.instance_id;
4803 memcpy(srvc_id.id.uuid.uuid, desc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4805 char_id.inst_id = desc->characteristic.instance_id;
4806 memcpy(char_id.uuid.uuid, desc->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4808 desc_id.inst_id = desc->descriptor.instance_id;
4809 memcpy(desc_id.uuid.uuid, desc->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4812 BT_INFO("Connection ID [%d] write type [%d] data length [%d]", conn_info->connection_id, write_type, data->length);
4813 for (k = 0; k < data->length; k++)
4814 BT_INFO("Data[%d] [0x%x]", k, data->data[k]);
4816 ret = gattc_write_descriptor(conn_info->connection_id,
4817 &srvc_id, &char_id, &desc_id,
4818 (oal_gatt_write_type_t)write_type, data->length,
4819 OAL_GATT_AUTH_REQ_NONE, (char *)(&data->data[0]));
4820 if (ret != OAL_STATUS_SUCCESS) {
4821 BT_ERR("ret: %d", ret);
4823 return _bt_convert_oal_status_to_bt_error(ret);
4826 return BLUETOOTH_ERROR_NONE;
4829 int _bt_gatt_watch_characteristic(
4830 bluetooth_gatt_client_char_prop_info_t *chr,
4834 struct gatt_server_info_t *conn_info = NULL;
4835 oal_gatt_srvc_id_t srvc_id;
4836 oal_gatt_id_t char_id;
4837 int ret = OAL_STATUS_SUCCESS;
4840 BT_CHECK_PARAMETER(chr, return);
4842 BT_INFO("Client ID [%d] Is Notify [%d]", client_id, is_notify);
4844 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4845 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4847 /* Check if remote GATT Server is connected or not */
4848 conn_info = _bt_find_remote_gatt_server_info(addr);
4850 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4851 conn_info->addr, conn_info->connection_id);
4853 BT_ERR("GATT Server is not yet connected..");
4855 return BLUETOOTH_ERROR_NOT_CONNECTED;
4857 srvc_id.is_prmry = TRUE;
4858 srvc_id.id.inst_id = chr->svc.instance_id;
4859 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4861 char_id.inst_id = chr->characteristic.instance_id;
4862 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4864 /* Register or unregister Notification characteristic */
4866 ret = gattc_register_for_notification(conn_info->connection_id,
4867 (bt_address_t*)&(chr->device_address),
4868 &srvc_id, &char_id);
4870 ret = gattc_deregister_for_notification(conn_info->connection_id,
4871 (bt_address_t*)&(chr->device_address),
4872 &srvc_id, &char_id);
4874 BT_INFO("Result[%d]", ret);
4875 if (ret != OAL_STATUS_SUCCESS) {
4876 BT_ERR("ret: %d", ret);
4878 return _bt_convert_oal_status_to_bt_error(ret);
4881 return BLUETOOTH_ERROR_NONE;
4885 int _bt_disconnect_le_device(bluetooth_device_address_t *address,
4888 struct gatt_server_info_t *conn_info = NULL;
4889 struct gatt_client_info_t *rem_client_conn_info = NULL;
4890 invocation_info_t *req_info = NULL;
4891 int ret = OAL_STATUS_SUCCESS;
4893 char *remote_address = NULL;
4895 BT_CHECK_PARAMETER(address, return);
4897 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4898 _bt_convert_addr_type_to_string(addr, address->addr);
4899 BT_INFO("GATT Client Disconnect request for address [%s]", addr + 12);
4901 /* Check if Remote Device is already under connection progress */
4902 req_info = _bt_get_request_info_data_from_function_name(BT_DISCONNECT_LE);
4904 remote_address = (char*)req_info->user_data;
4905 if (remote_address && !strcasecmp(remote_address, addr)) {/* Address matched */
4906 BT_DBG("Already DisConnection ongoing for same remote GATT Server address [%s]", remote_address);
4907 /* Return success and wait for events to be sent to all apps */
4909 return BLUETOOTH_ERROR_IN_PROGRESS;
4912 /* Check if remote GATT Server is connected or not */
4913 conn_info = _bt_find_remote_gatt_server_info(addr);
4915 /* Check if app sent 0 client id for Disconnection, in such case, use default gatt client ID */
4916 if (client_id == 0) {
4917 BT_INFO("GATT CLient Disconnect request sent by an app without any client instance [%d]",
4919 BT_INFO("Assign default GATT client id [%d]", gatt_default_client);
4920 client_id = gatt_default_client;
4923 BT_INFO("Disconnect remote gatt server using CLient ID [%d] Connection ID [%d]", client_id, conn_info->connection_id);
4924 ret = gattc_disconnect(client_id, (bt_address_t*)(address),
4925 conn_info->connection_id);
4927 /* check if remote client is connected */
4928 rem_client_conn_info = _bt_find_remote_gatt_client_info(addr);
4930 if (!rem_client_conn_info || client_id != 0) {
4931 BT_ERR("GATT device is not connected..");
4933 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
4936 BT_INFO("Disconnect remote gatt client ");
4938 ret = gatts_disconnect(rem_client_conn_info->instance_id,
4939 (bt_address_t*)(address), rem_client_conn_info->connection_id);
4942 if (ret != OAL_STATUS_SUCCESS) {
4943 BT_ERR("ret: %d", ret);
4945 return _bt_convert_oal_status_to_bt_error(ret);
4948 return BLUETOOTH_ERROR_NONE;
4951 int _bt_gatt_watch_service_changed_indication(const char *sender,
4952 bluetooth_device_address_t *address,
4953 gboolean is_enabled)
4956 bt_service_app_info_t *info = NULL;
4958 BT_INFO("%s Servic changed Indication watcher for app [%s]",
4959 is_enabled ? "Enable":"Disable", sender);
4961 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
4964 if (g_strcmp0(sender, info->sender) == 0 &&
4965 memcmp(info->address.addr, address->addr,
4966 sizeof(bluetooth_device_address_t)) == 0) {
4967 BT_DBG("Found GATT client App.. [%s], sender [%s]", info->uuid, info->sender);
4968 info->is_watcher_enabled = is_enabled;
4972 return BLUETOOTH_ERROR_NONE;
4975 int _bt_unregister_gatt_client_instance(const char *sender, int client_id)
4977 bt_service_app_info_t *info = NULL;
4980 BT_DBG("Unregister Allocated GATT Client instance [%s] Client ID [%d]", sender, client_id);
4982 /* Unregister CLient instance associated with address X. It is possible that another app still
4983 has client_id valid for same remote address */
4984 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
4987 /* Exact matching of sender */
4988 if (!g_strcmp0(info->sender, sender) && info->client_id == client_id) { /* Check for only valid GATT client Instance */
4989 numapps[k].client_id = -1;
4990 numapps[k].is_initialized = FALSE;
4991 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
4992 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
4993 memset(&numapps[k].address.addr, 0x00, sizeof(bluetooth_device_address_t));
4995 /* Its a GATT Client Instance */
4996 ret = gattc_deregister(client_id);
4997 if (ret != OAL_STATUS_SUCCESS) {
4998 BT_ERR("DeAllocate GATT Client instance with stack Fail ret: %d", ret);
4999 return _bt_convert_oal_status_to_bt_error(ret);
5001 return BLUETOOTH_ERROR_NONE;
5006 return BLUETOOTH_ERROR_NOT_FOUND;
5009 static void __bt_handle_client_mtu_exchange_completed(event_gattc_mtu_configured_t *event_data)
5011 int result = BLUETOOTH_ERROR_NONE;
5012 struct gatt_server_info_t *conn_info = NULL;
5013 GVariant *param = NULL;
5017 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(event_data->conn_id);
5018 if (conn_info == NULL) {
5019 BT_ERR("Cant find connection Information");
5023 BT_DBG("GATT Client: MTU Configured from addr [%s] status [%d] MTU size [%d]",
5024 conn_info->addr, event_data->status, event_data->mtu);
5026 if (event_data->status != OAL_STATUS_SUCCESS)
5027 result = BLUETOOTH_ERROR_INTERNAL;
5029 /* DBUS Return fo BT_REQ_ATT_MTU for all the apps */
5030 __bt_gatt_handle_pending_request_info(result, BT_REQ_ATT_MTU, conn_info->addr,
5031 BT_ADDRESS_STRING_SIZE);
5033 if (result == BLUETOOTH_ERROR_NONE) {
5034 mtu = event_data->mtu;
5035 param = g_variant_new("(isqy)",
5041 /* Send event to BT-API */
5042 _bt_send_event(BT_DEVICE_EVENT,
5043 BLUETOOTH_EVENT_GATT_ATT_MTU_CHANGED,
5046 /* Update the MTU for current connection */
5047 __bt_update_mtu_gatt_device(conn_info->addr, event_data->mtu);
5051 static struct gatt_mtu_info_t *__bt_find_mtu_gatt_device(char *address)
5054 struct gatt_mtu_info_t *info = NULL;
5056 for (l = gatt_mtu_info_list; l != NULL; l = g_slist_next(l)) {
5057 info = (struct gatt_mtu_info_t*)l->data;
5061 if (!g_strcmp0(info->addr, address)) {
5062 BT_DBG("Remote GATT device found addr[%s]", info->addr);
5067 BT_DBG("Not found Remote GATT device addr[%s]", address);
5071 static void __bt_remove_mtu_gatt_device(char *address)
5073 struct gatt_mtu_info_t *dev_info = NULL;
5075 dev_info = __bt_find_mtu_gatt_device(address);
5078 BT_DBG("removing the gatt device from mtu list");
5079 gatt_mtu_info_list = g_slist_remove(gatt_mtu_info_list, dev_info);
5080 g_free(dev_info->addr);
5085 static void __bt_add_mtu_gatt_device(char *address)
5087 struct gatt_mtu_info_t *dev_info = NULL;
5089 dev_info = __bt_find_mtu_gatt_device(address);
5092 BT_DBG("adding the gatt device in mtu list");
5093 dev_info = g_new0(struct gatt_mtu_info_t, 1);
5094 dev_info->addr = g_strdup(address);
5095 dev_info->att_mtu = BT_DEFAULT_ATT_MTU;
5096 gatt_mtu_info_list = g_slist_append(gatt_mtu_info_list, dev_info);
5100 static void __bt_update_mtu_gatt_device(char *address, int mtu)
5102 struct gatt_mtu_info_t *dev_info = NULL;
5104 dev_info = __bt_find_mtu_gatt_device(address);
5107 dev_info->att_mtu = mtu;
5111 int _bt_gatt_get_data_batching_available_packets(
5112 guint *available_packets)
5114 int ret = OAL_STATUS_SUCCESS;
5116 BT_CHECK_PARAMETER(available_packets, return);
5118 ret = gatt_get_data_batching_available_packets(available_packets);
5119 if (ret != OAL_STATUS_SUCCESS) {
5120 BT_ERR("ret: %d", ret);
5121 return _bt_convert_oal_status_to_bt_error(ret);
5124 return BLUETOOTH_ERROR_NONE;
5127 int _bt_gatt_enable_data_batching(bluetooth_device_address_t *address,
5128 int packet_threshold, int timeout)
5130 int ret = OAL_STATUS_SUCCESS;
5131 char remote_address[BT_ADDRESS_STRING_SIZE] = { 0 };
5133 BT_CHECK_PARAMETER(address, return);
5135 _bt_convert_addr_type_to_string(remote_address, address->addr);
5136 BT_INFO("Enable GATT data batching. address[%s] packet_threshold[%d] timeout[%d]",
5137 remote_address, packet_threshold, timeout);
5139 ret = gatt_enable_data_batching((bt_address_t*)(address), packet_threshold, timeout);
5141 if (ret != OAL_STATUS_SUCCESS) {
5142 BT_ERR("ret: %d", ret);
5143 return _bt_convert_oal_status_to_bt_error(ret);
5146 return BLUETOOTH_ERROR_NONE;
5149 int _bt_gatt_disable_data_batching(bluetooth_device_address_t *address)
5151 int ret = OAL_STATUS_SUCCESS;
5152 char remote_address[BT_ADDRESS_STRING_SIZE] = { 0 };
5154 BT_CHECK_PARAMETER(address, return);
5156 _bt_convert_addr_type_to_string(remote_address, address->addr);
5157 BT_INFO("Disable GATT data batching. address[%s]", remote_address);
5159 ret = gatt_disable_data_batching((bt_address_t*)(address));
5161 if (ret != OAL_STATUS_SUCCESS) {
5162 BT_ERR("ret: %d", ret);
5163 return _bt_convert_oal_status_to_bt_error(ret);
5166 return BLUETOOTH_ERROR_NONE;