Skip to content

Commit 77d88c3

Browse files
Martin Lendersmiri64
authored andcommitted
[WIP] Initial import of uart_net header definition
1 parent df2317f commit 77d88c3

File tree

5 files changed

+313
-0
lines changed

5 files changed

+313
-0
lines changed

Makefile.dep

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ ifneq (,$(filter sixlowpan,$(USEMODULE)))
3434
USEMODULE += vtimer
3535
endif
3636

37+
ifneq (,$(filter uart_net,$(USEMODULE)))
38+
USEMODULE += basic_net
39+
USEMODULE += ringbuffer
40+
ifeq(,$(filter-out $(BOARD),arduino-due iot-lab_M3 pca10000 pca10005 stm32f0discovery stm32f3discovery stm32f4discovery))
41+
USEMODULE += posix
42+
USEMODULE += uart0
43+
endif
44+
endif
45+
3746
ifneq (,$(filter posix,$(USEMODULE)))
3847
USEMODULE += uart0
3948
USEMODULE += timex

drivers/include/net_dev/base.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ typedef enum {
8080
NET_DEV_PROTO_UDP = 0x0006, /**< UDP */
8181
NET_DEV_PROTO_TCP = 0x0007, /**< TCP */
8282
NET_DEV_PROTO_CCNL = 0x0008, /**< CCN lite */
83+
NET_DEV_PROTO_UART_NET = 0x0009, /**< @ref uart_net packet */
8384
} net_dev_proto_t;
8485

