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 internal implementation details.
14#include "detail/Detail.h"
15
16// Include Observers management functionality.
17#include "observers/Observers.h"
18
25namespace Coap
26{
27
28 // SECTION Functions.
48 ErrorCode getRandomToken(size_t length, uint8_t *buffer);
49
50 // End of Functions group
51 // !SECTION End of Functions.
52
58 struct Option
59 {
71 const uint8_t *value;
75 size_t length;
76
77 Option() : number(static_cast<OptionNumber>(0)), value(nullptr), length(0) {}
84 Option(OptionNumber number, const uint8_t *value, size_t length)
86 };
87
95 {
96
97 // Give access to private members to Message methods.
98 friend class Message;
102 const Message &_message;
108 size_t _currentByte;
112 uint16_t _currentOptionNumber;
113
114 public:
123 OptionIterator(const Message &message);
124
146 ErrorCode next(Option &option);
147 };
148
161 {
162 private:
166 uint8_t _message[COAP_MAX_MESSAGE_SIZE];
173 size_t _messageLength;
188 ErrorCode _insert(size_t startPosition, const uint8_t *data, size_t length);
189
201 ErrorCode _remove(size_t startPosition, size_t length);
202
211 static uint16_t _getNextId();
212
220 void _setId(uint16_t id);
221
222 public:
233
244 Message(MessageType type, MessageCode code) : Message(type, code, _getNextId()) {}
245
256 Message(MessageType type, MessageCode code, uint16_t id);
257
265 const uint8_t *asRaw() const
266 {
267 return this->_message;
268 }
269
284 static ErrorCode fromUdp(UDP *udp, Message &message);
285
301 ErrorCode buildResponse(MessageCode code, Message &response) const;
302
311 uint8_t getVersion() const
312 {
313 return (this->_message[0] >> 6) & 0x03;
314 }
315
320 size_t getLength() const
321 {
322 return this->_messageLength;
323 }
324
330 void setType(MessageType type);
331
337 MessageType getType() const;
338
344 void setCode(MessageCode code);
350 MessageCode getCode() const;
351
359 uint16_t getId() const;
360
365 size_t getTokenLength() const;
366
377 ErrorCode setToken(const uint8_t *token, size_t length);
378
399 ErrorCode addRandomToken(size_t length);
400
419 const uint8_t *getToken() const;
420
429 bool matchesToken(const uint8_t *token, size_t length) const;
430
467 ErrorCode addOption(OptionNumber number, const uint8_t *value, size_t length);
468
474 {
475 return this->addOption(option.number, option.value, option.length);
476 };
477
491 ErrorCode addHost(IPAddress ip);
492
505 ErrorCode addPort(uint16_t port);
506
528 ErrorCode addPath(const char *path);
529
545 ErrorCode getPath(String *path) const;
546
566 ErrorCode getPayload(const uint8_t *&payload, size_t &length) const;
567
581 ErrorCode addPayload(const uint8_t *payload, size_t length);
582
596 ErrorCode addPayload(const uint8_t *payload, size_t length, ContentFormat format);
597
614 ErrorCode getObserveValue(uint32_t &observeValue);
615
627 bool isObserveRegister();
628
640 bool isObserveDeregister();
641
665 ErrorCode buildNotification(Observer &observer, Message &notification) const;
666 };
667
668 class Node; // Forward declaration.
669
670 namespace Detail
671 {
679 struct RetransmissionEntry
680 {
682 Coap::Message message;
687 unsigned short attempts = COAP_MAX_RETRANSMIT;
692 unsigned long timeoutBaseInterval = COAP_ACK_MAX_TIMEOUT_MS;
694 unsigned long nextAttemptDeadline = 0;
696 IPAddress ip;
698 uint16_t port = 0;
699
705 RetransmissionEntry() = default;
706
719 void set(Coap::Message message, IPAddress ip, uint16_t port);
720
729 bool isEmpty() const
730 {
731 return this->attempts >= COAP_MAX_RETRANSMIT;
732 }
733
739 unsigned long getDeadline() const
740 {
741 return this->nextAttemptDeadline;
742 }
743
749 void setAsCompleted()
750 {
751 this->attempts = COAP_MAX_RETRANSMIT;
752 }
753
763 ErrorCode retransmit(UDP *udp);
764 };
765
771 class RetransmissionQueue
772 {
773 private:
777 RetransmissionEntry _entries[COAP_CONFIRMABLE_MESSAGE_QUEUE_SIZE];
778
779 public:
780 RetransmissionQueue() = default;
781
793 ErrorCode add(const Coap::Message &message, IPAddress ip, uint16_t port);
794
803 ErrorCode process(UDP *udp);
804
820 void matchResponse(const Coap::Message &response);
821 };
822 }
823
830 class Node
831 {
832 private:
834 UDP *_udp;
836 uint16_t _port;
838 Callback _responseHandler;
840 Detail::UriRegistry _serverRegistry;
842 Detail::RetransmissionQueue _retransmissionQueue;
843
844 public:
853
860 Node(UDP &udp, uint16_t port) : _udp(&udp), _port(port), _responseHandler(nullptr), _retransmissionQueue() {}
861
866 uint16_t getPort() const
867 {
868 return this->_port;
869 }
870
880
895 void setResponseHandler(Callback handler) { _responseHandler = handler; }
896
917 ErrorCode serve(const char *path, Callback callback);
918
929 ErrorCode loop();
930
944 ErrorCode sendMessage(const Message &message, IPAddress ip, uint16_t port);
945 };
946
947} // End of namespace Coap
948
949#endif
This header file contains detail functions, not meant for public use.
A CoAP message.
Definition ProsecCoAP.h:161
uint16_t getId() const
Get the message ID.
Definition ProsecCoAP.cpp:147
void setCode(MessageCode code)
Set the message code.
Definition ProsecCoAP.cpp:226
MessageCode getCode() const
Get the message code.
Definition ProsecCoAP.cpp:231
ErrorCode getObserveValue(uint32_t &observeValue)
Extract the Observe option value, if present.
Definition ProsecCoAP.cpp:868
MessageType getType() const
Get the message type.
Definition ProsecCoAP.cpp:219
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:925
size_t getLength() const
Get the message length.
Definition ProsecCoAP.h:320
ErrorCode getPayload(const uint8_t *&payload, size_t &length) const
Get the payload from the message.
Definition ProsecCoAP.cpp:747
size_t getTokenLength() const
Get the current token length.
Definition ProsecCoAP.cpp:236
static ErrorCode fromUdp(UDP *udp, Message &message)
Build a CoAP message reading from a UDP instance.
Definition ProsecCoAP.cpp:44
uint8_t getVersion() const
Get the CoAP version of this message.
Definition ProsecCoAP.h:311
ErrorCode addPort(uint16_t port)
Add the Uri-Port option to the message.
Definition ProsecCoAP.cpp:558
ErrorCode addPayload(const uint8_t *payload, size_t length)
Add a payload to the message.
Definition ProsecCoAP.cpp:785
ErrorCode addOption(Option option)
Add an option to the message.
Definition ProsecCoAP.h:473
ErrorCode addOption(OptionNumber number, const uint8_t *value, size_t length)
Add an option to the message.
Definition ProsecCoAP.cpp:300
OptionIterator getOptionIterator() const
Return an iterator over the message options.
Definition ProsecCoAP.cpp:539
Message()
Builds a default CoAP message.
Definition ProsecCoAP.h:232
const uint8_t * asRaw() const
Get the raw binary representation of the message.
Definition ProsecCoAP.h:265
ErrorCode buildResponse(MessageCode code, Message &response) const
Build a response message based on a request message.
Definition ProsecCoAP.cpp:77
const uint8_t * getToken() const
Get the pointer to the current token.
Definition ProsecCoAP.cpp:282
ErrorCode addPath(const char *path)
Add the URI path and query to the message.
Definition ProsecCoAP.cpp:571
bool isObserveDeregister()
Check if the message is an Observe deregister GET request.
Definition ProsecCoAP.cpp:911
ErrorCode buildNotification(Observer &observer, Message &notification) const
Build a notification message for the given observer.
Definition ProsecCoAP.cpp:100
ErrorCode setToken(const uint8_t *token, size_t length)
Sets the token of the message with the given value and length.
Definition ProsecCoAP.cpp:242
bool isObserveRegister()
Check if the message is an Observe register GET request.
Definition ProsecCoAP.cpp:897
ErrorCode addRandomToken(size_t length)
Add a token of the given length to the message.
Definition ProsecCoAP.cpp:266
bool matchesToken(const uint8_t *token, size_t length) const
Check if the message token matches the given token and length.
Definition ProsecCoAP.cpp:289
ErrorCode addHost(IPAddress ip)
Add the Uri-Host option to the message.
Definition ProsecCoAP.cpp:545
void setType(MessageType type)
Set the message type.
Definition ProsecCoAP.cpp:211
Message(MessageType type, MessageCode code)
Builds a CoAP message with the given type and code.
Definition ProsecCoAP.h:244
The CoAP node that runs on this device.
Definition ProsecCoAP.h:831
Node(UDP &udp, uint16_t port)
Build a CoAP node using the given UDP instance and port.
Definition ProsecCoAP.h:860
ErrorCode loop()
Perform a single iteration of the CoAP event loop.
Definition ProsecCoAP.cpp:1067
ErrorCode start()
Start the CoAP instance.
Definition ProsecCoAP.cpp:1054
void setResponseHandler(Callback handler)
Set the response callback.
Definition ProsecCoAP.h:895
uint16_t getPort() const
Get the local port used by this CoAP node.
Definition ProsecCoAP.h:866
ErrorCode sendMessage(const Message &message, IPAddress ip, uint16_t port)
Send a CoAP message to the specified IP address and port.
Definition ProsecCoAP.cpp:1168
ErrorCode serve(const char *path, Callback callback)
Serve a given URI path with the specified callback.
Definition ProsecCoAP.cpp:1049
Node(UDP &udp)
Build a CoAP node using the given UDP instance.
Definition ProsecCoAP.h:852
A remote CoAP observer, actively observing a resource.
Definition Observers.h:23
The option iterator.
Definition ProsecCoAP.h:95
OptionIterator(const Message &message)
Initialize the option iterator for the given message.
Definition ProsecCoAP.cpp:532
ErrorCode next(Option &option)
Get the next option in the message.
Definition ProsecCoAP.cpp:645
#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:7
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:59
Option(OptionNumber number, const uint8_t *value, size_t length)
Build a CoAP option.
Definition ProsecCoAP.h:84
size_t length
Length of the option value in bytes.
Definition ProsecCoAP.h:75
OptionNumber number
The option number.
Definition ProsecCoAP.h:65
const uint8_t * value
Pointer to the option value.
Definition ProsecCoAP.h:71
Option()
Definition ProsecCoAP.h:77