#include<unistd.h>
#include<stdint.h>
#include<stdbool.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <gio/gunixfdlist.h>
#include "bt-common.h"
/* TODO_40 : 4.0 merge - Need to check why includes bt-event-handler.h */
#define BT_GATT_ATT_UUID_LEN_MAX 50
#define BT_GATT_SERVER_DBUS_NAME_LEN_MAX 50
+static GSList *gatt_characteristic_server_notify_list = NULL;;
/* Common defintions to follow , applicable for both
GATT_DIRECT and RELAY */
#define NUMBER_OF_FLAGS 10
+
+
int bluetooth_gatt_convert_prop2string(
bt_gatt_characteristic_property_t properties,
char *char_properties[])
static struct gatt_req_info *__bt_gatt_find_request_info(guint request_id);
+
+
+typedef struct {
+ int write_fd;
+ int relpy_fd;
+ int mtu;
+ int att_hand;
+ char *path ;
+} bluetooth_gatt_acquire_notify_info_t;
+
+
+static int bluetooth_get_characteristic_fd(int att_handle , char *path)
+{
+ GSList *l;
+
+ BT_INFO("request found path [%s] att_handle [ %d]", path, att_handle);
+ for (l = gatt_characteristic_server_notify_list; l != NULL; l = l->next) {
+ bluetooth_gatt_acquire_notify_info_t *info = l->data;
+ BT_INFO(" sid [ %d]" , info->att_hand);
+ if (info->att_hand == att_handle)
+ return info->write_fd;
+ }
+ return -1;
+}
+
+static bluetooth_gatt_acquire_notify_info_t * bluetooth_get_characteristic_info_from_path(int att_handle)
+{
+ GSList *l;
+
+ BT_INFO("request found att_handle [ %d]", att_handle);
+ for (l = gatt_characteristic_server_notify_list; l != NULL; l = l->next) {
+ bluetooth_gatt_acquire_notify_info_t *info = l->data;
+ BT_INFO(" sid [ %d]" , info->att_hand);
+ if (info->att_hand == att_handle)
+ return info;
+ }
+ return NULL;
+}
+
+
+static void bluetooth_characteristic_info_free(bluetooth_gatt_acquire_notify_info_t *chr_info)
+{
+ g_free(chr_info);
+}
+
+static gboolean bluetooth_gatt_write_channel_watch_cb(GIOChannel *gio,
+ GIOCondition cond, gpointer data)
+{
+ bluetooth_gatt_acquire_notify_info_t *chr_info = (bluetooth_gatt_acquire_notify_info_t *)data;
+
+ if (!chr_info)
+ return FALSE;
+
+ if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) {
+ BT_ERR("Error : GIOCondition %d, []", cond);;
+ g_io_channel_shutdown(gio, TRUE, NULL);
+ g_io_channel_unref(gio);
+
+ gatt_characteristic_server_notify_list = g_slist_remove(gatt_characteristic_server_notify_list, chr_info);
+ bluetooth_characteristic_info_free(chr_info);
+
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+static int bluetooth_gatt_write_characteristics_value_to_fd_(
+ int fd, const guint8 *value, int length,
+ 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_INFO("write data failed %d is ", written);
+ } else
+ BT_INFO("write data %s is sucess ", value);
+
+ return att_result;
+}
+
static void __bt_gatt_close_gdbus_connection(void)
{
GError *err = NULL;
BT_CHECK_PARAMETER(param, return);
BT_CHECK_PARAMETER(att_value, return);
BT_CHECK_ENABLED(return);
- int result;
+ int result = 0 ;
+ char addr[BLUETOOTH_ADDRESS_STRING_LENGTH] ;
+ int fd = -1;
BT_INIT_PARAMS();
BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
g_array_append_vals(in_param2, param, sizeof(bluetooth_gatt_server_indication_params_t));
g_array_append_vals(in_param3, addr_hex, sizeof(bluetooth_device_address_t));
- result = _bt_send_request(BT_BLUEZ_SERVICE, BT_GATT_SERVER_SEND_INDICATION,
- in_param1, in_param2, in_param3, in_param4, &out_param);
+ _bt_convert_addr_type_to_string(addr, addr_hex->addr);
+ fd = bluetooth_get_characteristic_fd(param->atrribute_handle, addr);
+
+ if (fd > -1)
+ result = bluetooth_gatt_write_characteristics_value_to_fd_(fd, att_value->data, att_value->length, NULL);
+ else
+ result = _bt_send_request(BT_BLUEZ_SERVICE, BT_GATT_SERVER_SEND_INDICATION,
+ 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;
}
+
+static gboolean bluetooth_gatt_server_acquire_channel_write_cb(GIOChannel *gio,
+ GIOCondition cond, gpointer data)
+{
+
+ bluetooth_gatt_server_acquire_write_info_t *write_data = (bluetooth_gatt_server_acquire_write_info_t*)data;
+
+ BT_INFO("FD io write data received remote adress [%s]\n", write_data->adress);
+
+ if (cond & G_IO_IN) {
+ GIOStatus status = G_IO_STATUS_NORMAL;
+ GError *err = NULL;
+ char *buffer = NULL;
+ gsize len = 0;
+ int BUF = BLUETOOTH_GATT_ATT_DATA_LENGTH_MAX;
+
+ buffer = g_malloc0(BUF);
+
+ status = g_io_channel_read_chars(gio, buffer,
+ BUF, &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);
+
+ return FALSE;
+ }
+ g_error_free(err);
+ }
+ return FALSE;
+ }
+
+ if (len > 0) {
+
+ BT_INFO(" FD io sending value changed %s %d \n", buffer, len);
+
+
+ bluetooth_gatt_server_write_requested_info_t write_info;
+ if (len < BLUETOOTH_GATT_ATT_DATA_LENGTH_MAX)
+ memcpy(write_info.data.data, buffer, len);
+
+ write_info.length = len;
+ write_info.need_resp = false;
+ write_info.attribute_handle = write_data->attribute_handle;
+ //memcpy()
+ _bt_convert_addr_string_to_type(write_info.device_address.addr, write_data->adress);
+ write_info.connection_id = write_data->connection_id;
+ write_info.offset = write_data->offset;
+ write_info.request_id = -2;
+
+ BT_INFO("ACQUIRING EVENT \n");
+
+ bt_event_info_t *event_info;
+ event_info = _bt_event_get_cb_data(BT_GATT_SERVER_EVENT);
+
+ if (event_info) {
+
+ _bt_common_event_cb(BLUETOOTH_EVENT_GATT_SERVER_VALUE_CHANGED,
+ BLUETOOTH_ERROR_NONE, &write_info,
+ event_info->cb, event_info->user_data);
+ } else {
+ BT_ERR("eventinfo failed");
+ }
+
+
+ }
+ g_free(buffer);
+
+ return TRUE;
+ }
+
+ if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) {
+ BT_ERR("Error : GIOCondition %d, ]", cond);
+ g_io_channel_shutdown(gio, TRUE, NULL);
+ g_io_channel_unref(gio);
+
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+void bluetooth_gatt_server_send_acquire_write_response(GVariant * parameters)
+{
+ int con_id = -1;
+ int tran_id = -1;
+ int att_han = -1;
+ int pipefd[2] = {-1,};
+ int mtu = -1;
+ int offset = -1;
+ char err_msg[512] = {'\0'};
+ GIOChannel *channel = NULL;
+ char *addr = NULL;
+ int result = -1;
+
+ g_variant_get(parameters, "(iiiiii&s)",
+ &result,
+ &con_id,
+ &tran_id,
+ &att_han,
+ &mtu,
+ &offset,
+ &addr);
+
+ BT_DBG("GATT Server Acquire Write From Remote Client [%s]", addr);
+ BT_DBG("GATT ServerAcquire Conn ID: [%d]", con_id);
+ BT_DBG("GATT Server Acquire write att handle:[%d]", att_han);
+ BT_DBG("GATT Server Acquire Write Offset: [%d]", offset);
+
+
+ if (socketpair(AF_UNIX, SOCK_STREAM, 0, pipefd) < 0) {
+ strerror_r(errno, err_msg, sizeof(err_msg));
+ BT_ERR("socketpair(): %s", err_msg);
+ return ;
+ }
+
+ BT_INIT_PARAMS();
+ BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
+
+ //param1 = g_array_new(TRUE, TRUE, sizeof(gchar));
+ bluetooth_gatt_server_acquire_response_params_t data;
+ data.req_type = BLUETOOTH_GATT_REQUEST_TYPE_ACQUIRE_WRITE;
+ data.fd = pipefd[1];
+ data.mtu = mtu;
+ data.request_id = tran_id;
+
+ bluetooth_gatt_server_acquire_write_info_t *write_info = g_malloc0(sizeof(bluetooth_gatt_server_acquire_write_info_t)) ;
+
+ write_info->attribute_handle = att_han;
+ write_info->connection_id = tran_id;
+ write_info->offset = offset;
+
+ memcpy(write_info->adress, addr , BLUETOOTH_ADDRESS_STRING_LENGTH);
+
+ BT_INFO("FD read %d remote adress [%s ] \n", pipefd[0], addr);
+
+
+ channel = g_io_channel_unix_new(pipefd[0]);
+ 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_IN | G_IO_ERR | G_IO_HUP),
+ bluetooth_gatt_server_acquire_channel_write_cb, write_info);
+
+
+ GUnixFDList *fd_list = g_unix_fd_list_new();
+ GError *error = NULL;
+
+ g_unix_fd_list_append(fd_list, pipefd[1], &error);
+ g_assert_no_error(error);
+ close(pipefd[1]);
+
+ g_array_append_vals(in_param1, &data, sizeof(bluetooth_gatt_server_acquire_response_params_t));
+
+ BT_INFO("Sending event BT_GATT_SERVER_ACQURE_WRITE_RESPONSE file descriptor value [%d] [ %s],", data.fd, addr);
+
+ result = _bt_send_request_with_unix_fd_list(BT_BLUEZ_SERVICE, BT_GATT_SERVER_ACQURE_WRITE_RESPONSE,
+ in_param1, in_param2, in_param3, in_param4, fd_list, &out_param, NULL);
+
+ BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
+
+}
+
+
+
+void bluetooth_gatt_server_send_acquire_notify_response(GVariant * parameters)
+{
+ int con_id = -1;
+ int tran_id = -1;
+ int att_han = -1;
+ int pipefd[2] = {-1,};
+ int mtu = -1;
+ int offset = -1;
+ char err_msg[512] = {'\0'};
+ GIOChannel *channel = NULL;
+ int result = -1;
+ int fd = -1;
+ bluetooth_gatt_acquire_notify_info_t *chr_info;
+
+ g_variant_get(parameters, "(iiiiii)",
+ &result,
+ &con_id,
+ &tran_id,
+ &att_han,
+ &mtu,
+ &offset);
+
+ BT_DBG("GATT ServerAcquire Conn ID: [%d]", con_id);
+ BT_DBG("GATT Server Acquire notify att handle:[%d]", att_han);
+ BT_DBG("GATT Server Acquire Notify Offset: [%d]", offset);
+
+
+ if (socketpair(AF_UNIX, SOCK_STREAM, 0, pipefd) < 0) {
+ strerror_r(errno, err_msg, sizeof(err_msg));
+ BT_ERR("socketpair(): %s", err_msg);
+ return ;
+ }
+
+ fd = pipefd[0];
+
+ BT_INIT_PARAMS();
+ BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
+
+ //param1 = g_array_new(TRUE, TRUE, sizeof(gchar));
+ bluetooth_gatt_server_acquire_response_params_t data;
+ data.req_type = BLUETOOTH_GATT_REQUEST_TYPE_ACQUIRE_NOTIFY;
+ data.fd = pipefd[1];
+ data.mtu = mtu;
+ data.request_id = tran_id;
+
+ BT_INFO("FD write %d characterstics path \n", pipefd[0]);
+
+ chr_info = bluetooth_get_characteristic_info_from_path(att_han);
+ if (!chr_info) {
+ chr_info = g_malloc0(sizeof(bluetooth_gatt_acquire_notify_info_t));
+ chr_info->write_fd = fd;
+ chr_info->att_hand = att_han;
+
+ gatt_characteristic_server_notify_list = g_slist_append(gatt_characteristic_server_notify_list, chr_info);
+ } else
+ chr_info->write_fd = fd;
+
+
+ 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_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL),
+ bluetooth_gatt_write_channel_watch_cb, chr_info);
+
+
+
+
+ GUnixFDList *fd_list = g_unix_fd_list_new();
+ GError *error = NULL;
+
+ g_unix_fd_list_append(fd_list, pipefd[1], &error);
+ g_assert_no_error(error);
+ close(pipefd[1]);
+
+ g_array_append_vals(in_param1, &data, sizeof(bluetooth_gatt_server_acquire_response_params_t));
+
+ BT_INFO("Sending event BT_GATT_SERVER_ACQUIRE_NOTIFY_RESPONSE file descriptor value [%d] ", data.fd);
+
+ result = _bt_send_request_with_unix_fd_list(BT_BLUEZ_SERVICE, BT_GATT_SERVER_ACQUIRE_NOTIFY_RESPONSE,
+ in_param1, in_param2, in_param3, in_param4, fd_list, &out_param, NULL);
+
+ BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
+
+}