@@ -12,10 +12,23 @@ void CoapPacket::addOption(uint8_t number, uint8_t length, uint8_t *opt_payload)
1212 ++optionnum;
1313}
1414
15+
1516Coap::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
2134bool Coap::start () {
@@ -33,8 +46,7 @@ uint16_t Coap::sendPacket(CoapPacket &packet, IPAddress ip) {
3346}
3447
3548uint16_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
219231bool 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))
0 commit comments