mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2025-12-25 06:23:53 +01:00
examples: add example for rdcli
This commit is contained in:
parent
899d7fee8a
commit
c9c7bf8d41
33
examples/rdcli/Makefile
Normal file
33
examples/rdcli/Makefile
Normal file
@ -0,0 +1,33 @@
|
||||
# name of your application
|
||||
APPLICATION = rdcli
|
||||
|
||||
# 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)/../..
|
||||
|
||||
USEMODULE += gnrc_netdev_default
|
||||
USEMODULE += auto_init_gnrc_netif
|
||||
USEMODULE += gnrc_ipv6_default
|
||||
USEMODULE += gnrc_icmpv6_echo
|
||||
|
||||
USEMODULE += rdcli_standalone
|
||||
|
||||
USEMODULE += shell
|
||||
USEMODULE += shell_commands
|
||||
USEMODULE += ps
|
||||
USEMODULE += fmt
|
||||
|
||||
# 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
|
||||
|
||||
# For debugging and demonstration purposes, we limit the lifetime to 60s
|
||||
CFLAGS += -DRDCLI_LT=60
|
||||
|
||||
# Change this to 0 show compiler invocation lines by default:
|
||||
QUIET ?= 1
|
||||
|
||||
include $(RIOTBASE)/Makefile.include
|
||||
20
examples/rdcli/README.md
Normal file
20
examples/rdcli/README.md
Normal file
@ -0,0 +1,20 @@
|
||||
CoRE Resource Directory Client Example
|
||||
======================================
|
||||
This example application demonstrates the usage of RIOT's Resource Directory
|
||||
(RD) client module, called `rdcli`. This module supports the registration,
|
||||
update, and removal processes as defined in the
|
||||
[Resource Directory Draft](https://tools.ietf.org/html/draft-ietf-core-resource-directory-14).
|
||||
|
||||
Usage
|
||||
=====
|
||||
The examples includes a shell command that you can use to interact with a given
|
||||
RD server, called `rdcli`. Simply use that shell command without parameters for
|
||||
more information on its usage.
|
||||
|
||||
Some connection parameters are configured statically during compile time,
|
||||
namely the lifetime (`RDCLI_LT`) and the node's endpoint name (`RDCLI_EP`). You
|
||||
can change these values during command line by overriding these values in the
|
||||
application's Makefile, e.g. add
|
||||
```
|
||||
CFLAGS += "-DRDCLI_EP=\"MyNewEpName\""
|
||||
```
|
||||
108
examples/rdcli/main.c
Normal file
108
examples/rdcli/main.c
Normal file
@ -0,0 +1,108 @@
|
||||
/*
|
||||
* Copyright (C) 2017-2018 Freie Universität Berlin
|
||||
*
|
||||
* 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 CoRE Resource Directory client (rdcli) example application
|
||||
*
|
||||
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
||||
*
|
||||
* @}
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "fmt.h"
|
||||
#include "shell.h"
|
||||
#include "net/ipv6/addr.h"
|
||||
#include "net/gcoap.h"
|
||||
#include "net/rdcli_common.h"
|
||||
#include "net/rdcli_standalone.h"
|
||||
|
||||
#define MAIN_QUEUE_SIZE (8)
|
||||
static msg_t _main_msg_queue[MAIN_QUEUE_SIZE];
|
||||
|
||||
/* we will use a custom event handler for dumping rdcli_standalone events */
|
||||
static void _on_rdcli_event(rdcli_standalone_event_t event)
|
||||
{
|
||||
switch (event) {
|
||||
case RDCLI_REGISTERED:
|
||||
puts("rdcli event: now registered with a RD");
|
||||
break;
|
||||
case RDCLI_DEREGISTERED:
|
||||
puts("rdcli event: dropped client registration");
|
||||
break;
|
||||
case RDCLI_UPDATED:
|
||||
puts("rdcli event: successfully updated client registration");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* define some dummy CoAP resources */
|
||||
static ssize_t _handler_dummy(coap_pkt_t *pdu,
|
||||
uint8_t *buf, size_t len, void *ctx)
|
||||
{
|
||||
(void)ctx;
|
||||
|
||||
/* get random data */
|
||||
int16_t val = 23;
|
||||
|
||||
gcoap_resp_init(pdu, buf, len, COAP_CODE_CONTENT);
|
||||
size_t plen = fmt_s16_dec((char *)pdu->payload, val);
|
||||
return gcoap_finish(pdu, plen, COAP_FORMAT_TEXT);
|
||||
}
|
||||
|
||||
static ssize_t _handler_info(coap_pkt_t *pdu,
|
||||
uint8_t *buf, size_t len, void *ctx)
|
||||
{
|
||||
(void)ctx;
|
||||
|
||||
gcoap_resp_init(pdu, buf, len, COAP_CODE_CONTENT);
|
||||
size_t slen = sizeof("SOME NODE INFOMRATION");
|
||||
memcpy(pdu->payload, "SOME NODE INFOMRATION", slen);
|
||||
return gcoap_finish(pdu, slen, COAP_FORMAT_TEXT);
|
||||
}
|
||||
|
||||
static const coap_resource_t _resources[] = {
|
||||
{ "/node/info", COAP_GET, _handler_info, NULL },
|
||||
{ "/sense/hum", COAP_GET, _handler_dummy, NULL },
|
||||
{ "/sense/temp", COAP_GET, _handler_dummy, NULL }
|
||||
};
|
||||
|
||||
static gcoap_listener_t _listener = {
|
||||
.resources = (coap_resource_t *)&_resources[0],
|
||||
.resources_len = sizeof(_resources) / sizeof(_resources[0]),
|
||||
.next = NULL
|
||||
};
|
||||
|
||||
int main(void)
|
||||
{
|
||||
/* we need a message queue for the thread running the shell in order to
|
||||
* receive potentially fast incoming networking packets */
|
||||
msg_init_queue(_main_msg_queue, MAIN_QUEUE_SIZE);
|
||||
|
||||
puts("CoRE RD client example!\n");
|
||||
|
||||
/* setup CoAP resources */
|
||||
gcoap_register_listener(&_listener);
|
||||
|
||||
/* register event callback with rdcli_standalone */
|
||||
rdcli_standalone_reg_cb(_on_rdcli_event);
|
||||
|
||||
puts("Client information:");
|
||||
printf(" ep: %s\n", rdcli_common_get_ep());
|
||||
printf(" lt: %is\n", (int)RDCLI_LT);
|
||||
|
||||
char line_buf[SHELL_DEFAULT_BUFSIZE];
|
||||
shell_run(NULL, line_buf, SHELL_DEFAULT_BUFSIZE);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user