ProsecCoAP 🥂
 
Loading...
Searching...
No Matches
Observers.h
Go to the documentation of this file.
1
8#ifndef OBSERVERS_H_INCLUDED
9#define OBSERVERS_H_INCLUDED
10
11#include <Arduino.h>
12#include "../Definitions.h"
13
14namespace Coap
15{
16
23 {
24 private:
28 bool _active : 8;
29
33 IPAddress _ip;
37 uint16_t _port = 0;
44 uint8_t _token[COAP_MAX_TOKEN_LENGTH] = {0};
51 uint8_t _tokenLength = 0;
52
53 public:
57 Observer() : _active(false) {}
58
67 Observer(IPAddress ip, uint16_t port, const uint8_t *token, uint8_t tokenLength)
68 : _active(true), _ip(ip), _port(port), _tokenLength(tokenLength)
69 {
70 // Copy the token value, ensuring that we do not exceed the maximum token length.
71 memcpy(this->_token, token, tokenLength > COAP_MAX_TOKEN_LENGTH ? COAP_MAX_TOKEN_LENGTH : tokenLength);
72 }
73
78 bool isActive() const
79 {
80 return this->_active;
81 }
82
87 void setActive(bool active)
88 {
89 this->_active = active;
90 }
91
96 IPAddress getIp() const
97 {
98 return this->_ip;
99 }
100
105 uint16_t getPort() const
106 {
107 return this->_port;
108 }
109
114 const uint8_t *getToken() const
115 {
116 return this->_token;
117 }
118
123 uint8_t getTokenLength() const
124 {
125 return this->_tokenLength;
126 }
127
141 {
142 unsigned long currentTime = millis();
143 return currentTime & 0xFFFFFF; // Return only the least significant 24 bits.
144 }
145 };
146
161 template <size_t N>
163 {
164 private:
168 Observer _observers[N];
169
170 public:
176 Observer &operator[](size_t index)
177 {
178 return _observers[index];
179 }
180
186 const Observer &operator[](size_t index) const
187 {
188 return _observers[index];
189 }
190
196 size_t length() const
197 {
198 return N;
199 }
200
215 ErrorCode add(IPAddress ip, uint16_t port, const uint8_t *token, uint8_t tokenLength)
216 {
217 // We MUST check if the observer is already present before trying to add it, to avoid duplicates.
218 for (size_t i = 0; i < N; i++)
219 {
220 if (!this->_observers[i].isActive())
221 continue; // Skip inactive observers.
222
223 // An observer is considered the same if it matches the combination of IP address, port, token and token length.
224 if (this->_observers[i].getIp() == ip &&
225 this->_observers[i].getPort() == port &&
226 this->_observers[i].getTokenLength() == tokenLength &&
227 memcmp(this->_observers[i].getToken(), token, tokenLength) == 0)
228 {
229 // Matching active observer found. No need to add again.
230 return ErrorCode::OK;
231 }
232 }
233
234 // New observer.
235 // Find the first inactive observer slot and add the new observer there.
236 for (size_t i = 0; i < N; i++)
237 {
238 if (!this->_observers[i].isActive())
239 {
240 // Inactive slot found. Add the new observer here (active by default, see constructor).
241 this->_observers[i] = Observer(ip, port, token, tokenLength);
242 return ErrorCode::OK;
243 }
244 }
245
247 }
248
262 ErrorCode remove(IPAddress ip, uint16_t port, const uint8_t *token, uint8_t tokenLength)
263 {
264 // Find the observer matching the given parameters and remove it.
265 for (size_t i = 0; i < N; i++)
266 {
267 if (!this->_observers[i].isActive())
268 continue; // Skip inactive observers.
269 if (this->_observers[i].getIp() == ip &&
270 this->_observers[i].getPort() == port &&
271 this->_observers[i].getTokenLength() == tokenLength &&
272 memcmp(this->_observers[i].getToken(), token, tokenLength) == 0)
273 {
274 // Matching observer found. Remove it by marking it as inactive.
275 this->_observers[i].setActive(false);
276 return ErrorCode::OK;
277 }
278 }
280 }
281
290 ErrorCode remove(const Observer &observer)
291 {
292 return this->remove(observer.getIp(), observer.getPort(), observer.getToken(), observer.getTokenLength());
293 }
294 };
295}
296
297#endif // OBSERVERS_H_INCLUDED
A resource observer registry.
Definition Observers.h:163
ErrorCode remove(const Observer &observer)
Remove an observer from the registry.
Definition Observers.h:290
size_t length() const
Get the maximum number of observers that can be stored in the registry.
Definition Observers.h:196
ErrorCode remove(IPAddress ip, uint16_t port, const uint8_t *token, uint8_t tokenLength)
Remove an observer from the registry.
Definition Observers.h:262
ErrorCode add(IPAddress ip, uint16_t port, const uint8_t *token, uint8_t tokenLength)
Add a new observer to the registry.
Definition Observers.h:215
Observer & operator[](size_t index)
Get the observer at the given index.
Definition Observers.h:176
const Observer & operator[](size_t index) const
Get the observer at the given index.
Definition Observers.h:186
A remote CoAP observer, actively observing a resource.
Definition Observers.h:23
uint8_t getTokenLength() const
Get the token length used by the observer.
Definition Observers.h:123
Observer()
Default constructor that creates an inactive observer.
Definition Observers.h:57
const uint8_t * getToken() const
Get the token pointer used by the observer.
Definition Observers.h:114
uint16_t getPort() const
Get the port of the observer.
Definition Observers.h:105
Observer(IPAddress ip, uint16_t port, const uint8_t *token, uint8_t tokenLength)
Constructor that creates an active observer with the given parameters.
Definition Observers.h:67
void setActive(bool active)
Set the observer as active or inactive.
Definition Observers.h:87
bool isActive() const
Check if the observer is currently active.
Definition Observers.h:78
IPAddress getIp() const
Get the IP address of the observer.
Definition Observers.h:96
uint32_t getNextSequentialNumber()
The sequential number for notifications, as per specifications.
Definition Observers.h:140
constexpr uint8_t COAP_MAX_TOKEN_LENGTH
The maximum length of a CoAP token.
Definition Definitions.h:149
Namespace for the library.
Definition Definitions.h:155
ErrorCode
Error codes used in the library.
Definition Definitions.h:321
IPAddress ip(192, 168, 0, DEVICE_ID)