Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nanocoap_sock: implement FETCH methods #20238

Merged
merged 1 commit into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 56 additions & 1 deletion sys/include/net/nanocoap_sock.h
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,61 @@ ssize_t nanocoap_sock_post_url(const char *url,
const void *request, size_t len,
void *response, size_t len_max);

/**
* @brief Simple synchronous CoAP (confirmable) FETCH
* ([RFC 8132](https://datatracker.ietf.org/doc/html/rfc8132))
*
* @param[in] sock socket to use for the request
* @param[in] path remote path
* @param[in] request buffer containing the payload
* @param[in] len length of the payload to send
* @param[out] response buffer for the response, may be NULL
* @param[in] len_max length of @p response
*
* @returns length of response payload on success
* @returns <0 on error
*/
ssize_t nanocoap_sock_fetch(nanocoap_sock_t *sock, const char *path,
const void *request, size_t len,
void *response, size_t len_max);

/**
* @brief Simple non-confirmable FETCH
* ([RFC 8132](https://datatracker.ietf.org/doc/html/rfc8132))
*
* @param[in] sock socket to use for the request
* @param[in] path remote path
* @param[in] request buffer containing the payload
* @param[in] len length of the payload to send
* @param[out] response buffer for the response, may be NULL
* @param[in] len_max length of @p response
*
* @returns length of response payload on success
* @returns 0 if the request was sent and no response buffer was provided,
* independently of success (because no response is requested in that case)
* @returns <0 on error
*/
ssize_t nanocoap_sock_fetch_non(nanocoap_sock_t *sock, const char *path,
const void *request, size_t len,
void *response, size_t len_max);

/**
* @brief Simple synchronous CoAP (confirmable) FETCH to URL
* ([RFC 8132](https://datatracker.ietf.org/doc/html/rfc8132))
*
* @param[in] url Absolute URL pointer to source path
* @param[in] request buffer containing the payload
* @param[in] len length of the payload to send
* @param[out] response buffer for the response, may be NULL
* @param[in] len_max length of @p response
*
* @returns length of response payload on success
* @returns <0 on error
*/
ssize_t nanocoap_sock_fetch_url(const char *url,
const void *request, size_t len,
void *response, size_t len_max);

/**
* @brief Simple synchronous CoAP (confirmable) DELETE
*
Expand Down Expand Up @@ -571,7 +626,7 @@ ssize_t nanocoap_request(coap_pkt_t *pkt, const sock_udp_ep_t *local,
* @param[out] ctx The block request context to initialize
* @param[out] sock Socket to initialize and use for the request
* @param[in] url The request URL
* @param[in] method Request method (`COAP_METHOD_{GET|PUT|POST}`)
* @param[in] method Request method (`COAP_METHOD_{GET|PUT|POST|FETCH}`)
* @param[in] blksize Request blocksize exponent
*
* @retval 0 Success
Expand Down
23 changes: 23 additions & 0 deletions sys/net/application_layer/nanocoap/sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@

/* random timeout, deadline for receive retries */
uint32_t timeout = random_uint32_range(CONFIG_COAP_ACK_TIMEOUT_MS * US_PER_MS,
CONFIG_COAP_ACK_TIMEOUT_MS * CONFIG_COAP_RANDOM_FACTOR_1000);

Check warning on line 193 in sys/net/application_layer/nanocoap/sock.c

View workflow job for this annotation

GitHub Actions / static-tests

line is longer than 100 characters
uint32_t deadline = _deadline_from_interval(timeout);

/* check if we expect a reply */
Expand Down Expand Up @@ -451,6 +451,14 @@
response, len_max);
}

ssize_t nanocoap_sock_fetch(nanocoap_sock_t *sock, const char *path,
const void *request, size_t len,
void *response, size_t len_max)
{
return _sock_put_post(sock, path, COAP_METHOD_FETCH, COAP_TYPE_CON, request, len,
response, len_max);
}

ssize_t nanocoap_sock_put_non(nanocoap_sock_t *sock, const char *path,
const void *request, size_t len,
void *response, size_t len_max)
Expand All @@ -467,6 +475,14 @@
response, len_max);
}

ssize_t nanocoap_sock_fetch_non(nanocoap_sock_t *sock, const char *path,
const void *request, size_t len,
void *response, size_t len_max)
{
return _sock_put_post(sock, path, COAP_METHOD_FETCH, COAP_TYPE_NON, request, len,
response, len_max);
}

static ssize_t _sock_put_post_url(const char *url, unsigned code,
const void *request, size_t len,
void *response, size_t len_max)
Expand Down Expand Up @@ -498,6 +514,13 @@
return _sock_put_post_url(url, COAP_METHOD_POST, request, len, response, len_max);
}

ssize_t nanocoap_sock_fetch_url(const char *url,
const void *request, size_t len,
void *response, size_t len_max)
{
return _sock_put_post_url(url, COAP_METHOD_FETCH, request, len, response, len_max);
}

ssize_t nanocoap_sock_delete(nanocoap_sock_t *sock, const char *path)
{
/* buffer for CoAP header */
Expand Down
Loading