Uniot Core
0.9.0
Loading...
Searching...
No Matches
Common.h
Go to the documentation of this file.
1/*
2 * This is a part of the Uniot project.
3 * Copyright (C) 2016-2024 Uniot <contact@uniot.io>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#pragma once
20
21#include <memory>
22#include <utility>
23
29
38#define UNIOT_SEMVER_TO_INT(major, minor, patch) ((major * 10000) + (minor * 100) + patch)
39
48#define UNIOT_CORE_VERSION UNIOT_SEMVER_TO_INT(0, 9, 0)
49
54#ifndef UNIOT_MQTT_HOST
55#define UNIOT_MQTT_HOST "mqtt.uniot.io"
56#endif
57
62#ifndef UNIOT_MQTT_PORT
63#define UNIOT_MQTT_PORT 1883
64#endif
65
72#ifndef UNIOT_WIFI_AP_PREFIX
73#define UNIOT_WIFI_AP_PREFIX "UNIOT"
74#endif
75
84#ifndef UNIOT_WIFI_AP_PASSWORD
85#define UNIOT_WIFI_AP_PASSWORD ""
86#endif
87
103#ifndef UNIOT_WIFI_REBOOT_RESET_COUNT
104#define UNIOT_WIFI_REBOOT_RESET_COUNT 5
105#endif
106
114#ifndef UNIOT_WIFI_REBOOT_WINDOW_MS
115#define UNIOT_WIFI_REBOOT_WINDOW_MS 10000
116#endif
117
127#ifndef UNIOT_LISP_HEAP
128// Not split by ESP32 variant: object sizes are identical on both parts.
129#if defined(ESP32)
130#define UNIOT_LISP_HEAP 24576
131#elif defined(ESP8266)
132#define UNIOT_LISP_HEAP 12288
133#else
134#define UNIOT_LISP_HEAP 8192
135#endif
136#endif
137
156#ifndef UNIOT_LISP_MAX_EVAL_STACK
157#if defined(ESP8266)
158#define UNIOT_LISP_MAX_EVAL_STACK 1280
159#elif defined(CONFIG_IDF_TARGET_ESP32C3)
160#define UNIOT_LISP_MAX_EVAL_STACK 3072
161#elif defined(ESP32)
162#define UNIOT_LISP_MAX_EVAL_STACK 3072
163#else
164// An unknown platform is assumed to be a small MCU, not a host: too little heap raises a
165// clean error, too much stack resets the device, so the fallbacks lean small. A host build
166// with room to spare should set both explicitly rather than inherit these.
167#define UNIOT_LISP_MAX_EVAL_STACK 2048
168#endif
169#endif
170
179#ifndef UNIOT_WIFI_NO_SLEEP
180#define UNIOT_WIFI_NO_SLEEP 0
181#endif
182
195template <int a, int b, int c, int d>
196struct FourCC {
197 static const uint32_t Value = (((((d << 8) | c) << 8) | b) << 8) | a;
198};
199
209template <typename... Args>
210inline void UNUSED(Args &&...args) {
211 (void)(sizeof...(args));
212}
213
225inline uint32_t CRC32(const void *data, size_t length, uint32_t crc = 0) {
226 const uint8_t *ldata = (const uint8_t *)data;
227
228 crc = ~crc;
229 while (length--) {
230 crc ^= *ldata++;
231 for (uint8_t k = 0; k < 8; k++)
232 crc = crc & 1 ? (crc >> 1) ^ 0x82f63b78 : crc >> 1; // CRC-32C (iSCSI) polynomial in reversed bit order.
233 }
234 return ~crc;
235}
236
241#define COUNT_OF(arr) (sizeof(arr) / sizeof(arr[0]))
242
250#define ARRAY_ELEMENT_SAFE(arr, index) ((arr)[(((index) < COUNT_OF(arr)) ? (index) : (COUNT_OF(arr) - 1))])
251
260#define FOURCC(name) FourCC<ARRAY_ELEMENT_SAFE(#name, 0), ARRAY_ELEMENT_SAFE(#name, 1), ARRAY_ELEMENT_SAFE(#name, 2), ARRAY_ELEMENT_SAFE(#name, 3)>::Value
261
272#define ALIAS_IMPLICIT_FUNCTION(high, low) \
273 template <typename... Args> \
274 inline auto high(Args &&...args) -> decltype(low(std::forward<Args>(args)...)) { \
275 return low(std::forward<Args>(args)...); \
276 }
277
287#define ALIAS_EXPLICIT_FUNCTION(high, low) \
288 template <typename T, typename... Args> \
289 inline auto high(Args &&...args) -> decltype(low<T>(std::forward<Args>(args)...)) { \
290 return low<T>(std::forward<Args>(args)...); \
291 }
292
293namespace uniot {
298
304template <typename T>
305using UniquePointer = std::unique_ptr<T>;
306
312template <typename T>
313using SharedPointer = std::shared_ptr<T>;
314
321template <typename T_First, typename T_Second>
322using Pair = std::pair<T_First, T_Second>;
323
328
329
333
338
340} // namespace uniot
auto MakePair(Args &&...args) -> decltype(std::make_pair(std::forward< Args >(args)...))
Creates a pair instance, alias for std::make_pair.
Definition Common.h:337
std::unique_ptr< T > UniquePointer
Type alias for std::unique_ptr with cleaner syntax.
Definition Common.h:305
#define ALIAS_IMPLICIT_FUNCTION(high, low)
Creates an alias for a function that forwards all arguments implicitly.
Definition Common.h:272
std::shared_ptr< T > SharedPointer
Type alias for std::shared_ptr with cleaner syntax.
Definition Common.h:313
void UNUSED(Args &&...args)
Utility function to mark function parameters as intentionally unused.
Definition Common.h:210
std::pair< T_First, T_Second > Pair
Type alias for std::pair with cleaner syntax.
Definition Common.h:322
auto MakeUnique(Args &&...args) -> decltype(std::make_unique< T >(std::forward< Args >(args)...))
Creates a unique pointer instance, alias for std::make_unique.
Definition Common.h:332
#define ALIAS_EXPLICIT_FUNCTION(high, low)
Creates an alias for a template function that forwards all arguments explicitly.
Definition Common.h:287
uint32_t CRC32(const void *data, size_t length, uint32_t crc=0)
Calculates CRC32-C (Castagnoli) checksum for data integrity verification.
Definition Common.h:225
auto MakeShared(Args &&...args) -> decltype(std::make_shared< T >(std::forward< Args >(args)...))
Creates a shared pointer instance, alias for std::make_shared.
Definition Common.h:327
Contains all classes and functions related to the Uniot Core.
Creates a four-character code (FourCC) value from template parameters.
Definition Common.h:196
static const uint32_t Value
Definition Common.h:197