4 * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd.
6 * Contact: Anupam Roy (anupam.r@samsung.com)
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
23 #include "bt-service-common.h"
24 #include "bt-service-core-adapter.h"
25 #include "bt-service-event-receiver.h"
26 #include "bt-request-handler.h"
27 #include "bluetooth-api.h"
29 #include "bluetooth-api.h"
30 #include "bt-internal-types.h"
31 #include "bt-service-util.h"
32 #include "bt-service-common.h"
33 #include "bt-service-event.h"
35 #include "bt-internal-types.h"
36 #include "bt-service-gatt.h"
37 #include "bt-service-gatt.h"
38 #include "bt-service-core-device.h"
39 #include "bt-service-core-adapter-le.h"
40 #ifdef TIZEN_GATT_CLIENT
41 #include "bluetooth-gatt-client-api.h"
44 #include <oal-hardware.h>
45 #include <oal-manager.h>
46 #include <oal-event.h>
47 #include <oal-adapter-mgr.h>
48 #include <oal-device-mgr.h>
51 #define BT_GATT_TRANSPORT_LE 0
52 #define BT_GATT_TRANSPORT_BR_EDR 1
53 #define BT_GATT_TRANSPORT_LE_BR_EDR 2
54 #define BT_UUID_STRING_MAX 64
55 #define BT_SENDER_MAX_LENGTH 50
56 #define MAX_APPS_SUPPORTED 11 /* Slot 0 is not used */
58 #ifdef TIZEN_GATT_CLIENT
65 #define UUID_MAX_LEN 50
68 #define BDADDR_ANY (&(bluetooth_device_address_t) {{0, 0, 0, 0, 0, 0} })
70 #ifdef TIZEN_GATT_CLIENT
71 char uuid_list[NUM_UUID][BT_UUID_STRING_MAX] = {"0000b00b-0000-0000-f065-080080fa49b5", /* Used by BLEAPP */
72 "0000b00b-1111-1111-0123-456789ab0cd2", /* Used by BLEAPP */
73 "0000b00b-2222-1111-0123-456789ab0cd2",
74 "0000b00b-3333-1111-0123-456789ab0cd2",
75 "0000b00b-4444-1111-0123-456789ab0cd2",
76 "0000b00b-5555-1111-0123-456789ab0cd2",
77 "0000b00b-6666-1111-0123-456789ab0cd2",
78 "0000b00b-7777-1111-0123-456789ab0cd2",
79 "0000b00b-8888-1111-0123-456789ab0cd2",
80 "0000b00b-9999-1111-0123-456789ab0cd2",
81 "0000b00b-aaaa-1111-0123-456789ab0cd2",
82 "0000b00b-bbbb-1111-0123-456789ab0cd2",
83 "0000b00b-cccc-1111-0123-456789ab0cd2",
84 "0000b00b-dddd-1111-0123-456789ab0cd2",
85 "0000b00b-eeee-1111-0123-456789ab0cd2",
86 "0000b00b-ffff-1111-0123-456789ab0cd2",
87 "0000b00c-0000-1111-0123-456789ab0cd2",
88 "0000b00c-1111-1111-0123-456789ab0cd2",
89 "0000b00c-2222-1111-0123-456789ab0cd2",
90 "0000b00c-3333-1111-0123-456789ab0cd2"};
92 char uuid_list[NUM_UUID][BT_UUID_STRING_MAX] = {"0000b00b-0000-0000-f065-080080fa49b5", /* Used by BLEAPP */
93 "0000b00b-1111-1111-0123-456789ab0cd2", /* Used by BLEAPP */
94 "0000b00b-2222-1111-0123-456789ab0cd2",
95 "0000b00b-3333-1111-0123-456789ab0cd2",
96 "0000b00b-4444-1111-0123-456789ab0cd2",
97 "0000b00b-5555-1111-0123-456789ab0cd2",
98 "0000b00b-6666-1111-0123-456789ab0cd2",
99 "0000b00b-7777-1111-0123-456789ab0cd2",
100 "0000b00b-8888-1111-0123-456789ab0cd2",
101 "0000b00b-9999-1111-0123-456789ab0cd2"};
104 #ifdef TIZEN_GATT_CLIENT
105 /* Reserved GATT client Instance UUID. This is used only internally by bt-service */
106 #define DEFAULT_GATT_CLIENT_UUID "0000a00a-1111-1111-0123-456789abcdef"
108 static int gatt_default_client = -1;
111 gboolean is_registered;
112 bluetooth_device_address_t addr;
113 unsigned char svc_uuid[BLUETOOTH_UUID_HEX_MAX_LEN];
115 unsigned char char_uuid[BLUETOOTH_UUID_HEX_MAX_LEN];
117 } bt_gatt_notif_reg_info_t;
119 struct gatt_out_conn_info_t {
120 int client_id; /* This value unique identifies a GATT Client instance */
121 char *addr; /* Remote GATT Server address */
124 /* Linked List of outgoing gatt connection list
125 Note: This is valid only for local GATT client */
126 static GSList *outgoing_gatt_conn_list = NULL;
128 /* GATT Server Info(Local Client) List Structure */
129 struct gatt_server_info_t {
130 int connection_id; /* This value will uniquely identify a GATT client-server connection */
131 int client_id; /* This value unique identifies a GATT Client instance */
132 char *addr; /* Remote GATT Server address */
135 /* Linked List of connected Remote GATT Servers */
136 static GSList *gatt_server_info_list = NULL;
140 unsigned char uuid[BLUETOOTH_UUID_HEX_MAX_LEN]; /* If any service added */
141 } bt_gatt_svc_changed_info_t;
146 // bluetooth_device_address_t address; /* Remote BLE Device Address */
147 GSList *services; /* List of all services of above remote device */
148 int count; /* Number of services browsed from remote device */
149 bt_gatt_svc_changed_info_t info;
150 } bt_gatt_service_info_list_t;
153 unsigned char uuid[BLUETOOTH_UUID_HEX_MAX_LEN];
157 GSList *included_svcs;
158 gboolean is_removed; /* 0 => Service is added, 1=> removed */
159 } bt_gatt_service_info_t;
162 unsigned char uuid[BLUETOOTH_UUID_HEX_MAX_LEN];
166 unsigned char val[BLUETOOTH_GATT_ATT_DATA_LENGTH_MAX];
168 } bt_gatt_char_info_t;
171 unsigned char uuid[BLUETOOTH_UUID_HEX_MAX_LEN];
174 unsigned char val[BLUETOOTH_GATT_ATT_DATA_LENGTH_MAX];
175 } bt_gatt_descriptor_info_t;
178 unsigned char uuid[BLUETOOTH_UUID_HEX_MAX_LEN];
180 } bt_gatt_included_service_info_t;
182 GSList *list_gatt_info;
187 /* App Information structure */
190 char sender[BT_SENDER_MAX_LENGTH];
191 char uuid[BT_UUID_STRING_MAX];
194 bluetooth_advertising_data_t adv_data; /* Will store adv data for specific slot */
196 bluetooth_scan_resp_data_t scan_rsp; /* Will store scan rsp data for specific slot */
198 gboolean is_initialized;
199 GSList *service_handles;
200 #ifdef TIZEN_GATT_CLIENT
201 int client_id; /* GATT Client instance ID */
202 bluetooth_device_address_t address; /* Remote BLE Device Address */
203 gboolean is_watcher_enabled;
205 } bt_service_app_info_t;
207 /* GATT Server Request Info Structure */
208 struct gatt_server_req_info {
209 int connection_id; /* This value will uniquely identify a GATT client-server connection */
210 int request_id; /* This is an unique transaction ID assigned against each request by stack */
211 int attribute_handle; /* GATT server attribute handle */
212 int offset; /* GATT server attribute offset on which request is invoked by GATT client */
213 bluetooth_gatt_att_request_type_e request_type; /* Read or Write request */
214 char *addr; /* Remote GATT client address */
217 /* GATT Client Info List Structure */
218 struct gatt_client_info_t {
219 int connection_id; /* This value will uniquely identify a GATT client-server connection */
220 int instance_id; /* This value unique identifies a GATT server instance */
221 char *addr; /* Remote GATT client address */
224 /* GATT Indicate confirm result */
225 struct gatt_indicate_cfm_result_info_t {
226 int result; /* Result of event */
227 char *addr; /* Remote GATT client address */
228 int att_hdl; /* Characteristic Attribute handle */
229 int completed; /* 1 if last event, otheriwse 0 */
232 /* Request Search Utility method */
233 static struct gatt_server_req_info *__bt_gatt_server_find_request_info(int request_id,
234 bluetooth_gatt_att_request_type_e req_type);
236 static int __bt_gatt_send_indication_to_all_connected_clients(bluetooth_gatt_att_data_t *data,
237 bluetooth_gatt_server_indication_params_t *param);
239 static void __bt_remove_all_service_handles(int instance_id);
241 #ifdef TIZEN_GATT_CLIENT
242 static void __bt_handle_client_instance_registered(event_gattc_register_t *data);
243 static void __bt_handle_client_connected(event_gattc_conn_t *event_data);
244 static void __bt_handle_client_disconnected(event_gattc_conn_t *event_data);
245 static void __bt_handle_client_service_search_completed(event_gattc_conn_status_t *event_data);
246 static void __bt_handle_client_service_search_result(event_gattc_service_result_t *event_data);
247 static void __bt_handle_client_characteristic_search_result(
248 event_gattc_characteristic_result_t *event_data);
249 static void __bt_handle_client_descriptor_search_result(event_gattc_descriptor_result_t *event_data);
250 static void __bt_handle_client_characteristic_read_data(event_gattc_read_data *event_data);
251 static void __bt_handle_client_descriptor_read_data(event_gattc_read_data *event_data);
252 static void __bt_handle_client_characteristic_write_data(event_gattc_write_data *event_data);
253 static void __bt_handle_client_descriptor_write_data(event_gattc_write_data *event_data);
254 static void __bt_hanlde_le_device_disconnection(event_dev_conn_status_t *event_data);
255 static void __bt_handle_client_notification_registered(event_gattc_regdereg_notify_t *event_data,
256 gboolean is_registered);
257 static void __bt_handle_client_notification_data(event_gattc_notify_data *event_data);
261 static int __bt_unregister_gatt_server_instance(int server_instance);
263 /* Linked List of GATT requests from Remote GATT Clients */
264 static GSList *gatt_server_requests = NULL;
266 /* Linked List of connected Remote GATT clients */
267 static GSList *gatt_client_info_list = NULL;
269 /* Number of clients to be notified to */
270 static int num_indicate_clients;
272 /* List of applications */
273 static bt_service_app_info_t numapps[MAX_APPS_SUPPORTED];
275 static void __bt_gatt_handle_pending_request_info(int result,
276 int service_function, void *data, unsigned int size);
278 static void __bt_handle_server_instance_registered(event_gatts_register_t *data);
280 static void __bt_gatt_event_handler(int event_type, gpointer event_data);
282 void _bt_check_adv_app_termination(const char *name)
284 bt_service_app_info_t *app = NULL;
286 int apps[MAX_APPS_SUPPORTED];
288 ret_if(NULL == name);
290 memset(&apps, -1, sizeof(apps));
292 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
295 /* Search for a app which has same sender and stop adv is running */
296 if (!strncasecmp(app->sender, name, strlen(name)) && app->is_initialized == TRUE) {
297 BT_DBG("Match found, name: %s", name);
299 /* Unregister all service handles with stack */
300 __bt_remove_all_service_handles(app->instance_id);
302 /* If Advertising is enabled, stop it */
303 if (app->adv_handle > 0) {
304 BT_INFO("Stop advertising on instance ID [%d]", app->instance_id);
305 /* Disable adv if running */
306 BT_INFO("Disable Advertising Adv Handle [%d] sender [%s]",
307 app->adv_handle, name);
308 _bt_set_advertising(app->sender, app->adv_handle, FALSE, FALSE);
311 /* Save instances of all apps that need to be unregistered */
312 apps[app->instance_id] = 1;
316 /* Unregister all apps one by one */
317 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
319 BT_INFO("Unregister app[%d]", k);
320 /* Unregister server instance */
321 __bt_unregister_gatt_server_instance(k);
326 static int __bt_gatt_send_indication_to_all_connected_clients(bluetooth_gatt_att_data_t *data,
327 bluetooth_gatt_server_indication_params_t *param)
330 int ret = OAL_STATUS_SUCCESS;
331 int result = OAL_STATUS_INTERNAL_ERROR;
333 BT_INFO("Current total number of connected clients [%d]", g_slist_length(gatt_client_info_list));
334 for (l = gatt_client_info_list; l != NULL; l = l->next) {
335 struct gatt_client_info_t *info = l->data;
338 BT_INFO("GATT Remote client address [%s] connection Id [%d]", info->addr, info->connection_id);
340 ret = gatts_send_indication(param->instance_id, param->atrribute_handle,
341 info->connection_id, data->length,
342 param->need_confirmation, (char *)(&data->data[0]));
344 BT_INFO("Send Indication to GATT client [%s] result: [%d]", info->addr, ret);
345 if (ret == OAL_STATUS_SUCCESS) {
346 BT_INFO("Send Indication sent successfully to GATT client [%s]", info->addr);
348 num_indicate_clients++;
352 BT_INFO("Indication sending done for total number of clients [%d]", num_indicate_clients);
356 static struct gatt_server_req_info *__bt_gatt_server_find_request_info(int request_id,
357 bluetooth_gatt_att_request_type_e req_type)
361 for (l = gatt_server_requests; l != NULL; l = l->next) {
362 struct gatt_server_req_info *req_info = l->data;
364 if (req_info && req_info->request_id == request_id && req_info->request_type == req_type) {
365 BT_INFO("GATT Server request info found Req ID [%d] handle [%d] conn ID [%d]",
366 req_info->request_id, req_info->attribute_handle, req_info->connection_id);
370 BT_ERR("Gatt Request not found");
374 void _bt_get_adv_handle_from_instance(int server_inst, int *adv_handle)
378 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
379 if (numapps[k].is_initialized == 1 && numapps[k].instance_id == server_inst) {
380 *adv_handle = numapps[k].adv_handle;
386 #ifdef TIZEN_GATT_CLIENT
387 char * _bt_gatt_get_default_gatt_client_uuid(void)
389 return g_strdup(DEFAULT_GATT_CLIENT_UUID);
393 static void __bt_register_default_gatt_client()
398 uuid_str = _bt_gatt_get_default_gatt_client_uuid();
399 _bt_string_to_uuid(uuid_str, (service_uuid_t*)&uuid);
401 BT_INFO("Register Default GATT client uuid [%s]", uuid_str);
403 if (OAL_STATUS_SUCCESS != gattc_register(&uuid)) /* for only Smart Control */
404 BT_ERR("gattc register failed");
410 int _bt_gatt_init(void)
414 if (OAL_STATUS_SUCCESS != gatt_enable()) {
415 BT_ERR("gatt Init failed");
416 return BLUETOOTH_ERROR_INTERNAL;
419 /* Register gatt event handler */
420 _bt_service_register_event_handler_callback(BT_GATT_MODULE, __bt_gatt_event_handler);
421 #ifdef TIZEN_GATT_CLIENT
422 /*In bluedroid product, defacult gatt client is handled differently*/
423 __bt_register_default_gatt_client();
426 return BLUETOOTH_ERROR_NONE;
429 static void __bt_service_reset_gatt_data()
433 BT_INFO("Rest numapp");
436 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
437 numapps[k].is_initialized = 0;
438 numapps[k].instance_id = -1;
439 numapps[k].adv_handle = 0;
440 numapps[k].adv_instance = -1;
441 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
442 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
443 memset(numapps[k].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
444 memset(numapps[k].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
445 numapps[k].adv_data_len = 0;
446 numapps[k].scan_rsp_len = 0;
450 void _bt_gatt_deinit(void)
452 BT_INFO("GATT deinit");
454 if (OAL_STATUS_SUCCESS != gatt_disable())
455 BT_ERR("gatt deinit failed");
457 /* Un-register gatt event handler */
458 _bt_service_unregister_event_handler_callback(BT_GATT_MODULE);
460 __bt_service_reset_gatt_data();
463 void _bt_update_adv_handle(const char *sender, int adv_handle)
466 BT_DBG("Sender [%s] Adv handle [%d]", sender, adv_handle);
467 bt_service_app_info_t *info = NULL;
469 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
471 /* Search for a app which has same sender and adv handle as 0 */
472 if (!g_strcmp0(info->sender, sender) && info->adv_handle == 0)
473 info->adv_handle = adv_handle;
477 int _bt_is_sender_gatt_server_with_no_adv(const char *sender, int adv_handle)
480 BT_DBG("Sender [%s] Adv handle [%d]", sender, adv_handle);
481 bt_service_app_info_t *info = NULL;
483 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
485 /* Search for a app which has same sender and adv handle as 0
486 It is possible that same sender but different adv handle */
487 if (!g_strcmp0(info->sender, sender) && info->adv_handle == 0) {
488 //info->adv_handle = adv_handle;
489 return info->instance_id;
495 int _bt_get_allocated_server_instance(const char *sender, int adv_handle, gboolean use_reserved_slot)
498 BT_DBG("Sender [%s] Adv handle [%d]", sender, adv_handle);
499 bt_service_app_info_t *info = NULL;
501 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
504 /* Exact matching of Adv handle + sender combination */
505 if (!g_strcmp0(info->sender, sender) && info->adv_handle == adv_handle)
506 return info->instance_id;
508 if (!g_strcmp0(info->sender, sender) && info->adv_handle == -1)
509 return info->instance_id;
515 char * _bt_get_sender_and_handle(int server_instance, int *adv_handle)
518 bt_service_app_info_t *info = NULL;
520 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
523 if (info->instance_id == server_instance && info->adv_handle != -1) {
524 *adv_handle = info->adv_handle;
525 BT_DBG("Server instance [%d] Adv handle [%d] Sender [%s]", server_instance, *adv_handle, info->sender);
526 return g_strdup(info->sender);
532 void _bt_set_new_adv_data(bluetooth_advertising_data_t *adv, int len, int instance)
536 bt_service_app_info_t *info = NULL;
537 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
540 if (info->instance_id == instance) {
541 memcpy(info->adv_data.data, &adv->data, len);
548 void _bt_set_new_scan_rsp_data(bluetooth_scan_resp_data_t *scan, int len, int instance)
552 bt_service_app_info_t *info = NULL;
553 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
556 if (info->instance_id == instance) {
557 memcpy(info->scan_rsp.data, &scan->data, len);
564 void _bt_get_previous_adv_data(bluetooth_advertising_data_t *adv, int *len, int instance)
568 bt_service_app_info_t *info = NULL;
570 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
573 if (info->instance_id == instance) {
574 memcpy(&adv->data, info->adv_data.data, info->adv_data_len);
575 *len = info->adv_data_len;
581 void _bt_get_previous_scan_rsp_data(bluetooth_scan_resp_data_t *scan, int *len, int instance)
585 bt_service_app_info_t *info = NULL;
587 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
590 if (info->instance_id == instance) {
591 memcpy(&scan->data, info->scan_rsp.data, info->scan_rsp_len);
592 *len = info->scan_rsp_len;
598 static int __bt_unregister_gatt_server_instance(int server_instance)
600 int ret = OAL_STATUS_SUCCESS;
603 /* Unregister the server instance */
604 ret = gatts_unregister(server_instance);
605 if (ret != OAL_STATUS_SUCCESS) {
606 BT_ERR("DeAllocate server instance with stack Fail ret: %d", ret);
607 return BLUETOOTH_ERROR_INTERNAL;
609 BT_INFO("DeAllocated server instance with stack successful..");
612 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
613 if (numapps[k].instance_id == server_instance) {
614 numapps[k].is_initialized = 0;
615 numapps[k].instance_id = -1;
616 numapps[k].adv_handle = 0;
617 numapps[k].adv_instance = -1;
618 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
619 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
620 memset(numapps[k].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
621 memset(numapps[k].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
622 numapps[k].adv_data_len = 0;
623 numapps[k].scan_rsp_len = 0;
627 BT_DBG("Going8 to reset numapp block num [%d]", k);
628 return BLUETOOTH_ERROR_NONE;
631 static void __bt_remove_all_service_handles(int instance_id)
635 int ret = OAL_STATUS_SUCCESS;
638 count = g_slist_length(numapps[instance_id].service_handles);
639 BT_INFO("Before handle removal: current count [%d]", count);
641 for (l = numapps[instance_id].service_handles; l != NULL;) {
646 BT_INFO("Server Handle to be Removed [%d] Instance ID [%d]", *handle, instance_id);
647 if (_bt_gatt_server_stop_service(numapps[instance_id].sender, *handle, instance_id) != BLUETOOTH_ERROR_NONE)
650 ret = gatts_delete_service(instance_id, *handle);
651 if (ret != OAL_STATUS_SUCCESS) {
652 BT_ERR("ret: %d", ret);
655 numapps[instance_id].service_handles = g_slist_remove(numapps[instance_id].service_handles, handle);
658 count = g_slist_length(numapps[instance_id].service_handles);
659 BT_INFO("After deleting current count [%d]", count);
665 int _bt_unregister_server_instance(const char *sender, int adv_handle)
667 BT_INFO("Unregister Allocated server instance request Sender [%s] Adv handle [%d]", sender, adv_handle);
668 int result = BLUETOOTH_ERROR_NONE;
669 int apps[MAX_APPS_SUPPORTED];
673 memset(&apps, -1, sizeof(apps));
675 if (adv_handle == 0) {
676 BT_DBG("Its a direct GATT Server app request to unregister");
677 /* Unregister server instance for each app with same sender (case: GATT Server with multiple adv handle) */
678 bt_service_app_info_t *info = NULL;
680 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
683 /* Exact matching of sender */
684 if (!g_strcmp0(info->sender, sender)) {
685 BT_INFO("Unregister GATT server instance [%d]", info->instance_id);
686 /* Unregister all service handles with stack */
687 __bt_remove_all_service_handles(info->instance_id);
689 /* Disable adv if running */
690 BT_INFO("Disable Advertising Adv Handle [%d] sender [%s]",
691 info->adv_handle, sender);
692 _bt_set_advertising(sender, info->adv_handle, FALSE, FALSE);
694 /* Save all instances which need to be unregistered */
695 apps[info->instance_id] = 1;
699 BT_DBG("Its an Internal unregister request by adv application");
700 server_instance = _bt_get_allocated_server_instance(sender, adv_handle, FALSE);
701 BT_DBG("Its an Internal unregister request by adv application: Adv disabled srv instance [%d]", server_instance);
702 if (server_instance == -1) {
703 BT_ERR("No allocated server instance to be removed");
704 return BLUETOOTH_ERROR_INVALID_PARAM;
706 if (!numapps[server_instance].service_handles) {
707 BT_INFO("There are no Service handles with this app, safe to unregister");
708 /* Unregister server instance only if this sender does not have any gatt services in it */
709 result = __bt_unregister_gatt_server_instance(server_instance);
711 numapps[server_instance].adv_handle = 0;
712 memset(numapps[server_instance].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
713 numapps[server_instance].adv_data_len = 0;
714 memset(numapps[server_instance].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
715 numapps[server_instance].scan_rsp_len = 0;
720 /* Unregister all apps one by one */
721 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
723 BT_INFO("Unregister app[%d]", k);
724 /* Unregister server instance */
725 __bt_unregister_gatt_server_instance(k);
732 int _bt_register_server_instance(const char *sender, int adv_handle)
734 int ret = OAL_STATUS_SUCCESS;
735 char *uuid_string = NULL;
740 BT_INFO("###Check on which instance Server instance can be initialized....");
741 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
742 if (numapps[k].is_initialized == 1) {
743 BT_INFO("Instance ID [%d] is already in use..Check next slot", numapps[k].instance_id);
746 BT_INFO("Time to register GATT Server..UUID to be used is [%s] slot [%d]", uuid_list[slot-1], slot);
752 BT_ERR("No Slot if free for GATT Server registration..");
753 return BLUETOOTH_ERROR_REGISTRATION_FAILED;
756 uuid_string = g_malloc0(BT_UUID_STRING_MAX);
757 _bt_string_to_uuid(uuid_list[slot-1], (service_uuid_t*)&uuid);
758 g_strlcpy(uuid_string, uuid_list[slot-1], BT_UUID_STRING_MAX);
759 BT_INFO("Copied UUID string [%s]", uuid_string);
761 /* Register GATT Server */
762 ret = gatts_register(&uuid);
763 if (ret != OAL_STATUS_SUCCESS) {
764 BT_ERR("ret: %d", ret);
766 return BLUETOOTH_ERROR_INTERNAL;
768 BT_INFO("GATT Server registration call successfully accepted by OAL..wait for Instance Initialized event from OAL..");
769 /* Return & wait for GATT Server Instance Initialization event */
770 memset(numapps[slot].sender, 0x00, sizeof(numapps[slot].sender));
771 memset(numapps[slot].uuid, 0x00, sizeof(numapps[slot].uuid));
773 g_strlcpy(numapps[slot].sender, sender, sizeof(numapps[slot].sender));
774 g_strlcpy(numapps[slot].uuid, uuid_string, sizeof(numapps[slot].uuid));
776 numapps[slot].is_initialized = 0; /* Set initialization from app registered callback */
777 numapps[slot].adv_handle = adv_handle;
780 return BLUETOOTH_ERROR_NONE;
784 static void __bt_gatt_handle_pending_request_info(int result,
785 int service_function, void *data, unsigned int size)
789 invocation_info_t *req_info = NULL;
790 ret_if(data == NULL);
793 for (l = _bt_get_invocation_list(); l != NULL; ) {
796 if (req_info == NULL || req_info->service_function != service_function)
799 switch (service_function) {
801 case BT_GATT_SERVER_REGISTER: {
802 bt_service_app_info_t *param = (bt_service_app_info_t*)data;
803 BT_INFO("Sender [%s] Request Sender [%s]", param->sender, req_info->sender);
804 if (!g_strcmp0(req_info->sender, param->sender)) {
805 BT_DBG("GATT Server app found [%s]", req_info->sender);
807 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
808 g_array_append_vals(out_param, ¶m->instance_id, sizeof(int));
809 _bt_service_method_return(req_info->context, out_param, result);
811 g_free(req_info->user_data);
812 _bt_free_info_from_invocation_list(req_info);
813 g_array_free(out_param, TRUE);
817 case BT_GATT_SERVER_START_SERVICE:
818 case BT_GATT_SERVER_DELETE_SERVICE: {
819 bt_service_app_info_t *param = (bt_service_app_info_t*)data;
820 BT_INFO("Sender [%s] Request Sender [%s]", param->sender, req_info->sender);
822 int *saved_instance_id = (int*)req_info->user_data;
823 if (!g_strcmp0(req_info->sender, param->sender) && param->instance_id == *saved_instance_id) {
824 BT_DBG("GATT Server app found [%s] Instance ID [%d] Reply DBUS",
825 req_info->sender, *saved_instance_id);
827 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
828 g_array_append_vals(out_param, &saved_instance_id, sizeof(int));
829 _bt_service_method_return(req_info->context, out_param, result);
831 g_free(req_info->user_data);
832 _bt_free_info_from_invocation_list(req_info);
833 g_array_free(out_param, TRUE);
837 case BT_GATT_SERVER_ADD_SERVICE:
838 case BT_GATT_SERVER_ADD_DESCRIPTOR:
839 case BT_GATT_SERVER_ADD_CHARACTERISTIC: {
840 int *handle = (int*)data;
841 BT_INFO("Characteristic added: Handle [%d]", *handle);
842 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
844 g_array_append_vals(out_param, handle, sizeof(int));
845 _bt_service_method_return(req_info->context, out_param, result);
847 g_free(req_info->user_data);
848 _bt_free_info_from_invocation_list(req_info);
849 g_array_free(out_param, TRUE);
854 case BT_DISCONNECT_LE: {
855 #ifdef TIZEN_GATT_CLIENT
856 char *addr = (char*)req_info->user_data;
857 bluetooth_device_address_t address;
859 if (!g_strcmp0(addr, (char*)data)) {
860 BT_INFO("GATT Client connect-disconnect call pending for app [%s] addr [%s]",
861 req_info->sender, addr);
862 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
863 _bt_convert_addr_string_to_type(address.addr, addr);
865 g_array_append_vals(out_param, (bluetooth_device_address_t*)&address,
866 sizeof(bluetooth_device_address_t));
867 _bt_service_method_return(req_info->context, out_param, result);
869 g_free(req_info->user_data);
870 _bt_free_info_from_invocation_list(req_info);
871 g_array_free(out_param, TRUE);
874 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
876 g_array_append_vals(out_param, (bluetooth_device_address_t*)data,
877 sizeof(bluetooth_device_address_t));
878 _bt_service_method_return(req_info->context, out_param, result);
880 g_free(req_info->user_data);
881 _bt_free_info_from_invocation_list(req_info);
883 g_array_free(out_param, TRUE);
887 #ifdef TIZEN_GATT_CLIENT
888 case BT_GATT_CLIENT_REGISTER: {
889 bt_service_app_info_t *param = (bt_service_app_info_t*)data;
890 BT_INFO("Sender [%s] Request Sender [%s]", param->sender, req_info->sender);
891 if (!g_strcmp0(req_info->sender, param->sender)) {
892 BT_DBG("GATT Client app found [%s] created client ID [%d]",
893 req_info->sender, param->client_id);
895 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
896 g_array_append_vals(out_param, ¶m->client_id, sizeof(int));
897 _bt_service_method_return(req_info->context, out_param, result);
899 g_free(req_info->user_data);
900 _bt_free_info_from_invocation_list(req_info);
901 g_array_free(out_param, TRUE);
905 case BT_GATT_GET_PRIMARY_SERVICES: {
906 bt_services_browse_info_t *param = (bt_services_browse_info_t*)data;
907 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
908 _bt_convert_addr_type_to_string(address,
909 (unsigned char *)(¶m->device_addr.addr));
911 BT_INFO("Request Sender [%s] address [%s]", req_info->sender, address);
913 /* Match address to determine same request */
914 if (!g_strcmp0((char*)req_info->user_data, address)) {
915 BT_DBG("GATT Client app found [%s] Remote address [%s]",
916 req_info->sender, address);
918 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
919 g_array_append_vals(out_param, param,
920 sizeof(bt_services_browse_info_t));
922 //g_array_append_vals(out_param, ¶m->client_id, sizeof(int));
923 _bt_service_method_return(req_info->context, out_param, result);
925 g_free(req_info->user_data);
926 _bt_free_info_from_invocation_list(req_info);
927 g_array_free(out_param, TRUE);
932 case BT_GATT_GET_SERVICE_PROPERTIES: {
933 bt_char_browse_info_t param;
934 memcpy((void*)¶m, data, sizeof(bt_char_browse_info_t));
935 //bt_char_browse_info_t *param = (bt_char_browse_info_t*)data;
936 BT_INFO("Request Sender [%s]", req_info->sender);
937 bluetooth_gatt_client_svc_prop_info_t *prop = (bluetooth_gatt_client_svc_prop_info_t*)req_info->user_data;
939 /* Match both address and service properties to determine same request */
940 if (!memcmp(param.device_addr.addr,
941 prop->device_address.addr,
942 sizeof(bluetooth_device_address_t)) &&
943 !memcmp(param.svc_uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
944 param.svc_inst_id == prop->svc.instance_id) {
945 BT_INFO("Returning Service properties");
947 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
948 g_array_append_vals(out_param, ¶m, sizeof(bt_char_browse_info_t));
949 _bt_service_method_return(req_info->context, out_param, result);
951 g_free(req_info->user_data);
952 _bt_free_info_from_invocation_list(req_info);
953 g_array_free(out_param, TRUE);
957 case BT_GATT_GET_CHARACTERISTIC_PROPERTIES: {
958 bt_descriptor_browse_info_t *param = (bt_descriptor_browse_info_t*)data;
960 BT_INFO("Request Sender [%s]", req_info->sender);
961 bluetooth_gatt_client_char_prop_info_t *prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
963 /* Match both address, service properties &char properties to determine same request */
964 if (!memcmp(param->device_addr.addr,
965 prop->device_address.addr,
966 sizeof(bluetooth_device_address_t)) &&
967 !memcmp(param->svc_uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
968 param->svc_inst_id == prop->svc.instance_id &&
969 !memcmp(param->char_uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
970 param->char_inst_id == prop->characteristic.instance_id) {
971 BT_INFO("Returning Characteristic properties");
972 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
973 g_array_append_vals(out_param, param, sizeof(bt_descriptor_browse_info_t));
974 _bt_service_method_return(req_info->context, out_param, result);
976 g_free(req_info->user_data);
977 _bt_free_info_from_invocation_list(req_info);
978 g_array_free(out_param, TRUE);
982 case BT_GATT_WATCH_CHARACTERISTIC: {
983 bt_gatt_notif_reg_info_t *param = (bt_gatt_notif_reg_info_t*)data;
984 bluetooth_gatt_client_char_prop_info_t *prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
985 BT_INFO("Request Sender [%s]", req_info->sender);
986 /* Match both address, service properties &char properties to determine same request */
987 if (!memcmp(param->addr.addr,
988 prop->device_address.addr,
989 sizeof(bluetooth_device_address_t)) &&
990 !memcmp(param->svc_uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
991 param->svc_inst == prop->svc.instance_id &&
992 !memcmp(param->char_uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
993 param->char_inst == prop->characteristic.instance_id) {
994 BT_INFO("Characteristic Watch Successful: Is registered [%d]",
995 param->is_registered);
996 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
997 g_array_append_vals(out_param, param, sizeof(bt_gatt_notif_reg_info_t));
998 _bt_service_method_return(req_info->context, out_param, result);
999 g_free(req_info->user_data);
1000 _bt_free_info_from_invocation_list(req_info);
1001 g_array_free(out_param, TRUE);
1005 case BT_GATT_READ_CHARACTERISTIC:
1006 case BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE: {
1007 bluetooth_gatt_client_char_prop_info_t *param = (bluetooth_gatt_client_char_prop_info_t*)data;
1008 BT_INFO("Request Sender [%s]", req_info->sender);
1010 char *addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
1011 bluetooth_gatt_client_char_prop_info_t *prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
1012 _bt_convert_addr_type_to_string(addr, prop->device_address.addr);
1014 if (!memcmp(param, prop, sizeof(bluetooth_gatt_client_char_prop_info_t))) {
1015 BT_INFO("Gatt Char read or write request matched for address [%s]", addr);
1016 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1017 g_array_append_vals(out_param, param, sizeof(bluetooth_gatt_client_char_prop_info_t));
1018 _bt_service_method_return(req_info->context, out_param, result);
1020 g_free(req_info->user_data);
1021 _bt_free_info_from_invocation_list(req_info);
1022 g_array_free(out_param, TRUE);
1027 case BT_GATT_READ_DESCRIPTOR_VALUE:
1028 case BT_GATT_WRITE_DESCRIPTOR_VALUE: {
1029 bluetooth_gatt_client_desc_prop_info_t *param = (bluetooth_gatt_client_desc_prop_info_t*)data;
1030 BT_INFO("Request Sender [%s]", req_info->sender);
1032 char *addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
1033 bluetooth_gatt_client_desc_prop_info_t *prop = (bluetooth_gatt_client_desc_prop_info_t*)req_info->user_data;
1034 _bt_convert_addr_type_to_string(addr, prop->device_address.addr);
1036 if (!memcmp(param, prop, sizeof(bluetooth_gatt_client_desc_prop_info_t))) {
1037 BT_INFO("Descriptor read or write request matched for address [%s]", addr);
1038 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1039 g_array_append_vals(out_param, param, sizeof(bluetooth_gatt_client_desc_prop_info_t));
1040 _bt_service_method_return(req_info->context, out_param, result);
1042 g_free(req_info->user_data);
1043 _bt_free_info_from_invocation_list(req_info);
1044 g_array_free(out_param, TRUE);
1057 static void __bt_handle_server_instance_registered(event_gatts_register_t *data)
1059 bt_service_app_info_t *info = NULL;
1061 char *uuid_string = g_malloc0(BT_UUID_STRING_MAX);
1063 _bt_uuid_to_string(&(data->server_uuid), uuid_string);
1064 BT_INFO("Instance ID is Intialized [%d] UUID initialized [%s]", data->server_inst, uuid_string);
1066 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1067 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1070 if (g_strcmp0(info->uuid, uuid_string) == 0) {
1071 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1072 info->is_initialized = TRUE;
1073 info->instance_id = data->server_inst;
1074 info->adv_instance = data->server_inst;
1075 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE, BT_GATT_SERVER_REGISTER,
1076 (void*)info, sizeof(bt_service_app_info_t));
1080 g_free(uuid_string);
1083 static void __bt_handle_gatt_server_service_added(event_gatts_srvc_prm_t *event)
1085 int result = BLUETOOTH_ERROR_NONE;
1089 bt_service_app_info_t *info = NULL;
1092 char *uuid_str = g_malloc0(BT_UUID_STRING_MAX);
1093 _bt_uuid_to_string(&(event->gatt_srvc_id.id.uuid), uuid_str);
1094 BT_INFO("GATT Added Service UUID: [%s] Server Ins [%d] Service hdl [%d] Is Primary [%d]\n",
1095 uuid_str, event->gatt_srvc_stat.server_inst, svc_handle, event->gatt_srvc_id.is_prmry);
1097 if (event->gatt_srvc_stat.status != OAL_STATUS_SUCCESS) {
1098 result = BLUETOOTH_ERROR_INTERNAL;
1099 svc_handle = 0; /* Service handle set to 0 indicates.
1100 0 is reserved by BT SIG, cant be used by app*/
1102 svc_handle = event->gatt_srvc_stat.servic_hndl;
1104 BT_INFO("GATT Added Service Status [%d] Server Ins [%d] Service hdl [%d] Is Primary [%d]\n",
1105 event->gatt_srvc_stat.status, event->gatt_srvc_stat.server_inst, svc_handle, event->gatt_srvc_id.is_prmry);
1107 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1108 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1111 if (info->instance_id == event->gatt_srvc_stat.server_inst) {
1112 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1113 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_ADD_SERVICE,
1114 (int*)&svc_handle, sizeof(int));
1116 /* Add Service Handle */
1117 if (svc_handle > 0) {
1118 handle = g_malloc0(sizeof(int));
1119 *handle = svc_handle;
1120 numapps[event->gatt_srvc_stat.server_inst].service_handles = g_slist_append(numapps[event->gatt_srvc_stat.server_inst].service_handles, handle);
1121 count = g_slist_length(numapps[event->gatt_srvc_stat.server_inst].service_handles);
1122 BT_INFO("Added Service handle [%d] to list..current count [%d]", svc_handle, count);
1131 static void __bt_handle_gatt_server_characteristic_added(event_gatts_srvc_charctr_t *event)
1133 int result = BLUETOOTH_ERROR_NONE;
1135 bt_service_app_info_t *info = NULL;
1137 char *uuid_str = g_malloc0(BT_UUID_STRING_MAX);
1139 BT_INFO("GATT Server Char added status [%d]", event->gatt_srvc_stat.status);
1140 BT_INFO("GATT Server_inst: [%d]", event->gatt_srvc_stat.server_inst);
1141 BT_INFO("GATT Service Handle: [%d]", event->gatt_srvc_stat.servic_hndl);
1142 BT_INFO("GATT Add characteristic Status: [%d]", event->gatt_srvc_stat.status);
1143 BT_INFO("GATT Service characteristic Handle: [%d]", event->charctr_hndl);
1145 if (event->gatt_srvc_stat.status != OAL_STATUS_SUCCESS) {
1146 result = BLUETOOTH_ERROR_INTERNAL;
1147 char_handle = 0; /* characteristic handle set to 0 indicates.
1148 0 is reserved by BT SIG, cant be used by app*/
1150 char_handle = event->charctr_hndl;
1153 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1154 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1157 if (info->instance_id == event->gatt_srvc_stat.server_inst) {
1158 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1159 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_ADD_CHARACTERISTIC,
1160 (int*)&char_handle, sizeof(int));
1166 _bt_uuid_to_string(&(event->charctr_uuid), uuid_str);
1167 BT_INFO("GATT Added Characteristic: UUID: [%s]", uuid_str);
1172 static void __bt_handle_gatt_server_descriptor_added(event_gatts_srvc_descr_t* event)
1174 int result = BLUETOOTH_ERROR_NONE;
1176 bt_service_app_info_t *info = NULL;
1178 char *uuid_str = g_malloc0(BT_UUID_STRING_MAX);
1180 BT_INFO("GATT Server Descriptor added status [%d]", event->gatt_srvc_stat.status);
1181 BT_INFO("GATT Server_inst: [%d]", event->gatt_srvc_stat.server_inst);
1182 BT_INFO("GATT Service Handle: [%d]", event->gatt_srvc_stat.servic_hndl);
1183 BT_INFO("GATT Add Descriptor Status: [%d]", event->gatt_srvc_stat.status);
1184 BT_INFO("GATT Service Descriptor Handle: [%d]", event->descrptr_hndl);
1186 if (event->gatt_srvc_stat.status != OAL_STATUS_SUCCESS) {
1187 result = BLUETOOTH_ERROR_INTERNAL;
1188 desc_handle = 0; /* Service handle set to 0 indicates.
1189 0 is reserved by BT SIG, cant be used by app*/
1191 desc_handle = event->descrptr_hndl;
1193 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1194 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1197 if (info->instance_id == event->gatt_srvc_stat.server_inst) {
1198 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1199 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_ADD_DESCRIPTOR,
1200 (int*)&desc_handle, sizeof(int));
1205 _bt_uuid_to_string(&(event->descrptr_uuid), uuid_str);
1206 BT_INFO("GATT Added Descriptor: UUID: [%s]", uuid_str);
1211 static void __bt_handle_gatt_server_service_started(event_gatts_srvc_t *event)
1213 bt_service_app_info_t *info = NULL;
1214 int result = BLUETOOTH_ERROR_NONE;
1216 BT_INFO("GATT Server Service Started..");
1219 BT_INFO("GATT Server_inst: [%d]", event->server_inst);
1220 BT_INFO("GATT Service Handle: [%d]", event->servic_hndl);
1221 BT_INFO("GATT Service Started Status: [%d]", event->status);
1223 if (event->status != OAL_STATUS_SUCCESS) {
1224 BT_ERR("GATT Server Service Start Failed Err: [%d]", event->status);
1225 result = BLUETOOTH_ERROR_INTERNAL;
1228 /* Check if the just registered Instance ID belongs to requester */
1229 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1232 if (info->instance_id == event->server_inst) {
1233 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1234 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_START_SERVICE,
1235 (void*)info, sizeof(bt_service_app_info_t));
1241 static void __bt_handle_gatt_server_service_stopped(event_gatts_srvc_t *event)
1243 int result = BLUETOOTH_ERROR_NONE;
1244 bt_service_app_info_t *info = NULL;
1246 BT_INFO("GATT Server Service Stopped..");
1248 BT_INFO("GATT Server_inst: [%d]", event->server_inst);
1249 BT_INFO("GATT Service Handle: [%d]", event->servic_hndl);
1250 BT_INFO("GATT Service Stopped Status: [%d]", event->status);
1252 if (event->status != OAL_STATUS_SUCCESS) {
1253 BT_ERR("GATT Server Service Stop Failed Err: [%d]", event->status);
1254 result = BLUETOOTH_ERROR_INTERNAL;
1257 /* Check if the just registered Instance ID belongs to requester */
1258 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1261 if (info->instance_id == event->server_inst) {
1262 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1263 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_STOP_SERVICE,
1264 (void*)info, sizeof(bt_service_app_info_t));
1270 static void __bt_handle_gatt_server_service_deleted(event_gatts_srvc_t *event)
1272 int result = BLUETOOTH_ERROR_NONE;
1273 bt_service_app_info_t *info = NULL;
1275 BT_INFO("GATT Server Service Deleted..");
1277 BT_INFO("GATT Server_inst: [%d]", event->server_inst);
1278 BT_INFO("GATT Service Handle: [%d]", event->servic_hndl);
1279 BT_INFO("GATT Service Deleted Status: [%d]", event->status);
1281 if (event->status != OAL_STATUS_SUCCESS) {
1282 BT_ERR("GATT Server Service Delete Failed Err: [%d]", event->status);
1283 result = BLUETOOTH_ERROR_INTERNAL;
1286 /* Check if the just registered Instance ID belongs to requester */
1287 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1290 if (info->instance_id == event->server_inst) {
1291 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1292 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_DELETE_SERVICE,
1293 (void*)info, sizeof(bt_service_app_info_t));
1299 static struct gatt_client_info_t *__bt_find_remote_gatt_client_info(char *address)
1302 struct gatt_client_info_t *info = NULL;
1303 for (l = gatt_client_info_list; l != NULL; l = g_slist_next(l)) {
1304 info = (struct gatt_client_info_t*)l->data;
1308 if (!g_strcmp0(info->addr, address)) {
1309 BT_INFO("Remote GATT client found addr[%s]", info->addr);
1316 static struct gatt_client_info_t *__bt_find_remote_gatt_client_info_from_conn_id(int conn_id)
1319 struct gatt_client_info_t *info = NULL;
1321 for (l = gatt_client_info_list; l != NULL; l = g_slist_next(l)) {
1322 info = (struct gatt_client_info_t*)l->data;
1326 if (info->connection_id == conn_id) {
1327 BT_INFO("Remote GATT client found addr[%s]", info->addr);
1334 #ifdef TIZEN_GATT_CLIENT
1335 static struct gatt_server_info_t *__bt_find_remote_gatt_server_info(char *address)
1338 struct gatt_server_info_t *info = NULL;
1339 for (l = gatt_server_info_list; l != NULL; l = g_slist_next(l)) {
1340 info = (struct gatt_server_info_t*)l->data;
1344 if (!g_strcmp0(info->addr, address)) {
1345 BT_INFO("Remote GATT Server found addr[%s]", info->addr);
1352 static struct gatt_out_conn_info_t* __bt_find_gatt_outgoing_conn_info(char *address)
1355 struct gatt_out_conn_info_t *info = NULL;
1356 for (l = outgoing_gatt_conn_list; l != NULL; l = g_slist_next(l)) {
1357 info = (struct gatt_out_conn_info_t*)l->data;
1361 if (!g_strcmp0(info->addr, address)) {
1362 BT_INFO("Outgoing connection info found addr[%s]", info->addr);
1369 static struct gatt_server_info_t *__bt_find_remote_gatt_server_info_from_client_if(int client_if)
1372 struct gatt_server_info_t *info = NULL;
1374 for (l = gatt_server_info_list; l != NULL; l = g_slist_next(l)) {
1375 info = (struct gatt_server_info_t*)l->data;
1379 if (info->client_id == client_if) {
1380 BT_INFO("Remote GATT server found addr[%s]", info->addr);
1391 #ifdef TIZEN_GATT_CLIENT
1392 static void __bt_handle_gatt_server_connection_state(event_gatts_conn_t *event)
1394 int result = BLUETOOTH_ERROR_NONE;
1395 struct gatt_client_info_t *conn_info = NULL;
1396 bluetooth_device_address_t dev_addr;
1397 GVariant *param = NULL;
1399 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1401 memcpy(dev_addr.addr, event->address.addr, 6);
1403 /* REPLY dbus Context to application which called BT_CONNECT_LE. There is status
1405 _bt_convert_addr_type_to_string(address,
1406 (unsigned char *)dev_addr.addr);
1408 if (event->status != OAL_STATUS_SUCCESS)
1409 result = BLUETOOTH_ERROR_INTERNAL;
1411 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
1412 address, BT_ADDRESS_STRING_SIZE);
1414 BT_INFO("GATT Server Connedted: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1415 address, event->server_inst, event->conn_id);
1418 /* Check if device is already in connected list */
1419 conn_info = __bt_find_remote_gatt_client_info(address);
1422 BT_INFO("Conn Info absent: But no need to Send Local GATT Server Connected event to apps");
1424 param = g_variant_new("(is)", result, address);
1426 /* Send event to application */
1427 _bt_send_event(BT_DEVICE_EVENT,
1428 BLUETOOTH_EVENT_GATT_SERVER_CONNECTED, /* Local device is GATT server */
1431 /* Save Connection info */
1432 conn_info = g_new0(struct gatt_client_info_t, 1);
1433 conn_info->addr = g_strdup(address);
1434 BT_INFO("Added GATT client addr[%s]", conn_info->addr);
1435 conn_info->connection_id = event->conn_id;
1436 conn_info->instance_id = event->server_inst;
1437 gatt_client_info_list = g_slist_append(gatt_client_info_list, conn_info);
1438 BT_INFO("Total num of connected Remote GATT Clients [%d]", g_slist_length(gatt_client_info_list));
1444 /* GATT Server Dis connected */
1445 static void __bt_handle_gatt_server_disconnection_state(event_gatts_conn_t *event)
1447 int result = BLUETOOTH_ERROR_NONE;
1448 struct gatt_client_info_t *conn_info = NULL;
1449 bluetooth_device_address_t dev_addr;
1450 GVariant *param = NULL;
1451 char address[BT_ADDRESS_STRING_SIZE];
1453 memcpy(dev_addr.addr, event->address.addr, 6);
1455 /* REPLY dbus Context to application which called BT_DISCONNECT_LE. There is status
1457 _bt_convert_addr_type_to_string(address,
1458 (unsigned char *)dev_addr.addr);
1460 if (event->status != OAL_STATUS_SUCCESS)
1461 result = BLUETOOTH_ERROR_INTERNAL;
1463 if (NULL == _bt_get_request_info_data(BT_DISCONNECT_LE, address)) {
1464 if (NULL != _bt_get_request_info_data(BT_CONNECT_LE, address)) {
1465 result = BLUETOOTH_ERROR_INTERNAL;
1467 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
1468 address, BT_ADDRESS_STRING_SIZE);
1470 BT_ERR("Failed to connect Local GATT Server Remote Client addr[%s]", address);
1474 __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE,
1475 address, BT_ADDRESS_STRING_SIZE);
1478 BT_INFO("Local GATT Server DisConnected: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1479 address, event->server_inst, event->conn_id);
1480 /* Remove Connection info */
1481 conn_info = __bt_find_remote_gatt_client_info(address);
1483 BT_INFO("No need to Send Local GATT Server Disconnected event to apps, just remove remote client info");
1485 param = g_variant_new("(is)", result, address);
1486 /* Send event to application */
1487 _bt_send_event(BT_DEVICE_EVENT,
1488 BLUETOOTH_EVENT_GATT_SERVER_DISCONNECTED, /* Local device is GATT server */
1491 /* Remove info from List */
1492 gatt_client_info_list = g_slist_remove(gatt_client_info_list, conn_info);
1493 BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
1494 g_free(conn_info->addr);
1500 static void __bt_handle_gatt_server_connection_state(event_gatts_conn_t *event)
1502 int result = BLUETOOTH_ERROR_NONE;
1503 struct gatt_client_info_t *conn_info = NULL;
1504 bt_service_app_info_t *info = NULL;
1505 bluetooth_device_address_t dev_addr;
1506 GVariant *param = NULL;
1509 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1511 memcpy(dev_addr.addr, event->address.addr, 6);
1514 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1515 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1518 if (info->instance_id == event->server_inst) {
1519 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1520 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
1521 (bluetooth_device_address_t*)&dev_addr, sizeof(bluetooth_device_address_t));
1527 /* Send event to BT-API */
1528 _bt_convert_addr_type_to_string(address,
1529 (unsigned char *)dev_addr.addr);
1531 BT_INFO("GATT Server Connedted: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1532 address, event->server_inst, event->conn_id);
1535 /* Check if device is already in connected list */
1536 conn_info = __bt_find_remote_gatt_client_info(address);
1539 param = g_variant_new("(is)", result, address);
1540 /* Send event to application */
1541 _bt_send_event(BT_GATT_SERVER_EVENT,
1542 BLUETOOTH_EVENT_GATT_CONNECTED,
1545 /* Save Connection info */
1546 conn_info = g_new0(struct gatt_client_info_t, 1);
1547 conn_info->addr = g_strdup(address);
1548 BT_INFO("Added GATT client addr[%s]", conn_info->addr);
1549 conn_info->connection_id = event->conn_id;
1550 conn_info->instance_id = event->server_inst;
1551 gatt_client_info_list = g_slist_append(gatt_client_info_list, conn_info);
1552 BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
1558 /* GATT Server Dis connected */
1559 static void __bt_handle_gatt_server_disconnection_state(event_gatts_conn_t *event)
1561 int result = BLUETOOTH_ERROR_NONE;
1562 struct gatt_client_info_t *conn_info = NULL;
1563 bluetooth_device_address_t dev_addr;
1564 bt_service_app_info_t *info = NULL;
1565 GVariant *param = NULL;
1568 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1570 memcpy(dev_addr.addr, event->address.addr, 6);
1572 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1573 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1576 if (info->instance_id == event->server_inst) {
1577 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1578 __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE,
1579 (bluetooth_device_address_t*)&dev_addr, sizeof(bluetooth_device_address_t));
1585 /* Send event to BT-API */
1586 _bt_convert_addr_type_to_string(address,
1587 (unsigned char *)dev_addr.addr);
1589 BT_INFO("GATT Server DisConnected: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1590 address, event->server_inst, event->conn_id);
1592 param = g_variant_new("(is)", result, address);
1593 /* Send event to application */
1594 _bt_send_event(BT_GATT_SERVER_EVENT,
1595 BLUETOOTH_EVENT_GATT_DISCONNECTED,
1598 /* Remove Connection info */
1599 conn_info = __bt_find_remote_gatt_client_info(address);
1601 BT_INFO("Remove GATT client info from List..");
1602 /* Remove info from List */
1603 gatt_client_info_list = g_slist_remove(gatt_client_info_list, conn_info);
1604 BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
1605 g_free(conn_info->addr);
1614 static void __bt_handle_gatt_server_acquire_write_requested(event_gatts_srvc_acquire_attr_t *event)
1616 GVariant *param = NULL;
1617 int result = BLUETOOTH_ERROR_NONE;
1618 struct gatt_server_req_info *req_info = NULL;
1619 bluetooth_device_address_t dev_addr;
1620 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1622 BT_INFO("GATT Server ACQUIRE Write Req Connection ID: [%d]", event->attr_trans.conn_id);
1623 BT_INFO("GATT Server ACQUIRE Write Req Transaction ID:[%d]", event->attr_trans.trans_id);
1624 BT_INFO("GATT Server ACQUIRE Write Req Attribute Handle: [%d]", event->attr_trans.attr_handle);
1626 //address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1627 memcpy(dev_addr.addr, event->address.addr, 6);
1629 req_info = g_new0(struct gatt_server_req_info, 1);
1630 req_info->request_id = event->attr_trans.trans_id;
1631 req_info->attribute_handle = event->attr_trans.attr_handle;
1632 req_info->connection_id = event->attr_trans.conn_id;
1633 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_ACQUIRE_WRITE;
1634 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1636 _bt_convert_addr_type_to_string(address,
1637 (unsigned char *)dev_addr.addr);
1639 param = g_variant_new("(iiiiiis)", result,
1640 event->attr_trans.conn_id,
1641 event->attr_trans.trans_id,
1642 event->attr_trans.attr_handle,
1643 event->mtu, event->attr_trans.offset, address);
1644 BT_INFO("GATT Server ACQUIRE Write Req Attribute remote address : [%s]", address);
1646 _bt_send_event(BT_GATT_SERVER_EVENT,
1647 BLUETOOTH_EVENT_GATT_SERVER_ACQUIRE_WRITE,
1652 static void __bt_handle_gatt_server_acquire_notify_requested(event_gatts_srvc_acquire_attr_t *event)
1654 GVariant *param = NULL;
1655 int result = BLUETOOTH_ERROR_NONE;
1656 struct gatt_server_req_info *req_info = NULL;
1657 bluetooth_device_address_t dev_addr;
1658 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1660 BT_INFO("GATT Server ACQUIRE Notify Req Connection ID: [%d]", event->attr_trans.conn_id);
1661 BT_INFO("GATT Server ACQUIRE Notify Req Transaction ID:[%d]", event->attr_trans.trans_id);
1662 BT_INFO("GATT Server ACQUIRE Notify Req Attribute Handle: [%d]", event->attr_trans.attr_handle);
1663 BT_INFO("GATT Server ACQUIRE notify Req address : [%s]", address);
1665 memcpy(dev_addr.addr, event->address.addr, 6);
1666 _bt_convert_addr_type_to_string(address,
1667 (unsigned char *)dev_addr.addr);
1668 BT_INFO("GATT Server ACQUIRE notify Req remote address : [%s]", address);
1670 req_info = g_new0(struct gatt_server_req_info, 1);
1671 req_info->request_id = event->attr_trans.trans_id;
1672 req_info->attribute_handle = event->attr_trans.attr_handle;
1673 req_info->connection_id = event->attr_trans.conn_id;
1674 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_ACQUIRE_NOTIFY;
1675 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1677 param = g_variant_new("(iiiiiis)", result,
1678 event->attr_trans.conn_id,
1679 event->attr_trans.trans_id,
1680 event->attr_trans.attr_handle,
1681 event->mtu, event->attr_trans.offset,
1684 BT_INFO("GATT Server ACQUIRE Notify Req Attribute : ");
1686 _bt_send_event(BT_GATT_SERVER_EVENT,
1687 BLUETOOTH_EVENT_GATT_SERVER_ACQUIRE_NOTIFY,
1691 static void __bt_handle_gatt_server_write_requested(event_gatts_srvc_write_attr_t *event)
1694 bluetooth_device_address_t dev_addr;
1695 GVariant *param = NULL;
1696 int result = BLUETOOTH_ERROR_NONE;
1698 gboolean is_prepare_write;
1700 GVariant *data = NULL;
1702 struct gatt_server_req_info *req_info = NULL;
1703 BT_INFO("GATT Server Write Requested");
1705 memcpy(dev_addr.addr, event->address.addr, 6);
1707 BT_INFO("GATT Server Write Req Connection ID: [%d]", event->attr_trans.conn_id);
1708 BT_INFO("GATT Server Write Req Transaction ID:[%d]", event->attr_trans.trans_id);
1709 BT_INFO("GATT Server Write Req Attribute Handle: [%d]", event->attr_trans.attr_handle);
1710 BT_INFO("GATT Server Write Req Attribute Offset: [%d]", event->attr_trans.offset);
1711 BT_INFO("GATT Server Write Req value length [%d]", event->length);
1712 BT_INFO("GATT Server Write Req needs response: [%d]", event->need_rsp);
1713 BT_INFO("GATT Server Write Req Is Prep: [%d]", event->is_prep);
1715 if (event->length <= 0) {
1716 BT_INFO("GATT Server write requested, but length of attr value is 0");
1720 need_resp = event->need_rsp;
1721 is_prepare_write = event->is_prep;
1723 write_val = g_memdup(&event->value[0], event->length);
1725 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1726 _bt_convert_addr_type_to_string(address,
1727 (unsigned char *)dev_addr.addr);
1729 BT_INFO("GATT Server Write Request from remote client [%s]", address);
1731 if (event->length > 0) {
1732 for (i = 0; i < event->length; i++)
1733 BT_DBG("Data[%d] = [0x%x]", i, event->value[i]);
1735 /* Save Write Request Info */
1736 req_info = g_new0(struct gatt_server_req_info, 1);
1737 req_info->request_id = event->attr_trans.trans_id;
1738 req_info->attribute_handle = event->attr_trans.attr_handle;
1739 req_info->connection_id = event->attr_trans.conn_id;
1740 req_info->addr = address;
1741 req_info->offset = event->attr_trans.offset;
1742 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_WRITE;
1743 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1745 data = g_variant_new_from_data(
1746 G_VARIANT_TYPE_BYTESTRING,
1751 param = g_variant_new("(iiiiiibbsn@ay)", result,
1752 event->attr_trans.conn_id,
1753 event->attr_trans.trans_id,
1754 event->attr_trans.attr_handle,
1755 event->attr_trans.offset,
1763 _bt_send_event(BT_GATT_SERVER_EVENT,
1764 BLUETOOTH_EVENT_GATT_SERVER_VALUE_CHANGED,
1770 static void __bt_handle_gatt_server_read_requested(event_gatts_srvc_read_attr_t *event)
1772 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1773 bluetooth_device_address_t dev_addr;
1774 int result = BLUETOOTH_ERROR_NONE;
1775 struct gatt_server_req_info *req_info = NULL;
1776 GVariant *param = NULL;
1778 BT_INFO("GATT Server Read Requested");
1780 memcpy(dev_addr.addr, event->address.addr, 6);
1782 BT_INFO("GATT Server-Client Connection ID: [%d]", event->attr_trans.conn_id);
1783 BT_INFO("GATT Server-Client Transaction ID: [%d]", event->attr_trans.trans_id);
1784 BT_INFO("GATT Server Attribute Handle: [%d]", event->attr_trans.attr_handle);
1785 BT_INFO("GATT Server Attribute Offset for read: [%d]", event->attr_trans.offset);
1786 BT_INFO("GATT Server Attribute is long: [%d]", event->is_long);
1788 is_long = event->is_long;
1790 /* Save Read Request Info */
1791 req_info = g_new0(struct gatt_server_req_info, 1);
1792 req_info->request_id = event->attr_trans.trans_id;
1793 req_info->attribute_handle = event->attr_trans.attr_handle;
1794 req_info->connection_id = event->attr_trans.conn_id;
1795 req_info->addr = address;
1796 req_info->offset = event->attr_trans.offset;
1797 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_READ;
1798 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1800 /* Send event to BT-API */
1801 _bt_convert_addr_type_to_string(address,
1802 (unsigned char *)dev_addr.addr);
1804 BT_INFO("GATT Server Read Request from remote client [%s]", address);
1806 param = g_variant_new("(iiiiibs)", result,
1807 event->attr_trans.conn_id,
1808 event->attr_trans.trans_id,
1809 event->attr_trans.attr_handle,
1810 event->attr_trans.offset,
1814 _bt_send_event(BT_GATT_SERVER_EVENT,
1815 BLUETOOTH_EVENT_GATT_SERVER_READ_REQUESTED,
1819 static void __bt_handle_gatt_server_indicate_confirmed(event_gatts_ind_cnfrm_t *event)
1821 bluetooth_device_address_t dev_addr;
1822 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1823 int cur_connected_clients;
1824 static int recvd = 0;
1825 gboolean completed = 0;
1826 GVariant *param = NULL;
1828 /* OAL event does provide error, so MW assumes event will never contain wrong data,
1829 incase of any issues, check with OAL */
1830 int result = BLUETOOTH_ERROR_NONE;
1832 memcpy(dev_addr.addr, event->address.addr, 6);
1833 _bt_convert_addr_type_to_string(address,
1834 (unsigned char *)dev_addr.addr);
1836 BT_INFO("Indication sent to GATT client [%s] conn_ ID [%d] transaction ID [%d] Att handle [%d]",
1837 address, event->conn_id, event->trans_id, event->attr_handle);
1840 cur_connected_clients = g_slist_length(gatt_client_info_list);
1841 BT_INFO("Number of connected clients during sending Indication [%d] & current connected count [%d]",
1842 num_indicate_clients, cur_connected_clients);
1845 if (recvd == num_indicate_clients) {
1846 BT_INFO("Gatt indication confirm event for last GATT client.. [%s]", address);
1847 completed = 1; /* Last event */
1848 recvd = 0; /* Reset */
1849 num_indicate_clients = 0;
1852 param = g_variant_new("(isib)",
1858 /* Send event to BT-API */
1859 _bt_send_event(BT_GATT_SERVER_EVENT,
1860 BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_COMPLETED,
1863 BT_INFO("Received Indication confirm for client number [%d]", recvd);
1867 /* Tizen Platform Specific */
1868 static void __bt_handle_gatt_server_notification_changed(event_gatts_notif_t *event)
1870 bluetooth_device_address_t dev_addr;
1871 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1872 GVariant *param = NULL;
1875 /* OAL event does provide error, so MW assumes event will never contain wrong data,
1876 incase of any issues, check with OAL */
1877 int result = BLUETOOTH_ERROR_NONE;
1879 memcpy(dev_addr.addr, event->address.addr, 6);
1880 _bt_convert_addr_type_to_string(address,
1881 (unsigned char *)dev_addr.addr);
1883 BT_INFO("notification_changed [%s] conn_ ID [%d] transaction ID [%d] Att handle [%d] Notify[%d]",
1884 address, event->conn_id, event->trans_id, event->attr_handle, event->notify);
1886 /* Set Notifcation status */
1887 notify = event->notify;
1889 param = g_variant_new("(isib)",
1895 /* Send event to BT-API */
1896 _bt_send_event(BT_GATT_SERVER_EVENT,
1897 BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_STATE_CHANGED,
1903 static void __bt_handle_gatt_mtu_changed_event(event_gatts_mtu_changed_t *event)
1905 int result = BLUETOOTH_ERROR_NONE;
1906 struct gatt_client_info_t *conn_info = NULL;
1907 GVariant *param = NULL;
1909 BT_INFO("GATT Server MTU changed event");
1911 conn_info = __bt_find_remote_gatt_client_info_from_conn_id(event->conn_id);
1912 if (conn_info == NULL) {
1913 BT_ERR("Cant find connection Information");
1916 BT_INFO("Got connection Info GATT client [%s] MTU Size [%d]",
1917 conn_info->addr, event->mtu_size);
1919 param = g_variant_new("(isqy)",
1925 /* Send event to BT-API */
1926 _bt_send_event(BT_GATT_SERVER_EVENT,
1927 BLUETOOTH_EVENT_GATT_ATT_MTU_CHANGED,
1931 static void __bt_gatt_event_handler(int event_type, gpointer event_data)
1933 BT_INFO("OAL event = 0x%x, \n", event_type);
1934 switch (event_type) {
1935 case OAL_EVENT_BLE_SERVER_INSTANCE_INITIALISED: {
1936 BT_INFO("OAL Event: Server Instance Registered");
1937 /* GATT Server Registered event is handled in MAIN thread context */
1938 __bt_handle_server_instance_registered((event_gatts_register_t *)event_data);
1941 case OAL_EVENT_GATTS_SERVICE_ADDED: {
1942 BT_INFO("OAL Event: GATT Service added");
1943 __bt_handle_gatt_server_service_added((event_gatts_srvc_prm_t *)event_data);
1946 case OAL_EVENT_GATTS_CHARACTERISTIC_ADDED: {
1947 BT_INFO("OAL Event: GATT characteristic added");
1948 __bt_handle_gatt_server_characteristic_added((event_gatts_srvc_charctr_t *)event_data);
1951 case OAL_EVENT_GATTS_DESCRIPTOR_ADDED: {
1952 BT_INFO("OAL Event: GATT descriptor added");
1953 __bt_handle_gatt_server_descriptor_added((event_gatts_srvc_descr_t *)event_data);
1956 case OAL_EVENT_GATTS_SERVICE_STARTED: {
1957 BT_INFO("OAL Event: GATT Service started");
1958 __bt_handle_gatt_server_service_started((event_gatts_srvc_t *)event_data);
1961 case OAL_EVENT_GATTS_SERVICE_STOPED: {
1962 BT_INFO("OAL Event: GATT Service stopped");
1963 __bt_handle_gatt_server_service_stopped((event_gatts_srvc_t *)event_data);
1966 case OAL_EVENT_GATTS_SERVICE_DELETED: {
1967 BT_INFO("OAL Event: GATT Service deleted");
1968 __bt_handle_gatt_server_service_deleted((event_gatts_srvc_t *) event_data);
1971 case OAL_EVENT_GATTS_CONNECTION_COMPLETED: {
1972 BT_INFO("OAL Event: GATT Server Connected");
1973 __bt_handle_gatt_server_connection_state((event_gatts_conn_t *)event_data);
1976 case OAL_EVENT_GATTS_DISCONNECTION_COMPLETED: {
1977 BT_INFO("OAL Event: GATT Server Disconnected");
1978 __bt_handle_gatt_server_disconnection_state((event_gatts_conn_t *)event_data);
1981 case OAL_EVENT_GATTS_REQUEST_READ: {
1982 BT_INFO("OAL Event: GATT Server Read Request");
1983 __bt_handle_gatt_server_read_requested((event_gatts_srvc_read_attr_t *)event_data);
1986 case OAL_EVENT_GATTS_REQUEST_WRITE: {
1987 BT_INFO("OAL Event: GATT Server Write Request");
1988 __bt_handle_gatt_server_write_requested((event_gatts_srvc_write_attr_t *)event_data);
1991 case OAL_EVENT_GATTS_REQUEST_ACQUIRE_WRITE: {
1992 BT_INFO("OAL Event: GATT Server Acquire Write Request");
1993 __bt_handle_gatt_server_acquire_write_requested((event_gatts_srvc_acquire_attr_t*)event_data);
1996 case OAL_EVENT_GATTS_REQUEST_ACQUIRE_NOTIFY: {
1997 BT_INFO("OAL Event: GATT ServerAcquire Notify Request");
1998 __bt_handle_gatt_server_acquire_notify_requested((event_gatts_srvc_acquire_attr_t*)event_data);
2001 case OAL_EVENT_GATTS_IND_CONFIRM: {
2002 BT_INFO("OAL Event: GATT Server Indication confirmed");
2003 __bt_handle_gatt_server_indicate_confirmed((event_gatts_ind_cnfrm_t *)event_data);
2006 case OAL_EVENT_GATTS_NOTIFICATION: { /* Tizen Platform Specific */
2007 BT_INFO("OAL Event: GATT Server DisConnected");
2008 __bt_handle_gatt_server_notification_changed((event_gatts_notif_t *)event_data);
2011 case OAL_EVENT_GATTS_MTU_CHANGED: {
2012 BT_INFO("OAL Event: GATT Server MTU changed event callback");
2013 __bt_handle_gatt_mtu_changed_event((event_gatts_mtu_changed_t *)event_data);
2016 #ifdef TIZEN_GATT_CLIENT
2017 case OAL_EVENT_GATTC_REGISTRATION: {
2018 BT_INFO("OAL Event: GATT Client instance Registered");
2019 __bt_handle_client_instance_registered((event_gattc_register_t *) event_data);
2022 case OAL_EVENT_GATTC_CONNECTION_COMPLETED: {
2023 BT_INFO("OAL Event: GATT Client Connected");
2024 __bt_handle_client_connected((event_gattc_conn_t *) event_data);
2027 case OAL_EVENT_GATTC_DISCONNECTION_COMPLETED: {
2028 BT_INFO("OAL Event: GATT Client DisConnected");
2029 __bt_handle_client_disconnected((event_gattc_conn_t *) event_data);
2032 case OAL_EVENT_GATTC_SERVICE_SEARCH_RESULT: {
2033 BT_INFO("OAL Event: GATT Client Service Search Result");
2034 __bt_handle_client_service_search_result((event_gattc_service_result_t *) event_data);
2037 case OAL_EVENT_GATTC_SERVICE_SEARCH_DONE: {
2038 BT_INFO("OAL Event: GATT Client Service Completed");
2039 __bt_handle_client_service_search_completed((event_gattc_conn_status_t *) event_data);
2042 case OAL_EVENT_GATTC_CHARAC_SERACH_RESULT: {
2043 BT_INFO("OAL Event: GATT Client Characteristic Search Result");
2044 __bt_handle_client_characteristic_search_result((event_gattc_characteristic_result_t *) event_data);
2047 case OAL_EVENT_GATTC_DESC_SERACH_RESULT: {
2048 BT_INFO("OAL Event: GATT Client Descriptor Search Result");
2049 __bt_handle_client_descriptor_search_result((event_gattc_descriptor_result_t *) event_data);
2052 case OAL_EVENT_GATTC_READ_CHARAC: {
2053 BT_INFO("OAL Event: GATT Client Characteristic Read Data");
2054 __bt_handle_client_characteristic_read_data((event_gattc_read_data *) event_data);
2057 case OAL_EVENT_GATTC_READ_DESCR: {
2058 BT_INFO("OAL Event: GATT Client Descriptor Read Data");
2059 __bt_handle_client_descriptor_read_data((event_gattc_read_data *) event_data);
2062 case OAL_EVENT_GATTC_WRITE_CHARAC: {
2063 BT_INFO("OAL Event: GATT Client Characteristic Write Data");
2064 __bt_handle_client_characteristic_write_data((event_gattc_write_data *) event_data);
2067 case OAL_EVENT_GATTC_WRITE_DESCR: {
2068 BT_INFO("OAL Event: GATT Client Descriptor Write Data");
2069 __bt_handle_client_descriptor_write_data((event_gattc_write_data *) event_data);
2072 case OAL_EVENT_DEVICE_LE_DISCONNECTED: {
2073 BT_INFO("OAL Event: LE device disconnected");
2074 __bt_hanlde_le_device_disconnection((event_dev_conn_status_t *)event_data);
2077 case OAL_EVENT_GATTC_NOTIFICATION_REGISTERED: {
2078 BT_INFO("OAL Event: GATT Client Notification Registered");
2079 __bt_handle_client_notification_registered((event_gattc_regdereg_notify_t *) event_data, TRUE);
2082 case OAL_EVENT_GATTC_NOTIFICATION_DEREGISTERED: {
2083 BT_INFO("OAL Event: GATT Client Notification Registered");
2084 __bt_handle_client_notification_registered((event_gattc_regdereg_notify_t *) event_data, FALSE);
2087 case OAL_EVENT_GATTC_NOTIFY_DATA: {
2088 BT_INFO("OAL Event: GATT Client Notification Data");
2089 __bt_handle_client_notification_data((event_gattc_notify_data *) event_data);
2099 int _bt_gatt_server_add_service(char *sender, int service_type,
2100 int num_handles, char *svc_uuid, int instance_id)
2102 BT_CHECK_PARAMETER(svc_uuid, return);
2103 BT_CHECK_PARAMETER(sender, return);
2104 int ret = OAL_STATUS_SUCCESS;
2106 oal_gatt_srvc_id_t svc_data;
2108 svc_data.is_prmry = service_type;
2109 svc_data.id.inst_id = instance_id;
2111 BT_INFO("Service UUID [%s] Num handles [%d] Instance ID [%d]", svc_uuid, num_handles, instance_id);
2112 _bt_string_to_uuid(svc_uuid, (service_uuid_t*)&svc_data.id.uuid);
2114 ret = gatts_add_service(instance_id, &svc_data, num_handles);
2115 if (ret != OAL_STATUS_SUCCESS) {
2116 BT_ERR("ret: %d", ret);
2117 return BLUETOOTH_ERROR_INTERNAL;
2120 return BLUETOOTH_ERROR_NONE;
2124 int _bt_gatt_server_add_included_service(char *sender, int instance_id,
2125 int service_handle, int included_svc_handle)
2127 BT_CHECK_PARAMETER(sender, return);
2128 int ret = OAL_STATUS_SUCCESS;
2130 ret = gatts_add_included_services(instance_id, service_handle, included_svc_handle);
2131 if (ret != OAL_STATUS_SUCCESS) {
2132 BT_ERR("ret: %d", ret);
2133 return BLUETOOTH_ERROR_INTERNAL;
2135 return BLUETOOTH_ERROR_NONE;
2138 int _bt_gatt_server_add_characteristic(char *sender, char *char_uuid,
2139 bluetooth_gatt_server_attribute_params_t *param)
2141 BT_CHECK_PARAMETER(char_uuid, return);
2142 BT_CHECK_PARAMETER(sender, return);
2143 BT_CHECK_PARAMETER(param, return);
2144 int ret = OAL_STATUS_SUCCESS;
2146 oal_uuid_t uuid = {{0} };
2148 BT_INFO("Char UUID [%s] Instance ID [%d]", char_uuid, param->instance_id);
2149 _bt_string_to_uuid(char_uuid, (service_uuid_t*)&uuid);
2151 BT_INFO("Char permission From API [0x%x]", param->permissions);
2153 ret = gatts_add_characteristics(param->instance_id, param->service_handle, &uuid,
2154 param->properties, (int)param->permissions);
2155 if (ret != OAL_STATUS_SUCCESS) {
2156 BT_ERR("ret: %d", ret);
2157 return BLUETOOTH_ERROR_INTERNAL;
2159 return BLUETOOTH_ERROR_NONE;
2162 int _bt_gatt_server_add_descriptor(char *sender, char *desc_uuid,
2163 bt_gatt_permission_t *param, int service_handle, int instance_id)
2165 BT_CHECK_PARAMETER(desc_uuid, return);
2166 BT_CHECK_PARAMETER(sender, return);
2167 BT_CHECK_PARAMETER(param, return);
2168 int ret = OAL_STATUS_SUCCESS;
2170 oal_uuid_t uuid = {{0} };
2172 BT_INFO("Descriptor UUID [%s] Instance ID [%d] Service handle [%d]",
2173 desc_uuid, service_handle, instance_id);
2175 _bt_string_to_uuid(desc_uuid, (service_uuid_t*)&uuid);
2177 BT_INFO("Descriptor permission From API [0x%x]", *param);
2178 ret = gatts_add_descriptor(instance_id, service_handle, &uuid, (int)*param);
2180 if (ret != OAL_STATUS_SUCCESS) {
2181 BT_ERR("ret: %d", ret);
2182 return BLUETOOTH_ERROR_INTERNAL;
2184 return BLUETOOTH_ERROR_NONE;
2187 int _bt_gatt_server_start_service(char *sender, int service_handle, int instance_id)
2189 BT_CHECK_PARAMETER(sender, return);
2190 int ret = OAL_STATUS_SUCCESS;
2192 ret = gatts_start_service(instance_id, service_handle, BT_GATT_TRANSPORT_LE);
2193 if (ret != OAL_STATUS_SUCCESS) {
2194 BT_ERR("ret: %d", ret);
2195 return BLUETOOTH_ERROR_INTERNAL;
2197 return BLUETOOTH_ERROR_NONE;
2200 int _bt_gatt_server_stop_service(char *sender, int service_handle, int instance_id)
2202 BT_CHECK_PARAMETER(sender, return);
2203 int ret = OAL_STATUS_SUCCESS;
2205 ret = gatts_stop_service(instance_id, service_handle);
2206 if (ret != OAL_STATUS_SUCCESS) {
2207 BT_ERR("ret: %d", ret);
2208 return BLUETOOTH_ERROR_INTERNAL;
2210 return BLUETOOTH_ERROR_NONE;
2213 int _bt_gatt_server_delete_service(char *sender, int service_handle, int instance_id)
2215 BT_CHECK_PARAMETER(sender, return);
2216 int ret = OAL_STATUS_SUCCESS;
2220 ret = gatts_delete_service(instance_id, service_handle);
2221 if (ret != OAL_STATUS_SUCCESS) {
2222 BT_ERR("ret: %d", ret);
2223 return BLUETOOTH_ERROR_INTERNAL;
2226 /* Remove the Service Handle */
2227 for (l = numapps[instance_id].service_handles; l != NULL;) {
2229 l = g_slist_next(l);
2230 if (handle && *handle == service_handle) {
2231 BT_INFO("Remove Service handle [%d]", *handle);
2232 numapps[instance_id].service_handles = g_slist_remove(numapps[instance_id].service_handles, handle);
2238 return BLUETOOTH_ERROR_NONE;
2241 int _bt_gatt_server_send_response(char *sender, bluetooth_gatt_att_data_t *data,
2242 bluetooth_gatt_server_response_params_t *param)
2244 BT_CHECK_PARAMETER(sender, return);
2245 BT_CHECK_PARAMETER(data, return);
2246 BT_CHECK_PARAMETER(param, return);
2247 struct gatt_server_req_info *req_info = NULL;
2248 int ret = OAL_STATUS_SUCCESS;
2250 oal_gatt_response_t response;
2252 BT_INFO("GATT Server Response: Req Type [%d] req_id [%d] status [%d] auth_req [%d] offset[%d] data len[%d]",
2253 param->req_type, param->request_id,
2254 param->response_status, param->auth_req,
2255 data->offset, data->length);
2257 /* Search for matching Request in List */
2258 req_info = __bt_gatt_server_find_request_info(param->request_id, param->req_type);
2260 BT_ERR("GATT Server Req Info not found for current response..return Error");
2261 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
2264 memset(&response, 0x00, sizeof(oal_gatt_response_t));
2266 response.handle = req_info->attribute_handle;
2267 response.attr_value.auth_req = param->auth_req;
2268 response.attr_value.handle = req_info->attribute_handle;
2269 response.attr_value.offset = data->offset;
2270 response.attr_value.len = data->length;
2271 memcpy(&response.attr_value.value, &data->data, data->length);
2274 ret = gatts_send_response(req_info->connection_id, param->request_id,
2275 param->response_status, &response);
2277 if (ret != OAL_STATUS_SUCCESS) {
2278 BT_ERR("ret: %d", ret);
2279 return BLUETOOTH_ERROR_INTERNAL;
2282 BT_INFO("GATT Server Response successfully sent");
2283 /* Remove GATT server request from list */
2284 gatt_server_requests = g_slist_remove(gatt_server_requests, req_info);
2285 g_free(req_info->addr);
2287 return BLUETOOTH_ERROR_NONE;
2290 int _bt_gatt_server_acquire_send_response(char *sender, bluetooth_gatt_server_acquire_response_params_t *param , void *fd_list)
2292 BT_CHECK_PARAMETER(sender, return);
2293 BT_CHECK_PARAMETER(param, return);
2294 struct gatt_server_req_info *req_info = NULL;
2295 int ret = OAL_STATUS_SUCCESS;
2298 BT_INFO("GATT acquire Server Response: Req Type [%d] req_id [%d] fd [%d] mtu[%d]",
2299 param->req_type, param->request_id,
2303 /* Search for matching Request in List */
2304 req_info = __bt_gatt_server_find_request_info(param->request_id, param->req_type);
2306 BT_ERR("GATT acquire Server Req Info not found for current response..return Error");
2307 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
2310 ret = gatt_send_response_acquire(req_info->connection_id, param->request_id, 0, param->fd, param->mtu, fd_list);
2312 if (ret != OAL_STATUS_SUCCESS) {
2313 BT_ERR("ret: %d", ret);
2314 return BLUETOOTH_ERROR_INTERNAL;
2317 BT_INFO("GATT acquire Server Response successfully sent");
2318 /* Remove GATT server request from list */
2319 gatt_server_requests = g_slist_remove(gatt_server_requests, req_info);
2320 g_free(req_info->addr);
2322 return BLUETOOTH_ERROR_NONE;
2327 int _bt_gatt_server_send_indication(char *sender, bluetooth_device_address_t *dev_addr,
2328 bluetooth_gatt_att_data_t *data,
2329 bluetooth_gatt_server_indication_params_t *param)
2331 BT_CHECK_PARAMETER(sender, return);
2332 BT_CHECK_PARAMETER(data, return);
2333 BT_CHECK_PARAMETER(param, return);
2335 gboolean all_send = FALSE;
2336 int ret = OAL_STATUS_SUCCESS;
2337 struct gatt_client_info_t *conn;
2339 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2340 _bt_convert_addr_type_to_string(address, dev_addr->addr);
2342 if (memcmp(dev_addr->addr, BDADDR_ANY, 6) == 0) {
2343 BT_INFO("GATT Server: Send Indication to all connected GATT clients..");
2346 BT_INFO("GATT Server: Send Indication to connected GATT client addr [%s]", address);
2349 /* Attempt to send Notification/Indication to all Connected GATT clients */
2351 ret = __bt_gatt_send_indication_to_all_connected_clients(data, param);
2352 if (ret != OAL_STATUS_SUCCESS) {
2353 BT_ERR("ret: %d", ret);
2355 return BLUETOOTH_ERROR_INTERNAL;
2359 conn = __bt_find_remote_gatt_client_info(address);
2361 ret = gatts_send_indication(param->instance_id, param->atrribute_handle,
2362 conn->connection_id, data->length,
2363 param->need_confirmation, (char *)(&data->data[0]));
2365 if (ret != OAL_STATUS_SUCCESS) {
2366 BT_ERR("ret: %d", ret);
2367 BT_INFO("Indication failed to send to Remote GATT Client [%s]", address);
2369 return BLUETOOTH_ERROR_INTERNAL;
2371 BT_INFO("Indication sent to Remote GATT Client [%s] wait for Notification completed event from OAL", address);
2373 num_indicate_clients = 1;
2374 return BLUETOOTH_ERROR_NONE;
2376 BT_ERR("Remote GATT client [%s] is not connected..Cant send Indication!!", address);
2378 return BLUETOOTH_ERROR_NOT_CONNECTED;
2381 return BLUETOOTH_ERROR_NONE;
2384 int _bt_gatt_server_update_attribute_value(char *sender, int instance_id,
2385 bluetooth_gatt_server_update_value_t *param)
2387 BT_CHECK_PARAMETER(sender, return);
2388 BT_CHECK_PARAMETER(param, return);
2389 int ret = OAL_STATUS_SUCCESS;
2391 oal_gatt_value_t value;
2392 BT_INFO("GATT Server Update value: Instance ID [%d] attr handle [%d] Value len [%d]",
2393 instance_id, param->attribute_handle, param->length);
2396 memset(&value, 0x00, sizeof(oal_gatt_value_t));
2398 value.handle = param->attribute_handle;
2399 value.len = param->length;
2400 memcpy(&value.value, ¶m->data.data, param->length);
2402 ret = gatts_update_att_value(instance_id, &value);
2404 if (ret != OAL_STATUS_SUCCESS) {
2405 BT_ERR("ret: %d", ret);
2406 return BLUETOOTH_ERROR_INTERNAL;
2409 BT_INFO("GATT Server Update Attribute Value successfully");
2410 return BLUETOOTH_ERROR_NONE;
2413 int _bt_get_att_mtu(bluetooth_device_address_t *address,
2416 BT_CHECK_PARAMETER(address, return);
2417 BT_CHECK_PARAMETER(mtu, return);
2418 struct gatt_client_info_t *conn_info = NULL;
2419 char addr[BT_ADDRESS_STRING_SIZE] = { 0 };
2420 int ret = OAL_STATUS_SUCCESS;
2423 _bt_convert_addr_type_to_string(addr, address->addr);
2425 BT_INFO("Get current MTU size for the remote client:DevAddress:[%s]", addr);
2427 conn_info = __bt_find_remote_gatt_client_info(addr);
2429 BT_INFO("GATT Client [%s] is connected, conn Id [%d] Instance ID [%d]",
2430 conn_info->addr, conn_info->connection_id, conn_info->instance_id);
2432 BT_ERR("GATT Client [%s] is not yet connected..", addr);
2433 return BLUETOOTH_ERROR_NOT_CONNECTED;
2436 ret = gatts_get_att_mtu(conn_info->connection_id, &stack_mtu);
2437 if (ret != OAL_STATUS_SUCCESS) {
2438 BT_ERR("ret: %d", ret);
2439 return BLUETOOTH_ERROR_INTERNAL;
2442 BT_INFO("ATT MTU received from OAL [%d]", stack_mtu);
2443 *mtu = (unsigned int)stack_mtu;
2446 BT_ERR("MTU value is zero, GATT Client [%s] is not yet connected..", addr);
2447 return BLUETOOTH_ERROR_NOT_CONNECTED;
2450 return BLUETOOTH_ERROR_NONE;
2453 #ifdef TIZEN_GATT_CLIENT
2454 /* GATT Client utility static functions */
2455 static bt_gatt_service_info_list_t * __bt_get_service_info_list(int conn_id)
2458 bt_gatt_service_info_list_t *info = NULL;
2460 for (l = list_gatt_info; l != NULL; l = g_slist_next(l)) {
2461 info = (bt_gatt_service_info_list_t*)l->data;
2465 if (info->conn_id == conn_id)
2472 static bt_gatt_service_info_t* __bt_find_matching_service(
2473 bt_gatt_service_info_list_t *svc_list, oal_gatt_srvc_id_t *svc)
2476 bt_gatt_service_info_t *info = NULL;
2478 for (l = svc_list->services; l != NULL; l = g_slist_next(l)) {
2479 info = (bt_gatt_service_info_t*)l->data;
2483 /* Match UUID and instance ID */
2484 if (!memcmp(&svc->id.uuid.uuid, &info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
2485 && (svc->id.inst_id == info->inst_id)) {
2492 static bt_gatt_char_info_t* __bt_find_matching_charc(
2493 bt_gatt_service_info_t *svc_info, oal_gatt_id_t *charc)
2496 bt_gatt_char_info_t *info = NULL;
2498 for (l = svc_info->chars; l != NULL; l = g_slist_next(l)) {
2499 info = (bt_gatt_char_info_t*)l->data;
2503 /* Match UUID and instance ID */
2504 if (!memcmp(&charc->uuid.uuid, &info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
2505 && (charc->inst_id == info->inst_id)) {
2512 static bt_gatt_descriptor_info_t* __bt_find_matching_desc(
2513 bt_gatt_char_info_t *char_info, oal_gatt_id_t *desc)
2516 bt_gatt_descriptor_info_t *info = NULL;
2518 for (l = char_info->descs; l != NULL; l = g_slist_next(l)) {
2519 info = (bt_gatt_descriptor_info_t*)l->data;
2523 /* Match UUID and instance ID */
2524 if (!memcmp(&desc->uuid, &info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
2525 && (desc->inst_id == info->inst_id)) {
2533 static struct gatt_server_info_t *__bt_find_remote_gatt_server_info_from_conn_id(int conn_id)
2536 struct gatt_server_info_t *info = NULL;
2538 for (l = gatt_server_info_list; l != NULL; l = g_slist_next(l)) {
2539 info = (struct gatt_server_info_t*)l->data;
2543 if (info->connection_id == conn_id) {
2544 BT_INFO("Remote GATT server found addr[%s]", info->addr);
2551 static bt_gatt_service_info_t* __bt_find_removed_service(bt_gatt_service_info_list_t *svc_list)
2554 bt_gatt_service_info_t *info = NULL;
2556 for (l = svc_list->services; l != NULL; l = g_slist_next(l)) {
2557 info = (bt_gatt_service_info_t*)l->data;
2561 /* Service is marked a removed */
2562 if (info->is_removed == 1)
2568 static void __bt_remove_service_info_from_list(bt_gatt_service_info_t *svc_info)
2574 bt_gatt_char_info_t *charc = NULL;
2575 bt_gatt_included_service_info_t *incl = NULL;
2576 bt_gatt_descriptor_info_t *desc = NULL;
2578 /* Remove all Characteristic and Descriptors within characteristic */
2579 for (l = svc_info->chars; l != NULL;) {
2580 charc = (bt_gatt_char_info_t*)l->data;
2581 l = g_slist_next(l); /* Incase if l is removed, saving next to l */
2586 /* Inside Characteristic */
2587 for (l1 = charc->descs; l1 != NULL;) {
2589 desc = (bt_gatt_descriptor_info_t*)l1->data;
2590 l1 = g_slist_next(l1);
2595 /* Remove Descriptor */
2596 charc->descs = g_slist_remove(charc->descs, desc);
2599 /* Remove Characteristic */
2600 svc_info->chars = g_slist_remove(svc_info->chars, charc);
2604 /* Remove all Included Services */
2605 for (l2 = svc_info->included_svcs; l2 != NULL;) {
2606 incl = (bt_gatt_included_service_info_t*)l2->data;
2607 l2 = g_slist_next(l2); /* Incase if l is removed, saving next to l */
2612 /* Remove included service */
2613 svc_info->included_svcs = g_slist_remove(svc_info->included_svcs, incl);
2620 static void __bt_build_service_browse_info(int conn_id,
2621 bt_services_browse_info_t* info)
2624 bt_gatt_service_info_list_t *svc_info_list;
2625 bt_gatt_service_info_t *svc_info;
2627 service_uuid_t uuid;
2628 struct gatt_server_info_t *conn_info = NULL;
2630 char uuid_string[BLUETOOTH_UUID_STRING_MAX];
2633 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
2638 _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
2640 svc_info_list = __bt_get_service_info_list(conn_id);
2645 info->count = g_slist_length(svc_info_list->services);
2646 BT_INFO("Total services present in the svc info list for this conn id [%d] is [%d]",
2647 conn_id, info->count);
2649 for (l = svc_info_list->services; l != NULL; l = g_slist_next(l)) {
2650 svc_info = (bt_gatt_service_info_t*)l->data;
2651 if (svc_info == NULL)
2654 memcpy(&uuid.uuid, &svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2655 _bt_uuid_to_string(&uuid, uuid_string);
2657 BT_INFO("Service UUID formed [%s] strlen [%zd]", uuid_string, strlen(uuid_string));
2659 /* Fill UUID of service */
2660 g_strlcpy(info->uuids[count], uuid_string,
2661 BLUETOOTH_UUID_STRING_MAX);
2663 BT_INFO("Service UUID formed TO be sent [%s] strlen [%zd]",
2664 info->uuids[count], strlen(info->uuids[count]));
2665 /* Fill instance ID of service */
2666 info->inst_id[count] = svc_info->inst_id;
2668 /* Fill primary service or not info */
2669 info->primary[count] = svc_info->is_primary;
2671 /* Increment count of services browsed */
2675 BT_INFO("Total services browsed [%d]", count);
2678 static void __bt_build_char_browse_info(int conn_id,
2679 bt_gatt_service_info_t *svc_info,
2680 bt_char_browse_info_t* info)
2683 bt_gatt_char_info_t *char_info;
2684 service_uuid_t uuid;
2686 struct gatt_server_info_t *conn_info = NULL;
2688 char uuid_string[BLUETOOTH_UUID_STRING_MAX];
2692 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
2696 /* Fill default data, this will be required even in case of failure */
2697 _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
2698 memcpy(&info->svc_uuid, svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2699 info->svc_inst_id = svc_info->inst_id;
2701 if (!svc_info->chars) {
2702 BT_ERR("No Chars browsed for address [%s]", conn_info->addr);
2706 info->count = g_slist_length(svc_info->chars);
2707 BT_INFO("Total count of Characteristics [%d]", info->count);
2709 for (l = svc_info->chars; l != NULL; l = g_slist_next(l)) {
2710 char_info = (bt_gatt_char_info_t*)l->data;
2711 if (char_info == NULL)
2714 memcpy(&uuid.uuid, &char_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2715 _bt_uuid_to_string(&uuid, uuid_string);
2717 /* Fill UUID of characteristic */
2718 g_strlcpy(info->uuids[count], uuid_string,
2719 BLUETOOTH_UUID_STRING_MAX);
2721 /* Fill instance ID of characteristic */
2722 info->inst_id[count] = char_info->inst_id;
2724 /* Fill property of characteristic */
2725 info->props[count] = char_info->props;
2727 /* Increment count of services browsed */
2730 BT_INFO("Total characteristics browsed [%d]", count);
2733 static void __bt_build_descriptor_browse_info(int conn_id,
2734 bt_gatt_service_info_t *svc_info,
2735 bt_gatt_char_info_t *char_info,
2736 bt_descriptor_browse_info_t* info)
2739 bt_gatt_descriptor_info_t *desc_info;
2741 service_uuid_t uuid;
2742 struct gatt_server_info_t *conn_info = NULL;
2744 char uuid_string[BLUETOOTH_UUID_STRING_MAX];
2746 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
2748 /* Fill default data, this will be required even in case of failure */
2749 _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
2750 memcpy(&info->svc_uuid, svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2751 info->svc_inst_id = svc_info->inst_id;
2752 memcpy(&info->char_uuid, char_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2753 info->char_inst_id = char_info->inst_id;
2755 /* Fill property of the parent characteristic of this descriptor */
2756 info->char_props_map = char_info->props;
2758 info->count = g_slist_length(char_info->descs);
2759 BT_INFO("Total count of Descriptors [%d]", info->count);
2761 if (!char_info->descs) {
2762 BT_ERR("No Descriptors browsed for address [%s]", conn_info->addr);
2766 for (l = char_info->descs; l != NULL; l = g_slist_next(l)) {
2767 desc_info = (bt_gatt_descriptor_info_t*)l->data;
2768 if (desc_info == NULL)
2771 memcpy(&uuid.uuid, &desc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2772 _bt_uuid_to_string(&uuid, uuid_string);
2774 /* Fill UUID of Descriptor */
2775 g_strlcpy(info->uuids[count], uuid_string,
2776 BLUETOOTH_UUID_STRING_MAX);
2778 /* Fill instance ID of Descriptor */
2779 info->inst_id[count] = desc_info->inst_id;
2782 /* Increment count of Descriptor browsed */
2786 BT_INFO("Total descriptors browsed [%d]", count);
2790 static void __bt_cleanup_remote_services(struct gatt_server_info_t *conn_info)
2797 bt_gatt_service_info_list_t * svc_info_list = NULL;
2798 bt_gatt_service_info_t *svc = NULL;
2799 bt_gatt_char_info_t *chr = NULL;
2800 bt_gatt_descriptor_info_t *desc = NULL;
2801 bt_gatt_included_service_info_t *incl_svc = NULL;
2803 BT_INFO("Start Cleanup of all services");
2805 svc_info_list = __bt_get_service_info_list(conn_info->connection_id);
2806 if (!svc_info_list) {
2807 BT_INFO("Could not find Svc Info list for the connection ID [%d]",
2808 conn_info->connection_id);
2812 BT_INFO("Num Services [%d]", g_slist_length(svc_info_list->services));
2813 for (l = svc_info_list->services; l;) {
2814 svc = (bt_gatt_service_info_t*)l->data;
2815 l = g_slist_next(l);
2819 BT_INFO("Service info Is Prim[%d] Inst ID [%d]",
2820 svc->is_primary, svc->inst_id);
2821 BT_INFO("Num chars [%d]", g_slist_length(svc->chars));
2822 /* Delete all chars and its descriptors */
2823 for (ll = svc->chars; ll;) {
2824 chr = (bt_gatt_char_info_t*)ll->data;
2825 ll = g_slist_next(ll);
2829 BT_INFO("Num descs [%d]", g_slist_length(chr->descs));
2830 for (lll = chr->descs; lll;) {
2831 desc = (bt_gatt_descriptor_info_t *)lll->data;
2832 lll = g_slist_next(lll);
2835 chr->descs = g_slist_remove(chr->descs, desc);
2838 svc->chars = g_slist_remove(svc->chars, chr);
2842 BT_INFO("Num incl svcs [%d]", g_slist_length(svc->included_svcs));
2843 /* Delete all included services */
2844 for (llll = svc->included_svcs; llll;) {
2845 incl_svc = (bt_gatt_included_service_info_t*)llll->data;
2846 llll = g_slist_next(llll);
2847 if (incl_svc == NULL)
2850 svc->included_svcs = g_slist_remove(svc->included_svcs, incl_svc);
2853 svc_info_list->services = g_slist_remove(svc_info_list->services, svc);
2857 list_gatt_info = g_slist_remove(list_gatt_info, svc_info_list);
2858 g_free(svc_info_list);
2859 BT_INFO("Cleanup of all services done");
2862 int _bt_register_gatt_client_instance(const char *sender,
2863 bluetooth_device_address_t *address)
2865 int ret = OAL_STATUS_SUCCESS;
2866 char *uuid_string = NULL;
2871 /* App should ensure that it should not send */
2872 BT_INFO("###Check on which instance GATT Client instance can be initialized....");
2873 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
2874 if (numapps[k].is_initialized == 1) {
2875 BT_INFO("Instance ID [%d] is already in use..Check next slot",
2876 numapps[k].instance_id);
2879 BT_INFO("Time to register GATT client instancer..UUID to be used is [%s] slot [%d]",
2880 uuid_list[slot-1], slot);
2886 BT_ERR("No Slot if free for GATT Client registration..");
2887 return BLUETOOTH_ERROR_REGISTRATION_FAILED;
2890 uuid_string = g_malloc0(BT_UUID_STRING_MAX);
2891 _bt_string_to_uuid(uuid_list[slot-1], (service_uuid_t*)&uuid);
2892 g_strlcpy(uuid_string, uuid_list[slot-1], BT_UUID_STRING_MAX);
2893 BT_INFO("Copied UUID string [%s]", uuid_string);
2895 /* Register GATT Client */
2896 ret = gattc_register(&uuid);
2897 if (ret != OAL_STATUS_SUCCESS) {
2898 BT_ERR("ret: %d", ret);
2899 g_free(uuid_string);
2900 return BLUETOOTH_ERROR_INTERNAL;
2903 BT_INFO("GATT Client registration call successfully accepted by OAL..wait for Instance Initialized event from OAL..");
2905 /* Return & wait for GATT Client Instance Initialization event */
2906 memset(numapps[slot].sender, 0x00, sizeof(numapps[slot].sender));
2907 memset(numapps[slot].uuid, 0x00, sizeof(numapps[slot].uuid));
2909 g_strlcpy(numapps[slot].sender, sender, sizeof(numapps[slot].sender));
2910 g_strlcpy(numapps[slot].uuid, uuid_string, sizeof(numapps[slot].uuid));
2912 /* Address is saved here. When event comes, sender + address are matched for replying pending
2913 request. It is impossible for same sender to have requests with two same addresses */
2914 memcpy(&numapps[slot].address.addr, address->addr, sizeof(bluetooth_device_address_t));
2916 BT_INFO("Slot [%d] occupied", slot);
2917 numapps[slot].is_initialized = TRUE; /* Set initialization to true here itself */
2919 g_free(uuid_string);
2920 return BLUETOOTH_ERROR_NONE;
2926 /* GATT client events */
2927 static void __bt_handle_client_instance_registered(event_gattc_register_t *data)
2929 bt_service_app_info_t *info = NULL;
2931 char *uuid_string = g_malloc0(BT_UUID_STRING_MAX);
2933 _bt_uuid_to_string(&(data->client_uuid), uuid_string);
2934 BT_INFO("CLient ID is Initialized [%d] UUID initialized [%s]", data->client_if, uuid_string);
2936 /* Platform GATT client framwork does not use Default GATT client instance
2937 This GATT client instance is never deregistred in the lifetime of bt-service */
2938 BT_INFO("Default UUID [%s] current registered uuid [%s]",
2939 DEFAULT_GATT_CLIENT_UUID, uuid_string);
2940 if (g_strcmp0(uuid_string, DEFAULT_GATT_CLIENT_UUID) == 0) {
2941 BT_INFO("Default client Instance Registered [%s] Client instance [%d]",
2942 uuid_string, data->client_if);
2943 gatt_default_client = data->client_if;
2944 g_free(uuid_string);
2948 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
2951 if (g_strcmp0(info->uuid, uuid_string) == 0) {
2952 BT_INFO("Found GATT client.. UUID [%s], sender [%s]", info->uuid, info->sender);
2953 BT_INFO("Slot [%d] occupied", k);
2954 info->is_initialized = TRUE;
2955 info->client_id = data->client_if;
2956 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE, BT_GATT_CLIENT_REGISTER,
2957 (void*)info, sizeof(bt_service_app_info_t));
2961 g_free(uuid_string);
2964 static void __bt_handle_client_connected(event_gattc_conn_t *event_data)
2966 int result = BLUETOOTH_ERROR_NONE;
2967 struct gatt_server_info_t *conn_info = NULL;
2968 struct gatt_out_conn_info_t *out_conn_info = NULL;
2970 GVariant *param = NULL;
2972 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2973 _bt_convert_addr_type_to_string(address,
2974 (unsigned char *)event_data->address.addr);
2976 if (event_data->status != OAL_STATUS_SUCCESS)
2977 result = BLUETOOTH_ERROR_INTERNAL;
2979 /* DBUS Return fo BT_CONNECT_LE for all the apps */
2980 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE, address,
2981 BT_ADDRESS_STRING_SIZE);
2983 BT_INFO("Local GATT Client Connected: Remote addr[%s] Client Interface [%d] Connection ID [%d] status[%d]",
2984 address, event_data->client_if, event_data->conn_id, event_data->status);
2986 if (result == BLUETOOTH_ERROR_NONE) {
2987 /* Check if device is already in connected list */
2988 conn_info = __bt_find_remote_gatt_server_info(address);
2991 /* Send event to BT-API */
2992 param = g_variant_new("(is)", result, address);
2993 _bt_send_event(BT_DEVICE_EVENT,
2994 BLUETOOTH_EVENT_GATT_CLIENT_CONNECTED, /* Local device is GATT client */
2997 /* Save Connection info */
2998 conn_info = g_new0(struct gatt_server_info_t, 1);
2999 conn_info->addr = g_strdup(address);
3000 conn_info->client_id = event_data->client_if;
3001 BT_INFO("Added GATT server addr[%s]", conn_info->addr);
3002 conn_info->connection_id = event_data->conn_id;
3003 gatt_server_info_list = g_slist_append(gatt_server_info_list, conn_info);
3004 BT_INFO("Total num of connected Remote GATT server devices [%d]",
3005 g_slist_length(gatt_server_info_list));
3008 BT_INFO("Do a Internal refresh");
3009 if (OAL_STATUS_SUCCESS != gattc_refresh(conn_info->client_id, &event_data->address))
3010 BT_ERR("GATT database refresh failed!!");
3012 BT_INFO("GATT database refresh Success!!");
3015 BT_ERR("Local GATT Client connected event for addr[%s], but device is in connected list already", address);
3017 BT_ERR("GATT Client Connection failed!!");
3019 /* If outgoing connection Info is present, then remove it */
3020 out_conn_info = __bt_find_gatt_outgoing_conn_info(address);
3021 if (out_conn_info) {
3022 BT_ERR("Outgoing Client connect request was sent");
3023 outgoing_gatt_conn_list = g_slist_remove(outgoing_gatt_conn_list, out_conn_info);
3024 g_free(out_conn_info->addr);
3025 g_free(out_conn_info);
3030 static void __bt_handle_client_disconnected(event_gattc_conn_t *event_data)
3032 int result = BLUETOOTH_ERROR_NONE;
3034 struct gatt_server_info_t *conn_info = NULL;
3035 struct gatt_out_conn_info_t *out_conn_info = NULL;
3037 GVariant *param = NULL;
3039 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
3040 _bt_convert_addr_type_to_string(address,
3041 (unsigned char *)event_data->address.addr);
3043 if (event_data->status != OAL_STATUS_SUCCESS)
3044 result = BLUETOOTH_ERROR_INTERNAL;
3046 if (NULL == _bt_get_request_info_data(BT_DISCONNECT_LE, address)) {
3047 if (NULL != _bt_get_request_info_data(BT_CONNECT_LE, address)) {
3048 result = BLUETOOTH_ERROR_INTERNAL;
3049 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
3050 address, BT_ADDRESS_STRING_SIZE);
3051 BT_ERR("Failed to connect Local GATT Remote addr[%s]", address);
3056 /* DBUS Return for BT_DISCONNECT_LE for all the apps */
3057 __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE, address,
3058 BT_ADDRESS_STRING_SIZE);
3060 BT_INFO("Local GATT Client DisConnected: Remote addr[%s] Client Interface [%d] Connection ID [%d] status [%d]",
3061 address, event_data->client_if, event_data->conn_id, event_data->status);
3063 /* Remove Connection info */
3064 conn_info = __bt_find_remote_gatt_server_info(address);
3067 param = g_variant_new("(is)", result, address);
3068 /* Send event to application */
3069 _bt_send_event(BT_DEVICE_EVENT,
3070 BLUETOOTH_EVENT_GATT_CLIENT_DISCONNECTED,
3073 BT_INFO("Remove GATT server info from List..");
3074 /* Remove all services from info list_gatt_info */
3075 __bt_cleanup_remote_services(conn_info);
3077 /* Remove info from List */
3078 gatt_server_info_list = g_slist_remove(gatt_server_info_list, conn_info);
3079 BT_INFO("Total num of connected GATT servers [%d]", g_slist_length(gatt_server_info_list));
3080 g_free(conn_info->addr);
3083 BT_INFO("Can not find conn info, already removed!");
3085 /* If outgoing connection Info is present, then remove it */
3086 out_conn_info = __bt_find_gatt_outgoing_conn_info(address);
3087 if (out_conn_info) {
3088 BT_ERR("Client Disconnected event, but outgoing connect request was sent");
3089 outgoing_gatt_conn_list = g_slist_remove(outgoing_gatt_conn_list, out_conn_info);
3090 g_free(out_conn_info->addr);
3091 g_free(out_conn_info);
3097 static void __bt_handle_client_service_search_result(
3098 event_gattc_service_result_t *event_data)
3101 BT_INFO("received the gatt service search result");
3102 /* Pre: status is never fail from OAL */
3104 /* Find service list from address */
3105 bt_gatt_service_info_list_t *svc_info_list;
3106 bt_gatt_service_info_t *svc_info;
3107 BT_INFO("Search Result: status [%d] conn_id [%d]",
3108 event_data->conn_status.status,
3109 event_data->conn_status.conn_id);
3111 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3112 if (!svc_info_list) {
3113 BT_INFO("Service info list not present for this connection ID, means first time browse");
3114 /* Means for this conn_id, no services are ever browsed, first time,
3115 create service info list for this conn_id */
3116 svc_info_list = g_malloc0(sizeof(bt_gatt_service_info_list_t));
3117 svc_info_list->conn_id = event_data->conn_status.conn_id;
3118 list_gatt_info = g_slist_append(list_gatt_info, svc_info_list);
3120 BT_INFO("Service info list Already present for this connection ID, means not first time browse for this conn ID ");
3123 /* send list and current service's uuid and instance id to find it */
3124 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3127 /* If not found, check if service changed, if yes, means this is a new service added
3128 in remote GATT device, update uuid info in svc info list structure, to be used when
3129 search is completed */
3131 BT_INFO("Service Not found in svc info list for this connection ID");
3132 if (svc_info_list->info.is_changed) {
3133 BT_INFO("Service Changed indication already found for this connection ID");
3134 memcpy(svc_info_list->info.uuid, event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3136 /* Create and add new service in service list */
3137 svc_info = g_malloc0(sizeof(bt_gatt_service_info_t));
3138 memcpy(svc_info->uuid, event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3139 svc_info->inst_id = event_data->srvc_id.id.inst_id;
3140 svc_info->is_primary = event_data->srvc_id.is_prmry;
3141 svc_info_list->services = g_slist_append(svc_info_list->services, svc_info);
3142 BT_INFO("Service created and added in Svc info listf or this connection ID");
3144 BT_INFO("Service Already found to be present inside the svc info list for this connection ID");
3145 /* If returned matching service info, then just update service_rmeoved value inside it to 0 */
3146 svc_info->is_removed = 0;
3150 static void __bt_handle_client_service_search_completed(
3151 event_gattc_conn_status_t *event_data)
3154 struct gatt_server_info_t *conn_info = NULL;
3155 bt_gatt_service_info_list_t *svc_info_list;
3156 bt_gatt_service_info_t *svc_info;
3157 bt_services_browse_info_t browse_info;
3158 unsigned char uuid_empty[BLUETOOTH_UUID_HEX_MAX_LEN];
3160 memset(&uuid_empty, 0x00, BLUETOOTH_UUID_HEX_MAX_LEN);
3161 memset(&browse_info, 0x00, sizeof(bt_services_browse_info_t));
3162 BT_INFO("Primary Services browsing completed status[%d] conn ID [%d]",
3163 event_data->status, event_data->conn_id);
3165 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(event_data->conn_id);
3167 svc_info_list = __bt_get_service_info_list(event_data->conn_id);
3168 if (!svc_info_list) {
3169 BT_ERR("No services browsed ever for addr [%s]", conn_info->addr);
3171 /* Just build response and return ERROR */
3172 __bt_build_service_browse_info(event_data->conn_id, &browse_info);
3174 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_INTERNAL,
3175 BT_GATT_GET_PRIMARY_SERVICES, &browse_info,
3176 sizeof(bt_services_browse_info_t));
3180 /* If fail, then send event with error */
3181 if (event_data->status != OAL_STATUS_SUCCESS) {
3182 /* Just build response and return ERROR */
3183 __bt_build_service_browse_info(event_data->conn_id, &browse_info);
3185 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_INTERNAL,
3186 BT_GATT_GET_PRIMARY_SERVICES, &browse_info,
3187 sizeof(bt_services_browse_info_t));
3191 /* If success, then find service info list from address */
3193 /* If svc_changed == 1 and uuid valid, means a new service is added*/
3194 if (svc_info_list->info.is_changed && !memcmp(uuid_empty, svc_info_list->info.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)) {
3195 /* TODO: Send event -Service added with instance ID and UUID of newly added service */
3196 BT_INFO("new service added");
3198 BT_INFO("TODO new service added");
3201 /* If svc_changed == 1 and uuid invalid, then a service is removed */
3202 if (svc_info_list->info.is_changed && memcmp(uuid_empty, svc_info_list->info.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)) {
3203 /* Scan through the service info list to find service with is_removed = 1*/
3204 svc_info = __bt_find_removed_service(svc_info_list);
3206 /* TODO Send event - Service removed with instance ID and UUID of just rmeoved service */
3208 /* Remove that service info from service info list */
3209 svc_info_list->services = g_slist_remove(svc_info_list->services, svc_info);
3211 /* Delete that service completely from svc_info list*/
3212 __bt_remove_service_info_from_list(svc_info);
3216 /* Reset svc_changed = 0, and reset UUID = all 0's */
3217 svc_info_list->info.is_changed = 0;
3218 memset(&svc_info_list->info.uuid, 0x00, BLUETOOTH_UUID_HEX_MAX_LEN);
3220 /* Build Reply and send to service browse primary services request of pending apps */
3221 __bt_build_service_browse_info(event_data->conn_id, &browse_info);
3223 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE,
3224 BT_GATT_GET_PRIMARY_SERVICES, &browse_info,
3225 sizeof(bt_services_browse_info_t));
3229 static void __bt_handle_client_characteristic_search_result(
3230 event_gattc_characteristic_result_t *event_data)
3232 bt_gatt_service_info_list_t *svc_info_list;
3233 bt_gatt_service_info_t *svc_info;
3234 bt_gatt_char_info_t *char_info;
3235 bt_char_browse_info_t browse_info;
3237 BT_INFO("Characteristic search result status [%d]",
3238 event_data->conn_status.status);
3240 memset(&browse_info, 0x00, sizeof(bt_char_browse_info_t));
3243 if (event_data->conn_status.status == OAL_STATUS_SUCCESS) {
3244 /* Find service info list from address */
3245 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3246 if (svc_info_list == NULL) {
3247 BT_ERR("svc_info_list is NULL");
3252 /* Find matching service info from svc info list */
3253 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3254 if (svc_info == NULL) {
3255 BT_ERR("svc_info is NULL");
3259 /* Find Matching char from service info in event */
3260 char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
3261 if (char_info == NULL)
3262 BT_ERR("char_info is NULL");
3264 /* If not found, then add new characteristic and return */
3266 BT_INFO(" add new characteristic");
3267 char_info = g_malloc0(sizeof(bt_gatt_char_info_t));
3268 memcpy(char_info->uuid, event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3270 char_info->inst_id = event_data->char_id.inst_id;
3271 char_info->props = event_data->char_prop;
3272 svc_info->chars = g_slist_append(svc_info->chars, char_info);
3274 /* If found, then return */
3275 BT_INFO("update char property as Characteristic browsed is already present");
3276 char_info->props |= event_data->char_prop;
3279 /* If Not success: Means Charc browse is completed */
3280 /* Find char list from service in event */
3281 /* Find service list from address */
3282 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3284 /* Find service info from service in event */
3285 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3287 /* Build char list from service in event */
3288 __bt_build_char_browse_info(event_data->conn_status.conn_id,
3289 svc_info, &browse_info);
3291 /* Create response and return by sending event*/
3292 /* Build Reply and send to service browse All Included services request of pending apps */
3293 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE,
3294 BT_GATT_GET_SERVICE_PROPERTIES,
3296 sizeof(bt_char_browse_info_t));
3300 static void __bt_handle_client_descriptor_search_result(
3301 event_gattc_descriptor_result_t *event_data)
3303 bt_gatt_service_info_list_t *svc_info_list;
3304 bt_gatt_service_info_t *svc_info;
3305 bt_gatt_char_info_t *char_info;
3306 bt_gatt_descriptor_info_t *desc_info;
3307 bt_descriptor_browse_info_t browse_info;
3309 BT_INFO("descriptor search result status [%d]", event_data->conn_status.status);
3311 memset(&browse_info, 0x00, sizeof(bt_descriptor_browse_info_t));
3314 if (event_data->conn_status.status == OAL_STATUS_SUCCESS) {
3315 /* Find service list from address */
3316 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3317 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3318 char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
3319 desc_info = __bt_find_matching_desc(char_info, &event_data->descr_id);
3321 /* If not found, add new descriptor and return */
3323 desc_info = g_malloc0(sizeof(bt_gatt_descriptor_info_t));
3324 memcpy(desc_info->uuid, event_data->descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3326 desc_info->inst_id = event_data->descr_id.inst_id;
3327 char_info->descs = g_slist_append(char_info->descs, desc_info);
3330 /* If found, then return */
3331 BT_INFO("Characteristic browsed is already presesnt");
3334 /* If Not success */
3335 /* Find service list from address */
3336 /* Find included service list from service in event */
3337 /* Create response and return by sending event*/
3338 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3340 /* Find service info from service in event */
3341 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3343 /* Find char info from char in event */
3344 char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
3346 /* Build descriptor list from char in event */
3347 __bt_build_descriptor_browse_info(event_data->conn_status.conn_id,
3348 svc_info, char_info, &browse_info);
3351 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE,
3352 BT_GATT_GET_CHARACTERISTIC_PROPERTIES,
3354 sizeof(bt_descriptor_browse_info_t));
3358 static void __bt_handle_client_characteristic_read_data(
3359 event_gattc_read_data *event_data)
3361 int result = BLUETOOTH_ERROR_NONE;
3362 struct gatt_server_info_t *conn_info = NULL;
3363 bluetooth_gatt_client_char_prop_info_t read_info;
3365 /* Read Information data structures */
3366 GVariant *param = NULL;
3367 GVariant *data = NULL;
3368 GVariant *data_svc_uuid = NULL;
3369 GVariant *data_char_uuid = NULL;
3370 char *read_val = NULL;
3371 char *svc_uuid = NULL;
3372 char *char_uuid = NULL;
3378 //memset(&read_info, 0x00, sizeof(bt_gatt_handle_property_t));
3379 memset(&read_info, 0x00, sizeof(bluetooth_gatt_client_char_prop_info_t));
3381 /* Extract Address from conn_id of event data */
3382 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3383 event_data->uuid_status.conn_status.conn_id);
3385 BT_INFO("Characteristic Read result from addr [%s] status [%d]",
3386 conn_info->addr, event_data->uuid_status.conn_status.status);
3388 /* Fill char in buffer */
3389 memcpy(&read_info.characteristic.uuid,
3390 event_data->uuid_status.char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3391 read_info.characteristic.instance_id = event_data->uuid_status.char_id.inst_id;
3393 /* Fill Service in buffer */
3394 memcpy(&read_info.svc.uuid,
3395 event_data->uuid_status.srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3396 read_info.svc.instance_id = event_data->uuid_status.srvc_id.id.inst_id;
3398 /* Fill remote device address */
3399 _bt_convert_addr_string_to_type(read_info.device_address.addr, conn_info->addr);
3401 /* Fill data and reply to all apps waiting for Read result on the same characteristic
3402 Note: Even in case of failure, address, handles and result code should be returned */
3403 if (event_data->uuid_status.conn_status.status != OAL_STATUS_SUCCESS)
3404 result = BLUETOOTH_ERROR_INTERNAL;
3406 BT_ERR("read data len is [%d]", event_data->data_len);
3407 if (event_data->data_len > 0) {
3409 for (i = 0; i < event_data->data_len; i++)
3410 BT_INFO("Data[%d] = [0x%x]", i, event_data->data[i]);
3413 read_val = g_memdup(&event_data->data[0], event_data->data_len);
3415 data = g_variant_new_from_data(
3416 G_VARIANT_TYPE_BYTESTRING,
3418 event_data->data_len,
3421 BT_ERR("Characteristic Read success, but no data!!!");
3423 data = g_variant_new_from_data(
3424 G_VARIANT_TYPE_BYTESTRING,
3431 svc_uuid = g_memdup(&event_data->uuid_status.srvc_id.id.uuid.uuid[0], uuid_len);
3433 data_svc_uuid = g_variant_new_from_data(
3434 G_VARIANT_TYPE_BYTESTRING,
3440 char_uuid = g_memdup(&event_data->uuid_status.char_id.uuid.uuid[0], uuid_len);
3442 data_char_uuid = g_variant_new_from_data(
3443 G_VARIANT_TYPE_BYTESTRING,
3448 param = g_variant_new("(isn@ayin@ayin@ay)", result,
3452 event_data->uuid_status.srvc_id.id.inst_id,
3455 event_data->uuid_status.char_id.inst_id,
3456 event_data->data_len,
3460 _bt_send_event(BT_GATT_CLIENT_EVENT,
3461 BLUETOOTH_EVENT_GATT_READ_CHAR,
3464 /* Send DBUS return */
3465 __bt_gatt_handle_pending_request_info(result,
3466 BT_GATT_READ_CHARACTERISTIC,
3468 sizeof(bluetooth_gatt_client_char_prop_info_t));
3479 static void __bt_handle_client_descriptor_read_data(
3480 event_gattc_read_data *event_data)
3482 int result = BLUETOOTH_ERROR_NONE;
3483 struct gatt_server_info_t *conn_info = NULL;
3484 bluetooth_gatt_client_desc_prop_info_t read_info;
3486 /* Read Information data structures */
3487 GVariant *param = NULL;
3488 GVariant *data = NULL;
3489 GVariant *data_svc_uuid = NULL;
3490 GVariant *data_char_uuid = NULL;
3491 GVariant *data_desc_uuid = NULL;
3492 char *read_val = NULL;
3493 char *svc_uuid = NULL;
3494 char *char_uuid = NULL;
3495 char *desc_uuid = NULL;
3500 memset(&read_info, 0x00, sizeof(bluetooth_gatt_client_desc_prop_info_t));
3502 /* Extract Address from conn_id of event data */
3503 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3504 event_data->uuid_status.conn_status.conn_id);
3506 BT_INFO("Descriptor Read result from addr [%s] status [%d]",
3507 conn_info->addr, event_data->uuid_status.conn_status.status);
3509 /* Fill descriptor informations in buffer */
3510 memcpy(&read_info.descriptor.uuid,
3511 event_data->uuid_status.descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3512 read_info.descriptor.instance_id = event_data->uuid_status.descr_id.inst_id;
3514 /* Fill Characteristic informations in buffer */
3515 memcpy(&read_info.characteristic.uuid,
3516 event_data->uuid_status.char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3517 read_info.characteristic.instance_id = event_data->uuid_status.char_id.inst_id;
3519 /* Fill Service informations in buffer */
3520 memcpy(&read_info.svc.uuid,
3521 event_data->uuid_status.srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3522 read_info.svc.instance_id = event_data->uuid_status.srvc_id.id.inst_id;
3524 /* Fill remote device address */
3525 _bt_convert_addr_string_to_type(read_info.device_address.addr, conn_info->addr);
3527 /* Fill data and reply to all apps waiting for Read result on the same characteristic */
3528 if (event_data->uuid_status.conn_status.status != OAL_STATUS_SUCCESS)
3529 result = BLUETOOTH_ERROR_INTERNAL;
3531 BT_INFO("desc data len:%d", event_data->data_len);
3532 if (event_data->data_len > 0) {
3534 for (i = 0; i < event_data->data_len; i++)
3535 BT_DBG("Data[%d] = [0x%x]", i, event_data->data[i]);
3538 read_val = g_memdup(&event_data->data[0], event_data->data_len);
3540 data = g_variant_new_from_data(
3541 G_VARIANT_TYPE_BYTESTRING,
3543 event_data->data_len,
3546 BT_INFO("Descriptor Read success, but no data!!!");
3548 data = g_variant_new_from_data(
3549 G_VARIANT_TYPE_BYTESTRING,
3555 svc_uuid = g_memdup(&event_data->uuid_status.srvc_id.id.uuid.uuid[0], uuid_len);
3557 data_svc_uuid = g_variant_new_from_data(
3558 G_VARIANT_TYPE_BYTESTRING,
3564 char_uuid = g_memdup(&event_data->uuid_status.char_id.uuid.uuid[0], uuid_len);
3566 data_char_uuid = g_variant_new_from_data(
3567 G_VARIANT_TYPE_BYTESTRING,
3573 desc_uuid = g_memdup(&event_data->uuid_status.descr_id.uuid.uuid[0], uuid_len);
3575 data_desc_uuid = g_variant_new_from_data(
3576 G_VARIANT_TYPE_BYTESTRING,
3581 param = g_variant_new("(isn@ayin@ayin@ayin@ay)", result,
3585 event_data->uuid_status.srvc_id.id.inst_id,
3588 event_data->uuid_status.char_id.inst_id,
3591 event_data->uuid_status.descr_id.inst_id,
3592 event_data->data_len,
3596 _bt_send_event(BT_GATT_CLIENT_EVENT,
3597 BLUETOOTH_EVENT_GATT_READ_DESC,
3600 BT_INFO("Send DBUS rpely for GATT Read Descriptor");
3601 /* Send DBUS return */
3602 __bt_gatt_handle_pending_request_info(result,
3603 BT_GATT_READ_DESCRIPTOR_VALUE,
3605 sizeof(bluetooth_gatt_client_desc_prop_info_t));
3616 static void __bt_handle_client_characteristic_write_data(
3617 event_gattc_write_data *event_data)
3619 int result = BLUETOOTH_ERROR_NONE;
3620 struct gatt_server_info_t *conn_info = NULL;
3621 bluetooth_gatt_client_char_prop_info_t write_info;
3623 /* Read Information data structures */
3624 GVariant *param = NULL;
3625 GVariant *data_svc_uuid = NULL;
3626 GVariant *data_char_uuid = NULL;
3627 char *svc_uuid = NULL;
3628 char *char_uuid = NULL;
3632 memset(&write_info, 0x00, sizeof(bluetooth_gatt_client_char_prop_info_t));
3634 /* Extract Address from conn_id of event data */
3635 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3636 event_data->conn_status.conn_id);
3638 BT_INFO("Characteristic Write callback from addr [%s] status [%d]",
3639 conn_info->addr, event_data->conn_status.status);
3641 /* Fill char in buffer */
3642 memcpy(&write_info.characteristic.uuid,
3643 event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3644 write_info.characteristic.instance_id = event_data->char_id.inst_id;
3646 /* Fill Service in buffer */
3647 memcpy(&write_info.svc.uuid,
3648 event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3649 write_info.svc.instance_id = event_data->srvc_id.id.inst_id;
3651 /* Fill remote device address */
3652 _bt_convert_addr_string_to_type(write_info.device_address.addr, conn_info->addr);
3654 if (event_data->conn_status.status != OAL_STATUS_SUCCESS) {
3655 result = BLUETOOTH_ERROR_INTERNAL;
3661 svc_uuid = g_memdup(&event_data->srvc_id.id.uuid.uuid[0], uuid_len);
3663 data_svc_uuid = g_variant_new_from_data(
3664 G_VARIANT_TYPE_BYTESTRING,
3670 char_uuid = g_memdup(&event_data->char_id.uuid.uuid[0], uuid_len);
3672 data_char_uuid = g_variant_new_from_data(
3673 G_VARIANT_TYPE_BYTESTRING,
3678 param = g_variant_new("(isn@ayin@ayi)", result,
3682 event_data->srvc_id.id.inst_id,
3685 event_data->char_id.inst_id);
3688 _bt_send_event(BT_GATT_CLIENT_EVENT,
3689 BLUETOOTH_EVENT_GATT_WRITE_CHAR,
3698 /* Send DBUS return */
3699 __bt_gatt_handle_pending_request_info(result,
3700 BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE,
3702 sizeof(bluetooth_gatt_client_char_prop_info_t));
3706 static void __bt_handle_client_descriptor_write_data(
3707 event_gattc_write_data *event_data)
3709 int result = BLUETOOTH_ERROR_NONE;
3710 struct gatt_server_info_t *conn_info = NULL;
3711 bluetooth_gatt_client_desc_prop_info_t write_info;
3713 /* Write Information data structures */
3714 GVariant *param = NULL;
3715 GVariant *data_svc_uuid = NULL;
3716 GVariant *data_char_uuid = NULL;
3717 GVariant *data_desc_uuid = NULL;
3718 char *svc_uuid = NULL;
3719 char *char_uuid = NULL;
3720 char *desc_uuid = NULL;
3724 memset(&write_info, 0x00, sizeof(bluetooth_gatt_client_desc_prop_info_t));
3726 /* Extract Address from conn_id of event data */
3727 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3728 event_data->conn_status.conn_id);
3730 if (NULL == conn_info) {
3732 BT_INFO("Failed to get the conn info for conn_id [%d]", event_data->conn_status.conn_id);
3736 BT_INFO("Descriptor Write callback from addr [%s] status [%d]",
3737 conn_info->addr, event_data->conn_status.status);
3739 /* Fill descriptor informations in buffer */
3740 memcpy(&write_info.descriptor.uuid,
3741 event_data->descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3742 write_info.descriptor.instance_id = event_data->descr_id.inst_id;
3744 /* Fill Characteristic informations in buffer */
3745 memcpy(&write_info.characteristic.uuid,
3746 event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3747 write_info.characteristic.instance_id = event_data->char_id.inst_id;
3749 /* Fill Service informations in buffer */
3750 memcpy(&write_info.svc.uuid,
3751 event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3752 write_info.svc.instance_id = event_data->srvc_id.id.inst_id;
3754 /* Fill remote device address */
3755 _bt_convert_addr_string_to_type(write_info.device_address.addr, conn_info->addr);
3757 if (event_data->conn_status.status != OAL_STATUS_SUCCESS) {
3758 result = BLUETOOTH_ERROR_INTERNAL;
3764 svc_uuid = g_memdup(&event_data->srvc_id.id.uuid.uuid[0], uuid_len);
3766 data_svc_uuid = g_variant_new_from_data(
3767 G_VARIANT_TYPE_BYTESTRING,
3773 char_uuid = g_memdup(&event_data->char_id.uuid.uuid[0], uuid_len);
3775 data_char_uuid = g_variant_new_from_data(
3776 G_VARIANT_TYPE_BYTESTRING,
3782 desc_uuid = g_memdup(&event_data->descr_id.uuid.uuid[0], uuid_len);
3784 data_desc_uuid = g_variant_new_from_data(
3785 G_VARIANT_TYPE_BYTESTRING,
3790 param = g_variant_new("(isn@ayin@ayin@ayi)", result,
3794 event_data->srvc_id.id.inst_id,
3797 event_data->char_id.inst_id,
3800 event_data->descr_id.inst_id);
3803 _bt_send_event(BT_GATT_CLIENT_EVENT,
3804 BLUETOOTH_EVENT_GATT_WRITE_DESC,
3815 /* Send DBUS return */
3816 __bt_gatt_handle_pending_request_info(result,
3817 BT_GATT_WRITE_DESCRIPTOR_VALUE,
3819 sizeof(bluetooth_gatt_client_desc_prop_info_t));
3822 static void __bt_hanlde_le_device_disconnection(event_dev_conn_status_t *event_data)
3824 int result = BLUETOOTH_ERROR_INTERNAL;
3825 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
3827 _bt_convert_addr_type_to_string(address, (unsigned char *)event_data->address.addr);
3829 /* DBUS Return with fail of pending BT_CONNECT_LE for all the apps */
3830 BT_INFO("Local GATT Client disconnected: Remote addr[%s] ", address);
3832 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE, address,
3833 BT_ADDRESS_STRING_SIZE);
3837 static void __bt_handle_client_notification_registered(
3838 event_gattc_regdereg_notify_t *event_data,
3839 gboolean is_registered)
3841 int result = BLUETOOTH_ERROR_NONE;
3842 struct gatt_server_info_t *conn_info = NULL;
3843 bt_gatt_notif_reg_info_t notif_info;
3846 memset(¬if_info, 0x00, sizeof(bt_gatt_notif_reg_info_t));
3848 BT_INFO("Client Interface [%d] status [%d]",
3849 event_data->client_if,
3850 event_data->status);
3852 /* Extract Address from conn_id of event data */
3853 conn_info = __bt_find_remote_gatt_server_info_from_client_if(
3854 event_data->client_if);
3857 BT_INFO("Connection Info is not present, return");
3860 BT_INFO("Notification Registered for addr [%s]",
3864 /* Fill svc informations in buffer */
3865 memcpy(¬if_info.svc_uuid,
3866 event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3867 notif_info.svc_inst = event_data->srvc_id.id.inst_id;
3869 /* Fill char in buffer */
3870 memcpy(¬if_info.char_uuid,
3871 event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3872 notif_info.char_inst = event_data->char_id.inst_id;
3874 /* Fill remote device address */
3875 _bt_convert_addr_string_to_type(notif_info.addr.addr, conn_info->addr);
3877 notif_info.is_registered = is_registered;
3879 if (event_data->status != OAL_STATUS_SUCCESS)
3880 result = BLUETOOTH_ERROR_INTERNAL;
3882 /* Send DBUS Return for BT_GATT_WATCH_CHARACTERISTIC */
3883 __bt_gatt_handle_pending_request_info(result,
3884 BT_GATT_WATCH_CHARACTERISTIC,
3886 sizeof(bt_gatt_notif_reg_info_t));
3889 static void __bt_handle_client_notification_data(event_gattc_notify_data *event_data)
3891 /* No status in this event from OAL */
3892 int result = BLUETOOTH_ERROR_NONE;
3894 /* Read Information data structures */
3895 GVariant *param = NULL;
3896 GVariant *data = NULL;
3897 GVariant *data_svc_uuid = NULL;
3898 GVariant *data_char_uuid = NULL;
3899 char *read_val = NULL;
3900 char *svc_uuid = NULL;
3901 char *char_uuid = NULL;
3907 BT_INFO("Notifcation of charc data changed");
3909 if (event_data->data_len > 0) {
3911 for (i = 0; i < event_data->data_len; i++)
3912 BT_INFO("Data[%d] = [0x%x]", i, event_data->data[i]);
3915 addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
3916 _bt_convert_addr_type_to_string(addr,
3917 (unsigned char *)&(event_data->address.addr));
3920 read_val = g_memdup(&event_data->data[0], event_data->data_len);
3922 data = g_variant_new_from_data(
3923 G_VARIANT_TYPE_BYTESTRING,
3925 event_data->data_len,
3928 svc_uuid = g_memdup(&event_data->srvc_id.id.uuid.uuid[0], uuid_len);
3930 data_svc_uuid = g_variant_new_from_data(
3931 G_VARIANT_TYPE_BYTESTRING,
3937 char_uuid = g_memdup(&event_data->char_id.uuid.uuid[0], uuid_len);
3939 data_char_uuid = g_variant_new_from_data(
3940 G_VARIANT_TYPE_BYTESTRING,
3946 param = g_variant_new("(isn@ayin@ayin@ay)", result,
3950 event_data->srvc_id.id.inst_id,
3953 event_data->char_id.inst_id,
3954 event_data->data_len,
3958 _bt_send_event(BT_GATT_CLIENT_EVENT,
3959 BLUETOOTH_EVENT_GATT_CHAR_VAL_CHANGED,
3962 BT_ERR("No Data!!");
3975 gboolean _bt_is_remote_gatt_device_connected(bluetooth_device_address_t *address)
3978 struct gatt_server_info_t *conn_info = NULL;
3979 gboolean connected = FALSE;
3982 addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
3983 _bt_convert_addr_type_to_string(addr,
3984 (unsigned char *)&(address->addr));
3986 BT_INFO("Check Connected or not for [%s]", addr);
3987 /* Check if device is already in connected list */
3988 conn_info = __bt_find_remote_gatt_server_info(addr);
3991 BT_INFO("Remote GATT Server device [%s] is Connected", conn_info->addr);
3994 struct gatt_client_info_t *client_info = NULL;
3996 BT_INFO("Remote GATT Server Device [%s] is not Connected", addr);
3998 /* Check if device is already in connected list */
3999 client_info = __bt_find_remote_gatt_client_info(addr);
4002 BT_INFO("Remote Client device [%s] is Connected", client_info->addr);
4005 BT_INFO("Remote GATT Client Device [%s] is not Connected", addr);
4014 int _bt_connect_le_device(bluetooth_device_address_t *address,
4015 int auto_connect, int client_id)
4017 struct gatt_server_info_t *conn_info = NULL;
4018 struct gatt_out_conn_info_t *out_conn_info = NULL;
4020 invocation_info_t *req_info = NULL;
4021 int ret = OAL_STATUS_SUCCESS;
4023 char *remote_address = NULL;
4025 BT_CHECK_PARAMETER(address, return);
4029 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4030 _bt_convert_addr_type_to_string(addr, address->addr);
4031 BT_INFO("GATT Client connect request for address [%s] client instance [%d]",
4035 /* Check if Remote Device is already under connection progress */
4036 req_info = _bt_get_request_info_data_from_function_name(BT_CONNECT_LE);
4038 remote_address = (char*)req_info->user_data;
4039 if (remote_address && !strcasecmp(remote_address, addr)) {/* Address matched */
4040 BT_INFO("Already Connection ongoing for same remote GATT Server address [%s]", remote_address);
4041 /* Return and wait for events to be sent to all apps */
4043 return BLUETOOTH_ERROR_IN_PROGRESS;
4047 /* Check if remote GATT Server is connected or not */
4048 conn_info = __bt_find_remote_gatt_server_info(addr);
4050 BT_ERR("GATT Server is already connected..");
4052 return BLUETOOTH_ERROR_ALREADY_CONNECT;
4055 /* TODO Check Requirement of holding Advertisement before initiating LE connect */
4057 /* Check if app sent 0 client id for connection, in such case, use default gatt client ID */
4058 if (client_id == 0) {
4059 BT_INFO("GATT CLient connect request sent by an app without any client instance [%d]",
4061 BT_INFO("Assign default GATT client id [%d]", gatt_default_client);
4062 client_id = gatt_default_client;
4065 BT_INFO("Connect using CLient ID [%d]", client_id);
4066 ret = gattc_connect(client_id, (bt_address_t*)(address), auto_connect);
4068 if (ret != OAL_STATUS_SUCCESS) {
4069 BT_ERR("ret: %d", ret);
4071 return BLUETOOTH_ERROR_INTERNAL;
4074 /* Mark this as outgoing connection */
4075 out_conn_info = g_new0(struct gatt_out_conn_info_t, 1);
4076 out_conn_info->addr = g_strdup(addr);
4077 out_conn_info->client_id = client_id;
4078 BT_INFO("Added outgoing connection info addr[%s]", out_conn_info->addr);
4079 outgoing_gatt_conn_list = g_slist_append(outgoing_gatt_conn_list, out_conn_info);
4082 return BLUETOOTH_ERROR_NONE;
4085 int _bt_gatt_get_primary_services(char *address)
4087 BT_CHECK_PARAMETER(address, return);
4088 struct gatt_server_info_t *conn_info = NULL;
4089 invocation_info_t *req_info = NULL;
4090 int ret = OAL_STATUS_SUCCESS;
4093 /* Check if any app is already browsing primary services on the same remote GATT Server */
4094 req_info = _bt_get_request_info_data(BT_GATT_GET_PRIMARY_SERVICES, address);
4096 BT_INFO("Already Primary Service Browsing ongoing for same rmeote GATT Server");
4097 /* Return and wait for events to be sent to all apps */
4098 return BLUETOOTH_ERROR_NONE;
4101 /* Check if remote GATT Server is connected or not */
4102 conn_info = __bt_find_remote_gatt_server_info(address);
4104 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4105 conn_info->addr, conn_info->connection_id);
4107 BT_ERR("GATT Server is not yet connected..");
4108 return BLUETOOTH_ERROR_NOT_CONNECTED;
4111 /* Send Primary Service Browsing request to stack */
4112 ret = gattc_search_service(conn_info->connection_id, NULL);
4113 if (ret != OAL_STATUS_SUCCESS) {
4114 BT_ERR("ret: %d", ret);
4115 return BLUETOOTH_ERROR_INTERNAL;
4117 return BLUETOOTH_ERROR_NONE;
4120 int _bt_gatt_get_all_characteristic(bluetooth_gatt_client_svc_prop_info_t *svc)
4122 BT_CHECK_PARAMETER(svc, return);
4123 struct gatt_server_info_t *conn_info = NULL;
4124 invocation_info_t *req_info = NULL;
4125 bluetooth_gatt_client_svc_prop_info_t *prop;
4126 oal_gatt_srvc_id_t srvc_id;
4127 int ret = OAL_STATUS_SUCCESS;
4131 /* Check if any app is already browsing characteristics of the same service on the same remote GATT Server */
4132 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_GET_SERVICE_PROPERTIES);
4134 prop = (bluetooth_gatt_client_svc_prop_info_t*)req_info->user_data;
4135 if (prop && !memcmp(svc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t))
4136 && memcmp(prop->svc.uuid, svc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
4137 && prop->svc.instance_id == svc->svc.instance_id) {
4138 BT_INFO("Already Properties browsing for Primary Service ongoing for same remote GATT Server");
4139 /* Return and wait for events to be sent to all apps */
4140 return BLUETOOTH_ERROR_NONE;
4144 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4145 _bt_convert_addr_type_to_string(addr, svc->device_address.addr);
4147 /* Check if remote GATT Server is connected or not */
4148 conn_info = __bt_find_remote_gatt_server_info(addr);
4150 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4151 conn_info->addr, conn_info->connection_id);
4153 BT_ERR("GATT Server is not yet connected..");
4155 return BLUETOOTH_ERROR_NOT_CONNECTED;
4158 srvc_id.is_prmry = TRUE;
4159 srvc_id.id.inst_id = svc->svc.instance_id;
4160 memcpy(srvc_id.id.uuid.uuid, svc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4162 /* Search All Characteristic */
4163 ret = gattc_get_characteristic(conn_info->connection_id, &srvc_id, NULL);
4164 if (ret != OAL_STATUS_SUCCESS) {
4165 BT_ERR("ret: %d", ret);
4167 return BLUETOOTH_ERROR_INTERNAL;
4170 return BLUETOOTH_ERROR_NONE;
4173 int _bt_gatt_get_all_characteristic_properties(
4174 bluetooth_gatt_client_char_prop_info_t *chr)
4176 struct gatt_server_info_t *conn_info = NULL;
4177 invocation_info_t *req_info = NULL;
4178 bluetooth_gatt_client_char_prop_info_t *prop;
4179 oal_gatt_srvc_id_t srvc_id;
4180 oal_gatt_id_t char_id;
4181 int ret = OAL_STATUS_SUCCESS;
4184 BT_CHECK_PARAMETER(chr, return);
4188 /* Check if any app is already browsing descriptors of the same char of
4189 particular service on the same remote GATT Server */
4190 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_GET_CHARACTERISTIC_PROPERTIES);
4192 prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
4193 if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4194 && memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4195 && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4196 && memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4197 && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
4198 BT_INFO("Already Properties browsing for Characteristic ongoing for same remote GATT Server");
4199 /* Return and wait for events to be sent to all apps */
4200 return BLUETOOTH_ERROR_NONE;
4204 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4205 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4207 /* Check if remote GATT Server is connected or not */
4208 conn_info = __bt_find_remote_gatt_server_info(addr);
4210 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4211 conn_info->addr, conn_info->connection_id);
4213 BT_ERR("GATT Server is not yet connected..");
4215 return BLUETOOTH_ERROR_NOT_CONNECTED;
4218 srvc_id.is_prmry = TRUE;
4219 srvc_id.id.inst_id = chr->svc.instance_id;
4220 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4222 char_id.inst_id = chr->characteristic.instance_id;
4223 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4225 /* Search All Descriptors */
4226 ret = gattc_get_descriptor(conn_info->connection_id, &srvc_id, &char_id, NULL);
4227 if (ret != OAL_STATUS_SUCCESS) {
4228 BT_ERR("ret: %d", ret);
4230 return BLUETOOTH_ERROR_INTERNAL;
4233 return BLUETOOTH_ERROR_NONE;
4236 int _bt_gatt_read_characteristic_value(
4237 bluetooth_gatt_client_char_prop_info_t *chr)
4239 struct gatt_server_info_t *conn_info = NULL;
4240 invocation_info_t *req_info = NULL;
4241 bluetooth_gatt_client_char_prop_info_t *prop;
4242 oal_gatt_srvc_id_t srvc_id;
4243 oal_gatt_id_t char_id;
4244 int ret = OAL_STATUS_SUCCESS;
4247 BT_CHECK_PARAMETER(chr, return);
4251 /* Check if any app is already Reading characteristic of the same char of
4252 particular service on the same remote GATT Server */
4253 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_READ_CHARACTERISTIC);
4255 prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
4256 if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4257 && memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4258 && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4259 && memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4260 && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
4261 BT_INFO("Already Characteristic value Read operation in progress for same remote GATT Server");
4262 /* Return and wait for events to be sent to all apps */
4263 return BLUETOOTH_ERROR_NONE;
4267 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4268 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4270 /* Check if remote GATT Server is connected or not */
4271 conn_info = __bt_find_remote_gatt_server_info(addr);
4273 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4274 conn_info->addr, conn_info->connection_id);
4276 BT_ERR("GATT Server is not yet connected..");
4278 return BLUETOOTH_ERROR_NOT_CONNECTED;
4281 srvc_id.is_prmry = TRUE;
4282 srvc_id.id.inst_id = chr->svc.instance_id;
4283 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4285 char_id.inst_id = chr->characteristic.instance_id;
4286 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4288 /* Search All Descriptors */
4289 ret = gattc_read_characteristic(conn_info->connection_id, &srvc_id, &char_id, OAL_GATT_AUTH_REQ_NONE);
4290 if (ret != OAL_STATUS_SUCCESS) {
4291 BT_ERR("ret: %d", ret);
4293 return BLUETOOTH_ERROR_INTERNAL;
4296 return BLUETOOTH_ERROR_NONE;
4299 int _bt_gatt_read_descriptor_value(
4300 bluetooth_gatt_client_desc_prop_info_t *desc)
4302 struct gatt_server_info_t *conn_info = NULL;
4303 invocation_info_t *req_info = NULL;
4304 bluetooth_gatt_client_desc_prop_info_t *prop;
4305 oal_gatt_srvc_id_t srvc_id;
4306 oal_gatt_id_t char_id;
4307 oal_gatt_id_t desc_id;
4308 int ret = OAL_STATUS_SUCCESS;
4311 BT_CHECK_PARAMETER(desc, return);
4315 /* Check if any app is already Reading descriptors of the same char of
4316 particular service on the same remote GATT Server */
4317 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_READ_DESCRIPTOR_VALUE);
4319 prop = (bluetooth_gatt_client_desc_prop_info_t*)req_info->user_data;
4320 if (prop && !memcmp(desc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4321 && memcmp(desc->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4322 && desc->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4323 && memcmp(desc->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4324 && desc->characteristic.instance_id == prop->characteristic.instance_id /* Characteristic Instance ID matched */
4325 && memcmp(desc->descriptor.uuid, prop->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Descriptor UUID matched */
4326 && desc->descriptor.instance_id == prop->descriptor.instance_id) { /* Descriptor Instance ID matched */
4327 BT_INFO("Already Descriptor value Read operation in progress for same remote GATT Server");
4328 /* Return and wait for events to be sent to all apps */
4329 return BLUETOOTH_ERROR_NONE;
4333 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4334 _bt_convert_addr_type_to_string(addr, desc->device_address.addr);
4336 /* Check if remote GATT Server is connected or not */
4337 conn_info = __bt_find_remote_gatt_server_info(addr);
4339 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4340 conn_info->addr, conn_info->connection_id);
4342 BT_ERR("GATT Server is not yet connected..");
4344 return BLUETOOTH_ERROR_NOT_CONNECTED;
4347 srvc_id.is_prmry = TRUE;
4348 srvc_id.id.inst_id = desc->svc.instance_id;
4349 memcpy(srvc_id.id.uuid.uuid, desc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4351 char_id.inst_id = desc->characteristic.instance_id;
4352 memcpy(char_id.uuid.uuid, desc->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4354 desc_id.inst_id = desc->descriptor.instance_id;
4355 memcpy(desc_id.uuid.uuid, desc->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4357 /* Search All Descriptors */
4358 ret = gattc_read_descriptor(conn_info->connection_id, &srvc_id, &char_id,
4359 &desc_id, OAL_GATT_AUTH_REQ_NONE);
4360 if (ret != OAL_STATUS_SUCCESS) {
4361 BT_ERR("ret: %d", ret);
4363 return BLUETOOTH_ERROR_INTERNAL;
4366 return BLUETOOTH_ERROR_NONE;
4370 int _bt_gatt_acquire_notify(bluetooth_gatt_client_char_prop_info_t *chr, int *fd, int *mtu)
4372 struct gatt_server_info_t *conn_info = NULL;
4373 oal_gatt_srvc_id_t srvc_id;
4374 oal_gatt_id_t char_id;
4375 int ret = OAL_STATUS_SUCCESS;
4378 BT_CHECK_PARAMETER(chr, return);
4382 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4383 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4385 /* Check if remote GATT Server is connected or not */
4386 conn_info = __bt_find_remote_gatt_server_info(addr);
4388 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4389 conn_info->addr, conn_info->connection_id);
4391 BT_ERR("GATT Server is not yet connected..");
4393 return BLUETOOTH_ERROR_NOT_CONNECTED;
4396 srvc_id.is_prmry = TRUE;
4397 srvc_id.id.inst_id = chr->svc.instance_id;
4398 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4400 char_id.inst_id = chr->characteristic.instance_id;
4401 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4403 ret = gattc_acquire_notify(conn_info->connection_id, &srvc_id, &char_id, fd, mtu);
4405 if (ret != OAL_STATUS_SUCCESS) {
4406 BT_ERR("ret: %d", ret);
4408 return BLUETOOTH_ERROR_INTERNAL;
4410 BT_INFO("GATT characterstics FD [%d] mtu[%d]", *fd, *mtu);
4412 return BLUETOOTH_ERROR_NONE;
4416 int _bt_gatt_acquire_write(bluetooth_gatt_client_char_prop_info_t *chr, int *fd, int *mtu)
4419 struct gatt_server_info_t *conn_info = NULL;
4420 oal_gatt_srvc_id_t srvc_id;
4421 oal_gatt_id_t char_id;
4422 int ret = OAL_STATUS_SUCCESS;
4425 BT_CHECK_PARAMETER(chr, return);
4429 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4430 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4432 /* Check if remote GATT Server is connected or not */
4433 conn_info = __bt_find_remote_gatt_server_info(addr);
4435 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4436 conn_info->addr, conn_info->connection_id);
4438 BT_ERR("GATT Server is not yet connected..");
4440 return BLUETOOTH_ERROR_NOT_CONNECTED;
4443 srvc_id.is_prmry = TRUE;
4444 srvc_id.id.inst_id = chr->svc.instance_id;
4445 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4447 char_id.inst_id = chr->characteristic.instance_id;
4448 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4450 ret = gattc_acquire_write(conn_info->connection_id, &srvc_id, &char_id,
4451 OAL_GATT_AUTH_REQ_NONE, fd, mtu);
4452 if (ret != OAL_STATUS_SUCCESS) {
4453 BT_ERR("ret: %d", ret);
4455 return BLUETOOTH_ERROR_INTERNAL;
4457 BT_INFO("GATT characterstics FD [%d] mtu [%d]", *fd, *mtu);
4459 return BLUETOOTH_ERROR_NONE;
4464 /* Write Characteristic */
4465 int _bt_gatt_write_characteristic_value_by_type(
4466 bluetooth_gatt_client_char_prop_info_t *chr,
4467 bluetooth_gatt_att_data_t *data,
4468 bluetooth_gatt_write_type_e write_type)
4470 struct gatt_server_info_t *conn_info = NULL;
4471 invocation_info_t *req_info = NULL;
4472 bluetooth_gatt_client_char_prop_info_t *prop;
4473 oal_gatt_srvc_id_t srvc_id;
4474 oal_gatt_id_t char_id;
4475 int ret = OAL_STATUS_SUCCESS;
4479 BT_CHECK_PARAMETER(chr, return);
4480 BT_CHECK_PARAMETER(data, return);
4484 /* Check if any app is already writing same char of
4485 particular service on the same remote GATT Server */
4486 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE);
4488 prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
4489 if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4490 && memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4491 && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4492 && memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4493 && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
4494 BT_INFO("Already Characteristic Write Value operation in progress for same remote GATT Server");
4495 /* Return and wait for events to be sent to all apps */
4496 return BLUETOOTH_ERROR_NONE;
4500 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4501 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4503 /* Check if remote GATT Server is connected or not */
4504 conn_info = __bt_find_remote_gatt_server_info(addr);
4506 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4507 conn_info->addr, conn_info->connection_id);
4509 BT_ERR("GATT Server is not yet connected..");
4511 return BLUETOOTH_ERROR_NOT_CONNECTED;
4514 srvc_id.is_prmry = TRUE;
4515 srvc_id.id.inst_id = chr->svc.instance_id;
4516 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4518 char_id.inst_id = chr->characteristic.instance_id;
4519 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4521 /* Write CHar value */
4523 BT_INFO("Connection ID [%d] write type [%d] data length [%d]", conn_info->connection_id, write_type, data->length);
4524 for (k = 0; k < data->length; k++)
4525 BT_INFO("Data[%d] [0x%x]", k, data->data[k]);
4527 ret = gattc_write_characteristic(conn_info->connection_id,
4529 (oal_gatt_write_type_t)write_type, data->length,
4530 OAL_GATT_AUTH_REQ_NONE, (char *)(&data->data[0]));
4531 if (ret != OAL_STATUS_SUCCESS) {
4532 BT_ERR("ret: %d", ret);
4534 return BLUETOOTH_ERROR_INTERNAL;
4537 return BLUETOOTH_ERROR_NONE;
4540 /* Write Descriptor */
4541 int _bt_gatt_write_descriptor_value_by_type(
4542 bluetooth_gatt_client_desc_prop_info_t *desc,
4543 bluetooth_gatt_att_data_t *data,
4544 bluetooth_gatt_write_type_e write_type)
4546 struct gatt_server_info_t *conn_info = NULL;
4547 invocation_info_t *req_info = NULL;
4548 bluetooth_gatt_client_desc_prop_info_t *prop;
4549 oal_gatt_srvc_id_t srvc_id;
4550 oal_gatt_id_t char_id;
4551 oal_gatt_id_t desc_id;
4552 int ret = OAL_STATUS_SUCCESS;
4556 BT_CHECK_PARAMETER(desc, return);
4557 BT_CHECK_PARAMETER(data, return);
4561 /* Check if any app is already writing on same Descriptor of the same char of
4562 particular service on the same remote GATT Server */
4563 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_WRITE_DESCRIPTOR_VALUE);
4565 prop = (bluetooth_gatt_client_desc_prop_info_t*)req_info->user_data;
4566 if (prop && !memcmp(desc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4567 && memcmp(desc->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4568 && desc->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4569 && memcmp(desc->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4570 && desc->characteristic.instance_id == prop->characteristic.instance_id /* Characteristic Instance ID matched */
4571 && memcmp(desc->descriptor.uuid, prop->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Descriptor UUID matched */
4572 && desc->descriptor.instance_id == prop->descriptor.instance_id) { /* Descriptor Instance ID matched */
4573 BT_INFO("Already Descriptor value Write operation in progress for same remote GATT Server");
4574 /* Return and wait for events to be sent to all apps */
4575 return BLUETOOTH_ERROR_NONE;
4579 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4580 _bt_convert_addr_type_to_string(addr, desc->device_address.addr);
4582 /* Check if remote GATT Server is connected or not */
4583 conn_info = __bt_find_remote_gatt_server_info(addr);
4585 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4586 conn_info->addr, conn_info->connection_id);
4588 BT_ERR("GATT Server is not yet connected..");
4590 return BLUETOOTH_ERROR_NOT_CONNECTED;
4593 srvc_id.is_prmry = TRUE;
4594 srvc_id.id.inst_id = desc->svc.instance_id;
4595 memcpy(srvc_id.id.uuid.uuid, desc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4597 char_id.inst_id = desc->characteristic.instance_id;
4598 memcpy(char_id.uuid.uuid, desc->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4600 desc_id.inst_id = desc->descriptor.instance_id;
4601 memcpy(desc_id.uuid.uuid, desc->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4604 BT_INFO("Connection ID [%d] write type [%d] data length [%d]", conn_info->connection_id, write_type, data->length);
4605 for (k = 0; k < data->length; k++)
4606 BT_INFO("Data[%d] [0x%x]", k, data->data[k]);
4608 ret = gattc_write_descriptor(conn_info->connection_id,
4609 &srvc_id, &char_id, &desc_id,
4610 (oal_gatt_write_type_t)write_type, data->length,
4611 OAL_GATT_AUTH_REQ_NONE, (char *)(&data->data[0]));
4612 if (ret != OAL_STATUS_SUCCESS) {
4613 BT_ERR("ret: %d", ret);
4615 return BLUETOOTH_ERROR_INTERNAL;
4618 return BLUETOOTH_ERROR_NONE;
4621 int _bt_gatt_watch_characteristic(
4622 bluetooth_gatt_client_char_prop_info_t *chr,
4626 struct gatt_server_info_t *conn_info = NULL;
4627 oal_gatt_srvc_id_t srvc_id;
4628 oal_gatt_id_t char_id;
4629 int ret = OAL_STATUS_SUCCESS;
4632 BT_CHECK_PARAMETER(chr, return);
4634 BT_INFO("Client ID [%d] Is Notify [%d]", client_id, is_notify);
4636 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4637 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4639 /* Check if remote GATT Server is connected or not */
4640 conn_info = __bt_find_remote_gatt_server_info(addr);
4642 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4643 conn_info->addr, conn_info->connection_id);
4645 BT_ERR("GATT Server is not yet connected..");
4647 return BLUETOOTH_ERROR_NOT_CONNECTED;
4649 srvc_id.is_prmry = TRUE;
4650 srvc_id.id.inst_id = chr->svc.instance_id;
4651 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4653 char_id.inst_id = chr->characteristic.instance_id;
4654 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4656 /* Register or unregister Notification characteristic */
4658 ret = gattc_register_for_notification(conn_info->client_id,
4659 (bt_address_t*)&(chr->device_address),
4660 &srvc_id, &char_id);
4662 ret = gattc_deregister_for_notification(conn_info->client_id,
4663 (bt_address_t*)&(chr->device_address),
4664 &srvc_id, &char_id);
4666 BT_INFO("Result[%d]", ret);
4667 if (ret != OAL_STATUS_SUCCESS) {
4668 BT_ERR("ret: %d", ret);
4670 return BLUETOOTH_ERROR_INTERNAL;
4673 return BLUETOOTH_ERROR_NONE;
4677 int _bt_disconnect_le_device(bluetooth_device_address_t *address,
4680 struct gatt_server_info_t *conn_info = NULL;
4681 struct gatt_client_info_t *rem_client_conn_info = NULL;
4682 invocation_info_t *req_info = NULL;
4683 int ret = OAL_STATUS_SUCCESS;
4685 char *remote_address = NULL;
4687 BT_CHECK_PARAMETER(address, return);
4691 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4692 _bt_convert_addr_type_to_string(addr, address->addr);
4693 BT_INFO("GATT Client Disconnect request for address [%s]", addr);
4695 /* Check if Remote Device is already under connection progress */
4696 req_info = _bt_get_request_info_data_from_function_name(BT_DISCONNECT_LE);
4698 remote_address = (char*)req_info->user_data;
4699 if (remote_address && !strcasecmp(remote_address, addr)) {/* Address matched */
4700 BT_INFO("Already DisConnection ongoing for same remote GATT Server address [%s]", remote_address);
4701 /* Return success and wait for events to be sent to all apps */
4703 return BLUETOOTH_ERROR_IN_PROGRESS;
4706 /* Check if remote GATT Server is connected or not */
4707 conn_info = __bt_find_remote_gatt_server_info(addr);
4709 /* Check if app sent 0 client id for Disconnection, in such case, use default gatt client ID */
4710 if (client_id == 0) {
4711 BT_INFO("GATT CLient Disconnect request sent by an app without any client instance [%d]",
4713 BT_INFO("Assign default GATT client id [%d]", gatt_default_client);
4714 client_id = gatt_default_client;
4717 BT_INFO("Disconnect remote gatt server using CLient ID [%d] Connection ID [%d]", client_id, conn_info->connection_id);
4718 ret = gattc_disconnect(client_id, (bt_address_t*)(address),
4719 conn_info->connection_id);
4721 /* check if remote client is connected */
4722 rem_client_conn_info = __bt_find_remote_gatt_client_info(addr);
4724 if (!rem_client_conn_info || client_id != 0) {
4725 BT_ERR("GATT device is not connected..");
4727 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
4730 BT_INFO("Disconnect remote gatt client ");
4732 ret = gatts_disconnect(rem_client_conn_info->instance_id,
4733 (bt_address_t*)(address), rem_client_conn_info->connection_id);
4736 if (ret != OAL_STATUS_SUCCESS) {
4737 BT_ERR("ret: %d", ret);
4739 return BLUETOOTH_ERROR_INTERNAL;
4742 return BLUETOOTH_ERROR_NONE;
4745 int _bt_gatt_watch_service_changed_indication(const char *sender,
4746 bluetooth_device_address_t *address,
4747 gboolean is_enabled)
4750 bt_service_app_info_t *info = NULL;
4752 BT_INFO("Enable Servic changed Indication watcher [%d] for app [%s]",
4753 is_enabled, sender);
4755 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
4758 if (g_strcmp0(sender, info->sender) == 0 &&
4759 memcmp(info->address.addr, address->addr,
4760 sizeof(bluetooth_device_address_t)) == 0) {
4761 BT_INFO("Found GATT client App.. UUID [%s], sender [%s]", info->uuid, info->sender);
4762 info->is_watcher_enabled = is_enabled;
4766 return BLUETOOTH_ERROR_NONE;
4769 int _bt_unregister_gatt_client_instance(const char *sender, int client_id)
4771 bt_service_app_info_t *info = NULL;
4774 BT_INFO("Unregister Allocated GATT Client instance [%s] Client ID [%d]", sender, client_id);
4776 /* Unregister CLient instance associated with address X. It is possible that another app still
4777 has client_id valid for same remote address */
4778 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
4781 /* Exact matching of sender */
4782 if (!g_strcmp0(info->sender, sender) && info->client_id == client_id) { /* Check for only valid GATT client Instance */
4783 BT_INFO("Unregister GATT client instance [%d]", info->client_id);
4784 numapps[k].client_id = -1;
4785 numapps[k].is_initialized = FALSE;
4786 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
4787 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
4788 memset(&numapps[k].address.addr, 0x00, sizeof(bluetooth_device_address_t));
4790 /* Its a GATT Client Instance */
4791 ret = gattc_deregister(client_id);
4792 if (ret != OAL_STATUS_SUCCESS) {
4793 BT_ERR("DeAllocate GATT Client instance with stack Fail ret: %d", ret);
4794 return BLUETOOTH_ERROR_INTERNAL;
4796 return BLUETOOTH_ERROR_NONE;
4801 return BLUETOOTH_ERROR_NOT_FOUND;