external/wakaama : separate coap_serialize_message function for udp/tcp
authorJin-Seong Kim <jseong82.kim@samsung.com>
Fri, 26 May 2017 10:11:40 +0000 (19:11 +0900)
committerEunBong Song <eunb.song@samsung.com>
Tue, 11 Jul 2017 01:26:56 +0000 (10:26 +0900)
This commit is to separate coap_serialize_message function for udp/tcp
 - to maintain wakaama legacy code, separate tcp/udp api in er-coap

Change-Id: I029aeee28c0cde3981911d3c4e62c4bd3d14633c
Signed-off-by: Jin-Seong Kim <jseong82.kim@samsung.com>
external/wakaama/core/er-coap-13/er-coap-13.c
external/wakaama/core/er-coap-13/er-coap-13.h
external/wakaama/core/packet.c
external/wakaama/core/transaction.c

index 50f1e21..f416909 100644 (file)
@@ -38,7 +38,6 @@
  *    David Navarro, Intel Corporation - Adapt to usage in liblwm2m
  */
 
-
 #include <stdlib.h>
 
 #include <string.h>
@@ -46,7 +45,7 @@
 
 #include "er-coap-13.h"
 
-#include "liblwm2m.h" /* for lwm2m_malloc() and lwm2m_free() */
+#include "liblwm2m.h"                  /* for lwm2m_malloc() and lwm2m_free() */
 
 #define DEBUG 0
 #if DEBUG
@@ -71,1446 +70,1533 @@ const char *coap_error_message = "";
 /*- LOCAL HELP FUNCTIONS ------------------------------------------------------------*/
 /*-----------------------------------------------------------------------------------*/
 static
-uint16_t
-coap_log_2(uint16_t value)
+uint16_t coap_log_2(uint16_t value)
 {
-  uint16_t result = 0;
-  do {
-    value = value >> 1;
-    result++;
-  } while (value);
+       uint16_t result = 0;
+       do {
+               value = value >> 1;
+               result++;
+       } while (value);
 
-  return result ? result - 1 : result;
-}
-/*-----------------------------------------------------------------------------------*/
-static
-uint32_t
-coap_parse_int_option(uint8_t *bytes, size_t length)
-{
-  uint32_t var = 0;
-  size_t i = 0;
-  while (i<length)
-  {
-    var <<= 8;
-    var |= bytes[i++];
-  }
-  return var;
+       return result ? result - 1 : result;
 }
+
 /*-----------------------------------------------------------------------------------*/
-static
-uint8_t
-coap_option_nibble(unsigned int value)
+static uint32_t coap_parse_int_option(uint8_t *bytes, size_t length)
 {
-  if (value<13)
-  {
-    return value;
-  }
-  else if (value<=0xFF+13)
-  {
-    return 13;
-  }
-  else
-  {
-    return 14;
-  }
+       uint32_t var = 0;
+       size_t i = 0;
+       while (i < length) {
+               var <<= 8;
+               var |= bytes[i++];
+       }
+       return var;
 }
+
 /*-----------------------------------------------------------------------------------*/
-static
-size_t
-coap_set_option_header(unsigned int delta, size_t length, uint8_t *buffer)
+static uint8_t coap_option_nibble(unsigned int value)
 {
-  size_t written = 0;
-  unsigned int *x = &delta;
-
-  buffer[0] = coap_option_nibble(delta)<<4 | coap_option_nibble(length);
-
-  /* avoids code duplication without function overhead */
-  do
-  {
-    if (*x>268)
-    {
-      buffer[++written] = (*x-269)>>8;
-      buffer[++written] = (*x-269);
-    }
-    else if (*x>12)
-    {
-      buffer[++written] = (*x-13);
-    }
-  }
-  while (x!=(unsigned int *)&length && (x=(unsigned int *)&length));
-
-  PRINTF("WRITTEN %u B opt header\n", written);
-
-  return ++written;
+       if (value < 13) {
+               return value;
+       } else if (value <= 0xFF + 13) {
+               return 13;
+       } else {
+               return 14;
+       }
 }
+
 /*-----------------------------------------------------------------------------------*/
-static
-size_t
-coap_serialize_int_option(unsigned int number, unsigned int current_number, uint8_t *buffer, uint32_t value)
+static size_t coap_set_option_header(unsigned int delta, size_t length, uint8_t *buffer)
 {
-  size_t i = 0;
+       size_t written = 0;
+       unsigned int *x = &delta;
 
-  if (0xFF000000 & value) ++i;
-  if (0xFFFF0000 & value) ++i;
-  if (0xFFFFFF00 & value) ++i;
-  if (0xFFFFFFFF & value) ++i;
+       buffer[0] = coap_option_nibble(delta) << 4 | coap_option_nibble(length);
 
-  PRINTF("OPTION %u (delta %u, len %u)\n", number, number - current_number, i);
+       /* avoids code duplication without function overhead */
+       do {
+               if (*x > 268) {
+                       buffer[++written] = (*x - 269) >> 8;
+                       buffer[++written] = (*x - 269);
+               } else if (*x > 12) {
+                       buffer[++written] = (*x - 13);
+               }
+       } while (x != (unsigned int *)&length && (x = (unsigned int *)&length));
 
-  i = coap_set_option_header(number - current_number, i, buffer);
+       PRINTF("WRITTEN %u B opt header\n", written);
 
-  if (0xFF000000 & value) buffer[i++] = (uint8_t) (value>>24);
-  if (0xFFFF0000 & value) buffer[i++] = (uint8_t) (value>>16);
-  if (0xFFFFFF00 & value) buffer[i++] = (uint8_t) (value>>8);
-  if (0xFFFFFFFF & value) buffer[i++] = (uint8_t) (value);
+       return ++written;
+}
 
-  return i;
+/*-----------------------------------------------------------------------------------*/
+static size_t coap_serialize_int_option(unsigned int number, unsigned int current_number, uint8_t *buffer, uint32_t value)
+{
+       size_t i = 0;
+
+       if (0xFF000000 & value) {
+               ++i;
+       }
+       if (0xFFFF0000 & value) {
+               ++i;
+       }
+       if (0xFFFFFF00 & value) {
+               ++i;
+       }
+       if (0xFFFFFFFF & value) {
+               ++i;
+       }
+
+       PRINTF("OPTION %u (delta %u, len %u)\n", number, number - current_number, i);
+
+       i = coap_set_option_header(number - current_number, i, buffer);
+
+       if (0xFF000000 & value) {
+               buffer[i++] = (uint8_t)(value >> 24);
+       }
+       if (0xFFFF0000 & value) {
+               buffer[i++] = (uint8_t)(value >> 16);
+       }
+       if (0xFFFFFF00 & value) {
+               buffer[i++] = (uint8_t)(value >> 8);
+       }
+       if (0xFFFFFFFF & value) {
+               buffer[i++] = (uint8_t)(value);
+       }
+
+       return i;
 }
+
 /*-----------------------------------------------------------------------------------*/
-static
-size_t
-coap_serialize_array_option(unsigned int number, unsigned int current_number, uint8_t *buffer, uint8_t *array, size_t length, char split_char)
+static size_t coap_serialize_array_option(unsigned int number, unsigned int current_number, uint8_t *buffer, uint8_t *array, size_t length, char split_char)
 {
-  size_t i = 0;
-
-  if (split_char!='\0')
-  {
-    size_t j;
-    uint8_t *part_start = array;
-    uint8_t *part_end = NULL;
-    size_t temp_length;
-
-    for (j = 0; j<=length; ++j)
-    {
-      if (array[j]==split_char || j==length)
-      {
-        part_end = array + j;
-        temp_length = part_end-part_start;
-
-        i += coap_set_option_header(number - current_number, temp_length, &buffer[i]);
-        memcpy(&buffer[i], part_start, temp_length);
-        i += temp_length;
-
-        PRINTF("OPTION type %u, delta %u, len %u, part [%.*s]\n", number, number - current_number, i, temp_length, part_start);
-
-        ++j; /* skip the splitter */
-        current_number = number;
-        part_start = array + j;
-      }
-    } /* for */
-  }
-  else
-  {
-    i += coap_set_option_header(number - current_number, length, &buffer[i]);
-    memcpy(&buffer[i], array, length);
-    i += length;
-
-    PRINTF("OPTION type %u, delta %u, len %u\n", number, number - current_number, length);
-  }
-
-  return i;
+       size_t i = 0;
+
+       if (split_char != '\0') {
+               size_t j;
+               uint8_t *part_start = array;
+               uint8_t *part_end = NULL;
+               size_t temp_length;
+
+               for (j = 0; j <= length; ++j) {
+                       if (array[j] == split_char || j == length) {
+                               part_end = array + j;
+                               temp_length = part_end - part_start;
+
+                               i += coap_set_option_header(number - current_number, temp_length, &buffer[i]);
+                               memcpy(&buffer[i], part_start, temp_length);
+                               i += temp_length;
+
+                               PRINTF("OPTION type %u, delta %u, len %u, part [%.*s]\n", number, number - current_number, i, temp_length, part_start);
+
+                               ++j;                    /* skip the splitter */
+                               current_number = number;
+                               part_start = array + j;
+                       }
+               }                                               /* for */
+       } else {
+               i += coap_set_option_header(number - current_number, length, &buffer[i]);
+               memcpy(&buffer[i], array, length);
+               i += length;
+
+               PRINTF("OPTION type %u, delta %u, len %u\n", number, number - current_number, length);
+       }
+
+       return i;
 }
+
 /*-----------------------------------------------------------------------------------*/
-static
-size_t
-coap_serialize_multi_option(unsigned int number, unsigned int current_number, uint8_t *buffer, multi_option_t *array)
+static size_t coap_serialize_multi_option(unsigned int number, unsigned int current_number, uint8_t *buffer, multi_option_t *array)
 {
-  size_t i = 0;
-  multi_option_t * j;
-
-  for (j = array; j != NULL; j= j->next)
-  {
-     i += coap_set_option_header(number - current_number, j->len, &buffer[i]);
-     current_number = number;
-     memcpy(&buffer[i], j->data, j->len);
-     i += j->len;
-  } /* for */
-
-  return i;
+       size_t i = 0;
+       multi_option_t *j;
+
+       for (j = array; j != NULL; j = j->next) {
+               i += coap_set_option_header(number - current_number, j->len, &buffer[i]);
+               current_number = number;
+               memcpy(&buffer[i], j->data, j->len);
+               i += j->len;
+       }                                                       /* for */
+
+       return i;
 }
+
 /*-----------------------------------------------------------------------------------*/
 static
-void
-coap_merge_multi_option(uint8_t **dst, size_t *dst_len, uint8_t *option, size_t option_len, char separator)
+void coap_merge_multi_option(uint8_t **dst, size_t *dst_len, uint8_t *option, size_t option_len, char separator)
 {
-  /* Merge multiple options. */
-  if (*dst_len > 0)
-  {
-    /* dst already contains an option: concatenate */
-    (*dst)[*dst_len] = separator;
-    *dst_len += 1;
-
-    /* memmove handles 2-byte option headers */
-    memmove((*dst)+(*dst_len), option, option_len);
-
-    *dst_len += option_len;
-  }
-  else
-  {
-    /* dst is empty: set to option */
-    *dst = option;
-    *dst_len = option_len;
-  }
+       /* Merge multiple options. */
+       if (*dst_len > 0) {
+               /* dst already contains an option: concatenate */
+               (*dst)[*dst_len] = separator;
+               *dst_len += 1;
+
+               /* memmove handles 2-byte option headers */
+               memmove((*dst) + (*dst_len), option, option_len);
+
+               *dst_len += option_len;
+       } else {
+               /* dst is empty: set to option */
+               *dst = option;
+               *dst_len = option_len;
+       }
 }
 
-void
-coap_add_multi_option(multi_option_t **dst, uint8_t *option, size_t option_len, uint8_t is_static)
+void coap_add_multi_option(multi_option_t **dst, uint8_t *option, size_t option_len, uint8_t is_static)
 {
-  multi_option_t *opt = (multi_option_t *)lwm2m_malloc(sizeof(multi_option_t));
-
-  if (opt)
-  {
-    opt->next = NULL;
-    opt->len = (uint8_t)option_len;
-    if (is_static)
-    {
-      opt->data = option;
-      opt->is_static = 1;
-    }
-    else
-    {
-        opt->is_static = 0;
-        opt->data = (uint8_t *)lwm2m_malloc(option_len);
-        if (opt->data == NULL)
-        {
-            lwm2m_free(opt);
-            return;
-        }
-        memcpy(opt->data, option, option_len);
-    }
-
-    if (*dst)
-    {
-      multi_option_t * i = *dst;
-      while (i->next)
-      {
-        i = i->next;
-      }
-      i->next = opt;
-    }
-    else
-    {
-      *dst = opt;
-    }
-  }
+       multi_option_t *opt = (multi_option_t *) lwm2m_malloc(sizeof(multi_option_t));
+
+       if (opt) {
+               opt->next = NULL;
+               opt->len = (uint8_t) option_len;
+               if (is_static) {
+                       opt->data = option;
+                       opt->is_static = 1;
+               } else {
+                       opt->is_static = 0;
+                       opt->data = (uint8_t *) lwm2m_malloc(option_len);
+                       if (opt->data == NULL) {
+                               lwm2m_free(opt);
+                               return;
+                       }
+                       memcpy(opt->data, option, option_len);
+               }
+
+               if (*dst) {
+                       multi_option_t *i = *dst;
+                       while (i->next) {
+                               i = i->next;
+                       }
+                       i->next = opt;
+               } else {
+                       *dst = opt;
+               }
+       }
 }
 
-void
-free_multi_option(multi_option_t *dst)
+void free_multi_option(multi_option_t *dst)
 {
-  if (dst)
-  {
-    multi_option_t *n = dst->next;
-    dst->next = NULL;
-    if (dst->is_static == 0)
-    {
-        lwm2m_free(dst->data);
-    }
-    lwm2m_free(dst);
-    free_multi_option(n);
-  }
+       if (dst) {
+               multi_option_t *n = dst->next;
+               dst->next = NULL;
+               if (dst->is_static == 0) {
+                       lwm2m_free(dst->data);
+               }
+               lwm2m_free(dst);
+               free_multi_option(n);
+       }
 }
 
-char * coap_get_multi_option_as_string(multi_option_t * option)
+char *coap_get_multi_option_as_string(multi_option_t *option)
 {
-    size_t len = 0;
-    multi_option_t * opt;
-    char * output;
-
-    for (opt = option; opt != NULL; opt = opt->next)
-    {
-       len += opt->len + 1;     // for separator
-    }
-
-    output = lwm2m_malloc(len + 1); // for String terminator
-    if (output != NULL)
-    {
-        size_t i = 0;
-
-        for (opt = option; opt != NULL; opt = opt->next)
-        {
-            output[i] = '/';
-            i += 1;
-
-            memmove(output + i, opt->data, opt->len);
-            i += opt->len;
-        }
-        output[i] = 0;
-    }
-
-    return output;
+       size_t len = 0;
+       multi_option_t *opt;
+       char *output;
+
+       for (opt = option; opt != NULL; opt = opt->next) {
+               len += opt->len + 1;    // for separator
+       }
+
+       output = lwm2m_malloc(len + 1); // for String terminator
+       if (output != NULL) {
+               size_t i = 0;
+
+               for (opt = option; opt != NULL; opt = opt->next) {
+                       output[i] = '/';
+                       i += 1;
+
+                       memmove(output + i, opt->data, opt->len);
+                       i += opt->len;
+               }
+               output[i] = 0;
+       }
+
+       return output;
 }
 
 /*-----------------------------------------------------------------------------------*/
 static
-int
-coap_get_variable(const uint8_t *buffer, size_t length, const char *name, const char **output)
+int coap_get_variable(const uint8_t *buffer, size_t length, const char *name, const char **output)
 {
-  const uint8_t *start = NULL;
-  const uint8_t *end = NULL;
-  const uint8_t *value_end = NULL;
-  size_t name_len = 0;
+       const uint8_t *start = NULL;
+       const uint8_t *end = NULL;
+       const uint8_t *value_end = NULL;
+       size_t name_len = 0;
 
-  /*initialize the output buffer first*/
-  *output = 0;
+       /*initialize the output buffer first */
+       *output = 0;
 
-  name_len = strlen(name);
-  end = buffer + length;
+       name_len = strlen(name);
+       end = buffer + length;
 
-  for (start = buffer; start + name_len < end; ++start){
-    if ((start == buffer || start[-1] == '&') && start[name_len] == '=' &&
-        strncmp(name, (char *)start, name_len)==0) {
+       for (start = buffer; start + name_len < end; ++start) {
+               if ((start == buffer || start[-1] == '&') && start[name_len] == '=' && strncmp(name, (char *)start, name_len) == 0) {
 
-      /* Point start to variable value */
-      start += name_len + 1;
+                       /* Point start to variable value */
+                       start += name_len + 1;
 
-      /* Point end to the end of the value */
-      value_end = (const uint8_t *) memchr(start, '&', end - start);
-      if (value_end == NULL) {
-        value_end = end;
-      }
+                       /* Point end to the end of the value */
+                       value_end = (const uint8_t *)memchr(start, '&', end - start);
+                       if (value_end == NULL) {
+                               value_end = end;
+                       }
 
-      *output = (char *)start;
+                       *output = (char *)start;
 
-      return (value_end - start);
-    }
-  }
+                       return (value_end - start);
+               }
+       }
 
-  return 0;
+       return 0;
 }
 
 /*-----------------------------------------------------------------------------------*/
