]> rtime.felk.cvut.cz Git - mirosot.git/blob - bluez_test/l2captest.c
Added bluez_test applications.
[mirosot.git] / bluez_test / l2captest.c
1 #include <stdlib.h>
2 #include <unistd.h>
3 #include <sys/poll.h>
4 #include <errno.h>
5 #include <signal.h>
6
7 #include <bluetooth/bluetooth.h>
8 #include <bluetooth/l2cap.h>
9 #include <bluetooth/hci.h>
10 #include <bluetooth/hci_lib.h>
11
12 #define MY_PSM 0x1001           /* Protocol/Service Multiplexor */
13
14 static bdaddr_t bdaddr;         /* Adresa lokalniho BT zarizeni  */
15
16
17 void komunikuj(int sk)
18 {
19     int end = 0;
20     int ret;
21
22     struct pollfd p[2];
23
24     p[0].fd = fileno(stdin);
25     p[0].events = POLLIN;
26     p[1].fd = sk;
27     p[1].events = POLLIN;
28     
29     while (!end) {
30         ret = poll(p, 2, 1000);
31         if (ret > 0) {
32             char buf[100];
33             
34             if (p[0].revents & POLLIN) {
35                 if (fgets(buf, 99, stdin) != NULL)
36                     send(sk, buf, strlen(buf), 0); /* posli data */
37                 else end = 1; /* konec (Ctrl-D) */
38             }
39             if (p[1].revents & POLLIN) {
40                 ret = recv(sk, buf, 99, 0);
41                 if (ret >= 0) buf[ret] = '\0';
42                 printf("%s", buf);
43             }
44         } else if (ret < 0) {
45             perror("poll");
46             exit(1);
47         }
48     }
49 }
50
51
52 static void klient(char *dest)
53 {
54         struct sockaddr_l2 addr;
55         int sk;
56
57         /* Create socket */
58         sk = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
59         if (sk < 0) {
60                 perror("Can't create socket");
61                 exit(1);
62         }
63
64         /* Bind to local address */
65         memset(&addr, 0, sizeof(addr));
66         bacpy(&addr.l2_bdaddr, &bdaddr);
67         addr.l2_family = AF_BLUETOOTH;
68
69         if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
70                 perror("Can't bind socket");
71                 close(sk);
72                 exit(1);
73         }
74
75         /* Connect to remote device */
76         memset(&addr, 0, sizeof(addr));
77         addr.l2_family = AF_BLUETOOTH;
78         str2ba(dest, &addr.l2_bdaddr);
79         addr.l2_psm = htobs(MY_PSM);
80
81         if (connect(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
82                 perror("Can't connect");
83                 close(sk);
84                 exit(1);
85         }
86         printf("Spojeno (Ctrl-D konec)\n");
87
88         /* Komunikace */
89         komunikuj(sk);
90
91         /* Uzavri spojeni */
92         close(sk);
93
94 }
95
96 static void server()
97 {
98         struct sockaddr_l2 addr, client_addr;
99         int sk, sk2;
100         socklen_t addr_len;
101
102         /* Create socket */
103         sk = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
104         if (sk < 0) {
105                 perror("Can't create socket");
106                 exit(1);
107         }
108
109         /* Bind to local address */
110         memset(&addr, 0, sizeof(addr));
111         addr.l2_family = AF_BLUETOOTH;
112         bacpy(&addr.l2_bdaddr, &bdaddr);
113         addr.l2_psm = htobs(MY_PSM);   /* Protocol/Service Multiplexor */
114
115         if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
116                 perror("Can't bind socket");
117                 close(sk);
118                 exit(1);
119         }
120
121         /* Pockej az se pripoji klient */
122         if (listen(sk, 1) < 0) {
123                 perror("listen");
124                 close(sk);
125                 exit(1);
126         }
127
128         
129         printf("Cekam na spojeni...\n");
130         addr_len = sizeof(client_addr);
131         sk2 = accept(sk, (struct sockaddr*)&client_addr, &addr_len);
132         printf("Spojeno (Ctrl-D konec)\n");
133
134         /* Komunikace */
135         komunikuj(sk2);
136
137         /* Uzavri spojeni */
138         close(sk2);
139         close(sk);
140
141 }
142
143 static void usage(void)
144 {
145         printf("l2captest - L2CAP test\n");
146         printf("Usage:\n");
147         printf("\tl2captest [-i device] [<server bdaddr>]\n");
148 }
149
150 int main(int argc, char *argv[])
151 {
152         int opt;
153
154         /* Default options */
155         bacpy(&bdaddr, BDADDR_ANY);
156
157         while ((opt=getopt(argc,argv,"i:")) != EOF) {
158                 switch(opt) {
159                 case 'i':       /* likalni BT zarizeni (hciX nebo BD addresa) */
160                         if (!strncasecmp(optarg, "hci", 3))
161                                 hci_devba(atoi(optarg + 3), &bdaddr);
162                         else
163                                 str2ba(optarg, &bdaddr);
164                         break;
165
166                 default:
167                         usage();
168                         exit(1);
169                 }
170         }
171
172         if (!(argc - optind)) {
173             server();           /* ceka na pripojeni libovolneho klienta */
174         } else
175             klient(argv[optind]);
176
177         return 0;
178 }