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);
1133 static void __bt_handle_gatt_server_service_added(event_gatts_srvc_prm_t *event)
1135 int result = BLUETOOTH_ERROR_NONE;
1139 bt_service_app_info_t *info = NULL;
1142 char *uuid_str = g_malloc0(BT_UUID_STRING_MAX);
1143 _bt_uuid_to_string(&(event->gatt_srvc_id.id.uuid), uuid_str);
1144 BT_INFO("GATT Added Service UUID: [%s] Server Ins [%d] Service hdl [%d] Is Primary [%d]\n",
1145 uuid_str, event->gatt_srvc_stat.server_inst, svc_handle, event->gatt_srvc_id.is_prmry);
1147 if (event->gatt_srvc_stat.status != OAL_STATUS_SUCCESS) {
1148 result = BLUETOOTH_ERROR_INTERNAL;
1149 svc_handle = 0; /* Service handle set to 0 indicates.
1150 0 is reserved by BT SIG, cant be used by app*/
1152 svc_handle = event->gatt_srvc_stat.servic_hndl;
1154 BT_INFO("GATT Added Service Status [%d] Server Ins [%d] Service hdl [%d] Is Primary [%d]\n",
1155 event->gatt_srvc_stat.status, event->gatt_srvc_stat.server_inst, svc_handle, event->gatt_srvc_id.is_prmry);
1157 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1158 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1161 if (info->instance_id == event->gatt_srvc_stat.server_inst) {
1162 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1163 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_ADD_SERVICE,
1164 (int*)&svc_handle, sizeof(int));
1166 /* Add Service Handle */
1167 if (svc_handle > 0) {
1168 handle = g_malloc0(sizeof(int));
1169 *handle = svc_handle;
1170 numapps[event->gatt_srvc_stat.server_inst].service_handles = g_slist_append(numapps[event->gatt_srvc_stat.server_inst].service_handles, handle);
1171 count = g_slist_length(numapps[event->gatt_srvc_stat.server_inst].service_handles);
1172 BT_INFO("Added Service handle [%d] to list..current count [%d]", svc_handle, count);
1181 static void __bt_handle_gatt_server_characteristic_added(event_gatts_srvc_charctr_t *event)
1183 int result = BLUETOOTH_ERROR_NONE;
1185 bt_service_app_info_t *info = NULL;
1187 char *uuid_str = g_malloc0(BT_UUID_STRING_MAX);
1189 BT_INFO("GATT Server Char added status [%d]", event->gatt_srvc_stat.status);
1190 BT_INFO("GATT Server_inst: [%d]", event->gatt_srvc_stat.server_inst);
1191 BT_INFO("GATT Service Handle: [%d]", event->gatt_srvc_stat.servic_hndl);
1192 BT_INFO("GATT Add characteristic Status: [%d]", event->gatt_srvc_stat.status);
1193 BT_INFO("GATT Service characteristic Handle: [%d]", event->charctr_hndl);
1195 if (event->gatt_srvc_stat.status != OAL_STATUS_SUCCESS) {
1196 result = BLUETOOTH_ERROR_INTERNAL;
1197 char_handle = 0; /* characteristic handle set to 0 indicates.
1198 0 is reserved by BT SIG, cant be used by app*/
1200 char_handle = event->charctr_hndl;
1203 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1204 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1207 if (info->instance_id == event->gatt_srvc_stat.server_inst) {
1208 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1209 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_ADD_CHARACTERISTIC,
1210 (int*)&char_handle, sizeof(int));
1216 _bt_uuid_to_string(&(event->charctr_uuid), uuid_str);
1217 BT_INFO("GATT Added Characteristic: UUID: [%s]", uuid_str);
1222 static void __bt_handle_gatt_server_descriptor_added(event_gatts_srvc_descr_t* event)
1224 int result = BLUETOOTH_ERROR_NONE;
1226 bt_service_app_info_t *info = NULL;
1228 char *uuid_str = g_malloc0(BT_UUID_STRING_MAX);
1230 BT_INFO("GATT Server Descriptor added status [%d]", event->gatt_srvc_stat.status);
1231 BT_INFO("GATT Server_inst: [%d]", event->gatt_srvc_stat.server_inst);
1232 BT_INFO("GATT Service Handle: [%d]", event->gatt_srvc_stat.servic_hndl);
1233 BT_INFO("GATT Add Descriptor Status: [%d]", event->gatt_srvc_stat.status);
1234 BT_INFO("GATT Service Descriptor Handle: [%d]", event->descrptr_hndl);
1236 if (event->gatt_srvc_stat.status != OAL_STATUS_SUCCESS) {
1237 result = BLUETOOTH_ERROR_INTERNAL;
1238 desc_handle = 0; /* Service handle set to 0 indicates.
1239 0 is reserved by BT SIG, cant be used by app*/
1241 desc_handle = event->descrptr_hndl;
1243 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1244 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1247 if (info->instance_id == event->gatt_srvc_stat.server_inst) {
1248 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1249 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_ADD_DESCRIPTOR,
1250 (int*)&desc_handle, sizeof(int));
1255 _bt_uuid_to_string(&(event->descrptr_uuid), uuid_str);
1256 BT_INFO("GATT Added Descriptor: UUID: [%s]", uuid_str);
1261 static void __bt_handle_gatt_server_service_started(event_gatts_srvc_t *event)
1263 bt_service_app_info_t *info = NULL;
1264 int result = BLUETOOTH_ERROR_NONE;
1266 BT_INFO("GATT Server Service Started..");
1269 BT_INFO("GATT Server_inst: [%d]", event->server_inst);
1270 BT_INFO("GATT Service Handle: [%d]", event->servic_hndl);
1271 BT_INFO("GATT Service Started Status: [%d]", event->status);
1273 if (event->status != OAL_STATUS_SUCCESS) {
1274 BT_ERR("GATT Server Service Start Failed Err: [%d]", event->status);
1275 result = BLUETOOTH_ERROR_INTERNAL;
1278 /* Check if the just registered Instance ID belongs to requester */
1279 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1282 if (info->instance_id == event->server_inst) {
1283 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1284 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_START_SERVICE,
1285 (void*)info, sizeof(bt_service_app_info_t));
1291 static void __bt_handle_gatt_server_service_stopped(event_gatts_srvc_t *event)
1293 int result = BLUETOOTH_ERROR_NONE;
1294 bt_service_app_info_t *info = NULL;
1296 BT_INFO("GATT Server Service Stopped..");
1298 BT_INFO("GATT Server_inst: [%d]", event->server_inst);
1299 BT_INFO("GATT Service Handle: [%d]", event->servic_hndl);
1300 BT_INFO("GATT Service Stopped Status: [%d]", event->status);
1302 if (event->status != OAL_STATUS_SUCCESS) {
1303 BT_ERR("GATT Server Service Stop Failed Err: [%d]", event->status);
1304 result = BLUETOOTH_ERROR_INTERNAL;
1307 /* Check if the just registered Instance ID belongs to requester */
1308 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1311 if (info->instance_id == event->server_inst) {
1312 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1313 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_STOP_SERVICE,
1314 (void*)info, sizeof(bt_service_app_info_t));
1320 static void __bt_handle_gatt_server_service_deleted(event_gatts_srvc_t *event)
1322 int result = BLUETOOTH_ERROR_NONE;
1323 bt_service_app_info_t *info = NULL;
1325 BT_INFO("GATT Server Service Deleted..");
1327 BT_INFO("GATT Server_inst: [%d]", event->server_inst);
1328 BT_INFO("GATT Service Handle: [%d]", event->servic_hndl);
1329 BT_INFO("GATT Service Deleted Status: [%d]", event->status);
1331 if (event->status != OAL_STATUS_SUCCESS) {
1332 BT_ERR("GATT Server Service Delete Failed Err: [%d]", event->status);
1333 result = BLUETOOTH_ERROR_INTERNAL;
1336 /* Check if the just registered Instance ID belongs to requester */
1337 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1340 if (info->instance_id == event->server_inst) {
1341 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1342 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_DELETE_SERVICE,
1343 (void*)info, sizeof(bt_service_app_info_t));
1349 static struct gatt_client_info_t *__bt_find_remote_gatt_client_info(char *address)
1352 struct gatt_client_info_t *info = NULL;
1353 for (l = gatt_client_info_list; l != NULL; l = g_slist_next(l)) {
1354 info = (struct gatt_client_info_t*)l->data;
1358 if (!g_strcmp0(info->addr, address)) {
1359 BT_INFO("Remote GATT client found addr[%s]", info->addr);
1366 static struct gatt_client_info_t *__bt_find_remote_gatt_client_info_from_conn_id(int conn_id)
1369 struct gatt_client_info_t *info = NULL;
1371 for (l = gatt_client_info_list; l != NULL; l = g_slist_next(l)) {
1372 info = (struct gatt_client_info_t*)l->data;
1376 if (info->connection_id == conn_id) {
1377 BT_INFO("Remote GATT client found addr[%s]", info->addr);
1384 #ifdef TIZEN_GATT_CLIENT
1385 static struct gatt_server_info_t *__bt_find_remote_gatt_server_info(char *address)
1388 struct gatt_server_info_t *info = NULL;
1389 for (l = gatt_server_info_list; l != NULL; l = g_slist_next(l)) {
1390 info = (struct gatt_server_info_t*)l->data;
1394 if (!g_strcmp0(info->addr, address)) {
1395 BT_INFO("Remote GATT Server found addr[%s]", info->addr);
1402 static struct gatt_out_conn_info_t* __bt_find_gatt_outgoing_conn_info(char *address)
1405 struct gatt_out_conn_info_t *info = NULL;
1406 for (l = outgoing_gatt_conn_list; l != NULL; l = g_slist_next(l)) {
1407 info = (struct gatt_out_conn_info_t*)l->data;
1411 if (!g_strcmp0(info->addr, address)) {
1412 BT_INFO("Outgoing connection info found addr[%s]", info->addr);
1419 static struct gatt_server_info_t *__bt_find_remote_gatt_server_info_from_client_if(int client_if)
1422 struct gatt_server_info_t *info = NULL;
1424 for (l = gatt_server_info_list; l != NULL; l = g_slist_next(l)) {
1425 info = (struct gatt_server_info_t*)l->data;
1429 if (info->client_id == client_if) {
1430 BT_INFO("Remote GATT server found addr[%s]", info->addr);
1441 #ifdef TIZEN_GATT_CLIENT
1442 static void __bt_handle_gatt_server_connection_state(event_gatts_conn_t *event)
1444 int result = BLUETOOTH_ERROR_NONE;
1445 struct gatt_client_info_t *conn_info = NULL;
1446 bluetooth_device_address_t dev_addr;
1447 GVariant *param = NULL;
1449 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1451 memcpy(dev_addr.addr, event->address.addr, 6);
1453 /* REPLY dbus Context to application which called BT_CONNECT_LE. There is status
1455 _bt_convert_addr_type_to_string(address,
1456 (unsigned char *)dev_addr.addr);
1457 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
1458 address, BT_ADDRESS_STRING_SIZE);
1460 BT_INFO("GATT Server Connedted: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1461 address, event->server_inst, event->conn_id);
1464 /* Check if device is already in connected list */
1465 conn_info = __bt_find_remote_gatt_client_info(address);
1468 BT_INFO("Conn Info absent: But no need to Send Local GATT Server Connected event to apps");
1470 param = g_variant_new("(is)", result, address);
1472 /* Send event to application */
1473 _bt_send_event(BT_DEVICE_EVENT,
1474 BLUETOOTH_EVENT_GATT_SERVER_CONNECTED, /* Local device is GATT server */
1477 /* Save Connection info */
1478 conn_info = g_new0(struct gatt_client_info_t, 1);
1479 conn_info->addr = g_strdup(address);
1480 BT_INFO("Added GATT client addr[%s]", conn_info->addr);
1481 conn_info->connection_id = event->conn_id;
1482 conn_info->instance_id = event->server_inst;
1483 gatt_client_info_list = g_slist_append(gatt_client_info_list, conn_info);
1484 BT_INFO("Total num of connected Remote GATT Clients [%d]", g_slist_length(gatt_client_info_list));
1490 /* GATT Server Dis connected */
1491 static void __bt_handle_gatt_server_disconnection_state(event_gatts_conn_t *event)
1493 int result = BLUETOOTH_ERROR_NONE;
1494 struct gatt_client_info_t *conn_info = NULL;
1495 bluetooth_device_address_t dev_addr;
1496 GVariant *param = NULL;
1497 char address[BT_ADDRESS_STRING_SIZE];
1499 memcpy(dev_addr.addr, event->address.addr, 6);
1501 /* REPLY dbus Context to application which called BT_DISCONNECT_LE. There is status
1503 _bt_convert_addr_type_to_string(address,
1504 (unsigned char *)dev_addr.addr);
1506 if (NULL == _bt_get_request_info_data(BT_DISCONNECT_LE, address)) {
1507 if (NULL != _bt_get_request_info_data(BT_CONNECT_LE, address)) {
1508 result = BLUETOOTH_ERROR_INTERNAL;
1510 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
1511 address, BT_ADDRESS_STRING_SIZE);
1513 BT_ERR("Failed to connect Local GATT Server Remote Client addr[%s]", address);
1517 __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE,
1518 address, BT_ADDRESS_STRING_SIZE);
1521 BT_INFO("Local GATT Server DisConnected: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1522 address, event->server_inst, event->conn_id);
1523 /* Remove Connection info */
1524 conn_info = __bt_find_remote_gatt_client_info(address);
1526 BT_INFO("No need to Send Local GATT Server Disconnected event to apps, just remove remote client info");
1528 param = g_variant_new("(is)", result, address);
1529 /* Send event to application */
1530 _bt_send_event(BT_DEVICE_EVENT,
1531 BLUETOOTH_EVENT_GATT_SERVER_DISCONNECTED, /* Local device is GATT server */
1534 /* Remove info from List */
1535 gatt_client_info_list = g_slist_remove(gatt_client_info_list, conn_info);
1536 BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
1537 g_free(conn_info->addr);
1543 static void __bt_handle_gatt_server_connection_state(event_gatts_conn_t *event)
1545 int result = BLUETOOTH_ERROR_NONE;
1546 struct gatt_client_info_t *conn_info = NULL;
1547 bt_service_app_info_t *info = NULL;
1548 bluetooth_device_address_t dev_addr;
1549 GVariant *param = NULL;
1552 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1554 memcpy(dev_addr.addr, event->address.addr, 6);
1557 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1558 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1561 if (info->instance_id == event->server_inst) {
1562 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1563 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
1564 (bluetooth_device_address_t*)&dev_addr, sizeof(bluetooth_device_address_t));
1570 /* Send event to BT-API */
1571 _bt_convert_addr_type_to_string(address,
1572 (unsigned char *)dev_addr.addr);
1574 BT_INFO("GATT Server Connedted: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1575 address, event->server_inst, event->conn_id);
1578 /* Check if device is already in connected list */
1579 conn_info = __bt_find_remote_gatt_client_info(address);
1582 param = g_variant_new("(is)", result, address);
1583 /* Send event to application */
1584 _bt_send_event(BT_GATT_SERVER_EVENT,
1585 BLUETOOTH_EVENT_GATT_CONNECTED,
1588 /* Save Connection info */
1589 conn_info = g_new0(struct gatt_client_info_t, 1);
1590 conn_info->addr = g_strdup(address);
1591 BT_INFO("Added GATT client addr[%s]", conn_info->addr);
1592 conn_info->connection_id = event->conn_id;
1593 conn_info->instance_id = event->server_inst;
1594 gatt_client_info_list = g_slist_append(gatt_client_info_list, conn_info);
1595 BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
1601 /* GATT Server Dis connected */
1602 static void __bt_handle_gatt_server_disconnection_state(event_gatts_conn_t *event)
1604 int result = BLUETOOTH_ERROR_NONE;
1605 struct gatt_client_info_t *conn_info = NULL;
1606 bluetooth_device_address_t dev_addr;
1607 bt_service_app_info_t *info = NULL;
1608 GVariant *param = NULL;
1611 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1613 memcpy(dev_addr.addr, event->address.addr, 6);
1615 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1616 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1619 if (info->instance_id == event->server_inst) {
1620 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1621 __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE,
1622 (bluetooth_device_address_t*)&dev_addr, sizeof(bluetooth_device_address_t));
1628 /* Send event to BT-API */
1629 _bt_convert_addr_type_to_string(address,
1630 (unsigned char *)dev_addr.addr);
1632 BT_INFO("GATT Server DisConnected: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1633 address, event->server_inst, event->conn_id);
1635 param = g_variant_new("(is)", result, address);
1636 /* Send event to application */
1637 _bt_send_event(BT_GATT_SERVER_EVENT,
1638 BLUETOOTH_EVENT_GATT_DISCONNECTED,
1641 /* Remove Connection info */
1642 conn_info = __bt_find_remote_gatt_client_info(address);
1644 BT_INFO("Remove GATT client info from List..");
1645 /* Remove info from List */
1646 gatt_client_info_list = g_slist_remove(gatt_client_info_list, conn_info);
1647 BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
1648 g_free(conn_info->addr);
1657 static void __bt_handle_gatt_server_acquire_write_requested(event_gatts_srvc_acquire_attr_t *event)
1659 GVariant *param = NULL;
1660 int result = BLUETOOTH_ERROR_NONE;
1661 struct gatt_server_req_info *req_info = NULL;
1662 bluetooth_device_address_t dev_addr;
1663 char adress[BT_ADDRESS_STRING_SIZE] = {'/0',};
1665 BT_INFO("GATT Server ACQUIRE Write Req Connection ID: [%d]", event->attr_trans.conn_id);
1666 BT_INFO("GATT Server ACQUIRE Write Req Transaction ID:[%d]", event->attr_trans.trans_id);
1667 BT_INFO("GATT Server ACQUIRE Write Req Attribute Handle: [%d]", event->attr_trans.attr_handle);
1669 //adress = g_malloc0(BT_ADDRESS_STRING_SIZE);
1670 memcpy(dev_addr.addr, event->address.addr, 6);
1672 req_info = g_new0(struct gatt_server_req_info, 1);
1673 req_info->request_id = event->attr_trans.trans_id;
1674 req_info->attribute_handle = event->attr_trans.attr_handle;
1675 req_info->connection_id = event->attr_trans.conn_id;
1676 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_ACQUIRE_WRITE;
1677 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1679 _bt_convert_addr_type_to_string(adress,
1680 (unsigned char *)dev_addr.addr);
1682 param = g_variant_new("(iiiiiis)", result,
1683 event->attr_trans.conn_id,
1684 event->attr_trans.trans_id,
1685 event->attr_trans.attr_handle,
1686 event->mtu, event->attr_trans.offset, adress);
1687 BT_INFO("GATT Server ACQUIRE Write Req Attribute remote adress : [%s]", adress);
1690 _bt_send_event(BT_GATT_SERVER_EVENT,
1691 BLUETOOTH_EVENT_GATT_SERVER_ACQUIRE_WRITE,
1696 static void __bt_handle_gatt_server_acquire_notify_requested(event_gatts_srvc_acquire_attr_t *event)
1698 GVariant *param = NULL;
1699 int result = BLUETOOTH_ERROR_NONE;
1700 struct gatt_server_req_info *req_info = NULL;
1702 BT_INFO("GATT Server ACQUIRE Notify Req Connection ID: [%d]", event->attr_trans.conn_id);
1703 BT_INFO("GATT Server ACQUIRE Notify Req Transaction ID:[%d]", event->attr_trans.trans_id);
1704 BT_INFO("GATT Server ACQUIRE Notify Req Attribute Handle: [%d]", event->attr_trans.attr_handle);
1706 req_info = g_new0(struct gatt_server_req_info, 1);
1707 req_info->request_id = event->attr_trans.trans_id;
1708 req_info->attribute_handle = event->attr_trans.attr_handle;
1709 req_info->connection_id = event->attr_trans.conn_id;
1710 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_ACQUIRE_NOTIFY;
1711 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1713 param = g_variant_new("(iiiiii)", result,
1714 event->attr_trans.conn_id,
1715 event->attr_trans.trans_id,
1716 event->attr_trans.attr_handle,
1717 event->mtu, event->attr_trans.offset);
1719 BT_INFO("GATT Server ACQUIRE Notify Req Attribute : ");
1721 _bt_send_event(BT_GATT_SERVER_EVENT,
1722 BLUETOOTH_EVENT_GATT_SERVER_ACQUIRE_NOTIFY,
1726 static void __bt_handle_gatt_server_write_requested(event_gatts_srvc_write_attr_t *event)
1729 bluetooth_device_address_t dev_addr;
1730 GVariant *param = NULL;
1731 int result = BLUETOOTH_ERROR_NONE;
1733 gboolean is_prepare_write;
1735 GVariant *data = NULL;
1737 struct gatt_server_req_info *req_info = NULL;
1738 BT_INFO("GATT Server Write Requested");
1740 memcpy(dev_addr.addr, event->address.addr, 6);
1742 BT_INFO("GATT Server Write Req Connection ID: [%d]", event->attr_trans.conn_id);
1743 BT_INFO("GATT Server Write Req Transaction ID:[%d]", event->attr_trans.trans_id);
1744 BT_INFO("GATT Server Write Req Attribute Handle: [%d]", event->attr_trans.attr_handle);
1745 BT_INFO("GATT Server Write Req Attribute Offset: [%d]", event->attr_trans.offset);
1746 BT_INFO("GATT Server Write Req value length [%d]", event->length);
1747 BT_INFO("GATT Server Write Req needs response: [%d]", event->need_rsp);
1748 BT_INFO("GATT Server Write Req Is Prep: [%d]", event->is_prep);
1750 if (event->length <= 0) {
1751 BT_INFO("GATT Server write requested, but length of attr value is 0");
1755 need_resp = event->need_rsp;
1756 is_prepare_write = event->is_prep;
1758 write_val = g_memdup(&event->value[0], event->length);
1760 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1761 _bt_convert_addr_type_to_string(address,
1762 (unsigned char *)dev_addr.addr);
1764 BT_INFO("GATT Server Write Request from remote client [%s]", address);
1766 if (event->length > 0) {
1767 for (i = 0; i < event->length; i++)
1768 BT_DBG("Data[%d] = [0x%x]", i, event->value[i]);
1770 /* Save Write Request Info */
1771 req_info = g_new0(struct gatt_server_req_info, 1);
1772 req_info->request_id = event->attr_trans.trans_id;
1773 req_info->attribute_handle = event->attr_trans.attr_handle;
1774 req_info->connection_id = event->attr_trans.conn_id;
1775 req_info->addr = address;
1776 req_info->offset = event->attr_trans.offset;
1777 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_WRITE;
1778 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1780 data = g_variant_new_from_data(
1781 G_VARIANT_TYPE_BYTESTRING,
1786 param = g_variant_new("(iiiiiibbsn@ay)", result,
1787 event->attr_trans.conn_id,
1788 event->attr_trans.trans_id,
1789 event->attr_trans.attr_handle,
1790 event->attr_trans.offset,
1798 _bt_send_event(BT_GATT_SERVER_EVENT,
1799 BLUETOOTH_EVENT_GATT_SERVER_VALUE_CHANGED,
1805 static void __bt_handle_gatt_server_read_requested(event_gatts_srvc_read_attr_t *event)
1807 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1808 bluetooth_device_address_t dev_addr;
1809 int result = BLUETOOTH_ERROR_NONE;
1810 struct gatt_server_req_info *req_info = NULL;
1811 GVariant *param = NULL;
1813 BT_INFO("GATT Server Read Requested");
1815 memcpy(dev_addr.addr, event->address.addr, 6);
1817 BT_INFO("GATT Server-Client Connection ID: [%d]", event->attr_trans.conn_id);
1818 BT_INFO("GATT Server-Client Transaction ID: [%d]", event->attr_trans.trans_id);
1819 BT_INFO("GATT Server Attribute Handle: [%d]", event->attr_trans.attr_handle);
1820 BT_INFO("GATT Server Attribute Offset for read: [%d]", event->attr_trans.offset);
1821 BT_INFO("GATT Server Attribute is long: [%d]", event->is_long);
1823 is_long = event->is_long;
1825 /* Save Read Request Info */
1826 req_info = g_new0(struct gatt_server_req_info, 1);
1827 req_info->request_id = event->attr_trans.trans_id;
1828 req_info->attribute_handle = event->attr_trans.attr_handle;
1829 req_info->connection_id = event->attr_trans.conn_id;
1830 req_info->addr = address;
1831 req_info->offset = event->attr_trans.offset;
1832 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_READ;
1833 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1835 /* Send event to BT-API */
1836 _bt_convert_addr_type_to_string(address,
1837 (unsigned char *)dev_addr.addr);
1839 BT_INFO("GATT Server Read Request from remote client [%s]", address);
1841 param = g_variant_new("(iiiiibs)", result,
1842 event->attr_trans.conn_id,
1843 event->attr_trans.trans_id,
1844 event->attr_trans.attr_handle,
1845 event->attr_trans.offset,
1849 _bt_send_event(BT_GATT_SERVER_EVENT,
1850 BLUETOOTH_EVENT_GATT_SERVER_READ_REQUESTED,
1854 static void __bt_handle_gatt_server_indicate_confirmed(event_gatts_ind_cnfrm_t *event)
1856 bluetooth_device_address_t dev_addr;
1857 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1858 int cur_connected_clients;
1859 static int recvd = 0;
1860 gboolean completed = 0;
1861 GVariant *param = NULL;
1863 /* OAL event does provide error, so MW assumes event will never contain wrong data,
1864 incase of any issues, check with OAL */
1865 int result = BLUETOOTH_ERROR_NONE;
1867 memcpy(dev_addr.addr, event->address.addr, 6);
1868 _bt_convert_addr_type_to_string(address,
1869 (unsigned char *)dev_addr.addr);
1871 BT_INFO("Indication sent to GATT client [%s] conn_ ID [%d] transaction ID [%d] Att handle [%d]",
1872 address, event->conn_id, event->trans_id, event->attr_handle);
1875 cur_connected_clients = g_slist_length(gatt_client_info_list);
1876 BT_INFO("Number of connected clients during sending Indication [%d] & current connected count [%d]",
1877 num_indicate_clients, cur_connected_clients);
1880 if (recvd == num_indicate_clients) {
1881 BT_INFO("Gatt indication confirm event for last GATT client.. [%s]", address);
1882 completed = 1; /* Last event */
1883 recvd = 0; /* Reset */
1884 num_indicate_clients = 0;
1887 param = g_variant_new("(isib)",
1893 /* Send event to BT-API */
1894 _bt_send_event(BT_GATT_SERVER_EVENT,
1895 BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_COMPLETED,
1898 BT_INFO("Received Indication confirm for client number [%d]", recvd);
1902 /* Tizen Platform Specific */
1903 static void __bt_handle_gatt_server_notification_changed(event_gatts_notif_t *event)
1905 bluetooth_device_address_t dev_addr;
1906 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1907 GVariant *param = NULL;
1910 /* OAL event does provide error, so MW assumes event will never contain wrong data,
1911 incase of any issues, check with OAL */
1912 int result = BLUETOOTH_ERROR_NONE;
1914 memcpy(dev_addr.addr, event->address.addr, 6);
1915 _bt_convert_addr_type_to_string(address,
1916 (unsigned char *)dev_addr.addr);
1918 BT_INFO("notification_changed [%s] conn_ ID [%d] transaction ID [%d] Att handle [%d] Notify[%d]",
1919 address, event->conn_id, event->trans_id, event->attr_handle, event->notify);
1921 /* Set Notifcation status */
1922 notify = event->notify;
1924 param = g_variant_new("(isib)",
1930 /* Send event to BT-API */
1931 _bt_send_event(BT_GATT_SERVER_EVENT,
1932 BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_STATE_CHANGED,
1938 static void __bt_handle_gatt_mtu_changed_event(event_gatts_mtu_changed_t *event)
1940 int result = BLUETOOTH_ERROR_NONE;
1941 struct gatt_client_info_t *conn_info = NULL;
1942 GVariant *param = NULL;
1944 BT_INFO("GATT Server MTU changed event");
1946 conn_info = __bt_find_remote_gatt_client_info_from_conn_id(event->conn_id);
1947 if (conn_info == NULL) {
1948 BT_ERR("Cant find connection Information");
1951 BT_INFO("Got connection Info GATT client [%s] MTU Size [%d]",
1952 conn_info->addr, event->mtu_size);
1954 param = g_variant_new("(isqy)",
1960 /* Send event to BT-API */
1961 _bt_send_event(BT_GATT_SERVER_EVENT,
1962 BLUETOOTH_EVENT_GATT_ATT_MTU_CHANGED,
1966 static void __bt_gatt_event_handler(int event_type, gpointer event_data)
1968 BT_INFO("OAL event = 0x%x, \n", event_type);
1969 switch (event_type) {
1970 case OAL_EVENT_BLE_SERVER_INSTANCE_INITIALISED: {
1971 BT_INFO("OAL Event: Server Instance Registered");
1972 /* GATT Server Registered event is handled in MAIN thread context */
1973 __bt_handle_server_instance_registered((event_gatts_register_t *)event_data);
1976 case OAL_EVENT_GATTS_SERVICE_ADDED: {
1977 BT_INFO("OAL Event: GATT Service added");
1978 __bt_handle_gatt_server_service_added((event_gatts_srvc_prm_t *)event_data);
1981 case OAL_EVENT_GATTS_CHARACTERISTIC_ADDED: {
1982 BT_INFO("OAL Event: GATT characteristic added");
1983 __bt_handle_gatt_server_characteristic_added((event_gatts_srvc_charctr_t *)event_data);
1986 case OAL_EVENT_GATTS_DESCRIPTOR_ADDED: {
1987 BT_INFO("OAL Event: GATT descriptor added");
1988 __bt_handle_gatt_server_descriptor_added((event_gatts_srvc_descr_t *)event_data);
1991 case OAL_EVENT_GATTS_SERVICE_STARTED: {
1992 BT_INFO("OAL Event: GATT Service started");
1993 __bt_handle_gatt_server_service_started((event_gatts_srvc_t *)event_data);
1996 case OAL_EVENT_GATTS_SERVICE_STOPED: {
1997 BT_INFO("OAL Event: GATT Service stopped");
1998 __bt_handle_gatt_server_service_stopped((event_gatts_srvc_t *)event_data);
2001 case OAL_EVENT_GATTS_SERVICE_DELETED: {
2002 BT_INFO("OAL Event: GATT Service deleted");
2003 __bt_handle_gatt_server_service_deleted((event_gatts_srvc_t *) event_data);
2006 case OAL_EVENT_GATTS_CONNECTION_COMPLETED: {
2007 BT_INFO("OAL Event: GATT Server Connected");
2008 __bt_handle_gatt_server_connection_state((event_gatts_conn_t *)event_data);
2011 case OAL_EVENT_GATTS_DISCONNECTION_COMPLETED: {
2012 BT_INFO("OAL Event: GATT Server Disconnected");
2013 __bt_handle_gatt_server_disconnection_state((event_gatts_conn_t *)event_data);
2016 case OAL_EVENT_GATTS_REQUEST_READ: {
2017 BT_INFO("OAL Event: GATT Server Read Request");
2018 __bt_handle_gatt_server_read_requested((event_gatts_srvc_read_attr_t *)event_data);
2021 case OAL_EVENT_GATTS_REQUEST_WRITE: {
2022 BT_INFO("OAL Event: GATT Server Write Request");
2023 __bt_handle_gatt_server_write_requested((event_gatts_srvc_write_attr_t *)event_data);
2026 case OAL_EVENT_GATTS_REQUEST_ACQUIRE_WRITE: {
2027 BT_INFO("OAL Event: GATT Server Acquire Write Request");
2028 __bt_handle_gatt_server_acquire_write_requested((event_gatts_srvc_acquire_attr_t*)event_data);
2031 case OAL_EVENT_GATTS_REQUEST_ACQUIRE_NOTIFY: {
2032 BT_INFO("OAL Event: GATT ServerAcquire Notify Request");
2033 __bt_handle_gatt_server_acquire_notify_requested((event_gatts_srvc_acquire_attr_t*)event_data);
2036 case OAL_EVENT_GATTS_IND_CONFIRM: {
2037 BT_INFO("OAL Event: GATT Server Indication confirmed");
2038 __bt_handle_gatt_server_indicate_confirmed((event_gatts_ind_cnfrm_t *)event_data);
2041 case OAL_EVENT_GATTS_NOTIFICATION: { /* Tizen Platform Specific */
2042 BT_INFO("OAL Event: GATT Server DisConnected");
2043 __bt_handle_gatt_server_notification_changed((event_gatts_notif_t *)event_data);
2046 case OAL_EVENT_GATTS_MTU_CHANGED: {
2047 BT_INFO("OAL Event: GATT Server MTU changed event callback");
2048 __bt_handle_gatt_mtu_changed_event((event_gatts_mtu_changed_t *)event_data);
2051 #ifdef TIZEN_GATT_CLIENT
2052 case OAL_EVENT_GATTC_REGISTRATION: {
2053 BT_INFO("OAL Event: GATT Client instance Registered");
2054 __bt_handle_client_instance_registered((event_gattc_register_t *) event_data);
2057 case OAL_EVENT_GATTC_CONNECTION_COMPLETED: {
2058 BT_INFO("OAL Event: GATT Client Connected");
2059 __bt_handle_client_connected((event_gattc_conn_t *) event_data);
2062 case OAL_EVENT_GATTC_DISCONNECTION_COMPLETED: {
2063 BT_INFO("OAL Event: GATT Client DisConnected");
2064 __bt_handle_client_disconnected((event_gattc_conn_t *) event_data);
2067 case OAL_EVENT_GATTC_SERVICE_SEARCH_RESULT: {
2068 BT_INFO("OAL Event: GATT Client Service Search Result");
2069 __bt_handle_client_service_search_result((event_gattc_service_result_t *) event_data);
2072 case OAL_EVENT_GATTC_SERVICE_SEARCH_DONE: {
2073 BT_INFO("OAL Event: GATT Client Service Completed");
2074 __bt_handle_client_service_search_completed((event_gattc_conn_status_t *) event_data);
2077 case OAL_EVENT_GATTC_CHARAC_SERACH_RESULT: {
2078 BT_INFO("OAL Event: GATT Client Characteristic Search Result");
2079 __bt_handle_client_characteristic_search_result((event_gattc_characteristic_result_t *) event_data);
2082 case OAL_EVENT_GATTC_DESC_SERACH_RESULT: {
2083 BT_INFO("OAL Event: GATT Client Descriptor Search Result");
2084 __bt_handle_client_descriptor_search_result((event_gattc_descriptor_result_t *) event_data);
2087 case OAL_EVENT_GATTC_READ_CHARAC: {
2088 BT_INFO("OAL Event: GATT Client Characteristic Read Data");
2089 __bt_handle_client_characteristic_read_data((event_gattc_read_data *) event_data);
2092 case OAL_EVENT_GATTC_READ_DESCR: {
2093 BT_INFO("OAL Event: GATT Client Descriptor Read Data");
2094 __bt_handle_client_descriptor_read_data((event_gattc_read_data *) event_data);
2097 case OAL_EVENT_GATTC_WRITE_CHARAC: {
2098 BT_INFO("OAL Event: GATT Client Characteristic Write Data");
2099 __bt_handle_client_characteristic_write_data((event_gattc_write_data *) event_data);
2102 case OAL_EVENT_GATTC_WRITE_DESCR: {
2103 BT_INFO("OAL Event: GATT Client Descriptor Write Data");
2104 __bt_handle_client_descriptor_write_data((event_gattc_write_data *) event_data);
2107 case OAL_EVENT_DEVICE_LE_DISCONNECTED: {
2108 BT_INFO("OAL Event: LE device disconnected");
2109 __bt_hanlde_le_device_disconnection((event_dev_conn_status_t *)event_data);
2112 case OAL_EVENT_GATTC_NOTIFICATION_REGISTERED: {
2113 BT_INFO("OAL Event: GATT Client Notification Registered");
2114 __bt_handle_client_notification_registered((event_gattc_regdereg_notify_t *) event_data, TRUE);
2117 case OAL_EVENT_GATTC_NOTIFICATION_DEREGISTERED: {
2118 BT_INFO("OAL Event: GATT Client Notification Registered");
2119 __bt_handle_client_notification_registered((event_gattc_regdereg_notify_t *) event_data, FALSE);
2129 int _bt_gatt_server_add_service(char *sender, int service_type,
2130 int num_handles, char *svc_uuid, int instance_id)
2132 BT_CHECK_PARAMETER(svc_uuid, return);
2133 BT_CHECK_PARAMETER(sender, return);
2134 int ret = OAL_STATUS_SUCCESS;
2136 oal_gatt_srvc_id_t svc_data;
2138 svc_data.is_prmry = service_type;
2139 svc_data.id.inst_id = instance_id;
2141 BT_INFO("Service UUID [%s] Num handles [%d] Instance ID [%d]", svc_uuid, num_handles, instance_id);
2142 _bt_string_to_uuid(svc_uuid, (service_uuid_t*)&svc_data.id.uuid);
2144 ret = gatts_add_service(instance_id, &svc_data, num_handles);
2145 if (ret != OAL_STATUS_SUCCESS) {
2146 BT_ERR("ret: %d", ret);
2147 return BLUETOOTH_ERROR_INTERNAL;
2150 return BLUETOOTH_ERROR_NONE;
2154 int _bt_gatt_server_add_included_service(char *sender, int instance_id,
2155 int service_handle, int included_svc_handle)
2157 BT_CHECK_PARAMETER(sender, return);
2158 int ret = OAL_STATUS_SUCCESS;
2160 ret = gatts_add_included_services(instance_id, service_handle, included_svc_handle);
2161 if (ret != OAL_STATUS_SUCCESS) {
2162 BT_ERR("ret: %d", ret);
2163 return BLUETOOTH_ERROR_INTERNAL;
2165 return BLUETOOTH_ERROR_NONE;
2168 int _bt_gatt_server_add_characteristic(char *sender, char *char_uuid,
2169 bluetooth_gatt_server_attribute_params_t *param)
2171 BT_CHECK_PARAMETER(char_uuid, return);
2172 BT_CHECK_PARAMETER(sender, return);
2173 BT_CHECK_PARAMETER(param, return);
2174 int ret = OAL_STATUS_SUCCESS;
2176 oal_uuid_t uuid = {{0} };
2178 BT_INFO("Char UUID [%s] Instance ID [%d]", char_uuid, param->instance_id);
2179 _bt_string_to_uuid(char_uuid, (service_uuid_t*)&uuid);
2181 BT_INFO("Char permission From API [0x%x]", param->permissions);
2183 ret = gatts_add_characteristics(param->instance_id, param->service_handle, &uuid,
2184 param->properties, (int)param->permissions);
2185 if (ret != OAL_STATUS_SUCCESS) {
2186 BT_ERR("ret: %d", ret);
2187 return BLUETOOTH_ERROR_INTERNAL;
2189 return BLUETOOTH_ERROR_NONE;
2192 int _bt_gatt_server_add_descriptor(char *sender, char *desc_uuid,
2193 bt_gatt_permission_t *param, int service_handle, int instance_id)
2195 BT_CHECK_PARAMETER(desc_uuid, return);
2196 BT_CHECK_PARAMETER(sender, return);
2197 BT_CHECK_PARAMETER(param, return);
2198 int ret = OAL_STATUS_SUCCESS;
2200 oal_uuid_t uuid = {{0} };
2202 BT_INFO("Descriptor UUID [%s] Instance ID [%d] Service handle [%d]",
2203 desc_uuid, service_handle, instance_id);
2205 _bt_string_to_uuid(desc_uuid, (service_uuid_t*)&uuid);
2207 BT_INFO("Descriptor permission From API [0x%x]", *param);
2208 ret = gatts_add_descriptor(instance_id, service_handle, &uuid, (int)*param);
2210 if (ret != OAL_STATUS_SUCCESS) {
2211 BT_ERR("ret: %d", ret);
2212 return BLUETOOTH_ERROR_INTERNAL;
2214 return BLUETOOTH_ERROR_NONE;
2217 int _bt_gatt_server_start_service(char *sender, int service_handle, int instance_id)
2219 BT_CHECK_PARAMETER(sender, return);
2220 int ret = OAL_STATUS_SUCCESS;
2222 ret = gatts_start_service(instance_id, service_handle, BT_GATT_TRANSPORT_LE);
2223 if (ret != OAL_STATUS_SUCCESS) {
2224 BT_ERR("ret: %d", ret);
2225 return BLUETOOTH_ERROR_INTERNAL;
2227 return BLUETOOTH_ERROR_NONE;
2230 int _bt_gatt_server_stop_service(char *sender, int service_handle, int instance_id)
2232 BT_CHECK_PARAMETER(sender, return);
2233 int ret = OAL_STATUS_SUCCESS;
2235 ret = gatts_stop_service(instance_id, service_handle);
2236 if (ret != OAL_STATUS_SUCCESS) {
2237 BT_ERR("ret: %d", ret);
2238 return BLUETOOTH_ERROR_INTERNAL;
2240 return BLUETOOTH_ERROR_NONE;
2243 int _bt_gatt_server_delete_service(char *sender, int service_handle, int instance_id)
2245 BT_CHECK_PARAMETER(sender, return);
2246 int ret = OAL_STATUS_SUCCESS;
2250 ret = gatts_delete_service(instance_id, service_handle);
2251 if (ret != OAL_STATUS_SUCCESS) {
2252 BT_ERR("ret: %d", ret);
2253 return BLUETOOTH_ERROR_INTERNAL;
2256 /* Remove the Service Handle */
2257 for (l = numapps[instance_id].service_handles; l != NULL;) {
2259 l = g_slist_next(l);
2260 if (handle && *handle == service_handle) {
2261 BT_INFO("Remove Service handle [%d]", *handle);
2262 numapps[instance_id].service_handles = g_slist_remove(numapps[instance_id].service_handles, handle);
2268 return BLUETOOTH_ERROR_NONE;
2271 int _bt_gatt_server_send_response(char *sender, bluetooth_gatt_att_data_t *data,
2272 bluetooth_gatt_server_response_params_t *param)
2274 BT_CHECK_PARAMETER(sender, return);
2275 BT_CHECK_PARAMETER(data, return);
2276 BT_CHECK_PARAMETER(param, return);
2277 struct gatt_server_req_info *req_info = NULL;
2278 int ret = OAL_STATUS_SUCCESS;
2280 oal_gatt_response_t response;
2282 BT_INFO("GATT Server Response: Req Type [%d] req_id [%d] status [%d] auth_req [%d] offset[%d] data len[%d]",
2283 param->req_type, param->request_id,
2284 param->response_status, param->auth_req,
2285 data->offset, data->length);
2287 /* Search for matching Request in List */
2288 req_info = __bt_gatt_server_find_request_info(param->request_id, param->req_type);
2290 BT_ERR("GATT Server Req Info not found for current response..return Error");
2291 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
2294 memset(&response, 0x00, sizeof(oal_gatt_response_t));
2296 response.handle = req_info->attribute_handle;
2297 response.attr_value.auth_req = param->auth_req;
2298 response.attr_value.handle = req_info->attribute_handle;
2299 response.attr_value.offset = data->offset;
2300 response.attr_value.len = data->length;
2301 memcpy(&response.attr_value.value, &data->data, data->length);
2304 ret = gatts_send_response(req_info->connection_id, param->request_id,
2305 param->response_status, &response);
2307 if (ret != OAL_STATUS_SUCCESS) {
2308 BT_ERR("ret: %d", ret);
2309 return BLUETOOTH_ERROR_INTERNAL;
2312 BT_INFO("GATT Server Response successfully sent");
2313 /* Remove GATT server request from list */
2314 gatt_server_requests = g_slist_remove(gatt_server_requests, req_info);
2315 g_free(req_info->addr);
2317 return BLUETOOTH_ERROR_NONE;
2320 int _bt_gatt_server_acquire_send_response(char *sender, bluetooth_gatt_server_acquire_response_params_t *param , void *fd_list)
2322 BT_CHECK_PARAMETER(sender, return);
2323 BT_CHECK_PARAMETER(param, return);
2324 struct gatt_server_req_info *req_info = NULL;
2325 int ret = OAL_STATUS_SUCCESS;
2328 BT_INFO("GATT acquire Server Response: Req Type [%d] req_id [%d] fd [%d] mtu[%d]",
2329 param->req_type, param->request_id,
2333 /* Search for matching Request in List */
2334 req_info = __bt_gatt_server_find_request_info(param->request_id, param->req_type);
2336 BT_ERR("GATT acquire Server Req Info not found for current response..return Error");
2337 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
2340 ret = gatt_send_response_acquire(req_info->connection_id, param->request_id, 0, param->fd, param->mtu, fd_list);
2342 if (ret != OAL_STATUS_SUCCESS) {
2343 BT_ERR("ret: %d", ret);
2344 return BLUETOOTH_ERROR_INTERNAL;
2347 BT_INFO("GATT acquire Server Response successfully sent");
2348 /* Remove GATT server request from list */
2349 gatt_server_requests = g_slist_remove(gatt_server_requests, req_info);
2350 g_free(req_info->addr);
2352 return BLUETOOTH_ERROR_NONE;
2357 int _bt_gatt_server_send_indication(char *sender, bluetooth_device_address_t *dev_addr,
2358 bluetooth_gatt_att_data_t *data,
2359 bluetooth_gatt_server_indication_params_t *param)
2361 BT_CHECK_PARAMETER(sender, return);
2362 BT_CHECK_PARAMETER(data, return);
2363 BT_CHECK_PARAMETER(param, return);
2365 gboolean all_send = FALSE;
2366 int ret = OAL_STATUS_SUCCESS;
2367 struct gatt_client_info_t *conn;
2369 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2370 _bt_convert_addr_type_to_string(address, dev_addr->addr);
2372 if (memcmp(dev_addr->addr, BDADDR_ANY, 6) == 0) {
2373 BT_INFO("GATT Server: Send Indication to all connected GATT clients..");
2376 BT_INFO("GATT Server: Send Indication to connected GATT client addr [%s]", address);
2379 /* Attempt to send Notification/Indication to all Connected GATT clients */
2381 ret = __bt_gatt_send_indication_to_all_connected_clients(data, param);
2382 if (ret != OAL_STATUS_SUCCESS) {
2383 BT_ERR("ret: %d", ret);
2385 return BLUETOOTH_ERROR_INTERNAL;
2389 conn = __bt_find_remote_gatt_client_info(address);
2391 ret = gatts_send_indication(param->instance_id, param->atrribute_handle,
2392 conn->connection_id, data->length,
2393 param->need_confirmation, (char *)(&data->data[0]));
2395 if (ret != OAL_STATUS_SUCCESS) {
2396 BT_ERR("ret: %d", ret);
2397 BT_INFO("Indication failed to send to Remote GATT Client [%s]", address);
2399 return BLUETOOTH_ERROR_INTERNAL;
2401 BT_INFO("Indication sent to Remote GATT Client [%s] wait for Notification completed event from OAL", address);
2403 num_indicate_clients = 1;
2404 return BLUETOOTH_ERROR_NONE;
2406 BT_ERR("Remote GATT client [%s] is not connected..Cant send Indication!!", address);
2408 return BLUETOOTH_ERROR_NOT_CONNECTED;
2411 return BLUETOOTH_ERROR_NONE;
2414 int _bt_gatt_server_update_attribute_value(char *sender, int instance_id,
2415 bluetooth_gatt_server_update_value_t *param)
2417 BT_CHECK_PARAMETER(sender, return);
2418 BT_CHECK_PARAMETER(param, return);
2419 int ret = OAL_STATUS_SUCCESS;
2421 oal_gatt_value_t value;
2422 BT_INFO("GATT Server Update value: Instance ID [%d] attr handle [%d] Value len [%d]",
2423 instance_id, param->attribute_handle, param->length);
2426 memset(&value, 0x00, sizeof(oal_gatt_value_t));
2428 value.handle = param->attribute_handle;
2429 value.len = param->length;
2430 memcpy(&value.value, ¶m->data.data, param->length);
2432 ret = gatts_update_att_value(instance_id, &value);
2434 if (ret != OAL_STATUS_SUCCESS) {
2435 BT_ERR("ret: %d", ret);
2436 return BLUETOOTH_ERROR_INTERNAL;
2439 BT_INFO("GATT Server Update Attribute Value successfully");
2440 return BLUETOOTH_ERROR_NONE;
2443 int _bt_get_att_mtu(bluetooth_device_address_t *address,
2446 BT_CHECK_PARAMETER(address, return);
2447 BT_CHECK_PARAMETER(mtu, return);
2448 struct gatt_client_info_t *conn_info = NULL;
2449 char addr[BT_ADDRESS_STRING_SIZE] = { 0 };
2450 int ret = OAL_STATUS_SUCCESS;
2453 _bt_convert_addr_type_to_string(addr, address->addr);
2455 BT_INFO("Get current MTU size for the remote client:DevAddress:[%s]", addr);
2457 conn_info = __bt_find_remote_gatt_client_info(addr);
2459 BT_INFO("GATT Client [%s] is connected, conn Id [%d] Instance ID [%d]",
2460 conn_info->addr, conn_info->connection_id, conn_info->instance_id);
2462 BT_ERR("GATT Client [%s] is not yet connected..");
2463 return BLUETOOTH_ERROR_NOT_CONNECTED;
2466 ret = gatts_get_att_mtu(conn_info->connection_id, &stack_mtu);
2467 if (ret != OAL_STATUS_SUCCESS) {
2468 BT_ERR("ret: %d", ret);
2469 return BLUETOOTH_ERROR_INTERNAL;
2471 BT_INFO("ATT MTU received from OAL [%d]", stack_mtu);
2472 *mtu = (unsigned int)stack_mtu;
2473 return BLUETOOTH_ERROR_NONE;
2476 #ifdef TIZEN_GATT_CLIENT
2477 /* GATT Client utility static functions */
2478 static bt_gatt_service_info_list_t * __bt_get_service_info_list(int conn_id)
2481 bt_gatt_service_info_list_t *info = NULL;
2483 for (l = list_gatt_info; l != NULL; l = g_slist_next(l)) {
2484 info = (bt_gatt_service_info_list_t*)l->data;
2488 if (info->conn_id == conn_id)
2495 static bt_gatt_service_info_t* __bt_find_matching_service(
2496 bt_gatt_service_info_list_t *svc_list, oal_gatt_srvc_id_t *svc)
2499 bt_gatt_service_info_t *info = NULL;
2501 for (l = svc_list->services; l != NULL; l = g_slist_next(l)) {
2502 info = (bt_gatt_service_info_t*)l->data;
2506 /* Match UUID and instance ID */
2507 if (!memcmp(&svc->id.uuid.uuid, &info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
2508 && (svc->id.inst_id == info->inst_id)) {
2515 static bt_gatt_char_info_t* __bt_find_matching_charc(
2516 bt_gatt_service_info_t *svc_info, oal_gatt_id_t *charc)
2519 bt_gatt_char_info_t *info = NULL;
2521 for (l = svc_info->chars; l != NULL; l = g_slist_next(l)) {
2522 info = (bt_gatt_char_info_t*)l->data;
2526 /* Match UUID and instance ID */
2527 if (!memcmp(&charc->uuid.uuid, &info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
2528 && (charc->inst_id == info->inst_id)) {
2535 static bt_gatt_descriptor_info_t* __bt_find_matching_desc(
2536 bt_gatt_char_info_t *char_info, oal_gatt_id_t *desc)
2539 bt_gatt_descriptor_info_t *info = NULL;
2541 for (l = char_info->descs; l != NULL; l = g_slist_next(l)) {
2542 info = (bt_gatt_descriptor_info_t*)l->data;
2546 /* Match UUID and instance ID */
2547 if (!memcmp(&desc->uuid, &info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
2548 && (desc->inst_id == info->inst_id)) {
2556 static struct gatt_server_info_t *__bt_find_remote_gatt_server_info_from_conn_id(int conn_id)
2559 struct gatt_server_info_t *info = NULL;
2561 for (l = gatt_server_info_list; l != NULL; l = g_slist_next(l)) {
2562 info = (struct gatt_server_info_t*)l->data;
2566 if (info->connection_id == conn_id) {
2567 BT_INFO("Remote GATT server found addr[%s]", info->addr);
2574 static bt_gatt_service_info_t* __bt_find_removed_service(bt_gatt_service_info_list_t *svc_list)
2577 bt_gatt_service_info_t *info = NULL;
2579 for (l = svc_list->services; l != NULL; l = g_slist_next(l)) {
2580 info = (bt_gatt_service_info_t*)l->data;
2584 /* Service is marked a removed */
2585 if (info->is_removed == 1)
2591 static void __bt_remove_service_info_from_list(bt_gatt_service_info_t *svc_info)
2597 bt_gatt_char_info_t *charc = NULL;
2598 bt_gatt_included_service_info_t *incl = NULL;
2599 bt_gatt_descriptor_info_t *desc = NULL;
2601 /* Remove all Characteristic and Descriptors within characteristic */
2602 for (l = svc_info->chars; l != NULL;) {
2603 charc = (bt_gatt_char_info_t*)l->data;
2604 l = g_slist_next(l); /* Incase if l is removed, saving next to l */
2609 /* Inside Characteristic */
2610 for (l1 = charc->descs; l1 != NULL;) {
2612 desc = (bt_gatt_descriptor_info_t*)l1->data;
2613 l1 = g_slist_next(l1);
2618 /* Remove Descriptor */
2619 charc->descs = g_slist_remove(charc->descs, desc);
2622 /* Remove Characteristic */
2623 svc_info->chars = g_slist_remove(svc_info->chars, charc);
2627 /* Remove all Included Services */
2628 for (l2 = svc_info->included_svcs; l2 != NULL;) {
2629 incl = (bt_gatt_included_service_info_t*)l2->data;
2630 l2 = g_slist_next(l2); /* Incase if l is removed, saving next to l */
2635 /* Remove included service */
2636 svc_info->included_svcs = g_slist_remove(svc_info->included_svcs, incl);
2643 static void __bt_build_service_browse_info(int conn_id,
2644 bt_services_browse_info_t* info)
2647 bt_gatt_service_info_list_t *svc_info_list;
2648 bt_gatt_service_info_t *svc_info;
2650 service_uuid_t uuid;
2651 struct gatt_server_info_t *conn_info = NULL;
2653 char uuid_string[BLUETOOTH_UUID_STRING_MAX];
2656 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
2661 _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
2663 svc_info_list = __bt_get_service_info_list(conn_id);
2668 info->count = g_slist_length(svc_info_list->services);
2669 BT_INFO("Total services present in the svc info list for this conn id [%d] is [%d]",
2670 conn_id, info->count);
2672 for (l = svc_info_list->services; l != NULL; l = g_slist_next(l)) {
2673 svc_info = (bt_gatt_service_info_t*)l->data;
2674 if (svc_info == NULL)
2677 memcpy(&uuid.uuid, &svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2678 _bt_uuid_to_string(&uuid, uuid_string);
2680 BT_INFO("Service UUID formed [%s] strlen [%d]", uuid_string, strlen(uuid_string));
2682 /* Fill UUID of service */
2683 g_strlcpy(info->uuids[count], uuid_string,
2684 BLUETOOTH_UUID_STRING_MAX);
2686 BT_INFO("Service UUID formed TO be sent [%s] strlen [%d]",
2687 info->uuids[count], strlen(info->uuids[count]));
2688 /* Fill instance ID of service */
2689 info->inst_id[count] = svc_info->inst_id;
2691 /* Fill primary service or not info */
2692 info->primary[count] = svc_info->is_primary;
2694 /* Increment count of services browsed */
2698 BT_INFO("Total services browsed [%d]", count);
2701 static void __bt_build_char_browse_info(int conn_id,
2702 bt_gatt_service_info_t *svc_info,
2703 bt_char_browse_info_t* info)
2706 bt_gatt_char_info_t *char_info;
2707 service_uuid_t uuid;
2709 struct gatt_server_info_t *conn_info = NULL;
2711 char uuid_string[BLUETOOTH_UUID_STRING_MAX];
2715 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
2719 /* Fill default data, this will be required even in case of failure */
2720 _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
2721 memcpy(&info->svc_uuid, svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2722 info->svc_inst_id = svc_info->inst_id;
2724 if (!svc_info->chars) {
2725 BT_ERR("No Chars browsed for address [%s]", conn_info->addr);
2729 info->count = g_slist_length(svc_info->chars);
2730 BT_INFO("Total count of Characteristics [%d]", info->count);
2732 for (l = svc_info->chars; l != NULL; l = g_slist_next(l)) {
2733 char_info = (bt_gatt_char_info_t*)l->data;
2734 if (char_info == NULL)
2737 memcpy(&uuid.uuid, &char_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2738 _bt_uuid_to_string(&uuid, uuid_string);
2740 /* Fill UUID of characteristic */
2741 g_strlcpy(info->uuids[count], uuid_string,
2742 BLUETOOTH_UUID_STRING_MAX);
2744 /* Fill instance ID of characteristic */
2745 info->inst_id[count] = char_info->inst_id;
2747 /* Fill property of characteristic */
2748 info->props[count] = char_info->props;
2750 /* Increment count of services browsed */
2753 BT_INFO("Total characteristics browsed [%d]", count);
2756 static void __bt_build_descriptor_browse_info(int conn_id,
2757 bt_gatt_service_info_t *svc_info,
2758 bt_gatt_char_info_t *char_info,
2759 bt_descriptor_browse_info_t* info)
2762 bt_gatt_descriptor_info_t *desc_info;
2764 service_uuid_t uuid;
2765 struct gatt_server_info_t *conn_info = NULL;
2767 char uuid_string[BLUETOOTH_UUID_STRING_MAX];
2769 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
2771 /* Fill default data, this will be required even in case of failure */
2772 _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
2773 memcpy(&info->svc_uuid, svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2774 info->svc_inst_id = svc_info->inst_id;
2775 memcpy(&info->char_uuid, char_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2776 info->char_inst_id = char_info->inst_id;
2778 /* Fill property of the parent characteristic of this descriptor */
2779 info->char_props_map = char_info->props;
2781 info->count = g_slist_length(char_info->descs);
2782 BT_INFO("Total count of Descriptors [%d]", info->count);
2784 if (!char_info->descs) {
2785 BT_ERR("No Descriptors browsed for address [%s]", conn_info->addr);
2789 for (l = char_info->descs; l != NULL; l = g_slist_next(l)) {
2790 desc_info = (bt_gatt_descriptor_info_t*)l->data;
2791 if (desc_info == NULL)
2794 memcpy(&uuid.uuid, &desc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2795 _bt_uuid_to_string(&uuid, uuid_string);
2797 /* Fill UUID of Descriptor */
2798 g_strlcpy(info->uuids[count], uuid_string,
2799 BLUETOOTH_UUID_STRING_MAX);
2801 /* Fill instance ID of Descriptor */
2802 info->inst_id[count] = desc_info->inst_id;
2805 /* Increment count of Descriptor browsed */
2809 BT_INFO("Total descriptors browsed [%d]", count);
2813 static void __bt_cleanup_remote_services(struct gatt_server_info_t *conn_info)
2820 bt_gatt_service_info_list_t * svc_info_list = NULL;
2821 bt_gatt_service_info_t *svc = NULL;
2822 bt_gatt_char_info_t *chr = NULL;
2823 bt_gatt_descriptor_info_t *desc = NULL;
2824 bt_gatt_included_service_info_t *incl_svc = NULL;
2826 BT_INFO("Start Cleanup of all services");
2828 svc_info_list = __bt_get_service_info_list(conn_info->connection_id);
2829 if (!svc_info_list) {
2830 BT_INFO("Could not find Svc Info list for the connection ID [%d]",
2831 conn_info->connection_id);
2835 BT_INFO("Num Services [%d]", g_slist_length(svc_info_list->services));
2836 for (l = svc_info_list->services; l;) {
2837 svc = (bt_gatt_service_info_t*)l->data;
2838 l = g_slist_next(l);
2842 BT_INFO("Service info Is Prim[%d] Inst ID [%d]",
2843 svc->is_primary, svc->inst_id);
2844 BT_INFO("Num chars [%d]", g_slist_length(svc->chars));
2845 /* Delete all chars and its descriptors */
2846 for (ll = svc->chars; ll;) {
2847 chr = (bt_gatt_char_info_t*)ll->data;
2848 ll = g_slist_next(ll);
2852 BT_INFO("Num descs [%d]", g_slist_length(chr->descs));
2853 for (lll = chr->descs; lll;) {
2854 desc = (bt_gatt_descriptor_info_t *)lll->data;
2855 lll = g_slist_next(lll);
2858 chr->descs = g_slist_remove(chr->descs, desc);
2861 svc->chars = g_slist_remove(svc->chars, chr);
2865 BT_INFO("Num incl svcs [%d]", g_slist_length(svc->included_svcs));
2866 /* Delete all included services */
2867 for (llll = svc->included_svcs; llll;) {
2868 incl_svc = (bt_gatt_included_service_info_t*)llll->data;
2869 llll = g_slist_next(llll);
2870 if (incl_svc == NULL)
2873 svc->included_svcs = g_slist_remove(svc->included_svcs, incl_svc);
2876 svc_info_list->services = g_slist_remove(svc_info_list->services, svc);
2879 g_free(svc_info_list);
2880 BT_INFO("Cleanup of all services done");
2883 int _bt_register_gatt_client_instance(const char *sender,
2884 bluetooth_device_address_t *address)
2886 int ret = OAL_STATUS_SUCCESS;
2887 char *uuid_string = NULL;
2892 /* App should ensure that it should not send */
2893 BT_INFO("###Check on which instance GATT Client instance can be initialized....");
2894 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
2895 if (numapps[k].is_initialized == 1) {
2896 BT_INFO("Instance ID [%d] is already in use..Check next slot",
2897 numapps[k].instance_id);
2900 BT_INFO("Time to register GATT client instancer..UUID to be used is [%s] slot [%d]",
2901 uuid_list[slot-1], slot);
2907 BT_ERR("No Slot if free for GATT Client registration..");
2908 return BLUETOOTH_ERROR_REGISTRATION_FAILED;
2911 uuid_string = g_malloc0(BT_UUID_STRING_MAX);
2912 _bt_string_to_uuid(uuid_list[slot-1], (service_uuid_t*)&uuid);
2913 g_strlcpy(uuid_string, uuid_list[slot-1], BT_UUID_STRING_MAX);
2914 BT_INFO("Copied UUID string [%s]", uuid_string);
2916 /* Register GATT Client */
2917 ret = gattc_register(&uuid);
2918 if (ret != OAL_STATUS_SUCCESS) {
2919 BT_ERR("ret: %d", ret);
2920 g_free(uuid_string);
2921 return BLUETOOTH_ERROR_INTERNAL;
2924 BT_INFO("GATT Client registration call successfully accepted by OAL..wait for Instance Initialized event from OAL..");
2926 /* Return & wait for GATT Client Instance Initialization event */
2927 memset(numapps[slot].sender, 0x00, sizeof(numapps[slot].sender));
2928 memset(numapps[slot].uuid, 0x00, sizeof(numapps[slot].uuid));
2930 g_strlcpy(numapps[slot].sender, sender, sizeof(numapps[slot].sender));
2931 g_strlcpy(numapps[slot].uuid, uuid_string, sizeof(numapps[slot].uuid));
2933 /* Address is saved here. When event comes, sender + address are matched for replying pending
2934 request. It is impossible for same sender to have requests with two same addresses */
2935 memcpy(&numapps[slot].address.addr, address->addr, sizeof(bluetooth_device_address_t));
2937 BT_INFO("Slot [%d] occupied", slot);
2938 numapps[slot].is_initialized = TRUE; /* Set initialization to true here itself */
2940 g_free(uuid_string);
2941 return BLUETOOTH_ERROR_NONE;
2947 /* GATT client events */
2948 static void __bt_handle_client_instance_registered(event_gattc_register_t *data)
2950 bt_service_app_info_t *info = NULL;
2952 char *uuid_string = g_malloc0(BT_UUID_STRING_MAX);
2954 _bt_uuid_to_string(&(data->client_uuid), uuid_string);
2955 BT_INFO("CLient ID is Initialized [%d] UUID initialized [%s]", data->client_if, uuid_string);
2957 /* Platform GATT client framwork does not use Default GATT client instance
2958 This GATT client instance is never deregistred in the lifetime of bt-service */
2959 BT_INFO("Default UUID [%s] current registered uuid [%s]",
2960 DEFAULT_GATT_CLIENT_UUID, uuid_string);
2961 if (g_strcmp0(uuid_string, DEFAULT_GATT_CLIENT_UUID) == 0) {
2962 BT_INFO("Default client Instance Registered [%s] Client instance [%d]",
2963 uuid_string, data->client_if);
2964 gatt_default_client = data->client_if;
2965 g_free(uuid_string);
2969 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
2972 if (g_strcmp0(info->uuid, uuid_string) == 0) {
2973 BT_INFO("Found GATT client.. UUID [%s], sender [%s]", info->uuid, info->sender);
2974 BT_INFO("Slot [%d] occupied", k);
2975 info->is_initialized = TRUE;
2976 info->client_id = data->client_if;
2977 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE, BT_GATT_CLIENT_REGISTER,
2978 (void*)info, sizeof(bt_service_app_info_t));
2982 g_free(uuid_string);
2985 static void __bt_handle_client_connected(event_gattc_conn_t *event_data)
2987 int result = BLUETOOTH_ERROR_NONE;
2988 struct gatt_server_info_t *conn_info = NULL;
2989 struct gatt_out_conn_info_t *out_conn_info = NULL;
2991 GVariant *param = NULL;
2993 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2994 _bt_convert_addr_type_to_string(address,
2995 (unsigned char *)event_data->address.addr);
2997 if (event_data->status != OAL_STATUS_SUCCESS)
2998 result = BLUETOOTH_ERROR_INTERNAL;
3000 /* DBUS Return fo BT_CONNECT_LE for all the apps */
3001 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE, address,
3002 BT_ADDRESS_STRING_SIZE);
3004 BT_INFO("Local GATT Client Connected: Remote addr[%s] Client Interface [%d] Connection ID [%d] status[%d]",
3005 address, event_data->client_if, event_data->conn_id, event_data->status);
3007 if (result == BLUETOOTH_ERROR_NONE) {
3008 /* Check if device is already in connected list */
3009 conn_info = __bt_find_remote_gatt_server_info(address);
3012 /* Send event to BT-API */
3013 param = g_variant_new("(is)", result, address);
3014 _bt_send_event(BT_DEVICE_EVENT,
3015 BLUETOOTH_EVENT_GATT_CLIENT_CONNECTED, /* Local device is GATT client */
3018 /* Save Connection info */
3019 conn_info = g_new0(struct gatt_server_info_t, 1);
3020 conn_info->addr = g_strdup(address);
3021 conn_info->client_id = event_data->client_if;
3022 BT_INFO("Added GATT server addr[%s]", conn_info->addr);
3023 conn_info->connection_id = event_data->conn_id;
3024 gatt_server_info_list = g_slist_append(gatt_server_info_list, conn_info);
3025 BT_INFO("Total num of connected Remote GATT server devices [%d]",
3026 g_slist_length(gatt_server_info_list));
3029 BT_INFO("Do a Internal refresh");
3030 if (OAL_STATUS_SUCCESS != gattc_refresh(conn_info->client_id, &event_data->address))
3031 BT_ERR("GATT database refresh failed!!");
3033 BT_INFO("GATT database refresh Success!!");
3036 BT_ERR("Local GATT Client connected event for addr[%s], but device is in connected list already", address);
3038 BT_ERR("GATT Client Connection failed!!");
3040 /* If outgoing connection Info is present, then remove it */
3041 out_conn_info = __bt_find_gatt_outgoing_conn_info(address);
3042 if (out_conn_info) {
3043 BT_ERR("Outgoing Client connect request was sent");
3044 outgoing_gatt_conn_list = g_slist_remove(outgoing_gatt_conn_list, out_conn_info);
3045 g_free(out_conn_info->addr);
3046 g_free(out_conn_info);
3051 static void __bt_handle_client_disconnected(event_gattc_conn_t *event_data)
3053 int result = BLUETOOTH_ERROR_NONE;
3055 struct gatt_server_info_t *conn_info = NULL;
3056 struct gatt_out_conn_info_t *out_conn_info = NULL;
3058 GVariant *param = NULL;
3060 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
3061 _bt_convert_addr_type_to_string(address,
3062 (unsigned char *)event_data->address.addr);
3064 if (event_data->status != OAL_STATUS_SUCCESS)
3065 result = BLUETOOTH_ERROR_INTERNAL;
3067 if (NULL == _bt_get_request_info_data(BT_DISCONNECT_LE, address)) {
3068 if (NULL != _bt_get_request_info_data(BT_CONNECT_LE, address)) {
3069 result = BLUETOOTH_ERROR_INTERNAL;
3070 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
3071 address, BT_ADDRESS_STRING_SIZE);
3072 BT_ERR("Failed to connect Local GATT Remote addr[%s]", address);
3077 /* DBUS Return for BT_DISCONNECT_LE for all the apps */
3078 __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE, address,
3079 BT_ADDRESS_STRING_SIZE);
3081 BT_INFO("Local GATT Client DisConnected: Remote addr[%s] Client Interface [%d] Connection ID [%d] status [%d]",
3082 address, event_data->client_if, event_data->conn_id, event_data->status);
3084 /* Remove Connection info */
3085 conn_info = __bt_find_remote_gatt_server_info(address);
3088 param = g_variant_new("(is)", result, address);
3089 /* Send event to application */
3090 _bt_send_event(BT_DEVICE_EVENT,
3091 BLUETOOTH_EVENT_GATT_CLIENT_DISCONNECTED,
3094 BT_INFO("Remove GATT server info from List..");
3095 /* Remove all services from info list_gatt_info */
3096 __bt_cleanup_remote_services(conn_info);
3098 /* Remove info from List */
3099 gatt_server_info_list = g_slist_remove(gatt_server_info_list, conn_info);
3100 BT_INFO("Total num of connected GATT servers [%d]", g_slist_length(gatt_server_info_list));
3101 g_free(conn_info->addr);
3104 BT_INFO("Can not find conn info, already removed!");
3106 /* If outgoing connection Info is present, then remove it */
3107 out_conn_info = __bt_find_gatt_outgoing_conn_info(address);
3108 if (out_conn_info) {
3109 BT_ERR("Client Disconnected event, but outgoing connect request was sent");
3110 outgoing_gatt_conn_list = g_slist_remove(outgoing_gatt_conn_list, out_conn_info);
3111 g_free(out_conn_info->addr);
3112 g_free(out_conn_info);
3118 static void __bt_handle_client_service_search_result(
3119 event_gattc_service_result_t *event_data)
3122 BT_INFO("received the gatt service search result");
3123 /* Pre: status is never fail from OAL */
3125 /* Find service list from address */
3126 bt_gatt_service_info_list_t *svc_info_list;
3127 bt_gatt_service_info_t *svc_info;
3128 BT_INFO("Search Result: status [%d] conn_id [%d]",
3129 event_data->conn_status.status,
3130 event_data->conn_status.conn_id);
3132 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3133 if (!svc_info_list) {
3134 BT_INFO("Service info list not present for this connection ID, means first time browse");
3135 /* Means for this conn_id, no services are ever browsed, first time,
3136 create service info list for this conn_id */
3137 svc_info_list = g_malloc0(sizeof(bt_gatt_service_info_list_t));
3138 svc_info_list->conn_id = event_data->conn_status.conn_id;
3139 list_gatt_info = g_slist_append(list_gatt_info, svc_info_list);
3141 BT_INFO("Service info list Already present for this connection ID, means not first time browse for this conn ID ");
3144 /* send list and current service's uuid and instance id to find it */
3145 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3148 /* If not found, check if service changed, if yes, means this is a new service added
3149 in remote GATT device, update uuid info in svc info list structure, to be used when
3150 search is completed */
3152 BT_INFO("Service Not found in svc info list for this connection ID");
3153 if (svc_info_list->info.is_changed) {
3154 BT_INFO("Service Changed indication already found for this connection ID");
3155 memcpy(svc_info_list->info.uuid, event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3157 /* Create and add new service in service list */
3158 svc_info = g_malloc0(sizeof(bt_gatt_service_info_t));
3159 memcpy(svc_info->uuid, event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3160 svc_info->inst_id = event_data->srvc_id.id.inst_id;
3161 svc_info->is_primary = event_data->srvc_id.is_prmry;
3162 svc_info_list->services = g_slist_append(svc_info_list->services, svc_info);
3163 BT_INFO("Service created and added in Svc info listf or this connection ID");
3165 BT_INFO("Service Already found to be present inside the svc info list for this connection ID");
3166 /* If returned matching service info, then just update service_rmeoved value inside it to 0 */
3167 svc_info->is_removed = 0;
3171 static void __bt_handle_client_service_search_completed(
3172 event_gattc_conn_status_t *event_data)
3175 struct gatt_server_info_t *conn_info = NULL;
3176 bt_gatt_service_info_list_t *svc_info_list;
3177 bt_gatt_service_info_t *svc_info;
3178 bt_services_browse_info_t browse_info;
3179 unsigned char uuid_empty[BLUETOOTH_UUID_HEX_MAX_LEN];
3181 memset(&uuid_empty, 0x00, BLUETOOTH_UUID_HEX_MAX_LEN);
3182 memset(&browse_info, 0x00, sizeof(bt_services_browse_info_t));
3183 BT_INFO("Primary Services browsing completed status[%d] conn ID [%d]",
3184 event_data->status, event_data->conn_id);
3186 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(event_data->conn_id);
3188 svc_info_list = __bt_get_service_info_list(event_data->conn_id);
3189 if (!svc_info_list) {
3190 BT_ERR("No services browsed ever for addr [%s]", conn_info->addr);
3192 /* Just build response and return ERROR */
3193 __bt_build_service_browse_info(event_data->conn_id, &browse_info);
3195 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_INTERNAL,
3196 BT_GATT_GET_PRIMARY_SERVICES, &browse_info,
3197 sizeof(bt_services_browse_info_t));
3201 /* If fail, then send event with error */
3202 if (event_data->status != OAL_STATUS_SUCCESS) {
3203 /* Just build response and return ERROR */
3204 __bt_build_service_browse_info(event_data->conn_id, &browse_info);
3206 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_INTERNAL,
3207 BT_GATT_GET_PRIMARY_SERVICES, &browse_info,
3208 sizeof(bt_services_browse_info_t));
3212 /* If success, then find service info list from address */
3214 /* If svc_changed == 1 and uuid valid, means a new service is added*/
3215 if (svc_info_list->info.is_changed && !memcmp(uuid_empty, svc_info_list->info.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)) {
3216 /* TODO: Send event -Service added with instance ID and UUID of newly added service */
3217 BT_INFO("new service added");
3219 BT_INFO("TODO new service added");
3222 /* If svc_changed == 1 and uuid invalid, then a service is removed */
3223 if (svc_info_list->info.is_changed && memcmp(uuid_empty, svc_info_list->info.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)) {
3224 /* Scan through the service info list to find service with is_removed = 1*/
3225 svc_info = __bt_find_removed_service(svc_info_list);
3227 /* TODO Send event - Service removed with instance ID and UUID of just rmeoved service */
3229 /* Remove that service info from service info list */
3230 svc_info_list->services = g_slist_remove(svc_info_list->services, svc_info);
3232 /* Delete that service completely from svc_info list*/
3233 __bt_remove_service_info_from_list(svc_info);
3237 /* Reset svc_changed = 0, and reset UUID = all 0's */
3238 svc_info_list->info.is_changed = 0;
3239 memset(&svc_info_list->info.uuid, 0x00, BLUETOOTH_UUID_HEX_MAX_LEN);
3241 /* Build Reply and send to service browse primary services request of pending apps */
3242 __bt_build_service_browse_info(event_data->conn_id, &browse_info);
3244 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE,
3245 BT_GATT_GET_PRIMARY_SERVICES, &browse_info,
3246 sizeof(bt_services_browse_info_t));
3250 static void __bt_handle_client_characteristic_search_result(
3251 event_gattc_characteristic_result_t *event_data)
3253 bt_gatt_service_info_list_t *svc_info_list;
3254 bt_gatt_service_info_t *svc_info;
3255 bt_gatt_char_info_t *char_info;
3256 bt_char_browse_info_t browse_info;
3258 BT_INFO("Characteristic search result status [%d]",
3259 event_data->conn_status.status);
3261 memset(&browse_info, 0x00, sizeof(bt_char_browse_info_t));
3264 if (event_data->conn_status.status == OAL_STATUS_SUCCESS) {
3265 /* Find service info list from address */
3266 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3267 if (svc_info_list == NULL) {
3268 BT_ERR("svc_info_list is NULL");
3273 /* Find matching service info from svc info list */
3274 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3275 if (svc_info == NULL) {
3276 BT_ERR("svc_info is NULL");
3280 /* Find Matching char from service info in event */
3281 char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
3282 if (char_info == NULL)
3283 BT_ERR("char_info is NULL");
3285 /* If not found, then add new characteristic and return */
3287 BT_INFO(" add new characteristic");
3288 char_info = g_malloc0(sizeof(bt_gatt_char_info_t));
3289 memcpy(char_info->uuid, event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3291 char_info->inst_id = event_data->char_id.inst_id;
3292 char_info->props = event_data->char_prop;
3293 svc_info->chars = g_slist_append(svc_info->chars, char_info);
3295 /* If found, then return */
3296 BT_INFO("update char property as Characteristic browsed is already present");
3297 char_info->props |= event_data->char_prop;
3300 /* If Not success: Means Charc browse is completed */
3301 /* Find char list from service in event */
3302 /* Find service list from address */
3303 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3305 /* Find service info from service in event */
3306 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3308 /* Build char list from service in event */
3309 __bt_build_char_browse_info(event_data->conn_status.conn_id,
3310 svc_info, &browse_info);
3312 /* Create response and return by sending event*/
3313 /* Build Reply and send to service browse All Included services request of pending apps */
3314 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE,
3315 BT_GATT_GET_SERVICE_PROPERTIES,
3317 sizeof(bt_char_browse_info_t));
3321 static void __bt_handle_client_descriptor_search_result(
3322 event_gattc_descriptor_result_t *event_data)
3324 bt_gatt_service_info_list_t *svc_info_list;
3325 bt_gatt_service_info_t *svc_info;
3326 bt_gatt_char_info_t *char_info;
3327 bt_gatt_descriptor_info_t *desc_info;
3328 bt_descriptor_browse_info_t browse_info;
3330 BT_INFO("descriptor search result status [%d]", event_data->conn_status.status);
3332 memset(&browse_info, 0x00, sizeof(bt_descriptor_browse_info_t));
3335 if (event_data->conn_status.status == OAL_STATUS_SUCCESS) {
3336 /* Find service list from address */
3337 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3338 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3339 char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
3340 desc_info = __bt_find_matching_desc(char_info, &event_data->descr_id);
3342 /* If not found, add new descriptor and return */
3344 desc_info = g_malloc0(sizeof(bt_gatt_descriptor_info_t));
3345 memcpy(desc_info->uuid, event_data->descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3347 desc_info->inst_id = event_data->descr_id.inst_id;
3348 char_info->descs = g_slist_append(char_info->descs, desc_info);
3351 /* If found, then return */
3352 BT_INFO("Characteristic browsed is already presesnt");
3355 /* If Not success */
3356 /* Find service list from address */
3357 /* Find included service list from service in event */
3358 /* Create response and return by sending event*/
3359 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3361 /* Find service info from service in event */
3362 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3364 /* Find char info from char in event */
3365 char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
3367 /* Build descriptor list from char in event */
3368 __bt_build_descriptor_browse_info(event_data->conn_status.conn_id,
3369 svc_info, char_info, &browse_info);
3372 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE,
3373 BT_GATT_GET_CHARACTERISTIC_PROPERTIES,
3375 sizeof(bt_descriptor_browse_info_t));
3379 static void __bt_handle_client_characteristic_read_data(
3380 event_gattc_read_data *event_data)
3382 int result = BLUETOOTH_ERROR_NONE;
3383 struct gatt_server_info_t *conn_info = NULL;
3384 bluetooth_gatt_client_char_prop_info_t read_info;
3386 /* Read Information data structures */
3387 GVariant *param = NULL;
3388 GVariant *data = NULL;
3389 GVariant *data_svc_uuid = NULL;
3390 GVariant *data_char_uuid = NULL;
3391 char *read_val = NULL;
3392 char *svc_uuid = NULL;
3393 char *char_uuid = NULL;
3399 //memset(&read_info, 0x00, sizeof(bt_gatt_handle_property_t));
3400 memset(&read_info, 0x00, sizeof(bluetooth_gatt_client_char_prop_info_t));
3402 /* Extract Address from conn_id of event data */
3403 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3404 event_data->uuid_status.conn_status.conn_id);
3406 BT_INFO("Characteristic Read result from addr [%s] status [%d]",
3407 conn_info->addr, event_data->uuid_status.conn_status.status);
3409 /* Fill char in buffer */
3410 memcpy(&read_info.characteristic.uuid,
3411 event_data->uuid_status.char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3412 read_info.characteristic.instance_id = event_data->uuid_status.char_id.inst_id;
3414 /* Fill Service in buffer */
3415 memcpy(&read_info.svc.uuid,
3416 event_data->uuid_status.srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3417 read_info.svc.instance_id = event_data->uuid_status.srvc_id.id.inst_id;
3419 /* Fill remote device address */
3420 _bt_convert_addr_string_to_type(read_info.device_address.addr, conn_info->addr);
3422 /* Fill data and reply to all apps waiting for Read result on the same characteristic
3423 Note: Even in case of failure, address, handles and result code should be returned */
3424 if (event_data->uuid_status.conn_status.status != OAL_STATUS_SUCCESS)
3425 result = BLUETOOTH_ERROR_INTERNAL;
3427 BT_ERR("read data len is [%d]", event_data->data_len);
3428 if (event_data->data_len > 0) {
3430 for (i = 0; i < event_data->data_len; i++)
3431 BT_INFO("Data[%d] = [0x%x]", i, event_data->data[i]);
3434 read_val = g_memdup(&event_data->data[0], event_data->data_len);
3436 data = g_variant_new_from_data(
3437 G_VARIANT_TYPE_BYTESTRING,
3439 event_data->data_len,
3442 BT_ERR("Characteristic Read success, but no data!!!");
3444 data = g_variant_new_from_data(
3445 G_VARIANT_TYPE_BYTESTRING,
3452 svc_uuid = g_memdup(&event_data->uuid_status.srvc_id.id.uuid.uuid[0], uuid_len);
3454 data_svc_uuid = g_variant_new_from_data(
3455 G_VARIANT_TYPE_BYTESTRING,
3461 char_uuid = g_memdup(&event_data->uuid_status.char_id.uuid.uuid[0], uuid_len);
3463 data_char_uuid = g_variant_new_from_data(
3464 G_VARIANT_TYPE_BYTESTRING,
3469 param = g_variant_new("(isn@ayin@ayin@ay)", result,
3473 event_data->uuid_status.srvc_id.id.inst_id,
3476 event_data->uuid_status.char_id.inst_id,
3477 event_data->data_len,
3481 _bt_send_event(BT_GATT_CLIENT_EVENT,
3482 BLUETOOTH_EVENT_GATT_READ_CHAR,
3485 /* Send DBUS return */
3486 __bt_gatt_handle_pending_request_info(result,
3487 BT_GATT_READ_CHARACTERISTIC,
3489 sizeof(bluetooth_gatt_client_char_prop_info_t));
3500 static void __bt_handle_client_descriptor_read_data(
3501 event_gattc_read_data *event_data)
3503 int result = BLUETOOTH_ERROR_NONE;
3504 struct gatt_server_info_t *conn_info = NULL;
3505 bluetooth_gatt_client_desc_prop_info_t read_info;
3507 /* Read Information data structures */
3508 GVariant *param = NULL;
3509 GVariant *data = NULL;
3510 GVariant *data_svc_uuid = NULL;
3511 GVariant *data_char_uuid = NULL;
3512 GVariant *data_desc_uuid = NULL;
3513 char *read_val = NULL;
3514 char *svc_uuid = NULL;
3515 char *char_uuid = NULL;
3516 char *desc_uuid = NULL;
3521 memset(&read_info, 0x00, sizeof(bluetooth_gatt_client_desc_prop_info_t));
3523 /* Extract Address from conn_id of event data */
3524 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3525 event_data->uuid_status.conn_status.conn_id);
3527 BT_INFO("Descriptor Read result from addr [%s] status [%d]",
3528 conn_info->addr, event_data->uuid_status.conn_status.status);
3530 /* Fill descriptor informations in buffer */
3531 memcpy(&read_info.descriptor.uuid,
3532 event_data->uuid_status.descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3533 read_info.descriptor.instance_id = event_data->uuid_status.descr_id.inst_id;
3535 /* Fill Characteristic informations in buffer */
3536 memcpy(&read_info.characteristic.uuid,
3537 event_data->uuid_status.char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3538 read_info.characteristic.instance_id = event_data->uuid_status.char_id.inst_id;
3540 /* Fill Service informations in buffer */
3541 memcpy(&read_info.svc.uuid,
3542 event_data->uuid_status.srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3543 read_info.svc.instance_id = event_data->uuid_status.srvc_id.id.inst_id;
3545 /* Fill remote device address */
3546 _bt_convert_addr_string_to_type(read_info.device_address.addr, conn_info->addr);
3548 /* Fill data and reply to all apps waiting for Read result on the same characteristic */
3549 if (event_data->uuid_status.conn_status.status != OAL_STATUS_SUCCESS)
3550 result = BLUETOOTH_ERROR_INTERNAL;
3552 BT_INFO("desc data len:", event_data->data_len);
3553 if (event_data->data_len > 0) {
3555 for (i = 0; i < event_data->data_len; i++)
3556 BT_DBG("Data[%d] = [0x%x]", i, event_data->data[i]);
3559 read_val = g_memdup(&event_data->data[0], event_data->data_len);
3561 data = g_variant_new_from_data(
3562 G_VARIANT_TYPE_BYTESTRING,
3564 event_data->data_len,
3567 BT_INFO("Descriptor Read success, but no data!!!");
3569 data = g_variant_new_from_data(
3570 G_VARIANT_TYPE_BYTESTRING,
3576 svc_uuid = g_memdup(&event_data->uuid_status.srvc_id.id.uuid.uuid[0], uuid_len);
3578 data_svc_uuid = g_variant_new_from_data(
3579 G_VARIANT_TYPE_BYTESTRING,
3585 char_uuid = g_memdup(&event_data->uuid_status.char_id.uuid.uuid[0], uuid_len);
3587 data_char_uuid = g_variant_new_from_data(
3588 G_VARIANT_TYPE_BYTESTRING,
3594 desc_uuid = g_memdup(&event_data->uuid_status.descr_id.uuid.uuid[0], uuid_len);
3596 data_desc_uuid = g_variant_new_from_data(
3597 G_VARIANT_TYPE_BYTESTRING,
3602 param = g_variant_new("(isn@ayin@ayin@ayin@ay)", result,
3606 event_data->uuid_status.srvc_id.id.inst_id,
3609 event_data->uuid_status.char_id.inst_id,
3612 event_data->uuid_status.descr_id.inst_id,
3613 event_data->data_len,
3617 _bt_send_event(BT_GATT_CLIENT_EVENT,
3618 BLUETOOTH_EVENT_GATT_READ_DESC,
3621 BT_INFO("Send DBUS rpely for GATT Read Descriptor");
3622 /* Send DBUS return */
3623 __bt_gatt_handle_pending_request_info(result,
3624 BT_GATT_READ_DESCRIPTOR_VALUE,
3626 sizeof(bluetooth_gatt_client_desc_prop_info_t));
3637 static void __bt_handle_client_characteristic_write_data(
3638 event_gattc_write_data *event_data)
3640 int result = BLUETOOTH_ERROR_NONE;
3641 struct gatt_server_info_t *conn_info = NULL;
3642 bluetooth_gatt_client_char_prop_info_t write_info;
3644 /* Read Information data structures */
3645 GVariant *param = NULL;
3646 GVariant *data_svc_uuid = NULL;
3647 GVariant *data_char_uuid = NULL;
3648 char *svc_uuid = NULL;
3649 char *char_uuid = NULL;
3653 memset(&write_info, 0x00, sizeof(bluetooth_gatt_client_char_prop_info_t));
3655 /* Extract Address from conn_id of event data */
3656 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3657 event_data->conn_status.conn_id);
3659 BT_INFO("Characteristic Write callback from addr [%s] status [%d]",
3660 conn_info->addr, event_data->conn_status.status);
3662 /* Fill char in buffer */
3663 memcpy(&write_info.characteristic.uuid,
3664 event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3665 write_info.characteristic.instance_id = event_data->char_id.inst_id;
3667 /* Fill Service in buffer */
3668 memcpy(&write_info.svc.uuid,
3669 event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3670 write_info.svc.instance_id = event_data->srvc_id.id.inst_id;
3672 /* Fill remote device address */
3673 _bt_convert_addr_string_to_type(write_info.device_address.addr, conn_info->addr);
3675 if (event_data->conn_status.status != OAL_STATUS_SUCCESS) {
3676 result = BLUETOOTH_ERROR_INTERNAL;
3682 svc_uuid = g_memdup(&event_data->srvc_id.id.uuid.uuid[0], uuid_len);
3684 data_svc_uuid = g_variant_new_from_data(
3685 G_VARIANT_TYPE_BYTESTRING,
3691 char_uuid = g_memdup(&event_data->char_id.uuid.uuid[0], uuid_len);
3693 data_char_uuid = g_variant_new_from_data(
3694 G_VARIANT_TYPE_BYTESTRING,
3699 param = g_variant_new("(isn@ayin@ayi)", result,
3703 event_data->srvc_id.id.inst_id,
3706 event_data->char_id.inst_id);
3709 _bt_send_event(BT_GATT_CLIENT_EVENT,
3710 BLUETOOTH_EVENT_GATT_WRITE_CHAR,
3719 /* Send DBUS return */
3720 __bt_gatt_handle_pending_request_info(result,
3721 BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE,
3723 sizeof(bluetooth_gatt_client_char_prop_info_t));
3727 static void __bt_handle_client_descriptor_write_data(
3728 event_gattc_write_data *event_data)
3730 int result = BLUETOOTH_ERROR_NONE;
3731 struct gatt_server_info_t *conn_info = NULL;
3732 bluetooth_gatt_client_desc_prop_info_t write_info;
3734 /* Write Information data structures */
3735 GVariant *param = NULL;
3736 GVariant *data_svc_uuid = NULL;
3737 GVariant *data_char_uuid = NULL;
3738 GVariant *data_desc_uuid = NULL;
3739 char *svc_uuid = NULL;
3740 char *char_uuid = NULL;
3741 char *desc_uuid = NULL;
3745 memset(&write_info, 0x00, sizeof(bluetooth_gatt_client_desc_prop_info_t));
3747 /* Extract Address from conn_id of event data */
3748 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3749 event_data->conn_status.conn_id);
3751 if (NULL == conn_info) {
3753 BT_INFO("Failed to get the conn info for conn_id [%d]", event_data->conn_status.conn_id);
3757 BT_INFO("Descriptor Write callback from addr [%s] status [%d]",
3758 conn_info->addr, event_data->conn_status.status);
3760 /* Fill descriptor informations in buffer */
3761 memcpy(&write_info.descriptor.uuid,
3762 event_data->descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3763 write_info.descriptor.instance_id = event_data->descr_id.inst_id;
3765 /* Fill Characteristic informations in buffer */
3766 memcpy(&write_info.characteristic.uuid,
3767 event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3768 write_info.characteristic.instance_id = event_data->char_id.inst_id;
3770 /* Fill Service informations in buffer */
3771 memcpy(&write_info.svc.uuid,
3772 event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3773 write_info.svc.instance_id = event_data->srvc_id.id.inst_id;
3775 /* Fill remote device address */
3776 _bt_convert_addr_string_to_type(write_info.device_address.addr, conn_info->addr);
3778 if (event_data->conn_status.status != OAL_STATUS_SUCCESS) {
3779 result = BLUETOOTH_ERROR_INTERNAL;
3785 svc_uuid = g_memdup(&event_data->srvc_id.id.uuid.uuid[0], uuid_len);
3787 data_svc_uuid = g_variant_new_from_data(
3788 G_VARIANT_TYPE_BYTESTRING,
3794 char_uuid = g_memdup(&event_data->char_id.uuid.uuid[0], uuid_len);
3796 data_char_uuid = g_variant_new_from_data(
3797 G_VARIANT_TYPE_BYTESTRING,
3803 desc_uuid = g_memdup(&event_data->descr_id.uuid.uuid[0], uuid_len);
3805 data_desc_uuid = g_variant_new_from_data(
3806 G_VARIANT_TYPE_BYTESTRING,
3811 param = g_variant_new("(isn@ayin@ayin@ayi)", result,
3815 event_data->srvc_id.id.inst_id,
3818 event_data->char_id.inst_id,
3821 event_data->descr_id.inst_id);
3824 _bt_send_event(BT_GATT_CLIENT_EVENT,
3825 BLUETOOTH_EVENT_GATT_WRITE_DESC,
3836 /* Send DBUS return */
3837 __bt_gatt_handle_pending_request_info(result,
3838 BT_GATT_WRITE_DESCRIPTOR_VALUE,
3840 sizeof(bluetooth_gatt_client_desc_prop_info_t));
3843 static void __bt_hanlde_le_device_disconnection(event_dev_conn_status_t *event_data)
3845 int result = BLUETOOTH_ERROR_INTERNAL;
3846 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
3848 _bt_convert_addr_type_to_string(address, (unsigned char *)event_data->address.addr);
3850 /* DBUS Return with fail of pending BT_CONNECT_LE for all the apps */
3851 BT_INFO("Local GATT Client disconnected: Remote addr[%s] ", address);
3853 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE, address,
3854 BT_ADDRESS_STRING_SIZE);
3858 static void __bt_handle_client_notification_registered(
3859 event_gattc_regdereg_notify_t *event_data,
3860 gboolean is_registered)
3862 int result = BLUETOOTH_ERROR_NONE;
3863 struct gatt_server_info_t *conn_info = NULL;
3864 bt_gatt_notif_reg_info_t notif_info;
3867 memset(¬if_info, 0x00, sizeof(bt_gatt_notif_reg_info_t));
3869 BT_INFO("Client Interface [%d] status [%d]",
3870 event_data->client_if,
3871 event_data->status);
3873 /* Extract Address from conn_id of event data */
3874 conn_info = __bt_find_remote_gatt_server_info_from_client_if(
3875 event_data->client_if);
3878 BT_INFO("Connection Info is not present, return");
3881 BT_INFO("Notification Registered for addr [%s]",
3885 /* Fill svc informations in buffer */
3886 memcpy(¬if_info.svc_uuid,
3887 event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3888 notif_info.svc_inst = event_data->srvc_id.id.inst_id;
3890 /* Fill char in buffer */
3891 memcpy(¬if_info.char_uuid,
3892 event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3893 notif_info.char_inst = event_data->char_id.inst_id;
3895 /* Fill remote device address */
3896 _bt_convert_addr_string_to_type(notif_info.addr.addr, conn_info->addr);
3898 notif_info.is_registered = is_registered;
3900 if (event_data->status != OAL_STATUS_SUCCESS)
3901 result = BLUETOOTH_ERROR_INTERNAL;
3903 /* Send DBUS Return for BT_GATT_WATCH_CHARACTERISTIC */
3904 __bt_gatt_handle_pending_request_info(result,
3905 BT_GATT_WATCH_CHARACTERISTIC,
3907 sizeof(bt_gatt_notif_reg_info_t));
3911 gboolean _bt_is_remote_gatt_device_connected(bluetooth_device_address_t *address)
3914 struct gatt_server_info_t *conn_info = NULL;
3915 gboolean connected = FALSE;
3918 addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
3919 _bt_convert_addr_type_to_string(addr,
3920 (unsigned char *)&(address->addr));
3922 BT_INFO("Check Connected or not for [%s]", addr);
3923 /* Check if device is already in connected list */
3924 conn_info = __bt_find_remote_gatt_server_info(addr);
3927 BT_INFO("Remote GATT Server device [%s] is Connected", conn_info->addr);
3930 BT_INFO("Remote GATT Server Device [%s] is not Connected", addr);
3936 int _bt_connect_le_device(bluetooth_device_address_t *address,
3937 int auto_connect, int client_id)
3939 struct gatt_server_info_t *conn_info = NULL;
3940 struct gatt_out_conn_info_t *out_conn_info = NULL;
3942 invocation_info_t *req_info = NULL;
3943 int ret = OAL_STATUS_SUCCESS;
3945 char *remote_address = NULL;
3947 BT_CHECK_PARAMETER(address, return);
3951 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
3952 _bt_convert_addr_type_to_string(addr, address->addr);
3953 BT_INFO("GATT Client connect request for address [%s] client instance [%d]",
3957 /* Check if Remote Device is already under connection progress */
3958 req_info = _bt_get_request_info_data_from_function_name(BT_CONNECT_LE);
3960 remote_address = (char*)req_info->user_data;
3961 if (remote_address && !strcasecmp(remote_address, addr)) {/* Address matched */
3962 BT_INFO("Already Connection ongoing for same remote GATT Server address [%s]", remote_address);
3963 /* Return and wait for events to be sent to all apps */
3965 return BLUETOOTH_ERROR_IN_PROGRESS;
3969 /* Check if remote GATT Server is connected or not */
3970 conn_info = __bt_find_remote_gatt_server_info(addr);
3972 BT_ERR("GATT Server is already connected..");
3974 return BLUETOOTH_ERROR_ALREADY_CONNECT;
3977 /* TODO Check Requirement of holding Advertisement before initiating LE connect */
3979 /* Check if app sent 0 client id for connection, in such case, use default gatt client ID */
3980 if (client_id == 0) {
3981 BT_INFO("GATT CLient connect request sent by an app without any client instance [%d]",
3983 BT_INFO("Assign default GATT client id [%d]", gatt_default_client);
3984 client_id = gatt_default_client;
3987 BT_INFO("Connect using CLient ID [%d]", client_id);
3988 ret = gattc_connect(client_id, (bt_address_t*)(address), auto_connect);
3990 if (ret != OAL_STATUS_SUCCESS) {
3991 BT_ERR("ret: %d", ret);
3993 return BLUETOOTH_ERROR_INTERNAL;
3996 /* Mark this as outgoing connection */
3997 out_conn_info = g_new0(struct gatt_out_conn_info_t, 1);
3998 out_conn_info->addr = g_strdup(addr);
3999 out_conn_info->client_id = client_id;
4000 BT_INFO("Added outgoing connection info addr[%s]", out_conn_info->addr);
4001 outgoing_gatt_conn_list = g_slist_append(outgoing_gatt_conn_list, out_conn_info);
4004 return BLUETOOTH_ERROR_NONE;
4007 int _bt_gatt_get_primary_services(char *address)
4009 BT_CHECK_PARAMETER(address, return);
4010 struct gatt_server_info_t *conn_info = NULL;
4011 invocation_info_t *req_info = NULL;
4012 int ret = OAL_STATUS_SUCCESS;
4015 /* Check if any app is already browsing primary services on the same remote GATT Server */
4016 req_info = _bt_get_request_info_data(BT_GATT_GET_PRIMARY_SERVICES, address);
4018 BT_INFO("Already Primary Service Browsing ongoing for same rmeote GATT Server");
4019 /* Return and wait for events to be sent to all apps */
4020 return BLUETOOTH_ERROR_NONE;
4023 /* Check if remote GATT Server is connected or not */
4024 conn_info = __bt_find_remote_gatt_server_info(address);
4026 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4027 conn_info->addr, conn_info->connection_id);
4029 BT_ERR("GATT Server is not yet connected..");
4030 return BLUETOOTH_ERROR_NOT_CONNECTED;
4033 /* Send Primary Service Browsing request to stack */
4034 ret = gattc_search_service(conn_info->connection_id, NULL);
4035 if (ret != OAL_STATUS_SUCCESS) {
4036 BT_ERR("ret: %d", ret);
4037 return BLUETOOTH_ERROR_INTERNAL;
4039 return BLUETOOTH_ERROR_NONE;
4042 int _bt_gatt_get_all_characteristic(bluetooth_gatt_client_svc_prop_info_t *svc)
4044 BT_CHECK_PARAMETER(svc, return);
4045 struct gatt_server_info_t *conn_info = NULL;
4046 invocation_info_t *req_info = NULL;
4047 bluetooth_gatt_client_svc_prop_info_t *prop;
4048 oal_gatt_srvc_id_t srvc_id;
4049 int ret = OAL_STATUS_SUCCESS;
4053 /* Check if any app is already browsing characteristics of the same service on the same remote GATT Server */
4054 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_GET_SERVICE_PROPERTIES);
4056 prop = (bluetooth_gatt_client_svc_prop_info_t*)req_info->user_data;
4057 if (prop && !memcmp(svc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t))
4058 && memcmp(prop->svc.uuid, svc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
4059 && prop->svc.instance_id == svc->svc.instance_id) {
4060 BT_INFO("Already Properties browsing for Primary Service ongoing for same remote GATT Server");
4061 /* Return and wait for events to be sent to all apps */
4062 return BLUETOOTH_ERROR_NONE;
4066 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4067 _bt_convert_addr_type_to_string(addr, svc->device_address.addr);
4069 /* Check if remote GATT Server is connected or not */
4070 conn_info = __bt_find_remote_gatt_server_info(addr);
4072 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4073 conn_info->addr, conn_info->connection_id);
4075 BT_ERR("GATT Server is not yet connected..");
4077 return BLUETOOTH_ERROR_NOT_CONNECTED;
4080 srvc_id.is_prmry = TRUE;
4081 srvc_id.id.inst_id = svc->svc.instance_id;
4082 memcpy(srvc_id.id.uuid.uuid, svc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4084 /* Search All Characteristic */
4085 ret = gattc_get_characteristic(conn_info->connection_id, &srvc_id, NULL);
4086 if (ret != OAL_STATUS_SUCCESS) {
4087 BT_ERR("ret: %d", ret);
4089 return BLUETOOTH_ERROR_INTERNAL;
4092 return BLUETOOTH_ERROR_NONE;
4095 int _bt_gatt_get_all_characteristic_properties(
4096 bluetooth_gatt_client_char_prop_info_t *chr)
4098 struct gatt_server_info_t *conn_info = NULL;
4099 invocation_info_t *req_info = NULL;
4100 bluetooth_gatt_client_char_prop_info_t *prop;
4101 oal_gatt_srvc_id_t srvc_id;
4102 oal_gatt_id_t char_id;
4103 int ret = OAL_STATUS_SUCCESS;
4106 BT_CHECK_PARAMETER(chr, return);
4110 /* Check if any app is already browsing descriptors of the same char of
4111 particular service on the same remote GATT Server */
4112 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_GET_CHARACTERISTIC_PROPERTIES);
4114 prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
4115 if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4116 && memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4117 && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4118 && memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4119 && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
4120 BT_INFO("Already Properties browsing for Characteristic ongoing for same remote GATT Server");
4121 /* Return and wait for events to be sent to all apps */
4122 return BLUETOOTH_ERROR_NONE;
4126 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4127 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4129 /* Check if remote GATT Server is connected or not */
4130 conn_info = __bt_find_remote_gatt_server_info(addr);
4132 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4133 conn_info->addr, conn_info->connection_id);
4135 BT_ERR("GATT Server is not yet connected..");
4137 return BLUETOOTH_ERROR_NOT_CONNECTED;
4140 srvc_id.is_prmry = TRUE;
4141 srvc_id.id.inst_id = chr->svc.instance_id;
4142 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4144 char_id.inst_id = chr->characteristic.instance_id;
4145 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4147 /* Search All Descriptors */
4148 ret = gattc_get_descriptor(conn_info->connection_id, &srvc_id, &char_id, NULL);
4149 if (ret != OAL_STATUS_SUCCESS) {
4150 BT_ERR("ret: %d", ret);
4152 return BLUETOOTH_ERROR_INTERNAL;
4155 return BLUETOOTH_ERROR_NONE;
4158 int _bt_gatt_read_characteristic_value(
4159 bluetooth_gatt_client_char_prop_info_t *chr)
4161 struct gatt_server_info_t *conn_info = NULL;
4162 invocation_info_t *req_info = NULL;
4163 bluetooth_gatt_client_char_prop_info_t *prop;
4164 oal_gatt_srvc_id_t srvc_id;
4165 oal_gatt_id_t char_id;
4166 int ret = OAL_STATUS_SUCCESS;
4169 BT_CHECK_PARAMETER(chr, return);
4173 /* Check if any app is already Reading characteristic of the same char of
4174 particular service on the same remote GATT Server */
4175 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_READ_CHARACTERISTIC);
4177 prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
4178 if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4179 && memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4180 && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4181 && memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4182 && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
4183 BT_INFO("Already Characteristic value Read operation in progress for same remote GATT Server");
4184 /* Return and wait for events to be sent to all apps */
4185 return BLUETOOTH_ERROR_NONE;
4189 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4190 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4192 /* Check if remote GATT Server is connected or not */
4193 conn_info = __bt_find_remote_gatt_server_info(addr);
4195 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4196 conn_info->addr, conn_info->connection_id);
4198 BT_ERR("GATT Server is not yet connected..");
4200 return BLUETOOTH_ERROR_NOT_CONNECTED;
4203 srvc_id.is_prmry = TRUE;
4204 srvc_id.id.inst_id = chr->svc.instance_id;
4205 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4207 char_id.inst_id = chr->characteristic.instance_id;
4208 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4210 /* Search All Descriptors */
4211 ret = gattc_read_characteristic(conn_info->connection_id, &srvc_id, &char_id, OAL_GATT_AUTH_REQ_NONE);
4212 if (ret != OAL_STATUS_SUCCESS) {
4213 BT_ERR("ret: %d", ret);
4215 return BLUETOOTH_ERROR_INTERNAL;
4218 return BLUETOOTH_ERROR_NONE;
4221 int _bt_gatt_read_descriptor_value(
4222 bluetooth_gatt_client_desc_prop_info_t *desc)
4224 struct gatt_server_info_t *conn_info = NULL;
4225 invocation_info_t *req_info = NULL;
4226 bluetooth_gatt_client_desc_prop_info_t *prop;
4227 oal_gatt_srvc_id_t srvc_id;
4228 oal_gatt_id_t char_id;
4229 oal_gatt_id_t desc_id;
4230 int ret = OAL_STATUS_SUCCESS;
4233 BT_CHECK_PARAMETER(desc, return);
4237 /* Check if any app is already Reading descriptors of the same char of
4238 particular service on the same remote GATT Server */
4239 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_READ_DESCRIPTOR_VALUE);
4241 prop = (bluetooth_gatt_client_desc_prop_info_t*)req_info->user_data;
4242 if (prop && !memcmp(desc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4243 && memcmp(desc->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4244 && desc->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4245 && memcmp(desc->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4246 && desc->characteristic.instance_id == prop->characteristic.instance_id /* Characteristic Instance ID matched */
4247 && memcmp(desc->descriptor.uuid, prop->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Descriptor UUID matched */
4248 && desc->descriptor.instance_id == prop->descriptor.instance_id) { /* Descriptor Instance ID matched */
4249 BT_INFO("Already Descriptor value Read operation in progress for same remote GATT Server");
4250 /* Return and wait for events to be sent to all apps */
4251 return BLUETOOTH_ERROR_NONE;
4255 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4256 _bt_convert_addr_type_to_string(addr, desc->device_address.addr);
4258 /* Check if remote GATT Server is connected or not */
4259 conn_info = __bt_find_remote_gatt_server_info(addr);
4261 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4262 conn_info->addr, conn_info->connection_id);
4264 BT_ERR("GATT Server is not yet connected..");
4266 return BLUETOOTH_ERROR_NOT_CONNECTED;
4269 srvc_id.is_prmry = TRUE;
4270 srvc_id.id.inst_id = desc->svc.instance_id;
4271 memcpy(srvc_id.id.uuid.uuid, desc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4273 char_id.inst_id = desc->characteristic.instance_id;
4274 memcpy(char_id.uuid.uuid, desc->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4276 desc_id.inst_id = desc->descriptor.instance_id;
4277 memcpy(desc_id.uuid.uuid, desc->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4279 /* Search All Descriptors */
4280 ret = gattc_read_descriptor(conn_info->connection_id, &srvc_id, &char_id,
4281 &desc_id, OAL_GATT_AUTH_REQ_NONE);
4282 if (ret != OAL_STATUS_SUCCESS) {
4283 BT_ERR("ret: %d", ret);
4285 return BLUETOOTH_ERROR_INTERNAL;
4288 return BLUETOOTH_ERROR_NONE;
4292 /* Write Characteristic */
4293 int _bt_gatt_write_characteristic_value_by_type(
4294 bluetooth_gatt_client_char_prop_info_t *chr,
4295 bluetooth_gatt_att_data_t *data,
4296 bluetooth_gatt_write_type_e write_type)
4298 struct gatt_server_info_t *conn_info = NULL;
4299 invocation_info_t *req_info = NULL;
4300 bluetooth_gatt_client_char_prop_info_t *prop;
4301 oal_gatt_srvc_id_t srvc_id;
4302 oal_gatt_id_t char_id;
4303 int ret = OAL_STATUS_SUCCESS;
4307 BT_CHECK_PARAMETER(chr, return);
4308 BT_CHECK_PARAMETER(data, return);
4312 /* Check if any app is already writing same char of
4313 particular service on the same remote GATT Server */
4314 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE);
4316 prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
4317 if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4318 && memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4319 && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4320 && memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4321 && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
4322 BT_INFO("Already Characteristic Write Value operation in progress for same remote GATT Server");
4323 /* Return and wait for events to be sent to all apps */
4324 return BLUETOOTH_ERROR_NONE;
4328 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4329 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4331 /* Check if remote GATT Server is connected or not */
4332 conn_info = __bt_find_remote_gatt_server_info(addr);
4334 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4335 conn_info->addr, conn_info->connection_id);
4337 BT_ERR("GATT Server is not yet connected..");
4339 return BLUETOOTH_ERROR_NOT_CONNECTED;
4342 srvc_id.is_prmry = TRUE;
4343 srvc_id.id.inst_id = chr->svc.instance_id;
4344 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4346 char_id.inst_id = chr->characteristic.instance_id;
4347 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4349 /* Write CHar value */
4351 BT_INFO("Connection ID [%d] write type [%d] data length [%d]", conn_info->connection_id, write_type, data->length);
4352 for (k = 0; k < data->length; k++)
4353 BT_INFO("Data[%d] [0x%x]", k, data->data[k]);
4355 ret = gattc_write_characteristic(conn_info->connection_id,
4357 (oal_gatt_write_type_t)write_type, data->length,
4358 OAL_GATT_AUTH_REQ_NONE, (char *)(&data->data[0]));
4359 if (ret != OAL_STATUS_SUCCESS) {
4360 BT_ERR("ret: %d", ret);
4362 return BLUETOOTH_ERROR_INTERNAL;
4365 return BLUETOOTH_ERROR_NONE;
4368 /* Write Descriptor */
4369 int _bt_gatt_write_descriptor_value_by_type(
4370 bluetooth_gatt_client_desc_prop_info_t *desc,
4371 bluetooth_gatt_att_data_t *data,
4372 bluetooth_gatt_write_type_e write_type)
4374 struct gatt_server_info_t *conn_info = NULL;
4375 invocation_info_t *req_info = NULL;
4376 bluetooth_gatt_client_desc_prop_info_t *prop;
4377 oal_gatt_srvc_id_t srvc_id;
4378 oal_gatt_id_t char_id;
4379 oal_gatt_id_t desc_id;
4380 int ret = OAL_STATUS_SUCCESS;
4384 BT_CHECK_PARAMETER(desc, return);
4385 BT_CHECK_PARAMETER(data, return);
4389 /* Check if any app is already writing on same Descriptor of the same char of
4390 particular service on the same remote GATT Server */
4391 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_WRITE_DESCRIPTOR_VALUE);
4393 prop = (bluetooth_gatt_client_desc_prop_info_t*)req_info->user_data;
4394 if (prop && !memcmp(desc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4395 && memcmp(desc->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4396 && desc->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4397 && memcmp(desc->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4398 && desc->characteristic.instance_id == prop->characteristic.instance_id /* Characteristic Instance ID matched */
4399 && memcmp(desc->descriptor.uuid, prop->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Descriptor UUID matched */
4400 && desc->descriptor.instance_id == prop->descriptor.instance_id) { /* Descriptor Instance ID matched */
4401 BT_INFO("Already Descriptor value Write operation in progress for same remote GATT Server");
4402 /* Return and wait for events to be sent to all apps */
4403 return BLUETOOTH_ERROR_NONE;
4407 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4408 _bt_convert_addr_type_to_string(addr, desc->device_address.addr);
4410 /* Check if remote GATT Server is connected or not */
4411 conn_info = __bt_find_remote_gatt_server_info(addr);
4413 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4414 conn_info->addr, conn_info->connection_id);
4416 BT_ERR("GATT Server is not yet connected..");
4418 return BLUETOOTH_ERROR_NOT_CONNECTED;
4421 srvc_id.is_prmry = TRUE;
4422 srvc_id.id.inst_id = desc->svc.instance_id;
4423 memcpy(srvc_id.id.uuid.uuid, desc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4425 char_id.inst_id = desc->characteristic.instance_id;
4426 memcpy(char_id.uuid.uuid, desc->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4428 desc_id.inst_id = desc->descriptor.instance_id;
4429 memcpy(desc_id.uuid.uuid, desc->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4432 BT_INFO("Connection ID [%d] write type [%d] data length [%d]", conn_info->connection_id, write_type, data->length);
4433 for (k = 0; k < data->length; k++)
4434 BT_INFO("Data[%d] [0x%x]", k, data->data[k]);
4436 ret = gattc_write_descriptor(conn_info->connection_id,
4437 &srvc_id, &char_id, &desc_id,
4438 (oal_gatt_write_type_t)write_type, data->length,
4439 OAL_GATT_AUTH_REQ_NONE, (char *)(&data->data[0]));
4440 if (ret != OAL_STATUS_SUCCESS) {
4441 BT_ERR("ret: %d", ret);
4443 return BLUETOOTH_ERROR_INTERNAL;
4446 return BLUETOOTH_ERROR_NONE;
4449 int _bt_gatt_watch_characteristic(
4450 bluetooth_gatt_client_char_prop_info_t *chr,
4454 struct gatt_server_info_t *conn_info = NULL;
4455 oal_gatt_srvc_id_t srvc_id;
4456 oal_gatt_id_t char_id;
4457 int ret = OAL_STATUS_SUCCESS;
4460 BT_CHECK_PARAMETER(chr, return);
4462 BT_INFO("Client ID [%d] Is Notify [%d]", client_id, is_notify);
4464 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4465 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4467 /* Check if remote GATT Server is connected or not */
4468 conn_info = __bt_find_remote_gatt_server_info(addr);
4470 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4471 conn_info->addr, conn_info->connection_id);
4473 BT_ERR("GATT Server is not yet connected..");
4475 return BLUETOOTH_ERROR_NOT_CONNECTED;
4477 srvc_id.is_prmry = TRUE;
4478 srvc_id.id.inst_id = chr->svc.instance_id;
4479 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4481 char_id.inst_id = chr->characteristic.instance_id;
4482 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4484 /* Register or unregister Notification characteristic */
4486 ret = gattc_register_for_notification(conn_info->client_id,
4487 (bt_address_t*)&(chr->device_address),
4488 &srvc_id, &char_id);
4490 ret = gattc_deregister_for_notification(conn_info->client_id,
4491 (bt_address_t*)&(chr->device_address),
4492 &srvc_id, &char_id);
4494 BT_INFO("Result[%d]", ret);
4495 if (ret != OAL_STATUS_SUCCESS) {
4496 BT_ERR("ret: %d", ret);
4498 return BLUETOOTH_ERROR_INTERNAL;
4501 return BLUETOOTH_ERROR_NONE;
4505 int _bt_disconnect_le_device(bluetooth_device_address_t *address,
4508 struct gatt_server_info_t *conn_info = NULL;
4509 invocation_info_t *req_info = NULL;
4510 int ret = OAL_STATUS_SUCCESS;
4512 char *remote_address = NULL;
4514 BT_CHECK_PARAMETER(address, return);
4518 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4519 _bt_convert_addr_type_to_string(addr, address->addr);
4520 BT_INFO("GATT Client Disconnect request for address [%s]", addr);
4522 /* Check if Remote Device is already under connection progress */
4523 req_info = _bt_get_request_info_data_from_function_name(BT_DISCONNECT_LE);
4525 remote_address = (char*)req_info->user_data;
4526 if (remote_address && !strcasecmp(remote_address, addr)) {/* Address matched */
4527 BT_INFO("Already DisConnection ongoing for same remote GATT Server address [%s]", remote_address);
4528 /* Return success and wait for events to be sent to all apps */
4530 return BLUETOOTH_ERROR_IN_PROGRESS;
4533 /* Check if remote GATT Server is connected or not */
4534 conn_info = __bt_find_remote_gatt_server_info(addr);
4536 BT_ERR("GATT Server is not connected..");
4538 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
4541 /* Check if app sent 0 client id for Disconnection, in such case, use default gatt client ID */
4542 if (client_id == 0) {
4543 BT_INFO("GATT CLient Disconnect request sent by an app without any client instance [%d]",
4545 BT_INFO("Assign default GATT client id [%d]", gatt_default_client);
4546 client_id = gatt_default_client;
4549 BT_INFO("Disconnect using CLient ID [%d] Connection ID [%d]", client_id, conn_info->connection_id);
4550 ret = gattc_disconnect(client_id, (bt_address_t*)(address),
4551 conn_info->connection_id);
4553 if (ret != OAL_STATUS_SUCCESS) {
4554 BT_ERR("ret: %d", ret);
4556 return BLUETOOTH_ERROR_INTERNAL;
4559 return BLUETOOTH_ERROR_NONE;
4562 int _bt_gatt_watch_service_changed_indication(const char *sender,
4563 bluetooth_device_address_t *address,
4564 gboolean is_enabled)
4567 bt_service_app_info_t *info = NULL;
4569 BT_INFO("Enable Servic changed Indication watcher [%d] for app [%s]",
4570 is_enabled, sender);
4572 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
4575 if (g_strcmp0(sender, info->sender) == 0 &&
4576 memcmp(info->address.addr, address->addr,
4577 sizeof(bluetooth_device_address_t)) == 0) {
4578 BT_INFO("Found GATT client App.. UUID [%s], sender [%s]", info->uuid, info->sender);
4579 info->is_watcher_enabled = is_enabled;
4583 return BLUETOOTH_ERROR_NONE;
4586 int _bt_unregister_gatt_client_instance(const char *sender, int client_id)
4588 BT_INFO("Unregister Allocated GATT CLient instance [%s] Client ID [%d]",
4590 int result = BLUETOOTH_ERROR_NONE;
4593 /* Unregister CLient instance associated with address X. It is possible that another app still
4594 has client_id valid for same remote address */
4595 bt_service_app_info_t *info = NULL;
4597 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
4600 /* Exact matching of sender */
4601 if (!g_strcmp0(info->sender, sender) && info->client_id == client_id) { /* Check for only valid GATT client Instance */
4602 BT_INFO("Unregister GATT client instance [%d]", info->client_id);
4603 result = __bt_do_unregister_gatt_instance(info->client_id);
4604 if (result != BLUETOOTH_ERROR_NONE)
4605 BT_ERR("Error in unregistering GATT Client Interface");