Contiki 3.x
coap-common.h
1 /*
2  * coap.h
3  *
4  * Created on: Aug 25, 2010
5  * Author: dogan
6  */
7 
8 #ifndef COAP_COMMON_H_
9 #define COAP_COMMON_H_
10 
11 #include "contiki-net.h"
12 
13 /*COAP method types*/
14 typedef enum {
15  COAP_GET = 1,
16  COAP_POST,
17  COAP_PUT,
18  COAP_DELETE
19 } coap_method_t;
20 
21 typedef enum {
22  MESSAGE_TYPE_CON,
23  MESSAGE_TYPE_NON,
24  MESSAGE_TYPE_ACK,
25  MESSAGE_TYPE_RST
26 } message_type;
27 
28 typedef enum {
29  OK_200 = 80,
30  CREATED_201 = 81,
31  NOT_MODIFIED_304 = 124,
32  BAD_REQUEST_400 = 160,
33  NOT_FOUND_404 = 164,
34  METHOD_NOT_ALLOWED_405 = 165,
35  UNSUPPORTED_MADIA_TYPE_415 = 175,
36  INTERNAL_SERVER_ERROR_500 = 200,
37  BAD_GATEWAY_502 = 202,
38  GATEWAY_TIMEOUT_504 = 204
39 } status_code_t;
40 
41 typedef enum {
42  Option_Type_Content_Type = 1,
43  Option_Type_Max_Age = 2,
44  Option_Type_Etag = 4,
45  Option_Type_Uri_Authority = 5,
46  Option_Type_Location = 6,
47  Option_Type_Uri_Path = 9,
48  Option_Type_Subscription_Lifetime = 10,
49  Option_Type_Token = 11,
50  Option_Type_Block = 13,
51  Option_Type_Uri_Query = 15
52 } option_type;
53 
54 typedef enum {
55  TEXT_PLAIN = 0,
56  TEXT_XML = 1,
57  TEXT_CSV = 2,
58  TEXT_HTML = 3,
59  IMAGE_GIF = 21,
60  IMAGE_JPEG = 22,
61  IMAGE_PNG = 23,
62  IMAGE_TIFF = 24,
63  AUDIO_RAW = 25,
64  VIDEO_RAW = 26,
65  APPLICATION_LINK_FORMAT = 40,
66  APPLICATION_XML = 41,
67  APPLICATION_OCTET_STREAM = 42,
68  APPLICATION_RDF_XML = 43,
69  APPLICATION_SOAP_XML = 44,
70  APPLICATION_ATOM_XML = 45,
71  APPLICATION_XMPP_XML = 46,
72  APPLICATION_EXI = 47,
73  APPLICATION_X_BXML = 48,
74  APPLICATION_FASTINFOSET = 49,
75  APPLICATION_SOAP_FASTINFOSET = 50,
76  APPLICATION_JSON = 51
77 } content_type_t;
78 
79 #define COAP_HEADER_VERSION_MASK 0xC0
80 #define COAP_HEADER_TYPE_MASK 0x30
81 #define COAP_HEADER_OPTION_COUNT_MASK 0x0F
82 #define COAP_HEADER_OPTION_DELTA_MASK 0xF0
83 #define COAP_HEADER_OPTION_SHORT_LENGTH_MASK 0x0F
84 
85 #define COAP_HEADER_VERSION_POSITION 6
86 #define COAP_HEADER_TYPE_POSITION 4
87 #define COAP_HEADER_OPTION_DELTA_POSITION 4
88 
89 #define REQUEST_BUFFER_SIZE 200
90 
91 #define DEFAULT_CONTENT_TYPE 0
92 #define DEFAULT_MAX_AGE 60
93 #define DEFAULT_URI_AUTHORITY ""
94 #define DEFAULT_URI_PATH ""
95 
96 //keep open requests and their xactid
97 
98 struct header_option_t
99 {
100  struct header_option_t* next;
101  uint16_t option;
102  uint16_t len;
103  uint8_t* value;
104 };
105 typedef struct header_option_t header_option_t;
106 
107 struct block_option_t {
108  uint32_t number;
109  uint8_t more;
110  uint8_t size;
111 };
112 typedef struct block_option_t block_option_t;
113 
114 typedef struct
115 {
116  uint8_t ver; //2-bits currently set to 1.
117  uint8_t type; //2-bits Confirmable (0), Non-Confirmable (1), Acknowledgment (2) or Reset (3)
118  uint8_t option_count; //4-bits
119  uint8_t code; //8-bits Method or response code
120  uint16_t tid; //16-bit unsigned integer
121  header_option_t* options;
122  char* url; //put it just as a shortcut or else need to parse options everytime to access it.
123  uint16_t url_len;
124  char* query;
125  uint16_t query_len;
126  uint16_t payload_len;
127  uint8_t* payload;
128  uip_ipaddr_t addr;
129 } coap_packet_t;
130 
131 /*error definitions*/
132 typedef enum
133 {
134  NO_ERROR,
135 
136  /*Memory errors*/
137  MEMORY_ALLOC_ERR,
138  MEMORY_BOUNDARY_EXCEEDED
139 } error_t;
140 
141 int serialize_packet(coap_packet_t* request, uint8_t* buffer);
142 void init_packet(coap_packet_t* packet);
143 
144 #endif /* COAP_COMMON_H_ */