-uint16_t
-coap_get_mid()
+uint16_t coap_get_mid(void)
 {
-  return ++current_mid;
+       return ++current_mid;
 }
+
 /*-----------------------------------------------------------------------------------*/
 /*- MEASSAGE PROCESSING -------------------------------------------------------------*/
 /*-----------------------------------------------------------------------------------*/
-void
-coap_init_message(void *packet, coap_protocol_t protocol, coap_message_type_t type, uint8_t code, uint16_t mid)
+void coap_init_message(void *packet, coap_protocol_t protocol, coap_message_type_t type, uint8_t code, uint16_t mid)
 {
-  coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
+       coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
 
-  /* Important thing */
-  memset(coap_pkt, 0, sizeof(coap_packet_t));
+       /* Important thing */
+       memset(coap_pkt, 0, sizeof(coap_packet_t));
 
-  coap_pkt->protocol = protocol;
-  coap_pkt->type = type;
-  coap_pkt->code = code;
-  coap_pkt->mid = mid;
+       coap_pkt->protocol = protocol;
+       coap_pkt->type = type;
+       coap_pkt->code = code;
+       coap_pkt->mid = mid;
 }
 
-void
-coap_free_header(void *packet)
+void coap_free_header(void *packet)
 {
-    coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
-
-    free_multi_option(coap_pkt->uri_path);
-    free_multi_option(coap_pkt->uri_query);
-    free_multi_option(coap_pkt->location_path);
-    coap_pkt->uri_path = NULL;
-    coap_pkt->uri_query = NULL;
-    coap_pkt->location_path = NULL;
+       coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
+
+       free_multi_option(coap_pkt->uri_path);
+       free_multi_option(coap_pkt->uri_query);
+       free_multi_option(coap_pkt->location_path);
+       coap_pkt->uri_path = NULL;
+       coap_pkt->uri_query = NULL;
+       coap_pkt->location_path = NULL;
 }
 
 /*-----------------------------------------------------------------------------------*/
+size_t coap_serialize_get_size_tcp(void *packet)
+{
+       coap_packet_t *coap_pkt = (coap_packet_t *) packet;
+
+       uint16_t len = 0;
+       uint16_t options_len = 0;
+
+       size_t length = 0;
+
+       if (packet == NULL) {
+               PRINTF("ERROR : packet is null\n");
+               return 0;
+       }
+
+       if (IS_OPTION(coap_pkt, COAP_OPTION_IF_MATCH)) {
+               options_len += COAP_MAX_OPTION_HEADER_LEN + coap_pkt->if_match_len;
+       }
+       if (IS_OPTION(coap_pkt, COAP_OPTION_URI_HOST)) {
+               options_len += COAP_MAX_OPTION_HEADER_LEN + coap_pkt->uri_host_len;
+       }
+       if (IS_OPTION(coap_pkt, COAP_OPTION_ETAG)) {
+               options_len += COAP_MAX_OPTION_HEADER_LEN + coap_pkt->etag_len;
+       }
+       if (IS_OPTION(coap_pkt, COAP_OPTION_IF_NONE_MATCH)) {
+               // can be stored in extended fields
+               options_len += COAP_MAX_OPTION_HEADER_LEN;
+       }
+       if (IS_OPTION(coap_pkt, COAP_OPTION_OBSERVE)) {
+               // can be stored in extended fields
+               options_len += COAP_MAX_OPTION_HEADER_LEN;
+       }
+       if (IS_OPTION(coap_pkt, COAP_OPTION_URI_PORT)) {
+               // can be stored in extended fields
+               options_len += COAP_MAX_OPTION_HEADER_LEN;
+       }
+       if (IS_OPTION(coap_pkt, COAP_OPTION_LOCATION_PATH)) {
+               multi_option_t *optP;
+
+               for (optP = coap_pkt->location_path; optP != NULL; optP = optP->next) {
+                       options_len += COAP_MAX_OPTION_HEADER_LEN + optP->len;
+               }
+       }
+       if (IS_OPTION(coap_pkt, COAP_OPTION_URI_PATH)) {
+               multi_option_t *optP;
+
+               for (optP = coap_pkt->uri_path; optP != NULL; optP = optP->next) {
+                       options_len += COAP_MAX_OPTION_HEADER_LEN + optP->len;
+               }
+       }
+       if (IS_OPTION(coap_pkt, COAP_OPTION_CONTENT_TYPE)) {
+               // can be stored in extended fields
+               options_len += COAP_MAX_OPTION_HEADER_LEN;
+       }
+       if (IS_OPTION(coap_pkt, COAP_OPTION_MAX_AGE)) {
+               // can be stored in extended fields
+               options_len += COAP_MAX_OPTION_HEADER_LEN;
+       }
+       if (IS_OPTION(coap_pkt, COAP_OPTION_URI_QUERY)) {
+               multi_option_t *optP;
+
+               for (optP = coap_pkt->uri_query; optP != NULL; optP = optP->next) {
+                       options_len += COAP_MAX_OPTION_HEADER_LEN + optP->len;
+               }
+       }
+       if (IS_OPTION(coap_pkt, COAP_OPTION_ACCEPT)) {
+               options_len += coap_pkt->accept_num * COAP_MAX_OPTION_HEADER_LEN;
+       }
+       if (IS_OPTION(coap_pkt, COAP_OPTION_LOCATION_QUERY)) {
+               options_len += COAP_MAX_OPTION_HEADER_LEN + coap_pkt->location_query_len;
+       }
+       if (IS_OPTION(coap_pkt, COAP_OPTION_BLOCK2)) {
+               // can be stored in extended fields
+               options_len += COAP_MAX_OPTION_HEADER_LEN;
+       }
+       if (IS_OPTION(coap_pkt, COAP_OPTION_BLOCK1)) {
+               // can be stored in extended fields
+               options_len += COAP_MAX_OPTION_HEADER_LEN;
+       }
+       if (IS_OPTION(coap_pkt, COAP_OPTION_SIZE)) {
+               // can be stored in extended fields
+               options_len += COAP_MAX_OPTION_HEADER_LEN;
+       }
+       if (IS_OPTION(coap_pkt, COAP_OPTION_PROXY_URI)) {
+               options_len += COAP_MAX_OPTION_HEADER_LEN + coap_pkt->proxy_uri_len;
+       }
+
+       coap_pkt->options_len = options_len;
+
+       len = coap_pkt->options_len + coap_pkt->payload_len;
+
+       PRINTF("options len %d, payload len %d\n", coap_pkt->options_len, coap_pkt->payload_len);
+
+       if (len < 13) {
+               length += 2;
+       } else if (len < ((1 << 8) + 13)) {
+               length += 3;
+       } else if (len < ((1 << 16) + 269)) {
+               length += 4;
+       } else {
+               length += 6;
+       }
+
+       length += coap_pkt->token_len + coap_pkt->options_len;
+       if (coap_pkt->payload_len > 0) {
+               length += COAP_OPTIONS_MARKER_LEN + coap_pkt->payload_len;
+       }
+
+       return length;
+}
+
 size_t coap_serialize_get_size(void *packet)
 {
-    if (packet == NULL) return 0;
-
-    coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
-    size_t length = 0;
-
-    coap_pkt->options_len = 0;
-    if (IS_OPTION(coap_pkt, COAP_OPTION_IF_MATCH))
-    {
-        coap_pkt->options_len += COAP_MAX_OPTION_HEADER_LEN + coap_pkt->if_match_len;
-    }
-    if (IS_OPTION(coap_pkt, COAP_OPTION_URI_HOST))
-    {
-        coap_pkt->options_len += COAP_MAX_OPTION_HEADER_LEN + coap_pkt->uri_host_len;
-    }
-    if (IS_OPTION(coap_pkt, COAP_OPTION_ETAG))
-    {
-        coap_pkt->options_len += COAP_MAX_OPTION_HEADER_LEN + coap_pkt->etag_len;
-    }
-    if (IS_OPTION(coap_pkt, COAP_OPTION_IF_NONE_MATCH))
-    {
-        // can be stored in extended fields
-        coap_pkt->options_len += COAP_MAX_OPTION_HEADER_LEN;
-    }
-    if (IS_OPTION(coap_pkt, COAP_OPTION_OBSERVE))
-    {
-        // can be stored in extended fields
-        coap_pkt->options_len += COAP_MAX_OPTION_HEADER_LEN;
-    }
-    if (IS_OPTION(coap_pkt, COAP_OPTION_URI_PORT))
-    {
-        // can be stored in extended fields
-        coap_pkt->options_len += COAP_MAX_OPTION_HEADER_LEN;
-    }
-    if (IS_OPTION(coap_pkt, COAP_OPTION_LOCATION_PATH))
-    {
-        multi_option_t * optP;
-
-        for (optP = coap_pkt->location_path ; optP != NULL ; optP = optP->next)
-        {
-            coap_pkt->options_len += COAP_MAX_OPTION_HEADER_LEN + optP->len;
-        }
-    }
-    if (IS_OPTION(coap_pkt, COAP_OPTION_URI_PATH))
-    {
-        multi_option_t * optP;
-
-        for (optP = coap_pkt->uri_path ; optP != NULL ; optP = optP->next)
-        {
-            coap_pkt->options_len += COAP_MAX_OPTION_HEADER_LEN + optP->len;
-        }
-    }
-    if (IS_OPTION(coap_pkt, COAP_OPTION_CONTENT_TYPE))
-    {
-        // can be stored in extended fields
-        coap_pkt->options_len += COAP_MAX_OPTION_HEADER_LEN;
-    }
-    if (IS_OPTION(coap_pkt, COAP_OPTION_MAX_AGE))
-    {
-        // can be stored in extended fields
-        coap_pkt->options_len += COAP_MAX_OPTION_HEADER_LEN;
-    }
-    if (IS_OPTION(coap_pkt, COAP_OPTION_URI_QUERY))
-    {
-        multi_option_t * optP;
-
-        for (optP = coap_pkt->uri_query ; optP != NULL ; optP = optP->next)
-        {
-            coap_pkt->options_len += COAP_MAX_OPTION_HEADER_LEN + optP->len;
-        }
-    }
-    if (IS_OPTION(coap_pkt, COAP_OPTION_ACCEPT))
-    {
-        coap_pkt->options_len += coap_pkt->accept_num * COAP_MAX_OPTION_HEADER_LEN;
-    }
-    if (IS_OPTION(coap_pkt, COAP_OPTION_LOCATION_QUERY))
-    {
-        coap_pkt->options_len += COAP_MAX_OPTION_HEADER_LEN + coap_pkt->location_query_len;
-    }
-    if (IS_OPTION(coap_pkt, COAP_OPTION_BLOCK2))
-    {
-        // can be stored in extended fields
-        coap_pkt->options_len += COAP_MAX_OPTION_HEADER_LEN;
-    }
-    if (IS_OPTION(coap_pkt, COAP_OPTION_BLOCK1))
-    {
-        // can be stored in extended fields
-        coap_pkt->options_len += COAP_MAX_OPTION_HEADER_LEN;
-    }
-    if (IS_OPTION(coap_pkt, COAP_OPTION_SIZE))
-    {
-        // can be stored in extended fields
-        coap_pkt->options_len += COAP_MAX_OPTION_HEADER_LEN;
-    }
-    if (IS_OPTION(coap_pkt, COAP_OPTION_PROXY_URI))
-    {
-        coap_pkt->options_len += COAP_MAX_OPTION_HEADER_LEN + coap_pkt->proxy_uri_len;
-    }
-
-    switch (coap_pkt->protocol)
-    {
-    case COAP_TCP:
-    case COAP_TCP_TLS:
-        {
-            int len = coap_pkt->options_len + coap_pkt->payload_len;
-            if (len < 13)
-            {
-                length += 1;
-            }
-            else if (len < ((1 << 8) + 13))
-            {
-                length += 2;
-            }
-            else if (len < ((1 << 16) + 269))
-            {
-                length += 3;
-            }
-            else
-            {
-                length += 5;
-            }
-
-            length += coap_pkt->token_len + coap_pkt->options_len;
-            if (coap_pkt->payload_len > 0)
-                length += COAP_OPTIONS_MARKER_LEN + coap_pkt->payload_len;
-        }
-        break;
-    case COAP_UDP:
-    case COAP_UDP_DTLS:
-        length += COAP_HEADER_LEN + coap_pkt->token_len + coap_pkt->options_len;
-        if (coap_pkt->payload_len > 0)
-            length += COAP_OPTIONS_MARKER_LEN + coap_pkt->payload_len;
-        break;
-    default:
-        break;
-    }
-
-    return length;
+       coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
+       size_t length = 0;
+
+       if (packet == NULL) {
+               PRINTF("ERROR : packet is null\n");
+               return 0;
+       }
+
+       length = COAP_HEADER_LEN + coap_pkt->payload_len + coap_pkt->token_len;
+
+       if (IS_OPTION(coap_pkt, COAP_OPTION_IF_MATCH)) {
+               length += COAP_MAX_OPTION_HEADER_LEN + coap_pkt->if_match_len;
+       }
+       if (IS_OPTION(coap_pkt, COAP_OPTION_URI_HOST)) {
+               length += COAP_MAX_OPTION_HEADER_LEN + coap_pkt->uri_host_len;
+       }
+       if (IS_OPTION(coap_pkt, COAP_OPTION_ETAG)) {
+               length += COAP_MAX_OPTION_HEADER_LEN + coap_pkt->etag_len;
+       }
+       if (IS_OPTION(coap_pkt, COAP_OPTION_IF_NONE_MATCH)) {
+               // can be stored in extended fields
+               length += COAP_MAX_OPTION_HEADER_LEN;
+       }
+       if (IS_OPTION(coap_pkt, COAP_OPTION_OBSERVE)) {
+               // can be stored in extended fields
+               length += COAP_MAX_OPTION_HEADER_LEN;
+       }
+       if (IS_OPTION(coap_pkt, COAP_OPTION_URI_PORT)) {
+               // can be stored in extended fields
+               length += COAP_MAX_OPTION_HEADER_LEN;
+       }
+       if (IS_OPTION(coap_pkt, COAP_OPTION_LOCATION_PATH)) {
+               multi_option_t *optP;
+
+               for (optP = coap_pkt->location_path; optP != NULL; optP = optP->next) {
+                       length += COAP_MAX_OPTION_HEADER_LEN + optP->len;
+               }
+       }
+       if (IS_OPTION(coap_pkt, COAP_OPTION_URI_PATH)) {
+               multi_option_t *optP;
+
+               for (optP = coap_pkt->uri_path; optP != NULL; optP = optP->next) {
+                       length += COAP_MAX_OPTION_HEADER_LEN + optP->len;
+               }
+       }
+       if (IS_OPTION(coap_pkt, COAP_OPTION_CONTENT_TYPE)) {
+               // can be stored in extended fields
+               length += COAP_MAX_OPTION_HEADER_LEN;
+       }
+       if (IS_OPTION(coap_pkt, COAP_OPTION_MAX_AGE)) {
+               // can be stored in extended fields
+               length += COAP_MAX_OPTION_HEADER_LEN;
+       }
+       if (IS_OPTION(coap_pkt, COAP_OPTION_URI_QUERY)) {
+               multi_option_t *optP;
+
+               for (optP = coap_pkt->uri_query; optP != NULL; optP = optP->next) {
+                       length += COAP_MAX_OPTION_HEADER_LEN + optP->len;
+               }
+       }
+       if (IS_OPTION(coap_pkt, COAP_OPTION_ACCEPT)) {
+               length += coap_pkt->accept_num * COAP_MAX_OPTION_HEADER_LEN;
+       }
+       if (IS_OPTION(coap_pkt, COAP_OPTION_LOCATION_QUERY)) {
+               length += COAP_MAX_OPTION_HEADER_LEN + coap_pkt->location_query_len;
+       }
+       if (IS_OPTION(coap_pkt, COAP_OPTION_BLOCK2)) {
+               // can be stored in extended fields
+               length += COAP_MAX_OPTION_HEADER_LEN;
+       }
+       if (IS_OPTION(coap_pkt, COAP_OPTION_BLOCK1)) {
+               // can be stored in extended fields
+               length += COAP_MAX_OPTION_HEADER_LEN;
+       }
+       if (IS_OPTION(coap_pkt, COAP_OPTION_SIZE)) {
+               // can be stored in extended fields
+               length += COAP_MAX_OPTION_HEADER_LEN;
+       }
+       if (IS_OPTION(coap_pkt, COAP_OPTION_PROXY_URI)) {
+               length += COAP_MAX_OPTION_HEADER_LEN + coap_pkt->proxy_uri_len;
+       }
+
+       return length;
+}
+
+size_t coap_serialize_message_tcp(void *packet, uint8_t *buffer)
+{
+       /* CoAP TCP header is different from CoAP UDP header (draft-ietf-core-coap-tcp-tls-04)
+        * | Len (4bits) | TKL (4bits) | Extended Length (8bits)| Code (8bits) | TKL bytes (8bits) |
+        * | Options (if any) ....                                                                 |
+        * | 1 1 1 1 1 1 1 1           | Payload (if any) ...                                      |
+        */
+
+       coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
+       uint8_t *option = NULL;
+       uint8_t *options_buf = NULL;
+       unsigned int current_number = 0;
+
+       /* Initialize */
+       coap_pkt->buffer = buffer;
+       coap_pkt->version = 1;
+
+       PRINTF("\n-Serializing MID %u to %p\n", coap_pkt->mid, coap_pkt->buffer);
+
+       /* First serialize the options to get their exact size */
+       PRINTF("\nEntering : CoAP TCP Header (options_len : %d)\n", coap_pkt->options_len);
+
+       /* FIXME : Someone has better idea to resolve options_len = 0 case */
+       if (coap_pkt->options_len == 0) {
+               PRINTF("ERROR : options_len == 0\n");
+               coap_pkt->options_len = COAP_MAX_OPTION_HEADER_LEN;
+       }
+
+       options_buf = lwm2m_malloc(coap_pkt->options_len);
+       if (!options_buf) {
+               PRINTF("ERROR : failed to memory allocation\n");
+               return 0;
+       }
+       option = options_buf;
+
+       /* Serialize options */
+       current_number = 0;
+
+       PRINTF("-Serializing options at %p-\n", option);
+
+       /* The options must be serialized in the order of their number */
+       COAP_SERIALIZE_BYTE_OPTION(COAP_OPTION_IF_MATCH, if_match, "If-Match")
+       COAP_SERIALIZE_STRING_OPTION(COAP_OPTION_URI_HOST, uri_host, '\0', "Uri-Host")
+       COAP_SERIALIZE_BYTE_OPTION(COAP_OPTION_ETAG, etag, "ETag")
+       COAP_SERIALIZE_INT_OPTION(COAP_OPTION_IF_NONE_MATCH, content_type - coap_pkt->content_type, "If-None-Match")    /* hack to get a zero field */
+       COAP_SERIALIZE_INT_OPTION(COAP_OPTION_OBSERVE, observe, "Observe")
+       COAP_SERIALIZE_INT_OPTION(COAP_OPTION_URI_PORT, uri_port, "Uri-Port")
+       COAP_SERIALIZE_MULTI_OPTION(COAP_OPTION_LOCATION_PATH, location_path, "Location-Path")
+       COAP_SERIALIZE_MULTI_OPTION(COAP_OPTION_URI_PATH, uri_path, "Uri-Path")
+       COAP_SERIALIZE_INT_OPTION(COAP_OPTION_CONTENT_TYPE, content_type, "Content-Format")
+       COAP_SERIALIZE_INT_OPTION(COAP_OPTION_MAX_AGE, max_age, "Max-Age")
+       COAP_SERIALIZE_MULTI_OPTION(COAP_OPTION_URI_QUERY, uri_query, "Uri-Query")
+       COAP_SERIALIZE_ACCEPT_OPTION(COAP_OPTION_ACCEPT, accept, "Accept")
+       COAP_SERIALIZE_STRING_OPTION(COAP_OPTION_LOCATION_QUERY, location_query, '&', "Location-Query")
+       COAP_SERIALIZE_BLOCK_OPTION(COAP_OPTION_BLOCK2, block2, "Block2")
+       COAP_SERIALIZE_BLOCK_OPTION(COAP_OPTION_BLOCK1, block1, "Block1")
+       COAP_SERIALIZE_INT_OPTION(COAP_OPTION_SIZE, size, "Size")
+       COAP_SERIALIZE_STRING_OPTION(COAP_OPTION_PROXY_URI, proxy_uri, '\0', "Proxy-Uri")
+
+       PRINTF("-Done serializing at %p----\n", option);
+
+       coap_pkt->options_len = option - options_buf;
+
+       PRINTF("- Serialized options_len : %d\n", coap_pkt->options_len);
+
+       uint32_t len = coap_pkt->options_len;
+
+       if (coap_pkt->payload_len > 0) {
+               len += COAP_OPTIONS_MARKER_LEN + coap_pkt->payload_len;
+       }
+
+       PRINTF("Options+Payload (len %u)", len);
+       if (len < 13) {
+               coap_pkt->buffer[0] = (COAP_HEADER_TOKEN_LEN_MASK & coap_pkt->token_len);
+               coap_pkt->buffer[0] |= (len << 4);
+               coap_pkt->buffer[1] = coap_pkt->code;
+               option = coap_pkt->buffer + 2;
+       } else if (len < ((1 << 8) + 13)) {
+               coap_pkt->buffer[0] = (COAP_HEADER_TOKEN_LEN_MASK & coap_pkt->token_len);
+               coap_pkt->buffer[0] |= (13 << 4);
+               coap_pkt->buffer[1] = len - 13;
+               coap_pkt->buffer[2] = coap_pkt->code;
+               option = coap_pkt->buffer + 3;
+       } else if (len < ((1 << 16) + 269)) {
+               coap_pkt->buffer[0] = (COAP_HEADER_TOKEN_LEN_MASK & coap_pkt->token_len);
+               coap_pkt->buffer[0] |= (14 << 4);
+               coap_pkt->buffer[1] = ((len - 269) & 0xFF00) >> 8;
+               coap_pkt->buffer[2] = (len - 269) & 0x00FF;
+               coap_pkt->buffer[3] = coap_pkt->code;
+               option = coap_pkt->buffer + 4;
+       } else {
+               coap_pkt->buffer[0] = (COAP_HEADER_TOKEN_LEN_MASK & coap_pkt->token_len);
+               coap_pkt->buffer[0] |= (15 << 4);
+               coap_pkt->buffer[1] = ((len - 65805) & 0xFF000000) >> 24;
+               coap_pkt->buffer[2] = ((len - 65805) & 0x00FF0000) >> 16;
+               coap_pkt->buffer[3] = ((len - 65805) & 0x0000FF00) >> 8;
+               coap_pkt->buffer[4] = (len - 65805) & 0x00FF;
+               coap_pkt->buffer[5] = coap_pkt->code;
+               option = coap_pkt->buffer + 6;
+       }
+
+       /* set Token */
+       PRINTF("Token (len %u)", coap_pkt->token_len);
+
+       for (current_number = 0; current_number < coap_pkt->token_len; ++current_number) {
+               PRINTF(" %02X", coap_pkt->token[current_number]);
+               *option = coap_pkt->token[current_number];
+               ++option;
+       }
+       PRINTF("-\n");
+
+       /* Copy options */
+       memcpy(option, options_buf, coap_pkt->options_len);
+       option += coap_pkt->options_len;
+       lwm2m_free(options_buf);
+
+       /* Free allocated header fields */
+       coap_free_header(packet);
+
+       /* Pack payload */
+       /* Payload marker */
+       if (coap_pkt->payload_len) {
+               *option = 0xFF;
+               ++option;
+       }
+
+       memmove(option, coap_pkt->payload, coap_pkt->payload_len);
+
+       PRINTF("-Done %u B (header len %u, payload len %u)-\n", coap_pkt->payload_len + option - buffer, option - buffer, coap_pkt->payload_len);
+
+       PRINTF("Dump [0x%02X %02X %02X %02X  %02X %02X %02X %02X]\n", coap_pkt->buffer[0], coap_pkt->buffer[1], coap_pkt->buffer[2], coap_pkt->buffer[3], coap_pkt->buffer[4], coap_pkt->buffer[5], coap_pkt->buffer[6], coap_pkt->buffer[7]
+                 );
+
+       return (option - buffer) + coap_pkt->payload_len;       /* packet length */
 }
 
 /*-----------------------------------------------------------------------------------*/
