Skip to content

Commit

Permalink
Merge pull request #1 from Koushik4082/Koushik4082-patch-1
Browse files Browse the repository at this point in the history
Update pdu.c
  • Loading branch information
Koushik4082 authored May 17, 2018
2 parents f4d20bc + 5dc2fdc commit 7164418
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/pdu.c
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,63 @@ coap_add_token2(coap_pdu_t *pdu, size_t len, const unsigned char *data,
return 1;
}

int coap_add_token_to_empty_message(coap_pdu_t *pdu, size_t len, const unsigned char *data,
coap_transport_t transport)
{
const size_t HEADERLENGTH = len;
/* must allow for pdu == NULL as callers may rely on this */
if (!pdu || len > 8)
return 0;

unsigned char* token = NULL;
switch(transport)
{
case COAP_UDP:
pdu->transport_hdr->udp.token_length = len;
token = pdu->transport_hdr->udp.token;
pdu->length = HEADERLENGTH;
break;
#ifdef WITH_TCP
case COAP_TCP:
pdu->transport_hdr->tcp.header_data[0] =
pdu->transport_hdr->tcp.header_data[0] | len;
token = pdu->transport_hdr->tcp.token;
pdu->length = len + COAP_TCP_HEADER_NO_FIELD;
break;
case COAP_TCP_8BIT:
pdu->transport_hdr->tcp_8bit.header_data[0] =
pdu->transport_hdr->tcp_8bit.header_data[0] | len;
token = pdu->transport_hdr->tcp_8bit.token;
pdu->length = len + COAP_TCP_HEADER_8_BIT;
break;
case COAP_TCP_16BIT:
pdu->transport_hdr->tcp_16bit.header_data[0] =
pdu->transport_hdr->tcp_16bit.header_data[0] | len;
token = pdu->transport_hdr->tcp_16bit.token;
pdu->length = len + COAP_TCP_HEADER_16_BIT;
break;
case COAP_TCP_32BIT:
pdu->transport_hdr->tcp_32bit.header_data[0] =
pdu->transport_hdr->tcp_32bit.header_data[0] | len;
token = pdu->transport_hdr->tcp_32bit.token;
pdu->length = len + COAP_TCP_HEADER_32_BIT;
break;
#endif
default:
debug("it has wrong type\n");
}

if (len)
{
memcpy(token, data, len);
}

pdu->max_delta = 0;
pdu->data = NULL;

return 1;
}

void
coap_get_token(const coap_hdr_t *pdu_hdr,
unsigned char **token, unsigned int *token_length) {
Expand Down

0 comments on commit 7164418

Please sign in to comment.