-
Notifications
You must be signed in to change notification settings - Fork 24
/
server.cc
123 lines (104 loc) · 4 KB
/
server.cc
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
113
114
115
116
117
118
119
120
121
122
123
/* minimal CoAP server
*
* Copyright (C) 2018-2024 Olaf Bergmann <bergmann@tzi.org>
*/
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include "common.hh"
/*
* This server listens to Unicast CoAP traffic coming in on port 5683 and handles it
* as appropriate.
*
* If support for multicast traffic is not required, comment out the COAP_LISTEN_MCAST_IPV*
* definitions.
*/
#define COAP_LISTEN_UCAST_IP "::"
#define COAP_LISTEN_MCAST_IPV4 "224.0.1.187"
#define COAP_LISTEN_MCAST_IPV6 "ff02::fd"
int
main(void) {
coap_context_t *ctx = nullptr;
coap_resource_t *resource = nullptr;
int result = EXIT_FAILURE;;
uint32_t scheme_hint_bits;
coap_addr_info_t *info = nullptr;
coap_addr_info_t *info_list = nullptr;
coap_str_const_t *my_address = coap_make_str_const(COAP_LISTEN_UCAST_IP);
bool have_ep = false;
/* Initialize libcoap library */
coap_startup();
/* Set logging level */
coap_set_log_level(COAP_LOG_WARN);
/* Create CoAP context */
ctx = coap_new_context(nullptr);
if (!ctx) {
coap_log_emerg("cannot initialize context\n");
goto finish;
}
/* Let libcoap do the multi-block payload handling (if any) */
coap_context_set_block_mode(ctx, COAP_BLOCK_USE_LIBCOAP|COAP_BLOCK_SINGLE_BODY);
scheme_hint_bits = coap_get_available_scheme_hint_bits(0, 0, COAP_PROTO_NONE);
info_list = coap_resolve_address_info(my_address, 0, 0, 0, 0,
0,
scheme_hint_bits, COAP_RESOLVE_TYPE_LOCAL);
/* Create CoAP listening endpoint(s) */
for (info = info_list; info != NULL; info = info->next) {
coap_endpoint_t *ep;
ep = coap_new_endpoint(ctx, &info->addr, info->proto);
if (!ep) {
coap_log_warn("cannot create endpoint for CoAP proto %u\n",
info->proto);
} else {
have_ep = true;
}
}
coap_free_address_info(info_list);
if (have_ep == false) {
coap_log_err("No context available for interface '%s'\n",
(const char *)my_address->s);
goto finish;
}
/* Add in Multicast listening as appropriate */
#ifdef COAP_LISTEN_MCAST_IPV4
coap_join_mcast_group_intf(ctx, COAP_LISTEN_MCAST_IPV4, NULL);
#endif /* COAP_LISTEN_MCAST_IPV4 */
#ifdef COAP_LISTEN_MCAST_IPV6
coap_join_mcast_group_intf(ctx, COAP_LISTEN_MCAST_IPV6, NULL);
#endif /* COAP_LISTEN_MCAST_IPV6 */
/* Create a resource that the server can respond to with information */
resource = coap_resource_init(coap_make_str_const("hello"), 0);
coap_register_handler(resource, COAP_REQUEST_GET,
[](auto, auto,
const coap_pdu_t *request,
auto, coap_pdu_t *response) {
coap_show_pdu(COAP_LOG_WARN, request);
coap_pdu_set_code(response, COAP_RESPONSE_CODE_CONTENT);
coap_add_data(response, 5,
(const uint8_t *)"world");
coap_show_pdu(COAP_LOG_WARN, response);
});
coap_add_resource(ctx, resource);
/* Create another resource that the server can respond to with information */
resource = coap_resource_init(coap_make_str_const("hello/my"), 0);
coap_register_handler(resource, COAP_REQUEST_GET,
[](auto, auto,
const coap_pdu_t *request,
auto, coap_pdu_t *response) {
coap_show_pdu(COAP_LOG_WARN, request);
coap_pdu_set_code(response, COAP_RESPONSE_CODE_CONTENT);
coap_add_data(response, 8,
(const uint8_t *)"my world");
coap_show_pdu(COAP_LOG_WARN, response);
});
coap_add_resource(ctx, resource);
/* Handle any libcoap I/O requirements */
while (true) {
coap_io_process(ctx, COAP_IO_WAIT);
}
result = EXIT_SUCCESS;
finish:
coap_free_context(ctx);
coap_cleanup();
return result;
}