-size_t
-coap_serialize_message(void *packet, uint8_t *buffer)
+size_t coap_serialize_message(void *packet, uint8_t *buffer)
 {
-  coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
-  uint8_t *option;
-  uint8_t *options_buf;
-  unsigned int current_number = 0;
-
-  /* Initialize */
-  coap_pkt->buffer = buffer;
-  coap_pkt->version = 1;
-
-  PRINTF("-Serializing MID %u to %p, ", coap_pkt->mid, coap_pkt->buffer);
-
-  /* First serialize the options to get their exact size */
-  current_number = 0;
-  options_buf = lwm2m_malloc(coap_pkt->options_len);
-  if (!options_buf)
-      return 0;
-  option = options_buf;
-
-  PRINTF("-Serializing options at %p-\n", option);
-
-  /* The options must be serialized in the order of their number */
-  COAP_SERIALIZE_BYTE_OPTION(   COAP_OPTION_IF_MATCH,       if_match, "If-Match")
-  COAP_SERIALIZE_STRING_OPTION( COAP_OPTION_URI_HOST,       uri_host, '\0', "Uri-Host")
-  COAP_SERIALIZE_BYTE_OPTION(   COAP_OPTION_ETAG,           etag, "ETag")
-  COAP_SERIALIZE_INT_OPTION(    COAP_OPTION_IF_NONE_MATCH,  content_type-coap_pkt->content_type, "If-None-Match") /* hack to get a zero field */
-  COAP_SERIALIZE_INT_OPTION(    COAP_OPTION_OBSERVE,        observe, "Observe")
-  COAP_SERIALIZE_INT_OPTION(    COAP_OPTION_URI_PORT,       uri_port, "Uri-Port")
-  COAP_SERIALIZE_MULTI_OPTION(  COAP_OPTION_LOCATION_PATH,  location_path, "Location-Path")
-  COAP_SERIALIZE_MULTI_OPTION(  COAP_OPTION_URI_PATH,       uri_path, "Uri-Path")
-  COAP_SERIALIZE_INT_OPTION(    COAP_OPTION_CONTENT_TYPE,   content_type, "Content-Format")
-  COAP_SERIALIZE_INT_OPTION(    COAP_OPTION_MAX_AGE,        max_age, "Max-Age")
-  COAP_SERIALIZE_MULTI_OPTION(  COAP_OPTION_URI_QUERY,      uri_query, "Uri-Query")
-  COAP_SERIALIZE_ACCEPT_OPTION( COAP_OPTION_ACCEPT,         accept, "Accept")
-  COAP_SERIALIZE_STRING_OPTION( COAP_OPTION_LOCATION_QUERY, location_query, '&', "Location-Query")
-  COAP_SERIALIZE_BLOCK_OPTION(  COAP_OPTION_BLOCK2,         block2, "Block2")
-  COAP_SERIALIZE_BLOCK_OPTION(  COAP_OPTION_BLOCK1,         block1, "Block1")
-  COAP_SERIALIZE_INT_OPTION(    COAP_OPTION_SIZE,           size, "Size")
-  COAP_SERIALIZE_STRING_OPTION( COAP_OPTION_PROXY_URI,      proxy_uri, '\0', "Proxy-Uri")
-
-  coap_pkt->options_len = option - options_buf;
-
-  PRINTF("-Done serializing at %p----\n", option);
-
-  switch (coap_pkt->protocol)
-  {
-  case COAP_UDP:
-  case COAP_UDP_DTLS:
-         coap_pkt->buffer[0]  = 0x00;
-         coap_pkt->buffer[0] |= COAP_HEADER_VERSION_MASK & (coap_pkt->version)<<COAP_HEADER_VERSION_POSITION;
-         coap_pkt->buffer[0] |= COAP_HEADER_TYPE_MASK & (coap_pkt->type)<<COAP_HEADER_TYPE_POSITION;
-         coap_pkt->buffer[0] |= COAP_HEADER_TOKEN_LEN_MASK & (coap_pkt->token_len)<<COAP_HEADER_TOKEN_LEN_POSITION;
-         coap_pkt->buffer[1] = coap_pkt->code;
-         coap_pkt->buffer[2] = (uint8_t) ((coap_pkt->mid)>>8);
-         coap_pkt->buffer[3] = (uint8_t) (coap_pkt->mid);
-         option = coap_pkt->buffer + COAP_HEADER_LEN;
-         break;
-
-  case COAP_TCP:
-  case COAP_TCP_TLS:
-      {
-          uint32_t len = coap_pkt->options_len;
-          if (coap_pkt->payload_len > 0)
-              len += COAP_OPTIONS_MARKER_LEN + coap_pkt->payload_len;
-
-          PRINTF("Options+Payload (len %u)", len);
-          if (len < 13)
-          {
-              coap_pkt->buffer[0] = (COAP_HEADER_TOKEN_LEN_MASK & coap_pkt->token_len);
-              coap_pkt->buffer[0] |= (len << 4);
-              coap_pkt->buffer[1] = coap_pkt->code;
-              option = coap_pkt->buffer + 2;
-          }
-          else if (len < ((1 << 8) + 13))
-          {
-              coap_pkt->buffer[0] = (COAP_HEADER_TOKEN_LEN_MASK & coap_pkt->token_len);
-              coap_pkt->buffer[0] |= (13 << 4);
-              coap_pkt->buffer[1] = len - 13;
-              coap_pkt->buffer[2] = coap_pkt->code;
-              option = coap_pkt->buffer + 3;
-          }
-          else if (len < ((1 << 16) + 269))
-          {
-              coap_pkt->buffer[0] = (COAP_HEADER_TOKEN_LEN_MASK & coap_pkt->token_len);
-              coap_pkt->buffer[0] |= (14 << 4);
-              coap_pkt->buffer[1] = ((len - 269) & 0xFF00) >> 8;
-              coap_pkt->buffer[2] = (len - 269) & 0x00FF;
-              coap_pkt->buffer[3] = coap_pkt->code;
-              option = coap_pkt->buffer + 4;
-          }
-          else
-          {
-              coap_pkt->buffer[0] = (COAP_HEADER_TOKEN_LEN_MASK & coap_pkt->token_len);
-              coap_pkt->buffer[0] |= (15 << 4);
-              coap_pkt->buffer[1] = ((len - 65805) & 0xFF000000) >> 24;
-              coap_pkt->buffer[2] = ((len - 65805) & 0x00FF0000) >> 16;
-              coap_pkt->buffer[3] = ((len - 65805) & 0x0000FF00) >> 8;
-              coap_pkt->buffer[4] = (len - 65805) & 0x00FF;
-              coap_pkt->buffer[5] = coap_pkt->code;
-              option = coap_pkt->buffer + 6;
-          }
-      }
-         break;
-  default:
-         break;
-  }
-
-  /* set Token */
-  PRINTF("Token (len %u)", coap_pkt->token_len);
-
-  for (current_number=0; current_number<coap_pkt->token_len; ++current_number)
-  {
-    PRINTF(" %02X", coap_pkt->token[current_number]);
-    *option = coap_pkt->token[current_number];
-    ++option;
-  }
-  PRINTF("-\n");
-
-  /* Copy options */
-  memcpy(option, options_buf, coap_pkt->options_len);
-  option += coap_pkt->options_len;
-  lwm2m_free(options_buf);
-
-  /* Free allocated header fields */
-  coap_free_header(packet);
-
-  /* Pack payload */
-  /* Payload marker */
-  if (coap_pkt->payload_len)
-  {
-    *option = 0xFF;
-    ++option;
-  }
-
-  memmove(option, coap_pkt->payload, coap_pkt->payload_len);
-
-  PRINTF("-Done %u B (header len %u, payload len %u)-\n", coap_pkt->payload_len + option - buffer, option - buffer, coap_pkt->payload_len);
-
-  PRINTF("Dump [0x%02X %02X %02X %02X  %02X %02X %02X %02X]\n",
-      coap_pkt->buffer[0],
-      coap_pkt->buffer[1],
-      coap_pkt->buffer[2],
-      coap_pkt->buffer[3],
-      coap_pkt->buffer[4],
-      coap_pkt->buffer[5],
-      coap_pkt->buffer[6],
-      coap_pkt->buffer[7]
-    );
-
-  return (option - buffer) + coap_pkt->payload_len; /* packet length */
+       coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
+       uint8_t *option = NULL;
+       unsigned int current_number = 0;
+
+       /* Initialize */
+       coap_pkt->buffer = buffer;
+       coap_pkt->version = 1;
+
+       PRINTF("\n-Serializing MID %u to %p\n", coap_pkt->mid, coap_pkt->buffer);
+
+       /* set header fields */
+       coap_pkt->buffer[0] = 0x00;
+       coap_pkt->buffer[0] |= COAP_HEADER_VERSION_MASK & (coap_pkt->version) << COAP_HEADER_VERSION_POSITION;
+       coap_pkt->buffer[0] |= COAP_HEADER_TYPE_MASK & (coap_pkt->type) << COAP_HEADER_TYPE_POSITION;
+       coap_pkt->buffer[0] |= COAP_HEADER_TOKEN_LEN_MASK & (coap_pkt->token_len) << COAP_HEADER_TOKEN_LEN_POSITION;
+       coap_pkt->buffer[1] = coap_pkt->code;
+       coap_pkt->buffer[2] = (uint8_t)((coap_pkt->mid) >> 8);
+       coap_pkt->buffer[3] = (uint8_t)(coap_pkt->mid);
+
+       /* set Token */
+       PRINTF("Token (len %u)", coap_pkt->token_len);
+       option = coap_pkt->buffer + COAP_HEADER_LEN;
+       for (current_number = 0; current_number < coap_pkt->token_len; ++current_number) {
+               PRINTF(" %02X", coap_pkt->token[current_number]);
+               *option = coap_pkt->token[current_number];
+               ++option;
+       }
+       PRINTF("-\n");
+
+       /* Serialize options */
+       current_number = 0;
+
+       PRINTF("-Serializing options at %p-\n", option);
+
+       /* The options must be serialized in the order of their number */
+       COAP_SERIALIZE_BYTE_OPTION(COAP_OPTION_IF_MATCH, if_match, "If-Match")
+       COAP_SERIALIZE_STRING_OPTION(COAP_OPTION_URI_HOST, uri_host, '\0', "Uri-Host")
+       COAP_SERIALIZE_BYTE_OPTION(COAP_OPTION_ETAG, etag, "ETag")
+       COAP_SERIALIZE_INT_OPTION(COAP_OPTION_IF_NONE_MATCH, content_type - coap_pkt->content_type, "If-None-Match")    /* hack to get a zero field */
+       COAP_SERIALIZE_INT_OPTION(COAP_OPTION_OBSERVE, observe, "Observe")
+       COAP_SERIALIZE_INT_OPTION(COAP_OPTION_URI_PORT, uri_port, "Uri-Port")
+       COAP_SERIALIZE_MULTI_OPTION(COAP_OPTION_LOCATION_PATH, location_path, "Location-Path")
+       COAP_SERIALIZE_MULTI_OPTION(COAP_OPTION_URI_PATH, uri_path, "Uri-Path")
+       COAP_SERIALIZE_INT_OPTION(COAP_OPTION_CONTENT_TYPE, content_type, "Content-Format")
+       COAP_SERIALIZE_INT_OPTION(COAP_OPTION_MAX_AGE, max_age, "Max-Age")
+       COAP_SERIALIZE_MULTI_OPTION(COAP_OPTION_URI_QUERY, uri_query, "Uri-Query")
+       COAP_SERIALIZE_ACCEPT_OPTION(COAP_OPTION_ACCEPT, accept, "Accept")
+       COAP_SERIALIZE_STRING_OPTION(COAP_OPTION_LOCATION_QUERY, location_query, '&', "Location-Query")
+       COAP_SERIALIZE_BLOCK_OPTION(COAP_OPTION_BLOCK2, block2, "Block2")
+       COAP_SERIALIZE_BLOCK_OPTION(COAP_OPTION_BLOCK1, block1, "Block1")
+       COAP_SERIALIZE_INT_OPTION(COAP_OPTION_SIZE, size, "Size")
+       COAP_SERIALIZE_STRING_OPTION(COAP_OPTION_PROXY_URI, proxy_uri, '\0', "Proxy-Uri")
+
+       PRINTF("-Done serializing at %p----\n", option);
+
+       /* Free allocated header fields */
+       coap_free_header(packet);
+
+       /* Pack payload */
+       /* Payload marker */
+       if (coap_pkt->payload_len) {
+               *option = 0xFF;
+               ++option;
+       }
+
+       memmove(option, coap_pkt->payload, coap_pkt->payload_len);
+
+       PRINTF("-Done %u B (header len %u, payload len %u)-\n", coap_pkt->payload_len + option - buffer, option - buffer, coap_pkt->payload_len);
+
+       PRINTF("Dump [0x%02X %02X %02X %02X  %02X %02X %02X %02X]\n", coap_pkt->buffer[0], coap_pkt->buffer[1], coap_pkt->buffer[2], coap_pkt->buffer[3], coap_pkt->buffer[4], coap_pkt->buffer[5], coap_pkt->buffer[6], coap_pkt->buffer[7]
+                 );
+
+       return (option - buffer) + coap_pkt->payload_len;       /* packet length */
 }
