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);
1475 #ifdef TIZEN_GATT_CLIENT
1476 static void __bt_handle_gatt_server_connection_state(event_gatts_conn_t *event)
1478 int result = BLUETOOTH_ERROR_NONE;
1479 struct gatt_client_info_t *client_info = NULL;
1480 struct gatt_server_info_t *server_info = NULL;
1481 bluetooth_device_address_t dev_addr;
1482 GVariant *param = NULL;
1485 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1487 memcpy(dev_addr.addr, event->address.addr, 6);
1489 /* REPLY dbus Context to application which called BT_CONNECT_LE. There is status
1491 _bt_convert_addr_type_to_string(address,
1492 (unsigned char *)dev_addr.addr);
1494 if (event->status != OAL_STATUS_SUCCESS)
1495 result = BLUETOOTH_ERROR_INTERNAL;
1497 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
1498 address, BT_ADDRESS_STRING_SIZE);
1500 BT_INFO("GATT Server Connedted: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1501 address, event->server_inst, event->conn_id);
1504 /* Check if device is already in connected list */
1505 client_info = _bt_find_remote_gatt_client_info(address);
1508 BT_INFO("Conn Info absent: But no need to Send Local GATT Server Connected event to apps");
1510 param = g_variant_new("(is)", result, address);
1512 /* Send event to application */
1513 _bt_send_event(BT_DEVICE_EVENT,
1514 BLUETOOTH_EVENT_GATT_SERVER_CONNECTED, /* Local device is GATT server */
1517 /* Save client connection info */
1518 client_info = g_new0(struct gatt_client_info_t, 1);
1519 client_info->addr = g_strdup(address);
1520 BT_INFO("Added GATT client addr[%s]", client_info->addr);
1521 client_info->connection_id = event->conn_id;
1522 client_info->instance_id = event->server_inst;
1523 gatt_client_info_list = g_slist_append(gatt_client_info_list, client_info);
1524 BT_INFO("Total num of connected Remote GATT Clients [%d]", g_slist_length(gatt_client_info_list));
1526 /* Save server connection info */
1527 server_info = g_new0(struct gatt_server_info_t, 1);
1528 server_info->addr = g_strdup(address);
1529 server_info->client_id = -1;
1530 BT_INFO("Added GATT server addr[%s]", server_info->addr);
1531 server_info->connection_id = event->conn_id;
1532 gatt_server_info_list = g_slist_append(gatt_server_info_list, server_info);
1533 BT_INFO("Total num of connected Remote GATT Servers [%d]", g_slist_length(gatt_server_info_list));
1535 ret = gattc_add_connection_info((bt_address_t *)&dev_addr, event->conn_id, event->server_inst);
1536 if (ret != OAL_STATUS_SUCCESS) {
1537 BT_ERR("gattc register server instance failed");
1541 __bt_add_mtu_gatt_device(address);
1546 /* GATT Server Dis connected */
1547 static void __bt_handle_gatt_server_disconnection_state(event_gatts_conn_t *event)
1549 int result = BLUETOOTH_ERROR_NONE;
1550 struct gatt_client_info_t *client_info = NULL;
1551 struct gatt_server_info_t *server_info = NULL;
1552 bluetooth_device_address_t dev_addr;
1553 GVariant *param = NULL;
1554 char address[BT_ADDRESS_STRING_SIZE];
1556 memcpy(dev_addr.addr, event->address.addr, 6);
1558 /* REPLY dbus Context to application which called BT_DISCONNECT_LE. There is status
1560 _bt_convert_addr_type_to_string(address,
1561 (unsigned char *)dev_addr.addr);
1563 if (event->status != OAL_STATUS_SUCCESS)
1564 result = BLUETOOTH_ERROR_INTERNAL;
1566 if (NULL == _bt_get_request_info_data(BT_DISCONNECT_LE, address)) {
1567 if (NULL != _bt_get_request_info_data(BT_CONNECT_LE, address)) {
1568 result = BLUETOOTH_ERROR_INTERNAL;
1570 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
1571 address, BT_ADDRESS_STRING_SIZE);
1573 BT_ERR("Failed to connect Local GATT Server Remote Client addr[%s]", address);
1577 __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE,
1578 address, BT_ADDRESS_STRING_SIZE);
1581 BT_INFO("Local GATT Server DisConnected: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1582 address, event->server_inst, event->conn_id);
1583 /* Remove Connection info */
1584 client_info = _bt_find_remote_gatt_client_info(address);
1586 BT_INFO("No need to Send Local GATT Server Disconnected event to apps, just remove remote client info");
1588 param = g_variant_new("(is)", result, address);
1589 /* Send event to application */
1590 _bt_send_event(BT_DEVICE_EVENT,
1591 BLUETOOTH_EVENT_GATT_SERVER_DISCONNECTED, /* Local device is GATT server */
1594 /* Remove server info from list */
1595 server_info = _bt_find_remote_gatt_server_info(address);
1597 gatt_server_info_list = g_slist_remove(gatt_server_info_list, server_info);
1599 BT_INFO("Can not find conn info, already removed!");
1601 /* Remove client info from List */
1602 gatt_client_info_list = g_slist_remove(gatt_client_info_list, client_info);
1603 BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
1604 g_free(client_info->addr);
1605 g_free(client_info);
1608 __bt_remove_mtu_gatt_device(address);
1612 static void __bt_handle_gatt_server_connection_state(event_gatts_conn_t *event)
1614 int result = BLUETOOTH_ERROR_NONE;
1615 struct gatt_client_info_t *conn_info = NULL;
1616 bt_service_app_info_t *info = NULL;
1617 bluetooth_device_address_t dev_addr;
1618 GVariant *param = NULL;
1621 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1623 memcpy(dev_addr.addr, event->address.addr, 6);
1626 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1627 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1630 if (info->instance_id == event->server_inst) {
1631 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1632 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
1633 (bluetooth_device_address_t*)&dev_addr, sizeof(bluetooth_device_address_t));
1639 /* Send event to BT-API */
1640 _bt_convert_addr_type_to_string(address,
1641 (unsigned char *)dev_addr.addr);
1643 BT_INFO("GATT Server Connedted: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1644 address, event->server_inst, event->conn_id);
1647 /* Check if device is already in connected list */
1648 conn_info = _bt_find_remote_gatt_client_info(address);
1651 param = g_variant_new("(is)", result, address);
1652 /* Send event to application */
1653 _bt_send_event(BT_GATT_SERVER_EVENT,
1654 BLUETOOTH_EVENT_GATT_CONNECTED,
1657 /* Save Connection info */
1658 conn_info = g_new0(struct gatt_client_info_t, 1);
1659 conn_info->addr = g_strdup(address);
1660 BT_INFO("Added GATT client addr[%s]", conn_info->addr);
1661 conn_info->connection_id = event->conn_id;
1662 conn_info->instance_id = event->server_inst;
1663 gatt_client_info_list = g_slist_append(gatt_client_info_list, conn_info);
1664 BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
1670 /* GATT Server Dis connected */
1671 static void __bt_handle_gatt_server_disconnection_state(event_gatts_conn_t *event)
1673 int result = BLUETOOTH_ERROR_NONE;
1674 struct gatt_client_info_t *conn_info = NULL;
1675 bluetooth_device_address_t dev_addr;
1676 bt_service_app_info_t *info = NULL;
1677 GVariant *param = NULL;
1680 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1682 memcpy(dev_addr.addr, event->address.addr, 6);
1684 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1685 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1688 if (info->instance_id == event->server_inst) {
1689 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1690 __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE,
1691 (bluetooth_device_address_t*)&dev_addr, sizeof(bluetooth_device_address_t));
1697 /* Send event to BT-API */
1698 _bt_convert_addr_type_to_string(address,
1699 (unsigned char *)dev_addr.addr);
1701 BT_INFO("GATT Server DisConnected: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1702 address, event->server_inst, event->conn_id);
1704 param = g_variant_new("(is)", result, address);
1705 /* Send event to application */
1706 _bt_send_event(BT_GATT_SERVER_EVENT,
1707 BLUETOOTH_EVENT_GATT_DISCONNECTED,
1710 /* Remove Connection info */
1711 conn_info = _bt_find_remote_gatt_client_info(address);
1713 BT_INFO("Remove GATT client info from List..");
1714 /* Remove info from List */
1715 gatt_client_info_list = g_slist_remove(gatt_client_info_list, conn_info);
1716 BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
1717 g_free(conn_info->addr);
1726 static void __bt_handle_gatt_server_acquire_write_requested(event_gatts_srvc_acquire_attr_t *event)
1728 GVariant *param = NULL;
1729 int result = BLUETOOTH_ERROR_NONE;
1730 struct gatt_server_req_info *req_info = NULL;
1731 bluetooth_device_address_t dev_addr;
1732 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1734 BT_INFO("GATT Server ACQUIRE Write Req Connection ID: [%d]", event->attr_trans.conn_id);
1735 BT_INFO("GATT Server ACQUIRE Write Req Transaction ID:[%d]", event->attr_trans.trans_id);
1736 BT_INFO("GATT Server ACQUIRE Write Req Attribute Handle: [%d]", event->attr_trans.attr_handle);
1738 //address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1739 memcpy(dev_addr.addr, event->address.addr, 6);
1741 req_info = g_new0(struct gatt_server_req_info, 1);
1742 req_info->request_id = event->attr_trans.trans_id;
1743 req_info->attribute_handle = event->attr_trans.attr_handle;
1744 req_info->connection_id = event->attr_trans.conn_id;
1745 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_ACQUIRE_WRITE;
1746 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1748 _bt_convert_addr_type_to_string(address,
1749 (unsigned char *)dev_addr.addr);
1751 param = g_variant_new("(iiiiiis)", result,
1752 event->attr_trans.conn_id,
1753 event->attr_trans.trans_id,
1754 event->attr_trans.attr_handle,
1755 event->mtu, event->attr_trans.offset, address);
1756 BT_INFO("GATT Server ACQUIRE Write Req Attribute remote address : [%s]", address);
1758 _bt_send_event(BT_GATT_SERVER_EVENT,
1759 BLUETOOTH_EVENT_GATT_SERVER_ACQUIRE_WRITE,
1764 static void __bt_handle_gatt_server_acquire_notify_requested(event_gatts_srvc_acquire_attr_t *event)
1766 GVariant *param = NULL;
1767 int result = BLUETOOTH_ERROR_NONE;
1768 struct gatt_server_req_info *req_info = NULL;
1769 bluetooth_device_address_t dev_addr;
1770 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1772 BT_INFO("GATT Server ACQUIRE Notify Req Connection ID: [%d]", event->attr_trans.conn_id);
1773 BT_INFO("GATT Server ACQUIRE Notify Req Transaction ID:[%d]", event->attr_trans.trans_id);
1774 BT_INFO("GATT Server ACQUIRE Notify Req Attribute Handle: [%d]", event->attr_trans.attr_handle);
1775 BT_INFO("GATT Server ACQUIRE notify Req address : [%s]", address);
1777 memcpy(dev_addr.addr, event->address.addr, 6);
1778 _bt_convert_addr_type_to_string(address,
1779 (unsigned char *)dev_addr.addr);
1780 BT_INFO("GATT Server ACQUIRE notify Req remote address : [%s]", address);
1782 req_info = g_new0(struct gatt_server_req_info, 1);
1783 req_info->request_id = event->attr_trans.trans_id;
1784 req_info->attribute_handle = event->attr_trans.attr_handle;
1785 req_info->connection_id = event->attr_trans.conn_id;
1786 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_ACQUIRE_NOTIFY;
1787 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1789 param = g_variant_new("(iiiiiis)", result,
1790 event->attr_trans.conn_id,
1791 event->attr_trans.trans_id,
1792 event->attr_trans.attr_handle,
1793 event->mtu, event->attr_trans.offset,
1796 BT_INFO("GATT Server ACQUIRE Notify Req Attribute : ");
1798 _bt_send_event(BT_GATT_SERVER_EVENT,
1799 BLUETOOTH_EVENT_GATT_SERVER_ACQUIRE_NOTIFY,
1803 static void __bt_handle_gatt_server_write_requested(event_gatts_srvc_write_attr_t *event)
1806 bluetooth_device_address_t dev_addr;
1807 GVariant *param = NULL;
1808 int result = BLUETOOTH_ERROR_NONE;
1810 gboolean is_prepare_write;
1812 GVariant *data = NULL;
1814 struct gatt_server_req_info *req_info = NULL;
1815 BT_INFO("GATT Server Write Requested");
1817 memcpy(dev_addr.addr, event->address.addr, 6);
1819 BT_INFO("GATT Server Write Req Connection ID: [%d]", event->attr_trans.conn_id);
1820 BT_INFO("GATT Server Write Req Transaction ID:[%d]", event->attr_trans.trans_id);
1821 BT_INFO("GATT Server Write Req Attribute Handle: [%d]", event->attr_trans.attr_handle);
1822 BT_INFO("GATT Server Write Req Attribute Offset: [%d]", event->attr_trans.offset);
1823 BT_INFO("GATT Server Write Req value length [%d]", event->length);
1824 BT_INFO("GATT Server Write Req needs response: [%d]", event->need_rsp);
1825 BT_INFO("GATT Server Write Req Is Prep: [%d]", event->is_prep);
1827 if (event->length <= 0) {
1828 BT_INFO("GATT Server write requested, but length of attr value is 0");
1832 need_resp = event->need_rsp;
1833 is_prepare_write = event->is_prep;
1835 write_val = g_memdup(&event->value[0], event->length);
1837 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1838 _bt_convert_addr_type_to_string(address,
1839 (unsigned char *)dev_addr.addr);
1841 BT_INFO("GATT Server Write Request from remote client [%s]", address);
1843 if (event->length > 0) {
1844 for (i = 0; i < event->length; i++)
1845 BT_DBG("Data[%d] = [0x%x]", i, event->value[i]);
1847 /* Save Write Request Info */
1848 req_info = g_new0(struct gatt_server_req_info, 1);
1849 req_info->request_id = event->attr_trans.trans_id;
1850 req_info->attribute_handle = event->attr_trans.attr_handle;
1851 req_info->connection_id = event->attr_trans.conn_id;
1852 req_info->addr = address;
1853 req_info->offset = event->attr_trans.offset;
1854 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_WRITE;
1855 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1857 data = g_variant_new_from_data(
1858 G_VARIANT_TYPE_BYTESTRING,
1863 param = g_variant_new("(iiiiiibbsn@ay)", result,
1864 event->attr_trans.conn_id,
1865 event->attr_trans.trans_id,
1866 event->attr_trans.attr_handle,
1867 event->attr_trans.offset,
1875 _bt_send_event(BT_GATT_SERVER_EVENT,
1876 BLUETOOTH_EVENT_GATT_SERVER_VALUE_CHANGED,
1882 static void __bt_handle_gatt_server_read_requested(event_gatts_srvc_read_attr_t *event)
1884 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1885 bluetooth_device_address_t dev_addr;
1886 int result = BLUETOOTH_ERROR_NONE;
1887 struct gatt_server_req_info *req_info = NULL;
1888 GVariant *param = NULL;
1890 BT_INFO("GATT Server Read Requested");
1892 memcpy(dev_addr.addr, event->address.addr, 6);
1894 BT_INFO("GATT Server-Client Connection ID: [%d]", event->attr_trans.conn_id);
1895 BT_INFO("GATT Server-Client Transaction ID: [%d]", event->attr_trans.trans_id);
1896 BT_INFO("GATT Server Attribute Handle: [%d]", event->attr_trans.attr_handle);
1897 BT_INFO("GATT Server Attribute Offset for read: [%d]", event->attr_trans.offset);
1898 BT_INFO("GATT Server Attribute is long: [%d]", event->is_long);
1900 is_long = event->is_long;
1902 /* Save Read Request Info */
1903 req_info = g_new0(struct gatt_server_req_info, 1);
1904 req_info->request_id = event->attr_trans.trans_id;
1905 req_info->attribute_handle = event->attr_trans.attr_handle;
1906 req_info->connection_id = event->attr_trans.conn_id;
1907 req_info->addr = address;
1908 req_info->offset = event->attr_trans.offset;
1909 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_READ;
1910 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1912 /* Send event to BT-API */
1913 _bt_convert_addr_type_to_string(address,
1914 (unsigned char *)dev_addr.addr);
1916 BT_INFO("GATT Server Read Request from remote client [%s]", address);
1918 param = g_variant_new("(iiiiibs)", result,
1919 event->attr_trans.conn_id,
1920 event->attr_trans.trans_id,
1921 event->attr_trans.attr_handle,
1922 event->attr_trans.offset,
1926 _bt_send_event(BT_GATT_SERVER_EVENT,
1927 BLUETOOTH_EVENT_GATT_SERVER_READ_REQUESTED,
1931 static void __bt_handle_gatt_server_indicate_confirmed(event_gatts_ind_cnfrm_t *event)
1933 bluetooth_device_address_t dev_addr;
1934 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1935 int cur_connected_clients;
1936 static int recvd = 0;
1937 gboolean completed = 0;
1938 GVariant *param = NULL;
1940 /* OAL event does provide error, so MW assumes event will never contain wrong data,
1941 incase of any issues, check with OAL */
1942 int result = BLUETOOTH_ERROR_NONE;
1944 memcpy(dev_addr.addr, event->address.addr, 6);
1945 _bt_convert_addr_type_to_string(address,
1946 (unsigned char *)dev_addr.addr);
1948 BT_INFO("Indication sent to GATT client [%s] conn_ ID [%d] transaction ID [%d] Att handle [%d]",
1949 address, event->conn_id, event->trans_id, event->attr_handle);
1952 cur_connected_clients = g_slist_length(gatt_client_info_list);
1953 BT_INFO("Number of connected clients during sending Indication [%d] & current connected count [%d]",
1954 num_indicate_clients, cur_connected_clients);
1957 if (recvd == num_indicate_clients) {
1958 BT_INFO("Gatt indication confirm event for last GATT client.. [%s]", address);
1959 completed = 1; /* Last event */
1960 recvd = 0; /* Reset */
1961 num_indicate_clients = 0;
1964 param = g_variant_new("(isib)",
1970 /* Send event to BT-API */
1971 _bt_send_event(BT_GATT_SERVER_EVENT,
1972 BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_COMPLETED,
1975 BT_INFO("Received Indication confirm for client number [%d]", recvd);
1979 /* Tizen Platform Specific */
1980 static void __bt_handle_gatt_server_notification_changed(event_gatts_notif_t *event)
1982 bluetooth_device_address_t dev_addr;
1983 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1984 GVariant *param = NULL;
1987 /* OAL event does provide error, so MW assumes event will never contain wrong data,
1988 incase of any issues, check with OAL */
1989 int result = BLUETOOTH_ERROR_NONE;
1991 memcpy(dev_addr.addr, event->address.addr, 6);
1992 _bt_convert_addr_type_to_string(address,
1993 (unsigned char *)dev_addr.addr);
1995 BT_INFO("notification_changed [%s] conn_ ID [%d] transaction ID [%d] Att handle [%d] Notify[%d]",
1996 address, event->conn_id, event->trans_id, event->attr_handle, event->notify);
1998 /* Set Notifcation status */
1999 notify = event->notify;
2001 param = g_variant_new("(isib)",
2007 /* Send event to BT-API */
2008 _bt_send_event(BT_GATT_SERVER_EVENT,
2009 BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_STATE_CHANGED,
2015 static void __bt_handle_gatt_mtu_changed_event(event_gatts_mtu_changed_t *event)
2017 int result = BLUETOOTH_ERROR_NONE;
2018 struct gatt_client_info_t *conn_info = NULL;
2019 GVariant *param = NULL;
2022 conn_info = __bt_find_remote_gatt_client_info_from_conn_id(event->conn_id);
2023 if (conn_info == NULL) {
2024 BT_ERR("Cant find connection Information");
2027 BT_INFO("Got connection Info GATT client [%s] MTU Size [%d]",
2028 conn_info->addr, event->mtu_size);
2030 __bt_update_mtu_gatt_device(conn_info->addr, event->mtu_size);
2032 param = g_variant_new("(isqy)",
2038 /* Send event to BT-API */
2039 _bt_send_event(BT_GATT_SERVER_EVENT,
2040 BLUETOOTH_EVENT_GATT_ATT_MTU_CHANGED,
2044 static void __bt_gatt_event_handler(int event_type, gpointer event_data)
2046 BT_INFO("OAL event = 0x%x, \n", event_type);
2047 switch (event_type) {
2048 case OAL_EVENT_BLE_SERVER_INSTANCE_INITIALISED: {
2049 BT_INFO("OAL Event: Server Instance Registered");
2050 /* GATT Server Registered event is handled in MAIN thread context */
2051 __bt_handle_server_instance_registered((event_gatts_register_t *)event_data);
2054 case OAL_EVENT_GATTS_SERVICE_ADDED: {
2055 BT_INFO("OAL Event: GATT Service added");
2056 __bt_handle_gatt_server_service_added((event_gatts_srvc_prm_t *)event_data);
2059 case OAL_EVENT_GATTS_CHARACTERISTIC_ADDED: {
2060 BT_INFO("OAL Event: GATT characteristic added");
2061 __bt_handle_gatt_server_characteristic_added((event_gatts_srvc_charctr_t *)event_data);
2064 case OAL_EVENT_GATTS_DESCRIPTOR_ADDED: {
2065 BT_INFO("OAL Event: GATT descriptor added");
2066 __bt_handle_gatt_server_descriptor_added((event_gatts_srvc_descr_t *)event_data);
2069 case OAL_EVENT_GATTS_SERVICE_STARTED: {
2070 BT_INFO("OAL Event: GATT Service started");
2071 __bt_handle_gatt_server_service_started((event_gatts_srvc_t *)event_data);
2074 case OAL_EVENT_GATTS_SERVICE_STOPED: {
2075 BT_INFO("OAL Event: GATT Service stopped");
2076 __bt_handle_gatt_server_service_stopped((event_gatts_srvc_t *)event_data);
2079 case OAL_EVENT_GATTS_SERVICE_DELETED: {
2080 BT_INFO("OAL Event: GATT Service deleted");
2081 __bt_handle_gatt_server_service_deleted((event_gatts_srvc_t *) event_data);
2084 case OAL_EVENT_GATTS_CONNECTION_COMPLETED: {
2085 BT_INFO("OAL Event: GATT Server Connected");
2086 __bt_handle_gatt_server_connection_state((event_gatts_conn_t *)event_data);
2089 case OAL_EVENT_GATTS_DISCONNECTION_COMPLETED: {
2090 BT_INFO("OAL Event: GATT Server Disconnected");
2091 __bt_handle_gatt_server_disconnection_state((event_gatts_conn_t *)event_data);
2094 case OAL_EVENT_GATTS_REQUEST_READ: {
2095 BT_INFO("OAL Event: GATT Server Read Request");
2096 __bt_handle_gatt_server_read_requested((event_gatts_srvc_read_attr_t *)event_data);
2099 case OAL_EVENT_GATTS_REQUEST_WRITE: {
2100 BT_INFO("OAL Event: GATT Server Write Request");
2101 __bt_handle_gatt_server_write_requested((event_gatts_srvc_write_attr_t *)event_data);
2104 case OAL_EVENT_GATTS_REQUEST_ACQUIRE_WRITE: {
2105 BT_INFO("OAL Event: GATT Server Acquire Write Request");
2106 __bt_handle_gatt_server_acquire_write_requested((event_gatts_srvc_acquire_attr_t*)event_data);
2109 case OAL_EVENT_GATTS_REQUEST_ACQUIRE_NOTIFY: {
2110 BT_INFO("OAL Event: GATT ServerAcquire Notify Request");
2111 __bt_handle_gatt_server_acquire_notify_requested((event_gatts_srvc_acquire_attr_t*)event_data);
2114 case OAL_EVENT_GATTS_IND_CONFIRM: {
2115 BT_INFO("OAL Event: GATT Server Indication confirmed");
2116 __bt_handle_gatt_server_indicate_confirmed((event_gatts_ind_cnfrm_t *)event_data);
2119 case OAL_EVENT_GATTS_NOTIFICATION: { /* Tizen Platform Specific */
2120 BT_INFO("OAL Event: GATT Server DisConnected");
2121 __bt_handle_gatt_server_notification_changed((event_gatts_notif_t *)event_data);
2124 case OAL_EVENT_GATTS_MTU_CHANGED: {
2125 BT_INFO("OAL Event: GATT Server MTU changed event callback");
2126 __bt_handle_gatt_mtu_changed_event((event_gatts_mtu_changed_t *)event_data);
2129 #ifdef TIZEN_GATT_CLIENT
2130 case OAL_EVENT_GATTC_REGISTRATION: {
2131 BT_INFO("OAL Event: GATT Client instance Registered");
2132 __bt_handle_client_instance_registered((event_gattc_register_t *) event_data);
2135 case OAL_EVENT_GATTC_CONNECTION_COMPLETED: {
2136 BT_INFO("OAL Event: GATT Client Connected");
2137 __bt_handle_client_connected((event_gattc_conn_t *) event_data);
2140 case OAL_EVENT_GATTC_DISCONNECTION_COMPLETED: {
2141 BT_INFO("OAL Event: GATT Client DisConnected");
2142 __bt_handle_client_disconnected((event_gattc_conn_t *) event_data);
2145 case OAL_EVENT_GATTC_SERVICE_SEARCH_RESULT: {
2146 BT_INFO("OAL Event: GATT Client Service Search Result");
2147 __bt_handle_client_service_search_result((event_gattc_service_result_t *) event_data);
2150 case OAL_EVENT_GATTC_SERVICE_SEARCH_DONE: {
2151 BT_INFO("OAL Event: GATT Client Service Completed");
2152 __bt_handle_client_service_search_completed((event_gattc_conn_status_t *) event_data);
2155 case OAL_EVENT_GATTC_CHARAC_SERACH_RESULT: {
2156 BT_INFO("OAL Event: GATT Client Characteristic Search Result");
2157 __bt_handle_client_characteristic_search_result((event_gattc_characteristic_result_t *) event_data);
2160 case OAL_EVENT_GATTC_DESC_SERACH_RESULT: {
2161 BT_INFO("OAL Event: GATT Client Descriptor Search Result");
2162 __bt_handle_client_descriptor_search_result((event_gattc_descriptor_result_t *) event_data);
2165 case OAL_EVENT_GATTC_READ_CHARAC: {
2166 BT_INFO("OAL Event: GATT Client Characteristic Read Data");
2167 __bt_handle_client_characteristic_read_data((event_gattc_read_data *) event_data);
2170 case OAL_EVENT_GATTC_READ_DESCR: {
2171 BT_INFO("OAL Event: GATT Client Descriptor Read Data");
2172 __bt_handle_client_descriptor_read_data((event_gattc_read_data *) event_data);
2175 case OAL_EVENT_GATTC_WRITE_CHARAC: {
2176 BT_INFO("OAL Event: GATT Client Characteristic Write Data");
2177 __bt_handle_client_characteristic_write_data((event_gattc_write_data *) event_data);
2180 case OAL_EVENT_GATTC_WRITE_DESCR: {
2181 BT_INFO("OAL Event: GATT Client Descriptor Write Data");
2182 __bt_handle_client_descriptor_write_data((event_gattc_write_data *) event_data);
2185 case OAL_EVENT_DEVICE_LE_DISCONNECTED: {
2186 BT_INFO("OAL Event: LE device disconnected");
2187 __bt_hanlde_le_device_disconnection((event_dev_conn_status_t *)event_data);
2190 case OAL_EVENT_GATTC_NOTIFICATION_REGISTERED: {
2191 BT_INFO("OAL Event: GATT Client Notification Registered");
2192 __bt_handle_client_notification_registered((event_gattc_regdereg_notify_t *) event_data, TRUE);
2195 case OAL_EVENT_GATTC_NOTIFICATION_DEREGISTERED: {
2196 BT_INFO("OAL Event: GATT Client Notification Registered");
2197 __bt_handle_client_notification_registered((event_gattc_regdereg_notify_t *) event_data, FALSE);
2200 case OAL_EVENT_GATTC_NOTIFY_DATA: {
2201 BT_INFO("OAL Event: GATT Client Notification Data");
2202 __bt_handle_client_notification_data((event_gattc_notify_data *) event_data);
2205 case OAL_EVENT_GATTC_SERVICE_CHANGED_IND: {
2206 BT_INFO("OAL Event: GATT Client service changed indication");
2207 __bt_handle_client_service_changed_ind((event_gattc_service_changed_data *)event_data);
2210 case OAL_EVENT_GATTC_MTU_EXCHANGE_COMPLETED: {
2211 BT_INFO("OAL Event: GATT Client MTU Exchange Complete");
2212 __bt_handle_client_mtu_exchange_completed((event_gattc_mtu_configured_t *) event_data);
2221 int _bt_gatt_server_add_service(char *sender, int service_type,
2222 int num_handles, char *svc_uuid, int instance_id)
2224 BT_CHECK_PARAMETER(svc_uuid, return);
2225 BT_CHECK_PARAMETER(sender, return);
2226 int ret = OAL_STATUS_SUCCESS;
2228 oal_gatt_srvc_id_t svc_data;
2230 svc_data.is_prmry = service_type;
2231 svc_data.id.inst_id = instance_id;
2233 BT_INFO("Service UUID [%s] Num handles [%d] Instance ID [%d]", svc_uuid, num_handles, instance_id);
2234 _bt_string_to_uuid(svc_uuid, (service_uuid_t*)&svc_data.id.uuid);
2236 ret = gatts_add_service(instance_id, &svc_data, num_handles);
2237 if (ret != OAL_STATUS_SUCCESS) {
2238 BT_ERR("ret: %d", ret);
2239 return BLUETOOTH_ERROR_INTERNAL;
2242 return BLUETOOTH_ERROR_NONE;
2246 int _bt_gatt_server_add_included_service(char *sender, int instance_id,
2247 int service_handle, int included_svc_handle)
2249 BT_CHECK_PARAMETER(sender, return);
2250 int ret = OAL_STATUS_SUCCESS;
2252 ret = gatts_add_included_services(instance_id, service_handle, included_svc_handle);
2253 if (ret != OAL_STATUS_SUCCESS) {
2254 BT_ERR("ret: %d", ret);
2255 return BLUETOOTH_ERROR_INTERNAL;
2257 return BLUETOOTH_ERROR_NONE;
2260 int _bt_gatt_server_add_characteristic(char *sender, char *char_uuid,
2261 bluetooth_gatt_server_attribute_params_t *param)
2263 BT_CHECK_PARAMETER(char_uuid, return);
2264 BT_CHECK_PARAMETER(sender, return);
2265 BT_CHECK_PARAMETER(param, return);
2266 int ret = OAL_STATUS_SUCCESS;
2268 oal_uuid_t uuid = {{0} };
2270 BT_INFO("Char UUID [%s] Instance ID [%d]", char_uuid, param->instance_id);
2271 _bt_string_to_uuid(char_uuid, (service_uuid_t*)&uuid);
2273 BT_INFO("Char permission From API [0x%x]", param->permissions);
2275 ret = gatts_add_characteristics(param->instance_id, param->service_handle, &uuid,
2276 param->properties, (int)param->permissions);
2277 if (ret != OAL_STATUS_SUCCESS) {
2278 BT_ERR("ret: %d", ret);
2279 return BLUETOOTH_ERROR_INTERNAL;
2281 return BLUETOOTH_ERROR_NONE;
2284 int _bt_gatt_server_add_descriptor(char *sender, char *desc_uuid,
2285 bt_gatt_permission_t *param, int service_handle, int instance_id)
2287 BT_CHECK_PARAMETER(desc_uuid, return);
2288 BT_CHECK_PARAMETER(sender, return);
2289 BT_CHECK_PARAMETER(param, return);
2290 int ret = OAL_STATUS_SUCCESS;
2292 oal_uuid_t uuid = {{0} };
2294 BT_INFO("Descriptor UUID [%s] Instance ID [%d] Service handle [%d]",
2295 desc_uuid, service_handle, instance_id);
2297 _bt_string_to_uuid(desc_uuid, (service_uuid_t*)&uuid);
2299 BT_INFO("Descriptor permission From API [0x%x]", *param);
2300 ret = gatts_add_descriptor(instance_id, service_handle, &uuid, (int)*param);
2302 if (ret != OAL_STATUS_SUCCESS) {
2303 BT_ERR("ret: %d", ret);
2304 return BLUETOOTH_ERROR_INTERNAL;
2306 return BLUETOOTH_ERROR_NONE;
2309 int _bt_gatt_server_start_service(char *sender, int service_handle, int instance_id)
2311 BT_CHECK_PARAMETER(sender, return);
2312 int ret = OAL_STATUS_SUCCESS;
2314 ret = gatts_start_service(instance_id, service_handle, BT_GATT_TRANSPORT_LE);
2315 if (ret != OAL_STATUS_SUCCESS) {
2316 BT_ERR("ret: %d", ret);
2317 return BLUETOOTH_ERROR_INTERNAL;
2319 return BLUETOOTH_ERROR_NONE;
2322 int _bt_gatt_server_stop_service(char *sender, int service_handle, int instance_id)
2324 BT_CHECK_PARAMETER(sender, return);
2325 int ret = OAL_STATUS_SUCCESS;
2327 ret = gatts_stop_service(instance_id, service_handle);
2328 if (ret != OAL_STATUS_SUCCESS) {
2329 BT_ERR("ret: %d", ret);
2330 return BLUETOOTH_ERROR_INTERNAL;
2332 return BLUETOOTH_ERROR_NONE;
2335 int _bt_gatt_server_delete_service(char *sender, int service_handle, int instance_id)
2337 BT_CHECK_PARAMETER(sender, return);
2338 int ret = OAL_STATUS_SUCCESS;
2342 ret = gatts_delete_service(instance_id, service_handle);
2343 if (ret != OAL_STATUS_SUCCESS) {
2344 BT_ERR("ret: %d", ret);
2345 return BLUETOOTH_ERROR_INTERNAL;
2348 /* Remove the Service Handle */
2349 for (l = numapps[instance_id].service_handles; l != NULL;) {
2351 l = g_slist_next(l);
2352 if (handle && *handle == service_handle) {
2353 BT_INFO("Remove Service handle [%d]", *handle);
2354 numapps[instance_id].service_handles = g_slist_remove(numapps[instance_id].service_handles, handle);
2360 return BLUETOOTH_ERROR_NONE;
2363 int _bt_gatt_server_send_response(char *sender, bluetooth_gatt_att_data_t *data,
2364 bluetooth_gatt_server_response_params_t *param)
2366 BT_CHECK_PARAMETER(sender, return);
2367 BT_CHECK_PARAMETER(data, return);
2368 BT_CHECK_PARAMETER(param, return);
2369 struct gatt_server_req_info *req_info = NULL;
2370 int ret = OAL_STATUS_SUCCESS;
2372 oal_gatt_response_t response;
2374 BT_INFO("GATT Server Response: Req Type [%d] req_id [%d] status [%d] auth_req [%d] offset[%d] data len[%d]",
2375 param->req_type, param->request_id,
2376 param->response_status, param->auth_req,
2377 data->offset, data->length);
2379 /* Search for matching Request in List */
2380 req_info = __bt_gatt_server_find_request_info(param->request_id, param->req_type);
2382 BT_ERR("GATT Server Req Info not found for current response..return Error");
2383 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
2386 memset(&response, 0x00, sizeof(oal_gatt_response_t));
2388 response.handle = req_info->attribute_handle;
2389 response.attr_value.auth_req = param->auth_req;
2390 response.attr_value.handle = req_info->attribute_handle;
2391 response.attr_value.offset = data->offset;
2392 response.attr_value.len = data->length;
2393 memcpy(&response.attr_value.value, &data->data, data->length);
2396 ret = gatts_send_response(req_info->connection_id, param->request_id,
2397 param->response_status, &response);
2399 if (ret != OAL_STATUS_SUCCESS) {
2400 BT_ERR("ret: %d", ret);
2401 return BLUETOOTH_ERROR_INTERNAL;
2404 BT_INFO("GATT Server Response successfully sent");
2405 /* Remove GATT server request from list */
2406 gatt_server_requests = g_slist_remove(gatt_server_requests, req_info);
2407 g_free(req_info->addr);
2409 return BLUETOOTH_ERROR_NONE;
2412 int _bt_gatt_server_acquire_send_response(char *sender, bluetooth_gatt_server_acquire_response_params_t *param , void *fd_list)
2414 BT_CHECK_PARAMETER(sender, return);
2415 BT_CHECK_PARAMETER(param, return);
2416 struct gatt_server_req_info *req_info = NULL;
2417 int ret = OAL_STATUS_SUCCESS;
2420 BT_INFO("GATT acquire Server Response: Req Type [%d] req_id [%d] fd [%d] mtu[%d]",
2421 param->req_type, param->request_id,
2425 /* Search for matching Request in List */
2426 req_info = __bt_gatt_server_find_request_info(param->request_id, param->req_type);
2428 BT_ERR("GATT acquire Server Req Info not found for current response..return Error");
2429 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
2432 ret = gatt_send_response_acquire(req_info->connection_id, param->request_id, 0, param->fd, param->mtu, fd_list);
2434 if (ret != OAL_STATUS_SUCCESS) {
2435 BT_ERR("ret: %d", ret);
2436 return BLUETOOTH_ERROR_INTERNAL;
2439 BT_INFO("GATT acquire Server Response successfully sent");
2440 /* Remove GATT server request from list */
2441 gatt_server_requests = g_slist_remove(gatt_server_requests, req_info);
2442 g_free(req_info->addr);
2444 return BLUETOOTH_ERROR_NONE;
2449 int _bt_gatt_server_send_indication(char *sender, bluetooth_device_address_t *dev_addr,
2450 bluetooth_gatt_att_data_t *data,
2451 bluetooth_gatt_server_indication_params_t *param)
2453 BT_CHECK_PARAMETER(sender, return);
2454 BT_CHECK_PARAMETER(data, return);
2455 BT_CHECK_PARAMETER(param, return);
2457 gboolean all_send = FALSE;
2458 int ret = OAL_STATUS_SUCCESS;
2459 struct gatt_client_info_t *conn;
2461 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2462 _bt_convert_addr_type_to_string(address, dev_addr->addr);
2464 if (memcmp(dev_addr->addr, BDADDR_ANY, 6) == 0) {
2465 BT_INFO("GATT Server: Send Indication to all connected GATT clients..");
2468 BT_INFO("GATT Server: Send Indication to connected GATT client addr [%s]", address);
2471 /* Attempt to send Notification/Indication to all Connected GATT clients */
2473 ret = __bt_gatt_send_indication_to_all_connected_clients(data, param);
2474 if (ret != OAL_STATUS_SUCCESS) {
2475 BT_ERR("ret: %d", ret);
2477 return BLUETOOTH_ERROR_INTERNAL;
2481 conn = _bt_find_remote_gatt_client_info(address);
2483 ret = gatts_send_indication(param->instance_id, param->atrribute_handle,
2484 conn->connection_id, data->length,
2485 param->need_confirmation, (char *)(&data->data[0]));
2487 if (ret != OAL_STATUS_SUCCESS) {
2488 BT_ERR("ret: %d", ret);
2489 BT_INFO("Indication failed to send to Remote GATT Client [%s]", address);
2491 return BLUETOOTH_ERROR_INTERNAL;
2493 BT_INFO("Indication sent to Remote GATT Client [%s] wait for Notification completed event from OAL", address);
2495 num_indicate_clients = 1;
2496 return BLUETOOTH_ERROR_NONE;
2498 BT_ERR("Remote GATT client [%s] is not connected..Cant send Indication!!", address);
2500 return BLUETOOTH_ERROR_NOT_CONNECTED;
2503 return BLUETOOTH_ERROR_NONE;
2506 int _bt_gatt_server_update_attribute_value(char *sender, int instance_id,
2507 bluetooth_gatt_server_update_value_t *param)
2509 BT_CHECK_PARAMETER(sender, return);
2510 BT_CHECK_PARAMETER(param, return);
2511 int ret = OAL_STATUS_SUCCESS;
2513 oal_gatt_value_t value;
2514 BT_INFO("GATT Server Update value: Instance ID [%d] attr handle [%d] Value len [%d]",
2515 instance_id, param->attribute_handle, param->length);
2518 memset(&value, 0x00, sizeof(oal_gatt_value_t));
2520 value.handle = param->attribute_handle;
2521 value.len = param->length;
2522 memcpy(&value.value, ¶m->data.data, param->length);
2524 ret = gatts_update_att_value(instance_id, &value);
2526 if (ret != OAL_STATUS_SUCCESS) {
2527 BT_ERR("ret: %d", ret);
2528 return BLUETOOTH_ERROR_INTERNAL;
2531 BT_INFO("GATT Server Update Attribute Value successfully");
2532 return BLUETOOTH_ERROR_NONE;
2535 int _bt_request_att_mtu(bluetooth_device_address_t *device_address,
2538 struct gatt_server_info_t *conn_info = NULL;
2540 int ret = OAL_STATUS_SUCCESS;
2542 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
2543 _bt_convert_addr_type_to_string(addr, device_address->addr);
2545 /* Check if remote GATT Server is connected or not */
2546 conn_info = _bt_find_remote_gatt_server_info(addr);
2548 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
2549 conn_info->addr, conn_info->connection_id);
2551 BT_ERR("GATT Server is not yet connected..");
2553 return BLUETOOTH_ERROR_NOT_CONNECTED;
2556 ret = gattc_configure_mtu(conn_info->connection_id, mtu);
2557 if (ret != OAL_STATUS_SUCCESS) {
2558 BT_ERR("ret: %d", ret);
2560 return BLUETOOTH_ERROR_INTERNAL;
2564 return BLUETOOTH_ERROR_NONE;
2567 int _bt_get_att_mtu(bluetooth_device_address_t *address,
2570 BT_CHECK_PARAMETER(address, return);
2571 BT_CHECK_PARAMETER(mtu, return);
2572 struct gatt_client_info_t *conn_info = NULL;
2573 char addr[BT_ADDRESS_STRING_SIZE] = { 0 };
2574 int ret = OAL_STATUS_SUCCESS;
2577 _bt_convert_addr_type_to_string(addr, address->addr);
2579 BT_INFO("Get current MTU size for the remote client:DevAddress:[%s]", addr);
2581 conn_info = _bt_find_remote_gatt_client_info(addr);
2583 BT_INFO("GATT Client [%s] is connected, conn Id [%d] Instance ID [%d]",
2584 conn_info->addr, conn_info->connection_id, conn_info->instance_id);
2586 BT_ERR("GATT Client [%s] is not yet connected..", addr);
2587 return BLUETOOTH_ERROR_NOT_CONNECTED;
2590 ret = gatts_get_att_mtu(conn_info->connection_id, &stack_mtu);
2591 if (ret != OAL_STATUS_SUCCESS) {
2592 BT_ERR("ret: %d", ret);
2593 return BLUETOOTH_ERROR_INTERNAL;
2596 BT_INFO("ATT MTU received from OAL [%d]", stack_mtu);
2597 *mtu = (unsigned int)stack_mtu;
2600 BT_ERR("MTU value is zero, GATT Client [%s] is not yet connected..", addr);
2601 return BLUETOOTH_ERROR_NOT_CONNECTED;
2604 return BLUETOOTH_ERROR_NONE;
2607 #ifdef TIZEN_GATT_CLIENT
2608 /* GATT Client utility static functions */
2609 static bt_gatt_service_info_list_t *__bt_get_service_info_list(int conn_id)
2612 bt_gatt_service_info_list_t *info = NULL;
2614 for (l = list_gatt_info; l != NULL; l = g_slist_next(l)) {
2615 info = (bt_gatt_service_info_list_t *)l->data;
2619 if (info->conn_id == conn_id)
2626 static bt_gatt_service_info_t *__bt_find_matching_service(
2627 bt_gatt_service_info_list_t *svc_list, oal_gatt_srvc_id_t *svc)
2630 bt_gatt_service_info_t *info = NULL;
2632 for (l = svc_list->services; l != NULL; l = g_slist_next(l)) {
2633 info = (bt_gatt_service_info_t *)l->data;
2637 /* Match UUID and instance ID */
2638 if (!memcmp(&svc->id.uuid.uuid, &info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
2639 && (svc->id.inst_id == info->inst_id)) {
2646 static bt_gatt_char_info_t *__bt_find_matching_charc(
2647 bt_gatt_service_info_t *svc_info, oal_gatt_id_t *charc)
2650 bt_gatt_char_info_t *info = NULL;
2652 for (l = svc_info->chars; l != NULL; l = g_slist_next(l)) {
2653 info = (bt_gatt_char_info_t *)l->data;
2657 /* Match UUID and instance ID */
2658 if (!memcmp(&charc->uuid.uuid, &info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
2659 && (charc->inst_id == info->inst_id)) {
2666 static bt_gatt_descriptor_info_t *__bt_find_matching_desc(
2667 bt_gatt_char_info_t *char_info, oal_gatt_id_t *desc)
2670 bt_gatt_descriptor_info_t *info = NULL;
2672 for (l = char_info->descs; l != NULL; l = g_slist_next(l)) {
2673 info = (bt_gatt_descriptor_info_t *)l->data;
2677 /* Match UUID and instance ID */
2678 if (!memcmp(&desc->uuid, &info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
2679 && (desc->inst_id == info->inst_id)) {
2687 static struct gatt_server_info_t *__bt_find_remote_gatt_server_info_from_conn_id(int conn_id)
2690 struct gatt_server_info_t *info = NULL;
2692 for (l = gatt_server_info_list; l != NULL; l = g_slist_next(l)) {
2693 info = (struct gatt_server_info_t*)l->data;
2697 if (info->connection_id == conn_id) {
2698 BT_INFO("Remote GATT server found addr[%s]", info->addr);
2705 static bt_gatt_service_info_t* __bt_find_removed_service(bt_gatt_service_info_list_t *svc_list)
2708 bt_gatt_service_info_t *info = NULL;
2710 for (l = svc_list->services; l != NULL; l = g_slist_next(l)) {
2711 info = (bt_gatt_service_info_t*)l->data;
2715 /* Service is marked a removed */
2716 if (info->is_removed == 1)
2722 static void __bt_remove_service_info_from_list(bt_gatt_service_info_t *svc_info)
2728 bt_gatt_char_info_t *charc = NULL;
2729 bt_gatt_included_service_info_t *incl = NULL;
2730 bt_gatt_descriptor_info_t *desc = NULL;
2732 /* Remove all Characteristic and Descriptors within characteristic */
2733 for (l = svc_info->chars; l != NULL;) {
2734 charc = (bt_gatt_char_info_t*)l->data;
2735 l = g_slist_next(l); /* Incase if l is removed, saving next to l */
2740 /* Inside Characteristic */
2741 for (l1 = charc->descs; l1 != NULL;) {
2743 desc = (bt_gatt_descriptor_info_t*)l1->data;
2744 l1 = g_slist_next(l1);
2749 /* Remove Descriptor */
2750 charc->descs = g_slist_remove(charc->descs, desc);
2753 /* Remove Characteristic */
2754 svc_info->chars = g_slist_remove(svc_info->chars, charc);
2758 /* Remove all Included Services */
2759 for (l2 = svc_info->included_svcs; l2 != NULL;) {
2760 incl = (bt_gatt_included_service_info_t*)l2->data;
2761 l2 = g_slist_next(l2); /* Incase if l is removed, saving next to l */
2766 /* Remove included service */
2767 svc_info->included_svcs = g_slist_remove(svc_info->included_svcs, incl);
2774 static void __bt_build_service_browse_info(int conn_id,
2775 bt_services_browse_info_t* info)
2778 bt_gatt_service_info_list_t *svc_info_list;
2779 bt_gatt_service_info_t *svc_info;
2781 service_uuid_t uuid;
2782 struct gatt_server_info_t *conn_info = NULL;
2784 char uuid_string[BLUETOOTH_UUID_STRING_MAX];
2786 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
2791 _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
2793 svc_info_list = __bt_get_service_info_list(conn_id);
2797 info->count = g_slist_length(svc_info_list->services);
2798 BT_DBG("Total services present in the svc info list for this conn id [%d] is [%d]",
2799 conn_id, info->count);
2801 for (l = svc_info_list->services; l != NULL; l = g_slist_next(l)) {
2802 svc_info = (bt_gatt_service_info_t*)l->data;
2803 if (svc_info == NULL)
2806 memcpy(&uuid.uuid, &svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2807 _bt_uuid_to_string(&uuid, uuid_string);
2809 BT_INFO("[%d] %s [%s]", count, uuid_string, _bt_convert_uuid_to_string(uuid_string));
2811 /* Fill UUID of service */
2812 g_strlcpy(info->uuids[count], uuid_string,
2813 BLUETOOTH_UUID_STRING_MAX);
2815 /* Fill instance ID of service */
2816 info->inst_id[count] = svc_info->inst_id;
2818 /* Fill primary service or not info */
2819 info->primary[count] = svc_info->is_primary;
2821 /* Increment count of services browsed */
2826 static void __bt_build_char_browse_info(int conn_id,
2827 bt_gatt_service_info_t *svc_info,
2828 bt_char_browse_info_t* info)
2831 bt_gatt_char_info_t *char_info;
2832 service_uuid_t uuid;
2834 struct gatt_server_info_t *conn_info = NULL;
2836 char uuid_string[BLUETOOTH_UUID_STRING_MAX];
2838 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
2842 /* Fill default data, this will be required even in case of failure */
2843 _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
2844 memcpy(&info->svc_uuid, svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2845 info->svc_inst_id = svc_info->inst_id;
2847 if (!svc_info->chars) {
2848 BT_ERR("No Chars browsed for address [%s]", conn_info->addr);
2852 info->count = g_slist_length(svc_info->chars);
2854 for (l = svc_info->chars; l != NULL; l = g_slist_next(l)) {
2855 char_info = (bt_gatt_char_info_t*)l->data;
2856 if (char_info == NULL)
2859 memcpy(&uuid.uuid, &char_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2860 _bt_uuid_to_string(&uuid, uuid_string);
2862 /* Fill UUID of characteristic */
2863 g_strlcpy(info->uuids[count], uuid_string,
2864 BLUETOOTH_UUID_STRING_MAX);
2866 /* Fill instance ID of characteristic */
2867 info->inst_id[count] = char_info->inst_id;
2869 /* Fill property of characteristic */
2870 info->props[count] = char_info->props;
2872 /* Increment count of services browsed */
2875 BT_DBG("Total characteristics browsed [%d]", count);
2878 static void __bt_build_descriptor_browse_info(int conn_id,
2879 bt_gatt_service_info_t *svc_info,
2880 bt_gatt_char_info_t *char_info,
2881 bt_descriptor_browse_info_t* info)
2884 bt_gatt_descriptor_info_t *desc_info;
2886 service_uuid_t uuid;
2887 struct gatt_server_info_t *conn_info = NULL;
2889 char uuid_string[BLUETOOTH_UUID_STRING_MAX];
2891 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
2893 /* Fill default data, this will be required even in case of failure */
2894 _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
2895 memcpy(&info->svc_uuid, svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2896 info->svc_inst_id = svc_info->inst_id;
2897 memcpy(&info->char_uuid, char_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2898 info->char_inst_id = char_info->inst_id;
2900 /* Fill property of the parent characteristic of this descriptor */
2901 info->char_props_map = char_info->props;
2903 info->count = g_slist_length(char_info->descs);
2905 if (!char_info->descs) {
2906 BT_ERR("No Descriptors browsed for address [%s]", conn_info->addr);
2910 for (l = char_info->descs; l != NULL; l = g_slist_next(l)) {
2911 desc_info = (bt_gatt_descriptor_info_t*)l->data;
2912 if (desc_info == NULL)
2915 memcpy(&uuid.uuid, &desc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2916 _bt_uuid_to_string(&uuid, uuid_string);
2918 /* Fill UUID of Descriptor */
2919 g_strlcpy(info->uuids[count], uuid_string,
2920 BLUETOOTH_UUID_STRING_MAX);
2922 /* Fill instance ID of Descriptor */
2923 info->inst_id[count] = desc_info->inst_id;
2926 /* Increment count of Descriptor browsed */
2930 BT_INFO("Total descriptors browsed [%d]", count);
2933 static void __bt_free_service_info(bt_gatt_service_info_t *svc)
2935 GSList *ll, *lll, *llll;
2936 bt_gatt_char_info_t *chr = NULL;
2937 bt_gatt_descriptor_info_t *desc = NULL;
2938 bt_gatt_included_service_info_t *incl_svc = NULL;
2940 BT_DBG("Service info Is Prim[%d] Inst ID [%d]", svc->is_primary, svc->inst_id);
2941 /* Delete all chars and its descriptors */
2942 for (ll = svc->chars; ll != NULL; ) {
2943 chr = (bt_gatt_char_info_t *)ll->data;
2944 ll = g_slist_next(ll);
2948 for (lll = chr->descs; lll != NULL; ) {
2949 desc = (bt_gatt_descriptor_info_t *)lll->data;
2950 lll = g_slist_next(lll);
2953 chr->descs = g_slist_remove(chr->descs, desc);
2956 svc->chars = g_slist_remove(svc->chars, chr);
2960 /* Delete all included services */
2961 for (llll = svc->included_svcs; llll != NULL; ) {
2962 incl_svc = (bt_gatt_included_service_info_t *)llll->data;
2963 llll = g_slist_next(llll);
2964 if (incl_svc == NULL)
2966 svc->included_svcs = g_slist_remove(svc->included_svcs, incl_svc);
2971 static void __bt_cleanup_remote_services(struct gatt_server_info_t *conn_info)
2973 bt_gatt_service_info_list_t *svc_info_list = NULL;
2974 bt_gatt_service_info_t *svc = NULL;
2977 svc_info_list = __bt_get_service_info_list(conn_info->connection_id);
2978 if (!svc_info_list) {
2979 BT_INFO("Could not find Svc Info list for the connection ID [%d]",
2980 conn_info->connection_id);
2984 BT_INFO("Start Cleanup of all services. Num Services [%d]", g_slist_length(svc_info_list->services));
2985 for (l = svc_info_list->services; l != NULL; ) {
2986 svc = (bt_gatt_service_info_t *)l->data;
2987 l = g_slist_next(l);
2991 __bt_free_service_info(svc);
2992 svc_info_list->services = g_slist_remove(svc_info_list->services, svc);
2996 list_gatt_info = g_slist_remove(list_gatt_info, svc_info_list);
2997 g_free(svc_info_list);
3000 int _bt_register_gatt_client_instance(const char *sender,
3001 bluetooth_device_address_t *address)
3003 int ret = OAL_STATUS_SUCCESS;
3004 char *uuid_string = NULL;
3009 /* App should ensure that it should not send */
3010 BT_INFO("### Check on which instance GATT Client instance can be initialized....");
3011 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
3012 if (numapps[k].is_initialized == 1) {
3013 BT_INFO("Instance ID [%d] is already in use..Check next slot",
3014 numapps[k].instance_id);
3017 BT_INFO("Time to register GATT client instancer..UUID to be used is [%s] slot [%d]",
3018 uuid_list[slot-1], slot);
3024 BT_ERR("No Slot if free for GATT Client registration..");
3025 return BLUETOOTH_ERROR_REGISTRATION_FAILED;
3028 uuid_string = g_malloc0(BT_UUID_STRING_MAX);
3029 _bt_string_to_uuid(uuid_list[slot-1], (service_uuid_t*)&uuid);
3030 g_strlcpy(uuid_string, uuid_list[slot-1], BT_UUID_STRING_MAX);
3031 BT_INFO("Copied UUID string [%s]", uuid_string);
3033 /* Register GATT Client */
3034 ret = gattc_register(&uuid);
3035 if (ret != OAL_STATUS_SUCCESS) {
3036 BT_ERR("ret: %d", ret);
3037 g_free(uuid_string);
3038 return BLUETOOTH_ERROR_INTERNAL;
3041 BT_INFO("GATT Client registration call successfully accepted by OAL..wait for Instance Initialized event from OAL..");
3043 /* Return & wait for GATT Client Instance Initialization event */
3044 memset(numapps[slot].sender, 0x00, sizeof(numapps[slot].sender));
3045 memset(numapps[slot].uuid, 0x00, sizeof(numapps[slot].uuid));
3047 g_strlcpy(numapps[slot].sender, sender, sizeof(numapps[slot].sender));
3048 g_strlcpy(numapps[slot].uuid, uuid_string, sizeof(numapps[slot].uuid));
3050 /* Address is saved here. When event comes, sender + address are matched for replying pending
3051 request. It is impossible for same sender to have requests with two same addresses */
3052 memcpy(&numapps[slot].address.addr, address->addr, sizeof(bluetooth_device_address_t));
3054 BT_INFO("Slot [%d] occupied", slot);
3055 numapps[slot].is_initialized = TRUE; /* Set initialization to true here itself */
3057 g_free(uuid_string);
3058 return BLUETOOTH_ERROR_NONE;
3064 /* GATT client events */
3065 static void __bt_handle_client_instance_registered(event_gattc_register_t *data)
3067 bt_service_app_info_t *info = NULL;
3069 char *uuid_string = g_malloc0(BT_UUID_STRING_MAX);
3071 _bt_uuid_to_string(&(data->client_uuid), uuid_string);
3072 BT_INFO("CLient ID is Initialized [%d] UUID initialized [%s]", data->client_if, uuid_string);
3074 /* Platform GATT client framwork does not use Default GATT client instance
3075 This GATT client instance is never deregistred in the lifetime of bt-service */
3076 if (g_strcmp0(uuid_string, DEFAULT_GATT_CLIENT_UUID) == 0) {
3077 BT_INFO("Default client Instance Registered");
3078 gatt_default_client = data->client_if;
3079 g_free(uuid_string);
3083 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
3086 if (g_strcmp0(info->uuid, uuid_string) == 0) {
3087 BT_INFO("Found GATT client.. sender [%s] Slot [%d] occupied", info->sender, k);
3088 info->is_initialized = TRUE;
3089 info->client_id = data->client_if;
3090 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE, BT_GATT_CLIENT_REGISTER,
3091 (void*)info, sizeof(bt_service_app_info_t));
3095 g_free(uuid_string);
3098 static void __bt_handle_client_connected(event_gattc_conn_t *event_data)
3100 int result = BLUETOOTH_ERROR_NONE;
3101 struct gatt_server_info_t *conn_info = NULL;
3102 struct gatt_out_conn_info_t *out_conn_info = NULL;
3104 GVariant *param = NULL;
3106 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
3107 _bt_convert_addr_type_to_string(address,
3108 (unsigned char *)event_data->address.addr);
3110 if (event_data->status != OAL_STATUS_SUCCESS)
3111 result = BLUETOOTH_ERROR_INTERNAL;
3113 /* DBUS Return fo BT_CONNECT_LE for all the apps */
3114 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE, address,
3115 BT_ADDRESS_STRING_SIZE);
3117 BT_INFO("Local GATT Client Connected: Remote addr[%s] Client Interface [%d] Connection ID [%d] status[%d]",
3118 address, event_data->client_if, event_data->conn_id, event_data->status);
3120 if (result == BLUETOOTH_ERROR_NONE) {
3121 /* Check if device is already in connected list */
3122 conn_info = _bt_find_remote_gatt_server_info(address);
3125 /* Send event to BT-API */
3126 param = g_variant_new("(is)", result, address);
3127 _bt_send_event(BT_DEVICE_EVENT,
3128 BLUETOOTH_EVENT_GATT_CLIENT_CONNECTED, /* Local device is GATT client */
3131 /* Save Connection info */
3132 conn_info = g_new0(struct gatt_server_info_t, 1);
3133 conn_info->addr = g_strdup(address);
3134 conn_info->client_id = event_data->client_if;
3135 conn_info->connection_id = event_data->conn_id;
3136 gatt_server_info_list = g_slist_append(gatt_server_info_list, conn_info);
3137 BT_DBG("Total num of connected Remote GATT server devices [%d]",
3138 g_slist_length(gatt_server_info_list));
3141 BT_INFO("Do a Internal refresh");
3142 if (OAL_STATUS_SUCCESS != gattc_refresh(conn_info->client_id, &event_data->address))
3143 BT_ERR("GATT database refresh failed!!");
3145 BT_INFO("GATT database refresh Success!!");
3148 BT_ERR("Local GATT Client connected event for addr[%s], but device is in connected list already", address);
3150 __bt_add_mtu_gatt_device(address);
3152 BT_ERR("GATT Client Connection failed!!");
3154 /* If outgoing connection Info is present, then remove it */
3155 out_conn_info = __bt_find_gatt_outgoing_conn_info(address);
3156 if (out_conn_info) {
3157 BT_ERR("Outgoing Client connect request was sent");
3158 outgoing_gatt_conn_list = g_slist_remove(outgoing_gatt_conn_list, out_conn_info);
3159 g_free(out_conn_info->addr);
3160 g_free(out_conn_info);
3165 static void __bt_handle_client_disconnected(event_gattc_conn_t *event_data)
3167 int result = BLUETOOTH_ERROR_NONE;
3169 struct gatt_server_info_t *conn_info = NULL;
3170 struct gatt_out_conn_info_t *out_conn_info = NULL;
3172 GVariant *param = NULL;
3174 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
3175 _bt_convert_addr_type_to_string(address,
3176 (unsigned char *)event_data->address.addr);
3178 if (event_data->status != OAL_STATUS_SUCCESS)
3179 result = BLUETOOTH_ERROR_INTERNAL;
3181 if (NULL == _bt_get_request_info_data(BT_DISCONNECT_LE, address)) {
3182 if (NULL != _bt_get_request_info_data(BT_CONNECT_LE, address)) {
3183 result = BLUETOOTH_ERROR_INTERNAL;
3184 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
3185 address, BT_ADDRESS_STRING_SIZE);
3186 BT_ERR("Failed to connect Local GATT Remote addr[%s]", address);
3191 /* DBUS Return for BT_DISCONNECT_LE for all the apps */
3192 __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE, address,
3193 BT_ADDRESS_STRING_SIZE);
3195 BT_INFO("Local GATT Client DisConnected: Remote addr[%s] Client Interface [%d] Connection ID [%d] status [%d]",
3196 address, event_data->client_if, event_data->conn_id, event_data->status);
3198 /* Remove Connection info */
3199 conn_info = _bt_find_remote_gatt_server_info(address);
3202 param = g_variant_new("(is)", result, address);
3203 /* Send event to application */
3204 _bt_send_event(BT_DEVICE_EVENT,
3205 BLUETOOTH_EVENT_GATT_CLIENT_DISCONNECTED,
3208 BT_INFO("Remove GATT server info from List..");
3209 /* Remove all services from info list_gatt_info */
3210 __bt_cleanup_remote_services(conn_info);
3212 /* Remove info from List */
3213 gatt_server_info_list = g_slist_remove(gatt_server_info_list, conn_info);
3214 BT_INFO("Total num of connected GATT servers [%d]", g_slist_length(gatt_server_info_list));
3215 g_free(conn_info->addr);
3218 BT_INFO("Can not find conn info, already removed!");
3220 __bt_remove_mtu_gatt_device(address);
3222 /* If outgoing connection Info is present, then remove it */
3223 out_conn_info = __bt_find_gatt_outgoing_conn_info(address);
3224 if (out_conn_info) {
3225 BT_ERR("Client Disconnected event, but outgoing connect request was sent");
3226 outgoing_gatt_conn_list = g_slist_remove(outgoing_gatt_conn_list, out_conn_info);
3227 g_free(out_conn_info->addr);
3228 g_free(out_conn_info);
3234 static void __bt_handle_client_service_search_result(
3235 event_gattc_service_result_t *event_data)
3237 /* Pre: status is never fail from OAL */
3239 /* Find service list from address */
3240 bt_gatt_service_info_list_t *svc_info_list;
3241 bt_gatt_service_info_t *svc_info;
3243 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3244 if (!svc_info_list) {
3245 BT_DBG("Service info list not present for connection ID %d, means first time browse", event_data->conn_status.conn_id);
3246 /* Means for this conn_id, no services are ever browsed, first time,
3247 create service info list for this conn_id */
3248 svc_info_list = g_malloc0(sizeof(bt_gatt_service_info_list_t));
3249 svc_info_list->conn_id = event_data->conn_status.conn_id;
3250 list_gatt_info = g_slist_append(list_gatt_info, svc_info_list);
3253 /* send list and current service's uuid and instance id to find it */
3254 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3257 /* If not found, check if service changed, if yes, means this is a new service added
3258 in remote GATT device, update uuid info in svc info list structure, to be used when
3259 search is completed */
3261 if (svc_info_list->info.is_changed) {
3262 BT_DBG("Service Changed indication already found for connection ID %d", event_data->conn_status.conn_id);
3263 memcpy(svc_info_list->info.uuid, event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3265 /* Create and add new service in service list */
3266 svc_info = g_malloc0(sizeof(bt_gatt_service_info_t));
3267 memcpy(svc_info->uuid, event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3268 svc_info->inst_id = event_data->srvc_id.id.inst_id;
3269 svc_info->is_primary = event_data->srvc_id.is_prmry;
3270 svc_info_list->services = g_slist_append(svc_info_list->services, svc_info);
3271 BT_DBG("Service created and added in Svc info list or connection ID %d", event_data->conn_status.conn_id);
3273 /* If returned matching service info, then just update service_rmeoved value inside it to 0 */
3274 svc_info->is_removed = 0;
3278 static void __bt_handle_client_service_search_completed(
3279 event_gattc_conn_status_t *event_data)
3281 struct gatt_server_info_t *conn_info = NULL;
3282 bt_gatt_service_info_list_t *svc_info_list;
3283 bt_gatt_service_info_t *svc_info;
3284 bt_services_browse_info_t browse_info;
3285 unsigned char uuid_empty[BLUETOOTH_UUID_HEX_MAX_LEN];
3287 memset(&uuid_empty, 0x00, BLUETOOTH_UUID_HEX_MAX_LEN);
3288 memset(&browse_info, 0x00, sizeof(bt_services_browse_info_t));
3289 BT_INFO("Primary Services browsing completed status[%d] conn ID [%d]",
3290 event_data->status, event_data->conn_id);
3292 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(event_data->conn_id);
3294 svc_info_list = __bt_get_service_info_list(event_data->conn_id);
3295 if (!svc_info_list) {
3296 BT_ERR("No services browsed ever for addr [%s]", conn_info->addr);
3298 /* Just build response and return ERROR */
3299 __bt_build_service_browse_info(event_data->conn_id, &browse_info);
3301 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_INTERNAL,
3302 BT_GATT_GET_PRIMARY_SERVICES, &browse_info,
3303 sizeof(bt_services_browse_info_t));
3307 /* If fail, then send event with error */
3308 if (event_data->status != OAL_STATUS_SUCCESS) {
3309 /* Just build response and return ERROR */
3310 __bt_build_service_browse_info(event_data->conn_id, &browse_info);
3312 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_INTERNAL,
3313 BT_GATT_GET_PRIMARY_SERVICES, &browse_info,
3314 sizeof(bt_services_browse_info_t));
3318 /* If success, then find service info list from address */
3320 /* If svc_changed == 1 and uuid valid, means a new service is added*/
3321 if (svc_info_list->info.is_changed && !memcmp(uuid_empty, svc_info_list->info.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)) {
3322 /* TODO: Send event -Service added with instance ID and UUID of newly added service */
3323 BT_INFO("new service added");
3325 BT_INFO("TODO new service added");
3328 /* If svc_changed == 1 and uuid invalid, then a service is removed */
3329 if (svc_info_list->info.is_changed && memcmp(uuid_empty, svc_info_list->info.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)) {
3330 /* Scan through the service info list to find service with is_removed = 1*/
3331 svc_info = __bt_find_removed_service(svc_info_list);
3333 /* TODO Send event - Service removed with instance ID and UUID of just rmeoved service */
3335 /* Remove that service info from service info list */
3336 svc_info_list->services = g_slist_remove(svc_info_list->services, svc_info);
3338 /* Delete that service completely from svc_info list*/
3339 __bt_remove_service_info_from_list(svc_info);
3343 /* Reset svc_changed = 0, and reset UUID = all 0's */
3344 svc_info_list->info.is_changed = 0;
3345 memset(&svc_info_list->info.uuid, 0x00, BLUETOOTH_UUID_HEX_MAX_LEN);
3347 /* Build Reply and send to service browse primary services request of pending apps */
3348 __bt_build_service_browse_info(event_data->conn_id, &browse_info);
3350 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE,
3351 BT_GATT_GET_PRIMARY_SERVICES, &browse_info,
3352 sizeof(bt_services_browse_info_t));
3356 static void __bt_handle_client_characteristic_search_result(
3357 event_gattc_characteristic_result_t *event_data)
3359 bt_gatt_service_info_list_t *svc_info_list;
3360 bt_gatt_service_info_t *svc_info;
3361 bt_gatt_char_info_t *char_info;
3362 bt_char_browse_info_t browse_info;
3364 memset(&browse_info, 0x00, sizeof(bt_char_browse_info_t));
3367 if (event_data->conn_status.status == OAL_STATUS_SUCCESS) {
3368 /* Find service info list from address */
3369 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3370 if (svc_info_list == NULL) {
3371 BT_ERR("svc_info_list is NULL");
3376 /* Find matching service info from svc info list */
3377 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3378 if (svc_info == NULL) {
3379 BT_ERR("svc_info is NULL");
3383 /* Find Matching char from service info in event */
3384 char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
3386 /* If not found, then add new characteristic and return */
3388 BT_DBG("Add new characteristic");
3389 char_info = g_malloc0(sizeof(bt_gatt_char_info_t));
3390 memcpy(char_info->uuid, event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3392 char_info->inst_id = event_data->char_id.inst_id;
3393 char_info->props = event_data->char_prop;
3394 svc_info->chars = g_slist_append(svc_info->chars, char_info);
3396 /* If found, then return */
3397 BT_DBG("update char property as Characteristic browsed is already present");
3398 char_info->props |= event_data->char_prop;
3401 /* If Not success: Means Charc browse is completed */
3402 /* Find char list from service in event */
3403 /* Find service list from address */
3404 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3405 if (svc_info_list == NULL) {
3406 BT_ERR("svc_info_list is NULL");
3410 /* Find service info from service in event */
3411 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3412 if (svc_info == NULL) {
3413 BT_ERR("svc_info is NULL");
3417 /* Build char list from service in event */
3418 __bt_build_char_browse_info(event_data->conn_status.conn_id,
3419 svc_info, &browse_info);
3421 /* Create response and return by sending event*/
3422 /* Build Reply and send to service browse All Included services request of pending apps */
3423 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE,
3424 BT_GATT_GET_SERVICE_PROPERTIES,
3426 sizeof(bt_char_browse_info_t));
3430 static void __bt_handle_client_descriptor_search_result(
3431 event_gattc_descriptor_result_t *event_data)
3433 bt_gatt_service_info_list_t *svc_info_list;
3434 bt_gatt_service_info_t *svc_info;
3435 bt_gatt_char_info_t *char_info;
3436 bt_gatt_descriptor_info_t *desc_info;
3437 bt_descriptor_browse_info_t browse_info;
3439 BT_INFO("descriptor search result status [%d]", event_data->conn_status.status);
3441 memset(&browse_info, 0x00, sizeof(bt_descriptor_browse_info_t));
3444 if (event_data->conn_status.status == OAL_STATUS_SUCCESS) {
3445 /* Find service list from address */
3446 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3447 if (svc_info_list == NULL) {
3448 BT_ERR("svc_info_list is NULL");
3452 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3453 if (svc_info == NULL) {
3454 BT_ERR("svc_info is NULL");
3458 char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
3459 if (char_info == NULL) {
3460 BT_ERR("char_info is NULL");
3464 desc_info = __bt_find_matching_desc(char_info, &event_data->descr_id);
3465 /* If not found, add new descriptor and return */
3467 desc_info = g_malloc0(sizeof(bt_gatt_descriptor_info_t));
3468 memcpy(desc_info->uuid, event_data->descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3470 desc_info->inst_id = event_data->descr_id.inst_id;
3471 char_info->descs = g_slist_append(char_info->descs, desc_info);
3474 /* If found, then return */
3475 BT_INFO("Characteristic browsed is already presesnt");
3478 /* If Not success */
3479 /* Find service list from address */
3480 /* Find included service list from service in event */
3481 /* Create response and return by sending event*/
3482 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3483 if (svc_info_list == NULL) {
3484 BT_ERR("svc_info_list is NULL");
3488 /* Find service info from service in event */
3489 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3490 if (svc_info == NULL) {
3491 BT_ERR("svc_info is NULL");
3495 /* Find char info from char in event */
3496 char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
3497 if (char_info == NULL) {
3498 BT_ERR("char_info is NULL");
3502 /* Build descriptor list from char in event */
3503 __bt_build_descriptor_browse_info(event_data->conn_status.conn_id,
3504 svc_info, char_info, &browse_info);
3507 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE,
3508 BT_GATT_GET_CHARACTERISTIC_PROPERTIES,
3510 sizeof(bt_descriptor_browse_info_t));
3514 static void __bt_handle_client_characteristic_read_data(
3515 event_gattc_read_data *event_data)
3517 int result = BLUETOOTH_ERROR_NONE;
3518 struct gatt_server_info_t *conn_info = NULL;
3519 bluetooth_gatt_client_char_prop_info_t read_info;
3521 /* Read Information data structures */
3522 GVariant *param = NULL;
3523 GVariant *data = NULL;
3524 GVariant *data_svc_uuid = NULL;
3525 GVariant *data_char_uuid = NULL;
3526 char *read_val = NULL;
3527 char *svc_uuid = NULL;
3528 char *char_uuid = NULL;
3531 //memset(&read_info, 0x00, sizeof(bt_gatt_handle_property_t));
3532 memset(&read_info, 0x00, sizeof(bluetooth_gatt_client_char_prop_info_t));
3534 /* Extract Address from conn_id of event data */
3535 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3536 event_data->uuid_status.conn_status.conn_id);
3538 BT_INFO("Characteristic Read result from addr [%s] status [%d]",
3539 conn_info->addr, event_data->uuid_status.conn_status.status);
3541 /* Fill char in buffer */
3542 memcpy(&read_info.characteristic.uuid,
3543 event_data->uuid_status.char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3544 read_info.characteristic.instance_id = event_data->uuid_status.char_id.inst_id;
3546 /* Fill Service in buffer */
3547 memcpy(&read_info.svc.uuid,
3548 event_data->uuid_status.srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3549 read_info.svc.instance_id = event_data->uuid_status.srvc_id.id.inst_id;
3551 /* Fill remote device address */
3552 _bt_convert_addr_string_to_type(read_info.device_address.addr, conn_info->addr);
3554 /* Fill data and reply to all apps waiting for Read result on the same characteristic
3555 Note: Even in case of failure, address, handles and result code should be returned */
3556 if (event_data->uuid_status.conn_status.status != OAL_STATUS_SUCCESS)
3557 result = BLUETOOTH_ERROR_INTERNAL;
3559 if (event_data->data_len > 0) {
3561 // for (i = 0; i < event_data->data_len; i++)
3562 // BT_INFO("Data[%d] = [0x%x]", i, event_data->data[i]);
3565 read_val = g_memdup(&event_data->data[0], event_data->data_len);
3567 data = g_variant_new_from_data(
3568 G_VARIANT_TYPE_BYTESTRING,
3570 event_data->data_len,
3573 BT_ERR("Characteristic Read success, but no data!!!");
3575 data = g_variant_new_from_data(
3576 G_VARIANT_TYPE_BYTESTRING,
3583 svc_uuid = g_memdup(&event_data->uuid_status.srvc_id.id.uuid.uuid[0], uuid_len);
3585 data_svc_uuid = g_variant_new_from_data(
3586 G_VARIANT_TYPE_BYTESTRING,
3592 char_uuid = g_memdup(&event_data->uuid_status.char_id.uuid.uuid[0], uuid_len);
3594 data_char_uuid = g_variant_new_from_data(
3595 G_VARIANT_TYPE_BYTESTRING,
3600 param = g_variant_new("(isn@ayin@ayin@ay)", result,
3604 event_data->uuid_status.srvc_id.id.inst_id,
3607 event_data->uuid_status.char_id.inst_id,
3608 event_data->data_len,
3612 _bt_send_event(BT_GATT_CLIENT_EVENT,
3613 BLUETOOTH_EVENT_GATT_READ_CHAR,
3616 /* Send DBUS return */
3617 __bt_gatt_handle_pending_request_info(result,
3618 BT_GATT_READ_CHARACTERISTIC,
3620 sizeof(bluetooth_gatt_client_char_prop_info_t));
3631 static void __bt_handle_client_descriptor_read_data(
3632 event_gattc_read_data *event_data)
3634 int result = BLUETOOTH_ERROR_NONE;
3635 struct gatt_server_info_t *conn_info = NULL;
3636 bluetooth_gatt_client_desc_prop_info_t read_info;
3638 /* Read Information data structures */
3639 GVariant *param = NULL;
3640 GVariant *data = NULL;
3641 GVariant *data_svc_uuid = NULL;
3642 GVariant *data_char_uuid = NULL;
3643 GVariant *data_desc_uuid = NULL;
3644 char *read_val = NULL;
3645 char *svc_uuid = NULL;
3646 char *char_uuid = NULL;
3647 char *desc_uuid = NULL;
3652 memset(&read_info, 0x00, sizeof(bluetooth_gatt_client_desc_prop_info_t));
3654 /* Extract Address from conn_id of event data */
3655 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3656 event_data->uuid_status.conn_status.conn_id);
3658 BT_INFO("Descriptor Read result from addr [%s] status [%d]",
3659 conn_info->addr, event_data->uuid_status.conn_status.status);
3661 /* Fill descriptor informations in buffer */
3662 memcpy(&read_info.descriptor.uuid,
3663 event_data->uuid_status.descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3664 read_info.descriptor.instance_id = event_data->uuid_status.descr_id.inst_id;
3666 /* Fill Characteristic informations in buffer */
3667 memcpy(&read_info.characteristic.uuid,
3668 event_data->uuid_status.char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3669 read_info.characteristic.instance_id = event_data->uuid_status.char_id.inst_id;
3671 /* Fill Service informations in buffer */
3672 memcpy(&read_info.svc.uuid,
3673 event_data->uuid_status.srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3674 read_info.svc.instance_id = event_data->uuid_status.srvc_id.id.inst_id;
3676 /* Fill remote device address */
3677 _bt_convert_addr_string_to_type(read_info.device_address.addr, conn_info->addr);
3679 /* Fill data and reply to all apps waiting for Read result on the same characteristic */
3680 if (event_data->uuid_status.conn_status.status != OAL_STATUS_SUCCESS)
3681 result = BLUETOOTH_ERROR_INTERNAL;
3683 BT_INFO("desc data len:%d", event_data->data_len);
3684 if (event_data->data_len > 0) {
3686 for (i = 0; i < event_data->data_len; i++)
3687 BT_DBG("Data[%d] = [0x%x]", i, event_data->data[i]);
3690 read_val = g_memdup(&event_data->data[0], event_data->data_len);
3692 data = g_variant_new_from_data(
3693 G_VARIANT_TYPE_BYTESTRING,
3695 event_data->data_len,
3698 BT_INFO("Descriptor Read success, but no data!!!");
3700 data = g_variant_new_from_data(
3701 G_VARIANT_TYPE_BYTESTRING,
3707 svc_uuid = g_memdup(&event_data->uuid_status.srvc_id.id.uuid.uuid[0], uuid_len);
3709 data_svc_uuid = g_variant_new_from_data(
3710 G_VARIANT_TYPE_BYTESTRING,
3716 char_uuid = g_memdup(&event_data->uuid_status.char_id.uuid.uuid[0], uuid_len);
3718 data_char_uuid = g_variant_new_from_data(
3719 G_VARIANT_TYPE_BYTESTRING,
3725 desc_uuid = g_memdup(&event_data->uuid_status.descr_id.uuid.uuid[0], uuid_len);
3727 data_desc_uuid = g_variant_new_from_data(
3728 G_VARIANT_TYPE_BYTESTRING,
3733 param = g_variant_new("(isn@ayin@ayin@ayin@ay)", result,
3737 event_data->uuid_status.srvc_id.id.inst_id,
3740 event_data->uuid_status.char_id.inst_id,
3743 event_data->uuid_status.descr_id.inst_id,
3744 event_data->data_len,
3748 _bt_send_event(BT_GATT_CLIENT_EVENT,
3749 BLUETOOTH_EVENT_GATT_READ_DESC,
3752 BT_INFO("Send DBUS rpely for GATT Read Descriptor");
3753 /* Send DBUS return */
3754 __bt_gatt_handle_pending_request_info(result,
3755 BT_GATT_READ_DESCRIPTOR_VALUE,
3757 sizeof(bluetooth_gatt_client_desc_prop_info_t));
3768 static void __bt_handle_client_characteristic_write_data(
3769 event_gattc_write_data *event_data)
3771 int result = BLUETOOTH_ERROR_NONE;
3772 struct gatt_server_info_t *conn_info = NULL;
3773 bluetooth_gatt_client_char_prop_info_t write_info;
3775 /* Read Information data structures */
3776 GVariant *param = NULL;
3777 GVariant *data_svc_uuid = NULL;
3778 GVariant *data_char_uuid = NULL;
3779 char *svc_uuid = NULL;
3780 char *char_uuid = NULL;
3784 memset(&write_info, 0x00, sizeof(bluetooth_gatt_client_char_prop_info_t));
3786 /* Extract Address from conn_id of event data */
3787 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3788 event_data->conn_status.conn_id);
3790 BT_INFO("Characteristic Write callback from addr [%s] status [%d]",
3791 conn_info->addr, event_data->conn_status.status);
3793 /* Fill char in buffer */
3794 memcpy(&write_info.characteristic.uuid,
3795 event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3796 write_info.characteristic.instance_id = event_data->char_id.inst_id;
3798 /* Fill Service in buffer */
3799 memcpy(&write_info.svc.uuid,
3800 event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3801 write_info.svc.instance_id = event_data->srvc_id.id.inst_id;
3803 /* Fill remote device address */
3804 _bt_convert_addr_string_to_type(write_info.device_address.addr, conn_info->addr);
3806 if (event_data->conn_status.status != OAL_STATUS_SUCCESS) {
3807 result = BLUETOOTH_ERROR_INTERNAL;
3813 svc_uuid = g_memdup(&event_data->srvc_id.id.uuid.uuid[0], uuid_len);
3815 data_svc_uuid = g_variant_new_from_data(
3816 G_VARIANT_TYPE_BYTESTRING,
3822 char_uuid = g_memdup(&event_data->char_id.uuid.uuid[0], uuid_len);
3824 data_char_uuid = g_variant_new_from_data(
3825 G_VARIANT_TYPE_BYTESTRING,
3830 param = g_variant_new("(isn@ayin@ayi)", result,
3834 event_data->srvc_id.id.inst_id,
3837 event_data->char_id.inst_id);
3840 _bt_send_event(BT_GATT_CLIENT_EVENT,
3841 BLUETOOTH_EVENT_GATT_WRITE_CHAR,
3850 /* Send DBUS return */
3851 __bt_gatt_handle_pending_request_info(result,
3852 BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE,
3854 sizeof(bluetooth_gatt_client_char_prop_info_t));
3858 static void __bt_handle_client_descriptor_write_data(
3859 event_gattc_write_data *event_data)
3861 int result = BLUETOOTH_ERROR_NONE;
3862 struct gatt_server_info_t *conn_info = NULL;
3863 bluetooth_gatt_client_desc_prop_info_t write_info;
3865 /* Write Information data structures */
3866 GVariant *param = NULL;
3867 GVariant *data_svc_uuid = NULL;
3868 GVariant *data_char_uuid = NULL;
3869 GVariant *data_desc_uuid = NULL;
3870 char *svc_uuid = NULL;
3871 char *char_uuid = NULL;
3872 char *desc_uuid = NULL;
3876 memset(&write_info, 0x00, sizeof(bluetooth_gatt_client_desc_prop_info_t));
3878 /* Extract Address from conn_id of event data */
3879 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3880 event_data->conn_status.conn_id);
3882 if (NULL == conn_info) {
3884 BT_INFO("Failed to get the conn info for conn_id [%d]", event_data->conn_status.conn_id);
3888 BT_INFO("Descriptor Write callback from addr [%s] status [%d]",
3889 conn_info->addr, event_data->conn_status.status);
3891 /* Fill descriptor informations in buffer */
3892 memcpy(&write_info.descriptor.uuid,
3893 event_data->descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3894 write_info.descriptor.instance_id = event_data->descr_id.inst_id;
3896 /* Fill Characteristic informations in buffer */
3897 memcpy(&write_info.characteristic.uuid,
3898 event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3899 write_info.characteristic.instance_id = event_data->char_id.inst_id;
3901 /* Fill Service informations in buffer */
3902 memcpy(&write_info.svc.uuid,
3903 event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3904 write_info.svc.instance_id = event_data->srvc_id.id.inst_id;
3906 /* Fill remote device address */
3907 _bt_convert_addr_string_to_type(write_info.device_address.addr, conn_info->addr);
3909 if (event_data->conn_status.status != OAL_STATUS_SUCCESS) {
3910 result = BLUETOOTH_ERROR_INTERNAL;
3916 svc_uuid = g_memdup(&event_data->srvc_id.id.uuid.uuid[0], uuid_len);
3918 data_svc_uuid = g_variant_new_from_data(
3919 G_VARIANT_TYPE_BYTESTRING,
3925 char_uuid = g_memdup(&event_data->char_id.uuid.uuid[0], uuid_len);
3927 data_char_uuid = g_variant_new_from_data(
3928 G_VARIANT_TYPE_BYTESTRING,
3934 desc_uuid = g_memdup(&event_data->descr_id.uuid.uuid[0], uuid_len);
3936 data_desc_uuid = g_variant_new_from_data(
3937 G_VARIANT_TYPE_BYTESTRING,
3942 param = g_variant_new("(isn@ayin@ayin@ayi)", result,
3946 event_data->srvc_id.id.inst_id,
3949 event_data->char_id.inst_id,
3952 event_data->descr_id.inst_id);
3955 _bt_send_event(BT_GATT_CLIENT_EVENT,
3956 BLUETOOTH_EVENT_GATT_WRITE_DESC,
3967 /* Send DBUS return */
3968 __bt_gatt_handle_pending_request_info(result,
3969 BT_GATT_WRITE_DESCRIPTOR_VALUE,
3971 sizeof(bluetooth_gatt_client_desc_prop_info_t));
3974 static void __bt_hanlde_le_device_disconnection(event_dev_conn_status_t *event_data)
3976 int result = BLUETOOTH_ERROR_INTERNAL;
3977 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
3979 _bt_convert_addr_type_to_string(address, (unsigned char *)event_data->address.addr);
3981 /* DBUS Return with fail of pending BT_CONNECT_LE for all the apps */
3982 BT_INFO("Local GATT Client disconnected: Remote addr[%s] ", address);
3984 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE, address,
3985 BT_ADDRESS_STRING_SIZE);
3989 static void __bt_handle_client_notification_registered(
3990 event_gattc_regdereg_notify_t *event_data,
3991 gboolean is_registered)
3993 int result = BLUETOOTH_ERROR_NONE;
3994 struct gatt_server_info_t *conn_info = NULL;
3995 bt_gatt_notif_reg_info_t notif_info;
3998 memset(¬if_info, 0x00, sizeof(bt_gatt_notif_reg_info_t));
4000 BT_INFO("Client Interface [%d] status [%d]",
4001 event_data->conn_id,
4002 event_data->status);
4004 /* Extract Address from conn_id of event data */
4005 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(event_data->conn_id);
4007 BT_INFO("Connection Info is not present, return");
4010 BT_INFO("Notification Registered for addr [%s]", conn_info->addr);
4012 /* Fill svc informations in buffer */
4013 memcpy(¬if_info.svc_uuid,
4014 event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4015 notif_info.svc_inst = event_data->srvc_id.id.inst_id;
4017 /* Fill char in buffer */
4018 memcpy(¬if_info.char_uuid,
4019 event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4020 notif_info.char_inst = event_data->char_id.inst_id;
4022 /* Fill remote device address */
4023 _bt_convert_addr_string_to_type(notif_info.addr.addr, conn_info->addr);
4025 notif_info.is_registered = is_registered;
4027 if (event_data->status != OAL_STATUS_SUCCESS)
4028 result = BLUETOOTH_ERROR_INTERNAL;
4030 /* Send DBUS Return for BT_GATT_WATCH_CHARACTERISTIC */
4031 __bt_gatt_handle_pending_request_info(result,
4032 BT_GATT_WATCH_CHARACTERISTIC,
4034 sizeof(bt_gatt_notif_reg_info_t));
4037 static void __bt_handle_client_notification_data(event_gattc_notify_data *event_data)
4039 /* No status in this event from OAL */
4040 int result = BLUETOOTH_ERROR_NONE;
4042 /* Read Information data structures */
4043 GVariant *param = NULL;
4044 GVariant *data = NULL;
4045 GVariant *data_svc_uuid = NULL;
4046 GVariant *data_char_uuid = NULL;
4047 char *read_val = NULL;
4048 char *svc_uuid = NULL;
4049 char *char_uuid = NULL;
4055 BT_INFO("Notifcation of charc data changed");
4057 if (event_data->data_len > 0) {
4059 for (i = 0; i < event_data->data_len; i++)
4060 BT_INFO("Data[%d] = [0x%x]", i, event_data->data[i]);
4063 addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
4064 _bt_convert_addr_type_to_string(addr,
4065 (unsigned char *)&(event_data->address.addr));
4068 read_val = g_memdup(&event_data->data[0], event_data->data_len);
4070 data = g_variant_new_from_data(
4071 G_VARIANT_TYPE_BYTESTRING,
4073 event_data->data_len,
4076 svc_uuid = g_memdup(&event_data->srvc_id.id.uuid.uuid[0], uuid_len);
4078 data_svc_uuid = g_variant_new_from_data(
4079 G_VARIANT_TYPE_BYTESTRING,
4085 char_uuid = g_memdup(&event_data->char_id.uuid.uuid[0], uuid_len);
4087 data_char_uuid = g_variant_new_from_data(
4088 G_VARIANT_TYPE_BYTESTRING,
4094 param = g_variant_new("(isn@ayin@ayin@ay)", result,
4098 event_data->srvc_id.id.inst_id,
4101 event_data->char_id.inst_id,
4102 event_data->data_len,
4106 _bt_send_event(BT_GATT_CLIENT_EVENT,
4107 BLUETOOTH_EVENT_GATT_CHAR_VAL_CHANGED,
4110 BT_ERR("No Data!!");
4123 static void __bt_handle_client_service_changed_ind(event_gattc_service_changed_data *event_data)
4125 bt_gatt_service_info_list_t *svc_info_list;
4127 bt_gatt_service_info_t *svc_info;
4128 GVariant *param = NULL;
4129 char *address_str = NULL;
4130 char *uuid_str = NULL;
4132 svc_info_list = __bt_get_service_info_list(event_data->conn_id);
4133 if (svc_info_list == NULL) {
4134 BT_ERR("svc_info_list is NULL");
4138 if (event_data->change_type) {
4139 /* Add service UUID in list */
4140 svc_info = g_malloc0(sizeof(bt_gatt_service_info_t));
4141 memcpy(svc_info->uuid, event_data->uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4142 svc_info->inst_id = event_data->inst_id;
4143 svc_info->is_primary = 1; // TODO: Need to check is_primary is required or not
4144 svc_info_list->services = g_slist_append(svc_info_list->services, svc_info);
4145 BT_DBG("Service created and added in Svc info list or connection ID %d", event_data->conn_id);
4147 /* Remove service UUID in list */
4148 for (l = svc_info_list->services; l != NULL; l = g_slist_next(l)) {
4149 svc_info = (bt_gatt_service_info_t *)l->data;
4150 if (svc_info == NULL)
4153 if (!memcmp(svc_info->uuid, event_data->uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)) {
4154 svc_info_list->services = g_slist_remove(svc_info_list->services, svc_info);
4155 __bt_free_service_info(svc_info);
4161 address_str = g_malloc0(BT_ADDRESS_STRING_SIZE);
4162 uuid_str = g_malloc0(BT_UUID_STRING_MAX);
4163 _bt_convert_addr_type_to_string(address_str, event_data->address.addr);
4164 _bt_uuid_to_string(&event_data->uuid, uuid_str);
4166 param = g_variant_new("(iiss)", event_data->inst_id, event_data->change_type, address_str, uuid_str);
4168 _bt_send_event(BT_GATT_CLIENT_EVENT,
4169 BLUETOOTH_EVENT_GATT_CLIENT_SERVICE_CHANGED,
4171 g_free(address_str);
4175 gboolean _bt_is_remote_gatt_device_connected(bluetooth_device_address_t *address)
4178 struct gatt_server_info_t *conn_info = NULL;
4179 gboolean connected = FALSE;
4181 addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
4182 _bt_convert_addr_type_to_string(addr,
4183 (unsigned char *)&(address->addr));
4185 BT_DBG("Check GATT connection status of [%s]", addr);
4186 /* Check if device is already in connected list */
4187 conn_info = _bt_find_remote_gatt_server_info(addr);
4190 BT_DBG("Remote GATT Server device [%s] is Connected", conn_info->addr);
4193 struct gatt_client_info_t *client_info = NULL;
4195 BT_INFO("Remote GATT Server Device [%s] is not Connected", addr);
4197 /* Check if device is already in connected list */
4198 client_info = _bt_find_remote_gatt_client_info(addr);
4201 BT_DBG("Remote Client device [%s] is Connected", client_info->addr);
4204 BT_INFO("Remote GATT Client Device [%s] is not Connected", addr);
4212 void _bt_handle_invocation_context(int function_name, void *data)
4214 switch (function_name) {
4216 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE, BT_CONNECT_LE,
4217 (char *)data, BT_ADDRESS_STRING_SIZE);
4224 int _bt_connect_le_device(bluetooth_device_address_t *address,
4225 int auto_connect, int client_id)
4227 struct gatt_server_info_t *conn_info = NULL;
4228 struct gatt_out_conn_info_t *out_conn_info = NULL;
4230 invocation_info_t *req_info = NULL;
4231 int ret = OAL_STATUS_SUCCESS;
4233 char *remote_address = NULL;
4235 BT_CHECK_PARAMETER(address, return);
4237 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4238 _bt_convert_addr_type_to_string(addr, address->addr);
4239 BT_DBG("GATT Client connect request for address [%s] client instance [%d]",
4243 /* Check if Remote Device is already under connection progress */
4244 req_info = _bt_get_request_info_data_from_function_name(BT_CONNECT_LE);
4246 remote_address = (char*)req_info->user_data;
4247 if (remote_address && !strcasecmp(remote_address, addr)) {/* Address matched */
4248 BT_DBG("Already Connection ongoing for same remote GATT Server address [%s]", remote_address);
4249 /* Return and wait for events to be sent to all apps */
4251 return BLUETOOTH_ERROR_IN_PROGRESS;
4255 /* Check if remote GATT Server is connected or not */
4256 conn_info = _bt_find_remote_gatt_server_info(addr);
4258 BT_ERR("GATT Server is already connected..");
4260 return BLUETOOTH_ERROR_ALREADY_CONNECT;
4263 /* TODO Check Requirement of holding Advertisement before initiating LE connect */
4265 /* Check if app sent 0 client id for connection, in such case, use default gatt client ID */
4266 if (client_id == 0) {
4267 /* GATT CLient connect request sent by an app without any client instance [0] */
4268 BT_DBG("Assign default GATT client id [%d]", gatt_default_client);
4269 client_id = gatt_default_client;
4272 BT_INFO("Connect using CLient ID [%d]", client_id);
4273 ret = gattc_connect(client_id, (bt_address_t*)(address), auto_connect);
4275 if (ret != OAL_STATUS_SUCCESS) {
4276 BT_ERR("gattc_connect is failed. ret: %d", ret);
4278 return BLUETOOTH_ERROR_INTERNAL;
4281 /* Mark this as outgoing connection */
4282 out_conn_info = g_new0(struct gatt_out_conn_info_t, 1);
4283 out_conn_info->addr = g_strdup(addr);
4284 out_conn_info->client_id = client_id;
4285 BT_INFO("Added outgoing connection info addr[%s]", out_conn_info->addr);
4286 outgoing_gatt_conn_list = g_slist_append(outgoing_gatt_conn_list, out_conn_info);
4289 return BLUETOOTH_ERROR_NONE;
4292 int _bt_gatt_get_primary_services(char *address)
4294 BT_CHECK_PARAMETER(address, return);
4295 struct gatt_server_info_t *conn_info = NULL;
4296 invocation_info_t *req_info = NULL;
4297 int ret = OAL_STATUS_SUCCESS;
4300 /* Check if any app is already browsing primary services on the same remote GATT Server */
4301 req_info = _bt_get_request_info_data(BT_GATT_GET_PRIMARY_SERVICES, address);
4303 BT_INFO("Already Primary Service Browsing ongoing for same rmeote GATT Server");
4304 /* Return and wait for events to be sent to all apps */
4305 return BLUETOOTH_ERROR_NONE;
4308 /* Check if remote GATT Server is connected or not */
4309 conn_info = _bt_find_remote_gatt_server_info(address);
4311 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4312 conn_info->addr, conn_info->connection_id);
4314 BT_ERR("GATT Server is not yet connected..");
4315 return BLUETOOTH_ERROR_NOT_CONNECTED;
4318 BT_INFO("Remove GATT server info from List..");
4319 /* Remove all services from info list_gatt_info */
4320 __bt_cleanup_remote_services(conn_info);
4322 /* Send Primary Service Browsing request to stack */
4323 ret = gattc_search_service(conn_info->connection_id, NULL);
4324 if (ret != OAL_STATUS_SUCCESS) {
4325 BT_ERR("ret: %d", ret);
4326 return BLUETOOTH_ERROR_INTERNAL;
4328 return BLUETOOTH_ERROR_NONE;
4331 int _bt_gatt_get_all_characteristic(bluetooth_gatt_client_svc_prop_info_t *svc)
4333 BT_CHECK_PARAMETER(svc, return);
4334 struct gatt_server_info_t *conn_info = NULL;
4335 invocation_info_t *req_info = NULL;
4336 bluetooth_gatt_client_svc_prop_info_t *prop;
4337 oal_gatt_srvc_id_t srvc_id;
4338 int ret = OAL_STATUS_SUCCESS;
4341 /* Check if any app is already browsing characteristics of the same service on the same remote GATT Server */
4342 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_GET_SERVICE_PROPERTIES);
4344 prop = (bluetooth_gatt_client_svc_prop_info_t*)req_info->user_data;
4345 if (prop && !memcmp(svc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t))
4346 && !memcmp(prop->svc.uuid, svc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
4347 && prop->svc.instance_id == svc->svc.instance_id) {
4348 BT_INFO("Already Properties browsing for Primary Service ongoing for same remote GATT Server");
4349 /* Return and wait for events to be sent to all apps */
4350 return BLUETOOTH_ERROR_NONE;
4354 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4355 _bt_convert_addr_type_to_string(addr, svc->device_address.addr);
4357 /* Check if remote GATT Server is connected or not */
4358 conn_info = _bt_find_remote_gatt_server_info(addr);
4360 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4361 conn_info->addr, conn_info->connection_id);
4363 BT_ERR("GATT Server is not yet connected..");
4365 return BLUETOOTH_ERROR_NOT_CONNECTED;
4368 srvc_id.is_prmry = TRUE;
4369 srvc_id.id.inst_id = svc->svc.instance_id;
4370 memcpy(srvc_id.id.uuid.uuid, svc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4372 /* Search All Characteristic */
4373 ret = gattc_get_characteristic(conn_info->connection_id, &srvc_id, NULL);
4374 if (ret != OAL_STATUS_SUCCESS) {
4375 BT_ERR("ret: %d", ret);
4377 return BLUETOOTH_ERROR_INTERNAL;
4380 return BLUETOOTH_ERROR_NONE;
4383 int _bt_gatt_get_all_characteristic_properties(
4384 bluetooth_gatt_client_char_prop_info_t *chr)
4386 struct gatt_server_info_t *conn_info = NULL;
4387 invocation_info_t *req_info = NULL;
4388 bluetooth_gatt_client_char_prop_info_t *prop;
4389 oal_gatt_srvc_id_t srvc_id;
4390 oal_gatt_id_t char_id;
4391 int ret = OAL_STATUS_SUCCESS;
4394 BT_CHECK_PARAMETER(chr, return);
4396 /* Check if any app is already browsing descriptors of the same char of
4397 particular service on the same remote GATT Server */
4398 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_GET_CHARACTERISTIC_PROPERTIES);
4400 prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
4401 if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4402 && !memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4403 && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4404 && !memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4405 && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
4406 BT_INFO("Already Properties browsing for Characteristic ongoing for same remote GATT Server");
4407 /* Return and wait for events to be sent to all apps */
4408 return BLUETOOTH_ERROR_NONE;
4412 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4413 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4415 /* Check if remote GATT Server is connected or not */
4416 conn_info = _bt_find_remote_gatt_server_info(addr);
4418 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4419 conn_info->addr, conn_info->connection_id);
4421 BT_ERR("GATT Server is not yet connected..");
4423 return BLUETOOTH_ERROR_NOT_CONNECTED;
4426 srvc_id.is_prmry = TRUE;
4427 srvc_id.id.inst_id = chr->svc.instance_id;
4428 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4430 char_id.inst_id = chr->characteristic.instance_id;
4431 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4433 /* Search All Descriptors */
4434 ret = gattc_get_descriptor(conn_info->connection_id, &srvc_id, &char_id, NULL);
4435 if (ret != OAL_STATUS_SUCCESS) {
4436 BT_ERR("ret: %d", ret);
4438 return BLUETOOTH_ERROR_INTERNAL;
4441 return BLUETOOTH_ERROR_NONE;
4444 int _bt_gatt_read_characteristic_value(
4445 bluetooth_gatt_client_char_prop_info_t *chr)
4447 struct gatt_server_info_t *conn_info = NULL;
4448 invocation_info_t *req_info = NULL;
4449 bluetooth_gatt_client_char_prop_info_t *prop;
4450 oal_gatt_srvc_id_t srvc_id;
4451 oal_gatt_id_t char_id;
4452 int ret = OAL_STATUS_SUCCESS;
4455 BT_CHECK_PARAMETER(chr, return);
4457 /* Check if any app is already Reading characteristic of the same char of
4458 particular service on the same remote GATT Server */
4459 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_READ_CHARACTERISTIC);
4461 prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
4462 if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4463 && memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4464 && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4465 && memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4466 && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
4467 BT_INFO("Already Characteristic value Read operation in progress for same remote GATT Server");
4468 /* Return and wait for events to be sent to all apps */
4469 return BLUETOOTH_ERROR_NONE;
4473 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4474 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4476 /* Check if remote GATT Server is connected or not */
4477 conn_info = _bt_find_remote_gatt_server_info(addr);
4479 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4480 conn_info->addr, conn_info->connection_id);
4482 BT_ERR("GATT Server is not yet connected..");
4484 return BLUETOOTH_ERROR_NOT_CONNECTED;
4487 srvc_id.is_prmry = TRUE;
4488 srvc_id.id.inst_id = chr->svc.instance_id;
4489 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4491 char_id.inst_id = chr->characteristic.instance_id;
4492 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4494 /* Search All Descriptors */
4495 ret = gattc_read_characteristic(conn_info->connection_id, &srvc_id, &char_id, OAL_GATT_AUTH_REQ_NONE);
4496 if (ret != OAL_STATUS_SUCCESS) {
4497 BT_ERR("ret: %d", ret);
4499 return BLUETOOTH_ERROR_INTERNAL;
4502 return BLUETOOTH_ERROR_NONE;
4505 int _bt_gatt_read_descriptor_value(
4506 bluetooth_gatt_client_desc_prop_info_t *desc)
4508 struct gatt_server_info_t *conn_info = NULL;
4509 invocation_info_t *req_info = NULL;
4510 bluetooth_gatt_client_desc_prop_info_t *prop;
4511 oal_gatt_srvc_id_t srvc_id;
4512 oal_gatt_id_t char_id;
4513 oal_gatt_id_t desc_id;
4514 int ret = OAL_STATUS_SUCCESS;
4517 BT_CHECK_PARAMETER(desc, return);
4519 /* Check if any app is already Reading descriptors of the same char of
4520 particular service on the same remote GATT Server */
4521 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_READ_DESCRIPTOR_VALUE);
4523 prop = (bluetooth_gatt_client_desc_prop_info_t*)req_info->user_data;
4524 if (prop && !memcmp(desc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4525 && memcmp(desc->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4526 && desc->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4527 && memcmp(desc->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4528 && desc->characteristic.instance_id == prop->characteristic.instance_id /* Characteristic Instance ID matched */
4529 && memcmp(desc->descriptor.uuid, prop->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Descriptor UUID matched */
4530 && desc->descriptor.instance_id == prop->descriptor.instance_id) { /* Descriptor Instance ID matched */
4531 BT_INFO("Already Descriptor value Read operation in progress for same remote GATT Server");
4532 /* Return and wait for events to be sent to all apps */
4533 return BLUETOOTH_ERROR_NONE;
4537 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4538 _bt_convert_addr_type_to_string(addr, desc->device_address.addr);
4540 /* Check if remote GATT Server is connected or not */
4541 conn_info = _bt_find_remote_gatt_server_info(addr);
4543 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4544 conn_info->addr, conn_info->connection_id);
4546 BT_ERR("GATT Server is not yet connected..");
4548 return BLUETOOTH_ERROR_NOT_CONNECTED;
4551 srvc_id.is_prmry = TRUE;
4552 srvc_id.id.inst_id = desc->svc.instance_id;
4553 memcpy(srvc_id.id.uuid.uuid, desc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4555 char_id.inst_id = desc->characteristic.instance_id;
4556 memcpy(char_id.uuid.uuid, desc->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4558 desc_id.inst_id = desc->descriptor.instance_id;
4559 memcpy(desc_id.uuid.uuid, desc->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4561 /* Search All Descriptors */
4562 ret = gattc_read_descriptor(conn_info->connection_id, &srvc_id, &char_id,
4563 &desc_id, OAL_GATT_AUTH_REQ_NONE);
4564 if (ret != OAL_STATUS_SUCCESS) {
4565 BT_ERR("ret: %d", ret);
4567 return BLUETOOTH_ERROR_INTERNAL;
4570 return BLUETOOTH_ERROR_NONE;
4574 int _bt_gatt_acquire_notify(bluetooth_gatt_client_char_prop_info_t *chr, int *fd, int *mtu)
4576 struct gatt_server_info_t *conn_info = NULL;
4577 oal_gatt_srvc_id_t srvc_id;
4578 oal_gatt_id_t char_id;
4579 int ret = OAL_STATUS_SUCCESS;
4582 BT_CHECK_PARAMETER(chr, return);
4584 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4585 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4587 /* Check if remote GATT Server is connected or not */
4588 conn_info = _bt_find_remote_gatt_server_info(addr);
4590 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4591 conn_info->addr, conn_info->connection_id);
4593 BT_ERR("GATT Server is not yet connected..");
4595 return BLUETOOTH_ERROR_NOT_CONNECTED;
4598 srvc_id.is_prmry = TRUE;
4599 srvc_id.id.inst_id = chr->svc.instance_id;
4600 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4602 char_id.inst_id = chr->characteristic.instance_id;
4603 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4605 ret = gattc_acquire_notify(conn_info->connection_id, &srvc_id, &char_id, fd, mtu);
4607 if (ret != OAL_STATUS_SUCCESS) {
4608 BT_ERR("ret: %d", ret);
4610 return BLUETOOTH_ERROR_INTERNAL;
4612 BT_INFO("GATT characterstics FD [%d] mtu[%d]", *fd, *mtu);
4614 return BLUETOOTH_ERROR_NONE;
4618 int _bt_gatt_acquire_write(bluetooth_gatt_client_char_prop_info_t *chr, int *fd, int *mtu)
4621 struct gatt_server_info_t *conn_info = NULL;
4622 oal_gatt_srvc_id_t srvc_id;
4623 oal_gatt_id_t char_id;
4624 int ret = OAL_STATUS_SUCCESS;
4627 BT_CHECK_PARAMETER(chr, return);
4629 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4630 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4632 /* Check if remote GATT Server is connected or not */
4633 conn_info = _bt_find_remote_gatt_server_info(addr);
4635 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4636 conn_info->addr, conn_info->connection_id);
4638 BT_ERR("GATT Server is not yet connected..");
4640 return BLUETOOTH_ERROR_NOT_CONNECTED;
4643 srvc_id.is_prmry = TRUE;
4644 srvc_id.id.inst_id = chr->svc.instance_id;
4645 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4647 char_id.inst_id = chr->characteristic.instance_id;
4648 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4650 ret = gattc_acquire_write(conn_info->connection_id, &srvc_id, &char_id,
4651 OAL_GATT_AUTH_REQ_NONE, fd, mtu);
4652 if (ret != OAL_STATUS_SUCCESS) {
4653 BT_ERR("ret: %d", ret);
4655 return BLUETOOTH_ERROR_INTERNAL;
4657 BT_INFO("GATT characterstics FD [%d] mtu [%d]", *fd, *mtu);
4659 return BLUETOOTH_ERROR_NONE;
4664 /* Write Characteristic */
4665 int _bt_gatt_write_characteristic_value_by_type(
4666 bluetooth_gatt_client_char_prop_info_t *chr,
4667 bluetooth_gatt_att_data_t *data,
4668 bluetooth_gatt_write_type_e write_type)
4670 struct gatt_server_info_t *conn_info = NULL;
4671 invocation_info_t *req_info = NULL;
4672 bluetooth_gatt_client_char_prop_info_t *prop;
4673 oal_gatt_srvc_id_t srvc_id;
4674 oal_gatt_id_t char_id;
4675 int ret = OAL_STATUS_SUCCESS;
4679 BT_CHECK_PARAMETER(chr, return);
4680 BT_CHECK_PARAMETER(data, return);
4684 /* Check if any app is already writing same char of
4685 particular service on the same remote GATT Server */
4686 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE);
4688 prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
4689 if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4690 && memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4691 && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4692 && memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4693 && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
4694 BT_INFO("Already Characteristic Write Value operation in progress for same remote GATT Server");
4695 /* Return and wait for events to be sent to all apps */
4696 return BLUETOOTH_ERROR_NONE;
4700 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4701 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4703 /* Check if remote GATT Server is connected or not */
4704 conn_info = _bt_find_remote_gatt_server_info(addr);
4706 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4707 conn_info->addr, conn_info->connection_id);
4709 BT_ERR("GATT Server is not yet connected..");
4711 return BLUETOOTH_ERROR_NOT_CONNECTED;
4714 srvc_id.is_prmry = TRUE;
4715 srvc_id.id.inst_id = chr->svc.instance_id;
4716 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4718 char_id.inst_id = chr->characteristic.instance_id;
4719 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4721 /* Write CHar value */
4723 BT_INFO("Connection ID [%d] write type [%d] data length [%d]", conn_info->connection_id, write_type, data->length);
4724 for (k = 0; k < data->length; k++)
4725 BT_INFO("Data[%d] [0x%x]", k, data->data[k]);
4727 ret = gattc_write_characteristic(conn_info->connection_id,
4729 (oal_gatt_write_type_t)write_type, data->length,
4730 OAL_GATT_AUTH_REQ_NONE, (char *)(&data->data[0]));
4731 if (ret != OAL_STATUS_SUCCESS) {
4732 BT_ERR("ret: %d", ret);
4734 return BLUETOOTH_ERROR_INTERNAL;
4737 return BLUETOOTH_ERROR_NONE;
4740 /* Write Descriptor */
4741 int _bt_gatt_write_descriptor_value_by_type(
4742 bluetooth_gatt_client_desc_prop_info_t *desc,
4743 bluetooth_gatt_att_data_t *data,
4744 bluetooth_gatt_write_type_e write_type)
4746 struct gatt_server_info_t *conn_info = NULL;
4747 invocation_info_t *req_info = NULL;
4748 bluetooth_gatt_client_desc_prop_info_t *prop;
4749 oal_gatt_srvc_id_t srvc_id;
4750 oal_gatt_id_t char_id;
4751 oal_gatt_id_t desc_id;
4752 int ret = OAL_STATUS_SUCCESS;
4756 BT_CHECK_PARAMETER(desc, return);
4757 BT_CHECK_PARAMETER(data, return);
4761 /* Check if any app is already writing on same Descriptor of the same char of
4762 particular service on the same remote GATT Server */
4763 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_WRITE_DESCRIPTOR_VALUE);
4765 prop = (bluetooth_gatt_client_desc_prop_info_t*)req_info->user_data;
4766 if (prop && !memcmp(desc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4767 && memcmp(desc->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4768 && desc->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4769 && memcmp(desc->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4770 && desc->characteristic.instance_id == prop->characteristic.instance_id /* Characteristic Instance ID matched */
4771 && memcmp(desc->descriptor.uuid, prop->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Descriptor UUID matched */
4772 && desc->descriptor.instance_id == prop->descriptor.instance_id) { /* Descriptor Instance ID matched */
4773 BT_INFO("Already Descriptor value Write operation in progress for same remote GATT Server");
4774 /* Return and wait for events to be sent to all apps */
4775 return BLUETOOTH_ERROR_NONE;
4779 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4780 _bt_convert_addr_type_to_string(addr, desc->device_address.addr);
4782 /* Check if remote GATT Server is connected or not */
4783 conn_info = _bt_find_remote_gatt_server_info(addr);
4785 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4786 conn_info->addr, conn_info->connection_id);
4788 BT_ERR("GATT Server is not yet connected..");
4790 return BLUETOOTH_ERROR_NOT_CONNECTED;
4793 srvc_id.is_prmry = TRUE;
4794 srvc_id.id.inst_id = desc->svc.instance_id;
4795 memcpy(srvc_id.id.uuid.uuid, desc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4797 char_id.inst_id = desc->characteristic.instance_id;
4798 memcpy(char_id.uuid.uuid, desc->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4800 desc_id.inst_id = desc->descriptor.instance_id;
4801 memcpy(desc_id.uuid.uuid, desc->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4804 BT_INFO("Connection ID [%d] write type [%d] data length [%d]", conn_info->connection_id, write_type, data->length);
4805 for (k = 0; k < data->length; k++)
4806 BT_INFO("Data[%d] [0x%x]", k, data->data[k]);
4808 ret = gattc_write_descriptor(conn_info->connection_id,
4809 &srvc_id, &char_id, &desc_id,
4810 (oal_gatt_write_type_t)write_type, data->length,
4811 OAL_GATT_AUTH_REQ_NONE, (char *)(&data->data[0]));
4812 if (ret != OAL_STATUS_SUCCESS) {
4813 BT_ERR("ret: %d", ret);
4815 return BLUETOOTH_ERROR_INTERNAL;
4818 return BLUETOOTH_ERROR_NONE;
4821 int _bt_gatt_watch_characteristic(
4822 bluetooth_gatt_client_char_prop_info_t *chr,
4826 struct gatt_server_info_t *conn_info = NULL;
4827 oal_gatt_srvc_id_t srvc_id;
4828 oal_gatt_id_t char_id;
4829 int ret = OAL_STATUS_SUCCESS;
4832 BT_CHECK_PARAMETER(chr, return);
4834 BT_INFO("Client ID [%d] Is Notify [%d]", client_id, is_notify);
4836 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4837 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4839 /* Check if remote GATT Server is connected or not */
4840 conn_info = _bt_find_remote_gatt_server_info(addr);
4842 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4843 conn_info->addr, conn_info->connection_id);
4845 BT_ERR("GATT Server is not yet connected..");
4847 return BLUETOOTH_ERROR_NOT_CONNECTED;
4849 srvc_id.is_prmry = TRUE;
4850 srvc_id.id.inst_id = chr->svc.instance_id;
4851 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4853 char_id.inst_id = chr->characteristic.instance_id;
4854 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4856 /* Register or unregister Notification characteristic */
4858 ret = gattc_register_for_notification(conn_info->connection_id,
4859 (bt_address_t*)&(chr->device_address),
4860 &srvc_id, &char_id);
4862 ret = gattc_deregister_for_notification(conn_info->connection_id,
4863 (bt_address_t*)&(chr->device_address),
4864 &srvc_id, &char_id);
4866 BT_INFO("Result[%d]", ret);
4867 if (ret != OAL_STATUS_SUCCESS) {
4868 BT_ERR("ret: %d", ret);
4870 return BLUETOOTH_ERROR_INTERNAL;
4873 return BLUETOOTH_ERROR_NONE;
4877 int _bt_disconnect_le_device(bluetooth_device_address_t *address,
4880 struct gatt_server_info_t *conn_info = NULL;
4881 struct gatt_client_info_t *rem_client_conn_info = NULL;
4882 invocation_info_t *req_info = NULL;
4883 int ret = OAL_STATUS_SUCCESS;
4885 char *remote_address = NULL;
4887 BT_CHECK_PARAMETER(address, return);
4889 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4890 _bt_convert_addr_type_to_string(addr, address->addr);
4891 BT_INFO("GATT Client Disconnect request for address [%s]", addr);
4893 /* Check if Remote Device is already under connection progress */
4894 req_info = _bt_get_request_info_data_from_function_name(BT_DISCONNECT_LE);
4896 remote_address = (char*)req_info->user_data;
4897 if (remote_address && !strcasecmp(remote_address, addr)) {/* Address matched */
4898 BT_DBG("Already DisConnection ongoing for same remote GATT Server address [%s]", remote_address);
4899 /* Return success and wait for events to be sent to all apps */
4901 return BLUETOOTH_ERROR_IN_PROGRESS;
4904 /* Check if remote GATT Server is connected or not */
4905 conn_info = _bt_find_remote_gatt_server_info(addr);
4907 /* Check if app sent 0 client id for Disconnection, in such case, use default gatt client ID */
4908 if (client_id == 0) {
4909 BT_INFO("GATT CLient Disconnect request sent by an app without any client instance [%d]",
4911 BT_INFO("Assign default GATT client id [%d]", gatt_default_client);
4912 client_id = gatt_default_client;
4915 BT_INFO("Disconnect remote gatt server using CLient ID [%d] Connection ID [%d]", client_id, conn_info->connection_id);
4916 ret = gattc_disconnect(client_id, (bt_address_t*)(address),
4917 conn_info->connection_id);
4919 /* check if remote client is connected */
4920 rem_client_conn_info = _bt_find_remote_gatt_client_info(addr);
4922 if (!rem_client_conn_info || client_id != 0) {
4923 BT_ERR("GATT device is not connected..");
4925 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
4928 BT_INFO("Disconnect remote gatt client ");
4930 ret = gatts_disconnect(rem_client_conn_info->instance_id,
4931 (bt_address_t*)(address), rem_client_conn_info->connection_id);
4934 if (ret != OAL_STATUS_SUCCESS) {
4935 BT_ERR("ret: %d", ret);
4937 return BLUETOOTH_ERROR_INTERNAL;
4940 return BLUETOOTH_ERROR_NONE;
4943 int _bt_gatt_watch_service_changed_indication(const char *sender,
4944 bluetooth_device_address_t *address,
4945 gboolean is_enabled)
4948 bt_service_app_info_t *info = NULL;
4950 BT_INFO("%s Servic changed Indication watcher for app [%s]",
4951 is_enabled ? "Enable":"Disable", sender);
4953 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
4956 if (g_strcmp0(sender, info->sender) == 0 &&
4957 memcmp(info->address.addr, address->addr,
4958 sizeof(bluetooth_device_address_t)) == 0) {
4959 BT_DBG("Found GATT client App.. [%s], sender [%s]", info->uuid, info->sender);
4960 info->is_watcher_enabled = is_enabled;
4964 return BLUETOOTH_ERROR_NONE;
4967 int _bt_unregister_gatt_client_instance(const char *sender, int client_id)
4969 bt_service_app_info_t *info = NULL;
4972 BT_INFO("Unregister Allocated GATT Client instance [%s] Client ID [%d]", sender, client_id);
4974 /* Unregister CLient instance associated with address X. It is possible that another app still
4975 has client_id valid for same remote address */
4976 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
4979 /* Exact matching of sender */
4980 if (!g_strcmp0(info->sender, sender) && info->client_id == client_id) { /* Check for only valid GATT client Instance */
4981 BT_INFO("Unregister GATT client instance [%d]", info->client_id);
4982 numapps[k].client_id = -1;
4983 numapps[k].is_initialized = FALSE;
4984 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
4985 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
4986 memset(&numapps[k].address.addr, 0x00, sizeof(bluetooth_device_address_t));
4988 /* Its a GATT Client Instance */
4989 ret = gattc_deregister(client_id);
4990 if (ret != OAL_STATUS_SUCCESS) {
4991 BT_ERR("DeAllocate GATT Client instance with stack Fail ret: %d", ret);
4992 return BLUETOOTH_ERROR_INTERNAL;
4994 return BLUETOOTH_ERROR_NONE;
4999 return BLUETOOTH_ERROR_NOT_FOUND;
5002 static void __bt_handle_client_mtu_exchange_completed(event_gattc_mtu_configured_t *event_data)
5004 int result = BLUETOOTH_ERROR_NONE;
5005 struct gatt_server_info_t *conn_info = NULL;
5006 GVariant *param = NULL;
5010 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(event_data->conn_id);
5011 if (conn_info == NULL) {
5012 BT_ERR("Cant find connection Information");
5016 BT_INFO("GATT Client: MTU Configured from addr [%s] status [%d] MTU size [%d]",
5017 conn_info->addr, event_data->status, event_data->mtu);
5019 if (event_data->status != OAL_STATUS_SUCCESS)
5020 result = BLUETOOTH_ERROR_INTERNAL;
5022 BT_INFO("reply BT_REQ_ATT_MTU ");
5024 /* DBUS Return fo BT_REQ_ATT_MTU for all the apps */
5025 __bt_gatt_handle_pending_request_info(result, BT_REQ_ATT_MTU, conn_info->addr,
5026 BT_ADDRESS_STRING_SIZE);
5028 if (result == BLUETOOTH_ERROR_NONE) {
5029 BT_INFO("sending mtu event");
5031 mtu = event_data->mtu;
5032 param = g_variant_new("(isqy)",
5038 /* Send event to BT-API */
5039 _bt_send_event(BT_DEVICE_EVENT,
5040 BLUETOOTH_EVENT_GATT_ATT_MTU_CHANGED,
5043 /* Update the MTU for current connection */
5044 BT_INFO("Updating the MTU for current connection");
5045 __bt_update_mtu_gatt_device(conn_info->addr, event_data->mtu);
5049 static struct gatt_mtu_info_t *__bt_find_mtu_gatt_device(char *address)
5052 struct gatt_mtu_info_t *info = NULL;
5056 for (l = gatt_mtu_info_list; l != NULL; l = g_slist_next(l)) {
5057 info = (struct gatt_mtu_info_t*)l->data;
5061 if (!g_strcmp0(info->addr, address)) {
5062 BT_INFO("Remote GATT device found addr[%s]", info->addr);
5067 BT_INFO("Not found Remote GATT device addr[%s]", address);
5071 static void __bt_remove_mtu_gatt_device(char *address)
5073 struct gatt_mtu_info_t *dev_info = NULL;
5077 dev_info = __bt_find_mtu_gatt_device(address);
5080 BT_INFO("removing the gatt device from mtu list");
5081 gatt_mtu_info_list = g_slist_remove(gatt_mtu_info_list, dev_info);
5082 g_free(dev_info->addr);
5087 static void __bt_add_mtu_gatt_device(char *address)
5089 struct gatt_mtu_info_t *dev_info = NULL;
5093 dev_info = __bt_find_mtu_gatt_device(address);
5096 BT_INFO("adding the gatt device in mtu list");
5097 dev_info = g_new0(struct gatt_mtu_info_t, 1);
5098 dev_info->addr = g_strdup(address);
5099 dev_info->att_mtu = BT_DEFAULT_ATT_MTU;
5100 gatt_mtu_info_list = g_slist_append(gatt_mtu_info_list, dev_info);
5104 static void __bt_update_mtu_gatt_device(char *address, int mtu)
5106 struct gatt_mtu_info_t *dev_info = NULL;
5110 dev_info = __bt_find_mtu_gatt_device(address);
5113 BT_INFO("Udating the mtu");
5114 dev_info->att_mtu = mtu;