FreeCalypso > hg > fc-rfcal-tools
annotate autocal/tsidsock.c @ 60:81e8f7e99d89
fc-rfcal-rxband: upload of GMagic to DUT implemented
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sat, 27 May 2017 23:35:06 +0000 |
parents | f67f46e56355 |
children |
rev | line source |
---|---|
14
93e5194e5511
autocal/tsidsock.c written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 /* |
93e5194e5511
autocal/tsidsock.c written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
2 * TSID local socket interface |
93e5194e5511
autocal/tsidsock.c written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 */ |
93e5194e5511
autocal/tsidsock.c written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 |
93e5194e5511
autocal/tsidsock.c written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 #include <sys/types.h> |
93e5194e5511
autocal/tsidsock.c written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 #include <sys/socket.h> |
93e5194e5511
autocal/tsidsock.c written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 #include <sys/un.h> |
93e5194e5511
autocal/tsidsock.c written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 #include <stdio.h> |
93e5194e5511
autocal/tsidsock.c written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 #include <string.h> |
93e5194e5511
autocal/tsidsock.c written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 #include <strings.h> |
93e5194e5511
autocal/tsidsock.c written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 #include <stdlib.h> |
93e5194e5511
autocal/tsidsock.c written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 #include <unistd.h> |
93e5194e5511
autocal/tsidsock.c written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 #include <rvinterf/exitcodes.h> |
93e5194e5511
autocal/tsidsock.c written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 |
93e5194e5511
autocal/tsidsock.c written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 char *tsid_socket_pathname = "/tmp/fc_rftest_socket"; |
93e5194e5511
autocal/tsidsock.c written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 int tsid_socket; |
19
f67f46e56355
autocal/tsidsock.c: implemented command/response protocol
Mychaela Falconia <falcon@freecalypso.org>
parents:
14
diff
changeset
|
17 char tsid_response[4096]; |
14
93e5194e5511
autocal/tsidsock.c written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 |
93e5194e5511
autocal/tsidsock.c written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 connect_tsid_socket() |
93e5194e5511
autocal/tsidsock.c written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 { |
93e5194e5511
autocal/tsidsock.c written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 /* local socket binding voodoo copied from osmocon */ |
93e5194e5511
autocal/tsidsock.c written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 struct sockaddr_un local; |
93e5194e5511
autocal/tsidsock.c written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 unsigned int namelen; |
93e5194e5511
autocal/tsidsock.c written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 int rc; |
93e5194e5511
autocal/tsidsock.c written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 |
93e5194e5511
autocal/tsidsock.c written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 tsid_socket = socket(AF_UNIX, SOCK_STREAM, 0); |
93e5194e5511
autocal/tsidsock.c written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 if (tsid_socket < 0) { |
93e5194e5511
autocal/tsidsock.c written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
28 perror("socket(AF_UNIX, SOCK_STREAM, 0)"); |
93e5194e5511
autocal/tsidsock.c written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 exit(ERROR_UNIX); |
93e5194e5511
autocal/tsidsock.c written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 } |
93e5194e5511
autocal/tsidsock.c written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 |
93e5194e5511
autocal/tsidsock.c written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 local.sun_family = AF_UNIX; |
93e5194e5511
autocal/tsidsock.c written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 strncpy(local.sun_path, tsid_socket_pathname, sizeof(local.sun_path)); |
93e5194e5511
autocal/tsidsock.c written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
34 local.sun_path[sizeof(local.sun_path) - 1] = '\0'; |
93e5194e5511
autocal/tsidsock.c written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
35 |
93e5194e5511
autocal/tsidsock.c written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 /* we use the same magic that X11 uses in Xtranssock.c for |
93e5194e5511
autocal/tsidsock.c written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 * calculating the proper length of the sockaddr */ |
93e5194e5511
autocal/tsidsock.c written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 #if defined(BSD44SOCKETS) || defined(__UNIXWARE__) |
93e5194e5511
autocal/tsidsock.c written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 local.sun_len = strlen(local.sun_path); |
93e5194e5511
autocal/tsidsock.c written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
40 #endif |
93e5194e5511
autocal/tsidsock.c written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 #if defined(BSD44SOCKETS) || defined(SUN_LEN) |
93e5194e5511
autocal/tsidsock.c written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 namelen = SUN_LEN(&local); |
93e5194e5511
autocal/tsidsock.c written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 #else |
93e5194e5511
autocal/tsidsock.c written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 namelen = strlen(local.sun_path) + |
93e5194e5511
autocal/tsidsock.c written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
45 offsetof(struct sockaddr_un, sun_path) + 1; |
93e5194e5511
autocal/tsidsock.c written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 #endif |
93e5194e5511
autocal/tsidsock.c written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 |
93e5194e5511
autocal/tsidsock.c written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 rc = connect(tsid_socket, (struct sockaddr *) &local, namelen); |
93e5194e5511
autocal/tsidsock.c written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 if (rc != 0) { |
93e5194e5511
autocal/tsidsock.c written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
50 perror(tsid_socket_pathname); |
93e5194e5511
autocal/tsidsock.c written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
51 exit(ERROR_RFTEST); |
93e5194e5511
autocal/tsidsock.c written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
52 } |
19
f67f46e56355
autocal/tsidsock.c: implemented command/response protocol
Mychaela Falconia <falcon@freecalypso.org>
parents:
14
diff
changeset
|
53 collect_tsid_response(); |
14
93e5194e5511
autocal/tsidsock.c written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
54 return(0); |
93e5194e5511
autocal/tsidsock.c written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
55 } |
19
f67f46e56355
autocal/tsidsock.c: implemented command/response protocol
Mychaela Falconia <falcon@freecalypso.org>
parents:
14
diff
changeset
|
56 |
f67f46e56355
autocal/tsidsock.c: implemented command/response protocol
Mychaela Falconia <falcon@freecalypso.org>
parents:
14
diff
changeset
|
57 collect_tsid_response() |
f67f46e56355
autocal/tsidsock.c: implemented command/response protocol
Mychaela Falconia <falcon@freecalypso.org>
parents:
14
diff
changeset
|
58 { |
f67f46e56355
autocal/tsidsock.c: implemented command/response protocol
Mychaela Falconia <falcon@freecalypso.org>
parents:
14
diff
changeset
|
59 char buf[BUFSIZ]; |
f67f46e56355
autocal/tsidsock.c: implemented command/response protocol
Mychaela Falconia <falcon@freecalypso.org>
parents:
14
diff
changeset
|
60 int cc, pos; |
f67f46e56355
autocal/tsidsock.c: implemented command/response protocol
Mychaela Falconia <falcon@freecalypso.org>
parents:
14
diff
changeset
|
61 |
f67f46e56355
autocal/tsidsock.c: implemented command/response protocol
Mychaela Falconia <falcon@freecalypso.org>
parents:
14
diff
changeset
|
62 for (pos = 0; ; ) { |
f67f46e56355
autocal/tsidsock.c: implemented command/response protocol
Mychaela Falconia <falcon@freecalypso.org>
parents:
14
diff
changeset
|
63 cc = read(tsid_socket, buf, sizeof buf); |
f67f46e56355
autocal/tsidsock.c: implemented command/response protocol
Mychaela Falconia <falcon@freecalypso.org>
parents:
14
diff
changeset
|
64 if (cc <= 0) { |
f67f46e56355
autocal/tsidsock.c: implemented command/response protocol
Mychaela Falconia <falcon@freecalypso.org>
parents:
14
diff
changeset
|
65 perror("read from TSID socket"); |
f67f46e56355
autocal/tsidsock.c: implemented command/response protocol
Mychaela Falconia <falcon@freecalypso.org>
parents:
14
diff
changeset
|
66 exit(ERROR_RFTEST); |
f67f46e56355
autocal/tsidsock.c: implemented command/response protocol
Mychaela Falconia <falcon@freecalypso.org>
parents:
14
diff
changeset
|
67 } |
f67f46e56355
autocal/tsidsock.c: implemented command/response protocol
Mychaela Falconia <falcon@freecalypso.org>
parents:
14
diff
changeset
|
68 if (pos + cc > sizeof tsid_response) { |
f67f46e56355
autocal/tsidsock.c: implemented command/response protocol
Mychaela Falconia <falcon@freecalypso.org>
parents:
14
diff
changeset
|
69 fprintf(stderr, |
f67f46e56355
autocal/tsidsock.c: implemented command/response protocol
Mychaela Falconia <falcon@freecalypso.org>
parents:
14
diff
changeset
|
70 "error: response from TSID exceeds our buffer size\n"); |
f67f46e56355
autocal/tsidsock.c: implemented command/response protocol
Mychaela Falconia <falcon@freecalypso.org>
parents:
14
diff
changeset
|
71 exit(ERROR_RFTEST); |
f67f46e56355
autocal/tsidsock.c: implemented command/response protocol
Mychaela Falconia <falcon@freecalypso.org>
parents:
14
diff
changeset
|
72 } |
f67f46e56355
autocal/tsidsock.c: implemented command/response protocol
Mychaela Falconia <falcon@freecalypso.org>
parents:
14
diff
changeset
|
73 bcopy(buf, tsid_response + pos, cc); |
f67f46e56355
autocal/tsidsock.c: implemented command/response protocol
Mychaela Falconia <falcon@freecalypso.org>
parents:
14
diff
changeset
|
74 pos += cc; |
f67f46e56355
autocal/tsidsock.c: implemented command/response protocol
Mychaela Falconia <falcon@freecalypso.org>
parents:
14
diff
changeset
|
75 if (tsid_response[pos-1] == '\n') |
f67f46e56355
autocal/tsidsock.c: implemented command/response protocol
Mychaela Falconia <falcon@freecalypso.org>
parents:
14
diff
changeset
|
76 break; |
f67f46e56355
autocal/tsidsock.c: implemented command/response protocol
Mychaela Falconia <falcon@freecalypso.org>
parents:
14
diff
changeset
|
77 } |
f67f46e56355
autocal/tsidsock.c: implemented command/response protocol
Mychaela Falconia <falcon@freecalypso.org>
parents:
14
diff
changeset
|
78 tsid_response[pos-1] = '\0'; |
f67f46e56355
autocal/tsidsock.c: implemented command/response protocol
Mychaela Falconia <falcon@freecalypso.org>
parents:
14
diff
changeset
|
79 if (tsid_response[0] != '+') { |
f67f46e56355
autocal/tsidsock.c: implemented command/response protocol
Mychaela Falconia <falcon@freecalypso.org>
parents:
14
diff
changeset
|
80 fprintf(stderr, "Error from TSID: %s\n", tsid_response); |
f67f46e56355
autocal/tsidsock.c: implemented command/response protocol
Mychaela Falconia <falcon@freecalypso.org>
parents:
14
diff
changeset
|
81 exit(ERROR_RFTEST); |
f67f46e56355
autocal/tsidsock.c: implemented command/response protocol
Mychaela Falconia <falcon@freecalypso.org>
parents:
14
diff
changeset
|
82 } |
f67f46e56355
autocal/tsidsock.c: implemented command/response protocol
Mychaela Falconia <falcon@freecalypso.org>
parents:
14
diff
changeset
|
83 return(0); |
f67f46e56355
autocal/tsidsock.c: implemented command/response protocol
Mychaela Falconia <falcon@freecalypso.org>
parents:
14
diff
changeset
|
84 } |
f67f46e56355
autocal/tsidsock.c: implemented command/response protocol
Mychaela Falconia <falcon@freecalypso.org>
parents:
14
diff
changeset
|
85 |
f67f46e56355
autocal/tsidsock.c: implemented command/response protocol
Mychaela Falconia <falcon@freecalypso.org>
parents:
14
diff
changeset
|
86 tsid_command(cmd) |
f67f46e56355
autocal/tsidsock.c: implemented command/response protocol
Mychaela Falconia <falcon@freecalypso.org>
parents:
14
diff
changeset
|
87 char *cmd; |
f67f46e56355
autocal/tsidsock.c: implemented command/response protocol
Mychaela Falconia <falcon@freecalypso.org>
parents:
14
diff
changeset
|
88 { |
f67f46e56355
autocal/tsidsock.c: implemented command/response protocol
Mychaela Falconia <falcon@freecalypso.org>
parents:
14
diff
changeset
|
89 write(tsid_socket, cmd, strlen(cmd)); |
f67f46e56355
autocal/tsidsock.c: implemented command/response protocol
Mychaela Falconia <falcon@freecalypso.org>
parents:
14
diff
changeset
|
90 collect_tsid_response(); |
f67f46e56355
autocal/tsidsock.c: implemented command/response protocol
Mychaela Falconia <falcon@freecalypso.org>
parents:
14
diff
changeset
|
91 return(0); |
f67f46e56355
autocal/tsidsock.c: implemented command/response protocol
Mychaela Falconia <falcon@freecalypso.org>
parents:
14
diff
changeset
|
92 } |