Skip to content

Commit

Permalink
Merge pull request #20238 from benpicco/nanocoap_sock_fetch
Browse files Browse the repository at this point in the history
nanocoap_sock: implement FETCH methods
  • Loading branch information
maribu authored Jan 10, 2024
2 parents 2e3037c + 7240f70 commit 5fa94b5
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
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 @@ -451,6 +451,14 @@ ssize_t nanocoap_sock_post(nanocoap_sock_t *sock, const char *path,
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 @@ ssize_t nanocoap_sock_post_non(nanocoap_sock_t *sock, const char *path,
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 @@ ssize_t nanocoap_sock_post_url(const char *url,
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

0 comments on commit 5fa94b5

Please sign in to comment.