+
 /*-----------------------------------------------------------------------------------*/
-coap_status_t
-coap_parse_message(void *packet, coap_protocol_t protocol, uint8_t *data, uint16_t data_len)
+coap_status_t coap_parse_message(void *packet, coap_protocol_t protocol, uint8_t *data, uint16_t data_len)
 {
-  coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
-  uint8_t *current_option = NULL;
-  unsigned int option_number = 0;
-  unsigned int option_delta = 0;
-  size_t option_length = 0;
-  unsigned int *x;
-
-  /* Initialize packet */
-  memset(coap_pkt, 0, sizeof(coap_packet_t));
-
-  coap_pkt->protocol = protocol;
-
-  /* pointer to packet bytes */
-  coap_pkt->buffer = data;
-
-  /* parse header fields */
-  switch(coap_pkt->protocol)
-  {
-  case COAP_UDP:
-  case COAP_UDP_DTLS:
-         coap_pkt->version = (COAP_HEADER_VERSION_MASK & coap_pkt->buffer[0])>>COAP_HEADER_VERSION_POSITION;
-         coap_pkt->type = (COAP_HEADER_TYPE_MASK & coap_pkt->buffer[0])>>COAP_HEADER_TYPE_POSITION;
-         coap_pkt->token_len = MIN(COAP_TOKEN_LEN, (COAP_HEADER_TOKEN_LEN_MASK & coap_pkt->buffer[0])>>COAP_HEADER_TOKEN_LEN_POSITION);
-         coap_pkt->code = coap_pkt->buffer[1];
-         coap_pkt->mid = coap_pkt->buffer[2]<<8 | coap_pkt->buffer[3];
-         current_option = data + COAP_HEADER_LEN;
-         break;
-  case COAP_TCP:
-  case COAP_TCP_TLS:
-      coap_pkt->token_len = coap_pkt->buffer[0] & COAP_HEADER_TOKEN_LEN_MASK;
-      int nibble = (coap_pkt->buffer[0] & 0xF0) >> 4;
-      if (nibble < 13)
-      {
-          coap_pkt->code = coap_pkt->buffer[1];
-          current_option = data + 2;
-      }
-      else if (nibble == 13)
-      {
-          coap_pkt->code = coap_pkt->buffer[2];
-          current_option = data + 3;
-      }
-      else if (nibble == 14)
-      {
-          coap_pkt->code = coap_pkt->buffer[3];
-          current_option = data + 4;
-      }
-      else
-      {
-          coap_pkt->code = coap_pkt->buffer[5];
-          current_option = data + 6;
-      }
-
-      /* Not passed in TCP, just set defaults */
-      coap_pkt->version = 1;
-      coap_pkt->type = COAP_TYPE_CON;
-      coap_pkt->mid = 0;
-         break;
-  default:
-         break;
-  }
-
-  if (coap_pkt->version != 1)
-  {
-    coap_error_message = "CoAP version must be 1";
-    return BAD_REQUEST_4_00;
-  }
-
-  if (coap_pkt->token_len != 0)
-  {
-      memcpy(coap_pkt->token, current_option, coap_pkt->token_len);
-      SET_OPTION(coap_pkt, COAP_OPTION_TOKEN);
-
-      PRINTF("Token (len %u) [0x%02X%02X%02X%02X%02X%02X%02X%02X]\n", coap_pkt->token_len,
-        coap_pkt->token[0],
-        coap_pkt->token[1],
-        coap_pkt->token[2],
-        coap_pkt->token[3],
-        coap_pkt->token[4],
-        coap_pkt->token[5],
-        coap_pkt->token[6],
-        coap_pkt->token[7]
-      ); /*FIXME always prints 8 bytes */
-  }
-
-  /* parse options */
-  current_option += coap_pkt->token_len;
-
-  while (current_option < data+data_len)
-  {
-    /* Payload marker 0xFF, currently only checking for 0xF* because rest is reserved */
-    if ((current_option[0] & 0xF0)==0xF0)
-    {
-      coap_pkt->payload = ++current_option;
-      coap_pkt->payload_len = data_len - (coap_pkt->payload - data);
-
-      break;
-    }
-
-    option_delta = current_option[0]>>4;
-    option_length = current_option[0] & 0x0F;
-    ++current_option;
-
-    /* avoids code duplication without function overhead */
-    x = &option_delta;
-    do
-    {
-      if (*x==13)
-      {
-        *x += current_option[0];
-        ++current_option;
-      }
-      else if (*x==14)
-      {
-        *x += 255;
-        *x += current_option[0]<<8;
-        ++current_option;
-        *x += current_option[0];
-        ++current_option;
-      }
-    }
-    while (x!=(unsigned int *)&option_length && (x=(unsigned int *)&option_length));
-
-    option_number += option_delta;
-
-    PRINTF("OPTION %u (delta %u, len %u): ", option_number, option_delta, option_length);
-
-    SET_OPTION(coap_pkt, option_number);
-
-    switch (option_number)
-    {
-      case COAP_OPTION_CONTENT_TYPE:
-        coap_pkt->content_type = coap_parse_int_option(current_option, option_length);
-        PRINTF("Content-Format [%u]\n", coap_pkt->content_type);
-        break;
-      case COAP_OPTION_MAX_AGE:
-        coap_pkt->max_age = coap_parse_int_option(current_option, option_length);
-        PRINTF("Max-Age [%lu]\n", coap_pkt->max_age);
-        break;
-      case COAP_OPTION_ETAG:
-        coap_pkt->etag_len = (uint8_t)(MIN(COAP_ETAG_LEN, option_length));
-        memcpy(coap_pkt->etag, current_option, coap_pkt->etag_len);
-        PRINTF("ETag %u [0x%02X%02X%02X%02X%02X%02X%02X%02X]\n", coap_pkt->etag_len,
-          coap_pkt->etag[0],
-          coap_pkt->etag[1],
-          coap_pkt->etag[2],
-          coap_pkt->etag[3],
-          coap_pkt->etag[4],
-          coap_pkt->etag[5],
-          coap_pkt->etag[6],
-          coap_pkt->etag[7]
-        ); /*FIXME always prints 8 bytes */
-        break;
-      case COAP_OPTION_ACCEPT:
-        if (coap_pkt->accept_num < COAP_MAX_ACCEPT_NUM)
-        {
-          coap_pkt->accept[coap_pkt->accept_num] = coap_parse_int_option(current_option, option_length);
-          coap_pkt->accept_num += 1;
-          PRINTF("Accept [%u]\n", coap_pkt->content_type);
-        }
-        break;
-      case COAP_OPTION_IF_MATCH:
-        /*FIXME support multiple ETags */
-        coap_pkt->if_match_len = (uint8_t)(MIN(COAP_ETAG_LEN, option_length));
-        memcpy(coap_pkt->if_match, current_option, coap_pkt->if_match_len);
-        PRINTF("If-Match %u [0x%02X%02X%02X%02X%02X%02X%02X%02X]\n", coap_pkt->if_match_len,
-          coap_pkt->if_match[0],
-          coap_pkt->if_match[1],
-          coap_pkt->if_match[2],
-          coap_pkt->if_match[3],
-          coap_pkt->if_match[4],
-          coap_pkt->if_match[5],
-          coap_pkt->if_match[6],
-          coap_pkt->if_match[7]
-        ); /*FIXME always prints 8 bytes */
-        break;
-      case COAP_OPTION_IF_NONE_MATCH:
-        coap_pkt->if_none_match = 1;
-        PRINTF("If-None-Match\n");
-        break;
-
-      case COAP_OPTION_URI_HOST:
-        coap_pkt->uri_host = current_option;
-        coap_pkt->uri_host_len = option_length;
-        PRINTF("Uri-Host [%.*s]\n", coap_pkt->uri_host_len, coap_pkt->uri_host);
-        break;
-      case COAP_OPTION_URI_PORT:
-        coap_pkt->uri_port = coap_parse_int_option(current_option, option_length);
-        PRINTF("Uri-Port [%u]\n", coap_pkt->uri_port);
-        break;
-      case COAP_OPTION_URI_PATH:
-        /* coap_merge_multi_option() operates in-place on the IPBUF, but final packet field should be const string -> cast to string */
-        // coap_merge_multi_option( (char **) &(coap_pkt->uri_path), &(coap_pkt->uri_path_len), current_option, option_length, 0);
-        coap_add_multi_option( &(coap_pkt->uri_path), current_option, option_length, 1);
-        PRINTF("Uri-Path [%.*s]\n", option_length, current_option);
-        break;
-      case COAP_OPTION_URI_QUERY:
-        /* coap_merge_multi_option() operates in-place on the IPBUF, but final packet field should be const string -> cast to string */
-        // coap_merge_multi_option( (char **) &(coap_pkt->uri_query), &(coap_pkt->uri_query_len), current_option, option_length, '&');
-        coap_add_multi_option( &(coap_pkt->uri_query), current_option, option_length, 1);
-        PRINTF("Uri-Query [%.*s]\n", option_length, current_option);
-        break;
-
-      case COAP_OPTION_LOCATION_PATH:
-        coap_add_multi_option( &(coap_pkt->location_path), current_option, option_length, 1);
-        break;
-      case COAP_OPTION_LOCATION_QUERY:
-        /* coap_merge_multi_option() operates in-place on the IPBUF, but final packet field should be const string -> cast to string */
-        coap_merge_multi_option( &(coap_pkt->location_query), &(coap_pkt->location_query_len), current_option, option_length, '&');
-        PRINTF("Location-Query [%.*s]\n", option_length, current_option);
-        break;
-
-      case COAP_OPTION_PROXY_URI:
-        /*FIXME check for own end-point */
-        coap_pkt->proxy_uri = current_option;
-        coap_pkt->proxy_uri_len = option_length;
-        /*TODO length > 270 not implemented (actually not required) */
-        PRINTF("Proxy-Uri NOT IMPLEMENTED [%.*s]\n", coap_pkt->proxy_uri_len, coap_pkt->proxy_uri);
-        coap_error_message = "This is a constrained server (Contiki)";
-        return PROXYING_NOT_SUPPORTED_5_05;
-        break;
-
-      case COAP_OPTION_OBSERVE:
-        coap_pkt->observe = coap_parse_int_option(current_option, option_length);
-        PRINTF("Observe [%lu]\n", coap_pkt->observe);
-        break;
-      case COAP_OPTION_BLOCK2:
-        coap_pkt->block2_num = coap_parse_int_option(current_option, option_length);
-        coap_pkt->block2_more = (coap_pkt->block2_num & 0x08)>>3;
-        coap_pkt->block2_size = 16 << (coap_pkt->block2_num & 0x07);
-        coap_pkt->block2_offset = (coap_pkt->block2_num & ~0x0000000F)<<(coap_pkt->block2_num & 0x07);
-        coap_pkt->block2_num >>= 4;
-        PRINTF("Block2 [%lu%s (%u B/blk)]\n", coap_pkt->block2_num, coap_pkt->block2_more ? "+" : "", coap_pkt->block2_size);
-        break;
-      case COAP_OPTION_BLOCK1:
-        coap_pkt->block1_num = coap_parse_int_option(current_option, option_length);
-        coap_pkt->block1_more = (coap_pkt->block1_num & 0x08)>>3;
-        coap_pkt->block1_size = 16 << (coap_pkt->block1_num & 0x07);
-        coap_pkt->block1_offset = (coap_pkt->block1_num & ~0x0000000F)<<(coap_pkt->block1_num & 0x07);
-        coap_pkt->block1_num >>= 4;
-        PRINTF("Block1 [%lu%s (%u B/blk)]\n", coap_pkt->block1_num, coap_pkt->block1_more ? "+" : "", coap_pkt->block1_size);
-        break;
-      case COAP_OPTION_SIZE:
-        coap_pkt->size = coap_parse_int_option(current_option, option_length);
-        PRINTF("Size [%lu]\n", coap_pkt->size);
-        break;
-      default:
-        PRINTF("unknown (%u)\n", option_number);
-        /* Check if critical (odd) */
-        if (option_number & 1)
-        {
-          coap_error_message = "Unsupported critical option";
-          return BAD_OPTION_4_02;
-        }
-    }
-
-    current_option += option_length;
-  } /* for */
-  PRINTF("-Done parsing-------\n");
-
-
-
-  return NO_ERROR;
+       coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
+       uint8_t *current_option = NULL;
+       unsigned int option_number = 0;
+       unsigned int option_delta = 0;
+       size_t option_length = 0;
+       unsigned int *x;
+
+       /* Initialize packet */
+       memset(coap_pkt, 0, sizeof(coap_packet_t));
+
+       coap_pkt->protocol = protocol;
+
+       /* pointer to packet bytes */
+       coap_pkt->buffer = data;
+
+       /* parse header fields */
+       switch (coap_pkt->protocol) {
+       case COAP_UDP:
+       case COAP_UDP_DTLS:
+               coap_pkt->version = (COAP_HEADER_VERSION_MASK & coap_pkt->buffer[0]) >> COAP_HEADER_VERSION_POSITION;
+               coap_pkt->type = (COAP_HEADER_TYPE_MASK & coap_pkt->buffer[0]) >> COAP_HEADER_TYPE_POSITION;
+               coap_pkt->token_len = MIN(COAP_TOKEN_LEN, (COAP_HEADER_TOKEN_LEN_MASK & coap_pkt->buffer[0]) >> COAP_HEADER_TOKEN_LEN_POSITION);
+               coap_pkt->code = coap_pkt->buffer[1];
+               coap_pkt->mid = coap_pkt->buffer[2] << 8 | coap_pkt->buffer[3];
+               current_option = data + COAP_HEADER_LEN;
+               break;
+       case COAP_TCP:
+       case COAP_TCP_TLS:
+               coap_pkt->token_len = coap_pkt->buffer[0] & COAP_HEADER_TOKEN_LEN_MASK;
+               int nibble = (coap_pkt->buffer[0] & 0xF0) >> 4;
+               if (nibble < 13) {
+                       coap_pkt->code = coap_pkt->buffer[1];
+                       current_option = data + 2;
+               } else if (nibble == 13) {
+                       coap_pkt->code = coap_pkt->buffer[2];
+                       current_option = data + 3;
+               } else if (nibble == 14) {
+                       coap_pkt->code = coap_pkt->buffer[3];
+                       current_option = data + 4;
+               } else {
+                       coap_pkt->code = coap_pkt->buffer[5];
+                       current_option = data + 6;
+               }
+
+               /* Not passed in TCP, just set defaults */
+               coap_pkt->version = 1;
+               coap_pkt->type = COAP_TYPE_CON;
+               coap_pkt->mid = 0;
+               break;
+       default:
+               break;
+       }
+
+       if (coap_pkt->version != 1) {
+               coap_error_message = "CoAP version must be 1";
+               return BAD_REQUEST_4_00;
+       }
+
+       if (coap_pkt->token_len != 0) {
+               memcpy(coap_pkt->token, current_option, coap_pkt->token_len);
+               SET_OPTION(coap_pkt, COAP_OPTION_TOKEN);
+
+               PRINTF("Token (len %u) [0x%02X%02X%02X%02X%02X%02X%02X%02X]\n", coap_pkt->token_len, coap_pkt->token[0], coap_pkt->token[1], coap_pkt->token[2], coap_pkt->token[3], coap_pkt->token[4], coap_pkt->token[5], coap_pkt->token[6], coap_pkt->token[7]
+                         );                                    /*FIXME always prints 8 bytes */
+       }
+
+       /* parse options */
+       current_option += coap_pkt->token_len;
+
+       while (current_option < data + data_len) {
+               /* Payload marker 0xFF, currently only checking for 0xF* because rest is reserved */
+               if ((current_option[0] & 0xF0) == 0xF0) {
+                       coap_pkt->payload = ++current_option;
+                       coap_pkt->payload_len = data_len - (coap_pkt->payload - data);
+
+                       break;
+               }
+
+               option_delta = current_option[0] >> 4;
+               option_length = current_option[0] & 0x0F;
+               ++current_option;
+
+               /* avoids code duplication without function overhead */
+               x = &option_delta;
+               do {
+                       if (*x == 13) {
+                               *x += current_option[0];
+                               ++current_option;
+                       } else if (*x == 14) {
+                               *x += 255;
+                               *x += current_option[0] << 8;
+                               ++current_option;
+                               *x += current_option[0];
+                               ++current_option;
+                       }
+               } while (x != (unsigned int *)&option_length && (x = (unsigned int *)&option_length));
+
+               option_number += option_delta;
+
+               PRINTF("OPTION %u (delta %u, len %u): ", option_number, option_delta, option_length);
+
+               SET_OPTION(coap_pkt, option_number);
+
+               switch (option_number) {
+               case COAP_OPTION_CONTENT_TYPE:
+                       coap_pkt->content_type = coap_parse_int_option(current_option, option_length);
+                       PRINTF("Content-Format [%u]\n", coap_pkt->content_type);
+                       break;
+               case COAP_OPTION_MAX_AGE:
+                       coap_pkt->max_age = coap_parse_int_option(current_option, option_length);
+                       PRINTF("Max-Age [%lu]\n", coap_pkt->max_age);
+                       break;
+               case COAP_OPTION_ETAG:
+                       coap_pkt->etag_len = (uint8_t)(MIN(COAP_ETAG_LEN, option_length));
+                       memcpy(coap_pkt->etag, current_option, coap_pkt->etag_len);
+                       PRINTF("ETag %u [0x%02X%02X%02X%02X%02X%02X%02X%02X]\n", coap_pkt->etag_len, coap_pkt->etag[0], coap_pkt->etag[1], coap_pkt->etag[2], coap_pkt->etag[3], coap_pkt->etag[4], coap_pkt->etag[5], coap_pkt->etag[6], coap_pkt->etag[7]
+                                 );                            /*FIXME always prints 8 bytes */
+                       break;
+               case COAP_OPTION_ACCEPT:
+                       if (coap_pkt->accept_num < COAP_MAX_ACCEPT_NUM) {
+                               coap_pkt->accept[coap_pkt->accept_num] = coap_parse_int_option(current_option, option_length);
+                               coap_pkt->accept_num += 1;
+                               PRINTF("Accept [%u]\n", coap_pkt->content_type);
+                       }
+                       break;
+               case COAP_OPTION_IF_MATCH:
+                       /*FIXME support multiple ETags */
+                       coap_pkt->if_match_len = (uint8_t)(MIN(COAP_ETAG_LEN, option_length));
+                       memcpy(coap_pkt->if_match, current_option, coap_pkt->if_match_len);
+                       PRINTF("If-Match %u [0x%02X%02X%02X%02X%02X%02X%02X%02X]\n", coap_pkt->if_match_len, coap_pkt->if_match[0], coap_pkt->if_match[1], coap_pkt->if_match[2], coap_pkt->if_match[3], coap_pkt->if_match[4], coap_pkt->if_match[5], coap_pkt->if_match[6], coap_pkt->if_match[7]
+                                 );                            /*FIXME always prints 8 bytes */
+                       break;
+               case COAP_OPTION_IF_NONE_MATCH:
+                       coap_pkt->if_none_match = 1;
+                       PRINTF("If-None-Match\n");
+                       break;
+
+               case COAP_OPTION_URI_HOST:
+                       coap_pkt->uri_host = current_option;
+                       coap_pkt->uri_host_len = option_length;
+                       PRINTF("Uri-Host [%.*s]\n", coap_pkt->uri_host_len, coap_pkt->uri_host);
+                       break;
+               case COAP_OPTION_URI_PORT:
+                       coap_pkt->uri_port = coap_parse_int_option(current_option, option_length);
+                       PRINTF("Uri-Port [%u]\n", coap_pkt->uri_port);
+                       break;
+               case COAP_OPTION_URI_PATH:
+                       /* coap_merge_multi_option() operates in-place on the IPBUF, but final packet field should be const string -> cast to string */
+                       // coap_merge_multi_option( (char **) &(coap_pkt->uri_path), &(coap_pkt->uri_path_len), current_option, option_length, 0);
+                       coap_add_multi_option(&(coap_pkt->uri_path), current_option, option_length, 1);
+                       PRINTF("Uri-Path [%.*s]\n", option_length, current_option);
+                       break;
+               case COAP_OPTION_URI_QUERY:
+                       /* coap_merge_multi_option() operates in-place on the IPBUF, but final packet field should be const string -> cast to string */
+                       // coap_merge_multi_option( (char **) &(coap_pkt->uri_query), &(coap_pkt->uri_query_len), current_option, option_length, '&');
+                       coap_add_multi_option(&(coap_pkt->uri_query), current_option, option_length, 1);
+                       PRINTF("Uri-Query [%.*s]\n", option_length, current_option);
+                       break;
+
+               case COAP_OPTION_LOCATION_PATH:
+                       coap_add_multi_option(&(coap_pkt->location_path), current_option, option_length, 1);
+                       break;
+               case COAP_OPTION_LOCATION_QUERY:
+                       /* coap_merge_multi_option() operates in-place on the IPBUF, but final packet field should be const string -> cast to string */
+                       coap_merge_multi_option(&(coap_pkt->location_query), &(coap_pkt->location_query_len), current_option, option_length, '&');
+                       PRINTF("Location-Query [%.*s]\n", option_length, current_option);
+                       break;
+
+               case COAP_OPTION_PROXY_URI:
+                       /*FIXME check for own end-point */
+                       coap_pkt->proxy_uri = current_option;
+                       coap_pkt->proxy_uri_len = option_length;
+                       /*TODO length > 270 not implemented (actually not required) */
+                       PRINTF("Proxy-Uri NOT IMPLEMENTED [%.*s]\n", coap_pkt->proxy_uri_len, coap_pkt->proxy_uri);
+                       coap_error_message = "This is a constrained server (Contiki)";
+                       return PROXYING_NOT_SUPPORTED_5_05;
+                       break;
+
+               case COAP_OPTION_OBSERVE:
+                       coap_pkt->observe = coap_parse_int_option(current_option, option_length);
+                       PRINTF("Observe [%lu]\n", coap_pkt->observe);
+                       break;
+               case COAP_OPTION_BLOCK2:
+                       coap_pkt->block2_num = coap_parse_int_option(current_option, option_length);
+                       coap_pkt->block2_more = (coap_pkt->block2_num & 0x08) >> 3;
+                       coap_pkt->block2_size = 16 << (coap_pkt->block2_num & 0x07);
+                       coap_pkt->block2_offset = (coap_pkt->block2_num & ~0x0000000F) << (coap_pkt->block2_num & 0x07);
+                       coap_pkt->block2_num >>= 4;
+                       PRINTF("Block2 [%lu%s (%u B/blk)]\n", coap_pkt->block2_num, coap_pkt->block2_more ? "+" : "", coap_pkt->block2_size);
+                       break;
+               case COAP_OPTION_BLOCK1:
+                       coap_pkt->block1_num = coap_parse_int_option(current_option, option_length);
+                       coap_pkt->block1_more = (coap_pkt->block1_num & 0x08) >> 3;
+                       coap_pkt->block1_size = 16 << (coap_pkt->block1_num & 0x07);
+                       coap_pkt->block1_offset = (coap_pkt->block1_num & ~0x0000000F) << (coap_pkt->block1_num & 0x07);
+                       coap_pkt->block1_num >>= 4;
+                       PRINTF("Block1 [%lu%s (%u B/blk)]\n", coap_pkt->block1_num, coap_pkt->block1_more ? "+" : "", coap_pkt->block1_size);
+                       break;
+               case COAP_OPTION_SIZE:
+                       coap_pkt->size = coap_parse_int_option(current_option, option_length);
+                       PRINTF("Size [%lu]\n", coap_pkt->size);
+                       break;
+               default:
+                       PRINTF("unknown (%u)\n", option_number);
+                       /* Check if critical (odd) */
+                       if (option_number & 1) {
+                               coap_error_message = "Unsupported critical option";
+                               return BAD_OPTION_4_02;
+                       }
+               }
+
+               current_option += option_length;
+       }                                                       /* for */
+       PRINTF("-Done parsing-------\n");
+
+       return NO_ERROR;
 }
