Skip to content

Commit

Permalink
[SQUASH ME] slip: port to netapi change
Browse files Browse the repository at this point in the history
  • Loading branch information
miri64 committed Jan 14, 2015
1 parent cde28bd commit 39baf32
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 72 deletions.
18 changes: 8 additions & 10 deletions sys/net/include/slip.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <stdint.h>

#include "netapi.h"
#include "pkt.h"
#include "periph/uart.h"
#include "ringbuffer.h"

Expand Down Expand Up @@ -59,22 +60,19 @@ kernel_pid_t slip_init(uart_t uart, uint32_t baudrate, ringbuffer_t *in_buf);
* @func slip_send_l3_packet
* @brief Send a layer 3 packet over the UART device
*
* @param[in] pid The PID for the @ref slip 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
* @param[in] data_len Length of *data*
* @param[in] pid The PID for the @ref slip control thread.
* @param[in] pkt Packet to send over the UART device.
*
* @return Number of bytes transmitted.
*/

#ifdef MODULE_NETAPI
static inline int slip_send_l3_packet(kernel_pid_t pid, netdev_hlist_t *upper_layer_hdrs,
void *data, size_t data_len)
static inline int slip_send_l3_packet(kernel_pid_t pid, pkt_t *pkt)
{
return netapi_send_packet(pid, upper_layer_hdrs, NULL, 0, data, data_len);
return netapi_send_packet(pid, NULL, 0, pkt);
}
#else
int slip_send_l3_packet(kernel_pid_t pid, netdev_hlist_t *upper_layer_hdrs,
void *data, size_t data_len);
int slip_send_l3_packet(kernel_pid_t pid, pkt_t *pkt);
#endif

#ifdef __cplusplus
Expand Down
88 changes: 41 additions & 47 deletions sys/net/link_layer/slip/slip.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,35 +135,35 @@ static int _slip_putc(uart_t uart, char c)

static void _slip_receive(msg_t *uart_msg, _uart_ctx_t *ctx)
{
netapi_rcv_pkt_t packet;
netapi_pkt_t rcv;
netapi_ack_t ack_mem;
msg_t l3_pkt, l3_ack, uart_reply;
size_t bytes = ctx->bytes;
char *pkt = (char *)pktbuf_alloc(bytes);

ringbuffer_get(ctx->in_buf, pkt, bytes);

msg_reply(uart_msg, &uart_reply);

packet.type = NETAPI_CMD_RCV;
packet.ack = &ack_mem;
packet.src = NULL;
packet.src_len = 0;
packet.dest = NULL;
packet.dest_len = 0;
rcv.type = NETAPI_CMD_RCV;
rcv.ack = &ack_mem;
rcv.src = NULL;
rcv.src_len = 0;
rcv.dest = NULL;
rcv.dest_len = 0;
l3_pkt.type = NETAPI_MSG_TYPE;
l3_pkt.content.ptr = (char *)(&packet);
l3_pkt.content.ptr = (char *)(&rcv);

for (int i = 0; i < _SLIP_REGISTRY_SIZE; i++) {
if (ctx->registry[i] != KERNEL_PID_UNDEF) {
size_t offset = 0;

while (offset < ctx->bytes) {
packet.data = (void *)(pkt + offset);
packet.data_len = bytes - offset;
pkt_t *pkt = pktbuf_alloc(bytes);

ringbuffer_get(ctx->in_buf, pkt->payload_data, bytes - offset);
rcv.pkt = pkt;
netapi_ack_t *ack;

msg_send_receive(&l3_pkt, &l3_ack, ctx->registry[i]);
pktbuf_release(pkt);
ack = (netapi_ack_t *)(l3_ack.content.ptr);

if (l3_ack.type == NETAPI_MSG_TYPE &&
Expand All @@ -186,8 +186,6 @@ static void _slip_receive(msg_t *uart_msg, _uart_ctx_t *ctx)
return;
}
}

pktbuf_release(pkt);
}
}
}
Expand Down Expand Up @@ -227,40 +225,35 @@ static int _slip_write(uart_t uart, uint8_t *bytes, size_t len)
return len;
}

