Skip to content

Commit 174d5c7

Browse files
committed
Merge pull request #12 from geky/nsapi
Matched changes in the NetworkSocketAPI
2 parents 567461e + 03475f3 commit 174d5c7

19 files changed

+1283
-457
lines changed

net/LWIPInterface/LWIPInterface.cpp

Lines changed: 154 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
* limitations under the License.
1515
*/
1616

17+
#include "mbed.h"
1718
#include "LWIPInterface.h"
1819

19-
#include "mbed.h"
2020
#include "lwip/inet.h"
2121
#include "lwip/netif.h"
2222
#include "lwip/dhcp.h"
@@ -26,15 +26,11 @@
2626
#include "netif/etharp.h"
2727
#include "eth_arch.h"
2828

29-
30-
#define LWIP_TIMEOUT 15000
31-
32-
3329
/* TCP/IP and Network Interface Initialisation */
3430
static struct netif netif;
3531

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";
3834

3935
static Semaphore tcpip_inited(0);
4036
static Semaphore netif_linked(0);
@@ -86,8 +82,7 @@ static void set_mac_address(void)
8682
}
8783

8884

89-
// LWIPInterface implementation
90-
int32_t LWIPInterface::connect()
85+
int LWIPInterface::connect()
9186
{
9287
// Set up network
9388
set_mac_address();
@@ -100,14 +95,14 @@ int32_t LWIPInterface::connect()
10095

10196
// Wait for an IP Address
10297
// -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;
105100
}
106101

107102
return 0;
108103
}
109104

110-
int32_t LWIPInterface::disconnect()
105+
int LWIPInterface::disconnect()
111106
{
112107
dhcp_release(&netif);
113108
dhcp_stop(&netif);
@@ -117,90 +112,204 @@ int32_t LWIPInterface::disconnect()
117112
return 0;
118113
}
119114

120-
const char *LWIPInterface::getIPAddress()
115+
const char *LWIPInterface::get_ip_address()
121116
{
122117
return ip_addr;
123118
}
124119

125-
const char *LWIPInterface::getMACAddress()
120+
const char *LWIPInterface::get_mac_address()
126121
{
127122
return mac_addr;
128123
}
129124

130-
SocketInterface *LWIPInterface::createSocket(ns_protocol_t proto)
125+
void *LWIPInterface::socket_create(nsapi_protocol_t proto)
131126
{
132-
int type = (proto == NS_UDP) ? SOCK_DGRAM : SOCK_STREAM;
127+
int type = (proto == NSAPI_UDP) ? SOCK_DGRAM : SOCK_STREAM;
133128
int fd = lwip_socket(AF_INET, type, 0);
134129
if (fd < 0) {
135130
return 0;
136131
}
137132

138-
return new LWIPSocket(fd);
133+
return (void *)(fd+1);
139134
}
140135

141-
void LWIPInterface::destroySocket(SocketInterface *siface)
136+
void LWIPInterface::socket_destroy(void *handle)
142137
{
143-
LWIPSocket *socket = (LWIPSocket *)siface;
144-
lwip_close(socket->fd);
138+
int fd = (int)handle-1;
139+
lwip_close(fd);
140+
141+
}
145142

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);
147147
}
148148

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+
}
149154

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)
152156
{
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+
}
158176

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;
161188
}
162189

163190
return 0;
164191
}
192+
193+
bool LWIPInterface::socket_is_connected(void *handle)
194+
{
195+
return true;
196+
}
165197

166-
int32_t LWIPInterface::LWIPSocket::close()
198+
int LWIPInterface::socket_accept(void *handle, void **connection)
167199
{
168-
return 0;
200+
return NSAPI_ERROR_UNSUPPORTED;
169201
}
170202

171-
int32_t LWIPInterface::LWIPSocket::send(const void *voiddata, uint32_t size)
203+
int LWIPInterface::socket_send(void *handle, const void *p, unsigned size)
172204
{
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;
175208

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);
178211

179212
if (ret > 0) {
180-
writtenLen += ret;
213+
written += ret;
181214
} else if (ret == 0) {
182-
return NS_ERROR_NO_CONNECTION;
215+
return NSAPI_ERROR_NO_CONNECTION;
183216
} else {
184-
return NS_ERROR_DEVICE_ERROR;
217+
return NSAPI_ERROR_DEVICE_ERROR;
185218
}
186219
}
187220

188-
return writtenLen;
221+
return written;
189222
}
190223

191-
int32_t LWIPInterface::LWIPSocket::recv(void *data, uint32_t size)
224+
int LWIPInterface::socket_recv(void *handle, void *data, unsigned size)
192225
{
226+
int fd = (int)handle-1;
193227
int ret = lwip_recv(fd, data, size, MSG_DONTWAIT);
194228

195229
if (ret > 0) {
196230
return ret;
197231
} else if (ret == 0) {
198-
return NS_ERROR_NO_CONNECTION;
232+
return NSAPI_ERROR_NO_CONNECTION;
199233
} else if (ret == -1) {
200-
return NS_ERROR_WOULD_BLOCK;
234+
return NSAPI_ERROR_WOULD_BLOCK;
201235
} else {
202-
return NS_ERROR_DEVICE_ERROR;
236+
return NSAPI_ERROR_DEVICE_ERROR;
203237
}
204238
}
205239

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+
}
206315

0 commit comments

Comments
 (0)