blob: 8f78b989c8858338e866bb3350e3fb2f0205b432 [file] [log] [blame]
/*
*
* BlueZ - Bluetooth protocol stack for Linux
*
* Copyright (C) 2006-2010 Nokia Corporation
* Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdbool.h>
#include <errno.h>
#include <dirent.h>
#include <time.h>
#include <glib.h>
#include <dbus/dbus.h>
#include "lib/bluetooth.h"
#include "lib/sdp.h"
#include "lib/sdp_lib.h"
#include "lib/uuid.h"
#include "gdbus/gdbus.h"
#include "log.h"
#include "src/shared/util.h"
#include "src/shared/att.h"
#include "src/shared/queue.h"
#include "src/shared/gatt-db.h"
#include "src/shared/gatt-client.h"
#include "src/shared/gatt-server.h"
#include "src/shared/ad.h"
#include "btio/btio.h"
#include "lib/mgmt.h"
#include "attrib/att.h"
#include "hcid.h"
#include "adapter.h"
#include "gatt-database.h"
#include "attrib/gattrib.h"
#include "device.h"
#include "gatt-client.h"
#include "profile.h"
#include "service.h"
#include "dbus-common.h"
#include "error.h"
#include "uuid-helper.h"
#include "sdp-client.h"
#include "attrib/gatt.h"
#include "agent.h"
#include "textfile.h"
#include "storage.h"
#include "attrib-server.h"
#include "eir.h"
#define IO_CAPABILITY_NOINPUTNOOUTPUT 0x03
#define DISCONNECT_TIMER 2
#define DISCOVERY_TIMER 1
#define INVALID_FLAGS 0xff
#ifndef MIN
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#endif
#define RSSI_THRESHOLD 8
#define GATT_PRIM_SVC_UUID_STR "2800"
#define GATT_SND_SVC_UUID_STR "2801"
#define GATT_INCLUDE_UUID_STR "2802"
#define GATT_CHARAC_UUID_STR "2803"
static DBusConnection *dbus_conn = NULL;
static unsigned service_state_cb_id;
struct btd_disconnect_data {
guint id;
disconnect_watch watch;
void *user_data;
GDestroyNotify destroy;
};
struct bonding_req {
DBusMessage *msg;
guint listener_id;
struct btd_device *device;
uint8_t bdaddr_type;
struct agent *agent;
struct btd_adapter_pin_cb_iter *cb_iter;
uint8_t status;
guint retry_timer;
struct timespec attempt_start_time;
long last_attempt_duration_ms;
};
typedef enum {
AUTH_TYPE_PINCODE,
AUTH_TYPE_PASSKEY,
AUTH_TYPE_CONFIRM,
AUTH_TYPE_NOTIFY_PASSKEY,
AUTH_TYPE_NOTIFY_PINCODE,
} auth_type_t;
struct authentication_req {
auth_type_t type;
struct agent *agent;
struct btd_device *device;
uint32_t passkey;
char *pincode;
gboolean secure;
};
struct browse_req {
DBusMessage *msg;
struct btd_device *device;
GSList *match_uuids;
GSList *profiles_added;
sdp_list_t *records;
int search_uuid;
int reconnect_attempt;
guint listener_id;
uint16_t sdp_flags;
};
struct included_search {
struct browse_req *req;
GSList *services;
GSList *current;
};
struct svc_callback {
unsigned int id;
guint idle_id;
struct btd_device *dev;
device_svc_cb_t func;
void *user_data;
};
/* Per-bearer (LE or BR/EDR) device state */
struct bearer_state {
bool paired;
bool bonded;
bool connected;
bool svc_resolved;
};
struct csrk_info {
uint8_t key[16];
uint32_t counter;
};
struct btd_device {
int ref_count;
bdaddr_t bdaddr;
uint8_t bdaddr_type;
char *path;
bool bredr;
bool le;
bool pending_paired; /* "Paired" waiting for SDP */
bool svc_refreshed;
GSList *svc_callbacks;
GSList *eir_uuids;
struct bt_ad *ad;
uint8_t ad_flags[1];
char name[MAX_NAME_LENGTH + 1];
char *alias;
uint32_t class;
uint16_t vendor_src;
uint16_t vendor;
uint16_t product;
uint16_t version;
uint16_t appearance;
char *modalias;
struct btd_adapter *adapter;
GSList *uuids;
GSList *primaries; /* List of primary services */
GSList *services; /* List of btd_service */
GSList *pending; /* Pending services */
GSList *watches; /* List of disconnect_data */
bool temporary;
guint disconn_timer;
guint discov_timer;
struct browse_req *browse; /* service discover request */
struct bonding_req *bonding;
struct authentication_req *authr; /* authentication request */
GSList *disconnects; /* disconnects message */
DBusMessage *connect; /* connect message */
DBusMessage *disconnect; /* disconnect message */
GAttrib *attrib;
struct bt_att *att; /* The new ATT transport */
uint16_t att_mtu; /* The ATT MTU */
unsigned int att_disconn_id;
/*
* TODO: For now, device creates and owns the client-role gatt_db, but
* this needs to be persisted in a more central place so that proper
* attribute cache support can be built.
*/
struct gatt_db *db; /* GATT db cache */
struct bt_gatt_client *client; /* GATT client instance */
struct bt_gatt_server *server; /* GATT server instance */
struct btd_gatt_client *client_dbus;
struct bearer_state bredr_state;
struct bearer_state le_state;
struct csrk_info *local_csrk;
struct csrk_info *remote_csrk;
sdp_list_t *tmp_records;
time_t bredr_seen;
time_t le_seen;
gboolean trusted;
gboolean blocked;
gboolean auto_connect;
gboolean disable_auto_connect;
gboolean general_connect;
bool legacy;
int8_t rssi;
int8_t tx_power;
GIOChannel *att_io;
guint store_id;
};
static const uint16_t uuid_list[] = {
L2CAP_UUID,
PNP_INFO_SVCLASS_ID,
PUBLIC_BROWSE_GROUP,
0
};
static int device_browse_gatt(struct btd_device *device, DBusMessage *msg);
static int device_browse_sdp(struct btd_device *device, DBusMessage *msg);
static struct bearer_state *get_state(struct btd_device *dev,
uint8_t bdaddr_type)
{
if (bdaddr_type == BDADDR_BREDR)
return &dev->bredr_state;
else
return &dev->le_state;
}
static GSList *find_service_with_profile(GSList *list, struct btd_profile *p)
{
GSList *l;
for (l = list; l != NULL; l = g_slist_next(l)) {
struct btd_service *service = l->data;
if (btd_service_get_profile(service) == p)
return l;
}
return NULL;
}
static GSList *find_service_with_state(GSList *list,
btd_service_state_t state)
{
GSList *l;
for (l = list; l != NULL; l = g_slist_next(l)) {
struct btd_service *service = l->data;
if (btd_service_get_state(service) == state)
return l;
}
return NULL;
}
static GSList *find_service_with_uuid(GSList *list, char *uuid)
{
GSList *l;
for (l = list; l != NULL; l = g_slist_next(l)) {
struct btd_service *service = l->data;
struct btd_profile *profile = btd_service_get_profile(service);
if (bt_uuid_strcmp(profile->remote_uuid, uuid) == 0)
return l;
}
return NULL;
}
static void update_technologies(GKeyFile *file, struct btd_device *dev)
{
const char *list[2];
size_t len = 0;
if (dev->bredr)
list[len++] = "BR/EDR";
if (dev->le) {
const char *type;
if (dev->bdaddr_type == BDADDR_LE_PUBLIC)
type = "public";
else
type = "static";
g_key_file_set_string(file, "General", "AddressType", type);
list[len++] = "LE";
}
g_key_file_set_string_list(file, "General", "SupportedTechnologies",
list, len);
}
static void store_csrk(struct csrk_info *csrk, GKeyFile *key_file,
const char *group)
{
char key[33];
int i;
for (i = 0; i < 16; i++)
sprintf(key + (i * 2), "%2.2X", csrk->key[i]);
g_key_file_set_string(key_file, group, "Key", key);
g_key_file_set_integer(key_file, group, "Counter", csrk->counter);
}
static gboolean store_device_info_cb(gpointer user_data)
{
struct btd_device *device = user_data;
GKeyFile *key_file;
char filename[PATH_MAX];
char adapter_addr[18];
char device_addr[18];
char *str;
char class[9];
char **uuids = NULL;
gsize length = 0;
device->store_id = 0;
ba2str(btd_adapter_get_address(device->adapter), adapter_addr);
ba2str(&device->bdaddr, device_addr);
snprintf(filename, PATH_MAX, STORAGEDIR "/%s/%s/info", adapter_addr,
device_addr);
key_file = g_key_file_new();
g_key_file_load_from_file(key_file, filename, 0, NULL);
g_key_file_set_string(key_file, "General", "Name", device->name);
if (device->alias != NULL)
g_key_file_set_string(key_file, "General", "Alias",
device->alias);
else
g_key_file_remove_key(key_file, "General", "Alias", NULL);
if (device->class) {
sprintf(class, "0x%6.6x", device->class);
g_key_file_set_string(key_file, "General", "Class", class);
} else {
g_key_file_remove_key(key_file, "General", "Class", NULL);
}
if (device->appearance) {
sprintf(class, "0x%4.4x", device->appearance);
g_key_file_set_string(key_file, "General", "Appearance", class);
} else {
g_key_file_remove_key(key_file, "General", "Appearance", NULL);
}
update_technologies(key_file, device);
g_key_file_set_boolean(key_file, "General", "Trusted",
device->trusted);
g_key_file_set_boolean(key_file, "General", "Blocked",
device->blocked);
if (device->uuids) {
GSList *l;
int i;
uuids = g_new0(char *, g_slist_length(device->uuids) + 1);
for (i = 0, l = device->uuids; l; l = g_slist_next(l), i++)
uuids[i] = l->data;
g_key_file_set_string_list(key_file, "General", "Services",
(const char **)uuids, i);
} else {
g_key_file_remove_key(key_file, "General", "Services", NULL);
}
if (device->vendor_src) {
g_key_file_set_integer(key_file, "DeviceID", "Source",
device->vendor_src);
g_key_file_set_integer(key_file, "DeviceID", "Vendor",
device->vendor);
g_key_file_set_integer(key_file, "DeviceID", "Product",
device->product);
g_key_file_set_integer(key_file, "DeviceID", "Version",
device->version);
} else {
g_key_file_remove_group(key_file, "DeviceID", NULL);
}
if (device->local_csrk)
store_csrk(device->local_csrk, key_file, "LocalSignatureKey");
if (device->remote_csrk)
store_csrk(device->remote_csrk, key_file, "RemoteSignatureKey");
create_file(filename, S_IRUSR | S_IWUSR);
str = g_key_file_to_data(key_file, &length, NULL);
g_file_set_contents(filename, str, length, NULL);
g_free(str);
g_key_file_free(key_file);
g_free(uuids);
return FALSE;
}
static bool device_address_is_private(struct btd_device *dev)
{
if (dev->bdaddr_type != BDADDR_LE_RANDOM)
return false;
switch (dev->bdaddr.b[5] >> 6) {
case 0x00: /* Private non-resolvable */
case 0x01: /* Private resolvable */
return true;
default:
return false;
}
}
static void store_device_info(struct btd_device *device)
{
if (device->temporary || device->store_id > 0)
return;
if (device_address_is_private(device)) {
warn("Can't store info for private addressed device %s",
device->path);
return;
}
device->store_id = g_idle_add(store_device_info_cb, device);
}
void device_store_cached_name(struct btd_device *dev, const char *name)
{
char filename[PATH_MAX];
char s_addr[18], d_addr[18];
GKeyFile *key_file;
char *data;
gsize length = 0;
if (device_address_is_private(dev)) {
warn("Can't store name for private addressed device %s",
dev->path);
return;
}
ba2str(btd_adapter_get_address(dev->adapter), s_addr);
ba2str(&dev->bdaddr, d_addr);
snprintf(filename, PATH_MAX, STORAGEDIR "/%s/cache/%s", s_addr, d_addr);
create_file(filename, S_IRUSR | S_IWUSR);
key_file = g_key_file_new();
g_key_file_load_from_file(key_file, filename, 0, NULL);
g_key_file_set_string(key_file, "General", "Name", name);
data = g_key_file_to_data(key_file, &length, NULL);
g_file_set_contents(filename, data, length, NULL);
g_free(data);
g_key_file_free(key_file);
}
static void browse_request_free(struct browse_req *req)
{
if (req->listener_id)
g_dbus_remove_watch(dbus_conn, req->listener_id);
if (req->msg)
dbus_message_unref(req->msg);
g_slist_free_full(req->profiles_added, g_free);
if (req->records)
sdp_list_free(req->records, (sdp_free_func_t) sdp_record_free);
g_free(req);
}
static void gatt_client_cleanup(struct btd_device *device)
{
if (!device->client)
return;
bt_gatt_client_set_service_changed(device->client, NULL, NULL, NULL);
bt_gatt_client_set_ready_handler(device->client, NULL, NULL, NULL);
bt_gatt_client_unref(device->client);
device->client = NULL;
}
static void gatt_server_cleanup(struct btd_device *device)
{
if (!device->server)
return;
bt_gatt_server_unref(device->server);
device->server = NULL;
}
static void attio_cleanup(struct btd_device *device)
{
if (device->att_disconn_id)
bt_att_unregister_disconnect(device->att,
device->att_disconn_id);
if (device->att_io) {
g_io_channel_shutdown(device->att_io, FALSE, NULL);
g_io_channel_unref(device->att_io);
device->att_io = NULL;
}
gatt_client_cleanup(device);
gatt_server_cleanup(device);
if (device->att) {
bt_att_unref(device->att);
device->att = NULL;
}
if (device->attrib) {
GAttrib *attrib = device->attrib;
device->attrib = NULL;
g_attrib_cancel_all(attrib);
g_attrib_unref(attrib);
}
}
static void browse_request_cancel(struct browse_req *req)
{
struct btd_device *device = req->device;
struct btd_adapter *adapter = device->adapter;
DBG("");
bt_cancel_discovery(btd_adapter_get_address(adapter), &device->bdaddr);
attio_cleanup(device);
device->browse = NULL;
browse_request_free(req);
}
static void svc_dev_remove(gpointer user_data)
{
struct svc_callback *cb = user_data;
if (cb->idle_id > 0)
g_source_remove(cb->idle_id);
cb->func(cb->dev, -ENODEV, cb->user_data);
g_free(cb);
}
static void device_free(gpointer user_data)
{
struct btd_device *device = user_data;
btd_gatt_client_destroy(device->client_dbus);
device->client_dbus = NULL;
g_slist_free_full(device->uuids, g_free);
g_slist_free_full(device->primaries, g_free);
g_slist_free_full(device->svc_callbacks, svc_dev_remove);
/* Reset callbacks since the device is going to be freed */
gatt_db_register(device->db, NULL, NULL, NULL, NULL);
attio_cleanup(device);
gatt_db_unref(device->db);
bt_ad_unref(device->ad);
if (device->tmp_records)
sdp_list_free(device->tmp_records,
(sdp_free_func_t) sdp_record_free);
if (device->disconn_timer)
g_source_remove(device->disconn_timer);
if (device->discov_timer)
g_source_remove(device->discov_timer);
if (device->connect)
dbus_message_unref(device->connect);
if (device->disconnect)
dbus_message_unref(device->disconnect);
DBG("%p", device);
if (device->authr) {
if (device->authr->agent)
agent_unref(device->authr->agent);
g_free(device->authr->pincode);
g_free(device->authr);
}
if (device->eir_uuids)
g_slist_free_full(device->eir_uuids, g_free);
g_free(device->local_csrk);
g_free(device->remote_csrk);
g_free(device->path);
g_free(device->alias);
free(device->modalias);
g_free(device);
}
bool device_is_paired(struct btd_device *device, uint8_t bdaddr_type)
{
struct bearer_state *state = get_state(device, bdaddr_type);
return state->paired;
}
bool device_is_bonded(struct btd_device *device, uint8_t bdaddr_type)
{
struct bearer_state *state = get_state(device, bdaddr_type);
return state->bonded;
}
gboolean device_is_trusted(struct btd_device *device)
{
return device->trusted;
}
static gboolean dev_property_get_address(const GDBusPropertyTable *property,
DBusMessageIter *iter, void *data)
{
struct btd_device *device = data;
char dstaddr[18];
const char *ptr = dstaddr;
ba2str(&device->bdaddr, dstaddr);
dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &ptr);
return TRUE;
}
static gboolean dev_property_get_name(const GDBusPropertyTable *property,
DBusMessageIter *iter, void *data)
{
struct btd_device *device = data;
const char *ptr = device->name;
dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &ptr);
return TRUE;
}
static gboolean dev_property_exists_name(const GDBusPropertyTable *property,
void *data)
{
struct btd_device *dev = data;
return device_name_known(dev);
}
static gboolean dev_property_get_alias(const GDBusPropertyTable *property,
DBusMessageIter *iter, void *data)
{
struct btd_device *device = data;
char dstaddr[18];
const char *ptr;
/* Alias (fallback to name or address) */
if (device->alias != NULL)
ptr = device->alias;
else if (strlen(device->name) > 0) {
ptr = device->name;
} else {
ba2str(&device->bdaddr, dstaddr);
g_strdelimit(dstaddr, ":", '-');
ptr = dstaddr;
}
dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &ptr);
return TRUE;
}
static void set_alias(GDBusPendingPropertySet id, const char *alias,
void *data)
{
struct btd_device *device = data;
/* No change */
if ((device->alias == NULL && g_str_equal(alias, "")) ||
g_strcmp0(device->alias, alias) == 0) {
g_dbus_pending_property_success(id);
return;
}
g_free(device->alias);
device->alias = g_str_equal(alias, "") ? NULL : g_strdup(alias);
store_device_info(device);
g_dbus_emit_property_changed(dbus_conn, device->path,
DEVICE_INTERFACE, "Alias");
g_dbus_pending_property_success(id);
}
static void dev_property_set_alias(const GDBusPropertyTable *property,
DBusMessageIter *value,
GDBusPendingPropertySet id, void *data)
{
const char *alias;
if (dbus_message_iter_get_arg_type(value) != DBUS_TYPE_STRING) {
g_dbus_pending_property_error(id,
ERROR_INTERFACE ".InvalidArguments",
"Invalid arguments in method call");
return;
}
dbus_message_iter_get_basic(value, &alias);
set_alias(id, alias, data);
}
static gboolean dev_property_exists_class(const GDBusPropertyTable *property,
void *data)
{
struct btd_device *device = data;
return device->class != 0;
}
static gboolean dev_property_get_class(const GDBusPropertyTable *property,
DBusMessageIter *iter, void *data)
{
struct btd_device *device = data;
if (device->class == 0)
return FALSE;
dbus_message_iter_append_basic(iter, DBUS_TYPE_UINT32, &device->class);
return TRUE;
}
static gboolean get_appearance(const GDBusPropertyTable *property, void *data,
uint16_t *appearance)
{
struct btd_device *device = data;
if (dev_property_exists_class(property, data))
return FALSE;
if (device->appearance) {
*appearance = device->appearance;
return TRUE;
}
return FALSE;
}
static gboolean dev_property_exists_appearance(
const GDBusPropertyTable *property, void *data)
{
uint16_t appearance;
return get_appearance(property, data, &appearance);
}
static gboolean dev_property_get_appearance(const GDBusPropertyTable *property,
DBusMessageIter *iter, void *data)
{
uint16_t appearance;
if (!get_appearance(property, data, &appearance))
return FALSE;
dbus_message_iter_append_basic(iter, DBUS_TYPE_UINT16, &appearance);
return TRUE;
}
static const char *get_icon(const GDBusPropertyTable *property, void *data)
{
struct btd_device *device = data;
const char *icon = NULL;
uint16_t appearance;
if (device->class != 0)
icon = class_to_icon(device->class);
else if (get_appearance(property, data, &appearance))
icon = gap_appearance_to_icon(appearance);
return icon;
}
static gboolean dev_property_exists_icon(
const GDBusPropertyTable *property, void *data)
{
return get_icon(property, data) != NULL;
}
static gboolean dev_property_get_icon(const GDBusPropertyTable *property,
DBusMessageIter *iter, void *data)
{
const char *icon;
icon = get_icon(property, data);
if (icon == NULL)
return FALSE;
dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &icon);
return TRUE;
}
static gboolean dev_property_get_paired(const GDBusPropertyTable *property,
DBusMessageIter *iter, void *data)
{
struct btd_device *dev = data;
dbus_bool_t val;
if (dev->bredr_state.paired || dev->le_state.paired)
val = TRUE;
else
val = FALSE;
dbus_message_iter_append_basic(iter, DBUS_TYPE_BOOLEAN, &val);
return TRUE;
}
static gboolean dev_property_get_legacy(const GDBusPropertyTable *property,
DBusMessageIter *iter, void *data)
{
struct btd_device *device = data;
dbus_bool_t val = device->legacy;
dbus_message_iter_append_basic(iter, DBUS_TYPE_BOOLEAN, &val);
return TRUE;
}
static gboolean dev_property_get_rssi(const GDBusPropertyTable *property,
DBusMessageIter *iter, void *data)
{
struct btd_device *dev = data;
dbus_int16_t val = dev->rssi;
dbus_message_iter_append_basic(iter, DBUS_TYPE_INT16, &val);
return TRUE;
}
static gboolean dev_property_exists_rssi(const GDBusPropertyTable *property,
void *data)
{
struct btd_device *dev = data;
if (dev->rssi == 0)
return FALSE;
return TRUE;
}
static gboolean dev_property_get_tx_power(const GDBusPropertyTable *property,
DBusMessageIter *iter, void *data)
{
struct btd_device *dev = data;
dbus_int16_t val = dev->tx_power;
dbus_message_iter_append_basic(iter, DBUS_TYPE_INT16, &val);
return TRUE;
}
static gboolean dev_property_exists_tx_power(const GDBusPropertyTable *property,
void *data)
{
struct btd_device *dev = data;
if (dev->tx_power == 127)
return FALSE;
return TRUE;
}
static gboolean
dev_property_get_svc_resolved(const GDBusPropertyTable *property,
DBusMessageIter *iter, void *data)
{
struct btd_device *device = data;
gboolean val = device->svc_refreshed;
dbus_message_iter_append_basic(iter, DBUS_TYPE_BOOLEAN, &val);
return TRUE;
}
static gboolean dev_property_flags_exist(const GDBusPropertyTable *property,
void *data)
{
struct btd_device *device = data;
return device->ad_flags[0] != INVALID_FLAGS;
}
static gboolean
dev_property_get_flags(const GDBusPropertyTable *property,
DBusMessageIter *iter, void *data)
{
struct btd_device *device = data;
uint8_t *flags = device->ad_flags;
DBusMessageIter array;
dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
DBUS_TYPE_BYTE_AS_STRING, &array);
dbus_message_iter_append_fixed_array(&array, DBUS_TYPE_BYTE,
&flags, sizeof(device->ad_flags));
dbus_message_iter_close_container(iter, &array);
return TRUE;
}
static gboolean dev_property_get_trusted(const GDBusPropertyTable *property,
DBusMessageIter *iter, void *data)
{
struct btd_device *device = data;
gboolean val = device_is_trusted(device);
dbus_message_iter_append_basic(iter, DBUS_TYPE_BOOLEAN, &val);
return TRUE;
}
static void set_trust(GDBusPendingPropertySet id, gboolean value, void *data)
{
struct btd_device *device = data;
btd_device_set_trusted(device, value);
g_dbus_pending_property_success(id);
}
static void dev_property_set_trusted(const GDBusPropertyTable *property,
DBusMessageIter *value,
GDBusPendingPropertySet id, void *data)
{
dbus_bool_t b;
if (dbus_message_iter_get_arg_type(value) != DBUS_TYPE_BOOLEAN) {
g_dbus_pending_property_error(id,
ERROR_INTERFACE ".InvalidArguments",
"Invalid arguments in method call");
return;
}
dbus_message_iter_get_basic(value, &b);
set_trust(id, b, data);
}
static gboolean dev_property_get_blocked(const GDBusPropertyTable *property,
DBusMessageIter *iter, void *data)
{
struct btd_device *device = data;
dbus_message_iter_append_basic(iter, DBUS_TYPE_BOOLEAN,
&device->blocked);
return TRUE;
}
static void set_blocked(GDBusPendingPropertySet id, gboolean value, void *data)
{
struct btd_device *device = data;
int err;
if (value)
err = device_block(device, FALSE);
else
err = device_unblock(device, FALSE, FALSE);
switch (-err) {
case 0:
g_dbus_pending_property_success(id);
break;
case EINVAL:
g_dbus_pending_property_error(id, ERROR_INTERFACE ".Failed",
"Kernel lacks blacklist support");
break;
default:
g_dbus_pending_property_error(id, ERROR_INTERFACE ".Failed",
strerror(-err));
break;
}
}
static void dev_property_set_blocked(const GDBusPropertyTable *property,
DBusMessageIter *value,
GDBusPendingPropertySet id, void *data)
{
dbus_bool_t b;
if (dbus_message_iter_get_arg_type(value) != DBUS_TYPE_BOOLEAN) {
g_dbus_pending_property_error(id,
ERROR_INTERFACE ".InvalidArguments",
"Invalid arguments in method call");
return;
}
dbus_message_iter_get_basic(value, &b);
set_blocked(id, b, data);
}
static gboolean dev_property_get_connected(const GDBusPropertyTable *property,
DBusMessageIter *iter, void *data)
{
struct btd_device *dev = data;
dbus_bool_t connected;
if (dev->bredr_state.connected || dev->le_state.connected)
connected = TRUE;
else
connected = FALSE;
dbus_message_iter_append_basic(iter, DBUS_TYPE_BOOLEAN, &connected);
return TRUE;
}
static gboolean dev_property_get_uuids(const GDBusPropertyTable *property,
DBusMessageIter *iter, void *data)
{
struct btd_device *dev = data;
DBusMessageIter entry;
GSList *l;
dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
DBUS_TYPE_STRING_AS_STRING, &entry);
if (dev->bredr_state.svc_resolved || dev->le_state.svc_resolved)
l = dev->uuids;
else if (dev->eir_uuids)
l = dev->eir_uuids;
else
l = dev->uuids;
for (; l != NULL; l = l->next)
dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING,
&l->data);
dbus_message_iter_close_container(iter, &entry);
return TRUE;
}
static gboolean dev_property_get_modalias(const GDBusPropertyTable *property,
DBusMessageIter *iter, void *data)
{
struct btd_device *device = data;
if (!device->modalias)
return FALSE;
dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
&device->modalias);
return TRUE;
}
static gboolean dev_property_exists_modalias(const GDBusPropertyTable *property,
void *data)
{
struct btd_device *device = data;
return device->modalias ? TRUE : FALSE;
}
static gboolean dev_property_get_adapter(const GDBusPropertyTable *property,
DBusMessageIter *iter, void *data)
{
struct btd_device *device = data;
const char *str = adapter_get_path(device->adapter);
dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &str);
return TRUE;
}
static void append_manufacturer_data(void *data, void *user_data)
{
struct bt_ad_manufacturer_data *md = data;
DBusMessageIter *dict = user_data;
dict_append_basic_array(dict, DBUS_TYPE_UINT16, &md->manufacturer_id,
DBUS_TYPE_BYTE, &md->data, md->len);
}
static gboolean
dev_property_get_manufacturer_data(const GDBusPropertyTable *property,
DBusMessageIter *iter, void *data)
{
struct btd_device *device = data;
DBusMessageIter dict;
dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
DBUS_TYPE_UINT16_AS_STRING
DBUS_TYPE_VARIANT_AS_STRING
DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
&dict);
bt_ad_foreach_manufacturer_data(device->ad, append_manufacturer_data,
&dict);
dbus_message_iter_close_container(iter, &dict);
return TRUE;
}
static gboolean
dev_property_manufacturer_data_exist(const GDBusPropertyTable *property,
void *data)
{
struct btd_device *device = data;
return bt_ad_has_manufacturer_data(device->ad, NULL);
}
static void append_service_data(void *data, void *user_data)
{
struct bt_ad_service_data *sd = data;
DBusMessageIter *dict = user_data;
char uuid_str[MAX_LEN_UUID_STR];
bt_uuid_to_string(&sd->uuid, uuid_str, sizeof(uuid_str));
dict_append_array(dict, uuid_str, DBUS_TYPE_BYTE, &sd->data, sd->len);
}
static gboolean
dev_property_get_service_data(const GDBusPropertyTable *property,
DBusMessageIter *iter, void *data)
{
struct btd_device *device = data;
DBusMessageIter dict;
dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
DBUS_TYPE_STRING_AS_STRING
DBUS_TYPE_VARIANT_AS_STRING
DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
&dict);
bt_ad_foreach_service_data(device->ad, append_service_data, &dict);
dbus_message_iter_close_container(iter, &dict);
return TRUE;
}
static gboolean
dev_property_service_data_exist(const GDBusPropertyTable *property,
void *data)
{
struct btd_device *device = data;
return bt_ad_has_service_data(device->ad, NULL);
}
static gboolean disconnect_all(gpointer user_data)
{
struct btd_device *device = user_data;
device->disconn_timer = 0;
if (device->bredr_state.connected)
btd_adapter_disconnect_device(device->adapter, &device->bdaddr,
BDADDR_BREDR);
if (device->le_state.connected)
btd_adapter_disconnect_device(device->adapter, &device->bdaddr,
device->bdaddr_type);
return FALSE;
}
int device_block(struct btd_device *device, gboolean update_only)
{
int err = 0;
if (device->blocked)
return 0;
if (device->disconn_timer > 0)
g_source_remove(device->disconn_timer);
disconnect_all(device);
while (device->services != NULL) {
struct btd_service *service = device->services->data;
device->services = g_slist_remove(device->services, service);
service_remove(service);
}
if (!update_only) {
if (device->le)
err = btd_adapter_block_address(device->adapter,
&device->bdaddr,
device->bdaddr_type);
if (!err && device->bredr)
err = btd_adapter_block_address(device->adapter,
&device->bdaddr,
BDADDR_BREDR);
}
if (err < 0)
return err;
device->blocked = TRUE;
store_device_info(device);
btd_device_set_temporary(device, false);
g_dbus_emit_property_changed(dbus_conn, device->path,
DEVICE_INTERFACE, "Blocked");
return 0;
}
int device_unblock(struct btd_device *device, gboolean silent,
gboolean update_only)
{
int err = 0;
if (!device->blocked)
return 0;
if (!update_only) {
if (device->le)
err = btd_adapter_unblock_address(device->adapter,
&device->bdaddr,
device->bdaddr_type);
if (!err && device->bredr)
err = btd_adapter_unblock_address(device->adapter,
&device->bdaddr,
BDADDR_BREDR);
}
if (err < 0)
return err;
device->blocked = FALSE;
store_device_info(device);
if (!silent) {
g_dbus_emit_property_changed(dbus_conn, device->path,
DEVICE_INTERFACE, "Blocked");
device_probe_profiles(device, device->uuids);
}
return 0;
}
static void browse_request_exit(DBusConnection *conn, void *user_data)
{
struct browse_req *req = user_data;
DBG("Requestor exited");
browse_request_cancel(req);
}
static void bonding_request_cancel(struct bonding_req *bonding)
{
struct btd_device *device = bonding->device;
struct btd_adapter *adapter = device->adapter;
adapter_cancel_bonding(adapter, &device->bdaddr, device->bdaddr_type);
}
static void dev_disconn_service(gpointer a, gpointer b)
{
btd_service_disconnect(a);
}
void device_request_disconnect(struct btd_device *device, DBusMessage *msg)
{
if (device->bonding)
bonding_request_cancel(device->bonding);
if (device->browse)
browse_request_cancel(device->browse);
if (device->att_io) {
g_io_channel_shutdown(device->att_io, FALSE, NULL);
g_io_channel_unref(device->att_io);
device->att_io = NULL;
}
if (device->connect) {
DBusMessage *reply = btd_error_failed(device->connect,
"Cancelled");
g_dbus_send_message(dbus_conn, reply);
dbus_message_unref(device->connect);
device->connect = NULL;
}
if (btd_device_is_connected(device) && msg)
device->disconnects = g_slist_append(device->disconnects,
dbus_message_ref(msg));
if (device->disconn_timer)
return;
g_slist_foreach(device->services, dev_disconn_service, NULL);
g_slist_free(device->pending);
device->pending = NULL;
while (device->watches) {
struct btd_disconnect_data *data = device->watches->data;
if (data->watch)
/* temporary is set if device is going to be removed */
data->watch(device, device->temporary,
data->user_data);
/* Check if the watch has been removed by callback function */
if (!g_slist_find(device->watches, data))
continue;
device->watches = g_slist_remove(device->watches, data);
g_free(data);
}
if (!btd_device_is_connected(device)) {
if (msg)
g_dbus_send_reply(dbus_conn, msg, DBUS_TYPE_INVALID);
return;
}
device->disconn_timer = g_timeout_add_seconds(DISCONNECT_TIMER,
disconnect_all,
device);
}
bool device_is_disconnecting(struct btd_device *device)
{
return device->disconn_timer > 0;
}
static void device_set_auto_connect(struct btd_device *device, gboolean enable)
{
char addr[18];
if (!device || !device->le)
return;
ba2str(&device->bdaddr, addr);
DBG("%s auto connect: %d", addr, enable);
if (device->auto_connect == enable)
return;
device->auto_connect = enable;
/* Disabling auto connect */
if (enable == FALSE) {
adapter_connect_list_remove(device->adapter, device);
adapter_auto_connect_remove(device->adapter, device);
return;
}
/* Enabling auto connect */
adapter_auto_connect_add(device->adapter, device);
if (device->attrib) {
DBG("Already connected");
return;
}
adapter_connect_list_add(device->adapter, device);
}
static DBusMessage *dev_disconnect(DBusConnection *conn, DBusMessage *msg,
void *user_data)
{
struct btd_device *device = user_data;
/*
* Disable connections through passive scanning until
* Device1.Connect is called
*/
if (device->auto_connect) {
device->disable_auto_connect = TRUE;
device_set_auto_connect(device, FALSE);
}
device_request_disconnect(device, msg);
return NULL;
}
static int connect_next(struct btd_device *dev)
{
struct btd_service *service;
int err = -ENOENT;
while (dev->pending) {
service = dev->pending->data;
err = btd_service_connect(service);
if (!err)
return 0;
dev->pending = g_slist_delete_link(dev->pending, dev->pending);
}
return err;
}
static void device_profile_connected(struct btd_device *dev,
struct btd_profile *profile, int err)
{
struct btd_service *pending;
GSList *l;
DBG("%s %s (%d)", profile->name, strerror(-err), -err);
if (!err)
btd_device_set_temporary(dev, false);
if (dev->pending == NULL)
goto done;
if (!btd_device_is_connected(dev)) {
switch (-err) {
case EHOSTDOWN: /* page timeout */
case EHOSTUNREACH: /* adapter not powered */
case ECONNABORTED: /* adapter powered down */
goto done;
}
}
pending = dev->pending->data;
l = find_service_with_profile(dev->pending, profile);
if (l != NULL)
dev->pending = g_slist_delete_link(dev->pending, l);
/* Only continue connecting the next profile if it matches the first
* pending, otherwise it will trigger another connect to the same
* profile
*/
if (profile != btd_service_get_profile(pending))
return;
if (connect_next(dev) == 0)
return;
done:
g_slist_free(dev->pending);
dev->pending = NULL;
if (!dev->connect)
return;
if (!err && dbus_message_is_method_call(dev->connect, DEVICE_INTERFACE,
"Connect"))
dev->general_connect = TRUE;
DBG("returning response to %s", dbus_message_get_sender(dev->connect));
l = find_service_with_state(dev->services, BTD_SERVICE_STATE_CONNECTED);
if (err && l == NULL)
g_dbus_send_message(dbus_conn,
btd_error_failed(dev->connect, strerror(-err)));
else {
/* Start passive SDP discovery to update known services */
if (dev->bredr && !dev->svc_refreshed)
device_browse_sdp(dev, NULL);
g_dbus_send_reply(dbus_conn, dev->connect, DBUS_TYPE_INVALID);
}
dbus_message_unref(dev->connect);
dev->connect = NULL;
}
void device_add_eir_uuids(struct btd_device *dev, GSList *uuids)
{
GSList *l;
bool added = false;
if (dev->bredr_state.svc_resolved || dev->le_state.svc_resolved)
return;
for (l = uuids; l != NULL; l = l->next) {
const char *str = l->data;
if (g_slist_find_custom(dev->eir_uuids, str, bt_uuid_strcmp))
continue;
added = true;
dev->eir_uuids = g_slist_append(dev->eir_uuids, g_strdup(str));
}
if (added)
g_dbus_emit_property_changed(dbus_conn, dev->path,
DEVICE_INTERFACE, "UUIDs");
}
static void add_manufacturer_data(void *data, void *user_data)
{
struct eir_msd *msd = data;
struct btd_device *dev = user_data;
if (!bt_ad_add_manufacturer_data(dev->ad, msd->company, msd->data,
msd->data_len))
return;
g_dbus_emit_property_changed(dbus_conn, dev->path,
DEVICE_INTERFACE, "ManufacturerData");
}
void device_set_manufacturer_data(struct btd_device *dev, GSList *list)
{
g_slist_foreach(list, add_manufacturer_data, dev);
}
static void add_service_data(void *data, void *user_data)
{
struct eir_sd *sd = data;
struct btd_device *dev = user_data;
bt_uuid_t uuid;
if (bt_string_to_uuid(&uuid, sd->uuid) < 0)
return;
if (!bt_ad_add_service_data(dev->ad, &uuid, sd->data, sd->data_len))
return;
g_dbus_emit_property_changed(dbus_conn, dev->path,
DEVICE_INTERFACE, "ServiceData");
}
void device_set_service_data(struct btd_device *dev, GSList *list)
{
g_slist_foreach(list, add_service_data, dev);
}
static struct btd_service *find_connectable_service(struct btd_device *dev,
const char *uuid)
{
GSList *l;
for (l = dev->services; l != NULL; l = g_slist_next(l)) {
struct btd_service *service = l->data;
struct btd_profile *p = btd_service_get_profile(service);
if (!p->connect || !p->remote_uuid)
continue;
if (strcasecmp(uuid, p->remote_uuid) == 0)
return service;
}
return NULL;
}
static int service_prio_cmp(gconstpointer a, gconstpointer b)
{
struct btd_profile *p1 = btd_service_get_profile(a);
struct btd_profile *p2 = btd_service_get_profile(b);
return p2->priority - p1->priority;
}
static GSList *create_pending_list(struct btd_device *dev, const char *uuid)
{
struct btd_service *service;
struct btd_profile *p;
GSList *l;
if (uuid) {
service = find_connectable_service(dev, uuid);
if (service)
return g_slist_prepend(dev->pending, service);
return dev->pending;
}
for (l = dev->services; l != NULL; l = g_slist_next(l)) {
service = l->data;
p = btd_service_get_profile(service);
if (!p->auto_connect)
continue;
if (g_slist_find(dev->pending, service))
continue;
if (btd_service_get_state(service) !=
BTD_SERVICE_STATE_DISCONNECTED)
continue;
dev->pending = g_slist_insert_sorted(dev->pending, service,
service_prio_cmp);
}
return dev->pending;
}
int btd_device_connect_services(struct btd_device *dev, GSList *services)
{
GSList *l;
if (dev->pending || dev->connect || dev->browse)
return -EBUSY;
if (!btd_adapter_get_powered(dev->adapter))
return -ENETDOWN;
if (!dev->bredr_state.svc_resolved)
return -ENOENT;
for (l = services; l; l = g_slist_next(l)) {
struct btd_service *service = l->data;
dev->pending = g_slist_append(dev->pending, service);
}
return connect_next(dev);
}
static DBusMessage *connect_profiles(struct btd_device *dev, uint8_t bdaddr_type,
DBusMessage *msg, const char *uuid)
{
struct bearer_state *state = get_state(dev, bdaddr_type);
int err;
DBG("%s %s, client %s", dev->path, uuid ? uuid : "(all)",
dbus_message_get_sender(msg));
if (dev->pending || dev->connect || dev->browse)
return btd_error_in_progress(msg);
if (!btd_adapter_get_powered(dev->adapter))
return btd_error_not_ready(msg);
btd_device_set_temporary(dev, false);
if (!state->svc_resolved)
goto resolve_services;
dev->pending = create_pending_list(dev, uuid);
if (!dev->pending) {
if (dev->svc_refreshed) {
if (find_service_with_state(dev->services,
BTD_SERVICE_STATE_CONNECTED))
return dbus_message_new_method_return(msg);
else
return btd_error_not_available(msg);
}
goto resolve_services;
}
err = connect_next(dev);
if (err < 0) {
if (err == -EALREADY)
return dbus_message_new_method_return(msg);
return btd_error_failed(msg, strerror(-err));
}
dev->connect = dbus_message_ref(msg);
return NULL;
resolve_services:
DBG("Resolving services for %s", dev->path);
if (bdaddr_type == BDADDR_BREDR)
err = device_browse_sdp(dev, msg);
else
err = device_browse_gatt(dev, msg);
if (err < 0)
return btd_error_failed(msg, strerror(-err));
return NULL;
}
#define NVAL_TIME ((time_t) -1)
#define SEEN_TRESHHOLD 300
static uint8_t select_conn_bearer(struct btd_device *dev)
{
time_t bredr_last = NVAL_TIME, le_last = NVAL_TIME;
time_t current = time(NULL);
/* Prefer bonded bearer in case only one is bonded */
if (dev->bredr_state.bonded && !dev->le_state.bonded )
return BDADDR_BREDR;
else if (!dev->bredr_state.bonded && dev->le_state.bonded)
return dev->bdaddr_type;
/* If the address is random it can only be connected over LE */
if (dev->bdaddr_type == BDADDR_LE_RANDOM)
return dev->bdaddr_type;
if (dev->bredr_seen) {
bredr_last = current - dev->bredr_seen;
if (bredr_last > SEEN_TRESHHOLD)
bredr_last = NVAL_TIME;
}
if (dev->le_seen) {
le_last = current - dev->le_seen;
if (le_last > SEEN_TRESHHOLD)
le_last = NVAL_TIME;
}
if (le_last == NVAL_TIME && bredr_last == NVAL_TIME)
return dev->bdaddr_type;
if (dev->bredr && (!dev->le || le_last == NVAL_TIME))
return BDADDR_BREDR;
if (dev->le && (!dev->bredr || bredr_last == NVAL_TIME))
return dev->bdaddr_type;
/*
* Prefer BR/EDR if time is the same since it might be from an
* advertisement with BR/EDR flag set.
*/
if (bredr_last <= le_last)
return BDADDR_BREDR;
return dev->bdaddr_type;
}
static DBusMessage *dev_connect(DBusConnection *conn, DBusMessage *msg,
void *user_data)
{
struct btd_device *dev = user_data;
uint8_t bdaddr_type;
if (dev->bredr_state.connected) {
/*
* Check if services have been resolved and there is at list
* one connected before switching to connect LE.
*/
if (dev->bredr_state.svc_resolved &&
find_service_with_state(dev->services,
BTD_SERVICE_STATE_CONNECTED))
bdaddr_type = dev->bdaddr_type;
else
bdaddr_type = BDADDR_BREDR;
} else if (dev->le_state.connected && dev->bredr)
bdaddr_type = BDADDR_BREDR;
else
bdaddr_type = select_conn_bearer(dev);
if (bdaddr_type != BDADDR_BREDR) {
int err;
if (dev->le_state.connected)
return dbus_message_new_method_return(msg);
btd_device_set_temporary(dev, false);
if (dev->disable_auto_connect) {
dev->disable_auto_connect = FALSE;
device_set_auto_connect(dev, TRUE);
}
err = device_connect_le(dev);
if (err < 0)
return btd_error_failed(msg, strerror(-err));
dev->connect = dbus_message_ref(msg);
return NULL;
}
return connect_profiles(dev, bdaddr_type, msg, NULL);
}
static DBusMessage *connect_profile(DBusConnection *conn, DBusMessage *msg,
void *user_data)
{
struct btd_device *dev = user_data;
const char *pattern;
char *uuid;
DBusMessage *reply;
if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &pattern,
DBUS_TYPE_INVALID))
return btd_error_invalid_args(msg);
uuid = bt_name2string(pattern);
reply = connect_profiles(dev, BDADDR_BREDR, msg, uuid);
free(uuid);
return reply;
}
static void device_profile_disconnected(struct btd_device *dev,
struct btd_profile *profile, int err)
{
if (!dev->disconnect)
return;
if (err)
g_dbus_send_message(dbus_conn,
btd_error_failed(dev->disconnect,
strerror(-err)));
else
g_dbus_send_reply(dbus_conn, dev->disconnect,
DBUS_TYPE_INVALID);
dbus_message_unref(dev->disconnect);
dev->disconnect = NULL;
}
static DBusMessage *disconnect_profile(DBusConnection *conn, DBusMessage *msg,
void *user_data)
{
struct btd_device *dev = user_data;
struct btd_service *service;
const char *pattern;
char *uuid;
int err;
if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &pattern,
DBUS_TYPE_INVALID))
return btd_error_invalid_args(msg);
uuid = bt_name2string(pattern);
if (uuid == NULL)
return btd_error_invalid_args(msg);
service = find_connectable_service(dev, uuid);
free(uuid);
if (!service)
return btd_error_invalid_args(msg);
if (dev->disconnect)
return btd_error_in_progress(msg);
dev->disconnect = dbus_message_ref(msg);
err = btd_service_disconnect(service);
if (err == 0)
return NULL;
dbus_message_unref(dev->disconnect);
dev->disconnect = NULL;
if (err == -ENOTSUP)
return btd_error_not_supported(msg);
return btd_error_failed(msg, strerror(-err));
}
static void store_services(struct btd_device *device)
{
struct btd_adapter *adapter = device->adapter;
char filename[PATH_MAX];
char src_addr[18], dst_addr[18];
uuid_t uuid;
char *prim_uuid;
GKeyFile *key_file;
GSList *l;
char *data;
gsize length = 0;
if (device_address_is_private(device)) {
warn("Can't store services for private addressed device %s",
device->path);
return;
}
sdp_uuid16_create(&uuid, GATT_PRIM_SVC_UUID);
prim_uuid = bt_uuid2string(&uuid);
if (prim_uuid == NULL)
return;
ba2str(btd_adapter_get_address(adapter), src_addr);
ba2str(&device->bdaddr, dst_addr);
snprintf(filename, PATH_MAX, STORAGEDIR "/%s/%s/attributes", src_addr,
dst_addr);
key_file = g_key_file_new();
for (l = device->primaries; l; l = l->next) {
struct gatt_primary *primary = l->data;
char handle[6], uuid_str[33];
int i;
sprintf(handle, "%hu", primary->range.start);
bt_string2uuid(&uuid, primary->uuid);
sdp_uuid128_to_uuid(&uuid);
switch (uuid.type) {
case SDP_UUID16:
sprintf(uuid_str, "%4.4X", uuid.value.uuid16);
break;
case SDP_UUID32:
sprintf(uuid_str, "%8.8X", uuid.value.uuid32);
break;
case SDP_UUID128:
for (i = 0; i < 16; i++)
sprintf(uuid_str + (i * 2), "%2.2X",
uuid.value.uuid128.data[i]);
break;
default:
uuid_str[0] = '\0';
}
g_key_file_set_string(key_file, handle, "UUID", prim_uuid);
g_key_file_set_string(key_file, handle, "Value", uuid_str);
g_key_file_set_integer(key_file, handle, "EndGroupHandle",
primary->range.end);
}
data = g_key_file_to_data(key_file, &length, NULL);
if (length > 0) {
create_file(filename, S_IRUSR | S_IWUSR);
g_file_set_contents(filename, data, length, NULL);
}
free(prim_uuid);
g_free(data);
g_key_file_free(key_file);
}
struct gatt_saver {
struct btd_device *device;
uint16_t ext_props;
GKeyFile *key_file;
};
static void store_desc(struct gatt_db_attribute *attr, void *user_data)
{
struct gatt_saver *saver = user_data;
GKeyFile *key_file = saver->key_file;
char handle[6], value[100], uuid_str[MAX_LEN_UUID_STR];
const bt_uuid_t *uuid;
bt_uuid_t ext_uuid;
uint16_t handle_num;
handle_num = gatt_db_attribute_get_handle(attr);
sprintf(handle, "%04hx", handle_num);
uuid = gatt_db_attribute_get_type(attr);
bt_uuid_to_string(uuid, uuid_str, sizeof(uuid_str));
bt_uuid16_create(&ext_uuid, GATT_CHARAC_EXT_PROPER_UUID);
if (!bt_uuid_cmp(uuid, &ext_uuid) && saver->ext_props)
sprintf(value, "%04hx:%s", saver->ext_props, uuid_str);
else
sprintf(value, "%s", uuid_str);
g_key_file_set_string(key_file, "Attributes", handle, value);
}
static void store_chrc(struct gatt_db_attribute *attr, void *user_data)
{
struct gatt_saver *saver = user_data;
GKeyFile *key_file = saver->key_file;
char handle[6], value[100], uuid_str[MAX_LEN_UUID_STR];
uint16_t handle_num, value_handle;
uint8_t properties;
bt_uuid_t uuid;
if (!gatt_db_attribute_get_char_data(attr, &handle_num, &value_handle,
&properties, &saver->ext_props,
&uuid)) {
warn("Error storing characteristic - can't get data");
return;
}
sprintf(handle, "%04hx", handle_num);
bt_uuid_to_string(&uuid, uuid_str, sizeof(uuid_str));
sprintf(value, GATT_CHARAC_UUID_STR ":%04hx:%02hhx:%s", value_handle,
properties, uuid_str);
g_key_file_set_string(key_file, "Attributes", handle, value);
gatt_db_service_foreach_desc(attr, store_desc, saver);
}
static void store_incl(struct gatt_db_attribute *attr, void *user_data)
{
struct gatt_saver *saver = user_data;
GKeyFile *key_file = saver->key_file;
struct gatt_db_attribute *service;
char handle[6], value[100], uuid_str[MAX_LEN_UUID_STR];
uint16_t handle_num, start, end;
bt_uuid_t uuid;
if (!gatt_db_attribute_get_incl_data(attr, &handle_num, &start, &end)) {
warn("Error storing included service - can't get data");
return;
}
service = gatt_db_get_attribute(saver->device->db, start);
if (!service) {
warn("Error storing included service - can't find it");
return;
}
sprintf(handle, "%04hx", handle_num);
gatt_db_attribute_get_service_uuid(service, &uuid);
bt_uuid_to_string(&uuid, uuid_str, sizeof(uuid_str));
sprintf(value, GATT_INCLUDE_UUID_STR ":%04hx:%04hx:%s", start,
end, uuid_str);
g_key_file_set_string(key_file, "Attributes", handle, value);
}
static void store_service(struct gatt_db_attribute *attr, void *user_data)
{
struct gatt_saver *saver = user_data;
GKeyFile *key_file = saver->key_file;
char uuid_str[MAX_LEN_UUID_STR], handle[6], value[256];
uint16_t start, end;
bt_uuid_t uuid;
bool primary;
char *type;
if (!gatt_db_attribute_get_service_data(attr, &start, &end, &primary,
&uuid)) {
warn("Error storing service - can't get data");
return;
}
sprintf(handle, "%04hx", start);
bt_uuid_to_string(&uuid, uuid_str, sizeof(uuid_str));
if (primary)
type = GATT_PRIM_SVC_UUID_STR;
else
type = GATT_SND_SVC_UUID_STR;
sprintf(value, "%s:%04hx:%s", type, end, uuid_str);
g_key_file_set_string(key_file, "Attributes", handle, value);
gatt_db_service_foreach_incl(attr, store_incl, saver);
gatt_db_service_foreach_char(attr, store_chrc, saver);
}
static void store_gatt_db(struct btd_device *device)
{
struct btd_adapter *adapter = device->adapter;
char filename[PATH_MAX];
char src_addr[18], dst_addr[18];
GKeyFile *key_file;
char *data;
gsize length = 0;
struct gatt_saver saver;
if (device_address_is_private(device)) {
warn("Can't store GATT db for private addressed device %s",
device->path);
return;
}
ba2str(btd_adapter_get_address(adapter), src_addr);
ba2str(&device->bdaddr, dst_addr);
snprintf(filename, PATH_MAX, STORAGEDIR "/%s/cache/%s", src_addr,
dst_addr);
create_file(filename, S_IRUSR | S_IWUSR);
key_file = g_key_file_new();
g_key_file_load_from_file(key_file, filename, 0, NULL);
/* Remove current attributes since it might have changed */
g_key_file_remove_group(key_file, "Attributes", NULL);
saver.key_file = key_file;
saver.device = device;
gatt_db_foreach_service(device->db, NULL, store_service, &saver);
data = g_key_file_to_data(key_file, &length, NULL);
g_file_set_contents(filename, data, length, NULL);
g_free(data);
g_key_file_free(key_file);
}
static void browse_request_complete(struct browse_req *req, uint8_t bdaddr_type,
int err)
{
struct btd_device *dev = req->device;
DBusMessage *reply = NULL;
if (!req->msg)
goto done;
if (dbus_message_is_method_call(req->msg, DEVICE_INTERFACE, "Pair")) {
if (!device_is_paired(dev, bdaddr_type)) {
reply = btd_error_failed(req->msg, "Not paired");
goto done;
}
if (dev->pending_paired) {
g_dbus_emit_property_changed(dbus_conn, dev->path,
DEVICE_INTERFACE, "Paired");
dev->pending_paired = false;
}
/* Disregard browse errors in case of Pair */
reply = g_dbus_create_reply(req->msg, DBUS_TYPE_INVALID);
goto done;
}
if (err) {
reply = btd_error_failed(req->msg, strerror(-err));
goto done;
}
if (dbus_message_is_method_call(req->msg, DEVICE_INTERFACE, "Connect"))
reply = dev_connect(dbus_conn, req->msg, dev);
else if (dbus_message_is_method_call(req->msg, DEVICE_INTERFACE,
"ConnectProfile"))
reply = connect_profile(dbus_conn, req->msg, dev);
else
reply = g_dbus_create_reply(req->msg, DBUS_TYPE_INVALID);
done:
if (reply)
g_dbus_send_message(dbus_conn, reply);
browse_request_free(req);
}
static void device_set_svc_refreshed(struct btd_device *device, bool value)
{
if (device->svc_refreshed == value)
return;
device->svc_refreshed = value;
g_dbus_emit_property_changed(dbus_conn, device->path,
DEVICE_INTERFACE, "ServicesResolved");
}
static void device_svc_resolved(struct btd_device *dev, uint8_t bdaddr_type,
int err)
{
struct bearer_state *state = get_state(dev, bdaddr_type);
struct browse_req *req = dev->browse;
DBG("%s err %d", dev->path, err);
state->svc_resolved = true;
/* Disconnection notification can happen before this function
* gets called, so don't set svc_refreshed for a disconnected
* device.
*/
if (state->connected)
device_set_svc_refreshed(dev, true);
g_slist_free_full(dev->eir_uuids, g_free);
dev->eir_uuids = NULL;
if (dev->pending_paired) {
g_dbus_emit_property_changed(dbus_conn, dev->path,
DEVICE_INTERFACE, "Paired");
dev->pending_paired = false;
}
while (dev->svc_callbacks) {
struct svc_callback *cb = dev->svc_callbacks->data;
if (cb->idle_id > 0)
g_source_remove(cb->idle_id);
cb->func(dev, err, cb->user_data);
dev->svc_callbacks = g_slist_delete_link(dev->svc_callbacks,
dev->svc_callbacks);
g_free(cb);
}
if (!dev->temporary)
store_device_info(dev);
if (bdaddr_type != BDADDR_BREDR && err == 0)
store_services(dev);
if (!req)
return;
dev->browse = NULL;
browse_request_complete(req, bdaddr_type, err);
}
static struct bonding_req *bonding_request_new(DBusMessage *msg,
struct btd_device *device,
uint8_t bdaddr_type,
struct agent *agent)
{
struct bonding_req *bonding;
char addr[18];
ba2str(&device->bdaddr, addr);
DBG("Requesting bonding for %s", addr);
bonding = g_new0(struct bonding_req, 1);
bonding->msg = dbus_message_ref(msg);
bonding->bdaddr_type = bdaddr_type;
bonding->cb_iter = btd_adapter_pin_cb_iter_new(device->adapter);
/* Marks the bonding start time for the first attempt on request
* construction. The following attempts will be updated on
* device_bonding_retry. */
clock_gettime(CLOCK_MONOTONIC, &bonding->attempt_start_time);
if (agent)
bonding->agent = agent_ref(agent);
return bonding;
}
void device_bonding_restart_timer(struct btd_device *device)
{
if (!device || !device->bonding)
return;
clock_gettime(CLOCK_MONOTONIC, &device->bonding->attempt_start_time);
}
static void bonding_request_stop_timer(struct bonding_req *bonding)
{
struct timespec current;
clock_gettime(CLOCK_MONOTONIC, &current);
/* Compute the time difference in ms. */
bonding->last_attempt_duration_ms =
(current.tv_sec - bonding->attempt_start_time.tv_sec) * 1000L +
(current.tv_nsec - bonding->attempt_start_time.tv_nsec)
/ 1000000L;
}
/* Returns the duration of the last bonding attempt in milliseconds. The
* duration is measured starting from the latest of the following three
* events and finishing when the Command complete event is received for the
* authentication request:
* - MGMT_OP_PAIR_DEVICE is sent,
* - MGMT_OP_PIN_CODE_REPLY is sent and
* - Command complete event is received for the sent MGMT_OP_PIN_CODE_REPLY.
*/
long device_bonding_last_duration(struct btd_device *device)
{
struct bonding_req *bonding = device->bonding;
if (!bonding)
return 0;
return bonding->last_attempt_duration_ms;
}
static void create_bond_req_exit(DBusConnection *conn, void *user_data)
{
struct btd_device *device = user_data;
char addr[18];
ba2str(&device->bdaddr, addr);
DBG("%s: requestor exited before bonding was completed", addr);
if (device->authr)
device_cancel_authentication(device, FALSE);
if (device->bonding) {
device->bonding->listener_id = 0;
device_request_disconnect(device, NULL);
}
}
static void bonding_request_free(struct bonding_req *bonding)
{
if (!bonding)
return;
if (bonding->listener_id)
g_dbus_remove_watch(dbus_conn, bonding->listener_id);
if (bonding->msg)
dbus_message_unref(bonding->msg);
if (bonding->cb_iter)
g_free(bonding->cb_iter);
if (bonding->agent) {
agent_cancel(bonding->agent);
agent_unref(bonding->agent);
bonding->agent = NULL;
}
if (bonding->retry_timer)
g_source_remove(bonding->retry_timer);
if (bonding->device)
bonding->device->bonding = NULL;
g_free(bonding);
}
static DBusMessage *pair_device(DBusConnection *conn, DBusMessage *msg,
void *data)
{
struct btd_device *device = data;
struct btd_adapter *adapter = device->adapter;
struct bearer_state *state;
uint8_t bdaddr_type;
const char *sender;
struct agent *agent;
struct bonding_req *bonding;
uint8_t io_cap;
int err;
btd_device_set_temporary(device, false);
if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_INVALID))
return btd_error_invalid_args(msg);
if (device->bonding)
return btd_error_in_progress(msg);
if (device->bredr_state.bonded)
bdaddr_type = device->bdaddr_type;
else if (device->le_state.bonded)
bdaddr_type = BDADDR_BREDR;
else
bdaddr_type = select_conn_bearer(device);
state = get_state(device, bdaddr_type);
if (state->bonded)
return btd_error_already_exists(msg);
sender = dbus_message_get_sender(msg);
agent = agent_get(sender);
if (agent)
io_cap = agent_get_io_capability(agent);
else
io_cap = IO_CAPABILITY_NOINPUTNOOUTPUT;
bonding = bonding_request_new(msg, device, bdaddr_type, agent);
if (agent)
agent_unref(agent);
bonding->listener_id = g_dbus_add_disconnect_watch(dbus_conn,
sender, create_bond_req_exit,
device, NULL);
device->bonding = bonding;
bonding->device = device;
/* Due to a bug in the kernel we might loose out on ATT commands
* that arrive during the SMP procedure, so connect the ATT
* channel first and only then start pairing (there's code for
* this in the ATT connect callback)
*/
if (bdaddr_type != BDADDR_BREDR) {
if (!state->connected && btd_le_connect_before_pairing())
err = device_connect_le(device);
else
err = adapter_create_bonding(adapter, &device->bdaddr,
device->bdaddr_type,
io_cap);
} else {
err = adapter_create_bonding(adapter, &device->bdaddr,
BDADDR_BREDR, io_cap);
}
if (err < 0) {
bonding_request_free(device->bonding);
return btd_error_failed(msg, strerror(-err));
}
return NULL;
}
static DBusMessage *new_authentication_return(DBusMessage *msg, uint8_t status)
{
switch (status) {
case MGMT_STATUS_SUCCESS:
return dbus_message_new_method_return(msg);
case MGMT_STATUS_CONNECT_FAILED:
return dbus_message_new_error(msg,
ERROR_INTERFACE ".ConnectionAttemptFailed",
"Page Timeout");
case MGMT_STATUS_TIMEOUT:
return dbus_message_new_error(msg,
ERROR_INTERFACE ".AuthenticationTimeout",
"Authentication Timeout");
case MGMT_STATUS_BUSY:
case MGMT_STATUS_REJECTED:
return dbus_message_new_error(msg,
ERROR_INTERFACE ".AuthenticationRejected",
"Authentication Rejected");
case MGMT_STATUS_CANCELLED:
case MGMT_STATUS_NO_RESOURCES:
case MGMT_STATUS_DISCONNECTED:
return dbus_message_new_error(msg,
ERROR_INTERFACE ".AuthenticationCanceled",
"Authentication Canceled");
case MGMT_STATUS_ALREADY_PAIRED:
return dbus_message_new_error(msg,
ERROR_INTERFACE ".AlreadyExists",
"Already Paired");
default:
return dbus_message_new_error(msg,
ERROR_INTERFACE ".AuthenticationFailed",
"Authentication Failed");
}
}
static void device_cancel_bonding(struct btd_device *device, uint8_t status)
{
struct bonding_req *bonding = device->bonding;
DBusMessage *reply;
char addr[18];
if (!bonding)
return;
ba2str(&device->bdaddr, addr);
DBG("Canceling bonding request for %s", addr);
if (device->authr)
device_cancel_authentication(device, FALSE);
reply = new_authentication_return(bonding->msg, status);
g_dbus_send_message(dbus_conn, reply);
bonding_request_cancel(bonding);
bonding_request_free(bonding);
}
static DBusMessage *cancel_pairing(DBusConnection *conn, DBusMessage *msg,
void *data)
{
struct btd_device *device = data;
struct bonding_req *req = device->bonding;
DBG("");
if (!req)
return btd_error_does_not_exist(msg);
device_cancel_bonding(device, MGMT_STATUS_CANCELLED);
return dbus_message_new_method_return(msg);
}
static const GDBusMethodTable device_methods[] = {
{ GDBUS_ASYNC_METHOD("Disconnect", NULL, NULL, dev_disconnect) },
{ GDBUS_ASYNC_METHOD("Connect", NULL, NULL, dev_connect) },
{ GDBUS_ASYNC_METHOD("ConnectProfile", GDBUS_ARGS({ "UUID", "s" }),
NULL, connect_profile) },
{ GDBUS_ASYNC_METHOD("DisconnectProfile", GDBUS_ARGS({ "UUID", "s" }),
NULL, disconnect_profile) },
{ GDBUS_ASYNC_METHOD("Pair", NULL, NULL, pair_device) },
{ GDBUS_METHOD("CancelPairing", NULL, NULL, cancel_pairing) },
{ }
};
static const GDBusPropertyTable device_properties[] = {
{ "Address", "s", dev_property_get_address },
{ "Name", "s", dev_property_get_name, NULL, dev_property_exists_name },
{ "Alias", "s", dev_property_get_alias, dev_property_set_alias },
{ "Class", "u", dev_property_get_class, NULL,
dev_property_exists_class },
{ "Appearance", "q", dev_property_get_appearance, NULL,
dev_property_exists_appearance },
{ "Icon", "s", dev_property_get_icon, NULL,
dev_property_exists_icon },
{ "Paired", "b", dev_property_get_paired },
{ "Trusted", "b", dev_property_get_trusted, dev_property_set_trusted },
{ "Blocked", "b", dev_property_get_blocked, dev_property_set_blocked },
{ "LegacyPairing", "b", dev_property_get_legacy },
{ "RSSI", "n", dev_property_get_rssi, NULL, dev_property_exists_rssi },
{ "Connected", "b", dev_property_get_connected },
{ "UUIDs", "as", dev_property_get_uuids },
{ "Modalias", "s", dev_property_get_modalias, NULL,
dev_property_exists_modalias },
{ "Adapter", "o", dev_property_get_adapter },
{ "ManufacturerData", "a{qv}", dev_property_get_manufacturer_data,
NULL, dev_property_manufacturer_data_exist },
{ "ServiceData", "a{sv}", dev_property_get_service_data,
NULL, dev_property_service_data_exist },
{ "TxPower", "n", dev_property_get_tx_power, NULL,
dev_property_exists_tx_power },
{ "ServicesResolved", "b", dev_property_get_svc_resolved, NULL, NULL },
{ "AdvertisingFlags", "ay", dev_property_get_flags, NULL,
dev_property_flags_exist,
G_DBUS_PROPERTY_FLAG_EXPERIMENTAL},
{ }
};
uint8_t btd_device_get_bdaddr_type(struct btd_device *dev)
{
return dev->bdaddr_type;
}
bool btd_device_is_connected(struct btd_device *dev)
{
return dev->bredr_state.connected || dev->le_state.connected;
}
void device_add_connection(struct btd_device *dev, uint8_t bdaddr_type)
{
struct bearer_state *state = get_state(dev, bdaddr_type);
device_update_last_seen(dev, bdaddr_type);
if (state->connected) {
char addr[18];
ba2str(&dev->bdaddr, addr);
error("Device %s is already connected", addr);
return;
}
/* If this is the first connection over this bearer */
if (bdaddr_type == BDADDR_BREDR)
device_set_bredr_support(dev);
else
device_set_le_support(dev, bdaddr_type);
state->connected = true;
if (dev->le_state.connected && dev->bredr_state.connected)
return;
g_dbus_emit_property_changed(dbus_conn, dev->path, DEVICE_INTERFACE,
"Connected");
}
void device_remove_connection(struct btd_device *device, uint8_t bdaddr_type)
{
struct bearer_state *state = get_state(device, bdaddr_type);
if (!state->connected)
return;
state->connected = false;
device->general_connect = FALSE;
device_set_svc_refreshed(device, false);
if (device->disconn_timer > 0) {
g_source_remove(device->disconn_timer);
device->disconn_timer = 0;
}
while (device->disconnects) {
DBusMessage *msg = device->disconnects->data;
g_dbus_send_reply(dbus_conn, msg, DBUS_TYPE_INVALID);
device->disconnects = g_slist_remove(device->disconnects, msg);
dbus_message_unref(msg);
}
if (state->paired && !state->bonded)
btd_adapter_remove_bonding(device->adapter, &device->bdaddr,
bdaddr_type);
if (device->bredr_state.connected || device->le_state.connected)
return;
g_dbus_emit_property_changed(dbus_conn, device->path,
DEVICE_INTERFACE, "Connected");
}
guint device_add_disconnect_watch(struct btd_device *device,
disconnect_watch watch, void *user_data,
GDestroyNotify destroy)
{
struct btd_disconnect_data *data;
static guint id = 0;
data = g_new0(struct btd_disconnect_data, 1);
data->id = ++id;
data->watch = watch;
data->user_data = user_data;
data->destroy = destroy;
device->watches = g_slist_append(device->watches, data);
return data->id;
}
void device_remove_disconnect_watch(struct btd_device *device, guint id)
{
GSList *l;
for (l = device->watches; l; l = l->next) {
struct btd_disconnect_data *data = l->data;
if (data->id == id) {
device->watches = g_slist_remove(device->watches,
data);
if (data->destroy)
data->destroy(data->user_data);
g_free(data);
return;
}
}
}
static char *load_cached_name(struct btd_device *device, const char *local,
const char *peer)
{
char filename[PATH_MAX];
GKeyFile *key_file;
char *str = NULL;
int len;
if (device_address_is_private(device))
return NULL;
snprintf(filename, PATH_MAX, STORAGEDIR "/%s/cache/%s", local, peer);
key_file = g_key_file_new();
if (!g_key_file_load_from_file(key_file, filename, 0, NULL))
goto failed;
str = g_key_file_get_string(key_file, "General", "Name", NULL);
if (str) {
len = strlen(str);
if (len > HCI_MAX_NAME_LENGTH)
str[HCI_MAX_NAME_LENGTH] = '\0';
}
failed:
g_key_file_free(key_file);
return str;
}
static struct csrk_info *load_csrk(GKeyFile *key_file, const char *group)
{
struct csrk_info *csrk;
char *str;
int i;
str = g_key_file_get_string(key_file, group, "Key", NULL);
if (!str)
return NULL;
csrk = g_new0(struct csrk_info, 1);
for (i = 0; i < 16; i++) {
if (sscanf(str + (i * 2), "%2hhx", &csrk->key[i]) != 1)
goto fail;
}
/*
* In case of older storage this will return 0 which is fine since it
* didn't support signing at that point the counter should never have
* been used.
*/
csrk->counter = g_key_file_get_integer(key_file, group, "Counter",
NULL);
g_free(str);
return csrk;
fail:
g_free(str);
g_free(csrk);
return NULL;
}
static void load_services(struct btd_device *device, char **uuids)
{
char **uuid;
for (uuid = uuids; *uuid; uuid++) {
if (g_slist_find_custom(device->uuids, *uuid, bt_uuid_strcmp))
continue;
device->uuids = g_slist_insert_sorted(device->uuids,
g_strdup(*uuid),
bt_uuid_strcmp);
}
g_strfreev(uuids);
}
static void convert_info(struct btd_device *device, GKeyFile *key_file)
{
char filename[PATH_MAX];
char adapter_addr[18];
char device_addr[18];
char **uuids;
char *str;
gsize length = 0;
/* Load device profile list from legacy properties */
uuids = g_key_file_get_string_list(key_file, "General", "SDPServices",
NULL, NULL);
if (uuids)
load_services(device, uuids);
uuids = g_key_file_get_string_list(key_file, "General", "GATTServices",
NULL, NULL);
if (uuids)
load_services(device, uuids);
if (!device->uuids)
return;
/* Remove old entries so they are not loaded again */
g_key_file_remove_key(key_file, "General", "SDPServices", NULL);
g_key_file_remove_key(key_file, "General", "GATTServices", NULL);
ba2str(btd_adapter_get_address(device->adapter), adapter_addr);
ba2str(&device->bdaddr, device_addr);
snprintf(filename, PATH_MAX, STORAGEDIR "/%s/%s/info", adapter_addr,
device_addr);
str = g_key_file_to_data(key_file, &length, NULL);
g_file_set_contents(filename, str, length, NULL);
g_free(str);
store_device_inf