bluez/input/server.c

170 lines
3.8 KiB
C
Raw Normal View History

2007-01-22 07:23:19 +08:00
/*
*
* BlueZ - Bluetooth protocol stack for Linux
*
2008-02-02 11:37:05 +08:00
* Copyright (C) 2004-2008 Marcel Holtmann <marcel@holtmann.org>
2007-01-22 07:23:19 +08:00
*
*
* 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 <unistd.h>
#include <bluetooth/bluetooth.h>
2008-05-16 06:34:08 +08:00
#include <bluetooth/sdp.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 "logging.h"
#include "adapter.h"
#include "device.h"
2008-05-16 06:34:08 +08:00
#include "glib-helper.h"
static GSList *servers = NULL;
struct server {
bdaddr_t src;
GIOChannel *ctrl;
GIOChannel *intr;
};
struct authorization_data {
bdaddr_t src;
bdaddr_t dst;
};
static gint server_cmp(gconstpointer s, gconstpointer user_data)
{
const struct server *server = s;
const bdaddr_t *src = user_data;
return bacmp(&server->src, src);
}
static void auth_callback(DBusError *derr, void *user_data)
{
struct authorization_data *auth = user_data;
if (derr) {
error("Access denied: %s", derr->message);
input_device_close_channels(&auth->src, &auth->dst);
} else
input_device_connadd(&auth->src, &auth->dst);
g_free(auth);
}
2008-05-16 06:49:02 +08:00
static int authorize_device(const bdaddr_t *src, const bdaddr_t *dst)
{
struct authorization_data *auth;
auth = g_new0(struct authorization_data, 1);
bacpy(&auth->src, src);
bacpy(&auth->dst, dst);
return btd_request_authorization(src, dst, HID_UUID,
auth_callback, auth);
}
2008-05-16 06:34:08 +08:00
static void connect_event_cb(GIOChannel *chan, int err, const bdaddr_t *src,
const bdaddr_t *dst, gpointer data)
{
2008-05-16 06:34:08 +08:00
int sk, psm = GPOINTER_TO_UINT(data);
2007-01-22 07:23:19 +08:00
2008-05-16 06:34:08 +08:00
if (err < 0) {
error("accept: %s (%d)", strerror(-err), -err);
return;
}
2008-05-16 06:34:08 +08:00
sk = g_io_channel_unix_get_fd(chan);
debug("Incoming connection on PSM %d", psm);
2008-05-16 06:34:08 +08:00
if (input_device_set_channel(src, dst, psm, sk) < 0) {
/* Send unplug virtual cable to unknown devices */
if (psm == L2CAP_PSM_HIDP_CTRL) {
2007-06-24 03:45:13 +08:00
unsigned char unplug[] = { 0x15 };
int err;
2008-05-16 06:34:08 +08:00
err = write(sk, unplug, sizeof(unplug));
}
2007-01-22 07:23:19 +08:00
close(sk);
2008-05-16 06:34:08 +08:00
return;
2007-01-22 07:23:19 +08:00
}
2008-05-16 06:34:08 +08:00
if ((psm == L2CAP_PSM_HIDP_INTR) && (authorize_device(src, dst) < 0))
input_device_close_channels(src, dst);
2007-01-22 07:23:19 +08:00
2008-05-16 06:34:08 +08:00
return;
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 server *server;
GIOChannel *ctrl_io, *intr_io;
ctrl_io = bt_l2cap_listen(src, L2CAP_PSM_HIDP_CTRL, 0, 0,
2008-05-16 06:41:31 +08:00
connect_event_cb,
GUINT_TO_POINTER(L2CAP_PSM_HIDP_CTRL));
2007-01-24 23:43:06 +08:00
if (!ctrl_io) {
error("Failed to listen on control channel");
2007-01-22 07:23:19 +08:00
return -1;
2007-01-24 23:43:06 +08:00
}
g_io_channel_set_close_on_unref(ctrl_io, TRUE);
2007-01-22 07:23:19 +08:00
intr_io = bt_l2cap_listen(src, L2CAP_PSM_HIDP_INTR, 0, 0,
2008-05-16 06:41:31 +08:00
connect_event_cb,
GUINT_TO_POINTER(L2CAP_PSM_HIDP_INTR));
2007-01-22 07:23:19 +08:00
if (!intr_io) {
2007-01-24 23:43:06 +08:00
error("Failed to listen on interrupt channel");
2007-01-22 07:23:19 +08:00
g_io_channel_unref(ctrl_io);
return -1;
2007-01-22 07:23:19 +08:00
}
g_io_channel_set_close_on_unref(intr_io, TRUE);
2007-01-22 07:23:19 +08:00
server = g_new0(struct server, 1);
bacpy(&server->src, src);
server->ctrl = ctrl_io;
server->intr = intr_io;
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 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_unref(server->intr);
g_io_channel_unref(server->ctrl);
servers = g_slist_remove(servers, server);
g_free(server);
2007-01-22 07:23:19 +08:00
}