File manager

Dependencies:   SDFileSystem

Dependents:   RwSDCard_Xml_GPS

Revision:
14:4ba6147f067b
Parent:
13:d83e2dcc882d
Child:
15:b63a539c3754
diff -r d83e2dcc882d -r 4ba6147f067b FileManager.cpp
--- a/FileManager.cpp	Wed May 18 12:55:07 2016 +0000
+++ b/FileManager.cpp	Thu May 19 10:44:35 2016 +0000
@@ -1,10 +1,22 @@
+/**
+  ******************************************************************************
+  * @file    FileManager.cpp
+  * @author  Narut T
+  * @version V1
+  * @date    19/05/2016
+  * @brief   File Manager for managing file system in SD Card
+  ******************************************************************************/
+
 #include "mbed.h"
 #include "FileManager.h"
 #include "SDFileSystem.h"
 #include "main.h"
 
-char m_GpsInterval[XMLTEXT_SIZE];           // GPS Interval
-char m_DataInterval[XMLTEXT_SIZE];          // Data Interval
+char m_StrGpsInterval[XMLTEXT_SIZE];           // GPS Interval
+char m_StrDataInterval[XMLTEXT_SIZE];          // Data Interval
+
+uint32_t m_GpsInterval;
+uint32_t m_DataInterval;
 Variable_Data_TypeDef m_varList[MAX_VAR];   // Variable List
 unsigned int m_amountVarList = 0;           // Amount of variable list
 
@@ -16,10 +28,10 @@
 
 /* ###############  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);
+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
@@ -28,7 +40,7 @@
  * @param size of char array
  * @retval space removed char array
  */
-static void FileManager_RemoveSpaces(char* s , int size)
+static void FILEMANAGER_RemoveSpaces(char* s , int size)
 {
     char* cpy = s;  // an alias to iterate through s without moving s
     char* temp = s;
@@ -48,7 +60,7 @@
  * @param char array to be populate XML text
  * @retval XML text 
  */
-static void FileManager_GetXmlText(char *str, char *ret)
+static void FILEMANAGER_GetXmlText(char *str, char *ret)
 {
     int size = strlen(str);
     int i;
@@ -77,7 +89,7 @@
     }
     
     /* Remove space from return value */
-    FileManager_RemoveSpaces(ret_addr, XMLTEXT_SIZE);
+    FILEMANAGER_RemoveSpaces(ret_addr, XMLTEXT_SIZE);
 }
 /**
  * @brief Utility function to get File Name with Date
@@ -86,25 +98,25 @@
  * @param file_name - char array to file name 
  * @retval renamed file name
  */
-static void FileManager_GenerateFileNameWithTime(time_t timestamp, char * file_name)
+static void FILEMANAGER_GenerateFileNameWithTime(time_t timestamp, char * file_name)
 {
-    char str[5];
+    char str[RENAME_FILE_BUFFER_SIZE];
     struct tm * ptm;
     
     /* Convert timestamp to readable format */
     ptm = localtime ( &timestamp );
     
     /* Replacing YYYY to the converted year */  
-    sprintf(str,"%04d", ptm->tm_year-100 + 2000);
-    memcpy(&file_name[4], str, 4);
+    sprintf(str,"%04d", ptm->tm_year + YEAR_4DIGITS_OFFSET);
+    memcpy(&file_name[TIMESTAMP_YEAR_OFFSET], str, TIMESTAMP_YEAR_SIZE);
     
     /* Replacing MM to converted month */
     sprintf(str,"%02d", ptm->tm_mon+1);
-    memcpy(&file_name[9], str, 2);
+    memcpy(&file_name[TIMESTAMP_MONTH_OFFSET], str, TIMESTAMP_MONTH_SIZE);
     
     /* Replacing DD to converted date */
     sprintf(str,"%02d", ptm->tm_mday);
-    memcpy(&file_name[12], str, 2); 
+    memcpy(&file_name[TIMESTAMP_DATE_OFFSET], str,TIMESTAMP_DATE_SIZE); 
 }
 /**
  * @brief Function to perform read setup file
@@ -112,7 +124,7 @@
  * @param None
  * @retval None
  */
