3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2000-2001 Qualcomm Incorporated
6 * Copyright (C) 2002-2003 Maxim Krasnyansky <maxk@qualcomm.com>
7 * Copyright (C) 2002-2010 Marcel Holtmann <marcel@holtmann.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
39 #include <sys/socket.h>
41 #include "lib/bluetooth.h"
43 #include "lib/hci_lib.h"
44 #include "lib/l2cap.h"
47 static bdaddr_t bdaddr;
49 static int ident = 200;
51 static int count = -1;
52 static int timeout = 10;
53 static int reverse = 0;
54 static int verify = 0;
57 static int sent_pkt = 0;
58 static int recv_pkt = 0;
60 static float tv2fl(struct timeval tv)
62 return (float)(tv.tv_sec*1000.0) + (float)(tv.tv_usec/1000.0);
65 static void stat(int sig)
67 int loss = sent_pkt ? (float)((sent_pkt-recv_pkt)/(sent_pkt/100.0)) : 0;
68 printf("%d sent, %d received, %d%% loss\n", sent_pkt, recv_pkt, loss);
72 static void ping(char *svr)
75 struct sockaddr_l2 addr;
77 unsigned char *send_buf;
78 unsigned char *recv_buf;
83 memset(&sa, 0, sizeof(sa));
85 sigaction(SIGINT, &sa, NULL);
87 send_buf = malloc(L2CAP_CMD_HDR_SIZE + size);
88 recv_buf = malloc(L2CAP_CMD_HDR_SIZE + size);
89 if (!send_buf || !recv_buf) {
90 perror("Can't allocate buffer");
95 sk = socket(PF_BLUETOOTH, SOCK_RAW, BTPROTO_L2CAP);
97 perror("Can't create socket");
101 /* Bind to local address */
102 memset(&addr, 0, sizeof(addr));
103 addr.l2_family = AF_BLUETOOTH;
104 bacpy(&addr.l2_bdaddr, &bdaddr);
106 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
107 perror("Can't bind socket");
111 /* Connect to remote device */
112 memset(&addr, 0, sizeof(addr));
113 addr.l2_family = AF_BLUETOOTH;
114 str2ba(svr, &addr.l2_bdaddr);
116 if (connect(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
117 perror("Can't connect");
121 /* Get local address */
122 memset(&addr, 0, sizeof(addr));
123 optlen = sizeof(addr);
125 if (getsockname(sk, (struct sockaddr *) &addr, &optlen) < 0) {
126 perror("Can't get local address");
130 ba2str(&addr.l2_bdaddr, str);
131 printf("Ping: %s from %s (data size %d) ...\n", svr, str, size);
133 /* Initialize send buffer */
134 for (i = 0; i < size; i++)
135 send_buf[L2CAP_CMD_HDR_SIZE + i] = (i % 40) + 'A';
139 while (count == -1 || count-- > 0) {
140 struct timeval tv_send, tv_recv, tv_diff;
141 l2cap_cmd_hdr *send_cmd = (l2cap_cmd_hdr *) send_buf;
142 l2cap_cmd_hdr *recv_cmd = (l2cap_cmd_hdr *) recv_buf;
144 /* Build command header */
145 send_cmd->ident = id;
146 send_cmd->len = htobs(size);
149 send_cmd->code = L2CAP_ECHO_RSP;
151 send_cmd->code = L2CAP_ECHO_REQ;
153 gettimeofday(&tv_send, NULL);
155 /* Send Echo Command */
156 if (send(sk, send_buf, L2CAP_CMD_HDR_SIZE + size, 0) <= 0) {
157 perror("Send failed");
161 /* Wait for Echo Response */
168 pf[0].events = POLLIN;
170 if ((err = poll(pf, 1, timeout * 1000)) < 0) {
171 perror("Poll failed");
180 if ((err = recv(sk, recv_buf, L2CAP_CMD_HDR_SIZE + size, 0)) < 0) {
181 perror("Recv failed");
186 printf("Disconnected\n");
190 recv_cmd->len = btohs(recv_cmd->len);
192 /* Check for our id */
193 if (recv_cmd->ident != id)
197 if (!reverse && recv_cmd->code == L2CAP_ECHO_RSP)
200 if (recv_cmd->code == L2CAP_COMMAND_REJ) {
201 printf("Peer doesn't support Echo packets\n");
211 gettimeofday(&tv_recv, NULL);
212 timersub(&tv_recv, &tv_send, &tv_diff);
215 /* Check payload length */
216 if (recv_cmd->len != size) {
217 fprintf(stderr, "Received %d bytes, expected %d\n",
218 recv_cmd->len, size);
223 if (memcmp(&send_buf[L2CAP_CMD_HDR_SIZE],
224 &recv_buf[L2CAP_CMD_HDR_SIZE], size)) {
225 fprintf(stderr, "Response payload different.\n");
230 printf("%d bytes from %s id %d time %.2fms\n", recv_cmd->len, svr,
231 id - ident, tv2fl(tv_diff));
236 printf("no response from %s: id %d\n", svr, id - ident);
254 static void usage(void)
256 printf("l2ping - L2CAP ping\n");
258 printf("\tl2ping [-i device] [-s size] [-c count] [-t timeout] [-d delay] [-f] [-r] [-v] <bdaddr>\n");
259 printf("\t-f Flood ping (delay = 0)\n");
260 printf("\t-r Reverse ping\n");
261 printf("\t-v Verify request and response payload\n");
264 int main(int argc, char *argv[])
268 /* Default options */
269 bacpy(&bdaddr, BDADDR_ANY);
271 while ((opt=getopt(argc,argv,"i:d:s:c:t:frv")) != EOF) {
274 if (!strncasecmp(optarg, "hci", 3))
275 hci_devba(atoi(optarg + 3), &bdaddr);
277 str2ba(optarg, &bdaddr);
281 delay = atoi(optarg);
285 /* Kinda flood ping */
290 /* Use responses instead of requests */
299 count = atoi(optarg);
303 timeout = atoi(optarg);
316 if (!(argc - optind)) {