Atmel Software Framework

datalog.c File Reference
#include <string.h>
#include "gpio.h"
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "semphr.h"
#include "datalog.h"
#include "tracedump.h"
#include "fsaccess.h"
#include "sensor.h"
#include "temperature.h"
#include "light.h"
#include "potentiometer.h"
#include "cptime.h"
#include "BasicSMTP.h"

Macros

#define DATALOG_INVALID_LOGSRC_ID   (0xFFFFFFFF)
 
#define DATALOG_LOGFILE_MAXSIZE   20480
 
#define DATALOG_LOGSQUEUE_HITHRESHOLD   (DATALOG_LOGSQUEUE_SIZE*70/100)
 
#define DATALOG_LOGSQUEUE_LOTHRESHOLD   (DATALOG_LOGSQUEUE_SIZE*30/100)
 
#define DATALOG_LOGSQUEUE_SIZE   30
 
#define DATALOG_STACK_SIZE   configMINIMAL_STACK_SIZE
 
#define LOGTOFILE_STACK_SIZE   configMINIMAL_STACK_SIZE
 

Functions

bool b_datalog_set_current_log_drive_toDF (void)
 Set the current logs to be stored to the DataFlash. More...
 
bool b_datalog_set_current_log_drive_toSDMMC (void)
 Set the current logs to be stored to the SDMMC card. More...
 
bool bdatalog_start (unsigned portBASE_TYPE uxPriority)
 
static portTASK_FUNCTION (vDataLogTask, pvParameters)
 The datalog gathering task. More...
 
static portTASK_FUNCTION (vLogToFileTask, pvParameters)
 The log-to-file task. More...
 
void prv_vdatalog_pool_log_free (void *pxLog)
 Mark a data log structure from the pool of logs as free. More...
 
static void prv_vsave_logs (int fd_current_logfile)
 If the log file descriptor is valid, pop the oldest logs from the logs queue into the current log file until the low threshold of the logs queue is reached; then close the current log file. If the log file descriptor is invalid, simply pop the oldest logs from the logs queue until the low threshold of the logs queue is reached. More...
 
static int prv_xopen_current_logfile (void)
 Open the current log file. More...
 
xLogDefpxdatalog_log_alloc_init (bool bAllocType)
 Allocate and init a xLogDef structure. More...
 
int sprintf (char *out, const char *format,...)
 
void v_datalog_AddSensorLog (eLogSourceId SensorId)
 Get the current value of a sensor and log it in the xLogsQueue. More...
 
void vdatalog_log_free (xLogDef *pxLog)
 Free a data log structure and its content. More...
 
void vdatalog_make_logstring (xLogDef *pxLog, signed portCHAR *pcLogString)
 Build a log string from a xLogDef log structure. More...
 
portBASE_TYPE x_datalog_AddLogFromISR (xLogDef *pxLog)
 Add a log in the xLogsQueue, from within an ISR. More...
 
eStatus xDataLog_GetStatus (void)
 

Variables

static char acLogFileName [25]
 
char * acLogSourceName [12]
 
static char acLogString [DATALOG_LOG_MAXSIZE]
 
static char acPreviousLogFileName [25]
 
static xLogDef axLogsPool [DATALOG_LOGSQUEUE_HITHRESHOLD]
 
const signed portCHAR * pcStringCurrentLogDirectoryName
 
const signed portCHAR *const pcStringLogDirectoryNameA = (signed portCHAR *)"A:/LOG"
 
const signed portCHAR *const pcStringLogDirectoryNameB = (signed portCHAR *)"B:/LOG"
 
static char pcTempoDate [17]
 
static U8 u8FreeLogIdx = 0
 
static unsigned portBASE_TYPE uxNbMsgsInLogsQueue = 0
 
time_t xcptime_LocalTime
 
static xTaskHandle xDataLogHndl = NULL
 
static eStatus xDataLogStatus = SYS_STATUS_STARTUP
 
xSemaphoreHandle xLOGMutex
 
static xQueueHandle xLogsQueue = 0
 
static xTaskHandle xLogToFileHndl = NULL
 

#define DATALOG_INVALID_LOGSRC_ID   (0xFFFFFFFF)
#define DATALOG_LOGFILE_MAXSIZE   20480

The max log file size.

Referenced by prv_xopen_current_logfile().

#define DATALOG_LOGSQUEUE_HITHRESHOLD   (DATALOG_LOGSQUEUE_SIZE*70/100)

The nb of items in the xLogsQueue hi-threshold upon which we should dump the logs to file = 70% of the max nb of items the queue can hold.

Referenced by bdatalog_start(), pxdatalog_log_alloc_init(), and v_datalog_AddSensorLog().

#define DATALOG_LOGSQUEUE_LOTHRESHOLD   (DATALOG_LOGSQUEUE_SIZE*30/100)

The nb of items in the xLogsQueue lo-threshold upon which we should stop dumping the logs to file = 30% of the max nb of items the queue can hold.