-void FileManager_ReadSetupFile()
+void FILEMANAGER_ReadSetupFile()
 {
     /* Open file in reading mode */
     FILE *fp = fopen(SETUP_FILE_NAME, "r");
@@ -122,17 +134,17 @@
         printf("\nError! Unable to open file! %s \n", SETUP_FILE_NAME);
         
         /* Indicate LED Status (OFF)*/
-        FileManager_SetLedStatus(false);
+        FILEMANAGER_SetLedStatus(false);
     } else { 
 
         /* Initialized state */
         ReadingFileState  state = STATE_FINDING;
         
         /* Allocate buffer for reading file */
-        char buf[1024];
+        char buf[READ_FILE_BUFFER_SIZE];
         
         /* Indicate LED Status (ON)*/
-        FileManager_SetLedStatus(true);
+        FILEMANAGER_SetLedStatus(true);
         
         /* Read line from the file */
         while (fgets(buf, sizeof(buf), fp) != NULL) {
@@ -152,9 +164,10 @@
                 if (state == STATE_FOUND_GPS) 
                 {
                     /* Get XML text for GPS Interval */
-                    FileManager_GetXmlText(buf, m_GpsInterval);
+                    FILEMANAGER_GetXmlText(buf, m_StrGpsInterval);
+                    m_GpsInterval = atoi(m_StrGpsInterval);
                     #if DEBUG
-                    printf("\r\n-found GPS interval %s ", m_GpsInterval);
+                    printf("\r\n-found GPS interval %s ", m_StrGpsInterval);
                     #endif
                     
                     /* Set state to indicate that we are finding */
@@ -163,10 +176,10 @@
                 else if(state == STATE_FOUND_DATA) 
                 {
                     /* Get XML text for Data Interval */
-                    FileManager_GetXmlText(buf, m_DataInterval);
-                    
+                    FILEMANAGER_GetXmlText(buf, m_StrDataInterval);
+                    m_DataInterval = atoi(m_StrDataInterval);
                     #if DEBUG
-                    printf("\r\n-found Data interval %s ", m_DataInterval);
+                    printf("\r\n-found Data interval %s ", m_StrDataInterval);
                     #endif
                     
                     /* Set state to indicate that we are finding */
@@ -186,13 +199,13 @@
  * @param longti    - char array for longtitude
  * @retval None
  */
-void FileManager_LogGPSData(time_t timestamp ,char lat[], char longti[])
+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);
+    FILEMANAGER_GenerateFileNameWithTime(timestamp,file_name);
     
     /* Open file with "APPEND" mode */
     FILE *fp  = fopen(file_name, "a");
@@ -203,12 +216,12 @@
         printf("Error! Unable to open file %s!\n",file_name);
         
         /* Indicate LED Status (OFF)*/
-        FileManager_SetLedStatus(false);
+        FILEMANAGER_SetLedStatus(false);
     } 
     else 
     {  
         /* Indicate LED Status (ON)*/
-        FileManager_SetLedStatus(true);
+        FILEMANAGER_SetLedStatus(true);
         
         /* Print some message for information */
         printf("\r\n Writing to Gps Log File (%s)....",file_name);
@@ -229,17 +242,17 @@
  * @param size      - size of the float array
  * @retval None
  */
-void FileManager_LogRMSData(time_t timestamp ,float * var, int size)
+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))
+    FILEMANAGER_GenerateFileNameWithTime(timestamp,file_name);
+    if (!FILEMANAGER_IsFileExist(file_name))
     {
         /* If file is not exist, log the header */
-        FileManager_LogRMSHeader(timestamp);
+        FILEMANAGER_LogRMSHeader(timestamp);
     }   
     /* Open file with "APPEND" mode */
     FILE *fp  = fopen(file_name, "a");
@@ -250,21 +263,18 @@
         printf("Error! Unable to open file %s!\n",file_name);
         
         /* Indicate LED Status (OFF)*/
-        FileManager_SetLedStatus(false);
+        FILEMANAGER_SetLedStatus(false);
     } 
     else
     {  
         /* Indicate LED Status (ON)*/
-        FileManager_SetLedStatus(true);
+        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); 
+        fprintf(fp, "%d,%d",timestamp,0); 
 
         /* Write variable data */
         for(int i = 0; i < size; i++)
@@ -285,13 +295,13 @@
  * @param timestamp - time structure to get Date 
  * @retval None
  */
-void FileManager_LogRMSHeader(time_t timestamp)
+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);
+    FILEMANAGER_GenerateFileNameWithTime(timestamp,file_name);
     
     /* Open file in append mode */
     FILE *fp  = fopen(file_name, "a");
@@ -302,20 +312,29 @@
         printf("Error! Unable to open file %s!\n",file_name);
         
         /* Indicate LED Status (OFF)*/
