-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcoap.cpp
325 lines (270 loc) · 9.64 KB
/
coap.cpp
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#include "coap.h"
#if defined(ARDUINO)
#include "Arduino.h"
#elif defined(SPARK)
#include "application.h"
#endif
#define LOGGING
Coap::Coap(
#if defined(ARDUINO)
UDP& udp
#endif
) {
#if defined(ARDUINO)
this->_udp = &udp;
#elif defined(SPARK)
this->_udp = new UDP();
#endif
}
bool Coap::start() {
this->start(COAP_DEFAULT_PORT);
return true;
}
bool Coap::start(int port) {
this->_udp->begin(port);
return true;
}
uint16_t Coap::sendPacket(CoapPacket &packet, IPAddress ip) {
return this->sendPacket(packet, ip, COAP_DEFAULT_PORT);
}
uint16_t Coap::sendPacket(CoapPacket &packet, IPAddress ip, int port) {
uint8_t buffer[BUF_MAX_SIZE];
uint8_t *p = buffer;
uint16_t running_delta = 0;
uint16_t packetSize = 0;
// make coap packet base header
*p = 0x01 << 6;
*p |= (packet.type & 0x03) << 4;
*p++ |= (packet.tokenlen & 0x0F);
*p++ = packet.code;
*p++ = (packet.messageid >> 8);
*p++ = (packet.messageid & 0xFF);
p = buffer + COAP_HEADER_SIZE;
packetSize += 4;
// make token
if (packet.token != NULL && packet.tokenlen <= 0x0F) {
memcpy(p, packet.token, packet.tokenlen);
p += packet.tokenlen;
packetSize += packet.tokenlen;
}
// make option header
for (int i = 0; i < packet.optionnum; i++) {
uint32_t optdelta;
uint8_t len, delta;
if (packetSize + 5 + packet.options[i].length >= BUF_MAX_SIZE) {
return 0;
}
optdelta = packet.options[i].number - running_delta;
COAP_OPTION_DELTA(optdelta, &delta);
COAP_OPTION_DELTA((uint32_t)packet.options[i].length, &len);
*p++ = (0xFF & (delta << 4 | len));
if (delta == 13) {
*p++ = (optdelta - 13);
packetSize++;
} else if (delta == 14) {
*p++ = ((optdelta - 269) >> 8);
*p++ = (0xFF & (optdelta - 269));
packetSize+=2;
} if (len == 13) {
*p++ = (packet.options[i].length - 13);
packetSize++;
} else if (len == 14) {
*p++ = (packet.options[i].length >> 8);
*p++ = (0xFF & (packet.options[i].length - 269));
packetSize+=2;
}
memcpy(p, packet.options[i].buffer, packet.options[i].length);
p += packet.options[i].length;
packetSize += packet.options[i].length + 1;
running_delta = packet.options[i].number;
}
// make payload
if (packet.payloadlen > 0) {
if ((packetSize + 1 + packet.payloadlen) >= BUF_MAX_SIZE) {
return 0;
}
*p++ = 0xFF;
memcpy(p, packet.payload, packet.payloadlen);
packetSize += 1 + packet.payloadlen;
}
_udp->beginPacket(ip, port);
_udp->write(buffer, packetSize);
_udp->endPacket();
return packet.messageid;
}
uint16_t Coap::get(IPAddress ip, int port, char *url) {
return this->send(ip, port, url, COAP_CON, COAP_GET, NULL, 0, NULL, 0);
}
uint16_t Coap::put(IPAddress ip, int port, char *url, char *payload) {
return this->send(ip, port, url, COAP_CON, COAP_PUT, NULL, 0, (uint8_t *)payload, strlen(payload));
}
uint16_t Coap::put(IPAddress ip, int port, char *url, char *payload, int payloadlen) {
return this->send(ip, port, url, COAP_CON, COAP_PUT, NULL, 0, (uint8_t *)payload, payloadlen);
}
uint16_t Coap::send(IPAddress ip, int port, char *url, COAP_TYPE type, COAP_METHOD method, uint8_t *token, uint8_t tokenlen, uint8_t *payload, uint32_t payloadlen) {
// make packet
CoapPacket packet;
packet.type = type;
packet.code = method;
packet.token = token;
packet.tokenlen = tokenlen;
packet.payload = payload;
packet.payloadlen = payloadlen;
packet.optionnum = 0;
packet.messageid = rand();
// if more options?
packet.options[packet.optionnum].buffer = (uint8_t *)url;
packet.options[packet.optionnum].length = strlen(url);
packet.options[packet.optionnum].number = COAP_URI_PATH;
packet.optionnum++;
// send packet
return this->sendPacket(packet, ip, port);
}
int Coap::parseOption(CoapOption *option, uint16_t *running_delta, uint8_t **buf, size_t buflen) {
uint8_t *p = *buf;
uint8_t headlen = 1;
uint16_t len, delta;
if (buflen < headlen) return -1;
delta = (p[0] & 0xF0) >> 4;
len = p[0] & 0x0F;
if (delta == 13) {
headlen++;
if (buflen < headlen) return -1;
delta = p[1] + 13;
p++;
} else if (delta == 14) {
headlen += 2;
if (buflen < headlen) return -1;
delta = ((p[1] << 8) | p[2]) + 269;
p+=2;
} else if (delta == 15) return -1;
if (len == 13) {
headlen++;
if (buflen < headlen) return -1;
len = p[1] + 13;
p++;
} else if (len == 14) {
headlen += 2;
if (buflen < headlen) return -1;
len = ((p[1] << 8) | p[2]) + 269;
p+=2;
} else if (len == 15)
return -1;
if ((p + 1 + len) > (*buf + buflen)) return -1;
option->number = delta + *running_delta;
option->buffer = p+1;
option->length = len;
*buf = p + 1 + len;
*running_delta += delta;
return 0;
}
bool Coap::loop() {
uint8_t buffer[BUF_MAX_SIZE];
int32_t packetlen = _udp->parsePacket();
while (packetlen > 0) {
packetlen = _udp->read(buffer, packetlen >= BUF_MAX_SIZE ? BUF_MAX_SIZE : packetlen);
CoapPacket packet;
// parse coap packet header
if (packetlen < COAP_HEADER_SIZE || (((buffer[0] & 0xC0) >> 6) != 1)) {
packetlen = _udp->parsePacket();
continue;
}
packet.type = (buffer[0] & 0x30) >> 4;
packet.tokenlen = buffer[0] & 0x0F;
packet.code = buffer[1];
packet.messageid = 0xFF00 & (buffer[2] << 8);
packet.messageid |= 0x00FF & buffer[3];
if (packet.tokenlen == 0) packet.token = NULL;
else if (packet.tokenlen <= 8) packet.token = buffer + 4;
else {
packetlen = _udp->parsePacket();
continue;
}
// parse packet options/payload
if (COAP_HEADER_SIZE + packet.tokenlen < packetlen) {
int optionIndex = 0;
uint16_t delta = 0;
uint8_t *end = buffer + packetlen;
uint8_t *p = buffer + COAP_HEADER_SIZE + packet.tokenlen;
while(optionIndex < MAX_OPTION_NUM && *p != 0xFF && p < end) {
packet.options[optionIndex];
if (0 != parseOption(&packet.options[optionIndex], &delta, &p, end-p))
return false;
optionIndex++;
}
packet.optionnum = optionIndex;
if (p+1 < end && *p == 0xFF) {
packet.payload = p+1;
packet.payloadlen = end-(p+1);
} else {
packet.payload = NULL;
packet.payloadlen= 0;
}
}
if (packet.type == COAP_ACK) {
// call response function
resp(packet, _udp->remoteIP(), _udp->remotePort());
} else if (packet.type == COAP_CON) {
// call endpoint url function
String url = "";
for (int i = 0; i < packet.optionnum; i++) {
if (packet.options[i].number == COAP_URI_PATH && packet.options[i].length > 0) {
char urlname[packet.options[i].length + 1];
memcpy(urlname, packet.options[i].buffer, packet.options[i].length);
urlname[packet.options[i].length] = NULL;
if(url.length() > 0)
url += "/";
url += urlname;
}
}
#if defined(ARDUINO)
if (!uri.find(url)) {
#elif defined(SPARK)
if (uri.find(url) == uri.end()) {
#endif
sendResponse(_udp->remoteIP(), _udp->remotePort(), packet.messageid, NULL, 0,
COAP_NOT_FOUNT, COAP_NONE, NULL, 0);
} else {
#if defined(ARDUINO)
uri.find(url)(packet, _udp->remoteIP(), _udp->remotePort());
#elif defined(SPARK)
uri[url](packet, _udp->remoteIP(), _udp->remotePort());
#endif
}
}
// next packet
packetlen = _udp->parsePacket();
}
return true;
}
uint16_t Coap::sendResponse(IPAddress ip, int port, uint16_t messageid) {
this->sendResponse(ip, port, messageid, NULL, 0, COAP_CONTENT, COAP_TEXT_PLAIN, NULL, 0);
}
uint16_t Coap::sendResponse(IPAddress ip, int port, uint16_t messageid, char *payload) {
this->sendResponse(ip, port, messageid, payload, strlen(payload), COAP_CONTENT, COAP_TEXT_PLAIN, NULL, 0);
}
uint16_t Coap::sendResponse(IPAddress ip, int port, uint16_t messageid, char *payload, int payloadlen) {
this->sendResponse(ip, port, messageid, payload, payloadlen, COAP_CONTENT, COAP_TEXT_PLAIN, NULL, 0);
}
uint16_t Coap::sendResponse(IPAddress ip, int port, uint16_t messageid, char *payload, int payloadlen,
COAP_RESPONSE_CODE code, COAP_CONTENT_TYPE type, uint8_t *token, int tokenlen) {
// make packet
CoapPacket packet;
packet.type = COAP_ACK;
packet.code = code;
packet.token = token;
packet.tokenlen = tokenlen;
packet.payload = (uint8_t *)payload;
packet.payloadlen = payloadlen;
packet.optionnum = 0;
packet.messageid = messageid;
// if more options?
char optionBuffer[2];
optionBuffer[0] = ((uint16_t)type & 0xFF00) >> 8;
optionBuffer[1] = ((uint16_t)type & 0x00FF) ;
packet.options[packet.optionnum].buffer = (uint8_t *)optionBuffer;
packet.options[packet.optionnum].length = 2;
packet.options[packet.optionnum].number = COAP_CONTENT_FORMAT;
packet.optionnum++;
return this->sendPacket(packet, ip, port);
}