1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00
RIOT/examples/rpl_udp/udp.c
Lucas Jenss 426170b064 Improve naming of thread stacksize/priority constants
As discussed in #2725, this commit renames a number of stacksize constants to
better convey their intended usage. In addition, constants for thread priority
are given a `THREAD_` prefix. Changes are:

* KERNEL_CONF_STACKSIZE_PRINTF renamed to THREAD_EXTRA_STACKSIZE_PRINTF
* KERNEL_CONF_STACKSIZE_DEFAULT renamed to THREAD_STACKSIZE_DEFAULT
* KERNEL_CONF_STACKSIZE_IDLE renamed to THREAD_STACKSIZE_IDLE
* KERNEL_CONF_STACKSIZE_MAIN renamed to THREAD_STACKSIZE_MAIN
* Move thread stacksizes from kernel.h to thread.h, since the prefix changed
* PRIORITY_MIN renamed to THREAD_PRIORITY_MIN
* PRIORITY_IDLE renamed to THREAD_PRIORITY_IDLE
* PRIORITY_MAIN renamed to THREAD_PRIORITY_MAIN
* Move thread priorities from kernel.h to thread.h since the prefix has changed
* MINIMUM_STACK_SIZE renamed to THREAD_STACKSIZE_MINIMUM for consistency
2015-05-21 00:14:23 +02:00

160 lines
3.8 KiB
C

/*
* Copyright (C) 2013, 2014 INRIA
*
* 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 UDP RPL example application
*
* @author Oliver Hahm <oliver.hahm@inria.fr>
*
* @}
*/
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <inttypes.h>
#include "thread.h"
#include "socket_base/socket.h"
#include "net_help.h"
#include "rpl_udp.h"
#define UDP_BUFFER_SIZE (128)
#define SERVER_PORT (0xFF01)
static char udp_server_stack_buffer[THREAD_STACKSIZE_MAIN];
char addr_str[IPV6_MAX_ADDR_STR_LEN];
static void *init_udp_server(void *);
/* UDP server thread */
int udp_server(int argc, char **argv)
{
(void) argc;
(void) argv;
kernel_pid_t udp_server_thread_pid = thread_create(udp_server_stack_buffer,
sizeof(udp_server_stack_buffer),
THREAD_PRIORITY_MAIN, CREATE_STACKTEST,
init_udp_server,
NULL,
"init_udp_server");
printf("UDP SERVER ON PORT %d (THREAD PID: %" PRIkernel_pid ")\n", HTONS(SERVER_PORT), udp_server_thread_pid);
return 0;
}
static void *init_udp_server(void *arg)
{
(void) arg;
sockaddr6_t sa;
char buffer_main[UDP_BUFFER_SIZE];
uint32_t fromlen;
int sock = socket_base_socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
memset(&sa, 0, sizeof(sa));
sa.sin6_family = AF_INET;
sa.sin6_port = HTONS(SERVER_PORT);
fromlen = sizeof(sa);
if (-1 == socket_base_bind(sock, &sa, sizeof(sa))) {
printf("Error bind failed!\n");
socket_base_close(sock);
return NULL;
}
while (1) {
int32_t recsize = socket_base_recvfrom(sock, (void *)buffer_main, UDP_BUFFER_SIZE, 0, &sa, &fromlen);
if (recsize < 0) {
printf("ERROR: recsize < 0!\n");
}
printf("UDP packet received, payload: %s\n", buffer_main);
}
socket_base_close(sock);
return NULL;
}
/* UDP send command */
int udp_send(int argc, char **argv)
{
int sock, res;
sockaddr6_t sa;
ipv6_addr_t ipaddr;
int bytes_sent;
int address;
if (argc != 3) {
printf("usage: send <addr> <text>\n");
return 1;
}
/* max payload size = MTU - MAC - AES - IPV6_HDR_LEN - UDP_HDR_LEN
* 33 = 127 - 25 - 21 - 40 - 8
*/
if (strlen(argv[2]) > 32) {
puts("<text> is too large to be sent (max. 33 characters).");
return 1;
}
address = atoi(argv[1]);
sock = socket_base_socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
if (-1 == sock) {
printf("Error Creating Socket!");
return 1;
}
memset(&sa, 0, sizeof(sa));
if (address) {
ipv6_addr_init(&ipaddr, 0xabcd, 0x0, 0x0, 0x0, 0x0, 0x00ff, 0xfe00, (uint16_t)address);
}
else {
ipv6_addr_set_all_nodes_addr(&ipaddr);
}
sa.sin6_family = AF_INET;
memcpy(&sa.sin6_addr, &ipaddr, 16);
sa.sin6_port = HTONS(SERVER_PORT);
bytes_sent = socket_base_sendto(sock, argv[2],
strlen(argv[2]), 0, &sa,
sizeof(sa));
if (bytes_sent < 0) {
printf("Error sending packet!\n");
res = 1;
}
else {
printf("Successful deliverd %i bytes over UDP to %s to 6LoWPAN\n",
bytes_sent, ipv6_addr_to_str(addr_str, IPV6_MAX_ADDR_STR_LEN,
&ipaddr));
res = 0;
}
socket_base_close(sock);
return res;
}