2018-09-11 15:09:22 +02:00
|
|
|
/*
|
|
|
|
* 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
|
2018-10-15 11:46:38 +02:00
|
|
|
* @brief CoRE Resource Directory endpoint (cord_ep) example
|
2018-09-11 15:09:22 +02:00
|
|
|
*
|
|
|
|
* @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"
|
2018-10-15 10:05:33 +02:00
|
|
|
#include "net/cord/common.h"
|
2018-10-15 11:19:06 +02:00
|
|
|
#include "net/cord/ep_standalone.h"
|
2018-09-11 15:09:22 +02:00
|
|
|
|
|
|
|
#define MAIN_QUEUE_SIZE (8)
|
|
|
|
static msg_t _main_msg_queue[MAIN_QUEUE_SIZE];
|
|
|
|
|
2019-04-16 10:10:11 +02:00
|
|
|
#define NODE_INFO "SOME NODE INFORMATION"
|
|
|
|
|
2018-10-15 11:46:38 +02:00
|
|
|
/* we will use a custom event handler for dumping cord_ep events */
|
|
|
|
static void _on_ep_event(cord_ep_standalone_event_t event)
|
2018-09-11 15:09:22 +02:00
|
|
|
{
|
|
|
|
switch (event) {
|
2018-10-15 11:19:06 +02:00
|
|
|
case CORD_EP_REGISTERED:
|
2018-10-15 11:46:38 +02:00
|
|
|
puts("RD endpoint event: now registered with a RD");
|
2018-09-11 15:09:22 +02:00
|
|
|
break;
|
2018-10-15 11:19:06 +02:00
|
|
|
case CORD_EP_DEREGISTERED:
|
2018-10-15 11:46:38 +02:00
|
|
|
puts("RD endpoint event: dropped client registration");
|
2018-09-11 15:09:22 +02:00
|
|
|
break;
|
2018-10-15 11:19:06 +02:00
|
|
|
case CORD_EP_UPDATED:
|
2018-10-15 11:46:38 +02:00
|
|
|
puts("RD endpoint event: successfully updated client registration");
|
2018-09-11 15:09:22 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* define some dummy CoAP resources */
|
|
|
|
static ssize_t _handler_dummy(coap_pkt_t *pdu,
|
2022-04-30 00:17:47 +02:00
|
|
|
uint8_t *buf, size_t len, coap_request_ctx_t *ctx)
|
2018-09-11 15:09:22 +02:00
|
|
|
{
|
|
|
|
(void)ctx;
|
|
|
|
|
|
|
|
/* get random data */
|
|
|
|
int16_t val = 23;
|
|
|
|
|
|
|
|
gcoap_resp_init(pdu, buf, len, COAP_CODE_CONTENT);
|
2019-02-01 20:10:37 +01:00
|
|
|
size_t resp_len = coap_opt_finish(pdu, COAP_OPT_FINISH_PAYLOAD);
|
|
|
|
resp_len += fmt_s16_dec((char *)pdu->payload, val);
|
|
|
|
return resp_len;
|
2018-09-11 15:09:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t _handler_info(coap_pkt_t *pdu,
|
2022-04-30 00:17:47 +02:00
|
|
|
uint8_t *buf, size_t len, coap_request_ctx_t *ctx)
|
2018-09-11 15:09:22 +02:00
|
|
|
{
|
|
|
|
(void)ctx;
|
|
|
|
|
|
|
|
gcoap_resp_init(pdu, buf, len, COAP_CODE_CONTENT);
|
2019-02-01 20:10:37 +01:00
|
|
|
size_t resp_len = coap_opt_finish(pdu, COAP_OPT_FINISH_PAYLOAD);
|
2019-04-16 10:10:11 +02:00
|
|
|
size_t slen = sizeof(NODE_INFO);
|
|
|
|
memcpy(pdu->payload, NODE_INFO, slen);
|
2019-02-01 20:10:37 +01:00
|
|
|
return resp_len + slen;
|
2018-09-11 15:09:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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],
|
2019-07-18 15:18:32 +02:00
|
|
|
.resources_len = ARRAY_SIZE(_resources),
|
2018-09-11 15:09:22 +02:00
|
|
|
.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);
|
|
|
|
|
2018-10-15 11:19:06 +02:00
|
|
|
/* register event callback with cord_ep_standalone */
|
2018-10-15 11:46:38 +02:00
|
|
|
cord_ep_standalone_reg_cb(_on_ep_event);
|
2018-09-11 15:09:22 +02:00
|
|
|
|
|
|
|
puts("Client information:");
|
2018-10-15 10:05:33 +02:00
|
|
|
printf(" ep: %s\n", cord_common_get_ep());
|
2020-06-17 16:44:36 +02:00
|
|
|
printf(" lt: %is\n", (int)CONFIG_CORD_LT);
|
2018-09-11 15:09:22 +02:00
|
|
|
|
|
|
|
char line_buf[SHELL_DEFAULT_BUFSIZE];
|
|
|
|
shell_run(NULL, line_buf, SHELL_DEFAULT_BUFSIZE);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|