+
+#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 {
+ 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);
+ /* 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);
+ } else {
+ BT_ERR("Characteristic Read success, but no data!!!");
+ result = BLUETOOTH_ERROR_INTERNAL;
+ }
+ }
+ /* 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);
+}
+
+
+
+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_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