2004-04-10 00:18:00 +00:00
|
|
|
/*
|
|
|
|
* session.c
|
2006-09-07 21:47:49 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 2006 Maksim Yevmenkin <m_evmenkin@yahoo.com>
|
2004-04-10 00:18:00 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
2006-09-07 21:47:49 +00:00
|
|
|
* $Id: session.c,v 1.3 2006/09/07 21:06:53 max Exp $
|
2004-04-10 00:18:00 +00:00
|
|
|
* $FreeBSD$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/queue.h>
|
|
|
|
#include <assert.h>
|
2015-04-07 16:48:23 +00:00
|
|
|
#define L2CAP_SOCKET_CHECKED
|
2004-04-10 00:18:00 +00:00
|
|
|
#include <bluetooth.h>
|
2006-09-07 21:47:49 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2004-04-10 00:18:00 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2006-09-07 21:47:49 +00:00
|
|
|
#include <syslog.h>
|
2004-04-10 00:18:00 +00:00
|
|
|
#include <unistd.h>
|
2006-09-07 21:47:49 +00:00
|
|
|
#include <usbhid.h>
|
|
|
|
#include "bthid_config.h"
|
2004-04-10 00:18:00 +00:00
|
|
|
#include "bthidd.h"
|
2004-11-18 18:05:15 +00:00
|
|
|
#include "kbd.h"
|
2004-04-10 00:18:00 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Create new session
|
|
|
|
*/
|
|
|
|
|
|
|
|
bthid_session_p
|
2006-09-07 21:47:49 +00:00
|
|
|
session_open(bthid_server_p srv, hid_device_p const d)
|
2004-04-10 00:18:00 +00:00
|
|
|
{
|
2006-09-07 21:47:49 +00:00
|
|
|
bthid_session_p s;
|
2004-04-10 00:18:00 +00:00
|
|
|
|
|
|
|
assert(srv != NULL);
|
2006-09-07 21:47:49 +00:00
|
|
|
assert(d != NULL);
|
|
|
|
|
|
|
|
if ((s = (bthid_session_p) malloc(sizeof(*s))) == NULL)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
s->srv = srv;
|
|
|
|
memcpy(&s->bdaddr, &d->bdaddr, sizeof(s->bdaddr));
|
|
|
|
s->ctrl = -1;
|
|
|
|
s->intr = -1;
|
|
|
|
|
|
|
|
if (d->keyboard) {
|
|
|
|
/* Open /dev/vkbdctl */
|
|
|
|
s->vkbd = open("/dev/vkbdctl", O_RDWR);
|
|
|
|
if (s->vkbd < 0) {
|
|
|
|
syslog(LOG_ERR, "Could not open /dev/vkbdctl " \
|
|
|
|
"for %s. %s (%d)", bt_ntoa(&s->bdaddr, NULL),
|
|
|
|
strerror(errno), errno);
|
2004-11-18 18:05:15 +00:00
|
|
|
free(s);
|
2006-09-07 21:47:49 +00:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
s->vkbd = -1;
|
|
|
|
|
|
|
|
s->state = CLOSED;
|
|
|
|
|
|
|
|
s->keys1 = bit_alloc(kbd_maxkey());
|
|
|
|
if (s->keys1 == NULL) {
|
|
|
|
free(s);
|
|
|
|
return (NULL);
|
2004-04-10 00:18:00 +00:00
|
|
|
}
|
|
|
|
|
2006-09-07 21:47:49 +00:00
|
|
|
s->keys2 = bit_alloc(kbd_maxkey());
|
|
|
|
if (s->keys2 == NULL) {
|
|
|
|
free(s->keys1);
|
|
|
|
free(s);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
LIST_INSERT_HEAD(&srv->sessions, s, next);
|
|
|
|
|
2004-04-10 00:18:00 +00:00
|
|
|
return (s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Lookup session by bdaddr
|
|
|
|
*/
|
|
|
|
|
|
|
|
bthid_session_p
|
|
|
|
session_by_bdaddr(bthid_server_p srv, bdaddr_p bdaddr)
|
|
|
|
{
|
2006-09-07 21:47:49 +00:00
|
|
|
bthid_session_p s;
|
2004-04-10 00:18:00 +00:00
|
|
|
|
|
|
|
assert(srv != NULL);
|
|
|
|
assert(bdaddr != NULL);
|
|
|
|
|
|
|
|
LIST_FOREACH(s, &srv->sessions, next)
|
|
|
|
if (memcmp(&s->bdaddr, bdaddr, sizeof(s->bdaddr)) == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
return (s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Lookup session by fd
|
|
|
|
*/
|
|
|
|
|
|
|
|
bthid_session_p
|
2006-09-07 21:47:49 +00:00
|
|
|
session_by_fd(bthid_server_p srv, int32_t fd)
|
2004-04-10 00:18:00 +00:00
|
|
|
{
|
2006-09-07 21:47:49 +00:00
|
|
|
bthid_session_p s;
|
2004-04-10 00:18:00 +00:00
|
|
|
|
|
|
|
assert(srv != NULL);
|
|
|
|
assert(fd >= 0);
|
|
|
|
|
|
|
|
LIST_FOREACH(s, &srv->sessions, next)
|
2006-09-07 21:47:49 +00:00
|
|
|
if (s->ctrl == fd || s->intr == fd || s->vkbd == fd)
|
2004-04-10 00:18:00 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
return (s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Close session
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
|
|
|
session_close(bthid_session_p s)
|
|
|
|
{
|
|
|
|
assert(s != NULL);
|
|
|
|
assert(s->srv != NULL);
|
|
|
|
|
|
|
|
LIST_REMOVE(s, next);
|
|
|
|
|
|
|
|
if (s->intr != -1) {
|
|
|
|
FD_CLR(s->intr, &s->srv->rfdset);
|
|
|
|
FD_CLR(s->intr, &s->srv->wfdset);
|
|
|
|
close(s->intr);
|
|
|
|
|
|
|
|
if (s->srv->maxfd == s->intr)
|
|
|
|
s->srv->maxfd --;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s->ctrl != -1) {
|
|
|
|
FD_CLR(s->ctrl, &s->srv->rfdset);
|
|
|
|
FD_CLR(s->ctrl, &s->srv->wfdset);
|
|
|
|
close(s->ctrl);
|
|
|
|
|
|
|
|
if (s->srv->maxfd == s->ctrl)
|
|
|
|
s->srv->maxfd --;
|
|
|
|
}
|
|
|
|
|
2006-09-07 21:47:49 +00:00
|
|
|
if (s->vkbd != -1) {
|
|
|
|
FD_CLR(s->vkbd, &s->srv->rfdset);
|
|
|
|
close(s->vkbd);
|
|
|
|
|
|
|
|
if (s->srv->maxfd == s->vkbd)
|
|
|
|
s->srv->maxfd --;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(s->keys1);
|
|
|
|
free(s->keys2);
|
2004-11-18 18:05:15 +00:00
|
|
|
|
2004-04-10 00:18:00 +00:00
|
|
|
memset(s, 0, sizeof(*s));
|
|
|
|
free(s);
|
|
|
|
}
|
|
|
|
|