Skip to content

Commit

Permalink
nanocoap_sock: add nanocoap_sock_put()
Browse files Browse the repository at this point in the history
  • Loading branch information
benpicco committed Aug 25, 2022
1 parent 18d500e commit f8dd952
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 12 deletions.
17 changes: 16 additions & 1 deletion sys/include/net/nanocoap_sock.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -219,6 +219,21 @@ 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, out] buf buffer containing the payload, will be used for response
* @param[in] len length of the payload to send
* @param[in] len_max length of @p buffer
*
* @returns length of response payload on success
* @returns <0 on error
*/
ssize_t nanocoap_sock_put(nanocoap_sock_t *sock, const char *path, void *buf,
size_t len, size_t len_max);

/**
* @brief Performs a blockwise coap get request on a socket.
*
Expand Down
45 changes: 34 additions & 11 deletions sys/net/application_layer/nanocoap/sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -298,25 +298,48 @@ static int _get_cb(void *arg, coap_pkt_t *pkt)
return pkt->payload_len;
}

static ssize_t _sock_req_simple(nanocoap_sock_t *sock, const char *path, unsigned code,
coap_pkt_t *pkt, size_t buffer_len)
{
uint8_t *pktpos = (void *)pkt->hdr;

struct iovec ctx = {
.iov_base = &pkt->hdr,
.iov_len = buffer_len,
};

pktpos += coap_build_hdr(pkt->hdr, COAP_TYPE_CON, NULL, 0, code, _get_id());
pktpos += coap_opt_put_uri_path(pktpos, 0, path);

pkt->payload = pktpos;
pkt->payload_len = 0;

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)
{
uint8_t *pktpos = buf;
coap_pkt_t pkt = {
.hdr = buf,
};

struct iovec ctx = {
.iov_base = buf,
.iov_len = len,
};
return _sock_req_simple(sock, path, COAP_METHOD_GET, &pkt, len);
}

pktpos += coap_build_hdr(pkt.hdr, COAP_TYPE_CON, NULL, 0, COAP_METHOD_GET, _get_id());
pktpos += coap_opt_put_uri_path(pktpos, 0, path);
ssize_t nanocoap_sock_put(nanocoap_sock_t *sock, const char *path,
void *buf, size_t len, size_t len_max)
{
iolist_t payload = {
.iol_base = buf,
.iol_len = len,
};

pkt.payload = pktpos;
pkt.payload_len = 0;
coap_pkt_t pkt = {
.hdr = buf,
.snips = &payload,
};

return nanocoap_sock_request_cb(sock, &pkt, _get_cb, &ctx);
return _sock_req_simple(sock, path, COAP_METHOD_PUT, &pkt, len_max);
}

ssize_t nanocoap_request(coap_pkt_t *pkt, sock_udp_ep_t *local,
Expand Down

0 comments on commit f8dd952

Please sign in to comment.