-
Notifications
You must be signed in to change notification settings - Fork 2k
/
main.c
112 lines (92 loc) · 3.02 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
* 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 endpoint (cord_ep) example
*
* @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/cord/common.h"
#include "net/cord/ep_standalone.h"
#define MAIN_QUEUE_SIZE (8)
static msg_t _main_msg_queue[MAIN_QUEUE_SIZE];
#define NODE_INFO "SOME NODE INFORMATION"
/* we will use a custom event handler for dumping cord_ep events */
static void _on_ep_event(cord_ep_standalone_event_t event)
{
switch (event) {
case CORD_EP_REGISTERED:
puts("RD endpoint event: now registered with a RD");
break;
case CORD_EP_DEREGISTERED:
puts("RD endpoint event: dropped client registration");
break;
case CORD_EP_UPDATED:
puts("RD endpoint 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 resp_len = coap_opt_finish(pdu, COAP_OPT_FINISH_PAYLOAD);
resp_len += fmt_s16_dec((char *)pdu->payload, val);
return resp_len;
}
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 resp_len = coap_opt_finish(pdu, COAP_OPT_FINISH_PAYLOAD);
size_t slen = sizeof(NODE_INFO);
memcpy(pdu->payload, NODE_INFO, slen);
return resp_len + slen;
}
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 = ARRAY_SIZE(_resources),
.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 cord_ep_standalone */
cord_ep_standalone_reg_cb(_on_ep_event);
puts("Client information:");
printf(" ep: %s\n", cord_common_get_ep());
printf(" lt: %is\n", (int)CONFIG_CORD_LT);
char line_buf[SHELL_DEFAULT_BUFSIZE];
shell_run(NULL, line_buf, SHELL_DEFAULT_BUFSIZE);
return 0;
}