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}()
This commit is contained in:
parent
18d500ed4d
commit
514f039c30
@ -206,7 +206,7 @@ static inline void nanocoap_sock_close(nanocoap_sock_t *sock)
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Simple synchronous CoAP (confirmable) get
|
||||
* @brief Simple synchronous CoAP (confirmable) GET
|
||||
*
|
||||
* @param[in] sock socket to use for the request
|
||||
* @param[in] path remote path
|
||||
@ -219,6 +219,40 @@ static inline void nanocoap_sock_close(nanocoap_sock_t *sock)
|
||||
ssize_t nanocoap_sock_get(nanocoap_sock_t *sock, const char *path, void *buf,
|
||||
size_t len);
|
||||
|
||||
/**
|
||||
* @brief Simple synchronous CoAP (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 on error
|
||||
*/
|
||||
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) 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 on error
|
||||
*/
|
||||
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 Performs a blockwise coap get request on a socket.
|
||||
*
|
||||
|
@ -280,7 +280,7 @@ ssize_t nanocoap_sock_request(sock_udp_t *sock, coap_pkt_t *pkt, size_t len)
|
||||
return nanocoap_sock_request_cb(sock, pkt, _request_cb, &buf);
|
||||
}
|
||||
|
||||
static int _get_cb(void *arg, coap_pkt_t *pkt)
|
||||
static int _get_put_cb(void *arg, coap_pkt_t *pkt)
|
||||
{
|
||||
struct iovec *buf = arg;
|
||||
|
||||
@ -316,7 +316,58 @@ ssize_t nanocoap_sock_get(nanocoap_sock_t *sock, const char *path, void *buf, si
|
||||
pkt.payload = pktpos;
|
||||
pkt.payload_len = 0;
|
||||
|
||||
return nanocoap_sock_request_cb(sock, &pkt, _get_cb, &ctx);
|
||||
return nanocoap_sock_request_cb(sock, &pkt, _get_put_cb, &ctx);
|
||||
}
|
||||
|
||||
ssize_t _sock_put_post(nanocoap_sock_t *sock, const char *path, unsigned code,
|
||||
const void *request, size_t len,
|
||||
void *response, size_t max_len)
|
||||
{
|
||||
/* buffer for CoAP header */
|
||||
uint8_t buffer[CONFIG_NANOCOAP_BLOCK_HEADER_MAX];
|
||||
uint8_t *pktpos = buffer;
|
||||
|
||||
iolist_t payload = {
|
||||
.iol_base = (void *)request,
|
||||
.iol_len = len,
|
||||
};
|
||||
|
||||
coap_pkt_t pkt = {
|
||||
.hdr = (void *)buffer,
|
||||
.snips = &payload,
|
||||
};
|
||||
|
||||
struct iovec ctx = {
|
||||
.iov_base = response,
|
||||
.iov_len = max_len,
|
||||
};
|
||||
|
||||
pktpos += coap_build_hdr(pkt.hdr, COAP_TYPE_CON, NULL, 0, code, _get_id());
|
||||
pktpos += coap_opt_put_uri_path(pktpos, 0, path);
|
||||
|
||||
if (len) {
|
||||
/* set payload marker */
|
||||
*pktpos++ = 0xFF;
|
||||
}
|
||||
|
||||
pkt.payload = pktpos;
|
||||
pkt.payload_len = 0;
|
||||
|
||||
return nanocoap_sock_request_cb(sock, &pkt, response ? _get_put_cb : NULL, &ctx);
|
||||
}
|
||||
|
||||
ssize_t nanocoap_sock_put(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, request, len, response, len_max);
|
||||
}
|
||||
|
||||
ssize_t nanocoap_sock_post(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, request, len, response, len_max);
|
||||
}
|
||||
|
||||
ssize_t nanocoap_request(coap_pkt_t *pkt, sock_udp_ep_t *local,
|
||||
|
Loading…
Reference in New Issue
Block a user