Commit graph

189 commits

Author SHA1 Message Date
Jukka Rissanen
5a9a39caf3 net: mgmt: Convert the mgmt API to use 64-bit masks
Instead of using 32 bit enum values for event numbers, convert
the code to use 64 bit long bit fields. This means that the
user API is changed to use 64 bit event values instead of 32
bit event values.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2025-06-18 10:54:44 +02:00
Benjamin Cabé
1032dd47b2 net: coap: fix offset typo in insert_be16
Fix usage of the wrong variable in insert_be16 which could cause
insertion at the wrong offset.

Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org>
2025-06-18 07:40:12 +02:00
Benjamin Cabé
5e287bb73f net: coap: adopt SHELL_HELP
Adopt SHELL_HELP macro for coap_server_shell

Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org>
2025-06-13 10:11:29 -07:00
Pieter De Gendt
d5931bae93 net: lib: coap: Return an error on removing a non-existing observer
If we're parsing a CoAP request with an observe option of '1', but there is
no matching observer, return an error instead of returning a zero.

Signed-off-by: Pieter De Gendt <pieter.degendt@basalte.be>
2025-05-30 10:28:02 +02:00
Pieter De Gendt
857abda15e net: lib: coap: coap_server: Change error print to debug
The POLLHUP is expected with, for example DTLS UDP sockets.

Signed-off-by: Pieter De Gendt <pieter.degendt@basalte.be>
2025-05-01 08:53:32 -04:00
Pieter De Gendt
470b582e04 net: lib: coap: Add CoAP secure support
Add optional TLS secure tag lists for CoAP services to enable DTLS
sockets.

Signed-off-by: Pieter De Gendt <pieter.degendt@basalte.be>
2025-05-01 08:53:32 -04:00
Adrian Friedli
89f8f5bb07 net: lib: coap: fix division by zero
Prevent the modulo operation causing a division by zero error by falling
back to the minimal ACK timeout value if the random factor is 1.0. Also
fall back to the same value if the random factor is erroneously smaller
than 1.0. Additionally make the upper bound of the random range
reachable.

Signed-off-by: Adrian Friedli <adrian.friedli@husqvarnagroup.com>
2025-04-29 19:04:16 +02:00
Robert Lubos
cf0b6068d2 net: coap_client: Fix CoAP client thread priority
The default thread priority for the CoAP client thread is set to
NUM_PREEMPT_PRIORITIES which is not a valid thread priority, as the
lowest application thread priority is actually
NUM_PREEMPT_PRIORITIES - 1. Because of this, CoAP client library gave an
assert on boot if assertions were enabled.

Kconfig does not allow for arithmetics when setting integer defaults,
therefore handle this at the preprocessor stage by limiting the actual
priority assigned to the CoAP client thread to a valid range.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2025-04-04 18:17:30 +02:00
Pieter De Gendt
044a94f06a net: lib: coap: coap_server: Use eventfd instead of socket pair
Convert the socket poll logic to use a more lightweight eventfd file
descriptor instead of a socket pair.

Signed-off-by: Pieter De Gendt <pieter.degendt@basalte.be>
2025-03-26 08:55:45 +01:00
Seppo Takalo
334b32d9f5 net: coap_client: Stop lifetime on piggybacked Ack
When we receive piggybacked Ack, it means that we have
received all information for this request.
There is no need to track exchange lifetime anymore.

Once we reset the internal request, it is free to be used
for the application. So if we only have few requests allocated,
it would be slow to send those as get_free_request() only
gives request structures that don't have a lifetime left.

Signed-off-by: Seppo Takalo <seppo.takalo@nordicsemi.no>
2025-03-07 20:01:26 +01:00
Povilas Selevicius
37ddeb459c net: coap_client: expose has_ongoing_exchange function
The coap_client has a static/internal function, has_ongoing_exchange(),
which can be an useful addition to the public API. This would provide
a mechanism to determine when it is safe to close a socket gracefully.

