L14-Click 1.0
STM32WLE5CC LoRaWAN Sensor Platform
Loading...
Searching...
No Matches
mysensors_base.c File Reference
#include "mysensors_base.h"
#include "mysensors.h"
#include "main.h"
#include "se-identity.h"
#include "lora_app.h"
#include "sys_app.h"
#include "utils/mydefs.h"
#include "utils/utils.h"
Include dependency graph for mysensors_base.c:

Go to the source code of this file.

Macros

#define SYS_REF(i, f)
#define SYS_WR(inst, member)
#define SYS_WR_ARR(inst, member, len)
#define SYS_WR_STR(inst, member)
#define SYS_WR_TIME(inst, member)

Functions

static int8_t systemParamsRef_Get (uint8_t inx)
 
  • find the index of field

static void OnTimerToOff (void *)
 
  • Dummy function - the timer need empty dummy, cannot be NULL

static void getDevEuiFromHardware (uint8_t *devEui)
uint8_t * systemParams_getAppDevEUI ()
 Return a pointer to the DevEUI stored in _systemParams. The DevEUI is auto-generated from the chip UID during initialisation and is read-only from the phone app's perspective.
uint8_t * systemParams_getAppEUI ()
 Return a pointer to the AppEUI (JoinEUI) stored in _systemParams. Used by the LoRaWAN stack during the OTAA join procedure.
uint8_t * systemParams_getAppKey ()
 Return a pointer to the AppKey stored in _systemParams. Used by the LoRaWAN stack to derive session keys during OTAA join.
void systemParams_SetCRCSystemParams ()
 Recalculate and store the CRC in _systemParams.Crc. Call this after modifying any field of _systemParams before writing the structure to the NFC EEPROM.
uint8_t systemParams_CheckCRC (const systemParams_t *par)
 Verify the CRC field of a systemParams_t structure.
void sensorsBase_Init ()
 Initialization of base sensor data structure.
void sensorsBase_StartTimerToOff ()
 start timer to off the function is called from PWR_EnterStopMode - timer is started
void sensorsBase_StopTimerToOff ()
 stop timer to off the function is called from PWR_ExitStopMode - the timer value is checked and the PWR_ExitStopMode was invoked from this times, system goes to OFF mode setModeDevice(MODEDEVICE_OFF)
void systemParams_Correction ()
 Clamp all RW fields of _systemParams to their valid ranges, called after loading parameters from NFC to ensure no out-of-range values are used (e.g. port numbers, timeouts, data rates).
The DEVEui cannot be changed - problem of rewrite of DEVUi accross devices.
uint8_t systemParams_IsSensorAvaiable (systemParams_Sensors_t sensorType)
 Check whether a particular sensor is enabled in _systemParams.AvailableSensors.
uint8_t systemParams_CorrectSystemTime ()
 setting/correction of the system time from _systemParams.MqttTime, if contains valid UNIX time(low uint32_t). MCU is reading MqttTime from NFC and compares with current system time - SysTimeGet The system time is changed(SysTimeSet) if is less that time in _systemParams.MqttTime
void systemParams_SetFromPart (const uint8_t *buffer, uint8_t bufSize)
 The setting of systemParams_t from server dowlink. The buffer contains the combination for setting of value
.
void sensorsBase_MeasureNFCInit ()
 the default initialization of _measureNFC struct
void sensorsBase_SetCRCMeasureNFC ()
 Recalculate and store the CRC in _measureNFC.Crc.
Call this after modifying any field of _measureNFC before writing the structure to the NFC EEPROM.
uint8_t sensorsBase_CheckCRCMeasureNFC ()
 Verify the CRC field of a _measureNFC structure.
static void _systemParams_unixToDateTime (uint64_t t, uint16_t *year, uint8_t *month, uint8_t *day, uint8_t *hour, uint8_t *min, uint8_t *sec)
 Convert a Unix UTC timestamp (seconds since 1970-01-01 00:00:00) to broken-down calendar fields.
void systemParams_Log (const char *info)
 Log the current _systemParams fields to the UART debug output.

Variables

