1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

nanocoap_server: add nanocoap_server_auto_init

This commit is contained in:
Benjamin Valentin 2023-03-10 01:09:21 +01:00 committed by Benjamin Valentin
parent 88789454dd
commit 9495dc4e2e
5 changed files with 80 additions and 0 deletions

View File

@ -739,6 +739,15 @@ ifneq (,$(filter nanocoap_dtls,$(USEMODULE)))
USEPKG += tinydtls
endif
ifneq (,$(filter nanocoap_server_auto_init,$(USEMODULE)))
USEMODULE += nanocoap_server
endif
ifneq (,$(filter nanocoap_server,$(USEMODULE)))
USEMODULE += nanocoap_resources
USEMODULE += nanocoap_sock
endif
ifneq (,$(filter nanocoap_sock,$(USEMODULE)))
USEMODULE += sock_udp
USEMODULE += sock_util

View File

@ -160,6 +160,10 @@ extern void gcoap_init(void);
AUTO_INIT(gcoap_init,
AUTO_INIT_PRIO_MOD_GCOAP);
#endif
#if IS_USED(MODULE_NANOCOAP_SERVER_AUTO_INIT)
extern void auto_init_nanocoap_server(void);
AUTO_INIT(auto_init_nanocoap_server, AUTO_INIT_PRIO_MOD_NANOCOAP);
#endif
#if IS_USED(MODULE_DEVFS)
extern void auto_init_devfs(void);
AUTO_INIT(auto_init_devfs,

View File

@ -174,6 +174,12 @@ extern "C" {
#define AUTO_INIT_PRIO_MOD_UWB_CORE 1230
#endif
#ifndef AUTO_INIT_PRIO_MOD_GCOAP
/**
* @brief nanoCoAP server priority
*/
#define AUTO_INIT_PRIO_MOD_NANOCOAP 1235
#endif
#ifndef AUTO_INIT_PRIO_MOD_GCOAP
/**
* @brief GCoAP priority
*/

View File

@ -159,6 +159,22 @@ extern "C" {
#define CONFIG_NANOCOAP_SOCK_DTLS_TAG (0xc0ab)
#endif
/**
* @brief CoAP server work buf size
* Used both for RX and TX, needs to hold payload block + header
*/
#ifndef CONFIG_NANOCOAP_SERVER_BUF_SIZE
#define CONFIG_NANOCOAP_SERVER_BUF_SIZE ((1 << (CONFIG_NANOCOAP_BLOCKSIZE_DEFAULT + 3)) \
+ CONFIG_NANOCOAP_URI_MAX + 16)
#endif
/**
* @brief CoAP server thread stack size
*/
#ifndef CONFIG_NANOCOAP_SERVER_STACK_SIZE
#define CONFIG_NANOCOAP_SERVER_STACK_SIZE THREAD_STACKSIZE_DEFAULT
#endif
/**
* @brief NanoCoAP socket types
*/
@ -220,6 +236,18 @@ static inline uint16_t nanocoap_sock_next_msg_id(nanocoap_sock_t *sock)
*/
int nanocoap_server(sock_udp_ep_t *local, uint8_t *buf, size_t bufsize);
/**
* @brief Create and start the nanoCoAP server thread
*
* To automatically start the nanoCoAP server on startup, select the
* `nanocoap_server_auto_init` module.
*
* @param[in] local UDP endpoint to bind to
*
* @return pid of the server thread
*/
kernel_pid_t nanocoap_server_start(const sock_udp_ep_t *local);
/**
* @brief Create a CoAP client socket
*

View File

@ -817,3 +817,36 @@ int nanocoap_server(sock_udp_ep_t *local, uint8_t *buf, size_t bufsize)
return 0;
}
static kernel_pid_t _coap_server_pid;
static void *_nanocoap_server_thread(void *local)
{
static uint8_t buf[CONFIG_NANOCOAP_SERVER_BUF_SIZE];
nanocoap_server(local, buf, sizeof(buf));
return NULL;
}
kernel_pid_t nanocoap_server_start(const sock_udp_ep_t *local)
{
static char stack[CONFIG_NANOCOAP_SERVER_STACK_SIZE];
if (_coap_server_pid) {
return _coap_server_pid;
}
_coap_server_pid = thread_create(stack, sizeof(stack), THREAD_PRIORITY_MAIN - 1,
THREAD_CREATE_STACKTEST, _nanocoap_server_thread,
(void *)local, "nanoCoAP server");
return _coap_server_pid;
}
void auto_init_nanocoap_server(void)
{
sock_udp_ep_t local = {
.port = COAP_PORT,
.family = AF_INET6,
};
nanocoap_server_start(&local);
}