+
+
+
+/* GATT Client*/
+BT_EXPORT_API int bluetooth_gatt_client_get_primary_services(
+ const bluetooth_device_address_t *address, /* Remote GATT Server */
+ bt_gatt_handle_info_t *prim_svc) /* UUID & instance_id */
+{
+
+ int result = BLUETOOTH_ERROR_NONE;
+ bt_services_browse_info_t service_props;
+
+ BT_CHECK_PARAMETER(address, return);
+ BT_CHECK_PARAMETER(prim_svc, return);
+ BT_CHECK_ENABLED(return);
+
+ BT_INIT_PARAMS();
+ BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
+ g_array_append_vals(in_param1, address, sizeof(bluetooth_device_address_t));
+
+ result = _bt_send_request(BT_BLUEZ_SERVICE,
+ BT_GATT_GET_PRIMARY_SERVICES,
+ in_param1, in_param2, in_param3, in_param4, &out_param);
+
+ if (BLUETOOTH_ERROR_NONE != result)
+ goto done;
+
+ memset(&service_props, 0x00, sizeof(bt_services_browse_info_t));
+
+ service_props = g_array_index(
+ out_param, bt_services_browse_info_t, 0);
+
+ __bt_fill_service_handle_informations(
+ &service_props, prim_svc);
+
+done:
+ BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
+ return result;
+}
+
+BT_EXPORT_API int bluetooth_gatt_client_get_service_property(
+ const char *address,
+ bt_gatt_handle_property_t *service_handle,
+ bt_gatt_service_property_t *service)
+{
+ int result = BLUETOOTH_ERROR_NONE;
+ bluetooth_gatt_client_svc_prop_info_t svc_prop;
+ bt_char_browse_info_t char_handles_info;
+
+ BT_CHECK_PARAMETER(address, return);
+ BT_CHECK_PARAMETER(service_handle, return);
+ BT_CHECK_PARAMETER(service, return);
+ BT_CHECK_ENABLED(return);
+
+ /* Call to bt-service (sync) and send address and service_handle info */
+ BT_INIT_PARAMS();
+ BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
+
+ memset(&svc_prop, 0x00, sizeof(bluetooth_gatt_client_svc_prop_info_t));
+ /* All characteristics handles are discovered */
+ memset(&char_handles_info, 0x00, sizeof(bt_char_browse_info_t));
+
+ svc_prop.svc.instance_id = service_handle->instance_id;
+ memcpy(&svc_prop.svc.uuid, &service_handle->uuid, 16);
+
+ _bt_convert_addr_string_to_type(svc_prop.device_address.addr, address);
+
+ g_array_append_vals(in_param1, &svc_prop, sizeof(bluetooth_gatt_client_svc_prop_info_t));
+
+ result = _bt_send_request(BT_BLUEZ_SERVICE,
+ BT_GATT_GET_SERVICE_PROPERTIES,
+ in_param1, in_param2, in_param3, in_param4, &out_param);
+
+ if (BLUETOOTH_ERROR_NONE != result)
+ goto done;
+
+ char_handles_info = g_array_index(
+ out_param, bt_char_browse_info_t, 0);
+ __bt_fill_char_handle_informations(&char_handles_info,
+ service);
+ /* TODO Get all Included Services */
+
+done:
+ BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
+ return result;
+}
+
+BT_EXPORT_API int bluetooth_gatt_client_get_characteristics_property(
+ const char *address,
+ bt_gatt_handle_property_t *service_handle,
+ bt_gatt_handle_property_t *char_handle,
+ bt_gatt_char_property_t *char_property)
+{
+ int result = BLUETOOTH_ERROR_NONE;
+ bt_descriptor_browse_info_t desc_handles_info;
+ bluetooth_gatt_client_char_prop_info_t char_prop;
+
+ BT_CHECK_PARAMETER(address, return);
+ BT_CHECK_PARAMETER(service_handle, return);
+ BT_CHECK_PARAMETER(char_handle, return);
+ BT_CHECK_PARAMETER(char_property, return);
+ BT_CHECK_ENABLED(return);
+
+
+ /* Call to bt-service (sync) and send address service_handle info & char handle info */
+ BT_INIT_PARAMS();
+ BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
+
+ memset(&char_prop, 0x00, sizeof(bluetooth_gatt_client_char_prop_info_t));
+ char_prop.svc.instance_id = service_handle->instance_id;
+ memcpy(&char_prop.svc.uuid, &service_handle->uuid, 16);
+
+ char_prop.characteristic.instance_id = char_handle->instance_id;
+ memcpy(&char_prop.characteristic.uuid, &char_handle->uuid, 16);
+
+ _bt_convert_addr_string_to_type(char_prop.device_address.addr, address);
+
+ g_array_append_vals(in_param1, &char_prop, sizeof(bluetooth_gatt_client_char_prop_info_t));
+
+ result = _bt_send_request(BT_BLUEZ_SERVICE,
+ BT_GATT_GET_CHARACTERISTIC_PROPERTIES,
+ in_param1, in_param2, in_param3, in_param4, &out_param);
+
+ if (BLUETOOTH_ERROR_NONE != result)
+ goto done;
+
+ /* All descriptors handles are discovered */
+ memset(&desc_handles_info, 0x00, sizeof(bt_descriptor_browse_info_t));
+
+ desc_handles_info = g_array_index(
+ out_param, bt_descriptor_browse_info_t, 0);
+
+ __bt_fill_desc_handle_informations(&desc_handles_info,
+ char_property);
+
+done:
+ BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
+ return result;
+ /* Call to bt-service (sync) and send address, service_handle & char_handle infos */
+
+ /* After result is fetched, extract descriptor handles (UUID's & instance_id's) */
+
+ /* Fill the info in *char_prop */
+
+ /*No: Before returning, call bluetooth_gatt_client_read_characteristic_value as
+ an asyn function and leave every thing else in the callback */
+}
+
+BT_EXPORT_API int bluetooth_gatt_client_get_char_descriptor_property(
+ const char *address,
+ bt_gatt_handle_property_t *service_handle,
+ bt_gatt_handle_property_t *char_handle,
+ bt_gatt_handle_property_t *descriptor_handle,
+ bt_gatt_char_descriptor_property_t *desc_prop)
+{
+ char uuid_string[BLUETOOTH_UUID_STRING_MAX];
+
+ BT_CHECK_PARAMETER(address, return);
+ BT_CHECK_PARAMETER(service_handle, return);
+ BT_CHECK_PARAMETER(char_handle, return);
+ BT_CHECK_PARAMETER(descriptor_handle, return);
+ BT_CHECK_PARAMETER(desc_prop, return);
+ /* No Need to Call to bt-service (sync) and send address, service_handle,
+ char_handle & descriptor handle infos */
+ /* After result is fetched, extract descriptior handles (UUID's & instance_id's) */
+ /* Fill the info in *desc_prop */
+ __bt_uuid_hex_to_string(descriptor_handle->uuid, uuid_string);
+
+ /* Before filling all desc handles, fill the charac's UUID and instance ID */
+ desc_prop->uuid = g_strdup(uuid_string);
+
+ return BLUETOOTH_ERROR_NONE;
+ /* No: Before returning, call bluetooth_gatt_client_read_descriptor_value
+ as an asyn function and leave every thing else in the callback */
+}
+
+
+static gboolean bluetooth_gatt_client_notify_channel_watch_cb(GIOChannel *gio,
+ GIOCondition cond, gpointer data)
+{
+ bt_gatt_characteristic_notify_info_t *chr_info = (bt_gatt_characteristic_notify_info_t *)data;
+
+ if (!chr_info) {
+ BT_ERR("chr_info is invalid");
+ return FALSE;
+ }
+
+ if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) {
+ BT_ERR("Error : GIOCondition %d, [%s]", cond, chr_info->UUID);
+ g_io_channel_shutdown(gio, TRUE, NULL);
+ g_io_channel_unref(gio);
+
+ gatt_characteristic_notify_list = g_slist_remove(gatt_characteristic_notify_list, chr_info);
+ g_free(chr_info);
+
+ return FALSE;
+ }
+
+ if (cond & G_IO_IN) {
+ GIOStatus status = G_IO_STATUS_NORMAL;
+ GError *err = NULL;
+ char *buffer = NULL;
+ gsize len = 0;
+ bt_event_info_t *event_info;
+
+ buffer = g_malloc0(chr_info->mtu + 1);
+ memset(buffer, 0, chr_info->mtu + 1);
+
+ status = g_io_channel_read_chars(gio, buffer,
+ chr_info->mtu, &len, &err);
+ if (status != G_IO_STATUS_NORMAL) {
+ BT_ERR("IO Channel read is failed with %d", status);
+ g_free(buffer);
+ if (err) {
+ BT_ERR("IO Channel read error [%s]", err->message);
+ if (status == G_IO_STATUS_ERROR) {
+ BT_ERR("cond : %d", cond);
+ g_error_free(err);
+ g_io_channel_shutdown(gio, TRUE, NULL);
+ g_io_channel_unref(gio);
+
+ gatt_characteristic_notify_list = g_slist_remove(gatt_characteristic_notify_list, chr_info);
+ g_free(chr_info);
+ return FALSE;
+ }
+ g_error_free(err);
+ }
+ return FALSE;
+ }
+
+ if (len > 0 && len < chr_info->mtu) {
+ bt_gatt_char_property_t char_val;
+// BT_DBG("FD io sending value changed %x %x %x %x %x %x %x %zd \n", buffer[0], buffer[1], buffer[3], buffer[4], buffer[5], buffer[6], buffer[7], len);
+
+ memcpy(char_val.prop.uuid, chr_info->UUID, 16);
+ memcpy(char_val.value, buffer, len);
+ char_val.val_len = len;
+ memcpy(char_val.address, chr_info->address, 18);
+
+ event_info = _bt_event_get_cb_data(BT_GATT_CLIENT_EVENT);
+ if (event_info) {
+ _bt_common_event_cb(BLUETOOTH_EVENT_GATT_CHAR_VAL_CHANGED,
+ BLUETOOTH_ERROR_NONE, &char_val,
+ event_info->cb, event_info->user_data);
+ } else {
+ BT_ERR("event_info failed");
+ }
+
+ } else
+ BT_ERR("Packet corrupted");
+
+ g_free(buffer);
+
+ return TRUE;
+ }
+
+ return TRUE;
+}
+
+#ifndef TIZEN_FEATURE_BT_GATT_CLIENT_FD_DISABLE
+static bt_gatt_characteristic_notify_info_t * bluetooth_gatt_client_get_characteristic_notify_info(unsigned char *handle , int id)
+{
+ GSList *l;
+
+ for (l = gatt_characteristic_notify_list; l != NULL; l = l->next) {
+ bt_gatt_characteristic_notify_info_t *info = l->data;
+ if (memcmp(info->UUID, handle, 16) == 0 && info->id == id)
+ return info;
+ }
+ return NULL;
+}
+#endif
+
+static bt_gatt_characteristic_notify_info_t * bluetooth_gatt_client_create_watch_io(int fd, int id, int mtu, char * address, unsigned char *uuid)
+{
+ GIOChannel *channel;
+ bt_gatt_characteristic_notify_info_t *chr_info;
+
+ chr_info = g_malloc0(sizeof(bt_gatt_characteristic_notify_info_t));
+ chr_info->notify_fd = fd;
+ chr_info->id = id;
+ chr_info->mtu = mtu;
+ g_strlcpy(chr_info->address, address, 18);
+ memcpy(chr_info->UUID, uuid, 16);
+
+ channel = g_io_channel_unix_new(fd);
+
+ chr_info->io_channel = channel;
+
+ g_io_channel_set_encoding(channel, NULL, NULL);
+ g_io_channel_set_buffered(channel, FALSE);
+ g_io_channel_set_close_on_unref(channel, TRUE);
+ g_io_channel_set_flags(channel, G_IO_FLAG_NONBLOCK, NULL);
+
+ chr_info->watch_id = g_io_add_watch(channel, (G_IO_IN | G_IO_ERR | G_IO_HUP),
+ bluetooth_gatt_client_notify_channel_watch_cb, chr_info);
+
+ return chr_info;
+
+}
+
+BT_EXPORT_API int bluetooth_gatt_client_watch_characteristics(
+ const char *address,
+ bt_gatt_handle_property_t *service_handle,
+ bt_gatt_handle_property_t *char_handle,
+ int client_id,
+ gboolean is_notify,
+ gboolean is_indicate)
+{
+ int result = BLUETOOTH_ERROR_NONE;
+ bluetooth_gatt_client_char_prop_info_t param;
+ bt_gatt_characteristic_notify_info_t *chr_info;
+
+ BT_CHECK_PARAMETER(address, return);
+ BT_CHECK_PARAMETER(service_handle, return);
+ BT_CHECK_PARAMETER(char_handle, return);
+
+#ifndef TIZEN_FEATURE_BT_GATT_CLIENT_FD_DISABLE
+ chr_info = bluetooth_gatt_client_get_characteristic_notify_info(char_handle->uuid , char_handle->instance_id);
+ if (chr_info && !is_notify) {
+ BT_INFO("Already CCCD enabled. fd %d", chr_info->notify_fd);
+
+ if (chr_info->watch_id > 0)
+ g_source_remove(chr_info->watch_id);
+
+ if (chr_info->io_channel) {
+ g_io_channel_shutdown(chr_info->io_channel, TRUE, NULL);
+ g_io_channel_unref(chr_info->io_channel);
+ }
+
+ gatt_characteristic_notify_list = g_slist_remove(gatt_characteristic_notify_list, chr_info);
+
+ g_free(chr_info);
+
+ return result;
+ }
+#endif
+
+ /* ASync Function, result expected in callback from bt-service */
+
+ BT_INIT_PARAMS();
+ BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
+
+ memset(¶m, 0x00, sizeof(bluetooth_gatt_client_char_prop_info_t));
+
+ memcpy(¶m.svc.uuid, service_handle->uuid, 16);
+ param.svc.instance_id = service_handle->instance_id;
+
+ memcpy(¶m.characteristic.uuid, char_handle->uuid, 16);
+ param.characteristic.instance_id = char_handle->instance_id;
+
+ _bt_convert_addr_string_to_type(param.device_address.addr, address);
+
+ g_array_append_vals(in_param1, ¶m, sizeof(bluetooth_gatt_client_char_prop_info_t));
+ g_array_append_vals(in_param2, &client_id, sizeof(int));
+ g_array_append_vals(in_param3, &is_notify, sizeof(gboolean));
+ g_array_append_vals(in_param4, &is_indicate, sizeof(gboolean));
+
+#ifdef TIZEN_FEATURE_BT_GATT_CLIENT_FD_DISABLE
+ result = _bt_send_request(BT_BLUEZ_SERVICE,
+ BT_GATT_WATCH_CHARACTERISTIC,
+ in_param1, in_param2, in_param3, in_param4, &out_param);
+
+ BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
+
+ BT_INFO("Result [%d]", result);
+ return result;
+#endif
+
+ GUnixFDList *out_fd_list = NULL;
+ char *svc_name = NULL;
+ char str_uuid[37];
+
+ _bt_convert_uuid_type_to_string(str_uuid, param.svc.uuid);
+ bluetooth_get_uuid_name(str_uuid, &svc_name);
+
+ _bt_convert_uuid_type_to_string(str_uuid, param.characteristic.uuid);
+
+ BT_INFO_C("### Request subscription Notify : %s [%s]", str_uuid, svc_name);
+ g_free(svc_name);
+
+ result = _bt_send_request_with_unix_fd_list(BT_BLUEZ_SERVICE, BT_GATT_WATCH_CHARACTERISTIC,
+ in_param1, in_param2, in_param3, in_param4, NULL, &out_param, &out_fd_list);
+ if (result != BLUETOOTH_ERROR_NONE) {
+ BT_ERR("Fail to get Nofify FD. result %d", result);
+ BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
+ return result;
+ }
+
+ if (is_indicate == false) {
+ if (NULL == out_fd_list) {
+ BT_ERR("out_fd_list is NULL");
+ result = BLUETOOTH_ERROR_INTERNAL;
+ } else {
+ int *fd_list_array;
+ int len = 0;
+ int mtu;
+ int fd = -1;
+
+ fd_list_array = g_unix_fd_list_steal_fds(out_fd_list, &len);
+ BT_DBG("Num fds in fd_list is : %d, fd_list[0]: %d", len, fd_list_array[0]);
+ fd = fd_list_array[0];
+ mtu = g_array_index(out_param, int, 0);
+
+ BT_INFO("Acquired characteristic Notify FD %d, mtu %d", fd, mtu);
+ chr_info = bluetooth_gatt_client_create_watch_io(fd, char_handle->instance_id, mtu, (char *)address, char_handle->uuid);
+
+ gatt_characteristic_notify_list = g_slist_append(gatt_characteristic_notify_list, chr_info);
+
+ g_free(fd_list_array);
+ g_object_unref(out_fd_list);
+ }
+ }
+
+ /*result = _bt_send_request(BT_BLUEZ_SERVICE,
+ BT_GATT_WATCH_CHARACTERISTIC,
+ in_param1, in_param2, in_param3, in_param4, &out_param);*/
+
+ BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
+
+ return result;
+}
+
+BT_EXPORT_API int bluetooth_gatt_client_read_characteristic_value(
+ const char *address,
+ bt_gatt_handle_property_t *service_handle,
+ bt_gatt_handle_property_t *char_handle)
+{
+ int result = BLUETOOTH_ERROR_NONE;
+ bt_user_info_t *user_info;
+ bluetooth_gatt_client_char_prop_info_t param;
+
+ BT_CHECK_PARAMETER(address, return);
+ BT_CHECK_PARAMETER(service_handle, return);
+ BT_CHECK_PARAMETER(char_handle, return);
+
+ user_info = _bt_get_user_data(BT_GATT_CLIENT);
+ retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
+
+ BT_INIT_PARAMS();
+ BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
+
+ /* Async Function, result expected in callback from bt-service */
+ /* Call to bt-service (sync) and send address service_handle info & char handle info */
+ memset(¶m, 0x00, sizeof(bluetooth_gatt_client_char_prop_info_t));
+
+ memcpy(¶m.svc.uuid, service_handle->uuid, 16);
+ param.svc.instance_id = service_handle->instance_id;
+
+ memcpy(¶m.characteristic.uuid, char_handle->uuid, 16);
+ param.characteristic.instance_id = char_handle->instance_id;
+
+ _bt_convert_addr_string_to_type(param.device_address.addr, address);
+
+ g_array_append_vals(in_param1, ¶m,
+ sizeof(bluetooth_gatt_client_char_prop_info_t));
+
+ result = _bt_send_request_async(BT_BLUEZ_SERVICE, BT_GATT_READ_CHARACTERISTIC,
+ in_param1, in_param2, in_param3, in_param4,
+ user_info->cb, user_info->user_data);
+
+ BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
+ return result;
+}
+
+BT_EXPORT_API int bluetooth_gatt_client_read_descriptor_value(
+ const char *address,
+ bt_gatt_handle_property_t *service_handle,
+ bt_gatt_handle_property_t *char_handle,
+ bt_gatt_handle_property_t *descriptor_handle)
+{
+ int result = BLUETOOTH_ERROR_NONE;
+ bt_user_info_t *user_info;
+ bluetooth_gatt_client_desc_prop_info_t param;
+
+ BT_CHECK_PARAMETER(address, return);
+ BT_CHECK_PARAMETER(service_handle, return);
+ BT_CHECK_PARAMETER(char_handle, return);
+ BT_CHECK_PARAMETER(descriptor_handle, return);
+
+ user_info = _bt_get_user_data(BT_GATT_CLIENT);
+ retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
+
+ BT_INIT_PARAMS();
+ BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
+
+ /* Async Function, result expected in callback from bt-service */
+ memset(¶m, 0x00, sizeof(bluetooth_gatt_client_desc_prop_info_t));
+
+ memcpy(¶m.svc.uuid, service_handle->uuid, 16);
+ param.svc.instance_id = service_handle->instance_id;
+
+ memcpy(¶m.characteristic.uuid, char_handle->uuid, 16);
+ param.characteristic.instance_id = char_handle->instance_id;
+
+ memcpy(¶m.descriptor.uuid, descriptor_handle->uuid, 16);
+ param.descriptor.instance_id = descriptor_handle->instance_id;
+
+ _bt_convert_addr_string_to_type(param.device_address.addr, address);
+
+ g_array_append_vals(in_param1, ¶m,
+ sizeof(bluetooth_gatt_client_desc_prop_info_t));
+
+ result = _bt_send_request_async(BT_BLUEZ_SERVICE, BT_GATT_READ_DESCRIPTOR_VALUE,
+ in_param1, in_param2, in_param3, in_param4,
+ user_info->cb, user_info->user_data);
+
+ BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
+ return result;
+}
+
+
+static bt_gatt_characteristic_write_info_t * bluetooth_gatt_client_get_characteristic_fd(unsigned char *handle, int id)
+{
+ GSList *l;
+ char str[37];
+
+ for (l = gatt_characteristic_write_list; l != NULL; l = l->next) {
+ bt_gatt_characteristic_write_info_t *info = l->data;
+
+ if (memcmp(info->UUID, handle, 16) == 0 && info->id == id)
+ return info;
+ }
+
+ _bt_convert_uuid_type_to_string(str, handle);
+ BT_ERR("Unable to get characteristic fd. [%s] id [ %d]", str, id);
+ return NULL;
+}
+
+static gboolean bluetooth_gatt_client_write_channel_watch_cb(GIOChannel *gio,
+ GIOCondition cond, gpointer data)
+{
+ bt_gatt_characteristic_write_info_t *chr_info = (bt_gatt_characteristic_write_info_t *)data;
+
+ if (!chr_info)
+ return FALSE;
+
+ if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) {
+ char uuid_str[37];
+ _bt_convert_uuid_type_to_string(uuid_str, chr_info->UUID);
+ BT_ERR("Error : GIOCondition %d, [%s]", cond, uuid_str);
+ g_io_channel_shutdown(gio, TRUE, NULL);
+ g_io_channel_unref(gio);
+
+ gatt_characteristic_write_list = g_slist_remove(gatt_characteristic_write_list, chr_info);
+ g_free(chr_info);
+
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+static int bluetooth_gatt_client_write_characteristics_value_to_fd(
+ int fd, const guint8 *value, int length, int mtu,
+ gpointer user_data)
+{
+ int written;
+ int att_result = BLUETOOTH_ERROR_NONE;
+ BT_CHECK_PARAMETER(value, return);
+ written = write(fd, value, length);
+ if (written != length) {
+ att_result = BLUETOOTH_ERROR_INTERNAL;
+ BT_ERR("write data failed. ret : %d ", written);
+ }
+
+ return att_result;
+}
+
+static void bluetooth_gatt_client_create_write_io_channel(int fd, unsigned char * uuid, int id, int mtu)
+{
+ bt_gatt_characteristic_write_info_t *chr_info;
+ GIOChannel *channel;
+
+ chr_info = g_malloc0(sizeof(bt_gatt_characteristic_write_info_t));
+ chr_info->write_fd = fd;
+ chr_info->id = id;
+ chr_info->mtu = mtu;
+
+ memcpy(chr_info->UUID, uuid, 16);
+ channel = g_io_channel_unix_new(fd);
+ g_io_channel_set_encoding(channel, NULL, NULL);
+ g_io_channel_set_buffered(channel, FALSE);
+ g_io_channel_set_close_on_unref(channel, TRUE);
+ g_io_channel_set_flags(channel, G_IO_FLAG_NONBLOCK, NULL);
+ g_io_add_watch(channel, (G_IO_ERR | G_IO_HUP | G_IO_NVAL),
+ bluetooth_gatt_client_write_channel_watch_cb, chr_info);
+
+ gatt_characteristic_write_list = g_slist_append(gatt_characteristic_write_list, chr_info);
+
+}
+
+BT_EXPORT_API int bluetooth_gatt_client_write_characteristic_value_by_type(
+ const char *address,
+ bt_gatt_handle_property_t *service_handle,
+ bt_gatt_handle_property_t *char_handle,
+ bluetooth_gatt_att_data_t *data,
+ bluetooth_gatt_write_type_e write_type)
+{
+ int result = BLUETOOTH_ERROR_NONE;
+ bt_user_info_t *user_info;
+ bluetooth_gatt_client_char_prop_info_t param;
+
+ BT_CHECK_PARAMETER(address, return);
+ BT_CHECK_PARAMETER(service_handle, return);
+ BT_CHECK_PARAMETER(char_handle, return);
+ BT_CHECK_PARAMETER(data, return);
+
+ /* ASync Function, result expected in callback from bt-service */
+ user_info = _bt_get_user_data(BT_GATT_CLIENT);
+ retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
+
+ BT_INIT_PARAMS();
+ BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
+
+ memset(¶m, 0x00, sizeof(bluetooth_gatt_client_char_prop_info_t));
+
+ memcpy(¶m.svc.uuid, service_handle->uuid, 16);
+ param.svc.instance_id = service_handle->instance_id;
+
+ memcpy(¶m.characteristic.uuid, char_handle->uuid, 16);
+ param.characteristic.instance_id = char_handle->instance_id;
+
+ _bt_convert_addr_string_to_type(param.device_address.addr, address);
+
+ g_array_append_vals(in_param1, ¶m, sizeof(bluetooth_gatt_client_char_prop_info_t));
+ g_array_append_vals(in_param2, data, sizeof(bluetooth_gatt_att_data_t));
+ g_array_append_vals(in_param3, &write_type, sizeof(bluetooth_gatt_write_type_e));
+
+#ifdef TIZEN_FEATURE_BT_GATT_CLIENT_FD_DISABLE
+ goto done;
+#endif
+
+ if (write_type == BLUETOOTH_GATT_TYPE_WRITE_NO_RESPONSE) {
+ int fd = -1;
+ int mtu = 0;
+ bt_gatt_characteristic_write_info_t *info;
+ info = bluetooth_gatt_client_get_characteristic_fd(char_handle->uuid, service_handle->instance_id);
+
+ if (info) {
+ fd = info->write_fd;
+ mtu = info->mtu;
+ }
+
+ if (fd < 0) {
+
+ GUnixFDList *out_fd_list = NULL;
+
+ result = _bt_send_request_with_unix_fd_list(BT_BLUEZ_SERVICE, BT_GATT_ACQUIRE_WRITE,
+ in_param1, in_param2, in_param3, in_param4, NULL, &out_param, &out_fd_list);
+
+ mtu = g_array_index(out_param, int, 0);
+
+ if (result != BLUETOOTH_ERROR_NONE) {
+ BT_ERR("Fail to get Write FD. result %d", result);
+ BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
+ return result;
+ } else if (NULL == out_fd_list) {
+ BT_ERR("out_fd_list is NULL");
+ BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
+ return BLUETOOTH_ERROR_INTERNAL;
+ } else {
+ int *fd_list_array;
+ int len = 0;
+
+ fd_list_array = g_unix_fd_list_steal_fds(out_fd_list, &len);
+ fd = fd_list_array[0];
+
+ g_free(fd_list_array);
+ g_object_unref(out_fd_list);
+ }
+ BT_INFO("Acquired characteristic Write FD %d, mtu %d", fd, mtu);
+
+ if (fd > -1) {
+ bluetooth_gatt_client_create_write_io_channel(fd, char_handle->uuid, service_handle->instance_id, mtu);
+
+ result = bluetooth_gatt_client_write_characteristics_value_to_fd(fd, data->data, data->length, mtu, NULL);
+
+ } else {
+ BT_ERR("characteristic info FD is invalid");
+ goto done;
+ }
+
+ } else {
+ result = bluetooth_gatt_client_write_characteristics_value_to_fd(fd, data->data, data->length, mtu, NULL);
+ }
+
+ } else {
+done:
+ result = _bt_send_request_async(BT_BLUEZ_SERVICE,
+ BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE,
+ in_param1, in_param2, in_param3, in_param4,
+ user_info->cb, user_info->user_data);
+ }
+
+ BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
+
+ return result;
+}
+
+BT_EXPORT_API int bluetooth_gatt_client_write_descriptor_value(
+ const char *address,
+ bt_gatt_handle_property_t *service_handle,
+ bt_gatt_handle_property_t *char_handle,
+ bt_gatt_handle_property_t *descriptor_handle,
+ bluetooth_gatt_att_data_t *data,
+ bluetooth_gatt_write_type_e write_type)
+{
+ int result = BLUETOOTH_ERROR_NONE;
+ bt_user_info_t *user_info;
+ bluetooth_gatt_client_desc_prop_info_t param;
+
+ BT_CHECK_PARAMETER(address, return);
+ BT_CHECK_PARAMETER(service_handle, return);
+ BT_CHECK_PARAMETER(char_handle, return);
+ BT_CHECK_PARAMETER(descriptor_handle, return);
+ BT_CHECK_PARAMETER(data, return);
+
+ /* Async Function, result expected in callback from bt-service */
+ user_info = _bt_get_user_data(BT_GATT_CLIENT);
+ retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
+
+ BT_INIT_PARAMS();
+ BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
+
+ memset(¶m, 0x00, sizeof(bluetooth_gatt_client_desc_prop_info_t));
+
+ memcpy(¶m.svc.uuid, service_handle->uuid, 16);
+ param.svc.instance_id = service_handle->instance_id;
+
+ memcpy(¶m.characteristic.uuid, char_handle->uuid, 16);
+ param.characteristic.instance_id = char_handle->instance_id;
+
+ memcpy(¶m.descriptor.uuid, descriptor_handle->uuid, 16);
+ param.descriptor.instance_id = descriptor_handle->instance_id;
+
+ _bt_convert_addr_string_to_type(param.device_address.addr, address);
+
+ g_array_append_vals(in_param1, ¶m, sizeof(bluetooth_gatt_client_desc_prop_info_t));
+ g_array_append_vals(in_param2, data, sizeof(bluetooth_gatt_att_data_t));
+ g_array_append_vals(in_param3, &write_type, sizeof(bluetooth_gatt_write_type_e));
+
+ result = _bt_send_request_async(BT_BLUEZ_SERVICE,
+ BT_GATT_WRITE_DESCRIPTOR_VALUE,
+ in_param1, in_param2, in_param3, in_param4,
+ user_info->cb, user_info->user_data);
+
+ BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
+
+ return result;
+}
+
+BT_EXPORT_API int bluetooth_gatt_client_set_service_change_watcher(
+ const bluetooth_device_address_t *address, gboolean enable)
+{
+ GSList *l;
+ bluetooth_device_address_t *addr = NULL;
+ char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
+ char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
+ int result = BLUETOOTH_ERROR_NONE;
+
+ BT_INIT_PARAMS();
+ BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
+
+ g_array_append_vals(in_param1, address, sizeof(bluetooth_device_address_t));
+ g_array_append_vals(in_param2, &enable, sizeof(gboolean));
+
+ _bt_convert_addr_type_to_string(device_address, (unsigned char *)address->addr);
+ _bt_convert_addr_string_to_secure_string(secure_address, device_address);
+ BT_INFO("%s watcher for [%s]", enable ? "Set":"Unset", secure_address);
+
+ if (enable == TRUE) {
+ if (_bluetooth_gatt_check_service_change_watcher_address(address)
+ == TRUE) {
+ BT_INFO("The watcher is already set");
+ goto done;
+ }
+
+ if (service_monitor_list == NULL) {
+ //_bt_register_manager_subscribe_signal(TRUE);
+
+ result = _bt_send_request(BT_BLUEZ_SERVICE,
+ BT_GATT_WATCH_SERVICE_CHANGED_INDICATION,
+ in_param1, in_param2, in_param3, in_param4, &out_param);
+ }
+
+ if (result == BLUETOOTH_ERROR_NONE) {
+ addr = g_malloc0(sizeof(bluetooth_device_address_t));
+ memcpy(addr, address, sizeof(bluetooth_device_address_t));
+
+ service_monitor_list =
+ g_slist_append(service_monitor_list, addr);
+ }
+ } else {
+ for (l = service_monitor_list; l != NULL; l = l->next) {
+ addr = l->data;
+
+ if (!memcmp(address, addr,
+ sizeof(bluetooth_device_address_t))) {
+ service_monitor_list =
+ g_slist_remove(service_monitor_list, addr);
+ g_free(addr);
+ break;
+ }
+ }
+
+ if (service_monitor_list == NULL) {
+ //_bt_register_manager_subscribe_signal(FALSE);
+ result = _bt_send_request(BT_BLUEZ_SERVICE,
+ BT_GATT_WATCH_SERVICE_CHANGED_INDICATION,
+ in_param1, in_param2, in_param3, in_param4, &out_param);
+
+ }
+ }
+
+done:
+ BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
+ return result;
+}
+
+BT_EXPORT_API int bluetooth_gatt_client_deinit(
+ int client_id)
+{
+ int result;
+ int *count;
+ bt_event_info_t *event_info;
+
+ BT_CHECK_ENABLED(return);
+
+ BT_INFO("GATT Client Deinit Client instance ID [%d]", client_id);
+
+ BT_INIT_PARAMS();
+ BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
+
+ g_array_append_vals(in_param1, &client_id, sizeof(int));
+
+ /* Unregistration MUST NOT FAIL */
+ result = _bt_send_request(BT_BLUEZ_SERVICE, BT_GATT_CLIENT_UNREGISTER,
+ in_param1, in_param2, in_param3, in_param4, &out_param);
+
+ if (result != BLUETOOTH_ERROR_NONE)
+ BT_ERR("GATT Client Unregistration failed result [%d]", result);
+
+ /* Unregister event handler if this is the only instance */
+ event_info = _bt_event_get_cb_data(BT_GATT_CLIENT_EVENT);
+
+ if (event_info) {
+ count = (int*)event_info->user_data;
+
+ BT_DBG("Total num of GATT client instances [%d]", *count);
+
+ if (*count == 1) {
+ BT_DBG("Currently only one GATT client instance, so remove it and unregister GATT client events");
+ _bt_unregister_event(BT_GATT_CLIENT_EVENT);
+ _bt_set_user_data(BT_GATT_CLIENT, NULL, NULL);
+ } else
+ *count -= 1;
+ } else
+ BT_ERR("Impossible that client is created, but no event handler is registered!!!");
+
+ BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
+ return result;
+}