+
 /*-----------------------------------------------------------------------------------*/
 /*- REST FRAMEWORK FUNCTIONS --------------------------------------------------------*/
 /*-----------------------------------------------------------------------------------*/
-int
-coap_get_query_variable(void *packet, const char *name, const char **output)
+int coap_get_query_variable(void *packet, const char *name, const char **output)
 {
-/*
-  coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
-
-  if (IS_OPTION(coap_pkt, COAP_OPTION_URI_QUERY)) {
-    return coap_get_variable(coap_pkt->uri_query, coap_pkt->uri_query_len, name, output);
-  }
-*/
-  return 0;
+       /*
+         coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
+
+         if (IS_OPTION(coap_pkt, COAP_OPTION_URI_QUERY)) {
+           return coap_get_variable(coap_pkt->uri_query, coap_pkt->uri_query_len, name, output);
+         }
+       */
+       return 0;
 }
 
-int
-coap_get_post_variable(void *packet, const char *name, const char **output)
+int coap_get_post_variable(void *packet, const char *name, const char **output)
 {
-  coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
+       coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
 
-  if (coap_pkt->payload_len) {
-    return coap_get_variable(coap_pkt->payload, coap_pkt->payload_len, name, output);
-  }
-  return 0;
+       if (coap_pkt->payload_len) {
+               return coap_get_variable(coap_pkt->payload, coap_pkt->payload_len, name, output);
+       }
+       return 0;
 }
+
 /*-----------------------------------------------------------------------------------*/
-int
-coap_set_status_code(void *packet, unsigned int code)
+int coap_set_status_code(void *packet, unsigned int code)
 {
-  if (code <= 0xFF)
-  {
-    ((coap_packet_t *)packet)->code = (uint8_t) code;
-    return 1;
-  }
-  else
-  {
-    return 0;
-  }
+       if (code <= 0xFF) {
+               ((coap_packet_t *) packet)->code = (uint8_t) code;
+               return 1;
+       } else {
+               return 0;
+       }
 }
+
 /*-----------------------------------------------------------------------------------*/
 /*- HEADER OPTION GETTERS AND SETTERS -----------------------------------------------*/
 /*-----------------------------------------------------------------------------------*/
-unsigned int
-coap_get_header_content_type(void *packet)
+unsigned int coap_get_header_content_type(void *packet)
 {
-  coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
+       coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
 
-  if (!IS_OPTION(coap_pkt, COAP_OPTION_CONTENT_TYPE)) return -1;
+       if (!IS_OPTION(coap_pkt, COAP_OPTION_CONTENT_TYPE)) {
+               return -1;
+       }
 
-  return coap_pkt->content_type;
+       return coap_pkt->content_type;
 }
 
-int
-coap_set_header_content_type(void *packet, unsigned int content_type)
+int coap_set_header_content_type(void *packet, unsigned int content_type)
 {
-  coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
+       coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
 
-  coap_pkt->content_type = (coap_content_type_t) content_type;
-  SET_OPTION(coap_pkt, COAP_OPTION_CONTENT_TYPE);
-  return 1;
+       coap_pkt->content_type = (coap_content_type_t) content_type;
+       SET_OPTION(coap_pkt, COAP_OPTION_CONTENT_TYPE);
+       return 1;
 }
+
 /*-----------------------------------------------------------------------------------*/
-int
-coap_get_header_accept(void *packet, const uint16_t **accept)
+int coap_get_header_accept(void *packet, const uint16_t **accept)
 {
-  coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
+       coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
 
-  if (!IS_OPTION(coap_pkt, COAP_OPTION_ACCEPT)) return 0;
+       if (!IS_OPTION(coap_pkt, COAP_OPTION_ACCEPT)) {
+               return 0;
+       }
 
-  *accept = coap_pkt->accept;
-  return coap_pkt->accept_num;
+       *accept = coap_pkt->accept;
+       return coap_pkt->accept_num;
 }
 
-int
-coap_set_header_accept(void *packet, uint16_t accept)
+int coap_set_header_accept(void *packet, uint16_t accept)
 {
-  coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
+       coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
 
-  if (coap_pkt->accept_num < COAP_MAX_ACCEPT_NUM)
-  {
-    coap_pkt->accept[coap_pkt->accept_num] = accept;
-    coap_pkt->accept_num += 1;
+       if (coap_pkt->accept_num < COAP_MAX_ACCEPT_NUM) {
+               coap_pkt->accept[coap_pkt->accept_num] = accept;
+               coap_pkt->accept_num += 1;
 
-    SET_OPTION(coap_pkt, COAP_OPTION_ACCEPT);
-  }
-  return coap_pkt->accept_num;
+               SET_OPTION(coap_pkt, COAP_OPTION_ACCEPT);
+       }
+       return coap_pkt->accept_num;
 }
+
 /*-----------------------------------------------------------------------------------*/
-int
-coap_get_header_max_age(void *packet, uint32_t *age)
+int coap_get_header_max_age(void *packet, uint32_t *age)
 {
-  coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
-
-  if (!IS_OPTION(coap_pkt, COAP_OPTION_MAX_AGE)) {
-    *age = COAP_DEFAULT_MAX_AGE;
-  } else {
-    *age = coap_pkt->max_age;
-  }
-  return 1;
+       coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
+
+       if (!IS_OPTION(coap_pkt, COAP_OPTION_MAX_AGE)) {
+               *age = COAP_DEFAULT_MAX_AGE;
+       } else {
+               *age = coap_pkt->max_age;
+       }
+       return 1;
 }
 
-int
-coap_set_header_max_age(void *packet, uint32_t age)
+int coap_set_header_max_age(void *packet, uint32_t age)
 {
-  coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
+       coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
 
-  coap_pkt->max_age = age;
-  SET_OPTION(coap_pkt, COAP_OPTION_MAX_AGE);
-  return 1;
+       coap_pkt->max_age = age;
+       SET_OPTION(coap_pkt, COAP_OPTION_MAX_AGE);
+       return 1;
 }
+
 /*-----------------------------------------------------------------------------------*/
