Back to home page

Quest Cross Reference

 
 

    


Warning, cross-references for /kernel/lwip/netif/slipif.c need to be fixed.

0001 /**
0002  * @file
0003  * SLIP Interface
0004  *
0005  */
0006 
0007 /*
0008  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
0009  * All rights reserved. 
0010  *
0011  * Redistribution and use in source and binary forms, with or without 
0012  * modification, are permitted provided that the following conditions 
0013  * are met: 
0014  * 1. Redistributions of source code must retain the above copyright 
0015  *    notice, this list of conditions and the following disclaimer. 
0016  * 2. Redistributions in binary form must reproduce the above copyright 
0017  *    notice, this list of conditions and the following disclaimer in the 
0018  *    documentation and/or other materials provided with the distribution. 
0019  * 3. Neither the name of the Institute nor the names of its contributors 
0020  *    may be used to endorse or promote products derived from this software 
0021  *    without specific prior written permission. 
0022  *
0023  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 
0024  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
0025  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
0026  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 
0027  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
0028  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
0029  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
0030  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
0031  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
0032  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
0033  * SUCH DAMAGE. 
0034  *
0035  * This file is built upon the file: src/arch/rtxc/netif/sioslip.c
0036  *
0037  * Author: Magnus Ivarsson <magnus.ivarsson(at)volvo.com> 
0038  */
0039 
0040 /* 
0041  * This is an arch independent SLIP netif. The specific serial hooks must be
0042  * provided by another file. They are sio_open, sio_read/sio_tryread and sio_send
0043  */
0044 
0045 #include "netif/slipif.h"
0046 #include "lwip/opt.h"
0047 
0048 #if LWIP_HAVE_SLIPIF
0049 
0050 #include "lwip/def.h"
0051 #include "lwip/pbuf.h"
0052 #include "lwip/sys.h"
0053 #include "lwip/stats.h"
0054 #include "lwip/snmp.h"
0055 #include "lwip/sio.h"
0056 
0057 #define SLIP_BLOCK     1
0058 #define SLIP_DONTBLOCK 0
0059 
0060 #define SLIP_END     0300 /* 0xC0 */
0061 #define SLIP_ESC     0333 /* 0xDB */
0062 #define SLIP_ESC_END 0334 /* 0xDC */
0063 #define SLIP_ESC_ESC 0335 /* 0xDD */
0064 
0065 #define SLIP_MAX_SIZE 1500
0066 
0067 enum slipif_recv_state {
0068     SLIP_RECV_NORMAL,
0069     SLIP_RECV_ESCAPE,
0070 };
0071 
0072 struct slipif_priv {
0073   sio_fd_t sd;
0074   /* q is the whole pbuf chain for a packet, p is the current pbuf in the chain */
0075   struct pbuf *p, *q;
0076   enum slipif_recv_state state;
0077   u16_t i, recved;
0078 };
0079 
0080 /**
0081  * Send a pbuf doing the necessary SLIP encapsulation
0082  *
0083  * Uses the serial layer's sio_send()
0084  *
0085  * @param netif the lwip network interface structure for this slipif
0086  * @param p the pbuf chaing packet to send
0087  * @param ipaddr the ip address to send the packet to (not used for slipif)
0088  * @return always returns ERR_OK since the serial layer does not provide return values
0089  */
0090 err_t
0091 slipif_output(struct netif *netif, struct pbuf *p, struct ip_addr *ipaddr)
0092 {
0093   struct slipif_priv *priv;
0094   struct pbuf *q;
0095   u16_t i;
0096   u8_t c;
0097 
0098   LWIP_ASSERT("netif != NULL", (netif != NULL));
0099   LWIP_ASSERT("netif->state != NULL", (netif->state != NULL));
0100   LWIP_ASSERT("p != NULL", (p != NULL));
0101 
0102   LWIP_UNUSED_ARG(ipaddr);
0103 
0104   priv = netif->state;
0105 
0106   /* Send pbuf out on the serial I/O device. */
0107   sio_send(SLIP_END, priv->sd);
0108 
0109   for (q = p; q != NULL; q = q->next) {
0110     for (i = 0; i < q->len; i++) {
0111       c = ((u8_t *)q->payload)[i];
0112       switch (c) {
0113       case SLIP_END:
0114         sio_send(SLIP_ESC, priv->sd);
0115         sio_send(SLIP_ESC_END, priv->sd);
0116         break;
0117       case SLIP_ESC:
0118         sio_send(SLIP_ESC, priv->sd);
0119         sio_send(SLIP_ESC_ESC, priv->sd);
0120         break;
0121       default:
0122         sio_send(c, priv->sd);
0123         break;
0124       }
0125     }
0126   }
0127   sio_send(SLIP_END, priv->sd);
0128   return ERR_OK;
0129 }
0130 
0131 /**
0132  * Static function for easy use of blockig or non-blocking
0133  * sio_read
0134  *
0135  * @param fd serial device handle
0136  * @param data pointer to data buffer for receiving
0137  * @param len maximum length (in bytes) of data to receive
0138  * @param block if 1, call sio_read; if 0, call sio_tryread
0139  * @return return value of sio_read of sio_tryread
0140  */
0141 static u32_t
0142 slip_sio_read(sio_fd_t fd, u8_t* data, u32_t len, u8_t block)
0143 {
0144   if (block) {
0145     return sio_read(fd, data, len);
0146   } else {
0147     return sio_tryread(fd, data, len);
0148   }
0149 }
0150 
0151 /**
0152  * Handle the incoming SLIP stream character by character
0153  *
0154  * Poll the serial layer by calling sio_read() or sio_tryread().
0155  *
0156  * @param netif the lwip network interface structure for this slipif
0157  * @param block if 1, block until data is received; if 0, return when all data
0158  *        from the buffer is received (multiple calls to this function will
0159  *        return a complete packet, NULL is returned before - used for polling)
0160  * @return The IP packet when SLIP_END is received
0161  */
0162 static struct pbuf *
0163 slipif_input(struct netif *netif, u8_t block)
0164 {
0165   struct slipif_priv *priv;
0166   u8_t c;
0167   struct pbuf *t;
0168 
0169   LWIP_ASSERT("netif != NULL", (netif != NULL));
0170   LWIP_ASSERT("netif->state != NULL", (netif->state != NULL));
0171 
0172   priv = netif->state;
0173 
0174   while (slip_sio_read(priv->sd, &c, 1, block) > 0) {
0175     switch (priv->state) {
0176     case SLIP_RECV_NORMAL:
0177       switch (c) {
0178       case SLIP_END:
0179         if (priv->recved > 0) {
0180           /* Received whole packet. */
0181           /* Trim the pbuf to the size of the received packet. */
0182           pbuf_realloc(priv->q, priv->recved);
0183 
0184           LINK_STATS_INC(link.recv);
0185 
0186           LWIP_DEBUGF(SLIP_DEBUG, ("slipif: Got packet\n"));
0187           t = priv->q;
0188           priv->p = priv->q = NULL;
0189           priv->i = priv->recved = 0;
0190           return t;
0191         }
0192         continue;
0193       case SLIP_ESC:
0194         priv->state = SLIP_RECV_ESCAPE;
0195         continue;
0196       }
0197       break;
0198     case SLIP_RECV_ESCAPE:
0199       switch (c) {
0200       case SLIP_ESC_END:
0201         c = SLIP_END;
0202         break;
0203       case SLIP_ESC_ESC:
0204         c = SLIP_ESC;
0205         break;
0206       }
0207       priv->state = SLIP_RECV_NORMAL;
0208       /* FALLTHROUGH */
0209     }
0210 
0211     /* byte received, packet not yet completely received */
0212     if (priv->p == NULL) {
0213       /* allocate a new pbuf */
0214       LWIP_DEBUGF(SLIP_DEBUG, ("slipif_input: alloc\n"));
0215       priv->p = pbuf_alloc(PBUF_LINK, (PBUF_POOL_BUFSIZE - PBUF_LINK_HLEN), PBUF_POOL);
0216 
0217       if (priv->p == NULL) {
0218         LINK_STATS_INC(link.drop);
0219         LWIP_DEBUGF(SLIP_DEBUG, ("slipif_input: no new pbuf! (DROP)\n"));
0220         /* don't process any further since we got no pbuf to receive to */
0221         break;
0222       }
0223 
0224       if (priv->q != NULL) {
0225         /* 'chain' the pbuf to the existing chain */
0226         pbuf_cat(priv->q, priv->p);
0227       } else {
0228         /* p is the first pbuf in the chain */
0229         priv->q = priv->p;
0230       }
0231     }
0232 
0233     /* this automatically drops bytes if > SLIP_MAX_SIZE */
0234     if ((priv->p != NULL) && (priv->recved <= SLIP_MAX_SIZE)) {
0235       ((u8_t *)priv->p->payload)[priv->i] = c;
0236       priv->recved++;
0237       priv->i++;
0238       if (priv->i >= priv->p->len) {
0239         /* on to the next pbuf */
0240         priv->i = 0;
0241         if (priv->p->next != NULL && priv->p->next->len > 0) {
0242           /* p is a chain, on to the next in the chain */
0243             priv->p = priv->p->next;
0244         } else {
0245           /* p is a single pbuf, set it to NULL so next time a new
0246            * pbuf is allocated */
0247             priv->p = NULL;
0248         }
0249       }
0250     }
0251   }
0252 
0253   return NULL;
0254 }
0255 
0256 #if !NO_SYS
0257 /**
0258  * The SLIP input thread.
0259  *
0260  * Feed the IP layer with incoming packets
0261  *
0262  * @param nf the lwip network interface structure for this slipif
0263  */
0264 static void
0265 slipif_loop_thread(void *nf)
0266 {
0267   struct pbuf *p;
0268   struct netif *netif = (struct netif *)nf;
0269 
0270   while (1) {
0271     p = slipif_input(netif, SLIP_BLOCK);
0272     if (p != NULL) {
0273       if (netif->input(p, netif) != ERR_OK) {
0274         pbuf_free(p);
0275         p = NULL;
0276       }
0277     }
0278   }
0279 }
0280 #endif /* !NO_SYS */
0281 
0282 /**
0283  * SLIP netif initialization
0284  *
0285  * Call the arch specific sio_open and remember
0286  * the opened device in the state field of the netif.
0287  *
0288  * @param netif the lwip network interface structure for this slipif
0289  * @return ERR_OK if serial line could be opened,
0290  *         ERR_MEM if no memory could be allocated,
0291  *         ERR_IF is serial line couldn't be opened
0292  *
0293  * @note netif->num must contain the number of the serial port to open
0294  *       (0 by default)
0295  */
0296 err_t
0297 slipif_init(struct netif *netif)
0298 {
0299   struct slipif_priv *priv;
0300 
0301   LWIP_DEBUGF(SLIP_DEBUG, ("slipif_init: netif->num=%"U16_F"\n", (u16_t)netif->num));
0302 
0303   /* Allocate private data */
0304   priv = mem_malloc(sizeof(struct slipif_priv));
0305   if (!priv) {
0306     return ERR_MEM;
0307   }
0308 
0309   netif->name[0] = 's';
0310   netif->name[1] = 'l';
0311   netif->output = slipif_output;
0312   netif->mtu = SLIP_MAX_SIZE;
0313   netif->flags |= NETIF_FLAG_POINTTOPOINT;
0314 
0315   /* Try to open the serial port (netif->num contains the port number). */
0316   priv->sd = sio_open(netif->num);
0317   if (!priv->sd) {
0318     /* Opening the serial port failed. */
0319     mem_free(priv);
0320     return ERR_IF;
0321   }
0322 
0323   /* Initialize private data */
0324   priv->p = NULL;
0325   priv->q = NULL;
0326   priv->state = SLIP_RECV_NORMAL;
0327   priv->i = 0;
0328   priv->recved = 0;
0329 
0330   netif->state = priv;
0331 
0332   /* initialize the snmp variables and counters inside the struct netif
0333    * ifSpeed: no assumption can be made without knowing more about the
0334    * serial line!
0335    */
0336   NETIF_INIT_SNMP(netif, snmp_ifType_slip, 0);
0337 
0338   /* Create a thread to poll the serial line. */
0339   sys_thread_new(SLIPIF_THREAD_NAME, slipif_loop_thread, netif,
0340     SLIPIF_THREAD_STACKSIZE, SLIPIF_THREAD_PRIO);
0341   return ERR_OK;
0342 }
0343 
0344 /**
0345  * Polls the serial device and feeds the IP layer with incoming packets.
0346  *
0347  * @param netif The lwip network interface structure for this slipif
0348  */
0349 void
0350 slipif_poll(struct netif *netif)
0351 {
0352   struct pbuf *p;
0353   struct slipif_priv *priv;
0354 
0355   LWIP_ASSERT("netif != NULL", (netif != NULL));
0356   LWIP_ASSERT("netif->state != NULL", (netif->state != NULL));
0357 
0358   priv = netif->state;
0359 
0360   while ((p = slipif_input(netif, SLIP_DONTBLOCK)) != NULL) {
0361     if (netif->input(p, netif) != ERR_OK) {
0362       pbuf_free(p);
0363     }
0364   }
0365 }
0366 
0367 #endif /* LWIP_HAVE_SLIPIF */