systemParams_t _systemParams
systemParams_t _systemParamsBck = {}
systemParamsRef_t _systemParamRef []
 
  • the definition of _systemParams and struct position and sizes

int8_t _tryInit = 1
measureNFC_t _measureNFC = {}
uint8_t _batPercLevel = 0
static UTIL_TIMER_Object_t _timerToOff = {}
static uint32_t _timerTick = 0

Macro Definition Documentation

◆ SYS_REF

#define SYS_REF ( i,
f )
Value:
{.Inx = i, .Size = sizeof(_systemParams.f), .Addr = ((uint8_t*)&_systemParams.f - (uint8_t*)&_systemParams)}
systemParams_t _systemParams

Definition at line 92 of file mysensors_base.c.

◆ SYS_WR

#define SYS_WR ( inst,
member )
Value:
writeLog("%03u(%02X) " #member ":%d(%02X)", \
(unsigned)((uint8_t*)&((inst).member) - (uint8_t*)&(inst)), \
(unsigned)((uint8_t*)&((inst).member) - (uint8_t*)&(inst)), \
(int)((inst).member),(int)((inst).member))
void writeLog(const char *format,...)
Format and send a log message over UART (printf-style). Available only when WRITELOG is defined; comp...
Definition main.c:105

Definition at line 396 of file mysensors_base.c.

Referenced by systemParams_Log().

◆ SYS_WR_ARR

#define SYS_WR_ARR ( inst,
member,
len )
Value:
do { \
unsigned _off = (unsigned)((uint8_t*)&((inst).member) - (uint8_t*)&(inst)); \
char _buf[3*(len)]; \
for (unsigned _i = 0; _i < (unsigned)(len); _i++) { \
_buf[_i*3+0] = "0123456789ABCDEF"[((inst).member[_i] >> 4) & 0xF]; \
_buf[_i*3+1] = "0123456789ABCDEF"[ (inst).member[_i] & 0xF]; \
_buf[_i*3+2] = (_i < (unsigned)(len)-1) ? ':' : '\0'; \
} \
writeLog("%03u(%02X) " #member ":%s", _off, _off, _buf); \
} while(0)

Definition at line 403 of file mysensors_base.c.

Referenced by systemParams_Log().

◆ SYS_WR_STR

#define SYS_WR_STR ( inst,
member )
Value:
writeLog("%03u(%02X) " #member ":%s", \
(unsigned)((uint8_t*)&((inst).member) - (uint8_t*)&(inst)), \
(unsigned)((uint8_t*)&((inst).member) - (uint8_t*)&(inst)), \
(inst).member)

Definition at line 416 of file mysensors_base.c.

Referenced by systemParams_Log().

◆ SYS_WR_TIME

#define SYS_WR_TIME ( inst,
member )
Value:
do { \
unsigned _off = (unsigned)((uint8_t*)&((inst).member) - (uint8_t*)&(inst)); \
uint64_t _t = (uint64_t)((inst).member); \
uint32_t _thi = (uint32_t)((_t >> 32) & 0xFFFFFFFFUL); \
uint32_t _tlo = (uint32_t)(_t & 0xFFFFFFFFUL); \
if (_t == 0ULL) { \
writeLog("%03u(%02X) " #member ":%lu%08lu(%08lX%08lX) (not set)", \
_off, _off, 0UL, 0UL, 0UL, 0UL); \
} else { \
uint16_t _yr; uint8_t _mo, _dy, _hh, _mm, _ss; \
_systemParams_unixToDateTime(_t, &_yr, &_mo, &_dy, &_hh, &_mm, &_ss); \
writeLog("%03u(%02X) " #member ":%lu%08lu(%08lX%08lX) %04u-%02u-%02u %02u:%02u:%02u UTC", \
_off, _off, \
(unsigned long)_thi, (unsigned long)_tlo, \
(unsigned long)_thi, (unsigned long)_tlo, \
(unsigned)_yr, (unsigned)_mo, (unsigned)_dy, \
(unsigned)_hh, (unsigned)_mm, (unsigned)_ss); \
} \
} while(0)

Definition at line 423 of file mysensors_base.c.

