mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
91 lines
2.4 KiB
C
91 lines
2.4 KiB
C
/*
|
|
* Copyright (C) 2017 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 Test application demonstrating the simple registration
|
|
* process to a CoRE RD using gcoap
|
|
*
|
|
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
|
*
|
|
* @}
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "shell.h"
|
|
#include "net/gcoap.h"
|
|
#include "net/cord/common.h"
|
|
|
|
#define BUFSIZE (64U)
|
|
|
|
static char riot_info[BUFSIZE];
|
|
|
|
/* define some dummy CoAP resources */
|
|
static ssize_t text_resp(coap_pkt_t *pdu, uint8_t *buf, size_t len,
|
|
const char *text, unsigned format)
|
|
{
|
|
gcoap_resp_init(pdu, buf, len, COAP_CODE_CONTENT);
|
|
coap_opt_add_format(pdu, format);
|
|
ssize_t resp_len = coap_opt_finish(pdu, COAP_OPT_FINISH_PAYLOAD);
|
|
size_t slen = strlen(text);
|
|
memcpy(pdu->payload, text, slen);
|
|
return resp_len + slen;
|
|
}
|
|
|
|
static ssize_t handler_info(coap_pkt_t *pdu, uint8_t *buf, size_t len, void *ctx)
|
|
{
|
|
(void)ctx;
|
|
return text_resp(pdu, buf, len, riot_info, COAP_FORMAT_JSON);
|
|
}
|
|
|
|
static ssize_t handler_text(coap_pkt_t *pdu, uint8_t *buf, size_t len, void *ctx)
|
|
{
|
|
return text_resp(pdu, buf, len, (char *)ctx, COAP_FORMAT_TEXT);
|
|
}
|
|
|
|
static const coap_resource_t resources[] = {
|
|
{ "/riot/bar", COAP_GET, handler_text, "foo" },
|
|
{ "/riot/foo", COAP_GET, handler_text, "bar" },
|
|
{ "/riot/info", COAP_GET, handler_info, NULL }
|
|
};
|
|
|
|
static gcoap_listener_t listener = {
|
|
.resources = &resources[0],
|
|
.resources_len = sizeof(resources) / sizeof(resources[0]),
|
|
.next = NULL
|
|
};
|
|
|
|
int main(void)
|
|
{
|
|
puts("CoAP simplified RD registration example!\n");
|
|
|
|
/* fill riot info */
|
|
sprintf(riot_info, "{\"ep\":\"%s\",\"lt\":%u}",
|
|
cord_common_get_ep(), CORD_LT);
|
|
|
|
/* register resource handlers with gcoap */
|
|
gcoap_register_listener(&listener);
|
|
|
|
/* print RD client information */
|
|
puts("RD client information:");
|
|
printf(" RD addr: %s\n", CORD_SERVER_ADDR);
|
|
printf(" RD port: %u\n", (unsigned)CORD_SERVER_PORT);
|
|
printf(" ep: %s\n", cord_common_get_ep());
|
|
printf(" lt: %is\n", (int)CORD_LT);
|
|
|
|
/* run the shell */
|
|
char line_buf[SHELL_DEFAULT_BUFSIZE];
|
|
shell_run(NULL, line_buf, SHELL_DEFAULT_BUFSIZE);
|
|
|
|
return 0;
|
|
}
|