Skip to content

Commit

Permalink
[WIP] Initial import of uart_net header definition
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Lenders authored and miri64 committed Aug 26, 2014
1 parent df2317f commit 77d88c3
Show file tree
Hide file tree
Showing 5 changed files with 313 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Makefile.dep
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ ifneq (,$(filter sixlowpan,$(USEMODULE)))
USEMODULE += vtimer
endif

ifneq (,$(filter uart_net,$(USEMODULE)))
USEMODULE += basic_net
USEMODULE += ringbuffer
ifeq(,$(filter-out $(BOARD),arduino-due iot-lab_M3 pca10000 pca10005 stm32f0discovery stm32f3discovery stm32f4discovery))
USEMODULE += posix
USEMODULE += uart0
endif
endif

ifneq (,$(filter posix,$(USEMODULE)))
USEMODULE += uart0
USEMODULE += timex
Expand Down
1 change: 1 addition & 0 deletions drivers/include/net_dev/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ typedef enum {
NET_DEV_PROTO_UDP = 0x0006, /**< UDP */
NET_DEV_PROTO_TCP = 0x0007, /**< TCP */
NET_DEV_PROTO_CCNL = 0x0008, /**< CCN lite */
NET_DEV_PROTO_UART_NET = 0x0009, /**< @ref uart_net packet */
} net_dev_proto_t;

/**
Expand Down
180 changes: 180 additions & 0 deletions sys/net/include/uart_net.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
/*
* Copyright (C) 2014 Martin Lenders
*
* This file is subject to the terms and conditions of the GNU Lesser General
* Public License. See the file LICENSE in the top level directory for more
* details.
*/

/**
* @defgroup uart_net UART transceiver
* @ingroup net
* @{
*
* @file uart_net.h
* @brief Provides a transceiver over the UART interface. Use
* `dist/tools/linux-border_router` to border route the packets
* on a Linux machine
*
* @author Martine Lenders <[email protected]>
*
* @}
*/

#ifndef __UART_NET_H_
#define __UART_NET_H_

#include <stdint.h>

#include "periph/uart.h"
#include "basic_net.h"

#include "ipv6.h"

#if !UART_NUMOF
/**
* @brief Guard type in the case that the board does not implement the new
* driver model
*/
typedef uint8_t uart_t;
#endif

/**
* @brief Type to define packet type.
*/
typedef uint8_t uart_net_packet_type_t;

#define UART_NET_PACKET_RAW_TYPE (0) /**< Raw packet type */
#define UART_NET_PACKET_CONF_TYPE (2) /**< configuration packet type */
#define UART_NET_PACKET_L3_TYPE (3) /**< Layer 3 packet type */

/**
* @brief Type to define configuration type
*/
typedef uint8_t uart_net_conf_type_byte_t;

/**
* @brief A 6LoWPAN header compression context.
* @see <a href="http://tools.ietf.org/html/rfc6775#section-7.2">
* RFC 6775, section 7.2
* </a>
* @see <a href="http://tools.ietf.org/html/rfc6282#section-3.1.2">
* RFC 6282, section 3.1.2
* </a>
*/
struct {
uint16_t version; /**< version of the context */
uint8_t cid; /**< context identifier */
ipv6_addr_t prefix; /**< prefix associated to CID */
uint8_t length; /**< length of the prefix */
uint8_t comp; /**< compression flag */
uint16_t lifetime; /**< lifetime of validity */
} uart_net_6lp_context_t;

/**
* @brief Type to configure a 6LoWPAN context
* @see @ref sixlowpan
*/
#define UART_NET_CONF_BYTE_6LP_CONTEXT (2)
#define UART_NET_CONF_BYTE_IPV6_ADDR (3) /**< Type to set IPv6 address */

/**
* @brief Basic packet type definition
*/
typedef struct __attribute__((packet)) {
uint8_t null; /**< Marker. Must always be 0 to deferentiate
between `uart_net` packets and normal
stdout. */
uart_net_packet_type_t type; /**< Type of the packet */
uint8_t seq_num; /**< Sequence number of the packet */
} uart_net_packet_t;

