File manager

Dependencies:   SDFileSystem

Dependents:   RwSDCard_Xml_GPS

Committer:
Lucyjungz
Date:
Sat May 21 14:35:10 2016 +0000
Revision:
15:b63a539c3754
Parent:
14:4ba6147f067b
Child:
16:c13dc589bb55
Bug fix log unit instead of variable name; SDFilename must be call in main function

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Lucyjungz 14:4ba6147f067b 1 /**
Lucyjungz 14:4ba6147f067b 2 ******************************************************************************
Lucyjungz 14:4ba6147f067b 3 * @file FileManager.cpp
Lucyjungz 14:4ba6147f067b 4 * @author Narut T
Lucyjungz 14:4ba6147f067b 5 * @version V1
Lucyjungz 14:4ba6147f067b 6 * @date 19/05/2016
Lucyjungz 14:4ba6147f067b 7 * @brief File Manager for managing file system in SD Card
Lucyjungz 14:4ba6147f067b 8 ******************************************************************************/
Lucyjungz 14:4ba6147f067b 9
Lucyjungz 1:1f1f2b99756b 10 #include "mbed.h"
nsrwsurasak 0:a27e0d3581d1 11 #include "FileManager.h"
nsrwsurasak 0:a27e0d3581d1 12 #include "SDFileSystem.h"
nsrwsurasak 13:d83e2dcc882d 13 #include "main.h"
nsrwsurasak 0:a27e0d3581d1 14
Lucyjungz 14:4ba6147f067b 15 char m_StrGpsInterval[XMLTEXT_SIZE]; // GPS Interval
Lucyjungz 14:4ba6147f067b 16 char m_StrDataInterval[XMLTEXT_SIZE]; // Data Interval
Lucyjungz 14:4ba6147f067b 17
Lucyjungz 14:4ba6147f067b 18 uint32_t m_GpsInterval;
Lucyjungz 14:4ba6147f067b 19 uint32_t m_DataInterval;
Lucyjungz 3:6e08d0bba1bb 20 Variable_Data_TypeDef m_varList[MAX_VAR]; // Variable List
Lucyjungz 3:6e08d0bba1bb 21 unsigned int m_amountVarList = 0; // Amount of variable list
Lucyjungz 3:6e08d0bba1bb 22
Lucyjungz 11:e21d4c5bfd1b 23 #ifdef LED_SDCARD
Lucyjungz 11:e21d4c5bfd1b 24 DigitalOut ledStatus(LED_SDCARD);
Lucyjungz 11:e21d4c5bfd1b 25 #else
Lucyjungz 11:e21d4c5bfd1b 26 DigitalOut ledStatus(NC);
Lucyjungz 11:e21d4c5bfd1b 27 #endif
Lucyjungz 11:e21d4c5bfd1b 28
Lucyjungz 3:6e08d0bba1bb 29 /* ############### Static function prototype ################## */
nsrwsurasak 0:a27e0d3581d1 30
Lucyjungz 14:4ba6147f067b 31 static void FILEMANAGER_RemoveSpaces(char* s , int size);
Lucyjungz 14:4ba6147f067b 32 static void FILEMANAGER_GetXmlText(char *str, char *ret);
Lucyjungz 14:4ba6147f067b 33 static void FILEMANAGER_GenerateFileNameWithTime(time_t timestamp, char * file_name);
Lucyjungz 14:4ba6147f067b 34 static void FILEMANAGER_SetLedStatus(bool on);
Lucyjungz 3:6e08d0bba1bb 35
Lucyjungz 3:6e08d0bba1bb 36 /**
Lucyjungz 3:6e08d0bba1bb 37 * @brief Utility function to Remove space charector from given char array
Lucyjungz 3:6e08d0bba1bb 38 * @note
Lucyjungz 3:6e08d0bba1bb 39 * @param char array to process remove spaces
Lucyjungz 3:6e08d0bba1bb 40 * @param size of char array
Lucyjungz 3:6e08d0bba1bb 41 * @retval space removed char array
Lucyjungz 3:6e08d0bba1bb 42 */
Lucyjungz 14:4ba6147f067b 43 static void FILEMANAGER_RemoveSpaces(char* s , int size)
nsrwsurasak 0:a27e0d3581d1 44 {
nsrwsurasak 0:a27e0d3581d1 45 char* cpy = s; // an alias to iterate through s without moving s
nsrwsurasak 0:a27e0d3581d1 46 char* temp = s;
nsrwsurasak 0:a27e0d3581d1 47
nsrwsurasak 0:a27e0d3581d1 48 for (int i = 0 ; i < size ; i++) {
nsrwsurasak 0:a27e0d3581d1 49 if (*cpy != ' ')
nsrwsurasak 0:a27e0d3581d1 50 *temp++ = *cpy;
nsrwsurasak 0:a27e0d3581d1 51 cpy++;
nsrwsurasak 0:a27e0d3581d1 52 }
nsrwsurasak 0:a27e0d3581d1 53 *temp = 0;
nsrwsurasak 0:a27e0d3581d1 54 return;
nsrwsurasak 0:a27e0d3581d1 55 }
Lucyjungz 3:6e08d0bba1bb 56 /**
Lucyjungz 3:6e08d0bba1bb 57 * @brief Utility function to get XML tag
Lucyjungz 3:6e08d0bba1bb 58 * @note Only First tag will be returned
Lucyjungz 3:6e08d0bba1bb 59 * @param char array to get XML Text
Lucyjungz 3:6e08d0bba1bb 60 * @param char array to be populate XML text
Lucyjungz 3:6e08d0bba1bb 61 * @retval XML text
Lucyjungz 3:6e08d0bba1bb 62 */
Lucyjungz 14:4ba6147f067b 63 static void FILEMANAGER_GetXmlText(char *str, char *ret)
nsrwsurasak 0:a27e0d3581d1 64 {
nsrwsurasak 0:a27e0d3581d1 65 int size = strlen(str);
nsrwsurasak 0:a27e0d3581d1 66 int i;
nsrwsurasak 0:a27e0d3581d1 67 bool begin_text = false;
nsrwsurasak 0:a27e0d3581d1 68 char * ret_addr = ret;
Lucyjungz 3:6e08d0bba1bb 69 /* initialized our return value */
nsrwsurasak 0:a27e0d3581d1 70 memset (ret,' ',XMLTEXT_SIZE);
nsrwsurasak 0:a27e0d3581d1 71
Lucyjungz 3:6e08d0bba1bb 72 /* Loop to check XML tag symbols */
nsrwsurasak 0:a27e0d3581d1 73 for(i = 0; i < size ; i++) {
nsrwsurasak 0:a27e0d3581d1 74
nsrwsurasak 0:a27e0d3581d1 75 if (*str == '>') {
Lucyjungz 3:6e08d0bba1bb 76 /* Found begining of the tag */
nsrwsurasak 0:a27e0d3581d1 77 begin_text = true;
nsrwsurasak 0:a27e0d3581d1 78 } else if (begin_text && *str == '<') {
Lucyjungz 3:6e08d0bba1bb 79 /* Reach the end of text message */
nsrwsurasak 0:a27e0d3581d1 80 begin_text = false;
nsrwsurasak 0:a27e0d3581d1 81 break;
nsrwsurasak 0:a27e0d3581d1 82 } else if (begin_text && *str != ' ') {
Lucyjungz 3:6e08d0bba1bb 83 /* Populate the return value */
nsrwsurasak 0:a27e0d3581d1 84 *ret = *str;
nsrwsurasak 0:a27e0d3581d1 85 ret++;
nsrwsurasak 0:a27e0d3581d1 86 }
Lucyjungz 3:6e08d0bba1bb 87 /* Move to next char */
nsrwsurasak 0:a27e0d3581d1 88 str++;
nsrwsurasak 0:a27e0d3581d1 89 }
Lucyjungz 3:6e08d0bba1bb 90
Lucyjungz 3:6e08d0bba1bb 91 /* Remove space from return value */
Lucyjungz 14:4ba6147f067b 92 FILEMANAGER_RemoveSpaces(ret_addr, XMLTEXT_SIZE);
nsrwsurasak 0:a27e0d3581d1 93 }
Lucyjungz 3:6e08d0bba1bb 94 /**
Lucyjungz 3:6e08d0bba1bb 95 * @brief Utility function to get File Name with Date
Lucyjungz 3:6e08d0bba1bb 96 * @note Format file will be YYYY-MM-DD.filename
Lucyjungz 3:6e08d0bba1bb 97 * @param timestamp - time structure to get Date
Lucyjungz 3:6e08d0bba1bb 98 * @param file_name - char array to file name
Lucyjungz 3:6e08d0bba1bb 99 * @retval renamed file name
Lucyjungz 3:6e08d0bba1bb 100 */
Lucyjungz 14:4ba6147f067b 101 static void FILEMANAGER_GenerateFileNameWithTime(time_t timestamp, char * file_name)
Lucyjungz 1:1f1f2b99756b 102 {
Lucyjungz 14:4ba6147f067b 103 char str[RENAME_FILE_BUFFER_SIZE];
Lucyjungz 1:1f1f2b99756b 104 struct tm * ptm;
Lucyjungz 1:1f1f2b99756b 105
Lucyjungz 3:6e08d0bba1bb 106 /* Convert timestamp to readable format */
Lucyjungz 3:6e08d0bba1bb 107 ptm = localtime ( &timestamp );
Lucyjungz 3:6e08d0bba1bb 108
Lucyjungz 3:6e08d0bba1bb 109 /* Replacing YYYY to the converted year */
Lucyjungz 14:4ba6147f067b 110 sprintf(str,"%04d", ptm->tm_year + YEAR_4DIGITS_OFFSET);
Lucyjungz 14:4ba6147f067b 111 memcpy(&file_name[TIMESTAMP_YEAR_OFFSET], str, TIMESTAMP_YEAR_SIZE);
Lucyjungz 1:1f1f2b99756b 112
Lucyjungz 3:6e08d0bba1bb 113 /* Replacing MM to converted month */
Lucyjungz 1:1f1f2b99756b 114 sprintf(str,"%02d", ptm->tm_mon+1);
Lucyjungz 14:4ba6147f067b 115 memcpy(&file_name[TIMESTAMP_MONTH_OFFSET], str, TIMESTAMP_MONTH_SIZE);
Lucyjungz 1:1f1f2b99756b 116
Lucyjungz 3:6e08d0bba1bb 117 /* Replacing DD to converted date */
Lucyjungz 1:1f1f2b99756b 118 sprintf(str,"%02d", ptm->tm_mday);
Lucyjungz 14:4ba6147f067b 119 memcpy(&file_name[TIMESTAMP_DATE_OFFSET], str,TIMESTAMP_DATE_SIZE);
Lucyjungz 1:1f1f2b99756b 120 }
Lucyjungz 3:6e08d0bba1bb 121 /**
Lucyjungz 3:6e08d0bba1bb 122 * @brief Function to perform read setup file
Lucyjungz 3:6e08d0bba1bb 123 * @note filename must be defined in FileManager.h, GPS/ Data interval will be stored in dedicated variable
Lucyjungz 3:6e08d0bba1bb 124 * @param None
Lucyjungz 3:6e08d0bba1bb 125 * @retval None
Lucyjungz 3:6e08d0bba1bb 126 */
Lucyjungz 14:4ba6147f067b 127 void FILEMANAGER_ReadSetupFile()
nsrwsurasak 0:a27e0d3581d1 128 {
Lucyjungz 3:6e08d0bba1bb 129 /* Open file in reading mode */
nsrwsurasak 0:a27e0d3581d1 130 FILE *fp = fopen(SETUP_FILE_NAME, "r");
nsrwsurasak 0:a27e0d3581d1 131
Lucyjungz 3:6e08d0bba1bb 132 if (fp == NULL) {
Lucyjungz 3:6e08d0bba1bb 133 /* In case of error, print the message */
nsrwsurasak 0:a27e0d3581d1 134 printf("\nError! Unable to open file! %s \n", SETUP_FILE_NAME);
Lucyjungz 11:e21d4c5bfd1b 135
Lucyjungz 11:e21d4c5bfd1b 136 /* Indicate LED Status (OFF)*/
Lucyjungz 14:4ba6147f067b 137 FILEMANAGER_SetLedStatus(false);
Lucyjungz 3:6e08d0bba1bb 138 } else {
nsrwsurasak 0:a27e0d3581d1 139
Lucyjungz 3:6e08d0bba1bb 140 /* Initialized state */
nsrwsurasak 0:a27e0d3581d1 141 ReadingFileState state = STATE_FINDING;
Lucyjungz 3:6e08d0bba1bb 142
Lucyjungz 3:6e08d0bba1bb 143 /* Allocate buffer for reading file */
Lucyjungz 14:4ba6147f067b 144 char buf[READ_FILE_BUFFER_SIZE];
Lucyjungz 3:6e08d0bba1bb 145
Lucyjungz 11:e21d4c5bfd1b 146 /* Indicate LED Status (ON)*/
Lucyjungz 14:4ba6147f067b 147 FILEMANAGER_SetLedStatus(true);
Lucyjungz 11:e21d4c5bfd1b 148
Lucyjungz 3:6e08d0bba1bb 149 /* Read line from the file */
nsrwsurasak 0:a27e0d3581d1 150 while (fgets(buf, sizeof(buf), fp) != NULL) {
Lucyjungz 3:6e08d0bba1bb 151
Lucyjungz 3:6e08d0bba1bb 152 /* Check the TAG */
Lucyjungz 3:6e08d0bba1bb 153 if (strstr (buf,DATA_TAG))
Lucyjungz 3:6e08d0bba1bb 154 {
Lucyjungz 3:6e08d0bba1bb 155 /* Found the DATA TAG */
nsrwsurasak 0:a27e0d3581d1 156 state = STATE_FOUND_DATA;
Lucyjungz 3:6e08d0bba1bb 157 } else if (strstr (buf,GPS_TAG))
Lucyjungz 3:6e08d0bba1bb 158 {
Lucyjungz 3:6e08d0bba1bb 159 /* Found GPS TAG */
nsrwsurasak 0:a27e0d3581d1 160 state = STATE_FOUND_GPS;
Lucyjungz 3:6e08d0bba1bb 161 } else if (strstr (buf,UPDATE_INTERVAL_TAG))
Lucyjungz 3:6e08d0bba1bb 162 {
Lucyjungz 3:6e08d0bba1bb 163 /* Found Interval TAG */
Lucyjungz 3:6e08d0bba1bb 164 if (state == STATE_FOUND_GPS)
Lucyjungz 3:6e08d0bba1bb 165 {
Lucyjungz 3:6e08d0bba1bb 166 /* Get XML text for GPS Interval */
Lucyjungz 14:4ba6147f067b 167 FILEMANAGER_GetXmlText(buf, m_StrGpsInterval);
Lucyjungz 14:4ba6147f067b 168 m_GpsInterval = atoi(m_StrGpsInterval);
Lucyjungz 8:5af4e12c43b2 169 #if DEBUG
Lucyjungz 14:4ba6147f067b 170 printf("\r\n-found GPS interval %s ", m_StrGpsInterval);
Lucyjungz 8:5af4e12c43b2 171 #endif
Lucyjungz 8:5af4e12c43b2 172
Lucyjungz 8:5af4e12c43b2 173 /* Set state to indicate that we are finding */
nsrwsurasak 0:a27e0d3581d1 174 state = STATE_FINDING;
Lucyjungz 3:6e08d0bba1bb 175 }
Lucyjungz 3:6e08d0bba1bb 176 else if(state == STATE_FOUND_DATA)
Lucyjungz 3:6e08d0bba1bb 177 {
Lucyjungz 3:6e08d0bba1bb 178 /* Get XML text for Data Interval */
Lucyjungz 14:4ba6147f067b 179 FILEMANAGER_GetXmlText(buf, m_StrDataInterval);
Lucyjungz 14:4ba6147f067b 180 m_DataInterval = atoi(m_StrDataInterval);
Lucyjungz 8:5af4e12c43b2 181 #if DEBUG
Lucyjungz 14:4ba6147f067b 182 printf("\r\n-found Data interval %s ", m_StrDataInterval);
Lucyjungz 8:5af4e12c43b2 183 #endif
Lucyjungz 8:5af4e12c43b2 184
Lucyjungz 8:5af4e12c43b2 185 /* Set state to indicate that we are finding */
nsrwsurasak 0:a27e0d3581d1 186 state = STATE_FINDING;
nsrwsurasak 0:a27e0d3581d1 187 }
nsrwsurasak 0:a27e0d3581d1 188 }
nsrwsurasak 0:a27e0d3581d1 189 }
Lucyjungz 3:6e08d0bba1bb 190 /* Ensure file is closed */
Lucyjungz 3:6e08d0bba1bb 191 fclose(fp);
nsrwsurasak 0:a27e0d3581d1 192 }
nsrwsurasak 0:a27e0d3581d1 193 }
Lucyjungz 3:6e08d0bba1bb 194 /**
Lucyjungz 3:6e08d0bba1bb 195 * @brief Function to log GPS Data
Lucyjungz 3:6e08d0bba1bb 196 * @note
Lucyjungz 3:6e08d0bba1bb 197 * @param timestamp - time structure to get Date
Lucyjungz 3:6e08d0bba1bb 198 * @param lat - char array for lattitude
Lucyjungz 3:6e08d0bba1bb 199 * @param longti - char array for longtitude
Lucyjungz 3:6e08d0bba1bb 200 * @retval None
Lucyjungz 3:6e08d0bba1bb 201 */
Lucyjungz 14:4ba6147f067b 202 void FILEMANAGER_LogGPSData(time_t timestamp ,char lat[], char longti[])
nsrwsurasak 0:a27e0d3581d1 203 {
Lucyjungz 3:6e08d0bba1bb 204 /* Get File name */
Lucyjungz 1:1f1f2b99756b 205 char file_name[] = GPS_LOG_FILE_NAME;
Lucyjungz 1:1f1f2b99756b 206
Lucyjungz 3:6e08d0bba1bb 207 /* Generate file name with time stamp */
Lucyjungz 14:4ba6147f067b 208 FILEMANAGER_GenerateFileNameWithTime(timestamp,file_name);
Lucyjungz 3:6e08d0bba1bb 209
Lucyjungz 3:6e08d0bba1bb 210 /* Open file with "APPEND" mode */
Lucyjungz 1:1f1f2b99756b 211 FILE *fp = fopen(file_name, "a");
Lucyjungz 1:1f1f2b99756b 212
Lucyjungz 3:6e08d0bba1bb 213 if (fp == NULL)
Lucyjungz 3:6e08d0bba1bb 214 {
Lucyjungz 3:6e08d0bba1bb 215 /* if it can't open the file then print error message */
Lucyjungz 1:1f1f2b99756b 216 printf("Error! Unable to open file %s!\n",file_name);
Lucyjungz 11:e21d4c5bfd1b 217
Lucyjungz 11:e21d4c5bfd1b 218 /* Indicate LED Status (OFF)*/
Lucyjungz 14:4ba6147f067b 219 FILEMANAGER_SetLedStatus(false);
Lucyjungz 3:6e08d0bba1bb 220 }
Lucyjungz 11:e21d4c5bfd1b 221 else
Lucyjungz 11:e21d4c5bfd1b 222 {
Lucyjungz 11:e21d4c5bfd1b 223 /* Indicate LED Status (ON)*/
Lucyjungz 14:4ba6147f067b 224 FILEMANAGER_SetLedStatus(true);
Lucyjungz 11:e21d4c5bfd1b 225
Lucyjungz 3:6e08d0bba1bb 226 /* Print some message for information */
Lucyjungz 1:1f1f2b99756b 227 printf("\r\n Writing to Gps Log File (%s)....",file_name);
Lucyjungz 1:1f1f2b99756b 228
Lucyjungz 3:6e08d0bba1bb 229 /* Write the line with lattitude and longtitude */
Lucyjungz 3:6e08d0bba1bb 230 fprintf(fp, "%d,%s,%s\n",timestamp,lat,longti);
Lucyjungz 1:1f1f2b99756b 231
Lucyjungz 1:1f1f2b99756b 232 printf("Done");
Lucyjungz 3:6e08d0bba1bb 233 /* Close file once it done */
Lucyjungz 3:6e08d0bba1bb 234 fclose(fp);
Lucyjungz 1:1f1f2b99756b 235 }
Lucyjungz 1:1f1f2b99756b 236 }
Lucyjungz 3:6e08d0bba1bb 237 /**
Lucyjungz 3:6e08d0bba1bb 238 * @brief Function to log RMS Data
Lucyjungz 3:6e08d0bba1bb 239 * @note sequence must be in the same order with variableList
Lucyjungz 3:6e08d0bba1bb 240 * @param timestamp - time structure to get Date
Lucyjungz 3:6e08d0bba1bb 241 * @param var - float array to be log in the file
Lucyjungz 3:6e08d0bba1bb 242 * @param size - size of the float array
Lucyjungz 3:6e08d0bba1bb 243 * @retval None
Lucyjungz 3:6e08d0bba1bb 244 */
Lucyjungz 14:4ba6147f067b 245 void FILEMANAGER_LogRMSData(time_t timestamp ,float * var, int size)
Lucyjungz 1:1f1f2b99756b 246 {
Lucyjungz 3:6e08d0bba1bb 247 /* Get File name */
Lucyjungz 1:1f1f2b99756b 248 char file_name[] = RTL_LOG_FILE_NAME;
Lucyjungz 1:1f1f2b99756b 249
Lucyjungz 3:6e08d0bba1bb 250 /* Generate File name with timestamp */
Lucyjungz 14:4ba6147f067b 251 FILEMANAGER_GenerateFileNameWithTime(timestamp,file_name);
Lucyjungz 14:4ba6147f067b 252 if (!FILEMANAGER_IsFileExist(file_name))
Lucyjungz 1:1f1f2b99756b 253 {
Lucyjungz 3:6e08d0bba1bb 254 /* If file is not exist, log the header */
Lucyjungz 14:4ba6147f067b 255 FILEMANAGER_LogRMSHeader(timestamp);
Lucyjungz 1:1f1f2b99756b 256 }
Lucyjungz 3:6e08d0bba1bb 257 /* Open file with "APPEND" mode */
Lucyjungz 1:1f1f2b99756b 258 FILE *fp = fopen(file_name, "a");
nsrwsurasak 0:a27e0d3581d1 259
Lucyjungz 3:6e08d0bba1bb 260 if (fp == NULL)
Lucyjungz 3:6e08d0bba1bb 261 {
Lucyjungz 3:6e08d0bba1bb 262 /* In case of error, print the error message */
Lucyjungz 1:1f1f2b99756b 263 printf("Error! Unable to open file %s!\n",file_name);
Lucyjungz 11:e21d4c5bfd1b 264
Lucyjungz 11:e21d4c5bfd1b 265 /* Indicate LED Status (OFF)*/
Lucyjungz 14:4ba6147f067b 266 FILEMANAGER_SetLedStatus(false);
Lucyjungz 3:6e08d0bba1bb 267 }
Lucyjungz 3:6e08d0bba1bb 268 else
Lucyjungz 3:6e08d0bba1bb 269 {
Lucyjungz 11:e21d4c5bfd1b 270 /* Indicate LED Status (ON)*/
Lucyjungz 14:4ba6147f067b 271 FILEMANAGER_SetLedStatus(true);
Lucyjungz 11:e21d4c5bfd1b 272
Lucyjungz 3:6e08d0bba1bb 273 /* Print some message for information */
Lucyjungz 1:1f1f2b99756b 274 printf("\r\n Writing to Log File (%s)....",file_name);
Lucyjungz 1:1f1f2b99756b 275
Lucyjungz 3:6e08d0bba1bb 276 /* Write timestamp */
Lucyjungz 14:4ba6147f067b 277 fprintf(fp, "%d,%d",timestamp,0);
Lucyjungz 1:1f1f2b99756b 278
Lucyjungz 3:6e08d0bba1bb 279 /* Write variable data */
Lucyjungz 1:1f1f2b99756b 280 for(int i = 0; i < size; i++)
Lucyjungz 1:1f1f2b99756b 281 {
Lucyjungz 3:6e08d0bba1bb 282 fprintf(fp, ",%f",var[i]);
Lucyjungz 1:1f1f2b99756b 283 }
Lucyjungz 3:6e08d0bba1bb 284 /* Write new line as we done */
Lucyjungz 3:6e08d0bba1bb 285 fprintf(fp, "\n");
nsrwsurasak 0:a27e0d3581d1 286
Lucyjungz 1:1f1f2b99756b 287 printf("Done");
Lucyjungz 3:6e08d0bba1bb 288 /* Close the file */
Lucyjungz 3:6e08d0bba1bb 289 fclose(fp);
Lucyjungz 3:6e08d0bba1bb 290 }
Lucyjungz 3:6e08d0bba1bb 291 }
Lucyjungz 3:6e08d0bba1bb 292 /**
Lucyjungz 3:6e08d0bba1bb 293 * @brief Function to log RMS Header
Lucyjungz 3:6e08d0bba1bb 294 * @note sequence must be in the same order with variableList
Lucyjungz 3:6e08d0bba1bb 295 * @param timestamp - time structure to get Date
Lucyjungz 3:6e08d0bba1bb 296 * @retval None
Lucyjungz 3:6e08d0bba1bb 297 */
Lucyjungz 14:4ba6147f067b 298 void FILEMANAGER_LogRMSHeader(time_t timestamp)
Lucyjungz 3:6e08d0bba1bb 299 {
Lucyjungz 3:6e08d0bba1bb 300 /* Get File name */
Lucyjungz 3:6e08d0bba1bb 301 char file_name[] = RTL_LOG_FILE_NAME;
Lucyjungz 3:6e08d0bba1bb 302
Lucyjungz 3:6e08d0bba1bb 303 /* Generate file name with time */
Lucyjungz 14:4ba6147f067b 304 FILEMANAGER_GenerateFileNameWithTime(timestamp,file_name);
Lucyjungz 3:6e08d0bba1bb 305
Lucyjungz 3:6e08d0bba1bb 306 /* Open file in append mode */
Lucyjungz 3:6e08d0bba1bb 307 FILE *fp = fopen(file_name, "a");
Lucyjungz 3:6e08d0bba1bb 308
Lucyjungz 3:6e08d0bba1bb 309 if (fp == NULL)
Lucyjungz 3:6e08d0bba1bb 310 {
Lucyjungz 3:6e08d0bba1bb 311 /* In case of error, print the error message */
Lucyjungz 3:6e08d0bba1bb 312 printf("Error! Unable to open file %s!\n",file_name);
Lucyjungz 11:e21d4c5bfd1b 313
Lucyjungz 11:e21d4c5bfd1b 314 /* Indicate LED Status (OFF)*/
Lucyjungz 14:4ba6147f067b 315 FILEMANAGER_SetLedStatus(false);
Lucyjungz 3:6e08d0bba1bb 316 }
Lucyjungz 3:6e08d0bba1bb 317 else
Lucyjungz 3:6e08d0bba1bb 318 {
Lucyjungz 11:e21d4c5bfd1b 319 /* Indicate LED Status (ON)*/
Lucyjungz 14:4ba6147f067b 320 FILEMANAGER_SetLedStatus(true);
Lucyjungz 11:e21d4c5bfd1b 321
Lucyjungz 3:6e08d0bba1bb 322 /* opened file so can write */
Lucyjungz 3:6e08d0bba1bb 323 printf("\r\n Writing to Log File (%s)....",file_name);
Lucyjungz 3:6e08d0bba1bb 324
Lucyjungz 3:6e08d0bba1bb 325 /* Write the header to the file */
Lucyjungz 14:4ba6147f067b 326 fprintf(fp, "%s,%s",RMS_HEADER_TIME,RMS_HEADER_MSECOND);
Lucyjungz 7:ab015947e368 327
Lucyjungz 14:4ba6147f067b 328 for(int i = 0; i < m_amountVarList; i++)
Lucyjungz 14:4ba6147f067b 329 {
Lucyjungz 14:4ba6147f067b 330 fprintf(fp, ",%s",m_varList[i].varName);
Lucyjungz 14:4ba6147f067b 331 }
Lucyjungz 14:4ba6147f067b 332 /* Write new line as done */
Lucyjungz 14:4ba6147f067b 333 fprintf(fp, "\n");
Lucyjungz 14:4ba6147f067b 334
Lucyjungz 14:4ba6147f067b 335 /* Write the timestamp unit to the file */
Lucyjungz 14:4ba6147f067b 336 fprintf(fp, "-,-");
Lucyjungz 14:4ba6147f067b 337
Lucyjungz 15:b63a539c3754 338
Lucyjungz 15:b63a539c3754 339 /* Write the unit of variables to the file */
Lucyjungz 3:6e08d0bba1bb 340 for(int i = 0; i < m_amountVarList; i++)
Lucyjungz 3:6e08d0bba1bb 341 {
Lucyjungz 15:b63a539c3754 342 fprintf(fp, ",%s",m_varList[i].varUnit);
Lucyjungz 3:6e08d0bba1bb 343 }
Lucyjungz 15:b63a539c3754 344
Lucyjungz 3:6e08d0bba1bb 345 /* Write new line as done */
Lucyjungz 3:6e08d0bba1bb 346 fprintf(fp, "\n");
Lucyjungz 3:6e08d0bba1bb 347
Lucyjungz 3:6e08d0bba1bb 348 printf("Done");
Lucyjungz 3:6e08d0bba1bb 349
Lucyjungz 3:6e08d0bba1bb 350 /* Close the file */
Lucyjungz 3:6e08d0bba1bb 351 fclose(fp);
Lucyjungz 3:6e08d0bba1bb 352 }
Lucyjungz 3:6e08d0bba1bb 353 }
Lucyjungz 3:6e08d0bba1bb 354 /**
Lucyjungz 3:6e08d0bba1bb 355 * @brief Function to log Mini RMS System Data
Lucyjungz 3:6e08d0bba1bb 356 * @note this file is only for debug
Lucyjungz 3:6e08d0bba1bb 357 * @param gps_interval -
Lucyjungz 3:6e08d0bba1bb 358 * @retval None
Lucyjungz 3:6e08d0bba1bb 359 */
Lucyjungz 14:4ba6147f067b 360 void FILEMANAGER_LogSystemData(float gps_interval)
Lucyjungz 3:6e08d0bba1bb 361 {
Lucyjungz 3:6e08d0bba1bb 362 /* Open the file in append mode */
Lucyjungz 3:6e08d0bba1bb 363 FILE *fp = fopen(MINIRMS_LOG_FILE_NAME, "a");
Lucyjungz 3:6e08d0bba1bb 364
Lucyjungz 3:6e08d0bba1bb 365 if (fp == NULL) {
Lucyjungz 3:6e08d0bba1bb 366 /* In case of error, print the msg */
Lucyjungz 3:6e08d0bba1bb 367 printf("Error! Unable to open file!\n");
Lucyjungz 11:e21d4c5bfd1b 368
Lucyjungz 11:e21d4c5bfd1b 369 /* Indicate LED Status (OFF)*/
Lucyjungz 14:4ba6147f067b 370 FILEMANAGER_SetLedStatus(false);
Lucyjungz 3:6e08d0bba1bb 371 }
Lucyjungz 3:6e08d0bba1bb 372 else
Lucyjungz 3:6e08d0bba1bb 373 {
Lucyjungz 11:e21d4c5bfd1b 374 /* Indicate LED Status (ON)*/
Lucyjungz 14:4ba6147f067b 375 FILEMANAGER_SetLedStatus(true);
Lucyjungz 11:e21d4c5bfd1b 376
Lucyjungz 3:6e08d0bba1bb 377 /* Write some message, which is TBD */
Lucyjungz 3:6e08d0bba1bb 378 fprintf(fp, "\nStart Mini-RMS System with Gps Interval = %f",gps_interval);
Lucyjungz 1:1f1f2b99756b 379 fclose(fp); // ensure you close the file after writing
Lucyjungz 1:1f1f2b99756b 380 }
Lucyjungz 1:1f1f2b99756b 381 }
Lucyjungz 3:6e08d0bba1bb 382 /**
Lucyjungz 3:6e08d0bba1bb 383 * @brief Utility to delete file
Lucyjungz 3:6e08d0bba1bb 384 * @note
Lucyjungz 3:6e08d0bba1bb 385 * @param filename - file name to be deleted
Lucyjungz 3:6e08d0bba1bb 386 * @retval None
Lucyjungz 3:6e08d0bba1bb 387 */
Lucyjungz 14:4ba6147f067b 388 void FILEMANAGER_Deletefile(char filename[])
nsrwsurasak 0:a27e0d3581d1 389 {
nsrwsurasak 0:a27e0d3581d1 390 printf("Deleting file '%s'...",filename);
nsrwsurasak 0:a27e0d3581d1 391 FILE *fp = fopen(filename, "r"); // try and open file
nsrwsurasak 0:a27e0d3581d1 392 if (fp != NULL) { // if it does open...
nsrwsurasak 0:a27e0d3581d1 393 fclose(fp); // close it
nsrwsurasak 0:a27e0d3581d1 394 remove(filename); // and then delete
nsrwsurasak 0:a27e0d3581d1 395 printf("Done!\n");
nsrwsurasak 0:a27e0d3581d1 396 }
nsrwsurasak 0:a27e0d3581d1 397 // if we can't open it, it doesn't exist and so we can't delete it
nsrwsurasak 0:a27e0d3581d1 398 }
Lucyjungz 3:6e08d0bba1bb 399 /**
Lucyjungz 3:6e08d0bba1bb 400 * @brief Utility to check file available
Lucyjungz 3:6e08d0bba1bb 401 * @note
Lucyjungz 3:6e08d0bba1bb 402 * @param filename - file name to be checked
Lucyjungz 3:6e08d0bba1bb 403 * @retval true - file is exist , false - file is not exist
Lucyjungz 3:6e08d0bba1bb 404 */
Lucyjungz 14:4ba6147f067b 405 bool FILEMANAGER_IsFileExist(char filename[])
Lucyjungz 1:1f1f2b99756b 406 {
Lucyjungz 1:1f1f2b99756b 407 bool exist = false;
Lucyjungz 1:1f1f2b99756b 408 FILE *fp = fopen(filename, "r"); // try and open file
Lucyjungz 1:1f1f2b99756b 409 if (fp != NULL) { // if it does open...
Lucyjungz 1:1f1f2b99756b 410 fclose(fp); // close it
Lucyjungz 1:1f1f2b99756b 411 exist = true;
Lucyjungz 1:1f1f2b99756b 412 }
Lucyjungz 1:1f1f2b99756b 413
Lucyjungz 1:1f1f2b99756b 414 return exist;
Lucyjungz 1:1f1f2b99756b 415 }
Lucyjungz 3:6e08d0bba1bb 416 /**
Lucyjungz 3:6e08d0bba1bb 417 * @brief Utility to get GPS Interval
Lucyjungz 14:4ba6147f067b 418 * @note must be called after read the setup file
Lucyjungz 3:6e08d0bba1bb 419 * @param None
Lucyjungz 3:6e08d0bba1bb 420 * @retval GPS interval
Lucyjungz 3:6e08d0bba1bb 421 */
Lucyjungz 14:4ba6147f067b 422 int FILEMANAGER_GPSInterval()
nsrwsurasak 0:a27e0d3581d1 423 {
Lucyjungz 3:6e08d0bba1bb 424 /* Return interval in int */
Lucyjungz 14:4ba6147f067b 425 return ( m_GpsInterval );
nsrwsurasak 0:a27e0d3581d1 426 }
Lucyjungz 3:6e08d0bba1bb 427 /**
Lucyjungz 3:6e08d0bba1bb 428 * @brief Utility to get Data Interval
Lucyjungz 14:4ba6147f067b 429 * @note must be called after read the setup file
Lucyjungz 3:6e08d0bba1bb 430 * @param None
Lucyjungz 3:6e08d0bba1bb 431 * @retval Data interval
Lucyjungz 3:6e08d0bba1bb 432 */
Lucyjungz 14:4ba6147f067b 433 int FILEMANAGER_DataInterval()
nsrwsurasak 0:a27e0d3581d1 434 {
Lucyjungz 3:6e08d0bba1bb 435 /* Return interval in int */
Lucyjungz 14:4ba6147f067b 436 return ( m_DataInterval );
nsrwsurasak 0:a27e0d3581d1 437 }
Lucyjungz 3:6e08d0bba1bb 438 /**
Lucyjungz 3:6e08d0bba1bb 439 * @brief Function to read the variable list file
Lucyjungz 3:6e08d0bba1bb 440 * @note Recommended to call this function at initilization phase
Lucyjungz 3:6e08d0bba1bb 441 * @param None
Lucyjungz 3:6e08d0bba1bb 442 * @retval pointer to variable list
Lucyjungz 3:6e08d0bba1bb 443 */
Lucyjungz 14:4ba6147f067b 444 Variable_Data_TypeDef * FILEMANAGER_ReadVarFile()
nsrwsurasak 0:a27e0d3581d1 445 {
Lucyjungz 3:6e08d0bba1bb 446 /* Open the file with reading mode */
nsrwsurasak 0:a27e0d3581d1 447 FILE *fp = fopen(VARIABLE_FILE_NAME, "r");
nsrwsurasak 0:a27e0d3581d1 448
Lucyjungz 3:6e08d0bba1bb 449 if (fp == NULL)
Lucyjungz 3:6e08d0bba1bb 450 {
Lucyjungz 3:6e08d0bba1bb 451 /* if it can't open the file then print error message */
nsrwsurasak 0:a27e0d3581d1 452 printf("\nError! Unable to open file! %s \n", VARIABLE_FILE_NAME);
Lucyjungz 11:e21d4c5bfd1b 453 /* Indicate LED Status (OFF)*/
Lucyjungz 14:4ba6147f067b 454 FILEMANAGER_SetLedStatus(false);
Lucyjungz 11:e21d4c5bfd1b 455
nsrwsurasak 0:a27e0d3581d1 456 return NULL;
Lucyjungz 3:6e08d0bba1bb 457 }
Lucyjungz 3:6e08d0bba1bb 458 else
Lucyjungz 3:6e08d0bba1bb 459 {
Lucyjungz 11:e21d4c5bfd1b 460 /* Indicate LED Status (ON)*/
Lucyjungz 14:4ba6147f067b 461 FILEMANAGER_SetLedStatus(true);
nsrwsurasak 0:a27e0d3581d1 462
Lucyjungz 3:6e08d0bba1bb 463 /* Allocate buffer for reading */
Lucyjungz 14:4ba6147f067b 464 char buf[READ_FILE_BUFFER_SIZE];
nsrwsurasak 0:a27e0d3581d1 465 int index = 0;
Lucyjungz 3:6e08d0bba1bb 466
Lucyjungz 3:6e08d0bba1bb 467 /* Initialize return value */
nsrwsurasak 0:a27e0d3581d1 468 memset(m_varList, ' ', sizeof(m_varList));
Lucyjungz 3:6e08d0bba1bb 469
Lucyjungz 3:6e08d0bba1bb 470 /* Read line from the file */
Lucyjungz 3:6e08d0bba1bb 471 while (fgets(buf, sizeof(buf), fp) != NULL)
Lucyjungz 3:6e08d0bba1bb 472 {
Lucyjungz 3:6e08d0bba1bb 473 /* Check the TAG */
Lucyjungz 3:6e08d0bba1bb 474 if (strstr (buf,VAR_NAME_TAG))
Lucyjungz 3:6e08d0bba1bb 475 {
Lucyjungz 3:6e08d0bba1bb 476 /* Found variable TAG, populate it*/
Lucyjungz 14:4ba6147f067b 477 FILEMANAGER_GetXmlText(buf , m_varList[index].varName);
Lucyjungz 3:6e08d0bba1bb 478 }
Lucyjungz 3:6e08d0bba1bb 479 else if (strstr (buf,VAR_ADDR_TAG))
Lucyjungz 3:6e08d0bba1bb 480 {
Lucyjungz 3:6e08d0bba1bb 481 /* Found variable address TAG, populate it*/
Lucyjungz 14:4ba6147f067b 482 FILEMANAGER_GetXmlText(buf , m_varList[index].varAddress);
Lucyjungz 5:7c513eee7b2b 483 }
Lucyjungz 10:a8003d357cf2 484 else if (strstr (buf,VAR_TYPE_TAG))
Lucyjungz 10:a8003d357cf2 485 {
Lucyjungz 10:a8003d357cf2 486 /* Found variable type TAG, populate it*/
Lucyjungz 14:4ba6147f067b 487 FILEMANAGER_GetXmlText(buf , m_varList[index].varType);
Lucyjungz 10:a8003d357cf2 488 }
Lucyjungz 6:5bd75c0f607c 489 else if (strstr (buf,VAR_LSB1_TAG))
Lucyjungz 6:5bd75c0f607c 490 {
Lucyjungz 6:5bd75c0f607c 491 /* Found variable LSB1 TAG, populate it*/
Lucyjungz 14:4ba6147f067b 492 FILEMANAGER_GetXmlText(buf , m_varList[index].varLSB1);
Lucyjungz 6:5bd75c0f607c 493 }
Lucyjungz 6:5bd75c0f607c 494 else if (strstr (buf,VAR_LSB2_TAG))
Lucyjungz 5:7c513eee7b2b 495 {
Lucyjungz 6:5bd75c0f607c 496 /* Found variable LSB2 TAG, populate it*/
Lucyjungz 14:4ba6147f067b 497 FILEMANAGER_GetXmlText(buf , m_varList[index].varLSB2);
Lucyjungz 6:5bd75c0f607c 498 }
Lucyjungz 6:5bd75c0f607c 499 else if (strstr (buf,VAR_BITMASK_TAG))
Lucyjungz 6:5bd75c0f607c 500 {
Lucyjungz 6:5bd75c0f607c 501 /* Found variable BitMask TAG, populate it*/
Lucyjungz 14:4ba6147f067b 502 FILEMANAGER_GetXmlText(buf , m_varList[index].varBitMask);
Lucyjungz 5:7c513eee7b2b 503 }
Lucyjungz 5:7c513eee7b2b 504 else if (strstr (buf,VAR_UNIT_TAG))
Lucyjungz 5:7c513eee7b2b 505 {
Lucyjungz 5:7c513eee7b2b 506 /* Found variable unit TAG, populate it*/
Lucyjungz 14:4ba6147f067b 507 FILEMANAGER_GetXmlText(buf , m_varList[index].varUnit);
nsrwsurasak 0:a27e0d3581d1 508 index++;
nsrwsurasak 0:a27e0d3581d1 509 }
nsrwsurasak 0:a27e0d3581d1 510
nsrwsurasak 0:a27e0d3581d1 511 }
Lucyjungz 3:6e08d0bba1bb 512 /* Close File */
Lucyjungz 3:6e08d0bba1bb 513 fclose(fp);
Lucyjungz 3:6e08d0bba1bb 514
Lucyjungz 3:6e08d0bba1bb 515 /* Populate amount */
nsrwsurasak 0:a27e0d3581d1 516 m_amountVarList = index;
Lucyjungz 3:6e08d0bba1bb 517
Lucyjungz 3:6e08d0bba1bb 518 /* Return variable list */
nsrwsurasak 0:a27e0d3581d1 519 return m_varList;
nsrwsurasak 0:a27e0d3581d1 520 }
nsrwsurasak 0:a27e0d3581d1 521 }
Lucyjungz 3:6e08d0bba1bb 522 /**
Lucyjungz 3:6e08d0bba1bb 523 * @brief Function to amount of variable for data logging
Lucyjungz 3:6e08d0bba1bb 524 * @note Must be called after readVarFile
Lucyjungz 3:6e08d0bba1bb 525 * @param None
Lucyjungz 3:6e08d0bba1bb 526 * @retval amount of variable list
Lucyjungz 3:6e08d0bba1bb 527 */
Lucyjungz 14:4ba6147f067b 528 int FILEMANAGER_GetAmountVarList()
nsrwsurasak 0:a27e0d3581d1 529 {
nsrwsurasak 0:a27e0d3581d1 530 return m_amountVarList;
nsrwsurasak 0:a27e0d3581d1 531 }
Lucyjungz 3:6e08d0bba1bb 532 /**
Lucyjungz 3:6e08d0bba1bb 533 * @brief Function to get variable list
Lucyjungz 3:6e08d0bba1bb 534 * @note Must be called after readVarFile
Lucyjungz 3:6e08d0bba1bb 535 * @param None
Lucyjungz 3:6e08d0bba1bb 536 * @retval pointer to variable list
Lucyjungz 3:6e08d0bba1bb 537 */
Lucyjungz 14:4ba6147f067b 538 Variable_Data_TypeDef * FILEMANAGER_GetVarList()
nsrwsurasak 0:a27e0d3581d1 539 {
nsrwsurasak 0:a27e0d3581d1 540 return m_varList;
nsrwsurasak 0:a27e0d3581d1 541 }
Lucyjungz 14:4ba6147f067b 542
Lucyjungz 11:e21d4c5bfd1b 543 /**
Lucyjungz 11:e21d4c5bfd1b 544 * @brief Utility to play with LED status
Lucyjungz 11:e21d4c5bfd1b 545 * @note Libary user need to assign proper PinName to LED_SDCARD
Lucyjungz 11:e21d4c5bfd1b 546 * @param on - True for turning LED ON, otherwise LED off
Lucyjungz 11:e21d4c5bfd1b 547 * @retval None
Lucyjungz 11:e21d4c5bfd1b 548 */
Lucyjungz 14:4ba6147f067b 549 static void FILEMANAGER_SetLedStatus(bool on)
Lucyjungz 11:e21d4c5bfd1b 550 {
Lucyjungz 12:0045fca3c160 551 /* Check LED Connection */
Lucyjungz 11:e21d4c5bfd1b 552 if (ledStatus.is_connected())
Lucyjungz 11:e21d4c5bfd1b 553 {
Lucyjungz 12:0045fca3c160 554 /* Set LED regarding to given argment */
Lucyjungz 11:e21d4c5bfd1b 555 ledStatus = on;
Lucyjungz 12:0045fca3c160 556 }
Lucyjungz 12:0045fca3c160 557 #if DEBUG
Lucyjungz 12:0045fca3c160 558 else
Lucyjungz 12:0045fca3c160 559 {
Lucyjungz 14:4ba6147f067b 560 printf("\r\nSDCard LED is not connected !!");
Lucyjungz 12:0045fca3c160 561 }
Lucyjungz 12:0045fca3c160 562 #endif
Lucyjungz 11:e21d4c5bfd1b 563 }
Lucyjungz 15:b63a539c3754 564