Skip to content

Commit

Permalink
Merge pull request #6 from SenthilKumarGS/develop
Browse files Browse the repository at this point in the history
Added CoAP over WebSocket PDU support.
  • Loading branch information
dthaler authored May 30, 2017
2 parents 577804c + 2f6ff0c commit f4d20bc
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 10 deletions.
22 changes: 19 additions & 3 deletions include/coap/pdu.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ typedef int coap_tid_t;
*/
#define COAP_DROPPED_RESPONSE -2

#define COAP_WS_HEADER 2

#define COAP_TCP_HEADER_NO_FIELD 2
#define COAP_TCP_HEADER_8_BIT 3
#define COAP_TCP_HEADER_16_BIT 4
Expand Down Expand Up @@ -207,7 +209,10 @@ typedef enum {
COAP_TCP,
COAP_TCP_8BIT,
COAP_TCP_16BIT,
COAP_TCP_32BIT
COAP_TCP_32BIT,
#endif
#ifdef WITH_WS
COAP_WS
#endif
} coap_transport_t;

Expand Down Expand Up @@ -255,14 +260,24 @@ typedef struct {
} coap_hdr_tcp_32bit_t;
#endif /* WITH_TCP */

#ifdef WITH_WS
typedef struct {
unsigned char header_data[COAP_WS_HEADER];
unsigned char token[]; /* the actual token, if any */
} coap_hdr_ws_t;
#endif /* WITH_WS */

typedef union {
coap_hdr_udp_t udp;
#ifdef WITH_TCP
coap_hdr_tcp_t tcp;
coap_hdr_tcp_t tcp;
coap_hdr_tcp_8bit_t tcp_8bit;
coap_hdr_tcp_16bit_t tcp_16bit;
coap_hdr_tcp_32bit_t tcp_32bit;
#endif /* WITH_TCP */
#ifdef WITH_WS
coap_hdr_ws_t ws;
#endif /* WITH_WS */
} coap_hdr_transport_t;

// Typedef for backwards compatibility.
Expand Down Expand Up @@ -534,6 +549,8 @@ unsigned int coap_get_tcp_header_length(unsigned char *data);
*/
unsigned int coap_get_tcp_header_length_for_transport(coap_transport_t transport);

#endif /* WITH_TCP */

/**
* Get option length.
*
Expand All @@ -542,7 +559,6 @@ unsigned int coap_get_tcp_header_length_for_transport(coap_transport_t transport
* @return total option length
*/
size_t coap_get_opt_header_length(unsigned short key, size_t length);
#endif /* WITH_TCP */

