@@ -139,6 +139,7 @@ struct lwip_socket {
139
139
struct tcp_pcb *tcp;
140
140
};
141
141
142
+ struct tcp_pcb *npcb;
142
143
struct pbuf *rx_chain;
143
144
Semaphore *sem;
144
145
@@ -238,9 +239,25 @@ int LWIPInterface::socket_bind(void *handle, const SocketAddress &address)
238
239
return NSAPI_ERROR_DEVICE_ERROR;
239
240
}
240
241
242
+ static err_t tcp_accept_irq (void *arg, struct tcp_pcb *tpcb, err_t err);
243
+
241
244
int LWIPInterface::socket_listen (void *handle, int backlog)
242
245
{
243
- return NSAPI_ERROR_UNSUPPORTED;
246
+ struct lwip_socket *s = (struct lwip_socket *)handle;
247
+
248
+ if (s->tcp ->state != LISTEN) {
249
+ struct tcp_pcb *server = tcp_listen (s->tcp );
250
+ if (!server) {
251
+ return NSAPI_ERROR_NO_SOCKET;
252
+ }
253
+
254
+ s->tcp = server;
255
+ s->npcb = 0 ;
256
+ }
257
+
258
+ tcp_arg (s->tcp , s);
259
+ tcp_accept (s->tcp , tcp_accept_irq);
260
+ return 0 ;
244
261
}
245
262
246
263
static err_t tcp_sent_irq (void *arg, struct tcp_pcb *tpcb, uint16_t len);
@@ -277,9 +294,53 @@ int LWIPInterface::socket_connect(void *handle, const SocketAddress &addr)
277
294
return 0 ;
278
295
}
279
296
297
+ static err_t tcp_refuse_irq (void *handle, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
298
+ {
299
+ return ERR_WOULDBLOCK;
300
+ }
301
+
302
+ static err_t tcp_accept_irq (void *handle, struct tcp_pcb *npcb, err_t err)
303
+ {
304
+ struct lwip_socket *s = (struct lwip_socket *)handle;
305
+ if (s->npcb ) {
306
+ tcp_abort (npcb);
307
+ return ERR_ABRT;
308
+ }
309
+
310
+ tcp_recv (npcb, tcp_refuse_irq);
311
+ s->npcb = npcb;
312
+
313
+ if (s->callback ) {
314
+ s->callback (s->data );
315
+ }
316
+
317
+ return ERR_OK;
318
+ }
319
+
280
320
int LWIPInterface::socket_accept (void **handle, void *server)
281
321
{
282
- return NSAPI_ERROR_UNSUPPORTED;
322
+ struct lwip_socket *s = (struct lwip_socket *)server;
323
+ if (!s->npcb ) {
324
+ return NSAPI_ERROR_WOULD_BLOCK;
325
+ }
326
+
327
+ struct lwip_socket *ns = new struct lwip_socket ;
328
+ if (!ns) {
329
+ return NSAPI_ERROR_NO_SOCKET;
330
+ }
331
+
332
+ memset (ns, 0 , sizeof *ns);
333
+
334
+ ns->tcp = s->npcb ;
335
+ s->npcb = 0 ;
336
+
337
+ tcp_accepted (ns->tcp );
338
+ tcp_arg (ns->tcp , ns);
339
+ // tcp_err(ns->tcp, tcp_error_irq);
340
+ tcp_sent (ns->tcp , tcp_sent_irq);
341
+ tcp_recv (ns->tcp , tcp_recv_irq);
342
+ *handle = ns;
343
+ return 0 ;
283
344
}
284
345
285
346
static struct pbuf *pbuf_consume (struct pbuf *p, size_t consume, bool free_partial)
0 commit comments