static int _slip_send(uart_t uart, netapi_snd_pkt_t *snd)
static int _slip_send(uart_t uart, netapi_pkt_t *snd)
{
int snd_byte = 0, res;
pkt_hlist_t *ptr = snd->pkt->headers;

if (snd->ulh != NULL) {
netdev_hlist_t *ptr = snd->ulh;

do {
pktbuf_hold(ptr->header);
pktbuf_hold(snd->pkt);

if ((res = _slip_write(uart, ptr->header, ptr->header_len)) < 0) {
return res;
}
while (ptr) {
if ((res = _slip_write(uart, ptr->header_data, ptr->header_len)) < 0) {
return res;
}

pktbuf_release(ptr->header);
snd_byte += res;
netdev_hlist_advance(&ptr);
} while (ptr != snd->ulh);
snd_byte += res;
pkt_hlist_advance(&ptr);
}

if (snd->data != NULL) {
pktbuf_hold(snd->data);

if ((res = _slip_write(uart, snd->data, snd->data_len))) {
if (snd->pkt->payload_data != NULL) {
if ((res = _slip_write(uart, snd->pkt->payload_data,
snd->pkt->payload_len))) {
return res;
}

pktbuf_release(snd->data);

snd_byte += res;
}

_slip_putc(uart, _SLIP_END);

pktbuf_release(snd->pkt);

return snd_byte;
}

Expand All @@ -278,7 +271,7 @@ static void _slip_handle_cmd(uart_t uart, msg_t *msg_cmd)

switch (cmd->type) {
case NETAPI_CMD_SND:
ack->result = _slip_send(uart, (netapi_snd_pkt_t *)cmd);
ack->result = _slip_send(uart, (netapi_pkt_t *)cmd);
break;

case NETAPI_CMD_GET:
Expand Down Expand Up @@ -405,31 +398,32 @@ kernel_pid_t slip_init(uart_t uart, uint32_t baudrate, ringbuffer_t *in_buf)
}

#ifndef MODULE_NETAPI
int slip_send_l3_packet(kernel_pid_t pid, netdev_hlist_t *upper_layer_hdrs,
void *data, size_t data_len)
int slip_send_l3_packet(kernel_pid_t pid, pkt_t *pkt)
{
msg_t msg_pkt, msg_ack;
netapi_snd_pkt_t pkt;
netapi_pkt_t snd;
netapi_ack_t ack;
int ack_result;

msg_pkt.type = NETAPI_MSG_TYPE;
msg_pkt.content.ptr = (char *)(&pkt);
msg_pkt.content.ptr = (char *)(&snd);

pktbuf_hold(pkt);

pkt.type = NETAPI_CMD_SND;
pkt.ack = &ack;
pkt.ulh = upper_layer_hdrs;
pkt.dest = NULL;
pkt.dest_len = 0;
pkt.data = data;
pkt.data_len = data_len;
snd.type = NETAPI_CMD_SND;
snd.ack = &ack;
snd.dest = NULL;
snd.dest_len = 0;
snd.pkt = pkt;

msg_send_receive(&msg_pkt, &msg_ack, pid);

pktbuf_release(pkt);

if (msg_ack.content.ptr != (char *)(&ack) ||
msg_ack.type != NETAPI_MSG_TYPE ||
ack.type != NETAPI_CMD_ACK ||
ack.orig != pkt.type) {
ack.orig != snd.type) {
return -ENOMSG;
}

Expand Down
50 changes: 35 additions & 15 deletions tests/slip/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,34 +35,54 @@
#define INBUF_SIZE (64)
#define READER_STACK_SIZE (KERNEL_CONF_STACKSIZE_DEFAULT)

#ifndef LED_ON
# ifdef LED_GREEN_ON
# define LED_ON LED_GREEN_ON;
# elif defined(LED_ORANGE_ON)
# define LED_ON LED_ORANGE_ON;
# elif defined(LED_RED_ON)
# define LED_ON LED_RED_ON;
# endif
#endif

#ifndef LED_OFF
# ifdef LED_GREEN_OFF
# define LED_OFF LED_GREEN_OFF;
# elif defined(LED_ORANGE_OFF)
# define LED_OFF LED_ORANGE_OFF;
# elif defined(LED_RED_OFF)
# define LED_OFF LED_RED_OFF;
# endif
#endif

static char inbuf_mem[INBUF_SIZE], reader_stack[READER_STACK_SIZE];
static ringbuffer_t inbuf = RINGBUFFER_INIT(inbuf_mem);
static kernel_pid_t slip_pid;

static void blink_led(uint8_t *data, size_t data_len)
static void blink_led(pkt_t *pkt)
{
for (size_t i = 0; i < data_len; i++) {
if (data[i] > 127) {
LED_RED_ON;
LED_GREEN_ON;
uint8_t *data = pkt->payload_data;

for (size_t i = 0; i < pkt->payload_len; i++) {
if (data[i] % 2) {
LED_ON;
}
else {
LED_RED_OFF;
LED_GREEN_OFF;
LED_OFF;
}
}
}

static inline void echo(void *data, size_t size)
static inline void echo(pkt_t *pkt)
{
slip_send_l3_packet(slip_pid, NULL, data, size);
slip_send_l3_packet(slip_pid, pkt);
}

static void *reader(void *args)
{
msg_t msg_rcv, msg_ack, msg_queue[MSG_QUEUE_SIZE];

netapi_rcv_pkt_t *rcv;
netapi_pkt_t *rcv;
netapi_ack_t *ack;

(void)args;
Expand All @@ -80,7 +100,7 @@ static void *reader(void *args)
continue;
}

rcv = (netapi_rcv_pkt_t *)(msg_rcv.content.ptr);
rcv = (netapi_pkt_t *)(msg_rcv.content.ptr);
msg_ack.content.ptr = (char *)rcv->ack;
ack = rcv->ack;

Expand All @@ -90,12 +110,12 @@ static void *reader(void *args)
continue;
}

pktbuf_hold(rcv->data);
pktbuf_hold(rcv->pkt);
msg_reply(&msg_rcv, &msg_ack);

blink_led(rcv->data, rcv->data_len);
echo(rcv->data, rcv->data_len);
pktbuf_release(rcv->data);
blink_led(rcv->pkt);
echo(rcv->pkt);
pktbuf_release(rcv->pkt);
}

return NULL;
Expand Down

0 comments on commit 39baf32

Please sign in to comment.