Integrating the ublox LISA C200 modem

Fork of SprintUSBModemHTTPClientTest by Donatien Garnier

Committer:
sam_grove
Date:
Thu Sep 26 00:44:20 2013 -0500
Revision:
5:3f93dd1d4cb3
Exported program and replaced contents of the repo with the source
to build and debug using keil mdk. Libs NOT upto date are lwip, lwip-sys
and socket. these have newer versions under mbed_official but were starting
from a know working point

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sam_grove 5:3f93dd1d4cb3 1 /**
sam_grove 5:3f93dd1d4cb3 2 * @file
sam_grove 5:3f93dd1d4cb3 3 * SLIP Interface
sam_grove 5:3f93dd1d4cb3 4 *
sam_grove 5:3f93dd1d4cb3 5 */
sam_grove 5:3f93dd1d4cb3 6
sam_grove 5:3f93dd1d4cb3 7 /*
sam_grove 5:3f93dd1d4cb3 8 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
sam_grove 5:3f93dd1d4cb3 9 * All rights reserved.
sam_grove 5:3f93dd1d4cb3 10 *
sam_grove 5:3f93dd1d4cb3 11 * Redistribution and use in source and binary forms, with or without
sam_grove 5:3f93dd1d4cb3 12 * modification, are permitted provided that the following conditions
sam_grove 5:3f93dd1d4cb3 13 * are met:
sam_grove 5:3f93dd1d4cb3 14 * 1. Redistributions of source code must retain the above copyright
sam_grove 5:3f93dd1d4cb3 15 * notice, this list of conditions and the following disclaimer.
sam_grove 5:3f93dd1d4cb3 16 * 2. Redistributions in binary form must reproduce the above copyright
sam_grove 5:3f93dd1d4cb3 17 * notice, this list of conditions and the following disclaimer in the
sam_grove 5:3f93dd1d4cb3 18 * documentation and/or other materials provided with the distribution.
sam_grove 5:3f93dd1d4cb3 19 * 3. Neither the name of the Institute nor the names of its contributors
sam_grove 5:3f93dd1d4cb3 20 * may be used to endorse or promote products derived from this software
sam_grove 5:3f93dd1d4cb3 21 * without specific prior written permission.
sam_grove 5:3f93dd1d4cb3 22 *
sam_grove 5:3f93dd1d4cb3 23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
sam_grove 5:3f93dd1d4cb3 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
sam_grove 5:3f93dd1d4cb3 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
sam_grove 5:3f93dd1d4cb3 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
sam_grove 5:3f93dd1d4cb3 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
sam_grove 5:3f93dd1d4cb3 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
sam_grove 5:3f93dd1d4cb3 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
sam_grove 5:3f93dd1d4cb3 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
sam_grove 5:3f93dd1d4cb3 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
sam_grove 5:3f93dd1d4cb3 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
sam_grove 5:3f93dd1d4cb3 33 * SUCH DAMAGE.
sam_grove 5:3f93dd1d4cb3 34 *
sam_grove 5:3f93dd1d4cb3 35 * This file is built upon the file: src/arch/rtxc/netif/sioslip.c
sam_grove 5:3f93dd1d4cb3 36 *
sam_grove 5:3f93dd1d4cb3 37 * Author: Magnus Ivarsson <magnus.ivarsson(at)volvo.com>
sam_grove 5:3f93dd1d4cb3 38 */
sam_grove 5:3f93dd1d4cb3 39
sam_grove 5:3f93dd1d4cb3 40 /*
sam_grove 5:3f93dd1d4cb3 41 * This is an arch independent SLIP netif. The specific serial hooks must be
sam_grove 5:3f93dd1d4cb3 42 * provided by another file. They are sio_open, sio_read/sio_tryread and sio_send
sam_grove 5:3f93dd1d4cb3 43 */
sam_grove 5:3f93dd1d4cb3 44
sam_grove 5:3f93dd1d4cb3 45 #include "netif/slipif.h"
sam_grove 5:3f93dd1d4cb3 46 #include "lwip/opt.h"
sam_grove 5:3f93dd1d4cb3 47
sam_grove 5:3f93dd1d4cb3 48 #if LWIP_HAVE_SLIPIF
sam_grove 5:3f93dd1d4cb3 49
sam_grove 5:3f93dd1d4cb3 50 #include "lwip/def.h"
sam_grove 5:3f93dd1d4cb3 51 #include "lwip/pbuf.h"
sam_grove 5:3f93dd1d4cb3 52 #include "lwip/sys.h"
sam_grove 5:3f93dd1d4cb3 53 #include "lwip/stats.h"
sam_grove 5:3f93dd1d4cb3 54 #include "lwip/snmp.h"
sam_grove 5:3f93dd1d4cb3 55 #include "lwip/sio.h"
sam_grove 5:3f93dd1d4cb3 56
sam_grove 5:3f93dd1d4cb3 57 #define SLIP_BLOCK 1
sam_grove 5:3f93dd1d4cb3 58 #define SLIP_DONTBLOCK 0
sam_grove 5:3f93dd1d4cb3 59
sam_grove 5:3f93dd1d4cb3 60 #define SLIP_END 0300 /* 0xC0 */
sam_grove 5:3f93dd1d4cb3 61 #define SLIP_ESC 0333 /* 0xDB */
sam_grove 5:3f93dd1d4cb3 62 #define SLIP_ESC_END 0334 /* 0xDC */
sam_grove 5:3f93dd1d4cb3 63 #define SLIP_ESC_ESC 0335 /* 0xDD */
sam_grove 5:3f93dd1d4cb3 64
sam_grove 5:3f93dd1d4cb3 65 #define SLIP_MAX_SIZE 1500
sam_grove 5:3f93dd1d4cb3 66
sam_grove 5:3f93dd1d4cb3 67 enum slipif_recv_state {
sam_grove 5:3f93dd1d4cb3 68 SLIP_RECV_NORMAL,
sam_grove 5:3f93dd1d4cb3 69 SLIP_RECV_ESCAPE,
sam_grove 5:3f93dd1d4cb3 70 };
sam_grove 5:3f93dd1d4cb3 71
sam_grove 5:3f93dd1d4cb3 72 struct slipif_priv {
sam_grove 5:3f93dd1d4cb3 73 sio_fd_t sd;
sam_grove 5:3f93dd1d4cb3 74 /* q is the whole pbuf chain for a packet, p is the current pbuf in the chain */
sam_grove 5:3f93dd1d4cb3 75 struct pbuf *p, *q;
sam_grove 5:3f93dd1d4cb3 76 enum slipif_recv_state state;
sam_grove 5:3f93dd1d4cb3 77 u16_t i, recved;
sam_grove 5:3f93dd1d4cb3 78 };
sam_grove 5:3f93dd1d4cb3 79
sam_grove 5:3f93dd1d4cb3 80 /**
sam_grove 5:3f93dd1d4cb3 81 * Send a pbuf doing the necessary SLIP encapsulation
sam_grove 5:3f93dd1d4cb3 82 *
sam_grove 5:3f93dd1d4cb3 83 * Uses the serial layer's sio_send()
sam_grove 5:3f93dd1d4cb3 84 *
sam_grove 5:3f93dd1d4cb3 85 * @param netif the lwip network interface structure for this slipif
sam_grove 5:3f93dd1d4cb3 86 * @param p the pbuf chaing packet to send
sam_grove 5:3f93dd1d4cb3 87 * @param ipaddr the ip address to send the packet to (not used for slipif)
sam_grove 5:3f93dd1d4cb3 88 * @return always returns ERR_OK since the serial layer does not provide return values
sam_grove 5:3f93dd1d4cb3 89 */
sam_grove 5:3f93dd1d4cb3 90 err_t
sam_grove 5:3f93dd1d4cb3 91 slipif_output(struct netif *netif, struct pbuf *p, ip_addr_t *ipaddr)
sam_grove 5:3f93dd1d4cb3 92 {
sam_grove 5:3f93dd1d4cb3 93 struct slipif_priv *priv;
sam_grove 5:3f93dd1d4cb3 94 struct pbuf *q;
sam_grove 5:3f93dd1d4cb3 95 u16_t i;
sam_grove 5:3f93dd1d4cb3 96 u8_t c;
sam_grove 5:3f93dd1d4cb3 97
sam_grove 5:3f93dd1d4cb3 98 LWIP_ASSERT("netif != NULL", (netif != NULL));
sam_grove 5:3f93dd1d4cb3 99 LWIP_ASSERT("netif->state != NULL", (netif->state != NULL));
sam_grove 5:3f93dd1d4cb3 100 LWIP_ASSERT("p != NULL", (p != NULL));
sam_grove 5:3f93dd1d4cb3 101
sam_grove 5:3f93dd1d4cb3 102 LWIP_UNUSED_ARG(ipaddr);
sam_grove 5:3f93dd1d4cb3 103
sam_grove 5:3f93dd1d4cb3 104 priv = netif->state;
sam_grove 5:3f93dd1d4cb3 105
sam_grove 5:3f93dd1d4cb3 106 /* Send pbuf out on the serial I/O device. */
sam_grove 5:3f93dd1d4cb3 107 sio_send(SLIP_END, priv->sd);
sam_grove 5:3f93dd1d4cb3 108
sam_grove 5:3f93dd1d4cb3 109 for (q = p; q != NULL; q = q->next) {
sam_grove 5:3f93dd1d4cb3 110 for (i = 0; i < q->len; i++) {
sam_grove 5:3f93dd1d4cb3 111 c = ((u8_t *)q->payload)[i];
sam_grove 5:3f93dd1d4cb3 112 switch (c) {
sam_grove 5:3f93dd1d4cb3 113 case SLIP_END:
sam_grove 5:3f93dd1d4cb3 114 sio_send(SLIP_ESC, priv->sd);
sam_grove 5:3f93dd1d4cb3 115 sio_send(SLIP_ESC_END, priv->sd);
sam_grove 5:3f93dd1d4cb3 116 break;
sam_grove 5:3f93dd1d4cb3 117 case SLIP_ESC:
sam_grove 5:3f93dd1d4cb3 118 sio_send(SLIP_ESC, priv->sd);
sam_grove 5:3f93dd1d4cb3 119 sio_send(SLIP_ESC_ESC, priv->sd);
sam_grove 5:3f93dd1d4cb3 120 break;
sam_grove 5:3f93dd1d4cb3 121 default:
sam_grove 5:3f93dd1d4cb3 122 sio_send(c, priv->sd);
sam_grove 5:3f93dd1d4cb3 123 break;
sam_grove 5:3f93dd1d4cb3 124 }
sam_grove 5:3f93dd1d4cb3 125 }
sam_grove 5:3f93dd1d4cb3 126 }
sam_grove 5:3f93dd1d4cb3 127 sio_send(SLIP_END, priv->sd);
sam_grove 5:3f93dd1d4cb3 128 return ERR_OK;
sam_grove 5:3f93dd1d4cb3 129 }
sam_grove 5:3f93dd1d4cb3 130
sam_grove 5:3f93dd1d4cb3 131 /**
sam_grove 5:3f93dd1d4cb3 132 * Static function for easy use of blockig or non-blocking
sam_grove 5:3f93dd1d4cb3 133 * sio_read
sam_grove 5:3f93dd1d4cb3 134 *
sam_grove 5:3f93dd1d4cb3 135 * @param fd serial device handle
sam_grove 5:3f93dd1d4cb3 136 * @param data pointer to data buffer for receiving
sam_grove 5:3f93dd1d4cb3 137 * @param len maximum length (in bytes) of data to receive
sam_grove 5:3f93dd1d4cb3 138 * @param block if 1, call sio_read; if 0, call sio_tryread
sam_grove 5:3f93dd1d4cb3 139 * @return return value of sio_read of sio_tryread
sam_grove 5:3f93dd1d4cb3 140 */
sam_grove 5:3f93dd1d4cb3 141 static u32_t
sam_grove 5:3f93dd1d4cb3 142 slip_sio_read(sio_fd_t fd, u8_t* data, u32_t len, u8_t block)
sam_grove 5:3f93dd1d4cb3 143 {
sam_grove 5:3f93dd1d4cb3 144 if (block) {
sam_grove 5:3f93dd1d4cb3 145 return sio_read(fd, data, len);
sam_grove 5:3f93dd1d4cb3 146 } else {
sam_grove 5:3f93dd1d4cb3 147 return sio_tryread(fd, data, len);
sam_grove 5:3f93dd1d4cb3 148 }
sam_grove 5:3f93dd1d4cb3 149 }
sam_grove 5:3f93dd1d4cb3 150
sam_grove 5:3f93dd1d4cb3 151 /**
sam_grove 5:3f93dd1d4cb3 152 * Handle the incoming SLIP stream character by character
sam_grove 5:3f93dd1d4cb3 153 *
sam_grove 5:3f93dd1d4cb3 154 * Poll the serial layer by calling sio_read() or sio_tryread().
sam_grove 5:3f93dd1d4cb3 155 *
sam_grove 5:3f93dd1d4cb3 156 * @param netif the lwip network interface structure for this slipif
sam_grove 5:3f93dd1d4cb3 157 * @param block if 1, block until data is received; if 0, return when all data
sam_grove 5:3f93dd1d4cb3 158 * from the buffer is received (multiple calls to this function will
sam_grove 5:3f93dd1d4cb3 159 * return a complete packet, NULL is returned before - used for polling)
sam_grove 5:3f93dd1d4cb3 160 * @return The IP packet when SLIP_END is received
sam_grove 5:3f93dd1d4cb3 161 */
sam_grove 5:3f93dd1d4cb3 162 static struct pbuf *
sam_grove 5:3f93dd1d4cb3 163 slipif_input(struct netif *netif, u8_t block)
sam_grove 5:3f93dd1d4cb3 164 {
sam_grove 5:3f93dd1d4cb3 165 struct slipif_priv *priv;
sam_grove 5:3f93dd1d4cb3 166 u8_t c;
sam_grove 5:3f93dd1d4cb3 167 struct pbuf *t;
sam_grove 5:3f93dd1d4cb3 168
sam_grove 5:3f93dd1d4cb3 169 LWIP_ASSERT("netif != NULL", (netif != NULL));
sam_grove 5:3f93dd1d4cb3 170 LWIP_ASSERT("netif->state != NULL", (netif->state != NULL));
sam_grove 5:3f93dd1d4cb3 171
sam_grove 5:3f93dd1d4cb3 172 priv = netif->state;
sam_grove 5:3f93dd1d4cb3 173
sam_grove 5:3f93dd1d4cb3 174 while (slip_sio_read(priv->sd, &c, 1, block) > 0) {
sam_grove 5:3f93dd1d4cb3 175 switch (priv->state) {
sam_grove 5:3f93dd1d4cb3 176 case SLIP_RECV_NORMAL:
sam_grove 5:3f93dd1d4cb3 177 switch (c) {
sam_grove 5:3f93dd1d4cb3 178 case SLIP_END:
sam_grove 5:3f93dd1d4cb3 179 if (priv->recved > 0) {
sam_grove 5:3f93dd1d4cb3 180 /* Received whole packet. */
sam_grove 5:3f93dd1d4cb3 181 /* Trim the pbuf to the size of the received packet. */
sam_grove 5:3f93dd1d4cb3 182 pbuf_realloc(priv->q, priv->recved);
sam_grove 5:3f93dd1d4cb3 183
sam_grove 5:3f93dd1d4cb3 184 LINK_STATS_INC(link.recv);
sam_grove 5:3f93dd1d4cb3 185
sam_grove 5:3f93dd1d4cb3 186 LWIP_DEBUGF(SLIP_DEBUG, ("slipif: Got packet\n"));
sam_grove 5:3f93dd1d4cb3 187 t = priv->q;
sam_grove 5:3f93dd1d4cb3 188 priv->p = priv->q = NULL;
sam_grove 5:3f93dd1d4cb3 189 priv->i = priv->recved = 0;
sam_grove 5:3f93dd1d4cb3 190 return t;
sam_grove 5:3f93dd1d4cb3 191 }
sam_grove 5:3f93dd1d4cb3 192 continue;
sam_grove 5:3f93dd1d4cb3 193 case SLIP_ESC:
sam_grove 5:3f93dd1d4cb3 194 priv->state = SLIP_RECV_ESCAPE;
sam_grove 5:3f93dd1d4cb3 195 continue;
sam_grove 5:3f93dd1d4cb3 196 }
sam_grove 5:3f93dd1d4cb3 197 break;
sam_grove 5:3f93dd1d4cb3 198 case SLIP_RECV_ESCAPE:
sam_grove 5:3f93dd1d4cb3 199 switch (c) {
sam_grove 5:3f93dd1d4cb3 200 case SLIP_ESC_END:
sam_grove 5:3f93dd1d4cb3 201 c = SLIP_END;
sam_grove 5:3f93dd1d4cb3 202 break;
sam_grove 5:3f93dd1d4cb3 203 case SLIP_ESC_ESC:
sam_grove 5:3f93dd1d4cb3 204 c = SLIP_ESC;
sam_grove 5:3f93dd1d4cb3 205 break;
sam_grove 5:3f93dd1d4cb3 206 }
sam_grove 5:3f93dd1d4cb3 207 priv->state = SLIP_RECV_NORMAL;
sam_grove 5:3f93dd1d4cb3 208 /* FALLTHROUGH */
sam_grove 5:3f93dd1d4cb3 209 }
sam_grove 5:3f93dd1d4cb3 210
sam_grove 5:3f93dd1d4cb3 211 /* byte received, packet not yet completely received */
sam_grove 5:3f93dd1d4cb3 212 if (priv->p == NULL) {
sam_grove 5:3f93dd1d4cb3 213 /* allocate a new pbuf */
sam_grove 5:3f93dd1d4cb3 214 LWIP_DEBUGF(SLIP_DEBUG, ("slipif_input: alloc\n"));
sam_grove 5:3f93dd1d4cb3 215 priv->p = pbuf_alloc(PBUF_LINK, (PBUF_POOL_BUFSIZE - PBUF_LINK_HLEN), PBUF_POOL);
sam_grove 5:3f93dd1d4cb3 216
sam_grove 5:3f93dd1d4cb3 217 if (priv->p == NULL) {
sam_grove 5:3f93dd1d4cb3 218 LINK_STATS_INC(link.drop);
sam_grove 5:3f93dd1d4cb3 219 LWIP_DEBUGF(SLIP_DEBUG, ("slipif_input: no new pbuf! (DROP)\n"));
sam_grove 5:3f93dd1d4cb3 220 /* don't process any further since we got no pbuf to receive to */
sam_grove 5:3f93dd1d4cb3 221 break;
sam_grove 5:3f93dd1d4cb3 222 }
sam_grove 5:3f93dd1d4cb3 223
sam_grove 5:3f93dd1d4cb3 224 if (priv->q != NULL) {
sam_grove 5:3f93dd1d4cb3 225 /* 'chain' the pbuf to the existing chain */
sam_grove 5:3f93dd1d4cb3 226 pbuf_cat(priv->q, priv->p);
sam_grove 5:3f93dd1d4cb3 227 } else {
sam_grove 5:3f93dd1d4cb3 228 /* p is the first pbuf in the chain */
sam_grove 5:3f93dd1d4cb3 229 priv->q = priv->p;
sam_grove 5:3f93dd1d4cb3 230 }
sam_grove 5:3f93dd1d4cb3 231 }
sam_grove 5:3f93dd1d4cb3 232
sam_grove 5:3f93dd1d4cb3 233 /* this automatically drops bytes if > SLIP_MAX_SIZE */
sam_grove 5:3f93dd1d4cb3 234 if ((priv->p != NULL) && (priv->recved <= SLIP_MAX_SIZE)) {
sam_grove 5:3f93dd1d4cb3 235 ((u8_t *)priv->p->payload)[priv->i] = c;
sam_grove 5:3f93dd1d4cb3 236 priv->recved++;
sam_grove 5:3f93dd1d4cb3 237 priv->i++;
sam_grove 5:3f93dd1d4cb3 238 if (priv->i >= priv->p->len) {
sam_grove 5:3f93dd1d4cb3 239 /* on to the next pbuf */
sam_grove 5:3f93dd1d4cb3 240 priv->i = 0;
sam_grove 5:3f93dd1d4cb3 241 if (priv->p->next != NULL && priv->p->next->len > 0) {
sam_grove 5:3f93dd1d4cb3 242 /* p is a chain, on to the next in the chain */
sam_grove 5:3f93dd1d4cb3 243 priv->p = priv->p->next;
sam_grove 5:3f93dd1d4cb3 244 } else {
sam_grove 5:3f93dd1d4cb3 245 /* p is a single pbuf, set it to NULL so next time a new
sam_grove 5:3f93dd1d4cb3 246 * pbuf is allocated */
sam_grove 5:3f93dd1d4cb3 247 priv->p = NULL;
sam_grove 5:3f93dd1d4cb3 248 }
sam_grove 5:3f93dd1d4cb3 249 }
sam_grove 5:3f93dd1d4cb3 250 }
sam_grove 5:3f93dd1d4cb3 251 }
sam_grove 5:3f93dd1d4cb3 252
sam_grove 5:3f93dd1d4cb3 253 return NULL;
sam_grove 5:3f93dd1d4cb3 254 }
sam_grove 5:3f93dd1d4cb3 255
sam_grove 5:3f93dd1d4cb3 256 #if !NO_SYS
sam_grove 5:3f93dd1d4cb3 257 /**
sam_grove 5:3f93dd1d4cb3 258 * The SLIP input thread.
sam_grove 5:3f93dd1d4cb3 259 *
sam_grove 5:3f93dd1d4cb3 260 * Feed the IP layer with incoming packets
sam_grove 5:3f93dd1d4cb3 261 *
sam_grove 5:3f93dd1d4cb3 262 * @param nf the lwip network interface structure for this slipif
sam_grove 5:3f93dd1d4cb3 263 */
sam_grove 5:3f93dd1d4cb3 264 static void
sam_grove 5:3f93dd1d4cb3 265 slipif_loop_thread(void *nf)
sam_grove 5:3f93dd1d4cb3 266 {
sam_grove 5:3f93dd1d4cb3 267 struct pbuf *p;
sam_grove 5:3f93dd1d4cb3 268 struct netif *netif = (struct netif *)nf;
sam_grove 5:3f93dd1d4cb3 269
sam_grove 5:3f93dd1d4cb3 270 while (1) {
sam_grove 5:3f93dd1d4cb3 271 p = slipif_input(netif, SLIP_BLOCK);
sam_grove 5:3f93dd1d4cb3 272 if (p != NULL) {
sam_grove 5:3f93dd1d4cb3 273 if (netif->input(p, netif) != ERR_OK) {
sam_grove 5:3f93dd1d4cb3 274 pbuf_free(p);
sam_grove 5:3f93dd1d4cb3 275 p = NULL;
sam_grove 5:3f93dd1d4cb3 276 }
sam_grove 5:3f93dd1d4cb3 277 }
sam_grove 5:3f93dd1d4cb3 278 }
sam_grove 5:3f93dd1d4cb3 279 }
sam_grove 5:3f93dd1d4cb3 280 #endif /* !NO_SYS */
sam_grove 5:3f93dd1d4cb3 281
sam_grove 5:3f93dd1d4cb3 282 /**
sam_grove 5:3f93dd1d4cb3 283 * SLIP netif initialization
sam_grove 5:3f93dd1d4cb3 284 *
sam_grove 5:3f93dd1d4cb3 285 * Call the arch specific sio_open and remember
sam_grove 5:3f93dd1d4cb3 286 * the opened device in the state field of the netif.
sam_grove 5:3f93dd1d4cb3 287 *
sam_grove 5:3f93dd1d4cb3 288 * @param netif the lwip network interface structure for this slipif
sam_grove 5:3f93dd1d4cb3 289 * @return ERR_OK if serial line could be opened,
sam_grove 5:3f93dd1d4cb3 290 * ERR_MEM if no memory could be allocated,
sam_grove 5:3f93dd1d4cb3 291 * ERR_IF is serial line couldn't be opened
sam_grove 5:3f93dd1d4cb3 292 *
sam_grove 5:3f93dd1d4cb3 293 * @note netif->num must contain the number of the serial port to open
sam_grove 5:3f93dd1d4cb3 294 * (0 by default)
sam_grove 5:3f93dd1d4cb3 295 */
sam_grove 5:3f93dd1d4cb3 296 err_t
sam_grove 5:3f93dd1d4cb3 297 slipif_init(struct netif *netif)
sam_grove 5:3f93dd1d4cb3 298 {
sam_grove 5:3f93dd1d4cb3 299 struct slipif_priv *priv;
sam_grove 5:3f93dd1d4cb3 300
sam_grove 5:3f93dd1d4cb3 301 LWIP_DEBUGF(SLIP_DEBUG, ("slipif_init: netif->num=%"U16_F"\n", (u16_t)netif->num));
sam_grove 5:3f93dd1d4cb3 302
sam_grove 5:3f93dd1d4cb3 303 /* Allocate private data */
sam_grove 5:3f93dd1d4cb3 304 priv = mem_malloc(sizeof(struct slipif_priv));
sam_grove 5:3f93dd1d4cb3 305 if (!priv) {
sam_grove 5:3f93dd1d4cb3 306 return ERR_MEM;
sam_grove 5:3f93dd1d4cb3 307 }
sam_grove 5:3f93dd1d4cb3 308
sam_grove 5:3f93dd1d4cb3 309 netif->name[0] = 's';
sam_grove 5:3f93dd1d4cb3 310 netif->name[1] = 'l';
sam_grove 5:3f93dd1d4cb3 311 netif->output = slipif_output;
sam_grove 5:3f93dd1d4cb3 312 netif->mtu = SLIP_MAX_SIZE;
sam_grove 5:3f93dd1d4cb3 313 netif->flags |= NETIF_FLAG_POINTTOPOINT;
sam_grove 5:3f93dd1d4cb3 314
sam_grove 5:3f93dd1d4cb3 315 /* Try to open the serial port (netif->num contains the port number). */
sam_grove 5:3f93dd1d4cb3 316 priv->sd = sio_open(netif->num);
sam_grove 5:3f93dd1d4cb3 317 if (!priv->sd) {
sam_grove 5:3f93dd1d4cb3 318 /* Opening the serial port failed. */
sam_grove 5:3f93dd1d4cb3 319 mem_free(priv);
sam_grove 5:3f93dd1d4cb3 320 return ERR_IF;
sam_grove 5:3f93dd1d4cb3 321 }
sam_grove 5:3f93dd1d4cb3 322
sam_grove 5:3f93dd1d4cb3 323 /* Initialize private data */
sam_grove 5:3f93dd1d4cb3 324 priv->p = NULL;
sam_grove 5:3f93dd1d4cb3 325 priv->q = NULL;
sam_grove 5:3f93dd1d4cb3 326 priv->state = SLIP_RECV_NORMAL;
sam_grove 5:3f93dd1d4cb3 327 priv->i = 0;
sam_grove 5:3f93dd1d4cb3 328 priv->recved = 0;
sam_grove 5:3f93dd1d4cb3 329
sam_grove 5:3f93dd1d4cb3 330 netif->state = priv;
sam_grove 5:3f93dd1d4cb3 331
sam_grove 5:3f93dd1d4cb3 332 /* initialize the snmp variables and counters inside the struct netif
sam_grove 5:3f93dd1d4cb3 333 * ifSpeed: no assumption can be made without knowing more about the
sam_grove 5:3f93dd1d4cb3 334 * serial line!
sam_grove 5:3f93dd1d4cb3 335 */
sam_grove 5:3f93dd1d4cb3 336 NETIF_INIT_SNMP(netif, snmp_ifType_slip, 0);
sam_grove 5:3f93dd1d4cb3 337
sam_grove 5:3f93dd1d4cb3 338 /* Create a thread to poll the serial line. */
sam_grove 5:3f93dd1d4cb3 339 sys_thread_new(SLIPIF_THREAD_NAME, slipif_loop_thread, netif,
sam_grove 5:3f93dd1d4cb3 340 SLIPIF_THREAD_STACKSIZE, SLIPIF_THREAD_PRIO);
sam_grove 5:3f93dd1d4cb3 341 return ERR_OK;
sam_grove 5:3f93dd1d4cb3 342 }
sam_grove 5:3f93dd1d4cb3 343
sam_grove 5:3f93dd1d4cb3 344 /**
sam_grove 5:3f93dd1d4cb3 345 * Polls the serial device and feeds the IP layer with incoming packets.
sam_grove 5:3f93dd1d4cb3 346 *
sam_grove 5:3f93dd1d4cb3 347 * @param netif The lwip network interface structure for this slipif
sam_grove 5:3f93dd1d4cb3 348 */
sam_grove 5:3f93dd1d4cb3 349 void
sam_grove 5:3f93dd1d4cb3 350 slipif_poll(struct netif *netif)
sam_grove 5:3f93dd1d4cb3 351 {
sam_grove 5:3f93dd1d4cb3 352 struct pbuf *p;
sam_grove 5:3f93dd1d4cb3 353 struct slipif_priv *priv;
sam_grove 5:3f93dd1d4cb3 354
sam_grove 5:3f93dd1d4cb3 355 LWIP_ASSERT("netif != NULL", (netif != NULL));
sam_grove 5:3f93dd1d4cb3 356 LWIP_ASSERT("netif->state != NULL", (netif->state != NULL));
sam_grove 5:3f93dd1d4cb3 357
sam_grove 5:3f93dd1d4cb3 358 priv = netif->state;
sam_grove 5:3f93dd1d4cb3 359
sam_grove 5:3f93dd1d4cb3 360 while ((p = slipif_input(netif, SLIP_DONTBLOCK)) != NULL) {
sam_grove 5:3f93dd1d4cb3 361 if (netif->input(p, netif) != ERR_OK) {
sam_grove 5:3f93dd1d4cb3 362 pbuf_free(p);
sam_grove 5:3f93dd1d4cb3 363 }
sam_grove 5:3f93dd1d4cb3 364 }
sam_grove 5:3f93dd1d4cb3 365 }
sam_grove 5:3f93dd1d4cb3 366
sam_grove 5:3f93dd1d4cb3 367 #endif /* LWIP_HAVE_SLIPIF */