24#define MAX_NOACKREPEATCOUNT 3
51 _.DataSendBuffer = NULL;
52 _.DataSendInx =
_.DataSendSz = 0;
62 LoRaMacTxInfo_t txInfo;
63 LoRaMacStatus_t status = LoRaMacQueryTxPossible(0, &txInfo);
65 return (status == LORAMAC_STATUS_OK) ? txInfo.MaxPossibleApplicationDataSize : 51;
76 if (LORAMAC_HANDLER_SUCCESS != LmHandlerStop())
78 writeLog(
"myLoraWan_Reconnect Stop on going ...");
82 writeLog(
"myLoraWan_Reconnect Start join");
85 if (
_.IsMeasureQueued)
94 writeLog(
"myLoraWan_StartReconnect - count:%d", (
int)
_.ConnectedCount);
120 LmHandlerAppData_t appData;
121 LmHandlerErrorStatus_t status;
126 if (data == NULL || dataSize == 0 || dataSize > 242)
128 writeLog(
"Invalid data parameters");
129 return LORAMAC_HANDLER_ERROR;
132 appData.Buffer = data;
133 appData.BufferSize = dataSize;
136 writeLog(
"Sending confirmed data to LoRaWAN: port=%d, size=%d bytes", port, dataSize);
139 status = LmHandlerSend(&appData, LORAMAC_HANDLER_CONFIRMED_MSG,
false);
142 case LORAMAC_HANDLER_SUCCESS:
143 writeLog(
"Confirmed data queued for transmission");
146 case LORAMAC_HANDLER_NO_NETWORK_JOINED:
147 writeLog(
"Cannot send: Not joined to network");
150 case LORAMAC_HANDLER_BUSY_ERROR:
151 writeLog(
"Cannot send: MAC layer busy");
154 case LORAMAC_HANDLER_DUTYCYCLE_RESTRICTED:
161 writeLog(
"Send failed with status: %d", status);
179 uint8_t *ptr, sz, isOK = 1;
182 _.CurrentSendingDataSz = 0;
186 isOK = (!maxSize ||
_.CurrentSendingDataSz + sz <= maxSize);
187 if (ptr != NULL && isOK)
189 memcpy(
_.CurrentSendingDataBuffer +
_.CurrentSendingDataSz, ptr, sz);
190 _.CurrentSendingDataSz += sz;
209 writeLog(
"myloraWan_MeasureFinish - data is in sending, measure is queued...");
210 _.IsMeasureQueued = 1;
220 if (memsData != NULL)
224 memset(
_.CurrentSendingDataBuffer, 0,
sizeof(
_.CurrentSendingDataBuffer));
238 uint8_t addOff = blk->
Data - (uint8_t*) blk;
240 _.CurrentSendingDataSz = addOff;
248 _.CurrentSendingDataSz -= addOff;
249 memcpy(
_.CurrentSendingDataBuffer,
_.CurrentSendingDataBuffer + addOff,
_.CurrentSendingDataSz);
254 _.CurrentSendingDataSz = 0;
269 writeLog(
"myloraWan_MeasureFinish - end");
270 _.IsMeasureQueued = 0;
287 writeLog(
"myLoraWan_LastDataSend - begin");
289 if (
_.IsDataSending == 0)
315 if (
_.CurrentSendingDataSz == 0)
317 SysTime_t tim = SysTimeGet();
319 HAL_StatusTypeDef memsStatus;
327 writeLog(
"myLoraWan_LastDataSend: voltage OK, %dmv", (
int) currentBatVoltage);
330 if (
_.DataSendBuffer != NULL &&
_.DataSendSz > 0)
333 writeLog(
"DataSendBuffer: sz:%d, inx:%d", (
int)
_.DataSendSz, (
int)
_.DataSendInx);
334 _.CurrentSendingDataSz = MIN(maxPayLoad,
_.DataSendSz);
335 memcpy(
_.CurrentSendingDataBuffer,
_.DataSendBuffer +
_.DataSendInx,
_.CurrentSendingDataSz);
336 _.DataSendSz -=
_.CurrentSendingDataSz;
337 _.DataSendInx +=
_.CurrentSendingDataSz;
338 if (
_.DataSendSz == 0)
342 _.DataSendBuffer = NULL;
347 writeLog(
"DataSendBuffer still: %d", (
int)
_.DataSendSz);
360 writeLog(
"myLoraWan_LastDataSend - mems sent data, still:%d", (
int)
_memsMainBlock.Data_CountCurrent -
_.FromEndInx);
365 writeLog(
"myLoraWan_LastDataSend - maximum data payload reached:%d", (
int) maxPayLoad);
368 memsStatus = HAL_BUSY;
372 writeLog(
"myLoraWan_LastDataSend - mems no more data");
375 writeLog(
"myLoraWan_LastDataSend - mems error");
377 }
while (
_systemParams.SensSendInOnePacket && memsStatus == HAL_OK && ++
_.FromEndInx);
382 writeLog(
"myLoraWan_LastDataSend - low voltage: %dmV, exit", (
int) currentBatVoltage);
387 if (
_.CurrentSendingDataSz > 0)
390 writeLog(
"myLoraWan_LastDataSend - sendConfirmedData:%d", (
int)
_.CurrentSendingDataSz);
394 writeLog(
"myLoraWan_LastDataSend - sendConfirmedData: no data");
399 writeLog(
"myLoraWan_LastDataSend - busy");
400 writeLog(
"myLoraWan_LastDataSend - end");
404 writeLog(
"myLoraWan_LastDataSend - not connected");
418 writeLog(
"LoRaWAN connected successfully!");
423 LmHandlerErrorStatus_t status = LmHandlerDeviceTimeReq();
425 if (status == LORAMAC_HANDLER_SUCCESS)
427 writeLog(
"Time synchronization request sent");
431 writeLog(
"Failed to send time synchronization request");
457 writeLog(
"Time synchronized: systemParams_CorrectSystemTime returns: %d", (
int) timeSync);
459 SysTime_t sysTime = SysTimeGet();
462 SysTimeLocalTime(sysTime.Seconds, &localTime);
463 writeLog(
"Time synchronized: %04d-%02d-%02d %02d:%02d:%02d", localTime.tm_year + 1900, localTime.tm_mon + 1, localTime.tm_mday, localTime.tm_hour, localTime.tm_min,
479 if (appData == NULL || params == NULL)
481 writeLog(
"Invalid RX data parameters");
485 writeLog(
"Received data from server:");
486 writeLog(
" Port: %d", appData->Port);
487 writeLog(
" Size: %d bytes", appData->BufferSize);
488 writeLog(
" RSSI: %d dBm", params->Rssi);
489 writeLog(
" SNR: %d dB", params->Snr);
490 writeLog(
" Downlink Counter: %lu", params->DownlinkCounter);
496 _.CurrentSendingDataSz = 0;
499 if (
_.IsMeasureQueued)
503 writeLog(
" Status: next data is going to send");
507 switch (appData->Port)
523 _.LoraWanPort = appData->Port;
583 if (params->IsMcpsIndication == 0)
585 writeLog(
" This is an ACK for confirmed uplink");
599 if (
_.CurrentSendingDataSz > 0 && params != NULL)
601 writeLog(
"Transmission completed:");
602 writeLog(
" Uplink Counter: %lu", params->UplinkCounter);
603 writeLog(
" Datarate: DR%d", params->Datarate);
604 writeLog(
" TX Power: %d dBm", params->TxPower);
605 writeLog(
" Channel: %d", params->Channel);
607 if (params->MsgType == LORAMAC_HANDLER_CONFIRMED_MSG)
609 if (params->AckReceived)
611 writeLog(
" Status: ACK received from server");
614 if (
_.DataSendBuffer == NULL)
619 writeLog(
" Status: No ACK received (will retry)");
620 if (
_.NoAckRepeatCount)
622 writeLog(
" Retrying send, retries left: %d", (
int)
_.NoAckRepeatCount);
624 --
_.NoAckRepeatCount;
628 writeLog(
" All retries exhausted, reconnecting...");
635 writeLog(
" Status: Unconfirmed message sent");
638 if (params->Status != LORAMAC_EVENT_INFO_STATUS_OK)
640 writeLog(
" TX Error: %d", params->Status);
648 _.MyloraWanAppBit = myloraWanAppBit;
uint8_t CayenneLppMaxBuffer()
Implements the Cayenne Low Power Protocol.
Header for ADC interface configuration.
uint16_t SYS_GetBatteryLevel(void)
Get the current battery level.
This file contains all the function prototypes for the i2c.c file.
Header of application of the LRWAN Middleware.
#define LORAWAN_DEFAULT_ACTIVATION_TYPE
static struct @042147022327042232212126043022001043033342036075 _
: Header for main.c file. This file contains the common defines of the application....
void writeLog(const char *format,...)
Format and send a log message over UART (printf-style). Available only when WRITELOG is defined; comp...
static void myloraWan_MeasureFinish()
the measure has been finished, sensor data save to memory or local buffer(in case if no memory is exi...
static void myLoraWan_ResetDataDefault()
uint8_t CurrentSendingDataSz
static void myLoraWan_LastDataSend()
take data from LIFO - last data and send. Cannot be nested only if is LoRa connected
void OnTimeSynchronized(void)
Called after date/time has been synchronized from LoRaWAN.
static uint8_t myloraWan_PrepareSendingBuffer(const mems_DataBlock_t *data, uint8_t reset, uint8_t maxSize, SysTime_t prevMeasureTime)
the preparing _currentSendingDataBuffer/_currentSendingDataSz from CayennelLpp to send data to LoRaWa...
static uint8_t myLoRaWAN_GetMaxPayloadSize(void)
Get the maximum application payload size for the current data rate. Returns 0 if the query fails or c...
loraWanPort_t LoraWanPort
void OnLoRaWANRxData(LmHandlerAppData_t *appData, LmHandlerRxParams_t *params)
Handle received downlink data from LoRaWAN server.
static void myLoraWan_Reconnect()
Reconnect to LoRa gateway. In case is measure data are queued, the myloraWan_MeasureFinish is called ...
uint8_t CurrentSendingDataBuffer[256]
void OnLoRaWanConnected(void)
Called after LoRaWAN successfully connects to the server.
#define MAX_NOACKREPEATCOUNT
static void myLoraWan_StartReconnect()
StartReconnect - processing of myLoraWan_Reconnect via sequencer in case ConnectedCount is not falled...
void OnLoRaWANTxData(LmHandlerTxParams_t *params)
Handle transmission event callback.
static LmHandlerErrorStatus_t LoRaWAN_SendConfirmedData(uint8_t *data, uint8_t dataSize, uint8_t port)
Send confirmed data to LoRaWAN network.
void myloraWan_Init(uint8_t myloraWanAppBit)
initialization of myloraWan process, sequencer
const uint8_t * DataSendBuffer
void mems_RemoveLastData(uint8_t fromEndTo)
remove last data
HAL_StatusTypeDef mems_GetLastData(mems_DataBlock_t **memBlock, uint8_t inxFromEnd)
Get the last sensor data if exists. The data and mem block is stored in work buffer,...
mems_MainBlock_t _memsMainBlock
Global instance of the main flash control block; loaded by mems_ReadMainBlock().
HAL_StatusTypeDef mems_AddData(const void *data, mems_DataType_t type, uint8_t size)
write new data of sensor at the end of queue, if is no space, the most older data start to rewrites
mems_DataBlock_t * mems_InicBuffer(mems_DataType_t type)
Initialize the work buffer - _memsDataBlock.
uint8_t * sensors_CayennelFromBckData(uint8_t *sizeOut, SysTime_t *prevMeasureTime)
collecting sensor data into CayenneLppGetBuffer buffer for sending them via LoRaWan,...
void sensors_WriteFromBckToDataBlock(mems_DataBlock_t *data)
filling the data block from sensor data - store to memory the methods works with _bck_XXXXX senzor,...
SysTime_t sensors_GetTimeFromBlock(const mems_DataBlock_t *data)
getting the SysTime_t from block data of memory
void sensors_CopyToBck()
The copying data from current sensors buffer to backUp - _bck_XXXXX.
uint8_t sensors_ReadToBckFromDataBlock(const mems_DataBlock_t *data)
reading from memory block and filling specified sensor from memory - ready for sensors_CayennelData t...
systemParams_t _systemParams
uint8_t systemParams_CorrectSystemTime()
setting/correction of the system time from _systemParams.MqttTime, if contains valid UNIX time(low ui...
void systemParams_SetCRCSystemParams()
Recalculate and store the CRC in _systemParams.Crc. Call this after modifying any field of _systemPar...
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...
systemParams_t _systemParamsBck
uint8_t systemParams_CheckCRC(const systemParams_t *par)
Verify the CRC field of a systemParams_t structure.
#define SYSTEMPARAMS_BATTMINWORK
the voltage (mV) for processing. The system goes to OFF mode, if current votage is lower SYSTEMPARAMS...
loraWanPort_t
the list of LORAWAN port.
@ LORAWANPORT_CONFIG_GET_LAST
@ LORAWANPORT_CONFIG_SET_PART
HAL_StatusTypeDef nfc_WriteMeasureData(I2C_HandleTypeDef *hi2c, const measureDataNFC_t *data)
write the measure data paylod in measureDataNFC_t to NFC on next free position - in circular queue
This file contains all the function prototypes for the rtc.c file.
block of one measure data in CayennelLPP payload format The Data field contains the same payload as i...
uint8_t Data[(4+(4+3)+0+0+(4+0)+0+0)]
Sensor data record stored as one element in the flash circular queue.
Function prototypes for sys_app.c file.