1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

Merge pull request #18639 from benpicco/nanocoap_sock_put_url

sys/shell: ncput: add option to read from stdin
This commit is contained in:
benpicco 2022-10-06 12:05:39 +02:00 committed by GitHub
commit 4493afb582
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 72 additions and 1 deletions

View File

@ -240,6 +240,22 @@ ssize_t nanocoap_sock_put(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
*
* @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_put_url(const char *url,
const void *request, size_t len,
void *response, size_t len_max);
/**
* @brief Simple synchronous CoAP (confirmable) POST
*
@ -257,6 +273,22 @@ ssize_t nanocoap_sock_post(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
*
* @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_post_url(const char *url,
const void *request, size_t len,
void *response, size_t len_max);
/**
* @brief Performs a blockwise coap get request on a socket.
*

View File

@ -361,6 +361,36 @@ ssize_t nanocoap_sock_post(nanocoap_sock_t *sock, const char *path,
return _sock_put_post(sock, path, COAP_METHOD_POST, 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)
{
nanocoap_sock_t sock;
int res = nanocoap_sock_url_connect(url, &sock);
if (res) {
return res;
}
res = _sock_put_post(&sock, sock_urlpath(url), code, request, len, response, len_max);
nanocoap_sock_close(&sock);
return res;
}
ssize_t nanocoap_sock_put_url(const char *url,
const void *request, size_t len,
void *response, size_t len_max)
{
return _sock_put_post_url(url, COAP_METHOD_PUT, request, len, response, len_max);
}
ssize_t nanocoap_sock_post_url(const char *url,
const void *request, size_t len,
void *response, size_t len_max)
{
return _sock_put_post_url(url, COAP_METHOD_POST, request, len, response, len_max);
}
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

@ -184,7 +184,16 @@ static int _nanocoap_put_handler(int argc, char **argv)
url = buffer;
}
res = nanocoap_vfs_put_url(url, file, work_buf, sizeof(work_buf));
if (strcmp(file, "-") == 0) {
if (argc < 4) {
printf("Usage: %s - <url> <data>\n", argv[0]);
return -EINVAL;
}
res = nanocoap_sock_put_url(url, argv[3], strlen(argv[3]), NULL, 0);
} else {
res = nanocoap_vfs_put_url(url, file, work_buf, sizeof(work_buf));
}
if (res < 0) {
printf("Upload failed: %s\n", strerror(-res));
}