Skip to content

Commit b7d8118

Browse files
authored
Merge pull request #32 from hirotakaster/dev-128
update for #26
2 parents 13a70ce + 11fee0a commit b7d8118

File tree

8 files changed

+49
-25
lines changed

8 files changed

+49
-25
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
# CoAP client, server library for Arduino.
2-
<a href="http://coap.technology/" target=_blank>CoAP</a> simple server, client library for Arduino IDE, ESP32.
2+
<a href="http://coap.technology/" target=_blank>CoAP</a> simple server, client library for Arduino IDE/PlatformIO, ESP32, ESP8266.
33

44
## Source Code
5-
This lightweight library's source code contains only 2 files. coap.cpp, coap.h.
5+
This lightweight library's source code contains only 2 files. coap-simple.cpp, coap-simple.h.
66

77
## Example
88
Some sample sketches for Arduino included(/examples/).
99

1010
- coaptest.ino : simple request/response sample.
1111
- coapserver.ino : server endpoint url callback sample.
12+
- esp32.ino, esp8266.ino : server endpoint url callback/response.
1213

1314
## How to use
1415
Download this source code branch zip file and extract it to the Arduino libraries directory or checkout repository. Here is checkout on MacOS X.

coap-simple.cpp

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,23 @@ void CoapPacket::addOption(uint8_t number, uint8_t length, uint8_t *opt_payload)
1212
++optionnum;
1313
}
1414

15+
1516
Coap::Coap(
16-
UDP& udp
17+
UDP& udp,
18+
int coap_buf_size /* default value is COAP_BUF_MAX_SIZE */
1719
) {
1820
this->_udp = &udp;
21+
this->coap_buf_size = coap_buf_size;
22+
this->tx_buffer = new uint8_t[this->coap_buf_size];
23+
this->rx_buffer = new uint8_t[this->coap_buf_size];
24+
}
25+
26+
Coap::~Coap() {
27+
if (this->tx_buffer != NULL)
28+
delete[] this->tx_buffer;
29+
30+
if (this->rx_buffer != NULL)
31+
delete[] this->rx_buffer;
1932
}
2033

