ThingsBoard Client SDK 0.16.0
Client SDK to connect with ThingsBoard IoT Platform from IoT devices (Arduino, Espressif, etc.)
Loading...
Searching...
No Matches
Helper.h
Go to the documentation of this file.
1#ifndef Helper_h
2#define Helper_h
3
4// Local includes.
5#include "Configuration.h"
6
7// Library include.
8#include <ArduinoJson.h>
9#if THINGSBOARD_ENABLE_STL
10#include <iterator>
11#endif // THINGSBOARD_ENABLE_STL
12#include <stdint.h>
13#include <assert.h>
14#include <stdarg.h>
15#include <stdio.h>
16
17
19class Helper {
20 public:
31 template<typename... Args>
32 static size_t Calculate_Print_Size(char const * format, Args const &... args) {
33 const int result = snprintf(nullptr, 0U, format, args...) + 1U;
34 assert(result > 0);
35 return static_cast<size_t>(result);
36 }
37
45 static size_t Calculate_Symbol_Occurences(uint8_t const * bytes, char symbol, uint32_t length);
46
51 static bool String_IsNull_Or_Empty(char const * str);
52
63 static size_t Split_Topic_Into_Request_ID(char const * received_topic, size_t const & end_position);
64
72 template <typename TSource>
73 static size_t Measure_Json(TSource const & source) {
74 return measureJson(source) + 1U;
75 }
76
84 template<typename InputIterator>
85 static size_t distance(InputIterator const & first, InputIterator const & last) {
86#if THINGSBOARD_ENABLE_STL
87 return std::distance(first, last);
88#else
89 // Subtracting last by the first is only a valid way to calculate the distance if we can guarantee that the given iterators are random access,
90 // to keep compatibility with code that supports the STL we allow InputIterators, therefore we have to implement the size calculation the more inneficient O(n) way instead.
91 // This allows the edge case where an end-user uses this method themselves in the code with their own implemented list data type.
92 size_t size = 0U;
93 for (auto it = first; it != last; ++it, ++size) {}
94 return size;
95#endif // THINGSBOARD_ENABLE_STL
96 }
97};
98
99#endif // Helper
Static helper class that includes some functionalities used in multiple places throughout the library...
Definition: Helper.h:19
static size_t Calculate_Print_Size(char const *format, Args const &... args)
Returns the total amount of bytes needed to store the formatted string with null termination,...
Definition: Helper.h:32
static size_t distance(InputIterator const &first, InputIterator const &last)
Calculates the distance between two iterators.
Definition: Helper.h:85
static bool String_IsNull_Or_Empty(char const *str)
Returns wheter the given string is either a nullptr or is an empty string, meaning it only contains a...
Definition: Helper.cpp:27
static size_t Measure_Json(TSource const &source)
Calculates the total size of the string the serializeJson method would produce including the null end...
Definition: Helper.h:73
static size_t Split_Topic_Into_Request_ID(char const *received_topic, size_t const &end_position)
Splits the topic at the given position and extracts the request id parameter from the remaining strin...
Definition: Helper.cpp:31
static size_t Calculate_Symbol_Occurences(uint8_t const *bytes, char symbol, uint32_t length)
Returns the amount of occurences of the given smybol in the given byte payload.
Definition: Helper.cpp:10