Fix bug which eats pbufs if SLIP_END comes at a pbuf boundary.Also cleaned up and commented the code.

This commit is contained in:
jani 2002-11-14 08:03:25 +00:00
parent 05a91a4725
commit ecf0f56d33

View File

@ -31,13 +31,17 @@
* Author: Magnus Ivarsson <magnus.ivarsson(at)volvo.com> * Author: Magnus Ivarsson <magnus.ivarsson(at)volvo.com>
*/ */
/*
* This is an arch independent SLIP netif. The specific serial hooks must be provided
* by another file.They are sio_open, sio_recv and sio_send
*/
#include "netif/slipif.h" #include "netif/slipif.h"
#include "lwip/debug.h" #include "lwip/debug.h"
#include "lwip/def.h" #include "lwip/def.h"
#include "lwip/pbuf.h" #include "lwip/pbuf.h"
#include "lwip/sys.h" #include "lwip/sys.h"
#include "lwip/stats.h" #include "lwip/stats.h"
#include "netif/sio.h"
#define SLIP_END 0300 #define SLIP_END 0300
#define SLIP_ESC 0333 #define SLIP_ESC 0333
@ -45,53 +49,54 @@
#define SLIP_ESC_ESC 0335 #define SLIP_ESC_ESC 0335
#define MAX_SIZE 1500 #define MAX_SIZE 1500
#define SLIPIF_NUM_OF_INTERFACES 2
typedef struct slip_status_t { /**
void *sio; * Send a pbuf doing the necessary SLIP encapsulation
} slip_status_t; *
* Uses the serial layer's sio_send()
/* yes, this is ugly; TODO: should be dynamicaly allocated instead */ */
static slip_status_t statusar[SLIPIF_NUM_OF_INTERFACES];
/*-----------------------------------------------------------------------------------*/
err_t err_t
slipif_output(struct netif *netif, struct pbuf *p, struct ip_addr *ipaddr) slipif_output(struct netif *netif, struct pbuf *p, struct ip_addr *ipaddr)
{ {
slip_status_t *slipState = (slip_status_t *)netif->state;
struct pbuf *q; struct pbuf *q;
int i; int i;
u8_t c; u8_t c;
/* Send pbuf out on the serial I/O device. */ /* Send pbuf out on the serial I/O device. */
sio_send(SLIP_END, slipState->sio); sio_send(SLIP_END, netif->state);
for(q = p; q != NULL; q = q->next) { for(q = p; q != NULL; q = q->next) {
for(i = 0; i < q->len; i++) { for(i = 0; i < q->len; i++) {
c = ((u8_t *)q->payload)[i]; c = ((u8_t *)q->payload)[i];
switch(c) { switch(c) {
case SLIP_END: case SLIP_END:
sio_send(SLIP_ESC, slipState->sio); sio_send(SLIP_ESC, netif->state);
sio_send(SLIP_ESC_END, slipState->sio); sio_send(SLIP_ESC_END, netif->state);
break; break;
case SLIP_ESC: case SLIP_ESC:
sio_send(SLIP_ESC, slipState->sio); sio_send(SLIP_ESC, netif->state);
sio_send(SLIP_ESC_ESC, slipState->sio); sio_send(SLIP_ESC_ESC, netif->state);
break; break;
default: default:
sio_send(c, slipState->sio); sio_send(c, netif->state);
break; break;
} }
} }
} }
sio_send(SLIP_END, slipState->sio); sio_send(SLIP_END, netif->state);
return 0; return 0;
} }
/*-----------------------------------------------------------------------------------*/
/**
* Handle the incoming SLIP stream character by character
*
* Poll the serial layer by calling sio_recv()
*
* @return The IP packet when SLIP_END is received
*/
static struct pbuf * static struct pbuf *
slipif_input( struct netif * netif ) slipif_input( struct netif * netif )
{ {
slip_status_t *slipState = (slip_status_t *)netif->state;
u8_t c; u8_t c;
struct pbuf *p, *q; struct pbuf *p, *q;
int recved; int recved;
@ -102,12 +107,9 @@ slipif_input( struct netif * netif )
c = 0; c = 0;
while(1) { while(1) {
c = sio_recv(slipState->sio); c = sio_recv(netif->state);
switch(c) { switch(c) {
case SLIP_END: case SLIP_END:
if(p == NULL) {
return slipif_input(netif);
}
if(recved > 0) { if(recved > 0) {
/* Received whole packet. */ /* Received whole packet. */
pbuf_realloc(q, recved); pbuf_realloc(q, recved);
@ -122,7 +124,7 @@ slipif_input( struct netif * netif )
break; break;
case SLIP_ESC: case SLIP_ESC:
c = sio_recv(slipState->sio); c = sio_recv(netif->state);
switch(c) { switch(c) {
case SLIP_ESC_END: case SLIP_ESC_END:
c = SLIP_END; c = SLIP_END;
@ -166,40 +168,41 @@ slipif_input( struct netif * netif )
} }
return NULL; return NULL;
} }
/*-----------------------------------------------------------------------------------*/
/**
* The SLIP input thread
*
* Feed the IP layer with incoming packets
*/
static void static void
slipif_loop(void *nf) slipif_loop(void *nf)
{ {
struct pbuf *p; struct pbuf *p;
struct netif *netif = (struct netif *)nf; struct netif *netif = (struct netif *)nf;
/* slip_status_t *slipState = (slip_status_t *) netif->state; */
while(1) { while(1) {
p = slipif_input(netif); p = slipif_input(netif);
netif->input(p, netif); netif->input(p, netif);
} }
} }
/*-----------------------------------------------------------------------------------*/
/**
* SLIP netif initialization
*
* Call the arch specific sio_open and remember
* the opened device in the state field of the netif.
*/
void void
slipif_init(struct netif *netif) slipif_init(struct netif *netif)
{ {
slip_status_t *slipState;
DEBUGF(SLIP_DEBUG, ("slipif_init: netif->num=%x\n", (int)netif->num)); DEBUGF(SLIP_DEBUG, ("slipif_init: netif->num=%x\n", (int)netif->num));
if(netif->num >= SLIPIF_NUM_OF_INTERFACES) {
DEBUGF( SLIP_DEBUG, ("ERROR: To many slipifs"));
return;
}
/* dynamic allocation would be nice */
netif->state = &statusar[netif->num];
netif->name[0] = 's'; netif->name[0] = 's';
netif->name[1] = 'l'; netif->name[1] = 'l';
netif->output = slipif_output; netif->output = slipif_output;
slipState = (slip_status_t *)netif->state; netif->state = sio_open(netif->num);
slipState->sio = sio_open(netif->num);
sys_thread_new(slipif_loop, netif); sys_thread_new(slipif_loop, netif);
} }
/*-----------------------------------------------------------------------------------*/