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 "Definitions.h"
12
13namespace Coap
14{
21 {
22 private:
26 bool _active : 8;
27
31 IPAddress _ip;
35 uint16_t _port = 0;
42 uint8_t _token[COAP_MAX_TOKEN_LENGTH] = {0};
49 uint8_t _tokenLength = 0;
53 unsigned long _lastSeen = 0;
54
55 public:
59 Observer() : _active(false) {}
60
69 Observer(IPAddress ip, uint16_t port, const uint8_t *token, uint8_t tokenLength)
70 : _ip(ip), _port(port), _tokenLength(tokenLength)
71 {
72 // Copy the token value, ensuring that we do not exceed the maximum token length.
73 memcpy(this->_token, token, tokenLength > COAP_MAX_TOKEN_LENGTH ? COAP_MAX_TOKEN_LENGTH : tokenLength);
74 this->activate();
75 }
76
83 bool isActive() const
84 {
85 return this->_active;
86 }
87
95 bool isStale() const;
96
103 void activate();
104
110 void deactivate();
111
116 IPAddress getIp() const
117 {
118 return this->_ip;
119 }
120
125 uint16_t getPort() const
126 {
127 return this->_port;
128 }
129
134 const uint8_t *getToken() const
135 {
136 return this->_token;
137 }
138
143 uint8_t getTokenLength() const
144 {
145 return this->_tokenLength;
146 }
147
160 uint32_t getNextSequentialNumber();
161 };
162
175 template <size_t N>
177 {
178 // Ensure N is at least 1.
179 static_assert(N >= 1, "ObserverRegistry Error: N must be 1 or greater!");
180
181 private:
185 Observer _observers[N];
186
187 public:
193 Observer &operator[](size_t index)
194 {
195 return _observers[index];
196 }
197
203 const Observer &operator[](size_t index) const
204 {
205 return _observers[index];
206 }
207
213 size_t length() const
214 {
215 return N;
216 }
217
223 size_t countActive() const
224 {
225 size_t count = 0;
226 for (size_t i = 0; i < N; i++)
227 {
228 if (this->_observers[i].isActive())
229 count++;
230 }
231 return count;
232 }
233
253 ErrorCode add(IPAddress ip, uint16_t port, const uint8_t *token, uint8_t tokenLength)
254 {
255 Observer new_observer(ip, port, token, tokenLength);
256 return this->add(new_observer);
257 }
258
271 {
272 if (!observer.isActive())
273 {
274 // Inactive observer cannot be added to the registry.
276 }
277
278 // We MUST check if the observer is already present before trying to add it, to avoid duplicates.
279 for (size_t i = 0; i < N; i++)
280 {
281 if (!this->_observers[i].isActive())
282 continue; // Skip inactive observers.
283
284 // An observer is considered the same if it matches the combination of IP address, port, token and token length.
285 if (this->_observers[i].getIp() == observer.getIp() &&
286 this->_observers[i].getPort() == observer.getPort() &&
287 this->_observers[i].getTokenLength() == observer.getTokenLength() &&
288 memcmp(this->_observers[i].getToken(), observer.getToken(), observer.getTokenLength()) == 0)
289 {
290 // Matching active observer found. No need to add it again.
291 return ErrorCode::OK;
292 }
293 }
294
295 // New observer.
296 // Find the first inactive observer slot and add the new observer there.
297 for (size_t i = 0; i < N; i++)
298 {
299 if (!this->_observers[i].isActive())
300 {
301 // Inactive slot found. Add the new observer here.
302 this->_observers[i] = observer;
303 return ErrorCode::OK;
304 }
305 }
306 // Could not find an inactive slot, the registry is full.
308 }
309
323 ErrorCode remove(IPAddress ip, uint16_t port, const uint8_t *token, uint8_t tokenLength)
324 {
325 // Find the observer matching the given parameters and remove it.
326 for (size_t i = 0; i < N; i++)
327 {
328 if (!this->_observers[i].isActive())
329 continue; // Skip inactive observers.
330 if (this->_observers[i].getIp() == ip &&
331 this->_observers[i].getPort() == port &&
332 this->_observers[i].getTokenLength() == tokenLength &&
333 memcmp(this->_observers[i].getToken(), token, tokenLength) == 0)
334 {
335 // Matching observer found. Remove it by marking it as inactive.
336 this->_observers[i].deactivate();
337 return ErrorCode::OK;
338 }
339 }
341 }
342
354 ErrorCode remove(IPAddress ip, uint16_t port)
355 {
357 // Find the observer matching the given parameters and remove it.
358 for (size_t i = 0; i < N; i++)
359 {
360 if (!this->_observers[i].isActive())
361 continue; // Skip inactive observers.
362 if (this->_observers[i].getIp() == ip &&
363 this->_observers[i].getPort() == port)
364 {
365 // Matching observer found. Remove it by marking it as inactive.
366 this->_observers[i].deactivate();
367 result = ErrorCode::OK;
368 }
369 }
370 return result;
371 }
372
383 ErrorCode remove(IPAddress ip)
384 {
386 // Find the observer matching the given parameters and remove it.
387 for (size_t i = 0; i < N; i++)
388 {
389 if (!this->_observers[i].isActive())
390 continue; // Skip inactive observers.
391 if (this->_observers[i].getIp() == ip)
392 {
393 // Matching observer found. Remove it by marking it as inactive.
394 this->_observers[i].deactivate();
395 result = ErrorCode::OK;
396 }
397 }
398 return result;
399 }
400
409 ErrorCode remove(const Observer &observer)
410 {
411 return this->remove(observer.getIp(), observer.getPort(), observer.getToken(), observer.getTokenLength());
412 }
413
420 {
421 for (size_t i = 0; i < N; i++)
422 {
423 if (this->_observers[i].isActive() && this->_observers[i].isStale())
424 {
425 this->_observers[i].deactivate();
426 }
427 }
428 return ErrorCode::OK;
429 }
430 };
431}
432
433#endif // OBSERVERS_H_INCLUDED
Shared definitions for the library.
A resource observer registry.
Definition Observers.h:177
ErrorCode remove(const Observer &observer)
Remove an observer from the registry.
Definition Observers.h:409
size_t countActive() const
Get the number of active observers currently stored in the registry.
Definition Observers.h:223
size_t length() const
Get the maximum number of observers that can be stored in the registry.
Definition Observers.h:213
ErrorCode remove(IPAddress ip, uint16_t port, const uint8_t *token, uint8_t tokenLength)
Remove an observer from the registry.
Definition Observers.h:323
ErrorCode add(IPAddress ip, uint16_t port, const uint8_t *token, uint8_t tokenLength)
Add a new active observer to the registry.
Definition Observers.h:253
ErrorCode remove(IPAddress ip, uint16_t port)
Remove any observers with the given combination of IP and port.
Definition Observers.h:354
ErrorCode remove(IPAddress ip)
Remove any observers with the given IP.
Definition Observers.h:383
ErrorCode add(Observer observer)
Add a new observer to the registry.
Definition Observers.h:270
Observer & operator[](size_t index)
Get the observer at the given index.
Definition Observers.h:193
ErrorCode removeStale()
Removes all stale observers from the registry.
Definition Observers.h:419
const Observer & operator[](size_t index) const
Get the observer at the given index.
Definition Observers.h:203
A remote CoAP observer.
Definition Observers.h:21
uint8_t getTokenLength() const
Get the token length used by the observer.
Definition Observers.h:143
Observer()
Default constructor that creates an inactive observer.
Definition Observers.h:59
const uint8_t * getToken() const
Get the token pointer used by the observer.
Definition Observers.h:134
void activate()
Set the observer as active.
Definition Observers.cpp:12
uint16_t getPort() const
Get the port of the observer.
Definition Observers.h:125
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:69
bool isActive() const
Check if the observer is currently active.
Definition Observers.h:83
IPAddress getIp() const
Get the IP address of the observer.
Definition Observers.h:116
bool isStale() const
Check if the observer is stale.
Definition Observers.cpp:24
uint32_t getNextSequentialNumber()
The sequential number for notifications, as per specifications.
Definition Observers.cpp:6
void deactivate()
Set the observer as deactivated.
Definition Observers.cpp:19
constexpr uint8_t COAP_MAX_TOKEN_LENGTH
The maximum length of a CoAP token.
Definition Definitions.h:155
Namespace for the library.
Definition Definitions.h:161
ErrorCode
Error codes used in the library.
Definition Definitions.h:327
IPAddress ip(192, 168, 0, DEVICE_ID)