1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-01-17 05:12:57 +01:00

Merge pull request #20852 from fabian18/nanocoap_sock_get_non

sys/net/application_layer/nanocoap: add `nanocoap_sock_get_non()`
This commit is contained in:
Teufelchen 2024-09-10 15:06:27 +00:00 committed by GitHub
commit e31e814cff
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 77 additions and 14 deletions

View File

@ -384,11 +384,25 @@ static inline void nanocoap_sock_close(nanocoap_sock_t *sock)
* @param[in] len length of @p buffer
*
* @returns length of response payload on success
* @returns <0 on error
* @returns @see nanocoap_sock_request_cb on error
*/
ssize_t nanocoap_sock_get(nanocoap_sock_t *sock, const char *path, void *buf,
size_t len);
/**
* @brief Simple non-confirmable GET
*
* @param[in] sock socket to use for the request
* @param[in] path remote path and query
* @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 @see nanocoap_sock_request_cb on error
*/
ssize_t nanocoap_sock_get_non(nanocoap_sock_t *sock, const char *path,
void *response, size_t len_max);
/**
* @brief Simple synchronous CoAP (confirmable) PUT
*
@ -400,7 +414,7 @@ ssize_t nanocoap_sock_get(nanocoap_sock_t *sock, const char *path, void *buf,
* @param[in] len_max length of @p response
*
* @returns length of response payload on success
* @returns <0 on error
* @returns @see nanocoap_sock_request_cb on error
*/
ssize_t nanocoap_sock_put(nanocoap_sock_t *sock, const char *path,
const void *request, size_t len,
@ -419,7 +433,7 @@ ssize_t nanocoap_sock_put(nanocoap_sock_t *sock, const char *path,
* @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
* @returns @see nanocoap_sock_request_cb on error
*/
ssize_t nanocoap_sock_put_non(nanocoap_sock_t *sock, const char *path,
const void *request, size_t len,
@ -452,7 +466,7 @@ ssize_t nanocoap_sock_put_url(const char *url,
* @param[in] len_max length of @p response
*
* @returns length of response payload on success
* @returns <0 on error
* @returns @see nanocoap_sock_request_cb on error
*/
ssize_t nanocoap_sock_post(nanocoap_sock_t *sock, const char *path,
const void *request, size_t len,
@ -471,7 +485,7 @@ ssize_t nanocoap_sock_post(nanocoap_sock_t *sock, const char *path,
* @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
* @returns @see nanocoap_sock_request_cb on error
*/
ssize_t nanocoap_sock_post_non(nanocoap_sock_t *sock, const char *path,
const void *request, size_t len,
@ -505,7 +519,7 @@ ssize_t nanocoap_sock_post_url(const char *url,
* @param[in] len_max length of @p response
*
* @returns length of response payload on success
* @returns <0 on error
* @returns @see nanocoap_sock_request_cb on error
*/
ssize_t nanocoap_sock_fetch(nanocoap_sock_t *sock, const char *path,
const void *request, size_t len,
@ -525,7 +539,7 @@ ssize_t nanocoap_sock_fetch(nanocoap_sock_t *sock, const char *path,
* @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
* @returns @see nanocoap_sock_request_cb on error
*/
ssize_t nanocoap_sock_fetch_non(nanocoap_sock_t *sock, const char *path,
const void *request, size_t len,
@ -555,7 +569,7 @@ ssize_t nanocoap_sock_fetch_url(const char *url,
* @param[in] path remote path (with query) to delete
*
* @returns 0 on success
* @returns <0 on error
* @returns @see nanocoap_sock_request_cb on error
*/
ssize_t nanocoap_sock_delete(nanocoap_sock_t *sock, const char *path);
@ -702,7 +716,15 @@ ssize_t nanocoap_sock_request(nanocoap_sock_t *sock, coap_pkt_t *pkt, size_t len
* @param[in] arg Optional callback argumnent
*
* @returns length of response on success
* @returns <0 on error
* @returns 0 for a request for which no response is expected, indicated by @p cb == NULL,
* or for a 2.xx response
* @returns -ETIMEDOUT, if no matching ACK or no response was received
* @returns -EBADMSG, if a matching RST was received
* @returns -ENXIO, if @p cb == NULL and the response indicates a 4.xx client error
* @returns -ENETRESET, if @p cb == NULL and the response indicates a 5.xx server error
* @returns any error on @see sock_udp_sendv or @see sock_dtls_sendv
* @returns any error on @see sock_udp_recv_buf or @see sock_dtls_recv_buf
* @returns any return value of @p cb for a matching response
*/
ssize_t nanocoap_sock_request_cb(nanocoap_sock_t *sock, coap_pkt_t *pkt,
coap_request_cb_t cb, void *arg);
@ -718,7 +740,7 @@ ssize_t nanocoap_sock_request_cb(nanocoap_sock_t *sock, coap_pkt_t *pkt,
* request
*
* @returns length of response on success
* @returns <0 on error
* @returns @see nanocoap_sock_request_cb on error
*/
ssize_t nanocoap_request(coap_pkt_t *pkt, const sock_udp_ep_t *local,
const sock_udp_ep_t *remote, size_t len);

View File

@ -390,7 +390,9 @@ static int _get_put_cb(void *arg, coap_pkt_t *pkt)
return pkt->payload_len;
}
ssize_t nanocoap_sock_get(nanocoap_sock_t *sock, const char *path, void *buf, size_t len)
static ssize_t _sock_get(nanocoap_sock_t *sock, const char *path,
uint8_t type,
void *response, size_t max_len)
{
/* buffer for CoAP header */
uint8_t buffer[CONFIG_NANOCOAP_BLOCK_HEADER_MAX];
@ -401,11 +403,11 @@ ssize_t nanocoap_sock_get(nanocoap_sock_t *sock, const char *path, void *buf, si
};
struct iovec ctx = {
.iov_base = buf,
.iov_len = len,
.iov_base = response,
.iov_len = max_len,
};
pktpos += coap_build_hdr(pkt.hdr, COAP_TYPE_CON, NULL, 0, COAP_METHOD_GET,
pktpos += coap_build_hdr(pkt.hdr, type, NULL, 0, COAP_METHOD_GET,
nanocoap_sock_next_msg_id(sock));
pktpos += coap_opt_put_uri_pathquery(pktpos, NULL, path);
@ -415,6 +417,11 @@ ssize_t nanocoap_sock_get(nanocoap_sock_t *sock, const char *path, void *buf, si
return nanocoap_sock_request_cb(sock, &pkt, _get_put_cb, &ctx);
}
ssize_t nanocoap_sock_get(nanocoap_sock_t *sock, const char *path, void *buf, size_t len)
{
return _sock_get(sock, path, COAP_TYPE_CON, buf, len);
}
ssize_t _sock_put_post(nanocoap_sock_t *sock, const char *path, unsigned code,
uint8_t type, const void *request, size_t len,
void *response, size_t max_len)
@ -507,6 +514,12 @@ ssize_t nanocoap_sock_fetch_non(nanocoap_sock_t *sock, const char *path,
response, len_max);
}
ssize_t nanocoap_sock_get_non(nanocoap_sock_t *sock, const char *path,
void *response, size_t len_max)
{
return _sock_get(sock, path, COAP_TYPE_NON, 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)

View File

@ -53,6 +53,7 @@ extern int nanotest_client_url_cmd(int argc, char **argv);
extern int nanotest_server_cmd(int argc, char **argv);
extern int nanotest_client_put_cmd(int argc, char **argv);
extern int nanotest_client_put_non_cmd(int argc, char **argv);
extern int nanotest_client_get_non_cmd(int argc, char **argv);
static int _list_all_inet6(int argc, char **argv);
static const shell_command_t shell_commands[] = {
@ -60,6 +61,7 @@ static const shell_command_t shell_commands[] = {
{ "url", "CoAP client URL request", nanotest_client_url_cmd },
{ "put", "experimental put", nanotest_client_put_cmd },
{ "put_non", "non-confirmable put", nanotest_client_put_non_cmd },
{ "get_non", "non-confirmable get", nanotest_client_get_non_cmd },
{ "server", "CoAP server", nanotest_server_cmd },
{ "inet6", "IPv6 addresses", _list_all_inet6 },
{ NULL, NULL, NULL }

View File

@ -322,3 +322,29 @@ int nanotest_client_put_non_cmd(int argc, char **argv)
return res;
}
int nanotest_client_get_non_cmd(int argc, char **argv)
{
int res;
uint8_t response[CONFIG_NANOCOAP_BLOCKSIZE_DEFAULT];
if (argc < 2) {
printf("usage: %s <url>\n", argv[0]);
return 1;
}
nanocoap_sock_t sock;
nanocoap_sock_url_connect(argv[1], &sock);
res = nanocoap_sock_get_non(&sock, sock_urlpath(argv[1]), response, sizeof(response));
nanocoap_sock_close(&sock);
if (res >= 0) {
od_hex_dump(response, res, OD_WIDTH_DEFAULT);
}
else {
printf("error: %d\n", res);
}
return res;
}