Referenced by systemParams_Log().

Function Documentation

◆ _systemParams_unixToDateTime()

void _systemParams_unixToDateTime ( uint64_t t,
uint16_t * year,
uint8_t * month,
uint8_t * day,
uint8_t * hour,
uint8_t * min,
uint8_t * sec )
static

Convert a Unix UTC timestamp (seconds since 1970-01-01 00:00:00) to broken-down calendar fields.

Uses Howard Hinnant's pure-integer civil-from-days Gregorian algorithm (no libc time functions required), correct for any 64-bit Unix timestamp including leap years and century / 400-year boundaries.

Parameters
tSeconds since the Unix epoch (UTC).
yearOutput: full calendar year (e.g. 2026).
monthOutput: month 1–12.
dayOutput: day-of-month 1–31.
hourOutput: hours 0–23.
minOutput: minutes 0–59.
secOutput: seconds 0–59.

Definition at line 460 of file mysensors_base.c.

◆ getDevEuiFromHardware()

void getDevEuiFromHardware ( uint8_t * devEui)
static

Definition at line 165 of file mysensors_base.c.

References calculateCrc(), SYSTEM_APP_DevEUI0, SYSTEM_APP_DevEUI1, and SYSTEM_APP_DevEUI2.

Referenced by systemParams_Correction(), and systemParams_getAppDevEUI().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ OnTimerToOff()

void OnTimerToOff ( void * )
static

  • Dummy function - the timer need empty dummy, cannot be NULL

Definition at line 161 of file mysensors_base.c.

Referenced by sensorsBase_StartTimerToOff().

Here is the caller graph for this function:

◆ sensorsBase_CheckCRCMeasureNFC()

uint8_t sensorsBase_CheckCRCMeasureNFC ( )

Verify the CRC field of a _measureNFC structure.

Return values
1– CRC is valid (structure is intact).
0– CRC mismatch (structure is corrupt or uninitialised).

Definition at line 377 of file mysensors_base.c.

References _measureNFC, calculateCrc(), CLAMP, and sensorsBase_SetCRCMeasureNFC().

Referenced by nfc_Init(), and nfc_WriteMeasureData().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sensorsBase_Init()

void sensorsBase_Init ( )

Initialization of base sensor data structure.

Definition at line 225 of file mysensors_base.c.

References Error_Handler(), MEASUEREDATANFC_EEPROMSIZE, MEASUREDATANFC_ADDR, sensorsBase_MeasureNFCInit(), systemParams_getAppDevEUI(), systemParams_SetCRCSystemParams(), SZ_MEASURENFC, SZ_SYSTEMPARAMS, and writeLog().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sensorsBase_MeasureNFCInit()

void sensorsBase_MeasureNFCInit ( )

the default initialization of _measureNFC struct

Definition at line 362 of file mysensors_base.c.

References _measureNFC, MEASUEREDATANFC_EEPROMSIZE, MEASUREDATANFC_ADDR, and sensorsBase_SetCRCMeasureNFC().

Referenced by nfc_Init(), nfc_WriteMeasureData(), and sensorsBase_Init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sensorsBase_SetCRCMeasureNFC()

void sensorsBase_SetCRCMeasureNFC ( )

Recalculate and store the CRC in _measureNFC.Crc.
Call this after modifying any field of _measureNFC before writing the structure to the NFC EEPROM.

Definition at line 372 of file mysensors_base.c.

References _measureNFC, and calculateCrc().

Referenced by nfc_WriteMeasureNFC(), sensorsBase_CheckCRCMeasureNFC(), and sensorsBase_MeasureNFCInit().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sensorsBase_StartTimerToOff()

void sensorsBase_StartTimerToOff ( )

start timer to off the function is called from PWR_EnterStopMode - timer is started

Definition at line 270 of file mysensors_base.c.

References _timerTick, _timerToOff, HAL_GetTick(), OnTimerToOff(), and SYSTEMPARAMS_MINTIME2OFF.

Referenced by PWR_EnterStopMode().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sensorsBase_StopTimerToOff()

void sensorsBase_StopTimerToOff ( )