8586
/**

sys/net/include/uart_net.h

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
/*
2+
* Copyright (C) 2014 Martin Lenders
3+
*
4+
* This file is subject to the terms and conditions of the GNU Lesser General
5+
* Public License. See the file LICENSE in the top level directory for more
6+
* details.
7+
*/
8+
9+
/**
10+
* @defgroup uart_net UART transceiver
11+
* @ingroup net
12+
* @{
13+
*
14+
* @file uart_net.h
15+
* @brief Provides a transceiver over the UART interface. Use
16+
* `dist/tools/linux-border_router` to border route the packets
17+
* on a Linux machine
18+
*
19+
* @author Martine Lenders <[email protected]>
20+
*
21+
* @}
22+
*/
23+
24+
#ifndef __UART_NET_H_
25+
#define __UART_NET_H_
26+
27+
#include <stdint.h>
28+
29+
#include "periph/uart.h"
30+
#include "basic_net.h"
31+
32+
#include "ipv6.h"
33+
34+
#if !UART_NUMOF
35+
/**
36+
* @brief Guard type in the case that the board does not implement the new
37+
* driver model
38+
*/
39+
typedef uint8_t uart_t;
40+
#endif
41+
42+
/**
43+
* @brief Type to define packet type.
44+
*/
45+
typedef uint8_t uart_net_packet_type_t;
46+
47+
#define UART_NET_PACKET_RAW_TYPE (0) /**< Raw packet type */
48+
#define UART_NET_PACKET_CONF_TYPE (2) /**< configuration packet type */
49+
#define UART_NET_PACKET_L3_TYPE (3) /**< Layer 3 packet type */
50+
51+
/**
52+
* @brief Type to define configuration type
53+
*/
54+
typedef uint8_t uart_net_conf_type_byte_t;
55+
56+
/**
57+
* @brief A 6LoWPAN header compression context.
58+
* @see <a href="http://tools.ietf.org/html/rfc6775#section-7.2">
59+
* RFC 6775, section 7.2
60+
* </a>
61+
* @see <a href="http://tools.ietf.org/html/rfc6282#section-3.1.2">
62+
* RFC 6282, section 3.1.2
63+
* </a>
64+
*/
65+
struct {
66+
uint16_t version; /**< version of the context */
67+
uint8_t cid; /**< context identifier */
68+
ipv6_addr_t prefix; /**< prefix associated to CID */
69+
uint8_t length; /**< length of the prefix */
70+
uint8_t comp; /**< compression flag */
71+
uint16_t lifetime; /**< lifetime of validity */
72+
} uart_net_6lp_context_t;
73+
74+
/**
75+
* @brief Type to configure a 6LoWPAN context
76+
* @see @ref sixlowpan
77+
*/
78+
#define UART_NET_CONF_BYTE_6LP_CONTEXT (2)
79+
#define UART_NET_CONF_BYTE_IPV6_ADDR (3) /**< Type to set IPv6 address */
80+
81+
/**
82+
* @brief Basic packet type definition
83+
*/
84+
typedef struct __attribute__((packet)) {
85+
uint8_t null; /**< Marker. Must always be 0 to deferentiate
86+
between `uart_net` packets and normal
87+
stdout. */
88+
uart_net_packet_type_t type; /**< Type of the packet */
89+
uint8_t seq_num; /**< Sequence number of the packet */
90+
} uart_net_packet_t;
91+
92+
/**
93+
* @brief Layer 3 packet type definition
94+
* @extends uart_net_packet_t
95+
*/
96+
typedef struct __attribute__((packet)) {
97+
uint8_t null; /**< Marker. Must always be 0 to deferentiate
98+
between `uart_net` packets and normal
99+
stdout. */
100+
uart_net_packet_type_t type; /**< Type of the packet */
101+
uint8_t seq_num; /**< Sequence number of the packet */
102+
103+
/**
104+
* @brief Type of the layer 3 packet, identified by EtherType
105+
* @see <a href="http://standards.ieee.org/develop/regauth/ethertype/eth.txt">
106+
* IEEE public EtherTypes
107+
* </a>
108+
*/
109+
uint16_t eth;
110+
} uart_net_l3_t;
111+
112+
/**
113+
* @brief Configuration packet type definition
114+
* @extends uart_net_packet_t
115+
*/
116+
typedef struct __attribute__((packed)) {
117+
uint8_t null; /**< Marker. Must always be 0 to deferentiate
118+
between `uart_net` packets and normal
119+
stdout. */
120+
uart_net_packet_type_t type; /**< Type of the packet */
121+
uint8_t seq_num; /**< Sequence number of the packet */
122+
uart_net_conf_type_byte_t conf_type; /**< Configuration type. */
123+
} uart_net_conf_t;
124+
125+
/**
126+
* @brief Configuration packet type for 6LoWPAN context
127+
* @extends uart_net_conf_t
128+
*/
129+
typedef struct __attribute__((packet)) {
130+
uint8_t null; /**< Marker. Must always be 0 to deferentiate
131+
between `uart_net` packets and normal
132+
stdout. */
133+
uart_net_packet_type_t type; /**< Type of the packet */
134+
uint8_t seq_num; /**< Sequence number of the packet */
135+
uart_net_conf_type_byte_t conf_type; /**< Configuration type. */
136+
uart_net_conf_6lc_t context; /**< The context. */
137+
} uart_net_conf_6lc_t;
138+
139+
/**
140+
* @brief Configuration packet type for IPv6 address
141+
* @extends uart_net_conf_t
142+
*/
143+
typedef struct __attribute__((packet))
144+
{
145+
uint8_t null; /**< Marker. Must always be 0 to deferentiate
146+
between `uart_net` packets and normal
147+
stdout. */
148+
uart_net_packet_type_t type; /**< Type of the packet */
149+
uint8_t seq_num; /**< Sequence number of the packet */
150+
uart_net_conf_type_t conf_type; /**< Configuration type. */
151+
ipv6_addr_t addr; /**< The IPv6 address */
152+
} uart_net_conf_ipv6_t;
153+
154+
/**
155+
* Initializes a new @ref uart_net control thread for UART device *uart*. If
156+
* the board does not support the uart_t type it falls back to using
157+
* board_uart0.h
158+
*
159+
* @param[in] uart The UART device, ignored if UART_NUMOF == 0 or not defined
160+
*
161+
* @return The PID of the @ref uart_net control thread
162+
*/
163+
kernel_pid_t uart_net_init(uart_t uart);
164+
165+
/**
166+
* @brief Send a layer 3 packet over the UART device
167+
*
168+
* @param[in] pid The PID for the @ref uart_net control thread
169+
* @param[in] upper_layer_hdrs All upper layer headers including layer 3.
170+
* @param[in] data Data (*without* all upper layer headers) to send
171+
* over the UART device
172+
*/
173+
static inline int uart_net_send_l3_packet(kernel_pid_t pid,
174+
net_dev_hlist_t *upper_layer_hdrs,
175+
void *data, size_t data_len)
176+
{
177+
return basic_net_send_data2(pid, upper_layer_hdrs, NULL, 0, data, data_len);
178+
}
179+
180+
#endif /* __UART_NET_H_ */