-int
-coap_get_header_etag(void *packet, const uint8_t **etag)
+int coap_get_header_etag(void *packet, const uint8_t **etag)
 {
-  coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
+       coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
 
-  if (!IS_OPTION(coap_pkt, COAP_OPTION_ETAG)) return 0;
+       if (!IS_OPTION(coap_pkt, COAP_OPTION_ETAG)) {
+               return 0;
+       }
 
-  *etag = coap_pkt->etag;
-  return coap_pkt->etag_len;
+       *etag = coap_pkt->etag;
+       return coap_pkt->etag_len;
 }
 
-int
-coap_set_header_etag(void *packet, const uint8_t *etag, size_t etag_len)
+int coap_set_header_etag(void *packet, const uint8_t *etag, size_t etag_len)
 {
-  coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
+       coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
 
-  coap_pkt->etag_len = (uint8_t)(MIN(COAP_ETAG_LEN, etag_len));
-  memcpy(coap_pkt->etag, etag, coap_pkt->etag_len);
+       coap_pkt->etag_len = (uint8_t)(MIN(COAP_ETAG_LEN, etag_len));
+       memcpy(coap_pkt->etag, etag, coap_pkt->etag_len);
 
-  SET_OPTION(coap_pkt, COAP_OPTION_ETAG);
-  return coap_pkt->etag_len;
+       SET_OPTION(coap_pkt, COAP_OPTION_ETAG);
+       return coap_pkt->etag_len;
 }
+
 /*-----------------------------------------------------------------------------------*/
 /*FIXME support multiple ETags */
-int
-coap_get_header_if_match(void *packet, const uint8_t **etag)
+int coap_get_header_if_match(void *packet, const uint8_t **etag)
 {
-  coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
+       coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
 
-  if (!IS_OPTION(coap_pkt, COAP_OPTION_IF_MATCH)) return 0;
+       if (!IS_OPTION(coap_pkt, COAP_OPTION_IF_MATCH)) {
+               return 0;
+       }
 
-  *etag = coap_pkt->if_match;
-  return coap_pkt->if_match_len;
+       *etag = coap_pkt->if_match;
+       return coap_pkt->if_match_len;
 }
 
-int
-coap_set_header_if_match(void *packet, const uint8_t *etag, size_t etag_len)
+int coap_set_header_if_match(void *packet, const uint8_t *etag, size_t etag_len)
 {
-  coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
+       coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
 
-  coap_pkt->if_match_len = (uint8_t)(MIN(COAP_ETAG_LEN, etag_len));
-  memcpy(coap_pkt->if_match, etag, coap_pkt->if_match_len);
+       coap_pkt->if_match_len = (uint8_t)(MIN(COAP_ETAG_LEN, etag_len));
+       memcpy(coap_pkt->if_match, etag, coap_pkt->if_match_len);
 
-  SET_OPTION(coap_pkt, COAP_OPTION_IF_MATCH);
-  return coap_pkt->if_match_len;
+       SET_OPTION(coap_pkt, COAP_OPTION_IF_MATCH);
+       return coap_pkt->if_match_len;
 }
+
 /*-----------------------------------------------------------------------------------*/
-int
-coap_get_header_if_none_match(void *packet)
+int coap_get_header_if_none_match(void *packet)
 {
-  return IS_OPTION((coap_packet_t *)packet, COAP_OPTION_IF_NONE_MATCH) ? 1 : 0;
+       return IS_OPTION((coap_packet_t *) packet, COAP_OPTION_IF_NONE_MATCH) ? 1 : 0;
 }
 
-int
-coap_set_header_if_none_match(void *packet)
+int coap_set_header_if_none_match(void *packet)
 {
-  SET_OPTION((coap_packet_t *)packet, COAP_OPTION_IF_NONE_MATCH);
-  return 1;
+       SET_OPTION((coap_packet_t *) packet, COAP_OPTION_IF_NONE_MATCH);
+       return 1;
 }
+
 /*-----------------------------------------------------------------------------------*/
-int
-coap_get_header_token(void *packet, const uint8_t **token)
+int coap_get_header_token(void *packet, const uint8_t **token)
 {
-  coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
+       coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
 
-  if (!IS_OPTION(coap_pkt, COAP_OPTION_TOKEN)) return 0;
+       if (!IS_OPTION(coap_pkt, COAP_OPTION_TOKEN)) {
+               return 0;
+       }
 
-  *token = coap_pkt->token;
-  return coap_pkt->token_len;
+       *token = coap_pkt->token;
+       return coap_pkt->token_len;
 }
 
-int
-coap_set_header_token(void *packet, const uint8_t *token, size_t token_len)
+int coap_set_header_token(void *packet, const uint8_t *token, size_t token_len)
 {
-  coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
+       coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
 
-  coap_pkt->token_len = (uint8_t)(MIN(COAP_TOKEN_LEN, token_len));
-  memcpy(coap_pkt->token, token, coap_pkt->token_len);
+       coap_pkt->token_len = (uint8_t)(MIN(COAP_TOKEN_LEN, token_len));
+       memcpy(coap_pkt->token, token, coap_pkt->token_len);
 
-  SET_OPTION(coap_pkt, COAP_OPTION_TOKEN);
-  return coap_pkt->token_len;
+       SET_OPTION(coap_pkt, COAP_OPTION_TOKEN);
+       return coap_pkt->token_len;
 }
+
 /*-----------------------------------------------------------------------------------*/
-int
-coap_get_header_proxy_uri(void *packet, const char **uri)
+int coap_get_header_proxy_uri(void *packet, const char **uri)
 {
-  coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
+       coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
 
-  if (!IS_OPTION(coap_pkt, COAP_OPTION_PROXY_URI)) return 0;
+       if (!IS_OPTION(coap_pkt, COAP_OPTION_PROXY_URI)) {
+               return 0;
+       }
 
-  *uri = (const char *)coap_pkt->proxy_uri;
-  return coap_pkt->proxy_uri_len;
+       *uri = (const char *)coap_pkt->proxy_uri;
+       return coap_pkt->proxy_uri_len;
 }
 
-int
-coap_set_header_proxy_uri(void *packet, const char *uri)
+int coap_set_header_proxy_uri(void *packet, const char *uri)
 {
-  coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
+       coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
 
-  coap_pkt->proxy_uri = (uint8_t *)uri;
-  coap_pkt->proxy_uri_len = strlen(uri);
+       coap_pkt->proxy_uri = (uint8_t *) uri;
+       coap_pkt->proxy_uri_len = strlen(uri);
 
-  SET_OPTION(coap_pkt, COAP_OPTION_PROXY_URI);
-  return coap_pkt->proxy_uri_len;
+       SET_OPTION(coap_pkt, COAP_OPTION_PROXY_URI);
+       return coap_pkt->proxy_uri_len;
 }
+
 /*-----------------------------------------------------------------------------------*/
-int
-coap_get_header_uri_host(void *packet, const char **host)
+int coap_get_header_uri_host(void *packet, const char **host)
 {
-  coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
+       coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
 
-  if (!IS_OPTION(coap_pkt, COAP_OPTION_URI_HOST)) return 0;
+       if (!IS_OPTION(coap_pkt, COAP_OPTION_URI_HOST)) {
+               return 0;
+       }
 
-  *host = (char *)coap_pkt->uri_host;
-  return coap_pkt->uri_host_len;
+       *host = (char *)coap_pkt->uri_host;
+       return coap_pkt->uri_host_len;
 }
 
-int
-coap_set_header_uri_host(void *packet, const char *host)
+int coap_set_header_uri_host(void *packet, const char *host)
 {
-  coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
+       coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
 
-  coap_pkt->uri_host = (uint8_t *)host;
-  coap_pkt->uri_host_len = strlen(host);
+       coap_pkt->uri_host = (uint8_t *) host;
+       coap_pkt->uri_host_len = strlen(host);
 
-  SET_OPTION(coap_pkt, COAP_OPTION_URI_HOST);
-  return coap_pkt->uri_host_len;
+       SET_OPTION(coap_pkt, COAP_OPTION_URI_HOST);
+       return coap_pkt->uri_host_len;
 }
+
 /*-----------------------------------------------------------------------------------*/
-int
-coap_get_header_uri_path(void *packet, const char **path)
+int coap_get_header_uri_path(void *packet, const char **path)
 {
-  coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
+       coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
 
-  if (!IS_OPTION(coap_pkt, COAP_OPTION_URI_PATH)) return 0;
+       if (!IS_OPTION(coap_pkt, COAP_OPTION_URI_PATH)) {
+               return 0;
+       }
 
-  *path = NULL; //coap_pkt->uri_path;
-  return 0; //coap_pkt->uri_path_len;
+       *path = NULL;                           //coap_pkt->uri_path;
+       return 0;                                       //coap_pkt->uri_path_len;
 }
 
-int
-coap_set_header_uri_path(void *packet, const char *path)
+int coap_set_header_uri_path(void *packet, const char *path)
 {
-  coap_packet_t *coap_pkt = (coap_packet_t *) packet;
-  int length = 0;
+       coap_packet_t *coap_pkt = (coap_packet_t *) packet;
+       int length = 0;
 
-  free_multi_option(coap_pkt->uri_path);
-  coap_pkt->uri_path = NULL;
+       free_multi_option(coap_pkt->uri_path);
+       coap_pkt->uri_path = NULL;
 
-  if (path[0]=='/') ++path;
+       if (path[0] == '/') {
+               ++path;
+       }
 
-  do
-  {
-      int i = 0;
+       do {
+               int i = 0;
 
-      while (path[i] != 0 && path[i] != '/') i++;
-      coap_add_multi_option(&(coap_pkt->uri_path), (uint8_t *)path, i, 0);
+               while (path[i] != 0 && path[i] != '/') {
+                       i++;
+               }
+               coap_add_multi_option(&(coap_pkt->uri_path), (uint8_t *) path, i, 0);
 
-      if (path[i] == '/') i++;
-      path += i;
-      length += i;
-  } while (path[0] != 0);
+               if (path[i] == '/') {
+                       i++;
+               }
+               path += i;
+               length += i;
+       } while (path[0] != 0);
 
-  SET_OPTION(coap_pkt, COAP_OPTION_URI_PATH);
-  return length;
+       SET_OPTION(coap_pkt, COAP_OPTION_URI_PATH);
+       return length;
 }
 
-int
-coap_set_header_uri_path_segment(void *packet, const char *segment)
+int coap_set_header_uri_path_segment(void *packet, const char *segment)
 {
-  coap_packet_t *coap_pkt = (coap_packet_t *) packet;
-  int length;
-
-  if (segment == NULL || segment[0] == 0)
-  {
-      coap_add_multi_option(&(coap_pkt->uri_path), NULL, 0, 1);
-      length = 0;
-  }
-  else
-  {
-      length = strlen(segment);
-      coap_add_multi_option(&(coap_pkt->uri_path), (uint8_t *)segment, length, 0);
-  }
-
-  SET_OPTION(coap_pkt, COAP_OPTION_URI_PATH);
-  return length;
+       coap_packet_t *coap_pkt = (coap_packet_t *) packet;
+       int length;
+
+       if (segment == NULL || segment[0] == 0) {
+               coap_add_multi_option(&(coap_pkt->uri_path), NULL, 0, 1);
+               length = 0;
+       } else {
+               length = strlen(segment);
+               coap_add_multi_option(&(coap_pkt->uri_path), (uint8_t *) segment, length, 0);
+       }
+
+       SET_OPTION(coap_pkt, COAP_OPTION_URI_PATH);
+       return length;
 }
+
 /*-----------------------------------------------------------------------------------*/
-int
-coap_get_header_uri_query(void *packet, const char **query)
+int coap_get_header_uri_query(void *packet, const char **query)
 {
-  coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
+       coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
 
-  if (!IS_OPTION(coap_pkt, COAP_OPTION_URI_QUERY)) return 0;
+       if (!IS_OPTION(coap_pkt, COAP_OPTION_URI_QUERY)) {
+               return 0;
+       }
 
-  *query = NULL; //coap_pkt->uri_query;
-  return 0; //coap_pkt->uri_query_len;
+       *query = NULL;                          //coap_pkt->uri_query;
+       return 0;                                       //coap_pkt->uri_query_len;
 }
 
-int
-coap_set_header_uri_query(void *packet, const char *query)
+int coap_set_header_uri_query(void *packet, const char *query)
 {
-    int length = 0;
-    coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
+       int length = 0;
+       coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
 
-    free_multi_option(coap_pkt->uri_query);
-    coap_pkt->uri_query = NULL;
+       free_multi_option(coap_pkt->uri_query);
+       coap_pkt->uri_query = NULL;
 
-    if (query[0]=='?') ++query;
+       if (query[0] == '?') {
+               ++query;
+       }
 
-    do
-    {
-        int i = 0;
+       do {
+               int i = 0;
 
-        while (query[i] != 0 && query[i] != '&') i++;
-        coap_add_multi_option(&(coap_pkt->uri_query), (uint8_t *)query, i, 0);
+               while (query[i] != 0 && query[i] != '&') {
+                       i++;
+               }
+               coap_add_multi_option(&(coap_pkt->uri_query), (uint8_t *) query, i, 0);
 
-        if (query[i] == '&') i++;
-        query += i;
-        length += i;
-    } while (query[0] != 0);
+               if (query[i] == '&') {
+                       i++;
+               }
+               query += i;
+               length += i;
+       } while (query[0] != 0);
+
+       SET_OPTION(coap_pkt, COAP_OPTION_URI_QUERY);
+       return length;
+}
 
-    SET_OPTION(coap_pkt, COAP_OPTION_URI_QUERY);
-    return length;
- }
 /*-----------------------------------------------------------------------------------*/
-int
-coap_get_header_location_path(void *packet, const char **path)
+int coap_get_header_location_path(void *packet, const char **path)
 {
-  coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
+       coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
 
-  if (!IS_OPTION(coap_pkt, COAP_OPTION_LOCATION_PATH)) return 0;
+       if (!IS_OPTION(coap_pkt, COAP_OPTION_LOCATION_PATH)) {
+               return 0;
+       }
 
-  *path = NULL; //coap_pkt->location_path;
-  return 0; //coap_pkt->location_path_len;
+       *path = NULL;                           //coap_pkt->location_path;
+       return 0;                                       //coap_pkt->location_path_len;
 }
 
-int
-coap_set_header_location_path(void *packet, const char *path)
+int coap_set_header_location_path(void *packet, const char *path)
 {
-    coap_packet_t *coap_pkt = (coap_packet_t *) packet;
-    int length = 0;
+       coap_packet_t *coap_pkt = (coap_packet_t *) packet;
+       int length = 0;
 
-    free_multi_option(coap_pkt->location_path);
-    coap_pkt->location_path = NULL;
+       free_multi_option(coap_pkt->location_path);
+       coap_pkt->location_path = NULL;
 
-    if (path[0]=='/') ++path;
+       if (path[0] == '/') {
+               ++path;
+       }
 
-    do
-    {
-        int i = 0;
+       do {
+               int i = 0;
 
-        while (path[i] != 0 && path[i] != '/') i++;
-        coap_add_multi_option(&(coap_pkt->location_path), (uint8_t *)path, i, 0);
+               while (path[i] != 0 && path[i] != '/') {
+                       i++;
+               }
+               coap_add_multi_option(&(coap_pkt->location_path), (uint8_t *) path, i, 0);
 
-        if (path[i] == '/') i++;
-        path += i;
-        length += i;
-    } while (path[0] != 0);
+               if (path[i] == '/') {
+                       i++;
+               }
+               path += i;
+               length += i;
+       } while (path[0] != 0);
 
-    SET_OPTION(coap_pkt, COAP_OPTION_LOCATION_PATH);
-    return length;
+       SET_OPTION(coap_pkt, COAP_OPTION_LOCATION_PATH);
+       return length;
 }
+
 /*-----------------------------------------------------------------------------------*/
-int
-coap_get_header_location_query(void *packet, const char **query)
+int coap_get_header_location_query(void *packet, const char **query)
 {
-  coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
+       coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
 
-  if (!IS_OPTION(coap_pkt, COAP_OPTION_LOCATION_QUERY)) return 0;
+       if (!IS_OPTION(coap_pkt, COAP_OPTION_LOCATION_QUERY)) {
+               return 0;
+       }
 
-  *query = (const char*)coap_pkt->location_query;
-  return coap_pkt->location_query_len;
+       *query = (const char *)coap_pkt->location_query;
+       return coap_pkt->location_query_len;
 }
 
-int
-coap_set_header_location_query(void *packet, char *query)
+int coap_set_header_location_query(void *packet, char *query)
 {
-  coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
+       coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
 
-  while (query[0]=='?') ++query;
+       while (query[0] == '?') {
+               ++query;
+       }
 
-  coap_pkt->location_query = (uint8_t *)query;
-  coap_pkt->location_query_len = strlen(query);
+       coap_pkt->location_query = (uint8_t *) query;
+       coap_pkt->location_query_len = strlen(query);
 
-  SET_OPTION(coap_pkt, COAP_OPTION_LOCATION_QUERY);
-  return coap_pkt->location_query_len;
+       SET_OPTION(coap_pkt, COAP_OPTION_LOCATION_QUERY);
+       return coap_pkt->location_query_len;
 }