stop timer to off the function is called from PWR_ExitStopMode - the timer value is checked and the PWR_ExitStopMode was invoked from this times, system goes to OFF mode setModeDevice(MODEDEVICE_OFF)

Definition at line 278 of file mysensors_base.c.

References _timerTick, _timerToOff, HAL_GetTick(), MODEDEVICE_OFF, setModeDevice(), and SYSTEMPARAMS_MINTIME2OFF.

Referenced by PWR_ExitStopMode().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ systemParams_CheckCRC()

uint8_t systemParams_CheckCRC ( const systemParams_t * par)

Verify the CRC field of a systemParams_t structure.

Parameters
parPointer to the systemParams_t to check.
Return values
1– CRC is valid (structure is intact).
0– CRC mismatch (structure is corrupt or uninitialised).

Definition at line 219 of file mysensors_base.c.

References calculateCrc(), and systemParams_t::Crc.

Referenced by nfc_ReadSystemParamsToBck(), and OnLoRaWANRxData().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ systemParams_Correction()

void systemParams_Correction ( )

Clamp all RW fields of _systemParams to their valid ranges, called after loading parameters from NFC to ensure no out-of-range values are used (e.g. port numbers, timeouts, data rates).
The DEVEui cannot be changed - problem of rewrite of DEVUi accross devices.

Definition at line 290 of file mysensors_base.c.

References _systemParams, CLAMP, GetDevAddr(), getDevEuiFromHardware(), SYSTEMPARAMS_MAXSENSORTIME, and SYSTEMPARAMS_MINSENSORTIME.

Referenced by nfc_Init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ systemParams_CorrectSystemTime()

uint8_t systemParams_CorrectSystemTime ( )

setting/correction of the system time from _systemParams.MqttTime, if contains valid UNIX time(low uint32_t). MCU is reading MqttTime from NFC and compares with current system time - SysTimeGet The system time is changed(SysTimeSet) if is less that time in _systemParams.MqttTime

Return values
0- no changes
1- the system time has been update
2- the _systemParams.MqttTime

Definition at line 310 of file mysensors_base.c.

References _systemParams.

Referenced by main(), and OnTimeSynchronized().

Here is the caller graph for this function:

◆ systemParams_getAppDevEUI()

uint8_t * systemParams_getAppDevEUI ( )

Return a pointer to the DevEUI stored in _systemParams. The DevEUI is auto-generated from the chip UID during initialisation and is read-only from the phone app's perspective.

Returns
Pointer to the 8-byte DevEUI array inside _systemParams.

Definition at line 189 of file mysensors_base.c.

References _systemParams, GetDevAddr(), and getDevEuiFromHardware().

Referenced by LoRaWAN_Init(), and sensorsBase_Init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ systemParams_getAppEUI()

uint8_t * systemParams_getAppEUI ( )

Return a pointer to the AppEUI (JoinEUI) stored in _systemParams. Used by the LoRaWAN stack during the OTAA join procedure.

Returns
Pointer to the 8-byte AppEUI array inside _systemParams.

Definition at line 203 of file mysensors_base.c.

References _systemParams.

Referenced by LoRaWAN_Init().

Here is the caller graph for this function:

◆ systemParams_getAppKey()

uint8_t * systemParams_getAppKey ( )

Return a pointer to the AppKey stored in _systemParams. Used by the LoRaWAN stack to derive session keys during OTAA join.

Returns
Pointer to the 16-byte AppKey array inside _systemParams.

Definition at line 208 of file mysensors_base.c.

References _systemParams.

Referenced by LoRaWAN_Init().

Here is the caller graph for this function:

◆ systemParams_IsSensorAvaiable()

uint8_t systemParams_IsSensorAvaiable ( systemParams_Sensors_t sensorType)

Check whether a particular sensor is enabled in _systemParams.AvailableSensors.

Parameters
sensorTypeOne of the systemParams_Sensors_t bitmask values.
Return values
1– sensor is marked as available and should be polled.
0– sensor is disabled in the configuration.

Definition at line 305 of file mysensors_base.c.

References _systemParams.

