-
Notifications
You must be signed in to change notification settings - Fork 1
/
coapmessage.cpp
257 lines (228 loc) · 6.82 KB
/
coapmessage.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
/*
University of Parma - Italy
This file is part of ArduinoCoAP
ArduinoCoAP is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Author:
Daniele Sportillo (daniele.sportillo@gmail.com)
*/
#include "coapmessage.h"
CoapMessage::CoapMessage(byte type, byte code, Options* opts, char* payload) {
this->ver = VER;
this->type = type;
this->code = code;
this->messageId = this->pickMessageId();
this->options = opts;
this->token = pickToken();
this->tokenLen = 2;
this->payloadLen = strlen(payload);
this->payload = (char*)malloc(this->payloadLen);
strcpy(this->payload, payload);
}
CoapMessage::CoapMessage(byte type, byte code, char* payload) {
this->ver = VER;
this->type = type;
this->code = code;
this->messageId = this->pickMessageId();
this->options = NULL;
this->tokenLen = 2;
this->token = pickToken();
this->payloadLen = strlen(payload);
this->payload = (char*)malloc(this->payloadLen);
strcpy(this->payload, payload);
}
CoapMessage::CoapMessage(byte type, byte code, byte* token, char* payload) {
this->ver = VER;
this->type = type;
this->code = code;
this->messageId = this->pickMessageId();
this->options = NULL;
this->tokenLen = 2;
this->token = (byte*)malloc(this->tokenLen);
for(byte i=0; i<this->tokenLen; i++)
this->token[i] = token[i];
this->payloadLen = strlen(payload);
this->payload = (char*)malloc(this->payloadLen);
strcpy(this->payload, payload);
}
CoapMessage::CoapMessage(unsigned int messageId) {
this->ver = VER;
this->type = 2;
this->code = 0;
this->messageId = messageId;
this->options = NULL;
this->token = NULL;
this->payload = NULL;
}
CoapMessage::~CoapMessage() {
if(this->options != NULL){
delete this->options;
this->options = NULL;
}
if(this->token != NULL){
free(this->token);
this->token = NULL;
}
if(this->payload != NULL){
free(this->payload);
this->payload = NULL;
}
}
Option* CoapMessage::parseCoapOption(byte prevOptNum, char* buf, byte index) {
Option* opt = new Option();
opt->init(prevOptNum, buf, index);
return opt;
}
void CoapMessage::send(EthernetUDP& Udp, IPAddress address, unsigned int port) {
byte* packet = (byte*)this->toBuffer();
Udp.beginPacket(address, port);
Udp.write(&packet[1], packet[0]);
Udp.endPacket();
free(packet);
packet = NULL;
}
CoapMessage::CoapMessage(char* packetBuffer, byte len) {
byte index = 0;
this->ver=(byte)(packetBuffer[index]>>6)&0x3;
this->type=(byte)((packetBuffer[index]>>4)&0x3);
this->tokenLen=(byte)packetBuffer[index++]&0xf;
this->code=(byte)packetBuffer[index++]&0xff;
this->messageId=((packetBuffer[index]&0xff)<<8)+(packetBuffer[index+1]&0xff);
index += 2;
if (tokenLen>0) {
this->token = (byte*)malloc(tokenLen*sizeof(byte));
if (token == NULL) Serial.println("MEMORY ERROR");
for (int i=0; i<tokenLen; i++) {
this->token[i] = (byte)packetBuffer[index++];
}
}else{
this->token = NULL;
}
this->options = new Options();
byte prevOptNum=0;
while (index<len && packetBuffer[index]!=PAYLOAD_MARKER) {
Option* opt = this->parseCoapOption(prevOptNum, packetBuffer, index);
this->options->addOption(opt, true);
index += opt->getLen(prevOptNum);
prevOptNum = opt->getNumber();
}
if (index<len) {
index++;
this->payloadLen = len-index;
this->payload = (char*)malloc(payloadLen*sizeof(char));
for (byte i=0; i<payloadLen; i++) {
this->payload[i] = (char)packetBuffer[i+index];
}
}else{
this->payloadLen = 0;
this->payload = NULL;
}
}
byte* CoapMessage::pickToken(){
int n = random(255);
byte *token = (byte*)&n;
return token;
}
unsigned int CoapMessage::pickMessageId() {
return rand() % 20000;
}
char* CoapMessage::toBuffer() {
byte index=1;
char* buf = (char*)malloc(UDP_TX_PACKET_MAX_SIZE);
buf[index++] = (char)(((ver&0x3)<<6) | ((type&0x3)<<4) | (tokenLen&0xf));
buf[index++] = (char)0xff&code;
buf[index++] = (char)0xFF& (messageId >> 8);
buf[index++] = (char)0xFF & messageId;
buf[index++] = (char)token[0];
buf[index++] = (char)token[1];
if(this->options != NULL) {
byte prevOptNum = 0;
for(byte i = 0; i<options->getSize(); i++){
Option* opt = options->getOptionAt(i);
index+=opt->getBytes(prevOptNum, buf, index);
prevOptNum = opt->getNumber();
free(opt);
opt = NULL;
}
}
if (payload && payloadLen>0) {
buf[index++] = PAYLOAD_MARKER;
for (byte i=0; i<payloadLen; i++)
buf[index++]=(char)payload[i];
}
buf[index++] = 0;
buf[0]=index-2;
return buf;
}
boolean CoapMessage::isRequest() {
return this->code>=1 && this->code <32;
}
void CoapMessage::toString(){
Serial.println("\n######START#####");
Serial.print("Version: ");
Serial.println(this->ver);
Serial.print("Type [0 CON, 1 NON, 2 ACK, 3 RST]: ");
Serial.println(this->type);
Serial.print("Code [1 GET, 2 POST, 3 PUT, 4 DELETE]: ");
Serial.println(this->code);
Serial.print("Message ID: ");
Serial.println(this->messageId);
Serial.print("Token: ");
if(this->token != NULL){
for(byte i=0; i<this->tokenLen; i++)
Serial.print(token[i], HEX);
}
Serial.print("\nOptions: ");
if(this->options && this->options->getSize()>0){
for(int i = 0; i<this->options->getSize(); i++) {
Serial.print("\n\tNumber: ");
Serial.println(this->options->getOptionAt(i)->getNumber());
Serial.print("\tValue: ");
Serial.println(this->options->getOptionAt(i)->getValue());
}
}
Serial.print("\nPayload: ");
if (this->payload!=NULL) {
for(byte i = 0; i<this->payloadLen; i++)
Serial.print(this->payload[i]);
}
Serial.print("\nPayloadLEN: ");
Serial.println(this->payloadLen);
Serial.println("\n######END#####");
}
byte CoapMessage::getCode() {
return this->code;
}
byte CoapMessage::getType() {
return this->type;
}
unsigned int CoapMessage::getMessageId() {
return this->messageId;
}
char* CoapMessage::getPayload() {
return this->payload;
}
Options* CoapMessage::getOptions() {
return this->options;
}
byte* CoapMessage::getToken(){
return this->token;
}
void CoapMessage::setRemoteSocketAddress(IPAddress address, unsigned int port){
this->remoteIPAddr = address;
this->remotePort = port;
}
IPAddress CoapMessage::getRemoteIPAddr() {
return this->remoteIPAddr;
}
unsigned int CoapMessage::getRemotePort() {
return this->remotePort;
}