|
| 1 | +/* |
| 2 | + * Copyright (C) 2016 Kaspar Schleiser <[email protected]> |
| 3 | + * |
| 4 | + * This file is subject to the terms and conditions of the GNU Lesser |
| 5 | + * General Public License v2.1. See the file LICENSE in the top level |
| 6 | + * directory for more details. |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * @ingroup examples |
| 11 | + * @{ |
| 12 | + * |
| 13 | + * @file |
| 14 | + * @brief CoAP example server application (using nanocoap) |
| 15 | + * |
| 16 | + * @author Kaspar Schleiser <[email protected]> |
| 17 | + * @} |
| 18 | + */ |
| 19 | + |
| 20 | +#include <stdio.h> |
| 21 | + |
| 22 | +#include "net/nanocoap_sock.h" |
| 23 | +#include "time_units.h" |
| 24 | +#include "ztimer.h" |
| 25 | + |
| 26 | +#if MODULE_NANOCOAP_SERVER_TCP |
| 27 | +# include "event/thread.h" |
| 28 | +#endif |
| 29 | + |
| 30 | +#define COAP_INBUF_SIZE (256U) |
| 31 | + |
| 32 | +#define MAIN_QUEUE_SIZE (8) |
| 33 | +static msg_t _main_msg_queue[MAIN_QUEUE_SIZE]; |
| 34 | + |
| 35 | +#if MODULE_NANOCOAP_SERVER_TCP |
| 36 | +static nanocoap_tcp_server_ctx_t tcp_ctx; |
| 37 | +#endif |
| 38 | + |
| 39 | +#if MODULE_NANOCOAP_SERVER_WS && MODULE_NANOCOAP_WS_UDP_YOLO |
| 40 | +static coap_ws_over_udp_yolo_init_arg_t _ws_ctx; |
| 41 | +#endif |
| 42 | + |
| 43 | +int main(void) |
| 44 | +{ |
| 45 | + puts("RIOT nanocoap example application"); |
| 46 | + |
| 47 | + /* nanocoap_server uses gnrc sock which uses gnrc which needs a msg queue */ |
| 48 | + msg_init_queue(_main_msg_queue, MAIN_QUEUE_SIZE); |
| 49 | + |
| 50 | + puts("Waiting for address autoconfiguration..."); |
| 51 | + ztimer_sleep(ZTIMER_USEC, 3 * US_PER_SEC); |
| 52 | + |
| 53 | + /* print network addresses */ |
| 54 | + printf("{\"IPv6 addresses\": [\""); |
| 55 | + netifs_print_ipv6("\", \""); |
| 56 | + puts("\"]}"); |
| 57 | + |
| 58 | +#if MODULE_NANOCOAP_SERVER_TCP |
| 59 | + nanocoap_server_tcp(&tcp_ctx, EVENT_PRIO_MEDIUM, NULL); |
| 60 | + printf("CoAP+TCP on PORT %u\n", (unsigned)tcp_ctx.local.port); |
| 61 | +#endif |
| 62 | + |
| 63 | +#if MODULE_NANOCOAP_SERVER_WS && MODULE_NANOCOAP_WS_UDP_YOLO |
| 64 | + sock_udp_ep_t local_ws = { .port = 1337, .family = AF_INET6 }; |
| 65 | + nanocoap_server_ws(&coap_ws_over_udp_yolo, &_ws_ctx, &local_ws, sizeof(local_ws)); |
| 66 | + printf("CoAP+YOLO on PORT %u\n", (unsigned)local_ws.port); |
| 67 | +#endif |
| 68 | + |
| 69 | +#if MODULE_NANOCOAP_UDP |
| 70 | + /* initialize nanocoap server instance */ |
| 71 | + uint8_t buf[COAP_INBUF_SIZE]; |
| 72 | + sock_udp_ep_t local = { .port=COAP_PORT, .family=AF_INET6 }; |
| 73 | + printf("CoAP (UDP) on PORT %u\n", (unsigned)local.port); |
| 74 | + nanocoap_server_udp(&local, buf, sizeof(buf)); |
| 75 | +#endif |
| 76 | + |
| 77 | + /* should be never reached */ |
| 78 | + return 0; |
| 79 | +} |
0 commit comments