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 d7c4268
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
16 changes: 15 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,20 @@ 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[out] buf buffer to write response to
* @param[in] len 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);

/**
* @brief Performs a blockwise coap get request on a socket.
*
Expand Down
19 changes: 15 additions & 4 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,7 +298,8 @@ static int _get_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_req_simple(nanocoap_sock_t *sock, const char *path, unsigned code,
void *buf, size_t len)
{
uint8_t *pktpos = buf;
coap_pkt_t pkt = {
Expand All @@ -310,13 +311,23 @@ ssize_t nanocoap_sock_get(nanocoap_sock_t *sock, const char *path, void *buf, si
.iov_len = len,
};

pktpos += coap_build_hdr(pkt.hdr, COAP_TYPE_CON, NULL, 0, COAP_METHOD_GET, _get_id());
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_cb, &ctx);
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_req_simple(sock, path, COAP_METHOD_GET, buf, len);
}

ssize_t nanocoap_sock_put(nanocoap_sock_t *sock, const char *path, void *buf, size_t len)
{
return _sock_req_simple(sock, path, COAP_METHOD_PUT, buf, len);
}

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

0 comments on commit d7c4268

Please sign in to comment.