/**
* @brief Layer 3 packet type definition
* @extends uart_net_packet_t
*/
typedef struct __attribute__((packet)) {
uint8_t null; /**< Marker. Must always be 0 to deferentiate
between `uart_net` packets and normal
stdout. */
uart_net_packet_type_t type; /**< Type of the packet */
uint8_t seq_num; /**< Sequence number of the packet */

/**
* @brief Type of the layer 3 packet, identified by EtherType
* @see <a href="http://standards.ieee.org/develop/regauth/ethertype/eth.txt">
* IEEE public EtherTypes
* </a>
*/
uint16_t eth;
} uart_net_l3_t;

/**
* @brief Configuration packet type definition
* @extends uart_net_packet_t
*/
typedef struct __attribute__((packed)) {
uint8_t null; /**< Marker. Must always be 0 to deferentiate
between `uart_net` packets and normal
stdout. */
uart_net_packet_type_t type; /**< Type of the packet */
uint8_t seq_num; /**< Sequence number of the packet */
uart_net_conf_type_byte_t conf_type; /**< Configuration type. */
} uart_net_conf_t;

/**
* @brief Configuration packet type for 6LoWPAN context
* @extends uart_net_conf_t
*/
typedef struct __attribute__((packet)) {
uint8_t null; /**< Marker. Must always be 0 to deferentiate
between `uart_net` packets and normal
stdout. */
uart_net_packet_type_t type; /**< Type of the packet */
uint8_t seq_num; /**< Sequence number of the packet */
uart_net_conf_type_byte_t conf_type; /**< Configuration type. */
uart_net_conf_6lc_t context; /**< The context. */
} uart_net_conf_6lc_t;

/**
* @brief Configuration packet type for IPv6 address
* @extends uart_net_conf_t
*/
typedef struct __attribute__((packet))
{
uint8_t null; /**< Marker. Must always be 0 to deferentiate
between `uart_net` packets and normal
stdout. */
uart_net_packet_type_t type; /**< Type of the packet */
uint8_t seq_num; /**< Sequence number of the packet */
uart_net_conf_type_t conf_type; /**< Configuration type. */
ipv6_addr_t addr; /**< The IPv6 address */
} uart_net_conf_ipv6_t;

/**
* Initializes a new @ref uart_net control thread for UART device *uart*. If
* the board does not support the uart_t type it falls back to using
* board_uart0.h
*
* @param[in] uart The UART device, ignored if UART_NUMOF == 0 or not defined
*
* @return The PID of the @ref uart_net control thread
*/
kernel_pid_t uart_net_init(uart_t uart);

/**
* @brief Send a layer 3 packet over the UART device
*
* @param[in] pid The PID for the @ref uart_net control thread
* @param[in] upper_layer_hdrs All upper layer headers including layer 3.
* @param[in] data Data (*without* all upper layer headers) to send
* over the UART device
*/
static inline int uart_net_send_l3_packet(kernel_pid_t pid,
net_dev_hlist_t *upper_layer_hdrs,
void *data, size_t data_len)
{
return basic_net_send_data2(pid, upper_layer_hdrs, NULL, 0, data, data_len);
}

#endif /* __UART_NET_H_ */
2 changes: 2 additions & 0 deletions sys/net/link_layer/uart_net/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
INCLUDES += -I$(RIOTBASE)/sys/net/include
include $(RIOTBASE)/Makefile.base
121 changes: 121 additions & 0 deletions sys/net/link_layer/uart_net/uart_net.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* Copyright (C) 2014 Freie Universität Berlin
*
* This file is subject to the terms and conditions of the GNU Lesser General
* Public License. See the file LICENSE in the top level directory for more
* details.
*/

#include "stdlib.h"

