1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

Add pnet tests

This commit is contained in:
Martin Lenders 2014-01-17 12:49:48 +01:00
parent 4333467e02
commit 82af516691
2 changed files with 142 additions and 0 deletions

52
tests/test_pnet/Makefile Normal file
View File

@ -0,0 +1,52 @@
####
#### Sample Makefile for building apps with the RIOT OS
####
#### The Sample Filesystem Layout is:
#### /this makefile
#### ../../RIOT
#### ../../boards for board definitions (if you have one or more)
####
# name of your project
export PROJECT =test_pnet
# for easy switching of boards
ifeq ($(strip $(BOARD)),)
export BOARD = native
endif
# this has to be the absolute path of the RIOT-base dir
export RIOTBASE =$(CURDIR)/../..
export RIOTBOARD =$(RIOTBASE)/boards
ifeq ($(BOARD),stm32f4discovery)
include Makefile.$(BOARD)
endif
## Modules to include.
USEMODULE += auto_init
USEMODULE += posix
USEMODULE += pnet
USEMODULE += vtimer
ifeq ($(strip $(BOARD)),native)
USEMODULE += nativenet
else ifeq ($(strip $(BOARD)),msba2)
USEMODULE += cc110x_ng
endif
ifeq ($(BOARD),native)
CFLAGS += -isystem $(RIOTBASE)/sys/net/include \
-isystem $(RIOTBASE)/sys/posix/pnet/include
else
export INCLUDES += -I$(RIOTBASE)/sys/net/include \
-I$(RIOTBASE)/sys/posix/pnet/include \
-I$(RIOTBASE)/sys/posix/include
endif
ifeq ($(BOARD),msba2)
export INCLUDES += -I$(RIOTBASE)/drivers/cc110x_ng/include \
-I$(RIOTBASE)/boards/msba2-common/include
endif
include $(RIOTBASE)/Makefile.include

90
tests/test_pnet/main.c Normal file
View File

@ -0,0 +1,90 @@
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include "transceiver.h"
#include "ipv6.h"
#ifdef MODULE_NATIVENET
#define TRANSCEIVER TRANSCEIVER_NATIVE
#else
#define TRANSCEIVER TRANSCEIVER_CC1100
#endif
#ifndef R_ADDR
#define R_ADDR (1)
#endif
#define PORT (1234)
void init_local_address(uint16_t r_addr) {
ipv6_addr_t std_addr;
ipv6_addr_init(&std_addr, 0xabcd, 0xef12, 0, 0, 0x1034, 0x00ff, 0xfe00,
r_addr);
sixlowpan_lowpan_adhoc_init(TRANSCEIVER, &std_addr, r_addr);
}
int main(void) {
int sockfd, res;
struct sockaddr_in6 my_addr, their_addr = {
.sin6_family = AF_INET6,
#if R_ADDR == 1
.sin6_port = PORT,
#else
.sin6_port = 0,
#endif
.sin6_flowinfo = 0,
#if R_ADDR == 1
.sin6_addr = {{{0xab, 0xcd, 0xef, 0x12, 0x00, 0x00, 0x00, 0x00,
0x10, 0x34, 0x00, 0xff, 0xfe, 0x00, 0x00, 0x02}}},
#else
.sin6_addr = {{{0xab, 0xcd, 0xef, 0x12, 0x00, 0x00, 0x00, 0x00,
0x10, 0x34, 0x00, 0xff, 0xfe, 0x00, 0x00, 0x01}}},
#endif
.sin6_scope_id = 0,
};
char buffer[14];
init_local_address(R_ADDR);
memcpy(buffer, "Hello, World!", 14);
memcpy(&my_addr, &in6addr_any, sizeof (my_addr));
my_addr.sin6_port = PORT;
sockfd = socket(AF_INET6, SOCK_DGRAM, 0);
res = bind(sockfd, (struct sockaddr *)&my_addr, sizeof (my_addr));
if (res < 0) {
perror("Socket could not be bind");
return 1;
}
#if R_ADDR == 1
res = sendto(sockfd, buffer, strlen(buffer), 0,
(struct sockaddr *)&their_addr,
(socklen_t) sizeof (their_addr));
#else
socklen_t their_len;
res = recvfrom(sockfd, buffer, strlen(buffer), 0,
(struct sockaddr *)&their_addr,
&their_len);
if (their_addr.sin6_addr.uint8[15] != 1) {
fprintf(stderr, "Wrong sender address: %d\n", their_addr.sin6_addr.uint8[11]);
return 1;
}
printf("Port: %d\n", their_addr.sin6_port);
#endif
if (res < 0) {
perror("Message error");
return 1;
}
close(sockfd);
printf("All tests successful.\n");
return 0;
}