Signed-off-by: Povilas Selevicius <povilas.selevicius@quadigi.com>
2025-03-07 19:52:22 +01:00
Robert Lubos
7ccf870670 net: coap_client: Stop all socket activities when cancelling requests
Calling coap_client_cancel_requests() clears the internal request
context only for active requests (i. e. not replied yet). However,
if there are any pending request context monitoring ACK duplicates,
those would still make the corresponding client socket being monitored
by poll(). In result, when application closes the socket, the polling
thread will throw POLLNVAL error for the socket.

Fix this, by resetting all request contexts unconditionally. The request
callback will only be called for the active requests.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2025-02-18 13:31:15 +01:00
Tom Hughes
d811760779 net: lib: coap: Remove unused function
Building with clang warns:

subsys/net/lib/coap/coap_link_format.c:39:20: error: unused function
'append_be16' [-Werror,-Wunused-function]
static inline bool append_be16(struct coap_packet *cpkt, uint16_t data)

Signed-off-by: Tom Hughes <tomhughes@chromium.org>
2025-02-12 02:25:53 +01:00
Robert Lubos
d6792494b7 net: coap_client: Fix build with cpp projects
The coap_client.h header won't build if included from c++ file:

error: expected primary-expression before ‘.’ token
  .value[0] = coap_bytes_to_block_size(CONFIG_COAP_CLIENT_BLOCK_SIZE),

Therefore move the actual function implementation to the library C file
to prevent this.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2025-02-11 15:41:37 +01:00
Jakub Witowski
05ab017774 net: coap: fix uninitialized usage of option struct
option struct was uninitialized in insert_option() function
during encode_options() call when the while loop wasn't
executed before.

Signed-off-by: Jakub Witowski <jakub.witowski9302@gmail.com>
2025-02-11 11:45:05 +00:00
Jakub Witowski
828f5e8b23 net: coap: fix uninitialized usage of hdr_len
The hdr_len was uninitialized when none of if-else
conditions were met in decode_delta() function.

Signed-off-by: Jakub Witowski <jakub.witowski9302@gmail.com>
2025-02-11 11:45:05 +00:00
Dave Lacerte
d9aab69fbf net: coap: Fix uninitialized variable warning
The variable block_len in function coap_next_block_for_option()
can be used uninitialized in case of empty packet in
coap_packet_get_payload(). Mute the compiler warning with
default value of 0.

Signed-off-by: Dave Lacerte <lacerte.dave@hydroquebec.com>
2025-02-06 17:49:42 +01:00
Robert Lubos
64e5a31b61 net: coap: Fix coap_packet_is_request() check for empty code
Empty code was incorrectly matched as a request, fix that.

Align coap_handle_request_len() function to behave as documented in the
API documentation - in case of invalid request code (which is also the
case for empty code) -ENOTSUP Should be returned.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2025-01-21 19:30:06 +01:00
Robert Lubos
13cd48a431 net: coap: Fix response matching algorithm
The algorithm for matching request with response was incorrect, which
could lead to false matches (for example if request had a token, and
piggybacked reply had no token but matching message ID only, that would
still be counted as a match).

This commit fixes it. The request/reply matching is implemented based on
RFC now, with separate conditions for piggybacked/separate responses.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2025-01-21 19:30:06 +01:00
Pieter De Gendt
81b87ac35b net: lib: coap: Handle truncated messages in CoAP server
If the CoAP server receives a message that doesn't fit into the receive
buffer, we should stop processing the message and respond to the client
with 4.13 "Request Entity too large".

Signed-off-by: Pieter De Gendt <pieter.degendt@basalte.be>
2025-01-13 20:22:47 +01:00
Seppo Takalo
2066cf6a3d net: lib: coap_client: Release non-confirmable requests
Non-confirmable CoAP requests need lifetime tracking as well
so we can free the structure after a timeout.

