File manager

Dependencies:   SDFileSystem

Dependents:   RwSDCard_Xml_GPS

Committer:
nsrwsurasak
Date:
Tue May 24 10:44:46 2016 +0000
Revision:
18:d6980681b321
Parent:
16:c13dc589bb55
Update revision and fix bug

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