File manager

Dependencies:   SDFileSystem

Dependents:   RwSDCard_Xml_GPS

Committer:
Lucyjungz
Date:
Sun May 15 09:56:42 2016 +0000
Revision:
3:6e08d0bba1bb
Parent:
2:18e004a47f52
Child:
4:ec62bf823914
Update comment

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
nsrwsurasak 0:a27e0d3581d1 12 static void removeSpaces(char* s , int size);
nsrwsurasak 0:a27e0d3581d1 13 static void getXmlText(char *str, char *ret);
Lucyjungz 1:1f1f2b99756b 14 static void 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 */
nsrwsurasak 0:a27e0d3581d1 24 static void 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 */
nsrwsurasak 0:a27e0d3581d1 44 static void 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 */
nsrwsurasak 0:a27e0d3581d1 73 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 1:1f1f2b99756b 82 static void 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 */
nsrwsurasak 0:a27e0d3581d1 108 void 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 */
nsrwsurasak 0:a27e0d3581d1 142 getXmlText(buf, m_GpsInterval);
nsrwsurasak 0:a27e0d3581d1 143 printf("\r\n-found GPS interval %s ", m_GpsInterval);
nsrwsurasak 0:a27e0d3581d1 144 state = STATE_FINDING;
Lucyjungz 3:6e08d0bba1bb 145 }
Lucyjungz 3:6e08d0bba1bb 146 else if(state == STATE_FOUND_DATA)
Lucyjungz 3:6e08d0bba1bb 147 {
Lucyjungz 3:6e08d0bba1bb 148 /* Get XML text for Data Interval */
nsrwsurasak 0:a27e0d3581d1 149 getXmlText(buf, m_DataInterval);
nsrwsurasak 0:a27e0d3581d1 150 printf("\r\n-found Data interval %s ", m_DataInterval);
nsrwsurasak 0:a27e0d3581d1 151 state = STATE_FINDING;
nsrwsurasak 0:a27e0d3581d1 152 }
nsrwsurasak 0:a27e0d3581d1 153 }
nsrwsurasak 0:a27e0d3581d1 154 }
Lucyjungz 3:6e08d0bba1bb 155 /* Ensure file is closed */
Lucyjungz 3:6e08d0bba1bb 156 fclose(fp);
nsrwsurasak 0:a27e0d3581d1 157 }
nsrwsurasak 0:a27e0d3581d1 158 }
Lucyjungz 3:6e08d0bba1bb 159 /**
Lucyjungz 3:6e08d0bba1bb 160 * @brief Function to log GPS Data
Lucyjungz 3:6e08d0bba1bb 161 * @note
Lucyjungz 3:6e08d0bba1bb 162 * @param timestamp - time structure to get Date
Lucyjungz 3:6e08d0bba1bb 163 * @param lat - char array for lattitude
Lucyjungz 3:6e08d0bba1bb 164 * @param longti - char array for longtitude
Lucyjungz 3:6e08d0bba1bb 165 * @retval None
Lucyjungz 3:6e08d0bba1bb 166 */
Lucyjungz 1:1f1f2b99756b 167 void logGPSData(time_t timestamp ,char lat[], char longti[])
nsrwsurasak 0:a27e0d3581d1 168 {
Lucyjungz 3:6e08d0bba1bb 169 /* Get File name */
Lucyjungz 1:1f1f2b99756b 170 char file_name[] = GPS_LOG_FILE_NAME;
Lucyjungz 1:1f1f2b99756b 171
Lucyjungz 3:6e08d0bba1bb 172 /* Generate file name with time stamp */
Lucyjungz 1:1f1f2b99756b 173 generateFileNameWithTime(timestamp,file_name);
Lucyjungz 3:6e08d0bba1bb 174
Lucyjungz 3:6e08d0bba1bb 175 /* Open file with "APPEND" mode */
Lucyjungz 1:1f1f2b99756b 176 FILE *fp = fopen(file_name, "a");
Lucyjungz 1:1f1f2b99756b 177
Lucyjungz 3:6e08d0bba1bb 178 if (fp == NULL)
Lucyjungz 3:6e08d0bba1bb 179 {
Lucyjungz 3:6e08d0bba1bb 180 /* if it can't open the file then print error message */
Lucyjungz 1:1f1f2b99756b 181 printf("Error! Unable to open file %s!\n",file_name);
Lucyjungz 3:6e08d0bba1bb 182 }
Lucyjungz 3:6e08d0bba1bb 183 else {
Lucyjungz 3:6e08d0bba1bb 184 /* Print some message for information */
Lucyjungz 1:1f1f2b99756b 185 printf("\r\n Writing to Gps Log File (%s)....",file_name);
Lucyjungz 1:1f1f2b99756b 186
Lucyjungz 3:6e08d0bba1bb 187 /* Write the line with lattitude and longtitude */
Lucyjungz 3:6e08d0bba1bb 188 fprintf(fp, "%d,%s,%s\n",timestamp,lat,longti);
Lucyjungz 1:1f1f2b99756b 189
Lucyjungz 1:1f1f2b99756b 190 printf("Done");
Lucyjungz 3:6e08d0bba1bb 191 /* Close file once it done */
Lucyjungz 3:6e08d0bba1bb 192 fclose(fp);
Lucyjungz 1:1f1f2b99756b 193 }
Lucyjungz 1:1f1f2b99756b 194 }
Lucyjungz 3:6e08d0bba1bb 195 /**
Lucyjungz 3:6e08d0bba1bb 196 * @brief Function to log RMS Data
Lucyjungz 3:6e08d0bba1bb 197 * @note sequence must be in the same order with variableList
Lucyjungz 3:6e08d0bba1bb 198 * @param timestamp - time structure to get Date
Lucyjungz 3:6e08d0bba1bb 199 * @param var - float array to be log in the file
Lucyjungz 3:6e08d0bba1bb 200 * @param size - size of the float array
Lucyjungz 3:6e08d0bba1bb 201 * @retval None
Lucyjungz 3:6e08d0bba1bb 202 */
Lucyjungz 1:1f1f2b99756b 203 void logRMSData(time_t timestamp ,float * var, int size)
Lucyjungz 1:1f1f2b99756b 204 {
Lucyjungz 3:6e08d0bba1bb 205 /* Get File name */
Lucyjungz 1:1f1f2b99756b 206 char file_name[] = RTL_LOG_FILE_NAME;
Lucyjungz 1:1f1f2b99756b 207
Lucyjungz 3:6e08d0bba1bb 208 /* Generate File name with timestamp */
Lucyjungz 1:1f1f2b99756b 209 generateFileNameWithTime(timestamp,file_name);
Lucyjungz 1:1f1f2b99756b 210 if (!is_file_exist(file_name))
Lucyjungz 1:1f1f2b99756b 211 {
Lucyjungz 3:6e08d0bba1bb 212 /* If file is not exist, log the header */
Lucyjungz 1:1f1f2b99756b 213 logRMSHeader(timestamp);
Lucyjungz 1:1f1f2b99756b 214 }
Lucyjungz 3:6e08d0bba1bb 215 /* Open file with "APPEND" mode */
Lucyjungz 1:1f1f2b99756b 216 FILE *fp = fopen(file_name, "a");
nsrwsurasak 0:a27e0d3581d1 217
Lucyjungz 3:6e08d0bba1bb 218 if (fp == NULL)
Lucyjungz 3:6e08d0bba1bb 219 {
Lucyjungz 3:6e08d0bba1bb 220 /* In case of error, print the error message */
Lucyjungz 1:1f1f2b99756b 221 printf("Error! Unable to open file %s!\n",file_name);
Lucyjungz 3:6e08d0bba1bb 222 }
Lucyjungz 3:6e08d0bba1bb 223 else
Lucyjungz 3:6e08d0bba1bb 224 {
Lucyjungz 3:6e08d0bba1bb 225 /* Print some message for information */
Lucyjungz 1:1f1f2b99756b 226 printf("\r\n Writing to Log File (%s)....",file_name);
Lucyjungz 1:1f1f2b99756b 227
Lucyjungz 3:6e08d0bba1bb 228 /* Write timestamp */
Lucyjungz 3:6e08d0bba1bb 229 fprintf(fp, "%d",timestamp);
Lucyjungz 1:1f1f2b99756b 230
Lucyjungz 3:6e08d0bba1bb 231 /* Write variable data */
Lucyjungz 1:1f1f2b99756b 232 for(int i = 0; i < size; i++)
Lucyjungz 1:1f1f2b99756b 233 {
Lucyjungz 3:6e08d0bba1bb 234 fprintf(fp, ",%f",var[i]);
Lucyjungz 1:1f1f2b99756b 235 }
Lucyjungz 3:6e08d0bba1bb 236 /* Write new line as we done */
Lucyjungz 3:6e08d0bba1bb 237 fprintf(fp, "\n");
nsrwsurasak 0:a27e0d3581d1 238
Lucyjungz 1:1f1f2b99756b 239 printf("Done");
Lucyjungz 3:6e08d0bba1bb 240 /* Close the file */
Lucyjungz 3:6e08d0bba1bb 241 fclose(fp);
Lucyjungz 3:6e08d0bba1bb 242 }
Lucyjungz 3:6e08d0bba1bb 243 }
Lucyjungz 3:6e08d0bba1bb 244 /**
Lucyjungz 3:6e08d0bba1bb 245 * @brief Function to log RMS Header
Lucyjungz 3:6e08d0bba1bb 246 * @note sequence must be in the same order with variableList
Lucyjungz 3:6e08d0bba1bb 247 * @param timestamp - time structure to get Date
Lucyjungz 3:6e08d0bba1bb 248 * @retval None
Lucyjungz 3:6e08d0bba1bb 249 */
Lucyjungz 3:6e08d0bba1bb 250 void logRMSHeader(time_t timestamp)
Lucyjungz 3:6e08d0bba1bb 251 {
Lucyjungz 3:6e08d0bba1bb 252 /* Get File name */
Lucyjungz 3:6e08d0bba1bb 253 char file_name[] = RTL_LOG_FILE_NAME;
Lucyjungz 3:6e08d0bba1bb 254
Lucyjungz 3:6e08d0bba1bb 255 /* Generate file name with time */
Lucyjungz 3:6e08d0bba1bb 256 generateFileNameWithTime(timestamp,file_name);
Lucyjungz 3:6e08d0bba1bb 257
Lucyjungz 3:6e08d0bba1bb 258 /* Open file in append mode */
Lucyjungz 3:6e08d0bba1bb 259 FILE *fp = fopen(file_name, "a");
Lucyjungz 3:6e08d0bba1bb 260
Lucyjungz 3:6e08d0bba1bb 261 if (fp == NULL)
Lucyjungz 3:6e08d0bba1bb 262 {
Lucyjungz 3:6e08d0bba1bb 263 /* In case of error, print the error message */
Lucyjungz 3:6e08d0bba1bb 264 printf("Error! Unable to open file %s!\n",file_name);
Lucyjungz 3:6e08d0bba1bb 265 }
Lucyjungz 3:6e08d0bba1bb 266 else
Lucyjungz 3:6e08d0bba1bb 267 {
Lucyjungz 3:6e08d0bba1bb 268 /* opened file so can write */
Lucyjungz 3:6e08d0bba1bb 269 printf("\r\n Writing to Log File (%s)....",file_name);
Lucyjungz 3:6e08d0bba1bb 270
Lucyjungz 3:6e08d0bba1bb 271 /* Write the header to the file */
Lucyjungz 3:6e08d0bba1bb 272 fprintf(fp, "%s",RMS_HEADER_TIME);
Lucyjungz 3:6e08d0bba1bb 273 for(int i = 0; i < m_amountVarList; i++)
Lucyjungz 3:6e08d0bba1bb 274 {
Lucyjungz 3:6e08d0bba1bb 275 fprintf(fp, ",%s",m_varList[i].varName);
Lucyjungz 3:6e08d0bba1bb 276 }
Lucyjungz 3:6e08d0bba1bb 277 /* Write new line as done */
Lucyjungz 3:6e08d0bba1bb 278 fprintf(fp, "\n");
Lucyjungz 3:6e08d0bba1bb 279
Lucyjungz 3:6e08d0bba1bb 280 printf("Done");
Lucyjungz 3:6e08d0bba1bb 281
Lucyjungz 3:6e08d0bba1bb 282 /* Close the file */
Lucyjungz 3:6e08d0bba1bb 283 fclose(fp);
Lucyjungz 3:6e08d0bba1bb 284 }
Lucyjungz 3:6e08d0bba1bb 285 }
Lucyjungz 3:6e08d0bba1bb 286 /**
Lucyjungz 3:6e08d0bba1bb 287 * @brief Function to log Mini RMS System Data
Lucyjungz 3:6e08d0bba1bb 288 * @note this file is only for debug
Lucyjungz 3:6e08d0bba1bb 289 * @param gps_interval -
Lucyjungz 3:6e08d0bba1bb 290 * @retval None
Lucyjungz 3:6e08d0bba1bb 291 */
Lucyjungz 3:6e08d0bba1bb 292 void logSystemData(float gps_interval)
Lucyjungz 3:6e08d0bba1bb 293 {
Lucyjungz 3:6e08d0bba1bb 294 /* Open the file in append mode */
Lucyjungz 3:6e08d0bba1bb 295 FILE *fp = fopen(MINIRMS_LOG_FILE_NAME, "a");
Lucyjungz 3:6e08d0bba1bb 296
Lucyjungz 3:6e08d0bba1bb 297 if (fp == NULL) {
Lucyjungz 3:6e08d0bba1bb 298 /* In case of error, print the msg */
Lucyjungz 3:6e08d0bba1bb 299 printf("Error! Unable to open file!\n");
Lucyjungz 3:6e08d0bba1bb 300 }
Lucyjungz 3:6e08d0bba1bb 301 else
Lucyjungz 3:6e08d0bba1bb 302 {
Lucyjungz 3:6e08d0bba1bb 303 /* Write some message, which is TBD */
Lucyjungz 3:6e08d0bba1bb 304 fprintf(fp, "\nStart Mini-RMS System with Gps Interval = %f",gps_interval);
Lucyjungz 1:1f1f2b99756b 305 fclose(fp); // ensure you close the file after writing
Lucyjungz 1:1f1f2b99756b 306 }
Lucyjungz 1:1f1f2b99756b 307 }
Lucyjungz 3:6e08d0bba1bb 308 /**
Lucyjungz 3:6e08d0bba1bb 309 * @brief Utility to delete file
Lucyjungz 3:6e08d0bba1bb 310 * @note
Lucyjungz 3:6e08d0bba1bb 311 * @param filename - file name to be deleted
Lucyjungz 3:6e08d0bba1bb 312 * @retval None
Lucyjungz 3:6e08d0bba1bb 313 */
nsrwsurasak 0:a27e0d3581d1 314 void delete_file(char filename[])
nsrwsurasak 0:a27e0d3581d1 315 {
nsrwsurasak 0:a27e0d3581d1 316 printf("Deleting file '%s'...",filename);
nsrwsurasak 0:a27e0d3581d1 317 FILE *fp = fopen(filename, "r"); // try and open file
nsrwsurasak 0:a27e0d3581d1 318 if (fp != NULL) { // if it does open...
nsrwsurasak 0:a27e0d3581d1 319 fclose(fp); // close it
nsrwsurasak 0:a27e0d3581d1 320 remove(filename); // and then delete
nsrwsurasak 0:a27e0d3581d1 321 printf("Done!\n");
nsrwsurasak 0:a27e0d3581d1 322 }
nsrwsurasak 0:a27e0d3581d1 323 // if we can't open it, it doesn't exist and so we can't delete it
nsrwsurasak 0:a27e0d3581d1 324 }
Lucyjungz 3:6e08d0bba1bb 325 /**
Lucyjungz 3:6e08d0bba1bb 326 * @brief Utility to check file available
Lucyjungz 3:6e08d0bba1bb 327 * @note
Lucyjungz 3:6e08d0bba1bb 328 * @param filename - file name to be checked
Lucyjungz 3:6e08d0bba1bb 329 * @retval true - file is exist , false - file is not exist
Lucyjungz 3:6e08d0bba1bb 330 */
Lucyjungz 1:1f1f2b99756b 331 bool is_file_exist(char filename[])
Lucyjungz 1:1f1f2b99756b 332 {
Lucyjungz 1:1f1f2b99756b 333 bool exist = false;
Lucyjungz 1:1f1f2b99756b 334 FILE *fp = fopen(filename, "r"); // try and open file
Lucyjungz 1:1f1f2b99756b 335 if (fp != NULL) { // if it does open...
Lucyjungz 1:1f1f2b99756b 336 fclose(fp); // close it
Lucyjungz 1:1f1f2b99756b 337 exist = true;
Lucyjungz 1:1f1f2b99756b 338 }
Lucyjungz 1:1f1f2b99756b 339
Lucyjungz 1:1f1f2b99756b 340 return exist;
Lucyjungz 1:1f1f2b99756b 341 }
Lucyjungz 3:6e08d0bba1bb 342 /**
Lucyjungz 3:6e08d0bba1bb 343 * @brief Utility to get GPS Interval
Lucyjungz 3:6e08d0bba1bb 344 * @note must be after read the setup file
Lucyjungz 3:6e08d0bba1bb 345 * @param None
Lucyjungz 3:6e08d0bba1bb 346 * @retval GPS interval
Lucyjungz 3:6e08d0bba1bb 347 */
nsrwsurasak 0:a27e0d3581d1 348 int GPSInterval()
nsrwsurasak 0:a27e0d3581d1 349 {
Lucyjungz 3:6e08d0bba1bb 350 /* Return interval in int */
nsrwsurasak 0:a27e0d3581d1 351 return atoi( m_GpsInterval );
nsrwsurasak 0:a27e0d3581d1 352 }
Lucyjungz 3:6e08d0bba1bb 353 /**
Lucyjungz 3:6e08d0bba1bb 354 * @brief Utility to get Data Interval
Lucyjungz 3:6e08d0bba1bb 355 * @note must be after read the setup file
Lucyjungz 3:6e08d0bba1bb 356 * @param None
Lucyjungz 3:6e08d0bba1bb 357 * @retval Data interval
Lucyjungz 3:6e08d0bba1bb 358 */
nsrwsurasak 0:a27e0d3581d1 359 int DataInterval()
nsrwsurasak 0:a27e0d3581d1 360 {
Lucyjungz 3:6e08d0bba1bb 361 /* Return interval in int */
nsrwsurasak 0:a27e0d3581d1 362 return atoi( m_DataInterval );
nsrwsurasak 0:a27e0d3581d1 363 }
Lucyjungz 3:6e08d0bba1bb 364 /**
Lucyjungz 3:6e08d0bba1bb 365 * @brief Function to read the variable list file
Lucyjungz 3:6e08d0bba1bb 366 * @note Recommended to call this function at initilization phase
Lucyjungz 3:6e08d0bba1bb 367 * @param None
Lucyjungz 3:6e08d0bba1bb 368 * @retval pointer to variable list
Lucyjungz 3:6e08d0bba1bb 369 */
nsrwsurasak 0:a27e0d3581d1 370 Variable_Data_TypeDef * readVarFile()
nsrwsurasak 0:a27e0d3581d1 371 {
Lucyjungz 3:6e08d0bba1bb 372 /* Open the file with reading mode */
nsrwsurasak 0:a27e0d3581d1 373 FILE *fp = fopen(VARIABLE_FILE_NAME, "r");
nsrwsurasak 0:a27e0d3581d1 374
Lucyjungz 3:6e08d0bba1bb 375 if (fp == NULL)
Lucyjungz 3:6e08d0bba1bb 376 {
Lucyjungz 3:6e08d0bba1bb 377 /* if it can't open the file then print error message */
nsrwsurasak 0:a27e0d3581d1 378 printf("\nError! Unable to open file! %s \n", VARIABLE_FILE_NAME);
nsrwsurasak 0:a27e0d3581d1 379 return NULL;
Lucyjungz 3:6e08d0bba1bb 380 }
Lucyjungz 3:6e08d0bba1bb 381 else
Lucyjungz 3:6e08d0bba1bb 382 {
Lucyjungz 3:6e08d0bba1bb 383 /* opened file so can write */
nsrwsurasak 0:a27e0d3581d1 384
Lucyjungz 3:6e08d0bba1bb 385 /* Allocate buffer for reading */
nsrwsurasak 0:a27e0d3581d1 386 char buf[1024];
nsrwsurasak 0:a27e0d3581d1 387 int index = 0;
Lucyjungz 3:6e08d0bba1bb 388
Lucyjungz 3:6e08d0bba1bb 389 /* Initialize return value */
nsrwsurasak 0:a27e0d3581d1 390 memset(m_varList, ' ', sizeof(m_varList));
Lucyjungz 3:6e08d0bba1bb 391
Lucyjungz 3:6e08d0bba1bb 392 /* Read line from the file */
Lucyjungz 3:6e08d0bba1bb 393 while (fgets(buf, sizeof(buf), fp) != NULL)
Lucyjungz 3:6e08d0bba1bb 394 {
Lucyjungz 3:6e08d0bba1bb 395 /* Check the TAG */
Lucyjungz 3:6e08d0bba1bb 396 if (strstr (buf,VAR_NAME_TAG))
Lucyjungz 3:6e08d0bba1bb 397 {
Lucyjungz 3:6e08d0bba1bb 398 /* Found variable TAG, populate it*/
nsrwsurasak 0:a27e0d3581d1 399 getXmlText(buf , m_varList[index].varName);
nsrwsurasak 0:a27e0d3581d1 400
Lucyjungz 3:6e08d0bba1bb 401 }
Lucyjungz 3:6e08d0bba1bb 402 else if (strstr (buf,VAR_ADDR_TAG))
Lucyjungz 3:6e08d0bba1bb 403 {
Lucyjungz 3:6e08d0bba1bb 404 /* Found variable address TAG, populate it*/
nsrwsurasak 0:a27e0d3581d1 405 getXmlText(buf , m_varList[index].varAddress);
nsrwsurasak 0:a27e0d3581d1 406 index++;
nsrwsurasak 0:a27e0d3581d1 407 }
nsrwsurasak 0:a27e0d3581d1 408
nsrwsurasak 0:a27e0d3581d1 409 }
Lucyjungz 3:6e08d0bba1bb 410 /* Close File */
Lucyjungz 3:6e08d0bba1bb 411 fclose(fp);
Lucyjungz 3:6e08d0bba1bb 412
Lucyjungz 3:6e08d0bba1bb 413 /* Populate amount */
nsrwsurasak 0:a27e0d3581d1 414 m_amountVarList = index;
Lucyjungz 3:6e08d0bba1bb 415
Lucyjungz 3:6e08d0bba1bb 416 /* Return variable list */
nsrwsurasak 0:a27e0d3581d1 417 return m_varList;
nsrwsurasak 0:a27e0d3581d1 418 }
nsrwsurasak 0:a27e0d3581d1 419 }
Lucyjungz 3:6e08d0bba1bb 420 /**
Lucyjungz 3:6e08d0bba1bb 421 * @brief Function to amount of variable for data logging
Lucyjungz 3:6e08d0bba1bb 422 * @note Must be called after readVarFile
Lucyjungz 3:6e08d0bba1bb 423 * @param None
Lucyjungz 3:6e08d0bba1bb 424 * @retval amount of variable list
Lucyjungz 3:6e08d0bba1bb 425 */
nsrwsurasak 0:a27e0d3581d1 426 int getAmountVarList()
nsrwsurasak 0:a27e0d3581d1 427 {
nsrwsurasak 0:a27e0d3581d1 428 return m_amountVarList;
nsrwsurasak 0:a27e0d3581d1 429 }
Lucyjungz 3:6e08d0bba1bb 430 /**
Lucyjungz 3:6e08d0bba1bb 431 * @brief Function to get variable list
Lucyjungz 3:6e08d0bba1bb 432 * @note Must be called after readVarFile
Lucyjungz 3:6e08d0bba1bb 433 * @param None
Lucyjungz 3:6e08d0bba1bb 434 * @retval pointer to variable list
Lucyjungz 3:6e08d0bba1bb 435 */
nsrwsurasak 0:a27e0d3581d1 436 Variable_Data_TypeDef * getVarList()
nsrwsurasak 0:a27e0d3581d1 437 {
nsrwsurasak 0:a27e0d3581d1 438 return m_varList;
nsrwsurasak 0:a27e0d3581d1 439 }
nsrwsurasak 0:a27e0d3581d1 440
nsrwsurasak 0:a27e0d3581d1 441