Referenced by prv_vsave_logs().

#define DATALOG_LOGSQUEUE_SIZE   30

Detailed description of the module: TODO

The maximum number of data logs that the xLogsQueue can contain.

Referenced by bdatalog_start(), and x_datalog_AddLogFromISR().

#define DATALOG_STACK_SIZE   configMINIMAL_STACK_SIZE

Data log task stack size.

Referenced by bdatalog_start().

#define LOGTOFILE_STACK_SIZE   configMINIMAL_STACK_SIZE

LogToFile task stack size.

Referenced by bdatalog_start().

bool b_datalog_set_current_log_drive_toDF ( void  )

Set the current logs to be stored to the DataFlash.

Returns
true

References acLogFileName, pcStringCurrentLogDirectoryName, pcStringLogDirectoryNameA, and pdTRUE.

bool b_datalog_set_current_log_drive_toSDMMC ( void  )

Set the current logs to be stored to the SDMMC card.

Returns
true

References acLogFileName, pcStringCurrentLogDirectoryName, pcStringLogDirectoryNameB, and pdTRUE.

bool bdatalog_start ( unsigned portBASE_TYPE  uxPriority)

Start the data logger module.

Parameters
uxPriorityThe base priority of the data logger tasks.
Returns
true upon success, else false.

References DATALOG_INVALID_LOGSRC_ID, DATALOG_LOGSQUEUE_HITHRESHOLD, DATALOG_LOGSQUEUE_SIZE, DATALOG_STACK_SIZE, i, LOGTOFILE_STACK_SIZE, NULL, pcStringCurrentLogDirectoryName, pcStringLogDirectoryNameA, pdPASS, SYS_STATUS_DOWN, vTaskDelete(), xDataLogHndl, xDataLogStatus, xLogToFileHndl, xQueueCreate(), and xTaskCreate.

Referenced by portTASK_FUNCTION().

static portTASK_FUNCTION ( vDataLogTask  ,
pvParameters   
)
static
static portTASK_FUNCTION ( vLogToFileTask  ,
pvParameters   
)
static
void prv_vdatalog_pool_log_free ( void *  pxLog)

Mark a data log structure from the pool of logs as free.

Parameters
pxLogpointer on the data log structure to free.

References DATALOG_INVALID_LOGSRC_ID.

Referenced by pxdatalog_log_alloc_init().

static void prv_vsave_logs ( int  fd_current_logfile)
static

If the log file descriptor is valid, pop the oldest logs from the logs queue into the current log file until the low threshold of the logs queue is reached; then close the current log file. If the log file descriptor is invalid, simply pop the oldest logs from the logs queue until the low threshold of the logs queue is reached.

Parameters
fd_current_logfileThe file descriptor of the current log file.

References acLogString, close(), DATALOG_LOGSQUEUE_LOTHRESHOLD, FS_DATE_LAST_WRITE, nav_file_dateset(), pcTempoDate, pdTRUE, uxNbMsgsInLogsQueue, uxQueueMessagesWaiting(), v_cptime_GetDateInFatStringFormat(), vdatalog_log_free(), vdatalog_make_logstring(), vTaskDelayUntil(), write(), xQueueReceive, and xTaskGetTickCount().

Referenced by portTASK_FUNCTION().

xLogDef* pxdatalog_log_alloc_init ( bool  bAllocType)

Allocate and init a xLogDef structure.

Parameters
bAllocTypeAllocation type (dynamic or static(FromISR))
Returns
Pointer on an xLogDef structure or NULL if the alloc failed.

References DATALOG_ALLOC_DYNAMIC, DATALOG_INVALID_LOGSRC_ID, DATALOG_LOGSQUEUE_HITHRESHOLD, NULL, LogDef::pcStringLog, LogDef::pfFreeLogStruct, LogDef::pfFreeStringLog, prv_vdatalog_pool_log_free(), pvPortMalloc(), LogDef::rawdate, u8FreeLogIdx, vPortFree(), and xcptime_LocalTime.

Referenced by e_actuator_cmd_get_value(), e_sensor_cmd_get_value(), and v_datalog_AddSensorLog().

int sprintf ( char *  out,
const char *  format,
  ... 
)
void v_datalog_AddSensorLog ( eLogSourceId  SensorId)

Get the current value of a sensor and log it in the xLogsQueue.

Parameters
SensorIdThe sensor id of the sensor to get the value from.

References b_sensor_get_value(), DATALOG_ALLOC_DYNAMIC, DATALOG_LOGSQUEUE_HITHRESHOLD, errQUEUE_FULL, LogDef::id, NULL, pxdatalog_log_alloc_init(), uxNbMsgsInLogsQueue, uxQueueMessagesWaiting(), vdatalog_log_free(), vTaskResume(), vTaskSuspend(), xLogToFileHndl, and xQueueSend.

