#include <stdio.h>#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <strings.h>#include <string.h>#include <arpa/inet.h>#include <stdlib.h>#include <unistd.h>#include <signal.h>#include <errno.h>#include "ftdi_helper.h"#include "server_command_line_parser.h"Include dependency graph for server.c:

Go to the source code of this file.
Defines | |
| #define | STATE_DELAY_US 10000 |
Functions | |
| void | error (char *msg) |
| int | delay_uS (int) |
| void | sigproc () |
| void | quitproc () |
| int | print_sock_error (int) |
| int | main (int argc, char *argv[]) |
Variables | |
| int | sockfd |
| int | portno |
| int | clilen |
| int | newsockfd = 0 |
|
|
|
|
|
Definition at line 196 of file server.c. 00196 {
00197 struct timeval to;
00198 to.tv_sec = 0;
00199 to.tv_usec = uS;
00200 select(0,NULL,NULL,NULL,&to);
00201 return (0);
00202 }
|
|
|
Definition at line 66 of file server.c. References msg. Referenced by jitter(), and main(). 00066 {
00067 perror(msg);
00068 exit(1);
00069 }
|
|
||||||||||||
|
Definition at line 81 of file server.c. References clilen, command_line_parser(), command_options_t, delay_uS(), error(), init_command_line_options(), newsockfd, options, command_options::port, quitproc(), sigproc(), sockfd, and STATE_DELAY_US. 00081 {
00082 char buffer[256];
00083 struct sockaddr_in serv_addr, cli_addr;
00084 int n;
00085
00086 int select_status;
00087 fd_set readfds;
00088 struct timeval to;
00089
00090 enum {EUNCONNECTED,EREAD,FTDI,EWRITE};
00091 static int bridge_state=EUNCONNECTED;
00092
00093 char local_buffer[QUEUE_LENGTH];
00094 int read_chars=0;
00095 int write_chars=0;
00096
00097 command_options_t options;
00098 unsigned int yes=1;
00099
00100 // Setup signal handlers
00101 signal(SIGINT,sigproc);
00102 signal(SIGQUIT,quitproc);
00103
00104 // Handle Command Line Options
00105 init_command_line_options(&options);
00106 command_line_parser (argc, argv, &options);
00107
00108 // Initialize the FTDI console channel
00109 if (ftd2xx_init(ftd2xx_console_desc,!options.attach)) {
00110 printf("Initialization Failed\n");
00111 return (1);
00112 }
00113
00114 // Setup to listen on the socket
00115 sockfd = socket(AF_INET, SOCK_STREAM, 0); // Load file descriptor, sockfd
00116 if (sockfd < 0) {
00117 error("ERROR opening socket");
00118 }
00119
00120 /* if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(yes))<0) { */
00121 /* perror("Reusing ADDR failed"); */
00122 /* exit(1); */
00123 /* } */
00124 bzero((char *) &serv_addr, sizeof(serv_addr)); // zero the
00125 serv_addr.sin_family = AF_INET;
00126 serv_addr.sin_addr.s_addr = INADDR_ANY;
00127 serv_addr.sin_port = htons(options.port);
00128 if (bind(sockfd, (struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) {
00129 error("ERROR on binding");
00130 }
00131 listen(sockfd,5);
00132
00133 while (1) {
00134 switch (bridge_state) {
00135 case EUNCONNECTED:
00136 printf("Waiting for a connection on port %d...",options.port);
00137 fflush(stdout);
00138 newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr, &clilen);
00139 printf("Connection Accepted from %s\n",inet_ntoa(cli_addr.sin_addr));
00140 purge_ftdi();
00141 init_queue(&ftdi_tx_queue);
00142 init_queue(&ftdi_rx_queue);
00143 bridge_state=EREAD;
00144 break;
00145 case EREAD:
00146 bzero(buffer,256);
00147 FD_ZERO(&readfds); // clear the read file descriptor set
00148 FD_SET(newsockfd,&readfds); // listen to the new socket for data
00149 to.tv_sec = 0;
00150 to.tv_usec = 1000;
00151 select_status=select(newsockfd+1,&readfds,NULL,NULL,&to);
00152 switch (select_status) {
00153 case -1:
00154 printf("Select Error\n");
00155 bridge_state=EUNCONNECTED;
00156 close(newsockfd);
00157 break;
00158 case 0:
00159 bridge_state=FTDI;
00160 break;
00161 default:
00162 bridge_state=FTDI;
00163 if (FD_ISSET(newsockfd,&readfds)) {
00164 n = read(newsockfd,buffer,255);
00165 if (n) {
00166 enqueue(&ftdi_tx_queue,buffer,n);
00167 }
00168 else {
00169 bridge_state=EUNCONNECTED;
00170 close(newsockfd);
00171 }
00172 }
00173 }
00174 break;
00175 case FTDI:
00176 eval_ftdi_console();
00177 bridge_state=EWRITE;
00178 break;
00179 case EWRITE:
00180 bzero(local_buffer,QUEUE_LENGTH);
00181 if ((read_chars=dequeue(&ftdi_rx_queue,local_buffer,FTDI_RX_TRANSFER_SIZE))) {
00182 write_chars=write(newsockfd,local_buffer,read_chars);
00183 }
00184 bridge_state=EREAD;
00185 break;
00186 default:
00187 printf("Error: Unknown Communication State\n");
00188 exit(1);
00189 break;
00190 }
00191 delay_uS(STATE_DELAY_US);
00192 }
00193 return 0;
00194 }
|
Here is the call graph for this function:

|
|
Definition at line 216 of file server.c. 00216 {
00217 switch (sock_error) {
00218 case EBADF:
00219 printf("EBADF: The socket argument is not a valid file descriptor.\n");
00220 break;
00221 case EINVAL:
00222 printf("EINVAL: The socket shutdown method argument is invalid.\n");
00223 break;
00224 case ENOTCONN:
00225 printf("ENOTCONN: The socket is not connected.\n");
00226 break;
00227 case ENOTSOCK:
00228 printf("ENOTCONN: The socket argument does not refer to a socket.\n");
00229 break;
00230 default:
00231 printf("%d:Unrecognized Socket Error\n",sock_error);
00232 }
00233 return(0);
00234 }
|
|
|
Definition at line 210 of file server.c. References newsockfd, and sockfd. Referenced by main(). 00210 {
00211 close(newsockfd);
00212 close(sockfd);
00213 exit(0);
00214 }
|
|
|
Definition at line 204 of file server.c. References newsockfd, and sockfd. Referenced by main(). 00204 {
00205 close(newsockfd);
00206 close(sockfd);
00207 exit(0);
00208 }
|
|
|
Definition at line 77 of file server.c. Referenced by main(), and socket_comm(). |
|
|
Definition at line 78 of file server.c. Referenced by main(), quitproc(), sigproc(), and socket_comm(). |
|
|
|
|
|
Definition at line 77 of file server.c. Referenced by main(), quitproc(), sigproc(), and socket_comm(). |
1.3.9.1