2134
bool Coap::start() {
@@ -33,8 +46,7 @@ uint16_t Coap::sendPacket(CoapPacket &packet, IPAddress ip) {
3346
}
3447

3548
uint16_t Coap::sendPacket(CoapPacket &packet, IPAddress ip, int port) {
36-
uint8_t buffer[COAP_BUF_MAX_SIZE];
37-
uint8_t *p = buffer;
49+
uint8_t *p = this->tx_buffer;
3850
uint16_t running_delta = 0;
3951
uint16_t packetSize = 0;
4052

@@ -45,7 +57,7 @@ uint16_t Coap::sendPacket(CoapPacket &packet, IPAddress ip, int port) {
4557
*p++ = packet.code;
4658
*p++ = (packet.messageid >> 8);
4759
*p++ = (packet.messageid & 0xFF);
48-
p = buffer + COAP_HEADER_SIZE;
60+
p = this->tx_buffer + COAP_HEADER_SIZE;
4961
packetSize += 4;
5062

5163
// make token
@@ -60,7 +72,7 @@ uint16_t Coap::sendPacket(CoapPacket &packet, IPAddress ip, int port) {
6072
uint32_t optdelta;
6173
uint8_t len, delta;
6274

63-
if (packetSize + 5 + packet.options[i].length >= COAP_BUF_MAX_SIZE) {
75+
if (packetSize + 5 + packet.options[i].length >= coap_buf_size) {
6476
return 0;
6577
}
6678
optdelta = packet.options[i].number - running_delta;
@@ -92,7 +104,7 @@ uint16_t Coap::sendPacket(CoapPacket &packet, IPAddress ip, int port) {
92104

93105
// make payload
94106
if (packet.payloadlen > 0) {
95-
if ((packetSize + 1 + packet.payloadlen) >= COAP_BUF_MAX_SIZE) {
107+
if ((packetSize + 1 + packet.payloadlen) >= coap_buf_size) {
96108
return 0;
97109
}
98110
*p++ = 0xFF;
@@ -101,7 +113,7 @@ uint16_t Coap::sendPacket(CoapPacket &packet, IPAddress ip, int port) {
101113
}
102114

103115
_udp->beginPacket(ip, port);
104-
_udp->write(buffer, packetSize);
116+
_udp->write(this->tx_buffer, packetSize);
105117
_udp->endPacket();
106118

107119
return packet.messageid;
@@ -217,29 +229,27 @@ int Coap::parseOption(CoapOption *option, uint16_t *running_delta, uint8_t **buf
217229
}
218230

219231
bool Coap::loop() {
220-
221-
uint8_t buffer[COAP_BUF_MAX_SIZE];
222232
int32_t packetlen = _udp->parsePacket();
223233

224234
while (packetlen > 0) {
225-
packetlen = _udp->read(buffer, packetlen >= COAP_BUF_MAX_SIZE ? COAP_BUF_MAX_SIZE : packetlen);
235+
packetlen = _udp->read(this->rx_buffer, packetlen >= coap_buf_size ? coap_buf_size : packetlen);
226236

227237
CoapPacket packet;
228238

229239
// parse coap packet header
230-
if (packetlen < COAP_HEADER_SIZE || (((buffer[0] & 0xC0) >> 6) != 1)) {
240+
if (packetlen < COAP_HEADER_SIZE || (((this->rx_buffer[0] & 0xC0) >> 6) != 1)) {
231241
packetlen = _udp->parsePacket();
232242
continue;
233243
}
234244

235-
packet.type = (buffer[0] & 0x30) >> 4;
236-
packet.tokenlen = buffer[0] & 0x0F;
237-
packet.code = buffer[1];
238-
packet.messageid = 0xFF00 & (buffer[2] << 8);
239-
packet.messageid |= 0x00FF & buffer[3];
245+
packet.type = (this->rx_buffer[0] & 0x30) >> 4;
246+
packet.tokenlen = this->rx_buffer[0] & 0x0F;
247+
packet.code = this->rx_buffer[1];
248+
packet.messageid = 0xFF00 & (this->rx_buffer[2] << 8);
249+
packet.messageid |= 0x00FF & this->rx_buffer[3];
240250

241251
if (packet.tokenlen == 0) packet.token = NULL;
242-
else if (packet.tokenlen <= 8) packet.token = buffer + 4;
252+
else if (packet.tokenlen <= 8) packet.token = this->rx_buffer + 4;
243253
else {
244254
packetlen = _udp->parsePacket();
245255
continue;
@@ -249,8 +259,8 @@ bool Coap::loop() {
249259
if (COAP_HEADER_SIZE + packet.tokenlen < packetlen) {
250260
int optionIndex = 0;
251261
uint16_t delta = 0;
252-
uint8_t *end = buffer + packetlen;
253-
uint8_t *p = buffer + COAP_HEADER_SIZE + packet.tokenlen;
262+
uint8_t *end = this->rx_buffer + packetlen;
263+
uint8_t *p = this->rx_buffer + COAP_HEADER_SIZE + packet.tokenlen;
254264
while(optionIndex < COAP_MAX_OPTION_NUM && *p != 0xFF && p < end) {
255265
//packet.options[optionIndex];
256266
if (0 != parseOption(&packet.options[optionIndex], &delta, &p, end-p))

coap-simple.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,15 +177,20 @@ class Coap {
177177
CoapUri uri;
178178
CoapCallback resp;
179179
int _port;
180+
int coap_buf_size;
181+
uint8_t *tx_buffer = NULL;
182+
uint8_t *rx_buffer = NULL;
180183

181184
uint16_t sendPacket(CoapPacket &packet, IPAddress ip);
182185
uint16_t sendPacket(CoapPacket &packet, IPAddress ip, int port);
183186
int parseOption(CoapOption *option, uint16_t *running_delta, uint8_t **buf, size_t buflen);
184187

185188
public:
186189
Coap(
187-
UDP& udp
190+
UDP& udp,
191+
int coap_buf_size = COAP_BUF_MAX_SIZE
188192
);
193+
~Coap();
189194
bool start();
190195
bool start(int port);
191196
void response(CoapCallback c) { resp = c; }

examples/esp32/esp32.ino

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ void callback_response(CoapPacket &packet, IPAddress ip, int port);
1212
void callback_light(CoapPacket &packet, IPAddress ip, int port);
1313

1414
// UDP and CoAP class
15+
// other initialize is "Coap coap(Udp, 512);"
16+
// 2nd default parameter is COAP_BUF_MAX_SIZE(defaulit:128)
17+
// For UDP fragmentation, it is good to set the maximum under
18+
// 1280byte when using the internet connection.
1519
WiFiUDP udp;
1620
Coap coap(udp);
1721

examples/esp8266/esp8266.ino

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ void callback_response(CoapPacket &packet, IPAddress ip, int port);
1212
void callback_light(CoapPacket &packet, IPAddress ip, int port);
1313

1414
// UDP and CoAP class
15+
// other initialize is "Coap coap(Udp, 512);"
16+
// 2nd default parameter is COAP_BUF_MAX_SIZE(defaulit:128)
17+
// For UDP fragmentation, it is good to set the maximum under
18+
// 1280byte when using the internet connection.
1519
WiFiUDP udp;
1620
Coap coap(udp);
1721

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name" : "CoAP simple library",
3-
"version" : "1.3.24",
3+
"version" : "1.3.25",
44
"description" : "Simple CoAP client/server library for generic Arduino Client hardware.",
55
"keywords" : "communication, coap, wifi",
66
"authors" : {

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=CoAP simple library
2-
version=1.3.24
2+
version=1.3.25
33
author=Hirotaka Niisato <[email protected]>
44
maintainer=Hirotaka Niisato <[email protected]>
55
sentence=Simple CoAP client/server library for generic Arduino Client hardware.

license.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2018 Hirotaka Niisato
1+
Copyright (c) 2022 Hirotaka Niisato
22
This software is released under the MIT License.
33

44
Permission is hereby granted, free of charge, to any person obtaining

0 commit comments

Comments
 (0)