Signed-off-by: Seppo Takalo <seppo.takalo@nordicsemi.no>
2024-11-21 14:47:38 +01:00
Seppo Takalo
6c169668e9 net: lib: coap_client: Fix timeout for separate response
When waiting for response after receiving the empty Ack, client
actually used way too timeout.
CoAP timeout only holds the timeout value in ms. t0 is the starting time.

Signed-off-by: Seppo Takalo <seppo.takalo@nordicsemi.no>
2024-11-21 14:47:38 +01:00
Seppo Takalo
b3f3bce23e net: lib: coap_client: Add API to cancel specific request
Add a new API to cancel just one, or mathing requests,
instead of cancelling all ongoing requests.

Signed-off-by: Seppo Takalo <seppo.takalo@nordicsemi.no>
2024-11-16 13:33:28 -05:00
Seppo Takalo
f0c6efe798 net: lib: coap_client: Stop polling on unneeded sockets
poll() only for sockets that have traffic ongoing or have some lifetime
left.
On socket failures during a poll(), stop listening for the socket.
Application can recover by reconnecting the socket.

Signed-off-by: Seppo Takalo <seppo.takalo@nordicsemi.no>
2024-11-16 13:33:28 -05:00
Seppo Takalo
f72d634826 net: lib: coap_client: All error cases should be reported to callback
When the client fails when parsing the response and we stop proceeding,
we should report that to the application.

Signed-off-by: Seppo Takalo <seppo.takalo@nordicsemi.no>
2024-11-16 13:33:28 -05:00
Seppo Takalo
c0eb260c2c net: lib: coap_client: Drop duplicate MID only after responding with Ack
Even if we receive duplicate confirmable message, we should still
respond with the Ack. Just don't deliver the second callback.

This is achieved by moving the MID deduplication to after Ack handling.

Signed-off-by: Seppo Takalo <seppo.takalo@nordicsemi.no>
2024-11-16 13:33:28 -05:00
Seppo Takalo
48434a3c1b net: lib: coap_client: Return -errno from send_request()
Return the -errno when zsock_sendto() or zsock_recvfrom() fails, so
rest of the code can deal with return values, instead of separately
comparing errno and return value.


Signed-off-by: Seppo Takalo <seppo.takalo@nordicsemi.no>
2024-11-16 13:33:28 -05:00
Seppo Takalo
a1368a7ff7 net: lib: coap_client: Drop duplicate responses
When response is already handled, don't forward anymore responses
to the client application.

Signed-off-by: Seppo Takalo <seppo.takalo@nordicsemi.no>
2024-11-16 13:33:28 -05:00
Seppo Takalo
934c74f26e net: lib: coap_client: Don't match zero length tokens
If our internal structure is cleared, don't match tokens.

Signed-off-by: Seppo Takalo <seppo.takalo@nordicsemi.no>
2024-11-16 13:33:28 -05:00
Seppo Takalo
7b0cce4418 net: lib: coap_client: Parse incoming MID only once
Incomming Message-ID is already parsed, use it as a parameter to
get_request_with_mid().

Signed-off-by: Seppo Takalo <seppo.takalo@nordicsemi.no>
2024-11-16 13:33:28 -05:00
Seppo Takalo
41ee35ae8b net: lib: coap_client: Don't clear internal structures on response
When response is received and handled, don't just clear the structure
but instead mark it as ongoing=false.
So if we later on receive a duplicate response for it, we can still
respond with Ack or Rst.

This is achieved by using release_internal_request() when we don't
expect any response for it and reset_internal_request() when we really
fill up a new request.

Signed-off-by: Seppo Takalo <seppo.takalo@nordicsemi.no>
2024-11-16 13:33:28 -05:00
Seppo Takalo
1e5a537ade net: lib: coap_client: Remove unnecessary atomic variable
In receiving thread, continuing the loops is based on
has_ongoing_exchanges() so it does not need atomic
coap_client_recv_active variable.

When idling, it wakes from semaphore. But there was potential
deadlock when coap_client_schedule_poll() would not signal the
semaphore, if atomic variable was already showing that it runs.
Removing the atomic variable removes this deadlock.

