Path: blob/main/usr.sbin/bluetooth/bthidd/client.c
102447 views
/*1* client.c2*/34/*-5* SPDX-License-Identifier: BSD-2-Clause6*7* Copyright (c) 2006 Maksim Yevmenkin <[email protected]>8* All rights reserved.9*10* Redistribution and use in source and binary forms, with or without11* modification, are permitted provided that the following conditions12* are met:13* 1. Redistributions of source code must retain the above copyright14* notice, this list of conditions and the following disclaimer.15* 2. Redistributions in binary form must reproduce the above copyright16* notice, this list of conditions and the following disclaimer in the17* documentation and/or other materials provided with the distribution.18*19* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND20* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE21* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE22* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE23* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL24* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS25* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)26* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT27* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY28* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF29* SUCH DAMAGE.30*31* $Id: client.c,v 1.7 2006/09/07 21:06:53 max Exp $32*/3334#include <sys/queue.h>35#include <assert.h>36#define L2CAP_SOCKET_CHECKED37#include <bluetooth.h>38#include <errno.h>39#include <fcntl.h>40#include <stdio.h>41#include <stdlib.h>42#include <string.h>43#include <syslog.h>44#include <unistd.h>45#include <usbhid.h>46#include "bthid_config.h"47#include "bthidd.h"4849static int32_t client_socket(bdaddr_p bdaddr, uint16_t psm);5051/*52* Get next config entry and create outbound connection (if required)53*54* XXX Do only one device at a time. At least one of my devices (3COM55* Bluetooth PCCARD) rejects Create_Connection command if another56* Create_Connection command is still pending. Weird...57*/5859static int32_t connect_in_progress = 0;6061int32_t62client_rescan(bthid_server_p srv)63{64static hid_device_p d;65bthid_session_p s;6667assert(srv != NULL);6869if (connect_in_progress)70return (0); /* another connect is still pending */7172d = get_next_hid_device(d);73if (d == NULL)74return (0); /* XXX should not happen? empty config? */7576if ((s = session_by_bdaddr(srv, &d->bdaddr)) != NULL)77return (0); /* session already active */7879if (!d->new_device) {80if (d->reconnect_initiate)81return (0); /* device will initiate reconnect */82}8384syslog(LOG_NOTICE, "Opening outbound session for %s " \85"(new_device=%d, reconnect_initiate=%d)",86bt_ntoa(&d->bdaddr, NULL), d->new_device, d->reconnect_initiate);8788if ((s = session_open(srv, d)) == NULL) {89syslog(LOG_CRIT, "Could not create outbound session for %s",90bt_ntoa(&d->bdaddr, NULL));91return (-1);92}9394/* Open control channel */95s->ctrl = client_socket(&s->bdaddr, d->control_psm);96if (s->ctrl < 0) {97syslog(LOG_ERR, "Could not open control channel to %s. %s (%d)",98bt_ntoa(&s->bdaddr, NULL), strerror(errno), errno);99session_close(s);100return (-1);101}102103s->state = W4CTRL;104105FD_SET(s->ctrl, &srv->wfdset);106if (s->ctrl > srv->maxfd)107srv->maxfd = s->ctrl;108109connect_in_progress = 1;110111return (0);112}113114/*115* Process connect on the socket116*/117118int32_t119client_connect(bthid_server_p srv, int32_t fd)120{121bthid_session_p s;122hid_device_p d;123int32_t error;124socklen_t len;125126assert(srv != NULL);127assert(fd >= 0);128129s = session_by_fd(srv, fd);130assert(s != NULL);131132d = get_hid_device(&s->bdaddr);133assert(d != NULL);134135error = 0;136len = sizeof(error);137if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {138syslog(LOG_ERR, "Could not get socket error for %s. %s (%d)",139bt_ntoa(&s->bdaddr, NULL), strerror(errno), errno);140session_close(s);141connect_in_progress = 0;142143return (-1);144}145146if (error != 0) {147syslog(LOG_ERR, "Could not connect to %s. %s (%d)",148bt_ntoa(&s->bdaddr, NULL), strerror(error), error);149session_close(s);150connect_in_progress = 0;151152return (0);153}154155switch (s->state) {156case W4CTRL: /* Control channel is open */157assert(s->ctrl == fd);158assert(s->intr == -1);159160/* Open interrupt channel */161s->intr = client_socket(&s->bdaddr, d->interrupt_psm);162if (s->intr < 0) {163syslog(LOG_ERR, "Could not open interrupt channel " \164"to %s. %s (%d)", bt_ntoa(&s->bdaddr, NULL),165strerror(errno), errno);166session_close(s);167connect_in_progress = 0;168169return (-1);170}171172s->state = W4INTR;173174FD_SET(s->intr, &srv->wfdset);175if (s->intr > srv->maxfd)176srv->maxfd = s->intr;177178d->new_device = 0; /* reset new device flag */179write_hids_file();180break;181182case W4INTR: /* Interrupt channel is open */183assert(s->ctrl != -1);184assert(s->intr == fd);185186s->state = OPEN;187connect_in_progress = 0;188189/* Create kbd/mouse after both channels are established */190if (session_run(s) < 0) {191session_close(s);192return (-1);193}194break;195196default:197assert(0);198break;199}200201/* Move fd to from the write fd set into read fd set */202FD_CLR(fd, &srv->wfdset);203FD_SET(fd, &srv->rfdset);204205return (0);206}207208/*209* Create bound non-blocking socket and initiate connect210*/211212static int213client_socket(bdaddr_p bdaddr, uint16_t psm)214{215struct sockaddr_l2cap l2addr;216int32_t s, m;217218s = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BLUETOOTH_PROTO_L2CAP);219if (s < 0)220return (-1);221222m = fcntl(s, F_GETFL);223if (m < 0) {224close(s);225return (-1);226}227228if (fcntl(s, F_SETFL, (m|O_NONBLOCK)) < 0) {229close(s);230return (-1);231}232233l2addr.l2cap_len = sizeof(l2addr);234l2addr.l2cap_family = AF_BLUETOOTH;235memset(&l2addr.l2cap_bdaddr, 0, sizeof(l2addr.l2cap_bdaddr));236l2addr.l2cap_psm = 0;237l2addr.l2cap_bdaddr_type = BDADDR_BREDR;238l2addr.l2cap_cid = 0;239240if (bind(s, (struct sockaddr *) &l2addr, sizeof(l2addr)) < 0) {241close(s);242return (-1);243}244245memcpy(&l2addr.l2cap_bdaddr, bdaddr, sizeof(l2addr.l2cap_bdaddr));246l2addr.l2cap_psm = htole16(psm);247248if (connect(s, (struct sockaddr *) &l2addr, sizeof(l2addr)) < 0 &&249errno != EINPROGRESS) {250close(s);251return (-1);252}253254return (s);255}256257258259