LightHub 4.6.0
Open-source smart home controller firmware
Loading...
Searching...
No Matches
utils.h File Reference

Utility helpers of LightHub. More...

#include <Arduino.h>
#include <IPAddress.h>
#include "aJSON.h"
#include "options.h"
#include "item.h"
#include "Streaming.h"

Go to the source code of this file.

Macros

#define Q(x)
#define QUOTE(x)
#define VALUE_TO_STRING(x)
#define VALUE(x)
#define VAR_NAME_VALUE(var)
#define serialParamType   uint16_t
 Type for a serial parameter (uint32 on ESP32, uint16 elsewhere).

Enumerations

enum  topicType { T_ROOT = 4 , T_DEV = 1 , T_BCST = 2 , T_OUT = 3 }

Functions

void PrintBytes (uint8_t *addr, uint8_t count, bool newline)
void SetBytes (uint8_t *addr, uint8_t count, char *out)
bool SetAddr (char *in, uint8_t *addr)
uint8_t HEX2DEC (char i, bool *err)
long getIntFromStr (char **chan)
itemCmd getNumber (char **chan)
unsigned long freeRam ()
void parseBytes (const char *str, char separator, byte *bytes, int maxBytes, int base)
int log (const char *str,...)
void printFloatValueToStr (char *valstr, float value)
uint32_t ReadUniqueID (uint32_t *pdwUniqueID)
int _inet_aton (const char *aIPAddrString, IPAddress &aResult)
char * _inet_ntoa_r (IPAddress addr, char *buf, int buflen)
void printIPAddress (IPAddress ipAddress)
char * setTopic (char *buf, int8_t buflen, topicType tt, const char *suffix=NULL)
void printUlongValueToStr (char *valstr, unsigned long value)
void scan_i2c_bus ()
void softRebootFunc ()
bool isTimeOver (uint32_t timestamp, uint32_t currTime, uint32_t time, uint32_t modulo=0)
bool executeCommand (aJsonObject *cmd, int8_t toggle=-1)
bool executeCommand (aJsonObject *cmd, int8_t toggle, itemCmd _itemCmd, aJsonObject *defaultItem=NULL, aJsonObject *defaultEmit=NULL, aJsonObject *defaultCan=NULL)
 Execute a JSON command object: control an item (Ctrl), publish an emit topic (MQTT) and/or send a CAN command.
itemCmd mapInt (int32_t arg, aJsonObject *map)
uint32_t millisNZ (uint8_t shift=0, uint32_t mask=0xFFFFFFFFUL)
serialParamType str2SerialParam (char *str)
String toString (const IPAddress &address)
bool getPinVal (uint8_t pin)
int str2regSize (char *str)
 Преобразует строку в тип регистра.
bool checkToken (char *token, char *data)
bool isProtectedPin (short pin)
bool i2cReset ()
uint16_t getCRC (aJsonObject *in)
char * getStringFromJson (aJsonObject *a, int i)
char * getStringFromJson (aJsonObject *a, const char *name)
long getIntFromJson (aJsonObject *a, int i, long def=0)
long getIntFromJson (aJsonObject *a, const char *name, long def=0)
float getFloatFromJson (aJsonObject *a, int i, float def=0.0)
float getFloatFromJson (aJsonObject *a, const char *name, float def=0.0)
long getIntFromJson (aJsonObject *a, const char *name, int i, long def)
float getFloatFromJson (aJsonObject *a, const char *name, int i, float def)
void setupInPin (short pin)
bool readInPin (short pin)
void writeOutPin (short pin, bool val)
template<typename Type>
aJsonObject * getCreateObject (aJsonObject *a, int n, Type def)
aJsonObject * getCreateObject (aJsonObject *a, int n)
template<typename Type>
aJsonObject * getCreateObject (aJsonObject *a, const char *name, Type def)
aJsonObject * getCreateObject (aJsonObject *a, const char *name)
template<typename Type>
aJsonObject * setValToJson (aJsonObject *a, int n, Type val)
template<typename Type>
aJsonObject * setValToJson (aJsonObject *a, const char *name, Type val)
template<typename Type>
aJsonObject * getCreateObject (aJsonObject *a, const char *name, int n, Type def)

Detailed Description

Utility helpers of LightHub.

Miscellaneous platform and protocol helpers: JSON (aJSON) getters and object creation, command execution (executeCommand), MQTT topic building (setTopic), number/byte/string parsing, IP address utilities, CRC16 of JSON objects, pin access and logging.

Macro Definition Documentation

◆ Q

#define Q ( x)
Value:
#x

◆ QUOTE

