]> git.karo-electronics.de Git - oswald.git/blob - linux-bt/l2cap_client.c
a3fb782012e4ab4b9c5e1c4a1fe0c984aec8baa7
[oswald.git] / linux-bt / l2cap_client.c
1 /*
2  * (c) 2013 Nils Faerber, Siegen, Germany
3  * licensed under GPLv2
4  */
5
6 #include <stdio.h>
7 #include <string.h>
8 #include <unistd.h>
9 #include <stdlib.h>
10 #include <sys/select.h>
11 #include <sys/time.h>
12 #include <sys/types.h>
13 #include <sys/socket.h>
14 #include <fcntl.h>
15 #include <bluetooth/bluetooth.h>
16 #include <bluetooth/l2cap.h>
17
18 int main(int argc, char **argv)
19 {
20         struct sockaddr_l2 addr = { 0 };
21         int s, status, len, i;
22         char buf[255];
23         char dest[18];
24         fd_set infds;
25         fd_set efds;
26
27         if (argc < 2) {
28                 fprintf(stderr, "usage: %s <bt_addr>\n", argv[0]);
29                 exit(2);
30         }
31
32         strncpy(dest, argv[1], 18);
33
34         // allocate a socket
35         s = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
36
37         // set the connection parameters (who to connect to)
38         addr.l2_family = AF_BLUETOOTH;
39         addr.l2_psm = htobs(0x1001);
40         str2ba( dest, &addr.l2_bdaddr );
41
42         // connect to server
43         status = connect(s, (struct sockaddr *)&addr, sizeof(addr));
44
45         // send a message
46         if ( status != 0 ) {
47                 perror("open socket");
48                 close(s);
49                 return -1;
50         }
51
52         fprintf(stderr, "connected\n");
53
54         if (fcntl(0, F_SETFL, O_NONBLOCK) != 0)
55                 perror("stdin nonblock");
56         if (fcntl(s, F_SETFL, O_NONBLOCK) != 0)
57                 perror("socket nonblock");
58
59         while (1) {
60                 FD_ZERO(&infds);
61                 FD_SET(s, &infds);
62                 FD_SET(0, &infds);
63                 FD_ZERO(&efds);
64                 FD_SET(s, &efds);
65                 status = select(s+1, &infds, NULL, &efds, NULL);
66                 // fprintf(stderr, "select = %d\n", status);
67                 if ( status > 0) {
68                         if (FD_ISSET(0, &infds)) {
69                                 len = read(0, buf, 240);
70                                 if (len > 0) {
71                                         status = write(s, buf, len);
72                                         if (status < 0)
73                                                 perror("write");
74                                         fsync(s);
75                                 }
76                         } else if (FD_ISSET(s, &infds)) {
77                                 len = read(s, buf, 240);
78                                 // fprintf(stderr, "read = %d\n", len);
79                                 for (i=0; i<len; i++)
80                                         fprintf(stderr, "%c", buf[i]);
81                                 // fprintf(stderr, "\n");
82                         } else if (FD_ISSET(s, &efds)) {
83                                 fprintf(stderr, "e on socket\n");
84                         } else
85                                 fprintf(stderr, "hu?");
86                 } else
87                         perror("select");
88         };
89
90
91         close(s);
92
93         return 0;
94 }
95