File manager

Dependencies:   SDFileSystem

Dependents:   RwSDCard_Xml_GPS

Committer:
Lucyjungz
Date:
Wed May 18 07:41:14 2016 +0000
Revision:
9:b9115b3a77f9
Parent:
8:5af4e12c43b2
Parent:
7:ab015947e368
Child:
10:a8003d357cf2
Support MSecond in RMS log

Who changed what in which revision?

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