server.c (128080) | server.c (137868) |
---|---|
1/* 2 * server.c 3 * 4 * Copyright (c) 2004 Maksim Yevmenkin <m_evmenkin@yahoo.com> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions --- 11 unchanged lines hidden (view full) --- 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * | 1/* 2 * server.c 3 * 4 * Copyright (c) 2004 Maksim Yevmenkin <m_evmenkin@yahoo.com> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions --- 11 unchanged lines hidden (view full) --- 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * |
28 * $Id: server.c,v 1.5 2004/02/26 21:43:36 max Exp $ 29 * $FreeBSD: head/usr.sbin/bluetooth/bthidd/server.c 128080 2004-04-10 00:18:00Z emax $ | 28 * $Id: server.c,v 1.7 2004/11/17 21:59:42 max Exp $ 29 * $FreeBSD: head/usr.sbin/bluetooth/bthidd/server.c 137868 2004-11-18 18:05:15Z emax $ |
30 */ 31 32#include <sys/queue.h> 33#include <assert.h> 34#include <bluetooth.h> 35#include <errno.h> 36#include <fcntl.h> 37#include <stdio.h> 38#include <stdlib.h> 39#include <string.h> 40#include <syslog.h> 41#include <unistd.h> 42#include <usbhid.h> 43#include "bthidd.h" 44#include "bthid_config.h" | 30 */ 31 32#include <sys/queue.h> 33#include <assert.h> 34#include <bluetooth.h> 35#include <errno.h> 36#include <fcntl.h> 37#include <stdio.h> 38#include <stdlib.h> 39#include <string.h> 40#include <syslog.h> 41#include <unistd.h> 42#include <usbhid.h> 43#include "bthidd.h" 44#include "bthid_config.h" |
45#include "kbd.h" |
|
45 46#undef max 47#define max(x, y) (((x) > (y))? (x) : (y)) 48 49static int server_accept (bthid_server_p srv, int fd); 50static int server_process(bthid_server_p srv, int fd); 51 52/* --- 7 unchanged lines hidden (view full) --- 60 61 assert(srv != NULL); 62 63 srv->ctrl = srv->intr = -1; 64 FD_ZERO(&srv->rfdset); 65 FD_ZERO(&srv->wfdset); 66 LIST_INIT(&srv->sessions); 67 | 46 47#undef max 48#define max(x, y) (((x) > (y))? (x) : (y)) 49 50static int server_accept (bthid_server_p srv, int fd); 51static int server_process(bthid_server_p srv, int fd); 52 53/* --- 7 unchanged lines hidden (view full) --- 61 62 assert(srv != NULL); 63 64 srv->ctrl = srv->intr = -1; 65 FD_ZERO(&srv->rfdset); 66 FD_ZERO(&srv->wfdset); 67 LIST_INIT(&srv->sessions); 68 |
69 /* Allocate HID keycodes buffer */ 70 srv->keys = bit_alloc(kbd_maxkey()); 71 if (srv->keys == NULL) { 72 syslog(LOG_ERR, "Could not allocate HID keys buffer"); 73 return (-1); 74 } 75 memset(srv->keys, 0, bitstr_size(kbd_maxkey())); 76 77 /* Get wired keyboard index (if was not specified) */ 78 if (srv->windex == -1) { 79 srv->windex = kbd_get_index("/dev/console"); 80 if (srv->windex < 0) { 81 syslog(LOG_ERR, "Could not open get wired keyboard " \ 82 "index. %s (%d)", strerror(errno), errno); 83 free(srv->keys); 84 return (-1); 85 } 86 } 87 |
|
68 /* Open /dev/consolectl */ 69 srv->cons = open("/dev/consolectl", O_RDWR); 70 if (srv->cons < 0) { 71 syslog(LOG_ERR, "Could not open /dev/consolectl. %s (%d)", 72 strerror(errno), errno); 73 return (-1); 74 } 75 | 88 /* Open /dev/consolectl */ 89 srv->cons = open("/dev/consolectl", O_RDWR); 90 if (srv->cons < 0) { 91 syslog(LOG_ERR, "Could not open /dev/consolectl. %s (%d)", 92 strerror(errno), errno); 93 return (-1); 94 } 95 |
96 /* Open /dev/vkbdctl */ 97 srv->vkbd = open("/dev/vkbdctl", O_RDWR); 98 if (srv->vkbd < 0) { 99 syslog(LOG_ERR, "Could not open /dev/vkbdctl. %s (%d)", 100 strerror(errno), errno); 101 close(srv->cons); 102 free(srv->keys); 103 return (-1); 104 } 105 |
|
76 /* Create control socket */ 77 srv->ctrl = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BLUETOOTH_PROTO_L2CAP); 78 if (srv->ctrl < 0) { 79 syslog(LOG_ERR, "Could not create control L2CAP socket. " \ 80 "%s (%d)", strerror(errno), errno); | 106 /* Create control socket */ 107 srv->ctrl = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BLUETOOTH_PROTO_L2CAP); 108 if (srv->ctrl < 0) { 109 syslog(LOG_ERR, "Could not create control L2CAP socket. " \ 110 "%s (%d)", strerror(errno), errno); |
111 close(srv->vkbd); |
|
81 close(srv->cons); | 112 close(srv->cons); |
113 free(srv->keys); |
|
82 return (-1); 83 } 84 85 l2addr.l2cap_len = sizeof(l2addr); 86 l2addr.l2cap_family = AF_BLUETOOTH; 87 memcpy(&l2addr.l2cap_bdaddr, &srv->bdaddr, sizeof(l2addr.l2cap_bdaddr)); 88 l2addr.l2cap_psm = 0x11; 89 90 if (bind(srv->ctrl, (struct sockaddr *) &l2addr, sizeof(l2addr)) < 0) { 91 syslog(LOG_ERR, "Could not bind control L2CAP socket. " \ 92 "%s (%d)", strerror(errno), errno); | 114 return (-1); 115 } 116 117 l2addr.l2cap_len = sizeof(l2addr); 118 l2addr.l2cap_family = AF_BLUETOOTH; 119 memcpy(&l2addr.l2cap_bdaddr, &srv->bdaddr, sizeof(l2addr.l2cap_bdaddr)); 120 l2addr.l2cap_psm = 0x11; 121 122 if (bind(srv->ctrl, (struct sockaddr *) &l2addr, sizeof(l2addr)) < 0) { 123 syslog(LOG_ERR, "Could not bind control L2CAP socket. " \ 124 "%s (%d)", strerror(errno), errno); |
125 close(srv->ctrl); 126 close(srv->vkbd); |
|
93 close(srv->cons); | 127 close(srv->cons); |
128 free(srv->keys); |
|
94 return (-1); 95 } 96 97 if (listen(srv->ctrl, 10) < 0) { 98 syslog(LOG_ERR, "Could not listen on control L2CAP socket. " \ 99 "%s (%d)", strerror(errno), errno); | 129 return (-1); 130 } 131 132 if (listen(srv->ctrl, 10) < 0) { 133 syslog(LOG_ERR, "Could not listen on control L2CAP socket. " \ 134 "%s (%d)", strerror(errno), errno); |
135 close(srv->ctrl); 136 close(srv->vkbd); |
|
100 close(srv->cons); | 137 close(srv->cons); |
138 free(srv->keys); |
|
101 return (-1); 102 } 103 104 /* Create intrrupt socket */ 105 srv->intr = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BLUETOOTH_PROTO_L2CAP); 106 if (srv->intr < 0) { 107 syslog(LOG_ERR, "Could not create interrupt L2CAP socket. " \ 108 "%s (%d)", strerror(errno), errno); 109 close(srv->ctrl); | 139 return (-1); 140 } 141 142 /* Create intrrupt socket */ 143 srv->intr = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BLUETOOTH_PROTO_L2CAP); 144 if (srv->intr < 0) { 145 syslog(LOG_ERR, "Could not create interrupt L2CAP socket. " \ 146 "%s (%d)", strerror(errno), errno); 147 close(srv->ctrl); |
148 close(srv->vkbd); |
|
110 close(srv->cons); | 149 close(srv->cons); |
150 free(srv->keys); |
|
111 return (-1); 112 } 113 114 l2addr.l2cap_psm = 0x13; 115 116 if (bind(srv->intr, (struct sockaddr *) &l2addr, sizeof(l2addr)) < 0) { 117 syslog(LOG_ERR, "Could not bind interrupt L2CAP socket. " \ 118 "%s (%d)", strerror(errno), errno); | 151 return (-1); 152 } 153 154 l2addr.l2cap_psm = 0x13; 155 156 if (bind(srv->intr, (struct sockaddr *) &l2addr, sizeof(l2addr)) < 0) { 157 syslog(LOG_ERR, "Could not bind interrupt L2CAP socket. " \ 158 "%s (%d)", strerror(errno), errno); |
159 close(srv->intr); |
|
119 close(srv->ctrl); | 160 close(srv->ctrl); |
161 close(srv->vkbd); |
|
120 close(srv->cons); | 162 close(srv->cons); |
163 free(srv->keys); |
|
121 return (-1); 122 } 123 124 if (listen(srv->intr, 10) < 0) { 125 syslog(LOG_ERR, "Could not listen on interrupt L2CAP socket. "\ 126 "%s (%d)", strerror(errno), errno); | 164 return (-1); 165 } 166 167 if (listen(srv->intr, 10) < 0) { 168 syslog(LOG_ERR, "Could not listen on interrupt L2CAP socket. "\ 169 "%s (%d)", strerror(errno), errno); |
170 close(srv->intr); |
|
127 close(srv->ctrl); | 171 close(srv->ctrl); |
172 close(srv->vkbd); |
|
128 close(srv->cons); | 173 close(srv->cons); |
174 free(srv->keys); |
|
129 return (-1); 130 } 131 132 FD_SET(srv->ctrl, &srv->rfdset); 133 FD_SET(srv->intr, &srv->rfdset); 134 srv->maxfd = max(srv->ctrl, srv->intr); 135 136 return (0); --- 4 unchanged lines hidden (view full) --- 141 */ 142 143void 144server_shutdown(bthid_server_p srv) 145{ 146 assert(srv != NULL); 147 148 close(srv->cons); | 175 return (-1); 176 } 177 178 FD_SET(srv->ctrl, &srv->rfdset); 179 FD_SET(srv->intr, &srv->rfdset); 180 srv->maxfd = max(srv->ctrl, srv->intr); 181 182 return (0); --- 4 unchanged lines hidden (view full) --- 187 */ 188 189void 190server_shutdown(bthid_server_p srv) 191{ 192 assert(srv != NULL); 193 194 close(srv->cons); |
195 close(srv->vkbd); |
|
149 close(srv->ctrl); 150 close(srv->intr); 151 152 while (!LIST_EMPTY(&srv->sessions)) 153 session_close(LIST_FIRST(&srv->sessions)); 154 | 196 close(srv->ctrl); 197 close(srv->intr); 198 199 while (!LIST_EMPTY(&srv->sessions)) 200 session_close(LIST_FIRST(&srv->sessions)); 201 |
202 free(srv->keys); 203 |
|
155 memset(srv, 0, sizeof(*srv)); 156} 157 158/* 159 * Do one server iteration 160 */ 161 162int --- 154 unchanged lines hidden --- | 204 memset(srv, 0, sizeof(*srv)); 205} 206 207/* 208 * Do one server iteration 209 */ 210 211int --- 154 unchanged lines hidden --- |