sys/net/link_layer/uart_net/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
INCLUDES += -I$(RIOTBASE)/sys/net/include
2+
include $(RIOTBASE)/Makefile.base
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
* Copyright (C) 2014 Freie Universität Berlin
3+
*
4+
* This file is subject to the terms and conditions of the GNU Lesser General
5+
* Public License. See the file LICENSE in the top level directory for more
6+
* details.
7+
*/
8+
9+
#include "stdlib.h"
10+
11+
#include "kernel.h"
12+
#include "mutex.h"
13+
#include "net_dev/base.h"
14+
#include "periph/uart.h"
15+
#include "ringbuffer.h"
16+
#include "uart_net.h"
17+
18+
#if !UART_NUMOF
19+
#include "board_uart0.h"
20+
#include "posix_io.h"
21+
#endif
22+
23+
#define ENABLE_DEBUG (0)
24+
#include "debug.h"
25+
26+
27+
#define _UART_NET_STACK_SIZE (KERNEL_CONF_STACKSIZE_DEFAULT)
28+
#define _UART_NET_MAX_NAME_LEN (24)
29+
30+
#ifndef _UART_NET_REGISTRY_SIZE
31+
#define _UART_NET_REGISTRY_SIZE (1)
32+
#endif
33+
#define _UART_NET_BUFFER_SIZE (sizeof(border_l3_header_t) + IPV6_MTU)
34+
35+
typedef struct {
36+
kernel_pid_t registrar; /**< the thread the registrant is registered too*/
37+
kernel_pid_t registrant;/**< the registered thread */
38+
/**
39+
* @brief protocol of the registered thread.
40+
*
41+
* @detail Must be either NET_DEV_PROTO_UNKNOWN, NET_DEV_PROTO_IPV6, or
42+
* NET_DEV_PROTO_6LOWPAN
43+
*/
44+
net_dev_proto_t proto;
45+
} _uart_net_registry_t;
46+
47+
static char _uart_net_reader_stack[_UART_NET_STACK_SIZE];
48+
static char _uart_net_worker_stack[_UART_NET_STACK_SIZE];
49+
static uart_net_registry_t _uart_net_registry[_UART_NET_REGISTRY_SIZE];
50+
static char _uart_net_in_data[_UART_NET_BUFFER_SIZE];
51+
static char _uart_net_out_data[_UART_NET_BUFFER_SIZE];
52+
static mutex_t _uart_net_in_mutex = MUTEX_INIT;
53+
static mutex_t _uart_net_out_mutex = MUTEX_INIT;
54+
static ringbuffer_t _uart_net_in_buf = RINGBUFFER_INIT(_uart_net_in_data);
55+
static ringbuffer_t _uart_net_in_buf = RINGBUFFER_INIT(_uart_net_in_data);
56+
57+
void *_uart_net_uart_reader(void *arg)
58+
{
59+
size_t bytes, offset;
60+
uart_t uart = 0;
61+
uart_net_packet_type_t *packet;
62+
63+
#if UART_NUMOF
64+
memcpy(&uart, arg, sizeof(uart_t));
65+
#else
66+
(void)arg;
67+
68+
posix_open(uart0_handler_pid, 0);
69+
#endif
70+
71+
while (1) {
72+
bytes = readpacket(uart);
73+
74+
if (bytes < 0) {
75+
DEBUG("Error %d occured on read\n", bytes);
76+
continue;
77+
}
78+
79+
/* TODO */
80+
}
81+
}
82+
83+
void *_uart_net_uart_reader(void *arg)
84+
{
85+
kernel_pid_t reader_pid;
86+
87+
memcpy(&reader_pid, arg, sizeof(kernel_pid_t));
88+
89+
/* TODO */
90+
}
91+
92+
kernel_pid_t uart_init(uart_t uart)
93+
{
94+
kernel_pid_t reader_pid;
95+
char worker_name[_UART_NET_MAX_NAME_LEN];
96+
#if UART_NUMOF
97+
char reader_name[_UART_NET_MAX_NAME_LEN];
98+
char number[_UART_NET_MAX_NAME_LEN - strlen("uart_net_worker_")];
99+
100+
strcpy(reader_name, "uart_net_reader_");
101+
strcpy(reader_name, atoi(uart, number, 10));
102+
strcpy(worker_name, "uart_net_worker_");
103+
strcpy(worker_name, number);
104+
105+
reader_pid = thread_create(_uart_net_reader_stack, _UART_NET_STACK_SIZE,
106+
PRIORITY_MAIN - 1, 0, _uart_net_uart_reader, &uart,
107+
name);
108+
#else
109+
(void)uart;
110+
111+
strcpy(worker_name, "uart_net_worker");
112+
113+
reader_pid = thread_create(_uart_net_reader_stack, _UART_NET_STACK_SIZE,
114+
PRIORITY_MAIN - 1, 0, _uart_net_uart_reader, NULL,
115+
"uart_net_reader");
116+
#endif
117+
118+
return thread_create(_uart_net_worker_stack, _UART_NET_STACK_SIZE,
119+
PRIORITY_MAIN - 1, 0, _uart_net_worker, &reader_pid,
120+
worker_name);
121+
}

0 commit comments

Comments
 (0)