bluez/profiles/input/server.c

331 lines
7.0 KiB
C
Raw Normal View History

profiles: Add SPDX License Identifier This patch adds SPDX License Identifier and removes the license text. ------------------------------------- License COUNT ------------------------------------- GPL-2.0-or-later : 72 LGPL-2.1-or-later : 6 License: GPL-2.0-or-later profiles/deviceinfo/dis.c profiles/deviceinfo/deviceinfo.c profiles/health/hdp_types.h profiles/health/mcap.c profiles/health/hdp_manager.c profiles/health/hdp_main.c profiles/health/mcap.h profiles/health/hdp_util.h profiles/health/hdp_manager.h profiles/health/hdp_util.c profiles/health/hdp.c profiles/health/hdp.h profiles/network/server.c profiles/network/connection.h profiles/network/connection.c profiles/network/manager.c profiles/network/bnep.c profiles/network/server.h profiles/network/bnep.h profiles/battery/battery.c profiles/cups/cups.h profiles/cups/sdp.c profiles/cups/main.c profiles/cups/spp.c profiles/cups/hcrp.c profiles/scanparam/scpp.c profiles/scanparam/scan.c profiles/audio/transport.h profiles/audio/avdtp.h profiles/audio/source.c profiles/audio/player.c profiles/audio/avctp.c profiles/audio/control.h profiles/audio/a2dp.c profiles/audio/source.h profiles/audio/transport.c profiles/audio/sink.c profiles/audio/avrcp.h profiles/audio/avdtp.c profiles/audio/a2dp.h profiles/audio/avrcp.c profiles/audio/media.h profiles/audio/avctp.h profiles/audio/control.c profiles/audio/sink.h profiles/audio/player.h profiles/audio/media.c profiles/sap/server.c profiles/sap/sap-dummy.c profiles/sap/main.c profiles/sap/sap.h profiles/sap/manager.c profiles/sap/server.h profiles/sap/manager.h profiles/iap/main.c profiles/gap/gas.c profiles/midi/midi.c profiles/midi/libmidi.c profiles/midi/libmidi.h profiles/input/server.c profiles/input/suspend.h profiles/input/uhid_copy.h profiles/input/device.c profiles/input/hog.c profiles/input/device.h profiles/input/hidp_defs.h profiles/input/manager.c profiles/input/suspend-none.c profiles/input/sixaxis.h profiles/input/hog-lib.c profiles/input/suspend-dummy.c profiles/input/server.h License: LGPL-2.1-or-later profiles/deviceinfo/dis.h profiles/battery/bas.h profiles/battery/bas.c profiles/scanparam/scpp.h profiles/audio/a2dp-codecs.h profiles/input/hog-lib.h
2020-09-22 03:00:34 +08:00
// SPDX-License-Identifier: GPL-2.0-or-later
2007-01-22 07:23:19 +08:00
/*
*
* BlueZ - Bluetooth protocol stack for Linux
*
2010-01-02 09:08:17 +08:00
* Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
2007-01-22 07:23:19 +08:00
*
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
2007-01-22 07:23:19 +08:00
#include <unistd.h>
2012-09-25 17:49:26 +08:00
#include <stdbool.h>
2009-02-23 20:10:54 +08:00
#include <errno.h>
2007-01-22 07:23:19 +08:00
#include <glib.h>
#include <dbus/dbus.h>
2007-01-22 07:23:19 +08:00
#include "lib/bluetooth.h"
#include "lib/sdp.h"
#include "lib/uuid.h"
#include "src/log.h"
#include "src/uuid-helper.h"
2013-01-10 12:30:30 +08:00
#include "btio/btio.h"
#include "src/adapter.h"
#include "src/device.h"
#include "src/profile.h"
2012-09-25 17:49:26 +08:00
#include "sixaxis.h"
#include "device.h"
2008-12-23 11:32:20 +08:00
#include "server.h"
struct confirm_data {
bdaddr_t dst;
GIOChannel *io;
};
static GSList *servers = NULL;
struct input_server {
bdaddr_t src;
GIOChannel *ctrl;
GIOChannel *intr;
struct confirm_data *confirm;
};
static int server_cmp(gconstpointer s, gconstpointer user_data)
{
const struct input_server *server = s;
const bdaddr_t *src = user_data;
return bacmp(&server->src, src);
}
struct sixaxis_data {
GIOChannel *chan;
uint16_t psm;
};
static void sixaxis_sdp_cb(struct btd_device *dev, int err, void *user_data)
{
struct sixaxis_data *data = user_data;
const bdaddr_t *src;
DBG("err %d (%s)", err, strerror(-err));
if (err < 0)
goto fail;
src = btd_adapter_get_address(device_get_adapter(dev));
input: Fix crash on authorization reply with first sixaxis connection Bogus unref in sixaxis_sdp_cb was resulting in NULL pointer dereference when auth_callback was called. src/adapter.c:connected_callback() hci0 device 00:06:F7:57:08:9E connected eir_len 5 src/device.c:device_set_class() /org/bluez/hci0/dev_00_06_F7_57_08_9E 0x000508 profiles/input/server.c:connect_event_cb() Incoming connection from 00:06:F7:57:08:9E on PSM 17 profiles/input/device.c:input_device_set_channel() idev (nil) psm 17 profiles/input/server.c:confirm_event_cb() src/agent.c:agent_ref() 0x8117eb8: ref=2 src/agent.c:agent_authorize_service() authorize service request was sent for /org/bluez/hci0/dev_00_06_F7_57_08_9E src/device.c:device_probe_profiles() Probing profiles for device 00:06:F7:57:08:9E profiles/input/device.c:input_device_register() /org/bluez/hci0/dev_00_06_F7_57_08_9E src/service.c:btd_service_ref() 0x811f580: ref=2 src/service.c:change_state() 0x811f580: device 00:06:F7:57:08:9E profile input-hid state changed: unavailable -> disconnected (0) src/device.c:device_svc_resolved() /org/bluez/hci0/dev_00_06_F7_57_08_9E err 0 profiles/input/server.c:sixaxis_sdp_cb() err 0 (Success) profiles/input/device.c:input_device_set_channel() idev 0x8118568 psm 17 profiles/input/server.c:connect_event_cb() Incoming connection from 00:06:F7:57:08:9E on PSM 19 profiles/input/device.c:input_device_set_channel() idev 0x8118568 psm 19 src/service.c:change_state() 0x811f580: device 00:06:F7:57:08:9E profile input-hid state changed: disconnected -> connected (0) sixaxis: compatible device connected: PLAYSTATION(R)3 Controller (054C:0268) plugins/sixaxis.c:setup_leds() number 2 sixaxis: failed to set LEDS (0 bytes written) src/agent.c:agent_ref() 0x8117eb8: ref=3 Program received signal SIGSEGV, Segmentation fault.
2014-01-08 08:02:23 +08:00
if (input_device_set_channel(src, device_get_address(dev), data->psm,
data->chan) < 0)
goto fail;
g_io_channel_unref(data->chan);
g_free(data);
return;
fail:
g_io_channel_shutdown(data->chan, TRUE, NULL);
g_io_channel_unref(data->chan);
g_free(data);
}
static void sixaxis_browse_sdp(const bdaddr_t *src, const bdaddr_t *dst,
GIOChannel *chan, uint16_t psm)
{
struct btd_device *device;
struct sixaxis_data *data;
device = btd_adapter_find_device(adapter_find(src), dst, BDADDR_BREDR);
if (!device)
return;
data = g_new0(struct sixaxis_data, 1);
data->chan = g_io_channel_ref(chan);
data->psm = psm;
if (psm == L2CAP_PSM_HIDP_CTRL)
device_discover_services(device);
device_wait_for_svc_complete(device, sixaxis_sdp_cb, data);
}
static bool dev_is_sixaxis(const bdaddr_t *src, const bdaddr_t *dst)
{
struct btd_device *device;
uint16_t vid, pid;
const struct cable_pairing *cp;
device = btd_adapter_find_device(adapter_find(src), dst, BDADDR_BREDR);
if (!device)
return false;
vid = btd_device_get_vendor(device);
pid = btd_device_get_product(device);
cp = get_pairing(vid, pid, NULL);
if (cp && (cp->type == CABLE_PAIRING_SIXAXIS ||
cp->type == CABLE_PAIRING_DS4))
return true;
return false;
}
2009-02-19 06:17:50 +08:00
static void connect_event_cb(GIOChannel *chan, GError *err, gpointer data)
{
2009-02-22 21:44:14 +08:00
uint16_t psm;
2009-02-19 06:17:50 +08:00
bdaddr_t src, dst;
char address[18];
2009-02-19 06:17:50 +08:00
GError *gerr = NULL;
2009-02-23 20:10:54 +08:00
int ret;
2007-01-22 07:23:19 +08:00
2009-02-19 06:17:50 +08:00
if (err) {
error("%s", err->message);
2008-05-16 06:34:08 +08:00
return;
}
bt_io_get(chan, &gerr,
2009-02-19 06:17:50 +08:00
BT_IO_OPT_SOURCE_BDADDR, &src,
BT_IO_OPT_DEST_BDADDR, &dst,
BT_IO_OPT_PSM, &psm,
2009-02-22 22:50:59 +08:00
BT_IO_OPT_INVALID);
2009-02-19 06:17:50 +08:00
if (gerr) {
error("%s", gerr->message);
g_error_free(gerr);
g_io_channel_shutdown(chan, TRUE, NULL);
2009-02-19 06:17:50 +08:00
return;
}
ba2str(&dst, address);
DBG("Incoming connection from %s on PSM %d", address, psm);
2009-02-23 20:10:54 +08:00
ret = input_device_set_channel(&src, &dst, psm, chan);
if (ret == 0)
2008-05-16 06:34:08 +08:00
return;
if (ret == -ENOENT && dev_is_sixaxis(&src, &dst)) {
sixaxis_browse_sdp(&src, &dst, chan, psm);
return;
}
error("Refusing input device connect: %s (%d)", strerror(-ret), -ret);
/* Send unplug virtual cable to unknown devices */
if (ret == -ENOENT && psm == L2CAP_PSM_HIDP_CTRL) {
unsigned char unplug = 0x15;
int sk = g_io_channel_unix_get_fd(chan);
if (write(sk, &unplug, sizeof(unplug)) < 0)
error("Unable to send virtual cable unplug");
}
g_io_channel_shutdown(chan, TRUE, NULL);
}
static void auth_callback(DBusError *derr, void *user_data)
{
struct input_server *server = user_data;
struct confirm_data *confirm = server->confirm;
GError *err = NULL;
if (derr) {
error("Access denied: %s", derr->message);
goto reject;
}
if (!input_device_exists(&server->src, &confirm->dst) &&
!dev_is_sixaxis(&server->src, &confirm->dst))
return;
if (!bt_io_accept(confirm->io, connect_event_cb, server, NULL, &err)) {
error("bt_io_accept: %s", err->message);
g_error_free(err);
goto reject;
2007-01-22 07:23:19 +08:00
}
g_io_channel_unref(confirm->io);
g_free(server->confirm);
server->confirm = NULL;
return;
reject:
g_io_channel_shutdown(confirm->io, TRUE, NULL);
g_io_channel_unref(confirm->io);
server->confirm = NULL;
input_device_close_channels(&server->src, &confirm->dst);
g_free(confirm);
}
static void confirm_event_cb(GIOChannel *chan, gpointer user_data)
{
struct input_server *server = user_data;
bdaddr_t src, dst;
GError *err = NULL;
char addr[18];
guint ret;
DBG("");
bt_io_get(chan, &err,
BT_IO_OPT_SOURCE_BDADDR, &src,
BT_IO_OPT_DEST_BDADDR, &dst,
BT_IO_OPT_INVALID);
if (err) {
error("%s", err->message);
g_error_free(err);
g_io_channel_shutdown(chan, TRUE, NULL);
return;
}
ba2str(&dst, addr);
if (server->confirm) {
error("Refusing connection from %s: setup in progress", addr);
goto drop;
}
if (!input_device_exists(&src, &dst) && !dev_is_sixaxis(&src, &dst)) {
error("Refusing connection from %s: unknown device", addr);
goto drop;
}
server->confirm = g_new0(struct confirm_data, 1);
server->confirm->io = g_io_channel_ref(chan);
bacpy(&server->confirm->dst, &dst);
ret = btd_request_authorization(&src, &dst, HID_UUID,
auth_callback, server);
if (ret != 0)
return;
error("input: authorization for device %s failed", addr);
g_io_channel_unref(server->confirm->io);
g_free(server->confirm);
server->confirm = NULL;
drop:
input_device_close_channels(&src, &dst);
g_io_channel_shutdown(chan, TRUE, NULL);
2007-01-22 07:23:19 +08:00
}
2008-09-10 02:01:31 +08:00
int server_start(const bdaddr_t *src)
2007-01-22 07:23:19 +08:00
{
struct input_server *server;
2009-02-19 06:17:50 +08:00
GError *err = NULL;
BtIOSecLevel sec_level = input_get_classic_bonded_only() ?
BT_IO_SEC_MEDIUM : BT_IO_SEC_LOW;
server = g_new0(struct input_server, 1);
bacpy(&server->src, src);
server->ctrl = bt_io_listen(connect_event_cb, NULL,
server, NULL, &err,
2009-02-19 06:17:50 +08:00
BT_IO_OPT_SOURCE_BDADDR, src,
BT_IO_OPT_PSM, L2CAP_PSM_HIDP_CTRL,
BT_IO_OPT_SEC_LEVEL, sec_level,
2009-02-19 06:17:50 +08:00
BT_IO_OPT_INVALID);
if (!server->ctrl) {
2007-01-24 23:43:06 +08:00
error("Failed to listen on control channel");
g_error_free(err);
g_free(server);
2007-01-22 07:23:19 +08:00
return -1;
2007-01-24 23:43:06 +08:00
}
2007-01-22 07:23:19 +08:00
server->intr = bt_io_listen(NULL, confirm_event_cb,
server, NULL, &err,
2009-02-19 06:17:50 +08:00
BT_IO_OPT_SOURCE_BDADDR, src,
BT_IO_OPT_PSM, L2CAP_PSM_HIDP_INTR,
BT_IO_OPT_SEC_LEVEL, sec_level,
2009-02-19 06:17:50 +08:00
BT_IO_OPT_INVALID);
if (!server->intr) {
2007-01-24 23:43:06 +08:00
error("Failed to listen on interrupt channel");
g_io_channel_unref(server->ctrl);
g_error_free(err);
g_free(server);
return -1;
2007-01-22 07:23:19 +08:00
}
servers = g_slist_append(servers, server);
2007-01-22 07:23:19 +08:00
return 0;
}
2008-09-10 02:01:31 +08:00
void server_stop(const bdaddr_t *src)
2007-01-22 07:23:19 +08:00
{
struct input_server *server;
GSList *l;
2007-01-22 07:23:19 +08:00
l = g_slist_find_custom(servers, src, server_cmp);
if (!l)
return;
server = l->data;
g_io_channel_shutdown(server->intr, TRUE, NULL);
g_io_channel_unref(server->intr);
2009-02-19 06:17:50 +08:00
g_io_channel_shutdown(server->ctrl, TRUE, NULL);
g_io_channel_unref(server->ctrl);
servers = g_slist_remove(servers, server);
g_free(server);
2007-01-22 07:23:19 +08:00
}