4 * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd.
6 * Contact: Anupam Roy (anupam.r@samsung.com)
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
23 #include "bt-service-common.h"
24 #include "bt-service-core-adapter.h"
25 #include "bt-service-event-receiver.h"
26 #include "bt-request-handler.h"
27 #include "bluetooth-api.h"
29 #include "bluetooth-api.h"
30 #include "bt-internal-types.h"
31 #include "bt-service-util.h"
32 #include "bt-service-common.h"
33 #include "bt-service-event.h"
35 #include "bt-internal-types.h"
36 #include "bt-service-gatt.h"
37 #include "bt-service-gatt.h"
38 #include "bt-service-core-device.h"
39 #include "bt-service-core-adapter-le.h"
40 #ifdef TIZEN_GATT_CLIENT
41 #include "bluetooth-gatt-client-api.h"
44 #include <oal-hardware.h>
45 #include <oal-manager.h>
46 #include <oal-event.h>
47 #include <oal-adapter-mgr.h>
48 #include <oal-device-mgr.h>
51 #define BT_GATT_TRANSPORT_LE 0
52 #define BT_GATT_TRANSPORT_BR_EDR 1
53 #define BT_GATT_TRANSPORT_LE_BR_EDR 2
54 #define BT_UUID_STRING_MAX 64
55 #define BT_SENDER_MAX_LENGTH 50
56 #define MAX_APPS_SUPPORTED 11 /* Slot 0 is not used */
57 #define BT_DEFAULT_ATT_MTU 23
59 #ifdef TIZEN_GATT_CLIENT
66 #define UUID_MAX_LEN 50
69 #define BDADDR_ANY (&(bluetooth_device_address_t) {{0, 0, 0, 0, 0, 0} })
71 #ifdef TIZEN_GATT_CLIENT
72 char uuid_list[NUM_UUID][BT_UUID_STRING_MAX] = {"0000b00b-0000-0000-f065-080080fa49b5", /* Used by BLEAPP */
73 "0000b00b-1111-1111-0123-456789ab0cd2", /* Used by BLEAPP */
74 "0000b00b-2222-1111-0123-456789ab0cd2",
75 "0000b00b-3333-1111-0123-456789ab0cd2",
76 "0000b00b-4444-1111-0123-456789ab0cd2",
77 "0000b00b-5555-1111-0123-456789ab0cd2",
78 "0000b00b-6666-1111-0123-456789ab0cd2",
79 "0000b00b-7777-1111-0123-456789ab0cd2",
80 "0000b00b-8888-1111-0123-456789ab0cd2",
81 "0000b00b-9999-1111-0123-456789ab0cd2",
82 "0000b00b-aaaa-1111-0123-456789ab0cd2",
83 "0000b00b-bbbb-1111-0123-456789ab0cd2",
84 "0000b00b-cccc-1111-0123-456789ab0cd2",
85 "0000b00b-dddd-1111-0123-456789ab0cd2",
86 "0000b00b-eeee-1111-0123-456789ab0cd2",
87 "0000b00b-ffff-1111-0123-456789ab0cd2",
88 "0000b00c-0000-1111-0123-456789ab0cd2",
89 "0000b00c-1111-1111-0123-456789ab0cd2",
90 "0000b00c-2222-1111-0123-456789ab0cd2",
91 "0000b00c-3333-1111-0123-456789ab0cd2"};
93 char uuid_list[NUM_UUID][BT_UUID_STRING_MAX] = {"0000b00b-0000-0000-f065-080080fa49b5", /* Used by BLEAPP */
94 "0000b00b-1111-1111-0123-456789ab0cd2", /* Used by BLEAPP */
95 "0000b00b-2222-1111-0123-456789ab0cd2",
96 "0000b00b-3333-1111-0123-456789ab0cd2",
97 "0000b00b-4444-1111-0123-456789ab0cd2",
98 "0000b00b-5555-1111-0123-456789ab0cd2",
99 "0000b00b-6666-1111-0123-456789ab0cd2",
100 "0000b00b-7777-1111-0123-456789ab0cd2",
101 "0000b00b-8888-1111-0123-456789ab0cd2",
102 "0000b00b-9999-1111-0123-456789ab0cd2"};
105 #ifdef TIZEN_GATT_CLIENT
106 /* Reserved GATT client Instance UUID. This is used only internally by bt-service */
107 #define DEFAULT_GATT_CLIENT_UUID "0000a00a-1111-1111-0123-456789abcdef"
109 static int gatt_default_client = -1;
112 gboolean is_registered;
113 bluetooth_device_address_t addr;
114 unsigned char svc_uuid[BLUETOOTH_UUID_HEX_MAX_LEN];
116 unsigned char char_uuid[BLUETOOTH_UUID_HEX_MAX_LEN];
118 } bt_gatt_notif_reg_info_t;
120 struct gatt_out_conn_info_t {
121 int client_id; /* This value unique identifies a GATT Client instance */
122 char *addr; /* Remote GATT Server address */
125 /* Linked List of outgoing gatt connection list
126 Note: This is valid only for local GATT client */
127 static GSList *outgoing_gatt_conn_list = NULL;
129 /* GATT Server Info(Local Client) List Structure */
130 struct gatt_server_info_t {
131 int connection_id; /* This value will uniquely identify a GATT client-server connection */
132 int client_id; /* This value unique identifies a GATT Client instance */
133 char *addr; /* Remote GATT Server address */
136 /* Linked List of connected Remote GATT Servers */
137 static GSList *gatt_server_info_list = NULL;
141 unsigned char uuid[BLUETOOTH_UUID_HEX_MAX_LEN]; /* If any service added */
142 } bt_gatt_svc_changed_info_t;
147 // bluetooth_device_address_t address; /* Remote BLE Device Address */
148 GSList *services; /* List of all services of above remote device */
149 int count; /* Number of services browsed from remote device */
150 bt_gatt_svc_changed_info_t info;
151 } bt_gatt_service_info_list_t;
154 unsigned char uuid[BLUETOOTH_UUID_HEX_MAX_LEN];
158 GSList *included_svcs;
159 gboolean is_removed; /* 0 => Service is added, 1=> removed */
160 } bt_gatt_service_info_t;
163 unsigned char uuid[BLUETOOTH_UUID_HEX_MAX_LEN];
167 unsigned char val[BLUETOOTH_GATT_ATT_DATA_LENGTH_MAX];
169 } bt_gatt_char_info_t;
172 unsigned char uuid[BLUETOOTH_UUID_HEX_MAX_LEN];
175 unsigned char val[BLUETOOTH_GATT_ATT_DATA_LENGTH_MAX];
176 } bt_gatt_descriptor_info_t;
179 unsigned char uuid[BLUETOOTH_UUID_HEX_MAX_LEN];
181 } bt_gatt_included_service_info_t;
183 GSList *list_gatt_info;
188 /* App Information structure */
191 char sender[BT_SENDER_MAX_LENGTH];
192 char uuid[BT_UUID_STRING_MAX];
195 bluetooth_advertising_data_t adv_data; /* Will store adv data for specific slot */
197 bluetooth_scan_resp_data_t scan_rsp; /* Will store scan rsp data for specific slot */
199 gboolean is_initialized;
200 GSList *service_handles;
201 #ifdef TIZEN_GATT_CLIENT
202 int client_id; /* GATT Client instance ID */
203 bluetooth_device_address_t address; /* Remote BLE Device Address */
204 gboolean is_watcher_enabled;
206 } bt_service_app_info_t;
208 /* GATT Server Request Info Structure */
209 struct gatt_server_req_info {
210 int connection_id; /* This value will uniquely identify a GATT client-server connection */
211 int request_id; /* This is an unique transaction ID assigned against each request by stack */
212 int attribute_handle; /* GATT server attribute handle */
213 int offset; /* GATT server attribute offset on which request is invoked by GATT client */
214 bluetooth_gatt_att_request_type_e request_type; /* Read or Write request */
215 char *addr; /* Remote GATT client address */
218 /* GATT Client Info List Structure */
219 struct gatt_client_info_t {
220 int connection_id; /* This value will uniquely identify a GATT client-server connection */
221 int instance_id; /* This value unique identifies a GATT server instance */
222 char *addr; /* Remote GATT client address */
225 /* GATT Indicate confirm result */
226 struct gatt_indicate_cfm_result_info_t {
227 int result; /* Result of event */
228 char *addr; /* Remote GATT client address */
229 int att_hdl; /* Characteristic Attribute handle */
230 int completed; /* 1 if last event, otheriwse 0 */
233 /* Request Search Utility method */
234 static struct gatt_server_req_info *__bt_gatt_server_find_request_info(int request_id,
235 bluetooth_gatt_att_request_type_e req_type);
237 static int __bt_gatt_send_indication_to_all_connected_clients(bluetooth_gatt_att_data_t *data,
238 bluetooth_gatt_server_indication_params_t *param);
240 static void __bt_remove_all_service_handles(int instance_id);
242 #ifdef TIZEN_GATT_CLIENT
243 static void __bt_handle_client_instance_registered(event_gattc_register_t *data);
244 static void __bt_handle_client_connected(event_gattc_conn_t *event_data);
245 static void __bt_handle_client_disconnected(event_gattc_conn_t *event_data);
246 static void __bt_handle_client_service_search_completed(event_gattc_conn_status_t *event_data);
247 static void __bt_handle_client_service_search_result(event_gattc_service_result_t *event_data);
248 static void __bt_handle_client_characteristic_search_result(
249 event_gattc_characteristic_result_t *event_data);
250 static void __bt_handle_client_descriptor_search_result(event_gattc_descriptor_result_t *event_data);
251 static void __bt_handle_client_characteristic_read_data(event_gattc_read_data *event_data);
252 static void __bt_handle_client_descriptor_read_data(event_gattc_read_data *event_data);
253 static void __bt_handle_client_characteristic_write_data(event_gattc_write_data *event_data);
254 static void __bt_handle_client_descriptor_write_data(event_gattc_write_data *event_data);
255 static void __bt_hanlde_le_device_disconnection(event_dev_conn_status_t *event_data);
256 static void __bt_handle_client_notification_registered(event_gattc_regdereg_notify_t *event_data,
257 gboolean is_registered);
258 static void __bt_handle_client_notification_data(event_gattc_notify_data *event_data);
259 static void __bt_handle_client_service_changed_ind(event_gattc_service_changed_data *event_data);
260 static void __bt_handle_client_mtu_exchange_completed(event_gattc_mtu_configured_t *event_data);
263 static int __bt_unregister_gatt_server_instance(int server_instance);
266 struct gatt_mtu_info_t {
267 char *addr; /* Remote GATT Server address */
271 static GSList *gatt_mtu_info_list = NULL;
273 static struct gatt_mtu_info_t *__bt_find_mtu_gatt_device(char *address);
274 static void __bt_remove_mtu_gatt_device(char *address);
275 static void __bt_add_mtu_gatt_device(char *address);
276 static void __bt_update_mtu_gatt_device(char *address, int mtu);
278 /* Linked List of GATT requests from Remote GATT Clients */
279 static GSList *gatt_server_requests = NULL;
281 /* Linked List of connected Remote GATT clients */
282 static GSList *gatt_client_info_list = NULL;
284 /* Number of clients to be notified to */
285 static int num_indicate_clients;
287 /* List of applications */
288 static bt_service_app_info_t numapps[MAX_APPS_SUPPORTED];
290 static void __bt_gatt_handle_pending_request_info(int result,
291 int service_function, void *data, unsigned int size);
293 static void __bt_handle_server_instance_registered(event_gatts_register_t *data);
295 static void __bt_gatt_event_handler(int event_type, gpointer event_data);
297 void _bt_check_adv_app_termination(const char *name)
299 bt_service_app_info_t *app = NULL;
301 int apps[MAX_APPS_SUPPORTED];
303 ret_if(NULL == name);
305 memset(&apps, -1, sizeof(apps));
307 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
310 /* Search for a app which has same sender and stop adv is running */
311 if (!strncasecmp(app->sender, name, strlen(name)) && app->is_initialized == TRUE) {
312 BT_DBG("Match found, name: %s", name);
314 /* Unregister all service handles with stack */
315 __bt_remove_all_service_handles(app->instance_id);
317 /* If Advertising is enabled, stop it */
318 if (app->adv_handle > 0) {
319 BT_INFO("Stop advertising on instance ID [%d]", app->instance_id);
320 /* Disable adv if running */
321 BT_INFO("Disable Advertising Adv Handle [%d] sender [%s]",
322 app->adv_handle, name);
323 _bt_set_advertising(app->sender, app->adv_handle, FALSE, FALSE);
326 /* Save instances of all apps that need to be unregistered */
327 apps[app->instance_id] = 1;
331 /* Unregister all apps one by one */
332 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
334 BT_INFO("Unregister app[%d]", k);
335 /* Unregister server instance */
336 __bt_unregister_gatt_server_instance(k);
341 static int __bt_gatt_send_indication_to_all_connected_clients(bluetooth_gatt_att_data_t *data,
342 bluetooth_gatt_server_indication_params_t *param)
345 int ret = OAL_STATUS_SUCCESS;
346 int result = OAL_STATUS_INTERNAL_ERROR;
348 BT_INFO("Current total number of connected clients [%d]", g_slist_length(gatt_client_info_list));
349 for (l = gatt_client_info_list; l != NULL; l = l->next) {
350 struct gatt_client_info_t *info = l->data;
353 BT_INFO("GATT Remote client address [%s] connection Id [%d]", info->addr, info->connection_id);
355 ret = gatts_send_indication(param->instance_id, param->atrribute_handle,
356 info->connection_id, data->length,
357 param->need_confirmation, (char *)(&data->data[0]));
359 BT_INFO("Send Indication to GATT client [%s] result: [%d]", info->addr, ret);
360 if (ret == OAL_STATUS_SUCCESS) {
361 BT_INFO("Send Indication sent successfully to GATT client [%s]", info->addr);
363 num_indicate_clients++;
367 BT_INFO("Indication sending done for total number of clients [%d]", num_indicate_clients);
371 static struct gatt_server_req_info *__bt_gatt_server_find_request_info(int request_id,
372 bluetooth_gatt_att_request_type_e req_type)
376 for (l = gatt_server_requests; l != NULL; l = l->next) {
377 struct gatt_server_req_info *req_info = l->data;
379 if (req_info && req_info->request_id == request_id && req_info->request_type == req_type) {
380 BT_INFO("GATT Server request info found Req ID [%d] handle [%d] conn ID [%d]",
381 req_info->request_id, req_info->attribute_handle, req_info->connection_id);
385 BT_ERR("Gatt Request not found");
389 void _bt_get_adv_handle_from_instance(int server_inst, int *adv_handle)
393 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
394 if (numapps[k].is_initialized == 1 && numapps[k].instance_id == server_inst) {
395 *adv_handle = numapps[k].adv_handle;
401 #ifdef TIZEN_GATT_CLIENT
402 char * _bt_gatt_get_default_gatt_client_uuid(void)
404 return g_strdup(DEFAULT_GATT_CLIENT_UUID);
408 static void __bt_register_default_gatt_client()
413 uuid_str = _bt_gatt_get_default_gatt_client_uuid();
414 _bt_string_to_uuid(uuid_str, (service_uuid_t*)&uuid);
416 BT_INFO("Register Default GATT client uuid [%s]", uuid_str);
418 if (OAL_STATUS_SUCCESS != gattc_register(&uuid)) /* for only Smart Control */
419 BT_ERR("gattc register failed");
425 int _bt_gatt_init(void)
429 if (OAL_STATUS_SUCCESS != gatt_enable()) {
430 BT_ERR("gatt Init failed");
431 return BLUETOOTH_ERROR_INTERNAL;
434 /* Register gatt event handler */
435 _bt_service_register_event_handler_callback(BT_GATT_MODULE, __bt_gatt_event_handler);
436 #ifdef TIZEN_GATT_CLIENT
437 /*In bluedroid product, defacult gatt client is handled differently*/
438 __bt_register_default_gatt_client();
441 return BLUETOOTH_ERROR_NONE;
444 static void __bt_service_reset_gatt_data()
448 BT_INFO("Rest numapp");
451 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
452 numapps[k].is_initialized = 0;
453 numapps[k].instance_id = -1;
454 numapps[k].adv_handle = 0;
455 numapps[k].adv_instance = -1;
456 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
457 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
458 memset(numapps[k].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
459 memset(numapps[k].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
460 numapps[k].adv_data_len = 0;
461 numapps[k].scan_rsp_len = 0;
465 void _bt_gatt_deinit(void)
467 BT_INFO("GATT deinit");
469 if (OAL_STATUS_SUCCESS != gatt_disable())
470 BT_ERR("gatt deinit failed");
472 /* Un-register gatt event handler */
473 _bt_service_unregister_event_handler_callback(BT_GATT_MODULE);
475 __bt_service_reset_gatt_data();
478 void _bt_update_adv_handle(const char *sender, int adv_handle)
481 BT_DBG("Sender [%s] Adv handle [%d]", sender, adv_handle);
482 bt_service_app_info_t *info = NULL;
484 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
486 /* Search for a app which has same sender and adv handle as 0 */
487 if (!g_strcmp0(info->sender, sender) && info->adv_handle == 0)
488 info->adv_handle = adv_handle;
492 int _bt_is_sender_gatt_server_with_no_adv(const char *sender, int adv_handle)
495 BT_DBG("Sender [%s] Adv handle [%d]", sender, adv_handle);
496 bt_service_app_info_t *info = NULL;
498 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
500 /* Search for a app which has same sender and adv handle as 0
501 It is possible that same sender but different adv handle */
502 if (!g_strcmp0(info->sender, sender) && info->adv_handle == 0) {
503 //info->adv_handle = adv_handle;
504 return info->instance_id;
510 int _bt_get_allocated_server_instance(const char *sender, int adv_handle, gboolean use_reserved_slot)
513 BT_DBG("Sender [%s] Adv handle [%d]", sender, adv_handle);
514 bt_service_app_info_t *info = NULL;
516 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
519 /* Exact matching of Adv handle + sender combination */
520 if (!g_strcmp0(info->sender, sender) && info->adv_handle == adv_handle)
521 return info->instance_id;
523 if (!g_strcmp0(info->sender, sender) && info->adv_handle == -1)
524 return info->instance_id;
530 char * _bt_get_sender_and_handle(int server_instance, int *adv_handle)
533 bt_service_app_info_t *info = NULL;
535 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
538 if (info->instance_id == server_instance && info->adv_handle != -1) {
539 *adv_handle = info->adv_handle;
540 BT_DBG("Server instance [%d] Adv handle [%d] Sender [%s]", server_instance, *adv_handle, info->sender);
541 return g_strdup(info->sender);
547 void _bt_set_new_adv_data(bluetooth_advertising_data_t *adv, int len, int instance)
551 bt_service_app_info_t *info = NULL;
552 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
555 if (info->instance_id == instance) {
556 memcpy(info->adv_data.data, &adv->data, len);
563 void _bt_set_new_scan_rsp_data(bluetooth_scan_resp_data_t *scan, int len, int instance)
567 bt_service_app_info_t *info = NULL;
568 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
571 if (info->instance_id == instance) {
572 memcpy(info->scan_rsp.data, &scan->data, len);
579 void _bt_get_previous_adv_data(bluetooth_advertising_data_t *adv, int *len, int instance)
583 bt_service_app_info_t *info = NULL;
585 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
588 if (info->instance_id == instance) {
589 memcpy(&adv->data, info->adv_data.data, info->adv_data_len);
590 *len = info->adv_data_len;
596 void _bt_get_previous_scan_rsp_data(bluetooth_scan_resp_data_t *scan, int *len, int instance)
600 bt_service_app_info_t *info = NULL;
602 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
605 if (info->instance_id == instance) {
606 memcpy(&scan->data, info->scan_rsp.data, info->scan_rsp_len);
607 *len = info->scan_rsp_len;
613 static int __bt_unregister_gatt_server_instance(int server_instance)
615 int ret = OAL_STATUS_SUCCESS;
618 /* Unregister the server instance */
619 ret = gatts_unregister(server_instance);
620 if (ret != OAL_STATUS_SUCCESS) {
621 BT_ERR("DeAllocate server instance with stack Fail ret: %d", ret);
622 return BLUETOOTH_ERROR_INTERNAL;
624 BT_INFO("DeAllocated server instance with stack successful..");
627 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
628 if (numapps[k].instance_id == server_instance) {
629 numapps[k].is_initialized = 0;
630 numapps[k].instance_id = -1;
631 numapps[k].adv_handle = 0;
632 numapps[k].adv_instance = -1;
633 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
634 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
635 memset(numapps[k].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
636 memset(numapps[k].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
637 numapps[k].adv_data_len = 0;
638 numapps[k].scan_rsp_len = 0;
642 BT_DBG("Going8 to reset numapp block num [%d]", k);
643 return BLUETOOTH_ERROR_NONE;
646 static void __bt_remove_all_service_handles(int instance_id)
650 int ret = OAL_STATUS_SUCCESS;
653 count = g_slist_length(numapps[instance_id].service_handles);
654 BT_INFO("Before handle removal: current count [%d]", count);
656 for (l = numapps[instance_id].service_handles; l != NULL;) {
661 BT_INFO("Server Handle to be Removed [%d] Instance ID [%d]", *handle, instance_id);
662 if (_bt_gatt_server_stop_service(numapps[instance_id].sender, *handle, instance_id) != BLUETOOTH_ERROR_NONE)
665 ret = gatts_delete_service(instance_id, *handle);
666 if (ret != OAL_STATUS_SUCCESS) {
667 BT_ERR("ret: %d", ret);
670 numapps[instance_id].service_handles = g_slist_remove(numapps[instance_id].service_handles, handle);
673 count = g_slist_length(numapps[instance_id].service_handles);
674 BT_INFO("After deleting current count [%d]", count);
680 int _bt_unregister_server_instance(const char *sender, int adv_handle)
682 BT_INFO("Unregister Allocated server instance request Sender [%s] Adv handle [%d]", sender, adv_handle);
683 int result = BLUETOOTH_ERROR_NONE;
684 int apps[MAX_APPS_SUPPORTED];
688 memset(&apps, -1, sizeof(apps));
690 if (adv_handle == 0) {
691 BT_DBG("Its a direct GATT Server app request to unregister");
692 /* Unregister server instance for each app with same sender (case: GATT Server with multiple adv handle) */
693 bt_service_app_info_t *info = NULL;
695 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
698 /* Exact matching of sender */
699 if (!g_strcmp0(info->sender, sender)) {
700 BT_INFO("Unregister GATT server instance [%d]", info->instance_id);
701 /* Unregister all service handles with stack */
702 __bt_remove_all_service_handles(info->instance_id);
704 /* Disable adv if running */
705 BT_INFO("Disable Advertising Adv Handle [%d] sender [%s]",
706 info->adv_handle, sender);
707 _bt_set_advertising(sender, info->adv_handle, FALSE, FALSE);
709 /* Save all instances which need to be unregistered */
710 apps[info->instance_id] = 1;
714 BT_DBG("Its an Internal unregister request by adv application");
715 server_instance = _bt_get_allocated_server_instance(sender, adv_handle, FALSE);
716 BT_DBG("Its an Internal unregister request by adv application: Adv disabled srv instance [%d]", server_instance);
717 if (server_instance == -1) {
718 BT_ERR("No allocated server instance to be removed");
719 return BLUETOOTH_ERROR_INVALID_PARAM;
721 if (!numapps[server_instance].service_handles) {
722 BT_INFO("There are no Service handles with this app, safe to unregister");
723 /* Unregister server instance only if this sender does not have any gatt services in it */
724 result = __bt_unregister_gatt_server_instance(server_instance);
726 numapps[server_instance].adv_handle = 0;
727 memset(numapps[server_instance].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
728 numapps[server_instance].adv_data_len = 0;
729 memset(numapps[server_instance].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
730 numapps[server_instance].scan_rsp_len = 0;
735 /* Unregister all apps one by one */
736 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
738 BT_INFO("Unregister app[%d]", k);
739 /* Unregister server instance */
740 __bt_unregister_gatt_server_instance(k);
747 int _bt_register_server_instance(const char *sender, int adv_handle)
749 int ret = OAL_STATUS_SUCCESS;
750 char *uuid_string = NULL;
755 BT_INFO("###Check on which instance Server instance can be initialized....");
756 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
757 if (numapps[k].is_initialized == 1) {
758 BT_INFO("Instance ID [%d] is already in use..Check next slot", numapps[k].instance_id);
761 BT_INFO("Time to register GATT Server..UUID to be used is [%s] slot [%d]", uuid_list[slot-1], slot);
767 BT_ERR("No Slot if free for GATT Server registration..");
768 return BLUETOOTH_ERROR_REGISTRATION_FAILED;
771 uuid_string = g_malloc0(BT_UUID_STRING_MAX);
772 _bt_string_to_uuid(uuid_list[slot-1], (service_uuid_t*)&uuid);
773 g_strlcpy(uuid_string, uuid_list[slot-1], BT_UUID_STRING_MAX);
774 BT_INFO("Copied UUID string [%s]", uuid_string);
776 /* Register GATT Server */
777 ret = gatts_register(&uuid);
778 if (ret != OAL_STATUS_SUCCESS) {
779 BT_ERR("ret: %d", ret);
781 return BLUETOOTH_ERROR_INTERNAL;
783 BT_INFO("GATT Server registration call successfully accepted by OAL..wait for Instance Initialized event from OAL..");
784 /* Return & wait for GATT Server Instance Initialization event */
785 memset(numapps[slot].sender, 0x00, sizeof(numapps[slot].sender));
786 memset(numapps[slot].uuid, 0x00, sizeof(numapps[slot].uuid));
788 g_strlcpy(numapps[slot].sender, sender, sizeof(numapps[slot].sender));
789 g_strlcpy(numapps[slot].uuid, uuid_string, sizeof(numapps[slot].uuid));
791 numapps[slot].is_initialized = 0; /* Set initialization from app registered callback */
792 numapps[slot].adv_handle = adv_handle;
795 return BLUETOOTH_ERROR_NONE;
799 static void __bt_gatt_handle_pending_request_info(int result,
800 int service_function, void *data, unsigned int size)
804 invocation_info_t *req_info = NULL;
805 ret_if(data == NULL);
808 for (l = _bt_get_invocation_list(); l != NULL; ) {
811 if (req_info == NULL || req_info->service_function != service_function)
814 switch (service_function) {
816 case BT_GATT_SERVER_REGISTER: {
817 bt_service_app_info_t *param = (bt_service_app_info_t*)data;
818 BT_INFO("Sender [%s] Request Sender [%s]", param->sender, req_info->sender);
819 if (!g_strcmp0(req_info->sender, param->sender)) {
820 BT_DBG("GATT Server app found [%s]", req_info->sender);
822 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
823 g_array_append_vals(out_param, ¶m->instance_id, sizeof(int));
824 _bt_service_method_return(req_info->context, out_param, result);
826 g_free(req_info->user_data);
827 _bt_free_info_from_invocation_list(req_info);
828 g_array_free(out_param, TRUE);
832 case BT_GATT_SERVER_START_SERVICE:
833 case BT_GATT_SERVER_DELETE_SERVICE: {
834 bt_service_app_info_t *param = (bt_service_app_info_t*)data;
835 BT_INFO("Sender [%s] Request Sender [%s]", param->sender, req_info->sender);
837 int *saved_instance_id = (int*)req_info->user_data;
838 if (!g_strcmp0(req_info->sender, param->sender) && param->instance_id == *saved_instance_id) {
839 BT_DBG("GATT Server app found [%s] Instance ID [%d] Reply DBUS",
840 req_info->sender, *saved_instance_id);
842 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
843 g_array_append_vals(out_param, &saved_instance_id, sizeof(int));
844 _bt_service_method_return(req_info->context, out_param, result);
846 g_free(req_info->user_data);
847 _bt_free_info_from_invocation_list(req_info);
848 g_array_free(out_param, TRUE);
852 case BT_GATT_SERVER_ADD_SERVICE:
853 case BT_GATT_SERVER_ADD_DESCRIPTOR:
854 case BT_GATT_SERVER_ADD_CHARACTERISTIC: {
855 int *handle = (int*)data;
856 BT_INFO("Characteristic added: Handle [%d]", *handle);
857 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
859 g_array_append_vals(out_param, handle, sizeof(int));
860 _bt_service_method_return(req_info->context, out_param, result);
862 g_free(req_info->user_data);
863 _bt_free_info_from_invocation_list(req_info);
864 g_array_free(out_param, TRUE);
869 case BT_DISCONNECT_LE: {
870 #ifdef TIZEN_GATT_CLIENT
871 char *addr = (char*)req_info->user_data;
872 bluetooth_device_address_t address;
874 if (!g_strcmp0(addr, (char*)data)) {
875 BT_INFO("GATT Client connect-disconnect call pending for app [%s] addr [%s]",
876 req_info->sender, addr);
877 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
878 _bt_convert_addr_string_to_type(address.addr, addr);
880 g_array_append_vals(out_param, (bluetooth_device_address_t*)&address,
881 sizeof(bluetooth_device_address_t));
882 _bt_service_method_return(req_info->context, out_param, result);
884 g_free(req_info->user_data);
885 _bt_free_info_from_invocation_list(req_info);
886 g_array_free(out_param, TRUE);
889 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
891 g_array_append_vals(out_param, (bluetooth_device_address_t*)data,
892 sizeof(bluetooth_device_address_t));
893 _bt_service_method_return(req_info->context, out_param, result);
895 g_free(req_info->user_data);
896 _bt_free_info_from_invocation_list(req_info);
898 g_array_free(out_param, TRUE);
902 #ifdef TIZEN_GATT_CLIENT
903 case BT_GATT_CLIENT_REGISTER: {
904 bt_service_app_info_t *param = (bt_service_app_info_t*)data;
905 BT_INFO("Sender [%s] Request Sender [%s]", param->sender, req_info->sender);
906 if (!g_strcmp0(req_info->sender, param->sender)) {
907 BT_DBG("GATT Client app found [%s] created client ID [%d]",
908 req_info->sender, param->client_id);
910 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
911 g_array_append_vals(out_param, ¶m->client_id, sizeof(int));
912 _bt_service_method_return(req_info->context, out_param, result);
914 g_free(req_info->user_data);
915 _bt_free_info_from_invocation_list(req_info);
916 g_array_free(out_param, TRUE);
920 case BT_GATT_GET_PRIMARY_SERVICES: {
921 bt_services_browse_info_t *param = (bt_services_browse_info_t*)data;
922 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
923 _bt_convert_addr_type_to_string(address,
924 (unsigned char *)(¶m->device_addr.addr));
926 BT_INFO("Request Sender [%s] address [%s]", req_info->sender, address);
928 /* Match address to determine same request */
929 if (!g_strcmp0((char*)req_info->user_data, address)) {
930 BT_DBG("GATT Client app found [%s] Remote address [%s]",
931 req_info->sender, address);
933 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
934 g_array_append_vals(out_param, param,
935 sizeof(bt_services_browse_info_t));
937 //g_array_append_vals(out_param, ¶m->client_id, sizeof(int));
938 _bt_service_method_return(req_info->context, out_param, result);
940 g_free(req_info->user_data);
941 _bt_free_info_from_invocation_list(req_info);
942 g_array_free(out_param, TRUE);
947 case BT_GATT_GET_SERVICE_PROPERTIES: {
948 bt_char_browse_info_t param;
949 memcpy((void*)¶m, data, sizeof(bt_char_browse_info_t));
950 //bt_char_browse_info_t *param = (bt_char_browse_info_t*)data;
951 BT_INFO("Request Sender [%s]", req_info->sender);
952 bluetooth_gatt_client_svc_prop_info_t *prop = (bluetooth_gatt_client_svc_prop_info_t*)req_info->user_data;
954 /* Match both address and service properties to determine same request */
955 if (!memcmp(param.device_addr.addr,
956 prop->device_address.addr,
957 sizeof(bluetooth_device_address_t)) &&
958 !memcmp(param.svc_uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
959 param.svc_inst_id == prop->svc.instance_id) {
960 BT_INFO("Returning Service properties");
962 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
963 g_array_append_vals(out_param, ¶m, sizeof(bt_char_browse_info_t));
964 _bt_service_method_return(req_info->context, out_param, result);
966 g_free(req_info->user_data);
967 _bt_free_info_from_invocation_list(req_info);
968 g_array_free(out_param, TRUE);
972 case BT_GATT_GET_CHARACTERISTIC_PROPERTIES: {
973 bt_descriptor_browse_info_t *param = (bt_descriptor_browse_info_t*)data;
975 BT_INFO("Request Sender [%s]", req_info->sender);
976 bluetooth_gatt_client_char_prop_info_t *prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
978 /* Match both address, service properties &char 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 !memcmp(param->char_uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
985 param->char_inst_id == prop->characteristic.instance_id) {
986 BT_INFO("Returning Characteristic properties");
987 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
988 g_array_append_vals(out_param, param, sizeof(bt_descriptor_browse_info_t));
989 _bt_service_method_return(req_info->context, out_param, result);
991 g_free(req_info->user_data);
992 _bt_free_info_from_invocation_list(req_info);
993 g_array_free(out_param, TRUE);
997 case BT_GATT_WATCH_CHARACTERISTIC: {
998 bt_gatt_notif_reg_info_t *param = (bt_gatt_notif_reg_info_t*)data;
999 bluetooth_gatt_client_char_prop_info_t *prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
1000 BT_INFO("Request Sender [%s]", req_info->sender);
1001 /* Match both address, service properties &char properties to determine same request */
1002 if (!memcmp(param->addr.addr,
1003 prop->device_address.addr,
1004 sizeof(bluetooth_device_address_t)) &&
1005 !memcmp(param->svc_uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1006 param->svc_inst == prop->svc.instance_id &&
1007 !memcmp(param->char_uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1008 param->char_inst == prop->characteristic.instance_id) {
1009 BT_INFO("Characteristic Watch Successful: Is registered [%d]",
1010 param->is_registered);
1011 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1012 g_array_append_vals(out_param, param, sizeof(bt_gatt_notif_reg_info_t));
1013 _bt_service_method_return(req_info->context, out_param, result);
1014 g_free(req_info->user_data);
1015 _bt_free_info_from_invocation_list(req_info);
1016 g_array_free(out_param, TRUE);
1020 case BT_GATT_READ_CHARACTERISTIC:
1021 case BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE: {
1022 bluetooth_gatt_client_char_prop_info_t *param = (bluetooth_gatt_client_char_prop_info_t*)data;
1023 BT_INFO("Request Sender [%s]", req_info->sender);
1025 char *addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
1026 bluetooth_gatt_client_char_prop_info_t *prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
1027 _bt_convert_addr_type_to_string(addr, prop->device_address.addr);
1029 if (!memcmp(param, prop, sizeof(bluetooth_gatt_client_char_prop_info_t))) {
1030 BT_INFO("Gatt Char read or write request matched for address [%s]", addr);
1031 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1032 g_array_append_vals(out_param, param, sizeof(bluetooth_gatt_client_char_prop_info_t));
1033 _bt_service_method_return(req_info->context, out_param, result);
1035 g_free(req_info->user_data);
1036 _bt_free_info_from_invocation_list(req_info);
1037 g_array_free(out_param, TRUE);
1042 case BT_GATT_READ_DESCRIPTOR_VALUE:
1043 case BT_GATT_WRITE_DESCRIPTOR_VALUE: {
1044 bluetooth_gatt_client_desc_prop_info_t *param = (bluetooth_gatt_client_desc_prop_info_t*)data;
1045 BT_INFO("Request Sender [%s]", req_info->sender);
1047 char *addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
1048 bluetooth_gatt_client_desc_prop_info_t *prop = (bluetooth_gatt_client_desc_prop_info_t*)req_info->user_data;
1049 _bt_convert_addr_type_to_string(addr, prop->device_address.addr);
1051 if (!memcmp(param, prop, sizeof(bluetooth_gatt_client_desc_prop_info_t))) {
1052 BT_INFO("Descriptor read or write request matched for address [%s]", addr);
1053 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1054 g_array_append_vals(out_param, param, sizeof(bluetooth_gatt_client_desc_prop_info_t));
1055 _bt_service_method_return(req_info->context, out_param, result);
1057 g_free(req_info->user_data);
1058 _bt_free_info_from_invocation_list(req_info);
1059 g_array_free(out_param, TRUE);
1064 case BT_REQ_ATT_MTU: {
1065 char *addr = (char*)req_info->user_data;
1066 bluetooth_device_address_t address;
1068 if (!g_strcmp0(addr, (char*)data)) {
1069 BT_INFO("GATT Client BT_REQ_ATT_MTU call pending for app [%s] addr [%s]",
1070 req_info->sender, addr);
1071 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1072 _bt_convert_addr_string_to_type(address.addr, addr);
1074 g_array_append_vals(out_param, (bluetooth_device_address_t*)&address,
1075 sizeof(bluetooth_device_address_t));
1076 _bt_service_method_return(req_info->context, out_param, result);
1078 g_free(req_info->user_data);
1079 _bt_free_info_from_invocation_list(req_info);
1080 g_array_free(out_param, TRUE);
1091 static void __bt_handle_server_instance_registered(event_gatts_register_t *data)
1093 bt_service_app_info_t *info = NULL;
1095 char *uuid_string = g_malloc0(BT_UUID_STRING_MAX);
1097 _bt_uuid_to_string(&(data->server_uuid), uuid_string);
1098 BT_INFO("Instance ID is Intialized [%d] UUID initialized [%s]", data->server_inst, uuid_string);
1100 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1101 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1104 if (g_strcmp0(info->uuid, uuid_string) == 0) {
1105 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1106 info->is_initialized = TRUE;
1107 info->instance_id = data->server_inst;
1108 info->adv_instance = data->server_inst;
1109 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE, BT_GATT_SERVER_REGISTER,
1110 (void*)info, sizeof(bt_service_app_info_t));
1114 g_free(uuid_string);
1117 static void __bt_handle_gatt_server_service_added(event_gatts_srvc_prm_t *event)
1119 int result = BLUETOOTH_ERROR_NONE;
1123 bt_service_app_info_t *info = NULL;
1126 char *uuid_str = g_malloc0(BT_UUID_STRING_MAX);
1127 _bt_uuid_to_string(&(event->gatt_srvc_id.id.uuid), uuid_str);
1128 BT_INFO("GATT Added Service UUID: [%s] Server Ins [%d] Service hdl [%d] Is Primary [%d]\n",
1129 uuid_str, event->gatt_srvc_stat.server_inst, svc_handle, event->gatt_srvc_id.is_prmry);
1131 if (event->gatt_srvc_stat.status != OAL_STATUS_SUCCESS) {
1132 result = BLUETOOTH_ERROR_INTERNAL;
1133 svc_handle = 0; /* Service handle set to 0 indicates.
1134 0 is reserved by BT SIG, cant be used by app*/
1136 svc_handle = event->gatt_srvc_stat.servic_hndl;
1138 BT_INFO("GATT Added Service Status [%d] Server Ins [%d] Service hdl [%d] Is Primary [%d]\n",
1139 event->gatt_srvc_stat.status, event->gatt_srvc_stat.server_inst, svc_handle, event->gatt_srvc_id.is_prmry);
1141 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1142 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1145 if (info->instance_id == event->gatt_srvc_stat.server_inst) {
1146 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1147 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_ADD_SERVICE,
1148 (int*)&svc_handle, sizeof(int));
1150 /* Add Service Handle */
1151 if (svc_handle > 0) {
1152 handle = g_malloc0(sizeof(int));
1153 *handle = svc_handle;
1154 numapps[event->gatt_srvc_stat.server_inst].service_handles = g_slist_append(numapps[event->gatt_srvc_stat.server_inst].service_handles, handle);
1155 count = g_slist_length(numapps[event->gatt_srvc_stat.server_inst].service_handles);
1156 BT_INFO("Added Service handle [%d] to list..current count [%d]", svc_handle, count);
1165 static void __bt_handle_gatt_server_characteristic_added(event_gatts_srvc_charctr_t *event)
1167 int result = BLUETOOTH_ERROR_NONE;
1169 bt_service_app_info_t *info = NULL;
1171 char *uuid_str = g_malloc0(BT_UUID_STRING_MAX);
1173 BT_INFO("GATT Server Char added status [%d]", event->gatt_srvc_stat.status);
1174 BT_INFO("GATT Server_inst: [%d]", event->gatt_srvc_stat.server_inst);
1175 BT_INFO("GATT Service Handle: [%d]", event->gatt_srvc_stat.servic_hndl);
1176 BT_INFO("GATT Add characteristic Status: [%d]", event->gatt_srvc_stat.status);
1177 BT_INFO("GATT Service characteristic Handle: [%d]", event->charctr_hndl);
1179 if (event->gatt_srvc_stat.status != OAL_STATUS_SUCCESS) {
1180 result = BLUETOOTH_ERROR_INTERNAL;
1181 char_handle = 0; /* characteristic handle set to 0 indicates.
1182 0 is reserved by BT SIG, cant be used by app*/
1184 char_handle = event->charctr_hndl;
1187 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1188 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1191 if (info->instance_id == event->gatt_srvc_stat.server_inst) {
1192 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1193 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_ADD_CHARACTERISTIC,
1194 (int*)&char_handle, sizeof(int));
1200 _bt_uuid_to_string(&(event->charctr_uuid), uuid_str);
1201 BT_INFO("GATT Added Characteristic: UUID: [%s]", uuid_str);
1206 static void __bt_handle_gatt_server_descriptor_added(event_gatts_srvc_descr_t* event)
1208 int result = BLUETOOTH_ERROR_NONE;
1210 bt_service_app_info_t *info = NULL;
1212 char *uuid_str = g_malloc0(BT_UUID_STRING_MAX);
1214 BT_INFO("GATT Server Descriptor added status [%d]", event->gatt_srvc_stat.status);
1215 BT_INFO("GATT Server_inst: [%d]", event->gatt_srvc_stat.server_inst);
1216 BT_INFO("GATT Service Handle: [%d]", event->gatt_srvc_stat.servic_hndl);
1217 BT_INFO("GATT Add Descriptor Status: [%d]", event->gatt_srvc_stat.status);
1218 BT_INFO("GATT Service Descriptor Handle: [%d]", event->descrptr_hndl);
1220 if (event->gatt_srvc_stat.status != OAL_STATUS_SUCCESS) {
1221 result = BLUETOOTH_ERROR_INTERNAL;
1222 desc_handle = 0; /* Service handle set to 0 indicates.
1223 0 is reserved by BT SIG, cant be used by app*/
1225 desc_handle = event->descrptr_hndl;
1227 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1228 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1231 if (info->instance_id == event->gatt_srvc_stat.server_inst) {
1232 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1233 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_ADD_DESCRIPTOR,
1234 (int*)&desc_handle, sizeof(int));
1239 _bt_uuid_to_string(&(event->descrptr_uuid), uuid_str);
1240 BT_INFO("GATT Added Descriptor: UUID: [%s]", uuid_str);
1245 static void __bt_handle_gatt_server_service_started(event_gatts_srvc_t *event)
1247 bt_service_app_info_t *info = NULL;
1248 int result = BLUETOOTH_ERROR_NONE;
1250 BT_INFO("GATT Server Service Started..");
1253 BT_INFO("GATT Server_inst: [%d]", event->server_inst);
1254 BT_INFO("GATT Service Handle: [%d]", event->servic_hndl);
1255 BT_INFO("GATT Service Started Status: [%d]", event->status);
1257 if (event->status != OAL_STATUS_SUCCESS) {
1258 BT_ERR("GATT Server Service Start Failed Err: [%d]", event->status);
1259 result = BLUETOOTH_ERROR_INTERNAL;
1262 /* Check if the just registered Instance ID belongs to requester */
1263 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1266 if (info->instance_id == event->server_inst) {
1267 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1268 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_START_SERVICE,
1269 (void*)info, sizeof(bt_service_app_info_t));
1275 static void __bt_handle_gatt_server_service_stopped(event_gatts_srvc_t *event)
1277 int result = BLUETOOTH_ERROR_NONE;
1278 bt_service_app_info_t *info = NULL;
1280 BT_INFO("GATT Server Service Stopped..");
1282 BT_INFO("GATT Server_inst: [%d]", event->server_inst);
1283 BT_INFO("GATT Service Handle: [%d]", event->servic_hndl);
1284 BT_INFO("GATT Service Stopped Status: [%d]", event->status);
1286 if (event->status != OAL_STATUS_SUCCESS) {
1287 BT_ERR("GATT Server Service Stop Failed Err: [%d]", event->status);
1288 result = BLUETOOTH_ERROR_INTERNAL;
1291 /* Check if the just registered Instance ID belongs to requester */
1292 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1295 if (info->instance_id == event->server_inst) {
1296 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1297 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_STOP_SERVICE,
1298 (void*)info, sizeof(bt_service_app_info_t));
1304 static void __bt_handle_gatt_server_service_deleted(event_gatts_srvc_t *event)
1306 int result = BLUETOOTH_ERROR_NONE;
1307 bt_service_app_info_t *info = NULL;
1309 BT_INFO("GATT Server Service Deleted..");
1311 BT_INFO("GATT Server_inst: [%d]", event->server_inst);
1312 BT_INFO("GATT Service Handle: [%d]", event->servic_hndl);
1313 BT_INFO("GATT Service Deleted Status: [%d]", event->status);
1315 if (event->status != OAL_STATUS_SUCCESS) {
1316 BT_ERR("GATT Server Service Delete Failed Err: [%d]", event->status);
1317 result = BLUETOOTH_ERROR_INTERNAL;
1320 /* Check if the just registered Instance ID belongs to requester */
1321 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1324 if (info->instance_id == event->server_inst) {
1325 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1326 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_DELETE_SERVICE,
1327 (void*)info, sizeof(bt_service_app_info_t));
1333 struct gatt_client_info_t *_bt_find_remote_gatt_client_info(char *address)
1336 struct gatt_client_info_t *info = NULL;
1337 for (l = gatt_client_info_list; l != NULL; l = g_slist_next(l)) {
1338 info = (struct gatt_client_info_t*)l->data;
1342 if (!g_strcmp0(info->addr, address)) {
1343 BT_INFO("Remote GATT client found addr[%s]", info->addr);
1350 static struct gatt_client_info_t *__bt_find_remote_gatt_client_info_from_conn_id(int conn_id)
1353 struct gatt_client_info_t *info = NULL;
1355 for (l = gatt_client_info_list; l != NULL; l = g_slist_next(l)) {
1356 info = (struct gatt_client_info_t*)l->data;
1360 if (info->connection_id == conn_id) {
1361 BT_INFO("Remote GATT client found addr[%s]", info->addr);
1368 #ifdef TIZEN_GATT_CLIENT
1369 struct gatt_server_info_t *_bt_find_remote_gatt_server_info(char *address)
1372 struct gatt_server_info_t *info = NULL;
1373 for (l = gatt_server_info_list; l != NULL; l = g_slist_next(l)) {
1374 info = (struct gatt_server_info_t*)l->data;
1378 if (!g_strcmp0(info->addr, address)) {
1379 BT_INFO("Remote GATT Server found addr[%s]", info->addr);
1386 static struct gatt_out_conn_info_t* __bt_find_gatt_outgoing_conn_info(char *address)
1389 struct gatt_out_conn_info_t *info = NULL;
1390 for (l = outgoing_gatt_conn_list; l != NULL; l = g_slist_next(l)) {
1391 info = (struct gatt_out_conn_info_t*)l->data;
1395 if (!g_strcmp0(info->addr, address)) {
1396 BT_INFO("Outgoing connection info found addr[%s]", info->addr);
1403 static struct gatt_server_info_t *__bt_find_remote_gatt_server_info_from_client_if(int client_if)
1406 struct gatt_server_info_t *info = NULL;
1408 for (l = gatt_server_info_list; l != NULL; l = g_slist_next(l)) {
1409 info = (struct gatt_server_info_t*)l->data;
1413 if (info->client_id == client_if) {
1414 BT_INFO("Remote GATT server found addr[%s]", info->addr);
1425 #ifdef TIZEN_GATT_CLIENT
1426 static void __bt_handle_gatt_server_connection_state(event_gatts_conn_t *event)
1428 int result = BLUETOOTH_ERROR_NONE;
1429 struct gatt_client_info_t *client_info = NULL;
1430 struct gatt_server_info_t *server_info = NULL;
1431 bluetooth_device_address_t dev_addr;
1432 GVariant *param = NULL;
1435 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1437 memcpy(dev_addr.addr, event->address.addr, 6);
1439 /* REPLY dbus Context to application which called BT_CONNECT_LE. There is status
1441 _bt_convert_addr_type_to_string(address,
1442 (unsigned char *)dev_addr.addr);
1444 if (event->status != OAL_STATUS_SUCCESS)
1445 result = BLUETOOTH_ERROR_INTERNAL;
1447 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
1448 address, BT_ADDRESS_STRING_SIZE);
1450 BT_INFO("GATT Server Connedted: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1451 address, event->server_inst, event->conn_id);
1454 /* Check if device is already in connected list */
1455 client_info = _bt_find_remote_gatt_client_info(address);
1458 BT_INFO("Conn Info absent: But no need to Send Local GATT Server Connected event to apps");
1460 param = g_variant_new("(is)", result, address);
1462 /* Send event to application */
1463 _bt_send_event(BT_DEVICE_EVENT,
1464 BLUETOOTH_EVENT_GATT_SERVER_CONNECTED, /* Local device is GATT server */
1467 /* Save client connection info */
1468 client_info = g_new0(struct gatt_client_info_t, 1);
1469 client_info->addr = g_strdup(address);
1470 BT_INFO("Added GATT client addr[%s]", client_info->addr);
1471 client_info->connection_id = event->conn_id;
1472 client_info->instance_id = event->server_inst;
1473 gatt_client_info_list = g_slist_append(gatt_client_info_list, client_info);
1474 BT_INFO("Total num of connected Remote GATT Clients [%d]", g_slist_length(gatt_client_info_list));
1476 /* Save server connection info */
1477 server_info = g_new0(struct gatt_server_info_t, 1);
1478 server_info->addr = g_strdup(address);
1479 server_info->client_id = -1;
1480 BT_INFO("Added GATT server addr[%s]", server_info->addr);
1481 server_info->connection_id = event->conn_id;
1482 gatt_server_info_list = g_slist_append(gatt_server_info_list, server_info);
1483 BT_INFO("Total num of connected Remote GATT Servers [%d]", g_slist_length(gatt_server_info_list));
1485 ret = gattc_add_connection_info((bt_address_t *)&dev_addr, event->conn_id, event->server_inst);
1486 if (ret != OAL_STATUS_SUCCESS) {
1487 BT_ERR("gattc register server instance failed");
1491 __bt_add_mtu_gatt_device(address);
1496 /* GATT Server Dis connected */
1497 static void __bt_handle_gatt_server_disconnection_state(event_gatts_conn_t *event)
1499 int result = BLUETOOTH_ERROR_NONE;
1500 struct gatt_client_info_t *client_info = NULL;
1501 struct gatt_server_info_t *server_info = NULL;
1502 bluetooth_device_address_t dev_addr;
1503 GVariant *param = NULL;
1504 char address[BT_ADDRESS_STRING_SIZE];
1506 memcpy(dev_addr.addr, event->address.addr, 6);
1508 /* REPLY dbus Context to application which called BT_DISCONNECT_LE. There is status
1510 _bt_convert_addr_type_to_string(address,
1511 (unsigned char *)dev_addr.addr);
1513 if (event->status != OAL_STATUS_SUCCESS)
1514 result = BLUETOOTH_ERROR_INTERNAL;
1516 if (NULL == _bt_get_request_info_data(BT_DISCONNECT_LE, address)) {
1517 if (NULL != _bt_get_request_info_data(BT_CONNECT_LE, address)) {
1518 result = BLUETOOTH_ERROR_INTERNAL;
1520 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
1521 address, BT_ADDRESS_STRING_SIZE);
1523 BT_ERR("Failed to connect Local GATT Server Remote Client addr[%s]", address);
1527 __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE,
1528 address, BT_ADDRESS_STRING_SIZE);
1531 BT_INFO("Local GATT Server DisConnected: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1532 address, event->server_inst, event->conn_id);
1533 /* Remove Connection info */
1534 client_info = _bt_find_remote_gatt_client_info(address);
1536 BT_INFO("No need to Send Local GATT Server Disconnected event to apps, just remove remote client info");
1538 param = g_variant_new("(is)", result, address);
1539 /* Send event to application */
1540 _bt_send_event(BT_DEVICE_EVENT,
1541 BLUETOOTH_EVENT_GATT_SERVER_DISCONNECTED, /* Local device is GATT server */
1544 /* Remove server info from list */
1545 server_info = _bt_find_remote_gatt_server_info(address);
1547 gatt_server_info_list = g_slist_remove(gatt_server_info_list, server_info);
1549 BT_INFO("Can not find conn info, already removed!");
1551 /* Remove client info from List */
1552 gatt_client_info_list = g_slist_remove(gatt_client_info_list, client_info);
1553 BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
1554 g_free(client_info->addr);
1555 g_free(client_info);
1558 __bt_remove_mtu_gatt_device(address);
1562 static void __bt_handle_gatt_server_connection_state(event_gatts_conn_t *event)
1564 int result = BLUETOOTH_ERROR_NONE;
1565 struct gatt_client_info_t *conn_info = NULL;
1566 bt_service_app_info_t *info = NULL;
1567 bluetooth_device_address_t dev_addr;
1568 GVariant *param = NULL;
1571 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1573 memcpy(dev_addr.addr, event->address.addr, 6);
1576 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1577 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1580 if (info->instance_id == event->server_inst) {
1581 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1582 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
1583 (bluetooth_device_address_t*)&dev_addr, sizeof(bluetooth_device_address_t));
1589 /* Send event to BT-API */
1590 _bt_convert_addr_type_to_string(address,
1591 (unsigned char *)dev_addr.addr);
1593 BT_INFO("GATT Server Connedted: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1594 address, event->server_inst, event->conn_id);
1597 /* Check if device is already in connected list */
1598 conn_info = _bt_find_remote_gatt_client_info(address);
1601 param = g_variant_new("(is)", result, address);
1602 /* Send event to application */
1603 _bt_send_event(BT_GATT_SERVER_EVENT,
1604 BLUETOOTH_EVENT_GATT_CONNECTED,
1607 /* Save Connection info */
1608 conn_info = g_new0(struct gatt_client_info_t, 1);
1609 conn_info->addr = g_strdup(address);
1610 BT_INFO("Added GATT client addr[%s]", conn_info->addr);
1611 conn_info->connection_id = event->conn_id;
1612 conn_info->instance_id = event->server_inst;
1613 gatt_client_info_list = g_slist_append(gatt_client_info_list, conn_info);
1614 BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
1620 /* GATT Server Dis connected */
1621 static void __bt_handle_gatt_server_disconnection_state(event_gatts_conn_t *event)
1623 int result = BLUETOOTH_ERROR_NONE;
1624 struct gatt_client_info_t *conn_info = NULL;
1625 bluetooth_device_address_t dev_addr;
1626 bt_service_app_info_t *info = NULL;
1627 GVariant *param = NULL;
1630 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1632 memcpy(dev_addr.addr, event->address.addr, 6);
1634 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1635 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1638 if (info->instance_id == event->server_inst) {
1639 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1640 __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE,
1641 (bluetooth_device_address_t*)&dev_addr, sizeof(bluetooth_device_address_t));
1647 /* Send event to BT-API */
1648 _bt_convert_addr_type_to_string(address,
1649 (unsigned char *)dev_addr.addr);
1651 BT_INFO("GATT Server DisConnected: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1652 address, event->server_inst, event->conn_id);
1654 param = g_variant_new("(is)", result, address);
1655 /* Send event to application */
1656 _bt_send_event(BT_GATT_SERVER_EVENT,
1657 BLUETOOTH_EVENT_GATT_DISCONNECTED,
1660 /* Remove Connection info */
1661 conn_info = _bt_find_remote_gatt_client_info(address);
1663 BT_INFO("Remove GATT client info from List..");
1664 /* Remove info from List */
1665 gatt_client_info_list = g_slist_remove(gatt_client_info_list, conn_info);
1666 BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
1667 g_free(conn_info->addr);
1676 static void __bt_handle_gatt_server_acquire_write_requested(event_gatts_srvc_acquire_attr_t *event)
1678 GVariant *param = NULL;
1679 int result = BLUETOOTH_ERROR_NONE;
1680 struct gatt_server_req_info *req_info = NULL;
1681 bluetooth_device_address_t dev_addr;
1682 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1684 BT_INFO("GATT Server ACQUIRE Write Req Connection ID: [%d]", event->attr_trans.conn_id);
1685 BT_INFO("GATT Server ACQUIRE Write Req Transaction ID:[%d]", event->attr_trans.trans_id);
1686 BT_INFO("GATT Server ACQUIRE Write Req Attribute Handle: [%d]", event->attr_trans.attr_handle);
1688 //address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1689 memcpy(dev_addr.addr, event->address.addr, 6);
1691 req_info = g_new0(struct gatt_server_req_info, 1);
1692 req_info->request_id = event->attr_trans.trans_id;
1693 req_info->attribute_handle = event->attr_trans.attr_handle;
1694 req_info->connection_id = event->attr_trans.conn_id;
1695 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_ACQUIRE_WRITE;
1696 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1698 _bt_convert_addr_type_to_string(address,
1699 (unsigned char *)dev_addr.addr);
1701 param = g_variant_new("(iiiiiis)", result,
1702 event->attr_trans.conn_id,
1703 event->attr_trans.trans_id,
1704 event->attr_trans.attr_handle,
1705 event->mtu, event->attr_trans.offset, address);
1706 BT_INFO("GATT Server ACQUIRE Write Req Attribute remote address : [%s]", address);
1708 _bt_send_event(BT_GATT_SERVER_EVENT,
1709 BLUETOOTH_EVENT_GATT_SERVER_ACQUIRE_WRITE,
1714 static void __bt_handle_gatt_server_acquire_notify_requested(event_gatts_srvc_acquire_attr_t *event)
1716 GVariant *param = NULL;
1717 int result = BLUETOOTH_ERROR_NONE;
1718 struct gatt_server_req_info *req_info = NULL;
1719 bluetooth_device_address_t dev_addr;
1720 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1722 BT_INFO("GATT Server ACQUIRE Notify Req Connection ID: [%d]", event->attr_trans.conn_id);
1723 BT_INFO("GATT Server ACQUIRE Notify Req Transaction ID:[%d]", event->attr_trans.trans_id);
1724 BT_INFO("GATT Server ACQUIRE Notify Req Attribute Handle: [%d]", event->attr_trans.attr_handle);
1725 BT_INFO("GATT Server ACQUIRE notify Req address : [%s]", address);
1727 memcpy(dev_addr.addr, event->address.addr, 6);
1728 _bt_convert_addr_type_to_string(address,
1729 (unsigned char *)dev_addr.addr);
1730 BT_INFO("GATT Server ACQUIRE notify Req remote address : [%s]", address);
1732 req_info = g_new0(struct gatt_server_req_info, 1);
1733 req_info->request_id = event->attr_trans.trans_id;
1734 req_info->attribute_handle = event->attr_trans.attr_handle;
1735 req_info->connection_id = event->attr_trans.conn_id;
1736 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_ACQUIRE_NOTIFY;
1737 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1739 param = g_variant_new("(iiiiiis)", result,
1740 event->attr_trans.conn_id,
1741 event->attr_trans.trans_id,
1742 event->attr_trans.attr_handle,
1743 event->mtu, event->attr_trans.offset,
1746 BT_INFO("GATT Server ACQUIRE Notify Req Attribute : ");
1748 _bt_send_event(BT_GATT_SERVER_EVENT,
1749 BLUETOOTH_EVENT_GATT_SERVER_ACQUIRE_NOTIFY,
1753 static void __bt_handle_gatt_server_write_requested(event_gatts_srvc_write_attr_t *event)
1756 bluetooth_device_address_t dev_addr;
1757 GVariant *param = NULL;
1758 int result = BLUETOOTH_ERROR_NONE;
1760 gboolean is_prepare_write;
1762 GVariant *data = NULL;
1764 struct gatt_server_req_info *req_info = NULL;
1765 BT_INFO("GATT Server Write Requested");
1767 memcpy(dev_addr.addr, event->address.addr, 6);
1769 BT_INFO("GATT Server Write Req Connection ID: [%d]", event->attr_trans.conn_id);
1770 BT_INFO("GATT Server Write Req Transaction ID:[%d]", event->attr_trans.trans_id);
1771 BT_INFO("GATT Server Write Req Attribute Handle: [%d]", event->attr_trans.attr_handle);
1772 BT_INFO("GATT Server Write Req Attribute Offset: [%d]", event->attr_trans.offset);
1773 BT_INFO("GATT Server Write Req value length [%d]", event->length);
1774 BT_INFO("GATT Server Write Req needs response: [%d]", event->need_rsp);
1775 BT_INFO("GATT Server Write Req Is Prep: [%d]", event->is_prep);
1777 if (event->length <= 0) {
1778 BT_INFO("GATT Server write requested, but length of attr value is 0");
1782 need_resp = event->need_rsp;
1783 is_prepare_write = event->is_prep;
1785 write_val = g_memdup(&event->value[0], event->length);
1787 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1788 _bt_convert_addr_type_to_string(address,
1789 (unsigned char *)dev_addr.addr);
1791 BT_INFO("GATT Server Write Request from remote client [%s]", address);
1793 if (event->length > 0) {
1794 for (i = 0; i < event->length; i++)
1795 BT_DBG("Data[%d] = [0x%x]", i, event->value[i]);
1797 /* Save Write Request Info */
1798 req_info = g_new0(struct gatt_server_req_info, 1);
1799 req_info->request_id = event->attr_trans.trans_id;
1800 req_info->attribute_handle = event->attr_trans.attr_handle;
1801 req_info->connection_id = event->attr_trans.conn_id;
1802 req_info->addr = address;
1803 req_info->offset = event->attr_trans.offset;
1804 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_WRITE;
1805 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1807 data = g_variant_new_from_data(
1808 G_VARIANT_TYPE_BYTESTRING,
1813 param = g_variant_new("(iiiiiibbsn@ay)", result,
1814 event->attr_trans.conn_id,
1815 event->attr_trans.trans_id,
1816 event->attr_trans.attr_handle,
1817 event->attr_trans.offset,
1825 _bt_send_event(BT_GATT_SERVER_EVENT,
1826 BLUETOOTH_EVENT_GATT_SERVER_VALUE_CHANGED,
1832 static void __bt_handle_gatt_server_read_requested(event_gatts_srvc_read_attr_t *event)
1834 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1835 bluetooth_device_address_t dev_addr;
1836 int result = BLUETOOTH_ERROR_NONE;
1837 struct gatt_server_req_info *req_info = NULL;
1838 GVariant *param = NULL;
1840 BT_INFO("GATT Server Read Requested");
1842 memcpy(dev_addr.addr, event->address.addr, 6);
1844 BT_INFO("GATT Server-Client Connection ID: [%d]", event->attr_trans.conn_id);
1845 BT_INFO("GATT Server-Client Transaction ID: [%d]", event->attr_trans.trans_id);
1846 BT_INFO("GATT Server Attribute Handle: [%d]", event->attr_trans.attr_handle);
1847 BT_INFO("GATT Server Attribute Offset for read: [%d]", event->attr_trans.offset);
1848 BT_INFO("GATT Server Attribute is long: [%d]", event->is_long);
1850 is_long = event->is_long;
1852 /* Save Read Request Info */
1853 req_info = g_new0(struct gatt_server_req_info, 1);
1854 req_info->request_id = event->attr_trans.trans_id;
1855 req_info->attribute_handle = event->attr_trans.attr_handle;
1856 req_info->connection_id = event->attr_trans.conn_id;
1857 req_info->addr = address;
1858 req_info->offset = event->attr_trans.offset;
1859 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_READ;
1860 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1862 /* Send event to BT-API */
1863 _bt_convert_addr_type_to_string(address,
1864 (unsigned char *)dev_addr.addr);
1866 BT_INFO("GATT Server Read Request from remote client [%s]", address);
1868 param = g_variant_new("(iiiiibs)", result,
1869 event->attr_trans.conn_id,
1870 event->attr_trans.trans_id,
1871 event->attr_trans.attr_handle,
1872 event->attr_trans.offset,
1876 _bt_send_event(BT_GATT_SERVER_EVENT,
1877 BLUETOOTH_EVENT_GATT_SERVER_READ_REQUESTED,
1881 static void __bt_handle_gatt_server_indicate_confirmed(event_gatts_ind_cnfrm_t *event)
1883 bluetooth_device_address_t dev_addr;
1884 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1885 int cur_connected_clients;
1886 static int recvd = 0;
1887 gboolean completed = 0;
1888 GVariant *param = NULL;
1890 /* OAL event does provide error, so MW assumes event will never contain wrong data,
1891 incase of any issues, check with OAL */
1892 int result = BLUETOOTH_ERROR_NONE;
1894 memcpy(dev_addr.addr, event->address.addr, 6);
1895 _bt_convert_addr_type_to_string(address,
1896 (unsigned char *)dev_addr.addr);
1898 BT_INFO("Indication sent to GATT client [%s] conn_ ID [%d] transaction ID [%d] Att handle [%d]",
1899 address, event->conn_id, event->trans_id, event->attr_handle);
1902 cur_connected_clients = g_slist_length(gatt_client_info_list);
1903 BT_INFO("Number of connected clients during sending Indication [%d] & current connected count [%d]",
1904 num_indicate_clients, cur_connected_clients);
1907 if (recvd == num_indicate_clients) {
1908 BT_INFO("Gatt indication confirm event for last GATT client.. [%s]", address);
1909 completed = 1; /* Last event */
1910 recvd = 0; /* Reset */
1911 num_indicate_clients = 0;
1914 param = g_variant_new("(isib)",
1920 /* Send event to BT-API */
1921 _bt_send_event(BT_GATT_SERVER_EVENT,
1922 BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_COMPLETED,
1925 BT_INFO("Received Indication confirm for client number [%d]", recvd);
1929 /* Tizen Platform Specific */
1930 static void __bt_handle_gatt_server_notification_changed(event_gatts_notif_t *event)
1932 bluetooth_device_address_t dev_addr;
1933 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1934 GVariant *param = NULL;
1937 /* OAL event does provide error, so MW assumes event will never contain wrong data,
1938 incase of any issues, check with OAL */
1939 int result = BLUETOOTH_ERROR_NONE;
1941 memcpy(dev_addr.addr, event->address.addr, 6);
1942 _bt_convert_addr_type_to_string(address,
1943 (unsigned char *)dev_addr.addr);
1945 BT_INFO("notification_changed [%s] conn_ ID [%d] transaction ID [%d] Att handle [%d] Notify[%d]",
1946 address, event->conn_id, event->trans_id, event->attr_handle, event->notify);
1948 /* Set Notifcation status */
1949 notify = event->notify;
1951 param = g_variant_new("(isib)",
1957 /* Send event to BT-API */
1958 _bt_send_event(BT_GATT_SERVER_EVENT,
1959 BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_STATE_CHANGED,
1965 static void __bt_handle_gatt_mtu_changed_event(event_gatts_mtu_changed_t *event)
1967 int result = BLUETOOTH_ERROR_NONE;
1968 struct gatt_client_info_t *conn_info = NULL;
1969 GVariant *param = NULL;
1971 BT_INFO("GATT Server MTU changed event");
1973 conn_info = __bt_find_remote_gatt_client_info_from_conn_id(event->conn_id);
1974 if (conn_info == NULL) {
1975 BT_ERR("Cant find connection Information");
1978 BT_INFO("Got connection Info GATT client [%s] MTU Size [%d]",
1979 conn_info->addr, event->mtu_size);
1981 __bt_update_mtu_gatt_device(conn_info->addr, event->mtu_size);
1983 param = g_variant_new("(isqy)",
1989 /* Send event to BT-API */
1990 _bt_send_event(BT_GATT_SERVER_EVENT,
1991 BLUETOOTH_EVENT_GATT_ATT_MTU_CHANGED,
1995 static void __bt_gatt_event_handler(int event_type, gpointer event_data)
1997 BT_INFO("OAL event = 0x%x, \n", event_type);
1998 switch (event_type) {
1999 case OAL_EVENT_BLE_SERVER_INSTANCE_INITIALISED: {
2000 BT_INFO("OAL Event: Server Instance Registered");
2001 /* GATT Server Registered event is handled in MAIN thread context */
2002 __bt_handle_server_instance_registered((event_gatts_register_t *)event_data);
2005 case OAL_EVENT_GATTS_SERVICE_ADDED: {
2006 BT_INFO("OAL Event: GATT Service added");
2007 __bt_handle_gatt_server_service_added((event_gatts_srvc_prm_t *)event_data);
2010 case OAL_EVENT_GATTS_CHARACTERISTIC_ADDED: {
2011 BT_INFO("OAL Event: GATT characteristic added");
2012 __bt_handle_gatt_server_characteristic_added((event_gatts_srvc_charctr_t *)event_data);
2015 case OAL_EVENT_GATTS_DESCRIPTOR_ADDED: {
2016 BT_INFO("OAL Event: GATT descriptor added");
2017 __bt_handle_gatt_server_descriptor_added((event_gatts_srvc_descr_t *)event_data);
2020 case OAL_EVENT_GATTS_SERVICE_STARTED: {
2021 BT_INFO("OAL Event: GATT Service started");
2022 __bt_handle_gatt_server_service_started((event_gatts_srvc_t *)event_data);
2025 case OAL_EVENT_GATTS_SERVICE_STOPED: {
2026 BT_INFO("OAL Event: GATT Service stopped");
2027 __bt_handle_gatt_server_service_stopped((event_gatts_srvc_t *)event_data);
2030 case OAL_EVENT_GATTS_SERVICE_DELETED: {
2031 BT_INFO("OAL Event: GATT Service deleted");
2032 __bt_handle_gatt_server_service_deleted((event_gatts_srvc_t *) event_data);
2035 case OAL_EVENT_GATTS_CONNECTION_COMPLETED: {
2036 BT_INFO("OAL Event: GATT Server Connected");
2037 __bt_handle_gatt_server_connection_state((event_gatts_conn_t *)event_data);
2040 case OAL_EVENT_GATTS_DISCONNECTION_COMPLETED: {
2041 BT_INFO("OAL Event: GATT Server Disconnected");
2042 __bt_handle_gatt_server_disconnection_state((event_gatts_conn_t *)event_data);
2045 case OAL_EVENT_GATTS_REQUEST_READ: {
2046 BT_INFO("OAL Event: GATT Server Read Request");
2047 __bt_handle_gatt_server_read_requested((event_gatts_srvc_read_attr_t *)event_data);
2050 case OAL_EVENT_GATTS_REQUEST_WRITE: {
2051 BT_INFO("OAL Event: GATT Server Write Request");
2052 __bt_handle_gatt_server_write_requested((event_gatts_srvc_write_attr_t *)event_data);
2055 case OAL_EVENT_GATTS_REQUEST_ACQUIRE_WRITE: {
2056 BT_INFO("OAL Event: GATT Server Acquire Write Request");
2057 __bt_handle_gatt_server_acquire_write_requested((event_gatts_srvc_acquire_attr_t*)event_data);
2060 case OAL_EVENT_GATTS_REQUEST_ACQUIRE_NOTIFY: {
2061 BT_INFO("OAL Event: GATT ServerAcquire Notify Request");
2062 __bt_handle_gatt_server_acquire_notify_requested((event_gatts_srvc_acquire_attr_t*)event_data);
2065 case OAL_EVENT_GATTS_IND_CONFIRM: {
2066 BT_INFO("OAL Event: GATT Server Indication confirmed");
2067 __bt_handle_gatt_server_indicate_confirmed((event_gatts_ind_cnfrm_t *)event_data);
2070 case OAL_EVENT_GATTS_NOTIFICATION: { /* Tizen Platform Specific */
2071 BT_INFO("OAL Event: GATT Server DisConnected");
2072 __bt_handle_gatt_server_notification_changed((event_gatts_notif_t *)event_data);
2075 case OAL_EVENT_GATTS_MTU_CHANGED: {
2076 BT_INFO("OAL Event: GATT Server MTU changed event callback");
2077 __bt_handle_gatt_mtu_changed_event((event_gatts_mtu_changed_t *)event_data);
2080 #ifdef TIZEN_GATT_CLIENT
2081 case OAL_EVENT_GATTC_REGISTRATION: {
2082 BT_INFO("OAL Event: GATT Client instance Registered");
2083 __bt_handle_client_instance_registered((event_gattc_register_t *) event_data);
2086 case OAL_EVENT_GATTC_CONNECTION_COMPLETED: {
2087 BT_INFO("OAL Event: GATT Client Connected");
2088 __bt_handle_client_connected((event_gattc_conn_t *) event_data);
2091 case OAL_EVENT_GATTC_DISCONNECTION_COMPLETED: {
2092 BT_INFO("OAL Event: GATT Client DisConnected");
2093 __bt_handle_client_disconnected((event_gattc_conn_t *) event_data);
2096 case OAL_EVENT_GATTC_SERVICE_SEARCH_RESULT: {
2097 BT_INFO("OAL Event: GATT Client Service Search Result");
2098 __bt_handle_client_service_search_result((event_gattc_service_result_t *) event_data);
2101 case OAL_EVENT_GATTC_SERVICE_SEARCH_DONE: {
2102 BT_INFO("OAL Event: GATT Client Service Completed");
2103 __bt_handle_client_service_search_completed((event_gattc_conn_status_t *) event_data);
2106 case OAL_EVENT_GATTC_CHARAC_SERACH_RESULT: {
2107 BT_INFO("OAL Event: GATT Client Characteristic Search Result");
2108 __bt_handle_client_characteristic_search_result((event_gattc_characteristic_result_t *) event_data);
2111 case OAL_EVENT_GATTC_DESC_SERACH_RESULT: {
2112 BT_INFO("OAL Event: GATT Client Descriptor Search Result");
2113 __bt_handle_client_descriptor_search_result((event_gattc_descriptor_result_t *) event_data);
2116 case OAL_EVENT_GATTC_READ_CHARAC: {
2117 BT_INFO("OAL Event: GATT Client Characteristic Read Data");
2118 __bt_handle_client_characteristic_read_data((event_gattc_read_data *) event_data);
2121 case OAL_EVENT_GATTC_READ_DESCR: {
2122 BT_INFO("OAL Event: GATT Client Descriptor Read Data");
2123 __bt_handle_client_descriptor_read_data((event_gattc_read_data *) event_data);
2126 case OAL_EVENT_GATTC_WRITE_CHARAC: {
2127 BT_INFO("OAL Event: GATT Client Characteristic Write Data");
2128 __bt_handle_client_characteristic_write_data((event_gattc_write_data *) event_data);
2131 case OAL_EVENT_GATTC_WRITE_DESCR: {
2132 BT_INFO("OAL Event: GATT Client Descriptor Write Data");
2133 __bt_handle_client_descriptor_write_data((event_gattc_write_data *) event_data);
2136 case OAL_EVENT_DEVICE_LE_DISCONNECTED: {
2137 BT_INFO("OAL Event: LE device disconnected");
2138 __bt_hanlde_le_device_disconnection((event_dev_conn_status_t *)event_data);
2141 case OAL_EVENT_GATTC_NOTIFICATION_REGISTERED: {
2142 BT_INFO("OAL Event: GATT Client Notification Registered");
2143 __bt_handle_client_notification_registered((event_gattc_regdereg_notify_t *) event_data, TRUE);
2146 case OAL_EVENT_GATTC_NOTIFICATION_DEREGISTERED: {
2147 BT_INFO("OAL Event: GATT Client Notification Registered");
2148 __bt_handle_client_notification_registered((event_gattc_regdereg_notify_t *) event_data, FALSE);
2151 case OAL_EVENT_GATTC_NOTIFY_DATA: {
2152 BT_INFO("OAL Event: GATT Client Notification Data");
2153 __bt_handle_client_notification_data((event_gattc_notify_data *) event_data);
2156 case OAL_EVENT_GATTC_SERVICE_CHANGED_IND: {
2157 BT_INFO("OAL Event: GATT Client service changed indication");
2158 __bt_handle_client_service_changed_ind((event_gattc_service_changed_data *)event_data);
2161 case OAL_EVENT_GATTC_MTU_EXCHANGE_COMPLETED: {
2162 BT_INFO("OAL Event: GATT Client MTU Exchange Complete");
2163 __bt_handle_client_mtu_exchange_completed((event_gattc_mtu_configured_t *) event_data);
2173 int _bt_gatt_server_add_service(char *sender, int service_type,
2174 int num_handles, char *svc_uuid, int instance_id)
2176 BT_CHECK_PARAMETER(svc_uuid, return);
2177 BT_CHECK_PARAMETER(sender, return);
2178 int ret = OAL_STATUS_SUCCESS;
2180 oal_gatt_srvc_id_t svc_data;
2182 svc_data.is_prmry = service_type;
2183 svc_data.id.inst_id = instance_id;
2185 BT_INFO("Service UUID [%s] Num handles [%d] Instance ID [%d]", svc_uuid, num_handles, instance_id);
2186 _bt_string_to_uuid(svc_uuid, (service_uuid_t*)&svc_data.id.uuid);
2188 ret = gatts_add_service(instance_id, &svc_data, num_handles);
2189 if (ret != OAL_STATUS_SUCCESS) {
2190 BT_ERR("ret: %d", ret);
2191 return BLUETOOTH_ERROR_INTERNAL;
2194 return BLUETOOTH_ERROR_NONE;
2198 int _bt_gatt_server_add_included_service(char *sender, int instance_id,
2199 int service_handle, int included_svc_handle)
2201 BT_CHECK_PARAMETER(sender, return);
2202 int ret = OAL_STATUS_SUCCESS;
2204 ret = gatts_add_included_services(instance_id, service_handle, included_svc_handle);
2205 if (ret != OAL_STATUS_SUCCESS) {
2206 BT_ERR("ret: %d", ret);
2207 return BLUETOOTH_ERROR_INTERNAL;
2209 return BLUETOOTH_ERROR_NONE;
2212 int _bt_gatt_server_add_characteristic(char *sender, char *char_uuid,
2213 bluetooth_gatt_server_attribute_params_t *param)
2215 BT_CHECK_PARAMETER(char_uuid, return);
2216 BT_CHECK_PARAMETER(sender, return);
2217 BT_CHECK_PARAMETER(param, return);
2218 int ret = OAL_STATUS_SUCCESS;
2220 oal_uuid_t uuid = {{0} };
2222 BT_INFO("Char UUID [%s] Instance ID [%d]", char_uuid, param->instance_id);
2223 _bt_string_to_uuid(char_uuid, (service_uuid_t*)&uuid);
2225 BT_INFO("Char permission From API [0x%x]", param->permissions);
2227 ret = gatts_add_characteristics(param->instance_id, param->service_handle, &uuid,
2228 param->properties, (int)param->permissions);
2229 if (ret != OAL_STATUS_SUCCESS) {
2230 BT_ERR("ret: %d", ret);
2231 return BLUETOOTH_ERROR_INTERNAL;
2233 return BLUETOOTH_ERROR_NONE;
2236 int _bt_gatt_server_add_descriptor(char *sender, char *desc_uuid,
2237 bt_gatt_permission_t *param, int service_handle, int instance_id)
2239 BT_CHECK_PARAMETER(desc_uuid, return);
2240 BT_CHECK_PARAMETER(sender, return);
2241 BT_CHECK_PARAMETER(param, return);
2242 int ret = OAL_STATUS_SUCCESS;
2244 oal_uuid_t uuid = {{0} };
2246 BT_INFO("Descriptor UUID [%s] Instance ID [%d] Service handle [%d]",
2247 desc_uuid, service_handle, instance_id);
2249 _bt_string_to_uuid(desc_uuid, (service_uuid_t*)&uuid);
2251 BT_INFO("Descriptor permission From API [0x%x]", *param);
2252 ret = gatts_add_descriptor(instance_id, service_handle, &uuid, (int)*param);
2254 if (ret != OAL_STATUS_SUCCESS) {
2255 BT_ERR("ret: %d", ret);
2256 return BLUETOOTH_ERROR_INTERNAL;
2258 return BLUETOOTH_ERROR_NONE;
2261 int _bt_gatt_server_start_service(char *sender, int service_handle, int instance_id)
2263 BT_CHECK_PARAMETER(sender, return);
2264 int ret = OAL_STATUS_SUCCESS;
2266 ret = gatts_start_service(instance_id, service_handle, BT_GATT_TRANSPORT_LE);
2267 if (ret != OAL_STATUS_SUCCESS) {
2268 BT_ERR("ret: %d", ret);
2269 return BLUETOOTH_ERROR_INTERNAL;
2271 return BLUETOOTH_ERROR_NONE;
2274 int _bt_gatt_server_stop_service(char *sender, int service_handle, int instance_id)
2276 BT_CHECK_PARAMETER(sender, return);
2277 int ret = OAL_STATUS_SUCCESS;
2279 ret = gatts_stop_service(instance_id, service_handle);
2280 if (ret != OAL_STATUS_SUCCESS) {
2281 BT_ERR("ret: %d", ret);
2282 return BLUETOOTH_ERROR_INTERNAL;
2284 return BLUETOOTH_ERROR_NONE;
2287 int _bt_gatt_server_delete_service(char *sender, int service_handle, int instance_id)
2289 BT_CHECK_PARAMETER(sender, return);
2290 int ret = OAL_STATUS_SUCCESS;
2294 ret = gatts_delete_service(instance_id, service_handle);
2295 if (ret != OAL_STATUS_SUCCESS) {
2296 BT_ERR("ret: %d", ret);
2297 return BLUETOOTH_ERROR_INTERNAL;
2300 /* Remove the Service Handle */
2301 for (l = numapps[instance_id].service_handles; l != NULL;) {
2303 l = g_slist_next(l);
2304 if (handle && *handle == service_handle) {
2305 BT_INFO("Remove Service handle [%d]", *handle);
2306 numapps[instance_id].service_handles = g_slist_remove(numapps[instance_id].service_handles, handle);
2312 return BLUETOOTH_ERROR_NONE;
2315 int _bt_gatt_server_send_response(char *sender, bluetooth_gatt_att_data_t *data,
2316 bluetooth_gatt_server_response_params_t *param)
2318 BT_CHECK_PARAMETER(sender, return);
2319 BT_CHECK_PARAMETER(data, return);
2320 BT_CHECK_PARAMETER(param, return);
2321 struct gatt_server_req_info *req_info = NULL;
2322 int ret = OAL_STATUS_SUCCESS;
2324 oal_gatt_response_t response;
2326 BT_INFO("GATT Server Response: Req Type [%d] req_id [%d] status [%d] auth_req [%d] offset[%d] data len[%d]",
2327 param->req_type, param->request_id,
2328 param->response_status, param->auth_req,
2329 data->offset, data->length);
2331 /* Search for matching Request in List */
2332 req_info = __bt_gatt_server_find_request_info(param->request_id, param->req_type);
2334 BT_ERR("GATT Server Req Info not found for current response..return Error");
2335 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
2338 memset(&response, 0x00, sizeof(oal_gatt_response_t));
2340 response.handle = req_info->attribute_handle;
2341 response.attr_value.auth_req = param->auth_req;
2342 response.attr_value.handle = req_info->attribute_handle;
2343 response.attr_value.offset = data->offset;
2344 response.attr_value.len = data->length;
2345 memcpy(&response.attr_value.value, &data->data, data->length);
2348 ret = gatts_send_response(req_info->connection_id, param->request_id,
2349 param->response_status, &response);
2351 if (ret != OAL_STATUS_SUCCESS) {
2352 BT_ERR("ret: %d", ret);
2353 return BLUETOOTH_ERROR_INTERNAL;
2356 BT_INFO("GATT Server Response successfully sent");
2357 /* Remove GATT server request from list */
2358 gatt_server_requests = g_slist_remove(gatt_server_requests, req_info);
2359 g_free(req_info->addr);
2361 return BLUETOOTH_ERROR_NONE;
2364 int _bt_gatt_server_acquire_send_response(char *sender, bluetooth_gatt_server_acquire_response_params_t *param , void *fd_list)
2366 BT_CHECK_PARAMETER(sender, return);
2367 BT_CHECK_PARAMETER(param, return);
2368 struct gatt_server_req_info *req_info = NULL;
2369 int ret = OAL_STATUS_SUCCESS;
2372 BT_INFO("GATT acquire Server Response: Req Type [%d] req_id [%d] fd [%d] mtu[%d]",
2373 param->req_type, param->request_id,
2377 /* Search for matching Request in List */
2378 req_info = __bt_gatt_server_find_request_info(param->request_id, param->req_type);
2380 BT_ERR("GATT acquire Server Req Info not found for current response..return Error");
2381 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
2384 ret = gatt_send_response_acquire(req_info->connection_id, param->request_id, 0, param->fd, param->mtu, fd_list);
2386 if (ret != OAL_STATUS_SUCCESS) {
2387 BT_ERR("ret: %d", ret);
2388 return BLUETOOTH_ERROR_INTERNAL;
2391 BT_INFO("GATT acquire Server Response successfully sent");
2392 /* Remove GATT server request from list */
2393 gatt_server_requests = g_slist_remove(gatt_server_requests, req_info);
2394 g_free(req_info->addr);
2396 return BLUETOOTH_ERROR_NONE;
2401 int _bt_gatt_server_send_indication(char *sender, bluetooth_device_address_t *dev_addr,
2402 bluetooth_gatt_att_data_t *data,
2403 bluetooth_gatt_server_indication_params_t *param)
2405 BT_CHECK_PARAMETER(sender, return);
2406 BT_CHECK_PARAMETER(data, return);
2407 BT_CHECK_PARAMETER(param, return);
2409 gboolean all_send = FALSE;
2410 int ret = OAL_STATUS_SUCCESS;
2411 struct gatt_client_info_t *conn;
2413 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2414 _bt_convert_addr_type_to_string(address, dev_addr->addr);
2416 if (memcmp(dev_addr->addr, BDADDR_ANY, 6) == 0) {
2417 BT_INFO("GATT Server: Send Indication to all connected GATT clients..");
2420 BT_INFO("GATT Server: Send Indication to connected GATT client addr [%s]", address);
2423 /* Attempt to send Notification/Indication to all Connected GATT clients */
2425 ret = __bt_gatt_send_indication_to_all_connected_clients(data, param);
2426 if (ret != OAL_STATUS_SUCCESS) {
2427 BT_ERR("ret: %d", ret);
2429 return BLUETOOTH_ERROR_INTERNAL;
2433 conn = _bt_find_remote_gatt_client_info(address);
2435 ret = gatts_send_indication(param->instance_id, param->atrribute_handle,
2436 conn->connection_id, data->length,
2437 param->need_confirmation, (char *)(&data->data[0]));
2439 if (ret != OAL_STATUS_SUCCESS) {
2440 BT_ERR("ret: %d", ret);
2441 BT_INFO("Indication failed to send to Remote GATT Client [%s]", address);
2443 return BLUETOOTH_ERROR_INTERNAL;
2445 BT_INFO("Indication sent to Remote GATT Client [%s] wait for Notification completed event from OAL", address);
2447 num_indicate_clients = 1;
2448 return BLUETOOTH_ERROR_NONE;
2450 BT_ERR("Remote GATT client [%s] is not connected..Cant send Indication!!", address);
2452 return BLUETOOTH_ERROR_NOT_CONNECTED;
2455 return BLUETOOTH_ERROR_NONE;
2458 int _bt_gatt_server_update_attribute_value(char *sender, int instance_id,
2459 bluetooth_gatt_server_update_value_t *param)
2461 BT_CHECK_PARAMETER(sender, return);
2462 BT_CHECK_PARAMETER(param, return);
2463 int ret = OAL_STATUS_SUCCESS;
2465 oal_gatt_value_t value;
2466 BT_INFO("GATT Server Update value: Instance ID [%d] attr handle [%d] Value len [%d]",
2467 instance_id, param->attribute_handle, param->length);
2470 memset(&value, 0x00, sizeof(oal_gatt_value_t));
2472 value.handle = param->attribute_handle;
2473 value.len = param->length;
2474 memcpy(&value.value, ¶m->data.data, param->length);
2476 ret = gatts_update_att_value(instance_id, &value);
2478 if (ret != OAL_STATUS_SUCCESS) {
2479 BT_ERR("ret: %d", ret);
2480 return BLUETOOTH_ERROR_INTERNAL;
2483 BT_INFO("GATT Server Update Attribute Value successfully");
2484 return BLUETOOTH_ERROR_NONE;
2487 int _bt_request_att_mtu(bluetooth_device_address_t *device_address,
2490 struct gatt_server_info_t *conn_info = NULL;
2492 int ret = OAL_STATUS_SUCCESS;
2494 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
2495 _bt_convert_addr_type_to_string(addr, device_address->addr);
2497 /* Check if remote GATT Server is connected or not */
2498 conn_info = _bt_find_remote_gatt_server_info(addr);
2500 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
2501 conn_info->addr, conn_info->connection_id);
2503 BT_ERR("GATT Server is not yet connected..");
2505 return BLUETOOTH_ERROR_NOT_CONNECTED;
2508 ret = gattc_configure_mtu(conn_info->connection_id, mtu);
2509 if (ret != OAL_STATUS_SUCCESS) {
2510 BT_ERR("ret: %d", ret);
2512 return BLUETOOTH_ERROR_INTERNAL;
2516 return BLUETOOTH_ERROR_NONE;
2519 int _bt_get_att_mtu(bluetooth_device_address_t *address,
2522 BT_CHECK_PARAMETER(address, return);
2523 BT_CHECK_PARAMETER(mtu, return);
2524 struct gatt_client_info_t *conn_info = NULL;
2525 char addr[BT_ADDRESS_STRING_SIZE] = { 0 };
2526 int ret = OAL_STATUS_SUCCESS;
2529 _bt_convert_addr_type_to_string(addr, address->addr);
2531 BT_INFO("Get current MTU size for the remote client:DevAddress:[%s]", addr);
2533 conn_info = _bt_find_remote_gatt_client_info(addr);
2535 BT_INFO("GATT Client [%s] is connected, conn Id [%d] Instance ID [%d]",
2536 conn_info->addr, conn_info->connection_id, conn_info->instance_id);
2538 BT_ERR("GATT Client [%s] is not yet connected..", addr);
2539 return BLUETOOTH_ERROR_NOT_CONNECTED;
2542 ret = gatts_get_att_mtu(conn_info->connection_id, &stack_mtu);
2543 if (ret != OAL_STATUS_SUCCESS) {
2544 BT_ERR("ret: %d", ret);
2545 return BLUETOOTH_ERROR_INTERNAL;
2548 BT_INFO("ATT MTU received from OAL [%d]", stack_mtu);
2549 *mtu = (unsigned int)stack_mtu;
2552 BT_ERR("MTU value is zero, GATT Client [%s] is not yet connected..", addr);
2553 return BLUETOOTH_ERROR_NOT_CONNECTED;
2556 return BLUETOOTH_ERROR_NONE;
2559 #ifdef TIZEN_GATT_CLIENT
2560 /* GATT Client utility static functions */
2561 static bt_gatt_service_info_list_t * __bt_get_service_info_list(int conn_id)
2564 bt_gatt_service_info_list_t *info = NULL;
2566 for (l = list_gatt_info; l != NULL; l = g_slist_next(l)) {
2567 info = (bt_gatt_service_info_list_t*)l->data;
2571 if (info->conn_id == conn_id)
2578 static bt_gatt_service_info_t* __bt_find_matching_service(
2579 bt_gatt_service_info_list_t *svc_list, oal_gatt_srvc_id_t *svc)
2582 bt_gatt_service_info_t *info = NULL;
2584 for (l = svc_list->services; l != NULL; l = g_slist_next(l)) {
2585 info = (bt_gatt_service_info_t*)l->data;
2589 /* Match UUID and instance ID */
2590 if (!memcmp(&svc->id.uuid.uuid, &info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
2591 && (svc->id.inst_id == info->inst_id)) {
2598 static bt_gatt_char_info_t* __bt_find_matching_charc(
2599 bt_gatt_service_info_t *svc_info, oal_gatt_id_t *charc)
2602 bt_gatt_char_info_t *info = NULL;
2604 for (l = svc_info->chars; l != NULL; l = g_slist_next(l)) {
2605 info = (bt_gatt_char_info_t*)l->data;
2609 /* Match UUID and instance ID */
2610 if (!memcmp(&charc->uuid.uuid, &info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
2611 && (charc->inst_id == info->inst_id)) {
2618 static bt_gatt_descriptor_info_t* __bt_find_matching_desc(
2619 bt_gatt_char_info_t *char_info, oal_gatt_id_t *desc)
2622 bt_gatt_descriptor_info_t *info = NULL;
2624 for (l = char_info->descs; l != NULL; l = g_slist_next(l)) {
2625 info = (bt_gatt_descriptor_info_t*)l->data;
2629 /* Match UUID and instance ID */
2630 if (!memcmp(&desc->uuid, &info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
2631 && (desc->inst_id == info->inst_id)) {
2639 static struct gatt_server_info_t *__bt_find_remote_gatt_server_info_from_conn_id(int conn_id)
2642 struct gatt_server_info_t *info = NULL;
2644 for (l = gatt_server_info_list; l != NULL; l = g_slist_next(l)) {
2645 info = (struct gatt_server_info_t*)l->data;
2649 if (info->connection_id == conn_id) {
2650 BT_INFO("Remote GATT server found addr[%s]", info->addr);
2657 static bt_gatt_service_info_t* __bt_find_removed_service(bt_gatt_service_info_list_t *svc_list)
2660 bt_gatt_service_info_t *info = NULL;
2662 for (l = svc_list->services; l != NULL; l = g_slist_next(l)) {
2663 info = (bt_gatt_service_info_t*)l->data;
2667 /* Service is marked a removed */
2668 if (info->is_removed == 1)
2674 static void __bt_remove_service_info_from_list(bt_gatt_service_info_t *svc_info)
2680 bt_gatt_char_info_t *charc = NULL;
2681 bt_gatt_included_service_info_t *incl = NULL;
2682 bt_gatt_descriptor_info_t *desc = NULL;
2684 /* Remove all Characteristic and Descriptors within characteristic */
2685 for (l = svc_info->chars; l != NULL;) {
2686 charc = (bt_gatt_char_info_t*)l->data;
2687 l = g_slist_next(l); /* Incase if l is removed, saving next to l */
2692 /* Inside Characteristic */
2693 for (l1 = charc->descs; l1 != NULL;) {
2695 desc = (bt_gatt_descriptor_info_t*)l1->data;
2696 l1 = g_slist_next(l1);
2701 /* Remove Descriptor */
2702 charc->descs = g_slist_remove(charc->descs, desc);
2705 /* Remove Characteristic */
2706 svc_info->chars = g_slist_remove(svc_info->chars, charc);
2710 /* Remove all Included Services */
2711 for (l2 = svc_info->included_svcs; l2 != NULL;) {
2712 incl = (bt_gatt_included_service_info_t*)l2->data;
2713 l2 = g_slist_next(l2); /* Incase if l is removed, saving next to l */
2718 /* Remove included service */
2719 svc_info->included_svcs = g_slist_remove(svc_info->included_svcs, incl);
2726 static void __bt_build_service_browse_info(int conn_id,
2727 bt_services_browse_info_t* info)
2730 bt_gatt_service_info_list_t *svc_info_list;
2731 bt_gatt_service_info_t *svc_info;
2733 service_uuid_t uuid;
2734 struct gatt_server_info_t *conn_info = NULL;
2736 char uuid_string[BLUETOOTH_UUID_STRING_MAX];
2738 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
2743 _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
2745 svc_info_list = __bt_get_service_info_list(conn_id);
2750 info->count = g_slist_length(svc_info_list->services);
2751 BT_DBG("Total services present in the svc info list for this conn id [%d] is [%d]",
2752 conn_id, info->count);
2754 for (l = svc_info_list->services; l != NULL; l = g_slist_next(l)) {
2755 svc_info = (bt_gatt_service_info_t*)l->data;
2756 if (svc_info == NULL)
2759 memcpy(&uuid.uuid, &svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2760 _bt_uuid_to_string(&uuid, uuid_string);
2762 BT_INFO("[%d] %s [%s]", count, uuid_string, _bt_convert_uuid_to_string(uuid_string));
2764 /* Fill UUID of service */
2765 g_strlcpy(info->uuids[count], uuid_string,
2766 BLUETOOTH_UUID_STRING_MAX);
2768 /* Fill instance ID of service */
2769 info->inst_id[count] = svc_info->inst_id;
2771 /* Fill primary service or not info */
2772 info->primary[count] = svc_info->is_primary;
2774 /* Increment count of services browsed */
2779 static void __bt_build_char_browse_info(int conn_id,
2780 bt_gatt_service_info_t *svc_info,
2781 bt_char_browse_info_t* info)
2784 bt_gatt_char_info_t *char_info;
2785 service_uuid_t uuid;
2787 struct gatt_server_info_t *conn_info = NULL;
2789 char uuid_string[BLUETOOTH_UUID_STRING_MAX];
2793 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
2797 /* Fill default data, this will be required even in case of failure */
2798 _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
2799 memcpy(&info->svc_uuid, svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2800 info->svc_inst_id = svc_info->inst_id;
2802 if (!svc_info->chars) {
2803 BT_ERR("No Chars browsed for address [%s]", conn_info->addr);
2807 info->count = g_slist_length(svc_info->chars);
2808 BT_INFO("Total count of Characteristics [%d]", info->count);
2810 for (l = svc_info->chars; l != NULL; l = g_slist_next(l)) {
2811 char_info = (bt_gatt_char_info_t*)l->data;
2812 if (char_info == NULL)
2815 memcpy(&uuid.uuid, &char_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2816 _bt_uuid_to_string(&uuid, uuid_string);
2818 /* Fill UUID of characteristic */
2819 g_strlcpy(info->uuids[count], uuid_string,
2820 BLUETOOTH_UUID_STRING_MAX);
2822 /* Fill instance ID of characteristic */
2823 info->inst_id[count] = char_info->inst_id;
2825 /* Fill property of characteristic */
2826 info->props[count] = char_info->props;
2828 /* Increment count of services browsed */
2831 BT_INFO("Total characteristics browsed [%d]", count);
2834 static void __bt_build_descriptor_browse_info(int conn_id,
2835 bt_gatt_service_info_t *svc_info,
2836 bt_gatt_char_info_t *char_info,
2837 bt_descriptor_browse_info_t* info)
2840 bt_gatt_descriptor_info_t *desc_info;
2842 service_uuid_t uuid;
2843 struct gatt_server_info_t *conn_info = NULL;
2845 char uuid_string[BLUETOOTH_UUID_STRING_MAX];
2847 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
2849 /* Fill default data, this will be required even in case of failure */
2850 _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
2851 memcpy(&info->svc_uuid, svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2852 info->svc_inst_id = svc_info->inst_id;
2853 memcpy(&info->char_uuid, char_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2854 info->char_inst_id = char_info->inst_id;
2856 /* Fill property of the parent characteristic of this descriptor */
2857 info->char_props_map = char_info->props;
2859 info->count = g_slist_length(char_info->descs);
2860 BT_INFO("Total count of Descriptors [%d]", info->count);
2862 if (!char_info->descs) {
2863 BT_ERR("No Descriptors browsed for address [%s]", conn_info->addr);
2867 for (l = char_info->descs; l != NULL; l = g_slist_next(l)) {
2868 desc_info = (bt_gatt_descriptor_info_t*)l->data;
2869 if (desc_info == NULL)
2872 memcpy(&uuid.uuid, &desc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2873 _bt_uuid_to_string(&uuid, uuid_string);
2875 /* Fill UUID of Descriptor */
2876 g_strlcpy(info->uuids[count], uuid_string,
2877 BLUETOOTH_UUID_STRING_MAX);
2879 /* Fill instance ID of Descriptor */
2880 info->inst_id[count] = desc_info->inst_id;
2883 /* Increment count of Descriptor browsed */
2887 BT_INFO("Total descriptors browsed [%d]", count);
2891 static void __bt_cleanup_remote_services(struct gatt_server_info_t *conn_info)
2898 bt_gatt_service_info_list_t * svc_info_list = NULL;
2899 bt_gatt_service_info_t *svc = NULL;
2900 bt_gatt_char_info_t *chr = NULL;
2901 bt_gatt_descriptor_info_t *desc = NULL;
2902 bt_gatt_included_service_info_t *incl_svc = NULL;
2904 svc_info_list = __bt_get_service_info_list(conn_info->connection_id);
2905 if (!svc_info_list) {
2906 BT_INFO("Could not find Svc Info list for the connection ID [%d]",
2907 conn_info->connection_id);
2911 BT_INFO("Start Cleanup of all services. Num Services [%d]", g_slist_length(svc_info_list->services));
2912 for (l = svc_info_list->services; l;) {
2913 svc = (bt_gatt_service_info_t*)l->data;
2914 l = g_slist_next(l);
2918 BT_DBG("Service info Is Prim[%d] Inst ID [%d]",
2919 svc->is_primary, svc->inst_id);
2920 /* Delete all chars and its descriptors */
2921 for (ll = svc->chars; ll;) {
2922 chr = (bt_gatt_char_info_t*)ll->data;
2923 ll = g_slist_next(ll);
2927 for (lll = chr->descs; lll;) {
2928 desc = (bt_gatt_descriptor_info_t *)lll->data;
2929 lll = g_slist_next(lll);
2932 chr->descs = g_slist_remove(chr->descs, desc);
2935 svc->chars = g_slist_remove(svc->chars, chr);
2939 /* Delete all included services */
2940 for (llll = svc->included_svcs; llll;) {
2941 incl_svc = (bt_gatt_included_service_info_t*)llll->data;
2942 llll = g_slist_next(llll);
2943 if (incl_svc == NULL)
2946 svc->included_svcs = g_slist_remove(svc->included_svcs, incl_svc);
2949 svc_info_list->services = g_slist_remove(svc_info_list->services, svc);
2953 list_gatt_info = g_slist_remove(list_gatt_info, svc_info_list);
2954 g_free(svc_info_list);
2957 int _bt_register_gatt_client_instance(const char *sender,
2958 bluetooth_device_address_t *address)
2960 int ret = OAL_STATUS_SUCCESS;
2961 char *uuid_string = NULL;
2966 /* App should ensure that it should not send */
2967 BT_INFO("###Check on which instance GATT Client instance can be initialized....");
2968 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
2969 if (numapps[k].is_initialized == 1) {
2970 BT_INFO("Instance ID [%d] is already in use..Check next slot",
2971 numapps[k].instance_id);
2974 BT_INFO("Time to register GATT client instancer..UUID to be used is [%s] slot [%d]",
2975 uuid_list[slot-1], slot);
2981 BT_ERR("No Slot if free for GATT Client registration..");
2982 return BLUETOOTH_ERROR_REGISTRATION_FAILED;
2985 uuid_string = g_malloc0(BT_UUID_STRING_MAX);
2986 _bt_string_to_uuid(uuid_list[slot-1], (service_uuid_t*)&uuid);
2987 g_strlcpy(uuid_string, uuid_list[slot-1], BT_UUID_STRING_MAX);
2988 BT_INFO("Copied UUID string [%s]", uuid_string);
2990 /* Register GATT Client */
2991 ret = gattc_register(&uuid);
2992 if (ret != OAL_STATUS_SUCCESS) {
2993 BT_ERR("ret: %d", ret);
2994 g_free(uuid_string);
2995 return BLUETOOTH_ERROR_INTERNAL;
2998 BT_INFO("GATT Client registration call successfully accepted by OAL..wait for Instance Initialized event from OAL..");
3000 /* Return & wait for GATT Client Instance Initialization event */
3001 memset(numapps[slot].sender, 0x00, sizeof(numapps[slot].sender));
3002 memset(numapps[slot].uuid, 0x00, sizeof(numapps[slot].uuid));
3004 g_strlcpy(numapps[slot].sender, sender, sizeof(numapps[slot].sender));
3005 g_strlcpy(numapps[slot].uuid, uuid_string, sizeof(numapps[slot].uuid));
3007 /* Address is saved here. When event comes, sender + address are matched for replying pending
3008 request. It is impossible for same sender to have requests with two same addresses */
3009 memcpy(&numapps[slot].address.addr, address->addr, sizeof(bluetooth_device_address_t));
3011 BT_INFO("Slot [%d] occupied", slot);
3012 numapps[slot].is_initialized = TRUE; /* Set initialization to true here itself */
3014 g_free(uuid_string);
3015 return BLUETOOTH_ERROR_NONE;
3021 /* GATT client events */
3022 static void __bt_handle_client_instance_registered(event_gattc_register_t *data)
3024 bt_service_app_info_t *info = NULL;
3026 char *uuid_string = g_malloc0(BT_UUID_STRING_MAX);
3028 _bt_uuid_to_string(&(data->client_uuid), uuid_string);
3029 BT_INFO("CLient ID is Initialized [%d] UUID initialized [%s]", data->client_if, uuid_string);
3031 /* Platform GATT client framwork does not use Default GATT client instance
3032 This GATT client instance is never deregistred in the lifetime of bt-service */
3033 BT_INFO("Default UUID [%s] current registered uuid [%s]",
3034 DEFAULT_GATT_CLIENT_UUID, uuid_string);
3035 if (g_strcmp0(uuid_string, DEFAULT_GATT_CLIENT_UUID) == 0) {
3036 BT_INFO("Default client Instance Registered [%s] Client instance [%d]",
3037 uuid_string, data->client_if);
3038 gatt_default_client = data->client_if;
3039 g_free(uuid_string);
3043 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
3046 if (g_strcmp0(info->uuid, uuid_string) == 0) {
3047 BT_INFO("Found GATT client.. UUID [%s], sender [%s]", info->uuid, info->sender);
3048 BT_INFO("Slot [%d] occupied", k);
3049 info->is_initialized = TRUE;
3050 info->client_id = data->client_if;
3051 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE, BT_GATT_CLIENT_REGISTER,
3052 (void*)info, sizeof(bt_service_app_info_t));
3056 g_free(uuid_string);
3059 static void __bt_handle_client_connected(event_gattc_conn_t *event_data)
3061 int result = BLUETOOTH_ERROR_NONE;
3062 struct gatt_server_info_t *conn_info = NULL;
3063 struct gatt_out_conn_info_t *out_conn_info = NULL;
3065 GVariant *param = NULL;
3067 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
3068 _bt_convert_addr_type_to_string(address,
3069 (unsigned char *)event_data->address.addr);
3071 if (event_data->status != OAL_STATUS_SUCCESS)
3072 result = BLUETOOTH_ERROR_INTERNAL;
3074 /* DBUS Return fo BT_CONNECT_LE for all the apps */
3075 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE, address,
3076 BT_ADDRESS_STRING_SIZE);
3078 BT_INFO("Local GATT Client Connected: Remote addr[%s] Client Interface [%d] Connection ID [%d] status[%d]",
3079 address, event_data->client_if, event_data->conn_id, event_data->status);
3081 if (result == BLUETOOTH_ERROR_NONE) {
3082 /* Check if device is already in connected list */
3083 conn_info = _bt_find_remote_gatt_server_info(address);
3086 /* Send event to BT-API */
3087 param = g_variant_new("(is)", result, address);
3088 _bt_send_event(BT_DEVICE_EVENT,
3089 BLUETOOTH_EVENT_GATT_CLIENT_CONNECTED, /* Local device is GATT client */
3092 /* Save Connection info */
3093 conn_info = g_new0(struct gatt_server_info_t, 1);
3094 conn_info->addr = g_strdup(address);
3095 conn_info->client_id = event_data->client_if;
3096 BT_INFO("Added GATT server addr[%s]", conn_info->addr);
3097 conn_info->connection_id = event_data->conn_id;
3098 gatt_server_info_list = g_slist_append(gatt_server_info_list, conn_info);
3099 BT_INFO("Total num of connected Remote GATT server devices [%d]",
3100 g_slist_length(gatt_server_info_list));
3103 BT_INFO("Do a Internal refresh");
3104 if (OAL_STATUS_SUCCESS != gattc_refresh(conn_info->client_id, &event_data->address))
3105 BT_ERR("GATT database refresh failed!!");
3107 BT_INFO("GATT database refresh Success!!");
3110 BT_ERR("Local GATT Client connected event for addr[%s], but device is in connected list already", address);
3112 __bt_add_mtu_gatt_device(address);
3114 BT_ERR("GATT Client Connection failed!!");
3116 /* If outgoing connection Info is present, then remove it */
3117 out_conn_info = __bt_find_gatt_outgoing_conn_info(address);
3118 if (out_conn_info) {
3119 BT_ERR("Outgoing Client connect request was sent");
3120 outgoing_gatt_conn_list = g_slist_remove(outgoing_gatt_conn_list, out_conn_info);
3121 g_free(out_conn_info->addr);
3122 g_free(out_conn_info);
3127 static void __bt_handle_client_disconnected(event_gattc_conn_t *event_data)
3129 int result = BLUETOOTH_ERROR_NONE;
3131 struct gatt_server_info_t *conn_info = NULL;
3132 struct gatt_out_conn_info_t *out_conn_info = NULL;
3134 GVariant *param = NULL;
3136 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
3137 _bt_convert_addr_type_to_string(address,
3138 (unsigned char *)event_data->address.addr);
3140 if (event_data->status != OAL_STATUS_SUCCESS)
3141 result = BLUETOOTH_ERROR_INTERNAL;
3143 if (NULL == _bt_get_request_info_data(BT_DISCONNECT_LE, address)) {
3144 if (NULL != _bt_get_request_info_data(BT_CONNECT_LE, address)) {
3145 result = BLUETOOTH_ERROR_INTERNAL;
3146 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
3147 address, BT_ADDRESS_STRING_SIZE);
3148 BT_ERR("Failed to connect Local GATT Remote addr[%s]", address);
3153 /* DBUS Return for BT_DISCONNECT_LE for all the apps */
3154 __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE, address,
3155 BT_ADDRESS_STRING_SIZE);
3157 BT_INFO("Local GATT Client DisConnected: Remote addr[%s] Client Interface [%d] Connection ID [%d] status [%d]",
3158 address, event_data->client_if, event_data->conn_id, event_data->status);
3160 /* Remove Connection info */
3161 conn_info = _bt_find_remote_gatt_server_info(address);
3164 param = g_variant_new("(is)", result, address);
3165 /* Send event to application */
3166 _bt_send_event(BT_DEVICE_EVENT,
3167 BLUETOOTH_EVENT_GATT_CLIENT_DISCONNECTED,
3170 BT_INFO("Remove GATT server info from List..");
3171 /* Remove all services from info list_gatt_info */
3172 __bt_cleanup_remote_services(conn_info);
3174 /* Remove info from List */
3175 gatt_server_info_list = g_slist_remove(gatt_server_info_list, conn_info);
3176 BT_INFO("Total num of connected GATT servers [%d]", g_slist_length(gatt_server_info_list));
3177 g_free(conn_info->addr);
3180 BT_INFO("Can not find conn info, already removed!");
3182 __bt_remove_mtu_gatt_device(address);
3184 /* If outgoing connection Info is present, then remove it */
3185 out_conn_info = __bt_find_gatt_outgoing_conn_info(address);
3186 if (out_conn_info) {
3187 BT_ERR("Client Disconnected event, but outgoing connect request was sent");
3188 outgoing_gatt_conn_list = g_slist_remove(outgoing_gatt_conn_list, out_conn_info);
3189 g_free(out_conn_info->addr);
3190 g_free(out_conn_info);
3196 static void __bt_handle_client_service_search_result(
3197 event_gattc_service_result_t *event_data)
3199 BT_INFO("received the gatt service search result");
3200 /* Pre: status is never fail from OAL */
3202 /* Find service list from address */
3203 bt_gatt_service_info_list_t *svc_info_list;
3204 bt_gatt_service_info_t *svc_info;
3205 BT_INFO("Search Result: status [%d] conn_id [%d]",
3206 event_data->conn_status.status,
3207 event_data->conn_status.conn_id);
3209 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3210 if (!svc_info_list) {
3211 BT_INFO("Service info list not present for this connection ID, means first time browse");
3212 /* Means for this conn_id, no services are ever browsed, first time,
3213 create service info list for this conn_id */
3214 svc_info_list = g_malloc0(sizeof(bt_gatt_service_info_list_t));
3215 svc_info_list->conn_id = event_data->conn_status.conn_id;
3216 list_gatt_info = g_slist_append(list_gatt_info, svc_info_list);
3218 BT_INFO("Service info list Already present for this connection ID, means not first time browse for this conn ID ");
3221 /* send list and current service's uuid and instance id to find it */
3222 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3225 /* If not found, check if service changed, if yes, means this is a new service added
3226 in remote GATT device, update uuid info in svc info list structure, to be used when
3227 search is completed */
3229 BT_INFO("Service Not found in svc info list for this connection ID");
3230 if (svc_info_list->info.is_changed) {
3231 BT_INFO("Service Changed indication already found for this connection ID");
3232 memcpy(svc_info_list->info.uuid, event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3234 /* Create and add new service in service list */
3235 svc_info = g_malloc0(sizeof(bt_gatt_service_info_t));
3236 memcpy(svc_info->uuid, event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3237 svc_info->inst_id = event_data->srvc_id.id.inst_id;
3238 svc_info->is_primary = event_data->srvc_id.is_prmry;
3239 svc_info_list->services = g_slist_append(svc_info_list->services, svc_info);
3240 BT_INFO("Service created and added in Svc info listf or this connection ID");
3242 BT_INFO("Service Already found to be present inside the svc info list for this connection ID");
3243 /* If returned matching service info, then just update service_rmeoved value inside it to 0 */
3244 svc_info->is_removed = 0;
3248 static void __bt_handle_client_service_search_completed(
3249 event_gattc_conn_status_t *event_data)
3252 struct gatt_server_info_t *conn_info = NULL;
3253 bt_gatt_service_info_list_t *svc_info_list;
3254 bt_gatt_service_info_t *svc_info;
3255 bt_services_browse_info_t browse_info;
3256 unsigned char uuid_empty[BLUETOOTH_UUID_HEX_MAX_LEN];
3258 memset(&uuid_empty, 0x00, BLUETOOTH_UUID_HEX_MAX_LEN);
3259 memset(&browse_info, 0x00, sizeof(bt_services_browse_info_t));
3260 BT_INFO("Primary Services browsing completed status[%d] conn ID [%d]",
3261 event_data->status, event_data->conn_id);
3263 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(event_data->conn_id);
3265 svc_info_list = __bt_get_service_info_list(event_data->conn_id);
3266 if (!svc_info_list) {
3267 BT_ERR("No services browsed ever for addr [%s]", conn_info->addr);
3269 /* Just build response and return ERROR */
3270 __bt_build_service_browse_info(event_data->conn_id, &browse_info);
3272 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_INTERNAL,
3273 BT_GATT_GET_PRIMARY_SERVICES, &browse_info,
3274 sizeof(bt_services_browse_info_t));
3278 /* If fail, then send event with error */
3279 if (event_data->status != OAL_STATUS_SUCCESS) {
3280 /* Just build response and return ERROR */
3281 __bt_build_service_browse_info(event_data->conn_id, &browse_info);
3283 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_INTERNAL,
3284 BT_GATT_GET_PRIMARY_SERVICES, &browse_info,
3285 sizeof(bt_services_browse_info_t));
3289 /* If success, then find service info list from address */
3291 /* If svc_changed == 1 and uuid valid, means a new service is added*/
3292 if (svc_info_list->info.is_changed && !memcmp(uuid_empty, svc_info_list->info.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)) {
3293 /* TODO: Send event -Service added with instance ID and UUID of newly added service */
3294 BT_INFO("new service added");
3296 BT_INFO("TODO new service added");
3299 /* If svc_changed == 1 and uuid invalid, then a service is removed */
3300 if (svc_info_list->info.is_changed && memcmp(uuid_empty, svc_info_list->info.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)) {
3301 /* Scan through the service info list to find service with is_removed = 1*/
3302 svc_info = __bt_find_removed_service(svc_info_list);
3304 /* TODO Send event - Service removed with instance ID and UUID of just rmeoved service */
3306 /* Remove that service info from service info list */
3307 svc_info_list->services = g_slist_remove(svc_info_list->services, svc_info);
3309 /* Delete that service completely from svc_info list*/
3310 __bt_remove_service_info_from_list(svc_info);
3314 /* Reset svc_changed = 0, and reset UUID = all 0's */
3315 svc_info_list->info.is_changed = 0;
3316 memset(&svc_info_list->info.uuid, 0x00, BLUETOOTH_UUID_HEX_MAX_LEN);
3318 /* Build Reply and send to service browse primary services request of pending apps */
3319 __bt_build_service_browse_info(event_data->conn_id, &browse_info);
3321 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE,
3322 BT_GATT_GET_PRIMARY_SERVICES, &browse_info,
3323 sizeof(bt_services_browse_info_t));
3327 static void __bt_handle_client_characteristic_search_result(
3328 event_gattc_characteristic_result_t *event_data)
3330 bt_gatt_service_info_list_t *svc_info_list;
3331 bt_gatt_service_info_t *svc_info;
3332 bt_gatt_char_info_t *char_info;
3333 bt_char_browse_info_t browse_info;
3335 BT_INFO("Characteristic search result status [%d]",
3336 event_data->conn_status.status);
3338 memset(&browse_info, 0x00, sizeof(bt_char_browse_info_t));
3341 if (event_data->conn_status.status == OAL_STATUS_SUCCESS) {
3342 /* Find service info list from address */
3343 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3344 if (svc_info_list == NULL) {
3345 BT_ERR("svc_info_list is NULL");
3350 /* Find matching service info from svc info list */
3351 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3352 if (svc_info == NULL) {
3353 BT_ERR("svc_info is NULL");
3357 /* Find Matching char from service info in event */
3358 char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
3359 if (char_info == NULL)
3360 BT_ERR("char_info is NULL");
3362 /* If not found, then add new characteristic and return */
3364 BT_INFO(" add new characteristic");
3365 char_info = g_malloc0(sizeof(bt_gatt_char_info_t));
3366 memcpy(char_info->uuid, event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3368 char_info->inst_id = event_data->char_id.inst_id;
3369 char_info->props = event_data->char_prop;
3370 svc_info->chars = g_slist_append(svc_info->chars, char_info);
3372 /* If found, then return */
3373 BT_INFO("update char property as Characteristic browsed is already present");
3374 char_info->props |= event_data->char_prop;
3377 /* If Not success: Means Charc browse is completed */
3378 /* Find char list from service in event */
3379 /* Find service list from address */
3380 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3382 /* Find service info from service in event */
3383 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3385 /* Build char list from service in event */
3386 __bt_build_char_browse_info(event_data->conn_status.conn_id,
3387 svc_info, &browse_info);
3389 /* Create response and return by sending event*/
3390 /* Build Reply and send to service browse All Included services request of pending apps */
3391 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE,
3392 BT_GATT_GET_SERVICE_PROPERTIES,
3394 sizeof(bt_char_browse_info_t));
3398 static void __bt_handle_client_descriptor_search_result(
3399 event_gattc_descriptor_result_t *event_data)
3401 bt_gatt_service_info_list_t *svc_info_list;
3402 bt_gatt_service_info_t *svc_info;
3403 bt_gatt_char_info_t *char_info;
3404 bt_gatt_descriptor_info_t *desc_info;
3405 bt_descriptor_browse_info_t browse_info;
3407 BT_INFO("descriptor search result status [%d]", event_data->conn_status.status);
3409 memset(&browse_info, 0x00, sizeof(bt_descriptor_browse_info_t));
3412 if (event_data->conn_status.status == OAL_STATUS_SUCCESS) {
3413 /* Find service list from address */
3414 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3415 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3416 char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
3417 desc_info = __bt_find_matching_desc(char_info, &event_data->descr_id);
3419 /* If not found, add new descriptor and return */
3421 desc_info = g_malloc0(sizeof(bt_gatt_descriptor_info_t));
3422 memcpy(desc_info->uuid, event_data->descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3424 desc_info->inst_id = event_data->descr_id.inst_id;
3425 char_info->descs = g_slist_append(char_info->descs, desc_info);
3428 /* If found, then return */
3429 BT_INFO("Characteristic browsed is already presesnt");
3432 /* If Not success */
3433 /* Find service list from address */
3434 /* Find included service list from service in event */
3435 /* Create response and return by sending event*/
3436 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3438 /* Find service info from service in event */
3439 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3441 /* Find char info from char in event */
3442 char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
3444 /* Build descriptor list from char in event */
3445 __bt_build_descriptor_browse_info(event_data->conn_status.conn_id,
3446 svc_info, char_info, &browse_info);
3449 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE,
3450 BT_GATT_GET_CHARACTERISTIC_PROPERTIES,
3452 sizeof(bt_descriptor_browse_info_t));
3456 static void __bt_handle_client_characteristic_read_data(
3457 event_gattc_read_data *event_data)
3459 int result = BLUETOOTH_ERROR_NONE;
3460 struct gatt_server_info_t *conn_info = NULL;
3461 bluetooth_gatt_client_char_prop_info_t read_info;
3463 /* Read Information data structures */
3464 GVariant *param = NULL;
3465 GVariant *data = NULL;
3466 GVariant *data_svc_uuid = NULL;
3467 GVariant *data_char_uuid = NULL;
3468 char *read_val = NULL;
3469 char *svc_uuid = NULL;
3470 char *char_uuid = NULL;
3473 //memset(&read_info, 0x00, sizeof(bt_gatt_handle_property_t));
3474 memset(&read_info, 0x00, sizeof(bluetooth_gatt_client_char_prop_info_t));
3476 /* Extract Address from conn_id of event data */
3477 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3478 event_data->uuid_status.conn_status.conn_id);
3480 BT_INFO("Characteristic Read result from addr [%s] status [%d]",
3481 conn_info->addr, event_data->uuid_status.conn_status.status);
3483 /* Fill char in buffer */
3484 memcpy(&read_info.characteristic.uuid,
3485 event_data->uuid_status.char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3486 read_info.characteristic.instance_id = event_data->uuid_status.char_id.inst_id;
3488 /* Fill Service in buffer */
3489 memcpy(&read_info.svc.uuid,
3490 event_data->uuid_status.srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3491 read_info.svc.instance_id = event_data->uuid_status.srvc_id.id.inst_id;
3493 /* Fill remote device address */
3494 _bt_convert_addr_string_to_type(read_info.device_address.addr, conn_info->addr);
3496 /* Fill data and reply to all apps waiting for Read result on the same characteristic
3497 Note: Even in case of failure, address, handles and result code should be returned */
3498 if (event_data->uuid_status.conn_status.status != OAL_STATUS_SUCCESS)
3499 result = BLUETOOTH_ERROR_INTERNAL;
3501 if (event_data->data_len > 0) {
3503 // for (i = 0; i < event_data->data_len; i++)
3504 // BT_INFO("Data[%d] = [0x%x]", i, event_data->data[i]);
3507 read_val = g_memdup(&event_data->data[0], event_data->data_len);
3509 data = g_variant_new_from_data(
3510 G_VARIANT_TYPE_BYTESTRING,
3512 event_data->data_len,
3515 BT_ERR("Characteristic Read success, but no data!!!");
3517 data = g_variant_new_from_data(
3518 G_VARIANT_TYPE_BYTESTRING,
3525 svc_uuid = g_memdup(&event_data->uuid_status.srvc_id.id.uuid.uuid[0], uuid_len);
3527 data_svc_uuid = g_variant_new_from_data(
3528 G_VARIANT_TYPE_BYTESTRING,
3534 char_uuid = g_memdup(&event_data->uuid_status.char_id.uuid.uuid[0], uuid_len);
3536 data_char_uuid = g_variant_new_from_data(
3537 G_VARIANT_TYPE_BYTESTRING,
3542 param = g_variant_new("(isn@ayin@ayin@ay)", result,
3546 event_data->uuid_status.srvc_id.id.inst_id,
3549 event_data->uuid_status.char_id.inst_id,
3550 event_data->data_len,
3554 _bt_send_event(BT_GATT_CLIENT_EVENT,
3555 BLUETOOTH_EVENT_GATT_READ_CHAR,
3558 /* Send DBUS return */
3559 __bt_gatt_handle_pending_request_info(result,
3560 BT_GATT_READ_CHARACTERISTIC,
3562 sizeof(bluetooth_gatt_client_char_prop_info_t));
3573 static void __bt_handle_client_descriptor_read_data(
3574 event_gattc_read_data *event_data)
3576 int result = BLUETOOTH_ERROR_NONE;
3577 struct gatt_server_info_t *conn_info = NULL;
3578 bluetooth_gatt_client_desc_prop_info_t read_info;
3580 /* Read Information data structures */
3581 GVariant *param = NULL;
3582 GVariant *data = NULL;
3583 GVariant *data_svc_uuid = NULL;
3584 GVariant *data_char_uuid = NULL;
3585 GVariant *data_desc_uuid = NULL;
3586 char *read_val = NULL;
3587 char *svc_uuid = NULL;
3588 char *char_uuid = NULL;
3589 char *desc_uuid = NULL;
3594 memset(&read_info, 0x00, sizeof(bluetooth_gatt_client_desc_prop_info_t));
3596 /* Extract Address from conn_id of event data */
3597 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3598 event_data->uuid_status.conn_status.conn_id);
3600 BT_INFO("Descriptor Read result from addr [%s] status [%d]",
3601 conn_info->addr, event_data->uuid_status.conn_status.status);
3603 /* Fill descriptor informations in buffer */
3604 memcpy(&read_info.descriptor.uuid,
3605 event_data->uuid_status.descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3606 read_info.descriptor.instance_id = event_data->uuid_status.descr_id.inst_id;
3608 /* Fill Characteristic informations in buffer */
3609 memcpy(&read_info.characteristic.uuid,
3610 event_data->uuid_status.char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3611 read_info.characteristic.instance_id = event_data->uuid_status.char_id.inst_id;
3613 /* Fill Service informations in buffer */
3614 memcpy(&read_info.svc.uuid,
3615 event_data->uuid_status.srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3616 read_info.svc.instance_id = event_data->uuid_status.srvc_id.id.inst_id;
3618 /* Fill remote device address */
3619 _bt_convert_addr_string_to_type(read_info.device_address.addr, conn_info->addr);
3621 /* Fill data and reply to all apps waiting for Read result on the same characteristic */
3622 if (event_data->uuid_status.conn_status.status != OAL_STATUS_SUCCESS)
3623 result = BLUETOOTH_ERROR_INTERNAL;
3625 BT_INFO("desc data len:%d", event_data->data_len);
3626 if (event_data->data_len > 0) {
3628 for (i = 0; i < event_data->data_len; i++)
3629 BT_DBG("Data[%d] = [0x%x]", i, event_data->data[i]);
3632 read_val = g_memdup(&event_data->data[0], event_data->data_len);
3634 data = g_variant_new_from_data(
3635 G_VARIANT_TYPE_BYTESTRING,
3637 event_data->data_len,
3640 BT_INFO("Descriptor Read success, but no data!!!");
3642 data = g_variant_new_from_data(
3643 G_VARIANT_TYPE_BYTESTRING,
3649 svc_uuid = g_memdup(&event_data->uuid_status.srvc_id.id.uuid.uuid[0], uuid_len);
3651 data_svc_uuid = g_variant_new_from_data(
3652 G_VARIANT_TYPE_BYTESTRING,
3658 char_uuid = g_memdup(&event_data->uuid_status.char_id.uuid.uuid[0], uuid_len);
3660 data_char_uuid = g_variant_new_from_data(
3661 G_VARIANT_TYPE_BYTESTRING,
3667 desc_uuid = g_memdup(&event_data->uuid_status.descr_id.uuid.uuid[0], uuid_len);
3669 data_desc_uuid = g_variant_new_from_data(
3670 G_VARIANT_TYPE_BYTESTRING,
3675 param = g_variant_new("(isn@ayin@ayin@ayin@ay)", result,
3679 event_data->uuid_status.srvc_id.id.inst_id,
3682 event_data->uuid_status.char_id.inst_id,
3685 event_data->uuid_status.descr_id.inst_id,
3686 event_data->data_len,
3690 _bt_send_event(BT_GATT_CLIENT_EVENT,
3691 BLUETOOTH_EVENT_GATT_READ_DESC,
3694 BT_INFO("Send DBUS rpely for GATT Read Descriptor");
3695 /* Send DBUS return */
3696 __bt_gatt_handle_pending_request_info(result,
3697 BT_GATT_READ_DESCRIPTOR_VALUE,
3699 sizeof(bluetooth_gatt_client_desc_prop_info_t));
3710 static void __bt_handle_client_characteristic_write_data(
3711 event_gattc_write_data *event_data)
3713 int result = BLUETOOTH_ERROR_NONE;
3714 struct gatt_server_info_t *conn_info = NULL;
3715 bluetooth_gatt_client_char_prop_info_t write_info;
3717 /* Read Information data structures */
3718 GVariant *param = NULL;
3719 GVariant *data_svc_uuid = NULL;
3720 GVariant *data_char_uuid = NULL;
3721 char *svc_uuid = NULL;
3722 char *char_uuid = NULL;
3726 memset(&write_info, 0x00, sizeof(bluetooth_gatt_client_char_prop_info_t));
3728 /* Extract Address from conn_id of event data */
3729 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3730 event_data->conn_status.conn_id);
3732 BT_INFO("Characteristic Write callback from addr [%s] status [%d]",
3733 conn_info->addr, event_data->conn_status.status);
3735 /* Fill char in buffer */
3736 memcpy(&write_info.characteristic.uuid,
3737 event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3738 write_info.characteristic.instance_id = event_data->char_id.inst_id;
3740 /* Fill Service in buffer */
3741 memcpy(&write_info.svc.uuid,
3742 event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3743 write_info.svc.instance_id = event_data->srvc_id.id.inst_id;
3745 /* Fill remote device address */
3746 _bt_convert_addr_string_to_type(write_info.device_address.addr, conn_info->addr);
3748 if (event_data->conn_status.status != OAL_STATUS_SUCCESS) {
3749 result = BLUETOOTH_ERROR_INTERNAL;
3755 svc_uuid = g_memdup(&event_data->srvc_id.id.uuid.uuid[0], uuid_len);
3757 data_svc_uuid = g_variant_new_from_data(
3758 G_VARIANT_TYPE_BYTESTRING,
3764 char_uuid = g_memdup(&event_data->char_id.uuid.uuid[0], uuid_len);
3766 data_char_uuid = g_variant_new_from_data(
3767 G_VARIANT_TYPE_BYTESTRING,
3772 param = g_variant_new("(isn@ayin@ayi)", result,
3776 event_data->srvc_id.id.inst_id,
3779 event_data->char_id.inst_id);
3782 _bt_send_event(BT_GATT_CLIENT_EVENT,
3783 BLUETOOTH_EVENT_GATT_WRITE_CHAR,
3792 /* Send DBUS return */
3793 __bt_gatt_handle_pending_request_info(result,
3794 BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE,
3796 sizeof(bluetooth_gatt_client_char_prop_info_t));
3800 static void __bt_handle_client_descriptor_write_data(
3801 event_gattc_write_data *event_data)
3803 int result = BLUETOOTH_ERROR_NONE;
3804 struct gatt_server_info_t *conn_info = NULL;
3805 bluetooth_gatt_client_desc_prop_info_t write_info;
3807 /* Write Information data structures */
3808 GVariant *param = NULL;
3809 GVariant *data_svc_uuid = NULL;
3810 GVariant *data_char_uuid = NULL;
3811 GVariant *data_desc_uuid = NULL;
3812 char *svc_uuid = NULL;
3813 char *char_uuid = NULL;
3814 char *desc_uuid = NULL;
3818 memset(&write_info, 0x00, sizeof(bluetooth_gatt_client_desc_prop_info_t));
3820 /* Extract Address from conn_id of event data */
3821 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3822 event_data->conn_status.conn_id);
3824 if (NULL == conn_info) {
3826 BT_INFO("Failed to get the conn info for conn_id [%d]", event_data->conn_status.conn_id);
3830 BT_INFO("Descriptor Write callback from addr [%s] status [%d]",
3831 conn_info->addr, event_data->conn_status.status);
3833 /* Fill descriptor informations in buffer */
3834 memcpy(&write_info.descriptor.uuid,
3835 event_data->descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3836 write_info.descriptor.instance_id = event_data->descr_id.inst_id;
3838 /* Fill Characteristic informations in buffer */
3839 memcpy(&write_info.characteristic.uuid,
3840 event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3841 write_info.characteristic.instance_id = event_data->char_id.inst_id;
3843 /* Fill Service informations in buffer */
3844 memcpy(&write_info.svc.uuid,
3845 event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3846 write_info.svc.instance_id = event_data->srvc_id.id.inst_id;
3848 /* Fill remote device address */
3849 _bt_convert_addr_string_to_type(write_info.device_address.addr, conn_info->addr);
3851 if (event_data->conn_status.status != OAL_STATUS_SUCCESS) {
3852 result = BLUETOOTH_ERROR_INTERNAL;
3858 svc_uuid = g_memdup(&event_data->srvc_id.id.uuid.uuid[0], uuid_len);
3860 data_svc_uuid = g_variant_new_from_data(
3861 G_VARIANT_TYPE_BYTESTRING,
3867 char_uuid = g_memdup(&event_data->char_id.uuid.uuid[0], uuid_len);
3869 data_char_uuid = g_variant_new_from_data(
3870 G_VARIANT_TYPE_BYTESTRING,
3876 desc_uuid = g_memdup(&event_data->descr_id.uuid.uuid[0], uuid_len);
3878 data_desc_uuid = g_variant_new_from_data(
3879 G_VARIANT_TYPE_BYTESTRING,
3884 param = g_variant_new("(isn@ayin@ayin@ayi)", result,
3888 event_data->srvc_id.id.inst_id,
3891 event_data->char_id.inst_id,
3894 event_data->descr_id.inst_id);
3897 _bt_send_event(BT_GATT_CLIENT_EVENT,
3898 BLUETOOTH_EVENT_GATT_WRITE_DESC,
3909 /* Send DBUS return */
3910 __bt_gatt_handle_pending_request_info(result,
3911 BT_GATT_WRITE_DESCRIPTOR_VALUE,
3913 sizeof(bluetooth_gatt_client_desc_prop_info_t));
3916 static void __bt_hanlde_le_device_disconnection(event_dev_conn_status_t *event_data)
3918 int result = BLUETOOTH_ERROR_INTERNAL;
3919 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
3921 _bt_convert_addr_type_to_string(address, (unsigned char *)event_data->address.addr);
3923 /* DBUS Return with fail of pending BT_CONNECT_LE for all the apps */
3924 BT_INFO("Local GATT Client disconnected: Remote addr[%s] ", address);
3926 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE, address,
3927 BT_ADDRESS_STRING_SIZE);
3931 static void __bt_handle_client_notification_registered(
3932 event_gattc_regdereg_notify_t *event_data,
3933 gboolean is_registered)
3935 int result = BLUETOOTH_ERROR_NONE;
3936 struct gatt_server_info_t *conn_info = NULL;
3937 bt_gatt_notif_reg_info_t notif_info;
3940 memset(¬if_info, 0x00, sizeof(bt_gatt_notif_reg_info_t));
3942 BT_INFO("Client Interface [%d] status [%d]",
3943 event_data->client_if,
3944 event_data->status);
3946 /* Extract Address from conn_id of event data */
3947 conn_info = __bt_find_remote_gatt_server_info_from_client_if(
3948 event_data->client_if);
3951 BT_INFO("Connection Info is not present, return");
3954 BT_INFO("Notification Registered for addr [%s]",
3958 /* Fill svc informations in buffer */
3959 memcpy(¬if_info.svc_uuid,
3960 event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3961 notif_info.svc_inst = event_data->srvc_id.id.inst_id;
3963 /* Fill char in buffer */
3964 memcpy(¬if_info.char_uuid,
3965 event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3966 notif_info.char_inst = event_data->char_id.inst_id;
3968 /* Fill remote device address */
3969 _bt_convert_addr_string_to_type(notif_info.addr.addr, conn_info->addr);
3971 notif_info.is_registered = is_registered;
3973 if (event_data->status != OAL_STATUS_SUCCESS)
3974 result = BLUETOOTH_ERROR_INTERNAL;
3976 /* Send DBUS Return for BT_GATT_WATCH_CHARACTERISTIC */
3977 __bt_gatt_handle_pending_request_info(result,
3978 BT_GATT_WATCH_CHARACTERISTIC,
3980 sizeof(bt_gatt_notif_reg_info_t));
3983 static void __bt_handle_client_notification_data(event_gattc_notify_data *event_data)
3985 /* No status in this event from OAL */
3986 int result = BLUETOOTH_ERROR_NONE;
3988 /* Read Information data structures */
3989 GVariant *param = NULL;
3990 GVariant *data = NULL;
3991 GVariant *data_svc_uuid = NULL;
3992 GVariant *data_char_uuid = NULL;
3993 char *read_val = NULL;
3994 char *svc_uuid = NULL;
3995 char *char_uuid = NULL;
4001 BT_INFO("Notifcation of charc data changed");
4003 if (event_data->data_len > 0) {
4005 for (i = 0; i < event_data->data_len; i++)
4006 BT_INFO("Data[%d] = [0x%x]", i, event_data->data[i]);
4009 addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
4010 _bt_convert_addr_type_to_string(addr,
4011 (unsigned char *)&(event_data->address.addr));
4014 read_val = g_memdup(&event_data->data[0], event_data->data_len);
4016 data = g_variant_new_from_data(
4017 G_VARIANT_TYPE_BYTESTRING,
4019 event_data->data_len,
4022 svc_uuid = g_memdup(&event_data->srvc_id.id.uuid.uuid[0], uuid_len);
4024 data_svc_uuid = g_variant_new_from_data(
4025 G_VARIANT_TYPE_BYTESTRING,
4031 char_uuid = g_memdup(&event_data->char_id.uuid.uuid[0], uuid_len);
4033 data_char_uuid = g_variant_new_from_data(
4034 G_VARIANT_TYPE_BYTESTRING,
4040 param = g_variant_new("(isn@ayin@ayin@ay)", result,
4044 event_data->srvc_id.id.inst_id,
4047 event_data->char_id.inst_id,
4048 event_data->data_len,
4052 _bt_send_event(BT_GATT_CLIENT_EVENT,
4053 BLUETOOTH_EVENT_GATT_CHAR_VAL_CHANGED,
4056 BT_ERR("No Data!!");
4069 static void __bt_handle_client_service_changed_ind(event_gattc_service_changed_data *event_data)
4071 GVariant *param = NULL;
4072 char *address_str = NULL;
4073 char *uuid_str = NULL;
4075 address_str = g_malloc0(BT_ADDRESS_STRING_SIZE);
4076 uuid_str = g_malloc0(BT_UUID_STRING_MAX);
4077 _bt_convert_addr_type_to_string(address_str, event_data->address.addr);
4078 _bt_uuid_to_string(&event_data->uuid, uuid_str);
4080 param = g_variant_new("(iss)", event_data->change_type, address_str, uuid_str);
4082 _bt_send_event(BT_GATT_CLIENT_EVENT,
4083 BLUETOOTH_EVENT_GATT_CLIENT_SERVICE_CHANGED,
4087 gboolean _bt_is_remote_gatt_device_connected(bluetooth_device_address_t *address)
4090 struct gatt_server_info_t *conn_info = NULL;
4091 gboolean connected = FALSE;
4094 addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
4095 _bt_convert_addr_type_to_string(addr,
4096 (unsigned char *)&(address->addr));
4098 BT_INFO("Check Connected or not for [%s]", addr);
4099 /* Check if device is already in connected list */
4100 conn_info = _bt_find_remote_gatt_server_info(addr);
4103 BT_INFO("Remote GATT Server device [%s] is Connected", conn_info->addr);
4106 struct gatt_client_info_t *client_info = NULL;
4108 BT_INFO("Remote GATT Server Device [%s] is not Connected", addr);
4110 /* Check if device is already in connected list */
4111 client_info = _bt_find_remote_gatt_client_info(addr);
4114 BT_INFO("Remote Client device [%s] is Connected", client_info->addr);
4117 BT_INFO("Remote GATT Client Device [%s] is not Connected", addr);
4126 int _bt_connect_le_device(bluetooth_device_address_t *address,
4127 int auto_connect, int client_id)
4129 struct gatt_server_info_t *conn_info = NULL;
4130 struct gatt_out_conn_info_t *out_conn_info = NULL;
4132 invocation_info_t *req_info = NULL;
4133 int ret = OAL_STATUS_SUCCESS;
4135 char *remote_address = NULL;
4137 BT_CHECK_PARAMETER(address, return);
4141 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4142 _bt_convert_addr_type_to_string(addr, address->addr);
4143 BT_INFO("GATT Client connect request for address [%s] client instance [%d]",
4147 /* Check if Remote Device is already under connection progress */
4148 req_info = _bt_get_request_info_data_from_function_name(BT_CONNECT_LE);
4150 remote_address = (char*)req_info->user_data;
4151 if (remote_address && !strcasecmp(remote_address, addr)) {/* Address matched */
4152 BT_INFO("Already Connection ongoing for same remote GATT Server address [%s]", remote_address);
4153 /* Return and wait for events to be sent to all apps */
4155 return BLUETOOTH_ERROR_IN_PROGRESS;
4159 /* Check if remote GATT Server is connected or not */
4160 conn_info = _bt_find_remote_gatt_server_info(addr);
4162 BT_ERR("GATT Server is already connected..");
4164 return BLUETOOTH_ERROR_ALREADY_CONNECT;
4167 /* TODO Check Requirement of holding Advertisement before initiating LE connect */
4169 /* Check if app sent 0 client id for connection, in such case, use default gatt client ID */
4170 if (client_id == 0) {
4171 BT_INFO("GATT CLient connect request sent by an app without any client instance [%d]",
4173 BT_INFO("Assign default GATT client id [%d]", gatt_default_client);
4174 client_id = gatt_default_client;
4177 BT_INFO("Connect using CLient ID [%d]", client_id);
4178 ret = gattc_connect(client_id, (bt_address_t*)(address), auto_connect);
4180 if (ret != OAL_STATUS_SUCCESS) {
4181 BT_ERR("ret: %d", ret);
4183 return BLUETOOTH_ERROR_INTERNAL;
4186 /* Mark this as outgoing connection */
4187 out_conn_info = g_new0(struct gatt_out_conn_info_t, 1);
4188 out_conn_info->addr = g_strdup(addr);
4189 out_conn_info->client_id = client_id;
4190 BT_INFO("Added outgoing connection info addr[%s]", out_conn_info->addr);
4191 outgoing_gatt_conn_list = g_slist_append(outgoing_gatt_conn_list, out_conn_info);
4194 return BLUETOOTH_ERROR_NONE;
4197 int _bt_gatt_get_primary_services(char *address)
4199 BT_CHECK_PARAMETER(address, return);
4200 struct gatt_server_info_t *conn_info = NULL;
4201 invocation_info_t *req_info = NULL;
4202 int ret = OAL_STATUS_SUCCESS;
4205 /* Check if any app is already browsing primary services on the same remote GATT Server */
4206 req_info = _bt_get_request_info_data(BT_GATT_GET_PRIMARY_SERVICES, address);
4208 BT_INFO("Already Primary Service Browsing ongoing for same rmeote GATT Server");
4209 /* Return and wait for events to be sent to all apps */
4210 return BLUETOOTH_ERROR_NONE;
4213 /* Check if remote GATT Server is connected or not */
4214 conn_info = _bt_find_remote_gatt_server_info(address);
4216 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4217 conn_info->addr, conn_info->connection_id);
4219 BT_ERR("GATT Server is not yet connected..");
4220 return BLUETOOTH_ERROR_NOT_CONNECTED;
4223 BT_INFO("Remove GATT server info from List..");
4224 /* Remove all services from info list_gatt_info */
4225 __bt_cleanup_remote_services(conn_info);
4227 /* Send Primary Service Browsing request to stack */
4228 ret = gattc_search_service(conn_info->connection_id, NULL);
4229 if (ret != OAL_STATUS_SUCCESS) {
4230 BT_ERR("ret: %d", ret);
4231 return BLUETOOTH_ERROR_INTERNAL;
4233 return BLUETOOTH_ERROR_NONE;
4236 int _bt_gatt_get_all_characteristic(bluetooth_gatt_client_svc_prop_info_t *svc)
4238 BT_CHECK_PARAMETER(svc, return);
4239 struct gatt_server_info_t *conn_info = NULL;
4240 invocation_info_t *req_info = NULL;
4241 bluetooth_gatt_client_svc_prop_info_t *prop;
4242 oal_gatt_srvc_id_t srvc_id;
4243 int ret = OAL_STATUS_SUCCESS;
4247 /* Check if any app is already browsing characteristics of the same service on the same remote GATT Server */
4248 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_GET_SERVICE_PROPERTIES);
4250 prop = (bluetooth_gatt_client_svc_prop_info_t*)req_info->user_data;
4251 if (prop && !memcmp(svc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t))
4252 && memcmp(prop->svc.uuid, svc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
4253 && prop->svc.instance_id == svc->svc.instance_id) {
4254 BT_INFO("Already Properties browsing for Primary Service ongoing for same remote GATT Server");
4255 /* Return and wait for events to be sent to all apps */
4256 return BLUETOOTH_ERROR_NONE;
4260 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4261 _bt_convert_addr_type_to_string(addr, svc->device_address.addr);
4263 /* Check if remote GATT Server is connected or not */
4264 conn_info = _bt_find_remote_gatt_server_info(addr);
4266 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4267 conn_info->addr, conn_info->connection_id);
4269 BT_ERR("GATT Server is not yet connected..");
4271 return BLUETOOTH_ERROR_NOT_CONNECTED;
4274 srvc_id.is_prmry = TRUE;
4275 srvc_id.id.inst_id = svc->svc.instance_id;
4276 memcpy(srvc_id.id.uuid.uuid, svc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4278 /* Search All Characteristic */
4279 ret = gattc_get_characteristic(conn_info->connection_id, &srvc_id, NULL);
4280 if (ret != OAL_STATUS_SUCCESS) {
4281 BT_ERR("ret: %d", ret);
4283 return BLUETOOTH_ERROR_INTERNAL;
4286 return BLUETOOTH_ERROR_NONE;
4289 int _bt_gatt_get_all_characteristic_properties(
4290 bluetooth_gatt_client_char_prop_info_t *chr)
4292 struct gatt_server_info_t *conn_info = NULL;
4293 invocation_info_t *req_info = NULL;
4294 bluetooth_gatt_client_char_prop_info_t *prop;
4295 oal_gatt_srvc_id_t srvc_id;
4296 oal_gatt_id_t char_id;
4297 int ret = OAL_STATUS_SUCCESS;
4300 BT_CHECK_PARAMETER(chr, return);
4304 /* Check if any app is already browsing descriptors of the same char of
4305 particular service on the same remote GATT Server */
4306 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_GET_CHARACTERISTIC_PROPERTIES);
4308 prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
4309 if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4310 && memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4311 && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4312 && memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4313 && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
4314 BT_INFO("Already Properties browsing for Characteristic ongoing for same remote GATT Server");
4315 /* Return and wait for events to be sent to all apps */
4316 return BLUETOOTH_ERROR_NONE;
4320 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4321 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4323 /* Check if remote GATT Server is connected or not */
4324 conn_info = _bt_find_remote_gatt_server_info(addr);
4326 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4327 conn_info->addr, conn_info->connection_id);
4329 BT_ERR("GATT Server is not yet connected..");
4331 return BLUETOOTH_ERROR_NOT_CONNECTED;
4334 srvc_id.is_prmry = TRUE;
4335 srvc_id.id.inst_id = chr->svc.instance_id;
4336 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4338 char_id.inst_id = chr->characteristic.instance_id;
4339 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4341 /* Search All Descriptors */
4342 ret = gattc_get_descriptor(conn_info->connection_id, &srvc_id, &char_id, NULL);
4343 if (ret != OAL_STATUS_SUCCESS) {
4344 BT_ERR("ret: %d", ret);
4346 return BLUETOOTH_ERROR_INTERNAL;
4349 return BLUETOOTH_ERROR_NONE;
4352 int _bt_gatt_read_characteristic_value(
4353 bluetooth_gatt_client_char_prop_info_t *chr)
4355 struct gatt_server_info_t *conn_info = NULL;
4356 invocation_info_t *req_info = NULL;
4357 bluetooth_gatt_client_char_prop_info_t *prop;
4358 oal_gatt_srvc_id_t srvc_id;
4359 oal_gatt_id_t char_id;
4360 int ret = OAL_STATUS_SUCCESS;
4363 BT_CHECK_PARAMETER(chr, return);
4367 /* Check if any app is already Reading characteristic of the same char of
4368 particular service on the same remote GATT Server */
4369 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_READ_CHARACTERISTIC);
4371 prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
4372 if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4373 && memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4374 && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4375 && memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4376 && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
4377 BT_INFO("Already Characteristic value Read operation in progress for same remote GATT Server");
4378 /* Return and wait for events to be sent to all apps */
4379 return BLUETOOTH_ERROR_NONE;
4383 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4384 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4386 /* Check if remote GATT Server is connected or not */
4387 conn_info = _bt_find_remote_gatt_server_info(addr);
4389 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4390 conn_info->addr, conn_info->connection_id);
4392 BT_ERR("GATT Server is not yet connected..");
4394 return BLUETOOTH_ERROR_NOT_CONNECTED;
4397 srvc_id.is_prmry = TRUE;
4398 srvc_id.id.inst_id = chr->svc.instance_id;
4399 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4401 char_id.inst_id = chr->characteristic.instance_id;
4402 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4404 /* Search All Descriptors */
4405 ret = gattc_read_characteristic(conn_info->connection_id, &srvc_id, &char_id, OAL_GATT_AUTH_REQ_NONE);
4406 if (ret != OAL_STATUS_SUCCESS) {
4407 BT_ERR("ret: %d", ret);
4409 return BLUETOOTH_ERROR_INTERNAL;
4412 return BLUETOOTH_ERROR_NONE;
4415 int _bt_gatt_read_descriptor_value(
4416 bluetooth_gatt_client_desc_prop_info_t *desc)
4418 struct gatt_server_info_t *conn_info = NULL;
4419 invocation_info_t *req_info = NULL;
4420 bluetooth_gatt_client_desc_prop_info_t *prop;
4421 oal_gatt_srvc_id_t srvc_id;
4422 oal_gatt_id_t char_id;
4423 oal_gatt_id_t desc_id;
4424 int ret = OAL_STATUS_SUCCESS;
4427 BT_CHECK_PARAMETER(desc, return);
4431 /* Check if any app is already Reading descriptors of the same char of
4432 particular service on the same remote GATT Server */
4433 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_READ_DESCRIPTOR_VALUE);
4435 prop = (bluetooth_gatt_client_desc_prop_info_t*)req_info->user_data;
4436 if (prop && !memcmp(desc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4437 && memcmp(desc->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4438 && desc->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4439 && memcmp(desc->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4440 && desc->characteristic.instance_id == prop->characteristic.instance_id /* Characteristic Instance ID matched */
4441 && memcmp(desc->descriptor.uuid, prop->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Descriptor UUID matched */
4442 && desc->descriptor.instance_id == prop->descriptor.instance_id) { /* Descriptor Instance ID matched */
4443 BT_INFO("Already Descriptor value Read operation in progress for same remote GATT Server");
4444 /* Return and wait for events to be sent to all apps */
4445 return BLUETOOTH_ERROR_NONE;
4449 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4450 _bt_convert_addr_type_to_string(addr, desc->device_address.addr);
4452 /* Check if remote GATT Server is connected or not */
4453 conn_info = _bt_find_remote_gatt_server_info(addr);
4455 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4456 conn_info->addr, conn_info->connection_id);
4458 BT_ERR("GATT Server is not yet connected..");
4460 return BLUETOOTH_ERROR_NOT_CONNECTED;
4463 srvc_id.is_prmry = TRUE;
4464 srvc_id.id.inst_id = desc->svc.instance_id;
4465 memcpy(srvc_id.id.uuid.uuid, desc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4467 char_id.inst_id = desc->characteristic.instance_id;
4468 memcpy(char_id.uuid.uuid, desc->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4470 desc_id.inst_id = desc->descriptor.instance_id;
4471 memcpy(desc_id.uuid.uuid, desc->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4473 /* Search All Descriptors */
4474 ret = gattc_read_descriptor(conn_info->connection_id, &srvc_id, &char_id,
4475 &desc_id, OAL_GATT_AUTH_REQ_NONE);
4476 if (ret != OAL_STATUS_SUCCESS) {
4477 BT_ERR("ret: %d", ret);
4479 return BLUETOOTH_ERROR_INTERNAL;
4482 return BLUETOOTH_ERROR_NONE;
4486 int _bt_gatt_acquire_notify(bluetooth_gatt_client_char_prop_info_t *chr, int *fd, int *mtu)
4488 struct gatt_server_info_t *conn_info = NULL;
4489 oal_gatt_srvc_id_t srvc_id;
4490 oal_gatt_id_t char_id;
4491 int ret = OAL_STATUS_SUCCESS;
4494 BT_CHECK_PARAMETER(chr, return);
4498 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4499 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4501 /* Check if remote GATT Server is connected or not */
4502 conn_info = _bt_find_remote_gatt_server_info(addr);
4504 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4505 conn_info->addr, conn_info->connection_id);
4507 BT_ERR("GATT Server is not yet connected..");
4509 return BLUETOOTH_ERROR_NOT_CONNECTED;
4512 srvc_id.is_prmry = TRUE;
4513 srvc_id.id.inst_id = chr->svc.instance_id;
4514 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4516 char_id.inst_id = chr->characteristic.instance_id;
4517 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4519 ret = gattc_acquire_notify(conn_info->connection_id, &srvc_id, &char_id, fd, mtu);
4521 if (ret != OAL_STATUS_SUCCESS) {
4522 BT_ERR("ret: %d", ret);
4524 return BLUETOOTH_ERROR_INTERNAL;
4526 BT_INFO("GATT characterstics FD [%d] mtu[%d]", *fd, *mtu);
4528 return BLUETOOTH_ERROR_NONE;
4532 int _bt_gatt_acquire_write(bluetooth_gatt_client_char_prop_info_t *chr, int *fd, int *mtu)
4535 struct gatt_server_info_t *conn_info = NULL;
4536 oal_gatt_srvc_id_t srvc_id;
4537 oal_gatt_id_t char_id;
4538 int ret = OAL_STATUS_SUCCESS;
4541 BT_CHECK_PARAMETER(chr, return);
4545 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4546 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4548 /* Check if remote GATT Server is connected or not */
4549 conn_info = _bt_find_remote_gatt_server_info(addr);
4551 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4552 conn_info->addr, conn_info->connection_id);
4554 BT_ERR("GATT Server is not yet connected..");
4556 return BLUETOOTH_ERROR_NOT_CONNECTED;
4559 srvc_id.is_prmry = TRUE;
4560 srvc_id.id.inst_id = chr->svc.instance_id;
4561 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4563 char_id.inst_id = chr->characteristic.instance_id;
4564 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4566 ret = gattc_acquire_write(conn_info->connection_id, &srvc_id, &char_id,
4567 OAL_GATT_AUTH_REQ_NONE, fd, mtu);
4568 if (ret != OAL_STATUS_SUCCESS) {
4569 BT_ERR("ret: %d", ret);
4571 return BLUETOOTH_ERROR_INTERNAL;
4573 BT_INFO("GATT characterstics FD [%d] mtu [%d]", *fd, *mtu);
4575 return BLUETOOTH_ERROR_NONE;
4580 /* Write Characteristic */
4581 int _bt_gatt_write_characteristic_value_by_type(
4582 bluetooth_gatt_client_char_prop_info_t *chr,
4583 bluetooth_gatt_att_data_t *data,
4584 bluetooth_gatt_write_type_e write_type)
4586 struct gatt_server_info_t *conn_info = NULL;
4587 invocation_info_t *req_info = NULL;
4588 bluetooth_gatt_client_char_prop_info_t *prop;
4589 oal_gatt_srvc_id_t srvc_id;
4590 oal_gatt_id_t char_id;
4591 int ret = OAL_STATUS_SUCCESS;
4595 BT_CHECK_PARAMETER(chr, return);
4596 BT_CHECK_PARAMETER(data, return);
4600 /* Check if any app is already writing same char of
4601 particular service on the same remote GATT Server */
4602 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE);
4604 prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
4605 if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4606 && memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4607 && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4608 && memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4609 && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
4610 BT_INFO("Already Characteristic Write Value operation in progress for same remote GATT Server");
4611 /* Return and wait for events to be sent to all apps */
4612 return BLUETOOTH_ERROR_NONE;
4616 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4617 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4619 /* Check if remote GATT Server is connected or not */
4620 conn_info = _bt_find_remote_gatt_server_info(addr);
4622 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4623 conn_info->addr, conn_info->connection_id);
4625 BT_ERR("GATT Server is not yet connected..");
4627 return BLUETOOTH_ERROR_NOT_CONNECTED;
4630 srvc_id.is_prmry = TRUE;
4631 srvc_id.id.inst_id = chr->svc.instance_id;
4632 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4634 char_id.inst_id = chr->characteristic.instance_id;
4635 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4637 /* Write CHar value */
4639 BT_INFO("Connection ID [%d] write type [%d] data length [%d]", conn_info->connection_id, write_type, data->length);
4640 for (k = 0; k < data->length; k++)
4641 BT_INFO("Data[%d] [0x%x]", k, data->data[k]);
4643 ret = gattc_write_characteristic(conn_info->connection_id,
4645 (oal_gatt_write_type_t)write_type, data->length,
4646 OAL_GATT_AUTH_REQ_NONE, (char *)(&data->data[0]));
4647 if (ret != OAL_STATUS_SUCCESS) {
4648 BT_ERR("ret: %d", ret);
4650 return BLUETOOTH_ERROR_INTERNAL;
4653 return BLUETOOTH_ERROR_NONE;
4656 /* Write Descriptor */
4657 int _bt_gatt_write_descriptor_value_by_type(
4658 bluetooth_gatt_client_desc_prop_info_t *desc,
4659 bluetooth_gatt_att_data_t *data,
4660 bluetooth_gatt_write_type_e write_type)
4662 struct gatt_server_info_t *conn_info = NULL;
4663 invocation_info_t *req_info = NULL;
4664 bluetooth_gatt_client_desc_prop_info_t *prop;
4665 oal_gatt_srvc_id_t srvc_id;
4666 oal_gatt_id_t char_id;
4667 oal_gatt_id_t desc_id;
4668 int ret = OAL_STATUS_SUCCESS;
4672 BT_CHECK_PARAMETER(desc, return);
4673 BT_CHECK_PARAMETER(data, return);
4677 /* Check if any app is already writing on same Descriptor of the same char of
4678 particular service on the same remote GATT Server */
4679 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_WRITE_DESCRIPTOR_VALUE);
4681 prop = (bluetooth_gatt_client_desc_prop_info_t*)req_info->user_data;
4682 if (prop && !memcmp(desc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4683 && memcmp(desc->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4684 && desc->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4685 && memcmp(desc->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4686 && desc->characteristic.instance_id == prop->characteristic.instance_id /* Characteristic Instance ID matched */
4687 && memcmp(desc->descriptor.uuid, prop->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Descriptor UUID matched */
4688 && desc->descriptor.instance_id == prop->descriptor.instance_id) { /* Descriptor Instance ID matched */
4689 BT_INFO("Already Descriptor value Write operation in progress for same remote GATT Server");
4690 /* Return and wait for events to be sent to all apps */
4691 return BLUETOOTH_ERROR_NONE;
4695 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4696 _bt_convert_addr_type_to_string(addr, desc->device_address.addr);
4698 /* Check if remote GATT Server is connected or not */
4699 conn_info = _bt_find_remote_gatt_server_info(addr);
4701 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4702 conn_info->addr, conn_info->connection_id);
4704 BT_ERR("GATT Server is not yet connected..");
4706 return BLUETOOTH_ERROR_NOT_CONNECTED;
4709 srvc_id.is_prmry = TRUE;
4710 srvc_id.id.inst_id = desc->svc.instance_id;
4711 memcpy(srvc_id.id.uuid.uuid, desc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4713 char_id.inst_id = desc->characteristic.instance_id;
4714 memcpy(char_id.uuid.uuid, desc->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4716 desc_id.inst_id = desc->descriptor.instance_id;
4717 memcpy(desc_id.uuid.uuid, desc->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4720 BT_INFO("Connection ID [%d] write type [%d] data length [%d]", conn_info->connection_id, write_type, data->length);
4721 for (k = 0; k < data->length; k++)
4722 BT_INFO("Data[%d] [0x%x]", k, data->data[k]);
4724 ret = gattc_write_descriptor(conn_info->connection_id,
4725 &srvc_id, &char_id, &desc_id,
4726 (oal_gatt_write_type_t)write_type, data->length,
4727 OAL_GATT_AUTH_REQ_NONE, (char *)(&data->data[0]));
4728 if (ret != OAL_STATUS_SUCCESS) {
4729 BT_ERR("ret: %d", ret);
4731 return BLUETOOTH_ERROR_INTERNAL;
4734 return BLUETOOTH_ERROR_NONE;
4737 int _bt_gatt_watch_characteristic(
4738 bluetooth_gatt_client_char_prop_info_t *chr,
4742 struct gatt_server_info_t *conn_info = NULL;
4743 oal_gatt_srvc_id_t srvc_id;
4744 oal_gatt_id_t char_id;
4745 int ret = OAL_STATUS_SUCCESS;
4748 BT_CHECK_PARAMETER(chr, return);
4750 BT_INFO("Client ID [%d] Is Notify [%d]", client_id, is_notify);
4752 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4753 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4755 /* Check if remote GATT Server is connected or not */
4756 conn_info = _bt_find_remote_gatt_server_info(addr);
4758 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4759 conn_info->addr, conn_info->connection_id);
4761 BT_ERR("GATT Server is not yet connected..");
4763 return BLUETOOTH_ERROR_NOT_CONNECTED;
4765 srvc_id.is_prmry = TRUE;
4766 srvc_id.id.inst_id = chr->svc.instance_id;
4767 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4769 char_id.inst_id = chr->characteristic.instance_id;
4770 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4772 /* Register or unregister Notification characteristic */
4774 ret = gattc_register_for_notification(conn_info->client_id,
4775 (bt_address_t*)&(chr->device_address),
4776 &srvc_id, &char_id);
4778 ret = gattc_deregister_for_notification(conn_info->client_id,
4779 (bt_address_t*)&(chr->device_address),
4780 &srvc_id, &char_id);
4782 BT_INFO("Result[%d]", ret);
4783 if (ret != OAL_STATUS_SUCCESS) {
4784 BT_ERR("ret: %d", ret);
4786 return BLUETOOTH_ERROR_INTERNAL;
4789 return BLUETOOTH_ERROR_NONE;
4793 int _bt_disconnect_le_device(bluetooth_device_address_t *address,
4796 struct gatt_server_info_t *conn_info = NULL;
4797 struct gatt_client_info_t *rem_client_conn_info = NULL;
4798 invocation_info_t *req_info = NULL;
4799 int ret = OAL_STATUS_SUCCESS;
4801 char *remote_address = NULL;
4803 BT_CHECK_PARAMETER(address, return);
4807 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4808 _bt_convert_addr_type_to_string(addr, address->addr);
4809 BT_INFO("GATT Client Disconnect request for address [%s]", addr);
4811 /* Check if Remote Device is already under connection progress */
4812 req_info = _bt_get_request_info_data_from_function_name(BT_DISCONNECT_LE);
4814 remote_address = (char*)req_info->user_data;
4815 if (remote_address && !strcasecmp(remote_address, addr)) {/* Address matched */
4816 BT_INFO("Already DisConnection ongoing for same remote GATT Server address [%s]", remote_address);
4817 /* Return success and wait for events to be sent to all apps */
4819 return BLUETOOTH_ERROR_IN_PROGRESS;
4822 /* Check if remote GATT Server is connected or not */
4823 conn_info = _bt_find_remote_gatt_server_info(addr);
4825 /* Check if app sent 0 client id for Disconnection, in such case, use default gatt client ID */
4826 if (client_id == 0) {
4827 BT_INFO("GATT CLient Disconnect request sent by an app without any client instance [%d]",
4829 BT_INFO("Assign default GATT client id [%d]", gatt_default_client);
4830 client_id = gatt_default_client;
4833 BT_INFO("Disconnect remote gatt server using CLient ID [%d] Connection ID [%d]", client_id, conn_info->connection_id);
4834 ret = gattc_disconnect(client_id, (bt_address_t*)(address),
4835 conn_info->connection_id);
4837 /* check if remote client is connected */
4838 rem_client_conn_info = _bt_find_remote_gatt_client_info(addr);
4840 if (!rem_client_conn_info || client_id != 0) {
4841 BT_ERR("GATT device is not connected..");
4843 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
4846 BT_INFO("Disconnect remote gatt client ");
4848 ret = gatts_disconnect(rem_client_conn_info->instance_id,
4849 (bt_address_t*)(address), rem_client_conn_info->connection_id);
4852 if (ret != OAL_STATUS_SUCCESS) {
4853 BT_ERR("ret: %d", ret);
4855 return BLUETOOTH_ERROR_INTERNAL;
4858 return BLUETOOTH_ERROR_NONE;
4861 int _bt_gatt_watch_service_changed_indication(const char *sender,
4862 bluetooth_device_address_t *address,
4863 gboolean is_enabled)
4866 bt_service_app_info_t *info = NULL;
4868 BT_INFO("Enable Servic changed Indication watcher [%d] for app [%s]",
4869 is_enabled, sender);
4871 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
4874 if (g_strcmp0(sender, info->sender) == 0 &&
4875 memcmp(info->address.addr, address->addr,
4876 sizeof(bluetooth_device_address_t)) == 0) {
4877 BT_INFO("Found GATT client App.. UUID [%s], sender [%s]", info->uuid, info->sender);
4878 info->is_watcher_enabled = is_enabled;
4882 return BLUETOOTH_ERROR_NONE;
4885 int _bt_unregister_gatt_client_instance(const char *sender, int client_id)
4887 bt_service_app_info_t *info = NULL;
4890 BT_INFO("Unregister Allocated GATT Client instance [%s] Client ID [%d]", sender, client_id);
4892 /* Unregister CLient instance associated with address X. It is possible that another app still
4893 has client_id valid for same remote address */
4894 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
4897 /* Exact matching of sender */
4898 if (!g_strcmp0(info->sender, sender) && info->client_id == client_id) { /* Check for only valid GATT client Instance */
4899 BT_INFO("Unregister GATT client instance [%d]", info->client_id);
4900 numapps[k].client_id = -1;
4901 numapps[k].is_initialized = FALSE;
4902 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
4903 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
4904 memset(&numapps[k].address.addr, 0x00, sizeof(bluetooth_device_address_t));
4906 /* Its a GATT Client Instance */
4907 ret = gattc_deregister(client_id);
4908 if (ret != OAL_STATUS_SUCCESS) {
4909 BT_ERR("DeAllocate GATT Client instance with stack Fail ret: %d", ret);
4910 return BLUETOOTH_ERROR_INTERNAL;
4912 return BLUETOOTH_ERROR_NONE;
4917 return BLUETOOTH_ERROR_NOT_FOUND;
4920 static void __bt_handle_client_mtu_exchange_completed(event_gattc_mtu_configured_t *event_data)
4922 int result = BLUETOOTH_ERROR_NONE;
4923 struct gatt_server_info_t *conn_info = NULL;
4924 GVariant *param = NULL;
4928 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(event_data->conn_id);
4929 if (conn_info == NULL) {
4930 BT_ERR("Cant find connection Information");
4934 BT_INFO("GATT Client: MTU Configured from addr [%s] status [%d] MTU size [%d]",
4935 conn_info->addr, event_data->status, event_data->mtu);
4937 if (event_data->status != OAL_STATUS_SUCCESS)
4938 result = BLUETOOTH_ERROR_INTERNAL;
4940 BT_INFO("reply BT_REQ_ATT_MTU ");
4942 /* DBUS Return fo BT_REQ_ATT_MTU for all the apps */
4943 __bt_gatt_handle_pending_request_info(result, BT_REQ_ATT_MTU, conn_info->addr,
4944 BT_ADDRESS_STRING_SIZE);
4946 if (result == BLUETOOTH_ERROR_NONE) {
4947 BT_INFO("sending mtu event");
4949 mtu = event_data->mtu;
4950 param = g_variant_new("(isqy)",
4956 /* Send event to BT-API */
4957 _bt_send_event(BT_DEVICE_EVENT,
4958 BLUETOOTH_EVENT_GATT_ATT_MTU_CHANGED,
4961 /* Update the MTU for current connection */
4962 BT_INFO("Updating the MTU for current connection");
4963 __bt_update_mtu_gatt_device(conn_info->addr, event_data->mtu);
4967 static struct gatt_mtu_info_t *__bt_find_mtu_gatt_device(char *address)
4970 struct gatt_mtu_info_t *info = NULL;
4974 for (l = gatt_mtu_info_list; l != NULL; l = g_slist_next(l)) {
4975 info = (struct gatt_mtu_info_t*)l->data;
4979 if (!g_strcmp0(info->addr, address)) {
4980 BT_INFO("Remote GATT device found addr[%s]", info->addr);
4985 BT_INFO("Not found Remote GATT device addr[%s]", address);
4989 static void __bt_remove_mtu_gatt_device(char *address)
4991 struct gatt_mtu_info_t *dev_info = NULL;
4995 dev_info = __bt_find_mtu_gatt_device(address);
4998 BT_INFO("removing the gatt device from mtu list");
4999 gatt_mtu_info_list = g_slist_remove(gatt_mtu_info_list, dev_info);
5000 g_free(dev_info->addr);
5005 static void __bt_add_mtu_gatt_device(char *address)
5007 struct gatt_mtu_info_t *dev_info = NULL;
5011 dev_info = __bt_find_mtu_gatt_device(address);
5014 BT_INFO("adding the gatt device in mtu list");
5015 dev_info = g_new0(struct gatt_mtu_info_t, 1);
5016 dev_info->addr = g_strdup(address);
5017 dev_info->att_mtu = BT_DEFAULT_ATT_MTU;
5018 gatt_mtu_info_list = g_slist_append(gatt_mtu_info_list, dev_info);
5022 static void __bt_update_mtu_gatt_device(char *address, int mtu)
5024 struct gatt_mtu_info_t *dev_info = NULL;
5028 dev_info = __bt_find_mtu_gatt_device(address);
5031 BT_INFO("Udating the mtu");
5032 dev_info->att_mtu = mtu;