Merge pull request #190 from g-oikonomou/contrib/there-can-be-only-one-slip

Various SLIP-related improvements
This commit is contained in:
Simon Duquennoy 2017-11-24 14:23:18 +01:00 committed by GitHub
commit 01048c8f66
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 39 additions and 166 deletions

View File

@ -167,7 +167,7 @@ slip_write_char(uint8_t c)
slip_arch_writeb(c); slip_arch_writeb(c);
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
uint8_t void
slip_write(const void *_ptr, int len) slip_write(const void *_ptr, int len)
{ {
const uint8_t *ptr = _ptr; const uint8_t *ptr = _ptr;
@ -181,14 +181,12 @@ slip_write(const void *_ptr, int len)
slip_write_char(c); slip_write_char(c);
} }
slip_arch_writeb(SLIP_END); slip_arch_writeb(SLIP_END);
return len;
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
/* slip_send: forward (IPv4) packets with {UIP_FW_NETIF(..., slip_send)} /* slip_send: forward (IPv4) packets with {UIP_FW_NETIF(..., slip_send)}
* was used in slip-bridge.c * was used in slip-bridge.c
*/ */
uint8_t void
slip_send(void) slip_send(void)
{ {
uint16_t i; uint16_t i;
@ -203,8 +201,6 @@ slip_send(void)
slip_write_char(c); slip_write_char(c);
} }
slip_arch_writeb(SLIP_END); slip_arch_writeb(SLIP_END);
return UIP_FW_OK;
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
static void static void

View File

@ -37,7 +37,7 @@
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
#define UIP_CONF_ROUTER 0 #define UIP_CONF_ROUTER 0
#define CMD_CONF_OUTPUT slip_radio_cmd_output #define CMD_CONF_OUTPUT slip_write
/* Default CMD handlers if the target did not specify them */ /* Default CMD handlers if the target did not specify them */
#ifndef CMD_CONF_HANDLERS #ifndef CMD_CONF_HANDLERS

View File