+
 /*-----------------------------------------------------------------------------------*/
-int
-coap_get_header_observe(void *packet, uint32_t *observe)
+int coap_get_header_observe(void *packet, uint32_t *observe)
 {
-  coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
+       coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
 
-  if (!IS_OPTION(coap_pkt, COAP_OPTION_OBSERVE)) return 0;
+       if (!IS_OPTION(coap_pkt, COAP_OPTION_OBSERVE)) {
+               return 0;
+       }
 
-  *observe = coap_pkt->observe;
-  return 1;
+       *observe = coap_pkt->observe;
+       return 1;
 }
 
-int
-coap_set_header_observe(void *packet, uint32_t observe)
+int coap_set_header_observe(void *packet, uint32_t observe)
 {
-  coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
+       coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
 
-  coap_pkt->observe = 0x00FFFFFF & observe;
-  SET_OPTION(coap_pkt, COAP_OPTION_OBSERVE);
-  return 1;
+       coap_pkt->observe = 0x00FFFFFF & observe;
+       SET_OPTION(coap_pkt, COAP_OPTION_OBSERVE);
+       return 1;
 }
+
 /*-----------------------------------------------------------------------------------*/
-int
-coap_get_header_block2(void *packet, uint32_t *num, uint8_t *more, uint16_t *size, uint32_t *offset)
+int coap_get_header_block2(void *packet, uint32_t *num, uint8_t *more, uint16_t *size, uint32_t *offset)
 {
-  coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
-
-  if (!IS_OPTION(coap_pkt, COAP_OPTION_BLOCK2)) return 0;
-
-  /* pointers may be NULL to get only specific block parameters */
-  if (num!=NULL) *num = coap_pkt->block2_num;
-  if (more!=NULL) *more = coap_pkt->block2_more;
-  if (size!=NULL) *size = coap_pkt->block2_size;
-  if (offset!=NULL) *offset = coap_pkt->block2_offset;
-
-  return 1;
+       coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
+
+       if (!IS_OPTION(coap_pkt, COAP_OPTION_BLOCK2)) {
+               return 0;
+       }
+
+       /* pointers may be NULL to get only specific block parameters */
+       if (num != NULL) {
+               *num = coap_pkt->block2_num;
+       }
+       if (more != NULL) {
+               *more = coap_pkt->block2_more;
+       }
+       if (size != NULL) {
+               *size = coap_pkt->block2_size;
+       }
+       if (offset != NULL) {
+               *offset = coap_pkt->block2_offset;
+       }
+
+       return 1;
 }
 
-int
-coap_set_header_block2(void *packet, uint32_t num, uint8_t more, uint16_t size)
+int coap_set_header_block2(void *packet, uint32_t num, uint8_t more, uint16_t size)
 {
-  coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
-
-  if (size<16) return 0;
-  if (size>2048) return 0;
-  if (num>0x0FFFFF) return 0;
-
-  coap_pkt->block2_num = num;
-  coap_pkt->block2_more = more ? 1 : 0;
-  coap_pkt->block2_size = size;
-
-  SET_OPTION(coap_pkt, COAP_OPTION_BLOCK2);
-  return 1;
+       coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
+
+       if (size < 16) {
+               return 0;
+       }
+       if (size > 2048) {
+               return 0;
+       }
+       if (num > 0x0FFFFF) {
+               return 0;
+       }
+
+       coap_pkt->block2_num = num;
+       coap_pkt->block2_more = more ? 1 : 0;
+       coap_pkt->block2_size = size;
+
+       SET_OPTION(coap_pkt, COAP_OPTION_BLOCK2);
+       return 1;
 }
+
 /*-----------------------------------------------------------------------------------*/
-int
-coap_get_header_block1(void *packet, uint32_t *num, uint8_t *more, uint16_t *size, uint32_t *offset)
+int coap_get_header_block1(void *packet, uint32_t *num, uint8_t *more, uint16_t *size, uint32_t *offset)
 {
-  coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
-
-  if (!IS_OPTION(coap_pkt, COAP_OPTION_BLOCK1)) return 0;
-
-  /* pointers may be NULL to get only specific block parameters */
-  if (num!=NULL) *num = coap_pkt->block1_num;
-  if (more!=NULL) *more = coap_pkt->block1_more;
-  if (size!=NULL) *size = coap_pkt->block1_size;
-  if (offset!=NULL) *offset = coap_pkt->block1_offset;
-
-  return 1;
+       coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
+
+       if (!IS_OPTION(coap_pkt, COAP_OPTION_BLOCK1)) {
+               return 0;
+       }
+
+       /* pointers may be NULL to get only specific block parameters */
+       if (num != NULL) {
+               *num = coap_pkt->block1_num;
+       }
+       if (more != NULL) {
+               *more = coap_pkt->block1_more;
+       }
+       if (size != NULL) {
+               *size = coap_pkt->block1_size;
+       }
+       if (offset != NULL) {
+               *offset = coap_pkt->block1_offset;
+       }
+
+       return 1;
 }
 
-int
-coap_set_header_block1(void *packet, uint32_t num, uint8_t more, uint16_t size)
+int coap_set_header_block1(void *packet, uint32_t num, uint8_t more, uint16_t size)
 {
-  coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
-
-  if (size<16) return 0;
-  if (size>2048) return 0;
-  if (num>0x0FFFFF) return 0;
-
-  coap_pkt->block1_num = num;
-  coap_pkt->block1_more = more;
-  coap_pkt->block1_size = size;
-
-  SET_OPTION(coap_pkt, COAP_OPTION_BLOCK1);
-  return 1;
+       coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
+
+       if (size < 16) {
+               return 0;
+       }
+       if (size > 2048) {
+               return 0;
+       }
+       if (num > 0x0FFFFF) {
+               return 0;
+       }
+
+       coap_pkt->block1_num = num;
+       coap_pkt->block1_more = more;
+       coap_pkt->block1_size = size;
+
+       SET_OPTION(coap_pkt, COAP_OPTION_BLOCK1);
+       return 1;
 }
+
 /*-----------------------------------------------------------------------------------*/
-int
-coap_get_header_size(void *packet, uint32_t *size)
+int coap_get_header_size(void *packet, uint32_t *size)
 {
-  coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
+       coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
+
+       if (!IS_OPTION(coap_pkt, COAP_OPTION_SIZE)) {
+               return 0;
+       }
 
-  if (!IS_OPTION(coap_pkt, COAP_OPTION_SIZE)) return 0;
-  
-  *size = coap_pkt->size;
-  return 1;
+       *size = coap_pkt->size;
+       return 1;
 }
 
-int
-coap_set_header_size(void *packet, uint32_t size)
+int coap_set_header_size(void *packet, uint32_t size)
 {
-  coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
+       coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
 
-  coap_pkt->size = size;
-  SET_OPTION(coap_pkt, COAP_OPTION_SIZE);
-  return 1;
+       coap_pkt->size = size;
+       SET_OPTION(coap_pkt, COAP_OPTION_SIZE);
+       return 1;
 }
+
 /*-----------------------------------------------------------------------------------*/
 /*- PAYLOAD -------------------------------------------------------------------------*/
 /*-----------------------------------------------------------------------------------*/
-int
-coap_get_payload(void *packet, const uint8_t **payload)
+int coap_get_payload(void *packet, const uint8_t **payload)
 {
-  coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
-
-  if (coap_pkt->payload) {
-    *payload = coap_pkt->payload;
-    return coap_pkt->payload_len;
-  } else {
-    *payload = NULL;
-    return 0;
-  }
+       coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
+
+       if (coap_pkt->payload) {
+               *payload = coap_pkt->payload;
+               return coap_pkt->payload_len;
+       } else {
+               *payload = NULL;
+               return 0;
+       }
 }
 
-int
-coap_set_payload(void *packet, const void *payload, size_t length)
+int coap_set_payload(void *packet, const void *payload, size_t length)
 {
-  coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
+       coap_packet_t *const coap_pkt = (coap_packet_t *) packet;
 
-  coap_pkt->payload = (uint8_t *) payload;
-  coap_pkt->payload_len = (uint16_t)(length);
+       coap_pkt->payload = (uint8_t *) payload;
+       coap_pkt->payload_len = (uint16_t)(length);
 
-  return coap_pkt->payload_len;
+       return coap_pkt->payload_len;
 }
+
 /*-----------------------------------------------------------------------------------*/
index 561702f..6eb6371 100644 (file)
  *    David Navarro, Intel Corporation - Adapt to usage in liblwm2m
  */
 
-
 #ifndef COAP_13_H_
 #define COAP_13_H_
 
 #include <stdint.h>
-#include <stddef.h> /* for size_t */
+#include <stddef.h>                            /* for size_t */
 
 /*
  * The maximum buffer size that is provided for resource responses and must be respected due to the limited IP buffer.
 #define COAP_ACK_RANDOM_FACTOR               1.5
 
 #define COAP_MAX_TRANSMIT_WAIT               ((COAP_RESPONSE_TIMEOUT * ( (1 << (COAP_MAX_RETRANSMIT + 1) ) - 1) * COAP_ACK_RANDOM_FACTOR))
-#define COAP_TCP_SHIM_LEN                    4 /* 32bit Length shim header for TCP */
-#define COAP_HEADER_LEN                      4 /* | version:0x03 type:0x0C tkl:0xF0 | code | mid:0x00FF | mid:0xFF00 | */
-#define COAP_ETAG_LEN                        8 /* The maximum number of bytes for the ETag */
-#define COAP_TOKEN_LEN                       8 /* The maximum number of bytes for the Token */
-#define COAP_OPTIONS_MARKER_LEN              1 /* Length of marker (0xFF) between options and payload */
+#define COAP_TCP_SHIM_LEN                    4 /* 32bit Length shim header for TCP */
+#define COAP_HEADER_LEN                      4 /* | version:0x03 type:0x0C tkl:0xF0 | code | mid:0x00FF | mid:0xFF00 | */
+#define COAP_ETAG_LEN                        8 /* The maximum number of bytes for the ETag */
+#define COAP_TOKEN_LEN                       8 /* The maximum number of bytes for the Token */
+#define COAP_OPTIONS_MARKER_LEN              1 /* Length of marker (0xFF) between options and payload */
 
-#define COAP_MAX_ACCEPT_NUM                  2 /* The maximum number of accept preferences to parse/store */
+#define COAP_MAX_ACCEPT_NUM                  2 /* The maximum number of accept preferences to parse/store */
 
 #define COAP_MAX_OPTION_HEADER_LEN           5
 
@@ -84,8 +83,8 @@
  */
 #ifndef COAP_MAX_HEADER_SIZE
 /*                            Hdr CoT Age  Tag              Obs  Tok               Blo strings */
-#define COAP_MAX_HEADER_SIZE  (COAP_HEADER_LEN + 3 + 5 + 1+COAP_ETAG_LEN + 3 + 1+COAP_TOKEN_LEN + 4 + 30) /* 70 */
-#endif /* COAP_MAX_HEADER_SIZE */
+#define COAP_MAX_HEADER_SIZE  (COAP_HEADER_LEN + 3 + 5 + 1+COAP_ETAG_LEN + 3 + 1+COAP_TOKEN_LEN + 4 + 30)      /* 70 */
+#endif                                                 /* COAP_MAX_HEADER_SIZE */
 
 #define COAP_MAX_PACKET_SIZE  (COAP_MAX_HEADER_SIZE + REST_MAX_CHUNK_SIZE)
 /*                                        0/14          48 for IPv6 (28 for IPv4) */
@@ -98,175 +97,174 @@ enum { OPTION_MAP_SIZE = sizeof(uint8_t) * 8 };
 
 #ifndef MIN
 #define MIN(a, b) ((a) < (b)? (a) : (b))
