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.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
216 public:
224 void setId(uint16_t id);
225
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
303 ErrorCode intoResponse(const Message &request, MessageCode code, MessageType type);
304
314 {
315 return intoResponse(request, code, MessageType::ACK);
316 }
317
326 uint8_t getVersion() const
327 {
328 return (this->_message[0] >> 6) & 0x03;
329 }
330
335 size_t getLength() const
336 {
337 return this->_messageLength;
338 }
339
345 void setType(MessageType type);
346
352 MessageType getType() const;
353
359 void setCode(MessageCode code);
365 MessageCode getCode() const;
366
377 uint16_t getId() const;
378
383 size_t getTokenLength() const;
384
398 ErrorCode setToken(const uint8_t *token, size_t length);
399
420 ErrorCode addRandomToken(size_t length);
421
440 const uint8_t *getToken() const;
441
450 bool matchesToken(const uint8_t *token, size_t length) const;
451
457
475 ErrorCode getOption(OptionNumber number, Option &option) const;
476
506 ErrorCode addOption(OptionNumber number, const uint8_t *value, size_t length);
507
513 {
514 return this->addOption(option.number, option.value, option.length);
515 }
516
530 ErrorCode addHost(IPAddress ip);
531
544 ErrorCode addPort(uint16_t port);
545
567 ErrorCode addPath(const char *path);
568
584 ErrorCode getPath(String &path) const;
585
605 ErrorCode getPayload(const uint8_t *&payload, size_t &length) const;
606
622 ErrorCode addPayload(const uint8_t *payload, size_t length);
623
637 ErrorCode addPayload(const uint8_t *payload, size_t length, ContentFormat format);
638
655 ErrorCode getObserveValue(uint32_t &observeValue);
656
675 ErrorCode getMaxAge(uint32_t &age);
676
688 ErrorCode setMaxAge(uint32_t age);
689
701 bool isObserveRegister();
702
714 bool isObserveDeregister();
715
738 };
739
740 class Node; // Forward declaration.
741
742 namespace Detail
743 {
751 struct RetransmissionEntry
752 {
754 Coap::Message message;
759 unsigned short attempts = COAP_MAX_RETRANSMIT;
764 unsigned long timeoutBaseInterval = COAP_ACK_MAX_TIMEOUT_MS;
766 unsigned long nextAttemptDeadline = 0;
768 IPAddress ip;
770 uint16_t port = 0;
771
777 RetransmissionEntry() = default;
778
791 void set(Coap::Message message, IPAddress ip, uint16_t port);
792
801 bool isEmpty() const
802 {
803 return this->attempts >= COAP_MAX_RETRANSMIT;
804 }
805
811 unsigned long getDeadline() const
812 {
813 return this->nextAttemptDeadline;
814 }
815
821 void setAsCompleted()
822 {
823 this->attempts = COAP_MAX_RETRANSMIT;
824 }
825
835 ErrorCode retransmit(UDP *udp);
836 };
837
843 class RetransmissionQueue
844 {
845 private:
849 RetransmissionEntry _entries[COAP_CONFIRMABLE_MESSAGE_QUEUE_SIZE];
850
851 public:
852 RetransmissionQueue() = default;
853
865 ErrorCode add(const Coap::Message &message, IPAddress ip, uint16_t port);
866
875 ErrorCode process(UDP *udp);
876
892 void matchResponse(const Coap::Message &response);
893 };
894 }
895
902 class Node
903 {
904 private:
906 UDP *_udp;
908 uint16_t _port;
910 Callback _responseHandler;
912 Detail::UriRegistry _serverRegistry;
914 Detail::RetransmissionQueue _retransmissionQueue;
915
916 public:
925
932 Node(UDP &udp, uint16_t port) : _udp(&udp), _port(port), _responseHandler(nullptr), _retransmissionQueue() {}
933
938 uint16_t getPort() const
939 {
940 return this->_port;
941 }
942
952
967 void setResponseHandler(Callback handler) { _responseHandler = handler; }
968
989 ErrorCode serve(const char *path, Callback callback);
990
1001 ErrorCode loop();
1002
1016 ErrorCode sendMessage(const Message &message, IPAddress ip, uint16_t port);
1017 };
1018
1019} // End of namespace Coap
1020
1021#endif
Shared definitions for the library.
This header file contains internal implementation functions, not meant for external use.
Manage observers for the observe mechanism of CoAP.
Utility functions for the ProsecCoAP library.
A CoAP message.
Definition ProsecCoAP.h:164
uint16_t getId() const
Get the message ID.
Definition ProsecCoAP.cpp:146
void setCode(MessageCode code)
Set the message code.
Definition ProsecCoAP.cpp:225
MessageCode getCode() const
Get the message code.
Definition ProsecCoAP.cpp:230
ErrorCode getObserveValue(uint32_t &observeValue)
Extract the Observe option value, if present.
Definition ProsecCoAP.cpp:917
ErrorCode intoResponse(const Message &request, MessageCode code)
Convert the message into an ACK response to the given request.
Definition ProsecCoAP.h:313
MessageType getType() const
Get the message type.
Definition ProsecCoAP.cpp:218
ErrorCode setMaxAge(uint32_t age)
Add the MAX_AGE option to the message.
Definition ProsecCoAP.cpp:1009
size_t getLength() const
Get the message length.
Definition ProsecCoAP.h:335
ErrorCode getPayload(const uint8_t *&payload, size_t &length) const
Get the payload from the message.
Definition ProsecCoAP.cpp:788
size_t getTokenLength() const
Get the current token length.
Definition ProsecCoAP.cpp:235
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:326
ErrorCode addPort(uint16_t port)
Add the Uri-Port option to the message.
Definition ProsecCoAP.cpp:594
ErrorCode addPayload(const uint8_t *payload, size_t length)
Add a payload to the message.
Definition ProsecCoAP.cpp:826
ErrorCode addOption(Option option)
Add an option to the message.
Definition ProsecCoAP.h:512
ErrorCode intoResponse(const Message &request, MessageCode code, MessageType type)
Convert the message into a response to the given request.
Definition ProsecCoAP.cpp:86
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:555
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 intoNotification(Observer &observer)
Convert the message into a notification message for the given observer.
Definition ProsecCoAP.cpp:103
ErrorCode getMaxAge(uint32_t &age)
Get the MAX_AGE option value from the message.
Definition ProsecCoAP.cpp:976
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:612
bool isObserveDeregister()
Check if the message is an Observe deregister GET request.
Definition ProsecCoAP.cpp:962
ErrorCode getOption(OptionNumber number, Option &option) const
Return the first occurrence of the specified option.
Definition ProsecCoAP.cpp:561
ErrorCode setToken(const uint8_t *token, size_t length)
Sets the token of the message with the given value and length.
Definition ProsecCoAP.cpp:241
bool isObserveRegister()
Check if the message is an Observe register GET request.
Definition ProsecCoAP.cpp:948
void setId(uint16_t id)
Set the message ID.
Definition ProsecCoAP.cpp:37
ErrorCode addRandomToken(size_t length)
Add a token of the given length to the message.
Definition ProsecCoAP.cpp:266
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:1023
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:581
void setType(MessageType type)
Set the message type.
Definition ProsecCoAP.cpp:210
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:903
Node(UDP &udp, uint16_t port)
Build a CoAP node using the given UDP instance and port.
Definition ProsecCoAP.h:932
ErrorCode loop()
Perform a single iteration of the CoAP event loop.
Definition ProsecCoAP.cpp:1165
ErrorCode start()
Start the CoAP instance.
Definition ProsecCoAP.cpp:1152
void setResponseHandler(Callback handler)
Set the response callback.
Definition ProsecCoAP.h:967
uint16_t getPort() const
Get the local port used by this CoAP node.
Definition ProsecCoAP.h:938
ErrorCode sendMessage(const Message &message, IPAddress ip, uint16_t port)
Send a CoAP message to the specified IP address and port.
Definition ProsecCoAP.cpp:1278
ErrorCode serve(const char *path, Callback callback)
Serve a given URI path with the specified callback.
Definition ProsecCoAP.cpp:1142
Node(UDP &udp)
Build a CoAP node using the given UDP instance.
Definition ProsecCoAP.h:924
A remote CoAP observer.
Definition Observers.h:21
The option iterator.
Definition ProsecCoAP.h:98
OptionIterator(const Message &message)
Initialize the option iterator for the given message.
Definition ProsecCoAP.cpp:548
ErrorCode next(Option &option)
Get the next option in the message.
Definition ProsecCoAP.cpp:686
#define COAP_ACK_MAX_TIMEOUT_MS
The precomputed maximum ACK timeout derived from the minimum timeout and the random factor.
Definition Definitions.h:91
#define COAP_MAX_MESSAGE_SIZE
Maximum size of a CoAP message in bytes.
Definition Definitions.h:52
#define COAP_MAX_RETRANSMIT
The maximum number of retransmission attempts for confirmable messages. Default to 4 as per RFC 7252,...
Definition Definitions.h:98
#define COAP_CONFIRMABLE_MESSAGE_QUEUE_SIZE
The maximum number of confirmable messages that are stored for retransmission.
Definition Definitions.h:110
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:145
Namespace for the library.
Definition Definitions.h:161
void(* Callback)(Message &message, IPAddress ip, uint16_t port)
Callback function type for handling incoming messages.
Definition Definitions.h:361
ErrorCode
Error codes used in the library.
Definition Definitions.h:327
MessageType
The CoAP message type.
Definition Definitions.h:170
OptionNumber
Definition Definitions.h:261
MessageCode
The CoAP code.
Definition Definitions.h:192
ContentFormat
The CoAP content format.
Definition Definitions.h:292
WiFiUDP udp
Definition serverEsp32.ino:18
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