14
14
* limitations under the License.
15
15
*/
16
16
17
+ #include " mbed.h"
17
18
#include " LWIPInterface.h"
18
19
19
- #include " mbed.h"
20
20
#include " lwip/inet.h"
21
21
#include " lwip/netif.h"
22
22
#include " lwip/dhcp.h"
26
26
#include " netif/etharp.h"
27
27
#include " eth_arch.h"
28
28
29
-
30
- #define LWIP_TIMEOUT 15000
31
-
32
-
33
29
/* TCP/IP and Network Interface Initialisation */
34
30
static struct netif netif;
35
31
36
- static char ip_addr[NS_IP_SIZE ] = " \0 " ;
37
- static char mac_addr[NS_MAC_SIZE ] = " \0 " ;
32
+ static char ip_addr[NSAPI_IP_SIZE ] = " \0 " ;
33
+ static char mac_addr[NSAPI_MAC_SIZE ] = " \0 " ;
38
34
39
35
static Semaphore tcpip_inited (0 );
40
36
static Semaphore netif_linked (0 );
@@ -86,8 +82,7 @@ static void set_mac_address(void)
86
82
}
87
83
88
84
89
- // LWIPInterface implementation
90
- int32_t LWIPInterface::connect ()
85
+ int LWIPInterface::connect ()
91
86
{
92
87
// Set up network
93
88
set_mac_address ();
@@ -100,14 +95,14 @@ int32_t LWIPInterface::connect()
100
95
101
96
// Wait for an IP Address
102
97
// -1: error, 0: timeout
103
- if (netif_up.wait (LWIP_TIMEOUT ) < 0 ) {
104
- return NS_ERROR_TIMEOUT ;
98
+ if (netif_up.wait (1500 ) < 0 ) {
99
+ return NSAPI_ERROR_DHCP_FAILURE ;
105
100
}
106
101
107
102
return 0 ;
108
103
}
109
104
110
- int32_t LWIPInterface::disconnect ()
105
+ int LWIPInterface::disconnect ()
111
106
{
112
107
dhcp_release (&netif);
113
108
dhcp_stop (&netif);
@@ -117,90 +112,204 @@ int32_t LWIPInterface::disconnect()
117
112
return 0 ;
118
113
}
119
114
120
- const char *LWIPInterface::getIPAddress ()
115
+ const char *LWIPInterface::get_ip_address ()
121
116
{
122
117
return ip_addr;
123
118
}
124
119
125
- const char *LWIPInterface::getMACAddress ()
120
+ const char *LWIPInterface::get_mac_address ()
126
121
{
127
122
return mac_addr;
128
123
}
129
124
130
- SocketInterface *LWIPInterface::createSocket ( ns_protocol_t proto)
125
+ void *LWIPInterface::socket_create ( nsapi_protocol_t proto)
131
126
{
132
- int type = (proto == NS_UDP ) ? SOCK_DGRAM : SOCK_STREAM;
127
+ int type = (proto == NSAPI_UDP ) ? SOCK_DGRAM : SOCK_STREAM;
133
128
int fd = lwip_socket (AF_INET, type, 0 );
134
129
if (fd < 0 ) {
135
130
return 0 ;
136
131
}
137
132
138
- return new LWIPSocket (fd);
133
+ return ( void *) (fd+ 1 );
139
134
}
140
135
141
- void LWIPInterface::destroySocket (SocketInterface *siface )
136
+ void LWIPInterface::socket_destroy ( void *handle )
142
137
{
143
- LWIPSocket *socket = (LWIPSocket *)siface;
144
- lwip_close (socket->fd );
138
+ int fd = (int )handle-1 ;
139
+ lwip_close (fd);
140
+
141
+ }
145
142
146
- delete socket;
143
+ int LWIPInterface::socket_set_option (void *handle, int optname, const void *optval, unsigned optlen)
144
+ {
145
+ int fd = (int )handle-1 ;
146
+ return lwip_setsockopt (fd, SOL_SOCKET, optname, optval, (socklen_t )optlen);
147
147
}
148
148
149
+ int LWIPInterface::socket_get_option (void *handle, int optname, void *optval, unsigned *optlen)
150
+ {
151
+ int fd = (int )handle-1 ;
152
+ return lwip_getsockopt (fd, SOL_SOCKET, optname, optval, (socklen_t *)optlen);
153
+ }
149
154
150
- // TCP SocketInterface implementation
151
- int32_t LWIPInterface::LWIPSocket::open (const char *ip, uint16_t port)
155
+ int LWIPInterface::socket_bind (void *handle, int port)
152
156
{
153
- struct sockaddr_in host;
154
- memset (&host, 0 , sizeof host);
155
- inet_aton (ip, &host.sin_addr );
156
- host.sin_family = AF_INET;
157
- host.sin_port = htons (port);
157
+ int fd = (int )handle-1 ;
158
+ struct sockaddr_in sa;
159
+ memset (&sa, 0 , sizeof sa);
160
+
161
+ sa.sin_family = AF_INET;
162
+ sa.sin_port = htons (port);
163
+ sa.sin_addr .s_addr = INADDR_ANY;
164
+
165
+ if (lwip_bind (fd, (const struct sockaddr *)&sa, sizeof sa) < 0 ) {
166
+ return NSAPI_ERROR_DEVICE_ERROR;
167
+ }
168
+
169
+ return 0 ;
170
+ }
171
+
172
+ int LWIPInterface::socket_listen (void *handle, int backlog)
173
+ {
174
+ return NSAPI_ERROR_UNSUPPORTED;
175
+ }
158
176
159
- if (lwip_connect (fd, (const struct sockaddr *)&host, sizeof host) < 0 ) {
160
- return NS_ERROR_NO_CONNECTION;
177
+ int LWIPInterface::socket_connect (void *handle, const SocketAddress &addr)
178
+ {
179
+ int fd = (int )handle-1 ;
180
+ struct sockaddr_in sa;
181
+ memset (&sa, 0 , sizeof sa);
182
+ inet_aton (addr.get_ip_address (), &sa.sin_addr );
183
+ sa.sin_family = AF_INET;
184
+ sa.sin_port = htons (addr.get_port ());
185
+
186
+ if (lwip_connect (fd, (const struct sockaddr *)&sa, sizeof sa) < 0 ) {
187
+ return NSAPI_ERROR_NO_CONNECTION;
161
188
}
162
189
163
190
return 0 ;
164
191
}
192
+
193
+ bool LWIPInterface::socket_is_connected (void *handle)
194
+ {
195
+ return true ;
196
+ }
165
197
166
- int32_t LWIPInterface::LWIPSocket::close ( )
198
+ int LWIPInterface::socket_accept ( void *handle, void **connection )
167
199
{
168
- return 0 ;
200
+ return NSAPI_ERROR_UNSUPPORTED ;
169
201
}
170
202
171
- int32_t LWIPInterface::LWIPSocket::send ( const void *voiddata, uint32_t size)
203
+ int LWIPInterface::socket_send ( void *handle, const void *p, unsigned size)
172
204
{
173
- uint8_t *data = (uint8_t *)voiddata;
174
- uint32_t writtenLen = 0 ;
205
+ int fd = (int )handle-1 ;
206
+ uint8_t *data = (uint8_t *)p;
207
+ unsigned written = 0 ;
175
208
176
- while (writtenLen < size) {
177
- int ret = lwip_send (fd, data + writtenLen , size - writtenLen , 0 );
209
+ while (written < size) {
210
+ int ret = lwip_send (fd, data + written , size - written , 0 );
178
211
179
212
if (ret > 0 ) {
180
- writtenLen += ret;
213
+ written += ret;
181
214
} else if (ret == 0 ) {
182
- return NS_ERROR_NO_CONNECTION ;
215
+ return NSAPI_ERROR_NO_CONNECTION ;
183
216
} else {
184
- return NS_ERROR_DEVICE_ERROR ;
217
+ return NSAPI_ERROR_DEVICE_ERROR ;
185
218
}
186
219
}
187
220
188
- return writtenLen ;
221
+ return written ;
189
222
}
190
223
191
- int32_t LWIPInterface::LWIPSocket::recv (void *data, uint32_t size)
224
+ int LWIPInterface::socket_recv (void *handle, void * data, unsigned size)
192
225
{
226
+ int fd = (int )handle-1 ;
193
227
int ret = lwip_recv (fd, data, size, MSG_DONTWAIT);
194
228
195
229
if (ret > 0 ) {
196
230
return ret;
197
231
} else if (ret == 0 ) {
198
- return NS_ERROR_NO_CONNECTION ;
232
+ return NSAPI_ERROR_NO_CONNECTION ;
199
233
} else if (ret == -1 ) {
200
- return NS_ERROR_WOULD_BLOCK ;
234
+ return NSAPI_ERROR_WOULD_BLOCK ;
201
235
} else {
202
- return NS_ERROR_DEVICE_ERROR ;
236
+ return NSAPI_ERROR_DEVICE_ERROR ;
203
237
}
204
238
}
205
239
240
+ int LWIPInterface::socket_sendto (void *handle, const SocketAddress &addr, const void *p, unsigned size)
241
+ {
242
+ int fd = (int )handle-1 ;
243
+ uint8_t *data = (uint8_t *)p;
244
+ unsigned written = 0 ;
245
+
246
+ struct sockaddr_in sa;
247
+ memset (&sa, 0 , sizeof sa);
248
+ inet_aton (addr.get_ip_address (), &sa.sin_addr );
249
+ sa.sin_family = AF_INET;
250
+ sa.sin_port = htons (addr.get_port ());
251
+
252
+ while (written < size) {
253
+ int ret = lwip_sendto (fd, data + written, size - written, 0 ,
254
+ (const struct sockaddr *)&sa, sizeof sa);
255
+
256
+ if (ret > 0 ) {
257
+ written += ret;
258
+ } else if (ret == 0 ) {
259
+ return NSAPI_ERROR_NO_CONNECTION;
260
+ } else {
261
+ return NSAPI_ERROR_DEVICE_ERROR;
262
+ }
263
+ }
264
+
265
+ return written;
266
+ }
267
+
268
+ int LWIPInterface::socket_recvfrom (void *handle, SocketAddress *addr, void *data, unsigned size)
269
+ {
270
+ int fd = (int )handle-1 ;
271
+ struct sockaddr_in sa;
272
+ socklen_t sa_len = sizeof sa;
273
+
274
+ int ret = lwip_recvfrom (fd, data, size, MSG_DONTWAIT,
275
+ (struct sockaddr *)&sa, &sa_len);
276
+
277
+ if (ret > 0 && addr) {
278
+ addr->set_ip_address (inet_ntoa (sa.sin_addr ));
279
+ addr->set_port (ntohs (sa.sin_port ));
280
+ }
281
+
282
+ if (ret > 0 ) {
283
+ return ret;
284
+ } else if (ret == 0 ) {
285
+ return NSAPI_ERROR_NO_CONNECTION;
286
+ } else if (ret == -1 ) {
287
+ return NSAPI_ERROR_WOULD_BLOCK;
288
+ } else {
289
+ return NSAPI_ERROR_DEVICE_ERROR;
290
+ }
291
+ }
292
+
293
+ int LWIPInterface::socket_close (void *handle, bool shutdown)
294
+ {
295
+ int fd = (int )handle-1 ;
296
+ if (shutdown) {
297
+ lwip_shutdown (fd, SHUT_RDWR);
298
+ }
299
+
300
+ lwip_close (fd);
301
+ return 0 ;
302
+ }
303
+
304
+ void LWIPInterface::socket_attach_accept (void *handle, void (*callback)(void *), void *id)
305
+ {
306
+ }
307
+
308
+ void LWIPInterface::socket_attach_send (void *handle, void (*callback)(void *), void *id)
309
+ {
310
+ }
311
+
312
+ void LWIPInterface::socket_attach_recv (void *handle, void (*callback)(void *), void *id)
313
+ {
314
+ }
206
315
0 commit comments