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_unregister_gatt_client_instance(int client_if);
246 static void __bt_service_reset_gatt_data(void);
248 #ifdef TIZEN_GATT_CLIENT
249 static void __bt_handle_client_instance_registered(event_gattc_register_t *data);
250 static void __bt_handle_client_connected(event_gattc_conn_t *event_data);
251 static void __bt_handle_client_disconnected(event_gattc_conn_t *event_data);
252 static void __bt_handle_client_service_search_completed(event_gattc_conn_status_t *event_data);
253 static void __bt_handle_client_service_search_result(event_gattc_service_result_t *event_data);
254 static void __bt_handle_client_characteristic_search_result(
255 event_gattc_characteristic_result_t *event_data);
256 static void __bt_handle_client_descriptor_search_result(event_gattc_descriptor_result_t *event_data);
257 static void __bt_handle_client_characteristic_read_data(event_gattc_read_data *event_data);
258 static void __bt_handle_client_descriptor_read_data(event_gattc_read_data *event_data);
259 static void __bt_handle_client_characteristic_write_data(event_gattc_write_data *event_data);
260 static void __bt_handle_client_descriptor_write_data(event_gattc_write_data *event_data);
261 static void __bt_hanlde_le_device_disconnection(event_dev_conn_status_t *event_data);
262 static void __bt_handle_client_notification_registered(event_gattc_regdereg_notify_t *event_data,
263 gboolean is_registered);
264 static void __bt_handle_client_notification_data(event_gattc_notify_data *event_data);
265 static void __bt_handle_client_service_changed_ind(event_gattc_service_changed_data *event_data);
266 static void __bt_handle_client_mtu_exchange_completed(event_gattc_mtu_configured_t *event_data);
269 static int __bt_unregister_gatt_server_instance(int server_instance);
272 struct gatt_mtu_info_t {
273 char *addr; /* Remote GATT Server address */
277 static GSList *gatt_mtu_info_list = NULL;
279 static struct gatt_mtu_info_t *__bt_find_mtu_gatt_device(char *address);
280 static void __bt_remove_mtu_gatt_device(char *address);
281 static void __bt_add_mtu_gatt_device(char *address);
282 static void __bt_update_mtu_gatt_device(char *address, int mtu);
284 /* Linked List of GATT requests from Remote GATT Clients */
285 static GSList *gatt_server_requests = NULL;
287 /* Linked List of connected Remote GATT clients */
288 static GSList *gatt_client_info_list = NULL;
290 /* Number of clients to be notified to */
291 static int num_indicate_clients;
293 /* List of applications */
294 static bt_service_app_info_t numapps[MAX_APPS_SUPPORTED];
296 static void __bt_gatt_handle_pending_request_info(int result,
297 int service_function, void *data, unsigned int size);
299 static void __bt_handle_server_instance_registered(event_gatts_register_t *data);
301 static void __bt_gatt_event_handler(int event_type, gpointer event_data);
308 void _bt_check_adv_app_termination(const char *name)
310 bt_service_app_info_t *app = NULL;
312 int apps[MAX_APPS_SUPPORTED] = { 0, };
314 ret_if(NULL == name);
316 memset(&apps, -1, sizeof(apps));
318 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
321 /* Search for a app which has same sender and stop adv is running */
322 if (!strncasecmp(app->sender, name, strlen(name)) && app->is_initialized == TRUE) {
323 BT_DBG("Match found, name: %s", name);
325 /* TODO 2: Need to manage app info as list, not array.
326 This loop always run for MAX count if any apps are terminated.
329 /* Save instances of all apps that need to be unregistered */
330 if (app->instance_id != -1) {
332 /* Unregister all service handles with stack */
333 __bt_remove_all_service_handles(app);
335 /* If Advertising is enabled, stop it */
336 if (app->adv_handle > 0) {
337 BT_INFO("Stop advertising on instance ID [%d]", app->instance_id);
338 /* Disable adv if running */
339 BT_INFO("Disable Advertising Adv Handle [%d] sender [%s]",
340 app->adv_handle, name);
341 _bt_set_advertising(app->sender, app->adv_handle, FALSE, FALSE);
344 apps[app->instance_id] = BT_GATT_SERVER; /* App holds a GATT server Instance */
345 } else if (app->client_id != -1) {
347 apps[app->client_id] = BT_GATT_CLIENT; /* App holds a GATT client Instance */
352 /* Unregister all apps one by one */
353 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
354 if (apps[k] == BT_GATT_SERVER) {
355 BT_INFO("Unregister server app[%d]", k);
356 /* Unregister server instance */
357 __bt_unregister_gatt_server_instance(k);
358 } else if (apps[k] == BT_GATT_CLIENT) {
359 BT_INFO("Unregister client app[%d]", k);
360 /* Unregister client instance */
361 __bt_unregister_gatt_client_instance(k);
366 static int __bt_gatt_send_indication_to_all_connected_clients(bluetooth_gatt_att_data_t *data,
367 bluetooth_gatt_server_indication_params_t *param)
370 int ret = OAL_STATUS_SUCCESS;
371 int result = OAL_STATUS_INTERNAL_ERROR;
373 BT_INFO("Current total number of connected clients [%d]", g_slist_length(gatt_client_info_list));
374 for (l = gatt_client_info_list; l != NULL; l = l->next) {
375 struct gatt_client_info_t *info = l->data;
378 BT_INFO("GATT Remote client address [%s] connection Id [%d]", info->addr, info->connection_id);
380 ret = gatts_send_indication(param->instance_id, param->atrribute_handle,
381 info->connection_id, data->length,
382 param->need_confirmation, (char *)(&data->data[0]));
384 BT_INFO("Send Indication to GATT client [%s] result: [%d]", info->addr, ret);
385 if (ret == OAL_STATUS_SUCCESS) {
386 BT_INFO("Send Indication sent successfully to GATT client [%s]", info->addr);
388 num_indicate_clients++;
392 BT_INFO("Indication sending done for total number of clients [%d]", num_indicate_clients);
396 static struct gatt_server_req_info *__bt_gatt_server_find_request_info(int request_id,
397 bluetooth_gatt_att_request_type_e req_type)
401 for (l = gatt_server_requests; l != NULL; l = l->next) {
402 struct gatt_server_req_info *req_info = l->data;
404 if (req_info && req_info->request_id == request_id && req_info->request_type == req_type) {
405 BT_INFO("GATT Server request info found Req ID [%d] handle [%d] conn ID [%d]",
406 req_info->request_id, req_info->attribute_handle, req_info->connection_id);
410 BT_ERR("Gatt Request not found");
414 void _bt_get_adv_handle_from_instance(int server_inst, int *adv_handle)
418 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
419 if (numapps[k].is_initialized == 1 && numapps[k].instance_id == server_inst) {
420 *adv_handle = numapps[k].adv_handle;
426 #ifdef TIZEN_GATT_CLIENT
427 char * _bt_gatt_get_default_gatt_client_uuid(void)
429 return g_strdup(DEFAULT_GATT_CLIENT_UUID);
433 static void __bt_register_default_gatt_client()
438 uuid_str = _bt_gatt_get_default_gatt_client_uuid();
439 _bt_string_to_uuid(uuid_str, (service_uuid_t*)&uuid);
441 BT_INFO("Register Default GATT client uuid [%s]", uuid_str);
443 if (OAL_STATUS_SUCCESS != gattc_register(&uuid)) /* for only Smart Control */
444 BT_ERR("gattc register failed");
450 int _bt_gatt_init(void)
452 const char *stack_name = NULL;
456 if (OAL_STATUS_SUCCESS != gatt_enable()) {
457 BT_ERR("gatt Init failed");
458 return BLUETOOTH_ERROR_INTERNAL;
461 /* Register gatt event handler */
462 _bt_service_register_event_handler_callback(BT_GATT_MODULE, __bt_gatt_event_handler);
464 __bt_service_reset_gatt_data();
466 stack_name = oal_get_stack_name();
468 if (stack_name && g_strcmp0(stack_name, "bluez") == 0) {
469 /*In the platform, defacult gatt client should be registered */
470 __bt_register_default_gatt_client();
474 return BLUETOOTH_ERROR_NONE;
477 static void __bt_service_reset_gatt_data(void)
481 BT_INFO("Rest numapp");
484 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
485 numapps[k].is_initialized = 0;
486 numapps[k].instance_id = -1;
487 numapps[k].adv_handle = 0;
488 numapps[k].adv_instance = -1;
489 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
490 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
491 memset(numapps[k].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
492 memset(numapps[k].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
493 numapps[k].adv_data_len = 0;
494 numapps[k].scan_rsp_len = 0;
497 numapps[k].client_id = -1;
498 memset(numapps[k].address.addr, 0x00, BLUETOOTH_ADDRESS_LENGTH);
499 numapps[k].is_watcher_enabled = FALSE;
503 void _bt_gatt_deinit(void)
505 BT_INFO("GATT deinit");
507 /* Un-register the default gatt client before */
508 __bt_unregister_gatt_client_instance(gatt_default_client);
510 if (OAL_STATUS_SUCCESS != gatt_disable())
511 BT_ERR("gatt deinit failed");
513 /* Un-register gatt event handler */
514 _bt_service_unregister_event_handler_callback(BT_GATT_MODULE);
516 __bt_service_reset_gatt_data();
519 void _bt_update_adv_handle(const char *sender, int adv_handle)
522 BT_DBG("Sender [%s] Adv handle [%d]", sender, adv_handle);
523 bt_service_app_info_t *info = NULL;
525 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
527 /* Do not update client instance */
528 if (info->instance_id == -1)
530 /* Search for a app which has same sender and adv handle as 0 */
531 if (!g_strcmp0(info->sender, sender) && info->adv_handle == 0)
532 info->adv_handle = adv_handle;
536 int _bt_is_sender_gatt_server_with_no_adv(const char *sender, int adv_handle)
539 BT_DBG("Sender [%s] Adv handle [%d]", sender, adv_handle);
540 bt_service_app_info_t *info = NULL;
542 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
544 /* Search for a app which has same sender and adv handle as 0
545 It is possible that same sender but different adv handle */
546 if (!g_strcmp0(info->sender, sender) && info->adv_handle == 0) {
547 //info->adv_handle = adv_handle;
548 return info->instance_id;
554 int _bt_get_allocated_server_instance(const char *sender, int adv_handle, gboolean use_reserved_slot)
557 BT_DBG("Sender [%s] Adv handle [%d]", sender, adv_handle);
558 bt_service_app_info_t *info = NULL;
560 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
563 /* Exact matching of Adv handle + sender combination */
564 if (!g_strcmp0(info->sender, sender) && info->adv_handle == adv_handle)
565 return info->instance_id;
567 if (!g_strcmp0(info->sender, sender) && info->adv_handle == -1)
568 return info->instance_id;
574 char * _bt_get_sender_and_handle(int server_instance, int *adv_handle)
577 bt_service_app_info_t *info = NULL;
579 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
582 if (info->instance_id == server_instance && info->adv_handle != -1) {
583 *adv_handle = info->adv_handle;
584 BT_DBG("Server instance [%d] Adv handle [%d] Sender [%s]", server_instance, *adv_handle, info->sender);
585 return g_strdup(info->sender);
591 void _bt_set_new_adv_data(bluetooth_advertising_data_t *adv, 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->adv_data.data, &adv->data, len);
606 void _bt_set_new_scan_rsp_data(bluetooth_scan_resp_data_t *scan, int len, int instance)
610 bt_service_app_info_t *info = NULL;
611 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
614 if (info->instance_id == instance) {
615 memcpy(info->scan_rsp.data, &scan->data, len);
621 void _bt_get_previous_adv_data(bluetooth_advertising_data_t *adv, int *len, int instance)
624 bt_service_app_info_t *info = NULL;
626 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
629 if (info->instance_id == instance) {
630 memcpy(&adv->data, info->adv_data.data, info->adv_data_len);
631 *len = info->adv_data_len;
637 void _bt_get_previous_scan_rsp_data(bluetooth_scan_resp_data_t *scan, int *len, int instance)
641 bt_service_app_info_t *info = NULL;
643 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
646 if (info->instance_id == instance) {
647 memcpy(&scan->data, info->scan_rsp.data, info->scan_rsp_len);
648 *len = info->scan_rsp_len;
654 static int __bt_unregister_gatt_client_instance(int client_if)
656 int ret = OAL_STATUS_SUCCESS;
659 BT_INFO("DeAllocate client instance ID [%d]", client_if);
661 /* Reset data: instance_id parameter could be either for GATT Server or for GATT client */
662 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
663 if (numapps[k].client_id == client_if) {
664 BT_INFO("This is a GATT client app, unregister: Slot [%d] vacant", k);
665 numapps[k].client_id = -1;
666 numapps[k].is_initialized = FALSE;
667 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
668 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
669 memset(&numapps[k].address.addr, 0x00, sizeof(bluetooth_device_address_t));
671 /* Its a GATT Client Instance */
672 ret = gattc_deregister(client_if);
673 if (ret != OAL_STATUS_SUCCESS) {
674 BT_ERR("DeAllocate GATT Client instance with stack Fail ret: %d", ret);
675 return BLUETOOTH_ERROR_INTERNAL;
680 return BLUETOOTH_ERROR_NONE;
683 static int __bt_unregister_gatt_server_instance(int server_instance)
685 int ret = OAL_STATUS_SUCCESS;
688 /* Unregister the server instance */
689 ret = gatts_unregister(server_instance);
690 if (ret != OAL_STATUS_SUCCESS) {
691 BT_ERR("DeAllocate server instance with stack Fail ret: %d", ret);
692 return BLUETOOTH_ERROR_INTERNAL;
694 BT_INFO("DeAllocated server instance with stack successful..");
697 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
698 if (numapps[k].instance_id == server_instance) {
699 numapps[k].is_initialized = 0;
700 numapps[k].instance_id = -1;
701 numapps[k].adv_handle = 0;
702 numapps[k].adv_instance = -1;
703 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
704 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
705 memset(numapps[k].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
706 memset(numapps[k].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
707 numapps[k].adv_data_len = 0;
708 numapps[k].scan_rsp_len = 0;
712 BT_DBG("Going8 to reset numapp block num [%d]", k);
713 return BLUETOOTH_ERROR_NONE;
716 static void __bt_remove_all_service_handles(bt_service_app_info_t *app_info)
720 int ret = OAL_STATUS_SUCCESS;
723 if (app_info == NULL)
726 count = g_slist_length(app_info->service_handles);
727 BT_INFO("Before handle removal: current count [%d]", count);
729 for (l = app_info->service_handles; l != NULL;) {
734 BT_INFO("Server Handle to be Removed [%d] Instance ID [%d]", *handle, app_info->instance_id);
735 if (_bt_gatt_server_stop_service(app_info->sender, *handle, app_info->instance_id) != BLUETOOTH_ERROR_NONE)
738 ret = gatts_delete_service(app_info->instance_id, *handle);
739 if (ret != OAL_STATUS_SUCCESS) {
740 BT_ERR("ret: %d", ret);
743 app_info->service_handles = g_slist_remove(app_info->service_handles, handle);
746 count = g_slist_length(app_info->service_handles);
747 BT_INFO("After deleting current count [%d]", count);
753 int _bt_unregister_server_instance(const char *sender, int adv_handle)
755 BT_INFO("Unregister Allocated server instance request Sender [%s] Adv handle [%d]", sender, adv_handle);
756 int result = BLUETOOTH_ERROR_NONE;
757 int apps[MAX_APPS_SUPPORTED];
761 memset(&apps, -1, sizeof(apps));
763 if (adv_handle == 0) {
764 BT_DBG("Its a direct GATT Server app request to unregister");
765 /* Unregister server instance for each app with same sender (case: GATT Server with multiple adv handle) */
766 bt_service_app_info_t *info = NULL;
768 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
771 /* Exact matching of sender */
772 if (!g_strcmp0(info->sender, sender)) {
773 BT_INFO("Unregister GATT server instance [%d]", info->instance_id);
774 /* Unregister all service handles with stack */
775 __bt_remove_all_service_handles(info);
777 /* Disable adv if running */
778 BT_INFO("Disable Advertising Adv Handle [%d] sender [%s]",
779 info->adv_handle, sender);
780 _bt_set_advertising(sender, info->adv_handle, FALSE, FALSE);
782 /* Save all instances which need to be unregistered */
783 apps[info->instance_id] = 1;
787 BT_DBG("Its an Internal unregister request by adv application");
788 server_instance = _bt_get_allocated_server_instance(sender, adv_handle, FALSE);
789 BT_DBG("Its an Internal unregister request by adv application: Adv disabled srv instance [%d]", server_instance);
790 if (server_instance == -1) {
791 BT_ERR("No allocated server instance to be removed");
792 return BLUETOOTH_ERROR_INVALID_PARAM;
794 if (!numapps[server_instance].service_handles) {
795 BT_INFO("There are no Service handles with this app, safe to unregister");
796 /* Unregister server instance only if this sender does not have any gatt services in it */
797 result = __bt_unregister_gatt_server_instance(server_instance);
799 numapps[server_instance].adv_handle = 0;
800 memset(numapps[server_instance].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
801 numapps[server_instance].adv_data_len = 0;
802 memset(numapps[server_instance].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
803 numapps[server_instance].scan_rsp_len = 0;
808 /* Unregister all apps one by one */
809 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
811 BT_INFO("Unregister app[%d]", k);
812 /* Unregister server instance */
813 __bt_unregister_gatt_server_instance(k);
820 int _bt_register_server_instance(const char *sender, int adv_handle)
822 int ret = OAL_STATUS_SUCCESS;
823 char *uuid_string = NULL;
828 BT_INFO("###Check on which instance Server instance can be initialized....");
829 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
830 if (numapps[k].is_initialized == 1) {
831 BT_INFO("Instance ID [%d] is already in use..Check next slot", numapps[k].instance_id);
834 BT_INFO("Time to register GATT Server..UUID to be used is [%s] slot [%d]", uuid_list[slot-1], slot);
840 BT_ERR("No Slot if free for GATT Server registration..");
841 return BLUETOOTH_ERROR_REGISTRATION_FAILED;
844 uuid_string = g_malloc0(BT_UUID_STRING_MAX);
845 _bt_string_to_uuid(uuid_list[slot-1], (service_uuid_t*)&uuid);
846 g_strlcpy(uuid_string, uuid_list[slot-1], BT_UUID_STRING_MAX);
847 BT_INFO("Copied UUID string [%s]", uuid_string);
849 /* Register GATT Server */
850 ret = gatts_register(&uuid);
851 if (ret != OAL_STATUS_SUCCESS) {
852 BT_ERR("ret: %d", ret);
854 return BLUETOOTH_ERROR_INTERNAL;
856 BT_INFO("GATT Server registration call successfully accepted by OAL..wait for Instance Initialized event from OAL..");
857 /* Return & wait for GATT Server Instance Initialization event */
858 memset(numapps[slot].sender, 0x00, sizeof(numapps[slot].sender));
859 memset(numapps[slot].uuid, 0x00, sizeof(numapps[slot].uuid));
861 g_strlcpy(numapps[slot].sender, sender, sizeof(numapps[slot].sender));
862 g_strlcpy(numapps[slot].uuid, uuid_string, sizeof(numapps[slot].uuid));
864 numapps[slot].is_initialized = 0; /* Set initialization from app registered callback */
865 numapps[slot].adv_handle = adv_handle;
868 return BLUETOOTH_ERROR_NONE;
872 static void __bt_gatt_handle_pending_request_info(int result,
873 int service_function, void *data, unsigned int size)
877 invocation_info_t *req_info = NULL;
878 ret_if(data == NULL);
880 for (l = _bt_get_invocation_list(); l != NULL; ) {
883 if (req_info == NULL || req_info->service_function != service_function)
886 switch (service_function) {
888 case BT_GATT_SERVER_REGISTER: {
889 bt_service_app_info_t *param = (bt_service_app_info_t*)data;
890 BT_DBG("Sender [%s] Request Sender [%s]", param->sender, req_info->sender);
891 if (!g_strcmp0(req_info->sender, param->sender)) {
892 BT_DBG("GATT Server app found [%s]", req_info->sender);
894 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
895 g_array_append_vals(out_param, ¶m->instance_id, sizeof(int));
896 _bt_service_method_return(req_info->context, out_param, result);
898 g_free(req_info->user_data);
899 _bt_free_info_from_invocation_list(req_info);
900 g_array_free(out_param, TRUE);
904 case BT_GATT_SERVER_START_SERVICE:
905 case BT_GATT_SERVER_DELETE_SERVICE: {
906 bt_service_app_info_t *param = (bt_service_app_info_t*)data;
907 BT_DBG("Sender [%s] Request Sender [%s]", param->sender, req_info->sender);
909 int *saved_instance_id = (int*)req_info->user_data;
910 if (!g_strcmp0(req_info->sender, param->sender) && param->instance_id == *saved_instance_id) {
911 BT_DBG("GATT Server app found [%s] Instance ID [%d] Reply DBUS",
912 req_info->sender, *saved_instance_id);
914 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
915 g_array_append_vals(out_param, &saved_instance_id, sizeof(int));
916 _bt_service_method_return(req_info->context, out_param, result);
918 g_free(req_info->user_data);
919 _bt_free_info_from_invocation_list(req_info);
920 g_array_free(out_param, TRUE);
924 case BT_GATT_SERVER_ADD_SERVICE:
925 case BT_GATT_SERVER_ADD_DESCRIPTOR:
926 case BT_GATT_SERVER_ADD_CHARACTERISTIC: {
927 int *handle = (int*)data;
928 BT_DBG("Characteristic added: Handle [%d]", *handle);
929 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
931 g_array_append_vals(out_param, handle, sizeof(int));
932 _bt_service_method_return(req_info->context, out_param, result);
934 g_free(req_info->user_data);
935 _bt_free_info_from_invocation_list(req_info);
936 g_array_free(out_param, TRUE);
941 case BT_DISCONNECT_LE: {
942 #ifdef TIZEN_GATT_CLIENT
943 char *addr = (char*)req_info->user_data;
944 bluetooth_device_address_t address;
946 if (!g_strcmp0(addr, (char*)data)) {
947 BT_INFO("GATT Client connect-disconnect call pending for app [%s] addr [%s]",
948 req_info->sender, addr);
949 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
950 _bt_convert_addr_string_to_type(address.addr, addr);
952 g_array_append_vals(out_param, (bluetooth_device_address_t*)&address,
953 sizeof(bluetooth_device_address_t));
954 _bt_service_method_return(req_info->context, out_param, result);
956 g_free(req_info->user_data);
957 _bt_free_info_from_invocation_list(req_info);
958 g_array_free(out_param, TRUE);
961 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
963 g_array_append_vals(out_param, (bluetooth_device_address_t*)data,
964 sizeof(bluetooth_device_address_t));
965 _bt_service_method_return(req_info->context, out_param, result);
967 g_free(req_info->user_data);
968 _bt_free_info_from_invocation_list(req_info);
970 g_array_free(out_param, TRUE);
974 #ifdef TIZEN_GATT_CLIENT
975 case BT_GATT_CLIENT_REGISTER: {
976 bt_service_app_info_t *param = (bt_service_app_info_t*)data;
977 BT_DBG("Sender [%s] Request Sender [%s]", param->sender, req_info->sender);
978 if (!g_strcmp0(req_info->sender, param->sender)) {
979 BT_DBG("GATT Client app found [%s] created client ID [%d]",
980 req_info->sender, param->client_id);
982 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
983 g_array_append_vals(out_param, ¶m->client_id, sizeof(int));
984 _bt_service_method_return(req_info->context, out_param, result);
986 g_free(req_info->user_data);
987 _bt_free_info_from_invocation_list(req_info);
988 g_array_free(out_param, TRUE);
992 case BT_GATT_GET_PRIMARY_SERVICES: {
993 bt_services_browse_info_t *param = (bt_services_browse_info_t*)data;
994 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
995 _bt_convert_addr_type_to_string(address,
996 (unsigned char *)(¶m->device_addr.addr));
998 BT_DBG("Request Sender [%s] address [%s]", req_info->sender, address);
1000 /* Match address to determine same request */
1001 if (!g_strcmp0((char*)req_info->user_data, address)) {
1002 BT_DBG("GATT Client app found [%s] Remote address [%s]",
1003 req_info->sender, address);
1005 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1006 g_array_append_vals(out_param, param,
1007 sizeof(bt_services_browse_info_t));
1009 //g_array_append_vals(out_param, ¶m->client_id, sizeof(int));
1010 _bt_service_method_return(req_info->context, out_param, result);
1012 g_free(req_info->user_data);
1013 _bt_free_info_from_invocation_list(req_info);
1014 g_array_free(out_param, TRUE);
1019 case BT_GATT_GET_SERVICE_PROPERTIES: {
1020 bt_char_browse_info_t param;
1021 memcpy((void*)¶m, data, sizeof(bt_char_browse_info_t));
1022 //bt_char_browse_info_t *param = (bt_char_browse_info_t*)data;
1023 BT_DBG("Request Sender [%s]", req_info->sender);
1024 bluetooth_gatt_client_svc_prop_info_t *prop = (bluetooth_gatt_client_svc_prop_info_t*)req_info->user_data;
1026 /* Match both address and service properties to determine same request */
1027 if (!memcmp(param.device_addr.addr,
1028 prop->device_address.addr,
1029 sizeof(bluetooth_device_address_t)) &&
1030 !memcmp(param.svc_uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1031 param.svc_inst_id == prop->svc.instance_id) {
1032 BT_DBG("Returning Service properties");
1034 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1035 g_array_append_vals(out_param, ¶m, sizeof(bt_char_browse_info_t));
1036 _bt_service_method_return(req_info->context, out_param, result);
1038 g_free(req_info->user_data);
1039 _bt_free_info_from_invocation_list(req_info);
1040 g_array_free(out_param, TRUE);
1044 case BT_GATT_GET_CHARACTERISTIC_PROPERTIES: {
1045 bt_descriptor_browse_info_t *param = (bt_descriptor_browse_info_t*)data;
1047 BT_DBG("Request Sender [%s]", req_info->sender);
1048 bluetooth_gatt_client_char_prop_info_t *prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
1050 /* Match both address, service properties &char properties to determine same request */
1051 if (!memcmp(param->device_addr.addr,
1052 prop->device_address.addr,
1053 sizeof(bluetooth_device_address_t)) &&
1054 !memcmp(param->svc_uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1055 param->svc_inst_id == prop->svc.instance_id &&
1056 !memcmp(param->char_uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1057 param->char_inst_id == prop->characteristic.instance_id) {
1058 BT_DBG("Returning Characteristic properties");
1059 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1060 g_array_append_vals(out_param, param, sizeof(bt_descriptor_browse_info_t));
1061 _bt_service_method_return(req_info->context, out_param, result);
1063 g_free(req_info->user_data);
1064 _bt_free_info_from_invocation_list(req_info);
1065 g_array_free(out_param, TRUE);
1069 case BT_GATT_WATCH_CHARACTERISTIC: {
1070 bt_gatt_notif_reg_info_t *param = (bt_gatt_notif_reg_info_t*)data;
1071 bluetooth_gatt_client_char_prop_info_t *prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
1072 BT_DBG("Request Sender [%s]", req_info->sender);
1073 /* Match both address, service properties &char properties to determine same request */
1074 if (!memcmp(param->addr.addr,
1075 prop->device_address.addr,
1076 sizeof(bluetooth_device_address_t)) &&
1077 !memcmp(param->svc_uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1078 param->svc_inst == prop->svc.instance_id &&
1079 !memcmp(param->char_uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1080 param->char_inst == prop->characteristic.instance_id) {
1081 BT_INFO("Characteristic Watch Successful: Is registered [%d]",
1082 param->is_registered);
1083 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1084 g_array_append_vals(out_param, param, sizeof(bt_gatt_notif_reg_info_t));
1085 _bt_service_method_return(req_info->context, out_param, result);
1086 g_free(req_info->user_data);
1087 _bt_free_info_from_invocation_list(req_info);
1088 g_array_free(out_param, TRUE);
1092 case BT_GATT_READ_CHARACTERISTIC:
1093 case BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE: {
1094 bluetooth_gatt_client_char_prop_info_t *param = (bluetooth_gatt_client_char_prop_info_t*)data;
1095 BT_DBG("Request Sender [%s]", req_info->sender);
1097 char *addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
1098 bluetooth_gatt_client_char_prop_info_t *prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
1099 _bt_convert_addr_type_to_string(addr, prop->device_address.addr);
1101 if (!memcmp(param, prop, sizeof(bluetooth_gatt_client_char_prop_info_t))) {
1102 BT_DBG("Gatt Char read or write request matched for address [%s]", addr);
1103 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1104 g_array_append_vals(out_param, param, sizeof(bluetooth_gatt_client_char_prop_info_t));
1105 _bt_service_method_return(req_info->context, out_param, result);
1107 g_free(req_info->user_data);
1108 _bt_free_info_from_invocation_list(req_info);
1109 g_array_free(out_param, TRUE);
1114 case BT_GATT_READ_DESCRIPTOR_VALUE:
1115 case BT_GATT_WRITE_DESCRIPTOR_VALUE: {
1116 bluetooth_gatt_client_desc_prop_info_t *param = (bluetooth_gatt_client_desc_prop_info_t*)data;
1117 BT_DBG("Request Sender [%s]", req_info->sender);
1119 char *addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
1120 bluetooth_gatt_client_desc_prop_info_t *prop = (bluetooth_gatt_client_desc_prop_info_t*)req_info->user_data;
1121 _bt_convert_addr_type_to_string(addr, prop->device_address.addr);
1123 if (!memcmp(param, prop, sizeof(bluetooth_gatt_client_desc_prop_info_t))) {
1124 BT_DBG("Descriptor read or write request matched for address [%s]", addr);
1125 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1126 g_array_append_vals(out_param, param, sizeof(bluetooth_gatt_client_desc_prop_info_t));
1127 _bt_service_method_return(req_info->context, out_param, result);
1129 g_free(req_info->user_data);
1130 _bt_free_info_from_invocation_list(req_info);
1131 g_array_free(out_param, TRUE);
1136 case BT_REQ_ATT_MTU: {
1137 char *addr = (char*)req_info->user_data;
1138 bluetooth_device_address_t address;
1140 if (!g_strcmp0(addr, (char*)data)) {
1141 BT_DBG("GATT Client BT_REQ_ATT_MTU call pending for app [%s] addr [%s]",
1142 req_info->sender, addr);
1143 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1144 _bt_convert_addr_string_to_type(address.addr, addr);
1146 g_array_append_vals(out_param, (bluetooth_device_address_t*)&address,
1147 sizeof(bluetooth_device_address_t));
1148 _bt_service_method_return(req_info->context, out_param, result);
1150 g_free(req_info->user_data);
1151 _bt_free_info_from_invocation_list(req_info);
1152 g_array_free(out_param, TRUE);
1162 static void __bt_handle_server_instance_registered(event_gatts_register_t *data)
1164 bt_service_app_info_t *info = NULL;
1166 char *uuid_string = g_malloc0(BT_UUID_STRING_MAX);
1168 _bt_uuid_to_string(&(data->server_uuid), uuid_string);
1169 BT_INFO("Instance ID is Intialized [%d] UUID initialized [%s]", data->server_inst, uuid_string);
1171 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1172 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1175 if (g_strcmp0(info->uuid, uuid_string) == 0) {
1176 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1177 info->is_initialized = TRUE;
1178 info->instance_id = data->server_inst;
1179 info->adv_instance = data->server_inst;
1180 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE, BT_GATT_SERVER_REGISTER,
1181 (void*)info, sizeof(bt_service_app_info_t));
1185 g_free(uuid_string);
1188 static void __bt_handle_gatt_server_service_added(event_gatts_srvc_prm_t *event)
1190 int result = BLUETOOTH_ERROR_NONE;
1194 bt_service_app_info_t *info = NULL;
1197 char *uuid_str = g_malloc0(BT_UUID_STRING_MAX);
1198 _bt_uuid_to_string(&(event->gatt_srvc_id.id.uuid), uuid_str);
1199 BT_INFO("GATT Added Service UUID: [%s] Server Ins [%d] Service hdl [%d] Is Primary [%d]\n",
1200 uuid_str, event->gatt_srvc_stat.server_inst, svc_handle, event->gatt_srvc_id.is_prmry);
1202 if (event->gatt_srvc_stat.status != OAL_STATUS_SUCCESS) {
1203 result = BLUETOOTH_ERROR_INTERNAL;
1204 svc_handle = 0; /* Service handle set to 0 indicates.
1205 0 is reserved by BT SIG, cant be used by app*/
1207 svc_handle = event->gatt_srvc_stat.servic_hndl;
1209 BT_INFO("GATT Added Service Status [%d] Server Ins [%d] Service hdl [%d] Is Primary [%d]\n",
1210 event->gatt_srvc_stat.status, event->gatt_srvc_stat.server_inst, svc_handle, event->gatt_srvc_id.is_prmry);
1212 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1213 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1216 if (info->instance_id == event->gatt_srvc_stat.server_inst) {
1217 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1218 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_ADD_SERVICE,
1219 (int*)&svc_handle, sizeof(int));
1221 /* Add Service Handle */
1222 if (svc_handle > 0) {
1223 handle = g_malloc0(sizeof(int));
1224 *handle = svc_handle;
1225 numapps[event->gatt_srvc_stat.server_inst].service_handles = g_slist_append(numapps[event->gatt_srvc_stat.server_inst].service_handles, handle);
1226 count = g_slist_length(numapps[event->gatt_srvc_stat.server_inst].service_handles);
1227 BT_INFO("Added Service handle [%d] to list..current count [%d]", svc_handle, count);
1236 static void __bt_handle_gatt_server_characteristic_added(event_gatts_srvc_charctr_t *event)
1238 int result = BLUETOOTH_ERROR_NONE;
1240 bt_service_app_info_t *info = NULL;
1242 char *uuid_str = g_malloc0(BT_UUID_STRING_MAX);
1244 BT_INFO("GATT Server Char added status [%d]", event->gatt_srvc_stat.status);
1245 BT_INFO("GATT Server_inst: [%d]", event->gatt_srvc_stat.server_inst);
1246 BT_INFO("GATT Service Handle: [%d]", event->gatt_srvc_stat.servic_hndl);
1247 BT_INFO("GATT Add characteristic Status: [%d]", event->gatt_srvc_stat.status);
1248 BT_INFO("GATT Service characteristic Handle: [%d]", event->charctr_hndl);
1250 if (event->gatt_srvc_stat.status != OAL_STATUS_SUCCESS) {
1251 result = BLUETOOTH_ERROR_INTERNAL;
1252 char_handle = 0; /* characteristic handle set to 0 indicates.
1253 0 is reserved by BT SIG, cant be used by app*/
1255 char_handle = event->charctr_hndl;
1258 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1259 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1262 if (info->instance_id == event->gatt_srvc_stat.server_inst) {
1263 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1264 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_ADD_CHARACTERISTIC,
1265 (int*)&char_handle, sizeof(int));
1271 _bt_uuid_to_string(&(event->charctr_uuid), uuid_str);
1272 BT_INFO("GATT Added Characteristic: UUID: [%s]", uuid_str);
1277 static void __bt_handle_gatt_server_descriptor_added(event_gatts_srvc_descr_t* event)
1279 int result = BLUETOOTH_ERROR_NONE;
1281 bt_service_app_info_t *info = NULL;
1283 char *uuid_str = g_malloc0(BT_UUID_STRING_MAX);
1285 BT_INFO("GATT Server Descriptor added status [%d]", event->gatt_srvc_stat.status);
1286 BT_INFO("GATT Server_inst: [%d]", event->gatt_srvc_stat.server_inst);
1287 BT_INFO("GATT Service Handle: [%d]", event->gatt_srvc_stat.servic_hndl);
1288 BT_INFO("GATT Add Descriptor Status: [%d]", event->gatt_srvc_stat.status);
1289 BT_INFO("GATT Service Descriptor Handle: [%d]", event->descrptr_hndl);
1291 if (event->gatt_srvc_stat.status != OAL_STATUS_SUCCESS) {
1292 result = BLUETOOTH_ERROR_INTERNAL;
1293 desc_handle = 0; /* Service handle set to 0 indicates.
1294 0 is reserved by BT SIG, cant be used by app*/
1296 desc_handle = event->descrptr_hndl;
1298 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1299 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1302 if (info->instance_id == event->gatt_srvc_stat.server_inst) {
1303 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1304 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_ADD_DESCRIPTOR,
1305 (int*)&desc_handle, sizeof(int));
1310 _bt_uuid_to_string(&(event->descrptr_uuid), uuid_str);
1311 BT_INFO("GATT Added Descriptor: UUID: [%s]", uuid_str);
1316 static void __bt_handle_gatt_server_service_started(event_gatts_srvc_t *event)
1318 bt_service_app_info_t *info = NULL;
1319 int result = BLUETOOTH_ERROR_NONE;
1321 BT_INFO("GATT Server Service Started..");
1324 BT_INFO("GATT Server_inst: [%d]", event->server_inst);
1325 BT_INFO("GATT Service Handle: [%d]", event->servic_hndl);
1326 BT_INFO("GATT Service Started Status: [%d]", event->status);
1328 if (event->status != OAL_STATUS_SUCCESS) {
1329 BT_ERR("GATT Server Service Start Failed Err: [%d]", event->status);
1330 result = BLUETOOTH_ERROR_INTERNAL;
1333 /* Check if the just registered Instance ID belongs to requester */
1334 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1337 if (info->instance_id == event->server_inst) {
1338 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1339 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_START_SERVICE,
1340 (void*)info, sizeof(bt_service_app_info_t));
1346 static void __bt_handle_gatt_server_service_stopped(event_gatts_srvc_t *event)
1348 int result = BLUETOOTH_ERROR_NONE;
1349 bt_service_app_info_t *info = NULL;
1351 BT_INFO("GATT Server Service Stopped..");
1353 BT_INFO("GATT Server_inst: [%d]", event->server_inst);
1354 BT_INFO("GATT Service Handle: [%d]", event->servic_hndl);
1355 BT_INFO("GATT Service Stopped Status: [%d]", event->status);
1357 if (event->status != OAL_STATUS_SUCCESS) {
1358 BT_ERR("GATT Server Service Stop Failed Err: [%d]", event->status);
1359 result = BLUETOOTH_ERROR_INTERNAL;
1362 /* Check if the just registered Instance ID belongs to requester */
1363 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1366 if (info->instance_id == event->server_inst) {
1367 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1368 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_STOP_SERVICE,
1369 (void*)info, sizeof(bt_service_app_info_t));
1375 static void __bt_handle_gatt_server_service_deleted(event_gatts_srvc_t *event)
1377 int result = BLUETOOTH_ERROR_NONE;
1378 bt_service_app_info_t *info = NULL;
1380 BT_INFO("GATT Server Service Deleted..");
1382 BT_INFO("GATT Server_inst: [%d]", event->server_inst);
1383 BT_INFO("GATT Service Handle: [%d]", event->servic_hndl);
1384 BT_INFO("GATT Service Deleted Status: [%d]", event->status);
1386 if (event->status != OAL_STATUS_SUCCESS) {
1387 BT_ERR("GATT Server Service Delete Failed Err: [%d]", event->status);
1388 result = BLUETOOTH_ERROR_INTERNAL;
1391 /* Check if the just registered Instance ID belongs to requester */
1392 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1395 if (info->instance_id == event->server_inst) {
1396 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1397 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_DELETE_SERVICE,
1398 (void*)info, sizeof(bt_service_app_info_t));
1404 struct gatt_client_info_t *_bt_find_remote_gatt_client_info(char *address)
1407 struct gatt_client_info_t *info = NULL;
1408 for (l = gatt_client_info_list; l != NULL; l = g_slist_next(l)) {
1409 info = (struct gatt_client_info_t*)l->data;
1413 if (!g_strcmp0(info->addr, address)) {
1414 BT_DBG("Remote GATT client found addr[%s]", info->addr);
1421 static struct gatt_client_info_t *__bt_find_remote_gatt_client_info_from_conn_id(int conn_id)
1424 struct gatt_client_info_t *info = NULL;
1426 for (l = gatt_client_info_list; l != NULL; l = g_slist_next(l)) {
1427 info = (struct gatt_client_info_t*)l->data;
1431 if (info->connection_id == conn_id) {
1432 BT_INFO("Remote GATT client found addr[%s]", info->addr);
1439 #ifdef TIZEN_GATT_CLIENT
1440 struct gatt_server_info_t *_bt_find_remote_gatt_server_info(char *address)
1443 struct gatt_server_info_t *info = NULL;
1444 for (l = gatt_server_info_list; l != NULL; l = g_slist_next(l)) {
1445 info = (struct gatt_server_info_t*)l->data;
1449 if (!g_strcmp0(info->addr, address)) {
1450 BT_DBG("Remote GATT Server found addr[%s]", info->addr);
1457 static struct gatt_out_conn_info_t* __bt_find_gatt_outgoing_conn_info(char *address)
1460 struct gatt_out_conn_info_t *info = NULL;
1461 for (l = outgoing_gatt_conn_list; l != NULL; l = g_slist_next(l)) {
1462 info = (struct gatt_out_conn_info_t*)l->data;
1466 if (!g_strcmp0(info->addr, address)) {
1467 BT_INFO("Outgoing connection info found addr[%s]", info->addr);
1474 static struct gatt_server_info_t *__bt_find_remote_gatt_server_info_from_client_if(int client_if)
1477 struct gatt_server_info_t *info = NULL;
1479 for (l = gatt_server_info_list; l != NULL; l = g_slist_next(l)) {
1480 info = (struct gatt_server_info_t*)l->data;
1484 if (info->client_id == client_if) {
1485 BT_INFO("Remote GATT server found addr[%s]", info->addr);
1496 #ifdef TIZEN_GATT_CLIENT
1497 static void __bt_handle_gatt_server_connection_state(event_gatts_conn_t *event)
1499 int result = BLUETOOTH_ERROR_NONE;
1500 struct gatt_client_info_t *client_info = NULL;
1501 struct gatt_server_info_t *server_info = NULL;
1502 bluetooth_device_address_t dev_addr;
1503 GVariant *param = NULL;
1506 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1508 memcpy(dev_addr.addr, event->address.addr, 6);
1510 /* REPLY dbus Context to application which called BT_CONNECT_LE. There is status
1512 _bt_convert_addr_type_to_string(address,
1513 (unsigned char *)dev_addr.addr);
1515 if (event->status != OAL_STATUS_SUCCESS)
1516 result = BLUETOOTH_ERROR_INTERNAL;
1518 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
1519 address, BT_ADDRESS_STRING_SIZE);
1521 BT_INFO("GATT Server Connedted: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1522 address, event->server_inst, event->conn_id);
1525 /* Check if device is already in connected list */
1526 client_info = _bt_find_remote_gatt_client_info(address);
1529 BT_INFO("Conn Info absent: But no need to Send Local GATT Server Connected event to apps");
1531 param = g_variant_new("(is)", result, address);
1533 /* Send event to application */
1534 _bt_send_event(BT_DEVICE_EVENT,
1535 BLUETOOTH_EVENT_GATT_SERVER_CONNECTED, /* Local device is GATT server */
1538 /* Save client connection info */
1539 client_info = g_new0(struct gatt_client_info_t, 1);
1540 client_info->addr = g_strdup(address);
1541 BT_INFO("Added GATT client addr[%s]", client_info->addr);
1542 client_info->connection_id = event->conn_id;
1543 client_info->instance_id = event->server_inst;
1544 gatt_client_info_list = g_slist_append(gatt_client_info_list, client_info);
1545 BT_INFO("Total num of connected Remote GATT Clients [%d]", g_slist_length(gatt_client_info_list));
1547 /* Save server connection info */
1548 server_info = g_new0(struct gatt_server_info_t, 1);
1549 server_info->addr = g_strdup(address);
1550 server_info->client_id = -1;
1551 BT_INFO("Added GATT server addr[%s]", server_info->addr);
1552 server_info->connection_id = event->conn_id;
1553 gatt_server_info_list = g_slist_append(gatt_server_info_list, server_info);
1554 BT_INFO("Total num of connected Remote GATT Servers [%d]", g_slist_length(gatt_server_info_list));
1556 ret = gattc_add_connection_info((bt_address_t *)&dev_addr, event->conn_id, event->server_inst);
1557 if (ret != OAL_STATUS_SUCCESS) {
1558 BT_ERR("gattc register server instance failed");
1562 __bt_add_mtu_gatt_device(address);
1567 /* GATT Server Dis connected */
1568 static void __bt_handle_gatt_server_disconnection_state(event_gatts_conn_t *event)
1570 int result = BLUETOOTH_ERROR_NONE;
1571 struct gatt_client_info_t *client_info = NULL;
1572 struct gatt_server_info_t *server_info = NULL;
1573 bluetooth_device_address_t dev_addr;
1574 GVariant *param = NULL;
1575 char address[BT_ADDRESS_STRING_SIZE];
1577 memcpy(dev_addr.addr, event->address.addr, 6);
1579 /* REPLY dbus Context to application which called BT_DISCONNECT_LE. There is status
1581 _bt_convert_addr_type_to_string(address,
1582 (unsigned char *)dev_addr.addr);
1584 if (event->status != OAL_STATUS_SUCCESS)
1585 result = BLUETOOTH_ERROR_INTERNAL;
1587 if (NULL == _bt_get_request_info_data(BT_DISCONNECT_LE, address)) {
1588 if (NULL != _bt_get_request_info_data(BT_CONNECT_LE, address)) {
1589 result = BLUETOOTH_ERROR_INTERNAL;
1591 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
1592 address, BT_ADDRESS_STRING_SIZE);
1594 BT_ERR("Failed to connect Local GATT Server Remote Client addr[%s]", address);
1598 __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE,
1599 address, BT_ADDRESS_STRING_SIZE);
1602 BT_INFO("Local GATT Server DisConnected: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1603 address, event->server_inst, event->conn_id);
1604 /* Remove Connection info */
1605 client_info = _bt_find_remote_gatt_client_info(address);
1607 BT_INFO("No need to Send Local GATT Server Disconnected event to apps, just remove remote client info");
1609 param = g_variant_new("(is)", result, address);
1610 /* Send event to application */
1611 _bt_send_event(BT_DEVICE_EVENT,
1612 BLUETOOTH_EVENT_GATT_SERVER_DISCONNECTED, /* Local device is GATT server */
1615 /* Remove server info from list */
1616 server_info = _bt_find_remote_gatt_server_info(address);
1618 gatt_server_info_list = g_slist_remove(gatt_server_info_list, server_info);
1620 BT_INFO("Can not find conn info, already removed!");
1622 /* Remove client info from List */
1623 gatt_client_info_list = g_slist_remove(gatt_client_info_list, client_info);
1624 BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
1625 g_free(client_info->addr);
1626 g_free(client_info);
1629 __bt_remove_mtu_gatt_device(address);
1633 static void __bt_handle_gatt_server_connection_state(event_gatts_conn_t *event)
1635 int result = BLUETOOTH_ERROR_NONE;
1636 struct gatt_client_info_t *conn_info = NULL;
1637 bt_service_app_info_t *info = NULL;
1638 bluetooth_device_address_t dev_addr;
1639 GVariant *param = NULL;
1642 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1644 memcpy(dev_addr.addr, event->address.addr, 6);
1647 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1648 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1651 if (info->instance_id == event->server_inst) {
1652 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1653 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
1654 (bluetooth_device_address_t*)&dev_addr, sizeof(bluetooth_device_address_t));
1660 /* Send event to BT-API */
1661 _bt_convert_addr_type_to_string(address,
1662 (unsigned char *)dev_addr.addr);
1664 BT_INFO("GATT Server Connedted: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1665 address, event->server_inst, event->conn_id);
1668 /* Check if device is already in connected list */
1669 conn_info = _bt_find_remote_gatt_client_info(address);
1672 param = g_variant_new("(is)", result, address);
1673 /* Send event to application */
1674 _bt_send_event(BT_GATT_SERVER_EVENT,
1675 BLUETOOTH_EVENT_GATT_CONNECTED,
1678 /* Save Connection info */
1679 conn_info = g_new0(struct gatt_client_info_t, 1);
1680 conn_info->addr = g_strdup(address);
1681 BT_INFO("Added GATT client addr[%s]", conn_info->addr);
1682 conn_info->connection_id = event->conn_id;
1683 conn_info->instance_id = event->server_inst;
1684 gatt_client_info_list = g_slist_append(gatt_client_info_list, conn_info);
1685 BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
1691 /* GATT Server Dis connected */
1692 static void __bt_handle_gatt_server_disconnection_state(event_gatts_conn_t *event)
1694 int result = BLUETOOTH_ERROR_NONE;
1695 struct gatt_client_info_t *conn_info = NULL;
1696 bluetooth_device_address_t dev_addr;
1697 bt_service_app_info_t *info = NULL;
1698 GVariant *param = NULL;
1701 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1703 memcpy(dev_addr.addr, event->address.addr, 6);
1705 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1706 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1709 if (info->instance_id == event->server_inst) {
1710 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1711 __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE,
1712 (bluetooth_device_address_t*)&dev_addr, sizeof(bluetooth_device_address_t));
1718 /* Send event to BT-API */
1719 _bt_convert_addr_type_to_string(address,
1720 (unsigned char *)dev_addr.addr);
1722 BT_INFO("GATT Server DisConnected: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1723 address, event->server_inst, event->conn_id);
1725 param = g_variant_new("(is)", result, address);
1726 /* Send event to application */
1727 _bt_send_event(BT_GATT_SERVER_EVENT,
1728 BLUETOOTH_EVENT_GATT_DISCONNECTED,
1731 /* Remove Connection info */
1732 conn_info = _bt_find_remote_gatt_client_info(address);
1734 BT_INFO("Remove GATT client info from List..");
1735 /* Remove info from List */
1736 gatt_client_info_list = g_slist_remove(gatt_client_info_list, conn_info);
1737 BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
1738 g_free(conn_info->addr);
1747 static void __bt_handle_gatt_server_acquire_write_requested(event_gatts_srvc_acquire_attr_t *event)
1749 GVariant *param = NULL;
1750 int result = BLUETOOTH_ERROR_NONE;
1751 struct gatt_server_req_info *req_info = NULL;
1752 bluetooth_device_address_t dev_addr;
1753 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1755 BT_INFO("GATT Server ACQUIRE Write Req Connection ID: [%d]", event->attr_trans.conn_id);
1756 BT_INFO("GATT Server ACQUIRE Write Req Transaction ID:[%d]", event->attr_trans.trans_id);
1757 BT_INFO("GATT Server ACQUIRE Write Req Attribute Handle: [%d]", event->attr_trans.attr_handle);
1759 //address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1760 memcpy(dev_addr.addr, event->address.addr, 6);
1762 req_info = g_new0(struct gatt_server_req_info, 1);
1763 req_info->request_id = event->attr_trans.trans_id;
1764 req_info->attribute_handle = event->attr_trans.attr_handle;
1765 req_info->connection_id = event->attr_trans.conn_id;
1766 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_ACQUIRE_WRITE;
1767 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1769 _bt_convert_addr_type_to_string(address,
1770 (unsigned char *)dev_addr.addr);
1772 param = g_variant_new("(iiiiiis)", result,
1773 event->attr_trans.conn_id,
1774 event->attr_trans.trans_id,
1775 event->attr_trans.attr_handle,
1776 event->mtu, event->attr_trans.offset, address);
1777 BT_INFO("GATT Server ACQUIRE Write Req Attribute remote address : [%s]", address);
1779 _bt_send_event(BT_GATT_SERVER_EVENT,
1780 BLUETOOTH_EVENT_GATT_SERVER_ACQUIRE_WRITE,
1785 static void __bt_handle_gatt_server_acquire_notify_requested(event_gatts_srvc_acquire_attr_t *event)
1787 GVariant *param = NULL;
1788 int result = BLUETOOTH_ERROR_NONE;
1789 struct gatt_server_req_info *req_info = NULL;
1790 bluetooth_device_address_t dev_addr;
1791 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1793 BT_INFO("GATT Server ACQUIRE Notify Req Connection ID: [%d]", event->attr_trans.conn_id);
1794 BT_INFO("GATT Server ACQUIRE Notify Req Transaction ID:[%d]", event->attr_trans.trans_id);
1795 BT_INFO("GATT Server ACQUIRE Notify Req Attribute Handle: [%d]", event->attr_trans.attr_handle);
1796 BT_INFO("GATT Server ACQUIRE notify Req address : [%s]", address);
1798 memcpy(dev_addr.addr, event->address.addr, 6);
1799 _bt_convert_addr_type_to_string(address,
1800 (unsigned char *)dev_addr.addr);
1801 BT_INFO("GATT Server ACQUIRE notify Req remote address : [%s]", address);
1803 req_info = g_new0(struct gatt_server_req_info, 1);
1804 req_info->request_id = event->attr_trans.trans_id;
1805 req_info->attribute_handle = event->attr_trans.attr_handle;
1806 req_info->connection_id = event->attr_trans.conn_id;
1807 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_ACQUIRE_NOTIFY;
1808 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1810 param = g_variant_new("(iiiiiis)", result,
1811 event->attr_trans.conn_id,
1812 event->attr_trans.trans_id,
1813 event->attr_trans.attr_handle,
1814 event->mtu, event->attr_trans.offset,
1817 BT_INFO("GATT Server ACQUIRE Notify Req Attribute : ");
1819 _bt_send_event(BT_GATT_SERVER_EVENT,
1820 BLUETOOTH_EVENT_GATT_SERVER_ACQUIRE_NOTIFY,
1824 static void __bt_handle_gatt_server_write_requested(event_gatts_srvc_write_attr_t *event)
1827 bluetooth_device_address_t dev_addr;
1828 GVariant *param = NULL;
1829 int result = BLUETOOTH_ERROR_NONE;
1831 gboolean is_prepare_write;
1833 GVariant *data = NULL;
1835 struct gatt_server_req_info *req_info = NULL;
1836 BT_INFO("GATT Server Write Requested");
1838 memcpy(dev_addr.addr, event->address.addr, 6);
1840 BT_INFO("GATT Server Write Req Connection ID: [%d]", event->attr_trans.conn_id);
1841 BT_INFO("GATT Server Write Req Transaction ID:[%d]", event->attr_trans.trans_id);
1842 BT_INFO("GATT Server Write Req Attribute Handle: [%d]", event->attr_trans.attr_handle);
1843 BT_INFO("GATT Server Write Req Attribute Offset: [%d]", event->attr_trans.offset);
1844 BT_INFO("GATT Server Write Req value length [%d]", event->length);
1845 BT_INFO("GATT Server Write Req needs response: [%d]", event->need_rsp);
1846 BT_INFO("GATT Server Write Req Is Prep: [%d]", event->is_prep);
1848 if (event->length <= 0) {
1849 BT_INFO("GATT Server write requested, but length of attr value is 0");
1853 need_resp = event->need_rsp;
1854 is_prepare_write = event->is_prep;
1856 write_val = g_memdup(&event->value[0], event->length);
1858 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1859 _bt_convert_addr_type_to_string(address,
1860 (unsigned char *)dev_addr.addr);
1862 BT_INFO("GATT Server Write Request from remote client [%s]", address);
1864 if (event->length > 0) {
1865 for (i = 0; i < event->length; i++)
1866 BT_DBG("Data[%d] = [0x%x]", i, event->value[i]);
1868 /* Save Write Request Info */
1869 req_info = g_new0(struct gatt_server_req_info, 1);
1870 req_info->request_id = event->attr_trans.trans_id;
1871 req_info->attribute_handle = event->attr_trans.attr_handle;
1872 req_info->connection_id = event->attr_trans.conn_id;
1873 req_info->addr = address;
1874 req_info->offset = event->attr_trans.offset;
1875 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_WRITE;
1876 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1878 data = g_variant_new_from_data(
1879 G_VARIANT_TYPE_BYTESTRING,
1884 param = g_variant_new("(iiiiiibbsn@ay)", result,
1885 event->attr_trans.conn_id,
1886 event->attr_trans.trans_id,
1887 event->attr_trans.attr_handle,
1888 event->attr_trans.offset,
1896 _bt_send_event(BT_GATT_SERVER_EVENT,
1897 BLUETOOTH_EVENT_GATT_SERVER_VALUE_CHANGED,
1903 static void __bt_handle_gatt_server_read_requested(event_gatts_srvc_read_attr_t *event)
1905 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1906 bluetooth_device_address_t dev_addr;
1907 int result = BLUETOOTH_ERROR_NONE;
1908 struct gatt_server_req_info *req_info = NULL;
1909 GVariant *param = NULL;
1911 BT_INFO("GATT Server Read Requested");
1913 memcpy(dev_addr.addr, event->address.addr, 6);
1915 BT_INFO("GATT Server-Client Connection ID: [%d]", event->attr_trans.conn_id);
1916 BT_INFO("GATT Server-Client Transaction ID: [%d]", event->attr_trans.trans_id);
1917 BT_INFO("GATT Server Attribute Handle: [%d]", event->attr_trans.attr_handle);
1918 BT_INFO("GATT Server Attribute Offset for read: [%d]", event->attr_trans.offset);
1919 BT_INFO("GATT Server Attribute is long: [%d]", event->is_long);
1921 is_long = event->is_long;
1923 /* Save Read Request Info */
1924 req_info = g_new0(struct gatt_server_req_info, 1);
1925 req_info->request_id = event->attr_trans.trans_id;
1926 req_info->attribute_handle = event->attr_trans.attr_handle;
1927 req_info->connection_id = event->attr_trans.conn_id;
1928 req_info->addr = address;
1929 req_info->offset = event->attr_trans.offset;
1930 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_READ;
1931 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1933 /* Send event to BT-API */
1934 _bt_convert_addr_type_to_string(address,
1935 (unsigned char *)dev_addr.addr);
1937 BT_INFO("GATT Server Read Request from remote client [%s]", address);
1939 param = g_variant_new("(iiiiibs)", result,
1940 event->attr_trans.conn_id,
1941 event->attr_trans.trans_id,
1942 event->attr_trans.attr_handle,
1943 event->attr_trans.offset,
1947 _bt_send_event(BT_GATT_SERVER_EVENT,
1948 BLUETOOTH_EVENT_GATT_SERVER_READ_REQUESTED,
1952 static void __bt_handle_gatt_server_indicate_confirmed(event_gatts_ind_cnfrm_t *event)
1954 bluetooth_device_address_t dev_addr;
1955 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1956 int cur_connected_clients;
1957 static int recvd = 0;
1958 gboolean completed = 0;
1959 GVariant *param = NULL;
1961 /* OAL event does provide error, so MW assumes event will never contain wrong data,
1962 incase of any issues, check with OAL */
1963 int result = BLUETOOTH_ERROR_NONE;
1965 memcpy(dev_addr.addr, event->address.addr, 6);
1966 _bt_convert_addr_type_to_string(address,
1967 (unsigned char *)dev_addr.addr);
1969 BT_INFO("Indication sent to GATT client [%s] conn_ ID [%d] transaction ID [%d] Att handle [%d]",
1970 address, event->conn_id, event->trans_id, event->attr_handle);
1973 cur_connected_clients = g_slist_length(gatt_client_info_list);
1974 BT_INFO("Number of connected clients during sending Indication [%d] & current connected count [%d]",
1975 num_indicate_clients, cur_connected_clients);
1978 if (recvd == num_indicate_clients) {
1979 BT_INFO("Gatt indication confirm event for last GATT client.. [%s]", address);
1980 completed = 1; /* Last event */
1981 recvd = 0; /* Reset */
1982 num_indicate_clients = 0;
1985 param = g_variant_new("(isib)",
1991 /* Send event to BT-API */
1992 _bt_send_event(BT_GATT_SERVER_EVENT,
1993 BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_COMPLETED,
1996 BT_INFO("Received Indication confirm for client number [%d]", recvd);
2000 /* Tizen Platform Specific */
2001 static void __bt_handle_gatt_server_notification_changed(event_gatts_notif_t *event)
2003 bluetooth_device_address_t dev_addr;
2004 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2005 GVariant *param = NULL;
2008 /* OAL event does provide error, so MW assumes event will never contain wrong data,
2009 incase of any issues, check with OAL */
2010 int result = BLUETOOTH_ERROR_NONE;
2012 memcpy(dev_addr.addr, event->address.addr, 6);
2013 _bt_convert_addr_type_to_string(address,
2014 (unsigned char *)dev_addr.addr);
2016 BT_INFO("notification_changed [%s] conn_ ID [%d] transaction ID [%d] Att handle [%d] Notify[%d]",
2017 address, event->conn_id, event->trans_id, event->attr_handle, event->notify);
2019 /* Set Notifcation status */
2020 notify = event->notify;
2022 param = g_variant_new("(isib)",
2028 /* Send event to BT-API */
2029 _bt_send_event(BT_GATT_SERVER_EVENT,
2030 BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_STATE_CHANGED,
2036 static void __bt_handle_gatt_mtu_changed_event(event_gatts_mtu_changed_t *event)
2038 int result = BLUETOOTH_ERROR_NONE;
2039 struct gatt_client_info_t *conn_info = NULL;
2040 GVariant *param = NULL;
2043 conn_info = __bt_find_remote_gatt_client_info_from_conn_id(event->conn_id);
2044 if (conn_info == NULL) {
2045 BT_ERR("Cant find connection Information");
2048 BT_INFO("Got connection Info GATT client [%s] MTU Size [%d]",
2049 conn_info->addr, event->mtu_size);
2051 __bt_update_mtu_gatt_device(conn_info->addr, event->mtu_size);
2053 param = g_variant_new("(isqy)",
2059 /* Send event to BT-API */
2060 _bt_send_event(BT_GATT_SERVER_EVENT,
2061 BLUETOOTH_EVENT_GATT_ATT_MTU_CHANGED,
2065 static void __bt_gatt_event_handler(int event_type, gpointer event_data)
2067 BT_INFO("OAL event = 0x%x, \n", event_type);
2068 switch (event_type) {
2069 case OAL_EVENT_BLE_SERVER_INSTANCE_INITIALISED: {
2070 BT_INFO("OAL Event: Server Instance Registered");
2071 /* GATT Server Registered event is handled in MAIN thread context */
2072 __bt_handle_server_instance_registered((event_gatts_register_t *)event_data);
2075 case OAL_EVENT_GATTS_SERVICE_ADDED: {
2076 BT_INFO("OAL Event: GATT Service added");
2077 __bt_handle_gatt_server_service_added((event_gatts_srvc_prm_t *)event_data);
2080 case OAL_EVENT_GATTS_CHARACTERISTIC_ADDED: {
2081 BT_INFO("OAL Event: GATT characteristic added");
2082 __bt_handle_gatt_server_characteristic_added((event_gatts_srvc_charctr_t *)event_data);
2085 case OAL_EVENT_GATTS_DESCRIPTOR_ADDED: {
2086 BT_INFO("OAL Event: GATT descriptor added");
2087 __bt_handle_gatt_server_descriptor_added((event_gatts_srvc_descr_t *)event_data);
2090 case OAL_EVENT_GATTS_SERVICE_STARTED: {
2091 BT_INFO("OAL Event: GATT Service started");
2092 __bt_handle_gatt_server_service_started((event_gatts_srvc_t *)event_data);
2095 case OAL_EVENT_GATTS_SERVICE_STOPED: {
2096 BT_INFO("OAL Event: GATT Service stopped");
2097 __bt_handle_gatt_server_service_stopped((event_gatts_srvc_t *)event_data);
2100 case OAL_EVENT_GATTS_SERVICE_DELETED: {
2101 BT_INFO("OAL Event: GATT Service deleted");
2102 __bt_handle_gatt_server_service_deleted((event_gatts_srvc_t *) event_data);
2105 case OAL_EVENT_GATTS_CONNECTION_COMPLETED: {
2106 BT_INFO("OAL Event: GATT Server Connected");
2107 __bt_handle_gatt_server_connection_state((event_gatts_conn_t *)event_data);
2110 case OAL_EVENT_GATTS_DISCONNECTION_COMPLETED: {
2111 BT_INFO("OAL Event: GATT Server Disconnected");
2112 __bt_handle_gatt_server_disconnection_state((event_gatts_conn_t *)event_data);
2115 case OAL_EVENT_GATTS_REQUEST_READ: {
2116 BT_INFO("OAL Event: GATT Server Read Request");
2117 __bt_handle_gatt_server_read_requested((event_gatts_srvc_read_attr_t *)event_data);
2120 case OAL_EVENT_GATTS_REQUEST_WRITE: {
2121 BT_INFO("OAL Event: GATT Server Write Request");
2122 __bt_handle_gatt_server_write_requested((event_gatts_srvc_write_attr_t *)event_data);
2125 case OAL_EVENT_GATTS_REQUEST_ACQUIRE_WRITE: {
2126 BT_INFO("OAL Event: GATT Server Acquire Write Request");
2127 __bt_handle_gatt_server_acquire_write_requested((event_gatts_srvc_acquire_attr_t*)event_data);
2130 case OAL_EVENT_GATTS_REQUEST_ACQUIRE_NOTIFY: {
2131 BT_INFO("OAL Event: GATT ServerAcquire Notify Request");
2132 __bt_handle_gatt_server_acquire_notify_requested((event_gatts_srvc_acquire_attr_t*)event_data);
2135 case OAL_EVENT_GATTS_IND_CONFIRM: {
2136 BT_INFO("OAL Event: GATT Server Indication confirmed");
2137 __bt_handle_gatt_server_indicate_confirmed((event_gatts_ind_cnfrm_t *)event_data);
2140 case OAL_EVENT_GATTS_NOTIFICATION: { /* Tizen Platform Specific */
2141 BT_INFO("OAL Event: GATT Server DisConnected");
2142 __bt_handle_gatt_server_notification_changed((event_gatts_notif_t *)event_data);
2145 case OAL_EVENT_GATTS_MTU_CHANGED: {
2146 BT_INFO("OAL Event: GATT Server MTU changed event callback");
2147 __bt_handle_gatt_mtu_changed_event((event_gatts_mtu_changed_t *)event_data);
2150 #ifdef TIZEN_GATT_CLIENT
2151 case OAL_EVENT_GATTC_REGISTRATION: {
2152 BT_INFO("OAL Event: GATT Client instance Registered");
2153 __bt_handle_client_instance_registered((event_gattc_register_t *) event_data);
2156 case OAL_EVENT_GATTC_CONNECTION_COMPLETED: {
2157 BT_INFO("OAL Event: GATT Client Connected");
2158 __bt_handle_client_connected((event_gattc_conn_t *) event_data);
2161 case OAL_EVENT_GATTC_DISCONNECTION_COMPLETED: {
2162 BT_INFO("OAL Event: GATT Client DisConnected");
2163 __bt_handle_client_disconnected((event_gattc_conn_t *) event_data);
2166 case OAL_EVENT_GATTC_SERVICE_SEARCH_RESULT: {
2167 BT_INFO("OAL Event: GATT Client Service Search Result");
2168 __bt_handle_client_service_search_result((event_gattc_service_result_t *) event_data);
2171 case OAL_EVENT_GATTC_SERVICE_SEARCH_DONE: {
2172 BT_INFO("OAL Event: GATT Client Service Completed");
2173 __bt_handle_client_service_search_completed((event_gattc_conn_status_t *) event_data);
2176 case OAL_EVENT_GATTC_CHARAC_SERACH_RESULT: {
2177 BT_INFO("OAL Event: GATT Client Characteristic Search Result");
2178 __bt_handle_client_characteristic_search_result((event_gattc_characteristic_result_t *) event_data);
2181 case OAL_EVENT_GATTC_DESC_SERACH_RESULT: {
2182 BT_INFO("OAL Event: GATT Client Descriptor Search Result");
2183 __bt_handle_client_descriptor_search_result((event_gattc_descriptor_result_t *) event_data);
2186 case OAL_EVENT_GATTC_READ_CHARAC: {
2187 BT_INFO("OAL Event: GATT Client Characteristic Read Data");
2188 __bt_handle_client_characteristic_read_data((event_gattc_read_data *) event_data);
2191 case OAL_EVENT_GATTC_READ_DESCR: {
2192 BT_INFO("OAL Event: GATT Client Descriptor Read Data");
2193 __bt_handle_client_descriptor_read_data((event_gattc_read_data *) event_data);
2196 case OAL_EVENT_GATTC_WRITE_CHARAC: {
2197 BT_INFO("OAL Event: GATT Client Characteristic Write Data");
2198 __bt_handle_client_characteristic_write_data((event_gattc_write_data *) event_data);
2201 case OAL_EVENT_GATTC_WRITE_DESCR: {
2202 BT_INFO("OAL Event: GATT Client Descriptor Write Data");
2203 __bt_handle_client_descriptor_write_data((event_gattc_write_data *) event_data);
2206 case OAL_EVENT_DEVICE_LE_DISCONNECTED: {
2207 BT_INFO("OAL Event: LE device disconnected");
2208 __bt_hanlde_le_device_disconnection((event_dev_conn_status_t *)event_data);
2211 case OAL_EVENT_GATTC_NOTIFICATION_REGISTERED: {
2212 BT_INFO("OAL Event: GATT Client Notification Registered");
2213 __bt_handle_client_notification_registered((event_gattc_regdereg_notify_t *) event_data, TRUE);
2216 case OAL_EVENT_GATTC_NOTIFICATION_DEREGISTERED: {
2217 BT_INFO("OAL Event: GATT Client Notification Registered");
2218 __bt_handle_client_notification_registered((event_gattc_regdereg_notify_t *) event_data, FALSE);
2221 case OAL_EVENT_GATTC_NOTIFY_DATA: {
2222 BT_INFO("OAL Event: GATT Client Notification Data");
2223 __bt_handle_client_notification_data((event_gattc_notify_data *) event_data);
2226 case OAL_EVENT_GATTC_SERVICE_CHANGED_IND: {
2227 BT_INFO("OAL Event: GATT Client service changed indication");
2228 __bt_handle_client_service_changed_ind((event_gattc_service_changed_data *)event_data);
2231 case OAL_EVENT_GATTC_MTU_EXCHANGE_COMPLETED: {
2232 BT_INFO("OAL Event: GATT Client MTU Exchange Complete");
2233 __bt_handle_client_mtu_exchange_completed((event_gattc_mtu_configured_t *) event_data);
2242 int _bt_gatt_server_add_service(char *sender, int service_type,
2243 int num_handles, char *svc_uuid, int instance_id)
2245 BT_CHECK_PARAMETER(svc_uuid, return);
2246 BT_CHECK_PARAMETER(sender, return);
2247 int ret = OAL_STATUS_SUCCESS;
2249 oal_gatt_srvc_id_t svc_data;
2251 svc_data.is_prmry = service_type;
2252 svc_data.id.inst_id = instance_id;
2254 BT_INFO("Service UUID [%s] Num handles [%d] Instance ID [%d]", svc_uuid, num_handles, instance_id);
2255 _bt_string_to_uuid(svc_uuid, (service_uuid_t*)&svc_data.id.uuid);
2257 ret = gatts_add_service(instance_id, &svc_data, num_handles);
2258 if (ret != OAL_STATUS_SUCCESS) {
2259 BT_ERR("ret: %d", ret);
2260 return BLUETOOTH_ERROR_INTERNAL;
2263 return BLUETOOTH_ERROR_NONE;
2267 int _bt_gatt_server_add_included_service(char *sender, int instance_id,
2268 int service_handle, int included_svc_handle)
2270 BT_CHECK_PARAMETER(sender, return);
2271 int ret = OAL_STATUS_SUCCESS;
2273 ret = gatts_add_included_services(instance_id, service_handle, included_svc_handle);
2274 if (ret != OAL_STATUS_SUCCESS) {
2275 BT_ERR("ret: %d", ret);
2276 return BLUETOOTH_ERROR_INTERNAL;
2278 return BLUETOOTH_ERROR_NONE;
2281 int _bt_gatt_server_add_characteristic(char *sender, char *char_uuid,
2282 bluetooth_gatt_server_attribute_params_t *param)
2284 BT_CHECK_PARAMETER(char_uuid, return);
2285 BT_CHECK_PARAMETER(sender, return);
2286 BT_CHECK_PARAMETER(param, return);
2287 int ret = OAL_STATUS_SUCCESS;
2289 oal_uuid_t uuid = {{0} };
2291 BT_INFO("Char UUID [%s] Instance ID [%d]", char_uuid, param->instance_id);
2292 _bt_string_to_uuid(char_uuid, (service_uuid_t*)&uuid);
2294 BT_INFO("Char permission From API [0x%x]", param->permissions);
2296 ret = gatts_add_characteristics(param->instance_id, param->service_handle, &uuid,
2297 param->properties, (int)param->permissions);
2298 if (ret != OAL_STATUS_SUCCESS) {
2299 BT_ERR("ret: %d", ret);
2300 return BLUETOOTH_ERROR_INTERNAL;
2302 return BLUETOOTH_ERROR_NONE;
2305 int _bt_gatt_server_add_descriptor(char *sender, char *desc_uuid,
2306 bt_gatt_permission_t *param, int service_handle, int instance_id)
2308 BT_CHECK_PARAMETER(desc_uuid, return);
2309 BT_CHECK_PARAMETER(sender, return);
2310 BT_CHECK_PARAMETER(param, return);
2311 int ret = OAL_STATUS_SUCCESS;
2313 oal_uuid_t uuid = {{0} };
2315 BT_INFO("Descriptor UUID [%s] Instance ID [%d] Service handle [%d]",
2316 desc_uuid, service_handle, instance_id);
2318 _bt_string_to_uuid(desc_uuid, (service_uuid_t*)&uuid);
2320 BT_INFO("Descriptor permission From API [0x%x]", *param);
2321 ret = gatts_add_descriptor(instance_id, service_handle, &uuid, (int)*param);
2323 if (ret != OAL_STATUS_SUCCESS) {
2324 BT_ERR("ret: %d", ret);
2325 return BLUETOOTH_ERROR_INTERNAL;
2327 return BLUETOOTH_ERROR_NONE;
2330 int _bt_gatt_server_start_service(char *sender, int service_handle, int instance_id)
2332 BT_CHECK_PARAMETER(sender, return);
2333 int ret = OAL_STATUS_SUCCESS;
2335 ret = gatts_start_service(instance_id, service_handle, BT_GATT_TRANSPORT_LE);
2336 if (ret != OAL_STATUS_SUCCESS) {
2337 BT_ERR("ret: %d", ret);
2338 return BLUETOOTH_ERROR_INTERNAL;
2340 return BLUETOOTH_ERROR_NONE;
2343 int _bt_gatt_server_stop_service(char *sender, int service_handle, int instance_id)
2345 BT_CHECK_PARAMETER(sender, return);
2346 int ret = OAL_STATUS_SUCCESS;
2348 ret = gatts_stop_service(instance_id, service_handle);
2349 if (ret != OAL_STATUS_SUCCESS) {
2350 BT_ERR("ret: %d", ret);
2351 return BLUETOOTH_ERROR_INTERNAL;
2353 return BLUETOOTH_ERROR_NONE;
2356 int _bt_gatt_server_delete_service(char *sender, int service_handle, int instance_id)
2358 BT_CHECK_PARAMETER(sender, return);
2359 int ret = OAL_STATUS_SUCCESS;
2363 ret = gatts_delete_service(instance_id, service_handle);
2364 if (ret != OAL_STATUS_SUCCESS) {
2365 BT_ERR("ret: %d", ret);
2366 return BLUETOOTH_ERROR_INTERNAL;
2369 /* Remove the Service Handle */
2370 for (l = numapps[instance_id].service_handles; l != NULL;) {
2372 l = g_slist_next(l);
2373 if (handle && *handle == service_handle) {
2374 BT_INFO("Remove Service handle [%d]", *handle);
2375 numapps[instance_id].service_handles = g_slist_remove(numapps[instance_id].service_handles, handle);
2381 return BLUETOOTH_ERROR_NONE;
2384 int _bt_gatt_server_send_response(char *sender, bluetooth_gatt_att_data_t *data,
2385 bluetooth_gatt_server_response_params_t *param)
2387 BT_CHECK_PARAMETER(sender, return);
2388 BT_CHECK_PARAMETER(data, return);
2389 BT_CHECK_PARAMETER(param, return);
2390 struct gatt_server_req_info *req_info = NULL;
2391 int ret = OAL_STATUS_SUCCESS;
2393 oal_gatt_response_t response;
2395 BT_INFO("GATT Server Response: Req Type [%d] req_id [%d] status [%d] auth_req [%d] offset[%d] data len[%d]",
2396 param->req_type, param->request_id,
2397 param->response_status, param->auth_req,
2398 data->offset, data->length);
2400 /* Search for matching Request in List */
2401 req_info = __bt_gatt_server_find_request_info(param->request_id, param->req_type);
2403 BT_ERR("GATT Server Req Info not found for current response..return Error");
2404 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
2407 memset(&response, 0x00, sizeof(oal_gatt_response_t));
2409 response.handle = req_info->attribute_handle;
2410 response.attr_value.auth_req = param->auth_req;
2411 response.attr_value.handle = req_info->attribute_handle;
2412 response.attr_value.offset = data->offset;
2413 response.attr_value.len = data->length;
2414 memcpy(&response.attr_value.value, &data->data, data->length);
2417 ret = gatts_send_response(req_info->connection_id, param->request_id,
2418 param->response_status, &response);
2420 if (ret != OAL_STATUS_SUCCESS) {
2421 BT_ERR("ret: %d", ret);
2422 return BLUETOOTH_ERROR_INTERNAL;
2425 BT_INFO("GATT Server Response successfully sent");
2426 /* Remove GATT server request from list */
2427 gatt_server_requests = g_slist_remove(gatt_server_requests, req_info);
2428 g_free(req_info->addr);
2430 return BLUETOOTH_ERROR_NONE;
2433 int _bt_gatt_server_acquire_send_response(char *sender, bluetooth_gatt_server_acquire_response_params_t *param , void *fd_list)
2435 BT_CHECK_PARAMETER(sender, return);
2436 BT_CHECK_PARAMETER(param, return);
2437 struct gatt_server_req_info *req_info = NULL;
2438 int ret = OAL_STATUS_SUCCESS;
2441 BT_INFO("GATT acquire Server Response: Req Type [%d] req_id [%d] fd [%d] mtu[%d]",
2442 param->req_type, param->request_id,
2446 /* Search for matching Request in List */
2447 req_info = __bt_gatt_server_find_request_info(param->request_id, param->req_type);
2449 BT_ERR("GATT acquire Server Req Info not found for current response..return Error");
2450 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
2453 ret = gatt_send_response_acquire(req_info->connection_id, param->request_id, 0, param->fd, param->mtu, fd_list);
2455 if (ret != OAL_STATUS_SUCCESS) {
2456 BT_ERR("ret: %d", ret);
2457 return BLUETOOTH_ERROR_INTERNAL;
2460 BT_INFO("GATT acquire Server Response successfully sent");
2461 /* Remove GATT server request from list */
2462 gatt_server_requests = g_slist_remove(gatt_server_requests, req_info);
2463 g_free(req_info->addr);
2465 return BLUETOOTH_ERROR_NONE;
2470 int _bt_gatt_server_send_indication(char *sender, bluetooth_device_address_t *dev_addr,
2471 bluetooth_gatt_att_data_t *data,
2472 bluetooth_gatt_server_indication_params_t *param)
2474 BT_CHECK_PARAMETER(sender, return);
2475 BT_CHECK_PARAMETER(data, return);
2476 BT_CHECK_PARAMETER(param, return);
2478 gboolean all_send = FALSE;
2479 int ret = OAL_STATUS_SUCCESS;
2480 struct gatt_client_info_t *conn;
2482 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2483 _bt_convert_addr_type_to_string(address, dev_addr->addr);
2485 if (memcmp(dev_addr->addr, BDADDR_ANY, 6) == 0) {
2486 BT_INFO("GATT Server: Send Indication to all connected GATT clients..");
2489 BT_INFO("GATT Server: Send Indication to connected GATT client addr [%s]", address);
2492 /* Attempt to send Notification/Indication to all Connected GATT clients */
2494 ret = __bt_gatt_send_indication_to_all_connected_clients(data, param);
2495 if (ret != OAL_STATUS_SUCCESS) {
2496 BT_ERR("ret: %d", ret);
2498 return BLUETOOTH_ERROR_INTERNAL;
2502 conn = _bt_find_remote_gatt_client_info(address);
2504 ret = gatts_send_indication(param->instance_id, param->atrribute_handle,
2505 conn->connection_id, data->length,
2506 param->need_confirmation, (char *)(&data->data[0]));
2508 if (ret != OAL_STATUS_SUCCESS) {
2509 BT_ERR("ret: %d", ret);
2510 BT_INFO("Indication failed to send to Remote GATT Client [%s]", address);
2512 return BLUETOOTH_ERROR_INTERNAL;
2514 BT_INFO("Indication sent to Remote GATT Client [%s] wait for Notification completed event from OAL", address);
2516 num_indicate_clients = 1;
2517 return BLUETOOTH_ERROR_NONE;
2519 BT_ERR("Remote GATT client [%s] is not connected..Cant send Indication!!", address);
2521 return BLUETOOTH_ERROR_NOT_CONNECTED;
2524 return BLUETOOTH_ERROR_NONE;
2527 int _bt_gatt_server_update_attribute_value(char *sender, int instance_id,
2528 bluetooth_gatt_server_update_value_t *param)
2530 BT_CHECK_PARAMETER(sender, return);
2531 BT_CHECK_PARAMETER(param, return);
2532 int ret = OAL_STATUS_SUCCESS;
2534 oal_gatt_value_t value;
2535 BT_INFO("GATT Server Update value: Instance ID [%d] attr handle [%d] Value len [%d]",
2536 instance_id, param->attribute_handle, param->length);
2539 memset(&value, 0x00, sizeof(oal_gatt_value_t));
2541 value.handle = param->attribute_handle;
2542 value.len = param->length;
2543 memcpy(&value.value, ¶m->data.data, param->length);
2545 ret = gatts_update_att_value(instance_id, &value);
2547 if (ret != OAL_STATUS_SUCCESS) {
2548 BT_ERR("ret: %d", ret);
2549 return BLUETOOTH_ERROR_INTERNAL;
2552 BT_INFO("GATT Server Update Attribute Value successfully");
2553 return BLUETOOTH_ERROR_NONE;
2556 int _bt_request_att_mtu(bluetooth_device_address_t *device_address,
2559 struct gatt_server_info_t *conn_info = NULL;
2561 int ret = OAL_STATUS_SUCCESS;
2563 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
2564 _bt_convert_addr_type_to_string(addr, device_address->addr);
2566 /* Check if remote GATT Server is connected or not */
2567 conn_info = _bt_find_remote_gatt_server_info(addr);
2569 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
2570 conn_info->addr, conn_info->connection_id);
2572 BT_ERR("GATT Server is not yet connected..");
2574 return BLUETOOTH_ERROR_NOT_CONNECTED;
2577 ret = gattc_configure_mtu(conn_info->connection_id, mtu);
2578 if (ret != OAL_STATUS_SUCCESS) {
2579 BT_ERR("ret: %d", ret);
2581 return BLUETOOTH_ERROR_INTERNAL;
2585 return BLUETOOTH_ERROR_NONE;
2588 int _bt_get_att_mtu(bluetooth_device_address_t *address,
2591 BT_CHECK_PARAMETER(address, return);
2592 BT_CHECK_PARAMETER(mtu, return);
2593 struct gatt_client_info_t *conn_info = NULL;
2594 char addr[BT_ADDRESS_STRING_SIZE] = { 0 };
2595 int ret = OAL_STATUS_SUCCESS;
2598 _bt_convert_addr_type_to_string(addr, address->addr);
2600 BT_INFO("Get current MTU size for the remote client:DevAddress:[%s]", addr);
2602 conn_info = _bt_find_remote_gatt_client_info(addr);
2604 BT_INFO("GATT Client [%s] is connected, conn Id [%d] Instance ID [%d]",
2605 conn_info->addr, conn_info->connection_id, conn_info->instance_id);
2607 BT_ERR("GATT Client [%s] is not yet connected..", addr);
2608 return BLUETOOTH_ERROR_NOT_CONNECTED;
2611 ret = gatts_get_att_mtu(conn_info->connection_id, &stack_mtu);
2612 if (ret != OAL_STATUS_SUCCESS) {
2613 BT_ERR("ret: %d", ret);
2614 return BLUETOOTH_ERROR_INTERNAL;
2617 BT_INFO("ATT MTU received from OAL [%d]", stack_mtu);
2618 *mtu = (unsigned int)stack_mtu;
2621 BT_ERR("MTU value is zero, GATT Client [%s] is not yet connected..", addr);
2622 return BLUETOOTH_ERROR_NOT_CONNECTED;
2625 return BLUETOOTH_ERROR_NONE;
2628 #ifdef TIZEN_GATT_CLIENT
2629 /* GATT Client utility static functions */
2630 static bt_gatt_service_info_list_t *__bt_get_service_info_list(int conn_id)
2633 bt_gatt_service_info_list_t *info = NULL;
2635 for (l = list_gatt_info; l != NULL; l = g_slist_next(l)) {
2636 info = (bt_gatt_service_info_list_t *)l->data;
2640 if (info->conn_id == conn_id)
2647 static bt_gatt_service_info_t *__bt_find_matching_service(
2648 bt_gatt_service_info_list_t *svc_list, oal_gatt_srvc_id_t *svc)
2651 bt_gatt_service_info_t *info = NULL;
2653 for (l = svc_list->services; l != NULL; l = g_slist_next(l)) {
2654 info = (bt_gatt_service_info_t *)l->data;
2658 /* Match UUID and instance ID */
2659 if (!memcmp(&svc->id.uuid.uuid, &info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
2660 && (svc->id.inst_id == info->inst_id)) {
2667 static bt_gatt_char_info_t *__bt_find_matching_charc(
2668 bt_gatt_service_info_t *svc_info, oal_gatt_id_t *charc)
2671 bt_gatt_char_info_t *info = NULL;
2673 for (l = svc_info->chars; l != NULL; l = g_slist_next(l)) {
2674 info = (bt_gatt_char_info_t *)l->data;
2678 /* Match UUID and instance ID */
2679 if (!memcmp(&charc->uuid.uuid, &info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
2680 && (charc->inst_id == info->inst_id)) {
2687 static bt_gatt_descriptor_info_t *__bt_find_matching_desc(
2688 bt_gatt_char_info_t *char_info, oal_gatt_id_t *desc)
2691 bt_gatt_descriptor_info_t *info = NULL;
2693 for (l = char_info->descs; l != NULL; l = g_slist_next(l)) {
2694 info = (bt_gatt_descriptor_info_t *)l->data;
2698 /* Match UUID and instance ID */
2699 if (!memcmp(&desc->uuid, &info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
2700 && (desc->inst_id == info->inst_id)) {
2708 static struct gatt_server_info_t *__bt_find_remote_gatt_server_info_from_conn_id(int conn_id)
2711 struct gatt_server_info_t *info = NULL;
2713 for (l = gatt_server_info_list; l != NULL; l = g_slist_next(l)) {
2714 info = (struct gatt_server_info_t*)l->data;
2718 if (info->connection_id == conn_id) {
2719 BT_INFO("Remote GATT server found addr[%s]", info->addr);
2726 static bt_gatt_service_info_t* __bt_find_removed_service(bt_gatt_service_info_list_t *svc_list)
2729 bt_gatt_service_info_t *info = NULL;
2731 for (l = svc_list->services; l != NULL; l = g_slist_next(l)) {
2732 info = (bt_gatt_service_info_t*)l->data;
2736 /* Service is marked a removed */
2737 if (info->is_removed == 1)
2743 static void __bt_remove_service_info_from_list(bt_gatt_service_info_t *svc_info)
2749 bt_gatt_char_info_t *charc = NULL;
2750 bt_gatt_included_service_info_t *incl = NULL;
2751 bt_gatt_descriptor_info_t *desc = NULL;
2753 /* Remove all Characteristic and Descriptors within characteristic */
2754 for (l = svc_info->chars; l != NULL;) {
2755 charc = (bt_gatt_char_info_t*)l->data;
2756 l = g_slist_next(l); /* Incase if l is removed, saving next to l */
2761 /* Inside Characteristic */
2762 for (l1 = charc->descs; l1 != NULL;) {
2764 desc = (bt_gatt_descriptor_info_t*)l1->data;
2765 l1 = g_slist_next(l1);
2770 /* Remove Descriptor */
2771 charc->descs = g_slist_remove(charc->descs, desc);
2774 /* Remove Characteristic */
2775 svc_info->chars = g_slist_remove(svc_info->chars, charc);
2779 /* Remove all Included Services */
2780 for (l2 = svc_info->included_svcs; l2 != NULL;) {
2781 incl = (bt_gatt_included_service_info_t*)l2->data;
2782 l2 = g_slist_next(l2); /* Incase if l is removed, saving next to l */
2787 /* Remove included service */
2788 svc_info->included_svcs = g_slist_remove(svc_info->included_svcs, incl);
2795 static void __bt_build_service_browse_info(int conn_id,
2796 bt_services_browse_info_t* info)
2799 bt_gatt_service_info_list_t *svc_info_list;
2800 bt_gatt_service_info_t *svc_info;
2802 service_uuid_t uuid;
2803 struct gatt_server_info_t *conn_info = NULL;
2805 char uuid_string[BLUETOOTH_UUID_STRING_MAX];
2807 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
2812 _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
2814 svc_info_list = __bt_get_service_info_list(conn_id);
2818 info->count = g_slist_length(svc_info_list->services);
2819 BT_DBG("Total services present in the svc info list for this conn id [%d] is [%d]",
2820 conn_id, info->count);
2822 for (l = svc_info_list->services; l != NULL; l = g_slist_next(l)) {
2823 svc_info = (bt_gatt_service_info_t*)l->data;
2824 if (svc_info == NULL)
2827 memcpy(&uuid.uuid, &svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2828 _bt_uuid_to_string(&uuid, uuid_string);
2830 BT_INFO("[%d] %s [%s]", count, uuid_string, _bt_convert_uuid_to_string(uuid_string));
2832 /* Fill UUID of service */
2833 g_strlcpy(info->uuids[count], uuid_string,
2834 BLUETOOTH_UUID_STRING_MAX);
2836 /* Fill instance ID of service */
2837 info->inst_id[count] = svc_info->inst_id;
2839 /* Fill primary service or not info */
2840 info->primary[count] = svc_info->is_primary;
2842 /* Increment count of services browsed */
2847 static void __bt_build_char_browse_info(int conn_id,
2848 bt_gatt_service_info_t *svc_info,
2849 bt_char_browse_info_t* info)
2852 bt_gatt_char_info_t *char_info;
2853 service_uuid_t uuid;
2855 struct gatt_server_info_t *conn_info = NULL;
2857 char uuid_string[BLUETOOTH_UUID_STRING_MAX];
2859 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
2863 /* Fill default data, this will be required even in case of failure */
2864 _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
2865 memcpy(&info->svc_uuid, svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2866 info->svc_inst_id = svc_info->inst_id;
2868 if (!svc_info->chars) {
2869 BT_ERR("No Chars browsed for address [%s]", conn_info->addr);
2873 info->count = g_slist_length(svc_info->chars);
2875 for (l = svc_info->chars; l != NULL; l = g_slist_next(l)) {
2876 char_info = (bt_gatt_char_info_t*)l->data;
2877 if (char_info == NULL)
2880 memcpy(&uuid.uuid, &char_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2881 _bt_uuid_to_string(&uuid, uuid_string);
2883 /* Fill UUID of characteristic */
2884 g_strlcpy(info->uuids[count], uuid_string,
2885 BLUETOOTH_UUID_STRING_MAX);
2887 /* Fill instance ID of characteristic */
2888 info->inst_id[count] = char_info->inst_id;
2890 /* Fill property of characteristic */
2891 info->props[count] = char_info->props;
2893 /* Increment count of services browsed */
2896 BT_DBG("Total characteristics browsed [%d]", count);
2899 static void __bt_build_descriptor_browse_info(int conn_id,
2900 bt_gatt_service_info_t *svc_info,
2901 bt_gatt_char_info_t *char_info,
2902 bt_descriptor_browse_info_t* info)
2905 bt_gatt_descriptor_info_t *desc_info;
2907 service_uuid_t uuid;
2908 struct gatt_server_info_t *conn_info = NULL;
2910 char uuid_string[BLUETOOTH_UUID_STRING_MAX];
2912 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
2914 /* Fill default data, this will be required even in case of failure */
2915 _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
2916 memcpy(&info->svc_uuid, svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2917 info->svc_inst_id = svc_info->inst_id;
2918 memcpy(&info->char_uuid, char_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2919 info->char_inst_id = char_info->inst_id;
2921 /* Fill property of the parent characteristic of this descriptor */
2922 info->char_props_map = char_info->props;
2924 info->count = g_slist_length(char_info->descs);
2926 if (!char_info->descs) {
2927 BT_ERR("No Descriptors browsed for address [%s]", conn_info->addr);
2931 for (l = char_info->descs; l != NULL; l = g_slist_next(l)) {
2932 desc_info = (bt_gatt_descriptor_info_t*)l->data;
2933 if (desc_info == NULL)
2936 memcpy(&uuid.uuid, &desc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2937 _bt_uuid_to_string(&uuid, uuid_string);
2939 /* Fill UUID of Descriptor */
2940 g_strlcpy(info->uuids[count], uuid_string,
2941 BLUETOOTH_UUID_STRING_MAX);
2943 /* Fill instance ID of Descriptor */
2944 info->inst_id[count] = desc_info->inst_id;
2947 /* Increment count of Descriptor browsed */
2951 BT_INFO("Total descriptors browsed [%d]", count);
2954 static void __bt_free_service_info(bt_gatt_service_info_t *svc)
2956 GSList *ll, *lll, *llll;
2957 bt_gatt_char_info_t *chr = NULL;
2958 bt_gatt_descriptor_info_t *desc = NULL;
2959 bt_gatt_included_service_info_t *incl_svc = NULL;
2961 BT_DBG("Service info Is Prim[%d] Inst ID [%d]", svc->is_primary, svc->inst_id);
2962 /* Delete all chars and its descriptors */
2963 for (ll = svc->chars; ll != NULL; ) {
2964 chr = (bt_gatt_char_info_t *)ll->data;
2965 ll = g_slist_next(ll);
2969 for (lll = chr->descs; lll != NULL; ) {
2970 desc = (bt_gatt_descriptor_info_t *)lll->data;
2971 lll = g_slist_next(lll);
2974 chr->descs = g_slist_remove(chr->descs, desc);
2977 svc->chars = g_slist_remove(svc->chars, chr);
2981 /* Delete all included services */
2982 for (llll = svc->included_svcs; llll != NULL; ) {
2983 incl_svc = (bt_gatt_included_service_info_t *)llll->data;
2984 llll = g_slist_next(llll);
2985 if (incl_svc == NULL)
2987 svc->included_svcs = g_slist_remove(svc->included_svcs, incl_svc);
2992 static void __bt_cleanup_remote_services(struct gatt_server_info_t *conn_info)
2994 bt_gatt_service_info_list_t *svc_info_list = NULL;
2995 bt_gatt_service_info_t *svc = NULL;
2998 svc_info_list = __bt_get_service_info_list(conn_info->connection_id);
2999 if (!svc_info_list) {
3000 BT_INFO("Could not find Svc Info list for the connection ID [%d]",
3001 conn_info->connection_id);
3005 BT_INFO("Start Cleanup of all services. Num Services [%d]", g_slist_length(svc_info_list->services));
3006 for (l = svc_info_list->services; l != NULL; ) {
3007 svc = (bt_gatt_service_info_t *)l->data;
3008 l = g_slist_next(l);
3012 __bt_free_service_info(svc);
3013 svc_info_list->services = g_slist_remove(svc_info_list->services, svc);
3017 list_gatt_info = g_slist_remove(list_gatt_info, svc_info_list);
3018 g_free(svc_info_list);
3021 int _bt_register_gatt_client_instance(const char *sender,
3022 bluetooth_device_address_t *address)
3024 int ret = OAL_STATUS_SUCCESS;
3025 char *uuid_string = NULL;
3030 /* App should ensure that it should not send */
3031 BT_INFO("### Check on which instance GATT Client instance can be initialized....");
3032 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
3033 if (numapps[k].is_initialized == 1) {
3034 BT_INFO("Instance ID [%d] is already in use..Check next slot",
3035 numapps[k].instance_id);
3038 BT_INFO("Time to register GATT client instancer..UUID to be used is [%s] slot [%d]",
3039 uuid_list[slot-1], slot);
3045 BT_ERR("No Slot if free for GATT Client registration..");
3046 return BLUETOOTH_ERROR_REGISTRATION_FAILED;
3049 uuid_string = g_malloc0(BT_UUID_STRING_MAX);
3050 _bt_string_to_uuid(uuid_list[slot-1], (service_uuid_t*)&uuid);
3051 g_strlcpy(uuid_string, uuid_list[slot-1], BT_UUID_STRING_MAX);
3052 BT_INFO("Copied UUID string [%s]", uuid_string);
3054 /* Register GATT Client */
3055 ret = gattc_register(&uuid);
3056 if (ret != OAL_STATUS_SUCCESS) {
3057 BT_ERR("ret: %d", ret);
3058 g_free(uuid_string);
3059 return BLUETOOTH_ERROR_INTERNAL;
3062 BT_INFO("GATT Client registration call successfully accepted by OAL..wait for Instance Initialized event from OAL..");
3064 /* Return & wait for GATT Client Instance Initialization event */
3065 memset(numapps[slot].sender, 0x00, sizeof(numapps[slot].sender));
3066 memset(numapps[slot].uuid, 0x00, sizeof(numapps[slot].uuid));
3068 g_strlcpy(numapps[slot].sender, sender, sizeof(numapps[slot].sender));
3069 g_strlcpy(numapps[slot].uuid, uuid_string, sizeof(numapps[slot].uuid));
3071 /* Address is saved here. When event comes, sender + address are matched for replying pending
3072 request. It is impossible for same sender to have requests with two same addresses */
3073 memcpy(&numapps[slot].address.addr, address->addr, sizeof(bluetooth_device_address_t));
3075 BT_INFO("Slot [%d] occupied", slot);
3076 numapps[slot].is_initialized = TRUE; /* Set initialization to true here itself */
3078 g_free(uuid_string);
3079 return BLUETOOTH_ERROR_NONE;
3085 /* GATT client events */
3086 static void __bt_handle_client_instance_registered(event_gattc_register_t *data)
3088 bt_service_app_info_t *info = NULL;
3090 char *uuid_string = g_malloc0(BT_UUID_STRING_MAX);
3092 _bt_uuid_to_string(&(data->client_uuid), uuid_string);
3093 BT_INFO("CLient ID is Initialized [%d] UUID initialized [%s]", data->client_if, uuid_string);
3095 /* Platform GATT client framwork does not use Default GATT client instance
3096 This GATT client instance is never deregistred in the lifetime of bt-service */
3097 if (g_strcmp0(uuid_string, DEFAULT_GATT_CLIENT_UUID) == 0) {
3098 BT_INFO("Default client Instance Registered");
3099 gatt_default_client = data->client_if;
3100 g_free(uuid_string);
3104 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
3107 if (g_strcmp0(info->uuid, uuid_string) == 0) {
3108 BT_INFO("Found GATT client.. sender [%s] Slot [%d] occupied", info->sender, k);
3109 info->is_initialized = TRUE;
3110 info->client_id = data->client_if;
3111 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE, BT_GATT_CLIENT_REGISTER,
3112 (void*)info, sizeof(bt_service_app_info_t));
3116 g_free(uuid_string);
3119 static void __bt_handle_client_connected(event_gattc_conn_t *event_data)
3121 int result = BLUETOOTH_ERROR_NONE;
3122 struct gatt_server_info_t *conn_info = NULL;
3123 struct gatt_out_conn_info_t *out_conn_info = NULL;
3125 GVariant *param = NULL;
3127 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
3128 _bt_convert_addr_type_to_string(address,
3129 (unsigned char *)event_data->address.addr);
3131 if (event_data->status != OAL_STATUS_SUCCESS)
3132 result = BLUETOOTH_ERROR_INTERNAL;
3134 /* DBUS Return fo BT_CONNECT_LE for all the apps */
3135 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE, address,
3136 BT_ADDRESS_STRING_SIZE);
3138 BT_INFO("Local GATT Client Connected: Remote addr[%s] Client Interface [%d] Connection ID [%d] status[%d]",
3139 address, event_data->client_if, event_data->conn_id, event_data->status);
3141 if (result == BLUETOOTH_ERROR_NONE) {
3142 /* Check if device is already in connected list */
3143 conn_info = _bt_find_remote_gatt_server_info(address);
3146 /* Send event to BT-API */
3147 param = g_variant_new("(is)", result, address);
3148 _bt_send_event(BT_DEVICE_EVENT,
3149 BLUETOOTH_EVENT_GATT_CLIENT_CONNECTED, /* Local device is GATT client */
3152 /* Save Connection info */
3153 conn_info = g_new0(struct gatt_server_info_t, 1);
3154 conn_info->addr = g_strdup(address);
3155 conn_info->client_id = event_data->client_if;
3156 conn_info->connection_id = event_data->conn_id;
3157 gatt_server_info_list = g_slist_append(gatt_server_info_list, conn_info);
3158 BT_DBG("Total num of connected Remote GATT server devices [%d]",
3159 g_slist_length(gatt_server_info_list));
3162 BT_INFO("Do a Internal refresh");
3163 if (OAL_STATUS_SUCCESS != gattc_refresh(conn_info->client_id, &event_data->address))
3164 BT_ERR("GATT database refresh failed!!");
3166 BT_INFO("GATT database refresh Success!!");
3169 BT_ERR("Local GATT Client connected event for addr[%s], but device is in connected list already", address);
3171 __bt_add_mtu_gatt_device(address);
3173 BT_ERR("GATT Client Connection failed!!");
3175 /* If outgoing connection Info is present, then remove it */
3176 out_conn_info = __bt_find_gatt_outgoing_conn_info(address);
3177 if (out_conn_info) {
3178 BT_ERR("Outgoing Client connect request was sent");
3179 outgoing_gatt_conn_list = g_slist_remove(outgoing_gatt_conn_list, out_conn_info);
3180 g_free(out_conn_info->addr);
3181 g_free(out_conn_info);
3186 static void __bt_handle_client_disconnected(event_gattc_conn_t *event_data)
3188 int result = BLUETOOTH_ERROR_NONE;
3190 struct gatt_server_info_t *conn_info = NULL;
3191 struct gatt_out_conn_info_t *out_conn_info = NULL;
3193 GVariant *param = NULL;
3195 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
3196 _bt_convert_addr_type_to_string(address,
3197 (unsigned char *)event_data->address.addr);
3199 if (event_data->status != OAL_STATUS_SUCCESS)
3200 result = BLUETOOTH_ERROR_INTERNAL;
3202 if (NULL == _bt_get_request_info_data(BT_DISCONNECT_LE, address)) {
3203 if (NULL != _bt_get_request_info_data(BT_CONNECT_LE, address)) {
3204 result = BLUETOOTH_ERROR_INTERNAL;
3205 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
3206 address, BT_ADDRESS_STRING_SIZE);
3207 BT_ERR("Failed to connect Local GATT Remote addr[%s]", address);
3212 /* DBUS Return for BT_DISCONNECT_LE for all the apps */
3213 __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE, address,
3214 BT_ADDRESS_STRING_SIZE);
3216 BT_INFO("Local GATT Client DisConnected: Remote addr[%s] Client Interface [%d] Connection ID [%d] status [%d]",
3217 address, event_data->client_if, event_data->conn_id, event_data->status);
3219 /* Remove Connection info */
3220 conn_info = _bt_find_remote_gatt_server_info(address);
3223 param = g_variant_new("(is)", result, address);
3224 /* Send event to application */
3225 _bt_send_event(BT_DEVICE_EVENT,
3226 BLUETOOTH_EVENT_GATT_CLIENT_DISCONNECTED,
3229 BT_INFO("Remove GATT server info from List..");
3230 /* Remove all services from info list_gatt_info */
3231 __bt_cleanup_remote_services(conn_info);
3233 /* Remove info from List */
3234 gatt_server_info_list = g_slist_remove(gatt_server_info_list, conn_info);
3235 BT_INFO("Total num of connected GATT servers [%d]", g_slist_length(gatt_server_info_list));
3236 g_free(conn_info->addr);
3239 BT_INFO("Can not find conn info, already removed!");
3241 __bt_remove_mtu_gatt_device(address);
3243 /* If outgoing connection Info is present, then remove it */
3244 out_conn_info = __bt_find_gatt_outgoing_conn_info(address);
3245 if (out_conn_info) {
3246 BT_ERR("Client Disconnected event, but outgoing connect request was sent");
3247 outgoing_gatt_conn_list = g_slist_remove(outgoing_gatt_conn_list, out_conn_info);
3248 g_free(out_conn_info->addr);
3249 g_free(out_conn_info);
3255 static void __bt_handle_client_service_search_result(
3256 event_gattc_service_result_t *event_data)
3258 /* Pre: status is never fail from OAL */
3260 /* Find service list from address */
3261 bt_gatt_service_info_list_t *svc_info_list;
3262 bt_gatt_service_info_t *svc_info;
3264 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3265 if (!svc_info_list) {
3266 BT_DBG("Service info list not present for connection ID %d, means first time browse", event_data->conn_status.conn_id);
3267 /* Means for this conn_id, no services are ever browsed, first time,
3268 create service info list for this conn_id */
3269 svc_info_list = g_malloc0(sizeof(bt_gatt_service_info_list_t));
3270 svc_info_list->conn_id = event_data->conn_status.conn_id;
3271 list_gatt_info = g_slist_append(list_gatt_info, svc_info_list);
3274 /* send list and current service's uuid and instance id to find it */
3275 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3278 /* If not found, check if service changed, if yes, means this is a new service added
3279 in remote GATT device, update uuid info in svc info list structure, to be used when
3280 search is completed */
3282 if (svc_info_list->info.is_changed) {
3283 BT_DBG("Service Changed indication already found for connection ID %d", event_data->conn_status.conn_id);
3284 memcpy(svc_info_list->info.uuid, event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3286 /* Create and add new service in service list */
3287 svc_info = g_malloc0(sizeof(bt_gatt_service_info_t));
3288 memcpy(svc_info->uuid, event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3289 svc_info->inst_id = event_data->srvc_id.id.inst_id;
3290 svc_info->is_primary = event_data->srvc_id.is_prmry;
3291 svc_info_list->services = g_slist_append(svc_info_list->services, svc_info);
3292 BT_DBG("Service created and added in Svc info list or connection ID %d", event_data->conn_status.conn_id);
3294 /* If returned matching service info, then just update service_rmeoved value inside it to 0 */
3295 svc_info->is_removed = 0;
3299 static void __bt_handle_client_service_search_completed(
3300 event_gattc_conn_status_t *event_data)
3302 struct gatt_server_info_t *conn_info = NULL;
3303 bt_gatt_service_info_list_t *svc_info_list;
3304 bt_gatt_service_info_t *svc_info;
3305 bt_services_browse_info_t browse_info;
3306 unsigned char uuid_empty[BLUETOOTH_UUID_HEX_MAX_LEN];
3308 memset(&uuid_empty, 0x00, BLUETOOTH_UUID_HEX_MAX_LEN);
3309 memset(&browse_info, 0x00, sizeof(bt_services_browse_info_t));
3310 BT_INFO("Primary Services browsing completed status[%d] conn ID [%d]",
3311 event_data->status, event_data->conn_id);
3313 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(event_data->conn_id);
3315 svc_info_list = __bt_get_service_info_list(event_data->conn_id);
3316 if (!svc_info_list) {
3317 BT_ERR("No services browsed ever for addr [%s]", conn_info->addr);
3319 /* Just build response and return ERROR */
3320 __bt_build_service_browse_info(event_data->conn_id, &browse_info);
3322 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_INTERNAL,
3323 BT_GATT_GET_PRIMARY_SERVICES, &browse_info,
3324 sizeof(bt_services_browse_info_t));
3328 /* If fail, then send event with error */
3329 if (event_data->status != OAL_STATUS_SUCCESS) {
3330 /* Just build response and return ERROR */
3331 __bt_build_service_browse_info(event_data->conn_id, &browse_info);
3333 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_INTERNAL,
3334 BT_GATT_GET_PRIMARY_SERVICES, &browse_info,
3335 sizeof(bt_services_browse_info_t));
3339 /* If success, then find service info list from address */
3341 /* If svc_changed == 1 and uuid valid, means a new service is added*/
3342 if (svc_info_list->info.is_changed && !memcmp(uuid_empty, svc_info_list->info.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)) {
3343 /* TODO: Send event -Service added with instance ID and UUID of newly added service */
3344 BT_INFO("new service added");
3346 BT_INFO("TODO new service added");
3349 /* If svc_changed == 1 and uuid invalid, then a service is removed */
3350 if (svc_info_list->info.is_changed && memcmp(uuid_empty, svc_info_list->info.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)) {
3351 /* Scan through the service info list to find service with is_removed = 1*/
3352 svc_info = __bt_find_removed_service(svc_info_list);
3354 /* TODO Send event - Service removed with instance ID and UUID of just rmeoved service */
3356 /* Remove that service info from service info list */
3357 svc_info_list->services = g_slist_remove(svc_info_list->services, svc_info);
3359 /* Delete that service completely from svc_info list*/
3360 __bt_remove_service_info_from_list(svc_info);
3364 /* Reset svc_changed = 0, and reset UUID = all 0's */
3365 svc_info_list->info.is_changed = 0;
3366 memset(&svc_info_list->info.uuid, 0x00, BLUETOOTH_UUID_HEX_MAX_LEN);
3368 /* Build Reply and send to service browse primary services request of pending apps */
3369 __bt_build_service_browse_info(event_data->conn_id, &browse_info);
3371 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE,
3372 BT_GATT_GET_PRIMARY_SERVICES, &browse_info,
3373 sizeof(bt_services_browse_info_t));
3377 static void __bt_handle_client_characteristic_search_result(
3378 event_gattc_characteristic_result_t *event_data)
3380 bt_gatt_service_info_list_t *svc_info_list;
3381 bt_gatt_service_info_t *svc_info;
3382 bt_gatt_char_info_t *char_info;
3383 bt_char_browse_info_t browse_info;
3385 memset(&browse_info, 0x00, sizeof(bt_char_browse_info_t));
3388 if (event_data->conn_status.status == OAL_STATUS_SUCCESS) {
3389 /* Find service info list from address */
3390 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3391 if (svc_info_list == NULL) {
3392 BT_ERR("svc_info_list is NULL");
3397 /* Find matching service info from svc info list */
3398 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3399 if (svc_info == NULL) {
3400 BT_ERR("svc_info is NULL");
3404 /* Find Matching char from service info in event */
3405 char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
3407 /* If not found, then add new characteristic and return */
3409 BT_DBG("Add new characteristic");
3410 char_info = g_malloc0(sizeof(bt_gatt_char_info_t));
3411 memcpy(char_info->uuid, event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3413 char_info->inst_id = event_data->char_id.inst_id;
3414 char_info->props = event_data->char_prop;
3415 svc_info->chars = g_slist_append(svc_info->chars, char_info);
3417 /* If found, then return */
3418 BT_DBG("update char property as Characteristic browsed is already present");
3419 char_info->props |= event_data->char_prop;
3422 /* If Not success: Means Charc browse is completed */
3423 /* Find char list from service in event */
3424 /* Find service list from address */
3425 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3426 if (svc_info_list == NULL) {
3427 BT_ERR("svc_info_list is NULL");
3431 /* Find service info from service in event */
3432 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3433 if (svc_info == NULL) {
3434 BT_ERR("svc_info is NULL");
3438 /* Build char list from service in event */
3439 __bt_build_char_browse_info(event_data->conn_status.conn_id,
3440 svc_info, &browse_info);
3442 /* Create response and return by sending event*/
3443 /* Build Reply and send to service browse All Included services request of pending apps */
3444 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE,
3445 BT_GATT_GET_SERVICE_PROPERTIES,
3447 sizeof(bt_char_browse_info_t));
3451 static void __bt_handle_client_descriptor_search_result(
3452 event_gattc_descriptor_result_t *event_data)
3454 bt_gatt_service_info_list_t *svc_info_list;
3455 bt_gatt_service_info_t *svc_info;
3456 bt_gatt_char_info_t *char_info;
3457 bt_gatt_descriptor_info_t *desc_info;
3458 bt_descriptor_browse_info_t browse_info;
3460 BT_INFO("descriptor search result status [%d]", event_data->conn_status.status);
3462 memset(&browse_info, 0x00, sizeof(bt_descriptor_browse_info_t));
3465 if (event_data->conn_status.status == OAL_STATUS_SUCCESS) {
3466 /* Find service list from address */
3467 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3468 if (svc_info_list == NULL) {
3469 BT_ERR("svc_info_list is NULL");
3473 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3474 if (svc_info == NULL) {
3475 BT_ERR("svc_info is NULL");
3479 char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
3480 if (char_info == NULL) {
3481 BT_ERR("char_info is NULL");
3485 desc_info = __bt_find_matching_desc(char_info, &event_data->descr_id);
3486 /* If not found, add new descriptor and return */
3488 desc_info = g_malloc0(sizeof(bt_gatt_descriptor_info_t));
3489 memcpy(desc_info->uuid, event_data->descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3491 desc_info->inst_id = event_data->descr_id.inst_id;
3492 char_info->descs = g_slist_append(char_info->descs, desc_info);
3495 /* If found, then return */
3496 BT_INFO("Characteristic browsed is already presesnt");
3499 /* If Not success */
3500 /* Find service list from address */
3501 /* Find included service list from service in event */
3502 /* Create response and return by sending event*/
3503 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3504 if (svc_info_list == NULL) {
3505 BT_ERR("svc_info_list is NULL");
3509 /* Find service info from service in event */
3510 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3511 if (svc_info == NULL) {
3512 BT_ERR("svc_info is NULL");
3516 /* Find char info from char in event */
3517 char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
3518 if (char_info == NULL) {
3519 BT_ERR("char_info is NULL");
3523 /* Build descriptor list from char in event */
3524 __bt_build_descriptor_browse_info(event_data->conn_status.conn_id,
3525 svc_info, char_info, &browse_info);
3528 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE,
3529 BT_GATT_GET_CHARACTERISTIC_PROPERTIES,
3531 sizeof(bt_descriptor_browse_info_t));
3535 static void __bt_handle_client_characteristic_read_data(
3536 event_gattc_read_data *event_data)
3538 int result = BLUETOOTH_ERROR_NONE;
3539 struct gatt_server_info_t *conn_info = NULL;
3540 bluetooth_gatt_client_char_prop_info_t read_info;
3542 /* Read Information data structures */
3543 GVariant *param = NULL;
3544 GVariant *data = NULL;
3545 GVariant *data_svc_uuid = NULL;
3546 GVariant *data_char_uuid = NULL;
3547 char *read_val = NULL;
3548 char *svc_uuid = NULL;
3549 char *char_uuid = NULL;
3552 //memset(&read_info, 0x00, sizeof(bt_gatt_handle_property_t));
3553 memset(&read_info, 0x00, sizeof(bluetooth_gatt_client_char_prop_info_t));
3555 /* Extract Address from conn_id of event data */
3556 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3557 event_data->uuid_status.conn_status.conn_id);
3559 BT_INFO("Characteristic Read result from addr [%s] status [%d]",
3560 conn_info->addr, event_data->uuid_status.conn_status.status);
3562 /* Fill char in buffer */
3563 memcpy(&read_info.characteristic.uuid,
3564 event_data->uuid_status.char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3565 read_info.characteristic.instance_id = event_data->uuid_status.char_id.inst_id;
3567 /* Fill Service in buffer */
3568 memcpy(&read_info.svc.uuid,
3569 event_data->uuid_status.srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3570 read_info.svc.instance_id = event_data->uuid_status.srvc_id.id.inst_id;
3572 /* Fill remote device address */
3573 _bt_convert_addr_string_to_type(read_info.device_address.addr, conn_info->addr);
3575 /* Fill data and reply to all apps waiting for Read result on the same characteristic
3576 Note: Even in case of failure, address, handles and result code should be returned */
3577 if (event_data->uuid_status.conn_status.status != OAL_STATUS_SUCCESS)
3578 result = BLUETOOTH_ERROR_INTERNAL;
3580 if (event_data->data_len > 0) {
3582 // for (i = 0; i < event_data->data_len; i++)
3583 // BT_INFO("Data[%d] = [0x%x]", i, event_data->data[i]);
3586 read_val = g_memdup(&event_data->data[0], event_data->data_len);
3588 data = g_variant_new_from_data(
3589 G_VARIANT_TYPE_BYTESTRING,
3591 event_data->data_len,
3594 BT_ERR("Characteristic Read success, but no data!!!");
3596 data = g_variant_new_from_data(
3597 G_VARIANT_TYPE_BYTESTRING,
3604 svc_uuid = g_memdup(&event_data->uuid_status.srvc_id.id.uuid.uuid[0], uuid_len);
3606 data_svc_uuid = g_variant_new_from_data(
3607 G_VARIANT_TYPE_BYTESTRING,
3613 char_uuid = g_memdup(&event_data->uuid_status.char_id.uuid.uuid[0], uuid_len);
3615 data_char_uuid = g_variant_new_from_data(
3616 G_VARIANT_TYPE_BYTESTRING,
3621 param = g_variant_new("(isn@ayin@ayin@ay)", result,
3625 event_data->uuid_status.srvc_id.id.inst_id,
3628 event_data->uuid_status.char_id.inst_id,
3629 event_data->data_len,
3633 _bt_send_event(BT_GATT_CLIENT_EVENT,
3634 BLUETOOTH_EVENT_GATT_READ_CHAR,
3637 /* Send DBUS return */
3638 __bt_gatt_handle_pending_request_info(result,
3639 BT_GATT_READ_CHARACTERISTIC,
3641 sizeof(bluetooth_gatt_client_char_prop_info_t));
3652 static void __bt_handle_client_descriptor_read_data(
3653 event_gattc_read_data *event_data)
3655 int result = BLUETOOTH_ERROR_NONE;
3656 struct gatt_server_info_t *conn_info = NULL;
3657 bluetooth_gatt_client_desc_prop_info_t read_info;
3659 /* Read Information data structures */
3660 GVariant *param = NULL;
3661 GVariant *data = NULL;
3662 GVariant *data_svc_uuid = NULL;
3663 GVariant *data_char_uuid = NULL;
3664 GVariant *data_desc_uuid = NULL;
3665 char *read_val = NULL;
3666 char *svc_uuid = NULL;
3667 char *char_uuid = NULL;
3668 char *desc_uuid = NULL;
3673 memset(&read_info, 0x00, sizeof(bluetooth_gatt_client_desc_prop_info_t));
3675 /* Extract Address from conn_id of event data */
3676 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3677 event_data->uuid_status.conn_status.conn_id);
3679 BT_INFO("Descriptor Read result from addr [%s] status [%d]",
3680 conn_info->addr, event_data->uuid_status.conn_status.status);
3682 /* Fill descriptor informations in buffer */
3683 memcpy(&read_info.descriptor.uuid,
3684 event_data->uuid_status.descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3685 read_info.descriptor.instance_id = event_data->uuid_status.descr_id.inst_id;
3687 /* Fill Characteristic informations in buffer */
3688 memcpy(&read_info.characteristic.uuid,
3689 event_data->uuid_status.char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3690 read_info.characteristic.instance_id = event_data->uuid_status.char_id.inst_id;
3692 /* Fill Service informations in buffer */
3693 memcpy(&read_info.svc.uuid,
3694 event_data->uuid_status.srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3695 read_info.svc.instance_id = event_data->uuid_status.srvc_id.id.inst_id;
3697 /* Fill remote device address */
3698 _bt_convert_addr_string_to_type(read_info.device_address.addr, conn_info->addr);
3700 /* Fill data and reply to all apps waiting for Read result on the same characteristic */
3701 if (event_data->uuid_status.conn_status.status != OAL_STATUS_SUCCESS)
3702 result = BLUETOOTH_ERROR_INTERNAL;
3704 BT_INFO("desc data len:%d", event_data->data_len);
3705 if (event_data->data_len > 0) {
3707 for (i = 0; i < event_data->data_len; i++)
3708 BT_DBG("Data[%d] = [0x%x]", i, event_data->data[i]);
3711 read_val = g_memdup(&event_data->data[0], event_data->data_len);
3713 data = g_variant_new_from_data(
3714 G_VARIANT_TYPE_BYTESTRING,
3716 event_data->data_len,
3719 BT_INFO("Descriptor Read success, but no data!!!");
3721 data = g_variant_new_from_data(
3722 G_VARIANT_TYPE_BYTESTRING,
3728 svc_uuid = g_memdup(&event_data->uuid_status.srvc_id.id.uuid.uuid[0], uuid_len);
3730 data_svc_uuid = g_variant_new_from_data(
3731 G_VARIANT_TYPE_BYTESTRING,
3737 char_uuid = g_memdup(&event_data->uuid_status.char_id.uuid.uuid[0], uuid_len);
3739 data_char_uuid = g_variant_new_from_data(
3740 G_VARIANT_TYPE_BYTESTRING,
3746 desc_uuid = g_memdup(&event_data->uuid_status.descr_id.uuid.uuid[0], uuid_len);
3748 data_desc_uuid = g_variant_new_from_data(
3749 G_VARIANT_TYPE_BYTESTRING,
3754 param = g_variant_new("(isn@ayin@ayin@ayin@ay)", result,
3758 event_data->uuid_status.srvc_id.id.inst_id,
3761 event_data->uuid_status.char_id.inst_id,
3764 event_data->uuid_status.descr_id.inst_id,
3765 event_data->data_len,
3769 _bt_send_event(BT_GATT_CLIENT_EVENT,
3770 BLUETOOTH_EVENT_GATT_READ_DESC,
3773 BT_INFO("Send DBUS rpely for GATT Read Descriptor");
3774 /* Send DBUS return */
3775 __bt_gatt_handle_pending_request_info(result,
3776 BT_GATT_READ_DESCRIPTOR_VALUE,
3778 sizeof(bluetooth_gatt_client_desc_prop_info_t));
3789 static void __bt_handle_client_characteristic_write_data(
3790 event_gattc_write_data *event_data)
3792 int result = BLUETOOTH_ERROR_NONE;
3793 struct gatt_server_info_t *conn_info = NULL;
3794 bluetooth_gatt_client_char_prop_info_t write_info;
3796 /* Read Information data structures */
3797 GVariant *param = NULL;
3798 GVariant *data_svc_uuid = NULL;
3799 GVariant *data_char_uuid = NULL;
3800 char *svc_uuid = NULL;
3801 char *char_uuid = NULL;
3805 memset(&write_info, 0x00, sizeof(bluetooth_gatt_client_char_prop_info_t));
3807 /* Extract Address from conn_id of event data */
3808 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3809 event_data->conn_status.conn_id);
3811 BT_INFO("Characteristic Write callback from addr [%s] status [%d]",
3812 conn_info->addr, event_data->conn_status.status);
3814 /* Fill char in buffer */
3815 memcpy(&write_info.characteristic.uuid,
3816 event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3817 write_info.characteristic.instance_id = event_data->char_id.inst_id;
3819 /* Fill Service in buffer */
3820 memcpy(&write_info.svc.uuid,
3821 event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3822 write_info.svc.instance_id = event_data->srvc_id.id.inst_id;
3824 /* Fill remote device address */
3825 _bt_convert_addr_string_to_type(write_info.device_address.addr, conn_info->addr);
3827 if (event_data->conn_status.status != OAL_STATUS_SUCCESS) {
3828 result = BLUETOOTH_ERROR_INTERNAL;
3834 svc_uuid = g_memdup(&event_data->srvc_id.id.uuid.uuid[0], uuid_len);
3836 data_svc_uuid = g_variant_new_from_data(
3837 G_VARIANT_TYPE_BYTESTRING,
3843 char_uuid = g_memdup(&event_data->char_id.uuid.uuid[0], uuid_len);
3845 data_char_uuid = g_variant_new_from_data(
3846 G_VARIANT_TYPE_BYTESTRING,
3851 param = g_variant_new("(isn@ayin@ayi)", result,
3855 event_data->srvc_id.id.inst_id,
3858 event_data->char_id.inst_id);
3861 _bt_send_event(BT_GATT_CLIENT_EVENT,
3862 BLUETOOTH_EVENT_GATT_WRITE_CHAR,
3871 /* Send DBUS return */
3872 __bt_gatt_handle_pending_request_info(result,
3873 BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE,
3875 sizeof(bluetooth_gatt_client_char_prop_info_t));
3879 static void __bt_handle_client_descriptor_write_data(
3880 event_gattc_write_data *event_data)
3882 int result = BLUETOOTH_ERROR_NONE;
3883 struct gatt_server_info_t *conn_info = NULL;
3884 bluetooth_gatt_client_desc_prop_info_t write_info;
3886 /* Write Information data structures */
3887 GVariant *param = NULL;
3888 GVariant *data_svc_uuid = NULL;
3889 GVariant *data_char_uuid = NULL;
3890 GVariant *data_desc_uuid = NULL;
3891 char *svc_uuid = NULL;
3892 char *char_uuid = NULL;
3893 char *desc_uuid = NULL;
3897 memset(&write_info, 0x00, sizeof(bluetooth_gatt_client_desc_prop_info_t));
3899 /* Extract Address from conn_id of event data */
3900 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3901 event_data->conn_status.conn_id);
3903 if (NULL == conn_info) {
3905 BT_INFO("Failed to get the conn info for conn_id [%d]", event_data->conn_status.conn_id);
3909 BT_INFO("Descriptor Write callback from addr [%s] status [%d]",
3910 conn_info->addr, event_data->conn_status.status);
3912 /* Fill descriptor informations in buffer */
3913 memcpy(&write_info.descriptor.uuid,
3914 event_data->descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3915 write_info.descriptor.instance_id = event_data->descr_id.inst_id;
3917 /* Fill Characteristic informations in buffer */
3918 memcpy(&write_info.characteristic.uuid,
3919 event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3920 write_info.characteristic.instance_id = event_data->char_id.inst_id;
3922 /* Fill Service informations in buffer */
3923 memcpy(&write_info.svc.uuid,
3924 event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3925 write_info.svc.instance_id = event_data->srvc_id.id.inst_id;
3927 /* Fill remote device address */
3928 _bt_convert_addr_string_to_type(write_info.device_address.addr, conn_info->addr);
3930 if (event_data->conn_status.status != OAL_STATUS_SUCCESS) {
3931 result = BLUETOOTH_ERROR_INTERNAL;
3937 svc_uuid = g_memdup(&event_data->srvc_id.id.uuid.uuid[0], uuid_len);
3939 data_svc_uuid = g_variant_new_from_data(
3940 G_VARIANT_TYPE_BYTESTRING,
3946 char_uuid = g_memdup(&event_data->char_id.uuid.uuid[0], uuid_len);
3948 data_char_uuid = g_variant_new_from_data(
3949 G_VARIANT_TYPE_BYTESTRING,
3955 desc_uuid = g_memdup(&event_data->descr_id.uuid.uuid[0], uuid_len);
3957 data_desc_uuid = g_variant_new_from_data(
3958 G_VARIANT_TYPE_BYTESTRING,
3963 param = g_variant_new("(isn@ayin@ayin@ayi)", result,
3967 event_data->srvc_id.id.inst_id,
3970 event_data->char_id.inst_id,
3973 event_data->descr_id.inst_id);
3976 _bt_send_event(BT_GATT_CLIENT_EVENT,
3977 BLUETOOTH_EVENT_GATT_WRITE_DESC,
3988 /* Send DBUS return */
3989 __bt_gatt_handle_pending_request_info(result,
3990 BT_GATT_WRITE_DESCRIPTOR_VALUE,
3992 sizeof(bluetooth_gatt_client_desc_prop_info_t));
3995 static void __bt_hanlde_le_device_disconnection(event_dev_conn_status_t *event_data)
3997 int result = BLUETOOTH_ERROR_INTERNAL;
3998 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
4000 _bt_convert_addr_type_to_string(address, (unsigned char *)event_data->address.addr);
4002 /* DBUS Return with fail of pending BT_CONNECT_LE for all the apps */
4003 BT_INFO("Local GATT Client disconnected: Remote addr[%s] ", address);
4005 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE, address,
4006 BT_ADDRESS_STRING_SIZE);
4010 static void __bt_handle_client_notification_registered(
4011 event_gattc_regdereg_notify_t *event_data,
4012 gboolean is_registered)
4014 int result = BLUETOOTH_ERROR_NONE;
4015 struct gatt_server_info_t *conn_info = NULL;
4016 bt_gatt_notif_reg_info_t notif_info;
4019 memset(¬if_info, 0x00, sizeof(bt_gatt_notif_reg_info_t));
4021 BT_INFO("Client Interface [%d] status [%d]",
4022 event_data->client_if,
4023 event_data->status);
4025 /* Extract Address from conn_id of event data */
4026 conn_info = __bt_find_remote_gatt_server_info_from_client_if(
4027 event_data->client_if);
4030 BT_INFO("Connection Info is not present, return");
4033 BT_INFO("Notification Registered for addr [%s]",
4037 /* Fill svc informations in buffer */
4038 memcpy(¬if_info.svc_uuid,
4039 event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4040 notif_info.svc_inst = event_data->srvc_id.id.inst_id;
4042 /* Fill char in buffer */
4043 memcpy(¬if_info.char_uuid,
4044 event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4045 notif_info.char_inst = event_data->char_id.inst_id;
4047 /* Fill remote device address */
4048 _bt_convert_addr_string_to_type(notif_info.addr.addr, conn_info->addr);
4050 notif_info.is_registered = is_registered;
4052 if (event_data->status != OAL_STATUS_SUCCESS)
4053 result = BLUETOOTH_ERROR_INTERNAL;
4055 /* Send DBUS Return for BT_GATT_WATCH_CHARACTERISTIC */
4056 __bt_gatt_handle_pending_request_info(result,
4057 BT_GATT_WATCH_CHARACTERISTIC,
4059 sizeof(bt_gatt_notif_reg_info_t));
4062 static void __bt_handle_client_notification_data(event_gattc_notify_data *event_data)
4064 /* No status in this event from OAL */
4065 int result = BLUETOOTH_ERROR_NONE;
4067 /* Read Information data structures */
4068 GVariant *param = NULL;
4069 GVariant *data = NULL;
4070 GVariant *data_svc_uuid = NULL;
4071 GVariant *data_char_uuid = NULL;
4072 char *read_val = NULL;
4073 char *svc_uuid = NULL;
4074 char *char_uuid = NULL;
4080 BT_INFO("Notifcation of charc data changed");
4082 if (event_data->data_len > 0) {
4084 for (i = 0; i < event_data->data_len; i++)
4085 BT_INFO("Data[%d] = [0x%x]", i, event_data->data[i]);
4088 addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
4089 _bt_convert_addr_type_to_string(addr,
4090 (unsigned char *)&(event_data->address.addr));
4093 read_val = g_memdup(&event_data->data[0], event_data->data_len);
4095 data = g_variant_new_from_data(
4096 G_VARIANT_TYPE_BYTESTRING,
4098 event_data->data_len,
4101 svc_uuid = g_memdup(&event_data->srvc_id.id.uuid.uuid[0], uuid_len);
4103 data_svc_uuid = g_variant_new_from_data(
4104 G_VARIANT_TYPE_BYTESTRING,
4110 char_uuid = g_memdup(&event_data->char_id.uuid.uuid[0], uuid_len);
4112 data_char_uuid = g_variant_new_from_data(
4113 G_VARIANT_TYPE_BYTESTRING,
4119 param = g_variant_new("(isn@ayin@ayin@ay)", result,
4123 event_data->srvc_id.id.inst_id,
4126 event_data->char_id.inst_id,
4127 event_data->data_len,
4131 _bt_send_event(BT_GATT_CLIENT_EVENT,
4132 BLUETOOTH_EVENT_GATT_CHAR_VAL_CHANGED,
4135 BT_ERR("No Data!!");
4148 static void __bt_handle_client_service_changed_ind(event_gattc_service_changed_data *event_data)
4150 bt_gatt_service_info_list_t *svc_info_list;
4152 bt_gatt_service_info_t *svc_info;
4153 GVariant *param = NULL;
4154 char *address_str = NULL;
4155 char *uuid_str = NULL;
4157 svc_info_list = __bt_get_service_info_list(event_data->conn_id);
4158 if (svc_info_list == NULL) {
4159 BT_ERR("svc_info_list is NULL");
4163 if (event_data->change_type) {
4164 /* Add service UUID in list */
4165 svc_info = g_malloc0(sizeof(bt_gatt_service_info_t));
4166 memcpy(svc_info->uuid, event_data->uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4167 svc_info->inst_id = event_data->inst_id;
4168 svc_info->is_primary = 1; // TODO: Need to check is_primary is required or not
4169 svc_info_list->services = g_slist_append(svc_info_list->services, svc_info);
4170 BT_DBG("Service created and added in Svc info list or connection ID %d", event_data->conn_id);
4172 /* Remove service UUID in list */
4173 for (l = svc_info_list->services; l != NULL; l = g_slist_next(l)) {
4174 svc_info = (bt_gatt_service_info_t *)l->data;
4175 if (svc_info == NULL)
4178 if (!memcmp(svc_info->uuid, event_data->uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)) {
4179 svc_info_list->services = g_slist_remove(svc_info_list->services, svc_info);
4180 __bt_free_service_info(svc_info);
4186 address_str = g_malloc0(BT_ADDRESS_STRING_SIZE);
4187 uuid_str = g_malloc0(BT_UUID_STRING_MAX);
4188 _bt_convert_addr_type_to_string(address_str, event_data->address.addr);
4189 _bt_uuid_to_string(&event_data->uuid, uuid_str);
4191 param = g_variant_new("(iiss)", event_data->inst_id, event_data->change_type, address_str, uuid_str);
4193 _bt_send_event(BT_GATT_CLIENT_EVENT,
4194 BLUETOOTH_EVENT_GATT_CLIENT_SERVICE_CHANGED,
4196 g_free(address_str);
4200 gboolean _bt_is_remote_gatt_device_connected(bluetooth_device_address_t *address)
4203 struct gatt_server_info_t *conn_info = NULL;
4204 gboolean connected = FALSE;
4206 addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
4207 _bt_convert_addr_type_to_string(addr,
4208 (unsigned char *)&(address->addr));
4210 BT_DBG("Check GATT connection status of [%s]", addr);
4211 /* Check if device is already in connected list */
4212 conn_info = _bt_find_remote_gatt_server_info(addr);
4215 BT_DBG("Remote GATT Server device [%s] is Connected", conn_info->addr);
4218 struct gatt_client_info_t *client_info = NULL;
4220 BT_INFO("Remote GATT Server Device [%s] is not Connected", addr);
4222 /* Check if device is already in connected list */
4223 client_info = _bt_find_remote_gatt_client_info(addr);
4226 BT_DBG("Remote Client device [%s] is Connected", client_info->addr);
4229 BT_INFO("Remote GATT Client Device [%s] is not Connected", addr);
4237 void _bt_handle_invocation_context(int function_name, void *data)
4239 switch (function_name) {
4241 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE, BT_CONNECT_LE,
4242 (char *)data, BT_ADDRESS_STRING_SIZE);
4249 int _bt_connect_le_device(bluetooth_device_address_t *address,
4250 int auto_connect, int client_id)
4252 struct gatt_server_info_t *conn_info = NULL;
4253 struct gatt_out_conn_info_t *out_conn_info = NULL;
4255 invocation_info_t *req_info = NULL;
4256 int ret = OAL_STATUS_SUCCESS;
4258 char *remote_address = NULL;
4260 BT_CHECK_PARAMETER(address, return);
4262 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4263 _bt_convert_addr_type_to_string(addr, address->addr);
4264 BT_DBG("GATT Client connect request for address [%s] client instance [%d]",
4268 /* Check if Remote Device is already under connection progress */
4269 req_info = _bt_get_request_info_data_from_function_name(BT_CONNECT_LE);
4271 remote_address = (char*)req_info->user_data;
4272 if (remote_address && !strcasecmp(remote_address, addr)) {/* Address matched */
4273 BT_DBG("Already Connection ongoing for same remote GATT Server address [%s]", remote_address);
4274 /* Return and wait for events to be sent to all apps */
4276 return BLUETOOTH_ERROR_IN_PROGRESS;
4280 /* Check if remote GATT Server is connected or not */
4281 conn_info = _bt_find_remote_gatt_server_info(addr);
4283 BT_ERR("GATT Server is already connected..");
4285 return BLUETOOTH_ERROR_ALREADY_CONNECT;
4288 /* TODO Check Requirement of holding Advertisement before initiating LE connect */
4290 /* Check if app sent 0 client id for connection, in such case, use default gatt client ID */
4291 if (client_id == 0) {
4292 /* GATT CLient connect request sent by an app without any client instance [0] */
4293 BT_DBG("Assign default GATT client id [%d]", gatt_default_client);
4294 client_id = gatt_default_client;
4297 BT_INFO("Connect using CLient ID [%d]", client_id);
4298 ret = gattc_connect(client_id, (bt_address_t*)(address), auto_connect);
4300 if (ret != OAL_STATUS_SUCCESS) {
4301 BT_ERR("gattc_connect is failed. ret: %d", ret);
4303 return BLUETOOTH_ERROR_INTERNAL;
4306 /* Mark this as outgoing connection */
4307 out_conn_info = g_new0(struct gatt_out_conn_info_t, 1);
4308 out_conn_info->addr = g_strdup(addr);
4309 out_conn_info->client_id = client_id;
4310 BT_INFO("Added outgoing connection info addr[%s]", out_conn_info->addr);
4311 outgoing_gatt_conn_list = g_slist_append(outgoing_gatt_conn_list, out_conn_info);
4314 return BLUETOOTH_ERROR_NONE;
4317 int _bt_gatt_get_primary_services(char *address)
4319 BT_CHECK_PARAMETER(address, return);
4320 struct gatt_server_info_t *conn_info = NULL;
4321 invocation_info_t *req_info = NULL;
4322 int ret = OAL_STATUS_SUCCESS;
4325 /* Check if any app is already browsing primary services on the same remote GATT Server */
4326 req_info = _bt_get_request_info_data(BT_GATT_GET_PRIMARY_SERVICES, address);
4328 BT_INFO("Already Primary Service Browsing ongoing for same rmeote GATT Server");
4329 /* Return and wait for events to be sent to all apps */
4330 return BLUETOOTH_ERROR_NONE;
4333 /* Check if remote GATT Server is connected or not */
4334 conn_info = _bt_find_remote_gatt_server_info(address);
4336 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4337 conn_info->addr, conn_info->connection_id);
4339 BT_ERR("GATT Server is not yet connected..");
4340 return BLUETOOTH_ERROR_NOT_CONNECTED;
4343 BT_INFO("Remove GATT server info from List..");
4344 /* Remove all services from info list_gatt_info */
4345 __bt_cleanup_remote_services(conn_info);
4347 /* Send Primary Service Browsing request to stack */
4348 ret = gattc_search_service(conn_info->connection_id, NULL);
4349 if (ret != OAL_STATUS_SUCCESS) {
4350 BT_ERR("ret: %d", ret);
4351 return BLUETOOTH_ERROR_INTERNAL;
4353 return BLUETOOTH_ERROR_NONE;
4356 int _bt_gatt_get_all_characteristic(bluetooth_gatt_client_svc_prop_info_t *svc)
4358 BT_CHECK_PARAMETER(svc, return);
4359 struct gatt_server_info_t *conn_info = NULL;
4360 invocation_info_t *req_info = NULL;
4361 bluetooth_gatt_client_svc_prop_info_t *prop;
4362 oal_gatt_srvc_id_t srvc_id;
4363 int ret = OAL_STATUS_SUCCESS;
4366 /* Check if any app is already browsing characteristics of the same service on the same remote GATT Server */
4367 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_GET_SERVICE_PROPERTIES);
4369 prop = (bluetooth_gatt_client_svc_prop_info_t*)req_info->user_data;
4370 if (prop && !memcmp(svc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t))
4371 && memcmp(prop->svc.uuid, svc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
4372 && prop->svc.instance_id == svc->svc.instance_id) {
4373 BT_INFO("Already Properties browsing for Primary Service ongoing for same remote GATT Server");
4374 /* Return and wait for events to be sent to all apps */
4375 return BLUETOOTH_ERROR_NONE;
4379 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4380 _bt_convert_addr_type_to_string(addr, svc->device_address.addr);
4382 /* Check if remote GATT Server is connected or not */
4383 conn_info = _bt_find_remote_gatt_server_info(addr);
4385 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4386 conn_info->addr, conn_info->connection_id);
4388 BT_ERR("GATT Server is not yet connected..");
4390 return BLUETOOTH_ERROR_NOT_CONNECTED;
4393 srvc_id.is_prmry = TRUE;
4394 srvc_id.id.inst_id = svc->svc.instance_id;
4395 memcpy(srvc_id.id.uuid.uuid, svc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4397 /* Search All Characteristic */
4398 ret = gattc_get_characteristic(conn_info->connection_id, &srvc_id, NULL);
4399 if (ret != OAL_STATUS_SUCCESS) {
4400 BT_ERR("ret: %d", ret);
4402 return BLUETOOTH_ERROR_INTERNAL;
4405 return BLUETOOTH_ERROR_NONE;
4408 int _bt_gatt_get_all_characteristic_properties(
4409 bluetooth_gatt_client_char_prop_info_t *chr)
4411 struct gatt_server_info_t *conn_info = NULL;
4412 invocation_info_t *req_info = NULL;
4413 bluetooth_gatt_client_char_prop_info_t *prop;
4414 oal_gatt_srvc_id_t srvc_id;
4415 oal_gatt_id_t char_id;
4416 int ret = OAL_STATUS_SUCCESS;
4419 BT_CHECK_PARAMETER(chr, return);
4421 /* Check if any app is already browsing descriptors of the same char of
4422 particular service on the same remote GATT Server */
4423 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_GET_CHARACTERISTIC_PROPERTIES);
4425 prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
4426 if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4427 && memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4428 && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4429 && memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4430 && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
4431 BT_INFO("Already Properties browsing for Characteristic ongoing for same remote GATT Server");
4432 /* Return and wait for events to be sent to all apps */
4433 return BLUETOOTH_ERROR_NONE;
4437 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4438 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4440 /* Check if remote GATT Server is connected or not */
4441 conn_info = _bt_find_remote_gatt_server_info(addr);
4443 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4444 conn_info->addr, conn_info->connection_id);
4446 BT_ERR("GATT Server is not yet connected..");
4448 return BLUETOOTH_ERROR_NOT_CONNECTED;
4451 srvc_id.is_prmry = TRUE;
4452 srvc_id.id.inst_id = chr->svc.instance_id;
4453 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4455 char_id.inst_id = chr->characteristic.instance_id;
4456 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4458 /* Search All Descriptors */
4459 ret = gattc_get_descriptor(conn_info->connection_id, &srvc_id, &char_id, NULL);
4460 if (ret != OAL_STATUS_SUCCESS) {
4461 BT_ERR("ret: %d", ret);
4463 return BLUETOOTH_ERROR_INTERNAL;
4466 return BLUETOOTH_ERROR_NONE;
4469 int _bt_gatt_read_characteristic_value(
4470 bluetooth_gatt_client_char_prop_info_t *chr)
4472 struct gatt_server_info_t *conn_info = NULL;
4473 invocation_info_t *req_info = NULL;
4474 bluetooth_gatt_client_char_prop_info_t *prop;
4475 oal_gatt_srvc_id_t srvc_id;
4476 oal_gatt_id_t char_id;
4477 int ret = OAL_STATUS_SUCCESS;
4480 BT_CHECK_PARAMETER(chr, return);
4482 /* Check if any app is already Reading characteristic of the same char of
4483 particular service on the same remote GATT Server */
4484 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_READ_CHARACTERISTIC);
4486 prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
4487 if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4488 && memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4489 && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4490 && memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4491 && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
4492 BT_INFO("Already Characteristic value Read operation in progress for same remote GATT Server");
4493 /* Return and wait for events to be sent to all apps */
4494 return BLUETOOTH_ERROR_NONE;
4498 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4499 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4501 /* Check if remote GATT Server is connected or not */
4502 conn_info = _bt_find_remote_gatt_server_info(addr);
4504 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4505 conn_info->addr, conn_info->connection_id);
4507 BT_ERR("GATT Server is not yet connected..");
4509 return BLUETOOTH_ERROR_NOT_CONNECTED;
4512 srvc_id.is_prmry = TRUE;
4513 srvc_id.id.inst_id = chr->svc.instance_id;
4514 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4516 char_id.inst_id = chr->characteristic.instance_id;
4517 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4519 /* Search All Descriptors */
4520 ret = gattc_read_characteristic(conn_info->connection_id, &srvc_id, &char_id, OAL_GATT_AUTH_REQ_NONE);
4521 if (ret != OAL_STATUS_SUCCESS) {
4522 BT_ERR("ret: %d", ret);
4524 return BLUETOOTH_ERROR_INTERNAL;
4527 return BLUETOOTH_ERROR_NONE;
4530 int _bt_gatt_read_descriptor_value(
4531 bluetooth_gatt_client_desc_prop_info_t *desc)
4533 struct gatt_server_info_t *conn_info = NULL;
4534 invocation_info_t *req_info = NULL;
4535 bluetooth_gatt_client_desc_prop_info_t *prop;
4536 oal_gatt_srvc_id_t srvc_id;
4537 oal_gatt_id_t char_id;
4538 oal_gatt_id_t desc_id;
4539 int ret = OAL_STATUS_SUCCESS;
4542 BT_CHECK_PARAMETER(desc, return);
4544 /* Check if any app is already Reading descriptors of the same char of
4545 particular service on the same remote GATT Server */
4546 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_READ_DESCRIPTOR_VALUE);
4548 prop = (bluetooth_gatt_client_desc_prop_info_t*)req_info->user_data;
4549 if (prop && !memcmp(desc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4550 && memcmp(desc->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4551 && desc->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4552 && memcmp(desc->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4553 && desc->characteristic.instance_id == prop->characteristic.instance_id /* Characteristic Instance ID matched */
4554 && memcmp(desc->descriptor.uuid, prop->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Descriptor UUID matched */
4555 && desc->descriptor.instance_id == prop->descriptor.instance_id) { /* Descriptor Instance ID matched */
4556 BT_INFO("Already Descriptor value Read operation in progress for same remote GATT Server");
4557 /* Return and wait for events to be sent to all apps */
4558 return BLUETOOTH_ERROR_NONE;
4562 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4563 _bt_convert_addr_type_to_string(addr, desc->device_address.addr);
4565 /* Check if remote GATT Server is connected or not */
4566 conn_info = _bt_find_remote_gatt_server_info(addr);
4568 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4569 conn_info->addr, conn_info->connection_id);
4571 BT_ERR("GATT Server is not yet connected..");
4573 return BLUETOOTH_ERROR_NOT_CONNECTED;
4576 srvc_id.is_prmry = TRUE;
4577 srvc_id.id.inst_id = desc->svc.instance_id;
4578 memcpy(srvc_id.id.uuid.uuid, desc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4580 char_id.inst_id = desc->characteristic.instance_id;
4581 memcpy(char_id.uuid.uuid, desc->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4583 desc_id.inst_id = desc->descriptor.instance_id;
4584 memcpy(desc_id.uuid.uuid, desc->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4586 /* Search All Descriptors */
4587 ret = gattc_read_descriptor(conn_info->connection_id, &srvc_id, &char_id,
4588 &desc_id, OAL_GATT_AUTH_REQ_NONE);
4589 if (ret != OAL_STATUS_SUCCESS) {
4590 BT_ERR("ret: %d", ret);
4592 return BLUETOOTH_ERROR_INTERNAL;
4595 return BLUETOOTH_ERROR_NONE;
4599 int _bt_gatt_acquire_notify(bluetooth_gatt_client_char_prop_info_t *chr, int *fd, int *mtu)
4601 struct gatt_server_info_t *conn_info = NULL;
4602 oal_gatt_srvc_id_t srvc_id;
4603 oal_gatt_id_t char_id;
4604 int ret = OAL_STATUS_SUCCESS;
4607 BT_CHECK_PARAMETER(chr, return);
4609 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4610 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4612 /* Check if remote GATT Server is connected or not */
4613 conn_info = _bt_find_remote_gatt_server_info(addr);
4615 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4616 conn_info->addr, conn_info->connection_id);
4618 BT_ERR("GATT Server is not yet connected..");
4620 return BLUETOOTH_ERROR_NOT_CONNECTED;
4623 srvc_id.is_prmry = TRUE;
4624 srvc_id.id.inst_id = chr->svc.instance_id;
4625 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4627 char_id.inst_id = chr->characteristic.instance_id;
4628 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4630 ret = gattc_acquire_notify(conn_info->connection_id, &srvc_id, &char_id, fd, mtu);
4632 if (ret != OAL_STATUS_SUCCESS) {
4633 BT_ERR("ret: %d", ret);
4635 return BLUETOOTH_ERROR_INTERNAL;
4637 BT_INFO("GATT characterstics FD [%d] mtu[%d]", *fd, *mtu);
4639 return BLUETOOTH_ERROR_NONE;
4643 int _bt_gatt_acquire_write(bluetooth_gatt_client_char_prop_info_t *chr, int *fd, int *mtu)
4646 struct gatt_server_info_t *conn_info = NULL;
4647 oal_gatt_srvc_id_t srvc_id;
4648 oal_gatt_id_t char_id;
4649 int ret = OAL_STATUS_SUCCESS;
4652 BT_CHECK_PARAMETER(chr, return);
4654 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4655 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4657 /* Check if remote GATT Server is connected or not */
4658 conn_info = _bt_find_remote_gatt_server_info(addr);
4660 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4661 conn_info->addr, conn_info->connection_id);
4663 BT_ERR("GATT Server is not yet connected..");
4665 return BLUETOOTH_ERROR_NOT_CONNECTED;
4668 srvc_id.is_prmry = TRUE;
4669 srvc_id.id.inst_id = chr->svc.instance_id;
4670 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4672 char_id.inst_id = chr->characteristic.instance_id;
4673 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4675 ret = gattc_acquire_write(conn_info->connection_id, &srvc_id, &char_id,
4676 OAL_GATT_AUTH_REQ_NONE, fd, mtu);
4677 if (ret != OAL_STATUS_SUCCESS) {
4678 BT_ERR("ret: %d", ret);
4680 return BLUETOOTH_ERROR_INTERNAL;
4682 BT_INFO("GATT characterstics FD [%d] mtu [%d]", *fd, *mtu);
4684 return BLUETOOTH_ERROR_NONE;
4689 /* Write Characteristic */
4690 int _bt_gatt_write_characteristic_value_by_type(
4691 bluetooth_gatt_client_char_prop_info_t *chr,
4692 bluetooth_gatt_att_data_t *data,
4693 bluetooth_gatt_write_type_e write_type)
4695 struct gatt_server_info_t *conn_info = NULL;
4696 invocation_info_t *req_info = NULL;
4697 bluetooth_gatt_client_char_prop_info_t *prop;
4698 oal_gatt_srvc_id_t srvc_id;
4699 oal_gatt_id_t char_id;
4700 int ret = OAL_STATUS_SUCCESS;
4704 BT_CHECK_PARAMETER(chr, return);
4705 BT_CHECK_PARAMETER(data, return);
4709 /* Check if any app is already writing same char of
4710 particular service on the same remote GATT Server */
4711 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE);
4713 prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
4714 if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4715 && memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4716 && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4717 && memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4718 && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
4719 BT_INFO("Already Characteristic Write Value operation in progress for same remote GATT Server");
4720 /* Return and wait for events to be sent to all apps */
4721 return BLUETOOTH_ERROR_NONE;
4725 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4726 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4728 /* Check if remote GATT Server is connected or not */
4729 conn_info = _bt_find_remote_gatt_server_info(addr);
4731 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4732 conn_info->addr, conn_info->connection_id);
4734 BT_ERR("GATT Server is not yet connected..");
4736 return BLUETOOTH_ERROR_NOT_CONNECTED;
4739 srvc_id.is_prmry = TRUE;
4740 srvc_id.id.inst_id = chr->svc.instance_id;
4741 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4743 char_id.inst_id = chr->characteristic.instance_id;
4744 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4746 /* Write CHar value */
4748 BT_INFO("Connection ID [%d] write type [%d] data length [%d]", conn_info->connection_id, write_type, data->length);
4749 for (k = 0; k < data->length; k++)
4750 BT_INFO("Data[%d] [0x%x]", k, data->data[k]);
4752 ret = gattc_write_characteristic(conn_info->connection_id,
4754 (oal_gatt_write_type_t)write_type, data->length,
4755 OAL_GATT_AUTH_REQ_NONE, (char *)(&data->data[0]));
4756 if (ret != OAL_STATUS_SUCCESS) {
4757 BT_ERR("ret: %d", ret);
4759 return BLUETOOTH_ERROR_INTERNAL;
4762 return BLUETOOTH_ERROR_NONE;
4765 /* Write Descriptor */
4766 int _bt_gatt_write_descriptor_value_by_type(
4767 bluetooth_gatt_client_desc_prop_info_t *desc,
4768 bluetooth_gatt_att_data_t *data,
4769 bluetooth_gatt_write_type_e write_type)
4771 struct gatt_server_info_t *conn_info = NULL;
4772 invocation_info_t *req_info = NULL;
4773 bluetooth_gatt_client_desc_prop_info_t *prop;
4774 oal_gatt_srvc_id_t srvc_id;
4775 oal_gatt_id_t char_id;
4776 oal_gatt_id_t desc_id;
4777 int ret = OAL_STATUS_SUCCESS;
4781 BT_CHECK_PARAMETER(desc, return);
4782 BT_CHECK_PARAMETER(data, return);
4786 /* Check if any app is already writing on same Descriptor of the same char of
4787 particular service on the same remote GATT Server */
4788 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_WRITE_DESCRIPTOR_VALUE);
4790 prop = (bluetooth_gatt_client_desc_prop_info_t*)req_info->user_data;
4791 if (prop && !memcmp(desc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4792 && memcmp(desc->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4793 && desc->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4794 && memcmp(desc->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4795 && desc->characteristic.instance_id == prop->characteristic.instance_id /* Characteristic Instance ID matched */
4796 && memcmp(desc->descriptor.uuid, prop->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Descriptor UUID matched */
4797 && desc->descriptor.instance_id == prop->descriptor.instance_id) { /* Descriptor Instance ID matched */
4798 BT_INFO("Already Descriptor value Write operation in progress for same remote GATT Server");
4799 /* Return and wait for events to be sent to all apps */
4800 return BLUETOOTH_ERROR_NONE;
4804 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4805 _bt_convert_addr_type_to_string(addr, desc->device_address.addr);
4807 /* Check if remote GATT Server is connected or not */
4808 conn_info = _bt_find_remote_gatt_server_info(addr);
4810 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4811 conn_info->addr, conn_info->connection_id);
4813 BT_ERR("GATT Server is not yet connected..");
4815 return BLUETOOTH_ERROR_NOT_CONNECTED;
4818 srvc_id.is_prmry = TRUE;
4819 srvc_id.id.inst_id = desc->svc.instance_id;
4820 memcpy(srvc_id.id.uuid.uuid, desc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4822 char_id.inst_id = desc->characteristic.instance_id;
4823 memcpy(char_id.uuid.uuid, desc->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4825 desc_id.inst_id = desc->descriptor.instance_id;
4826 memcpy(desc_id.uuid.uuid, desc->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4829 BT_INFO("Connection ID [%d] write type [%d] data length [%d]", conn_info->connection_id, write_type, data->length);
4830 for (k = 0; k < data->length; k++)
4831 BT_INFO("Data[%d] [0x%x]", k, data->data[k]);
4833 ret = gattc_write_descriptor(conn_info->connection_id,
4834 &srvc_id, &char_id, &desc_id,
4835 (oal_gatt_write_type_t)write_type, data->length,
4836 OAL_GATT_AUTH_REQ_NONE, (char *)(&data->data[0]));
4837 if (ret != OAL_STATUS_SUCCESS) {
4838 BT_ERR("ret: %d", ret);
4840 return BLUETOOTH_ERROR_INTERNAL;
4843 return BLUETOOTH_ERROR_NONE;
4846 int _bt_gatt_watch_characteristic(
4847 bluetooth_gatt_client_char_prop_info_t *chr,
4851 struct gatt_server_info_t *conn_info = NULL;
4852 oal_gatt_srvc_id_t srvc_id;
4853 oal_gatt_id_t char_id;
4854 int ret = OAL_STATUS_SUCCESS;
4857 BT_CHECK_PARAMETER(chr, return);
4859 BT_INFO("Client ID [%d] Is Notify [%d]", client_id, is_notify);
4861 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4862 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4864 /* Check if remote GATT Server is connected or not */
4865 conn_info = _bt_find_remote_gatt_server_info(addr);
4867 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4868 conn_info->addr, conn_info->connection_id);
4870 BT_ERR("GATT Server is not yet connected..");
4872 return BLUETOOTH_ERROR_NOT_CONNECTED;
4874 srvc_id.is_prmry = TRUE;
4875 srvc_id.id.inst_id = chr->svc.instance_id;
4876 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4878 char_id.inst_id = chr->characteristic.instance_id;
4879 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4881 /* Register or unregister Notification characteristic */
4883 ret = gattc_register_for_notification(conn_info->client_id,
4884 (bt_address_t*)&(chr->device_address),
4885 &srvc_id, &char_id);
4887 ret = gattc_deregister_for_notification(conn_info->client_id,
4888 (bt_address_t*)&(chr->device_address),
4889 &srvc_id, &char_id);
4891 BT_INFO("Result[%d]", ret);
4892 if (ret != OAL_STATUS_SUCCESS) {
4893 BT_ERR("ret: %d", ret);
4895 return BLUETOOTH_ERROR_INTERNAL;
4898 return BLUETOOTH_ERROR_NONE;
4902 int _bt_disconnect_le_device(bluetooth_device_address_t *address,
4905 struct gatt_server_info_t *conn_info = NULL;
4906 struct gatt_client_info_t *rem_client_conn_info = NULL;
4907 invocation_info_t *req_info = NULL;
4908 int ret = OAL_STATUS_SUCCESS;
4910 char *remote_address = NULL;
4912 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_DBG("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;