comparison tsid-test/fc-tsid-shell.c @ 0:bd62be88259d

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