annotate rfcal/tsid-test/fc-tsid-shell.c @ 206:70bb11ba7485

fc-tsid-shell (fc-cmu200d development aid) written
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 14 May 2017 00:34:23 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
206
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This program connects to the RF calibration Test System Interface Daemon
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * (TSID) over the local socket interface and allows manual human interaction
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 * with the TSID for development.
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 */
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <sys/types.h>
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <sys/socket.h>
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <sys/un.h>
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <sys/errno.h>
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include <stdio.h>
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 #include <string.h>
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 #include <strings.h>
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 #include <stdlib.h>
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 #include <unistd.h>
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 extern int errno;
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 static char default_socket_pathname[] = "/tmp/fc_rftest_socket";
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 char *socket_pathname;
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 int sock;
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 connect_local_socket()
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 {
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 /* local socket binding voodoo copied from osmocon */
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 struct sockaddr_un local;
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 unsigned int namelen;
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 int rc;
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 sock = socket(AF_UNIX, SOCK_STREAM, 0);
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 if (sock < 0) {
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 perror("socket(AF_UNIX, SOCK_STREAM, 0)");
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 exit(1);
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 }
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 local.sun_family = AF_UNIX;
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 strncpy(local.sun_path, socket_pathname, sizeof(local.sun_path));
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 local.sun_path[sizeof(local.sun_path) - 1] = '\0';
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 /* we use the same magic that X11 uses in Xtranssock.c for
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 * calculating the proper length of the sockaddr */
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 #if defined(BSD44SOCKETS) || defined(__UNIXWARE__)
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 local.sun_len = strlen(local.sun_path);
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 #endif
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 #if defined(BSD44SOCKETS) || defined(SUN_LEN)
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 namelen = SUN_LEN(&local);
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 #else
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 namelen = strlen(local.sun_path) +
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 offsetof(struct sockaddr_un, sun_path) + 1;
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 #endif
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 rc = connect(sock, (struct sockaddr *) &local, namelen);
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 if (rc != 0) {
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 perror(socket_pathname);
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 exit(1);
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 }
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 return(0);
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 }
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 main(argc, argv)
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 char **argv;
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 {
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 char buf[BUFSIZ];
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 fd_set fds, fds1;
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 register int i, cc, max;
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 switch (argc) {
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 case 1:
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 socket_pathname = default_socket_pathname;
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 break;
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 case 2:
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 socket_pathname = argv[1];
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 break;
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 default:
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 fprintf(stderr, "usage: %s [socket-pathname]\n", argv[0]);
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 exit(1);
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 }
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 connect_local_socket();
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 FD_ZERO(&fds);
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 FD_SET(0, &fds);
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 FD_SET(sock, &fds);
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 max = sock + 1;
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 for (;;) {
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 bcopy(&fds, &fds1, sizeof(fd_set));
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 i = select(max, &fds1, NULL, NULL, NULL);
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 if (i < 0) {
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89 if (errno == EINTR)
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90 continue;
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91 perror("select");
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
92 exit(1);
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
93 }
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
94 if (FD_ISSET(0, &fds1)) {
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
95 cc = read(0, buf, sizeof buf);
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
96 if (cc <= 0)
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
97 exit(0);
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
98 write(sock, buf, cc);
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
99 }
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
100 if (FD_ISSET(sock, &fds1)) {
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
101 cc = read(sock, buf, sizeof buf);
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
102 if (cc <= 0) {
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
103 fprintf(stderr, "EOF/error on socket read\n");
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
104 exit(1);
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
105 }
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
106 write(1, buf, cc);
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
107 }
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
108 }
70bb11ba7485 fc-tsid-shell (fc-cmu200d development aid) written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
109 }