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__ // TODO: this feature can be used if easy setup scenario is fully supported and the name need to be changed to avoid confusion with existing GATT flags
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;
217 /* App Information structure */
220 char sender[BT_SENDER_MAX_LENGTH];
221 char uuid[BT_UUID_STRING_MAX];
224 bluetooth_advertising_data_t adv_data; /* Will store adv data for specific slot */
226 bluetooth_scan_resp_data_t scan_rsp; /* Will store scan rsp data for specific slot */
228 gboolean is_initialized;
229 GSList *service_handles;
230 #ifdef TIZEN_GATT_CLIENT
231 int client_id; /* GATT Client instance ID */
232 bluetooth_device_address_t address; /* Remote BLE Device Address */
233 gboolean is_watcher_enabled;
235 } bt_service_app_info_t;
237 /* GATT Server Request Info Structure */
238 struct gatt_server_req_info {
239 int connection_id; /* This value will uniquely identify a GATT client-server connection */
240 int request_id; /* This is an unique transaction ID assigned against each request by stack */
241 int attribute_handle; /* GATT server attribute handle */
242 int offset; /* GATT server attribute offset on which request is invoked by GATT client */
243 bluetooth_gatt_att_request_type_e request_type; /* Read or Write request */
244 char *addr; /* Remote GATT client address */
247 /* GATT Indicate confirm result */
248 struct gatt_indicate_cfm_result_info_t {
249 int result; /* Result of event */
250 char *addr; /* Remote GATT client address */
251 int att_hdl; /* Characteristic Attribute handle */
252 int completed; /* 1 if last event, otheriwse 0 */
255 /* Request Search Utility method */
256 static struct gatt_server_req_info *__bt_gatt_server_find_request_info(int request_id,
257 bluetooth_gatt_att_request_type_e req_type);
259 static int __bt_gatt_send_indication_to_all_connected_clients(bluetooth_gatt_att_data_t *data,
260 bluetooth_gatt_server_indication_params_t *param);
262 static void __bt_remove_all_service_handles(bt_service_app_info_t *app_info);
264 static void __bt_free_service_info(bt_gatt_service_info_t *service_info);
266 static int __bt_unregister_gatt_client_instance(int client_if);
268 static void __bt_service_reset_gatt_data(void);
270 #ifdef TIZEN_GATT_CLIENT
271 static void __bt_handle_client_instance_registered(event_gattc_register_t *data);
272 static void __bt_handle_client_connected(event_gattc_conn_t *event_data);
273 static void __bt_handle_client_disconnected(event_gattc_conn_t *event_data);
274 static void __bt_handle_client_service_search_completed(event_gattc_conn_status_t *event_data);
275 static void __bt_handle_client_service_search_result(event_gattc_service_result_t *event_data);
276 static void __bt_handle_client_characteristic_search_result(
277 event_gattc_characteristic_result_t *event_data);
278 static void __bt_handle_client_descriptor_search_result(event_gattc_descriptor_result_t *event_data);
279 static void __bt_handle_client_characteristic_read_data(event_gattc_read_data *event_data);
280 static void __bt_handle_client_descriptor_read_data(event_gattc_read_data *event_data);
281 static void __bt_handle_client_characteristic_write_data(event_gattc_write_data *event_data);
282 static void __bt_handle_client_descriptor_write_data(event_gattc_write_data *event_data);
283 static void __bt_hanlde_le_device_disconnection(event_dev_conn_status_t *event_data);
284 static void __bt_handle_client_notification_registered(event_gattc_regdereg_notify_t *event_data,
285 gboolean is_registered);
286 static void __bt_handle_client_notification_data(event_gattc_notify_data *event_data);
287 static void __bt_handle_client_service_changed_ind(event_gattc_service_changed_data *event_data);
288 static void __bt_handle_client_mtu_exchange_completed(event_gattc_mtu_configured_t *event_data);
291 static int __bt_unregister_gatt_server_instance(int server_instance);
292 static void __bt_cleanup_remote_services(struct gatt_server_info_t *conn_info);
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 (strcasecmp(app->sender, name) == 0 && app->is_initialized == TRUE) {
343 BT_DBG("numapps[%d] Match found, name: %s", k, 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_DBG("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;
477 result = gatt_enable();
478 if (result != OAL_STATUS_SUCCESS) {
479 BT_ERR("gatt Init failed");
480 return _bt_convert_oal_status_to_bt_error(result);
483 /* Register gatt event handler */
484 _bt_service_register_event_handler_callback(BT_GATT_MODULE, __bt_gatt_event_handler);
486 __bt_service_reset_gatt_data();
488 stack_name = oal_get_stack_name();
490 if (stack_name && g_strcmp0(stack_name, "bluez") == 0) {
491 /*In the platform, defacult gatt client should be registered */
492 __bt_register_default_gatt_client();
496 return BLUETOOTH_ERROR_NONE;
499 static void __bt_service_reset_gatt_data(void)
503 BT_INFO("Rest numapp");
506 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
507 numapps[k].is_initialized = 0;
508 numapps[k].instance_id = -1;
509 numapps[k].adv_handle = 0;
510 numapps[k].adv_instance = -1;
511 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
512 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
513 memset(numapps[k].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
514 memset(numapps[k].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
515 numapps[k].adv_data_len = 0;
516 numapps[k].scan_rsp_len = 0;
519 numapps[k].client_id = -1;
520 memset(numapps[k].address.addr, 0x00, BLUETOOTH_ADDRESS_LENGTH);
521 numapps[k].is_watcher_enabled = FALSE;
525 void _bt_gatt_deinit(void)
527 BT_INFO("GATT deinit");
529 /* Un-register the default gatt client before */
530 __bt_unregister_gatt_client_instance(gatt_default_client);
532 if (OAL_STATUS_SUCCESS != gatt_disable())
533 BT_ERR("gatt deinit failed");
535 /* Un-register gatt event handler */
536 _bt_service_unregister_event_handler_callback(BT_GATT_MODULE);
538 __bt_service_reset_gatt_data();
541 void _bt_update_adv_handle(const char *sender, int adv_handle)
544 BT_DBG("Sender [%s] Adv handle [%d]", sender, adv_handle);
545 bt_service_app_info_t *info = NULL;
547 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
549 /* Do not update client instance */
550 if (info->instance_id == -1)
552 /* Search for a app which has same sender and adv handle as 0 */
553 if (!g_strcmp0(info->sender, sender) && info->adv_handle == 0)
554 info->adv_handle = adv_handle;
558 int _bt_is_sender_gatt_server_with_no_adv(const char *sender, int adv_handle)
561 BT_DBG("Sender [%s] Adv handle [%d]", sender, adv_handle);
562 bt_service_app_info_t *info = NULL;
564 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
566 /* Search for a app which has same sender and adv handle as 0
567 It is possible that same sender but different adv handle */
568 if (!g_strcmp0(info->sender, sender) && info->adv_handle == 0) {
569 //info->adv_handle = adv_handle;
570 return info->instance_id;
576 int _bt_get_allocated_server_instance(const char *sender, int adv_handle, gboolean use_reserved_slot)
579 BT_DBG("Sender [%s] Adv handle [%d]", sender, adv_handle);
580 bt_service_app_info_t *info = NULL;
582 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
585 /* Exact matching of Adv handle + sender combination */
586 if (!g_strcmp0(info->sender, sender) && info->adv_handle == adv_handle)
587 return info->instance_id;
589 if (!g_strcmp0(info->sender, sender) && info->adv_handle == -1)
590 return info->instance_id;
596 char * _bt_get_sender_and_handle(int server_instance, int *adv_handle)
599 bt_service_app_info_t *info = NULL;
601 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
604 if (info->instance_id == server_instance && info->adv_handle != 0) {
605 *adv_handle = info->adv_handle;
606 BT_DBG("Server instance [%d] Adv handle [%d] Sender [%s]", server_instance, *adv_handle, info->sender);
607 return g_strdup(info->sender);
613 void _bt_set_new_adv_data(bluetooth_advertising_data_t *adv, int len, int instance)
617 bt_service_app_info_t *info = NULL;
618 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
621 if (info->instance_id == instance) {
622 memcpy(info->adv_data.data, &adv->data, len);
628 void _bt_set_new_scan_rsp_data(bluetooth_scan_resp_data_t *scan, int len, int instance)
632 bt_service_app_info_t *info = NULL;
633 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
636 if (info->instance_id == instance) {
637 memcpy(info->scan_rsp.data, &scan->data, len);
643 void _bt_get_previous_adv_data(bluetooth_advertising_data_t *adv, int *len, int instance)
646 bt_service_app_info_t *info = NULL;
648 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
651 if (info->instance_id == instance) {
652 memcpy(&adv->data, info->adv_data.data, info->adv_data_len);
653 *len = info->adv_data_len;
659 void _bt_get_previous_scan_rsp_data(bluetooth_scan_resp_data_t *scan, int *len, int instance)
663 bt_service_app_info_t *info = NULL;
665 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
668 if (info->instance_id == instance) {
669 memcpy(&scan->data, info->scan_rsp.data, info->scan_rsp_len);
670 *len = info->scan_rsp_len;
676 static int __bt_unregister_gatt_client_instance(int client_if)
678 int ret = OAL_STATUS_SUCCESS;
681 BT_INFO("DeAllocate client instance ID [%d]", client_if);
683 /* Reset data: instance_id parameter could be either for GATT Server or for GATT client */
684 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
685 if (numapps[k].client_id == client_if) {
686 BT_INFO("This is a GATT client app, unregister: Slot [%d] vacant", k);
687 numapps[k].client_id = -1;
688 numapps[k].is_initialized = FALSE;
689 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
690 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
691 memset(&numapps[k].address.addr, 0x00, sizeof(bluetooth_device_address_t));
693 /* Its a GATT Client Instance */
694 ret = gattc_deregister(client_if);
695 if (ret != OAL_STATUS_SUCCESS) {
696 BT_ERR("DeAllocate GATT Client instance with stack Fail ret: %d", ret);
697 return _bt_convert_oal_status_to_bt_error(ret);
702 return BLUETOOTH_ERROR_NONE;
705 static int __bt_unregister_gatt_server_instance(int server_instance)
707 int ret = OAL_STATUS_SUCCESS;
710 /* Unregister the server instance */
711 ret = gatts_unregister(server_instance);
712 if (ret != OAL_STATUS_SUCCESS) {
713 BT_ERR("DeAllocate server instance with stack Fail ret: %d", ret);
714 return _bt_convert_oal_status_to_bt_error(ret);
716 BT_INFO("DeAllocated server instance with stack successful..");
719 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
720 if (numapps[k].instance_id == server_instance) {
721 numapps[k].is_initialized = 0;
722 numapps[k].instance_id = -1;
723 numapps[k].adv_handle = 0;
724 numapps[k].adv_instance = -1;
725 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
726 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
727 memset(numapps[k].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
728 memset(numapps[k].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
729 numapps[k].adv_data_len = 0;
730 numapps[k].scan_rsp_len = 0;
734 BT_DBG("Going8 to reset numapp block num [%d]", k);
735 return BLUETOOTH_ERROR_NONE;
738 static void __bt_remove_all_service_handles(bt_service_app_info_t *app_info)
742 int ret = OAL_STATUS_SUCCESS;
745 if (app_info == NULL)
748 count = g_slist_length(app_info->service_handles);
749 BT_INFO("Before handle removal: current count [%d]", count);
751 for (l = app_info->service_handles; l != NULL;) {
756 BT_INFO("Server Handle to be Removed [%d] Instance ID [%d]", *handle, app_info->instance_id);
757 if (_bt_gatt_server_stop_service(app_info->sender, *handle, app_info->instance_id) != BLUETOOTH_ERROR_NONE)
760 ret = gatts_delete_service(app_info->instance_id, *handle);
761 if (ret != OAL_STATUS_SUCCESS) {
762 BT_ERR("ret: %d", ret);
765 app_info->service_handles = g_slist_remove(app_info->service_handles, handle);
768 count = g_slist_length(app_info->service_handles);
769 BT_INFO("After deleting current count [%d]", count);
775 int _bt_unregister_server_instance(const char *sender, int adv_handle)
777 BT_INFO("Unregister Allocated server instance request Sender [%s] Adv handle [%d]", sender, adv_handle);
778 int result = BLUETOOTH_ERROR_NONE;
779 int apps[MAX_APPS_SUPPORTED];
782 bt_service_app_info_t *info = NULL;
784 memset(&apps, -1, sizeof(apps));
786 if (adv_handle == 0) {
787 BT_DBG("Its a direct GATT Server app request to unregister");
788 /* Unregister server instance for each app with same sender (case: GATT Server with multiple adv handle) */
790 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
793 /* Exact matching of sender */
794 if (!g_strcmp0(info->sender, sender)) {
795 BT_INFO("Unregister GATT server instance [%d]", info->instance_id);
796 /* Unregister all service handles with stack */
797 __bt_remove_all_service_handles(info);
799 /* Disable adv if running */
800 BT_INFO("Disable Advertising Adv Handle [%d] sender [%s]",
801 info->adv_handle, sender);
802 _bt_set_advertising(sender, info->adv_handle, FALSE, FALSE);
804 /* Save all instances which need to be unregistered */
805 apps[info->instance_id] = 1;
809 BT_DBG("Its an Internal unregister request by adv application");
810 server_instance = _bt_get_allocated_server_instance(sender, adv_handle, FALSE);
811 BT_DBG("Its an Internal unregister request by adv application: Adv disabled srv instance [%d]", server_instance);
812 if (server_instance == -1) {
813 BT_ERR("No allocated server instance to be removed");
814 return BLUETOOTH_ERROR_INVALID_PARAM;
817 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
819 if (info->instance_id == server_instance) {
820 if (info->service_handles == NULL) {
821 BT_INFO("There are no Service handles with this app, safe to unregister");
822 /* Unregister server instance only if this sender does not have any gatt services in it */
823 result = __bt_unregister_gatt_server_instance(server_instance);
825 info->adv_handle = 0;
826 memset(info->adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
827 info->adv_data_len = 0;
828 memset(info->scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
829 info->scan_rsp_len = 0;
836 /* Unregister all apps one by one */
837 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
839 BT_INFO("Unregister app[%d]", k);
840 /* Unregister server instance */
841 __bt_unregister_gatt_server_instance(k);
848 int _bt_register_server_instance(const char *sender, int adv_handle)
850 int ret = OAL_STATUS_SUCCESS;
851 char *uuid_string = NULL;
856 BT_INFO("Check on which instance Server instance can be initialized....");
857 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
858 if (numapps[k].is_initialized == 1) {
859 BT_DBG("Instance ID [%d] is already in use..Check next slot", numapps[k].instance_id);
862 BT_DBG("Time to register GATT Server..UUID to be used is [%s] slot [%d]", uuid_list[slot-1], slot);
868 BT_ERR("No Slot if free for GATT Server registration..");
869 return BLUETOOTH_ERROR_REGISTRATION_FAILED;
872 uuid_string = g_malloc0(BT_UUID_STRING_MAX);
873 _bt_string_to_uuid(uuid_list[slot-1], (service_uuid_t*)&uuid);
874 g_strlcpy(uuid_string, uuid_list[slot-1], BT_UUID_STRING_MAX);
875 BT_INFO("Copied UUID string [%s] slot [%d]", uuid_string, slot);
877 /* Register GATT Server */
878 ret = gatts_register(&uuid);
879 if (ret != OAL_STATUS_SUCCESS) {
880 BT_ERR("ret: %d", ret);
882 return _bt_convert_oal_status_to_bt_error(ret);
884 BT_DBG("GATT Server registration call successfully accepted by OAL..wait for Instance Initialized event from OAL..");
885 /* Return & wait for GATT Server Instance Initialization event */
886 memset(numapps[slot].sender, 0x00, sizeof(numapps[slot].sender));
887 memset(numapps[slot].uuid, 0x00, sizeof(numapps[slot].uuid));
889 g_strlcpy(numapps[slot].sender, sender, sizeof(numapps[slot].sender));
890 g_strlcpy(numapps[slot].uuid, uuid_string, sizeof(numapps[slot].uuid));
892 numapps[slot].is_initialized = 0; /* Set initialization from app registered callback */
893 numapps[slot].adv_handle = adv_handle;
896 return BLUETOOTH_ERROR_NONE;
900 static void __bt_gatt_get_pending_request_info(int service_function,
904 invocation_info_t *req_info = NULL;
906 for (l = _bt_get_invocation_list(); l != NULL; l = g_slist_next(l)) {
908 if (req_info == NULL)
911 if (req_info->service_function == service_function) {
912 *sender = req_info->sender;
918 static void __bt_gatt_handle_pending_request_info(int result,
919 int service_function, void *data, unsigned int size)
923 invocation_info_t *req_info = NULL;
924 ret_if(data == NULL);
926 for (l = _bt_get_invocation_list(); l != NULL; ) {
929 if (req_info == NULL || req_info->service_function != service_function)
932 switch (service_function) {
934 case BT_GATT_SERVER_REGISTER: {
935 bt_service_app_info_t *param = (bt_service_app_info_t*)data;
937 if (!g_strcmp0(req_info->sender, param->sender)) {
938 BT_DBG("GATT Server app found [%s]", req_info->sender);
940 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
941 g_array_append_vals(out_param, ¶m->instance_id, sizeof(int));
942 _bt_service_method_return(req_info->context, out_param, result);
944 _bt_free_info_from_invocation_list(req_info);
945 g_array_free(out_param, TRUE);
949 case BT_GATT_SERVER_START_SERVICE:
950 case BT_GATT_SERVER_DELETE_SERVICE: {
951 bt_service_app_info_t *param = (bt_service_app_info_t*)data;
953 int *saved_instance_id = (int*)req_info->user_data;
954 if (!g_strcmp0(req_info->sender, param->sender) && param->instance_id == *saved_instance_id) {
955 BT_DBG("GATT Server app found [%s] Instance ID [%d] Reply DBUS",
956 req_info->sender, *saved_instance_id);
958 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
959 g_array_append_vals(out_param, &saved_instance_id, sizeof(int));
960 _bt_service_method_return(req_info->context, out_param, result);
962 _bt_free_info_from_invocation_list(req_info);
963 g_array_free(out_param, TRUE);
967 case BT_GATT_SERVER_ADD_SERVICE:
968 case BT_GATT_SERVER_ADD_DESCRIPTOR:
969 case BT_GATT_SERVER_ADD_CHARACTERISTIC: {
970 int *handle = (int*)data;
971 BT_DBG("Characteristic added: Handle [%d]", *handle);
972 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
974 g_array_append_vals(out_param, handle, sizeof(int));
975 _bt_service_method_return(req_info->context, out_param, result);
977 _bt_free_info_from_invocation_list(req_info);
978 g_array_free(out_param, TRUE);
983 case BT_DISCONNECT_LE: {
984 #ifdef TIZEN_GATT_CLIENT
985 char *addr = (char*)req_info->user_data;
986 bluetooth_device_address_t address;
988 if (!g_strcmp0(addr, (char*)data)) {
989 BT_INFO("GATT Client connect-disconnect call pending for app [%s] addr [%s]",
990 req_info->sender, addr + 12);
991 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
992 _bt_convert_addr_string_to_type(address.addr, addr);
994 g_array_append_vals(out_param, (bluetooth_device_address_t*)&address,
995 sizeof(bluetooth_device_address_t));
996 _bt_service_method_return(req_info->context, out_param, result);
998 _bt_free_info_from_invocation_list(req_info);
999 g_array_free(out_param, TRUE);
1002 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1004 g_array_append_vals(out_param, (bluetooth_device_address_t*)data,
1005 sizeof(bluetooth_device_address_t));
1006 _bt_service_method_return(req_info->context, out_param, result);
1008 _bt_free_info_from_invocation_list(req_info);
1010 g_array_free(out_param, TRUE);
1014 #ifdef TIZEN_GATT_CLIENT
1015 case BT_GATT_CLIENT_REGISTER: {
1016 bt_service_app_info_t *param = (bt_service_app_info_t*)data;
1018 if (!g_strcmp0(req_info->sender, param->sender)) {
1019 BT_DBG("GATT Client app found [%s] created client ID [%d]",
1020 req_info->sender, param->client_id);
1022 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1023 g_array_append_vals(out_param, ¶m->client_id, sizeof(int));
1024 _bt_service_method_return(req_info->context, out_param, result);
1026 _bt_free_info_from_invocation_list(req_info);
1027 g_array_free(out_param, TRUE);
1031 case BT_GATT_GET_PRIMARY_SERVICES: {
1032 bt_services_browse_info_t *param = (bt_services_browse_info_t*)data;
1033 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1034 _bt_convert_addr_type_to_string(address,
1035 (unsigned char *)(¶m->device_addr.addr));
1037 /* Match address to determine same request */
1038 if (!g_strcmp0((char*)req_info->user_data, address)) {
1039 BT_DBG("GATT Client app found [%s] Remote address [%s]",
1040 req_info->sender, address);
1042 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1043 g_array_append_vals(out_param, param,
1044 sizeof(bt_services_browse_info_t));
1046 //g_array_append_vals(out_param, ¶m->client_id, sizeof(int));
1047 _bt_service_method_return(req_info->context, out_param, result);
1049 _bt_free_info_from_invocation_list(req_info);
1050 g_array_free(out_param, TRUE);
1055 case BT_GATT_GET_SERVICE_PROPERTIES: {
1056 bt_char_browse_info_t param;
1057 memcpy((void*)¶m, data, sizeof(bt_char_browse_info_t));
1058 //bt_char_browse_info_t *param = (bt_char_browse_info_t*)data;
1060 bluetooth_gatt_client_svc_prop_info_t *prop = (bluetooth_gatt_client_svc_prop_info_t*)req_info->user_data;
1062 /* Match both address and service properties to determine same request */
1063 if (!memcmp(param.device_addr.addr,
1064 prop->device_address.addr,
1065 sizeof(bluetooth_device_address_t)) &&
1066 !memcmp(param.svc_uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1067 param.svc_inst_id == prop->svc.instance_id) {
1068 BT_DBG("Returning Service properties");
1070 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1071 g_array_append_vals(out_param, ¶m, sizeof(bt_char_browse_info_t));
1072 _bt_service_method_return(req_info->context, out_param, result);
1074 _bt_free_info_from_invocation_list(req_info);
1075 g_array_free(out_param, TRUE);
1079 case BT_GATT_GET_CHARACTERISTIC_PROPERTIES: {
1080 bt_descriptor_browse_info_t *param = (bt_descriptor_browse_info_t*)data;
1082 bluetooth_gatt_client_char_prop_info_t *prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
1084 /* Match both address, service properties &char properties to determine same request */
1085 if (!memcmp(param->device_addr.addr,
1086 prop->device_address.addr,
1087 sizeof(bluetooth_device_address_t)) &&
1088 !memcmp(param->svc_uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1089 param->svc_inst_id == prop->svc.instance_id &&
1090 !memcmp(param->char_uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1091 param->char_inst_id == prop->characteristic.instance_id) {
1092 BT_DBG("Returning Characteristic properties");
1093 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1094 g_array_append_vals(out_param, param, sizeof(bt_descriptor_browse_info_t));
1095 _bt_service_method_return(req_info->context, out_param, result);
1097 _bt_free_info_from_invocation_list(req_info);
1098 g_array_free(out_param, TRUE);
1102 case BT_GATT_WATCH_CHARACTERISTIC: {
1103 bt_gatt_notif_reg_info_t *param = (bt_gatt_notif_reg_info_t*)data;
1104 bluetooth_gatt_client_char_prop_info_t *prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
1106 /* Match both address, service properties &char properties to determine same request */
1107 if (!memcmp(param->addr.addr,
1108 prop->device_address.addr,
1109 sizeof(bluetooth_device_address_t)) &&
1110 !memcmp(param->svc_uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1111 param->svc_inst == prop->svc.instance_id &&
1112 !memcmp(param->char_uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1113 param->char_inst == prop->characteristic.instance_id) {
1114 BT_INFO("Characteristic Watch Successful: Is registered [%d]",
1115 param->is_registered);
1116 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1117 g_array_append_vals(out_param, param, sizeof(bt_gatt_notif_reg_info_t));
1118 _bt_service_method_return(req_info->context, out_param, result);
1119 _bt_free_info_from_invocation_list(req_info);
1120 g_array_free(out_param, TRUE);
1124 case BT_GATT_READ_CHARACTERISTIC:
1125 case BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE: {
1126 bluetooth_gatt_client_char_prop_info_t *param = (bluetooth_gatt_client_char_prop_info_t*)data;
1128 char *addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
1129 bluetooth_gatt_client_char_prop_info_t *prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
1130 _bt_convert_addr_type_to_string(addr, prop->device_address.addr);
1132 if (!memcmp(param, prop, sizeof(bluetooth_gatt_client_char_prop_info_t))) {
1133 BT_DBG("Gatt Char read or write request matched for address [%s]", addr);
1134 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1135 g_array_append_vals(out_param, param, sizeof(bluetooth_gatt_client_char_prop_info_t));
1136 _bt_service_method_return(req_info->context, out_param, result);
1138 _bt_free_info_from_invocation_list(req_info);
1139 g_array_free(out_param, TRUE);
1144 case BT_GATT_READ_DESCRIPTOR_VALUE:
1145 case BT_GATT_WRITE_DESCRIPTOR_VALUE: {
1146 bluetooth_gatt_client_desc_prop_info_t *param = (bluetooth_gatt_client_desc_prop_info_t*)data;
1148 char *addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
1149 bluetooth_gatt_client_desc_prop_info_t *prop = (bluetooth_gatt_client_desc_prop_info_t*)req_info->user_data;
1150 _bt_convert_addr_type_to_string(addr, prop->device_address.addr);
1152 if (!memcmp(param, prop, sizeof(bluetooth_gatt_client_desc_prop_info_t))) {
1153 BT_DBG("Descriptor read or write request matched for address [%s]", addr);
1154 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1155 g_array_append_vals(out_param, param, sizeof(bluetooth_gatt_client_desc_prop_info_t));
1156 _bt_service_method_return(req_info->context, out_param, result);
1158 _bt_free_info_from_invocation_list(req_info);
1159 g_array_free(out_param, TRUE);
1164 case BT_REQ_ATT_MTU: {
1165 char *addr = (char*)req_info->user_data;
1166 bluetooth_device_address_t address;
1168 if (!g_strcmp0(addr, (char*)data)) {
1169 BT_DBG("GATT Client BT_REQ_ATT_MTU call pending for app [%s] addr [%s]",
1170 req_info->sender, addr);
1171 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1172 _bt_convert_addr_string_to_type(address.addr, addr);
1174 g_array_append_vals(out_param, (bluetooth_device_address_t*)&address,
1175 sizeof(bluetooth_device_address_t));
1176 _bt_service_method_return(req_info->context, out_param, result);
1178 _bt_free_info_from_invocation_list(req_info);
1179 g_array_free(out_param, TRUE);
1189 static void __bt_handle_server_instance_registered(event_gatts_register_t *data)
1191 bt_service_app_info_t *info = NULL;
1193 char *uuid_string = g_malloc0(BT_UUID_STRING_MAX);
1195 _bt_uuid_to_string(&(data->server_uuid), uuid_string);
1196 BT_INFO("Instance ID is Intialized [%d] UUID initialized [%s]", data->server_inst, uuid_string);
1198 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1199 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1202 if (g_strcmp0(info->uuid, uuid_string) == 0) {
1203 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1204 info->is_initialized = TRUE;
1205 info->instance_id = data->server_inst;
1206 info->adv_instance = data->server_inst;
1207 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE, BT_GATT_SERVER_REGISTER,
1208 (void*)info, sizeof(bt_service_app_info_t));
1212 g_free(uuid_string);
1215 static void __bt_handle_gatt_server_service_added(event_gatts_srvc_prm_t *event)
1217 int result = BLUETOOTH_ERROR_NONE;
1221 bt_service_app_info_t *info = NULL;
1224 char *uuid_str = g_malloc0(BT_UUID_STRING_MAX);
1225 _bt_uuid_to_string(&(event->gatt_srvc_id.id.uuid), uuid_str);
1226 BT_INFO("GATT Added Service UUID: [%s] Server Ins [%d] Service hdl [%d] Is Primary [%d]\n",
1227 uuid_str, event->gatt_srvc_stat.server_inst, svc_handle, event->gatt_srvc_id.is_prmry);
1229 if (event->gatt_srvc_stat.status != OAL_STATUS_SUCCESS) {
1230 result = BLUETOOTH_ERROR_INTERNAL;
1231 svc_handle = 0; /* Service handle set to 0 indicates.
1232 0 is reserved by BT SIG, cant be used by app*/
1234 svc_handle = event->gatt_srvc_stat.servic_hndl;
1236 BT_INFO("GATT Added Service Status [%d] Server Ins [%d] Service hdl [%d] Is Primary [%d]\n",
1237 event->gatt_srvc_stat.status, event->gatt_srvc_stat.server_inst, svc_handle, event->gatt_srvc_id.is_prmry);
1239 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1240 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1243 if (info->instance_id == event->gatt_srvc_stat.server_inst) {
1244 BT_INFO("numapps[%d] Found GATT Server.. UUID [%s], sender [%s]", k, info->uuid, info->sender);
1245 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_ADD_SERVICE,
1246 (int*)&svc_handle, sizeof(int));
1248 /* Add Service Handle */
1249 if (svc_handle > 0) {
1250 handle = g_malloc0(sizeof(int));
1251 *handle = svc_handle;
1252 numapps[k].service_handles = g_slist_append(numapps[k].service_handles, handle);
1253 count = g_slist_length(numapps[k].service_handles);
1254 BT_INFO("Added Service handle [%d] to list..current count [%d]", svc_handle, count);
1263 static void __bt_handle_gatt_server_characteristic_added(event_gatts_srvc_charctr_t *event)
1265 int result = BLUETOOTH_ERROR_NONE;
1267 bt_service_app_info_t *info = NULL;
1269 char *uuid_str = g_malloc0(BT_UUID_STRING_MAX);
1271 BT_INFO("GATT Server Char added status [%d]", event->gatt_srvc_stat.status);
1272 BT_INFO("GATT Server_inst: [%d]", event->gatt_srvc_stat.server_inst);
1273 BT_INFO("GATT Service Handle: [%d]", event->gatt_srvc_stat.servic_hndl);
1274 BT_INFO("GATT Add characteristic Status: [%d]", event->gatt_srvc_stat.status);
1275 BT_INFO("GATT Service characteristic Handle: [%d]", event->charctr_hndl);
1277 if (event->gatt_srvc_stat.status != OAL_STATUS_SUCCESS) {
1278 result = BLUETOOTH_ERROR_INTERNAL;
1279 char_handle = 0; /* characteristic handle set to 0 indicates.
1280 0 is reserved by BT SIG, cant be used by app*/
1282 char_handle = event->charctr_hndl;
1285 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1286 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1289 if (info->instance_id == event->gatt_srvc_stat.server_inst) {
1290 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1291 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_ADD_CHARACTERISTIC,
1292 (int*)&char_handle, sizeof(int));
1298 _bt_uuid_to_string(&(event->charctr_uuid), uuid_str);
1299 BT_INFO("GATT Added Characteristic: UUID: [%s]", uuid_str);
1304 static void __bt_handle_gatt_server_descriptor_added(event_gatts_srvc_descr_t* event)
1306 int result = BLUETOOTH_ERROR_NONE;
1308 bt_service_app_info_t *info = NULL;
1310 char *uuid_str = g_malloc0(BT_UUID_STRING_MAX);
1312 BT_INFO("GATT Server Descriptor added status [%d]", event->gatt_srvc_stat.status);
1313 BT_INFO("GATT Server_inst: [%d]", event->gatt_srvc_stat.server_inst);
1314 BT_INFO("GATT Service Handle: [%d]", event->gatt_srvc_stat.servic_hndl);
1315 BT_INFO("GATT Add Descriptor Status: [%d]", event->gatt_srvc_stat.status);
1316 BT_INFO("GATT Service Descriptor Handle: [%d]", event->descrptr_hndl);
1318 if (event->gatt_srvc_stat.status != OAL_STATUS_SUCCESS) {
1319 result = BLUETOOTH_ERROR_INTERNAL;
1320 desc_handle = 0; /* Service handle set to 0 indicates.
1321 0 is reserved by BT SIG, cant be used by app*/
1323 desc_handle = event->descrptr_hndl;
1325 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1326 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1329 if (info->instance_id == event->gatt_srvc_stat.server_inst) {
1330 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1331 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_ADD_DESCRIPTOR,
1332 (int*)&desc_handle, sizeof(int));
1337 _bt_uuid_to_string(&(event->descrptr_uuid), uuid_str);
1338 BT_INFO("GATT Added Descriptor: UUID: [%s]", uuid_str);
1343 static void __bt_handle_gatt_server_service_started(event_gatts_srvc_t *event)
1345 bt_service_app_info_t *info = NULL;
1346 int result = BLUETOOTH_ERROR_NONE;
1348 BT_INFO("GATT Server Service Started..");
1351 BT_INFO("GATT Server_inst: [%d]", event->server_inst);
1352 BT_INFO("GATT Service Handle: [%d]", event->servic_hndl);
1353 BT_INFO("GATT Service Started Status: [%d]", event->status);
1355 if (event->status != OAL_STATUS_SUCCESS) {
1356 BT_ERR("GATT Server Service Start Failed Err: [%d]", event->status);
1357 result = BLUETOOTH_ERROR_INTERNAL;
1360 /* Check if the just registered Instance ID belongs to requester */
1361 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1364 if (info->instance_id == event->server_inst) {
1365 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1366 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_START_SERVICE,
1367 (void*)info, sizeof(bt_service_app_info_t));
1373 static void __bt_handle_gatt_server_service_stopped(event_gatts_srvc_t *event)
1375 int result = BLUETOOTH_ERROR_NONE;
1376 bt_service_app_info_t *info = NULL;
1378 BT_INFO("GATT Server Service Stopped..");
1380 BT_INFO("GATT Server_inst: [%d]", event->server_inst);
1381 BT_INFO("GATT Service Handle: [%d]", event->servic_hndl);
1382 BT_INFO("GATT Service Stopped Status: [%d]", event->status);
1384 if (event->status != OAL_STATUS_SUCCESS) {
1385 BT_ERR("GATT Server Service Stop Failed Err: [%d]", event->status);
1386 result = BLUETOOTH_ERROR_INTERNAL;
1389 /* Check if the just registered Instance ID belongs to requester */
1390 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1393 if (info->instance_id == event->server_inst) {
1394 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1395 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_STOP_SERVICE,
1396 (void*)info, sizeof(bt_service_app_info_t));
1402 static void __bt_handle_gatt_server_service_deleted(event_gatts_srvc_t *event)
1404 int result = BLUETOOTH_ERROR_NONE;
1405 bt_service_app_info_t *info = NULL;
1407 BT_INFO("GATT Server Service Deleted..");
1409 BT_INFO("GATT Server_inst: [%d]", event->server_inst);
1410 BT_INFO("GATT Service Handle: [%d]", event->servic_hndl);
1411 BT_INFO("GATT Service Deleted Status: [%d]", event->status);
1413 if (event->status != OAL_STATUS_SUCCESS) {
1414 BT_ERR("GATT Server Service Delete Failed Err: [%d]", event->status);
1415 result = BLUETOOTH_ERROR_INTERNAL;
1418 /* Check if the just registered Instance ID belongs to requester */
1419 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1422 if (info->instance_id == event->server_inst) {
1423 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1424 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_DELETE_SERVICE,
1425 (void*)info, sizeof(bt_service_app_info_t));
1431 struct gatt_client_info_t *_bt_find_remote_gatt_client_info(char *address)
1434 struct gatt_client_info_t *info = NULL;
1435 for (l = gatt_client_info_list; l != NULL; l = g_slist_next(l)) {
1436 info = (struct gatt_client_info_t*)l->data;
1440 if (!g_strcmp0(info->addr, address)) {
1441 BT_DBG("Remote GATT client found addr[%s]", info->addr);
1448 static struct gatt_client_info_t *__bt_find_remote_gatt_client_info_from_conn_id(int conn_id)
1451 struct gatt_client_info_t *info = NULL;
1453 for (l = gatt_client_info_list; l != NULL; l = g_slist_next(l)) {
1454 info = (struct gatt_client_info_t*)l->data;
1458 if (info->connection_id == conn_id) {
1459 BT_INFO("Remote GATT client found addr[%s]", info->addr);
1466 #ifdef TIZEN_GATT_CLIENT
1467 struct gatt_server_info_t *_bt_find_remote_gatt_server_info(char *address)
1470 struct gatt_server_info_t *info = NULL;
1471 for (l = gatt_server_info_list; l != NULL; l = g_slist_next(l)) {
1472 info = (struct gatt_server_info_t*)l->data;
1476 if (!g_strcmp0(info->addr, address)) {
1477 BT_DBG("Remote GATT Server found addr[%s] connection_id %d", info->addr, info->connection_id);
1484 static struct gatt_out_conn_info_t* __bt_find_gatt_outgoing_conn_info(char *address)
1487 struct gatt_out_conn_info_t *info = NULL;
1488 for (l = outgoing_gatt_conn_list; l != NULL; l = g_slist_next(l)) {
1489 info = (struct gatt_out_conn_info_t*)l->data;
1493 if (!g_strcmp0(info->addr, address)) {
1494 BT_INFO("Outgoing connection info found addr[%s]", info->addr + 12);
1502 #ifdef TIZEN_GATT_CLIENT
1503 static void __bt_handle_gatt_server_connection_state(event_gatts_conn_t *event)
1505 int result = BLUETOOTH_ERROR_NONE;
1506 struct gatt_client_info_t *client_info = NULL;
1507 #ifndef __INTEGRATE_GATT_INFO__
1508 struct gatt_server_info_t *server_info = NULL;
1510 bluetooth_device_address_t dev_addr;
1511 GVariant *param = NULL;
1514 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1516 memcpy(dev_addr.addr, event->address.addr, 6);
1518 /* REPLY dbus Context to application which called BT_CONNECT_LE. There is status
1520 _bt_convert_addr_type_to_string(address,
1521 (unsigned char *)dev_addr.addr);
1523 if (event->status != OAL_STATUS_SUCCESS)
1524 result = BLUETOOTH_ERROR_INTERNAL;
1526 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
1527 address, BT_ADDRESS_STRING_SIZE);
1529 BT_INFO("GATT Server Connedted: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1530 address + 12, event->server_inst, event->conn_id);
1533 /* Check if device is already in connected list */
1534 client_info = _bt_find_remote_gatt_client_info(address);
1537 BT_DBG("Conn Info absent: But no need to Send Local GATT Server Connected event to apps");
1539 param = g_variant_new("(is)", result, address);
1541 /* Send event to application */
1542 _bt_send_event(BT_DEVICE_EVENT,
1543 BLUETOOTH_EVENT_GATT_SERVER_CONNECTED, /* Local device is GATT server */
1546 /* Save client connection info */
1547 client_info = g_new0(struct gatt_client_info_t, 1);
1548 client_info->addr = g_strdup(address);
1549 BT_INFO("Added GATT client addr[%s]", client_info->addr + 12);
1550 client_info->connection_id = event->conn_id;
1551 #ifdef __INTEGRATE_GATT_INFO__
1552 client_info->client_id = -1;
1554 client_info->instance_id = event->server_inst;
1555 gatt_client_info_list = g_slist_append(gatt_client_info_list, client_info);
1556 BT_INFO("Total num of connected Remote GATT Clients [%d]", g_slist_length(gatt_client_info_list));
1558 #ifndef __INTEGRATE_GATT_INFO__
1559 /* Save server connection info */
1560 server_info = g_new0(struct gatt_server_info_t, 1);
1561 server_info->addr = g_strdup(address);
1562 server_info->client_id = -1;
1563 BT_INFO("Added GATT server addr[%s]", server_info->addr + 12);
1564 server_info->connection_id = event->conn_id;
1565 gatt_server_info_list = g_slist_append(gatt_server_info_list, server_info);
1566 BT_INFO("Total num of connected Remote GATT Servers [%d]", g_slist_length(gatt_server_info_list));
1569 ret = gattc_add_connection_info((bt_address_t *)&dev_addr, event->conn_id, event->server_inst);
1570 if (ret != OAL_STATUS_SUCCESS) {
1571 BT_ERR("gattc register server instance failed");
1575 __bt_add_mtu_gatt_device(address);
1580 /* GATT Server Dis connected */
1581 static void __bt_handle_gatt_server_disconnection_state(event_gatts_conn_t *event)
1583 int result = BLUETOOTH_ERROR_NONE;
1584 struct gatt_client_info_t *client_info = NULL;
1585 #ifndef __INTEGRATE_GATT_INFO__
1586 struct gatt_server_info_t *server_info = NULL;
1588 bluetooth_device_address_t dev_addr;
1589 GVariant *param = NULL;
1590 char address[BT_ADDRESS_STRING_SIZE];
1592 memcpy(dev_addr.addr, event->address.addr, 6);
1594 /* REPLY dbus Context to application which called BT_DISCONNECT_LE. There is status
1596 _bt_convert_addr_type_to_string(address,
1597 (unsigned char *)dev_addr.addr);
1599 if (event->status != OAL_STATUS_SUCCESS)
1600 result = BLUETOOTH_ERROR_INTERNAL;
1602 if (NULL == _bt_get_request_info_data(BT_DISCONNECT_LE, address)) {
1603 if (NULL != _bt_get_request_info_data(BT_CONNECT_LE, address)) {
1604 result = BLUETOOTH_ERROR_INTERNAL;
1606 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
1607 address, BT_ADDRESS_STRING_SIZE);
1609 BT_ERR("Failed to connect Local GATT Server Remote Client addr[%s]", address + 12);
1613 __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE,
1614 address, BT_ADDRESS_STRING_SIZE);
1617 BT_INFO("Local GATT Server DisConnected: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1618 address + 12, event->server_inst, event->conn_id);
1619 /* Remove Connection info */
1620 client_info = _bt_find_remote_gatt_client_info(address);
1622 BT_DBG("No need to Send Local GATT Server Disconnected event to apps, just remove remote client info");
1624 param = g_variant_new("(is)", result, address);
1625 /* Send event to application */
1626 _bt_send_event(BT_DEVICE_EVENT,
1627 BLUETOOTH_EVENT_GATT_SERVER_DISCONNECTED, /* Local device is GATT server */
1630 #ifndef __INTEGRATE_GATT_INFO__
1631 /* Remove server info from list */
1632 server_info = _bt_find_remote_gatt_server_info(address);
1634 gatt_server_info_list = g_slist_remove(gatt_server_info_list, server_info);
1636 /* Remove all services from info list_gatt_info */
1637 __bt_cleanup_remote_services(server_info);
1639 BT_INFO("Can not find conn info, already removed!");
1642 /* Remove all services from info list_gatt_info */
1643 __bt_cleanup_remote_services(client_info);
1646 /* Remove client info from List */
1647 gatt_client_info_list = g_slist_remove(gatt_client_info_list, client_info);
1648 BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
1649 g_free(client_info->addr);
1650 g_free(client_info);
1653 __bt_remove_mtu_gatt_device(address);
1657 static void __bt_handle_gatt_server_connection_state(event_gatts_conn_t *event)
1659 int result = BLUETOOTH_ERROR_NONE;
1660 struct gatt_client_info_t *conn_info = NULL;
1661 bt_service_app_info_t *info = NULL;
1662 bluetooth_device_address_t dev_addr;
1663 GVariant *param = NULL;
1666 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1668 memcpy(dev_addr.addr, event->address.addr, 6);
1671 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1672 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1675 if (info->instance_id == event->server_inst) {
1676 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1677 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
1678 (bluetooth_device_address_t*)&dev_addr, sizeof(bluetooth_device_address_t));
1684 /* Send event to BT-API */
1685 _bt_convert_addr_type_to_string(address,
1686 (unsigned char *)dev_addr.addr);
1688 BT_INFO("GATT Server Connedted: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1689 address, event->server_inst, event->conn_id);
1692 /* Check if device is already in connected list */
1693 conn_info = _bt_find_remote_gatt_client_info(address);
1696 param = g_variant_new("(is)", result, address);
1697 /* Send event to application */
1698 _bt_send_event(BT_GATT_SERVER_EVENT,
1699 BLUETOOTH_EVENT_GATT_CONNECTED,
1702 /* Save Connection info */
1703 conn_info = g_new0(struct gatt_client_info_t, 1);
1704 conn_info->addr = g_strdup(address);
1705 BT_INFO("Added GATT client addr[%s]", conn_info->addr);
1706 conn_info->connection_id = event->conn_id;
1707 conn_info->instance_id = event->server_inst;
1708 gatt_client_info_list = g_slist_append(gatt_client_info_list, conn_info);
1709 BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
1715 /* GATT Server Dis connected */
1716 static void __bt_handle_gatt_server_disconnection_state(event_gatts_conn_t *event)
1718 int result = BLUETOOTH_ERROR_NONE;
1719 struct gatt_client_info_t *conn_info = NULL;
1720 bluetooth_device_address_t dev_addr;
1721 bt_service_app_info_t *info = NULL;
1722 GVariant *param = NULL;
1725 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1727 memcpy(dev_addr.addr, event->address.addr, 6);
1729 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1730 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1733 if (info->instance_id == event->server_inst) {
1734 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1735 __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE,
1736 (bluetooth_device_address_t*)&dev_addr, sizeof(bluetooth_device_address_t));
1742 /* Send event to BT-API */
1743 _bt_convert_addr_type_to_string(address,
1744 (unsigned char *)dev_addr.addr);
1746 BT_INFO("GATT Server DisConnected: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1747 address, event->server_inst, event->conn_id);
1749 param = g_variant_new("(is)", result, address);
1750 /* Send event to application */
1751 _bt_send_event(BT_GATT_SERVER_EVENT,
1752 BLUETOOTH_EVENT_GATT_DISCONNECTED,
1755 /* Remove Connection info */
1756 conn_info = _bt_find_remote_gatt_client_info(address);
1758 BT_INFO("Remove GATT client info from List..");
1759 /* Remove info from List */
1760 gatt_client_info_list = g_slist_remove(gatt_client_info_list, conn_info);
1761 BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
1762 g_free(conn_info->addr);
1771 static void __bt_handle_gatt_server_acquire_write_requested(event_gatts_srvc_acquire_attr_t *event)
1773 GVariant *param = NULL;
1774 int result = BLUETOOTH_ERROR_NONE;
1775 struct gatt_server_req_info *req_info = NULL;
1776 bluetooth_device_address_t dev_addr;
1777 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1779 BT_INFO("GATT Server ACQUIRE Write Req");
1780 BT_DBG(" conn id %d, trans id %d, attr andle %d", event->attr_trans.conn_id,
1781 event->attr_trans.trans_id, event->attr_trans.attr_handle);
1783 //address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1784 memcpy(dev_addr.addr, event->address.addr, 6);
1786 req_info = g_new0(struct gatt_server_req_info, 1);
1787 req_info->request_id = event->attr_trans.trans_id;
1788 req_info->attribute_handle = event->attr_trans.attr_handle;
1789 req_info->connection_id = event->attr_trans.conn_id;
1790 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_ACQUIRE_WRITE;
1791 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1793 _bt_convert_addr_type_to_string(address,
1794 (unsigned char *)dev_addr.addr);
1796 param = g_variant_new("(iiiiiis)", result,
1797 event->attr_trans.conn_id,
1798 event->attr_trans.trans_id,
1799 event->attr_trans.attr_handle,
1800 event->mtu, event->attr_trans.offset, address);
1801 BT_DBG("remote address : [%s]", address);
1803 _bt_send_event(BT_GATT_SERVER_EVENT,
1804 BLUETOOTH_EVENT_GATT_SERVER_ACQUIRE_WRITE,
1809 static void __bt_handle_gatt_server_acquire_notify_requested(event_gatts_srvc_acquire_attr_t *event)
1811 GVariant *param = NULL;
1812 int result = BLUETOOTH_ERROR_NONE;
1813 struct gatt_server_req_info *req_info = NULL;
1814 bluetooth_device_address_t dev_addr;
1815 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1817 BT_INFO("GATT Server ACQUIRE Notify Req");
1818 BT_DBG("conn id %d, trans id %d, attr handle %d, req address %s",
1819 event->attr_trans.conn_id, event->attr_trans.trans_id,
1820 event->attr_trans.attr_handle, address);
1822 memcpy(dev_addr.addr, event->address.addr, 6);
1823 _bt_convert_addr_type_to_string(address,
1824 (unsigned char *)dev_addr.addr);
1825 BT_DBG("remote address : [%s]", address);
1827 req_info = g_new0(struct gatt_server_req_info, 1);
1828 req_info->request_id = event->attr_trans.trans_id;
1829 req_info->attribute_handle = event->attr_trans.attr_handle;
1830 req_info->connection_id = event->attr_trans.conn_id;
1831 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_ACQUIRE_NOTIFY;
1832 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1834 param = g_variant_new("(iiiiiis)", result,
1835 event->attr_trans.conn_id,
1836 event->attr_trans.trans_id,
1837 event->attr_trans.attr_handle,
1838 event->mtu, event->attr_trans.offset,
1841 _bt_send_event(BT_GATT_SERVER_EVENT,
1842 BLUETOOTH_EVENT_GATT_SERVER_ACQUIRE_NOTIFY,
1846 static void __bt_handle_gatt_server_write_requested(event_gatts_srvc_write_attr_t *event)
1849 bluetooth_device_address_t dev_addr;
1850 GVariant *param = NULL;
1851 int result = BLUETOOTH_ERROR_NONE;
1853 gboolean is_prepare_write;
1855 GVariant *data = NULL;
1857 struct gatt_server_req_info *req_info = NULL;
1858 BT_INFO("GATT Server Write Requested");
1860 memcpy(dev_addr.addr, event->address.addr, 6);
1862 BT_INFO("GATT Server Write Req Connection ID: [%d]", event->attr_trans.conn_id);
1863 BT_INFO("GATT Server Write Req Transaction ID:[%d]", event->attr_trans.trans_id);
1864 BT_INFO("GATT Server Write Req Attribute Handle: [%d]", event->attr_trans.attr_handle);
1865 BT_INFO("GATT Server Write Req Attribute Offset: [%d]", event->attr_trans.offset);
1866 BT_INFO("GATT Server Write Req value length [%d]", event->length);
1867 BT_INFO("GATT Server Write Req needs response: [%d]", event->need_rsp);
1868 BT_INFO("GATT Server Write Req Is Prep: [%d]", event->is_prep);
1870 if (event->length <= 0) {
1871 BT_INFO("GATT Server write requested, but length of attr value is 0");
1875 need_resp = event->need_rsp;
1876 is_prepare_write = event->is_prep;
1878 write_val = g_memdup(&event->value[0], event->length);
1880 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1881 _bt_convert_addr_type_to_string(address,
1882 (unsigned char *)dev_addr.addr);
1884 BT_INFO("GATT Server Write Request from remote client [%s]", address);
1886 if (event->length > 0) {
1887 for (i = 0; i < event->length; i++)
1888 BT_DBG("Data[%d] = [0x%x]", i, event->value[i]);
1890 /* Save Write Request Info */
1891 req_info = g_new0(struct gatt_server_req_info, 1);
1892 req_info->request_id = event->attr_trans.trans_id;
1893 req_info->attribute_handle = event->attr_trans.attr_handle;
1894 req_info->connection_id = event->attr_trans.conn_id;
1895 req_info->addr = address;
1896 req_info->offset = event->attr_trans.offset;
1897 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_WRITE;
1898 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1900 data = g_variant_new_from_data(
1901 G_VARIANT_TYPE_BYTESTRING,
1906 param = g_variant_new("(iiiiiibbsn@ay)", result,
1907 event->attr_trans.conn_id,
1908 event->attr_trans.trans_id,
1909 event->attr_trans.attr_handle,
1910 event->attr_trans.offset,
1918 _bt_send_event(BT_GATT_SERVER_EVENT,
1919 BLUETOOTH_EVENT_GATT_SERVER_VALUE_CHANGED,
1925 static void __bt_handle_gatt_server_read_requested(event_gatts_srvc_read_attr_t *event)
1927 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1928 bluetooth_device_address_t dev_addr;
1929 int result = BLUETOOTH_ERROR_NONE;
1930 struct gatt_server_req_info *req_info = NULL;
1931 GVariant *param = NULL;
1934 memcpy(dev_addr.addr, event->address.addr, 6);
1935 _bt_convert_addr_type_to_string(address,
1936 (unsigned char *)dev_addr.addr);
1938 BT_DBG("conn_id %d, trans id %d, attr handle %d, offset %d, is_long %d, addr %s",
1939 event->attr_trans.conn_id, event->attr_trans.trans_id,
1940 event->attr_trans.attr_handle, event->attr_trans.offset,
1941 event->is_long, address);
1943 is_long = event->is_long;
1945 /* Save Read Request Info */
1946 req_info = g_new0(struct gatt_server_req_info, 1);
1947 req_info->request_id = event->attr_trans.trans_id;
1948 req_info->attribute_handle = event->attr_trans.attr_handle;
1949 req_info->connection_id = event->attr_trans.conn_id;
1950 req_info->addr = address;
1951 req_info->offset = event->attr_trans.offset;
1952 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_READ;
1953 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1955 /* Send event to BT-API */
1956 param = g_variant_new("(iiiiibs)", result,
1957 event->attr_trans.conn_id,
1958 event->attr_trans.trans_id,
1959 event->attr_trans.attr_handle,
1960 event->attr_trans.offset,
1964 _bt_send_event(BT_GATT_SERVER_EVENT,
1965 BLUETOOTH_EVENT_GATT_SERVER_READ_REQUESTED,
1969 static void __bt_handle_gatt_server_indicate_confirmed(event_gatts_ind_cnfrm_t *event)
1971 bluetooth_device_address_t dev_addr;
1972 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1973 int cur_connected_clients;
1974 static int recvd = 0;
1975 gboolean completed = 0;
1976 GVariant *param = NULL;
1978 /* OAL event does provide error, so MW assumes event will never contain wrong data,
1979 incase of any issues, check with OAL */
1980 int result = BLUETOOTH_ERROR_NONE;
1982 memcpy(dev_addr.addr, event->address.addr, 6);
1983 _bt_convert_addr_type_to_string(address,
1984 (unsigned char *)dev_addr.addr);
1986 BT_INFO("Indication sent to GATT client [%s] conn_ ID [%d] transaction ID [%d] Att handle [%d]",
1987 address, event->conn_id, event->trans_id, event->attr_handle);
1990 cur_connected_clients = g_slist_length(gatt_client_info_list);
1991 BT_INFO("Number of connected clients during sending Indication [%d] & current connected count [%d]",
1992 num_indicate_clients, cur_connected_clients);
1995 if (recvd == num_indicate_clients) {
1996 BT_INFO("Gatt indication confirm event for last GATT client.. [%s]", address);
1997 completed = 1; /* Last event */
1998 recvd = 0; /* Reset */
1999 num_indicate_clients = 0;
2002 param = g_variant_new("(isib)",
2008 /* Send event to BT-API */
2009 _bt_send_event(BT_GATT_SERVER_EVENT,
2010 BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_COMPLETED,
2013 BT_INFO("Received Indication confirm for client number [%d]", recvd);
2017 /* Tizen Platform Specific */
2018 static void __bt_handle_gatt_server_notification_changed(event_gatts_notif_t *event)
2020 bluetooth_device_address_t dev_addr;
2021 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2022 GVariant *param = NULL;
2025 /* OAL event does provide error, so MW assumes event will never contain wrong data,
2026 incase of any issues, check with OAL */
2027 int result = BLUETOOTH_ERROR_NONE;
2029 memcpy(dev_addr.addr, event->address.addr, 6);
2030 _bt_convert_addr_type_to_string(address,
2031 (unsigned char *)dev_addr.addr);
2033 BT_INFO("notification_changed [%s] conn_ ID [%d] transaction ID [%d] Att handle [%d] Notify[%d]",
2034 address, event->conn_id, event->trans_id, event->attr_handle, event->notify);
2036 /* Set Notifcation status */
2037 notify = event->notify;
2039 param = g_variant_new("(isib)",
2045 /* Send event to BT-API */
2046 _bt_send_event(BT_GATT_SERVER_EVENT,
2047 BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_STATE_CHANGED,
2053 static void __bt_handle_gatt_mtu_changed_event(event_gatts_mtu_changed_t *event)
2055 int result = BLUETOOTH_ERROR_NONE;
2056 struct gatt_client_info_t *conn_info = NULL;
2057 GVariant *param = NULL;
2060 conn_info = __bt_find_remote_gatt_client_info_from_conn_id(event->conn_id);
2061 if (conn_info == NULL) {
2062 BT_ERR("Cant find connection Information");
2065 BT_INFO("Got connection Info GATT client [%s] MTU Size [%d]",
2066 conn_info->addr, event->mtu_size);
2068 __bt_update_mtu_gatt_device(conn_info->addr, event->mtu_size);
2070 param = g_variant_new("(isqy)",
2076 /* Send event to BT-API */
2077 _bt_send_event(BT_GATT_SERVER_EVENT,
2078 BLUETOOTH_EVENT_GATT_ATT_MTU_CHANGED,
2082 static void __bt_gatt_event_handler(int event_type, gpointer event_data)
2084 switch (event_type) {
2085 case OAL_EVENT_BLE_SERVER_INSTANCE_INITIALISED: {
2086 BT_INFO("OAL Event: Server Instance Registered");
2087 /* GATT Server Registered event is handled in MAIN thread context */
2088 __bt_handle_server_instance_registered((event_gatts_register_t *)event_data);
2091 case OAL_EVENT_GATTS_SERVICE_ADDED: {
2092 BT_INFO("OAL Event: GATT Service added");
2093 __bt_handle_gatt_server_service_added((event_gatts_srvc_prm_t *)event_data);
2096 case OAL_EVENT_GATTS_CHARACTERISTIC_ADDED: {
2097 BT_INFO("OAL Event: GATT characteristic added");
2098 __bt_handle_gatt_server_characteristic_added((event_gatts_srvc_charctr_t *)event_data);
2101 case OAL_EVENT_GATTS_DESCRIPTOR_ADDED: {
2102 BT_INFO("OAL Event: GATT descriptor added");
2103 __bt_handle_gatt_server_descriptor_added((event_gatts_srvc_descr_t *)event_data);
2106 case OAL_EVENT_GATTS_SERVICE_STARTED: {
2107 BT_INFO("OAL Event: GATT Service started");
2108 __bt_handle_gatt_server_service_started((event_gatts_srvc_t *)event_data);
2111 case OAL_EVENT_GATTS_SERVICE_STOPED: {
2112 BT_INFO("OAL Event: GATT Service stopped");
2113 __bt_handle_gatt_server_service_stopped((event_gatts_srvc_t *)event_data);
2116 case OAL_EVENT_GATTS_SERVICE_DELETED: {
2117 BT_INFO("OAL Event: GATT Service deleted");
2118 __bt_handle_gatt_server_service_deleted((event_gatts_srvc_t *) event_data);
2121 case OAL_EVENT_GATTS_CONNECTION_COMPLETED: {
2122 BT_INFO("OAL Event: GATT Server Connected");
2123 __bt_handle_gatt_server_connection_state((event_gatts_conn_t *)event_data);
2126 case OAL_EVENT_GATTS_DISCONNECTION_COMPLETED: {
2127 BT_INFO("OAL Event: GATT Server Disconnected");
2128 __bt_handle_gatt_server_disconnection_state((event_gatts_conn_t *)event_data);
2131 case OAL_EVENT_GATTS_REQUEST_READ: {
2132 BT_DBG("OAL Event: GATT Server Read Request");
2133 __bt_handle_gatt_server_read_requested((event_gatts_srvc_read_attr_t *)event_data);
2136 case OAL_EVENT_GATTS_REQUEST_WRITE: {
2137 BT_DBG("OAL Event: GATT Server Write Request");
2138 __bt_handle_gatt_server_write_requested((event_gatts_srvc_write_attr_t *)event_data);
2141 case OAL_EVENT_GATTS_REQUEST_ACQUIRE_WRITE: {
2142 BT_INFO("OAL Event: GATT Server Acquire Write Request");
2143 __bt_handle_gatt_server_acquire_write_requested((event_gatts_srvc_acquire_attr_t*)event_data);
2146 case OAL_EVENT_GATTS_REQUEST_ACQUIRE_NOTIFY: {
2147 BT_INFO("OAL Event: GATT ServerAcquire Notify Request");
2148 __bt_handle_gatt_server_acquire_notify_requested((event_gatts_srvc_acquire_attr_t*)event_data);
2151 case OAL_EVENT_GATTS_IND_CONFIRM: {
2152 BT_INFO("OAL Event: GATT Server Indication confirmed");
2153 __bt_handle_gatt_server_indicate_confirmed((event_gatts_ind_cnfrm_t *)event_data);
2156 case OAL_EVENT_GATTS_NOTIFICATION: { /* Tizen Platform Specific */
2157 BT_INFO("OAL Event: GATT Server DisConnected");
2158 __bt_handle_gatt_server_notification_changed((event_gatts_notif_t *)event_data);
2161 case OAL_EVENT_GATTS_MTU_CHANGED: {
2162 BT_INFO("OAL Event: GATT Server MTU changed event callback");
2163 __bt_handle_gatt_mtu_changed_event((event_gatts_mtu_changed_t *)event_data);
2166 #ifdef TIZEN_GATT_CLIENT
2167 case OAL_EVENT_GATTC_REGISTRATION: {
2168 BT_INFO("OAL Event: GATT Client instance Registered");
2169 __bt_handle_client_instance_registered((event_gattc_register_t *) event_data);
2172 case OAL_EVENT_GATTC_CONNECTION_COMPLETED: {
2173 BT_INFO("OAL Event: GATT Client Connected");
2174 __bt_handle_client_connected((event_gattc_conn_t *) event_data);
2177 case OAL_EVENT_GATTC_DISCONNECTION_COMPLETED: {
2178 BT_INFO("OAL Event: GATT Client DisConnected");
2179 __bt_handle_client_disconnected((event_gattc_conn_t *) event_data);
2182 case OAL_EVENT_GATTC_SERVICE_SEARCH_RESULT: {
2183 BT_DBG("OAL Event: GATT Client Service Search Result");
2184 __bt_handle_client_service_search_result((event_gattc_service_result_t *) event_data);
2187 case OAL_EVENT_GATTC_SERVICE_SEARCH_DONE: {
2188 BT_INFO("OAL Event: GATT Client Service Completed");
2189 __bt_handle_client_service_search_completed((event_gattc_conn_status_t *) event_data);
2192 case OAL_EVENT_GATTC_CHARAC_SERACH_RESULT: {
2193 BT_DBG("OAL Event: GATT Client Characteristic Search Result");
2194 __bt_handle_client_characteristic_search_result((event_gattc_characteristic_result_t *) event_data);
2197 case OAL_EVENT_GATTC_DESC_SERACH_RESULT: {
2198 BT_DBG("OAL Event: GATT Client Descriptor Search Result");
2199 __bt_handle_client_descriptor_search_result((event_gattc_descriptor_result_t *) event_data);
2202 case OAL_EVENT_GATTC_READ_CHARAC: {
2203 BT_DBG("OAL Event: GATT Client Characteristic Read Data");
2204 __bt_handle_client_characteristic_read_data((event_gattc_read_data *) event_data);
2207 case OAL_EVENT_GATTC_READ_DESCR: {
2208 BT_DBG("OAL Event: GATT Client Descriptor Read Data");
2209 __bt_handle_client_descriptor_read_data((event_gattc_read_data *) event_data);
2212 case OAL_EVENT_GATTC_WRITE_CHARAC: {
2213 BT_DBG("OAL Event: GATT Client Characteristic Write Data");
2214 __bt_handle_client_characteristic_write_data((event_gattc_write_data *) event_data);
2217 case OAL_EVENT_GATTC_WRITE_DESCR: {
2218 BT_DBG("OAL Event: GATT Client Descriptor Write Data");
2219 __bt_handle_client_descriptor_write_data((event_gattc_write_data *) event_data);
2222 case OAL_EVENT_DEVICE_LE_DISCONNECTED: {
2223 BT_INFO("OAL Event: LE device disconnected");
2224 __bt_hanlde_le_device_disconnection((event_dev_conn_status_t *)event_data);
2227 case OAL_EVENT_GATTC_NOTIFICATION_REGISTERED: {
2228 BT_INFO("OAL Event: GATT Client Notification Registered");
2229 __bt_handle_client_notification_registered((event_gattc_regdereg_notify_t *) event_data, TRUE);
2232 case OAL_EVENT_GATTC_NOTIFICATION_DEREGISTERED: {
2233 BT_INFO("OAL Event: GATT Client Notification Registered");
2234 __bt_handle_client_notification_registered((event_gattc_regdereg_notify_t *) event_data, FALSE);
2237 case OAL_EVENT_GATTC_NOTIFY_DATA: {
2238 BT_DBG("OAL Event: GATT Client Notification Data");
2239 __bt_handle_client_notification_data((event_gattc_notify_data *) event_data);
2242 case OAL_EVENT_GATTC_SERVICE_CHANGED_IND: {
2243 BT_INFO("OAL Event: GATT Client service changed indication");
2244 __bt_handle_client_service_changed_ind((event_gattc_service_changed_data *)event_data);
2247 case OAL_EVENT_GATTC_MTU_EXCHANGE_COMPLETED: {
2248 BT_INFO("OAL Event: GATT Client MTU Exchange Complete");
2249 __bt_handle_client_mtu_exchange_completed((event_gattc_mtu_configured_t *) event_data);
2254 BT_DBG("Unhandled OAL event = 0x%x", event_type);
2259 int _bt_gatt_server_add_service(char *sender, int service_type,
2260 int num_handles, char *svc_uuid, int instance_id)
2262 BT_CHECK_PARAMETER(svc_uuid, return);
2263 BT_CHECK_PARAMETER(sender, return);
2264 int ret = OAL_STATUS_SUCCESS;
2266 oal_gatt_srvc_id_t svc_data;
2268 svc_data.is_prmry = service_type;
2269 svc_data.id.inst_id = instance_id;
2271 BT_INFO("Service UUID [%s] Num handles [%d] Instance ID [%d]", svc_uuid, num_handles, instance_id);
2272 _bt_string_to_uuid(svc_uuid, (service_uuid_t*)&svc_data.id.uuid);
2274 ret = gatts_add_service(instance_id, &svc_data, num_handles);
2275 if (ret != OAL_STATUS_SUCCESS) {
2276 BT_ERR("ret: %d", ret);
2277 return _bt_convert_oal_status_to_bt_error(ret);
2280 return BLUETOOTH_ERROR_NONE;
2284 int _bt_gatt_server_add_included_service(char *sender, int instance_id,
2285 int service_handle, int included_svc_handle)
2287 BT_CHECK_PARAMETER(sender, return);
2288 int ret = OAL_STATUS_SUCCESS;
2290 ret = gatts_add_included_services(instance_id, service_handle, included_svc_handle);
2291 if (ret != OAL_STATUS_SUCCESS) {
2292 BT_ERR("ret: %d", ret);
2293 return _bt_convert_oal_status_to_bt_error(ret);
2295 return BLUETOOTH_ERROR_NONE;
2298 int _bt_gatt_server_add_characteristic(char *sender, char *char_uuid,
2299 bluetooth_gatt_server_attribute_params_t *param)
2301 BT_CHECK_PARAMETER(char_uuid, return);
2302 BT_CHECK_PARAMETER(sender, return);
2303 BT_CHECK_PARAMETER(param, return);
2304 int ret = OAL_STATUS_SUCCESS;
2306 oal_uuid_t uuid = {{0} };
2308 BT_INFO("Char UUID [%s] Instance ID [%d]", char_uuid, param->instance_id);
2309 _bt_string_to_uuid(char_uuid, (service_uuid_t*)&uuid);
2311 BT_INFO("Char permission From API [0x%x]", param->permissions);
2313 ret = gatts_add_characteristics(param->instance_id, param->service_handle, &uuid,
2314 param->properties, (int)param->permissions);
2315 if (ret != OAL_STATUS_SUCCESS) {
2316 BT_ERR("ret: %d", ret);
2317 return _bt_convert_oal_status_to_bt_error(ret);
2319 return BLUETOOTH_ERROR_NONE;
2322 int _bt_gatt_server_add_descriptor(char *sender, char *desc_uuid,
2323 bt_gatt_permission_t *param, int service_handle, int instance_id)
2325 BT_CHECK_PARAMETER(desc_uuid, return);
2326 BT_CHECK_PARAMETER(sender, return);
2327 BT_CHECK_PARAMETER(param, return);
2328 int ret = OAL_STATUS_SUCCESS;
2330 oal_uuid_t uuid = {{0} };
2332 BT_INFO("Descriptor UUID [%s] Instance ID [%d] Service handle [%d]",
2333 desc_uuid, service_handle, instance_id);
2335 _bt_string_to_uuid(desc_uuid, (service_uuid_t*)&uuid);
2337 BT_INFO("Descriptor permission From API [0x%x]", *param);
2338 ret = gatts_add_descriptor(instance_id, service_handle, &uuid, (int)*param);
2340 if (ret != OAL_STATUS_SUCCESS) {
2341 BT_ERR("ret: %d", ret);
2342 return _bt_convert_oal_status_to_bt_error(ret);
2344 return BLUETOOTH_ERROR_NONE;
2347 int _bt_gatt_server_start_service(char *sender, int service_handle, int instance_id)
2349 BT_CHECK_PARAMETER(sender, return);
2350 int ret = OAL_STATUS_SUCCESS;
2352 ret = gatts_start_service(instance_id, service_handle, BT_GATT_TRANSPORT_LE);
2353 if (ret != OAL_STATUS_SUCCESS) {
2354 BT_ERR("ret: %d", ret);
2355 return _bt_convert_oal_status_to_bt_error(ret);
2357 return BLUETOOTH_ERROR_NONE;
2360 int _bt_gatt_server_stop_service(char *sender, int service_handle, int instance_id)
2362 BT_CHECK_PARAMETER(sender, return);
2363 int ret = OAL_STATUS_SUCCESS;
2365 ret = gatts_stop_service(instance_id, service_handle);
2366 if (ret != OAL_STATUS_SUCCESS) {
2367 BT_ERR("ret: %d", ret);
2368 return _bt_convert_oal_status_to_bt_error(ret);
2370 return BLUETOOTH_ERROR_NONE;
2373 int _bt_gatt_server_delete_service(char *sender, int service_handle, int instance_id)
2375 BT_CHECK_PARAMETER(sender, return);
2376 int ret = OAL_STATUS_SUCCESS;
2380 bt_service_app_info_t *info = NULL;
2382 ret = gatts_delete_service(instance_id, service_handle);
2383 if (ret != OAL_STATUS_SUCCESS) {
2384 BT_ERR("ret: %d", ret);
2385 return _bt_convert_oal_status_to_bt_error(ret);
2388 /* Remove the Service Handle */
2389 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
2391 if (info->instance_id == instance_id) {
2392 for (l = info->service_handles; l != NULL; ) {
2394 l = g_slist_next(l);
2395 if (handle && *handle == service_handle) {
2396 BT_INFO("Remove Service handle [%d]", *handle);
2397 info->service_handles = g_slist_remove(info->service_handles, handle);
2406 return BLUETOOTH_ERROR_NONE;
2409 int _bt_gatt_server_send_response(char *sender, bluetooth_gatt_att_data_t *data,
2410 bluetooth_gatt_server_response_params_t *param)
2412 BT_CHECK_PARAMETER(sender, return);
2413 BT_CHECK_PARAMETER(data, return);
2414 BT_CHECK_PARAMETER(param, return);
2415 struct gatt_server_req_info *req_info = NULL;
2416 int ret = OAL_STATUS_SUCCESS;
2418 oal_gatt_response_t response;
2420 BT_DBG("GATT Server Response: Req Type [%d] req_id [%d] status [%d] auth_req [%d] offset[%d] data len[%d]",
2421 param->req_type, param->request_id,
2422 param->response_status, param->auth_req,
2423 data->offset, data->length);
2425 /* Search for matching Request in List */
2426 req_info = __bt_gatt_server_find_request_info(param->request_id, param->req_type);
2428 BT_ERR("GATT Server Req Info not found for current response..return Error");
2429 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
2432 memset(&response, 0x00, sizeof(oal_gatt_response_t));
2434 response.handle = req_info->attribute_handle;
2435 response.attr_value.auth_req = param->auth_req;
2436 response.attr_value.handle = req_info->attribute_handle;
2437 response.attr_value.offset = data->offset;
2438 response.attr_value.len = data->length;
2439 memcpy(&response.attr_value.value, &data->data, data->length);
2442 ret = gatts_send_response(req_info->connection_id, param->request_id,
2443 param->response_status, &response);
2445 if (ret != OAL_STATUS_SUCCESS) {
2446 BT_ERR("ret: %d", ret);
2447 return _bt_convert_oal_status_to_bt_error(ret);
2450 /* Remove GATT server request from list */
2451 gatt_server_requests = g_slist_remove(gatt_server_requests, req_info);
2452 g_free(req_info->addr);
2454 return BLUETOOTH_ERROR_NONE;
2457 int _bt_gatt_server_acquire_send_response(char *sender, bluetooth_gatt_server_acquire_response_params_t *param , void *fd_list)
2459 BT_CHECK_PARAMETER(sender, return);
2460 BT_CHECK_PARAMETER(param, return);
2461 struct gatt_server_req_info *req_info = NULL;
2462 int ret = OAL_STATUS_SUCCESS;
2465 BT_INFO("GATT acquire Server Response: Req Type [%d] req_id [%d] fd [%d] mtu[%d]",
2466 param->req_type, param->request_id,
2470 /* Search for matching Request in List */
2471 req_info = __bt_gatt_server_find_request_info(param->request_id, param->req_type);
2473 BT_ERR("GATT acquire Server Req Info not found for current response..return Error");
2474 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
2477 ret = gatt_send_response_acquire(req_info->connection_id, param->request_id, 0, param->fd, param->mtu, fd_list);
2479 if (ret != OAL_STATUS_SUCCESS) {
2480 BT_ERR("ret: %d", ret);
2481 return _bt_convert_oal_status_to_bt_error(ret);
2484 /* Remove GATT server request from list */
2485 gatt_server_requests = g_slist_remove(gatt_server_requests, req_info);
2486 g_free(req_info->addr);
2488 return BLUETOOTH_ERROR_NONE;
2493 int _bt_gatt_server_send_indication(char *sender, bluetooth_device_address_t *dev_addr,
2494 bluetooth_gatt_att_data_t *data,
2495 bluetooth_gatt_server_indication_params_t *param)
2497 BT_CHECK_PARAMETER(sender, return);
2498 BT_CHECK_PARAMETER(data, return);
2499 BT_CHECK_PARAMETER(param, return);
2501 gboolean all_send = FALSE;
2502 int ret = OAL_STATUS_SUCCESS;
2503 struct gatt_client_info_t *conn;
2505 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2506 _bt_convert_addr_type_to_string(address, dev_addr->addr);
2508 if (memcmp(dev_addr->addr, BDADDR_ANY, 6) == 0) {
2509 BT_INFO("GATT Server: Send Indication to all connected GATT clients..");
2512 BT_INFO("GATT Server: Send Indication to connected GATT client addr [%s]", address);
2515 /* Attempt to send Notification/Indication to all Connected GATT clients */
2517 ret = __bt_gatt_send_indication_to_all_connected_clients(data, param);
2518 if (ret != OAL_STATUS_SUCCESS) {
2519 BT_ERR("ret: %d", ret);
2521 return _bt_convert_oal_status_to_bt_error(ret);
2525 conn = _bt_find_remote_gatt_client_info(address);
2527 ret = gatts_send_indication(param->instance_id, param->atrribute_handle,
2528 conn->connection_id, data->length,
2529 param->need_confirmation, (char *)(&data->data[0]));
2531 if (ret != OAL_STATUS_SUCCESS) {
2532 BT_ERR("ret: %d", ret);
2533 BT_INFO("Indication failed to send to Remote GATT Client [%s]", address);
2535 return _bt_convert_oal_status_to_bt_error(ret);
2537 BT_INFO("Indication sent to Remote GATT Client [%s] wait for Notification completed event from OAL", address);
2539 num_indicate_clients = 1;
2540 return BLUETOOTH_ERROR_NONE;
2542 BT_ERR("Remote GATT client [%s] is not connected..Cant send Indication!!", address);
2544 return BLUETOOTH_ERROR_NOT_CONNECTED;
2547 return BLUETOOTH_ERROR_NONE;
2550 int _bt_gatt_server_update_attribute_value(char *sender, int instance_id,
2551 bluetooth_gatt_server_update_value_t *param)
2553 BT_CHECK_PARAMETER(sender, return);
2554 BT_CHECK_PARAMETER(param, return);
2555 int ret = OAL_STATUS_SUCCESS;
2557 oal_gatt_value_t value;
2558 BT_DBG("GATT Server Update value: Instance ID [%d] attr handle [%d] Value len [%d]",
2559 instance_id, param->attribute_handle, param->length);
2561 memset(&value, 0x00, sizeof(oal_gatt_value_t));
2563 value.handle = param->attribute_handle;
2564 value.len = param->length;
2565 memcpy(&value.value, ¶m->data.data, param->length);
2567 ret = gatts_update_att_value(instance_id, &value);
2569 if (ret != OAL_STATUS_SUCCESS) {
2570 BT_ERR("ret: %d", ret);
2571 return _bt_convert_oal_status_to_bt_error(ret);
2574 return BLUETOOTH_ERROR_NONE;
2577 int _bt_request_att_mtu(bluetooth_device_address_t *device_address,
2580 struct gatt_server_info_t *conn_info = NULL;
2582 int ret = OAL_STATUS_SUCCESS;
2584 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
2585 _bt_convert_addr_type_to_string(addr, device_address->addr);
2587 /* Check if remote GATT Server is connected or not */
2588 conn_info = _bt_find_remote_gatt_server_info(addr);
2589 if (conn_info == NULL) {
2590 BT_ERR("GATT Server is not yet connected..");
2592 return BLUETOOTH_ERROR_NOT_CONNECTED;
2595 ret = gattc_configure_mtu(conn_info->connection_id, mtu);
2596 if (ret != OAL_STATUS_SUCCESS) {
2597 BT_ERR("ret: %d", ret);
2599 return _bt_convert_oal_status_to_bt_error(ret);
2603 return BLUETOOTH_ERROR_NONE;
2606 int _bt_get_att_mtu(bluetooth_device_address_t *address,
2609 BT_CHECK_PARAMETER(address, return);
2610 BT_CHECK_PARAMETER(mtu, return);
2611 struct gatt_client_info_t *conn_info = NULL;
2612 char addr[BT_ADDRESS_STRING_SIZE] = { 0 };
2613 int ret = OAL_STATUS_SUCCESS;
2616 _bt_convert_addr_type_to_string(addr, address->addr);
2618 BT_INFO("Get current MTU size for the remote client:DevAddress:[%s]", addr);
2620 conn_info = _bt_find_remote_gatt_client_info(addr);
2622 BT_INFO("GATT Client [%s] is connected, conn Id [%d] Instance ID [%d]",
2623 conn_info->addr, conn_info->connection_id, conn_info->instance_id);
2625 BT_ERR("GATT Client [%s] is not yet connected..", addr);
2626 return BLUETOOTH_ERROR_NOT_CONNECTED;
2629 if (conn_info->instance_id == -1) {
2630 ret = gattc_get_att_mtu(conn_info->connection_id, &stack_mtu);
2631 if (ret != OAL_STATUS_SUCCESS) {
2632 BT_ERR("ret: %d", ret);
2633 return _bt_convert_oal_status_to_bt_error(ret);
2636 ret = gatts_get_att_mtu(conn_info->connection_id, &stack_mtu);
2637 if (ret != OAL_STATUS_SUCCESS) {
2638 BT_ERR("ret: %d", ret);
2639 return _bt_convert_oal_status_to_bt_error(ret);
2643 BT_INFO("ATT MTU received from OAL [%d]", stack_mtu);
2644 *mtu = (unsigned int)stack_mtu;
2647 BT_ERR("MTU value is zero, GATT Client [%s] is not yet connected..", addr);
2648 return BLUETOOTH_ERROR_NOT_CONNECTED;
2651 return BLUETOOTH_ERROR_NONE;
2654 #ifdef TIZEN_GATT_CLIENT
2655 /* GATT Client utility static functions */
2656 static bt_gatt_service_info_list_t *__bt_get_service_info_list(int conn_id)
2659 bt_gatt_service_info_list_t *info = NULL;
2661 for (l = list_gatt_info; l != NULL; l = g_slist_next(l)) {
2662 info = (bt_gatt_service_info_list_t *)l->data;
2666 if (info->conn_id == conn_id)
2673 static bt_gatt_service_info_t *__bt_find_matching_service(
2674 bt_gatt_service_info_list_t *svc_list, oal_gatt_srvc_id_t *svc)
2677 bt_gatt_service_info_t *info = NULL;
2679 for (l = svc_list->services; l != NULL; l = g_slist_next(l)) {
2680 info = (bt_gatt_service_info_t *)l->data;
2684 /* Match UUID and instance ID */
2685 if (!memcmp(&svc->id.uuid.uuid, &info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
2686 && (svc->id.inst_id == info->inst_id)) {
2693 static bt_gatt_char_info_t *__bt_find_matching_charc(
2694 bt_gatt_service_info_t *svc_info, oal_gatt_id_t *charc)
2697 bt_gatt_char_info_t *info = NULL;
2699 for (l = svc_info->chars; l != NULL; l = g_slist_next(l)) {
2700 info = (bt_gatt_char_info_t *)l->data;
2704 /* Match UUID and instance ID */
2705 if (!memcmp(&charc->uuid.uuid, &info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
2706 && (charc->inst_id == info->inst_id)) {
2713 static bt_gatt_descriptor_info_t *__bt_find_matching_desc(
2714 bt_gatt_char_info_t *char_info, oal_gatt_id_t *desc)
2717 bt_gatt_descriptor_info_t *info = NULL;
2719 for (l = char_info->descs; l != NULL; l = g_slist_next(l)) {
2720 info = (bt_gatt_descriptor_info_t *)l->data;
2724 /* Match UUID and instance ID */
2725 if (!memcmp(&desc->uuid, &info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
2726 && (desc->inst_id == info->inst_id)) {
2734 static struct gatt_server_info_t *__bt_find_remote_gatt_server_info_from_conn_id(int conn_id)
2737 struct gatt_server_info_t *info = NULL;
2739 for (l = gatt_server_info_list; l != NULL; l = g_slist_next(l)) {
2740 info = (struct gatt_server_info_t*)l->data;
2744 if (info->connection_id == conn_id)
2750 static bt_gatt_service_info_t* __bt_find_removed_service(bt_gatt_service_info_list_t *svc_list)
2753 bt_gatt_service_info_t *info = NULL;
2755 for (l = svc_list->services; l != NULL; l = g_slist_next(l)) {
2756 info = (bt_gatt_service_info_t*)l->data;
2760 /* Service is marked a removed */
2761 if (info->is_removed == 1)
2767 static void __bt_remove_service_info_from_list(bt_gatt_service_info_t *svc_info)
2773 bt_gatt_char_info_t *charc = NULL;
2774 bt_gatt_included_service_info_t *incl = NULL;
2775 bt_gatt_descriptor_info_t *desc = NULL;
2777 /* Remove all Characteristic and Descriptors within characteristic */
2778 for (l = svc_info->chars; l != NULL;) {
2779 charc = (bt_gatt_char_info_t*)l->data;
2780 l = g_slist_next(l); /* Incase if l is removed, saving next to l */
2785 /* Inside Characteristic */
2786 for (l1 = charc->descs; l1 != NULL;) {
2788 desc = (bt_gatt_descriptor_info_t*)l1->data;
2789 l1 = g_slist_next(l1);
2794 /* Remove Descriptor */
2795 charc->descs = g_slist_remove(charc->descs, desc);
2798 /* Remove Characteristic */
2799 svc_info->chars = g_slist_remove(svc_info->chars, charc);
2803 /* Remove all Included Services */
2804 for (l2 = svc_info->included_svcs; l2 != NULL;) {
2805 incl = (bt_gatt_included_service_info_t*)l2->data;
2806 l2 = g_slist_next(l2); /* Incase if l is removed, saving next to l */
2811 /* Remove included service */
2812 svc_info->included_svcs = g_slist_remove(svc_info->included_svcs, incl);
2819 static void __bt_build_service_browse_info(int conn_id,
2820 bt_services_browse_info_t* info)
2823 bt_gatt_service_info_list_t *svc_info_list;
2824 bt_gatt_service_info_t *svc_info;
2826 service_uuid_t uuid;
2827 struct gatt_server_info_t *conn_info = NULL;
2829 char uuid_string[BLUETOOTH_UUID_STRING_MAX];
2831 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
2832 if (conn_info == NULL) {
2833 BT_ERR("Cant find connection Information");
2837 _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
2839 svc_info_list = __bt_get_service_info_list(conn_id);
2843 info->count = g_slist_length(svc_info_list->services);
2844 BT_DBG("Total services present in the svc info list for this conn id [%d] is [%d]",
2845 conn_id, info->count);
2847 for (l = svc_info_list->services; l != NULL; l = g_slist_next(l)) {
2848 svc_info = (bt_gatt_service_info_t*)l->data;
2849 if (svc_info == NULL)
2852 memcpy(&uuid.uuid, &svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2853 _bt_uuid_to_string(&uuid, uuid_string);
2855 BT_INFO("[%d] %s [%s]", count, uuid_string, _bt_convert_uuid_to_string(uuid_string));
2857 /* Fill UUID of service */
2858 g_strlcpy(info->uuids[count], uuid_string,
2859 BLUETOOTH_UUID_STRING_MAX);
2861 /* Fill instance ID of service */
2862 info->inst_id[count] = svc_info->inst_id;
2864 /* Fill primary service or not info */
2865 info->primary[count] = svc_info->is_primary;
2867 /* Increment count of services browsed */
2872 static void __bt_build_char_browse_info(int conn_id,
2873 bt_gatt_service_info_t *svc_info,
2874 bt_char_browse_info_t* info)
2877 bt_gatt_char_info_t *char_info;
2878 service_uuid_t uuid;
2880 struct gatt_server_info_t *conn_info = NULL;
2882 char uuid_string[BLUETOOTH_UUID_STRING_MAX];
2884 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
2885 if (conn_info == NULL) {
2886 BT_ERR("Cant find connection Information");
2890 /* Fill default data, this will be required even in case of failure */
2891 _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
2892 memcpy(&info->svc_uuid, svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2893 info->svc_inst_id = svc_info->inst_id;
2895 if (!svc_info->chars) {
2896 BT_ERR("No Chars browsed for address [%s]", conn_info->addr);
2900 info->count = g_slist_length(svc_info->chars);
2902 for (l = svc_info->chars; l != NULL; l = g_slist_next(l)) {
2903 char_info = (bt_gatt_char_info_t*)l->data;
2904 if (char_info == NULL)
2907 memcpy(&uuid.uuid, &char_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2908 _bt_uuid_to_string(&uuid, uuid_string);
2910 /* Fill UUID of characteristic */
2911 g_strlcpy(info->uuids[count], uuid_string,
2912 BLUETOOTH_UUID_STRING_MAX);
2914 /* Fill instance ID of characteristic */
2915 info->inst_id[count] = char_info->inst_id;
2917 /* Fill property of characteristic */
2918 info->props[count] = char_info->props;
2920 /* Increment count of services browsed */
2923 BT_DBG("Total characteristics browsed [%d]", count);
2926 static void __bt_build_descriptor_browse_info(int conn_id,
2927 bt_gatt_service_info_t *svc_info,
2928 bt_gatt_char_info_t *char_info,
2929 bt_descriptor_browse_info_t* info)
2932 bt_gatt_descriptor_info_t *desc_info;
2934 service_uuid_t uuid;
2935 struct gatt_server_info_t *conn_info = NULL;
2937 char uuid_string[BLUETOOTH_UUID_STRING_MAX];
2939 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
2941 /* Fill default data, this will be required even in case of failure */
2942 _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
2943 memcpy(&info->svc_uuid, svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2944 info->svc_inst_id = svc_info->inst_id;
2945 memcpy(&info->char_uuid, char_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2946 info->char_inst_id = char_info->inst_id;
2948 /* Fill property of the parent characteristic of this descriptor */
2949 info->char_props_map = char_info->props;
2951 info->count = g_slist_length(char_info->descs);
2953 if (!char_info->descs) {
2954 BT_ERR("No Descriptors browsed for address [%s]", conn_info->addr + 12);
2958 for (l = char_info->descs; l != NULL; l = g_slist_next(l)) {
2959 desc_info = (bt_gatt_descriptor_info_t*)l->data;
2960 if (desc_info == NULL)
2963 memcpy(&uuid.uuid, &desc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2964 _bt_uuid_to_string(&uuid, uuid_string);
2966 /* Fill UUID of Descriptor */
2967 g_strlcpy(info->uuids[count], uuid_string,
2968 BLUETOOTH_UUID_STRING_MAX);
2970 /* Fill instance ID of Descriptor */
2971 info->inst_id[count] = desc_info->inst_id;
2974 /* Increment count of Descriptor browsed */
2978 BT_INFO("Total descriptors browsed [%d]", count);
2981 static void __bt_free_service_info(bt_gatt_service_info_t *svc)
2983 GSList *ll, *lll, *llll;
2984 bt_gatt_char_info_t *chr = NULL;
2985 bt_gatt_descriptor_info_t *desc = NULL;
2986 bt_gatt_included_service_info_t *incl_svc = NULL;
2988 BT_DBG("Service info Is Prim[%d] Inst ID [%d]", svc->is_primary, svc->inst_id);
2989 /* Delete all chars and its descriptors */
2990 for (ll = svc->chars; ll != NULL; ) {
2991 chr = (bt_gatt_char_info_t *)ll->data;
2992 ll = g_slist_next(ll);
2996 for (lll = chr->descs; lll != NULL; ) {
2997 desc = (bt_gatt_descriptor_info_t *)lll->data;
2998 lll = g_slist_next(lll);
3001 chr->descs = g_slist_remove(chr->descs, desc);
3004 svc->chars = g_slist_remove(svc->chars, chr);
3008 /* Delete all included services */
3009 for (llll = svc->included_svcs; llll != NULL; ) {
3010 incl_svc = (bt_gatt_included_service_info_t *)llll->data;
3011 llll = g_slist_next(llll);
3012 if (incl_svc == NULL)
3014 svc->included_svcs = g_slist_remove(svc->included_svcs, incl_svc);
3019 static void __bt_cleanup_remote_services(struct gatt_server_info_t *conn_info)
3021 bt_gatt_service_info_list_t *svc_info_list = NULL;
3022 bt_gatt_service_info_t *svc = NULL;
3026 BT_ERR("conn_info is NULL");
3030 svc_info_list = __bt_get_service_info_list(conn_info->connection_id);
3031 if (!svc_info_list) {
3032 BT_INFO("Could not find Svc Info list for the connection ID [%d]",
3033 conn_info->connection_id);
3037 BT_INFO("Start Cleanup of all services. Num Services [%d]", g_slist_length(svc_info_list->services));
3038 for (l = svc_info_list->services; l != NULL; ) {
3039 svc = (bt_gatt_service_info_t *)l->data;
3040 l = g_slist_next(l);
3044 __bt_free_service_info(svc);
3045 svc_info_list->services = g_slist_remove(svc_info_list->services, svc);
3049 list_gatt_info = g_slist_remove(list_gatt_info, svc_info_list);
3050 g_free(svc_info_list);
3053 int _bt_register_gatt_client_instance(const char *sender,
3054 bluetooth_device_address_t *address)
3056 int ret = OAL_STATUS_SUCCESS;
3057 char *uuid_string = NULL;
3062 /* App should ensure that it should not send */
3063 BT_INFO("Check on which instance GATT Client instance can be initialized....");
3064 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
3065 if (numapps[k].is_initialized == 1) {
3066 BT_DBG("Instance ID [%d] is already in use..Check next slot",
3067 numapps[k].instance_id);
3070 BT_DBG("Time to register GATT client instancer..UUID to be used is [%s] slot [%d]",
3071 uuid_list[slot-1], slot);
3077 BT_ERR("No Slot if free for GATT Client registration..");
3078 return BLUETOOTH_ERROR_REGISTRATION_FAILED;
3081 uuid_string = g_malloc0(BT_UUID_STRING_MAX);
3082 _bt_string_to_uuid(uuid_list[slot-1], (service_uuid_t*)&uuid);
3083 g_strlcpy(uuid_string, uuid_list[slot-1], BT_UUID_STRING_MAX);
3084 BT_INFO("Copied UUID string [%s] slot [%d]", uuid_string, slot);
3086 /* Register GATT Client */
3087 ret = gattc_register(&uuid);
3088 if (ret != OAL_STATUS_SUCCESS) {
3089 BT_ERR("ret: %d", ret);
3090 g_free(uuid_string);
3091 return _bt_convert_oal_status_to_bt_error(ret);
3094 BT_DBG("GATT Client registration call successfully accepted by OAL..wait for Instance Initialized event from OAL..");
3096 /* Return & wait for GATT Client Instance Initialization event */
3097 memset(numapps[slot].sender, 0x00, sizeof(numapps[slot].sender));
3098 memset(numapps[slot].uuid, 0x00, sizeof(numapps[slot].uuid));
3100 g_strlcpy(numapps[slot].sender, sender, sizeof(numapps[slot].sender));
3101 g_strlcpy(numapps[slot].uuid, uuid_string, sizeof(numapps[slot].uuid));
3103 /* Address is saved here. When event comes, sender + address are matched for replying pending
3104 request. It is impossible for same sender to have requests with two same addresses */
3105 memcpy(&numapps[slot].address.addr, address->addr, sizeof(bluetooth_device_address_t));
3107 numapps[slot].is_initialized = TRUE; /* Set initialization to true here itself */
3109 g_free(uuid_string);
3110 return BLUETOOTH_ERROR_NONE;
3116 /* GATT client events */
3117 static void __bt_handle_client_instance_registered(event_gattc_register_t *data)
3119 bt_service_app_info_t *info = NULL;
3121 char *uuid_string = g_malloc0(BT_UUID_STRING_MAX);
3123 _bt_uuid_to_string(&(data->client_uuid), uuid_string);
3124 BT_INFO("Client ID is Initialized [%d] UUID initialized [%s]", data->client_if, uuid_string);
3126 /* Platform GATT client framwork does not use Default GATT client instance
3127 This GATT client instance is never deregistred in the lifetime of bt-service */
3128 if (g_strcmp0(uuid_string, DEFAULT_GATT_CLIENT_UUID) == 0) {
3129 BT_INFO("Default client Instance Registered");
3130 gatt_default_client = data->client_if;
3131 g_free(uuid_string);
3135 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
3138 if (g_strcmp0(info->uuid, uuid_string) == 0) {
3139 BT_INFO("Found GATT client.. sender [%s] Slot [%d] occupied", info->sender, k);
3140 info->is_initialized = TRUE;
3141 info->client_id = data->client_if;
3142 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE, BT_GATT_CLIENT_REGISTER,
3143 (void*)info, sizeof(bt_service_app_info_t));
3147 g_free(uuid_string);
3150 static void __bt_handle_client_connected(event_gattc_conn_t *event_data)
3152 int result = BLUETOOTH_ERROR_NONE;
3153 struct gatt_server_info_t *conn_info = NULL;
3154 struct gatt_out_conn_info_t *out_conn_info = NULL;
3156 GVariant *param = NULL;
3158 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
3159 _bt_convert_addr_type_to_string(address,
3160 (unsigned char *)event_data->address.addr);
3162 if (event_data->status != OAL_STATUS_SUCCESS)
3163 result = BLUETOOTH_ERROR_INTERNAL;
3165 /* DBUS Return fo BT_CONNECT_LE for all the apps */
3166 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE, address,
3167 BT_ADDRESS_STRING_SIZE);
3169 BT_INFO("Local GATT Client Connected: Remote addr[%s] Client Interface [%d] Connection ID [%d] status[%d]",
3170 address, event_data->client_if, event_data->conn_id, event_data->status);
3172 if (result == BLUETOOTH_ERROR_NONE) {
3173 /* Check if device is already in connected list */
3174 conn_info = _bt_find_remote_gatt_server_info(address);
3177 /* Send event to BT-API */
3178 param = g_variant_new("(is)", result, address);
3179 _bt_send_event(BT_DEVICE_EVENT,
3180 BLUETOOTH_EVENT_GATT_CLIENT_CONNECTED, /* Local device is GATT client */
3183 /* Save Connection info */
3184 conn_info = g_new0(struct gatt_server_info_t, 1);
3185 conn_info->addr = g_strdup(address);
3186 conn_info->client_id = event_data->client_if;
3187 #ifdef __INTEGRATE_GATT_INFO__
3188 conn_info->instance_id = -1;
3190 conn_info->connection_id = event_data->conn_id;
3191 gatt_server_info_list = g_slist_append(gatt_server_info_list, conn_info);
3192 BT_DBG("Total num of connected Remote GATT server devices [%d]",
3193 g_slist_length(gatt_server_info_list));
3196 BT_INFO("Do a Internal refresh");
3197 if (OAL_STATUS_SUCCESS != gattc_refresh(conn_info->client_id, &event_data->address))
3198 BT_ERR("GATT database refresh failed!!");
3200 BT_INFO("GATT database refresh Success!!");
3203 BT_ERR("Local GATT Client connected event for addr[%s], but device is in connected list already", address);
3205 __bt_add_mtu_gatt_device(address);
3207 BT_ERR("GATT Client Connection failed!!");
3209 /* If outgoing connection Info is present, then remove it */
3210 out_conn_info = __bt_find_gatt_outgoing_conn_info(address);
3211 if (out_conn_info) {
3212 BT_ERR("Outgoing Client connect request was sent");
3213 outgoing_gatt_conn_list = g_slist_remove(outgoing_gatt_conn_list, out_conn_info);
3214 g_free(out_conn_info->addr);
3215 g_free(out_conn_info);
3220 static void __bt_handle_client_disconnected(event_gattc_conn_t *event_data)
3222 int result = BLUETOOTH_ERROR_NONE;
3224 struct gatt_server_info_t *conn_info = NULL;
3225 struct gatt_out_conn_info_t *out_conn_info = NULL;
3227 GVariant *param = NULL;
3229 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
3230 _bt_convert_addr_type_to_string(address,
3231 (unsigned char *)event_data->address.addr);
3233 if (event_data->status != OAL_STATUS_SUCCESS)
3234 result = BLUETOOTH_ERROR_INTERNAL;
3236 if (NULL == _bt_get_request_info_data(BT_DISCONNECT_LE, address)) {
3237 if (NULL != _bt_get_request_info_data(BT_CONNECT_LE, address)) {
3238 result = BLUETOOTH_ERROR_INTERNAL;
3239 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
3240 address, BT_ADDRESS_STRING_SIZE);
3241 BT_ERR("Failed to connect Local GATT Remote addr[%s]", address);
3246 /* DBUS Return for BT_DISCONNECT_LE for all the apps */
3247 __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE, address,
3248 BT_ADDRESS_STRING_SIZE);
3250 BT_INFO("Local GATT Client DisConnected: Remote addr[%s] Client Interface [%d] Connection ID [%d] status [%d]",
3251 address + 12, event_data->client_if, event_data->conn_id, event_data->status);
3253 /* Remove Connection info */
3254 conn_info = _bt_find_remote_gatt_server_info(address);
3257 param = g_variant_new("(is)", result, address);
3258 /* Send event to application */
3259 _bt_send_event(BT_DEVICE_EVENT,
3260 BLUETOOTH_EVENT_GATT_CLIENT_DISCONNECTED,
3263 BT_INFO("Remove GATT server info from List..");
3264 /* Remove all services from info list_gatt_info */
3265 __bt_cleanup_remote_services(conn_info);
3267 /* Remove info from List */
3268 gatt_server_info_list = g_slist_remove(gatt_server_info_list, conn_info);
3269 BT_INFO("Total num of connected GATT servers [%d]", g_slist_length(gatt_server_info_list));
3270 g_free(conn_info->addr);
3273 BT_INFO("Can not find conn info, already removed!");
3275 __bt_remove_mtu_gatt_device(address);
3277 /* If outgoing connection Info is present, then remove it */
3278 out_conn_info = __bt_find_gatt_outgoing_conn_info(address);
3279 if (out_conn_info) {
3280 BT_ERR("Client Disconnected event, but outgoing connect request was sent");
3281 outgoing_gatt_conn_list = g_slist_remove(outgoing_gatt_conn_list, out_conn_info);
3282 g_free(out_conn_info->addr);
3283 g_free(out_conn_info);
3289 static void __bt_handle_client_service_search_result(
3290 event_gattc_service_result_t *event_data)
3292 /* Pre: status is never fail from OAL */
3294 /* Find service list from address */
3295 bt_gatt_service_info_list_t *svc_info_list;
3296 bt_gatt_service_info_t *svc_info;
3298 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3299 if (!svc_info_list) {
3300 BT_DBG("Service info list not present for connection ID %d, means first time browse", event_data->conn_status.conn_id);
3301 /* Means for this conn_id, no services are ever browsed, first time,
3302 create service info list for this conn_id */
3303 svc_info_list = g_malloc0(sizeof(bt_gatt_service_info_list_t));
3304 svc_info_list->conn_id = event_data->conn_status.conn_id;
3305 list_gatt_info = g_slist_append(list_gatt_info, svc_info_list);
3308 /* send list and current service's uuid and instance id to find it */
3309 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3310 /* If not found, check if service changed, if yes, means this is a new service added
3311 in remote GATT device, update uuid info in svc info list structure, to be used when
3312 search is completed */
3314 if (svc_info_list->info.is_changed) {
3315 BT_DBG("Service Changed indication already found for connection ID %d", event_data->conn_status.conn_id);
3316 memcpy(svc_info_list->info.uuid, event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3318 /* Create and add new service in service list */
3319 svc_info = g_malloc0(sizeof(bt_gatt_service_info_t));
3320 memcpy(svc_info->uuid, event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3321 svc_info->inst_id = event_data->srvc_id.id.inst_id;
3322 svc_info->is_primary = event_data->srvc_id.is_prmry;
3323 svc_info_list->services = g_slist_append(svc_info_list->services, svc_info);
3324 BT_DBG("Service created and added in Svc info list or connection ID %d", event_data->conn_status.conn_id);
3326 /* If returned matching service info, then just update service_rmeoved value inside it to 0 */
3327 svc_info->is_removed = 0;
3331 static void __bt_handle_client_service_search_completed(
3332 event_gattc_conn_status_t *event_data)
3334 struct gatt_server_info_t *conn_info = NULL;
3335 bt_gatt_service_info_list_t *svc_info_list;
3336 bt_gatt_service_info_t *svc_info;
3337 bt_services_browse_info_t browse_info;
3338 unsigned char uuid_empty[BLUETOOTH_UUID_HEX_MAX_LEN];
3340 memset(&uuid_empty, 0x00, BLUETOOTH_UUID_HEX_MAX_LEN);
3341 memset(&browse_info, 0x00, sizeof(bt_services_browse_info_t));
3342 BT_INFO("Primary Services browsing completed status[%d] conn ID [%d]",
3343 event_data->status, event_data->conn_id);
3345 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(event_data->conn_id);
3347 svc_info_list = __bt_get_service_info_list(event_data->conn_id);
3348 if (!svc_info_list) {
3349 BT_ERR("No services browsed ever for addr [%s]", conn_info->addr);
3351 /* Just build response and return ERROR */
3352 __bt_build_service_browse_info(event_data->conn_id, &browse_info);
3354 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_INTERNAL,
3355 BT_GATT_GET_PRIMARY_SERVICES, &browse_info,
3356 sizeof(bt_services_browse_info_t));
3360 /* If fail, then send event with error */
3361 if (event_data->status != OAL_STATUS_SUCCESS) {
3362 /* Just build response and return ERROR */
3363 __bt_build_service_browse_info(event_data->conn_id, &browse_info);
3365 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_INTERNAL,
3366 BT_GATT_GET_PRIMARY_SERVICES, &browse_info,
3367 sizeof(bt_services_browse_info_t));
3371 /* If success, then find service info list from address */
3373 /* If svc_changed == 1 and uuid valid, means a new service is added*/
3374 if (svc_info_list->info.is_changed && !memcmp(uuid_empty, svc_info_list->info.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)) {
3375 /* TODO: Send event -Service added with instance ID and UUID of newly added service */
3376 BT_INFO("new service added");
3378 BT_INFO("TODO new service added");
3381 /* If svc_changed == 1 and uuid invalid, then a service is removed */
3382 if (svc_info_list->info.is_changed && memcmp(uuid_empty, svc_info_list->info.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)) {
3383 /* Scan through the service info list to find service with is_removed = 1*/
3384 svc_info = __bt_find_removed_service(svc_info_list);
3386 /* TODO Send event - Service removed with instance ID and UUID of just rmeoved service */
3388 /* Remove that service info from service info list */
3389 svc_info_list->services = g_slist_remove(svc_info_list->services, svc_info);
3391 /* Delete that service completely from svc_info list*/
3392 __bt_remove_service_info_from_list(svc_info);
3396 /* Reset svc_changed = 0, and reset UUID = all 0's */
3397 svc_info_list->info.is_changed = 0;
3398 memset(&svc_info_list->info.uuid, 0x00, BLUETOOTH_UUID_HEX_MAX_LEN);
3400 /* Build Reply and send to service browse primary services request of pending apps */
3401 __bt_build_service_browse_info(event_data->conn_id, &browse_info);
3403 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE,
3404 BT_GATT_GET_PRIMARY_SERVICES, &browse_info,
3405 sizeof(bt_services_browse_info_t));
3409 static void __bt_handle_client_characteristic_search_result(
3410 event_gattc_characteristic_result_t *event_data)
3412 bt_gatt_service_info_list_t *svc_info_list;
3413 bt_gatt_service_info_t *svc_info;
3414 bt_gatt_char_info_t *char_info;
3415 bt_char_browse_info_t browse_info;
3417 memset(&browse_info, 0x00, sizeof(bt_char_browse_info_t));
3420 if (event_data->conn_status.status == OAL_STATUS_SUCCESS) {
3421 /* Find service info list from address */
3422 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3423 if (svc_info_list == NULL) {
3424 BT_ERR("svc_info_list is NULL");
3428 /* Find matching service info from svc info list */
3429 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3430 if (svc_info == NULL) {
3431 BT_ERR("svc_info is NULL");
3435 /* Find Matching char from service info in event */
3436 char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
3437 /* If not found, then add new characteristic and return */
3439 BT_DBG("Add new characteristic");
3440 char_info = g_malloc0(sizeof(bt_gatt_char_info_t));
3441 memcpy(char_info->uuid, event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3443 char_info->inst_id = event_data->char_id.inst_id;
3444 char_info->props = event_data->char_prop;
3445 svc_info->chars = g_slist_append(svc_info->chars, char_info);
3447 /* If found, then return */
3448 BT_DBG("update char property as Characteristic browsed is already present");
3449 char_info->props |= event_data->char_prop;
3452 /* If Not success: Means Charc browse is completed */
3453 /* Find char list from service in event */
3454 /* Find service list from address */
3455 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3456 if (svc_info_list == NULL) {
3457 BT_ERR("svc_info_list is NULL");
3461 /* Find service info from service in event */
3462 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3463 if (svc_info == NULL) {
3464 BT_ERR("svc_info is NULL");
3468 /* Build char list from service in event */
3469 __bt_build_char_browse_info(event_data->conn_status.conn_id,
3470 svc_info, &browse_info);
3472 /* Create response and return by sending event*/
3473 /* Build Reply and send to service browse All Included services request of pending apps */
3474 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE,
3475 BT_GATT_GET_SERVICE_PROPERTIES,
3477 sizeof(bt_char_browse_info_t));
3481 static void __bt_handle_client_descriptor_search_result(
3482 event_gattc_descriptor_result_t *event_data)
3484 bt_gatt_service_info_list_t *svc_info_list;
3485 bt_gatt_service_info_t *svc_info;
3486 bt_gatt_char_info_t *char_info;
3487 bt_gatt_descriptor_info_t *desc_info;
3488 bt_descriptor_browse_info_t browse_info;
3490 BT_DBG("descriptor search result status [%d]", event_data->conn_status.status);
3492 memset(&browse_info, 0x00, sizeof(bt_descriptor_browse_info_t));
3495 if (event_data->conn_status.status == OAL_STATUS_SUCCESS) {
3496 /* Find service list from address */
3497 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3498 if (svc_info_list == NULL) {
3499 BT_ERR("svc_info_list is NULL");
3503 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3504 if (svc_info == NULL) {
3505 BT_ERR("svc_info is NULL");
3509 char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
3510 if (char_info == NULL) {
3511 BT_ERR("char_info is NULL");
3515 desc_info = __bt_find_matching_desc(char_info, &event_data->descr_id);
3516 /* If not found, add new descriptor and return */
3518 desc_info = g_malloc0(sizeof(bt_gatt_descriptor_info_t));
3519 memcpy(desc_info->uuid, event_data->descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3521 desc_info->inst_id = event_data->descr_id.inst_id;
3522 char_info->descs = g_slist_append(char_info->descs, desc_info);
3525 /* If found, then return */
3526 BT_DBG("Descriptor browsed is already presesnt");
3529 /* If Not success */
3530 /* Find service list from address */
3531 /* Find included service list from service in event */
3532 /* Create response and return by sending event*/
3533 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3534 if (svc_info_list == NULL) {
3535 BT_ERR("svc_info_list is NULL");
3539 /* Find service info from service in event */
3540 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3541 if (svc_info == NULL) {
3542 BT_ERR("svc_info is NULL");
3546 /* Find char info from char in event */
3547 char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
3548 if (char_info == NULL) {
3549 BT_ERR("char_info is NULL");
3553 /* Build descriptor list from char in event */
3554 __bt_build_descriptor_browse_info(event_data->conn_status.conn_id,
3555 svc_info, char_info, &browse_info);
3558 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE,
3559 BT_GATT_GET_CHARACTERISTIC_PROPERTIES,
3561 sizeof(bt_descriptor_browse_info_t));
3565 static void __bt_handle_client_characteristic_read_data(
3566 event_gattc_read_data *event_data)
3568 int result = BLUETOOTH_ERROR_NONE;
3569 struct gatt_server_info_t *conn_info = NULL;
3570 bluetooth_gatt_client_char_prop_info_t read_info;
3572 /* Read Information data structures */
3573 GVariant *param = NULL;
3574 GVariant *data = NULL;
3575 GVariant *data_svc_uuid = NULL;
3576 GVariant *data_char_uuid = NULL;
3577 char *read_val = NULL;
3578 char *svc_uuid = NULL;
3579 char *char_uuid = NULL;
3582 //memset(&read_info, 0x00, sizeof(bt_gatt_handle_property_t));
3583 memset(&read_info, 0x00, sizeof(bluetooth_gatt_client_char_prop_info_t));
3585 /* Extract Address from conn_id of event data */
3586 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3587 event_data->uuid_status.conn_status.conn_id);
3589 BT_INFO("Characteristic Read result from addr [%s] status [%d]",
3590 conn_info->addr, event_data->uuid_status.conn_status.status);
3592 /* Fill char in buffer */
3593 memcpy(&read_info.characteristic.uuid,
3594 event_data->uuid_status.char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3595 read_info.characteristic.instance_id = event_data->uuid_status.char_id.inst_id;
3597 /* Fill Service in buffer */
3598 memcpy(&read_info.svc.uuid,
3599 event_data->uuid_status.srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3600 read_info.svc.instance_id = event_data->uuid_status.srvc_id.id.inst_id;
3602 /* Fill remote device address */
3603 _bt_convert_addr_string_to_type(read_info.device_address.addr, conn_info->addr);
3605 /* Fill data and reply to all apps waiting for Read result on the same characteristic
3606 Note: Even in case of failure, address, handles and result code should be returned */
3607 if (event_data->uuid_status.conn_status.status != OAL_STATUS_SUCCESS)
3608 result = BLUETOOTH_ERROR_INTERNAL;
3610 if (event_data->data_len > 0) {
3612 // for (i = 0; i < event_data->data_len; i++)
3613 // BT_INFO("Data[%d] = [0x%x]", i, event_data->data[i]);
3616 read_val = g_memdup(&event_data->data[0], event_data->data_len);
3618 data = g_variant_new_from_data(
3619 G_VARIANT_TYPE_BYTESTRING,
3621 event_data->data_len,
3624 BT_ERR("Characteristic Read success, but no data!!!");
3626 data = g_variant_new_from_data(
3627 G_VARIANT_TYPE_BYTESTRING,
3634 svc_uuid = g_memdup(&event_data->uuid_status.srvc_id.id.uuid.uuid[0], uuid_len);
3636 data_svc_uuid = g_variant_new_from_data(
3637 G_VARIANT_TYPE_BYTESTRING,
3643 char_uuid = g_memdup(&event_data->uuid_status.char_id.uuid.uuid[0], uuid_len);
3645 data_char_uuid = g_variant_new_from_data(
3646 G_VARIANT_TYPE_BYTESTRING,
3651 param = g_variant_new("(isn@ayin@ayin@ay)", result,
3655 event_data->uuid_status.srvc_id.id.inst_id,
3658 event_data->uuid_status.char_id.inst_id,
3659 event_data->data_len,
3663 char *sender = NULL;
3664 __bt_gatt_get_pending_request_info(BT_GATT_READ_CHARACTERISTIC, &sender);
3665 _bt_send_event_to_dest(sender, BT_GATT_CLIENT_EVENT,
3666 BLUETOOTH_EVENT_GATT_READ_CHAR,
3669 /* Send DBUS return */
3670 __bt_gatt_handle_pending_request_info(result,
3671 BT_GATT_READ_CHARACTERISTIC,
3673 sizeof(bluetooth_gatt_client_char_prop_info_t));
3684 static void __bt_handle_client_descriptor_read_data(
3685 event_gattc_read_data *event_data)
3687 int result = BLUETOOTH_ERROR_NONE;
3688 struct gatt_server_info_t *conn_info = NULL;
3689 bluetooth_gatt_client_desc_prop_info_t read_info;
3691 /* Read Information data structures */
3692 GVariant *param = NULL;
3693 GVariant *data = NULL;
3694 GVariant *data_svc_uuid = NULL;
3695 GVariant *data_char_uuid = NULL;
3696 GVariant *data_desc_uuid = NULL;
3697 char *read_val = NULL;
3698 char *svc_uuid = NULL;
3699 char *char_uuid = NULL;
3700 char *desc_uuid = NULL;
3704 memset(&read_info, 0x00, sizeof(bluetooth_gatt_client_desc_prop_info_t));
3706 /* Extract Address from conn_id of event data */
3707 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3708 event_data->uuid_status.conn_status.conn_id);
3710 BT_DBG("Descriptor Read result from addr [%s] status [%d]",
3711 conn_info->addr, event_data->uuid_status.conn_status.status);
3713 /* Fill descriptor informations in buffer */
3714 memcpy(&read_info.descriptor.uuid,
3715 event_data->uuid_status.descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3716 read_info.descriptor.instance_id = event_data->uuid_status.descr_id.inst_id;
3718 /* Fill Characteristic informations in buffer */
3719 memcpy(&read_info.characteristic.uuid,
3720 event_data->uuid_status.char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3721 read_info.characteristic.instance_id = event_data->uuid_status.char_id.inst_id;
3723 /* Fill Service informations in buffer */
3724 memcpy(&read_info.svc.uuid,
3725 event_data->uuid_status.srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3726 read_info.svc.instance_id = event_data->uuid_status.srvc_id.id.inst_id;
3728 /* Fill remote device address */
3729 _bt_convert_addr_string_to_type(read_info.device_address.addr, conn_info->addr);
3731 /* Fill data and reply to all apps waiting for Read result on the same characteristic */
3732 if (event_data->uuid_status.conn_status.status != OAL_STATUS_SUCCESS)
3733 result = BLUETOOTH_ERROR_INTERNAL;
3735 if (event_data->data_len > 0) {
3737 for (i = 0; i < event_data->data_len; i++)
3738 BT_DBG("Data[%d] = [0x%x]", i, event_data->data[i]);
3741 read_val = g_memdup(&event_data->data[0], event_data->data_len);
3743 data = g_variant_new_from_data(
3744 G_VARIANT_TYPE_BYTESTRING,
3746 event_data->data_len,
3749 BT_INFO("Descriptor Read success, but no data!!!");
3751 data = g_variant_new_from_data(
3752 G_VARIANT_TYPE_BYTESTRING,
3758 svc_uuid = g_memdup(&event_data->uuid_status.srvc_id.id.uuid.uuid[0], uuid_len);
3760 data_svc_uuid = g_variant_new_from_data(
3761 G_VARIANT_TYPE_BYTESTRING,
3767 char_uuid = g_memdup(&event_data->uuid_status.char_id.uuid.uuid[0], uuid_len);
3769 data_char_uuid = g_variant_new_from_data(
3770 G_VARIANT_TYPE_BYTESTRING,
3776 desc_uuid = g_memdup(&event_data->uuid_status.descr_id.uuid.uuid[0], uuid_len);
3778 data_desc_uuid = g_variant_new_from_data(
3779 G_VARIANT_TYPE_BYTESTRING,
3784 param = g_variant_new("(isn@ayin@ayin@ayin@ay)", result,
3788 event_data->uuid_status.srvc_id.id.inst_id,
3791 event_data->uuid_status.char_id.inst_id,
3794 event_data->uuid_status.descr_id.inst_id,
3795 event_data->data_len,
3799 char *sender = NULL;
3800 __bt_gatt_get_pending_request_info(BT_GATT_READ_DESCRIPTOR_VALUE, &sender);
3801 _bt_send_event_to_dest(sender, BT_GATT_CLIENT_EVENT,
3802 BLUETOOTH_EVENT_GATT_READ_DESC,
3806 /* Send DBUS return */
3807 __bt_gatt_handle_pending_request_info(result,
3808 BT_GATT_READ_DESCRIPTOR_VALUE,
3810 sizeof(bluetooth_gatt_client_desc_prop_info_t));
3821 static void __bt_handle_client_characteristic_write_data(
3822 event_gattc_write_data *event_data)
3824 int result = BLUETOOTH_ERROR_NONE;
3825 struct gatt_server_info_t *conn_info = NULL;
3826 bluetooth_gatt_client_char_prop_info_t write_info;
3828 /* Read Information data structures */
3829 GVariant *param = NULL;
3830 GVariant *data_svc_uuid = NULL;
3831 GVariant *data_char_uuid = NULL;
3832 char *svc_uuid = NULL;
3833 char *char_uuid = NULL;
3836 memset(&write_info, 0x00, sizeof(bluetooth_gatt_client_char_prop_info_t));
3838 /* Extract Address from conn_id of event data */
3839 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3840 event_data->conn_status.conn_id);
3842 BT_DBG("Characteristic Write callback from addr [%s] status [%d]",
3843 conn_info->addr, event_data->conn_status.status);
3845 /* Fill char in buffer */
3846 memcpy(&write_info.characteristic.uuid,
3847 event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3848 write_info.characteristic.instance_id = event_data->char_id.inst_id;
3850 /* Fill Service in buffer */
3851 memcpy(&write_info.svc.uuid,
3852 event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3853 write_info.svc.instance_id = event_data->srvc_id.id.inst_id;
3855 /* Fill remote device address */
3856 _bt_convert_addr_string_to_type(write_info.device_address.addr, conn_info->addr);
3858 if (event_data->conn_status.status != OAL_STATUS_SUCCESS) {
3859 result = BLUETOOTH_ERROR_INTERNAL;
3865 svc_uuid = g_memdup(&event_data->srvc_id.id.uuid.uuid[0], uuid_len);
3867 data_svc_uuid = g_variant_new_from_data(
3868 G_VARIANT_TYPE_BYTESTRING,
3874 char_uuid = g_memdup(&event_data->char_id.uuid.uuid[0], uuid_len);
3876 data_char_uuid = g_variant_new_from_data(
3877 G_VARIANT_TYPE_BYTESTRING,
3882 param = g_variant_new("(isn@ayin@ayi)", result,
3886 event_data->srvc_id.id.inst_id,
3889 event_data->char_id.inst_id);
3892 char *sender = NULL;
3893 __bt_gatt_get_pending_request_info(BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE, &sender);
3894 _bt_send_event_to_dest(sender, BT_GATT_CLIENT_EVENT,
3895 BLUETOOTH_EVENT_GATT_WRITE_CHAR,
3905 /* Send DBUS return */
3906 __bt_gatt_handle_pending_request_info(result,
3907 BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE,
3909 sizeof(bluetooth_gatt_client_char_prop_info_t));
3913 static void __bt_handle_client_descriptor_write_data(
3914 event_gattc_write_data *event_data)
3916 int result = BLUETOOTH_ERROR_NONE;
3917 struct gatt_server_info_t *conn_info = NULL;
3918 bluetooth_gatt_client_desc_prop_info_t write_info;
3920 /* Write Information data structures */
3921 GVariant *param = NULL;
3922 GVariant *data_svc_uuid = NULL;
3923 GVariant *data_char_uuid = NULL;
3924 GVariant *data_desc_uuid = NULL;
3925 char *svc_uuid = NULL;
3926 char *char_uuid = NULL;
3927 char *desc_uuid = NULL;
3930 memset(&write_info, 0x00, sizeof(bluetooth_gatt_client_desc_prop_info_t));
3932 /* Extract Address from conn_id of event data */
3933 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3934 event_data->conn_status.conn_id);
3936 if (NULL == conn_info) {
3937 BT_ERR("Failed to get the conn info for conn_id [%d]", event_data->conn_status.conn_id);
3941 BT_DBG("Descriptor Write callback from addr [%s] status [%d]",
3942 conn_info->addr, event_data->conn_status.status);
3944 /* Fill descriptor informations in buffer */
3945 memcpy(&write_info.descriptor.uuid,
3946 event_data->descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3947 write_info.descriptor.instance_id = event_data->descr_id.inst_id;
3949 /* Fill Characteristic informations in buffer */
3950 memcpy(&write_info.characteristic.uuid,
3951 event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3952 write_info.characteristic.instance_id = event_data->char_id.inst_id;
3954 /* Fill Service informations in buffer */
3955 memcpy(&write_info.svc.uuid,
3956 event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3957 write_info.svc.instance_id = event_data->srvc_id.id.inst_id;
3959 /* Fill remote device address */
3960 _bt_convert_addr_string_to_type(write_info.device_address.addr, conn_info->addr);
3962 if (event_data->conn_status.status != OAL_STATUS_SUCCESS) {
3963 result = BLUETOOTH_ERROR_INTERNAL;
3969 svc_uuid = g_memdup(&event_data->srvc_id.id.uuid.uuid[0], uuid_len);
3971 data_svc_uuid = g_variant_new_from_data(
3972 G_VARIANT_TYPE_BYTESTRING,
3978 char_uuid = g_memdup(&event_data->char_id.uuid.uuid[0], uuid_len);
3980 data_char_uuid = g_variant_new_from_data(
3981 G_VARIANT_TYPE_BYTESTRING,
3987 desc_uuid = g_memdup(&event_data->descr_id.uuid.uuid[0], uuid_len);
3989 data_desc_uuid = g_variant_new_from_data(
3990 G_VARIANT_TYPE_BYTESTRING,
3995 param = g_variant_new("(isn@ayin@ayin@ayi)", result,
3999 event_data->srvc_id.id.inst_id,
4002 event_data->char_id.inst_id,
4005 event_data->descr_id.inst_id);
4008 char *sender = NULL;
4009 __bt_gatt_get_pending_request_info(BT_GATT_WRITE_DESCRIPTOR_VALUE, &sender);
4010 _bt_send_event_to_dest(sender, BT_GATT_CLIENT_EVENT,
4011 BLUETOOTH_EVENT_GATT_WRITE_DESC,
4022 /* Send DBUS return */
4023 __bt_gatt_handle_pending_request_info(result,
4024 BT_GATT_WRITE_DESCRIPTOR_VALUE,
4026 sizeof(bluetooth_gatt_client_desc_prop_info_t));
4029 static void __bt_hanlde_le_device_disconnection(event_dev_conn_status_t *event_data)
4031 int result = BLUETOOTH_ERROR_INTERNAL;
4032 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
4034 _bt_convert_addr_type_to_string(address, (unsigned char *)event_data->address.addr);
4036 /* DBUS Return with fail of pending BT_CONNECT_LE for all the apps */
4037 BT_INFO("Local GATT Client disconnected: Remote addr[%s] ", address + 12);
4039 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE, address,
4040 BT_ADDRESS_STRING_SIZE);
4044 static void __bt_handle_client_notification_registered(
4045 event_gattc_regdereg_notify_t *event_data,
4046 gboolean is_registered)
4048 int result = BLUETOOTH_ERROR_NONE;
4049 struct gatt_server_info_t *conn_info = NULL;
4050 bt_gatt_notif_reg_info_t notif_info;
4053 memset(¬if_info, 0x00, sizeof(bt_gatt_notif_reg_info_t));
4055 BT_INFO("Client Interface [%d] status [%d]",
4056 event_data->conn_id,
4057 event_data->status);
4059 /* Extract Address from conn_id of event data */
4060 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(event_data->conn_id);
4062 BT_INFO("Connection Info is not present, return");
4065 BT_INFO("Notification Registered for addr [%s]", conn_info->addr);
4067 /* Fill svc informations in buffer */
4068 memcpy(¬if_info.svc_uuid,
4069 event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4070 notif_info.svc_inst = event_data->srvc_id.id.inst_id;
4072 /* Fill char in buffer */
4073 memcpy(¬if_info.char_uuid,
4074 event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4075 notif_info.char_inst = event_data->char_id.inst_id;
4077 /* Fill remote device address */
4078 _bt_convert_addr_string_to_type(notif_info.addr.addr, conn_info->addr);
4080 notif_info.is_registered = is_registered;
4082 if (event_data->status != OAL_STATUS_SUCCESS)
4083 result = BLUETOOTH_ERROR_INTERNAL;
4085 /* Send DBUS Return for BT_GATT_WATCH_CHARACTERISTIC */
4086 __bt_gatt_handle_pending_request_info(result,
4087 BT_GATT_WATCH_CHARACTERISTIC,
4089 sizeof(bt_gatt_notif_reg_info_t));
4092 static void __bt_handle_client_notification_data(event_gattc_notify_data *event_data)
4094 /* No status in this event from OAL */
4095 int result = BLUETOOTH_ERROR_NONE;
4097 /* Read Information data structures */
4098 GVariant *param = NULL;
4099 GVariant *data = NULL;
4100 GVariant *data_svc_uuid = NULL;
4101 GVariant *data_char_uuid = NULL;
4102 char *read_val = NULL;
4103 char *svc_uuid = NULL;
4104 char *char_uuid = NULL;
4110 BT_INFO("Notifcation of charc data changed");
4112 if (event_data->data_len > 0) {
4114 for (i = 0; i < event_data->data_len; i++)
4115 BT_INFO("Data[%d] = [0x%x]", i, event_data->data[i]);
4118 addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
4119 _bt_convert_addr_type_to_string(addr,
4120 (unsigned char *)&(event_data->address.addr));
4123 read_val = g_memdup(&event_data->data[0], event_data->data_len);
4125 data = g_variant_new_from_data(
4126 G_VARIANT_TYPE_BYTESTRING,
4128 event_data->data_len,
4131 svc_uuid = g_memdup(&event_data->srvc_id.id.uuid.uuid[0], uuid_len);
4133 data_svc_uuid = g_variant_new_from_data(
4134 G_VARIANT_TYPE_BYTESTRING,
4140 char_uuid = g_memdup(&event_data->char_id.uuid.uuid[0], uuid_len);
4142 data_char_uuid = g_variant_new_from_data(
4143 G_VARIANT_TYPE_BYTESTRING,
4149 param = g_variant_new("(isn@ayin@ayin@ay)", result,
4153 event_data->srvc_id.id.inst_id,
4156 event_data->char_id.inst_id,
4157 event_data->data_len,
4161 _bt_send_event(BT_GATT_CLIENT_EVENT,
4162 BLUETOOTH_EVENT_GATT_CHAR_VAL_CHANGED,
4165 BT_ERR("No Data!!");
4178 static void __bt_handle_client_service_changed_ind(event_gattc_service_changed_data *event_data)
4180 bt_gatt_service_info_list_t *svc_info_list;
4182 bt_gatt_service_info_t *svc_info;
4183 GVariant *param = NULL;
4184 char *address_str = NULL;
4185 char *uuid_str = NULL;
4187 svc_info_list = __bt_get_service_info_list(event_data->conn_id);
4188 if (svc_info_list == NULL) {
4189 BT_ERR("svc_info_list is NULL");
4193 if (event_data->change_type) {
4194 /* Add service UUID in list */
4195 svc_info = g_malloc0(sizeof(bt_gatt_service_info_t));
4196 memcpy(svc_info->uuid, event_data->uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4197 svc_info->inst_id = event_data->inst_id;
4198 svc_info->is_primary = 1; // TODO: Need to check is_primary is required or not
4199 svc_info_list->services = g_slist_append(svc_info_list->services, svc_info);
4200 BT_DBG("Service created and added in Svc info list or connection ID %d", event_data->conn_id);
4202 /* Remove service UUID in list */
4203 for (l = svc_info_list->services; l != NULL; l = g_slist_next(l)) {
4204 svc_info = (bt_gatt_service_info_t *)l->data;
4205 if (svc_info == NULL)
4208 if (!memcmp(svc_info->uuid, event_data->uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)) {
4209 svc_info_list->services = g_slist_remove(svc_info_list->services, svc_info);
4210 __bt_free_service_info(svc_info);
4216 address_str = g_malloc0(BT_ADDRESS_STRING_SIZE);
4217 uuid_str = g_malloc0(BT_UUID_STRING_MAX);
4218 _bt_convert_addr_type_to_string(address_str, event_data->address.addr);
4219 _bt_uuid_to_string(&event_data->uuid, uuid_str);
4221 param = g_variant_new("(iiss)", event_data->inst_id, event_data->change_type, address_str, uuid_str);
4223 _bt_send_event(BT_GATT_CLIENT_EVENT,
4224 BLUETOOTH_EVENT_GATT_CLIENT_SERVICE_CHANGED,
4226 g_free(address_str);
4230 gboolean _bt_is_remote_gatt_device_connected(bluetooth_device_address_t *address)
4233 struct gatt_server_info_t *conn_info = NULL;
4234 gboolean connected = FALSE;
4236 addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
4237 _bt_convert_addr_type_to_string(addr,
4238 (unsigned char *)&(address->addr));
4240 BT_DBG("Check GATT connection status of [%s]", addr);
4241 /* Check if device is already in connected list */
4242 conn_info = _bt_find_remote_gatt_server_info(addr);
4244 BT_DBG("Remote GATT Server device [%s] is Connected", conn_info->addr);
4247 struct gatt_client_info_t *client_info = NULL;
4249 BT_DBG("Remote GATT Server Device [%s] is not Connected", addr);
4251 /* Check if device is already in connected list */
4252 client_info = _bt_find_remote_gatt_client_info(addr);
4254 BT_DBG("Remote Client device [%s] is Connected", client_info->addr);
4257 BT_DBG("Remote GATT Client Device [%s] is not Connected", addr);
4265 void _bt_handle_invocation_context(int function_name, void *data)
4267 switch (function_name) {
4269 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE, BT_CONNECT_LE,
4270 (char *)data, BT_ADDRESS_STRING_SIZE);
4277 int _bt_connect_le_device(bluetooth_device_address_t *address,
4278 int auto_connect, int client_id)
4280 struct gatt_server_info_t *conn_info = NULL;
4281 struct gatt_out_conn_info_t *out_conn_info = NULL;
4283 invocation_info_t *req_info = NULL;
4284 int ret = OAL_STATUS_SUCCESS;
4286 char *remote_address = NULL;
4288 BT_CHECK_PARAMETER(address, return);
4290 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4291 _bt_convert_addr_type_to_string(addr, address->addr);
4292 BT_DBG("GATT Client connect request for address [%s] client instance [%d]",
4296 /* Check if Remote Device is already under connection progress */
4297 req_info = _bt_get_request_info_data_from_function_name(BT_CONNECT_LE);
4299 remote_address = (char*)req_info->user_data;
4300 if (remote_address && !strcasecmp(remote_address, addr)) {/* Address matched */
4301 BT_DBG("Already Connection ongoing for same remote GATT Server address [%s]", remote_address);
4302 /* Return and wait for events to be sent to all apps */
4304 return BLUETOOTH_ERROR_IN_PROGRESS;
4308 /* Check if remote GATT Server is connected or not */
4309 conn_info = _bt_find_remote_gatt_server_info(addr);
4311 BT_ERR("GATT Server is already connected..");
4313 return BLUETOOTH_ERROR_ALREADY_CONNECT;
4316 /* TODO Check Requirement of holding Advertisement before initiating LE connect */
4318 /* Check if app sent 0 client id for connection, in such case, use default gatt client ID */
4319 if (client_id == 0) {
4320 /* GATT CLient connect request sent by an app without any client instance [0] */
4321 BT_DBG("Assign default GATT client id [%d]", gatt_default_client);
4322 client_id = gatt_default_client;
4325 BT_INFO("Connect using CLient ID [%d]", client_id);
4326 ret = gattc_connect(client_id, (bt_address_t*)(address), auto_connect);
4328 if (ret != OAL_STATUS_SUCCESS) {
4329 BT_ERR("gattc_connect is failed. ret: %d", ret);
4331 return _bt_convert_oal_status_to_bt_error(ret);
4334 /* Mark this as outgoing connection */
4335 out_conn_info = g_new0(struct gatt_out_conn_info_t, 1);
4336 out_conn_info->addr = g_strdup(addr);
4337 out_conn_info->client_id = client_id;
4338 BT_INFO("Added outgoing connection info addr[%s]", out_conn_info->addr + 12);
4339 outgoing_gatt_conn_list = g_slist_append(outgoing_gatt_conn_list, out_conn_info);
4342 return BLUETOOTH_ERROR_NONE;
4345 int _bt_gatt_get_primary_services(char *address)
4347 BT_CHECK_PARAMETER(address, return);
4348 struct gatt_server_info_t *conn_info = NULL;
4349 invocation_info_t *req_info = NULL;
4350 int ret = OAL_STATUS_SUCCESS;
4352 /* Check if any app is already browsing primary services on the same remote GATT Server */
4353 req_info = _bt_get_request_info_data(BT_GATT_GET_PRIMARY_SERVICES, address);
4355 BT_INFO("Already Primary Service Browsing ongoing for same rmeote GATT Server");
4356 /* Return and wait for events to be sent to all apps */
4357 return BLUETOOTH_ERROR_NONE;
4360 /* Check if remote GATT Server is connected or not */
4361 conn_info = _bt_find_remote_gatt_server_info(address);
4363 BT_ERR("GATT Server is not yet connected..");
4364 return BLUETOOTH_ERROR_NOT_CONNECTED;
4367 BT_INFO("Get all services. GATT Server [%s] is connected, conn Id [%d]",
4368 conn_info->addr + 12, conn_info->connection_id);
4370 /* Send Primary Service Browsing request to stack */
4371 ret = gattc_search_service(conn_info->connection_id, NULL);
4372 if (ret != OAL_STATUS_SUCCESS) {
4373 BT_ERR("ret: %d", ret);
4374 return _bt_convert_oal_status_to_bt_error(ret);
4376 return BLUETOOTH_ERROR_NONE;
4379 int _bt_gatt_get_all_characteristic(bluetooth_gatt_client_svc_prop_info_t *svc)
4381 BT_CHECK_PARAMETER(svc, return);
4382 struct gatt_server_info_t *conn_info = NULL;
4383 invocation_info_t *req_info = NULL;
4384 bluetooth_gatt_client_svc_prop_info_t *prop;
4385 oal_gatt_srvc_id_t srvc_id;
4386 int ret = OAL_STATUS_SUCCESS;
4389 /* Check if any app is already browsing characteristics of the same service on the same remote GATT Server */
4390 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_GET_SERVICE_PROPERTIES);
4392 prop = (bluetooth_gatt_client_svc_prop_info_t*)req_info->user_data;
4393 if (prop && !memcmp(svc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t))
4394 && !memcmp(prop->svc.uuid, svc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
4395 && prop->svc.instance_id == svc->svc.instance_id) {
4396 BT_INFO("Already Properties browsing for Primary Service ongoing for same remote GATT Server");
4397 /* Return and wait for events to be sent to all apps */
4398 return BLUETOOTH_ERROR_NONE;
4402 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4403 _bt_convert_addr_type_to_string(addr, svc->device_address.addr);
4405 /* Check if remote GATT Server is connected or not */
4406 conn_info = _bt_find_remote_gatt_server_info(addr);
4408 BT_ERR("GATT Server is not yet connected..");
4410 return BLUETOOTH_ERROR_NOT_CONNECTED;
4413 BT_DBG("Get all charc. GATT Server [%s] is connected, conn Id [%d]",
4414 conn_info->addr, conn_info->connection_id);
4416 srvc_id.is_prmry = TRUE;
4417 srvc_id.id.inst_id = svc->svc.instance_id;
4418 memcpy(srvc_id.id.uuid.uuid, svc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4420 /* Search All Characteristic */
4421 ret = gattc_get_characteristic(conn_info->connection_id, &srvc_id, NULL);
4422 if (ret != OAL_STATUS_SUCCESS) {
4423 BT_ERR("ret: %d", ret);
4425 return _bt_convert_oal_status_to_bt_error(ret);
4428 return BLUETOOTH_ERROR_NONE;
4431 int _bt_gatt_get_all_characteristic_properties(
4432 bluetooth_gatt_client_char_prop_info_t *chr)
4434 struct gatt_server_info_t *conn_info = NULL;
4435 invocation_info_t *req_info = NULL;
4436 bluetooth_gatt_client_char_prop_info_t *prop;
4437 oal_gatt_srvc_id_t srvc_id;
4438 oal_gatt_id_t char_id;
4439 int ret = OAL_STATUS_SUCCESS;
4442 BT_CHECK_PARAMETER(chr, return);
4444 /* Check if any app is already browsing descriptors of the same char of
4445 particular service on the same remote GATT Server */
4446 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_GET_CHARACTERISTIC_PROPERTIES);
4448 prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
4449 if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4450 && !memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4451 && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4452 && !memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4453 && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
4454 BT_INFO("Already Properties browsing for Characteristic ongoing for same remote GATT Server");
4455 /* Return and wait for events to be sent to all apps */
4456 return BLUETOOTH_ERROR_NONE;
4460 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4461 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4463 /* Check if remote GATT Server is connected or not */
4464 conn_info = _bt_find_remote_gatt_server_info(addr);
4466 BT_ERR("GATT Server is not yet connected..");
4468 return BLUETOOTH_ERROR_NOT_CONNECTED;
4471 BT_DBG("Get all desc. GATT Server [%s] is connected, conn Id [%d]",
4472 conn_info->addr, conn_info->connection_id);
4474 srvc_id.is_prmry = TRUE;
4475 srvc_id.id.inst_id = chr->svc.instance_id;
4476 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4478 char_id.inst_id = chr->characteristic.instance_id;
4479 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4481 /* Search All Descriptors */
4482 ret = gattc_get_descriptor(conn_info->connection_id, &srvc_id, &char_id, NULL);
4483 if (ret != OAL_STATUS_SUCCESS) {
4484 BT_ERR("ret: %d", ret);
4486 return _bt_convert_oal_status_to_bt_error(ret);
4489 return BLUETOOTH_ERROR_NONE;
4492 int _bt_gatt_read_characteristic_value(
4493 bluetooth_gatt_client_char_prop_info_t *chr)
4495 struct gatt_server_info_t *conn_info = NULL;
4496 invocation_info_t *req_info = NULL;
4497 bluetooth_gatt_client_char_prop_info_t *prop;
4498 oal_gatt_srvc_id_t srvc_id;
4499 oal_gatt_id_t char_id;
4500 int ret = OAL_STATUS_SUCCESS;
4503 BT_CHECK_PARAMETER(chr, return);
4505 /* Check if any app is already Reading characteristic of the same char of
4506 particular service on the same remote GATT Server */
4507 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_READ_CHARACTERISTIC);
4509 prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
4510 if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4511 && !memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4512 && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4513 && !memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4514 && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
4515 BT_INFO("Already Characteristic value Read operation in progress for same remote GATT Server");
4516 /* Return and wait for events to be sent to all apps */
4517 return BLUETOOTH_ERROR_NONE;
4521 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4522 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4524 /* Check if remote GATT Server is connected or not */
4525 conn_info = _bt_find_remote_gatt_server_info(addr);
4527 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4528 conn_info->addr, conn_info->connection_id);
4530 BT_ERR("GATT Server is not yet connected..");
4532 return BLUETOOTH_ERROR_NOT_CONNECTED;
4535 srvc_id.is_prmry = TRUE;
4536 srvc_id.id.inst_id = chr->svc.instance_id;
4537 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4539 char_id.inst_id = chr->characteristic.instance_id;
4540 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4542 /* Search All Descriptors */
4543 ret = gattc_read_characteristic(conn_info->connection_id, &srvc_id, &char_id, OAL_GATT_AUTH_REQ_NONE);
4544 if (ret != OAL_STATUS_SUCCESS) {
4545 BT_ERR("ret: %d", ret);
4547 return _bt_convert_oal_status_to_bt_error(ret);
4550 return BLUETOOTH_ERROR_NONE;
4553 int _bt_gatt_read_descriptor_value(
4554 bluetooth_gatt_client_desc_prop_info_t *desc)
4556 struct gatt_server_info_t *conn_info = NULL;
4557 invocation_info_t *req_info = NULL;
4558 bluetooth_gatt_client_desc_prop_info_t *prop;
4559 oal_gatt_srvc_id_t srvc_id;
4560 oal_gatt_id_t char_id;
4561 oal_gatt_id_t desc_id;
4562 int ret = OAL_STATUS_SUCCESS;
4565 BT_CHECK_PARAMETER(desc, return);
4567 /* Check if any app is already Reading descriptors of the same char of
4568 particular service on the same remote GATT Server */
4569 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_READ_DESCRIPTOR_VALUE);
4571 prop = (bluetooth_gatt_client_desc_prop_info_t*)req_info->user_data;
4572 if (prop && !memcmp(desc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4573 && !memcmp(desc->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4574 && desc->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4575 && !memcmp(desc->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4576 && desc->characteristic.instance_id == prop->characteristic.instance_id /* Characteristic Instance ID matched */
4577 && !memcmp(desc->descriptor.uuid, prop->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Descriptor UUID matched */
4578 && desc->descriptor.instance_id == prop->descriptor.instance_id) { /* Descriptor Instance ID matched */
4579 BT_INFO("Already Descriptor value Read operation in progress for same remote GATT Server");
4580 /* Return and wait for events to be sent to all apps */
4581 return BLUETOOTH_ERROR_NONE;
4585 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4586 _bt_convert_addr_type_to_string(addr, desc->device_address.addr);
4588 /* Check if remote GATT Server is connected or not */
4589 conn_info = _bt_find_remote_gatt_server_info(addr);
4591 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4592 conn_info->addr, conn_info->connection_id);
4594 BT_ERR("GATT Server is not yet connected..");
4596 return BLUETOOTH_ERROR_NOT_CONNECTED;
4599 srvc_id.is_prmry = TRUE;
4600 srvc_id.id.inst_id = desc->svc.instance_id;
4601 memcpy(srvc_id.id.uuid.uuid, desc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4603 char_id.inst_id = desc->characteristic.instance_id;
4604 memcpy(char_id.uuid.uuid, desc->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4606 desc_id.inst_id = desc->descriptor.instance_id;
4607 memcpy(desc_id.uuid.uuid, desc->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4609 /* Search All Descriptors */
4610 ret = gattc_read_descriptor(conn_info->connection_id, &srvc_id, &char_id,
4611 &desc_id, OAL_GATT_AUTH_REQ_NONE);
4612 if (ret != OAL_STATUS_SUCCESS) {
4613 BT_ERR("ret: %d", ret);
4615 return _bt_convert_oal_status_to_bt_error(ret);
4618 return BLUETOOTH_ERROR_NONE;
4622 int _bt_gatt_acquire_notify(bluetooth_gatt_client_char_prop_info_t *chr, int *fd, int *mtu)
4624 struct gatt_server_info_t *conn_info = NULL;
4625 oal_gatt_srvc_id_t srvc_id;
4626 oal_gatt_id_t char_id;
4627 int ret = OAL_STATUS_SUCCESS;
4630 BT_CHECK_PARAMETER(chr, return);
4632 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4633 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4635 /* Check if remote GATT Server is connected or not */
4636 conn_info = _bt_find_remote_gatt_server_info(addr);
4638 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4639 conn_info->addr, conn_info->connection_id);
4641 BT_ERR("GATT Server is not yet connected..");
4643 return BLUETOOTH_ERROR_NOT_CONNECTED;
4646 srvc_id.is_prmry = TRUE;
4647 srvc_id.id.inst_id = chr->svc.instance_id;
4648 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4650 char_id.inst_id = chr->characteristic.instance_id;
4651 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4653 ret = gattc_acquire_notify(conn_info->connection_id, &srvc_id, &char_id, fd, mtu);
4655 if (ret != OAL_STATUS_SUCCESS) {
4656 BT_ERR("ret: %d", ret);
4658 return _bt_convert_oal_status_to_bt_error(ret);
4660 BT_INFO("GATT characterstics FD [%d] mtu[%d]", *fd, *mtu);
4662 return BLUETOOTH_ERROR_NONE;
4666 int _bt_gatt_acquire_write(bluetooth_gatt_client_char_prop_info_t *chr, int *fd, int *mtu)
4669 struct gatt_server_info_t *conn_info = NULL;
4670 oal_gatt_srvc_id_t srvc_id;
4671 oal_gatt_id_t char_id;
4672 int ret = OAL_STATUS_SUCCESS;
4675 BT_CHECK_PARAMETER(chr, return);
4677 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4678 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4680 /* Check if remote GATT Server is connected or not */
4681 conn_info = _bt_find_remote_gatt_server_info(addr);
4683 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4684 conn_info->addr, conn_info->connection_id);
4686 BT_ERR("GATT Server is not yet connected..");
4688 return BLUETOOTH_ERROR_NOT_CONNECTED;
4691 srvc_id.is_prmry = TRUE;
4692 srvc_id.id.inst_id = chr->svc.instance_id;
4693 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4695 char_id.inst_id = chr->characteristic.instance_id;
4696 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4698 ret = gattc_acquire_write(conn_info->connection_id, &srvc_id, &char_id,
4699 OAL_GATT_AUTH_REQ_NONE, fd, mtu);
4700 if (ret != OAL_STATUS_SUCCESS) {
4701 BT_ERR("ret: %d", ret);
4703 return _bt_convert_oal_status_to_bt_error(ret);
4705 BT_INFO("GATT characterstics FD [%d] mtu [%d]", *fd, *mtu);
4707 return BLUETOOTH_ERROR_NONE;
4712 /* Write Characteristic */
4713 int _bt_gatt_write_characteristic_value_by_type(
4714 bluetooth_gatt_client_char_prop_info_t *chr,
4715 bluetooth_gatt_att_data_t *data,
4716 bluetooth_gatt_write_type_e write_type)
4718 struct gatt_server_info_t *conn_info = NULL;
4719 invocation_info_t *req_info = NULL;
4720 bluetooth_gatt_client_char_prop_info_t *prop;
4721 oal_gatt_srvc_id_t srvc_id;
4722 oal_gatt_id_t char_id;
4723 int ret = OAL_STATUS_SUCCESS;
4726 BT_CHECK_PARAMETER(chr, return);
4727 BT_CHECK_PARAMETER(data, return);
4729 /* Check if any app is already writing same char of
4730 particular service on the same remote GATT Server */
4731 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE);
4733 prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
4734 if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4735 && !memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4736 && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4737 && !memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4738 && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
4739 BT_INFO("Already Characteristic Write Value operation in progress for same remote GATT Server");
4740 /* Return and wait for events to be sent to all apps */
4741 return BLUETOOTH_ERROR_NONE;
4745 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4746 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4748 /* Check if remote GATT Server is connected or not */
4749 conn_info = _bt_find_remote_gatt_server_info(addr);
4751 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4752 conn_info->addr, conn_info->connection_id);
4754 BT_ERR("GATT Server is not yet connected..");
4756 return BLUETOOTH_ERROR_NOT_CONNECTED;
4759 srvc_id.is_prmry = TRUE;
4760 srvc_id.id.inst_id = chr->svc.instance_id;
4761 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4763 char_id.inst_id = chr->characteristic.instance_id;
4764 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4766 /* Write CHar value */
4767 ret = gattc_write_characteristic(conn_info->connection_id,
4769 (oal_gatt_write_type_t)write_type, data->length,
4770 OAL_GATT_AUTH_REQ_NONE, (char *)(&data->data[0]));
4771 if (ret != OAL_STATUS_SUCCESS) {
4772 BT_ERR("ret: %d", ret);
4774 return _bt_convert_oal_status_to_bt_error(ret);
4777 return BLUETOOTH_ERROR_NONE;
4780 /* Write Descriptor */
4781 int _bt_gatt_write_descriptor_value_by_type(
4782 bluetooth_gatt_client_desc_prop_info_t *desc,
4783 bluetooth_gatt_att_data_t *data,
4784 bluetooth_gatt_write_type_e write_type)
4786 struct gatt_server_info_t *conn_info = NULL;
4787 invocation_info_t *req_info = NULL;
4788 bluetooth_gatt_client_desc_prop_info_t *prop;
4789 oal_gatt_srvc_id_t srvc_id;
4790 oal_gatt_id_t char_id;
4791 oal_gatt_id_t desc_id;
4792 int ret = OAL_STATUS_SUCCESS;
4796 BT_CHECK_PARAMETER(desc, return);
4797 BT_CHECK_PARAMETER(data, return);
4801 /* Check if any app is already writing on same Descriptor of the same char of
4802 particular service on the same remote GATT Server */
4803 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_WRITE_DESCRIPTOR_VALUE);
4805 prop = (bluetooth_gatt_client_desc_prop_info_t*)req_info->user_data;
4806 if (prop && !memcmp(desc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4807 && !memcmp(desc->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4808 && desc->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4809 && !memcmp(desc->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4810 && desc->characteristic.instance_id == prop->characteristic.instance_id /* Characteristic Instance ID matched */
4811 && !memcmp(desc->descriptor.uuid, prop->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Descriptor UUID matched */
4812 && desc->descriptor.instance_id == prop->descriptor.instance_id) { /* Descriptor Instance ID matched */
4813 BT_INFO("Already Descriptor value Write operation in progress for same remote GATT Server");
4814 /* Return and wait for events to be sent to all apps */
4815 return BLUETOOTH_ERROR_NONE;
4819 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4820 _bt_convert_addr_type_to_string(addr, desc->device_address.addr);
4822 /* Check if remote GATT Server is connected or not */
4823 conn_info = _bt_find_remote_gatt_server_info(addr);
4825 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4826 conn_info->addr, conn_info->connection_id);
4828 BT_ERR("GATT Server is not yet connected..");
4830 return BLUETOOTH_ERROR_NOT_CONNECTED;
4833 srvc_id.is_prmry = TRUE;
4834 srvc_id.id.inst_id = desc->svc.instance_id;
4835 memcpy(srvc_id.id.uuid.uuid, desc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4837 char_id.inst_id = desc->characteristic.instance_id;
4838 memcpy(char_id.uuid.uuid, desc->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4840 desc_id.inst_id = desc->descriptor.instance_id;
4841 memcpy(desc_id.uuid.uuid, desc->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4844 BT_INFO("Connection ID [%d] write type [%d] data length [%d]", conn_info->connection_id, write_type, data->length);
4845 for (k = 0; k < data->length; k++)
4846 BT_INFO("Data[%d] [0x%x]", k, data->data[k]);
4848 ret = gattc_write_descriptor(conn_info->connection_id,
4849 &srvc_id, &char_id, &desc_id,
4850 (oal_gatt_write_type_t)write_type, data->length,
4851 OAL_GATT_AUTH_REQ_NONE, (char *)(&data->data[0]));
4852 if (ret != OAL_STATUS_SUCCESS) {
4853 BT_ERR("ret: %d", ret);
4855 return _bt_convert_oal_status_to_bt_error(ret);
4858 return BLUETOOTH_ERROR_NONE;
4861 int _bt_gatt_watch_characteristic(
4862 bluetooth_gatt_client_char_prop_info_t *chr,
4866 struct gatt_server_info_t *conn_info = NULL;
4867 oal_gatt_srvc_id_t srvc_id;
4868 oal_gatt_id_t char_id;
4869 int ret = OAL_STATUS_SUCCESS;
4872 BT_CHECK_PARAMETER(chr, return);
4874 BT_INFO("Client ID [%d] Is Notify [%d]", client_id, is_notify);
4876 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4877 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4879 /* Check if remote GATT Server is connected or not */
4880 conn_info = _bt_find_remote_gatt_server_info(addr);
4882 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4883 conn_info->addr, conn_info->connection_id);
4885 BT_ERR("GATT Server is not yet connected..");
4887 return BLUETOOTH_ERROR_NOT_CONNECTED;
4889 srvc_id.is_prmry = TRUE;
4890 srvc_id.id.inst_id = chr->svc.instance_id;
4891 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4893 char_id.inst_id = chr->characteristic.instance_id;
4894 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4896 /* Register or unregister Notification characteristic */
4898 ret = gattc_register_for_notification(conn_info->connection_id,
4899 (bt_address_t*)&(chr->device_address),
4900 &srvc_id, &char_id);
4902 ret = gattc_deregister_for_notification(conn_info->connection_id,
4903 (bt_address_t*)&(chr->device_address),
4904 &srvc_id, &char_id);
4906 BT_INFO("Result[%d]", ret);
4907 if (ret != OAL_STATUS_SUCCESS) {
4908 BT_ERR("ret: %d", ret);
4910 return _bt_convert_oal_status_to_bt_error(ret);
4913 return BLUETOOTH_ERROR_NONE;
4917 int _bt_disconnect_le_device(bluetooth_device_address_t *address,
4920 struct gatt_server_info_t *conn_info = NULL;
4921 struct gatt_client_info_t *rem_client_conn_info = NULL;
4922 invocation_info_t *req_info = NULL;
4923 int ret = OAL_STATUS_SUCCESS;
4925 char *remote_address = NULL;
4927 BT_CHECK_PARAMETER(address, return);
4929 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4930 _bt_convert_addr_type_to_string(addr, address->addr);
4931 BT_INFO("GATT Client Disconnect request for address [%s]", addr + 12);
4933 /* Check if Remote Device is already under connection progress */
4934 req_info = _bt_get_request_info_data_from_function_name(BT_DISCONNECT_LE);
4936 remote_address = (char*)req_info->user_data;
4937 if (remote_address && !strcasecmp(remote_address, addr)) {/* Address matched */
4938 BT_DBG("Already DisConnection ongoing for same remote GATT Server address [%s]", remote_address);
4939 /* Return success and wait for events to be sent to all apps */
4941 return BLUETOOTH_ERROR_IN_PROGRESS;
4944 /* Check if remote GATT Server is connected or not */
4945 conn_info = _bt_find_remote_gatt_server_info(addr);
4947 /* Check if app sent 0 client id for Disconnection, in such case, use default gatt client ID */
4948 if (client_id == 0) {
4949 BT_INFO("GATT CLient Disconnect request sent by an app without any client instance [%d]",
4951 BT_INFO("Assign default GATT client id [%d]", gatt_default_client);
4952 client_id = gatt_default_client;
4955 BT_INFO("Disconnect remote gatt server using CLient ID [%d] Connection ID [%d]", client_id, conn_info->connection_id);
4956 ret = gattc_disconnect(client_id, (bt_address_t*)(address),
4957 conn_info->connection_id);
4959 /* check if remote client is connected */
4960 rem_client_conn_info = _bt_find_remote_gatt_client_info(addr);
4962 if (!rem_client_conn_info || client_id != 0) {
4963 BT_ERR("GATT device is not connected..");
4965 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
4968 BT_INFO("Disconnect remote gatt client ");
4970 ret = gatts_disconnect(rem_client_conn_info->instance_id,
4971 (bt_address_t*)(address), rem_client_conn_info->connection_id);
4974 if (ret != OAL_STATUS_SUCCESS) {
4975 BT_ERR("ret: %d", ret);
4977 return _bt_convert_oal_status_to_bt_error(ret);
4980 return BLUETOOTH_ERROR_NONE;
4983 int _bt_gatt_watch_service_changed_indication(const char *sender,
4984 bluetooth_device_address_t *address,
4985 gboolean is_enabled)
4988 bt_service_app_info_t *info = NULL;
4990 BT_INFO("%s Servic changed Indication watcher for app [%s]",
4991 is_enabled ? "Enable":"Disable", sender);
4993 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
4996 if (g_strcmp0(sender, info->sender) == 0 &&
4997 memcmp(info->address.addr, address->addr,
4998 sizeof(bluetooth_device_address_t)) == 0) {
4999 BT_DBG("Found GATT client App.. [%s], sender [%s]", info->uuid, info->sender);
5000 info->is_watcher_enabled = is_enabled;
5004 return BLUETOOTH_ERROR_NONE;
5007 int _bt_unregister_gatt_client_instance(const char *sender, int client_id)
5009 bt_service_app_info_t *info = NULL;
5012 BT_DBG("Unregister Allocated GATT Client instance [%s] Client ID [%d]", sender, client_id);
5014 /* Unregister CLient instance associated with address X. It is possible that another app still
5015 has client_id valid for same remote address */
5016 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
5019 /* Exact matching of sender */
5020 if (!g_strcmp0(info->sender, sender) && info->client_id == client_id) { /* Check for only valid GATT client Instance */
5021 numapps[k].client_id = -1;
5022 numapps[k].is_initialized = FALSE;
5023 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
5024 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
5025 memset(&numapps[k].address.addr, 0x00, sizeof(bluetooth_device_address_t));
5027 /* Its a GATT Client Instance */
5028 ret = gattc_deregister(client_id);
5029 if (ret != OAL_STATUS_SUCCESS) {
5030 BT_ERR("DeAllocate GATT Client instance with stack Fail ret: %d", ret);
5031 return _bt_convert_oal_status_to_bt_error(ret);
5033 return BLUETOOTH_ERROR_NONE;
5038 return BLUETOOTH_ERROR_NOT_FOUND;
5041 static void __bt_handle_client_mtu_exchange_completed(event_gattc_mtu_configured_t *event_data)
5043 int result = BLUETOOTH_ERROR_NONE;
5044 struct gatt_server_info_t *conn_info = NULL;
5045 GVariant *param = NULL;
5049 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(event_data->conn_id);
5050 if (conn_info == NULL) {
5051 BT_ERR("Cant find connection Information");
5055 BT_DBG("GATT Client: MTU Configured from addr [%s] status [%d] MTU size [%d]",
5056 conn_info->addr, event_data->status, event_data->mtu);
5058 if (event_data->status != OAL_STATUS_SUCCESS)
5059 result = BLUETOOTH_ERROR_INTERNAL;
5061 /* DBUS Return fo BT_REQ_ATT_MTU for all the apps */
5062 __bt_gatt_handle_pending_request_info(result, BT_REQ_ATT_MTU, conn_info->addr,
5063 BT_ADDRESS_STRING_SIZE);
5065 if (result == BLUETOOTH_ERROR_NONE) {
5066 mtu = event_data->mtu;
5067 param = g_variant_new("(isqy)",
5073 /* Send event to BT-API */
5074 _bt_send_event(BT_DEVICE_EVENT,
5075 BLUETOOTH_EVENT_GATT_ATT_MTU_CHANGED,
5078 /* Update the MTU for current connection */
5079 __bt_update_mtu_gatt_device(conn_info->addr, event_data->mtu);
5083 static struct gatt_mtu_info_t *__bt_find_mtu_gatt_device(char *address)
5086 struct gatt_mtu_info_t *info = NULL;
5088 for (l = gatt_mtu_info_list; l != NULL; l = g_slist_next(l)) {
5089 info = (struct gatt_mtu_info_t*)l->data;
5093 if (!g_strcmp0(info->addr, address)) {
5094 BT_DBG("Remote GATT device found addr[%s]", info->addr);
5099 BT_DBG("Not found Remote GATT device addr[%s]", address);
5103 static void __bt_remove_mtu_gatt_device(char *address)
5105 struct gatt_mtu_info_t *dev_info = NULL;
5107 dev_info = __bt_find_mtu_gatt_device(address);
5110 BT_DBG("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;
5121 dev_info = __bt_find_mtu_gatt_device(address);
5124 BT_DBG("adding the gatt device in mtu list");
5125 dev_info = g_new0(struct gatt_mtu_info_t, 1);
5126 dev_info->addr = g_strdup(address);
5127 dev_info->att_mtu = BT_DEFAULT_ATT_MTU;
5128 gatt_mtu_info_list = g_slist_append(gatt_mtu_info_list, dev_info);
5132 static void __bt_update_mtu_gatt_device(char *address, int mtu)
5134 struct gatt_mtu_info_t *dev_info = NULL;
5136 dev_info = __bt_find_mtu_gatt_device(address);
5139 dev_info->att_mtu = mtu;
5143 int _bt_gatt_get_data_batching_available_packets(
5144 guint *available_packets)
5146 int ret = OAL_STATUS_SUCCESS;
5148 BT_CHECK_PARAMETER(available_packets, return);
5150 ret = gatt_get_data_batching_available_packets(available_packets);
5151 if (ret != OAL_STATUS_SUCCESS) {
5152 BT_ERR("ret: %d", ret);
5153 return _bt_convert_oal_status_to_bt_error(ret);
5156 return BLUETOOTH_ERROR_NONE;
5159 int _bt_gatt_enable_data_batching(bluetooth_device_address_t *address,
5160 int packet_threshold, int timeout)
5162 int ret = OAL_STATUS_SUCCESS;
5163 char remote_address[BT_ADDRESS_STRING_SIZE] = { 0 };
5165 BT_CHECK_PARAMETER(address, return);
5167 _bt_convert_addr_type_to_string(remote_address, address->addr);
5168 BT_INFO("Enable GATT data batching. address[%s] packet_threshold[%d] timeout[%d]",
5169 remote_address, packet_threshold, timeout);
5171 ret = gatt_enable_data_batching((bt_address_t*)(address), packet_threshold, timeout);
5173 if (ret != OAL_STATUS_SUCCESS) {
5174 BT_ERR("ret: %d", ret);
5175 return _bt_convert_oal_status_to_bt_error(ret);
5178 return BLUETOOTH_ERROR_NONE;
5181 int _bt_gatt_disable_data_batching(bluetooth_device_address_t *address)
5183 int ret = OAL_STATUS_SUCCESS;
5184 char remote_address[BT_ADDRESS_STRING_SIZE] = { 0 };
5186 BT_CHECK_PARAMETER(address, return);
5188 _bt_convert_addr_type_to_string(remote_address, address->addr);
5189 BT_INFO("Disable GATT data batching. address[%s]", remote_address);
5191 ret = gatt_disable_data_batching((bt_address_t*)(address));
5193 if (ret != OAL_STATUS_SUCCESS) {
5194 BT_ERR("ret: %d", ret);
5195 return _bt_convert_oal_status_to_bt_error(ret);
5198 return BLUETOOTH_ERROR_NONE;