Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
FileManager.cpp
- Committer:
- Lucyjungz
- Date:
- 2016-05-18
- Revision:
- 12:0045fca3c160
- Parent:
- 11:e21d4c5bfd1b
- Child:
- 13:d83e2dcc882d
File content as of revision 12:0045fca3c160:
#include "mbed.h"
#include "FileManager.h"
#include "SDFileSystem.h"
char m_GpsInterval[XMLTEXT_SIZE]; // GPS Interval
char m_DataInterval[XMLTEXT_SIZE]; // Data Interval
Variable_Data_TypeDef m_varList[MAX_VAR]; // Variable List
unsigned int m_amountVarList = 0; // Amount of variable list
#ifdef LED_SDCARD
DigitalOut ledStatus(LED_SDCARD);
#else
DigitalOut ledStatus(NC);
#endif
/* ############### Static function prototype ################## */
static void FileManager_RemoveSpaces(char* s , int size);
static void FileManager_GetXmlText(char *str, char *ret);
static void FileManager_GenerateFileNameWithTime(time_t timestamp, char * file_name);
static void FileManager_SetLedStatus(bool on);
/**
* @brief Utility function to Remove space charector from given char array
* @note
* @param char array to process remove spaces
* @param size of char array
* @retval space removed char array
*/
static void FileManager_RemoveSpaces(char* s , int size)
{
char* cpy = s; // an alias to iterate through s without moving s
char* temp = s;
for (int i = 0 ; i < size ; i++) {
if (*cpy != ' ')
*temp++ = *cpy;
cpy++;
}
*temp = 0;
return;
}
/**
* @brief Utility function to get XML tag
* @note Only First tag will be returned
* @param char array to get XML Text
* @param char array to be populate XML text
* @retval XML text
*/
static void FileManager_GetXmlText(char *str, char *ret)
{
int size = strlen(str);
int i;
bool begin_text = false;
char * ret_addr = ret;
/* initialized our return value */
memset (ret,' ',XMLTEXT_SIZE);
/* Loop to check XML tag symbols */
for(i = 0; i < size ; i++) {
if (*str == '>') {
/* Found begining of the tag */
begin_text = true;
} else if (begin_text && *str == '<') {
/* Reach the end of text message */
begin_text = false;
break;
} else if (begin_text && *str != ' ') {
/* Populate the return value */
*ret = *str;
ret++;
}
/* Move to next char */
str++;
}
/* Remove space from return value */
FileManager_RemoveSpaces(ret_addr, XMLTEXT_SIZE);
}
/**
* @brief Utility function to get File Name with Date
* @note Format file will be YYYY-MM-DD.filename
* @param timestamp - time structure to get Date
* @param file_name - char array to file name
* @retval renamed file name
*/
static void FileManager_GenerateFileNameWithTime(time_t timestamp, char * file_name)
{
char str[5];
struct tm * ptm;
/* Convert timestamp to readable format */
ptm = localtime ( ×tamp );
/* Replacing YYYY to the converted year */
sprintf(str,"%04d", ptm->tm_year-100 + 2000);
memcpy(&file_name[4], str, 4);
/* Replacing MM to converted month */
sprintf(str,"%02d", ptm->tm_mon+1);
memcpy(&file_name[9], str, 2);
/* Replacing DD to converted date */
sprintf(str,"%02d", ptm->tm_mday);
memcpy(&file_name[12], str, 2);
}
/**
* @brief Function to perform read setup file
* @note filename must be defined in FileManager.h, GPS/ Data interval will be stored in dedicated variable
* @param None
* @retval None
*/
void FileManager_ReadSetupFile()
{
/* Open file in reading mode */
FILE *fp = fopen(SETUP_FILE_NAME, "r");
if (fp == NULL) {
/* In case of error, print the message */
printf("\nError! Unable to open file! %s \n", SETUP_FILE_NAME);
/* Indicate LED Status (OFF)*/
FileManager_SetLedStatus(false);
} else {
/* Initialized state */
ReadingFileState state = STATE_FINDING;
/* Allocate buffer for reading file */
char buf[1024];
/* Indicate LED Status (ON)*/
FileManager_SetLedStatus(true);
/* Read line from the file */
while (fgets(buf, sizeof(buf), fp) != NULL) {
/* Check the TAG */
if (strstr (buf,DATA_TAG))
{
/* Found the DATA TAG */
state = STATE_FOUND_DATA;
} else if (strstr (buf,GPS_TAG))
{
/* Found GPS TAG */
state = STATE_FOUND_GPS;
} else if (strstr (buf,UPDATE_INTERVAL_TAG))
{
/* Found Interval TAG */
if (state == STATE_FOUND_GPS)
{
/* Get XML text for GPS Interval */
FileManager_GetXmlText(buf, m_GpsInterval);
#if DEBUG
printf("\r\n-found GPS interval %s ", m_GpsInterval);
#endif
/* Set state to indicate that we are finding */
state = STATE_FINDING;
}
else if(state == STATE_FOUND_DATA)
{
/* Get XML text for Data Interval */
FileManager_GetXmlText(buf, m_DataInterval);
#if DEBUG
printf("\r\n-found Data interval %s ", m_DataInterval);
#endif
/* Set state to indicate that we are finding */
state = STATE_FINDING;
}
}
}
/* Ensure file is closed */
fclose(fp);
}
}
/**
* @brief Function to log GPS Data
* @note
* @param timestamp - time structure to get Date
* @param lat - char array for lattitude
* @param longti - char array for longtitude
* @retval None
*/
void FileManager_LogGPSData(time_t timestamp ,char lat[], char longti[])
{
/* Get File name */
char file_name[] = GPS_LOG_FILE_NAME;
/* Generate file name with time stamp */
FileManager_GenerateFileNameWithTime(timestamp,file_name);
/* Open file with "APPEND" mode */
FILE *fp = fopen(file_name, "a");
if (fp == NULL)
{
/* if it can't open the file then print error message */
printf("Error! Unable to open file %s!\n",file_name);
/* Indicate LED Status (OFF)*/
FileManager_SetLedStatus(false);
}
else
{
/* Indicate LED Status (ON)*/
FileManager_SetLedStatus(true);
/* Print some message for information */
printf("\r\n Writing to Gps Log File (%s)....",file_name);
/* Write the line with lattitude and longtitude */
fprintf(fp, "%d,%s,%s\n",timestamp,lat,longti);
printf("Done");
/* Close file once it done */
fclose(fp);
}
}
/**
* @brief Function to log RMS Data
* @note sequence must be in the same order with variableList
* @param timestamp - time structure to get Date
* @param var - float array to be log in the file
* @param size - size of the float array
* @retval None
*/
void FileManager_LogRMSData(time_t timestamp ,float * var, int size)
{
/* Get File name */
char file_name[] = RTL_LOG_FILE_NAME;
/* Generate File name with timestamp */
FileManager_GenerateFileNameWithTime(timestamp,file_name);
if (!FileManager_IsFileExist(file_name))
{
/* If file is not exist, log the header */
FileManager_LogRMSHeader(timestamp);
}
/* Open file with "APPEND" mode */
FILE *fp = fopen(file_name, "a");
if (fp == NULL)
{
/* In case of error, print the error message */
printf("Error! Unable to open file %s!\n",file_name);
/* Indicate LED Status (OFF)*/
FileManager_SetLedStatus(false);
}
else
{
/* Indicate LED Status (ON)*/
FileManager_SetLedStatus(true);
/* Print some message for information */
printf("\r\n Writing to Log File (%s)....",file_name);
/* Write timestamp */
fprintf(fp, "%d",timestamp);
/* Write MSecond */
fprintf(fp, "%d",0);
/* Write variable data */
for(int i = 0; i < size; i++)
{
fprintf(fp, ",%f",var[i]);
}
/* Write new line as we done */
fprintf(fp, "\n");
printf("Done");
/* Close the file */
fclose(fp);
}
}
/**
* @brief Function to log RMS Header
* @note sequence must be in the same order with variableList
* @param timestamp - time structure to get Date
* @retval None
*/
void FileManager_LogRMSHeader(time_t timestamp)
{
/* Get File name */
char file_name[] = RTL_LOG_FILE_NAME;
/* Generate file name with time */
FileManager_GenerateFileNameWithTime(timestamp,file_name);
/* Open file in append mode */
FILE *fp = fopen(file_name, "a");
if (fp == NULL)
{
/* In case of error, print the error message */
printf("Error! Unable to open file %s!\n",file_name);
/* Indicate LED Status (OFF)*/
FileManager_SetLedStatus(false);
}
else
{
/* Indicate LED Status (ON)*/
FileManager_SetLedStatus(true);
/* opened file so can write */
printf("\r\n Writing to Log File (%s)....",file_name);
/* Write the header to the file */
fprintf(fp, "%s",RMS_HEADER_TIME);
fprintf(fp, "%s",RMS_HEADER_MSECOND);
for(int i = 0; i < m_amountVarList; i++)
{
fprintf(fp, ",%s",m_varList[i].varName);
}
/* Write new line as done */
fprintf(fp, "\n");
printf("Done");
/* Close the file */
fclose(fp);
}
}
/**
* @brief Function to log Mini RMS System Data
* @note this file is only for debug
* @param gps_interval -
* @retval None
*/
void FileManager_LogSystemData(float gps_interval)
{
/* Open the file in append mode */
FILE *fp = fopen(MINIRMS_LOG_FILE_NAME, "a");
if (fp == NULL) {
/* In case of error, print the msg */
printf("Error! Unable to open file!\n");
/* Indicate LED Status (OFF)*/
FileManager_SetLedStatus(false);
}
else
{
/* Indicate LED Status (ON)*/
FileManager_SetLedStatus(true);
/* Write some message, which is TBD */
fprintf(fp, "\nStart Mini-RMS System with Gps Interval = %f",gps_interval);
fclose(fp); // ensure you close the file after writing
}
}
/**
* @brief Utility to delete file
* @note
* @param filename - file name to be deleted
* @retval None
*/
void FileManager_Deletefile(char filename[])
{
printf("Deleting file '%s'...",filename);
FILE *fp = fopen(filename, "r"); // try and open file
if (fp != NULL) { // if it does open...
fclose(fp); // close it
remove(filename); // and then delete
printf("Done!\n");
}
// if we can't open it, it doesn't exist and so we can't delete it
}
/**
* @brief Utility to check file available
* @note
* @param filename - file name to be checked
* @retval true - file is exist , false - file is not exist
*/
bool FileManager_IsFileExist(char filename[])
{
bool exist = false;
FILE *fp = fopen(filename, "r"); // try and open file
if (fp != NULL) { // if it does open...
fclose(fp); // close it
exist = true;
}
return exist;
}
/**
* @brief Utility to get GPS Interval
* @note must be after read the setup file
* @param None
* @retval GPS interval
*/
int FileManager_GPSInterval()
{
/* Return interval in int */
return atoi( m_GpsInterval );
}
/**
* @brief Utility to get Data Interval
* @note must be after read the setup file
* @param None
* @retval Data interval
*/
int FileManager_DataInterval()
{
/* Return interval in int */
return atoi( m_DataInterval );
}
/**
* @brief Function to read the variable list file
* @note Recommended to call this function at initilization phase
* @param None
* @retval pointer to variable list
*/
Variable_Data_TypeDef * FileManager_ReadVarFile()
{
/* Open the file with reading mode */
FILE *fp = fopen(VARIABLE_FILE_NAME, "r");
if (fp == NULL)
{
/* if it can't open the file then print error message */
printf("\nError! Unable to open file! %s \n", VARIABLE_FILE_NAME);
/* Indicate LED Status (OFF)*/
FileManager_SetLedStatus(false);
return NULL;
}
else
{
/* Indicate LED Status (ON)*/
FileManager_SetLedStatus(true);
/* Allocate buffer for reading */
char buf[1024];
int index = 0;
/* Initialize return value */
memset(m_varList, ' ', sizeof(m_varList));
/* Read line from the file */
while (fgets(buf, sizeof(buf), fp) != NULL)
{
/* Check the TAG */
if (strstr (buf,VAR_NAME_TAG))
{
/* Found variable TAG, populate it*/
FileManager_GetXmlText(buf , m_varList[index].varName);
}
else if (strstr (buf,VAR_ADDR_TAG))
{
/* Found variable address TAG, populate it*/
FileManager_GetXmlText(buf , m_varList[index].varAddress);
}
else if (strstr (buf,VAR_TYPE_TAG))
{
/* Found variable type TAG, populate it*/
FileManager_GetXmlText(buf , m_varList[index].varType);
}
else if (strstr (buf,VAR_LSB1_TAG))
{
/* Found variable LSB1 TAG, populate it*/
FileManager_GetXmlText(buf , m_varList[index].varLSB1);
}
else if (strstr (buf,VAR_LSB2_TAG))
{
/* Found variable LSB2 TAG, populate it*/
FileManager_GetXmlText(buf , m_varList[index].varLSB2);
}
else if (strstr (buf,VAR_BITMASK_TAG))
{
/* Found variable BitMask TAG, populate it*/
FileManager_GetXmlText(buf , m_varList[index].varBitMask);
}
else if (strstr (buf,VAR_UNIT_TAG))
{
/* Found variable unit TAG, populate it*/
FileManager_GetXmlText(buf , m_varList[index].varUnit);
index++;
}
}
/* Close File */
fclose(fp);
/* Populate amount */
m_amountVarList = index;
/* Return variable list */
return m_varList;
}
}
/**
* @brief Function to amount of variable for data logging
* @note Must be called after readVarFile
* @param None
* @retval amount of variable list
*/
int FileManager_GetAmountVarList()
{
return m_amountVarList;
}
/**
* @brief Function to get variable list
* @note Must be called after readVarFile
* @param None
* @retval pointer to variable list
*/
Variable_Data_TypeDef * FileManager_GetVarList()
{
return m_varList;
}
/**
* @brief Utility to play with LED status
* @note Libary user need to assign proper PinName to LED_SDCARD
* @param on - True for turning LED ON, otherwise LED off
* @retval None
*/
static void FileManager_SetLedStatus(bool on)
{
/* Check LED Connection */
if (ledStatus.is_connected())
{
/* Set LED regarding to given argment */
ledStatus = on;
}
#if DEBUG
else
{
printf("\r\nSDCard LED is connected !!");
}
#endif
}
