Can't CoAP
 All Classes Functions Enumerations Pages
cantcoap.h
1 
2 #include <unistd.h>
3 #include <stdint.h>
4 
5 #define INFO(...) printf(__VA_ARGS__); printf("\r\n");
6 #define INFOX(...); printf(__VA_ARGS__);
7 #define ERR(...) printf(__VA_ARGS__); printf("\r\n");
8 
9 #ifdef DEBUG
10  #define DBG(...) fprintf(stderr,"%s:%d ",__FILE__,__LINE__); fprintf(stderr,__VA_ARGS__); fprintf(stderr,"\r\n");
11  #define DBGX(...) fprintf(stderr,__VA_ARGS__);
12  #define DBGLX(...) fprintf(stderr,"%s:%d ",__FILE__,__LINE__); fprintf(stderr,__VA_ARGS__);
13  #define DBG_PDU() printBin();
14 #else
15  #define DBG(...) {};
16  #define DBGX(...) {};
17  #define DBGLX(...) {};
18  #define DBG_PDU() {};
19 #endif
20 
21 #define COAP_HDR_SIZE 4
22 #define COAP_OPTION_HDR_BYTE 1
23 
24 // CoAP PDU format
25 
26 // 0 1 2 3
27 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
28 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
29 // |Ver| T | TKL | Code | Message ID |
30 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
31 // | Token (if any, TKL bytes) ...
32 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
33 // | Options (if any) ...
34 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
35 // |1 1 1 1 1 1 1 1| Payload (if any) ...
36 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
37 
38 class CoapPDU {
39 
40 
41  public:
43  enum Type {
44  COAP_CONFIRMABLE=0x00,
45  COAP_NON_CONFIRMABLE=0x10,
46  COAP_ACKNOWLEDGEMENT=0x20,
47  COAP_RESET=0x30
48  };
49 
50  // CoAP response codes.
51  enum Code {
52  COAP_EMPTY=0x00,
53  COAP_GET,
54  COAP_POST,
55  COAP_PUT,
56  COAP_DELETE,
57  COAP_CREATED=0x41,
58  COAP_DELETED,
59  COAP_VALID,
60  COAP_CHANGED,
61  COAP_CONTENT,
62  COAP_BAD_REQUEST=0x80,
63  COAP_UNAUTHORIZED,
64  COAP_BAD_OPTION,
65  COAP_FORBIDDEN,
66  COAP_NOT_FOUND,
67  COAP_METHOD_NOT_ALLOWED,
68  COAP_NOT_ACCEPTABLE,
69  COAP_PRECONDITION_FAILED=0x8C,
70  COAP_REQUEST_ENTITY_TOO_LARGE=0x8D,
71  COAP_UNSUPPORTED_CONTENT_FORMAT=0x8F,
72  COAP_INTERNAL_SERVER_ERROR=0xA0,
73  COAP_NOT_IMPLEMENTED,
74  COAP_BAD_GATEWAY,
75  COAP_SERVICE_UNAVAILABLE,
76  COAP_GATEWAY_TIMEOUT,
77  COAP_PROXYING_NOT_SUPPORTED
78  };
79 
81  enum Option {
82  COAP_OPTION_IF_MATCH=1,
83  COAP_OPTION_URI_HOST=3,
84  COAP_OPTION_ETAG,
85  COAP_OPTION_IF_NONE_MATCH,
86  COAP_OPTION_OBSERVE,
87  COAP_OPTION_URI_PORT,
88  COAP_OPTION_LOCATION_PATH,
89  COAP_OPTION_URI_PATH=11,
90  COAP_OPTION_CONTENT_FORMAT,
91  COAP_OPTION_MAX_AGE=14,
92  COAP_OPTION_URI_QUERY,
93  COAP_OPTION_ACCEPT=17,
94  COAP_OPTION_LOCATION_QUERY=20,
95  COAP_OPTION_BLOCK2=23,
96  COAP_OPTION_BLOCK1=27,
97  COAP_OPTION_SIZE2,
98  COAP_OPTION_PROXY_URI=35,
99  COAP_OPTION_PROXY_SCHEME=39,
100  COAP_OPTION_SIZE1=60
101  };
102 
105  COAP_CONTENT_FORMAT_TEXT_PLAIN = 0,
106  COAP_CONTENT_FORMAT_APP_LINK = 40,
107  COAP_CONTENT_FORMAT_APP_XML,
108  COAP_CONTENT_FORMAT_APP_OCTET,
109  COAP_CONTENT_FORMAT_APP_EXI = 47,
110  COAP_CONTENT_FORMAT_APP_JSON = 50
111  };
112 
114  struct CoapOption {
115  uint16_t optionDelta;
116  uint16_t optionNumber;
117  uint16_t optionValueLength;
118  int totalLength;
119  uint8_t *optionPointer;
120  uint8_t *optionValuePointer;
121  };
122 
123  // construction and destruction
124  CoapPDU();
125  CoapPDU(uint8_t *pdu, int pduLength);
126  CoapPDU(uint8_t *buffer, int bufferLength, int pduLength);
127  ~CoapPDU();
128  int reset();
129  int validate();
130 
131  // version
132  int setVersion(uint8_t version);
133  uint8_t getVersion();
134 
135  // message type
136  void setType(CoapPDU::Type type);
138 
139  // tokens
140  int setTokenLength(uint8_t tokenLength);
141  int getTokenLength();
142  uint8_t* getTokenPointer();
143  int setToken(uint8_t *token, uint8_t tokenLength);
144 
145  // message code
146  void setCode(CoapPDU::Code code);
147  CoapPDU::Code getCode();
148 
149  // message ID
150  int setMessageID(uint16_t messageID);
151  uint16_t getMessageID();
152 
153  // options
154  int addOption(uint16_t optionNumber, uint16_t optionLength, uint8_t *optionValue);
155  // gets a list of all options
157  int getNumOptions();
158  // shorthand helpers
159  int setURI(char *uri, int urilen);
160  int getURI(char *dst, int dstlen, int *outLen);
161 
162  // content format helper
164 
165  // payload
166  uint8_t* mallocPayload(int bytes);
167  int setPayload(uint8_t *value, int len);
168  uint8_t* getPayloadPointer();
169  int getPayloadLength();
170  uint8_t* getPayloadCopy();
171 
172  // pdu
173  int getPDULength();
174  uint8_t* getPDUPointer();
175  void setPDULength(int len);
176 
177  // debugging
178  static void printBinary(uint8_t b);
179  void print();
180  void printBin();
181  void printHex();
182  void printOptionHuman(uint8_t *option);
183  void printHuman();
184  void printPDUAsCArray();
185 
186  private:
187  // variables
188  uint8_t *_pdu;
189  int _pduLength;
190 
191  int _constructedFromBuffer;
192  int _bufferLength;
193 
194  uint8_t *_payloadPointer;
195  int _payloadLength;
196 
197  int _numOptions;
198  uint16_t _maxAddedOptionNumber;
199 
200  // functions
201  void shiftPDUUp(int shiftOffset, int shiftAmount);
202  void shiftPDUDown(int startLocation, int shiftOffset, int shiftAmount);
203  uint8_t codeToValue(CoapPDU::Code c);
204 
205  // option stuff
206  int findInsertionPosition(uint16_t optionNumber, uint16_t *prevOptionNumber);
207  int computeExtraBytes(uint16_t n);
208  int insertOption(int insertionPosition, uint16_t optionDelta, uint16_t optionValueLength, uint8_t *optionValue);
209  uint16_t getOptionDelta(uint8_t *option);
210  void setOptionDelta(int optionPosition, uint16_t optionDelta);
211  uint16_t getOptionValueLength(uint8_t *option);
212 
213 };
214 
215 /*
216 #define COAP_CODE_EMPTY 0x00
217 
218 // method codes 0.01-0.31
219 #define COAP_CODE_GET 0x01
220 #define COAP_CODE_POST 0x02
221 #define COAP_CODE_PUT 0x03
222 #define COAP_CODE_DELETE 0x04
223 
224 // Response codes 2.00 - 5.31
225 // 2.00 - 2.05
226 #define COAP_CODE_CREATED 0x41
227 #define COAP_CODE_DELETED 0x42
228 #define COAP_CODE_VALID 0x43
229 #define COAP_CODE_CHANGED 0x44
230 #define COAP_CODE_CONTENT 0x45
231 
232 // 4.00 - 4.15
233 #define COAP_CODE_BAD_REQUEST 0x80
234 #define COAP_CODE_UNAUTHORIZED 0x81
235 #define COAP_CODE_BAD_OPTION 0x82
236 #define COAP_CODE_FORBIDDEN 0x83
237 #define COAP_CODE_NOT_FOUND 0x84
238 #define COAP_CODE_METHOD_NOT_ALLOWED 0x85
239 #define COAP_CODE_NOT_ACCEPTABLE 0x86
240 #define COAP_CODE_PRECONDITION_FAILED 0x8C
241 #define COAP_CODE_REQUEST_ENTITY_TOO_LARGE 0x8D
242 #define COAP_CODE_UNSUPPORTED_CONTENT_FORMAT 0x8F
243 
244 // 5.00 - 5.05
245 #define COAP_CODE_INTERNAL_SERVER_ERROR 0xA0
246 #define COAP_CODE_NOT_IMPLEMENTED 0xA1
247 #define COAP_CODE_BAD_GATEWAY 0xA2
248 #define COAP_CODE_SERVICE_UNAVAILABLE 0xA3
249 #define COAP_CODE_GATEWAY_TIMEOUT 0xA4
250 #define COAP_CODE_PROXYING_NOT_SUPPORTED 0xA5
251 */