Commit 3e95e496 authored by Robert Hartung's avatar Robert Hartung
Browse files

First working version (over night), except samr21-xpro, but telosb and inga

parents
APPLICATION=software
BOARD ?= inga_blue
RIOTBASE ?= $(CURDIR)/../../RIOT/
DEVELHELP ?= 1
USEMODULE += shell
USEMODULE += shell_commands
USEMODULE += ps
USEMODULE += saul_default
USEMODULE += gnrc
USEMODULE += gnrc_netdev_default
USEMODULE += auto_init_gnrc_netif
#USEMODULE += gnrc_txtsnd
#USEMODULE += gnrc_pktdump
NODE_ID ?= 0
CFLAGS += -DNODE_ID=$(NODE_ID)
CFLAGS += -DDEBUG_ASSERT_VERBOSE
include $(RIOTBASE)/Makefile.include
/*
* Copyright (C) 2008, 2009, 2010 Kaspar Schleiser <kaspar@schleiser.de>
* Copyright (C) 2013 INRIA
* Copyright (C) 2013 Ludwig Knüpfer <ludwig.knuepfer@fu-berlin.de>
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup examples
* @{
*
* @file
* @brief Default application that shows a lot of functionality of RIOT
*
* @author Kaspar Schleiser <kaspar@schleiser.de>
* @author Oliver Hahm <oliver.hahm@inria.fr>
* @author Ludwig Knüpfer <ludwig.knuepfer@fu-berlin.de>
*
* @}
*/
#include <stdio.h>
#include <string.h>
#include "msg.h"
#include "thread.h"
#include "shell.h"
#include "shell_commands.h"
#include "xtimer.h"
#include "net/gnrc.h"
#include "net/gnrc/netreg.h"
#include "net/gnrc/netif/ieee802154.h"
#include "fmt.h"
#define SEND_INTERVAL (1)
#define RCV_QUEUE_SIZE (4)
#define MAX_PAYLOAD_LENGTH (128)
#define MAGIC_STRING ("IBREVAL")
#ifndef NODE_ID
#error NODE_ID undefined
#endif
char dump_thread_stack[256];
static msg_t dump_thread_msg_queue[RCV_QUEUE_SIZE];
static kernel_pid_t dump_thread_pid;
char send_thread_stack[256];
static gnrc_netif_t *ieee802154_netif = NULL;
typedef struct {
char magic_string[7];
uint8_t node_id;
uint16_t seq_nr;
uint8_t temp;
uint8_t payload[MAX_PAYLOAD_LENGTH];
} eval_message_t;
eval_message_t eval_message = { .node_id = NODE_ID, .magic_string = MAGIC_STRING };
static void _dump(gnrc_pktsnip_t *pkt) {
gnrc_pktsnip_t *snip = pkt;
gnrc_pktsnip_t *payload = NULL;
uint8_t *data;
eval_message_t *packet;
gnrc_netif_hdr_t *hdr;
uint8_t len;
// printf("_dump: %d %d\n", gnrc_netreg_num(GNRC_NETTYPE_UNDEF, 0), gnrc_netreg_num(GNRC_NETTYPE_NETIF, 0));
while(snip != NULL) {
switch(snip->type) {
case GNRC_NETTYPE_UNDEF :
payload = snip;
break;
case GNRC_NETTYPE_NETIF :
hdr = snip->data;
printf("HDR: %d %d %d\n", hdr->rssi, hdr->lqi, hdr->crc_valid);
/*
print("HDR: ", 5);
print_u32_dec((uint32_t)hdr->rssi);
print(" ", 1);
print_u32_dec((uint32_t)hdr->lqi);
print(" ", 1);
print_u32_dec((uint32_t)hdr->crc_valid);
print("\n", 1);
*/
packet = (eval_message_t*)payload->data;
if(hdr->crc_valid && strcmp(packet->magic_string, MAGIC_STRING) == 0) {
printf("PACKET: #%d from %d\n", packet->seq_nr, packet->node_id);
} else {
len = payload->size;
if(len > 100) {
len = 100;
}
data = (uint8_t*)payload->data;
/*
print("Contents(", 9);
print_u32_dec((uint32_t)len);
print("Contents)", 1);
*/
printf("Contents(%02d):", len);
for(uint8_t i=0; i<len; i++) {
//print_byte_hex(data[i]);
printf(" %02X", data[i]);
}
//print(" | ", 3);
printf(" | ");
for(uint8_t i=0; i<len; i++) {
if(data[i] >= ' ' && data[i] <= '~') {
printf("%c", data[i]);
//print((char*)&data[i], 1);
} else {
printf(".");
//print(".", 1);
}
}
puts("");
}
break;
default :
printf("snip of type %d\n", snip->type);
break;
}
snip = snip->next;
}
gnrc_pktbuf_release(pkt);
}
void *dump_thread(void *arg)
{
(void) arg;
msg_init_queue(dump_thread_msg_queue, RCV_QUEUE_SIZE);
gnrc_netreg_entry_t me_reg = GNRC_NETREG_ENTRY_INIT_PID(GNRC_NETREG_DEMUX_CTX_ALL, sched_active_pid);
gnrc_netreg_register(GNRC_NETTYPE_UNDEF, &me_reg);
msg_t msg;
while(1) {
if(msg_receive(&msg) != 1) {
puts("Unable to receive message");
continue;
}
//printf("[dump_thread] message received: %d\n", msg.type);
switch(msg.type) {
case GNRC_NETAPI_MSG_TYPE_RCV :
_dump( msg.content.ptr );
break;
default :
puts("ERROR: Unknown message type???");
/// gnrc_pktbuf_release( msg.content.ptr );
break;
}
}
puts("END OF dump_thread");
return NULL;
}
static netopt_enable_t enable = true;
static netopt_enable_t disable = false;
void *send_thread(void *arg)
{
(void) arg;
/*
struct iovec vector[2];
uint8_t hdr[IEEE802154_MAX_HDR_LEN];
uint8_t src[2] = {1,1};
uint8_t dst[2] = {0xFF, 0xFF};
//uint8_t pan[2] = {0x12, 0x34};
le_uint16_t pan = {.u16 = 0x1234};
uint8_t seq = 0;
int res = ieee802154_set_frame_hdr(hdr, src, 2, dst, 2, pan, pan, NETDEV_IEEE802154_RAW | IEEE802154_FCF_TYPE_DATA, seq);
vector[0].iov_base = hdr;
vector[0].iov_len = (size_t)res;
vector[1].iov_base = data;
vector[1].iov_len = size;
*/
size_t addr_len = 0;
uint8_t addr[GNRC_NETIF_L2ADDR_MAXLEN];
gnrc_pktsnip_t *pkt, *hdr;
gnrc_netif_hdr_t *nethdr;
int ret;
ret = gnrc_netapi_set(ieee802154_netif->pid, NETOPT_PROMISCUOUSMODE, 0, &enable, sizeof(netopt_enable_t));
ret = gnrc_netapi_set(ieee802154_netif->pid, NETOPT_AUTOACK, 0, &disable, sizeof(netopt_enable_t));
ret = gnrc_netapi_set(ieee802154_netif->pid, NETOPT_CSMA, 0, &disable, sizeof(netopt_enable_t));
uint8_t retrans = 0;
ret = gnrc_netapi_set(ieee802154_netif->pid, NETOPT_RETRANS, 0, &retrans, sizeof(retrans));
int8_t retries = 7;
ret = gnrc_netapi_set(ieee802154_netif->pid, NETOPT_CSMA_RETRIES, 0, &retries, sizeof(retries));
(void)ret;
uint8_t flags = 0 | GNRC_NETIF_HDR_FLAGS_BROADCAST;
//uint8_t data[25];
//uint8_t size;
uint8_t payload_length = sprintf((char*)eval_message.payload, "Hello from %2d", NODE_ID);
while(1) {
xtimer_sleep(SEND_INTERVAL);
printf("send... ");
eval_message.seq_nr++;
/* put packet together */
pkt = gnrc_pktbuf_add(NULL, &eval_message, sizeof(eval_message) - MAX_PAYLOAD_LENGTH + payload_length, GNRC_NETTYPE_UNDEF);
if (pkt == NULL) {
puts("ERROR: packet buffer full");
return NULL;
}
hdr = gnrc_netif_hdr_build(NULL, 0, addr, addr_len);
if (hdr == NULL) {
puts("ERROR: packet buffer full");
gnrc_pktbuf_release(pkt);
return NULL;
}
LL_PREPEND(pkt, hdr);
nethdr = (gnrc_netif_hdr_t *)hdr->data;
nethdr->flags = flags;
/* and send it */
if (gnrc_netapi_send(ieee802154_netif->pid, pkt) < 1) {
puts("ERROR: unable to send");
gnrc_pktbuf_release(pkt);
} else {
puts("OK");
}
}
return NULL;
}
int main(void)
{
/// +1 -> INGA working, but TelosB/Sky not
dump_thread_pid = thread_create(dump_thread_stack, sizeof(dump_thread_stack), THREAD_PRIORITY_MAIN + 2, 0, dump_thread, NULL, "dump_thread");
gnrc_netif_t *netif = NULL;
if((netif = gnrc_netif_iter(netif))) {
puts("Found gnrc netif");
ieee802154_netif = netif;
/// +2 -> INGA working, but TelosB/Sky not
thread_create(send_thread_stack, sizeof(send_thread_stack), THREAD_PRIORITY_MAIN + 1, THREAD_CREATE_STACKTEST, send_thread, NULL, "send_thread");
} else {
puts("Unable to find netif");
}
(void) puts("Welcome to RIOT!");
printf("This is node %d\n", NODE_ID);
char line_buf[SHELL_DEFAULT_BUFSIZE];
shell_run(NULL, line_buf, SHELL_DEFAULT_BUFSIZE);
return 0;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment