2011-09-28 16:29:01 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <malloc.h>
|
2012-01-30 22:44:38 +01:00
|
|
|
#include <stdint.h>
|
2011-09-28 16:29:01 +02:00
|
|
|
|
|
|
|
#include <posix_io.h>
|
|
|
|
#include <shell.h>
|
|
|
|
#include <board_uart0.h>
|
2012-01-24 03:19:11 +01:00
|
|
|
#include "hwtimer.h"
|
2011-09-28 16:29:01 +02:00
|
|
|
#include <vtimer.h>
|
|
|
|
#include <ltc4150.h>
|
|
|
|
#include <thread.h>
|
|
|
|
#include <cc110x_ng.h>
|
|
|
|
#include <transceiver.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <rtc.h>
|
|
|
|
#include "sys/net/sixlowpan/sixlowmac.h"
|
|
|
|
#include "sys/net/sixlowpan/sixlowip.h"
|
|
|
|
#include "sys/net/sixlowpan/sixlowborder.h"
|
|
|
|
#include "sys/net/sixlowpan/sixlowpan.h"
|
|
|
|
#include "sys/net/sixlowpan/sixlowerror.h"
|
2012-02-10 05:20:01 +01:00
|
|
|
#include "sys/net/sixlowpan/sixlownd.h"
|
2011-09-28 16:29:01 +02:00
|
|
|
#include "sys/net/destiny/udp.h"
|
2011-10-13 04:31:07 +02:00
|
|
|
#include "sys/net/destiny/tcp.h"
|
2011-09-28 16:29:01 +02:00
|
|
|
#include "sys/net/destiny/socket.h"
|
|
|
|
#include "sys/net/destiny/in.h"
|
|
|
|
#include "sys/net/destiny/destiny.h"
|
2012-01-24 03:19:11 +01:00
|
|
|
#include "sys/net/destiny/tcp_timer.h"
|
2011-10-13 04:31:07 +02:00
|
|
|
#include "sys/net/net_help/net_help.h"
|
2012-01-24 03:19:11 +01:00
|
|
|
#include "sys/net/net_help/msg_help.h"
|
2011-09-28 16:29:01 +02:00
|
|
|
|
2012-02-15 19:48:44 +01:00
|
|
|
#define SEND_TCP_THREAD_SIZE 1536
|
|
|
|
#define TCP_CLOSE_THREAD_STACK_SIZE 1536
|
|
|
|
#define RECV_FROM_TCP_THREAD_STACK_SIZE1 512
|
|
|
|
#define RECV_FROM_TCP_THREAD_STACK_SIZE2 512
|
2012-01-30 22:44:38 +01:00
|
|
|
|
2011-10-13 04:31:07 +02:00
|
|
|
uint8_t udp_server_thread_pid;
|
|
|
|
char udp_server_stack_buffer[UDP_STACK_SIZE];
|
2011-09-28 16:29:01 +02:00
|
|
|
|
2011-10-13 04:31:07 +02:00
|
|
|
uint8_t tcp_server_thread_pid;
|
|
|
|
char tcp_server_stack_buffer[TCP_STACK_SIZE];
|
2011-09-28 16:29:01 +02:00
|
|
|
|
2011-11-01 05:47:28 +01:00
|
|
|
uint8_t tcp_cht_pid;
|
|
|
|
char tcp_cht_stack_buffer[TCP_STACK_SIZE];
|
|
|
|
|
2012-02-01 21:07:57 +01:00
|
|
|
// Socket ID used for sending/receiving packets via different threads
|
2012-01-27 02:54:59 +01:00
|
|
|
int tcp_socket_id = -1;
|
2012-01-30 22:44:38 +01:00
|
|
|
|
2012-02-01 21:07:57 +01:00
|
|
|
uint8_t tcp_send_pid = -1;
|
2012-01-30 22:44:38 +01:00
|
|
|
char tcp_send_stack_buffer[SEND_TCP_THREAD_SIZE];
|
2012-02-01 21:07:57 +01:00
|
|
|
|
|
|
|
char tcp_close_thread_stack[TCP_CLOSE_THREAD_STACK_SIZE];
|
2012-01-27 02:54:59 +01:00
|
|
|
|
2012-02-15 19:48:44 +01:00
|
|
|
char recv_from_tcp_thread_stack1[RECV_FROM_TCP_THREAD_STACK_SIZE1];
|
|
|
|
char recv_from_tcp_thread_stack2[RECV_FROM_TCP_THREAD_STACK_SIZE2];
|
|
|
|
|
2012-02-16 22:56:25 +01:00
|
|
|
#ifdef DBG_IGNORE
|
2012-02-09 01:20:49 +01:00
|
|
|
static msg_t mesg;
|
|
|
|
static transceiver_command_t tcmd;
|
2012-02-16 22:56:25 +01:00
|
|
|
#endif
|
2012-02-09 01:20:49 +01:00
|
|
|
|
2012-02-15 19:48:44 +01:00
|
|
|
static uint8_t running_recv_threads = 0;
|
|
|
|
static uint8_t recv_socket_id1 = 0;
|
|
|
|
static uint8_t recv_socket_id2 = 0;
|
|
|
|
|
2011-11-01 21:04:28 +01:00
|
|
|
typedef struct tcp_msg_t
|
2011-11-01 05:47:28 +01:00
|
|
|
{
|
|
|
|
int node_number;
|
2012-02-07 04:24:00 +01:00
|
|
|
char tcp_string_msg[80];
|
2011-11-01 05:47:28 +01:00
|
|
|
}tcp_message_t;
|
|
|
|
tcp_message_t current_message;
|
2011-10-28 04:37:12 +02:00
|
|
|
|
2012-02-15 19:48:44 +01:00
|
|
|
void recv_from_tcp_thread1 (void);
|
|
|
|
void recv_from_tcp_thread2 (void);
|
|
|
|
|
2011-10-13 04:31:07 +02:00
|
|
|
void init_tl (char *str)
|
|
|
|
{
|
|
|
|
init_transport_layer();
|
|
|
|
}
|
|
|
|
|
2011-11-01 05:47:28 +01:00
|
|
|
void tcp_ch(void)
|
2011-10-28 04:37:12 +02:00
|
|
|
{
|
2012-01-27 02:54:59 +01:00
|
|
|
msg_t recv_msg;
|
|
|
|
int read_bytes = 0;
|
|
|
|
char buff_msg[MAX_TCP_BUFFER];
|
2011-11-02 02:55:38 +01:00
|
|
|
sockaddr6_t stSockAddr;
|
2011-11-11 03:41:08 +01:00
|
|
|
msg_receive(&recv_msg);
|
|
|
|
int SocketFD = socket(PF_INET6, SOCK_STREAM, IPPROTO_TCP);
|
|
|
|
if (-1 == SocketFD)
|
|
|
|
{
|
|
|
|
printf("cannot create socket");
|
2012-01-24 03:19:11 +01:00
|
|
|
return;
|
2011-11-11 03:41:08 +01:00
|
|
|
}
|
|
|
|
memset(&stSockAddr, 0, sizeof(stSockAddr));
|
2012-02-01 21:07:57 +01:00
|
|
|
|
2011-11-11 03:41:08 +01:00
|
|
|
stSockAddr.sin6_family = AF_INET6;
|
|
|
|
stSockAddr.sin6_port = HTONS(1100);
|
2012-02-01 21:07:57 +01:00
|
|
|
|
2011-11-11 03:41:08 +01:00
|
|
|
ipv6_init_address(&stSockAddr.sin6_addr, 0xabcd, 0x0, 0x0, 0x0, 0x3612, 0x00ff, 0xfe00, current_message.node_number);
|
|
|
|
ipv6_print_addr(&stSockAddr.sin6_addr);
|
2012-02-01 21:07:57 +01:00
|
|
|
|
2012-01-27 02:54:59 +01:00
|
|
|
if (-1 == connect(SocketFD, &stSockAddr, sizeof(stSockAddr)))
|
2011-11-11 03:41:08 +01:00
|
|
|
{
|
2012-01-24 03:19:11 +01:00
|
|
|
printf("Connect failed!\n");
|
2011-11-11 03:41:08 +01:00
|
|
|
close(SocketFD);
|
2012-01-24 03:19:11 +01:00
|
|
|
return;
|
2011-11-11 03:41:08 +01:00
|
|
|
}
|
2012-01-27 02:54:59 +01:00
|
|
|
tcp_socket_id = SocketFD;
|
|
|
|
while (read_bytes != -1)
|
2011-11-11 03:41:08 +01:00
|
|
|
{
|
2012-01-27 02:54:59 +01:00
|
|
|
read_bytes = recv(SocketFD, buff_msg, MAX_TCP_BUFFER, 0);
|
2012-01-30 22:44:38 +01:00
|
|
|
if (read_bytes > 0)
|
|
|
|
{
|
2012-02-08 04:46:54 +01:00
|
|
|
printf("--- Message: %s ---\n", buff_msg);
|
2012-01-30 22:44:38 +01:00
|
|
|
}
|
2011-10-28 04:37:12 +02:00
|
|
|
|
2011-11-11 03:41:08 +01:00
|
|
|
}
|
2011-10-28 04:37:12 +02:00
|
|
|
}
|
|
|
|
|
2011-10-13 04:31:07 +02:00
|
|
|
void init_udp_server(void)
|
|
|
|
{
|
2011-11-02 02:55:38 +01:00
|
|
|
sockaddr6_t sa;
|
2011-10-13 04:31:07 +02:00
|
|
|
char buffer_main[256];
|
|
|
|
ssize_t recsize;
|
|
|
|
uint32_t fromlen;
|
|
|
|
int sock = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
|
|
|
|
memset(&sa, 0, sizeof sa);
|
2012-02-01 21:07:57 +01:00
|
|
|
|
2011-10-13 04:31:07 +02:00
|
|
|
sa.sin6_family = AF_INET;
|
2011-10-28 04:37:12 +02:00
|
|
|
sa.sin6_port = HTONS(7654);
|
2012-02-01 21:07:57 +01:00
|
|
|
|
2011-10-13 04:31:07 +02:00
|
|
|
fromlen = sizeof(sa);
|
2012-01-27 02:54:59 +01:00
|
|
|
if (-1 == bind(sock, &sa, sizeof(sa)))
|
2011-10-13 04:31:07 +02:00
|
|
|
{
|
|
|
|
printf("Error bind failed!\n");
|
|
|
|
close(sock);
|
|
|
|
}
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
recsize = recvfrom(sock, (void *)buffer_main, 256, 0, &sa, &fromlen);
|
|
|
|
if (recsize < 0)
|
|
|
|
{
|
|
|
|
printf("ERROR: recsize < 0!\n");
|
|
|
|
}
|
2012-02-01 21:07:57 +01:00
|
|
|
printf("recsize: %i\n ", recsize);
|
|
|
|
printf("datagram: %s\n", buffer_main);
|
2011-10-13 04:31:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void init_tcp_server(void)
|
|
|
|
{
|
2011-11-02 02:55:38 +01:00
|
|
|
sockaddr6_t stSockAddr;
|
2012-01-30 22:44:38 +01:00
|
|
|
int read_bytes;
|
2011-11-25 01:31:54 +01:00
|
|
|
char buff_msg[MAX_TCP_BUFFER];
|
2012-02-07 04:24:00 +01:00
|
|
|
memset(buff_msg, 0, MAX_TCP_BUFFER);
|
2011-10-13 04:31:07 +02:00
|
|
|
int SocketFD = socket(PF_INET6, SOCK_STREAM, IPPROTO_TCP);
|
|
|
|
|
|
|
|
if(-1 == SocketFD)
|
|
|
|
{
|
|
|
|
perror("can not create socket");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&stSockAddr, 0, sizeof(stSockAddr));
|
|
|
|
|
|
|
|
stSockAddr.sin6_family = AF_INET6;
|
|
|
|
stSockAddr.sin6_port = HTONS(1100);
|
2012-02-01 21:07:57 +01:00
|
|
|
|
2011-11-01 05:47:28 +01:00
|
|
|
ipv6_init_address(&stSockAddr.sin6_addr, 0xabcd, 0x0, 0x0, 0x0, 0x3612, 0x00ff, 0xfe00, get_radio_address());
|
|
|
|
ipv6_print_addr(&stSockAddr.sin6_addr);
|
2011-10-13 04:31:07 +02:00
|
|
|
|
2012-01-27 02:54:59 +01:00
|
|
|
if(-1 == bind(SocketFD, &stSockAddr, sizeof(stSockAddr)))
|
2011-10-13 04:31:07 +02:00
|
|
|
{
|
2012-01-30 22:44:38 +01:00
|
|
|
printf("error bind failed\n");
|
2011-10-13 04:31:07 +02:00
|
|
|
close(SocketFD);
|
2012-01-30 22:44:38 +01:00
|
|
|
return;
|
2011-10-13 04:31:07 +02:00
|
|
|
}
|
2011-11-25 01:31:54 +01:00
|
|
|
print_internal_socket(getSocket(SocketFD));
|
2011-10-13 04:31:07 +02:00
|
|
|
if(-1 == listen(SocketFD, 10))
|
|
|
|
{
|
2012-01-30 22:44:38 +01:00
|
|
|
printf("error listen failed\n");
|
2012-01-28 16:15:09 +01:00
|
|
|
close(SocketFD);
|
2012-01-30 22:44:38 +01:00
|
|
|
return;
|
2012-01-28 16:15:09 +01:00
|
|
|
}
|
2012-01-30 22:44:38 +01:00
|
|
|
while (1)
|
2012-01-28 16:15:09 +01:00
|
|
|
{
|
2012-01-30 22:44:38 +01:00
|
|
|
read_bytes = 0;
|
|
|
|
printf("INFO: WAITING FOR INC CONNECTIONS!\n");
|
|
|
|
int ConnectFD = accept(SocketFD, NULL, 0);
|
|
|
|
if(0 > ConnectFD)
|
|
|
|
{
|
|
|
|
printf("error accept failed\n");
|
|
|
|
close(SocketFD);
|
|
|
|
return;
|
|
|
|
}
|
2012-02-15 19:48:44 +01:00
|
|
|
else
|
2012-01-30 22:44:38 +01:00
|
|
|
{
|
2012-02-15 19:48:44 +01:00
|
|
|
printf("Connection established on socket %u.\n", ConnectFD);
|
|
|
|
if (running_recv_threads == 0)
|
2012-01-30 22:44:38 +01:00
|
|
|
{
|
2012-02-15 19:48:44 +01:00
|
|
|
recv_socket_id1 = ConnectFD;
|
|
|
|
thread_create(recv_from_tcp_thread_stack1, RECV_FROM_TCP_THREAD_STACK_SIZE1, PRIORITY_MAIN,
|
|
|
|
CREATE_STACKTEST, recv_from_tcp_thread1, "recv_from_tcp_thread1");
|
2012-01-30 22:44:38 +01:00
|
|
|
}
|
2012-02-15 19:48:44 +01:00
|
|
|
else if (running_recv_threads == 1)
|
|
|
|
{
|
|
|
|
recv_socket_id2 = ConnectFD;
|
|
|
|
thread_create(recv_from_tcp_thread_stack2, RECV_FROM_TCP_THREAD_STACK_SIZE2, PRIORITY_MAIN,
|
|
|
|
CREATE_STACKTEST, recv_from_tcp_thread2, "recv_from_tcp_thread2");
|
|
|
|
}
|
|
|
|
running_recv_threads++;
|
2012-01-30 22:44:38 +01:00
|
|
|
}
|
2012-01-28 16:15:09 +01:00
|
|
|
}
|
2011-10-13 04:31:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void init_udp_server_thread(char *str)
|
|
|
|
{
|
|
|
|
udp_server_thread_pid = thread_create(udp_server_stack_buffer, UDP_STACK_SIZE, PRIORITY_MAIN, CREATE_STACKTEST, init_udp_server, "init_udp_server");
|
|
|
|
printf("UDP SERVER THREAD PID: %i\n", udp_server_thread_pid);
|
|
|
|
}
|
|
|
|
|
|
|
|
void init_tcp_server_thread(char *str)
|
|
|
|
{
|
|
|
|
tcp_server_thread_pid = thread_create(tcp_server_stack_buffer, TCP_STACK_SIZE, PRIORITY_MAIN, CREATE_STACKTEST, init_tcp_server, "init_tcp_server");
|
|
|
|
printf("TCP SERVER THREAD PID: %i\n", tcp_server_thread_pid);
|
|
|
|
}
|
|
|
|
|
2011-11-01 05:47:28 +01:00
|
|
|
// Init TCP connection handler thread
|
|
|
|
void init_tcp_cht(char *str)
|
|
|
|
{
|
|
|
|
tcp_cht_pid = thread_create( tcp_cht_stack_buffer,
|
|
|
|
TCP_STACK_SIZE,
|
|
|
|
PRIORITY_MAIN,
|
|
|
|
CREATE_STACKTEST,
|
|
|
|
tcp_ch,
|
2012-01-24 03:19:11 +01:00
|
|
|
"init_conn_handler");
|
2011-11-01 05:47:28 +01:00
|
|
|
printf("TCP CONNECTION HANDLER THREAD PID: %i\n", tcp_cht_pid);
|
|
|
|
}
|
|
|
|
|
2012-01-27 02:54:59 +01:00
|
|
|
void send_tcp_thread (void)
|
|
|
|
{
|
|
|
|
msg_t recv_msg, send_msg;
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
msg_receive(&recv_msg);
|
|
|
|
if (send(tcp_socket_id, (void*) current_message.tcp_string_msg, strlen(current_message.tcp_string_msg)+1, 0) < 0)
|
|
|
|
{
|
|
|
|
printf("Could not send %s!\n", current_message.tcp_string_msg);
|
|
|
|
}
|
2012-02-12 04:26:55 +01:00
|
|
|
// printf("Finished sending!\n");
|
2012-01-27 02:54:59 +01:00
|
|
|
msg_reply(&recv_msg, &send_msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-27 05:31:52 +01:00
|
|
|
void send_tcp_msg(char *str)
|
2011-10-13 04:31:07 +02:00
|
|
|
{
|
2012-01-24 03:19:11 +01:00
|
|
|
msg_t send_msg, recv_msg;
|
2011-11-11 03:41:08 +01:00
|
|
|
sscanf(str, "send_tcp %s", current_message.tcp_string_msg);
|
2012-02-07 04:39:28 +01:00
|
|
|
// printf("Message: %s\n", current_message.tcp_string_msg);
|
2012-01-03 02:16:43 +01:00
|
|
|
if (strcmp(current_message.tcp_string_msg, "close") == 0)
|
|
|
|
{
|
|
|
|
send_msg.content.value = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
send_msg.content.value = 1;
|
2012-01-24 03:19:11 +01:00
|
|
|
}
|
2012-01-27 02:54:59 +01:00
|
|
|
msg_send_receive(&send_msg, &recv_msg, tcp_send_pid);
|
2012-01-24 03:19:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void send_tcp_bulk(char *str)
|
|
|
|
{
|
|
|
|
int i = 0, count;
|
|
|
|
char command[61];
|
|
|
|
char msg_string[50];
|
|
|
|
sscanf(str, "send_tcp_bulk %i %s", &count, msg_string);
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
{
|
2012-02-07 04:24:00 +01:00
|
|
|
sprintf(command, "send_tcp %s%.5i", msg_string, i);
|
2012-01-24 03:19:11 +01:00
|
|
|
send_tcp_msg(command);
|
2012-01-03 02:16:43 +01:00
|
|
|
}
|
2011-11-11 03:41:08 +01:00
|
|
|
}
|
|
|
|
|
2012-01-30 22:44:38 +01:00
|
|
|
void send_tcp_bandwidth_test(char *str)
|
|
|
|
{
|
|
|
|
timex_t start, end, total;
|
2012-02-14 01:37:06 +01:00
|
|
|
double secs;
|
2012-01-30 22:44:38 +01:00
|
|
|
|
|
|
|
int i = 0, count;
|
2012-02-07 04:24:00 +01:00
|
|
|
char command[80];
|
2012-02-14 21:28:51 +01:00
|
|
|
// char msg_string[] = "abcdefghijklmnopqrstuvwxyz0123456789!-";
|
2012-02-07 04:24:00 +01:00
|
|
|
char msg_string[] = "abcdefghijklmnopqrstuvwxyz0123456789!-=/%$";
|
2012-01-30 22:44:38 +01:00
|
|
|
|
|
|
|
sscanf(str, "tcp_bw %i", &count);
|
2012-02-16 22:56:25 +01:00
|
|
|
ltc4150_start();
|
|
|
|
printf("Start power: %f\n", ltc4150_get_total_mAh());
|
2012-01-30 22:44:38 +01:00
|
|
|
start = vtimer_now();
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
{
|
2012-02-07 04:24:00 +01:00
|
|
|
sprintf(command, "send_tcp %s%.5i", msg_string, i);
|
2012-01-30 22:44:38 +01:00
|
|
|
send_tcp_msg(command);
|
|
|
|
}
|
|
|
|
end = vtimer_now();
|
|
|
|
total = timex_sub(end, start);
|
2012-02-14 01:37:06 +01:00
|
|
|
secs = total.microseconds / 1000000.0f;
|
2012-02-20 04:25:52 +01:00
|
|
|
printf("Used power: %f\n", ltc4150_get_total_Joule());
|
2012-01-30 22:44:38 +01:00
|
|
|
printf("Start: %lu, End: %lu, Total: %lu\n", start.microseconds, end.microseconds, total.microseconds);
|
2012-02-14 01:37:06 +01:00
|
|
|
printf("Time: %f seconds, Bandwidth: %f byte/second\n", secs, (count*48)/secs);
|
2012-01-30 22:44:38 +01:00
|
|
|
}
|
|
|
|
|
2011-11-11 03:41:08 +01:00
|
|
|
void connect_tcp(char *str)
|
|
|
|
{
|
|
|
|
msg_t send_msg;
|
|
|
|
sscanf(str, "connect_tcp %i", ¤t_message.node_number);
|
|
|
|
send_msg.content.value = 1;
|
|
|
|
msg_send(&send_msg, tcp_cht_pid, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void disconnect_tcp(char *str)
|
|
|
|
{
|
|
|
|
msg_t send_msg;
|
2011-11-01 05:47:28 +01:00
|
|
|
|
2011-11-11 03:41:08 +01:00
|
|
|
send_msg.content.value = 0;
|
|
|
|
msg_send(&send_msg, tcp_cht_pid, 0);
|
2011-10-13 04:31:07 +02:00
|
|
|
}
|
2011-09-28 16:29:01 +02:00
|
|
|
|
2012-02-15 19:48:44 +01:00
|
|
|
void recv_from_tcp_thread2 (void)
|
|
|
|
{
|
|
|
|
int read_bytes = 0;
|
|
|
|
char buff_msg[MAX_TCP_BUFFER];
|
|
|
|
memset(buff_msg, 0, MAX_TCP_BUFFER);
|
|
|
|
|
|
|
|
while (read_bytes != -1)
|
|
|
|
{
|
|
|
|
read_bytes = recv(recv_socket_id2, buff_msg, MAX_TCP_BUFFER, 0);
|
|
|
|
|
|
|
|
if (read_bytes > 0)
|
|
|
|
{
|
2012-02-20 04:25:52 +01:00
|
|
|
// printf("--- Read bytes: %i, Strlen(): %i, Message: %s ---\n", read_bytes, strlen(buff_msg), buff_msg);
|
2012-02-15 19:48:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void recv_from_tcp_thread1 (void)
|
|
|
|
{
|
|
|
|
int read_bytes = 0;
|
|
|
|
char buff_msg[MAX_TCP_BUFFER];
|
|
|
|
memset(buff_msg, 0, MAX_TCP_BUFFER);
|
|
|
|
|
|
|
|
while (read_bytes != -1)
|
|
|
|
{
|
|
|
|
read_bytes = recv(recv_socket_id1, buff_msg, MAX_TCP_BUFFER, 0);
|
|
|
|
|
|
|
|
if (read_bytes > 0)
|
|
|
|
{
|
2012-02-20 04:25:52 +01:00
|
|
|
// printf("--- Read bytes: %i, Strlen(): %i, Message: %s ---\n", read_bytes, strlen(buff_msg), buff_msg);
|
2012-02-15 19:48:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-28 16:29:01 +02:00
|
|
|
void init(char *str){
|
|
|
|
char command;
|
|
|
|
uint16_t r_addr;
|
|
|
|
ipv6_addr_t std_addr;
|
|
|
|
|
|
|
|
int res = sscanf(str, "init %c %hu", &command, &r_addr);
|
|
|
|
|
|
|
|
if(res < 1){
|
|
|
|
printf("Usage: init {h | r | a | e} radio_address\n");
|
|
|
|
printf("\th\tinitialize as host\n");
|
|
|
|
printf("\tr\tinitialize as router\n");
|
|
|
|
printf("\ta\tinitialize as ad-hoc router\n");
|
|
|
|
printf("\tb\tinitialize as border router\n\n");
|
|
|
|
printf("\tradio_address must be an 8 bit integer\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
ipv6_init_address(&std_addr,0xABCD,0,0,0,0x1034,0x00FF,0xFE00,r_addr);
|
|
|
|
|
|
|
|
switch (command) {
|
|
|
|
case 'h':
|
|
|
|
printf("INFO: Initialize as host on radio address %hu\n", r_addr);
|
|
|
|
if (r_addr > 255) {
|
|
|
|
printf("ERROR: radio_address not an 8 bit integer\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
sixlowpan_init(TRANSCEIVER_CC1100,r_addr,0);
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
printf("INFO: Initialize as router on radio address %hu\n", r_addr);
|
|
|
|
if (r_addr > 255) {
|
|
|
|
printf("ERROR: radio_address not an 8 bit integer\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
sixlowpan_init(TRANSCEIVER_CC1100, r_addr,0);
|
|
|
|
ipv6_init_iface_as_router();
|
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
printf("INFO: Initialize as adhoc router on radio address %hu\n", r_addr);
|
|
|
|
if (r_addr > 255) {
|
|
|
|
printf("ERROR: radio_address not an 8 bit integer\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
sixlowpan_adhoc_init(TRANSCEIVER_CC1100, &std_addr, r_addr);
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
printf("INFO: Initialize as border router on radio address %hu\n", r_addr);
|
|
|
|
if (r_addr > 255) {
|
|
|
|
printf("ERROR: radio_address not an 8 bit integer\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
res = border_initialize(TRANSCEIVER_CC1100, &std_addr);
|
|
|
|
switch (res) {
|
|
|
|
case (SUCCESS): printf("INFO: Border router initialized.\n"); break;
|
|
|
|
case (SIXLOWERROR_ADDRESS): printf("ERROR: Illegal IP address: ");
|
|
|
|
ipv6_print_addr(&std_addr); break;
|
|
|
|
default: printf("ERROR: Unknown error (%d).\n", res); break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
printf("ERROR: Unknown command '%c'\n", command);
|
|
|
|
break;
|
|
|
|
}
|
2012-01-27 02:54:59 +01:00
|
|
|
tcp_send_pid = thread_create( tcp_send_stack_buffer,
|
2012-01-30 22:44:38 +01:00
|
|
|
SEND_TCP_THREAD_SIZE,
|
2012-01-27 02:54:59 +01:00
|
|
|
PRIORITY_MAIN,
|
|
|
|
CREATE_STACKTEST,
|
|
|
|
send_tcp_thread,
|
|
|
|
"send_tcp_thread");
|
2011-09-28 16:29:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void bootstrapping(char *str){
|
|
|
|
sixlowpan_bootstrapping();
|
|
|
|
}
|
|
|
|
|
|
|
|
void send_udp(char *str)
|
|
|
|
{
|
2012-01-30 22:44:38 +01:00
|
|
|
timex_t start, end, total;
|
2012-02-13 23:31:17 +01:00
|
|
|
long secs;
|
2011-09-28 16:29:01 +02:00
|
|
|
int sock;
|
2011-11-02 02:55:38 +01:00
|
|
|
sockaddr6_t sa;
|
2011-09-28 16:29:01 +02:00
|
|
|
ipv6_addr_t ipaddr;
|
|
|
|
int bytes_sent;
|
2012-01-30 22:44:38 +01:00
|
|
|
int address, count;
|
2012-02-20 04:25:52 +01:00
|
|
|
char text[] = "abcdefghijklmnopqrstuvwxyz0123456789!-=$%&/()";
|
|
|
|
sscanf(str, "send_udp %i %i %s", &count, &address, text);
|
2011-09-28 16:29:01 +02:00
|
|
|
|
|
|
|
sock = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
|
|
|
|
if (-1 == sock)
|
|
|
|
{
|
|
|
|
printf("Error Creating Socket!");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&sa, 0, sizeof sa);
|
|
|
|
|
2011-10-13 04:31:07 +02:00
|
|
|
ipv6_init_address(&ipaddr, 0xabcd, 0x0, 0x0, 0x0, 0x3612, 0x00ff, 0xfe00, (uint16_t)address);
|
2011-09-28 16:29:01 +02:00
|
|
|
ipv6_print_addr(&ipaddr);
|
|
|
|
|
|
|
|
sa.sin6_family = AF_INET;
|
|
|
|
memcpy(&sa.sin6_addr, &ipaddr, 16);
|
2011-10-28 04:37:12 +02:00
|
|
|
sa.sin6_port = HTONS(7654);
|
2012-02-16 22:56:25 +01:00
|
|
|
ltc4150_start();
|
2012-02-20 04:25:52 +01:00
|
|
|
printf("Start power: %f\n", ltc4150_get_total_Joule());
|
2012-01-30 22:44:38 +01:00
|
|
|
start = vtimer_now();
|
|
|
|
for (int i = 0; i < count; i++)
|
2011-09-28 16:29:01 +02:00
|
|
|
{
|
2012-01-30 22:44:38 +01:00
|
|
|
bytes_sent = sendto(sock, (char*)text, strlen((char*)text)+1, 0, &sa, sizeof sa);
|
|
|
|
if (bytes_sent < 0)
|
|
|
|
{
|
|
|
|
printf("Error sending packet!\n");
|
|
|
|
}
|
2012-02-20 04:25:52 +01:00
|
|
|
// hwtimer_wait(20*1000);
|
2011-09-28 16:29:01 +02:00
|
|
|
}
|
2012-01-30 22:44:38 +01:00
|
|
|
end = vtimer_now();
|
|
|
|
total = timex_sub(end, start);
|
2012-02-13 23:31:17 +01:00
|
|
|
secs = total.microseconds / 1000000;
|
2012-02-20 04:25:52 +01:00
|
|
|
printf("Used power: %f\n", ltc4150_get_total_Joule());
|
2012-01-30 22:44:38 +01:00
|
|
|
printf("Start: %lu, End: %lu, Total: %lu\n", start.microseconds, end.microseconds, total.microseconds);
|
|
|
|
secs = total.microseconds / 1000000;
|
2012-02-13 23:31:17 +01:00
|
|
|
printf("Time: %lu seconds, Bandwidth: %lu byte/second\n", secs, (count*48)/secs);
|
2011-09-28 16:29:01 +02:00
|
|
|
close(sock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_radio_chann(char *str){
|
|
|
|
uint16_t chann;
|
|
|
|
int res = sscanf(str, "set_chann %hu", &chann);
|
|
|
|
if(res < 1){
|
|
|
|
printf("Usage: set_chann [channel]\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
cc110x_set_channel(chann);
|
|
|
|
}
|
|
|
|
|
|
|
|
void get_r_address(char *str){
|
|
|
|
printf("radio: %hu\n", cc110x_get_address());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ip(char *str){
|
|
|
|
ipv6_iface_print_addrs();
|
|
|
|
}
|
|
|
|
|
|
|
|
void context(char *str){
|
|
|
|
uint8_t i;
|
|
|
|
lowpan_context_t *context;
|
|
|
|
|
|
|
|
for(i = 0; i < LOWPAN_CONTEXT_MAX; i++){
|
|
|
|
context = lowpan_context_num_lookup(i);
|
|
|
|
if (context != NULL) {
|
|
|
|
printf("%2d\tLifetime: %5u\tLength: %3d\t",context->num,context->lifetime,context->length);
|
|
|
|
ipv6_print_addr(&(context->prefix));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-01 05:47:28 +01:00
|
|
|
void shows(char *str)
|
|
|
|
{
|
|
|
|
print_sockets();
|
|
|
|
}
|
|
|
|
|
2011-12-26 02:59:58 +01:00
|
|
|
void showReas(char *str)
|
|
|
|
{
|
|
|
|
printReasBuffers();
|
|
|
|
}
|
|
|
|
|
2012-01-24 03:19:11 +01:00
|
|
|
void kill_process(char *str)
|
|
|
|
{
|
|
|
|
msg_t send;
|
|
|
|
int mypid;
|
|
|
|
send.type = RETURNNOW;
|
|
|
|
sscanf(str, "kill_process %i", &mypid);
|
|
|
|
msg_send(&send, mypid, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void continue_process(char *str)
|
|
|
|
{
|
|
|
|
msg_t send;
|
|
|
|
int pid;
|
|
|
|
sscanf(str, "continue_process %i", &pid);
|
|
|
|
send.type = TCP_CONTINUE;
|
|
|
|
msg_send(&send, pid, 0);
|
|
|
|
}
|
|
|
|
|
2012-01-30 22:44:38 +01:00
|
|
|
void close_tcp_thread(void)
|
|
|
|
{
|
|
|
|
close(tcp_socket_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
void close_tcp (char *str)
|
|
|
|
{
|
2012-02-01 21:07:57 +01:00
|
|
|
thread_create(tcp_close_thread_stack, TCP_CLOSE_THREAD_STACK_SIZE, PRIORITY_MAIN,
|
|
|
|
CREATE_STACKTEST, close_tcp_thread, "tcp_close_thread");
|
2012-01-30 22:44:38 +01:00
|
|
|
}
|
|
|
|
|
2012-02-05 00:33:55 +01:00
|
|
|
void boot_server(char *str)
|
|
|
|
{
|
|
|
|
bootstrapping(NULL);
|
|
|
|
vtimer_usleep(1000*1000*2);
|
|
|
|
init_tcp_server_thread(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void boot_client(char *str)
|
|
|
|
{
|
|
|
|
init_tcp_cht(NULL);
|
|
|
|
vtimer_usleep(1000*1000*2);
|
|
|
|
connect_tcp("connect_tcp 2");
|
|
|
|
}
|
|
|
|
|
2012-02-10 05:20:01 +01:00
|
|
|
void show_nbr_cache(char *str)
|
|
|
|
{
|
2012-02-11 04:21:20 +01:00
|
|
|
// print_nbr_cache();
|
2012-02-10 05:20:01 +01:00
|
|
|
}
|
|
|
|
|
2012-02-09 01:20:49 +01:00
|
|
|
#ifdef DBG_IGNORE
|
|
|
|
void ignore(char *addr) {
|
|
|
|
uint16_t a;
|
|
|
|
mesg.type = DBG_IGN;
|
|
|
|
mesg.content.ptr = (char*) &tcmd;
|
|
|
|
|
|
|
|
tcmd.transceivers = TRANSCEIVER_CC1100;
|
|
|
|
tcmd.data = &a;
|
|
|
|
if (sscanf(addr, "ign %hu", &a) == 1) {
|
|
|
|
msg_send(&mesg, transceiver_pid, 1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
puts("Usage:\tign <addr>");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-02-20 04:25:52 +01:00
|
|
|
/* HACK: Simple routing on IP layer:
|
2012-02-14 21:28:51 +01:00
|
|
|
*
|
2012-02-20 04:25:52 +01:00
|
|
|
* This routing method is used to forward IP packets over N hops in 2 directions.
|
2012-02-14 21:28:51 +01:00
|
|
|
*
|
|
|
|
* Example: A <--> B <--> C <--> D (N = 4)
|
|
|
|
*
|
|
|
|
* To achieve the network topology described in the example above one has to
|
2012-02-20 04:25:52 +01:00
|
|
|
* declare the nodes A to D as "static_routes" and assign them radio addresses in ascending or descending order
|
|
|
|
* without gaps (ie 2-3-4-5 is OK, 2-3-5-6 is NOT OK).
|
2012-02-14 21:28:51 +01:00
|
|
|
*
|
2012-02-20 04:25:52 +01:00
|
|
|
* The variable which needs to be set in every node is static_route in sys/net/sixlowpan/sixlowpan.c. */
|
|
|
|
|
2012-02-14 21:28:51 +01:00
|
|
|
void static_routing (char *str)
|
|
|
|
{
|
|
|
|
if (static_route == 0)
|
|
|
|
{
|
|
|
|
static_route = 1;
|
2012-02-20 04:25:52 +01:00
|
|
|
printf("Static Routing: TRUE\n");
|
2012-02-14 21:28:51 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
static_route = 0;
|
2012-02-20 04:25:52 +01:00
|
|
|
printf("Static Routing: FALSE\n");
|
2012-02-14 21:28:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-20 04:25:52 +01:00
|
|
|
void print_fragment_counter (char *str)
|
2012-02-14 21:28:51 +01:00
|
|
|
{
|
2012-02-20 04:25:52 +01:00
|
|
|
printf("Fragment Counter: %u\n", fragmentcounter);
|
|
|
|
}
|
|
|
|
|
|
|
|
void pfifo_buf (char *str)
|
|
|
|
{
|
|
|
|
printFIFOBuffers();
|
|
|
|
}
|
|
|
|
|
|
|
|
void sleep_now(char *str)
|
|
|
|
{
|
|
|
|
int time;
|
|
|
|
sscanf(str, "sleep %i", &time);
|
|
|
|
vtimer_usleep(time*1000*1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
void get_rtt (char *str)
|
|
|
|
{
|
|
|
|
int socket;
|
|
|
|
sscanf(str, "get_rtt %i", &socket);
|
|
|
|
printf("SRTT: %f, RTO: %f, RTTVAR: %f\n", getSocket(socket)->socket_values.tcp_control.srtt,
|
|
|
|
getSocket(socket)->socket_values.tcp_control.rto,
|
|
|
|
getSocket(socket)->socket_values.tcp_control.rttvar);
|
2012-02-14 21:28:51 +01:00
|
|
|
}
|
|
|
|
|
2011-09-28 16:29:01 +02:00
|
|
|
const shell_command_t shell_commands[] = {
|
|
|
|
{"init", "", init},
|
|
|
|
{"addr", "", get_r_address},
|
|
|
|
{"set_chann", "", set_radio_chann},
|
|
|
|
{"boot", "", bootstrapping},
|
|
|
|
{"ip", "", ip},
|
2011-12-26 02:59:58 +01:00
|
|
|
{"shows", "Show Sockets", shows},
|
|
|
|
{"show_reas", "Show reassembly Buffers", showReas},
|
2011-09-28 16:29:01 +02:00
|
|
|
{"context", "", context},
|
2011-10-13 04:31:07 +02:00
|
|
|
{"init_udp_server_thread", "", init_udp_server_thread},
|
|
|
|
{"init_tcp_server_thread", "", init_tcp_server_thread},
|
2011-11-01 05:47:28 +01:00
|
|
|
{"init_tcp_cht", "", init_tcp_cht},
|
2011-11-11 03:41:08 +01:00
|
|
|
{"connect_tcp", "", connect_tcp},
|
2011-12-27 05:31:52 +01:00
|
|
|
{"send_tcp", "", send_tcp_msg},
|
2011-09-28 16:29:01 +02:00
|
|
|
{"send_udp", "", send_udp},
|
2012-01-24 03:19:11 +01:00
|
|
|
{"send_tcp_bulk", "send_tcp_bulk NO_OF_PACKETS PAYLOAD", send_tcp_bulk},
|
|
|
|
{"kill_process", "", kill_process},
|
|
|
|
{"continue_process", "", continue_process},
|
2012-01-30 22:44:38 +01:00
|
|
|
{"close_tcp", "", close_tcp},
|
|
|
|
{"tcp_bw", "tcp_bw NO_OF_PACKETS", send_tcp_bandwidth_test},
|
2012-02-05 00:33:55 +01:00
|
|
|
{"boots", "", boot_server},
|
|
|
|
{"bootc", "", boot_client},
|
2012-02-10 05:20:01 +01:00
|
|
|
{"print_nbr_cache", "", show_nbr_cache},
|
2012-02-14 21:28:51 +01:00
|
|
|
{"static_routing", "", static_routing},
|
2012-02-20 04:25:52 +01:00
|
|
|
// {"static_head", "", static_head},
|
|
|
|
{"pfrag", "", print_fragment_counter},
|
|
|
|
{"show_fifo", "", pfifo_buf},
|
|
|
|
{"sleep", "", sleep_now},
|
|
|
|
{"get_rtt", "", get_rtt},
|
2012-02-09 01:20:49 +01:00
|
|
|
#ifdef DBG_IGNORE
|
|
|
|
{"ign", "ignore node", ignore},
|
|
|
|
#endif
|
2011-09-28 16:29:01 +02:00
|
|
|
{NULL, NULL, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
int main(void) {
|
|
|
|
printf("6LoWPAN Transport Layers\n");
|
|
|
|
posix_open(uart0_handler_pid, 0);
|
2012-02-01 21:07:57 +01:00
|
|
|
|
2011-10-13 04:31:07 +02:00
|
|
|
init_tl(NULL);
|
2012-01-30 22:44:38 +01:00
|
|
|
|
2011-09-28 16:29:01 +02:00
|
|
|
shell_t shell;
|
|
|
|
shell_init(&shell, shell_commands, uart0_readc, uart0_putc);
|
2011-10-13 04:31:07 +02:00
|
|
|
|
2011-09-28 16:29:01 +02:00
|
|
|
shell_run(&shell);
|
2011-10-13 04:31:07 +02:00
|
|
|
|
2011-09-28 16:29:01 +02:00
|
|
|
return 0;
|
|
|
|
}
|