CoAP - Callback function

Hello Nordic forum,

I must not quite understand how CoAP callbacks should work. Would anyone be able to give me any advice as to why my modified coap_client results in undefined behavior? In my main program, I'd call the function coap_send() at various points while varying the tx_payload string.

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "coap.h"
#include <stdio.h>
#include <string.h>
#if defined(CONFIG_POSIX_API)
#include <zephyr/posix/arpa/inet.h>
#include <zephyr/posix/netdb.h>
#include <zephyr/posix/sys/socket.h>
#include <zephyr/posix/poll.h>
#else
#include <zephyr/net/socket.h>
#endif /* CONFIG_POSIX_API */
#include <zephyr/kernel.h>
#include <zephyr/sys/reboot.h>
#include <zephyr/net/coap.h>
#include <zephyr/net/socket.h>
#include <zephyr/net/conn_mgr_connectivity.h>
#include <zephyr/net/conn_mgr_monitor.h>
#include <zephyr/random/random.h>
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

The first time coap_send() is called, everything works fine. Message gets sent and callback is received. The second time, however, the message gets sent (verified by looking at my endpoint), but I never get the LOG_INF notifying me of the server response.

When I build my program using the default optimizations, the program crashes without warning directly after sending the second message. Interestingly, when I build my program using the debugging optimization, the program will actually work and send messages correctly, but I will not receive a server response.

To me, this seems to indicate a problem with how I'm doing the response callback.