File manager

Dependencies:   SDFileSystem

Dependents:   RwSDCard_Xml_GPS

Committer:
Lucyjungz
Date:
Tue May 24 07:16:38 2016 +0000
Revision:
17:d24d2b2bbd42
Parent:
16:c13dc589bb55
Child:
19:5af5c60e7a9f
Support SDCard Switch

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