-#endif /* MIN */
+#endif                                                 /* MIN */
 
 /* CoAP protocols */
 typedef enum {
-  COAP_UDP = 0,
-  COAP_UDP_DTLS,
-  COAP_TCP,
-  COAP_TCP_TLS,
-  COAP_PROTOCOL_MAX
+       COAP_UDP = 0,
+       COAP_UDP_DTLS,
+       COAP_TCP,
+       COAP_TCP_TLS,
+       COAP_PROTOCOL_MAX
 } coap_protocol_t;
 
 /* CoAP message types */
 typedef enum {
-  COAP_TYPE_CON, /* confirmables */
-  COAP_TYPE_NON, /* non-confirmables */
-  COAP_TYPE_ACK, /* acknowledgements */
-  COAP_TYPE_RST  /* reset */
+       COAP_TYPE_CON,                          /* confirmables */
+       COAP_TYPE_NON,                          /* non-confirmables */
+       COAP_TYPE_ACK,                          /* acknowledgements */
+       COAP_TYPE_RST                           /* reset */
 } coap_message_type_t;
 
 /* CoAP request method codes */
 typedef enum {
-  COAP_GET = 1,
-  COAP_POST,
-  COAP_PUT,
-  COAP_DELETE
+       COAP_GET = 1,
+       COAP_POST,
+       COAP_PUT,
+       COAP_DELETE
 } coap_method_t;
 
 /* CoAP response codes */
 typedef enum {
-  NO_ERROR = 0,
-
-  CREATED_2_01 = 65,                    /* CREATED */
-  DELETED_2_02 = 66,                    /* DELETED */
-  VALID_2_03 = 67,                      /* NOT_MODIFIED */
-  CHANGED_2_04 = 68,                    /* CHANGED */
-  CONTENT_2_05 = 69,                    /* OK */
-
-  BAD_REQUEST_4_00 = 128,               /* BAD_REQUEST */
-  UNAUTHORIZED_4_01 = 129,              /* UNAUTHORIZED */
-  BAD_OPTION_4_02 = 130,                /* BAD_OPTION */
-  FORBIDDEN_4_03 = 131,                 /* FORBIDDEN */
-  NOT_FOUND_4_04 = 132,                 /* NOT_FOUND */
-  METHOD_NOT_ALLOWED_4_05 = 133,        /* METHOD_NOT_ALLOWED */
-  NOT_ACCEPTABLE_4_06 = 134,            /* NOT_ACCEPTABLE */
-  PRECONDITION_FAILED_4_12 = 140,       /* BAD_REQUEST */
-  REQUEST_ENTITY_TOO_LARGE_4_13 = 141,  /* REQUEST_ENTITY_TOO_LARGE */
-  UNSUPPORTED_MEDIA_TYPE_4_15 = 143,    /* UNSUPPORTED_MEDIA_TYPE */
-
-  INTERNAL_SERVER_ERROR_5_00 = 160,     /* INTERNAL_SERVER_ERROR */
-  NOT_IMPLEMENTED_5_01 = 161,           /* NOT_IMPLEMENTED */
-  BAD_GATEWAY_5_02 = 162,               /* BAD_GATEWAY */
-  SERVICE_UNAVAILABLE_5_03 = 163,       /* SERVICE_UNAVAILABLE */
-  GATEWAY_TIMEOUT_5_04 = 164,           /* GATEWAY_TIMEOUT */
-  PROXYING_NOT_SUPPORTED_5_05 = 165,    /* PROXYING_NOT_SUPPORTED */
-
-  /* Erbium errors */
-  MEMORY_ALLOCATION_ERROR = 192,
-  PACKET_SERIALIZATION_ERROR,
-
-  /* Erbium hooks */
-  MANUAL_RESPONSE
-
+       NO_ERROR = 0,
+
+       CREATED_2_01 = 65,                      /* CREATED */
+       DELETED_2_02 = 66,                      /* DELETED */
+       VALID_2_03 = 67,                        /* NOT_MODIFIED */
+       CHANGED_2_04 = 68,                      /* CHANGED */
+       CONTENT_2_05 = 69,                      /* OK */
+
+       BAD_REQUEST_4_00 = 128,         /* BAD_REQUEST */
+       UNAUTHORIZED_4_01 = 129,        /* UNAUTHORIZED */
+       BAD_OPTION_4_02 = 130,          /* BAD_OPTION */
+       FORBIDDEN_4_03 = 131,           /* FORBIDDEN */
+       NOT_FOUND_4_04 = 132,           /* NOT_FOUND */
+       METHOD_NOT_ALLOWED_4_05 = 133,  /* METHOD_NOT_ALLOWED */
+       NOT_ACCEPTABLE_4_06 = 134,      /* NOT_ACCEPTABLE */
+       PRECONDITION_FAILED_4_12 = 140, /* BAD_REQUEST */
+       REQUEST_ENTITY_TOO_LARGE_4_13 = 141,    /* REQUEST_ENTITY_TOO_LARGE */
+       UNSUPPORTED_MEDIA_TYPE_4_15 = 143,      /* UNSUPPORTED_MEDIA_TYPE */
+
+       INTERNAL_SERVER_ERROR_5_00 = 160,       /* INTERNAL_SERVER_ERROR */
+       NOT_IMPLEMENTED_5_01 = 161,     /* NOT_IMPLEMENTED */
+       BAD_GATEWAY_5_02 = 162,         /* BAD_GATEWAY */
+       SERVICE_UNAVAILABLE_5_03 = 163, /* SERVICE_UNAVAILABLE */
+       GATEWAY_TIMEOUT_5_04 = 164,     /* GATEWAY_TIMEOUT */
+       PROXYING_NOT_SUPPORTED_5_05 = 165,      /* PROXYING_NOT_SUPPORTED */
+
+       /* Erbium errors */
+       MEMORY_ALLOCATION_ERROR = 192,
+       PACKET_SERIALIZATION_ERROR,
+
+       /* Erbium hooks */
+       MANUAL_RESPONSE
 } coap_status_t;
 
 /* CoAP header options */
 typedef enum {
-  COAP_OPTION_IF_MATCH = 1,       /* 0-8 B */
-  COAP_OPTION_URI_HOST = 3,       /* 1-255 B */
-  COAP_OPTION_ETAG = 4,           /* 1-8 B */
-  COAP_OPTION_IF_NONE_MATCH = 5,  /* 0 B */
-  COAP_OPTION_OBSERVE = 6,        /* 0-3 B */
-  COAP_OPTION_URI_PORT = 7,       /* 0-2 B */
-  COAP_OPTION_LOCATION_PATH = 8,  /* 0-255 B */
-  COAP_OPTION_URI_PATH = 11,      /* 0-255 B */
-  COAP_OPTION_CONTENT_TYPE = 12,  /* 0-2 B */
-  COAP_OPTION_MAX_AGE = 14,       /* 0-4 B */
-  COAP_OPTION_URI_QUERY = 15,     /* 0-270 B */
-  COAP_OPTION_ACCEPT = 17,        /* 0-2 B */
-  COAP_OPTION_TOKEN = 19,         /* 1-8 B */
-  COAP_OPTION_LOCATION_QUERY = 20, /* 1-270 B */
-  COAP_OPTION_BLOCK2 = 23,        /* 1-3 B */
-  COAP_OPTION_BLOCK1 = 27,        /* 1-3 B */
-  COAP_OPTION_SIZE = 28,          /* 0-4 B */
-  COAP_OPTION_PROXY_URI = 35,     /* 1-270 B */
-  OPTION_MAX_VALUE = 0xFFFF
+       COAP_OPTION_IF_MATCH = 1,       /* 0-8 B */
+       COAP_OPTION_URI_HOST = 3,       /* 1-255 B */
+       COAP_OPTION_ETAG = 4,           /* 1-8 B */
+       COAP_OPTION_IF_NONE_MATCH = 5,  /* 0 B */
+       COAP_OPTION_OBSERVE = 6,        /* 0-3 B */
+       COAP_OPTION_URI_PORT = 7,       /* 0-2 B */
+       COAP_OPTION_LOCATION_PATH = 8,  /* 0-255 B */
+       COAP_OPTION_URI_PATH = 11,      /* 0-255 B */
+       COAP_OPTION_CONTENT_TYPE = 12,  /* 0-2 B */
+       COAP_OPTION_MAX_AGE = 14,       /* 0-4 B */
+       COAP_OPTION_URI_QUERY = 15,     /* 0-270 B */
+       COAP_OPTION_ACCEPT = 17,        /* 0-2 B */
+       COAP_OPTION_TOKEN = 19,         /* 1-8 B */
+       COAP_OPTION_LOCATION_QUERY = 20,        /* 1-270 B */
+       COAP_OPTION_BLOCK2 = 23,        /* 1-3 B */
+       COAP_OPTION_BLOCK1 = 27,        /* 1-3 B */
+       COAP_OPTION_SIZE = 28,          /* 0-4 B */
+       COAP_OPTION_PROXY_URI = 35,     /* 1-270 B */
+       OPTION_MAX_VALUE = 0xFFFF
 } coap_option_t;
 
 /* CoAP Content-Types */
 typedef enum {
-  TEXT_PLAIN = 0,
-  TEXT_XML = 1, /* Indented types are not in the initial registry. */
-  TEXT_CSV = 2,
-  TEXT_HTML = 3,
-  IMAGE_GIF = 21,
-  IMAGE_JPEG = 22,
-  IMAGE_PNG = 23,
-  IMAGE_TIFF = 24,
-  AUDIO_RAW = 25,
-  VIDEO_RAW = 26,
-  APPLICATION_LINK_FORMAT = 40,
-  APPLICATION_XML = 41,
-  APPLICATION_OCTET_STREAM = 42,
-  APPLICATION_RDF_XML = 43,
-  APPLICATION_SOAP_XML = 44,
-  APPLICATION_ATOM_XML = 45,
-  APPLICATION_XMPP_XML = 46,
-  APPLICATION_EXI = 47,
-  APPLICATION_FASTINFOSET = 48,
-  APPLICATION_SOAP_FASTINFOSET = 49,
-  APPLICATION_JSON = 50,
-  APPLICATION_X_OBIX_BINARY = 51,
-  CONTENT_MAX_VALUE = 0xFFFF
+       TEXT_PLAIN = 0,
+       TEXT_XML = 1,                           /* Indented types are not in the initial registry. */
+       TEXT_CSV = 2,
+       TEXT_HTML = 3,
+       IMAGE_GIF = 21,
+       IMAGE_JPEG = 22,
+       IMAGE_PNG = 23,
+       IMAGE_TIFF = 24,
+       AUDIO_RAW = 25,
+       VIDEO_RAW = 26,
+       APPLICATION_LINK_FORMAT = 40,
+       APPLICATION_XML = 41,
+       APPLICATION_OCTET_STREAM = 42,
+       APPLICATION_RDF_XML = 43,
+       APPLICATION_SOAP_XML = 44,
+       APPLICATION_ATOM_XML = 45,
+       APPLICATION_XMPP_XML = 46,
+       APPLICATION_EXI = 47,
+       APPLICATION_FASTINFOSET = 48,
+       APPLICATION_SOAP_FASTINFOSET = 49,
+       APPLICATION_JSON = 50,
+       APPLICATION_X_OBIX_BINARY = 51,
+       CONTENT_MAX_VALUE = 0xFFFF
 } coap_content_type_t;
 
 typedef struct _multi_option_t {
-  struct _multi_option_t *next;
-  uint8_t is_static;
-  uint8_t len;
-  uint8_t *data;
+       struct _multi_option_t *next;
+       uint8_t is_static;
+       uint8_t len;
+       uint8_t *data;
 } multi_option_t;
 
 /* Parsed message struct */
 typedef struct {
-  uint8_t *buffer; /* pointer to CoAP header / incoming packet buffer / memory to serialize packet */
-
-  uint8_t version;
-  coap_message_type_t type;
-  uint8_t code;
-  uint16_t mid;
-
-  uint8_t options[COAP_OPTION_PROXY_URI / OPTION_MAP_SIZE + 1]; /* Bitmap to check if option is set */
-  uint16_t options_len;
-
-  coap_content_type_t content_type; /* Parse options once and store; allows setting options in random order  */
-  uint32_t max_age;
-  size_t proxy_uri_len;
-  const uint8_t *proxy_uri;
-  uint8_t etag_len;
-  uint8_t etag[COAP_ETAG_LEN];
-  size_t uri_host_len;
-  const uint8_t *uri_host;
-  multi_option_t *location_path;
-  uint16_t uri_port;
-  size_t location_query_len;
-  uint8_t *location_query;
-  multi_option_t *uri_path;
-  uint32_t observe;
-  uint8_t token_len;
-  uint8_t token[COAP_TOKEN_LEN];
-  uint8_t accept_num;
-  uint16_t accept[COAP_MAX_ACCEPT_NUM];
-  uint8_t if_match_len;
-  uint8_t if_match[COAP_ETAG_LEN];
-  uint32_t block2_num;
-  uint8_t block2_more;
-  uint16_t block2_size;
-  uint32_t block2_offset;
-  uint32_t block1_num;
-  uint8_t block1_more;
-  uint16_t block1_size;
-  uint32_t block1_offset;
-  uint32_t size;
-  multi_option_t *uri_query;
-  uint8_t if_none_match;
-
-  uint16_t payload_len;
-  uint8_t *payload;
-
-  coap_protocol_t protocol;
+       uint8_t *buffer;                        /* pointer to CoAP header / incoming packet buffer / memory to serialize packet */
+
+       uint8_t version;
+       coap_message_type_t type;
+       uint8_t code;
+       uint16_t mid;
+
+       uint8_t options[COAP_OPTION_PROXY_URI / OPTION_MAP_SIZE + 1];   /* Bitmap to check if option is set */
+       uint16_t options_len;
+
+       coap_content_type_t content_type;       /* Parse options once and store; allows setting options in random order  */
+       uint32_t max_age;
+       size_t proxy_uri_len;
+       const uint8_t *proxy_uri;
+       uint8_t etag_len;
+       uint8_t etag[COAP_ETAG_LEN];
+       size_t uri_host_len;
+       const uint8_t *uri_host;
+       multi_option_t *location_path;
+       uint16_t uri_port;
+       size_t location_query_len;
+       uint8_t *location_query;
+       multi_option_t *uri_path;
+       uint32_t observe;
+       uint8_t token_len;
+       uint8_t token[COAP_TOKEN_LEN];
+       uint8_t accept_num;
+       uint16_t accept[COAP_MAX_ACCEPT_NUM];
+       uint8_t if_match_len;
+       uint8_t if_match[COAP_ETAG_LEN];
+       uint32_t block2_num;
+       uint8_t block2_more;
+       uint16_t block2_size;
+       uint32_t block2_offset;
+       uint32_t block1_num;
+       uint8_t block1_more;
+       uint16_t block1_size;
+       uint32_t block1_offset;
+       uint32_t size;
+       multi_option_t *uri_query;
+       uint8_t if_none_match;
+
+       uint16_t payload_len;
+       uint8_t *payload;
+
+       coap_protocol_t protocol;
 
 } coap_packet_t;
 
@@ -334,10 +332,13 @@ uint16_t coap_get_mid(void);
 void coap_init_message(void *packet, coap_protocol_t protocol, coap_message_type_t type, uint8_t code, uint16_t mid);
 size_t coap_serialize_get_size(void *packet);
 size_t coap_serialize_message(void *packet, uint8_t *buffer);
+/* To support TCP header */
+size_t coap_serialize_get_size_tcp(void *packet);
+size_t coap_serialize_message_tcp(void *packet, uint8_t *buffer);
 coap_status_t coap_parse_message(void *request, coap_protocol_t protocol, uint8_t *data, uint16_t data_len);
 void coap_free_header(void *packet);
 
-char * coap_get_multi_option_as_string(multi_option_t * option);
+char *coap_get_multi_option_as_string(multi_option_t *option);
 void coap_add_multi_option(multi_option_t **dst, uint8_t *option, size_t option_len, uint8_t is_static);
 void free_multi_option(multi_option_t *dst);
 
@@ -369,23 +370,23 @@ int coap_set_header_if_none_match(void *packet);
 int coap_get_header_token(void *packet, const uint8_t **token);
 int coap_set_header_token(void *packet, const uint8_t *token, size_t token_len);
 
-int coap_get_header_proxy_uri(void *packet, const char **uri); /* In-place string might not be 0-terminated. */
+int coap_get_header_proxy_uri(void *packet, const char **uri); /* In-place string might not be 0-terminated. */
 int coap_set_header_proxy_uri(void *packet, const char *uri);
 
-int coap_get_header_uri_host(void *packet, const char **host); /* In-place string might not be 0-terminated. */
+int coap_get_header_uri_host(void *packet, const char **host); /* In-place string might not be 0-terminated. */
 int coap_set_header_uri_host(void *packet, const char *host);
 
-int coap_get_header_uri_path(void *packet, const char **path); /* In-place string might not be 0-terminated. */
+int coap_get_header_uri_path(void *packet, const char **path); /* In-place string might not be 0-terminated. */
 int coap_set_header_uri_path(void *packet, const char *path);
 int coap_set_header_uri_path_segment(void *packet, const char *path);
 
-int coap_get_header_uri_query(void *packet, const char **query); /* In-place string might not be 0-terminated. */
+int coap_get_header_uri_query(void *packet, const char **query);       /* In-place string might not be 0-terminated. */
 int coap_set_header_uri_query(void *packet, const char *query);
 
-int coap_get_header_location_path(void *packet, const char **path); /* In-place string might not be 0-terminated. */
-int coap_set_header_location_path(void *packet, const char *path); /* Also splits optional query into Location-Query option. */
+int coap_get_header_location_path(void *packet, const char **path);    /* In-place string might not be 0-terminated. */
+int coap_set_header_location_path(void *packet, const char *path);     /* Also splits optional query into Location-Query option. */
 
-int coap_get_header_location_query(void *packet, const char **query); /* In-place string might not be 0-terminated. */
+int coap_get_header_location_query(void *packet, const char **query);  /* In-place string might not be 0-terminated. */
 int coap_set_header_location_query(void *packet, char *query);
 
 int coap_get_header_observe(void *packet, uint32_t *observe);
@@ -403,4 +404,4 @@ int coap_set_header_size(void *packet, uint32_t size);
 int coap_get_payload(void *packet, const uint8_t **payload);
 int coap_set_payload(void *packet, const void *payload, size_t length);
 
-#endif /* COAP_13_H_ */
+#endif                                                 /* COAP_13_H_ */
index 84bc853..93941a7 100644 (file)
@@ -430,18 +430,45 @@ coap_status_t message_send(lwm2m_context_t * contextP,
     coap_status_t result = COAP_500_INTERNAL_SERVER_ERROR;
     uint8_t * pktBuffer;
     size_t pktBufferLen = 0;
-    size_t allocLen;
+    size_t allocLen = 0;
+
+       coap_protocol_t proto = contextP->protocol;
 
     LOG("Entering");
-    allocLen = coap_serialize_get_size(message);
-    LOG_ARG("Size to allocate: %d", allocLen);
+
+       switch (proto) {
+       case COAP_UDP:
+       case COAP_UDP_DTLS:
+               allocLen = coap_serialize_get_size(message);
+               break;
+       case COAP_TCP:
+       case COAP_TCP_TLS:
+               allocLen = coap_serialize_get_size_tcp(message);
+               break;
+       default:
+               break;
+       }
+       LOG_ARG("Size to allocate: %d (protocol %d)", allocLen, proto);
+
     if (allocLen == 0) return COAP_500_INTERNAL_SERVER_ERROR;
 
     pktBuffer = (uint8_t *)lwm2m_malloc(allocLen);
     if (pktBuffer != NULL)
     {
-        pktBufferLen = coap_serialize_message(message, pktBuffer);
-        LOG_ARG("coap_serialize_message() returned %d", pktBufferLen);
+               switch (proto) {
+               case COAP_UDP:
+               case COAP_UDP_DTLS:
+                       pktBufferLen = coap_serialize_message(message, pktBuffer);
+                       break;
+               case COAP_TCP:
+               case COAP_TCP_TLS:
+                       pktBufferLen = coap_serialize_message_tcp(message, pktBuffer);
+                       break;
+               default:
+                       break;
+               }
+               LOG_ARG("coap_serialize_message() returned %d (protocol %d)", pktBufferLen, proto);
+
         if (0 != pktBufferLen)
         {
             result = lwm2m_buffer_send(sessionH, pktBuffer, pktBufferLen, contextP->userData, contextP->protocol);
index b615ca8..d379a9e 100644 (file)
@@ -359,16 +359,44 @@ int transaction_send(lwm2m_context_t * contextP,
 {
     bool maxRetriesReached = false;
 
+       coap_protocol_t proto = contextP->protocol;
+
     LOG("Entering");
     if (transacP->buffer == NULL)
     {
-        transacP->buffer_len = coap_serialize_get_size(transacP->message);
+               switch (proto) {
+               case COAP_UDP:
+               case COAP_UDP_DTLS:
+                       transacP->buffer_len = coap_serialize_get_size(transacP->message);
+                       break;
+               case COAP_TCP:
+               case COAP_TCP_TLS:
+                       transacP->buffer_len = coap_serialize_get_size_tcp(transacP->message);
+                       break;
+               default:
+                       break;
+               }
+               LOG_ARG("get transaction message size %d, (protocol %d)", transacP->buffer_len, proto);
+
         if (transacP->buffer_len == 0) return COAP_500_INTERNAL_SERVER_ERROR;
 
         transacP->buffer = (uint8_t*)lwm2m_malloc(transacP->buffer_len);
         if (transacP->buffer == NULL) return COAP_500_INTERNAL_SERVER_ERROR;
 
-        transacP->buffer_len = coap_serialize_message(transacP->message, transacP->buffer);
+               switch (proto) {
+               case COAP_UDP:\
+               case COAP_UDP_DTLS:
+                       transacP->buffer_len = coap_serialize_message(transacP->message, transacP->buffer);
+                       break;
+               case COAP_TCP:
+               case COAP_TCP_TLS:
+                       transacP->buffer_len = coap_serialize_message_tcp(transacP->message, transacP->buffer);
+                       break;
+               default:
+                       break;
+               }
+               LOG_ARG("transaction buffer len %d, (protocol %d)", transacP->buffer_len, proto);
+
         if (transacP->buffer_len == 0)
         {
             lwm2m_free(transacP->buffer);