Contiki 3.x
coap-common.c
1 /*
2  * coap-common.c
3  *
4  * Created on: Aug 30, 2010
5  * Author: dogan
6  */
7 
8 #ifdef CONTIKI_TARGET_NETSIM
9  #include <stdio.h>
10  #include <iostream>
11  #include <cstring>
12  #include <cstdlib>
13  #include <unistd.h>
14  #include <errno.h>
15  #include <string.h>
16  #include <sys/types.h>
17  #include <sys/socket.h>
18  #include <netinet/in.h>
19  #include <arpa/inet.h>
20  #include <netdb.h>
21 #else
22  #include "contiki.h"
23  #include "contiki-net.h"
24  #include <string.h>
25 #endif
26 
27 #include "coap-common.h"
28 
29 #define DEBUG 0
30 #if DEBUG
31 #include <stdio.h>
32 #define PRINTF(...) printf(__VA_ARGS__)
33 #define PRINT6ADDR(addr) PRINTF(" %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x ", ((uint8_t *)addr)[0], ((uint8_t *)addr)[1], ((uint8_t *)addr)[2], ((uint8_t *)addr)[3], ((uint8_t *)addr)[4], ((uint8_t *)addr)[5], ((uint8_t *)addr)[6], ((uint8_t *)addr)[7], ((uint8_t *)addr)[8], ((uint8_t *)addr)[9], ((uint8_t *)addr)[10], ((uint8_t *)addr)[11], ((uint8_t *)addr)[12], ((uint8_t *)addr)[13], ((uint8_t *)addr)[14], ((uint8_t *)addr)[15])
34 #define PRINTLLADDR(lladdr) PRINTF(" %02x:%02x:%02x:%02x:%02x:%02x ",(lladdr)->addr[0], (lladdr)->addr[1], (lladdr)->addr[2], (lladdr)->addr[3],(lladdr)->addr[4], (lladdr)->addr[5])
35 #else
36 #define PRINTF(...)
37 #define PRINT6ADDR(addr)
38 #define PRINTLLADDR(addr)
39 #endif
40 
41 void init_packet(coap_packet_t* packet)
42 {
43  packet->ver = 1;
44  packet->type = 0;
45  packet->option_count = 0;
46  packet->code = 0;
47  packet->tid = 0;
48  packet->options = NULL;
49  packet->url = NULL;
50  packet->url_len = 0;
51  packet->query = NULL;
52  packet->query_len = 0;
53  packet->payload = NULL;
54  packet->payload_len = 0;
55 }
56 
57 int serialize_packet(coap_packet_t* packet, uint8_t* buffer)
58 {
59  int index = 0;
60  header_option_t* option = NULL;
61  uint16_t option_delta = 0;
62 
63  buffer[0] = (packet->ver) << COAP_HEADER_VERSION_POSITION;
64  buffer[0] |= (packet->type) << COAP_HEADER_TYPE_POSITION;
65  buffer[0] |= packet->option_count;
66  buffer[1] = packet->code;
67  uint16_t temp = uip_htons(packet->tid);
68  memcpy(
69  (void*)&buffer[2],
70  (void*)(&temp),
71  sizeof(packet->tid));
72 
73  index += 4;
74 
75  PRINTF("serialize option_count %u\n", packet->option_count);
76 
77  /*Options should be sorted beforehand*/
78  for (option = packet->options ; option ; option = option->next){
79  uint16_t delta = option->option - option_delta;
80  if ( !delta ){
81  PRINTF("WARNING: Delta==Zero\n");
82  }
83  buffer[index] = (delta) << COAP_HEADER_OPTION_DELTA_POSITION;
84 
85  PRINTF("option %u len %u option diff %u option_value addr %x option addr %x next option addr %x", option->option, option->len, option->option - option_delta, (unsigned int) option->value, (unsigned int)option, (unsigned int)option->next);
86 
87  int i = 0;
88  for ( ; i < option->len ; i++ ){
89  PRINTF(" (%u)", option->value[i]);
90  }
91  PRINTF("\n");
92 
93  if (option->len < 0xF){
94  buffer[index] |= option->len;
95  index++;
96  } else{
97  buffer[index] |= (0xF); //1111
98  buffer[index + 1] = option->len - (0xF);
99  index += 2;
100  }
101 
102  memcpy((char*)&buffer[index], option->value, option->len);
103  index += option->len;
104  option_delta = option->option;
105  }
106 
107  if(packet->payload){
108  memcpy(&buffer[index], packet->payload, packet->payload_len);
109  index += packet->payload_len;
110  }
111 
112  return index;
113 }
#define NULL
The null pointer.
CCIF uint16_t uip_htons(uint16_t val)
Convert a 16-bit quantity from host byte order to network byte order.
Definition: uip6.c:2298