-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathcantcoap.h
266 lines (232 loc) · 9.49 KB
/
cantcoap.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#pragma once
/// Copyright (c) 2013, Ashley Mills.
#include <unistd.h>
#include <stdint.h>
#include "dbg.h"
#define COAP_HDR_SIZE 4
#define COAP_OPTION_HDR_BYTE 1
// CoAP PDU format
// 0 1 2 3
// 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
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// |Ver| T | TKL | Code | Message ID |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | Token (if any, TKL bytes) ...
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | Options (if any) ...
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// |1 1 1 1 1 1 1 1| Payload (if any) ...
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
class CoapPDU {
public:
/// CoAP message types. Note, values only work as enum.
enum Type {
COAP_CONFIRMABLE=0x00,
COAP_NON_CONFIRMABLE=0x10,
COAP_ACKNOWLEDGEMENT=0x20,
COAP_RESET=0x30
};
// CoAP response codes.
enum Code {
COAP_EMPTY=0x00,
COAP_GET,
COAP_POST,
COAP_PUT,
COAP_DELETE,
COAP_LASTMETHOD=0x1F,
COAP_CREATED=0x41,
COAP_DELETED,
COAP_VALID,
COAP_CHANGED,
COAP_CONTENT,
COAP_BAD_REQUEST=0x80,
COAP_UNAUTHORIZED,
COAP_BAD_OPTION,
COAP_FORBIDDEN,
COAP_NOT_FOUND,
COAP_METHOD_NOT_ALLOWED,
COAP_NOT_ACCEPTABLE,
COAP_PRECONDITION_FAILED=0x8C,
COAP_REQUEST_ENTITY_TOO_LARGE=0x8D,
COAP_UNSUPPORTED_CONTENT_FORMAT=0x8F,
COAP_INTERNAL_SERVER_ERROR=0xA0,
COAP_NOT_IMPLEMENTED,
COAP_BAD_GATEWAY,
COAP_SERVICE_UNAVAILABLE,
COAP_GATEWAY_TIMEOUT,
COAP_PROXYING_NOT_SUPPORTED,
COAP_UNDEFINED_CODE=0xFF
};
/// CoAP option numbers.
enum Option {
COAP_OPTION_IF_MATCH=1,
COAP_OPTION_URI_HOST=3,
COAP_OPTION_ETAG,
COAP_OPTION_IF_NONE_MATCH,
COAP_OPTION_OBSERVE,
COAP_OPTION_URI_PORT,
COAP_OPTION_LOCATION_PATH,
COAP_OPTION_URI_PATH=11,
COAP_OPTION_CONTENT_FORMAT,
COAP_OPTION_MAX_AGE=14,
COAP_OPTION_URI_QUERY,
COAP_OPTION_ACCEPT=17,
COAP_OPTION_LOCATION_QUERY=20,
COAP_OPTION_BLOCK2=23,
COAP_OPTION_BLOCK1=27,
COAP_OPTION_SIZE2,
COAP_OPTION_PROXY_URI=35,
COAP_OPTION_PROXY_SCHEME=39,
COAP_OPTION_SIZE1=60
};
/// CoAP content-formats.
enum ContentFormat {
/* https://www.iana.org/assignments/core-parameters/core-parameters.xhtml#content-formats */
/* 0-255 Expert Review */
COAP_CONTENT_FORMAT_TEXT_PLAIN = 0 , // text/plain; charset=utf-8 /* Ref: [RFC2046][RFC3676][RFC5147] */
COAP_CONTENT_FORMAT_APP_COSE_ENCRYPT0 = 16 , // application/cose; cose-type="cose-encrypt0" /* Ref: [RFC8152] */
COAP_CONTENT_FORMAT_APP_COSE_MAC0 = 17 , // application/cose; cose-type="cose-mac0" /* Ref: [RFC8152] */
COAP_CONTENT_FORMAT_APP_COSE_SIGN1 = 18 , // application/cose; cose-type="cose-sign1" /* Ref: [RFC8152] */
COAP_CONTENT_FORMAT_APP_LINKFORMAT = 40 , // application/link-format /* Ref: [RFC6690] */
COAP_CONTENT_FORMAT_APP_XML = 41 , // application/xml /* Ref: [RFC3023] */
COAP_CONTENT_FORMAT_APP_OCTECT_STREAM = 42 , // application/octet-stream /* Ref: [RFC2045][RFC2046] */
COAP_CONTENT_FORMAT_APP_EXI = 47 , // application/exi /* Ref: ["Efficient XML Interchange (EXI) Format 1.0 (Second Edition)" ,February 2014] */
COAP_CONTENT_FORMAT_APP_JSON = 50 , // application/json /* Ref: [RFC4627] */
COAP_CONTENT_FORMAT_APP_JSON_PATCH_JSON = 51 , // application/json-patch+json /* Ref: [RFC6902] */
COAP_CONTENT_FORMAT_APP_MERGE_PATCH_JSON = 52 , // application/merge-patch+json /* Ref: [RFC7396] */
COAP_CONTENT_FORMAT_APP_CBOR = 60 , // application/cbor /* Ref: [RFC7049] */
COAP_CONTENT_FORMAT_APP_CWT = 61 , // application/cwt /* Ref: [RFC8392] */
COAP_CONTENT_FORMAT_APP_COSE_ENCRYPT = 96 , // application/cose; cose-type="cose-encrypt" /* Ref: [RFC8152] */
COAP_CONTENT_FORMAT_APP_COSE_MAC = 97 , // application/cose; cose-type="cose-mac" /* Ref: [RFC8152] */
COAP_CONTENT_FORMAT_APP_COSE_SIGN = 98 , // application/cose; cose-type="cose-sign" /* Ref: [RFC8152] */
COAP_CONTENT_FORMAT_APP_COSE_KEY = 101 , // application/cose-key /* Ref: [RFC8152] */
COAP_CONTENT_FORMAT_APP_COSE_KEY_SET = 102 , // application/cose-key-set /* Ref: [RFC8152] */
COAP_CONTENT_FORMAT_APP_COAP_GROUP_JSON = 256 , // application/coap-group+json /* Ref: [RFC7390] */
/* 256-9999 IETF Review or IESG Approval */
COAP_CONTENT_FORMAT_APP_OMA_TLV_OLD = 1542 , // Keep old value for backward-compatibility /* Ref: [OMA-TS-LightweightM2M-V1_0] */
COAP_CONTENT_FORMAT_APP_OMA_JSON_OLD = 1543 , // Keep old value for backward-compatibility /* Ref: [OMA-TS-LightweightM2M-V1_0] */
/* 10000-64999 First Come First Served */
COAP_CONTENT_FORMAT_APP_VND_OCF_CBOR = 10000, // application/vnd.ocf+cbor /* Ref: [Michael_Koster] */
COAP_CONTENT_FORMAT_APP_OMA_TLV = 11542, // application/vnd.oma.lwm2m+tlv /* Ref: [OMA-TS-LightweightM2M-V1_0] */
COAP_CONTENT_FORMAT_APP_OMA_JSON = 11543 // application/vnd.oma.lwm2m+json /* Ref: [OMA-TS-LightweightM2M-V1_0] */
/* 65000-65535 Experimental use (no operational use) */
};
/// Sequence of these is returned by CoapPDU::getOptions()
struct CoapOption {
uint16_t optionDelta;
uint16_t optionNumber;
uint16_t optionValueLength;
int totalLength;
uint8_t *optionPointer;
uint8_t *optionValuePointer;
};
// construction and destruction
CoapPDU();
CoapPDU(uint8_t *pdu, int pduLength);
CoapPDU(uint8_t *buffer, int bufferLength, int pduLength);
~CoapPDU();
int reset();
int validate();
// version
int setVersion(uint8_t version);
uint8_t getVersion();
// message type
void setType(CoapPDU::Type type);
CoapPDU::Type getType();
// tokens
int setTokenLength(uint8_t tokenLength);
int getTokenLength();
uint8_t* getTokenPointer();
int setToken(uint8_t *token, uint8_t tokenLength);
// message code
void setCode(CoapPDU::Code code);
CoapPDU::Code getCode();
CoapPDU::Code httpStatusToCode(int httpStatus);
// message ID
int setMessageID(uint16_t messageID);
uint16_t getMessageID();
// options
int addOption(uint16_t optionNumber, uint16_t optionLength, uint8_t *optionValue);
// gets a list of all options
CoapOption* getOptions();
int getNumOptions();
// shorthand helpers
int setURI(char *uri);
int setURI(char *uri, int urilen);
int getURI(char *dst, int dstlen, int *outLen);
int addURIQuery(char *query);
// content format helper
int setContentFormat(CoapPDU::ContentFormat format);
// payload
uint8_t* mallocPayload(int bytes);
int setPayload(uint8_t *value, int len);
uint8_t* getPayloadPointer();
int getPayloadLength();
uint8_t* getPayloadCopy();
// pdu
int getPDULength();
uint8_t* getPDUPointer();
void setPDULength(int len);
// debugging
static void printBinary(uint8_t b);
void print();
void printBin();
void printHex();
void printOptionHuman(uint8_t *option);
void printHuman();
void printPDUAsCArray();
private:
// variables
uint8_t *_pdu;
int _pduLength;
int _constructedFromBuffer;
int _bufferLength;
uint8_t *_payloadPointer;
int _payloadLength;
int _numOptions;
uint16_t _maxAddedOptionNumber;
// functions
void shiftPDUUp(int shiftOffset, int shiftAmount);
void shiftPDUDown(int startLocation, int shiftOffset, int shiftAmount);
uint8_t codeToValue(CoapPDU::Code c);
// option stuff
int findInsertionPosition(uint16_t optionNumber, uint16_t *prevOptionNumber);
int computeExtraBytes(uint16_t n);
int insertOption(int insertionPosition, uint16_t optionDelta, uint16_t optionValueLength, uint8_t *optionValue);
uint16_t getOptionDelta(uint8_t *option);
void setOptionDelta(int optionPosition, uint16_t optionDelta);
uint16_t getOptionValueLength(uint8_t *option);
};
/*
#define COAP_CODE_EMPTY 0x00
// method codes 0.01-0.31
#define COAP_CODE_GET 0x01
#define COAP_CODE_POST 0x02
#define COAP_CODE_PUT 0x03
#define COAP_CODE_DELETE 0x04
// Response codes 2.00 - 5.31
// 2.00 - 2.05
#define COAP_CODE_CREATED 0x41
#define COAP_CODE_DELETED 0x42
#define COAP_CODE_VALID 0x43
#define COAP_CODE_CHANGED 0x44
#define COAP_CODE_CONTENT 0x45
// 4.00 - 4.15
#define COAP_CODE_BAD_REQUEST 0x80
#define COAP_CODE_UNAUTHORIZED 0x81
#define COAP_CODE_BAD_OPTION 0x82
#define COAP_CODE_FORBIDDEN 0x83
#define COAP_CODE_NOT_FOUND 0x84
#define COAP_CODE_METHOD_NOT_ALLOWED 0x85
#define COAP_CODE_NOT_ACCEPTABLE 0x86
#define COAP_CODE_PRECONDITION_FAILED 0x8C
#define COAP_CODE_REQUEST_ENTITY_TOO_LARGE 0x8D
#define COAP_CODE_UNSUPPORTED_CONTENT_FORMAT 0x8F
// 5.00 - 5.05
#define COAP_CODE_INTERNAL_SERVER_ERROR 0xA0
#define COAP_CODE_NOT_IMPLEMENTED 0xA1
#define COAP_CODE_BAD_GATEWAY 0xA2
#define COAP_CODE_SERVICE_UNAVAILABLE 0xA3
#define COAP_CODE_GATEWAY_TIMEOUT 0xA4
#define COAP_CODE_PROXYING_NOT_SUPPORTED 0xA5
*/