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_mtu_exchange_completed(event_gattc_mtu_configured_t *event_data);
262 static int __bt_unregister_gatt_server_instance(int server_instance);
265 struct gatt_mtu_info_t {
266 char *addr; /* Remote GATT Server address */
270 static GSList *gatt_mtu_info_list = NULL;
272 static struct gatt_mtu_info_t *__bt_find_mtu_gatt_device(char *address);
273 static void __bt_remove_mtu_gatt_device(char *address);
274 static void __bt_add_mtu_gatt_device(char *address);
275 static void __bt_update_mtu_gatt_device(char *address, int mtu);
277 /* Linked List of GATT requests from Remote GATT Clients */
278 static GSList *gatt_server_requests = NULL;
280 /* Linked List of connected Remote GATT clients */
281 static GSList *gatt_client_info_list = NULL;
283 /* Number of clients to be notified to */
284 static int num_indicate_clients;
286 /* List of applications */
287 static bt_service_app_info_t numapps[MAX_APPS_SUPPORTED];
289 static void __bt_gatt_handle_pending_request_info(int result,
290 int service_function, void *data, unsigned int size);
292 static void __bt_handle_server_instance_registered(event_gatts_register_t *data);
294 static void __bt_gatt_event_handler(int event_type, gpointer event_data);
296 void _bt_check_adv_app_termination(const char *name)
298 bt_service_app_info_t *app = NULL;
300 int apps[MAX_APPS_SUPPORTED];
302 ret_if(NULL == name);
304 memset(&apps, -1, sizeof(apps));
306 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
309 /* Search for a app which has same sender and stop adv is running */
310 if (!strncasecmp(app->sender, name, strlen(name)) && app->is_initialized == TRUE) {
311 BT_DBG("Match found, name: %s", name);
313 /* Unregister all service handles with stack */
314 __bt_remove_all_service_handles(app->instance_id);
316 /* If Advertising is enabled, stop it */
317 if (app->adv_handle > 0) {
318 BT_INFO("Stop advertising on instance ID [%d]", app->instance_id);
319 /* Disable adv if running */
320 BT_INFO("Disable Advertising Adv Handle [%d] sender [%s]",
321 app->adv_handle, name);
322 _bt_set_advertising(app->sender, app->adv_handle, FALSE, FALSE);
325 /* Save instances of all apps that need to be unregistered */
326 apps[app->instance_id] = 1;
330 /* Unregister all apps one by one */
331 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
333 BT_INFO("Unregister app[%d]", k);
334 /* Unregister server instance */
335 __bt_unregister_gatt_server_instance(k);
340 static int __bt_gatt_send_indication_to_all_connected_clients(bluetooth_gatt_att_data_t *data,
341 bluetooth_gatt_server_indication_params_t *param)
344 int ret = OAL_STATUS_SUCCESS;
345 int result = OAL_STATUS_INTERNAL_ERROR;
347 BT_INFO("Current total number of connected clients [%d]", g_slist_length(gatt_client_info_list));
348 for (l = gatt_client_info_list; l != NULL; l = l->next) {
349 struct gatt_client_info_t *info = l->data;
352 BT_INFO("GATT Remote client address [%s] connection Id [%d]", info->addr, info->connection_id);
354 ret = gatts_send_indication(param->instance_id, param->atrribute_handle,
355 info->connection_id, data->length,
356 param->need_confirmation, (char *)(&data->data[0]));
358 BT_INFO("Send Indication to GATT client [%s] result: [%d]", info->addr, ret);
359 if (ret == OAL_STATUS_SUCCESS) {
360 BT_INFO("Send Indication sent successfully to GATT client [%s]", info->addr);
362 num_indicate_clients++;
366 BT_INFO("Indication sending done for total number of clients [%d]", num_indicate_clients);
370 static struct gatt_server_req_info *__bt_gatt_server_find_request_info(int request_id,
371 bluetooth_gatt_att_request_type_e req_type)
375 for (l = gatt_server_requests; l != NULL; l = l->next) {
376 struct gatt_server_req_info *req_info = l->data;
378 if (req_info && req_info->request_id == request_id && req_info->request_type == req_type) {
379 BT_INFO("GATT Server request info found Req ID [%d] handle [%d] conn ID [%d]",
380 req_info->request_id, req_info->attribute_handle, req_info->connection_id);
384 BT_ERR("Gatt Request not found");
388 void _bt_get_adv_handle_from_instance(int server_inst, int *adv_handle)
392 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
393 if (numapps[k].is_initialized == 1 && numapps[k].instance_id == server_inst) {
394 *adv_handle = numapps[k].adv_handle;
400 #ifdef TIZEN_GATT_CLIENT
401 char * _bt_gatt_get_default_gatt_client_uuid(void)
403 return g_strdup(DEFAULT_GATT_CLIENT_UUID);
407 static void __bt_register_default_gatt_client()
412 uuid_str = _bt_gatt_get_default_gatt_client_uuid();
413 _bt_string_to_uuid(uuid_str, (service_uuid_t*)&uuid);
415 BT_INFO("Register Default GATT client uuid [%s]", uuid_str);
417 if (OAL_STATUS_SUCCESS != gattc_register(&uuid)) /* for only Smart Control */
418 BT_ERR("gattc register failed");
424 int _bt_gatt_init(void)
428 if (OAL_STATUS_SUCCESS != gatt_enable()) {
429 BT_ERR("gatt Init failed");
430 return BLUETOOTH_ERROR_INTERNAL;
433 /* Register gatt event handler */
434 _bt_service_register_event_handler_callback(BT_GATT_MODULE, __bt_gatt_event_handler);
435 #ifdef TIZEN_GATT_CLIENT
436 /*In bluedroid product, defacult gatt client is handled differently*/
437 __bt_register_default_gatt_client();
440 return BLUETOOTH_ERROR_NONE;
443 static void __bt_service_reset_gatt_data()
447 BT_INFO("Rest numapp");
450 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
451 numapps[k].is_initialized = 0;
452 numapps[k].instance_id = -1;
453 numapps[k].adv_handle = 0;
454 numapps[k].adv_instance = -1;
455 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
456 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
457 memset(numapps[k].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
458 memset(numapps[k].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
459 numapps[k].adv_data_len = 0;
460 numapps[k].scan_rsp_len = 0;
464 void _bt_gatt_deinit(void)
466 BT_INFO("GATT deinit");
468 if (OAL_STATUS_SUCCESS != gatt_disable())
469 BT_ERR("gatt deinit failed");
471 /* Un-register gatt event handler */
472 _bt_service_unregister_event_handler_callback(BT_GATT_MODULE);
474 __bt_service_reset_gatt_data();
477 void _bt_update_adv_handle(const char *sender, int adv_handle)
480 BT_DBG("Sender [%s] Adv handle [%d]", sender, adv_handle);
481 bt_service_app_info_t *info = NULL;
483 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
485 /* Search for a app which has same sender and adv handle as 0 */
486 if (!g_strcmp0(info->sender, sender) && info->adv_handle == 0)
487 info->adv_handle = adv_handle;
491 int _bt_is_sender_gatt_server_with_no_adv(const char *sender, int adv_handle)
494 BT_DBG("Sender [%s] Adv handle [%d]", sender, adv_handle);
495 bt_service_app_info_t *info = NULL;
497 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
499 /* Search for a app which has same sender and adv handle as 0
500 It is possible that same sender but different adv handle */
501 if (!g_strcmp0(info->sender, sender) && info->adv_handle == 0) {
502 //info->adv_handle = adv_handle;
503 return info->instance_id;
509 int _bt_get_allocated_server_instance(const char *sender, int adv_handle, gboolean use_reserved_slot)
512 BT_DBG("Sender [%s] Adv handle [%d]", sender, adv_handle);
513 bt_service_app_info_t *info = NULL;
515 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
518 /* Exact matching of Adv handle + sender combination */
519 if (!g_strcmp0(info->sender, sender) && info->adv_handle == adv_handle)
520 return info->instance_id;
522 if (!g_strcmp0(info->sender, sender) && info->adv_handle == -1)
523 return info->instance_id;
529 char * _bt_get_sender_and_handle(int server_instance, int *adv_handle)
532 bt_service_app_info_t *info = NULL;
534 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
537 if (info->instance_id == server_instance && info->adv_handle != -1) {
538 *adv_handle = info->adv_handle;
539 BT_DBG("Server instance [%d] Adv handle [%d] Sender [%s]", server_instance, *adv_handle, info->sender);
540 return g_strdup(info->sender);
546 void _bt_set_new_adv_data(bluetooth_advertising_data_t *adv, int len, int instance)
550 bt_service_app_info_t *info = NULL;
551 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
554 if (info->instance_id == instance) {
555 memcpy(info->adv_data.data, &adv->data, len);
562 void _bt_set_new_scan_rsp_data(bluetooth_scan_resp_data_t *scan, int len, int instance)
566 bt_service_app_info_t *info = NULL;
567 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
570 if (info->instance_id == instance) {
571 memcpy(info->scan_rsp.data, &scan->data, len);
578 void _bt_get_previous_adv_data(bluetooth_advertising_data_t *adv, int *len, int instance)
582 bt_service_app_info_t *info = NULL;
584 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
587 if (info->instance_id == instance) {
588 memcpy(&adv->data, info->adv_data.data, info->adv_data_len);
589 *len = info->adv_data_len;
595 void _bt_get_previous_scan_rsp_data(bluetooth_scan_resp_data_t *scan, int *len, int instance)
599 bt_service_app_info_t *info = NULL;
601 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
604 if (info->instance_id == instance) {
605 memcpy(&scan->data, info->scan_rsp.data, info->scan_rsp_len);
606 *len = info->scan_rsp_len;
612 static int __bt_unregister_gatt_server_instance(int server_instance)
614 int ret = OAL_STATUS_SUCCESS;
617 /* Unregister the server instance */
618 ret = gatts_unregister(server_instance);
619 if (ret != OAL_STATUS_SUCCESS) {
620 BT_ERR("DeAllocate server instance with stack Fail ret: %d", ret);
621 return BLUETOOTH_ERROR_INTERNAL;
623 BT_INFO("DeAllocated server instance with stack successful..");
626 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
627 if (numapps[k].instance_id == server_instance) {
628 numapps[k].is_initialized = 0;
629 numapps[k].instance_id = -1;
630 numapps[k].adv_handle = 0;
631 numapps[k].adv_instance = -1;
632 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
633 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
634 memset(numapps[k].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
635 memset(numapps[k].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
636 numapps[k].adv_data_len = 0;
637 numapps[k].scan_rsp_len = 0;
641 BT_DBG("Going8 to reset numapp block num [%d]", k);
642 return BLUETOOTH_ERROR_NONE;
645 static void __bt_remove_all_service_handles(int instance_id)
649 int ret = OAL_STATUS_SUCCESS;
652 count = g_slist_length(numapps[instance_id].service_handles);
653 BT_INFO("Before handle removal: current count [%d]", count);
655 for (l = numapps[instance_id].service_handles; l != NULL;) {
660 BT_INFO("Server Handle to be Removed [%d] Instance ID [%d]", *handle, instance_id);
661 if (_bt_gatt_server_stop_service(numapps[instance_id].sender, *handle, instance_id) != BLUETOOTH_ERROR_NONE)
664 ret = gatts_delete_service(instance_id, *handle);
665 if (ret != OAL_STATUS_SUCCESS) {
666 BT_ERR("ret: %d", ret);
669 numapps[instance_id].service_handles = g_slist_remove(numapps[instance_id].service_handles, handle);
672 count = g_slist_length(numapps[instance_id].service_handles);
673 BT_INFO("After deleting current count [%d]", count);
679 int _bt_unregister_server_instance(const char *sender, int adv_handle)
681 BT_INFO("Unregister Allocated server instance request Sender [%s] Adv handle [%d]", sender, adv_handle);
682 int result = BLUETOOTH_ERROR_NONE;
683 int apps[MAX_APPS_SUPPORTED];
687 memset(&apps, -1, sizeof(apps));
689 if (adv_handle == 0) {
690 BT_DBG("Its a direct GATT Server app request to unregister");
691 /* Unregister server instance for each app with same sender (case: GATT Server with multiple adv handle) */
692 bt_service_app_info_t *info = NULL;
694 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
697 /* Exact matching of sender */
698 if (!g_strcmp0(info->sender, sender)) {
699 BT_INFO("Unregister GATT server instance [%d]", info->instance_id);
700 /* Unregister all service handles with stack */
701 __bt_remove_all_service_handles(info->instance_id);
703 /* Disable adv if running */
704 BT_INFO("Disable Advertising Adv Handle [%d] sender [%s]",
705 info->adv_handle, sender);
706 _bt_set_advertising(sender, info->adv_handle, FALSE, FALSE);
708 /* Save all instances which need to be unregistered */
709 apps[info->instance_id] = 1;
713 BT_DBG("Its an Internal unregister request by adv application");
714 server_instance = _bt_get_allocated_server_instance(sender, adv_handle, FALSE);
715 BT_DBG("Its an Internal unregister request by adv application: Adv disabled srv instance [%d]", server_instance);
716 if (server_instance == -1) {
717 BT_ERR("No allocated server instance to be removed");
718 return BLUETOOTH_ERROR_INVALID_PARAM;
720 if (!numapps[server_instance].service_handles) {
721 BT_INFO("There are no Service handles with this app, safe to unregister");
722 /* Unregister server instance only if this sender does not have any gatt services in it */
723 result = __bt_unregister_gatt_server_instance(server_instance);
725 numapps[server_instance].adv_handle = 0;
726 memset(numapps[server_instance].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
727 numapps[server_instance].adv_data_len = 0;
728 memset(numapps[server_instance].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
729 numapps[server_instance].scan_rsp_len = 0;
734 /* Unregister all apps one by one */
735 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
737 BT_INFO("Unregister app[%d]", k);
738 /* Unregister server instance */
739 __bt_unregister_gatt_server_instance(k);
746 int _bt_register_server_instance(const char *sender, int adv_handle)
748 int ret = OAL_STATUS_SUCCESS;
749 char *uuid_string = NULL;
754 BT_INFO("###Check on which instance Server instance can be initialized....");
755 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
756 if (numapps[k].is_initialized == 1) {
757 BT_INFO("Instance ID [%d] is already in use..Check next slot", numapps[k].instance_id);
760 BT_INFO("Time to register GATT Server..UUID to be used is [%s] slot [%d]", uuid_list[slot-1], slot);
766 BT_ERR("No Slot if free for GATT Server registration..");
767 return BLUETOOTH_ERROR_REGISTRATION_FAILED;
770 uuid_string = g_malloc0(BT_UUID_STRING_MAX);
771 _bt_string_to_uuid(uuid_list[slot-1], (service_uuid_t*)&uuid);
772 g_strlcpy(uuid_string, uuid_list[slot-1], BT_UUID_STRING_MAX);
773 BT_INFO("Copied UUID string [%s]", uuid_string);
775 /* Register GATT Server */
776 ret = gatts_register(&uuid);
777 if (ret != OAL_STATUS_SUCCESS) {
778 BT_ERR("ret: %d", ret);
780 return BLUETOOTH_ERROR_INTERNAL;
782 BT_INFO("GATT Server registration call successfully accepted by OAL..wait for Instance Initialized event from OAL..");
783 /* Return & wait for GATT Server Instance Initialization event */
784 memset(numapps[slot].sender, 0x00, sizeof(numapps[slot].sender));
785 memset(numapps[slot].uuid, 0x00, sizeof(numapps[slot].uuid));
787 g_strlcpy(numapps[slot].sender, sender, sizeof(numapps[slot].sender));
788 g_strlcpy(numapps[slot].uuid, uuid_string, sizeof(numapps[slot].uuid));
790 numapps[slot].is_initialized = 0; /* Set initialization from app registered callback */
791 numapps[slot].adv_handle = adv_handle;
794 return BLUETOOTH_ERROR_NONE;
798 static void __bt_gatt_handle_pending_request_info(int result,
799 int service_function, void *data, unsigned int size)
803 invocation_info_t *req_info = NULL;
804 ret_if(data == NULL);
807 for (l = _bt_get_invocation_list(); l != NULL; ) {
810 if (req_info == NULL || req_info->service_function != service_function)
813 switch (service_function) {
815 case BT_GATT_SERVER_REGISTER: {
816 bt_service_app_info_t *param = (bt_service_app_info_t*)data;
817 BT_INFO("Sender [%s] Request Sender [%s]", param->sender, req_info->sender);
818 if (!g_strcmp0(req_info->sender, param->sender)) {
819 BT_DBG("GATT Server app found [%s]", req_info->sender);
821 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
822 g_array_append_vals(out_param, ¶m->instance_id, sizeof(int));
823 _bt_service_method_return(req_info->context, out_param, result);
825 g_free(req_info->user_data);
826 _bt_free_info_from_invocation_list(req_info);
827 g_array_free(out_param, TRUE);
831 case BT_GATT_SERVER_START_SERVICE:
832 case BT_GATT_SERVER_DELETE_SERVICE: {
833 bt_service_app_info_t *param = (bt_service_app_info_t*)data;
834 BT_INFO("Sender [%s] Request Sender [%s]", param->sender, req_info->sender);
836 int *saved_instance_id = (int*)req_info->user_data;
837 if (!g_strcmp0(req_info->sender, param->sender) && param->instance_id == *saved_instance_id) {
838 BT_DBG("GATT Server app found [%s] Instance ID [%d] Reply DBUS",
839 req_info->sender, *saved_instance_id);
841 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
842 g_array_append_vals(out_param, &saved_instance_id, sizeof(int));
843 _bt_service_method_return(req_info->context, out_param, result);
845 g_free(req_info->user_data);
846 _bt_free_info_from_invocation_list(req_info);
847 g_array_free(out_param, TRUE);
851 case BT_GATT_SERVER_ADD_SERVICE:
852 case BT_GATT_SERVER_ADD_DESCRIPTOR:
853 case BT_GATT_SERVER_ADD_CHARACTERISTIC: {
854 int *handle = (int*)data;
855 BT_INFO("Characteristic added: Handle [%d]", *handle);
856 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
858 g_array_append_vals(out_param, handle, sizeof(int));
859 _bt_service_method_return(req_info->context, out_param, result);
861 g_free(req_info->user_data);
862 _bt_free_info_from_invocation_list(req_info);
863 g_array_free(out_param, TRUE);
868 case BT_DISCONNECT_LE: {
869 #ifdef TIZEN_GATT_CLIENT
870 char *addr = (char*)req_info->user_data;
871 bluetooth_device_address_t address;
873 if (!g_strcmp0(addr, (char*)data)) {
874 BT_INFO("GATT Client connect-disconnect call pending for app [%s] addr [%s]",
875 req_info->sender, addr);
876 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
877 _bt_convert_addr_string_to_type(address.addr, addr);
879 g_array_append_vals(out_param, (bluetooth_device_address_t*)&address,
880 sizeof(bluetooth_device_address_t));
881 _bt_service_method_return(req_info->context, out_param, result);
883 g_free(req_info->user_data);
884 _bt_free_info_from_invocation_list(req_info);
885 g_array_free(out_param, TRUE);
888 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
890 g_array_append_vals(out_param, (bluetooth_device_address_t*)data,
891 sizeof(bluetooth_device_address_t));
892 _bt_service_method_return(req_info->context, out_param, result);
894 g_free(req_info->user_data);
895 _bt_free_info_from_invocation_list(req_info);
897 g_array_free(out_param, TRUE);
901 #ifdef TIZEN_GATT_CLIENT
902 case BT_GATT_CLIENT_REGISTER: {
903 bt_service_app_info_t *param = (bt_service_app_info_t*)data;
904 BT_INFO("Sender [%s] Request Sender [%s]", param->sender, req_info->sender);
905 if (!g_strcmp0(req_info->sender, param->sender)) {
906 BT_DBG("GATT Client app found [%s] created client ID [%d]",
907 req_info->sender, param->client_id);
909 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
910 g_array_append_vals(out_param, ¶m->client_id, sizeof(int));
911 _bt_service_method_return(req_info->context, out_param, result);
913 g_free(req_info->user_data);
914 _bt_free_info_from_invocation_list(req_info);
915 g_array_free(out_param, TRUE);
919 case BT_GATT_GET_PRIMARY_SERVICES: {
920 bt_services_browse_info_t *param = (bt_services_browse_info_t*)data;
921 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
922 _bt_convert_addr_type_to_string(address,
923 (unsigned char *)(¶m->device_addr.addr));
925 BT_INFO("Request Sender [%s] address [%s]", req_info->sender, address);
927 /* Match address to determine same request */
928 if (!g_strcmp0((char*)req_info->user_data, address)) {
929 BT_DBG("GATT Client app found [%s] Remote address [%s]",
930 req_info->sender, address);
932 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
933 g_array_append_vals(out_param, param,
934 sizeof(bt_services_browse_info_t));
936 //g_array_append_vals(out_param, ¶m->client_id, sizeof(int));
937 _bt_service_method_return(req_info->context, out_param, result);
939 g_free(req_info->user_data);
940 _bt_free_info_from_invocation_list(req_info);
941 g_array_free(out_param, TRUE);
946 case BT_GATT_GET_SERVICE_PROPERTIES: {
947 bt_char_browse_info_t param;
948 memcpy((void*)¶m, data, sizeof(bt_char_browse_info_t));
949 //bt_char_browse_info_t *param = (bt_char_browse_info_t*)data;
950 BT_INFO("Request Sender [%s]", req_info->sender);
951 bluetooth_gatt_client_svc_prop_info_t *prop = (bluetooth_gatt_client_svc_prop_info_t*)req_info->user_data;
953 /* Match both address and service properties to determine same request */
954 if (!memcmp(param.device_addr.addr,
955 prop->device_address.addr,
956 sizeof(bluetooth_device_address_t)) &&
957 !memcmp(param.svc_uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
958 param.svc_inst_id == prop->svc.instance_id) {
959 BT_INFO("Returning Service properties");
961 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
962 g_array_append_vals(out_param, ¶m, sizeof(bt_char_browse_info_t));
963 _bt_service_method_return(req_info->context, out_param, result);
965 g_free(req_info->user_data);
966 _bt_free_info_from_invocation_list(req_info);
967 g_array_free(out_param, TRUE);
971 case BT_GATT_GET_CHARACTERISTIC_PROPERTIES: {
972 bt_descriptor_browse_info_t *param = (bt_descriptor_browse_info_t*)data;
974 BT_INFO("Request Sender [%s]", req_info->sender);
975 bluetooth_gatt_client_char_prop_info_t *prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
977 /* Match both address, service properties &char properties to determine same request */
978 if (!memcmp(param->device_addr.addr,
979 prop->device_address.addr,
980 sizeof(bluetooth_device_address_t)) &&
981 !memcmp(param->svc_uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
982 param->svc_inst_id == prop->svc.instance_id &&
983 !memcmp(param->char_uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
984 param->char_inst_id == prop->characteristic.instance_id) {
985 BT_INFO("Returning Characteristic properties");
986 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
987 g_array_append_vals(out_param, param, sizeof(bt_descriptor_browse_info_t));
988 _bt_service_method_return(req_info->context, out_param, result);
990 g_free(req_info->user_data);
991 _bt_free_info_from_invocation_list(req_info);
992 g_array_free(out_param, TRUE);
996 case BT_GATT_WATCH_CHARACTERISTIC: {
997 bt_gatt_notif_reg_info_t *param = (bt_gatt_notif_reg_info_t*)data;
998 bluetooth_gatt_client_char_prop_info_t *prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
999 BT_INFO("Request Sender [%s]", req_info->sender);
1000 /* Match both address, service properties &char properties to determine same request */
1001 if (!memcmp(param->addr.addr,
1002 prop->device_address.addr,
1003 sizeof(bluetooth_device_address_t)) &&
1004 !memcmp(param->svc_uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1005 param->svc_inst == prop->svc.instance_id &&
1006 !memcmp(param->char_uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1007 param->char_inst == prop->characteristic.instance_id) {
1008 BT_INFO("Characteristic Watch Successful: Is registered [%d]",
1009 param->is_registered);
1010 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1011 g_array_append_vals(out_param, param, sizeof(bt_gatt_notif_reg_info_t));
1012 _bt_service_method_return(req_info->context, out_param, result);
1013 g_free(req_info->user_data);
1014 _bt_free_info_from_invocation_list(req_info);
1015 g_array_free(out_param, TRUE);
1019 case BT_GATT_READ_CHARACTERISTIC:
1020 case BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE: {
1021 bluetooth_gatt_client_char_prop_info_t *param = (bluetooth_gatt_client_char_prop_info_t*)data;
1022 BT_INFO("Request Sender [%s]", req_info->sender);
1024 char *addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
1025 bluetooth_gatt_client_char_prop_info_t *prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
1026 _bt_convert_addr_type_to_string(addr, prop->device_address.addr);
1028 if (!memcmp(param, prop, sizeof(bluetooth_gatt_client_char_prop_info_t))) {
1029 BT_INFO("Gatt Char read or write request matched for address [%s]", addr);
1030 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1031 g_array_append_vals(out_param, param, sizeof(bluetooth_gatt_client_char_prop_info_t));
1032 _bt_service_method_return(req_info->context, out_param, result);
1034 g_free(req_info->user_data);
1035 _bt_free_info_from_invocation_list(req_info);
1036 g_array_free(out_param, TRUE);
1041 case BT_GATT_READ_DESCRIPTOR_VALUE:
1042 case BT_GATT_WRITE_DESCRIPTOR_VALUE: {
1043 bluetooth_gatt_client_desc_prop_info_t *param = (bluetooth_gatt_client_desc_prop_info_t*)data;
1044 BT_INFO("Request Sender [%s]", req_info->sender);
1046 char *addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
1047 bluetooth_gatt_client_desc_prop_info_t *prop = (bluetooth_gatt_client_desc_prop_info_t*)req_info->user_data;
1048 _bt_convert_addr_type_to_string(addr, prop->device_address.addr);
1050 if (!memcmp(param, prop, sizeof(bluetooth_gatt_client_desc_prop_info_t))) {
1051 BT_INFO("Descriptor read or write request matched for address [%s]", addr);
1052 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1053 g_array_append_vals(out_param, param, sizeof(bluetooth_gatt_client_desc_prop_info_t));
1054 _bt_service_method_return(req_info->context, out_param, result);
1056 g_free(req_info->user_data);
1057 _bt_free_info_from_invocation_list(req_info);
1058 g_array_free(out_param, TRUE);
1063 case BT_REQ_ATT_MTU: {
1064 char *addr = (char*)req_info->user_data;
1065 bluetooth_device_address_t address;
1067 if (!g_strcmp0(addr, (char*)data)) {
1068 BT_INFO("GATT Client BT_REQ_ATT_MTU call pending for app [%s] addr [%s]",
1069 req_info->sender, addr);
1070 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1071 _bt_convert_addr_string_to_type(address.addr, addr);
1073 g_array_append_vals(out_param, (bluetooth_device_address_t*)&address,
1074 sizeof(bluetooth_device_address_t));
1075 _bt_service_method_return(req_info->context, out_param, result);
1077 g_free(req_info->user_data);
1078 _bt_free_info_from_invocation_list(req_info);
1079 g_array_free(out_param, TRUE);
1090 static void __bt_handle_server_instance_registered(event_gatts_register_t *data)
1092 bt_service_app_info_t *info = NULL;
1094 char *uuid_string = g_malloc0(BT_UUID_STRING_MAX);
1096 _bt_uuid_to_string(&(data->server_uuid), uuid_string);
1097 BT_INFO("Instance ID is Intialized [%d] UUID initialized [%s]", data->server_inst, uuid_string);
1099 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1100 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1103 if (g_strcmp0(info->uuid, uuid_string) == 0) {
1104 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1105 info->is_initialized = TRUE;
1106 info->instance_id = data->server_inst;
1107 info->adv_instance = data->server_inst;
1108 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE, BT_GATT_SERVER_REGISTER,
1109 (void*)info, sizeof(bt_service_app_info_t));
1113 g_free(uuid_string);
1116 static void __bt_handle_gatt_server_service_added(event_gatts_srvc_prm_t *event)
1118 int result = BLUETOOTH_ERROR_NONE;
1122 bt_service_app_info_t *info = NULL;
1125 char *uuid_str = g_malloc0(BT_UUID_STRING_MAX);
1126 _bt_uuid_to_string(&(event->gatt_srvc_id.id.uuid), uuid_str);
1127 BT_INFO("GATT Added Service UUID: [%s] Server Ins [%d] Service hdl [%d] Is Primary [%d]\n",
1128 uuid_str, event->gatt_srvc_stat.server_inst, svc_handle, event->gatt_srvc_id.is_prmry);
1130 if (event->gatt_srvc_stat.status != OAL_STATUS_SUCCESS) {
1131 result = BLUETOOTH_ERROR_INTERNAL;
1132 svc_handle = 0; /* Service handle set to 0 indicates.
1133 0 is reserved by BT SIG, cant be used by app*/
1135 svc_handle = event->gatt_srvc_stat.servic_hndl;
1137 BT_INFO("GATT Added Service Status [%d] Server Ins [%d] Service hdl [%d] Is Primary [%d]\n",
1138 event->gatt_srvc_stat.status, event->gatt_srvc_stat.server_inst, svc_handle, event->gatt_srvc_id.is_prmry);
1140 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1141 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1144 if (info->instance_id == event->gatt_srvc_stat.server_inst) {
1145 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1146 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_ADD_SERVICE,
1147 (int*)&svc_handle, sizeof(int));
1149 /* Add Service Handle */
1150 if (svc_handle > 0) {
1151 handle = g_malloc0(sizeof(int));
1152 *handle = svc_handle;
1153 numapps[event->gatt_srvc_stat.server_inst].service_handles = g_slist_append(numapps[event->gatt_srvc_stat.server_inst].service_handles, handle);
1154 count = g_slist_length(numapps[event->gatt_srvc_stat.server_inst].service_handles);
1155 BT_INFO("Added Service handle [%d] to list..current count [%d]", svc_handle, count);
1164 static void __bt_handle_gatt_server_characteristic_added(event_gatts_srvc_charctr_t *event)
1166 int result = BLUETOOTH_ERROR_NONE;
1168 bt_service_app_info_t *info = NULL;
1170 char *uuid_str = g_malloc0(BT_UUID_STRING_MAX);
1172 BT_INFO("GATT Server Char added status [%d]", event->gatt_srvc_stat.status);
1173 BT_INFO("GATT Server_inst: [%d]", event->gatt_srvc_stat.server_inst);
1174 BT_INFO("GATT Service Handle: [%d]", event->gatt_srvc_stat.servic_hndl);
1175 BT_INFO("GATT Add characteristic Status: [%d]", event->gatt_srvc_stat.status);
1176 BT_INFO("GATT Service characteristic Handle: [%d]", event->charctr_hndl);
1178 if (event->gatt_srvc_stat.status != OAL_STATUS_SUCCESS) {
1179 result = BLUETOOTH_ERROR_INTERNAL;
1180 char_handle = 0; /* characteristic handle set to 0 indicates.
1181 0 is reserved by BT SIG, cant be used by app*/
1183 char_handle = event->charctr_hndl;
1186 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1187 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1190 if (info->instance_id == event->gatt_srvc_stat.server_inst) {
1191 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1192 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_ADD_CHARACTERISTIC,
1193 (int*)&char_handle, sizeof(int));
1199 _bt_uuid_to_string(&(event->charctr_uuid), uuid_str);
1200 BT_INFO("GATT Added Characteristic: UUID: [%s]", uuid_str);
1205 static void __bt_handle_gatt_server_descriptor_added(event_gatts_srvc_descr_t* event)
1207 int result = BLUETOOTH_ERROR_NONE;
1209 bt_service_app_info_t *info = NULL;
1211 char *uuid_str = g_malloc0(BT_UUID_STRING_MAX);
1213 BT_INFO("GATT Server Descriptor added status [%d]", event->gatt_srvc_stat.status);
1214 BT_INFO("GATT Server_inst: [%d]", event->gatt_srvc_stat.server_inst);
1215 BT_INFO("GATT Service Handle: [%d]", event->gatt_srvc_stat.servic_hndl);
1216 BT_INFO("GATT Add Descriptor Status: [%d]", event->gatt_srvc_stat.status);
1217 BT_INFO("GATT Service Descriptor Handle: [%d]", event->descrptr_hndl);
1219 if (event->gatt_srvc_stat.status != OAL_STATUS_SUCCESS) {
1220 result = BLUETOOTH_ERROR_INTERNAL;
1221 desc_handle = 0; /* Service handle set to 0 indicates.
1222 0 is reserved by BT SIG, cant be used by app*/
1224 desc_handle = event->descrptr_hndl;
1226 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1227 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1230 if (info->instance_id == event->gatt_srvc_stat.server_inst) {
1231 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1232 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_ADD_DESCRIPTOR,
1233 (int*)&desc_handle, sizeof(int));
1238 _bt_uuid_to_string(&(event->descrptr_uuid), uuid_str);
1239 BT_INFO("GATT Added Descriptor: UUID: [%s]", uuid_str);
1244 static void __bt_handle_gatt_server_service_started(event_gatts_srvc_t *event)
1246 bt_service_app_info_t *info = NULL;
1247 int result = BLUETOOTH_ERROR_NONE;
1249 BT_INFO("GATT Server Service Started..");
1252 BT_INFO("GATT Server_inst: [%d]", event->server_inst);
1253 BT_INFO("GATT Service Handle: [%d]", event->servic_hndl);
1254 BT_INFO("GATT Service Started Status: [%d]", event->status);
1256 if (event->status != OAL_STATUS_SUCCESS) {
1257 BT_ERR("GATT Server Service Start Failed Err: [%d]", event->status);
1258 result = BLUETOOTH_ERROR_INTERNAL;
1261 /* Check if the just registered Instance ID belongs to requester */
1262 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1265 if (info->instance_id == event->server_inst) {
1266 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1267 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_START_SERVICE,
1268 (void*)info, sizeof(bt_service_app_info_t));
1274 static void __bt_handle_gatt_server_service_stopped(event_gatts_srvc_t *event)
1276 int result = BLUETOOTH_ERROR_NONE;
1277 bt_service_app_info_t *info = NULL;
1279 BT_INFO("GATT Server Service Stopped..");
1281 BT_INFO("GATT Server_inst: [%d]", event->server_inst);
1282 BT_INFO("GATT Service Handle: [%d]", event->servic_hndl);
1283 BT_INFO("GATT Service Stopped Status: [%d]", event->status);
1285 if (event->status != OAL_STATUS_SUCCESS) {
1286 BT_ERR("GATT Server Service Stop Failed Err: [%d]", event->status);
1287 result = BLUETOOTH_ERROR_INTERNAL;
1290 /* Check if the just registered Instance ID belongs to requester */
1291 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1294 if (info->instance_id == event->server_inst) {
1295 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1296 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_STOP_SERVICE,
1297 (void*)info, sizeof(bt_service_app_info_t));
1303 static void __bt_handle_gatt_server_service_deleted(event_gatts_srvc_t *event)
1305 int result = BLUETOOTH_ERROR_NONE;
1306 bt_service_app_info_t *info = NULL;
1308 BT_INFO("GATT Server Service Deleted..");
1310 BT_INFO("GATT Server_inst: [%d]", event->server_inst);
1311 BT_INFO("GATT Service Handle: [%d]", event->servic_hndl);
1312 BT_INFO("GATT Service Deleted Status: [%d]", event->status);
1314 if (event->status != OAL_STATUS_SUCCESS) {
1315 BT_ERR("GATT Server Service Delete Failed Err: [%d]", event->status);
1316 result = BLUETOOTH_ERROR_INTERNAL;
1319 /* Check if the just registered Instance ID belongs to requester */
1320 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1323 if (info->instance_id == event->server_inst) {
1324 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1325 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_DELETE_SERVICE,
1326 (void*)info, sizeof(bt_service_app_info_t));
1332 struct gatt_client_info_t *_bt_find_remote_gatt_client_info(char *address)
1335 struct gatt_client_info_t *info = NULL;
1336 for (l = gatt_client_info_list; l != NULL; l = g_slist_next(l)) {
1337 info = (struct gatt_client_info_t*)l->data;
1341 if (!g_strcmp0(info->addr, address)) {
1342 BT_INFO("Remote GATT client found addr[%s]", info->addr);
1349 static struct gatt_client_info_t *__bt_find_remote_gatt_client_info_from_conn_id(int conn_id)
1352 struct gatt_client_info_t *info = NULL;
1354 for (l = gatt_client_info_list; l != NULL; l = g_slist_next(l)) {
1355 info = (struct gatt_client_info_t*)l->data;
1359 if (info->connection_id == conn_id) {
1360 BT_INFO("Remote GATT client found addr[%s]", info->addr);
1367 #ifdef TIZEN_GATT_CLIENT
1368 struct gatt_server_info_t *_bt_find_remote_gatt_server_info(char *address)
1371 struct gatt_server_info_t *info = NULL;
1372 for (l = gatt_server_info_list; l != NULL; l = g_slist_next(l)) {
1373 info = (struct gatt_server_info_t*)l->data;
1377 if (!g_strcmp0(info->addr, address)) {
1378 BT_INFO("Remote GATT Server found addr[%s]", info->addr);
1385 static struct gatt_out_conn_info_t* __bt_find_gatt_outgoing_conn_info(char *address)
1388 struct gatt_out_conn_info_t *info = NULL;
1389 for (l = outgoing_gatt_conn_list; l != NULL; l = g_slist_next(l)) {
1390 info = (struct gatt_out_conn_info_t*)l->data;
1394 if (!g_strcmp0(info->addr, address)) {
1395 BT_INFO("Outgoing connection info found addr[%s]", info->addr);
1402 static struct gatt_server_info_t *__bt_find_remote_gatt_server_info_from_client_if(int client_if)
1405 struct gatt_server_info_t *info = NULL;
1407 for (l = gatt_server_info_list; l != NULL; l = g_slist_next(l)) {
1408 info = (struct gatt_server_info_t*)l->data;
1412 if (info->client_id == client_if) {
1413 BT_INFO("Remote GATT server found addr[%s]", info->addr);
1424 #ifdef TIZEN_GATT_CLIENT
1425 static void __bt_handle_gatt_server_connection_state(event_gatts_conn_t *event)
1427 int result = BLUETOOTH_ERROR_NONE;
1428 struct gatt_client_info_t *client_info = NULL;
1429 struct gatt_server_info_t *server_info = NULL;
1430 bluetooth_device_address_t dev_addr;
1431 GVariant *param = NULL;
1434 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1436 memcpy(dev_addr.addr, event->address.addr, 6);
1438 /* REPLY dbus Context to application which called BT_CONNECT_LE. There is status
1440 _bt_convert_addr_type_to_string(address,
1441 (unsigned char *)dev_addr.addr);
1443 if (event->status != OAL_STATUS_SUCCESS)
1444 result = BLUETOOTH_ERROR_INTERNAL;
1446 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
1447 address, BT_ADDRESS_STRING_SIZE);
1449 BT_INFO("GATT Server Connedted: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1450 address, event->server_inst, event->conn_id);
1453 /* Check if device is already in connected list */
1454 client_info = _bt_find_remote_gatt_client_info(address);
1457 BT_INFO("Conn Info absent: But no need to Send Local GATT Server Connected event to apps");
1459 param = g_variant_new("(is)", result, address);
1461 /* Send event to application */
1462 _bt_send_event(BT_DEVICE_EVENT,
1463 BLUETOOTH_EVENT_GATT_SERVER_CONNECTED, /* Local device is GATT server */
1466 /* Save client connection info */
1467 client_info = g_new0(struct gatt_client_info_t, 1);
1468 client_info->addr = g_strdup(address);
1469 BT_INFO("Added GATT client addr[%s]", client_info->addr);
1470 client_info->connection_id = event->conn_id;
1471 client_info->instance_id = event->server_inst;
1472 gatt_client_info_list = g_slist_append(gatt_client_info_list, client_info);
1473 BT_INFO("Total num of connected Remote GATT Clients [%d]", g_slist_length(gatt_client_info_list));
1475 /* Save server connection info */
1476 server_info = g_new0(struct gatt_server_info_t, 1);
1477 server_info->addr = g_strdup(address);
1478 server_info->client_id = -1;
1479 BT_INFO("Added GATT server addr[%s]", server_info->addr);
1480 server_info->connection_id = event->conn_id;
1481 gatt_server_info_list = g_slist_append(gatt_server_info_list, server_info);
1482 BT_INFO("Total num of connected Remote GATT Servers [%d]", g_slist_length(gatt_server_info_list));
1484 ret = gattc_add_connection_info((bt_address_t *)&dev_addr, event->conn_id, event->server_inst);
1485 if (ret != OAL_STATUS_SUCCESS) {
1486 BT_ERR("gattc register server instance failed");
1490 __bt_add_mtu_gatt_device(address);
1495 /* GATT Server Dis connected */
1496 static void __bt_handle_gatt_server_disconnection_state(event_gatts_conn_t *event)
1498 int result = BLUETOOTH_ERROR_NONE;
1499 struct gatt_client_info_t *client_info = NULL;
1500 struct gatt_server_info_t *server_info = NULL;
1501 bluetooth_device_address_t dev_addr;
1502 GVariant *param = NULL;
1503 char address[BT_ADDRESS_STRING_SIZE];
1505 memcpy(dev_addr.addr, event->address.addr, 6);
1507 /* REPLY dbus Context to application which called BT_DISCONNECT_LE. There is status
1509 _bt_convert_addr_type_to_string(address,
1510 (unsigned char *)dev_addr.addr);
1512 if (event->status != OAL_STATUS_SUCCESS)
1513 result = BLUETOOTH_ERROR_INTERNAL;
1515 if (NULL == _bt_get_request_info_data(BT_DISCONNECT_LE, address)) {
1516 if (NULL != _bt_get_request_info_data(BT_CONNECT_LE, address)) {
1517 result = BLUETOOTH_ERROR_INTERNAL;
1519 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
1520 address, BT_ADDRESS_STRING_SIZE);
1522 BT_ERR("Failed to connect Local GATT Server Remote Client addr[%s]", address);
1526 __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE,
1527 address, BT_ADDRESS_STRING_SIZE);
1530 BT_INFO("Local GATT Server DisConnected: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1531 address, event->server_inst, event->conn_id);
1532 /* Remove Connection info */
1533 client_info = _bt_find_remote_gatt_client_info(address);
1535 BT_INFO("No need to Send Local GATT Server Disconnected event to apps, just remove remote client info");
1537 param = g_variant_new("(is)", result, address);
1538 /* Send event to application */
1539 _bt_send_event(BT_DEVICE_EVENT,
1540 BLUETOOTH_EVENT_GATT_SERVER_DISCONNECTED, /* Local device is GATT server */
1543 /* Remove server info from list */
1544 server_info = _bt_find_remote_gatt_server_info(address);
1546 gatt_server_info_list = g_slist_remove(gatt_server_info_list, server_info);
1548 BT_INFO("Can not find conn info, already removed!");
1550 /* Remove client info from List */
1551 gatt_client_info_list = g_slist_remove(gatt_client_info_list, client_info);
1552 BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
1553 g_free(client_info->addr);
1554 g_free(client_info);
1557 __bt_remove_mtu_gatt_device(address);
1561 static void __bt_handle_gatt_server_connection_state(event_gatts_conn_t *event)
1563 int result = BLUETOOTH_ERROR_NONE;
1564 struct gatt_client_info_t *conn_info = NULL;
1565 bt_service_app_info_t *info = NULL;
1566 bluetooth_device_address_t dev_addr;
1567 GVariant *param = NULL;
1570 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1572 memcpy(dev_addr.addr, event->address.addr, 6);
1575 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1576 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1579 if (info->instance_id == event->server_inst) {
1580 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1581 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
1582 (bluetooth_device_address_t*)&dev_addr, sizeof(bluetooth_device_address_t));
1588 /* Send event to BT-API */
1589 _bt_convert_addr_type_to_string(address,
1590 (unsigned char *)dev_addr.addr);
1592 BT_INFO("GATT Server Connedted: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1593 address, event->server_inst, event->conn_id);
1596 /* Check if device is already in connected list */
1597 conn_info = _bt_find_remote_gatt_client_info(address);
1600 param = g_variant_new("(is)", result, address);
1601 /* Send event to application */
1602 _bt_send_event(BT_GATT_SERVER_EVENT,
1603 BLUETOOTH_EVENT_GATT_CONNECTED,
1606 /* Save Connection info */
1607 conn_info = g_new0(struct gatt_client_info_t, 1);
1608 conn_info->addr = g_strdup(address);
1609 BT_INFO("Added GATT client addr[%s]", conn_info->addr);
1610 conn_info->connection_id = event->conn_id;
1611 conn_info->instance_id = event->server_inst;
1612 gatt_client_info_list = g_slist_append(gatt_client_info_list, conn_info);
1613 BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
1619 /* GATT Server Dis connected */
1620 static void __bt_handle_gatt_server_disconnection_state(event_gatts_conn_t *event)
1622 int result = BLUETOOTH_ERROR_NONE;
1623 struct gatt_client_info_t *conn_info = NULL;
1624 bluetooth_device_address_t dev_addr;
1625 bt_service_app_info_t *info = NULL;
1626 GVariant *param = NULL;
1629 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1631 memcpy(dev_addr.addr, event->address.addr, 6);
1633 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1634 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1637 if (info->instance_id == event->server_inst) {
1638 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1639 __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE,
1640 (bluetooth_device_address_t*)&dev_addr, sizeof(bluetooth_device_address_t));
1646 /* Send event to BT-API */
1647 _bt_convert_addr_type_to_string(address,
1648 (unsigned char *)dev_addr.addr);
1650 BT_INFO("GATT Server DisConnected: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1651 address, event->server_inst, event->conn_id);
1653 param = g_variant_new("(is)", result, address);
1654 /* Send event to application */
1655 _bt_send_event(BT_GATT_SERVER_EVENT,
1656 BLUETOOTH_EVENT_GATT_DISCONNECTED,
1659 /* Remove Connection info */
1660 conn_info = _bt_find_remote_gatt_client_info(address);
1662 BT_INFO("Remove GATT client info from List..");
1663 /* Remove info from List */
1664 gatt_client_info_list = g_slist_remove(gatt_client_info_list, conn_info);
1665 BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
1666 g_free(conn_info->addr);
1675 static void __bt_handle_gatt_server_acquire_write_requested(event_gatts_srvc_acquire_attr_t *event)
1677 GVariant *param = NULL;
1678 int result = BLUETOOTH_ERROR_NONE;
1679 struct gatt_server_req_info *req_info = NULL;
1680 bluetooth_device_address_t dev_addr;
1681 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1683 BT_INFO("GATT Server ACQUIRE Write Req Connection ID: [%d]", event->attr_trans.conn_id);
1684 BT_INFO("GATT Server ACQUIRE Write Req Transaction ID:[%d]", event->attr_trans.trans_id);
1685 BT_INFO("GATT Server ACQUIRE Write Req Attribute Handle: [%d]", event->attr_trans.attr_handle);
1687 //address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1688 memcpy(dev_addr.addr, event->address.addr, 6);
1690 req_info = g_new0(struct gatt_server_req_info, 1);
1691 req_info->request_id = event->attr_trans.trans_id;
1692 req_info->attribute_handle = event->attr_trans.attr_handle;
1693 req_info->connection_id = event->attr_trans.conn_id;
1694 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_ACQUIRE_WRITE;
1695 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1697 _bt_convert_addr_type_to_string(address,
1698 (unsigned char *)dev_addr.addr);
1700 param = g_variant_new("(iiiiiis)", result,
1701 event->attr_trans.conn_id,
1702 event->attr_trans.trans_id,
1703 event->attr_trans.attr_handle,
1704 event->mtu, event->attr_trans.offset, address);
1705 BT_INFO("GATT Server ACQUIRE Write Req Attribute remote address : [%s]", address);
1707 _bt_send_event(BT_GATT_SERVER_EVENT,
1708 BLUETOOTH_EVENT_GATT_SERVER_ACQUIRE_WRITE,
1713 static void __bt_handle_gatt_server_acquire_notify_requested(event_gatts_srvc_acquire_attr_t *event)
1715 GVariant *param = NULL;
1716 int result = BLUETOOTH_ERROR_NONE;
1717 struct gatt_server_req_info *req_info = NULL;
1718 bluetooth_device_address_t dev_addr;
1719 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1721 BT_INFO("GATT Server ACQUIRE Notify Req Connection ID: [%d]", event->attr_trans.conn_id);
1722 BT_INFO("GATT Server ACQUIRE Notify Req Transaction ID:[%d]", event->attr_trans.trans_id);
1723 BT_INFO("GATT Server ACQUIRE Notify Req Attribute Handle: [%d]", event->attr_trans.attr_handle);
1724 BT_INFO("GATT Server ACQUIRE notify Req address : [%s]", address);
1726 memcpy(dev_addr.addr, event->address.addr, 6);
1727 _bt_convert_addr_type_to_string(address,
1728 (unsigned char *)dev_addr.addr);
1729 BT_INFO("GATT Server ACQUIRE notify Req remote address : [%s]", address);
1731 req_info = g_new0(struct gatt_server_req_info, 1);
1732 req_info->request_id = event->attr_trans.trans_id;
1733 req_info->attribute_handle = event->attr_trans.attr_handle;
1734 req_info->connection_id = event->attr_trans.conn_id;
1735 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_ACQUIRE_NOTIFY;
1736 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1738 param = g_variant_new("(iiiiiis)", result,
1739 event->attr_trans.conn_id,
1740 event->attr_trans.trans_id,
1741 event->attr_trans.attr_handle,
1742 event->mtu, event->attr_trans.offset,
1745 BT_INFO("GATT Server ACQUIRE Notify Req Attribute : ");
1747 _bt_send_event(BT_GATT_SERVER_EVENT,
1748 BLUETOOTH_EVENT_GATT_SERVER_ACQUIRE_NOTIFY,
1752 static void __bt_handle_gatt_server_write_requested(event_gatts_srvc_write_attr_t *event)
1755 bluetooth_device_address_t dev_addr;
1756 GVariant *param = NULL;
1757 int result = BLUETOOTH_ERROR_NONE;
1759 gboolean is_prepare_write;
1761 GVariant *data = NULL;
1763 struct gatt_server_req_info *req_info = NULL;
1764 BT_INFO("GATT Server Write Requested");
1766 memcpy(dev_addr.addr, event->address.addr, 6);
1768 BT_INFO("GATT Server Write Req Connection ID: [%d]", event->attr_trans.conn_id);
1769 BT_INFO("GATT Server Write Req Transaction ID:[%d]", event->attr_trans.trans_id);
1770 BT_INFO("GATT Server Write Req Attribute Handle: [%d]", event->attr_trans.attr_handle);
1771 BT_INFO("GATT Server Write Req Attribute Offset: [%d]", event->attr_trans.offset);
1772 BT_INFO("GATT Server Write Req value length [%d]", event->length);
1773 BT_INFO("GATT Server Write Req needs response: [%d]", event->need_rsp);
1774 BT_INFO("GATT Server Write Req Is Prep: [%d]", event->is_prep);
1776 if (event->length <= 0) {
1777 BT_INFO("GATT Server write requested, but length of attr value is 0");
1781 need_resp = event->need_rsp;
1782 is_prepare_write = event->is_prep;
1784 write_val = g_memdup(&event->value[0], event->length);
1786 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1787 _bt_convert_addr_type_to_string(address,
1788 (unsigned char *)dev_addr.addr);
1790 BT_INFO("GATT Server Write Request from remote client [%s]", address);
1792 if (event->length > 0) {
1793 for (i = 0; i < event->length; i++)
1794 BT_DBG("Data[%d] = [0x%x]", i, event->value[i]);
1796 /* Save Write Request Info */
1797 req_info = g_new0(struct gatt_server_req_info, 1);
1798 req_info->request_id = event->attr_trans.trans_id;
1799 req_info->attribute_handle = event->attr_trans.attr_handle;
1800 req_info->connection_id = event->attr_trans.conn_id;
1801 req_info->addr = address;
1802 req_info->offset = event->attr_trans.offset;
1803 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_WRITE;
1804 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1806 data = g_variant_new_from_data(
1807 G_VARIANT_TYPE_BYTESTRING,
1812 param = g_variant_new("(iiiiiibbsn@ay)", result,
1813 event->attr_trans.conn_id,
1814 event->attr_trans.trans_id,
1815 event->attr_trans.attr_handle,
1816 event->attr_trans.offset,
1824 _bt_send_event(BT_GATT_SERVER_EVENT,
1825 BLUETOOTH_EVENT_GATT_SERVER_VALUE_CHANGED,
1831 static void __bt_handle_gatt_server_read_requested(event_gatts_srvc_read_attr_t *event)
1833 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1834 bluetooth_device_address_t dev_addr;
1835 int result = BLUETOOTH_ERROR_NONE;
1836 struct gatt_server_req_info *req_info = NULL;
1837 GVariant *param = NULL;
1839 BT_INFO("GATT Server Read Requested");
1841 memcpy(dev_addr.addr, event->address.addr, 6);
1843 BT_INFO("GATT Server-Client Connection ID: [%d]", event->attr_trans.conn_id);
1844 BT_INFO("GATT Server-Client Transaction ID: [%d]", event->attr_trans.trans_id);
1845 BT_INFO("GATT Server Attribute Handle: [%d]", event->attr_trans.attr_handle);
1846 BT_INFO("GATT Server Attribute Offset for read: [%d]", event->attr_trans.offset);
1847 BT_INFO("GATT Server Attribute is long: [%d]", event->is_long);
1849 is_long = event->is_long;
1851 /* Save Read Request Info */
1852 req_info = g_new0(struct gatt_server_req_info, 1);
1853 req_info->request_id = event->attr_trans.trans_id;
1854 req_info->attribute_handle = event->attr_trans.attr_handle;
1855 req_info->connection_id = event->attr_trans.conn_id;
1856 req_info->addr = address;
1857 req_info->offset = event->attr_trans.offset;
1858 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_READ;
1859 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1861 /* Send event to BT-API */
1862 _bt_convert_addr_type_to_string(address,
1863 (unsigned char *)dev_addr.addr);
1865 BT_INFO("GATT Server Read Request from remote client [%s]", address);
1867 param = g_variant_new("(iiiiibs)", result,
1868 event->attr_trans.conn_id,
1869 event->attr_trans.trans_id,
1870 event->attr_trans.attr_handle,
1871 event->attr_trans.offset,
1875 _bt_send_event(BT_GATT_SERVER_EVENT,
1876 BLUETOOTH_EVENT_GATT_SERVER_READ_REQUESTED,
1880 static void __bt_handle_gatt_server_indicate_confirmed(event_gatts_ind_cnfrm_t *event)
1882 bluetooth_device_address_t dev_addr;
1883 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1884 int cur_connected_clients;
1885 static int recvd = 0;
1886 gboolean completed = 0;
1887 GVariant *param = NULL;
1889 /* OAL event does provide error, so MW assumes event will never contain wrong data,
1890 incase of any issues, check with OAL */
1891 int result = BLUETOOTH_ERROR_NONE;
1893 memcpy(dev_addr.addr, event->address.addr, 6);
1894 _bt_convert_addr_type_to_string(address,
1895 (unsigned char *)dev_addr.addr);
1897 BT_INFO("Indication sent to GATT client [%s] conn_ ID [%d] transaction ID [%d] Att handle [%d]",
1898 address, event->conn_id, event->trans_id, event->attr_handle);
1901 cur_connected_clients = g_slist_length(gatt_client_info_list);
1902 BT_INFO("Number of connected clients during sending Indication [%d] & current connected count [%d]",
1903 num_indicate_clients, cur_connected_clients);
1906 if (recvd == num_indicate_clients) {
1907 BT_INFO("Gatt indication confirm event for last GATT client.. [%s]", address);
1908 completed = 1; /* Last event */
1909 recvd = 0; /* Reset */
1910 num_indicate_clients = 0;
1913 param = g_variant_new("(isib)",
1919 /* Send event to BT-API */
1920 _bt_send_event(BT_GATT_SERVER_EVENT,
1921 BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_COMPLETED,
1924 BT_INFO("Received Indication confirm for client number [%d]", recvd);
1928 /* Tizen Platform Specific */
1929 static void __bt_handle_gatt_server_notification_changed(event_gatts_notif_t *event)
1931 bluetooth_device_address_t dev_addr;
1932 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1933 GVariant *param = NULL;
1936 /* OAL event does provide error, so MW assumes event will never contain wrong data,
1937 incase of any issues, check with OAL */
1938 int result = BLUETOOTH_ERROR_NONE;
1940 memcpy(dev_addr.addr, event->address.addr, 6);
1941 _bt_convert_addr_type_to_string(address,
1942 (unsigned char *)dev_addr.addr);
1944 BT_INFO("notification_changed [%s] conn_ ID [%d] transaction ID [%d] Att handle [%d] Notify[%d]",
1945 address, event->conn_id, event->trans_id, event->attr_handle, event->notify);
1947 /* Set Notifcation status */
1948 notify = event->notify;
1950 param = g_variant_new("(isib)",
1956 /* Send event to BT-API */
1957 _bt_send_event(BT_GATT_SERVER_EVENT,
1958 BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_STATE_CHANGED,
1964 static void __bt_handle_gatt_mtu_changed_event(event_gatts_mtu_changed_t *event)
1966 int result = BLUETOOTH_ERROR_NONE;
1967 struct gatt_client_info_t *conn_info = NULL;
1968 GVariant *param = NULL;
1970 BT_INFO("GATT Server MTU changed event");
1972 conn_info = __bt_find_remote_gatt_client_info_from_conn_id(event->conn_id);
1973 if (conn_info == NULL) {
1974 BT_ERR("Cant find connection Information");
1977 BT_INFO("Got connection Info GATT client [%s] MTU Size [%d]",
1978 conn_info->addr, event->mtu_size);
1980 __bt_update_mtu_gatt_device(conn_info->addr, event->mtu_size);
1982 param = g_variant_new("(isqy)",
1988 /* Send event to BT-API */
1989 _bt_send_event(BT_GATT_SERVER_EVENT,
1990 BLUETOOTH_EVENT_GATT_ATT_MTU_CHANGED,
1994 static void __bt_gatt_event_handler(int event_type, gpointer event_data)
1996 BT_INFO("OAL event = 0x%x, \n", event_type);
1997 switch (event_type) {
1998 case OAL_EVENT_BLE_SERVER_INSTANCE_INITIALISED: {
1999 BT_INFO("OAL Event: Server Instance Registered");
2000 /* GATT Server Registered event is handled in MAIN thread context */
2001 __bt_handle_server_instance_registered((event_gatts_register_t *)event_data);
2004 case OAL_EVENT_GATTS_SERVICE_ADDED: {
2005 BT_INFO("OAL Event: GATT Service added");
2006 __bt_handle_gatt_server_service_added((event_gatts_srvc_prm_t *)event_data);
2009 case OAL_EVENT_GATTS_CHARACTERISTIC_ADDED: {
2010 BT_INFO("OAL Event: GATT characteristic added");
2011 __bt_handle_gatt_server_characteristic_added((event_gatts_srvc_charctr_t *)event_data);
2014 case OAL_EVENT_GATTS_DESCRIPTOR_ADDED: {
2015 BT_INFO("OAL Event: GATT descriptor added");
2016 __bt_handle_gatt_server_descriptor_added((event_gatts_srvc_descr_t *)event_data);
2019 case OAL_EVENT_GATTS_SERVICE_STARTED: {
2020 BT_INFO("OAL Event: GATT Service started");
2021 __bt_handle_gatt_server_service_started((event_gatts_srvc_t *)event_data);
2024 case OAL_EVENT_GATTS_SERVICE_STOPED: {
2025 BT_INFO("OAL Event: GATT Service stopped");
2026 __bt_handle_gatt_server_service_stopped((event_gatts_srvc_t *)event_data);
2029 case OAL_EVENT_GATTS_SERVICE_DELETED: {
2030 BT_INFO("OAL Event: GATT Service deleted");
2031 __bt_handle_gatt_server_service_deleted((event_gatts_srvc_t *) event_data);
2034 case OAL_EVENT_GATTS_CONNECTION_COMPLETED: {
2035 BT_INFO("OAL Event: GATT Server Connected");
2036 __bt_handle_gatt_server_connection_state((event_gatts_conn_t *)event_data);
2039 case OAL_EVENT_GATTS_DISCONNECTION_COMPLETED: {
2040 BT_INFO("OAL Event: GATT Server Disconnected");
2041 __bt_handle_gatt_server_disconnection_state((event_gatts_conn_t *)event_data);
2044 case OAL_EVENT_GATTS_REQUEST_READ: {
2045 BT_INFO("OAL Event: GATT Server Read Request");
2046 __bt_handle_gatt_server_read_requested((event_gatts_srvc_read_attr_t *)event_data);
2049 case OAL_EVENT_GATTS_REQUEST_WRITE: {
2050 BT_INFO("OAL Event: GATT Server Write Request");
2051 __bt_handle_gatt_server_write_requested((event_gatts_srvc_write_attr_t *)event_data);
2054 case OAL_EVENT_GATTS_REQUEST_ACQUIRE_WRITE: {
2055 BT_INFO("OAL Event: GATT Server Acquire Write Request");
2056 __bt_handle_gatt_server_acquire_write_requested((event_gatts_srvc_acquire_attr_t*)event_data);
2059 case OAL_EVENT_GATTS_REQUEST_ACQUIRE_NOTIFY: {
2060 BT_INFO("OAL Event: GATT ServerAcquire Notify Request");
2061 __bt_handle_gatt_server_acquire_notify_requested((event_gatts_srvc_acquire_attr_t*)event_data);
2064 case OAL_EVENT_GATTS_IND_CONFIRM: {
2065 BT_INFO("OAL Event: GATT Server Indication confirmed");
2066 __bt_handle_gatt_server_indicate_confirmed((event_gatts_ind_cnfrm_t *)event_data);
2069 case OAL_EVENT_GATTS_NOTIFICATION: { /* Tizen Platform Specific */
2070 BT_INFO("OAL Event: GATT Server DisConnected");
2071 __bt_handle_gatt_server_notification_changed((event_gatts_notif_t *)event_data);
2074 case OAL_EVENT_GATTS_MTU_CHANGED: {
2075 BT_INFO("OAL Event: GATT Server MTU changed event callback");
2076 __bt_handle_gatt_mtu_changed_event((event_gatts_mtu_changed_t *)event_data);
2079 #ifdef TIZEN_GATT_CLIENT
2080 case OAL_EVENT_GATTC_REGISTRATION: {
2081 BT_INFO("OAL Event: GATT Client instance Registered");
2082 __bt_handle_client_instance_registered((event_gattc_register_t *) event_data);
2085 case OAL_EVENT_GATTC_CONNECTION_COMPLETED: {
2086 BT_INFO("OAL Event: GATT Client Connected");
2087 __bt_handle_client_connected((event_gattc_conn_t *) event_data);
2090 case OAL_EVENT_GATTC_DISCONNECTION_COMPLETED: {
2091 BT_INFO("OAL Event: GATT Client DisConnected");
2092 __bt_handle_client_disconnected((event_gattc_conn_t *) event_data);
2095 case OAL_EVENT_GATTC_SERVICE_SEARCH_RESULT: {
2096 BT_INFO("OAL Event: GATT Client Service Search Result");
2097 __bt_handle_client_service_search_result((event_gattc_service_result_t *) event_data);
2100 case OAL_EVENT_GATTC_SERVICE_SEARCH_DONE: {
2101 BT_INFO("OAL Event: GATT Client Service Completed");
2102 __bt_handle_client_service_search_completed((event_gattc_conn_status_t *) event_data);
2105 case OAL_EVENT_GATTC_CHARAC_SERACH_RESULT: {
2106 BT_INFO("OAL Event: GATT Client Characteristic Search Result");
2107 __bt_handle_client_characteristic_search_result((event_gattc_characteristic_result_t *) event_data);
2110 case OAL_EVENT_GATTC_DESC_SERACH_RESULT: {
2111 BT_INFO("OAL Event: GATT Client Descriptor Search Result");
2112 __bt_handle_client_descriptor_search_result((event_gattc_descriptor_result_t *) event_data);
2115 case OAL_EVENT_GATTC_READ_CHARAC: {
2116 BT_INFO("OAL Event: GATT Client Characteristic Read Data");
2117 __bt_handle_client_characteristic_read_data((event_gattc_read_data *) event_data);
2120 case OAL_EVENT_GATTC_READ_DESCR: {
2121 BT_INFO("OAL Event: GATT Client Descriptor Read Data");
2122 __bt_handle_client_descriptor_read_data((event_gattc_read_data *) event_data);
2125 case OAL_EVENT_GATTC_WRITE_CHARAC: {
2126 BT_INFO("OAL Event: GATT Client Characteristic Write Data");
2127 __bt_handle_client_characteristic_write_data((event_gattc_write_data *) event_data);
2130 case OAL_EVENT_GATTC_WRITE_DESCR: {
2131 BT_INFO("OAL Event: GATT Client Descriptor Write Data");
2132 __bt_handle_client_descriptor_write_data((event_gattc_write_data *) event_data);
2135 case OAL_EVENT_DEVICE_LE_DISCONNECTED: {
2136 BT_INFO("OAL Event: LE device disconnected");
2137 __bt_hanlde_le_device_disconnection((event_dev_conn_status_t *)event_data);
2140 case OAL_EVENT_GATTC_NOTIFICATION_REGISTERED: {
2141 BT_INFO("OAL Event: GATT Client Notification Registered");
2142 __bt_handle_client_notification_registered((event_gattc_regdereg_notify_t *) event_data, TRUE);
2145 case OAL_EVENT_GATTC_NOTIFICATION_DEREGISTERED: {
2146 BT_INFO("OAL Event: GATT Client Notification Registered");
2147 __bt_handle_client_notification_registered((event_gattc_regdereg_notify_t *) event_data, FALSE);
2150 case OAL_EVENT_GATTC_NOTIFY_DATA: {
2151 BT_INFO("OAL Event: GATT Client Notification Data");
2152 __bt_handle_client_notification_data((event_gattc_notify_data *) event_data);
2155 case OAL_EVENT_GATTC_MTU_EXCHANGE_COMPLETED: {
2156 BT_INFO("OAL Event: GATT Client MTU Exchange Complete");
2157 __bt_handle_client_mtu_exchange_completed((event_gattc_mtu_configured_t *) event_data);
2167 int _bt_gatt_server_add_service(char *sender, int service_type,
2168 int num_handles, char *svc_uuid, int instance_id)
2170 BT_CHECK_PARAMETER(svc_uuid, return);
2171 BT_CHECK_PARAMETER(sender, return);
2172 int ret = OAL_STATUS_SUCCESS;
2174 oal_gatt_srvc_id_t svc_data;
2176 svc_data.is_prmry = service_type;
2177 svc_data.id.inst_id = instance_id;
2179 BT_INFO("Service UUID [%s] Num handles [%d] Instance ID [%d]", svc_uuid, num_handles, instance_id);
2180 _bt_string_to_uuid(svc_uuid, (service_uuid_t*)&svc_data.id.uuid);
2182 ret = gatts_add_service(instance_id, &svc_data, num_handles);
2183 if (ret != OAL_STATUS_SUCCESS) {
2184 BT_ERR("ret: %d", ret);
2185 return BLUETOOTH_ERROR_INTERNAL;
2188 return BLUETOOTH_ERROR_NONE;
2192 int _bt_gatt_server_add_included_service(char *sender, int instance_id,
2193 int service_handle, int included_svc_handle)
2195 BT_CHECK_PARAMETER(sender, return);
2196 int ret = OAL_STATUS_SUCCESS;
2198 ret = gatts_add_included_services(instance_id, service_handle, included_svc_handle);
2199 if (ret != OAL_STATUS_SUCCESS) {
2200 BT_ERR("ret: %d", ret);
2201 return BLUETOOTH_ERROR_INTERNAL;
2203 return BLUETOOTH_ERROR_NONE;
2206 int _bt_gatt_server_add_characteristic(char *sender, char *char_uuid,
2207 bluetooth_gatt_server_attribute_params_t *param)
2209 BT_CHECK_PARAMETER(char_uuid, return);
2210 BT_CHECK_PARAMETER(sender, return);
2211 BT_CHECK_PARAMETER(param, return);
2212 int ret = OAL_STATUS_SUCCESS;
2214 oal_uuid_t uuid = {{0} };
2216 BT_INFO("Char UUID [%s] Instance ID [%d]", char_uuid, param->instance_id);
2217 _bt_string_to_uuid(char_uuid, (service_uuid_t*)&uuid);
2219 BT_INFO("Char permission From API [0x%x]", param->permissions);
2221 ret = gatts_add_characteristics(param->instance_id, param->service_handle, &uuid,
2222 param->properties, (int)param->permissions);
2223 if (ret != OAL_STATUS_SUCCESS) {
2224 BT_ERR("ret: %d", ret);
2225 return BLUETOOTH_ERROR_INTERNAL;
2227 return BLUETOOTH_ERROR_NONE;
2230 int _bt_gatt_server_add_descriptor(char *sender, char *desc_uuid,
2231 bt_gatt_permission_t *param, int service_handle, int instance_id)
2233 BT_CHECK_PARAMETER(desc_uuid, return);
2234 BT_CHECK_PARAMETER(sender, return);
2235 BT_CHECK_PARAMETER(param, return);
2236 int ret = OAL_STATUS_SUCCESS;
2238 oal_uuid_t uuid = {{0} };
2240 BT_INFO("Descriptor UUID [%s] Instance ID [%d] Service handle [%d]",
2241 desc_uuid, service_handle, instance_id);
2243 _bt_string_to_uuid(desc_uuid, (service_uuid_t*)&uuid);
2245 BT_INFO("Descriptor permission From API [0x%x]", *param);
2246 ret = gatts_add_descriptor(instance_id, service_handle, &uuid, (int)*param);
2248 if (ret != OAL_STATUS_SUCCESS) {
2249 BT_ERR("ret: %d", ret);
2250 return BLUETOOTH_ERROR_INTERNAL;
2252 return BLUETOOTH_ERROR_NONE;
2255 int _bt_gatt_server_start_service(char *sender, int service_handle, int instance_id)
2257 BT_CHECK_PARAMETER(sender, return);
2258 int ret = OAL_STATUS_SUCCESS;
2260 ret = gatts_start_service(instance_id, service_handle, BT_GATT_TRANSPORT_LE);
2261 if (ret != OAL_STATUS_SUCCESS) {
2262 BT_ERR("ret: %d", ret);
2263 return BLUETOOTH_ERROR_INTERNAL;
2265 return BLUETOOTH_ERROR_NONE;
2268 int _bt_gatt_server_stop_service(char *sender, int service_handle, int instance_id)
2270 BT_CHECK_PARAMETER(sender, return);
2271 int ret = OAL_STATUS_SUCCESS;
2273 ret = gatts_stop_service(instance_id, service_handle);
2274 if (ret != OAL_STATUS_SUCCESS) {
2275 BT_ERR("ret: %d", ret);
2276 return BLUETOOTH_ERROR_INTERNAL;
2278 return BLUETOOTH_ERROR_NONE;
2281 int _bt_gatt_server_delete_service(char *sender, int service_handle, int instance_id)
2283 BT_CHECK_PARAMETER(sender, return);
2284 int ret = OAL_STATUS_SUCCESS;
2288 ret = gatts_delete_service(instance_id, service_handle);
2289 if (ret != OAL_STATUS_SUCCESS) {
2290 BT_ERR("ret: %d", ret);
2291 return BLUETOOTH_ERROR_INTERNAL;
2294 /* Remove the Service Handle */
2295 for (l = numapps[instance_id].service_handles; l != NULL;) {
2297 l = g_slist_next(l);
2298 if (handle && *handle == service_handle) {
2299 BT_INFO("Remove Service handle [%d]", *handle);
2300 numapps[instance_id].service_handles = g_slist_remove(numapps[instance_id].service_handles, handle);
2306 return BLUETOOTH_ERROR_NONE;
2309 int _bt_gatt_server_send_response(char *sender, bluetooth_gatt_att_data_t *data,
2310 bluetooth_gatt_server_response_params_t *param)
2312 BT_CHECK_PARAMETER(sender, return);
2313 BT_CHECK_PARAMETER(data, return);
2314 BT_CHECK_PARAMETER(param, return);
2315 struct gatt_server_req_info *req_info = NULL;
2316 int ret = OAL_STATUS_SUCCESS;
2318 oal_gatt_response_t response;
2320 BT_INFO("GATT Server Response: Req Type [%d] req_id [%d] status [%d] auth_req [%d] offset[%d] data len[%d]",
2321 param->req_type, param->request_id,
2322 param->response_status, param->auth_req,
2323 data->offset, data->length);
2325 /* Search for matching Request in List */
2326 req_info = __bt_gatt_server_find_request_info(param->request_id, param->req_type);
2328 BT_ERR("GATT Server Req Info not found for current response..return Error");
2329 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
2332 memset(&response, 0x00, sizeof(oal_gatt_response_t));
2334 response.handle = req_info->attribute_handle;
2335 response.attr_value.auth_req = param->auth_req;
2336 response.attr_value.handle = req_info->attribute_handle;
2337 response.attr_value.offset = data->offset;
2338 response.attr_value.len = data->length;
2339 memcpy(&response.attr_value.value, &data->data, data->length);
2342 ret = gatts_send_response(req_info->connection_id, param->request_id,
2343 param->response_status, &response);
2345 if (ret != OAL_STATUS_SUCCESS) {
2346 BT_ERR("ret: %d", ret);
2347 return BLUETOOTH_ERROR_INTERNAL;
2350 BT_INFO("GATT Server Response successfully sent");
2351 /* Remove GATT server request from list */
2352 gatt_server_requests = g_slist_remove(gatt_server_requests, req_info);
2353 g_free(req_info->addr);
2355 return BLUETOOTH_ERROR_NONE;
2358 int _bt_gatt_server_acquire_send_response(char *sender, bluetooth_gatt_server_acquire_response_params_t *param , void *fd_list)
2360 BT_CHECK_PARAMETER(sender, return);
2361 BT_CHECK_PARAMETER(param, return);
2362 struct gatt_server_req_info *req_info = NULL;
2363 int ret = OAL_STATUS_SUCCESS;
2366 BT_INFO("GATT acquire Server Response: Req Type [%d] req_id [%d] fd [%d] mtu[%d]",
2367 param->req_type, param->request_id,
2371 /* Search for matching Request in List */
2372 req_info = __bt_gatt_server_find_request_info(param->request_id, param->req_type);
2374 BT_ERR("GATT acquire Server Req Info not found for current response..return Error");
2375 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
2378 ret = gatt_send_response_acquire(req_info->connection_id, param->request_id, 0, param->fd, param->mtu, fd_list);
2380 if (ret != OAL_STATUS_SUCCESS) {
2381 BT_ERR("ret: %d", ret);
2382 return BLUETOOTH_ERROR_INTERNAL;
2385 BT_INFO("GATT acquire Server Response successfully sent");
2386 /* Remove GATT server request from list */
2387 gatt_server_requests = g_slist_remove(gatt_server_requests, req_info);
2388 g_free(req_info->addr);
2390 return BLUETOOTH_ERROR_NONE;
2395 int _bt_gatt_server_send_indication(char *sender, bluetooth_device_address_t *dev_addr,
2396 bluetooth_gatt_att_data_t *data,
2397 bluetooth_gatt_server_indication_params_t *param)
2399 BT_CHECK_PARAMETER(sender, return);
2400 BT_CHECK_PARAMETER(data, return);
2401 BT_CHECK_PARAMETER(param, return);
2403 gboolean all_send = FALSE;
2404 int ret = OAL_STATUS_SUCCESS;
2405 struct gatt_client_info_t *conn;
2407 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2408 _bt_convert_addr_type_to_string(address, dev_addr->addr);
2410 if (memcmp(dev_addr->addr, BDADDR_ANY, 6) == 0) {
2411 BT_INFO("GATT Server: Send Indication to all connected GATT clients..");
2414 BT_INFO("GATT Server: Send Indication to connected GATT client addr [%s]", address);
2417 /* Attempt to send Notification/Indication to all Connected GATT clients */
2419 ret = __bt_gatt_send_indication_to_all_connected_clients(data, param);
2420 if (ret != OAL_STATUS_SUCCESS) {
2421 BT_ERR("ret: %d", ret);
2423 return BLUETOOTH_ERROR_INTERNAL;
2427 conn = _bt_find_remote_gatt_client_info(address);
2429 ret = gatts_send_indication(param->instance_id, param->atrribute_handle,
2430 conn->connection_id, data->length,
2431 param->need_confirmation, (char *)(&data->data[0]));
2433 if (ret != OAL_STATUS_SUCCESS) {
2434 BT_ERR("ret: %d", ret);
2435 BT_INFO("Indication failed to send to Remote GATT Client [%s]", address);
2437 return BLUETOOTH_ERROR_INTERNAL;
2439 BT_INFO("Indication sent to Remote GATT Client [%s] wait for Notification completed event from OAL", address);
2441 num_indicate_clients = 1;
2442 return BLUETOOTH_ERROR_NONE;
2444 BT_ERR("Remote GATT client [%s] is not connected..Cant send Indication!!", address);
2446 return BLUETOOTH_ERROR_NOT_CONNECTED;
2449 return BLUETOOTH_ERROR_NONE;
2452 int _bt_gatt_server_update_attribute_value(char *sender, int instance_id,
2453 bluetooth_gatt_server_update_value_t *param)
2455 BT_CHECK_PARAMETER(sender, return);
2456 BT_CHECK_PARAMETER(param, return);
2457 int ret = OAL_STATUS_SUCCESS;
2459 oal_gatt_value_t value;
2460 BT_INFO("GATT Server Update value: Instance ID [%d] attr handle [%d] Value len [%d]",
2461 instance_id, param->attribute_handle, param->length);
2464 memset(&value, 0x00, sizeof(oal_gatt_value_t));
2466 value.handle = param->attribute_handle;
2467 value.len = param->length;
2468 memcpy(&value.value, ¶m->data.data, param->length);
2470 ret = gatts_update_att_value(instance_id, &value);
2472 if (ret != OAL_STATUS_SUCCESS) {
2473 BT_ERR("ret: %d", ret);
2474 return BLUETOOTH_ERROR_INTERNAL;
2477 BT_INFO("GATT Server Update Attribute Value successfully");
2478 return BLUETOOTH_ERROR_NONE;
2481 int _bt_request_att_mtu(bluetooth_device_address_t *device_address,
2484 struct gatt_server_info_t *conn_info = NULL;
2486 int ret = OAL_STATUS_SUCCESS;
2488 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
2489 _bt_convert_addr_type_to_string(addr, device_address->addr);
2491 /* Check if remote GATT Server is connected or not */
2492 conn_info = _bt_find_remote_gatt_server_info(addr);
2494 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
2495 conn_info->addr, conn_info->connection_id);
2497 BT_ERR("GATT Server is not yet connected..");
2499 return BLUETOOTH_ERROR_NOT_CONNECTED;
2502 ret = gattc_configure_mtu(conn_info->connection_id, mtu);
2503 if (ret != OAL_STATUS_SUCCESS) {
2504 BT_ERR("ret: %d", ret);
2506 return BLUETOOTH_ERROR_INTERNAL;
2510 return BLUETOOTH_ERROR_NONE;
2513 int _bt_get_att_mtu(bluetooth_device_address_t *address,
2516 BT_CHECK_PARAMETER(address, return);
2517 BT_CHECK_PARAMETER(mtu, return);
2518 struct gatt_client_info_t *conn_info = NULL;
2519 char addr[BT_ADDRESS_STRING_SIZE] = { 0 };
2520 int ret = OAL_STATUS_SUCCESS;
2523 _bt_convert_addr_type_to_string(addr, address->addr);
2525 BT_INFO("Get current MTU size for the remote client:DevAddress:[%s]", addr);
2527 conn_info = _bt_find_remote_gatt_client_info(addr);
2529 BT_INFO("GATT Client [%s] is connected, conn Id [%d] Instance ID [%d]",
2530 conn_info->addr, conn_info->connection_id, conn_info->instance_id);
2532 BT_ERR("GATT Client [%s] is not yet connected..", addr);
2533 return BLUETOOTH_ERROR_NOT_CONNECTED;
2536 ret = gatts_get_att_mtu(conn_info->connection_id, &stack_mtu);
2537 if (ret != OAL_STATUS_SUCCESS) {
2538 BT_ERR("ret: %d", ret);
2539 return BLUETOOTH_ERROR_INTERNAL;
2542 BT_INFO("ATT MTU received from OAL [%d]", stack_mtu);
2543 *mtu = (unsigned int)stack_mtu;
2546 BT_ERR("MTU value is zero, GATT Client [%s] is not yet connected..", addr);
2547 return BLUETOOTH_ERROR_NOT_CONNECTED;
2550 return BLUETOOTH_ERROR_NONE;
2553 #ifdef TIZEN_GATT_CLIENT
2554 /* GATT Client utility static functions */
2555 static bt_gatt_service_info_list_t * __bt_get_service_info_list(int conn_id)
2558 bt_gatt_service_info_list_t *info = NULL;
2560 for (l = list_gatt_info; l != NULL; l = g_slist_next(l)) {
2561 info = (bt_gatt_service_info_list_t*)l->data;
2565 if (info->conn_id == conn_id)
2572 static bt_gatt_service_info_t* __bt_find_matching_service(
2573 bt_gatt_service_info_list_t *svc_list, oal_gatt_srvc_id_t *svc)
2576 bt_gatt_service_info_t *info = NULL;
2578 for (l = svc_list->services; l != NULL; l = g_slist_next(l)) {
2579 info = (bt_gatt_service_info_t*)l->data;
2583 /* Match UUID and instance ID */
2584 if (!memcmp(&svc->id.uuid.uuid, &info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
2585 && (svc->id.inst_id == info->inst_id)) {
2592 static bt_gatt_char_info_t* __bt_find_matching_charc(
2593 bt_gatt_service_info_t *svc_info, oal_gatt_id_t *charc)
2596 bt_gatt_char_info_t *info = NULL;
2598 for (l = svc_info->chars; l != NULL; l = g_slist_next(l)) {
2599 info = (bt_gatt_char_info_t*)l->data;
2603 /* Match UUID and instance ID */
2604 if (!memcmp(&charc->uuid.uuid, &info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
2605 && (charc->inst_id == info->inst_id)) {
2612 static bt_gatt_descriptor_info_t* __bt_find_matching_desc(
2613 bt_gatt_char_info_t *char_info, oal_gatt_id_t *desc)
2616 bt_gatt_descriptor_info_t *info = NULL;
2618 for (l = char_info->descs; l != NULL; l = g_slist_next(l)) {
2619 info = (bt_gatt_descriptor_info_t*)l->data;
2623 /* Match UUID and instance ID */
2624 if (!memcmp(&desc->uuid, &info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
2625 && (desc->inst_id == info->inst_id)) {
2633 static struct gatt_server_info_t *__bt_find_remote_gatt_server_info_from_conn_id(int conn_id)
2636 struct gatt_server_info_t *info = NULL;
2638 for (l = gatt_server_info_list; l != NULL; l = g_slist_next(l)) {
2639 info = (struct gatt_server_info_t*)l->data;
2643 if (info->connection_id == conn_id) {
2644 BT_INFO("Remote GATT server found addr[%s]", info->addr);
2651 static bt_gatt_service_info_t* __bt_find_removed_service(bt_gatt_service_info_list_t *svc_list)
2654 bt_gatt_service_info_t *info = NULL;
2656 for (l = svc_list->services; l != NULL; l = g_slist_next(l)) {
2657 info = (bt_gatt_service_info_t*)l->data;
2661 /* Service is marked a removed */
2662 if (info->is_removed == 1)
2668 static void __bt_remove_service_info_from_list(bt_gatt_service_info_t *svc_info)
2674 bt_gatt_char_info_t *charc = NULL;
2675 bt_gatt_included_service_info_t *incl = NULL;
2676 bt_gatt_descriptor_info_t *desc = NULL;
2678 /* Remove all Characteristic and Descriptors within characteristic */
2679 for (l = svc_info->chars; l != NULL;) {
2680 charc = (bt_gatt_char_info_t*)l->data;
2681 l = g_slist_next(l); /* Incase if l is removed, saving next to l */
2686 /* Inside Characteristic */
2687 for (l1 = charc->descs; l1 != NULL;) {
2689 desc = (bt_gatt_descriptor_info_t*)l1->data;
2690 l1 = g_slist_next(l1);
2695 /* Remove Descriptor */
2696 charc->descs = g_slist_remove(charc->descs, desc);
2699 /* Remove Characteristic */
2700 svc_info->chars = g_slist_remove(svc_info->chars, charc);
2704 /* Remove all Included Services */
2705 for (l2 = svc_info->included_svcs; l2 != NULL;) {
2706 incl = (bt_gatt_included_service_info_t*)l2->data;
2707 l2 = g_slist_next(l2); /* Incase if l is removed, saving next to l */
2712 /* Remove included service */
2713 svc_info->included_svcs = g_slist_remove(svc_info->included_svcs, incl);
2720 static void __bt_build_service_browse_info(int conn_id,
2721 bt_services_browse_info_t* info)
2724 bt_gatt_service_info_list_t *svc_info_list;
2725 bt_gatt_service_info_t *svc_info;
2727 service_uuid_t uuid;
2728 struct gatt_server_info_t *conn_info = NULL;
2730 char uuid_string[BLUETOOTH_UUID_STRING_MAX];
2732 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
2737 _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
2739 svc_info_list = __bt_get_service_info_list(conn_id);
2744 info->count = g_slist_length(svc_info_list->services);
2745 BT_DBG("Total services present in the svc info list for this conn id [%d] is [%d]",
2746 conn_id, info->count);
2748 for (l = svc_info_list->services; l != NULL; l = g_slist_next(l)) {
2749 svc_info = (bt_gatt_service_info_t*)l->data;
2750 if (svc_info == NULL)
2753 memcpy(&uuid.uuid, &svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2754 _bt_uuid_to_string(&uuid, uuid_string);
2756 BT_INFO("[%d] %s [%s]", count, uuid_string, _bt_convert_uuid_to_string(uuid_string));
2758 /* Fill UUID of service */
2759 g_strlcpy(info->uuids[count], uuid_string,
2760 BLUETOOTH_UUID_STRING_MAX);
2762 /* Fill instance ID of service */
2763 info->inst_id[count] = svc_info->inst_id;
2765 /* Fill primary service or not info */
2766 info->primary[count] = svc_info->is_primary;
2768 /* Increment count of services browsed */
2773 static void __bt_build_char_browse_info(int conn_id,
2774 bt_gatt_service_info_t *svc_info,
2775 bt_char_browse_info_t* info)
2778 bt_gatt_char_info_t *char_info;
2779 service_uuid_t uuid;
2781 struct gatt_server_info_t *conn_info = NULL;
2783 char uuid_string[BLUETOOTH_UUID_STRING_MAX];
2787 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
2791 /* Fill default data, this will be required even in case of failure */
2792 _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
2793 memcpy(&info->svc_uuid, svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2794 info->svc_inst_id = svc_info->inst_id;
2796 if (!svc_info->chars) {
2797 BT_ERR("No Chars browsed for address [%s]", conn_info->addr);
2801 info->count = g_slist_length(svc_info->chars);
2802 BT_INFO("Total count of Characteristics [%d]", info->count);
2804 for (l = svc_info->chars; l != NULL; l = g_slist_next(l)) {
2805 char_info = (bt_gatt_char_info_t*)l->data;
2806 if (char_info == NULL)
2809 memcpy(&uuid.uuid, &char_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2810 _bt_uuid_to_string(&uuid, uuid_string);
2812 /* Fill UUID of characteristic */
2813 g_strlcpy(info->uuids[count], uuid_string,
2814 BLUETOOTH_UUID_STRING_MAX);
2816 /* Fill instance ID of characteristic */
2817 info->inst_id[count] = char_info->inst_id;
2819 /* Fill property of characteristic */
2820 info->props[count] = char_info->props;
2822 /* Increment count of services browsed */
2825 BT_INFO("Total characteristics browsed [%d]", count);
2828 static void __bt_build_descriptor_browse_info(int conn_id,
2829 bt_gatt_service_info_t *svc_info,
2830 bt_gatt_char_info_t *char_info,
2831 bt_descriptor_browse_info_t* info)
2834 bt_gatt_descriptor_info_t *desc_info;
2836 service_uuid_t uuid;
2837 struct gatt_server_info_t *conn_info = NULL;
2839 char uuid_string[BLUETOOTH_UUID_STRING_MAX];
2841 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
2843 /* Fill default data, this will be required even in case of failure */
2844 _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
2845 memcpy(&info->svc_uuid, svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2846 info->svc_inst_id = svc_info->inst_id;
2847 memcpy(&info->char_uuid, char_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2848 info->char_inst_id = char_info->inst_id;
2850 /* Fill property of the parent characteristic of this descriptor */
2851 info->char_props_map = char_info->props;
2853 info->count = g_slist_length(char_info->descs);
2854 BT_INFO("Total count of Descriptors [%d]", info->count);
2856 if (!char_info->descs) {
2857 BT_ERR("No Descriptors browsed for address [%s]", conn_info->addr);
2861 for (l = char_info->descs; l != NULL; l = g_slist_next(l)) {
2862 desc_info = (bt_gatt_descriptor_info_t*)l->data;
2863 if (desc_info == NULL)
2866 memcpy(&uuid.uuid, &desc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2867 _bt_uuid_to_string(&uuid, uuid_string);
2869 /* Fill UUID of Descriptor */
2870 g_strlcpy(info->uuids[count], uuid_string,
2871 BLUETOOTH_UUID_STRING_MAX);
2873 /* Fill instance ID of Descriptor */
2874 info->inst_id[count] = desc_info->inst_id;
2877 /* Increment count of Descriptor browsed */
2881 BT_INFO("Total descriptors browsed [%d]", count);
2885 static void __bt_cleanup_remote_services(struct gatt_server_info_t *conn_info)
2892 bt_gatt_service_info_list_t * svc_info_list = NULL;
2893 bt_gatt_service_info_t *svc = NULL;
2894 bt_gatt_char_info_t *chr = NULL;
2895 bt_gatt_descriptor_info_t *desc = NULL;
2896 bt_gatt_included_service_info_t *incl_svc = NULL;
2898 svc_info_list = __bt_get_service_info_list(conn_info->connection_id);
2899 if (!svc_info_list) {
2900 BT_INFO("Could not find Svc Info list for the connection ID [%d]",
2901 conn_info->connection_id);
2905 BT_INFO("Start Cleanup of all services. Num Services [%d]", g_slist_length(svc_info_list->services));
2906 for (l = svc_info_list->services; l;) {
2907 svc = (bt_gatt_service_info_t*)l->data;
2908 l = g_slist_next(l);
2912 BT_DBG("Service info Is Prim[%d] Inst ID [%d]",
2913 svc->is_primary, svc->inst_id);
2914 /* Delete all chars and its descriptors */
2915 for (ll = svc->chars; ll;) {
2916 chr = (bt_gatt_char_info_t*)ll->data;
2917 ll = g_slist_next(ll);
2921 for (lll = chr->descs; lll;) {
2922 desc = (bt_gatt_descriptor_info_t *)lll->data;
2923 lll = g_slist_next(lll);
2926 chr->descs = g_slist_remove(chr->descs, desc);
2929 svc->chars = g_slist_remove(svc->chars, chr);
2933 /* Delete all included services */
2934 for (llll = svc->included_svcs; llll;) {
2935 incl_svc = (bt_gatt_included_service_info_t*)llll->data;
2936 llll = g_slist_next(llll);
2937 if (incl_svc == NULL)
2940 svc->included_svcs = g_slist_remove(svc->included_svcs, incl_svc);
2943 svc_info_list->services = g_slist_remove(svc_info_list->services, svc);
2947 list_gatt_info = g_slist_remove(list_gatt_info, svc_info_list);
2948 g_free(svc_info_list);
2951 int _bt_register_gatt_client_instance(const char *sender,
2952 bluetooth_device_address_t *address)
2954 int ret = OAL_STATUS_SUCCESS;
2955 char *uuid_string = NULL;
2960 /* App should ensure that it should not send */
2961 BT_INFO("###Check on which instance GATT Client instance can be initialized....");
2962 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
2963 if (numapps[k].is_initialized == 1) {
2964 BT_INFO("Instance ID [%d] is already in use..Check next slot",
2965 numapps[k].instance_id);
2968 BT_INFO("Time to register GATT client instancer..UUID to be used is [%s] slot [%d]",
2969 uuid_list[slot-1], slot);
2975 BT_ERR("No Slot if free for GATT Client registration..");
2976 return BLUETOOTH_ERROR_REGISTRATION_FAILED;
2979 uuid_string = g_malloc0(BT_UUID_STRING_MAX);
2980 _bt_string_to_uuid(uuid_list[slot-1], (service_uuid_t*)&uuid);
2981 g_strlcpy(uuid_string, uuid_list[slot-1], BT_UUID_STRING_MAX);
2982 BT_INFO("Copied UUID string [%s]", uuid_string);
2984 /* Register GATT Client */
2985 ret = gattc_register(&uuid);
2986 if (ret != OAL_STATUS_SUCCESS) {
2987 BT_ERR("ret: %d", ret);
2988 g_free(uuid_string);
2989 return BLUETOOTH_ERROR_INTERNAL;
2992 BT_INFO("GATT Client registration call successfully accepted by OAL..wait for Instance Initialized event from OAL..");
2994 /* Return & wait for GATT Client Instance Initialization event */
2995 memset(numapps[slot].sender, 0x00, sizeof(numapps[slot].sender));
2996 memset(numapps[slot].uuid, 0x00, sizeof(numapps[slot].uuid));
2998 g_strlcpy(numapps[slot].sender, sender, sizeof(numapps[slot].sender));
2999 g_strlcpy(numapps[slot].uuid, uuid_string, sizeof(numapps[slot].uuid));
3001 /* Address is saved here. When event comes, sender + address are matched for replying pending
3002 request. It is impossible for same sender to have requests with two same addresses */
3003 memcpy(&numapps[slot].address.addr, address->addr, sizeof(bluetooth_device_address_t));
3005 BT_INFO("Slot [%d] occupied", slot);
3006 numapps[slot].is_initialized = TRUE; /* Set initialization to true here itself */
3008 g_free(uuid_string);
3009 return BLUETOOTH_ERROR_NONE;
3015 /* GATT client events */
3016 static void __bt_handle_client_instance_registered(event_gattc_register_t *data)
3018 bt_service_app_info_t *info = NULL;
3020 char *uuid_string = g_malloc0(BT_UUID_STRING_MAX);
3022 _bt_uuid_to_string(&(data->client_uuid), uuid_string);
3023 BT_INFO("CLient ID is Initialized [%d] UUID initialized [%s]", data->client_if, uuid_string);
3025 /* Platform GATT client framwork does not use Default GATT client instance
3026 This GATT client instance is never deregistred in the lifetime of bt-service */
3027 BT_INFO("Default UUID [%s] current registered uuid [%s]",
3028 DEFAULT_GATT_CLIENT_UUID, uuid_string);
3029 if (g_strcmp0(uuid_string, DEFAULT_GATT_CLIENT_UUID) == 0) {
3030 BT_INFO("Default client Instance Registered [%s] Client instance [%d]",
3031 uuid_string, data->client_if);
3032 gatt_default_client = data->client_if;
3033 g_free(uuid_string);
3037 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
3040 if (g_strcmp0(info->uuid, uuid_string) == 0) {
3041 BT_INFO("Found GATT client.. UUID [%s], sender [%s]", info->uuid, info->sender);
3042 BT_INFO("Slot [%d] occupied", k);
3043 info->is_initialized = TRUE;
3044 info->client_id = data->client_if;
3045 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE, BT_GATT_CLIENT_REGISTER,
3046 (void*)info, sizeof(bt_service_app_info_t));
3050 g_free(uuid_string);
3053 static void __bt_handle_client_connected(event_gattc_conn_t *event_data)
3055 int result = BLUETOOTH_ERROR_NONE;
3056 struct gatt_server_info_t *conn_info = NULL;
3057 struct gatt_out_conn_info_t *out_conn_info = NULL;
3059 GVariant *param = NULL;
3061 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
3062 _bt_convert_addr_type_to_string(address,
3063 (unsigned char *)event_data->address.addr);
3065 if (event_data->status != OAL_STATUS_SUCCESS)
3066 result = BLUETOOTH_ERROR_INTERNAL;
3068 /* DBUS Return fo BT_CONNECT_LE for all the apps */
3069 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE, address,
3070 BT_ADDRESS_STRING_SIZE);
3072 BT_INFO("Local GATT Client Connected: Remote addr[%s] Client Interface [%d] Connection ID [%d] status[%d]",
3073 address, event_data->client_if, event_data->conn_id, event_data->status);
3075 if (result == BLUETOOTH_ERROR_NONE) {
3076 /* Check if device is already in connected list */
3077 conn_info = _bt_find_remote_gatt_server_info(address);
3080 /* Send event to BT-API */
3081 param = g_variant_new("(is)", result, address);
3082 _bt_send_event(BT_DEVICE_EVENT,
3083 BLUETOOTH_EVENT_GATT_CLIENT_CONNECTED, /* Local device is GATT client */
3086 /* Save Connection info */
3087 conn_info = g_new0(struct gatt_server_info_t, 1);
3088 conn_info->addr = g_strdup(address);
3089 conn_info->client_id = event_data->client_if;
3090 BT_INFO("Added GATT server addr[%s]", conn_info->addr);
3091 conn_info->connection_id = event_data->conn_id;
3092 gatt_server_info_list = g_slist_append(gatt_server_info_list, conn_info);
3093 BT_INFO("Total num of connected Remote GATT server devices [%d]",
3094 g_slist_length(gatt_server_info_list));
3097 BT_INFO("Do a Internal refresh");
3098 if (OAL_STATUS_SUCCESS != gattc_refresh(conn_info->client_id, &event_data->address))
3099 BT_ERR("GATT database refresh failed!!");
3101 BT_INFO("GATT database refresh Success!!");
3104 BT_ERR("Local GATT Client connected event for addr[%s], but device is in connected list already", address);
3106 __bt_add_mtu_gatt_device(address);
3108 BT_ERR("GATT Client Connection failed!!");
3110 /* If outgoing connection Info is present, then remove it */
3111 out_conn_info = __bt_find_gatt_outgoing_conn_info(address);
3112 if (out_conn_info) {
3113 BT_ERR("Outgoing Client connect request was sent");
3114 outgoing_gatt_conn_list = g_slist_remove(outgoing_gatt_conn_list, out_conn_info);
3115 g_free(out_conn_info->addr);
3116 g_free(out_conn_info);
3121 static void __bt_handle_client_disconnected(event_gattc_conn_t *event_data)
3123 int result = BLUETOOTH_ERROR_NONE;
3125 struct gatt_server_info_t *conn_info = NULL;
3126 struct gatt_out_conn_info_t *out_conn_info = NULL;
3128 GVariant *param = NULL;
3130 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
3131 _bt_convert_addr_type_to_string(address,
3132 (unsigned char *)event_data->address.addr);
3134 if (event_data->status != OAL_STATUS_SUCCESS)
3135 result = BLUETOOTH_ERROR_INTERNAL;
3137 if (NULL == _bt_get_request_info_data(BT_DISCONNECT_LE, address)) {
3138 if (NULL != _bt_get_request_info_data(BT_CONNECT_LE, address)) {
3139 result = BLUETOOTH_ERROR_INTERNAL;
3140 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
3141 address, BT_ADDRESS_STRING_SIZE);
3142 BT_ERR("Failed to connect Local GATT Remote addr[%s]", address);
3147 /* DBUS Return for BT_DISCONNECT_LE for all the apps */
3148 __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE, address,
3149 BT_ADDRESS_STRING_SIZE);
3151 BT_INFO("Local GATT Client DisConnected: Remote addr[%s] Client Interface [%d] Connection ID [%d] status [%d]",
3152 address, event_data->client_if, event_data->conn_id, event_data->status);
3154 /* Remove Connection info */
3155 conn_info = _bt_find_remote_gatt_server_info(address);
3158 param = g_variant_new("(is)", result, address);
3159 /* Send event to application */
3160 _bt_send_event(BT_DEVICE_EVENT,
3161 BLUETOOTH_EVENT_GATT_CLIENT_DISCONNECTED,
3164 BT_INFO("Remove GATT server info from List..");
3165 /* Remove all services from info list_gatt_info */
3166 __bt_cleanup_remote_services(conn_info);
3168 /* Remove info from List */
3169 gatt_server_info_list = g_slist_remove(gatt_server_info_list, conn_info);
3170 BT_INFO("Total num of connected GATT servers [%d]", g_slist_length(gatt_server_info_list));
3171 g_free(conn_info->addr);
3174 BT_INFO("Can not find conn info, already removed!");
3176 __bt_remove_mtu_gatt_device(address);
3178 /* If outgoing connection Info is present, then remove it */
3179 out_conn_info = __bt_find_gatt_outgoing_conn_info(address);
3180 if (out_conn_info) {
3181 BT_ERR("Client Disconnected event, but outgoing connect request was sent");
3182 outgoing_gatt_conn_list = g_slist_remove(outgoing_gatt_conn_list, out_conn_info);
3183 g_free(out_conn_info->addr);
3184 g_free(out_conn_info);
3190 static void __bt_handle_client_service_search_result(
3191 event_gattc_service_result_t *event_data)
3193 BT_INFO("received the gatt service search result");
3194 /* Pre: status is never fail from OAL */
3196 /* Find service list from address */
3197 bt_gatt_service_info_list_t *svc_info_list;
3198 bt_gatt_service_info_t *svc_info;
3199 BT_INFO("Search Result: status [%d] conn_id [%d]",
3200 event_data->conn_status.status,
3201 event_data->conn_status.conn_id);
3203 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3204 if (!svc_info_list) {
3205 BT_INFO("Service info list not present for this connection ID, means first time browse");
3206 /* Means for this conn_id, no services are ever browsed, first time,
3207 create service info list for this conn_id */
3208 svc_info_list = g_malloc0(sizeof(bt_gatt_service_info_list_t));
3209 svc_info_list->conn_id = event_data->conn_status.conn_id;
3210 list_gatt_info = g_slist_append(list_gatt_info, svc_info_list);
3212 BT_INFO("Service info list Already present for this connection ID, means not first time browse for this conn ID ");
3215 /* send list and current service's uuid and instance id to find it */
3216 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3219 /* If not found, check if service changed, if yes, means this is a new service added
3220 in remote GATT device, update uuid info in svc info list structure, to be used when
3221 search is completed */
3223 BT_INFO("Service Not found in svc info list for this connection ID");
3224 if (svc_info_list->info.is_changed) {
3225 BT_INFO("Service Changed indication already found for this connection ID");
3226 memcpy(svc_info_list->info.uuid, event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3228 /* Create and add new service in service list */
3229 svc_info = g_malloc0(sizeof(bt_gatt_service_info_t));
3230 memcpy(svc_info->uuid, event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3231 svc_info->inst_id = event_data->srvc_id.id.inst_id;
3232 svc_info->is_primary = event_data->srvc_id.is_prmry;
3233 svc_info_list->services = g_slist_append(svc_info_list->services, svc_info);
3234 BT_INFO("Service created and added in Svc info listf or this connection ID");
3236 BT_INFO("Service Already found to be present inside the svc info list for this connection ID");
3237 /* If returned matching service info, then just update service_rmeoved value inside it to 0 */
3238 svc_info->is_removed = 0;
3242 static void __bt_handle_client_service_search_completed(
3243 event_gattc_conn_status_t *event_data)
3246 struct gatt_server_info_t *conn_info = NULL;
3247 bt_gatt_service_info_list_t *svc_info_list;
3248 bt_gatt_service_info_t *svc_info;
3249 bt_services_browse_info_t browse_info;
3250 unsigned char uuid_empty[BLUETOOTH_UUID_HEX_MAX_LEN];
3252 memset(&uuid_empty, 0x00, BLUETOOTH_UUID_HEX_MAX_LEN);
3253 memset(&browse_info, 0x00, sizeof(bt_services_browse_info_t));
3254 BT_INFO("Primary Services browsing completed status[%d] conn ID [%d]",
3255 event_data->status, event_data->conn_id);
3257 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(event_data->conn_id);
3259 svc_info_list = __bt_get_service_info_list(event_data->conn_id);
3260 if (!svc_info_list) {
3261 BT_ERR("No services browsed ever for addr [%s]", conn_info->addr);
3263 /* Just build response and return ERROR */
3264 __bt_build_service_browse_info(event_data->conn_id, &browse_info);
3266 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_INTERNAL,
3267 BT_GATT_GET_PRIMARY_SERVICES, &browse_info,
3268 sizeof(bt_services_browse_info_t));
3272 /* If fail, then send event with error */
3273 if (event_data->status != OAL_STATUS_SUCCESS) {
3274 /* Just build response and return ERROR */
3275 __bt_build_service_browse_info(event_data->conn_id, &browse_info);
3277 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_INTERNAL,
3278 BT_GATT_GET_PRIMARY_SERVICES, &browse_info,
3279 sizeof(bt_services_browse_info_t));
3283 /* If success, then find service info list from address */
3285 /* If svc_changed == 1 and uuid valid, means a new service is added*/
3286 if (svc_info_list->info.is_changed && !memcmp(uuid_empty, svc_info_list->info.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)) {
3287 /* TODO: Send event -Service added with instance ID and UUID of newly added service */
3288 BT_INFO("new service added");
3290 BT_INFO("TODO new service added");
3293 /* If svc_changed == 1 and uuid invalid, then a service is removed */
3294 if (svc_info_list->info.is_changed && memcmp(uuid_empty, svc_info_list->info.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)) {
3295 /* Scan through the service info list to find service with is_removed = 1*/
3296 svc_info = __bt_find_removed_service(svc_info_list);
3298 /* TODO Send event - Service removed with instance ID and UUID of just rmeoved service */
3300 /* Remove that service info from service info list */
3301 svc_info_list->services = g_slist_remove(svc_info_list->services, svc_info);
3303 /* Delete that service completely from svc_info list*/
3304 __bt_remove_service_info_from_list(svc_info);
3308 /* Reset svc_changed = 0, and reset UUID = all 0's */
3309 svc_info_list->info.is_changed = 0;
3310 memset(&svc_info_list->info.uuid, 0x00, BLUETOOTH_UUID_HEX_MAX_LEN);
3312 /* Build Reply and send to service browse primary services request of pending apps */
3313 __bt_build_service_browse_info(event_data->conn_id, &browse_info);
3315 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE,
3316 BT_GATT_GET_PRIMARY_SERVICES, &browse_info,
3317 sizeof(bt_services_browse_info_t));
3321 static void __bt_handle_client_characteristic_search_result(
3322 event_gattc_characteristic_result_t *event_data)
3324 bt_gatt_service_info_list_t *svc_info_list;
3325 bt_gatt_service_info_t *svc_info;
3326 bt_gatt_char_info_t *char_info;
3327 bt_char_browse_info_t browse_info;
3329 BT_INFO("Characteristic search result status [%d]",
3330 event_data->conn_status.status);
3332 memset(&browse_info, 0x00, sizeof(bt_char_browse_info_t));
3335 if (event_data->conn_status.status == OAL_STATUS_SUCCESS) {
3336 /* Find service info list from address */
3337 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3338 if (svc_info_list == NULL) {
3339 BT_ERR("svc_info_list is NULL");
3344 /* Find matching service info from svc info list */
3345 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3346 if (svc_info == NULL) {
3347 BT_ERR("svc_info is NULL");
3351 /* Find Matching char from service info in event */
3352 char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
3353 if (char_info == NULL)
3354 BT_ERR("char_info is NULL");
3356 /* If not found, then add new characteristic and return */
3358 BT_INFO(" add new characteristic");
3359 char_info = g_malloc0(sizeof(bt_gatt_char_info_t));
3360 memcpy(char_info->uuid, event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3362 char_info->inst_id = event_data->char_id.inst_id;
3363 char_info->props = event_data->char_prop;
3364 svc_info->chars = g_slist_append(svc_info->chars, char_info);
3366 /* If found, then return */
3367 BT_INFO("update char property as Characteristic browsed is already present");
3368 char_info->props |= event_data->char_prop;
3371 /* If Not success: Means Charc browse is completed */
3372 /* Find char list from service in event */
3373 /* Find service list from address */
3374 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3376 /* Find service info from service in event */
3377 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3379 /* Build char list from service in event */
3380 __bt_build_char_browse_info(event_data->conn_status.conn_id,
3381 svc_info, &browse_info);
3383 /* Create response and return by sending event*/
3384 /* Build Reply and send to service browse All Included services request of pending apps */
3385 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE,
3386 BT_GATT_GET_SERVICE_PROPERTIES,
3388 sizeof(bt_char_browse_info_t));
3392 static void __bt_handle_client_descriptor_search_result(
3393 event_gattc_descriptor_result_t *event_data)
3395 bt_gatt_service_info_list_t *svc_info_list;
3396 bt_gatt_service_info_t *svc_info;
3397 bt_gatt_char_info_t *char_info;
3398 bt_gatt_descriptor_info_t *desc_info;
3399 bt_descriptor_browse_info_t browse_info;
3401 BT_INFO("descriptor search result status [%d]", event_data->conn_status.status);
3403 memset(&browse_info, 0x00, sizeof(bt_descriptor_browse_info_t));
3406 if (event_data->conn_status.status == OAL_STATUS_SUCCESS) {
3407 /* Find service list from address */
3408 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3409 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3410 char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
3411 desc_info = __bt_find_matching_desc(char_info, &event_data->descr_id);
3413 /* If not found, add new descriptor and return */
3415 desc_info = g_malloc0(sizeof(bt_gatt_descriptor_info_t));
3416 memcpy(desc_info->uuid, event_data->descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3418 desc_info->inst_id = event_data->descr_id.inst_id;
3419 char_info->descs = g_slist_append(char_info->descs, desc_info);
3422 /* If found, then return */
3423 BT_INFO("Characteristic browsed is already presesnt");
3426 /* If Not success */
3427 /* Find service list from address */
3428 /* Find included service list from service in event */
3429 /* Create response and return by sending event*/
3430 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3432 /* Find service info from service in event */
3433 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3435 /* Find char info from char in event */
3436 char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
3438 /* Build descriptor list from char in event */
3439 __bt_build_descriptor_browse_info(event_data->conn_status.conn_id,
3440 svc_info, char_info, &browse_info);
3443 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE,
3444 BT_GATT_GET_CHARACTERISTIC_PROPERTIES,
3446 sizeof(bt_descriptor_browse_info_t));
3450 static void __bt_handle_client_characteristic_read_data(
3451 event_gattc_read_data *event_data)
3453 int result = BLUETOOTH_ERROR_NONE;
3454 struct gatt_server_info_t *conn_info = NULL;
3455 bluetooth_gatt_client_char_prop_info_t read_info;
3457 /* Read Information data structures */
3458 GVariant *param = NULL;
3459 GVariant *data = NULL;
3460 GVariant *data_svc_uuid = NULL;
3461 GVariant *data_char_uuid = NULL;
3462 char *read_val = NULL;
3463 char *svc_uuid = NULL;
3464 char *char_uuid = NULL;
3467 //memset(&read_info, 0x00, sizeof(bt_gatt_handle_property_t));
3468 memset(&read_info, 0x00, sizeof(bluetooth_gatt_client_char_prop_info_t));
3470 /* Extract Address from conn_id of event data */
3471 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3472 event_data->uuid_status.conn_status.conn_id);
3474 BT_INFO("Characteristic Read result from addr [%s] status [%d]",
3475 conn_info->addr, event_data->uuid_status.conn_status.status);
3477 /* Fill char in buffer */
3478 memcpy(&read_info.characteristic.uuid,
3479 event_data->uuid_status.char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3480 read_info.characteristic.instance_id = event_data->uuid_status.char_id.inst_id;
3482 /* Fill Service in buffer */
3483 memcpy(&read_info.svc.uuid,
3484 event_data->uuid_status.srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3485 read_info.svc.instance_id = event_data->uuid_status.srvc_id.id.inst_id;
3487 /* Fill remote device address */
3488 _bt_convert_addr_string_to_type(read_info.device_address.addr, conn_info->addr);
3490 /* Fill data and reply to all apps waiting for Read result on the same characteristic
3491 Note: Even in case of failure, address, handles and result code should be returned */
3492 if (event_data->uuid_status.conn_status.status != OAL_STATUS_SUCCESS)
3493 result = BLUETOOTH_ERROR_INTERNAL;
3495 if (event_data->data_len > 0) {
3497 // for (i = 0; i < event_data->data_len; i++)
3498 // BT_INFO("Data[%d] = [0x%x]", i, event_data->data[i]);
3501 read_val = g_memdup(&event_data->data[0], event_data->data_len);
3503 data = g_variant_new_from_data(
3504 G_VARIANT_TYPE_BYTESTRING,
3506 event_data->data_len,
3509 BT_ERR("Characteristic Read success, but no data!!!");
3511 data = g_variant_new_from_data(
3512 G_VARIANT_TYPE_BYTESTRING,
3519 svc_uuid = g_memdup(&event_data->uuid_status.srvc_id.id.uuid.uuid[0], uuid_len);
3521 data_svc_uuid = g_variant_new_from_data(
3522 G_VARIANT_TYPE_BYTESTRING,
3528 char_uuid = g_memdup(&event_data->uuid_status.char_id.uuid.uuid[0], uuid_len);
3530 data_char_uuid = g_variant_new_from_data(
3531 G_VARIANT_TYPE_BYTESTRING,
3536 param = g_variant_new("(isn@ayin@ayin@ay)", result,
3540 event_data->uuid_status.srvc_id.id.inst_id,
3543 event_data->uuid_status.char_id.inst_id,
3544 event_data->data_len,
3548 _bt_send_event(BT_GATT_CLIENT_EVENT,
3549 BLUETOOTH_EVENT_GATT_READ_CHAR,
3552 /* Send DBUS return */
3553 __bt_gatt_handle_pending_request_info(result,
3554 BT_GATT_READ_CHARACTERISTIC,
3556 sizeof(bluetooth_gatt_client_char_prop_info_t));
3567 static void __bt_handle_client_descriptor_read_data(
3568 event_gattc_read_data *event_data)
3570 int result = BLUETOOTH_ERROR_NONE;
3571 struct gatt_server_info_t *conn_info = NULL;
3572 bluetooth_gatt_client_desc_prop_info_t read_info;
3574 /* Read Information data structures */
3575 GVariant *param = NULL;
3576 GVariant *data = NULL;
3577 GVariant *data_svc_uuid = NULL;
3578 GVariant *data_char_uuid = NULL;
3579 GVariant *data_desc_uuid = NULL;
3580 char *read_val = NULL;
3581 char *svc_uuid = NULL;
3582 char *char_uuid = NULL;
3583 char *desc_uuid = NULL;
3588 memset(&read_info, 0x00, sizeof(bluetooth_gatt_client_desc_prop_info_t));
3590 /* Extract Address from conn_id of event data */
3591 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3592 event_data->uuid_status.conn_status.conn_id);
3594 BT_INFO("Descriptor Read result from addr [%s] status [%d]",
3595 conn_info->addr, event_data->uuid_status.conn_status.status);
3597 /* Fill descriptor informations in buffer */
3598 memcpy(&read_info.descriptor.uuid,
3599 event_data->uuid_status.descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3600 read_info.descriptor.instance_id = event_data->uuid_status.descr_id.inst_id;
3602 /* Fill Characteristic informations in buffer */
3603 memcpy(&read_info.characteristic.uuid,
3604 event_data->uuid_status.char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3605 read_info.characteristic.instance_id = event_data->uuid_status.char_id.inst_id;
3607 /* Fill Service informations in buffer */
3608 memcpy(&read_info.svc.uuid,
3609 event_data->uuid_status.srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3610 read_info.svc.instance_id = event_data->uuid_status.srvc_id.id.inst_id;
3612 /* Fill remote device address */
3613 _bt_convert_addr_string_to_type(read_info.device_address.addr, conn_info->addr);
3615 /* Fill data and reply to all apps waiting for Read result on the same characteristic */
3616 if (event_data->uuid_status.conn_status.status != OAL_STATUS_SUCCESS)
3617 result = BLUETOOTH_ERROR_INTERNAL;
3619 BT_INFO("desc data len:%d", event_data->data_len);
3620 if (event_data->data_len > 0) {
3622 for (i = 0; i < event_data->data_len; i++)
3623 BT_DBG("Data[%d] = [0x%x]", i, event_data->data[i]);
3626 read_val = g_memdup(&event_data->data[0], event_data->data_len);
3628 data = g_variant_new_from_data(
3629 G_VARIANT_TYPE_BYTESTRING,
3631 event_data->data_len,
3634 BT_INFO("Descriptor Read success, but no data!!!");
3636 data = g_variant_new_from_data(
3637 G_VARIANT_TYPE_BYTESTRING,
3643 svc_uuid = g_memdup(&event_data->uuid_status.srvc_id.id.uuid.uuid[0], uuid_len);
3645 data_svc_uuid = g_variant_new_from_data(
3646 G_VARIANT_TYPE_BYTESTRING,
3652 char_uuid = g_memdup(&event_data->uuid_status.char_id.uuid.uuid[0], uuid_len);
3654 data_char_uuid = g_variant_new_from_data(
3655 G_VARIANT_TYPE_BYTESTRING,
3661 desc_uuid = g_memdup(&event_data->uuid_status.descr_id.uuid.uuid[0], uuid_len);
3663 data_desc_uuid = g_variant_new_from_data(
3664 G_VARIANT_TYPE_BYTESTRING,
3669 param = g_variant_new("(isn@ayin@ayin@ayin@ay)", result,
3673 event_data->uuid_status.srvc_id.id.inst_id,
3676 event_data->uuid_status.char_id.inst_id,
3679 event_data->uuid_status.descr_id.inst_id,
3680 event_data->data_len,
3684 _bt_send_event(BT_GATT_CLIENT_EVENT,
3685 BLUETOOTH_EVENT_GATT_READ_DESC,
3688 BT_INFO("Send DBUS rpely for GATT Read Descriptor");
3689 /* Send DBUS return */
3690 __bt_gatt_handle_pending_request_info(result,
3691 BT_GATT_READ_DESCRIPTOR_VALUE,
3693 sizeof(bluetooth_gatt_client_desc_prop_info_t));
3704 static void __bt_handle_client_characteristic_write_data(
3705 event_gattc_write_data *event_data)
3707 int result = BLUETOOTH_ERROR_NONE;
3708 struct gatt_server_info_t *conn_info = NULL;
3709 bluetooth_gatt_client_char_prop_info_t write_info;
3711 /* Read Information data structures */
3712 GVariant *param = NULL;
3713 GVariant *data_svc_uuid = NULL;
3714 GVariant *data_char_uuid = NULL;
3715 char *svc_uuid = NULL;
3716 char *char_uuid = NULL;
3720 memset(&write_info, 0x00, sizeof(bluetooth_gatt_client_char_prop_info_t));
3722 /* Extract Address from conn_id of event data */
3723 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3724 event_data->conn_status.conn_id);
3726 BT_INFO("Characteristic Write callback from addr [%s] status [%d]",
3727 conn_info->addr, event_data->conn_status.status);
3729 /* Fill char in buffer */
3730 memcpy(&write_info.characteristic.uuid,
3731 event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3732 write_info.characteristic.instance_id = event_data->char_id.inst_id;
3734 /* Fill Service in buffer */
3735 memcpy(&write_info.svc.uuid,
3736 event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3737 write_info.svc.instance_id = event_data->srvc_id.id.inst_id;
3739 /* Fill remote device address */
3740 _bt_convert_addr_string_to_type(write_info.device_address.addr, conn_info->addr);
3742 if (event_data->conn_status.status != OAL_STATUS_SUCCESS) {
3743 result = BLUETOOTH_ERROR_INTERNAL;
3749 svc_uuid = g_memdup(&event_data->srvc_id.id.uuid.uuid[0], uuid_len);
3751 data_svc_uuid = g_variant_new_from_data(
3752 G_VARIANT_TYPE_BYTESTRING,
3758 char_uuid = g_memdup(&event_data->char_id.uuid.uuid[0], uuid_len);
3760 data_char_uuid = g_variant_new_from_data(
3761 G_VARIANT_TYPE_BYTESTRING,
3766 param = g_variant_new("(isn@ayin@ayi)", result,
3770 event_data->srvc_id.id.inst_id,
3773 event_data->char_id.inst_id);
3776 _bt_send_event(BT_GATT_CLIENT_EVENT,
3777 BLUETOOTH_EVENT_GATT_WRITE_CHAR,
3786 /* Send DBUS return */
3787 __bt_gatt_handle_pending_request_info(result,
3788 BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE,
3790 sizeof(bluetooth_gatt_client_char_prop_info_t));
3794 static void __bt_handle_client_descriptor_write_data(
3795 event_gattc_write_data *event_data)
3797 int result = BLUETOOTH_ERROR_NONE;
3798 struct gatt_server_info_t *conn_info = NULL;
3799 bluetooth_gatt_client_desc_prop_info_t write_info;
3801 /* Write Information data structures */
3802 GVariant *param = NULL;
3803 GVariant *data_svc_uuid = NULL;
3804 GVariant *data_char_uuid = NULL;
3805 GVariant *data_desc_uuid = NULL;
3806 char *svc_uuid = NULL;
3807 char *char_uuid = NULL;
3808 char *desc_uuid = NULL;
3812 memset(&write_info, 0x00, sizeof(bluetooth_gatt_client_desc_prop_info_t));
3814 /* Extract Address from conn_id of event data */
3815 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3816 event_data->conn_status.conn_id);
3818 if (NULL == conn_info) {
3820 BT_INFO("Failed to get the conn info for conn_id [%d]", event_data->conn_status.conn_id);
3824 BT_INFO("Descriptor Write callback from addr [%s] status [%d]",
3825 conn_info->addr, event_data->conn_status.status);
3827 /* Fill descriptor informations in buffer */
3828 memcpy(&write_info.descriptor.uuid,
3829 event_data->descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3830 write_info.descriptor.instance_id = event_data->descr_id.inst_id;
3832 /* Fill Characteristic informations in buffer */
3833 memcpy(&write_info.characteristic.uuid,
3834 event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3835 write_info.characteristic.instance_id = event_data->char_id.inst_id;
3837 /* Fill Service informations in buffer */
3838 memcpy(&write_info.svc.uuid,
3839 event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3840 write_info.svc.instance_id = event_data->srvc_id.id.inst_id;
3842 /* Fill remote device address */
3843 _bt_convert_addr_string_to_type(write_info.device_address.addr, conn_info->addr);
3845 if (event_data->conn_status.status != OAL_STATUS_SUCCESS) {
3846 result = BLUETOOTH_ERROR_INTERNAL;
3852 svc_uuid = g_memdup(&event_data->srvc_id.id.uuid.uuid[0], uuid_len);
3854 data_svc_uuid = g_variant_new_from_data(
3855 G_VARIANT_TYPE_BYTESTRING,
3861 char_uuid = g_memdup(&event_data->char_id.uuid.uuid[0], uuid_len);
3863 data_char_uuid = g_variant_new_from_data(
3864 G_VARIANT_TYPE_BYTESTRING,
3870 desc_uuid = g_memdup(&event_data->descr_id.uuid.uuid[0], uuid_len);
3872 data_desc_uuid = g_variant_new_from_data(
3873 G_VARIANT_TYPE_BYTESTRING,
3878 param = g_variant_new("(isn@ayin@ayin@ayi)", result,
3882 event_data->srvc_id.id.inst_id,
3885 event_data->char_id.inst_id,
3888 event_data->descr_id.inst_id);
3891 _bt_send_event(BT_GATT_CLIENT_EVENT,
3892 BLUETOOTH_EVENT_GATT_WRITE_DESC,
3903 /* Send DBUS return */
3904 __bt_gatt_handle_pending_request_info(result,
3905 BT_GATT_WRITE_DESCRIPTOR_VALUE,
3907 sizeof(bluetooth_gatt_client_desc_prop_info_t));
3910 static void __bt_hanlde_le_device_disconnection(event_dev_conn_status_t *event_data)
3912 int result = BLUETOOTH_ERROR_INTERNAL;
3913 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
3915 _bt_convert_addr_type_to_string(address, (unsigned char *)event_data->address.addr);
3917 /* DBUS Return with fail of pending BT_CONNECT_LE for all the apps */
3918 BT_INFO("Local GATT Client disconnected: Remote addr[%s] ", address);
3920 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE, address,
3921 BT_ADDRESS_STRING_SIZE);
3925 static void __bt_handle_client_notification_registered(
3926 event_gattc_regdereg_notify_t *event_data,
3927 gboolean is_registered)
3929 int result = BLUETOOTH_ERROR_NONE;
3930 struct gatt_server_info_t *conn_info = NULL;
3931 bt_gatt_notif_reg_info_t notif_info;
3934 memset(¬if_info, 0x00, sizeof(bt_gatt_notif_reg_info_t));
3936 BT_INFO("Client Interface [%d] status [%d]",
3937 event_data->client_if,
3938 event_data->status);
3940 /* Extract Address from conn_id of event data */
3941 conn_info = __bt_find_remote_gatt_server_info_from_client_if(
3942 event_data->client_if);
3945 BT_INFO("Connection Info is not present, return");
3948 BT_INFO("Notification Registered for addr [%s]",
3952 /* Fill svc informations in buffer */
3953 memcpy(¬if_info.svc_uuid,
3954 event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3955 notif_info.svc_inst = event_data->srvc_id.id.inst_id;
3957 /* Fill char in buffer */
3958 memcpy(¬if_info.char_uuid,
3959 event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3960 notif_info.char_inst = event_data->char_id.inst_id;
3962 /* Fill remote device address */
3963 _bt_convert_addr_string_to_type(notif_info.addr.addr, conn_info->addr);
3965 notif_info.is_registered = is_registered;
3967 if (event_data->status != OAL_STATUS_SUCCESS)
3968 result = BLUETOOTH_ERROR_INTERNAL;
3970 /* Send DBUS Return for BT_GATT_WATCH_CHARACTERISTIC */
3971 __bt_gatt_handle_pending_request_info(result,
3972 BT_GATT_WATCH_CHARACTERISTIC,
3974 sizeof(bt_gatt_notif_reg_info_t));
3977 static void __bt_handle_client_notification_data(event_gattc_notify_data *event_data)
3979 /* No status in this event from OAL */
3980 int result = BLUETOOTH_ERROR_NONE;
3982 /* Read Information data structures */
3983 GVariant *param = NULL;
3984 GVariant *data = NULL;
3985 GVariant *data_svc_uuid = NULL;
3986 GVariant *data_char_uuid = NULL;
3987 char *read_val = NULL;
3988 char *svc_uuid = NULL;
3989 char *char_uuid = NULL;
3995 BT_INFO("Notifcation of charc data changed");
3997 if (event_data->data_len > 0) {
3999 for (i = 0; i < event_data->data_len; i++)
4000 BT_INFO("Data[%d] = [0x%x]", i, event_data->data[i]);
4003 addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
4004 _bt_convert_addr_type_to_string(addr,
4005 (unsigned char *)&(event_data->address.addr));
4008 read_val = g_memdup(&event_data->data[0], event_data->data_len);
4010 data = g_variant_new_from_data(
4011 G_VARIANT_TYPE_BYTESTRING,
4013 event_data->data_len,
4016 svc_uuid = g_memdup(&event_data->srvc_id.id.uuid.uuid[0], uuid_len);
4018 data_svc_uuid = g_variant_new_from_data(
4019 G_VARIANT_TYPE_BYTESTRING,
4025 char_uuid = g_memdup(&event_data->char_id.uuid.uuid[0], uuid_len);
4027 data_char_uuid = g_variant_new_from_data(
4028 G_VARIANT_TYPE_BYTESTRING,
4034 param = g_variant_new("(isn@ayin@ayin@ay)", result,
4038 event_data->srvc_id.id.inst_id,
4041 event_data->char_id.inst_id,
4042 event_data->data_len,
4046 _bt_send_event(BT_GATT_CLIENT_EVENT,
4047 BLUETOOTH_EVENT_GATT_CHAR_VAL_CHANGED,
4050 BT_ERR("No Data!!");
4063 gboolean _bt_is_remote_gatt_device_connected(bluetooth_device_address_t *address)
4066 struct gatt_server_info_t *conn_info = NULL;
4067 gboolean connected = FALSE;
4070 addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
4071 _bt_convert_addr_type_to_string(addr,
4072 (unsigned char *)&(address->addr));
4074 BT_INFO("Check Connected or not for [%s]", addr);
4075 /* Check if device is already in connected list */
4076 conn_info = _bt_find_remote_gatt_server_info(addr);
4079 BT_INFO("Remote GATT Server device [%s] is Connected", conn_info->addr);
4082 struct gatt_client_info_t *client_info = NULL;
4084 BT_INFO("Remote GATT Server Device [%s] is not Connected", addr);
4086 /* Check if device is already in connected list */
4087 client_info = _bt_find_remote_gatt_client_info(addr);
4090 BT_INFO("Remote Client device [%s] is Connected", client_info->addr);
4093 BT_INFO("Remote GATT Client Device [%s] is not Connected", addr);
4102 int _bt_connect_le_device(bluetooth_device_address_t *address,
4103 int auto_connect, int client_id)
4105 struct gatt_server_info_t *conn_info = NULL;
4106 struct gatt_out_conn_info_t *out_conn_info = NULL;
4108 invocation_info_t *req_info = NULL;
4109 int ret = OAL_STATUS_SUCCESS;
4111 char *remote_address = NULL;
4113 BT_CHECK_PARAMETER(address, return);
4117 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4118 _bt_convert_addr_type_to_string(addr, address->addr);
4119 BT_INFO("GATT Client connect request for address [%s] client instance [%d]",
4123 /* Check if Remote Device is already under connection progress */
4124 req_info = _bt_get_request_info_data_from_function_name(BT_CONNECT_LE);
4126 remote_address = (char*)req_info->user_data;
4127 if (remote_address && !strcasecmp(remote_address, addr)) {/* Address matched */
4128 BT_INFO("Already Connection ongoing for same remote GATT Server address [%s]", remote_address);
4129 /* Return and wait for events to be sent to all apps */
4131 return BLUETOOTH_ERROR_IN_PROGRESS;
4135 /* Check if remote GATT Server is connected or not */
4136 conn_info = _bt_find_remote_gatt_server_info(addr);
4138 BT_ERR("GATT Server is already connected..");
4140 return BLUETOOTH_ERROR_ALREADY_CONNECT;
4143 /* TODO Check Requirement of holding Advertisement before initiating LE connect */
4145 /* Check if app sent 0 client id for connection, in such case, use default gatt client ID */
4146 if (client_id == 0) {
4147 BT_INFO("GATT CLient connect request sent by an app without any client instance [%d]",
4149 BT_INFO("Assign default GATT client id [%d]", gatt_default_client);
4150 client_id = gatt_default_client;
4153 BT_INFO("Connect using CLient ID [%d]", client_id);
4154 ret = gattc_connect(client_id, (bt_address_t*)(address), auto_connect);
4156 if (ret != OAL_STATUS_SUCCESS) {
4157 BT_ERR("ret: %d", ret);
4159 return BLUETOOTH_ERROR_INTERNAL;
4162 /* Mark this as outgoing connection */
4163 out_conn_info = g_new0(struct gatt_out_conn_info_t, 1);
4164 out_conn_info->addr = g_strdup(addr);
4165 out_conn_info->client_id = client_id;
4166 BT_INFO("Added outgoing connection info addr[%s]", out_conn_info->addr);
4167 outgoing_gatt_conn_list = g_slist_append(outgoing_gatt_conn_list, out_conn_info);
4170 return BLUETOOTH_ERROR_NONE;
4173 int _bt_gatt_get_primary_services(char *address)
4175 BT_CHECK_PARAMETER(address, return);
4176 struct gatt_server_info_t *conn_info = NULL;
4177 invocation_info_t *req_info = NULL;
4178 int ret = OAL_STATUS_SUCCESS;
4181 /* Check if any app is already browsing primary services on the same remote GATT Server */
4182 req_info = _bt_get_request_info_data(BT_GATT_GET_PRIMARY_SERVICES, address);
4184 BT_INFO("Already Primary Service Browsing ongoing for same rmeote GATT Server");
4185 /* Return and wait for events to be sent to all apps */
4186 return BLUETOOTH_ERROR_NONE;
4189 /* Check if remote GATT Server is connected or not */
4190 conn_info = _bt_find_remote_gatt_server_info(address);
4192 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4193 conn_info->addr, conn_info->connection_id);
4195 BT_ERR("GATT Server is not yet connected..");
4196 return BLUETOOTH_ERROR_NOT_CONNECTED;
4199 /* Send Primary Service Browsing request to stack */
4200 ret = gattc_search_service(conn_info->connection_id, NULL);
4201 if (ret != OAL_STATUS_SUCCESS) {
4202 BT_ERR("ret: %d", ret);
4203 return BLUETOOTH_ERROR_INTERNAL;
4205 return BLUETOOTH_ERROR_NONE;
4208 int _bt_gatt_get_all_characteristic(bluetooth_gatt_client_svc_prop_info_t *svc)
4210 BT_CHECK_PARAMETER(svc, return);
4211 struct gatt_server_info_t *conn_info = NULL;
4212 invocation_info_t *req_info = NULL;
4213 bluetooth_gatt_client_svc_prop_info_t *prop;
4214 oal_gatt_srvc_id_t srvc_id;
4215 int ret = OAL_STATUS_SUCCESS;
4219 /* Check if any app is already browsing characteristics of the same service on the same remote GATT Server */
4220 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_GET_SERVICE_PROPERTIES);
4222 prop = (bluetooth_gatt_client_svc_prop_info_t*)req_info->user_data;
4223 if (prop && !memcmp(svc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t))
4224 && memcmp(prop->svc.uuid, svc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
4225 && prop->svc.instance_id == svc->svc.instance_id) {
4226 BT_INFO("Already Properties browsing for Primary Service ongoing for same remote GATT Server");
4227 /* Return and wait for events to be sent to all apps */
4228 return BLUETOOTH_ERROR_NONE;
4232 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4233 _bt_convert_addr_type_to_string(addr, svc->device_address.addr);
4235 /* Check if remote GATT Server is connected or not */
4236 conn_info = _bt_find_remote_gatt_server_info(addr);
4238 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4239 conn_info->addr, conn_info->connection_id);
4241 BT_ERR("GATT Server is not yet connected..");
4243 return BLUETOOTH_ERROR_NOT_CONNECTED;
4246 srvc_id.is_prmry = TRUE;
4247 srvc_id.id.inst_id = svc->svc.instance_id;
4248 memcpy(srvc_id.id.uuid.uuid, svc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4250 /* Search All Characteristic */
4251 ret = gattc_get_characteristic(conn_info->connection_id, &srvc_id, NULL);
4252 if (ret != OAL_STATUS_SUCCESS) {
4253 BT_ERR("ret: %d", ret);
4255 return BLUETOOTH_ERROR_INTERNAL;
4258 return BLUETOOTH_ERROR_NONE;
4261 int _bt_gatt_get_all_characteristic_properties(
4262 bluetooth_gatt_client_char_prop_info_t *chr)
4264 struct gatt_server_info_t *conn_info = NULL;
4265 invocation_info_t *req_info = NULL;
4266 bluetooth_gatt_client_char_prop_info_t *prop;
4267 oal_gatt_srvc_id_t srvc_id;
4268 oal_gatt_id_t char_id;
4269 int ret = OAL_STATUS_SUCCESS;
4272 BT_CHECK_PARAMETER(chr, return);
4276 /* Check if any app is already browsing descriptors of the same char of
4277 particular service on the same remote GATT Server */
4278 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_GET_CHARACTERISTIC_PROPERTIES);
4280 prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
4281 if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4282 && memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4283 && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4284 && memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4285 && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
4286 BT_INFO("Already Properties browsing for Characteristic ongoing for same remote GATT Server");
4287 /* Return and wait for events to be sent to all apps */
4288 return BLUETOOTH_ERROR_NONE;
4292 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4293 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4295 /* Check if remote GATT Server is connected or not */
4296 conn_info = _bt_find_remote_gatt_server_info(addr);
4298 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4299 conn_info->addr, conn_info->connection_id);
4301 BT_ERR("GATT Server is not yet connected..");
4303 return BLUETOOTH_ERROR_NOT_CONNECTED;
4306 srvc_id.is_prmry = TRUE;
4307 srvc_id.id.inst_id = chr->svc.instance_id;
4308 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4310 char_id.inst_id = chr->characteristic.instance_id;
4311 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4313 /* Search All Descriptors */
4314 ret = gattc_get_descriptor(conn_info->connection_id, &srvc_id, &char_id, NULL);
4315 if (ret != OAL_STATUS_SUCCESS) {
4316 BT_ERR("ret: %d", ret);
4318 return BLUETOOTH_ERROR_INTERNAL;
4321 return BLUETOOTH_ERROR_NONE;
4324 int _bt_gatt_read_characteristic_value(
4325 bluetooth_gatt_client_char_prop_info_t *chr)
4327 struct gatt_server_info_t *conn_info = NULL;
4328 invocation_info_t *req_info = NULL;
4329 bluetooth_gatt_client_char_prop_info_t *prop;
4330 oal_gatt_srvc_id_t srvc_id;
4331 oal_gatt_id_t char_id;
4332 int ret = OAL_STATUS_SUCCESS;
4335 BT_CHECK_PARAMETER(chr, return);
4339 /* Check if any app is already Reading characteristic of the same char of
4340 particular service on the same remote GATT Server */
4341 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_READ_CHARACTERISTIC);
4343 prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
4344 if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4345 && memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4346 && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4347 && memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4348 && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
4349 BT_INFO("Already Characteristic value Read operation in progress for same remote GATT Server");
4350 /* Return and wait for events to be sent to all apps */
4351 return BLUETOOTH_ERROR_NONE;
4355 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4356 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4358 /* Check if remote GATT Server is connected or not */
4359 conn_info = _bt_find_remote_gatt_server_info(addr);
4361 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4362 conn_info->addr, conn_info->connection_id);
4364 BT_ERR("GATT Server is not yet connected..");
4366 return BLUETOOTH_ERROR_NOT_CONNECTED;
4369 srvc_id.is_prmry = TRUE;
4370 srvc_id.id.inst_id = chr->svc.instance_id;
4371 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4373 char_id.inst_id = chr->characteristic.instance_id;
4374 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4376 /* Search All Descriptors */
4377 ret = gattc_read_characteristic(conn_info->connection_id, &srvc_id, &char_id, OAL_GATT_AUTH_REQ_NONE);
4378 if (ret != OAL_STATUS_SUCCESS) {
4379 BT_ERR("ret: %d", ret);
4381 return BLUETOOTH_ERROR_INTERNAL;
4384 return BLUETOOTH_ERROR_NONE;
4387 int _bt_gatt_read_descriptor_value(
4388 bluetooth_gatt_client_desc_prop_info_t *desc)
4390 struct gatt_server_info_t *conn_info = NULL;
4391 invocation_info_t *req_info = NULL;
4392 bluetooth_gatt_client_desc_prop_info_t *prop;
4393 oal_gatt_srvc_id_t srvc_id;
4394 oal_gatt_id_t char_id;
4395 oal_gatt_id_t desc_id;
4396 int ret = OAL_STATUS_SUCCESS;
4399 BT_CHECK_PARAMETER(desc, return);
4403 /* Check if any app is already Reading descriptors of the same char of
4404 particular service on the same remote GATT Server */
4405 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_READ_DESCRIPTOR_VALUE);
4407 prop = (bluetooth_gatt_client_desc_prop_info_t*)req_info->user_data;
4408 if (prop && !memcmp(desc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4409 && memcmp(desc->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4410 && desc->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4411 && memcmp(desc->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4412 && desc->characteristic.instance_id == prop->characteristic.instance_id /* Characteristic Instance ID matched */
4413 && memcmp(desc->descriptor.uuid, prop->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Descriptor UUID matched */
4414 && desc->descriptor.instance_id == prop->descriptor.instance_id) { /* Descriptor Instance ID matched */
4415 BT_INFO("Already Descriptor value Read operation in progress for same remote GATT Server");
4416 /* Return and wait for events to be sent to all apps */
4417 return BLUETOOTH_ERROR_NONE;
4421 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4422 _bt_convert_addr_type_to_string(addr, desc->device_address.addr);
4424 /* Check if remote GATT Server is connected or not */
4425 conn_info = _bt_find_remote_gatt_server_info(addr);
4427 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4428 conn_info->addr, conn_info->connection_id);
4430 BT_ERR("GATT Server is not yet connected..");
4432 return BLUETOOTH_ERROR_NOT_CONNECTED;
4435 srvc_id.is_prmry = TRUE;
4436 srvc_id.id.inst_id = desc->svc.instance_id;
4437 memcpy(srvc_id.id.uuid.uuid, desc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4439 char_id.inst_id = desc->characteristic.instance_id;
4440 memcpy(char_id.uuid.uuid, desc->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4442 desc_id.inst_id = desc->descriptor.instance_id;
4443 memcpy(desc_id.uuid.uuid, desc->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4445 /* Search All Descriptors */
4446 ret = gattc_read_descriptor(conn_info->connection_id, &srvc_id, &char_id,
4447 &desc_id, OAL_GATT_AUTH_REQ_NONE);
4448 if (ret != OAL_STATUS_SUCCESS) {
4449 BT_ERR("ret: %d", ret);
4451 return BLUETOOTH_ERROR_INTERNAL;
4454 return BLUETOOTH_ERROR_NONE;
4458 int _bt_gatt_acquire_notify(bluetooth_gatt_client_char_prop_info_t *chr, int *fd, int *mtu)
4460 struct gatt_server_info_t *conn_info = NULL;
4461 oal_gatt_srvc_id_t srvc_id;
4462 oal_gatt_id_t char_id;
4463 int ret = OAL_STATUS_SUCCESS;
4466 BT_CHECK_PARAMETER(chr, return);
4470 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4471 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4473 /* Check if remote GATT Server is connected or not */
4474 conn_info = _bt_find_remote_gatt_server_info(addr);
4476 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4477 conn_info->addr, conn_info->connection_id);
4479 BT_ERR("GATT Server is not yet connected..");
4481 return BLUETOOTH_ERROR_NOT_CONNECTED;
4484 srvc_id.is_prmry = TRUE;
4485 srvc_id.id.inst_id = chr->svc.instance_id;
4486 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4488 char_id.inst_id = chr->characteristic.instance_id;
4489 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4491 ret = gattc_acquire_notify(conn_info->connection_id, &srvc_id, &char_id, fd, mtu);
4493 if (ret != OAL_STATUS_SUCCESS) {
4494 BT_ERR("ret: %d", ret);
4496 return BLUETOOTH_ERROR_INTERNAL;
4498 BT_INFO("GATT characterstics FD [%d] mtu[%d]", *fd, *mtu);
4500 return BLUETOOTH_ERROR_NONE;
4504 int _bt_gatt_acquire_write(bluetooth_gatt_client_char_prop_info_t *chr, int *fd, int *mtu)
4507 struct gatt_server_info_t *conn_info = NULL;
4508 oal_gatt_srvc_id_t srvc_id;
4509 oal_gatt_id_t char_id;
4510 int ret = OAL_STATUS_SUCCESS;
4513 BT_CHECK_PARAMETER(chr, return);
4517 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4518 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4520 /* Check if remote GATT Server is connected or not */
4521 conn_info = _bt_find_remote_gatt_server_info(addr);
4523 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4524 conn_info->addr, conn_info->connection_id);
4526 BT_ERR("GATT Server is not yet connected..");
4528 return BLUETOOTH_ERROR_NOT_CONNECTED;
4531 srvc_id.is_prmry = TRUE;
4532 srvc_id.id.inst_id = chr->svc.instance_id;
4533 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4535 char_id.inst_id = chr->characteristic.instance_id;
4536 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4538 ret = gattc_acquire_write(conn_info->connection_id, &srvc_id, &char_id,
4539 OAL_GATT_AUTH_REQ_NONE, fd, mtu);
4540 if (ret != OAL_STATUS_SUCCESS) {
4541 BT_ERR("ret: %d", ret);
4543 return BLUETOOTH_ERROR_INTERNAL;
4545 BT_INFO("GATT characterstics FD [%d] mtu [%d]", *fd, *mtu);
4547 return BLUETOOTH_ERROR_NONE;
4552 /* Write Characteristic */
4553 int _bt_gatt_write_characteristic_value_by_type(
4554 bluetooth_gatt_client_char_prop_info_t *chr,
4555 bluetooth_gatt_att_data_t *data,
4556 bluetooth_gatt_write_type_e write_type)
4558 struct gatt_server_info_t *conn_info = NULL;
4559 invocation_info_t *req_info = NULL;
4560 bluetooth_gatt_client_char_prop_info_t *prop;
4561 oal_gatt_srvc_id_t srvc_id;
4562 oal_gatt_id_t char_id;
4563 int ret = OAL_STATUS_SUCCESS;
4567 BT_CHECK_PARAMETER(chr, return);
4568 BT_CHECK_PARAMETER(data, return);
4572 /* Check if any app is already writing same char of
4573 particular service on the same remote GATT Server */
4574 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE);
4576 prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
4577 if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4578 && memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4579 && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4580 && memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4581 && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
4582 BT_INFO("Already Characteristic Write Value operation in progress for same remote GATT Server");
4583 /* Return and wait for events to be sent to all apps */
4584 return BLUETOOTH_ERROR_NONE;
4588 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4589 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4591 /* Check if remote GATT Server is connected or not */
4592 conn_info = _bt_find_remote_gatt_server_info(addr);
4594 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4595 conn_info->addr, conn_info->connection_id);
4597 BT_ERR("GATT Server is not yet connected..");
4599 return BLUETOOTH_ERROR_NOT_CONNECTED;
4602 srvc_id.is_prmry = TRUE;
4603 srvc_id.id.inst_id = chr->svc.instance_id;
4604 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4606 char_id.inst_id = chr->characteristic.instance_id;
4607 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4609 /* Write CHar value */
4611 BT_INFO("Connection ID [%d] write type [%d] data length [%d]", conn_info->connection_id, write_type, data->length);
4612 for (k = 0; k < data->length; k++)
4613 BT_INFO("Data[%d] [0x%x]", k, data->data[k]);
4615 ret = gattc_write_characteristic(conn_info->connection_id,
4617 (oal_gatt_write_type_t)write_type, data->length,
4618 OAL_GATT_AUTH_REQ_NONE, (char *)(&data->data[0]));
4619 if (ret != OAL_STATUS_SUCCESS) {
4620 BT_ERR("ret: %d", ret);
4622 return BLUETOOTH_ERROR_INTERNAL;
4625 return BLUETOOTH_ERROR_NONE;
4628 /* Write Descriptor */
4629 int _bt_gatt_write_descriptor_value_by_type(
4630 bluetooth_gatt_client_desc_prop_info_t *desc,
4631 bluetooth_gatt_att_data_t *data,
4632 bluetooth_gatt_write_type_e write_type)
4634 struct gatt_server_info_t *conn_info = NULL;
4635 invocation_info_t *req_info = NULL;
4636 bluetooth_gatt_client_desc_prop_info_t *prop;
4637 oal_gatt_srvc_id_t srvc_id;
4638 oal_gatt_id_t char_id;
4639 oal_gatt_id_t desc_id;
4640 int ret = OAL_STATUS_SUCCESS;
4644 BT_CHECK_PARAMETER(desc, return);
4645 BT_CHECK_PARAMETER(data, return);
4649 /* Check if any app is already writing on same Descriptor of the same char of
4650 particular service on the same remote GATT Server */
4651 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_WRITE_DESCRIPTOR_VALUE);
4653 prop = (bluetooth_gatt_client_desc_prop_info_t*)req_info->user_data;
4654 if (prop && !memcmp(desc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4655 && memcmp(desc->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4656 && desc->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4657 && memcmp(desc->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4658 && desc->characteristic.instance_id == prop->characteristic.instance_id /* Characteristic Instance ID matched */
4659 && memcmp(desc->descriptor.uuid, prop->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Descriptor UUID matched */
4660 && desc->descriptor.instance_id == prop->descriptor.instance_id) { /* Descriptor Instance ID matched */
4661 BT_INFO("Already Descriptor value Write operation in progress for same remote GATT Server");
4662 /* Return and wait for events to be sent to all apps */
4663 return BLUETOOTH_ERROR_NONE;
4667 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4668 _bt_convert_addr_type_to_string(addr, desc->device_address.addr);
4670 /* Check if remote GATT Server is connected or not */
4671 conn_info = _bt_find_remote_gatt_server_info(addr);
4673 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4674 conn_info->addr, conn_info->connection_id);
4676 BT_ERR("GATT Server is not yet connected..");
4678 return BLUETOOTH_ERROR_NOT_CONNECTED;
4681 srvc_id.is_prmry = TRUE;
4682 srvc_id.id.inst_id = desc->svc.instance_id;
4683 memcpy(srvc_id.id.uuid.uuid, desc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4685 char_id.inst_id = desc->characteristic.instance_id;
4686 memcpy(char_id.uuid.uuid, desc->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4688 desc_id.inst_id = desc->descriptor.instance_id;
4689 memcpy(desc_id.uuid.uuid, desc->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4692 BT_INFO("Connection ID [%d] write type [%d] data length [%d]", conn_info->connection_id, write_type, data->length);
4693 for (k = 0; k < data->length; k++)
4694 BT_INFO("Data[%d] [0x%x]", k, data->data[k]);
4696 ret = gattc_write_descriptor(conn_info->connection_id,
4697 &srvc_id, &char_id, &desc_id,
4698 (oal_gatt_write_type_t)write_type, data->length,
4699 OAL_GATT_AUTH_REQ_NONE, (char *)(&data->data[0]));
4700 if (ret != OAL_STATUS_SUCCESS) {
4701 BT_ERR("ret: %d", ret);
4703 return BLUETOOTH_ERROR_INTERNAL;
4706 return BLUETOOTH_ERROR_NONE;
4709 int _bt_gatt_watch_characteristic(
4710 bluetooth_gatt_client_char_prop_info_t *chr,
4714 struct gatt_server_info_t *conn_info = NULL;
4715 oal_gatt_srvc_id_t srvc_id;
4716 oal_gatt_id_t char_id;
4717 int ret = OAL_STATUS_SUCCESS;
4720 BT_CHECK_PARAMETER(chr, return);
4722 BT_INFO("Client ID [%d] Is Notify [%d]", client_id, is_notify);
4724 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4725 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4727 /* Check if remote GATT Server is connected or not */
4728 conn_info = _bt_find_remote_gatt_server_info(addr);
4730 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4731 conn_info->addr, conn_info->connection_id);
4733 BT_ERR("GATT Server is not yet connected..");
4735 return BLUETOOTH_ERROR_NOT_CONNECTED;
4737 srvc_id.is_prmry = TRUE;
4738 srvc_id.id.inst_id = chr->svc.instance_id;
4739 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4741 char_id.inst_id = chr->characteristic.instance_id;
4742 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4744 /* Register or unregister Notification characteristic */
4746 ret = gattc_register_for_notification(conn_info->client_id,
4747 (bt_address_t*)&(chr->device_address),
4748 &srvc_id, &char_id);
4750 ret = gattc_deregister_for_notification(conn_info->client_id,
4751 (bt_address_t*)&(chr->device_address),
4752 &srvc_id, &char_id);
4754 BT_INFO("Result[%d]", ret);
4755 if (ret != OAL_STATUS_SUCCESS) {
4756 BT_ERR("ret: %d", ret);
4758 return BLUETOOTH_ERROR_INTERNAL;
4761 return BLUETOOTH_ERROR_NONE;
4765 int _bt_disconnect_le_device(bluetooth_device_address_t *address,
4768 struct gatt_server_info_t *conn_info = NULL;
4769 struct gatt_client_info_t *rem_client_conn_info = NULL;
4770 invocation_info_t *req_info = NULL;
4771 int ret = OAL_STATUS_SUCCESS;
4773 char *remote_address = NULL;
4775 BT_CHECK_PARAMETER(address, return);
4779 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4780 _bt_convert_addr_type_to_string(addr, address->addr);
4781 BT_INFO("GATT Client Disconnect request for address [%s]", addr);
4783 /* Check if Remote Device is already under connection progress */
4784 req_info = _bt_get_request_info_data_from_function_name(BT_DISCONNECT_LE);
4786 remote_address = (char*)req_info->user_data;
4787 if (remote_address && !strcasecmp(remote_address, addr)) {/* Address matched */
4788 BT_INFO("Already DisConnection ongoing for same remote GATT Server address [%s]", remote_address);
4789 /* Return success and wait for events to be sent to all apps */
4791 return BLUETOOTH_ERROR_IN_PROGRESS;
4794 /* Check if remote GATT Server is connected or not */
4795 conn_info = _bt_find_remote_gatt_server_info(addr);
4797 /* Check if app sent 0 client id for Disconnection, in such case, use default gatt client ID */
4798 if (client_id == 0) {
4799 BT_INFO("GATT CLient Disconnect request sent by an app without any client instance [%d]",
4801 BT_INFO("Assign default GATT client id [%d]", gatt_default_client);
4802 client_id = gatt_default_client;
4805 BT_INFO("Disconnect remote gatt server using CLient ID [%d] Connection ID [%d]", client_id, conn_info->connection_id);
4806 ret = gattc_disconnect(client_id, (bt_address_t*)(address),
4807 conn_info->connection_id);
4809 /* check if remote client is connected */
4810 rem_client_conn_info = _bt_find_remote_gatt_client_info(addr);
4812 if (!rem_client_conn_info || client_id != 0) {
4813 BT_ERR("GATT device is not connected..");
4815 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
4818 BT_INFO("Disconnect remote gatt client ");
4820 ret = gatts_disconnect(rem_client_conn_info->instance_id,
4821 (bt_address_t*)(address), rem_client_conn_info->connection_id);
4824 if (ret != OAL_STATUS_SUCCESS) {
4825 BT_ERR("ret: %d", ret);
4827 return BLUETOOTH_ERROR_INTERNAL;
4830 return BLUETOOTH_ERROR_NONE;
4833 int _bt_gatt_watch_service_changed_indication(const char *sender,
4834 bluetooth_device_address_t *address,
4835 gboolean is_enabled)
4838 bt_service_app_info_t *info = NULL;
4840 BT_INFO("Enable Servic changed Indication watcher [%d] for app [%s]",
4841 is_enabled, sender);
4843 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
4846 if (g_strcmp0(sender, info->sender) == 0 &&
4847 memcmp(info->address.addr, address->addr,
4848 sizeof(bluetooth_device_address_t)) == 0) {
4849 BT_INFO("Found GATT client App.. UUID [%s], sender [%s]", info->uuid, info->sender);
4850 info->is_watcher_enabled = is_enabled;
4854 return BLUETOOTH_ERROR_NONE;
4857 int _bt_unregister_gatt_client_instance(const char *sender, int client_id)
4859 bt_service_app_info_t *info = NULL;
4862 BT_INFO("Unregister Allocated GATT Client instance [%s] Client ID [%d]", sender, client_id);
4864 /* Unregister CLient instance associated with address X. It is possible that another app still
4865 has client_id valid for same remote address */
4866 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
4869 /* Exact matching of sender */
4870 if (!g_strcmp0(info->sender, sender) && info->client_id == client_id) { /* Check for only valid GATT client Instance */
4871 BT_INFO("Unregister GATT client instance [%d]", info->client_id);
4872 numapps[k].client_id = -1;
4873 numapps[k].is_initialized = FALSE;
4874 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
4875 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
4876 memset(&numapps[k].address.addr, 0x00, sizeof(bluetooth_device_address_t));
4878 /* Its a GATT Client Instance */
4879 ret = gattc_deregister(client_id);
4880 if (ret != OAL_STATUS_SUCCESS) {
4881 BT_ERR("DeAllocate GATT Client instance with stack Fail ret: %d", ret);
4882 return BLUETOOTH_ERROR_INTERNAL;
4884 return BLUETOOTH_ERROR_NONE;
4889 return BLUETOOTH_ERROR_NOT_FOUND;
4892 static void __bt_handle_client_mtu_exchange_completed(event_gattc_mtu_configured_t *event_data)
4894 int result = BLUETOOTH_ERROR_NONE;
4895 struct gatt_server_info_t *conn_info = NULL;
4896 GVariant *param = NULL;
4900 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(event_data->conn_id);
4901 if (conn_info == NULL) {
4902 BT_ERR("Cant find connection Information");
4906 BT_INFO("GATT Client: MTU Configured from addr [%s] status [%d] MTU size [%d]",
4907 conn_info->addr, event_data->status, event_data->mtu);
4909 if (event_data->status != OAL_STATUS_SUCCESS)
4910 result = BLUETOOTH_ERROR_INTERNAL;
4912 BT_INFO("reply BT_REQ_ATT_MTU ");
4914 /* DBUS Return fo BT_REQ_ATT_MTU for all the apps */
4915 __bt_gatt_handle_pending_request_info(result, BT_REQ_ATT_MTU, conn_info->addr,
4916 BT_ADDRESS_STRING_SIZE);
4918 if (result == BLUETOOTH_ERROR_NONE) {
4919 BT_INFO("sending mtu event");
4921 mtu = event_data->mtu;
4922 param = g_variant_new("(isqy)",
4928 /* Send event to BT-API */
4929 _bt_send_event(BT_DEVICE_EVENT,
4930 BLUETOOTH_EVENT_GATT_ATT_MTU_CHANGED,
4933 /* Update the MTU for current connection */
4934 BT_INFO("Updating the MTU for current connection");
4935 __bt_update_mtu_gatt_device(conn_info->addr, event_data->mtu);
4939 static struct gatt_mtu_info_t *__bt_find_mtu_gatt_device(char *address)
4942 struct gatt_mtu_info_t *info = NULL;
4946 for (l = gatt_mtu_info_list; l != NULL; l = g_slist_next(l)) {
4947 info = (struct gatt_mtu_info_t*)l->data;
4951 if (!g_strcmp0(info->addr, address)) {
4952 BT_INFO("Remote GATT device found addr[%s]", info->addr);
4957 BT_INFO("Not found Remote GATT device addr[%s]", address);
4961 static void __bt_remove_mtu_gatt_device(char *address)
4963 struct gatt_mtu_info_t *dev_info = NULL;
4967 dev_info = __bt_find_mtu_gatt_device(address);
4970 BT_INFO("removing the gatt device from mtu list");
4971 gatt_mtu_info_list = g_slist_remove(gatt_mtu_info_list, dev_info);
4972 g_free(dev_info->addr);
4977 static void __bt_add_mtu_gatt_device(char *address)
4979 struct gatt_mtu_info_t *dev_info = NULL;
4983 dev_info = __bt_find_mtu_gatt_device(address);
4986 BT_INFO("adding the gatt device in mtu list");
4987 dev_info = g_new0(struct gatt_mtu_info_t, 1);
4988 dev_info->addr = g_strdup(address);
4989 dev_info->att_mtu = BT_DEFAULT_ATT_MTU;
4990 gatt_mtu_info_list = g_slist_append(gatt_mtu_info_list, dev_info);
4994 static void __bt_update_mtu_gatt_device(char *address, int mtu)
4996 struct gatt_mtu_info_t *dev_info = NULL;
5000 dev_info = __bt_find_mtu_gatt_device(address);
5003 BT_INFO("Udating the mtu");
5004 dev_info->att_mtu = mtu;