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 /* Linked List of connected Remote GATT Servers */
137 static GSList *gatt_server_info_list = NULL;
141 unsigned char uuid[BLUETOOTH_UUID_HEX_MAX_LEN]; /* If any service added */
142 } bt_gatt_svc_changed_info_t;
147 // bluetooth_device_address_t address; /* Remote BLE Device Address */
148 GSList *services; /* List of all services of above remote device */
149 int count; /* Number of services browsed from remote device */
150 bt_gatt_svc_changed_info_t info;
151 } bt_gatt_service_info_list_t;
154 unsigned char uuid[BLUETOOTH_UUID_HEX_MAX_LEN];
158 GSList *included_svcs;
159 gboolean is_removed; /* 0 => Service is added, 1=> removed */
160 } bt_gatt_service_info_t;
163 unsigned char uuid[BLUETOOTH_UUID_HEX_MAX_LEN];
167 unsigned char val[BLUETOOTH_GATT_ATT_DATA_LENGTH_MAX];
169 } bt_gatt_char_info_t;
172 unsigned char uuid[BLUETOOTH_UUID_HEX_MAX_LEN];
175 unsigned char val[BLUETOOTH_GATT_ATT_DATA_LENGTH_MAX];
176 } bt_gatt_descriptor_info_t;
179 unsigned char uuid[BLUETOOTH_UUID_HEX_MAX_LEN];
181 } bt_gatt_included_service_info_t;
183 GSList *list_gatt_info;
188 /* App Information structure */
191 char sender[BT_SENDER_MAX_LENGTH];
192 char uuid[BT_UUID_STRING_MAX];
195 bluetooth_advertising_data_t adv_data; /* Will store adv data for specific slot */
197 bluetooth_scan_resp_data_t scan_rsp; /* Will store scan rsp data for specific slot */
199 gboolean is_initialized;
200 GSList *service_handles;
201 #ifdef TIZEN_GATT_CLIENT
202 int client_id; /* GATT Client instance ID */
203 bluetooth_device_address_t address; /* Remote BLE Device Address */
204 gboolean is_watcher_enabled;
206 } bt_service_app_info_t;
208 /* GATT Server Request Info Structure */
209 struct gatt_server_req_info {
210 int connection_id; /* This value will uniquely identify a GATT client-server connection */
211 int request_id; /* This is an unique transaction ID assigned against each request by stack */
212 int attribute_handle; /* GATT server attribute handle */
213 int offset; /* GATT server attribute offset on which request is invoked by GATT client */
214 bluetooth_gatt_att_request_type_e request_type; /* Read or Write request */
215 char *addr; /* Remote GATT client address */
218 /* GATT Client Info List Structure */
219 struct gatt_client_info_t {
220 int connection_id; /* This value will uniquely identify a GATT client-server connection */
221 int instance_id; /* This value unique identifies a GATT server instance */
222 char *addr; /* Remote GATT client address */
225 /* GATT Indicate confirm result */
226 struct gatt_indicate_cfm_result_info_t {
227 int result; /* Result of event */
228 char *addr; /* Remote GATT client address */
229 int att_hdl; /* Characteristic Attribute handle */
230 int completed; /* 1 if last event, otheriwse 0 */
233 /* Request Search Utility method */
234 static struct gatt_server_req_info *__bt_gatt_server_find_request_info(int request_id,
235 bluetooth_gatt_att_request_type_e req_type);
237 static int __bt_gatt_send_indication_to_all_connected_clients(bluetooth_gatt_att_data_t *data,
238 bluetooth_gatt_server_indication_params_t *param);
240 static void __bt_remove_all_service_handles(bt_service_app_info_t *app_info);
242 static void __bt_free_service_info(bt_gatt_service_info_t *service_info);
244 static int __bt_do_unregister_gatt_instance(int instance_id);
245 static void __bt_service_reset_gatt_data(void);
247 #ifdef TIZEN_GATT_CLIENT
248 static void __bt_handle_client_instance_registered(event_gattc_register_t *data);
249 static void __bt_handle_client_connected(event_gattc_conn_t *event_data);
250 static void __bt_handle_client_disconnected(event_gattc_conn_t *event_data);
251 static void __bt_handle_client_service_search_completed(event_gattc_conn_status_t *event_data);
252 static void __bt_handle_client_service_search_result(event_gattc_service_result_t *event_data);
253 static void __bt_handle_client_characteristic_search_result(
254 event_gattc_characteristic_result_t *event_data);
255 static void __bt_handle_client_descriptor_search_result(event_gattc_descriptor_result_t *event_data);
256 static void __bt_handle_client_characteristic_read_data(event_gattc_read_data *event_data);
257 static void __bt_handle_client_descriptor_read_data(event_gattc_read_data *event_data);
258 static void __bt_handle_client_characteristic_write_data(event_gattc_write_data *event_data);
259 static void __bt_handle_client_descriptor_write_data(event_gattc_write_data *event_data);
260 static void __bt_hanlde_le_device_disconnection(event_dev_conn_status_t *event_data);
261 static void __bt_handle_client_notification_registered(event_gattc_regdereg_notify_t *event_data,
262 gboolean is_registered);
263 static void __bt_handle_client_notification_data(event_gattc_notify_data *event_data);
264 static void __bt_handle_client_service_changed_ind(event_gattc_service_changed_data *event_data);
265 static void __bt_handle_client_mtu_exchange_completed(event_gattc_mtu_configured_t *event_data);
268 static int __bt_unregister_gatt_server_instance(int server_instance);
271 struct gatt_mtu_info_t {
272 char *addr; /* Remote GATT Server address */
276 static GSList *gatt_mtu_info_list = NULL;
278 static struct gatt_mtu_info_t *__bt_find_mtu_gatt_device(char *address);
279 static void __bt_remove_mtu_gatt_device(char *address);
280 static void __bt_add_mtu_gatt_device(char *address);
281 static void __bt_update_mtu_gatt_device(char *address, int mtu);
283 /* Linked List of GATT requests from Remote GATT Clients */
284 static GSList *gatt_server_requests = NULL;
286 /* Linked List of connected Remote GATT clients */
287 static GSList *gatt_client_info_list = NULL;
289 /* Number of clients to be notified to */
290 static int num_indicate_clients;
292 /* List of applications */
293 static bt_service_app_info_t numapps[MAX_APPS_SUPPORTED];
295 static void __bt_gatt_handle_pending_request_info(int result,
296 int service_function, void *data, unsigned int size);
298 static void __bt_handle_server_instance_registered(event_gatts_register_t *data);
300 static void __bt_gatt_event_handler(int event_type, gpointer event_data);
302 void _bt_check_adv_app_termination(const char *name)
304 bt_service_app_info_t *app = NULL;
306 int apps[MAX_APPS_SUPPORTED];
308 ret_if(NULL == name);
310 memset(&apps, -1, sizeof(apps));
312 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
315 /* Search for a app which has same sender and stop adv is running */
316 if (!strncasecmp(app->sender, name, strlen(name)) && app->is_initialized == TRUE) {
317 BT_DBG("Match found, name: %s", name);
319 /* TODO 1: If App has both GATT client / server role? */
320 /* TODO 2: Need to manage app info as list, not array.
321 This loop always run for MAX count if any apps are terminated.
324 /* Save instances of all apps that need to be unregistered */
325 if (app->instance_id != -1) {
327 /* Unregister all service handles with stack */
328 __bt_remove_all_service_handles(app);
330 /* If Advertising is enabled, stop it */
331 if (app->adv_handle > 0) {
332 BT_INFO("Stop advertising on instance ID [%d]", app->instance_id);
333 /* Disable adv if running */
334 BT_INFO("Disable Advertising Adv Handle [%d] sender [%s]",
335 app->adv_handle, name);
336 _bt_set_advertising(app->sender, app->adv_handle, FALSE, FALSE);
339 apps[app->instance_id] = 1; /* App holds a GATT client Instance */
340 } else if (app->client_id != -1) {
342 apps[app->client_id] = 1; /* App holds a GATT server Instance */
347 /* Unregister all apps one by one */
348 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
350 BT_INFO("Unregister app[%d]", k);
351 /* Unregister client or server instance */
352 __bt_do_unregister_gatt_instance(k);
357 static int __bt_gatt_send_indication_to_all_connected_clients(bluetooth_gatt_att_data_t *data,
358 bluetooth_gatt_server_indication_params_t *param)
361 int ret = OAL_STATUS_SUCCESS;
362 int result = OAL_STATUS_INTERNAL_ERROR;
364 BT_INFO("Current total number of connected clients [%d]", g_slist_length(gatt_client_info_list));
365 for (l = gatt_client_info_list; l != NULL; l = l->next) {
366 struct gatt_client_info_t *info = l->data;
369 BT_INFO("GATT Remote client address [%s] connection Id [%d]", info->addr, info->connection_id);
371 ret = gatts_send_indication(param->instance_id, param->atrribute_handle,
372 info->connection_id, data->length,
373 param->need_confirmation, (char *)(&data->data[0]));
375 BT_INFO("Send Indication to GATT client [%s] result: [%d]", info->addr, ret);
376 if (ret == OAL_STATUS_SUCCESS) {
377 BT_INFO("Send Indication sent successfully to GATT client [%s]", info->addr);
379 num_indicate_clients++;
383 BT_INFO("Indication sending done for total number of clients [%d]", num_indicate_clients);
387 static struct gatt_server_req_info *__bt_gatt_server_find_request_info(int request_id,
388 bluetooth_gatt_att_request_type_e req_type)
392 for (l = gatt_server_requests; l != NULL; l = l->next) {
393 struct gatt_server_req_info *req_info = l->data;
395 if (req_info && req_info->request_id == request_id && req_info->request_type == req_type) {
396 BT_INFO("GATT Server request info found Req ID [%d] handle [%d] conn ID [%d]",
397 req_info->request_id, req_info->attribute_handle, req_info->connection_id);
401 BT_ERR("Gatt Request not found");
405 void _bt_get_adv_handle_from_instance(int server_inst, int *adv_handle)
409 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
410 if (numapps[k].is_initialized == 1 && numapps[k].instance_id == server_inst) {
411 *adv_handle = numapps[k].adv_handle;
417 #ifdef TIZEN_GATT_CLIENT
418 char * _bt_gatt_get_default_gatt_client_uuid(void)
420 return g_strdup(DEFAULT_GATT_CLIENT_UUID);
424 static void __bt_register_default_gatt_client()
429 uuid_str = _bt_gatt_get_default_gatt_client_uuid();
430 _bt_string_to_uuid(uuid_str, (service_uuid_t*)&uuid);
432 BT_INFO("Register Default GATT client uuid [%s]", uuid_str);
434 if (OAL_STATUS_SUCCESS != gattc_register(&uuid)) /* for only Smart Control */
435 BT_ERR("gattc register failed");
441 int _bt_gatt_init(void)
443 const char *stack_name = NULL;
447 if (OAL_STATUS_SUCCESS != gatt_enable()) {
448 BT_ERR("gatt Init failed");
449 return BLUETOOTH_ERROR_INTERNAL;
452 /* Register gatt event handler */
453 _bt_service_register_event_handler_callback(BT_GATT_MODULE, __bt_gatt_event_handler);
455 __bt_service_reset_gatt_data();
457 stack_name = oal_get_stack_name();
459 if (stack_name && g_strcmp0(stack_name, "bluez") == 0) {
460 /*In the platform, defacult gatt client should be registered */
461 __bt_register_default_gatt_client();
465 return BLUETOOTH_ERROR_NONE;
468 static void __bt_service_reset_gatt_data(void)
472 BT_INFO("Rest numapp");
475 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
476 numapps[k].is_initialized = 0;
477 numapps[k].instance_id = -1;
478 numapps[k].adv_handle = 0;
479 numapps[k].adv_instance = -1;
480 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
481 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
482 memset(numapps[k].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
483 memset(numapps[k].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
484 numapps[k].adv_data_len = 0;
485 numapps[k].scan_rsp_len = 0;
488 numapps[k].client_id = -1;
489 memset(numapps[k].address.addr, 0x00, BLUETOOTH_ADDRESS_LENGTH);
490 numapps[k].is_watcher_enabled = FALSE;
494 void _bt_gatt_deinit(void)
496 BT_INFO("GATT deinit");
498 if (OAL_STATUS_SUCCESS != gatt_disable())
499 BT_ERR("gatt deinit failed");
501 /* Un-register gatt event handler */
502 _bt_service_unregister_event_handler_callback(BT_GATT_MODULE);
504 __bt_service_reset_gatt_data();
507 void _bt_update_adv_handle(const char *sender, int adv_handle)
510 BT_DBG("Sender [%s] Adv handle [%d]", sender, adv_handle);
511 bt_service_app_info_t *info = NULL;
513 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
515 /* Search for a app which has same sender and adv handle as 0 */
516 if (!g_strcmp0(info->sender, sender) && info->adv_handle == 0)
517 info->adv_handle = adv_handle;
521 int _bt_is_sender_gatt_server_with_no_adv(const char *sender, int adv_handle)
524 BT_DBG("Sender [%s] Adv handle [%d]", sender, adv_handle);
525 bt_service_app_info_t *info = NULL;
527 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
529 /* Search for a app which has same sender and adv handle as 0
530 It is possible that same sender but different adv handle */
531 if (!g_strcmp0(info->sender, sender) && info->adv_handle == 0) {
532 //info->adv_handle = adv_handle;
533 return info->instance_id;
539 int _bt_get_allocated_server_instance(const char *sender, int adv_handle, gboolean use_reserved_slot)
542 BT_DBG("Sender [%s] Adv handle [%d]", sender, adv_handle);
543 bt_service_app_info_t *info = NULL;
545 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
548 /* Exact matching of Adv handle + sender combination */
549 if (!g_strcmp0(info->sender, sender) && info->adv_handle == adv_handle)
550 return info->instance_id;
552 if (!g_strcmp0(info->sender, sender) && info->adv_handle == -1)
553 return info->instance_id;
559 char * _bt_get_sender_and_handle(int server_instance, int *adv_handle)
562 bt_service_app_info_t *info = NULL;
564 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
567 if (info->instance_id == server_instance && info->adv_handle != -1) {
568 *adv_handle = info->adv_handle;
569 BT_DBG("Server instance [%d] Adv handle [%d] Sender [%s]", server_instance, *adv_handle, info->sender);
570 return g_strdup(info->sender);
576 void _bt_set_new_adv_data(bluetooth_advertising_data_t *adv, int len, int instance)
580 bt_service_app_info_t *info = NULL;
581 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
584 if (info->instance_id == instance) {
585 memcpy(info->adv_data.data, &adv->data, len);
591 void _bt_set_new_scan_rsp_data(bluetooth_scan_resp_data_t *scan, int len, int instance)
595 bt_service_app_info_t *info = NULL;
596 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
599 if (info->instance_id == instance) {
600 memcpy(info->scan_rsp.data, &scan->data, len);
606 void _bt_get_previous_adv_data(bluetooth_advertising_data_t *adv, int *len, int instance)
609 bt_service_app_info_t *info = NULL;
611 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
614 if (info->instance_id == instance) {
615 memcpy(&adv->data, info->adv_data.data, info->adv_data_len);
616 *len = info->adv_data_len;
622 void _bt_get_previous_scan_rsp_data(bluetooth_scan_resp_data_t *scan, int *len, int instance)
626 bt_service_app_info_t *info = NULL;
628 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
631 if (info->instance_id == instance) {
632 memcpy(&scan->data, info->scan_rsp.data, info->scan_rsp_len);
633 *len = info->scan_rsp_len;
639 static int __bt_do_unregister_gatt_instance(int instance_id)
641 int ret = OAL_STATUS_SUCCESS;
644 BT_INFO("DeAllocate server or client instance ID [%d]", instance_id);
646 /* Reset data: instance_id parameter could be either for GATT Server or for GATT client */
647 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
648 if (numapps[k].instance_id == instance_id) {
649 BT_INFO("This is a GATT server app, unregister: Slot [%d] vacant", k);
650 numapps[k].is_initialized = FALSE;
651 numapps[k].instance_id = -1;
652 numapps[k].adv_handle = 0;
653 numapps[k].adv_instance = -1;
654 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
655 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
656 memset(numapps[k].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
657 memset(numapps[k].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
658 numapps[k].adv_data_len = 0;
659 numapps[k].scan_rsp_len = 0;
661 /* Its a GATT Server Instance */
662 ret = gatts_unregister(instance_id);
663 if (ret != OAL_STATUS_SUCCESS) {
664 BT_ERR("DeAllocate server instance with stack Fail ret: %d", ret);
665 return BLUETOOTH_ERROR_INTERNAL;
668 } else if (numapps[k].client_id == instance_id) {
669 BT_INFO("This is a GATT client app, unregister: Slot [%d] vacant", k);
670 numapps[k].client_id = -1;
671 numapps[k].is_initialized = FALSE;
672 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
673 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
674 memset(&numapps[k].address.addr, 0x00, sizeof(bluetooth_device_address_t));
676 /* Its a GATT Client Instance */
677 ret = gattc_deregister(instance_id);
678 if (ret != OAL_STATUS_SUCCESS) {
679 BT_ERR("DeAllocate GATT Client instance with stack Fail ret: %d", ret);
680 return BLUETOOTH_ERROR_INTERNAL;
685 return BLUETOOTH_ERROR_NONE;
688 static int __bt_unregister_gatt_server_instance(int server_instance)
690 int ret = OAL_STATUS_SUCCESS;
693 /* Unregister the server instance */
694 ret = gatts_unregister(server_instance);
695 if (ret != OAL_STATUS_SUCCESS) {
696 BT_ERR("DeAllocate server instance with stack Fail ret: %d", ret);
697 return BLUETOOTH_ERROR_INTERNAL;
699 BT_INFO("DeAllocated server instance with stack successful..");
702 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
703 if (numapps[k].instance_id == server_instance) {
704 numapps[k].is_initialized = 0;
705 numapps[k].instance_id = -1;
706 numapps[k].adv_handle = 0;
707 numapps[k].adv_instance = -1;
708 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
709 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
710 memset(numapps[k].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
711 memset(numapps[k].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
712 numapps[k].adv_data_len = 0;
713 numapps[k].scan_rsp_len = 0;
717 BT_DBG("Going8 to reset numapp block num [%d]", k);
718 return BLUETOOTH_ERROR_NONE;
721 static void __bt_remove_all_service_handles(bt_service_app_info_t *app_info)
725 int ret = OAL_STATUS_SUCCESS;
728 if (app_info == NULL)
731 count = g_slist_length(app_info->service_handles);
732 BT_INFO("Before handle removal: current count [%d]", count);
734 for (l = app_info->service_handles; l != NULL;) {
739 BT_INFO("Server Handle to be Removed [%d] Instance ID [%d]", *handle, app_info->instance_id);
740 if (_bt_gatt_server_stop_service(app_info->sender, *handle, app_info->instance_id) != BLUETOOTH_ERROR_NONE)
743 ret = gatts_delete_service(app_info->instance_id, *handle);
744 if (ret != OAL_STATUS_SUCCESS) {
745 BT_ERR("ret: %d", ret);
748 app_info->service_handles = g_slist_remove(app_info->service_handles, handle);
751 count = g_slist_length(app_info->service_handles);
752 BT_INFO("After deleting current count [%d]", count);
758 int _bt_unregister_server_instance(const char *sender, int adv_handle)
760 BT_INFO("Unregister Allocated server instance request Sender [%s] Adv handle [%d]", sender, adv_handle);
761 int result = BLUETOOTH_ERROR_NONE;
762 int apps[MAX_APPS_SUPPORTED];
766 memset(&apps, -1, sizeof(apps));
768 if (adv_handle == 0) {
769 BT_DBG("Its a direct GATT Server app request to unregister");
770 /* Unregister server instance for each app with same sender (case: GATT Server with multiple adv handle) */
771 bt_service_app_info_t *info = NULL;
773 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
776 /* Exact matching of sender */
777 if (!g_strcmp0(info->sender, sender)) {
778 BT_INFO("Unregister GATT server instance [%d]", info->instance_id);
779 /* Unregister all service handles with stack */
780 __bt_remove_all_service_handles(info);
782 /* Disable adv if running */
783 BT_INFO("Disable Advertising Adv Handle [%d] sender [%s]",
784 info->adv_handle, sender);
785 _bt_set_advertising(sender, info->adv_handle, FALSE, FALSE);
787 /* Save all instances which need to be unregistered */
788 apps[info->instance_id] = 1;
792 BT_DBG("Its an Internal unregister request by adv application");
793 server_instance = _bt_get_allocated_server_instance(sender, adv_handle, FALSE);
794 BT_DBG("Its an Internal unregister request by adv application: Adv disabled srv instance [%d]", server_instance);
795 if (server_instance == -1) {
796 BT_ERR("No allocated server instance to be removed");
797 return BLUETOOTH_ERROR_INVALID_PARAM;
799 if (!numapps[server_instance].service_handles) {
800 BT_INFO("There are no Service handles with this app, safe to unregister");
801 /* Unregister server instance only if this sender does not have any gatt services in it */
802 result = __bt_unregister_gatt_server_instance(server_instance);
804 numapps[server_instance].adv_handle = 0;
805 memset(numapps[server_instance].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
806 numapps[server_instance].adv_data_len = 0;
807 memset(numapps[server_instance].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
808 numapps[server_instance].scan_rsp_len = 0;
813 /* Unregister all apps one by one */
814 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
816 BT_INFO("Unregister app[%d]", k);
817 /* Unregister server instance */
818 __bt_unregister_gatt_server_instance(k);
825 int _bt_register_server_instance(const char *sender, int adv_handle)
827 int ret = OAL_STATUS_SUCCESS;
828 char *uuid_string = NULL;
833 BT_INFO("###Check on which instance Server instance can be initialized....");
834 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
835 if (numapps[k].is_initialized == 1) {
836 BT_INFO("Instance ID [%d] is already in use..Check next slot", numapps[k].instance_id);
839 BT_INFO("Time to register GATT Server..UUID to be used is [%s] slot [%d]", uuid_list[slot-1], slot);
845 BT_ERR("No Slot if free for GATT Server registration..");
846 return BLUETOOTH_ERROR_REGISTRATION_FAILED;
849 uuid_string = g_malloc0(BT_UUID_STRING_MAX);
850 _bt_string_to_uuid(uuid_list[slot-1], (service_uuid_t*)&uuid);
851 g_strlcpy(uuid_string, uuid_list[slot-1], BT_UUID_STRING_MAX);
852 BT_INFO("Copied UUID string [%s]", uuid_string);
854 /* Register GATT Server */
855 ret = gatts_register(&uuid);
856 if (ret != OAL_STATUS_SUCCESS) {
857 BT_ERR("ret: %d", ret);
859 return BLUETOOTH_ERROR_INTERNAL;
861 BT_INFO("GATT Server registration call successfully accepted by OAL..wait for Instance Initialized event from OAL..");
862 /* Return & wait for GATT Server Instance Initialization event */
863 memset(numapps[slot].sender, 0x00, sizeof(numapps[slot].sender));
864 memset(numapps[slot].uuid, 0x00, sizeof(numapps[slot].uuid));
866 g_strlcpy(numapps[slot].sender, sender, sizeof(numapps[slot].sender));
867 g_strlcpy(numapps[slot].uuid, uuid_string, sizeof(numapps[slot].uuid));
869 numapps[slot].is_initialized = 0; /* Set initialization from app registered callback */
870 numapps[slot].adv_handle = adv_handle;
873 return BLUETOOTH_ERROR_NONE;
877 static void __bt_gatt_handle_pending_request_info(int result,
878 int service_function, void *data, unsigned int size)
882 invocation_info_t *req_info = NULL;
883 ret_if(data == NULL);
885 for (l = _bt_get_invocation_list(); l != NULL; ) {
888 if (req_info == NULL || req_info->service_function != service_function)
891 switch (service_function) {
893 case BT_GATT_SERVER_REGISTER: {
894 bt_service_app_info_t *param = (bt_service_app_info_t*)data;
895 BT_INFO("Sender [%s] Request Sender [%s]", param->sender, req_info->sender);
896 if (!g_strcmp0(req_info->sender, param->sender)) {
897 BT_DBG("GATT Server app found [%s]", req_info->sender);
899 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
900 g_array_append_vals(out_param, ¶m->instance_id, sizeof(int));
901 _bt_service_method_return(req_info->context, out_param, result);
903 g_free(req_info->user_data);
904 _bt_free_info_from_invocation_list(req_info);
905 g_array_free(out_param, TRUE);
909 case BT_GATT_SERVER_START_SERVICE:
910 case BT_GATT_SERVER_DELETE_SERVICE: {
911 bt_service_app_info_t *param = (bt_service_app_info_t*)data;
912 BT_INFO("Sender [%s] Request Sender [%s]", param->sender, req_info->sender);
914 int *saved_instance_id = (int*)req_info->user_data;
915 if (!g_strcmp0(req_info->sender, param->sender) && param->instance_id == *saved_instance_id) {
916 BT_DBG("GATT Server app found [%s] Instance ID [%d] Reply DBUS",
917 req_info->sender, *saved_instance_id);
919 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
920 g_array_append_vals(out_param, &saved_instance_id, sizeof(int));
921 _bt_service_method_return(req_info->context, out_param, result);
923 g_free(req_info->user_data);
924 _bt_free_info_from_invocation_list(req_info);
925 g_array_free(out_param, TRUE);
929 case BT_GATT_SERVER_ADD_SERVICE:
930 case BT_GATT_SERVER_ADD_DESCRIPTOR:
931 case BT_GATT_SERVER_ADD_CHARACTERISTIC: {
932 int *handle = (int*)data;
933 BT_INFO("Characteristic added: Handle [%d]", *handle);
934 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
936 g_array_append_vals(out_param, handle, sizeof(int));
937 _bt_service_method_return(req_info->context, out_param, result);
939 g_free(req_info->user_data);
940 _bt_free_info_from_invocation_list(req_info);
941 g_array_free(out_param, TRUE);
946 case BT_DISCONNECT_LE: {
947 #ifdef TIZEN_GATT_CLIENT
948 char *addr = (char*)req_info->user_data;
949 bluetooth_device_address_t address;
951 if (!g_strcmp0(addr, (char*)data)) {
952 BT_INFO("GATT Client connect-disconnect call pending for app [%s] addr [%s]",
953 req_info->sender, addr);
954 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
955 _bt_convert_addr_string_to_type(address.addr, addr);
957 g_array_append_vals(out_param, (bluetooth_device_address_t*)&address,
958 sizeof(bluetooth_device_address_t));
959 _bt_service_method_return(req_info->context, out_param, result);
961 g_free(req_info->user_data);
962 _bt_free_info_from_invocation_list(req_info);
963 g_array_free(out_param, TRUE);
966 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
968 g_array_append_vals(out_param, (bluetooth_device_address_t*)data,
969 sizeof(bluetooth_device_address_t));
970 _bt_service_method_return(req_info->context, out_param, result);
972 g_free(req_info->user_data);
973 _bt_free_info_from_invocation_list(req_info);
975 g_array_free(out_param, TRUE);
979 #ifdef TIZEN_GATT_CLIENT
980 case BT_GATT_CLIENT_REGISTER: {
981 bt_service_app_info_t *param = (bt_service_app_info_t*)data;
982 BT_INFO("Sender [%s] Request Sender [%s]", param->sender, req_info->sender);
983 if (!g_strcmp0(req_info->sender, param->sender)) {
984 BT_DBG("GATT Client app found [%s] created client ID [%d]",
985 req_info->sender, param->client_id);
987 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
988 g_array_append_vals(out_param, ¶m->client_id, sizeof(int));
989 _bt_service_method_return(req_info->context, out_param, result);
991 g_free(req_info->user_data);
992 _bt_free_info_from_invocation_list(req_info);
993 g_array_free(out_param, TRUE);
997 case BT_GATT_GET_PRIMARY_SERVICES: {
998 bt_services_browse_info_t *param = (bt_services_browse_info_t*)data;
999 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1000 _bt_convert_addr_type_to_string(address,
1001 (unsigned char *)(¶m->device_addr.addr));
1003 BT_INFO("Request Sender [%s] address [%s]", req_info->sender, address);
1005 /* Match address to determine same request */
1006 if (!g_strcmp0((char*)req_info->user_data, address)) {
1007 BT_DBG("GATT Client app found [%s] Remote address [%s]",
1008 req_info->sender, address);
1010 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1011 g_array_append_vals(out_param, param,
1012 sizeof(bt_services_browse_info_t));
1014 //g_array_append_vals(out_param, ¶m->client_id, sizeof(int));
1015 _bt_service_method_return(req_info->context, out_param, result);
1017 g_free(req_info->user_data);
1018 _bt_free_info_from_invocation_list(req_info);
1019 g_array_free(out_param, TRUE);
1024 case BT_GATT_GET_SERVICE_PROPERTIES: {
1025 bt_char_browse_info_t param;
1026 memcpy((void*)¶m, data, sizeof(bt_char_browse_info_t));
1027 //bt_char_browse_info_t *param = (bt_char_browse_info_t*)data;
1028 BT_INFO("Request Sender [%s]", req_info->sender);
1029 bluetooth_gatt_client_svc_prop_info_t *prop = (bluetooth_gatt_client_svc_prop_info_t*)req_info->user_data;
1031 /* Match both address and service properties to determine same request */
1032 if (!memcmp(param.device_addr.addr,
1033 prop->device_address.addr,
1034 sizeof(bluetooth_device_address_t)) &&
1035 !memcmp(param.svc_uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1036 param.svc_inst_id == prop->svc.instance_id) {
1037 BT_INFO("Returning Service properties");
1039 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1040 g_array_append_vals(out_param, ¶m, sizeof(bt_char_browse_info_t));
1041 _bt_service_method_return(req_info->context, out_param, result);
1043 g_free(req_info->user_data);
1044 _bt_free_info_from_invocation_list(req_info);
1045 g_array_free(out_param, TRUE);
1049 case BT_GATT_GET_CHARACTERISTIC_PROPERTIES: {
1050 bt_descriptor_browse_info_t *param = (bt_descriptor_browse_info_t*)data;
1052 BT_INFO("Request Sender [%s]", req_info->sender);
1053 bluetooth_gatt_client_char_prop_info_t *prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
1055 /* Match both address, service properties &char properties to determine same request */
1056 if (!memcmp(param->device_addr.addr,
1057 prop->device_address.addr,
1058 sizeof(bluetooth_device_address_t)) &&
1059 !memcmp(param->svc_uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1060 param->svc_inst_id == prop->svc.instance_id &&
1061 !memcmp(param->char_uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1062 param->char_inst_id == prop->characteristic.instance_id) {
1063 BT_INFO("Returning Characteristic properties");
1064 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1065 g_array_append_vals(out_param, param, sizeof(bt_descriptor_browse_info_t));
1066 _bt_service_method_return(req_info->context, out_param, result);
1068 g_free(req_info->user_data);
1069 _bt_free_info_from_invocation_list(req_info);
1070 g_array_free(out_param, TRUE);
1074 case BT_GATT_WATCH_CHARACTERISTIC: {
1075 bt_gatt_notif_reg_info_t *param = (bt_gatt_notif_reg_info_t*)data;
1076 bluetooth_gatt_client_char_prop_info_t *prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
1077 BT_INFO("Request Sender [%s]", req_info->sender);
1078 /* Match both address, service properties &char properties to determine same request */
1079 if (!memcmp(param->addr.addr,
1080 prop->device_address.addr,
1081 sizeof(bluetooth_device_address_t)) &&
1082 !memcmp(param->svc_uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1083 param->svc_inst == prop->svc.instance_id &&
1084 !memcmp(param->char_uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1085 param->char_inst == prop->characteristic.instance_id) {
1086 BT_INFO("Characteristic Watch Successful: Is registered [%d]",
1087 param->is_registered);
1088 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1089 g_array_append_vals(out_param, param, sizeof(bt_gatt_notif_reg_info_t));
1090 _bt_service_method_return(req_info->context, out_param, result);
1091 g_free(req_info->user_data);
1092 _bt_free_info_from_invocation_list(req_info);
1093 g_array_free(out_param, TRUE);
1097 case BT_GATT_READ_CHARACTERISTIC:
1098 case BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE: {
1099 bluetooth_gatt_client_char_prop_info_t *param = (bluetooth_gatt_client_char_prop_info_t*)data;
1100 BT_INFO("Request Sender [%s]", req_info->sender);
1102 char *addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
1103 bluetooth_gatt_client_char_prop_info_t *prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
1104 _bt_convert_addr_type_to_string(addr, prop->device_address.addr);
1106 if (!memcmp(param, prop, sizeof(bluetooth_gatt_client_char_prop_info_t))) {
1107 BT_INFO("Gatt Char read or write request matched for address [%s]", addr);
1108 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1109 g_array_append_vals(out_param, param, sizeof(bluetooth_gatt_client_char_prop_info_t));
1110 _bt_service_method_return(req_info->context, out_param, result);
1112 g_free(req_info->user_data);
1113 _bt_free_info_from_invocation_list(req_info);
1114 g_array_free(out_param, TRUE);
1119 case BT_GATT_READ_DESCRIPTOR_VALUE:
1120 case BT_GATT_WRITE_DESCRIPTOR_VALUE: {
1121 bluetooth_gatt_client_desc_prop_info_t *param = (bluetooth_gatt_client_desc_prop_info_t*)data;
1122 BT_INFO("Request Sender [%s]", req_info->sender);
1124 char *addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
1125 bluetooth_gatt_client_desc_prop_info_t *prop = (bluetooth_gatt_client_desc_prop_info_t*)req_info->user_data;
1126 _bt_convert_addr_type_to_string(addr, prop->device_address.addr);
1128 if (!memcmp(param, prop, sizeof(bluetooth_gatt_client_desc_prop_info_t))) {
1129 BT_INFO("Descriptor read or write request matched for address [%s]", addr);
1130 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1131 g_array_append_vals(out_param, param, sizeof(bluetooth_gatt_client_desc_prop_info_t));
1132 _bt_service_method_return(req_info->context, out_param, result);
1134 g_free(req_info->user_data);
1135 _bt_free_info_from_invocation_list(req_info);
1136 g_array_free(out_param, TRUE);
1141 case BT_REQ_ATT_MTU: {
1142 char *addr = (char*)req_info->user_data;
1143 bluetooth_device_address_t address;
1145 if (!g_strcmp0(addr, (char*)data)) {
1146 BT_INFO("GATT Client BT_REQ_ATT_MTU call pending for app [%s] addr [%s]",
1147 req_info->sender, addr);
1148 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1149 _bt_convert_addr_string_to_type(address.addr, addr);
1151 g_array_append_vals(out_param, (bluetooth_device_address_t*)&address,
1152 sizeof(bluetooth_device_address_t));
1153 _bt_service_method_return(req_info->context, out_param, result);
1155 g_free(req_info->user_data);
1156 _bt_free_info_from_invocation_list(req_info);
1157 g_array_free(out_param, TRUE);
1168 static void __bt_handle_server_instance_registered(event_gatts_register_t *data)
1170 bt_service_app_info_t *info = NULL;
1172 char *uuid_string = g_malloc0(BT_UUID_STRING_MAX);
1174 _bt_uuid_to_string(&(data->server_uuid), uuid_string);
1175 BT_INFO("Instance ID is Intialized [%d] UUID initialized [%s]", data->server_inst, uuid_string);
1177 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1178 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1181 if (g_strcmp0(info->uuid, uuid_string) == 0) {
1182 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1183 info->is_initialized = TRUE;
1184 info->instance_id = data->server_inst;
1185 info->adv_instance = data->server_inst;
1186 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE, BT_GATT_SERVER_REGISTER,
1187 (void*)info, sizeof(bt_service_app_info_t));
1191 g_free(uuid_string);
1194 static void __bt_handle_gatt_server_service_added(event_gatts_srvc_prm_t *event)
1196 int result = BLUETOOTH_ERROR_NONE;
1200 bt_service_app_info_t *info = NULL;
1203 char *uuid_str = g_malloc0(BT_UUID_STRING_MAX);
1204 _bt_uuid_to_string(&(event->gatt_srvc_id.id.uuid), uuid_str);
1205 BT_INFO("GATT Added Service UUID: [%s] Server Ins [%d] Service hdl [%d] Is Primary [%d]\n",
1206 uuid_str, event->gatt_srvc_stat.server_inst, svc_handle, event->gatt_srvc_id.is_prmry);
1208 if (event->gatt_srvc_stat.status != OAL_STATUS_SUCCESS) {
1209 result = BLUETOOTH_ERROR_INTERNAL;
1210 svc_handle = 0; /* Service handle set to 0 indicates.
1211 0 is reserved by BT SIG, cant be used by app*/
1213 svc_handle = event->gatt_srvc_stat.servic_hndl;
1215 BT_INFO("GATT Added Service Status [%d] Server Ins [%d] Service hdl [%d] Is Primary [%d]\n",
1216 event->gatt_srvc_stat.status, event->gatt_srvc_stat.server_inst, svc_handle, event->gatt_srvc_id.is_prmry);
1218 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1219 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1222 if (info->instance_id == event->gatt_srvc_stat.server_inst) {
1223 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1224 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_ADD_SERVICE,
1225 (int*)&svc_handle, sizeof(int));
1227 /* Add Service Handle */
1228 if (svc_handle > 0) {
1229 handle = g_malloc0(sizeof(int));
1230 *handle = svc_handle;
1231 numapps[event->gatt_srvc_stat.server_inst].service_handles = g_slist_append(numapps[event->gatt_srvc_stat.server_inst].service_handles, handle);
1232 count = g_slist_length(numapps[event->gatt_srvc_stat.server_inst].service_handles);
1233 BT_INFO("Added Service handle [%d] to list..current count [%d]", svc_handle, count);
1242 static void __bt_handle_gatt_server_characteristic_added(event_gatts_srvc_charctr_t *event)
1244 int result = BLUETOOTH_ERROR_NONE;
1246 bt_service_app_info_t *info = NULL;
1248 char *uuid_str = g_malloc0(BT_UUID_STRING_MAX);
1250 BT_INFO("GATT Server Char added status [%d]", event->gatt_srvc_stat.status);
1251 BT_INFO("GATT Server_inst: [%d]", event->gatt_srvc_stat.server_inst);
1252 BT_INFO("GATT Service Handle: [%d]", event->gatt_srvc_stat.servic_hndl);
1253 BT_INFO("GATT Add characteristic Status: [%d]", event->gatt_srvc_stat.status);
1254 BT_INFO("GATT Service characteristic Handle: [%d]", event->charctr_hndl);
1256 if (event->gatt_srvc_stat.status != OAL_STATUS_SUCCESS) {
1257 result = BLUETOOTH_ERROR_INTERNAL;
1258 char_handle = 0; /* characteristic handle set to 0 indicates.
1259 0 is reserved by BT SIG, cant be used by app*/
1261 char_handle = event->charctr_hndl;
1264 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1265 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1268 if (info->instance_id == event->gatt_srvc_stat.server_inst) {
1269 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1270 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_ADD_CHARACTERISTIC,
1271 (int*)&char_handle, sizeof(int));
1277 _bt_uuid_to_string(&(event->charctr_uuid), uuid_str);
1278 BT_INFO("GATT Added Characteristic: UUID: [%s]", uuid_str);
1283 static void __bt_handle_gatt_server_descriptor_added(event_gatts_srvc_descr_t* event)
1285 int result = BLUETOOTH_ERROR_NONE;
1287 bt_service_app_info_t *info = NULL;
1289 char *uuid_str = g_malloc0(BT_UUID_STRING_MAX);
1291 BT_INFO("GATT Server Descriptor added status [%d]", event->gatt_srvc_stat.status);
1292 BT_INFO("GATT Server_inst: [%d]", event->gatt_srvc_stat.server_inst);
1293 BT_INFO("GATT Service Handle: [%d]", event->gatt_srvc_stat.servic_hndl);
1294 BT_INFO("GATT Add Descriptor Status: [%d]", event->gatt_srvc_stat.status);
1295 BT_INFO("GATT Service Descriptor Handle: [%d]", event->descrptr_hndl);
1297 if (event->gatt_srvc_stat.status != OAL_STATUS_SUCCESS) {
1298 result = BLUETOOTH_ERROR_INTERNAL;
1299 desc_handle = 0; /* Service handle set to 0 indicates.
1300 0 is reserved by BT SIG, cant be used by app*/
1302 desc_handle = event->descrptr_hndl;
1304 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1305 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1308 if (info->instance_id == event->gatt_srvc_stat.server_inst) {
1309 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1310 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_ADD_DESCRIPTOR,
1311 (int*)&desc_handle, sizeof(int));
1316 _bt_uuid_to_string(&(event->descrptr_uuid), uuid_str);
1317 BT_INFO("GATT Added Descriptor: UUID: [%s]", uuid_str);
1322 static void __bt_handle_gatt_server_service_started(event_gatts_srvc_t *event)
1324 bt_service_app_info_t *info = NULL;
1325 int result = BLUETOOTH_ERROR_NONE;
1327 BT_INFO("GATT Server Service Started..");
1330 BT_INFO("GATT Server_inst: [%d]", event->server_inst);
1331 BT_INFO("GATT Service Handle: [%d]", event->servic_hndl);
1332 BT_INFO("GATT Service Started Status: [%d]", event->status);
1334 if (event->status != OAL_STATUS_SUCCESS) {
1335 BT_ERR("GATT Server Service Start Failed Err: [%d]", event->status);
1336 result = BLUETOOTH_ERROR_INTERNAL;
1339 /* Check if the just registered Instance ID belongs to requester */
1340 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1343 if (info->instance_id == event->server_inst) {
1344 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1345 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_START_SERVICE,
1346 (void*)info, sizeof(bt_service_app_info_t));
1352 static void __bt_handle_gatt_server_service_stopped(event_gatts_srvc_t *event)
1354 int result = BLUETOOTH_ERROR_NONE;
1355 bt_service_app_info_t *info = NULL;
1357 BT_INFO("GATT Server Service Stopped..");
1359 BT_INFO("GATT Server_inst: [%d]", event->server_inst);
1360 BT_INFO("GATT Service Handle: [%d]", event->servic_hndl);
1361 BT_INFO("GATT Service Stopped Status: [%d]", event->status);
1363 if (event->status != OAL_STATUS_SUCCESS) {
1364 BT_ERR("GATT Server Service Stop Failed Err: [%d]", event->status);
1365 result = BLUETOOTH_ERROR_INTERNAL;
1368 /* Check if the just registered Instance ID belongs to requester */
1369 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1372 if (info->instance_id == event->server_inst) {
1373 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1374 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_STOP_SERVICE,
1375 (void*)info, sizeof(bt_service_app_info_t));
1381 static void __bt_handle_gatt_server_service_deleted(event_gatts_srvc_t *event)
1383 int result = BLUETOOTH_ERROR_NONE;
1384 bt_service_app_info_t *info = NULL;
1386 BT_INFO("GATT Server Service Deleted..");
1388 BT_INFO("GATT Server_inst: [%d]", event->server_inst);
1389 BT_INFO("GATT Service Handle: [%d]", event->servic_hndl);
1390 BT_INFO("GATT Service Deleted Status: [%d]", event->status);
1392 if (event->status != OAL_STATUS_SUCCESS) {
1393 BT_ERR("GATT Server Service Delete Failed Err: [%d]", event->status);
1394 result = BLUETOOTH_ERROR_INTERNAL;
1397 /* Check if the just registered Instance ID belongs to requester */
1398 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1401 if (info->instance_id == event->server_inst) {
1402 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1403 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_DELETE_SERVICE,
1404 (void*)info, sizeof(bt_service_app_info_t));
1410 struct gatt_client_info_t *_bt_find_remote_gatt_client_info(char *address)
1413 struct gatt_client_info_t *info = NULL;
1414 for (l = gatt_client_info_list; l != NULL; l = g_slist_next(l)) {
1415 info = (struct gatt_client_info_t*)l->data;
1419 if (!g_strcmp0(info->addr, address)) {
1420 BT_DBG("Remote GATT client found addr[%s]", info->addr);
1427 static struct gatt_client_info_t *__bt_find_remote_gatt_client_info_from_conn_id(int conn_id)
1430 struct gatt_client_info_t *info = NULL;
1432 for (l = gatt_client_info_list; l != NULL; l = g_slist_next(l)) {
1433 info = (struct gatt_client_info_t*)l->data;
1437 if (info->connection_id == conn_id) {
1438 BT_INFO("Remote GATT client found addr[%s]", info->addr);
1445 #ifdef TIZEN_GATT_CLIENT
1446 struct gatt_server_info_t *_bt_find_remote_gatt_server_info(char *address)
1449 struct gatt_server_info_t *info = NULL;
1450 for (l = gatt_server_info_list; l != NULL; l = g_slist_next(l)) {
1451 info = (struct gatt_server_info_t*)l->data;
1455 if (!g_strcmp0(info->addr, address)) {
1456 BT_DBG("Remote GATT Server found addr[%s]", info->addr);
1463 static struct gatt_out_conn_info_t* __bt_find_gatt_outgoing_conn_info(char *address)
1466 struct gatt_out_conn_info_t *info = NULL;
1467 for (l = outgoing_gatt_conn_list; l != NULL; l = g_slist_next(l)) {
1468 info = (struct gatt_out_conn_info_t*)l->data;
1472 if (!g_strcmp0(info->addr, address)) {
1473 BT_INFO("Outgoing connection info found addr[%s]", info->addr);
1480 static struct gatt_server_info_t *__bt_find_remote_gatt_server_info_from_client_if(int client_if)
1483 struct gatt_server_info_t *info = NULL;
1485 for (l = gatt_server_info_list; l != NULL; l = g_slist_next(l)) {
1486 info = (struct gatt_server_info_t*)l->data;
1490 if (info->client_id == client_if) {
1491 BT_INFO("Remote GATT server found addr[%s]", info->addr);
1502 #ifdef TIZEN_GATT_CLIENT
1503 static void __bt_handle_gatt_server_connection_state(event_gatts_conn_t *event)
1505 int result = BLUETOOTH_ERROR_NONE;
1506 struct gatt_client_info_t *client_info = NULL;
1507 struct gatt_server_info_t *server_info = NULL;
1508 bluetooth_device_address_t dev_addr;
1509 GVariant *param = NULL;
1512 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1514 memcpy(dev_addr.addr, event->address.addr, 6);
1516 /* REPLY dbus Context to application which called BT_CONNECT_LE. There is status
1518 _bt_convert_addr_type_to_string(address,
1519 (unsigned char *)dev_addr.addr);
1521 if (event->status != OAL_STATUS_SUCCESS)
1522 result = BLUETOOTH_ERROR_INTERNAL;
1524 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
1525 address, BT_ADDRESS_STRING_SIZE);
1527 BT_INFO("GATT Server Connedted: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1528 address, event->server_inst, event->conn_id);
1531 /* Check if device is already in connected list */
1532 client_info = _bt_find_remote_gatt_client_info(address);
1535 BT_INFO("Conn Info absent: But no need to Send Local GATT Server Connected event to apps");
1537 param = g_variant_new("(is)", result, address);
1539 /* Send event to application */
1540 _bt_send_event(BT_DEVICE_EVENT,
1541 BLUETOOTH_EVENT_GATT_SERVER_CONNECTED, /* Local device is GATT server */
1544 /* Save client connection info */
1545 client_info = g_new0(struct gatt_client_info_t, 1);
1546 client_info->addr = g_strdup(address);
1547 BT_INFO("Added GATT client addr[%s]", client_info->addr);
1548 client_info->connection_id = event->conn_id;
1549 client_info->instance_id = event->server_inst;
1550 gatt_client_info_list = g_slist_append(gatt_client_info_list, client_info);
1551 BT_INFO("Total num of connected Remote GATT Clients [%d]", g_slist_length(gatt_client_info_list));
1553 /* Save server connection info */
1554 server_info = g_new0(struct gatt_server_info_t, 1);
1555 server_info->addr = g_strdup(address);
1556 server_info->client_id = -1;
1557 BT_INFO("Added GATT server addr[%s]", server_info->addr);
1558 server_info->connection_id = event->conn_id;
1559 gatt_server_info_list = g_slist_append(gatt_server_info_list, server_info);
1560 BT_INFO("Total num of connected Remote GATT Servers [%d]", g_slist_length(gatt_server_info_list));
1562 ret = gattc_add_connection_info((bt_address_t *)&dev_addr, event->conn_id, event->server_inst);
1563 if (ret != OAL_STATUS_SUCCESS) {
1564 BT_ERR("gattc register server instance failed");
1568 __bt_add_mtu_gatt_device(address);
1573 /* GATT Server Dis connected */
1574 static void __bt_handle_gatt_server_disconnection_state(event_gatts_conn_t *event)
1576 int result = BLUETOOTH_ERROR_NONE;
1577 struct gatt_client_info_t *client_info = NULL;
1578 struct gatt_server_info_t *server_info = NULL;
1579 bluetooth_device_address_t dev_addr;
1580 GVariant *param = NULL;
1581 char address[BT_ADDRESS_STRING_SIZE];
1583 memcpy(dev_addr.addr, event->address.addr, 6);
1585 /* REPLY dbus Context to application which called BT_DISCONNECT_LE. There is status
1587 _bt_convert_addr_type_to_string(address,
1588 (unsigned char *)dev_addr.addr);
1590 if (event->status != OAL_STATUS_SUCCESS)
1591 result = BLUETOOTH_ERROR_INTERNAL;
1593 if (NULL == _bt_get_request_info_data(BT_DISCONNECT_LE, address)) {
1594 if (NULL != _bt_get_request_info_data(BT_CONNECT_LE, address)) {
1595 result = BLUETOOTH_ERROR_INTERNAL;
1597 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
1598 address, BT_ADDRESS_STRING_SIZE);
1600 BT_ERR("Failed to connect Local GATT Server Remote Client addr[%s]", address);
1604 __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE,
1605 address, BT_ADDRESS_STRING_SIZE);
1608 BT_INFO("Local GATT Server DisConnected: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1609 address, event->server_inst, event->conn_id);
1610 /* Remove Connection info */
1611 client_info = _bt_find_remote_gatt_client_info(address);
1613 BT_INFO("No need to Send Local GATT Server Disconnected event to apps, just remove remote client info");
1615 param = g_variant_new("(is)", result, address);
1616 /* Send event to application */
1617 _bt_send_event(BT_DEVICE_EVENT,
1618 BLUETOOTH_EVENT_GATT_SERVER_DISCONNECTED, /* Local device is GATT server */
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 client info from List */
1629 gatt_client_info_list = g_slist_remove(gatt_client_info_list, client_info);
1630 BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
1631 g_free(client_info->addr);
1632 g_free(client_info);
1635 __bt_remove_mtu_gatt_device(address);
1639 static void __bt_handle_gatt_server_connection_state(event_gatts_conn_t *event)
1641 int result = BLUETOOTH_ERROR_NONE;
1642 struct gatt_client_info_t *conn_info = NULL;
1643 bt_service_app_info_t *info = NULL;
1644 bluetooth_device_address_t dev_addr;
1645 GVariant *param = NULL;
1648 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1650 memcpy(dev_addr.addr, event->address.addr, 6);
1653 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1654 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1657 if (info->instance_id == event->server_inst) {
1658 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1659 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
1660 (bluetooth_device_address_t*)&dev_addr, sizeof(bluetooth_device_address_t));
1666 /* Send event to BT-API */
1667 _bt_convert_addr_type_to_string(address,
1668 (unsigned char *)dev_addr.addr);
1670 BT_INFO("GATT Server Connedted: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1671 address, event->server_inst, event->conn_id);
1674 /* Check if device is already in connected list */
1675 conn_info = _bt_find_remote_gatt_client_info(address);
1678 param = g_variant_new("(is)", result, address);
1679 /* Send event to application */
1680 _bt_send_event(BT_GATT_SERVER_EVENT,
1681 BLUETOOTH_EVENT_GATT_CONNECTED,
1684 /* Save Connection info */
1685 conn_info = g_new0(struct gatt_client_info_t, 1);
1686 conn_info->addr = g_strdup(address);
1687 BT_INFO("Added GATT client addr[%s]", conn_info->addr);
1688 conn_info->connection_id = event->conn_id;
1689 conn_info->instance_id = event->server_inst;
1690 gatt_client_info_list = g_slist_append(gatt_client_info_list, conn_info);
1691 BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
1697 /* GATT Server Dis connected */
1698 static void __bt_handle_gatt_server_disconnection_state(event_gatts_conn_t *event)
1700 int result = BLUETOOTH_ERROR_NONE;
1701 struct gatt_client_info_t *conn_info = NULL;
1702 bluetooth_device_address_t dev_addr;
1703 bt_service_app_info_t *info = NULL;
1704 GVariant *param = NULL;
1707 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1709 memcpy(dev_addr.addr, event->address.addr, 6);
1711 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1712 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1715 if (info->instance_id == event->server_inst) {
1716 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1717 __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE,
1718 (bluetooth_device_address_t*)&dev_addr, sizeof(bluetooth_device_address_t));
1724 /* Send event to BT-API */
1725 _bt_convert_addr_type_to_string(address,
1726 (unsigned char *)dev_addr.addr);
1728 BT_INFO("GATT Server DisConnected: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1729 address, event->server_inst, event->conn_id);
1731 param = g_variant_new("(is)", result, address);
1732 /* Send event to application */
1733 _bt_send_event(BT_GATT_SERVER_EVENT,
1734 BLUETOOTH_EVENT_GATT_DISCONNECTED,
1737 /* Remove Connection info */
1738 conn_info = _bt_find_remote_gatt_client_info(address);
1740 BT_INFO("Remove GATT client info from List..");
1741 /* Remove info from List */
1742 gatt_client_info_list = g_slist_remove(gatt_client_info_list, conn_info);
1743 BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
1744 g_free(conn_info->addr);
1753 static void __bt_handle_gatt_server_acquire_write_requested(event_gatts_srvc_acquire_attr_t *event)
1755 GVariant *param = NULL;
1756 int result = BLUETOOTH_ERROR_NONE;
1757 struct gatt_server_req_info *req_info = NULL;
1758 bluetooth_device_address_t dev_addr;
1759 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1761 BT_INFO("GATT Server ACQUIRE Write Req Connection ID: [%d]", event->attr_trans.conn_id);
1762 BT_INFO("GATT Server ACQUIRE Write Req Transaction ID:[%d]", event->attr_trans.trans_id);
1763 BT_INFO("GATT Server ACQUIRE Write Req Attribute Handle: [%d]", event->attr_trans.attr_handle);
1765 //address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1766 memcpy(dev_addr.addr, event->address.addr, 6);
1768 req_info = g_new0(struct gatt_server_req_info, 1);
1769 req_info->request_id = event->attr_trans.trans_id;
1770 req_info->attribute_handle = event->attr_trans.attr_handle;
1771 req_info->connection_id = event->attr_trans.conn_id;
1772 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_ACQUIRE_WRITE;
1773 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1775 _bt_convert_addr_type_to_string(address,
1776 (unsigned char *)dev_addr.addr);
1778 param = g_variant_new("(iiiiiis)", result,
1779 event->attr_trans.conn_id,
1780 event->attr_trans.trans_id,
1781 event->attr_trans.attr_handle,
1782 event->mtu, event->attr_trans.offset, address);
1783 BT_INFO("GATT Server ACQUIRE Write Req Attribute remote address : [%s]", address);
1785 _bt_send_event(BT_GATT_SERVER_EVENT,
1786 BLUETOOTH_EVENT_GATT_SERVER_ACQUIRE_WRITE,
1791 static void __bt_handle_gatt_server_acquire_notify_requested(event_gatts_srvc_acquire_attr_t *event)
1793 GVariant *param = NULL;
1794 int result = BLUETOOTH_ERROR_NONE;
1795 struct gatt_server_req_info *req_info = NULL;
1796 bluetooth_device_address_t dev_addr;
1797 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1799 BT_INFO("GATT Server ACQUIRE Notify Req Connection ID: [%d]", event->attr_trans.conn_id);
1800 BT_INFO("GATT Server ACQUIRE Notify Req Transaction ID:[%d]", event->attr_trans.trans_id);
1801 BT_INFO("GATT Server ACQUIRE Notify Req Attribute Handle: [%d]", event->attr_trans.attr_handle);
1802 BT_INFO("GATT Server ACQUIRE notify Req address : [%s]", address);
1804 memcpy(dev_addr.addr, event->address.addr, 6);
1805 _bt_convert_addr_type_to_string(address,
1806 (unsigned char *)dev_addr.addr);
1807 BT_INFO("GATT Server ACQUIRE notify Req remote address : [%s]", address);
1809 req_info = g_new0(struct gatt_server_req_info, 1);
1810 req_info->request_id = event->attr_trans.trans_id;
1811 req_info->attribute_handle = event->attr_trans.attr_handle;
1812 req_info->connection_id = event->attr_trans.conn_id;
1813 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_ACQUIRE_NOTIFY;
1814 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1816 param = g_variant_new("(iiiiiis)", result,
1817 event->attr_trans.conn_id,
1818 event->attr_trans.trans_id,
1819 event->attr_trans.attr_handle,
1820 event->mtu, event->attr_trans.offset,
1823 BT_INFO("GATT Server ACQUIRE Notify Req Attribute : ");
1825 _bt_send_event(BT_GATT_SERVER_EVENT,
1826 BLUETOOTH_EVENT_GATT_SERVER_ACQUIRE_NOTIFY,
1830 static void __bt_handle_gatt_server_write_requested(event_gatts_srvc_write_attr_t *event)
1833 bluetooth_device_address_t dev_addr;
1834 GVariant *param = NULL;
1835 int result = BLUETOOTH_ERROR_NONE;
1837 gboolean is_prepare_write;
1839 GVariant *data = NULL;
1841 struct gatt_server_req_info *req_info = NULL;
1842 BT_INFO("GATT Server Write Requested");
1844 memcpy(dev_addr.addr, event->address.addr, 6);
1846 BT_INFO("GATT Server Write Req Connection ID: [%d]", event->attr_trans.conn_id);
1847 BT_INFO("GATT Server Write Req Transaction ID:[%d]", event->attr_trans.trans_id);
1848 BT_INFO("GATT Server Write Req Attribute Handle: [%d]", event->attr_trans.attr_handle);
1849 BT_INFO("GATT Server Write Req Attribute Offset: [%d]", event->attr_trans.offset);
1850 BT_INFO("GATT Server Write Req value length [%d]", event->length);
1851 BT_INFO("GATT Server Write Req needs response: [%d]", event->need_rsp);
1852 BT_INFO("GATT Server Write Req Is Prep: [%d]", event->is_prep);
1854 if (event->length <= 0) {
1855 BT_INFO("GATT Server write requested, but length of attr value is 0");
1859 need_resp = event->need_rsp;
1860 is_prepare_write = event->is_prep;
1862 write_val = g_memdup(&event->value[0], event->length);
1864 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1865 _bt_convert_addr_type_to_string(address,
1866 (unsigned char *)dev_addr.addr);
1868 BT_INFO("GATT Server Write Request from remote client [%s]", address);
1870 if (event->length > 0) {
1871 for (i = 0; i < event->length; i++)
1872 BT_DBG("Data[%d] = [0x%x]", i, event->value[i]);
1874 /* Save Write Request Info */
1875 req_info = g_new0(struct gatt_server_req_info, 1);
1876 req_info->request_id = event->attr_trans.trans_id;
1877 req_info->attribute_handle = event->attr_trans.attr_handle;
1878 req_info->connection_id = event->attr_trans.conn_id;
1879 req_info->addr = address;
1880 req_info->offset = event->attr_trans.offset;
1881 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_WRITE;
1882 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1884 data = g_variant_new_from_data(
1885 G_VARIANT_TYPE_BYTESTRING,
1890 param = g_variant_new("(iiiiiibbsn@ay)", result,
1891 event->attr_trans.conn_id,
1892 event->attr_trans.trans_id,
1893 event->attr_trans.attr_handle,
1894 event->attr_trans.offset,
1902 _bt_send_event(BT_GATT_SERVER_EVENT,
1903 BLUETOOTH_EVENT_GATT_SERVER_VALUE_CHANGED,
1909 static void __bt_handle_gatt_server_read_requested(event_gatts_srvc_read_attr_t *event)
1911 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1912 bluetooth_device_address_t dev_addr;
1913 int result = BLUETOOTH_ERROR_NONE;
1914 struct gatt_server_req_info *req_info = NULL;
1915 GVariant *param = NULL;
1917 BT_INFO("GATT Server Read Requested");
1919 memcpy(dev_addr.addr, event->address.addr, 6);
1921 BT_INFO("GATT Server-Client Connection ID: [%d]", event->attr_trans.conn_id);
1922 BT_INFO("GATT Server-Client Transaction ID: [%d]", event->attr_trans.trans_id);
1923 BT_INFO("GATT Server Attribute Handle: [%d]", event->attr_trans.attr_handle);
1924 BT_INFO("GATT Server Attribute Offset for read: [%d]", event->attr_trans.offset);
1925 BT_INFO("GATT Server Attribute is long: [%d]", event->is_long);
1927 is_long = event->is_long;
1929 /* Save Read Request Info */
1930 req_info = g_new0(struct gatt_server_req_info, 1);
1931 req_info->request_id = event->attr_trans.trans_id;
1932 req_info->attribute_handle = event->attr_trans.attr_handle;
1933 req_info->connection_id = event->attr_trans.conn_id;
1934 req_info->addr = address;
1935 req_info->offset = event->attr_trans.offset;
1936 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_READ;
1937 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1939 /* Send event to BT-API */
1940 _bt_convert_addr_type_to_string(address,
1941 (unsigned char *)dev_addr.addr);
1943 BT_INFO("GATT Server Read Request from remote client [%s]", address);
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;
2048 BT_INFO("GATT Server MTU changed event");
2050 conn_info = __bt_find_remote_gatt_client_info_from_conn_id(event->conn_id);
2051 if (conn_info == NULL) {
2052 BT_ERR("Cant find connection Information");
2055 BT_INFO("Got connection Info GATT client [%s] MTU Size [%d]",
2056 conn_info->addr, event->mtu_size);
2058 __bt_update_mtu_gatt_device(conn_info->addr, event->mtu_size);
2060 param = g_variant_new("(isqy)",
2066 /* Send event to BT-API */
2067 _bt_send_event(BT_GATT_SERVER_EVENT,
2068 BLUETOOTH_EVENT_GATT_ATT_MTU_CHANGED,
2072 static void __bt_gatt_event_handler(int event_type, gpointer event_data)
2074 BT_INFO("OAL event = 0x%x, \n", event_type);
2075 switch (event_type) {
2076 case OAL_EVENT_BLE_SERVER_INSTANCE_INITIALISED: {
2077 BT_INFO("OAL Event: Server Instance Registered");
2078 /* GATT Server Registered event is handled in MAIN thread context */
2079 __bt_handle_server_instance_registered((event_gatts_register_t *)event_data);
2082 case OAL_EVENT_GATTS_SERVICE_ADDED: {
2083 BT_INFO("OAL Event: GATT Service added");
2084 __bt_handle_gatt_server_service_added((event_gatts_srvc_prm_t *)event_data);
2087 case OAL_EVENT_GATTS_CHARACTERISTIC_ADDED: {
2088 BT_INFO("OAL Event: GATT characteristic added");
2089 __bt_handle_gatt_server_characteristic_added((event_gatts_srvc_charctr_t *)event_data);
2092 case OAL_EVENT_GATTS_DESCRIPTOR_ADDED: {
2093 BT_INFO("OAL Event: GATT descriptor added");
2094 __bt_handle_gatt_server_descriptor_added((event_gatts_srvc_descr_t *)event_data);
2097 case OAL_EVENT_GATTS_SERVICE_STARTED: {
2098 BT_INFO("OAL Event: GATT Service started");
2099 __bt_handle_gatt_server_service_started((event_gatts_srvc_t *)event_data);
2102 case OAL_EVENT_GATTS_SERVICE_STOPED: {
2103 BT_INFO("OAL Event: GATT Service stopped");
2104 __bt_handle_gatt_server_service_stopped((event_gatts_srvc_t *)event_data);
2107 case OAL_EVENT_GATTS_SERVICE_DELETED: {
2108 BT_INFO("OAL Event: GATT Service deleted");
2109 __bt_handle_gatt_server_service_deleted((event_gatts_srvc_t *) event_data);
2112 case OAL_EVENT_GATTS_CONNECTION_COMPLETED: {
2113 BT_INFO("OAL Event: GATT Server Connected");
2114 __bt_handle_gatt_server_connection_state((event_gatts_conn_t *)event_data);
2117 case OAL_EVENT_GATTS_DISCONNECTION_COMPLETED: {
2118 BT_INFO("OAL Event: GATT Server Disconnected");
2119 __bt_handle_gatt_server_disconnection_state((event_gatts_conn_t *)event_data);
2122 case OAL_EVENT_GATTS_REQUEST_READ: {
2123 BT_INFO("OAL Event: GATT Server Read Request");
2124 __bt_handle_gatt_server_read_requested((event_gatts_srvc_read_attr_t *)event_data);
2127 case OAL_EVENT_GATTS_REQUEST_WRITE: {
2128 BT_INFO("OAL Event: GATT Server Write Request");
2129 __bt_handle_gatt_server_write_requested((event_gatts_srvc_write_attr_t *)event_data);
2132 case OAL_EVENT_GATTS_REQUEST_ACQUIRE_WRITE: {
2133 BT_INFO("OAL Event: GATT Server Acquire Write Request");
2134 __bt_handle_gatt_server_acquire_write_requested((event_gatts_srvc_acquire_attr_t*)event_data);
2137 case OAL_EVENT_GATTS_REQUEST_ACQUIRE_NOTIFY: {
2138 BT_INFO("OAL Event: GATT ServerAcquire Notify Request");
2139 __bt_handle_gatt_server_acquire_notify_requested((event_gatts_srvc_acquire_attr_t*)event_data);
2142 case OAL_EVENT_GATTS_IND_CONFIRM: {
2143 BT_INFO("OAL Event: GATT Server Indication confirmed");
2144 __bt_handle_gatt_server_indicate_confirmed((event_gatts_ind_cnfrm_t *)event_data);
2147 case OAL_EVENT_GATTS_NOTIFICATION: { /* Tizen Platform Specific */
2148 BT_INFO("OAL Event: GATT Server DisConnected");
2149 __bt_handle_gatt_server_notification_changed((event_gatts_notif_t *)event_data);
2152 case OAL_EVENT_GATTS_MTU_CHANGED: {
2153 BT_INFO("OAL Event: GATT Server MTU changed event callback");
2154 __bt_handle_gatt_mtu_changed_event((event_gatts_mtu_changed_t *)event_data);
2157 #ifdef TIZEN_GATT_CLIENT
2158 case OAL_EVENT_GATTC_REGISTRATION: {
2159 BT_INFO("OAL Event: GATT Client instance Registered");
2160 __bt_handle_client_instance_registered((event_gattc_register_t *) event_data);
2163 case OAL_EVENT_GATTC_CONNECTION_COMPLETED: {
2164 BT_INFO("OAL Event: GATT Client Connected");
2165 __bt_handle_client_connected((event_gattc_conn_t *) event_data);
2168 case OAL_EVENT_GATTC_DISCONNECTION_COMPLETED: {
2169 BT_INFO("OAL Event: GATT Client DisConnected");
2170 __bt_handle_client_disconnected((event_gattc_conn_t *) event_data);
2173 case OAL_EVENT_GATTC_SERVICE_SEARCH_RESULT: {
2174 BT_INFO("OAL Event: GATT Client Service Search Result");
2175 __bt_handle_client_service_search_result((event_gattc_service_result_t *) event_data);
2178 case OAL_EVENT_GATTC_SERVICE_SEARCH_DONE: {
2179 BT_INFO("OAL Event: GATT Client Service Completed");
2180 __bt_handle_client_service_search_completed((event_gattc_conn_status_t *) event_data);
2183 case OAL_EVENT_GATTC_CHARAC_SERACH_RESULT: {
2184 BT_INFO("OAL Event: GATT Client Characteristic Search Result");
2185 __bt_handle_client_characteristic_search_result((event_gattc_characteristic_result_t *) event_data);
2188 case OAL_EVENT_GATTC_DESC_SERACH_RESULT: {
2189 BT_INFO("OAL Event: GATT Client Descriptor Search Result");
2190 __bt_handle_client_descriptor_search_result((event_gattc_descriptor_result_t *) event_data);
2193 case OAL_EVENT_GATTC_READ_CHARAC: {
2194 BT_INFO("OAL Event: GATT Client Characteristic Read Data");
2195 __bt_handle_client_characteristic_read_data((event_gattc_read_data *) event_data);
2198 case OAL_EVENT_GATTC_READ_DESCR: {
2199 BT_INFO("OAL Event: GATT Client Descriptor Read Data");
2200 __bt_handle_client_descriptor_read_data((event_gattc_read_data *) event_data);
2203 case OAL_EVENT_GATTC_WRITE_CHARAC: {
2204 BT_INFO("OAL Event: GATT Client Characteristic Write Data");
2205 __bt_handle_client_characteristic_write_data((event_gattc_write_data *) event_data);
2208 case OAL_EVENT_GATTC_WRITE_DESCR: {
2209 BT_INFO("OAL Event: GATT Client Descriptor Write Data");
2210 __bt_handle_client_descriptor_write_data((event_gattc_write_data *) event_data);
2213 case OAL_EVENT_DEVICE_LE_DISCONNECTED: {
2214 BT_INFO("OAL Event: LE device disconnected");
2215 __bt_hanlde_le_device_disconnection((event_dev_conn_status_t *)event_data);
2218 case OAL_EVENT_GATTC_NOTIFICATION_REGISTERED: {
2219 BT_INFO("OAL Event: GATT Client Notification Registered");
2220 __bt_handle_client_notification_registered((event_gattc_regdereg_notify_t *) event_data, TRUE);
2223 case OAL_EVENT_GATTC_NOTIFICATION_DEREGISTERED: {
2224 BT_INFO("OAL Event: GATT Client Notification Registered");
2225 __bt_handle_client_notification_registered((event_gattc_regdereg_notify_t *) event_data, FALSE);
2228 case OAL_EVENT_GATTC_NOTIFY_DATA: {
2229 BT_INFO("OAL Event: GATT Client Notification Data");
2230 __bt_handle_client_notification_data((event_gattc_notify_data *) event_data);
2233 case OAL_EVENT_GATTC_SERVICE_CHANGED_IND: {
2234 BT_INFO("OAL Event: GATT Client service changed indication");
2235 __bt_handle_client_service_changed_ind((event_gattc_service_changed_data *)event_data);
2238 case OAL_EVENT_GATTC_MTU_EXCHANGE_COMPLETED: {
2239 BT_INFO("OAL Event: GATT Client MTU Exchange Complete");
2240 __bt_handle_client_mtu_exchange_completed((event_gattc_mtu_configured_t *) event_data);
2249 int _bt_gatt_server_add_service(char *sender, int service_type,
2250 int num_handles, char *svc_uuid, int instance_id)
2252 BT_CHECK_PARAMETER(svc_uuid, return);
2253 BT_CHECK_PARAMETER(sender, return);
2254 int ret = OAL_STATUS_SUCCESS;
2256 oal_gatt_srvc_id_t svc_data;
2258 svc_data.is_prmry = service_type;
2259 svc_data.id.inst_id = instance_id;
2261 BT_INFO("Service UUID [%s] Num handles [%d] Instance ID [%d]", svc_uuid, num_handles, instance_id);
2262 _bt_string_to_uuid(svc_uuid, (service_uuid_t*)&svc_data.id.uuid);
2264 ret = gatts_add_service(instance_id, &svc_data, num_handles);
2265 if (ret != OAL_STATUS_SUCCESS) {
2266 BT_ERR("ret: %d", ret);
2267 return BLUETOOTH_ERROR_INTERNAL;
2270 return BLUETOOTH_ERROR_NONE;
2274 int _bt_gatt_server_add_included_service(char *sender, int instance_id,
2275 int service_handle, int included_svc_handle)
2277 BT_CHECK_PARAMETER(sender, return);
2278 int ret = OAL_STATUS_SUCCESS;
2280 ret = gatts_add_included_services(instance_id, service_handle, included_svc_handle);
2281 if (ret != OAL_STATUS_SUCCESS) {
2282 BT_ERR("ret: %d", ret);
2283 return BLUETOOTH_ERROR_INTERNAL;
2285 return BLUETOOTH_ERROR_NONE;
2288 int _bt_gatt_server_add_characteristic(char *sender, char *char_uuid,
2289 bluetooth_gatt_server_attribute_params_t *param)
2291 BT_CHECK_PARAMETER(char_uuid, return);
2292 BT_CHECK_PARAMETER(sender, return);
2293 BT_CHECK_PARAMETER(param, return);
2294 int ret = OAL_STATUS_SUCCESS;
2296 oal_uuid_t uuid = {{0} };
2298 BT_INFO("Char UUID [%s] Instance ID [%d]", char_uuid, param->instance_id);
2299 _bt_string_to_uuid(char_uuid, (service_uuid_t*)&uuid);
2301 BT_INFO("Char permission From API [0x%x]", param->permissions);
2303 ret = gatts_add_characteristics(param->instance_id, param->service_handle, &uuid,
2304 param->properties, (int)param->permissions);
2305 if (ret != OAL_STATUS_SUCCESS) {
2306 BT_ERR("ret: %d", ret);
2307 return BLUETOOTH_ERROR_INTERNAL;
2309 return BLUETOOTH_ERROR_NONE;
2312 int _bt_gatt_server_add_descriptor(char *sender, char *desc_uuid,
2313 bt_gatt_permission_t *param, int service_handle, int instance_id)
2315 BT_CHECK_PARAMETER(desc_uuid, return);
2316 BT_CHECK_PARAMETER(sender, return);
2317 BT_CHECK_PARAMETER(param, return);
2318 int ret = OAL_STATUS_SUCCESS;
2320 oal_uuid_t uuid = {{0} };
2322 BT_INFO("Descriptor UUID [%s] Instance ID [%d] Service handle [%d]",
2323 desc_uuid, service_handle, instance_id);
2325 _bt_string_to_uuid(desc_uuid, (service_uuid_t*)&uuid);
2327 BT_INFO("Descriptor permission From API [0x%x]", *param);
2328 ret = gatts_add_descriptor(instance_id, service_handle, &uuid, (int)*param);
2330 if (ret != OAL_STATUS_SUCCESS) {
2331 BT_ERR("ret: %d", ret);
2332 return BLUETOOTH_ERROR_INTERNAL;
2334 return BLUETOOTH_ERROR_NONE;
2337 int _bt_gatt_server_start_service(char *sender, int service_handle, int instance_id)
2339 BT_CHECK_PARAMETER(sender, return);
2340 int ret = OAL_STATUS_SUCCESS;
2342 ret = gatts_start_service(instance_id, service_handle, BT_GATT_TRANSPORT_LE);
2343 if (ret != OAL_STATUS_SUCCESS) {
2344 BT_ERR("ret: %d", ret);
2345 return BLUETOOTH_ERROR_INTERNAL;
2347 return BLUETOOTH_ERROR_NONE;
2350 int _bt_gatt_server_stop_service(char *sender, int service_handle, int instance_id)
2352 BT_CHECK_PARAMETER(sender, return);
2353 int ret = OAL_STATUS_SUCCESS;
2355 ret = gatts_stop_service(instance_id, service_handle);
2356 if (ret != OAL_STATUS_SUCCESS) {
2357 BT_ERR("ret: %d", ret);
2358 return BLUETOOTH_ERROR_INTERNAL;
2360 return BLUETOOTH_ERROR_NONE;
2363 int _bt_gatt_server_delete_service(char *sender, int service_handle, int instance_id)
2365 BT_CHECK_PARAMETER(sender, return);
2366 int ret = OAL_STATUS_SUCCESS;
2370 ret = gatts_delete_service(instance_id, service_handle);
2371 if (ret != OAL_STATUS_SUCCESS) {
2372 BT_ERR("ret: %d", ret);
2373 return BLUETOOTH_ERROR_INTERNAL;
2376 /* Remove the Service Handle */
2377 for (l = numapps[instance_id].service_handles; l != NULL;) {
2379 l = g_slist_next(l);
2380 if (handle && *handle == service_handle) {
2381 BT_INFO("Remove Service handle [%d]", *handle);
2382 numapps[instance_id].service_handles = g_slist_remove(numapps[instance_id].service_handles, handle);
2388 return BLUETOOTH_ERROR_NONE;
2391 int _bt_gatt_server_send_response(char *sender, bluetooth_gatt_att_data_t *data,
2392 bluetooth_gatt_server_response_params_t *param)
2394 BT_CHECK_PARAMETER(sender, return);
2395 BT_CHECK_PARAMETER(data, return);
2396 BT_CHECK_PARAMETER(param, return);
2397 struct gatt_server_req_info *req_info = NULL;
2398 int ret = OAL_STATUS_SUCCESS;
2400 oal_gatt_response_t response;
2402 BT_INFO("GATT Server Response: Req Type [%d] req_id [%d] status [%d] auth_req [%d] offset[%d] data len[%d]",
2403 param->req_type, param->request_id,
2404 param->response_status, param->auth_req,
2405 data->offset, data->length);
2407 /* Search for matching Request in List */
2408 req_info = __bt_gatt_server_find_request_info(param->request_id, param->req_type);
2410 BT_ERR("GATT Server Req Info not found for current response..return Error");
2411 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
2414 memset(&response, 0x00, sizeof(oal_gatt_response_t));
2416 response.handle = req_info->attribute_handle;
2417 response.attr_value.auth_req = param->auth_req;
2418 response.attr_value.handle = req_info->attribute_handle;
2419 response.attr_value.offset = data->offset;
2420 response.attr_value.len = data->length;
2421 memcpy(&response.attr_value.value, &data->data, data->length);
2424 ret = gatts_send_response(req_info->connection_id, param->request_id,
2425 param->response_status, &response);
2427 if (ret != OAL_STATUS_SUCCESS) {
2428 BT_ERR("ret: %d", ret);
2429 return BLUETOOTH_ERROR_INTERNAL;
2432 BT_INFO("GATT Server Response successfully sent");
2433 /* Remove GATT server request from list */
2434 gatt_server_requests = g_slist_remove(gatt_server_requests, req_info);
2435 g_free(req_info->addr);
2437 return BLUETOOTH_ERROR_NONE;
2440 int _bt_gatt_server_acquire_send_response(char *sender, bluetooth_gatt_server_acquire_response_params_t *param , void *fd_list)
2442 BT_CHECK_PARAMETER(sender, return);
2443 BT_CHECK_PARAMETER(param, return);
2444 struct gatt_server_req_info *req_info = NULL;
2445 int ret = OAL_STATUS_SUCCESS;
2448 BT_INFO("GATT acquire Server Response: Req Type [%d] req_id [%d] fd [%d] mtu[%d]",
2449 param->req_type, param->request_id,
2453 /* Search for matching Request in List */
2454 req_info = __bt_gatt_server_find_request_info(param->request_id, param->req_type);
2456 BT_ERR("GATT acquire Server Req Info not found for current response..return Error");
2457 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
2460 ret = gatt_send_response_acquire(req_info->connection_id, param->request_id, 0, param->fd, param->mtu, fd_list);
2462 if (ret != OAL_STATUS_SUCCESS) {
2463 BT_ERR("ret: %d", ret);
2464 return BLUETOOTH_ERROR_INTERNAL;
2467 BT_INFO("GATT acquire Server Response successfully sent");
2468 /* Remove GATT server request from list */
2469 gatt_server_requests = g_slist_remove(gatt_server_requests, req_info);
2470 g_free(req_info->addr);
2472 return BLUETOOTH_ERROR_NONE;
2477 int _bt_gatt_server_send_indication(char *sender, bluetooth_device_address_t *dev_addr,
2478 bluetooth_gatt_att_data_t *data,
2479 bluetooth_gatt_server_indication_params_t *param)
2481 BT_CHECK_PARAMETER(sender, return);
2482 BT_CHECK_PARAMETER(data, return);
2483 BT_CHECK_PARAMETER(param, return);
2485 gboolean all_send = FALSE;
2486 int ret = OAL_STATUS_SUCCESS;
2487 struct gatt_client_info_t *conn;
2489 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2490 _bt_convert_addr_type_to_string(address, dev_addr->addr);
2492 if (memcmp(dev_addr->addr, BDADDR_ANY, 6) == 0) {
2493 BT_INFO("GATT Server: Send Indication to all connected GATT clients..");
2496 BT_INFO("GATT Server: Send Indication to connected GATT client addr [%s]", address);
2499 /* Attempt to send Notification/Indication to all Connected GATT clients */
2501 ret = __bt_gatt_send_indication_to_all_connected_clients(data, param);
2502 if (ret != OAL_STATUS_SUCCESS) {
2503 BT_ERR("ret: %d", ret);
2505 return BLUETOOTH_ERROR_INTERNAL;
2509 conn = _bt_find_remote_gatt_client_info(address);
2511 ret = gatts_send_indication(param->instance_id, param->atrribute_handle,
2512 conn->connection_id, data->length,
2513 param->need_confirmation, (char *)(&data->data[0]));
2515 if (ret != OAL_STATUS_SUCCESS) {
2516 BT_ERR("ret: %d", ret);
2517 BT_INFO("Indication failed to send to Remote GATT Client [%s]", address);
2519 return BLUETOOTH_ERROR_INTERNAL;
2521 BT_INFO("Indication sent to Remote GATT Client [%s] wait for Notification completed event from OAL", address);
2523 num_indicate_clients = 1;
2524 return BLUETOOTH_ERROR_NONE;
2526 BT_ERR("Remote GATT client [%s] is not connected..Cant send Indication!!", address);
2528 return BLUETOOTH_ERROR_NOT_CONNECTED;
2531 return BLUETOOTH_ERROR_NONE;
2534 int _bt_gatt_server_update_attribute_value(char *sender, int instance_id,
2535 bluetooth_gatt_server_update_value_t *param)
2537 BT_CHECK_PARAMETER(sender, return);
2538 BT_CHECK_PARAMETER(param, return);
2539 int ret = OAL_STATUS_SUCCESS;
2541 oal_gatt_value_t value;
2542 BT_INFO("GATT Server Update value: Instance ID [%d] attr handle [%d] Value len [%d]",
2543 instance_id, param->attribute_handle, param->length);
2546 memset(&value, 0x00, sizeof(oal_gatt_value_t));
2548 value.handle = param->attribute_handle;
2549 value.len = param->length;
2550 memcpy(&value.value, ¶m->data.data, param->length);
2552 ret = gatts_update_att_value(instance_id, &value);
2554 if (ret != OAL_STATUS_SUCCESS) {
2555 BT_ERR("ret: %d", ret);
2556 return BLUETOOTH_ERROR_INTERNAL;
2559 BT_INFO("GATT Server Update Attribute Value successfully");
2560 return BLUETOOTH_ERROR_NONE;
2563 int _bt_request_att_mtu(bluetooth_device_address_t *device_address,
2566 struct gatt_server_info_t *conn_info = NULL;
2568 int ret = OAL_STATUS_SUCCESS;
2570 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
2571 _bt_convert_addr_type_to_string(addr, device_address->addr);
2573 /* Check if remote GATT Server is connected or not */
2574 conn_info = _bt_find_remote_gatt_server_info(addr);
2576 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
2577 conn_info->addr, conn_info->connection_id);
2579 BT_ERR("GATT Server is not yet connected..");
2581 return BLUETOOTH_ERROR_NOT_CONNECTED;
2584 ret = gattc_configure_mtu(conn_info->connection_id, mtu);
2585 if (ret != OAL_STATUS_SUCCESS) {
2586 BT_ERR("ret: %d", ret);
2588 return BLUETOOTH_ERROR_INTERNAL;
2592 return BLUETOOTH_ERROR_NONE;
2595 int _bt_get_att_mtu(bluetooth_device_address_t *address,
2598 BT_CHECK_PARAMETER(address, return);
2599 BT_CHECK_PARAMETER(mtu, return);
2600 struct gatt_client_info_t *conn_info = NULL;
2601 char addr[BT_ADDRESS_STRING_SIZE] = { 0 };
2602 int ret = OAL_STATUS_SUCCESS;
2605 _bt_convert_addr_type_to_string(addr, address->addr);
2607 BT_INFO("Get current MTU size for the remote client:DevAddress:[%s]", addr);
2609 conn_info = _bt_find_remote_gatt_client_info(addr);
2611 BT_INFO("GATT Client [%s] is connected, conn Id [%d] Instance ID [%d]",
2612 conn_info->addr, conn_info->connection_id, conn_info->instance_id);
2614 BT_ERR("GATT Client [%s] is not yet connected..", addr);
2615 return BLUETOOTH_ERROR_NOT_CONNECTED;
2618 ret = gatts_get_att_mtu(conn_info->connection_id, &stack_mtu);
2619 if (ret != OAL_STATUS_SUCCESS) {
2620 BT_ERR("ret: %d", ret);
2621 return BLUETOOTH_ERROR_INTERNAL;
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) {
2726 BT_INFO("Remote GATT server found addr[%s]", info->addr);
2733 static bt_gatt_service_info_t* __bt_find_removed_service(bt_gatt_service_info_list_t *svc_list)
2736 bt_gatt_service_info_t *info = NULL;
2738 for (l = svc_list->services; l != NULL; l = g_slist_next(l)) {
2739 info = (bt_gatt_service_info_t*)l->data;
2743 /* Service is marked a removed */
2744 if (info->is_removed == 1)
2750 static void __bt_remove_service_info_from_list(bt_gatt_service_info_t *svc_info)
2756 bt_gatt_char_info_t *charc = NULL;
2757 bt_gatt_included_service_info_t *incl = NULL;
2758 bt_gatt_descriptor_info_t *desc = NULL;
2760 /* Remove all Characteristic and Descriptors within characteristic */
2761 for (l = svc_info->chars; l != NULL;) {
2762 charc = (bt_gatt_char_info_t*)l->data;
2763 l = g_slist_next(l); /* Incase if l is removed, saving next to l */
2768 /* Inside Characteristic */
2769 for (l1 = charc->descs; l1 != NULL;) {
2771 desc = (bt_gatt_descriptor_info_t*)l1->data;
2772 l1 = g_slist_next(l1);
2777 /* Remove Descriptor */
2778 charc->descs = g_slist_remove(charc->descs, desc);
2781 /* Remove Characteristic */
2782 svc_info->chars = g_slist_remove(svc_info->chars, charc);
2786 /* Remove all Included Services */
2787 for (l2 = svc_info->included_svcs; l2 != NULL;) {
2788 incl = (bt_gatt_included_service_info_t*)l2->data;
2789 l2 = g_slist_next(l2); /* Incase if l is removed, saving next to l */
2794 /* Remove included service */
2795 svc_info->included_svcs = g_slist_remove(svc_info->included_svcs, incl);
2802 static void __bt_build_service_browse_info(int conn_id,
2803 bt_services_browse_info_t* info)
2806 bt_gatt_service_info_list_t *svc_info_list;
2807 bt_gatt_service_info_t *svc_info;
2809 service_uuid_t uuid;
2810 struct gatt_server_info_t *conn_info = NULL;
2812 char uuid_string[BLUETOOTH_UUID_STRING_MAX];
2814 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
2819 _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
2821 svc_info_list = __bt_get_service_info_list(conn_id);
2825 info->count = g_slist_length(svc_info_list->services);
2826 BT_DBG("Total services present in the svc info list for this conn id [%d] is [%d]",
2827 conn_id, info->count);
2829 for (l = svc_info_list->services; l != NULL; l = g_slist_next(l)) {
2830 svc_info = (bt_gatt_service_info_t*)l->data;
2831 if (svc_info == NULL)
2834 memcpy(&uuid.uuid, &svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2835 _bt_uuid_to_string(&uuid, uuid_string);
2837 BT_INFO("[%d] %s [%s]", count, uuid_string, _bt_convert_uuid_to_string(uuid_string));
2839 /* Fill UUID of service */
2840 g_strlcpy(info->uuids[count], uuid_string,
2841 BLUETOOTH_UUID_STRING_MAX);
2843 /* Fill instance ID of service */
2844 info->inst_id[count] = svc_info->inst_id;
2846 /* Fill primary service or not info */
2847 info->primary[count] = svc_info->is_primary;
2849 /* Increment count of services browsed */
2854 static void __bt_build_char_browse_info(int conn_id,
2855 bt_gatt_service_info_t *svc_info,
2856 bt_char_browse_info_t* info)
2859 bt_gatt_char_info_t *char_info;
2860 service_uuid_t uuid;
2862 struct gatt_server_info_t *conn_info = NULL;
2864 char uuid_string[BLUETOOTH_UUID_STRING_MAX];
2866 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
2870 /* Fill default data, this will be required even in case of failure */
2871 _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
2872 memcpy(&info->svc_uuid, svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2873 info->svc_inst_id = svc_info->inst_id;
2875 if (!svc_info->chars) {
2876 BT_ERR("No Chars browsed for address [%s]", conn_info->addr);
2880 info->count = g_slist_length(svc_info->chars);
2882 for (l = svc_info->chars; l != NULL; l = g_slist_next(l)) {
2883 char_info = (bt_gatt_char_info_t*)l->data;
2884 if (char_info == NULL)
2887 memcpy(&uuid.uuid, &char_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2888 _bt_uuid_to_string(&uuid, uuid_string);
2890 /* Fill UUID of characteristic */
2891 g_strlcpy(info->uuids[count], uuid_string,
2892 BLUETOOTH_UUID_STRING_MAX);
2894 /* Fill instance ID of characteristic */
2895 info->inst_id[count] = char_info->inst_id;
2897 /* Fill property of characteristic */
2898 info->props[count] = char_info->props;
2900 /* Increment count of services browsed */
2903 BT_DBG("Total characteristics browsed [%d]", count);
2906 static void __bt_build_descriptor_browse_info(int conn_id,
2907 bt_gatt_service_info_t *svc_info,
2908 bt_gatt_char_info_t *char_info,
2909 bt_descriptor_browse_info_t* info)
2912 bt_gatt_descriptor_info_t *desc_info;
2914 service_uuid_t uuid;
2915 struct gatt_server_info_t *conn_info = NULL;
2917 char uuid_string[BLUETOOTH_UUID_STRING_MAX];
2919 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
2921 /* Fill default data, this will be required even in case of failure */
2922 _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
2923 memcpy(&info->svc_uuid, svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2924 info->svc_inst_id = svc_info->inst_id;
2925 memcpy(&info->char_uuid, char_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2926 info->char_inst_id = char_info->inst_id;
2928 /* Fill property of the parent characteristic of this descriptor */
2929 info->char_props_map = char_info->props;
2931 info->count = g_slist_length(char_info->descs);
2933 if (!char_info->descs) {
2934 BT_ERR("No Descriptors browsed for address [%s]", conn_info->addr);
2938 for (l = char_info->descs; l != NULL; l = g_slist_next(l)) {
2939 desc_info = (bt_gatt_descriptor_info_t*)l->data;
2940 if (desc_info == NULL)
2943 memcpy(&uuid.uuid, &desc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2944 _bt_uuid_to_string(&uuid, uuid_string);
2946 /* Fill UUID of Descriptor */
2947 g_strlcpy(info->uuids[count], uuid_string,
2948 BLUETOOTH_UUID_STRING_MAX);
2950 /* Fill instance ID of Descriptor */
2951 info->inst_id[count] = desc_info->inst_id;
2954 /* Increment count of Descriptor browsed */
2958 BT_INFO("Total descriptors browsed [%d]", count);
2961 static void __bt_free_service_info(bt_gatt_service_info_t *svc)
2963 GSList *ll, *lll, *llll;
2964 bt_gatt_char_info_t *chr = NULL;
2965 bt_gatt_descriptor_info_t *desc = NULL;
2966 bt_gatt_included_service_info_t *incl_svc = NULL;
2968 BT_DBG("Service info Is Prim[%d] Inst ID [%d]", svc->is_primary, svc->inst_id);
2969 /* Delete all chars and its descriptors */
2970 for (ll = svc->chars; ll != NULL; ) {
2971 chr = (bt_gatt_char_info_t *)ll->data;
2972 ll = g_slist_next(ll);
2976 for (lll = chr->descs; lll != NULL; ) {
2977 desc = (bt_gatt_descriptor_info_t *)lll->data;
2978 lll = g_slist_next(lll);
2981 chr->descs = g_slist_remove(chr->descs, desc);
2984 svc->chars = g_slist_remove(svc->chars, chr);
2988 /* Delete all included services */
2989 for (llll = svc->included_svcs; llll != NULL; ) {
2990 incl_svc = (bt_gatt_included_service_info_t *)llll->data;
2991 llll = g_slist_next(llll);
2992 if (incl_svc == NULL)
2994 svc->included_svcs = g_slist_remove(svc->included_svcs, incl_svc);
2999 static void __bt_cleanup_remote_services(struct gatt_server_info_t *conn_info)
3001 bt_gatt_service_info_list_t *svc_info_list = NULL;
3002 bt_gatt_service_info_t *svc = NULL;
3005 svc_info_list = __bt_get_service_info_list(conn_info->connection_id);
3006 if (!svc_info_list) {
3007 BT_INFO("Could not find Svc Info list for the connection ID [%d]",
3008 conn_info->connection_id);
3012 BT_INFO("Start Cleanup of all services. Num Services [%d]", g_slist_length(svc_info_list->services));
3013 for (l = svc_info_list->services; l != NULL; ) {
3014 svc = (bt_gatt_service_info_t *)l->data;
3015 l = g_slist_next(l);
3019 __bt_free_service_info(svc);
3020 svc_info_list->services = g_slist_remove(svc_info_list->services, svc);
3024 list_gatt_info = g_slist_remove(list_gatt_info, svc_info_list);
3025 g_free(svc_info_list);
3028 int _bt_register_gatt_client_instance(const char *sender,
3029 bluetooth_device_address_t *address)
3031 int ret = OAL_STATUS_SUCCESS;
3032 char *uuid_string = NULL;
3037 /* App should ensure that it should not send */
3038 BT_INFO("### Check on which instance GATT Client instance can be initialized....");
3039 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
3040 if (numapps[k].is_initialized == 1) {
3041 BT_INFO("Instance ID [%d] is already in use..Check next slot",
3042 numapps[k].instance_id);
3045 BT_INFO("Time to register GATT client instancer..UUID to be used is [%s] slot [%d]",
3046 uuid_list[slot-1], slot);
3052 BT_ERR("No Slot if free for GATT Client registration..");
3053 return BLUETOOTH_ERROR_REGISTRATION_FAILED;
3056 uuid_string = g_malloc0(BT_UUID_STRING_MAX);
3057 _bt_string_to_uuid(uuid_list[slot-1], (service_uuid_t*)&uuid);
3058 g_strlcpy(uuid_string, uuid_list[slot-1], BT_UUID_STRING_MAX);
3059 BT_INFO("Copied UUID string [%s]", uuid_string);
3061 /* Register GATT Client */
3062 ret = gattc_register(&uuid);
3063 if (ret != OAL_STATUS_SUCCESS) {
3064 BT_ERR("ret: %d", ret);
3065 g_free(uuid_string);
3066 return BLUETOOTH_ERROR_INTERNAL;
3069 BT_INFO("GATT Client registration call successfully accepted by OAL..wait for Instance Initialized event from OAL..");
3071 /* Return & wait for GATT Client Instance Initialization event */
3072 memset(numapps[slot].sender, 0x00, sizeof(numapps[slot].sender));
3073 memset(numapps[slot].uuid, 0x00, sizeof(numapps[slot].uuid));
3075 g_strlcpy(numapps[slot].sender, sender, sizeof(numapps[slot].sender));
3076 g_strlcpy(numapps[slot].uuid, uuid_string, sizeof(numapps[slot].uuid));
3078 /* Address is saved here. When event comes, sender + address are matched for replying pending
3079 request. It is impossible for same sender to have requests with two same addresses */
3080 memcpy(&numapps[slot].address.addr, address->addr, sizeof(bluetooth_device_address_t));
3082 BT_INFO("Slot [%d] occupied", slot);
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 BT_INFO("Added GATT server addr[%s]", conn_info->addr);
3164 conn_info->connection_id = event_data->conn_id;
3165 gatt_server_info_list = g_slist_append(gatt_server_info_list, conn_info);
3166 BT_INFO("Total num of connected Remote GATT server devices [%d]",
3167 g_slist_length(gatt_server_info_list));
3170 BT_INFO("Do a Internal refresh");
3171 if (OAL_STATUS_SUCCESS != gattc_refresh(conn_info->client_id, &event_data->address))
3172 BT_ERR("GATT database refresh failed!!");
3174 BT_INFO("GATT database refresh Success!!");
3177 BT_ERR("Local GATT Client connected event for addr[%s], but device is in connected list already", address);
3179 __bt_add_mtu_gatt_device(address);
3181 BT_ERR("GATT Client Connection failed!!");
3183 /* If outgoing connection Info is present, then remove it */
3184 out_conn_info = __bt_find_gatt_outgoing_conn_info(address);
3185 if (out_conn_info) {
3186 BT_ERR("Outgoing Client connect request was sent");
3187 outgoing_gatt_conn_list = g_slist_remove(outgoing_gatt_conn_list, out_conn_info);
3188 g_free(out_conn_info->addr);
3189 g_free(out_conn_info);
3194 static void __bt_handle_client_disconnected(event_gattc_conn_t *event_data)
3196 int result = BLUETOOTH_ERROR_NONE;
3198 struct gatt_server_info_t *conn_info = NULL;
3199 struct gatt_out_conn_info_t *out_conn_info = NULL;
3201 GVariant *param = NULL;
3203 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
3204 _bt_convert_addr_type_to_string(address,
3205 (unsigned char *)event_data->address.addr);
3207 if (event_data->status != OAL_STATUS_SUCCESS)
3208 result = BLUETOOTH_ERROR_INTERNAL;
3210 if (NULL == _bt_get_request_info_data(BT_DISCONNECT_LE, address)) {
3211 if (NULL != _bt_get_request_info_data(BT_CONNECT_LE, address)) {
3212 result = BLUETOOTH_ERROR_INTERNAL;
3213 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
3214 address, BT_ADDRESS_STRING_SIZE);
3215 BT_ERR("Failed to connect Local GATT Remote addr[%s]", address);
3220 /* DBUS Return for BT_DISCONNECT_LE for all the apps */
3221 __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE, address,
3222 BT_ADDRESS_STRING_SIZE);
3224 BT_INFO("Local GATT Client DisConnected: Remote addr[%s] Client Interface [%d] Connection ID [%d] status [%d]",
3225 address, event_data->client_if, event_data->conn_id, event_data->status);
3227 /* Remove Connection info */
3228 conn_info = _bt_find_remote_gatt_server_info(address);
3231 param = g_variant_new("(is)", result, address);
3232 /* Send event to application */
3233 _bt_send_event(BT_DEVICE_EVENT,
3234 BLUETOOTH_EVENT_GATT_CLIENT_DISCONNECTED,
3237 BT_INFO("Remove GATT server info from List..");
3238 /* Remove all services from info list_gatt_info */
3239 __bt_cleanup_remote_services(conn_info);
3241 /* Remove info from List */
3242 gatt_server_info_list = g_slist_remove(gatt_server_info_list, conn_info);
3243 BT_INFO("Total num of connected GATT servers [%d]", g_slist_length(gatt_server_info_list));
3244 g_free(conn_info->addr);
3247 BT_INFO("Can not find conn info, already removed!");
3249 __bt_remove_mtu_gatt_device(address);
3251 /* If outgoing connection Info is present, then remove it */
3252 out_conn_info = __bt_find_gatt_outgoing_conn_info(address);
3253 if (out_conn_info) {
3254 BT_ERR("Client Disconnected event, but outgoing connect request was sent");
3255 outgoing_gatt_conn_list = g_slist_remove(outgoing_gatt_conn_list, out_conn_info);
3256 g_free(out_conn_info->addr);
3257 g_free(out_conn_info);
3263 static void __bt_handle_client_service_search_result(
3264 event_gattc_service_result_t *event_data)
3266 /* Pre: status is never fail from OAL */
3268 /* Find service list from address */
3269 bt_gatt_service_info_list_t *svc_info_list;
3270 bt_gatt_service_info_t *svc_info;
3272 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3273 if (!svc_info_list) {
3274 BT_DBG("Service info list not present for connection ID %d, means first time browse", event_data->conn_status.conn_id);
3275 /* Means for this conn_id, no services are ever browsed, first time,
3276 create service info list for this conn_id */
3277 svc_info_list = g_malloc0(sizeof(bt_gatt_service_info_list_t));
3278 svc_info_list->conn_id = event_data->conn_status.conn_id;
3279 list_gatt_info = g_slist_append(list_gatt_info, svc_info_list);
3282 /* send list and current service's uuid and instance id to find it */
3283 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");
3405 /* Find matching service info from svc info list */
3406 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3407 if (svc_info == NULL) {
3408 BT_ERR("svc_info is NULL");
3412 /* Find Matching char from service info in event */
3413 char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
3415 /* If not found, then add new characteristic and return */
3417 BT_DBG("Add new characteristic");
3418 char_info = g_malloc0(sizeof(bt_gatt_char_info_t));
3419 memcpy(char_info->uuid, event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3421 char_info->inst_id = event_data->char_id.inst_id;
3422 char_info->props = event_data->char_prop;
3423 svc_info->chars = g_slist_append(svc_info->chars, char_info);
3425 /* If found, then return */
3426 BT_DBG("update char property as Characteristic browsed is already present");
3427 char_info->props |= event_data->char_prop;
3430 /* If Not success: Means Charc browse is completed */
3431 /* Find char list from service in event */
3432 /* Find service list from address */
3433 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3434 if (svc_info_list == NULL) {
3435 BT_ERR("svc_info_list is NULL");
3439 /* Find service info from service in event */
3440 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3441 if (svc_info == NULL) {
3442 BT_ERR("svc_info is NULL");
3446 /* Build char list from service in event */
3447 __bt_build_char_browse_info(event_data->conn_status.conn_id,
3448 svc_info, &browse_info);
3450 /* Create response and return by sending event*/
3451 /* Build Reply and send to service browse All Included services request of pending apps */
3452 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE,
3453 BT_GATT_GET_SERVICE_PROPERTIES,
3455 sizeof(bt_char_browse_info_t));
3459 static void __bt_handle_client_descriptor_search_result(
3460 event_gattc_descriptor_result_t *event_data)
3462 bt_gatt_service_info_list_t *svc_info_list;
3463 bt_gatt_service_info_t *svc_info;
3464 bt_gatt_char_info_t *char_info;
3465 bt_gatt_descriptor_info_t *desc_info;
3466 bt_descriptor_browse_info_t browse_info;
3468 BT_INFO("descriptor search result status [%d]", event_data->conn_status.status);
3470 memset(&browse_info, 0x00, sizeof(bt_descriptor_browse_info_t));
3473 if (event_data->conn_status.status == OAL_STATUS_SUCCESS) {
3474 /* Find service list from address */
3475 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3476 if (svc_info_list == NULL) {
3477 BT_ERR("svc_info_list is NULL");
3481 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3482 if (svc_info == NULL) {
3483 BT_ERR("svc_info is NULL");
3487 char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
3488 if (char_info == NULL) {
3489 BT_ERR("char_info is NULL");
3493 desc_info = __bt_find_matching_desc(char_info, &event_data->descr_id);
3494 /* If not found, add new descriptor and return */
3496 desc_info = g_malloc0(sizeof(bt_gatt_descriptor_info_t));
3497 memcpy(desc_info->uuid, event_data->descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3499 desc_info->inst_id = event_data->descr_id.inst_id;
3500 char_info->descs = g_slist_append(char_info->descs, desc_info);
3503 /* If found, then return */
3504 BT_INFO("Characteristic browsed is already presesnt");
3507 /* If Not success */
3508 /* Find service list from address */
3509 /* Find included service list from service in event */
3510 /* Create response and return by sending event*/
3511 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3512 if (svc_info_list == NULL) {
3513 BT_ERR("svc_info_list is NULL");
3517 /* Find service info from service in event */
3518 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3519 if (svc_info == NULL) {
3520 BT_ERR("svc_info is NULL");
3524 /* Find char info from char in event */
3525 char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
3526 if (char_info == NULL) {
3527 BT_ERR("char_info is NULL");
3531 /* Build descriptor list from char in event */
3532 __bt_build_descriptor_browse_info(event_data->conn_status.conn_id,
3533 svc_info, char_info, &browse_info);
3536 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE,
3537 BT_GATT_GET_CHARACTERISTIC_PROPERTIES,
3539 sizeof(bt_descriptor_browse_info_t));
3543 static void __bt_handle_client_characteristic_read_data(
3544 event_gattc_read_data *event_data)
3546 int result = BLUETOOTH_ERROR_NONE;
3547 struct gatt_server_info_t *conn_info = NULL;
3548 bluetooth_gatt_client_char_prop_info_t read_info;
3550 /* Read Information data structures */
3551 GVariant *param = NULL;
3552 GVariant *data = NULL;
3553 GVariant *data_svc_uuid = NULL;
3554 GVariant *data_char_uuid = NULL;
3555 char *read_val = NULL;
3556 char *svc_uuid = NULL;
3557 char *char_uuid = NULL;
3560 //memset(&read_info, 0x00, sizeof(bt_gatt_handle_property_t));
3561 memset(&read_info, 0x00, sizeof(bluetooth_gatt_client_char_prop_info_t));
3563 /* Extract Address from conn_id of event data */
3564 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3565 event_data->uuid_status.conn_status.conn_id);
3567 BT_INFO("Characteristic Read result from addr [%s] status [%d]",
3568 conn_info->addr, event_data->uuid_status.conn_status.status);
3570 /* Fill char in buffer */
3571 memcpy(&read_info.characteristic.uuid,
3572 event_data->uuid_status.char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3573 read_info.characteristic.instance_id = event_data->uuid_status.char_id.inst_id;
3575 /* Fill Service in buffer */
3576 memcpy(&read_info.svc.uuid,
3577 event_data->uuid_status.srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3578 read_info.svc.instance_id = event_data->uuid_status.srvc_id.id.inst_id;
3580 /* Fill remote device address */
3581 _bt_convert_addr_string_to_type(read_info.device_address.addr, conn_info->addr);
3583 /* Fill data and reply to all apps waiting for Read result on the same characteristic
3584 Note: Even in case of failure, address, handles and result code should be returned */
3585 if (event_data->uuid_status.conn_status.status != OAL_STATUS_SUCCESS)
3586 result = BLUETOOTH_ERROR_INTERNAL;
3588 if (event_data->data_len > 0) {
3590 // for (i = 0; i < event_data->data_len; i++)
3591 // BT_INFO("Data[%d] = [0x%x]", i, event_data->data[i]);
3594 read_val = g_memdup(&event_data->data[0], event_data->data_len);
3596 data = g_variant_new_from_data(
3597 G_VARIANT_TYPE_BYTESTRING,
3599 event_data->data_len,
3602 BT_ERR("Characteristic Read success, but no data!!!");
3604 data = g_variant_new_from_data(
3605 G_VARIANT_TYPE_BYTESTRING,
3612 svc_uuid = g_memdup(&event_data->uuid_status.srvc_id.id.uuid.uuid[0], uuid_len);
3614 data_svc_uuid = g_variant_new_from_data(
3615 G_VARIANT_TYPE_BYTESTRING,
3621 char_uuid = g_memdup(&event_data->uuid_status.char_id.uuid.uuid[0], uuid_len);
3623 data_char_uuid = g_variant_new_from_data(
3624 G_VARIANT_TYPE_BYTESTRING,
3629 param = g_variant_new("(isn@ayin@ayin@ay)", result,
3633 event_data->uuid_status.srvc_id.id.inst_id,
3636 event_data->uuid_status.char_id.inst_id,
3637 event_data->data_len,
3641 _bt_send_event(BT_GATT_CLIENT_EVENT,
3642 BLUETOOTH_EVENT_GATT_READ_CHAR,
3645 /* Send DBUS return */
3646 __bt_gatt_handle_pending_request_info(result,
3647 BT_GATT_READ_CHARACTERISTIC,
3649 sizeof(bluetooth_gatt_client_char_prop_info_t));
3660 static void __bt_handle_client_descriptor_read_data(
3661 event_gattc_read_data *event_data)
3663 int result = BLUETOOTH_ERROR_NONE;
3664 struct gatt_server_info_t *conn_info = NULL;
3665 bluetooth_gatt_client_desc_prop_info_t read_info;
3667 /* Read Information data structures */
3668 GVariant *param = NULL;
3669 GVariant *data = NULL;
3670 GVariant *data_svc_uuid = NULL;
3671 GVariant *data_char_uuid = NULL;
3672 GVariant *data_desc_uuid = NULL;
3673 char *read_val = NULL;
3674 char *svc_uuid = NULL;
3675 char *char_uuid = NULL;
3676 char *desc_uuid = NULL;
3681 memset(&read_info, 0x00, sizeof(bluetooth_gatt_client_desc_prop_info_t));
3683 /* Extract Address from conn_id of event data */
3684 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3685 event_data->uuid_status.conn_status.conn_id);
3687 BT_INFO("Descriptor Read result from addr [%s] status [%d]",
3688 conn_info->addr, event_data->uuid_status.conn_status.status);
3690 /* Fill descriptor informations in buffer */
3691 memcpy(&read_info.descriptor.uuid,
3692 event_data->uuid_status.descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3693 read_info.descriptor.instance_id = event_data->uuid_status.descr_id.inst_id;
3695 /* Fill Characteristic informations in buffer */
3696 memcpy(&read_info.characteristic.uuid,
3697 event_data->uuid_status.char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3698 read_info.characteristic.instance_id = event_data->uuid_status.char_id.inst_id;
3700 /* Fill Service informations in buffer */
3701 memcpy(&read_info.svc.uuid,
3702 event_data->uuid_status.srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3703 read_info.svc.instance_id = event_data->uuid_status.srvc_id.id.inst_id;
3705 /* Fill remote device address */
3706 _bt_convert_addr_string_to_type(read_info.device_address.addr, conn_info->addr);
3708 /* Fill data and reply to all apps waiting for Read result on the same characteristic */
3709 if (event_data->uuid_status.conn_status.status != OAL_STATUS_SUCCESS)
3710 result = BLUETOOTH_ERROR_INTERNAL;
3712 BT_INFO("desc data len:%d", event_data->data_len);
3713 if (event_data->data_len > 0) {
3715 for (i = 0; i < event_data->data_len; i++)
3716 BT_DBG("Data[%d] = [0x%x]", i, event_data->data[i]);
3719 read_val = g_memdup(&event_data->data[0], event_data->data_len);
3721 data = g_variant_new_from_data(
3722 G_VARIANT_TYPE_BYTESTRING,
3724 event_data->data_len,
3727 BT_INFO("Descriptor Read success, but no data!!!");
3729 data = g_variant_new_from_data(
3730 G_VARIANT_TYPE_BYTESTRING,
3736 svc_uuid = g_memdup(&event_data->uuid_status.srvc_id.id.uuid.uuid[0], uuid_len);
3738 data_svc_uuid = g_variant_new_from_data(
3739 G_VARIANT_TYPE_BYTESTRING,
3745 char_uuid = g_memdup(&event_data->uuid_status.char_id.uuid.uuid[0], uuid_len);
3747 data_char_uuid = g_variant_new_from_data(
3748 G_VARIANT_TYPE_BYTESTRING,
3754 desc_uuid = g_memdup(&event_data->uuid_status.descr_id.uuid.uuid[0], uuid_len);
3756 data_desc_uuid = g_variant_new_from_data(
3757 G_VARIANT_TYPE_BYTESTRING,
3762 param = g_variant_new("(isn@ayin@ayin@ayin@ay)", result,
3766 event_data->uuid_status.srvc_id.id.inst_id,
3769 event_data->uuid_status.char_id.inst_id,
3772 event_data->uuid_status.descr_id.inst_id,
3773 event_data->data_len,
3777 _bt_send_event(BT_GATT_CLIENT_EVENT,
3778 BLUETOOTH_EVENT_GATT_READ_DESC,
3781 BT_INFO("Send DBUS rpely for GATT Read Descriptor");
3782 /* Send DBUS return */
3783 __bt_gatt_handle_pending_request_info(result,
3784 BT_GATT_READ_DESCRIPTOR_VALUE,
3786 sizeof(bluetooth_gatt_client_desc_prop_info_t));
3797 static void __bt_handle_client_characteristic_write_data(
3798 event_gattc_write_data *event_data)
3800 int result = BLUETOOTH_ERROR_NONE;
3801 struct gatt_server_info_t *conn_info = NULL;
3802 bluetooth_gatt_client_char_prop_info_t write_info;
3804 /* Read Information data structures */
3805 GVariant *param = NULL;
3806 GVariant *data_svc_uuid = NULL;
3807 GVariant *data_char_uuid = NULL;
3808 char *svc_uuid = NULL;
3809 char *char_uuid = NULL;
3813 memset(&write_info, 0x00, sizeof(bluetooth_gatt_client_char_prop_info_t));
3815 /* Extract Address from conn_id of event data */
3816 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3817 event_data->conn_status.conn_id);
3819 BT_INFO("Characteristic Write callback from addr [%s] status [%d]",
3820 conn_info->addr, event_data->conn_status.status);
3822 /* Fill char in buffer */
3823 memcpy(&write_info.characteristic.uuid,
3824 event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3825 write_info.characteristic.instance_id = event_data->char_id.inst_id;
3827 /* Fill Service in buffer */
3828 memcpy(&write_info.svc.uuid,
3829 event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3830 write_info.svc.instance_id = event_data->srvc_id.id.inst_id;
3832 /* Fill remote device address */
3833 _bt_convert_addr_string_to_type(write_info.device_address.addr, conn_info->addr);
3835 if (event_data->conn_status.status != OAL_STATUS_SUCCESS) {
3836 result = BLUETOOTH_ERROR_INTERNAL;
3842 svc_uuid = g_memdup(&event_data->srvc_id.id.uuid.uuid[0], uuid_len);
3844 data_svc_uuid = g_variant_new_from_data(
3845 G_VARIANT_TYPE_BYTESTRING,
3851 char_uuid = g_memdup(&event_data->char_id.uuid.uuid[0], uuid_len);
3853 data_char_uuid = g_variant_new_from_data(
3854 G_VARIANT_TYPE_BYTESTRING,
3859 param = g_variant_new("(isn@ayin@ayi)", result,
3863 event_data->srvc_id.id.inst_id,
3866 event_data->char_id.inst_id);
3869 _bt_send_event(BT_GATT_CLIENT_EVENT,
3870 BLUETOOTH_EVENT_GATT_WRITE_CHAR,
3879 /* Send DBUS return */
3880 __bt_gatt_handle_pending_request_info(result,
3881 BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE,
3883 sizeof(bluetooth_gatt_client_char_prop_info_t));
3887 static void __bt_handle_client_descriptor_write_data(
3888 event_gattc_write_data *event_data)
3890 int result = BLUETOOTH_ERROR_NONE;
3891 struct gatt_server_info_t *conn_info = NULL;
3892 bluetooth_gatt_client_desc_prop_info_t write_info;
3894 /* Write Information data structures */
3895 GVariant *param = NULL;
3896 GVariant *data_svc_uuid = NULL;
3897 GVariant *data_char_uuid = NULL;
3898 GVariant *data_desc_uuid = NULL;
3899 char *svc_uuid = NULL;
3900 char *char_uuid = NULL;
3901 char *desc_uuid = NULL;
3905 memset(&write_info, 0x00, sizeof(bluetooth_gatt_client_desc_prop_info_t));
3907 /* Extract Address from conn_id of event data */
3908 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3909 event_data->conn_status.conn_id);
3911 if (NULL == conn_info) {
3913 BT_INFO("Failed to get the conn info for conn_id [%d]", event_data->conn_status.conn_id);
3917 BT_INFO("Descriptor Write callback from addr [%s] status [%d]",
3918 conn_info->addr, event_data->conn_status.status);
3920 /* Fill descriptor informations in buffer */
3921 memcpy(&write_info.descriptor.uuid,
3922 event_data->descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3923 write_info.descriptor.instance_id = event_data->descr_id.inst_id;
3925 /* Fill Characteristic informations in buffer */
3926 memcpy(&write_info.characteristic.uuid,
3927 event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3928 write_info.characteristic.instance_id = event_data->char_id.inst_id;
3930 /* Fill Service informations in buffer */
3931 memcpy(&write_info.svc.uuid,
3932 event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3933 write_info.svc.instance_id = event_data->srvc_id.id.inst_id;
3935 /* Fill remote device address */
3936 _bt_convert_addr_string_to_type(write_info.device_address.addr, conn_info->addr);
3938 if (event_data->conn_status.status != OAL_STATUS_SUCCESS) {
3939 result = BLUETOOTH_ERROR_INTERNAL;
3945 svc_uuid = g_memdup(&event_data->srvc_id.id.uuid.uuid[0], uuid_len);
3947 data_svc_uuid = g_variant_new_from_data(
3948 G_VARIANT_TYPE_BYTESTRING,
3954 char_uuid = g_memdup(&event_data->char_id.uuid.uuid[0], uuid_len);
3956 data_char_uuid = g_variant_new_from_data(
3957 G_VARIANT_TYPE_BYTESTRING,
3963 desc_uuid = g_memdup(&event_data->descr_id.uuid.uuid[0], uuid_len);
3965 data_desc_uuid = g_variant_new_from_data(
3966 G_VARIANT_TYPE_BYTESTRING,
3971 param = g_variant_new("(isn@ayin@ayin@ayi)", result,
3975 event_data->srvc_id.id.inst_id,
3978 event_data->char_id.inst_id,
3981 event_data->descr_id.inst_id);
3984 _bt_send_event(BT_GATT_CLIENT_EVENT,
3985 BLUETOOTH_EVENT_GATT_WRITE_DESC,
3996 /* Send DBUS return */
3997 __bt_gatt_handle_pending_request_info(result,
3998 BT_GATT_WRITE_DESCRIPTOR_VALUE,
4000 sizeof(bluetooth_gatt_client_desc_prop_info_t));
4003 static void __bt_hanlde_le_device_disconnection(event_dev_conn_status_t *event_data)
4005 int result = BLUETOOTH_ERROR_INTERNAL;
4006 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
4008 _bt_convert_addr_type_to_string(address, (unsigned char *)event_data->address.addr);
4010 /* DBUS Return with fail of pending BT_CONNECT_LE for all the apps */
4011 BT_INFO("Local GATT Client disconnected: Remote addr[%s] ", address);
4013 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE, address,
4014 BT_ADDRESS_STRING_SIZE);
4018 static void __bt_handle_client_notification_registered(
4019 event_gattc_regdereg_notify_t *event_data,
4020 gboolean is_registered)
4022 int result = BLUETOOTH_ERROR_NONE;
4023 struct gatt_server_info_t *conn_info = NULL;
4024 bt_gatt_notif_reg_info_t notif_info;
4027 memset(¬if_info, 0x00, sizeof(bt_gatt_notif_reg_info_t));
4029 BT_INFO("Client Interface [%d] status [%d]",
4030 event_data->client_if,
4031 event_data->status);
4033 /* Extract Address from conn_id of event data */
4034 conn_info = __bt_find_remote_gatt_server_info_from_client_if(
4035 event_data->client_if);
4038 BT_INFO("Connection Info is not present, return");
4041 BT_INFO("Notification Registered for addr [%s]",
4045 /* Fill svc informations in buffer */
4046 memcpy(¬if_info.svc_uuid,
4047 event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4048 notif_info.svc_inst = event_data->srvc_id.id.inst_id;
4050 /* Fill char in buffer */
4051 memcpy(¬if_info.char_uuid,
4052 event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4053 notif_info.char_inst = event_data->char_id.inst_id;
4055 /* Fill remote device address */
4056 _bt_convert_addr_string_to_type(notif_info.addr.addr, conn_info->addr);
4058 notif_info.is_registered = is_registered;
4060 if (event_data->status != OAL_STATUS_SUCCESS)
4061 result = BLUETOOTH_ERROR_INTERNAL;
4063 /* Send DBUS Return for BT_GATT_WATCH_CHARACTERISTIC */
4064 __bt_gatt_handle_pending_request_info(result,
4065 BT_GATT_WATCH_CHARACTERISTIC,
4067 sizeof(bt_gatt_notif_reg_info_t));
4070 static void __bt_handle_client_notification_data(event_gattc_notify_data *event_data)
4072 /* No status in this event from OAL */
4073 int result = BLUETOOTH_ERROR_NONE;
4075 /* Read Information data structures */
4076 GVariant *param = NULL;
4077 GVariant *data = NULL;
4078 GVariant *data_svc_uuid = NULL;
4079 GVariant *data_char_uuid = NULL;
4080 char *read_val = NULL;
4081 char *svc_uuid = NULL;
4082 char *char_uuid = NULL;
4088 BT_INFO("Notifcation of charc data changed");
4090 if (event_data->data_len > 0) {
4092 for (i = 0; i < event_data->data_len; i++)
4093 BT_INFO("Data[%d] = [0x%x]", i, event_data->data[i]);
4096 addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
4097 _bt_convert_addr_type_to_string(addr,
4098 (unsigned char *)&(event_data->address.addr));
4101 read_val = g_memdup(&event_data->data[0], event_data->data_len);
4103 data = g_variant_new_from_data(
4104 G_VARIANT_TYPE_BYTESTRING,
4106 event_data->data_len,
4109 svc_uuid = g_memdup(&event_data->srvc_id.id.uuid.uuid[0], uuid_len);
4111 data_svc_uuid = g_variant_new_from_data(
4112 G_VARIANT_TYPE_BYTESTRING,
4118 char_uuid = g_memdup(&event_data->char_id.uuid.uuid[0], uuid_len);
4120 data_char_uuid = g_variant_new_from_data(
4121 G_VARIANT_TYPE_BYTESTRING,
4127 param = g_variant_new("(isn@ayin@ayin@ay)", result,
4131 event_data->srvc_id.id.inst_id,
4134 event_data->char_id.inst_id,
4135 event_data->data_len,
4139 _bt_send_event(BT_GATT_CLIENT_EVENT,
4140 BLUETOOTH_EVENT_GATT_CHAR_VAL_CHANGED,
4143 BT_ERR("No Data!!");
4156 static void __bt_handle_client_service_changed_ind(event_gattc_service_changed_data *event_data)
4158 bt_gatt_service_info_list_t *svc_info_list;
4160 bt_gatt_service_info_t *svc_info;
4161 GVariant *param = NULL;
4162 char *address_str = NULL;
4163 char *uuid_str = NULL;
4165 svc_info_list = __bt_get_service_info_list(event_data->conn_id);
4166 if (svc_info_list == NULL) {
4167 BT_ERR("svc_info_list is NULL");
4171 if (event_data->change_type) {
4172 /* Add service UUID in list */
4173 svc_info = g_malloc0(sizeof(bt_gatt_service_info_t));
4174 memcpy(svc_info->uuid, event_data->uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4175 svc_info->inst_id = event_data->inst_id;
4176 svc_info->is_primary = 1; // TODO: Need to check is_primary is required or not
4177 svc_info_list->services = g_slist_append(svc_info_list->services, svc_info);
4178 BT_DBG("Service created and added in Svc info list or connection ID %d", event_data->conn_id);
4180 /* Remove service UUID in list */
4181 for (l = svc_info_list->services; l != NULL; l = g_slist_next(l)) {
4182 svc_info = (bt_gatt_service_info_t *)l->data;
4183 if (svc_info == NULL)
4186 if (!memcmp(svc_info->uuid, event_data->uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)) {
4187 svc_info_list->services = g_slist_remove(svc_info_list->services, svc_info);
4188 __bt_free_service_info(svc_info);
4194 address_str = g_malloc0(BT_ADDRESS_STRING_SIZE);
4195 uuid_str = g_malloc0(BT_UUID_STRING_MAX);
4196 _bt_convert_addr_type_to_string(address_str, event_data->address.addr);
4197 _bt_uuid_to_string(&event_data->uuid, uuid_str);
4199 param = g_variant_new("(iiss)", event_data->inst_id, event_data->change_type, address_str, uuid_str);
4201 _bt_send_event(BT_GATT_CLIENT_EVENT,
4202 BLUETOOTH_EVENT_GATT_CLIENT_SERVICE_CHANGED,
4204 g_free(address_str);
4208 gboolean _bt_is_remote_gatt_device_connected(bluetooth_device_address_t *address)
4211 struct gatt_server_info_t *conn_info = NULL;
4212 gboolean connected = FALSE;
4214 addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
4215 _bt_convert_addr_type_to_string(addr,
4216 (unsigned char *)&(address->addr));
4218 BT_DBG("Check GATT connection status of [%s]", addr);
4219 /* Check if device is already in connected list */
4220 conn_info = _bt_find_remote_gatt_server_info(addr);
4223 BT_DBG("Remote GATT Server device [%s] is Connected", conn_info->addr);
4226 struct gatt_client_info_t *client_info = NULL;
4228 BT_INFO("Remote GATT Server Device [%s] is not Connected", addr);
4230 /* Check if device is already in connected list */
4231 client_info = _bt_find_remote_gatt_client_info(addr);
4234 BT_DBG("Remote Client device [%s] is Connected", client_info->addr);
4237 BT_INFO("Remote GATT Client Device [%s] is not Connected", addr);
4246 int _bt_connect_le_device(bluetooth_device_address_t *address,
4247 int auto_connect, int client_id)
4249 struct gatt_server_info_t *conn_info = NULL;
4250 struct gatt_out_conn_info_t *out_conn_info = NULL;
4252 invocation_info_t *req_info = NULL;
4253 int ret = OAL_STATUS_SUCCESS;
4255 char *remote_address = NULL;
4257 BT_CHECK_PARAMETER(address, return);
4259 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4260 _bt_convert_addr_type_to_string(addr, address->addr);
4261 BT_INFO("GATT Client connect request for address [%s] client instance [%d]",
4265 /* Check if Remote Device is already under connection progress */
4266 req_info = _bt_get_request_info_data_from_function_name(BT_CONNECT_LE);
4268 remote_address = (char*)req_info->user_data;
4269 if (remote_address && !strcasecmp(remote_address, addr)) {/* Address matched */
4270 BT_INFO("Already Connection ongoing for same remote GATT Server address [%s]", remote_address);
4271 /* Return and wait for events to be sent to all apps */
4273 return BLUETOOTH_ERROR_IN_PROGRESS;
4277 /* Check if remote GATT Server is connected or not */
4278 conn_info = _bt_find_remote_gatt_server_info(addr);
4280 BT_ERR("GATT Server is already connected..");
4282 return BLUETOOTH_ERROR_ALREADY_CONNECT;
4285 /* TODO Check Requirement of holding Advertisement before initiating LE connect */
4287 /* Check if app sent 0 client id for connection, in such case, use default gatt client ID */
4288 if (client_id == 0) {
4289 BT_INFO("GATT CLient connect request sent by an app without any client instance [%d]",
4291 BT_INFO("Assign default GATT client id [%d]", gatt_default_client);
4292 client_id = gatt_default_client;
4295 BT_INFO("Connect using CLient ID [%d]", client_id);
4296 ret = gattc_connect(client_id, (bt_address_t*)(address), auto_connect);
4298 if (ret != OAL_STATUS_SUCCESS) {
4299 BT_ERR("ret: %d", ret);
4301 return BLUETOOTH_ERROR_INTERNAL;
4304 /* Mark this as outgoing connection */
4305 out_conn_info = g_new0(struct gatt_out_conn_info_t, 1);
4306 out_conn_info->addr = g_strdup(addr);
4307 out_conn_info->client_id = client_id;
4308 BT_INFO("Added outgoing connection info addr[%s]", out_conn_info->addr);
4309 outgoing_gatt_conn_list = g_slist_append(outgoing_gatt_conn_list, out_conn_info);
4312 return BLUETOOTH_ERROR_NONE;
4315 int _bt_gatt_get_primary_services(char *address)
4317 BT_CHECK_PARAMETER(address, return);
4318 struct gatt_server_info_t *conn_info = NULL;
4319 invocation_info_t *req_info = NULL;
4320 int ret = OAL_STATUS_SUCCESS;
4323 /* Check if any app is already browsing primary services on the same remote GATT Server */
4324 req_info = _bt_get_request_info_data(BT_GATT_GET_PRIMARY_SERVICES, address);
4326 BT_INFO("Already Primary Service Browsing ongoing for same rmeote GATT Server");
4327 /* Return and wait for events to be sent to all apps */
4328 return BLUETOOTH_ERROR_NONE;
4331 /* Check if remote GATT Server is connected or not */
4332 conn_info = _bt_find_remote_gatt_server_info(address);
4334 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4335 conn_info->addr, conn_info->connection_id);
4337 BT_ERR("GATT Server is not yet connected..");
4338 return BLUETOOTH_ERROR_NOT_CONNECTED;
4341 BT_INFO("Remove GATT server info from List..");
4342 /* Remove all services from info list_gatt_info */
4343 __bt_cleanup_remote_services(conn_info);
4345 /* Send Primary Service Browsing request to stack */
4346 ret = gattc_search_service(conn_info->connection_id, NULL);
4347 if (ret != OAL_STATUS_SUCCESS) {
4348 BT_ERR("ret: %d", ret);
4349 return BLUETOOTH_ERROR_INTERNAL;
4351 return BLUETOOTH_ERROR_NONE;
4354 int _bt_gatt_get_all_characteristic(bluetooth_gatt_client_svc_prop_info_t *svc)
4356 BT_CHECK_PARAMETER(svc, return);
4357 struct gatt_server_info_t *conn_info = NULL;
4358 invocation_info_t *req_info = NULL;
4359 bluetooth_gatt_client_svc_prop_info_t *prop;
4360 oal_gatt_srvc_id_t srvc_id;
4361 int ret = OAL_STATUS_SUCCESS;
4364 /* Check if any app is already browsing characteristics of the same service on the same remote GATT Server */
4365 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_GET_SERVICE_PROPERTIES);
4367 prop = (bluetooth_gatt_client_svc_prop_info_t*)req_info->user_data;
4368 if (prop && !memcmp(svc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t))
4369 && memcmp(prop->svc.uuid, svc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
4370 && prop->svc.instance_id == svc->svc.instance_id) {
4371 BT_INFO("Already Properties browsing for Primary Service ongoing for same remote GATT Server");
4372 /* Return and wait for events to be sent to all apps */
4373 return BLUETOOTH_ERROR_NONE;
4377 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4378 _bt_convert_addr_type_to_string(addr, svc->device_address.addr);
4380 /* Check if remote GATT Server is connected or not */
4381 conn_info = _bt_find_remote_gatt_server_info(addr);
4383 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4384 conn_info->addr, conn_info->connection_id);
4386 BT_ERR("GATT Server is not yet connected..");
4388 return BLUETOOTH_ERROR_NOT_CONNECTED;
4391 srvc_id.is_prmry = TRUE;
4392 srvc_id.id.inst_id = svc->svc.instance_id;
4393 memcpy(srvc_id.id.uuid.uuid, svc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4395 /* Search All Characteristic */
4396 ret = gattc_get_characteristic(conn_info->connection_id, &srvc_id, NULL);
4397 if (ret != OAL_STATUS_SUCCESS) {
4398 BT_ERR("ret: %d", ret);
4400 return BLUETOOTH_ERROR_INTERNAL;
4403 return BLUETOOTH_ERROR_NONE;
4406 int _bt_gatt_get_all_characteristic_properties(
4407 bluetooth_gatt_client_char_prop_info_t *chr)
4409 struct gatt_server_info_t *conn_info = NULL;
4410 invocation_info_t *req_info = NULL;
4411 bluetooth_gatt_client_char_prop_info_t *prop;
4412 oal_gatt_srvc_id_t srvc_id;
4413 oal_gatt_id_t char_id;
4414 int ret = OAL_STATUS_SUCCESS;
4417 BT_CHECK_PARAMETER(chr, return);
4419 /* Check if any app is already browsing descriptors of the same char of
4420 particular service on the same remote GATT Server */
4421 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_GET_CHARACTERISTIC_PROPERTIES);
4423 prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
4424 if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4425 && memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4426 && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4427 && memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4428 && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
4429 BT_INFO("Already Properties browsing for Characteristic ongoing for same remote GATT Server");
4430 /* Return and wait for events to be sent to all apps */
4431 return BLUETOOTH_ERROR_NONE;
4435 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4436 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4438 /* Check if remote GATT Server is connected or not */
4439 conn_info = _bt_find_remote_gatt_server_info(addr);
4441 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4442 conn_info->addr, conn_info->connection_id);
4444 BT_ERR("GATT Server is not yet connected..");
4446 return BLUETOOTH_ERROR_NOT_CONNECTED;
4449 srvc_id.is_prmry = TRUE;
4450 srvc_id.id.inst_id = chr->svc.instance_id;
4451 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4453 char_id.inst_id = chr->characteristic.instance_id;
4454 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4456 /* Search All Descriptors */
4457 ret = gattc_get_descriptor(conn_info->connection_id, &srvc_id, &char_id, NULL);
4458 if (ret != OAL_STATUS_SUCCESS) {
4459 BT_ERR("ret: %d", ret);
4461 return BLUETOOTH_ERROR_INTERNAL;
4464 return BLUETOOTH_ERROR_NONE;
4467 int _bt_gatt_read_characteristic_value(
4468 bluetooth_gatt_client_char_prop_info_t *chr)
4470 struct gatt_server_info_t *conn_info = NULL;
4471 invocation_info_t *req_info = NULL;
4472 bluetooth_gatt_client_char_prop_info_t *prop;
4473 oal_gatt_srvc_id_t srvc_id;
4474 oal_gatt_id_t char_id;
4475 int ret = OAL_STATUS_SUCCESS;
4478 BT_CHECK_PARAMETER(chr, return);
4480 /* Check if any app is already Reading characteristic of the same char of
4481 particular service on the same remote GATT Server */
4482 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_READ_CHARACTERISTIC);
4484 prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
4485 if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4486 && memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4487 && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4488 && memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4489 && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
4490 BT_INFO("Already Characteristic value Read operation in progress for same remote GATT Server");
4491 /* Return and wait for events to be sent to all apps */
4492 return BLUETOOTH_ERROR_NONE;
4496 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4497 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4499 /* Check if remote GATT Server is connected or not */
4500 conn_info = _bt_find_remote_gatt_server_info(addr);
4502 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4503 conn_info->addr, conn_info->connection_id);
4505 BT_ERR("GATT Server is not yet connected..");
4507 return BLUETOOTH_ERROR_NOT_CONNECTED;
4510 srvc_id.is_prmry = TRUE;
4511 srvc_id.id.inst_id = chr->svc.instance_id;
4512 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4514 char_id.inst_id = chr->characteristic.instance_id;
4515 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4517 /* Search All Descriptors */
4518 ret = gattc_read_characteristic(conn_info->connection_id, &srvc_id, &char_id, OAL_GATT_AUTH_REQ_NONE);
4519 if (ret != OAL_STATUS_SUCCESS) {
4520 BT_ERR("ret: %d", ret);
4522 return BLUETOOTH_ERROR_INTERNAL;
4525 return BLUETOOTH_ERROR_NONE;
4528 int _bt_gatt_read_descriptor_value(
4529 bluetooth_gatt_client_desc_prop_info_t *desc)
4531 struct gatt_server_info_t *conn_info = NULL;
4532 invocation_info_t *req_info = NULL;
4533 bluetooth_gatt_client_desc_prop_info_t *prop;
4534 oal_gatt_srvc_id_t srvc_id;
4535 oal_gatt_id_t char_id;
4536 oal_gatt_id_t desc_id;
4537 int ret = OAL_STATUS_SUCCESS;
4540 BT_CHECK_PARAMETER(desc, return);
4542 /* Check if any app is already Reading descriptors of the same char of
4543 particular service on the same remote GATT Server */
4544 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_READ_DESCRIPTOR_VALUE);
4546 prop = (bluetooth_gatt_client_desc_prop_info_t*)req_info->user_data;
4547 if (prop && !memcmp(desc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4548 && memcmp(desc->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4549 && desc->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4550 && memcmp(desc->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4551 && desc->characteristic.instance_id == prop->characteristic.instance_id /* Characteristic Instance ID matched */
4552 && memcmp(desc->descriptor.uuid, prop->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Descriptor UUID matched */
4553 && desc->descriptor.instance_id == prop->descriptor.instance_id) { /* Descriptor Instance ID matched */
4554 BT_INFO("Already Descriptor value Read operation in progress for same remote GATT Server");
4555 /* Return and wait for events to be sent to all apps */
4556 return BLUETOOTH_ERROR_NONE;
4560 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4561 _bt_convert_addr_type_to_string(addr, desc->device_address.addr);
4563 /* Check if remote GATT Server is connected or not */
4564 conn_info = _bt_find_remote_gatt_server_info(addr);
4566 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4567 conn_info->addr, conn_info->connection_id);
4569 BT_ERR("GATT Server is not yet connected..");
4571 return BLUETOOTH_ERROR_NOT_CONNECTED;
4574 srvc_id.is_prmry = TRUE;
4575 srvc_id.id.inst_id = desc->svc.instance_id;
4576 memcpy(srvc_id.id.uuid.uuid, desc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4578 char_id.inst_id = desc->characteristic.instance_id;
4579 memcpy(char_id.uuid.uuid, desc->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4581 desc_id.inst_id = desc->descriptor.instance_id;
4582 memcpy(desc_id.uuid.uuid, desc->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4584 /* Search All Descriptors */
4585 ret = gattc_read_descriptor(conn_info->connection_id, &srvc_id, &char_id,
4586 &desc_id, OAL_GATT_AUTH_REQ_NONE);
4587 if (ret != OAL_STATUS_SUCCESS) {
4588 BT_ERR("ret: %d", ret);
4590 return BLUETOOTH_ERROR_INTERNAL;
4593 return BLUETOOTH_ERROR_NONE;
4597 int _bt_gatt_acquire_notify(bluetooth_gatt_client_char_prop_info_t *chr, int *fd, int *mtu)
4599 struct gatt_server_info_t *conn_info = NULL;
4600 oal_gatt_srvc_id_t srvc_id;
4601 oal_gatt_id_t char_id;
4602 int ret = OAL_STATUS_SUCCESS;
4605 BT_CHECK_PARAMETER(chr, return);
4607 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4608 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4610 /* Check if remote GATT Server is connected or not */
4611 conn_info = _bt_find_remote_gatt_server_info(addr);
4613 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4614 conn_info->addr, conn_info->connection_id);
4616 BT_ERR("GATT Server is not yet connected..");
4618 return BLUETOOTH_ERROR_NOT_CONNECTED;
4621 srvc_id.is_prmry = TRUE;
4622 srvc_id.id.inst_id = chr->svc.instance_id;
4623 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4625 char_id.inst_id = chr->characteristic.instance_id;
4626 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4628 ret = gattc_acquire_notify(conn_info->connection_id, &srvc_id, &char_id, fd, mtu);
4630 if (ret != OAL_STATUS_SUCCESS) {
4631 BT_ERR("ret: %d", ret);
4633 return BLUETOOTH_ERROR_INTERNAL;
4635 BT_INFO("GATT characterstics FD [%d] mtu[%d]", *fd, *mtu);
4637 return BLUETOOTH_ERROR_NONE;
4641 int _bt_gatt_acquire_write(bluetooth_gatt_client_char_prop_info_t *chr, int *fd, int *mtu)
4644 struct gatt_server_info_t *conn_info = NULL;
4645 oal_gatt_srvc_id_t srvc_id;
4646 oal_gatt_id_t char_id;
4647 int ret = OAL_STATUS_SUCCESS;
4650 BT_CHECK_PARAMETER(chr, return);
4652 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4653 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4655 /* Check if remote GATT Server is connected or not */
4656 conn_info = _bt_find_remote_gatt_server_info(addr);
4658 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4659 conn_info->addr, conn_info->connection_id);
4661 BT_ERR("GATT Server is not yet connected..");
4663 return BLUETOOTH_ERROR_NOT_CONNECTED;
4666 srvc_id.is_prmry = TRUE;
4667 srvc_id.id.inst_id = chr->svc.instance_id;
4668 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4670 char_id.inst_id = chr->characteristic.instance_id;
4671 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4673 ret = gattc_acquire_write(conn_info->connection_id, &srvc_id, &char_id,
4674 OAL_GATT_AUTH_REQ_NONE, fd, mtu);
4675 if (ret != OAL_STATUS_SUCCESS) {
4676 BT_ERR("ret: %d", ret);
4678 return BLUETOOTH_ERROR_INTERNAL;
4680 BT_INFO("GATT characterstics FD [%d] mtu [%d]", *fd, *mtu);
4682 return BLUETOOTH_ERROR_NONE;
4687 /* Write Characteristic */
4688 int _bt_gatt_write_characteristic_value_by_type(
4689 bluetooth_gatt_client_char_prop_info_t *chr,
4690 bluetooth_gatt_att_data_t *data,
4691 bluetooth_gatt_write_type_e write_type)
4693 struct gatt_server_info_t *conn_info = NULL;
4694 invocation_info_t *req_info = NULL;
4695 bluetooth_gatt_client_char_prop_info_t *prop;
4696 oal_gatt_srvc_id_t srvc_id;
4697 oal_gatt_id_t char_id;
4698 int ret = OAL_STATUS_SUCCESS;
4702 BT_CHECK_PARAMETER(chr, return);
4703 BT_CHECK_PARAMETER(data, return);
4707 /* Check if any app is already writing same char of
4708 particular service on the same remote GATT Server */
4709 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE);
4711 prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
4712 if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4713 && memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4714 && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4715 && memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4716 && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
4717 BT_INFO("Already Characteristic Write Value operation in progress for same remote GATT Server");
4718 /* Return and wait for events to be sent to all apps */
4719 return BLUETOOTH_ERROR_NONE;
4723 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4724 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4726 /* Check if remote GATT Server is connected or not */
4727 conn_info = _bt_find_remote_gatt_server_info(addr);
4729 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4730 conn_info->addr, conn_info->connection_id);
4732 BT_ERR("GATT Server is not yet connected..");
4734 return BLUETOOTH_ERROR_NOT_CONNECTED;
4737 srvc_id.is_prmry = TRUE;
4738 srvc_id.id.inst_id = chr->svc.instance_id;
4739 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4741 char_id.inst_id = chr->characteristic.instance_id;
4742 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4744 /* Write CHar value */
4746 BT_INFO("Connection ID [%d] write type [%d] data length [%d]", conn_info->connection_id, write_type, data->length);
4747 for (k = 0; k < data->length; k++)
4748 BT_INFO("Data[%d] [0x%x]", k, data->data[k]);
4750 ret = gattc_write_characteristic(conn_info->connection_id,
4752 (oal_gatt_write_type_t)write_type, data->length,
4753 OAL_GATT_AUTH_REQ_NONE, (char *)(&data->data[0]));
4754 if (ret != OAL_STATUS_SUCCESS) {
4755 BT_ERR("ret: %d", ret);
4757 return BLUETOOTH_ERROR_INTERNAL;
4760 return BLUETOOTH_ERROR_NONE;
4763 /* Write Descriptor */
4764 int _bt_gatt_write_descriptor_value_by_type(
4765 bluetooth_gatt_client_desc_prop_info_t *desc,
4766 bluetooth_gatt_att_data_t *data,
4767 bluetooth_gatt_write_type_e write_type)
4769 struct gatt_server_info_t *conn_info = NULL;
4770 invocation_info_t *req_info = NULL;
4771 bluetooth_gatt_client_desc_prop_info_t *prop;
4772 oal_gatt_srvc_id_t srvc_id;
4773 oal_gatt_id_t char_id;
4774 oal_gatt_id_t desc_id;
4775 int ret = OAL_STATUS_SUCCESS;
4779 BT_CHECK_PARAMETER(desc, return);
4780 BT_CHECK_PARAMETER(data, return);
4784 /* Check if any app is already writing on same Descriptor of the same char of
4785 particular service on the same remote GATT Server */
4786 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_WRITE_DESCRIPTOR_VALUE);
4788 prop = (bluetooth_gatt_client_desc_prop_info_t*)req_info->user_data;
4789 if (prop && !memcmp(desc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4790 && memcmp(desc->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4791 && desc->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4792 && memcmp(desc->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4793 && desc->characteristic.instance_id == prop->characteristic.instance_id /* Characteristic Instance ID matched */
4794 && memcmp(desc->descriptor.uuid, prop->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Descriptor UUID matched */
4795 && desc->descriptor.instance_id == prop->descriptor.instance_id) { /* Descriptor Instance ID matched */
4796 BT_INFO("Already Descriptor value Write operation in progress for same remote GATT Server");
4797 /* Return and wait for events to be sent to all apps */
4798 return BLUETOOTH_ERROR_NONE;
4802 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4803 _bt_convert_addr_type_to_string(addr, desc->device_address.addr);
4805 /* Check if remote GATT Server is connected or not */
4806 conn_info = _bt_find_remote_gatt_server_info(addr);
4808 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4809 conn_info->addr, conn_info->connection_id);
4811 BT_ERR("GATT Server is not yet connected..");
4813 return BLUETOOTH_ERROR_NOT_CONNECTED;
4816 srvc_id.is_prmry = TRUE;
4817 srvc_id.id.inst_id = desc->svc.instance_id;
4818 memcpy(srvc_id.id.uuid.uuid, desc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4820 char_id.inst_id = desc->characteristic.instance_id;
4821 memcpy(char_id.uuid.uuid, desc->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4823 desc_id.inst_id = desc->descriptor.instance_id;
4824 memcpy(desc_id.uuid.uuid, desc->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4827 BT_INFO("Connection ID [%d] write type [%d] data length [%d]", conn_info->connection_id, write_type, data->length);
4828 for (k = 0; k < data->length; k++)
4829 BT_INFO("Data[%d] [0x%x]", k, data->data[k]);
4831 ret = gattc_write_descriptor(conn_info->connection_id,
4832 &srvc_id, &char_id, &desc_id,
4833 (oal_gatt_write_type_t)write_type, data->length,
4834 OAL_GATT_AUTH_REQ_NONE, (char *)(&data->data[0]));
4835 if (ret != OAL_STATUS_SUCCESS) {
4836 BT_ERR("ret: %d", ret);
4838 return BLUETOOTH_ERROR_INTERNAL;
4841 return BLUETOOTH_ERROR_NONE;
4844 int _bt_gatt_watch_characteristic(
4845 bluetooth_gatt_client_char_prop_info_t *chr,
4849 struct gatt_server_info_t *conn_info = NULL;
4850 oal_gatt_srvc_id_t srvc_id;
4851 oal_gatt_id_t char_id;
4852 int ret = OAL_STATUS_SUCCESS;
4855 BT_CHECK_PARAMETER(chr, return);
4857 BT_INFO("Client ID [%d] Is Notify [%d]", client_id, is_notify);
4859 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4860 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4862 /* Check if remote GATT Server is connected or not */
4863 conn_info = _bt_find_remote_gatt_server_info(addr);
4865 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4866 conn_info->addr, conn_info->connection_id);
4868 BT_ERR("GATT Server is not yet connected..");
4870 return BLUETOOTH_ERROR_NOT_CONNECTED;
4872 srvc_id.is_prmry = TRUE;
4873 srvc_id.id.inst_id = chr->svc.instance_id;
4874 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4876 char_id.inst_id = chr->characteristic.instance_id;
4877 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4879 /* Register or unregister Notification characteristic */
4881 ret = gattc_register_for_notification(conn_info->client_id,
4882 (bt_address_t*)&(chr->device_address),
4883 &srvc_id, &char_id);
4885 ret = gattc_deregister_for_notification(conn_info->client_id,
4886 (bt_address_t*)&(chr->device_address),
4887 &srvc_id, &char_id);
4889 BT_INFO("Result[%d]", ret);
4890 if (ret != OAL_STATUS_SUCCESS) {
4891 BT_ERR("ret: %d", ret);
4893 return BLUETOOTH_ERROR_INTERNAL;
4896 return BLUETOOTH_ERROR_NONE;
4900 int _bt_disconnect_le_device(bluetooth_device_address_t *address,
4903 struct gatt_server_info_t *conn_info = NULL;
4904 struct gatt_client_info_t *rem_client_conn_info = NULL;
4905 invocation_info_t *req_info = NULL;
4906 int ret = OAL_STATUS_SUCCESS;
4908 char *remote_address = NULL;
4910 BT_CHECK_PARAMETER(address, return);
4914 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4915 _bt_convert_addr_type_to_string(addr, address->addr);
4916 BT_INFO("GATT Client Disconnect request for address [%s]", addr);
4918 /* Check if Remote Device is already under connection progress */
4919 req_info = _bt_get_request_info_data_from_function_name(BT_DISCONNECT_LE);
4921 remote_address = (char*)req_info->user_data;
4922 if (remote_address && !strcasecmp(remote_address, addr)) {/* Address matched */
4923 BT_INFO("Already DisConnection ongoing for same remote GATT Server address [%s]", remote_address);
4924 /* Return success and wait for events to be sent to all apps */
4926 return BLUETOOTH_ERROR_IN_PROGRESS;
4929 /* Check if remote GATT Server is connected or not */
4930 conn_info = _bt_find_remote_gatt_server_info(addr);
4932 /* Check if app sent 0 client id for Disconnection, in such case, use default gatt client ID */
4933 if (client_id == 0) {
4934 BT_INFO("GATT CLient Disconnect request sent by an app without any client instance [%d]",
4936 BT_INFO("Assign default GATT client id [%d]", gatt_default_client);
4937 client_id = gatt_default_client;
4940 BT_INFO("Disconnect remote gatt server using CLient ID [%d] Connection ID [%d]", client_id, conn_info->connection_id);
4941 ret = gattc_disconnect(client_id, (bt_address_t*)(address),
4942 conn_info->connection_id);
4944 /* check if remote client is connected */
4945 rem_client_conn_info = _bt_find_remote_gatt_client_info(addr);
4947 if (!rem_client_conn_info || client_id != 0) {
4948 BT_ERR("GATT device is not connected..");
4950 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
4953 BT_INFO("Disconnect remote gatt client ");
4955 ret = gatts_disconnect(rem_client_conn_info->instance_id,
4956 (bt_address_t*)(address), rem_client_conn_info->connection_id);
4959 if (ret != OAL_STATUS_SUCCESS) {
4960 BT_ERR("ret: %d", ret);
4962 return BLUETOOTH_ERROR_INTERNAL;
4965 return BLUETOOTH_ERROR_NONE;
4968 int _bt_gatt_watch_service_changed_indication(const char *sender,
4969 bluetooth_device_address_t *address,
4970 gboolean is_enabled)
4973 bt_service_app_info_t *info = NULL;
4975 BT_INFO("%s Servic changed Indication watcher for app [%s]",
4976 is_enabled ? "Enable":"Disable", sender);
4978 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
4981 if (g_strcmp0(sender, info->sender) == 0 &&
4982 memcmp(info->address.addr, address->addr,
4983 sizeof(bluetooth_device_address_t)) == 0) {
4984 BT_DBG("Found GATT client App.. [%s], sender [%s]", info->uuid, info->sender);
4985 info->is_watcher_enabled = is_enabled;
4989 return BLUETOOTH_ERROR_NONE;
4992 int _bt_unregister_gatt_client_instance(const char *sender, int client_id)
4994 bt_service_app_info_t *info = NULL;
4997 BT_INFO("Unregister Allocated GATT Client instance [%s] Client ID [%d]", sender, client_id);
4999 /* Unregister CLient instance associated with address X. It is possible that another app still
5000 has client_id valid for same remote address */
5001 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
5004 /* Exact matching of sender */
5005 if (!g_strcmp0(info->sender, sender) && info->client_id == client_id) { /* Check for only valid GATT client Instance */
5006 BT_INFO("Unregister GATT client instance [%d]", info->client_id);
5007 numapps[k].client_id = -1;
5008 numapps[k].is_initialized = FALSE;
5009 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
5010 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
5011 memset(&numapps[k].address.addr, 0x00, sizeof(bluetooth_device_address_t));
5013 /* Its a GATT Client Instance */
5014 ret = gattc_deregister(client_id);
5015 if (ret != OAL_STATUS_SUCCESS) {
5016 BT_ERR("DeAllocate GATT Client instance with stack Fail ret: %d", ret);
5017 return BLUETOOTH_ERROR_INTERNAL;
5019 return BLUETOOTH_ERROR_NONE;
5024 return BLUETOOTH_ERROR_NOT_FOUND;
5027 static void __bt_handle_client_mtu_exchange_completed(event_gattc_mtu_configured_t *event_data)
5029 int result = BLUETOOTH_ERROR_NONE;
5030 struct gatt_server_info_t *conn_info = NULL;
5031 GVariant *param = NULL;
5035 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(event_data->conn_id);
5036 if (conn_info == NULL) {
5037 BT_ERR("Cant find connection Information");
5041 BT_INFO("GATT Client: MTU Configured from addr [%s] status [%d] MTU size [%d]",
5042 conn_info->addr, event_data->status, event_data->mtu);
5044 if (event_data->status != OAL_STATUS_SUCCESS)
5045 result = BLUETOOTH_ERROR_INTERNAL;
5047 BT_INFO("reply BT_REQ_ATT_MTU ");
5049 /* DBUS Return fo BT_REQ_ATT_MTU for all the apps */
5050 __bt_gatt_handle_pending_request_info(result, BT_REQ_ATT_MTU, conn_info->addr,
5051 BT_ADDRESS_STRING_SIZE);
5053 if (result == BLUETOOTH_ERROR_NONE) {
5054 BT_INFO("sending mtu event");
5056 mtu = event_data->mtu;
5057 param = g_variant_new("(isqy)",
5063 /* Send event to BT-API */
5064 _bt_send_event(BT_DEVICE_EVENT,
5065 BLUETOOTH_EVENT_GATT_ATT_MTU_CHANGED,
5068 /* Update the MTU for current connection */
5069 BT_INFO("Updating the MTU for current connection");
5070 __bt_update_mtu_gatt_device(conn_info->addr, event_data->mtu);
5074 static struct gatt_mtu_info_t *__bt_find_mtu_gatt_device(char *address)
5077 struct gatt_mtu_info_t *info = NULL;
5081 for (l = gatt_mtu_info_list; l != NULL; l = g_slist_next(l)) {
5082 info = (struct gatt_mtu_info_t*)l->data;
5086 if (!g_strcmp0(info->addr, address)) {
5087 BT_INFO("Remote GATT device found addr[%s]", info->addr);
5092 BT_INFO("Not found Remote GATT device addr[%s]", address);
5096 static void __bt_remove_mtu_gatt_device(char *address)
5098 struct gatt_mtu_info_t *dev_info = NULL;
5102 dev_info = __bt_find_mtu_gatt_device(address);
5105 BT_INFO("removing the gatt device from mtu list");
5106 gatt_mtu_info_list = g_slist_remove(gatt_mtu_info_list, dev_info);
5107 g_free(dev_info->addr);
5112 static void __bt_add_mtu_gatt_device(char *address)
5114 struct gatt_mtu_info_t *dev_info = NULL;
5118 dev_info = __bt_find_mtu_gatt_device(address);
5121 BT_INFO("adding the gatt device in mtu list");
5122 dev_info = g_new0(struct gatt_mtu_info_t, 1);
5123 dev_info->addr = g_strdup(address);
5124 dev_info->att_mtu = BT_DEFAULT_ATT_MTU;
5125 gatt_mtu_info_list = g_slist_append(gatt_mtu_info_list, dev_info);
5129 static void __bt_update_mtu_gatt_device(char *address, int mtu)
5131 struct gatt_mtu_info_t *dev_info = NULL;
5135 dev_info = __bt_find_mtu_gatt_device(address);
5138 BT_INFO("Udating the mtu");
5139 dev_info->att_mtu = mtu;