2 ** Copyright 2009 The Android Open Source Project
4 ** Licensed under the Apache License, Version 2.0 (the "License");
5 ** you may not use this file except in compliance with the License.
6 ** You may obtain a copy of the License at
8 ** http://www.apache.org/licenses/LICENSE-2.0
10 ** Unless required by applicable law or agreed to in writing, software
11 ** distributed under the License is distributed on an "AS IS" BASIS,
12 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ** See the License for the specific language governing permissions and
14 ** limitations under the License.
30 #include <sys/socket.h>
31 #include <sys/ioctl.h>
34 #include <netinet/in.h>
36 #include <bluetooth/bluetooth.h>
37 #include <bluetooth/rfcomm.h>
38 #include <bluetooth/sco.h>
39 #include <bluetooth/l2cap.h>
41 #include "cutils/abort_socket.h"
57 struct sockaddr_un local_addr_un = {AF_UNIX, "/data/foo"};
58 struct sockaddr_rc local_addr_rc = {AF_BLUETOOTH, *BDADDR_ANY, 4};
59 struct sockaddr_sco local_addr_sco = {AF_BLUETOOTH, *BDADDR_LOCAL};
60 struct sockaddr_l2 local_addr_l2 = {AF_BLUETOOTH, htobs(0x1001), *BDADDR_ANY, 0};
61 struct sockaddr_in local_addr_in = {AF_INET, 9999, {0}, {0}};
63 struct sockaddr_un remote_addr_un ;
64 struct sockaddr_rc remote_addr_rc ;
65 struct sockaddr_sco remote_addr_sco ;
66 struct sockaddr_l2 remote_addr_l2 ;
67 struct sockaddr_in remote_addr_in ;
69 static void print_events(int events) {
70 if (events & POLLIN) printf("POLLIN ");
71 if (events & POLLPRI) printf("POLLPRI ");
72 if (events & POLLOUT) printf("POLLOUT ");
73 if (events & POLLERR) printf("POLLERR ");
74 if (events & POLLHUP) printf("POLLHUP ");
75 if (events & POLLNVAL) printf("POLLNVAL ");
79 static void print_fds(struct pollfd *ufds, nfds_t nfds) {
81 for (i=0; i<nfds; i++)
82 printf("%d ", ufds[i].fd);
85 static int _socket(int type) {
98 family = PF_BLUETOOTH;
100 protocol = BTPROTO_RFCOMM;
103 family = PF_BLUETOOTH;
104 typ = SOCK_SEQPACKET;
105 protocol = BTPROTO_SCO;
108 family = PF_BLUETOOTH;
109 typ = SOCK_SEQPACKET;
110 protocol = BTPROTO_L2CAP;
119 printf("%d: socket()\n", gettid());
120 ret = socket(family, typ, protocol);
121 printf("%d: socket() = %d\n", gettid(), ret);
122 if (ret < 0) printf("\terr %d (%s)\n", errno, strerror(errno));
127 static int _close(int fd, int type) {
130 printf("%d: close(%d)\n", gettid(), fd);
132 printf("%d: close(%d) = %d\n", gettid(), fd, ret);
133 if (ret < 0) printf("\terr %d (%s)\n", errno, strerror(errno));
138 static int _bind(int fd, int type) {
141 struct sockaddr *addr = NULL;
145 unlink(local_addr_un.sun_path);
146 addr = (struct sockaddr *) &local_addr_un;
147 len = sizeof(local_addr_un);
150 addr = (struct sockaddr *) &local_addr_rc;
151 len = sizeof(local_addr_rc);
154 addr = (struct sockaddr *) &local_addr_sco;
155 len = sizeof(local_addr_sco);
158 addr = (struct sockaddr *) &local_addr_l2;
159 len = sizeof(local_addr_l2);
162 addr = (struct sockaddr *) &local_addr_in;
163 len = sizeof(local_addr_in);
167 printf("%d: bind(%d)\n", gettid(), fd);
168 ret = bind(fd, addr, len);
169 printf("%d: bind(%d) = %d\n", gettid(), fd, ret);
170 if (ret < 0) printf("\terr %d (%s)\n", errno, strerror(errno));
175 static int _listen(int fd, int type) {
178 printf("%d: listen(%d)\n", gettid(), fd);
180 printf("%d: listen(%d) = %d\n", gettid(), fd, ret);
181 if (ret < 0) printf("\terr %d (%s)\n", errno, strerror(errno));
186 static int _read(int fd) {
190 printf("%d: read(%d)\n", gettid(), fd);
191 ret = read(fd, &buf, 1);
192 printf("%d: read(%d) = %d [%d]\n", gettid(), fd, ret, (int)buf);
193 if (ret < 0) printf("\terr %d (%s)\n", errno, strerror(errno));
199 static int _accept(int fd, int type) {
202 struct sockaddr *addr = NULL;
206 addr = (struct sockaddr *) &remote_addr_un;
207 len = sizeof(remote_addr_un);
210 addr = (struct sockaddr *) &remote_addr_rc;
211 len = sizeof(remote_addr_rc);
214 addr = (struct sockaddr *) &remote_addr_sco;
215 len = sizeof(remote_addr_sco);
218 addr = (struct sockaddr *) &remote_addr_l2;
219 len = sizeof(remote_addr_l2);
222 addr = (struct sockaddr *) &remote_addr_in;
223 len = sizeof(remote_addr_in);
227 printf("%d: accept(%d)\n", gettid(), fd);
228 ret = accept(fd, addr, &len);
229 printf("%d: accept(%d) = %d\n", gettid(), fd, ret);
230 if (ret < 0) printf("\terr %d (%s)\n", errno, strerror(errno));
232 printf("\tlen = %d\n", len);
238 static int _connect(int fd, int type) {
241 struct sockaddr *addr = NULL;
245 addr = (struct sockaddr *) &local_addr_un;
246 len = sizeof(local_addr_un);
249 addr = (struct sockaddr *) &local_addr_rc;
250 len = sizeof(local_addr_rc);
253 addr = (struct sockaddr *) &local_addr_sco;
254 len = sizeof(local_addr_sco);
257 addr = (struct sockaddr *) &local_addr_l2;
258 len = sizeof(local_addr_l2);
261 addr = (struct sockaddr *) &local_addr_in;
262 len = sizeof(local_addr_in);
266 printf("%d: connect(%d)\n", gettid(), fd);
267 ret = connect(fd, addr, len);
268 printf("%d: connect(%d) = %d\n", gettid(), fd, ret);
269 if (ret < 0) printf("\terr %d (%s)\n", errno, strerror(errno));
274 static int _write(int fd, int type) {
278 printf("%d: write(%d)\n", gettid(), fd);
279 ret = write(fd, &buf, 1);
280 printf("%d: write(%d) = %d\n", gettid(), fd, ret);
281 if (ret < 0) printf("\terr %d (%s)\n", errno, strerror(errno));
286 static int _shutdown(int fd, int how) {
289 printf("%d: shutdown(%d)\n", gettid(), fd);
290 ret = shutdown(fd, how);
291 printf("%d: shutdown(%d) = %d\n", gettid(), fd, ret);
292 if (ret < 0) printf("\terr %d (%s)\n", errno, strerror(errno));
297 static int _poll(struct pollfd *ufds, nfds_t nfds, int timeout) {
301 printf("%d: poll(", gettid());
302 print_fds(ufds, nfds);
304 ret = poll(ufds, nfds, timeout);
305 printf("%d: poll() = %d\n", gettid(), ret);
306 if (ret < 0) printf("\terr %d (%s)\n", errno, strerror(errno));
308 for (i=0; i<nfds; i++) {
309 if (ufds[i].revents) {
310 printf("\tfd %d ", ufds[i].fd); print_events(ufds[i].revents);
317 static void thread_delay_close(struct thread_args *args) {
318 printf("%d: START\n", gettid());
320 _close(args->fd, args->type);
321 printf("%d: END\n", gettid());
324 static void thread_poll(void *args) {
327 printf("%d: START\n", gettid());
331 printf("%d: END\n", gettid());
334 static void thread_read(void *args) {
336 printf("%d: START\n", gettid());
338 printf("%d: END\n", gettid());
341 static void thread_pollin(void *args) {
344 printf("%d: START\n", gettid());
348 printf("%d: END\n", gettid());
351 static void thread_shutdown(int fd) {
352 printf("%d: START\n", gettid());
354 _shutdown(fd, SHUT_RDWR);
355 printf("%d: END\n", gettid());
358 static void thread_accept(struct thread_args *args) {
359 printf("%d: START\n", gettid());
361 _accept(args->fd, args->type);
362 printf("%d: END\n", gettid());
365 static void thread_connect(struct thread_args *args) {
366 printf("%d: START\n", gettid());
368 _connect(args->fd, args->type);
369 printf("%d: END\n", gettid());
372 static void thread_delay_close_write(struct thread_args *args) {
373 printf("%d: START\n", gettid());
375 _close(args->fd, args->type);
377 _write(args->fd, args->type);
378 printf("%d: END\n", gettid());
381 static void thread_accept_write(struct thread_args *args) {
382 printf("%d: START\n", gettid());
384 _accept(args->fd, args->type);
386 _write(args->fd, args->type);
387 printf("%d: END\n", gettid());
390 static void thread_delay_connect(struct thread_args *args) {
391 printf("%d: START\n", gettid());
393 args->fd = _socket(args->type);
394 _connect(args->fd, args->type);
395 printf("%d: END\n", gettid());
398 static int do_accept_accept_accept(int type) {
402 if (fd < 0) goto error;
404 if (_bind(fd, type) < 0) goto error;
406 if (_listen(fd, type) < 0) goto error;
418 static int do_accept_and_close(int type) {
421 struct thread_args args = {-1, type, 1};
424 if (fd < 0) goto error;
426 if (_bind(fd, type) < 0) goto error;
428 if (_listen(fd, type) < 0) goto error;
431 pthread_create(&thread, NULL, (void *)thread_delay_close, (void *)&args);
435 pthread_join(thread, NULL);
443 static int do_accept_shutdown(int type) {
446 struct thread_args args = {-1, type, 0};
449 if (fd < 0) goto error;
451 if (_bind(fd, type) < 0) goto error;
453 if (_listen(fd, type) < 0) goto error;
456 pthread_create(&thread, NULL, (void *)thread_accept, (void *)&args);
459 _shutdown(fd, SHUT_RDWR);
461 pthread_join(thread, NULL);
471 static int do_connect_shutdown(int type) {
474 struct thread_args args = {-1, type, 0};
477 if (fd < 0) goto error;
480 pthread_create(&thread, NULL, (void *)thread_connect, (void *)&args);
483 _shutdown(fd, SHUT_RDWR);
485 pthread_join(thread, NULL);
495 // accept in one thread. close then write in another
496 static int do_accept_close_write(int type) {
499 struct thread_args args = {-1, type, 1};
502 if (fd < 0) goto error;
504 if (_bind(fd, type) < 0) goto error;
506 if (_listen(fd, type) < 0) goto error;
509 pthread_create(&thread, NULL, (void *)thread_delay_close_write, (void *)&args);
513 pthread_join(thread, NULL);
521 static int do_poll_poll_poll_shutdown(int type) {
522 const int MAX_T = 32;
529 for (i=0; i<MAX_T; i++)
530 pthread_create(&t[i], NULL, (void *)thread_poll, (void *)fd);
534 _shutdown(fd, SHUT_RDWR);
536 for (i=0; i<MAX_T; i++)
537 pthread_join(t[i], NULL);
544 static int do_poll_poll_poll_close(int type) {
545 const int MAX_T = 32;
552 for (i=0; i<MAX_T; i++)
553 pthread_create(&t[i], NULL, (void *)thread_poll, (void *)fd);
559 for (i=0; i<MAX_T; i++)
560 pthread_join(t[i], NULL);
565 static int do_read_read_read_close(int type) {
566 const int MAX_T = 32;
573 for (i=0; i<MAX_T; i++)
574 pthread_create(&t[i], NULL, (void *)thread_read, (void *)fd);
580 for (i=0; i<MAX_T; i++)
581 pthread_join(t[i], NULL);
586 static int do_read_read_read_shutdown(int type) {
587 const int MAX_T = 32;
594 for (i=0; i<MAX_T; i++)
595 pthread_create(&t[i], NULL, (void *)thread_read, (void *)fd);
599 _shutdown(fd, SHUT_RDWR);
601 for (i=0; i<MAX_T; i++)
602 pthread_join(t[i], NULL);
609 static int do_connected_read1_shutdown1(int type) {
613 struct thread_args a1 = {-1, type, 0};
614 struct thread_args a2 = {-1, type, 2};
617 if (fd1 < 0) goto error;
619 if (_bind(fd1, type) < 0) goto error;
621 if (_listen(fd1, type) < 0) goto error;
624 pthread_create(&t1, NULL, (void *)thread_accept_write, (void *)&a1);
627 if (_connect(fd2, type)) goto error;
629 pthread_create(&t2, NULL, (void *)thread_shutdown, (void *)&fd2);
631 while (1) if (_read(fd2)) break;
633 pthread_join(t1, NULL);
634 pthread_join(t2, NULL);
642 // accept in one thread, connect from two different threads
643 static int do_accept_connect_connect(int type) {
647 struct thread_args a1 = {-1, type, 1};
648 struct thread_args a2 = {-1, type, 2};
651 if (fd < 0) goto error;
653 if (_bind(fd, type) < 0) goto error;
655 if (_listen(fd, type) < 0) goto error;
657 pthread_create(&t1, NULL, (void *)thread_delay_connect, (void *)&a1);
658 pthread_create(&t2, NULL, (void *)thread_delay_connect, (void *)&a2);
662 pthread_join(t1, NULL);
663 pthread_join(t2, NULL);
675 {"accept_accept_accept", do_accept_accept_accept},
676 {"accept_and_close", do_accept_and_close},
677 {"accept_shutdown", do_accept_shutdown},
678 {"connect_shutdown", do_connect_shutdown},
679 {"accept_close_write", do_accept_close_write},
680 {"accept_connect_connect", do_accept_connect_connect},
681 {"poll_poll_poll_shutdown", do_poll_poll_poll_shutdown},
682 {"poll_poll_poll_close", do_poll_poll_poll_close},
683 {"read_read_read_shutdown", do_read_read_read_shutdown},
684 {"read_read_read_close", do_read_read_read_close},
685 {"connected_read1_shutdown1", do_connected_read1_shutdown1},
701 static void usage() {
704 printf("socktest TYPE ACTION\n");
706 for (i = 0; type_table[i].name; i++) {
707 printf("\t%s\n", type_table[i].name);
709 printf("\nACTION:\n");
710 for (i = 0; action_table[i].name; i++) {
711 printf("\t%s\n", action_table[i].name);
715 int main(int argc, char **argv) {
723 for (i = 0; type_table[i].name; i++) {
724 if (!strcmp(argv[1], type_table[i].name)) {
725 type = type_table[i].type;
733 for (i = 0; action_table[i].name; i++) {
734 if (!strcmp(argv[2], action_table[i].name)) {
735 printf("TYPE = %s ACTION = %s\n", type_table[type].name,
736 action_table[i].name);
737 return (*action_table[i].ptr)(type);