File manager

Dependencies:   SDFileSystem

Dependents:   RwSDCard_Xml_GPS

Committer:
nsrwsurasak
Date:
Mon May 23 11:19:54 2016 +0000
Revision:
16:c13dc589bb55
Parent:
15:b63a539c3754
Child:
17:d24d2b2bbd42
Child:
18:d6980681b321
Change header file

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 {
Lucyjungz 3:6e08d0bba1bb 279 fprintf(fp, ",%f",var[i]);
Lucyjungz 1:1f1f2b99756b 280 }
Lucyjungz 3:6e08d0bba1bb 281 /* Write new line as we done */
Lucyjungz 3:6e08d0bba1bb 282 fprintf(fp, "\n");
nsrwsurasak 0:a27e0d3581d1 283
Lucyjungz 1:1f1f2b99756b 284 printf("Done");
Lucyjungz 3:6e08d0bba1bb 285 /* Close the file */
Lucyjungz 3:6e08d0bba1bb 286 fclose(fp);
Lucyjungz 3:6e08d0bba1bb 287 }
Lucyjungz 3:6e08d0bba1bb 288 }
Lucyjungz 3:6e08d0bba1bb 289 /**
Lucyjungz 3:6e08d0bba1bb 290 * @brief Function to log RMS Header
Lucyjungz 3:6e08d0bba1bb 291 * @note sequence must be in the same order with variableList
Lucyjungz 3:6e08d0bba1bb 292 * @param timestamp - time structure to get Date
Lucyjungz 3:6e08d0bba1bb 293 * @retval None
Lucyjungz 3:6e08d0bba1bb 294 */
Lucyjungz 14:4ba6147f067b 295 void FILEMANAGER_LogRMSHeader(time_t timestamp)
Lucyjungz 3:6e08d0bba1bb 296 {
Lucyjungz 3:6e08d0bba1bb 297 /* Get File name */
Lucyjungz 3:6e08d0bba1bb 298 char file_name[] = RTL_LOG_FILE_NAME;
Lucyjungz 3:6e08d0bba1bb 299
Lucyjungz 3:6e08d0bba1bb 300 /* Generate file name with time */
Lucyjungz 14:4ba6147f067b 301 FILEMANAGER_GenerateFileNameWithTime(timestamp,file_name);
Lucyjungz 3:6e08d0bba1bb 302
Lucyjungz 3:6e08d0bba1bb 303 /* Open file in append mode */
Lucyjungz 3:6e08d0bba1bb 304 FILE *fp = fopen(file_name, "a");
Lucyjungz 3:6e08d0bba1bb 305
Lucyjungz 3:6e08d0bba1bb 306 if (fp == NULL)
Lucyjungz 3:6e08d0bba1bb 307 {
Lucyjungz 3:6e08d0bba1bb 308 /* In case of error, print the error message */
Lucyjungz 3:6e08d0bba1bb 309 printf("Error! Unable to open file %s!\n",file_name);
Lucyjungz 11:e21d4c5bfd1b 310
Lucyjungz 11:e21d4c5bfd1b 311 /* Indicate LED Status (OFF)*/
Lucyjungz 14:4ba6147f067b 312 FILEMANAGER_SetLedStatus(false);
Lucyjungz 3:6e08d0bba1bb 313 }
Lucyjungz 3:6e08d0bba1bb 314 else
Lucyjungz 3:6e08d0bba1bb 315 {
Lucyjungz 11:e21d4c5bfd1b 316 /* Indicate LED Status (ON)*/
Lucyjungz 14:4ba6147f067b 317 FILEMANAGER_SetLedStatus(true);
Lucyjungz 11:e21d4c5bfd1b 318
Lucyjungz 3:6e08d0bba1bb 319 /* opened file so can write */
Lucyjungz 3:6e08d0bba1bb 320 printf("\r\n Writing to Log File (%s)....",file_name);
Lucyjungz 3:6e08d0bba1bb 321
Lucyjungz 3:6e08d0bba1bb 322 /* Write the header to the file */
Lucyjungz 14:4ba6147f067b 323 fprintf(fp, "%s,%s",RMS_HEADER_TIME,RMS_HEADER_MSECOND);
Lucyjungz 7:ab015947e368 324
Lucyjungz 14:4ba6147f067b 325 for(int i = 0; i < m_amountVarList; i++)
Lucyjungz 14:4ba6147f067b 326 {
Lucyjungz 14:4ba6147f067b 327 fprintf(fp, ",%s",m_varList[i].varName);
Lucyjungz 14:4ba6147f067b 328 }
Lucyjungz 14:4ba6147f067b 329 /* Write new line as done */
Lucyjungz 14:4ba6147f067b 330 fprintf(fp, "\n");
Lucyjungz 14:4ba6147f067b 331
Lucyjungz 14:4ba6147f067b 332 /* Write the timestamp unit to the file */
Lucyjungz 14:4ba6147f067b 333 fprintf(fp, "-,-");
Lucyjungz 14:4ba6147f067b 334
Lucyjungz 15:b63a539c3754 335
Lucyjungz 15:b63a539c3754 336 /* Write the unit of variables to the file */
Lucyjungz 3:6e08d0bba1bb 337 for(int i = 0; i < m_amountVarList; i++)
Lucyjungz 3:6e08d0bba1bb 338 {
Lucyjungz 15:b63a539c3754 339 fprintf(fp, ",%s",m_varList[i].varUnit);
Lucyjungz 3:6e08d0bba1bb 340 }
Lucyjungz 15:b63a539c3754 341
Lucyjungz 3:6e08d0bba1bb 342 /* Write new line as done */
Lucyjungz 3:6e08d0bba1bb 343 fprintf(fp, "\n");
Lucyjungz 3:6e08d0bba1bb 344
Lucyjungz 3:6e08d0bba1bb 345 printf("Done");
Lucyjungz 3:6e08d0bba1bb 346
Lucyjungz 3:6e08d0bba1bb 347 /* Close the file */
Lucyjungz 3:6e08d0bba1bb 348 fclose(fp);
Lucyjungz 3:6e08d0bba1bb 349 }
Lucyjungz 3:6e08d0bba1bb 350 }
Lucyjungz 3:6e08d0bba1bb 351 /**
Lucyjungz 3:6e08d0bba1bb 352 * @brief Function to log Mini RMS System Data
Lucyjungz 3:6e08d0bba1bb 353 * @note this file is only for debug
Lucyjungz 3:6e08d0bba1bb 354 * @param gps_interval -
Lucyjungz 3:6e08d0bba1bb 355 * @retval None
Lucyjungz 3:6e08d0bba1bb 356 */
Lucyjungz 14:4ba6147f067b 357 void FILEMANAGER_LogSystemData(float gps_interval)
Lucyjungz 3:6e08d0bba1bb 358 {
Lucyjungz 3:6e08d0bba1bb 359 /* Open the file in append mode */
Lucyjungz 3:6e08d0bba1bb 360 FILE *fp = fopen(MINIRMS_LOG_FILE_NAME, "a");
Lucyjungz 3:6e08d0bba1bb 361
Lucyjungz 3:6e08d0bba1bb 362 if (fp == NULL) {
Lucyjungz 3:6e08d0bba1bb 363 /* In case of error, print the msg */
Lucyjungz 3:6e08d0bba1bb 364 printf("Error! Unable to open file!\n");
Lucyjungz 11:e21d4c5bfd1b 365
Lucyjungz 11:e21d4c5bfd1b 366 /* Indicate LED Status (OFF)*/
Lucyjungz 14:4ba6147f067b 367 FILEMANAGER_SetLedStatus(false);
Lucyjungz 3:6e08d0bba1bb 368 }
Lucyjungz 3:6e08d0bba1bb 369 else
Lucyjungz 3:6e08d0bba1bb 370 {
Lucyjungz 11:e21d4c5bfd1b 371 /* Indicate LED Status (ON)*/
Lucyjungz 14:4ba6147f067b 372 FILEMANAGER_SetLedStatus(true);
Lucyjungz 11:e21d4c5bfd1b 373
Lucyjungz 3:6e08d0bba1bb 374 /* Write some message, which is TBD */
Lucyjungz 3:6e08d0bba1bb 375 fprintf(fp, "\nStart Mini-RMS System with Gps Interval = %f",gps_interval);
Lucyjungz 1:1f1f2b99756b 376 fclose(fp); // ensure you close the file after writing
Lucyjungz 1:1f1f2b99756b 377 }
Lucyjungz 1:1f1f2b99756b 378 }
Lucyjungz 3:6e08d0bba1bb 379 /**
Lucyjungz 3:6e08d0bba1bb 380 * @brief Utility to delete file
Lucyjungz 3:6e08d0bba1bb 381 * @note
Lucyjungz 3:6e08d0bba1bb 382 * @param filename - file name to be deleted
Lucyjungz 3:6e08d0bba1bb 383 * @retval None
Lucyjungz 3:6e08d0bba1bb 384 */
Lucyjungz 14:4ba6147f067b 385 void FILEMANAGER_Deletefile(char filename[])
nsrwsurasak 0:a27e0d3581d1 386 {
nsrwsurasak 0:a27e0d3581d1 387 printf("Deleting file '%s'...",filename);
nsrwsurasak 0:a27e0d3581d1 388 FILE *fp = fopen(filename, "r"); // try and open file
nsrwsurasak 0:a27e0d3581d1 389 if (fp != NULL) { // if it does open...
nsrwsurasak 0:a27e0d3581d1 390 fclose(fp); // close it
nsrwsurasak 0:a27e0d3581d1 391 remove(filename); // and then delete
nsrwsurasak 0:a27e0d3581d1 392 printf("Done!\n");
nsrwsurasak 0:a27e0d3581d1 393 }
nsrwsurasak 0:a27e0d3581d1 394 // if we can't open it, it doesn't exist and so we can't delete it
nsrwsurasak 0:a27e0d3581d1 395 }
Lucyjungz 3:6e08d0bba1bb 396 /**
Lucyjungz 3:6e08d0bba1bb 397 * @brief Utility to check file available
Lucyjungz 3:6e08d0bba1bb 398 * @note
Lucyjungz 3:6e08d0bba1bb 399 * @param filename - file name to be checked
Lucyjungz 3:6e08d0bba1bb 400 * @retval true - file is exist , false - file is not exist
Lucyjungz 3:6e08d0bba1bb 401 */
Lucyjungz 14:4ba6147f067b 402 bool FILEMANAGER_IsFileExist(char filename[])
Lucyjungz 1:1f1f2b99756b 403 {
Lucyjungz 1:1f1f2b99756b 404 bool exist = false;
Lucyjungz 1:1f1f2b99756b 405 FILE *fp = fopen(filename, "r"); // try and open file
Lucyjungz 1:1f1f2b99756b 406 if (fp != NULL) { // if it does open...
Lucyjungz 1:1f1f2b99756b 407 fclose(fp); // close it
Lucyjungz 1:1f1f2b99756b 408 exist = true;
Lucyjungz 1:1f1f2b99756b 409 }
Lucyjungz 1:1f1f2b99756b 410
Lucyjungz 1:1f1f2b99756b 411 return exist;
Lucyjungz 1:1f1f2b99756b 412 }
Lucyjungz 3:6e08d0bba1bb 413 /**
Lucyjungz 3:6e08d0bba1bb 414 * @brief Utility to get GPS Interval
Lucyjungz 14:4ba6147f067b 415 * @note must be called after read the setup file
Lucyjungz 3:6e08d0bba1bb 416 * @param None
Lucyjungz 3:6e08d0bba1bb 417 * @retval GPS interval
Lucyjungz 3:6e08d0bba1bb 418 */
Lucyjungz 14:4ba6147f067b 419 int FILEMANAGER_GPSInterval()
nsrwsurasak 0:a27e0d3581d1 420 {
Lucyjungz 3:6e08d0bba1bb 421 /* Return interval in int */
Lucyjungz 14:4ba6147f067b 422 return ( m_GpsInterval );
nsrwsurasak 0:a27e0d3581d1 423 }
Lucyjungz 3:6e08d0bba1bb 424 /**
Lucyjungz 3:6e08d0bba1bb 425 * @brief Utility to get Data Interval
Lucyjungz 14:4ba6147f067b 426 * @note must be called after read the setup file
Lucyjungz 3:6e08d0bba1bb 427 * @param None
Lucyjungz 3:6e08d0bba1bb 428 * @retval Data interval
Lucyjungz 3:6e08d0bba1bb 429 */
Lucyjungz 14:4ba6147f067b 430 int FILEMANAGER_DataInterval()
nsrwsurasak 0:a27e0d3581d1 431 {
Lucyjungz 3:6e08d0bba1bb 432 /* Return interval in int */
Lucyjungz 14:4ba6147f067b 433 return ( m_DataInterval );
nsrwsurasak 0:a27e0d3581d1 434 }
Lucyjungz 3:6e08d0bba1bb 435 /**
Lucyjungz 3:6e08d0bba1bb 436 * @brief Function to read the variable list file
Lucyjungz 3:6e08d0bba1bb 437 * @note Recommended to call this function at initilization phase
Lucyjungz 3:6e08d0bba1bb 438 * @param None
Lucyjungz 3:6e08d0bba1bb 439 * @retval pointer to variable list
Lucyjungz 3:6e08d0bba1bb 440 */
Lucyjungz 14:4ba6147f067b 441 Variable_Data_TypeDef * FILEMANAGER_ReadVarFile()
nsrwsurasak 0:a27e0d3581d1 442 {
Lucyjungz 3:6e08d0bba1bb 443 /* Open the file with reading mode */
nsrwsurasak 0:a27e0d3581d1 444 FILE *fp = fopen(VARIABLE_FILE_NAME, "r");
nsrwsurasak 0:a27e0d3581d1 445
Lucyjungz 3:6e08d0bba1bb 446 if (fp == NULL)
Lucyjungz 3:6e08d0bba1bb 447 {
Lucyjungz 3:6e08d0bba1bb 448 /* if it can't open the file then print error message */
nsrwsurasak 0:a27e0d3581d1 449 printf("\nError! Unable to open file! %s \n", VARIABLE_FILE_NAME);
Lucyjungz 11:e21d4c5bfd1b 450 /* Indicate LED Status (OFF)*/
Lucyjungz 14:4ba6147f067b 451 FILEMANAGER_SetLedStatus(false);
Lucyjungz 11:e21d4c5bfd1b 452
nsrwsurasak 0:a27e0d3581d1 453 return NULL;
Lucyjungz 3:6e08d0bba1bb 454 }
Lucyjungz 3:6e08d0bba1bb 455 else
Lucyjungz 3:6e08d0bba1bb 456 {
Lucyjungz 11:e21d4c5bfd1b 457 /* Indicate LED Status (ON)*/
Lucyjungz 14:4ba6147f067b 458 FILEMANAGER_SetLedStatus(true);
nsrwsurasak 0:a27e0d3581d1 459
Lucyjungz 3:6e08d0bba1bb 460 /* Allocate buffer for reading */
Lucyjungz 14:4ba6147f067b 461 char buf[READ_FILE_BUFFER_SIZE];
nsrwsurasak 0:a27e0d3581d1 462 int index = 0;
Lucyjungz 3:6e08d0bba1bb 463
Lucyjungz 3:6e08d0bba1bb 464 /* Initialize return value */
nsrwsurasak 0:a27e0d3581d1 465 memset(m_varList, ' ', sizeof(m_varList));
Lucyjungz 3:6e08d0bba1bb 466
Lucyjungz 3:6e08d0bba1bb 467 /* Read line from the file */
Lucyjungz 3:6e08d0bba1bb 468 while (fgets(buf, sizeof(buf), fp) != NULL)
Lucyjungz 3:6e08d0bba1bb 469 {
Lucyjungz 3:6e08d0bba1bb 470 /* Check the TAG */
Lucyjungz 3:6e08d0bba1bb 471 if (strstr (buf,VAR_NAME_TAG))
Lucyjungz 3:6e08d0bba1bb 472 {
Lucyjungz 3:6e08d0bba1bb 473 /* Found variable TAG, populate it*/
Lucyjungz 14:4ba6147f067b 474 FILEMANAGER_GetXmlText(buf , m_varList[index].varName);
Lucyjungz 3:6e08d0bba1bb 475 }
Lucyjungz 3:6e08d0bba1bb 476 else if (strstr (buf,VAR_ADDR_TAG))
Lucyjungz 3:6e08d0bba1bb 477 {
Lucyjungz 3:6e08d0bba1bb 478 /* Found variable address TAG, populate it*/
Lucyjungz 14:4ba6147f067b 479 FILEMANAGER_GetXmlText(buf , m_varList[index].varAddress);
Lucyjungz 5:7c513eee7b2b 480 }
Lucyjungz 10:a8003d357cf2 481 else if (strstr (buf,VAR_TYPE_TAG))
Lucyjungz 10:a8003d357cf2 482 {
Lucyjungz 10:a8003d357cf2 483 /* Found variable type TAG, populate it*/
Lucyjungz 14:4ba6147f067b 484 FILEMANAGER_GetXmlText(buf , m_varList[index].varType);
Lucyjungz 10:a8003d357cf2 485 }
Lucyjungz 6:5bd75c0f607c 486 else if (strstr (buf,VAR_LSB1_TAG))
Lucyjungz 6:5bd75c0f607c 487 {
Lucyjungz 6:5bd75c0f607c 488 /* Found variable LSB1 TAG, populate it*/
Lucyjungz 14:4ba6147f067b 489 FILEMANAGER_GetXmlText(buf , m_varList[index].varLSB1);
Lucyjungz 6:5bd75c0f607c 490 }
Lucyjungz 6:5bd75c0f607c 491 else if (strstr (buf,VAR_LSB2_TAG))
Lucyjungz 5:7c513eee7b2b 492 {
Lucyjungz 6:5bd75c0f607c 493 /* Found variable LSB2 TAG, populate it*/
Lucyjungz 14:4ba6147f067b 494 FILEMANAGER_GetXmlText(buf , m_varList[index].varLSB2);
Lucyjungz 6:5bd75c0f607c 495 }
Lucyjungz 6:5bd75c0f607c 496 else if (strstr (buf,VAR_BITMASK_TAG))
Lucyjungz 6:5bd75c0f607c 497 {
Lucyjungz 6:5bd75c0f607c 498 /* Found variable BitMask TAG, populate it*/
Lucyjungz 14:4ba6147f067b 499 FILEMANAGER_GetXmlText(buf , m_varList[index].varBitMask);
Lucyjungz 5:7c513eee7b2b 500 }
Lucyjungz 5:7c513eee7b2b 501 else if (strstr (buf,VAR_UNIT_TAG))
Lucyjungz 5:7c513eee7b2b 502 {
Lucyjungz 5:7c513eee7b2b 503 /* Found variable unit TAG, populate it*/
Lucyjungz 14:4ba6147f067b 504 FILEMANAGER_GetXmlText(buf , m_varList[index].varUnit);
nsrwsurasak 0:a27e0d3581d1 505 index++;
nsrwsurasak 0:a27e0d3581d1 506 }
nsrwsurasak 0:a27e0d3581d1 507
nsrwsurasak 0:a27e0d3581d1 508 }
Lucyjungz 3:6e08d0bba1bb 509 /* Close File */
Lucyjungz 3:6e08d0bba1bb 510 fclose(fp);
Lucyjungz 3:6e08d0bba1bb 511
Lucyjungz 3:6e08d0bba1bb 512 /* Populate amount */
nsrwsurasak 0:a27e0d3581d1 513 m_amountVarList = index;
Lucyjungz 3:6e08d0bba1bb 514
Lucyjungz 3:6e08d0bba1bb 515 /* Return variable list */
nsrwsurasak 0:a27e0d3581d1 516 return m_varList;
nsrwsurasak 0:a27e0d3581d1 517 }
nsrwsurasak 0:a27e0d3581d1 518 }
Lucyjungz 3:6e08d0bba1bb 519 /**
Lucyjungz 3:6e08d0bba1bb 520 * @brief Function to amount of variable for data logging
Lucyjungz 3:6e08d0bba1bb 521 * @note Must be called after readVarFile
Lucyjungz 3:6e08d0bba1bb 522 * @param None
Lucyjungz 3:6e08d0bba1bb 523 * @retval amount of variable list
Lucyjungz 3:6e08d0bba1bb 524 */
Lucyjungz 14:4ba6147f067b 525 int FILEMANAGER_GetAmountVarList()
nsrwsurasak 0:a27e0d3581d1 526 {
nsrwsurasak 0:a27e0d3581d1 527 return m_amountVarList;
nsrwsurasak 0:a27e0d3581d1 528 }
Lucyjungz 3:6e08d0bba1bb 529 /**
Lucyjungz 3:6e08d0bba1bb 530 * @brief Function to get variable list
Lucyjungz 3:6e08d0bba1bb 531 * @note Must be called after readVarFile
Lucyjungz 3:6e08d0bba1bb 532 * @param None
Lucyjungz 3:6e08d0bba1bb 533 * @retval pointer to variable list
Lucyjungz 3:6e08d0bba1bb 534 */
Lucyjungz 14:4ba6147f067b 535 Variable_Data_TypeDef * FILEMANAGER_GetVarList()
nsrwsurasak 0:a27e0d3581d1 536 {
nsrwsurasak 0:a27e0d3581d1 537 return m_varList;
nsrwsurasak 0:a27e0d3581d1 538 }
Lucyjungz 14:4ba6147f067b 539
Lucyjungz 11:e21d4c5bfd1b 540 /**
Lucyjungz 11:e21d4c5bfd1b 541 * @brief Utility to play with LED status
Lucyjungz 11:e21d4c5bfd1b 542 * @note Libary user need to assign proper PinName to LED_SDCARD
Lucyjungz 11:e21d4c5bfd1b 543 * @param on - True for turning LED ON, otherwise LED off
Lucyjungz 11:e21d4c5bfd1b 544 * @retval None
Lucyjungz 11:e21d4c5bfd1b 545 */
Lucyjungz 14:4ba6147f067b 546 static void FILEMANAGER_SetLedStatus(bool on)
Lucyjungz 11:e21d4c5bfd1b 547 {
Lucyjungz 12:0045fca3c160 548 /* Check LED Connection */
Lucyjungz 11:e21d4c5bfd1b 549 if (ledStatus.is_connected())
Lucyjungz 11:e21d4c5bfd1b 550 {
Lucyjungz 12:0045fca3c160 551 /* Set LED regarding to given argment */
Lucyjungz 11:e21d4c5bfd1b 552 ledStatus = on;
Lucyjungz 12:0045fca3c160 553 }
Lucyjungz 12:0045fca3c160 554 #if DEBUG
Lucyjungz 12:0045fca3c160 555 else
Lucyjungz 12:0045fca3c160 556 {
Lucyjungz 14:4ba6147f067b 557 printf("\r\nSDCard LED is not connected !!");
Lucyjungz 12:0045fca3c160 558 }
Lucyjungz 12:0045fca3c160 559 #endif
Lucyjungz 11:e21d4c5bfd1b 560 }
Lucyjungz 15:b63a539c3754 561