File manager
FileManager.cpp
- Committer:
- Lucyjungz
- Date:
- 2016-05-15
- Revision:
- 4:ec62bf823914
- Parent:
- 3:6e08d0bba1bb
- Child:
- 5:7c513eee7b2b
File content as of revision 4:ec62bf823914:
#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 /* ############### 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); /** * @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); } else { /* Initialized state */ ReadingFileState state = STATE_FINDING; /* Allocate buffer for reading file */ char buf[1024]; /* 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); printf("\r\n-found GPS interval %s ", m_GpsInterval); state = STATE_FINDING; } else if(state == STATE_FOUND_DATA) { /* Get XML text for Data Interval */ FileManager_GetXmlText(buf, m_DataInterval); printf("\r\n-found Data interval %s ", m_DataInterval); 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); } else { /* 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); } else { /* Print some message for information */ printf("\r\n Writing to Log File (%s)....",file_name); /* Write timestamp */ fprintf(fp, "%d",timestamp); /* 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); } else { /* 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); 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"); } else { /* 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); return NULL; } else { /* opened file so can write */ /* 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); 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; }