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;
110 struct gatt_out_conn_info_t {
111 int client_id; /* This value unique identifies a GATT Client instance */
112 char *addr; /* Remote GATT Server address */
115 /* Linked List of outgoing gatt connection list
116 Note: This is valid only for local GATT client */
117 static GSList *outgoing_gatt_conn_list = NULL;
119 /* GATT Server Info(Local Client) List Structure */
120 struct gatt_server_info_t {
121 int connection_id; /* This value will uniquely identify a GATT client-server connection */
122 int client_id; /* This value unique identifies a GATT Client instance */
123 char *addr; /* Remote GATT Server address */
126 /* Linked List of connected Remote GATT Servers */
127 static GSList *gatt_server_info_list = NULL;
131 unsigned char uuid[BLUETOOTH_UUID_HEX_MAX_LEN]; /* If any service added */
132 } bt_gatt_svc_changed_info_t;
137 // bluetooth_device_address_t address; /* Remote BLE Device Address */
138 GSList *services; /* List of all services of above remote device */
139 int count; /* Number of services browsed from remote device */
140 bt_gatt_svc_changed_info_t info;
141 } bt_gatt_service_info_list_t;
144 unsigned char uuid[BLUETOOTH_UUID_HEX_MAX_LEN];
148 GSList *included_svcs;
149 gboolean is_removed; /* 0 => Service is added, 1=> removed */
150 } bt_gatt_service_info_t;
153 unsigned char uuid[BLUETOOTH_UUID_HEX_MAX_LEN];
157 unsigned char val[BLUETOOTH_GATT_ATT_DATA_LENGTH_MAX];
159 } bt_gatt_char_info_t;
162 unsigned char uuid[BLUETOOTH_UUID_HEX_MAX_LEN];
165 unsigned char val[BLUETOOTH_GATT_ATT_DATA_LENGTH_MAX];
166 } bt_gatt_descriptor_info_t;
169 unsigned char uuid[BLUETOOTH_UUID_HEX_MAX_LEN];
171 } bt_gatt_included_service_info_t;
173 GSList *list_gatt_info;
178 /* App Information structure */
181 char sender[BT_SENDER_MAX_LENGTH];
182 char uuid[BT_UUID_STRING_MAX];
185 bluetooth_advertising_data_t adv_data; /* Will store adv data for specific slot */
187 bluetooth_scan_resp_data_t scan_rsp; /* Will store scan rsp data for specific slot */
189 gboolean is_initialized;
190 GSList *service_handles;
191 #ifdef TIZEN_GATT_CLIENT
192 int client_id; /* GATT Client instance ID */
193 bluetooth_device_address_t address; /* Remote BLE Device Address */
194 gboolean is_watcher_enabled;
196 } bt_service_app_info_t;
198 /* GATT Server Request Info Structure */
199 struct gatt_server_req_info {
200 int connection_id; /* This value will uniquely identify a GATT client-server connection */
201 int request_id; /* This is an unique transaction ID assigned against each request by stack */
202 int attribute_handle; /* GATT server attribute handle */
203 int offset; /* GATT server attribute offset on which request is invoked by GATT client */
204 bluetooth_gatt_att_request_type_e request_type; /* Read or Write request */
205 char *addr; /* Remote GATT client address */
208 /* GATT Client Info List Structure */
209 struct gatt_client_info_t {
210 int connection_id; /* This value will uniquely identify a GATT client-server connection */
211 int instance_id; /* This value unique identifies a GATT server instance */
212 char *addr; /* Remote GATT client address */
215 /* GATT Indicate confirm result */
216 struct gatt_indicate_cfm_result_info_t {
217 int result; /* Result of event */
218 char *addr; /* Remote GATT client address */
219 int att_hdl; /* Characteristic Attribute handle */
220 int completed; /* 1 if last event, otheriwse 0 */
223 /* Request Search Utility method */
224 static struct gatt_server_req_info *__bt_gatt_server_find_request_info(int request_id,
225 bluetooth_gatt_att_request_type_e req_type);
227 static int __bt_gatt_send_indication_to_all_connected_clients(bluetooth_gatt_att_data_t *data,
228 bluetooth_gatt_server_indication_params_t *param);
230 static void __bt_remove_all_service_handles(int instance_id);
232 #ifdef TIZEN_GATT_CLIENT
233 static void __bt_handle_client_instance_registered(event_gattc_register_t *data);
234 static void __bt_handle_client_connected(event_gattc_conn_t *event_data);
235 static void __bt_handle_client_disconnected(event_gattc_conn_t *event_data);
236 static void __bt_handle_client_service_search_completed(event_gattc_conn_status_t *event_data);
237 static void __bt_handle_client_service_search_result(event_gattc_service_result_t *event_data);
238 static void __bt_handle_client_characteristic_search_result(
239 event_gattc_characteristic_result_t *event_data);
240 static void __bt_handle_client_descriptor_search_result(event_gattc_descriptor_result_t *event_data);
241 static void __bt_handle_client_characteristic_read_data(event_gattc_read_data *event_data);
242 static void __bt_handle_client_descriptor_read_data(event_gattc_read_data *event_data);
243 static void __bt_handle_client_characteristic_write_data(event_gattc_write_data *event_data);
244 static void __bt_handle_client_descriptor_write_data(event_gattc_write_data *event_data);
245 static int __bt_do_unregister_gatt_instance(int instance_id);
246 static void __bt_hanlde_le_device_disconnection(event_dev_conn_status_t *event_data);
250 static int __bt_do_unregister_server_instance(int server_instance);
252 /* Linked List of GATT requests from Remote GATT Clients */
253 static GSList *gatt_server_requests = NULL;
255 /* Linked List of connected Remote GATT clients */
256 static GSList *gatt_client_info_list = NULL;
258 /* Number of clients to be notified to */
259 static int num_indicate_clients;
261 /* List of applications */
262 static bt_service_app_info_t numapps[MAX_APPS_SUPPORTED];
264 static void __bt_gatt_handle_pending_request_info(int result,
265 int service_function, void *data, unsigned int size);
267 static void __bt_handle_server_instance_registered(event_gatts_register_t *data);
269 static void __bt_gatt_event_handler(int event_type, gpointer event_data);
271 void _bt_check_adv_app_termination(const char *name)
273 bt_service_app_info_t *app = NULL;
275 int apps[MAX_APPS_SUPPORTED];
277 ret_if(NULL == name);
279 memset(&apps, -1, sizeof(apps));
281 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
284 /* Search for a app which has same sender and stop adv is running */
285 if (!strncasecmp(app->sender, name, strlen(name)) && app->is_initialized == TRUE) {
286 BT_DBG("Match found, name: %s", name);
288 /* Unregister all service handles with stack */
289 __bt_remove_all_service_handles(app->instance_id);
291 /* If Advertising is enabled, stop it */
292 if (app->adv_handle > 0) {
293 BT_INFO("Stop advertising on instance ID [%d]", app->instance_id);
294 /* Disable adv if running */
295 BT_INFO("Disable Advertising Adv Handle [%d] sender [%s]",
296 app->adv_handle, name);
297 _bt_set_advertising(app->sender, app->adv_handle, FALSE, FALSE);
300 /* Save instances of all apps that need to be unregistered */
301 apps[app->instance_id] = 1;
305 /* Unregister all apps one by one */
306 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
308 BT_INFO("Unregister app[%d]", k);
309 /* Unregister server instance */
310 __bt_do_unregister_server_instance(k);
315 static int __bt_gatt_send_indication_to_all_connected_clients(bluetooth_gatt_att_data_t *data,
316 bluetooth_gatt_server_indication_params_t *param)
319 int ret = OAL_STATUS_SUCCESS;
320 int result = OAL_STATUS_INTERNAL_ERROR;
322 BT_INFO("Current total number of connected clients [%d]", g_slist_length(gatt_client_info_list));
323 for (l = gatt_client_info_list; l != NULL; l = l->next) {
324 struct gatt_client_info_t *info = l->data;
327 BT_INFO("GATT Remote client address [%s] connection Id [%d]", info->addr, info->connection_id);
329 ret = gatts_send_indication(param->instance_id, param->atrribute_handle,
330 info->connection_id, data->length,
331 param->need_confirmation, (char *)(&data->data[0]));
333 BT_INFO("Send Indication to GATT client [%s] result: [%d]", info->addr, ret);
334 if (ret == OAL_STATUS_SUCCESS) {
335 BT_INFO("Send Indication sent successfully to GATT client [%s]", info->addr);
337 num_indicate_clients++;
341 BT_INFO("Indication sending done for total number of clients [%d]", num_indicate_clients);
345 static struct gatt_server_req_info *__bt_gatt_server_find_request_info(int request_id,
346 bluetooth_gatt_att_request_type_e req_type)
350 for (l = gatt_server_requests; l != NULL; l = l->next) {
351 struct gatt_server_req_info *req_info = l->data;
353 if (req_info && req_info->request_id == request_id && req_info->request_type == req_type) {
354 BT_INFO("GATT Server request info found Req ID [%d] handle [%d] conn ID [%d]",
355 req_info->request_id, req_info->attribute_handle, req_info->connection_id);
359 BT_ERR("Gatt Request not found");
363 void _bt_get_adv_handle_from_instance(int server_inst, int *adv_handle)
367 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
368 if (numapps[k].is_initialized == 1 && numapps[k].instance_id == server_inst) {
369 *adv_handle = numapps[k].adv_handle;
375 #ifdef TIZEN_GATT_CLIENT
376 char * _bt_gatt_get_default_gatt_client_uuid(void)
378 return g_strdup(DEFAULT_GATT_CLIENT_UUID);
382 static void __bt_register_default_gatt_client()
387 uuid_str = _bt_gatt_get_default_gatt_client_uuid();
388 _bt_string_to_uuid(uuid_str, (service_uuid_t*)&uuid);
390 BT_INFO("Register Default GATT client uuid [%s]", uuid_str);
392 if (OAL_STATUS_SUCCESS != gattc_register(&uuid)) /* for only Smart Control */
393 BT_ERR("gattc register failed");
399 int _bt_gatt_init(void)
403 if (OAL_STATUS_SUCCESS != gatt_enable()) {
404 BT_ERR("gatt Init failed");
405 return BLUETOOTH_ERROR_INTERNAL;
408 /* Register gatt event handler */
409 _bt_service_register_event_handler_callback(BT_GATT_MODULE, __bt_gatt_event_handler);
410 #ifdef TIZEN_GATT_CLIENT
411 /*In bluedroid product, defacult gatt client is handled differently*/
412 __bt_register_default_gatt_client();
415 return BLUETOOTH_ERROR_NONE;
418 static void __bt_service_reset_gatt_data()
422 BT_INFO("Rest numapp");
425 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
426 numapps[k].is_initialized = 0;
427 numapps[k].instance_id = -1;
428 numapps[k].adv_handle = 0;
429 numapps[k].adv_instance = -1;
430 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
431 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
432 memset(numapps[k].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
433 memset(numapps[k].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
434 numapps[k].adv_data_len = 0;
435 numapps[k].scan_rsp_len = 0;
439 void _bt_gatt_deinit(void)
441 BT_INFO("GATT deinit");
443 if (OAL_STATUS_SUCCESS != gatt_disable())
444 BT_ERR("gatt deinit failed");
446 /* Un-register gatt event handler */
447 _bt_service_unregister_event_handler_callback(BT_GATT_MODULE);
449 __bt_service_reset_gatt_data();
452 void _bt_update_adv_handle(const char *sender, int adv_handle)
455 BT_DBG("Sender [%s] Adv handle [%d]", sender, adv_handle);
456 bt_service_app_info_t *info = NULL;
458 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
460 /* Search for a app which has same sender and adv handle as 0 */
461 if (!g_strcmp0(info->sender, sender) && info->adv_handle == 0)
462 info->adv_handle = adv_handle;
466 int _bt_is_sender_gatt_server_with_no_adv(const char *sender, int adv_handle)
469 BT_DBG("Sender [%s] Adv handle [%d]", sender, adv_handle);
470 bt_service_app_info_t *info = NULL;
472 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
474 /* Search for a app which has same sender and adv handle as 0
475 It is possible that same sender but different adv handle */
476 if (!g_strcmp0(info->sender, sender) && info->adv_handle == 0) {
477 //info->adv_handle = adv_handle;
478 return info->instance_id;
484 int _bt_get_allocated_server_instance(const char *sender, int adv_handle, gboolean use_reserved_slot)
487 BT_DBG("Sender [%s] Adv handle [%d]", sender, adv_handle);
488 bt_service_app_info_t *info = NULL;
490 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
493 /* Exact matching of Adv handle + sender combination */
494 if (!g_strcmp0(info->sender, sender) && info->adv_handle == adv_handle)
495 return info->instance_id;
497 if (!g_strcmp0(info->sender, sender) && info->adv_handle == -1)
498 return info->instance_id;
504 char * _bt_get_sender_and_handle(int server_instance, int *adv_handle)
507 bt_service_app_info_t *info = NULL;
509 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
512 if (info->instance_id == server_instance && info->adv_handle != -1) {
513 *adv_handle = info->adv_handle;
514 BT_DBG("Server instance [%d] Adv handle [%d] Sender [%s]", server_instance, *adv_handle, info->sender);
515 return g_strdup(info->sender);
521 void _bt_set_new_adv_data(bluetooth_advertising_data_t *adv, int len, int instance)
525 bt_service_app_info_t *info = NULL;
526 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
529 if (info->instance_id == instance) {
530 memcpy(info->adv_data.data, &adv->data, len);
537 void _bt_set_new_scan_rsp_data(bluetooth_scan_resp_data_t *scan, int len, int instance)
541 bt_service_app_info_t *info = NULL;
542 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
545 if (info->instance_id == instance) {
546 memcpy(info->scan_rsp.data, &scan->data, len);
553 void _bt_get_previous_adv_data(bluetooth_advertising_data_t *adv, int *len, int instance)
557 bt_service_app_info_t *info = NULL;
559 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
562 if (info->instance_id == instance) {
563 memcpy(&adv->data, info->adv_data.data, info->adv_data_len);
564 *len = info->adv_data_len;
570 void _bt_get_previous_scan_rsp_data(bluetooth_scan_resp_data_t *scan, int *len, int instance)
574 bt_service_app_info_t *info = NULL;
576 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
579 if (info->instance_id == instance) {
580 memcpy(&scan->data, info->scan_rsp.data, info->scan_rsp_len);
581 *len = info->scan_rsp_len;
587 #ifdef TIZEN_GATT_CLIENT
588 static int __bt_do_unregister_gatt_instance(int instance_id)
590 int ret = OAL_STATUS_SUCCESS;
593 BT_INFO("DeAllocate server or client instance ID [%d]", instance_id);
595 /* Reset data: instance_id parameter could be either for GATT Server or for GATT client */
596 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
597 if (numapps[k].instance_id == instance_id) {
598 BT_INFO("This is a GATT server app, unregister: Slot [%d] vacant", k);
599 numapps[k].is_initialized = FALSE;
600 numapps[k].instance_id = -1;
601 numapps[k].adv_handle = 0;
602 numapps[k].adv_instance = -1;
603 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
604 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
605 memset(numapps[k].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
606 memset(numapps[k].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
607 numapps[k].adv_data_len = 0;
608 numapps[k].scan_rsp_len = 0;
610 /* Its a GATT Server Instance */
611 ret = gatts_unregister(instance_id);
612 if (ret != OAL_STATUS_SUCCESS) {
613 BT_ERR("DeAllocate server instance with stack Fail ret: %d", ret);
614 return BLUETOOTH_ERROR_INTERNAL;
617 } else if (numapps[k].client_id == instance_id) {
618 BT_INFO("This is a GATT client app, unregister: Slot [%d] vacant", k);
619 numapps[k].client_id = -1;
620 numapps[k].is_initialized = FALSE;
621 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
622 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
623 memset(&numapps[k].address.addr, 0x00, sizeof(bluetooth_device_address_t));
625 /* Its a GATT Client Instance */
626 ret = gattc_deregister(instance_id);
627 if (ret != OAL_STATUS_SUCCESS) {
628 BT_ERR("DeAllocate GATT Client instance with stack Fail ret: %d", ret);
629 return BLUETOOTH_ERROR_INTERNAL;
634 return BLUETOOTH_ERROR_NONE;
638 static int __bt_do_unregister_server_instance(int server_instance)
640 int ret = OAL_STATUS_SUCCESS;
643 /* Unregister the server instance */
644 ret = gatts_unregister(server_instance);
645 if (ret != OAL_STATUS_SUCCESS) {
646 BT_ERR("DeAllocate server instance with stack Fail ret: %d", ret);
647 return BLUETOOTH_ERROR_INTERNAL;
649 BT_INFO("DeAllocated server instance with stack successful..");
652 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
653 if (numapps[k].instance_id == server_instance) {
654 numapps[k].is_initialized = 0;
655 numapps[k].instance_id = -1;
656 numapps[k].adv_handle = 0;
657 numapps[k].adv_instance = -1;
658 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
659 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
660 memset(numapps[k].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
661 memset(numapps[k].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
662 numapps[k].adv_data_len = 0;
663 numapps[k].scan_rsp_len = 0;
667 BT_DBG("Going8 to reset numapp block num [%d]", k);
668 return BLUETOOTH_ERROR_NONE;
671 static void __bt_remove_all_service_handles(int instance_id)
675 int ret = OAL_STATUS_SUCCESS;
678 count = g_slist_length(numapps[instance_id].service_handles);
679 BT_INFO("Before handle removal: current count [%d]", count);
681 for (l = numapps[instance_id].service_handles; l != NULL;) {
686 BT_INFO("Server Handle to be Removed [%d] Instance ID [%d]", *handle, instance_id);
687 if (_bt_gatt_server_stop_service(numapps[instance_id].sender, *handle, instance_id) != BLUETOOTH_ERROR_NONE)
690 ret = gatts_delete_service(instance_id, *handle);
691 if (ret != OAL_STATUS_SUCCESS) {
692 BT_ERR("ret: %d", ret);
695 numapps[instance_id].service_handles = g_slist_remove(numapps[instance_id].service_handles, handle);
698 count = g_slist_length(numapps[instance_id].service_handles);
699 BT_INFO("After deleting current count [%d]", count);
705 int _bt_unregister_server_instance(const char *sender, int adv_handle)
707 BT_INFO("Unregister Allocated server instance request Sender [%s] Adv handle [%d]", sender, adv_handle);
708 int result = BLUETOOTH_ERROR_NONE;
709 int apps[MAX_APPS_SUPPORTED];
713 memset(&apps, -1, sizeof(apps));
715 if (adv_handle == 0) {
716 BT_DBG("Its a direct GATT Server app request to unregister");
717 /* Unregister server instance for each app with same sender (case: GATT Server with multiple adv handle) */
718 bt_service_app_info_t *info = NULL;
720 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
723 /* Exact matching of sender */
724 if (!g_strcmp0(info->sender, sender)) {
725 BT_INFO("Unregister GATT server instance [%d]", info->instance_id);
726 /* Unregister all service handles with stack */
727 __bt_remove_all_service_handles(info->instance_id);
729 /* Disable adv if running */
730 BT_INFO("Disable Advertising Adv Handle [%d] sender [%s]",
731 info->adv_handle, sender);
732 _bt_set_advertising(sender, info->adv_handle, FALSE, FALSE);
734 /* Save all instances which need to be unregistered */
735 apps[info->instance_id] = 1;
739 BT_DBG("Its an Internal unregister request by adv application");
740 server_instance = _bt_get_allocated_server_instance(sender, adv_handle, FALSE);
741 BT_DBG("Its an Internal unregister request by adv application: Adv disabled srv instance [%d]", server_instance);
742 if (server_instance == -1) {
743 BT_ERR("No allocated server instance to be removed");
744 return BLUETOOTH_ERROR_INVALID_PARAM;
746 if (!numapps[server_instance].service_handles) {
747 BT_INFO("There are no Service handles with this app, safe to unregister");
748 /* Unregister server instance only if this sender does not have any gatt services in it */
749 result = __bt_do_unregister_server_instance(server_instance);
751 numapps[server_instance].adv_handle = 0;
752 memset(numapps[server_instance].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
753 numapps[server_instance].adv_data_len = 0;
754 memset(numapps[server_instance].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
755 numapps[server_instance].scan_rsp_len = 0;
760 /* Unregister all apps one by one */
761 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
763 BT_INFO("Unregister app[%d]", k);
764 /* Unregister server instance */
765 __bt_do_unregister_server_instance(k);
772 int _bt_register_server_instance(const char *sender, int adv_handle)
774 int ret = OAL_STATUS_SUCCESS;
775 char *uuid_string = NULL;
780 BT_INFO("###Check on which instance Server instance can be initialized....");
781 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
782 if (numapps[k].is_initialized == 1) {
783 BT_INFO("Instance ID [%d] is already in use..Check next slot", numapps[k].instance_id);
786 BT_INFO("Time to register GATT Server..UUID to be used is [%s] slot [%d]", uuid_list[slot-1], slot);
792 BT_ERR("No Slot if free for GATT Server registration..");
793 return BLUETOOTH_ERROR_REGISTRATION_FAILED;
796 uuid_string = g_malloc0(BT_UUID_STRING_MAX);
797 _bt_string_to_uuid(uuid_list[slot-1], (service_uuid_t*)&uuid);
798 g_strlcpy(uuid_string, uuid_list[slot-1], BT_UUID_STRING_MAX);
799 BT_INFO("Copied UUID string [%s]", uuid_string);
801 /* Register GATT Server */
802 ret = gatts_register(&uuid);
803 if (ret != OAL_STATUS_SUCCESS) {
804 BT_ERR("ret: %d", ret);
806 return BLUETOOTH_ERROR_INTERNAL;
808 BT_INFO("GATT Server registration call successfully accepted by OAL..wait for Instance Initialized event from OAL..");
809 /* Return & wait for GATT Server Instance Initialization event */
810 memset(numapps[slot].sender, 0x00, sizeof(numapps[slot].sender));
811 memset(numapps[slot].uuid, 0x00, sizeof(numapps[slot].uuid));
813 g_strlcpy(numapps[slot].sender, sender, sizeof(numapps[slot].sender));
814 g_strlcpy(numapps[slot].uuid, uuid_string, sizeof(numapps[slot].uuid));
816 numapps[slot].is_initialized = 0; /* Set initialization from app registered callback */
817 numapps[slot].adv_handle = adv_handle;
820 return BLUETOOTH_ERROR_NONE;
824 static void __bt_gatt_handle_pending_request_info(int result,
825 int service_function, void *data, unsigned int size)
829 invocation_info_t *req_info = NULL;
830 ret_if(data == NULL);
833 for (l = _bt_get_invocation_list(); l != NULL; l = g_slist_next(l)) {
835 if (req_info == NULL || req_info->service_function != service_function)
838 switch (service_function) {
840 case BT_GATT_SERVER_REGISTER: {
841 bt_service_app_info_t *param = (bt_service_app_info_t*)data;
842 BT_INFO("Sender [%s] Request Sender [%s]", param->sender, req_info->sender);
843 if (!g_strcmp0(req_info->sender, param->sender)) {
844 BT_DBG("GATT Server app found [%s]", req_info->sender);
846 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
847 g_array_append_vals(out_param, ¶m->instance_id, sizeof(int));
848 _bt_service_method_return(req_info->context, out_param, result);
850 g_free(req_info->user_data);
851 _bt_free_info_from_invocation_list(req_info);
852 g_array_free(out_param, TRUE);
856 case BT_GATT_SERVER_START_SERVICE:
857 case BT_GATT_SERVER_DELETE_SERVICE: {
858 bt_service_app_info_t *param = (bt_service_app_info_t*)data;
859 BT_INFO("Sender [%s] Request Sender [%s]", param->sender, req_info->sender);
861 int *saved_instance_id = (int*)req_info->user_data;
862 if (!g_strcmp0(req_info->sender, param->sender) && param->instance_id == *saved_instance_id) {
863 BT_DBG("GATT Server app found [%s] Instance ID [%d] Reply DBUS",
864 req_info->sender, *saved_instance_id);
866 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
867 g_array_append_vals(out_param, &saved_instance_id, sizeof(int));
868 _bt_service_method_return(req_info->context, out_param, result);
870 g_free(req_info->user_data);
871 _bt_free_info_from_invocation_list(req_info);
872 g_array_free(out_param, TRUE);
876 case BT_GATT_SERVER_ADD_SERVICE:
877 case BT_GATT_SERVER_ADD_DESCRIPTOR:
878 case BT_GATT_SERVER_ADD_CHARACTERISTIC: {
879 int *handle = (int*)data;
880 BT_INFO("Characteristic added: Handle [%d]", *handle);
881 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
883 g_array_append_vals(out_param, handle, sizeof(int));
884 _bt_service_method_return(req_info->context, out_param, result);
886 g_free(req_info->user_data);
887 _bt_free_info_from_invocation_list(req_info);
888 g_array_free(out_param, TRUE);
893 case BT_DISCONNECT_LE: {
894 #ifdef TIZEN_GATT_CLIENT
895 char *addr = (char*)req_info->user_data;
896 bluetooth_device_address_t address;
898 if (!g_strcmp0(addr, (char*)data)) {
899 BT_INFO("GATT Client connect-disconnect call pending for app [%s] addr [%s]",
900 req_info->sender, addr);
901 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
902 _bt_convert_addr_string_to_type(address.addr, addr);
904 g_array_append_vals(out_param, (bluetooth_device_address_t*)&address,
905 sizeof(bluetooth_device_address_t));
906 _bt_service_method_return(req_info->context, out_param, result);
908 g_free(req_info->user_data);
909 _bt_free_info_from_invocation_list(req_info);
910 g_array_free(out_param, TRUE);
913 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
915 g_array_append_vals(out_param, (bluetooth_device_address_t*)data,
916 sizeof(bluetooth_device_address_t));
917 _bt_service_method_return(req_info->context, out_param, result);
919 g_free(req_info->user_data);
920 _bt_free_info_from_invocation_list(req_info);
922 g_array_free(out_param, TRUE);
926 #ifdef TIZEN_GATT_CLIENT
927 case BT_GATT_CLIENT_REGISTER: {
928 bt_service_app_info_t *param = (bt_service_app_info_t*)data;
929 BT_INFO("Sender [%s] Request Sender [%s]", param->sender, req_info->sender);
930 if (!g_strcmp0(req_info->sender, param->sender)) {
931 BT_DBG("GATT Client app found [%s] created client ID [%d]",
932 req_info->sender, param->client_id);
934 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
935 g_array_append_vals(out_param, ¶m->client_id, sizeof(int));
936 _bt_service_method_return(req_info->context, out_param, result);
938 g_free(req_info->user_data);
939 _bt_free_info_from_invocation_list(req_info);
940 g_array_free(out_param, TRUE);
944 case BT_GATT_GET_PRIMARY_SERVICES: {
945 bt_services_browse_info_t *param = (bt_services_browse_info_t*)data;
946 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
947 _bt_convert_addr_type_to_string(address,
948 (unsigned char *)(¶m->device_addr.addr));
950 BT_INFO("Request Sender [%s] address [%s]", req_info->sender, address);
952 /* Match address to determine same request */
953 if (!g_strcmp0((char*)req_info->user_data, address)) {
954 BT_DBG("GATT Client app found [%s] Remote address [%s]",
955 req_info->sender, address);
957 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
958 g_array_append_vals(out_param, param,
959 sizeof(bt_services_browse_info_t));
961 //g_array_append_vals(out_param, ¶m->client_id, sizeof(int));
962 _bt_service_method_return(req_info->context, out_param, result);
964 g_free(req_info->user_data);
965 _bt_free_info_from_invocation_list(req_info);
966 g_array_free(out_param, TRUE);
971 case BT_GATT_GET_SERVICE_PROPERTIES: {
972 bt_char_browse_info_t param;
973 memcpy((void*)¶m, data, sizeof(bt_char_browse_info_t));
974 //bt_char_browse_info_t *param = (bt_char_browse_info_t*)data;
975 BT_INFO("Request Sender [%s]", req_info->sender);
976 bluetooth_gatt_client_svc_prop_info_t *prop = (bluetooth_gatt_client_svc_prop_info_t*)req_info->user_data;
978 /* Match both address and service properties to determine same request */
979 if (!memcmp(param.device_addr.addr,
980 prop->device_address.addr,
981 sizeof(bluetooth_device_address_t)) &&
982 !memcmp(param.svc_uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
983 param.svc_inst_id == prop->svc.instance_id) {
984 BT_INFO("Returning Service properties");
986 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
987 g_array_append_vals(out_param, ¶m, sizeof(bt_char_browse_info_t));
988 _bt_service_method_return(req_info->context, out_param, result);
990 g_free(req_info->user_data);
991 _bt_free_info_from_invocation_list(req_info);
992 g_array_free(out_param, TRUE);
996 case BT_GATT_GET_CHARACTERISTIC_PROPERTIES: {
997 bt_descriptor_browse_info_t *param = (bt_descriptor_browse_info_t*)data;
999 BT_INFO("Request Sender [%s]", req_info->sender);
1000 bluetooth_gatt_client_char_prop_info_t *prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
1002 /* Match both address, service properties &char properties to determine same request */
1003 if (!memcmp(param->device_addr.addr,
1004 prop->device_address.addr,
1005 sizeof(bluetooth_device_address_t)) &&
1006 !memcmp(param->svc_uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1007 param->svc_inst_id == prop->svc.instance_id &&
1008 !memcmp(param->char_uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1009 param->char_inst_id == prop->characteristic.instance_id) {
1010 BT_INFO("Returning Characteristic properties");
1011 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1012 g_array_append_vals(out_param, param, sizeof(bt_descriptor_browse_info_t));
1013 _bt_service_method_return(req_info->context, out_param, result);
1015 g_free(req_info->user_data);
1016 _bt_free_info_from_invocation_list(req_info);
1017 g_array_free(out_param, TRUE);
1021 case BT_GATT_READ_CHARACTERISTIC:
1022 case BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE: {
1023 bluetooth_gatt_client_char_prop_info_t *param = (bluetooth_gatt_client_char_prop_info_t*)data;
1024 BT_INFO("Request Sender [%s]", req_info->sender);
1026 char *addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
1027 bluetooth_gatt_client_char_prop_info_t *prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
1028 _bt_convert_addr_type_to_string(addr, prop->device_address.addr);
1030 if (!memcmp(param, prop, sizeof(bluetooth_gatt_client_char_prop_info_t))) {
1031 BT_INFO("Gatt Char read or write request matched for address [%s]", addr);
1032 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1033 g_array_append_vals(out_param, param, sizeof(bluetooth_gatt_client_char_prop_info_t));
1034 _bt_service_method_return(req_info->context, out_param, result);
1036 g_free(req_info->user_data);
1037 _bt_free_info_from_invocation_list(req_info);
1038 g_array_free(out_param, TRUE);
1043 case BT_GATT_READ_DESCRIPTOR_VALUE:
1044 case BT_GATT_WRITE_DESCRIPTOR_VALUE: {
1045 bluetooth_gatt_client_desc_prop_info_t *param = (bluetooth_gatt_client_desc_prop_info_t*)data;
1046 BT_INFO("Request Sender [%s]", req_info->sender);
1048 char *addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
1049 bluetooth_gatt_client_desc_prop_info_t *prop = (bluetooth_gatt_client_desc_prop_info_t*)req_info->user_data;
1050 _bt_convert_addr_type_to_string(addr, prop->device_address.addr);
1052 if (!memcmp(param, prop, sizeof(bluetooth_gatt_client_desc_prop_info_t))) {
1053 BT_INFO("Descriptor read or write request matched for address [%s]", addr);
1054 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1055 g_array_append_vals(out_param, param, sizeof(bluetooth_gatt_client_desc_prop_info_t));
1056 _bt_service_method_return(req_info->context, out_param, result);
1058 g_free(req_info->user_data);
1059 _bt_free_info_from_invocation_list(req_info);
1060 g_array_free(out_param, TRUE);
1073 static void __bt_handle_server_instance_registered(event_gatts_register_t *data)
1075 bt_service_app_info_t *info = NULL;
1077 char *uuid_string = g_malloc0(BT_UUID_STRING_MAX);
1079 _bt_uuid_to_string(&(data->server_uuid), uuid_string);
1080 BT_INFO("Instance ID is Intialized [%d] UUID initialized [%s]", data->server_inst, uuid_string);
1082 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1083 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1086 if (g_strcmp0(info->uuid, uuid_string) == 0) {
1087 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1088 info->is_initialized = TRUE;
1089 info->instance_id = data->server_inst;
1090 info->adv_instance = data->server_inst;
1091 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE, BT_GATT_SERVER_REGISTER,
1092 (void*)info, sizeof(bt_service_app_info_t));
1096 g_free(uuid_string);
1100 static void __bt_handle_gatt_server_service_added(event_gatts_srvc_prm_t *event)
1102 int result = BLUETOOTH_ERROR_NONE;
1106 bt_service_app_info_t *info = NULL;
1109 char *uuid_str = g_malloc0(BT_UUID_STRING_MAX);
1110 _bt_uuid_to_string(&(event->gatt_srvc_id.id.uuid), uuid_str);
1111 BT_INFO("GATT Added Service UUID: [%s] Server Ins [%d] Service hdl [%d] Is Primary [%d]\n",
1112 uuid_str, event->gatt_srvc_stat.server_inst, svc_handle, event->gatt_srvc_id.is_prmry);
1114 if (event->gatt_srvc_stat.status != OAL_STATUS_SUCCESS) {
1115 result = BLUETOOTH_ERROR_INTERNAL;
1116 svc_handle = 0; /* Service handle set to 0 indicates.
1117 0 is reserved by BT SIG, cant be used by app*/
1119 svc_handle = event->gatt_srvc_stat.servic_hndl;
1121 BT_INFO("GATT Added Service Status [%d] Server Ins [%d] Service hdl [%d] Is Primary [%d]\n",
1122 event->gatt_srvc_stat.status, event->gatt_srvc_stat.server_inst, svc_handle, event->gatt_srvc_id.is_prmry);
1124 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1125 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1128 if (info->instance_id == event->gatt_srvc_stat.server_inst) {
1129 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1130 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_ADD_SERVICE,
1131 (int*)&svc_handle, sizeof(int));
1133 /* Add Service Handle */
1134 if (svc_handle > 0) {
1135 handle = g_malloc0(sizeof(int));
1136 *handle = svc_handle;
1137 numapps[event->gatt_srvc_stat.server_inst].service_handles = g_slist_append(numapps[event->gatt_srvc_stat.server_inst].service_handles, handle);
1138 count = g_slist_length(numapps[event->gatt_srvc_stat.server_inst].service_handles);
1139 BT_INFO("Added Service handle [%d] to list..current count [%d]", svc_handle, count);
1149 static void __bt_handle_gatt_server_characteristic_added(event_gatts_srvc_charctr_t *event)
1151 int result = BLUETOOTH_ERROR_NONE;
1153 bt_service_app_info_t *info = NULL;
1155 char *uuid_str = g_malloc0(BT_UUID_STRING_MAX);
1157 BT_INFO("GATT Server Char added status [%d]", event->gatt_srvc_stat.status);
1158 BT_INFO("GATT Server_inst: [%d]", event->gatt_srvc_stat.server_inst);
1159 BT_INFO("GATT Service Handle: [%d]", event->gatt_srvc_stat.servic_hndl);
1160 BT_INFO("GATT Add characteristic Status: [%d]", event->gatt_srvc_stat.status);
1161 BT_INFO("GATT Service characteristic Handle: [%d]", event->charctr_hndl);
1163 if (event->gatt_srvc_stat.status != OAL_STATUS_SUCCESS) {
1164 result = BLUETOOTH_ERROR_INTERNAL;
1165 char_handle = 0; /* characteristic handle set to 0 indicates.
1166 0 is reserved by BT SIG, cant be used by app*/
1168 char_handle = event->charctr_hndl;
1171 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1172 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1175 if (info->instance_id == event->gatt_srvc_stat.server_inst) {
1176 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1177 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_ADD_CHARACTERISTIC,
1178 (int*)&char_handle, sizeof(int));
1184 _bt_uuid_to_string(&(event->charctr_uuid), uuid_str);
1185 BT_INFO("GATT Added Characteristic: UUID: [%s]", uuid_str);
1191 static void __bt_handle_gatt_server_descriptor_added(event_gatts_srvc_descr_t* event)
1193 int result = BLUETOOTH_ERROR_NONE;
1195 bt_service_app_info_t *info = NULL;
1197 char *uuid_str = g_malloc0(BT_UUID_STRING_MAX);
1199 BT_INFO("GATT Server Descriptor added status [%d]", event->gatt_srvc_stat.status);
1200 BT_INFO("GATT Server_inst: [%d]", event->gatt_srvc_stat.server_inst);
1201 BT_INFO("GATT Service Handle: [%d]", event->gatt_srvc_stat.servic_hndl);
1202 BT_INFO("GATT Add Descriptor Status: [%d]", event->gatt_srvc_stat.status);
1203 BT_INFO("GATT Service Descriptor Handle: [%d]", event->descrptr_hndl);
1205 if (event->gatt_srvc_stat.status != OAL_STATUS_SUCCESS) {
1206 result = BLUETOOTH_ERROR_INTERNAL;
1207 desc_handle = 0; /* Service handle set to 0 indicates.
1208 0 is reserved by BT SIG, cant be used by app*/
1210 desc_handle = event->descrptr_hndl;
1212 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1213 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1216 if (info->instance_id == event->gatt_srvc_stat.server_inst) {
1217 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1218 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_ADD_DESCRIPTOR,
1219 (int*)&desc_handle, sizeof(int));
1224 _bt_uuid_to_string(&(event->descrptr_uuid), uuid_str);
1225 BT_INFO("GATT Added Descriptor: UUID: [%s]", uuid_str);
1231 static void __bt_handle_gatt_server_service_started(event_gatts_srvc_t *event)
1233 bt_service_app_info_t *info = NULL;
1234 int result = BLUETOOTH_ERROR_NONE;
1236 BT_INFO("GATT Server Service Started..");
1239 BT_INFO("GATT Server_inst: [%d]", event->server_inst);
1240 BT_INFO("GATT Service Handle: [%d]", event->servic_hndl);
1241 BT_INFO("GATT Service Started Status: [%d]", event->status);
1243 if (event->status != OAL_STATUS_SUCCESS) {
1244 BT_ERR("GATT Server Service Start Failed Err: [%d]", event->status);
1245 result = BLUETOOTH_ERROR_INTERNAL;
1248 /* Check if the just registered Instance ID belongs to requester */
1249 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1252 if (info->instance_id == event->server_inst) {
1253 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1254 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_START_SERVICE,
1255 (void*)info, sizeof(bt_service_app_info_t));
1263 static void __bt_handle_gatt_server_service_stopped(event_gatts_srvc_t *event)
1265 int result = BLUETOOTH_ERROR_NONE;
1266 bt_service_app_info_t *info = NULL;
1268 BT_INFO("GATT Server Service Stopped..");
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 Stopped Status: [%d]", event->status);
1274 if (event->status != OAL_STATUS_SUCCESS) {
1275 BT_ERR("GATT Server Service Stop 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_STOP_SERVICE,
1286 (void*)info, sizeof(bt_service_app_info_t));
1294 static void __bt_handle_gatt_server_service_deleted(event_gatts_srvc_t *event)
1296 int result = BLUETOOTH_ERROR_NONE;
1297 bt_service_app_info_t *info = NULL;
1299 BT_INFO("GATT Server Service Deleted..");
1301 BT_INFO("GATT Server_inst: [%d]", event->server_inst);
1302 BT_INFO("GATT Service Handle: [%d]", event->servic_hndl);
1303 BT_INFO("GATT Service Deleted Status: [%d]", event->status);
1305 if (event->status != OAL_STATUS_SUCCESS) {
1306 BT_ERR("GATT Server Service Delete Failed Err: [%d]", event->status);
1307 result = BLUETOOTH_ERROR_INTERNAL;
1310 /* Check if the just registered Instance ID belongs to requester */
1311 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1314 if (info->instance_id == event->server_inst) {
1315 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1316 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_DELETE_SERVICE,
1317 (void*)info, sizeof(bt_service_app_info_t));
1325 static struct gatt_client_info_t *__bt_find_remote_gatt_client_info(char *address)
1328 struct gatt_client_info_t *info = NULL;
1329 for (l = gatt_client_info_list; l != NULL; l = g_slist_next(l)) {
1330 info = (struct gatt_client_info_t*)l->data;
1334 if (!g_strcmp0(info->addr, address)) {
1335 BT_INFO("Remote GATT client found addr[%s]", info->addr);
1342 static struct gatt_client_info_t *__bt_find_remote_gatt_client_info_from_conn_id(int conn_id)
1345 struct gatt_client_info_t *info = NULL;
1347 for (l = gatt_client_info_list; l != NULL; l = g_slist_next(l)) {
1348 info = (struct gatt_client_info_t*)l->data;
1352 if (info->connection_id == conn_id) {
1353 BT_INFO("Remote GATT client found addr[%s]", info->addr);
1360 #ifdef TIZEN_GATT_CLIENT
1361 static struct gatt_server_info_t *__bt_find_remote_gatt_server_info(char *address)
1364 struct gatt_server_info_t *info = NULL;
1365 for (l = gatt_server_info_list; l != NULL; l = g_slist_next(l)) {
1366 info = (struct gatt_server_info_t*)l->data;
1370 if (!g_strcmp0(info->addr, address)) {
1371 BT_INFO("Remote GATT Server found addr[%s]", info->addr);
1378 static struct gatt_out_conn_info_t* __bt_find_gatt_outgoing_conn_info(char *address)
1381 struct gatt_out_conn_info_t *info = NULL;
1382 for (l = outgoing_gatt_conn_list; l != NULL; l = g_slist_next(l)) {
1383 info = (struct gatt_out_conn_info_t*)l->data;
1387 if (!g_strcmp0(info->addr, address)) {
1388 BT_INFO("Outgoing connection info found addr[%s]", info->addr);
1399 #ifdef TIZEN_GATT_CLIENT
1400 static void __bt_handle_gatt_server_connection_state(event_gatts_conn_t *event)
1402 int result = BLUETOOTH_ERROR_NONE;
1403 struct gatt_client_info_t *conn_info = NULL;
1404 bluetooth_device_address_t dev_addr;
1406 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1408 memcpy(dev_addr.addr, event->address.addr, 6);
1410 /* REPLY dbus Context to application which called BT_CONNECT_LE. There is status
1412 _bt_convert_addr_type_to_string(address,
1413 (unsigned char *)dev_addr.addr);
1414 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
1415 address, BT_ADDRESS_STRING_SIZE);
1417 BT_INFO("GATT Server Connedted: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1418 address, event->server_inst, event->conn_id);
1421 /* Check if device is already in connected list */
1422 conn_info = __bt_find_remote_gatt_client_info(address);
1425 BT_INFO("Conn Info absent: But no need to Send Local GATT Server Connected event to apps");
1427 param = g_variant_new("(is)", result, address);
1429 /* Send event to application */
1430 _bt_send_event(BT_DEVICE_EVENT,
1431 BLUETOOTH_EVENT_GATT_SERVER_CONNECTED, /* Local device is GATT server */
1434 /* Save Connection info */
1435 conn_info = g_new0(struct gatt_client_info_t, 1);
1436 conn_info->addr = g_strdup(address);
1437 BT_INFO("Added GATT client addr[%s]", conn_info->addr);
1438 conn_info->connection_id = event->conn_id;
1439 conn_info->instance_id = event->server_inst;
1440 gatt_client_info_list = g_slist_append(gatt_client_info_list, conn_info);
1441 BT_INFO("Total num of connected Remote GATT Clients [%d]", g_slist_length(gatt_client_info_list));
1447 /* GATT Server Dis connected */
1448 static void __bt_handle_gatt_server_disconnection_state(event_gatts_conn_t *event)
1450 int result = BLUETOOTH_ERROR_NONE;
1451 struct gatt_client_info_t *conn_info = NULL;
1452 bluetooth_device_address_t dev_addr;
1454 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1456 memcpy(dev_addr.addr, event->address.addr, 6);
1458 /* REPLY dbus Context to application which called BT_DISCONNECT_LE. There is status
1460 _bt_convert_addr_type_to_string(address,
1461 (unsigned char *)dev_addr.addr);
1463 if (NULL == _bt_get_request_info_data(BT_DISCONNECT_LE, address)) {
1464 if (NULL != _bt_get_request_info_data(BT_CONNECT_LE, address)) {
1465 result = BLUETOOTH_ERROR_INTERNAL;
1467 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
1468 address, BT_ADDRESS_STRING_SIZE);
1470 BT_ERR("Failed to connect Local GATT Server Remote Client addr[%s]", address);
1474 __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE,
1475 address, BT_ADDRESS_STRING_SIZE);
1478 BT_INFO("Local GATT Server DisConnected: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1479 address, event->server_inst, event->conn_id);
1480 /* Remove Connection info */
1481 conn_info = __bt_find_remote_gatt_client_info(address);
1483 BT_INFO("No need to Send Local GATT Server Disconnected event to apps, just remove remote client info");
1485 param = g_variant_new("(is)", result, address);
1486 /* Send event to application */
1487 _bt_send_event(BT_DEVICE_EVENT,
1488 BLUETOOTH_EVENT_GATT_SERVER_DISCONNECTED, /* Local device is GATT server */
1491 /* Remove info from List */
1492 gatt_client_info_list = g_slist_remove(gatt_client_info_list, conn_info);
1493 BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
1494 g_free(conn_info->addr);
1502 static void __bt_handle_gatt_server_connection_state(event_gatts_conn_t *event)
1504 int result = BLUETOOTH_ERROR_NONE;
1505 struct gatt_client_info_t *conn_info = NULL;
1506 bt_service_app_info_t *info = NULL;
1507 bluetooth_device_address_t dev_addr;
1508 GVariant *param = NULL;
1511 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1513 memcpy(dev_addr.addr, event->address.addr, 6);
1516 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1517 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1520 if (info->instance_id == event->server_inst) {
1521 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1522 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
1523 (bluetooth_device_address_t*)&dev_addr, sizeof(bluetooth_device_address_t));
1529 /* Send event to BT-API */
1530 _bt_convert_addr_type_to_string(address,
1531 (unsigned char *)dev_addr.addr);
1533 BT_INFO("GATT Server Connedted: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1534 address, event->server_inst, event->conn_id);
1537 /* Check if device is already in connected list */
1538 conn_info = __bt_find_remote_gatt_client_info(address);
1541 param = g_variant_new("(is)", result, address);
1542 /* Send event to application */
1543 _bt_send_event(BT_GATT_SERVER_EVENT,
1544 BLUETOOTH_EVENT_GATT_CONNECTED,
1547 /* Save Connection info */
1548 conn_info = g_new0(struct gatt_client_info_t, 1);
1549 conn_info->addr = g_strdup(address);
1550 BT_INFO("Added GATT client addr[%s]", conn_info->addr);
1551 conn_info->connection_id = event->conn_id;
1552 conn_info->instance_id = event->server_inst;
1553 gatt_client_info_list = g_slist_append(gatt_client_info_list, conn_info);
1554 BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
1560 /* GATT Server Dis connected */
1561 static void __bt_handle_gatt_server_disconnection_state(event_gatts_conn_t *event)
1563 int result = BLUETOOTH_ERROR_NONE;
1564 struct gatt_client_info_t *conn_info = NULL;
1565 bluetooth_device_address_t dev_addr;
1566 bt_service_app_info_t *info = NULL;
1567 GVariant *param = NULL;
1570 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1572 memcpy(dev_addr.addr, event->address.addr, 6);
1574 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1575 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1578 if (info->instance_id == event->server_inst) {
1579 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1580 __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE,
1581 (bluetooth_device_address_t*)&dev_addr, sizeof(bluetooth_device_address_t));
1587 /* Send event to BT-API */
1588 _bt_convert_addr_type_to_string(address,
1589 (unsigned char *)dev_addr.addr);
1591 BT_INFO("GATT Server DisConnected: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1592 address, event->server_inst, event->conn_id);
1594 param = g_variant_new("(is)", result, address);
1595 /* Send event to application */
1596 _bt_send_event(BT_GATT_SERVER_EVENT,
1597 BLUETOOTH_EVENT_GATT_DISCONNECTED,
1600 /* Remove Connection info */
1601 conn_info = __bt_find_remote_gatt_client_info(address);
1603 BT_INFO("Remove GATT client info from List..");
1604 /* Remove info from List */
1605 gatt_client_info_list = g_slist_remove(gatt_client_info_list, conn_info);
1606 BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
1607 g_free(conn_info->addr);
1615 static void __bt_handle_gatt_server_write_requested(event_gatts_srvc_write_attr_t *event)
1618 bluetooth_device_address_t dev_addr;
1619 GVariant *param = NULL;
1620 int result = BLUETOOTH_ERROR_NONE;
1622 gboolean is_prepare_write;
1624 GVariant *data = NULL;
1626 struct gatt_server_req_info *req_info = NULL;
1627 BT_INFO("GATT Server Write Requested");
1629 memcpy(dev_addr.addr, event->address.addr, 6);
1631 BT_INFO("GATT Server Write Req Connection ID: [%d]", event->attr_trans.conn_id);
1632 BT_INFO("GATT Server Write Req Transaction ID:[%d]", event->attr_trans.trans_id);
1633 BT_INFO("GATT Server Write Req Attribute Handle: [%d]", event->attr_trans.attr_handle);
1634 BT_INFO("GATT Server Write Req Attribute Offset: [%d]", event->attr_trans.offset);
1635 BT_INFO("GATT Server Write Req value length [%d]", event->length);
1636 BT_INFO("GATT Server Write Req needs response: [%d]", event->need_rsp);
1637 BT_INFO("GATT Server Write Req Is Prep: [%d]", event->is_prep);
1639 if (event->length <= 0) {
1640 BT_INFO("GATT Server write requested, but length of attr value is 0");
1645 need_resp = event->need_rsp;
1646 is_prepare_write = event->is_prep;
1648 write_val = g_memdup(&event->value[0], event->length);
1650 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1651 _bt_convert_addr_type_to_string(address,
1652 (unsigned char *)dev_addr.addr);
1654 BT_INFO("GATT Server Write Request from remote client [%s]", address);
1656 if (event->length > 0) {
1657 for (i = 0; i < event->length; i++)
1658 BT_DBG("Data[%d] = [0x%x]", i, event->value[i]);
1660 /* Save Write Request Info */
1661 req_info = g_new0(struct gatt_server_req_info, 1);
1662 req_info->request_id = event->attr_trans.trans_id;
1663 req_info->attribute_handle = event->attr_trans.attr_handle;
1664 req_info->connection_id = event->attr_trans.conn_id;
1665 req_info->addr = address;
1666 req_info->offset = event->attr_trans.offset;
1667 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_WRITE;
1668 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1670 data = g_variant_new_from_data(
1671 G_VARIANT_TYPE_BYTESTRING,
1676 param = g_variant_new("(iiiiiibbsn@ay)", result,
1677 event->attr_trans.conn_id,
1678 event->attr_trans.trans_id,
1679 event->attr_trans.attr_handle,
1680 event->attr_trans.offset,
1688 _bt_send_event(BT_GATT_SERVER_EVENT,
1689 BLUETOOTH_EVENT_GATT_SERVER_VALUE_CHANGED,
1696 static void __bt_handle_gatt_server_read_requested(event_gatts_srvc_read_attr_t *event)
1698 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1699 bluetooth_device_address_t dev_addr;
1700 int result = BLUETOOTH_ERROR_NONE;
1701 struct gatt_server_req_info *req_info = NULL;
1702 GVariant *param = NULL;
1704 BT_INFO("GATT Server Read Requested");
1706 memcpy(dev_addr.addr, event->address.addr, 6);
1708 BT_INFO("GATT Server-Client Connection ID: [%d]", event->attr_trans.conn_id);
1709 BT_INFO("GATT Server-Client Transaction ID: [%d]", event->attr_trans.trans_id);
1710 BT_INFO("GATT Server Attribute Handle: [%d]", event->attr_trans.attr_handle);
1711 BT_INFO("GATT Server Attribute Offset for read: [%d]", event->attr_trans.offset);
1712 BT_INFO("GATT Server Attribute is long: [%d]", event->is_long);
1714 is_long = event->is_long;
1716 /* Save Read Request Info */
1717 req_info = g_new0(struct gatt_server_req_info, 1);
1718 req_info->request_id = event->attr_trans.trans_id;
1719 req_info->attribute_handle = event->attr_trans.attr_handle;
1720 req_info->connection_id = event->attr_trans.conn_id;
1721 req_info->addr = address;
1722 req_info->offset = event->attr_trans.offset;
1723 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_READ;
1724 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1726 /* Send event to BT-API */
1727 _bt_convert_addr_type_to_string(address,
1728 (unsigned char *)dev_addr.addr);
1730 BT_INFO("GATT Server Read Request from remote client [%s]", address);
1732 param = g_variant_new("(iiiiibs)", result,
1733 event->attr_trans.conn_id,
1734 event->attr_trans.trans_id,
1735 event->attr_trans.attr_handle,
1736 event->attr_trans.offset,
1740 _bt_send_event(BT_GATT_SERVER_EVENT,
1741 BLUETOOTH_EVENT_GATT_SERVER_READ_REQUESTED,
1747 static void __bt_handle_gatt_server_indicate_confirmed(event_gatts_ind_cnfrm_t *event)
1749 bluetooth_device_address_t dev_addr;
1750 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1751 int cur_connected_clients;
1752 static int recvd = 0;
1753 gboolean completed = 0;
1754 GVariant *param = NULL;
1756 /* OAL event does provide error, so MW assumes event will never contain wrong data,
1757 incase of any issues, check with OAL */
1758 int result = BLUETOOTH_ERROR_NONE;
1760 memcpy(dev_addr.addr, event->address.addr, 6);
1761 _bt_convert_addr_type_to_string(address,
1762 (unsigned char *)dev_addr.addr);
1764 BT_INFO("Indication sent to GATT client [%s] conn_ ID [%d] transaction ID [%d] Att handle [%d]",
1765 address, event->conn_id, event->trans_id, event->attr_handle);
1768 cur_connected_clients = g_slist_length(gatt_client_info_list);
1769 BT_INFO("Number of connected clients during sending Indication [%d] & current connected count [%d]",
1770 num_indicate_clients, cur_connected_clients);
1773 if (recvd == num_indicate_clients) {
1774 BT_INFO("Gatt indication confirm event for last GATT client.. [%s]", address);
1775 completed = 1; /* Last event */
1776 recvd = 0; /* Reset */
1777 num_indicate_clients = 0;
1780 param = g_variant_new("(isib)",
1786 /* Send event to BT-API */
1787 _bt_send_event(BT_GATT_SERVER_EVENT,
1788 BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_COMPLETED,
1791 BT_INFO("Received Indication confirm for client number [%d]", recvd);
1796 /* Tizen Platform Specific */
1797 static void __bt_handle_gatt_server_notification_changed(event_gatts_notif_t *event)
1799 bluetooth_device_address_t dev_addr;
1800 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1801 GVariant *param = NULL;
1804 /* OAL event does provide error, so MW assumes event will never contain wrong data,
1805 incase of any issues, check with OAL */
1806 int result = BLUETOOTH_ERROR_NONE;
1808 memcpy(dev_addr.addr, event->address.addr, 6);
1809 _bt_convert_addr_type_to_string(address,
1810 (unsigned char *)dev_addr.addr);
1812 BT_INFO("notification_changed [%s] conn_ ID [%d] transaction ID [%d] Att handle [%d] Notify[%d]",
1813 address, event->conn_id, event->trans_id, event->attr_handle, event->notify);
1815 /* Set Notifcation status */
1816 notify = event->notify;
1818 param = g_variant_new("(isib)",
1824 /* Send event to BT-API */
1825 _bt_send_event(BT_GATT_SERVER_EVENT,
1826 BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_STATE_CHANGED,
1833 static void __bt_handle_gatt_mtu_changed_event(event_gatts_mtu_changed_t *event)
1835 int result = BLUETOOTH_ERROR_NONE;
1836 struct gatt_client_info_t *conn_info = NULL;
1837 GVariant *param = NULL;
1839 BT_INFO("GATT Server MTU changed event");
1841 conn_info = __bt_find_remote_gatt_client_info_from_conn_id(event->conn_id);
1842 if (conn_info == NULL) {
1843 BT_ERR("Cant find connection Information");
1847 BT_INFO("Got connection Info GATT client [%s] MTU Size [%d]",
1848 conn_info->addr, event->mtu_size);
1850 param = g_variant_new("(isqy)",
1856 /* Send event to BT-API */
1857 _bt_send_event(BT_GATT_SERVER_EVENT,
1858 BLUETOOTH_EVENT_GATT_ATT_MTU_CHANGED,
1863 static void __bt_gatt_event_handler(int event_type, gpointer event_data)
1865 BT_INFO("OAL event = 0x%x, \n", event_type);
1866 switch (event_type) {
1867 case OAL_EVENT_BLE_SERVER_INSTANCE_INITIALISED: {
1868 BT_INFO("OAL Event: Server Instance Registered");
1869 event_gatts_register_t* event = g_memdup(event_data, sizeof(event_gatts_register_t));
1870 /* GATT Server Registered event is handled in MAIN thread context */
1871 __bt_handle_server_instance_registered(event);
1874 case OAL_EVENT_GATTS_SERVICE_ADDED: {
1875 BT_INFO("OAL Event: GATT Service added");
1876 event_gatts_srvc_prm_t *service_parm = g_memdup(event_data, sizeof(event_gatts_srvc_prm_t));
1877 __bt_handle_gatt_server_service_added(service_parm);
1880 case OAL_EVENT_GATTS_CHARACTERISTIC_ADDED: {
1881 BT_INFO("OAL Event: GATT characteristic added");
1882 event_gatts_srvc_charctr_t *char_parm = g_memdup(event_data, sizeof(event_gatts_srvc_charctr_t));
1883 __bt_handle_gatt_server_characteristic_added(char_parm);
1886 case OAL_EVENT_GATTS_DESCRIPTOR_ADDED: {
1887 BT_INFO("OAL Event: GATT descriptor added");
1888 event_gatts_srvc_descr_t *desc_parm = g_memdup(event_data, sizeof(event_gatts_srvc_descr_t));
1889 __bt_handle_gatt_server_descriptor_added(desc_parm);
1892 case OAL_EVENT_GATTS_SERVICE_STARTED: {
1893 BT_INFO("OAL Event: GATT Service started");
1894 event_gatts_srvc_t *svc_started = g_memdup(event_data, sizeof(event_gatts_srvc_t));
1895 __bt_handle_gatt_server_service_started(svc_started);
1898 case OAL_EVENT_GATTS_SERVICE_STOPED: {
1899 BT_INFO("OAL Event: GATT Service stopped");
1900 event_gatts_srvc_t *svc_stopped = g_memdup(event_data, sizeof(event_gatts_srvc_t));
1901 __bt_handle_gatt_server_service_stopped(svc_stopped);
1904 case OAL_EVENT_GATTS_SERVICE_DELETED: {
1905 BT_INFO("OAL Event: GATT Service deleted");
1906 event_gatts_srvc_t *svc_deleted = g_memdup(event_data, sizeof(event_gatts_srvc_t));
1907 __bt_handle_gatt_server_service_deleted(svc_deleted);
1910 case OAL_EVENT_GATTS_CONNECTION_COMPLETED: {
1911 BT_INFO("OAL Event: GATT Server Connected");
1912 event_gatts_conn_t* event = g_memdup(event_data, sizeof(event_gatts_conn_t));
1913 __bt_handle_gatt_server_connection_state(event);
1916 case OAL_EVENT_GATTS_DISCONNECTION_COMPLETED: {
1917 BT_INFO("OAL Event: GATT Server Disconnected");
1918 event_gatts_conn_t* event = g_memdup(event_data, sizeof(event_gatts_conn_t));
1919 __bt_handle_gatt_server_disconnection_state(event);
1922 case OAL_EVENT_GATTS_REQUEST_READ: {
1923 BT_INFO("OAL Event: GATT Server Read Request");
1924 event_gatts_srvc_read_attr_t *read_req = g_memdup(event_data, sizeof(event_gatts_srvc_read_attr_t));
1925 __bt_handle_gatt_server_read_requested(read_req);
1928 case OAL_EVENT_GATTS_REQUEST_WRITE: {
1929 BT_INFO("OAL Event: GATT Server Write Request");
1930 event_gatts_srvc_write_attr_t *write_req = g_memdup(event_data, sizeof(event_gatts_srvc_write_attr_t));
1931 __bt_handle_gatt_server_write_requested(write_req);
1934 case OAL_EVENT_GATTS_IND_CONFIRM: {
1935 BT_INFO("OAL Event: GATT Server Indication confirmed");
1936 event_gatts_ind_cnfrm_t *parm = g_memdup(event_data, sizeof(event_gatts_ind_cnfrm_t));
1937 __bt_handle_gatt_server_indicate_confirmed(parm);
1940 case OAL_EVENT_GATTS_NOTIFICATION: { /* Tizen Platform Specific */
1941 BT_INFO("OAL Event: GATT Server DisConnected");
1942 event_gatts_notif_t* notif = g_memdup(event_data, sizeof(event_gatts_notif_t));
1943 __bt_handle_gatt_server_notification_changed(notif);
1946 case OAL_EVENT_GATTS_MTU_CHANGED: {
1947 BT_INFO("OAL Event: GATT Server MTU changed event callback");
1948 event_gatts_mtu_changed_t *mtu_changed = g_memdup(event_data, sizeof(event_gatts_mtu_changed_t));
1949 __bt_handle_gatt_mtu_changed_event(mtu_changed);
1952 #ifdef TIZEN_GATT_CLIENT
1953 case OAL_EVENT_GATTC_REGISTRATION: {
1954 BT_INFO("OAL Event: GATT Client instance Registered");
1955 __bt_handle_client_instance_registered((event_gattc_register_t *) event_data);
1958 case OAL_EVENT_GATTC_CONNECTION_COMPLETED: {
1959 BT_INFO("OAL Event: GATT Client Connected");
1960 __bt_handle_client_connected((event_gattc_conn_t *) event_data);
1963 case OAL_EVENT_GATTC_DISCONNECTION_COMPLETED: {
1964 BT_INFO("OAL Event: GATT Client DisConnected");
1965 __bt_handle_client_disconnected((event_gattc_conn_t *) event_data);
1968 case OAL_EVENT_GATTC_SERVICE_SEARCH_RESULT: {
1969 BT_INFO("OAL Event: GATT Client Service Search Result");
1970 __bt_handle_client_service_search_result((event_gattc_service_result_t *) event_data);
1973 case OAL_EVENT_GATTC_SERVICE_SEARCH_DONE: {
1974 BT_INFO("OAL Event: GATT Client Service Completed");
1975 __bt_handle_client_service_search_completed((event_gattc_conn_status_t *) event_data);
1978 case OAL_EVENT_GATTC_CHARAC_SERACH_RESULT: {
1979 BT_INFO("OAL Event: GATT Client Characteristic Search Result");
1980 __bt_handle_client_characteristic_search_result((event_gattc_characteristic_result_t *) event_data);
1983 case OAL_EVENT_GATTC_DESC_SERACH_RESULT: {
1984 BT_INFO("OAL Event: GATT Client Descriptor Search Result");
1985 __bt_handle_client_descriptor_search_result((event_gattc_descriptor_result_t *) event_data);
1988 case OAL_EVENT_GATTC_READ_CHARAC: {
1989 BT_INFO("OAL Event: GATT Client Characteristic Read Data");
1990 __bt_handle_client_characteristic_read_data((event_gattc_read_data *) event_data);
1993 case OAL_EVENT_GATTC_READ_DESCR: {
1994 BT_INFO("OAL Event: GATT Client Descriptor Read Data");
1995 __bt_handle_client_descriptor_read_data((event_gattc_read_data *) event_data);
1998 case OAL_EVENT_GATTC_WRITE_CHARAC: {
1999 BT_INFO("OAL Event: GATT Client Characteristic Write Data");
2000 __bt_handle_client_characteristic_write_data((event_gattc_write_data *) event_data);
2003 case OAL_EVENT_GATTC_WRITE_DESCR: {
2004 BT_INFO("OAL Event: GATT Client Descriptor Write Data");
2005 __bt_handle_client_descriptor_write_data((event_gattc_write_data *) event_data);
2008 case OAL_EVENT_DEVICE_LE_DISCONNECTED: {
2009 BT_INFO("OAL Event: LE device disconnected");
2010 __bt_hanlde_le_device_disconnection((event_dev_conn_status_t *)event_data);
2019 int _bt_gatt_server_add_service(char *sender, int service_type,
2020 int num_handles, char *svc_uuid, int instance_id)
2022 BT_CHECK_PARAMETER(svc_uuid, return);
2023 BT_CHECK_PARAMETER(sender, return);
2024 int ret = OAL_STATUS_SUCCESS;
2026 oal_gatt_srvc_id_t svc_data;
2028 svc_data.is_prmry = service_type;
2029 svc_data.id.inst_id = instance_id;
2031 BT_INFO("Service UUID [%s] Num handles [%d] Instance ID [%d]", svc_uuid, num_handles, instance_id);
2032 _bt_string_to_uuid(svc_uuid, (service_uuid_t*)&svc_data.id.uuid);
2034 ret = gatts_add_service(instance_id, &svc_data, num_handles);
2035 if (ret != OAL_STATUS_SUCCESS) {
2036 BT_ERR("ret: %d", ret);
2037 return BLUETOOTH_ERROR_INTERNAL;
2040 return BLUETOOTH_ERROR_NONE;
2044 int _bt_gatt_server_add_included_service(char *sender, int instance_id,
2045 int service_handle, int included_svc_handle)
2047 BT_CHECK_PARAMETER(sender, return);
2048 int ret = OAL_STATUS_SUCCESS;
2050 ret = gatts_add_included_services(instance_id, service_handle, included_svc_handle);
2051 if (ret != OAL_STATUS_SUCCESS) {
2052 BT_ERR("ret: %d", ret);
2053 return BLUETOOTH_ERROR_INTERNAL;
2055 return BLUETOOTH_ERROR_NONE;
2058 int _bt_gatt_server_add_characteristic(char *sender, char *char_uuid,
2059 bluetooth_gatt_server_attribute_params_t *param)
2061 BT_CHECK_PARAMETER(char_uuid, return);
2062 BT_CHECK_PARAMETER(sender, return);
2063 BT_CHECK_PARAMETER(param, return);
2064 int ret = OAL_STATUS_SUCCESS;
2066 oal_uuid_t uuid = {{0} };
2068 BT_INFO("Char UUID [%s] Instance ID [%d]", char_uuid, param->instance_id);
2069 _bt_string_to_uuid(char_uuid, (service_uuid_t*)&uuid);
2071 BT_INFO("Char permission From API [0x%x]", param->permissions);
2073 ret = gatts_add_characteristics(param->instance_id, param->service_handle, &uuid,
2074 param->properties, (int)param->permissions);
2075 if (ret != OAL_STATUS_SUCCESS) {
2076 BT_ERR("ret: %d", ret);
2077 return BLUETOOTH_ERROR_INTERNAL;
2079 return BLUETOOTH_ERROR_NONE;
2082 int _bt_gatt_server_add_descriptor(char *sender, char *desc_uuid,
2083 bt_gatt_permission_t *param, int service_handle, int instance_id)
2085 BT_CHECK_PARAMETER(desc_uuid, return);
2086 BT_CHECK_PARAMETER(sender, return);
2087 BT_CHECK_PARAMETER(param, return);
2088 int ret = OAL_STATUS_SUCCESS;
2090 oal_uuid_t uuid = {{0} };
2092 BT_INFO("Descriptor UUID [%s] Instance ID [%d] Service handle [%d]",
2093 desc_uuid, service_handle, instance_id);
2095 _bt_string_to_uuid(desc_uuid, (service_uuid_t*)&uuid);
2097 BT_INFO("Descriptor permission From API [0x%x]", *param);
2098 ret = gatts_add_descriptor(instance_id, service_handle, &uuid, (int)*param);
2100 if (ret != OAL_STATUS_SUCCESS) {
2101 BT_ERR("ret: %d", ret);
2102 return BLUETOOTH_ERROR_INTERNAL;
2104 return BLUETOOTH_ERROR_NONE;
2107 int _bt_gatt_server_start_service(char *sender, int service_handle, int instance_id)
2109 BT_CHECK_PARAMETER(sender, return);
2110 int ret = OAL_STATUS_SUCCESS;
2112 ret = gatts_start_service(instance_id, service_handle, BT_GATT_TRANSPORT_LE);
2113 if (ret != OAL_STATUS_SUCCESS) {
2114 BT_ERR("ret: %d", ret);
2115 return BLUETOOTH_ERROR_INTERNAL;
2117 return BLUETOOTH_ERROR_NONE;
2120 int _bt_gatt_server_stop_service(char *sender, int service_handle, int instance_id)
2122 BT_CHECK_PARAMETER(sender, return);
2123 int ret = OAL_STATUS_SUCCESS;
2125 ret = gatts_stop_service(instance_id, service_handle);
2126 if (ret != OAL_STATUS_SUCCESS) {
2127 BT_ERR("ret: %d", ret);
2128 return BLUETOOTH_ERROR_INTERNAL;
2130 return BLUETOOTH_ERROR_NONE;
2133 int _bt_gatt_server_delete_service(char *sender, int service_handle, int instance_id)
2135 BT_CHECK_PARAMETER(sender, return);
2136 int ret = OAL_STATUS_SUCCESS;
2140 ret = gatts_delete_service(instance_id, service_handle);
2141 if (ret != OAL_STATUS_SUCCESS) {
2142 BT_ERR("ret: %d", ret);
2143 return BLUETOOTH_ERROR_INTERNAL;
2146 /* Remove the Service Handle */
2147 for (l = numapps[instance_id].service_handles; l != NULL;) {
2149 l = g_slist_next(l);
2150 if (handle && *handle == service_handle) {
2151 BT_INFO("Remove Service handle [%d]", *handle);
2152 numapps[instance_id].service_handles = g_slist_remove(numapps[instance_id].service_handles, handle);
2158 return BLUETOOTH_ERROR_NONE;
2161 int _bt_gatt_server_send_response(char *sender, bluetooth_gatt_att_data_t *data,
2162 bluetooth_gatt_server_response_params_t *param)
2164 BT_CHECK_PARAMETER(sender, return);
2165 BT_CHECK_PARAMETER(data, return);
2166 BT_CHECK_PARAMETER(param, return);
2167 struct gatt_server_req_info *req_info = NULL;
2168 int ret = OAL_STATUS_SUCCESS;
2170 oal_gatt_response_t response;
2172 BT_INFO("GATT Server Response: Req Type [%d] req_id [%d] status [%d] auth_req [%d] offset[%d] data len[%d]",
2173 param->req_type, param->request_id,
2174 param->response_status, param->auth_req,
2175 data->offset, data->length);
2177 /* Search for matching Request in List */
2178 req_info = __bt_gatt_server_find_request_info(param->request_id, param->req_type);
2180 BT_ERR("GATT Server Req Info not found for current response..return Error");
2181 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
2184 memset(&response, 0x00, sizeof(oal_gatt_response_t));
2186 response.handle = req_info->attribute_handle;
2187 response.attr_value.auth_req = param->auth_req;
2188 response.attr_value.handle = req_info->attribute_handle;
2189 response.attr_value.offset = data->offset;
2190 response.attr_value.len = data->length;
2191 memcpy(&response.attr_value.value, &data->data, data->length);
2194 ret = gatts_send_response(req_info->connection_id, param->request_id,
2195 param->response_status, &response);
2197 if (ret != OAL_STATUS_SUCCESS) {
2198 BT_ERR("ret: %d", ret);
2199 return BLUETOOTH_ERROR_INTERNAL;
2202 BT_INFO("GATT Server Response successfully sent");
2203 /* Remove GATT server request from list */
2204 gatt_server_requests = g_slist_remove(gatt_server_requests, req_info);
2205 g_free(req_info->addr);
2207 return BLUETOOTH_ERROR_NONE;
2210 int _bt_gatt_server_send_indication(char *sender, bluetooth_device_address_t *dev_addr,
2211 bluetooth_gatt_att_data_t *data,
2212 bluetooth_gatt_server_indication_params_t *param)
2214 BT_CHECK_PARAMETER(sender, return);
2215 BT_CHECK_PARAMETER(data, return);
2216 BT_CHECK_PARAMETER(param, return);
2218 gboolean all_send = FALSE;
2219 int ret = OAL_STATUS_SUCCESS;
2220 struct gatt_client_info_t *conn;
2222 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2223 _bt_convert_addr_type_to_string(address, dev_addr->addr);
2225 if (memcmp(dev_addr->addr, BDADDR_ANY, 6) == 0) {
2226 BT_INFO("GATT Server: Send Indication to all connected GATT clients..");
2229 BT_INFO("GATT Server: Send Indication to connected GATT client addr [%s]", address);
2232 /* Attempt to send Notification/Indication to all Connected GATT clients */
2234 ret = __bt_gatt_send_indication_to_all_connected_clients(data, param);
2235 if (ret != OAL_STATUS_SUCCESS) {
2236 BT_ERR("ret: %d", ret);
2238 return BLUETOOTH_ERROR_INTERNAL;
2242 conn = __bt_find_remote_gatt_client_info(address);
2244 ret = gatts_send_indication(param->instance_id, param->atrribute_handle,
2245 conn->connection_id, data->length,
2246 param->need_confirmation, (char *)(&data->data[0]));
2248 if (ret != OAL_STATUS_SUCCESS) {
2249 BT_ERR("ret: %d", ret);
2250 BT_INFO("Indication failed to send to Remote GATT Client [%s]", address);
2252 return BLUETOOTH_ERROR_INTERNAL;
2254 BT_INFO("Indication sent to Remote GATT Client [%s] wait for Notification completed event from OAL", address);
2256 num_indicate_clients = 1;
2257 return BLUETOOTH_ERROR_NONE;
2259 BT_ERR("Remote GATT client [%s] is not connected..Cant send Indication!!", address);
2261 return BLUETOOTH_ERROR_NOT_CONNECTED;
2264 return BLUETOOTH_ERROR_NONE;
2267 int _bt_gatt_server_update_attribute_value(char *sender, int instance_id,
2268 bluetooth_gatt_server_update_value_t *param)
2270 BT_CHECK_PARAMETER(sender, return);
2271 BT_CHECK_PARAMETER(param, return);
2272 int ret = OAL_STATUS_SUCCESS;
2274 oal_gatt_value_t value;
2275 BT_INFO("GATT Server Update value: Instance ID [%d] attr handle [%d] Value len [%d]",
2276 instance_id, param->attribute_handle, param->length);
2279 memset(&value, 0x00, sizeof(oal_gatt_value_t));
2281 value.handle = param->attribute_handle;
2282 value.len = param->length;
2283 memcpy(&value.value, ¶m->data.data, param->length);
2285 ret = gatts_update_att_value(instance_id, &value);
2287 if (ret != OAL_STATUS_SUCCESS) {
2288 BT_ERR("ret: %d", ret);
2289 return BLUETOOTH_ERROR_INTERNAL;
2292 BT_INFO("GATT Server Update Attribute Value successfully");
2293 return BLUETOOTH_ERROR_NONE;
2296 int _bt_get_att_mtu(bluetooth_device_address_t *address,
2299 BT_CHECK_PARAMETER(address, return);
2300 BT_CHECK_PARAMETER(mtu, return);
2301 struct gatt_client_info_t *conn_info = NULL;
2302 char addr[BT_ADDRESS_STRING_SIZE] = { 0 };
2303 int ret = OAL_STATUS_SUCCESS;
2306 _bt_convert_addr_type_to_string(addr, address->addr);
2308 BT_INFO("Get current MTU size for the remote client:DevAddress:[%s]", addr);
2310 conn_info = __bt_find_remote_gatt_client_info(addr);
2312 BT_INFO("GATT Client [%s] is connected, conn Id [%d] Instance ID [%d]",
2313 conn_info->addr, conn_info->connection_id, conn_info->instance_id);
2315 BT_ERR("GATT Client [%s] is not yet connected..");
2316 return BLUETOOTH_ERROR_NOT_CONNECTED;
2319 ret = gatts_get_att_mtu(conn_info->connection_id, &stack_mtu);
2320 if (ret != OAL_STATUS_SUCCESS) {
2321 BT_ERR("ret: %d", ret);
2322 return BLUETOOTH_ERROR_INTERNAL;
2324 BT_INFO("ATT MTU received from OAL [%d]", stack_mtu);
2325 *mtu = (unsigned int)stack_mtu;
2326 return BLUETOOTH_ERROR_NONE;
2329 #ifdef TIZEN_GATT_CLIENT
2330 static void __bt_convert_byte_ordering(char *data, int data_len,
2331 char *converted_data)
2334 char temp[BLUETOOTH_UUID_HEX_MAX_LEN];
2336 memcpy(&temp, data, data_len);
2337 for (i = 0, j = data_len - 1; i < data_len; i++, j--)
2338 converted_data[i] = temp[j];
2341 /* GATT Client utility static functions */
2342 static bt_gatt_service_info_list_t * __bt_get_service_info_list(int conn_id)
2345 bt_gatt_service_info_list_t *info = NULL;
2347 for (l = list_gatt_info; l != NULL; l = g_slist_next(l)) {
2348 info = (bt_gatt_service_info_list_t*)l->data;
2352 if (info->conn_id == conn_id)
2359 static bt_gatt_service_info_t* __bt_find_matching_service(
2360 bt_gatt_service_info_list_t *svc_list, oal_gatt_srvc_id_t *svc)
2363 bt_gatt_service_info_t *info = NULL;
2365 for (l = svc_list->services; l != NULL; l = g_slist_next(l)) {
2366 info = (bt_gatt_service_info_t*)l->data;
2370 /* Match UUID and instance ID */
2371 if (!memcmp(&svc->id.uuid.uuid, &info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
2372 && (svc->id.inst_id == info->inst_id)) {
2379 static bt_gatt_char_info_t* __bt_find_matching_charc(
2380 bt_gatt_service_info_t *svc_info, oal_gatt_id_t *charc)
2383 bt_gatt_char_info_t *info = NULL;
2385 for (l = svc_info->chars; l != NULL; l = g_slist_next(l)) {
2386 info = (bt_gatt_char_info_t*)l->data;
2390 /* Match UUID and instance ID */
2391 if (!memcmp(&charc->uuid.uuid, &info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
2392 && (charc->inst_id == info->inst_id)) {
2399 static bt_gatt_descriptor_info_t* __bt_find_matching_desc(
2400 bt_gatt_char_info_t *char_info, oal_gatt_id_t *desc)
2403 bt_gatt_descriptor_info_t *info = NULL;
2405 for (l = char_info->descs; l != NULL; l = g_slist_next(l)) {
2406 info = (bt_gatt_descriptor_info_t*)l->data;
2410 /* Match UUID and instance ID */
2411 if (!memcmp(&desc->uuid, &info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
2412 && (desc->inst_id == info->inst_id)) {
2420 static struct gatt_server_info_t *__bt_find_remote_gatt_server_info_from_conn_id(int conn_id)
2423 struct gatt_server_info_t *info = NULL;
2425 for (l = gatt_server_info_list; l != NULL; l = g_slist_next(l)) {
2426 info = (struct gatt_server_info_t*)l->data;
2430 if (info->connection_id == conn_id) {
2431 BT_INFO("Remote GATT server found addr[%s]", info->addr);
2438 static bt_gatt_service_info_t* __bt_find_removed_service(bt_gatt_service_info_list_t *svc_list)
2441 bt_gatt_service_info_t *info = NULL;
2443 for (l = svc_list->services; l != NULL; l = g_slist_next(l)) {
2444 info = (bt_gatt_service_info_t*)l->data;
2448 /* Service is marked a removed */
2449 if (info->is_removed == 1)
2455 static void __bt_remove_service_info_from_list(bt_gatt_service_info_t *svc_info)
2461 bt_gatt_char_info_t *charc = NULL;
2462 bt_gatt_included_service_info_t *incl = NULL;
2463 bt_gatt_descriptor_info_t *desc = NULL;
2465 /* Remove all Characteristic and Descriptors within characteristic */
2466 for (l = svc_info->chars; l != NULL;) {
2467 charc = (bt_gatt_char_info_t*)l->data;
2468 l = g_slist_next(l); /* Incase if l is removed, saving next to l */
2473 /* Inside Characteristic */
2474 for (l1 = charc->descs; l1 != NULL;) {
2476 desc = (bt_gatt_descriptor_info_t*)l1->data;
2477 l1 = g_slist_next(l1);
2482 /* Remove Descriptor */
2483 charc->descs = g_slist_remove(charc->descs, desc);
2486 /* Remove Characteristic */
2487 svc_info->chars = g_slist_remove(svc_info->chars, charc);
2491 /* Remove all Included Services */
2492 for (l2 = svc_info->included_svcs; l2 != NULL;) {
2493 incl = (bt_gatt_included_service_info_t*)l2->data;
2494 l2 = g_slist_next(l2); /* Incase if l is removed, saving next to l */
2499 /* Remove included service */
2500 svc_info->included_svcs = g_slist_remove(svc_info->included_svcs, incl);
2507 static void __bt_build_service_browse_info(int conn_id,
2508 bt_services_browse_info_t* info)
2511 bt_gatt_service_info_list_t *svc_info_list;
2512 bt_gatt_service_info_t *svc_info;
2514 service_uuid_t uuid;
2515 struct gatt_server_info_t *conn_info = NULL;
2517 char uuid_string[BLUETOOTH_UUID_STRING_MAX];
2520 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
2525 _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
2527 svc_info_list = __bt_get_service_info_list(conn_id);
2532 info->count = g_slist_length(svc_info_list->services);
2533 BT_INFO("Total services present in the svc info list for this conn id [%d] is [%d]",
2534 conn_id, info->count);
2536 for (l = svc_info_list->services; l != NULL; l = g_slist_next(l)) {
2537 svc_info = (bt_gatt_service_info_t*)l->data;
2538 if (svc_info == NULL)
2541 memcpy(&uuid.uuid, &svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2542 _bt_uuid_to_string(&uuid, uuid_string);
2544 BT_INFO("Service UUID formed [%s] strlen [%d]", uuid_string, strlen(uuid_string));
2546 /* Fill UUID of service */
2547 g_strlcpy(info->uuids[count], uuid_string,
2548 BLUETOOTH_UUID_STRING_MAX);
2550 BT_INFO("Service UUID formed TO be sent [%s] strlen [%d]",
2551 info->uuids[count], strlen(info->uuids[count]));
2552 /* Fill instance ID of service */
2553 info->inst_id[count] = svc_info->inst_id;
2555 /* Fill primary service or not info */
2556 info->primary[count] = svc_info->is_primary;
2558 /* Increment count of services browsed */
2562 BT_INFO("Total services browsed [%d]", count);
2565 static void __bt_build_char_browse_info(int conn_id,
2566 bt_gatt_service_info_t *svc_info,
2567 bt_char_browse_info_t* info)
2570 bt_gatt_char_info_t *char_info;
2571 service_uuid_t uuid;
2573 struct gatt_server_info_t *conn_info = NULL;
2575 char uuid_string[BLUETOOTH_UUID_STRING_MAX];
2579 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
2583 /* Fill default data, this will be required even in case of failure */
2584 _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
2585 memcpy(&info->svc_uuid, svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2586 info->svc_inst_id = svc_info->inst_id;
2588 if (!svc_info->chars) {
2589 BT_ERR("No Chars browsed for address [%s]", conn_info->addr);
2593 info->count = g_slist_length(svc_info->chars);
2594 BT_INFO("Total count of Characteristics [%d]", info->count);
2596 for (l = svc_info->chars; l != NULL; l = g_slist_next(l)) {
2597 char_info = (bt_gatt_char_info_t*)l->data;
2598 if (char_info == NULL)
2601 memcpy(&uuid.uuid, &char_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2602 _bt_uuid_to_string(&uuid, uuid_string);
2604 /* Fill UUID of characteristic */
2605 g_strlcpy(info->uuids[count], uuid_string,
2606 BLUETOOTH_UUID_STRING_MAX);
2608 /* Fill instance ID of characteristic */
2609 info->inst_id[count] = char_info->inst_id;
2611 /* Fill property of characteristic */
2612 info->props[count] = char_info->props;
2614 /* Increment count of services browsed */
2617 BT_INFO("Total characteristics browsed [%d]", count);
2620 static void __bt_build_descriptor_browse_info(int conn_id,
2621 bt_gatt_service_info_t *svc_info,
2622 bt_gatt_char_info_t *char_info,
2623 bt_descriptor_browse_info_t* info)
2626 bt_gatt_descriptor_info_t *desc_info;
2628 service_uuid_t uuid;
2629 struct gatt_server_info_t *conn_info = NULL;
2631 char uuid_string[BLUETOOTH_UUID_STRING_MAX];
2633 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
2635 /* Fill default data, this will be required even in case of failure */
2636 _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
2637 memcpy(&info->svc_uuid, svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2638 info->svc_inst_id = svc_info->inst_id;
2639 memcpy(&info->char_uuid, char_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2640 info->char_inst_id = char_info->inst_id;
2642 if (!char_info->descs) {
2643 BT_ERR("No Descriptors browsed for address [%s]", conn_info->addr);
2647 info->count = g_slist_length(char_info->descs);
2648 BT_INFO("Total count of Descriptors [%d]", info->count);
2650 for (l = char_info->descs; l != NULL; l = g_slist_next(l)) {
2651 desc_info = (bt_gatt_descriptor_info_t*)l->data;
2652 if (desc_info == NULL)
2655 memcpy(&uuid.uuid, &desc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2656 _bt_uuid_to_string(&uuid, uuid_string);
2658 /* Fill UUID of Descriptor */
2659 g_strlcpy(info->uuids[count], uuid_string,
2660 BLUETOOTH_UUID_STRING_MAX);
2662 /* Fill instance ID of Descriptor */
2663 info->inst_id[count] = desc_info->inst_id;
2665 /* Fill property of the parent characteristic of this descriptor */
2666 info->char_props_map = char_info->props;
2668 /* Increment count of Descriptor browsed */
2672 BT_INFO("Total descriptors browsed [%d]", count);
2676 static void __bt_cleanup_remote_services(struct gatt_server_info_t *conn_info)
2683 bt_gatt_service_info_list_t * svc_info_list = NULL;
2684 bt_gatt_service_info_t *svc = NULL;
2685 bt_gatt_char_info_t *chr = NULL;
2686 bt_gatt_descriptor_info_t *desc = NULL;
2687 bt_gatt_included_service_info_t *incl_svc = NULL;
2689 BT_INFO("Start Cleanup of all services");
2691 svc_info_list = __bt_get_service_info_list(conn_info->connection_id);
2692 if (!svc_info_list) {
2693 BT_INFO("Could not find Svc Info list for the connection ID [%d]",
2694 conn_info->connection_id);
2698 BT_INFO("Num Services [%d]", g_slist_length(svc_info_list->services));
2699 for (l = svc_info_list->services; l;) {
2700 svc = (bt_gatt_service_info_t*)l->data;
2701 l = g_slist_next(l);
2705 BT_INFO("Service info Is Prim[%d] Inst ID [%d]",
2706 svc->is_primary, svc->inst_id);
2707 BT_INFO("Num chars [%d]", g_slist_length(svc->chars));
2708 /* Delete all chars and its descriptors */
2709 for (ll = svc->chars; ll;) {
2710 chr = (bt_gatt_char_info_t*)ll->data;
2711 ll = g_slist_next(ll);
2715 BT_INFO("Num descs [%d]", g_slist_length(chr->descs));
2716 for (lll = chr->descs; lll;) {
2717 desc = (bt_gatt_descriptor_info_t *)lll->data;
2718 lll = g_slist_next(lll);
2721 chr->descs = g_slist_remove(chr->descs, desc);
2724 svc->chars = g_slist_remove(svc->chars, chr);
2728 BT_INFO("Num incl svcs [%d]", g_slist_length(svc->included_svcs));
2729 /* Delete all included services */
2730 for (llll = svc->included_svcs; llll;) {
2731 incl_svc = (bt_gatt_included_service_info_t*)llll->data;
2732 llll = g_slist_next(llll);
2733 if (incl_svc == NULL)
2736 svc->included_svcs = g_slist_remove(svc->included_svcs, incl_svc);
2739 svc_info_list->services = g_slist_remove(svc_info_list->services, svc);
2742 g_free(svc_info_list);
2743 BT_INFO("Cleanup of all services done");
2746 int _bt_register_gatt_client_instance(const char *sender,
2747 bluetooth_device_address_t *address)
2749 int ret = OAL_STATUS_SUCCESS;
2750 char *uuid_string = NULL;
2755 /* App should ensure that it should not send */
2756 BT_INFO("###Check on which instance GATT Client instance can be initialized....");
2757 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
2758 if (numapps[k].is_initialized == 1) {
2759 BT_INFO("Instance ID [%d] is already in use..Check next slot",
2760 numapps[k].instance_id);
2763 BT_INFO("Time to register GATT client instancer..UUID to be used is [%s] slot [%d]",
2764 uuid_list[slot-1], slot);
2770 BT_ERR("No Slot if free for GATT Client registration..");
2771 return BLUETOOTH_ERROR_REGISTRATION_FAILED;
2774 uuid_string = g_malloc0(BT_UUID_STRING_MAX);
2775 _bt_string_to_uuid(uuid_list[slot-1], (service_uuid_t*)&uuid);
2776 g_strlcpy(uuid_string, uuid_list[slot-1], BT_UUID_STRING_MAX);
2777 BT_INFO("Copied UUID string [%s]", uuid_string);
2779 /* Register GATT Client */
2780 ret = gattc_register(&uuid);
2781 if (ret != OAL_STATUS_SUCCESS) {
2782 BT_ERR("ret: %d", ret);
2783 g_free(uuid_string);
2784 return BLUETOOTH_ERROR_INTERNAL;
2787 BT_INFO("GATT Client registration call successfully accepted by OAL..wait for Instance Initialized event from OAL..");
2789 /* Return & wait for GATT Client Instance Initialization event */
2790 memset(numapps[slot].sender, 0x00, sizeof(numapps[slot].sender));
2791 memset(numapps[slot].uuid, 0x00, sizeof(numapps[slot].uuid));
2793 g_strlcpy(numapps[slot].sender, sender, sizeof(numapps[slot].sender));
2794 g_strlcpy(numapps[slot].uuid, uuid_string, sizeof(numapps[slot].uuid));
2796 /* Address is saved here. When event comes, sender + address are matched for replying pending
2797 request. It is impossible for same sender to have requests with two same addresses */
2798 memcpy(&numapps[slot].address.addr, address->addr, sizeof(bluetooth_device_address_t));
2800 BT_INFO("Slot [%d] occupied", slot);
2801 numapps[slot].is_initialized = TRUE; /* Set initialization to true here itself */
2803 g_free(uuid_string);
2804 return BLUETOOTH_ERROR_NONE;
2810 /* GATT client events */
2811 static void __bt_handle_client_instance_registered(event_gattc_register_t *data)
2813 bt_service_app_info_t *info = NULL;
2815 char *uuid_string = g_malloc0(BT_UUID_STRING_MAX);
2817 _bt_uuid_to_string(&(data->client_uuid), uuid_string);
2818 BT_INFO("CLient ID is Initialized [%d] UUID initialized [%s]", data->client_if, uuid_string);
2820 /* Platform GATT client framwork does not use Default GATT client instance
2821 This GATT client instance is never deregistred in the lifetime of bt-service */
2822 BT_INFO("Default UUID [%s] current registered uuid [%s]",
2823 DEFAULT_GATT_CLIENT_UUID, uuid_string);
2824 if (g_strcmp0(uuid_string, DEFAULT_GATT_CLIENT_UUID) == 0) {
2825 BT_INFO("Default client Instance Registered [%s] Client instance [%d]",
2826 uuid_string, data->client_if);
2827 gatt_default_client = data->client_if;
2828 g_free(uuid_string);
2832 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
2835 if (g_strcmp0(info->uuid, uuid_string) == 0) {
2836 BT_INFO("Found GATT client.. UUID [%s], sender [%s]", info->uuid, info->sender);
2837 BT_INFO("Slot [%d] occupied", k);
2838 info->is_initialized = TRUE;
2839 info->client_id = data->client_if;
2840 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE, BT_GATT_CLIENT_REGISTER,
2841 (void*)info, sizeof(bt_service_app_info_t));
2845 g_free(uuid_string);
2848 static void __bt_handle_client_connected(event_gattc_conn_t *event_data)
2850 int result = BLUETOOTH_ERROR_NONE;
2851 struct gatt_server_info_t *conn_info = NULL;
2852 struct gatt_out_conn_info_t *out_conn_info = NULL;
2854 GVariant *param = NULL;
2856 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2857 _bt_convert_addr_type_to_string(address,
2858 (unsigned char *)event_data->address.addr);
2860 if (event_data->status != OAL_STATUS_SUCCESS)
2861 result = BLUETOOTH_ERROR_INTERNAL;
2863 /* DBUS Return fo BT_CONNECT_LE for all the apps */
2864 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE, address,
2865 BT_ADDRESS_STRING_SIZE);
2867 BT_INFO("Local GATT Client Connected: Remote addr[%s] Client Interface [%d] Connection ID [%d] status[%d]",
2868 address, event_data->client_if, event_data->conn_id, event_data->status);
2870 if (result == BLUETOOTH_ERROR_NONE) {
2871 /* Check if device is already in connected list */
2872 conn_info = __bt_find_remote_gatt_server_info(address);
2875 /* Send event to BT-API */
2876 param = g_variant_new("(is)", result, address);
2877 _bt_send_event(BT_DEVICE_EVENT,
2878 BLUETOOTH_EVENT_GATT_CLIENT_CONNECTED, /* Local device is GATT client */
2881 /* Save Connection info */
2882 conn_info = g_new0(struct gatt_server_info_t, 1);
2883 conn_info->addr = g_strdup(address);
2884 conn_info->client_id = event_data->client_if;
2885 BT_INFO("Added GATT server addr[%s]", conn_info->addr);
2886 conn_info->connection_id = event_data->conn_id;
2887 gatt_server_info_list = g_slist_append(gatt_server_info_list, conn_info);
2888 BT_INFO("Total num of connected Remote GATT server devices [%d]",
2889 g_slist_length(gatt_server_info_list));
2892 BT_INFO("Do a Internal refresh");
2893 if (OAL_STATUS_SUCCESS != gattc_refresh(conn_info->client_id, &event_data->address))
2894 BT_ERR("GATT database refresh failed!!");
2896 BT_INFO("GATT database refresh Success!!");
2899 BT_ERR("Local GATT Client connected event for addr[%s], but device is in connected list already", address);
2901 BT_ERR("GATT Client Connection failed!!");
2903 /* If outgoing connection Info is present, then remove it */
2904 out_conn_info = __bt_find_gatt_outgoing_conn_info(address);
2905 if (out_conn_info) {
2906 BT_ERR("Outgoing Client connect request was sent");
2907 outgoing_gatt_conn_list = g_slist_remove(outgoing_gatt_conn_list, out_conn_info);
2908 g_free(out_conn_info->addr);
2909 g_free(out_conn_info);
2914 static void __bt_handle_client_disconnected(event_gattc_conn_t *event_data)
2916 int result = BLUETOOTH_ERROR_NONE;
2918 struct gatt_server_info_t *conn_info = NULL;
2919 struct gatt_out_conn_info_t *out_conn_info = NULL;
2921 GVariant *param = NULL;
2923 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2924 _bt_convert_addr_type_to_string(address,
2925 (unsigned char *)event_data->address.addr);
2927 if (event_data->status != OAL_STATUS_SUCCESS)
2928 result = BLUETOOTH_ERROR_INTERNAL;
2930 if (NULL == _bt_get_request_info_data(BT_DISCONNECT_LE, address)) {
2931 if (NULL != _bt_get_request_info_data(BT_CONNECT_LE, address)) {
2932 result = BLUETOOTH_ERROR_INTERNAL;
2933 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
2934 address, BT_ADDRESS_STRING_SIZE);
2935 BT_ERR("Failed to connect Local GATT Remote addr[%s]", address);
2939 /* DBUS Return for BT_DISCONNECT_LE for all the apps */
2940 __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE, address,
2941 BT_ADDRESS_STRING_SIZE);
2943 BT_INFO("Local GATT Client DisConnected: Remote addr[%s] Client Interface [%d] Connection ID [%d] status [%d]",
2944 address, event_data->client_if, event_data->conn_id, event_data->status);
2946 /* Remove Connection info */
2947 conn_info = __bt_find_remote_gatt_server_info(address);
2950 param = g_variant_new("(is)", result, address);
2951 /* Send event to application */
2952 _bt_send_event(BT_DEVICE_EVENT,
2953 BLUETOOTH_EVENT_GATT_CLIENT_DISCONNECTED,
2956 BT_INFO("Remove GATT server info from List..");
2957 /* Remove all services from info list_gatt_info */
2958 __bt_cleanup_remote_services(conn_info);
2960 /* Remove info from List */
2961 gatt_server_info_list = g_slist_remove(gatt_server_info_list, conn_info);
2962 BT_INFO("Total num of connected GATT servers [%d]", g_slist_length(gatt_server_info_list));
2963 g_free(conn_info->addr);
2966 BT_INFO("Can not find conn info, already removed!");
2968 /* If outgoing connection Info is present, then remove it */
2969 out_conn_info = __bt_find_gatt_outgoing_conn_info(address);
2970 if (out_conn_info) {
2971 BT_ERR("Client Disconnected event, but outgoing connect request was sent");
2972 outgoing_gatt_conn_list = g_slist_remove(outgoing_gatt_conn_list, out_conn_info);
2973 g_free(out_conn_info->addr);
2974 g_free(out_conn_info);
2980 static void __bt_handle_client_service_search_result(
2981 event_gattc_service_result_t *event_data)
2984 BT_INFO("received the gatt service search result");
2985 /* Pre: status is never fail from OAL */
2987 /* Find service list from address */
2988 bt_gatt_service_info_list_t *svc_info_list;
2989 bt_gatt_service_info_t *svc_info;
2990 BT_INFO("Search Result: status [%d] conn_id [%d]",
2991 event_data->conn_status.status,
2992 event_data->conn_status.conn_id);
2994 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
2995 if (!svc_info_list) {
2996 BT_INFO("Service info list not present for this connection ID, means first time browse");
2997 /* Means for this conn_id, no services are ever browsed, first time,
2998 create service info list for this conn_id */
2999 svc_info_list = g_malloc0(sizeof(bt_gatt_service_info_list_t));
3000 svc_info_list->conn_id = event_data->conn_status.conn_id;
3001 list_gatt_info = g_slist_append(list_gatt_info, svc_info_list);
3003 BT_INFO("Service info list Already present for this connection ID, means not first time browse for this conn ID ");
3006 /* send list and current service's uuid and instance id to find it */
3007 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3010 /* If not found, check if service changed, if yes, means this is a new service added
3011 in remote GATT device, update uuid info in svc info list structure, to be used when
3012 search is completed */
3014 BT_INFO("Service Not found in svc info list for this connection ID");
3015 if (svc_info_list->info.is_changed) {
3016 BT_INFO("Service Changed indication already found for this connection ID");
3017 memcpy(svc_info_list->info.uuid, event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3019 /* Create and add new service in service list */
3020 svc_info = g_malloc0(sizeof(bt_gatt_service_info_t));
3021 memcpy(svc_info->uuid, event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3022 svc_info->inst_id = event_data->srvc_id.id.inst_id;
3023 svc_info->is_primary = event_data->srvc_id.is_prmry;
3024 svc_info_list->services = g_slist_append(svc_info_list->services, svc_info);
3025 BT_INFO("Service created and added in Svc info listf or this connection ID");
3027 BT_INFO("Service Already found to be present inside the svc info list for this connection ID");
3028 /* If returned matching service info, then just update service_rmeoved value inside it to 0 */
3029 svc_info->is_removed = 0;
3033 static void __bt_handle_client_service_search_completed(
3034 event_gattc_conn_status_t *event_data)
3037 struct gatt_server_info_t *conn_info = NULL;
3038 bt_gatt_service_info_list_t *svc_info_list;
3039 bt_gatt_service_info_t *svc_info;
3040 bt_services_browse_info_t browse_info;
3041 unsigned char uuid_empty[BLUETOOTH_UUID_HEX_MAX_LEN];
3043 memset(&uuid_empty, 0x00, BLUETOOTH_UUID_HEX_MAX_LEN);
3044 memset(&browse_info, 0x00, sizeof(bt_services_browse_info_t));
3045 BT_INFO("Primary Services browsing completed status[%d] conn ID [%d]",
3046 event_data->status, event_data->conn_id);
3048 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(event_data->conn_id);
3050 svc_info_list = __bt_get_service_info_list(event_data->conn_id);
3051 if (!svc_info_list) {
3052 BT_ERR("No services browsed ever for addr [%s]", conn_info->addr);
3054 /* Just build response and return ERROR */
3055 __bt_build_service_browse_info(event_data->conn_id, &browse_info);
3057 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_INTERNAL,
3058 BT_GATT_GET_PRIMARY_SERVICES, &browse_info,
3059 sizeof(bt_services_browse_info_t));
3063 /* If fail, then send event with error */
3064 if (event_data->status != OAL_STATUS_SUCCESS) {
3065 /* Just build response and return ERROR */
3066 __bt_build_service_browse_info(event_data->conn_id, &browse_info);
3068 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_INTERNAL,
3069 BT_GATT_GET_PRIMARY_SERVICES, &browse_info,
3070 sizeof(bt_services_browse_info_t));
3074 /* If success, then find service info list from address */
3076 /* If svc_changed == 1 and uuid valid, means a new service is added*/
3077 if (svc_info_list->info.is_changed && !memcmp(uuid_empty, svc_info_list->info.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)) {
3078 /* TODO: Send event -Service added with instance ID and UUID of newly added service */
3079 BT_INFO("new service added");
3081 BT_INFO("TODO new service added");
3084 /* If svc_changed == 1 and uuid invalid, then a service is removed */
3085 if (svc_info_list->info.is_changed && memcmp(uuid_empty, svc_info_list->info.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)) {
3086 /* Scan through the service info list to find service with is_removed = 1*/
3087 svc_info = __bt_find_removed_service(svc_info_list);
3089 /* TODO Send event - Service removed with instance ID and UUID of just rmeoved service */
3091 /* Remove that service info from service info list */
3092 svc_info_list->services = g_slist_remove(svc_info_list->services, svc_info);
3094 /* Delete that service completely from svc_info list*/
3095 __bt_remove_service_info_from_list(svc_info);
3099 /* Reset svc_changed = 0, and reset UUID = all 0's */
3100 svc_info_list->info.is_changed = 0;
3101 memset(&svc_info_list->info.uuid, 0x00, BLUETOOTH_UUID_HEX_MAX_LEN);
3103 /* Build Reply and send to service browse primary services request of pending apps */
3104 __bt_build_service_browse_info(event_data->conn_id, &browse_info);
3106 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE,
3107 BT_GATT_GET_PRIMARY_SERVICES, &browse_info,
3108 sizeof(bt_services_browse_info_t));
3112 static void __bt_handle_client_characteristic_search_result(
3113 event_gattc_characteristic_result_t *event_data)
3115 bt_gatt_service_info_list_t *svc_info_list;
3116 bt_gatt_service_info_t *svc_info;
3117 bt_gatt_char_info_t *char_info;
3118 bt_char_browse_info_t browse_info;
3120 BT_INFO("Characteristic search result status [%d]",
3121 event_data->conn_status.status);
3123 memset(&browse_info, 0x00, sizeof(bt_char_browse_info_t));
3126 if (event_data->conn_status.status == OAL_STATUS_SUCCESS) {
3127 /* Find service info list from address */
3128 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3129 if (svc_info_list == NULL) {
3130 BT_ERR("svc_info_list is NULL");
3135 /* Find matching service info from svc info list */
3136 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3137 if (svc_info == NULL) {
3138 BT_ERR("svc_info is NULL");
3142 /* Find Matching char from service info in event */
3143 char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
3144 if (char_info == NULL)
3145 BT_ERR("char_info is NULL");
3147 /* If not found, then add new characteristic and return */
3149 BT_INFO(" add new characteristic");
3150 char_info = g_malloc0(sizeof(bt_gatt_char_info_t));
3151 memcpy(char_info->uuid, event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3153 char_info->inst_id = event_data->char_id.inst_id;
3154 char_info->props = event_data->char_prop;
3155 svc_info->chars = g_slist_append(svc_info->chars, char_info);
3157 /* If found, then return */
3158 BT_INFO("Characteristic browsed is already presesnt");
3161 /* If Not success: Means Charc browse is completed */
3162 /* Find char list from service in event */
3163 /* Find service list from address */
3164 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3166 /* Find service info from service in event */
3167 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3169 /* Build char list from service in event */
3170 __bt_build_char_browse_info(event_data->conn_status.conn_id,
3171 svc_info, &browse_info);
3173 /* Create response and return by sending event*/
3174 /* Build Reply and send to service browse All Included services request of pending apps */
3175 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE,
3176 BT_GATT_GET_SERVICE_PROPERTIES,
3178 sizeof(bt_char_browse_info_t));
3182 static void __bt_handle_client_descriptor_search_result(
3183 event_gattc_descriptor_result_t *event_data)
3185 bt_gatt_service_info_list_t *svc_info_list;
3186 bt_gatt_service_info_t *svc_info;
3187 bt_gatt_char_info_t *char_info;
3188 bt_gatt_descriptor_info_t *desc_info;
3189 bt_descriptor_browse_info_t browse_info;
3191 BT_INFO("descriptor search result status [%d]", event_data->conn_status.status);
3193 memset(&browse_info, 0x00, sizeof(bt_descriptor_browse_info_t));
3196 if (event_data->conn_status.status == OAL_STATUS_SUCCESS) {
3197 /* Find service list from address */
3198 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3199 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3200 char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
3201 desc_info = __bt_find_matching_desc(char_info, &event_data->descr_id);
3203 /* If not found, add new descriptor and return */
3205 desc_info = g_malloc0(sizeof(bt_gatt_descriptor_info_t));
3206 memcpy(desc_info->uuid, event_data->descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3208 desc_info->inst_id = event_data->descr_id.inst_id;
3209 char_info->descs = g_slist_append(char_info->descs, desc_info);
3212 /* If found, then return */
3213 BT_INFO("Characteristic browsed is already presesnt");
3216 /* If Not success */
3217 /* Find service list from address */
3218 /* Find included service list from service in event */
3219 /* Create response and return by sending event*/
3220 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3222 /* Find service info from service in event */
3223 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3225 /* Find char info from char in event */
3226 char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
3228 /* Build descriptor list from char in event */
3229 __bt_build_descriptor_browse_info(event_data->conn_status.conn_id,
3230 svc_info, char_info, &browse_info);
3233 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE,
3234 BT_GATT_GET_CHARACTERISTIC_PROPERTIES,
3236 sizeof(bt_descriptor_browse_info_t));
3240 static void __bt_handle_client_characteristic_read_data(
3241 event_gattc_read_data *event_data)
3243 int result = BLUETOOTH_ERROR_NONE;
3244 struct gatt_server_info_t *conn_info = NULL;
3245 bluetooth_gatt_client_char_prop_info_t read_info;
3247 /* Read Information data structures */
3248 GVariant *param = NULL;
3249 GVariant *data = NULL;
3250 GVariant *data_svc_uuid = NULL;
3251 GVariant *data_char_uuid = NULL;
3252 char *read_val = NULL;
3253 char *svc_uuid = NULL;
3254 char *char_uuid = NULL;
3260 //memset(&read_info, 0x00, sizeof(bt_gatt_handle_property_t));
3261 memset(&read_info, 0x00, sizeof(bluetooth_gatt_client_char_prop_info_t));
3263 /* Extract Address from conn_id of event data */
3264 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3265 event_data->uuid_status.conn_status.conn_id);
3267 BT_INFO("Characteristic Read result from addr [%s] status [%d]",
3268 conn_info->addr, event_data->uuid_status.conn_status.status);
3270 /* Fill char in buffer */
3271 memcpy(&read_info.characteristic.uuid,
3272 event_data->uuid_status.char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3273 read_info.characteristic.instance_id = event_data->uuid_status.char_id.inst_id;
3275 /* Fill Service in buffer */
3276 memcpy(&read_info.svc.uuid,
3277 event_data->uuid_status.srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3278 read_info.svc.instance_id = event_data->uuid_status.srvc_id.id.inst_id;
3280 /* Fill remote device address */
3281 _bt_convert_addr_string_to_type(read_info.device_address.addr, conn_info->addr);
3283 /* Fill data and reply to all apps waiting for Read result on the same characteristic
3284 Note: Even in case of failure, address, handles and result code should be returned */
3285 if (event_data->uuid_status.conn_status.status != OAL_STATUS_SUCCESS)
3286 result = BLUETOOTH_ERROR_INTERNAL;
3288 if (event_data->data_len > 0) {
3290 for (i = 0; i < event_data->data_len; i++)
3291 BT_INFO("Data[%d] = [0x%x]", i, event_data->data[i]);
3294 read_val = g_memdup(&event_data->data[0], event_data->data_len);
3296 data = g_variant_new_from_data(
3297 G_VARIANT_TYPE_BYTESTRING,
3299 event_data->data_len,
3302 svc_uuid = g_memdup(&event_data->uuid_status.srvc_id.id.uuid.uuid[0], uuid_len);
3304 data_svc_uuid = g_variant_new_from_data(
3305 G_VARIANT_TYPE_BYTESTRING,
3311 char_uuid = g_memdup(&event_data->uuid_status.char_id.uuid.uuid[0], uuid_len);
3313 data_char_uuid = g_variant_new_from_data(
3314 G_VARIANT_TYPE_BYTESTRING,
3318 param = g_variant_new("(isn@ayin@ayin@ay)", result,
3322 event_data->uuid_status.srvc_id.id.inst_id,
3325 event_data->uuid_status.char_id.inst_id,
3326 event_data->data_len,
3330 _bt_send_event(BT_GATT_CLIENT_EVENT,
3331 BLUETOOTH_EVENT_GATT_READ_CHAR,
3334 BT_ERR("Characteristic Read success, but no data!!!");
3335 result = BLUETOOTH_ERROR_INTERNAL;
3338 /* Send DBUS return */
3339 __bt_gatt_handle_pending_request_info(result,
3340 BT_GATT_READ_CHARACTERISTIC,
3342 sizeof(bluetooth_gatt_client_char_prop_info_t));
3353 static void __bt_handle_client_descriptor_read_data(
3354 event_gattc_read_data *event_data)
3356 int result = BLUETOOTH_ERROR_NONE;
3357 struct gatt_server_info_t *conn_info = NULL;
3358 bluetooth_gatt_client_desc_prop_info_t read_info;
3360 /* Read Information data structures */
3361 GVariant *param = NULL;
3362 GVariant *data = NULL;
3363 GVariant *data_svc_uuid = NULL;
3364 GVariant *data_char_uuid = NULL;
3365 GVariant *data_desc_uuid = NULL;
3366 char *read_val = NULL;
3367 char *svc_uuid = NULL;
3368 char *char_uuid = NULL;
3369 char *desc_uuid = NULL;
3374 memset(&read_info, 0x00, sizeof(bluetooth_gatt_client_desc_prop_info_t));
3376 /* Extract Address from conn_id of event data */
3377 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3378 event_data->uuid_status.conn_status.conn_id);
3380 BT_INFO("Descriptor Read result from addr [%s] status [%d]",
3381 conn_info->addr, event_data->uuid_status.conn_status.status);
3383 /* Fill descriptor informations in buffer */
3384 memcpy(&read_info.descriptor.uuid,
3385 event_data->uuid_status.descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3386 read_info.descriptor.instance_id = event_data->uuid_status.descr_id.inst_id;
3388 /* Fill Characteristic informations in buffer */
3389 memcpy(&read_info.characteristic.uuid,
3390 event_data->uuid_status.char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3391 read_info.characteristic.instance_id = event_data->uuid_status.char_id.inst_id;
3393 /* Fill Service informations in buffer */
3394 memcpy(&read_info.svc.uuid,
3395 event_data->uuid_status.srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3396 read_info.svc.instance_id = event_data->uuid_status.srvc_id.id.inst_id;
3398 /* Fill remote device address */
3399 _bt_convert_addr_string_to_type(read_info.device_address.addr, conn_info->addr);
3401 /* Fill data and reply to all apps waiting for Read result on the same characteristic */
3402 if (event_data->uuid_status.conn_status.status != OAL_STATUS_SUCCESS)
3403 result = BLUETOOTH_ERROR_INTERNAL;
3405 if (event_data->data_len > 0) {
3407 for (i = 0; i < event_data->data_len; i++)
3408 BT_DBG("Data[%d] = [0x%x]", i, event_data->data[i]);
3411 read_val = g_memdup(&event_data->data[0], event_data->data_len);
3413 data = g_variant_new_from_data(
3414 G_VARIANT_TYPE_BYTESTRING,
3416 event_data->data_len,
3419 svc_uuid = g_memdup(&event_data->uuid_status.srvc_id.id.uuid.uuid[0], uuid_len);
3421 data_svc_uuid = g_variant_new_from_data(
3422 G_VARIANT_TYPE_BYTESTRING,
3428 char_uuid = g_memdup(&event_data->uuid_status.char_id.uuid.uuid[0], uuid_len);
3430 data_char_uuid = g_variant_new_from_data(
3431 G_VARIANT_TYPE_BYTESTRING,
3437 desc_uuid = g_memdup(&event_data->uuid_status.descr_id.uuid.uuid[0], uuid_len);
3439 data_desc_uuid = g_variant_new_from_data(
3440 G_VARIANT_TYPE_BYTESTRING,
3445 param = g_variant_new("(isn@ayin@ayin@ayin@ay)", result,
3449 event_data->uuid_status.srvc_id.id.inst_id,
3452 event_data->uuid_status.char_id.inst_id,
3455 event_data->uuid_status.descr_id.inst_id,
3456 event_data->data_len,
3460 _bt_send_event(BT_GATT_CLIENT_EVENT,
3461 BLUETOOTH_EVENT_GATT_READ_DESC,
3464 BT_ERR("Descriptor Read success, but no data!!!");
3465 result = BLUETOOTH_ERROR_INTERNAL;
3468 BT_INFO("Send DBUS rpely for GATT Read Descriptor");
3469 /* Send DBUS return */
3470 __bt_gatt_handle_pending_request_info(result,
3471 BT_GATT_READ_DESCRIPTOR_VALUE,
3473 sizeof(bluetooth_gatt_client_desc_prop_info_t));
3484 static void __bt_handle_client_characteristic_write_data(
3485 event_gattc_write_data *event_data)
3487 int result = BLUETOOTH_ERROR_NONE;
3488 struct gatt_server_info_t *conn_info = NULL;
3489 bluetooth_gatt_client_char_prop_info_t write_info;
3491 /* Read Information data structures */
3492 GVariant *param = NULL;
3493 GVariant *data_svc_uuid = NULL;
3494 GVariant *data_char_uuid = NULL;
3495 char *svc_uuid = NULL;
3496 char *char_uuid = NULL;
3500 memset(&write_info, 0x00, sizeof(bluetooth_gatt_client_char_prop_info_t));
3502 /* Extract Address from conn_id of event data */
3503 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3504 event_data->conn_status.conn_id);
3506 BT_INFO("Characteristic Write callback from addr [%s] status [%d]",
3507 conn_info->addr, event_data->conn_status.status);
3509 /* Fill char in buffer */
3510 memcpy(&write_info.characteristic.uuid,
3511 event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3512 write_info.characteristic.instance_id = event_data->char_id.inst_id;
3514 /* Fill Service in buffer */
3515 memcpy(&write_info.svc.uuid,
3516 event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3517 write_info.svc.instance_id = event_data->srvc_id.id.inst_id;
3519 /* Fill remote device address */
3520 _bt_convert_addr_string_to_type(write_info.device_address.addr, conn_info->addr);
3522 if (event_data->conn_status.status != OAL_STATUS_SUCCESS) {
3523 result = BLUETOOTH_ERROR_INTERNAL;
3529 svc_uuid = g_memdup(&event_data->srvc_id.id.uuid.uuid[0], uuid_len);
3531 data_svc_uuid = g_variant_new_from_data(
3532 G_VARIANT_TYPE_BYTESTRING,
3538 char_uuid = g_memdup(&event_data->char_id.uuid.uuid[0], uuid_len);
3540 data_char_uuid = g_variant_new_from_data(
3541 G_VARIANT_TYPE_BYTESTRING,
3546 param = g_variant_new("(isn@ayin@ayi)", result,
3550 event_data->srvc_id.id.inst_id,
3553 event_data->char_id.inst_id);
3556 _bt_send_event(BT_GATT_CLIENT_EVENT,
3557 BLUETOOTH_EVENT_GATT_WRITE_CHAR,
3566 /* Send DBUS return */
3567 __bt_gatt_handle_pending_request_info(result,
3568 BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE,
3570 sizeof(bluetooth_gatt_client_char_prop_info_t));
3574 static void __bt_handle_client_descriptor_write_data(
3575 event_gattc_write_data *event_data)
3577 int result = BLUETOOTH_ERROR_NONE;
3578 struct gatt_server_info_t *conn_info = NULL;
3579 bluetooth_gatt_client_desc_prop_info_t write_info;
3581 /* Write Information data structures */
3582 GVariant *param = NULL;
3583 GVariant *data_svc_uuid = NULL;
3584 GVariant *data_char_uuid = NULL;
3585 GVariant *data_desc_uuid = NULL;
3586 char *svc_uuid = NULL;
3587 char *char_uuid = NULL;
3588 char *desc_uuid = NULL;
3592 memset(&write_info, 0x00, sizeof(bluetooth_gatt_client_desc_prop_info_t));
3594 /* Extract Address from conn_id of event data */
3595 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3596 event_data->conn_status.conn_id);
3598 if (NULL == conn_info) {
3600 BT_INFO("Failed to get the conn info for conn_id [%d]", event_data->conn_status.conn_id);
3604 BT_INFO("Descriptor Write callback from addr [%s] status [%d]",
3605 conn_info->addr, event_data->conn_status.status);
3607 /* Fill descriptor informations in buffer */
3608 memcpy(&write_info.descriptor.uuid,
3609 event_data->descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3610 write_info.descriptor.instance_id = event_data->descr_id.inst_id;
3612 /* Fill Characteristic informations in buffer */
3613 memcpy(&write_info.characteristic.uuid,
3614 event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3615 write_info.characteristic.instance_id = event_data->char_id.inst_id;
3617 /* Fill Service informations in buffer */
3618 memcpy(&write_info.svc.uuid,
3619 event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3620 write_info.svc.instance_id = event_data->srvc_id.id.inst_id;
3622 /* Fill remote device address */
3623 _bt_convert_addr_string_to_type(write_info.device_address.addr, conn_info->addr);
3625 if (event_data->conn_status.status != OAL_STATUS_SUCCESS) {
3626 result = BLUETOOTH_ERROR_INTERNAL;
3632 svc_uuid = g_memdup(&event_data->srvc_id.id.uuid.uuid[0], uuid_len);
3634 data_svc_uuid = g_variant_new_from_data(
3635 G_VARIANT_TYPE_BYTESTRING,
3641 char_uuid = g_memdup(&event_data->char_id.uuid.uuid[0], uuid_len);
3643 data_char_uuid = g_variant_new_from_data(
3644 G_VARIANT_TYPE_BYTESTRING,
3650 desc_uuid = g_memdup(&event_data->descr_id.uuid.uuid[0], uuid_len);
3652 data_desc_uuid = g_variant_new_from_data(
3653 G_VARIANT_TYPE_BYTESTRING,
3658 param = g_variant_new("(isn@ayin@ayin@ayi)", result,
3662 event_data->srvc_id.id.inst_id,
3665 event_data->char_id.inst_id,
3668 event_data->descr_id.inst_id);
3671 _bt_send_event(BT_GATT_CLIENT_EVENT,
3672 BLUETOOTH_EVENT_GATT_WRITE_DESC,
3683 /* Send DBUS return */
3684 __bt_gatt_handle_pending_request_info(result,
3685 BT_GATT_WRITE_DESCRIPTOR_VALUE,
3687 sizeof(bluetooth_gatt_client_desc_prop_info_t));
3690 static void __bt_hanlde_le_device_disconnection(event_dev_conn_status_t *event_data)
3692 int result = BLUETOOTH_ERROR_INTERNAL;
3693 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
3695 _bt_convert_addr_type_to_string(address, (unsigned char *)event_data->address.addr);
3697 /* DBUS Return with fail of pending BT_CONNECT_LE for all the apps */
3698 BT_INFO("Local GATT Client disconnected: Remote addr[%s] ", address);
3700 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE, address,
3701 BT_ADDRESS_STRING_SIZE);
3706 gboolean _bt_is_remote_gatt_device_connected(bluetooth_device_address_t *address)
3709 struct gatt_server_info_t *conn_info = NULL;
3710 gboolean connected = FALSE;
3713 addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
3714 _bt_convert_addr_type_to_string(addr,
3715 (unsigned char *)&(address->addr));
3717 BT_INFO("Check Connected or not for [%s]", addr);
3718 /* Check if device is already in connected list */
3719 conn_info = __bt_find_remote_gatt_server_info(addr);
3722 BT_INFO("Remote GATT Server device [%s] is Connected", conn_info->addr);
3725 BT_INFO("Remote GATT Server Device [%s] is not Connected", addr);
3731 int _bt_connect_le_device(bluetooth_device_address_t *address,
3732 int auto_connect, int client_id)
3734 struct gatt_server_info_t *conn_info = NULL;
3735 struct gatt_out_conn_info_t *out_conn_info = NULL;
3737 invocation_info_t *req_info = NULL;
3738 int ret = OAL_STATUS_SUCCESS;
3740 char *remote_address = NULL;
3742 BT_CHECK_PARAMETER(address, return);
3746 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
3747 _bt_convert_addr_type_to_string(addr, address->addr);
3748 BT_INFO("GATT Client connect request for address [%s] client instance [%d]",
3752 /* Check if Remote Device is already under connection progress */
3753 req_info = _bt_get_request_info_data_from_function_name(BT_CONNECT_LE);
3755 remote_address = (char*)req_info->user_data;
3756 if (remote_address && !strcasecmp(remote_address, addr)) {/* Address matched */
3757 BT_INFO("Already Connection ongoing for same remote GATT Server address [%s]", remote_address);
3758 /* Return and wait for events to be sent to all apps */
3760 return BLUETOOTH_ERROR_IN_PROGRESS;
3764 /* Check if remote GATT Server is connected or not */
3765 conn_info = __bt_find_remote_gatt_server_info(addr);
3767 BT_ERR("GATT Server is already connected..");
3769 return BLUETOOTH_ERROR_ALREADY_CONNECT;
3772 /* TODO Check Requirement of holding Advertisement before initiating LE connect */
3774 /* Check if app sent 0 client id for connection, in such case, use default gatt client ID */
3775 if (client_id == 0) {
3776 BT_INFO("GATT CLient connect request sent by an app without any client instance [%d]",
3778 BT_INFO("Assign default GATT client id [%d]", gatt_default_client);
3779 client_id = gatt_default_client;
3782 BT_INFO("Connect using CLient ID [%d]", client_id);
3783 ret = gattc_connect(client_id, (bt_address_t*)(address), auto_connect);
3785 if (ret != OAL_STATUS_SUCCESS) {
3786 BT_ERR("ret: %d", ret);
3788 return BLUETOOTH_ERROR_INTERNAL;
3791 /* Mark this as outgoing connection */
3792 out_conn_info = g_new0(struct gatt_out_conn_info_t, 1);
3793 out_conn_info->addr = g_strdup(addr);
3794 out_conn_info->client_id = client_id;
3795 BT_INFO("Added outgoing connection info addr[%s]", out_conn_info->addr);
3796 outgoing_gatt_conn_list = g_slist_append(outgoing_gatt_conn_list, out_conn_info);
3799 return BLUETOOTH_ERROR_NONE;
3802 int _bt_gatt_get_primary_services(char *address)
3804 BT_CHECK_PARAMETER(address, return);
3805 struct gatt_server_info_t *conn_info = NULL;
3806 invocation_info_t *req_info = NULL;
3807 int ret = OAL_STATUS_SUCCESS;
3810 /* Check if any app is already browsing primary services on the same remote GATT Server */
3811 req_info = _bt_get_request_info_data(BT_GATT_GET_PRIMARY_SERVICES, address);
3813 BT_INFO("Already Primary Service Browsing ongoing for same rmeote GATT Server");
3814 /* Return and wait for events to be sent to all apps */
3815 return BLUETOOTH_ERROR_NONE;
3818 /* Check if remote GATT Server is connected or not */
3819 conn_info = __bt_find_remote_gatt_server_info(address);
3821 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
3822 conn_info->addr, conn_info->connection_id);
3824 BT_ERR("GATT Server is not yet connected..");
3825 return BLUETOOTH_ERROR_NOT_CONNECTED;
3828 /* Send Primary Service Browsing request to stack */
3829 ret = gattc_search_service(conn_info->connection_id, NULL);
3830 if (ret != OAL_STATUS_SUCCESS) {
3831 BT_ERR("ret: %d", ret);
3832 return BLUETOOTH_ERROR_INTERNAL;
3834 return BLUETOOTH_ERROR_NONE;
3837 int _bt_gatt_get_all_characteristic(bluetooth_gatt_client_svc_prop_info_t *svc)
3839 BT_CHECK_PARAMETER(svc, return);
3840 struct gatt_server_info_t *conn_info = NULL;
3841 invocation_info_t *req_info = NULL;
3842 bluetooth_gatt_client_svc_prop_info_t *prop;
3843 oal_gatt_srvc_id_t srvc_id;
3844 int ret = OAL_STATUS_SUCCESS;
3848 /* Check if any app is already browsing characteristics of the same service on the same remote GATT Server */
3849 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_GET_SERVICE_PROPERTIES);
3851 prop = (bluetooth_gatt_client_svc_prop_info_t*)req_info->user_data;
3852 if (prop && !memcmp(svc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t))
3853 && memcmp(prop->svc.uuid, svc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
3854 && prop->svc.instance_id == svc->svc.instance_id) {
3855 BT_INFO("Already Properties browsing for Primary Service ongoing for same remote GATT Server");
3856 /* Return and wait for events to be sent to all apps */
3857 return BLUETOOTH_ERROR_NONE;
3861 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
3862 _bt_convert_addr_type_to_string(addr, svc->device_address.addr);
3864 /* Check if remote GATT Server is connected or not */
3865 conn_info = __bt_find_remote_gatt_server_info(addr);
3867 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
3868 conn_info->addr, conn_info->connection_id);
3870 BT_ERR("GATT Server is not yet connected..");
3872 return BLUETOOTH_ERROR_NOT_CONNECTED;
3875 srvc_id.is_prmry = TRUE;
3876 srvc_id.id.inst_id = svc->svc.instance_id;
3877 memcpy(srvc_id.id.uuid.uuid, svc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3879 /* Search All Characteristic */
3880 ret = gattc_get_characteristic(conn_info->connection_id, &srvc_id, NULL);
3881 if (ret != OAL_STATUS_SUCCESS) {
3882 BT_ERR("ret: %d", ret);
3884 return BLUETOOTH_ERROR_INTERNAL;
3887 return BLUETOOTH_ERROR_NONE;
3890 int _bt_gatt_get_all_characteristic_properties(
3891 bluetooth_gatt_client_char_prop_info_t *chr)
3893 struct gatt_server_info_t *conn_info = NULL;
3894 invocation_info_t *req_info = NULL;
3895 bluetooth_gatt_client_char_prop_info_t *prop;
3896 oal_gatt_srvc_id_t srvc_id;
3897 oal_gatt_id_t char_id;
3898 int ret = OAL_STATUS_SUCCESS;
3901 BT_CHECK_PARAMETER(chr, return);
3905 /* Check if any app is already browsing descriptors of the same char of
3906 particular service on the same remote GATT Server */
3907 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_GET_CHARACTERISTIC_PROPERTIES);
3909 prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
3910 if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
3911 && memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
3912 && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
3913 && memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
3914 && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
3915 BT_INFO("Already Properties browsing for Characteristic ongoing for same remote GATT Server");
3916 /* Return and wait for events to be sent to all apps */
3917 return BLUETOOTH_ERROR_NONE;
3921 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
3922 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
3924 /* Check if remote GATT Server is connected or not */
3925 conn_info = __bt_find_remote_gatt_server_info(addr);
3927 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
3928 conn_info->addr, conn_info->connection_id);
3930 BT_ERR("GATT Server is not yet connected..");
3932 return BLUETOOTH_ERROR_NOT_CONNECTED;
3935 srvc_id.is_prmry = TRUE;
3936 srvc_id.id.inst_id = chr->svc.instance_id;
3937 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3939 char_id.inst_id = chr->characteristic.instance_id;
3940 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3942 /* Search All Descriptors */
3943 ret = gattc_get_descriptor(conn_info->connection_id, &srvc_id, &char_id, NULL);
3944 if (ret != OAL_STATUS_SUCCESS) {
3945 BT_ERR("ret: %d", ret);
3947 return BLUETOOTH_ERROR_INTERNAL;
3950 return BLUETOOTH_ERROR_NONE;
3953 int _bt_gatt_read_characteristic_value(
3954 bluetooth_gatt_client_char_prop_info_t *chr)
3956 struct gatt_server_info_t *conn_info = NULL;
3957 invocation_info_t *req_info = NULL;
3958 bluetooth_gatt_client_char_prop_info_t *prop;
3959 oal_gatt_srvc_id_t srvc_id;
3960 oal_gatt_id_t char_id;
3961 int ret = OAL_STATUS_SUCCESS;
3964 BT_CHECK_PARAMETER(chr, return);
3968 /* Check if any app is already Reading characteristic of the same char of
3969 particular service on the same remote GATT Server */
3970 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_READ_CHARACTERISTIC);
3972 prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
3973 if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
3974 && memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
3975 && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
3976 && memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
3977 && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
3978 BT_INFO("Already Characteristic value Read operation in progress for same remote GATT Server");
3979 /* Return and wait for events to be sent to all apps */
3980 return BLUETOOTH_ERROR_NONE;
3984 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
3985 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
3987 /* Check if remote GATT Server is connected or not */
3988 conn_info = __bt_find_remote_gatt_server_info(addr);
3990 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
3991 conn_info->addr, conn_info->connection_id);
3993 BT_ERR("GATT Server is not yet connected..");
3995 return BLUETOOTH_ERROR_NOT_CONNECTED;
3998 srvc_id.is_prmry = TRUE;
3999 srvc_id.id.inst_id = chr->svc.instance_id;
4000 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4002 char_id.inst_id = chr->characteristic.instance_id;
4003 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4005 /* Search All Descriptors */
4006 ret = gattc_read_characteristic(conn_info->connection_id, &srvc_id, &char_id, OAL_GATT_AUTH_REQ_NONE);
4007 if (ret != OAL_STATUS_SUCCESS) {
4008 BT_ERR("ret: %d", ret);
4010 return BLUETOOTH_ERROR_INTERNAL;
4013 return BLUETOOTH_ERROR_NONE;
4016 int _bt_gatt_read_descriptor_value(
4017 bluetooth_gatt_client_desc_prop_info_t *desc)
4019 struct gatt_server_info_t *conn_info = NULL;
4020 invocation_info_t *req_info = NULL;
4021 bluetooth_gatt_client_desc_prop_info_t *prop;
4022 oal_gatt_srvc_id_t srvc_id;
4023 oal_gatt_id_t char_id;
4024 oal_gatt_id_t desc_id;
4025 int ret = OAL_STATUS_SUCCESS;
4028 BT_CHECK_PARAMETER(desc, return);
4032 /* Check if any app is already Reading descriptors of the same char of
4033 particular service on the same remote GATT Server */
4034 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_READ_DESCRIPTOR_VALUE);
4036 prop = (bluetooth_gatt_client_desc_prop_info_t*)req_info->user_data;
4037 if (prop && !memcmp(desc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4038 && memcmp(desc->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4039 && desc->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4040 && memcmp(desc->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4041 && desc->characteristic.instance_id == prop->characteristic.instance_id /* Characteristic Instance ID matched */
4042 && memcmp(desc->descriptor.uuid, prop->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Descriptor UUID matched */
4043 && desc->descriptor.instance_id == prop->descriptor.instance_id) { /* Descriptor Instance ID matched */
4044 BT_INFO("Already Descriptor value Read operation in progress for same remote GATT Server");
4045 /* Return and wait for events to be sent to all apps */
4046 return BLUETOOTH_ERROR_NONE;
4050 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4051 _bt_convert_addr_type_to_string(addr, desc->device_address.addr);
4053 /* Check if remote GATT Server is connected or not */
4054 conn_info = __bt_find_remote_gatt_server_info(addr);
4056 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4057 conn_info->addr, conn_info->connection_id);
4059 BT_ERR("GATT Server is not yet connected..");
4061 return BLUETOOTH_ERROR_NOT_CONNECTED;
4064 srvc_id.is_prmry = TRUE;
4065 srvc_id.id.inst_id = desc->svc.instance_id;
4066 memcpy(srvc_id.id.uuid.uuid, desc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4068 char_id.inst_id = desc->characteristic.instance_id;
4069 memcpy(char_id.uuid.uuid, desc->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4071 desc_id.inst_id = desc->descriptor.instance_id;
4072 memcpy(desc_id.uuid.uuid, desc->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4074 /* Search All Descriptors */
4075 ret = gattc_read_descriptor(conn_info->connection_id, &srvc_id, &char_id,
4076 &desc_id, OAL_GATT_AUTH_REQ_NONE);
4077 if (ret != OAL_STATUS_SUCCESS) {
4078 BT_ERR("ret: %d", ret);
4080 return BLUETOOTH_ERROR_INTERNAL;
4083 return BLUETOOTH_ERROR_NONE;
4087 /* Write Characteristic */
4088 int _bt_gatt_write_characteristic_value_by_type(
4089 bluetooth_gatt_client_char_prop_info_t *chr,
4090 bluetooth_gatt_att_data_t *data,
4091 bluetooth_gatt_write_type_e write_type)
4093 struct gatt_server_info_t *conn_info = NULL;
4094 invocation_info_t *req_info = NULL;
4095 bluetooth_gatt_client_char_prop_info_t *prop;
4096 oal_gatt_srvc_id_t srvc_id;
4097 oal_gatt_id_t char_id;
4098 int ret = OAL_STATUS_SUCCESS;
4102 BT_CHECK_PARAMETER(chr, return);
4103 BT_CHECK_PARAMETER(data, return);
4107 /* Check if any app is already writing same char of
4108 particular service on the same remote GATT Server */
4109 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE);
4111 prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
4112 if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4113 && memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4114 && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4115 && memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4116 && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
4117 BT_INFO("Already Characteristic Write Value operation in progress for same remote GATT Server");
4118 /* Return and wait for events to be sent to all apps */
4119 return BLUETOOTH_ERROR_NONE;
4123 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4124 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4126 /* Check if remote GATT Server is connected or not */
4127 conn_info = __bt_find_remote_gatt_server_info(addr);
4129 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4130 conn_info->addr, conn_info->connection_id);
4132 BT_ERR("GATT Server is not yet connected..");
4134 return BLUETOOTH_ERROR_NOT_CONNECTED;
4137 srvc_id.is_prmry = TRUE;
4138 srvc_id.id.inst_id = chr->svc.instance_id;
4139 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4141 char_id.inst_id = chr->characteristic.instance_id;
4142 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4144 /* Write CHar value */
4146 BT_INFO("Connection ID [%d] write type [%d] data length [%d]", conn_info->connection_id, write_type, data->length);
4147 for (k = 0; k < data->length; k++)
4148 BT_INFO("Data[%d] [0x%x]", k, data->data[k]);
4150 ret = gattc_write_characteristic(conn_info->connection_id,
4152 (oal_gatt_write_type_t)write_type, data->length,
4153 OAL_GATT_AUTH_REQ_NONE, (char *)(&data->data[0]));
4154 if (ret != OAL_STATUS_SUCCESS) {
4155 BT_ERR("ret: %d", ret);
4157 return BLUETOOTH_ERROR_INTERNAL;
4160 return BLUETOOTH_ERROR_NONE;
4163 /* Write Descriptor */
4164 int _bt_gatt_write_descriptor_value_by_type(
4165 bluetooth_gatt_client_desc_prop_info_t *desc,
4166 bluetooth_gatt_att_data_t *data,
4167 bluetooth_gatt_write_type_e write_type)
4169 struct gatt_server_info_t *conn_info = NULL;
4170 invocation_info_t *req_info = NULL;
4171 bluetooth_gatt_client_desc_prop_info_t *prop;
4172 oal_gatt_srvc_id_t srvc_id;
4173 oal_gatt_id_t char_id;
4174 oal_gatt_id_t desc_id;
4175 int ret = OAL_STATUS_SUCCESS;
4179 BT_CHECK_PARAMETER(desc, return);
4180 BT_CHECK_PARAMETER(data, return);
4184 /* Check if any app is already writing on same Descriptor of the same char of
4185 particular service on the same remote GATT Server */
4186 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_WRITE_DESCRIPTOR_VALUE);
4188 prop = (bluetooth_gatt_client_desc_prop_info_t*)req_info->user_data;
4189 if (prop && !memcmp(desc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4190 && memcmp(desc->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4191 && desc->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4192 && memcmp(desc->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4193 && desc->characteristic.instance_id == prop->characteristic.instance_id /* Characteristic Instance ID matched */
4194 && memcmp(desc->descriptor.uuid, prop->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Descriptor UUID matched */
4195 && desc->descriptor.instance_id == prop->descriptor.instance_id) { /* Descriptor Instance ID matched */
4196 BT_INFO("Already Descriptor value Write operation in progress for same remote GATT Server");
4197 /* Return and wait for events to be sent to all apps */
4198 return BLUETOOTH_ERROR_NONE;
4202 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4203 _bt_convert_addr_type_to_string(addr, desc->device_address.addr);
4205 /* Check if remote GATT Server is connected or not */
4206 conn_info = __bt_find_remote_gatt_server_info(addr);
4208 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4209 conn_info->addr, conn_info->connection_id);
4211 BT_ERR("GATT Server is not yet connected..");
4213 return BLUETOOTH_ERROR_NOT_CONNECTED;
4216 srvc_id.is_prmry = TRUE;
4217 srvc_id.id.inst_id = desc->svc.instance_id;
4218 memcpy(srvc_id.id.uuid.uuid, desc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4220 char_id.inst_id = desc->characteristic.instance_id;
4221 memcpy(char_id.uuid.uuid, desc->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4223 desc_id.inst_id = desc->descriptor.instance_id;
4224 memcpy(desc_id.uuid.uuid, desc->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4227 BT_INFO("Connection ID [%d] write type [%d] data length [%d]", conn_info->connection_id, write_type, data->length);
4228 for (k = 0; k < data->length; k++)
4229 BT_INFO("Data[%d] [0x%x]", k, data->data[k]);
4231 ret = gattc_write_descriptor(conn_info->connection_id,
4232 &srvc_id, &char_id, &desc_id,
4233 (oal_gatt_write_type_t)write_type, data->length,
4234 OAL_GATT_AUTH_REQ_NONE, (char *)(&data->data[0]));
4235 if (ret != OAL_STATUS_SUCCESS) {
4236 BT_ERR("ret: %d", ret);
4238 return BLUETOOTH_ERROR_INTERNAL;
4241 return BLUETOOTH_ERROR_NONE;
4244 int _bt_disconnect_le_device(bluetooth_device_address_t *address,
4247 struct gatt_server_info_t *conn_info = NULL;
4248 invocation_info_t *req_info = NULL;
4249 int ret = OAL_STATUS_SUCCESS;
4251 char *remote_address = NULL;
4253 BT_CHECK_PARAMETER(address, return);
4257 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4258 _bt_convert_addr_type_to_string(addr, address->addr);
4259 BT_INFO("GATT Client Disconnect request for address [%s]", addr);
4261 /* Check if Remote Device is already under connection progress */
4262 req_info = _bt_get_request_info_data_from_function_name(BT_DISCONNECT_LE);
4264 remote_address = (char*)req_info->user_data;
4265 if (remote_address && !strcasecmp(remote_address, addr)) {/* Address matched */
4266 BT_INFO("Already DisConnection ongoing for same remote GATT Server address [%s]", remote_address);
4267 /* Return success and wait for events to be sent to all apps */
4269 return BLUETOOTH_ERROR_IN_PROGRESS;
4272 /* Check if remote GATT Server is connected or not */
4273 conn_info = __bt_find_remote_gatt_server_info(addr);
4275 BT_ERR("GATT Server is not connected..");
4277 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
4280 /* Check if app sent 0 client id for Disconnection, in such case, use default gatt client ID */
4281 if (client_id == 0) {
4282 BT_INFO("GATT CLient Disconnect request sent by an app without any client instance [%d]",
4284 BT_INFO("Assign default GATT client id [%d]", gatt_default_client);
4285 client_id = gatt_default_client;
4288 BT_INFO("Disconnect using CLient ID [%d] Connection ID [%d]", client_id, conn_info->connection_id);
4289 ret = gattc_disconnect(client_id, (bt_address_t*)(address),
4290 conn_info->connection_id);
4292 if (ret != OAL_STATUS_SUCCESS) {
4293 BT_ERR("ret: %d", ret);
4295 return BLUETOOTH_ERROR_INTERNAL;
4298 return BLUETOOTH_ERROR_NONE;
4301 int _bt_gatt_watch_service_changed_indication(const char *sender,
4302 bluetooth_device_address_t *address,
4303 gboolean is_enabled)
4306 bt_service_app_info_t *info = NULL;
4308 BT_INFO("Enable Servic changed Indication watcher [%d] for app [%s]",
4309 is_enabled, sender);
4311 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
4314 if (g_strcmp0(sender, info->sender) == 0 &&
4315 memcmp(info->address.addr, address->addr,
4316 sizeof(bluetooth_device_address_t)) == 0) {
4317 BT_INFO("Found GATT client App.. UUID [%s], sender [%s]", info->uuid, info->sender);
4318 info->is_watcher_enabled = is_enabled;
4322 return BLUETOOTH_ERROR_NONE;
4325 int _bt_unregister_gatt_client_instance(const char *sender, int client_id)
4327 BT_INFO("Unregister Allocated GATT CLient instance [%s] Client ID [%d]",
4329 int result = BLUETOOTH_ERROR_NONE;
4332 /* Unregister CLient instance associated with address X. It is possible that another app still
4333 has client_id valid for same remote address */
4334 bt_service_app_info_t *info = NULL;
4336 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
4339 /* Exact matching of sender */
4340 if (!g_strcmp0(info->sender, sender) && info->client_id == client_id) { /* Check for only valid GATT client Instance */
4341 BT_INFO("Unregister GATT client instance [%d]", info->client_id);
4342 result = __bt_do_unregister_gatt_instance(info->client_id);
4343 if (result != BLUETOOTH_ERROR_NONE)
4344 BT_ERR("Error in unregistering GATT Client Interface");