1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-12-25 14:33:52 +01:00

[sys/net/sixlowpan/sixlowborder.*]

* outsourced multiplexing to bordermultiplex.*
This commit is contained in:
mlenders 2011-07-25 17:00:18 +02:00
parent b918924fc1
commit bdd9d81930
7 changed files with 289 additions and 266 deletions

View File

@ -2,7 +2,7 @@ SubDir TOP sys net sixlowpan ;
# HDRS += $(TOP)/sys/net/sixlowpan/ ;
Module 6lowpan : sixlowpan.c sixlowip.c sixlowmac.c sixlownd.c sixlowborder.c ieee802154_frame.c serialnumber.c semaphore.c flowcontrol.c ;
Module 6lowpan : sixlowpan.c sixlowip.c sixlowmac.c sixlownd.c sixlowborder.c ieee802154_frame.c serialnumber.c semaphore.c bordermultiplex.c flowcontrol.c ;
UseModule vtimer ;
UseModule transceiver ;

View File

@ -0,0 +1,209 @@
#include <stdio.h>
#include <string.h>
#include <board_uart0.h>
#include "flowcontrol.h"
#include "sixlowborder.h"
#include "sixlowerror.h"
#define DC3 0x0D
#define END 0xC0
#define ESC 0xDB
#define END_ESC 0xDC
#define ESC_ESC 0xDD
#define DC3_ESC 0xDE
uint8_t serial_out_buf[BORDER_BUFFER_SIZE];
uint8_t serial_in_buf[BORDER_BUFFER_SIZE];
uint8_t *get_serial_out_buffer(int offset) {
if (offset > BUFFER_SIZE) {
return NULL;
}
return &(serial_out_buf[offset]);
}
uint8_t *get_serial_in_buffer(int offset) {
if (offset > BUFFER_SIZE) {
return NULL;
}
return &(serial_in_buf[offset]);
}
void demultiplex(border_packet_t *packet, int len) {
switch (packet->type) {
case (BORDER_PACKET_RAW_TYPE):{
fputs(((char *)packet) + sizeof (border_packet_t), stdin);
break;
}
case (BORDER_PACKET_L3_TYPE):{
border_l3_header_t *l3_header_buf = (border_l3_header_t *)packet;
switch (l3_header_buf->ethertype) {
case (BORDER_ETHERTYPE_IPV6):{
struct ipv6_hdr_t *ipv6_buf = (struct ipv6_hdr_t *)(((unsigned char *)packet) + sizeof (border_l3_header_t));
border_send_ipv6_over_lowpan(ipv6_buf, 1, 1);
break;
}
default:
printf("ERROR: Unknown ethertype %02x\n", l3_header_buf->ethertype);
break;
}
break;
}
case (BORDER_PACKET_CONF_TYPE):{
border_conf_header_t *conf_header_buf = (border_conf_header_t *)packet;
switch (conf_header_buf->conftype) {
case (BORDER_CONF_CONTEXT):{
border_context_packet_t *context = (border_context_packet_t *)packet;
ipv6_addr_t target_addr;
ipv6_set_all_nds_mcast_addr(&target_addr);
mutex_lock(&lowpan_context_mutex);
lowpan_context_update(
context->context.cid,
&context->context.prefix,
context->context.length,
context->context.comp,
context->context.lifetime
);
// abr stuff
mutex_unlock(&lowpan_context_mutex,0);
// Send router advertisement
break;
}
case (BORDER_CONF_IPADDR):{
//border_addr_packet_t *addr_packet = (border_addr_packet_t *)packet;
// add address
break;
}
default:
printf("ERROR: Unknown conftype %02x\n", conf_header_buf->conftype);
break;
}
break;
}
default:
printf("ERROR: Unknown border packet type %02x\n", packet->type);
break;
}
}
void multiplex_send_ipv6_over_uart(struct ipv6_hdr_t *packet) {
border_l3_header_t *serial_buf;
serial_buf = (border_l3_header_t *)get_serial_out_buffer(0);
serial_buf->reserved = 0;
serial_buf->type = BORDER_PACKET_L3_TYPE;
serial_buf->ethertype = BORDER_ETHERTYPE_IPV6;
memcpy(get_serial_in_buffer(0)+sizeof (border_l3_header_t), packet, IPV6_HDR_LEN + packet->length);
flowcontrol_send_over_uart(
(border_packet_t *) serial_buf,
sizeof (border_l3_header_t)
);
}
void multiplex_send_addr_over_uart(ipv6_addr_t *addr) {
border_addr_packet_t *serial_buf;
serial_buf = (border_addr_packet_t *)get_serial_in_buffer(0);
serial_buf->reserved = 0;
serial_buf->type = BORDER_PACKET_CONF_TYPE;
serial_buf->conftype = BORDER_CONF_IPADDR;
memcpy(
&serial_buf->addr,
addr,
sizeof (ipv6_addr_t)
);
flowcontrol_send_over_uart(
(border_packet_t *) serial_buf,
sizeof (border_addr_packet_t)
);
}
int readpacket(uint8_t *packet_buf, size_t size) {
uint8_t *line_buf_ptr = packet_buf;
uint8_t byte = END+1;
uint8_t esc = 0;
while (byte != END) {
byte = uart0_readc();
if ( (line_buf_ptr - packet_buf) >= size-1) {
return -SIXLOWERROR_ARRAYFULL;
}
if (byte == DC3) continue;
if (esc) {
switch (byte) {
case(END_ESC):{
byte = END;
break;
}
case(ESC_ESC):{
byte = ESC;
break;
}
case(DC3_ESC):{
byte = DC3;
break;
}
default:
break;
}
esc = 0;
}
if (byte == ESC) {
esc = 1;
continue;
}
*line_buf_ptr++ = byte;
}
return (line_buf_ptr - packet_buf - 1);
}
int writepacket(uint8_t *packet_buf, size_t size) {
uint8_t *byte_ptr = packet_buf;
while ((byte_ptr - packet_buf) < size) {
if ((byte_ptr - packet_buf) > BORDER_BUFFER_SIZE) {
return -1;
}
printf("%02x ",*byte_ptr);
switch (*byte_ptr) {
case(DC3):{
*byte_ptr = DC3_ESC;
uart0_putc(ESC);
break;
}
case(END):{
*byte_ptr = END_ESC;
uart0_putc(ESC);
break;
}
case(ESC):{
*byte_ptr = ESC_ESC;
uart0_putc(ESC);
break;
}
default:{
break;
}
}
uart0_putc(*byte_ptr);
byte_ptr++;
}
printf("\n");
uart0_putc(END);
return (byte_ptr - packet_buf);
}

