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 */
58 #ifdef TIZEN_GATT_CLIENT
65 #define UUID_MAX_LEN 50
68 #define BDADDR_ANY (&(bluetooth_device_address_t) {{0, 0, 0, 0, 0, 0} })
70 #ifdef TIZEN_GATT_CLIENT
71 char uuid_list[NUM_UUID][BT_UUID_STRING_MAX] = {"0000b00b-0000-0000-f065-080080fa49b5", /* Used by BLEAPP */
72 "0000b00b-1111-1111-0123-456789ab0cd2", /* Used by BLEAPP */
73 "0000b00b-2222-1111-0123-456789ab0cd2",
74 "0000b00b-3333-1111-0123-456789ab0cd2",
75 "0000b00b-4444-1111-0123-456789ab0cd2",
76 "0000b00b-5555-1111-0123-456789ab0cd2",
77 "0000b00b-6666-1111-0123-456789ab0cd2",
78 "0000b00b-7777-1111-0123-456789ab0cd2",
79 "0000b00b-8888-1111-0123-456789ab0cd2",
80 "0000b00b-9999-1111-0123-456789ab0cd2",
81 "0000b00b-aaaa-1111-0123-456789ab0cd2",
82 "0000b00b-bbbb-1111-0123-456789ab0cd2",
83 "0000b00b-cccc-1111-0123-456789ab0cd2",
84 "0000b00b-dddd-1111-0123-456789ab0cd2",
85 "0000b00b-eeee-1111-0123-456789ab0cd2",
86 "0000b00b-ffff-1111-0123-456789ab0cd2",
87 "0000b00c-0000-1111-0123-456789ab0cd2",
88 "0000b00c-1111-1111-0123-456789ab0cd2",
89 "0000b00c-2222-1111-0123-456789ab0cd2",
90 "0000b00c-3333-1111-0123-456789ab0cd2"};
92 char uuid_list[NUM_UUID][BT_UUID_STRING_MAX] = {"0000b00b-0000-0000-f065-080080fa49b5", /* Used by BLEAPP */
93 "0000b00b-1111-1111-0123-456789ab0cd2", /* Used by BLEAPP */
94 "0000b00b-2222-1111-0123-456789ab0cd2",
95 "0000b00b-3333-1111-0123-456789ab0cd2",
96 "0000b00b-4444-1111-0123-456789ab0cd2",
97 "0000b00b-5555-1111-0123-456789ab0cd2",
98 "0000b00b-6666-1111-0123-456789ab0cd2",
99 "0000b00b-7777-1111-0123-456789ab0cd2",
100 "0000b00b-8888-1111-0123-456789ab0cd2",
101 "0000b00b-9999-1111-0123-456789ab0cd2"};
104 #ifdef TIZEN_GATT_CLIENT
105 /* Reserved GATT client Instance UUID. This is used only internally by bt-service */
106 #define DEFAULT_GATT_CLIENT_UUID "0000a00a-1111-1111-0123-456789abcdef"
108 static int gatt_default_client = -1;
111 gboolean is_registered;
112 bluetooth_device_address_t addr;
113 unsigned char svc_uuid[BLUETOOTH_UUID_HEX_MAX_LEN];
115 unsigned char char_uuid[BLUETOOTH_UUID_HEX_MAX_LEN];
117 } bt_gatt_notif_reg_info_t;
119 struct gatt_out_conn_info_t {
120 int client_id; /* This value unique identifies a GATT Client instance */
121 char *addr; /* Remote GATT Server address */
124 /* Linked List of outgoing gatt connection list
125 Note: This is valid only for local GATT client */
126 static GSList *outgoing_gatt_conn_list = NULL;
128 /* GATT Server Info(Local Client) List Structure */
129 struct gatt_server_info_t {
130 int connection_id; /* This value will uniquely identify a GATT client-server connection */
131 int client_id; /* This value unique identifies a GATT Client instance */
132 char *addr; /* Remote GATT Server address */
135 /* Linked List of connected Remote GATT Servers */
136 static GSList *gatt_server_info_list = NULL;
140 unsigned char uuid[BLUETOOTH_UUID_HEX_MAX_LEN]; /* If any service added */
141 } bt_gatt_svc_changed_info_t;
146 // bluetooth_device_address_t address; /* Remote BLE Device Address */
147 GSList *services; /* List of all services of above remote device */
148 int count; /* Number of services browsed from remote device */
149 bt_gatt_svc_changed_info_t info;
150 } bt_gatt_service_info_list_t;
153 unsigned char uuid[BLUETOOTH_UUID_HEX_MAX_LEN];
157 GSList *included_svcs;
158 gboolean is_removed; /* 0 => Service is added, 1=> removed */
159 } bt_gatt_service_info_t;
162 unsigned char uuid[BLUETOOTH_UUID_HEX_MAX_LEN];
166 unsigned char val[BLUETOOTH_GATT_ATT_DATA_LENGTH_MAX];
168 } bt_gatt_char_info_t;
171 unsigned char uuid[BLUETOOTH_UUID_HEX_MAX_LEN];
174 unsigned char val[BLUETOOTH_GATT_ATT_DATA_LENGTH_MAX];
175 } bt_gatt_descriptor_info_t;
178 unsigned char uuid[BLUETOOTH_UUID_HEX_MAX_LEN];
180 } bt_gatt_included_service_info_t;
182 GSList *list_gatt_info;
187 /* App Information structure */
190 char sender[BT_SENDER_MAX_LENGTH];
191 char uuid[BT_UUID_STRING_MAX];
194 bluetooth_advertising_data_t adv_data; /* Will store adv data for specific slot */
196 bluetooth_scan_resp_data_t scan_rsp; /* Will store scan rsp data for specific slot */
198 gboolean is_initialized;
199 GSList *service_handles;
200 #ifdef TIZEN_GATT_CLIENT
201 int client_id; /* GATT Client instance ID */
202 bluetooth_device_address_t address; /* Remote BLE Device Address */
203 gboolean is_watcher_enabled;
205 } bt_service_app_info_t;
207 /* GATT Server Request Info Structure */
208 struct gatt_server_req_info {
209 int connection_id; /* This value will uniquely identify a GATT client-server connection */
210 int request_id; /* This is an unique transaction ID assigned against each request by stack */
211 int attribute_handle; /* GATT server attribute handle */
212 int offset; /* GATT server attribute offset on which request is invoked by GATT client */
213 bluetooth_gatt_att_request_type_e request_type; /* Read or Write request */
214 char *addr; /* Remote GATT client address */
217 /* GATT Client Info List Structure */
218 struct gatt_client_info_t {
219 int connection_id; /* This value will uniquely identify a GATT client-server connection */
220 int instance_id; /* This value unique identifies a GATT server instance */
221 char *addr; /* Remote GATT client address */
224 /* GATT Indicate confirm result */
225 struct gatt_indicate_cfm_result_info_t {
226 int result; /* Result of event */
227 char *addr; /* Remote GATT client address */
228 int att_hdl; /* Characteristic Attribute handle */
229 int completed; /* 1 if last event, otheriwse 0 */
232 /* Request Search Utility method */
233 static struct gatt_server_req_info *__bt_gatt_server_find_request_info(int request_id,
234 bluetooth_gatt_att_request_type_e req_type);
236 static int __bt_gatt_send_indication_to_all_connected_clients(bluetooth_gatt_att_data_t *data,
237 bluetooth_gatt_server_indication_params_t *param);
239 static void __bt_remove_all_service_handles(int instance_id);
241 #ifdef TIZEN_GATT_CLIENT
242 static void __bt_handle_client_instance_registered(event_gattc_register_t *data);
243 static void __bt_handle_client_connected(event_gattc_conn_t *event_data);
244 static void __bt_handle_client_disconnected(event_gattc_conn_t *event_data);
245 static void __bt_handle_client_service_search_completed(event_gattc_conn_status_t *event_data);
246 static void __bt_handle_client_service_search_result(event_gattc_service_result_t *event_data);
247 static void __bt_handle_client_characteristic_search_result(
248 event_gattc_characteristic_result_t *event_data);
249 static void __bt_handle_client_descriptor_search_result(event_gattc_descriptor_result_t *event_data);
250 static void __bt_handle_client_characteristic_read_data(event_gattc_read_data *event_data);
251 static void __bt_handle_client_descriptor_read_data(event_gattc_read_data *event_data);
252 static void __bt_handle_client_characteristic_write_data(event_gattc_write_data *event_data);
253 static void __bt_handle_client_descriptor_write_data(event_gattc_write_data *event_data);
254 static int __bt_do_unregister_gatt_instance(int instance_id);
255 static void __bt_hanlde_le_device_disconnection(event_dev_conn_status_t *event_data);
256 static void __bt_handle_client_notification_registered(event_gattc_regdereg_notify_t *event_data,
257 gboolean is_registered);
261 static int __bt_do_unregister_server_instance(int server_instance);
263 /* Linked List of GATT requests from Remote GATT Clients */
264 static GSList *gatt_server_requests = NULL;
266 /* Linked List of connected Remote GATT clients */
267 static GSList *gatt_client_info_list = NULL;
269 /* Number of clients to be notified to */
270 static int num_indicate_clients;
272 /* List of applications */
273 static bt_service_app_info_t numapps[MAX_APPS_SUPPORTED];
275 static void __bt_gatt_handle_pending_request_info(int result,
276 int service_function, void *data, unsigned int size);
278 static void __bt_handle_server_instance_registered(event_gatts_register_t *data);
280 static void __bt_gatt_event_handler(int event_type, gpointer event_data);
282 void _bt_check_adv_app_termination(const char *name)
284 bt_service_app_info_t *app = NULL;
286 int apps[MAX_APPS_SUPPORTED];
288 ret_if(NULL == name);
290 memset(&apps, -1, sizeof(apps));
292 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
295 /* Search for a app which has same sender and stop adv is running */
296 if (!strncasecmp(app->sender, name, strlen(name)) && app->is_initialized == TRUE) {
297 BT_DBG("Match found, name: %s", name);
299 /* Unregister all service handles with stack */
300 __bt_remove_all_service_handles(app->instance_id);
302 /* If Advertising is enabled, stop it */
303 if (app->adv_handle > 0) {
304 BT_INFO("Stop advertising on instance ID [%d]", app->instance_id);
305 /* Disable adv if running */
306 BT_INFO("Disable Advertising Adv Handle [%d] sender [%s]",
307 app->adv_handle, name);
308 _bt_set_advertising(app->sender, app->adv_handle, FALSE, FALSE);
311 /* Save instances of all apps that need to be unregistered */
312 apps[app->instance_id] = 1;
316 /* Unregister all apps one by one */
317 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
319 BT_INFO("Unregister app[%d]", k);
320 /* Unregister server instance */
321 __bt_do_unregister_server_instance(k);
326 static int __bt_gatt_send_indication_to_all_connected_clients(bluetooth_gatt_att_data_t *data,
327 bluetooth_gatt_server_indication_params_t *param)
330 int ret = OAL_STATUS_SUCCESS;
331 int result = OAL_STATUS_INTERNAL_ERROR;
333 BT_INFO("Current total number of connected clients [%d]", g_slist_length(gatt_client_info_list));
334 for (l = gatt_client_info_list; l != NULL; l = l->next) {
335 struct gatt_client_info_t *info = l->data;
338 BT_INFO("GATT Remote client address [%s] connection Id [%d]", info->addr, info->connection_id);
340 ret = gatts_send_indication(param->instance_id, param->atrribute_handle,
341 info->connection_id, data->length,
342 param->need_confirmation, (char *)(&data->data[0]));
344 BT_INFO("Send Indication to GATT client [%s] result: [%d]", info->addr, ret);
345 if (ret == OAL_STATUS_SUCCESS) {
346 BT_INFO("Send Indication sent successfully to GATT client [%s]", info->addr);
348 num_indicate_clients++;
352 BT_INFO("Indication sending done for total number of clients [%d]", num_indicate_clients);
356 static struct gatt_server_req_info *__bt_gatt_server_find_request_info(int request_id,
357 bluetooth_gatt_att_request_type_e req_type)
361 for (l = gatt_server_requests; l != NULL; l = l->next) {
362 struct gatt_server_req_info *req_info = l->data;
364 if (req_info && req_info->request_id == request_id && req_info->request_type == req_type) {
365 BT_INFO("GATT Server request info found Req ID [%d] handle [%d] conn ID [%d]",
366 req_info->request_id, req_info->attribute_handle, req_info->connection_id);
370 BT_ERR("Gatt Request not found");
374 void _bt_get_adv_handle_from_instance(int server_inst, int *adv_handle)
378 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
379 if (numapps[k].is_initialized == 1 && numapps[k].instance_id == server_inst) {
380 *adv_handle = numapps[k].adv_handle;
386 #ifdef TIZEN_GATT_CLIENT
387 char * _bt_gatt_get_default_gatt_client_uuid(void)
389 return g_strdup(DEFAULT_GATT_CLIENT_UUID);
393 static void __bt_register_default_gatt_client()
398 uuid_str = _bt_gatt_get_default_gatt_client_uuid();
399 _bt_string_to_uuid(uuid_str, (service_uuid_t*)&uuid);
401 BT_INFO("Register Default GATT client uuid [%s]", uuid_str);
403 if (OAL_STATUS_SUCCESS != gattc_register(&uuid)) /* for only Smart Control */
404 BT_ERR("gattc register failed");
410 int _bt_gatt_init(void)
414 if (OAL_STATUS_SUCCESS != gatt_enable()) {
415 BT_ERR("gatt Init failed");
416 return BLUETOOTH_ERROR_INTERNAL;
419 /* Register gatt event handler */
420 _bt_service_register_event_handler_callback(BT_GATT_MODULE, __bt_gatt_event_handler);
421 #ifdef TIZEN_GATT_CLIENT
422 /*In bluedroid product, defacult gatt client is handled differently*/
423 __bt_register_default_gatt_client();
426 return BLUETOOTH_ERROR_NONE;
429 static void __bt_service_reset_gatt_data()
433 BT_INFO("Rest numapp");
436 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
437 numapps[k].is_initialized = 0;
438 numapps[k].instance_id = -1;
439 numapps[k].adv_handle = 0;
440 numapps[k].adv_instance = -1;
441 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
442 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
443 memset(numapps[k].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
444 memset(numapps[k].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
445 numapps[k].adv_data_len = 0;
446 numapps[k].scan_rsp_len = 0;
450 void _bt_gatt_deinit(void)
452 BT_INFO("GATT deinit");
454 if (OAL_STATUS_SUCCESS != gatt_disable())
455 BT_ERR("gatt deinit failed");
457 /* Un-register gatt event handler */
458 _bt_service_unregister_event_handler_callback(BT_GATT_MODULE);
460 __bt_service_reset_gatt_data();
463 void _bt_update_adv_handle(const char *sender, int adv_handle)
466 BT_DBG("Sender [%s] Adv handle [%d]", sender, adv_handle);
467 bt_service_app_info_t *info = NULL;
469 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
471 /* Search for a app which has same sender and adv handle as 0 */
472 if (!g_strcmp0(info->sender, sender) && info->adv_handle == 0)
473 info->adv_handle = adv_handle;
477 int _bt_is_sender_gatt_server_with_no_adv(const char *sender, int adv_handle)
480 BT_DBG("Sender [%s] Adv handle [%d]", sender, adv_handle);
481 bt_service_app_info_t *info = NULL;
483 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
485 /* Search for a app which has same sender and adv handle as 0
486 It is possible that same sender but different adv handle */
487 if (!g_strcmp0(info->sender, sender) && info->adv_handle == 0) {
488 //info->adv_handle = adv_handle;
489 return info->instance_id;
495 int _bt_get_allocated_server_instance(const char *sender, int adv_handle, gboolean use_reserved_slot)
498 BT_DBG("Sender [%s] Adv handle [%d]", sender, adv_handle);
499 bt_service_app_info_t *info = NULL;
501 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
504 /* Exact matching of Adv handle + sender combination */
505 if (!g_strcmp0(info->sender, sender) && info->adv_handle == adv_handle)
506 return info->instance_id;
508 if (!g_strcmp0(info->sender, sender) && info->adv_handle == -1)
509 return info->instance_id;
515 char * _bt_get_sender_and_handle(int server_instance, int *adv_handle)
518 bt_service_app_info_t *info = NULL;
520 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
523 if (info->instance_id == server_instance && info->adv_handle != -1) {
524 *adv_handle = info->adv_handle;
525 BT_DBG("Server instance [%d] Adv handle [%d] Sender [%s]", server_instance, *adv_handle, info->sender);
526 return g_strdup(info->sender);
532 void _bt_set_new_adv_data(bluetooth_advertising_data_t *adv, int len, int instance)
536 bt_service_app_info_t *info = NULL;
537 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
540 if (info->instance_id == instance) {
541 memcpy(info->adv_data.data, &adv->data, len);
548 void _bt_set_new_scan_rsp_data(bluetooth_scan_resp_data_t *scan, int len, int instance)
552 bt_service_app_info_t *info = NULL;
553 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
556 if (info->instance_id == instance) {
557 memcpy(info->scan_rsp.data, &scan->data, len);
564 void _bt_get_previous_adv_data(bluetooth_advertising_data_t *adv, int *len, int instance)
568 bt_service_app_info_t *info = NULL;
570 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
573 if (info->instance_id == instance) {
574 memcpy(&adv->data, info->adv_data.data, info->adv_data_len);
575 *len = info->adv_data_len;
581 void _bt_get_previous_scan_rsp_data(bluetooth_scan_resp_data_t *scan, int *len, int instance)
585 bt_service_app_info_t *info = NULL;
587 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
590 if (info->instance_id == instance) {
591 memcpy(&scan->data, info->scan_rsp.data, info->scan_rsp_len);
592 *len = info->scan_rsp_len;
598 #ifdef TIZEN_GATT_CLIENT
599 static int __bt_do_unregister_gatt_instance(int instance_id)
601 int ret = OAL_STATUS_SUCCESS;
604 BT_INFO("DeAllocate server or client instance ID [%d]", instance_id);
606 /* Reset data: instance_id parameter could be either for GATT Server or for GATT client */
607 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
608 if (numapps[k].instance_id == instance_id) {
609 BT_INFO("This is a GATT server app, unregister: Slot [%d] vacant", k);
610 numapps[k].is_initialized = FALSE;
611 numapps[k].instance_id = -1;
612 numapps[k].adv_handle = 0;
613 numapps[k].adv_instance = -1;
614 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
615 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
616 memset(numapps[k].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
617 memset(numapps[k].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
618 numapps[k].adv_data_len = 0;
619 numapps[k].scan_rsp_len = 0;
621 /* Its a GATT Server Instance */
622 ret = gatts_unregister(instance_id);
623 if (ret != OAL_STATUS_SUCCESS) {
624 BT_ERR("DeAllocate server instance with stack Fail ret: %d", ret);
625 return BLUETOOTH_ERROR_INTERNAL;
628 } else if (numapps[k].client_id == instance_id) {
629 BT_INFO("This is a GATT client app, unregister: Slot [%d] vacant", k);
630 numapps[k].client_id = -1;
631 numapps[k].is_initialized = FALSE;
632 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
633 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
634 memset(&numapps[k].address.addr, 0x00, sizeof(bluetooth_device_address_t));
636 /* Its a GATT Client Instance */
637 ret = gattc_deregister(instance_id);
638 if (ret != OAL_STATUS_SUCCESS) {
639 BT_ERR("DeAllocate GATT Client instance with stack Fail ret: %d", ret);
640 return BLUETOOTH_ERROR_INTERNAL;
645 return BLUETOOTH_ERROR_NONE;
649 static int __bt_do_unregister_server_instance(int server_instance)
651 int ret = OAL_STATUS_SUCCESS;
654 /* Unregister the server instance */
655 ret = gatts_unregister(server_instance);
656 if (ret != OAL_STATUS_SUCCESS) {
657 BT_ERR("DeAllocate server instance with stack Fail ret: %d", ret);
658 return BLUETOOTH_ERROR_INTERNAL;
660 BT_INFO("DeAllocated server instance with stack successful..");
663 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
664 if (numapps[k].instance_id == server_instance) {
665 numapps[k].is_initialized = 0;
666 numapps[k].instance_id = -1;
667 numapps[k].adv_handle = 0;
668 numapps[k].adv_instance = -1;
669 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
670 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
671 memset(numapps[k].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
672 memset(numapps[k].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
673 numapps[k].adv_data_len = 0;
674 numapps[k].scan_rsp_len = 0;
678 BT_DBG("Going8 to reset numapp block num [%d]", k);
679 return BLUETOOTH_ERROR_NONE;
682 static void __bt_remove_all_service_handles(int instance_id)
686 int ret = OAL_STATUS_SUCCESS;
689 count = g_slist_length(numapps[instance_id].service_handles);
690 BT_INFO("Before handle removal: current count [%d]", count);
692 for (l = numapps[instance_id].service_handles; l != NULL;) {
697 BT_INFO("Server Handle to be Removed [%d] Instance ID [%d]", *handle, instance_id);
698 if (_bt_gatt_server_stop_service(numapps[instance_id].sender, *handle, instance_id) != BLUETOOTH_ERROR_NONE)
701 ret = gatts_delete_service(instance_id, *handle);
702 if (ret != OAL_STATUS_SUCCESS) {
703 BT_ERR("ret: %d", ret);
706 numapps[instance_id].service_handles = g_slist_remove(numapps[instance_id].service_handles, handle);
709 count = g_slist_length(numapps[instance_id].service_handles);
710 BT_INFO("After deleting current count [%d]", count);
716 int _bt_unregister_server_instance(const char *sender, int adv_handle)
718 BT_INFO("Unregister Allocated server instance request Sender [%s] Adv handle [%d]", sender, adv_handle);
719 int result = BLUETOOTH_ERROR_NONE;
720 int apps[MAX_APPS_SUPPORTED];
724 memset(&apps, -1, sizeof(apps));
726 if (adv_handle == 0) {
727 BT_DBG("Its a direct GATT Server app request to unregister");
728 /* Unregister server instance for each app with same sender (case: GATT Server with multiple adv handle) */
729 bt_service_app_info_t *info = NULL;
731 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
734 /* Exact matching of sender */
735 if (!g_strcmp0(info->sender, sender)) {
736 BT_INFO("Unregister GATT server instance [%d]", info->instance_id);
737 /* Unregister all service handles with stack */
738 __bt_remove_all_service_handles(info->instance_id);
740 /* Disable adv if running */
741 BT_INFO("Disable Advertising Adv Handle [%d] sender [%s]",
742 info->adv_handle, sender);
743 _bt_set_advertising(sender, info->adv_handle, FALSE, FALSE);
745 /* Save all instances which need to be unregistered */
746 apps[info->instance_id] = 1;
750 BT_DBG("Its an Internal unregister request by adv application");
751 server_instance = _bt_get_allocated_server_instance(sender, adv_handle, FALSE);
752 BT_DBG("Its an Internal unregister request by adv application: Adv disabled srv instance [%d]", server_instance);
753 if (server_instance == -1) {
754 BT_ERR("No allocated server instance to be removed");
755 return BLUETOOTH_ERROR_INVALID_PARAM;
757 if (!numapps[server_instance].service_handles) {
758 BT_INFO("There are no Service handles with this app, safe to unregister");
759 /* Unregister server instance only if this sender does not have any gatt services in it */
760 result = __bt_do_unregister_server_instance(server_instance);
762 numapps[server_instance].adv_handle = 0;
763 memset(numapps[server_instance].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
764 numapps[server_instance].adv_data_len = 0;
765 memset(numapps[server_instance].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
766 numapps[server_instance].scan_rsp_len = 0;
771 /* Unregister all apps one by one */
772 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
774 BT_INFO("Unregister app[%d]", k);
775 /* Unregister server instance */
776 __bt_do_unregister_server_instance(k);
783 int _bt_register_server_instance(const char *sender, int adv_handle)
785 int ret = OAL_STATUS_SUCCESS;
786 char *uuid_string = NULL;
791 BT_INFO("###Check on which instance Server instance can be initialized....");
792 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
793 if (numapps[k].is_initialized == 1) {
794 BT_INFO("Instance ID [%d] is already in use..Check next slot", numapps[k].instance_id);
797 BT_INFO("Time to register GATT Server..UUID to be used is [%s] slot [%d]", uuid_list[slot-1], slot);
803 BT_ERR("No Slot if free for GATT Server registration..");
804 return BLUETOOTH_ERROR_REGISTRATION_FAILED;
807 uuid_string = g_malloc0(BT_UUID_STRING_MAX);
808 _bt_string_to_uuid(uuid_list[slot-1], (service_uuid_t*)&uuid);
809 g_strlcpy(uuid_string, uuid_list[slot-1], BT_UUID_STRING_MAX);
810 BT_INFO("Copied UUID string [%s]", uuid_string);
812 /* Register GATT Server */
813 ret = gatts_register(&uuid);
814 if (ret != OAL_STATUS_SUCCESS) {
815 BT_ERR("ret: %d", ret);
817 return BLUETOOTH_ERROR_INTERNAL;
819 BT_INFO("GATT Server registration call successfully accepted by OAL..wait for Instance Initialized event from OAL..");
820 /* Return & wait for GATT Server Instance Initialization event */
821 memset(numapps[slot].sender, 0x00, sizeof(numapps[slot].sender));
822 memset(numapps[slot].uuid, 0x00, sizeof(numapps[slot].uuid));
824 g_strlcpy(numapps[slot].sender, sender, sizeof(numapps[slot].sender));
825 g_strlcpy(numapps[slot].uuid, uuid_string, sizeof(numapps[slot].uuid));
827 numapps[slot].is_initialized = 0; /* Set initialization from app registered callback */
828 numapps[slot].adv_handle = adv_handle;
831 return BLUETOOTH_ERROR_NONE;
835 static void __bt_gatt_handle_pending_request_info(int result,
836 int service_function, void *data, unsigned int size)
840 invocation_info_t *req_info = NULL;
841 ret_if(data == NULL);
844 for (l = _bt_get_invocation_list(); l != NULL; l = g_slist_next(l)) {
846 if (req_info == NULL || req_info->service_function != service_function)
849 switch (service_function) {
851 case BT_GATT_SERVER_REGISTER: {
852 bt_service_app_info_t *param = (bt_service_app_info_t*)data;
853 BT_INFO("Sender [%s] Request Sender [%s]", param->sender, req_info->sender);
854 if (!g_strcmp0(req_info->sender, param->sender)) {
855 BT_DBG("GATT Server app found [%s]", req_info->sender);
857 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
858 g_array_append_vals(out_param, ¶m->instance_id, sizeof(int));
859 _bt_service_method_return(req_info->context, out_param, result);
861 g_free(req_info->user_data);
862 _bt_free_info_from_invocation_list(req_info);
863 g_array_free(out_param, TRUE);
867 case BT_GATT_SERVER_START_SERVICE:
868 case BT_GATT_SERVER_DELETE_SERVICE: {
869 bt_service_app_info_t *param = (bt_service_app_info_t*)data;
870 BT_INFO("Sender [%s] Request Sender [%s]", param->sender, req_info->sender);
872 int *saved_instance_id = (int*)req_info->user_data;
873 if (!g_strcmp0(req_info->sender, param->sender) && param->instance_id == *saved_instance_id) {
874 BT_DBG("GATT Server app found [%s] Instance ID [%d] Reply DBUS",
875 req_info->sender, *saved_instance_id);
877 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
878 g_array_append_vals(out_param, &saved_instance_id, sizeof(int));
879 _bt_service_method_return(req_info->context, out_param, result);
881 g_free(req_info->user_data);
882 _bt_free_info_from_invocation_list(req_info);
883 g_array_free(out_param, TRUE);
887 case BT_GATT_SERVER_ADD_SERVICE:
888 case BT_GATT_SERVER_ADD_DESCRIPTOR:
889 case BT_GATT_SERVER_ADD_CHARACTERISTIC: {
890 int *handle = (int*)data;
891 BT_INFO("Characteristic added: Handle [%d]", *handle);
892 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
894 g_array_append_vals(out_param, handle, sizeof(int));
895 _bt_service_method_return(req_info->context, out_param, result);
897 g_free(req_info->user_data);
898 _bt_free_info_from_invocation_list(req_info);
899 g_array_free(out_param, TRUE);
904 case BT_DISCONNECT_LE: {
905 #ifdef TIZEN_GATT_CLIENT
906 char *addr = (char*)req_info->user_data;
907 bluetooth_device_address_t address;
909 if (!g_strcmp0(addr, (char*)data)) {
910 BT_INFO("GATT Client connect-disconnect call pending for app [%s] addr [%s]",
911 req_info->sender, addr);
912 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
913 _bt_convert_addr_string_to_type(address.addr, addr);
915 g_array_append_vals(out_param, (bluetooth_device_address_t*)&address,
916 sizeof(bluetooth_device_address_t));
917 _bt_service_method_return(req_info->context, out_param, result);
919 g_free(req_info->user_data);
920 _bt_free_info_from_invocation_list(req_info);
921 g_array_free(out_param, TRUE);
924 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
926 g_array_append_vals(out_param, (bluetooth_device_address_t*)data,
927 sizeof(bluetooth_device_address_t));
928 _bt_service_method_return(req_info->context, out_param, result);
930 g_free(req_info->user_data);
931 _bt_free_info_from_invocation_list(req_info);
933 g_array_free(out_param, TRUE);
937 #ifdef TIZEN_GATT_CLIENT
938 case BT_GATT_CLIENT_REGISTER: {
939 bt_service_app_info_t *param = (bt_service_app_info_t*)data;
940 BT_INFO("Sender [%s] Request Sender [%s]", param->sender, req_info->sender);
941 if (!g_strcmp0(req_info->sender, param->sender)) {
942 BT_DBG("GATT Client app found [%s] created client ID [%d]",
943 req_info->sender, param->client_id);
945 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
946 g_array_append_vals(out_param, ¶m->client_id, sizeof(int));
947 _bt_service_method_return(req_info->context, out_param, result);
949 g_free(req_info->user_data);
950 _bt_free_info_from_invocation_list(req_info);
951 g_array_free(out_param, TRUE);
955 case BT_GATT_GET_PRIMARY_SERVICES: {
956 bt_services_browse_info_t *param = (bt_services_browse_info_t*)data;
957 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
958 _bt_convert_addr_type_to_string(address,
959 (unsigned char *)(¶m->device_addr.addr));
961 BT_INFO("Request Sender [%s] address [%s]", req_info->sender, address);
963 /* Match address to determine same request */
964 if (!g_strcmp0((char*)req_info->user_data, address)) {
965 BT_DBG("GATT Client app found [%s] Remote address [%s]",
966 req_info->sender, address);
968 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
969 g_array_append_vals(out_param, param,
970 sizeof(bt_services_browse_info_t));
972 //g_array_append_vals(out_param, ¶m->client_id, sizeof(int));
973 _bt_service_method_return(req_info->context, out_param, result);
975 g_free(req_info->user_data);
976 _bt_free_info_from_invocation_list(req_info);
977 g_array_free(out_param, TRUE);
982 case BT_GATT_GET_SERVICE_PROPERTIES: {
983 bt_char_browse_info_t param;
984 memcpy((void*)¶m, data, sizeof(bt_char_browse_info_t));
985 //bt_char_browse_info_t *param = (bt_char_browse_info_t*)data;
986 BT_INFO("Request Sender [%s]", req_info->sender);
987 bluetooth_gatt_client_svc_prop_info_t *prop = (bluetooth_gatt_client_svc_prop_info_t*)req_info->user_data;
989 /* Match both address and service properties to determine same request */
990 if (!memcmp(param.device_addr.addr,
991 prop->device_address.addr,
992 sizeof(bluetooth_device_address_t)) &&
993 !memcmp(param.svc_uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
994 param.svc_inst_id == prop->svc.instance_id) {
995 BT_INFO("Returning Service properties");
997 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
998 g_array_append_vals(out_param, ¶m, sizeof(bt_char_browse_info_t));
999 _bt_service_method_return(req_info->context, out_param, result);
1001 g_free(req_info->user_data);
1002 _bt_free_info_from_invocation_list(req_info);
1003 g_array_free(out_param, TRUE);
1007 case BT_GATT_GET_CHARACTERISTIC_PROPERTIES: {
1008 bt_descriptor_browse_info_t *param = (bt_descriptor_browse_info_t*)data;
1010 BT_INFO("Request Sender [%s]", req_info->sender);
1011 bluetooth_gatt_client_char_prop_info_t *prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
1013 /* Match both address, service properties &char properties to determine same request */
1014 if (!memcmp(param->device_addr.addr,
1015 prop->device_address.addr,
1016 sizeof(bluetooth_device_address_t)) &&
1017 !memcmp(param->svc_uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1018 param->svc_inst_id == prop->svc.instance_id &&
1019 !memcmp(param->char_uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1020 param->char_inst_id == prop->characteristic.instance_id) {
1021 BT_INFO("Returning Characteristic properties");
1022 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1023 g_array_append_vals(out_param, param, sizeof(bt_descriptor_browse_info_t));
1024 _bt_service_method_return(req_info->context, out_param, result);
1026 g_free(req_info->user_data);
1027 _bt_free_info_from_invocation_list(req_info);
1028 g_array_free(out_param, TRUE);
1032 case BT_GATT_WATCH_CHARACTERISTIC: {
1033 bt_gatt_notif_reg_info_t *param = (bt_gatt_notif_reg_info_t*)data;
1034 bluetooth_gatt_client_char_prop_info_t *prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
1035 BT_INFO("Request Sender [%s]", req_info->sender);
1036 /* Match both address, service properties &char properties to determine same request */
1037 if (!memcmp(param->addr.addr,
1038 prop->device_address.addr,
1039 sizeof(bluetooth_device_address_t)) &&
1040 !memcmp(param->svc_uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1041 param->svc_inst == prop->svc.instance_id &&
1042 !memcmp(param->char_uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1043 param->char_inst == prop->characteristic.instance_id) {
1044 BT_INFO("Characteristic Watch Successful: Is registered [%d]",
1045 param->is_registered);
1046 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1047 g_array_append_vals(out_param, param, sizeof(bt_gatt_notif_reg_info_t));
1048 _bt_service_method_return(req_info->context, out_param, result);
1049 g_free(req_info->user_data);
1050 _bt_free_info_from_invocation_list(req_info);
1051 g_array_free(out_param, TRUE);
1055 case BT_GATT_READ_CHARACTERISTIC:
1056 case BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE: {
1057 bluetooth_gatt_client_char_prop_info_t *param = (bluetooth_gatt_client_char_prop_info_t*)data;
1058 BT_INFO("Request Sender [%s]", req_info->sender);
1060 char *addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
1061 bluetooth_gatt_client_char_prop_info_t *prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
1062 _bt_convert_addr_type_to_string(addr, prop->device_address.addr);
1064 if (!memcmp(param, prop, sizeof(bluetooth_gatt_client_char_prop_info_t))) {
1065 BT_INFO("Gatt Char read or write request matched for address [%s]", addr);
1066 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1067 g_array_append_vals(out_param, param, sizeof(bluetooth_gatt_client_char_prop_info_t));
1068 _bt_service_method_return(req_info->context, out_param, result);
1070 g_free(req_info->user_data);
1071 _bt_free_info_from_invocation_list(req_info);
1072 g_array_free(out_param, TRUE);
1077 case BT_GATT_READ_DESCRIPTOR_VALUE:
1078 case BT_GATT_WRITE_DESCRIPTOR_VALUE: {
1079 bluetooth_gatt_client_desc_prop_info_t *param = (bluetooth_gatt_client_desc_prop_info_t*)data;
1080 BT_INFO("Request Sender [%s]", req_info->sender);
1082 char *addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
1083 bluetooth_gatt_client_desc_prop_info_t *prop = (bluetooth_gatt_client_desc_prop_info_t*)req_info->user_data;
1084 _bt_convert_addr_type_to_string(addr, prop->device_address.addr);
1086 if (!memcmp(param, prop, sizeof(bluetooth_gatt_client_desc_prop_info_t))) {
1087 BT_INFO("Descriptor read or write request matched for address [%s]", addr);
1088 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1089 g_array_append_vals(out_param, param, sizeof(bluetooth_gatt_client_desc_prop_info_t));
1090 _bt_service_method_return(req_info->context, out_param, result);
1092 g_free(req_info->user_data);
1093 _bt_free_info_from_invocation_list(req_info);
1094 g_array_free(out_param, TRUE);
1107 static void __bt_handle_server_instance_registered(event_gatts_register_t *data)
1109 bt_service_app_info_t *info = NULL;
1111 char *uuid_string = g_malloc0(BT_UUID_STRING_MAX);
1113 _bt_uuid_to_string(&(data->server_uuid), uuid_string);
1114 BT_INFO("Instance ID is Intialized [%d] UUID initialized [%s]", data->server_inst, uuid_string);
1116 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1117 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1120 if (g_strcmp0(info->uuid, uuid_string) == 0) {
1121 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1122 info->is_initialized = TRUE;
1123 info->instance_id = data->server_inst;
1124 info->adv_instance = data->server_inst;
1125 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE, BT_GATT_SERVER_REGISTER,
1126 (void*)info, sizeof(bt_service_app_info_t));
1130 g_free(uuid_string);
1134 static void __bt_handle_gatt_server_service_added(event_gatts_srvc_prm_t *event)
1136 int result = BLUETOOTH_ERROR_NONE;
1140 bt_service_app_info_t *info = NULL;
1143 char *uuid_str = g_malloc0(BT_UUID_STRING_MAX);
1144 _bt_uuid_to_string(&(event->gatt_srvc_id.id.uuid), uuid_str);
1145 BT_INFO("GATT Added Service UUID: [%s] Server Ins [%d] Service hdl [%d] Is Primary [%d]\n",
1146 uuid_str, event->gatt_srvc_stat.server_inst, svc_handle, event->gatt_srvc_id.is_prmry);
1148 if (event->gatt_srvc_stat.status != OAL_STATUS_SUCCESS) {
1149 result = BLUETOOTH_ERROR_INTERNAL;
1150 svc_handle = 0; /* Service handle set to 0 indicates.
1151 0 is reserved by BT SIG, cant be used by app*/
1153 svc_handle = event->gatt_srvc_stat.servic_hndl;
1155 BT_INFO("GATT Added Service Status [%d] Server Ins [%d] Service hdl [%d] Is Primary [%d]\n",
1156 event->gatt_srvc_stat.status, event->gatt_srvc_stat.server_inst, svc_handle, event->gatt_srvc_id.is_prmry);
1158 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1159 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1162 if (info->instance_id == event->gatt_srvc_stat.server_inst) {
1163 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1164 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_ADD_SERVICE,
1165 (int*)&svc_handle, sizeof(int));
1167 /* Add Service Handle */
1168 if (svc_handle > 0) {
1169 handle = g_malloc0(sizeof(int));
1170 *handle = svc_handle;
1171 numapps[event->gatt_srvc_stat.server_inst].service_handles = g_slist_append(numapps[event->gatt_srvc_stat.server_inst].service_handles, handle);
1172 count = g_slist_length(numapps[event->gatt_srvc_stat.server_inst].service_handles);
1173 BT_INFO("Added Service handle [%d] to list..current count [%d]", svc_handle, count);
1183 static void __bt_handle_gatt_server_characteristic_added(event_gatts_srvc_charctr_t *event)
1185 int result = BLUETOOTH_ERROR_NONE;
1187 bt_service_app_info_t *info = NULL;
1189 char *uuid_str = g_malloc0(BT_UUID_STRING_MAX);
1191 BT_INFO("GATT Server Char added status [%d]", event->gatt_srvc_stat.status);
1192 BT_INFO("GATT Server_inst: [%d]", event->gatt_srvc_stat.server_inst);
1193 BT_INFO("GATT Service Handle: [%d]", event->gatt_srvc_stat.servic_hndl);
1194 BT_INFO("GATT Add characteristic Status: [%d]", event->gatt_srvc_stat.status);
1195 BT_INFO("GATT Service characteristic Handle: [%d]", event->charctr_hndl);
1197 if (event->gatt_srvc_stat.status != OAL_STATUS_SUCCESS) {
1198 result = BLUETOOTH_ERROR_INTERNAL;
1199 char_handle = 0; /* characteristic handle set to 0 indicates.
1200 0 is reserved by BT SIG, cant be used by app*/
1202 char_handle = event->charctr_hndl;
1205 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1206 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1209 if (info->instance_id == event->gatt_srvc_stat.server_inst) {
1210 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1211 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_ADD_CHARACTERISTIC,
1212 (int*)&char_handle, sizeof(int));
1218 _bt_uuid_to_string(&(event->charctr_uuid), uuid_str);
1219 BT_INFO("GATT Added Characteristic: UUID: [%s]", uuid_str);
1225 static void __bt_handle_gatt_server_descriptor_added(event_gatts_srvc_descr_t* event)
1227 int result = BLUETOOTH_ERROR_NONE;
1229 bt_service_app_info_t *info = NULL;
1231 char *uuid_str = g_malloc0(BT_UUID_STRING_MAX);
1233 BT_INFO("GATT Server Descriptor added status [%d]", event->gatt_srvc_stat.status);
1234 BT_INFO("GATT Server_inst: [%d]", event->gatt_srvc_stat.server_inst);
1235 BT_INFO("GATT Service Handle: [%d]", event->gatt_srvc_stat.servic_hndl);
1236 BT_INFO("GATT Add Descriptor Status: [%d]", event->gatt_srvc_stat.status);
1237 BT_INFO("GATT Service Descriptor Handle: [%d]", event->descrptr_hndl);
1239 if (event->gatt_srvc_stat.status != OAL_STATUS_SUCCESS) {
1240 result = BLUETOOTH_ERROR_INTERNAL;
1241 desc_handle = 0; /* Service handle set to 0 indicates.
1242 0 is reserved by BT SIG, cant be used by app*/
1244 desc_handle = event->descrptr_hndl;
1246 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1247 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1250 if (info->instance_id == event->gatt_srvc_stat.server_inst) {
1251 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1252 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_ADD_DESCRIPTOR,
1253 (int*)&desc_handle, sizeof(int));
1258 _bt_uuid_to_string(&(event->descrptr_uuid), uuid_str);
1259 BT_INFO("GATT Added Descriptor: UUID: [%s]", uuid_str);
1265 static void __bt_handle_gatt_server_service_started(event_gatts_srvc_t *event)
1267 bt_service_app_info_t *info = NULL;
1268 int result = BLUETOOTH_ERROR_NONE;
1270 BT_INFO("GATT Server Service Started..");
1273 BT_INFO("GATT Server_inst: [%d]", event->server_inst);
1274 BT_INFO("GATT Service Handle: [%d]", event->servic_hndl);
1275 BT_INFO("GATT Service Started Status: [%d]", event->status);
1277 if (event->status != OAL_STATUS_SUCCESS) {
1278 BT_ERR("GATT Server Service Start Failed Err: [%d]", event->status);
1279 result = BLUETOOTH_ERROR_INTERNAL;
1282 /* Check if the just registered Instance ID belongs to requester */
1283 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1286 if (info->instance_id == event->server_inst) {
1287 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1288 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_START_SERVICE,
1289 (void*)info, sizeof(bt_service_app_info_t));
1297 static void __bt_handle_gatt_server_service_stopped(event_gatts_srvc_t *event)
1299 int result = BLUETOOTH_ERROR_NONE;
1300 bt_service_app_info_t *info = NULL;
1302 BT_INFO("GATT Server Service Stopped..");
1304 BT_INFO("GATT Server_inst: [%d]", event->server_inst);
1305 BT_INFO("GATT Service Handle: [%d]", event->servic_hndl);
1306 BT_INFO("GATT Service Stopped Status: [%d]", event->status);
1308 if (event->status != OAL_STATUS_SUCCESS) {
1309 BT_ERR("GATT Server Service Stop Failed Err: [%d]", event->status);
1310 result = BLUETOOTH_ERROR_INTERNAL;
1313 /* Check if the just registered Instance ID belongs to requester */
1314 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1317 if (info->instance_id == event->server_inst) {
1318 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1319 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_STOP_SERVICE,
1320 (void*)info, sizeof(bt_service_app_info_t));
1328 static void __bt_handle_gatt_server_service_deleted(event_gatts_srvc_t *event)
1330 int result = BLUETOOTH_ERROR_NONE;
1331 bt_service_app_info_t *info = NULL;
1333 BT_INFO("GATT Server Service Deleted..");
1335 BT_INFO("GATT Server_inst: [%d]", event->server_inst);
1336 BT_INFO("GATT Service Handle: [%d]", event->servic_hndl);
1337 BT_INFO("GATT Service Deleted Status: [%d]", event->status);
1339 if (event->status != OAL_STATUS_SUCCESS) {
1340 BT_ERR("GATT Server Service Delete Failed Err: [%d]", event->status);
1341 result = BLUETOOTH_ERROR_INTERNAL;
1344 /* Check if the just registered Instance ID belongs to requester */
1345 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1348 if (info->instance_id == event->server_inst) {
1349 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1350 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_DELETE_SERVICE,
1351 (void*)info, sizeof(bt_service_app_info_t));
1359 static struct gatt_client_info_t *__bt_find_remote_gatt_client_info(char *address)
1362 struct gatt_client_info_t *info = NULL;
1363 for (l = gatt_client_info_list; l != NULL; l = g_slist_next(l)) {
1364 info = (struct gatt_client_info_t*)l->data;
1368 if (!g_strcmp0(info->addr, address)) {
1369 BT_INFO("Remote GATT client found addr[%s]", info->addr);
1376 static struct gatt_client_info_t *__bt_find_remote_gatt_client_info_from_conn_id(int conn_id)
1379 struct gatt_client_info_t *info = NULL;
1381 for (l = gatt_client_info_list; l != NULL; l = g_slist_next(l)) {
1382 info = (struct gatt_client_info_t*)l->data;
1386 if (info->connection_id == conn_id) {
1387 BT_INFO("Remote GATT client found addr[%s]", info->addr);
1394 #ifdef TIZEN_GATT_CLIENT
1395 static struct gatt_server_info_t *__bt_find_remote_gatt_server_info(char *address)
1398 struct gatt_server_info_t *info = NULL;
1399 for (l = gatt_server_info_list; l != NULL; l = g_slist_next(l)) {
1400 info = (struct gatt_server_info_t*)l->data;
1404 if (!g_strcmp0(info->addr, address)) {
1405 BT_INFO("Remote GATT Server found addr[%s]", info->addr);
1412 static struct gatt_out_conn_info_t* __bt_find_gatt_outgoing_conn_info(char *address)
1415 struct gatt_out_conn_info_t *info = NULL;
1416 for (l = outgoing_gatt_conn_list; l != NULL; l = g_slist_next(l)) {
1417 info = (struct gatt_out_conn_info_t*)l->data;
1421 if (!g_strcmp0(info->addr, address)) {
1422 BT_INFO("Outgoing connection info found addr[%s]", info->addr);
1429 static struct gatt_server_info_t *__bt_find_remote_gatt_server_info_from_client_if(int client_if)
1432 struct gatt_server_info_t *info = NULL;
1434 for (l = gatt_server_info_list; l != NULL; l = g_slist_next(l)) {
1435 info = (struct gatt_server_info_t*)l->data;
1439 if (info->client_id == client_if) {
1440 BT_INFO("Remote GATT server found addr[%s]", info->addr);
1451 #ifdef TIZEN_GATT_CLIENT
1452 static void __bt_handle_gatt_server_connection_state(event_gatts_conn_t *event)
1454 int result = BLUETOOTH_ERROR_NONE;
1455 struct gatt_client_info_t *conn_info = NULL;
1456 bluetooth_device_address_t dev_addr;
1458 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1460 memcpy(dev_addr.addr, event->address.addr, 6);
1462 /* REPLY dbus Context to application which called BT_CONNECT_LE. There is status
1464 _bt_convert_addr_type_to_string(address,
1465 (unsigned char *)dev_addr.addr);
1466 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
1467 address, BT_ADDRESS_STRING_SIZE);
1469 BT_INFO("GATT Server Connedted: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1470 address, event->server_inst, event->conn_id);
1473 /* Check if device is already in connected list */
1474 conn_info = __bt_find_remote_gatt_client_info(address);
1477 BT_INFO("Conn Info absent: But no need to Send Local GATT Server Connected event to apps");
1479 param = g_variant_new("(is)", result, address);
1481 /* Send event to application */
1482 _bt_send_event(BT_DEVICE_EVENT,
1483 BLUETOOTH_EVENT_GATT_SERVER_CONNECTED, /* Local device is GATT server */
1486 /* Save Connection info */
1487 conn_info = g_new0(struct gatt_client_info_t, 1);
1488 conn_info->addr = g_strdup(address);
1489 BT_INFO("Added GATT client addr[%s]", conn_info->addr);
1490 conn_info->connection_id = event->conn_id;
1491 conn_info->instance_id = event->server_inst;
1492 gatt_client_info_list = g_slist_append(gatt_client_info_list, conn_info);
1493 BT_INFO("Total num of connected Remote GATT Clients [%d]", g_slist_length(gatt_client_info_list));
1499 /* GATT Server Dis connected */
1500 static void __bt_handle_gatt_server_disconnection_state(event_gatts_conn_t *event)
1502 int result = BLUETOOTH_ERROR_NONE;
1503 struct gatt_client_info_t *conn_info = NULL;
1504 bluetooth_device_address_t dev_addr;
1506 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1508 memcpy(dev_addr.addr, event->address.addr, 6);
1510 /* REPLY dbus Context to application which called BT_DISCONNECT_LE. There is status
1512 _bt_convert_addr_type_to_string(address,
1513 (unsigned char *)dev_addr.addr);
1515 if (NULL == _bt_get_request_info_data(BT_DISCONNECT_LE, address)) {
1516 if (NULL != _bt_get_request_info_data(BT_CONNECT_LE, address)) {
1517 result = BLUETOOTH_ERROR_INTERNAL;
1519 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
1520 address, BT_ADDRESS_STRING_SIZE);
1522 BT_ERR("Failed to connect Local GATT Server Remote Client addr[%s]", address);
1526 __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE,
1527 address, BT_ADDRESS_STRING_SIZE);
1530 BT_INFO("Local GATT Server DisConnected: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1531 address, event->server_inst, event->conn_id);
1532 /* Remove Connection info */
1533 conn_info = __bt_find_remote_gatt_client_info(address);
1535 BT_INFO("No need to Send Local GATT Server Disconnected event to apps, just remove remote client info");
1537 param = g_variant_new("(is)", result, address);
1538 /* Send event to application */
1539 _bt_send_event(BT_DEVICE_EVENT,
1540 BLUETOOTH_EVENT_GATT_SERVER_DISCONNECTED, /* Local device is GATT server */
1543 /* Remove info from List */
1544 gatt_client_info_list = g_slist_remove(gatt_client_info_list, conn_info);
1545 BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
1546 g_free(conn_info->addr);
1554 static void __bt_handle_gatt_server_connection_state(event_gatts_conn_t *event)
1556 int result = BLUETOOTH_ERROR_NONE;
1557 struct gatt_client_info_t *conn_info = NULL;
1558 bt_service_app_info_t *info = NULL;
1559 bluetooth_device_address_t dev_addr;
1560 GVariant *param = NULL;
1563 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1565 memcpy(dev_addr.addr, event->address.addr, 6);
1568 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1569 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1572 if (info->instance_id == event->server_inst) {
1573 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1574 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
1575 (bluetooth_device_address_t*)&dev_addr, sizeof(bluetooth_device_address_t));
1581 /* Send event to BT-API */
1582 _bt_convert_addr_type_to_string(address,
1583 (unsigned char *)dev_addr.addr);
1585 BT_INFO("GATT Server Connedted: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1586 address, event->server_inst, event->conn_id);
1589 /* Check if device is already in connected list */
1590 conn_info = __bt_find_remote_gatt_client_info(address);
1593 param = g_variant_new("(is)", result, address);
1594 /* Send event to application */
1595 _bt_send_event(BT_GATT_SERVER_EVENT,
1596 BLUETOOTH_EVENT_GATT_CONNECTED,
1599 /* Save Connection info */
1600 conn_info = g_new0(struct gatt_client_info_t, 1);
1601 conn_info->addr = g_strdup(address);
1602 BT_INFO("Added GATT client addr[%s]", conn_info->addr);
1603 conn_info->connection_id = event->conn_id;
1604 conn_info->instance_id = event->server_inst;
1605 gatt_client_info_list = g_slist_append(gatt_client_info_list, conn_info);
1606 BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
1612 /* GATT Server Dis connected */
1613 static void __bt_handle_gatt_server_disconnection_state(event_gatts_conn_t *event)
1615 int result = BLUETOOTH_ERROR_NONE;
1616 struct gatt_client_info_t *conn_info = NULL;
1617 bluetooth_device_address_t dev_addr;
1618 bt_service_app_info_t *info = NULL;
1619 GVariant *param = NULL;
1622 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1624 memcpy(dev_addr.addr, event->address.addr, 6);
1626 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1627 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1630 if (info->instance_id == event->server_inst) {
1631 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1632 __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE,
1633 (bluetooth_device_address_t*)&dev_addr, sizeof(bluetooth_device_address_t));
1639 /* Send event to BT-API */
1640 _bt_convert_addr_type_to_string(address,
1641 (unsigned char *)dev_addr.addr);
1643 BT_INFO("GATT Server DisConnected: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1644 address, event->server_inst, event->conn_id);
1646 param = g_variant_new("(is)", result, address);
1647 /* Send event to application */
1648 _bt_send_event(BT_GATT_SERVER_EVENT,
1649 BLUETOOTH_EVENT_GATT_DISCONNECTED,
1652 /* Remove Connection info */
1653 conn_info = __bt_find_remote_gatt_client_info(address);
1655 BT_INFO("Remove GATT client info from List..");
1656 /* Remove info from List */
1657 gatt_client_info_list = g_slist_remove(gatt_client_info_list, conn_info);
1658 BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
1659 g_free(conn_info->addr);
1667 static void __bt_handle_gatt_server_write_requested(event_gatts_srvc_write_attr_t *event)
1670 bluetooth_device_address_t dev_addr;
1671 GVariant *param = NULL;
1672 int result = BLUETOOTH_ERROR_NONE;
1674 gboolean is_prepare_write;
1676 GVariant *data = NULL;
1678 struct gatt_server_req_info *req_info = NULL;
1679 BT_INFO("GATT Server Write Requested");
1681 memcpy(dev_addr.addr, event->address.addr, 6);
1683 BT_INFO("GATT Server Write Req Connection ID: [%d]", event->attr_trans.conn_id);
1684 BT_INFO("GATT Server Write Req Transaction ID:[%d]", event->attr_trans.trans_id);
1685 BT_INFO("GATT Server Write Req Attribute Handle: [%d]", event->attr_trans.attr_handle);
1686 BT_INFO("GATT Server Write Req Attribute Offset: [%d]", event->attr_trans.offset);
1687 BT_INFO("GATT Server Write Req value length [%d]", event->length);
1688 BT_INFO("GATT Server Write Req needs response: [%d]", event->need_rsp);
1689 BT_INFO("GATT Server Write Req Is Prep: [%d]", event->is_prep);
1691 if (event->length <= 0) {
1692 BT_INFO("GATT Server write requested, but length of attr value is 0");
1697 need_resp = event->need_rsp;
1698 is_prepare_write = event->is_prep;
1700 write_val = g_memdup(&event->value[0], event->length);
1702 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1703 _bt_convert_addr_type_to_string(address,
1704 (unsigned char *)dev_addr.addr);
1706 BT_INFO("GATT Server Write Request from remote client [%s]", address);
1708 if (event->length > 0) {
1709 for (i = 0; i < event->length; i++)
1710 BT_DBG("Data[%d] = [0x%x]", i, event->value[i]);
1712 /* Save Write Request Info */
1713 req_info = g_new0(struct gatt_server_req_info, 1);
1714 req_info->request_id = event->attr_trans.trans_id;
1715 req_info->attribute_handle = event->attr_trans.attr_handle;
1716 req_info->connection_id = event->attr_trans.conn_id;
1717 req_info->addr = address;
1718 req_info->offset = event->attr_trans.offset;
1719 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_WRITE;
1720 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1722 data = g_variant_new_from_data(
1723 G_VARIANT_TYPE_BYTESTRING,
1728 param = g_variant_new("(iiiiiibbsn@ay)", result,
1729 event->attr_trans.conn_id,
1730 event->attr_trans.trans_id,
1731 event->attr_trans.attr_handle,
1732 event->attr_trans.offset,
1740 _bt_send_event(BT_GATT_SERVER_EVENT,
1741 BLUETOOTH_EVENT_GATT_SERVER_VALUE_CHANGED,
1748 static void __bt_handle_gatt_server_read_requested(event_gatts_srvc_read_attr_t *event)
1750 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1751 bluetooth_device_address_t dev_addr;
1752 int result = BLUETOOTH_ERROR_NONE;
1753 struct gatt_server_req_info *req_info = NULL;
1754 GVariant *param = NULL;
1756 BT_INFO("GATT Server Read Requested");
1758 memcpy(dev_addr.addr, event->address.addr, 6);
1760 BT_INFO("GATT Server-Client Connection ID: [%d]", event->attr_trans.conn_id);
1761 BT_INFO("GATT Server-Client Transaction ID: [%d]", event->attr_trans.trans_id);
1762 BT_INFO("GATT Server Attribute Handle: [%d]", event->attr_trans.attr_handle);
1763 BT_INFO("GATT Server Attribute Offset for read: [%d]", event->attr_trans.offset);
1764 BT_INFO("GATT Server Attribute is long: [%d]", event->is_long);
1766 is_long = event->is_long;
1768 /* Save Read Request Info */
1769 req_info = g_new0(struct gatt_server_req_info, 1);
1770 req_info->request_id = event->attr_trans.trans_id;
1771 req_info->attribute_handle = event->attr_trans.attr_handle;
1772 req_info->connection_id = event->attr_trans.conn_id;
1773 req_info->addr = address;
1774 req_info->offset = event->attr_trans.offset;
1775 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_READ;
1776 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1778 /* Send event to BT-API */
1779 _bt_convert_addr_type_to_string(address,
1780 (unsigned char *)dev_addr.addr);
1782 BT_INFO("GATT Server Read Request from remote client [%s]", address);
1784 param = g_variant_new("(iiiiibs)", result,
1785 event->attr_trans.conn_id,
1786 event->attr_trans.trans_id,
1787 event->attr_trans.attr_handle,
1788 event->attr_trans.offset,
1792 _bt_send_event(BT_GATT_SERVER_EVENT,
1793 BLUETOOTH_EVENT_GATT_SERVER_READ_REQUESTED,
1799 static void __bt_handle_gatt_server_indicate_confirmed(event_gatts_ind_cnfrm_t *event)
1801 bluetooth_device_address_t dev_addr;
1802 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1803 int cur_connected_clients;
1804 static int recvd = 0;
1805 gboolean completed = 0;
1806 GVariant *param = NULL;
1808 /* OAL event does provide error, so MW assumes event will never contain wrong data,
1809 incase of any issues, check with OAL */
1810 int result = BLUETOOTH_ERROR_NONE;
1812 memcpy(dev_addr.addr, event->address.addr, 6);
1813 _bt_convert_addr_type_to_string(address,
1814 (unsigned char *)dev_addr.addr);
1816 BT_INFO("Indication sent to GATT client [%s] conn_ ID [%d] transaction ID [%d] Att handle [%d]",
1817 address, event->conn_id, event->trans_id, event->attr_handle);
1820 cur_connected_clients = g_slist_length(gatt_client_info_list);
1821 BT_INFO("Number of connected clients during sending Indication [%d] & current connected count [%d]",
1822 num_indicate_clients, cur_connected_clients);
1825 if (recvd == num_indicate_clients) {
1826 BT_INFO("Gatt indication confirm event for last GATT client.. [%s]", address);
1827 completed = 1; /* Last event */
1828 recvd = 0; /* Reset */
1829 num_indicate_clients = 0;
1832 param = g_variant_new("(isib)",
1838 /* Send event to BT-API */
1839 _bt_send_event(BT_GATT_SERVER_EVENT,
1840 BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_COMPLETED,
1843 BT_INFO("Received Indication confirm for client number [%d]", recvd);
1848 /* Tizen Platform Specific */
1849 static void __bt_handle_gatt_server_notification_changed(event_gatts_notif_t *event)
1851 bluetooth_device_address_t dev_addr;
1852 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1853 GVariant *param = NULL;
1856 /* OAL event does provide error, so MW assumes event will never contain wrong data,
1857 incase of any issues, check with OAL */
1858 int result = BLUETOOTH_ERROR_NONE;
1860 memcpy(dev_addr.addr, event->address.addr, 6);
1861 _bt_convert_addr_type_to_string(address,
1862 (unsigned char *)dev_addr.addr);
1864 BT_INFO("notification_changed [%s] conn_ ID [%d] transaction ID [%d] Att handle [%d] Notify[%d]",
1865 address, event->conn_id, event->trans_id, event->attr_handle, event->notify);
1867 /* Set Notifcation status */
1868 notify = event->notify;
1870 param = g_variant_new("(isib)",
1876 /* Send event to BT-API */
1877 _bt_send_event(BT_GATT_SERVER_EVENT,
1878 BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_STATE_CHANGED,
1885 static void __bt_handle_gatt_mtu_changed_event(event_gatts_mtu_changed_t *event)
1887 int result = BLUETOOTH_ERROR_NONE;
1888 struct gatt_client_info_t *conn_info = NULL;
1889 GVariant *param = NULL;
1891 BT_INFO("GATT Server MTU changed event");
1893 conn_info = __bt_find_remote_gatt_client_info_from_conn_id(event->conn_id);
1894 if (conn_info == NULL) {
1895 BT_ERR("Cant find connection Information");
1899 BT_INFO("Got connection Info GATT client [%s] MTU Size [%d]",
1900 conn_info->addr, event->mtu_size);
1902 param = g_variant_new("(isqy)",
1908 /* Send event to BT-API */
1909 _bt_send_event(BT_GATT_SERVER_EVENT,
1910 BLUETOOTH_EVENT_GATT_ATT_MTU_CHANGED,
1915 static void __bt_gatt_event_handler(int event_type, gpointer event_data)
1917 BT_INFO("OAL event = 0x%x, \n", event_type);
1918 switch (event_type) {
1919 case OAL_EVENT_BLE_SERVER_INSTANCE_INITIALISED: {
1920 BT_INFO("OAL Event: Server Instance Registered");
1921 event_gatts_register_t* event = g_memdup(event_data, sizeof(event_gatts_register_t));
1922 /* GATT Server Registered event is handled in MAIN thread context */
1923 __bt_handle_server_instance_registered(event);
1926 case OAL_EVENT_GATTS_SERVICE_ADDED: {
1927 BT_INFO("OAL Event: GATT Service added");
1928 event_gatts_srvc_prm_t *service_parm = g_memdup(event_data, sizeof(event_gatts_srvc_prm_t));
1929 __bt_handle_gatt_server_service_added(service_parm);
1932 case OAL_EVENT_GATTS_CHARACTERISTIC_ADDED: {
1933 BT_INFO("OAL Event: GATT characteristic added");
1934 event_gatts_srvc_charctr_t *char_parm = g_memdup(event_data, sizeof(event_gatts_srvc_charctr_t));
1935 __bt_handle_gatt_server_characteristic_added(char_parm);
1938 case OAL_EVENT_GATTS_DESCRIPTOR_ADDED: {
1939 BT_INFO("OAL Event: GATT descriptor added");
1940 event_gatts_srvc_descr_t *desc_parm = g_memdup(event_data, sizeof(event_gatts_srvc_descr_t));
1941 __bt_handle_gatt_server_descriptor_added(desc_parm);
1944 case OAL_EVENT_GATTS_SERVICE_STARTED: {
1945 BT_INFO("OAL Event: GATT Service started");
1946 event_gatts_srvc_t *svc_started = g_memdup(event_data, sizeof(event_gatts_srvc_t));
1947 __bt_handle_gatt_server_service_started(svc_started);
1950 case OAL_EVENT_GATTS_SERVICE_STOPED: {
1951 BT_INFO("OAL Event: GATT Service stopped");
1952 event_gatts_srvc_t *svc_stopped = g_memdup(event_data, sizeof(event_gatts_srvc_t));
1953 __bt_handle_gatt_server_service_stopped(svc_stopped);
1956 case OAL_EVENT_GATTS_SERVICE_DELETED: {
1957 BT_INFO("OAL Event: GATT Service deleted");
1958 event_gatts_srvc_t *svc_deleted = g_memdup(event_data, sizeof(event_gatts_srvc_t));
1959 __bt_handle_gatt_server_service_deleted(svc_deleted);
1962 case OAL_EVENT_GATTS_CONNECTION_COMPLETED: {
1963 BT_INFO("OAL Event: GATT Server Connected");
1964 event_gatts_conn_t* event = g_memdup(event_data, sizeof(event_gatts_conn_t));
1965 __bt_handle_gatt_server_connection_state(event);
1968 case OAL_EVENT_GATTS_DISCONNECTION_COMPLETED: {
1969 BT_INFO("OAL Event: GATT Server Disconnected");
1970 event_gatts_conn_t* event = g_memdup(event_data, sizeof(event_gatts_conn_t));
1971 __bt_handle_gatt_server_disconnection_state(event);
1974 case OAL_EVENT_GATTS_REQUEST_READ: {
1975 BT_INFO("OAL Event: GATT Server Read Request");
1976 event_gatts_srvc_read_attr_t *read_req = g_memdup(event_data, sizeof(event_gatts_srvc_read_attr_t));
1977 __bt_handle_gatt_server_read_requested(read_req);
1980 case OAL_EVENT_GATTS_REQUEST_WRITE: {
1981 BT_INFO("OAL Event: GATT Server Write Request");
1982 event_gatts_srvc_write_attr_t *write_req = g_memdup(event_data, sizeof(event_gatts_srvc_write_attr_t));
1983 __bt_handle_gatt_server_write_requested(write_req);
1986 case OAL_EVENT_GATTS_IND_CONFIRM: {
1987 BT_INFO("OAL Event: GATT Server Indication confirmed");
1988 event_gatts_ind_cnfrm_t *parm = g_memdup(event_data, sizeof(event_gatts_ind_cnfrm_t));
1989 __bt_handle_gatt_server_indicate_confirmed(parm);
1992 case OAL_EVENT_GATTS_NOTIFICATION: { /* Tizen Platform Specific */
1993 BT_INFO("OAL Event: GATT Server DisConnected");
1994 event_gatts_notif_t* notif = g_memdup(event_data, sizeof(event_gatts_notif_t));
1995 __bt_handle_gatt_server_notification_changed(notif);
1998 case OAL_EVENT_GATTS_MTU_CHANGED: {
1999 BT_INFO("OAL Event: GATT Server MTU changed event callback");
2000 event_gatts_mtu_changed_t *mtu_changed = g_memdup(event_data, sizeof(event_gatts_mtu_changed_t));
2001 __bt_handle_gatt_mtu_changed_event(mtu_changed);
2004 #ifdef TIZEN_GATT_CLIENT
2005 case OAL_EVENT_GATTC_REGISTRATION: {
2006 BT_INFO("OAL Event: GATT Client instance Registered");
2007 __bt_handle_client_instance_registered((event_gattc_register_t *) event_data);
2010 case OAL_EVENT_GATTC_CONNECTION_COMPLETED: {
2011 BT_INFO("OAL Event: GATT Client Connected");
2012 __bt_handle_client_connected((event_gattc_conn_t *) event_data);
2015 case OAL_EVENT_GATTC_DISCONNECTION_COMPLETED: {
2016 BT_INFO("OAL Event: GATT Client DisConnected");
2017 __bt_handle_client_disconnected((event_gattc_conn_t *) event_data);
2020 case OAL_EVENT_GATTC_SERVICE_SEARCH_RESULT: {
2021 BT_INFO("OAL Event: GATT Client Service Search Result");
2022 __bt_handle_client_service_search_result((event_gattc_service_result_t *) event_data);
2025 case OAL_EVENT_GATTC_SERVICE_SEARCH_DONE: {
2026 BT_INFO("OAL Event: GATT Client Service Completed");
2027 __bt_handle_client_service_search_completed((event_gattc_conn_status_t *) event_data);
2030 case OAL_EVENT_GATTC_CHARAC_SERACH_RESULT: {
2031 BT_INFO("OAL Event: GATT Client Characteristic Search Result");
2032 __bt_handle_client_characteristic_search_result((event_gattc_characteristic_result_t *) event_data);
2035 case OAL_EVENT_GATTC_DESC_SERACH_RESULT: {
2036 BT_INFO("OAL Event: GATT Client Descriptor Search Result");
2037 __bt_handle_client_descriptor_search_result((event_gattc_descriptor_result_t *) event_data);
2040 case OAL_EVENT_GATTC_READ_CHARAC: {
2041 BT_INFO("OAL Event: GATT Client Characteristic Read Data");
2042 __bt_handle_client_characteristic_read_data((event_gattc_read_data *) event_data);
2045 case OAL_EVENT_GATTC_READ_DESCR: {
2046 BT_INFO("OAL Event: GATT Client Descriptor Read Data");
2047 __bt_handle_client_descriptor_read_data((event_gattc_read_data *) event_data);
2050 case OAL_EVENT_GATTC_WRITE_CHARAC: {
2051 BT_INFO("OAL Event: GATT Client Characteristic Write Data");
2052 __bt_handle_client_characteristic_write_data((event_gattc_write_data *) event_data);
2055 case OAL_EVENT_GATTC_WRITE_DESCR: {
2056 BT_INFO("OAL Event: GATT Client Descriptor Write Data");
2057 __bt_handle_client_descriptor_write_data((event_gattc_write_data *) event_data);
2060 case OAL_EVENT_DEVICE_LE_DISCONNECTED: {
2061 BT_INFO("OAL Event: LE device disconnected");
2062 __bt_hanlde_le_device_disconnection((event_dev_conn_status_t *)event_data);
2065 case OAL_EVENT_GATTC_NOTIFICATION_REGISTERED: {
2066 BT_INFO("OAL Event: GATT Client Notification Registered");
2067 __bt_handle_client_notification_registered((event_gattc_regdereg_notify_t *) event_data, TRUE);
2070 case OAL_EVENT_GATTC_NOTIFICATION_DEREGISTERED: {
2071 BT_INFO("OAL Event: GATT Client Notification Registered");
2072 __bt_handle_client_notification_registered((event_gattc_regdereg_notify_t *) event_data, FALSE);
2082 int _bt_gatt_server_add_service(char *sender, int service_type,
2083 int num_handles, char *svc_uuid, int instance_id)
2085 BT_CHECK_PARAMETER(svc_uuid, return);
2086 BT_CHECK_PARAMETER(sender, return);
2087 int ret = OAL_STATUS_SUCCESS;
2089 oal_gatt_srvc_id_t svc_data;
2091 svc_data.is_prmry = service_type;
2092 svc_data.id.inst_id = instance_id;
2094 BT_INFO("Service UUID [%s] Num handles [%d] Instance ID [%d]", svc_uuid, num_handles, instance_id);
2095 _bt_string_to_uuid(svc_uuid, (service_uuid_t*)&svc_data.id.uuid);
2097 ret = gatts_add_service(instance_id, &svc_data, num_handles);
2098 if (ret != OAL_STATUS_SUCCESS) {
2099 BT_ERR("ret: %d", ret);
2100 return BLUETOOTH_ERROR_INTERNAL;
2103 return BLUETOOTH_ERROR_NONE;
2107 int _bt_gatt_server_add_included_service(char *sender, int instance_id,
2108 int service_handle, int included_svc_handle)
2110 BT_CHECK_PARAMETER(sender, return);
2111 int ret = OAL_STATUS_SUCCESS;
2113 ret = gatts_add_included_services(instance_id, service_handle, included_svc_handle);
2114 if (ret != OAL_STATUS_SUCCESS) {
2115 BT_ERR("ret: %d", ret);
2116 return BLUETOOTH_ERROR_INTERNAL;
2118 return BLUETOOTH_ERROR_NONE;
2121 int _bt_gatt_server_add_characteristic(char *sender, char *char_uuid,
2122 bluetooth_gatt_server_attribute_params_t *param)
2124 BT_CHECK_PARAMETER(char_uuid, return);
2125 BT_CHECK_PARAMETER(sender, return);
2126 BT_CHECK_PARAMETER(param, return);
2127 int ret = OAL_STATUS_SUCCESS;
2129 oal_uuid_t uuid = {{0} };
2131 BT_INFO("Char UUID [%s] Instance ID [%d]", char_uuid, param->instance_id);
2132 _bt_string_to_uuid(char_uuid, (service_uuid_t*)&uuid);
2134 BT_INFO("Char permission From API [0x%x]", param->permissions);
2136 ret = gatts_add_characteristics(param->instance_id, param->service_handle, &uuid,
2137 param->properties, (int)param->permissions);
2138 if (ret != OAL_STATUS_SUCCESS) {
2139 BT_ERR("ret: %d", ret);
2140 return BLUETOOTH_ERROR_INTERNAL;
2142 return BLUETOOTH_ERROR_NONE;
2145 int _bt_gatt_server_add_descriptor(char *sender, char *desc_uuid,
2146 bt_gatt_permission_t *param, int service_handle, int instance_id)
2148 BT_CHECK_PARAMETER(desc_uuid, return);
2149 BT_CHECK_PARAMETER(sender, return);
2150 BT_CHECK_PARAMETER(param, return);
2151 int ret = OAL_STATUS_SUCCESS;
2153 oal_uuid_t uuid = {{0} };
2155 BT_INFO("Descriptor UUID [%s] Instance ID [%d] Service handle [%d]",
2156 desc_uuid, service_handle, instance_id);
2158 _bt_string_to_uuid(desc_uuid, (service_uuid_t*)&uuid);
2160 BT_INFO("Descriptor permission From API [0x%x]", *param);
2161 ret = gatts_add_descriptor(instance_id, service_handle, &uuid, (int)*param);
2163 if (ret != OAL_STATUS_SUCCESS) {
2164 BT_ERR("ret: %d", ret);
2165 return BLUETOOTH_ERROR_INTERNAL;
2167 return BLUETOOTH_ERROR_NONE;
2170 int _bt_gatt_server_start_service(char *sender, int service_handle, int instance_id)
2172 BT_CHECK_PARAMETER(sender, return);
2173 int ret = OAL_STATUS_SUCCESS;
2175 ret = gatts_start_service(instance_id, service_handle, BT_GATT_TRANSPORT_LE);
2176 if (ret != OAL_STATUS_SUCCESS) {
2177 BT_ERR("ret: %d", ret);
2178 return BLUETOOTH_ERROR_INTERNAL;
2180 return BLUETOOTH_ERROR_NONE;
2183 int _bt_gatt_server_stop_service(char *sender, int service_handle, int instance_id)
2185 BT_CHECK_PARAMETER(sender, return);
2186 int ret = OAL_STATUS_SUCCESS;
2188 ret = gatts_stop_service(instance_id, service_handle);
2189 if (ret != OAL_STATUS_SUCCESS) {
2190 BT_ERR("ret: %d", ret);
2191 return BLUETOOTH_ERROR_INTERNAL;
2193 return BLUETOOTH_ERROR_NONE;
2196 int _bt_gatt_server_delete_service(char *sender, int service_handle, int instance_id)
2198 BT_CHECK_PARAMETER(sender, return);
2199 int ret = OAL_STATUS_SUCCESS;
2203 ret = gatts_delete_service(instance_id, service_handle);
2204 if (ret != OAL_STATUS_SUCCESS) {
2205 BT_ERR("ret: %d", ret);
2206 return BLUETOOTH_ERROR_INTERNAL;
2209 /* Remove the Service Handle */
2210 for (l = numapps[instance_id].service_handles; l != NULL;) {
2212 l = g_slist_next(l);
2213 if (handle && *handle == service_handle) {
2214 BT_INFO("Remove Service handle [%d]", *handle);
2215 numapps[instance_id].service_handles = g_slist_remove(numapps[instance_id].service_handles, handle);
2221 return BLUETOOTH_ERROR_NONE;
2224 int _bt_gatt_server_send_response(char *sender, bluetooth_gatt_att_data_t *data,
2225 bluetooth_gatt_server_response_params_t *param)
2227 BT_CHECK_PARAMETER(sender, return);
2228 BT_CHECK_PARAMETER(data, return);
2229 BT_CHECK_PARAMETER(param, return);
2230 struct gatt_server_req_info *req_info = NULL;
2231 int ret = OAL_STATUS_SUCCESS;
2233 oal_gatt_response_t response;
2235 BT_INFO("GATT Server Response: Req Type [%d] req_id [%d] status [%d] auth_req [%d] offset[%d] data len[%d]",
2236 param->req_type, param->request_id,
2237 param->response_status, param->auth_req,
2238 data->offset, data->length);
2240 /* Search for matching Request in List */
2241 req_info = __bt_gatt_server_find_request_info(param->request_id, param->req_type);
2243 BT_ERR("GATT Server Req Info not found for current response..return Error");
2244 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
2247 memset(&response, 0x00, sizeof(oal_gatt_response_t));
2249 response.handle = req_info->attribute_handle;
2250 response.attr_value.auth_req = param->auth_req;
2251 response.attr_value.handle = req_info->attribute_handle;
2252 response.attr_value.offset = data->offset;
2253 response.attr_value.len = data->length;
2254 memcpy(&response.attr_value.value, &data->data, data->length);
2257 ret = gatts_send_response(req_info->connection_id, param->request_id,
2258 param->response_status, &response);
2260 if (ret != OAL_STATUS_SUCCESS) {
2261 BT_ERR("ret: %d", ret);
2262 return BLUETOOTH_ERROR_INTERNAL;
2265 BT_INFO("GATT Server Response successfully sent");
2266 /* Remove GATT server request from list */
2267 gatt_server_requests = g_slist_remove(gatt_server_requests, req_info);
2268 g_free(req_info->addr);
2270 return BLUETOOTH_ERROR_NONE;
2273 int _bt_gatt_server_send_indication(char *sender, bluetooth_device_address_t *dev_addr,
2274 bluetooth_gatt_att_data_t *data,
2275 bluetooth_gatt_server_indication_params_t *param)
2277 BT_CHECK_PARAMETER(sender, return);
2278 BT_CHECK_PARAMETER(data, return);
2279 BT_CHECK_PARAMETER(param, return);
2281 gboolean all_send = FALSE;
2282 int ret = OAL_STATUS_SUCCESS;
2283 struct gatt_client_info_t *conn;
2285 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2286 _bt_convert_addr_type_to_string(address, dev_addr->addr);
2288 if (memcmp(dev_addr->addr, BDADDR_ANY, 6) == 0) {
2289 BT_INFO("GATT Server: Send Indication to all connected GATT clients..");
2292 BT_INFO("GATT Server: Send Indication to connected GATT client addr [%s]", address);
2295 /* Attempt to send Notification/Indication to all Connected GATT clients */
2297 ret = __bt_gatt_send_indication_to_all_connected_clients(data, param);
2298 if (ret != OAL_STATUS_SUCCESS) {
2299 BT_ERR("ret: %d", ret);
2301 return BLUETOOTH_ERROR_INTERNAL;
2305 conn = __bt_find_remote_gatt_client_info(address);
2307 ret = gatts_send_indication(param->instance_id, param->atrribute_handle,
2308 conn->connection_id, data->length,
2309 param->need_confirmation, (char *)(&data->data[0]));
2311 if (ret != OAL_STATUS_SUCCESS) {
2312 BT_ERR("ret: %d", ret);
2313 BT_INFO("Indication failed to send to Remote GATT Client [%s]", address);
2315 return BLUETOOTH_ERROR_INTERNAL;
2317 BT_INFO("Indication sent to Remote GATT Client [%s] wait for Notification completed event from OAL", address);
2319 num_indicate_clients = 1;
2320 return BLUETOOTH_ERROR_NONE;
2322 BT_ERR("Remote GATT client [%s] is not connected..Cant send Indication!!", address);
2324 return BLUETOOTH_ERROR_NOT_CONNECTED;
2327 return BLUETOOTH_ERROR_NONE;
2330 int _bt_gatt_server_update_attribute_value(char *sender, int instance_id,
2331 bluetooth_gatt_server_update_value_t *param)
2333 BT_CHECK_PARAMETER(sender, return);
2334 BT_CHECK_PARAMETER(param, return);
2335 int ret = OAL_STATUS_SUCCESS;
2337 oal_gatt_value_t value;
2338 BT_INFO("GATT Server Update value: Instance ID [%d] attr handle [%d] Value len [%d]",
2339 instance_id, param->attribute_handle, param->length);
2342 memset(&value, 0x00, sizeof(oal_gatt_value_t));
2344 value.handle = param->attribute_handle;
2345 value.len = param->length;
2346 memcpy(&value.value, ¶m->data.data, param->length);
2348 ret = gatts_update_att_value(instance_id, &value);
2350 if (ret != OAL_STATUS_SUCCESS) {
2351 BT_ERR("ret: %d", ret);
2352 return BLUETOOTH_ERROR_INTERNAL;
2355 BT_INFO("GATT Server Update Attribute Value successfully");
2356 return BLUETOOTH_ERROR_NONE;
2359 int _bt_get_att_mtu(bluetooth_device_address_t *address,
2362 BT_CHECK_PARAMETER(address, return);
2363 BT_CHECK_PARAMETER(mtu, return);
2364 struct gatt_client_info_t *conn_info = NULL;
2365 char addr[BT_ADDRESS_STRING_SIZE] = { 0 };
2366 int ret = OAL_STATUS_SUCCESS;
2369 _bt_convert_addr_type_to_string(addr, address->addr);
2371 BT_INFO("Get current MTU size for the remote client:DevAddress:[%s]", addr);
2373 conn_info = __bt_find_remote_gatt_client_info(addr);
2375 BT_INFO("GATT Client [%s] is connected, conn Id [%d] Instance ID [%d]",
2376 conn_info->addr, conn_info->connection_id, conn_info->instance_id);
2378 BT_ERR("GATT Client [%s] is not yet connected..");
2379 return BLUETOOTH_ERROR_NOT_CONNECTED;
2382 ret = gatts_get_att_mtu(conn_info->connection_id, &stack_mtu);
2383 if (ret != OAL_STATUS_SUCCESS) {
2384 BT_ERR("ret: %d", ret);
2385 return BLUETOOTH_ERROR_INTERNAL;
2387 BT_INFO("ATT MTU received from OAL [%d]", stack_mtu);
2388 *mtu = (unsigned int)stack_mtu;
2389 return BLUETOOTH_ERROR_NONE;
2392 #ifdef TIZEN_GATT_CLIENT
2393 /* GATT Client utility static functions */
2394 static bt_gatt_service_info_list_t * __bt_get_service_info_list(int conn_id)
2397 bt_gatt_service_info_list_t *info = NULL;
2399 for (l = list_gatt_info; l != NULL; l = g_slist_next(l)) {
2400 info = (bt_gatt_service_info_list_t*)l->data;
2404 if (info->conn_id == conn_id)
2411 static bt_gatt_service_info_t* __bt_find_matching_service(
2412 bt_gatt_service_info_list_t *svc_list, oal_gatt_srvc_id_t *svc)
2415 bt_gatt_service_info_t *info = NULL;
2417 for (l = svc_list->services; l != NULL; l = g_slist_next(l)) {
2418 info = (bt_gatt_service_info_t*)l->data;
2422 /* Match UUID and instance ID */
2423 if (!memcmp(&svc->id.uuid.uuid, &info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
2424 && (svc->id.inst_id == info->inst_id)) {
2431 static bt_gatt_char_info_t* __bt_find_matching_charc(
2432 bt_gatt_service_info_t *svc_info, oal_gatt_id_t *charc)
2435 bt_gatt_char_info_t *info = NULL;
2437 for (l = svc_info->chars; l != NULL; l = g_slist_next(l)) {
2438 info = (bt_gatt_char_info_t*)l->data;
2442 /* Match UUID and instance ID */
2443 if (!memcmp(&charc->uuid.uuid, &info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
2444 && (charc->inst_id == info->inst_id)) {
2451 static bt_gatt_descriptor_info_t* __bt_find_matching_desc(
2452 bt_gatt_char_info_t *char_info, oal_gatt_id_t *desc)
2455 bt_gatt_descriptor_info_t *info = NULL;
2457 for (l = char_info->descs; l != NULL; l = g_slist_next(l)) {
2458 info = (bt_gatt_descriptor_info_t*)l->data;
2462 /* Match UUID and instance ID */
2463 if (!memcmp(&desc->uuid, &info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
2464 && (desc->inst_id == info->inst_id)) {
2472 static struct gatt_server_info_t *__bt_find_remote_gatt_server_info_from_conn_id(int conn_id)
2475 struct gatt_server_info_t *info = NULL;
2477 for (l = gatt_server_info_list; l != NULL; l = g_slist_next(l)) {
2478 info = (struct gatt_server_info_t*)l->data;
2482 if (info->connection_id == conn_id) {
2483 BT_INFO("Remote GATT server found addr[%s]", info->addr);
2490 static bt_gatt_service_info_t* __bt_find_removed_service(bt_gatt_service_info_list_t *svc_list)
2493 bt_gatt_service_info_t *info = NULL;
2495 for (l = svc_list->services; l != NULL; l = g_slist_next(l)) {
2496 info = (bt_gatt_service_info_t*)l->data;
2500 /* Service is marked a removed */
2501 if (info->is_removed == 1)
2507 static void __bt_remove_service_info_from_list(bt_gatt_service_info_t *svc_info)
2513 bt_gatt_char_info_t *charc = NULL;
2514 bt_gatt_included_service_info_t *incl = NULL;
2515 bt_gatt_descriptor_info_t *desc = NULL;
2517 /* Remove all Characteristic and Descriptors within characteristic */
2518 for (l = svc_info->chars; l != NULL;) {
2519 charc = (bt_gatt_char_info_t*)l->data;
2520 l = g_slist_next(l); /* Incase if l is removed, saving next to l */
2525 /* Inside Characteristic */
2526 for (l1 = charc->descs; l1 != NULL;) {
2528 desc = (bt_gatt_descriptor_info_t*)l1->data;
2529 l1 = g_slist_next(l1);
2534 /* Remove Descriptor */
2535 charc->descs = g_slist_remove(charc->descs, desc);
2538 /* Remove Characteristic */
2539 svc_info->chars = g_slist_remove(svc_info->chars, charc);
2543 /* Remove all Included Services */
2544 for (l2 = svc_info->included_svcs; l2 != NULL;) {
2545 incl = (bt_gatt_included_service_info_t*)l2->data;
2546 l2 = g_slist_next(l2); /* Incase if l is removed, saving next to l */
2551 /* Remove included service */
2552 svc_info->included_svcs = g_slist_remove(svc_info->included_svcs, incl);
2559 static void __bt_build_service_browse_info(int conn_id,
2560 bt_services_browse_info_t* info)
2563 bt_gatt_service_info_list_t *svc_info_list;
2564 bt_gatt_service_info_t *svc_info;
2566 service_uuid_t uuid;
2567 struct gatt_server_info_t *conn_info = NULL;
2569 char uuid_string[BLUETOOTH_UUID_STRING_MAX];
2572 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
2577 _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
2579 svc_info_list = __bt_get_service_info_list(conn_id);
2584 info->count = g_slist_length(svc_info_list->services);
2585 BT_INFO("Total services present in the svc info list for this conn id [%d] is [%d]",
2586 conn_id, info->count);
2588 for (l = svc_info_list->services; l != NULL; l = g_slist_next(l)) {
2589 svc_info = (bt_gatt_service_info_t*)l->data;
2590 if (svc_info == NULL)
2593 memcpy(&uuid.uuid, &svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2594 _bt_uuid_to_string(&uuid, uuid_string);
2596 BT_INFO("Service UUID formed [%s] strlen [%d]", uuid_string, strlen(uuid_string));
2598 /* Fill UUID of service */
2599 g_strlcpy(info->uuids[count], uuid_string,
2600 BLUETOOTH_UUID_STRING_MAX);
2602 BT_INFO("Service UUID formed TO be sent [%s] strlen [%d]",
2603 info->uuids[count], strlen(info->uuids[count]));
2604 /* Fill instance ID of service */
2605 info->inst_id[count] = svc_info->inst_id;
2607 /* Fill primary service or not info */
2608 info->primary[count] = svc_info->is_primary;
2610 /* Increment count of services browsed */
2614 BT_INFO("Total services browsed [%d]", count);
2617 static void __bt_build_char_browse_info(int conn_id,
2618 bt_gatt_service_info_t *svc_info,
2619 bt_char_browse_info_t* info)
2622 bt_gatt_char_info_t *char_info;
2623 service_uuid_t uuid;
2625 struct gatt_server_info_t *conn_info = NULL;
2627 char uuid_string[BLUETOOTH_UUID_STRING_MAX];
2631 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
2635 /* Fill default data, this will be required even in case of failure */
2636 _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
2637 memcpy(&info->svc_uuid, svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2638 info->svc_inst_id = svc_info->inst_id;
2640 if (!svc_info->chars) {
2641 BT_ERR("No Chars browsed for address [%s]", conn_info->addr);
2645 info->count = g_slist_length(svc_info->chars);
2646 BT_INFO("Total count of Characteristics [%d]", info->count);
2648 for (l = svc_info->chars; l != NULL; l = g_slist_next(l)) {
2649 char_info = (bt_gatt_char_info_t*)l->data;
2650 if (char_info == NULL)
2653 memcpy(&uuid.uuid, &char_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2654 _bt_uuid_to_string(&uuid, uuid_string);
2656 /* Fill UUID of characteristic */
2657 g_strlcpy(info->uuids[count], uuid_string,
2658 BLUETOOTH_UUID_STRING_MAX);
2660 /* Fill instance ID of characteristic */
2661 info->inst_id[count] = char_info->inst_id;
2663 /* Fill property of characteristic */
2664 info->props[count] = char_info->props;
2666 /* Increment count of services browsed */
2669 BT_INFO("Total characteristics browsed [%d]", count);
2672 static void __bt_build_descriptor_browse_info(int conn_id,
2673 bt_gatt_service_info_t *svc_info,
2674 bt_gatt_char_info_t *char_info,
2675 bt_descriptor_browse_info_t* info)
2678 bt_gatt_descriptor_info_t *desc_info;
2680 service_uuid_t uuid;
2681 struct gatt_server_info_t *conn_info = NULL;
2683 char uuid_string[BLUETOOTH_UUID_STRING_MAX];
2685 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
2687 /* Fill default data, this will be required even in case of failure */
2688 _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
2689 memcpy(&info->svc_uuid, svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2690 info->svc_inst_id = svc_info->inst_id;
2691 memcpy(&info->char_uuid, char_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2692 info->char_inst_id = char_info->inst_id;
2694 /* Fill property of the parent characteristic of this descriptor */
2695 info->char_props_map = char_info->props;
2697 info->count = g_slist_length(char_info->descs);
2698 BT_INFO("Total count of Descriptors [%d]", info->count);
2700 if (!char_info->descs) {
2701 BT_ERR("No Descriptors browsed for address [%s]", conn_info->addr);
2705 for (l = char_info->descs; l != NULL; l = g_slist_next(l)) {
2706 desc_info = (bt_gatt_descriptor_info_t*)l->data;
2707 if (desc_info == NULL)
2710 memcpy(&uuid.uuid, &desc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2711 _bt_uuid_to_string(&uuid, uuid_string);
2713 /* Fill UUID of Descriptor */
2714 g_strlcpy(info->uuids[count], uuid_string,
2715 BLUETOOTH_UUID_STRING_MAX);
2717 /* Fill instance ID of Descriptor */
2718 info->inst_id[count] = desc_info->inst_id;
2721 /* Increment count of Descriptor browsed */
2725 BT_INFO("Total descriptors browsed [%d]", count);
2729 static void __bt_cleanup_remote_services(struct gatt_server_info_t *conn_info)
2736 bt_gatt_service_info_list_t * svc_info_list = NULL;
2737 bt_gatt_service_info_t *svc = NULL;
2738 bt_gatt_char_info_t *chr = NULL;
2739 bt_gatt_descriptor_info_t *desc = NULL;
2740 bt_gatt_included_service_info_t *incl_svc = NULL;
2742 BT_INFO("Start Cleanup of all services");
2744 svc_info_list = __bt_get_service_info_list(conn_info->connection_id);
2745 if (!svc_info_list) {
2746 BT_INFO("Could not find Svc Info list for the connection ID [%d]",
2747 conn_info->connection_id);
2751 BT_INFO("Num Services [%d]", g_slist_length(svc_info_list->services));
2752 for (l = svc_info_list->services; l;) {
2753 svc = (bt_gatt_service_info_t*)l->data;
2754 l = g_slist_next(l);
2758 BT_INFO("Service info Is Prim[%d] Inst ID [%d]",
2759 svc->is_primary, svc->inst_id);
2760 BT_INFO("Num chars [%d]", g_slist_length(svc->chars));
2761 /* Delete all chars and its descriptors */
2762 for (ll = svc->chars; ll;) {
2763 chr = (bt_gatt_char_info_t*)ll->data;
2764 ll = g_slist_next(ll);
2768 BT_INFO("Num descs [%d]", g_slist_length(chr->descs));
2769 for (lll = chr->descs; lll;) {
2770 desc = (bt_gatt_descriptor_info_t *)lll->data;
2771 lll = g_slist_next(lll);
2774 chr->descs = g_slist_remove(chr->descs, desc);
2777 svc->chars = g_slist_remove(svc->chars, chr);
2781 BT_INFO("Num incl svcs [%d]", g_slist_length(svc->included_svcs));
2782 /* Delete all included services */
2783 for (llll = svc->included_svcs; llll;) {
2784 incl_svc = (bt_gatt_included_service_info_t*)llll->data;
2785 llll = g_slist_next(llll);
2786 if (incl_svc == NULL)
2789 svc->included_svcs = g_slist_remove(svc->included_svcs, incl_svc);
2792 svc_info_list->services = g_slist_remove(svc_info_list->services, svc);
2795 g_free(svc_info_list);
2796 BT_INFO("Cleanup of all services done");
2799 int _bt_register_gatt_client_instance(const char *sender,
2800 bluetooth_device_address_t *address)
2802 int ret = OAL_STATUS_SUCCESS;
2803 char *uuid_string = NULL;
2808 /* App should ensure that it should not send */
2809 BT_INFO("###Check on which instance GATT Client instance can be initialized....");
2810 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
2811 if (numapps[k].is_initialized == 1) {
2812 BT_INFO("Instance ID [%d] is already in use..Check next slot",
2813 numapps[k].instance_id);
2816 BT_INFO("Time to register GATT client instancer..UUID to be used is [%s] slot [%d]",
2817 uuid_list[slot-1], slot);
2823 BT_ERR("No Slot if free for GATT Client registration..");
2824 return BLUETOOTH_ERROR_REGISTRATION_FAILED;
2827 uuid_string = g_malloc0(BT_UUID_STRING_MAX);
2828 _bt_string_to_uuid(uuid_list[slot-1], (service_uuid_t*)&uuid);
2829 g_strlcpy(uuid_string, uuid_list[slot-1], BT_UUID_STRING_MAX);
2830 BT_INFO("Copied UUID string [%s]", uuid_string);
2832 /* Register GATT Client */
2833 ret = gattc_register(&uuid);
2834 if (ret != OAL_STATUS_SUCCESS) {
2835 BT_ERR("ret: %d", ret);
2836 g_free(uuid_string);
2837 return BLUETOOTH_ERROR_INTERNAL;
2840 BT_INFO("GATT Client registration call successfully accepted by OAL..wait for Instance Initialized event from OAL..");
2842 /* Return & wait for GATT Client Instance Initialization event */
2843 memset(numapps[slot].sender, 0x00, sizeof(numapps[slot].sender));
2844 memset(numapps[slot].uuid, 0x00, sizeof(numapps[slot].uuid));
2846 g_strlcpy(numapps[slot].sender, sender, sizeof(numapps[slot].sender));
2847 g_strlcpy(numapps[slot].uuid, uuid_string, sizeof(numapps[slot].uuid));
2849 /* Address is saved here. When event comes, sender + address are matched for replying pending
2850 request. It is impossible for same sender to have requests with two same addresses */
2851 memcpy(&numapps[slot].address.addr, address->addr, sizeof(bluetooth_device_address_t));
2853 BT_INFO("Slot [%d] occupied", slot);
2854 numapps[slot].is_initialized = TRUE; /* Set initialization to true here itself */
2856 g_free(uuid_string);
2857 return BLUETOOTH_ERROR_NONE;
2863 /* GATT client events */
2864 static void __bt_handle_client_instance_registered(event_gattc_register_t *data)
2866 bt_service_app_info_t *info = NULL;
2868 char *uuid_string = g_malloc0(BT_UUID_STRING_MAX);
2870 _bt_uuid_to_string(&(data->client_uuid), uuid_string);
2871 BT_INFO("CLient ID is Initialized [%d] UUID initialized [%s]", data->client_if, uuid_string);
2873 /* Platform GATT client framwork does not use Default GATT client instance
2874 This GATT client instance is never deregistred in the lifetime of bt-service */
2875 BT_INFO("Default UUID [%s] current registered uuid [%s]",
2876 DEFAULT_GATT_CLIENT_UUID, uuid_string);
2877 if (g_strcmp0(uuid_string, DEFAULT_GATT_CLIENT_UUID) == 0) {
2878 BT_INFO("Default client Instance Registered [%s] Client instance [%d]",
2879 uuid_string, data->client_if);
2880 gatt_default_client = data->client_if;
2881 g_free(uuid_string);
2885 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
2888 if (g_strcmp0(info->uuid, uuid_string) == 0) {
2889 BT_INFO("Found GATT client.. UUID [%s], sender [%s]", info->uuid, info->sender);
2890 BT_INFO("Slot [%d] occupied", k);
2891 info->is_initialized = TRUE;
2892 info->client_id = data->client_if;
2893 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE, BT_GATT_CLIENT_REGISTER,
2894 (void*)info, sizeof(bt_service_app_info_t));
2898 g_free(uuid_string);
2901 static void __bt_handle_client_connected(event_gattc_conn_t *event_data)
2903 int result = BLUETOOTH_ERROR_NONE;
2904 struct gatt_server_info_t *conn_info = NULL;
2905 struct gatt_out_conn_info_t *out_conn_info = NULL;
2907 GVariant *param = NULL;
2909 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2910 _bt_convert_addr_type_to_string(address,
2911 (unsigned char *)event_data->address.addr);
2913 if (event_data->status != OAL_STATUS_SUCCESS)
2914 result = BLUETOOTH_ERROR_INTERNAL;
2916 /* DBUS Return fo BT_CONNECT_LE for all the apps */
2917 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE, address,
2918 BT_ADDRESS_STRING_SIZE);
2920 BT_INFO("Local GATT Client Connected: Remote addr[%s] Client Interface [%d] Connection ID [%d] status[%d]",
2921 address, event_data->client_if, event_data->conn_id, event_data->status);
2923 if (result == BLUETOOTH_ERROR_NONE) {
2924 /* Check if device is already in connected list */
2925 conn_info = __bt_find_remote_gatt_server_info(address);
2928 /* Send event to BT-API */
2929 param = g_variant_new("(is)", result, address);
2930 _bt_send_event(BT_DEVICE_EVENT,
2931 BLUETOOTH_EVENT_GATT_CLIENT_CONNECTED, /* Local device is GATT client */
2934 /* Save Connection info */
2935 conn_info = g_new0(struct gatt_server_info_t, 1);
2936 conn_info->addr = g_strdup(address);
2937 conn_info->client_id = event_data->client_if;
2938 BT_INFO("Added GATT server addr[%s]", conn_info->addr);
2939 conn_info->connection_id = event_data->conn_id;
2940 gatt_server_info_list = g_slist_append(gatt_server_info_list, conn_info);
2941 BT_INFO("Total num of connected Remote GATT server devices [%d]",
2942 g_slist_length(gatt_server_info_list));
2945 BT_INFO("Do a Internal refresh");
2946 if (OAL_STATUS_SUCCESS != gattc_refresh(conn_info->client_id, &event_data->address))
2947 BT_ERR("GATT database refresh failed!!");
2949 BT_INFO("GATT database refresh Success!!");
2952 BT_ERR("Local GATT Client connected event for addr[%s], but device is in connected list already", address);
2954 BT_ERR("GATT Client Connection failed!!");
2956 /* If outgoing connection Info is present, then remove it */
2957 out_conn_info = __bt_find_gatt_outgoing_conn_info(address);
2958 if (out_conn_info) {
2959 BT_ERR("Outgoing Client connect request was sent");
2960 outgoing_gatt_conn_list = g_slist_remove(outgoing_gatt_conn_list, out_conn_info);
2961 g_free(out_conn_info->addr);
2962 g_free(out_conn_info);
2967 static void __bt_handle_client_disconnected(event_gattc_conn_t *event_data)
2969 int result = BLUETOOTH_ERROR_NONE;
2971 struct gatt_server_info_t *conn_info = NULL;
2972 struct gatt_out_conn_info_t *out_conn_info = NULL;
2974 GVariant *param = NULL;
2976 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2977 _bt_convert_addr_type_to_string(address,
2978 (unsigned char *)event_data->address.addr);
2980 if (event_data->status != OAL_STATUS_SUCCESS)
2981 result = BLUETOOTH_ERROR_INTERNAL;
2983 if (NULL == _bt_get_request_info_data(BT_DISCONNECT_LE, address)) {
2984 if (NULL != _bt_get_request_info_data(BT_CONNECT_LE, address)) {
2985 result = BLUETOOTH_ERROR_INTERNAL;
2986 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
2987 address, BT_ADDRESS_STRING_SIZE);
2988 BT_ERR("Failed to connect Local GATT Remote addr[%s]", address);
2992 /* DBUS Return for BT_DISCONNECT_LE for all the apps */
2993 __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE, address,
2994 BT_ADDRESS_STRING_SIZE);
2996 BT_INFO("Local GATT Client DisConnected: Remote addr[%s] Client Interface [%d] Connection ID [%d] status [%d]",
2997 address, event_data->client_if, event_data->conn_id, event_data->status);
2999 /* Remove Connection info */
3000 conn_info = __bt_find_remote_gatt_server_info(address);
3003 param = g_variant_new("(is)", result, address);
3004 /* Send event to application */
3005 _bt_send_event(BT_DEVICE_EVENT,
3006 BLUETOOTH_EVENT_GATT_CLIENT_DISCONNECTED,
3009 BT_INFO("Remove GATT server info from List..");
3010 /* Remove all services from info list_gatt_info */
3011 __bt_cleanup_remote_services(conn_info);
3013 /* Remove info from List */
3014 gatt_server_info_list = g_slist_remove(gatt_server_info_list, conn_info);
3015 BT_INFO("Total num of connected GATT servers [%d]", g_slist_length(gatt_server_info_list));
3016 g_free(conn_info->addr);
3019 BT_INFO("Can not find conn info, already removed!");
3021 /* If outgoing connection Info is present, then remove it */
3022 out_conn_info = __bt_find_gatt_outgoing_conn_info(address);
3023 if (out_conn_info) {
3024 BT_ERR("Client Disconnected event, but outgoing connect request was sent");
3025 outgoing_gatt_conn_list = g_slist_remove(outgoing_gatt_conn_list, out_conn_info);
3026 g_free(out_conn_info->addr);
3027 g_free(out_conn_info);
3033 static void __bt_handle_client_service_search_result(
3034 event_gattc_service_result_t *event_data)
3037 BT_INFO("received the gatt service search result");
3038 /* Pre: status is never fail from OAL */
3040 /* Find service list from address */
3041 bt_gatt_service_info_list_t *svc_info_list;
3042 bt_gatt_service_info_t *svc_info;
3043 BT_INFO("Search Result: status [%d] conn_id [%d]",
3044 event_data->conn_status.status,
3045 event_data->conn_status.conn_id);
3047 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3048 if (!svc_info_list) {
3049 BT_INFO("Service info list not present for this connection ID, means first time browse");
3050 /* Means for this conn_id, no services are ever browsed, first time,
3051 create service info list for this conn_id */
3052 svc_info_list = g_malloc0(sizeof(bt_gatt_service_info_list_t));
3053 svc_info_list->conn_id = event_data->conn_status.conn_id;
3054 list_gatt_info = g_slist_append(list_gatt_info, svc_info_list);
3056 BT_INFO("Service info list Already present for this connection ID, means not first time browse for this conn ID ");
3059 /* send list and current service's uuid and instance id to find it */
3060 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3063 /* If not found, check if service changed, if yes, means this is a new service added
3064 in remote GATT device, update uuid info in svc info list structure, to be used when
3065 search is completed */
3067 BT_INFO("Service Not found in svc info list for this connection ID");
3068 if (svc_info_list->info.is_changed) {
3069 BT_INFO("Service Changed indication already found for this connection ID");
3070 memcpy(svc_info_list->info.uuid, event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3072 /* Create and add new service in service list */
3073 svc_info = g_malloc0(sizeof(bt_gatt_service_info_t));
3074 memcpy(svc_info->uuid, event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3075 svc_info->inst_id = event_data->srvc_id.id.inst_id;
3076 svc_info->is_primary = event_data->srvc_id.is_prmry;
3077 svc_info_list->services = g_slist_append(svc_info_list->services, svc_info);
3078 BT_INFO("Service created and added in Svc info listf or this connection ID");
3080 BT_INFO("Service Already found to be present inside the svc info list for this connection ID");
3081 /* If returned matching service info, then just update service_rmeoved value inside it to 0 */
3082 svc_info->is_removed = 0;
3086 static void __bt_handle_client_service_search_completed(
3087 event_gattc_conn_status_t *event_data)
3090 struct gatt_server_info_t *conn_info = NULL;
3091 bt_gatt_service_info_list_t *svc_info_list;
3092 bt_gatt_service_info_t *svc_info;
3093 bt_services_browse_info_t browse_info;
3094 unsigned char uuid_empty[BLUETOOTH_UUID_HEX_MAX_LEN];
3096 memset(&uuid_empty, 0x00, BLUETOOTH_UUID_HEX_MAX_LEN);
3097 memset(&browse_info, 0x00, sizeof(bt_services_browse_info_t));
3098 BT_INFO("Primary Services browsing completed status[%d] conn ID [%d]",
3099 event_data->status, event_data->conn_id);
3101 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(event_data->conn_id);
3103 svc_info_list = __bt_get_service_info_list(event_data->conn_id);
3104 if (!svc_info_list) {
3105 BT_ERR("No services browsed ever for addr [%s]", conn_info->addr);
3107 /* Just build response and return ERROR */
3108 __bt_build_service_browse_info(event_data->conn_id, &browse_info);
3110 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_INTERNAL,
3111 BT_GATT_GET_PRIMARY_SERVICES, &browse_info,
3112 sizeof(bt_services_browse_info_t));
3116 /* If fail, then send event with error */
3117 if (event_data->status != OAL_STATUS_SUCCESS) {
3118 /* Just build response and return ERROR */
3119 __bt_build_service_browse_info(event_data->conn_id, &browse_info);
3121 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_INTERNAL,
3122 BT_GATT_GET_PRIMARY_SERVICES, &browse_info,
3123 sizeof(bt_services_browse_info_t));
3127 /* If success, then find service info list from address */
3129 /* If svc_changed == 1 and uuid valid, means a new service is added*/
3130 if (svc_info_list->info.is_changed && !memcmp(uuid_empty, svc_info_list->info.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)) {
3131 /* TODO: Send event -Service added with instance ID and UUID of newly added service */
3132 BT_INFO("new service added");
3134 BT_INFO("TODO new service added");
3137 /* If svc_changed == 1 and uuid invalid, then a service is removed */
3138 if (svc_info_list->info.is_changed && memcmp(uuid_empty, svc_info_list->info.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)) {
3139 /* Scan through the service info list to find service with is_removed = 1*/
3140 svc_info = __bt_find_removed_service(svc_info_list);
3142 /* TODO Send event - Service removed with instance ID and UUID of just rmeoved service */
3144 /* Remove that service info from service info list */
3145 svc_info_list->services = g_slist_remove(svc_info_list->services, svc_info);
3147 /* Delete that service completely from svc_info list*/
3148 __bt_remove_service_info_from_list(svc_info);
3152 /* Reset svc_changed = 0, and reset UUID = all 0's */
3153 svc_info_list->info.is_changed = 0;
3154 memset(&svc_info_list->info.uuid, 0x00, BLUETOOTH_UUID_HEX_MAX_LEN);
3156 /* Build Reply and send to service browse primary services request of pending apps */
3157 __bt_build_service_browse_info(event_data->conn_id, &browse_info);
3159 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE,
3160 BT_GATT_GET_PRIMARY_SERVICES, &browse_info,
3161 sizeof(bt_services_browse_info_t));
3165 static void __bt_handle_client_characteristic_search_result(
3166 event_gattc_characteristic_result_t *event_data)
3168 bt_gatt_service_info_list_t *svc_info_list;
3169 bt_gatt_service_info_t *svc_info;
3170 bt_gatt_char_info_t *char_info;
3171 bt_char_browse_info_t browse_info;
3173 BT_INFO("Characteristic search result status [%d]",
3174 event_data->conn_status.status);
3176 memset(&browse_info, 0x00, sizeof(bt_char_browse_info_t));
3179 if (event_data->conn_status.status == OAL_STATUS_SUCCESS) {
3180 /* Find service info list from address */
3181 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3182 if (svc_info_list == NULL) {
3183 BT_ERR("svc_info_list is NULL");
3188 /* Find matching service info from svc info list */
3189 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3190 if (svc_info == NULL) {
3191 BT_ERR("svc_info is NULL");
3195 /* Find Matching char from service info in event */
3196 char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
3197 if (char_info == NULL)
3198 BT_ERR("char_info is NULL");
3200 /* If not found, then add new characteristic and return */
3202 BT_INFO(" add new characteristic");
3203 char_info = g_malloc0(sizeof(bt_gatt_char_info_t));
3204 memcpy(char_info->uuid, event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3206 char_info->inst_id = event_data->char_id.inst_id;
3207 char_info->props = event_data->char_prop;
3208 svc_info->chars = g_slist_append(svc_info->chars, char_info);
3210 /* If found, then return */
3211 BT_INFO("update char property as Characteristic browsed is already present");
3212 char_info->props |= event_data->char_prop;
3215 /* If Not success: Means Charc browse is completed */
3216 /* Find char list from service in event */
3217 /* Find service list from address */
3218 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3220 /* Find service info from service in event */
3221 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3223 /* Build char list from service in event */
3224 __bt_build_char_browse_info(event_data->conn_status.conn_id,
3225 svc_info, &browse_info);
3227 /* Create response and return by sending event*/
3228 /* Build Reply and send to service browse All Included services request of pending apps */
3229 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE,
3230 BT_GATT_GET_SERVICE_PROPERTIES,
3232 sizeof(bt_char_browse_info_t));
3236 static void __bt_handle_client_descriptor_search_result(
3237 event_gattc_descriptor_result_t *event_data)
3239 bt_gatt_service_info_list_t *svc_info_list;
3240 bt_gatt_service_info_t *svc_info;
3241 bt_gatt_char_info_t *char_info;
3242 bt_gatt_descriptor_info_t *desc_info;
3243 bt_descriptor_browse_info_t browse_info;
3245 BT_INFO("descriptor search result status [%d]", event_data->conn_status.status);
3247 memset(&browse_info, 0x00, sizeof(bt_descriptor_browse_info_t));
3250 if (event_data->conn_status.status == OAL_STATUS_SUCCESS) {
3251 /* Find service list from address */
3252 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3253 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3254 char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
3255 desc_info = __bt_find_matching_desc(char_info, &event_data->descr_id);
3257 /* If not found, add new descriptor and return */
3259 desc_info = g_malloc0(sizeof(bt_gatt_descriptor_info_t));
3260 memcpy(desc_info->uuid, event_data->descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3262 desc_info->inst_id = event_data->descr_id.inst_id;
3263 char_info->descs = g_slist_append(char_info->descs, desc_info);
3266 /* If found, then return */
3267 BT_INFO("Characteristic browsed is already presesnt");
3270 /* If Not success */
3271 /* Find service list from address */
3272 /* Find included service list from service in event */
3273 /* Create response and return by sending event*/
3274 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3276 /* Find service info from service in event */
3277 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3279 /* Find char info from char in event */
3280 char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
3282 /* Build descriptor list from char in event */
3283 __bt_build_descriptor_browse_info(event_data->conn_status.conn_id,
3284 svc_info, char_info, &browse_info);
3287 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE,
3288 BT_GATT_GET_CHARACTERISTIC_PROPERTIES,
3290 sizeof(bt_descriptor_browse_info_t));
3294 static void __bt_handle_client_characteristic_read_data(
3295 event_gattc_read_data *event_data)
3297 int result = BLUETOOTH_ERROR_NONE;
3298 struct gatt_server_info_t *conn_info = NULL;
3299 bluetooth_gatt_client_char_prop_info_t read_info;
3301 /* Read Information data structures */
3302 GVariant *param = NULL;
3303 GVariant *data = NULL;
3304 GVariant *data_svc_uuid = NULL;
3305 GVariant *data_char_uuid = NULL;
3306 char *read_val = NULL;
3307 char *svc_uuid = NULL;
3308 char *char_uuid = NULL;
3314 //memset(&read_info, 0x00, sizeof(bt_gatt_handle_property_t));
3315 memset(&read_info, 0x00, sizeof(bluetooth_gatt_client_char_prop_info_t));
3317 /* Extract Address from conn_id of event data */
3318 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3319 event_data->uuid_status.conn_status.conn_id);
3321 BT_INFO("Characteristic Read result from addr [%s] status [%d]",
3322 conn_info->addr, event_data->uuid_status.conn_status.status);
3324 /* Fill char in buffer */
3325 memcpy(&read_info.characteristic.uuid,
3326 event_data->uuid_status.char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3327 read_info.characteristic.instance_id = event_data->uuid_status.char_id.inst_id;
3329 /* Fill Service in buffer */
3330 memcpy(&read_info.svc.uuid,
3331 event_data->uuid_status.srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3332 read_info.svc.instance_id = event_data->uuid_status.srvc_id.id.inst_id;
3334 /* Fill remote device address */
3335 _bt_convert_addr_string_to_type(read_info.device_address.addr, conn_info->addr);
3337 /* Fill data and reply to all apps waiting for Read result on the same characteristic
3338 Note: Even in case of failure, address, handles and result code should be returned */
3339 if (event_data->uuid_status.conn_status.status != OAL_STATUS_SUCCESS)
3340 result = BLUETOOTH_ERROR_INTERNAL;
3342 BT_ERR("read data len is [%d]", event_data->data_len);
3343 if (event_data->data_len > 0) {
3345 for (i = 0; i < event_data->data_len; i++)
3346 BT_INFO("Data[%d] = [0x%x]", i, event_data->data[i]);
3349 read_val = g_memdup(&event_data->data[0], event_data->data_len);
3351 data = g_variant_new_from_data(
3352 G_VARIANT_TYPE_BYTESTRING,
3354 event_data->data_len,
3357 BT_ERR("Characteristic Read success, but no data!!!");
3359 data = g_variant_new_from_data(
3360 G_VARIANT_TYPE_BYTESTRING,
3367 svc_uuid = g_memdup(&event_data->uuid_status.srvc_id.id.uuid.uuid[0], uuid_len);
3369 data_svc_uuid = g_variant_new_from_data(
3370 G_VARIANT_TYPE_BYTESTRING,
3376 char_uuid = g_memdup(&event_data->uuid_status.char_id.uuid.uuid[0], uuid_len);
3378 data_char_uuid = g_variant_new_from_data(
3379 G_VARIANT_TYPE_BYTESTRING,
3384 param = g_variant_new("(isn@ayin@ayin@ay)", result,
3388 event_data->uuid_status.srvc_id.id.inst_id,
3391 event_data->uuid_status.char_id.inst_id,
3392 event_data->data_len,
3396 _bt_send_event(BT_GATT_CLIENT_EVENT,
3397 BLUETOOTH_EVENT_GATT_READ_CHAR,
3400 /* Send DBUS return */
3401 __bt_gatt_handle_pending_request_info(result,
3402 BT_GATT_READ_CHARACTERISTIC,
3404 sizeof(bluetooth_gatt_client_char_prop_info_t));
3415 static void __bt_handle_client_descriptor_read_data(
3416 event_gattc_read_data *event_data)
3418 int result = BLUETOOTH_ERROR_NONE;
3419 struct gatt_server_info_t *conn_info = NULL;
3420 bluetooth_gatt_client_desc_prop_info_t read_info;
3422 /* Read Information data structures */
3423 GVariant *param = NULL;
3424 GVariant *data = NULL;
3425 GVariant *data_svc_uuid = NULL;
3426 GVariant *data_char_uuid = NULL;
3427 GVariant *data_desc_uuid = NULL;
3428 char *read_val = NULL;
3429 char *svc_uuid = NULL;
3430 char *char_uuid = NULL;
3431 char *desc_uuid = NULL;
3436 memset(&read_info, 0x00, sizeof(bluetooth_gatt_client_desc_prop_info_t));
3438 /* Extract Address from conn_id of event data */
3439 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3440 event_data->uuid_status.conn_status.conn_id);
3442 BT_INFO("Descriptor Read result from addr [%s] status [%d]",
3443 conn_info->addr, event_data->uuid_status.conn_status.status);
3445 /* Fill descriptor informations in buffer */
3446 memcpy(&read_info.descriptor.uuid,
3447 event_data->uuid_status.descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3448 read_info.descriptor.instance_id = event_data->uuid_status.descr_id.inst_id;
3450 /* Fill Characteristic informations in buffer */
3451 memcpy(&read_info.characteristic.uuid,
3452 event_data->uuid_status.char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3453 read_info.characteristic.instance_id = event_data->uuid_status.char_id.inst_id;
3455 /* Fill Service informations in buffer */
3456 memcpy(&read_info.svc.uuid,
3457 event_data->uuid_status.srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3458 read_info.svc.instance_id = event_data->uuid_status.srvc_id.id.inst_id;
3460 /* Fill remote device address */
3461 _bt_convert_addr_string_to_type(read_info.device_address.addr, conn_info->addr);
3463 /* Fill data and reply to all apps waiting for Read result on the same characteristic */
3464 if (event_data->uuid_status.conn_status.status != OAL_STATUS_SUCCESS)
3465 result = BLUETOOTH_ERROR_INTERNAL;
3467 BT_INFO("desc data len:", event_data->data_len);
3468 if (event_data->data_len > 0) {
3470 for (i = 0; i < event_data->data_len; i++)
3471 BT_DBG("Data[%d] = [0x%x]", i, event_data->data[i]);
3474 read_val = g_memdup(&event_data->data[0], event_data->data_len);
3476 data = g_variant_new_from_data(
3477 G_VARIANT_TYPE_BYTESTRING,
3479 event_data->data_len,
3482 BT_INFO("Descriptor Read success, but no data!!!");
3484 data = g_variant_new_from_data(
3485 G_VARIANT_TYPE_BYTESTRING,
3491 svc_uuid = g_memdup(&event_data->uuid_status.srvc_id.id.uuid.uuid[0], uuid_len);
3493 data_svc_uuid = g_variant_new_from_data(
3494 G_VARIANT_TYPE_BYTESTRING,
3500 char_uuid = g_memdup(&event_data->uuid_status.char_id.uuid.uuid[0], uuid_len);
3502 data_char_uuid = g_variant_new_from_data(
3503 G_VARIANT_TYPE_BYTESTRING,
3509 desc_uuid = g_memdup(&event_data->uuid_status.descr_id.uuid.uuid[0], uuid_len);
3511 data_desc_uuid = g_variant_new_from_data(
3512 G_VARIANT_TYPE_BYTESTRING,
3517 param = g_variant_new("(isn@ayin@ayin@ayin@ay)", result,
3521 event_data->uuid_status.srvc_id.id.inst_id,
3524 event_data->uuid_status.char_id.inst_id,
3527 event_data->uuid_status.descr_id.inst_id,
3528 event_data->data_len,
3532 _bt_send_event(BT_GATT_CLIENT_EVENT,
3533 BLUETOOTH_EVENT_GATT_READ_DESC,
3536 BT_INFO("Send DBUS rpely for GATT Read Descriptor");
3537 /* Send DBUS return */
3538 __bt_gatt_handle_pending_request_info(result,
3539 BT_GATT_READ_DESCRIPTOR_VALUE,
3541 sizeof(bluetooth_gatt_client_desc_prop_info_t));
3552 static void __bt_handle_client_characteristic_write_data(
3553 event_gattc_write_data *event_data)
3555 int result = BLUETOOTH_ERROR_NONE;
3556 struct gatt_server_info_t *conn_info = NULL;
3557 bluetooth_gatt_client_char_prop_info_t write_info;
3559 /* Read Information data structures */
3560 GVariant *param = NULL;
3561 GVariant *data_svc_uuid = NULL;
3562 GVariant *data_char_uuid = NULL;
3563 char *svc_uuid = NULL;
3564 char *char_uuid = NULL;
3568 memset(&write_info, 0x00, sizeof(bluetooth_gatt_client_char_prop_info_t));
3570 /* Extract Address from conn_id of event data */
3571 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3572 event_data->conn_status.conn_id);
3574 BT_INFO("Characteristic Write callback from addr [%s] status [%d]",
3575 conn_info->addr, event_data->conn_status.status);
3577 /* Fill char in buffer */
3578 memcpy(&write_info.characteristic.uuid,
3579 event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3580 write_info.characteristic.instance_id = event_data->char_id.inst_id;
3582 /* Fill Service in buffer */
3583 memcpy(&write_info.svc.uuid,
3584 event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3585 write_info.svc.instance_id = event_data->srvc_id.id.inst_id;
3587 /* Fill remote device address */
3588 _bt_convert_addr_string_to_type(write_info.device_address.addr, conn_info->addr);
3590 if (event_data->conn_status.status != OAL_STATUS_SUCCESS) {
3591 result = BLUETOOTH_ERROR_INTERNAL;
3597 svc_uuid = g_memdup(&event_data->srvc_id.id.uuid.uuid[0], uuid_len);
3599 data_svc_uuid = g_variant_new_from_data(
3600 G_VARIANT_TYPE_BYTESTRING,
3606 char_uuid = g_memdup(&event_data->char_id.uuid.uuid[0], uuid_len);
3608 data_char_uuid = g_variant_new_from_data(
3609 G_VARIANT_TYPE_BYTESTRING,
3614 param = g_variant_new("(isn@ayin@ayi)", result,
3618 event_data->srvc_id.id.inst_id,
3621 event_data->char_id.inst_id);
3624 _bt_send_event(BT_GATT_CLIENT_EVENT,
3625 BLUETOOTH_EVENT_GATT_WRITE_CHAR,
3634 /* Send DBUS return */
3635 __bt_gatt_handle_pending_request_info(result,
3636 BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE,
3638 sizeof(bluetooth_gatt_client_char_prop_info_t));
3642 static void __bt_handle_client_descriptor_write_data(
3643 event_gattc_write_data *event_data)
3645 int result = BLUETOOTH_ERROR_NONE;
3646 struct gatt_server_info_t *conn_info = NULL;
3647 bluetooth_gatt_client_desc_prop_info_t write_info;
3649 /* Write Information data structures */
3650 GVariant *param = NULL;
3651 GVariant *data_svc_uuid = NULL;
3652 GVariant *data_char_uuid = NULL;
3653 GVariant *data_desc_uuid = NULL;
3654 char *svc_uuid = NULL;
3655 char *char_uuid = NULL;
3656 char *desc_uuid = NULL;
3660 memset(&write_info, 0x00, sizeof(bluetooth_gatt_client_desc_prop_info_t));
3662 /* Extract Address from conn_id of event data */
3663 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3664 event_data->conn_status.conn_id);
3666 if (NULL == conn_info) {
3668 BT_INFO("Failed to get the conn info for conn_id [%d]", event_data->conn_status.conn_id);
3672 BT_INFO("Descriptor Write callback from addr [%s] status [%d]",
3673 conn_info->addr, event_data->conn_status.status);
3675 /* Fill descriptor informations in buffer */
3676 memcpy(&write_info.descriptor.uuid,
3677 event_data->descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3678 write_info.descriptor.instance_id = event_data->descr_id.inst_id;
3680 /* Fill Characteristic informations in buffer */
3681 memcpy(&write_info.characteristic.uuid,
3682 event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3683 write_info.characteristic.instance_id = event_data->char_id.inst_id;
3685 /* Fill Service informations in buffer */
3686 memcpy(&write_info.svc.uuid,
3687 event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3688 write_info.svc.instance_id = event_data->srvc_id.id.inst_id;
3690 /* Fill remote device address */
3691 _bt_convert_addr_string_to_type(write_info.device_address.addr, conn_info->addr);
3693 if (event_data->conn_status.status != OAL_STATUS_SUCCESS) {
3694 result = BLUETOOTH_ERROR_INTERNAL;
3700 svc_uuid = g_memdup(&event_data->srvc_id.id.uuid.uuid[0], uuid_len);
3702 data_svc_uuid = g_variant_new_from_data(
3703 G_VARIANT_TYPE_BYTESTRING,
3709 char_uuid = g_memdup(&event_data->char_id.uuid.uuid[0], uuid_len);
3711 data_char_uuid = g_variant_new_from_data(
3712 G_VARIANT_TYPE_BYTESTRING,
3718 desc_uuid = g_memdup(&event_data->descr_id.uuid.uuid[0], uuid_len);
3720 data_desc_uuid = g_variant_new_from_data(
3721 G_VARIANT_TYPE_BYTESTRING,
3726 param = g_variant_new("(isn@ayin@ayin@ayi)", result,
3730 event_data->srvc_id.id.inst_id,
3733 event_data->char_id.inst_id,
3736 event_data->descr_id.inst_id);
3739 _bt_send_event(BT_GATT_CLIENT_EVENT,
3740 BLUETOOTH_EVENT_GATT_WRITE_DESC,
3751 /* Send DBUS return */
3752 __bt_gatt_handle_pending_request_info(result,
3753 BT_GATT_WRITE_DESCRIPTOR_VALUE,
3755 sizeof(bluetooth_gatt_client_desc_prop_info_t));
3758 static void __bt_hanlde_le_device_disconnection(event_dev_conn_status_t *event_data)
3760 int result = BLUETOOTH_ERROR_INTERNAL;
3761 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
3763 _bt_convert_addr_type_to_string(address, (unsigned char *)event_data->address.addr);
3765 /* DBUS Return with fail of pending BT_CONNECT_LE for all the apps */
3766 BT_INFO("Local GATT Client disconnected: Remote addr[%s] ", address);
3768 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE, address,
3769 BT_ADDRESS_STRING_SIZE);
3772 static void __bt_handle_client_notification_registered(
3773 event_gattc_regdereg_notify_t *event_data,
3774 gboolean is_registered)
3776 int result = BLUETOOTH_ERROR_NONE;
3777 struct gatt_server_info_t *conn_info = NULL;
3778 bt_gatt_notif_reg_info_t notif_info;
3781 memset(¬if_info, 0x00, sizeof(bt_gatt_notif_reg_info_t));
3783 BT_INFO("Client Interface [%d] status [%d]",
3784 event_data->client_if,
3785 event_data->status);
3787 /* Extract Address from conn_id of event data */
3788 conn_info = __bt_find_remote_gatt_server_info_from_client_if(
3789 event_data->client_if);
3792 BT_INFO("Connection Info is not present, return");
3795 BT_INFO("Notification Registered for addr [%s]",
3799 /* Fill svc informations in buffer */
3800 memcpy(¬if_info.svc_uuid,
3801 event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3802 notif_info.svc_inst = event_data->srvc_id.id.inst_id;
3804 /* Fill char in buffer */
3805 memcpy(¬if_info.char_uuid,
3806 event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3807 notif_info.char_inst = event_data->char_id.inst_id;
3809 /* Fill remote device address */
3810 _bt_convert_addr_string_to_type(notif_info.addr.addr, conn_info->addr);
3812 notif_info.is_registered = is_registered;
3814 if (event_data->status != OAL_STATUS_SUCCESS)
3815 result = BLUETOOTH_ERROR_INTERNAL;
3817 /* Send DBUS Return for BT_GATT_WATCH_CHARACTERISTIC */
3818 __bt_gatt_handle_pending_request_info(result,
3819 BT_GATT_WATCH_CHARACTERISTIC,
3821 sizeof(bt_gatt_notif_reg_info_t));
3825 gboolean _bt_is_remote_gatt_device_connected(bluetooth_device_address_t *address)
3828 struct gatt_server_info_t *conn_info = NULL;
3829 gboolean connected = FALSE;
3832 addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
3833 _bt_convert_addr_type_to_string(addr,
3834 (unsigned char *)&(address->addr));
3836 BT_INFO("Check Connected or not for [%s]", addr);
3837 /* Check if device is already in connected list */
3838 conn_info = __bt_find_remote_gatt_server_info(addr);
3841 BT_INFO("Remote GATT Server device [%s] is Connected", conn_info->addr);
3844 BT_INFO("Remote GATT Server Device [%s] is not Connected", addr);
3850 int _bt_connect_le_device(bluetooth_device_address_t *address,
3851 int auto_connect, int client_id)
3853 struct gatt_server_info_t *conn_info = NULL;
3854 struct gatt_out_conn_info_t *out_conn_info = NULL;
3856 invocation_info_t *req_info = NULL;
3857 int ret = OAL_STATUS_SUCCESS;
3859 char *remote_address = NULL;
3861 BT_CHECK_PARAMETER(address, return);
3865 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
3866 _bt_convert_addr_type_to_string(addr, address->addr);
3867 BT_INFO("GATT Client connect request for address [%s] client instance [%d]",
3871 /* Check if Remote Device is already under connection progress */
3872 req_info = _bt_get_request_info_data_from_function_name(BT_CONNECT_LE);
3874 remote_address = (char*)req_info->user_data;
3875 if (remote_address && !strcasecmp(remote_address, addr)) {/* Address matched */
3876 BT_INFO("Already Connection ongoing for same remote GATT Server address [%s]", remote_address);
3877 /* Return and wait for events to be sent to all apps */
3879 return BLUETOOTH_ERROR_IN_PROGRESS;
3883 /* Check if remote GATT Server is connected or not */
3884 conn_info = __bt_find_remote_gatt_server_info(addr);
3886 BT_ERR("GATT Server is already connected..");
3888 return BLUETOOTH_ERROR_ALREADY_CONNECT;
3891 /* TODO Check Requirement of holding Advertisement before initiating LE connect */
3893 /* Check if app sent 0 client id for connection, in such case, use default gatt client ID */
3894 if (client_id == 0) {
3895 BT_INFO("GATT CLient connect request sent by an app without any client instance [%d]",
3897 BT_INFO("Assign default GATT client id [%d]", gatt_default_client);
3898 client_id = gatt_default_client;
3901 BT_INFO("Connect using CLient ID [%d]", client_id);
3902 ret = gattc_connect(client_id, (bt_address_t*)(address), auto_connect);
3904 if (ret != OAL_STATUS_SUCCESS) {
3905 BT_ERR("ret: %d", ret);
3907 return BLUETOOTH_ERROR_INTERNAL;
3910 /* Mark this as outgoing connection */
3911 out_conn_info = g_new0(struct gatt_out_conn_info_t, 1);
3912 out_conn_info->addr = g_strdup(addr);
3913 out_conn_info->client_id = client_id;
3914 BT_INFO("Added outgoing connection info addr[%s]", out_conn_info->addr);
3915 outgoing_gatt_conn_list = g_slist_append(outgoing_gatt_conn_list, out_conn_info);
3918 return BLUETOOTH_ERROR_NONE;
3921 int _bt_gatt_get_primary_services(char *address)
3923 BT_CHECK_PARAMETER(address, return);
3924 struct gatt_server_info_t *conn_info = NULL;
3925 invocation_info_t *req_info = NULL;
3926 int ret = OAL_STATUS_SUCCESS;
3929 /* Check if any app is already browsing primary services on the same remote GATT Server */
3930 req_info = _bt_get_request_info_data(BT_GATT_GET_PRIMARY_SERVICES, address);
3932 BT_INFO("Already Primary Service Browsing ongoing for same rmeote GATT Server");
3933 /* Return and wait for events to be sent to all apps */
3934 return BLUETOOTH_ERROR_NONE;
3937 /* Check if remote GATT Server is connected or not */
3938 conn_info = __bt_find_remote_gatt_server_info(address);
3940 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
3941 conn_info->addr, conn_info->connection_id);
3943 BT_ERR("GATT Server is not yet connected..");
3944 return BLUETOOTH_ERROR_NOT_CONNECTED;
3947 /* Send Primary Service Browsing request to stack */
3948 ret = gattc_search_service(conn_info->connection_id, NULL);
3949 if (ret != OAL_STATUS_SUCCESS) {
3950 BT_ERR("ret: %d", ret);
3951 return BLUETOOTH_ERROR_INTERNAL;
3953 return BLUETOOTH_ERROR_NONE;
3956 int _bt_gatt_get_all_characteristic(bluetooth_gatt_client_svc_prop_info_t *svc)
3958 BT_CHECK_PARAMETER(svc, return);
3959 struct gatt_server_info_t *conn_info = NULL;
3960 invocation_info_t *req_info = NULL;
3961 bluetooth_gatt_client_svc_prop_info_t *prop;
3962 oal_gatt_srvc_id_t srvc_id;
3963 int ret = OAL_STATUS_SUCCESS;
3967 /* Check if any app is already browsing characteristics of the same service on the same remote GATT Server */
3968 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_GET_SERVICE_PROPERTIES);
3970 prop = (bluetooth_gatt_client_svc_prop_info_t*)req_info->user_data;
3971 if (prop && !memcmp(svc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t))
3972 && memcmp(prop->svc.uuid, svc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
3973 && prop->svc.instance_id == svc->svc.instance_id) {
3974 BT_INFO("Already Properties browsing for Primary Service ongoing for same remote GATT Server");
3975 /* Return and wait for events to be sent to all apps */
3976 return BLUETOOTH_ERROR_NONE;
3980 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
3981 _bt_convert_addr_type_to_string(addr, svc->device_address.addr);
3983 /* Check if remote GATT Server is connected or not */
3984 conn_info = __bt_find_remote_gatt_server_info(addr);
3986 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
3987 conn_info->addr, conn_info->connection_id);
3989 BT_ERR("GATT Server is not yet connected..");
3991 return BLUETOOTH_ERROR_NOT_CONNECTED;
3994 srvc_id.is_prmry = TRUE;
3995 srvc_id.id.inst_id = svc->svc.instance_id;
3996 memcpy(srvc_id.id.uuid.uuid, svc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3998 /* Search All Characteristic */
3999 ret = gattc_get_characteristic(conn_info->connection_id, &srvc_id, NULL);
4000 if (ret != OAL_STATUS_SUCCESS) {
4001 BT_ERR("ret: %d", ret);
4003 return BLUETOOTH_ERROR_INTERNAL;
4006 return BLUETOOTH_ERROR_NONE;
4009 int _bt_gatt_get_all_characteristic_properties(
4010 bluetooth_gatt_client_char_prop_info_t *chr)
4012 struct gatt_server_info_t *conn_info = NULL;
4013 invocation_info_t *req_info = NULL;
4014 bluetooth_gatt_client_char_prop_info_t *prop;
4015 oal_gatt_srvc_id_t srvc_id;
4016 oal_gatt_id_t char_id;
4017 int ret = OAL_STATUS_SUCCESS;
4020 BT_CHECK_PARAMETER(chr, return);
4024 /* Check if any app is already browsing descriptors of the same char of
4025 particular service on the same remote GATT Server */
4026 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_GET_CHARACTERISTIC_PROPERTIES);
4028 prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
4029 if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4030 && memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4031 && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4032 && memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4033 && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
4034 BT_INFO("Already Properties browsing for Characteristic ongoing for same remote GATT Server");
4035 /* Return and wait for events to be sent to all apps */
4036 return BLUETOOTH_ERROR_NONE;
4040 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4041 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4043 /* Check if remote GATT Server is connected or not */
4044 conn_info = __bt_find_remote_gatt_server_info(addr);
4046 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4047 conn_info->addr, conn_info->connection_id);
4049 BT_ERR("GATT Server is not yet connected..");
4051 return BLUETOOTH_ERROR_NOT_CONNECTED;
4054 srvc_id.is_prmry = TRUE;
4055 srvc_id.id.inst_id = chr->svc.instance_id;
4056 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4058 char_id.inst_id = chr->characteristic.instance_id;
4059 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4061 /* Search All Descriptors */
4062 ret = gattc_get_descriptor(conn_info->connection_id, &srvc_id, &char_id, NULL);
4063 if (ret != OAL_STATUS_SUCCESS) {
4064 BT_ERR("ret: %d", ret);
4066 return BLUETOOTH_ERROR_INTERNAL;
4069 return BLUETOOTH_ERROR_NONE;
4072 int _bt_gatt_read_characteristic_value(
4073 bluetooth_gatt_client_char_prop_info_t *chr)
4075 struct gatt_server_info_t *conn_info = NULL;
4076 invocation_info_t *req_info = NULL;
4077 bluetooth_gatt_client_char_prop_info_t *prop;
4078 oal_gatt_srvc_id_t srvc_id;
4079 oal_gatt_id_t char_id;
4080 int ret = OAL_STATUS_SUCCESS;
4083 BT_CHECK_PARAMETER(chr, return);
4087 /* Check if any app is already Reading characteristic of the same char of
4088 particular service on the same remote GATT Server */
4089 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_READ_CHARACTERISTIC);
4091 prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
4092 if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4093 && memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4094 && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4095 && memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4096 && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
4097 BT_INFO("Already Characteristic value Read operation in progress for same remote GATT Server");
4098 /* Return and wait for events to be sent to all apps */
4099 return BLUETOOTH_ERROR_NONE;
4103 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4104 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4106 /* Check if remote GATT Server is connected or not */
4107 conn_info = __bt_find_remote_gatt_server_info(addr);
4109 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4110 conn_info->addr, conn_info->connection_id);
4112 BT_ERR("GATT Server is not yet connected..");
4114 return BLUETOOTH_ERROR_NOT_CONNECTED;
4117 srvc_id.is_prmry = TRUE;
4118 srvc_id.id.inst_id = chr->svc.instance_id;
4119 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4121 char_id.inst_id = chr->characteristic.instance_id;
4122 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4124 /* Search All Descriptors */
4125 ret = gattc_read_characteristic(conn_info->connection_id, &srvc_id, &char_id, OAL_GATT_AUTH_REQ_NONE);
4126 if (ret != OAL_STATUS_SUCCESS) {
4127 BT_ERR("ret: %d", ret);
4129 return BLUETOOTH_ERROR_INTERNAL;
4132 return BLUETOOTH_ERROR_NONE;
4135 int _bt_gatt_read_descriptor_value(
4136 bluetooth_gatt_client_desc_prop_info_t *desc)
4138 struct gatt_server_info_t *conn_info = NULL;
4139 invocation_info_t *req_info = NULL;
4140 bluetooth_gatt_client_desc_prop_info_t *prop;
4141 oal_gatt_srvc_id_t srvc_id;
4142 oal_gatt_id_t char_id;
4143 oal_gatt_id_t desc_id;
4144 int ret = OAL_STATUS_SUCCESS;
4147 BT_CHECK_PARAMETER(desc, return);
4151 /* Check if any app is already Reading descriptors of the same char of
4152 particular service on the same remote GATT Server */
4153 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_READ_DESCRIPTOR_VALUE);
4155 prop = (bluetooth_gatt_client_desc_prop_info_t*)req_info->user_data;
4156 if (prop && !memcmp(desc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4157 && memcmp(desc->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4158 && desc->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4159 && memcmp(desc->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4160 && desc->characteristic.instance_id == prop->characteristic.instance_id /* Characteristic Instance ID matched */
4161 && memcmp(desc->descriptor.uuid, prop->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Descriptor UUID matched */
4162 && desc->descriptor.instance_id == prop->descriptor.instance_id) { /* Descriptor Instance ID matched */
4163 BT_INFO("Already Descriptor value Read operation in progress for same remote GATT Server");
4164 /* Return and wait for events to be sent to all apps */
4165 return BLUETOOTH_ERROR_NONE;
4169 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4170 _bt_convert_addr_type_to_string(addr, desc->device_address.addr);
4172 /* Check if remote GATT Server is connected or not */
4173 conn_info = __bt_find_remote_gatt_server_info(addr);
4175 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4176 conn_info->addr, conn_info->connection_id);
4178 BT_ERR("GATT Server is not yet connected..");
4180 return BLUETOOTH_ERROR_NOT_CONNECTED;
4183 srvc_id.is_prmry = TRUE;
4184 srvc_id.id.inst_id = desc->svc.instance_id;
4185 memcpy(srvc_id.id.uuid.uuid, desc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4187 char_id.inst_id = desc->characteristic.instance_id;
4188 memcpy(char_id.uuid.uuid, desc->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4190 desc_id.inst_id = desc->descriptor.instance_id;
4191 memcpy(desc_id.uuid.uuid, desc->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4193 /* Search All Descriptors */
4194 ret = gattc_read_descriptor(conn_info->connection_id, &srvc_id, &char_id,
4195 &desc_id, OAL_GATT_AUTH_REQ_NONE);
4196 if (ret != OAL_STATUS_SUCCESS) {
4197 BT_ERR("ret: %d", ret);
4199 return BLUETOOTH_ERROR_INTERNAL;
4202 return BLUETOOTH_ERROR_NONE;
4206 /* Write Characteristic */
4207 int _bt_gatt_write_characteristic_value_by_type(
4208 bluetooth_gatt_client_char_prop_info_t *chr,
4209 bluetooth_gatt_att_data_t *data,
4210 bluetooth_gatt_write_type_e write_type)
4212 struct gatt_server_info_t *conn_info = NULL;
4213 invocation_info_t *req_info = NULL;
4214 bluetooth_gatt_client_char_prop_info_t *prop;
4215 oal_gatt_srvc_id_t srvc_id;
4216 oal_gatt_id_t char_id;
4217 int ret = OAL_STATUS_SUCCESS;
4221 BT_CHECK_PARAMETER(chr, return);
4222 BT_CHECK_PARAMETER(data, return);
4226 /* Check if any app is already writing same char of
4227 particular service on the same remote GATT Server */
4228 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE);
4230 prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
4231 if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4232 && memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4233 && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4234 && memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4235 && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
4236 BT_INFO("Already Characteristic Write Value operation in progress for same remote GATT Server");
4237 /* Return and wait for events to be sent to all apps */
4238 return BLUETOOTH_ERROR_NONE;
4242 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4243 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4245 /* Check if remote GATT Server is connected or not */
4246 conn_info = __bt_find_remote_gatt_server_info(addr);
4248 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4249 conn_info->addr, conn_info->connection_id);
4251 BT_ERR("GATT Server is not yet connected..");
4253 return BLUETOOTH_ERROR_NOT_CONNECTED;
4256 srvc_id.is_prmry = TRUE;
4257 srvc_id.id.inst_id = chr->svc.instance_id;
4258 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4260 char_id.inst_id = chr->characteristic.instance_id;
4261 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4263 /* Write CHar value */
4265 BT_INFO("Connection ID [%d] write type [%d] data length [%d]", conn_info->connection_id, write_type, data->length);
4266 for (k = 0; k < data->length; k++)
4267 BT_INFO("Data[%d] [0x%x]", k, data->data[k]);
4269 ret = gattc_write_characteristic(conn_info->connection_id,
4271 (oal_gatt_write_type_t)write_type, data->length,
4272 OAL_GATT_AUTH_REQ_NONE, (char *)(&data->data[0]));
4273 if (ret != OAL_STATUS_SUCCESS) {
4274 BT_ERR("ret: %d", ret);
4276 return BLUETOOTH_ERROR_INTERNAL;
4279 return BLUETOOTH_ERROR_NONE;
4282 /* Write Descriptor */
4283 int _bt_gatt_write_descriptor_value_by_type(
4284 bluetooth_gatt_client_desc_prop_info_t *desc,
4285 bluetooth_gatt_att_data_t *data,
4286 bluetooth_gatt_write_type_e write_type)
4288 struct gatt_server_info_t *conn_info = NULL;
4289 invocation_info_t *req_info = NULL;
4290 bluetooth_gatt_client_desc_prop_info_t *prop;
4291 oal_gatt_srvc_id_t srvc_id;
4292 oal_gatt_id_t char_id;
4293 oal_gatt_id_t desc_id;
4294 int ret = OAL_STATUS_SUCCESS;
4298 BT_CHECK_PARAMETER(desc, return);
4299 BT_CHECK_PARAMETER(data, return);
4303 /* Check if any app is already writing on same Descriptor of the same char of
4304 particular service on the same remote GATT Server */
4305 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_WRITE_DESCRIPTOR_VALUE);
4307 prop = (bluetooth_gatt_client_desc_prop_info_t*)req_info->user_data;
4308 if (prop && !memcmp(desc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4309 && memcmp(desc->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4310 && desc->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4311 && memcmp(desc->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4312 && desc->characteristic.instance_id == prop->characteristic.instance_id /* Characteristic Instance ID matched */
4313 && memcmp(desc->descriptor.uuid, prop->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Descriptor UUID matched */
4314 && desc->descriptor.instance_id == prop->descriptor.instance_id) { /* Descriptor Instance ID matched */
4315 BT_INFO("Already Descriptor value Write operation in progress for same remote GATT Server");
4316 /* Return and wait for events to be sent to all apps */
4317 return BLUETOOTH_ERROR_NONE;
4321 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4322 _bt_convert_addr_type_to_string(addr, desc->device_address.addr);
4324 /* Check if remote GATT Server is connected or not */
4325 conn_info = __bt_find_remote_gatt_server_info(addr);
4327 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4328 conn_info->addr, conn_info->connection_id);
4330 BT_ERR("GATT Server is not yet connected..");
4332 return BLUETOOTH_ERROR_NOT_CONNECTED;
4335 srvc_id.is_prmry = TRUE;
4336 srvc_id.id.inst_id = desc->svc.instance_id;
4337 memcpy(srvc_id.id.uuid.uuid, desc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4339 char_id.inst_id = desc->characteristic.instance_id;
4340 memcpy(char_id.uuid.uuid, desc->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4342 desc_id.inst_id = desc->descriptor.instance_id;
4343 memcpy(desc_id.uuid.uuid, desc->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4346 BT_INFO("Connection ID [%d] write type [%d] data length [%d]", conn_info->connection_id, write_type, data->length);
4347 for (k = 0; k < data->length; k++)
4348 BT_INFO("Data[%d] [0x%x]", k, data->data[k]);
4350 ret = gattc_write_descriptor(conn_info->connection_id,
4351 &srvc_id, &char_id, &desc_id,
4352 (oal_gatt_write_type_t)write_type, data->length,
4353 OAL_GATT_AUTH_REQ_NONE, (char *)(&data->data[0]));
4354 if (ret != OAL_STATUS_SUCCESS) {
4355 BT_ERR("ret: %d", ret);
4357 return BLUETOOTH_ERROR_INTERNAL;
4360 return BLUETOOTH_ERROR_NONE;
4363 int _bt_gatt_watch_characteristic(
4364 bluetooth_gatt_client_char_prop_info_t *chr,
4368 struct gatt_server_info_t *conn_info = NULL;
4369 oal_gatt_srvc_id_t srvc_id;
4370 oal_gatt_id_t char_id;
4371 int ret = OAL_STATUS_SUCCESS;
4374 BT_CHECK_PARAMETER(chr, return);
4376 BT_INFO("Client ID [%d] Is Notify [%d]", client_id, is_notify);
4378 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4379 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4381 /* Check if remote GATT Server is connected or not */
4382 conn_info = __bt_find_remote_gatt_server_info(addr);
4384 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4385 conn_info->addr, conn_info->connection_id);
4387 BT_ERR("GATT Server is not yet connected..");
4389 return BLUETOOTH_ERROR_NOT_CONNECTED;
4391 srvc_id.is_prmry = TRUE;
4392 srvc_id.id.inst_id = chr->svc.instance_id;
4393 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4395 char_id.inst_id = chr->characteristic.instance_id;
4396 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4398 /* Register or unregister Notification characteristic */
4400 ret = gattc_register_for_notification(conn_info->client_id,
4401 (bt_address_t*)&(chr->device_address),
4402 &srvc_id, &char_id);
4404 ret = gattc_deregister_for_notification(conn_info->client_id,
4405 (bt_address_t*)&(chr->device_address),
4406 &srvc_id, &char_id);
4408 BT_INFO("Result[%d]", ret);
4409 if (ret != OAL_STATUS_SUCCESS) {
4410 BT_ERR("ret: %d", ret);
4412 return BLUETOOTH_ERROR_INTERNAL;
4415 return BLUETOOTH_ERROR_NONE;
4419 int _bt_disconnect_le_device(bluetooth_device_address_t *address,
4422 struct gatt_server_info_t *conn_info = NULL;
4423 invocation_info_t *req_info = NULL;
4424 int ret = OAL_STATUS_SUCCESS;
4426 char *remote_address = NULL;
4428 BT_CHECK_PARAMETER(address, return);
4432 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4433 _bt_convert_addr_type_to_string(addr, address->addr);
4434 BT_INFO("GATT Client Disconnect request for address [%s]", addr);
4436 /* Check if Remote Device is already under connection progress */
4437 req_info = _bt_get_request_info_data_from_function_name(BT_DISCONNECT_LE);
4439 remote_address = (char*)req_info->user_data;
4440 if (remote_address && !strcasecmp(remote_address, addr)) {/* Address matched */
4441 BT_INFO("Already DisConnection ongoing for same remote GATT Server address [%s]", remote_address);
4442 /* Return success and wait for events to be sent to all apps */
4444 return BLUETOOTH_ERROR_IN_PROGRESS;
4447 /* Check if remote GATT Server is connected or not */
4448 conn_info = __bt_find_remote_gatt_server_info(addr);
4450 BT_ERR("GATT Server is not connected..");
4452 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
4455 /* Check if app sent 0 client id for Disconnection, in such case, use default gatt client ID */
4456 if (client_id == 0) {
4457 BT_INFO("GATT CLient Disconnect request sent by an app without any client instance [%d]",
4459 BT_INFO("Assign default GATT client id [%d]", gatt_default_client);
4460 client_id = gatt_default_client;
4463 BT_INFO("Disconnect using CLient ID [%d] Connection ID [%d]", client_id, conn_info->connection_id);
4464 ret = gattc_disconnect(client_id, (bt_address_t*)(address),
4465 conn_info->connection_id);
4467 if (ret != OAL_STATUS_SUCCESS) {
4468 BT_ERR("ret: %d", ret);
4470 return BLUETOOTH_ERROR_INTERNAL;
4473 return BLUETOOTH_ERROR_NONE;
4476 int _bt_gatt_watch_service_changed_indication(const char *sender,
4477 bluetooth_device_address_t *address,
4478 gboolean is_enabled)
4481 bt_service_app_info_t *info = NULL;
4483 BT_INFO("Enable Servic changed Indication watcher [%d] for app [%s]",
4484 is_enabled, sender);
4486 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
4489 if (g_strcmp0(sender, info->sender) == 0 &&
4490 memcmp(info->address.addr, address->addr,
4491 sizeof(bluetooth_device_address_t)) == 0) {
4492 BT_INFO("Found GATT client App.. UUID [%s], sender [%s]", info->uuid, info->sender);
4493 info->is_watcher_enabled = is_enabled;
4497 return BLUETOOTH_ERROR_NONE;
4500 int _bt_unregister_gatt_client_instance(const char *sender, int client_id)
4502 BT_INFO("Unregister Allocated GATT CLient instance [%s] Client ID [%d]",
4504 int result = BLUETOOTH_ERROR_NONE;
4507 /* Unregister CLient instance associated with address X. It is possible that another app still
4508 has client_id valid for same remote address */
4509 bt_service_app_info_t *info = NULL;
4511 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
4514 /* Exact matching of sender */
4515 if (!g_strcmp0(info->sender, sender) && info->client_id == client_id) { /* Check for only valid GATT client Instance */
4516 BT_INFO("Unregister GATT client instance [%d]", info->client_id);
4517 result = __bt_do_unregister_gatt_instance(info->client_id);
4518 if (result != BLUETOOTH_ERROR_NONE)
4519 BT_ERR("Error in unregistering GATT Client Interface");