Signed-off-by: Seppo Takalo <seppo.takalo@nordicsemi.no>
2024-10-31 14:17:58 -05:00
Seppo Takalo
350d20e027 net: lib: coap_client: Send RST for unknown queries
When receiving unknown response, respond with CoAP Reset.

Signed-off-by: Seppo Takalo <seppo.takalo@nordicsemi.no>
2024-10-31 14:17:58 -05:00
Seppo Takalo
1dc24872ce net: lib: coap_client: Remove duplicate token comparison
Response tokens are already compared in get_request_with_token().

Signed-off-by: Seppo Takalo <seppo.takalo@nordicsemi.no>
2024-10-31 14:17:58 -05:00
Seppo Takalo
e96e95b6f6 net: coap: Add API to send reset message
Add helper API to construct CoAP Reset message.

Signed-off-by: Seppo Takalo <seppo.takalo@nordicsemi.no>
2024-10-31 14:17:58 -05:00
Seppo Takalo
1890dbd637 net: lib: coap_client: Fix reset handling
Fix handling of received CoAP reset.


Signed-off-by: Seppo Takalo <seppo.takalo@nordicsemi.no>
2024-10-31 14:17:58 -05:00
Seppo Takalo
a14f083030 net: lib: coap_client: Use reset_internal_request() instead of flagging
It is error prone to flag separate booleans, so try to use
reset_internal_request() every time we release the internal request
structure.

Also refactor the reset_internal_request() so that we reset the
timeout value so it does not trigger again.

Signed-off-by: Seppo Takalo <seppo.takalo@nordicsemi.no>
2024-10-31 14:17:58 -05:00
Seppo Takalo
623a1ffd52 net: lib: coap_client: Don't decrease retry counter on send() failure
If send() fails, we have not technically send the CoAP retry yet, so
restore the same pending structure, so our timeouts and retry counters
stay the same.

This will trigger a retry next time the poll() return POLLOUT, so we
know that we can send.

Signed-off-by: Seppo Takalo <seppo.takalo@nordicsemi.no>
2024-10-31 14:17:58 -05:00
Seppo Takalo
6481b0ec6c net: lib: coap_client: Forward recv() errors to handling loop
Forward recv() errors to handle_poll(), so there is only one place to
handle error codes.

Signed-off-by: Seppo Takalo <seppo.takalo@nordicsemi.no>
2024-10-31 14:17:58 -05:00
Seppo Takalo
4c6dd4c7b7 net: lib: coap_client: check poll() condition before retrying CoAP msg
Refactor the CoAP retry handling into the handle_poll() function,
so that we only try to send retries if the socket reports POLLOUT.

Also move the receiving into same loop, so when poll() reports POLLIN
we recv() the message and handle it before proceeding to other sockets.

Also fix tests to handle POLLOUT flag and add support for testing
multiple clients.

Signed-off-by: Seppo Takalo <seppo.takalo@nordicsemi.no>
2024-10-31 14:17:58 -05:00
Seppo Takalo
46b7c84512 net: lib: coap_client: Release internal request when failed to send
When transmission of first request fails, reset the internal request
buffer as there is no ongoing CoAP transaction.

Application can deal with the failure.

Signed-off-by: Seppo Takalo <seppo.takalo@nordicsemi.no>
2024-10-31 14:17:58 -05:00
Seppo Takalo
1ea569d776 net: lib: coap_client: Protect initialization with mutex
Protect global list of clients with mutex.

Signed-off-by: Seppo Takalo <seppo.takalo@nordicsemi.no>
2024-10-31 14:17:58 -05:00
Benjamin Lindqvist
f8a7035c0a net: coap_client: signal socket error to user
Before this patch, any unexpected socket error during poll (caused by
LTE disconnects for instance) would lead to a infinite loop because the
error state was never cleared, handled or even signaled to the user.