#include "kernel.h"
#include "mutex.h"
#include "net_dev/base.h"
#include "periph/uart.h"
#include "ringbuffer.h"
#include "uart_net.h"

#if !UART_NUMOF
#include "board_uart0.h"
#include "posix_io.h"
#endif

#define ENABLE_DEBUG (0)
#include "debug.h"


#define _UART_NET_STACK_SIZE (KERNEL_CONF_STACKSIZE_DEFAULT)
#define _UART_NET_MAX_NAME_LEN (24)

#ifndef _UART_NET_REGISTRY_SIZE
#define _UART_NET_REGISTRY_SIZE (1)
#endif
#define _UART_NET_BUFFER_SIZE (sizeof(border_l3_header_t) + IPV6_MTU)

typedef struct {
kernel_pid_t registrar; /**< the thread the registrant is registered too*/
kernel_pid_t registrant;/**< the registered thread */
/**
* @brief protocol of the registered thread.
*
* @detail Must be either NET_DEV_PROTO_UNKNOWN, NET_DEV_PROTO_IPV6, or
* NET_DEV_PROTO_6LOWPAN
*/
net_dev_proto_t proto;
} _uart_net_registry_t;

static char _uart_net_reader_stack[_UART_NET_STACK_SIZE];
static char _uart_net_worker_stack[_UART_NET_STACK_SIZE];
static uart_net_registry_t _uart_net_registry[_UART_NET_REGISTRY_SIZE];
static char _uart_net_in_data[_UART_NET_BUFFER_SIZE];
static char _uart_net_out_data[_UART_NET_BUFFER_SIZE];
static mutex_t _uart_net_in_mutex = MUTEX_INIT;
static mutex_t _uart_net_out_mutex = MUTEX_INIT;
static ringbuffer_t _uart_net_in_buf = RINGBUFFER_INIT(_uart_net_in_data);
static ringbuffer_t _uart_net_in_buf = RINGBUFFER_INIT(_uart_net_in_data);

void *_uart_net_uart_reader(void *arg)
{
size_t bytes, offset;
uart_t uart = 0;
uart_net_packet_type_t *packet;

#if UART_NUMOF
memcpy(&uart, arg, sizeof(uart_t));
#else
(void)arg;

posix_open(uart0_handler_pid, 0);
#endif

while (1) {
bytes = readpacket(uart);

if (bytes < 0) {
DEBUG("Error %d occured on read\n", bytes);
continue;
}

/* TODO */
}
}

void *_uart_net_uart_reader(void *arg)
{
kernel_pid_t reader_pid;

memcpy(&reader_pid, arg, sizeof(kernel_pid_t));

/* TODO */
}

kernel_pid_t uart_init(uart_t uart)
{
kernel_pid_t reader_pid;
char worker_name[_UART_NET_MAX_NAME_LEN];
#if UART_NUMOF
char reader_name[_UART_NET_MAX_NAME_LEN];
char number[_UART_NET_MAX_NAME_LEN - strlen("uart_net_worker_")];

strcpy(reader_name, "uart_net_reader_");
strcpy(reader_name, atoi(uart, number, 10));
strcpy(worker_name, "uart_net_worker_");
strcpy(worker_name, number);

reader_pid = thread_create(_uart_net_reader_stack, _UART_NET_STACK_SIZE,
PRIORITY_MAIN - 1, 0, _uart_net_uart_reader, &uart,
name);
#else
(void)uart;

strcpy(worker_name, "uart_net_worker");

reader_pid = thread_create(_uart_net_reader_stack, _UART_NET_STACK_SIZE,
PRIORITY_MAIN - 1, 0, _uart_net_uart_reader, NULL,
"uart_net_reader");
#endif

return thread_create(_uart_net_worker_stack, _UART_NET_STACK_SIZE,
PRIORITY_MAIN - 1, 0, _uart_net_worker, &reader_pid,
worker_name);
}

0 comments on commit 77d88c3

Please sign in to comment.