Skip to content

Commit

Permalink
Merge pull request #9156 from kb2ma/gcoap/use_opt_add_api
Browse files Browse the repository at this point in the history
net/gcoap: Use nanocoap pkt/options API
  • Loading branch information
haukepetersen authored Nov 26, 2018
2 parents ac50b4a + 8fa6f10 commit b603c29
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 177 deletions.
5 changes: 3 additions & 2 deletions examples/gcoap/gcoap_cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ static void _resp_handler(unsigned req_state, coap_pkt_t* pdu,
coap_get_code_class(pdu),
coap_get_code_detail(pdu));
if (pdu->payload_len) {
if (pdu->content_type == COAP_FORMAT_TEXT
|| pdu->content_type == COAP_FORMAT_LINK
unsigned content_type = coap_get_content_type(pdu);
if (content_type == COAP_FORMAT_TEXT
|| content_type == COAP_FORMAT_LINK
|| coap_get_code_class(pdu) == COAP_CLASS_CLIENT_FAILURE
|| coap_get_code_class(pdu) == COAP_CLASS_SERVER_FAILURE) {
/* Expecting diagnostic payload in failure cases */
Expand Down
37 changes: 13 additions & 24 deletions sys/include/net/gcoap.h
Original file line number Diff line number Diff line change
Expand Up @@ -252,28 +252,6 @@ extern "C" {
#define GCOAP_PDU_BUF_SIZE (128)
#endif

/**
* @brief Size of the buffer used to write options, other than Uri-Path, in a
* request
*
* Accommodates Content-Format and Uri-Queries
*/
#define GCOAP_REQ_OPTIONS_BUF (40)

/**
* @brief Size of the buffer used to write options in a response
*
* Accommodates Content-Format.
*/
#define GCOAP_RESP_OPTIONS_BUF (8)

/**
* @brief Size of the buffer used to write options in an Observe notification
*
* Accommodates Content-Format and Observe.
*/
#define GCOAP_OBS_OPTIONS_BUF (8)

/**
* @brief Maximum number of requests awaiting a response
*/
Expand Down Expand Up @@ -506,6 +484,11 @@ void gcoap_register_listener(gcoap_listener_t *listener);

/**
* @brief Initializes a CoAP request PDU on a buffer.
* @warning After you use this function, you may not add Options with option
* number less than COAP_OPT_URI_PATH. Otherwise, use the struct-based API
* described with @link net_nanocoap nanocoap @endlink to initialize the
* message. See the implementation of gcoap_req_init() itself as an example.
*
* @param[out] pdu Request metadata
* @param[out] buf Buffer containing the PDU
Expand All @@ -525,8 +508,14 @@ int gcoap_req_init(coap_pkt_t *pdu, uint8_t *buf, size_t len,
/**
* @brief Finishes formatting a CoAP PDU after the payload has been written
*
* Assumes the PDU has been initialized with gcoap_req_init() or
* gcoap_resp_init().
* Assumes the PDU has been initialized with a gcoap_xxx_init() function, like
* gcoap_req_init().
*
* @warning To use this function, you only may have added an Option with
* option number less than COAP_OPT_CONTENT_FORMAT. Otherwise, use the
* struct-based API described with @link net_nanocoap nanocoap. @endlink With
* this API, you specify the format with coap_opt_add_uint(), prepare for the
* payload with coap_opt_finish(), and then write the payload.
*
* @param[in,out] pdu Request metadata
* @param[in] payload_len Length of the payload, or 0 if none
Expand Down
5 changes: 1 addition & 4 deletions sys/include/net/nanocoap.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ extern "C" {
/**
* @brief Nanocoap-specific value to indicate no format specified
*/
#define COAP_FORMAT_NONE (65535)
#define COAP_FORMAT_NONE (UINT16_MAX)

/**
* @name Nanocoap specific maximum values
Expand Down Expand Up @@ -232,9 +232,6 @@ typedef struct {
uint16_t options_len; /**< length of options array */
coap_optpos_t options[NANOCOAP_NOPTS_MAX]; /**< option offset array */
#ifdef MODULE_GCOAP
uint8_t url[NANOCOAP_URI_MAX]; /**< parsed request URL */
uint8_t qs[NANOCOAP_QS_MAX]; /**< parsed query string */
uint16_t content_type; /**< content type */
uint32_t observe_value; /**< observe value */
#endif
} coap_pkt_t;
Expand Down
7 changes: 4 additions & 3 deletions sys/net/application_layer/cord/ep/cord_ep.c
Original file line number Diff line number Diff line change
Expand Up @@ -273,18 +273,19 @@ int cord_ep_register(const sock_udp_ep_t *remote, const char *regif)
}
/* set some packet options and write query string */
coap_hdr_set_type(pkt.hdr, COAP_TYPE_CON);
coap_opt_add_uint(&pkt, COAP_OPT_CONTENT_FORMAT, COAP_FORMAT_LINK);
cord_common_add_qstring(&pkt);

pkt_len = coap_opt_finish(&pkt, COAP_OPT_FINISH_PAYLOAD);

/* add the resource description as payload */
res = gcoap_get_resource_list(pkt.payload, pkt.payload_len,
COAP_FORMAT_LINK);
if (res < 0) {
retval = CORD_EP_ERR;
goto end;
}

/* finish up the packet */
pkt_len = gcoap_finish(&pkt, res, COAP_FORMAT_LINK);
pkt_len += res;

/* send out the request */
res = gcoap_req_send2(buf, pkt_len, remote, _on_register);
Expand Down
Loading

0 comments on commit b603c29

Please sign in to comment.