2 * (c) 2013 Nils Faerber, Siegen, Germany
10 #include <sys/select.h>
12 #include <sys/types.h>
13 #include <sys/socket.h>
15 #include <bluetooth/bluetooth.h>
16 #include <bluetooth/l2cap.h>
24 // replace "markups" with appropr. chars
25 char *parse_buffer(char *inbuf, int *cmd)
28 static char outbuf[240];
34 // if a line starts with a \ then it is an internal command
35 if (inbuf[0] == '\\') {
47 for (i=0; i<strlen(inbuf); i++) {
48 if (inbuf[i] == '\\') {
53 outbuf[o++] = inbuf[i];
59 int main(int argc, char **argv)
61 struct sockaddr_l2 addr = { 0 };
62 int s, status, len, i, cmd;
70 fprintf(stderr, "usage: %s <bt_addr>\n", argv[0]);
74 strncpy(dest, argv[1], 18);
77 s = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
79 // set the connection parameters (who to connect to)
80 addr.l2_family = AF_BLUETOOTH;
81 addr.l2_psm = htobs(0x1001);
82 str2ba( dest, &addr.l2_bdaddr );
85 status = connect(s, (struct sockaddr *)&addr, sizeof(addr));
89 perror("open socket");
94 fprintf(stderr, "connected\n");
96 if (fcntl(0, F_SETFL, O_NONBLOCK) != 0)
97 perror("stdin nonblock");
98 if (fcntl(s, F_SETFL, O_NONBLOCK) != 0)
99 perror("socket nonblock");
107 status = select(s+1, &infds, NULL, &efds, NULL);
108 // fprintf(stderr, "select = %d\n", status);
110 if (FD_ISSET(0, &infds)) {
111 len = read(0, buf, 240);
113 // status = write(s, buf, len);
114 out = parse_buffer(buf, &cmd);
115 if (cmd != CMD_NULL) {
116 if (cmd == CMD_QUIT) {
122 status = write(s, out, len);
128 } else if (FD_ISSET(s, &infds)) {
129 len = read(s, buf, 240);
130 // fprintf(stderr, "read = %d\n", len);
131 for (i=0; i<len; i++)
132 fprintf(stderr, "%c", buf[i]);
133 // fprintf(stderr, "\n");
134 } else if (FD_ISSET(s, &efds)) {
135 fprintf(stderr, "e on socket\n");
137 fprintf(stderr, "hu?");