#include <dlog.h>
#include <gio/gio.h>
#include <glib.h>
+#include <gio/gunixfdlist.h>
#include "bt-hal-log.h"
typedef struct {
gchar *svc_path;
bt_uuid_t svc_uuid;
+ int is_primary;
GSList *gatt_list_chars;
} hal_gattc_service_t;
return NULL;
}
+static hal_gattc_char_t* _gattc_find_char_from_uuid_for_notify(hal_gattc_service_t *gattc_svc, bt_uuid_t *char_uuid)
+{
+ DBG("+");
+
+ GSList *l;
+ hal_gattc_char_t *info = NULL;
+
+ for (l = gattc_svc->gatt_list_chars; l != NULL; l = g_slist_next(l)) {
+ info = (hal_gattc_char_t*)l->data;
+ if (info == NULL)
+ continue;
+
+ if (!memcmp(&info->chr_uuid, char_uuid, sizeof(bt_uuid_t)) &&
+ ((info->permission & HAL_GATT_CHARACTERISTIC_PROPERTY_NOTIFY) ||
+ (info->permission & HAL_GATT_CHARACTERISTIC_PROPERTY_INDICATE))) {
+ INFO("Found GATT char uuid");
+ return info;
+ }
+ }
+ return NULL;
+}
+
static hal_gattc_desc_t* _gattc_find_desc_from_uuid(hal_gattc_char_t *gattc_char, bt_uuid_t *desc_uuid)
{
DBG("+");
static hal_gattc_service_t* _hal_gatt_client_add_service(hal_gattc_server_info_t *conn_info,
- const char *uuid_str, char *object_path)
+ const char *uuid_str, char *object_path, int is_primary)
{
DBG("+");
hal_gattc_service_t *gattc_service = NULL;
gattc_service = g_malloc0(sizeof(hal_gattc_service_t));
gattc_service->svc_path = g_strdup(object_path);
_bt_hal_convert_uuid_string_to_type(gattc_service->svc_uuid.uu, uuid_str);
+ gattc_service->is_primary = is_primary;
DBG("service count[%d]", g_slist_length(conn_info->gatt_list_services));
DBG("+");
/* add the service */
- gatt_svc = _hal_gatt_client_add_service(conn_info, uuid_str, object_path);
+ gatt_svc = _hal_gatt_client_add_service(conn_info, uuid_str, object_path, is_primary);
if (gatt_svc == NULL) {
ERR("Failed to add service");
return;
return BT_STATUS_SUCCESS;
}
+
+static int __bluetooth_gatt_acquire_write_fd(const char *chr, int *fd, int *mtu)
+{
+ GDBusConnection *conn;
+ GVariantBuilder *builder = NULL;
+ guint16 offset = 0;
+ GError *err = NULL;
+ GVariant *value;
+ gint32 idx;
+ guint16 att_mtu;
+ GUnixFDList *fd_list = NULL;
+
+ conn = _bt_hal_get_system_gconn();
+ if (NULL == conn) {
+ ERR("_bt_gdbus_get_system_gconn returned NULL");
+ return BT_STATUS_FAIL;
+ }
+
+ builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
+ //val = g_variant_new("ay", builder1);
+
+ g_variant_builder_add(builder, "{sv}", "offset",
+ g_variant_new("q", offset));
+
+ value = g_dbus_connection_call_with_unix_fd_list_sync(conn, BT_HAL_BLUEZ_NAME, chr, BT_HAL_GATT_CHAR_INTERFACE,
+ "AcquireWrite", g_variant_new("(a{sv})", builder),
+ G_VARIANT_TYPE("(hq)"), G_DBUS_CALL_FLAGS_NONE, -1,
+ NULL, &fd_list, NULL, &err);
+ if (err != NULL) {
+ g_dbus_error_strip_remote_error(err);
+ ERR("Error: %s", err->message);
+ g_error_free(err);
+ g_variant_builder_unref(builder);
+ return BT_STATUS_FAIL;
+ }
+
+ g_variant_get(value, "(hq)", &idx, &att_mtu);
+ *fd = g_unix_fd_list_get(fd_list, idx, NULL);
+
+ INFO("FD is %d index is %d mtu is %d", *fd, idx, att_mtu);
+ *mtu = att_mtu;
+
+ g_object_unref(fd_list);
+ g_variant_unref(value);
+ g_variant_builder_unref(builder);
+
+ return BT_STATUS_SUCCESS;
+}
+
+static int __bluetooth_gatt_acquire_notify_fd(const char *chr, int *fd, int *mtu)
+{
+ GDBusConnection *conn;
+ GVariantBuilder *builder = NULL;
+ guint16 offset = 0;
+ GError *err = NULL;
+ GVariant *value;
+ gint32 idx, notify_fd;
+ guint16 att_mtu;
+ GUnixFDList *fd_list = NULL;
+
+ conn = _bt_hal_get_system_gconn();
+
+ if (NULL == conn) {
+ ERR("_bt_gdbus_get_system_gconn returned NULL");
+ return BT_STATUS_FAIL;
+ }
+
+
+ builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
+
+
+ g_variant_builder_add(builder, "{sv}", "offset",
+ g_variant_new("q", offset));
+
+ value = g_dbus_connection_call_with_unix_fd_list_sync(conn, BT_HAL_BLUEZ_NAME, chr, BT_HAL_GATT_CHAR_INTERFACE,
+ "AcquireNotify", g_variant_new("(a{sv})", builder),
+ G_VARIANT_TYPE("(hq)"), G_DBUS_CALL_FLAGS_NONE, -1,
+ NULL, &fd_list, NULL, &err);
+ if (err != NULL) {
+ g_dbus_error_strip_remote_error(err);
+ ERR("Error: %s", err->message);
+ g_error_free(err);
+ g_variant_builder_unref(builder);
+ return BT_STATUS_FAIL;
+ }
+
+ g_variant_get(value, "(hq)", &idx, &att_mtu);
+ notify_fd = g_unix_fd_list_get(fd_list, idx, NULL);
+ *mtu = att_mtu;
+
+ INFO("Acquired characteristic Notify fd %d, mtu %d", notify_fd, *mtu);
+
+ *fd = notify_fd;
+
+ g_object_unref(fd_list);
+ g_variant_unref(value);
+ g_variant_builder_unref(builder);
+
+ return BT_STATUS_SUCCESS;
+}
+
+
static bt_status_t _hal_write_characteristic_value(int conn_id, btgatt_srvc_id_t *srvc_id,
btgatt_gatt_id_t *char_id, int write_type, int length, int auth_req, char* value)
{
return BT_STATUS_SUCCESS;
}
+bt_status_t btif_get_acquire_write_fd(int conn_id,
+ btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id,
+ int auth_req, int *fd, int*mtu)
+{
+ CHECK_BTGATT_INIT();
+
+ DBG("+");
+
+ DBG("svc isntance id:[%d]", srvc_id->id.inst_id);
+
+
+ hal_gattc_service_t *gattc_service = NULL;
+ hal_gattc_server_info_t * conn_info = NULL;
+ hal_gattc_char_t *gattc_char = NULL;
+ char char_uuid_str[BT_HAL_UUID_STRING_LEN];
+
+ bt_gatt_characteristic_property_t write_prop = HAL_GATT_CHARACTERISTIC_PROPERTY_WRITE_NO_RESPONSE;
+ int ret = BT_STATUS_SUCCESS;
+
+ DBG("+");
+
+ /* get the connection info */
+ conn_info = __bt_find_gatt_conn_info_from_conn_id(conn_id);
+ if (NULL == conn_info) {
+ DBG("Failed to get the conn_info for conn_id[%d]", conn_id);
+ return BT_STATUS_FAIL;
+ }
+
+ /* find service */
+ gattc_service = _gattc_find_service_from_uuid(conn_info, &srvc_id->id.uuid);
+ if (NULL == gattc_service) {
+ DBG("Failed to get the gatt service");
+ return BT_STATUS_FAIL;
+ }
+
+ gattc_char = _gattc_find_char_from_uuid(gattc_service, &char_id->uuid, write_prop);
+ if (NULL == gattc_char) {
+ DBG("Failed to get the gatt char");
+ return BT_STATUS_FAIL;
+ }
+
+ DBG("char path [%s]", gattc_char->chr_path);
+ _bt_hal_convert_uuid_type_to_string(char_uuid_str, gattc_char->chr_uuid.uu);
+ DBG("char uuid [%s]", char_uuid_str);
+
+
+ ret = __bluetooth_gatt_acquire_write_fd(gattc_char->chr_path, fd, mtu);
+ if (ret != BT_STATUS_SUCCESS)
+ return ret;
+
+
+ ERR("Characterstics FD erite characterstics fd is %d", *fd);
+
+ return ret;
+}
+
+bt_status_t btif_get_acquire_notify_fd(int conn_id,
+ btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id, int *fd, int *mtu)
+{
+ CHECK_BTGATT_INIT();
+
+ DBG("+");
+
+ DBG("svc isntance id:[%d]", srvc_id->id.inst_id);
+
+
+ hal_gattc_service_t *gattc_service = NULL;
+ hal_gattc_server_info_t * conn_info = NULL;
+ hal_gattc_char_t *gattc_char = NULL;
+ char char_uuid_str[BT_HAL_UUID_STRING_LEN];
+
+ bt_gatt_characteristic_property_t write_prop = HAL_GATT_CHARACTERISTIC_PROPERTY_NOTIFY;
+ int ret = BT_STATUS_SUCCESS;
+
+ DBG("+");
+
+ /* get the connection info */
+ conn_info = __bt_find_gatt_conn_info_from_conn_id(conn_id);
+ if (NULL == conn_info) {
+ DBG("Failed to get the conn_info for conn_id[%d]", conn_id);
+ return BT_STATUS_FAIL;
+ }
+
+ /* find service */
+ gattc_service = _gattc_find_service_from_uuid(conn_info, &srvc_id->id.uuid);
+ if (NULL == gattc_service) {
+ DBG("Failed to get the gatt service");
+ return BT_STATUS_FAIL;
+ }
+
+ gattc_char = _gattc_find_char_from_uuid(gattc_service, &char_id->uuid, write_prop);
+ if (NULL == gattc_char) {
+ DBG("Failed to get the gatt char");
+ return BT_STATUS_FAIL;
+ }
+
+ DBG("char path [%s]", gattc_char->chr_path);
+ _bt_hal_convert_uuid_type_to_string(char_uuid_str, gattc_char->chr_uuid.uu);
+ DBG("char uuid [%s]", char_uuid_str);
+
+
+ ret = __bluetooth_gatt_acquire_notify_fd(gattc_char->chr_path, fd, mtu);
+ if (ret != BT_STATUS_SUCCESS)
+ return ret;
+
+
+ ERR("Characterstics FD write characterstics fd is %d", *fd);
+
+ return ret;
+
+}
+
+
/** Write a remote characteristic */
bt_status_t btif_write_characteristic(int conn_id,
btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id,
/* find characteristic */
- gattc_char = _gattc_find_char_from_uuid(gattc_service, &char_id->uuid,
- HAL_GATT_CHARACTERISTIC_PROPERTY_NOTIFY);
+ gattc_char = _gattc_find_char_from_uuid_for_notify(gattc_service, &char_id->uuid);
if (NULL == gattc_char) {
DBG("Failed to get the gatt char");
return BT_STATUS_FAIL;
/* find characteristic */
- gattc_char = _gattc_find_char_from_uuid(gattc_service, &char_id->uuid,
- HAL_GATT_CHARACTERISTIC_PROPERTY_NOTIFY);
+ gattc_char = _gattc_find_char_from_uuid_for_notify(gattc_service, &char_id->uuid);
if (NULL == gattc_char) {
DBG("Failed to get the gatt char");
return BT_STATUS_FAIL;
}
const btgatt_client_interface_t btgatt_client_interface = {
- btif_gattc_register_client,
- btif_gattc_unregister_client,
- scan,
- btif_gattc_client_connect,
- btif_gattc_client_disconnect,
- refresh,
- btif_gattc_client_search_service,
- get_included_service,
- btif_gattc_get_characteristic,
- btif_gattc_get_descriptor,
- btif_read_characteristic,
- btif_write_characteristic,
- btif_read_descriptor,
- btif_write_descriptor,
- execute_write,
- btif_register_for_notification,
- btif_deregister_for_notification,
- read_remote_rssi,
- ota_fw_update,
- get_device_type,
- btif_gattc_conn_parameter_update,
- test_command,
- configure_mtu,
- scan_filter_param_setup,
- scan_filter_add_remove,
- scan_filter_clear,
- scan_filter_enable,
- set_scan_parameters,
- batchscan_cfg_storage,
- batchscan_enb_batch_scan,
- batchscan_dis_batch_scan,
- batchscan_read_reports
+ .register_client = btif_gattc_register_client,
+ .unregister_client = btif_gattc_unregister_client,
+ .scan = scan,
+ .connect = btif_gattc_client_connect,
+ .disconnect = btif_gattc_client_disconnect,
+ .refresh = refresh,
+ .search_service = btif_gattc_client_search_service,
+ .get_included_service = get_included_service,
+ .get_characteristic = btif_gattc_get_characteristic,
+ .get_descriptor = btif_gattc_get_descriptor,
+ .read_characteristic = btif_read_characteristic,
+ .write_characteristic = btif_write_characteristic,
+ .acquire_write = btif_get_acquire_write_fd,
+ .acquire_notify = btif_get_acquire_notify_fd,
+ .read_descriptor = btif_read_descriptor,
+ .write_descriptor = btif_write_descriptor,
+ .execute_write = execute_write,
+ .register_for_notification = btif_register_for_notification,
+ .deregister_for_notification = btif_deregister_for_notification,
+ .read_remote_rssi = read_remote_rssi,
+ .ota_fw_update = ota_fw_update,
+ .get_device_type = get_device_type,
+ .conn_parameter_update = btif_gattc_conn_parameter_update,
+ .test_command = test_command,
+ .configure_mtu = configure_mtu,
+ .scan_filter_param_setup = scan_filter_param_setup,
+ .scan_filter_add_remove = scan_filter_add_remove,
+ .scan_filter_clear = scan_filter_clear,
+ .scan_filter_enable = scan_filter_enable,
+ .set_scan_parameters = set_scan_parameters,
+ .batchscan_cfg_storage = batchscan_cfg_storage,
+ .batchscan_enb_batch_scan = batchscan_enb_batch_scan,
+ .batchscan_dis_batch_scan = batchscan_dis_batch_scan,
+ .batchscan_read_reports = batchscan_read_reports
};
static hal_gattc_server_info_t *__bt_find_gatt_conn_info(bt_bdaddr_t *serv_addr)
event_cb(HAL_EV_GATT_CLIENT_SEARCH_COMPLETE, (void *)&ev, sizeof(ev));
}
+
+static void _bt_hal_send_value_changed_event(hal_gattc_server_info_t *conn_info,
+ hal_gattc_service_t *svc_info, hal_gattc_char_t *char_info,
+ char *char_value, int len)
+{
+ struct hal_ev_gatt_client_notify_changed_value ev;
+ hal_gattc_client_info_t *gattc_client = NULL;
+
+ if (!event_cb) {
+ ERR("gatt client callback not registered");
+ return;
+ }
+
+ gattc_client = __bt_find_gatt_client_info(&conn_info->bd_addr);
+ if (NULL == gattc_client) {
+ ERR("failed to get the gatt client info");
+ return ;
+ }
+
+ //send event
+ DBG("sending gatt client connected status event");
+ memset(&ev, 0, sizeof(ev));
+
+ ev.conn_id = gattc_client->conn_id;
+ ev.inst_id = conn_info->inst_id;
+ ev.is_primary = svc_info->is_primary;
+ memcpy(ev.svc_uuid, svc_info->svc_uuid.uu, sizeof(ev.svc_uuid));
+ memcpy(ev.char_uuid, char_info->chr_uuid.uu, sizeof(ev.char_uuid));
+
+ memcpy(ev.bdaddr, conn_info->bd_addr.address, BT_HAL_ADDRESS_LENGTH_MAX);
+
+ if (len > 0 && (char_value != NULL)) {
+ memcpy(ev.value, char_value, len);
+ ev.len = len;
+ }
+
+ event_cb(HAL_EV_GATT_CLIENT_NOTIFY_CHANGED_VALUE, (void *)&ev, sizeof(ev));
+}
+
+void _bt_hal_handle_gattc_value_changed_event(int result, const char *char_handle,
+ char *char_value, int len)
+{
+ char device_address[BT_HAL_ADDRESS_STRING_SIZE] = { 0 };
+ hal_gattc_server_info_t *conn_info = NULL;
+ bt_bdaddr_t bd_addr;
+ GSList *l;
+ GSList *k;
+ hal_gattc_service_t *svc_info = NULL;
+ hal_gattc_char_t *char_info = NULL;
+
+ DBG("+");
+
+ _bt_hal_convert_device_path_to_address(char_handle, device_address);
+ device_address[BT_HAL_ADDRESS_STRING_SIZE - 1] = '\0';
+ DBG("device address:[%s]", device_address);
+ DBG("char handle:[%s]", char_handle);
+
+ _bt_hal_convert_addr_string_to_type(bd_addr.address, device_address);
+ conn_info = __bt_find_gatt_conn_info(&bd_addr);
+
+ if (conn_info != NULL) {
+ //find service for notified char path
+ for (l = conn_info->gatt_list_services; l != NULL; l = g_slist_next(l)) {
+ svc_info = (hal_gattc_service_t*)l->data;
+ if (svc_info == NULL)
+ continue;
+
+ /* find characteristic object path */
+ for (k = svc_info->gatt_list_chars; k != NULL; k = g_slist_next(k)) {
+ char_info = (hal_gattc_char_t *)k->data;
+ if (char_info == NULL)
+ continue;
+
+ if (g_strcmp0(char_info->chr_path, char_handle) == 0) {
+ DBG("Found char handle[%s]", char_info->chr_path);
+
+ //send event
+ _bt_hal_send_value_changed_event(conn_info, svc_info,
+ char_info, char_value, len);
+ return;
+ }
+ }
+ }
+ }
+}