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

pkg: add yacoap, Yet another CoAP library #5830

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions examples/yacoap_posix/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# name of your application
APPLICATION = yacoap_posix

# If no BOARD is found in the environment, use this default:
BOARD ?= native

# This has to be the absolute path to the RIOT base directory:
RIOTBASE ?= $(CURDIR)/../..

BOARD_INSUFFICIENT_MEMORY := airfy-beacon chronos msb-430 msb-430h nrf51dongle \
nrf6310 pca10000 pca10005 spark-core \
stm32f0discovery telosb weio wsn430-v1_3b wsn430-v1_4 \
yunjia-nrf51822 z1 nucleo-f072

# Include packages that pull up and auto-init the link layer.
# NOTE: 6LoWPAN will be included if IEEE802.15.4 devices are present
USEMODULE += gnrc_netdev_default
USEMODULE += auto_init_gnrc_netif
# Specify the mandatory networking modules for IPv6 and UDP
USEMODULE += gnrc_ipv6_default
USEMODULE += gnrc_udp
USEMODULE += gnrc_conn_udp
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please port to sock (see #6005).

USEMODULE += posix_sockets
USEMODULE += gnrc_icmpv6_echo
USEMODULE += shell
USEMODULE += shell_commands
USEMODULE += ps
# include YaCoAP package
USEPKG += yacoap

# Set a custom 802.15.4 channel if needed
DEFAULT_CHANNEL ?= 26
CFLAGS += -DDEFAULT_CHANNEL=$(DEFAULT_CHANNEL)

# Comment this out to disable code in RIOT that does safety checking
# which is not needed in a production environment but helps in the
# development process:
#CFLAGS += -DDEVELHELP

# Use different settings when compiling for one of the following (low-memory)
# boards
LOW_MEMORY_BOARDS := nucleo-f334

ifneq (,$(filter $(BOARD),$(LOW_MEMORY_BOARDS)))
$(info Using low-memory configuration for microcoap_server.)
## low-memory tuning values
# lower pktbuf buffer size
CFLAGS += -DGNRC_PKTBUF_SIZE=1000
# disable fib, rpl
DISABLE_MODULE += fib gnrc_rpl
USEMODULE += prng_minstd
endif

# Change this to 0 show compiler invocation lines by default:
QUIET ?= 1

include $(RIOTBASE)/Makefile.include
22 changes: 22 additions & 0 deletions examples/yacoap_posix/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# examples/yacoap_client_server

This examples shows basic usage of YaCoAP in RIOT. It shows CoAP client and
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/examples/example/

server side functions using POSIX sockets. However YaCoAP is completely
independent of any underlying send or receive API, its a plain CoAP packet
handler. So POSIX sockets can be replaced easily, e.g. using RIOTs `conn` or
future `sock` API.

## CoAP server part

This example starts a server thread that provides 2 distinct resources, namely:
`/riot/board` and `/riot/mcu`. Both can be accessed using CoAP GET requests.
Further, this example also provide the obligatory `/.well-known/core` in
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/provide/provides/

standard link-format.

## CoAP client part

This example provides 2 CoAP client calls through the RIOT shell, as follows:

* `get <ip>`: retrieving `/.well-known/core` from the specified CoAP server ip
* `put <ip> <path> <content>`: sending a PUT request to the specified CoAP
server creating or modifying resource in `path` with `content`.
88 changes: 88 additions & 0 deletions examples/yacoap_posix/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**
* @ingroup examples
* @{
*
* @file
* @brief YaCoAP example showing CoAP client and server functions
*
* @author smlng <s@mlng.net>
*
* @}
*/

// standard
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// riot
#include "board.h"
#include "periph/gpio.h"
#include "shell.h"

int coap_pid = -1;

extern int coap_start_thread(void);
extern int coap_get(const char *ip);
extern int coap_put(const char *ip, const char *path, const char *content);

static int cmd_get(int argc, char **argv);
static int cmd_put(int argc, char **argv);

// array with available shell commands
static const shell_command_t shell_commands[] = {
{ "get", "get ./well-known/core", cmd_get },
{ "put", "modify a resource", cmd_put },
{ NULL, NULL, NULL }
};

/**
* @brief the main programm loop
*
* @return non zero on error
*/
int main(void)
{
// some initial infos
puts(" YaCoAP example - Yet another CoAP library");
puts("==========================================");
printf("You are running RIOT on a(n) %s board.\n", RIOT_BOARD);
printf("This board features a(n) %s MCU.\n", RIOT_MCU);
puts("==========================================");
LED0_ON;
// start coap receiver
puts(". init coap");
if ((coap_pid = coap_start_thread()) < 0) {
puts("failed to init coap thread!");
return 1;
}
puts(":");
// start shell
puts(". init shell");
puts(":");
LED0_OFF;
char line_buf[SHELL_DEFAULT_BUFSIZE];
shell_run(shell_commands, line_buf, SHELL_DEFAULT_BUFSIZE);
// should be never reached
return 0;
}

int cmd_get(int argc, char **argv)
{
if (argc != 2) {
puts ("USAGE: get <ip>");
return 1;
}
coap_get(argv[1]);
return 0;
}

int cmd_put(int argc, char **argv)
{
if (argc != 4) {
puts("USAGE: put <ip> <path> <content>");
return 1;
}
coap_put(argv[1], argv[2], argv[3]);
return 0;
}
Loading