View File

@ -0,0 +1,75 @@
#ifndef BORDERMULTIPLEX_H
#define BORDERMULTIPLEX_H
#include <stdint.h>
#include <stdlib.h>
#include "sixlowip.h"
/* packet types of uart-packets */
#define BORDER_PACKET_RAW_TYPE 0
#define BORDER_PACKET_CONF_TYPE 2
#define BORDER_PACKET_L3_TYPE 3
/* configuration types */
#define BORDER_CONF_CONTEXT 2
#define BORDER_CONF_IPADDR 3
/* ethertypes for L3 packets */
#define BORDER_ETHERTYPE_IPV6 0x86DD
typedef struct __attribute__ ((packed)) border_packet_t {
uint8_t reserved;
uint8_t type;
uint8_t seq_num;
} border_packet_t;
typedef struct __attribute__ ((packed)) border_l3_header_t {
uint8_t reserved;
uint8_t type;
uint8_t seq_num;
uint16_t ethertype;
} border_l3_header_t;
typedef struct __attribute__ ((packed)) border_conf_header_t {
uint8_t reserved;
uint8_t type;
uint8_t seq_num;
uint8_t conftype;
} border_conf_header_t;
typedef struct __attribute__ ((packed)) border_addr_packet_t {
uint8_t reserved;
uint8_t type;
uint8_t seq_num;
uint8_t conftype;
ipv6_addr_t addr;
} border_addr_packet_t;
typedef struct __attribute__ ((packed)) border_context_packet_t {
uint8_t reserved;
uint8_t type;
uint8_t seq_num;
uint8_t conftype;
struct border_context_t {
uint8_t cid;
ipv6_addr_t prefix;
uint8_t length;
uint8_t comp;
uint16_t lifetime;
} context;
} border_context_packet_t;
#define BORDER_BUFFER_SIZE (sizeof (border_l3_header_t) + MTU)
uint8_t *get_serial_out_buffer(int offset);
uint8_t *get_serial_in_buffer(int offset);
void demultiplex(border_packet_t *packet, int len);
void multiplex_send_ipv6_over_uart(struct ipv6_hdr_t *packet);
void multiplex_send_addr_over_uart(ipv6_addr_t *addr);
int readpacket(uint8_t *packet_buf, size_t size);
int writepacket(uint8_t *packet_buf, size_t size);
#endif /* BORDERMULTIPLEX_H*/

View File

@ -6,6 +6,7 @@
#include "semaphore.h"
#include "bordermultiplex.h"
#include "flowcontrol.h"
flowcontrol_stat_t slwin_stat;
sem_t connection_established;

View File

