1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-01-18 12:52:44 +01:00

nanocoap_sock: add nanocoap_sock_{put, post}_non()

This commit is contained in:
Benjamin Valentin 2022-09-28 12:15:58 +02:00 committed by Benjamin Valentin
parent 56f36c09a0
commit 1cfcb307f2
2 changed files with 68 additions and 5 deletions

View File

@ -240,6 +240,25 @@ ssize_t nanocoap_sock_put(nanocoap_sock_t *sock, const char *path,
const void *request, size_t len, const void *request, size_t len,
void *response, size_t len_max); void *response, size_t len_max);
/**
* @brief Simple non-confirmable PUT
*
* @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_put_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) PUT to URL * @brief Simple synchronous CoAP (confirmable) PUT to URL
* *
@ -273,6 +292,25 @@ ssize_t nanocoap_sock_post(nanocoap_sock_t *sock, const char *path,
const void *request, size_t len, const void *request, size_t len,
void *response, size_t len_max); void *response, size_t len_max);
/**
* @brief Simple non-confirmable POST
*
* @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_post_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) POST to URL * @brief Simple synchronous CoAP (confirmable) POST to URL
* *

View File

@ -311,7 +311,7 @@ ssize_t nanocoap_sock_get(nanocoap_sock_t *sock, const char *path, void *buf, si
} }
ssize_t _sock_put_post(nanocoap_sock_t *sock, const char *path, unsigned code, ssize_t _sock_put_post(nanocoap_sock_t *sock, const char *path, unsigned code,
const void *request, size_t len, uint8_t type, const void *request, size_t len,
void *response, size_t max_len) void *response, size_t max_len)
{ {
/* buffer for CoAP header */ /* buffer for CoAP header */
@ -333,9 +333,15 @@ ssize_t _sock_put_post(nanocoap_sock_t *sock, const char *path, unsigned code,
.iov_len = max_len, .iov_len = max_len,
}; };
pktpos += coap_build_hdr(pkt.hdr, COAP_TYPE_CON, NULL, 0, code, _get_id()); pktpos += coap_build_hdr(pkt.hdr, type, NULL, 0, code, _get_id());
pktpos += coap_opt_put_uri_path(pktpos, 0, path); pktpos += coap_opt_put_uri_path(pktpos, 0, path);
if (response == NULL && type == COAP_TYPE_NON) {
/* all responses (2.xx, 4.xx and 5.xx) are ignored */
pktpos += coap_opt_put_uint(pktpos, COAP_OPT_URI_PATH,
COAP_OPT_NO_RESPONSE, 26);
}
if (len) { if (len) {
/* set payload marker */ /* set payload marker */
*pktpos++ = 0xFF; *pktpos++ = 0xFF;
@ -351,14 +357,32 @@ ssize_t nanocoap_sock_put(nanocoap_sock_t *sock, const char *path,
const void *request, size_t len, const void *request, size_t len,
void *response, size_t len_max) void *response, size_t len_max)
{ {
return _sock_put_post(sock, path, COAP_METHOD_PUT, request, len, response, len_max); return _sock_put_post(sock, path, COAP_METHOD_PUT, COAP_TYPE_CON, request, len,
response, len_max);
} }
ssize_t nanocoap_sock_post(nanocoap_sock_t *sock, const char *path, ssize_t nanocoap_sock_post(nanocoap_sock_t *sock, const char *path,
const void *request, size_t len, const void *request, size_t len,
void *response, size_t len_max) void *response, size_t len_max)
{ {
return _sock_put_post(sock, path, COAP_METHOD_POST, request, len, response, len_max); return _sock_put_post(sock, path, COAP_METHOD_POST, 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)
{
return _sock_put_post(sock, path, COAP_METHOD_PUT, COAP_TYPE_NON, request, len,
response, len_max);
}
ssize_t nanocoap_sock_post_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_POST, COAP_TYPE_NON, request, len,
response, len_max);
} }
static ssize_t _sock_put_post_url(const char *url, unsigned code, static ssize_t _sock_put_post_url(const char *url, unsigned code,
@ -371,7 +395,8 @@ static ssize_t _sock_put_post_url(const char *url, unsigned code,
return res; return res;
} }
res = _sock_put_post(&sock, sock_urlpath(url), code, request, len, response, len_max); res = _sock_put_post(&sock, sock_urlpath(url), code, COAP_TYPE_CON,
request, len, response, len_max);
nanocoap_sock_close(&sock); nanocoap_sock_close(&sock);
return res; return res;