~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Tina4/src/tools/serial/serial_sock.c

Version: ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2          serial_sock.c
  3 
  4          code to handle server and client sockets
  5 
  6          a.lacey@man.ac.uk
  7 */
  8 
  9 #include <stdio.h>
 10 #include <stdlib.h>
 11 #include <signal.h>
 12 #include <netdb.h>
 13 #include <sys/time.h>
 14 #include <sys/types.h>
 15 #include <sys/socket.h>
 16 #include <netinet/in.h>
 17 #include <unistd.h>
 18 #include <tina/sys.h>
 19 #include <tina/sysfuncs.h>
 20 
 21 static  struct sockaddr_in address;
 22 static  int addrlen;
 23 static int server_port = 7070;
 24 static int ssock = -1;
 25 static int master_s = -1;
 26 
 27 
 28 int get_serial_mastersid()
 29 {
 30         return master_s;
 31 }
 32 
 33 
 34 int get_serial_ssockid()
 35 {
 36         return ssock;
 37 }
 38 
 39 
 40 int get_serial_sport()
 41 {
 42         return server_port;
 43 }
 44 
 45 
 46 void set_serial_sport(int v)
 47 {
 48         server_port = v;
 49 }
 50 
 51 
 52 
 53 void serial_close(int *s)
 54 {
 55         if (s == NULL)
 56                 return;
 57 
 58         close(*s);
 59         *s = -1;
 60 }
 61 
 62 static int              install_timer(long length, void (*handler)())
 63 {
 64   struct    itimerval  ivalue;
 65         struct    sigaction  action;
 66         long      secs, usecs;
 67         int       rval;
 68 
 69         secs = (long)floor((double)(length/1000000));
 70         usecs = (long)(length - (secs*1000000));
 71 
 72         ivalue.it_interval.tv_sec = secs;
 73         ivalue.it_interval.tv_usec = usecs;
 74         ivalue.it_value.tv_sec = secs;
 75         ivalue.it_value.tv_usec = usecs;
 76 
 77         if (setitimer(ITIMER_REAL, &ivalue, NULL) !=0)
 78         {
 79                 format("install_timer: failure to set timer\n");
 80                 return -1;
 81         }
 82 
 83         action.sa_handler = handler;
 84         action.sa_flags = 0x0;
 85 
 86   if (sigaction(SIGALRM, &action, NULL) != 0)
 87         {
 88                 format("install_timer: failure to set timer\n");
 89                 return -1;
 90         }
 91 }
 92 
 93 
 94 static int              uninstall_timer()
 95 {
 96         struct    itimerval  ivalue;
 97   struct    sigaction action;
 98 
 99   ivalue.it_interval.tv_sec = 0;
100   ivalue.it_interval.tv_usec = 0;
101   ivalue.it_value.tv_sec = 0;
102   ivalue.it_value.tv_usec = 0;
103 
104         if (setitimer(ITIMER_REAL, &ivalue, NULL) !=0)
105         {
106                 format("uninstall_timer: failure to reset timer\n");
107                 return -1;
108         }
109 
110   signal(SIGALRM, SIG_DFL);
111 }
112 
113 
114 static void  server_receiver(int a)
115 {
116         fd_set  rfds;
117         struct  timeval         tval;
118 
119         FD_ZERO(&rfds);
120         FD_SET(ssock, &rfds);
121 
122         tval.tv_sec = 0;
123         tval.tv_usec = 100;
124 
125         if (!select(ssock+1, &rfds, NULL, NULL, &tval))
126                 return;
127 
128 printf("here, im fucking here!!!\n");   
129         put_srlsvr_status("active");
130         serial_rcvim_totos(ssock);
131         put_srlsvr_status("connected");
132 }
133 
134 
135 static void  server_listener(int a)
136 {
137         fd_set  rfds;
138         struct  timeval         tval;
139 
140         FD_ZERO(&rfds);
141         FD_SET(master_s, &rfds);
142 
143         tval.tv_sec = 0;
144         tval.tv_usec = 100;
145 
146         if (!select(master_s+1, &rfds, NULL, NULL, &tval))
147                 return;
148         
149         put_srlsvr_status("accepting");
150         uninstall_timer();      
151 
152         if ((ssock = accept(master_s, (struct sockaddr *) &address, &addrlen)) < 0)
153         {
154                 format("serial_server: failed to accept\n");
155                 ssock = -1;
156                 return;
157         }
158 
159         put_srlsvr_status("connected");
160         serial_close(&master_s);
161         install_timer(1000, server_receiver); 
162 }
163 
164 
165 int serial_server_start()
166 {
167 
168         if ((master_s = socket(AF_INET, SOCK_STREAM, 0)) == -1)
169         {
170                 format("serial_server: master socket failed\n");
171                 return -1;
172         }
173 
174         address.sin_family = AF_INET;
175         address.sin_addr.s_addr = INADDR_ANY;
176         address.sin_port = htons(server_port);
177         addrlen = sizeof(address);
178 
179         if (bind(master_s, (struct sockaddr *) &address, addrlen) < 0)
180         {
181                 format("serial_server: bind failed\n");
182                 serial_close(&master_s);
183                 return -1;
184         }
185 
186         if (listen(master_s, 1) < 0)
187         {
188                 format("serial_server: listen failed\n");
189                 serial_close(&master_s);
190                 return -1;
191         }
192 
193         put_srlsvr_status("listening");
194         install_timer(1000000, server_listener); 
195 
196         return 0;
197 }
198 
199 
200 int serial_server_stop()
201 {
202         uninstall_timer();      
203         serial_close(&master_s);
204         serial_close(&ssock);
205         put_srlsvr_status("offline");
206 }
207 
208 
209 struct in_addr *atoaddr(char *address)
210 {
211         struct hostent *host;
212         static struct in_addr saddr;
213 
214         /* First try it as aaa.bbb.ccc.ddd. */
215         saddr.s_addr = inet_addr(address);
216         if (saddr.s_addr != -1)
217         {
218                 return &saddr;
219         }
220         if ((host = gethostbyname(address)) != NULL)
221         {
222                 return (struct in_addr *) *host->h_addr_list;
223         }
224 
225         return NULL;
226 }
227 
228 
229 int serial_connect(char *netaddress)
230 {
231         struct in_addr *addr;
232         struct hostent *host;
233         struct sockaddr_in address;
234 
235         if (ssock != -1)
236         {
237                 format("serial_connect: socket in use\n");
238                 return -1;
239         }
240 
241         /*
242         if ((addr = atoaddr(netaddress)) == NULL)
243         {
244                 format("serial_connect: couldn't establish ip\n");
245                 return -1;
246         }
247         */
248 
249         if ((host = gethostbyname(netaddress)) == NULL)
250         {
251                 format("serial_connect: failed to resolve address\n");
252                 return -1;
253         }
254 
255         memcpy(&address.sin_addr, host->h_addr_list[0], host->h_length);
256         address.sin_family = AF_INET;
257         address.sin_port = htons(server_port);
258 
259         if ((ssock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
260         {
261                 format("serial_connect: socket failed\n");
262                 serial_close(&ssock);
263                 return -1;
264         }
265 
266         if (connect(ssock, (struct sockaddr *)&address, sizeof(address)) < 0)
267         {
268                 format("serial_connect: connect failed\n");
269                 serial_close(&ssock);
270                 return -1;
271         }
272 
273         put_srlclnt_status("connected");
274         return ssock;
275 }
276 
277 
278 void serial_disconnect()
279 {
280         serial_close(&ssock);
281         put_srlclnt_status("disconnected");
282 }
283 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.