Signed-off-by: Benjamin Lindqvist <benjamin@eub.se>
2024-10-26 03:55:20 +01:00
Robert Lubos
c4803752a8 net: Deprecate CONFIG_NET_SOCKETS_POLL_MAX
CONFIG_ZVFS_POLL_MAX is now used to control the maximum number of poll()
entires. Thereby, CONFIG_NET_SOCKETS_POLL_MAX is redundant and shall
be deprecated.

Modify the defaults for NET_SOCKETS_POLL_MAX and ZVS_POLL_MAX so that
the deprecation actually makes sense instead of symbol removal. In case
the application still sets the old config, it will modify the
ZVS_POLL_MAX default.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2024-10-14 13:01:51 +02:00
Adrian Friedli
98289e594d net: lib: coap: make ACK random factor runtime configurable
Extend the `coap_transmission_parameters` struct with the field
`ack_random_percent`. This was the last remaining CoAP transmission
parameter that was not configurable at runtime.

Signed-off-by: Adrian Friedli <adrian.friedli@husqvarnagroup.com>
2024-10-08 06:00:56 -04:00
Tommi Rantanen
a5aed04b7e net: lib: coap: Initialize response_truncated
Fix the following compilation warning given when using newlibc:
warning: 'response_truncated' may be used
uninitialized [-Wmaybe-uninitialized]

Issue is not seen with picolibc.

The variable was introduced as part of PR #76257

Signed-off-by: Tommi Rantanen <tommi.rantanen@nordicsemi.no>
2024-10-03 17:08:21 +01:00
Pete Skeggs
715b97397b net: lib: coap: Make use of ZSOCK_MSG_TRUNC configurable
Not all offloaded network stacks support this socket option so
control it using a Kconfig CONFIG_COAP_CLIENT_TRUNCATE_MSGS,
and enable it by default.

Signed-off-by: Pete Skeggs <peter.skeggs@nordicsemi.no>
2024-10-02 15:55:00 +02:00
Francois Gervais
bfed8d0966 net: lib: coap_client: wait for all acknowledgements
This commit makes sure we continue to wait for extra confirmations even
after the request is done so we can handle duplicate confirmations if any.

Detailed description:

rfc7252#section-4.5 specifies that:

"The recipient SHOULD acknowledge each duplicate copy of a
 Confirmable message".

So if, for example, the client sends to a multicast destination address,
the server will get multiple requests and will confirm all of them.

Without this commit, the client will set the request to done after
receiving the first answer.
From here the request object will be marked as free and the duplicate
acknowledgements will stay buffered in the network stack.
Once the client tries to send a new request, it will unbuffer those
duplicate acknowledgements but now the request object is unallocated
so the client won't be able to handle those acknowledgements as duplicates.
It will instead treat it as an unexpected ACK.

To work around this issue, rfc7252#section-4.8.2 states that:

"EXCHANGE_LIFETIME is the time from starting to send a Confirmable
 message to the time when an acknowledgement is no longer expected,
 i.e., message-layer information about the message exchange can be
 purged."

Keeping the request object allocated for EXCHANGE_LIFETIME ensures that
duplicate acknowledgements can be handled accordingly.

This commit adds a basic implementation of what is stated in the RFC.

EXCHANGE_LIFETIME has been arbitrarily set to 3 * ACK_TIMEOUT which
seems more reasonable than the 247 seconds stated in the RFC.

Signed-off-by: Francois Gervais <francoisgervais@gmail.com>
2024-09-26 09:20:23 -05:00
Robert Lubos
5f43388308 net: coap: Parse more flag in coap_get_block2_option()
Parse the more flag in coap_get_block2_option(), so that the function
can be used not only with requests but also with replies (where the more
flag should not be ignored).

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2024-08-02 22:12:47 -05:00
Robert Lubos
3902a990e5 net: coap: Fix underlying type for block number
The block number in block1/2 options can be encoded on up to 20 bits
according to RFC 7959, therefore the underlying type used in helper
functions to retrieve the block number should be large enough to hold
the result. Therefore, replace the container for block number with
uint32_t instead of uint8_t.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2024-08-02 22:12:47 -05:00