LightHub 4.6.0
Open-source smart home controller firmware
Loading...
Searching...
No Matches
utils.cpp File Reference
#include "utils.h"
#include "options.h"
#include "stdarg.h"
#include <Wire.h>
#include "main.h"
#include "item.h"
#include <PubSubClient.h>
#include <HardwareSerial.h>
#include "templateStr.h"

Classes

struct  serial_st

Macros

#define ARDBUFFER   16
#define serialModesNum   sizeof(serialModes_P)/sizeof(serial_st)

Functions

void PrintBytes (uint8_t *addr, uint8_t count, bool newline)
void SetBytes (uint8_t *addr, uint8_t count, char *out)
byte HEX2DEC (char i, bool *err)
bool SetAddr (char *in, uint8_t *addr)
long getIntFromStr (char **chan)
itemCmd getNumber (char **chan)
void parseBytes (const char *str, char separator, byte *bytes, int maxBytes, int base)
void printFloatValueToStr (char *valstr, float value)
int log (const char *str,...)
 __attribute__ ((section(".ramfunc"))) uint32_t ReadUniqueID(uint32_t *pdwUniqueID)
int _inet_aton (const char *aIPAddrString, IPAddress &aResult)
char * _inet_ntoa_r (IPAddress addr, char *buf, int buflen)
String toString (const IPAddress &address)
void printIPAddress (IPAddress ipAddress)
char * setTopic (char *buf, int8_t buflen, topicType tt, const char *suffix)
void printUlongValueToStr (char *valstr, unsigned long value)
void scan_i2c_bus ()
bool isTimeOver (uint32_t timestamp, uint32_t currTime, uint32_t time, uint32_t modulo)
bool executeCommand (aJsonObject *cmd, int8_t toggle)
bool executeCommand (aJsonObject *cmd, int8_t toggle, itemCmd _itemCmd, aJsonObject *defaultItem, aJsonObject *defaultEmit, aJsonObject *defaultCan)
 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, uint32_t mask)
serialParamType str2SerialParam (char *str)
bool getPinVal (uint8_t pin)
bool checkToken (char *token, char *data)
bool isProtectedPin (short pin)
bool i2cReset ()
char * getStringFromJson (aJsonObject *a, int i)
char * getStringFromJson (aJsonObject *a, const char *name)
long getIntFromJson (aJsonObject *a, int i, long def)
long getIntFromJson (aJsonObject *a, const char *name, long def)
long getIntFromJson (aJsonObject *a, const char *name, int i, long def)
float getFloatFromJson (aJsonObject *a, int i, float def)
float getFloatFromJson (aJsonObject *a, const char *name, float def)
float getFloatFromJson (aJsonObject *a, const char *name, int i, float def)
aJsonObject * getCreateObject (aJsonObject *a, int n)
aJsonObject * getCreateObject (aJsonObject *a, const char *name)
void setupInPin (short pin)
bool readInPin (short pin)
void writeOutPin (short pin, bool val)

Variables

PubSubClient mqttClient
int8_t ethernetIdleCount
int8_t configLocked
const char outTopic[] PROGMEM = OUTTOPIC
char * deviceName
aJsonObject * topics
const char HEXSTR [] = "0123456789ABCDEF"

Macro Definition Documentation

◆ ARDBUFFER

#define ARDBUFFER   16

◆ serialModesNum

#define serialModesNum   sizeof(serialModes_P)/sizeof(serial_st)

Function Documentation

◆ __attribute__()

__attribute__ ( (section(".ramfunc")) )

◆ _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 )

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

Convert an IPAddress to a string in buf.

◆ 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 = -1 )

Execute a JSON command object with an empty itemCmd.

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

◆ executeCommand() [2/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

◆ getCreateObject() [1/2]

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

◆ getCreateObject() [2/2]

aJsonObject * getCreateObject ( aJsonObject * a,
int n )

◆ 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()

byte 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.

◆ 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.

◆ 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.

Variable Documentation

◆ configLocked

int8_t configLocked
extern

◆ deviceName

char* deviceName
extern

◆ ethernetIdleCount

int8_t ethernetIdleCount
extern

◆ HEXSTR

const char HEXSTR[] = "0123456789ABCDEF"

◆ mqttClient

PubSubClient mqttClient
extern

◆ PROGMEM

const serial_st serialModes_P [] PROGMEM = OUTTOPIC

◆ topics

aJsonObject* topics
extern