-        FileManager_SetLedStatus(false);
+        FILEMANAGER_SetLedStatus(false);
     } 
     else 
     {  
         /* Indicate LED Status (ON)*/
-        FileManager_SetLedStatus(true);
+        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,%s",RMS_HEADER_TIME,RMS_HEADER_MSECOND);
         
-        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");
+        
+        /* Write the timestamp unit to the file  */
+        fprintf(fp, "-,-");
+        
         for(int i = 0; i < m_amountVarList; i++)
         {
             fprintf(fp, ",%s",m_varList[i].varName); 
@@ -335,7 +354,7 @@
  * @param gps_interval - 
  * @retval None
  */
-void FileManager_LogSystemData(float gps_interval)
+void FILEMANAGER_LogSystemData(float gps_interval)
 {
     /* Open the file in append mode */
     FILE *fp = fopen(MINIRMS_LOG_FILE_NAME, "a");
@@ -345,12 +364,12 @@
         printf("Error! Unable to open file!\n");
         
         /* Indicate LED Status (OFF)*/
-        FileManager_SetLedStatus(false);
+        FILEMANAGER_SetLedStatus(false);
     } 
     else 
     {
         /* Indicate LED Status (ON)*/
-        FileManager_SetLedStatus(true);
+        FILEMANAGER_SetLedStatus(true);
         
         /* Write some message, which is TBD */    
         fprintf(fp, "\nStart Mini-RMS System with Gps Interval = %f",gps_interval); 
@@ -363,7 +382,7 @@
  * @param filename - file name to be deleted
  * @retval None
  */
-void FileManager_Deletefile(char filename[])
+void FILEMANAGER_Deletefile(char filename[])
 {
     printf("Deleting file '%s'...",filename);
     FILE *fp = fopen(filename, "r");  // try and open file
@@ -380,7 +399,7 @@
  * @param filename - file name to be checked
  * @retval true - file is exist , false - file is not exist
  */
-bool FileManager_IsFileExist(char filename[])
+bool FILEMANAGER_IsFileExist(char filename[])
 {
     bool exist = false;
     FILE *fp = fopen(filename, "r");  // try and open file
@@ -393,25 +412,25 @@
 }
 /**
  * @brief Utility to get GPS Interval 
- * @note  must be after read the setup file
+ * @note  must be called after read the setup file
  * @param None
  * @retval GPS interval
  */
-int FileManager_GPSInterval()
+int FILEMANAGER_GPSInterval()
 {
     /* Return interval in int */
-    return atoi( m_GpsInterval );
+    return ( m_GpsInterval );
 }
 /**
  * @brief Utility to get Data Interval 
- * @note  must be after read the setup file
+ * @note  must be called after read the setup file
  * @param None
  * @retval Data interval
  */
-int FileManager_DataInterval()
+int FILEMANAGER_DataInterval()
 {
     /* Return interval in int */
-    return atoi( m_DataInterval );
+    return ( m_DataInterval );
 }
 /**
  * @brief Function to read the variable list file 
@@ -419,7 +438,7 @@
  * @param None
  * @retval pointer to variable list
  */
-Variable_Data_TypeDef * FileManager_ReadVarFile()
+Variable_Data_TypeDef * FILEMANAGER_ReadVarFile()
 {
     /* Open the file with reading mode */
     FILE *fp = fopen(VARIABLE_FILE_NAME, "r");
@@ -429,17 +448,17 @@
         /* 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);
+        FILEMANAGER_SetLedStatus(false);
         
         return NULL;
     } 
     else 
     {  
         /* Indicate LED Status (ON)*/
-        FileManager_SetLedStatus(true);
+        FILEMANAGER_SetLedStatus(true);
 
         /* Allocate buffer for reading  */
-        char buf[1024];
+        char buf[READ_FILE_BUFFER_SIZE];
         int index = 0;
         
         /* Initialize return value */
@@ -452,37 +471,37 @@
             if (strstr (buf,VAR_NAME_TAG)) 
             {
                 /* Found variable TAG, populate it*/
-                FileManager_GetXmlText(buf , m_varList[index].varName);
+                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);
+                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);
+                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);
+                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);
+                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);
+                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);
+                FILEMANAGER_GetXmlText(buf , m_varList[index].varUnit);
                 index++;
             }
 
@@ -503,7 +522,7 @@
  * @param None
  * @retval amount of variable list
  */
-int FileManager_GetAmountVarList()
+int FILEMANAGER_GetAmountVarList()
 {
     return m_amountVarList;
 }
@@ -513,17 +532,18 @@
  * @param None
  * @retval pointer to variable list
  */
-Variable_Data_TypeDef * FileManager_GetVarList()
+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)
+static void FILEMANAGER_SetLedStatus(bool on)
 {
     /* Check LED Connection  */
     if (ledStatus.is_connected())
@@ -534,8 +554,17 @@
     #if DEBUG
     else
     {
-        printf("\r\nSDCard LED is connected !!");
+        printf("\r\nSDCard LED is not connected !!");
     }
     #endif
 }
-
+/**
+ * @brief Function to init File System
+ * @note  Must be called during initialization
+ * @param 
+ * @retval 
+ */
+void FILEMANAGER_Init(void)
+{
+    SDFileSystem sd(A6, A5, A4, A0, DEVICE_NAME); // MOSI, MISO, SCK, CS
+}