Referenced by portTASK_FUNCTION().

void vdatalog_log_free ( xLogDef pxLog)

Free a data log structure and its content.

Parameters
pxLogpointer on the data log structure to free.

References NULL, LogDef::pcStringLog, LogDef::pfFreeLogStruct, and LogDef::pfFreeStringLog.

Referenced by e_actuator_cmd_get_value(), e_sensor_cmd_get_value(), prv_vsave_logs(), v_datalog_AddSensorLog(), and x_datalog_AddLogFromISR().

void vdatalog_make_logstring ( xLogDef pxLog,
signed portCHAR *  pcLogString 
)

Build a log string from a xLogDef log structure.

Parameters
pxLogPointer on a data log structure
pcLogStringAllocated DATALOG_LOG_MAXSIZE string to fill.

References acLogSourceName, LogDef::id, LogDef::pcStringLog, LogDef::rawdate, and sprintf().

Referenced by e_actuator_cmd_get_value(), e_sensor_cmd_get_value(), and prv_vsave_logs().

portBASE_TYPE x_datalog_AddLogFromISR ( xLogDef pxLog)

Add a log in the xLogsQueue, from within an ISR.

Parameters
pxLogThe log to add to the xLogsQueue.
Note
This function must be called last by an ISR (Interrupt Service Routine) associated with an event-based sensor.

References DATALOG_LOGSQUEUE_SIZE, NULL, pdFALSE, portENTER_CRITICAL, portEXIT_CRITICAL, uxNbMsgsInLogsQueue, vdatalog_log_free(), and xQueueSendFromISR.

Referenced by prvjoystick_ISR_NonNakedBehaviour(), and prvpushb_ISR_NonNakedBehaviour().

eStatus xDataLog_GetStatus ( void  )

Return the status of the datalog module.

Returns
one of the eStatus enum value.

References xDataLogStatus.

char acLogFileName[25]
static

The name of the current logfile, always "LOG/mmddyy_hhmm.log".
Thus, allocating 20Bytes for this string is enough.

Referenced by b_datalog_set_current_log_drive_toDF(), b_datalog_set_current_log_drive_toSDMMC(), portTASK_FUNCTION(), and prv_xopen_current_logfile().

char* acLogSourceName[12]
Initial value:
= { "temp", "pot", "light", "pb1", "pb2", "pb3", "js",
"ledb1", "ledb2", "ledm1", "ledm2", "lcd" }

Array of nicknames

Referenced by e_actuator_cmd_get_value(), e_actuator_cmd_set_value(), e_sensor_cmd_get_config(), e_sensor_cmd_get_value(), e_sensor_cmd_set_config(), and vdatalog_make_logstring().

char acLogString[DATALOG_LOG_MAXSIZE]
static

Pre-allocated memory space to build a log.

Referenced by prv_vsave_logs().

char acPreviousLogFileName[25]
static

Pool of xLogDef structures(FIFO) used for log allocation from ISR.

const signed portCHAR* pcStringCurrentLogDirectoryName

The <drive letter>:/LOG directory where the logs file are currently stored.

Referenced by b_datalog_set_current_log_drive_toDF(), b_datalog_set_current_log_drive_toSDMMC(), bdatalog_start(), and prv_xopen_current_logfile().

const signed portCHAR* const pcStringLogDirectoryNameA = (signed portCHAR *)"A:/LOG"

"\<drive letter>:/LOG" strings.

Referenced by b_datalog_set_current_log_drive_toDF(), and bdatalog_start().

const signed portCHAR* const pcStringLogDirectoryNameB = (signed portCHAR *)"B:/LOG"
char pcTempoDate[17]
static

Temporary array to set the current date of a log file.

Referenced by config_file_set_value(), e_fscmds_shell_mkdir(), e_fscmds_touch(), prv_append(), prv_vsave_logs(), and prv_xopen_current_logfile().

U8 u8FreeLogIdx = 0
static

Pointer on the next free log in the circular pool of xLogDef structures.

Referenced by pxdatalog_log_alloc_init().

unsigned portBASE_TYPE uxNbMsgsInLogsQueue = 0
static

The number of logs in xLogsQueue.

Referenced by prv_vsave_logs(), v_datalog_AddSensorLog(), and x_datalog_AddLogFromISR().

xTaskHandle xDataLogHndl = NULL
static

Data logger task handle.

Referenced by bdatalog_start().

eStatus xDataLogStatus = SYS_STATUS_STARTUP
static

The data logger module status.

Referenced by bdatalog_start(), portTASK_FUNCTION(), and xDataLog_GetStatus().

xSemaphoreHandle xLOGMutex

The datalog system mutex.

The LOG system mutex.

xQueueHandle xLogsQueue = 0
static

The handle of the queue of data logs.

xTaskHandle xLogToFileHndl = NULL
static

LogToFile task handle.

Referenced by bdatalog_start(), and v_datalog_AddSensorLog().