File manager
Diff: FileManager.cpp
- Revision:
- 3:6e08d0bba1bb
- Parent:
- 2:18e004a47f52
- Child:
- 4:ec62bf823914
--- a/FileManager.cpp Tue May 10 11:09:47 2016 +0000 +++ b/FileManager.cpp Sun May 15 09:56:42 2016 +0000 @@ -2,15 +2,25 @@ #include "FileManager.h" #include "SDFileSystem.h" -char m_GpsInterval[XMLTEXT_SIZE]; -char m_DataInterval[XMLTEXT_SIZE]; -Variable_Data_TypeDef m_varList[MAX_VAR]; -unsigned int m_amountVarList = 0; +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 removeSpaces(char* s , int size); static void getXmlText(char *str, char *ret); static void 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 removeSpaces(char* s , int size) { char* cpy = s; // an alias to iterate through s without moving s @@ -24,161 +34,283 @@ *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 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 */ 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 generateFileNameWithTime(time_t timestamp, char * file_name) { char str[5]; struct tm * ptm; - ptm = localtime ( ×tamp ); + /* 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 readSetupFile() { - // now open file for reading + /* Open file in reading mode */ FILE *fp = fopen(SETUP_FILE_NAME, "r"); - if (fp == NULL) { // if it can't open the file then print error message + if (fp == NULL) { + /* In case of error, print the message */ printf("\nError! Unable to open file! %s \n", SETUP_FILE_NAME); - } else { // opened file so can write -// fscanf(fp, "%d",&stored_top_score); // ensure data type matches - note address operator (&) -// serial.printf("Read %d from file.\n",stored_top_score); + } 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) { - if (strstr (buf,DATA_TAG)) { + + /* Check the TAG */ + if (strstr (buf,DATA_TAG)) + { + /* Found the DATA TAG */ state = STATE_FOUND_DATA; - } else if (strstr (buf,GPS_TAG)) { + } else if (strstr (buf,GPS_TAG)) + { + /* Found GPS TAG */ state = STATE_FOUND_GPS; - } else if (strstr (buf,UPDATE_INTERVAL_TAG)) { - if (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 */ getXmlText(buf, m_GpsInterval); printf("\r\n-found GPS interval %s ", m_GpsInterval); state = STATE_FINDING; - } else if(state == STATE_FOUND_DATA) { + } + else if(state == STATE_FOUND_DATA) + { + /* Get XML text for Data Interval */ getXmlText(buf, m_DataInterval); printf("\r\n-found Data interval %s ", m_DataInterval); state = STATE_FINDING; } } } - fclose(fp); // ensure you close the file after reading + /* 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 logGPSData(time_t timestamp ,char lat[], char longti[]) { + /* Get File name */ char file_name[] = GPS_LOG_FILE_NAME; + /* Generate file name with time stamp */ 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 + 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 { // opened file so can write + } + else { + /* Print some message for information */ printf("\r\n Writing to Gps Log File (%s)....",file_name); - fprintf(fp, "%d,%s,%s\n",timestamp,lat,longti); // print formatted string to file (CSV) + /* Write the line with lattitude and longtitude */ + fprintf(fp, "%d,%s,%s\n",timestamp,lat,longti); printf("Done"); - fclose(fp); // ensure you close the file after writing + /* 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 logRMSData(time_t timestamp ,float * var, int size) { + /* Get File name */ char file_name[] = RTL_LOG_FILE_NAME; + /* Generate File name with timestamp */ generateFileNameWithTime(timestamp,file_name); if (!is_file_exist(file_name)) { + /* If file is not exist, log the header */ logRMSHeader(timestamp); } + /* 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 + 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 + } + else + { + /* Print some message for information */ printf("\r\n Writing to Log File (%s)....",file_name); - fprintf(fp, "%d",timestamp); // print Timestamp + /* Write timestamp */ + fprintf(fp, "%d",timestamp); + /* Write variable data */ for(int i = 0; i < size; i++) { - fprintf(fp, ",%f",var[i]); // print variable list + fprintf(fp, ",%f",var[i]); } - fprintf(fp, "\n"); // print new line + /* 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 logRMSHeader(time_t timestamp) +{ + /* Get File name */ + char file_name[] = RTL_LOG_FILE_NAME; + + /* Generate file name with time */ + 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 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 } } -void logRMSHeader(time_t timestamp) -{ - char file_name[] = RTL_LOG_FILE_NAME; - - generateFileNameWithTime(timestamp,file_name); - - - 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 { // opened file so can write - printf("\r\n Writing to Log File (%s)....",file_name); - - fprintf(fp, "%s",RMS_HEADER_TIME); // print variable list - for(int i = 0; i < m_amountVarList; i++) - { - fprintf(fp, ",%s",m_varList[i].varName); // print variable list - } - fprintf(fp, "\n"); // print new line - - printf("Done"); - fclose(fp); // ensure you close the file after writing - } -} -void logSystemData(float gps_interval) -{ - FILE *fp = fopen(MINIRMS_LOG_FILE_NAME, "a"); - - if (fp == NULL) { // if it can't open the file then print error message - printf("Error! Unable to open file!\n"); - } else { // opened file so can write - fprintf(fp, "\nStart Mini-RMS System with Gps Interval = %f",gps_interval); // ensure data type matches - 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 delete_file(char filename[]) { printf("Deleting file '%s'...",filename); @@ -190,6 +322,12 @@ } // 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 is_file_exist(char filename[]) { bool exist = false; @@ -201,49 +339,100 @@ return exist; } +/** + * @brief Utility to get GPS Interval + * @note must be after read the setup file + * @param None + * @retval GPS interval + */ int GPSInterval() { - //Return whether or not CRC is enabled + /* 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 DataInterval() { - //Return whether or not CRC is enabled + /* 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 * readVarFile() { - // now open file for reading + /* 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 + 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 + } + 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)); - while (fgets(buf, sizeof(buf), fp) != NULL) { - if (strstr (buf,VAR_NAME_TAG)) { + + /* 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*/ getXmlText(buf , m_varList[index].varName); - } else if (strstr (buf,VAR_ADDR_TAG)) { + } + else if (strstr (buf,VAR_ADDR_TAG)) + { + /* Found variable address TAG, populate it*/ getXmlText(buf , m_varList[index].varAddress); index++; } } - fclose(fp); // ensure you close the file after reading + /* 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 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 * getVarList() { return m_varList;