Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added test application for nativenet transceiver #25

Merged
merged 3 commits into from Sep 24, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 37 additions & 0 deletions test_nativenet/Makefile
@@ -0,0 +1,37 @@
debug: CFLAGS += -g
debug: CFLAGS += -DENABLE_DEBUG

# name of your project
export PROJECT = test_nativenet
#
# for easy switching of boards
export BOARD = native

# this has to be the absolute path of the RIOT-base dir
export RIOTBASE =$(CURDIR)/../RIOT
export RIOTBOARD =$(CURDIR)/../../boards

ifeq (,$(findstring native,$(BOARD)))
include $(CURDIR)/../Makefile.unsupported
else

## Modules to include.

USEMODULE += auto_init
USEMODULE += hwtimer
USEMODULE += nativenet
USEMODULE += transceiver

export INCLUDES = -I$(RIOTBOARD)/$(BOARD)/include -I$(RIOTBASE)/core/include -I$(RIOTBASE)/cpu/$(CPU)/include -I$(RIOTBASE)/sys/lib -I$(RIOTBASE)/sys/include/ -I$(RIOTBASE)/drivers/include/

include $(RIOTBASE)/Makefile.include
debug: all

FORCE:
touch main.c

sender: CFLAGS += -DSENDER
sender: PROJECT = test_nativenet_sender
sender: FORCE all

endif
148 changes: 148 additions & 0 deletions test_nativenet/main.c
@@ -0,0 +1,148 @@
/*
* Copyright (C) 2013 Oliver Hahm <oliver.hahm@inria.fr>
*/

#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <time.h>

#include "hwtimer.h"
#include "board.h"
#include "transceiver.h"
#include "nativenet.h"
#include "msg.h"
#include "thread.h"

#define SENDER_ADDR (1)
#define DEFAULT_RCV_ADDR (2)

#define PACKET_SIZE (42)
#define WAIT_TIME (60)
#define SECOND (1000 * 1000)
#define SENDING_DELAY (10 * 1000)

#define RCV_BUFFER_SIZE (64)
#define RADIO_STACK_SIZE (KERNEL_CONF_STACKSIZE_DEFAULT)

char radio_stack_buffer[RADIO_STACK_SIZE];
msg_t msg_q[RCV_BUFFER_SIZE];
uint8_t snd_buffer[NATIVE_MAX_DATA_LENGTH];
uint8_t receiving = 1;
unsigned int last_seq = 0, missed_cnt = 0;
int first = -1;

void radio(void) {
msg_t m;
radio_packet_t *p;
unsigned int tmp = 0, cur_seq = 0;

msg_init_queue(msg_q, RCV_BUFFER_SIZE);

puts("Start receiving");
while (receiving) {
msg_receive(&m);
if (m.type == PKT_PENDING) {
p = (radio_packet_t*) m.content.ptr;
if ((p->src == SENDER_ADDR) && (p->length == PACKET_SIZE)) {
puts("received");
cur_seq = (p->data[0] << 8) + p->data[1];
if (first < 0) {
first = cur_seq;
}
else {
tmp = cur_seq - last_seq;
if (last_seq && (tmp > 1)) {
missed_cnt += (tmp - 1);
}
}
last_seq = cur_seq;
}
else {
printf("sender was %i\n", p->src);
}
p->processing--;
}
else if (m.type == ENOBUFFER) {
puts("Transceiver buffer full");
}
else {
puts("Unknown packet received");
}
}
}

void sender(void) {
unsigned int i=0;
msg_t mesg;
transceiver_command_t tcmd;
radio_packet_t p;

mesg.type = SND_PKT;
mesg.content.ptr = (char*) &tcmd;

tcmd.transceivers = TRANSCEIVER_NATIVE;
tcmd.data = &p;

p.length = PACKET_SIZE;
p.dst = 0;

puts("Start sending packets");
while (1) {
/* filling uint8_t buffer with uint16_t sequence number */
snd_buffer[0] = (i & 0xFF00) >> 8;
snd_buffer[1] = i & 0x00FF;
p.data = snd_buffer;
i++;
msg_send(&mesg, transceiver_pid, 1);
hwtimer_wait(HWTIMER_TICKS(SENDING_DELAY));
}
}

int main(void)
{
#ifndef SENDER
int radio_pid;
#endif
int16_t a;
msg_t mesg;
transceiver_command_t tcmd;

printf("\n\tmain(): initializing transceiver\n");
transceiver_init(TRANSCEIVER_NATIVE);

printf("\n\tmain(): starting transceiver\n");
transceiver_start();

#ifndef SENDER
printf("\n\tmain(): starting radio thread\n");
radio_pid = thread_create(radio_stack_buffer, RADIO_STACK_SIZE, PRIORITY_MAIN-2, CREATE_STACKTEST, radio, "radio");
transceiver_register(TRANSCEIVER_NATIVE, radio_pid);
#endif

#ifdef SENDER
a = SENDER_ADDR;
#elif defined ADDR
a = ADDR;
#else
a = DEFAULT_RCV_ADDR;
#endif
tcmd.transceivers = TRANSCEIVER_NATIVE;
tcmd.data = &a;
mesg.content.ptr = (char *) &tcmd;
mesg.type = SET_ADDRESS;

printf("[nativenet] trying to set address %" PRIi16 "\n", a);
msg_send_receive(&mesg, &mesg, transceiver_pid);

#ifdef SENDER
hwtimer_wait(HWTIMER_TICKS(SECOND));
sender();
#else
hwtimer_wait(HWTIMER_TICKS(WAIT_TIME * SECOND));
receiving = 0;
printf("Missed %u of %u packets after %u seconds\n", missed_cnt, (last_seq - first), WAIT_TIME);
#endif

return 0;
}