#include "bt-service-gatt.h"
#include "bt-service-core-device.h"
#include "bt-service-core-adapter-le.h"
+#ifdef TIZEN_GATT_CLIENT
+#include "bluetooth-gatt-client-api.h"
+#endif
#include <oal-hardware.h>
#include <oal-manager.h>
#define BT_UUID_STRING_MAX 64
#define BT_SENDER_MAX_LENGTH 50
#define MAX_APPS_SUPPORTED 11 /* Slot 0 is not used */
+
+#ifdef TIZEN_GATT_CLIENT
+#define NUM_UUID 20
+#else
#define NUM_UUID 10
+#endif
+
+
#define UUID_MAX_LEN 50
#define BDADDR_ANY (&(bluetooth_device_address_t) {{0, 0, 0, 0, 0, 0} })
+#ifdef TIZEN_GATT_CLIENT
+char uuid_list[NUM_UUID][BT_UUID_STRING_MAX] = {"0000b00b-0000-0000-f065-080080fa49b5", /* Used by BLEAPP */
+ "0000b00b-1111-1111-0123-456789ab0cd2", /* Used by BLEAPP */
+ "0000b00b-2222-1111-0123-456789ab0cd2",
+ "0000b00b-3333-1111-0123-456789ab0cd2",
+ "0000b00b-4444-1111-0123-456789ab0cd2",
+ "0000b00b-5555-1111-0123-456789ab0cd2",
+ "0000b00b-6666-1111-0123-456789ab0cd2",
+ "0000b00b-7777-1111-0123-456789ab0cd2",
+ "0000b00b-8888-1111-0123-456789ab0cd2",
+ "0000b00b-9999-1111-0123-456789ab0cd2",
+ "0000b00b-aaaa-1111-0123-456789ab0cd2",
+ "0000b00b-bbbb-1111-0123-456789ab0cd2",
+ "0000b00b-cccc-1111-0123-456789ab0cd2",
+ "0000b00b-dddd-1111-0123-456789ab0cd2",
+ "0000b00b-eeee-1111-0123-456789ab0cd2",
+ "0000b00b-ffff-1111-0123-456789ab0cd2",
+ "0000b00c-0000-1111-0123-456789ab0cd2",
+ "0000b00c-1111-1111-0123-456789ab0cd2",
+ "0000b00c-2222-1111-0123-456789ab0cd2",
+ "0000b00c-3333-1111-0123-456789ab0cd2"};
+#else
char uuid_list[NUM_UUID][BT_UUID_STRING_MAX] = {"0000b00b-0000-0000-f065-080080fa49b5", /* Used by BLEAPP */
"0000b00b-1111-1111-0123-456789ab0cd2", /* Used by BLEAPP */
"0000b00b-2222-1111-0123-456789ab0cd2",
"0000b00b-7777-1111-0123-456789ab0cd2",
"0000b00b-8888-1111-0123-456789ab0cd2",
"0000b00b-9999-1111-0123-456789ab0cd2"};
+#endif
+
+#ifdef TIZEN_GATT_CLIENT
+/* Reserved GATT client Instance UUID. This is used only internally by bt-service */
+#define DEFAULT_GATT_CLIENT_UUID "0000a00a-1111-1111-0123-456789abcdef"
+
+static int gatt_default_client = -1;
+
+typedef struct {
+ gboolean is_registered;
+ bluetooth_device_address_t addr;
+ unsigned char svc_uuid[BLUETOOTH_UUID_HEX_MAX_LEN];
+ int svc_inst;
+ unsigned char char_uuid[BLUETOOTH_UUID_HEX_MAX_LEN];
+ int char_inst;
+} bt_gatt_notif_reg_info_t;
+
+struct gatt_out_conn_info_t {
+ int client_id; /* This value unique identifies a GATT Client instance */
+ char *addr; /* Remote GATT Server address */
+};
+
+/* Linked List of outgoing gatt connection list
+ Note: This is valid only for local GATT client */
+static GSList *outgoing_gatt_conn_list = NULL;
+
+/* GATT Server Info(Local Client) List Structure */
+struct gatt_server_info_t {
+ int connection_id; /* This value will uniquely identify a GATT client-server connection */
+ int client_id; /* This value unique identifies a GATT Client instance */
+ char *addr; /* Remote GATT Server address */
+};
+
+/* Linked List of connected Remote GATT Servers */
+static GSList *gatt_server_info_list = NULL;
+
+typedef struct {
+ gboolean is_changed;
+ unsigned char uuid[BLUETOOTH_UUID_HEX_MAX_LEN]; /* If any service added */
+} bt_gatt_svc_changed_info_t;
+
+
+typedef struct {
+ int conn_id;
+ // bluetooth_device_address_t address; /* Remote BLE Device Address */
+ GSList *services; /* List of all services of above remote device */
+ int count; /* Number of services browsed from remote device */
+ bt_gatt_svc_changed_info_t info;
+} bt_gatt_service_info_list_t;
+
+typedef struct {
+ unsigned char uuid[BLUETOOTH_UUID_HEX_MAX_LEN];
+ int inst_id;
+ int is_primary;
+ GSList *chars;
+ GSList *included_svcs;
+ gboolean is_removed; /* 0 => Service is added, 1=> removed */
+} bt_gatt_service_info_t;
+typedef struct {
+ unsigned char uuid[BLUETOOTH_UUID_HEX_MAX_LEN];
+ int inst_id;
+ int props;
+ int val_len;
+ unsigned char val[BLUETOOTH_GATT_ATT_DATA_LENGTH_MAX];
+ GSList *descs;
+} bt_gatt_char_info_t;
+
+typedef struct {
+ unsigned char uuid[BLUETOOTH_UUID_HEX_MAX_LEN];
+ int inst_id;
+ int val_len;
+ unsigned char val[BLUETOOTH_GATT_ATT_DATA_LENGTH_MAX];
+} bt_gatt_descriptor_info_t;
+
+typedef struct {
+ unsigned char uuid[BLUETOOTH_UUID_HEX_MAX_LEN];
+ int inst_id;
+} bt_gatt_included_service_info_t;
+
+GSList *list_gatt_info;
+
+
+#endif
/* App Information structure */
typedef struct {
int scan_rsp_len;
gboolean is_initialized;
GSList *service_handles;
+#ifdef TIZEN_GATT_CLIENT
+ int client_id; /* GATT Client instance ID */
+ bluetooth_device_address_t address; /* Remote BLE Device Address */
+ gboolean is_watcher_enabled;
+#endif
} bt_service_app_info_t;
/* GATT Server Request Info Structure */
static void __bt_remove_all_service_handles(int instance_id);
+#ifdef TIZEN_GATT_CLIENT
+static void __bt_handle_client_instance_registered(event_gattc_register_t *data);
+static void __bt_handle_client_connected(event_gattc_conn_t *event_data);
+static void __bt_handle_client_disconnected(event_gattc_conn_t *event_data);
+static void __bt_handle_client_service_search_completed(event_gattc_conn_status_t *event_data);
+static void __bt_handle_client_service_search_result(event_gattc_service_result_t *event_data);
+static void __bt_handle_client_characteristic_search_result(
+ event_gattc_characteristic_result_t *event_data);
+static void __bt_handle_client_descriptor_search_result(event_gattc_descriptor_result_t *event_data);
+static void __bt_handle_client_characteristic_read_data(event_gattc_read_data *event_data);
+static void __bt_handle_client_descriptor_read_data(event_gattc_read_data *event_data);
+static void __bt_handle_client_characteristic_write_data(event_gattc_write_data *event_data);
+static void __bt_handle_client_descriptor_write_data(event_gattc_write_data *event_data);
+static int __bt_do_unregister_gatt_instance(int instance_id);
+static void __bt_hanlde_le_device_disconnection(event_dev_conn_status_t *event_data);
+static void __bt_handle_client_notification_registered(event_gattc_regdereg_notify_t *event_data,
+ gboolean is_registered);
+
+#endif
+
static int __bt_do_unregister_server_instance(int server_instance);
/* Linked List of GATT requests from Remote GATT Clients */
}
}
+#ifdef TIZEN_GATT_CLIENT
+char * _bt_gatt_get_default_gatt_client_uuid(void)
+{
+ return g_strdup(DEFAULT_GATT_CLIENT_UUID);
+}
+
+
+static void __bt_register_default_gatt_client()
+{
+ char *uuid_str;
+ oal_uuid_t uuid;
+
+ uuid_str = _bt_gatt_get_default_gatt_client_uuid();
+ _bt_string_to_uuid(uuid_str, (service_uuid_t*)&uuid);
+
+ BT_INFO("Register Default GATT client uuid [%s]", uuid_str);
+
+ if (OAL_STATUS_SUCCESS != gattc_register(&uuid)) /* for only Smart Control */
+ BT_ERR("gattc register failed");
+
+ g_free(uuid_str);
+}
+#endif
+
int _bt_gatt_init(void)
{
BT_DBG("+");
/* Register gatt event handler */
_bt_service_register_event_handler_callback(BT_GATT_MODULE, __bt_gatt_event_handler);
+#ifdef TIZEN_GATT_CLIENT
+ /*In bluedroid product, defacult gatt client is handled differently*/
+ __bt_register_default_gatt_client();
+#endif
BT_DBG("-");
return BLUETOOTH_ERROR_NONE;
}
}
}
+#ifdef TIZEN_GATT_CLIENT
+static int __bt_do_unregister_gatt_instance(int instance_id)
+{
+ int ret = OAL_STATUS_SUCCESS;
+ int k;
+
+ BT_INFO("DeAllocate server or client instance ID [%d]", instance_id);
+
+ /* Reset data: instance_id parameter could be either for GATT Server or for GATT client */
+ for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
+ if (numapps[k].instance_id == instance_id) {
+ BT_INFO("This is a GATT server app, unregister: Slot [%d] vacant", k);
+ numapps[k].is_initialized = FALSE;
+ numapps[k].instance_id = -1;
+ numapps[k].adv_handle = 0;
+ numapps[k].adv_instance = -1;
+ memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
+ memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
+ memset(numapps[k].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
+ memset(numapps[k].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
+ numapps[k].adv_data_len = 0;
+ numapps[k].scan_rsp_len = 0;
+
+ /* Its a GATT Server Instance */
+ ret = gatts_unregister(instance_id);
+ if (ret != OAL_STATUS_SUCCESS) {
+ BT_ERR("DeAllocate server instance with stack Fail ret: %d", ret);
+ return BLUETOOTH_ERROR_INTERNAL;
+ }
+ break;
+ } else if (numapps[k].client_id == instance_id) {
+ BT_INFO("This is a GATT client app, unregister: Slot [%d] vacant", k);
+ numapps[k].client_id = -1;
+ numapps[k].is_initialized = FALSE;
+ memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
+ memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
+ memset(&numapps[k].address.addr, 0x00, sizeof(bluetooth_device_address_t));
+
+ /* Its a GATT Client Instance */
+ ret = gattc_deregister(instance_id);
+ if (ret != OAL_STATUS_SUCCESS) {
+ BT_ERR("DeAllocate GATT Client instance with stack Fail ret: %d", ret);
+ return BLUETOOTH_ERROR_INTERNAL;
+ }
+ break;
+ }
+ }
+ return BLUETOOTH_ERROR_NONE;
+}
+#endif
+
static int __bt_do_unregister_server_instance(int server_instance)
{
int ret = OAL_STATUS_SUCCESS;
#endif
case BT_CONNECT_LE:
case BT_DISCONNECT_LE: {
+#ifdef TIZEN_GATT_CLIENT
+ char *addr = (char*)req_info->user_data;
+ bluetooth_device_address_t address;
+
+ if (!g_strcmp0(addr, (char*)data)) {
+ BT_INFO("GATT Client connect-disconnect call pending for app [%s] addr [%s]",
+ req_info->sender, addr);
+ out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
+ _bt_convert_addr_string_to_type(address.addr, addr);
+
+ g_array_append_vals(out_param, (bluetooth_device_address_t*)&address,
+ sizeof(bluetooth_device_address_t));
+ _bt_service_method_return(req_info->context, out_param, result);
+
+ g_free(req_info->user_data);
+ _bt_free_info_from_invocation_list(req_info);
+ g_array_free(out_param, TRUE);
+ }
+#else
out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
g_array_append_vals(out_param, (bluetooth_device_address_t*)data,
_bt_free_info_from_invocation_list(req_info);
g_array_free(out_param, TRUE);
+#endif
+ break;
+ }
+#ifdef TIZEN_GATT_CLIENT
+ case BT_GATT_CLIENT_REGISTER: {
+ bt_service_app_info_t *param = (bt_service_app_info_t*)data;
+ BT_INFO("Sender [%s] Request Sender [%s]", param->sender, req_info->sender);
+ if (!g_strcmp0(req_info->sender, param->sender)) {
+ BT_DBG("GATT Client app found [%s] created client ID [%d]",
+ req_info->sender, param->client_id);
+
+ out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
+ g_array_append_vals(out_param, ¶m->client_id, sizeof(int));
+ _bt_service_method_return(req_info->context, out_param, result);
+
+ g_free(req_info->user_data);
+ _bt_free_info_from_invocation_list(req_info);
+ g_array_free(out_param, TRUE);
+ }
+ break;
+ }
+ case BT_GATT_GET_PRIMARY_SERVICES: {
+ bt_services_browse_info_t *param = (bt_services_browse_info_t*)data;
+ char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
+ _bt_convert_addr_type_to_string(address,
+ (unsigned char *)(¶m->device_addr.addr));
+
+ BT_INFO("Request Sender [%s] address [%s]", req_info->sender, address);
+
+ /* Match address to determine same request */
+ if (!g_strcmp0((char*)req_info->user_data, address)) {
+ BT_DBG("GATT Client app found [%s] Remote address [%s]",
+ req_info->sender, address);
+
+ out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
+ g_array_append_vals(out_param, param,
+ sizeof(bt_services_browse_info_t));
+
+ //g_array_append_vals(out_param, ¶m->client_id, sizeof(int));
+ _bt_service_method_return(req_info->context, out_param, result);
+
+ g_free(req_info->user_data);
+ _bt_free_info_from_invocation_list(req_info);
+ g_array_free(out_param, TRUE);
+ }
+ g_free(address);
+ break;
+ }
+ case BT_GATT_GET_SERVICE_PROPERTIES: {
+ bt_char_browse_info_t param;
+ memcpy((void*)¶m, data, sizeof(bt_char_browse_info_t));
+ //bt_char_browse_info_t *param = (bt_char_browse_info_t*)data;
+ BT_INFO("Request Sender [%s]", req_info->sender);
+ bluetooth_gatt_client_svc_prop_info_t *prop = (bluetooth_gatt_client_svc_prop_info_t*)req_info->user_data;
+
+ /* Match both address and service properties to determine same request */
+ if (!memcmp(param.device_addr.addr,
+ prop->device_address.addr,
+ sizeof(bluetooth_device_address_t)) &&
+ !memcmp(param.svc_uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
+ param.svc_inst_id == prop->svc.instance_id) {
+ BT_INFO("Returning Service properties");
+
+ out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
+ g_array_append_vals(out_param, ¶m, sizeof(bt_char_browse_info_t));
+ _bt_service_method_return(req_info->context, out_param, result);
+
+ g_free(req_info->user_data);
+ _bt_free_info_from_invocation_list(req_info);
+ g_array_free(out_param, TRUE);
+ }
+ break;
+ }
+ case BT_GATT_GET_CHARACTERISTIC_PROPERTIES: {
+ bt_descriptor_browse_info_t *param = (bt_descriptor_browse_info_t*)data;
+
+ BT_INFO("Request Sender [%s]", req_info->sender);
+ bluetooth_gatt_client_char_prop_info_t *prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
+
+ /* Match both address, service properties &char properties to determine same request */
+ if (!memcmp(param->device_addr.addr,
+ prop->device_address.addr,
+ sizeof(bluetooth_device_address_t)) &&
+ !memcmp(param->svc_uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
+ param->svc_inst_id == prop->svc.instance_id &&
+ !memcmp(param->char_uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
+ param->char_inst_id == prop->characteristic.instance_id) {
+ BT_INFO("Returning Characteristic properties");
+ out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
+ g_array_append_vals(out_param, param, sizeof(bt_descriptor_browse_info_t));
+ _bt_service_method_return(req_info->context, out_param, result);
+
+ g_free(req_info->user_data);
+ _bt_free_info_from_invocation_list(req_info);
+ g_array_free(out_param, TRUE);
+ }
+ break;
+ }
+ case BT_GATT_WATCH_CHARACTERISTIC: {
+ bt_gatt_notif_reg_info_t *param = (bt_gatt_notif_reg_info_t*)data;
+ bluetooth_gatt_client_char_prop_info_t *prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
+ BT_INFO("Request Sender [%s]", req_info->sender);
+ /* Match both address, service properties &char properties to determine same request */
+ if (!memcmp(param->addr.addr,
+ prop->device_address.addr,
+ sizeof(bluetooth_device_address_t)) &&
+ !memcmp(param->svc_uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
+ param->svc_inst == prop->svc.instance_id &&
+ !memcmp(param->char_uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
+ param->char_inst == prop->characteristic.instance_id) {
+ BT_INFO("Characteristic Watch Successful: Is registered [%d]",
+ param->is_registered);
+ out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
+ g_array_append_vals(out_param, param, sizeof(bt_gatt_notif_reg_info_t));
+ _bt_service_method_return(req_info->context, out_param, result);
+ g_free(req_info->user_data);
+ _bt_free_info_from_invocation_list(req_info);
+ g_array_free(out_param, TRUE);
+ }
+ break;
+ }
+ case BT_GATT_READ_CHARACTERISTIC:
+ case BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE: {
+ bluetooth_gatt_client_char_prop_info_t *param = (bluetooth_gatt_client_char_prop_info_t*)data;
+ BT_INFO("Request Sender [%s]", req_info->sender);
+
+ char *addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
+ bluetooth_gatt_client_char_prop_info_t *prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
+ _bt_convert_addr_type_to_string(addr, prop->device_address.addr);
+
+ if (!memcmp(param, prop, sizeof(bluetooth_gatt_client_char_prop_info_t))) {
+ BT_INFO("Gatt Char read or write request matched for address [%s]", addr);
+ out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
+ g_array_append_vals(out_param, param, sizeof(bluetooth_gatt_client_char_prop_info_t));
+ _bt_service_method_return(req_info->context, out_param, result);
+
+ g_free(req_info->user_data);
+ _bt_free_info_from_invocation_list(req_info);
+ g_array_free(out_param, TRUE);
+ }
+ g_free(addr);
+ break;
}
+ case BT_GATT_READ_DESCRIPTOR_VALUE:
+ case BT_GATT_WRITE_DESCRIPTOR_VALUE: {
+ bluetooth_gatt_client_desc_prop_info_t *param = (bluetooth_gatt_client_desc_prop_info_t*)data;
+ BT_INFO("Request Sender [%s]", req_info->sender);
+
+ char *addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
+ bluetooth_gatt_client_desc_prop_info_t *prop = (bluetooth_gatt_client_desc_prop_info_t*)req_info->user_data;
+ _bt_convert_addr_type_to_string(addr, prop->device_address.addr);
+
+ if (!memcmp(param, prop, sizeof(bluetooth_gatt_client_desc_prop_info_t))) {
+ BT_INFO("Descriptor read or write request matched for address [%s]", addr);
+ out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
+ g_array_append_vals(out_param, param, sizeof(bluetooth_gatt_client_desc_prop_info_t));
+ _bt_service_method_return(req_info->context, out_param, result);
+
+ g_free(req_info->user_data);
+ _bt_free_info_from_invocation_list(req_info);
+ g_array_free(out_param, TRUE);
+ }
+ g_free(addr);
+ break;
+ }
+#endif
default:
break;
}
static void __bt_handle_gatt_server_service_added(event_gatts_srvc_prm_t *event)
{
int result = BLUETOOTH_ERROR_NONE;
- int svc_handle;
+ int svc_handle = 0;
int *handle;
int count;
bt_service_app_info_t *info = NULL;
return NULL;
}
-static void __bt_handle_gatt_server_connection_state(event_gatts_conn_t *event)
+#ifdef TIZEN_GATT_CLIENT
+static struct gatt_server_info_t *__bt_find_remote_gatt_server_info(char *address)
{
- int result = BLUETOOTH_ERROR_NONE;
- struct gatt_client_info_t *conn_info = NULL;
- bt_service_app_info_t *info = NULL;
- bluetooth_device_address_t dev_addr;
- GVariant *param = NULL;
- int k;
+ GSList *l;
+ struct gatt_server_info_t *info = NULL;
+ for (l = gatt_server_info_list; l != NULL; l = g_slist_next(l)) {
+ info = (struct gatt_server_info_t*)l->data;
+ if (info == NULL)
+ continue;
- char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
+ if (!g_strcmp0(info->addr, address)) {
+ BT_INFO("Remote GATT Server found addr[%s]", info->addr);
+ return info;
+ }
+ }
+ return NULL;
+}
- memcpy(dev_addr.addr, event->address.addr, 6);
+static struct gatt_out_conn_info_t* __bt_find_gatt_outgoing_conn_info(char *address)
+{
+ GSList *l;
+ struct gatt_out_conn_info_t *info = NULL;
+ for (l = outgoing_gatt_conn_list; l != NULL; l = g_slist_next(l)) {
+ info = (struct gatt_out_conn_info_t*)l->data;
+ if (info == NULL)
+ continue;
+ if (!g_strcmp0(info->addr, address)) {
+ BT_INFO("Outgoing connection info found addr[%s]", info->addr);
+ return info;
+ }
+ }
+ return NULL;
+}
- /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
- for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
- info = &numapps[k];
+static struct gatt_server_info_t *__bt_find_remote_gatt_server_info_from_client_if(int client_if)
+{
+ GSList *l;
+ struct gatt_server_info_t *info = NULL;
- if (info->instance_id == event->server_inst) {
- BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
- __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
- (bluetooth_device_address_t*)&dev_addr, sizeof(bluetooth_device_address_t));
+ for (l = gatt_server_info_list; l != NULL; l = g_slist_next(l)) {
+ info = (struct gatt_server_info_t*)l->data;
+ if (info == NULL)
+ continue;
- break;
+ if (info->client_id == client_if) {
+ BT_INFO("Remote GATT server found addr[%s]", info->addr);
+ return info;
}
}
+ return NULL;
+}
+#endif
- /* Send event to BT-API */
+
+
+
+#ifdef TIZEN_GATT_CLIENT
+static void __bt_handle_gatt_server_connection_state(event_gatts_conn_t *event)
+{
+ int result = BLUETOOTH_ERROR_NONE;
+ struct gatt_client_info_t *conn_info = NULL;
+ bluetooth_device_address_t dev_addr;
+
+ char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
+
+ memcpy(dev_addr.addr, event->address.addr, 6);
+
+ /* REPLY dbus Context to application which called BT_CONNECT_LE. There is status
+ in this event */
_bt_convert_addr_type_to_string(address,
(unsigned char *)dev_addr.addr);
+ __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
+ address, BT_ADDRESS_STRING_SIZE);
BT_INFO("GATT Server Connedted: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
address, event->server_inst, event->conn_id);
conn_info = __bt_find_remote_gatt_client_info(address);
if (!conn_info) {
+ BT_INFO("Conn Info absent: But no need to Send Local GATT Server Connected event to apps");
+#if 0
param = g_variant_new("(is)", result, address);
+
/* Send event to application */
- _bt_send_event(BT_GATT_SERVER_EVENT,
- BLUETOOTH_EVENT_GATT_CONNECTED,
+ _bt_send_event(BT_DEVICE_EVENT,
+ BLUETOOTH_EVENT_GATT_SERVER_CONNECTED, /* Local device is GATT server */
param);
-
+#endif
/* Save Connection info */
conn_info = g_new0(struct gatt_client_info_t, 1);
conn_info->addr = g_strdup(address);
conn_info->connection_id = event->conn_id;
conn_info->instance_id = event->server_inst;
gatt_client_info_list = g_slist_append(gatt_client_info_list, conn_info);
- BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
+ BT_INFO("Total num of connected Remote GATT Clients [%d]", g_slist_length(gatt_client_info_list));
}
g_free(address);
int result = BLUETOOTH_ERROR_NONE;
struct gatt_client_info_t *conn_info = NULL;
bluetooth_device_address_t dev_addr;
- bt_service_app_info_t *info = NULL;
- GVariant *param = NULL;
- int k;
char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
memcpy(dev_addr.addr, event->address.addr, 6);
- /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
- for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
- info = &numapps[k];
+ /* REPLY dbus Context to application which called BT_DISCONNECT_LE. There is status
+ in this event */
+ _bt_convert_addr_type_to_string(address,
+ (unsigned char *)dev_addr.addr);
- if (info->instance_id == event->server_inst) {
- BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
- __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE,
- (bluetooth_device_address_t*)&dev_addr, sizeof(bluetooth_device_address_t));
+ if (NULL == _bt_get_request_info_data(BT_DISCONNECT_LE, address)) {
+ if (NULL != _bt_get_request_info_data(BT_CONNECT_LE, address)) {
+ result = BLUETOOTH_ERROR_INTERNAL;
- break;
+ __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
+ address, BT_ADDRESS_STRING_SIZE);
+
+ BT_ERR("Failed to connect Local GATT Server Remote Client addr[%s]", address);
+ return;
}
+ } else {
+ __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE,
+ address, BT_ADDRESS_STRING_SIZE);
}
- /* Send event to BT-API */
- _bt_convert_addr_type_to_string(address,
- (unsigned char *)dev_addr.addr);
-
- BT_INFO("GATT Server DisConnected: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
+ BT_INFO("Local GATT Server DisConnected: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
address, event->server_inst, event->conn_id);
-
- param = g_variant_new("(is)", result, address);
- /* Send event to application */
- _bt_send_event(BT_GATT_SERVER_EVENT,
- BLUETOOTH_EVENT_GATT_DISCONNECTED,
- param);
+ /* Remove Connection info */
+ conn_info = __bt_find_remote_gatt_client_info(address);
+ if (conn_info) {
+ BT_INFO("No need to Send Local GATT Server Disconnected event to apps, just remove remote client info");
+#if 0
+ param = g_variant_new("(is)", result, address);
+ /* Send event to application */
+ _bt_send_event(BT_DEVICE_EVENT,
+ BLUETOOTH_EVENT_GATT_SERVER_DISCONNECTED, /* Local device is GATT server */
+ param);
+#endif
+ /* Remove info from List */
+ gatt_client_info_list = g_slist_remove(gatt_client_info_list, conn_info);
+ BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
+ g_free(conn_info->addr);
+ g_free(conn_info);
+ }
+
+ g_free(address);
+}
+#else
+
+static void __bt_handle_gatt_server_connection_state(event_gatts_conn_t *event)
+{
+ int result = BLUETOOTH_ERROR_NONE;
+ struct gatt_client_info_t *conn_info = NULL;
+ bt_service_app_info_t *info = NULL;
+ bluetooth_device_address_t dev_addr;
+ GVariant *param = NULL;
+ int k;
+
+ char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
+
+ memcpy(dev_addr.addr, event->address.addr, 6);
+
+
+ /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
+ for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
+ info = &numapps[k];
+
+ if (info->instance_id == event->server_inst) {
+ BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
+ __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
+ (bluetooth_device_address_t*)&dev_addr, sizeof(bluetooth_device_address_t));
+
+ break;
+ }
+ }
+
+ /* Send event to BT-API */
+ _bt_convert_addr_type_to_string(address,
+ (unsigned char *)dev_addr.addr);
+
+ BT_INFO("GATT Server Connedted: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
+ address, event->server_inst, event->conn_id);
+
+
+ /* Check if device is already in connected list */
+ conn_info = __bt_find_remote_gatt_client_info(address);
+
+ if (!conn_info) {
+ param = g_variant_new("(is)", result, address);
+ /* Send event to application */
+ _bt_send_event(BT_GATT_SERVER_EVENT,
+ BLUETOOTH_EVENT_GATT_CONNECTED,
+ param);
+
+ /* Save Connection info */
+ conn_info = g_new0(struct gatt_client_info_t, 1);
+ conn_info->addr = g_strdup(address);
+ BT_INFO("Added GATT client addr[%s]", conn_info->addr);
+ conn_info->connection_id = event->conn_id;
+ conn_info->instance_id = event->server_inst;
+ gatt_client_info_list = g_slist_append(gatt_client_info_list, conn_info);
+ BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
+ }
+
+ g_free(address);
+}
+
+/* GATT Server Dis connected */
+static void __bt_handle_gatt_server_disconnection_state(event_gatts_conn_t *event)
+{
+ int result = BLUETOOTH_ERROR_NONE;
+ struct gatt_client_info_t *conn_info = NULL;
+ bluetooth_device_address_t dev_addr;
+ bt_service_app_info_t *info = NULL;
+ GVariant *param = NULL;
+ int k;
+
+ char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
+
+ memcpy(dev_addr.addr, event->address.addr, 6);
+
+ /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
+ for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
+ info = &numapps[k];
+
+ if (info->instance_id == event->server_inst) {
+ BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
+ __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE,
+ (bluetooth_device_address_t*)&dev_addr, sizeof(bluetooth_device_address_t));
+
+ break;
+ }
+ }
+
+ /* Send event to BT-API */
+ _bt_convert_addr_type_to_string(address,
+ (unsigned char *)dev_addr.addr);
+
+ BT_INFO("GATT Server DisConnected: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
+ address, event->server_inst, event->conn_id);
+
+ param = g_variant_new("(is)", result, address);
+ /* Send event to application */
+ _bt_send_event(BT_GATT_SERVER_EVENT,
+ BLUETOOTH_EVENT_GATT_DISCONNECTED,
+ param);
/* Remove Connection info */
conn_info = __bt_find_remote_gatt_client_info(address);
g_free(address);
}
+#endif
static void __bt_handle_gatt_server_write_requested(event_gatts_srvc_write_attr_t *event)
{
__bt_handle_gatt_mtu_changed_event(mtu_changed);
break;
}
+#ifdef TIZEN_GATT_CLIENT
+ case OAL_EVENT_GATTC_REGISTRATION: {
+ BT_INFO("OAL Event: GATT Client instance Registered");
+ __bt_handle_client_instance_registered((event_gattc_register_t *) event_data);
+ break;
+ }
+ case OAL_EVENT_GATTC_CONNECTION_COMPLETED: {
+ BT_INFO("OAL Event: GATT Client Connected");
+ __bt_handle_client_connected((event_gattc_conn_t *) event_data);
+ break;
+ }
+ case OAL_EVENT_GATTC_DISCONNECTION_COMPLETED: {
+ BT_INFO("OAL Event: GATT Client DisConnected");
+ __bt_handle_client_disconnected((event_gattc_conn_t *) event_data);
+ break;
+ }
+ case OAL_EVENT_GATTC_SERVICE_SEARCH_RESULT: {
+ BT_INFO("OAL Event: GATT Client Service Search Result");
+ __bt_handle_client_service_search_result((event_gattc_service_result_t *) event_data);
+ break;
+ }
+ case OAL_EVENT_GATTC_SERVICE_SEARCH_DONE: {
+ BT_INFO("OAL Event: GATT Client Service Completed");
+ __bt_handle_client_service_search_completed((event_gattc_conn_status_t *) event_data);
+ break;
+ }
+ case OAL_EVENT_GATTC_CHARAC_SERACH_RESULT: {
+ BT_INFO("OAL Event: GATT Client Characteristic Search Result");
+ __bt_handle_client_characteristic_search_result((event_gattc_characteristic_result_t *) event_data);
+ break;
+ }
+ case OAL_EVENT_GATTC_DESC_SERACH_RESULT: {
+ BT_INFO("OAL Event: GATT Client Descriptor Search Result");
+ __bt_handle_client_descriptor_search_result((event_gattc_descriptor_result_t *) event_data);
+ break;
+ }
+ case OAL_EVENT_GATTC_READ_CHARAC: {
+ BT_INFO("OAL Event: GATT Client Characteristic Read Data");
+ __bt_handle_client_characteristic_read_data((event_gattc_read_data *) event_data);
+ break;
+ }
+ case OAL_EVENT_GATTC_READ_DESCR: {
+ BT_INFO("OAL Event: GATT Client Descriptor Read Data");
+ __bt_handle_client_descriptor_read_data((event_gattc_read_data *) event_data);
+ break;
+ }
+ case OAL_EVENT_GATTC_WRITE_CHARAC: {
+ BT_INFO("OAL Event: GATT Client Characteristic Write Data");
+ __bt_handle_client_characteristic_write_data((event_gattc_write_data *) event_data);
+ break;
+ }
+ case OAL_EVENT_GATTC_WRITE_DESCR: {
+ BT_INFO("OAL Event: GATT Client Descriptor Write Data");
+ __bt_handle_client_descriptor_write_data((event_gattc_write_data *) event_data);
+ break;
+ }
+ case OAL_EVENT_DEVICE_LE_DISCONNECTED: {
+ BT_INFO("OAL Event: LE device disconnected");
+ __bt_hanlde_le_device_disconnection((event_dev_conn_status_t *)event_data);
+ break;
+ }
+ case OAL_EVENT_GATTC_NOTIFICATION_REGISTERED: {
+ BT_INFO("OAL Event: GATT Client Notification Registered");
+ __bt_handle_client_notification_registered((event_gattc_regdereg_notify_t *) event_data, TRUE);
+ break;
+ }
+ case OAL_EVENT_GATTC_NOTIFICATION_DEREGISTERED: {
+ BT_INFO("OAL Event: GATT Client Notification Registered");
+ __bt_handle_client_notification_registered((event_gattc_regdereg_notify_t *) event_data, FALSE);
+ break;
+ }
+#endif
default:
break;
}
*mtu = (unsigned int)stack_mtu;
return BLUETOOTH_ERROR_NONE;
}
+
+#ifdef TIZEN_GATT_CLIENT
+static void __bt_convert_byte_ordering(char *data, int data_len,
+ char *converted_data)
+{
+ int i, j;
+ char temp[BLUETOOTH_UUID_HEX_MAX_LEN];
+
+ memcpy(&temp, data, data_len);
+ for (i = 0, j = data_len - 1; i < data_len; i++, j--)
+ converted_data[i] = temp[j];
+}
+
+/* GATT Client utility static functions */
+static bt_gatt_service_info_list_t * __bt_get_service_info_list(int conn_id)
+{
+ GSList *l;
+ bt_gatt_service_info_list_t *info = NULL;
+
+ for (l = list_gatt_info; l != NULL; l = g_slist_next(l)) {
+ info = (bt_gatt_service_info_list_t*)l->data;
+ if (info == NULL)
+ continue;
+
+ if (info->conn_id == conn_id)
+ return info;
+
+ }
+ return NULL;
+}
+
+static bt_gatt_service_info_t* __bt_find_matching_service(
+ bt_gatt_service_info_list_t *svc_list, oal_gatt_srvc_id_t *svc)
+{
+ GSList *l;
+ bt_gatt_service_info_t *info = NULL;
+
+ for (l = svc_list->services; l != NULL; l = g_slist_next(l)) {
+ info = (bt_gatt_service_info_t*)l->data;
+ if (info == NULL)
+ continue;
+
+ /* Match UUID and instance ID */
+ if (!memcmp(&svc->id.uuid.uuid, &info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
+ && (svc->id.inst_id == info->inst_id)) {
+ return info;
+ }
+ }
+ return NULL;
+}
+
+static bt_gatt_char_info_t* __bt_find_matching_charc(
+ bt_gatt_service_info_t *svc_info, oal_gatt_id_t *charc)
+{
+ GSList *l;
+ bt_gatt_char_info_t *info = NULL;
+
+ for (l = svc_info->chars; l != NULL; l = g_slist_next(l)) {
+ info = (bt_gatt_char_info_t*)l->data;
+ if (info == NULL)
+ continue;
+
+ /* Match UUID and instance ID */
+ if (!memcmp(&charc->uuid.uuid, &info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
+ && (charc->inst_id == info->inst_id)) {
+ return info;
+ }
+ }
+ return NULL;
+}
+
+static bt_gatt_descriptor_info_t* __bt_find_matching_desc(
+ bt_gatt_char_info_t *char_info, oal_gatt_id_t *desc)
+{
+ GSList *l;
+ bt_gatt_descriptor_info_t *info = NULL;
+
+ for (l = char_info->descs; l != NULL; l = g_slist_next(l)) {
+ info = (bt_gatt_descriptor_info_t*)l->data;
+ if (info == NULL)
+ continue;
+
+ /* Match UUID and instance ID */
+ if (!memcmp(&desc->uuid, &info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
+ && (desc->inst_id == info->inst_id)) {
+ return info;
+ }
+ }
+ return NULL;
+}
+
+
+static struct gatt_server_info_t *__bt_find_remote_gatt_server_info_from_conn_id(int conn_id)
+{
+ GSList *l;
+ struct gatt_server_info_t *info = NULL;
+
+ for (l = gatt_server_info_list; l != NULL; l = g_slist_next(l)) {
+ info = (struct gatt_server_info_t*)l->data;
+ if (info == NULL)
+ continue;
+
+ if (info->connection_id == conn_id) {
+ BT_INFO("Remote GATT server found addr[%s]", info->addr);
+ return info;
+ }
+ }
+ return NULL;
+}
+
+static bt_gatt_service_info_t* __bt_find_removed_service(bt_gatt_service_info_list_t *svc_list)
+{
+ GSList *l;
+ bt_gatt_service_info_t *info = NULL;
+
+ for (l = svc_list->services; l != NULL; l = g_slist_next(l)) {
+ info = (bt_gatt_service_info_t*)l->data;
+ if (info == NULL)
+ continue;
+
+ /* Service is marked a removed */
+ if (info->is_removed == 1)
+ return info;
+ }
+ return NULL;
+}
+
+static void __bt_remove_service_info_from_list(bt_gatt_service_info_t *svc_info)
+{
+#if 0
+ GSList *l;
+ GSList *l1;
+ GSList *l2;
+ bt_gatt_char_info_t *charc = NULL;
+ bt_gatt_included_service_info_t *incl = NULL;
+ bt_gatt_descriptor_info_t *desc = NULL;
+
+ /* Remove all Characteristic and Descriptors within characteristic */
+ for (l = svc_info->chars; l != NULL;) {
+ charc = (bt_gatt_char_info_t*)l->data;
+ l = g_slist_next(l); /* Incase if l is removed, saving next to l */
+
+ if (charc == NULL)
+ continue;
+
+ /* Inside Characteristic */
+ for (l1 = charc->descs; l1 != NULL;) {
+
+ desc = (bt_gatt_descriptor_info_t*)l1->data;
+ l1 = g_slist_next(l1);
+
+ if (desc == NULL)
+ continue;
+
+ /* Remove Descriptor */
+ charc->descs = g_slist_remove(charc->descs, desc);
+ g_free(desc);
+ }
+ /* Remove Characteristic */
+ svc_info->chars = g_slist_remove(svc_info->chars, charc);
+ g_free(charc);
+ }
+
+ /* Remove all Included Services */
+ for (l2 = svc_info->included_svcs; l2 != NULL;) {
+ incl = (bt_gatt_included_service_info_t*)l2->data;
+ l2 = g_slist_next(l2); /* Incase if l is removed, saving next to l */
+
+ if (incl == NULL)
+ continue;
+
+ /* Remove included service */
+ svc_info->included_svcs = g_slist_remove(svc_info->included_svcs, incl);
+ g_free(incl);
+ }
+#endif
+}
+
+
+static void __bt_build_service_browse_info(int conn_id,
+ bt_services_browse_info_t* info)
+{
+ GSList *l;
+ bt_gatt_service_info_list_t *svc_info_list;
+ bt_gatt_service_info_t *svc_info;
+
+ service_uuid_t uuid;
+ struct gatt_server_info_t *conn_info = NULL;
+ int count = 0;
+ char uuid_string[BLUETOOTH_UUID_STRING_MAX];
+ BT_INFO("+");
+
+ conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
+
+ if (!conn_info)
+ return;
+
+ _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
+
+ svc_info_list = __bt_get_service_info_list(conn_id);
+
+ if (!svc_info_list)
+ return;
+
+ info->count = g_slist_length(svc_info_list->services);
+ BT_INFO("Total services present in the svc info list for this conn id [%d] is [%d]",
+ conn_id, info->count);
+
+ for (l = svc_info_list->services; l != NULL; l = g_slist_next(l)) {
+ svc_info = (bt_gatt_service_info_t*)l->data;
+ if (svc_info == NULL)
+ continue;
+
+ memcpy(&uuid.uuid, &svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
+ _bt_uuid_to_string(&uuid, uuid_string);
+
+ BT_INFO("Service UUID formed [%s] strlen [%d]", uuid_string, strlen(uuid_string));
+
+ /* Fill UUID of service */
+ g_strlcpy(info->uuids[count], uuid_string,
+ BLUETOOTH_UUID_STRING_MAX);
+
+ BT_INFO("Service UUID formed TO be sent [%s] strlen [%d]",
+ info->uuids[count], strlen(info->uuids[count]));
+ /* Fill instance ID of service */
+ info->inst_id[count] = svc_info->inst_id;
+
+ /* Fill primary service or not info */
+ info->primary[count] = svc_info->is_primary;
+
+ /* Increment count of services browsed */
+ count++;
+ }
+
+ BT_INFO("Total services browsed [%d]", count);
+}
+
+static void __bt_build_char_browse_info(int conn_id,
+ bt_gatt_service_info_t *svc_info,
+ bt_char_browse_info_t* info)
+{
+ GSList *l;
+ bt_gatt_char_info_t *char_info;
+ service_uuid_t uuid;
+
+ struct gatt_server_info_t *conn_info = NULL;
+ int count = 0;
+ char uuid_string[BLUETOOTH_UUID_STRING_MAX];
+
+ BT_INFO("+");
+
+ conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
+ if (!conn_info)
+ return;
+
+ /* Fill default data, this will be required even in case of failure */
+ _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
+ memcpy(&info->svc_uuid, svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
+ info->svc_inst_id = svc_info->inst_id;
+
+ if (!svc_info->chars) {
+ BT_ERR("No Chars browsed for address [%s]", conn_info->addr);
+ return;
+ }
+
+ info->count = g_slist_length(svc_info->chars);
+ BT_INFO("Total count of Characteristics [%d]", info->count);
+
+ for (l = svc_info->chars; l != NULL; l = g_slist_next(l)) {
+ char_info = (bt_gatt_char_info_t*)l->data;
+ if (char_info == NULL)
+ continue;
+
+ memcpy(&uuid.uuid, &char_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
+ _bt_uuid_to_string(&uuid, uuid_string);
+
+ /* Fill UUID of characteristic */
+ g_strlcpy(info->uuids[count], uuid_string,
+ BLUETOOTH_UUID_STRING_MAX);
+
+ /* Fill instance ID of characteristic */
+ info->inst_id[count] = char_info->inst_id;
+
+ /* Fill property of characteristic */
+ info->props[count] = char_info->props;
+
+ /* Increment count of services browsed */
+ count++;
+ }
+ BT_INFO("Total characteristics browsed [%d]", count);
+}
+
+static void __bt_build_descriptor_browse_info(int conn_id,
+ bt_gatt_service_info_t *svc_info,
+ bt_gatt_char_info_t *char_info,
+ bt_descriptor_browse_info_t* info)
+{
+ GSList *l;
+ bt_gatt_descriptor_info_t *desc_info;
+
+ service_uuid_t uuid;
+ struct gatt_server_info_t *conn_info = NULL;
+ int count = 0;
+ char uuid_string[BLUETOOTH_UUID_STRING_MAX];
+
+ conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
+
+ /* Fill default data, this will be required even in case of failure */
+ _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
+ memcpy(&info->svc_uuid, svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
+ info->svc_inst_id = svc_info->inst_id;
+ memcpy(&info->char_uuid, char_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
+ info->char_inst_id = char_info->inst_id;
+
+ if (!char_info->descs) {
+ BT_ERR("No Descriptors browsed for address [%s]", conn_info->addr);
+ return;
+ }
+
+ info->count = g_slist_length(char_info->descs);
+ BT_INFO("Total count of Descriptors [%d]", info->count);
+
+ for (l = char_info->descs; l != NULL; l = g_slist_next(l)) {
+ desc_info = (bt_gatt_descriptor_info_t*)l->data;
+ if (desc_info == NULL)
+ continue;
+
+ memcpy(&uuid.uuid, &desc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
+ _bt_uuid_to_string(&uuid, uuid_string);
+
+ /* Fill UUID of Descriptor */
+ g_strlcpy(info->uuids[count], uuid_string,
+ BLUETOOTH_UUID_STRING_MAX);
+
+ /* Fill instance ID of Descriptor */
+ info->inst_id[count] = desc_info->inst_id;
+
+ /* Fill property of the parent characteristic of this descriptor */
+ info->char_props_map = char_info->props;
+
+ /* Increment count of Descriptor browsed */
+ count++;
+ }
+
+ BT_INFO("Total descriptors browsed [%d]", count);
+}
+
+
+static void __bt_cleanup_remote_services(struct gatt_server_info_t *conn_info)
+{
+ GSList *l;
+ GSList *ll;
+ GSList *lll;
+ GSList *llll;
+
+ bt_gatt_service_info_list_t * svc_info_list = NULL;
+ bt_gatt_service_info_t *svc = NULL;
+ bt_gatt_char_info_t *chr = NULL;
+ bt_gatt_descriptor_info_t *desc = NULL;
+ bt_gatt_included_service_info_t *incl_svc = NULL;
+
+ BT_INFO("Start Cleanup of all services");
+
+ svc_info_list = __bt_get_service_info_list(conn_info->connection_id);
+ if (!svc_info_list) {
+ BT_INFO("Could not find Svc Info list for the connection ID [%d]",
+ conn_info->connection_id);
+ return;
+ }
+
+ BT_INFO("Num Services [%d]", g_slist_length(svc_info_list->services));
+ for (l = svc_info_list->services; l;) {
+ svc = (bt_gatt_service_info_t*)l->data;
+ l = g_slist_next(l);
+ if (svc == NULL)
+ continue;
+
+ BT_INFO("Service info Is Prim[%d] Inst ID [%d]",
+ svc->is_primary, svc->inst_id);
+ BT_INFO("Num chars [%d]", g_slist_length(svc->chars));
+ /* Delete all chars and its descriptors */
+ for (ll = svc->chars; ll;) {
+ chr = (bt_gatt_char_info_t*)ll->data;
+ ll = g_slist_next(ll);
+ if (chr == NULL)
+ continue;
+
+ BT_INFO("Num descs [%d]", g_slist_length(chr->descs));
+ for (lll = chr->descs; lll;) {
+ desc = (bt_gatt_descriptor_info_t *)lll->data;
+ lll = g_slist_next(lll);
+ if (desc == NULL)
+ continue;
+ chr->descs = g_slist_remove(chr->descs, desc);
+ g_free(desc);
+ }
+ svc->chars = g_slist_remove(svc->chars, chr);
+ g_free(chr);
+ }
+
+ BT_INFO("Num incl svcs [%d]", g_slist_length(svc->included_svcs));
+ /* Delete all included services */
+ for (llll = svc->included_svcs; llll;) {
+ incl_svc = (bt_gatt_included_service_info_t*)llll->data;
+ llll = g_slist_next(llll);
+ if (incl_svc == NULL)
+ continue;
+
+ svc->included_svcs = g_slist_remove(svc->included_svcs, incl_svc);
+ g_free(incl_svc);
+ }
+ svc_info_list->services = g_slist_remove(svc_info_list->services, svc);
+ g_free(svc);
+ }
+ g_free(svc_info_list);
+ BT_INFO("Cleanup of all services done");
+}
+
+int _bt_register_gatt_client_instance(const char *sender,
+ bluetooth_device_address_t *address)
+{
+ int ret = OAL_STATUS_SUCCESS;
+ char *uuid_string = NULL;
+ int slot = -1;
+ int k;
+ oal_uuid_t uuid;
+
+ /* App should ensure that it should not send */
+ BT_INFO("###Check on which instance GATT Client instance can be initialized....");
+ for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
+ if (numapps[k].is_initialized == 1) {
+ BT_INFO("Instance ID [%d] is already in use..Check next slot",
+ numapps[k].instance_id);
+ } else {
+ slot = k;
+ BT_INFO("Time to register GATT client instancer..UUID to be used is [%s] slot [%d]",
+ uuid_list[slot-1], slot);
+ break;
+ }
+ }
+
+ if (slot == -1) {
+ BT_ERR("No Slot if free for GATT Client registration..");
+ return BLUETOOTH_ERROR_REGISTRATION_FAILED;
+ }
+
+ uuid_string = g_malloc0(BT_UUID_STRING_MAX);
+ _bt_string_to_uuid(uuid_list[slot-1], (service_uuid_t*)&uuid);
+ g_strlcpy(uuid_string, uuid_list[slot-1], BT_UUID_STRING_MAX);
+ BT_INFO("Copied UUID string [%s]", uuid_string);
+
+ /* Register GATT Client */
+ ret = gattc_register(&uuid);
+ if (ret != OAL_STATUS_SUCCESS) {
+ BT_ERR("ret: %d", ret);
+ g_free(uuid_string);
+ return BLUETOOTH_ERROR_INTERNAL;
+ }
+
+ BT_INFO("GATT Client registration call successfully accepted by OAL..wait for Instance Initialized event from OAL..");
+
+ /* Return & wait for GATT Client Instance Initialization event */
+ memset(numapps[slot].sender, 0x00, sizeof(numapps[slot].sender));
+ memset(numapps[slot].uuid, 0x00, sizeof(numapps[slot].uuid));
+
+ g_strlcpy(numapps[slot].sender, sender, sizeof(numapps[slot].sender));
+ g_strlcpy(numapps[slot].uuid, uuid_string, sizeof(numapps[slot].uuid));
+
+ /* Address is saved here. When event comes, sender + address are matched for replying pending
+ request. It is impossible for same sender to have requests with two same addresses */
+ memcpy(&numapps[slot].address.addr, address->addr, sizeof(bluetooth_device_address_t));
+
+ BT_INFO("Slot [%d] occupied", slot);
+ numapps[slot].is_initialized = TRUE; /* Set initialization to true here itself */
+
+ g_free(uuid_string);
+ return BLUETOOTH_ERROR_NONE;
+
+}
+
+
+
+/* GATT client events */
+static void __bt_handle_client_instance_registered(event_gattc_register_t *data)
+{
+ bt_service_app_info_t *info = NULL;
+ int k;
+ char *uuid_string = g_malloc0(BT_UUID_STRING_MAX);
+
+ _bt_uuid_to_string(&(data->client_uuid), uuid_string);
+ BT_INFO("CLient ID is Initialized [%d] UUID initialized [%s]", data->client_if, uuid_string);
+
+ /* Platform GATT client framwork does not use Default GATT client instance
+ This GATT client instance is never deregistred in the lifetime of bt-service */
+ BT_INFO("Default UUID [%s] current registered uuid [%s]",
+ DEFAULT_GATT_CLIENT_UUID, uuid_string);
+ if (g_strcmp0(uuid_string, DEFAULT_GATT_CLIENT_UUID) == 0) {
+ BT_INFO("Default client Instance Registered [%s] Client instance [%d]",
+ uuid_string, data->client_if);
+ gatt_default_client = data->client_if;
+ g_free(uuid_string);
+ return;
+ }
+
+ for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
+ info = &numapps[k];
+
+ if (g_strcmp0(info->uuid, uuid_string) == 0) {
+ BT_INFO("Found GATT client.. UUID [%s], sender [%s]", info->uuid, info->sender);
+ BT_INFO("Slot [%d] occupied", k);
+ info->is_initialized = TRUE;
+ info->client_id = data->client_if;
+ __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE, BT_GATT_CLIENT_REGISTER,
+ (void*)info, sizeof(bt_service_app_info_t));
+ break;
+ }
+ }
+ g_free(uuid_string);
+}
+
+static void __bt_handle_client_connected(event_gattc_conn_t *event_data)
+{
+ int result = BLUETOOTH_ERROR_NONE;
+ struct gatt_server_info_t *conn_info = NULL;
+ struct gatt_out_conn_info_t *out_conn_info = NULL;
+
+ GVariant *param = NULL;
+
+ char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
+ _bt_convert_addr_type_to_string(address,
+ (unsigned char *)event_data->address.addr);
+
+ if (event_data->status != OAL_STATUS_SUCCESS)
+ result = BLUETOOTH_ERROR_INTERNAL;
+
+ /* DBUS Return fo BT_CONNECT_LE for all the apps */
+ __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE, address,
+ BT_ADDRESS_STRING_SIZE);
+
+ BT_INFO("Local GATT Client Connected: Remote addr[%s] Client Interface [%d] Connection ID [%d] status[%d]",
+ address, event_data->client_if, event_data->conn_id, event_data->status);
+
+ if (result == BLUETOOTH_ERROR_NONE) {
+ /* Check if device is already in connected list */
+ conn_info = __bt_find_remote_gatt_server_info(address);
+
+ if (!conn_info) {
+ /* Send event to BT-API */
+ param = g_variant_new("(is)", result, address);
+ _bt_send_event(BT_DEVICE_EVENT,
+ BLUETOOTH_EVENT_GATT_CLIENT_CONNECTED, /* Local device is GATT client */
+ param);
+
+ /* Save Connection info */
+ conn_info = g_new0(struct gatt_server_info_t, 1);
+ conn_info->addr = g_strdup(address);
+ conn_info->client_id = event_data->client_if;
+ BT_INFO("Added GATT server addr[%s]", conn_info->addr);
+ conn_info->connection_id = event_data->conn_id;
+ gatt_server_info_list = g_slist_append(gatt_server_info_list, conn_info);
+ BT_INFO("Total num of connected Remote GATT server devices [%d]",
+ g_slist_length(gatt_server_info_list));
+
+#if 0
+ BT_INFO("Do a Internal refresh");
+ if (OAL_STATUS_SUCCESS != gattc_refresh(conn_info->client_id, &event_data->address))
+ BT_ERR("GATT database refresh failed!!");
+ else
+ BT_INFO("GATT database refresh Success!!");
+#endif
+ } else
+ BT_ERR("Local GATT Client connected event for addr[%s], but device is in connected list already", address);
+ } else
+ BT_ERR("GATT Client Connection failed!!");
+
+ /* If outgoing connection Info is present, then remove it */
+ out_conn_info = __bt_find_gatt_outgoing_conn_info(address);
+ if (out_conn_info) {
+ BT_ERR("Outgoing Client connect request was sent");
+ outgoing_gatt_conn_list = g_slist_remove(outgoing_gatt_conn_list, out_conn_info);
+ g_free(out_conn_info->addr);
+ g_free(out_conn_info);
+ }
+ g_free(address);
+}
+
+static void __bt_handle_client_disconnected(event_gattc_conn_t *event_data)
+{
+ int result = BLUETOOTH_ERROR_NONE;
+
+ struct gatt_server_info_t *conn_info = NULL;
+ struct gatt_out_conn_info_t *out_conn_info = NULL;
+
+ GVariant *param = NULL;
+
+ char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
+ _bt_convert_addr_type_to_string(address,
+ (unsigned char *)event_data->address.addr);
+
+ if (event_data->status != OAL_STATUS_SUCCESS)
+ result = BLUETOOTH_ERROR_INTERNAL;
+
+ if (NULL == _bt_get_request_info_data(BT_DISCONNECT_LE, address)) {
+ if (NULL != _bt_get_request_info_data(BT_CONNECT_LE, address)) {
+ result = BLUETOOTH_ERROR_INTERNAL;
+ __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
+ address, BT_ADDRESS_STRING_SIZE);
+ BT_ERR("Failed to connect Local GATT Remote addr[%s]", address);
+ return;
+ }
+ } else {
+ /* DBUS Return for BT_DISCONNECT_LE for all the apps */
+ __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE, address,
+ BT_ADDRESS_STRING_SIZE);
+ }
+ BT_INFO("Local GATT Client DisConnected: Remote addr[%s] Client Interface [%d] Connection ID [%d] status [%d]",
+ address, event_data->client_if, event_data->conn_id, event_data->status);
+
+ /* Remove Connection info */
+ conn_info = __bt_find_remote_gatt_server_info(address);
+
+ if (conn_info) {
+ param = g_variant_new("(is)", result, address);
+ /* Send event to application */
+ _bt_send_event(BT_DEVICE_EVENT,
+ BLUETOOTH_EVENT_GATT_CLIENT_DISCONNECTED,
+ param);
+
+ BT_INFO("Remove GATT server info from List..");
+ /* Remove all services from info list_gatt_info */
+ __bt_cleanup_remote_services(conn_info);
+
+ /* Remove info from List */
+ gatt_server_info_list = g_slist_remove(gatt_server_info_list, conn_info);
+ BT_INFO("Total num of connected GATT servers [%d]", g_slist_length(gatt_server_info_list));
+ g_free(conn_info->addr);
+ g_free(conn_info);
+ } else
+ BT_INFO("Can not find conn info, already removed!");
+
+ /* If outgoing connection Info is present, then remove it */
+ out_conn_info = __bt_find_gatt_outgoing_conn_info(address);
+ if (out_conn_info) {
+ BT_ERR("Client Disconnected event, but outgoing connect request was sent");
+ outgoing_gatt_conn_list = g_slist_remove(outgoing_gatt_conn_list, out_conn_info);
+ g_free(out_conn_info->addr);
+ g_free(out_conn_info);
+ }
+ g_free(address);
+}
+
+
+static void __bt_handle_client_service_search_result(
+ event_gattc_service_result_t *event_data)
+{
+ BT_INFO("+");
+ BT_INFO("received the gatt service search result");
+ /* Pre: status is never fail from OAL */
+
+ /* Find service list from address */
+ bt_gatt_service_info_list_t *svc_info_list;
+ bt_gatt_service_info_t *svc_info;
+ BT_INFO("Search Result: status [%d] conn_id [%d]",
+ event_data->conn_status.status,
+ event_data->conn_status.conn_id);
+
+ svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
+ if (!svc_info_list) {
+ BT_INFO("Service info list not present for this connection ID, means first time browse");
+ /* Means for this conn_id, no services are ever browsed, first time,
+ create service info list for this conn_id */
+ svc_info_list = g_malloc0(sizeof(bt_gatt_service_info_list_t));
+ svc_info_list->conn_id = event_data->conn_status.conn_id;
+ list_gatt_info = g_slist_append(list_gatt_info, svc_info_list);
+ } else {
+ BT_INFO("Service info list Already present for this connection ID, means not first time browse for this conn ID ");
+ }
+
+ /* send list and current service's uuid and instance id to find it */
+ svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
+
+
+ /* If not found, check if service changed, if yes, means this is a new service added
+ in remote GATT device, update uuid info in svc info list structure, to be used when
+ search is completed */
+ if (!svc_info) {
+ BT_INFO("Service Not found in svc info list for this connection ID");
+ if (svc_info_list->info.is_changed) {
+ BT_INFO("Service Changed indication already found for this connection ID");
+ memcpy(svc_info_list->info.uuid, event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
+ }
+ /* Create and add new service in service list */
+ svc_info = g_malloc0(sizeof(bt_gatt_service_info_t));
+ memcpy(svc_info->uuid, event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
+ svc_info->inst_id = event_data->srvc_id.id.inst_id;
+ svc_info->is_primary = event_data->srvc_id.is_prmry;
+ svc_info_list->services = g_slist_append(svc_info_list->services, svc_info);
+ BT_INFO("Service created and added in Svc info listf or this connection ID");
+ } else {
+ BT_INFO("Service Already found to be present inside the svc info list for this connection ID");
+ /* If returned matching service info, then just update service_rmeoved value inside it to 0 */
+ svc_info->is_removed = 0;
+ }
+}
+
+static void __bt_handle_client_service_search_completed(
+ event_gattc_conn_status_t *event_data)
+{
+ BT_INFO("+");
+ struct gatt_server_info_t *conn_info = NULL;
+ bt_gatt_service_info_list_t *svc_info_list;
+ bt_gatt_service_info_t *svc_info;
+ bt_services_browse_info_t browse_info;
+ unsigned char uuid_empty[BLUETOOTH_UUID_HEX_MAX_LEN];
+
+ memset(&uuid_empty, 0x00, BLUETOOTH_UUID_HEX_MAX_LEN);
+ memset(&browse_info, 0x00, sizeof(bt_services_browse_info_t));
+ BT_INFO("Primary Services browsing completed status[%d] conn ID [%d]",
+ event_data->status, event_data->conn_id);
+
+ conn_info = __bt_find_remote_gatt_server_info_from_conn_id(event_data->conn_id);
+
+ svc_info_list = __bt_get_service_info_list(event_data->conn_id);
+ if (!svc_info_list) {
+ BT_ERR("No services browsed ever for addr [%s]", conn_info->addr);
+
+ /* Just build response and return ERROR */
+ __bt_build_service_browse_info(event_data->conn_id, &browse_info);
+
+ __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_INTERNAL,
+ BT_GATT_GET_PRIMARY_SERVICES, &browse_info,
+ sizeof(bt_services_browse_info_t));
+ return;
+ }
+
+ /* If fail, then send event with error */
+ if (event_data->status != OAL_STATUS_SUCCESS) {
+ /* Just build response and return ERROR */
+ __bt_build_service_browse_info(event_data->conn_id, &browse_info);
+
+ __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_INTERNAL,
+ BT_GATT_GET_PRIMARY_SERVICES, &browse_info,
+ sizeof(bt_services_browse_info_t));
+ return;
+ }
+
+ /* If success, then find service info list from address */
+
+ /* If svc_changed == 1 and uuid valid, means a new service is added*/
+ if (svc_info_list->info.is_changed && !memcmp(uuid_empty, svc_info_list->info.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)) {
+ /* TODO: Send event -Service added with instance ID and UUID of newly added service */
+ BT_INFO("new service added");
+
+ BT_INFO("TODO new service added");
+ }
+
+ /* If svc_changed == 1 and uuid invalid, then a service is removed */
+ if (svc_info_list->info.is_changed && memcmp(uuid_empty, svc_info_list->info.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)) {
+ /* Scan through the service info list to find service with is_removed = 1*/
+ svc_info = __bt_find_removed_service(svc_info_list);
+
+ /* TODO Send event - Service removed with instance ID and UUID of just rmeoved service */
+
+ /* Remove that service info from service info list */
+ svc_info_list->services = g_slist_remove(svc_info_list->services, svc_info);
+
+ /* Delete that service completely from svc_info list*/
+ __bt_remove_service_info_from_list(svc_info);
+ g_free(svc_info);
+ }
+
+ /* Reset svc_changed = 0, and reset UUID = all 0's */
+ svc_info_list->info.is_changed = 0;
+ memset(&svc_info_list->info.uuid, 0x00, BLUETOOTH_UUID_HEX_MAX_LEN);
+
+ /* Build Reply and send to service browse primary services request of pending apps */
+ __bt_build_service_browse_info(event_data->conn_id, &browse_info);
+
+ __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE,
+ BT_GATT_GET_PRIMARY_SERVICES, &browse_info,
+ sizeof(bt_services_browse_info_t));
+}
+
+
+static void __bt_handle_client_characteristic_search_result(
+ event_gattc_characteristic_result_t *event_data)
+{
+ bt_gatt_service_info_list_t *svc_info_list;
+ bt_gatt_service_info_t *svc_info;
+ bt_gatt_char_info_t *char_info;
+ bt_char_browse_info_t browse_info;
+
+ BT_INFO("Characteristic search result status [%d]",
+ event_data->conn_status.status);
+
+ memset(&browse_info, 0x00, sizeof(bt_char_browse_info_t));
+
+ /* If success */
+ if (event_data->conn_status.status == OAL_STATUS_SUCCESS) {
+ /* Find service info list from address */
+ svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
+ if (svc_info_list == NULL) {
+ BT_ERR("svc_info_list is NULL");
+ return;
+ }
+
+
+ /* Find matching service info from svc info list */
+ svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
+ if (svc_info == NULL) {
+ BT_ERR("svc_info is NULL");
+ return;
+ }
+
+ /* Find Matching char from service info in event */
+ char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
+ if (char_info == NULL)
+ BT_ERR("char_info is NULL");
+
+ /* If not found, then add new characteristic and return */
+ if (!char_info) {
+ BT_INFO(" add new characteristic");
+ char_info = g_malloc0(sizeof(bt_gatt_char_info_t));
+ memcpy(char_info->uuid, event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
+
+ char_info->inst_id = event_data->char_id.inst_id;
+ char_info->props = event_data->char_prop;
+ svc_info->chars = g_slist_append(svc_info->chars, char_info);
+ } else {
+ /* If found, then return */
+ BT_INFO("Characteristic browsed is already presesnt");
+ }
+ } else {
+ /* If Not success: Means Charc browse is completed */
+ /* Find char list from service in event */
+ /* Find service list from address */
+ svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
+
+ /* Find service info from service in event */
+ svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
+
+ /* Build char list from service in event */
+ __bt_build_char_browse_info(event_data->conn_status.conn_id,
+ svc_info, &browse_info);
+
+ /* Create response and return by sending event*/
+ /* Build Reply and send to service browse All Included services request of pending apps */
+ __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE,
+ BT_GATT_GET_SERVICE_PROPERTIES,
+ &browse_info,
+ sizeof(bt_char_browse_info_t));
+ }
+}
+
+static void __bt_handle_client_descriptor_search_result(
+ event_gattc_descriptor_result_t *event_data)
+{
+ bt_gatt_service_info_list_t *svc_info_list;
+ bt_gatt_service_info_t *svc_info;
+ bt_gatt_char_info_t *char_info;
+ bt_gatt_descriptor_info_t *desc_info;
+ bt_descriptor_browse_info_t browse_info;
+
+ BT_INFO("descriptor search result status [%d]", event_data->conn_status.status);
+
+ memset(&browse_info, 0x00, sizeof(bt_descriptor_browse_info_t));
+
+ /* If success */
+ if (event_data->conn_status.status == OAL_STATUS_SUCCESS) {
+ /* Find service list from address */
+ svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
+ svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
+ char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
+ desc_info = __bt_find_matching_desc(char_info, &event_data->descr_id);
+
+ /* If not found, add new descriptor and return */
+ if (!desc_info) {
+ desc_info = g_malloc0(sizeof(bt_gatt_descriptor_info_t));
+ memcpy(desc_info->uuid, event_data->descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
+
+ desc_info->inst_id = event_data->descr_id.inst_id;
+ char_info->descs = g_slist_append(char_info->descs, desc_info);
+
+ } else {
+ /* If found, then return */
+ BT_INFO("Characteristic browsed is already presesnt");
+ }
+ } else {
+ /* If Not success */
+ /* Find service list from address */
+ /* Find included service list from service in event */
+ /* Create response and return by sending event*/
+ svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
+
+ /* Find service info from service in event */
+ svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
+
+ /* Find char info from char in event */
+ char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
+
+ /* Build descriptor list from char in event */
+ __bt_build_descriptor_browse_info(event_data->conn_status.conn_id,
+ svc_info, char_info, &browse_info);
+
+ /* DBUS returni */
+ __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE,
+ BT_GATT_GET_CHARACTERISTIC_PROPERTIES,
+ &browse_info,
+ sizeof(bt_descriptor_browse_info_t));
+ }
+}
+
+static void __bt_handle_client_characteristic_read_data(
+ event_gattc_read_data *event_data)
+{
+ int result = BLUETOOTH_ERROR_NONE;
+ struct gatt_server_info_t *conn_info = NULL;
+ bluetooth_gatt_client_char_prop_info_t read_info;
+
+ /* Read Information data structures */
+ GVariant *param = NULL;
+ GVariant *data = NULL;
+ GVariant *data_svc_uuid = NULL;
+ GVariant *data_char_uuid = NULL;
+ char *read_val = NULL;
+ char *svc_uuid = NULL;
+ char *char_uuid = NULL;
+ int i;
+ int uuid_len = 16;
+
+ BT_INFO("+");
+
+ //memset(&read_info, 0x00, sizeof(bt_gatt_handle_property_t));
+ memset(&read_info, 0x00, sizeof(bluetooth_gatt_client_char_prop_info_t));
+
+ /* Extract Address from conn_id of event data */
+ conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
+ event_data->uuid_status.conn_status.conn_id);
+
+ BT_INFO("Characteristic Read result from addr [%s] status [%d]",
+ conn_info->addr, event_data->uuid_status.conn_status.status);
+
+ /* Fill char in buffer */
+ memcpy(&read_info.characteristic.uuid,
+ event_data->uuid_status.char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
+ read_info.characteristic.instance_id = event_data->uuid_status.char_id.inst_id;
+
+ /* Fill Service in buffer */
+ memcpy(&read_info.svc.uuid,
+ event_data->uuid_status.srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
+ read_info.svc.instance_id = event_data->uuid_status.srvc_id.id.inst_id;
+
+ /* Fill remote device address */
+ _bt_convert_addr_string_to_type(read_info.device_address.addr, conn_info->addr);
+
+ /* Fill data and reply to all apps waiting for Read result on the same characteristic
+Note: Even in case of failure, address, handles and result code should be returned */
+ if (event_data->uuid_status.conn_status.status != OAL_STATUS_SUCCESS)
+ result = BLUETOOTH_ERROR_INTERNAL;
+ else {
+ BT_ERR("read data len is [%d]", event_data->data_len);
+ if (event_data->data_len > 0) {
+ /* DEBUG */
+ for (i = 0; i < event_data->data_len; i++)
+ BT_INFO("Data[%d] = [0x%x]", i, event_data->data[i]);
+
+ /* Read data */
+ read_val = g_memdup(&event_data->data[0], event_data->data_len);
+
+ data = g_variant_new_from_data(
+ G_VARIANT_TYPE_BYTESTRING,
+ read_val,
+ event_data->data_len,
+ TRUE, NULL, NULL);
+ } else {
+ BT_ERR("Characteristic Read success, but no data!!!");
+
+ data = g_variant_new_from_data(
+ G_VARIANT_TYPE_BYTESTRING,
+ NULL,
+ 0,
+ FALSE, NULL, NULL);
+ }
+
+ /* SVC uuid */
+ svc_uuid = g_memdup(&event_data->uuid_status.srvc_id.id.uuid.uuid[0], uuid_len);
+
+ data_svc_uuid = g_variant_new_from_data(
+ G_VARIANT_TYPE_BYTESTRING,
+ svc_uuid,
+ 16,
+ TRUE, NULL, NULL);
+
+ /* Char uuid */
+ char_uuid = g_memdup(&event_data->uuid_status.char_id.uuid.uuid[0], uuid_len);
+
+ data_char_uuid = g_variant_new_from_data(
+ G_VARIANT_TYPE_BYTESTRING,
+ char_uuid,
+ 16,
+ TRUE, NULL, NULL);
+
+ param = g_variant_new("(isn@ayin@ayin@ay)", result,
+ conn_info->addr,
+ 16,
+ data_svc_uuid,
+ event_data->uuid_status.srvc_id.id.inst_id,
+ 16,
+ data_char_uuid,
+ event_data->uuid_status.char_id.inst_id,
+ event_data->data_len,
+ data);
+
+ /* Send Event */
+ _bt_send_event(BT_GATT_CLIENT_EVENT,
+ BLUETOOTH_EVENT_GATT_READ_CHAR,
+ param);
+ }
+ /* Send DBUS return */
+ __bt_gatt_handle_pending_request_info(result,
+ BT_GATT_READ_CHARACTERISTIC,
+ &read_info,
+ sizeof(bluetooth_gatt_client_char_prop_info_t));
+
+ if (read_val)
+ g_free(read_val);
+ if (svc_uuid)
+ g_free(svc_uuid);
+ if (char_uuid)
+ g_free(char_uuid);
+}
+
+/* Modified */
+static void __bt_handle_client_descriptor_read_data(
+ event_gattc_read_data *event_data)
+{
+ int result = BLUETOOTH_ERROR_NONE;
+ struct gatt_server_info_t *conn_info = NULL;
+ bluetooth_gatt_client_desc_prop_info_t read_info;
+
+ /* Read Information data structures */
+ GVariant *param = NULL;
+ GVariant *data = NULL;
+ GVariant *data_svc_uuid = NULL;
+ GVariant *data_char_uuid = NULL;
+ GVariant *data_desc_uuid = NULL;
+ char *read_val = NULL;
+ char *svc_uuid = NULL;
+ char *char_uuid = NULL;
+ char *desc_uuid = NULL;
+ int i;
+ int uuid_len = 16;
+ BT_INFO("+");
+
+ memset(&read_info, 0x00, sizeof(bluetooth_gatt_client_desc_prop_info_t));
+
+ /* Extract Address from conn_id of event data */
+ conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
+ event_data->uuid_status.conn_status.conn_id);
+
+ BT_INFO("Descriptor Read result from addr [%s] status [%d]",
+ conn_info->addr, event_data->uuid_status.conn_status.status);
+
+ /* Fill descriptor informations in buffer */
+ memcpy(&read_info.descriptor.uuid,
+ event_data->uuid_status.descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
+ read_info.descriptor.instance_id = event_data->uuid_status.descr_id.inst_id;
+
+ /* Fill Characteristic informations in buffer */
+ memcpy(&read_info.characteristic.uuid,
+ event_data->uuid_status.char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
+ read_info.characteristic.instance_id = event_data->uuid_status.char_id.inst_id;
+
+ /* Fill Service informations in buffer */
+ memcpy(&read_info.svc.uuid,
+ event_data->uuid_status.srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
+ read_info.svc.instance_id = event_data->uuid_status.srvc_id.id.inst_id;
+
+ /* Fill remote device address */
+ _bt_convert_addr_string_to_type(read_info.device_address.addr, conn_info->addr);
+
+ /* Fill data and reply to all apps waiting for Read result on the same characteristic */
+ if (event_data->uuid_status.conn_status.status != OAL_STATUS_SUCCESS)
+ result = BLUETOOTH_ERROR_INTERNAL;
+ else {
+ if (event_data->data_len > 0) {
+ /* DEBUG */
+ for (i = 0; i < event_data->data_len; i++)
+ BT_DBG("Data[%d] = [0x%x]", i, event_data->data[i]);
+
+ /* Read data */
+ read_val = g_memdup(&event_data->data[0], event_data->data_len);
+
+ data = g_variant_new_from_data(
+ G_VARIANT_TYPE_BYTESTRING,
+ read_val,
+ event_data->data_len,
+ TRUE, NULL, NULL);
+ /* SVC uuid */
+ svc_uuid = g_memdup(&event_data->uuid_status.srvc_id.id.uuid.uuid[0], uuid_len);
+
+ data_svc_uuid = g_variant_new_from_data(
+ G_VARIANT_TYPE_BYTESTRING,
+ svc_uuid,
+ 16,
+ TRUE, NULL, NULL);
+
+ /* Char uuid */
+ char_uuid = g_memdup(&event_data->uuid_status.char_id.uuid.uuid[0], uuid_len);
+
+ data_char_uuid = g_variant_new_from_data(
+ G_VARIANT_TYPE_BYTESTRING,
+ char_uuid,
+ 16,
+ TRUE, NULL, NULL);
+
+ /* Desc uuid */
+ desc_uuid = g_memdup(&event_data->uuid_status.descr_id.uuid.uuid[0], uuid_len);
+
+ data_desc_uuid = g_variant_new_from_data(
+ G_VARIANT_TYPE_BYTESTRING,
+ desc_uuid,
+ 16,
+ TRUE, NULL, NULL);
+
+ param = g_variant_new("(isn@ayin@ayin@ayin@ay)", result,
+ conn_info->addr,
+ uuid_len,
+ data_svc_uuid,
+ event_data->uuid_status.srvc_id.id.inst_id,
+ 16,
+ data_char_uuid,
+ event_data->uuid_status.char_id.inst_id,
+ 16,
+ data_desc_uuid,
+ event_data->uuid_status.descr_id.inst_id,
+ event_data->data_len,
+ data);
+
+ /* Send Event */
+ _bt_send_event(BT_GATT_CLIENT_EVENT,
+ BLUETOOTH_EVENT_GATT_READ_DESC,
+ param);
+ } else {
+ BT_ERR("Descriptor Read success, but no data!!!");
+ result = BLUETOOTH_ERROR_INTERNAL;
+ }
+ }
+ BT_INFO("Send DBUS rpely for GATT Read Descriptor");
+ /* Send DBUS return */
+ __bt_gatt_handle_pending_request_info(result,
+ BT_GATT_READ_DESCRIPTOR_VALUE,
+ &read_info,
+ sizeof(bluetooth_gatt_client_desc_prop_info_t));
+ if (read_val)
+ g_free(read_val);
+ if (svc_uuid)
+ g_free(svc_uuid);
+ if (char_uuid)
+ g_free(char_uuid);
+ if (desc_uuid)
+ g_free(desc_uuid);
+}
+
+static void __bt_handle_client_characteristic_write_data(
+ event_gattc_write_data *event_data)
+{
+ int result = BLUETOOTH_ERROR_NONE;
+ struct gatt_server_info_t *conn_info = NULL;
+ bluetooth_gatt_client_char_prop_info_t write_info;
+
+ /* Read Information data structures */
+ GVariant *param = NULL;
+ GVariant *data_svc_uuid = NULL;
+ GVariant *data_char_uuid = NULL;
+ char *svc_uuid = NULL;
+ char *char_uuid = NULL;
+ int uuid_len = 16;
+ BT_INFO("+");
+
+ memset(&write_info, 0x00, sizeof(bluetooth_gatt_client_char_prop_info_t));
+
+ /* Extract Address from conn_id of event data */
+ conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
+ event_data->conn_status.conn_id);
+
+ BT_INFO("Characteristic Write callback from addr [%s] status [%d]",
+ conn_info->addr, event_data->conn_status.status);
+
+ /* Fill char in buffer */
+ memcpy(&write_info.characteristic.uuid,
+ event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
+ write_info.characteristic.instance_id = event_data->char_id.inst_id;
+
+ /* Fill Service in buffer */
+ memcpy(&write_info.svc.uuid,
+ event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
+ write_info.svc.instance_id = event_data->srvc_id.id.inst_id;
+
+ /* Fill remote device address */
+ _bt_convert_addr_string_to_type(write_info.device_address.addr, conn_info->addr);
+
+ if (event_data->conn_status.status != OAL_STATUS_SUCCESS) {
+ result = BLUETOOTH_ERROR_INTERNAL;
+ goto done;
+ }
+
+ /* Build event */
+ /* SVC uuid */
+ svc_uuid = g_memdup(&event_data->srvc_id.id.uuid.uuid[0], uuid_len);
+
+ data_svc_uuid = g_variant_new_from_data(
+ G_VARIANT_TYPE_BYTESTRING,
+ svc_uuid,
+ uuid_len,
+ TRUE, NULL, NULL);
+
+ /* Char uuid */
+ char_uuid = g_memdup(&event_data->char_id.uuid.uuid[0], uuid_len);
+
+ data_char_uuid = g_variant_new_from_data(
+ G_VARIANT_TYPE_BYTESTRING,
+ char_uuid,
+ uuid_len,
+ TRUE, NULL, NULL);
+
+ param = g_variant_new("(isn@ayin@ayi)", result,
+ conn_info->addr,
+ 16,
+ data_svc_uuid,
+ event_data->srvc_id.id.inst_id,
+ 16,
+ data_char_uuid,
+ event_data->char_id.inst_id);
+
+ /* Send Event */
+ _bt_send_event(BT_GATT_CLIENT_EVENT,
+ BLUETOOTH_EVENT_GATT_WRITE_CHAR,
+ param);
+
+ /* Free data */
+ if (svc_uuid)
+ g_free(svc_uuid);
+ if (char_uuid)
+ g_free(char_uuid);
+done:
+ /* Send DBUS return */
+ __bt_gatt_handle_pending_request_info(result,
+ BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE,
+ &write_info,
+ sizeof(bluetooth_gatt_client_char_prop_info_t));
+}
+
+
+static void __bt_handle_client_descriptor_write_data(
+ event_gattc_write_data *event_data)
+{
+ int result = BLUETOOTH_ERROR_NONE;
+ struct gatt_server_info_t *conn_info = NULL;
+ bluetooth_gatt_client_desc_prop_info_t write_info;
+
+ /* Write Information data structures */
+ GVariant *param = NULL;
+ GVariant *data_svc_uuid = NULL;
+ GVariant *data_char_uuid = NULL;
+ GVariant *data_desc_uuid = NULL;
+ char *svc_uuid = NULL;
+ char *char_uuid = NULL;
+ char *desc_uuid = NULL;
+ int uuid_len = 16;
+ BT_INFO("+");
+
+ memset(&write_info, 0x00, sizeof(bluetooth_gatt_client_desc_prop_info_t));
+
+ /* Extract Address from conn_id of event data */
+ conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
+ event_data->conn_status.conn_id);
+
+ if (NULL == conn_info) {
+
+ BT_INFO("Failed to get the conn info for conn_id [%d]", event_data->conn_status.conn_id);
+ return;
+ }
+
+ BT_INFO("Descriptor Write callback from addr [%s] status [%d]",
+ conn_info->addr, event_data->conn_status.status);
+
+ /* Fill descriptor informations in buffer */
+ memcpy(&write_info.descriptor.uuid,
+ event_data->descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
+ write_info.descriptor.instance_id = event_data->descr_id.inst_id;
+
+ /* Fill Characteristic informations in buffer */
+ memcpy(&write_info.characteristic.uuid,
+ event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
+ write_info.characteristic.instance_id = event_data->char_id.inst_id;
+
+ /* Fill Service informations in buffer */
+ memcpy(&write_info.svc.uuid,
+ event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
+ write_info.svc.instance_id = event_data->srvc_id.id.inst_id;
+
+ /* Fill remote device address */
+ _bt_convert_addr_string_to_type(write_info.device_address.addr, conn_info->addr);
+
+ if (event_data->conn_status.status != OAL_STATUS_SUCCESS) {
+ result = BLUETOOTH_ERROR_INTERNAL;
+ goto done;
+ }
+
+ /* Build event */
+ /* SVC uuid */
+ svc_uuid = g_memdup(&event_data->srvc_id.id.uuid.uuid[0], uuid_len);
+
+ data_svc_uuid = g_variant_new_from_data(
+ G_VARIANT_TYPE_BYTESTRING,
+ svc_uuid,
+ uuid_len,
+ TRUE, NULL, NULL);
+
+ /* Char uuid */
+ char_uuid = g_memdup(&event_data->char_id.uuid.uuid[0], uuid_len);
+
+ data_char_uuid = g_variant_new_from_data(
+ G_VARIANT_TYPE_BYTESTRING,
+ char_uuid,
+ uuid_len,
+ TRUE, NULL, NULL);
+
+ /* Desc uuid */
+ desc_uuid = g_memdup(&event_data->descr_id.uuid.uuid[0], uuid_len);
+
+ data_desc_uuid = g_variant_new_from_data(
+ G_VARIANT_TYPE_BYTESTRING,
+ desc_uuid,
+ uuid_len,
+ TRUE, NULL, NULL);
+
+ param = g_variant_new("(isn@ayin@ayin@ayi)", result,
+ conn_info->addr,
+ 16,
+ data_svc_uuid,
+ event_data->srvc_id.id.inst_id,
+ 16,
+ data_char_uuid,
+ event_data->char_id.inst_id,
+ 16,
+ data_desc_uuid,
+ event_data->descr_id.inst_id);
+
+ /* Send Event */
+ _bt_send_event(BT_GATT_CLIENT_EVENT,
+ BLUETOOTH_EVENT_GATT_WRITE_DESC,
+ param);
+
+ /* Free data */
+ if (svc_uuid)
+ g_free(svc_uuid);
+ if (char_uuid)
+ g_free(char_uuid);
+ if (desc_uuid)
+ g_free(desc_uuid);
+done:
+ /* Send DBUS return */
+ __bt_gatt_handle_pending_request_info(result,
+ BT_GATT_WRITE_DESCRIPTOR_VALUE,
+ &write_info,
+ sizeof(bluetooth_gatt_client_desc_prop_info_t));
+}
+
+static void __bt_hanlde_le_device_disconnection(event_dev_conn_status_t *event_data)
+{
+ int result = BLUETOOTH_ERROR_INTERNAL;
+ char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
+
+ _bt_convert_addr_type_to_string(address, (unsigned char *)event_data->address.addr);
+
+ /* DBUS Return with fail of pending BT_CONNECT_LE for all the apps */
+ BT_INFO("Local GATT Client disconnected: Remote addr[%s] ", address);
+
+ __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE, address,
+ BT_ADDRESS_STRING_SIZE);
+}
+
+static void __bt_handle_client_notification_registered(
+ event_gattc_regdereg_notify_t *event_data,
+ gboolean is_registered)
+{
+ int result = BLUETOOTH_ERROR_NONE;
+ struct gatt_server_info_t *conn_info = NULL;
+ bt_gatt_notif_reg_info_t notif_info;
+ BT_INFO("+");
+
+ memset(¬if_info, 0x00, sizeof(bt_gatt_notif_reg_info_t));
+
+ BT_INFO("Client Interface [%d] status [%d]",
+ event_data->client_if,
+ event_data->status);
+
+ /* Extract Address from conn_id of event data */
+ conn_info = __bt_find_remote_gatt_server_info_from_client_if(
+ event_data->client_if);
+
+ if (!conn_info) {
+ BT_INFO("Connection Info is not present, return");
+ return;
+ }
+ BT_INFO("Notification Registered for addr [%s]",
+ conn_info->addr);
+
+
+ /* Fill svc informations in buffer */
+ memcpy(¬if_info.svc_uuid,
+ event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
+ notif_info.svc_inst = event_data->srvc_id.id.inst_id;
+
+ /* Fill char in buffer */
+ memcpy(¬if_info.char_uuid,
+ event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
+ notif_info.char_inst = event_data->char_id.inst_id;
+
+ /* Fill remote device address */
+ _bt_convert_addr_string_to_type(notif_info.addr.addr, conn_info->addr);
+
+ notif_info.is_registered = is_registered;
+
+ if (event_data->status != OAL_STATUS_SUCCESS)
+ result = BLUETOOTH_ERROR_INTERNAL;
+
+ /* Send DBUS Return for BT_GATT_WATCH_CHARACTERISTIC */
+ __bt_gatt_handle_pending_request_info(result,
+ BT_GATT_WATCH_CHARACTERISTIC,
+ ¬if_info,
+ sizeof(bt_gatt_notif_reg_info_t));
+}
+
+
+gboolean _bt_is_remote_gatt_device_connected(bluetooth_device_address_t *address)
+{
+ char *addr;
+ struct gatt_server_info_t *conn_info = NULL;
+ gboolean connected = FALSE;
+ BT_INFO("+");
+
+ addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
+ _bt_convert_addr_type_to_string(addr,
+ (unsigned char *)&(address->addr));
+
+ BT_INFO("Check Connected or not for [%s]", addr);
+ /* Check if device is already in connected list */
+ conn_info = __bt_find_remote_gatt_server_info(addr);
+
+ if (conn_info) {
+ BT_INFO("Remote GATT Server device [%s] is Connected", conn_info->addr);
+ connected = TRUE;
+ } else
+ BT_INFO("Remote GATT Server Device [%s] is not Connected", addr);
+ g_free(addr);
+ return connected;
+}
+
+
+int _bt_connect_le_device(bluetooth_device_address_t *address,
+ int auto_connect, int client_id)
+{
+ struct gatt_server_info_t *conn_info = NULL;
+ struct gatt_out_conn_info_t *out_conn_info = NULL;
+
+ invocation_info_t *req_info = NULL;
+ int ret = OAL_STATUS_SUCCESS;
+ char *addr;
+ char *remote_address = NULL;
+
+ BT_CHECK_PARAMETER(address, return);
+
+ BT_INFO("+");
+
+ addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
+ _bt_convert_addr_type_to_string(addr, address->addr);
+ BT_INFO("GATT Client connect request for address [%s] client instance [%d]",
+ addr, client_id);
+
+
+ /* Check if Remote Device is already under connection progress */
+ req_info = _bt_get_request_info_data_from_function_name(BT_CONNECT_LE);
+ if (req_info) {
+ remote_address = (char*)req_info->user_data;
+ if (remote_address && !strcasecmp(remote_address, addr)) {/* Address matched */
+ BT_INFO("Already Connection ongoing for same remote GATT Server address [%s]", remote_address);
+ /* Return and wait for events to be sent to all apps */
+ g_free(addr);
+ return BLUETOOTH_ERROR_IN_PROGRESS;
+ }
+ }
+
+ /* Check if remote GATT Server is connected or not */
+ conn_info = __bt_find_remote_gatt_server_info(addr);
+ if (conn_info) {
+ BT_ERR("GATT Server is already connected..");
+ g_free(addr);
+ return BLUETOOTH_ERROR_ALREADY_CONNECT;
+ }
+
+ /* TODO Check Requirement of holding Advertisement before initiating LE connect */
+
+ /* Check if app sent 0 client id for connection, in such case, use default gatt client ID */
+ if (client_id == 0) {
+ BT_INFO("GATT CLient connect request sent by an app without any client instance [%d]",
+ client_id);
+ BT_INFO("Assign default GATT client id [%d]", gatt_default_client);
+ client_id = gatt_default_client;
+ }
+
+ BT_INFO("Connect using CLient ID [%d]", client_id);
+ ret = gattc_connect(client_id, (bt_address_t*)(address), auto_connect);
+
+ if (ret != OAL_STATUS_SUCCESS) {
+ BT_ERR("ret: %d", ret);
+ g_free(addr);
+ return BLUETOOTH_ERROR_INTERNAL;
+ }
+
+ /* Mark this as outgoing connection */
+ out_conn_info = g_new0(struct gatt_out_conn_info_t, 1);
+ out_conn_info->addr = g_strdup(addr);
+ out_conn_info->client_id = client_id;
+ BT_INFO("Added outgoing connection info addr[%s]", out_conn_info->addr);
+ outgoing_gatt_conn_list = g_slist_append(outgoing_gatt_conn_list, out_conn_info);
+
+ g_free(addr);
+ return BLUETOOTH_ERROR_NONE;
+}
+
+int _bt_gatt_get_primary_services(char *address)
+{
+ BT_CHECK_PARAMETER(address, return);
+ struct gatt_server_info_t *conn_info = NULL;
+ invocation_info_t *req_info = NULL;
+ int ret = OAL_STATUS_SUCCESS;
+ BT_INFO("+");
+
+ /* Check if any app is already browsing primary services on the same remote GATT Server */
+ req_info = _bt_get_request_info_data(BT_GATT_GET_PRIMARY_SERVICES, address);
+ if (req_info) {
+ BT_INFO("Already Primary Service Browsing ongoing for same rmeote GATT Server");
+ /* Return and wait for events to be sent to all apps */
+ return BLUETOOTH_ERROR_NONE;
+ }
+
+ /* Check if remote GATT Server is connected or not */
+ conn_info = __bt_find_remote_gatt_server_info(address);
+ if (conn_info) {
+ BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
+ conn_info->addr, conn_info->connection_id);
+ } else {
+ BT_ERR("GATT Server is not yet connected..");
+ return BLUETOOTH_ERROR_NOT_CONNECTED;
+ }
+
+ /* Send Primary Service Browsing request to stack */
+ ret = gattc_search_service(conn_info->connection_id, NULL);
+ if (ret != OAL_STATUS_SUCCESS) {
+ BT_ERR("ret: %d", ret);
+ return BLUETOOTH_ERROR_INTERNAL;
+ }
+ return BLUETOOTH_ERROR_NONE;
+}
+
+int _bt_gatt_get_all_characteristic(bluetooth_gatt_client_svc_prop_info_t *svc)
+{
+ BT_CHECK_PARAMETER(svc, return);
+ struct gatt_server_info_t *conn_info = NULL;
+ invocation_info_t *req_info = NULL;
+ bluetooth_gatt_client_svc_prop_info_t *prop;
+ oal_gatt_srvc_id_t srvc_id;
+ int ret = OAL_STATUS_SUCCESS;
+ char *addr;
+ BT_INFO("+");
+
+ /* Check if any app is already browsing characteristics of the same service on the same remote GATT Server */
+ req_info = _bt_get_request_info_data_from_function_name(BT_GATT_GET_SERVICE_PROPERTIES);
+ if (req_info) {
+ prop = (bluetooth_gatt_client_svc_prop_info_t*)req_info->user_data;
+ if (prop && !memcmp(svc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t))
+ && memcmp(prop->svc.uuid, svc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
+ && prop->svc.instance_id == svc->svc.instance_id) {
+ BT_INFO("Already Properties browsing for Primary Service ongoing for same remote GATT Server");
+ /* Return and wait for events to be sent to all apps */
+ return BLUETOOTH_ERROR_NONE;
+ }
+ }
+
+ addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
+ _bt_convert_addr_type_to_string(addr, svc->device_address.addr);
+
+ /* Check if remote GATT Server is connected or not */
+ conn_info = __bt_find_remote_gatt_server_info(addr);
+ if (conn_info) {
+ BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
+ conn_info->addr, conn_info->connection_id);
+ } else {
+ BT_ERR("GATT Server is not yet connected..");
+ g_free(addr);
+ return BLUETOOTH_ERROR_NOT_CONNECTED;
+ }
+
+ srvc_id.is_prmry = TRUE;
+ srvc_id.id.inst_id = svc->svc.instance_id;
+ memcpy(srvc_id.id.uuid.uuid, svc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
+
+ /* Search All Characteristic */
+ ret = gattc_get_characteristic(conn_info->connection_id, &srvc_id, NULL);
+ if (ret != OAL_STATUS_SUCCESS) {
+ BT_ERR("ret: %d", ret);
+ g_free(addr);
+ return BLUETOOTH_ERROR_INTERNAL;
+ }
+ g_free(addr);
+ return BLUETOOTH_ERROR_NONE;
+}
+
+int _bt_gatt_get_all_characteristic_properties(
+ bluetooth_gatt_client_char_prop_info_t *chr)
+{
+ struct gatt_server_info_t *conn_info = NULL;
+ invocation_info_t *req_info = NULL;
+ bluetooth_gatt_client_char_prop_info_t *prop;
+ oal_gatt_srvc_id_t srvc_id;
+ oal_gatt_id_t char_id;
+ int ret = OAL_STATUS_SUCCESS;
+ char *addr;
+
+ BT_CHECK_PARAMETER(chr, return);
+
+ BT_INFO("+");
+
+ /* Check if any app is already browsing descriptors of the same char of
+ particular service on the same remote GATT Server */
+ req_info = _bt_get_request_info_data_from_function_name(BT_GATT_GET_CHARACTERISTIC_PROPERTIES);
+ if (req_info) {
+ prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
+ if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
+ && memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
+ && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
+ && memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
+ && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
+ BT_INFO("Already Properties browsing for Characteristic ongoing for same remote GATT Server");
+ /* Return and wait for events to be sent to all apps */
+ return BLUETOOTH_ERROR_NONE;
+ }
+ }
+
+ addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
+ _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
+
+ /* Check if remote GATT Server is connected or not */
+ conn_info = __bt_find_remote_gatt_server_info(addr);
+ if (conn_info) {
+ BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
+ conn_info->addr, conn_info->connection_id);
+ } else {
+ BT_ERR("GATT Server is not yet connected..");
+ g_free(addr);
+ return BLUETOOTH_ERROR_NOT_CONNECTED;
+ }
+
+ srvc_id.is_prmry = TRUE;
+ srvc_id.id.inst_id = chr->svc.instance_id;
+ memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
+
+ char_id.inst_id = chr->characteristic.instance_id;
+ memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
+
+ /* Search All Descriptors */
+ ret = gattc_get_descriptor(conn_info->connection_id, &srvc_id, &char_id, NULL);
+ if (ret != OAL_STATUS_SUCCESS) {
+ BT_ERR("ret: %d", ret);
+ g_free(addr);
+ return BLUETOOTH_ERROR_INTERNAL;
+ }
+ g_free(addr);
+ return BLUETOOTH_ERROR_NONE;
+}
+
+int _bt_gatt_read_characteristic_value(
+ bluetooth_gatt_client_char_prop_info_t *chr)
+{
+ struct gatt_server_info_t *conn_info = NULL;
+ invocation_info_t *req_info = NULL;
+ bluetooth_gatt_client_char_prop_info_t *prop;
+ oal_gatt_srvc_id_t srvc_id;
+ oal_gatt_id_t char_id;
+ int ret = OAL_STATUS_SUCCESS;
+ char *addr;
+
+ BT_CHECK_PARAMETER(chr, return);
+
+ BT_INFO("+");
+
+ /* Check if any app is already Reading characteristic of the same char of
+ particular service on the same remote GATT Server */
+ req_info = _bt_get_request_info_data_from_function_name(BT_GATT_READ_CHARACTERISTIC);
+ if (req_info) {
+ prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
+ if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
+ && memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
+ && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
+ && memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
+ && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
+ BT_INFO("Already Characteristic value Read operation in progress for same remote GATT Server");
+ /* Return and wait for events to be sent to all apps */
+ return BLUETOOTH_ERROR_NONE;
+ }
+ }
+
+ addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
+ _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
+
+ /* Check if remote GATT Server is connected or not */
+ conn_info = __bt_find_remote_gatt_server_info(addr);
+ if (conn_info) {
+ BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
+ conn_info->addr, conn_info->connection_id);
+ } else {
+ BT_ERR("GATT Server is not yet connected..");
+ g_free(addr);
+ return BLUETOOTH_ERROR_NOT_CONNECTED;
+ }
+
+ srvc_id.is_prmry = TRUE;
+ srvc_id.id.inst_id = chr->svc.instance_id;
+ memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
+
+ char_id.inst_id = chr->characteristic.instance_id;
+ memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
+
+ /* Search All Descriptors */
+ ret = gattc_read_characteristic(conn_info->connection_id, &srvc_id, &char_id, OAL_GATT_AUTH_REQ_NONE);
+ if (ret != OAL_STATUS_SUCCESS) {
+ BT_ERR("ret: %d", ret);
+ g_free(addr);
+ return BLUETOOTH_ERROR_INTERNAL;
+ }
+ g_free(addr);
+ return BLUETOOTH_ERROR_NONE;
+}
+
+int _bt_gatt_read_descriptor_value(
+ bluetooth_gatt_client_desc_prop_info_t *desc)
+{
+ struct gatt_server_info_t *conn_info = NULL;
+ invocation_info_t *req_info = NULL;
+ bluetooth_gatt_client_desc_prop_info_t *prop;
+ oal_gatt_srvc_id_t srvc_id;
+ oal_gatt_id_t char_id;
+ oal_gatt_id_t desc_id;
+ int ret = OAL_STATUS_SUCCESS;
+ char *addr;
+
+ BT_CHECK_PARAMETER(desc, return);
+
+ BT_INFO("+");
+
+ /* Check if any app is already Reading descriptors of the same char of
+ particular service on the same remote GATT Server */
+ req_info = _bt_get_request_info_data_from_function_name(BT_GATT_READ_DESCRIPTOR_VALUE);
+ if (req_info) {
+ prop = (bluetooth_gatt_client_desc_prop_info_t*)req_info->user_data;
+ if (prop && !memcmp(desc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
+ && memcmp(desc->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
+ && desc->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
+ && memcmp(desc->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
+ && desc->characteristic.instance_id == prop->characteristic.instance_id /* Characteristic Instance ID matched */
+ && memcmp(desc->descriptor.uuid, prop->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Descriptor UUID matched */
+ && desc->descriptor.instance_id == prop->descriptor.instance_id) { /* Descriptor Instance ID matched */
+ BT_INFO("Already Descriptor value Read operation in progress for same remote GATT Server");
+ /* Return and wait for events to be sent to all apps */
+ return BLUETOOTH_ERROR_NONE;
+ }
+ }
+
+ addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
+ _bt_convert_addr_type_to_string(addr, desc->device_address.addr);
+
+ /* Check if remote GATT Server is connected or not */
+ conn_info = __bt_find_remote_gatt_server_info(addr);
+ if (conn_info) {
+ BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
+ conn_info->addr, conn_info->connection_id);
+ } else {
+ BT_ERR("GATT Server is not yet connected..");
+ g_free(addr);
+ return BLUETOOTH_ERROR_NOT_CONNECTED;
+ }
+
+ srvc_id.is_prmry = TRUE;
+ srvc_id.id.inst_id = desc->svc.instance_id;
+ memcpy(srvc_id.id.uuid.uuid, desc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
+
+ char_id.inst_id = desc->characteristic.instance_id;
+ memcpy(char_id.uuid.uuid, desc->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
+
+ desc_id.inst_id = desc->descriptor.instance_id;
+ memcpy(desc_id.uuid.uuid, desc->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
+
+ /* Search All Descriptors */
+ ret = gattc_read_descriptor(conn_info->connection_id, &srvc_id, &char_id,
+ &desc_id, OAL_GATT_AUTH_REQ_NONE);
+ if (ret != OAL_STATUS_SUCCESS) {
+ BT_ERR("ret: %d", ret);
+ g_free(addr);
+ return BLUETOOTH_ERROR_INTERNAL;
+ }
+ g_free(addr);
+ return BLUETOOTH_ERROR_NONE;
+}
+
+
+/* Write Characteristic */
+int _bt_gatt_write_characteristic_value_by_type(
+ bluetooth_gatt_client_char_prop_info_t *chr,
+ bluetooth_gatt_att_data_t *data,
+ bluetooth_gatt_write_type_e write_type)
+{
+ struct gatt_server_info_t *conn_info = NULL;
+ invocation_info_t *req_info = NULL;
+ bluetooth_gatt_client_char_prop_info_t *prop;
+ oal_gatt_srvc_id_t srvc_id;
+ oal_gatt_id_t char_id;
+ int ret = OAL_STATUS_SUCCESS;
+ char *addr;
+ int k;
+
+ BT_CHECK_PARAMETER(chr, return);
+ BT_CHECK_PARAMETER(data, return);
+
+ BT_INFO("+");
+
+ /* Check if any app is already writing same char of
+ particular service on the same remote GATT Server */
+ req_info = _bt_get_request_info_data_from_function_name(BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE);
+ if (req_info) {
+ prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
+ if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
+ && memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
+ && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
+ && memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
+ && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
+ BT_INFO("Already Characteristic Write Value operation in progress for same remote GATT Server");
+ /* Return and wait for events to be sent to all apps */
+ return BLUETOOTH_ERROR_NONE;
+ }
+ }
+
+ addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
+ _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
+
+ /* Check if remote GATT Server is connected or not */
+ conn_info = __bt_find_remote_gatt_server_info(addr);
+ if (conn_info) {
+ BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
+ conn_info->addr, conn_info->connection_id);
+ } else {
+ BT_ERR("GATT Server is not yet connected..");
+ g_free(addr);
+ return BLUETOOTH_ERROR_NOT_CONNECTED;
+ }
+
+ srvc_id.is_prmry = TRUE;
+ srvc_id.id.inst_id = chr->svc.instance_id;
+ memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
+
+ char_id.inst_id = chr->characteristic.instance_id;
+ memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
+
+ /* Write CHar value */
+ /* DEBUG */
+ BT_INFO("Connection ID [%d] write type [%d] data length [%d]", conn_info->connection_id, write_type, data->length);
+ for (k = 0; k < data->length; k++)
+ BT_INFO("Data[%d] [0x%x]", k, data->data[k]);
+
+ ret = gattc_write_characteristic(conn_info->connection_id,
+ &srvc_id, &char_id,
+ (oal_gatt_write_type_t)write_type, data->length,
+ OAL_GATT_AUTH_REQ_NONE, (char *)(&data->data[0]));
+ if (ret != OAL_STATUS_SUCCESS) {
+ BT_ERR("ret: %d", ret);
+ g_free(addr);
+ return BLUETOOTH_ERROR_INTERNAL;
+ }
+ g_free(addr);
+ return BLUETOOTH_ERROR_NONE;
+}
+
+/* Write Descriptor */
+int _bt_gatt_write_descriptor_value_by_type(
+ bluetooth_gatt_client_desc_prop_info_t *desc,
+ bluetooth_gatt_att_data_t *data,
+ bluetooth_gatt_write_type_e write_type)
+{
+ struct gatt_server_info_t *conn_info = NULL;
+ invocation_info_t *req_info = NULL;
+ bluetooth_gatt_client_desc_prop_info_t *prop;
+ oal_gatt_srvc_id_t srvc_id;
+ oal_gatt_id_t char_id;
+ oal_gatt_id_t desc_id;
+ int ret = OAL_STATUS_SUCCESS;
+ char *addr;
+ int k;
+
+ BT_CHECK_PARAMETER(desc, return);
+ BT_CHECK_PARAMETER(data, return);
+
+ BT_INFO("+");
+
+ /* Check if any app is already writing on same Descriptor of the same char of
+ particular service on the same remote GATT Server */
+ req_info = _bt_get_request_info_data_from_function_name(BT_GATT_WRITE_DESCRIPTOR_VALUE);
+ if (req_info) {
+ prop = (bluetooth_gatt_client_desc_prop_info_t*)req_info->user_data;
+ if (prop && !memcmp(desc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
+ && memcmp(desc->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
+ && desc->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
+ && memcmp(desc->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
+ && desc->characteristic.instance_id == prop->characteristic.instance_id /* Characteristic Instance ID matched */
+ && memcmp(desc->descriptor.uuid, prop->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Descriptor UUID matched */
+ && desc->descriptor.instance_id == prop->descriptor.instance_id) { /* Descriptor Instance ID matched */
+ BT_INFO("Already Descriptor value Write operation in progress for same remote GATT Server");
+ /* Return and wait for events to be sent to all apps */
+ return BLUETOOTH_ERROR_NONE;
+ }
+ }
+
+ addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
+ _bt_convert_addr_type_to_string(addr, desc->device_address.addr);
+
+ /* Check if remote GATT Server is connected or not */
+ conn_info = __bt_find_remote_gatt_server_info(addr);
+ if (conn_info) {
+ BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
+ conn_info->addr, conn_info->connection_id);
+ } else {
+ BT_ERR("GATT Server is not yet connected..");
+ g_free(addr);
+ return BLUETOOTH_ERROR_NOT_CONNECTED;
+ }
+
+ srvc_id.is_prmry = TRUE;
+ srvc_id.id.inst_id = desc->svc.instance_id;
+ memcpy(srvc_id.id.uuid.uuid, desc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
+
+ char_id.inst_id = desc->characteristic.instance_id;
+ memcpy(char_id.uuid.uuid, desc->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
+
+ desc_id.inst_id = desc->descriptor.instance_id;
+ memcpy(desc_id.uuid.uuid, desc->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
+
+ /* DEBUG */
+ BT_INFO("Connection ID [%d] write type [%d] data length [%d]", conn_info->connection_id, write_type, data->length);
+ for (k = 0; k < data->length; k++)
+ BT_INFO("Data[%d] [0x%x]", k, data->data[k]);
+
+ ret = gattc_write_descriptor(conn_info->connection_id,
+ &srvc_id, &char_id, &desc_id,
+ (oal_gatt_write_type_t)write_type, data->length,
+ OAL_GATT_AUTH_REQ_NONE, (char *)(&data->data[0]));
+ if (ret != OAL_STATUS_SUCCESS) {
+ BT_ERR("ret: %d", ret);
+ g_free(addr);
+ return BLUETOOTH_ERROR_INTERNAL;
+ }
+ g_free(addr);
+ return BLUETOOTH_ERROR_NONE;
+}
+
+int _bt_gatt_watch_characteristic(
+ bluetooth_gatt_client_char_prop_info_t *chr,
+ int client_id,
+ gboolean is_notify)
+{
+ struct gatt_server_info_t *conn_info = NULL;
+ oal_gatt_srvc_id_t srvc_id;
+ oal_gatt_id_t char_id;
+ int ret = OAL_STATUS_SUCCESS;
+ char *addr;
+
+ BT_CHECK_PARAMETER(chr, return);
+
+ BT_INFO("Client ID [%d] Is Notify [%d]", client_id, is_notify);
+
+ addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
+ _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
+
+ /* Check if remote GATT Server is connected or not */
+ conn_info = __bt_find_remote_gatt_server_info(addr);
+ if (conn_info) {
+ BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
+ conn_info->addr, conn_info->connection_id);
+ } else {
+ BT_ERR("GATT Server is not yet connected..");
+ g_free(addr);
+ return BLUETOOTH_ERROR_NOT_CONNECTED;
+ }
+ srvc_id.is_prmry = TRUE;
+ srvc_id.id.inst_id = chr->svc.instance_id;
+ memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
+
+ char_id.inst_id = chr->characteristic.instance_id;
+ memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
+
+ /* Register or unregister Notification characteristic */
+ if (is_notify)
+ ret = gattc_register_for_notification(conn_info->client_id,
+ (bt_address_t*)&(chr->device_address),
+ &srvc_id, &char_id);
+ else
+ ret = gattc_deregister_for_notification(conn_info->client_id,
+ (bt_address_t*)&(chr->device_address),
+ &srvc_id, &char_id);
+
+ BT_INFO("Result[%d]", ret);
+ if (ret != OAL_STATUS_SUCCESS) {
+ BT_ERR("ret: %d", ret);
+ g_free(addr);
+ return BLUETOOTH_ERROR_INTERNAL;
+ }
+ g_free(addr);
+ return BLUETOOTH_ERROR_NONE;
+}
+
+
+int _bt_disconnect_le_device(bluetooth_device_address_t *address,
+ int client_id)
+{
+ struct gatt_server_info_t *conn_info = NULL;
+ invocation_info_t *req_info = NULL;
+ int ret = OAL_STATUS_SUCCESS;
+ char *addr;
+ char *remote_address = NULL;
+
+ BT_CHECK_PARAMETER(address, return);
+
+ BT_INFO("+");
+
+ addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
+ _bt_convert_addr_type_to_string(addr, address->addr);
+ BT_INFO("GATT Client Disconnect request for address [%s]", addr);
+
+ /* Check if Remote Device is already under connection progress */
+ req_info = _bt_get_request_info_data_from_function_name(BT_DISCONNECT_LE);
+ if (req_info) {
+ remote_address = (char*)req_info->user_data;
+ if (remote_address && !strcasecmp(remote_address, addr)) {/* Address matched */
+ BT_INFO("Already DisConnection ongoing for same remote GATT Server address [%s]", remote_address);
+ /* Return success and wait for events to be sent to all apps */
+ g_free(addr);
+ return BLUETOOTH_ERROR_IN_PROGRESS;
+ }
+ }
+ /* Check if remote GATT Server is connected or not */
+ conn_info = __bt_find_remote_gatt_server_info(addr);
+ if (!conn_info) {
+ BT_ERR("GATT Server is not connected..");
+ g_free(addr);
+ return BLUETOOTH_ERROR_NOT_IN_OPERATION;
+ }
+
+ /* Check if app sent 0 client id for Disconnection, in such case, use default gatt client ID */
+ if (client_id == 0) {
+ BT_INFO("GATT CLient Disconnect request sent by an app without any client instance [%d]",
+ client_id);
+ BT_INFO("Assign default GATT client id [%d]", gatt_default_client);
+ client_id = gatt_default_client;
+ }
+
+ BT_INFO("Disconnect using CLient ID [%d] Connection ID [%d]", client_id, conn_info->connection_id);
+ ret = gattc_disconnect(client_id, (bt_address_t*)(address),
+ conn_info->connection_id);
+
+ if (ret != OAL_STATUS_SUCCESS) {
+ BT_ERR("ret: %d", ret);
+ g_free(addr);
+ return BLUETOOTH_ERROR_INTERNAL;
+ }
+ g_free(addr);
+ return BLUETOOTH_ERROR_NONE;
+}
+
+int _bt_gatt_watch_service_changed_indication(const char *sender,
+ bluetooth_device_address_t *address,
+ gboolean is_enabled)
+{
+ int k;
+ bt_service_app_info_t *info = NULL;
+
+ BT_INFO("Enable Servic changed Indication watcher [%d] for app [%s]",
+ is_enabled, sender);
+
+ for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
+ info = &numapps[k];
+
+ if (g_strcmp0(sender, info->sender) == 0 &&
+ memcmp(info->address.addr, address->addr,
+ sizeof(bluetooth_device_address_t)) == 0) {
+ BT_INFO("Found GATT client App.. UUID [%s], sender [%s]", info->uuid, info->sender);
+ info->is_watcher_enabled = is_enabled;
+ }
+ }
+
+ return BLUETOOTH_ERROR_NONE;
+}
+
+int _bt_unregister_gatt_client_instance(const char *sender, int client_id)
+{
+ BT_INFO("Unregister Allocated GATT CLient instance [%s] Client ID [%d]",
+ sender, client_id);
+ int result = BLUETOOTH_ERROR_NONE;
+ int k;
+
+ /* Unregister CLient instance associated with address X. It is possible that another app still
+ has client_id valid for same remote address */
+ bt_service_app_info_t *info = NULL;
+
+ for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
+ info = &numapps[k];
+
+ /* Exact matching of sender */
+ if (!g_strcmp0(info->sender, sender) && info->client_id == client_id) { /* Check for only valid GATT client Instance */
+ BT_INFO("Unregister GATT client instance [%d]", info->client_id);
+ result = __bt_do_unregister_gatt_instance(info->client_id);
+ if (result != BLUETOOTH_ERROR_NONE)
+ BT_ERR("Error in unregistering GATT Client Interface");
+
+ break;
+ }
+ }
+ return result;
+}
+
+#endif