3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2002-2003 Maxim Krasnyansky <maxk@qualcomm.com>
6 * Copyright (C) 2002-2008 Marcel Holtmann <marcel@holtmann.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
39 #include <sys/ioctl.h>
40 #include <sys/socket.h>
42 #include <bluetooth/bluetooth.h>
43 #include <bluetooth/hci.h>
44 #include <bluetooth/hci_lib.h>
45 #include <bluetooth/rfcomm.h>
59 static unsigned char *buf;
61 /* Default data size */
62 static long data_size = 127;
63 static long num_frames = -1;
65 /* Default number of consecutive frames before the delay */
68 /* Default delay after sending count number of frames */
69 static unsigned long delay = 0;
71 /* Default addr and channel */
72 static bdaddr_t bdaddr;
73 static uint8_t channel = 10;
75 static char *filename = NULL;
77 static int master = 0;
79 static int encrypt = 0;
80 static int secure = 0;
81 static int socktype = SOCK_STREAM;
82 static int linger = 0;
83 static int timestamp = 0;
85 static float tv2fl(struct timeval tv)
87 return (float)tv.tv_sec + (float)(tv.tv_usec/1000000.0);
90 static int do_connect(char *svr)
92 struct sockaddr_rc addr;
93 struct rfcomm_conninfo conn;
98 sk = socket(PF_BLUETOOTH, socktype, BTPROTO_RFCOMM);
100 syslog(LOG_ERR, "Can't create socket: %s (%d)",
101 strerror(errno), errno);
105 /* Bind to local address */
106 memset(&addr, 0, sizeof(addr));
107 addr.rc_family = AF_BLUETOOTH;
108 bacpy(&addr.rc_bdaddr, &bdaddr);
110 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
111 syslog(LOG_ERR, "Can't bind socket: %s (%d)",
112 strerror(errno), errno);
117 /* Enable SO_TIMESTAMP */
121 if (setsockopt(sk, SOL_SOCKET, SO_TIMESTAMP, &t, sizeof(t)) < 0) {
122 syslog(LOG_ERR, "Can't enable SO_TIMESTAMP: %s (%d)",
123 strerror(errno), errno);
129 /* Enable SO_LINGER */
131 struct linger l = { .l_onoff = 1, .l_linger = linger };
133 if (setsockopt(sk, SOL_SOCKET, SO_LINGER, &l, sizeof(l)) < 0) {
134 syslog(LOG_ERR, "Can't enable SO_LINGER: %s (%d)",
135 strerror(errno), errno);
143 opt |= RFCOMM_LM_MASTER;
145 opt |= RFCOMM_LM_AUTH;
147 opt |= RFCOMM_LM_ENCRYPT;
149 opt |= RFCOMM_LM_SECURE;
151 if (opt && setsockopt(sk, SOL_RFCOMM, RFCOMM_LM, &opt, sizeof(opt)) < 0) {
152 syslog(LOG_ERR, "Can't set RFCOMM link mode: %s (%d)",
153 strerror(errno), errno);
157 /* Connect to remote device */
158 memset(&addr, 0, sizeof(addr));
159 addr.rc_family = AF_BLUETOOTH;
160 str2ba(svr, &addr.rc_bdaddr);
161 addr.rc_channel = channel;
163 if (connect(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
164 syslog(LOG_ERR, "Can't connect: %s (%d)",
165 strerror(errno), errno);
169 /* Get connection information */
170 memset(&conn, 0, sizeof(conn));
171 optlen = sizeof(conn);
173 if (getsockopt(sk, SOL_RFCOMM, RFCOMM_CONNINFO, &conn, &optlen) < 0) {
174 syslog(LOG_ERR, "Can't get RFCOMM connection information: %s (%d)",
175 strerror(errno), errno);
179 syslog(LOG_INFO, "Connected [handle %d, class 0x%02x%02x%02x]",
181 conn.dev_class[2], conn.dev_class[1], conn.dev_class[0]);
190 static void do_listen(void (*handler)(int sk))
192 struct sockaddr_rc addr;
193 struct rfcomm_conninfo conn;
199 sk = socket(PF_BLUETOOTH, socktype, BTPROTO_RFCOMM);
201 syslog(LOG_ERR, "Can't create socket: %s (%d)",
202 strerror(errno), errno);
206 /* Bind to local address */
207 addr.rc_family = AF_BLUETOOTH;
208 bacpy(&addr.rc_bdaddr, &bdaddr);
209 addr.rc_channel = channel;
211 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
212 syslog(LOG_ERR, "Can't bind socket: %s (%d)",
213 strerror(errno), errno);
220 opt |= RFCOMM_LM_MASTER;
222 opt |= RFCOMM_LM_AUTH;
224 opt |= RFCOMM_LM_ENCRYPT;
226 opt |= RFCOMM_LM_SECURE;
228 if (opt && setsockopt(sk, SOL_RFCOMM, RFCOMM_LM, &opt, sizeof(opt)) < 0) {
229 syslog(LOG_ERR, "Can't set RFCOMM link mode: %s (%d)",
230 strerror(errno), errno);
234 /* Listen for connections */
235 if (listen(sk, 10)) {
236 syslog(LOG_ERR,"Can not listen on the socket: %s (%d)",
237 strerror(errno), errno);
241 /* Check for socket address */
242 memset(&addr, 0, sizeof(addr));
243 optlen = sizeof(addr);
245 if (getsockname(sk, (struct sockaddr *) &addr, &optlen) < 0) {
246 syslog(LOG_ERR, "Can't get socket name: %s (%d)",
247 strerror(errno), errno);
251 channel = addr.rc_channel;
253 syslog(LOG_INFO, "Waiting for connection on channel %d ...", channel);
256 memset(&addr, 0, sizeof(addr));
257 optlen = sizeof(addr);
259 nsk = accept(sk, (struct sockaddr *) &addr, &optlen);
261 syslog(LOG_ERR,"Accept failed: %s (%d)",
262 strerror(errno), errno);
273 /* Get connection information */
274 memset(&conn, 0, sizeof(conn));
275 optlen = sizeof(conn);
277 if (getsockopt(nsk, SOL_RFCOMM, RFCOMM_CONNINFO, &conn, &optlen) < 0) {
278 syslog(LOG_ERR, "Can't get RFCOMM connection information: %s (%d)",
279 strerror(errno), errno);
284 ba2str(&addr.rc_bdaddr, ba);
285 syslog(LOG_INFO, "Connect from %s [handle %d, class 0x%02x%02x%02x]",
287 conn.dev_class[2], conn.dev_class[1], conn.dev_class[0]);
290 /* Enable SO_TIMESTAMP */
294 if (setsockopt(nsk, SOL_SOCKET, SO_TIMESTAMP, &t, sizeof(t)) < 0) {
295 syslog(LOG_ERR, "Can't enable SO_TIMESTAMP: %s (%d)",
296 strerror(errno), errno);
302 /* Enable SO_LINGER */
304 struct linger l = { .l_onoff = 1, .l_linger = linger };
306 if (setsockopt(nsk, SOL_SOCKET, SO_LINGER, &l, sizeof(l)) < 0) {
307 syslog(LOG_ERR, "Can't enable SO_LINGER: %s (%d)",
308 strerror(errno), errno);
316 syslog(LOG_INFO, "Disconnect: %m");
327 static void dump_mode(int sk)
331 syslog(LOG_INFO, "Receiving ...");
332 while ((len = read(sk, buf, data_size)) > 0)
333 syslog(LOG_INFO, "Recevied %d bytes", len);
336 static void recv_mode(int sk)
338 struct timeval tv_beg, tv_end, tv_diff;
343 syslog(LOG_INFO, "Receiving ...");
345 memset(ts, 0, sizeof(ts));
349 gettimeofday(&tv_beg,NULL);
351 while (total < data_size) {
356 if ((r = recv(sk, buf, data_size, 0)) <= 0) {
358 syslog(LOG_ERR, "Read failed: %s (%d)",
359 strerror(errno), errno);
366 if (ioctl(sk, SIOCGSTAMP, &tv) < 0) {
368 memset(ts, 0, sizeof(ts));
370 sprintf(ts, "[%ld.%ld] ",
371 tv.tv_sec, tv.tv_usec);
377 sq = btohl(*(uint32_t *) buf);
379 syslog(LOG_INFO, "seq missmatch: %d -> %d", seq, sq);
385 l = btohs(*(uint16_t *) (buf + 4));
387 syslog(LOG_INFO, "size missmatch: %d -> %d", r, l);
392 for (i = 6; i < r; i++) {
394 syslog(LOG_INFO, "data missmatch: byte %d 0x%2.2x", i, buf[i]);
399 gettimeofday(&tv_end,NULL);
401 timersub(&tv_end,&tv_beg,&tv_diff);
403 syslog(LOG_INFO,"%s%ld bytes in %.2f sec, %.2f kB/s", ts, total,
404 tv2fl(tv_diff), (float)(total / tv2fl(tv_diff) ) / 1024.0);
408 static void do_send(int sk)
413 syslog(LOG_INFO,"Sending ...");
416 fd = open(filename, O_RDONLY);
418 syslog(LOG_ERR, "Open failed: %s (%d)",
419 strerror(errno), errno);
422 len = read(fd, buf, data_size);
423 send(sk, buf, len, 0);
426 for (i = 6; i < data_size; i++)
431 while ((num_frames == -1) || (num_frames-- > 0)) {
432 *(uint32_t *) buf = htobl(seq);
433 *(uint16_t *) (buf + 4) = htobs(data_size);
436 if (send(sk, buf, data_size, 0) <= 0) {
437 syslog(LOG_ERR, "Send failed: %s (%d)",
438 strerror(errno), errno);
442 if (num_frames && delay && count && !(seq % count))
447 static void send_mode(int sk)
451 syslog(LOG_INFO, "Closing channel ...");
452 if (shutdown(sk, SHUT_RDWR) < 0)
453 syslog(LOG_INFO, "Close failed: %m");
455 syslog(LOG_INFO, "Done");
458 static void reconnect_mode(char *svr)
461 int sk = do_connect(svr);
466 static void multi_connect_mode(int argc, char *argv[])
471 for (n = 0; n < argc; n++) {
472 for (i = 0; i < count; i++) {
477 sk = do_connect(argv[n]);
487 static void usage(void)
489 printf("rctest - RFCOMM testing\n"
491 printf("\trctest <mode> [options] [bdaddr]\n");
493 "\t-r listen and receive\n"
494 "\t-w listen and send\n"
495 "\t-d listen and dump incoming data\n"
496 "\t-s connect and send\n"
497 "\t-u connect and receive\n"
498 "\t-n connect and be silent\n"
499 "\t-c connect, disconnect, connect, ...\n"
500 "\t-m multiple connects\n");
503 "\t[-b bytes] [-i device] [-P channel]\n"
504 "\t[-L seconds] enabled SO_LINGER option\n"
505 "\t[-B filename] use data packets from file\n"
506 "\t[-N num] number of frames to send\n"
507 "\t[-C num] send num frames before delay (default = 1)\n"
508 "\t[-D milliseconds] delay after sending num frames (default = 0)\n"
509 "\t[-A] request authentication\n"
510 "\t[-E] request encryption\n"
511 "\t[-S] secure connection\n"
512 "\t[-M] become master\n"
513 "\t[-T] enable timestamps\n");
516 int main(int argc, char *argv[])
519 int opt, sk, mode = RECV, need_addr = 0;
521 bacpy(&bdaddr, BDADDR_ANY);
523 while ((opt=getopt(argc,argv,"rdscuwmnb:i:P:B:N:MAESL:C:D:T")) != EOF) {
563 data_size = atoi(optarg);
567 if (!strncasecmp(optarg, "hci", 3))
568 hci_devba(atoi(optarg + 3), &bdaddr);
570 str2ba(optarg, &bdaddr);
574 channel = atoi(optarg);
594 linger = atoi(optarg);
598 filename = strdup(optarg);
602 num_frames = atoi(optarg);
606 count = atoi(optarg);
610 delay = atoi(optarg) * 1000;
623 if (need_addr && !(argc - optind)) {
628 if (!(buf = malloc(data_size))) {
629 perror("Can't allocate data buffer");
633 memset(&sa, 0, sizeof(sa));
634 sa.sa_handler = SIG_IGN;
635 sa.sa_flags = SA_NOCLDSTOP;
636 sigaction(SIGCHLD, &sa, NULL);
638 openlog("rctest", LOG_PERROR | LOG_PID, LOG_LOCAL0);
642 do_listen(recv_mode);
646 sk = do_connect(argv[optind]);
653 do_listen(dump_mode);
657 sk = do_connect(argv[optind]);
664 do_listen(send_mode);
668 reconnect_mode(argv[optind]);
672 multi_connect_mode(argc - optind, argv + optind);
676 sk = do_connect(argv[optind]);
683 syslog(LOG_INFO, "Exit");