]> rtime.felk.cvut.cz Git - orte.git/blob - orte/liborte/sock.c
ROBOT_DEMO: use higher sampling rate for accelerometer
[orte.git] / orte / liborte / sock.c
1 /*
2  *  $Id: sock.c,v 0.0.0.1               2003/08/21 
3  *
4  *  DEBUG:  section 6                   Socket 
5  *
6  *  -------------------------------------------------------------------  
7  *                                ORTE                                 
8  *                      Open Real-Time Ethernet                       
9  *                                                                    
10  *                      Copyright (C) 2001-2006                       
11  *  Department of Control Engineering FEE CTU Prague, Czech Republic  
12  *                      http://dce.felk.cvut.cz                       
13  *                      http://www.ocera.org                          
14  *                                                                    
15  *  Author:              Petr Smolik    petr.smolik@wo.cz             
16  *  Advisor:             Pavel Pisa                                   
17  *  Project Responsible: Zdenek Hanzalek                              
18  *  --------------------------------------------------------------------
19  *
20  *  This program is free software; you can redistribute it and/or modify
21  *  it under the terms of the GNU General Public License as published by
22  *  the Free Software Foundation; either version 2 of the License, or
23  *  (at your option) any later version.
24  *  
25  *  This program is distributed in the hope that it will be useful,
26  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
27  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28  *  GNU General Public License for more details.
29  *  
30  */ 
31
32 #include "orte_all.h"
33
34 /*********************************************************************/
35 int
36 sock_start(void) {
37 #if defined(SOCK_BSD) || defined (SOCK_RTLWIP)
38   return 0;
39 #elif defined (SOCK_WIN)
40   WORD wVersionRequested;
41   WSADATA wsaData;
42   #ifdef SOCK_WIN_PHARLAP
43     wVersionRequested = MAKEWORD(1, 1);
44   #else
45     wVersionRequested = MAKEWORD(2, 0);
46   #endif
47   return WSAStartup(wVersionRequested, &wsaData);
48 #endif
49 }
50
51 /*********************************************************************/
52 inline void
53 sock_finish(void) {
54 #if defined(SOCK_WIN)
55   WSACleanup();
56 #endif
57 }
58
59 /*********************************************************************/
60 int
61 sock_init_udp(sock_t *sock) {
62   sock->fd = socket(AF_INET, SOCK_DGRAM, 0);
63   if (sock->fd < 0) return -1;
64   return 0;
65 }
66
67 /*********************************************************************/
68 inline void
69 sock_cleanup(sock_t *sock) {
70 #if defined(SOCK_BSD)
71   close(sock->fd);
72 #elif defined(SOCK_RTLWIP)
73   close_socket_np(sock->fd);
74 #elif defined(SOCK_WIN)
75   closesocket(sock->fd);
76 #endif
77 }
78
79 /*********************************************************************/
80 inline int
81 sock_setsockopt(sock_t *sock,int level,int optname,const char *optval, int optlen) {
82   if (setsockopt(sock->fd, level, optname,(void *)optval, optlen)) {
83     sock_cleanup(sock);
84     return -1;
85   }
86   return 0;
87 }
88
89 /*********************************************************************/
90 inline int
91 sock_getsockopt(sock_t *sock,int level,int optname,char *optval, int *optlen) {
92   if (getsockopt(sock->fd, level, optname,(void *)optval, (socklen_t *)optlen)) {
93     sock_cleanup(sock);
94     return -1;
95   }
96   return 0;
97 }
98
99 /*********************************************************************/
100 int
101 sock_bind(sock_t *sock,uint16_t port, IPAddress listen) {
102   struct sockaddr_in name;
103   int size;
104
105   name.sin_family = AF_INET;
106   name.sin_port = htons(port);
107   name.sin_addr.s_addr = htonl(listen);
108   if (bind(sock->fd, 
109           #ifndef CONFIG_ORTE_RTL_ONETD 
110             (struct sockaddr *)
111           #endif
112           &name, sizeof(name)) < 0) {
113     sock_cleanup(sock);
114     return -1;
115   }
116   size = sizeof(name);
117   if (getsockname(sock->fd,
118          #ifndef CONFIG_ORTE_RTL_ONETD 
119            (struct sockaddr *)
120          #endif
121          &name, 
122          #ifndef CONFIG_ORTE_RTL_ONETD 
123            (socklen_t *)&size
124          #else
125            size 
126          #endif
127          ) < 0) {
128     sock_cleanup(sock);
129     return -1;
130   }
131   sock->port=ntohs(name.sin_port);
132   return 0;
133 }
134
135 /*********************************************************************/
136 inline int
137 sock_recvfrom(sock_t *sock, void *buf, int max_len,struct sockaddr_in *des,int des_len) {
138   return recvfrom(sock->fd, buf, max_len, 0,
139     #ifndef CONFIG_ORTE_RTL_ONETD 
140       (struct sockaddr*)
141     #endif
142     des,(socklen_t *)&des_len);
143 }
144
145 /*********************************************************************/
146 inline int
147 sock_sendto(sock_t *sock, void *buf, int len,struct sockaddr_in *des,int des_len) {
148   return sendto(sock->fd, buf, len, 0,
149     #ifndef CONFIG_ORTE_RTL_ONETD 
150       (struct sockaddr*)
151     #endif
152     des,des_len);
153 }
154
155 /*********************************************************************/
156 inline int
157 sock_ioctl(sock_t *sock, long cmd, unsigned long *arg) {
158   return ioctl(sock->fd, cmd, arg);
159 }
160
161 /*********************************************************************/
162 int
163 sock_get_local_interfaces(sock_t *sock,ORTEIFProp *IFProp,char *IFCount) {
164 #if defined(SOCK_BSD)
165   struct ifconf           ifc;
166   char                    buf[MAX_INTERFACES*sizeof(struct ifreq)];
167   char                    *ptr;
168
169   ifc.ifc_len = sizeof(buf);
170   ifc.ifc_buf = buf;
171   *IFCount=0;
172   if (ioctl(sock->fd, SIOCGIFCONF, &ifc) < 0) return -1;
173   for (ptr = buf; ptr < (buf + ifc.ifc_len);ptr += sizeof(struct ifreq)) {
174     struct ifreq*     ifr = (struct ifreq*) ptr;
175     struct sockaddr   addr;
176     memcpy(&addr, &ifr->ifr_addr, sizeof(addr));
177     ioctl(sock->fd, SIOCGIFFLAGS, ifr);
178     if ((ifr->ifr_flags & IFF_UP) && !(ifr->ifr_flags & IFF_LOOPBACK)) {
179       (*IFCount)++;
180       IFProp->ifFlags=ifr->ifr_flags;
181       IFProp->ipAddress=ntohl(((struct sockaddr_in*)&addr)->sin_addr.s_addr);
182       IFProp++;
183     }
184   }
185   return 0;
186 #elif defined(SOCK_RTLWIP)
187   /* loopback iface is recognized if it has this address */
188   char ip_address [] = "127.0.0.1";
189   struct in_addr loopaddr;
190   int i;
191
192   *IFCount=0;
193   if (inet_aton(ip_address, &loopaddr) != 0) return -1;
194   
195   for (i = 0; i < NIC_TABLE_SIZE; i++) {
196     if (nic_table [i].nic_struct != NULL) {
197       if (nic_table[i].ipad.s_addr != loopaddr.s_addr) {
198         (*IFCount)++;
199         IFProp->ifFlags=0; //RT-Linux doesn't flags
200         IFProp->ipAddress=ntohl(nic_table[i].ipad.s_addr);
201         IFProp++;
202       }
203     }
204   }
205   return 0;
206 #elif defined(SOCK_WIN_PHARLAP)
207   DEVHANDLE hDev;
208   EK_TCPIPCFG *pCfg;
209   union {
210     EK_TCPETHSTATUS eth;
211     EK_TCPSLIPSTATUS slip;
212     EK_TCPPPPSTATUS ppp;
213   } status;
214   *IFCount = 0;
215   hDev = NULL;
216
217   while (hDev = EtsTCPIterateDeviceList(hDev)) {
218     pCfg = EtsTCPGetDeviceCfg(hDev);
219
220     if (pCfg->nwIPAddress == 0x0100007F) // 127.0.0.1 localhost
221       continue;
222
223     status.eth.length = sizeof(EK_TCPETHSTATUS);
224     EtsTCPGetDeviceStatus(hDev, &status);
225     if (status.eth.DevStatus.Flags & ETS_TCP_DEV_ONLINE) {
226       IFProp->ifFlags = IFF_UP;
227       IFProp->ipAddress = ntohl(pCfg->nwIPAddress);
228       (*IFCount)++;
229     }
230   }
231   return 0;
232 #elif defined(SOCK_WIN)
233   INTERFACE_INFO      InterfaceList[MAX_INTERFACES];
234   struct sockaddr_in* pAddress;
235   unsigned long       len,i;
236
237   *IFCount=0;
238   if (WSAIoctl(sock->fd,SIO_GET_INTERFACE_LIST,NULL,0,
239                InterfaceList, sizeof(InterfaceList),
240                &len, NULL, NULL)==SOCKET_ERROR) return -1;
241   len=len/sizeof(INTERFACE_INFO);
242   for(i=0;i<len;i++) {
243     long  nFlags;
244     pAddress = (struct sockaddr_in*)&(InterfaceList[i].iiAddress);
245     nFlags = InterfaceList[i].iiFlags;
246     if ((nFlags & IFF_UP) && !(nFlags & IFF_LOOPBACK)) {
247       IFProp->ifFlags=nFlags;
248       IFProp->ipAddress=ntohl(pAddress->sin_addr.s_addr);
249       IFProp++;
250       (*IFCount)++;
251     }
252   }
253   return 0;
254 #endif
255 }
256
257