OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [freertos-6.1.1/] [Demo/] [lwIP_Demo_Rowley_ARM7/] [lwip-1.1.0/] [src/] [api/] [api_msg.c] - Blame information for rev 583

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 583 jeremybenn
/*
2
 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without modification,
6
 * are permitted provided that the following conditions are met:
7
 *
8
 * 1. Redistributions of source code must retain the above copyright notice,
9
 *    this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright notice,
11
 *    this list of conditions and the following disclaimer in the documentation
12
 *    and/or other materials provided with the distribution.
13
 * 3. The name of the author may not be used to endorse or promote products
14
 *    derived from this software without specific prior written permission.
15
 *
16
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19
 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
21
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
25
 * OF SUCH DAMAGE.
26
 *
27
 * This file is part of the lwIP TCP/IP stack.
28
 *
29
 * Author: Adam Dunkels <adam@sics.se>
30
 *
31
 */
32
 
33
#include "lwip/opt.h"
34
#include "lwip/arch.h"
35
#include "lwip/api_msg.h"
36
#include "lwip/memp.h"
37
#include "lwip/sys.h"
38
#include "lwip/tcpip.h"
39
 
40
#if LWIP_RAW
41
static u8_t
42
recv_raw(void *arg, struct raw_pcb *pcb, struct pbuf *p,
43
    struct ip_addr *addr)
44
{
45
  struct netbuf *buf;
46
  struct netconn *conn;
47
 
48
  conn = arg;
49
  if (!conn) return 0;
50
 
51
  if (conn->recvmbox != SYS_MBOX_NULL) {
52
    if (!(buf = memp_malloc(MEMP_NETBUF))) {
53
      return 0;
54
    }
55
    pbuf_ref(p);
56
    buf->p = p;
57
    buf->ptr = p;
58
    buf->fromaddr = addr;
59
    buf->fromport = pcb->protocol;
60
 
61
    conn->recv_avail += p->tot_len;
62
    /* Register event with callback */
63
    if (conn->callback)
64
        (*conn->callback)(conn, NETCONN_EVT_RCVPLUS, p->tot_len);
65
    sys_mbox_post(conn->recvmbox, buf);
66
  }
67
 
68
  return 0; /* do not eat the packet */
69
}
70
#endif
71
#if LWIP_UDP
72
static void
73
recv_udp(void *arg, struct udp_pcb *pcb, struct pbuf *p,
74
   struct ip_addr *addr, u16_t port)
75
{
76
  struct netbuf *buf;
77
  struct netconn *conn;
78
 
79
  (void)pcb;
80
 
81
  conn = arg;
82
 
83
  if (conn == NULL) {
84
    pbuf_free(p);
85
    return;
86
  }
87
  if (conn->recvmbox != SYS_MBOX_NULL) {
88
    buf = memp_malloc(MEMP_NETBUF);
89
    if (buf == NULL) {
90
      pbuf_free(p);
91
      return;
92
    } else {
93
      buf->p = p;
94
      buf->ptr = p;
95
      buf->fromaddr = addr;
96
      buf->fromport = port;
97
    }
98
 
99
  conn->recv_avail += p->tot_len;
100
    /* Register event with callback */
101
    if (conn->callback)
102
        (*conn->callback)(conn, NETCONN_EVT_RCVPLUS, p->tot_len);
103
    sys_mbox_post(conn->recvmbox, buf);
104
  }
105
}
106
#endif /* LWIP_UDP */
107
#if LWIP_TCP
108
 
