4 * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd.
6 * Contact: Anupam Roy (anupam.r@samsung.com)
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
23 #include "bt-service-common.h"
24 #include "bt-service-core-adapter.h"
25 #include "bt-service-event-receiver.h"
26 #include "bt-request-handler.h"
27 #include "bluetooth-api.h"
29 #include "bluetooth-api.h"
30 #include "bt-internal-types.h"
31 #include "bt-service-util.h"
32 #include "bt-service-common.h"
33 #include "bt-service-event.h"
35 #include "bt-internal-types.h"
36 #include "bt-service-gatt.h"
37 #include "bt-service-gatt.h"
38 #include "bt-service-core-device.h"
39 #include "bt-service-core-adapter-le.h"
40 #ifdef TIZEN_GATT_CLIENT
41 #include "bluetooth-gatt-client-api.h"
44 #include <oal-hardware.h>
45 #include <oal-manager.h>
46 #include <oal-event.h>
47 #include <oal-adapter-mgr.h>
48 #include <oal-device-mgr.h>
51 #define BT_GATT_TRANSPORT_LE 0
52 #define BT_GATT_TRANSPORT_BR_EDR 1
53 #define BT_GATT_TRANSPORT_LE_BR_EDR 2
54 #define BT_UUID_STRING_MAX 64
55 #define BT_SENDER_MAX_LENGTH 50
56 #define MAX_APPS_SUPPORTED 11 /* Slot 0 is not used */
57 #define BT_DEFAULT_ATT_MTU 23
59 #ifdef TIZEN_GATT_CLIENT
66 #define UUID_MAX_LEN 50
69 #define BDADDR_ANY (&(bluetooth_device_address_t) {{0, 0, 0, 0, 0, 0} })
71 #ifdef TIZEN_GATT_CLIENT
72 char uuid_list[NUM_UUID][BT_UUID_STRING_MAX] = {"0000b00b-0000-0000-f065-080080fa49b5", /* Used by BLEAPP */
73 "0000b00b-1111-1111-0123-456789ab0cd2", /* Used by BLEAPP */
74 "0000b00b-2222-1111-0123-456789ab0cd2",
75 "0000b00b-3333-1111-0123-456789ab0cd2",
76 "0000b00b-4444-1111-0123-456789ab0cd2",
77 "0000b00b-5555-1111-0123-456789ab0cd2",
78 "0000b00b-6666-1111-0123-456789ab0cd2",
79 "0000b00b-7777-1111-0123-456789ab0cd2",
80 "0000b00b-8888-1111-0123-456789ab0cd2",
81 "0000b00b-9999-1111-0123-456789ab0cd2",
82 "0000b00b-aaaa-1111-0123-456789ab0cd2",
83 "0000b00b-bbbb-1111-0123-456789ab0cd2",
84 "0000b00b-cccc-1111-0123-456789ab0cd2",
85 "0000b00b-dddd-1111-0123-456789ab0cd2",
86 "0000b00b-eeee-1111-0123-456789ab0cd2",
87 "0000b00b-ffff-1111-0123-456789ab0cd2",
88 "0000b00c-0000-1111-0123-456789ab0cd2",
89 "0000b00c-1111-1111-0123-456789ab0cd2",
90 "0000b00c-2222-1111-0123-456789ab0cd2",
91 "0000b00c-3333-1111-0123-456789ab0cd2"};
93 char uuid_list[NUM_UUID][BT_UUID_STRING_MAX] = {"0000b00b-0000-0000-f065-080080fa49b5", /* Used by BLEAPP */
94 "0000b00b-1111-1111-0123-456789ab0cd2", /* Used by BLEAPP */
95 "0000b00b-2222-1111-0123-456789ab0cd2",
96 "0000b00b-3333-1111-0123-456789ab0cd2",
97 "0000b00b-4444-1111-0123-456789ab0cd2",
98 "0000b00b-5555-1111-0123-456789ab0cd2",
99 "0000b00b-6666-1111-0123-456789ab0cd2",
100 "0000b00b-7777-1111-0123-456789ab0cd2",
101 "0000b00b-8888-1111-0123-456789ab0cd2",
102 "0000b00b-9999-1111-0123-456789ab0cd2"};
105 #ifdef TIZEN_GATT_CLIENT
106 /* Reserved GATT client Instance UUID. This is used only internally by bt-service */
107 #define DEFAULT_GATT_CLIENT_UUID "0000a00a-1111-1111-0123-456789abcdef"
109 static int gatt_default_client = -1;
112 gboolean is_registered;
113 bluetooth_device_address_t addr;
114 unsigned char svc_uuid[BLUETOOTH_UUID_HEX_MAX_LEN];
116 unsigned char char_uuid[BLUETOOTH_UUID_HEX_MAX_LEN];
118 } bt_gatt_notif_reg_info_t;
120 struct gatt_out_conn_info_t {
121 int client_id; /* This value unique identifies a GATT Client instance */
122 char *addr; /* Remote GATT Server address */
125 /* Linked List of outgoing gatt connection list
126 Note: This is valid only for local GATT client */
127 static GSList *outgoing_gatt_conn_list = NULL;
129 /* GATT Server Info(Local Client) List Structure */
130 struct gatt_server_info_t {
131 int connection_id; /* This value will uniquely identify a GATT client-server connection */
132 int client_id; /* This value unique identifies a GATT Client instance */
133 char *addr; /* Remote GATT Server address */
136 struct gatt_client_info_t {
137 int connection_id; /* This value will uniquely identify a GATT client-server connection */
138 int instance_id; /* This value unique identifies a GATT server instance */
139 char *addr; /* Remote GATT client address */
143 * Remove this feature if code is verified
144 * Remove gatt_client/server_info_t and use gatt_conn_info_t
145 * Remove gatt_client/server_info_list and use gatt_conn_info_list
147 #define __INTEGRATE_GATT_INFO__
148 #ifndef __INTEGRATE_GATT_INFO__
149 /* Linked List of connected Remote GATT Servers */
150 static GSList *gatt_server_info_list = NULL;
151 /* Linked List of connected Remote GATT clients */
152 static GSList *gatt_client_info_list = NULL;
154 /* GATT Connection Info List Structure */
155 struct gatt_conn_info_t {
156 char *addr; /* Remote GATT address */
157 int connection_id; /* This value will uniquely identify a GATT client-server connection */
158 int client_id; /* This value unique identifies a GATT Client instance */
159 int instance_id; /* This value unique identifies a GATT Server instance */
162 /* Linked List of connected Remote GATT info */
163 static GSList *gatt_conn_info_list = NULL;
164 #define gatt_server_info_t gatt_conn_info_t
165 #define gatt_client_info_t gatt_conn_info_t
166 #define gatt_server_info_list gatt_conn_info_list
167 #define gatt_client_info_list gatt_conn_info_list
172 unsigned char uuid[BLUETOOTH_UUID_HEX_MAX_LEN]; /* If any service added */
173 } bt_gatt_svc_changed_info_t;
178 // bluetooth_device_address_t address; /* Remote BLE Device Address */
179 GSList *services; /* List of all services of above remote device */
180 int count; /* Number of services browsed from remote device */
181 bt_gatt_svc_changed_info_t info;
182 } bt_gatt_service_info_list_t;
185 unsigned char uuid[BLUETOOTH_UUID_HEX_MAX_LEN];
189 GSList *included_svcs;
190 gboolean is_removed; /* 0 => Service is added, 1=> removed */
191 } bt_gatt_service_info_t;
194 unsigned char uuid[BLUETOOTH_UUID_HEX_MAX_LEN];
198 unsigned char val[BLUETOOTH_GATT_ATT_DATA_LENGTH_MAX];
200 } bt_gatt_char_info_t;
203 unsigned char uuid[BLUETOOTH_UUID_HEX_MAX_LEN];
206 unsigned char val[BLUETOOTH_GATT_ATT_DATA_LENGTH_MAX];
207 } bt_gatt_descriptor_info_t;
210 unsigned char uuid[BLUETOOTH_UUID_HEX_MAX_LEN];
212 } bt_gatt_included_service_info_t;
214 static GSList *list_gatt_info = NULL;
218 /* App Information structure */
221 char sender[BT_SENDER_MAX_LENGTH];
222 char uuid[BT_UUID_STRING_MAX];
225 bluetooth_advertising_data_t adv_data; /* Will store adv data for specific slot */
227 bluetooth_scan_resp_data_t scan_rsp; /* Will store scan rsp data for specific slot */
229 gboolean is_initialized;
230 GSList *service_handles;
231 #ifdef TIZEN_GATT_CLIENT
232 int client_id; /* GATT Client instance ID */
233 bluetooth_device_address_t address; /* Remote BLE Device Address */
234 gboolean is_watcher_enabled;
236 } bt_service_app_info_t;
238 /* GATT Server Request Info Structure */
239 struct gatt_server_req_info {
240 int connection_id; /* This value will uniquely identify a GATT client-server connection */
241 int request_id; /* This is an unique transaction ID assigned against each request by stack */
242 int attribute_handle; /* GATT server attribute handle */
243 int offset; /* GATT server attribute offset on which request is invoked by GATT client */
244 bluetooth_gatt_att_request_type_e request_type; /* Read or Write request */
245 char *addr; /* Remote GATT client address */
248 /* GATT Indicate confirm result */
249 struct gatt_indicate_cfm_result_info_t {
250 int result; /* Result of event */
251 char *addr; /* Remote GATT client address */
252 int att_hdl; /* Characteristic Attribute handle */
253 int completed; /* 1 if last event, otheriwse 0 */
256 /* Request Search Utility method */
257 static struct gatt_server_req_info *__bt_gatt_server_find_request_info(int request_id,
258 bluetooth_gatt_att_request_type_e req_type);
260 static int __bt_gatt_send_indication_to_all_connected_clients(bluetooth_gatt_att_data_t *data,
261 bluetooth_gatt_server_indication_params_t *param);
263 static void __bt_remove_all_service_handles(bt_service_app_info_t *app_info);
265 static void __bt_free_service_info(bt_gatt_service_info_t *service_info);
267 static int __bt_unregister_gatt_client_instance(int client_if);
269 static void __bt_service_reset_gatt_data(void);
271 #ifdef TIZEN_GATT_CLIENT
272 static void __bt_handle_client_instance_registered(event_gattc_register_t *data);
273 static void __bt_handle_client_connected(event_gattc_conn_t *event_data);
274 static void __bt_handle_client_disconnected(event_gattc_conn_t *event_data);
275 static void __bt_handle_client_service_search_completed(event_gattc_conn_status_t *event_data);
276 static void __bt_handle_client_service_search_result(event_gattc_service_result_t *event_data);
277 static void __bt_handle_client_characteristic_search_result(
278 event_gattc_characteristic_result_t *event_data);
279 static void __bt_handle_client_descriptor_search_result(event_gattc_descriptor_result_t *event_data);
280 static void __bt_handle_client_characteristic_read_data(event_gattc_read_data *event_data);
281 static void __bt_handle_client_descriptor_read_data(event_gattc_read_data *event_data);
282 static void __bt_handle_client_characteristic_write_data(event_gattc_write_data *event_data);
283 static void __bt_handle_client_descriptor_write_data(event_gattc_write_data *event_data);
284 static void __bt_hanlde_le_device_disconnection(event_dev_conn_status_t *event_data);
285 static void __bt_handle_client_notification_registered(event_gattc_regdereg_notify_t *event_data,
286 gboolean is_registered);
287 static void __bt_handle_client_notification_data(event_gattc_notify_data *event_data);
288 static void __bt_handle_client_service_changed_ind(event_gattc_service_changed_data *event_data);
289 static void __bt_handle_client_mtu_exchange_completed(event_gattc_mtu_configured_t *event_data);
292 static int __bt_unregister_gatt_server_instance(int server_instance);
295 struct gatt_mtu_info_t {
296 char *addr; /* Remote GATT Server address */
300 static GSList *gatt_mtu_info_list = NULL;
302 static struct gatt_mtu_info_t *__bt_find_mtu_gatt_device(char *address);
303 static void __bt_remove_mtu_gatt_device(char *address);
304 static void __bt_add_mtu_gatt_device(char *address);
305 static void __bt_update_mtu_gatt_device(char *address, int mtu);
307 /* Linked List of GATT requests from Remote GATT Clients */
308 static GSList *gatt_server_requests = NULL;
310 /* Number of clients to be notified to */
311 static int num_indicate_clients;
313 /* List of applications */
314 static bt_service_app_info_t numapps[MAX_APPS_SUPPORTED];
316 static void __bt_gatt_handle_pending_request_info(int result,
317 int service_function, void *data, unsigned int size);
319 static void __bt_handle_server_instance_registered(event_gatts_register_t *data);
321 static void __bt_gatt_event_handler(int event_type, gpointer event_data);
328 void _bt_check_adv_app_termination(const char *name)
330 bt_service_app_info_t *app = NULL;
332 int apps[MAX_APPS_SUPPORTED] = { 0, };
334 ret_if(NULL == name);
336 memset(&apps, -1, sizeof(apps));
338 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
341 /* Search for a app which has same sender and stop adv is running */
342 if (!strncasecmp(app->sender, name, strlen(name)) && app->is_initialized == TRUE) {
343 BT_DBG("Match found, name: %s", name);
345 /* TODO 2: Need to manage app info as list, not array.
346 This loop always run for MAX count if any apps are terminated.
349 /* Save instances of all apps that need to be unregistered */
350 if (app->instance_id != -1) {
352 /* Unregister all service handles with stack */
353 __bt_remove_all_service_handles(app);
355 /* If Advertising is enabled, stop it */
356 if (app->adv_handle != 0) {
357 BT_INFO("Stop advertising on instance ID [%d]", app->instance_id);
358 /* Disable adv if running */
359 BT_INFO("Disable Advertising Adv Handle [%d] sender [%s]",
360 app->adv_handle, name);
361 _bt_set_advertising(app->sender, app->adv_handle, FALSE, FALSE);
364 apps[app->instance_id] = BT_GATT_SERVER; /* App holds a GATT server Instance */
365 } else if (app->client_id != -1) {
367 apps[app->client_id] = BT_GATT_CLIENT; /* App holds a GATT client Instance */
372 /* Unregister all apps one by one */
373 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
374 if (apps[k] == BT_GATT_SERVER) {
375 BT_INFO("Unregister server app[%d]", k);
376 /* Unregister server instance */
377 __bt_unregister_gatt_server_instance(k);
378 } else if (apps[k] == BT_GATT_CLIENT) {
379 BT_INFO("Unregister client app[%d]", k);
380 /* Unregister client instance */
381 __bt_unregister_gatt_client_instance(k);
386 static int __bt_gatt_send_indication_to_all_connected_clients(bluetooth_gatt_att_data_t *data,
387 bluetooth_gatt_server_indication_params_t *param)
390 int ret = OAL_STATUS_SUCCESS;
391 int result = OAL_STATUS_INTERNAL_ERROR;
393 BT_INFO("Current total number of connected clients [%d]", g_slist_length(gatt_client_info_list));
394 for (l = gatt_client_info_list; l != NULL; l = l->next) {
395 struct gatt_client_info_t *info = l->data;
398 BT_INFO("GATT Remote client address [%s] connection Id [%d]", info->addr, info->connection_id);
400 ret = gatts_send_indication(param->instance_id, param->atrribute_handle,
401 info->connection_id, data->length,
402 param->need_confirmation, (char *)(&data->data[0]));
404 BT_INFO("Send Indication to GATT client [%s] result: [%d]", info->addr, ret);
405 if (ret == OAL_STATUS_SUCCESS) {
406 BT_INFO("Send Indication sent successfully to GATT client [%s]", info->addr);
408 num_indicate_clients++;
412 BT_INFO("Indication sending done for total number of clients [%d]", num_indicate_clients);
416 static struct gatt_server_req_info *__bt_gatt_server_find_request_info(int request_id,
417 bluetooth_gatt_att_request_type_e req_type)
421 for (l = gatt_server_requests; l != NULL; l = l->next) {
422 struct gatt_server_req_info *req_info = l->data;
424 if (req_info && req_info->request_id == request_id && req_info->request_type == req_type) {
425 BT_INFO("GATT Server request info found Req ID [%d] handle [%d] conn ID [%d]",
426 req_info->request_id, req_info->attribute_handle, req_info->connection_id);
430 BT_ERR("Gatt Request not found");
434 void _bt_get_adv_handle_from_instance(int server_inst, int *adv_handle)
438 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
439 if (numapps[k].is_initialized == 1 && numapps[k].instance_id == server_inst) {
440 *adv_handle = numapps[k].adv_handle;
446 #ifdef TIZEN_GATT_CLIENT
447 char * _bt_gatt_get_default_gatt_client_uuid(void)
449 return g_strdup(DEFAULT_GATT_CLIENT_UUID);
453 static void __bt_register_default_gatt_client()
458 uuid_str = _bt_gatt_get_default_gatt_client_uuid();
459 _bt_string_to_uuid(uuid_str, (service_uuid_t*)&uuid);
461 BT_INFO("Register Default GATT client uuid [%s]", uuid_str);
463 if (OAL_STATUS_SUCCESS != gattc_register(&uuid)) /* for only Smart Control */
464 BT_ERR("gattc register failed");
470 int _bt_gatt_init(void)
472 const char *stack_name = NULL;
476 if (OAL_STATUS_SUCCESS != gatt_enable()) {
477 BT_ERR("gatt Init failed");
478 return BLUETOOTH_ERROR_INTERNAL;
481 /* Register gatt event handler */
482 _bt_service_register_event_handler_callback(BT_GATT_MODULE, __bt_gatt_event_handler);
484 __bt_service_reset_gatt_data();
486 stack_name = oal_get_stack_name();
488 if (stack_name && g_strcmp0(stack_name, "bluez") == 0) {
489 /*In the platform, defacult gatt client should be registered */
490 __bt_register_default_gatt_client();
494 return BLUETOOTH_ERROR_NONE;
497 static void __bt_service_reset_gatt_data(void)
501 BT_INFO("Rest numapp");
504 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
505 numapps[k].is_initialized = 0;
506 numapps[k].instance_id = -1;
507 numapps[k].adv_handle = 0;
508 numapps[k].adv_instance = -1;
509 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
510 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
511 memset(numapps[k].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
512 memset(numapps[k].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
513 numapps[k].adv_data_len = 0;
514 numapps[k].scan_rsp_len = 0;
517 numapps[k].client_id = -1;
518 memset(numapps[k].address.addr, 0x00, BLUETOOTH_ADDRESS_LENGTH);
519 numapps[k].is_watcher_enabled = FALSE;
523 void _bt_gatt_deinit(void)
525 BT_INFO("GATT deinit");
527 /* Un-register the default gatt client before */
528 __bt_unregister_gatt_client_instance(gatt_default_client);
530 if (OAL_STATUS_SUCCESS != gatt_disable())
531 BT_ERR("gatt deinit failed");
533 /* Un-register gatt event handler */
534 _bt_service_unregister_event_handler_callback(BT_GATT_MODULE);
536 __bt_service_reset_gatt_data();
539 void _bt_update_adv_handle(const char *sender, int adv_handle)
542 BT_DBG("Sender [%s] Adv handle [%d]", sender, adv_handle);
543 bt_service_app_info_t *info = NULL;
545 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
547 /* Do not update client instance */
548 if (info->instance_id == -1)
550 /* Search for a app which has same sender and adv handle as 0 */
551 if (!g_strcmp0(info->sender, sender) && info->adv_handle == 0)
552 info->adv_handle = adv_handle;
556 int _bt_is_sender_gatt_server_with_no_adv(const char *sender, int adv_handle)
559 BT_DBG("Sender [%s] Adv handle [%d]", sender, adv_handle);
560 bt_service_app_info_t *info = NULL;
562 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
564 /* Search for a app which has same sender and adv handle as 0
565 It is possible that same sender but different adv handle */
566 if (!g_strcmp0(info->sender, sender) && info->adv_handle == 0) {
567 //info->adv_handle = adv_handle;
568 return info->instance_id;
574 int _bt_get_allocated_server_instance(const char *sender, int adv_handle, gboolean use_reserved_slot)
577 BT_DBG("Sender [%s] Adv handle [%d]", sender, adv_handle);
578 bt_service_app_info_t *info = NULL;
580 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
583 /* Exact matching of Adv handle + sender combination */
584 if (!g_strcmp0(info->sender, sender) && info->adv_handle == adv_handle)
585 return info->instance_id;
587 if (!g_strcmp0(info->sender, sender) && info->adv_handle == -1)
588 return info->instance_id;
594 char * _bt_get_sender_and_handle(int server_instance, int *adv_handle)
597 bt_service_app_info_t *info = NULL;
599 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
602 if (info->instance_id == server_instance && info->adv_handle != 0) {
603 *adv_handle = info->adv_handle;
604 BT_DBG("Server instance [%d] Adv handle [%d] Sender [%s]", server_instance, *adv_handle, info->sender);
605 return g_strdup(info->sender);
611 void _bt_set_new_adv_data(bluetooth_advertising_data_t *adv, int len, int instance)
615 bt_service_app_info_t *info = NULL;
616 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
619 if (info->instance_id == instance) {
620 memcpy(info->adv_data.data, &adv->data, len);
626 void _bt_set_new_scan_rsp_data(bluetooth_scan_resp_data_t *scan, int len, int instance)
630 bt_service_app_info_t *info = NULL;
631 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
634 if (info->instance_id == instance) {
635 memcpy(info->scan_rsp.data, &scan->data, len);
641 void _bt_get_previous_adv_data(bluetooth_advertising_data_t *adv, int *len, int instance)
644 bt_service_app_info_t *info = NULL;
646 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
649 if (info->instance_id == instance) {
650 memcpy(&adv->data, info->adv_data.data, info->adv_data_len);
651 *len = info->adv_data_len;
657 void _bt_get_previous_scan_rsp_data(bluetooth_scan_resp_data_t *scan, int *len, int instance)
661 bt_service_app_info_t *info = NULL;
663 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
666 if (info->instance_id == instance) {
667 memcpy(&scan->data, info->scan_rsp.data, info->scan_rsp_len);
668 *len = info->scan_rsp_len;
674 static int __bt_unregister_gatt_client_instance(int client_if)
676 int ret = OAL_STATUS_SUCCESS;
679 BT_INFO("DeAllocate client instance ID [%d]", client_if);
681 /* Reset data: instance_id parameter could be either for GATT Server or for GATT client */
682 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
683 if (numapps[k].client_id == client_if) {
684 BT_INFO("This is a GATT client app, unregister: Slot [%d] vacant", k);
685 numapps[k].client_id = -1;
686 numapps[k].is_initialized = FALSE;
687 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
688 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
689 memset(&numapps[k].address.addr, 0x00, sizeof(bluetooth_device_address_t));
691 /* Its a GATT Client Instance */
692 ret = gattc_deregister(client_if);
693 if (ret != OAL_STATUS_SUCCESS) {
694 BT_ERR("DeAllocate GATT Client instance with stack Fail ret: %d", ret);
695 return BLUETOOTH_ERROR_INTERNAL;
700 return BLUETOOTH_ERROR_NONE;
703 static int __bt_unregister_gatt_server_instance(int server_instance)
705 int ret = OAL_STATUS_SUCCESS;
708 /* Unregister the server instance */
709 ret = gatts_unregister(server_instance);
710 if (ret != OAL_STATUS_SUCCESS) {
711 BT_ERR("DeAllocate server instance with stack Fail ret: %d", ret);
712 return BLUETOOTH_ERROR_INTERNAL;
714 BT_INFO("DeAllocated server instance with stack successful..");
717 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
718 if (numapps[k].instance_id == server_instance) {
719 numapps[k].is_initialized = 0;
720 numapps[k].instance_id = -1;
721 numapps[k].adv_handle = 0;
722 numapps[k].adv_instance = -1;
723 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
724 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
725 memset(numapps[k].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
726 memset(numapps[k].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
727 numapps[k].adv_data_len = 0;
728 numapps[k].scan_rsp_len = 0;
732 BT_DBG("Going8 to reset numapp block num [%d]", k);
733 return BLUETOOTH_ERROR_NONE;
736 static void __bt_remove_all_service_handles(bt_service_app_info_t *app_info)
740 int ret = OAL_STATUS_SUCCESS;
743 if (app_info == NULL)
746 count = g_slist_length(app_info->service_handles);
747 BT_INFO("Before handle removal: current count [%d]", count);
749 for (l = app_info->service_handles; l != NULL;) {
754 BT_INFO("Server Handle to be Removed [%d] Instance ID [%d]", *handle, app_info->instance_id);
755 if (_bt_gatt_server_stop_service(app_info->sender, *handle, app_info->instance_id) != BLUETOOTH_ERROR_NONE)
758 ret = gatts_delete_service(app_info->instance_id, *handle);
759 if (ret != OAL_STATUS_SUCCESS) {
760 BT_ERR("ret: %d", ret);
763 app_info->service_handles = g_slist_remove(app_info->service_handles, handle);
766 count = g_slist_length(app_info->service_handles);
767 BT_INFO("After deleting current count [%d]", count);
773 int _bt_unregister_server_instance(const char *sender, int adv_handle)
775 BT_INFO("Unregister Allocated server instance request Sender [%s] Adv handle [%d]", sender, adv_handle);
776 int result = BLUETOOTH_ERROR_NONE;
777 int apps[MAX_APPS_SUPPORTED];
781 memset(&apps, -1, sizeof(apps));
783 if (adv_handle == 0) {
784 BT_DBG("Its a direct GATT Server app request to unregister");
785 /* Unregister server instance for each app with same sender (case: GATT Server with multiple adv handle) */
786 bt_service_app_info_t *info = NULL;
788 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
791 /* Exact matching of sender */
792 if (!g_strcmp0(info->sender, sender)) {
793 BT_INFO("Unregister GATT server instance [%d]", info->instance_id);
794 /* Unregister all service handles with stack */
795 __bt_remove_all_service_handles(info);
797 /* Disable adv if running */
798 BT_INFO("Disable Advertising Adv Handle [%d] sender [%s]",
799 info->adv_handle, sender);
800 _bt_set_advertising(sender, info->adv_handle, FALSE, FALSE);
802 /* Save all instances which need to be unregistered */
803 apps[info->instance_id] = 1;
807 BT_DBG("Its an Internal unregister request by adv application");
808 server_instance = _bt_get_allocated_server_instance(sender, adv_handle, FALSE);
809 BT_DBG("Its an Internal unregister request by adv application: Adv disabled srv instance [%d]", server_instance);
810 if (server_instance == -1) {
811 BT_ERR("No allocated server instance to be removed");
812 return BLUETOOTH_ERROR_INVALID_PARAM;
814 if (!numapps[server_instance].service_handles) {
815 BT_INFO("There are no Service handles with this app, safe to unregister");
816 /* Unregister server instance only if this sender does not have any gatt services in it */
817 result = __bt_unregister_gatt_server_instance(server_instance);
819 numapps[server_instance].adv_handle = 0;
820 memset(numapps[server_instance].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
821 numapps[server_instance].adv_data_len = 0;
822 memset(numapps[server_instance].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
823 numapps[server_instance].scan_rsp_len = 0;
828 /* Unregister all apps one by one */
829 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
831 BT_INFO("Unregister app[%d]", k);
832 /* Unregister server instance */
833 __bt_unregister_gatt_server_instance(k);
840 int _bt_register_server_instance(const char *sender, int adv_handle)
842 int ret = OAL_STATUS_SUCCESS;
843 char *uuid_string = NULL;
848 BT_INFO("###Check on which instance Server instance can be initialized....");
849 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
850 if (numapps[k].is_initialized == 1) {
851 BT_INFO("Instance ID [%d] is already in use..Check next slot", numapps[k].instance_id);
854 BT_INFO("Time to register GATT Server..UUID to be used is [%s] slot [%d]", uuid_list[slot-1], slot);
860 BT_ERR("No Slot if free for GATT Server registration..");
861 return BLUETOOTH_ERROR_REGISTRATION_FAILED;
864 uuid_string = g_malloc0(BT_UUID_STRING_MAX);
865 _bt_string_to_uuid(uuid_list[slot-1], (service_uuid_t*)&uuid);
866 g_strlcpy(uuid_string, uuid_list[slot-1], BT_UUID_STRING_MAX);
867 BT_INFO("Copied UUID string [%s]", uuid_string);
869 /* Register GATT Server */
870 ret = gatts_register(&uuid);
871 if (ret != OAL_STATUS_SUCCESS) {
872 BT_ERR("ret: %d", ret);
874 return BLUETOOTH_ERROR_INTERNAL;
876 BT_INFO("GATT Server registration call successfully accepted by OAL..wait for Instance Initialized event from OAL..");
877 /* Return & wait for GATT Server Instance Initialization event */
878 memset(numapps[slot].sender, 0x00, sizeof(numapps[slot].sender));
879 memset(numapps[slot].uuid, 0x00, sizeof(numapps[slot].uuid));
881 g_strlcpy(numapps[slot].sender, sender, sizeof(numapps[slot].sender));
882 g_strlcpy(numapps[slot].uuid, uuid_string, sizeof(numapps[slot].uuid));
884 numapps[slot].is_initialized = 0; /* Set initialization from app registered callback */
885 numapps[slot].adv_handle = adv_handle;
888 return BLUETOOTH_ERROR_NONE;
892 static void __bt_gatt_handle_pending_request_info(int result,
893 int service_function, void *data, unsigned int size)
897 invocation_info_t *req_info = NULL;
898 ret_if(data == NULL);
900 for (l = _bt_get_invocation_list(); l != NULL; ) {
903 if (req_info == NULL || req_info->service_function != service_function)
906 switch (service_function) {
908 case BT_GATT_SERVER_REGISTER: {
909 bt_service_app_info_t *param = (bt_service_app_info_t*)data;
910 BT_DBG("Sender [%s] Request Sender [%s]", param->sender, req_info->sender);
911 if (!g_strcmp0(req_info->sender, param->sender)) {
912 BT_DBG("GATT Server app found [%s]", req_info->sender);
914 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
915 g_array_append_vals(out_param, ¶m->instance_id, sizeof(int));
916 _bt_service_method_return(req_info->context, out_param, result);
918 g_free(req_info->user_data);
919 _bt_free_info_from_invocation_list(req_info);
920 g_array_free(out_param, TRUE);
924 case BT_GATT_SERVER_START_SERVICE:
925 case BT_GATT_SERVER_DELETE_SERVICE: {
926 bt_service_app_info_t *param = (bt_service_app_info_t*)data;
927 BT_DBG("Sender [%s] Request Sender [%s]", param->sender, req_info->sender);
929 int *saved_instance_id = (int*)req_info->user_data;
930 if (!g_strcmp0(req_info->sender, param->sender) && param->instance_id == *saved_instance_id) {
931 BT_DBG("GATT Server app found [%s] Instance ID [%d] Reply DBUS",
932 req_info->sender, *saved_instance_id);
934 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
935 g_array_append_vals(out_param, &saved_instance_id, sizeof(int));
936 _bt_service_method_return(req_info->context, out_param, result);
938 g_free(req_info->user_data);
939 _bt_free_info_from_invocation_list(req_info);
940 g_array_free(out_param, TRUE);
944 case BT_GATT_SERVER_ADD_SERVICE:
945 case BT_GATT_SERVER_ADD_DESCRIPTOR:
946 case BT_GATT_SERVER_ADD_CHARACTERISTIC: {
947 int *handle = (int*)data;
948 BT_DBG("Characteristic added: Handle [%d]", *handle);
949 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
951 g_array_append_vals(out_param, handle, sizeof(int));
952 _bt_service_method_return(req_info->context, out_param, result);
954 g_free(req_info->user_data);
955 _bt_free_info_from_invocation_list(req_info);
956 g_array_free(out_param, TRUE);
961 case BT_DISCONNECT_LE: {
962 #ifdef TIZEN_GATT_CLIENT
963 char *addr = (char*)req_info->user_data;
964 bluetooth_device_address_t address;
966 if (!g_strcmp0(addr, (char*)data)) {
967 BT_INFO("GATT Client connect-disconnect call pending for app [%s] addr [%s]",
968 req_info->sender, addr);
969 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
970 _bt_convert_addr_string_to_type(address.addr, addr);
972 g_array_append_vals(out_param, (bluetooth_device_address_t*)&address,
973 sizeof(bluetooth_device_address_t));
974 _bt_service_method_return(req_info->context, out_param, result);
976 g_free(req_info->user_data);
977 _bt_free_info_from_invocation_list(req_info);
978 g_array_free(out_param, TRUE);
981 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
983 g_array_append_vals(out_param, (bluetooth_device_address_t*)data,
984 sizeof(bluetooth_device_address_t));
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);
990 g_array_free(out_param, TRUE);
994 #ifdef TIZEN_GATT_CLIENT
995 case BT_GATT_CLIENT_REGISTER: {
996 bt_service_app_info_t *param = (bt_service_app_info_t*)data;
997 BT_DBG("Sender [%s] Request Sender [%s]", param->sender, req_info->sender);
998 if (!g_strcmp0(req_info->sender, param->sender)) {
999 BT_DBG("GATT Client app found [%s] created client ID [%d]",
1000 req_info->sender, param->client_id);
1002 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1003 g_array_append_vals(out_param, ¶m->client_id, sizeof(int));
1004 _bt_service_method_return(req_info->context, out_param, result);
1006 g_free(req_info->user_data);
1007 _bt_free_info_from_invocation_list(req_info);
1008 g_array_free(out_param, TRUE);
1012 case BT_GATT_GET_PRIMARY_SERVICES: {
1013 bt_services_browse_info_t *param = (bt_services_browse_info_t*)data;
1014 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1015 _bt_convert_addr_type_to_string(address,
1016 (unsigned char *)(¶m->device_addr.addr));
1018 BT_DBG("Request Sender [%s] address [%s]", req_info->sender, address);
1020 /* Match address to determine same request */
1021 if (!g_strcmp0((char*)req_info->user_data, address)) {
1022 BT_DBG("GATT Client app found [%s] Remote address [%s]",
1023 req_info->sender, address);
1025 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1026 g_array_append_vals(out_param, param,
1027 sizeof(bt_services_browse_info_t));
1029 //g_array_append_vals(out_param, ¶m->client_id, sizeof(int));
1030 _bt_service_method_return(req_info->context, out_param, result);
1032 g_free(req_info->user_data);
1033 _bt_free_info_from_invocation_list(req_info);
1034 g_array_free(out_param, TRUE);
1039 case BT_GATT_GET_SERVICE_PROPERTIES: {
1040 bt_char_browse_info_t param;
1041 memcpy((void*)¶m, data, sizeof(bt_char_browse_info_t));
1042 //bt_char_browse_info_t *param = (bt_char_browse_info_t*)data;
1043 BT_DBG("Request Sender [%s]", req_info->sender);
1044 bluetooth_gatt_client_svc_prop_info_t *prop = (bluetooth_gatt_client_svc_prop_info_t*)req_info->user_data;
1046 /* Match both address and service properties to determine same request */
1047 if (!memcmp(param.device_addr.addr,
1048 prop->device_address.addr,
1049 sizeof(bluetooth_device_address_t)) &&
1050 !memcmp(param.svc_uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1051 param.svc_inst_id == prop->svc.instance_id) {
1052 BT_DBG("Returning Service properties");
1054 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1055 g_array_append_vals(out_param, ¶m, sizeof(bt_char_browse_info_t));
1056 _bt_service_method_return(req_info->context, out_param, result);
1058 g_free(req_info->user_data);
1059 _bt_free_info_from_invocation_list(req_info);
1060 g_array_free(out_param, TRUE);
1064 case BT_GATT_GET_CHARACTERISTIC_PROPERTIES: {
1065 bt_descriptor_browse_info_t *param = (bt_descriptor_browse_info_t*)data;
1067 BT_DBG("Request Sender [%s]", req_info->sender);
1068 bluetooth_gatt_client_char_prop_info_t *prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
1070 /* Match both address, service properties &char properties to determine same request */
1071 if (!memcmp(param->device_addr.addr,
1072 prop->device_address.addr,
1073 sizeof(bluetooth_device_address_t)) &&
1074 !memcmp(param->svc_uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1075 param->svc_inst_id == prop->svc.instance_id &&
1076 !memcmp(param->char_uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1077 param->char_inst_id == prop->characteristic.instance_id) {
1078 BT_DBG("Returning Characteristic properties");
1079 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1080 g_array_append_vals(out_param, param, sizeof(bt_descriptor_browse_info_t));
1081 _bt_service_method_return(req_info->context, out_param, result);
1083 g_free(req_info->user_data);
1084 _bt_free_info_from_invocation_list(req_info);
1085 g_array_free(out_param, TRUE);
1089 case BT_GATT_WATCH_CHARACTERISTIC: {
1090 bt_gatt_notif_reg_info_t *param = (bt_gatt_notif_reg_info_t*)data;
1091 bluetooth_gatt_client_char_prop_info_t *prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
1092 BT_DBG("Request Sender [%s]", req_info->sender);
1093 /* Match both address, service properties &char properties to determine same request */
1094 if (!memcmp(param->addr.addr,
1095 prop->device_address.addr,
1096 sizeof(bluetooth_device_address_t)) &&
1097 !memcmp(param->svc_uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1098 param->svc_inst == prop->svc.instance_id &&
1099 !memcmp(param->char_uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1100 param->char_inst == prop->characteristic.instance_id) {
1101 BT_INFO("Characteristic Watch Successful: Is registered [%d]",
1102 param->is_registered);
1103 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1104 g_array_append_vals(out_param, param, sizeof(bt_gatt_notif_reg_info_t));
1105 _bt_service_method_return(req_info->context, out_param, result);
1106 g_free(req_info->user_data);
1107 _bt_free_info_from_invocation_list(req_info);
1108 g_array_free(out_param, TRUE);
1112 case BT_GATT_READ_CHARACTERISTIC:
1113 case BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE: {
1114 bluetooth_gatt_client_char_prop_info_t *param = (bluetooth_gatt_client_char_prop_info_t*)data;
1115 BT_DBG("Request Sender [%s]", req_info->sender);
1117 char *addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
1118 bluetooth_gatt_client_char_prop_info_t *prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
1119 _bt_convert_addr_type_to_string(addr, prop->device_address.addr);
1121 if (!memcmp(param, prop, sizeof(bluetooth_gatt_client_char_prop_info_t))) {
1122 BT_DBG("Gatt Char read or write request matched for address [%s]", addr);
1123 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1124 g_array_append_vals(out_param, param, sizeof(bluetooth_gatt_client_char_prop_info_t));
1125 _bt_service_method_return(req_info->context, out_param, result);
1127 g_free(req_info->user_data);
1128 _bt_free_info_from_invocation_list(req_info);
1129 g_array_free(out_param, TRUE);
1134 case BT_GATT_READ_DESCRIPTOR_VALUE:
1135 case BT_GATT_WRITE_DESCRIPTOR_VALUE: {
1136 bluetooth_gatt_client_desc_prop_info_t *param = (bluetooth_gatt_client_desc_prop_info_t*)data;
1137 BT_DBG("Request Sender [%s]", req_info->sender);
1139 char *addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
1140 bluetooth_gatt_client_desc_prop_info_t *prop = (bluetooth_gatt_client_desc_prop_info_t*)req_info->user_data;
1141 _bt_convert_addr_type_to_string(addr, prop->device_address.addr);
1143 if (!memcmp(param, prop, sizeof(bluetooth_gatt_client_desc_prop_info_t))) {
1144 BT_DBG("Descriptor read or write request matched for address [%s]", addr);
1145 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1146 g_array_append_vals(out_param, param, sizeof(bluetooth_gatt_client_desc_prop_info_t));
1147 _bt_service_method_return(req_info->context, out_param, result);
1149 g_free(req_info->user_data);
1150 _bt_free_info_from_invocation_list(req_info);
1151 g_array_free(out_param, TRUE);
1156 case BT_REQ_ATT_MTU: {
1157 char *addr = (char*)req_info->user_data;
1158 bluetooth_device_address_t address;
1160 if (!g_strcmp0(addr, (char*)data)) {
1161 BT_DBG("GATT Client BT_REQ_ATT_MTU call pending for app [%s] addr [%s]",
1162 req_info->sender, addr);
1163 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1164 _bt_convert_addr_string_to_type(address.addr, addr);
1166 g_array_append_vals(out_param, (bluetooth_device_address_t*)&address,
1167 sizeof(bluetooth_device_address_t));
1168 _bt_service_method_return(req_info->context, out_param, result);
1170 g_free(req_info->user_data);
1171 _bt_free_info_from_invocation_list(req_info);
1172 g_array_free(out_param, TRUE);
1182 static void __bt_handle_server_instance_registered(event_gatts_register_t *data)
1184 bt_service_app_info_t *info = NULL;
1186 char *uuid_string = g_malloc0(BT_UUID_STRING_MAX);
1188 _bt_uuid_to_string(&(data->server_uuid), uuid_string);
1189 BT_INFO("Instance ID is Intialized [%d] UUID initialized [%s]", data->server_inst, uuid_string);
1191 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1192 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1195 if (g_strcmp0(info->uuid, uuid_string) == 0) {
1196 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1197 info->is_initialized = TRUE;
1198 info->instance_id = data->server_inst;
1199 info->adv_instance = data->server_inst;
1200 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE, BT_GATT_SERVER_REGISTER,
1201 (void*)info, sizeof(bt_service_app_info_t));
1205 g_free(uuid_string);
1208 static void __bt_handle_gatt_server_service_added(event_gatts_srvc_prm_t *event)
1210 int result = BLUETOOTH_ERROR_NONE;
1214 bt_service_app_info_t *info = NULL;
1217 char *uuid_str = g_malloc0(BT_UUID_STRING_MAX);
1218 _bt_uuid_to_string(&(event->gatt_srvc_id.id.uuid), uuid_str);
1219 BT_INFO("GATT Added Service UUID: [%s] Server Ins [%d] Service hdl [%d] Is Primary [%d]\n",
1220 uuid_str, event->gatt_srvc_stat.server_inst, svc_handle, event->gatt_srvc_id.is_prmry);
1222 if (event->gatt_srvc_stat.status != OAL_STATUS_SUCCESS) {
1223 result = BLUETOOTH_ERROR_INTERNAL;
1224 svc_handle = 0; /* Service handle set to 0 indicates.
1225 0 is reserved by BT SIG, cant be used by app*/
1227 svc_handle = event->gatt_srvc_stat.servic_hndl;
1229 BT_INFO("GATT Added Service Status [%d] Server Ins [%d] Service hdl [%d] Is Primary [%d]\n",
1230 event->gatt_srvc_stat.status, event->gatt_srvc_stat.server_inst, svc_handle, event->gatt_srvc_id.is_prmry);
1232 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1233 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1236 if (info->instance_id == event->gatt_srvc_stat.server_inst) {
1237 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1238 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_ADD_SERVICE,
1239 (int*)&svc_handle, sizeof(int));
1241 /* Add Service Handle */
1242 if (svc_handle > 0) {
1243 handle = g_malloc0(sizeof(int));
1244 *handle = svc_handle;
1245 numapps[event->gatt_srvc_stat.server_inst].service_handles = g_slist_append(numapps[event->gatt_srvc_stat.server_inst].service_handles, handle);
1246 count = g_slist_length(numapps[event->gatt_srvc_stat.server_inst].service_handles);
1247 BT_INFO("Added Service handle [%d] to list..current count [%d]", svc_handle, count);
1256 static void __bt_handle_gatt_server_characteristic_added(event_gatts_srvc_charctr_t *event)
1258 int result = BLUETOOTH_ERROR_NONE;
1260 bt_service_app_info_t *info = NULL;
1262 char *uuid_str = g_malloc0(BT_UUID_STRING_MAX);
1264 BT_INFO("GATT Server Char added status [%d]", event->gatt_srvc_stat.status);
1265 BT_INFO("GATT Server_inst: [%d]", event->gatt_srvc_stat.server_inst);
1266 BT_INFO("GATT Service Handle: [%d]", event->gatt_srvc_stat.servic_hndl);
1267 BT_INFO("GATT Add characteristic Status: [%d]", event->gatt_srvc_stat.status);
1268 BT_INFO("GATT Service characteristic Handle: [%d]", event->charctr_hndl);
1270 if (event->gatt_srvc_stat.status != OAL_STATUS_SUCCESS) {
1271 result = BLUETOOTH_ERROR_INTERNAL;
1272 char_handle = 0; /* characteristic handle set to 0 indicates.
1273 0 is reserved by BT SIG, cant be used by app*/
1275 char_handle = event->charctr_hndl;
1278 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1279 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1282 if (info->instance_id == event->gatt_srvc_stat.server_inst) {
1283 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1284 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_ADD_CHARACTERISTIC,
1285 (int*)&char_handle, sizeof(int));
1291 _bt_uuid_to_string(&(event->charctr_uuid), uuid_str);
1292 BT_INFO("GATT Added Characteristic: UUID: [%s]", uuid_str);
1297 static void __bt_handle_gatt_server_descriptor_added(event_gatts_srvc_descr_t* event)
1299 int result = BLUETOOTH_ERROR_NONE;
1301 bt_service_app_info_t *info = NULL;
1303 char *uuid_str = g_malloc0(BT_UUID_STRING_MAX);
1305 BT_INFO("GATT Server Descriptor added status [%d]", event->gatt_srvc_stat.status);
1306 BT_INFO("GATT Server_inst: [%d]", event->gatt_srvc_stat.server_inst);
1307 BT_INFO("GATT Service Handle: [%d]", event->gatt_srvc_stat.servic_hndl);
1308 BT_INFO("GATT Add Descriptor Status: [%d]", event->gatt_srvc_stat.status);
1309 BT_INFO("GATT Service Descriptor Handle: [%d]", event->descrptr_hndl);
1311 if (event->gatt_srvc_stat.status != OAL_STATUS_SUCCESS) {
1312 result = BLUETOOTH_ERROR_INTERNAL;
1313 desc_handle = 0; /* Service handle set to 0 indicates.
1314 0 is reserved by BT SIG, cant be used by app*/
1316 desc_handle = event->descrptr_hndl;
1318 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1319 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1322 if (info->instance_id == event->gatt_srvc_stat.server_inst) {
1323 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1324 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_ADD_DESCRIPTOR,
1325 (int*)&desc_handle, sizeof(int));
1330 _bt_uuid_to_string(&(event->descrptr_uuid), uuid_str);
1331 BT_INFO("GATT Added Descriptor: UUID: [%s]", uuid_str);
1336 static void __bt_handle_gatt_server_service_started(event_gatts_srvc_t *event)
1338 bt_service_app_info_t *info = NULL;
1339 int result = BLUETOOTH_ERROR_NONE;
1341 BT_INFO("GATT Server Service Started..");
1344 BT_INFO("GATT Server_inst: [%d]", event->server_inst);
1345 BT_INFO("GATT Service Handle: [%d]", event->servic_hndl);
1346 BT_INFO("GATT Service Started Status: [%d]", event->status);
1348 if (event->status != OAL_STATUS_SUCCESS) {
1349 BT_ERR("GATT Server Service Start Failed Err: [%d]", event->status);
1350 result = BLUETOOTH_ERROR_INTERNAL;
1353 /* Check if the just registered Instance ID belongs to requester */
1354 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1357 if (info->instance_id == event->server_inst) {
1358 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1359 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_START_SERVICE,
1360 (void*)info, sizeof(bt_service_app_info_t));
1366 static void __bt_handle_gatt_server_service_stopped(event_gatts_srvc_t *event)
1368 int result = BLUETOOTH_ERROR_NONE;
1369 bt_service_app_info_t *info = NULL;
1371 BT_INFO("GATT Server Service Stopped..");
1373 BT_INFO("GATT Server_inst: [%d]", event->server_inst);
1374 BT_INFO("GATT Service Handle: [%d]", event->servic_hndl);
1375 BT_INFO("GATT Service Stopped Status: [%d]", event->status);
1377 if (event->status != OAL_STATUS_SUCCESS) {
1378 BT_ERR("GATT Server Service Stop Failed Err: [%d]", event->status);
1379 result = BLUETOOTH_ERROR_INTERNAL;
1382 /* Check if the just registered Instance ID belongs to requester */
1383 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1386 if (info->instance_id == event->server_inst) {
1387 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1388 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_STOP_SERVICE,
1389 (void*)info, sizeof(bt_service_app_info_t));
1395 static void __bt_handle_gatt_server_service_deleted(event_gatts_srvc_t *event)
1397 int result = BLUETOOTH_ERROR_NONE;
1398 bt_service_app_info_t *info = NULL;
1400 BT_INFO("GATT Server Service Deleted..");
1402 BT_INFO("GATT Server_inst: [%d]", event->server_inst);
1403 BT_INFO("GATT Service Handle: [%d]", event->servic_hndl);
1404 BT_INFO("GATT Service Deleted Status: [%d]", event->status);
1406 if (event->status != OAL_STATUS_SUCCESS) {
1407 BT_ERR("GATT Server Service Delete Failed Err: [%d]", event->status);
1408 result = BLUETOOTH_ERROR_INTERNAL;
1411 /* Check if the just registered Instance ID belongs to requester */
1412 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1415 if (info->instance_id == event->server_inst) {
1416 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1417 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_DELETE_SERVICE,
1418 (void*)info, sizeof(bt_service_app_info_t));
1424 struct gatt_client_info_t *_bt_find_remote_gatt_client_info(char *address)
1427 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 (!g_strcmp0(info->addr, address)) {
1434 BT_DBG("Remote GATT client found addr[%s]", info->addr);
1441 static struct gatt_client_info_t *__bt_find_remote_gatt_client_info_from_conn_id(int conn_id)
1444 struct gatt_client_info_t *info = NULL;
1446 for (l = gatt_client_info_list; l != NULL; l = g_slist_next(l)) {
1447 info = (struct gatt_client_info_t*)l->data;
1451 if (info->connection_id == conn_id) {
1452 BT_INFO("Remote GATT client found addr[%s]", info->addr);
1459 #ifdef TIZEN_GATT_CLIENT
1460 struct gatt_server_info_t *_bt_find_remote_gatt_server_info(char *address)
1463 struct gatt_server_info_t *info = NULL;
1464 for (l = gatt_server_info_list; l != NULL; l = g_slist_next(l)) {
1465 info = (struct gatt_server_info_t*)l->data;
1469 if (!g_strcmp0(info->addr, address)) {
1470 BT_DBG("Remote GATT Server found addr[%s]", info->addr);
1477 static struct gatt_out_conn_info_t* __bt_find_gatt_outgoing_conn_info(char *address)
1480 struct gatt_out_conn_info_t *info = NULL;
1481 for (l = outgoing_gatt_conn_list; l != NULL; l = g_slist_next(l)) {
1482 info = (struct gatt_out_conn_info_t*)l->data;
1486 if (!g_strcmp0(info->addr, address)) {
1487 BT_INFO("Outgoing connection info found addr[%s]", info->addr);
1495 #ifdef TIZEN_GATT_CLIENT
1496 static void __bt_handle_gatt_server_connection_state(event_gatts_conn_t *event)
1498 int result = BLUETOOTH_ERROR_NONE;
1499 struct gatt_client_info_t *client_info = NULL;
1500 #ifndef __INTEGRATE_GATT_INFO__
1501 struct gatt_server_info_t *server_info = NULL;
1503 bluetooth_device_address_t dev_addr;
1504 GVariant *param = NULL;
1507 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1509 memcpy(dev_addr.addr, event->address.addr, 6);
1511 /* REPLY dbus Context to application which called BT_CONNECT_LE. There is status
1513 _bt_convert_addr_type_to_string(address,
1514 (unsigned char *)dev_addr.addr);
1516 if (event->status != OAL_STATUS_SUCCESS)
1517 result = BLUETOOTH_ERROR_INTERNAL;
1519 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
1520 address, BT_ADDRESS_STRING_SIZE);
1522 BT_INFO("GATT Server Connedted: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1523 address, event->server_inst, event->conn_id);
1526 /* Check if device is already in connected list */
1527 client_info = _bt_find_remote_gatt_client_info(address);
1530 BT_INFO("Conn Info absent: But no need to Send Local GATT Server Connected event to apps");
1532 param = g_variant_new("(is)", result, address);
1534 /* Send event to application */
1535 _bt_send_event(BT_DEVICE_EVENT,
1536 BLUETOOTH_EVENT_GATT_SERVER_CONNECTED, /* Local device is GATT server */
1539 /* Save client connection info */
1540 client_info = g_new0(struct gatt_client_info_t, 1);
1541 client_info->addr = g_strdup(address);
1542 BT_INFO("Added GATT client addr[%s]", client_info->addr);
1543 client_info->connection_id = event->conn_id;
1544 #ifdef __INTEGRATE_GATT_INFO__
1545 client_info->client_id = -1;
1547 client_info->instance_id = event->server_inst;
1548 gatt_client_info_list = g_slist_append(gatt_client_info_list, client_info);
1549 BT_INFO("Total num of connected Remote GATT Clients [%d]", g_slist_length(gatt_client_info_list));
1551 #ifndef __INTEGRATE_GATT_INFO__
1552 /* Save server connection info */
1553 server_info = g_new0(struct gatt_server_info_t, 1);
1554 server_info->addr = g_strdup(address);
1555 server_info->client_id = -1;
1556 BT_INFO("Added GATT server addr[%s]", server_info->addr);
1557 server_info->connection_id = event->conn_id;
1558 gatt_server_info_list = g_slist_append(gatt_server_info_list, server_info);
1559 BT_INFO("Total num of connected Remote GATT Servers [%d]", g_slist_length(gatt_server_info_list));
1562 ret = gattc_add_connection_info((bt_address_t *)&dev_addr, event->conn_id, event->server_inst);
1563 if (ret != OAL_STATUS_SUCCESS) {
1564 BT_ERR("gattc register server instance failed");
1568 __bt_add_mtu_gatt_device(address);
1573 /* GATT Server Dis connected */
1574 static void __bt_handle_gatt_server_disconnection_state(event_gatts_conn_t *event)
1576 int result = BLUETOOTH_ERROR_NONE;
1577 struct gatt_client_info_t *client_info = NULL;
1578 #ifndef __INTEGRATE_GATT_INFO__
1579 struct gatt_server_info_t *server_info = NULL;
1581 bluetooth_device_address_t dev_addr;
1582 GVariant *param = NULL;
1583 char address[BT_ADDRESS_STRING_SIZE];
1585 memcpy(dev_addr.addr, event->address.addr, 6);
1587 /* REPLY dbus Context to application which called BT_DISCONNECT_LE. There is status
1589 _bt_convert_addr_type_to_string(address,
1590 (unsigned char *)dev_addr.addr);
1592 if (event->status != OAL_STATUS_SUCCESS)
1593 result = BLUETOOTH_ERROR_INTERNAL;
1595 if (NULL == _bt_get_request_info_data(BT_DISCONNECT_LE, address)) {
1596 if (NULL != _bt_get_request_info_data(BT_CONNECT_LE, address)) {
1597 result = BLUETOOTH_ERROR_INTERNAL;
1599 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
1600 address, BT_ADDRESS_STRING_SIZE);
1602 BT_ERR("Failed to connect Local GATT Server Remote Client addr[%s]", address);
1606 __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE,
1607 address, BT_ADDRESS_STRING_SIZE);
1610 BT_INFO("Local GATT Server DisConnected: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1611 address, event->server_inst, event->conn_id);
1612 /* Remove Connection info */
1613 client_info = _bt_find_remote_gatt_client_info(address);
1615 BT_INFO("No need to Send Local GATT Server Disconnected event to apps, just remove remote client info");
1617 param = g_variant_new("(is)", result, address);
1618 /* Send event to application */
1619 _bt_send_event(BT_DEVICE_EVENT,
1620 BLUETOOTH_EVENT_GATT_SERVER_DISCONNECTED, /* Local device is GATT server */
1623 #ifndef __INTEGRATE_GATT_INFO__
1624 /* Remove server info from list */
1625 server_info = _bt_find_remote_gatt_server_info(address);
1627 gatt_server_info_list = g_slist_remove(gatt_server_info_list, server_info);
1629 BT_INFO("Can not find conn info, already removed!");
1632 /* Remove client info from List */
1633 gatt_client_info_list = g_slist_remove(gatt_client_info_list, client_info);
1634 BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
1635 g_free(client_info->addr);
1636 g_free(client_info);
1639 __bt_remove_mtu_gatt_device(address);
1643 static void __bt_handle_gatt_server_connection_state(event_gatts_conn_t *event)
1645 int result = BLUETOOTH_ERROR_NONE;
1646 struct gatt_client_info_t *conn_info = NULL;
1647 bt_service_app_info_t *info = NULL;
1648 bluetooth_device_address_t dev_addr;
1649 GVariant *param = NULL;
1652 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1654 memcpy(dev_addr.addr, event->address.addr, 6);
1657 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1658 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1661 if (info->instance_id == event->server_inst) {
1662 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1663 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
1664 (bluetooth_device_address_t*)&dev_addr, sizeof(bluetooth_device_address_t));
1670 /* Send event to BT-API */
1671 _bt_convert_addr_type_to_string(address,
1672 (unsigned char *)dev_addr.addr);
1674 BT_INFO("GATT Server Connedted: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1675 address, event->server_inst, event->conn_id);
1678 /* Check if device is already in connected list */
1679 conn_info = _bt_find_remote_gatt_client_info(address);
1682 param = g_variant_new("(is)", result, address);
1683 /* Send event to application */
1684 _bt_send_event(BT_GATT_SERVER_EVENT,
1685 BLUETOOTH_EVENT_GATT_CONNECTED,
1688 /* Save Connection info */
1689 conn_info = g_new0(struct gatt_client_info_t, 1);
1690 conn_info->addr = g_strdup(address);
1691 BT_INFO("Added GATT client addr[%s]", conn_info->addr);
1692 conn_info->connection_id = event->conn_id;
1693 conn_info->instance_id = event->server_inst;
1694 gatt_client_info_list = g_slist_append(gatt_client_info_list, conn_info);
1695 BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
1701 /* GATT Server Dis connected */
1702 static void __bt_handle_gatt_server_disconnection_state(event_gatts_conn_t *event)
1704 int result = BLUETOOTH_ERROR_NONE;
1705 struct gatt_client_info_t *conn_info = NULL;
1706 bluetooth_device_address_t dev_addr;
1707 bt_service_app_info_t *info = NULL;
1708 GVariant *param = NULL;
1711 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1713 memcpy(dev_addr.addr, event->address.addr, 6);
1715 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1716 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1719 if (info->instance_id == event->server_inst) {
1720 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1721 __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE,
1722 (bluetooth_device_address_t*)&dev_addr, sizeof(bluetooth_device_address_t));
1728 /* Send event to BT-API */
1729 _bt_convert_addr_type_to_string(address,
1730 (unsigned char *)dev_addr.addr);
1732 BT_INFO("GATT Server DisConnected: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1733 address, event->server_inst, event->conn_id);
1735 param = g_variant_new("(is)", result, address);
1736 /* Send event to application */
1737 _bt_send_event(BT_GATT_SERVER_EVENT,
1738 BLUETOOTH_EVENT_GATT_DISCONNECTED,
1741 /* Remove Connection info */
1742 conn_info = _bt_find_remote_gatt_client_info(address);
1744 BT_INFO("Remove GATT client info from List..");
1745 /* Remove info from List */
1746 gatt_client_info_list = g_slist_remove(gatt_client_info_list, conn_info);
1747 BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
1748 g_free(conn_info->addr);
1757 static void __bt_handle_gatt_server_acquire_write_requested(event_gatts_srvc_acquire_attr_t *event)
1759 GVariant *param = NULL;
1760 int result = BLUETOOTH_ERROR_NONE;
1761 struct gatt_server_req_info *req_info = NULL;
1762 bluetooth_device_address_t dev_addr;
1763 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1765 BT_INFO("GATT Server ACQUIRE Write Req Connection ID: [%d]", event->attr_trans.conn_id);
1766 BT_INFO("GATT Server ACQUIRE Write Req Transaction ID:[%d]", event->attr_trans.trans_id);
1767 BT_INFO("GATT Server ACQUIRE Write Req Attribute Handle: [%d]", event->attr_trans.attr_handle);
1769 //address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1770 memcpy(dev_addr.addr, event->address.addr, 6);
1772 req_info = g_new0(struct gatt_server_req_info, 1);
1773 req_info->request_id = event->attr_trans.trans_id;
1774 req_info->attribute_handle = event->attr_trans.attr_handle;
1775 req_info->connection_id = event->attr_trans.conn_id;
1776 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_ACQUIRE_WRITE;
1777 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1779 _bt_convert_addr_type_to_string(address,
1780 (unsigned char *)dev_addr.addr);
1782 param = g_variant_new("(iiiiiis)", result,
1783 event->attr_trans.conn_id,
1784 event->attr_trans.trans_id,
1785 event->attr_trans.attr_handle,
1786 event->mtu, event->attr_trans.offset, address);
1787 BT_INFO("GATT Server ACQUIRE Write Req Attribute remote address : [%s]", address);
1789 _bt_send_event(BT_GATT_SERVER_EVENT,
1790 BLUETOOTH_EVENT_GATT_SERVER_ACQUIRE_WRITE,
1795 static void __bt_handle_gatt_server_acquire_notify_requested(event_gatts_srvc_acquire_attr_t *event)
1797 GVariant *param = NULL;
1798 int result = BLUETOOTH_ERROR_NONE;
1799 struct gatt_server_req_info *req_info = NULL;
1800 bluetooth_device_address_t dev_addr;
1801 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1803 BT_INFO("GATT Server ACQUIRE Notify Req Connection ID: [%d]", event->attr_trans.conn_id);
1804 BT_INFO("GATT Server ACQUIRE Notify Req Transaction ID:[%d]", event->attr_trans.trans_id);
1805 BT_INFO("GATT Server ACQUIRE Notify Req Attribute Handle: [%d]", event->attr_trans.attr_handle);
1806 BT_INFO("GATT Server ACQUIRE notify Req address : [%s]", address);
1808 memcpy(dev_addr.addr, event->address.addr, 6);
1809 _bt_convert_addr_type_to_string(address,
1810 (unsigned char *)dev_addr.addr);
1811 BT_INFO("GATT Server ACQUIRE notify Req remote address : [%s]", address);
1813 req_info = g_new0(struct gatt_server_req_info, 1);
1814 req_info->request_id = event->attr_trans.trans_id;
1815 req_info->attribute_handle = event->attr_trans.attr_handle;
1816 req_info->connection_id = event->attr_trans.conn_id;
1817 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_ACQUIRE_NOTIFY;
1818 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1820 param = g_variant_new("(iiiiiis)", result,
1821 event->attr_trans.conn_id,
1822 event->attr_trans.trans_id,
1823 event->attr_trans.attr_handle,
1824 event->mtu, event->attr_trans.offset,
1827 BT_INFO("GATT Server ACQUIRE Notify Req Attribute : ");
1829 _bt_send_event(BT_GATT_SERVER_EVENT,
1830 BLUETOOTH_EVENT_GATT_SERVER_ACQUIRE_NOTIFY,
1834 static void __bt_handle_gatt_server_write_requested(event_gatts_srvc_write_attr_t *event)
1837 bluetooth_device_address_t dev_addr;
1838 GVariant *param = NULL;
1839 int result = BLUETOOTH_ERROR_NONE;
1841 gboolean is_prepare_write;
1843 GVariant *data = NULL;
1845 struct gatt_server_req_info *req_info = NULL;
1846 BT_INFO("GATT Server Write Requested");
1848 memcpy(dev_addr.addr, event->address.addr, 6);
1850 BT_INFO("GATT Server Write Req Connection ID: [%d]", event->attr_trans.conn_id);
1851 BT_INFO("GATT Server Write Req Transaction ID:[%d]", event->attr_trans.trans_id);
1852 BT_INFO("GATT Server Write Req Attribute Handle: [%d]", event->attr_trans.attr_handle);
1853 BT_INFO("GATT Server Write Req Attribute Offset: [%d]", event->attr_trans.offset);
1854 BT_INFO("GATT Server Write Req value length [%d]", event->length);
1855 BT_INFO("GATT Server Write Req needs response: [%d]", event->need_rsp);
1856 BT_INFO("GATT Server Write Req Is Prep: [%d]", event->is_prep);
1858 if (event->length <= 0) {
1859 BT_INFO("GATT Server write requested, but length of attr value is 0");
1863 need_resp = event->need_rsp;
1864 is_prepare_write = event->is_prep;
1866 write_val = g_memdup(&event->value[0], event->length);
1868 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1869 _bt_convert_addr_type_to_string(address,
1870 (unsigned char *)dev_addr.addr);
1872 BT_INFO("GATT Server Write Request from remote client [%s]", address);
1874 if (event->length > 0) {
1875 for (i = 0; i < event->length; i++)
1876 BT_DBG("Data[%d] = [0x%x]", i, event->value[i]);
1878 /* Save Write Request Info */
1879 req_info = g_new0(struct gatt_server_req_info, 1);
1880 req_info->request_id = event->attr_trans.trans_id;
1881 req_info->attribute_handle = event->attr_trans.attr_handle;
1882 req_info->connection_id = event->attr_trans.conn_id;
1883 req_info->addr = address;
1884 req_info->offset = event->attr_trans.offset;
1885 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_WRITE;
1886 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1888 data = g_variant_new_from_data(
1889 G_VARIANT_TYPE_BYTESTRING,
1894 param = g_variant_new("(iiiiiibbsn@ay)", result,
1895 event->attr_trans.conn_id,
1896 event->attr_trans.trans_id,
1897 event->attr_trans.attr_handle,
1898 event->attr_trans.offset,
1906 _bt_send_event(BT_GATT_SERVER_EVENT,
1907 BLUETOOTH_EVENT_GATT_SERVER_VALUE_CHANGED,
1913 static void __bt_handle_gatt_server_read_requested(event_gatts_srvc_read_attr_t *event)
1915 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1916 bluetooth_device_address_t dev_addr;
1917 int result = BLUETOOTH_ERROR_NONE;
1918 struct gatt_server_req_info *req_info = NULL;
1919 GVariant *param = NULL;
1921 BT_INFO("GATT Server Read Requested");
1923 memcpy(dev_addr.addr, event->address.addr, 6);
1925 BT_INFO("GATT Server-Client Connection ID: [%d]", event->attr_trans.conn_id);
1926 BT_INFO("GATT Server-Client Transaction ID: [%d]", event->attr_trans.trans_id);
1927 BT_INFO("GATT Server Attribute Handle: [%d]", event->attr_trans.attr_handle);
1928 BT_INFO("GATT Server Attribute Offset for read: [%d]", event->attr_trans.offset);
1929 BT_INFO("GATT Server Attribute is long: [%d]", event->is_long);
1931 is_long = event->is_long;
1933 /* Save Read Request Info */
1934 req_info = g_new0(struct gatt_server_req_info, 1);
1935 req_info->request_id = event->attr_trans.trans_id;
1936 req_info->attribute_handle = event->attr_trans.attr_handle;
1937 req_info->connection_id = event->attr_trans.conn_id;
1938 req_info->addr = address;
1939 req_info->offset = event->attr_trans.offset;
1940 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_READ;
1941 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1943 /* Send event to BT-API */
1944 _bt_convert_addr_type_to_string(address,
1945 (unsigned char *)dev_addr.addr);
1947 BT_INFO("GATT Server Read Request from remote client [%s]", address);
1949 param = g_variant_new("(iiiiibs)", result,
1950 event->attr_trans.conn_id,
1951 event->attr_trans.trans_id,
1952 event->attr_trans.attr_handle,
1953 event->attr_trans.offset,
1957 _bt_send_event(BT_GATT_SERVER_EVENT,
1958 BLUETOOTH_EVENT_GATT_SERVER_READ_REQUESTED,
1962 static void __bt_handle_gatt_server_indicate_confirmed(event_gatts_ind_cnfrm_t *event)
1964 bluetooth_device_address_t dev_addr;
1965 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1966 int cur_connected_clients;
1967 static int recvd = 0;
1968 gboolean completed = 0;
1969 GVariant *param = NULL;
1971 /* OAL event does provide error, so MW assumes event will never contain wrong data,
1972 incase of any issues, check with OAL */
1973 int result = BLUETOOTH_ERROR_NONE;
1975 memcpy(dev_addr.addr, event->address.addr, 6);
1976 _bt_convert_addr_type_to_string(address,
1977 (unsigned char *)dev_addr.addr);
1979 BT_INFO("Indication sent to GATT client [%s] conn_ ID [%d] transaction ID [%d] Att handle [%d]",
1980 address, event->conn_id, event->trans_id, event->attr_handle);
1983 cur_connected_clients = g_slist_length(gatt_client_info_list);
1984 BT_INFO("Number of connected clients during sending Indication [%d] & current connected count [%d]",
1985 num_indicate_clients, cur_connected_clients);
1988 if (recvd == num_indicate_clients) {
1989 BT_INFO("Gatt indication confirm event for last GATT client.. [%s]", address);
1990 completed = 1; /* Last event */
1991 recvd = 0; /* Reset */
1992 num_indicate_clients = 0;
1995 param = g_variant_new("(isib)",
2001 /* Send event to BT-API */
2002 _bt_send_event(BT_GATT_SERVER_EVENT,
2003 BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_COMPLETED,
2006 BT_INFO("Received Indication confirm for client number [%d]", recvd);
2010 /* Tizen Platform Specific */
2011 static void __bt_handle_gatt_server_notification_changed(event_gatts_notif_t *event)
2013 bluetooth_device_address_t dev_addr;
2014 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2015 GVariant *param = NULL;
2018 /* OAL event does provide error, so MW assumes event will never contain wrong data,
2019 incase of any issues, check with OAL */
2020 int result = BLUETOOTH_ERROR_NONE;
2022 memcpy(dev_addr.addr, event->address.addr, 6);
2023 _bt_convert_addr_type_to_string(address,
2024 (unsigned char *)dev_addr.addr);
2026 BT_INFO("notification_changed [%s] conn_ ID [%d] transaction ID [%d] Att handle [%d] Notify[%d]",
2027 address, event->conn_id, event->trans_id, event->attr_handle, event->notify);
2029 /* Set Notifcation status */
2030 notify = event->notify;
2032 param = g_variant_new("(isib)",
2038 /* Send event to BT-API */
2039 _bt_send_event(BT_GATT_SERVER_EVENT,
2040 BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_STATE_CHANGED,
2046 static void __bt_handle_gatt_mtu_changed_event(event_gatts_mtu_changed_t *event)
2048 int result = BLUETOOTH_ERROR_NONE;
2049 struct gatt_client_info_t *conn_info = NULL;
2050 GVariant *param = NULL;
2053 conn_info = __bt_find_remote_gatt_client_info_from_conn_id(event->conn_id);
2054 if (conn_info == NULL) {
2055 BT_ERR("Cant find connection Information");
2058 BT_INFO("Got connection Info GATT client [%s] MTU Size [%d]",
2059 conn_info->addr, event->mtu_size);
2061 __bt_update_mtu_gatt_device(conn_info->addr, event->mtu_size);
2063 param = g_variant_new("(isqy)",
2069 /* Send event to BT-API */
2070 _bt_send_event(BT_GATT_SERVER_EVENT,
2071 BLUETOOTH_EVENT_GATT_ATT_MTU_CHANGED,
2075 static void __bt_gatt_event_handler(int event_type, gpointer event_data)
2077 BT_INFO("OAL event = 0x%x, \n", event_type);
2078 switch (event_type) {
2079 case OAL_EVENT_BLE_SERVER_INSTANCE_INITIALISED: {
2080 BT_INFO("OAL Event: Server Instance Registered");
2081 /* GATT Server Registered event is handled in MAIN thread context */
2082 __bt_handle_server_instance_registered((event_gatts_register_t *)event_data);
2085 case OAL_EVENT_GATTS_SERVICE_ADDED: {
2086 BT_INFO("OAL Event: GATT Service added");
2087 __bt_handle_gatt_server_service_added((event_gatts_srvc_prm_t *)event_data);
2090 case OAL_EVENT_GATTS_CHARACTERISTIC_ADDED: {
2091 BT_INFO("OAL Event: GATT characteristic added");
2092 __bt_handle_gatt_server_characteristic_added((event_gatts_srvc_charctr_t *)event_data);
2095 case OAL_EVENT_GATTS_DESCRIPTOR_ADDED: {
2096 BT_INFO("OAL Event: GATT descriptor added");
2097 __bt_handle_gatt_server_descriptor_added((event_gatts_srvc_descr_t *)event_data);
2100 case OAL_EVENT_GATTS_SERVICE_STARTED: {
2101 BT_INFO("OAL Event: GATT Service started");
2102 __bt_handle_gatt_server_service_started((event_gatts_srvc_t *)event_data);
2105 case OAL_EVENT_GATTS_SERVICE_STOPED: {
2106 BT_INFO("OAL Event: GATT Service stopped");
2107 __bt_handle_gatt_server_service_stopped((event_gatts_srvc_t *)event_data);
2110 case OAL_EVENT_GATTS_SERVICE_DELETED: {
2111 BT_INFO("OAL Event: GATT Service deleted");
2112 __bt_handle_gatt_server_service_deleted((event_gatts_srvc_t *) event_data);
2115 case OAL_EVENT_GATTS_CONNECTION_COMPLETED: {
2116 BT_INFO("OAL Event: GATT Server Connected");
2117 __bt_handle_gatt_server_connection_state((event_gatts_conn_t *)event_data);
2120 case OAL_EVENT_GATTS_DISCONNECTION_COMPLETED: {
2121 BT_INFO("OAL Event: GATT Server Disconnected");
2122 __bt_handle_gatt_server_disconnection_state((event_gatts_conn_t *)event_data);
2125 case OAL_EVENT_GATTS_REQUEST_READ: {
2126 BT_INFO("OAL Event: GATT Server Read Request");
2127 __bt_handle_gatt_server_read_requested((event_gatts_srvc_read_attr_t *)event_data);
2130 case OAL_EVENT_GATTS_REQUEST_WRITE: {
2131 BT_INFO("OAL Event: GATT Server Write Request");
2132 __bt_handle_gatt_server_write_requested((event_gatts_srvc_write_attr_t *)event_data);
2135 case OAL_EVENT_GATTS_REQUEST_ACQUIRE_WRITE: {
2136 BT_INFO("OAL Event: GATT Server Acquire Write Request");
2137 __bt_handle_gatt_server_acquire_write_requested((event_gatts_srvc_acquire_attr_t*)event_data);
2140 case OAL_EVENT_GATTS_REQUEST_ACQUIRE_NOTIFY: {
2141 BT_INFO("OAL Event: GATT ServerAcquire Notify Request");
2142 __bt_handle_gatt_server_acquire_notify_requested((event_gatts_srvc_acquire_attr_t*)event_data);
2145 case OAL_EVENT_GATTS_IND_CONFIRM: {
2146 BT_INFO("OAL Event: GATT Server Indication confirmed");
2147 __bt_handle_gatt_server_indicate_confirmed((event_gatts_ind_cnfrm_t *)event_data);
2150 case OAL_EVENT_GATTS_NOTIFICATION: { /* Tizen Platform Specific */
2151 BT_INFO("OAL Event: GATT Server DisConnected");
2152 __bt_handle_gatt_server_notification_changed((event_gatts_notif_t *)event_data);
2155 case OAL_EVENT_GATTS_MTU_CHANGED: {
2156 BT_INFO("OAL Event: GATT Server MTU changed event callback");
2157 __bt_handle_gatt_mtu_changed_event((event_gatts_mtu_changed_t *)event_data);
2160 #ifdef TIZEN_GATT_CLIENT
2161 case OAL_EVENT_GATTC_REGISTRATION: {
2162 BT_INFO("OAL Event: GATT Client instance Registered");
2163 __bt_handle_client_instance_registered((event_gattc_register_t *) event_data);
2166 case OAL_EVENT_GATTC_CONNECTION_COMPLETED: {
2167 BT_INFO("OAL Event: GATT Client Connected");
2168 __bt_handle_client_connected((event_gattc_conn_t *) event_data);
2171 case OAL_EVENT_GATTC_DISCONNECTION_COMPLETED: {
2172 BT_INFO("OAL Event: GATT Client DisConnected");
2173 __bt_handle_client_disconnected((event_gattc_conn_t *) event_data);
2176 case OAL_EVENT_GATTC_SERVICE_SEARCH_RESULT: {
2177 BT_INFO("OAL Event: GATT Client Service Search Result");
2178 __bt_handle_client_service_search_result((event_gattc_service_result_t *) event_data);
2181 case OAL_EVENT_GATTC_SERVICE_SEARCH_DONE: {
2182 BT_INFO("OAL Event: GATT Client Service Completed");
2183 __bt_handle_client_service_search_completed((event_gattc_conn_status_t *) event_data);
2186 case OAL_EVENT_GATTC_CHARAC_SERACH_RESULT: {
2187 BT_INFO("OAL Event: GATT Client Characteristic Search Result");
2188 __bt_handle_client_characteristic_search_result((event_gattc_characteristic_result_t *) event_data);
2191 case OAL_EVENT_GATTC_DESC_SERACH_RESULT: {
2192 BT_INFO("OAL Event: GATT Client Descriptor Search Result");
2193 __bt_handle_client_descriptor_search_result((event_gattc_descriptor_result_t *) event_data);
2196 case OAL_EVENT_GATTC_READ_CHARAC: {
2197 BT_INFO("OAL Event: GATT Client Characteristic Read Data");
2198 __bt_handle_client_characteristic_read_data((event_gattc_read_data *) event_data);
2201 case OAL_EVENT_GATTC_READ_DESCR: {
2202 BT_INFO("OAL Event: GATT Client Descriptor Read Data");
2203 __bt_handle_client_descriptor_read_data((event_gattc_read_data *) event_data);
2206 case OAL_EVENT_GATTC_WRITE_CHARAC: {
2207 BT_INFO("OAL Event: GATT Client Characteristic Write Data");
2208 __bt_handle_client_characteristic_write_data((event_gattc_write_data *) event_data);
2211 case OAL_EVENT_GATTC_WRITE_DESCR: {
2212 BT_INFO("OAL Event: GATT Client Descriptor Write Data");
2213 __bt_handle_client_descriptor_write_data((event_gattc_write_data *) event_data);
2216 case OAL_EVENT_DEVICE_LE_DISCONNECTED: {
2217 BT_INFO("OAL Event: LE device disconnected");
2218 __bt_hanlde_le_device_disconnection((event_dev_conn_status_t *)event_data);
2221 case OAL_EVENT_GATTC_NOTIFICATION_REGISTERED: {
2222 BT_INFO("OAL Event: GATT Client Notification Registered");
2223 __bt_handle_client_notification_registered((event_gattc_regdereg_notify_t *) event_data, TRUE);
2226 case OAL_EVENT_GATTC_NOTIFICATION_DEREGISTERED: {
2227 BT_INFO("OAL Event: GATT Client Notification Registered");
2228 __bt_handle_client_notification_registered((event_gattc_regdereg_notify_t *) event_data, FALSE);
2231 case OAL_EVENT_GATTC_NOTIFY_DATA: {
2232 BT_INFO("OAL Event: GATT Client Notification Data");
2233 __bt_handle_client_notification_data((event_gattc_notify_data *) event_data);
2236 case OAL_EVENT_GATTC_SERVICE_CHANGED_IND: {
2237 BT_INFO("OAL Event: GATT Client service changed indication");
2238 __bt_handle_client_service_changed_ind((event_gattc_service_changed_data *)event_data);
2241 case OAL_EVENT_GATTC_MTU_EXCHANGE_COMPLETED: {
2242 BT_INFO("OAL Event: GATT Client MTU Exchange Complete");
2243 __bt_handle_client_mtu_exchange_completed((event_gattc_mtu_configured_t *) event_data);
2252 int _bt_gatt_server_add_service(char *sender, int service_type,
2253 int num_handles, char *svc_uuid, int instance_id)
2255 BT_CHECK_PARAMETER(svc_uuid, return);
2256 BT_CHECK_PARAMETER(sender, return);
2257 int ret = OAL_STATUS_SUCCESS;
2259 oal_gatt_srvc_id_t svc_data;
2261 svc_data.is_prmry = service_type;
2262 svc_data.id.inst_id = instance_id;
2264 BT_INFO("Service UUID [%s] Num handles [%d] Instance ID [%d]", svc_uuid, num_handles, instance_id);
2265 _bt_string_to_uuid(svc_uuid, (service_uuid_t*)&svc_data.id.uuid);
2267 ret = gatts_add_service(instance_id, &svc_data, num_handles);
2268 if (ret != OAL_STATUS_SUCCESS) {
2269 BT_ERR("ret: %d", ret);
2270 return BLUETOOTH_ERROR_INTERNAL;
2273 return BLUETOOTH_ERROR_NONE;
2277 int _bt_gatt_server_add_included_service(char *sender, int instance_id,
2278 int service_handle, int included_svc_handle)
2280 BT_CHECK_PARAMETER(sender, return);
2281 int ret = OAL_STATUS_SUCCESS;
2283 ret = gatts_add_included_services(instance_id, service_handle, included_svc_handle);
2284 if (ret != OAL_STATUS_SUCCESS) {
2285 BT_ERR("ret: %d", ret);
2286 return BLUETOOTH_ERROR_INTERNAL;
2288 return BLUETOOTH_ERROR_NONE;
2291 int _bt_gatt_server_add_characteristic(char *sender, char *char_uuid,
2292 bluetooth_gatt_server_attribute_params_t *param)
2294 BT_CHECK_PARAMETER(char_uuid, return);
2295 BT_CHECK_PARAMETER(sender, return);
2296 BT_CHECK_PARAMETER(param, return);
2297 int ret = OAL_STATUS_SUCCESS;
2299 oal_uuid_t uuid = {{0} };
2301 BT_INFO("Char UUID [%s] Instance ID [%d]", char_uuid, param->instance_id);
2302 _bt_string_to_uuid(char_uuid, (service_uuid_t*)&uuid);
2304 BT_INFO("Char permission From API [0x%x]", param->permissions);
2306 ret = gatts_add_characteristics(param->instance_id, param->service_handle, &uuid,
2307 param->properties, (int)param->permissions);
2308 if (ret != OAL_STATUS_SUCCESS) {
2309 BT_ERR("ret: %d", ret);
2310 return BLUETOOTH_ERROR_INTERNAL;
2312 return BLUETOOTH_ERROR_NONE;
2315 int _bt_gatt_server_add_descriptor(char *sender, char *desc_uuid,
2316 bt_gatt_permission_t *param, int service_handle, int instance_id)
2318 BT_CHECK_PARAMETER(desc_uuid, return);
2319 BT_CHECK_PARAMETER(sender, return);
2320 BT_CHECK_PARAMETER(param, return);
2321 int ret = OAL_STATUS_SUCCESS;
2323 oal_uuid_t uuid = {{0} };
2325 BT_INFO("Descriptor UUID [%s] Instance ID [%d] Service handle [%d]",
2326 desc_uuid, service_handle, instance_id);
2328 _bt_string_to_uuid(desc_uuid, (service_uuid_t*)&uuid);
2330 BT_INFO("Descriptor permission From API [0x%x]", *param);
2331 ret = gatts_add_descriptor(instance_id, service_handle, &uuid, (int)*param);
2333 if (ret != OAL_STATUS_SUCCESS) {
2334 BT_ERR("ret: %d", ret);
2335 return BLUETOOTH_ERROR_INTERNAL;
2337 return BLUETOOTH_ERROR_NONE;
2340 int _bt_gatt_server_start_service(char *sender, int service_handle, int instance_id)
2342 BT_CHECK_PARAMETER(sender, return);
2343 int ret = OAL_STATUS_SUCCESS;
2345 ret = gatts_start_service(instance_id, service_handle, BT_GATT_TRANSPORT_LE);
2346 if (ret != OAL_STATUS_SUCCESS) {
2347 BT_ERR("ret: %d", ret);
2348 return BLUETOOTH_ERROR_INTERNAL;
2350 return BLUETOOTH_ERROR_NONE;
2353 int _bt_gatt_server_stop_service(char *sender, int service_handle, int instance_id)
2355 BT_CHECK_PARAMETER(sender, return);
2356 int ret = OAL_STATUS_SUCCESS;
2358 ret = gatts_stop_service(instance_id, service_handle);
2359 if (ret != OAL_STATUS_SUCCESS) {
2360 BT_ERR("ret: %d", ret);
2361 return BLUETOOTH_ERROR_INTERNAL;
2363 return BLUETOOTH_ERROR_NONE;
2366 int _bt_gatt_server_delete_service(char *sender, int service_handle, int instance_id)
2368 BT_CHECK_PARAMETER(sender, return);
2369 int ret = OAL_STATUS_SUCCESS;
2373 ret = gatts_delete_service(instance_id, service_handle);
2374 if (ret != OAL_STATUS_SUCCESS) {
2375 BT_ERR("ret: %d", ret);
2376 return BLUETOOTH_ERROR_INTERNAL;
2379 /* Remove the Service Handle */
2380 for (l = numapps[instance_id].service_handles; l != NULL;) {
2382 l = g_slist_next(l);
2383 if (handle && *handle == service_handle) {
2384 BT_INFO("Remove Service handle [%d]", *handle);
2385 numapps[instance_id].service_handles = g_slist_remove(numapps[instance_id].service_handles, handle);
2391 return BLUETOOTH_ERROR_NONE;
2394 int _bt_gatt_server_send_response(char *sender, bluetooth_gatt_att_data_t *data,
2395 bluetooth_gatt_server_response_params_t *param)
2397 BT_CHECK_PARAMETER(sender, return);
2398 BT_CHECK_PARAMETER(data, return);
2399 BT_CHECK_PARAMETER(param, return);
2400 struct gatt_server_req_info *req_info = NULL;
2401 int ret = OAL_STATUS_SUCCESS;
2403 oal_gatt_response_t response;
2405 BT_INFO("GATT Server Response: Req Type [%d] req_id [%d] status [%d] auth_req [%d] offset[%d] data len[%d]",
2406 param->req_type, param->request_id,
2407 param->response_status, param->auth_req,
2408 data->offset, data->length);
2410 /* Search for matching Request in List */
2411 req_info = __bt_gatt_server_find_request_info(param->request_id, param->req_type);
2413 BT_ERR("GATT Server Req Info not found for current response..return Error");
2414 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
2417 memset(&response, 0x00, sizeof(oal_gatt_response_t));
2419 response.handle = req_info->attribute_handle;
2420 response.attr_value.auth_req = param->auth_req;
2421 response.attr_value.handle = req_info->attribute_handle;
2422 response.attr_value.offset = data->offset;
2423 response.attr_value.len = data->length;
2424 memcpy(&response.attr_value.value, &data->data, data->length);
2427 ret = gatts_send_response(req_info->connection_id, param->request_id,
2428 param->response_status, &response);
2430 if (ret != OAL_STATUS_SUCCESS) {
2431 BT_ERR("ret: %d", ret);
2432 return BLUETOOTH_ERROR_INTERNAL;
2435 BT_INFO("GATT Server Response successfully sent");
2436 /* Remove GATT server request from list */
2437 gatt_server_requests = g_slist_remove(gatt_server_requests, req_info);
2438 g_free(req_info->addr);
2440 return BLUETOOTH_ERROR_NONE;
2443 int _bt_gatt_server_acquire_send_response(char *sender, bluetooth_gatt_server_acquire_response_params_t *param , void *fd_list)
2445 BT_CHECK_PARAMETER(sender, return);
2446 BT_CHECK_PARAMETER(param, return);
2447 struct gatt_server_req_info *req_info = NULL;
2448 int ret = OAL_STATUS_SUCCESS;
2451 BT_INFO("GATT acquire Server Response: Req Type [%d] req_id [%d] fd [%d] mtu[%d]",
2452 param->req_type, param->request_id,
2456 /* Search for matching Request in List */
2457 req_info = __bt_gatt_server_find_request_info(param->request_id, param->req_type);
2459 BT_ERR("GATT acquire Server Req Info not found for current response..return Error");
2460 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
2463 ret = gatt_send_response_acquire(req_info->connection_id, param->request_id, 0, param->fd, param->mtu, fd_list);
2465 if (ret != OAL_STATUS_SUCCESS) {
2466 BT_ERR("ret: %d", ret);
2467 return BLUETOOTH_ERROR_INTERNAL;
2470 BT_INFO("GATT acquire Server Response successfully sent");
2471 /* Remove GATT server request from list */
2472 gatt_server_requests = g_slist_remove(gatt_server_requests, req_info);
2473 g_free(req_info->addr);
2475 return BLUETOOTH_ERROR_NONE;
2480 int _bt_gatt_server_send_indication(char *sender, bluetooth_device_address_t *dev_addr,
2481 bluetooth_gatt_att_data_t *data,
2482 bluetooth_gatt_server_indication_params_t *param)
2484 BT_CHECK_PARAMETER(sender, return);
2485 BT_CHECK_PARAMETER(data, return);
2486 BT_CHECK_PARAMETER(param, return);
2488 gboolean all_send = FALSE;
2489 int ret = OAL_STATUS_SUCCESS;
2490 struct gatt_client_info_t *conn;
2492 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2493 _bt_convert_addr_type_to_string(address, dev_addr->addr);
2495 if (memcmp(dev_addr->addr, BDADDR_ANY, 6) == 0) {
2496 BT_INFO("GATT Server: Send Indication to all connected GATT clients..");
2499 BT_INFO("GATT Server: Send Indication to connected GATT client addr [%s]", address);
2502 /* Attempt to send Notification/Indication to all Connected GATT clients */
2504 ret = __bt_gatt_send_indication_to_all_connected_clients(data, param);
2505 if (ret != OAL_STATUS_SUCCESS) {
2506 BT_ERR("ret: %d", ret);
2508 return BLUETOOTH_ERROR_INTERNAL;
2512 conn = _bt_find_remote_gatt_client_info(address);
2514 ret = gatts_send_indication(param->instance_id, param->atrribute_handle,
2515 conn->connection_id, data->length,
2516 param->need_confirmation, (char *)(&data->data[0]));
2518 if (ret != OAL_STATUS_SUCCESS) {
2519 BT_ERR("ret: %d", ret);
2520 BT_INFO("Indication failed to send to Remote GATT Client [%s]", address);
2522 return BLUETOOTH_ERROR_INTERNAL;
2524 BT_INFO("Indication sent to Remote GATT Client [%s] wait for Notification completed event from OAL", address);
2526 num_indicate_clients = 1;
2527 return BLUETOOTH_ERROR_NONE;
2529 BT_ERR("Remote GATT client [%s] is not connected..Cant send Indication!!", address);
2531 return BLUETOOTH_ERROR_NOT_CONNECTED;
2534 return BLUETOOTH_ERROR_NONE;
2537 int _bt_gatt_server_update_attribute_value(char *sender, int instance_id,
2538 bluetooth_gatt_server_update_value_t *param)
2540 BT_CHECK_PARAMETER(sender, return);
2541 BT_CHECK_PARAMETER(param, return);
2542 int ret = OAL_STATUS_SUCCESS;
2544 oal_gatt_value_t value;
2545 BT_INFO("GATT Server Update value: Instance ID [%d] attr handle [%d] Value len [%d]",
2546 instance_id, param->attribute_handle, param->length);
2549 memset(&value, 0x00, sizeof(oal_gatt_value_t));
2551 value.handle = param->attribute_handle;
2552 value.len = param->length;
2553 memcpy(&value.value, ¶m->data.data, param->length);
2555 ret = gatts_update_att_value(instance_id, &value);
2557 if (ret != OAL_STATUS_SUCCESS) {
2558 BT_ERR("ret: %d", ret);
2559 return BLUETOOTH_ERROR_INTERNAL;
2562 BT_INFO("GATT Server Update Attribute Value successfully");
2563 return BLUETOOTH_ERROR_NONE;
2566 int _bt_request_att_mtu(bluetooth_device_address_t *device_address,
2569 struct gatt_server_info_t *conn_info = NULL;
2571 int ret = OAL_STATUS_SUCCESS;
2573 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
2574 _bt_convert_addr_type_to_string(addr, device_address->addr);
2576 /* Check if remote GATT Server is connected or not */
2577 conn_info = _bt_find_remote_gatt_server_info(addr);
2579 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
2580 conn_info->addr, conn_info->connection_id);
2582 BT_ERR("GATT Server is not yet connected..");
2584 return BLUETOOTH_ERROR_NOT_CONNECTED;
2587 ret = gattc_configure_mtu(conn_info->connection_id, mtu);
2588 if (ret != OAL_STATUS_SUCCESS) {
2589 BT_ERR("ret: %d", ret);
2591 return BLUETOOTH_ERROR_INTERNAL;
2595 return BLUETOOTH_ERROR_NONE;
2598 int _bt_get_att_mtu(bluetooth_device_address_t *address,
2601 BT_CHECK_PARAMETER(address, return);
2602 BT_CHECK_PARAMETER(mtu, return);
2603 struct gatt_client_info_t *conn_info = NULL;
2604 char addr[BT_ADDRESS_STRING_SIZE] = { 0 };
2605 int ret = OAL_STATUS_SUCCESS;
2608 _bt_convert_addr_type_to_string(addr, address->addr);
2610 BT_INFO("Get current MTU size for the remote client:DevAddress:[%s]", addr);
2612 conn_info = _bt_find_remote_gatt_client_info(addr);
2614 BT_INFO("GATT Client [%s] is connected, conn Id [%d] Instance ID [%d]",
2615 conn_info->addr, conn_info->connection_id, conn_info->instance_id);
2617 BT_ERR("GATT Client [%s] is not yet connected..", addr);
2618 return BLUETOOTH_ERROR_NOT_CONNECTED;
2621 ret = gatts_get_att_mtu(conn_info->connection_id, &stack_mtu);
2622 if (ret != OAL_STATUS_SUCCESS) {
2623 BT_ERR("ret: %d", ret);
2624 return BLUETOOTH_ERROR_INTERNAL;
2627 BT_INFO("ATT MTU received from OAL [%d]", stack_mtu);
2628 *mtu = (unsigned int)stack_mtu;
2631 BT_ERR("MTU value is zero, GATT Client [%s] is not yet connected..", addr);
2632 return BLUETOOTH_ERROR_NOT_CONNECTED;
2635 return BLUETOOTH_ERROR_NONE;
2638 #ifdef TIZEN_GATT_CLIENT
2639 /* GATT Client utility static functions */
2640 static bt_gatt_service_info_list_t *__bt_get_service_info_list(int conn_id)
2643 bt_gatt_service_info_list_t *info = NULL;
2645 for (l = list_gatt_info; l != NULL; l = g_slist_next(l)) {
2646 info = (bt_gatt_service_info_list_t *)l->data;
2650 if (info->conn_id == conn_id)
2657 static bt_gatt_service_info_t *__bt_find_matching_service(
2658 bt_gatt_service_info_list_t *svc_list, oal_gatt_srvc_id_t *svc)
2661 bt_gatt_service_info_t *info = NULL;
2663 for (l = svc_list->services; l != NULL; l = g_slist_next(l)) {
2664 info = (bt_gatt_service_info_t *)l->data;
2668 /* Match UUID and instance ID */
2669 if (!memcmp(&svc->id.uuid.uuid, &info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
2670 && (svc->id.inst_id == info->inst_id)) {
2677 static bt_gatt_char_info_t *__bt_find_matching_charc(
2678 bt_gatt_service_info_t *svc_info, oal_gatt_id_t *charc)
2681 bt_gatt_char_info_t *info = NULL;
2683 for (l = svc_info->chars; l != NULL; l = g_slist_next(l)) {
2684 info = (bt_gatt_char_info_t *)l->data;
2688 /* Match UUID and instance ID */
2689 if (!memcmp(&charc->uuid.uuid, &info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
2690 && (charc->inst_id == info->inst_id)) {
2697 static bt_gatt_descriptor_info_t *__bt_find_matching_desc(
2698 bt_gatt_char_info_t *char_info, oal_gatt_id_t *desc)
2701 bt_gatt_descriptor_info_t *info = NULL;
2703 for (l = char_info->descs; l != NULL; l = g_slist_next(l)) {
2704 info = (bt_gatt_descriptor_info_t *)l->data;
2708 /* Match UUID and instance ID */
2709 if (!memcmp(&desc->uuid, &info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
2710 && (desc->inst_id == info->inst_id)) {
2718 static struct gatt_server_info_t *__bt_find_remote_gatt_server_info_from_conn_id(int conn_id)
2721 struct gatt_server_info_t *info = NULL;
2723 for (l = gatt_server_info_list; l != NULL; l = g_slist_next(l)) {
2724 info = (struct gatt_server_info_t*)l->data;
2728 if (info->connection_id == conn_id) {
2729 BT_INFO("Remote GATT server found addr[%s]", info->addr);
2736 static bt_gatt_service_info_t* __bt_find_removed_service(bt_gatt_service_info_list_t *svc_list)
2739 bt_gatt_service_info_t *info = NULL;
2741 for (l = svc_list->services; l != NULL; l = g_slist_next(l)) {
2742 info = (bt_gatt_service_info_t*)l->data;
2746 /* Service is marked a removed */
2747 if (info->is_removed == 1)
2753 static void __bt_remove_service_info_from_list(bt_gatt_service_info_t *svc_info)
2759 bt_gatt_char_info_t *charc = NULL;
2760 bt_gatt_included_service_info_t *incl = NULL;
2761 bt_gatt_descriptor_info_t *desc = NULL;
2763 /* Remove all Characteristic and Descriptors within characteristic */
2764 for (l = svc_info->chars; l != NULL;) {
2765 charc = (bt_gatt_char_info_t*)l->data;
2766 l = g_slist_next(l); /* Incase if l is removed, saving next to l */
2771 /* Inside Characteristic */
2772 for (l1 = charc->descs; l1 != NULL;) {
2774 desc = (bt_gatt_descriptor_info_t*)l1->data;
2775 l1 = g_slist_next(l1);
2780 /* Remove Descriptor */
2781 charc->descs = g_slist_remove(charc->descs, desc);
2784 /* Remove Characteristic */
2785 svc_info->chars = g_slist_remove(svc_info->chars, charc);
2789 /* Remove all Included Services */
2790 for (l2 = svc_info->included_svcs; l2 != NULL;) {
2791 incl = (bt_gatt_included_service_info_t*)l2->data;
2792 l2 = g_slist_next(l2); /* Incase if l is removed, saving next to l */
2797 /* Remove included service */
2798 svc_info->included_svcs = g_slist_remove(svc_info->included_svcs, incl);
2805 static void __bt_build_service_browse_info(int conn_id,
2806 bt_services_browse_info_t* info)
2809 bt_gatt_service_info_list_t *svc_info_list;
2810 bt_gatt_service_info_t *svc_info;
2812 service_uuid_t uuid;
2813 struct gatt_server_info_t *conn_info = NULL;
2815 char uuid_string[BLUETOOTH_UUID_STRING_MAX];
2817 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
2822 _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
2824 svc_info_list = __bt_get_service_info_list(conn_id);
2828 info->count = g_slist_length(svc_info_list->services);
2829 BT_DBG("Total services present in the svc info list for this conn id [%d] is [%d]",
2830 conn_id, info->count);
2832 for (l = svc_info_list->services; l != NULL; l = g_slist_next(l)) {
2833 svc_info = (bt_gatt_service_info_t*)l->data;
2834 if (svc_info == NULL)
2837 memcpy(&uuid.uuid, &svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2838 _bt_uuid_to_string(&uuid, uuid_string);
2840 BT_INFO("[%d] %s [%s]", count, uuid_string, _bt_convert_uuid_to_string(uuid_string));
2842 /* Fill UUID of service */
2843 g_strlcpy(info->uuids[count], uuid_string,
2844 BLUETOOTH_UUID_STRING_MAX);
2846 /* Fill instance ID of service */
2847 info->inst_id[count] = svc_info->inst_id;
2849 /* Fill primary service or not info */
2850 info->primary[count] = svc_info->is_primary;
2852 /* Increment count of services browsed */
2857 static void __bt_build_char_browse_info(int conn_id,
2858 bt_gatt_service_info_t *svc_info,
2859 bt_char_browse_info_t* info)
2862 bt_gatt_char_info_t *char_info;
2863 service_uuid_t uuid;
2865 struct gatt_server_info_t *conn_info = NULL;
2867 char uuid_string[BLUETOOTH_UUID_STRING_MAX];
2869 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
2873 /* Fill default data, this will be required even in case of failure */
2874 _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
2875 memcpy(&info->svc_uuid, svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2876 info->svc_inst_id = svc_info->inst_id;
2878 if (!svc_info->chars) {
2879 BT_ERR("No Chars browsed for address [%s]", conn_info->addr);
2883 info->count = g_slist_length(svc_info->chars);
2885 for (l = svc_info->chars; l != NULL; l = g_slist_next(l)) {
2886 char_info = (bt_gatt_char_info_t*)l->data;
2887 if (char_info == NULL)
2890 memcpy(&uuid.uuid, &char_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2891 _bt_uuid_to_string(&uuid, uuid_string);
2893 /* Fill UUID of characteristic */
2894 g_strlcpy(info->uuids[count], uuid_string,
2895 BLUETOOTH_UUID_STRING_MAX);
2897 /* Fill instance ID of characteristic */
2898 info->inst_id[count] = char_info->inst_id;
2900 /* Fill property of characteristic */
2901 info->props[count] = char_info->props;
2903 /* Increment count of services browsed */
2906 BT_DBG("Total characteristics browsed [%d]", count);
2909 static void __bt_build_descriptor_browse_info(int conn_id,
2910 bt_gatt_service_info_t *svc_info,
2911 bt_gatt_char_info_t *char_info,
2912 bt_descriptor_browse_info_t* info)
2915 bt_gatt_descriptor_info_t *desc_info;
2917 service_uuid_t uuid;
2918 struct gatt_server_info_t *conn_info = NULL;
2920 char uuid_string[BLUETOOTH_UUID_STRING_MAX];
2922 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
2924 /* Fill default data, this will be required even in case of failure */
2925 _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
2926 memcpy(&info->svc_uuid, svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2927 info->svc_inst_id = svc_info->inst_id;
2928 memcpy(&info->char_uuid, char_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2929 info->char_inst_id = char_info->inst_id;
2931 /* Fill property of the parent characteristic of this descriptor */
2932 info->char_props_map = char_info->props;
2934 info->count = g_slist_length(char_info->descs);
2936 if (!char_info->descs) {
2937 BT_ERR("No Descriptors browsed for address [%s]", conn_info->addr);
2941 for (l = char_info->descs; l != NULL; l = g_slist_next(l)) {
2942 desc_info = (bt_gatt_descriptor_info_t*)l->data;
2943 if (desc_info == NULL)
2946 memcpy(&uuid.uuid, &desc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2947 _bt_uuid_to_string(&uuid, uuid_string);
2949 /* Fill UUID of Descriptor */
2950 g_strlcpy(info->uuids[count], uuid_string,
2951 BLUETOOTH_UUID_STRING_MAX);
2953 /* Fill instance ID of Descriptor */
2954 info->inst_id[count] = desc_info->inst_id;
2957 /* Increment count of Descriptor browsed */
2961 BT_INFO("Total descriptors browsed [%d]", count);
2964 static void __bt_free_service_info(bt_gatt_service_info_t *svc)
2966 GSList *ll, *lll, *llll;
2967 bt_gatt_char_info_t *chr = NULL;
2968 bt_gatt_descriptor_info_t *desc = NULL;
2969 bt_gatt_included_service_info_t *incl_svc = NULL;
2971 BT_DBG("Service info Is Prim[%d] Inst ID [%d]", svc->is_primary, svc->inst_id);
2972 /* Delete all chars and its descriptors */
2973 for (ll = svc->chars; ll != NULL; ) {
2974 chr = (bt_gatt_char_info_t *)ll->data;
2975 ll = g_slist_next(ll);
2979 for (lll = chr->descs; lll != NULL; ) {
2980 desc = (bt_gatt_descriptor_info_t *)lll->data;
2981 lll = g_slist_next(lll);
2984 chr->descs = g_slist_remove(chr->descs, desc);
2987 svc->chars = g_slist_remove(svc->chars, chr);
2991 /* Delete all included services */
2992 for (llll = svc->included_svcs; llll != NULL; ) {
2993 incl_svc = (bt_gatt_included_service_info_t *)llll->data;
2994 llll = g_slist_next(llll);
2995 if (incl_svc == NULL)
2997 svc->included_svcs = g_slist_remove(svc->included_svcs, incl_svc);
3002 static void __bt_cleanup_remote_services(struct gatt_server_info_t *conn_info)
3004 bt_gatt_service_info_list_t *svc_info_list = NULL;
3005 bt_gatt_service_info_t *svc = NULL;
3008 svc_info_list = __bt_get_service_info_list(conn_info->connection_id);
3009 if (!svc_info_list) {
3010 BT_INFO("Could not find Svc Info list for the connection ID [%d]",
3011 conn_info->connection_id);
3015 BT_INFO("Start Cleanup of all services. Num Services [%d]", g_slist_length(svc_info_list->services));
3016 for (l = svc_info_list->services; l != NULL; ) {
3017 svc = (bt_gatt_service_info_t *)l->data;
3018 l = g_slist_next(l);
3022 __bt_free_service_info(svc);
3023 svc_info_list->services = g_slist_remove(svc_info_list->services, svc);
3027 list_gatt_info = g_slist_remove(list_gatt_info, svc_info_list);
3028 g_free(svc_info_list);
3031 int _bt_register_gatt_client_instance(const char *sender,
3032 bluetooth_device_address_t *address)
3034 int ret = OAL_STATUS_SUCCESS;
3035 char *uuid_string = NULL;
3040 /* App should ensure that it should not send */
3041 BT_INFO("### Check on which instance GATT Client instance can be initialized....");
3042 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
3043 if (numapps[k].is_initialized == 1) {
3044 BT_INFO("Instance ID [%d] is already in use..Check next slot",
3045 numapps[k].instance_id);
3048 BT_INFO("Time to register GATT client instancer..UUID to be used is [%s] slot [%d]",
3049 uuid_list[slot-1], slot);
3055 BT_ERR("No Slot if free for GATT Client registration..");
3056 return BLUETOOTH_ERROR_REGISTRATION_FAILED;
3059 uuid_string = g_malloc0(BT_UUID_STRING_MAX);
3060 _bt_string_to_uuid(uuid_list[slot-1], (service_uuid_t*)&uuid);
3061 g_strlcpy(uuid_string, uuid_list[slot-1], BT_UUID_STRING_MAX);
3062 BT_INFO("Copied UUID string [%s]", uuid_string);
3064 /* Register GATT Client */
3065 ret = gattc_register(&uuid);
3066 if (ret != OAL_STATUS_SUCCESS) {
3067 BT_ERR("ret: %d", ret);
3068 g_free(uuid_string);
3069 return BLUETOOTH_ERROR_INTERNAL;
3072 BT_INFO("GATT Client registration call successfully accepted by OAL..wait for Instance Initialized event from OAL..");
3074 /* Return & wait for GATT Client Instance Initialization event */
3075 memset(numapps[slot].sender, 0x00, sizeof(numapps[slot].sender));
3076 memset(numapps[slot].uuid, 0x00, sizeof(numapps[slot].uuid));
3078 g_strlcpy(numapps[slot].sender, sender, sizeof(numapps[slot].sender));
3079 g_strlcpy(numapps[slot].uuid, uuid_string, sizeof(numapps[slot].uuid));
3081 /* Address is saved here. When event comes, sender + address are matched for replying pending
3082 request. It is impossible for same sender to have requests with two same addresses */
3083 memcpy(&numapps[slot].address.addr, address->addr, sizeof(bluetooth_device_address_t));
3085 BT_INFO("Slot [%d] occupied", slot);
3086 numapps[slot].is_initialized = TRUE; /* Set initialization to true here itself */
3088 g_free(uuid_string);
3089 return BLUETOOTH_ERROR_NONE;
3095 /* GATT client events */
3096 static void __bt_handle_client_instance_registered(event_gattc_register_t *data)
3098 bt_service_app_info_t *info = NULL;
3100 char *uuid_string = g_malloc0(BT_UUID_STRING_MAX);
3102 _bt_uuid_to_string(&(data->client_uuid), uuid_string);
3103 BT_INFO("CLient ID is Initialized [%d] UUID initialized [%s]", data->client_if, uuid_string);
3105 /* Platform GATT client framwork does not use Default GATT client instance
3106 This GATT client instance is never deregistred in the lifetime of bt-service */
3107 if (g_strcmp0(uuid_string, DEFAULT_GATT_CLIENT_UUID) == 0) {
3108 BT_INFO("Default client Instance Registered");
3109 gatt_default_client = data->client_if;
3110 g_free(uuid_string);
3114 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
3117 if (g_strcmp0(info->uuid, uuid_string) == 0) {
3118 BT_INFO("Found GATT client.. sender [%s] Slot [%d] occupied", info->sender, k);
3119 info->is_initialized = TRUE;
3120 info->client_id = data->client_if;
3121 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE, BT_GATT_CLIENT_REGISTER,
3122 (void*)info, sizeof(bt_service_app_info_t));
3126 g_free(uuid_string);
3129 static void __bt_handle_client_connected(event_gattc_conn_t *event_data)
3131 int result = BLUETOOTH_ERROR_NONE;
3132 struct gatt_server_info_t *conn_info = NULL;
3133 struct gatt_out_conn_info_t *out_conn_info = NULL;
3135 GVariant *param = NULL;
3137 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
3138 _bt_convert_addr_type_to_string(address,
3139 (unsigned char *)event_data->address.addr);
3141 if (event_data->status != OAL_STATUS_SUCCESS)
3142 result = BLUETOOTH_ERROR_INTERNAL;
3144 /* DBUS Return fo BT_CONNECT_LE for all the apps */
3145 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE, address,
3146 BT_ADDRESS_STRING_SIZE);
3148 BT_INFO("Local GATT Client Connected: Remote addr[%s] Client Interface [%d] Connection ID [%d] status[%d]",
3149 address, event_data->client_if, event_data->conn_id, event_data->status);
3151 if (result == BLUETOOTH_ERROR_NONE) {
3152 /* Check if device is already in connected list */
3153 conn_info = _bt_find_remote_gatt_server_info(address);
3156 /* Send event to BT-API */
3157 param = g_variant_new("(is)", result, address);
3158 _bt_send_event(BT_DEVICE_EVENT,
3159 BLUETOOTH_EVENT_GATT_CLIENT_CONNECTED, /* Local device is GATT client */
3162 /* Save Connection info */
3163 conn_info = g_new0(struct gatt_server_info_t, 1);
3164 conn_info->addr = g_strdup(address);
3165 conn_info->client_id = event_data->client_if;
3166 #ifdef __INTEGRATE_GATT_INFO__
3167 conn_info->instance_id = -1;
3169 conn_info->connection_id = event_data->conn_id;
3170 gatt_server_info_list = g_slist_append(gatt_server_info_list, conn_info);
3171 BT_DBG("Total num of connected Remote GATT server devices [%d]",
3172 g_slist_length(gatt_server_info_list));
3175 BT_INFO("Do a Internal refresh");
3176 if (OAL_STATUS_SUCCESS != gattc_refresh(conn_info->client_id, &event_data->address))
3177 BT_ERR("GATT database refresh failed!!");
3179 BT_INFO("GATT database refresh Success!!");
3182 BT_ERR("Local GATT Client connected event for addr[%s], but device is in connected list already", address);
3184 __bt_add_mtu_gatt_device(address);
3186 BT_ERR("GATT Client Connection failed!!");
3188 /* If outgoing connection Info is present, then remove it */
3189 out_conn_info = __bt_find_gatt_outgoing_conn_info(address);
3190 if (out_conn_info) {
3191 BT_ERR("Outgoing Client connect request was sent");
3192 outgoing_gatt_conn_list = g_slist_remove(outgoing_gatt_conn_list, out_conn_info);
3193 g_free(out_conn_info->addr);
3194 g_free(out_conn_info);
3199 static void __bt_handle_client_disconnected(event_gattc_conn_t *event_data)
3201 int result = BLUETOOTH_ERROR_NONE;
3203 struct gatt_server_info_t *conn_info = NULL;
3204 struct gatt_out_conn_info_t *out_conn_info = NULL;
3206 GVariant *param = NULL;
3208 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
3209 _bt_convert_addr_type_to_string(address,
3210 (unsigned char *)event_data->address.addr);
3212 if (event_data->status != OAL_STATUS_SUCCESS)
3213 result = BLUETOOTH_ERROR_INTERNAL;
3215 if (NULL == _bt_get_request_info_data(BT_DISCONNECT_LE, address)) {
3216 if (NULL != _bt_get_request_info_data(BT_CONNECT_LE, address)) {
3217 result = BLUETOOTH_ERROR_INTERNAL;
3218 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
3219 address, BT_ADDRESS_STRING_SIZE);
3220 BT_ERR("Failed to connect Local GATT Remote addr[%s]", address);
3225 /* DBUS Return for BT_DISCONNECT_LE for all the apps */
3226 __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE, address,
3227 BT_ADDRESS_STRING_SIZE);
3229 BT_INFO("Local GATT Client DisConnected: Remote addr[%s] Client Interface [%d] Connection ID [%d] status [%d]",
3230 address, event_data->client_if, event_data->conn_id, event_data->status);
3232 /* Remove Connection info */
3233 conn_info = _bt_find_remote_gatt_server_info(address);
3236 param = g_variant_new("(is)", result, address);
3237 /* Send event to application */
3238 _bt_send_event(BT_DEVICE_EVENT,
3239 BLUETOOTH_EVENT_GATT_CLIENT_DISCONNECTED,
3242 BT_INFO("Remove GATT server info from List..");
3243 /* Remove all services from info list_gatt_info */
3244 __bt_cleanup_remote_services(conn_info);
3246 /* Remove info from List */
3247 gatt_server_info_list = g_slist_remove(gatt_server_info_list, conn_info);
3248 BT_INFO("Total num of connected GATT servers [%d]", g_slist_length(gatt_server_info_list));
3249 g_free(conn_info->addr);
3252 BT_INFO("Can not find conn info, already removed!");
3254 __bt_remove_mtu_gatt_device(address);
3256 /* If outgoing connection Info is present, then remove it */
3257 out_conn_info = __bt_find_gatt_outgoing_conn_info(address);
3258 if (out_conn_info) {
3259 BT_ERR("Client Disconnected event, but outgoing connect request was sent");
3260 outgoing_gatt_conn_list = g_slist_remove(outgoing_gatt_conn_list, out_conn_info);
3261 g_free(out_conn_info->addr);
3262 g_free(out_conn_info);
3268 static void __bt_handle_client_service_search_result(
3269 event_gattc_service_result_t *event_data)
3271 /* Pre: status is never fail from OAL */
3273 /* Find service list from address */
3274 bt_gatt_service_info_list_t *svc_info_list;
3275 bt_gatt_service_info_t *svc_info;
3277 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3278 if (!svc_info_list) {
3279 BT_DBG("Service info list not present for connection ID %d, means first time browse", event_data->conn_status.conn_id);
3280 /* Means for this conn_id, no services are ever browsed, first time,
3281 create service info list for this conn_id */
3282 svc_info_list = g_malloc0(sizeof(bt_gatt_service_info_list_t));
3283 svc_info_list->conn_id = event_data->conn_status.conn_id;
3284 list_gatt_info = g_slist_append(list_gatt_info, svc_info_list);
3287 /* send list and current service's uuid and instance id to find it */
3288 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3291 /* If not found, check if service changed, if yes, means this is a new service added
3292 in remote GATT device, update uuid info in svc info list structure, to be used when
3293 search is completed */
3295 if (svc_info_list->info.is_changed) {
3296 BT_DBG("Service Changed indication already found for connection ID %d", event_data->conn_status.conn_id);
3297 memcpy(svc_info_list->info.uuid, event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3299 /* Create and add new service in service list */
3300 svc_info = g_malloc0(sizeof(bt_gatt_service_info_t));
3301 memcpy(svc_info->uuid, event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3302 svc_info->inst_id = event_data->srvc_id.id.inst_id;
3303 svc_info->is_primary = event_data->srvc_id.is_prmry;
3304 svc_info_list->services = g_slist_append(svc_info_list->services, svc_info);
3305 BT_DBG("Service created and added in Svc info list or connection ID %d", event_data->conn_status.conn_id);
3307 /* If returned matching service info, then just update service_rmeoved value inside it to 0 */
3308 svc_info->is_removed = 0;
3312 static void __bt_handle_client_service_search_completed(
3313 event_gattc_conn_status_t *event_data)
3315 struct gatt_server_info_t *conn_info = NULL;
3316 bt_gatt_service_info_list_t *svc_info_list;
3317 bt_gatt_service_info_t *svc_info;
3318 bt_services_browse_info_t browse_info;
3319 unsigned char uuid_empty[BLUETOOTH_UUID_HEX_MAX_LEN];
3321 memset(&uuid_empty, 0x00, BLUETOOTH_UUID_HEX_MAX_LEN);
3322 memset(&browse_info, 0x00, sizeof(bt_services_browse_info_t));
3323 BT_INFO("Primary Services browsing completed status[%d] conn ID [%d]",
3324 event_data->status, event_data->conn_id);
3326 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(event_data->conn_id);
3328 svc_info_list = __bt_get_service_info_list(event_data->conn_id);
3329 if (!svc_info_list) {
3330 BT_ERR("No services browsed ever for addr [%s]", conn_info->addr);
3332 /* Just build response and return ERROR */
3333 __bt_build_service_browse_info(event_data->conn_id, &browse_info);
3335 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_INTERNAL,
3336 BT_GATT_GET_PRIMARY_SERVICES, &browse_info,
3337 sizeof(bt_services_browse_info_t));
3341 /* If fail, then send event with error */
3342 if (event_data->status != OAL_STATUS_SUCCESS) {
3343 /* Just build response and return ERROR */
3344 __bt_build_service_browse_info(event_data->conn_id, &browse_info);
3346 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_INTERNAL,
3347 BT_GATT_GET_PRIMARY_SERVICES, &browse_info,
3348 sizeof(bt_services_browse_info_t));
3352 /* If success, then find service info list from address */
3354 /* If svc_changed == 1 and uuid valid, means a new service is added*/
3355 if (svc_info_list->info.is_changed && !memcmp(uuid_empty, svc_info_list->info.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)) {
3356 /* TODO: Send event -Service added with instance ID and UUID of newly added service */
3357 BT_INFO("new service added");
3359 BT_INFO("TODO new service added");
3362 /* If svc_changed == 1 and uuid invalid, then a service is removed */
3363 if (svc_info_list->info.is_changed && memcmp(uuid_empty, svc_info_list->info.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)) {
3364 /* Scan through the service info list to find service with is_removed = 1*/
3365 svc_info = __bt_find_removed_service(svc_info_list);
3367 /* TODO Send event - Service removed with instance ID and UUID of just rmeoved service */
3369 /* Remove that service info from service info list */
3370 svc_info_list->services = g_slist_remove(svc_info_list->services, svc_info);
3372 /* Delete that service completely from svc_info list*/
3373 __bt_remove_service_info_from_list(svc_info);
3377 /* Reset svc_changed = 0, and reset UUID = all 0's */
3378 svc_info_list->info.is_changed = 0;
3379 memset(&svc_info_list->info.uuid, 0x00, BLUETOOTH_UUID_HEX_MAX_LEN);
3381 /* Build Reply and send to service browse primary services request of pending apps */
3382 __bt_build_service_browse_info(event_data->conn_id, &browse_info);
3384 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE,
3385 BT_GATT_GET_PRIMARY_SERVICES, &browse_info,
3386 sizeof(bt_services_browse_info_t));
3390 static void __bt_handle_client_characteristic_search_result(
3391 event_gattc_characteristic_result_t *event_data)
3393 bt_gatt_service_info_list_t *svc_info_list;
3394 bt_gatt_service_info_t *svc_info;
3395 bt_gatt_char_info_t *char_info;
3396 bt_char_browse_info_t browse_info;
3398 memset(&browse_info, 0x00, sizeof(bt_char_browse_info_t));
3401 if (event_data->conn_status.status == OAL_STATUS_SUCCESS) {
3402 /* Find service info list from address */
3403 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3404 if (svc_info_list == NULL) {
3405 BT_ERR("svc_info_list is NULL");
3410 /* Find matching service info from svc info list */
3411 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3412 if (svc_info == NULL) {
3413 BT_ERR("svc_info is NULL");
3417 /* Find Matching char from service info in event */
3418 char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
3420 /* If not found, then add new characteristic and return */
3422 BT_DBG("Add new characteristic");
3423 char_info = g_malloc0(sizeof(bt_gatt_char_info_t));
3424 memcpy(char_info->uuid, event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3426 char_info->inst_id = event_data->char_id.inst_id;
3427 char_info->props = event_data->char_prop;
3428 svc_info->chars = g_slist_append(svc_info->chars, char_info);
3430 /* If found, then return */
3431 BT_DBG("update char property as Characteristic browsed is already present");
3432 char_info->props |= event_data->char_prop;
3435 /* If Not success: Means Charc browse is completed */
3436 /* Find char list from service in event */
3437 /* Find service list from address */
3438 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3439 if (svc_info_list == NULL) {
3440 BT_ERR("svc_info_list is NULL");
3444 /* Find service info from service in event */
3445 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3446 if (svc_info == NULL) {
3447 BT_ERR("svc_info is NULL");
3451 /* Build char list from service in event */
3452 __bt_build_char_browse_info(event_data->conn_status.conn_id,
3453 svc_info, &browse_info);
3455 /* Create response and return by sending event*/
3456 /* Build Reply and send to service browse All Included services request of pending apps */
3457 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE,
3458 BT_GATT_GET_SERVICE_PROPERTIES,
3460 sizeof(bt_char_browse_info_t));
3464 static void __bt_handle_client_descriptor_search_result(
3465 event_gattc_descriptor_result_t *event_data)
3467 bt_gatt_service_info_list_t *svc_info_list;
3468 bt_gatt_service_info_t *svc_info;
3469 bt_gatt_char_info_t *char_info;
3470 bt_gatt_descriptor_info_t *desc_info;
3471 bt_descriptor_browse_info_t browse_info;
3473 BT_INFO("descriptor search result status [%d]", event_data->conn_status.status);
3475 memset(&browse_info, 0x00, sizeof(bt_descriptor_browse_info_t));
3478 if (event_data->conn_status.status == OAL_STATUS_SUCCESS) {
3479 /* Find service list from address */
3480 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3481 if (svc_info_list == NULL) {
3482 BT_ERR("svc_info_list is NULL");
3486 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3487 if (svc_info == NULL) {
3488 BT_ERR("svc_info is NULL");
3492 char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
3493 if (char_info == NULL) {
3494 BT_ERR("char_info is NULL");
3498 desc_info = __bt_find_matching_desc(char_info, &event_data->descr_id);
3499 /* If not found, add new descriptor and return */
3501 desc_info = g_malloc0(sizeof(bt_gatt_descriptor_info_t));
3502 memcpy(desc_info->uuid, event_data->descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3504 desc_info->inst_id = event_data->descr_id.inst_id;
3505 char_info->descs = g_slist_append(char_info->descs, desc_info);
3508 /* If found, then return */
3509 BT_INFO("Characteristic browsed is already presesnt");
3512 /* If Not success */
3513 /* Find service list from address */
3514 /* Find included service list from service in event */
3515 /* Create response and return by sending event*/
3516 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3517 if (svc_info_list == NULL) {
3518 BT_ERR("svc_info_list is NULL");
3522 /* Find service info from service in event */
3523 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3524 if (svc_info == NULL) {
3525 BT_ERR("svc_info is NULL");
3529 /* Find char info from char in event */
3530 char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
3531 if (char_info == NULL) {
3532 BT_ERR("char_info is NULL");
3536 /* Build descriptor list from char in event */
3537 __bt_build_descriptor_browse_info(event_data->conn_status.conn_id,
3538 svc_info, char_info, &browse_info);
3541 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE,
3542 BT_GATT_GET_CHARACTERISTIC_PROPERTIES,
3544 sizeof(bt_descriptor_browse_info_t));
3548 static void __bt_handle_client_characteristic_read_data(
3549 event_gattc_read_data *event_data)
3551 int result = BLUETOOTH_ERROR_NONE;
3552 struct gatt_server_info_t *conn_info = NULL;
3553 bluetooth_gatt_client_char_prop_info_t read_info;
3555 /* Read Information data structures */
3556 GVariant *param = NULL;
3557 GVariant *data = NULL;
3558 GVariant *data_svc_uuid = NULL;
3559 GVariant *data_char_uuid = NULL;
3560 char *read_val = NULL;
3561 char *svc_uuid = NULL;
3562 char *char_uuid = NULL;
3565 //memset(&read_info, 0x00, sizeof(bt_gatt_handle_property_t));
3566 memset(&read_info, 0x00, sizeof(bluetooth_gatt_client_char_prop_info_t));
3568 /* Extract Address from conn_id of event data */
3569 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3570 event_data->uuid_status.conn_status.conn_id);
3572 BT_INFO("Characteristic Read result from addr [%s] status [%d]",
3573 conn_info->addr, event_data->uuid_status.conn_status.status);
3575 /* Fill char in buffer */
3576 memcpy(&read_info.characteristic.uuid,
3577 event_data->uuid_status.char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3578 read_info.characteristic.instance_id = event_data->uuid_status.char_id.inst_id;
3580 /* Fill Service in buffer */
3581 memcpy(&read_info.svc.uuid,
3582 event_data->uuid_status.srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3583 read_info.svc.instance_id = event_data->uuid_status.srvc_id.id.inst_id;
3585 /* Fill remote device address */
3586 _bt_convert_addr_string_to_type(read_info.device_address.addr, conn_info->addr);
3588 /* Fill data and reply to all apps waiting for Read result on the same characteristic
3589 Note: Even in case of failure, address, handles and result code should be returned */
3590 if (event_data->uuid_status.conn_status.status != OAL_STATUS_SUCCESS)
3591 result = BLUETOOTH_ERROR_INTERNAL;
3593 if (event_data->data_len > 0) {
3595 // for (i = 0; i < event_data->data_len; i++)
3596 // BT_INFO("Data[%d] = [0x%x]", i, event_data->data[i]);
3599 read_val = g_memdup(&event_data->data[0], event_data->data_len);
3601 data = g_variant_new_from_data(
3602 G_VARIANT_TYPE_BYTESTRING,
3604 event_data->data_len,
3607 BT_ERR("Characteristic Read success, but no data!!!");
3609 data = g_variant_new_from_data(
3610 G_VARIANT_TYPE_BYTESTRING,
3617 svc_uuid = g_memdup(&event_data->uuid_status.srvc_id.id.uuid.uuid[0], uuid_len);
3619 data_svc_uuid = g_variant_new_from_data(
3620 G_VARIANT_TYPE_BYTESTRING,
3626 char_uuid = g_memdup(&event_data->uuid_status.char_id.uuid.uuid[0], uuid_len);
3628 data_char_uuid = g_variant_new_from_data(
3629 G_VARIANT_TYPE_BYTESTRING,
3634 param = g_variant_new("(isn@ayin@ayin@ay)", result,
3638 event_data->uuid_status.srvc_id.id.inst_id,
3641 event_data->uuid_status.char_id.inst_id,
3642 event_data->data_len,
3646 _bt_send_event(BT_GATT_CLIENT_EVENT,
3647 BLUETOOTH_EVENT_GATT_READ_CHAR,
3650 /* Send DBUS return */
3651 __bt_gatt_handle_pending_request_info(result,
3652 BT_GATT_READ_CHARACTERISTIC,
3654 sizeof(bluetooth_gatt_client_char_prop_info_t));
3665 static void __bt_handle_client_descriptor_read_data(
3666 event_gattc_read_data *event_data)
3668 int result = BLUETOOTH_ERROR_NONE;
3669 struct gatt_server_info_t *conn_info = NULL;
3670 bluetooth_gatt_client_desc_prop_info_t read_info;
3672 /* Read Information data structures */
3673 GVariant *param = NULL;
3674 GVariant *data = NULL;
3675 GVariant *data_svc_uuid = NULL;
3676 GVariant *data_char_uuid = NULL;
3677 GVariant *data_desc_uuid = NULL;
3678 char *read_val = NULL;
3679 char *svc_uuid = NULL;
3680 char *char_uuid = NULL;
3681 char *desc_uuid = NULL;
3686 memset(&read_info, 0x00, sizeof(bluetooth_gatt_client_desc_prop_info_t));
3688 /* Extract Address from conn_id of event data */
3689 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3690 event_data->uuid_status.conn_status.conn_id);
3692 BT_INFO("Descriptor Read result from addr [%s] status [%d]",
3693 conn_info->addr, event_data->uuid_status.conn_status.status);
3695 /* Fill descriptor informations in buffer */
3696 memcpy(&read_info.descriptor.uuid,
3697 event_data->uuid_status.descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3698 read_info.descriptor.instance_id = event_data->uuid_status.descr_id.inst_id;
3700 /* Fill Characteristic informations in buffer */
3701 memcpy(&read_info.characteristic.uuid,
3702 event_data->uuid_status.char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3703 read_info.characteristic.instance_id = event_data->uuid_status.char_id.inst_id;
3705 /* Fill Service informations in buffer */
3706 memcpy(&read_info.svc.uuid,
3707 event_data->uuid_status.srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3708 read_info.svc.instance_id = event_data->uuid_status.srvc_id.id.inst_id;
3710 /* Fill remote device address */
3711 _bt_convert_addr_string_to_type(read_info.device_address.addr, conn_info->addr);
3713 /* Fill data and reply to all apps waiting for Read result on the same characteristic */
3714 if (event_data->uuid_status.conn_status.status != OAL_STATUS_SUCCESS)
3715 result = BLUETOOTH_ERROR_INTERNAL;
3717 BT_INFO("desc data len:%d", event_data->data_len);
3718 if (event_data->data_len > 0) {
3720 for (i = 0; i < event_data->data_len; i++)
3721 BT_DBG("Data[%d] = [0x%x]", i, event_data->data[i]);
3724 read_val = g_memdup(&event_data->data[0], event_data->data_len);
3726 data = g_variant_new_from_data(
3727 G_VARIANT_TYPE_BYTESTRING,
3729 event_data->data_len,
3732 BT_INFO("Descriptor Read success, but no data!!!");
3734 data = g_variant_new_from_data(
3735 G_VARIANT_TYPE_BYTESTRING,
3741 svc_uuid = g_memdup(&event_data->uuid_status.srvc_id.id.uuid.uuid[0], uuid_len);
3743 data_svc_uuid = g_variant_new_from_data(
3744 G_VARIANT_TYPE_BYTESTRING,
3750 char_uuid = g_memdup(&event_data->uuid_status.char_id.uuid.uuid[0], uuid_len);
3752 data_char_uuid = g_variant_new_from_data(
3753 G_VARIANT_TYPE_BYTESTRING,
3759 desc_uuid = g_memdup(&event_data->uuid_status.descr_id.uuid.uuid[0], uuid_len);
3761 data_desc_uuid = g_variant_new_from_data(
3762 G_VARIANT_TYPE_BYTESTRING,
3767 param = g_variant_new("(isn@ayin@ayin@ayin@ay)", result,
3771 event_data->uuid_status.srvc_id.id.inst_id,
3774 event_data->uuid_status.char_id.inst_id,
3777 event_data->uuid_status.descr_id.inst_id,
3778 event_data->data_len,
3782 _bt_send_event(BT_GATT_CLIENT_EVENT,
3783 BLUETOOTH_EVENT_GATT_READ_DESC,
3786 BT_INFO("Send DBUS rpely for GATT Read Descriptor");
3787 /* Send DBUS return */
3788 __bt_gatt_handle_pending_request_info(result,
3789 BT_GATT_READ_DESCRIPTOR_VALUE,
3791 sizeof(bluetooth_gatt_client_desc_prop_info_t));
3802 static void __bt_handle_client_characteristic_write_data(
3803 event_gattc_write_data *event_data)
3805 int result = BLUETOOTH_ERROR_NONE;
3806 struct gatt_server_info_t *conn_info = NULL;
3807 bluetooth_gatt_client_char_prop_info_t write_info;
3809 /* Read Information data structures */
3810 GVariant *param = NULL;
3811 GVariant *data_svc_uuid = NULL;
3812 GVariant *data_char_uuid = NULL;
3813 char *svc_uuid = NULL;
3814 char *char_uuid = NULL;
3818 memset(&write_info, 0x00, sizeof(bluetooth_gatt_client_char_prop_info_t));
3820 /* Extract Address from conn_id of event data */
3821 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3822 event_data->conn_status.conn_id);
3824 BT_INFO("Characteristic Write callback from addr [%s] status [%d]",
3825 conn_info->addr, event_data->conn_status.status);
3827 /* Fill char in buffer */
3828 memcpy(&write_info.characteristic.uuid,
3829 event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3830 write_info.characteristic.instance_id = event_data->char_id.inst_id;
3832 /* Fill Service in buffer */
3833 memcpy(&write_info.svc.uuid,
3834 event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3835 write_info.svc.instance_id = event_data->srvc_id.id.inst_id;
3837 /* Fill remote device address */
3838 _bt_convert_addr_string_to_type(write_info.device_address.addr, conn_info->addr);
3840 if (event_data->conn_status.status != OAL_STATUS_SUCCESS) {
3841 result = BLUETOOTH_ERROR_INTERNAL;
3847 svc_uuid = g_memdup(&event_data->srvc_id.id.uuid.uuid[0], uuid_len);
3849 data_svc_uuid = g_variant_new_from_data(
3850 G_VARIANT_TYPE_BYTESTRING,
3856 char_uuid = g_memdup(&event_data->char_id.uuid.uuid[0], uuid_len);
3858 data_char_uuid = g_variant_new_from_data(
3859 G_VARIANT_TYPE_BYTESTRING,
3864 param = g_variant_new("(isn@ayin@ayi)", result,
3868 event_data->srvc_id.id.inst_id,
3871 event_data->char_id.inst_id);
3874 _bt_send_event(BT_GATT_CLIENT_EVENT,
3875 BLUETOOTH_EVENT_GATT_WRITE_CHAR,
3884 /* Send DBUS return */
3885 __bt_gatt_handle_pending_request_info(result,
3886 BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE,
3888 sizeof(bluetooth_gatt_client_char_prop_info_t));
3892 static void __bt_handle_client_descriptor_write_data(
3893 event_gattc_write_data *event_data)
3895 int result = BLUETOOTH_ERROR_NONE;
3896 struct gatt_server_info_t *conn_info = NULL;
3897 bluetooth_gatt_client_desc_prop_info_t write_info;
3899 /* Write Information data structures */
3900 GVariant *param = NULL;
3901 GVariant *data_svc_uuid = NULL;
3902 GVariant *data_char_uuid = NULL;
3903 GVariant *data_desc_uuid = NULL;
3904 char *svc_uuid = NULL;
3905 char *char_uuid = NULL;
3906 char *desc_uuid = NULL;
3910 memset(&write_info, 0x00, sizeof(bluetooth_gatt_client_desc_prop_info_t));
3912 /* Extract Address from conn_id of event data */
3913 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3914 event_data->conn_status.conn_id);
3916 if (NULL == conn_info) {
3918 BT_INFO("Failed to get the conn info for conn_id [%d]", event_data->conn_status.conn_id);
3922 BT_INFO("Descriptor Write callback from addr [%s] status [%d]",
3923 conn_info->addr, event_data->conn_status.status);
3925 /* Fill descriptor informations in buffer */
3926 memcpy(&write_info.descriptor.uuid,
3927 event_data->descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3928 write_info.descriptor.instance_id = event_data->descr_id.inst_id;
3930 /* Fill Characteristic informations in buffer */
3931 memcpy(&write_info.characteristic.uuid,
3932 event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3933 write_info.characteristic.instance_id = event_data->char_id.inst_id;
3935 /* Fill Service informations in buffer */
3936 memcpy(&write_info.svc.uuid,
3937 event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3938 write_info.svc.instance_id = event_data->srvc_id.id.inst_id;
3940 /* Fill remote device address */
3941 _bt_convert_addr_string_to_type(write_info.device_address.addr, conn_info->addr);
3943 if (event_data->conn_status.status != OAL_STATUS_SUCCESS) {
3944 result = BLUETOOTH_ERROR_INTERNAL;
3950 svc_uuid = g_memdup(&event_data->srvc_id.id.uuid.uuid[0], uuid_len);
3952 data_svc_uuid = g_variant_new_from_data(
3953 G_VARIANT_TYPE_BYTESTRING,
3959 char_uuid = g_memdup(&event_data->char_id.uuid.uuid[0], uuid_len);
3961 data_char_uuid = g_variant_new_from_data(
3962 G_VARIANT_TYPE_BYTESTRING,
3968 desc_uuid = g_memdup(&event_data->descr_id.uuid.uuid[0], uuid_len);
3970 data_desc_uuid = g_variant_new_from_data(
3971 G_VARIANT_TYPE_BYTESTRING,
3976 param = g_variant_new("(isn@ayin@ayin@ayi)", result,
3980 event_data->srvc_id.id.inst_id,
3983 event_data->char_id.inst_id,
3986 event_data->descr_id.inst_id);
3989 _bt_send_event(BT_GATT_CLIENT_EVENT,
3990 BLUETOOTH_EVENT_GATT_WRITE_DESC,
4001 /* Send DBUS return */
4002 __bt_gatt_handle_pending_request_info(result,
4003 BT_GATT_WRITE_DESCRIPTOR_VALUE,
4005 sizeof(bluetooth_gatt_client_desc_prop_info_t));
4008 static void __bt_hanlde_le_device_disconnection(event_dev_conn_status_t *event_data)
4010 int result = BLUETOOTH_ERROR_INTERNAL;
4011 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
4013 _bt_convert_addr_type_to_string(address, (unsigned char *)event_data->address.addr);
4015 /* DBUS Return with fail of pending BT_CONNECT_LE for all the apps */
4016 BT_INFO("Local GATT Client disconnected: Remote addr[%s] ", address);
4018 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE, address,
4019 BT_ADDRESS_STRING_SIZE);
4023 static void __bt_handle_client_notification_registered(
4024 event_gattc_regdereg_notify_t *event_data,
4025 gboolean is_registered)
4027 int result = BLUETOOTH_ERROR_NONE;
4028 struct gatt_server_info_t *conn_info = NULL;
4029 bt_gatt_notif_reg_info_t notif_info;
4032 memset(¬if_info, 0x00, sizeof(bt_gatt_notif_reg_info_t));
4034 BT_INFO("Client Interface [%d] status [%d]",
4035 event_data->conn_id,
4036 event_data->status);
4038 /* Extract Address from conn_id of event data */
4039 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(event_data->conn_id);
4041 BT_INFO("Connection Info is not present, return");
4044 BT_INFO("Notification Registered for addr [%s]", conn_info->addr);
4046 /* Fill svc informations in buffer */
4047 memcpy(¬if_info.svc_uuid,
4048 event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4049 notif_info.svc_inst = event_data->srvc_id.id.inst_id;
4051 /* Fill char in buffer */
4052 memcpy(¬if_info.char_uuid,
4053 event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4054 notif_info.char_inst = event_data->char_id.inst_id;
4056 /* Fill remote device address */
4057 _bt_convert_addr_string_to_type(notif_info.addr.addr, conn_info->addr);
4059 notif_info.is_registered = is_registered;
4061 if (event_data->status != OAL_STATUS_SUCCESS)
4062 result = BLUETOOTH_ERROR_INTERNAL;
4064 /* Send DBUS Return for BT_GATT_WATCH_CHARACTERISTIC */
4065 __bt_gatt_handle_pending_request_info(result,
4066 BT_GATT_WATCH_CHARACTERISTIC,
4068 sizeof(bt_gatt_notif_reg_info_t));
4071 static void __bt_handle_client_notification_data(event_gattc_notify_data *event_data)
4073 /* No status in this event from OAL */
4074 int result = BLUETOOTH_ERROR_NONE;
4076 /* Read Information data structures */
4077 GVariant *param = NULL;
4078 GVariant *data = NULL;
4079 GVariant *data_svc_uuid = NULL;
4080 GVariant *data_char_uuid = NULL;
4081 char *read_val = NULL;
4082 char *svc_uuid = NULL;
4083 char *char_uuid = NULL;
4089 BT_INFO("Notifcation of charc data changed");
4091 if (event_data->data_len > 0) {
4093 for (i = 0; i < event_data->data_len; i++)
4094 BT_INFO("Data[%d] = [0x%x]", i, event_data->data[i]);
4097 addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
4098 _bt_convert_addr_type_to_string(addr,
4099 (unsigned char *)&(event_data->address.addr));
4102 read_val = g_memdup(&event_data->data[0], event_data->data_len);
4104 data = g_variant_new_from_data(
4105 G_VARIANT_TYPE_BYTESTRING,
4107 event_data->data_len,
4110 svc_uuid = g_memdup(&event_data->srvc_id.id.uuid.uuid[0], uuid_len);
4112 data_svc_uuid = g_variant_new_from_data(
4113 G_VARIANT_TYPE_BYTESTRING,
4119 char_uuid = g_memdup(&event_data->char_id.uuid.uuid[0], uuid_len);
4121 data_char_uuid = g_variant_new_from_data(
4122 G_VARIANT_TYPE_BYTESTRING,
4128 param = g_variant_new("(isn@ayin@ayin@ay)", result,
4132 event_data->srvc_id.id.inst_id,
4135 event_data->char_id.inst_id,
4136 event_data->data_len,
4140 _bt_send_event(BT_GATT_CLIENT_EVENT,
4141 BLUETOOTH_EVENT_GATT_CHAR_VAL_CHANGED,
4144 BT_ERR("No Data!!");
4157 static void __bt_handle_client_service_changed_ind(event_gattc_service_changed_data *event_data)
4159 bt_gatt_service_info_list_t *svc_info_list;
4161 bt_gatt_service_info_t *svc_info;
4162 GVariant *param = NULL;
4163 char *address_str = NULL;
4164 char *uuid_str = NULL;
4166 svc_info_list = __bt_get_service_info_list(event_data->conn_id);
4167 if (svc_info_list == NULL) {
4168 BT_ERR("svc_info_list is NULL");
4172 if (event_data->change_type) {
4173 /* Add service UUID in list */
4174 svc_info = g_malloc0(sizeof(bt_gatt_service_info_t));
4175 memcpy(svc_info->uuid, event_data->uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4176 svc_info->inst_id = event_data->inst_id;
4177 svc_info->is_primary = 1; // TODO: Need to check is_primary is required or not
4178 svc_info_list->services = g_slist_append(svc_info_list->services, svc_info);
4179 BT_DBG("Service created and added in Svc info list or connection ID %d", event_data->conn_id);
4181 /* Remove service UUID in list */
4182 for (l = svc_info_list->services; l != NULL; l = g_slist_next(l)) {
4183 svc_info = (bt_gatt_service_info_t *)l->data;
4184 if (svc_info == NULL)
4187 if (!memcmp(svc_info->uuid, event_data->uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)) {
4188 svc_info_list->services = g_slist_remove(svc_info_list->services, svc_info);
4189 __bt_free_service_info(svc_info);
4195 address_str = g_malloc0(BT_ADDRESS_STRING_SIZE);
4196 uuid_str = g_malloc0(BT_UUID_STRING_MAX);
4197 _bt_convert_addr_type_to_string(address_str, event_data->address.addr);
4198 _bt_uuid_to_string(&event_data->uuid, uuid_str);
4200 param = g_variant_new("(iiss)", event_data->inst_id, event_data->change_type, address_str, uuid_str);
4202 _bt_send_event(BT_GATT_CLIENT_EVENT,
4203 BLUETOOTH_EVENT_GATT_CLIENT_SERVICE_CHANGED,
4205 g_free(address_str);
4209 gboolean _bt_is_remote_gatt_device_connected(bluetooth_device_address_t *address)
4212 struct gatt_server_info_t *conn_info = NULL;
4213 gboolean connected = FALSE;
4215 addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
4216 _bt_convert_addr_type_to_string(addr,
4217 (unsigned char *)&(address->addr));
4219 BT_DBG("Check GATT connection status of [%s]", addr);
4220 /* Check if device is already in connected list */
4221 conn_info = _bt_find_remote_gatt_server_info(addr);
4224 BT_DBG("Remote GATT Server device [%s] is Connected", conn_info->addr);
4227 struct gatt_client_info_t *client_info = NULL;
4229 BT_INFO("Remote GATT Server Device [%s] is not Connected", addr);
4231 /* Check if device is already in connected list */
4232 client_info = _bt_find_remote_gatt_client_info(addr);
4235 BT_DBG("Remote Client device [%s] is Connected", client_info->addr);
4238 BT_INFO("Remote GATT Client Device [%s] is not Connected", addr);
4246 void _bt_handle_invocation_context(int function_name, void *data)
4248 switch (function_name) {
4250 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE, BT_CONNECT_LE,
4251 (char *)data, BT_ADDRESS_STRING_SIZE);
4258 int _bt_connect_le_device(bluetooth_device_address_t *address,
4259 int auto_connect, int client_id)
4261 struct gatt_server_info_t *conn_info = NULL;
4262 struct gatt_out_conn_info_t *out_conn_info = NULL;
4264 invocation_info_t *req_info = NULL;
4265 int ret = OAL_STATUS_SUCCESS;
4267 char *remote_address = NULL;
4269 BT_CHECK_PARAMETER(address, return);
4271 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4272 _bt_convert_addr_type_to_string(addr, address->addr);
4273 BT_DBG("GATT Client connect request for address [%s] client instance [%d]",
4277 /* Check if Remote Device is already under connection progress */
4278 req_info = _bt_get_request_info_data_from_function_name(BT_CONNECT_LE);
4280 remote_address = (char*)req_info->user_data;
4281 if (remote_address && !strcasecmp(remote_address, addr)) {/* Address matched */
4282 BT_DBG("Already Connection ongoing for same remote GATT Server address [%s]", remote_address);
4283 /* Return and wait for events to be sent to all apps */
4285 return BLUETOOTH_ERROR_IN_PROGRESS;
4289 /* Check if remote GATT Server is connected or not */
4290 conn_info = _bt_find_remote_gatt_server_info(addr);
4292 BT_ERR("GATT Server is already connected..");
4294 return BLUETOOTH_ERROR_ALREADY_CONNECT;
4297 /* TODO Check Requirement of holding Advertisement before initiating LE connect */
4299 /* Check if app sent 0 client id for connection, in such case, use default gatt client ID */
4300 if (client_id == 0) {
4301 /* GATT CLient connect request sent by an app without any client instance [0] */
4302 BT_DBG("Assign default GATT client id [%d]", gatt_default_client);
4303 client_id = gatt_default_client;
4306 BT_INFO("Connect using CLient ID [%d]", client_id);
4307 ret = gattc_connect(client_id, (bt_address_t*)(address), auto_connect);
4309 if (ret != OAL_STATUS_SUCCESS) {
4310 BT_ERR("gattc_connect is failed. ret: %d", ret);
4312 return BLUETOOTH_ERROR_INTERNAL;
4315 /* Mark this as outgoing connection */
4316 out_conn_info = g_new0(struct gatt_out_conn_info_t, 1);
4317 out_conn_info->addr = g_strdup(addr);
4318 out_conn_info->client_id = client_id;
4319 BT_INFO("Added outgoing connection info addr[%s]", out_conn_info->addr);
4320 outgoing_gatt_conn_list = g_slist_append(outgoing_gatt_conn_list, out_conn_info);
4323 return BLUETOOTH_ERROR_NONE;
4326 int _bt_gatt_get_primary_services(char *address)
4328 BT_CHECK_PARAMETER(address, return);
4329 struct gatt_server_info_t *conn_info = NULL;
4330 invocation_info_t *req_info = NULL;
4331 int ret = OAL_STATUS_SUCCESS;
4334 /* Check if any app is already browsing primary services on the same remote GATT Server */
4335 req_info = _bt_get_request_info_data(BT_GATT_GET_PRIMARY_SERVICES, address);
4337 BT_INFO("Already Primary Service Browsing ongoing for same rmeote GATT Server");
4338 /* Return and wait for events to be sent to all apps */
4339 return BLUETOOTH_ERROR_NONE;
4342 /* Check if remote GATT Server is connected or not */
4343 conn_info = _bt_find_remote_gatt_server_info(address);
4345 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4346 conn_info->addr, conn_info->connection_id);
4348 BT_ERR("GATT Server is not yet connected..");
4349 return BLUETOOTH_ERROR_NOT_CONNECTED;
4352 /* Send Primary Service Browsing request to stack */
4353 ret = gattc_search_service(conn_info->connection_id, NULL);
4354 if (ret != OAL_STATUS_SUCCESS) {
4355 BT_ERR("ret: %d", ret);
4356 return BLUETOOTH_ERROR_INTERNAL;
4358 return BLUETOOTH_ERROR_NONE;
4361 int _bt_gatt_get_all_characteristic(bluetooth_gatt_client_svc_prop_info_t *svc)
4363 BT_CHECK_PARAMETER(svc, return);
4364 struct gatt_server_info_t *conn_info = NULL;
4365 invocation_info_t *req_info = NULL;
4366 bluetooth_gatt_client_svc_prop_info_t *prop;
4367 oal_gatt_srvc_id_t srvc_id;
4368 int ret = OAL_STATUS_SUCCESS;
4371 /* Check if any app is already browsing characteristics of the same service on the same remote GATT Server */
4372 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_GET_SERVICE_PROPERTIES);
4374 prop = (bluetooth_gatt_client_svc_prop_info_t*)req_info->user_data;
4375 if (prop && !memcmp(svc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t))
4376 && !memcmp(prop->svc.uuid, svc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
4377 && prop->svc.instance_id == svc->svc.instance_id) {
4378 BT_INFO("Already Properties browsing for Primary Service ongoing for same remote GATT Server");
4379 /* Return and wait for events to be sent to all apps */
4380 return BLUETOOTH_ERROR_NONE;
4384 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4385 _bt_convert_addr_type_to_string(addr, svc->device_address.addr);
4387 /* Check if remote GATT Server is connected or not */
4388 conn_info = _bt_find_remote_gatt_server_info(addr);
4390 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4391 conn_info->addr, conn_info->connection_id);
4393 BT_ERR("GATT Server is not yet connected..");
4395 return BLUETOOTH_ERROR_NOT_CONNECTED;
4398 srvc_id.is_prmry = TRUE;
4399 srvc_id.id.inst_id = svc->svc.instance_id;
4400 memcpy(srvc_id.id.uuid.uuid, svc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4402 /* Search All Characteristic */
4403 ret = gattc_get_characteristic(conn_info->connection_id, &srvc_id, NULL);
4404 if (ret != OAL_STATUS_SUCCESS) {
4405 BT_ERR("ret: %d", ret);
4407 return BLUETOOTH_ERROR_INTERNAL;
4410 return BLUETOOTH_ERROR_NONE;
4413 int _bt_gatt_get_all_characteristic_properties(
4414 bluetooth_gatt_client_char_prop_info_t *chr)
4416 struct gatt_server_info_t *conn_info = NULL;
4417 invocation_info_t *req_info = NULL;
4418 bluetooth_gatt_client_char_prop_info_t *prop;
4419 oal_gatt_srvc_id_t srvc_id;
4420 oal_gatt_id_t char_id;
4421 int ret = OAL_STATUS_SUCCESS;
4424 BT_CHECK_PARAMETER(chr, return);
4426 /* Check if any app is already browsing descriptors of the same char of
4427 particular service on the same remote GATT Server */
4428 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_GET_CHARACTERISTIC_PROPERTIES);
4430 prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
4431 if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4432 && !memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4433 && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4434 && !memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4435 && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
4436 BT_INFO("Already Properties browsing for Characteristic ongoing for same remote GATT Server");
4437 /* Return and wait for events to be sent to all apps */
4438 return BLUETOOTH_ERROR_NONE;
4442 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4443 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4445 /* Check if remote GATT Server is connected or not */
4446 conn_info = _bt_find_remote_gatt_server_info(addr);
4448 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4449 conn_info->addr, conn_info->connection_id);
4451 BT_ERR("GATT Server is not yet connected..");
4453 return BLUETOOTH_ERROR_NOT_CONNECTED;
4456 srvc_id.is_prmry = TRUE;
4457 srvc_id.id.inst_id = chr->svc.instance_id;
4458 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4460 char_id.inst_id = chr->characteristic.instance_id;
4461 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4463 /* Search All Descriptors */
4464 ret = gattc_get_descriptor(conn_info->connection_id, &srvc_id, &char_id, NULL);
4465 if (ret != OAL_STATUS_SUCCESS) {
4466 BT_ERR("ret: %d", ret);
4468 return BLUETOOTH_ERROR_INTERNAL;
4471 return BLUETOOTH_ERROR_NONE;
4474 int _bt_gatt_read_characteristic_value(
4475 bluetooth_gatt_client_char_prop_info_t *chr)
4477 struct gatt_server_info_t *conn_info = NULL;
4478 invocation_info_t *req_info = NULL;
4479 bluetooth_gatt_client_char_prop_info_t *prop;
4480 oal_gatt_srvc_id_t srvc_id;
4481 oal_gatt_id_t char_id;
4482 int ret = OAL_STATUS_SUCCESS;
4485 BT_CHECK_PARAMETER(chr, return);
4487 /* Check if any app is already Reading characteristic of the same char of
4488 particular service on the same remote GATT Server */
4489 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_READ_CHARACTERISTIC);
4491 prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
4492 if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4493 && memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4494 && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4495 && memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4496 && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
4497 BT_INFO("Already Characteristic value Read operation in progress for same remote GATT Server");
4498 /* Return and wait for events to be sent to all apps */
4499 return BLUETOOTH_ERROR_NONE;
4503 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4504 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4506 /* Check if remote GATT Server is connected or not */
4507 conn_info = _bt_find_remote_gatt_server_info(addr);
4509 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4510 conn_info->addr, conn_info->connection_id);
4512 BT_ERR("GATT Server is not yet connected..");
4514 return BLUETOOTH_ERROR_NOT_CONNECTED;
4517 srvc_id.is_prmry = TRUE;
4518 srvc_id.id.inst_id = chr->svc.instance_id;
4519 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4521 char_id.inst_id = chr->characteristic.instance_id;
4522 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4524 /* Search All Descriptors */
4525 ret = gattc_read_characteristic(conn_info->connection_id, &srvc_id, &char_id, OAL_GATT_AUTH_REQ_NONE);
4526 if (ret != OAL_STATUS_SUCCESS) {
4527 BT_ERR("ret: %d", ret);
4529 return BLUETOOTH_ERROR_INTERNAL;
4532 return BLUETOOTH_ERROR_NONE;
4535 int _bt_gatt_read_descriptor_value(
4536 bluetooth_gatt_client_desc_prop_info_t *desc)
4538 struct gatt_server_info_t *conn_info = NULL;
4539 invocation_info_t *req_info = NULL;
4540 bluetooth_gatt_client_desc_prop_info_t *prop;
4541 oal_gatt_srvc_id_t srvc_id;
4542 oal_gatt_id_t char_id;
4543 oal_gatt_id_t desc_id;
4544 int ret = OAL_STATUS_SUCCESS;
4547 BT_CHECK_PARAMETER(desc, return);
4549 /* Check if any app is already Reading descriptors of the same char of
4550 particular service on the same remote GATT Server */
4551 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_READ_DESCRIPTOR_VALUE);
4553 prop = (bluetooth_gatt_client_desc_prop_info_t*)req_info->user_data;
4554 if (prop && !memcmp(desc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4555 && memcmp(desc->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4556 && desc->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4557 && memcmp(desc->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4558 && desc->characteristic.instance_id == prop->characteristic.instance_id /* Characteristic Instance ID matched */
4559 && memcmp(desc->descriptor.uuid, prop->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Descriptor UUID matched */
4560 && desc->descriptor.instance_id == prop->descriptor.instance_id) { /* Descriptor Instance ID matched */
4561 BT_INFO("Already Descriptor value Read operation in progress for same remote GATT Server");
4562 /* Return and wait for events to be sent to all apps */
4563 return BLUETOOTH_ERROR_NONE;
4567 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4568 _bt_convert_addr_type_to_string(addr, desc->device_address.addr);
4570 /* Check if remote GATT Server is connected or not */
4571 conn_info = _bt_find_remote_gatt_server_info(addr);
4573 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4574 conn_info->addr, conn_info->connection_id);
4576 BT_ERR("GATT Server is not yet connected..");
4578 return BLUETOOTH_ERROR_NOT_CONNECTED;
4581 srvc_id.is_prmry = TRUE;
4582 srvc_id.id.inst_id = desc->svc.instance_id;
4583 memcpy(srvc_id.id.uuid.uuid, desc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4585 char_id.inst_id = desc->characteristic.instance_id;
4586 memcpy(char_id.uuid.uuid, desc->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4588 desc_id.inst_id = desc->descriptor.instance_id;
4589 memcpy(desc_id.uuid.uuid, desc->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4591 /* Search All Descriptors */
4592 ret = gattc_read_descriptor(conn_info->connection_id, &srvc_id, &char_id,
4593 &desc_id, OAL_GATT_AUTH_REQ_NONE);
4594 if (ret != OAL_STATUS_SUCCESS) {
4595 BT_ERR("ret: %d", ret);
4597 return BLUETOOTH_ERROR_INTERNAL;
4600 return BLUETOOTH_ERROR_NONE;
4604 int _bt_gatt_acquire_notify(bluetooth_gatt_client_char_prop_info_t *chr, int *fd, int *mtu)
4606 struct gatt_server_info_t *conn_info = NULL;
4607 oal_gatt_srvc_id_t srvc_id;
4608 oal_gatt_id_t char_id;
4609 int ret = OAL_STATUS_SUCCESS;
4612 BT_CHECK_PARAMETER(chr, return);
4614 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4615 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4617 /* Check if remote GATT Server is connected or not */
4618 conn_info = _bt_find_remote_gatt_server_info(addr);
4620 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4621 conn_info->addr, conn_info->connection_id);
4623 BT_ERR("GATT Server is not yet connected..");
4625 return BLUETOOTH_ERROR_NOT_CONNECTED;
4628 srvc_id.is_prmry = TRUE;
4629 srvc_id.id.inst_id = chr->svc.instance_id;
4630 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4632 char_id.inst_id = chr->characteristic.instance_id;
4633 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4635 ret = gattc_acquire_notify(conn_info->connection_id, &srvc_id, &char_id, fd, mtu);
4637 if (ret != OAL_STATUS_SUCCESS) {
4638 BT_ERR("ret: %d", ret);
4640 return BLUETOOTH_ERROR_INTERNAL;
4642 BT_INFO("GATT characterstics FD [%d] mtu[%d]", *fd, *mtu);
4644 return BLUETOOTH_ERROR_NONE;
4648 int _bt_gatt_acquire_write(bluetooth_gatt_client_char_prop_info_t *chr, int *fd, int *mtu)
4651 struct gatt_server_info_t *conn_info = NULL;
4652 oal_gatt_srvc_id_t srvc_id;
4653 oal_gatt_id_t char_id;
4654 int ret = OAL_STATUS_SUCCESS;
4657 BT_CHECK_PARAMETER(chr, return);
4659 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4660 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4662 /* Check if remote GATT Server is connected or not */
4663 conn_info = _bt_find_remote_gatt_server_info(addr);
4665 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4666 conn_info->addr, conn_info->connection_id);
4668 BT_ERR("GATT Server is not yet connected..");
4670 return BLUETOOTH_ERROR_NOT_CONNECTED;
4673 srvc_id.is_prmry = TRUE;
4674 srvc_id.id.inst_id = chr->svc.instance_id;
4675 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4677 char_id.inst_id = chr->characteristic.instance_id;
4678 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4680 ret = gattc_acquire_write(conn_info->connection_id, &srvc_id, &char_id,
4681 OAL_GATT_AUTH_REQ_NONE, fd, mtu);
4682 if (ret != OAL_STATUS_SUCCESS) {
4683 BT_ERR("ret: %d", ret);
4685 return BLUETOOTH_ERROR_INTERNAL;
4687 BT_INFO("GATT characterstics FD [%d] mtu [%d]", *fd, *mtu);
4689 return BLUETOOTH_ERROR_NONE;
4694 /* Write Characteristic */
4695 int _bt_gatt_write_characteristic_value_by_type(
4696 bluetooth_gatt_client_char_prop_info_t *chr,
4697 bluetooth_gatt_att_data_t *data,
4698 bluetooth_gatt_write_type_e write_type)
4700 struct gatt_server_info_t *conn_info = NULL;
4701 invocation_info_t *req_info = NULL;
4702 bluetooth_gatt_client_char_prop_info_t *prop;
4703 oal_gatt_srvc_id_t srvc_id;
4704 oal_gatt_id_t char_id;
4705 int ret = OAL_STATUS_SUCCESS;
4709 BT_CHECK_PARAMETER(chr, return);
4710 BT_CHECK_PARAMETER(data, return);
4714 /* Check if any app is already writing same char of
4715 particular service on the same remote GATT Server */
4716 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE);
4718 prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
4719 if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4720 && memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4721 && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4722 && memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4723 && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
4724 BT_INFO("Already Characteristic Write Value operation in progress for same remote GATT Server");
4725 /* Return and wait for events to be sent to all apps */
4726 return BLUETOOTH_ERROR_NONE;
4730 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4731 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4733 /* Check if remote GATT Server is connected or not */
4734 conn_info = _bt_find_remote_gatt_server_info(addr);
4736 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4737 conn_info->addr, conn_info->connection_id);
4739 BT_ERR("GATT Server is not yet connected..");
4741 return BLUETOOTH_ERROR_NOT_CONNECTED;
4744 srvc_id.is_prmry = TRUE;
4745 srvc_id.id.inst_id = chr->svc.instance_id;
4746 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4748 char_id.inst_id = chr->characteristic.instance_id;
4749 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4751 /* Write CHar value */
4753 BT_INFO("Connection ID [%d] write type [%d] data length [%d]", conn_info->connection_id, write_type, data->length);
4754 for (k = 0; k < data->length; k++)
4755 BT_INFO("Data[%d] [0x%x]", k, data->data[k]);
4757 ret = gattc_write_characteristic(conn_info->connection_id,
4759 (oal_gatt_write_type_t)write_type, data->length,
4760 OAL_GATT_AUTH_REQ_NONE, (char *)(&data->data[0]));
4761 if (ret != OAL_STATUS_SUCCESS) {
4762 BT_ERR("ret: %d", ret);
4764 return BLUETOOTH_ERROR_INTERNAL;
4767 return BLUETOOTH_ERROR_NONE;
4770 /* Write Descriptor */
4771 int _bt_gatt_write_descriptor_value_by_type(
4772 bluetooth_gatt_client_desc_prop_info_t *desc,
4773 bluetooth_gatt_att_data_t *data,
4774 bluetooth_gatt_write_type_e write_type)
4776 struct gatt_server_info_t *conn_info = NULL;
4777 invocation_info_t *req_info = NULL;
4778 bluetooth_gatt_client_desc_prop_info_t *prop;
4779 oal_gatt_srvc_id_t srvc_id;
4780 oal_gatt_id_t char_id;
4781 oal_gatt_id_t desc_id;
4782 int ret = OAL_STATUS_SUCCESS;
4786 BT_CHECK_PARAMETER(desc, return);
4787 BT_CHECK_PARAMETER(data, return);
4791 /* Check if any app is already writing on same Descriptor of the same char of
4792 particular service on the same remote GATT Server */
4793 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_WRITE_DESCRIPTOR_VALUE);
4795 prop = (bluetooth_gatt_client_desc_prop_info_t*)req_info->user_data;
4796 if (prop && !memcmp(desc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4797 && memcmp(desc->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4798 && desc->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4799 && memcmp(desc->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4800 && desc->characteristic.instance_id == prop->characteristic.instance_id /* Characteristic Instance ID matched */
4801 && memcmp(desc->descriptor.uuid, prop->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Descriptor UUID matched */
4802 && desc->descriptor.instance_id == prop->descriptor.instance_id) { /* Descriptor Instance ID matched */
4803 BT_INFO("Already Descriptor value Write operation in progress for same remote GATT Server");
4804 /* Return and wait for events to be sent to all apps */
4805 return BLUETOOTH_ERROR_NONE;
4809 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4810 _bt_convert_addr_type_to_string(addr, desc->device_address.addr);
4812 /* Check if remote GATT Server is connected or not */
4813 conn_info = _bt_find_remote_gatt_server_info(addr);
4815 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4816 conn_info->addr, conn_info->connection_id);
4818 BT_ERR("GATT Server is not yet connected..");
4820 return BLUETOOTH_ERROR_NOT_CONNECTED;
4823 srvc_id.is_prmry = TRUE;
4824 srvc_id.id.inst_id = desc->svc.instance_id;
4825 memcpy(srvc_id.id.uuid.uuid, desc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4827 char_id.inst_id = desc->characteristic.instance_id;
4828 memcpy(char_id.uuid.uuid, desc->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4830 desc_id.inst_id = desc->descriptor.instance_id;
4831 memcpy(desc_id.uuid.uuid, desc->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4834 BT_INFO("Connection ID [%d] write type [%d] data length [%d]", conn_info->connection_id, write_type, data->length);
4835 for (k = 0; k < data->length; k++)
4836 BT_INFO("Data[%d] [0x%x]", k, data->data[k]);
4838 ret = gattc_write_descriptor(conn_info->connection_id,
4839 &srvc_id, &char_id, &desc_id,
4840 (oal_gatt_write_type_t)write_type, data->length,
4841 OAL_GATT_AUTH_REQ_NONE, (char *)(&data->data[0]));
4842 if (ret != OAL_STATUS_SUCCESS) {
4843 BT_ERR("ret: %d", ret);
4845 return BLUETOOTH_ERROR_INTERNAL;
4848 return BLUETOOTH_ERROR_NONE;
4851 int _bt_gatt_watch_characteristic(
4852 bluetooth_gatt_client_char_prop_info_t *chr,
4856 struct gatt_server_info_t *conn_info = NULL;
4857 oal_gatt_srvc_id_t srvc_id;
4858 oal_gatt_id_t char_id;
4859 int ret = OAL_STATUS_SUCCESS;
4862 BT_CHECK_PARAMETER(chr, return);
4864 BT_INFO("Client ID [%d] Is Notify [%d]", client_id, is_notify);
4866 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4867 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4869 /* Check if remote GATT Server is connected or not */
4870 conn_info = _bt_find_remote_gatt_server_info(addr);
4872 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4873 conn_info->addr, conn_info->connection_id);
4875 BT_ERR("GATT Server is not yet connected..");
4877 return BLUETOOTH_ERROR_NOT_CONNECTED;
4879 srvc_id.is_prmry = TRUE;
4880 srvc_id.id.inst_id = chr->svc.instance_id;
4881 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4883 char_id.inst_id = chr->characteristic.instance_id;
4884 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4886 /* Register or unregister Notification characteristic */
4888 ret = gattc_register_for_notification(conn_info->connection_id,
4889 (bt_address_t*)&(chr->device_address),
4890 &srvc_id, &char_id);
4892 ret = gattc_deregister_for_notification(conn_info->connection_id,
4893 (bt_address_t*)&(chr->device_address),
4894 &srvc_id, &char_id);
4896 BT_INFO("Result[%d]", ret);
4897 if (ret != OAL_STATUS_SUCCESS) {
4898 BT_ERR("ret: %d", ret);
4900 return BLUETOOTH_ERROR_INTERNAL;
4903 return BLUETOOTH_ERROR_NONE;
4907 int _bt_disconnect_le_device(bluetooth_device_address_t *address,
4910 struct gatt_server_info_t *conn_info = NULL;
4911 struct gatt_client_info_t *rem_client_conn_info = NULL;
4912 invocation_info_t *req_info = NULL;
4913 int ret = OAL_STATUS_SUCCESS;
4915 char *remote_address = NULL;
4917 BT_CHECK_PARAMETER(address, return);
4919 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4920 _bt_convert_addr_type_to_string(addr, address->addr);
4921 BT_INFO("GATT Client Disconnect request for address [%s]", addr);
4923 /* Check if Remote Device is already under connection progress */
4924 req_info = _bt_get_request_info_data_from_function_name(BT_DISCONNECT_LE);
4926 remote_address = (char*)req_info->user_data;
4927 if (remote_address && !strcasecmp(remote_address, addr)) {/* Address matched */
4928 BT_DBG("Already DisConnection ongoing for same remote GATT Server address [%s]", remote_address);
4929 /* Return success and wait for events to be sent to all apps */
4931 return BLUETOOTH_ERROR_IN_PROGRESS;
4934 /* Check if remote GATT Server is connected or not */
4935 conn_info = _bt_find_remote_gatt_server_info(addr);
4937 /* Check if app sent 0 client id for Disconnection, in such case, use default gatt client ID */
4938 if (client_id == 0) {
4939 BT_INFO("GATT CLient Disconnect request sent by an app without any client instance [%d]",
4941 BT_INFO("Assign default GATT client id [%d]", gatt_default_client);
4942 client_id = gatt_default_client;
4945 BT_INFO("Disconnect remote gatt server using CLient ID [%d] Connection ID [%d]", client_id, conn_info->connection_id);
4946 ret = gattc_disconnect(client_id, (bt_address_t*)(address),
4947 conn_info->connection_id);
4949 /* check if remote client is connected */
4950 rem_client_conn_info = _bt_find_remote_gatt_client_info(addr);
4952 if (!rem_client_conn_info || client_id != 0) {
4953 BT_ERR("GATT device is not connected..");
4955 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
4958 BT_INFO("Disconnect remote gatt client ");
4960 ret = gatts_disconnect(rem_client_conn_info->instance_id,
4961 (bt_address_t*)(address), rem_client_conn_info->connection_id);
4964 if (ret != OAL_STATUS_SUCCESS) {
4965 BT_ERR("ret: %d", ret);
4967 return BLUETOOTH_ERROR_INTERNAL;
4970 return BLUETOOTH_ERROR_NONE;
4973 int _bt_gatt_watch_service_changed_indication(const char *sender,
4974 bluetooth_device_address_t *address,
4975 gboolean is_enabled)
4978 bt_service_app_info_t *info = NULL;
4980 BT_INFO("%s Servic changed Indication watcher for app [%s]",
4981 is_enabled ? "Enable":"Disable", sender);
4983 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
4986 if (g_strcmp0(sender, info->sender) == 0 &&
4987 memcmp(info->address.addr, address->addr,
4988 sizeof(bluetooth_device_address_t)) == 0) {
4989 BT_DBG("Found GATT client App.. [%s], sender [%s]", info->uuid, info->sender);
4990 info->is_watcher_enabled = is_enabled;
4994 return BLUETOOTH_ERROR_NONE;
4997 int _bt_unregister_gatt_client_instance(const char *sender, int client_id)
4999 bt_service_app_info_t *info = NULL;
5002 BT_INFO("Unregister Allocated GATT Client instance [%s] Client ID [%d]", sender, client_id);
5004 /* Unregister CLient instance associated with address X. It is possible that another app still
5005 has client_id valid for same remote address */
5006 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
5009 /* Exact matching of sender */
5010 if (!g_strcmp0(info->sender, sender) && info->client_id == client_id) { /* Check for only valid GATT client Instance */
5011 BT_INFO("Unregister GATT client instance [%d]", info->client_id);
5012 numapps[k].client_id = -1;
5013 numapps[k].is_initialized = FALSE;
5014 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
5015 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
5016 memset(&numapps[k].address.addr, 0x00, sizeof(bluetooth_device_address_t));
5018 /* Its a GATT Client Instance */
5019 ret = gattc_deregister(client_id);
5020 if (ret != OAL_STATUS_SUCCESS) {
5021 BT_ERR("DeAllocate GATT Client instance with stack Fail ret: %d", ret);
5022 return BLUETOOTH_ERROR_INTERNAL;
5024 return BLUETOOTH_ERROR_NONE;
5029 return BLUETOOTH_ERROR_NOT_FOUND;
5032 static void __bt_handle_client_mtu_exchange_completed(event_gattc_mtu_configured_t *event_data)
5034 int result = BLUETOOTH_ERROR_NONE;
5035 struct gatt_server_info_t *conn_info = NULL;
5036 GVariant *param = NULL;
5040 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(event_data->conn_id);
5041 if (conn_info == NULL) {
5042 BT_ERR("Cant find connection Information");
5046 BT_INFO("GATT Client: MTU Configured from addr [%s] status [%d] MTU size [%d]",
5047 conn_info->addr, event_data->status, event_data->mtu);
5049 if (event_data->status != OAL_STATUS_SUCCESS)
5050 result = BLUETOOTH_ERROR_INTERNAL;
5052 BT_INFO("reply BT_REQ_ATT_MTU ");
5054 /* DBUS Return fo BT_REQ_ATT_MTU for all the apps */
5055 __bt_gatt_handle_pending_request_info(result, BT_REQ_ATT_MTU, conn_info->addr,
5056 BT_ADDRESS_STRING_SIZE);
5058 if (result == BLUETOOTH_ERROR_NONE) {
5059 BT_INFO("sending mtu event");
5061 mtu = event_data->mtu;
5062 param = g_variant_new("(isqy)",
5068 /* Send event to BT-API */
5069 _bt_send_event(BT_DEVICE_EVENT,
5070 BLUETOOTH_EVENT_GATT_ATT_MTU_CHANGED,
5073 /* Update the MTU for current connection */
5074 BT_INFO("Updating the MTU for current connection");
5075 __bt_update_mtu_gatt_device(conn_info->addr, event_data->mtu);
5079 static struct gatt_mtu_info_t *__bt_find_mtu_gatt_device(char *address)
5082 struct gatt_mtu_info_t *info = NULL;
5086 for (l = gatt_mtu_info_list; l != NULL; l = g_slist_next(l)) {
5087 info = (struct gatt_mtu_info_t*)l->data;
5091 if (!g_strcmp0(info->addr, address)) {
5092 BT_INFO("Remote GATT device found addr[%s]", info->addr);
5097 BT_INFO("Not found Remote GATT device addr[%s]", address);
5101 static void __bt_remove_mtu_gatt_device(char *address)
5103 struct gatt_mtu_info_t *dev_info = NULL;
5107 dev_info = __bt_find_mtu_gatt_device(address);
5110 BT_INFO("removing the gatt device from mtu list");
5111 gatt_mtu_info_list = g_slist_remove(gatt_mtu_info_list, dev_info);
5112 g_free(dev_info->addr);
5117 static void __bt_add_mtu_gatt_device(char *address)
5119 struct gatt_mtu_info_t *dev_info = NULL;
5123 dev_info = __bt_find_mtu_gatt_device(address);
5126 BT_INFO("adding the gatt device in mtu list");
5127 dev_info = g_new0(struct gatt_mtu_info_t, 1);
5128 dev_info->addr = g_strdup(address);
5129 dev_info->att_mtu = BT_DEFAULT_ATT_MTU;
5130 gatt_mtu_info_list = g_slist_append(gatt_mtu_info_list, dev_info);
5134 static void __bt_update_mtu_gatt_device(char *address, int mtu)
5136 struct gatt_mtu_info_t *dev_info = NULL;
5140 dev_info = __bt_find_mtu_gatt_device(address);
5143 BT_INFO("Udating the mtu");
5144 dev_info->att_mtu = mtu;