@ -6,6 +6,7 @@
#include "semaphore.h"
#include "sixlowip.h"
#include "sixlowborder.h"
#include "bordermultiplex.h"
/* packet types for flowcontrol */

View File

@ -7,6 +7,8 @@
#include <posix_io.h>
#include <board_uart0.h>
#include "bordermultiplex.h"
#include "ieee802154_frame.h"
#include "flowcontrol.h"
#include "sixlowborder.h"
@ -15,21 +17,11 @@
#include "serialnumber.h"
#include "sixlowerror.h"
#define DC3 0x0D
#define END 0xC0
#define ESC 0xDB
#define END_ESC 0xDC
#define ESC_ESC 0xDD
#define DC3_ESC 0xDE
#define READER_STACK_SIZE 512
char serial_reader_stack[READER_STACK_SIZE];
uint16_t serial_reader_pid;
uint8_t serial_out_buf[BORDER_BUFFER_SIZE];
uint8_t serial_in_buf[BORDER_BUFFER_SIZE];
void serial_reader_f(void);
uint8_t border_initialize(transceiver_type_t trans,ipv6_addr_t *border_router_addr) {
@ -74,106 +66,6 @@ uint16_t border_get_serial_reader() {
return serial_reader_pid;
}
uint8_t *get_serial_out_buffer(int offset) {
if (offset > BUFFER_SIZE) {
return NULL;
}
return &(serial_out_buf[offset]);
}
uint8_t *get_serial_in_buffer(int offset) {
if (offset > BUFFER_SIZE) {
return NULL;
}
return &(serial_in_buf[offset]);
}
int readpacket(uint8_t *packet_buf, int size) {
uint8_t *line_buf_ptr = packet_buf;
uint8_t byte = END+1;
uint8_t esc = 0;
while (byte != END) {
byte = uart0_readc();
if ( (line_buf_ptr - packet_buf) >= size-1) {
return -SIXLOWERROR_ARRAYFULL;
}
if (byte == DC3) continue;
if (esc) {
switch (byte) {
case(END_ESC):{
byte = END;
break;
}
case(ESC_ESC):{
byte = ESC;
break;
}
case(DC3_ESC):{
byte = DC3;
break;
}
default:
break;
}
esc = 0;
}
if (byte == ESC) {
esc = 1;
continue;
}
*line_buf_ptr++ = byte;
}
return (line_buf_ptr - packet_buf - 1);
}
int writepacket(uint8_t *packet_buf, size_t size) {
uint8_t *byte_ptr = packet_buf;
while ((byte_ptr - packet_buf) < size) {
if ((byte_ptr - packet_buf) > BORDER_BUFFER_SIZE) {
return -1;
}
printf("%02x ",*byte_ptr);
switch (*byte_ptr) {
case(DC3):{
*byte_ptr = DC3_ESC;
uart0_putc(ESC);
break;
}
case(END):{
*byte_ptr = END_ESC;
uart0_putc(ESC);
break;
}
case(ESC):{
*byte_ptr = ESC_ESC;
uart0_putc(ESC);
break;
}
default:{
break;
}
}
uart0_putc(*byte_ptr);
byte_ptr++;
}
printf("\n");
uart0_putc(END);
return (byte_ptr - packet_buf);
}
void serial_reader_f(void) {
int main_pid = 0;
int bytes;
@ -217,97 +109,6 @@ void serial_reader_f(void) {
}
}
void demultiplex(border_packet_t *packet, int len) {
switch (packet->type) {
case (BORDER_PACKET_RAW_TYPE):{
fputs(((char *)packet) + sizeof (border_packet_t), stdin);
break;
}
case (BORDER_PACKET_L3_TYPE):{
border_l3_header_t *l3_header_buf = (border_l3_header_t *)packet;
switch (l3_header_buf->ethertype) {
case (BORDER_ETHERTYPE_IPV6):{
struct ipv6_hdr_t *ipv6_buf = (struct ipv6_hdr_t *)(((unsigned char *)packet) + sizeof (border_l3_header_t));
border_send_ipv6_over_lowpan(ipv6_buf, 1, 1);
break;
}
default:
printf("ERROR: Unknown ethertype %02x\n", l3_header_buf->ethertype);
break;
}
break;
}
case (BORDER_PACKET_CONF_TYPE):{
border_conf_header_t *conf_header_buf = (border_conf_header_t *)packet;
switch (conf_header_buf->conftype) {
case (BORDER_CONF_CONTEXT):{
border_context_packet_t *context = (border_context_packet_t *)packet;
ipv6_addr_t target_addr;
ipv6_set_all_nds_mcast_addr(&target_addr);
mutex_lock(&lowpan_context_mutex);
lowpan_context_update(
context->context.cid,
&context->context.prefix,
context->context.length,
context->context.comp,
context->context.lifetime
);
// abr stuff
mutex_unlock(&lowpan_context_mutex,0);
// Send router advertisement
break;
}
case (BORDER_CONF_IPADDR):{
//border_addr_packet_t *addr_packet = (border_addr_packet_t *)packet;
// add address
break;
}
default:
printf("ERROR: Unknown conftype %02x\n", conf_header_buf->conftype);
break;
}
break;
}
default:
printf("ERROR: Unknown border packet type %02x\n", packet->type);
break;
}
}
void multiplex_send_ipv6_over_uart(struct ipv6_hdr_t *packet) {
border_l3_header_t *serial_buf;
serial_buf = (border_l3_header_t *)get_serial_out_buffer(0);
serial_buf->reserved = 0;
serial_buf->type = BORDER_PACKET_L3_TYPE;
serial_buf->ethertype = BORDER_ETHERTYPE_IPV6;
memcpy(get_serial_in_buffer(0)+sizeof (border_l3_header_t), packet, IPV6_HDR_LEN + packet->length);
flowcontrol_send_over_uart(
(border_packet_t *) serial_buf,
sizeof (border_l3_header_t)
);
}
void multiplex_send_addr_over_uart(ipv6_addr_t *addr) {
border_addr_packet_t *serial_buf;
serial_buf = (border_addr_packet_t *)get_serial_in_buffer(0);
serial_buf->reserved = 0;
serial_buf->type = BORDER_PACKET_CONF_TYPE;
serial_buf->conftype = BORDER_CONF_IPADDR;
memcpy(
&serial_buf->addr,
addr,
sizeof (ipv6_addr_t)
);
flowcontrol_send_over_uart(
(border_packet_t *) serial_buf,
sizeof (border_addr_packet_t)
);
}
void border_send_ipv6_over_lowpan(struct ipv6_hdr_t *packet, uint8_t aro_flag, uint8_t sixco_flag) {
uint16_t offset = IPV6_HDR_LEN+HTONS(packet->length);

View File

@ -10,74 +10,10 @@
#include "sixlownd.h"
#include "semaphore.h"
/* packet types of uart-packets */
#define BORDER_PACKET_RAW_TYPE 0
#define BORDER_PACKET_CONF_TYPE 2
#define BORDER_PACKET_L3_TYPE 3
/* configuration types */
#define BORDER_CONF_CONTEXT 2
#define BORDER_CONF_IPADDR 3
/* ethertypes for L3 packets */
#define BORDER_ETHERTYPE_IPV6 0x86DD
typedef struct __attribute__ ((packed)) border_packet_t {
uint8_t reserved;
uint8_t type;
uint8_t seq_num;
} border_packet_t;
typedef struct __attribute__ ((packed)) border_l3_header_t {
uint8_t reserved;
uint8_t type;
uint8_t seq_num;
uint16_t ethertype;
} border_l3_header_t;
typedef struct __attribute__ ((packed)) border_conf_header_t {
uint8_t reserved;
uint8_t type;
uint8_t seq_num;
uint8_t conftype;
} border_conf_header_t;
typedef struct __attribute__ ((packed)) border_addr_packet_t {
uint8_t reserved;
uint8_t type;
uint8_t seq_num;
uint8_t conftype;
ipv6_addr_t addr;
} border_addr_packet_t;
typedef struct __attribute__ ((packed)) border_context_packet_t {
uint8_t reserved;
uint8_t type;
uint8_t seq_num;
uint8_t conftype;
struct border_context_t {
uint8_t cid;
ipv6_addr_t prefix;
uint8_t length;
uint8_t comp;
uint16_t lifetime;
} context;
} border_context_packet_t;
#define BORDER_BUFFER_SIZE (sizeof (border_l3_header_t) + MTU)
uint16_t border_get_serial_reader();
uint8_t *get_serial_out_buffer(int offset);
uint8_t *get_serial_in_buffer(int offset);
uint8_t border_initialize(transceiver_type_t trans,ipv6_addr_t *border_router_addr);
void demultiplex(border_packet_t *packet, int len);
void multiplex_send_ipv6_over_uart(struct ipv6_hdr_t *packet);
void multiplex_send_addr_over_uart(ipv6_addr_t *addr);
void border_send_ipv6_over_lowpan(struct ipv6_hdr_t *packet, uint8_t aro_flag, uint8_t sixco_flag);
void border_process_lowpan(void);
int readpacket(uint8_t *packet_buf, int size);
int writepacket(uint8_t *packet_buf, size_t size);
#endif /* SIXLOWBORDER_H*/