FreeCalypso > hg > fc-rfcal-tools
annotate cmu200/socket.c @ 34:99f753d4ccaf
fc-cmu200d: check for errors on serial port write
| author | Mychaela Falconia <falcon@freecalypso.org> | 
|---|---|
| date | Tue, 23 May 2017 00:33:10 +0000 | 
| parents | bd62be88259d | 
| children | 
| rev | line source | 
|---|---|
| 0 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 1 /* | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 2 * This module handles the local UNIX domain socket interface for fc-cmu200d. | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 3 */ | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 4 | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 5 #include <sys/types.h> | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 6 #include <sys/socket.h> | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 7 #include <sys/un.h> | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 8 #include <stdio.h> | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 9 #include <string.h> | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 10 #include <strings.h> | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 11 #include <stdlib.h> | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 12 #include <unistd.h> | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 13 | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 14 int listener, activesock; | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 15 | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 16 extern char *bind_socket_pathname; | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 17 | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 18 create_listener_socket() | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 19 { | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 20 /* local socket binding voodoo copied from osmocon */ | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 21 struct sockaddr_un local; | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 22 unsigned int namelen; | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 23 int rc; | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 24 | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 25 listener = socket(AF_UNIX, SOCK_STREAM, 0); | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 26 if (listener < 0) { | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 27 perror("socket(AF_UNIX, SOCK_STREAM, 0)"); | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 28 exit(1); | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 29 } | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 30 | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 31 local.sun_family = AF_UNIX; | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 32 strncpy(local.sun_path, bind_socket_pathname, sizeof(local.sun_path)); | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 33 local.sun_path[sizeof(local.sun_path) - 1] = '\0'; | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 34 unlink(local.sun_path); | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 35 | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 36 /* we use the same magic that X11 uses in Xtranssock.c for | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 37 * calculating the proper length of the sockaddr */ | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 38 #if defined(BSD44SOCKETS) || defined(__UNIXWARE__) | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 39 local.sun_len = strlen(local.sun_path); | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 40 #endif | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 41 #if defined(BSD44SOCKETS) || defined(SUN_LEN) | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 42 namelen = SUN_LEN(&local); | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 43 #else | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 44 namelen = strlen(local.sun_path) + | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 45 offsetof(struct sockaddr_un, sun_path) + 1; | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 46 #endif | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 47 | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 48 rc = bind(listener, (struct sockaddr *) &local, namelen); | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 49 if (rc != 0) { | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 50 perror("bind on local socket"); | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 51 exit(1); | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 52 } | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 53 rc = listen(listener, 1); | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 54 if (rc != 0) { | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 55 perror("listen"); | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 56 exit(1); | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 57 } | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 58 return(0); | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 59 } | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 60 | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 61 get_socket_connection() | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 62 { | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 63 struct sockaddr_un un_addr; | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 64 socklen_t len; | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 65 | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 66 len = sizeof(un_addr); | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 67 activesock = accept(listener, (struct sockaddr *) &un_addr, &len); | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 68 if (activesock < 0) { | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 69 perror("socket accept"); | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 70 exit(1); | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 71 } | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 72 printf("Accepted local socket connection\n"); | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 73 return(0); | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 74 } | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 75 | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 76 send_socket_response(str) | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 77 char *str; | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 78 { | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 79 printf("Msg to client: %s", str); | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 80 write(activesock, str, strlen(str)); | 
| 
bd62be88259d
initial import of rfcal code and docs from freecalypso-tools repository
 Mychaela Falconia <falcon@freecalypso.org> parents: diff
changeset | 81 } | 