#define QUOTE ( x)
Value:
Q(x)
#define Q(x)
Definition utils.h:31

◆ serialParamType

#define serialParamType   uint16_t

Type for a serial parameter (uint32 on ESP32, uint16 elsewhere).

◆ VALUE

#define VALUE ( x)
Value:
#define VALUE_TO_STRING(x)
Definition utils.h:33

◆ VALUE_TO_STRING

#define VALUE_TO_STRING ( x)
Value:
#x

◆ VAR_NAME_VALUE

#define VAR_NAME_VALUE ( var)
Value:
#var "=" VALUE(var)

Enumeration Type Documentation

◆ topicType

enum topicType

MQTT topic prefix types used by setTopic(): T_ROOT - root topic only, T_DEV - device topic, T_BCST - broadcast (incoming) topic, T_OUT - output (status) topic.

Enumerator
T_ROOT 
T_DEV 
T_BCST 
T_OUT 

Function Documentation

◆ _inet_aton()

int _inet_aton ( const char * aIPAddrString,
IPAddress & aResult )

Convert an IP address string to an IPAddress.

Returns
true on success.

◆ _inet_ntoa_r()

char * _inet_ntoa_r ( IPAddress addr,
char * buf,
int buflen )

Convert an IPAddress to a string in buf.

Same as ipaddr_ntoa, but reentrant since a user-supplied buffer is used.

Parameters
addrip address in network order to convert
buftarget buffer where the string is stored
buflenlength of buf
Returns
either pointer to buf which now holds the ASCII representation of addr or NULL if buf was too small

◆ checkToken()

bool checkToken ( char * token,
char * data )

Validate an authorization token against data.

Returns
true if the token is valid.

◆ executeCommand() [1/2]

bool executeCommand ( aJsonObject * cmd,
int8_t toggle,
itemCmd _itemCmd,
aJsonObject * defaultItem = NULL,
aJsonObject * defaultEmit = NULL,
aJsonObject * defaultCan = NULL )

Execute a JSON command object: control an item (Ctrl), publish an emit topic (MQTT) and/or send a CAN command.

Parameters
cmd- command object: array (iterate), object with item/emit/can/ icmd/irev/ecmd/erev/act fields, or a legacy string
toggle- 0: use icmd/ecmd, 1: use irev/erev, -1: no toggle
_itemCmd- itemCmd value to send when no command string is given
defaultItem/defaultEmit/defaultCan- fallback targets

ChangeMe - if no '/' in addr - template not working

◆ executeCommand() [2/2]

bool executeCommand ( aJsonObject * cmd,
int8_t toggle = -1 )

Execute a JSON command object with an empty itemCmd.

Parameters
toggle- 0: use icmd/ecmd, 1: use irev/erev, -1: no toggle

◆ freeRam()

unsigned long freeRam ( )
Returns
current free heap size in bytes.

◆ getCRC()

uint16_t getCRC ( aJsonObject * in)
Returns
CRC16 of a JSON object (config integrity, CANDRV builds).

◆ getCreateObject() [1/5]

aJsonObject * getCreateObject ( aJsonObject * a,
const char * name )

◆ getCreateObject() [2/5]

template<typename Type>
aJsonObject * getCreateObject ( aJsonObject * a,
const char * name,
int n,
Type def )

◆ getCreateObject() [3/5]

template<typename Type>
aJsonObject * getCreateObject ( aJsonObject * a,
const char * name,
Type def )

◆ getCreateObject() [4/5]

aJsonObject * getCreateObject ( aJsonObject * a,
int n )

◆ getCreateObject() [5/5]

template<typename Type>
aJsonObject * getCreateObject ( aJsonObject * a,
int n,
Type def )

◆ getFloatFromJson() [1/3]

float getFloatFromJson ( aJsonObject * a,
const char * name,
float def = 0.0 )
Returns
the float field of a JSON object by name, or def.

◆ getFloatFromJson() [2/3]

float getFloatFromJson ( aJsonObject * a,
const char * name,
int i,
float def )
Returns
the float field of a JSON object by name and array index, or def.

◆ getFloatFromJson() [3/3]

float getFloatFromJson ( aJsonObject * a,
int i,
float def = 0.0 )
Returns
the float element of a JSON array by index, or def.

◆ getIntFromJson() [1/3]

long getIntFromJson ( aJsonObject * a,
const char * name,
int i,
long def )
Returns
the int field of a JSON object by name and array index, or def.

◆ getIntFromJson() [2/3]

long getIntFromJson ( aJsonObject * a,
const char * name,
long def = 0 )
Returns
the int field of a JSON object by name, or def.

◆ getIntFromJson() [3/3]

