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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>