/**
* Add code in coap header.
Expand Down
14 changes: 14 additions & 0 deletions src/option.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ coap_option_iterator_init2(coap_pdu_t *pdu, coap_opt_iterator_t *oi,
token_length = pdu->transport_hdr->tcp_32bit.header_data[0] & 0x0f;
headerSize = COAP_TCP_HEADER_32_BIT;
break;
#endif
#ifdef WITH_WS
case COAP_WS:
token_length = pdu->transport_hdr->ws.header_data[0] & 0x0f;
headerSize = COAP_WS_HEADER;
break;
#endif
default:
token_length = pdu->transport_hdr->udp.token_length;
Expand All @@ -166,6 +172,14 @@ coap_option_iterator_init2(coap_pdu_t *pdu, coap_opt_iterator_t *oi,
return NULL;
}
}
#ifdef WITH_WS
else if (COAP_WS == transport) {
if ((unsigned char *) &(pdu->transport_hdr->ws) + pdu->length <= oi->next_option) {
oi->bad = 1;
return NULL;
}
}
#endif
#ifdef WITH_TCP
else {
if ((unsigned char *) &(pdu->transport_hdr->tcp) + pdu->length <= oi->next_option) {
Expand Down
68 changes: 61 additions & 7 deletions src/pdu.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ coap_pdu_clear2(coap_pdu_t *pdu, size_t size, coap_transport_t transport, unsign
/* data is NULL unless explicitly set by coap_add_data() */
pdu->length = sizeof(coap_hdr_t);
}
#ifdef WITH_TCP
#if defined(WITH_TCP) || defined(WITH_WS)
else {
/* data is NULL unless explicitly set by coap_add_data() */
pdu->length = (unsigned short)length;
Expand Down Expand Up @@ -124,14 +124,19 @@ coap_pdu_init2(unsigned char type, unsigned char code, unsigned short id,
case COAP_TCP_32BIT:
length = COAP_TCP_HEADER_32_BIT;
break;
#endif
#ifdef WITH_WS
case COAP_WS:
length = COAP_WS_HEADER;
break;
#endif
default:
debug("it has wrong type\n");
}

assert(length <= UINT_MAX);

#ifndef WITH_TCP
#if !defined(WITH_TCP) && !defined(WITH_WS)
assert(size <= COAP_MAX_PDU_SIZE);
/* Size must be large enough to fit the header. */
if (size < length || size > COAP_MAX_PDU_SIZE)
Expand Down Expand Up @@ -185,6 +190,12 @@ coap_pdu_init2(unsigned char type, unsigned char code, unsigned short id,
pdu->transport_hdr->tcp_32bit.header_data[0] = COAP_TCP_LENGTH_FIELD_NUM_32_BIT << 4;
pdu->transport_hdr->tcp_32bit.header_data[5] = code;
break;
#endif
#ifdef WITH_WS
case COAP_WS:
pdu->transport_hdr->ws.header_data[0] = 0;
pdu->transport_hdr->ws.header_data[1] = code;
break;
#endif
default:
debug("it has wrong type\n");
Expand Down Expand Up @@ -417,6 +428,7 @@ coap_get_tcp_header_length_for_transport(coap_transport_t transport) {

return length;
}
#endif

size_t
coap_get_opt_header_length(unsigned short key, size_t length) {
Expand Down Expand Up @@ -447,7 +459,6 @@ coap_get_opt_header_length(unsigned short key, size_t length) {

return headerLength;
}
#endif

void
coap_add_code(const coap_pdu_t *pdu, coap_transport_t transport, unsigned int code) {
Expand All @@ -473,6 +484,11 @@ coap_add_code(const coap_pdu_t *pdu, coap_transport_t transport, unsigned int co
case COAP_TCP_32BIT:
pdu->transport_hdr->tcp_32bit.header_data[5] = coap_code;
break;
#endif
#ifdef WITH_WS
case COAP_WS:
pdu->transport_hdr->ws.header_data[1] = coap_code;
break;
#endif
default:
debug("it has wrong type\n");
Expand Down Expand Up @@ -501,6 +517,11 @@ coap_get_code(const coap_pdu_t *pdu, coap_transport_t transport) {
case COAP_TCP_32BIT:
code = pdu->transport_hdr->tcp_32bit.header_data[5];
break;
#endif
#ifdef WITH_WS
case COAP_WS:
code = pdu->transport_hdr->ws.header_data[1];
break;
#endif
default:
debug("it has wrong type\n");
Expand Down Expand Up @@ -554,6 +575,14 @@ coap_add_token2(coap_pdu_t *pdu, size_t len, const unsigned char *data,
token = pdu->transport_hdr->tcp_32bit.token;
pdu->length = token_len + COAP_TCP_HEADER_32_BIT;
break;
#endif
#ifdef WITH_WS
case COAP_WS:
pdu->transport_hdr->ws.header_data[0] =
pdu->transport_hdr->ws.header_data[0] | token_len;
token = pdu->transport_hdr->ws.token;
pdu->length = token_len + COAP_WS_HEADER;
break;
#endif
default:
debug("it has wrong type\n");
Expand Down Expand Up @@ -604,6 +633,12 @@ coap_get_token2(const coap_hdr_transport_t *pdu_hdr, coap_transport_t transport,
*token_length = (pdu_hdr->tcp_32bit.header_data[0]) & 0x0f;
*token = (unsigned char *)pdu_hdr->tcp_32bit.token;
break;
#endif
#ifdef WITH_WS
case COAP_WS:
*token_length = (pdu_hdr->ws.header_data[0]) & 0x0f;
*token = (unsigned char *)pdu_hdr->ws.token;
break;
#endif
default:
debug("it has wrong type\n");
Expand All @@ -621,7 +656,7 @@ coap_add_option2(coap_pdu_t *pdu, unsigned short type, unsigned int len,
const unsigned char *data, coap_transport_t transport) {
size_t optsize;
coap_opt_t *opt;

assert(pdu);
pdu->data = NULL;

Expand All @@ -644,6 +679,11 @@ coap_add_option2(coap_pdu_t *pdu, unsigned short type, unsigned int len,
case COAP_TCP_32BIT:
opt = (unsigned char *) &(pdu->transport_hdr->tcp_32bit) + pdu->length;
break;
#endif
#ifdef WITH_WS
case COAP_WS:
opt = (unsigned char *) &(pdu->transport_hdr->ws) + pdu->length;
break;
#endif
default:
opt = (unsigned char *) &(pdu->transport_hdr->udp) + pdu->length;
Expand Down Expand Up @@ -832,6 +872,11 @@ coap_pdu_parse2(unsigned char *data, size_t length, coap_pdu_t *pdu,
if (COAP_UDP == transport) {
headerSize = sizeof(coap_hdr_t);
}
#ifdef WITH_WS
else if(COAP_WS == transport) {
headerSize = COAP_WS_HEADER;
}
#endif
#ifdef WITH_TCP
else {
headerSize = coap_get_tcp_header_length_for_transport(transport);
Expand All @@ -844,7 +889,7 @@ coap_pdu_parse2(unsigned char *data, size_t length, coap_pdu_t *pdu,

coap_opt_t *opt = NULL;
unsigned int tokenLength = 0;
#ifdef WITH_TCP
#if defined(WITH_TCP) || defined(WITH_WS)
switch (transport) {
case COAP_UDP:
break;
Expand Down Expand Up @@ -882,6 +927,15 @@ coap_pdu_parse2(unsigned char *data, size_t length, coap_pdu_t *pdu,
opt = ((unsigned char *) &(pdu->transport_hdr->tcp_32bit)) +
headerSize + tokenLength;
break;
case COAP_WS:
for (size_t i = 0 ; i < headerSize ; i++) {
pdu->transport_hdr->ws.header_data[i] = data[i];
}

tokenLength = data[0] & 0x0f;
opt = ((unsigned char *) &(pdu->transport_hdr->ws)) +
headerSize + tokenLength;
break;
default:
printf("it has wrong type\n");
}
Expand Down Expand Up @@ -922,8 +976,8 @@ coap_pdu_parse2(unsigned char *data, size_t length, coap_pdu_t *pdu,
length -= (tokenLength + headerSize);
opt = (unsigned char *) (&(pdu->transport_hdr->udp) + 1) + tokenLength;
}
#ifdef WITH_TCP
else { // common for tcp header setting
#if defined(WITH_TCP) || defined(WITH_WS)
else { // common for tcp and websocket header setting
pdu->data = NULL;

if (length < headerSize + tokenLength || tokenLength > 8) {
Expand Down

0 comments on commit f4d20bc

Please sign in to comment.