long getIntFromJson ( aJsonObject * a,
int i,
long def = 0 )
Returns
the int element of a JSON array by index, or def.

◆ getIntFromStr()

long getIntFromStr ( char ** chan)

Parse an integer from *chan, advancing the pointer past it (after ',').

◆ getNumber()

itemCmd getNumber ( char ** chan)

Parse a typed number (int / tens / float) from *chan into an itemCmd.

◆ getPinVal()

bool getPinVal ( uint8_t pin)
Returns
the current output state of a pin (fast register read).

◆ getStringFromJson() [1/2]

char * getStringFromJson ( aJsonObject * a,
const char * name )
Returns
the string field of a JSON object by name, or NULL.

◆ getStringFromJson() [2/2]

char * getStringFromJson ( aJsonObject * a,
int i )
Returns
the string element of a JSON array by index, or NULL.

◆ HEX2DEC()

uint8_t HEX2DEC ( char i,
bool * err )

Convert one hex digit to decimal, setting err on failure.

◆ i2cReset()

bool i2cReset ( )

Recover the I2C bus by toggling SCL.

Returns
true on success.

◆ isProtectedPin()

bool isProtectedPin ( short pin)
Returns
true if the pin is in the protected pins list (arming timer applies).

◆ isTimeOver()

bool isTimeOver ( uint32_t timestamp,
uint32_t currTime,
uint32_t time,
uint32_t modulo = 0 )
Returns
true if (currTime - timestamp) >= time, with wrap-around safety.

◆ log()

int log ( const char * str,
... )

Log a formatted message to the debug serial.

◆ mapInt()

itemCmd mapInt ( int32_t arg,
aJsonObject * map )

Map a number through a JSON map into an itemCmd.

◆ millisNZ()

uint32_t millisNZ ( uint8_t shift = 0,
uint32_t mask = 0xFFFFFFFFUL )
Returns
millis() shifted and masked, never returning 0 or -1 (for timestamp storage).

◆ parseBytes()

void parseBytes ( const char * str,
char separator,
byte * bytes,
int maxBytes,
int base )

Parse a delimited hex string into a byte array.

◆ PrintBytes()

void PrintBytes ( uint8_t * addr,
uint8_t count,
bool newline )

Print count bytes from addr to the debug serial as hex.

◆ printFloatValueToStr()

void printFloatValueToStr ( char * valstr,
float value )

Convert a float to a fixed-format string for MQTT publishing.

◆ printIPAddress()

void printIPAddress ( IPAddress ipAddress)

Print an IP address to the debug serial.

◆ printUlongValueToStr()

void printUlongValueToStr ( char * valstr,
unsigned long value )

Convert an unsigned long to a string for MQTT publishing.

◆ readInPin()

bool readInPin ( short pin)
Returns
the (debounced) value of an input pin.

◆ ReadUniqueID()

uint32_t ReadUniqueID ( uint32_t * pdwUniqueID)

Read the chip unique id (MAC) into 32-bit words.

Returns
the id.

◆ scan_i2c_bus()

void scan_i2c_bus ( )

Scan the I2C bus and log the found devices.

◆ SetAddr()

bool SetAddr ( char * in,
uint8_t * addr )

Parse a hex string (AA:BB:...) into a MAC/EUI byte buffer.

Returns
true on success.

◆ SetBytes()

void SetBytes ( uint8_t * addr,
uint8_t count,
char * out )

Format count bytes from addr as a hex string into out.

◆ setTopic()

char * setTopic ( char * buf,
int8_t buflen,
topicType tt,
const char * suffix = NULL )

Build the full MQTT topic prefix of the given type in buf.

Returns
buf.

◆ setupInPin()

void setupInPin ( short pin)

Configure a pin as an input.

◆ setValToJson() [1/2]

template<typename Type>
aJsonObject * setValToJson ( aJsonObject * a,
const char * name,
Type val )

◆ setValToJson() [2/2]

template<typename Type>
aJsonObject * setValToJson ( aJsonObject * a,
int n,
Type val )

◆ softRebootFunc()

void softRebootFunc ( )

Perform a soft reboot of the device.

◆ str2regSize()

int str2regSize ( char * str)

Преобразует строку в тип регистра.

Parse a register size string (Modbus).

Parameters
strСтрока с типом регистра.
Returns
Код типа регистра.

◆ str2SerialParam()

serialParamType str2SerialParam ( char * str)

Parse a serial parameter string (e.g. baud rate / parity) to its code.

◆ toString()

String toString ( const IPAddress & address)
Returns
an IPAddress as a string.

◆ writeOutPin()

void writeOutPin ( short pin,
bool val )

Write a value to an output pin.