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(int instance_id);
241 static void __bt_free_service_info(bt_gatt_service_info_t *service_info);
243 static int __bt_do_unregister_gatt_instance(int instance_id);
244 static void __bt_service_reset_gatt_data(void);
246 #ifdef TIZEN_GATT_CLIENT
247 static void __bt_handle_client_instance_registered(event_gattc_register_t *data);
248 static void __bt_handle_client_connected(event_gattc_conn_t *event_data);
249 static void __bt_handle_client_disconnected(event_gattc_conn_t *event_data);
250 static void __bt_handle_client_service_search_completed(event_gattc_conn_status_t *event_data);
251 static void __bt_handle_client_service_search_result(event_gattc_service_result_t *event_data);
252 static void __bt_handle_client_characteristic_search_result(
253 event_gattc_characteristic_result_t *event_data);
254 static void __bt_handle_client_descriptor_search_result(event_gattc_descriptor_result_t *event_data);
255 static void __bt_handle_client_characteristic_read_data(event_gattc_read_data *event_data);
256 static void __bt_handle_client_descriptor_read_data(event_gattc_read_data *event_data);
257 static void __bt_handle_client_characteristic_write_data(event_gattc_write_data *event_data);
258 static void __bt_handle_client_descriptor_write_data(event_gattc_write_data *event_data);
259 static void __bt_hanlde_le_device_disconnection(event_dev_conn_status_t *event_data);
260 static void __bt_handle_client_notification_registered(event_gattc_regdereg_notify_t *event_data,
261 gboolean is_registered);
262 static void __bt_handle_client_notification_data(event_gattc_notify_data *event_data);
263 static void __bt_handle_client_service_changed_ind(event_gattc_service_changed_data *event_data);
264 static void __bt_handle_client_mtu_exchange_completed(event_gattc_mtu_configured_t *event_data);
267 static int __bt_unregister_gatt_server_instance(int server_instance);
270 struct gatt_mtu_info_t {
271 char *addr; /* Remote GATT Server address */
275 static GSList *gatt_mtu_info_list = NULL;
277 static struct gatt_mtu_info_t *__bt_find_mtu_gatt_device(char *address);
278 static void __bt_remove_mtu_gatt_device(char *address);
279 static void __bt_add_mtu_gatt_device(char *address);
280 static void __bt_update_mtu_gatt_device(char *address, int mtu);
282 /* Linked List of GATT requests from Remote GATT Clients */
283 static GSList *gatt_server_requests = NULL;
285 /* Linked List of connected Remote GATT clients */
286 static GSList *gatt_client_info_list = NULL;
288 /* Number of clients to be notified to */
289 static int num_indicate_clients;
291 /* List of applications */
292 static bt_service_app_info_t numapps[MAX_APPS_SUPPORTED];
294 static void __bt_gatt_handle_pending_request_info(int result,
295 int service_function, void *data, unsigned int size);
297 static void __bt_handle_server_instance_registered(event_gatts_register_t *data);
299 static void __bt_gatt_event_handler(int event_type, gpointer event_data);
301 void _bt_check_adv_app_termination(const char *name)
303 bt_service_app_info_t *app = NULL;
305 int apps[MAX_APPS_SUPPORTED];
307 ret_if(NULL == name);
309 memset(&apps, -1, sizeof(apps));
311 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
314 /* Search for a app which has same sender and stop adv is running */
315 if (!strncasecmp(app->sender, name, strlen(name)) && app->is_initialized == TRUE) {
316 BT_DBG("Match found, name: %s", name);
318 /* TODO 1: If App has both GATT client / server role? */
319 /* TODO 2: Need to manage app info as list, not array.
320 This loop always run for MAX count if any apps are terminated.
323 /* Save instances of all apps that need to be unregistered */
324 if (app->instance_id != -1) {
326 /* Unregister all service handles with stack */
327 __bt_remove_all_service_handles(app->instance_id);
329 /* If Advertising is enabled, stop it */
330 if (app->adv_handle > 0) {
331 BT_INFO("Stop advertising on instance ID [%d]", app->instance_id);
332 /* Disable adv if running */
333 BT_INFO("Disable Advertising Adv Handle [%d] sender [%s]",
334 app->adv_handle, name);
335 _bt_set_advertising(app->sender, app->adv_handle, FALSE, FALSE);
338 apps[app->instance_id] = 1; /* App holds a GATT client Instance */
339 } else if (app->client_id != -1) {
341 apps[app->client_id] = 1; /* App holds a GATT server Instance */
346 /* Unregister all apps one by one */
347 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
349 BT_INFO("Unregister app[%d]", k);
350 /* Unregister client or server instance */
351 __bt_do_unregister_gatt_instance(k);
356 static int __bt_gatt_send_indication_to_all_connected_clients(bluetooth_gatt_att_data_t *data,
357 bluetooth_gatt_server_indication_params_t *param)
360 int ret = OAL_STATUS_SUCCESS;
361 int result = OAL_STATUS_INTERNAL_ERROR;
363 BT_INFO("Current total number of connected clients [%d]", g_slist_length(gatt_client_info_list));
364 for (l = gatt_client_info_list; l != NULL; l = l->next) {
365 struct gatt_client_info_t *info = l->data;
368 BT_INFO("GATT Remote client address [%s] connection Id [%d]", info->addr, info->connection_id);
370 ret = gatts_send_indication(param->instance_id, param->atrribute_handle,
371 info->connection_id, data->length,
372 param->need_confirmation, (char *)(&data->data[0]));
374 BT_INFO("Send Indication to GATT client [%s] result: [%d]", info->addr, ret);
375 if (ret == OAL_STATUS_SUCCESS) {
376 BT_INFO("Send Indication sent successfully to GATT client [%s]", info->addr);
378 num_indicate_clients++;
382 BT_INFO("Indication sending done for total number of clients [%d]", num_indicate_clients);
386 static struct gatt_server_req_info *__bt_gatt_server_find_request_info(int request_id,
387 bluetooth_gatt_att_request_type_e req_type)
391 for (l = gatt_server_requests; l != NULL; l = l->next) {
392 struct gatt_server_req_info *req_info = l->data;
394 if (req_info && req_info->request_id == request_id && req_info->request_type == req_type) {
395 BT_INFO("GATT Server request info found Req ID [%d] handle [%d] conn ID [%d]",
396 req_info->request_id, req_info->attribute_handle, req_info->connection_id);
400 BT_ERR("Gatt Request not found");
404 void _bt_get_adv_handle_from_instance(int server_inst, int *adv_handle)
408 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
409 if (numapps[k].is_initialized == 1 && numapps[k].instance_id == server_inst) {
410 *adv_handle = numapps[k].adv_handle;
416 #ifdef TIZEN_GATT_CLIENT
417 char * _bt_gatt_get_default_gatt_client_uuid(void)
419 return g_strdup(DEFAULT_GATT_CLIENT_UUID);
423 static void __bt_register_default_gatt_client()
428 uuid_str = _bt_gatt_get_default_gatt_client_uuid();
429 _bt_string_to_uuid(uuid_str, (service_uuid_t*)&uuid);
431 BT_INFO("Register Default GATT client uuid [%s]", uuid_str);
433 if (OAL_STATUS_SUCCESS != gattc_register(&uuid)) /* for only Smart Control */
434 BT_ERR("gattc register failed");
440 int _bt_gatt_init(void)
442 const char *stack_name = NULL;
446 if (OAL_STATUS_SUCCESS != gatt_enable()) {
447 BT_ERR("gatt Init failed");
448 return BLUETOOTH_ERROR_INTERNAL;
451 /* Register gatt event handler */
452 _bt_service_register_event_handler_callback(BT_GATT_MODULE, __bt_gatt_event_handler);
454 __bt_service_reset_gatt_data();
456 stack_name = oal_get_stack_name();
458 if (stack_name && g_strcmp0(stack_name, "bluez") == 0) {
459 /*In the platform, defacult gatt client should be registered */
460 __bt_register_default_gatt_client();
464 return BLUETOOTH_ERROR_NONE;
467 static void __bt_service_reset_gatt_data(void)
471 BT_INFO("Rest numapp");
474 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
475 numapps[k].is_initialized = 0;
476 numapps[k].instance_id = -1;
477 numapps[k].adv_handle = 0;
478 numapps[k].adv_instance = -1;
479 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
480 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
481 memset(numapps[k].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
482 memset(numapps[k].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
483 numapps[k].adv_data_len = 0;
484 numapps[k].scan_rsp_len = 0;
487 numapps[k].client_id = -1;
488 memset(numapps[k].address.addr, 0x00, BLUETOOTH_ADDRESS_LENGTH);
489 numapps[k].is_watcher_enabled = FALSE;
493 void _bt_gatt_deinit(void)
495 BT_INFO("GATT deinit");
497 if (OAL_STATUS_SUCCESS != gatt_disable())
498 BT_ERR("gatt deinit failed");
500 /* Un-register gatt event handler */
501 _bt_service_unregister_event_handler_callback(BT_GATT_MODULE);
503 __bt_service_reset_gatt_data();
506 void _bt_update_adv_handle(const char *sender, int adv_handle)
509 BT_DBG("Sender [%s] Adv handle [%d]", sender, adv_handle);
510 bt_service_app_info_t *info = NULL;
512 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
514 /* Search for a app which has same sender and adv handle as 0 */
515 if (!g_strcmp0(info->sender, sender) && info->adv_handle == 0)
516 info->adv_handle = adv_handle;
520 int _bt_is_sender_gatt_server_with_no_adv(const char *sender, int adv_handle)
523 BT_DBG("Sender [%s] Adv handle [%d]", sender, adv_handle);
524 bt_service_app_info_t *info = NULL;
526 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
528 /* Search for a app which has same sender and adv handle as 0
529 It is possible that same sender but different adv handle */
530 if (!g_strcmp0(info->sender, sender) && info->adv_handle == 0) {
531 //info->adv_handle = adv_handle;
532 return info->instance_id;
538 int _bt_get_allocated_server_instance(const char *sender, int adv_handle, gboolean use_reserved_slot)
541 BT_DBG("Sender [%s] Adv handle [%d]", sender, adv_handle);
542 bt_service_app_info_t *info = NULL;
544 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
547 /* Exact matching of Adv handle + sender combination */
548 if (!g_strcmp0(info->sender, sender) && info->adv_handle == adv_handle)
549 return info->instance_id;
551 if (!g_strcmp0(info->sender, sender) && info->adv_handle == -1)
552 return info->instance_id;
558 char * _bt_get_sender_and_handle(int server_instance, int *adv_handle)
561 bt_service_app_info_t *info = NULL;
563 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
566 if (info->instance_id == server_instance && info->adv_handle != -1) {
567 *adv_handle = info->adv_handle;
568 BT_DBG("Server instance [%d] Adv handle [%d] Sender [%s]", server_instance, *adv_handle, info->sender);
569 return g_strdup(info->sender);
575 void _bt_set_new_adv_data(bluetooth_advertising_data_t *adv, int len, int instance)
579 bt_service_app_info_t *info = NULL;
580 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
583 if (info->instance_id == instance) {
584 memcpy(info->adv_data.data, &adv->data, len);
590 void _bt_set_new_scan_rsp_data(bluetooth_scan_resp_data_t *scan, int len, int instance)
594 bt_service_app_info_t *info = NULL;
595 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
598 if (info->instance_id == instance) {
599 memcpy(info->scan_rsp.data, &scan->data, len);
605 void _bt_get_previous_adv_data(bluetooth_advertising_data_t *adv, int *len, int instance)
608 bt_service_app_info_t *info = NULL;
610 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
613 if (info->instance_id == instance) {
614 memcpy(&adv->data, info->adv_data.data, info->adv_data_len);
615 *len = info->adv_data_len;
621 void _bt_get_previous_scan_rsp_data(bluetooth_scan_resp_data_t *scan, int *len, int instance)
625 bt_service_app_info_t *info = NULL;
627 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
630 if (info->instance_id == instance) {
631 memcpy(&scan->data, info->scan_rsp.data, info->scan_rsp_len);
632 *len = info->scan_rsp_len;
638 static int __bt_do_unregister_gatt_instance(int instance_id)
640 int ret = OAL_STATUS_SUCCESS;
643 BT_INFO("DeAllocate server or client instance ID [%d]", instance_id);
645 /* Reset data: instance_id parameter could be either for GATT Server or for GATT client */
646 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
647 if (numapps[k].instance_id == instance_id) {
648 BT_INFO("This is a GATT server app, unregister: Slot [%d] vacant", k);
649 numapps[k].is_initialized = FALSE;
650 numapps[k].instance_id = -1;
651 numapps[k].adv_handle = 0;
652 numapps[k].adv_instance = -1;
653 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
654 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
655 memset(numapps[k].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
656 memset(numapps[k].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
657 numapps[k].adv_data_len = 0;
658 numapps[k].scan_rsp_len = 0;
660 /* Its a GATT Server Instance */
661 ret = gatts_unregister(instance_id);
662 if (ret != OAL_STATUS_SUCCESS) {
663 BT_ERR("DeAllocate server instance with stack Fail ret: %d", ret);
664 return BLUETOOTH_ERROR_INTERNAL;
667 } else if (numapps[k].client_id == instance_id) {
668 BT_INFO("This is a GATT client app, unregister: Slot [%d] vacant", k);
669 numapps[k].client_id = -1;
670 numapps[k].is_initialized = FALSE;
671 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
672 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
673 memset(&numapps[k].address.addr, 0x00, sizeof(bluetooth_device_address_t));
675 /* Its a GATT Client Instance */
676 ret = gattc_deregister(instance_id);
677 if (ret != OAL_STATUS_SUCCESS) {
678 BT_ERR("DeAllocate GATT Client instance with stack Fail ret: %d", ret);
679 return BLUETOOTH_ERROR_INTERNAL;
684 return BLUETOOTH_ERROR_NONE;
687 static int __bt_unregister_gatt_server_instance(int server_instance)
689 int ret = OAL_STATUS_SUCCESS;
692 /* Unregister the server instance */
693 ret = gatts_unregister(server_instance);
694 if (ret != OAL_STATUS_SUCCESS) {
695 BT_ERR("DeAllocate server instance with stack Fail ret: %d", ret);
696 return BLUETOOTH_ERROR_INTERNAL;
698 BT_INFO("DeAllocated server instance with stack successful..");
701 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
702 if (numapps[k].instance_id == server_instance) {
703 numapps[k].is_initialized = 0;
704 numapps[k].instance_id = -1;
705 numapps[k].adv_handle = 0;
706 numapps[k].adv_instance = -1;
707 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
708 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
709 memset(numapps[k].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
710 memset(numapps[k].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
711 numapps[k].adv_data_len = 0;
712 numapps[k].scan_rsp_len = 0;
716 BT_DBG("Going8 to reset numapp block num [%d]", k);
717 return BLUETOOTH_ERROR_NONE;
720 static void __bt_remove_all_service_handles(int instance_id)
724 int ret = OAL_STATUS_SUCCESS;
727 count = g_slist_length(numapps[instance_id].service_handles);
728 BT_INFO("Before handle removal: current count [%d]", count);
730 for (l = numapps[instance_id].service_handles; l != NULL;) {
735 BT_INFO("Server Handle to be Removed [%d] Instance ID [%d]", *handle, instance_id);
736 if (_bt_gatt_server_stop_service(numapps[instance_id].sender, *handle, instance_id) != BLUETOOTH_ERROR_NONE)
739 ret = gatts_delete_service(instance_id, *handle);
740 if (ret != OAL_STATUS_SUCCESS) {
741 BT_ERR("ret: %d", ret);
744 numapps[instance_id].service_handles = g_slist_remove(numapps[instance_id].service_handles, handle);
747 count = g_slist_length(numapps[instance_id].service_handles);
748 BT_INFO("After deleting current count [%d]", count);
754 int _bt_unregister_server_instance(const char *sender, int adv_handle)
756 BT_INFO("Unregister Allocated server instance request Sender [%s] Adv handle [%d]", sender, adv_handle);
757 int result = BLUETOOTH_ERROR_NONE;
758 int apps[MAX_APPS_SUPPORTED];
762 memset(&apps, -1, sizeof(apps));
764 if (adv_handle == 0) {
765 BT_DBG("Its a direct GATT Server app request to unregister");
766 /* Unregister server instance for each app with same sender (case: GATT Server with multiple adv handle) */
767 bt_service_app_info_t *info = NULL;
769 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
772 /* Exact matching of sender */
773 if (!g_strcmp0(info->sender, sender)) {
774 BT_INFO("Unregister GATT server instance [%d]", info->instance_id);
775 /* Unregister all service handles with stack */
776 __bt_remove_all_service_handles(info->instance_id);
778 /* Disable adv if running */
779 BT_INFO("Disable Advertising Adv Handle [%d] sender [%s]",
780 info->adv_handle, sender);
781 _bt_set_advertising(sender, info->adv_handle, FALSE, FALSE);
783 /* Save all instances which need to be unregistered */
784 apps[info->instance_id] = 1;
788 BT_DBG("Its an Internal unregister request by adv application");
789 server_instance = _bt_get_allocated_server_instance(sender, adv_handle, FALSE);
790 BT_DBG("Its an Internal unregister request by adv application: Adv disabled srv instance [%d]", server_instance);
791 if (server_instance == -1) {
792 BT_ERR("No allocated server instance to be removed");
793 return BLUETOOTH_ERROR_INVALID_PARAM;
795 if (!numapps[server_instance].service_handles) {
796 BT_INFO("There are no Service handles with this app, safe to unregister");
797 /* Unregister server instance only if this sender does not have any gatt services in it */
798 result = __bt_unregister_gatt_server_instance(server_instance);
800 numapps[server_instance].adv_handle = 0;
801 memset(numapps[server_instance].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
802 numapps[server_instance].adv_data_len = 0;
803 memset(numapps[server_instance].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
804 numapps[server_instance].scan_rsp_len = 0;
809 /* Unregister all apps one by one */
810 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
812 BT_INFO("Unregister app[%d]", k);
813 /* Unregister server instance */
814 __bt_unregister_gatt_server_instance(k);
821 int _bt_register_server_instance(const char *sender, int adv_handle)
823 int ret = OAL_STATUS_SUCCESS;
824 char *uuid_string = NULL;
829 BT_INFO("###Check on which instance Server instance can be initialized....");
830 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
831 if (numapps[k].is_initialized == 1) {
832 BT_INFO("Instance ID [%d] is already in use..Check next slot", numapps[k].instance_id);
835 BT_INFO("Time to register GATT Server..UUID to be used is [%s] slot [%d]", uuid_list[slot-1], slot);
841 BT_ERR("No Slot if free for GATT Server registration..");
842 return BLUETOOTH_ERROR_REGISTRATION_FAILED;
845 uuid_string = g_malloc0(BT_UUID_STRING_MAX);
846 _bt_string_to_uuid(uuid_list[slot-1], (service_uuid_t*)&uuid);
847 g_strlcpy(uuid_string, uuid_list[slot-1], BT_UUID_STRING_MAX);
848 BT_INFO("Copied UUID string [%s]", uuid_string);
850 /* Register GATT Server */
851 ret = gatts_register(&uuid);
852 if (ret != OAL_STATUS_SUCCESS) {
853 BT_ERR("ret: %d", ret);
855 return BLUETOOTH_ERROR_INTERNAL;
857 BT_INFO("GATT Server registration call successfully accepted by OAL..wait for Instance Initialized event from OAL..");
858 /* Return & wait for GATT Server Instance Initialization event */
859 memset(numapps[slot].sender, 0x00, sizeof(numapps[slot].sender));
860 memset(numapps[slot].uuid, 0x00, sizeof(numapps[slot].uuid));
862 g_strlcpy(numapps[slot].sender, sender, sizeof(numapps[slot].sender));
863 g_strlcpy(numapps[slot].uuid, uuid_string, sizeof(numapps[slot].uuid));
865 numapps[slot].is_initialized = 0; /* Set initialization from app registered callback */
866 numapps[slot].adv_handle = adv_handle;
869 return BLUETOOTH_ERROR_NONE;
873 static void __bt_gatt_handle_pending_request_info(int result,
874 int service_function, void *data, unsigned int size)
878 invocation_info_t *req_info = NULL;
879 ret_if(data == NULL);
881 for (l = _bt_get_invocation_list(); l != NULL; ) {
884 if (req_info == NULL || req_info->service_function != service_function)
887 switch (service_function) {
889 case BT_GATT_SERVER_REGISTER: {
890 bt_service_app_info_t *param = (bt_service_app_info_t*)data;
891 BT_INFO("Sender [%s] Request Sender [%s]", param->sender, req_info->sender);
892 if (!g_strcmp0(req_info->sender, param->sender)) {
893 BT_DBG("GATT Server app found [%s]", req_info->sender);
895 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
896 g_array_append_vals(out_param, ¶m->instance_id, sizeof(int));
897 _bt_service_method_return(req_info->context, out_param, result);
899 g_free(req_info->user_data);
900 _bt_free_info_from_invocation_list(req_info);
901 g_array_free(out_param, TRUE);
905 case BT_GATT_SERVER_START_SERVICE:
906 case BT_GATT_SERVER_DELETE_SERVICE: {
907 bt_service_app_info_t *param = (bt_service_app_info_t*)data;
908 BT_INFO("Sender [%s] Request Sender [%s]", param->sender, req_info->sender);
910 int *saved_instance_id = (int*)req_info->user_data;
911 if (!g_strcmp0(req_info->sender, param->sender) && param->instance_id == *saved_instance_id) {
912 BT_DBG("GATT Server app found [%s] Instance ID [%d] Reply DBUS",
913 req_info->sender, *saved_instance_id);
915 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
916 g_array_append_vals(out_param, &saved_instance_id, sizeof(int));
917 _bt_service_method_return(req_info->context, out_param, result);
919 g_free(req_info->user_data);
920 _bt_free_info_from_invocation_list(req_info);
921 g_array_free(out_param, TRUE);
925 case BT_GATT_SERVER_ADD_SERVICE:
926 case BT_GATT_SERVER_ADD_DESCRIPTOR:
927 case BT_GATT_SERVER_ADD_CHARACTERISTIC: {
928 int *handle = (int*)data;
929 BT_INFO("Characteristic added: Handle [%d]", *handle);
930 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
932 g_array_append_vals(out_param, handle, sizeof(int));
933 _bt_service_method_return(req_info->context, out_param, result);
935 g_free(req_info->user_data);
936 _bt_free_info_from_invocation_list(req_info);
937 g_array_free(out_param, TRUE);
942 case BT_DISCONNECT_LE: {
943 #ifdef TIZEN_GATT_CLIENT
944 char *addr = (char*)req_info->user_data;
945 bluetooth_device_address_t address;
947 if (!g_strcmp0(addr, (char*)data)) {
948 BT_INFO("GATT Client connect-disconnect call pending for app [%s] addr [%s]",
949 req_info->sender, addr);
950 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
951 _bt_convert_addr_string_to_type(address.addr, addr);
953 g_array_append_vals(out_param, (bluetooth_device_address_t*)&address,
954 sizeof(bluetooth_device_address_t));
955 _bt_service_method_return(req_info->context, out_param, result);
957 g_free(req_info->user_data);
958 _bt_free_info_from_invocation_list(req_info);
959 g_array_free(out_param, TRUE);
962 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
964 g_array_append_vals(out_param, (bluetooth_device_address_t*)data,
965 sizeof(bluetooth_device_address_t));
966 _bt_service_method_return(req_info->context, out_param, result);
968 g_free(req_info->user_data);
969 _bt_free_info_from_invocation_list(req_info);
971 g_array_free(out_param, TRUE);
975 #ifdef TIZEN_GATT_CLIENT
976 case BT_GATT_CLIENT_REGISTER: {
977 bt_service_app_info_t *param = (bt_service_app_info_t*)data;
978 BT_INFO("Sender [%s] Request Sender [%s]", param->sender, req_info->sender);
979 if (!g_strcmp0(req_info->sender, param->sender)) {
980 BT_DBG("GATT Client app found [%s] created client ID [%d]",
981 req_info->sender, param->client_id);
983 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
984 g_array_append_vals(out_param, ¶m->client_id, sizeof(int));
985 _bt_service_method_return(req_info->context, out_param, result);
987 g_free(req_info->user_data);
988 _bt_free_info_from_invocation_list(req_info);
989 g_array_free(out_param, TRUE);
993 case BT_GATT_GET_PRIMARY_SERVICES: {
994 bt_services_browse_info_t *param = (bt_services_browse_info_t*)data;
995 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
996 _bt_convert_addr_type_to_string(address,
997 (unsigned char *)(¶m->device_addr.addr));
999 BT_INFO("Request Sender [%s] address [%s]", req_info->sender, address);
1001 /* Match address to determine same request */
1002 if (!g_strcmp0((char*)req_info->user_data, address)) {
1003 BT_DBG("GATT Client app found [%s] Remote address [%s]",
1004 req_info->sender, address);
1006 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1007 g_array_append_vals(out_param, param,
1008 sizeof(bt_services_browse_info_t));
1010 //g_array_append_vals(out_param, ¶m->client_id, sizeof(int));
1011 _bt_service_method_return(req_info->context, out_param, result);
1013 g_free(req_info->user_data);
1014 _bt_free_info_from_invocation_list(req_info);
1015 g_array_free(out_param, TRUE);
1020 case BT_GATT_GET_SERVICE_PROPERTIES: {
1021 bt_char_browse_info_t param;
1022 memcpy((void*)¶m, data, sizeof(bt_char_browse_info_t));
1023 //bt_char_browse_info_t *param = (bt_char_browse_info_t*)data;
1024 BT_INFO("Request Sender [%s]", req_info->sender);
1025 bluetooth_gatt_client_svc_prop_info_t *prop = (bluetooth_gatt_client_svc_prop_info_t*)req_info->user_data;
1027 /* Match both address and service properties to determine same request */
1028 if (!memcmp(param.device_addr.addr,
1029 prop->device_address.addr,
1030 sizeof(bluetooth_device_address_t)) &&
1031 !memcmp(param.svc_uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1032 param.svc_inst_id == prop->svc.instance_id) {
1033 BT_INFO("Returning Service properties");
1035 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1036 g_array_append_vals(out_param, ¶m, sizeof(bt_char_browse_info_t));
1037 _bt_service_method_return(req_info->context, out_param, result);
1039 g_free(req_info->user_data);
1040 _bt_free_info_from_invocation_list(req_info);
1041 g_array_free(out_param, TRUE);
1045 case BT_GATT_GET_CHARACTERISTIC_PROPERTIES: {
1046 bt_descriptor_browse_info_t *param = (bt_descriptor_browse_info_t*)data;
1048 BT_INFO("Request Sender [%s]", req_info->sender);
1049 bluetooth_gatt_client_char_prop_info_t *prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
1051 /* Match both address, service properties &char properties to determine same request */
1052 if (!memcmp(param->device_addr.addr,
1053 prop->device_address.addr,
1054 sizeof(bluetooth_device_address_t)) &&
1055 !memcmp(param->svc_uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1056 param->svc_inst_id == prop->svc.instance_id &&
1057 !memcmp(param->char_uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1058 param->char_inst_id == prop->characteristic.instance_id) {
1059 BT_INFO("Returning Characteristic properties");
1060 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1061 g_array_append_vals(out_param, param, sizeof(bt_descriptor_browse_info_t));
1062 _bt_service_method_return(req_info->context, out_param, result);
1064 g_free(req_info->user_data);
1065 _bt_free_info_from_invocation_list(req_info);
1066 g_array_free(out_param, TRUE);
1070 case BT_GATT_WATCH_CHARACTERISTIC: {
1071 bt_gatt_notif_reg_info_t *param = (bt_gatt_notif_reg_info_t*)data;
1072 bluetooth_gatt_client_char_prop_info_t *prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
1073 BT_INFO("Request Sender [%s]", req_info->sender);
1074 /* Match both address, service properties &char properties to determine same request */
1075 if (!memcmp(param->addr.addr,
1076 prop->device_address.addr,
1077 sizeof(bluetooth_device_address_t)) &&
1078 !memcmp(param->svc_uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1079 param->svc_inst == prop->svc.instance_id &&
1080 !memcmp(param->char_uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1081 param->char_inst == prop->characteristic.instance_id) {
1082 BT_INFO("Characteristic Watch Successful: Is registered [%d]",
1083 param->is_registered);
1084 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1085 g_array_append_vals(out_param, param, sizeof(bt_gatt_notif_reg_info_t));
1086 _bt_service_method_return(req_info->context, out_param, result);
1087 g_free(req_info->user_data);
1088 _bt_free_info_from_invocation_list(req_info);
1089 g_array_free(out_param, TRUE);
1093 case BT_GATT_READ_CHARACTERISTIC:
1094 case BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE: {
1095 bluetooth_gatt_client_char_prop_info_t *param = (bluetooth_gatt_client_char_prop_info_t*)data;
1096 BT_INFO("Request Sender [%s]", req_info->sender);
1098 char *addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
1099 bluetooth_gatt_client_char_prop_info_t *prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
1100 _bt_convert_addr_type_to_string(addr, prop->device_address.addr);
1102 if (!memcmp(param, prop, sizeof(bluetooth_gatt_client_char_prop_info_t))) {
1103 BT_INFO("Gatt Char read or write request matched for address [%s]", addr);
1104 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1105 g_array_append_vals(out_param, param, sizeof(bluetooth_gatt_client_char_prop_info_t));
1106 _bt_service_method_return(req_info->context, out_param, result);
1108 g_free(req_info->user_data);
1109 _bt_free_info_from_invocation_list(req_info);
1110 g_array_free(out_param, TRUE);
1115 case BT_GATT_READ_DESCRIPTOR_VALUE:
1116 case BT_GATT_WRITE_DESCRIPTOR_VALUE: {
1117 bluetooth_gatt_client_desc_prop_info_t *param = (bluetooth_gatt_client_desc_prop_info_t*)data;
1118 BT_INFO("Request Sender [%s]", req_info->sender);
1120 char *addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
1121 bluetooth_gatt_client_desc_prop_info_t *prop = (bluetooth_gatt_client_desc_prop_info_t*)req_info->user_data;
1122 _bt_convert_addr_type_to_string(addr, prop->device_address.addr);
1124 if (!memcmp(param, prop, sizeof(bluetooth_gatt_client_desc_prop_info_t))) {
1125 BT_INFO("Descriptor read or write request matched for address [%s]", addr);
1126 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1127 g_array_append_vals(out_param, param, sizeof(bluetooth_gatt_client_desc_prop_info_t));
1128 _bt_service_method_return(req_info->context, out_param, result);
1130 g_free(req_info->user_data);
1131 _bt_free_info_from_invocation_list(req_info);
1132 g_array_free(out_param, TRUE);
1137 case BT_REQ_ATT_MTU: {
1138 char *addr = (char*)req_info->user_data;
1139 bluetooth_device_address_t address;
1141 if (!g_strcmp0(addr, (char*)data)) {
1142 BT_INFO("GATT Client BT_REQ_ATT_MTU call pending for app [%s] addr [%s]",
1143 req_info->sender, addr);
1144 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1145 _bt_convert_addr_string_to_type(address.addr, addr);
1147 g_array_append_vals(out_param, (bluetooth_device_address_t*)&address,
1148 sizeof(bluetooth_device_address_t));
1149 _bt_service_method_return(req_info->context, out_param, result);
1151 g_free(req_info->user_data);
1152 _bt_free_info_from_invocation_list(req_info);
1153 g_array_free(out_param, TRUE);
1164 static void __bt_handle_server_instance_registered(event_gatts_register_t *data)
1166 bt_service_app_info_t *info = NULL;
1168 char *uuid_string = g_malloc0(BT_UUID_STRING_MAX);
1170 _bt_uuid_to_string(&(data->server_uuid), uuid_string);
1171 BT_INFO("Instance ID is Intialized [%d] UUID initialized [%s]", data->server_inst, uuid_string);
1173 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1174 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1177 if (g_strcmp0(info->uuid, uuid_string) == 0) {
1178 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1179 info->is_initialized = TRUE;
1180 info->instance_id = data->server_inst;
1181 info->adv_instance = data->server_inst;
1182 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE, BT_GATT_SERVER_REGISTER,
1183 (void*)info, sizeof(bt_service_app_info_t));
1187 g_free(uuid_string);
1190 static void __bt_handle_gatt_server_service_added(event_gatts_srvc_prm_t *event)
1192 int result = BLUETOOTH_ERROR_NONE;
1196 bt_service_app_info_t *info = NULL;
1199 char *uuid_str = g_malloc0(BT_UUID_STRING_MAX);
1200 _bt_uuid_to_string(&(event->gatt_srvc_id.id.uuid), uuid_str);
1201 BT_INFO("GATT Added Service UUID: [%s] Server Ins [%d] Service hdl [%d] Is Primary [%d]\n",
1202 uuid_str, event->gatt_srvc_stat.server_inst, svc_handle, event->gatt_srvc_id.is_prmry);
1204 if (event->gatt_srvc_stat.status != OAL_STATUS_SUCCESS) {
1205 result = BLUETOOTH_ERROR_INTERNAL;
1206 svc_handle = 0; /* Service handle set to 0 indicates.
1207 0 is reserved by BT SIG, cant be used by app*/
1209 svc_handle = event->gatt_srvc_stat.servic_hndl;
1211 BT_INFO("GATT Added Service Status [%d] Server Ins [%d] Service hdl [%d] Is Primary [%d]\n",
1212 event->gatt_srvc_stat.status, event->gatt_srvc_stat.server_inst, svc_handle, event->gatt_srvc_id.is_prmry);
1214 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1215 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1218 if (info->instance_id == event->gatt_srvc_stat.server_inst) {
1219 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1220 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_ADD_SERVICE,
1221 (int*)&svc_handle, sizeof(int));
1223 /* Add Service Handle */
1224 if (svc_handle > 0) {
1225 handle = g_malloc0(sizeof(int));
1226 *handle = svc_handle;
1227 numapps[event->gatt_srvc_stat.server_inst].service_handles = g_slist_append(numapps[event->gatt_srvc_stat.server_inst].service_handles, handle);
1228 count = g_slist_length(numapps[event->gatt_srvc_stat.server_inst].service_handles);
1229 BT_INFO("Added Service handle [%d] to list..current count [%d]", svc_handle, count);
1238 static void __bt_handle_gatt_server_characteristic_added(event_gatts_srvc_charctr_t *event)
1240 int result = BLUETOOTH_ERROR_NONE;
1242 bt_service_app_info_t *info = NULL;
1244 char *uuid_str = g_malloc0(BT_UUID_STRING_MAX);
1246 BT_INFO("GATT Server Char added status [%d]", event->gatt_srvc_stat.status);
1247 BT_INFO("GATT Server_inst: [%d]", event->gatt_srvc_stat.server_inst);
1248 BT_INFO("GATT Service Handle: [%d]", event->gatt_srvc_stat.servic_hndl);
1249 BT_INFO("GATT Add characteristic Status: [%d]", event->gatt_srvc_stat.status);
1250 BT_INFO("GATT Service characteristic Handle: [%d]", event->charctr_hndl);
1252 if (event->gatt_srvc_stat.status != OAL_STATUS_SUCCESS) {
1253 result = BLUETOOTH_ERROR_INTERNAL;
1254 char_handle = 0; /* characteristic handle set to 0 indicates.
1255 0 is reserved by BT SIG, cant be used by app*/
1257 char_handle = event->charctr_hndl;
1260 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1261 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1264 if (info->instance_id == event->gatt_srvc_stat.server_inst) {
1265 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1266 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_ADD_CHARACTERISTIC,
1267 (int*)&char_handle, sizeof(int));
1273 _bt_uuid_to_string(&(event->charctr_uuid), uuid_str);
1274 BT_INFO("GATT Added Characteristic: UUID: [%s]", uuid_str);
1279 static void __bt_handle_gatt_server_descriptor_added(event_gatts_srvc_descr_t* event)
1281 int result = BLUETOOTH_ERROR_NONE;
1283 bt_service_app_info_t *info = NULL;
1285 char *uuid_str = g_malloc0(BT_UUID_STRING_MAX);
1287 BT_INFO("GATT Server Descriptor added status [%d]", event->gatt_srvc_stat.status);
1288 BT_INFO("GATT Server_inst: [%d]", event->gatt_srvc_stat.server_inst);
1289 BT_INFO("GATT Service Handle: [%d]", event->gatt_srvc_stat.servic_hndl);
1290 BT_INFO("GATT Add Descriptor Status: [%d]", event->gatt_srvc_stat.status);
1291 BT_INFO("GATT Service Descriptor Handle: [%d]", event->descrptr_hndl);
1293 if (event->gatt_srvc_stat.status != OAL_STATUS_SUCCESS) {
1294 result = BLUETOOTH_ERROR_INTERNAL;
1295 desc_handle = 0; /* Service handle set to 0 indicates.
1296 0 is reserved by BT SIG, cant be used by app*/
1298 desc_handle = event->descrptr_hndl;
1300 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1301 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1304 if (info->instance_id == event->gatt_srvc_stat.server_inst) {
1305 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1306 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_ADD_DESCRIPTOR,
1307 (int*)&desc_handle, sizeof(int));
1312 _bt_uuid_to_string(&(event->descrptr_uuid), uuid_str);
1313 BT_INFO("GATT Added Descriptor: UUID: [%s]", uuid_str);
1318 static void __bt_handle_gatt_server_service_started(event_gatts_srvc_t *event)
1320 bt_service_app_info_t *info = NULL;
1321 int result = BLUETOOTH_ERROR_NONE;
1323 BT_INFO("GATT Server Service Started..");
1326 BT_INFO("GATT Server_inst: [%d]", event->server_inst);
1327 BT_INFO("GATT Service Handle: [%d]", event->servic_hndl);
1328 BT_INFO("GATT Service Started Status: [%d]", event->status);
1330 if (event->status != OAL_STATUS_SUCCESS) {
1331 BT_ERR("GATT Server Service Start Failed Err: [%d]", event->status);
1332 result = BLUETOOTH_ERROR_INTERNAL;
1335 /* Check if the just registered Instance ID belongs to requester */
1336 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1339 if (info->instance_id == event->server_inst) {
1340 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1341 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_START_SERVICE,
1342 (void*)info, sizeof(bt_service_app_info_t));
1348 static void __bt_handle_gatt_server_service_stopped(event_gatts_srvc_t *event)
1350 int result = BLUETOOTH_ERROR_NONE;
1351 bt_service_app_info_t *info = NULL;
1353 BT_INFO("GATT Server Service Stopped..");
1355 BT_INFO("GATT Server_inst: [%d]", event->server_inst);
1356 BT_INFO("GATT Service Handle: [%d]", event->servic_hndl);
1357 BT_INFO("GATT Service Stopped Status: [%d]", event->status);
1359 if (event->status != OAL_STATUS_SUCCESS) {
1360 BT_ERR("GATT Server Service Stop Failed Err: [%d]", event->status);
1361 result = BLUETOOTH_ERROR_INTERNAL;
1364 /* Check if the just registered Instance ID belongs to requester */
1365 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1368 if (info->instance_id == event->server_inst) {
1369 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1370 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_STOP_SERVICE,
1371 (void*)info, sizeof(bt_service_app_info_t));
1377 static void __bt_handle_gatt_server_service_deleted(event_gatts_srvc_t *event)
1379 int result = BLUETOOTH_ERROR_NONE;
1380 bt_service_app_info_t *info = NULL;
1382 BT_INFO("GATT Server Service Deleted..");
1384 BT_INFO("GATT Server_inst: [%d]", event->server_inst);
1385 BT_INFO("GATT Service Handle: [%d]", event->servic_hndl);
1386 BT_INFO("GATT Service Deleted Status: [%d]", event->status);
1388 if (event->status != OAL_STATUS_SUCCESS) {
1389 BT_ERR("GATT Server Service Delete Failed Err: [%d]", event->status);
1390 result = BLUETOOTH_ERROR_INTERNAL;
1393 /* Check if the just registered Instance ID belongs to requester */
1394 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1397 if (info->instance_id == event->server_inst) {
1398 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1399 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_DELETE_SERVICE,
1400 (void*)info, sizeof(bt_service_app_info_t));
1406 struct gatt_client_info_t *_bt_find_remote_gatt_client_info(char *address)
1409 struct gatt_client_info_t *info = NULL;
1410 for (l = gatt_client_info_list; l != NULL; l = g_slist_next(l)) {
1411 info = (struct gatt_client_info_t*)l->data;
1415 if (!g_strcmp0(info->addr, address)) {
1416 BT_DBG("Remote GATT client found addr[%s]", info->addr);
1423 static struct gatt_client_info_t *__bt_find_remote_gatt_client_info_from_conn_id(int conn_id)
1426 struct gatt_client_info_t *info = NULL;
1428 for (l = gatt_client_info_list; l != NULL; l = g_slist_next(l)) {
1429 info = (struct gatt_client_info_t*)l->data;
1433 if (info->connection_id == conn_id) {
1434 BT_INFO("Remote GATT client found addr[%s]", info->addr);
1441 #ifdef TIZEN_GATT_CLIENT
1442 struct gatt_server_info_t *_bt_find_remote_gatt_server_info(char *address)
1445 struct gatt_server_info_t *info = NULL;
1446 for (l = gatt_server_info_list; l != NULL; l = g_slist_next(l)) {
1447 info = (struct gatt_server_info_t*)l->data;
1451 if (!g_strcmp0(info->addr, address)) {
1452 BT_DBG("Remote GATT Server found addr[%s]", info->addr);
1459 static struct gatt_out_conn_info_t* __bt_find_gatt_outgoing_conn_info(char *address)
1462 struct gatt_out_conn_info_t *info = NULL;
1463 for (l = outgoing_gatt_conn_list; l != NULL; l = g_slist_next(l)) {
1464 info = (struct gatt_out_conn_info_t*)l->data;
1468 if (!g_strcmp0(info->addr, address)) {
1469 BT_INFO("Outgoing connection info found addr[%s]", info->addr);
1476 static struct gatt_server_info_t *__bt_find_remote_gatt_server_info_from_client_if(int client_if)
1479 struct gatt_server_info_t *info = NULL;
1481 for (l = gatt_server_info_list; l != NULL; l = g_slist_next(l)) {
1482 info = (struct gatt_server_info_t*)l->data;
1486 if (info->client_id == client_if) {
1487 BT_INFO("Remote GATT server found addr[%s]", info->addr);
1498 #ifdef TIZEN_GATT_CLIENT
1499 static void __bt_handle_gatt_server_connection_state(event_gatts_conn_t *event)
1501 int result = BLUETOOTH_ERROR_NONE;
1502 struct gatt_client_info_t *client_info = NULL;
1503 struct gatt_server_info_t *server_info = NULL;
1504 bluetooth_device_address_t dev_addr;
1505 GVariant *param = NULL;
1508 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1510 memcpy(dev_addr.addr, event->address.addr, 6);
1512 /* REPLY dbus Context to application which called BT_CONNECT_LE. There is status
1514 _bt_convert_addr_type_to_string(address,
1515 (unsigned char *)dev_addr.addr);
1517 if (event->status != OAL_STATUS_SUCCESS)
1518 result = BLUETOOTH_ERROR_INTERNAL;
1520 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
1521 address, BT_ADDRESS_STRING_SIZE);
1523 BT_INFO("GATT Server Connedted: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1524 address, event->server_inst, event->conn_id);
1527 /* Check if device is already in connected list */
1528 client_info = _bt_find_remote_gatt_client_info(address);
1531 BT_INFO("Conn Info absent: But no need to Send Local GATT Server Connected event to apps");
1533 param = g_variant_new("(is)", result, address);
1535 /* Send event to application */
1536 _bt_send_event(BT_DEVICE_EVENT,
1537 BLUETOOTH_EVENT_GATT_SERVER_CONNECTED, /* Local device is GATT server */
1540 /* Save client connection info */
1541 client_info = g_new0(struct gatt_client_info_t, 1);
1542 client_info->addr = g_strdup(address);
1543 BT_INFO("Added GATT client addr[%s]", client_info->addr);
1544 client_info->connection_id = event->conn_id;
1545 client_info->instance_id = event->server_inst;
1546 gatt_client_info_list = g_slist_append(gatt_client_info_list, client_info);
1547 BT_INFO("Total num of connected Remote GATT Clients [%d]", g_slist_length(gatt_client_info_list));
1549 /* Save server connection info */
1550 server_info = g_new0(struct gatt_server_info_t, 1);
1551 server_info->addr = g_strdup(address);
1552 server_info->client_id = -1;
1553 BT_INFO("Added GATT server addr[%s]", server_info->addr);
1554 server_info->connection_id = event->conn_id;
1555 gatt_server_info_list = g_slist_append(gatt_server_info_list, server_info);
1556 BT_INFO("Total num of connected Remote GATT Servers [%d]", g_slist_length(gatt_server_info_list));
1558 ret = gattc_add_connection_info((bt_address_t *)&dev_addr, event->conn_id, event->server_inst);
1559 if (ret != OAL_STATUS_SUCCESS) {
1560 BT_ERR("gattc register server instance failed");
1564 __bt_add_mtu_gatt_device(address);
1569 /* GATT Server Dis connected */
1570 static void __bt_handle_gatt_server_disconnection_state(event_gatts_conn_t *event)
1572 int result = BLUETOOTH_ERROR_NONE;
1573 struct gatt_client_info_t *client_info = NULL;
1574 struct gatt_server_info_t *server_info = NULL;
1575 bluetooth_device_address_t dev_addr;
1576 GVariant *param = NULL;
1577 char address[BT_ADDRESS_STRING_SIZE];
1579 memcpy(dev_addr.addr, event->address.addr, 6);
1581 /* REPLY dbus Context to application which called BT_DISCONNECT_LE. There is status
1583 _bt_convert_addr_type_to_string(address,
1584 (unsigned char *)dev_addr.addr);
1586 if (event->status != OAL_STATUS_SUCCESS)
1587 result = BLUETOOTH_ERROR_INTERNAL;
1589 if (NULL == _bt_get_request_info_data(BT_DISCONNECT_LE, address)) {
1590 if (NULL != _bt_get_request_info_data(BT_CONNECT_LE, address)) {
1591 result = BLUETOOTH_ERROR_INTERNAL;
1593 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
1594 address, BT_ADDRESS_STRING_SIZE);
1596 BT_ERR("Failed to connect Local GATT Server Remote Client addr[%s]", address);
1600 __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE,
1601 address, BT_ADDRESS_STRING_SIZE);
1604 BT_INFO("Local GATT Server DisConnected: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1605 address, event->server_inst, event->conn_id);
1606 /* Remove Connection info */
1607 client_info = _bt_find_remote_gatt_client_info(address);
1609 BT_INFO("No need to Send Local GATT Server Disconnected event to apps, just remove remote client info");
1611 param = g_variant_new("(is)", result, address);
1612 /* Send event to application */
1613 _bt_send_event(BT_DEVICE_EVENT,
1614 BLUETOOTH_EVENT_GATT_SERVER_DISCONNECTED, /* Local device is GATT server */
1617 /* Remove server info from list */
1618 server_info = _bt_find_remote_gatt_server_info(address);
1620 gatt_server_info_list = g_slist_remove(gatt_server_info_list, server_info);
1622 BT_INFO("Can not find conn info, already removed!");
1624 /* Remove client info from List */
1625 gatt_client_info_list = g_slist_remove(gatt_client_info_list, client_info);
1626 BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
1627 g_free(client_info->addr);
1628 g_free(client_info);
1631 __bt_remove_mtu_gatt_device(address);
1635 static void __bt_handle_gatt_server_connection_state(event_gatts_conn_t *event)
1637 int result = BLUETOOTH_ERROR_NONE;
1638 struct gatt_client_info_t *conn_info = NULL;
1639 bt_service_app_info_t *info = NULL;
1640 bluetooth_device_address_t dev_addr;
1641 GVariant *param = NULL;
1644 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1646 memcpy(dev_addr.addr, event->address.addr, 6);
1649 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1650 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1653 if (info->instance_id == event->server_inst) {
1654 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1655 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
1656 (bluetooth_device_address_t*)&dev_addr, sizeof(bluetooth_device_address_t));
1662 /* Send event to BT-API */
1663 _bt_convert_addr_type_to_string(address,
1664 (unsigned char *)dev_addr.addr);
1666 BT_INFO("GATT Server Connedted: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1667 address, event->server_inst, event->conn_id);
1670 /* Check if device is already in connected list */
1671 conn_info = _bt_find_remote_gatt_client_info(address);
1674 param = g_variant_new("(is)", result, address);
1675 /* Send event to application */
1676 _bt_send_event(BT_GATT_SERVER_EVENT,
1677 BLUETOOTH_EVENT_GATT_CONNECTED,
1680 /* Save Connection info */
1681 conn_info = g_new0(struct gatt_client_info_t, 1);
1682 conn_info->addr = g_strdup(address);
1683 BT_INFO("Added GATT client addr[%s]", conn_info->addr);
1684 conn_info->connection_id = event->conn_id;
1685 conn_info->instance_id = event->server_inst;
1686 gatt_client_info_list = g_slist_append(gatt_client_info_list, conn_info);
1687 BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
1693 /* GATT Server Dis connected */
1694 static void __bt_handle_gatt_server_disconnection_state(event_gatts_conn_t *event)
1696 int result = BLUETOOTH_ERROR_NONE;
1697 struct gatt_client_info_t *conn_info = NULL;
1698 bluetooth_device_address_t dev_addr;
1699 bt_service_app_info_t *info = NULL;
1700 GVariant *param = NULL;
1703 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1705 memcpy(dev_addr.addr, event->address.addr, 6);
1707 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1708 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1711 if (info->instance_id == event->server_inst) {
1712 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1713 __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE,
1714 (bluetooth_device_address_t*)&dev_addr, sizeof(bluetooth_device_address_t));
1720 /* Send event to BT-API */
1721 _bt_convert_addr_type_to_string(address,
1722 (unsigned char *)dev_addr.addr);
1724 BT_INFO("GATT Server DisConnected: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1725 address, event->server_inst, event->conn_id);
1727 param = g_variant_new("(is)", result, address);
1728 /* Send event to application */
1729 _bt_send_event(BT_GATT_SERVER_EVENT,
1730 BLUETOOTH_EVENT_GATT_DISCONNECTED,
1733 /* Remove Connection info */
1734 conn_info = _bt_find_remote_gatt_client_info(address);
1736 BT_INFO("Remove GATT client info from List..");
1737 /* Remove info from List */
1738 gatt_client_info_list = g_slist_remove(gatt_client_info_list, conn_info);
1739 BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
1740 g_free(conn_info->addr);
1749 static void __bt_handle_gatt_server_acquire_write_requested(event_gatts_srvc_acquire_attr_t *event)
1751 GVariant *param = NULL;
1752 int result = BLUETOOTH_ERROR_NONE;
1753 struct gatt_server_req_info *req_info = NULL;
1754 bluetooth_device_address_t dev_addr;
1755 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1757 BT_INFO("GATT Server ACQUIRE Write Req Connection ID: [%d]", event->attr_trans.conn_id);
1758 BT_INFO("GATT Server ACQUIRE Write Req Transaction ID:[%d]", event->attr_trans.trans_id);
1759 BT_INFO("GATT Server ACQUIRE Write Req Attribute Handle: [%d]", event->attr_trans.attr_handle);
1761 //address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1762 memcpy(dev_addr.addr, event->address.addr, 6);
1764 req_info = g_new0(struct gatt_server_req_info, 1);
1765 req_info->request_id = event->attr_trans.trans_id;
1766 req_info->attribute_handle = event->attr_trans.attr_handle;
1767 req_info->connection_id = event->attr_trans.conn_id;
1768 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_ACQUIRE_WRITE;
1769 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1771 _bt_convert_addr_type_to_string(address,
1772 (unsigned char *)dev_addr.addr);
1774 param = g_variant_new("(iiiiiis)", result,
1775 event->attr_trans.conn_id,
1776 event->attr_trans.trans_id,
1777 event->attr_trans.attr_handle,
1778 event->mtu, event->attr_trans.offset, address);
1779 BT_INFO("GATT Server ACQUIRE Write Req Attribute remote address : [%s]", address);
1781 _bt_send_event(BT_GATT_SERVER_EVENT,
1782 BLUETOOTH_EVENT_GATT_SERVER_ACQUIRE_WRITE,
1787 static void __bt_handle_gatt_server_acquire_notify_requested(event_gatts_srvc_acquire_attr_t *event)
1789 GVariant *param = NULL;
1790 int result = BLUETOOTH_ERROR_NONE;
1791 struct gatt_server_req_info *req_info = NULL;
1792 bluetooth_device_address_t dev_addr;
1793 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1795 BT_INFO("GATT Server ACQUIRE Notify Req Connection ID: [%d]", event->attr_trans.conn_id);
1796 BT_INFO("GATT Server ACQUIRE Notify Req Transaction ID:[%d]", event->attr_trans.trans_id);
1797 BT_INFO("GATT Server ACQUIRE Notify Req Attribute Handle: [%d]", event->attr_trans.attr_handle);
1798 BT_INFO("GATT Server ACQUIRE notify Req address : [%s]", address);
1800 memcpy(dev_addr.addr, event->address.addr, 6);
1801 _bt_convert_addr_type_to_string(address,
1802 (unsigned char *)dev_addr.addr);
1803 BT_INFO("GATT Server ACQUIRE notify Req remote address : [%s]", address);
1805 req_info = g_new0(struct gatt_server_req_info, 1);
1806 req_info->request_id = event->attr_trans.trans_id;
1807 req_info->attribute_handle = event->attr_trans.attr_handle;
1808 req_info->connection_id = event->attr_trans.conn_id;
1809 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_ACQUIRE_NOTIFY;
1810 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1812 param = g_variant_new("(iiiiiis)", result,
1813 event->attr_trans.conn_id,
1814 event->attr_trans.trans_id,
1815 event->attr_trans.attr_handle,
1816 event->mtu, event->attr_trans.offset,
1819 BT_INFO("GATT Server ACQUIRE Notify Req Attribute : ");
1821 _bt_send_event(BT_GATT_SERVER_EVENT,
1822 BLUETOOTH_EVENT_GATT_SERVER_ACQUIRE_NOTIFY,
1826 static void __bt_handle_gatt_server_write_requested(event_gatts_srvc_write_attr_t *event)
1829 bluetooth_device_address_t dev_addr;
1830 GVariant *param = NULL;
1831 int result = BLUETOOTH_ERROR_NONE;
1833 gboolean is_prepare_write;
1835 GVariant *data = NULL;
1837 struct gatt_server_req_info *req_info = NULL;
1838 BT_INFO("GATT Server Write Requested");
1840 memcpy(dev_addr.addr, event->address.addr, 6);
1842 BT_INFO("GATT Server Write Req Connection ID: [%d]", event->attr_trans.conn_id);
1843 BT_INFO("GATT Server Write Req Transaction ID:[%d]", event->attr_trans.trans_id);
1844 BT_INFO("GATT Server Write Req Attribute Handle: [%d]", event->attr_trans.attr_handle);
1845 BT_INFO("GATT Server Write Req Attribute Offset: [%d]", event->attr_trans.offset);
1846 BT_INFO("GATT Server Write Req value length [%d]", event->length);
1847 BT_INFO("GATT Server Write Req needs response: [%d]", event->need_rsp);
1848 BT_INFO("GATT Server Write Req Is Prep: [%d]", event->is_prep);
1850 if (event->length <= 0) {
1851 BT_INFO("GATT Server write requested, but length of attr value is 0");
1855 need_resp = event->need_rsp;
1856 is_prepare_write = event->is_prep;
1858 write_val = g_memdup(&event->value[0], event->length);
1860 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1861 _bt_convert_addr_type_to_string(address,
1862 (unsigned char *)dev_addr.addr);
1864 BT_INFO("GATT Server Write Request from remote client [%s]", address);
1866 if (event->length > 0) {
1867 for (i = 0; i < event->length; i++)
1868 BT_DBG("Data[%d] = [0x%x]", i, event->value[i]);
1870 /* Save Write Request Info */
1871 req_info = g_new0(struct gatt_server_req_info, 1);
1872 req_info->request_id = event->attr_trans.trans_id;
1873 req_info->attribute_handle = event->attr_trans.attr_handle;
1874 req_info->connection_id = event->attr_trans.conn_id;
1875 req_info->addr = address;
1876 req_info->offset = event->attr_trans.offset;
1877 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_WRITE;
1878 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1880 data = g_variant_new_from_data(
1881 G_VARIANT_TYPE_BYTESTRING,
1886 param = g_variant_new("(iiiiiibbsn@ay)", result,
1887 event->attr_trans.conn_id,
1888 event->attr_trans.trans_id,
1889 event->attr_trans.attr_handle,
1890 event->attr_trans.offset,
1898 _bt_send_event(BT_GATT_SERVER_EVENT,
1899 BLUETOOTH_EVENT_GATT_SERVER_VALUE_CHANGED,
1905 static void __bt_handle_gatt_server_read_requested(event_gatts_srvc_read_attr_t *event)
1907 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1908 bluetooth_device_address_t dev_addr;
1909 int result = BLUETOOTH_ERROR_NONE;
1910 struct gatt_server_req_info *req_info = NULL;
1911 GVariant *param = NULL;
1913 BT_INFO("GATT Server Read Requested");
1915 memcpy(dev_addr.addr, event->address.addr, 6);
1917 BT_INFO("GATT Server-Client Connection ID: [%d]", event->attr_trans.conn_id);
1918 BT_INFO("GATT Server-Client Transaction ID: [%d]", event->attr_trans.trans_id);
1919 BT_INFO("GATT Server Attribute Handle: [%d]", event->attr_trans.attr_handle);
1920 BT_INFO("GATT Server Attribute Offset for read: [%d]", event->attr_trans.offset);
1921 BT_INFO("GATT Server Attribute is long: [%d]", event->is_long);
1923 is_long = event->is_long;
1925 /* Save Read Request Info */
1926 req_info = g_new0(struct gatt_server_req_info, 1);
1927 req_info->request_id = event->attr_trans.trans_id;
1928 req_info->attribute_handle = event->attr_trans.attr_handle;
1929 req_info->connection_id = event->attr_trans.conn_id;
1930 req_info->addr = address;
1931 req_info->offset = event->attr_trans.offset;
1932 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_READ;
1933 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1935 /* Send event to BT-API */
1936 _bt_convert_addr_type_to_string(address,
1937 (unsigned char *)dev_addr.addr);
1939 BT_INFO("GATT Server Read Request from remote client [%s]", address);
1941 param = g_variant_new("(iiiiibs)", result,
1942 event->attr_trans.conn_id,
1943 event->attr_trans.trans_id,
1944 event->attr_trans.attr_handle,
1945 event->attr_trans.offset,
1949 _bt_send_event(BT_GATT_SERVER_EVENT,
1950 BLUETOOTH_EVENT_GATT_SERVER_READ_REQUESTED,
1954 static void __bt_handle_gatt_server_indicate_confirmed(event_gatts_ind_cnfrm_t *event)
1956 bluetooth_device_address_t dev_addr;
1957 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1958 int cur_connected_clients;
1959 static int recvd = 0;
1960 gboolean completed = 0;
1961 GVariant *param = NULL;
1963 /* OAL event does provide error, so MW assumes event will never contain wrong data,
1964 incase of any issues, check with OAL */
1965 int result = BLUETOOTH_ERROR_NONE;
1967 memcpy(dev_addr.addr, event->address.addr, 6);
1968 _bt_convert_addr_type_to_string(address,
1969 (unsigned char *)dev_addr.addr);
1971 BT_INFO("Indication sent to GATT client [%s] conn_ ID [%d] transaction ID [%d] Att handle [%d]",
1972 address, event->conn_id, event->trans_id, event->attr_handle);
1975 cur_connected_clients = g_slist_length(gatt_client_info_list);
1976 BT_INFO("Number of connected clients during sending Indication [%d] & current connected count [%d]",
1977 num_indicate_clients, cur_connected_clients);
1980 if (recvd == num_indicate_clients) {
1981 BT_INFO("Gatt indication confirm event for last GATT client.. [%s]", address);
1982 completed = 1; /* Last event */
1983 recvd = 0; /* Reset */
1984 num_indicate_clients = 0;
1987 param = g_variant_new("(isib)",
1993 /* Send event to BT-API */
1994 _bt_send_event(BT_GATT_SERVER_EVENT,
1995 BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_COMPLETED,
1998 BT_INFO("Received Indication confirm for client number [%d]", recvd);
2002 /* Tizen Platform Specific */
2003 static void __bt_handle_gatt_server_notification_changed(event_gatts_notif_t *event)
2005 bluetooth_device_address_t dev_addr;
2006 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2007 GVariant *param = NULL;
2010 /* OAL event does provide error, so MW assumes event will never contain wrong data,
2011 incase of any issues, check with OAL */
2012 int result = BLUETOOTH_ERROR_NONE;
2014 memcpy(dev_addr.addr, event->address.addr, 6);
2015 _bt_convert_addr_type_to_string(address,
2016 (unsigned char *)dev_addr.addr);
2018 BT_INFO("notification_changed [%s] conn_ ID [%d] transaction ID [%d] Att handle [%d] Notify[%d]",
2019 address, event->conn_id, event->trans_id, event->attr_handle, event->notify);
2021 /* Set Notifcation status */
2022 notify = event->notify;
2024 param = g_variant_new("(isib)",
2030 /* Send event to BT-API */
2031 _bt_send_event(BT_GATT_SERVER_EVENT,
2032 BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_STATE_CHANGED,
2038 static void __bt_handle_gatt_mtu_changed_event(event_gatts_mtu_changed_t *event)
2040 int result = BLUETOOTH_ERROR_NONE;
2041 struct gatt_client_info_t *conn_info = NULL;
2042 GVariant *param = NULL;
2044 BT_INFO("GATT Server MTU changed event");
2046 conn_info = __bt_find_remote_gatt_client_info_from_conn_id(event->conn_id);
2047 if (conn_info == NULL) {
2048 BT_ERR("Cant find connection Information");
2051 BT_INFO("Got connection Info GATT client [%s] MTU Size [%d]",
2052 conn_info->addr, event->mtu_size);
2054 __bt_update_mtu_gatt_device(conn_info->addr, event->mtu_size);
2056 param = g_variant_new("(isqy)",
2062 /* Send event to BT-API */
2063 _bt_send_event(BT_GATT_SERVER_EVENT,
2064 BLUETOOTH_EVENT_GATT_ATT_MTU_CHANGED,
2068 static void __bt_gatt_event_handler(int event_type, gpointer event_data)
2070 BT_INFO("OAL event = 0x%x, \n", event_type);
2071 switch (event_type) {
2072 case OAL_EVENT_BLE_SERVER_INSTANCE_INITIALISED: {
2073 BT_INFO("OAL Event: Server Instance Registered");
2074 /* GATT Server Registered event is handled in MAIN thread context */
2075 __bt_handle_server_instance_registered((event_gatts_register_t *)event_data);
2078 case OAL_EVENT_GATTS_SERVICE_ADDED: {
2079 BT_INFO("OAL Event: GATT Service added");
2080 __bt_handle_gatt_server_service_added((event_gatts_srvc_prm_t *)event_data);
2083 case OAL_EVENT_GATTS_CHARACTERISTIC_ADDED: {
2084 BT_INFO("OAL Event: GATT characteristic added");
2085 __bt_handle_gatt_server_characteristic_added((event_gatts_srvc_charctr_t *)event_data);
2088 case OAL_EVENT_GATTS_DESCRIPTOR_ADDED: {
2089 BT_INFO("OAL Event: GATT descriptor added");
2090 __bt_handle_gatt_server_descriptor_added((event_gatts_srvc_descr_t *)event_data);
2093 case OAL_EVENT_GATTS_SERVICE_STARTED: {
2094 BT_INFO("OAL Event: GATT Service started");
2095 __bt_handle_gatt_server_service_started((event_gatts_srvc_t *)event_data);
2098 case OAL_EVENT_GATTS_SERVICE_STOPED: {
2099 BT_INFO("OAL Event: GATT Service stopped");
2100 __bt_handle_gatt_server_service_stopped((event_gatts_srvc_t *)event_data);
2103 case OAL_EVENT_GATTS_SERVICE_DELETED: {
2104 BT_INFO("OAL Event: GATT Service deleted");
2105 __bt_handle_gatt_server_service_deleted((event_gatts_srvc_t *) event_data);
2108 case OAL_EVENT_GATTS_CONNECTION_COMPLETED: {
2109 BT_INFO("OAL Event: GATT Server Connected");
2110 __bt_handle_gatt_server_connection_state((event_gatts_conn_t *)event_data);
2113 case OAL_EVENT_GATTS_DISCONNECTION_COMPLETED: {
2114 BT_INFO("OAL Event: GATT Server Disconnected");
2115 __bt_handle_gatt_server_disconnection_state((event_gatts_conn_t *)event_data);
2118 case OAL_EVENT_GATTS_REQUEST_READ: {
2119 BT_INFO("OAL Event: GATT Server Read Request");
2120 __bt_handle_gatt_server_read_requested((event_gatts_srvc_read_attr_t *)event_data);
2123 case OAL_EVENT_GATTS_REQUEST_WRITE: {
2124 BT_INFO("OAL Event: GATT Server Write Request");
2125 __bt_handle_gatt_server_write_requested((event_gatts_srvc_write_attr_t *)event_data);
2128 case OAL_EVENT_GATTS_REQUEST_ACQUIRE_WRITE: {
2129 BT_INFO("OAL Event: GATT Server Acquire Write Request");
2130 __bt_handle_gatt_server_acquire_write_requested((event_gatts_srvc_acquire_attr_t*)event_data);
2133 case OAL_EVENT_GATTS_REQUEST_ACQUIRE_NOTIFY: {
2134 BT_INFO("OAL Event: GATT ServerAcquire Notify Request");
2135 __bt_handle_gatt_server_acquire_notify_requested((event_gatts_srvc_acquire_attr_t*)event_data);
2138 case OAL_EVENT_GATTS_IND_CONFIRM: {
2139 BT_INFO("OAL Event: GATT Server Indication confirmed");
2140 __bt_handle_gatt_server_indicate_confirmed((event_gatts_ind_cnfrm_t *)event_data);
2143 case OAL_EVENT_GATTS_NOTIFICATION: { /* Tizen Platform Specific */
2144 BT_INFO("OAL Event: GATT Server DisConnected");
2145 __bt_handle_gatt_server_notification_changed((event_gatts_notif_t *)event_data);
2148 case OAL_EVENT_GATTS_MTU_CHANGED: {
2149 BT_INFO("OAL Event: GATT Server MTU changed event callback");
2150 __bt_handle_gatt_mtu_changed_event((event_gatts_mtu_changed_t *)event_data);
2153 #ifdef TIZEN_GATT_CLIENT
2154 case OAL_EVENT_GATTC_REGISTRATION: {
2155 BT_INFO("OAL Event: GATT Client instance Registered");
2156 __bt_handle_client_instance_registered((event_gattc_register_t *) event_data);
2159 case OAL_EVENT_GATTC_CONNECTION_COMPLETED: {
2160 BT_INFO("OAL Event: GATT Client Connected");
2161 __bt_handle_client_connected((event_gattc_conn_t *) event_data);
2164 case OAL_EVENT_GATTC_DISCONNECTION_COMPLETED: {
2165 BT_INFO("OAL Event: GATT Client DisConnected");
2166 __bt_handle_client_disconnected((event_gattc_conn_t *) event_data);
2169 case OAL_EVENT_GATTC_SERVICE_SEARCH_RESULT: {
2170 BT_INFO("OAL Event: GATT Client Service Search Result");
2171 __bt_handle_client_service_search_result((event_gattc_service_result_t *) event_data);
2174 case OAL_EVENT_GATTC_SERVICE_SEARCH_DONE: {
2175 BT_INFO("OAL Event: GATT Client Service Completed");
2176 __bt_handle_client_service_search_completed((event_gattc_conn_status_t *) event_data);
2179 case OAL_EVENT_GATTC_CHARAC_SERACH_RESULT: {
2180 BT_INFO("OAL Event: GATT Client Characteristic Search Result");
2181 __bt_handle_client_characteristic_search_result((event_gattc_characteristic_result_t *) event_data);
2184 case OAL_EVENT_GATTC_DESC_SERACH_RESULT: {
2185 BT_INFO("OAL Event: GATT Client Descriptor Search Result");
2186 __bt_handle_client_descriptor_search_result((event_gattc_descriptor_result_t *) event_data);
2189 case OAL_EVENT_GATTC_READ_CHARAC: {
2190 BT_INFO("OAL Event: GATT Client Characteristic Read Data");
2191 __bt_handle_client_characteristic_read_data((event_gattc_read_data *) event_data);
2194 case OAL_EVENT_GATTC_READ_DESCR: {
2195 BT_INFO("OAL Event: GATT Client Descriptor Read Data");
2196 __bt_handle_client_descriptor_read_data((event_gattc_read_data *) event_data);
2199 case OAL_EVENT_GATTC_WRITE_CHARAC: {
2200 BT_INFO("OAL Event: GATT Client Characteristic Write Data");
2201 __bt_handle_client_characteristic_write_data((event_gattc_write_data *) event_data);
2204 case OAL_EVENT_GATTC_WRITE_DESCR: {
2205 BT_INFO("OAL Event: GATT Client Descriptor Write Data");
2206 __bt_handle_client_descriptor_write_data((event_gattc_write_data *) event_data);
2209 case OAL_EVENT_DEVICE_LE_DISCONNECTED: {
2210 BT_INFO("OAL Event: LE device disconnected");
2211 __bt_hanlde_le_device_disconnection((event_dev_conn_status_t *)event_data);
2214 case OAL_EVENT_GATTC_NOTIFICATION_REGISTERED: {
2215 BT_INFO("OAL Event: GATT Client Notification Registered");
2216 __bt_handle_client_notification_registered((event_gattc_regdereg_notify_t *) event_data, TRUE);
2219 case OAL_EVENT_GATTC_NOTIFICATION_DEREGISTERED: {
2220 BT_INFO("OAL Event: GATT Client Notification Registered");
2221 __bt_handle_client_notification_registered((event_gattc_regdereg_notify_t *) event_data, FALSE);
2224 case OAL_EVENT_GATTC_NOTIFY_DATA: {
2225 BT_INFO("OAL Event: GATT Client Notification Data");
2226 __bt_handle_client_notification_data((event_gattc_notify_data *) event_data);
2229 case OAL_EVENT_GATTC_SERVICE_CHANGED_IND: {
2230 BT_INFO("OAL Event: GATT Client service changed indication");
2231 __bt_handle_client_service_changed_ind((event_gattc_service_changed_data *)event_data);
2234 case OAL_EVENT_GATTC_MTU_EXCHANGE_COMPLETED: {
2235 BT_INFO("OAL Event: GATT Client MTU Exchange Complete");
2236 __bt_handle_client_mtu_exchange_completed((event_gattc_mtu_configured_t *) event_data);
2245 int _bt_gatt_server_add_service(char *sender, int service_type,
2246 int num_handles, char *svc_uuid, int instance_id)
2248 BT_CHECK_PARAMETER(svc_uuid, return);
2249 BT_CHECK_PARAMETER(sender, return);
2250 int ret = OAL_STATUS_SUCCESS;
2252 oal_gatt_srvc_id_t svc_data;
2254 svc_data.is_prmry = service_type;
2255 svc_data.id.inst_id = instance_id;
2257 BT_INFO("Service UUID [%s] Num handles [%d] Instance ID [%d]", svc_uuid, num_handles, instance_id);
2258 _bt_string_to_uuid(svc_uuid, (service_uuid_t*)&svc_data.id.uuid);
2260 ret = gatts_add_service(instance_id, &svc_data, num_handles);
2261 if (ret != OAL_STATUS_SUCCESS) {
2262 BT_ERR("ret: %d", ret);
2263 return BLUETOOTH_ERROR_INTERNAL;
2266 return BLUETOOTH_ERROR_NONE;
2270 int _bt_gatt_server_add_included_service(char *sender, int instance_id,
2271 int service_handle, int included_svc_handle)
2273 BT_CHECK_PARAMETER(sender, return);
2274 int ret = OAL_STATUS_SUCCESS;
2276 ret = gatts_add_included_services(instance_id, service_handle, included_svc_handle);
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_characteristic(char *sender, char *char_uuid,
2285 bluetooth_gatt_server_attribute_params_t *param)
2287 BT_CHECK_PARAMETER(char_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("Char UUID [%s] Instance ID [%d]", char_uuid, param->instance_id);
2295 _bt_string_to_uuid(char_uuid, (service_uuid_t*)&uuid);
2297 BT_INFO("Char permission From API [0x%x]", param->permissions);
2299 ret = gatts_add_characteristics(param->instance_id, param->service_handle, &uuid,
2300 param->properties, (int)param->permissions);
2301 if (ret != OAL_STATUS_SUCCESS) {
2302 BT_ERR("ret: %d", ret);
2303 return BLUETOOTH_ERROR_INTERNAL;
2305 return BLUETOOTH_ERROR_NONE;
2308 int _bt_gatt_server_add_descriptor(char *sender, char *desc_uuid,
2309 bt_gatt_permission_t *param, int service_handle, int instance_id)
2311 BT_CHECK_PARAMETER(desc_uuid, return);
2312 BT_CHECK_PARAMETER(sender, return);
2313 BT_CHECK_PARAMETER(param, return);
2314 int ret = OAL_STATUS_SUCCESS;
2316 oal_uuid_t uuid = {{0} };
2318 BT_INFO("Descriptor UUID [%s] Instance ID [%d] Service handle [%d]",
2319 desc_uuid, service_handle, instance_id);
2321 _bt_string_to_uuid(desc_uuid, (service_uuid_t*)&uuid);
2323 BT_INFO("Descriptor permission From API [0x%x]", *param);
2324 ret = gatts_add_descriptor(instance_id, service_handle, &uuid, (int)*param);
2326 if (ret != OAL_STATUS_SUCCESS) {
2327 BT_ERR("ret: %d", ret);
2328 return BLUETOOTH_ERROR_INTERNAL;
2330 return BLUETOOTH_ERROR_NONE;
2333 int _bt_gatt_server_start_service(char *sender, int service_handle, int instance_id)
2335 BT_CHECK_PARAMETER(sender, return);
2336 int ret = OAL_STATUS_SUCCESS;
2338 ret = gatts_start_service(instance_id, service_handle, BT_GATT_TRANSPORT_LE);
2339 if (ret != OAL_STATUS_SUCCESS) {
2340 BT_ERR("ret: %d", ret);
2341 return BLUETOOTH_ERROR_INTERNAL;
2343 return BLUETOOTH_ERROR_NONE;
2346 int _bt_gatt_server_stop_service(char *sender, int service_handle, int instance_id)
2348 BT_CHECK_PARAMETER(sender, return);
2349 int ret = OAL_STATUS_SUCCESS;
2351 ret = gatts_stop_service(instance_id, service_handle);
2352 if (ret != OAL_STATUS_SUCCESS) {
2353 BT_ERR("ret: %d", ret);
2354 return BLUETOOTH_ERROR_INTERNAL;
2356 return BLUETOOTH_ERROR_NONE;
2359 int _bt_gatt_server_delete_service(char *sender, int service_handle, int instance_id)
2361 BT_CHECK_PARAMETER(sender, return);
2362 int ret = OAL_STATUS_SUCCESS;
2366 ret = gatts_delete_service(instance_id, service_handle);
2367 if (ret != OAL_STATUS_SUCCESS) {
2368 BT_ERR("ret: %d", ret);
2369 return BLUETOOTH_ERROR_INTERNAL;
2372 /* Remove the Service Handle */
2373 for (l = numapps[instance_id].service_handles; l != NULL;) {
2375 l = g_slist_next(l);
2376 if (handle && *handle == service_handle) {
2377 BT_INFO("Remove Service handle [%d]", *handle);
2378 numapps[instance_id].service_handles = g_slist_remove(numapps[instance_id].service_handles, handle);
2384 return BLUETOOTH_ERROR_NONE;
2387 int _bt_gatt_server_send_response(char *sender, bluetooth_gatt_att_data_t *data,
2388 bluetooth_gatt_server_response_params_t *param)
2390 BT_CHECK_PARAMETER(sender, return);
2391 BT_CHECK_PARAMETER(data, return);
2392 BT_CHECK_PARAMETER(param, return);
2393 struct gatt_server_req_info *req_info = NULL;
2394 int ret = OAL_STATUS_SUCCESS;
2396 oal_gatt_response_t response;
2398 BT_INFO("GATT Server Response: Req Type [%d] req_id [%d] status [%d] auth_req [%d] offset[%d] data len[%d]",
2399 param->req_type, param->request_id,
2400 param->response_status, param->auth_req,
2401 data->offset, data->length);
2403 /* Search for matching Request in List */
2404 req_info = __bt_gatt_server_find_request_info(param->request_id, param->req_type);
2406 BT_ERR("GATT Server Req Info not found for current response..return Error");
2407 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
2410 memset(&response, 0x00, sizeof(oal_gatt_response_t));
2412 response.handle = req_info->attribute_handle;
2413 response.attr_value.auth_req = param->auth_req;
2414 response.attr_value.handle = req_info->attribute_handle;
2415 response.attr_value.offset = data->offset;
2416 response.attr_value.len = data->length;
2417 memcpy(&response.attr_value.value, &data->data, data->length);
2420 ret = gatts_send_response(req_info->connection_id, param->request_id,
2421 param->response_status, &response);
2423 if (ret != OAL_STATUS_SUCCESS) {
2424 BT_ERR("ret: %d", ret);
2425 return BLUETOOTH_ERROR_INTERNAL;
2428 BT_INFO("GATT Server Response successfully sent");
2429 /* Remove GATT server request from list */
2430 gatt_server_requests = g_slist_remove(gatt_server_requests, req_info);
2431 g_free(req_info->addr);
2433 return BLUETOOTH_ERROR_NONE;
2436 int _bt_gatt_server_acquire_send_response(char *sender, bluetooth_gatt_server_acquire_response_params_t *param , void *fd_list)
2438 BT_CHECK_PARAMETER(sender, return);
2439 BT_CHECK_PARAMETER(param, return);
2440 struct gatt_server_req_info *req_info = NULL;
2441 int ret = OAL_STATUS_SUCCESS;
2444 BT_INFO("GATT acquire Server Response: Req Type [%d] req_id [%d] fd [%d] mtu[%d]",
2445 param->req_type, param->request_id,
2449 /* Search for matching Request in List */
2450 req_info = __bt_gatt_server_find_request_info(param->request_id, param->req_type);
2452 BT_ERR("GATT acquire Server Req Info not found for current response..return Error");
2453 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
2456 ret = gatt_send_response_acquire(req_info->connection_id, param->request_id, 0, param->fd, param->mtu, fd_list);
2458 if (ret != OAL_STATUS_SUCCESS) {
2459 BT_ERR("ret: %d", ret);
2460 return BLUETOOTH_ERROR_INTERNAL;
2463 BT_INFO("GATT acquire Server Response successfully sent");
2464 /* Remove GATT server request from list */
2465 gatt_server_requests = g_slist_remove(gatt_server_requests, req_info);
2466 g_free(req_info->addr);
2468 return BLUETOOTH_ERROR_NONE;
2473 int _bt_gatt_server_send_indication(char *sender, bluetooth_device_address_t *dev_addr,
2474 bluetooth_gatt_att_data_t *data,
2475 bluetooth_gatt_server_indication_params_t *param)
2477 BT_CHECK_PARAMETER(sender, return);
2478 BT_CHECK_PARAMETER(data, return);
2479 BT_CHECK_PARAMETER(param, return);
2481 gboolean all_send = FALSE;
2482 int ret = OAL_STATUS_SUCCESS;
2483 struct gatt_client_info_t *conn;
2485 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2486 _bt_convert_addr_type_to_string(address, dev_addr->addr);
2488 if (memcmp(dev_addr->addr, BDADDR_ANY, 6) == 0) {
2489 BT_INFO("GATT Server: Send Indication to all connected GATT clients..");
2492 BT_INFO("GATT Server: Send Indication to connected GATT client addr [%s]", address);
2495 /* Attempt to send Notification/Indication to all Connected GATT clients */
2497 ret = __bt_gatt_send_indication_to_all_connected_clients(data, param);
2498 if (ret != OAL_STATUS_SUCCESS) {
2499 BT_ERR("ret: %d", ret);
2501 return BLUETOOTH_ERROR_INTERNAL;
2505 conn = _bt_find_remote_gatt_client_info(address);
2507 ret = gatts_send_indication(param->instance_id, param->atrribute_handle,
2508 conn->connection_id, data->length,
2509 param->need_confirmation, (char *)(&data->data[0]));
2511 if (ret != OAL_STATUS_SUCCESS) {
2512 BT_ERR("ret: %d", ret);
2513 BT_INFO("Indication failed to send to Remote GATT Client [%s]", address);
2515 return BLUETOOTH_ERROR_INTERNAL;
2517 BT_INFO("Indication sent to Remote GATT Client [%s] wait for Notification completed event from OAL", address);
2519 num_indicate_clients = 1;
2520 return BLUETOOTH_ERROR_NONE;
2522 BT_ERR("Remote GATT client [%s] is not connected..Cant send Indication!!", address);
2524 return BLUETOOTH_ERROR_NOT_CONNECTED;
2527 return BLUETOOTH_ERROR_NONE;
2530 int _bt_gatt_server_update_attribute_value(char *sender, int instance_id,
2531 bluetooth_gatt_server_update_value_t *param)
2533 BT_CHECK_PARAMETER(sender, return);
2534 BT_CHECK_PARAMETER(param, return);
2535 int ret = OAL_STATUS_SUCCESS;
2537 oal_gatt_value_t value;
2538 BT_INFO("GATT Server Update value: Instance ID [%d] attr handle [%d] Value len [%d]",
2539 instance_id, param->attribute_handle, param->length);
2542 memset(&value, 0x00, sizeof(oal_gatt_value_t));
2544 value.handle = param->attribute_handle;
2545 value.len = param->length;
2546 memcpy(&value.value, ¶m->data.data, param->length);
2548 ret = gatts_update_att_value(instance_id, &value);
2550 if (ret != OAL_STATUS_SUCCESS) {
2551 BT_ERR("ret: %d", ret);
2552 return BLUETOOTH_ERROR_INTERNAL;
2555 BT_INFO("GATT Server Update Attribute Value successfully");
2556 return BLUETOOTH_ERROR_NONE;
2559 int _bt_request_att_mtu(bluetooth_device_address_t *device_address,
2562 struct gatt_server_info_t *conn_info = NULL;
2564 int ret = OAL_STATUS_SUCCESS;
2566 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
2567 _bt_convert_addr_type_to_string(addr, device_address->addr);
2569 /* Check if remote GATT Server is connected or not */
2570 conn_info = _bt_find_remote_gatt_server_info(addr);
2572 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
2573 conn_info->addr, conn_info->connection_id);
2575 BT_ERR("GATT Server is not yet connected..");
2577 return BLUETOOTH_ERROR_NOT_CONNECTED;
2580 ret = gattc_configure_mtu(conn_info->connection_id, mtu);
2581 if (ret != OAL_STATUS_SUCCESS) {
2582 BT_ERR("ret: %d", ret);
2584 return BLUETOOTH_ERROR_INTERNAL;
2588 return BLUETOOTH_ERROR_NONE;
2591 int _bt_get_att_mtu(bluetooth_device_address_t *address,
2594 BT_CHECK_PARAMETER(address, return);
2595 BT_CHECK_PARAMETER(mtu, return);
2596 struct gatt_client_info_t *conn_info = NULL;
2597 char addr[BT_ADDRESS_STRING_SIZE] = { 0 };
2598 int ret = OAL_STATUS_SUCCESS;
2601 _bt_convert_addr_type_to_string(addr, address->addr);
2603 BT_INFO("Get current MTU size for the remote client:DevAddress:[%s]", addr);
2605 conn_info = _bt_find_remote_gatt_client_info(addr);
2607 BT_INFO("GATT Client [%s] is connected, conn Id [%d] Instance ID [%d]",
2608 conn_info->addr, conn_info->connection_id, conn_info->instance_id);
2610 BT_ERR("GATT Client [%s] is not yet connected..", addr);
2611 return BLUETOOTH_ERROR_NOT_CONNECTED;
2614 ret = gatts_get_att_mtu(conn_info->connection_id, &stack_mtu);
2615 if (ret != OAL_STATUS_SUCCESS) {
2616 BT_ERR("ret: %d", ret);
2617 return BLUETOOTH_ERROR_INTERNAL;
2620 BT_INFO("ATT MTU received from OAL [%d]", stack_mtu);
2621 *mtu = (unsigned int)stack_mtu;
2624 BT_ERR("MTU value is zero, GATT Client [%s] is not yet connected..", addr);
2625 return BLUETOOTH_ERROR_NOT_CONNECTED;
2628 return BLUETOOTH_ERROR_NONE;
2631 #ifdef TIZEN_GATT_CLIENT
2632 /* GATT Client utility static functions */
2633 static bt_gatt_service_info_list_t *__bt_get_service_info_list(int conn_id)
2636 bt_gatt_service_info_list_t *info = NULL;
2638 for (l = list_gatt_info; l != NULL; l = g_slist_next(l)) {
2639 info = (bt_gatt_service_info_list_t *)l->data;
2643 if (info->conn_id == conn_id)
2650 static bt_gatt_service_info_t *__bt_find_matching_service(
2651 bt_gatt_service_info_list_t *svc_list, oal_gatt_srvc_id_t *svc)
2654 bt_gatt_service_info_t *info = NULL;
2656 for (l = svc_list->services; l != NULL; l = g_slist_next(l)) {
2657 info = (bt_gatt_service_info_t *)l->data;
2661 /* Match UUID and instance ID */
2662 if (!memcmp(&svc->id.uuid.uuid, &info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
2663 && (svc->id.inst_id == info->inst_id)) {
2670 static bt_gatt_char_info_t *__bt_find_matching_charc(
2671 bt_gatt_service_info_t *svc_info, oal_gatt_id_t *charc)
2674 bt_gatt_char_info_t *info = NULL;
2676 for (l = svc_info->chars; l != NULL; l = g_slist_next(l)) {
2677 info = (bt_gatt_char_info_t *)l->data;
2681 /* Match UUID and instance ID */
2682 if (!memcmp(&charc->uuid.uuid, &info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
2683 && (charc->inst_id == info->inst_id)) {
2690 static bt_gatt_descriptor_info_t *__bt_find_matching_desc(
2691 bt_gatt_char_info_t *char_info, oal_gatt_id_t *desc)
2694 bt_gatt_descriptor_info_t *info = NULL;
2696 for (l = char_info->descs; l != NULL; l = g_slist_next(l)) {
2697 info = (bt_gatt_descriptor_info_t *)l->data;
2701 /* Match UUID and instance ID */
2702 if (!memcmp(&desc->uuid, &info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
2703 && (desc->inst_id == info->inst_id)) {
2711 static struct gatt_server_info_t *__bt_find_remote_gatt_server_info_from_conn_id(int conn_id)
2714 struct gatt_server_info_t *info = NULL;
2716 for (l = gatt_server_info_list; l != NULL; l = g_slist_next(l)) {
2717 info = (struct gatt_server_info_t*)l->data;
2721 if (info->connection_id == conn_id) {
2722 BT_INFO("Remote GATT server found addr[%s]", info->addr);
2729 static bt_gatt_service_info_t* __bt_find_removed_service(bt_gatt_service_info_list_t *svc_list)
2732 bt_gatt_service_info_t *info = NULL;
2734 for (l = svc_list->services; l != NULL; l = g_slist_next(l)) {
2735 info = (bt_gatt_service_info_t*)l->data;
2739 /* Service is marked a removed */
2740 if (info->is_removed == 1)
2746 static void __bt_remove_service_info_from_list(bt_gatt_service_info_t *svc_info)
2752 bt_gatt_char_info_t *charc = NULL;
2753 bt_gatt_included_service_info_t *incl = NULL;
2754 bt_gatt_descriptor_info_t *desc = NULL;
2756 /* Remove all Characteristic and Descriptors within characteristic */
2757 for (l = svc_info->chars; l != NULL;) {
2758 charc = (bt_gatt_char_info_t*)l->data;
2759 l = g_slist_next(l); /* Incase if l is removed, saving next to l */
2764 /* Inside Characteristic */
2765 for (l1 = charc->descs; l1 != NULL;) {
2767 desc = (bt_gatt_descriptor_info_t*)l1->data;
2768 l1 = g_slist_next(l1);
2773 /* Remove Descriptor */
2774 charc->descs = g_slist_remove(charc->descs, desc);
2777 /* Remove Characteristic */
2778 svc_info->chars = g_slist_remove(svc_info->chars, charc);
2782 /* Remove all Included Services */
2783 for (l2 = svc_info->included_svcs; l2 != NULL;) {
2784 incl = (bt_gatt_included_service_info_t*)l2->data;
2785 l2 = g_slist_next(l2); /* Incase if l is removed, saving next to l */
2790 /* Remove included service */
2791 svc_info->included_svcs = g_slist_remove(svc_info->included_svcs, incl);
2798 static void __bt_build_service_browse_info(int conn_id,
2799 bt_services_browse_info_t* info)
2802 bt_gatt_service_info_list_t *svc_info_list;
2803 bt_gatt_service_info_t *svc_info;
2805 service_uuid_t uuid;
2806 struct gatt_server_info_t *conn_info = NULL;
2808 char uuid_string[BLUETOOTH_UUID_STRING_MAX];
2810 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
2815 _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
2817 svc_info_list = __bt_get_service_info_list(conn_id);
2821 info->count = g_slist_length(svc_info_list->services);
2822 BT_DBG("Total services present in the svc info list for this conn id [%d] is [%d]",
2823 conn_id, info->count);
2825 for (l = svc_info_list->services; l != NULL; l = g_slist_next(l)) {
2826 svc_info = (bt_gatt_service_info_t*)l->data;
2827 if (svc_info == NULL)
2830 memcpy(&uuid.uuid, &svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2831 _bt_uuid_to_string(&uuid, uuid_string);
2833 BT_INFO("[%d] %s [%s]", count, uuid_string, _bt_convert_uuid_to_string(uuid_string));
2835 /* Fill UUID of service */
2836 g_strlcpy(info->uuids[count], uuid_string,
2837 BLUETOOTH_UUID_STRING_MAX);
2839 /* Fill instance ID of service */
2840 info->inst_id[count] = svc_info->inst_id;
2842 /* Fill primary service or not info */
2843 info->primary[count] = svc_info->is_primary;
2845 /* Increment count of services browsed */
2850 static void __bt_build_char_browse_info(int conn_id,
2851 bt_gatt_service_info_t *svc_info,
2852 bt_char_browse_info_t* info)
2855 bt_gatt_char_info_t *char_info;
2856 service_uuid_t uuid;
2858 struct gatt_server_info_t *conn_info = NULL;
2860 char uuid_string[BLUETOOTH_UUID_STRING_MAX];
2862 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
2866 /* Fill default data, this will be required even in case of failure */
2867 _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
2868 memcpy(&info->svc_uuid, svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2869 info->svc_inst_id = svc_info->inst_id;
2871 if (!svc_info->chars) {
2872 BT_ERR("No Chars browsed for address [%s]", conn_info->addr);
2876 info->count = g_slist_length(svc_info->chars);
2878 for (l = svc_info->chars; l != NULL; l = g_slist_next(l)) {
2879 char_info = (bt_gatt_char_info_t*)l->data;
2880 if (char_info == NULL)
2883 memcpy(&uuid.uuid, &char_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2884 _bt_uuid_to_string(&uuid, uuid_string);
2886 /* Fill UUID of characteristic */
2887 g_strlcpy(info->uuids[count], uuid_string,
2888 BLUETOOTH_UUID_STRING_MAX);
2890 /* Fill instance ID of characteristic */
2891 info->inst_id[count] = char_info->inst_id;
2893 /* Fill property of characteristic */
2894 info->props[count] = char_info->props;
2896 /* Increment count of services browsed */
2899 BT_DBG("Total characteristics browsed [%d]", count);
2902 static void __bt_build_descriptor_browse_info(int conn_id,
2903 bt_gatt_service_info_t *svc_info,
2904 bt_gatt_char_info_t *char_info,
2905 bt_descriptor_browse_info_t* info)
2908 bt_gatt_descriptor_info_t *desc_info;
2910 service_uuid_t uuid;
2911 struct gatt_server_info_t *conn_info = NULL;
2913 char uuid_string[BLUETOOTH_UUID_STRING_MAX];
2915 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
2917 /* Fill default data, this will be required even in case of failure */
2918 _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
2919 memcpy(&info->svc_uuid, svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2920 info->svc_inst_id = svc_info->inst_id;
2921 memcpy(&info->char_uuid, char_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2922 info->char_inst_id = char_info->inst_id;
2924 /* Fill property of the parent characteristic of this descriptor */
2925 info->char_props_map = char_info->props;
2927 info->count = g_slist_length(char_info->descs);
2929 if (!char_info->descs) {
2930 BT_ERR("No Descriptors browsed for address [%s]", conn_info->addr);
2934 for (l = char_info->descs; l != NULL; l = g_slist_next(l)) {
2935 desc_info = (bt_gatt_descriptor_info_t*)l->data;
2936 if (desc_info == NULL)
2939 memcpy(&uuid.uuid, &desc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2940 _bt_uuid_to_string(&uuid, uuid_string);
2942 /* Fill UUID of Descriptor */
2943 g_strlcpy(info->uuids[count], uuid_string,
2944 BLUETOOTH_UUID_STRING_MAX);
2946 /* Fill instance ID of Descriptor */
2947 info->inst_id[count] = desc_info->inst_id;
2950 /* Increment count of Descriptor browsed */
2954 BT_INFO("Total descriptors browsed [%d]", count);
2957 static void __bt_free_service_info(bt_gatt_service_info_t *svc)
2959 GSList *ll, *lll, *llll;
2960 bt_gatt_char_info_t *chr = NULL;
2961 bt_gatt_descriptor_info_t *desc = NULL;
2962 bt_gatt_included_service_info_t *incl_svc = NULL;
2964 BT_DBG("Service info Is Prim[%d] Inst ID [%d]", svc->is_primary, svc->inst_id);
2965 /* Delete all chars and its descriptors */
2966 for (ll = svc->chars; ll != NULL; ) {
2967 chr = (bt_gatt_char_info_t *)ll->data;
2968 ll = g_slist_next(ll);
2972 for (lll = chr->descs; lll != NULL; ) {
2973 desc = (bt_gatt_descriptor_info_t *)lll->data;
2974 lll = g_slist_next(lll);
2977 chr->descs = g_slist_remove(chr->descs, desc);
2980 svc->chars = g_slist_remove(svc->chars, chr);
2984 /* Delete all included services */
2985 for (llll = svc->included_svcs; llll != NULL; ) {
2986 incl_svc = (bt_gatt_included_service_info_t *)llll->data;
2987 llll = g_slist_next(llll);
2988 if (incl_svc == NULL)
2990 svc->included_svcs = g_slist_remove(svc->included_svcs, incl_svc);
2995 static void __bt_cleanup_remote_services(struct gatt_server_info_t *conn_info)
2997 bt_gatt_service_info_list_t *svc_info_list = NULL;
2998 bt_gatt_service_info_t *svc = NULL;
3001 svc_info_list = __bt_get_service_info_list(conn_info->connection_id);
3002 if (!svc_info_list) {
3003 BT_INFO("Could not find Svc Info list for the connection ID [%d]",
3004 conn_info->connection_id);
3008 BT_INFO("Start Cleanup of all services. Num Services [%d]", g_slist_length(svc_info_list->services));
3009 for (l = svc_info_list->services; l != NULL; ) {
3010 svc = (bt_gatt_service_info_t *)l->data;
3011 l = g_slist_next(l);
3015 __bt_free_service_info(svc);
3016 svc_info_list->services = g_slist_remove(svc_info_list->services, svc);
3020 list_gatt_info = g_slist_remove(list_gatt_info, svc_info_list);
3021 g_free(svc_info_list);
3024 int _bt_register_gatt_client_instance(const char *sender,
3025 bluetooth_device_address_t *address)
3027 int ret = OAL_STATUS_SUCCESS;
3028 char *uuid_string = NULL;
3033 /* App should ensure that it should not send */
3034 BT_INFO("### Check on which instance GATT Client instance can be initialized....");
3035 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
3036 if (numapps[k].is_initialized == 1) {
3037 BT_INFO("Instance ID [%d] is already in use..Check next slot",
3038 numapps[k].instance_id);
3041 BT_INFO("Time to register GATT client instancer..UUID to be used is [%s] slot [%d]",
3042 uuid_list[slot-1], slot);
3048 BT_ERR("No Slot if free for GATT Client registration..");
3049 return BLUETOOTH_ERROR_REGISTRATION_FAILED;
3052 uuid_string = g_malloc0(BT_UUID_STRING_MAX);
3053 _bt_string_to_uuid(uuid_list[slot-1], (service_uuid_t*)&uuid);
3054 g_strlcpy(uuid_string, uuid_list[slot-1], BT_UUID_STRING_MAX);
3055 BT_INFO("Copied UUID string [%s]", uuid_string);
3057 /* Register GATT Client */
3058 ret = gattc_register(&uuid);
3059 if (ret != OAL_STATUS_SUCCESS) {
3060 BT_ERR("ret: %d", ret);
3061 g_free(uuid_string);
3062 return BLUETOOTH_ERROR_INTERNAL;
3065 BT_INFO("GATT Client registration call successfully accepted by OAL..wait for Instance Initialized event from OAL..");
3067 /* Return & wait for GATT Client Instance Initialization event */
3068 memset(numapps[slot].sender, 0x00, sizeof(numapps[slot].sender));
3069 memset(numapps[slot].uuid, 0x00, sizeof(numapps[slot].uuid));
3071 g_strlcpy(numapps[slot].sender, sender, sizeof(numapps[slot].sender));
3072 g_strlcpy(numapps[slot].uuid, uuid_string, sizeof(numapps[slot].uuid));
3074 /* Address is saved here. When event comes, sender + address are matched for replying pending
3075 request. It is impossible for same sender to have requests with two same addresses */
3076 memcpy(&numapps[slot].address.addr, address->addr, sizeof(bluetooth_device_address_t));
3078 BT_INFO("Slot [%d] occupied", slot);
3079 numapps[slot].is_initialized = TRUE; /* Set initialization to true here itself */
3081 g_free(uuid_string);
3082 return BLUETOOTH_ERROR_NONE;
3088 /* GATT client events */
3089 static void __bt_handle_client_instance_registered(event_gattc_register_t *data)
3091 bt_service_app_info_t *info = NULL;
3093 char *uuid_string = g_malloc0(BT_UUID_STRING_MAX);
3095 _bt_uuid_to_string(&(data->client_uuid), uuid_string);
3096 BT_INFO("CLient ID is Initialized [%d] UUID initialized [%s]", data->client_if, uuid_string);
3098 /* Platform GATT client framwork does not use Default GATT client instance
3099 This GATT client instance is never deregistred in the lifetime of bt-service */
3100 if (g_strcmp0(uuid_string, DEFAULT_GATT_CLIENT_UUID) == 0) {
3101 BT_INFO("Default client Instance Registered");
3102 gatt_default_client = data->client_if;
3103 g_free(uuid_string);
3107 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
3110 if (g_strcmp0(info->uuid, uuid_string) == 0) {
3111 BT_INFO("Found GATT client.. sender [%s] Slot [%d] occupied", info->sender, k);
3112 info->is_initialized = TRUE;
3113 info->client_id = data->client_if;
3114 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE, BT_GATT_CLIENT_REGISTER,
3115 (void*)info, sizeof(bt_service_app_info_t));
3119 g_free(uuid_string);
3122 static void __bt_handle_client_connected(event_gattc_conn_t *event_data)
3124 int result = BLUETOOTH_ERROR_NONE;
3125 struct gatt_server_info_t *conn_info = NULL;
3126 struct gatt_out_conn_info_t *out_conn_info = NULL;
3128 GVariant *param = NULL;
3130 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
3131 _bt_convert_addr_type_to_string(address,
3132 (unsigned char *)event_data->address.addr);
3134 if (event_data->status != OAL_STATUS_SUCCESS)
3135 result = BLUETOOTH_ERROR_INTERNAL;
3137 /* DBUS Return fo BT_CONNECT_LE for all the apps */
3138 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE, address,
3139 BT_ADDRESS_STRING_SIZE);
3141 BT_INFO("Local GATT Client Connected: Remote addr[%s] Client Interface [%d] Connection ID [%d] status[%d]",
3142 address, event_data->client_if, event_data->conn_id, event_data->status);
3144 if (result == BLUETOOTH_ERROR_NONE) {
3145 /* Check if device is already in connected list */
3146 conn_info = _bt_find_remote_gatt_server_info(address);
3149 /* Send event to BT-API */
3150 param = g_variant_new("(is)", result, address);
3151 _bt_send_event(BT_DEVICE_EVENT,
3152 BLUETOOTH_EVENT_GATT_CLIENT_CONNECTED, /* Local device is GATT client */
3155 /* Save Connection info */
3156 conn_info = g_new0(struct gatt_server_info_t, 1);
3157 conn_info->addr = g_strdup(address);
3158 conn_info->client_id = event_data->client_if;
3159 BT_INFO("Added GATT server addr[%s]", conn_info->addr);
3160 conn_info->connection_id = event_data->conn_id;
3161 gatt_server_info_list = g_slist_append(gatt_server_info_list, conn_info);
3162 BT_INFO("Total num of connected Remote GATT server devices [%d]",
3163 g_slist_length(gatt_server_info_list));
3166 BT_INFO("Do a Internal refresh");
3167 if (OAL_STATUS_SUCCESS != gattc_refresh(conn_info->client_id, &event_data->address))
3168 BT_ERR("GATT database refresh failed!!");
3170 BT_INFO("GATT database refresh Success!!");
3173 BT_ERR("Local GATT Client connected event for addr[%s], but device is in connected list already", address);
3175 __bt_add_mtu_gatt_device(address);
3177 BT_ERR("GATT Client Connection failed!!");
3179 /* If outgoing connection Info is present, then remove it */
3180 out_conn_info = __bt_find_gatt_outgoing_conn_info(address);
3181 if (out_conn_info) {
3182 BT_ERR("Outgoing Client connect request was sent");
3183 outgoing_gatt_conn_list = g_slist_remove(outgoing_gatt_conn_list, out_conn_info);
3184 g_free(out_conn_info->addr);
3185 g_free(out_conn_info);
3190 static void __bt_handle_client_disconnected(event_gattc_conn_t *event_data)
3192 int result = BLUETOOTH_ERROR_NONE;
3194 struct gatt_server_info_t *conn_info = NULL;
3195 struct gatt_out_conn_info_t *out_conn_info = NULL;
3197 GVariant *param = NULL;
3199 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
3200 _bt_convert_addr_type_to_string(address,
3201 (unsigned char *)event_data->address.addr);
3203 if (event_data->status != OAL_STATUS_SUCCESS)
3204 result = BLUETOOTH_ERROR_INTERNAL;
3206 if (NULL == _bt_get_request_info_data(BT_DISCONNECT_LE, address)) {
3207 if (NULL != _bt_get_request_info_data(BT_CONNECT_LE, address)) {
3208 result = BLUETOOTH_ERROR_INTERNAL;
3209 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
3210 address, BT_ADDRESS_STRING_SIZE);
3211 BT_ERR("Failed to connect Local GATT Remote addr[%s]", address);
3216 /* DBUS Return for BT_DISCONNECT_LE for all the apps */
3217 __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE, address,
3218 BT_ADDRESS_STRING_SIZE);
3220 BT_INFO("Local GATT Client DisConnected: Remote addr[%s] Client Interface [%d] Connection ID [%d] status [%d]",
3221 address, event_data->client_if, event_data->conn_id, event_data->status);
3223 /* Remove Connection info */
3224 conn_info = _bt_find_remote_gatt_server_info(address);
3227 param = g_variant_new("(is)", result, address);
3228 /* Send event to application */
3229 _bt_send_event(BT_DEVICE_EVENT,
3230 BLUETOOTH_EVENT_GATT_CLIENT_DISCONNECTED,
3233 BT_INFO("Remove GATT server info from List..");
3234 /* Remove all services from info list_gatt_info */
3235 __bt_cleanup_remote_services(conn_info);
3237 /* Remove info from List */
3238 gatt_server_info_list = g_slist_remove(gatt_server_info_list, conn_info);
3239 BT_INFO("Total num of connected GATT servers [%d]", g_slist_length(gatt_server_info_list));
3240 g_free(conn_info->addr);
3243 BT_INFO("Can not find conn info, already removed!");
3245 __bt_remove_mtu_gatt_device(address);
3247 /* If outgoing connection Info is present, then remove it */
3248 out_conn_info = __bt_find_gatt_outgoing_conn_info(address);
3249 if (out_conn_info) {
3250 BT_ERR("Client Disconnected event, but outgoing connect request was sent");
3251 outgoing_gatt_conn_list = g_slist_remove(outgoing_gatt_conn_list, out_conn_info);
3252 g_free(out_conn_info->addr);
3253 g_free(out_conn_info);
3259 static void __bt_handle_client_service_search_result(
3260 event_gattc_service_result_t *event_data)
3262 /* Pre: status is never fail from OAL */
3264 /* Find service list from address */
3265 bt_gatt_service_info_list_t *svc_info_list;
3266 bt_gatt_service_info_t *svc_info;
3268 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3269 if (!svc_info_list) {
3270 BT_DBG("Service info list not present for connection ID %d, means first time browse", event_data->conn_status.conn_id);
3271 /* Means for this conn_id, no services are ever browsed, first time,
3272 create service info list for this conn_id */
3273 svc_info_list = g_malloc0(sizeof(bt_gatt_service_info_list_t));
3274 svc_info_list->conn_id = event_data->conn_status.conn_id;
3275 list_gatt_info = g_slist_append(list_gatt_info, svc_info_list);
3278 /* send list and current service's uuid and instance id to find it */
3279 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3282 /* If not found, check if service changed, if yes, means this is a new service added
3283 in remote GATT device, update uuid info in svc info list structure, to be used when
3284 search is completed */
3286 if (svc_info_list->info.is_changed) {
3287 BT_DBG("Service Changed indication already found for connection ID %d", event_data->conn_status.conn_id);
3288 memcpy(svc_info_list->info.uuid, event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3290 /* Create and add new service in service list */
3291 svc_info = g_malloc0(sizeof(bt_gatt_service_info_t));
3292 memcpy(svc_info->uuid, event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3293 svc_info->inst_id = event_data->srvc_id.id.inst_id;
3294 svc_info->is_primary = event_data->srvc_id.is_prmry;
3295 svc_info_list->services = g_slist_append(svc_info_list->services, svc_info);
3296 BT_DBG("Service created and added in Svc info list or connection ID %d", event_data->conn_status.conn_id);
3298 /* If returned matching service info, then just update service_rmeoved value inside it to 0 */
3299 svc_info->is_removed = 0;
3303 static void __bt_handle_client_service_search_completed(
3304 event_gattc_conn_status_t *event_data)
3306 struct gatt_server_info_t *conn_info = NULL;
3307 bt_gatt_service_info_list_t *svc_info_list;
3308 bt_gatt_service_info_t *svc_info;
3309 bt_services_browse_info_t browse_info;
3310 unsigned char uuid_empty[BLUETOOTH_UUID_HEX_MAX_LEN];
3312 memset(&uuid_empty, 0x00, BLUETOOTH_UUID_HEX_MAX_LEN);
3313 memset(&browse_info, 0x00, sizeof(bt_services_browse_info_t));
3314 BT_INFO("Primary Services browsing completed status[%d] conn ID [%d]",
3315 event_data->status, event_data->conn_id);
3317 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(event_data->conn_id);
3319 svc_info_list = __bt_get_service_info_list(event_data->conn_id);
3320 if (!svc_info_list) {
3321 BT_ERR("No services browsed ever for addr [%s]", conn_info->addr);
3323 /* Just build response and return ERROR */
3324 __bt_build_service_browse_info(event_data->conn_id, &browse_info);
3326 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_INTERNAL,
3327 BT_GATT_GET_PRIMARY_SERVICES, &browse_info,
3328 sizeof(bt_services_browse_info_t));
3332 /* If fail, then send event with error */
3333 if (event_data->status != OAL_STATUS_SUCCESS) {
3334 /* Just build response and return ERROR */
3335 __bt_build_service_browse_info(event_data->conn_id, &browse_info);
3337 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_INTERNAL,
3338 BT_GATT_GET_PRIMARY_SERVICES, &browse_info,
3339 sizeof(bt_services_browse_info_t));
3343 /* If success, then find service info list from address */
3345 /* If svc_changed == 1 and uuid valid, means a new service is added*/
3346 if (svc_info_list->info.is_changed && !memcmp(uuid_empty, svc_info_list->info.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)) {
3347 /* TODO: Send event -Service added with instance ID and UUID of newly added service */
3348 BT_INFO("new service added");
3350 BT_INFO("TODO new service added");
3353 /* If svc_changed == 1 and uuid invalid, then a service is removed */
3354 if (svc_info_list->info.is_changed && memcmp(uuid_empty, svc_info_list->info.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)) {
3355 /* Scan through the service info list to find service with is_removed = 1*/
3356 svc_info = __bt_find_removed_service(svc_info_list);
3358 /* TODO Send event - Service removed with instance ID and UUID of just rmeoved service */
3360 /* Remove that service info from service info list */
3361 svc_info_list->services = g_slist_remove(svc_info_list->services, svc_info);
3363 /* Delete that service completely from svc_info list*/
3364 __bt_remove_service_info_from_list(svc_info);
3368 /* Reset svc_changed = 0, and reset UUID = all 0's */
3369 svc_info_list->info.is_changed = 0;
3370 memset(&svc_info_list->info.uuid, 0x00, BLUETOOTH_UUID_HEX_MAX_LEN);
3372 /* Build Reply and send to service browse primary services request of pending apps */
3373 __bt_build_service_browse_info(event_data->conn_id, &browse_info);
3375 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE,
3376 BT_GATT_GET_PRIMARY_SERVICES, &browse_info,
3377 sizeof(bt_services_browse_info_t));
3381 static void __bt_handle_client_characteristic_search_result(
3382 event_gattc_characteristic_result_t *event_data)
3384 bt_gatt_service_info_list_t *svc_info_list;
3385 bt_gatt_service_info_t *svc_info;
3386 bt_gatt_char_info_t *char_info;
3387 bt_char_browse_info_t browse_info;
3389 memset(&browse_info, 0x00, sizeof(bt_char_browse_info_t));
3392 if (event_data->conn_status.status == OAL_STATUS_SUCCESS) {
3393 /* Find service info list from address */
3394 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3395 if (svc_info_list == NULL) {
3396 BT_ERR("svc_info_list is NULL");
3401 /* Find matching service info from svc info list */
3402 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3403 if (svc_info == NULL) {
3404 BT_ERR("svc_info is NULL");
3408 /* Find Matching char from service info in event */
3409 char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
3411 /* If not found, then add new characteristic and return */
3413 BT_DBG("Add new characteristic");
3414 char_info = g_malloc0(sizeof(bt_gatt_char_info_t));
3415 memcpy(char_info->uuid, event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3417 char_info->inst_id = event_data->char_id.inst_id;
3418 char_info->props = event_data->char_prop;
3419 svc_info->chars = g_slist_append(svc_info->chars, char_info);
3421 /* If found, then return */
3422 BT_DBG("update char property as Characteristic browsed is already present");
3423 char_info->props |= event_data->char_prop;
3426 /* If Not success: Means Charc browse is completed */
3427 /* Find char list from service in event */
3428 /* Find service list from address */
3429 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3430 if (svc_info_list == NULL) {
3431 BT_ERR("svc_info_list is NULL");
3435 /* Find service info from service in event */
3436 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3437 if (svc_info == NULL) {
3438 BT_ERR("svc_info is NULL");
3442 /* Build char list from service in event */
3443 __bt_build_char_browse_info(event_data->conn_status.conn_id,
3444 svc_info, &browse_info);
3446 /* Create response and return by sending event*/
3447 /* Build Reply and send to service browse All Included services request of pending apps */
3448 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE,
3449 BT_GATT_GET_SERVICE_PROPERTIES,
3451 sizeof(bt_char_browse_info_t));
3455 static void __bt_handle_client_descriptor_search_result(
3456 event_gattc_descriptor_result_t *event_data)
3458 bt_gatt_service_info_list_t *svc_info_list;
3459 bt_gatt_service_info_t *svc_info;
3460 bt_gatt_char_info_t *char_info;
3461 bt_gatt_descriptor_info_t *desc_info;
3462 bt_descriptor_browse_info_t browse_info;
3464 BT_INFO("descriptor search result status [%d]", event_data->conn_status.status);
3466 memset(&browse_info, 0x00, sizeof(bt_descriptor_browse_info_t));
3469 if (event_data->conn_status.status == OAL_STATUS_SUCCESS) {
3470 /* Find service list from address */
3471 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3472 if (svc_info_list == NULL) {
3473 BT_ERR("svc_info_list is NULL");
3477 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3478 if (svc_info == NULL) {
3479 BT_ERR("svc_info is NULL");
3483 char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
3484 if (char_info == NULL) {
3485 BT_ERR("char_info is NULL");
3489 desc_info = __bt_find_matching_desc(char_info, &event_data->descr_id);
3490 /* If not found, add new descriptor and return */
3492 desc_info = g_malloc0(sizeof(bt_gatt_descriptor_info_t));
3493 memcpy(desc_info->uuid, event_data->descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3495 desc_info->inst_id = event_data->descr_id.inst_id;
3496 char_info->descs = g_slist_append(char_info->descs, desc_info);
3499 /* If found, then return */
3500 BT_INFO("Characteristic browsed is already presesnt");
3503 /* If Not success */
3504 /* Find service list from address */
3505 /* Find included service list from service in event */
3506 /* Create response and return by sending event*/
3507 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3508 if (svc_info_list == NULL) {
3509 BT_ERR("svc_info_list is NULL");
3513 /* Find service info from service in event */
3514 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3515 if (svc_info == NULL) {
3516 BT_ERR("svc_info is NULL");
3520 /* Find char info from char in event */
3521 char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
3522 if (char_info == NULL) {
3523 BT_ERR("char_info is NULL");
3527 /* Build descriptor list from char in event */
3528 __bt_build_descriptor_browse_info(event_data->conn_status.conn_id,
3529 svc_info, char_info, &browse_info);
3532 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE,
3533 BT_GATT_GET_CHARACTERISTIC_PROPERTIES,
3535 sizeof(bt_descriptor_browse_info_t));
3539 static void __bt_handle_client_characteristic_read_data(
3540 event_gattc_read_data *event_data)
3542 int result = BLUETOOTH_ERROR_NONE;
3543 struct gatt_server_info_t *conn_info = NULL;
3544 bluetooth_gatt_client_char_prop_info_t read_info;
3546 /* Read Information data structures */
3547 GVariant *param = NULL;
3548 GVariant *data = NULL;
3549 GVariant *data_svc_uuid = NULL;
3550 GVariant *data_char_uuid = NULL;
3551 char *read_val = NULL;
3552 char *svc_uuid = NULL;
3553 char *char_uuid = NULL;
3556 //memset(&read_info, 0x00, sizeof(bt_gatt_handle_property_t));
3557 memset(&read_info, 0x00, sizeof(bluetooth_gatt_client_char_prop_info_t));
3559 /* Extract Address from conn_id of event data */
3560 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3561 event_data->uuid_status.conn_status.conn_id);
3563 BT_INFO("Characteristic Read result from addr [%s] status [%d]",
3564 conn_info->addr, event_data->uuid_status.conn_status.status);
3566 /* Fill char in buffer */
3567 memcpy(&read_info.characteristic.uuid,
3568 event_data->uuid_status.char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3569 read_info.characteristic.instance_id = event_data->uuid_status.char_id.inst_id;
3571 /* Fill Service in buffer */
3572 memcpy(&read_info.svc.uuid,
3573 event_data->uuid_status.srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3574 read_info.svc.instance_id = event_data->uuid_status.srvc_id.id.inst_id;
3576 /* Fill remote device address */
3577 _bt_convert_addr_string_to_type(read_info.device_address.addr, conn_info->addr);
3579 /* Fill data and reply to all apps waiting for Read result on the same characteristic
3580 Note: Even in case of failure, address, handles and result code should be returned */
3581 if (event_data->uuid_status.conn_status.status != OAL_STATUS_SUCCESS)
3582 result = BLUETOOTH_ERROR_INTERNAL;
3584 if (event_data->data_len > 0) {
3586 // for (i = 0; i < event_data->data_len; i++)
3587 // BT_INFO("Data[%d] = [0x%x]", i, event_data->data[i]);
3590 read_val = g_memdup(&event_data->data[0], event_data->data_len);
3592 data = g_variant_new_from_data(
3593 G_VARIANT_TYPE_BYTESTRING,
3595 event_data->data_len,
3598 BT_ERR("Characteristic Read success, but no data!!!");
3600 data = g_variant_new_from_data(
3601 G_VARIANT_TYPE_BYTESTRING,
3608 svc_uuid = g_memdup(&event_data->uuid_status.srvc_id.id.uuid.uuid[0], uuid_len);
3610 data_svc_uuid = g_variant_new_from_data(
3611 G_VARIANT_TYPE_BYTESTRING,
3617 char_uuid = g_memdup(&event_data->uuid_status.char_id.uuid.uuid[0], uuid_len);
3619 data_char_uuid = g_variant_new_from_data(
3620 G_VARIANT_TYPE_BYTESTRING,
3625 param = g_variant_new("(isn@ayin@ayin@ay)", result,
3629 event_data->uuid_status.srvc_id.id.inst_id,
3632 event_data->uuid_status.char_id.inst_id,
3633 event_data->data_len,
3637 _bt_send_event(BT_GATT_CLIENT_EVENT,
3638 BLUETOOTH_EVENT_GATT_READ_CHAR,
3641 /* Send DBUS return */
3642 __bt_gatt_handle_pending_request_info(result,
3643 BT_GATT_READ_CHARACTERISTIC,
3645 sizeof(bluetooth_gatt_client_char_prop_info_t));
3656 static void __bt_handle_client_descriptor_read_data(
3657 event_gattc_read_data *event_data)
3659 int result = BLUETOOTH_ERROR_NONE;
3660 struct gatt_server_info_t *conn_info = NULL;
3661 bluetooth_gatt_client_desc_prop_info_t read_info;
3663 /* Read Information data structures */
3664 GVariant *param = NULL;
3665 GVariant *data = NULL;
3666 GVariant *data_svc_uuid = NULL;
3667 GVariant *data_char_uuid = NULL;
3668 GVariant *data_desc_uuid = NULL;
3669 char *read_val = NULL;
3670 char *svc_uuid = NULL;
3671 char *char_uuid = NULL;
3672 char *desc_uuid = NULL;
3677 memset(&read_info, 0x00, sizeof(bluetooth_gatt_client_desc_prop_info_t));
3679 /* Extract Address from conn_id of event data */
3680 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3681 event_data->uuid_status.conn_status.conn_id);
3683 BT_INFO("Descriptor Read result from addr [%s] status [%d]",
3684 conn_info->addr, event_data->uuid_status.conn_status.status);
3686 /* Fill descriptor informations in buffer */
3687 memcpy(&read_info.descriptor.uuid,
3688 event_data->uuid_status.descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3689 read_info.descriptor.instance_id = event_data->uuid_status.descr_id.inst_id;
3691 /* Fill Characteristic informations in buffer */
3692 memcpy(&read_info.characteristic.uuid,
3693 event_data->uuid_status.char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3694 read_info.characteristic.instance_id = event_data->uuid_status.char_id.inst_id;
3696 /* Fill Service informations in buffer */
3697 memcpy(&read_info.svc.uuid,
3698 event_data->uuid_status.srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3699 read_info.svc.instance_id = event_data->uuid_status.srvc_id.id.inst_id;
3701 /* Fill remote device address */
3702 _bt_convert_addr_string_to_type(read_info.device_address.addr, conn_info->addr);
3704 /* Fill data and reply to all apps waiting for Read result on the same characteristic */
3705 if (event_data->uuid_status.conn_status.status != OAL_STATUS_SUCCESS)
3706 result = BLUETOOTH_ERROR_INTERNAL;
3708 BT_INFO("desc data len:%d", event_data->data_len);
3709 if (event_data->data_len > 0) {
3711 for (i = 0; i < event_data->data_len; i++)
3712 BT_DBG("Data[%d] = [0x%x]", i, event_data->data[i]);
3715 read_val = g_memdup(&event_data->data[0], event_data->data_len);
3717 data = g_variant_new_from_data(
3718 G_VARIANT_TYPE_BYTESTRING,
3720 event_data->data_len,
3723 BT_INFO("Descriptor Read success, but no data!!!");
3725 data = g_variant_new_from_data(
3726 G_VARIANT_TYPE_BYTESTRING,
3732 svc_uuid = g_memdup(&event_data->uuid_status.srvc_id.id.uuid.uuid[0], uuid_len);
3734 data_svc_uuid = g_variant_new_from_data(
3735 G_VARIANT_TYPE_BYTESTRING,
3741 char_uuid = g_memdup(&event_data->uuid_status.char_id.uuid.uuid[0], uuid_len);
3743 data_char_uuid = g_variant_new_from_data(
3744 G_VARIANT_TYPE_BYTESTRING,
3750 desc_uuid = g_memdup(&event_data->uuid_status.descr_id.uuid.uuid[0], uuid_len);
3752 data_desc_uuid = g_variant_new_from_data(
3753 G_VARIANT_TYPE_BYTESTRING,
3758 param = g_variant_new("(isn@ayin@ayin@ayin@ay)", result,
3762 event_data->uuid_status.srvc_id.id.inst_id,
3765 event_data->uuid_status.char_id.inst_id,
3768 event_data->uuid_status.descr_id.inst_id,
3769 event_data->data_len,
3773 _bt_send_event(BT_GATT_CLIENT_EVENT,
3774 BLUETOOTH_EVENT_GATT_READ_DESC,
3777 BT_INFO("Send DBUS rpely for GATT Read Descriptor");
3778 /* Send DBUS return */
3779 __bt_gatt_handle_pending_request_info(result,
3780 BT_GATT_READ_DESCRIPTOR_VALUE,
3782 sizeof(bluetooth_gatt_client_desc_prop_info_t));
3793 static void __bt_handle_client_characteristic_write_data(
3794 event_gattc_write_data *event_data)
3796 int result = BLUETOOTH_ERROR_NONE;
3797 struct gatt_server_info_t *conn_info = NULL;
3798 bluetooth_gatt_client_char_prop_info_t write_info;
3800 /* Read Information data structures */
3801 GVariant *param = NULL;
3802 GVariant *data_svc_uuid = NULL;
3803 GVariant *data_char_uuid = NULL;
3804 char *svc_uuid = NULL;
3805 char *char_uuid = NULL;
3809 memset(&write_info, 0x00, sizeof(bluetooth_gatt_client_char_prop_info_t));
3811 /* Extract Address from conn_id of event data */
3812 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3813 event_data->conn_status.conn_id);
3815 BT_INFO("Characteristic Write callback from addr [%s] status [%d]",
3816 conn_info->addr, event_data->conn_status.status);
3818 /* Fill char in buffer */
3819 memcpy(&write_info.characteristic.uuid,
3820 event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3821 write_info.characteristic.instance_id = event_data->char_id.inst_id;
3823 /* Fill Service in buffer */
3824 memcpy(&write_info.svc.uuid,
3825 event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3826 write_info.svc.instance_id = event_data->srvc_id.id.inst_id;
3828 /* Fill remote device address */
3829 _bt_convert_addr_string_to_type(write_info.device_address.addr, conn_info->addr);
3831 if (event_data->conn_status.status != OAL_STATUS_SUCCESS) {
3832 result = BLUETOOTH_ERROR_INTERNAL;
3838 svc_uuid = g_memdup(&event_data->srvc_id.id.uuid.uuid[0], uuid_len);
3840 data_svc_uuid = g_variant_new_from_data(
3841 G_VARIANT_TYPE_BYTESTRING,
3847 char_uuid = g_memdup(&event_data->char_id.uuid.uuid[0], uuid_len);
3849 data_char_uuid = g_variant_new_from_data(
3850 G_VARIANT_TYPE_BYTESTRING,
3855 param = g_variant_new("(isn@ayin@ayi)", result,
3859 event_data->srvc_id.id.inst_id,
3862 event_data->char_id.inst_id);
3865 _bt_send_event(BT_GATT_CLIENT_EVENT,
3866 BLUETOOTH_EVENT_GATT_WRITE_CHAR,
3875 /* Send DBUS return */
3876 __bt_gatt_handle_pending_request_info(result,
3877 BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE,
3879 sizeof(bluetooth_gatt_client_char_prop_info_t));
3883 static void __bt_handle_client_descriptor_write_data(
3884 event_gattc_write_data *event_data)
3886 int result = BLUETOOTH_ERROR_NONE;
3887 struct gatt_server_info_t *conn_info = NULL;
3888 bluetooth_gatt_client_desc_prop_info_t write_info;
3890 /* Write Information data structures */
3891 GVariant *param = NULL;
3892 GVariant *data_svc_uuid = NULL;
3893 GVariant *data_char_uuid = NULL;
3894 GVariant *data_desc_uuid = NULL;
3895 char *svc_uuid = NULL;
3896 char *char_uuid = NULL;
3897 char *desc_uuid = NULL;
3901 memset(&write_info, 0x00, sizeof(bluetooth_gatt_client_desc_prop_info_t));
3903 /* Extract Address from conn_id of event data */
3904 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3905 event_data->conn_status.conn_id);
3907 if (NULL == conn_info) {
3909 BT_INFO("Failed to get the conn info for conn_id [%d]", event_data->conn_status.conn_id);
3913 BT_INFO("Descriptor Write callback from addr [%s] status [%d]",
3914 conn_info->addr, event_data->conn_status.status);
3916 /* Fill descriptor informations in buffer */
3917 memcpy(&write_info.descriptor.uuid,
3918 event_data->descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3919 write_info.descriptor.instance_id = event_data->descr_id.inst_id;
3921 /* Fill Characteristic informations in buffer */
3922 memcpy(&write_info.characteristic.uuid,
3923 event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3924 write_info.characteristic.instance_id = event_data->char_id.inst_id;
3926 /* Fill Service informations in buffer */
3927 memcpy(&write_info.svc.uuid,
3928 event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3929 write_info.svc.instance_id = event_data->srvc_id.id.inst_id;
3931 /* Fill remote device address */
3932 _bt_convert_addr_string_to_type(write_info.device_address.addr, conn_info->addr);
3934 if (event_data->conn_status.status != OAL_STATUS_SUCCESS) {
3935 result = BLUETOOTH_ERROR_INTERNAL;
3941 svc_uuid = g_memdup(&event_data->srvc_id.id.uuid.uuid[0], uuid_len);
3943 data_svc_uuid = g_variant_new_from_data(
3944 G_VARIANT_TYPE_BYTESTRING,
3950 char_uuid = g_memdup(&event_data->char_id.uuid.uuid[0], uuid_len);
3952 data_char_uuid = g_variant_new_from_data(
3953 G_VARIANT_TYPE_BYTESTRING,
3959 desc_uuid = g_memdup(&event_data->descr_id.uuid.uuid[0], uuid_len);
3961 data_desc_uuid = g_variant_new_from_data(
3962 G_VARIANT_TYPE_BYTESTRING,
3967 param = g_variant_new("(isn@ayin@ayin@ayi)", result,
3971 event_data->srvc_id.id.inst_id,
3974 event_data->char_id.inst_id,
3977 event_data->descr_id.inst_id);
3980 _bt_send_event(BT_GATT_CLIENT_EVENT,
3981 BLUETOOTH_EVENT_GATT_WRITE_DESC,
3992 /* Send DBUS return */
3993 __bt_gatt_handle_pending_request_info(result,
3994 BT_GATT_WRITE_DESCRIPTOR_VALUE,
3996 sizeof(bluetooth_gatt_client_desc_prop_info_t));
3999 static void __bt_hanlde_le_device_disconnection(event_dev_conn_status_t *event_data)
4001 int result = BLUETOOTH_ERROR_INTERNAL;
4002 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
4004 _bt_convert_addr_type_to_string(address, (unsigned char *)event_data->address.addr);
4006 /* DBUS Return with fail of pending BT_CONNECT_LE for all the apps */
4007 BT_INFO("Local GATT Client disconnected: Remote addr[%s] ", address);
4009 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE, address,
4010 BT_ADDRESS_STRING_SIZE);
4014 static void __bt_handle_client_notification_registered(
4015 event_gattc_regdereg_notify_t *event_data,
4016 gboolean is_registered)
4018 int result = BLUETOOTH_ERROR_NONE;
4019 struct gatt_server_info_t *conn_info = NULL;
4020 bt_gatt_notif_reg_info_t notif_info;
4023 memset(¬if_info, 0x00, sizeof(bt_gatt_notif_reg_info_t));
4025 BT_INFO("Client Interface [%d] status [%d]",
4026 event_data->client_if,
4027 event_data->status);
4029 /* Extract Address from conn_id of event data */
4030 conn_info = __bt_find_remote_gatt_server_info_from_client_if(
4031 event_data->client_if);
4034 BT_INFO("Connection Info is not present, return");
4037 BT_INFO("Notification Registered for addr [%s]",
4041 /* Fill svc informations in buffer */
4042 memcpy(¬if_info.svc_uuid,
4043 event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4044 notif_info.svc_inst = event_data->srvc_id.id.inst_id;
4046 /* Fill char in buffer */
4047 memcpy(¬if_info.char_uuid,
4048 event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4049 notif_info.char_inst = event_data->char_id.inst_id;
4051 /* Fill remote device address */
4052 _bt_convert_addr_string_to_type(notif_info.addr.addr, conn_info->addr);
4054 notif_info.is_registered = is_registered;
4056 if (event_data->status != OAL_STATUS_SUCCESS)
4057 result = BLUETOOTH_ERROR_INTERNAL;
4059 /* Send DBUS Return for BT_GATT_WATCH_CHARACTERISTIC */
4060 __bt_gatt_handle_pending_request_info(result,
4061 BT_GATT_WATCH_CHARACTERISTIC,
4063 sizeof(bt_gatt_notif_reg_info_t));
4066 static void __bt_handle_client_notification_data(event_gattc_notify_data *event_data)
4068 /* No status in this event from OAL */
4069 int result = BLUETOOTH_ERROR_NONE;
4071 /* Read Information data structures */
4072 GVariant *param = NULL;
4073 GVariant *data = NULL;
4074 GVariant *data_svc_uuid = NULL;
4075 GVariant *data_char_uuid = NULL;
4076 char *read_val = NULL;
4077 char *svc_uuid = NULL;
4078 char *char_uuid = NULL;
4084 BT_INFO("Notifcation of charc data changed");
4086 if (event_data->data_len > 0) {
4088 for (i = 0; i < event_data->data_len; i++)
4089 BT_INFO("Data[%d] = [0x%x]", i, event_data->data[i]);
4092 addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
4093 _bt_convert_addr_type_to_string(addr,
4094 (unsigned char *)&(event_data->address.addr));
4097 read_val = g_memdup(&event_data->data[0], event_data->data_len);
4099 data = g_variant_new_from_data(
4100 G_VARIANT_TYPE_BYTESTRING,
4102 event_data->data_len,
4105 svc_uuid = g_memdup(&event_data->srvc_id.id.uuid.uuid[0], uuid_len);
4107 data_svc_uuid = g_variant_new_from_data(
4108 G_VARIANT_TYPE_BYTESTRING,
4114 char_uuid = g_memdup(&event_data->char_id.uuid.uuid[0], uuid_len);
4116 data_char_uuid = g_variant_new_from_data(
4117 G_VARIANT_TYPE_BYTESTRING,
4123 param = g_variant_new("(isn@ayin@ayin@ay)", result,
4127 event_data->srvc_id.id.inst_id,
4130 event_data->char_id.inst_id,
4131 event_data->data_len,
4135 _bt_send_event(BT_GATT_CLIENT_EVENT,
4136 BLUETOOTH_EVENT_GATT_CHAR_VAL_CHANGED,
4139 BT_ERR("No Data!!");
4152 static void __bt_handle_client_service_changed_ind(event_gattc_service_changed_data *event_data)
4154 bt_gatt_service_info_list_t *svc_info_list;
4156 bt_gatt_service_info_t *svc_info;
4157 GVariant *param = NULL;
4158 char *address_str = NULL;
4159 char *uuid_str = NULL;
4161 svc_info_list = __bt_get_service_info_list(event_data->conn_id);
4162 if (svc_info_list == NULL) {
4163 BT_ERR("svc_info_list is NULL");
4167 if (event_data->change_type) {
4168 /* Add service UUID in list */
4169 svc_info = g_malloc0(sizeof(bt_gatt_service_info_t));
4170 memcpy(svc_info->uuid, event_data->uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4171 svc_info->inst_id = event_data->inst_id;
4172 svc_info->is_primary = 1; // TODO: Need to check is_primary is required or not
4173 svc_info_list->services = g_slist_append(svc_info_list->services, svc_info);
4174 BT_DBG("Service created and added in Svc info list or connection ID %d", event_data->conn_id);
4176 /* Remove service UUID in list */
4177 for (l = svc_info_list->services; l != NULL; l = g_slist_next(l)) {
4178 svc_info = (bt_gatt_service_info_t *)l->data;
4179 if (svc_info == NULL)
4182 if (!memcmp(svc_info->uuid, event_data->uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)) {
4183 svc_info_list->services = g_slist_remove(svc_info_list->services, svc_info);
4184 __bt_free_service_info(svc_info);
4190 address_str = g_malloc0(BT_ADDRESS_STRING_SIZE);
4191 uuid_str = g_malloc0(BT_UUID_STRING_MAX);
4192 _bt_convert_addr_type_to_string(address_str, event_data->address.addr);
4193 _bt_uuid_to_string(&event_data->uuid, uuid_str);
4195 param = g_variant_new("(iiss)", event_data->inst_id, event_data->change_type, address_str, uuid_str);
4197 _bt_send_event(BT_GATT_CLIENT_EVENT,
4198 BLUETOOTH_EVENT_GATT_CLIENT_SERVICE_CHANGED,
4200 g_free(address_str);
4204 gboolean _bt_is_remote_gatt_device_connected(bluetooth_device_address_t *address)
4207 struct gatt_server_info_t *conn_info = NULL;
4208 gboolean connected = FALSE;
4210 addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
4211 _bt_convert_addr_type_to_string(addr,
4212 (unsigned char *)&(address->addr));
4214 BT_DBG("Check GATT connection status of [%s]", addr);
4215 /* Check if device is already in connected list */
4216 conn_info = _bt_find_remote_gatt_server_info(addr);
4219 BT_DBG("Remote GATT Server device [%s] is Connected", conn_info->addr);
4222 struct gatt_client_info_t *client_info = NULL;
4224 BT_INFO("Remote GATT Server Device [%s] is not Connected", addr);
4226 /* Check if device is already in connected list */
4227 client_info = _bt_find_remote_gatt_client_info(addr);
4230 BT_DBG("Remote Client device [%s] is Connected", client_info->addr);
4233 BT_INFO("Remote GATT Client Device [%s] is not Connected", addr);
4242 int _bt_connect_le_device(bluetooth_device_address_t *address,
4243 int auto_connect, int client_id)
4245 struct gatt_server_info_t *conn_info = NULL;
4246 struct gatt_out_conn_info_t *out_conn_info = NULL;
4248 invocation_info_t *req_info = NULL;
4249 int ret = OAL_STATUS_SUCCESS;
4251 char *remote_address = NULL;
4253 BT_CHECK_PARAMETER(address, return);
4255 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4256 _bt_convert_addr_type_to_string(addr, address->addr);
4257 BT_INFO("GATT Client connect request for address [%s] client instance [%d]",
4261 /* Check if Remote Device is already under connection progress */
4262 req_info = _bt_get_request_info_data_from_function_name(BT_CONNECT_LE);
4264 remote_address = (char*)req_info->user_data;
4265 if (remote_address && !strcasecmp(remote_address, addr)) {/* Address matched */
4266 BT_INFO("Already Connection ongoing for same remote GATT Server address [%s]", remote_address);
4267 /* Return and wait for events to be sent to all apps */
4269 return BLUETOOTH_ERROR_IN_PROGRESS;
4273 /* Check if remote GATT Server is connected or not */
4274 conn_info = _bt_find_remote_gatt_server_info(addr);
4276 BT_ERR("GATT Server is already connected..");
4278 return BLUETOOTH_ERROR_ALREADY_CONNECT;
4281 /* TODO Check Requirement of holding Advertisement before initiating LE connect */
4283 /* Check if app sent 0 client id for connection, in such case, use default gatt client ID */
4284 if (client_id == 0) {
4285 BT_INFO("GATT CLient connect request sent by an app without any client instance [%d]",
4287 BT_INFO("Assign default GATT client id [%d]", gatt_default_client);
4288 client_id = gatt_default_client;
4291 BT_INFO("Connect using CLient ID [%d]", client_id);
4292 ret = gattc_connect(client_id, (bt_address_t*)(address), auto_connect);
4294 if (ret != OAL_STATUS_SUCCESS) {
4295 BT_ERR("ret: %d", ret);
4297 return BLUETOOTH_ERROR_INTERNAL;
4300 /* Mark this as outgoing connection */
4301 out_conn_info = g_new0(struct gatt_out_conn_info_t, 1);
4302 out_conn_info->addr = g_strdup(addr);
4303 out_conn_info->client_id = client_id;
4304 BT_INFO("Added outgoing connection info addr[%s]", out_conn_info->addr);
4305 outgoing_gatt_conn_list = g_slist_append(outgoing_gatt_conn_list, out_conn_info);
4308 return BLUETOOTH_ERROR_NONE;
4311 int _bt_gatt_get_primary_services(char *address)
4313 BT_CHECK_PARAMETER(address, return);
4314 struct gatt_server_info_t *conn_info = NULL;
4315 invocation_info_t *req_info = NULL;
4316 int ret = OAL_STATUS_SUCCESS;
4319 /* Check if any app is already browsing primary services on the same remote GATT Server */
4320 req_info = _bt_get_request_info_data(BT_GATT_GET_PRIMARY_SERVICES, address);
4322 BT_INFO("Already Primary Service Browsing ongoing for same rmeote GATT Server");
4323 /* Return and wait for events to be sent to all apps */
4324 return BLUETOOTH_ERROR_NONE;
4327 /* Check if remote GATT Server is connected or not */
4328 conn_info = _bt_find_remote_gatt_server_info(address);
4330 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4331 conn_info->addr, conn_info->connection_id);
4333 BT_ERR("GATT Server is not yet connected..");
4334 return BLUETOOTH_ERROR_NOT_CONNECTED;
4337 BT_INFO("Remove GATT server info from List..");
4338 /* Remove all services from info list_gatt_info */
4339 __bt_cleanup_remote_services(conn_info);
4341 /* Send Primary Service Browsing request to stack */
4342 ret = gattc_search_service(conn_info->connection_id, NULL);
4343 if (ret != OAL_STATUS_SUCCESS) {
4344 BT_ERR("ret: %d", ret);
4345 return BLUETOOTH_ERROR_INTERNAL;
4347 return BLUETOOTH_ERROR_NONE;
4350 int _bt_gatt_get_all_characteristic(bluetooth_gatt_client_svc_prop_info_t *svc)
4352 BT_CHECK_PARAMETER(svc, return);
4353 struct gatt_server_info_t *conn_info = NULL;
4354 invocation_info_t *req_info = NULL;
4355 bluetooth_gatt_client_svc_prop_info_t *prop;
4356 oal_gatt_srvc_id_t srvc_id;
4357 int ret = OAL_STATUS_SUCCESS;
4360 /* Check if any app is already browsing characteristics of the same service on the same remote GATT Server */
4361 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_GET_SERVICE_PROPERTIES);
4363 prop = (bluetooth_gatt_client_svc_prop_info_t*)req_info->user_data;
4364 if (prop && !memcmp(svc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t))
4365 && memcmp(prop->svc.uuid, svc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
4366 && prop->svc.instance_id == svc->svc.instance_id) {
4367 BT_INFO("Already Properties browsing for Primary Service ongoing for same remote GATT Server");
4368 /* Return and wait for events to be sent to all apps */
4369 return BLUETOOTH_ERROR_NONE;
4373 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4374 _bt_convert_addr_type_to_string(addr, svc->device_address.addr);
4376 /* Check if remote GATT Server is connected or not */
4377 conn_info = _bt_find_remote_gatt_server_info(addr);
4379 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4380 conn_info->addr, conn_info->connection_id);
4382 BT_ERR("GATT Server is not yet connected..");
4384 return BLUETOOTH_ERROR_NOT_CONNECTED;
4387 srvc_id.is_prmry = TRUE;
4388 srvc_id.id.inst_id = svc->svc.instance_id;
4389 memcpy(srvc_id.id.uuid.uuid, svc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4391 /* Search All Characteristic */
4392 ret = gattc_get_characteristic(conn_info->connection_id, &srvc_id, NULL);
4393 if (ret != OAL_STATUS_SUCCESS) {
4394 BT_ERR("ret: %d", ret);
4396 return BLUETOOTH_ERROR_INTERNAL;
4399 return BLUETOOTH_ERROR_NONE;
4402 int _bt_gatt_get_all_characteristic_properties(
4403 bluetooth_gatt_client_char_prop_info_t *chr)
4405 struct gatt_server_info_t *conn_info = NULL;
4406 invocation_info_t *req_info = NULL;
4407 bluetooth_gatt_client_char_prop_info_t *prop;
4408 oal_gatt_srvc_id_t srvc_id;
4409 oal_gatt_id_t char_id;
4410 int ret = OAL_STATUS_SUCCESS;
4413 BT_CHECK_PARAMETER(chr, return);
4415 /* Check if any app is already browsing descriptors of the same char of
4416 particular service on the same remote GATT Server */
4417 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_GET_CHARACTERISTIC_PROPERTIES);
4419 prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
4420 if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4421 && memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4422 && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4423 && memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4424 && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
4425 BT_INFO("Already Properties browsing for Characteristic ongoing for same remote GATT Server");
4426 /* Return and wait for events to be sent to all apps */
4427 return BLUETOOTH_ERROR_NONE;
4431 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4432 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4434 /* Check if remote GATT Server is connected or not */
4435 conn_info = _bt_find_remote_gatt_server_info(addr);
4437 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4438 conn_info->addr, conn_info->connection_id);
4440 BT_ERR("GATT Server is not yet connected..");
4442 return BLUETOOTH_ERROR_NOT_CONNECTED;
4445 srvc_id.is_prmry = TRUE;
4446 srvc_id.id.inst_id = chr->svc.instance_id;
4447 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4449 char_id.inst_id = chr->characteristic.instance_id;
4450 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4452 /* Search All Descriptors */
4453 ret = gattc_get_descriptor(conn_info->connection_id, &srvc_id, &char_id, NULL);
4454 if (ret != OAL_STATUS_SUCCESS) {
4455 BT_ERR("ret: %d", ret);
4457 return BLUETOOTH_ERROR_INTERNAL;
4460 return BLUETOOTH_ERROR_NONE;
4463 int _bt_gatt_read_characteristic_value(
4464 bluetooth_gatt_client_char_prop_info_t *chr)
4466 struct gatt_server_info_t *conn_info = NULL;
4467 invocation_info_t *req_info = NULL;
4468 bluetooth_gatt_client_char_prop_info_t *prop;
4469 oal_gatt_srvc_id_t srvc_id;
4470 oal_gatt_id_t char_id;
4471 int ret = OAL_STATUS_SUCCESS;
4474 BT_CHECK_PARAMETER(chr, return);
4476 /* Check if any app is already Reading characteristic of the same char of
4477 particular service on the same remote GATT Server */
4478 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_READ_CHARACTERISTIC);
4480 prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
4481 if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4482 && memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4483 && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4484 && memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4485 && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
4486 BT_INFO("Already Characteristic value Read operation in progress for same remote GATT Server");
4487 /* Return and wait for events to be sent to all apps */
4488 return BLUETOOTH_ERROR_NONE;
4492 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4493 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4495 /* Check if remote GATT Server is connected or not */
4496 conn_info = _bt_find_remote_gatt_server_info(addr);
4498 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4499 conn_info->addr, conn_info->connection_id);
4501 BT_ERR("GATT Server is not yet connected..");
4503 return BLUETOOTH_ERROR_NOT_CONNECTED;
4506 srvc_id.is_prmry = TRUE;
4507 srvc_id.id.inst_id = chr->svc.instance_id;
4508 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4510 char_id.inst_id = chr->characteristic.instance_id;
4511 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4513 /* Search All Descriptors */
4514 ret = gattc_read_characteristic(conn_info->connection_id, &srvc_id, &char_id, OAL_GATT_AUTH_REQ_NONE);
4515 if (ret != OAL_STATUS_SUCCESS) {
4516 BT_ERR("ret: %d", ret);
4518 return BLUETOOTH_ERROR_INTERNAL;
4521 return BLUETOOTH_ERROR_NONE;
4524 int _bt_gatt_read_descriptor_value(
4525 bluetooth_gatt_client_desc_prop_info_t *desc)
4527 struct gatt_server_info_t *conn_info = NULL;
4528 invocation_info_t *req_info = NULL;
4529 bluetooth_gatt_client_desc_prop_info_t *prop;
4530 oal_gatt_srvc_id_t srvc_id;
4531 oal_gatt_id_t char_id;
4532 oal_gatt_id_t desc_id;
4533 int ret = OAL_STATUS_SUCCESS;
4536 BT_CHECK_PARAMETER(desc, return);
4538 /* Check if any app is already Reading descriptors of the same char of
4539 particular service on the same remote GATT Server */
4540 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_READ_DESCRIPTOR_VALUE);
4542 prop = (bluetooth_gatt_client_desc_prop_info_t*)req_info->user_data;
4543 if (prop && !memcmp(desc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4544 && memcmp(desc->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4545 && desc->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4546 && memcmp(desc->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4547 && desc->characteristic.instance_id == prop->characteristic.instance_id /* Characteristic Instance ID matched */
4548 && memcmp(desc->descriptor.uuid, prop->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Descriptor UUID matched */
4549 && desc->descriptor.instance_id == prop->descriptor.instance_id) { /* Descriptor Instance ID matched */
4550 BT_INFO("Already Descriptor value Read operation in progress for same remote GATT Server");
4551 /* Return and wait for events to be sent to all apps */
4552 return BLUETOOTH_ERROR_NONE;
4556 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4557 _bt_convert_addr_type_to_string(addr, desc->device_address.addr);
4559 /* Check if remote GATT Server is connected or not */
4560 conn_info = _bt_find_remote_gatt_server_info(addr);
4562 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4563 conn_info->addr, conn_info->connection_id);
4565 BT_ERR("GATT Server is not yet connected..");
4567 return BLUETOOTH_ERROR_NOT_CONNECTED;
4570 srvc_id.is_prmry = TRUE;
4571 srvc_id.id.inst_id = desc->svc.instance_id;
4572 memcpy(srvc_id.id.uuid.uuid, desc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4574 char_id.inst_id = desc->characteristic.instance_id;
4575 memcpy(char_id.uuid.uuid, desc->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4577 desc_id.inst_id = desc->descriptor.instance_id;
4578 memcpy(desc_id.uuid.uuid, desc->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4580 /* Search All Descriptors */
4581 ret = gattc_read_descriptor(conn_info->connection_id, &srvc_id, &char_id,
4582 &desc_id, OAL_GATT_AUTH_REQ_NONE);
4583 if (ret != OAL_STATUS_SUCCESS) {
4584 BT_ERR("ret: %d", ret);
4586 return BLUETOOTH_ERROR_INTERNAL;
4589 return BLUETOOTH_ERROR_NONE;
4593 int _bt_gatt_acquire_notify(bluetooth_gatt_client_char_prop_info_t *chr, int *fd, int *mtu)
4595 struct gatt_server_info_t *conn_info = NULL;
4596 oal_gatt_srvc_id_t srvc_id;
4597 oal_gatt_id_t char_id;
4598 int ret = OAL_STATUS_SUCCESS;
4601 BT_CHECK_PARAMETER(chr, return);
4603 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4604 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4606 /* Check if remote GATT Server is connected or not */
4607 conn_info = _bt_find_remote_gatt_server_info(addr);
4609 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4610 conn_info->addr, conn_info->connection_id);
4612 BT_ERR("GATT Server is not yet connected..");
4614 return BLUETOOTH_ERROR_NOT_CONNECTED;
4617 srvc_id.is_prmry = TRUE;
4618 srvc_id.id.inst_id = chr->svc.instance_id;
4619 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4621 char_id.inst_id = chr->characteristic.instance_id;
4622 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4624 ret = gattc_acquire_notify(conn_info->connection_id, &srvc_id, &char_id, fd, mtu);
4626 if (ret != OAL_STATUS_SUCCESS) {
4627 BT_ERR("ret: %d", ret);
4629 return BLUETOOTH_ERROR_INTERNAL;
4631 BT_INFO("GATT characterstics FD [%d] mtu[%d]", *fd, *mtu);
4633 return BLUETOOTH_ERROR_NONE;
4637 int _bt_gatt_acquire_write(bluetooth_gatt_client_char_prop_info_t *chr, int *fd, int *mtu)
4640 struct gatt_server_info_t *conn_info = NULL;
4641 oal_gatt_srvc_id_t srvc_id;
4642 oal_gatt_id_t char_id;
4643 int ret = OAL_STATUS_SUCCESS;
4646 BT_CHECK_PARAMETER(chr, return);
4648 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4649 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4651 /* Check if remote GATT Server is connected or not */
4652 conn_info = _bt_find_remote_gatt_server_info(addr);
4654 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4655 conn_info->addr, conn_info->connection_id);
4657 BT_ERR("GATT Server is not yet connected..");
4659 return BLUETOOTH_ERROR_NOT_CONNECTED;
4662 srvc_id.is_prmry = TRUE;
4663 srvc_id.id.inst_id = chr->svc.instance_id;
4664 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4666 char_id.inst_id = chr->characteristic.instance_id;
4667 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4669 ret = gattc_acquire_write(conn_info->connection_id, &srvc_id, &char_id,
4670 OAL_GATT_AUTH_REQ_NONE, fd, mtu);
4671 if (ret != OAL_STATUS_SUCCESS) {
4672 BT_ERR("ret: %d", ret);
4674 return BLUETOOTH_ERROR_INTERNAL;
4676 BT_INFO("GATT characterstics FD [%d] mtu [%d]", *fd, *mtu);
4678 return BLUETOOTH_ERROR_NONE;
4683 /* Write Characteristic */
4684 int _bt_gatt_write_characteristic_value_by_type(
4685 bluetooth_gatt_client_char_prop_info_t *chr,
4686 bluetooth_gatt_att_data_t *data,
4687 bluetooth_gatt_write_type_e write_type)
4689 struct gatt_server_info_t *conn_info = NULL;
4690 invocation_info_t *req_info = NULL;
4691 bluetooth_gatt_client_char_prop_info_t *prop;
4692 oal_gatt_srvc_id_t srvc_id;
4693 oal_gatt_id_t char_id;
4694 int ret = OAL_STATUS_SUCCESS;
4698 BT_CHECK_PARAMETER(chr, return);
4699 BT_CHECK_PARAMETER(data, return);
4703 /* Check if any app is already writing same char of
4704 particular service on the same remote GATT Server */
4705 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE);
4707 prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
4708 if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4709 && memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4710 && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4711 && memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4712 && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
4713 BT_INFO("Already Characteristic Write Value operation in progress for same remote GATT Server");
4714 /* Return and wait for events to be sent to all apps */
4715 return BLUETOOTH_ERROR_NONE;
4719 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4720 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4722 /* Check if remote GATT Server is connected or not */
4723 conn_info = _bt_find_remote_gatt_server_info(addr);
4725 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4726 conn_info->addr, conn_info->connection_id);
4728 BT_ERR("GATT Server is not yet connected..");
4730 return BLUETOOTH_ERROR_NOT_CONNECTED;
4733 srvc_id.is_prmry = TRUE;
4734 srvc_id.id.inst_id = chr->svc.instance_id;
4735 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4737 char_id.inst_id = chr->characteristic.instance_id;
4738 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4740 /* Write CHar value */
4742 BT_INFO("Connection ID [%d] write type [%d] data length [%d]", conn_info->connection_id, write_type, data->length);
4743 for (k = 0; k < data->length; k++)
4744 BT_INFO("Data[%d] [0x%x]", k, data->data[k]);
4746 ret = gattc_write_characteristic(conn_info->connection_id,
4748 (oal_gatt_write_type_t)write_type, data->length,
4749 OAL_GATT_AUTH_REQ_NONE, (char *)(&data->data[0]));
4750 if (ret != OAL_STATUS_SUCCESS) {
4751 BT_ERR("ret: %d", ret);
4753 return BLUETOOTH_ERROR_INTERNAL;
4756 return BLUETOOTH_ERROR_NONE;
4759 /* Write Descriptor */
4760 int _bt_gatt_write_descriptor_value_by_type(
4761 bluetooth_gatt_client_desc_prop_info_t *desc,
4762 bluetooth_gatt_att_data_t *data,
4763 bluetooth_gatt_write_type_e write_type)
4765 struct gatt_server_info_t *conn_info = NULL;
4766 invocation_info_t *req_info = NULL;
4767 bluetooth_gatt_client_desc_prop_info_t *prop;
4768 oal_gatt_srvc_id_t srvc_id;
4769 oal_gatt_id_t char_id;
4770 oal_gatt_id_t desc_id;
4771 int ret = OAL_STATUS_SUCCESS;
4775 BT_CHECK_PARAMETER(desc, return);
4776 BT_CHECK_PARAMETER(data, return);
4780 /* Check if any app is already writing on same Descriptor of the same char of
4781 particular service on the same remote GATT Server */
4782 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_WRITE_DESCRIPTOR_VALUE);
4784 prop = (bluetooth_gatt_client_desc_prop_info_t*)req_info->user_data;
4785 if (prop && !memcmp(desc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4786 && memcmp(desc->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4787 && desc->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4788 && memcmp(desc->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4789 && desc->characteristic.instance_id == prop->characteristic.instance_id /* Characteristic Instance ID matched */
4790 && memcmp(desc->descriptor.uuid, prop->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Descriptor UUID matched */
4791 && desc->descriptor.instance_id == prop->descriptor.instance_id) { /* Descriptor Instance ID matched */
4792 BT_INFO("Already Descriptor value Write operation in progress for same remote GATT Server");
4793 /* Return and wait for events to be sent to all apps */
4794 return BLUETOOTH_ERROR_NONE;
4798 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4799 _bt_convert_addr_type_to_string(addr, desc->device_address.addr);
4801 /* Check if remote GATT Server is connected or not */
4802 conn_info = _bt_find_remote_gatt_server_info(addr);
4804 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4805 conn_info->addr, conn_info->connection_id);
4807 BT_ERR("GATT Server is not yet connected..");
4809 return BLUETOOTH_ERROR_NOT_CONNECTED;
4812 srvc_id.is_prmry = TRUE;
4813 srvc_id.id.inst_id = desc->svc.instance_id;
4814 memcpy(srvc_id.id.uuid.uuid, desc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4816 char_id.inst_id = desc->characteristic.instance_id;
4817 memcpy(char_id.uuid.uuid, desc->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4819 desc_id.inst_id = desc->descriptor.instance_id;
4820 memcpy(desc_id.uuid.uuid, desc->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4823 BT_INFO("Connection ID [%d] write type [%d] data length [%d]", conn_info->connection_id, write_type, data->length);
4824 for (k = 0; k < data->length; k++)
4825 BT_INFO("Data[%d] [0x%x]", k, data->data[k]);
4827 ret = gattc_write_descriptor(conn_info->connection_id,
4828 &srvc_id, &char_id, &desc_id,
4829 (oal_gatt_write_type_t)write_type, data->length,
4830 OAL_GATT_AUTH_REQ_NONE, (char *)(&data->data[0]));
4831 if (ret != OAL_STATUS_SUCCESS) {
4832 BT_ERR("ret: %d", ret);
4834 return BLUETOOTH_ERROR_INTERNAL;
4837 return BLUETOOTH_ERROR_NONE;
4840 int _bt_gatt_watch_characteristic(
4841 bluetooth_gatt_client_char_prop_info_t *chr,
4845 struct gatt_server_info_t *conn_info = NULL;
4846 oal_gatt_srvc_id_t srvc_id;
4847 oal_gatt_id_t char_id;
4848 int ret = OAL_STATUS_SUCCESS;
4851 BT_CHECK_PARAMETER(chr, return);
4853 BT_INFO("Client ID [%d] Is Notify [%d]", client_id, is_notify);
4855 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4856 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4858 /* Check if remote GATT Server is connected or not */
4859 conn_info = _bt_find_remote_gatt_server_info(addr);
4861 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4862 conn_info->addr, conn_info->connection_id);
4864 BT_ERR("GATT Server is not yet connected..");
4866 return BLUETOOTH_ERROR_NOT_CONNECTED;
4868 srvc_id.is_prmry = TRUE;
4869 srvc_id.id.inst_id = chr->svc.instance_id;
4870 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4872 char_id.inst_id = chr->characteristic.instance_id;
4873 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4875 /* Register or unregister Notification characteristic */
4877 ret = gattc_register_for_notification(conn_info->client_id,
4878 (bt_address_t*)&(chr->device_address),
4879 &srvc_id, &char_id);
4881 ret = gattc_deregister_for_notification(conn_info->client_id,
4882 (bt_address_t*)&(chr->device_address),
4883 &srvc_id, &char_id);
4885 BT_INFO("Result[%d]", ret);
4886 if (ret != OAL_STATUS_SUCCESS) {
4887 BT_ERR("ret: %d", ret);
4889 return BLUETOOTH_ERROR_INTERNAL;
4892 return BLUETOOTH_ERROR_NONE;
4896 int _bt_disconnect_le_device(bluetooth_device_address_t *address,
4899 struct gatt_server_info_t *conn_info = NULL;
4900 struct gatt_client_info_t *rem_client_conn_info = NULL;
4901 invocation_info_t *req_info = NULL;
4902 int ret = OAL_STATUS_SUCCESS;
4904 char *remote_address = NULL;
4906 BT_CHECK_PARAMETER(address, return);
4910 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4911 _bt_convert_addr_type_to_string(addr, address->addr);
4912 BT_INFO("GATT Client Disconnect request for address [%s]", addr);
4914 /* Check if Remote Device is already under connection progress */
4915 req_info = _bt_get_request_info_data_from_function_name(BT_DISCONNECT_LE);
4917 remote_address = (char*)req_info->user_data;
4918 if (remote_address && !strcasecmp(remote_address, addr)) {/* Address matched */
4919 BT_INFO("Already DisConnection ongoing for same remote GATT Server address [%s]", remote_address);
4920 /* Return success and wait for events to be sent to all apps */
4922 return BLUETOOTH_ERROR_IN_PROGRESS;
4925 /* Check if remote GATT Server is connected or not */
4926 conn_info = _bt_find_remote_gatt_server_info(addr);
4928 /* Check if app sent 0 client id for Disconnection, in such case, use default gatt client ID */
4929 if (client_id == 0) {
4930 BT_INFO("GATT CLient Disconnect request sent by an app without any client instance [%d]",
4932 BT_INFO("Assign default GATT client id [%d]", gatt_default_client);
4933 client_id = gatt_default_client;
4936 BT_INFO("Disconnect remote gatt server using CLient ID [%d] Connection ID [%d]", client_id, conn_info->connection_id);
4937 ret = gattc_disconnect(client_id, (bt_address_t*)(address),
4938 conn_info->connection_id);
4940 /* check if remote client is connected */
4941 rem_client_conn_info = _bt_find_remote_gatt_client_info(addr);
4943 if (!rem_client_conn_info || client_id != 0) {
4944 BT_ERR("GATT device is not connected..");
4946 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
4949 BT_INFO("Disconnect remote gatt client ");
4951 ret = gatts_disconnect(rem_client_conn_info->instance_id,
4952 (bt_address_t*)(address), rem_client_conn_info->connection_id);
4955 if (ret != OAL_STATUS_SUCCESS) {
4956 BT_ERR("ret: %d", ret);
4958 return BLUETOOTH_ERROR_INTERNAL;
4961 return BLUETOOTH_ERROR_NONE;
4964 int _bt_gatt_watch_service_changed_indication(const char *sender,
4965 bluetooth_device_address_t *address,
4966 gboolean is_enabled)
4969 bt_service_app_info_t *info = NULL;
4971 BT_INFO("%s Servic changed Indication watcher for app [%s]",
4972 is_enabled ? "Enable":"Disable", sender);
4974 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
4977 if (g_strcmp0(sender, info->sender) == 0 &&
4978 memcmp(info->address.addr, address->addr,
4979 sizeof(bluetooth_device_address_t)) == 0) {
4980 BT_DBG("Found GATT client App.. [%s], sender [%s]", info->uuid, info->sender);
4981 info->is_watcher_enabled = is_enabled;
4985 return BLUETOOTH_ERROR_NONE;
4988 int _bt_unregister_gatt_client_instance(const char *sender, int client_id)
4990 bt_service_app_info_t *info = NULL;
4993 BT_INFO("Unregister Allocated GATT Client instance [%s] Client ID [%d]", sender, client_id);
4995 /* Unregister CLient instance associated with address X. It is possible that another app still
4996 has client_id valid for same remote address */
4997 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
5000 /* Exact matching of sender */
5001 if (!g_strcmp0(info->sender, sender) && info->client_id == client_id) { /* Check for only valid GATT client Instance */
5002 BT_INFO("Unregister GATT client instance [%d]", info->client_id);
5003 numapps[k].client_id = -1;
5004 numapps[k].is_initialized = FALSE;
5005 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
5006 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
5007 memset(&numapps[k].address.addr, 0x00, sizeof(bluetooth_device_address_t));
5009 /* Its a GATT Client Instance */
5010 ret = gattc_deregister(client_id);
5011 if (ret != OAL_STATUS_SUCCESS) {
5012 BT_ERR("DeAllocate GATT Client instance with stack Fail ret: %d", ret);
5013 return BLUETOOTH_ERROR_INTERNAL;
5015 return BLUETOOTH_ERROR_NONE;
5020 return BLUETOOTH_ERROR_NOT_FOUND;
5023 static void __bt_handle_client_mtu_exchange_completed(event_gattc_mtu_configured_t *event_data)
5025 int result = BLUETOOTH_ERROR_NONE;
5026 struct gatt_server_info_t *conn_info = NULL;
5027 GVariant *param = NULL;
5031 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(event_data->conn_id);
5032 if (conn_info == NULL) {
5033 BT_ERR("Cant find connection Information");
5037 BT_INFO("GATT Client: MTU Configured from addr [%s] status [%d] MTU size [%d]",
5038 conn_info->addr, event_data->status, event_data->mtu);
5040 if (event_data->status != OAL_STATUS_SUCCESS)
5041 result = BLUETOOTH_ERROR_INTERNAL;
5043 BT_INFO("reply BT_REQ_ATT_MTU ");
5045 /* DBUS Return fo BT_REQ_ATT_MTU for all the apps */
5046 __bt_gatt_handle_pending_request_info(result, BT_REQ_ATT_MTU, conn_info->addr,
5047 BT_ADDRESS_STRING_SIZE);
5049 if (result == BLUETOOTH_ERROR_NONE) {
5050 BT_INFO("sending mtu event");
5052 mtu = event_data->mtu;
5053 param = g_variant_new("(isqy)",
5059 /* Send event to BT-API */
5060 _bt_send_event(BT_DEVICE_EVENT,
5061 BLUETOOTH_EVENT_GATT_ATT_MTU_CHANGED,
5064 /* Update the MTU for current connection */
5065 BT_INFO("Updating the MTU for current connection");
5066 __bt_update_mtu_gatt_device(conn_info->addr, event_data->mtu);
5070 static struct gatt_mtu_info_t *__bt_find_mtu_gatt_device(char *address)
5073 struct gatt_mtu_info_t *info = NULL;
5077 for (l = gatt_mtu_info_list; l != NULL; l = g_slist_next(l)) {
5078 info = (struct gatt_mtu_info_t*)l->data;
5082 if (!g_strcmp0(info->addr, address)) {
5083 BT_INFO("Remote GATT device found addr[%s]", info->addr);
5088 BT_INFO("Not found Remote GATT device addr[%s]", address);
5092 static void __bt_remove_mtu_gatt_device(char *address)
5094 struct gatt_mtu_info_t *dev_info = NULL;
5098 dev_info = __bt_find_mtu_gatt_device(address);
5101 BT_INFO("removing the gatt device from mtu list");
5102 gatt_mtu_info_list = g_slist_remove(gatt_mtu_info_list, dev_info);
5103 g_free(dev_info->addr);
5108 static void __bt_add_mtu_gatt_device(char *address)
5110 struct gatt_mtu_info_t *dev_info = NULL;
5114 dev_info = __bt_find_mtu_gatt_device(address);
5117 BT_INFO("adding the gatt device in mtu list");
5118 dev_info = g_new0(struct gatt_mtu_info_t, 1);
5119 dev_info->addr = g_strdup(address);
5120 dev_info->att_mtu = BT_DEFAULT_ATT_MTU;
5121 gatt_mtu_info_list = g_slist_append(gatt_mtu_info_list, dev_info);
5125 static void __bt_update_mtu_gatt_device(char *address, int mtu)
5127 struct gatt_mtu_info_t *dev_info = NULL;
5131 dev_info = __bt_find_mtu_gatt_device(address);
5134 BT_INFO("Udating the mtu");
5135 dev_info->att_mtu = mtu;