3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2001-2010 Marcel Holtmann <marcel@holtmann.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
35 #include <sys/ioctl.h>
36 #include <sys/socket.h>
38 #include <bluetooth/bluetooth.h>
39 #include <bluetooth/rfcomm.h>
41 static int at_command(int fd, char *cmd, int to)
44 struct timeval timeout;
48 len = write(fd, cmd, strlen(cmd));
50 for (i = 0; i < 100; i++) {
58 if ((sel = select(fd + 1, &rfds, NULL, NULL, &timeout)) > 0) {
60 if (FD_ISSET(fd, &rfds)) {
61 memset(buf, 0, sizeof(buf));
62 len = read(fd, buf, sizeof(buf));
63 for (n = 0; n < len; n++)
65 if (strstr(buf, "\r\nOK") != NULL)
67 if (strstr(buf, "\r\nERROR") != NULL)
69 if (strstr(buf, "\r\nCONNECT") != NULL)
80 static int open_device(char *device)
85 fd = open(device, O_RDWR | O_NOCTTY | O_NONBLOCK);
87 fprintf(stderr, "Can't open serial port: %s (%d)\n",
88 strerror(errno), errno);
92 tcflush(fd, TCIOFLUSH);
94 /* Switch tty to RAW mode */
96 tcsetattr(fd, TCSANOW, &ti);
101 static int open_socket(bdaddr_t *bdaddr, uint8_t channel)
103 struct sockaddr_rc addr;
106 sk = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
108 fprintf(stderr, "Can't create socket: %s (%d)\n",
109 strerror(errno), errno);
113 memset(&addr, 0, sizeof(addr));
114 addr.rc_family = AF_BLUETOOTH;
115 bacpy(&addr.rc_bdaddr, BDADDR_ANY);
117 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
118 fprintf(stderr, "Can't bind socket: %s (%d)\n",
119 strerror(errno), errno);
124 memset(&addr, 0, sizeof(addr));
125 addr.rc_family = AF_BLUETOOTH;
126 bacpy(&addr.rc_bdaddr, bdaddr);
127 addr.rc_channel = channel;
129 if (connect(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
130 fprintf(stderr, "Can't connect: %s (%d)\n",
131 strerror(errno), errno);
139 static void usage(void)
141 printf("Usage:\n\tattest <device> | <bdaddr> [channel]\n");
144 int main(int argc, char *argv[])
153 str2ba(argv[1], &bdaddr);
157 str2ba(argv[1], &bdaddr);
158 channel = atoi(argv[2]);
165 if (bacmp(BDADDR_ANY, &bdaddr)) {
166 printf("Connecting to %s on channel %d\n", argv[1], channel);
167 fd = open_socket(&bdaddr, channel);
169 printf("Opening device %s\n", argv[1]);
170 fd = open_device(argv[1]);
176 at_command(fd, "ATZ\r\n", 10000);
177 at_command(fd, "AT+CPBS=\"ME\"\r\n", 10000);
178 at_command(fd, "AT+CPBR=1,100\r\n", 100000);