@ -49,27 +49,6 @@ slipnet_init(void)
{ {
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
void
slip_send_packet(const uint8_t *ptr, int len)
{
uint16_t i;
uint8_t c;
slip_arch_writeb(SLIP_END);
for(i = 0; i < len; ++i) {
c = *ptr++;
if(c == SLIP_END) {
slip_arch_writeb(SLIP_ESC);
c = SLIP_ESC_END;
} else if(c == SLIP_ESC) {
slip_arch_writeb(SLIP_ESC);
c = SLIP_ESC_ESC;
}
slip_arch_writeb(c);
}
slip_arch_writeb(SLIP_END);
}
/*---------------------------------------------------------------------------*/
static void static void
slipnet_input(void) slipnet_input(void)
{ {
@ -94,7 +73,7 @@ slipnet_input(void)
} }
LOG_DBG_("\n"); LOG_DBG_("\n");
slip_send_packet(uip_buf, uip_len); slip_write(uip_buf, uip_len);
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
static uint8_t static uint8_t

View File

@ -56,8 +56,6 @@
extern const struct slip_radio_sensors SLIP_RADIO_CONF_SENSORS; extern const struct slip_radio_sensors SLIP_RADIO_CONF_SENSORS;
#endif #endif
void slip_send_packet(const uint8_t *ptr, int len);
/* max 16 packets at the same time??? */ /* max 16 packets at the same time??? */
uint8_t packet_ids[16]; uint8_t packet_ids[16];
int packet_pos; int packet_pos;
@ -187,12 +185,6 @@ slip_radio_cmd_handler(const uint8_t *data, int len)
return 0; return 0;
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
void
slip_radio_cmd_output(const uint8_t *data, int data_len)
{
slip_send_packet(data, data_len);
}
/*---------------------------------------------------------------------------*/
static void static void
slip_input_callback(void) slip_input_callback(void)
{ {

View File

@ -26,48 +26,41 @@
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
*
* This file is part of the Contiki operating system.
*
*/ */
/*---------------------------------------------------------------------------*/
#include "contiki.h"
#include "net/ipv6/uip.h"
#include "dev/slip.h"
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
/*---------------------------------------------------------------------------*/
#include "contiki.h"
#include "net/ipv6/uip.h"
#define BUF ((struct uip_tcpip_hdr *)&uip_buf[UIP_LLH_LEN])
#include "dev/slip.h"
#define SLIP_END 0300 #define SLIP_END 0300
#define SLIP_ESC 0333 #define SLIP_ESC 0333
#define SLIP_ESC_END 0334 #define SLIP_ESC_END 0334
#define SLIP_ESC_ESC 0335 #define SLIP_ESC_ESC 0335
/*---------------------------------------------------------------------------*/
PROCESS(slip_process, "SLIP driver"); PROCESS(slip_process, "SLIP driver");
/*---------------------------------------------------------------------------*/
uint8_t slip_active; static uint8_t slip_active;
/*---------------------------------------------------------------------------*/
#if 1 #if SLIP_CONF_WITH_STATS
#define SLIP_STATISTICS(statement) static uint16_t slip_rubbish, slip_twopackets, slip_overflow, slip_ip_drop;
#else
uint16_t slip_rubbish, slip_twopackets, slip_overflow, slip_ip_drop;
#define SLIP_STATISTICS(statement) statement #define SLIP_STATISTICS(statement) statement
#else
#define SLIP_STATISTICS(statement)
#endif #endif
/*---------------------------------------------------------------------------*/
/* Must be at least one byte larger than UIP_BUFSIZE! */ /* Must be at least one byte larger than UIP_BUFSIZE! */
#define RX_BUFSIZE (UIP_BUFSIZE - UIP_LLH_LEN + 16) #define RX_BUFSIZE (UIP_BUFSIZE - UIP_LLH_LEN + 16)
/*---------------------------------------------------------------------------*/
enum { enum {
STATE_TWOPACKETS = 0, /* We have 2 packets and drop incoming data. */ STATE_TWOPACKETS = 0, /* We have 2 packets and drop incoming data. */
STATE_OK = 1, STATE_OK = 1,
STATE_ESC = 2, STATE_ESC = 2,
STATE_RUBBISH = 3, STATE_RUBBISH = 3,
}; };
/*---------------------------------------------------------------------------*/
/* /*
* Variables begin and end manage the buffer space in a cyclic * Variables begin and end manage the buffer space in a cyclic
* fashion. The first used byte is at begin and end is one byte past * fashion. The first used byte is at begin and end is one byte past
@ -78,13 +71,12 @@ enum {
* [pkt_end, end). If more bytes arrive in state STATE_TWOPACKETS * [pkt_end, end). If more bytes arrive in state STATE_TWOPACKETS
* they are discarded. * they are discarded.
*/ */
static uint8_t state = STATE_TWOPACKETS; static uint8_t state = STATE_TWOPACKETS;
static uint16_t begin, next_free; static uint16_t begin, next_free;
static uint8_t rxbuf[RX_BUFSIZE]; static uint8_t rxbuf[RX_BUFSIZE];
static uint16_t pkt_end; /* SLIP_END tracker. */ static uint16_t pkt_end; /* SLIP_END tracker. */
static void (* input_callback)(void) = NULL; static void (*input_callback)(void) = NULL;
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
void void
slip_set_input_callback(void (*c)(void)) slip_set_input_callback(void (*c)(void))
@ -92,36 +84,13 @@ slip_set_input_callback(void (*c)(void))
input_callback = c; input_callback = c;
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
/* slip_send: forward (IPv4) packets with {UIP_FW_NETIF(..., slip_send)} void
* was used in slip-bridge.c
*/
uint8_t
slip_send(void) slip_send(void)
{ {
uint16_t i; slip_write(&uip_buf[UIP_LLH_LEN], uip_len);
uint8_t *ptr;
uint8_t c;
slip_arch_writeb(SLIP_END);
ptr = &uip_buf[UIP_LLH_LEN];
for(i = 0; i < uip_len; ++i) {
c = *ptr++;
if(c == SLIP_END) {
slip_arch_writeb(SLIP_ESC);
c = SLIP_ESC_END;
} else if(c == SLIP_ESC) {
slip_arch_writeb(SLIP_ESC);
c = SLIP_ESC_ESC;
}
slip_arch_writeb(c);
}
slip_arch_writeb(SLIP_END);
return UIP_FW_OK;
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
uint8_t void
slip_write(const void *_ptr, int len) slip_write(const void *_ptr, int len)
{ {
const uint8_t *ptr = _ptr; const uint8_t *ptr = _ptr;
@ -141,9 +110,8 @@ slip_write(const void *_ptr, int len)
} }
slip_arch_writeb(c); slip_arch_writeb(c);
} }
slip_arch_writeb(SLIP_END);
return len; slip_arch_writeb(SLIP_END);
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
static void static void
@ -153,56 +121,9 @@ rxbuf_init(void)
state = STATE_OK; state = STATE_OK;
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
/* Upper half does the polling. */
static uint16_t static uint16_t
slip_poll_handler(uint8_t *outbuf, uint16_t blen) slip_poll_handler(uint8_t *outbuf, uint16_t blen)
{ {
#ifdef SLIP_CONF_MICROSOFT_CHAT
/* This is a hack and won't work across buffer edge! */
if(rxbuf[begin] == 'C') {
int i;
if(begin < next_free && (next_free - begin) >= 6
&& memcmp(&rxbuf[begin], "CLIENT", 6) == 0) {
state = STATE_TWOPACKETS; /* Interrupts do nothing. */
memset(&rxbuf[begin], 0x0, 6);
rxbuf_init();
for(i = 0; i < 13; i++) {
slip_arch_writeb("CLIENTSERVER\300"[i]);
}
return 0;
}
}
#endif /* SLIP_CONF_MICROSOFT_CHAT */
#ifdef SLIP_CONF_ANSWER_MAC_REQUEST
else if(rxbuf[begin] == '?') {
/* Used by tapslip6 to request mac for auto configure */
int i, j;
char* hexchar = "0123456789abcdef";
if(begin < next_free && (next_free - begin) >= 2
&& rxbuf[begin + 1] == 'M') {
state = STATE_TWOPACKETS; /* Interrupts do nothing. */
rxbuf[begin] = 0;
rxbuf[begin + 1] = 0;
rxbuf_init();
linkaddr_t addr = get_mac_addr();
/* this is just a test so far... just to see if it works */
slip_arch_writeb('!');
slip_arch_writeb('M');
for(j = 0; j < 8; j++) {
slip_arch_writeb(hexchar[addr.u8[j] >> 4]);
slip_arch_writeb(hexchar[addr.u8[j] & 15]);
}
slip_arch_writeb(SLIP_END);
return 0;
}
}
#endif /* SLIP_CONF_ANSWER_MAC_REQUEST */
/* /*
* Interrupt can not change begin but may change pkt_end. * Interrupt can not change begin but may change pkt_end.
* If pkt_end != begin it will not change again. * If pkt_end != begin it will not change again.
@ -221,7 +142,7 @@ slip_poll_handler(uint8_t *outbuf, uint16_t blen)
len = 0; len = 0;
break; break;
} }
if (esc) { if(esc) {
if(rxbuf[i] == SLIP_ESC_ESC) { if(rxbuf[i] == SLIP_ESC_ESC) {
outbuf[len] = SLIP_ESC; outbuf[len] = SLIP_ESC;
len++; len++;
@ -245,7 +166,7 @@ slip_poll_handler(uint8_t *outbuf, uint16_t blen)
len = 0; len = 0;
break; break;
} }
if (esc) { if(esc) {
if(rxbuf[i] == SLIP_ESC_ESC) { if(rxbuf[i] == SLIP_ESC_ESC) {
outbuf[len] = SLIP_ESC; outbuf[len] = SLIP_ESC;
len++; len++;
@ -266,7 +187,7 @@ slip_poll_handler(uint8_t *outbuf, uint16_t blen)
len = 0; len = 0;
break; break;
} }
if (esc) { if(esc) {
if(rxbuf[i] == SLIP_ESC_ESC) { if(rxbuf[i] == SLIP_ESC_ESC) {
outbuf[len] = SLIP_ESC; outbuf[len] = SLIP_ESC;
len++; len++;
@ -332,17 +253,13 @@ PROCESS_THREAD(slip_process, ev, data)
/* Move packet from rxbuf to buffer provided by uIP. */ /* Move packet from rxbuf to buffer provided by uIP. */
uip_len = slip_poll_handler(&uip_buf[UIP_LLH_LEN], uip_len = slip_poll_handler(&uip_buf[UIP_LLH_LEN],
UIP_BUFSIZE - UIP_LLH_LEN); UIP_BUFSIZE - UIP_LLH_LEN);
if(uip_len > 0) { if(uip_len > 0) {
if(input_callback) { if(input_callback) {
input_callback(); input_callback();
} }
#ifdef SLIP_CONF_TCPIP_INPUT
SLIP_CONF_TCPIP_INPUT();
#else
tcpip_input(); tcpip_input();
#endif
} }
} }
@ -364,7 +281,7 @@ slip_input_byte(unsigned char c)
if(c != SLIP_ESC_END && c != SLIP_ESC_ESC) { if(c != SLIP_ESC_END && c != SLIP_ESC_ESC) {
state = STATE_RUBBISH; state = STATE_RUBBISH;
SLIP_STATISTICS(slip_rubbish++); SLIP_STATISTICS(slip_rubbish++);
next_free = pkt_end; /* remove rubbish */ next_free = pkt_end; /* remove rubbish */
return 0; return 0;
} }
state = STATE_OK; state = STATE_OK;
@ -389,22 +306,14 @@ slip_input_byte(unsigned char c)
} }
rxbuf[cur_end] = c; rxbuf[cur_end] = c;
#ifdef SLIP_CONF_MICROSOFT_CHAT
/* There could be a separate poll routine for this. */
if(c == 'T' && rxbuf[begin] == 'C') {
process_poll(&slip_process);
return 1;
}
#endif /* SLIP_CONF_MICROSOFT_CHAT */
if(c == SLIP_END) { if(c == SLIP_END) {
/* /*
* We have a new packet, possibly of zero length. * We have a new packet, possibly of zero length.
* *
* There may already be one packet buffered. * There may already be one packet buffered.
*/ */
if(cur_end != pkt_end) { /* Non zero length. */ if(cur_end != pkt_end) { /* Non zero length. */
if(begin == pkt_end) { /* None buffered. */ if(begin == pkt_end) { /* None buffered. */
pkt_end = cur_end; pkt_end = cur_end;
} else { } else {
SLIP_STATISTICS(slip_twopackets++); SLIP_STATISTICS(slip_twopackets++);

View File

@ -41,7 +41,7 @@ PROCESS_NAME(slip_process);
/** /**
* Send an IP packet from the uIP buffer with SLIP. * Send an IP packet from the uIP buffer with SLIP.
*/ */
uint8_t slip_send(void); void slip_send(void);
/** /**
* Input a SLIP byte. * Input a SLIP byte.
@ -62,13 +62,10 @@ uint8_t slip_send(void);
*/ */
int slip_input_byte(unsigned char c); int slip_input_byte(unsigned char c);
uint8_t slip_write(const void *ptr, int len); /**
* Send using SLIP len bytes starting from the location pointed to by ptr
/* Did we receive any bytes lately? */ */
extern uint8_t slip_active; void slip_write(const void *ptr, int len);
/* Statistics. */
extern uint16_t slip_rubbish, slip_twopackets, slip_overflow, slip_ip_drop;
/** /**
* Set a function to be called when there is activity on the SLIP * Set a function to be called when there is activity on the SLIP