File manager

Dependencies:   SDFileSystem

Dependents:   RwSDCard_Xml_GPS

Committer:
Lucyjungz
Date:
Tue May 17 10:22:32 2016 +0000
Revision:
6:5bd75c0f607c
Parent:
5:7c513eee7b2b
Child:
7:ab015947e368
Child:
8:5af4e12c43b2
Add support for LSB1/ LSB2 /BitMask

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);
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 */
Lucyjungz 4:ec62bf823914 149 FileManager_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 4:ec62bf823914 167 void FileManager_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 4:ec62bf823914 173 FileManager_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 4:ec62bf823914 203 void FileManager_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 4:ec62bf823914 209 FileManager_GenerateFileNameWithTime(timestamp,file_name);
Lucyjungz 4:ec62bf823914 210 if (!FileManager_IsFileExist(file_name))
Lucyjungz 1:1f1f2b99756b 211 {
Lucyjungz 3:6e08d0bba1bb 212 /* If file is not exist, log the header */
Lucyjungz 4:ec62bf823914 213 FileManager_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 4:ec62bf823914 250 void FileManager_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 4:ec62bf823914 256 FileManager_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 4:ec62bf823914 292 void FileManager_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 */
Lucyjungz 4:ec62bf823914 314 void FileManager_Deletefile(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 4:ec62bf823914 331 bool FileManager_IsFileExist(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 */
Lucyjungz 4:ec62bf823914 348 int FileManager_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 */
Lucyjungz 4:ec62bf823914 359 int FileManager_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 */
Lucyjungz 4:ec62bf823914 370 Variable_Data_TypeDef * FileManager_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*/
Lucyjungz 4:ec62bf823914 399 FileManager_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*/
Lucyjungz 4:ec62bf823914 405 FileManager_GetXmlText(buf , m_varList[index].varAddress);
Lucyjungz 5:7c513eee7b2b 406 }
Lucyjungz 6:5bd75c0f607c 407 else if (strstr (buf,VAR_LSB1_TAG))
Lucyjungz 6:5bd75c0f607c 408 {
Lucyjungz 6:5bd75c0f607c 409 /* Found variable LSB1 TAG, populate it*/
Lucyjungz 6:5bd75c0f607c 410 FileManager_GetXmlText(buf , m_varList[index].varLSB1);
Lucyjungz 6:5bd75c0f607c 411 }
Lucyjungz 6:5bd75c0f607c 412 else if (strstr (buf,VAR_LSB2_TAG))
Lucyjungz 5:7c513eee7b2b 413 {
Lucyjungz 6:5bd75c0f607c 414 /* Found variable LSB2 TAG, populate it*/
Lucyjungz 6:5bd75c0f607c 415 FileManager_GetXmlText(buf , m_varList[index].varLSB2);
Lucyjungz 6:5bd75c0f607c 416 }
Lucyjungz 6:5bd75c0f607c 417 else if (strstr (buf,VAR_BITMASK_TAG))
Lucyjungz 6:5bd75c0f607c 418 {
Lucyjungz 6:5bd75c0f607c 419 /* Found variable BitMask TAG, populate it*/
Lucyjungz 6:5bd75c0f607c 420 FileManager_GetXmlText(buf , m_varList[index].varBitMask);
Lucyjungz 5:7c513eee7b2b 421 }
Lucyjungz 5:7c513eee7b2b 422 else if (strstr (buf,VAR_UNIT_TAG))
Lucyjungz 5:7c513eee7b2b 423 {
Lucyjungz 5:7c513eee7b2b 424 /* Found variable unit TAG, populate it*/
Lucyjungz 5:7c513eee7b2b 425 FileManager_GetXmlText(buf , m_varList[index].varUnit);
nsrwsurasak 0:a27e0d3581d1 426 index++;
nsrwsurasak 0:a27e0d3581d1 427 }
nsrwsurasak 0:a27e0d3581d1 428
nsrwsurasak 0:a27e0d3581d1 429 }
Lucyjungz 3:6e08d0bba1bb 430 /* Close File */
Lucyjungz 3:6e08d0bba1bb 431 fclose(fp);
Lucyjungz 3:6e08d0bba1bb 432
Lucyjungz 3:6e08d0bba1bb 433 /* Populate amount */
nsrwsurasak 0:a27e0d3581d1 434 m_amountVarList = index;
Lucyjungz 3:6e08d0bba1bb 435
Lucyjungz 3:6e08d0bba1bb 436 /* Return variable list */
nsrwsurasak 0:a27e0d3581d1 437 return m_varList;
nsrwsurasak 0:a27e0d3581d1 438 }
nsrwsurasak 0:a27e0d3581d1 439 }
Lucyjungz 3:6e08d0bba1bb 440 /**
Lucyjungz 3:6e08d0bba1bb 441 * @brief Function to amount of variable for data logging
Lucyjungz 3:6e08d0bba1bb 442 * @note Must be called after readVarFile
Lucyjungz 3:6e08d0bba1bb 443 * @param None
Lucyjungz 3:6e08d0bba1bb 444 * @retval amount of variable list
Lucyjungz 3:6e08d0bba1bb 445 */
Lucyjungz 4:ec62bf823914 446 int FileManager_GetAmountVarList()
nsrwsurasak 0:a27e0d3581d1 447 {
nsrwsurasak 0:a27e0d3581d1 448 return m_amountVarList;
nsrwsurasak 0:a27e0d3581d1 449 }
Lucyjungz 3:6e08d0bba1bb 450 /**
Lucyjungz 3:6e08d0bba1bb 451 * @brief Function to get variable list
Lucyjungz 3:6e08d0bba1bb 452 * @note Must be called after readVarFile
Lucyjungz 3:6e08d0bba1bb 453 * @param None
Lucyjungz 3:6e08d0bba1bb 454 * @retval pointer to variable list
Lucyjungz 3:6e08d0bba1bb 455 */
Lucyjungz 4:ec62bf823914 456 Variable_Data_TypeDef * FileManager_GetVarList()
nsrwsurasak 0:a27e0d3581d1 457 {
nsrwsurasak 0:a27e0d3581d1 458 return m_varList;
nsrwsurasak 0:a27e0d3581d1 459 }
nsrwsurasak 0:a27e0d3581d1 460
nsrwsurasak 0:a27e0d3581d1 461