Skip to content

Commit

Permalink
Merge pull request #32 from hirotakaster/dev-128
Browse files Browse the repository at this point in the history
update for #26
  • Loading branch information
hirotakaster authored Aug 2, 2022
2 parents 13a70ce + 11fee0a commit b7d8118
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 25 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
# CoAP client, server library for Arduino.
<a href="http://coap.technology/" target=_blank>CoAP</a> simple server, client library for Arduino IDE, ESP32.
<a href="http://coap.technology/" target=_blank>CoAP</a> simple server, client library for Arduino IDE/PlatformIO, ESP32, ESP8266.

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

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

- coaptest.ino : simple request/response sample.
- coapserver.ino : server endpoint url callback sample.
- esp32.ino, esp8266.ino : server endpoint url callback/response.

## How to use
Download this source code branch zip file and extract it to the Arduino libraries directory or checkout repository. Here is checkout on MacOS X.
Expand Down
48 changes: 29 additions & 19 deletions coap-simple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,23 @@ void CoapPacket::addOption(uint8_t number, uint8_t length, uint8_t *opt_payload)
++optionnum;
}


Coap::Coap(
UDP& udp
UDP& udp,
int coap_buf_size /* default value is COAP_BUF_MAX_SIZE */
) {
this->_udp = &udp;
this->coap_buf_size = coap_buf_size;
this->tx_buffer = new uint8_t[this->coap_buf_size];
this->rx_buffer = new uint8_t[this->coap_buf_size];
}

Coap::~Coap() {
if (this->tx_buffer != NULL)
delete[] this->tx_buffer;

if (this->rx_buffer != NULL)
delete[] this->rx_buffer;
}

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

uint16_t Coap::sendPacket(CoapPacket &packet, IPAddress ip, int port) {
uint8_t buffer[COAP_BUF_MAX_SIZE];
uint8_t *p = buffer;
uint8_t *p = this->tx_buffer;
uint16_t running_delta = 0;
uint16_t packetSize = 0;

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

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

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

// make payload
if (packet.payloadlen > 0) {
if ((packetSize + 1 + packet.payloadlen) >= COAP_BUF_MAX_SIZE) {
if ((packetSize + 1 + packet.payloadlen) >= coap_buf_size) {
return 0;
}
*p++ = 0xFF;
Expand All @@ -101,7 +113,7 @@ uint16_t Coap::sendPacket(CoapPacket &packet, IPAddress ip, int port) {
}

_udp->beginPacket(ip, port);
_udp->write(buffer, packetSize);
_udp->write(this->tx_buffer, packetSize);
_udp->endPacket();

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

bool Coap::loop() {

uint8_t buffer[COAP_BUF_MAX_SIZE];
int32_t packetlen = _udp->parsePacket();

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

CoapPacket packet;

// parse coap packet header
if (packetlen < COAP_HEADER_SIZE || (((buffer[0] & 0xC0) >> 6) != 1)) {
if (packetlen < COAP_HEADER_SIZE || (((this->rx_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];
packet.type = (this->rx_buffer[0] & 0x30) >> 4;
packet.tokenlen = this->rx_buffer[0] & 0x0F;
packet.code = this->rx_buffer[1];
packet.messageid = 0xFF00 & (this->rx_buffer[2] << 8);
packet.messageid |= 0x00FF & this->rx_buffer[3];

if (packet.tokenlen == 0) packet.token = NULL;
else if (packet.tokenlen <= 8) packet.token = buffer + 4;
else if (packet.tokenlen <= 8) packet.token = this->rx_buffer + 4;
else {
packetlen = _udp->parsePacket();
continue;
Expand All @@ -249,8 +259,8 @@ bool Coap::loop() {
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;
uint8_t *end = this->rx_buffer + packetlen;
uint8_t *p = this->rx_buffer + COAP_HEADER_SIZE + packet.tokenlen;
while(optionIndex < COAP_MAX_OPTION_NUM && *p != 0xFF && p < end) {
//packet.options[optionIndex];
if (0 != parseOption(&packet.options[optionIndex], &delta, &p, end-p))
Expand Down
7 changes: 6 additions & 1 deletion coap-simple.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,20 @@ class Coap {
CoapUri uri;
CoapCallback resp;
int _port;
int coap_buf_size;
uint8_t *tx_buffer = NULL;
uint8_t *rx_buffer = NULL;

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

public:
Coap(
UDP& udp
UDP& udp,
int coap_buf_size = COAP_BUF_MAX_SIZE
);
~Coap();
bool start();
bool start(int port);
void response(CoapCallback c) { resp = c; }
Expand Down
4 changes: 4 additions & 0 deletions examples/esp32/esp32.ino
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ void callback_response(CoapPacket &packet, IPAddress ip, int port);
void callback_light(CoapPacket &packet, IPAddress ip, int port);

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

Expand Down
4 changes: 4 additions & 0 deletions examples/esp8266/esp8266.ino
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ void callback_response(CoapPacket &packet, IPAddress ip, int port);
void callback_light(CoapPacket &packet, IPAddress ip, int port);

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

Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name" : "CoAP simple library",
"version" : "1.3.24",
"version" : "1.3.25",
"description" : "Simple CoAP client/server library for generic Arduino Client hardware.",
"keywords" : "communication, coap, wifi",
"authors" : {
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=CoAP simple library
version=1.3.24
version=1.3.25
author=Hirotaka Niisato <hirotakaster@gmail.com>
maintainer=Hirotaka Niisato <hirotakaster@gmail.com>
sentence=Simple CoAP client/server library for generic Arduino Client hardware.
Expand Down
2 changes: 1 addition & 1 deletion license.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2018 Hirotaka Niisato
Copyright (c) 2022 Hirotaka Niisato
This software is released under the MIT License.

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

0 comments on commit b7d8118

Please sign in to comment.