File manager

Dependencies:   SDFileSystem

Dependents:   RwSDCard_Xml_GPS

Committer:
Lucyjungz
Date:
Thu May 19 10:44:35 2016 +0000
Revision:
14:4ba6147f067b
Parent:
13:d83e2dcc882d
Child:
15:b63a539c3754
Update Prefix / Comment

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 3:6e08d0bba1bb 338 for(int i = 0; i < m_amountVarList; i++)
Lucyjungz 3:6e08d0bba1bb 339 {
Lucyjungz 3:6e08d0bba1bb 340 fprintf(fp, ",%s",m_varList[i].varName);
Lucyjungz 3:6e08d0bba1bb 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 14:4ba6147f067b 561 /**
Lucyjungz 14:4ba6147f067b 562 * @brief Function to init File System
Lucyjungz 14:4ba6147f067b 563 * @note Must be called during initialization
Lucyjungz 14:4ba6147f067b 564 * @param
Lucyjungz 14:4ba6147f067b 565 * @retval
Lucyjungz 14:4ba6147f067b 566 */
Lucyjungz 14:4ba6147f067b 567 void FILEMANAGER_Init(void)
Lucyjungz 14:4ba6147f067b 568 {
Lucyjungz 14:4ba6147f067b 569 SDFileSystem sd(A6, A5, A4, A0, DEVICE_NAME); // MOSI, MISO, SCK, CS
Lucyjungz 14:4ba6147f067b 570 }