ProsecCoAP 🥂
 
Loading...
Searching...
No Matches
ProsecCoAP.h
Go to the documentation of this file.
1
7#ifndef __PROSECCOAP_H__
8#define __PROSECCOAP_H__
9
10// Include common types and definitions.
11#include "Definitions.h"
12
13// Include public utility helpers.
14#include "Utils.h"
15
16// Include internal implementation details.
17#include "detail/Detail.h"
18
19// Include Observers management functionality.
20#include "observers/Observers.h"
21
28namespace Coap
29{
30
31 // SECTION Functions.
51 ErrorCode getRandomToken(size_t length, uint8_t *buffer);
52
53 // End of Functions group
54 // !SECTION End of Functions.
55
61 struct Option
62 {
74 const uint8_t *value;
78 size_t length;
79
80 Option() : number(static_cast<OptionNumber>(0)), value(nullptr), length(0) {}
87 Option(OptionNumber number, const uint8_t *value, size_t length)
89 };
90
98 {
99
100 // Give access to private members to Message methods.
101 friend class Message;
105 const Message &_message;
111 size_t _currentByte;
115 uint16_t _currentOptionNumber;
116
117 public:
126 OptionIterator(const Message &message);
127
149 ErrorCode next(Option &option);
150 };
151
164 {
165 private:
169 uint8_t _message[COAP_MAX_MESSAGE_SIZE];
176 size_t _messageLength;
191 ErrorCode _insert(size_t startPosition, const uint8_t *data, size_t length);
192
204 ErrorCode _remove(size_t startPosition, size_t length);
205
214 static uint16_t _getNextId();
215
223 void _setId(uint16_t id);
224
225 public:
236
247 Message(MessageType type, MessageCode code) : Message(type, code, _getNextId()) {}
248
259 Message(MessageType type, MessageCode code, uint16_t id);
260
268 const uint8_t *asRaw() const
269 {
270 return this->_message;
271 }
272
287 static ErrorCode fromUdp(UDP *udp, Message &message);
288
304 ErrorCode buildResponse(MessageCode code, Message &response) const;
305
314 uint8_t getVersion() const
315 {
316 return (this->_message[0] >> 6) & 0x03;
317 }
318
323 size_t getLength() const
324 {
325 return this->_messageLength;
326 }
327
333 void setType(MessageType type);
334
340 MessageType getType() const;
341
347 void setCode(MessageCode code);
353 MessageCode getCode() const;
354
362 uint16_t getId() const;
363
368 size_t getTokenLength() const;
369
380 ErrorCode setToken(const uint8_t *token, size_t length);
381
402 ErrorCode addRandomToken(size_t length);
403
422 const uint8_t *getToken() const;
423
432 bool matchesToken(const uint8_t *token, size_t length) const;
433
470 ErrorCode addOption(OptionNumber number, const uint8_t *value, size_t length);
471
477 {
478 return this->addOption(option.number, option.value, option.length);
479 };
480
494 ErrorCode addHost(IPAddress ip);
495
508 ErrorCode addPort(uint16_t port);
509
531 ErrorCode addPath(const char *path);
532
548 ErrorCode getPath(String *path) const;
549
569 ErrorCode getPayload(const uint8_t *&payload, size_t &length) const;
570
584 ErrorCode addPayload(const uint8_t *payload, size_t length);
585
599 ErrorCode addPayload(const uint8_t *payload, size_t length, ContentFormat format);
600
617 ErrorCode getObserveValue(uint32_t &observeValue);
618
630 bool isObserveRegister();
631
643 bool isObserveDeregister();
644
668 ErrorCode buildNotification(Observer &observer, Message &notification) const;
669 };
670
671 class Node; // Forward declaration.
672
673 namespace Detail
674 {
682 struct RetransmissionEntry
683 {
685 Coap::Message message;
690 unsigned short attempts = COAP_MAX_RETRANSMIT;
695 unsigned long timeoutBaseInterval = COAP_ACK_MAX_TIMEOUT_MS;
697 unsigned long nextAttemptDeadline = 0;
699 IPAddress ip;
701 uint16_t port = 0;
702
708 RetransmissionEntry() = default;
709
722 void set(Coap::Message message, IPAddress ip, uint16_t port);
723
732 bool isEmpty() const
733 {
734 return this->attempts >= COAP_MAX_RETRANSMIT;
735 }
736
742 unsigned long getDeadline() const
743 {
744 return this->nextAttemptDeadline;
745 }
746
752 void setAsCompleted()
753 {
754 this->attempts = COAP_MAX_RETRANSMIT;
755 }
756
766 ErrorCode retransmit(UDP *udp);
767 };
768
774 class RetransmissionQueue
775 {
776 private:
780 RetransmissionEntry _entries[COAP_CONFIRMABLE_MESSAGE_QUEUE_SIZE];
781
782 public:
783 RetransmissionQueue() = default;
784
796 ErrorCode add(const Coap::Message &message, IPAddress ip, uint16_t port);
797
806 ErrorCode process(UDP *udp);
807
823 void matchResponse(const Coap::Message &response);
824 };
825 }
826
833 class Node
834 {
835 private:
837 UDP *_udp;
839 uint16_t _port;
841 Callback _responseHandler;
843 Detail::UriRegistry _serverRegistry;
845 Detail::RetransmissionQueue _retransmissionQueue;
846
847 public:
856
863 Node(UDP &udp, uint16_t port) : _udp(&udp), _port(port), _responseHandler(nullptr), _retransmissionQueue() {}
864
869 uint16_t getPort() const
870 {
871 return this->_port;
872 }
873
883
898 void setResponseHandler(Callback handler) { _responseHandler = handler; }
899
920 ErrorCode serve(const char *path, Callback callback);
921
932 ErrorCode loop();
933
947 ErrorCode sendMessage(const Message &message, IPAddress ip, uint16_t port);
948 };
949
950} // End of namespace Coap
951
952#endif
This header file contains detail functions, not meant for public use.
Utility functions for the ProsecCoAP library.
A CoAP message.
Definition ProsecCoAP.h:164
uint16_t getId() const
Get the message ID.
Definition ProsecCoAP.cpp:159
void setCode(MessageCode code)
Set the message code.
Definition ProsecCoAP.cpp:238
MessageCode getCode() const
Get the message code.
Definition ProsecCoAP.cpp:243
ErrorCode getObserveValue(uint32_t &observeValue)
Extract the Observe option value, if present.
Definition ProsecCoAP.cpp:893
MessageType getType() const
Get the message type.
Definition ProsecCoAP.cpp:231
ErrorCode getPath(String *path) const
Retrieve all the URI path and URI query options from the message and concatenate them into a single s...
Definition ProsecCoAP.cpp:952
size_t getLength() const
Get the message length.
Definition ProsecCoAP.h:323
ErrorCode getPayload(const uint8_t *&payload, size_t &length) const
Get the payload from the message.
Definition ProsecCoAP.cpp:764
size_t getTokenLength() const
Get the current token length.
Definition ProsecCoAP.cpp:248
static ErrorCode fromUdp(UDP *udp, Message &message)
Build a CoAP message reading from a UDP instance.
Definition ProsecCoAP.cpp:53
uint8_t getVersion() const
Get the CoAP version of this message.
Definition ProsecCoAP.h:314
ErrorCode addPort(uint16_t port)
Add the Uri-Port option to the message.
Definition ProsecCoAP.cpp:570
ErrorCode addPayload(const uint8_t *payload, size_t length)
Add a payload to the message.
Definition ProsecCoAP.cpp:802
ErrorCode addOption(Option option)
Add an option to the message.
Definition ProsecCoAP.h:476
ErrorCode addOption(OptionNumber number, const uint8_t *value, size_t length)
Add an option to the message.
Definition ProsecCoAP.cpp:312
OptionIterator getOptionIterator() const
Return an iterator over the message options.
Definition ProsecCoAP.cpp:551
Message()
Builds a default CoAP message.
Definition ProsecCoAP.h:235
const uint8_t * asRaw() const
Get the raw binary representation of the message.
Definition ProsecCoAP.h:268
ErrorCode buildResponse(MessageCode code, Message &response) const
Build a response message based on a request message.
Definition ProsecCoAP.cpp:86
const uint8_t * getToken() const
Get the pointer to the current token.
Definition ProsecCoAP.cpp:294
ErrorCode addPath(const char *path)
Add the URI path and query to the message.
Definition ProsecCoAP.cpp:588
bool isObserveDeregister()
Check if the message is an Observe deregister GET request.
Definition ProsecCoAP.cpp:938
ErrorCode buildNotification(Observer &observer, Message &notification) const
Build a notification message for the given observer.
Definition ProsecCoAP.cpp:109
ErrorCode setToken(const uint8_t *token, size_t length)
Sets the token of the message with the given value and length.
Definition ProsecCoAP.cpp:254
bool isObserveRegister()
Check if the message is an Observe register GET request.
Definition ProsecCoAP.cpp:924
ErrorCode addRandomToken(size_t length)
Add a token of the given length to the message.
Definition ProsecCoAP.cpp:278
bool matchesToken(const uint8_t *token, size_t length) const
Check if the message token matches the given token and length.
Definition ProsecCoAP.cpp:301
ErrorCode addHost(IPAddress ip)
Add the Uri-Host option to the message.
Definition ProsecCoAP.cpp:557
void setType(MessageType type)
Set the message type.
Definition ProsecCoAP.cpp:223
Message(MessageType type, MessageCode code)
Builds a CoAP message with the given type and code.
Definition ProsecCoAP.h:247
The CoAP node that runs on this device.
Definition ProsecCoAP.h:834
Node(UDP &udp, uint16_t port)
Build a CoAP node using the given UDP instance and port.
Definition ProsecCoAP.h:863
ErrorCode loop()
Perform a single iteration of the CoAP event loop.
Definition ProsecCoAP.cpp:1094
ErrorCode start()
Start the CoAP instance.
Definition ProsecCoAP.cpp:1081
void setResponseHandler(Callback handler)
Set the response callback.
Definition ProsecCoAP.h:898
uint16_t getPort() const
Get the local port used by this CoAP node.
Definition ProsecCoAP.h:869
ErrorCode sendMessage(const Message &message, IPAddress ip, uint16_t port)
Send a CoAP message to the specified IP address and port.
Definition ProsecCoAP.cpp:1195
ErrorCode serve(const char *path, Callback callback)
Serve a given URI path with the specified callback.
Definition ProsecCoAP.cpp:1076
Node(UDP &udp)
Build a CoAP node using the given UDP instance.
Definition ProsecCoAP.h:855
A remote CoAP observer, actively observing a resource.
Definition Observers.h:23
The option iterator.
Definition ProsecCoAP.h:98
OptionIterator(const Message &message)
Initialize the option iterator for the given message.
Definition ProsecCoAP.cpp:544
ErrorCode next(Option &option)
Get the next option in the message.
Definition ProsecCoAP.cpp:662
#define COAP_ACK_MAX_TIMEOUT_MS
The precomputed maximum ACK timeout derived from the minimum timeout and the random factor.
Definition Definitions.h:85
#define COAP_MAX_MESSAGE_SIZE
Maximum size of a CoAP message in bytes.
Definition Definitions.h:46
#define COAP_MAX_RETRANSMIT
The maximum number of retransmission attempts for confirmable messages. Default to 4 as per RFC 7252,...
Definition Definitions.h:92
#define COAP_CONFIRMABLE_MESSAGE_QUEUE_SIZE
The maximum number of confirmable messages that are stored for retransmission.
Definition Definitions.h:104
ErrorCode getRandomToken(size_t length, uint8_t *buffer)
Generate a random token of the given length.
Definition ProsecCoAP.cpp:8
constexpr uint16_t COAP_DEFAULT_PORT
The default CoAP port number.
Definition Definitions.h:139
Namespace for the library.
Definition Definitions.h:155
void(* Callback)(Message &message, IPAddress ip, uint16_t port)
Callback function type for handling incoming messages.
Definition Definitions.h:355
ErrorCode
Error codes used in the library.
Definition Definitions.h:321
MessageType
The CoAP message type.
Definition Definitions.h:164
OptionNumber
Definition Definitions.h:255
MessageCode
The CoAP code.
Definition Definitions.h:186
ContentFormat
The CoAP content format.
Definition Definitions.h:286
WiFiUDP udp
Definition serverEsp32.ino:17
IPAddress ip(192, 168, 0, DEVICE_ID)
A CoAP option.
Definition ProsecCoAP.h:62
Option(OptionNumber number, const uint8_t *value, size_t length)
Build a CoAP option.
Definition ProsecCoAP.h:87
size_t length
Length of the option value in bytes.
Definition ProsecCoAP.h:78
OptionNumber number
The option number.
Definition ProsecCoAP.h:68
const uint8_t * value
Pointer to the option value.
Definition ProsecCoAP.h:74
Option()
Definition ProsecCoAP.h:80