Skip to content

Commit

Permalink
refined internal state handling
Browse files Browse the repository at this point in the history
  • Loading branch information
smlng committed Aug 29, 2016
1 parent fcfcfde commit 02f4a4f
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 110 deletions.
53 changes: 29 additions & 24 deletions coap.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ static void _option_nibble(const uint32_t value, uint8_t *nibble)
}

/* --- PUBLIC --------------------------------------------------------------- */
int coap_build(const coap_packet_t *pkt, uint8_t *buf, size_t *buflen)
coap_state_t coap_build(const coap_packet_t *pkt, uint8_t *buf, size_t *buflen)
{
// build header
if (*buflen < (sizeof(coap_raw_header_t) + pkt->hdr.tkl)) {
Expand Down Expand Up @@ -128,10 +128,13 @@ int coap_build(const coap_packet_t *pkt, uint8_t *buf, size_t *buflen)
}
return COAP_SUCCESS;
}
int coap_make_request(const uint16_t msgid, const coap_buffer_t* tok,
const coap_resource_t *resource,
const uint8_t *content, const size_t content_len,
coap_packet_t *pkt)

coap_state_t coap_make_request(const uint16_t msgid,
const coap_buffer_t* tok,
const coap_resource_t *resource,
const uint8_t *content,
const size_t content_len,
coap_packet_t *pkt)
{
const coap_resource_path_t *path = resource->path;
const coap_msgtype_t msg_type = resource->msg_type;
Expand Down Expand Up @@ -172,23 +175,25 @@ int coap_make_request(const uint16_t msgid, const coap_buffer_t* tok,
// attach payload
pkt->payload.p = content;
pkt->payload.len = content_len;
return COAP_STATE_REQ_SEND;
return COAP_REQ_SEND;
}

int coap_make_ack(const coap_packet_t *inpkt, coap_packet_t *pkt)
coap_state_t coap_make_ack(const coap_packet_t *inpkt, coap_packet_t *pkt)
{
printf("coap_make_ack\n");
return coap_make_response(inpkt->hdr.id, &inpkt->tok,
COAP_TYPE_ACK, COAP_RSPCODE_EMPTY,
NULL, NULL, 0, pkt);
}

int coap_make_response(const uint16_t msgid, const coap_buffer_t* tok,
const coap_msgtype_t msgtype,
const coap_responsecode_t rspcode,
const uint8_t *content_type,
const uint8_t *content, const size_t content_len,
coap_packet_t *pkt)
coap_state_t coap_make_response(const uint16_t msgid,
const coap_buffer_t* tok,
const coap_msgtype_t msgtype,
const coap_responsecode_t rspcode,
const uint8_t *content_type,
const uint8_t *content,
const size_t content_len,
coap_packet_t *pkt)
{
pkt->hdr.ver = COAP_VERSION;
pkt->hdr.t = msgtype;
Expand All @@ -211,13 +216,13 @@ int coap_make_response(const uint16_t msgid, const coap_buffer_t* tok,
pkt->payload.p = content;
pkt->payload.len = content_len;
if ((msgtype == COAP_TYPE_ACK) && (rspcode == COAP_RSPCODE_EMPTY))
return COAP_STATE_ACK_SEND;
return COAP_STATE_RSP_SEND;
return COAP_ACK_SEND;
return COAP_RSP_SEND;
}

int coap_handle_request(coap_resource_t *resources,
const coap_packet_t *inpkt,
coap_packet_t *pkt)
coap_state_t coap_handle_request(coap_resource_t *resources,
const coap_packet_t *inpkt,
coap_packet_t *pkt)
{
uint8_t count;
coap_responsecode_t rspcode = COAP_RSPCODE_NOT_IMPLEMENTED;
Expand All @@ -235,7 +240,7 @@ int coap_handle_request(coap_resource_t *resources,
}
}
if (i == count) { // matching resource found
if ((inpkt->hdr.t == COAP_TYPE_CON) && (rs->msg_type != COAP_TYPE_ACK) && (rs->state != COAP_STATE_ACK_SEND)) { // no piggyback
if ((inpkt->hdr.t == COAP_TYPE_CON) && (rs->msg_type != COAP_TYPE_ACK) && (rs->state != COAP_ACK_SEND)) { // no piggyback
rs->state = coap_make_ack(inpkt, pkt);
}
else {
Expand All @@ -254,9 +259,9 @@ int coap_handle_request(coap_resource_t *resources,
NULL, NULL, 0, pkt);
}

int coap_handle_response(coap_resource_t *resources,
const coap_packet_t *reqpkt,
coap_packet_t *rsppkt)
coap_state_t coap_handle_response(coap_resource_t *resources,
const coap_packet_t *reqpkt,
coap_packet_t *rsppkt)
{
if (reqpkt->hdr.id != rsppkt->hdr.id )
return COAP_ERR_REQUEST_MSGID_MISMATCH;
Expand Down Expand Up @@ -288,8 +293,8 @@ int coap_handle_response(coap_resource_t *resources,
return COAP_ERR_REQUEST_NOT_FOUND;
}

int coap_make_link_format(const coap_resource_t *resources,
char *buf, size_t buflen)
coap_state_t coap_make_link_format(const coap_resource_t *resources,
char *buf, size_t buflen)
{
if (buflen < 4) { // <>;
return COAP_ERR_BUFFER_TOO_SMALL;
Expand Down
112 changes: 57 additions & 55 deletions coap.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,42 +207,38 @@ typedef enum
///////////////////////

/**
* Definition of (internal) error codes
* Definition of (internal) states and error codes
*/
typedef enum
{
COAP_ERR_NONE = 0,
COAP_ERR_HEADER_TOO_SHORT = 1,
COAP_ERR_VERSION_NOT_1 = 2,
COAP_ERR_TOKEN_TOO_SHORT = 3,
COAP_ERR_OPTION_TOO_SHORT_FOR_HEADER = 4,
COAP_ERR_OPTION_TOO_SHORT = 5,
COAP_ERR_OPTION_OVERRUNS_PACKET = 6,
COAP_ERR_OPTION_TOO_BIG = 7,
COAP_ERR_OPTION_LEN_INVALID = 8,
COAP_ERR_BUFFER_TOO_SMALL = 9,
COAP_ERR_UNSUPPORTED = 10,
COAP_ERR_OPTION_DELTA_INVALID = 11,
COAP_ERR_OPTION_NOT_FOUND = 12,
typedef enum {
COAP_SUCCESS = 0,
COAP_RDY,
COAP_ACK_RECV,
COAP_ACK_SEND,
COAP_ACK_WAIT,
COAP_RSP_RECV,
COAP_RSP_SEND,
COAP_RSP_WAIT,
COAP_REQ_RECV,
COAP_REQ_SEND,
COAP_REQ_WAIT,
COAP_ERR = 100,
COAP_ERR_HEADER_TOO_SHORT,
COAP_ERR_VERSION_NOT_1,
COAP_ERR_TOKEN_TOO_SHORT,
COAP_ERR_OPTION_TOO_SHORT_FOR_HEADER,
COAP_ERR_OPTION_TOO_SHORT,
COAP_ERR_OPTION_OVERRUNS_PACKET,
COAP_ERR_OPTION_TOO_BIG,
COAP_ERR_OPTION_LEN_INVALID,
COAP_ERR_BUFFER_TOO_SMALL,
COAP_ERR_UNSUPPORTED,
COAP_ERR_OPTION_DELTA_INVALID,
COAP_ERR_OPTION_NOT_FOUND,
COAP_ERR_REQUEST_NOT_FOUND,
COAP_ERR_REQUEST_MSGID_MISMATCH,
COAP_ERR_REQUEST_TOKEN_MISMATCH,
COAP_ERR_RESPONSE,
COAP_ERR_MAX = 99,
} coap_error_t;
#define COAP_SUCCESS COAP_ERR_NONE //!< Success return value if no error occured

typedef enum {
COAP_STATE_RDY = (COAP_ERR_MAX + 1),
COAP_STATE_ACK_RECV,
COAP_STATE_ACK_SEND,
COAP_STATE_ACK_WAIT,
COAP_STATE_RSP_RECV,
COAP_STATE_RSP_SEND,
COAP_STATE_RSP_WAIT,
COAP_STATE_REQ_RECV,
COAP_STATE_REQ_SEND,
COAP_STATE_REQ_WAIT,
COAP_ERR_MAX, // this has to be the last error code
} coap_state_t;


Expand Down Expand Up @@ -296,7 +292,8 @@ struct coap_resource
* @param[in] ct Content type given as uint16_t
* @return array with content type
*/
#define COAP_SET_CONTENTTYPE(ct) {((int16_t)ct & 0xFF00) >> 8, ((int16_t)ct & 0x00FF)}
#define COAP_SET_CONTENTTYPE(ct) {((int16_t)ct & 0xFF00) >> 8, \
((int16_t)ct & 0x00FF)}

/**
* @brief Get content type
Expand All @@ -317,9 +314,11 @@ struct coap_resource
* @param[in] buflen The lenth of \p buf in bytes.
* @param[out] pkt The coap_packet_t structure to be filled.
*
* @return 0 on success, or the according coap_error_t
* @return 0 on success, or the according coap_state_t
*/
int coap_parse(const uint8_t *buf, const size_t buflen, coap_packet_t *pkt);
coap_state_t coap_parse(const uint8_t *buf,
const size_t buflen,
coap_packet_t *pkt);

/**
* @brief Writes CoAP packet/message to transmission buffer
Expand All @@ -342,7 +341,7 @@ int coap_parse(const uint8_t *buf, const size_t buflen, coap_packet_t *pkt);
* token length specified in the buffer that actually holds the
* tokens
*/
int coap_build(const coap_packet_t *pkt, uint8_t *buf, size_t *buflen);
coap_state_t coap_build(const coap_packet_t *pkt, uint8_t *buf, size_t *buflen);

/**
* @brief Create CoAP acknowledgement
Expand All @@ -356,15 +355,15 @@ int coap_build(const coap_packet_t *pkt, uint8_t *buf, size_t *buflen);
*
* @return Always returns 0.
*/
int coap_make_ack(const coap_packet_t *inpkt, coap_packet_t *pkt);
coap_state_t coap_make_ack(const coap_packet_t *inpkt, coap_packet_t *pkt);

/**
*
*/
int coap_make_request(const uint16_t msgid, const coap_buffer_t* tok,
const coap_resource_t *resource,
const uint8_t *content, const size_t content_len,
coap_packet_t *pkt);
coap_state_t coap_make_request(const uint16_t msgid, const coap_buffer_t* tok,
const coap_resource_t *resource,
const uint8_t *content, const size_t content_len,
coap_packet_t *pkt);

/**
* @brief Create a CoAP response packet
Expand All @@ -384,12 +383,14 @@ int coap_make_request(const uint16_t msgid, const coap_buffer_t* tok,
* @return 0 on success, or COAP_ERR_BUFFER_TOO_SMALL if the length of the
* buffer pointed to by scratch is smaller than 2.
*/
int coap_make_response(const uint16_t msgid, const coap_buffer_t* tok,
const coap_msgtype_t msgtype,
const coap_responsecode_t rspcode,
const uint8_t *content_type,
const uint8_t *content, const size_t content_len,
coap_packet_t *pkt);
coap_state_t coap_make_response(const uint16_t msgid,
const coap_buffer_t* tok,
const coap_msgtype_t msgtype,
const coap_responsecode_t rspcode,
const uint8_t *content_type,
const uint8_t *content,
const size_t content_len,
coap_packet_t *pkt);

/**
* @brief Handle incoming CoAP request
Expand All @@ -405,14 +406,15 @@ int coap_make_response(const uint16_t msgid, const coap_buffer_t* tok,
*
* @return 0 on success, or a reasonable error code on failure.
*/
int coap_handle_request(coap_resource_t *resources,
const coap_packet_t *inpkt,
coap_packet_t *pkt);
coap_state_t coap_handle_request(coap_resource_t *resources,
const coap_packet_t *inpkt,
coap_packet_t *pkt);

coap_state_t coap_handle_response(coap_resource_t *resources,
const coap_packet_t *reqpkt,
coap_packet_t *rsppkt);

int coap_handle_response(coap_resource_t *resources,
const coap_packet_t *reqpkt,
coap_packet_t *rsppkt);
int coap_handle_packet();
coap_state_t coap_handle_packet();

/**
* @brief Create link format of resources
Expand All @@ -424,8 +426,8 @@ int coap_handle_packet();
*
* @return 0 on success, or COAP_ERR_BUFFER_TOO_SMALL if buflen is exceeded.
*/
int coap_make_link_format(const coap_resource_t *resources,
char *buf, size_t buflen);
coap_state_t coap_make_link_format(const coap_resource_t *resources,
char *buf, size_t buflen);

#ifdef __cplusplus
}
Expand Down
46 changes: 29 additions & 17 deletions coap_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,23 @@
#include "coap.h"

/* --- PRIVATE -------------------------------------------------------------- */
static int _parse_token(const uint8_t *buf, const size_t buflen,
coap_packet_t *pkt);
static int _parse_header(const uint8_t *buf, const size_t buflen,
coap_header_t *hdr);
static int _parse_options_payload(const uint8_t *buf, const size_t buflen,
coap_packet_t *pkt);
static int _parse_option(const uint8_t **buf, const size_t buflen,
coap_option_t *option, uint16_t *running_delta);
static coap_state_t _parse_token(const uint8_t *buf,
const size_t buflen,
coap_packet_t *pkt);
static coap_state_t _parse_header(const uint8_t *buf,
const size_t buflen,
coap_header_t *hdr);
static coap_state_t _parse_options_payload(const uint8_t *buf,
const size_t buflen,
coap_packet_t *pkt);
static coap_state_t _parse_option(const uint8_t **buf,
const size_t buflen,
coap_option_t *option,
uint16_t *running_delta);

static int _parse_header(const uint8_t *buf, const size_t buflen,
coap_header_t *hdr)
static coap_state_t _parse_header(const uint8_t *buf,
const size_t buflen,
coap_header_t *hdr)
{
if (buflen < sizeof(coap_raw_header_t)) {
return COAP_ERR_HEADER_TOO_SHORT;
Expand All @@ -34,8 +40,9 @@ static int _parse_header(const uint8_t *buf, const size_t buflen,
return COAP_SUCCESS;
}

static int _parse_token(const uint8_t *buf, const size_t buflen,
coap_packet_t *pkt)
static coap_state_t _parse_token(const uint8_t *buf,
const size_t buflen,
coap_packet_t *pkt)
{
coap_buffer_t *tok = &pkt->tok;
int toklen = pkt->hdr.tkl;
Expand All @@ -53,8 +60,10 @@ static int _parse_token(const uint8_t *buf, const size_t buflen,
return COAP_SUCCESS;
}

static int _parse_option(const uint8_t **buf, const size_t buflen,
coap_option_t *option, uint16_t *running_delta)
static coap_state_t _parse_option(const uint8_t **buf,
const size_t buflen,
coap_option_t *option,
uint16_t *running_delta)
{
const uint8_t *p = *buf;
uint8_t headlen = 1;
Expand Down Expand Up @@ -121,8 +130,9 @@ static int _parse_option(const uint8_t **buf, const size_t buflen,
}

// http://tools.ietf.org/html/rfc7252#section-3.1
static int _parse_options_payload(const uint8_t *buf, const size_t buflen,
coap_packet_t *pkt)
static coap_state_t _parse_options_payload(const uint8_t *buf,
const size_t buflen,
coap_packet_t *pkt)
{
size_t optionIndex = 0;
uint16_t delta = 0;
Expand Down Expand Up @@ -155,7 +165,9 @@ static int _parse_options_payload(const uint8_t *buf, const size_t buflen,
}

/* --- PUBLIC --------------------------------------------------------------- */
int coap_parse(const uint8_t *buf, const size_t buflen, coap_packet_t *pkt)
coap_state_t coap_parse(const uint8_t *buf,
const size_t buflen,
coap_packet_t *pkt)
{
int rc;
/* parse header, token, options, and payload */
Expand Down
6 changes: 3 additions & 3 deletions example/resources.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ static int handle_put_light(const coap_resource_t *resource,

coap_resource_t resources[] =
{
{COAP_STATE_RDY, COAP_METHOD_GET, COAP_TYPE_ACK,
{COAP_RDY, COAP_METHOD_GET, COAP_TYPE_ACK,
handle_get_well_known_core, &path_well_known_core,
COAP_SET_CONTENTTYPE(COAP_CONTENTTYPE_APP_LINKFORMAT)},
{COAP_STATE_RDY, COAP_METHOD_GET, COAP_TYPE_ACK,
{COAP_RDY, COAP_METHOD_GET, COAP_TYPE_ACK,
handle_get_light, &path_light,
COAP_SET_CONTENTTYPE(COAP_CONTENTTYPE_TXT_PLAIN)},
{COAP_STATE_RDY, COAP_METHOD_PUT, COAP_TYPE_ACK,
{COAP_RDY, COAP_METHOD_PUT, COAP_TYPE_ACK,
handle_put_light, &path_light,
COAP_SET_CONTENTTYPE(COAP_CONTENTTYPE_NONE)},
{(coap_state_t)0, (coap_method_t)0, (coap_msgtype_t)0,
Expand Down
Loading

0 comments on commit 02f4a4f

Please sign in to comment.