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 /* Connect to remote device */
152 memset(&addr, 0, sizeof(addr));
153 addr.rc_family = AF_BLUETOOTH;
154 str2ba(svr, &addr.rc_bdaddr);
155 addr.rc_channel = channel;
157 if (connect(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
158 syslog(LOG_ERR, "Can't connect: %s (%d)",
159 strerror(errno), errno);
163 /* Get connection information */
164 memset(&conn, 0, sizeof(conn));
165 optlen = sizeof(conn);
167 if (getsockopt(sk, SOL_RFCOMM, RFCOMM_CONNINFO, &conn, &optlen) < 0) {
168 syslog(LOG_ERR, "Can't get RFCOMM connection information: %s (%d)",
169 strerror(errno), errno);
173 syslog(LOG_INFO, "Connected [handle %d, class 0x%02x%02x%02x]",
175 conn.dev_class[2], conn.dev_class[1], conn.dev_class[0]);
184 static void do_listen(void (*handler)(int sk))
186 struct sockaddr_rc addr;
187 struct rfcomm_conninfo conn;
193 sk = socket(PF_BLUETOOTH, socktype, BTPROTO_RFCOMM);
195 syslog(LOG_ERR, "Can't create socket: %s (%d)",
196 strerror(errno), errno);
200 /* Bind to local address */
201 addr.rc_family = AF_BLUETOOTH;
202 bacpy(&addr.rc_bdaddr, &bdaddr);
203 addr.rc_channel = channel;
205 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
206 syslog(LOG_ERR, "Can't bind socket: %s (%d)",
207 strerror(errno), errno);
214 opt |= RFCOMM_LM_MASTER;
216 opt |= RFCOMM_LM_AUTH;
218 opt |= RFCOMM_LM_ENCRYPT;
220 opt |= RFCOMM_LM_SECURE;
222 if (opt && setsockopt(sk, SOL_RFCOMM, RFCOMM_LM, &opt, sizeof(opt)) < 0) {
223 syslog(LOG_ERR, "Can't set RFCOMM link mode: %s (%d)",
224 strerror(errno), errno);
228 /* Listen for connections */
229 if (listen(sk, 10)) {
230 syslog(LOG_ERR,"Can not listen on the socket: %s (%d)",
231 strerror(errno), errno);
235 /* Check for socket address */
236 memset(&addr, 0, sizeof(addr));
237 optlen = sizeof(addr);
239 if (getsockname(sk, (struct sockaddr *) &addr, &optlen) < 0) {
240 syslog(LOG_ERR, "Can't get socket name: %s (%d)",
241 strerror(errno), errno);
245 channel = addr.rc_channel;
247 syslog(LOG_INFO, "Waiting for connection on channel %d ...", channel);
250 memset(&addr, 0, sizeof(addr));
251 optlen = sizeof(addr);
253 nsk = accept(sk, (struct sockaddr *) &addr, &optlen);
255 syslog(LOG_ERR,"Accept failed: %s (%d)",
256 strerror(errno), errno);
267 /* Get connection information */
268 memset(&conn, 0, sizeof(conn));
269 optlen = sizeof(conn);
271 if (getsockopt(nsk, SOL_RFCOMM, RFCOMM_CONNINFO, &conn, &optlen) < 0) {
272 syslog(LOG_ERR, "Can't get RFCOMM connection information: %s (%d)",
273 strerror(errno), errno);
278 ba2str(&addr.rc_bdaddr, ba);
279 syslog(LOG_INFO, "Connect from %s [handle %d, class 0x%02x%02x%02x]",
281 conn.dev_class[2], conn.dev_class[1], conn.dev_class[0]);
284 /* Enable SO_TIMESTAMP */
288 if (setsockopt(nsk, SOL_SOCKET, SO_TIMESTAMP, &t, sizeof(t)) < 0) {
289 syslog(LOG_ERR, "Can't enable SO_TIMESTAMP: %s (%d)",
290 strerror(errno), errno);
296 /* Enable SO_LINGER */
298 struct linger l = { .l_onoff = 1, .l_linger = linger };
300 if (setsockopt(nsk, SOL_SOCKET, SO_LINGER, &l, sizeof(l)) < 0) {
301 syslog(LOG_ERR, "Can't enable SO_LINGER: %s (%d)",
302 strerror(errno), errno);
310 syslog(LOG_INFO, "Disconnect: %m");
321 static void dump_mode(int sk)
325 syslog(LOG_INFO, "Receiving ...");
326 while ((len = read(sk, buf, data_size)) > 0)
327 syslog(LOG_INFO, "Recevied %d bytes", len);
330 static void recv_mode(int sk)
332 struct timeval tv_beg, tv_end, tv_diff;
337 syslog(LOG_INFO, "Receiving ...");
339 memset(ts, 0, sizeof(ts));
343 gettimeofday(&tv_beg,NULL);
345 while (total < data_size) {
350 if ((r = recv(sk, buf, data_size, 0)) <= 0) {
352 syslog(LOG_ERR, "Read failed: %s (%d)",
353 strerror(errno), errno);
360 if (ioctl(sk, SIOCGSTAMP, &tv) < 0) {
362 memset(ts, 0, sizeof(ts));
364 sprintf(ts, "[%ld.%ld] ",
365 tv.tv_sec, tv.tv_usec);
371 sq = btohl(*(uint32_t *) buf);
373 syslog(LOG_INFO, "seq missmatch: %d -> %d", seq, sq);
379 l = btohs(*(uint16_t *) (buf + 4));
381 syslog(LOG_INFO, "size missmatch: %d -> %d", r, l);
386 for (i = 6; i < r; i++) {
388 syslog(LOG_INFO, "data missmatch: byte %d 0x%2.2x", i, buf[i]);
393 gettimeofday(&tv_end,NULL);
395 timersub(&tv_end,&tv_beg,&tv_diff);
397 syslog(LOG_INFO,"%s%ld bytes in %.2f sec, %.2f kB/s", ts, total,
398 tv2fl(tv_diff), (float)(total / tv2fl(tv_diff) ) / 1024.0);
402 static void do_send(int sk)
407 syslog(LOG_INFO,"Sending ...");
410 fd = open(filename, O_RDONLY);
412 syslog(LOG_ERR, "Open failed: %s (%d)",
413 strerror(errno), errno);
416 len = read(fd, buf, data_size);
417 send(sk, buf, len, 0);
420 for (i = 6; i < data_size; i++)
425 while ((num_frames == -1) || (num_frames-- > 0)) {
426 *(uint32_t *) buf = htobl(seq);
427 *(uint16_t *) (buf + 4) = htobs(data_size);
430 if (send(sk, buf, data_size, 0) <= 0) {
431 syslog(LOG_ERR, "Send failed: %s (%d)",
432 strerror(errno), errno);
436 if (num_frames && delay && count && !(seq % count))
441 static void send_mode(int sk)
445 syslog(LOG_INFO, "Closing channel ...");
446 if (shutdown(sk, SHUT_RDWR) < 0)
447 syslog(LOG_INFO, "Close failed: %m");
449 syslog(LOG_INFO, "Done");
452 static void reconnect_mode(char *svr)
455 int sk = do_connect(svr);
460 static void multi_connect_mode(int argc, char *argv[])
465 for (n = 0; n < argc; n++) {
466 for (i = 0; i < count; i++) {
471 sk = do_connect(argv[n]);
481 static void usage(void)
483 printf("rctest - RFCOMM testing\n"
485 printf("\trctest <mode> [options] [bdaddr]\n");
487 "\t-r listen and receive\n"
488 "\t-w listen and send\n"
489 "\t-d listen and dump incoming data\n"
490 "\t-s connect and send\n"
491 "\t-u connect and receive\n"
492 "\t-n connect and be silent\n"
493 "\t-c connect, disconnect, connect, ...\n"
494 "\t-m multiple connects\n");
497 "\t[-b bytes] [-i device] [-P channel]\n"
498 "\t[-L seconds] enabled SO_LINGER option\n"
499 "\t[-B filename] use data packets from file\n"
500 "\t[-N num] number of frames to send\n"
501 "\t[-C num] send num frames before delay (default = 1)\n"
502 "\t[-D milliseconds] delay after sending num frames (default = 0)\n"
503 "\t[-A] request authentication\n"
504 "\t[-E] request encryption\n"
505 "\t[-S] secure connection\n"
506 "\t[-M] become master\n"
507 "\t[-T] enable timestamps\n");
510 int main(int argc, char *argv[])
513 int opt, sk, mode = RECV, need_addr = 0;
515 bacpy(&bdaddr, BDADDR_ANY);
517 while ((opt=getopt(argc,argv,"rdscuwmnb:i:P:B:N:MAESL:C:D:T")) != EOF) {
557 data_size = atoi(optarg);
561 if (!strncasecmp(optarg, "hci", 3))
562 hci_devba(atoi(optarg + 3), &bdaddr);
564 str2ba(optarg, &bdaddr);
568 channel = atoi(optarg);
588 linger = atoi(optarg);
592 filename = strdup(optarg);
596 num_frames = atoi(optarg);
600 count = atoi(optarg);
604 delay = atoi(optarg) * 1000;
617 if (need_addr && !(argc - optind)) {
622 if (!(buf = malloc(data_size))) {
623 perror("Can't allocate data buffer");
627 memset(&sa, 0, sizeof(sa));
628 sa.sa_handler = SIG_IGN;
629 sa.sa_flags = SA_NOCLDSTOP;
630 sigaction(SIGCHLD, &sa, NULL);
632 openlog("rctest", LOG_PERROR | LOG_PID, LOG_LOCAL0);
636 do_listen(recv_mode);
640 sk = do_connect(argv[optind]);
647 do_listen(dump_mode);
651 sk = do_connect(argv[optind]);
658 do_listen(send_mode);
662 reconnect_mode(argv[optind]);
666 multi_connect_mode(argc - optind, argv + optind);
670 sk = do_connect(argv[optind]);
677 syslog(LOG_INFO, "Exit");