109
static err_t
110
recv_tcp(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
111
{
112
  struct netconn *conn;
113
  u16_t len;
114
 
115
  (void)pcb;
116
 
117
  conn = arg;
118
 
119
  if (conn == NULL) {
120
    pbuf_free(p);
121
    return ERR_VAL;
122
  }
123
 
124
  if (conn->recvmbox != SYS_MBOX_NULL) {
125
 
126
    conn->err = err;
127
    if (p != NULL) {
128
        len = p->tot_len;
129
        conn->recv_avail += len;
130
    }
131
    else
132
        len = 0;
133
    /* Register event with callback */
134
    if (conn->callback)
135
        (*conn->callback)(conn, NETCONN_EVT_RCVPLUS, len);
136
    sys_mbox_post(conn->recvmbox, p);
137
  }
138
  return ERR_OK;
139
}
140
 
141
 
142
static err_t
143
poll_tcp(void *arg, struct tcp_pcb *pcb)
144
{
145
  struct netconn *conn;
146
 
147
  (void)pcb;
148
 
149
  conn = arg;
150
  if (conn != NULL &&
151
     (conn->state == NETCONN_WRITE || conn->state == NETCONN_CLOSE) &&
152
     conn->sem != SYS_SEM_NULL) {
153
    sys_sem_signal(conn->sem);
154
  }
155
  return ERR_OK;
156
}
157
 
158
static err_t
159
sent_tcp(void *arg, struct tcp_pcb *pcb, u16_t len)
160
{
161
  struct netconn *conn;
162
 
163
  (void)pcb;
164
 
165
  conn = arg;
166
  if (conn != NULL && conn->sem != SYS_SEM_NULL) {
167
    sys_sem_signal(conn->sem);
168
  }
169
 
170
  if (conn && conn->callback)
171
      if (tcp_sndbuf(conn->pcb.tcp) > TCP_SNDLOWAT)
172
          (*conn->callback)(conn, NETCONN_EVT_SENDPLUS, len);
173
 
174
  return ERR_OK;
175
}
176
 
177
static void
178
err_tcp(void *arg, err_t err)
179
{
180
  struct netconn *conn;
181
 
182
  conn = arg;
183
 
184
  conn->pcb.tcp = NULL;
185
 
186
 
187
  conn->err = err;
188
  if (conn->recvmbox != SYS_MBOX_NULL) {
189
    /* Register event with callback */
190
    if (conn->callback)
191
      (*conn->callback)(conn, NETCONN_EVT_RCVPLUS, 0);
192
    sys_mbox_post(conn->recvmbox, NULL);
193
  }
194
  if (conn->mbox != SYS_MBOX_NULL) {
195
    sys_mbox_post(conn->mbox, NULL);
196
  }
197
  if (conn->acceptmbox != SYS_MBOX_NULL) {
198
     /* Register event with callback */
199
    if (conn->callback)
200
      (*conn->callback)(conn, NETCONN_EVT_RCVPLUS, 0);
201
    sys_mbox_post(conn->acceptmbox, NULL);
202
  }
203
  if (conn->sem != SYS_SEM_NULL) {
204
    sys_sem_signal(conn->sem);
205
  }
206
}
207
 
208
static void
209
setup_tcp(struct netconn *conn)
210
{
211
  struct tcp_pcb *pcb;
212
 
213
  pcb = conn->pcb.tcp;
214
  tcp_arg(pcb, conn);
215
  tcp_recv(pcb, recv_tcp);
216
  tcp_sent(pcb, sent_tcp);
217
  tcp_poll(pcb, poll_tcp, 4);
218
  tcp_err(pcb, err_tcp);
219
}
220
 
221
static err_t
222
accept_function(void *arg, struct tcp_pcb *newpcb, err_t err)
223
{
224
  sys_mbox_t mbox;
225
  struct netconn *newconn;
226
  struct netconn *conn;
227
 
228
#if API_MSG_DEBUG
229
#if TCP_DEBUG
230
  tcp_debug_print_state(newpcb->state);
231
#endif /* TCP_DEBUG */
232
#endif /* API_MSG_DEBUG */
233
  conn = (struct netconn *)arg;
234
  mbox = conn->acceptmbox;
235
  newconn = memp_malloc(MEMP_NETCONN);
236
  if (newconn == NULL) {
237
    return ERR_MEM;
238
  }
239
  newconn->type = NETCONN_TCP;
240
  newconn->pcb.tcp = newpcb;
241
  setup_tcp(newconn);
242
  newconn->recvmbox = sys_mbox_new();
243
  if (newconn->recvmbox == SYS_MBOX_NULL) {
244
    memp_free(MEMP_NETCONN, newconn);
245
    return ERR_MEM;
246
  }
247
  newconn->mbox = sys_mbox_new();
248
  if (newconn->mbox == SYS_MBOX_NULL) {
249
    sys_mbox_free(newconn->recvmbox);
250
    memp_free(MEMP_NETCONN, newconn);
251
    return ERR_MEM;
252
  }
253
  newconn->sem = sys_sem_new(0);
254
  if (newconn->sem == SYS_SEM_NULL) {
255
    sys_mbox_free(newconn->recvmbox);
256
    sys_mbox_free(newconn->mbox);
257
    memp_free(MEMP_NETCONN, newconn);
258
    return ERR_MEM;
259
  }
260
  newconn->acceptmbox = SYS_MBOX_NULL;
261
  newconn->err = err;
262
  /* Register event with callback */
263
  if (conn->callback)
264
  {
265
    (*conn->callback)(conn, NETCONN_EVT_RCVPLUS, 0);
266
    /* We have to set the callback here even though
267
     * the new socket is unknown. Mark the socket as -1. */
268
    newconn->callback = conn->callback;
269
    newconn->socket = -1;
270
  }
271
 
272
  sys_mbox_post(mbox, newconn);
273
  return ERR_OK;
274
}
275
#endif /* LWIP_TCP */
276
 
277
static void
278
do_newconn(struct api_msg_msg *msg)
279
{
280
   if(msg->conn->pcb.tcp != NULL) {
281
   /* This "new" connection already has a PCB allocated. */
282
   /* Is this an error condition? Should it be deleted?
283
      We currently just are happy and return. */
284
     sys_mbox_post(msg->conn->mbox, NULL);
285
     return;
286
   }
287
 
288
   msg->conn->err = ERR_OK;
289
 
290
   /* Allocate a PCB for this connection */
291
   switch(msg->conn->type) {
292
#if LWIP_RAW
293
   case NETCONN_RAW:
294
      msg->conn->pcb.raw = raw_new(msg->msg.bc.port); /* misusing the port field */
295
      raw_recv(msg->conn->pcb.raw, recv_raw, msg->conn);
296
     break;
297
#endif
298
#if LWIP_UDP
299
   case NETCONN_UDPLITE:
300
      msg->conn->pcb.udp = udp_new();
301
      if(msg->conn->pcb.udp == NULL) {
302
         msg->conn->err = ERR_MEM;
303
         break;
304
      }
305
      udp_setflags(msg->conn->pcb.udp, UDP_FLAGS_UDPLITE);
306
      udp_recv(msg->conn->pcb.udp, recv_udp, msg->conn);
307
      break;
308
   case NETCONN_UDPNOCHKSUM:
309
      msg->conn->pcb.udp = udp_new();
310
      if(msg->conn->pcb.udp == NULL) {
311
         msg->conn->err = ERR_MEM;
312
         break;
313
      }
314
      udp_setflags(msg->conn->pcb.udp, UDP_FLAGS_NOCHKSUM);
315
      udp_recv(msg->conn->pcb.udp, recv_udp, msg->conn);
316
      break;
317
   case NETCONN_UDP:
318
      msg->conn->pcb.udp = udp_new();
319
      if(msg->conn->pcb.udp == NULL) {
320
         msg->conn->err = ERR_MEM;
321
         break;
322
      }
323
      udp_recv(msg->conn->pcb.udp, recv_udp, msg->conn);
324
      break;
325
#endif /* LWIP_UDP */
326
#if LWIP_TCP
327
   case NETCONN_TCP:
328
      msg->conn->pcb.tcp = tcp_new();
329
      if(msg->conn->pcb.tcp == NULL) {
330
         msg->conn->err = ERR_MEM;
331
         break;
332
      }
333
      setup_tcp(msg->conn);
334
      break;
335
#endif
336
   }
337
 
338
 
339
  sys_mbox_post(msg->conn->mbox, NULL);
340
}
341
 
342
 
343
static void
344
do_delconn(struct api_msg_msg *msg)
345
{
346
  if (msg->conn->pcb.tcp != NULL) {
347
    switch (msg->conn->type) {
348
#if LWIP_RAW
349
    case NETCONN_RAW:
350
      raw_remove(msg->conn->pcb.raw);
351
      break;
352
#endif
353
#if LWIP_UDP
354
    case NETCONN_UDPLITE:
355
      /* FALLTHROUGH */
356
    case NETCONN_UDPNOCHKSUM:
357
      /* FALLTHROUGH */
358
    case NETCONN_UDP:
359
      msg->conn->pcb.udp->recv_arg = NULL;
360
      udp_remove(msg->conn->pcb.udp);
361
      break;
362
#endif /* LWIP_UDP */
363
#if LWIP_TCP      
364
    case NETCONN_TCP:
365
      if (msg->conn->pcb.tcp->state == LISTEN) {
366
  tcp_arg(msg->conn->pcb.tcp, NULL);
367
  tcp_accept(msg->conn->pcb.tcp, NULL);
368
  tcp_close(msg->conn->pcb.tcp);
369
      } else {
370
  tcp_arg(msg->conn->pcb.tcp, NULL);
371
  tcp_sent(msg->conn->pcb.tcp, NULL);
372
  tcp_recv(msg->conn->pcb.tcp, NULL);
373
  tcp_poll(msg->conn->pcb.tcp, NULL, 0);
374
  tcp_err(msg->conn->pcb.tcp, NULL);
375
  if (tcp_close(msg->conn->pcb.tcp) != ERR_OK) {
376
    tcp_abort(msg->conn->pcb.tcp);
377
  }
378
      }
379
#endif
380
    default:
381
    break;
382
    }
383
  }
384
  /* Trigger select() in socket layer */
385
  if (msg->conn->callback)
386
  {
387
      (*msg->conn->callback)(msg->conn, NETCONN_EVT_RCVPLUS, 0);
388
      (*msg->conn->callback)(msg->conn, NETCONN_EVT_SENDPLUS, 0);
389
  }
390
 
391
  if (msg->conn->mbox != SYS_MBOX_NULL) {
392
    sys_mbox_post(msg->conn->mbox, NULL);
393
  }
394
}
395
 
396
static void
397
do_bind(struct api_msg_msg *msg)
398
{
399
  if (msg->conn->pcb.tcp == NULL) {
400
    switch (msg->conn->type) {
401
#if LWIP_RAW
402
    case NETCONN_RAW:
403
      msg->conn->pcb.raw = raw_new(msg->msg.bc.port); /* misusing the port field as protocol */
404
      raw_recv(msg->conn->pcb.raw, recv_raw, msg->conn);
405
      break;
406
#endif
407
#if LWIP_UDP
408
    case NETCONN_UDPLITE:
409
      msg->conn->pcb.udp = udp_new();
410
      udp_setflags(msg->conn->pcb.udp, UDP_FLAGS_UDPLITE);
411
      udp_recv(msg->conn->pcb.udp, recv_udp, msg->conn);
412
      break;
413
    case NETCONN_UDPNOCHKSUM:
414
      msg->conn->pcb.udp = udp_new();
415
      udp_setflags(msg->conn->pcb.udp, UDP_FLAGS_NOCHKSUM);
416
      udp_recv(msg->conn->pcb.udp, recv_udp, msg->conn);
417
      break;
418
    case NETCONN_UDP:
419
      msg->conn->pcb.udp = udp_new();
420
      udp_recv(msg->conn->pcb.udp, recv_udp, msg->conn);
421
      break;
422
#endif /* LWIP_UDP */
423
#if LWIP_TCP      
424
    case NETCONN_TCP:
425
      msg->conn->pcb.tcp = tcp_new();
426
      setup_tcp(msg->conn);
427
#endif /* LWIP_TCP */
428
    default:
429
    break;
430
    }
431
  }
432
  switch (msg->conn->type) {
433
#if LWIP_RAW
434
  case NETCONN_RAW:
435
    msg->conn->err = raw_bind(msg->conn->pcb.raw,msg->msg.bc.ipaddr);
436
    break;
437
#endif
438
#if LWIP_UDP
439
  case NETCONN_UDPLITE:
440
    /* FALLTHROUGH */
441
  case NETCONN_UDPNOCHKSUM:
442
    /* FALLTHROUGH */
443
  case NETCONN_UDP:
444
    msg->conn->err = udp_bind(msg->conn->pcb.udp, msg->msg.bc.ipaddr, msg->msg.bc.port);
445
    break;
446
#endif /* LWIP_UDP */
447
#if LWIP_TCP
448
  case NETCONN_TCP:
449
    msg->conn->err = tcp_bind(msg->conn->pcb.tcp,
450
            msg->msg.bc.ipaddr, msg->msg.bc.port);
451
#endif /* LWIP_TCP */
452
  default:
453
    break;
454
  }
455
  sys_mbox_post(msg->conn->mbox, NULL);
456
}
457
#if LWIP_TCP
458
 
459
static err_t
460
do_connected(void *arg, struct tcp_pcb *pcb, err_t err)
461
{
462
  struct netconn *conn;
463
 
464
  (void)pcb;
465
 
466
  conn = arg;
467
 
468
  if (conn == NULL) {
469
    return ERR_VAL;
470
  }
471
 
472
  conn->err = err;
473
  if (conn->type == NETCONN_TCP && err == ERR_OK) {
474
    setup_tcp(conn);
475
  }
476
  sys_mbox_post(conn->mbox, NULL);
477
  return ERR_OK;
478
}
479
#endif  
480
 
481
static void
482
do_connect(struct api_msg_msg *msg)
483
{
484
  if (msg->conn->pcb.tcp == NULL) {
485
    switch (msg->conn->type) {
486
#if LWIP_RAW
487
    case NETCONN_RAW:
488
      msg->conn->pcb.raw = raw_new(msg->msg.bc.port); /* misusing the port field as protocol */
489
      raw_recv(msg->conn->pcb.raw, recv_raw, msg->conn);
490
      break;
491
#endif
492
#if LWIP_UDP
493
    case NETCONN_UDPLITE:
494
      msg->conn->pcb.udp = udp_new();
495
      if (msg->conn->pcb.udp == NULL) {
496
  msg->conn->err = ERR_MEM;
497
  sys_mbox_post(msg->conn->mbox, NULL);
498
  return;
499
      }
500
      udp_setflags(msg->conn->pcb.udp, UDP_FLAGS_UDPLITE);
501
      udp_recv(msg->conn->pcb.udp, recv_udp, msg->conn);
502
      break;
503
    case NETCONN_UDPNOCHKSUM:
504
      msg->conn->pcb.udp = udp_new();
505
      if (msg->conn->pcb.udp == NULL) {
506
  msg->conn->err = ERR_MEM;
507
  sys_mbox_post(msg->conn->mbox, NULL);
508
  return;
509
      }
510
      udp_setflags(msg->conn->pcb.udp, UDP_FLAGS_NOCHKSUM);
511
      udp_recv(msg->conn->pcb.udp, recv_udp, msg->conn);
512
      break;
513
    case NETCONN_UDP:
514
      msg->conn->pcb.udp = udp_new();
515
      if (msg->conn->pcb.udp == NULL) {
516
  msg->conn->err = ERR_MEM;
517
  sys_mbox_post(msg->conn->mbox, NULL);
518
  return;
519
      }
520
      udp_recv(msg->conn->pcb.udp, recv_udp, msg->conn);
521
      break;
522
#endif /* LWIP_UDP */
523
#if LWIP_TCP      
524
    case NETCONN_TCP:
525
      msg->conn->pcb.tcp = tcp_new();
526
      if (msg->conn->pcb.tcp == NULL) {
527
  msg->conn->err = ERR_MEM;
528
  sys_mbox_post(msg->conn->mbox, NULL);
529
  return;
530
      }
531
#endif
532
    default:
533
      break;
534
    }
535
  }
536
  switch (msg->conn->type) {
537
#if LWIP_RAW
538
  case NETCONN_RAW:
539
    raw_connect(msg->conn->pcb.raw, msg->msg.bc.ipaddr);
540
    sys_mbox_post(msg->conn->mbox, NULL);
541
    break;
542
#endif
543
#if LWIP_UDP
544
  case NETCONN_UDPLITE:
545
    /* FALLTHROUGH */
546
  case NETCONN_UDPNOCHKSUM:
547
    /* FALLTHROUGH */
548
  case NETCONN_UDP:
549
    udp_connect(msg->conn->pcb.udp, msg->msg.bc.ipaddr, msg->msg.bc.port);
550
    sys_mbox_post(msg->conn->mbox, NULL);
551
    break;
552
#endif 
553
#if LWIP_TCP      
554
  case NETCONN_TCP:
555
    /*    tcp_arg(msg->conn->pcb.tcp, msg->conn);*/
556
    setup_tcp(msg->conn);
557
    tcp_connect(msg->conn->pcb.tcp, msg->msg.bc.ipaddr, msg->msg.bc.port,
558
    do_connected);
559
    /*tcp_output(msg->conn->pcb.tcp);*/
560
#endif
561
 
562
  default:
563
    break;
564
  }
565
}
566
 
567
static void
568
do_disconnect(struct api_msg_msg *msg)
569
{
570
 
571
  switch (msg->conn->type) {
572
#if LWIP_RAW
573
  case NETCONN_RAW:
574
    /* Do nothing as connecting is only a helper for upper lwip layers */
575
    break;
576
#endif
577
#if LWIP_UDP
578
  case NETCONN_UDPLITE:
579
    /* FALLTHROUGH */
580
  case NETCONN_UDPNOCHKSUM:
581
    /* FALLTHROUGH */
582
  case NETCONN_UDP:
583
    udp_disconnect(msg->conn->pcb.udp);
584
    break;
585
#endif 
586
  case NETCONN_TCP:
587
    break;
588
  }
589
  sys_mbox_post(msg->conn->mbox, NULL);
590
}
591
 
592
 
593
static void
594
do_listen(struct api_msg_msg *msg)
595
{
596
  if (msg->conn->pcb.tcp != NULL) {
597
    switch (msg->conn->type) {
598
#if LWIP_RAW
599
    case NETCONN_RAW:
600
      LWIP_DEBUGF(API_MSG_DEBUG, ("api_msg: listen RAW: cannot listen for RAW.\n"));
601
      break;
602
#endif
603
#if LWIP_UDP
604
    case NETCONN_UDPLITE:
605
      /* FALLTHROUGH */
606
    case NETCONN_UDPNOCHKSUM:
607
      /* FALLTHROUGH */
608
    case NETCONN_UDP:
609
      LWIP_DEBUGF(API_MSG_DEBUG, ("api_msg: listen UDP: cannot listen for UDP.\n"));
610
      break;
611
#endif /* LWIP_UDP */
612
#if LWIP_TCP      
613
    case NETCONN_TCP:
614
      msg->conn->pcb.tcp = tcp_listen(msg->conn->pcb.tcp);
615
      if (msg->conn->pcb.tcp == NULL) {
616
  msg->conn->err = ERR_MEM;
617
      } else {
618
  if (msg->conn->acceptmbox == SYS_MBOX_NULL) {
619
    msg->conn->acceptmbox = sys_mbox_new();
620
    if (msg->conn->acceptmbox == SYS_MBOX_NULL) {
621
      msg->conn->err = ERR_MEM;
622
      break;
623
    }
624
  }
625
  tcp_arg(msg->conn->pcb.tcp, msg->conn);
626
  tcp_accept(msg->conn->pcb.tcp, accept_function);
627
      }
628
#endif
629
    default:
630
      break;
631
    }
632
  }
633
  sys_mbox_post(msg->conn->mbox, NULL);
634
}
635
 
636
static void
637
do_accept(struct api_msg_msg *msg)
638
{
639
  if (msg->conn->pcb.tcp != NULL) {
640
    switch (msg->conn->type) {
641
#if LWIP_RAW
642
    case NETCONN_RAW:
643
      LWIP_DEBUGF(API_MSG_DEBUG, ("api_msg: accept RAW: cannot accept for RAW.\n"));
644
      break;
645
#endif
646
#if LWIP_UDP
647
    case NETCONN_UDPLITE:
648
      /* FALLTHROUGH */
649
    case NETCONN_UDPNOCHKSUM:
650
      /* FALLTHROUGH */
651
    case NETCONN_UDP:
652
      LWIP_DEBUGF(API_MSG_DEBUG, ("api_msg: accept UDP: cannot accept for UDP.\n"));
653
      break;
654
#endif /* LWIP_UDP */
655
    case NETCONN_TCP:
656
      break;
657
    }
658
  }
659
}
660
 
661
static void
662
do_send(struct api_msg_msg *msg)
663
{
664
  if (msg->conn->pcb.tcp != NULL) {
665
    switch (msg->conn->type) {
666
#if LWIP_RAW
667
    case NETCONN_RAW:
668
      raw_send(msg->conn->pcb.raw, msg->msg.p);
669
      break;
670
#endif
671
#if LWIP_UDP
672
    case NETCONN_UDPLITE:
673
      /* FALLTHROUGH */
674
    case NETCONN_UDPNOCHKSUM:
675
      /* FALLTHROUGH */
676
    case NETCONN_UDP:
677
      udp_send(msg->conn->pcb.udp, msg->msg.p);
678
      break;
679
#endif /* LWIP_UDP */
680
    case NETCONN_TCP:
681
      break;
682
    }
683
  }
684
  sys_mbox_post(msg->conn->mbox, NULL);
685
}
686
 
687
static void
688
do_recv(struct api_msg_msg *msg)
689
{
690
#if LWIP_TCP
691
  if (msg->conn->pcb.tcp != NULL) {
692
    if (msg->conn->type == NETCONN_TCP) {
693
      tcp_recved(msg->conn->pcb.tcp, msg->msg.len);
694
    }
695
  }
696
#endif  
697
  sys_mbox_post(msg->conn->mbox, NULL);
698
}
699
 
700
static void
701
do_write(struct api_msg_msg *msg)
702
{
703
#if LWIP_TCP  
704
  err_t err;
705
#endif  
706
  if (msg->conn->pcb.tcp != NULL) {
707
    switch (msg->conn->type) {
708
#if LWIP_RAW
709
    case NETCONN_RAW:
710
      msg->conn->err = ERR_VAL;
711
      break;
712
#endif
713
#if LWIP_UDP 
714
    case NETCONN_UDPLITE:
715
      /* FALLTHROUGH */
716
    case NETCONN_UDPNOCHKSUM:
717
      /* FALLTHROUGH */
718
    case NETCONN_UDP:
719
      msg->conn->err = ERR_VAL;
720
      break;
721
#endif /* LWIP_UDP */
722
#if LWIP_TCP 
723
    case NETCONN_TCP:
724
      err = tcp_write(msg->conn->pcb.tcp, msg->msg.w.dataptr,
725
                      msg->msg.w.len, msg->msg.w.copy);
726
      /* This is the Nagle algorithm: inhibit the sending of new TCP
727
   segments when new outgoing data arrives from the user if any
728
   previously transmitted data on the connection remains
729
   unacknowledged. */
730
      if(err == ERR_OK && (msg->conn->pcb.tcp->unacked == NULL || (msg->conn->pcb.tcp->flags & TF_NODELAY)) ) {
731
  tcp_output(msg->conn->pcb.tcp);
732
      }
733
      msg->conn->err = err;
734
      if (msg->conn->callback)
735
          if (err == ERR_OK)
736
          {
737
              if (tcp_sndbuf(msg->conn->pcb.tcp) <= TCP_SNDLOWAT)
738
                  (*msg->conn->callback)(msg->conn, NETCONN_EVT_SENDMINUS, msg->msg.w.len);
739
          }
740
#endif
741
    default:
742
      break;
743
    }
744
  }
745
  sys_mbox_post(msg->conn->mbox, NULL);
746
}
747
 
748
static void
749
do_close(struct api_msg_msg *msg)
750
{
751
  err_t err;
752
 
753
  err = ERR_OK;
754
 
755
  if (msg->conn->pcb.tcp != NULL) {
756
    switch (msg->conn->type) {
757
#if LWIP_RAW
758
    case NETCONN_RAW:
759
      break;
760
#endif
761
#if LWIP_UDP
762
    case NETCONN_UDPLITE:
763
      /* FALLTHROUGH */
764
    case NETCONN_UDPNOCHKSUM:
765
      /* FALLTHROUGH */
766
    case NETCONN_UDP:
767
      break;
768
#endif /* LWIP_UDP */
769
#if LWIP_TCP
770
    case NETCONN_TCP:
771
      if (msg->conn->pcb.tcp->state == LISTEN) {
772
  err = tcp_close(msg->conn->pcb.tcp);
773
      }
774
      msg->conn->err = err;
775
#endif
776
    default:
777
      break;
778
    }
779
  }
780
  sys_mbox_post(msg->conn->mbox, NULL);
781
}
782
 
783
typedef void (* api_msg_decode)(struct api_msg_msg *msg);
784
static api_msg_decode decode[API_MSG_MAX] = {
785
  do_newconn,
786
  do_delconn,
787
  do_bind,
788
  do_connect,
789
  do_disconnect,
790
  do_listen,
791
  do_accept,
792
  do_send,
793
  do_recv,
794
  do_write,
795
  do_close
796
  };
797
void
798
api_msg_input(struct api_msg *msg)
799
{
800
  decode[msg->type](&(msg->msg));
801
}
802
 
803
void
804
api_msg_post(struct api_msg *msg)
805
{
806
  tcpip_apimsg(msg);
807
}
808
 
809
 
810
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.