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);
258 static void __bt_handle_client_notification_data(event_gattc_notify_data *event_data);
262 static int __bt_do_unregister_server_instance(int server_instance);
264 /* Linked List of GATT requests from Remote GATT Clients */
265 static GSList *gatt_server_requests = NULL;
267 /* Linked List of connected Remote GATT clients */
268 static GSList *gatt_client_info_list = NULL;
270 /* Number of clients to be notified to */
271 static int num_indicate_clients;
273 /* List of applications */
274 static bt_service_app_info_t numapps[MAX_APPS_SUPPORTED];
276 static void __bt_gatt_handle_pending_request_info(int result,
277 int service_function, void *data, unsigned int size);
279 static void __bt_handle_server_instance_registered(event_gatts_register_t *data);
281 static void __bt_gatt_event_handler(int event_type, gpointer event_data);
283 void _bt_check_adv_app_termination(const char *name)
285 bt_service_app_info_t *app = NULL;
287 int apps[MAX_APPS_SUPPORTED];
289 ret_if(NULL == name);
291 memset(&apps, -1, sizeof(apps));
293 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
296 /* Search for a app which has same sender and stop adv is running */
297 if (!strncasecmp(app->sender, name, strlen(name)) && app->is_initialized == TRUE) {
298 BT_DBG("Match found, name: %s", name);
300 /* Unregister all service handles with stack */
301 __bt_remove_all_service_handles(app->instance_id);
303 /* If Advertising is enabled, stop it */
304 if (app->adv_handle > 0) {
305 BT_INFO("Stop advertising on instance ID [%d]", app->instance_id);
306 /* Disable adv if running */
307 BT_INFO("Disable Advertising Adv Handle [%d] sender [%s]",
308 app->adv_handle, name);
309 _bt_set_advertising(app->sender, app->adv_handle, FALSE, FALSE);
312 /* Save instances of all apps that need to be unregistered */
313 apps[app->instance_id] = 1;
317 /* Unregister all apps one by one */
318 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
320 BT_INFO("Unregister app[%d]", k);
321 /* Unregister server instance */
322 __bt_do_unregister_server_instance(k);
327 static int __bt_gatt_send_indication_to_all_connected_clients(bluetooth_gatt_att_data_t *data,
328 bluetooth_gatt_server_indication_params_t *param)
331 int ret = OAL_STATUS_SUCCESS;
332 int result = OAL_STATUS_INTERNAL_ERROR;
334 BT_INFO("Current total number of connected clients [%d]", g_slist_length(gatt_client_info_list));
335 for (l = gatt_client_info_list; l != NULL; l = l->next) {
336 struct gatt_client_info_t *info = l->data;
339 BT_INFO("GATT Remote client address [%s] connection Id [%d]", info->addr, info->connection_id);
341 ret = gatts_send_indication(param->instance_id, param->atrribute_handle,
342 info->connection_id, data->length,
343 param->need_confirmation, (char *)(&data->data[0]));
345 BT_INFO("Send Indication to GATT client [%s] result: [%d]", info->addr, ret);
346 if (ret == OAL_STATUS_SUCCESS) {
347 BT_INFO("Send Indication sent successfully to GATT client [%s]", info->addr);
349 num_indicate_clients++;
353 BT_INFO("Indication sending done for total number of clients [%d]", num_indicate_clients);
357 static struct gatt_server_req_info *__bt_gatt_server_find_request_info(int request_id,
358 bluetooth_gatt_att_request_type_e req_type)
362 for (l = gatt_server_requests; l != NULL; l = l->next) {
363 struct gatt_server_req_info *req_info = l->data;
365 if (req_info && req_info->request_id == request_id && req_info->request_type == req_type) {
366 BT_INFO("GATT Server request info found Req ID [%d] handle [%d] conn ID [%d]",
367 req_info->request_id, req_info->attribute_handle, req_info->connection_id);
371 BT_ERR("Gatt Request not found");
375 void _bt_get_adv_handle_from_instance(int server_inst, int *adv_handle)
379 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
380 if (numapps[k].is_initialized == 1 && numapps[k].instance_id == server_inst) {
381 *adv_handle = numapps[k].adv_handle;
387 #ifdef TIZEN_GATT_CLIENT
388 char * _bt_gatt_get_default_gatt_client_uuid(void)
390 return g_strdup(DEFAULT_GATT_CLIENT_UUID);
394 static void __bt_register_default_gatt_client()
399 uuid_str = _bt_gatt_get_default_gatt_client_uuid();
400 _bt_string_to_uuid(uuid_str, (service_uuid_t*)&uuid);
402 BT_INFO("Register Default GATT client uuid [%s]", uuid_str);
404 if (OAL_STATUS_SUCCESS != gattc_register(&uuid)) /* for only Smart Control */
405 BT_ERR("gattc register failed");
411 int _bt_gatt_init(void)
415 if (OAL_STATUS_SUCCESS != gatt_enable()) {
416 BT_ERR("gatt Init failed");
417 return BLUETOOTH_ERROR_INTERNAL;
420 /* Register gatt event handler */
421 _bt_service_register_event_handler_callback(BT_GATT_MODULE, __bt_gatt_event_handler);
422 #ifdef TIZEN_GATT_CLIENT
423 /*In bluedroid product, defacult gatt client is handled differently*/
424 __bt_register_default_gatt_client();
427 return BLUETOOTH_ERROR_NONE;
430 static void __bt_service_reset_gatt_data()
434 BT_INFO("Rest numapp");
437 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
438 numapps[k].is_initialized = 0;
439 numapps[k].instance_id = -1;
440 numapps[k].adv_handle = 0;
441 numapps[k].adv_instance = -1;
442 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
443 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
444 memset(numapps[k].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
445 memset(numapps[k].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
446 numapps[k].adv_data_len = 0;
447 numapps[k].scan_rsp_len = 0;
451 void _bt_gatt_deinit(void)
453 BT_INFO("GATT deinit");
455 if (OAL_STATUS_SUCCESS != gatt_disable())
456 BT_ERR("gatt deinit failed");
458 /* Un-register gatt event handler */
459 _bt_service_unregister_event_handler_callback(BT_GATT_MODULE);
461 __bt_service_reset_gatt_data();
464 void _bt_update_adv_handle(const char *sender, int adv_handle)
467 BT_DBG("Sender [%s] Adv handle [%d]", sender, adv_handle);
468 bt_service_app_info_t *info = NULL;
470 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
472 /* Search for a app which has same sender and adv handle as 0 */
473 if (!g_strcmp0(info->sender, sender) && info->adv_handle == 0)
474 info->adv_handle = adv_handle;
478 int _bt_is_sender_gatt_server_with_no_adv(const char *sender, int adv_handle)
481 BT_DBG("Sender [%s] Adv handle [%d]", sender, adv_handle);
482 bt_service_app_info_t *info = NULL;
484 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
486 /* Search for a app which has same sender and adv handle as 0
487 It is possible that same sender but different adv handle */
488 if (!g_strcmp0(info->sender, sender) && info->adv_handle == 0) {
489 //info->adv_handle = adv_handle;
490 return info->instance_id;
496 int _bt_get_allocated_server_instance(const char *sender, int adv_handle, gboolean use_reserved_slot)
499 BT_DBG("Sender [%s] Adv handle [%d]", sender, adv_handle);
500 bt_service_app_info_t *info = NULL;
502 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
505 /* Exact matching of Adv handle + sender combination */
506 if (!g_strcmp0(info->sender, sender) && info->adv_handle == adv_handle)
507 return info->instance_id;
509 if (!g_strcmp0(info->sender, sender) && info->adv_handle == -1)
510 return info->instance_id;
516 char * _bt_get_sender_and_handle(int server_instance, int *adv_handle)
519 bt_service_app_info_t *info = NULL;
521 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
524 if (info->instance_id == server_instance && info->adv_handle != -1) {
525 *adv_handle = info->adv_handle;
526 BT_DBG("Server instance [%d] Adv handle [%d] Sender [%s]", server_instance, *adv_handle, info->sender);
527 return g_strdup(info->sender);
533 void _bt_set_new_adv_data(bluetooth_advertising_data_t *adv, int len, int instance)
537 bt_service_app_info_t *info = NULL;
538 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
541 if (info->instance_id == instance) {
542 memcpy(info->adv_data.data, &adv->data, len);
549 void _bt_set_new_scan_rsp_data(bluetooth_scan_resp_data_t *scan, int len, int instance)
553 bt_service_app_info_t *info = NULL;
554 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
557 if (info->instance_id == instance) {
558 memcpy(info->scan_rsp.data, &scan->data, len);
565 void _bt_get_previous_adv_data(bluetooth_advertising_data_t *adv, int *len, int instance)
569 bt_service_app_info_t *info = NULL;
571 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
574 if (info->instance_id == instance) {
575 memcpy(&adv->data, info->adv_data.data, info->adv_data_len);
576 *len = info->adv_data_len;
582 void _bt_get_previous_scan_rsp_data(bluetooth_scan_resp_data_t *scan, int *len, int instance)
586 bt_service_app_info_t *info = NULL;
588 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
591 if (info->instance_id == instance) {
592 memcpy(&scan->data, info->scan_rsp.data, info->scan_rsp_len);
593 *len = info->scan_rsp_len;
599 #ifdef TIZEN_GATT_CLIENT
600 static int __bt_do_unregister_gatt_instance(int instance_id)
602 int ret = OAL_STATUS_SUCCESS;
605 BT_INFO("DeAllocate server or client instance ID [%d]", instance_id);
607 /* Reset data: instance_id parameter could be either for GATT Server or for GATT client */
608 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
609 if (numapps[k].instance_id == instance_id) {
610 BT_INFO("This is a GATT server app, unregister: Slot [%d] vacant", k);
611 numapps[k].is_initialized = FALSE;
612 numapps[k].instance_id = -1;
613 numapps[k].adv_handle = 0;
614 numapps[k].adv_instance = -1;
615 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
616 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
617 memset(numapps[k].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
618 memset(numapps[k].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
619 numapps[k].adv_data_len = 0;
620 numapps[k].scan_rsp_len = 0;
622 /* Its a GATT Server Instance */
623 ret = gatts_unregister(instance_id);
624 if (ret != OAL_STATUS_SUCCESS) {
625 BT_ERR("DeAllocate server instance with stack Fail ret: %d", ret);
626 return BLUETOOTH_ERROR_INTERNAL;
629 } else if (numapps[k].client_id == instance_id) {
630 BT_INFO("This is a GATT client app, unregister: Slot [%d] vacant", k);
631 numapps[k].client_id = -1;
632 numapps[k].is_initialized = FALSE;
633 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
634 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
635 memset(&numapps[k].address.addr, 0x00, sizeof(bluetooth_device_address_t));
637 /* Its a GATT Client Instance */
638 ret = gattc_deregister(instance_id);
639 if (ret != OAL_STATUS_SUCCESS) {
640 BT_ERR("DeAllocate GATT Client instance with stack Fail ret: %d", ret);
641 return BLUETOOTH_ERROR_INTERNAL;
646 return BLUETOOTH_ERROR_NONE;
650 static int __bt_do_unregister_server_instance(int server_instance)
652 int ret = OAL_STATUS_SUCCESS;
655 /* Unregister the server instance */
656 ret = gatts_unregister(server_instance);
657 if (ret != OAL_STATUS_SUCCESS) {
658 BT_ERR("DeAllocate server instance with stack Fail ret: %d", ret);
659 return BLUETOOTH_ERROR_INTERNAL;
661 BT_INFO("DeAllocated server instance with stack successful..");
664 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
665 if (numapps[k].instance_id == server_instance) {
666 numapps[k].is_initialized = 0;
667 numapps[k].instance_id = -1;
668 numapps[k].adv_handle = 0;
669 numapps[k].adv_instance = -1;
670 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
671 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
672 memset(numapps[k].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
673 memset(numapps[k].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
674 numapps[k].adv_data_len = 0;
675 numapps[k].scan_rsp_len = 0;
679 BT_DBG("Going8 to reset numapp block num [%d]", k);
680 return BLUETOOTH_ERROR_NONE;
683 static void __bt_remove_all_service_handles(int instance_id)
687 int ret = OAL_STATUS_SUCCESS;
690 count = g_slist_length(numapps[instance_id].service_handles);
691 BT_INFO("Before handle removal: current count [%d]", count);
693 for (l = numapps[instance_id].service_handles; l != NULL;) {
698 BT_INFO("Server Handle to be Removed [%d] Instance ID [%d]", *handle, instance_id);
699 if (_bt_gatt_server_stop_service(numapps[instance_id].sender, *handle, instance_id) != BLUETOOTH_ERROR_NONE)
702 ret = gatts_delete_service(instance_id, *handle);
703 if (ret != OAL_STATUS_SUCCESS) {
704 BT_ERR("ret: %d", ret);
707 numapps[instance_id].service_handles = g_slist_remove(numapps[instance_id].service_handles, handle);
710 count = g_slist_length(numapps[instance_id].service_handles);
711 BT_INFO("After deleting current count [%d]", count);
717 int _bt_unregister_server_instance(const char *sender, int adv_handle)
719 BT_INFO("Unregister Allocated server instance request Sender [%s] Adv handle [%d]", sender, adv_handle);
720 int result = BLUETOOTH_ERROR_NONE;
721 int apps[MAX_APPS_SUPPORTED];
725 memset(&apps, -1, sizeof(apps));
727 if (adv_handle == 0) {
728 BT_DBG("Its a direct GATT Server app request to unregister");
729 /* Unregister server instance for each app with same sender (case: GATT Server with multiple adv handle) */
730 bt_service_app_info_t *info = NULL;
732 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
735 /* Exact matching of sender */
736 if (!g_strcmp0(info->sender, sender)) {
737 BT_INFO("Unregister GATT server instance [%d]", info->instance_id);
738 /* Unregister all service handles with stack */
739 __bt_remove_all_service_handles(info->instance_id);
741 /* Disable adv if running */
742 BT_INFO("Disable Advertising Adv Handle [%d] sender [%s]",
743 info->adv_handle, sender);
744 _bt_set_advertising(sender, info->adv_handle, FALSE, FALSE);
746 /* Save all instances which need to be unregistered */
747 apps[info->instance_id] = 1;
751 BT_DBG("Its an Internal unregister request by adv application");
752 server_instance = _bt_get_allocated_server_instance(sender, adv_handle, FALSE);
753 BT_DBG("Its an Internal unregister request by adv application: Adv disabled srv instance [%d]", server_instance);
754 if (server_instance == -1) {
755 BT_ERR("No allocated server instance to be removed");
756 return BLUETOOTH_ERROR_INVALID_PARAM;
758 if (!numapps[server_instance].service_handles) {
759 BT_INFO("There are no Service handles with this app, safe to unregister");
760 /* Unregister server instance only if this sender does not have any gatt services in it */
761 result = __bt_do_unregister_server_instance(server_instance);
763 numapps[server_instance].adv_handle = 0;
764 memset(numapps[server_instance].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
765 numapps[server_instance].adv_data_len = 0;
766 memset(numapps[server_instance].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
767 numapps[server_instance].scan_rsp_len = 0;
772 /* Unregister all apps one by one */
773 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
775 BT_INFO("Unregister app[%d]", k);
776 /* Unregister server instance */
777 __bt_do_unregister_server_instance(k);
784 int _bt_register_server_instance(const char *sender, int adv_handle)
786 int ret = OAL_STATUS_SUCCESS;
787 char *uuid_string = NULL;
792 BT_INFO("###Check on which instance Server instance can be initialized....");
793 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
794 if (numapps[k].is_initialized == 1) {
795 BT_INFO("Instance ID [%d] is already in use..Check next slot", numapps[k].instance_id);
798 BT_INFO("Time to register GATT Server..UUID to be used is [%s] slot [%d]", uuid_list[slot-1], slot);
804 BT_ERR("No Slot if free for GATT Server registration..");
805 return BLUETOOTH_ERROR_REGISTRATION_FAILED;
808 uuid_string = g_malloc0(BT_UUID_STRING_MAX);
809 _bt_string_to_uuid(uuid_list[slot-1], (service_uuid_t*)&uuid);
810 g_strlcpy(uuid_string, uuid_list[slot-1], BT_UUID_STRING_MAX);
811 BT_INFO("Copied UUID string [%s]", uuid_string);
813 /* Register GATT Server */
814 ret = gatts_register(&uuid);
815 if (ret != OAL_STATUS_SUCCESS) {
816 BT_ERR("ret: %d", ret);
818 return BLUETOOTH_ERROR_INTERNAL;
820 BT_INFO("GATT Server registration call successfully accepted by OAL..wait for Instance Initialized event from OAL..");
821 /* Return & wait for GATT Server Instance Initialization event */
822 memset(numapps[slot].sender, 0x00, sizeof(numapps[slot].sender));
823 memset(numapps[slot].uuid, 0x00, sizeof(numapps[slot].uuid));
825 g_strlcpy(numapps[slot].sender, sender, sizeof(numapps[slot].sender));
826 g_strlcpy(numapps[slot].uuid, uuid_string, sizeof(numapps[slot].uuid));
828 numapps[slot].is_initialized = 0; /* Set initialization from app registered callback */
829 numapps[slot].adv_handle = adv_handle;
832 return BLUETOOTH_ERROR_NONE;
836 static void __bt_gatt_handle_pending_request_info(int result,
837 int service_function, void *data, unsigned int size)
841 invocation_info_t *req_info = NULL;
842 ret_if(data == NULL);
845 for (l = _bt_get_invocation_list(); l != NULL; l = g_slist_next(l)) {
847 if (req_info == NULL || req_info->service_function != service_function)
850 switch (service_function) {
852 case BT_GATT_SERVER_REGISTER: {
853 bt_service_app_info_t *param = (bt_service_app_info_t*)data;
854 BT_INFO("Sender [%s] Request Sender [%s]", param->sender, req_info->sender);
855 if (!g_strcmp0(req_info->sender, param->sender)) {
856 BT_DBG("GATT Server app found [%s]", req_info->sender);
858 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
859 g_array_append_vals(out_param, ¶m->instance_id, sizeof(int));
860 _bt_service_method_return(req_info->context, out_param, result);
862 g_free(req_info->user_data);
863 _bt_free_info_from_invocation_list(req_info);
864 g_array_free(out_param, TRUE);
868 case BT_GATT_SERVER_START_SERVICE:
869 case BT_GATT_SERVER_DELETE_SERVICE: {
870 bt_service_app_info_t *param = (bt_service_app_info_t*)data;
871 BT_INFO("Sender [%s] Request Sender [%s]", param->sender, req_info->sender);
873 int *saved_instance_id = (int*)req_info->user_data;
874 if (!g_strcmp0(req_info->sender, param->sender) && param->instance_id == *saved_instance_id) {
875 BT_DBG("GATT Server app found [%s] Instance ID [%d] Reply DBUS",
876 req_info->sender, *saved_instance_id);
878 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
879 g_array_append_vals(out_param, &saved_instance_id, sizeof(int));
880 _bt_service_method_return(req_info->context, out_param, result);
882 g_free(req_info->user_data);
883 _bt_free_info_from_invocation_list(req_info);
884 g_array_free(out_param, TRUE);
888 case BT_GATT_SERVER_ADD_SERVICE:
889 case BT_GATT_SERVER_ADD_DESCRIPTOR:
890 case BT_GATT_SERVER_ADD_CHARACTERISTIC: {
891 int *handle = (int*)data;
892 BT_INFO("Characteristic added: Handle [%d]", *handle);
893 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
895 g_array_append_vals(out_param, handle, sizeof(int));
896 _bt_service_method_return(req_info->context, out_param, result);
898 g_free(req_info->user_data);
899 _bt_free_info_from_invocation_list(req_info);
900 g_array_free(out_param, TRUE);
905 case BT_DISCONNECT_LE: {
906 #ifdef TIZEN_GATT_CLIENT
907 char *addr = (char*)req_info->user_data;
908 bluetooth_device_address_t address;
910 if (!g_strcmp0(addr, (char*)data)) {
911 BT_INFO("GATT Client connect-disconnect call pending for app [%s] addr [%s]",
912 req_info->sender, addr);
913 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
914 _bt_convert_addr_string_to_type(address.addr, addr);
916 g_array_append_vals(out_param, (bluetooth_device_address_t*)&address,
917 sizeof(bluetooth_device_address_t));
918 _bt_service_method_return(req_info->context, out_param, result);
920 g_free(req_info->user_data);
921 _bt_free_info_from_invocation_list(req_info);
922 g_array_free(out_param, TRUE);
925 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
927 g_array_append_vals(out_param, (bluetooth_device_address_t*)data,
928 sizeof(bluetooth_device_address_t));
929 _bt_service_method_return(req_info->context, out_param, result);
931 g_free(req_info->user_data);
932 _bt_free_info_from_invocation_list(req_info);
934 g_array_free(out_param, TRUE);
938 #ifdef TIZEN_GATT_CLIENT
939 case BT_GATT_CLIENT_REGISTER: {
940 bt_service_app_info_t *param = (bt_service_app_info_t*)data;
941 BT_INFO("Sender [%s] Request Sender [%s]", param->sender, req_info->sender);
942 if (!g_strcmp0(req_info->sender, param->sender)) {
943 BT_DBG("GATT Client app found [%s] created client ID [%d]",
944 req_info->sender, param->client_id);
946 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
947 g_array_append_vals(out_param, ¶m->client_id, sizeof(int));
948 _bt_service_method_return(req_info->context, out_param, result);
950 g_free(req_info->user_data);
951 _bt_free_info_from_invocation_list(req_info);
952 g_array_free(out_param, TRUE);
956 case BT_GATT_GET_PRIMARY_SERVICES: {
957 bt_services_browse_info_t *param = (bt_services_browse_info_t*)data;
958 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
959 _bt_convert_addr_type_to_string(address,
960 (unsigned char *)(¶m->device_addr.addr));
962 BT_INFO("Request Sender [%s] address [%s]", req_info->sender, address);
964 /* Match address to determine same request */
965 if (!g_strcmp0((char*)req_info->user_data, address)) {
966 BT_DBG("GATT Client app found [%s] Remote address [%s]",
967 req_info->sender, address);
969 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
970 g_array_append_vals(out_param, param,
971 sizeof(bt_services_browse_info_t));
973 //g_array_append_vals(out_param, ¶m->client_id, sizeof(int));
974 _bt_service_method_return(req_info->context, out_param, result);
976 g_free(req_info->user_data);
977 _bt_free_info_from_invocation_list(req_info);
978 g_array_free(out_param, TRUE);
983 case BT_GATT_GET_SERVICE_PROPERTIES: {
984 bt_char_browse_info_t param;
985 memcpy((void*)¶m, data, sizeof(bt_char_browse_info_t));
986 //bt_char_browse_info_t *param = (bt_char_browse_info_t*)data;
987 BT_INFO("Request Sender [%s]", req_info->sender);
988 bluetooth_gatt_client_svc_prop_info_t *prop = (bluetooth_gatt_client_svc_prop_info_t*)req_info->user_data;
990 /* Match both address and service properties to determine same request */
991 if (!memcmp(param.device_addr.addr,
992 prop->device_address.addr,
993 sizeof(bluetooth_device_address_t)) &&
994 !memcmp(param.svc_uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
995 param.svc_inst_id == prop->svc.instance_id) {
996 BT_INFO("Returning Service properties");
998 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
999 g_array_append_vals(out_param, ¶m, sizeof(bt_char_browse_info_t));
1000 _bt_service_method_return(req_info->context, out_param, result);
1002 g_free(req_info->user_data);
1003 _bt_free_info_from_invocation_list(req_info);
1004 g_array_free(out_param, TRUE);
1008 case BT_GATT_GET_CHARACTERISTIC_PROPERTIES: {
1009 bt_descriptor_browse_info_t *param = (bt_descriptor_browse_info_t*)data;
1011 BT_INFO("Request Sender [%s]", req_info->sender);
1012 bluetooth_gatt_client_char_prop_info_t *prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
1014 /* Match both address, service properties &char properties to determine same request */
1015 if (!memcmp(param->device_addr.addr,
1016 prop->device_address.addr,
1017 sizeof(bluetooth_device_address_t)) &&
1018 !memcmp(param->svc_uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1019 param->svc_inst_id == prop->svc.instance_id &&
1020 !memcmp(param->char_uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1021 param->char_inst_id == prop->characteristic.instance_id) {
1022 BT_INFO("Returning Characteristic properties");
1023 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1024 g_array_append_vals(out_param, param, sizeof(bt_descriptor_browse_info_t));
1025 _bt_service_method_return(req_info->context, out_param, result);
1027 g_free(req_info->user_data);
1028 _bt_free_info_from_invocation_list(req_info);
1029 g_array_free(out_param, TRUE);
1033 case BT_GATT_WATCH_CHARACTERISTIC: {
1034 bt_gatt_notif_reg_info_t *param = (bt_gatt_notif_reg_info_t*)data;
1035 bluetooth_gatt_client_char_prop_info_t *prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
1036 BT_INFO("Request Sender [%s]", req_info->sender);
1037 /* Match both address, service properties &char properties to determine same request */
1038 if (!memcmp(param->addr.addr,
1039 prop->device_address.addr,
1040 sizeof(bluetooth_device_address_t)) &&
1041 !memcmp(param->svc_uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1042 param->svc_inst == prop->svc.instance_id &&
1043 !memcmp(param->char_uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1044 param->char_inst == prop->characteristic.instance_id) {
1045 BT_INFO("Characteristic Watch Successful: Is registered [%d]",
1046 param->is_registered);
1047 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1048 g_array_append_vals(out_param, param, sizeof(bt_gatt_notif_reg_info_t));
1049 _bt_service_method_return(req_info->context, out_param, result);
1050 g_free(req_info->user_data);
1051 _bt_free_info_from_invocation_list(req_info);
1052 g_array_free(out_param, TRUE);
1056 case BT_GATT_READ_CHARACTERISTIC:
1057 case BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE: {
1058 bluetooth_gatt_client_char_prop_info_t *param = (bluetooth_gatt_client_char_prop_info_t*)data;
1059 BT_INFO("Request Sender [%s]", req_info->sender);
1061 char *addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
1062 bluetooth_gatt_client_char_prop_info_t *prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
1063 _bt_convert_addr_type_to_string(addr, prop->device_address.addr);
1065 if (!memcmp(param, prop, sizeof(bluetooth_gatt_client_char_prop_info_t))) {
1066 BT_INFO("Gatt Char read or write request matched for address [%s]", addr);
1067 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1068 g_array_append_vals(out_param, param, sizeof(bluetooth_gatt_client_char_prop_info_t));
1069 _bt_service_method_return(req_info->context, out_param, result);
1071 g_free(req_info->user_data);
1072 _bt_free_info_from_invocation_list(req_info);
1073 g_array_free(out_param, TRUE);
1078 case BT_GATT_READ_DESCRIPTOR_VALUE:
1079 case BT_GATT_WRITE_DESCRIPTOR_VALUE: {
1080 bluetooth_gatt_client_desc_prop_info_t *param = (bluetooth_gatt_client_desc_prop_info_t*)data;
1081 BT_INFO("Request Sender [%s]", req_info->sender);
1083 char *addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
1084 bluetooth_gatt_client_desc_prop_info_t *prop = (bluetooth_gatt_client_desc_prop_info_t*)req_info->user_data;
1085 _bt_convert_addr_type_to_string(addr, prop->device_address.addr);
1087 if (!memcmp(param, prop, sizeof(bluetooth_gatt_client_desc_prop_info_t))) {
1088 BT_INFO("Descriptor read or write request matched for address [%s]", addr);
1089 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1090 g_array_append_vals(out_param, param, sizeof(bluetooth_gatt_client_desc_prop_info_t));
1091 _bt_service_method_return(req_info->context, out_param, result);
1093 g_free(req_info->user_data);
1094 _bt_free_info_from_invocation_list(req_info);
1095 g_array_free(out_param, TRUE);
1108 static void __bt_handle_server_instance_registered(event_gatts_register_t *data)
1110 bt_service_app_info_t *info = NULL;
1112 char *uuid_string = g_malloc0(BT_UUID_STRING_MAX);
1114 _bt_uuid_to_string(&(data->server_uuid), uuid_string);
1115 BT_INFO("Instance ID is Intialized [%d] UUID initialized [%s]", data->server_inst, uuid_string);
1117 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1118 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1121 if (g_strcmp0(info->uuid, uuid_string) == 0) {
1122 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1123 info->is_initialized = TRUE;
1124 info->instance_id = data->server_inst;
1125 info->adv_instance = data->server_inst;
1126 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE, BT_GATT_SERVER_REGISTER,
1127 (void*)info, sizeof(bt_service_app_info_t));
1131 g_free(uuid_string);
1134 static void __bt_handle_gatt_server_service_added(event_gatts_srvc_prm_t *event)
1136 int result = BLUETOOTH_ERROR_NONE;
1140 bt_service_app_info_t *info = NULL;
1143 char *uuid_str = g_malloc0(BT_UUID_STRING_MAX);
1144 _bt_uuid_to_string(&(event->gatt_srvc_id.id.uuid), uuid_str);
1145 BT_INFO("GATT Added Service UUID: [%s] Server Ins [%d] Service hdl [%d] Is Primary [%d]\n",
1146 uuid_str, event->gatt_srvc_stat.server_inst, svc_handle, event->gatt_srvc_id.is_prmry);
1148 if (event->gatt_srvc_stat.status != OAL_STATUS_SUCCESS) {
1149 result = BLUETOOTH_ERROR_INTERNAL;
1150 svc_handle = 0; /* Service handle set to 0 indicates.
1151 0 is reserved by BT SIG, cant be used by app*/
1153 svc_handle = event->gatt_srvc_stat.servic_hndl;
1155 BT_INFO("GATT Added Service Status [%d] Server Ins [%d] Service hdl [%d] Is Primary [%d]\n",
1156 event->gatt_srvc_stat.status, event->gatt_srvc_stat.server_inst, svc_handle, event->gatt_srvc_id.is_prmry);
1158 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1159 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1162 if (info->instance_id == event->gatt_srvc_stat.server_inst) {
1163 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1164 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_ADD_SERVICE,
1165 (int*)&svc_handle, sizeof(int));
1167 /* Add Service Handle */
1168 if (svc_handle > 0) {
1169 handle = g_malloc0(sizeof(int));
1170 *handle = svc_handle;
1171 numapps[event->gatt_srvc_stat.server_inst].service_handles = g_slist_append(numapps[event->gatt_srvc_stat.server_inst].service_handles, handle);
1172 count = g_slist_length(numapps[event->gatt_srvc_stat.server_inst].service_handles);
1173 BT_INFO("Added Service handle [%d] to list..current count [%d]", svc_handle, count);
1182 static void __bt_handle_gatt_server_characteristic_added(event_gatts_srvc_charctr_t *event)
1184 int result = BLUETOOTH_ERROR_NONE;
1186 bt_service_app_info_t *info = NULL;
1188 char *uuid_str = g_malloc0(BT_UUID_STRING_MAX);
1190 BT_INFO("GATT Server Char added status [%d]", event->gatt_srvc_stat.status);
1191 BT_INFO("GATT Server_inst: [%d]", event->gatt_srvc_stat.server_inst);
1192 BT_INFO("GATT Service Handle: [%d]", event->gatt_srvc_stat.servic_hndl);
1193 BT_INFO("GATT Add characteristic Status: [%d]", event->gatt_srvc_stat.status);
1194 BT_INFO("GATT Service characteristic Handle: [%d]", event->charctr_hndl);
1196 if (event->gatt_srvc_stat.status != OAL_STATUS_SUCCESS) {
1197 result = BLUETOOTH_ERROR_INTERNAL;
1198 char_handle = 0; /* characteristic handle set to 0 indicates.
1199 0 is reserved by BT SIG, cant be used by app*/
1201 char_handle = event->charctr_hndl;
1204 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1205 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1208 if (info->instance_id == event->gatt_srvc_stat.server_inst) {
1209 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1210 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_ADD_CHARACTERISTIC,
1211 (int*)&char_handle, sizeof(int));
1217 _bt_uuid_to_string(&(event->charctr_uuid), uuid_str);
1218 BT_INFO("GATT Added Characteristic: UUID: [%s]", uuid_str);
1223 static void __bt_handle_gatt_server_descriptor_added(event_gatts_srvc_descr_t* event)
1225 int result = BLUETOOTH_ERROR_NONE;
1227 bt_service_app_info_t *info = NULL;
1229 char *uuid_str = g_malloc0(BT_UUID_STRING_MAX);
1231 BT_INFO("GATT Server Descriptor added status [%d]", event->gatt_srvc_stat.status);
1232 BT_INFO("GATT Server_inst: [%d]", event->gatt_srvc_stat.server_inst);
1233 BT_INFO("GATT Service Handle: [%d]", event->gatt_srvc_stat.servic_hndl);
1234 BT_INFO("GATT Add Descriptor Status: [%d]", event->gatt_srvc_stat.status);
1235 BT_INFO("GATT Service Descriptor Handle: [%d]", event->descrptr_hndl);
1237 if (event->gatt_srvc_stat.status != OAL_STATUS_SUCCESS) {
1238 result = BLUETOOTH_ERROR_INTERNAL;
1239 desc_handle = 0; /* Service handle set to 0 indicates.
1240 0 is reserved by BT SIG, cant be used by app*/
1242 desc_handle = event->descrptr_hndl;
1244 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1245 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1248 if (info->instance_id == event->gatt_srvc_stat.server_inst) {
1249 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1250 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_ADD_DESCRIPTOR,
1251 (int*)&desc_handle, sizeof(int));
1256 _bt_uuid_to_string(&(event->descrptr_uuid), uuid_str);
1257 BT_INFO("GATT Added Descriptor: UUID: [%s]", uuid_str);
1262 static void __bt_handle_gatt_server_service_started(event_gatts_srvc_t *event)
1264 bt_service_app_info_t *info = NULL;
1265 int result = BLUETOOTH_ERROR_NONE;
1267 BT_INFO("GATT Server Service Started..");
1270 BT_INFO("GATT Server_inst: [%d]", event->server_inst);
1271 BT_INFO("GATT Service Handle: [%d]", event->servic_hndl);
1272 BT_INFO("GATT Service Started Status: [%d]", event->status);
1274 if (event->status != OAL_STATUS_SUCCESS) {
1275 BT_ERR("GATT Server Service Start Failed Err: [%d]", event->status);
1276 result = BLUETOOTH_ERROR_INTERNAL;
1279 /* Check if the just registered Instance ID belongs to requester */
1280 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1283 if (info->instance_id == event->server_inst) {
1284 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1285 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_START_SERVICE,
1286 (void*)info, sizeof(bt_service_app_info_t));
1292 static void __bt_handle_gatt_server_service_stopped(event_gatts_srvc_t *event)
1294 int result = BLUETOOTH_ERROR_NONE;
1295 bt_service_app_info_t *info = NULL;
1297 BT_INFO("GATT Server Service Stopped..");
1299 BT_INFO("GATT Server_inst: [%d]", event->server_inst);
1300 BT_INFO("GATT Service Handle: [%d]", event->servic_hndl);
1301 BT_INFO("GATT Service Stopped Status: [%d]", event->status);
1303 if (event->status != OAL_STATUS_SUCCESS) {
1304 BT_ERR("GATT Server Service Stop Failed Err: [%d]", event->status);
1305 result = BLUETOOTH_ERROR_INTERNAL;
1308 /* Check if the just registered Instance ID belongs to requester */
1309 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1312 if (info->instance_id == event->server_inst) {
1313 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1314 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_STOP_SERVICE,
1315 (void*)info, sizeof(bt_service_app_info_t));
1321 static void __bt_handle_gatt_server_service_deleted(event_gatts_srvc_t *event)
1323 int result = BLUETOOTH_ERROR_NONE;
1324 bt_service_app_info_t *info = NULL;
1326 BT_INFO("GATT Server Service Deleted..");
1328 BT_INFO("GATT Server_inst: [%d]", event->server_inst);
1329 BT_INFO("GATT Service Handle: [%d]", event->servic_hndl);
1330 BT_INFO("GATT Service Deleted Status: [%d]", event->status);
1332 if (event->status != OAL_STATUS_SUCCESS) {
1333 BT_ERR("GATT Server Service Delete Failed Err: [%d]", event->status);
1334 result = BLUETOOTH_ERROR_INTERNAL;
1337 /* Check if the just registered Instance ID belongs to requester */
1338 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1341 if (info->instance_id == event->server_inst) {
1342 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1343 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_DELETE_SERVICE,
1344 (void*)info, sizeof(bt_service_app_info_t));
1350 static struct gatt_client_info_t *__bt_find_remote_gatt_client_info(char *address)
1353 struct gatt_client_info_t *info = NULL;
1354 for (l = gatt_client_info_list; l != NULL; l = g_slist_next(l)) {
1355 info = (struct gatt_client_info_t*)l->data;
1359 if (!g_strcmp0(info->addr, address)) {
1360 BT_INFO("Remote GATT client found addr[%s]", info->addr);
1367 static struct gatt_client_info_t *__bt_find_remote_gatt_client_info_from_conn_id(int conn_id)
1370 struct gatt_client_info_t *info = NULL;
1372 for (l = gatt_client_info_list; l != NULL; l = g_slist_next(l)) {
1373 info = (struct gatt_client_info_t*)l->data;
1377 if (info->connection_id == conn_id) {
1378 BT_INFO("Remote GATT client found addr[%s]", info->addr);
1385 #ifdef TIZEN_GATT_CLIENT
1386 static struct gatt_server_info_t *__bt_find_remote_gatt_server_info(char *address)
1389 struct gatt_server_info_t *info = NULL;
1390 for (l = gatt_server_info_list; l != NULL; l = g_slist_next(l)) {
1391 info = (struct gatt_server_info_t*)l->data;
1395 if (!g_strcmp0(info->addr, address)) {
1396 BT_INFO("Remote GATT Server found addr[%s]", info->addr);
1403 static struct gatt_out_conn_info_t* __bt_find_gatt_outgoing_conn_info(char *address)
1406 struct gatt_out_conn_info_t *info = NULL;
1407 for (l = outgoing_gatt_conn_list; l != NULL; l = g_slist_next(l)) {
1408 info = (struct gatt_out_conn_info_t*)l->data;
1412 if (!g_strcmp0(info->addr, address)) {
1413 BT_INFO("Outgoing connection info found addr[%s]", info->addr);
1420 static struct gatt_server_info_t *__bt_find_remote_gatt_server_info_from_client_if(int client_if)
1423 struct gatt_server_info_t *info = NULL;
1425 for (l = gatt_server_info_list; l != NULL; l = g_slist_next(l)) {
1426 info = (struct gatt_server_info_t*)l->data;
1430 if (info->client_id == client_if) {
1431 BT_INFO("Remote GATT server found addr[%s]", info->addr);
1442 #ifdef TIZEN_GATT_CLIENT
1443 static void __bt_handle_gatt_server_connection_state(event_gatts_conn_t *event)
1445 int result = BLUETOOTH_ERROR_NONE;
1446 struct gatt_client_info_t *conn_info = NULL;
1447 bluetooth_device_address_t dev_addr;
1448 GVariant *param = NULL;
1450 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1452 memcpy(dev_addr.addr, event->address.addr, 6);
1454 /* REPLY dbus Context to application which called BT_CONNECT_LE. There is status
1456 _bt_convert_addr_type_to_string(address,
1457 (unsigned char *)dev_addr.addr);
1459 if (event->status != OAL_STATUS_SUCCESS)
1460 result = BLUETOOTH_ERROR_INTERNAL;
1462 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
1463 address, BT_ADDRESS_STRING_SIZE);
1465 BT_INFO("GATT Server Connedted: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1466 address, event->server_inst, event->conn_id);
1469 /* Check if device is already in connected list */
1470 conn_info = __bt_find_remote_gatt_client_info(address);
1473 BT_INFO("Conn Info absent: But no need to Send Local GATT Server Connected event to apps");
1475 param = g_variant_new("(is)", result, address);
1477 /* Send event to application */
1478 _bt_send_event(BT_DEVICE_EVENT,
1479 BLUETOOTH_EVENT_GATT_SERVER_CONNECTED, /* Local device is GATT server */
1482 /* Save Connection info */
1483 conn_info = g_new0(struct gatt_client_info_t, 1);
1484 conn_info->addr = g_strdup(address);
1485 BT_INFO("Added GATT client addr[%s]", conn_info->addr);
1486 conn_info->connection_id = event->conn_id;
1487 conn_info->instance_id = event->server_inst;
1488 gatt_client_info_list = g_slist_append(gatt_client_info_list, conn_info);
1489 BT_INFO("Total num of connected Remote GATT Clients [%d]", g_slist_length(gatt_client_info_list));
1495 /* GATT Server Dis connected */
1496 static void __bt_handle_gatt_server_disconnection_state(event_gatts_conn_t *event)
1498 int result = BLUETOOTH_ERROR_NONE;
1499 struct gatt_client_info_t *conn_info = NULL;
1500 bluetooth_device_address_t dev_addr;
1501 GVariant *param = NULL;
1502 char address[BT_ADDRESS_STRING_SIZE];
1504 memcpy(dev_addr.addr, event->address.addr, 6);
1506 /* REPLY dbus Context to application which called BT_DISCONNECT_LE. There is status
1508 _bt_convert_addr_type_to_string(address,
1509 (unsigned char *)dev_addr.addr);
1511 if (event->status != OAL_STATUS_SUCCESS)
1512 result = BLUETOOTH_ERROR_INTERNAL;
1514 if (NULL == _bt_get_request_info_data(BT_DISCONNECT_LE, address)) {
1515 if (NULL != _bt_get_request_info_data(BT_CONNECT_LE, address)) {
1516 result = BLUETOOTH_ERROR_INTERNAL;
1518 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
1519 address, BT_ADDRESS_STRING_SIZE);
1521 BT_ERR("Failed to connect Local GATT Server Remote Client addr[%s]", address);
1525 __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE,
1526 address, BT_ADDRESS_STRING_SIZE);
1529 BT_INFO("Local GATT Server DisConnected: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1530 address, event->server_inst, event->conn_id);
1531 /* Remove Connection info */
1532 conn_info = __bt_find_remote_gatt_client_info(address);
1534 BT_INFO("No need to Send Local GATT Server Disconnected event to apps, just remove remote client info");
1536 param = g_variant_new("(is)", result, address);
1537 /* Send event to application */
1538 _bt_send_event(BT_DEVICE_EVENT,
1539 BLUETOOTH_EVENT_GATT_SERVER_DISCONNECTED, /* Local device is GATT server */
1542 /* Remove info from List */
1543 gatt_client_info_list = g_slist_remove(gatt_client_info_list, conn_info);
1544 BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
1545 g_free(conn_info->addr);
1551 static void __bt_handle_gatt_server_connection_state(event_gatts_conn_t *event)
1553 int result = BLUETOOTH_ERROR_NONE;
1554 struct gatt_client_info_t *conn_info = NULL;
1555 bt_service_app_info_t *info = NULL;
1556 bluetooth_device_address_t dev_addr;
1557 GVariant *param = NULL;
1560 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1562 memcpy(dev_addr.addr, event->address.addr, 6);
1565 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1566 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1569 if (info->instance_id == event->server_inst) {
1570 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1571 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
1572 (bluetooth_device_address_t*)&dev_addr, sizeof(bluetooth_device_address_t));
1578 /* Send event to BT-API */
1579 _bt_convert_addr_type_to_string(address,
1580 (unsigned char *)dev_addr.addr);
1582 BT_INFO("GATT Server Connedted: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1583 address, event->server_inst, event->conn_id);
1586 /* Check if device is already in connected list */
1587 conn_info = __bt_find_remote_gatt_client_info(address);
1590 param = g_variant_new("(is)", result, address);
1591 /* Send event to application */
1592 _bt_send_event(BT_GATT_SERVER_EVENT,
1593 BLUETOOTH_EVENT_GATT_CONNECTED,
1596 /* Save Connection info */
1597 conn_info = g_new0(struct gatt_client_info_t, 1);
1598 conn_info->addr = g_strdup(address);
1599 BT_INFO("Added GATT client addr[%s]", conn_info->addr);
1600 conn_info->connection_id = event->conn_id;
1601 conn_info->instance_id = event->server_inst;
1602 gatt_client_info_list = g_slist_append(gatt_client_info_list, conn_info);
1603 BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
1609 /* GATT Server Dis connected */
1610 static void __bt_handle_gatt_server_disconnection_state(event_gatts_conn_t *event)
1612 int result = BLUETOOTH_ERROR_NONE;
1613 struct gatt_client_info_t *conn_info = NULL;
1614 bluetooth_device_address_t dev_addr;
1615 bt_service_app_info_t *info = NULL;
1616 GVariant *param = NULL;
1619 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1621 memcpy(dev_addr.addr, event->address.addr, 6);
1623 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1624 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1627 if (info->instance_id == event->server_inst) {
1628 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1629 __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE,
1630 (bluetooth_device_address_t*)&dev_addr, sizeof(bluetooth_device_address_t));
1636 /* Send event to BT-API */
1637 _bt_convert_addr_type_to_string(address,
1638 (unsigned char *)dev_addr.addr);
1640 BT_INFO("GATT Server DisConnected: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1641 address, event->server_inst, event->conn_id);
1643 param = g_variant_new("(is)", result, address);
1644 /* Send event to application */
1645 _bt_send_event(BT_GATT_SERVER_EVENT,
1646 BLUETOOTH_EVENT_GATT_DISCONNECTED,
1649 /* Remove Connection info */
1650 conn_info = __bt_find_remote_gatt_client_info(address);
1652 BT_INFO("Remove GATT client info from List..");
1653 /* Remove info from List */
1654 gatt_client_info_list = g_slist_remove(gatt_client_info_list, conn_info);
1655 BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
1656 g_free(conn_info->addr);
1665 static void __bt_handle_gatt_server_acquire_write_requested(event_gatts_srvc_acquire_attr_t *event)
1667 GVariant *param = NULL;
1668 int result = BLUETOOTH_ERROR_NONE;
1669 struct gatt_server_req_info *req_info = NULL;
1670 bluetooth_device_address_t dev_addr;
1671 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1673 BT_INFO("GATT Server ACQUIRE Write Req Connection ID: [%d]", event->attr_trans.conn_id);
1674 BT_INFO("GATT Server ACQUIRE Write Req Transaction ID:[%d]", event->attr_trans.trans_id);
1675 BT_INFO("GATT Server ACQUIRE Write Req Attribute Handle: [%d]", event->attr_trans.attr_handle);
1677 //address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1678 memcpy(dev_addr.addr, event->address.addr, 6);
1680 req_info = g_new0(struct gatt_server_req_info, 1);
1681 req_info->request_id = event->attr_trans.trans_id;
1682 req_info->attribute_handle = event->attr_trans.attr_handle;
1683 req_info->connection_id = event->attr_trans.conn_id;
1684 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_ACQUIRE_WRITE;
1685 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1687 _bt_convert_addr_type_to_string(address,
1688 (unsigned char *)dev_addr.addr);
1690 param = g_variant_new("(iiiiiis)", result,
1691 event->attr_trans.conn_id,
1692 event->attr_trans.trans_id,
1693 event->attr_trans.attr_handle,
1694 event->mtu, event->attr_trans.offset, address);
1695 BT_INFO("GATT Server ACQUIRE Write Req Attribute remote address : [%s]", address);
1697 _bt_send_event(BT_GATT_SERVER_EVENT,
1698 BLUETOOTH_EVENT_GATT_SERVER_ACQUIRE_WRITE,
1703 static void __bt_handle_gatt_server_acquire_notify_requested(event_gatts_srvc_acquire_attr_t *event)
1705 GVariant *param = NULL;
1706 int result = BLUETOOTH_ERROR_NONE;
1707 struct gatt_server_req_info *req_info = NULL;
1708 bluetooth_device_address_t dev_addr;
1709 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1711 BT_INFO("GATT Server ACQUIRE Notify Req Connection ID: [%d]", event->attr_trans.conn_id);
1712 BT_INFO("GATT Server ACQUIRE Notify Req Transaction ID:[%d]", event->attr_trans.trans_id);
1713 BT_INFO("GATT Server ACQUIRE Notify Req Attribute Handle: [%d]", event->attr_trans.attr_handle);
1714 BT_INFO("GATT Server ACQUIRE notify Req address : [%s]", address);
1716 memcpy(dev_addr.addr, event->address.addr, 6);
1717 _bt_convert_addr_type_to_string(address,
1718 (unsigned char *)dev_addr.addr);
1719 BT_INFO("GATT Server ACQUIRE notify Req remote address : [%s]", address);
1721 req_info = g_new0(struct gatt_server_req_info, 1);
1722 req_info->request_id = event->attr_trans.trans_id;
1723 req_info->attribute_handle = event->attr_trans.attr_handle;
1724 req_info->connection_id = event->attr_trans.conn_id;
1725 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_ACQUIRE_NOTIFY;
1726 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1728 param = g_variant_new("(iiiiiis)", result,
1729 event->attr_trans.conn_id,
1730 event->attr_trans.trans_id,
1731 event->attr_trans.attr_handle,
1732 event->mtu, event->attr_trans.offset,
1735 BT_INFO("GATT Server ACQUIRE Notify Req Attribute : ");
1737 _bt_send_event(BT_GATT_SERVER_EVENT,
1738 BLUETOOTH_EVENT_GATT_SERVER_ACQUIRE_NOTIFY,
1742 static void __bt_handle_gatt_server_write_requested(event_gatts_srvc_write_attr_t *event)
1745 bluetooth_device_address_t dev_addr;
1746 GVariant *param = NULL;
1747 int result = BLUETOOTH_ERROR_NONE;
1749 gboolean is_prepare_write;
1751 GVariant *data = NULL;
1753 struct gatt_server_req_info *req_info = NULL;
1754 BT_INFO("GATT Server Write Requested");
1756 memcpy(dev_addr.addr, event->address.addr, 6);
1758 BT_INFO("GATT Server Write Req Connection ID: [%d]", event->attr_trans.conn_id);
1759 BT_INFO("GATT Server Write Req Transaction ID:[%d]", event->attr_trans.trans_id);
1760 BT_INFO("GATT Server Write Req Attribute Handle: [%d]", event->attr_trans.attr_handle);
1761 BT_INFO("GATT Server Write Req Attribute Offset: [%d]", event->attr_trans.offset);
1762 BT_INFO("GATT Server Write Req value length [%d]", event->length);
1763 BT_INFO("GATT Server Write Req needs response: [%d]", event->need_rsp);
1764 BT_INFO("GATT Server Write Req Is Prep: [%d]", event->is_prep);
1766 if (event->length <= 0) {
1767 BT_INFO("GATT Server write requested, but length of attr value is 0");
1771 need_resp = event->need_rsp;
1772 is_prepare_write = event->is_prep;
1774 write_val = g_memdup(&event->value[0], event->length);
1776 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1777 _bt_convert_addr_type_to_string(address,
1778 (unsigned char *)dev_addr.addr);
1780 BT_INFO("GATT Server Write Request from remote client [%s]", address);
1782 if (event->length > 0) {
1783 for (i = 0; i < event->length; i++)
1784 BT_DBG("Data[%d] = [0x%x]", i, event->value[i]);
1786 /* Save Write Request Info */
1787 req_info = g_new0(struct gatt_server_req_info, 1);
1788 req_info->request_id = event->attr_trans.trans_id;
1789 req_info->attribute_handle = event->attr_trans.attr_handle;
1790 req_info->connection_id = event->attr_trans.conn_id;
1791 req_info->addr = address;
1792 req_info->offset = event->attr_trans.offset;
1793 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_WRITE;
1794 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1796 data = g_variant_new_from_data(
1797 G_VARIANT_TYPE_BYTESTRING,
1802 param = g_variant_new("(iiiiiibbsn@ay)", result,
1803 event->attr_trans.conn_id,
1804 event->attr_trans.trans_id,
1805 event->attr_trans.attr_handle,
1806 event->attr_trans.offset,
1814 _bt_send_event(BT_GATT_SERVER_EVENT,
1815 BLUETOOTH_EVENT_GATT_SERVER_VALUE_CHANGED,
1821 static void __bt_handle_gatt_server_read_requested(event_gatts_srvc_read_attr_t *event)
1823 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1824 bluetooth_device_address_t dev_addr;
1825 int result = BLUETOOTH_ERROR_NONE;
1826 struct gatt_server_req_info *req_info = NULL;
1827 GVariant *param = NULL;
1829 BT_INFO("GATT Server Read Requested");
1831 memcpy(dev_addr.addr, event->address.addr, 6);
1833 BT_INFO("GATT Server-Client Connection ID: [%d]", event->attr_trans.conn_id);
1834 BT_INFO("GATT Server-Client Transaction ID: [%d]", event->attr_trans.trans_id);
1835 BT_INFO("GATT Server Attribute Handle: [%d]", event->attr_trans.attr_handle);
1836 BT_INFO("GATT Server Attribute Offset for read: [%d]", event->attr_trans.offset);
1837 BT_INFO("GATT Server Attribute is long: [%d]", event->is_long);
1839 is_long = event->is_long;
1841 /* Save Read Request Info */
1842 req_info = g_new0(struct gatt_server_req_info, 1);
1843 req_info->request_id = event->attr_trans.trans_id;
1844 req_info->attribute_handle = event->attr_trans.attr_handle;
1845 req_info->connection_id = event->attr_trans.conn_id;
1846 req_info->addr = address;
1847 req_info->offset = event->attr_trans.offset;
1848 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_READ;
1849 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1851 /* Send event to BT-API */
1852 _bt_convert_addr_type_to_string(address,
1853 (unsigned char *)dev_addr.addr);
1855 BT_INFO("GATT Server Read Request from remote client [%s]", address);
1857 param = g_variant_new("(iiiiibs)", result,
1858 event->attr_trans.conn_id,
1859 event->attr_trans.trans_id,
1860 event->attr_trans.attr_handle,
1861 event->attr_trans.offset,
1865 _bt_send_event(BT_GATT_SERVER_EVENT,
1866 BLUETOOTH_EVENT_GATT_SERVER_READ_REQUESTED,
1870 static void __bt_handle_gatt_server_indicate_confirmed(event_gatts_ind_cnfrm_t *event)
1872 bluetooth_device_address_t dev_addr;
1873 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1874 int cur_connected_clients;
1875 static int recvd = 0;
1876 gboolean completed = 0;
1877 GVariant *param = NULL;
1879 /* OAL event does provide error, so MW assumes event will never contain wrong data,
1880 incase of any issues, check with OAL */
1881 int result = BLUETOOTH_ERROR_NONE;
1883 memcpy(dev_addr.addr, event->address.addr, 6);
1884 _bt_convert_addr_type_to_string(address,
1885 (unsigned char *)dev_addr.addr);
1887 BT_INFO("Indication sent to GATT client [%s] conn_ ID [%d] transaction ID [%d] Att handle [%d]",
1888 address, event->conn_id, event->trans_id, event->attr_handle);
1891 cur_connected_clients = g_slist_length(gatt_client_info_list);
1892 BT_INFO("Number of connected clients during sending Indication [%d] & current connected count [%d]",
1893 num_indicate_clients, cur_connected_clients);
1896 if (recvd == num_indicate_clients) {
1897 BT_INFO("Gatt indication confirm event for last GATT client.. [%s]", address);
1898 completed = 1; /* Last event */
1899 recvd = 0; /* Reset */
1900 num_indicate_clients = 0;
1903 param = g_variant_new("(isib)",
1909 /* Send event to BT-API */
1910 _bt_send_event(BT_GATT_SERVER_EVENT,
1911 BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_COMPLETED,
1914 BT_INFO("Received Indication confirm for client number [%d]", recvd);
1918 /* Tizen Platform Specific */
1919 static void __bt_handle_gatt_server_notification_changed(event_gatts_notif_t *event)
1921 bluetooth_device_address_t dev_addr;
1922 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1923 GVariant *param = NULL;
1926 /* OAL event does provide error, so MW assumes event will never contain wrong data,
1927 incase of any issues, check with OAL */
1928 int result = BLUETOOTH_ERROR_NONE;
1930 memcpy(dev_addr.addr, event->address.addr, 6);
1931 _bt_convert_addr_type_to_string(address,
1932 (unsigned char *)dev_addr.addr);
1934 BT_INFO("notification_changed [%s] conn_ ID [%d] transaction ID [%d] Att handle [%d] Notify[%d]",
1935 address, event->conn_id, event->trans_id, event->attr_handle, event->notify);
1937 /* Set Notifcation status */
1938 notify = event->notify;
1940 param = g_variant_new("(isib)",
1946 /* Send event to BT-API */
1947 _bt_send_event(BT_GATT_SERVER_EVENT,
1948 BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_STATE_CHANGED,
1954 static void __bt_handle_gatt_mtu_changed_event(event_gatts_mtu_changed_t *event)
1956 int result = BLUETOOTH_ERROR_NONE;
1957 struct gatt_client_info_t *conn_info = NULL;
1958 GVariant *param = NULL;
1960 BT_INFO("GATT Server MTU changed event");
1962 conn_info = __bt_find_remote_gatt_client_info_from_conn_id(event->conn_id);
1963 if (conn_info == NULL) {
1964 BT_ERR("Cant find connection Information");
1967 BT_INFO("Got connection Info GATT client [%s] MTU Size [%d]",
1968 conn_info->addr, event->mtu_size);
1970 param = g_variant_new("(isqy)",
1976 /* Send event to BT-API */
1977 _bt_send_event(BT_GATT_SERVER_EVENT,
1978 BLUETOOTH_EVENT_GATT_ATT_MTU_CHANGED,
1982 static void __bt_gatt_event_handler(int event_type, gpointer event_data)
1984 BT_INFO("OAL event = 0x%x, \n", event_type);
1985 switch (event_type) {
1986 case OAL_EVENT_BLE_SERVER_INSTANCE_INITIALISED: {
1987 BT_INFO("OAL Event: Server Instance Registered");
1988 /* GATT Server Registered event is handled in MAIN thread context */
1989 __bt_handle_server_instance_registered((event_gatts_register_t *)event_data);
1992 case OAL_EVENT_GATTS_SERVICE_ADDED: {
1993 BT_INFO("OAL Event: GATT Service added");
1994 __bt_handle_gatt_server_service_added((event_gatts_srvc_prm_t *)event_data);
1997 case OAL_EVENT_GATTS_CHARACTERISTIC_ADDED: {
1998 BT_INFO("OAL Event: GATT characteristic added");
1999 __bt_handle_gatt_server_characteristic_added((event_gatts_srvc_charctr_t *)event_data);
2002 case OAL_EVENT_GATTS_DESCRIPTOR_ADDED: {
2003 BT_INFO("OAL Event: GATT descriptor added");
2004 __bt_handle_gatt_server_descriptor_added((event_gatts_srvc_descr_t *)event_data);
2007 case OAL_EVENT_GATTS_SERVICE_STARTED: {
2008 BT_INFO("OAL Event: GATT Service started");
2009 __bt_handle_gatt_server_service_started((event_gatts_srvc_t *)event_data);
2012 case OAL_EVENT_GATTS_SERVICE_STOPED: {
2013 BT_INFO("OAL Event: GATT Service stopped");
2014 __bt_handle_gatt_server_service_stopped((event_gatts_srvc_t *)event_data);
2017 case OAL_EVENT_GATTS_SERVICE_DELETED: {
2018 BT_INFO("OAL Event: GATT Service deleted");
2019 __bt_handle_gatt_server_service_deleted((event_gatts_srvc_t *) event_data);
2022 case OAL_EVENT_GATTS_CONNECTION_COMPLETED: {
2023 BT_INFO("OAL Event: GATT Server Connected");
2024 __bt_handle_gatt_server_connection_state((event_gatts_conn_t *)event_data);
2027 case OAL_EVENT_GATTS_DISCONNECTION_COMPLETED: {
2028 BT_INFO("OAL Event: GATT Server Disconnected");
2029 __bt_handle_gatt_server_disconnection_state((event_gatts_conn_t *)event_data);
2032 case OAL_EVENT_GATTS_REQUEST_READ: {
2033 BT_INFO("OAL Event: GATT Server Read Request");
2034 __bt_handle_gatt_server_read_requested((event_gatts_srvc_read_attr_t *)event_data);
2037 case OAL_EVENT_GATTS_REQUEST_WRITE: {
2038 BT_INFO("OAL Event: GATT Server Write Request");
2039 __bt_handle_gatt_server_write_requested((event_gatts_srvc_write_attr_t *)event_data);
2042 case OAL_EVENT_GATTS_REQUEST_ACQUIRE_WRITE: {
2043 BT_INFO("OAL Event: GATT Server Acquire Write Request");
2044 __bt_handle_gatt_server_acquire_write_requested((event_gatts_srvc_acquire_attr_t*)event_data);
2047 case OAL_EVENT_GATTS_REQUEST_ACQUIRE_NOTIFY: {
2048 BT_INFO("OAL Event: GATT ServerAcquire Notify Request");
2049 __bt_handle_gatt_server_acquire_notify_requested((event_gatts_srvc_acquire_attr_t*)event_data);
2052 case OAL_EVENT_GATTS_IND_CONFIRM: {
2053 BT_INFO("OAL Event: GATT Server Indication confirmed");
2054 __bt_handle_gatt_server_indicate_confirmed((event_gatts_ind_cnfrm_t *)event_data);
2057 case OAL_EVENT_GATTS_NOTIFICATION: { /* Tizen Platform Specific */
2058 BT_INFO("OAL Event: GATT Server DisConnected");
2059 __bt_handle_gatt_server_notification_changed((event_gatts_notif_t *)event_data);
2062 case OAL_EVENT_GATTS_MTU_CHANGED: {
2063 BT_INFO("OAL Event: GATT Server MTU changed event callback");
2064 __bt_handle_gatt_mtu_changed_event((event_gatts_mtu_changed_t *)event_data);
2067 #ifdef TIZEN_GATT_CLIENT
2068 case OAL_EVENT_GATTC_REGISTRATION: {
2069 BT_INFO("OAL Event: GATT Client instance Registered");
2070 __bt_handle_client_instance_registered((event_gattc_register_t *) event_data);
2073 case OAL_EVENT_GATTC_CONNECTION_COMPLETED: {
2074 BT_INFO("OAL Event: GATT Client Connected");
2075 __bt_handle_client_connected((event_gattc_conn_t *) event_data);
2078 case OAL_EVENT_GATTC_DISCONNECTION_COMPLETED: {
2079 BT_INFO("OAL Event: GATT Client DisConnected");
2080 __bt_handle_client_disconnected((event_gattc_conn_t *) event_data);
2083 case OAL_EVENT_GATTC_SERVICE_SEARCH_RESULT: {
2084 BT_INFO("OAL Event: GATT Client Service Search Result");
2085 __bt_handle_client_service_search_result((event_gattc_service_result_t *) event_data);
2088 case OAL_EVENT_GATTC_SERVICE_SEARCH_DONE: {
2089 BT_INFO("OAL Event: GATT Client Service Completed");
2090 __bt_handle_client_service_search_completed((event_gattc_conn_status_t *) event_data);
2093 case OAL_EVENT_GATTC_CHARAC_SERACH_RESULT: {
2094 BT_INFO("OAL Event: GATT Client Characteristic Search Result");
2095 __bt_handle_client_characteristic_search_result((event_gattc_characteristic_result_t *) event_data);
2098 case OAL_EVENT_GATTC_DESC_SERACH_RESULT: {
2099 BT_INFO("OAL Event: GATT Client Descriptor Search Result");
2100 __bt_handle_client_descriptor_search_result((event_gattc_descriptor_result_t *) event_data);
2103 case OAL_EVENT_GATTC_READ_CHARAC: {
2104 BT_INFO("OAL Event: GATT Client Characteristic Read Data");
2105 __bt_handle_client_characteristic_read_data((event_gattc_read_data *) event_data);
2108 case OAL_EVENT_GATTC_READ_DESCR: {
2109 BT_INFO("OAL Event: GATT Client Descriptor Read Data");
2110 __bt_handle_client_descriptor_read_data((event_gattc_read_data *) event_data);
2113 case OAL_EVENT_GATTC_WRITE_CHARAC: {
2114 BT_INFO("OAL Event: GATT Client Characteristic Write Data");
2115 __bt_handle_client_characteristic_write_data((event_gattc_write_data *) event_data);
2118 case OAL_EVENT_GATTC_WRITE_DESCR: {
2119 BT_INFO("OAL Event: GATT Client Descriptor Write Data");
2120 __bt_handle_client_descriptor_write_data((event_gattc_write_data *) event_data);
2123 case OAL_EVENT_DEVICE_LE_DISCONNECTED: {
2124 BT_INFO("OAL Event: LE device disconnected");
2125 __bt_hanlde_le_device_disconnection((event_dev_conn_status_t *)event_data);
2128 case OAL_EVENT_GATTC_NOTIFICATION_REGISTERED: {
2129 BT_INFO("OAL Event: GATT Client Notification Registered");
2130 __bt_handle_client_notification_registered((event_gattc_regdereg_notify_t *) event_data, TRUE);
2133 case OAL_EVENT_GATTC_NOTIFICATION_DEREGISTERED: {
2134 BT_INFO("OAL Event: GATT Client Notification Registered");
2135 __bt_handle_client_notification_registered((event_gattc_regdereg_notify_t *) event_data, FALSE);
2138 case OAL_EVENT_GATTC_NOTIFY_DATA: {
2139 BT_INFO("OAL Event: GATT Client Notification Data");
2140 __bt_handle_client_notification_data((event_gattc_notify_data *) event_data);
2150 int _bt_gatt_server_add_service(char *sender, int service_type,
2151 int num_handles, char *svc_uuid, int instance_id)
2153 BT_CHECK_PARAMETER(svc_uuid, return);
2154 BT_CHECK_PARAMETER(sender, return);
2155 int ret = OAL_STATUS_SUCCESS;
2157 oal_gatt_srvc_id_t svc_data;
2159 svc_data.is_prmry = service_type;
2160 svc_data.id.inst_id = instance_id;
2162 BT_INFO("Service UUID [%s] Num handles [%d] Instance ID [%d]", svc_uuid, num_handles, instance_id);
2163 _bt_string_to_uuid(svc_uuid, (service_uuid_t*)&svc_data.id.uuid);
2165 ret = gatts_add_service(instance_id, &svc_data, num_handles);
2166 if (ret != OAL_STATUS_SUCCESS) {
2167 BT_ERR("ret: %d", ret);
2168 return BLUETOOTH_ERROR_INTERNAL;
2171 return BLUETOOTH_ERROR_NONE;
2175 int _bt_gatt_server_add_included_service(char *sender, int instance_id,
2176 int service_handle, int included_svc_handle)
2178 BT_CHECK_PARAMETER(sender, return);
2179 int ret = OAL_STATUS_SUCCESS;
2181 ret = gatts_add_included_services(instance_id, service_handle, included_svc_handle);
2182 if (ret != OAL_STATUS_SUCCESS) {
2183 BT_ERR("ret: %d", ret);
2184 return BLUETOOTH_ERROR_INTERNAL;
2186 return BLUETOOTH_ERROR_NONE;
2189 int _bt_gatt_server_add_characteristic(char *sender, char *char_uuid,
2190 bluetooth_gatt_server_attribute_params_t *param)
2192 BT_CHECK_PARAMETER(char_uuid, return);
2193 BT_CHECK_PARAMETER(sender, return);
2194 BT_CHECK_PARAMETER(param, return);
2195 int ret = OAL_STATUS_SUCCESS;
2197 oal_uuid_t uuid = {{0} };
2199 BT_INFO("Char UUID [%s] Instance ID [%d]", char_uuid, param->instance_id);
2200 _bt_string_to_uuid(char_uuid, (service_uuid_t*)&uuid);
2202 BT_INFO("Char permission From API [0x%x]", param->permissions);
2204 ret = gatts_add_characteristics(param->instance_id, param->service_handle, &uuid,
2205 param->properties, (int)param->permissions);
2206 if (ret != OAL_STATUS_SUCCESS) {
2207 BT_ERR("ret: %d", ret);
2208 return BLUETOOTH_ERROR_INTERNAL;
2210 return BLUETOOTH_ERROR_NONE;
2213 int _bt_gatt_server_add_descriptor(char *sender, char *desc_uuid,
2214 bt_gatt_permission_t *param, int service_handle, int instance_id)
2216 BT_CHECK_PARAMETER(desc_uuid, return);
2217 BT_CHECK_PARAMETER(sender, return);
2218 BT_CHECK_PARAMETER(param, return);
2219 int ret = OAL_STATUS_SUCCESS;
2221 oal_uuid_t uuid = {{0} };
2223 BT_INFO("Descriptor UUID [%s] Instance ID [%d] Service handle [%d]",
2224 desc_uuid, service_handle, instance_id);
2226 _bt_string_to_uuid(desc_uuid, (service_uuid_t*)&uuid);
2228 BT_INFO("Descriptor permission From API [0x%x]", *param);
2229 ret = gatts_add_descriptor(instance_id, service_handle, &uuid, (int)*param);
2231 if (ret != OAL_STATUS_SUCCESS) {
2232 BT_ERR("ret: %d", ret);
2233 return BLUETOOTH_ERROR_INTERNAL;
2235 return BLUETOOTH_ERROR_NONE;
2238 int _bt_gatt_server_start_service(char *sender, int service_handle, int instance_id)
2240 BT_CHECK_PARAMETER(sender, return);
2241 int ret = OAL_STATUS_SUCCESS;
2243 ret = gatts_start_service(instance_id, service_handle, BT_GATT_TRANSPORT_LE);
2244 if (ret != OAL_STATUS_SUCCESS) {
2245 BT_ERR("ret: %d", ret);
2246 return BLUETOOTH_ERROR_INTERNAL;
2248 return BLUETOOTH_ERROR_NONE;
2251 int _bt_gatt_server_stop_service(char *sender, int service_handle, int instance_id)
2253 BT_CHECK_PARAMETER(sender, return);
2254 int ret = OAL_STATUS_SUCCESS;
2256 ret = gatts_stop_service(instance_id, service_handle);
2257 if (ret != OAL_STATUS_SUCCESS) {
2258 BT_ERR("ret: %d", ret);
2259 return BLUETOOTH_ERROR_INTERNAL;
2261 return BLUETOOTH_ERROR_NONE;
2264 int _bt_gatt_server_delete_service(char *sender, int service_handle, int instance_id)
2266 BT_CHECK_PARAMETER(sender, return);
2267 int ret = OAL_STATUS_SUCCESS;
2271 ret = gatts_delete_service(instance_id, service_handle);
2272 if (ret != OAL_STATUS_SUCCESS) {
2273 BT_ERR("ret: %d", ret);
2274 return BLUETOOTH_ERROR_INTERNAL;
2277 /* Remove the Service Handle */
2278 for (l = numapps[instance_id].service_handles; l != NULL;) {
2280 l = g_slist_next(l);
2281 if (handle && *handle == service_handle) {
2282 BT_INFO("Remove Service handle [%d]", *handle);
2283 numapps[instance_id].service_handles = g_slist_remove(numapps[instance_id].service_handles, handle);
2289 return BLUETOOTH_ERROR_NONE;
2292 int _bt_gatt_server_send_response(char *sender, bluetooth_gatt_att_data_t *data,
2293 bluetooth_gatt_server_response_params_t *param)
2295 BT_CHECK_PARAMETER(sender, return);
2296 BT_CHECK_PARAMETER(data, return);
2297 BT_CHECK_PARAMETER(param, return);
2298 struct gatt_server_req_info *req_info = NULL;
2299 int ret = OAL_STATUS_SUCCESS;
2301 oal_gatt_response_t response;
2303 BT_INFO("GATT Server Response: Req Type [%d] req_id [%d] status [%d] auth_req [%d] offset[%d] data len[%d]",
2304 param->req_type, param->request_id,
2305 param->response_status, param->auth_req,
2306 data->offset, data->length);
2308 /* Search for matching Request in List */
2309 req_info = __bt_gatt_server_find_request_info(param->request_id, param->req_type);
2311 BT_ERR("GATT Server Req Info not found for current response..return Error");
2312 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
2315 memset(&response, 0x00, sizeof(oal_gatt_response_t));
2317 response.handle = req_info->attribute_handle;
2318 response.attr_value.auth_req = param->auth_req;
2319 response.attr_value.handle = req_info->attribute_handle;
2320 response.attr_value.offset = data->offset;
2321 response.attr_value.len = data->length;
2322 memcpy(&response.attr_value.value, &data->data, data->length);
2325 ret = gatts_send_response(req_info->connection_id, param->request_id,
2326 param->response_status, &response);
2328 if (ret != OAL_STATUS_SUCCESS) {
2329 BT_ERR("ret: %d", ret);
2330 return BLUETOOTH_ERROR_INTERNAL;
2333 BT_INFO("GATT Server Response successfully sent");
2334 /* Remove GATT server request from list */
2335 gatt_server_requests = g_slist_remove(gatt_server_requests, req_info);
2336 g_free(req_info->addr);
2338 return BLUETOOTH_ERROR_NONE;
2341 int _bt_gatt_server_acquire_send_response(char *sender, bluetooth_gatt_server_acquire_response_params_t *param , void *fd_list)
2343 BT_CHECK_PARAMETER(sender, return);
2344 BT_CHECK_PARAMETER(param, return);
2345 struct gatt_server_req_info *req_info = NULL;
2346 int ret = OAL_STATUS_SUCCESS;
2349 BT_INFO("GATT acquire Server Response: Req Type [%d] req_id [%d] fd [%d] mtu[%d]",
2350 param->req_type, param->request_id,
2354 /* Search for matching Request in List */
2355 req_info = __bt_gatt_server_find_request_info(param->request_id, param->req_type);
2357 BT_ERR("GATT acquire Server Req Info not found for current response..return Error");
2358 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
2361 ret = gatt_send_response_acquire(req_info->connection_id, param->request_id, 0, param->fd, param->mtu, fd_list);
2363 if (ret != OAL_STATUS_SUCCESS) {
2364 BT_ERR("ret: %d", ret);
2365 return BLUETOOTH_ERROR_INTERNAL;
2368 BT_INFO("GATT acquire Server Response successfully sent");
2369 /* Remove GATT server request from list */
2370 gatt_server_requests = g_slist_remove(gatt_server_requests, req_info);
2371 g_free(req_info->addr);
2373 return BLUETOOTH_ERROR_NONE;
2378 int _bt_gatt_server_send_indication(char *sender, bluetooth_device_address_t *dev_addr,
2379 bluetooth_gatt_att_data_t *data,
2380 bluetooth_gatt_server_indication_params_t *param)
2382 BT_CHECK_PARAMETER(sender, return);
2383 BT_CHECK_PARAMETER(data, return);
2384 BT_CHECK_PARAMETER(param, return);
2386 gboolean all_send = FALSE;
2387 int ret = OAL_STATUS_SUCCESS;
2388 struct gatt_client_info_t *conn;
2390 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2391 _bt_convert_addr_type_to_string(address, dev_addr->addr);
2393 if (memcmp(dev_addr->addr, BDADDR_ANY, 6) == 0) {
2394 BT_INFO("GATT Server: Send Indication to all connected GATT clients..");
2397 BT_INFO("GATT Server: Send Indication to connected GATT client addr [%s]", address);
2400 /* Attempt to send Notification/Indication to all Connected GATT clients */
2402 ret = __bt_gatt_send_indication_to_all_connected_clients(data, param);
2403 if (ret != OAL_STATUS_SUCCESS) {
2404 BT_ERR("ret: %d", ret);
2406 return BLUETOOTH_ERROR_INTERNAL;
2410 conn = __bt_find_remote_gatt_client_info(address);
2412 ret = gatts_send_indication(param->instance_id, param->atrribute_handle,
2413 conn->connection_id, data->length,
2414 param->need_confirmation, (char *)(&data->data[0]));
2416 if (ret != OAL_STATUS_SUCCESS) {
2417 BT_ERR("ret: %d", ret);
2418 BT_INFO("Indication failed to send to Remote GATT Client [%s]", address);
2420 return BLUETOOTH_ERROR_INTERNAL;
2422 BT_INFO("Indication sent to Remote GATT Client [%s] wait for Notification completed event from OAL", address);
2424 num_indicate_clients = 1;
2425 return BLUETOOTH_ERROR_NONE;
2427 BT_ERR("Remote GATT client [%s] is not connected..Cant send Indication!!", address);
2429 return BLUETOOTH_ERROR_NOT_CONNECTED;
2432 return BLUETOOTH_ERROR_NONE;
2435 int _bt_gatt_server_update_attribute_value(char *sender, int instance_id,
2436 bluetooth_gatt_server_update_value_t *param)
2438 BT_CHECK_PARAMETER(sender, return);
2439 BT_CHECK_PARAMETER(param, return);
2440 int ret = OAL_STATUS_SUCCESS;
2442 oal_gatt_value_t value;
2443 BT_INFO("GATT Server Update value: Instance ID [%d] attr handle [%d] Value len [%d]",
2444 instance_id, param->attribute_handle, param->length);
2447 memset(&value, 0x00, sizeof(oal_gatt_value_t));
2449 value.handle = param->attribute_handle;
2450 value.len = param->length;
2451 memcpy(&value.value, ¶m->data.data, param->length);
2453 ret = gatts_update_att_value(instance_id, &value);
2455 if (ret != OAL_STATUS_SUCCESS) {
2456 BT_ERR("ret: %d", ret);
2457 return BLUETOOTH_ERROR_INTERNAL;
2460 BT_INFO("GATT Server Update Attribute Value successfully");
2461 return BLUETOOTH_ERROR_NONE;
2464 int _bt_get_att_mtu(bluetooth_device_address_t *address,
2467 BT_CHECK_PARAMETER(address, return);
2468 BT_CHECK_PARAMETER(mtu, return);
2469 struct gatt_client_info_t *conn_info = NULL;
2470 char addr[BT_ADDRESS_STRING_SIZE] = { 0 };
2471 int ret = OAL_STATUS_SUCCESS;
2474 _bt_convert_addr_type_to_string(addr, address->addr);
2476 BT_INFO("Get current MTU size for the remote client:DevAddress:[%s]", addr);
2478 conn_info = __bt_find_remote_gatt_client_info(addr);
2480 BT_INFO("GATT Client [%s] is connected, conn Id [%d] Instance ID [%d]",
2481 conn_info->addr, conn_info->connection_id, conn_info->instance_id);
2483 BT_ERR("GATT Client [%s] is not yet connected..", addr);
2484 return BLUETOOTH_ERROR_NOT_CONNECTED;
2487 ret = gatts_get_att_mtu(conn_info->connection_id, &stack_mtu);
2488 if (ret != OAL_STATUS_SUCCESS) {
2489 BT_ERR("ret: %d", ret);
2490 return BLUETOOTH_ERROR_INTERNAL;
2493 BT_INFO("ATT MTU received from OAL [%d]", stack_mtu);
2494 *mtu = (unsigned int)stack_mtu;
2497 BT_ERR("MTU value is zero, GATT Client [%s] is not yet connected..", addr);
2498 return BLUETOOTH_ERROR_NOT_CONNECTED;
2501 return BLUETOOTH_ERROR_NONE;
2504 #ifdef TIZEN_GATT_CLIENT
2505 /* GATT Client utility static functions */
2506 static bt_gatt_service_info_list_t * __bt_get_service_info_list(int conn_id)
2509 bt_gatt_service_info_list_t *info = NULL;
2511 for (l = list_gatt_info; l != NULL; l = g_slist_next(l)) {
2512 info = (bt_gatt_service_info_list_t*)l->data;
2516 if (info->conn_id == conn_id)
2523 static bt_gatt_service_info_t* __bt_find_matching_service(
2524 bt_gatt_service_info_list_t *svc_list, oal_gatt_srvc_id_t *svc)
2527 bt_gatt_service_info_t *info = NULL;
2529 for (l = svc_list->services; l != NULL; l = g_slist_next(l)) {
2530 info = (bt_gatt_service_info_t*)l->data;
2534 /* Match UUID and instance ID */
2535 if (!memcmp(&svc->id.uuid.uuid, &info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
2536 && (svc->id.inst_id == info->inst_id)) {
2543 static bt_gatt_char_info_t* __bt_find_matching_charc(
2544 bt_gatt_service_info_t *svc_info, oal_gatt_id_t *charc)
2547 bt_gatt_char_info_t *info = NULL;
2549 for (l = svc_info->chars; l != NULL; l = g_slist_next(l)) {
2550 info = (bt_gatt_char_info_t*)l->data;
2554 /* Match UUID and instance ID */
2555 if (!memcmp(&charc->uuid.uuid, &info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
2556 && (charc->inst_id == info->inst_id)) {
2563 static bt_gatt_descriptor_info_t* __bt_find_matching_desc(
2564 bt_gatt_char_info_t *char_info, oal_gatt_id_t *desc)
2567 bt_gatt_descriptor_info_t *info = NULL;
2569 for (l = char_info->descs; l != NULL; l = g_slist_next(l)) {
2570 info = (bt_gatt_descriptor_info_t*)l->data;
2574 /* Match UUID and instance ID */
2575 if (!memcmp(&desc->uuid, &info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
2576 && (desc->inst_id == info->inst_id)) {
2584 static struct gatt_server_info_t *__bt_find_remote_gatt_server_info_from_conn_id(int conn_id)
2587 struct gatt_server_info_t *info = NULL;
2589 for (l = gatt_server_info_list; l != NULL; l = g_slist_next(l)) {
2590 info = (struct gatt_server_info_t*)l->data;
2594 if (info->connection_id == conn_id) {
2595 BT_INFO("Remote GATT server found addr[%s]", info->addr);
2602 static bt_gatt_service_info_t* __bt_find_removed_service(bt_gatt_service_info_list_t *svc_list)
2605 bt_gatt_service_info_t *info = NULL;
2607 for (l = svc_list->services; l != NULL; l = g_slist_next(l)) {
2608 info = (bt_gatt_service_info_t*)l->data;
2612 /* Service is marked a removed */
2613 if (info->is_removed == 1)
2619 static void __bt_remove_service_info_from_list(bt_gatt_service_info_t *svc_info)
2625 bt_gatt_char_info_t *charc = NULL;
2626 bt_gatt_included_service_info_t *incl = NULL;
2627 bt_gatt_descriptor_info_t *desc = NULL;
2629 /* Remove all Characteristic and Descriptors within characteristic */
2630 for (l = svc_info->chars; l != NULL;) {
2631 charc = (bt_gatt_char_info_t*)l->data;
2632 l = g_slist_next(l); /* Incase if l is removed, saving next to l */
2637 /* Inside Characteristic */
2638 for (l1 = charc->descs; l1 != NULL;) {
2640 desc = (bt_gatt_descriptor_info_t*)l1->data;
2641 l1 = g_slist_next(l1);
2646 /* Remove Descriptor */
2647 charc->descs = g_slist_remove(charc->descs, desc);
2650 /* Remove Characteristic */
2651 svc_info->chars = g_slist_remove(svc_info->chars, charc);
2655 /* Remove all Included Services */
2656 for (l2 = svc_info->included_svcs; l2 != NULL;) {
2657 incl = (bt_gatt_included_service_info_t*)l2->data;
2658 l2 = g_slist_next(l2); /* Incase if l is removed, saving next to l */
2663 /* Remove included service */
2664 svc_info->included_svcs = g_slist_remove(svc_info->included_svcs, incl);
2671 static void __bt_build_service_browse_info(int conn_id,
2672 bt_services_browse_info_t* info)
2675 bt_gatt_service_info_list_t *svc_info_list;
2676 bt_gatt_service_info_t *svc_info;
2678 service_uuid_t uuid;
2679 struct gatt_server_info_t *conn_info = NULL;
2681 char uuid_string[BLUETOOTH_UUID_STRING_MAX];
2684 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
2689 _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
2691 svc_info_list = __bt_get_service_info_list(conn_id);
2696 info->count = g_slist_length(svc_info_list->services);
2697 BT_INFO("Total services present in the svc info list for this conn id [%d] is [%d]",
2698 conn_id, info->count);
2700 for (l = svc_info_list->services; l != NULL; l = g_slist_next(l)) {
2701 svc_info = (bt_gatt_service_info_t*)l->data;
2702 if (svc_info == NULL)
2705 memcpy(&uuid.uuid, &svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2706 _bt_uuid_to_string(&uuid, uuid_string);
2708 BT_INFO("Service UUID formed [%s] strlen [%zd]", uuid_string, strlen(uuid_string));
2710 /* Fill UUID of service */
2711 g_strlcpy(info->uuids[count], uuid_string,
2712 BLUETOOTH_UUID_STRING_MAX);
2714 BT_INFO("Service UUID formed TO be sent [%s] strlen [%zd]",
2715 info->uuids[count], strlen(info->uuids[count]));
2716 /* Fill instance ID of service */
2717 info->inst_id[count] = svc_info->inst_id;
2719 /* Fill primary service or not info */
2720 info->primary[count] = svc_info->is_primary;
2722 /* Increment count of services browsed */
2726 BT_INFO("Total services browsed [%d]", count);
2729 static void __bt_build_char_browse_info(int conn_id,
2730 bt_gatt_service_info_t *svc_info,
2731 bt_char_browse_info_t* info)
2734 bt_gatt_char_info_t *char_info;
2735 service_uuid_t uuid;
2737 struct gatt_server_info_t *conn_info = NULL;
2739 char uuid_string[BLUETOOTH_UUID_STRING_MAX];
2743 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
2747 /* Fill default data, this will be required even in case of failure */
2748 _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
2749 memcpy(&info->svc_uuid, svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2750 info->svc_inst_id = svc_info->inst_id;
2752 if (!svc_info->chars) {
2753 BT_ERR("No Chars browsed for address [%s]", conn_info->addr);
2757 info->count = g_slist_length(svc_info->chars);
2758 BT_INFO("Total count of Characteristics [%d]", info->count);
2760 for (l = svc_info->chars; l != NULL; l = g_slist_next(l)) {
2761 char_info = (bt_gatt_char_info_t*)l->data;
2762 if (char_info == NULL)
2765 memcpy(&uuid.uuid, &char_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2766 _bt_uuid_to_string(&uuid, uuid_string);
2768 /* Fill UUID of characteristic */
2769 g_strlcpy(info->uuids[count], uuid_string,
2770 BLUETOOTH_UUID_STRING_MAX);
2772 /* Fill instance ID of characteristic */
2773 info->inst_id[count] = char_info->inst_id;
2775 /* Fill property of characteristic */
2776 info->props[count] = char_info->props;
2778 /* Increment count of services browsed */
2781 BT_INFO("Total characteristics browsed [%d]", count);
2784 static void __bt_build_descriptor_browse_info(int conn_id,
2785 bt_gatt_service_info_t *svc_info,
2786 bt_gatt_char_info_t *char_info,
2787 bt_descriptor_browse_info_t* info)
2790 bt_gatt_descriptor_info_t *desc_info;
2792 service_uuid_t uuid;
2793 struct gatt_server_info_t *conn_info = NULL;
2795 char uuid_string[BLUETOOTH_UUID_STRING_MAX];
2797 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
2799 /* Fill default data, this will be required even in case of failure */
2800 _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
2801 memcpy(&info->svc_uuid, svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2802 info->svc_inst_id = svc_info->inst_id;
2803 memcpy(&info->char_uuid, char_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2804 info->char_inst_id = char_info->inst_id;
2806 /* Fill property of the parent characteristic of this descriptor */
2807 info->char_props_map = char_info->props;
2809 info->count = g_slist_length(char_info->descs);
2810 BT_INFO("Total count of Descriptors [%d]", info->count);
2812 if (!char_info->descs) {
2813 BT_ERR("No Descriptors browsed for address [%s]", conn_info->addr);
2817 for (l = char_info->descs; l != NULL; l = g_slist_next(l)) {
2818 desc_info = (bt_gatt_descriptor_info_t*)l->data;
2819 if (desc_info == NULL)
2822 memcpy(&uuid.uuid, &desc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2823 _bt_uuid_to_string(&uuid, uuid_string);
2825 /* Fill UUID of Descriptor */
2826 g_strlcpy(info->uuids[count], uuid_string,
2827 BLUETOOTH_UUID_STRING_MAX);
2829 /* Fill instance ID of Descriptor */
2830 info->inst_id[count] = desc_info->inst_id;
2833 /* Increment count of Descriptor browsed */
2837 BT_INFO("Total descriptors browsed [%d]", count);
2841 static void __bt_cleanup_remote_services(struct gatt_server_info_t *conn_info)
2848 bt_gatt_service_info_list_t * svc_info_list = NULL;
2849 bt_gatt_service_info_t *svc = NULL;
2850 bt_gatt_char_info_t *chr = NULL;
2851 bt_gatt_descriptor_info_t *desc = NULL;
2852 bt_gatt_included_service_info_t *incl_svc = NULL;
2854 BT_INFO("Start Cleanup of all services");
2856 svc_info_list = __bt_get_service_info_list(conn_info->connection_id);
2857 if (!svc_info_list) {
2858 BT_INFO("Could not find Svc Info list for the connection ID [%d]",
2859 conn_info->connection_id);
2863 BT_INFO("Num Services [%d]", g_slist_length(svc_info_list->services));
2864 for (l = svc_info_list->services; l;) {
2865 svc = (bt_gatt_service_info_t*)l->data;
2866 l = g_slist_next(l);
2870 BT_INFO("Service info Is Prim[%d] Inst ID [%d]",
2871 svc->is_primary, svc->inst_id);
2872 BT_INFO("Num chars [%d]", g_slist_length(svc->chars));
2873 /* Delete all chars and its descriptors */
2874 for (ll = svc->chars; ll;) {
2875 chr = (bt_gatt_char_info_t*)ll->data;
2876 ll = g_slist_next(ll);
2880 BT_INFO("Num descs [%d]", g_slist_length(chr->descs));
2881 for (lll = chr->descs; lll;) {
2882 desc = (bt_gatt_descriptor_info_t *)lll->data;
2883 lll = g_slist_next(lll);
2886 chr->descs = g_slist_remove(chr->descs, desc);
2889 svc->chars = g_slist_remove(svc->chars, chr);
2893 BT_INFO("Num incl svcs [%d]", g_slist_length(svc->included_svcs));
2894 /* Delete all included services */
2895 for (llll = svc->included_svcs; llll;) {
2896 incl_svc = (bt_gatt_included_service_info_t*)llll->data;
2897 llll = g_slist_next(llll);
2898 if (incl_svc == NULL)
2901 svc->included_svcs = g_slist_remove(svc->included_svcs, incl_svc);
2904 svc_info_list->services = g_slist_remove(svc_info_list->services, svc);
2908 list_gatt_info = g_slist_remove(list_gatt_info, svc_info_list);
2909 g_free(svc_info_list);
2910 BT_INFO("Cleanup of all services done");
2913 int _bt_register_gatt_client_instance(const char *sender,
2914 bluetooth_device_address_t *address)
2916 int ret = OAL_STATUS_SUCCESS;
2917 char *uuid_string = NULL;
2922 /* App should ensure that it should not send */
2923 BT_INFO("###Check on which instance GATT Client instance can be initialized....");
2924 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
2925 if (numapps[k].is_initialized == 1) {
2926 BT_INFO("Instance ID [%d] is already in use..Check next slot",
2927 numapps[k].instance_id);
2930 BT_INFO("Time to register GATT client instancer..UUID to be used is [%s] slot [%d]",
2931 uuid_list[slot-1], slot);
2937 BT_ERR("No Slot if free for GATT Client registration..");
2938 return BLUETOOTH_ERROR_REGISTRATION_FAILED;
2941 uuid_string = g_malloc0(BT_UUID_STRING_MAX);
2942 _bt_string_to_uuid(uuid_list[slot-1], (service_uuid_t*)&uuid);
2943 g_strlcpy(uuid_string, uuid_list[slot-1], BT_UUID_STRING_MAX);
2944 BT_INFO("Copied UUID string [%s]", uuid_string);
2946 /* Register GATT Client */
2947 ret = gattc_register(&uuid);
2948 if (ret != OAL_STATUS_SUCCESS) {
2949 BT_ERR("ret: %d", ret);
2950 g_free(uuid_string);
2951 return BLUETOOTH_ERROR_INTERNAL;
2954 BT_INFO("GATT Client registration call successfully accepted by OAL..wait for Instance Initialized event from OAL..");
2956 /* Return & wait for GATT Client Instance Initialization event */
2957 memset(numapps[slot].sender, 0x00, sizeof(numapps[slot].sender));
2958 memset(numapps[slot].uuid, 0x00, sizeof(numapps[slot].uuid));
2960 g_strlcpy(numapps[slot].sender, sender, sizeof(numapps[slot].sender));
2961 g_strlcpy(numapps[slot].uuid, uuid_string, sizeof(numapps[slot].uuid));
2963 /* Address is saved here. When event comes, sender + address are matched for replying pending
2964 request. It is impossible for same sender to have requests with two same addresses */
2965 memcpy(&numapps[slot].address.addr, address->addr, sizeof(bluetooth_device_address_t));
2967 BT_INFO("Slot [%d] occupied", slot);
2968 numapps[slot].is_initialized = TRUE; /* Set initialization to true here itself */
2970 g_free(uuid_string);
2971 return BLUETOOTH_ERROR_NONE;
2977 /* GATT client events */
2978 static void __bt_handle_client_instance_registered(event_gattc_register_t *data)
2980 bt_service_app_info_t *info = NULL;
2982 char *uuid_string = g_malloc0(BT_UUID_STRING_MAX);
2984 _bt_uuid_to_string(&(data->client_uuid), uuid_string);
2985 BT_INFO("CLient ID is Initialized [%d] UUID initialized [%s]", data->client_if, uuid_string);
2987 /* Platform GATT client framwork does not use Default GATT client instance
2988 This GATT client instance is never deregistred in the lifetime of bt-service */
2989 BT_INFO("Default UUID [%s] current registered uuid [%s]",
2990 DEFAULT_GATT_CLIENT_UUID, uuid_string);
2991 if (g_strcmp0(uuid_string, DEFAULT_GATT_CLIENT_UUID) == 0) {
2992 BT_INFO("Default client Instance Registered [%s] Client instance [%d]",
2993 uuid_string, data->client_if);
2994 gatt_default_client = data->client_if;
2995 g_free(uuid_string);
2999 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
3002 if (g_strcmp0(info->uuid, uuid_string) == 0) {
3003 BT_INFO("Found GATT client.. UUID [%s], sender [%s]", info->uuid, info->sender);
3004 BT_INFO("Slot [%d] occupied", k);
3005 info->is_initialized = TRUE;
3006 info->client_id = data->client_if;
3007 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE, BT_GATT_CLIENT_REGISTER,
3008 (void*)info, sizeof(bt_service_app_info_t));
3012 g_free(uuid_string);
3015 static void __bt_handle_client_connected(event_gattc_conn_t *event_data)
3017 int result = BLUETOOTH_ERROR_NONE;
3018 struct gatt_server_info_t *conn_info = NULL;
3019 struct gatt_out_conn_info_t *out_conn_info = NULL;
3021 GVariant *param = NULL;
3023 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
3024 _bt_convert_addr_type_to_string(address,
3025 (unsigned char *)event_data->address.addr);
3027 if (event_data->status != OAL_STATUS_SUCCESS)
3028 result = BLUETOOTH_ERROR_INTERNAL;
3030 /* DBUS Return fo BT_CONNECT_LE for all the apps */
3031 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE, address,
3032 BT_ADDRESS_STRING_SIZE);
3034 BT_INFO("Local GATT Client Connected: Remote addr[%s] Client Interface [%d] Connection ID [%d] status[%d]",
3035 address, event_data->client_if, event_data->conn_id, event_data->status);
3037 if (result == BLUETOOTH_ERROR_NONE) {
3038 /* Check if device is already in connected list */
3039 conn_info = __bt_find_remote_gatt_server_info(address);
3042 /* Send event to BT-API */
3043 param = g_variant_new("(is)", result, address);
3044 _bt_send_event(BT_DEVICE_EVENT,
3045 BLUETOOTH_EVENT_GATT_CLIENT_CONNECTED, /* Local device is GATT client */
3048 /* Save Connection info */
3049 conn_info = g_new0(struct gatt_server_info_t, 1);
3050 conn_info->addr = g_strdup(address);
3051 conn_info->client_id = event_data->client_if;
3052 BT_INFO("Added GATT server addr[%s]", conn_info->addr);
3053 conn_info->connection_id = event_data->conn_id;
3054 gatt_server_info_list = g_slist_append(gatt_server_info_list, conn_info);
3055 BT_INFO("Total num of connected Remote GATT server devices [%d]",
3056 g_slist_length(gatt_server_info_list));
3059 BT_INFO("Do a Internal refresh");
3060 if (OAL_STATUS_SUCCESS != gattc_refresh(conn_info->client_id, &event_data->address))
3061 BT_ERR("GATT database refresh failed!!");
3063 BT_INFO("GATT database refresh Success!!");
3066 BT_ERR("Local GATT Client connected event for addr[%s], but device is in connected list already", address);
3068 BT_ERR("GATT Client Connection failed!!");
3070 /* If outgoing connection Info is present, then remove it */
3071 out_conn_info = __bt_find_gatt_outgoing_conn_info(address);
3072 if (out_conn_info) {
3073 BT_ERR("Outgoing Client connect request was sent");
3074 outgoing_gatt_conn_list = g_slist_remove(outgoing_gatt_conn_list, out_conn_info);
3075 g_free(out_conn_info->addr);
3076 g_free(out_conn_info);
3081 static void __bt_handle_client_disconnected(event_gattc_conn_t *event_data)
3083 int result = BLUETOOTH_ERROR_NONE;
3085 struct gatt_server_info_t *conn_info = NULL;
3086 struct gatt_out_conn_info_t *out_conn_info = NULL;
3088 GVariant *param = NULL;
3090 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
3091 _bt_convert_addr_type_to_string(address,
3092 (unsigned char *)event_data->address.addr);
3094 if (event_data->status != OAL_STATUS_SUCCESS)
3095 result = BLUETOOTH_ERROR_INTERNAL;
3097 if (NULL == _bt_get_request_info_data(BT_DISCONNECT_LE, address)) {
3098 if (NULL != _bt_get_request_info_data(BT_CONNECT_LE, address)) {
3099 result = BLUETOOTH_ERROR_INTERNAL;
3100 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
3101 address, BT_ADDRESS_STRING_SIZE);
3102 BT_ERR("Failed to connect Local GATT Remote addr[%s]", address);
3107 /* DBUS Return for BT_DISCONNECT_LE for all the apps */
3108 __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE, address,
3109 BT_ADDRESS_STRING_SIZE);
3111 BT_INFO("Local GATT Client DisConnected: Remote addr[%s] Client Interface [%d] Connection ID [%d] status [%d]",
3112 address, event_data->client_if, event_data->conn_id, event_data->status);
3114 /* Remove Connection info */
3115 conn_info = __bt_find_remote_gatt_server_info(address);
3118 param = g_variant_new("(is)", result, address);
3119 /* Send event to application */
3120 _bt_send_event(BT_DEVICE_EVENT,
3121 BLUETOOTH_EVENT_GATT_CLIENT_DISCONNECTED,
3124 BT_INFO("Remove GATT server info from List..");
3125 /* Remove all services from info list_gatt_info */
3126 __bt_cleanup_remote_services(conn_info);
3128 /* Remove info from List */
3129 gatt_server_info_list = g_slist_remove(gatt_server_info_list, conn_info);
3130 BT_INFO("Total num of connected GATT servers [%d]", g_slist_length(gatt_server_info_list));
3131 g_free(conn_info->addr);
3134 BT_INFO("Can not find conn info, already removed!");
3136 /* If outgoing connection Info is present, then remove it */
3137 out_conn_info = __bt_find_gatt_outgoing_conn_info(address);
3138 if (out_conn_info) {
3139 BT_ERR("Client Disconnected event, but outgoing connect request was sent");
3140 outgoing_gatt_conn_list = g_slist_remove(outgoing_gatt_conn_list, out_conn_info);
3141 g_free(out_conn_info->addr);
3142 g_free(out_conn_info);
3148 static void __bt_handle_client_service_search_result(
3149 event_gattc_service_result_t *event_data)
3152 BT_INFO("received the gatt service search result");
3153 /* Pre: status is never fail from OAL */
3155 /* Find service list from address */
3156 bt_gatt_service_info_list_t *svc_info_list;
3157 bt_gatt_service_info_t *svc_info;
3158 BT_INFO("Search Result: status [%d] conn_id [%d]",
3159 event_data->conn_status.status,
3160 event_data->conn_status.conn_id);
3162 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3163 if (!svc_info_list) {
3164 BT_INFO("Service info list not present for this connection ID, means first time browse");
3165 /* Means for this conn_id, no services are ever browsed, first time,
3166 create service info list for this conn_id */
3167 svc_info_list = g_malloc0(sizeof(bt_gatt_service_info_list_t));
3168 svc_info_list->conn_id = event_data->conn_status.conn_id;
3169 list_gatt_info = g_slist_append(list_gatt_info, svc_info_list);
3171 BT_INFO("Service info list Already present for this connection ID, means not first time browse for this conn ID ");
3174 /* send list and current service's uuid and instance id to find it */
3175 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3178 /* If not found, check if service changed, if yes, means this is a new service added
3179 in remote GATT device, update uuid info in svc info list structure, to be used when
3180 search is completed */
3182 BT_INFO("Service Not found in svc info list for this connection ID");
3183 if (svc_info_list->info.is_changed) {
3184 BT_INFO("Service Changed indication already found for this connection ID");
3185 memcpy(svc_info_list->info.uuid, event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3187 /* Create and add new service in service list */
3188 svc_info = g_malloc0(sizeof(bt_gatt_service_info_t));
3189 memcpy(svc_info->uuid, event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3190 svc_info->inst_id = event_data->srvc_id.id.inst_id;
3191 svc_info->is_primary = event_data->srvc_id.is_prmry;
3192 svc_info_list->services = g_slist_append(svc_info_list->services, svc_info);
3193 BT_INFO("Service created and added in Svc info listf or this connection ID");
3195 BT_INFO("Service Already found to be present inside the svc info list for this connection ID");
3196 /* If returned matching service info, then just update service_rmeoved value inside it to 0 */
3197 svc_info->is_removed = 0;
3201 static void __bt_handle_client_service_search_completed(
3202 event_gattc_conn_status_t *event_data)
3205 struct gatt_server_info_t *conn_info = NULL;
3206 bt_gatt_service_info_list_t *svc_info_list;
3207 bt_gatt_service_info_t *svc_info;
3208 bt_services_browse_info_t browse_info;
3209 unsigned char uuid_empty[BLUETOOTH_UUID_HEX_MAX_LEN];
3211 memset(&uuid_empty, 0x00, BLUETOOTH_UUID_HEX_MAX_LEN);
3212 memset(&browse_info, 0x00, sizeof(bt_services_browse_info_t));
3213 BT_INFO("Primary Services browsing completed status[%d] conn ID [%d]",
3214 event_data->status, event_data->conn_id);
3216 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(event_data->conn_id);
3218 svc_info_list = __bt_get_service_info_list(event_data->conn_id);
3219 if (!svc_info_list) {
3220 BT_ERR("No services browsed ever for addr [%s]", conn_info->addr);
3222 /* Just build response and return ERROR */
3223 __bt_build_service_browse_info(event_data->conn_id, &browse_info);
3225 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_INTERNAL,
3226 BT_GATT_GET_PRIMARY_SERVICES, &browse_info,
3227 sizeof(bt_services_browse_info_t));
3231 /* If fail, then send event with error */
3232 if (event_data->status != OAL_STATUS_SUCCESS) {
3233 /* Just build response and return ERROR */
3234 __bt_build_service_browse_info(event_data->conn_id, &browse_info);
3236 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_INTERNAL,
3237 BT_GATT_GET_PRIMARY_SERVICES, &browse_info,
3238 sizeof(bt_services_browse_info_t));
3242 /* If success, then find service info list from address */
3244 /* If svc_changed == 1 and uuid valid, means a new service is added*/
3245 if (svc_info_list->info.is_changed && !memcmp(uuid_empty, svc_info_list->info.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)) {
3246 /* TODO: Send event -Service added with instance ID and UUID of newly added service */
3247 BT_INFO("new service added");
3249 BT_INFO("TODO new service added");
3252 /* If svc_changed == 1 and uuid invalid, then a service is removed */
3253 if (svc_info_list->info.is_changed && memcmp(uuid_empty, svc_info_list->info.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)) {
3254 /* Scan through the service info list to find service with is_removed = 1*/
3255 svc_info = __bt_find_removed_service(svc_info_list);
3257 /* TODO Send event - Service removed with instance ID and UUID of just rmeoved service */
3259 /* Remove that service info from service info list */
3260 svc_info_list->services = g_slist_remove(svc_info_list->services, svc_info);
3262 /* Delete that service completely from svc_info list*/
3263 __bt_remove_service_info_from_list(svc_info);
3267 /* Reset svc_changed = 0, and reset UUID = all 0's */
3268 svc_info_list->info.is_changed = 0;
3269 memset(&svc_info_list->info.uuid, 0x00, BLUETOOTH_UUID_HEX_MAX_LEN);
3271 /* Build Reply and send to service browse primary services request of pending apps */
3272 __bt_build_service_browse_info(event_data->conn_id, &browse_info);
3274 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE,
3275 BT_GATT_GET_PRIMARY_SERVICES, &browse_info,
3276 sizeof(bt_services_browse_info_t));
3280 static void __bt_handle_client_characteristic_search_result(
3281 event_gattc_characteristic_result_t *event_data)
3283 bt_gatt_service_info_list_t *svc_info_list;
3284 bt_gatt_service_info_t *svc_info;
3285 bt_gatt_char_info_t *char_info;
3286 bt_char_browse_info_t browse_info;
3288 BT_INFO("Characteristic search result status [%d]",
3289 event_data->conn_status.status);
3291 memset(&browse_info, 0x00, sizeof(bt_char_browse_info_t));
3294 if (event_data->conn_status.status == OAL_STATUS_SUCCESS) {
3295 /* Find service info list from address */
3296 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3297 if (svc_info_list == NULL) {
3298 BT_ERR("svc_info_list is NULL");
3303 /* Find matching service info from svc info list */
3304 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3305 if (svc_info == NULL) {
3306 BT_ERR("svc_info is NULL");
3310 /* Find Matching char from service info in event */
3311 char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
3312 if (char_info == NULL)
3313 BT_ERR("char_info is NULL");
3315 /* If not found, then add new characteristic and return */
3317 BT_INFO(" add new characteristic");
3318 char_info = g_malloc0(sizeof(bt_gatt_char_info_t));
3319 memcpy(char_info->uuid, event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3321 char_info->inst_id = event_data->char_id.inst_id;
3322 char_info->props = event_data->char_prop;
3323 svc_info->chars = g_slist_append(svc_info->chars, char_info);
3325 /* If found, then return */
3326 BT_INFO("update char property as Characteristic browsed is already present");
3327 char_info->props |= event_data->char_prop;
3330 /* If Not success: Means Charc browse is completed */
3331 /* Find char list from service in event */
3332 /* Find service list from address */
3333 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3335 /* Find service info from service in event */
3336 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3338 /* Build char list from service in event */
3339 __bt_build_char_browse_info(event_data->conn_status.conn_id,
3340 svc_info, &browse_info);
3342 /* Create response and return by sending event*/
3343 /* Build Reply and send to service browse All Included services request of pending apps */
3344 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE,
3345 BT_GATT_GET_SERVICE_PROPERTIES,
3347 sizeof(bt_char_browse_info_t));
3351 static void __bt_handle_client_descriptor_search_result(
3352 event_gattc_descriptor_result_t *event_data)
3354 bt_gatt_service_info_list_t *svc_info_list;
3355 bt_gatt_service_info_t *svc_info;
3356 bt_gatt_char_info_t *char_info;
3357 bt_gatt_descriptor_info_t *desc_info;
3358 bt_descriptor_browse_info_t browse_info;
3360 BT_INFO("descriptor search result status [%d]", event_data->conn_status.status);
3362 memset(&browse_info, 0x00, sizeof(bt_descriptor_browse_info_t));
3365 if (event_data->conn_status.status == OAL_STATUS_SUCCESS) {
3366 /* Find service list from address */
3367 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3368 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3369 char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
3370 desc_info = __bt_find_matching_desc(char_info, &event_data->descr_id);
3372 /* If not found, add new descriptor and return */
3374 desc_info = g_malloc0(sizeof(bt_gatt_descriptor_info_t));
3375 memcpy(desc_info->uuid, event_data->descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3377 desc_info->inst_id = event_data->descr_id.inst_id;
3378 char_info->descs = g_slist_append(char_info->descs, desc_info);
3381 /* If found, then return */
3382 BT_INFO("Characteristic browsed is already presesnt");
3385 /* If Not success */
3386 /* Find service list from address */
3387 /* Find included service list from service in event */
3388 /* Create response and return by sending event*/
3389 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3391 /* Find service info from service in event */
3392 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3394 /* Find char info from char in event */
3395 char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
3397 /* Build descriptor list from char in event */
3398 __bt_build_descriptor_browse_info(event_data->conn_status.conn_id,
3399 svc_info, char_info, &browse_info);
3402 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE,
3403 BT_GATT_GET_CHARACTERISTIC_PROPERTIES,
3405 sizeof(bt_descriptor_browse_info_t));
3409 static void __bt_handle_client_characteristic_read_data(
3410 event_gattc_read_data *event_data)
3412 int result = BLUETOOTH_ERROR_NONE;
3413 struct gatt_server_info_t *conn_info = NULL;
3414 bluetooth_gatt_client_char_prop_info_t read_info;
3416 /* Read Information data structures */
3417 GVariant *param = NULL;
3418 GVariant *data = NULL;
3419 GVariant *data_svc_uuid = NULL;
3420 GVariant *data_char_uuid = NULL;
3421 char *read_val = NULL;
3422 char *svc_uuid = NULL;
3423 char *char_uuid = NULL;
3429 //memset(&read_info, 0x00, sizeof(bt_gatt_handle_property_t));
3430 memset(&read_info, 0x00, sizeof(bluetooth_gatt_client_char_prop_info_t));
3432 /* Extract Address from conn_id of event data */
3433 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3434 event_data->uuid_status.conn_status.conn_id);
3436 BT_INFO("Characteristic Read result from addr [%s] status [%d]",
3437 conn_info->addr, event_data->uuid_status.conn_status.status);
3439 /* Fill char in buffer */
3440 memcpy(&read_info.characteristic.uuid,
3441 event_data->uuid_status.char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3442 read_info.characteristic.instance_id = event_data->uuid_status.char_id.inst_id;
3444 /* Fill Service in buffer */
3445 memcpy(&read_info.svc.uuid,
3446 event_data->uuid_status.srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3447 read_info.svc.instance_id = event_data->uuid_status.srvc_id.id.inst_id;
3449 /* Fill remote device address */
3450 _bt_convert_addr_string_to_type(read_info.device_address.addr, conn_info->addr);
3452 /* Fill data and reply to all apps waiting for Read result on the same characteristic
3453 Note: Even in case of failure, address, handles and result code should be returned */
3454 if (event_data->uuid_status.conn_status.status != OAL_STATUS_SUCCESS)
3455 result = BLUETOOTH_ERROR_INTERNAL;
3457 BT_ERR("read data len is [%d]", event_data->data_len);
3458 if (event_data->data_len > 0) {
3460 for (i = 0; i < event_data->data_len; i++)
3461 BT_INFO("Data[%d] = [0x%x]", i, event_data->data[i]);
3464 read_val = g_memdup(&event_data->data[0], event_data->data_len);
3466 data = g_variant_new_from_data(
3467 G_VARIANT_TYPE_BYTESTRING,
3469 event_data->data_len,
3472 BT_ERR("Characteristic Read success, but no data!!!");
3474 data = g_variant_new_from_data(
3475 G_VARIANT_TYPE_BYTESTRING,
3482 svc_uuid = g_memdup(&event_data->uuid_status.srvc_id.id.uuid.uuid[0], uuid_len);
3484 data_svc_uuid = g_variant_new_from_data(
3485 G_VARIANT_TYPE_BYTESTRING,
3491 char_uuid = g_memdup(&event_data->uuid_status.char_id.uuid.uuid[0], uuid_len);
3493 data_char_uuid = g_variant_new_from_data(
3494 G_VARIANT_TYPE_BYTESTRING,
3499 param = g_variant_new("(isn@ayin@ayin@ay)", result,
3503 event_data->uuid_status.srvc_id.id.inst_id,
3506 event_data->uuid_status.char_id.inst_id,
3507 event_data->data_len,
3511 _bt_send_event(BT_GATT_CLIENT_EVENT,
3512 BLUETOOTH_EVENT_GATT_READ_CHAR,
3515 /* Send DBUS return */
3516 __bt_gatt_handle_pending_request_info(result,
3517 BT_GATT_READ_CHARACTERISTIC,
3519 sizeof(bluetooth_gatt_client_char_prop_info_t));
3530 static void __bt_handle_client_descriptor_read_data(
3531 event_gattc_read_data *event_data)
3533 int result = BLUETOOTH_ERROR_NONE;
3534 struct gatt_server_info_t *conn_info = NULL;
3535 bluetooth_gatt_client_desc_prop_info_t read_info;
3537 /* Read Information data structures */
3538 GVariant *param = NULL;
3539 GVariant *data = NULL;
3540 GVariant *data_svc_uuid = NULL;
3541 GVariant *data_char_uuid = NULL;
3542 GVariant *data_desc_uuid = NULL;
3543 char *read_val = NULL;
3544 char *svc_uuid = NULL;
3545 char *char_uuid = NULL;
3546 char *desc_uuid = NULL;
3551 memset(&read_info, 0x00, sizeof(bluetooth_gatt_client_desc_prop_info_t));
3553 /* Extract Address from conn_id of event data */
3554 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3555 event_data->uuid_status.conn_status.conn_id);
3557 BT_INFO("Descriptor Read result from addr [%s] status [%d]",
3558 conn_info->addr, event_data->uuid_status.conn_status.status);
3560 /* Fill descriptor informations in buffer */
3561 memcpy(&read_info.descriptor.uuid,
3562 event_data->uuid_status.descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3563 read_info.descriptor.instance_id = event_data->uuid_status.descr_id.inst_id;
3565 /* Fill Characteristic informations in buffer */
3566 memcpy(&read_info.characteristic.uuid,
3567 event_data->uuid_status.char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3568 read_info.characteristic.instance_id = event_data->uuid_status.char_id.inst_id;
3570 /* Fill Service informations in buffer */
3571 memcpy(&read_info.svc.uuid,
3572 event_data->uuid_status.srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3573 read_info.svc.instance_id = event_data->uuid_status.srvc_id.id.inst_id;
3575 /* Fill remote device address */
3576 _bt_convert_addr_string_to_type(read_info.device_address.addr, conn_info->addr);
3578 /* Fill data and reply to all apps waiting for Read result on the same characteristic */
3579 if (event_data->uuid_status.conn_status.status != OAL_STATUS_SUCCESS)
3580 result = BLUETOOTH_ERROR_INTERNAL;
3582 BT_INFO("desc data len:%d", event_data->data_len);
3583 if (event_data->data_len > 0) {
3585 for (i = 0; i < event_data->data_len; i++)
3586 BT_DBG("Data[%d] = [0x%x]", i, event_data->data[i]);
3589 read_val = g_memdup(&event_data->data[0], event_data->data_len);
3591 data = g_variant_new_from_data(
3592 G_VARIANT_TYPE_BYTESTRING,
3594 event_data->data_len,
3597 BT_INFO("Descriptor Read success, but no data!!!");
3599 data = g_variant_new_from_data(
3600 G_VARIANT_TYPE_BYTESTRING,
3606 svc_uuid = g_memdup(&event_data->uuid_status.srvc_id.id.uuid.uuid[0], uuid_len);
3608 data_svc_uuid = g_variant_new_from_data(
3609 G_VARIANT_TYPE_BYTESTRING,
3615 char_uuid = g_memdup(&event_data->uuid_status.char_id.uuid.uuid[0], uuid_len);
3617 data_char_uuid = g_variant_new_from_data(
3618 G_VARIANT_TYPE_BYTESTRING,
3624 desc_uuid = g_memdup(&event_data->uuid_status.descr_id.uuid.uuid[0], uuid_len);
3626 data_desc_uuid = g_variant_new_from_data(
3627 G_VARIANT_TYPE_BYTESTRING,
3632 param = g_variant_new("(isn@ayin@ayin@ayin@ay)", result,
3636 event_data->uuid_status.srvc_id.id.inst_id,
3639 event_data->uuid_status.char_id.inst_id,
3642 event_data->uuid_status.descr_id.inst_id,
3643 event_data->data_len,
3647 _bt_send_event(BT_GATT_CLIENT_EVENT,
3648 BLUETOOTH_EVENT_GATT_READ_DESC,
3651 BT_INFO("Send DBUS rpely for GATT Read Descriptor");
3652 /* Send DBUS return */
3653 __bt_gatt_handle_pending_request_info(result,
3654 BT_GATT_READ_DESCRIPTOR_VALUE,
3656 sizeof(bluetooth_gatt_client_desc_prop_info_t));
3667 static void __bt_handle_client_characteristic_write_data(
3668 event_gattc_write_data *event_data)
3670 int result = BLUETOOTH_ERROR_NONE;
3671 struct gatt_server_info_t *conn_info = NULL;
3672 bluetooth_gatt_client_char_prop_info_t write_info;
3674 /* Read Information data structures */
3675 GVariant *param = NULL;
3676 GVariant *data_svc_uuid = NULL;
3677 GVariant *data_char_uuid = NULL;
3678 char *svc_uuid = NULL;
3679 char *char_uuid = NULL;
3683 memset(&write_info, 0x00, sizeof(bluetooth_gatt_client_char_prop_info_t));
3685 /* Extract Address from conn_id of event data */
3686 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3687 event_data->conn_status.conn_id);
3689 BT_INFO("Characteristic Write callback from addr [%s] status [%d]",
3690 conn_info->addr, event_data->conn_status.status);
3692 /* Fill char in buffer */
3693 memcpy(&write_info.characteristic.uuid,
3694 event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3695 write_info.characteristic.instance_id = event_data->char_id.inst_id;
3697 /* Fill Service in buffer */
3698 memcpy(&write_info.svc.uuid,
3699 event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3700 write_info.svc.instance_id = event_data->srvc_id.id.inst_id;
3702 /* Fill remote device address */
3703 _bt_convert_addr_string_to_type(write_info.device_address.addr, conn_info->addr);
3705 if (event_data->conn_status.status != OAL_STATUS_SUCCESS) {
3706 result = BLUETOOTH_ERROR_INTERNAL;
3712 svc_uuid = g_memdup(&event_data->srvc_id.id.uuid.uuid[0], uuid_len);
3714 data_svc_uuid = g_variant_new_from_data(
3715 G_VARIANT_TYPE_BYTESTRING,
3721 char_uuid = g_memdup(&event_data->char_id.uuid.uuid[0], uuid_len);
3723 data_char_uuid = g_variant_new_from_data(
3724 G_VARIANT_TYPE_BYTESTRING,
3729 param = g_variant_new("(isn@ayin@ayi)", result,
3733 event_data->srvc_id.id.inst_id,
3736 event_data->char_id.inst_id);
3739 _bt_send_event(BT_GATT_CLIENT_EVENT,
3740 BLUETOOTH_EVENT_GATT_WRITE_CHAR,
3749 /* Send DBUS return */
3750 __bt_gatt_handle_pending_request_info(result,
3751 BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE,
3753 sizeof(bluetooth_gatt_client_char_prop_info_t));
3757 static void __bt_handle_client_descriptor_write_data(
3758 event_gattc_write_data *event_data)
3760 int result = BLUETOOTH_ERROR_NONE;
3761 struct gatt_server_info_t *conn_info = NULL;
3762 bluetooth_gatt_client_desc_prop_info_t write_info;
3764 /* Write Information data structures */
3765 GVariant *param = NULL;
3766 GVariant *data_svc_uuid = NULL;
3767 GVariant *data_char_uuid = NULL;
3768 GVariant *data_desc_uuid = NULL;
3769 char *svc_uuid = NULL;
3770 char *char_uuid = NULL;
3771 char *desc_uuid = NULL;
3775 memset(&write_info, 0x00, sizeof(bluetooth_gatt_client_desc_prop_info_t));
3777 /* Extract Address from conn_id of event data */
3778 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3779 event_data->conn_status.conn_id);
3781 if (NULL == conn_info) {
3783 BT_INFO("Failed to get the conn info for conn_id [%d]", event_data->conn_status.conn_id);
3787 BT_INFO("Descriptor Write callback from addr [%s] status [%d]",
3788 conn_info->addr, event_data->conn_status.status);
3790 /* Fill descriptor informations in buffer */
3791 memcpy(&write_info.descriptor.uuid,
3792 event_data->descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3793 write_info.descriptor.instance_id = event_data->descr_id.inst_id;
3795 /* Fill Characteristic informations in buffer */
3796 memcpy(&write_info.characteristic.uuid,
3797 event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3798 write_info.characteristic.instance_id = event_data->char_id.inst_id;
3800 /* Fill Service informations in buffer */
3801 memcpy(&write_info.svc.uuid,
3802 event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3803 write_info.svc.instance_id = event_data->srvc_id.id.inst_id;
3805 /* Fill remote device address */
3806 _bt_convert_addr_string_to_type(write_info.device_address.addr, conn_info->addr);
3808 if (event_data->conn_status.status != OAL_STATUS_SUCCESS) {
3809 result = BLUETOOTH_ERROR_INTERNAL;
3815 svc_uuid = g_memdup(&event_data->srvc_id.id.uuid.uuid[0], uuid_len);
3817 data_svc_uuid = g_variant_new_from_data(
3818 G_VARIANT_TYPE_BYTESTRING,
3824 char_uuid = g_memdup(&event_data->char_id.uuid.uuid[0], uuid_len);
3826 data_char_uuid = g_variant_new_from_data(
3827 G_VARIANT_TYPE_BYTESTRING,
3833 desc_uuid = g_memdup(&event_data->descr_id.uuid.uuid[0], uuid_len);
3835 data_desc_uuid = g_variant_new_from_data(
3836 G_VARIANT_TYPE_BYTESTRING,
3841 param = g_variant_new("(isn@ayin@ayin@ayi)", result,
3845 event_data->srvc_id.id.inst_id,
3848 event_data->char_id.inst_id,
3851 event_data->descr_id.inst_id);
3854 _bt_send_event(BT_GATT_CLIENT_EVENT,
3855 BLUETOOTH_EVENT_GATT_WRITE_DESC,
3866 /* Send DBUS return */
3867 __bt_gatt_handle_pending_request_info(result,
3868 BT_GATT_WRITE_DESCRIPTOR_VALUE,
3870 sizeof(bluetooth_gatt_client_desc_prop_info_t));
3873 static void __bt_hanlde_le_device_disconnection(event_dev_conn_status_t *event_data)
3875 int result = BLUETOOTH_ERROR_INTERNAL;
3876 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
3878 _bt_convert_addr_type_to_string(address, (unsigned char *)event_data->address.addr);
3880 /* DBUS Return with fail of pending BT_CONNECT_LE for all the apps */
3881 BT_INFO("Local GATT Client disconnected: Remote addr[%s] ", address);
3883 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE, address,
3884 BT_ADDRESS_STRING_SIZE);
3888 static void __bt_handle_client_notification_registered(
3889 event_gattc_regdereg_notify_t *event_data,
3890 gboolean is_registered)
3892 int result = BLUETOOTH_ERROR_NONE;
3893 struct gatt_server_info_t *conn_info = NULL;
3894 bt_gatt_notif_reg_info_t notif_info;
3897 memset(¬if_info, 0x00, sizeof(bt_gatt_notif_reg_info_t));
3899 BT_INFO("Client Interface [%d] status [%d]",
3900 event_data->client_if,
3901 event_data->status);
3903 /* Extract Address from conn_id of event data */
3904 conn_info = __bt_find_remote_gatt_server_info_from_client_if(
3905 event_data->client_if);
3908 BT_INFO("Connection Info is not present, return");
3911 BT_INFO("Notification Registered for addr [%s]",
3915 /* Fill svc informations in buffer */
3916 memcpy(¬if_info.svc_uuid,
3917 event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3918 notif_info.svc_inst = event_data->srvc_id.id.inst_id;
3920 /* Fill char in buffer */
3921 memcpy(¬if_info.char_uuid,
3922 event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3923 notif_info.char_inst = event_data->char_id.inst_id;
3925 /* Fill remote device address */
3926 _bt_convert_addr_string_to_type(notif_info.addr.addr, conn_info->addr);
3928 notif_info.is_registered = is_registered;
3930 if (event_data->status != OAL_STATUS_SUCCESS)
3931 result = BLUETOOTH_ERROR_INTERNAL;
3933 /* Send DBUS Return for BT_GATT_WATCH_CHARACTERISTIC */
3934 __bt_gatt_handle_pending_request_info(result,
3935 BT_GATT_WATCH_CHARACTERISTIC,
3937 sizeof(bt_gatt_notif_reg_info_t));
3940 static void __bt_handle_client_notification_data(event_gattc_notify_data *event_data)
3942 /* No status in this event from OAL */
3943 int result = BLUETOOTH_ERROR_NONE;
3945 /* Read Information data structures */
3946 GVariant *param = NULL;
3947 GVariant *data = NULL;
3948 GVariant *data_svc_uuid = NULL;
3949 GVariant *data_char_uuid = NULL;
3950 char *read_val = NULL;
3951 char *svc_uuid = NULL;
3952 char *char_uuid = NULL;
3958 BT_INFO("Notifcation of charc data changed");
3960 if (event_data->data_len > 0) {
3962 for (i = 0; i < event_data->data_len; i++)
3963 BT_INFO("Data[%d] = [0x%x]", i, event_data->data[i]);
3966 addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
3967 _bt_convert_addr_type_to_string(addr,
3968 (unsigned char *)&(event_data->address.addr));
3971 read_val = g_memdup(&event_data->data[0], event_data->data_len);
3973 data = g_variant_new_from_data(
3974 G_VARIANT_TYPE_BYTESTRING,
3976 event_data->data_len,
3979 svc_uuid = g_memdup(&event_data->srvc_id.id.uuid.uuid[0], uuid_len);
3981 data_svc_uuid = g_variant_new_from_data(
3982 G_VARIANT_TYPE_BYTESTRING,
3988 char_uuid = g_memdup(&event_data->char_id.uuid.uuid[0], uuid_len);
3990 data_char_uuid = g_variant_new_from_data(
3991 G_VARIANT_TYPE_BYTESTRING,
3997 param = g_variant_new("(isn@ayin@ayin@ay)", result,
4001 event_data->srvc_id.id.inst_id,
4004 event_data->char_id.inst_id,
4005 event_data->data_len,
4009 _bt_send_event(BT_GATT_CLIENT_EVENT,
4010 BLUETOOTH_EVENT_GATT_CHAR_VAL_CHANGED,
4013 BT_ERR("No Data!!");
4026 gboolean _bt_is_remote_gatt_device_connected(bluetooth_device_address_t *address)
4029 struct gatt_server_info_t *conn_info = NULL;
4030 gboolean connected = FALSE;
4033 addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
4034 _bt_convert_addr_type_to_string(addr,
4035 (unsigned char *)&(address->addr));
4037 BT_INFO("Check Connected or not for [%s]", addr);
4038 /* Check if device is already in connected list */
4039 conn_info = __bt_find_remote_gatt_server_info(addr);
4042 BT_INFO("Remote GATT Server device [%s] is Connected", conn_info->addr);
4045 BT_INFO("Remote GATT Server Device [%s] is not Connected", addr);
4051 int _bt_connect_le_device(bluetooth_device_address_t *address,
4052 int auto_connect, int client_id)
4054 struct gatt_server_info_t *conn_info = NULL;
4055 struct gatt_out_conn_info_t *out_conn_info = NULL;
4057 invocation_info_t *req_info = NULL;
4058 int ret = OAL_STATUS_SUCCESS;
4060 char *remote_address = NULL;
4062 BT_CHECK_PARAMETER(address, return);
4066 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4067 _bt_convert_addr_type_to_string(addr, address->addr);
4068 BT_INFO("GATT Client connect request for address [%s] client instance [%d]",
4072 /* Check if Remote Device is already under connection progress */
4073 req_info = _bt_get_request_info_data_from_function_name(BT_CONNECT_LE);
4075 remote_address = (char*)req_info->user_data;
4076 if (remote_address && !strcasecmp(remote_address, addr)) {/* Address matched */
4077 BT_INFO("Already Connection ongoing for same remote GATT Server address [%s]", remote_address);
4078 /* Return and wait for events to be sent to all apps */
4080 return BLUETOOTH_ERROR_IN_PROGRESS;
4084 /* Check if remote GATT Server is connected or not */
4085 conn_info = __bt_find_remote_gatt_server_info(addr);
4087 BT_ERR("GATT Server is already connected..");
4089 return BLUETOOTH_ERROR_ALREADY_CONNECT;
4092 /* TODO Check Requirement of holding Advertisement before initiating LE connect */
4094 /* Check if app sent 0 client id for connection, in such case, use default gatt client ID */
4095 if (client_id == 0) {
4096 BT_INFO("GATT CLient connect request sent by an app without any client instance [%d]",
4098 BT_INFO("Assign default GATT client id [%d]", gatt_default_client);
4099 client_id = gatt_default_client;
4102 BT_INFO("Connect using CLient ID [%d]", client_id);
4103 ret = gattc_connect(client_id, (bt_address_t*)(address), auto_connect);
4105 if (ret != OAL_STATUS_SUCCESS) {
4106 BT_ERR("ret: %d", ret);
4108 return BLUETOOTH_ERROR_INTERNAL;
4111 /* Mark this as outgoing connection */
4112 out_conn_info = g_new0(struct gatt_out_conn_info_t, 1);
4113 out_conn_info->addr = g_strdup(addr);
4114 out_conn_info->client_id = client_id;
4115 BT_INFO("Added outgoing connection info addr[%s]", out_conn_info->addr);
4116 outgoing_gatt_conn_list = g_slist_append(outgoing_gatt_conn_list, out_conn_info);
4119 return BLUETOOTH_ERROR_NONE;
4122 int _bt_gatt_get_primary_services(char *address)
4124 BT_CHECK_PARAMETER(address, return);
4125 struct gatt_server_info_t *conn_info = NULL;
4126 invocation_info_t *req_info = NULL;
4127 int ret = OAL_STATUS_SUCCESS;
4130 /* Check if any app is already browsing primary services on the same remote GATT Server */
4131 req_info = _bt_get_request_info_data(BT_GATT_GET_PRIMARY_SERVICES, address);
4133 BT_INFO("Already Primary Service Browsing ongoing for same rmeote GATT Server");
4134 /* Return and wait for events to be sent to all apps */
4135 return BLUETOOTH_ERROR_NONE;
4138 /* Check if remote GATT Server is connected or not */
4139 conn_info = __bt_find_remote_gatt_server_info(address);
4141 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4142 conn_info->addr, conn_info->connection_id);
4144 BT_ERR("GATT Server is not yet connected..");
4145 return BLUETOOTH_ERROR_NOT_CONNECTED;
4148 /* Send Primary Service Browsing request to stack */
4149 ret = gattc_search_service(conn_info->connection_id, NULL);
4150 if (ret != OAL_STATUS_SUCCESS) {
4151 BT_ERR("ret: %d", ret);
4152 return BLUETOOTH_ERROR_INTERNAL;
4154 return BLUETOOTH_ERROR_NONE;
4157 int _bt_gatt_get_all_characteristic(bluetooth_gatt_client_svc_prop_info_t *svc)
4159 BT_CHECK_PARAMETER(svc, return);
4160 struct gatt_server_info_t *conn_info = NULL;
4161 invocation_info_t *req_info = NULL;
4162 bluetooth_gatt_client_svc_prop_info_t *prop;
4163 oal_gatt_srvc_id_t srvc_id;
4164 int ret = OAL_STATUS_SUCCESS;
4168 /* Check if any app is already browsing characteristics of the same service on the same remote GATT Server */
4169 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_GET_SERVICE_PROPERTIES);
4171 prop = (bluetooth_gatt_client_svc_prop_info_t*)req_info->user_data;
4172 if (prop && !memcmp(svc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t))
4173 && memcmp(prop->svc.uuid, svc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
4174 && prop->svc.instance_id == svc->svc.instance_id) {
4175 BT_INFO("Already Properties browsing for Primary Service ongoing for same remote GATT Server");
4176 /* Return and wait for events to be sent to all apps */
4177 return BLUETOOTH_ERROR_NONE;
4181 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4182 _bt_convert_addr_type_to_string(addr, svc->device_address.addr);
4184 /* Check if remote GATT Server is connected or not */
4185 conn_info = __bt_find_remote_gatt_server_info(addr);
4187 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4188 conn_info->addr, conn_info->connection_id);
4190 BT_ERR("GATT Server is not yet connected..");
4192 return BLUETOOTH_ERROR_NOT_CONNECTED;
4195 srvc_id.is_prmry = TRUE;
4196 srvc_id.id.inst_id = svc->svc.instance_id;
4197 memcpy(srvc_id.id.uuid.uuid, svc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4199 /* Search All Characteristic */
4200 ret = gattc_get_characteristic(conn_info->connection_id, &srvc_id, NULL);
4201 if (ret != OAL_STATUS_SUCCESS) {
4202 BT_ERR("ret: %d", ret);
4204 return BLUETOOTH_ERROR_INTERNAL;
4207 return BLUETOOTH_ERROR_NONE;
4210 int _bt_gatt_get_all_characteristic_properties(
4211 bluetooth_gatt_client_char_prop_info_t *chr)
4213 struct gatt_server_info_t *conn_info = NULL;
4214 invocation_info_t *req_info = NULL;
4215 bluetooth_gatt_client_char_prop_info_t *prop;
4216 oal_gatt_srvc_id_t srvc_id;
4217 oal_gatt_id_t char_id;
4218 int ret = OAL_STATUS_SUCCESS;
4221 BT_CHECK_PARAMETER(chr, return);
4225 /* Check if any app is already browsing descriptors of the same char of
4226 particular service on the same remote GATT Server */
4227 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_GET_CHARACTERISTIC_PROPERTIES);
4229 prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
4230 if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4231 && memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4232 && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4233 && memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4234 && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
4235 BT_INFO("Already Properties browsing for Characteristic ongoing for same remote GATT Server");
4236 /* Return and wait for events to be sent to all apps */
4237 return BLUETOOTH_ERROR_NONE;
4241 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4242 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4244 /* Check if remote GATT Server is connected or not */
4245 conn_info = __bt_find_remote_gatt_server_info(addr);
4247 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4248 conn_info->addr, conn_info->connection_id);
4250 BT_ERR("GATT Server is not yet connected..");
4252 return BLUETOOTH_ERROR_NOT_CONNECTED;
4255 srvc_id.is_prmry = TRUE;
4256 srvc_id.id.inst_id = chr->svc.instance_id;
4257 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4259 char_id.inst_id = chr->characteristic.instance_id;
4260 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4262 /* Search All Descriptors */
4263 ret = gattc_get_descriptor(conn_info->connection_id, &srvc_id, &char_id, NULL);
4264 if (ret != OAL_STATUS_SUCCESS) {
4265 BT_ERR("ret: %d", ret);
4267 return BLUETOOTH_ERROR_INTERNAL;
4270 return BLUETOOTH_ERROR_NONE;
4273 int _bt_gatt_read_characteristic_value(
4274 bluetooth_gatt_client_char_prop_info_t *chr)
4276 struct gatt_server_info_t *conn_info = NULL;
4277 invocation_info_t *req_info = NULL;
4278 bluetooth_gatt_client_char_prop_info_t *prop;
4279 oal_gatt_srvc_id_t srvc_id;
4280 oal_gatt_id_t char_id;
4281 int ret = OAL_STATUS_SUCCESS;
4284 BT_CHECK_PARAMETER(chr, return);
4288 /* Check if any app is already Reading characteristic of the same char of
4289 particular service on the same remote GATT Server */
4290 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_READ_CHARACTERISTIC);
4292 prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
4293 if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4294 && memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4295 && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4296 && memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4297 && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
4298 BT_INFO("Already Characteristic value Read operation in progress for same remote GATT Server");
4299 /* Return and wait for events to be sent to all apps */
4300 return BLUETOOTH_ERROR_NONE;
4304 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4305 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4307 /* Check if remote GATT Server is connected or not */
4308 conn_info = __bt_find_remote_gatt_server_info(addr);
4310 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4311 conn_info->addr, conn_info->connection_id);
4313 BT_ERR("GATT Server is not yet connected..");
4315 return BLUETOOTH_ERROR_NOT_CONNECTED;
4318 srvc_id.is_prmry = TRUE;
4319 srvc_id.id.inst_id = chr->svc.instance_id;
4320 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4322 char_id.inst_id = chr->characteristic.instance_id;
4323 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4325 /* Search All Descriptors */
4326 ret = gattc_read_characteristic(conn_info->connection_id, &srvc_id, &char_id, OAL_GATT_AUTH_REQ_NONE);
4327 if (ret != OAL_STATUS_SUCCESS) {
4328 BT_ERR("ret: %d", ret);
4330 return BLUETOOTH_ERROR_INTERNAL;
4333 return BLUETOOTH_ERROR_NONE;
4336 int _bt_gatt_read_descriptor_value(
4337 bluetooth_gatt_client_desc_prop_info_t *desc)
4339 struct gatt_server_info_t *conn_info = NULL;
4340 invocation_info_t *req_info = NULL;
4341 bluetooth_gatt_client_desc_prop_info_t *prop;
4342 oal_gatt_srvc_id_t srvc_id;
4343 oal_gatt_id_t char_id;
4344 oal_gatt_id_t desc_id;
4345 int ret = OAL_STATUS_SUCCESS;
4348 BT_CHECK_PARAMETER(desc, return);
4352 /* Check if any app is already Reading descriptors of the same char of
4353 particular service on the same remote GATT Server */
4354 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_READ_DESCRIPTOR_VALUE);
4356 prop = (bluetooth_gatt_client_desc_prop_info_t*)req_info->user_data;
4357 if (prop && !memcmp(desc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4358 && memcmp(desc->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4359 && desc->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4360 && memcmp(desc->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4361 && desc->characteristic.instance_id == prop->characteristic.instance_id /* Characteristic Instance ID matched */
4362 && memcmp(desc->descriptor.uuid, prop->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Descriptor UUID matched */
4363 && desc->descriptor.instance_id == prop->descriptor.instance_id) { /* Descriptor Instance ID matched */
4364 BT_INFO("Already Descriptor value Read operation in progress for same remote GATT Server");
4365 /* Return and wait for events to be sent to all apps */
4366 return BLUETOOTH_ERROR_NONE;
4370 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4371 _bt_convert_addr_type_to_string(addr, desc->device_address.addr);
4373 /* Check if remote GATT Server is connected or not */
4374 conn_info = __bt_find_remote_gatt_server_info(addr);
4376 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4377 conn_info->addr, conn_info->connection_id);
4379 BT_ERR("GATT Server is not yet connected..");
4381 return BLUETOOTH_ERROR_NOT_CONNECTED;
4384 srvc_id.is_prmry = TRUE;
4385 srvc_id.id.inst_id = desc->svc.instance_id;
4386 memcpy(srvc_id.id.uuid.uuid, desc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4388 char_id.inst_id = desc->characteristic.instance_id;
4389 memcpy(char_id.uuid.uuid, desc->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4391 desc_id.inst_id = desc->descriptor.instance_id;
4392 memcpy(desc_id.uuid.uuid, desc->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4394 /* Search All Descriptors */
4395 ret = gattc_read_descriptor(conn_info->connection_id, &srvc_id, &char_id,
4396 &desc_id, OAL_GATT_AUTH_REQ_NONE);
4397 if (ret != OAL_STATUS_SUCCESS) {
4398 BT_ERR("ret: %d", ret);
4400 return BLUETOOTH_ERROR_INTERNAL;
4403 return BLUETOOTH_ERROR_NONE;
4407 int _bt_gatt_acquire_notify(bluetooth_gatt_client_char_prop_info_t *chr, int *fd, int *mtu)
4409 struct gatt_server_info_t *conn_info = NULL;
4410 oal_gatt_srvc_id_t srvc_id;
4411 oal_gatt_id_t char_id;
4412 int ret = OAL_STATUS_SUCCESS;
4415 BT_CHECK_PARAMETER(chr, return);
4419 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4420 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4422 /* Check if remote GATT Server is connected or not */
4423 conn_info = __bt_find_remote_gatt_server_info(addr);
4425 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4426 conn_info->addr, conn_info->connection_id);
4428 BT_ERR("GATT Server is not yet connected..");
4430 return BLUETOOTH_ERROR_NOT_CONNECTED;
4433 srvc_id.is_prmry = TRUE;
4434 srvc_id.id.inst_id = chr->svc.instance_id;
4435 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4437 char_id.inst_id = chr->characteristic.instance_id;
4438 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4440 ret = gattc_acquire_notify(conn_info->connection_id, &srvc_id, &char_id, fd, mtu);
4442 if (ret != OAL_STATUS_SUCCESS) {
4443 BT_ERR("ret: %d", ret);
4445 return BLUETOOTH_ERROR_INTERNAL;
4447 BT_INFO("GATT characterstics FD [%d] mtu[%d]", *fd, *mtu);
4449 return BLUETOOTH_ERROR_NONE;
4453 int _bt_gatt_acquire_write(bluetooth_gatt_client_char_prop_info_t *chr, int *fd, int *mtu)
4456 struct gatt_server_info_t *conn_info = NULL;
4457 oal_gatt_srvc_id_t srvc_id;
4458 oal_gatt_id_t char_id;
4459 int ret = OAL_STATUS_SUCCESS;
4462 BT_CHECK_PARAMETER(chr, return);
4466 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4467 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4469 /* Check if remote GATT Server is connected or not */
4470 conn_info = __bt_find_remote_gatt_server_info(addr);
4472 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4473 conn_info->addr, conn_info->connection_id);
4475 BT_ERR("GATT Server is not yet connected..");
4477 return BLUETOOTH_ERROR_NOT_CONNECTED;
4480 srvc_id.is_prmry = TRUE;
4481 srvc_id.id.inst_id = chr->svc.instance_id;
4482 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4484 char_id.inst_id = chr->characteristic.instance_id;
4485 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4487 ret = gattc_acquire_write(conn_info->connection_id, &srvc_id, &char_id,
4488 OAL_GATT_AUTH_REQ_NONE, fd, mtu);
4489 if (ret != OAL_STATUS_SUCCESS) {
4490 BT_ERR("ret: %d", ret);
4492 return BLUETOOTH_ERROR_INTERNAL;
4494 BT_INFO("GATT characterstics FD [%d] mtu [%d]", *fd, *mtu);
4496 return BLUETOOTH_ERROR_NONE;
4501 /* Write Characteristic */
4502 int _bt_gatt_write_characteristic_value_by_type(
4503 bluetooth_gatt_client_char_prop_info_t *chr,
4504 bluetooth_gatt_att_data_t *data,
4505 bluetooth_gatt_write_type_e write_type)
4507 struct gatt_server_info_t *conn_info = NULL;
4508 invocation_info_t *req_info = NULL;
4509 bluetooth_gatt_client_char_prop_info_t *prop;
4510 oal_gatt_srvc_id_t srvc_id;
4511 oal_gatt_id_t char_id;
4512 int ret = OAL_STATUS_SUCCESS;
4516 BT_CHECK_PARAMETER(chr, return);
4517 BT_CHECK_PARAMETER(data, return);
4521 /* Check if any app is already writing same char of
4522 particular service on the same remote GATT Server */
4523 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE);
4525 prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
4526 if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4527 && memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4528 && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4529 && memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4530 && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
4531 BT_INFO("Already Characteristic Write Value operation in progress for same remote GATT Server");
4532 /* Return and wait for events to be sent to all apps */
4533 return BLUETOOTH_ERROR_NONE;
4537 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4538 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4540 /* Check if remote GATT Server is connected or not */
4541 conn_info = __bt_find_remote_gatt_server_info(addr);
4543 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4544 conn_info->addr, conn_info->connection_id);
4546 BT_ERR("GATT Server is not yet connected..");
4548 return BLUETOOTH_ERROR_NOT_CONNECTED;
4551 srvc_id.is_prmry = TRUE;
4552 srvc_id.id.inst_id = chr->svc.instance_id;
4553 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4555 char_id.inst_id = chr->characteristic.instance_id;
4556 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4558 /* Write CHar value */
4560 BT_INFO("Connection ID [%d] write type [%d] data length [%d]", conn_info->connection_id, write_type, data->length);
4561 for (k = 0; k < data->length; k++)
4562 BT_INFO("Data[%d] [0x%x]", k, data->data[k]);
4564 ret = gattc_write_characteristic(conn_info->connection_id,
4566 (oal_gatt_write_type_t)write_type, data->length,
4567 OAL_GATT_AUTH_REQ_NONE, (char *)(&data->data[0]));
4568 if (ret != OAL_STATUS_SUCCESS) {
4569 BT_ERR("ret: %d", ret);
4571 return BLUETOOTH_ERROR_INTERNAL;
4574 return BLUETOOTH_ERROR_NONE;
4577 /* Write Descriptor */
4578 int _bt_gatt_write_descriptor_value_by_type(
4579 bluetooth_gatt_client_desc_prop_info_t *desc,
4580 bluetooth_gatt_att_data_t *data,
4581 bluetooth_gatt_write_type_e write_type)
4583 struct gatt_server_info_t *conn_info = NULL;
4584 invocation_info_t *req_info = NULL;
4585 bluetooth_gatt_client_desc_prop_info_t *prop;
4586 oal_gatt_srvc_id_t srvc_id;
4587 oal_gatt_id_t char_id;
4588 oal_gatt_id_t desc_id;
4589 int ret = OAL_STATUS_SUCCESS;
4593 BT_CHECK_PARAMETER(desc, return);
4594 BT_CHECK_PARAMETER(data, return);
4598 /* Check if any app is already writing on same Descriptor of the same char of
4599 particular service on the same remote GATT Server */
4600 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_WRITE_DESCRIPTOR_VALUE);
4602 prop = (bluetooth_gatt_client_desc_prop_info_t*)req_info->user_data;
4603 if (prop && !memcmp(desc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4604 && memcmp(desc->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4605 && desc->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4606 && memcmp(desc->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4607 && desc->characteristic.instance_id == prop->characteristic.instance_id /* Characteristic Instance ID matched */
4608 && memcmp(desc->descriptor.uuid, prop->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Descriptor UUID matched */
4609 && desc->descriptor.instance_id == prop->descriptor.instance_id) { /* Descriptor Instance ID matched */
4610 BT_INFO("Already Descriptor value Write operation in progress for same remote GATT Server");
4611 /* Return and wait for events to be sent to all apps */
4612 return BLUETOOTH_ERROR_NONE;
4616 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4617 _bt_convert_addr_type_to_string(addr, desc->device_address.addr);
4619 /* Check if remote GATT Server is connected or not */
4620 conn_info = __bt_find_remote_gatt_server_info(addr);
4622 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4623 conn_info->addr, conn_info->connection_id);
4625 BT_ERR("GATT Server is not yet connected..");
4627 return BLUETOOTH_ERROR_NOT_CONNECTED;
4630 srvc_id.is_prmry = TRUE;
4631 srvc_id.id.inst_id = desc->svc.instance_id;
4632 memcpy(srvc_id.id.uuid.uuid, desc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4634 char_id.inst_id = desc->characteristic.instance_id;
4635 memcpy(char_id.uuid.uuid, desc->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4637 desc_id.inst_id = desc->descriptor.instance_id;
4638 memcpy(desc_id.uuid.uuid, desc->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4641 BT_INFO("Connection ID [%d] write type [%d] data length [%d]", conn_info->connection_id, write_type, data->length);
4642 for (k = 0; k < data->length; k++)
4643 BT_INFO("Data[%d] [0x%x]", k, data->data[k]);
4645 ret = gattc_write_descriptor(conn_info->connection_id,
4646 &srvc_id, &char_id, &desc_id,
4647 (oal_gatt_write_type_t)write_type, data->length,
4648 OAL_GATT_AUTH_REQ_NONE, (char *)(&data->data[0]));
4649 if (ret != OAL_STATUS_SUCCESS) {
4650 BT_ERR("ret: %d", ret);
4652 return BLUETOOTH_ERROR_INTERNAL;
4655 return BLUETOOTH_ERROR_NONE;
4658 int _bt_gatt_watch_characteristic(
4659 bluetooth_gatt_client_char_prop_info_t *chr,
4663 struct gatt_server_info_t *conn_info = NULL;
4664 oal_gatt_srvc_id_t srvc_id;
4665 oal_gatt_id_t char_id;
4666 int ret = OAL_STATUS_SUCCESS;
4669 BT_CHECK_PARAMETER(chr, return);
4671 BT_INFO("Client ID [%d] Is Notify [%d]", client_id, is_notify);
4673 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4674 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4676 /* Check if remote GATT Server is connected or not */
4677 conn_info = __bt_find_remote_gatt_server_info(addr);
4679 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4680 conn_info->addr, conn_info->connection_id);
4682 BT_ERR("GATT Server is not yet connected..");
4684 return BLUETOOTH_ERROR_NOT_CONNECTED;
4686 srvc_id.is_prmry = TRUE;
4687 srvc_id.id.inst_id = chr->svc.instance_id;
4688 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4690 char_id.inst_id = chr->characteristic.instance_id;
4691 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4693 /* Register or unregister Notification characteristic */
4695 ret = gattc_register_for_notification(conn_info->client_id,
4696 (bt_address_t*)&(chr->device_address),
4697 &srvc_id, &char_id);
4699 ret = gattc_deregister_for_notification(conn_info->client_id,
4700 (bt_address_t*)&(chr->device_address),
4701 &srvc_id, &char_id);
4703 BT_INFO("Result[%d]", ret);
4704 if (ret != OAL_STATUS_SUCCESS) {
4705 BT_ERR("ret: %d", ret);
4707 return BLUETOOTH_ERROR_INTERNAL;
4710 return BLUETOOTH_ERROR_NONE;
4714 int _bt_disconnect_le_device(bluetooth_device_address_t *address,
4717 struct gatt_server_info_t *conn_info = NULL;
4718 struct gatt_client_info_t *rem_client_conn_info = NULL;
4719 invocation_info_t *req_info = NULL;
4720 int ret = OAL_STATUS_SUCCESS;
4722 char *remote_address = NULL;
4724 BT_CHECK_PARAMETER(address, return);
4728 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4729 _bt_convert_addr_type_to_string(addr, address->addr);
4730 BT_INFO("GATT Client Disconnect request for address [%s]", addr);
4732 /* Check if Remote Device is already under connection progress */
4733 req_info = _bt_get_request_info_data_from_function_name(BT_DISCONNECT_LE);
4735 remote_address = (char*)req_info->user_data;
4736 if (remote_address && !strcasecmp(remote_address, addr)) {/* Address matched */
4737 BT_INFO("Already DisConnection ongoing for same remote GATT Server address [%s]", remote_address);
4738 /* Return success and wait for events to be sent to all apps */
4740 return BLUETOOTH_ERROR_IN_PROGRESS;
4743 /* Check if remote GATT Server is connected or not */
4744 conn_info = __bt_find_remote_gatt_server_info(addr);
4746 /* Check if app sent 0 client id for Disconnection, in such case, use default gatt client ID */
4747 if (client_id == 0) {
4748 BT_INFO("GATT CLient Disconnect request sent by an app without any client instance [%d]",
4750 BT_INFO("Assign default GATT client id [%d]", gatt_default_client);
4751 client_id = gatt_default_client;
4754 BT_INFO("Disconnect remote gatt server using CLient ID [%d] Connection ID [%d]", client_id, conn_info->connection_id);
4755 ret = gattc_disconnect(client_id, (bt_address_t*)(address),
4756 conn_info->connection_id);
4758 /* check if remote client is connected */
4759 rem_client_conn_info = __bt_find_remote_gatt_client_info(addr);
4761 if (!rem_client_conn_info || client_id != 0) {
4762 BT_ERR("GATT device is not connected..");
4764 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
4767 BT_INFO("Disconnect remote gatt client ");
4769 ret = gatts_disconnect(rem_client_conn_info->instance_id,
4770 (bt_address_t*)(address), rem_client_conn_info->connection_id);
4773 if (ret != OAL_STATUS_SUCCESS) {
4774 BT_ERR("ret: %d", ret);
4776 return BLUETOOTH_ERROR_INTERNAL;
4779 return BLUETOOTH_ERROR_NONE;
4782 int _bt_gatt_watch_service_changed_indication(const char *sender,
4783 bluetooth_device_address_t *address,
4784 gboolean is_enabled)
4787 bt_service_app_info_t *info = NULL;
4789 BT_INFO("Enable Servic changed Indication watcher [%d] for app [%s]",
4790 is_enabled, sender);
4792 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
4795 if (g_strcmp0(sender, info->sender) == 0 &&
4796 memcmp(info->address.addr, address->addr,
4797 sizeof(bluetooth_device_address_t)) == 0) {
4798 BT_INFO("Found GATT client App.. UUID [%s], sender [%s]", info->uuid, info->sender);
4799 info->is_watcher_enabled = is_enabled;
4803 return BLUETOOTH_ERROR_NONE;
4806 int _bt_unregister_gatt_client_instance(const char *sender, int client_id)
4808 BT_INFO("Unregister Allocated GATT CLient instance [%s] Client ID [%d]",
4810 int result = BLUETOOTH_ERROR_NONE;
4813 /* Unregister CLient instance associated with address X. It is possible that another app still
4814 has client_id valid for same remote address */
4815 bt_service_app_info_t *info = NULL;
4817 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
4820 /* Exact matching of sender */
4821 if (!g_strcmp0(info->sender, sender) && info->client_id == client_id) { /* Check for only valid GATT client Instance */
4822 BT_INFO("Unregister GATT client instance [%d]", info->client_id);
4823 result = __bt_do_unregister_gatt_instance(info->client_id);
4824 if (result != BLUETOOTH_ERROR_NONE)
4825 BT_ERR("Error in unregistering GATT Client Interface");