Referenced by sensors_Init(), sensors_OnOff(), sensors_Read(), and sensors_Service().

Here is the caller graph for this function:

◆ systemParams_Log()

void systemParams_Log ( const char * info)

Log the current _systemParams fields to the UART debug output.

Parameters
infoOptional prefix string printed before the dump (e.g. "LOAD" or "SAVE").

Definition at line 489 of file mysensors_base.c.

References _measureNFC, _systemParamRef, _systemParams, COUNT_OF, SYS_WR, SYS_WR_ARR, SYS_WR_STR, SYS_WR_TIME, and writeLog().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ systemParams_SetCRCSystemParams()

void systemParams_SetCRCSystemParams ( )

Recalculate and store the CRC in _systemParams.Crc. Call this after modifying any field of _systemParams before writing the structure to the NFC EEPROM.

Definition at line 214 of file mysensors_base.c.

References _systemParams, and calculateCrc().

Referenced by nfc_WriteSystemParams(), OnLoRaWANRxData(), sensorsBase_Init(), and systemParams_SetFromPart().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ systemParams_SetFromPart()

void systemParams_SetFromPart ( const uint8_t * buffer,
uint8_t bufSize )

The setting of systemParams_t from server dowlink. The buffer contains the combination for setting of value
.

2-SensTimeoutMeasure,4,
[2][C0D40100] - the setting for SensTimeoutMeasure -> inx:2 size:4, value:4bytes (120000)
3-SensAltitude,2
[3][4803] - the setting for SensAltitude -> inx:3 size:2, value:2bytes (840)
in buffer: [2][C0D40100]|[3][4803] or [3][4803]|[2][C0D40100]

Definition at line 329 of file mysensors_base.c.

References _systemParamRef, _systemParams, systemParams_SetCRCSystemParams(), systemParamsRef_Get(), and writeLog().

Referenced by OnLoRaWANRxData().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ systemParamsRef_Get()

int8_t systemParamsRef_Get ( uint8_t inx)
static

  • find the index of field

Return values
indexinto _systemParamRef where is found inx param
-1- in case, if specified index not exist

Definition at line 148 of file mysensors_base.c.

References _systemParamRef, and COUNT_OF.

Referenced by systemParams_SetFromPart().

Here is the caller graph for this function:

Variable Documentation

◆ _batPercLevel

uint8_t _batPercLevel = 0

level of battery percentage on start

Definition at line 136 of file mysensors_base.c.

Referenced by main(), and sensors_CayennelFromBckData().

◆ _measureNFC

◆ _systemParamRef

systemParamsRef_t _systemParamRef[]

  • the definition of _systemParams and struct position and sizes

Definition at line 98 of file mysensors_base.c.

Referenced by systemParams_Log(), systemParams_SetFromPart(), and systemParamsRef_Get().

◆ _systemParams

◆ _systemParamsBck

systemParams_t _systemParamsBck = {}

Global system parameters backup buffer for NFC reading and and for sending via downlink to server

Definition at line 85 of file mysensors_base.c.

Referenced by nfc_Init(), nfc_ReadSystemParams(), nfc_ReadSystemParamsToBck(), and OnLoRaWANRxData().

◆ _timerTick

uint32_t _timerTick = 0
static

Definition at line 141 of file mysensors_base.c.

Referenced by sensorsBase_StartTimerToOff(), and sensorsBase_StopTimerToOff().

◆ _timerToOff

UTIL_TIMER_Object_t _timerToOff = {}
static

Definition at line 140 of file mysensors_base.c.

Referenced by sensorsBase_StartTimerToOff(), and sensorsBase_StopTimerToOff().

◆ _tryInit

int8_t _tryInit = 1

Flag used by xxx_Is() calls; when set to 1, the Is function attempts to (re-)initialize the sensor

Definition at line 134 of file mysensors_base.c.

Referenced by nfc_FactoryReset(), nfc_ReadMeasureNFC(), nfc_ReadSystemParams(), nfc_ReadSystemParamsToBck(), nfc_WriteMeasureData(), nfc_WriteMeasureNFC(), nfc_WriteSystemParams(), and sensors_Read().