File manager

Dependencies:   SDFileSystem

Dependents:   RwSDCard_Xml_GPS

Committer:
Lucyjungz
Date:
Tue Sep 13 09:22:31 2016 +0000
Revision:
32:69b7c870d69c
Parent:
31:983c30829d24
Child:
33:dd67a980e9d8
Support UART File Access

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Lucyjungz 14:4ba6147f067b 1 /**
Lucyjungz 14:4ba6147f067b 2 ******************************************************************************
Lucyjungz 14:4ba6147f067b 3 * @file FileManager.cpp
Lucyjungz 14:4ba6147f067b 4 * @author Narut T
Lucyjungz 14:4ba6147f067b 5 * @version V1
Lucyjungz 14:4ba6147f067b 6 * @date 19/05/2016
Lucyjungz 14:4ba6147f067b 7 * @brief File Manager for managing file system in SD Card
Lucyjungz 14:4ba6147f067b 8 ******************************************************************************/
Lucyjungz 14:4ba6147f067b 9
nsrwsurasak 13:d83e2dcc882d 10 #include "main.h"
nsrwsurasak 0:a27e0d3581d1 11
Lucyjungz 29:964610d82f8d 12 char m_StrGpsInterval[XMLTEXT_SIZE]; // GPS Interval
Lucyjungz 29:964610d82f8d 13 char m_StrDataInterval[XMLTEXT_SIZE]; // Data Interval
Lucyjungz 14:4ba6147f067b 14
Lucyjungz 14:4ba6147f067b 15 uint32_t m_GpsInterval;
Lucyjungz 14:4ba6147f067b 16 uint32_t m_DataInterval;
Lucyjungz 29:964610d82f8d 17 Variable_Data_TypeDef m_varList[MAX_VAR]; // Variable List
Lucyjungz 29:964610d82f8d 18 unsigned int m_amountVarList = 0; // Amount of variable list
Lucyjungz 29:964610d82f8d 19 bool m_SdCardHasDisconnected = false; // flag to indicate sd card has disconnected
Lucyjungz 21:0c52dbc23f52 20 char m_varListFileName[MAX_FILE_NAME_SIZE]; // Variable File Name
Lucyjungz 29:964610d82f8d 21 bool m_sdCardIsPresent; // Flag to indicate sd card is present
Lucyjungz 29:964610d82f8d 22
Lucyjungz 29:964610d82f8d 23 uint32_t m_TxID; // Transmit ID
Lucyjungz 29:964610d82f8d 24 uint32_t m_RxID; // Receive ID
Lucyjungz 29:964610d82f8d 25
Lucyjungz 32:69b7c870d69c 26 #if UART_FILE_ACCESS
Lucyjungz 32:69b7c870d69c 27 char m_bufFileName[MAX_FILE_NAME_SIZE]; // buffer for storing File Name
Lucyjungz 32:69b7c870d69c 28 #endif
Lucyjungz 21:0c52dbc23f52 29 /** Connect pin for SD Card LED */
Lucyjungz 11:e21d4c5bfd1b 30 #ifdef LED_SDCARD
Lucyjungz 17:d24d2b2bbd42 31 DigitalOut ledStatus(LED_SDCARD);
Lucyjungz 11:e21d4c5bfd1b 32 #else
Lucyjungz 11:e21d4c5bfd1b 33 DigitalOut ledStatus(NC);
Lucyjungz 11:e21d4c5bfd1b 34 #endif
Lucyjungz 11:e21d4c5bfd1b 35
nsrwsurasak 28:12c88b5e46e5 36 #if CAPTURE_TIME
nsrwsurasak 28:12c88b5e46e5 37 Timer t;
nsrwsurasak 28:12c88b5e46e5 38 #endif
Lucyjungz 3:6e08d0bba1bb 39 /* ############### Static function prototype ################## */
nsrwsurasak 0:a27e0d3581d1 40
Lucyjungz 14:4ba6147f067b 41 static void FILEMANAGER_RemoveSpaces(char* s , int size);
Lucyjungz 14:4ba6147f067b 42 static void FILEMANAGER_GetXmlText(char *str, char *ret);
Lucyjungz 14:4ba6147f067b 43 static void FILEMANAGER_GenerateFileNameWithTime(time_t timestamp, char * file_name);
Lucyjungz 14:4ba6147f067b 44 static void FILEMANAGER_SetLedStatus(bool on);
Lucyjungz 32:69b7c870d69c 45 #if UART_FILE_ACCESS
Lucyjungz 32:69b7c870d69c 46 static void FILEMANAGER_AddPrefixFilename(char* in, char* out);
Lucyjungz 32:69b7c870d69c 47 #endif
Lucyjungz 3:6e08d0bba1bb 48
Lucyjungz 3:6e08d0bba1bb 49 /**
Lucyjungz 3:6e08d0bba1bb 50 * @brief Utility function to Remove space charector from given char array
Lucyjungz 3:6e08d0bba1bb 51 * @note
Lucyjungz 3:6e08d0bba1bb 52 * @param char array to process remove spaces
Lucyjungz 3:6e08d0bba1bb 53 * @param size of char array
Lucyjungz 3:6e08d0bba1bb 54 * @retval space removed char array
Lucyjungz 3:6e08d0bba1bb 55 */
Lucyjungz 14:4ba6147f067b 56 static void FILEMANAGER_RemoveSpaces(char* s , int size)
nsrwsurasak 0:a27e0d3581d1 57 {
nsrwsurasak 0:a27e0d3581d1 58 char* cpy = s; // an alias to iterate through s without moving s
nsrwsurasak 0:a27e0d3581d1 59 char* temp = s;
nsrwsurasak 0:a27e0d3581d1 60
nsrwsurasak 0:a27e0d3581d1 61 for (int i = 0 ; i < size ; i++) {
nsrwsurasak 0:a27e0d3581d1 62 if (*cpy != ' ')
nsrwsurasak 0:a27e0d3581d1 63 *temp++ = *cpy;
nsrwsurasak 0:a27e0d3581d1 64 cpy++;
nsrwsurasak 0:a27e0d3581d1 65 }
nsrwsurasak 0:a27e0d3581d1 66 *temp = 0;
nsrwsurasak 0:a27e0d3581d1 67 return;
nsrwsurasak 0:a27e0d3581d1 68 }
Lucyjungz 3:6e08d0bba1bb 69 /**
Lucyjungz 3:6e08d0bba1bb 70 * @brief Utility function to get XML tag
Lucyjungz 3:6e08d0bba1bb 71 * @note Only First tag will be returned
Lucyjungz 3:6e08d0bba1bb 72 * @param char array to get XML Text
Lucyjungz 3:6e08d0bba1bb 73 * @param char array to be populate XML text
Lucyjungz 3:6e08d0bba1bb 74 * @retval XML text
Lucyjungz 3:6e08d0bba1bb 75 */
Lucyjungz 14:4ba6147f067b 76 static void FILEMANAGER_GetXmlText(char *str, char *ret)
nsrwsurasak 0:a27e0d3581d1 77 {
nsrwsurasak 0:a27e0d3581d1 78 int size = strlen(str);
nsrwsurasak 0:a27e0d3581d1 79 int i;
nsrwsurasak 0:a27e0d3581d1 80 bool begin_text = false;
nsrwsurasak 0:a27e0d3581d1 81 char * ret_addr = ret;
Lucyjungz 3:6e08d0bba1bb 82 /* initialized our return value */
nsrwsurasak 0:a27e0d3581d1 83 memset (ret,' ',XMLTEXT_SIZE);
nsrwsurasak 0:a27e0d3581d1 84
Lucyjungz 3:6e08d0bba1bb 85 /* Loop to check XML tag symbols */
nsrwsurasak 0:a27e0d3581d1 86 for(i = 0; i < size ; i++) {
nsrwsurasak 0:a27e0d3581d1 87
nsrwsurasak 0:a27e0d3581d1 88 if (*str == '>') {
Lucyjungz 3:6e08d0bba1bb 89 /* Found begining of the tag */
nsrwsurasak 0:a27e0d3581d1 90 begin_text = true;
nsrwsurasak 0:a27e0d3581d1 91 } else if (begin_text && *str == '<') {
Lucyjungz 3:6e08d0bba1bb 92 /* Reach the end of text message */
nsrwsurasak 0:a27e0d3581d1 93 begin_text = false;
nsrwsurasak 0:a27e0d3581d1 94 break;
nsrwsurasak 0:a27e0d3581d1 95 } else if (begin_text && *str != ' ') {
Lucyjungz 3:6e08d0bba1bb 96 /* Populate the return value */
nsrwsurasak 0:a27e0d3581d1 97 *ret = *str;
nsrwsurasak 0:a27e0d3581d1 98 ret++;
nsrwsurasak 0:a27e0d3581d1 99 }
Lucyjungz 3:6e08d0bba1bb 100 /* Move to next char */
nsrwsurasak 0:a27e0d3581d1 101 str++;
nsrwsurasak 0:a27e0d3581d1 102 }
Lucyjungz 3:6e08d0bba1bb 103
Lucyjungz 3:6e08d0bba1bb 104 /* Remove space from return value */
Lucyjungz 14:4ba6147f067b 105 FILEMANAGER_RemoveSpaces(ret_addr, XMLTEXT_SIZE);
nsrwsurasak 0:a27e0d3581d1 106 }
Lucyjungz 3:6e08d0bba1bb 107 /**
Lucyjungz 3:6e08d0bba1bb 108 * @brief Utility function to get File Name with Date
Lucyjungz 3:6e08d0bba1bb 109 * @note Format file will be YYYY-MM-DD.filename
Lucyjungz 3:6e08d0bba1bb 110 * @param timestamp - time structure to get Date
Lucyjungz 3:6e08d0bba1bb 111 * @param file_name - char array to file name
Lucyjungz 3:6e08d0bba1bb 112 * @retval renamed file name
Lucyjungz 3:6e08d0bba1bb 113 */
Lucyjungz 14:4ba6147f067b 114 static void FILEMANAGER_GenerateFileNameWithTime(time_t timestamp, char * file_name)
Lucyjungz 1:1f1f2b99756b 115 {
Lucyjungz 14:4ba6147f067b 116 char str[RENAME_FILE_BUFFER_SIZE];
Lucyjungz 1:1f1f2b99756b 117 struct tm * ptm;
Lucyjungz 1:1f1f2b99756b 118
Lucyjungz 3:6e08d0bba1bb 119 /* Convert timestamp to readable format */
Lucyjungz 3:6e08d0bba1bb 120 ptm = localtime ( &timestamp );
Lucyjungz 3:6e08d0bba1bb 121
Lucyjungz 3:6e08d0bba1bb 122 /* Replacing YYYY to the converted year */
Lucyjungz 14:4ba6147f067b 123 sprintf(str,"%04d", ptm->tm_year + YEAR_4DIGITS_OFFSET);
Lucyjungz 14:4ba6147f067b 124 memcpy(&file_name[TIMESTAMP_YEAR_OFFSET], str, TIMESTAMP_YEAR_SIZE);
Lucyjungz 1:1f1f2b99756b 125
Lucyjungz 3:6e08d0bba1bb 126 /* Replacing MM to converted month */
Lucyjungz 1:1f1f2b99756b 127 sprintf(str,"%02d", ptm->tm_mon+1);
Lucyjungz 14:4ba6147f067b 128 memcpy(&file_name[TIMESTAMP_MONTH_OFFSET], str, TIMESTAMP_MONTH_SIZE);
Lucyjungz 1:1f1f2b99756b 129
Lucyjungz 3:6e08d0bba1bb 130 /* Replacing DD to converted date */
Lucyjungz 1:1f1f2b99756b 131 sprintf(str,"%02d", ptm->tm_mday);
Lucyjungz 14:4ba6147f067b 132 memcpy(&file_name[TIMESTAMP_DATE_OFFSET], str,TIMESTAMP_DATE_SIZE);
Lucyjungz 1:1f1f2b99756b 133 }
Lucyjungz 3:6e08d0bba1bb 134 /**
Lucyjungz 3:6e08d0bba1bb 135 * @brief Function to perform read setup file
Lucyjungz 3:6e08d0bba1bb 136 * @note filename must be defined in FileManager.h, GPS/ Data interval will be stored in dedicated variable
Lucyjungz 3:6e08d0bba1bb 137 * @param None
Lucyjungz 3:6e08d0bba1bb 138 * @retval None
Lucyjungz 3:6e08d0bba1bb 139 */
Lucyjungz 14:4ba6147f067b 140 void FILEMANAGER_ReadSetupFile()
nsrwsurasak 0:a27e0d3581d1 141 {
Lucyjungz 3:6e08d0bba1bb 142 /* Open file in reading mode */
nsrwsurasak 0:a27e0d3581d1 143 FILE *fp = fopen(SETUP_FILE_NAME, "r");
nsrwsurasak 0:a27e0d3581d1 144
Lucyjungz 3:6e08d0bba1bb 145 if (fp == NULL) {
Lucyjungz 3:6e08d0bba1bb 146 /* In case of error, print the message */
nsrwsurasak 0:a27e0d3581d1 147 printf("\nError! Unable to open file! %s \n", SETUP_FILE_NAME);
Lucyjungz 11:e21d4c5bfd1b 148
Lucyjungz 11:e21d4c5bfd1b 149 /* Indicate LED Status (OFF)*/
Lucyjungz 14:4ba6147f067b 150 FILEMANAGER_SetLedStatus(false);
Lucyjungz 3:6e08d0bba1bb 151 } else {
nsrwsurasak 0:a27e0d3581d1 152
Lucyjungz 3:6e08d0bba1bb 153 /* Initialized state */
nsrwsurasak 0:a27e0d3581d1 154 ReadingFileState state = STATE_FINDING;
Lucyjungz 3:6e08d0bba1bb 155
Lucyjungz 3:6e08d0bba1bb 156 /* Allocate buffer for reading file */
Lucyjungz 14:4ba6147f067b 157 char buf[READ_FILE_BUFFER_SIZE];
Lucyjungz 3:6e08d0bba1bb 158
Lucyjungz 11:e21d4c5bfd1b 159 /* Indicate LED Status (ON)*/
Lucyjungz 14:4ba6147f067b 160 FILEMANAGER_SetLedStatus(true);
Lucyjungz 11:e21d4c5bfd1b 161
Lucyjungz 3:6e08d0bba1bb 162 /* Read line from the file */
nsrwsurasak 0:a27e0d3581d1 163 while (fgets(buf, sizeof(buf), fp) != NULL) {
Lucyjungz 3:6e08d0bba1bb 164
Lucyjungz 3:6e08d0bba1bb 165 /* Check the TAG */
Lucyjungz 3:6e08d0bba1bb 166 if (strstr (buf,DATA_TAG))
Lucyjungz 3:6e08d0bba1bb 167 {
Lucyjungz 3:6e08d0bba1bb 168 /* Found the DATA TAG */
nsrwsurasak 0:a27e0d3581d1 169 state = STATE_FOUND_DATA;
Lucyjungz 21:0c52dbc23f52 170 }
Lucyjungz 21:0c52dbc23f52 171 else if (strstr (buf,GPS_TAG))
Lucyjungz 3:6e08d0bba1bb 172 {
Lucyjungz 3:6e08d0bba1bb 173 /* Found GPS TAG */
nsrwsurasak 0:a27e0d3581d1 174 state = STATE_FOUND_GPS;
Lucyjungz 21:0c52dbc23f52 175 }
Lucyjungz 21:0c52dbc23f52 176 else if (strstr (buf,UPDATE_INTERVAL_TAG))
Lucyjungz 3:6e08d0bba1bb 177 {
Lucyjungz 3:6e08d0bba1bb 178 /* Found Interval TAG */
Lucyjungz 3:6e08d0bba1bb 179 if (state == STATE_FOUND_GPS)
Lucyjungz 3:6e08d0bba1bb 180 {
Lucyjungz 3:6e08d0bba1bb 181 /* Get XML text for GPS Interval */
Lucyjungz 14:4ba6147f067b 182 FILEMANAGER_GetXmlText(buf, m_StrGpsInterval);
Lucyjungz 14:4ba6147f067b 183 m_GpsInterval = atoi(m_StrGpsInterval);
Lucyjungz 8:5af4e12c43b2 184 #if DEBUG
Lucyjungz 14:4ba6147f067b 185 printf("\r\n-found GPS interval %s ", m_StrGpsInterval);
Lucyjungz 8:5af4e12c43b2 186 #endif
Lucyjungz 8:5af4e12c43b2 187
Lucyjungz 8:5af4e12c43b2 188 /* Set state to indicate that we are finding */
nsrwsurasak 0:a27e0d3581d1 189 state = STATE_FINDING;
Lucyjungz 3:6e08d0bba1bb 190 }
Lucyjungz 3:6e08d0bba1bb 191 else if(state == STATE_FOUND_DATA)
Lucyjungz 3:6e08d0bba1bb 192 {
Lucyjungz 3:6e08d0bba1bb 193 /* Get XML text for Data Interval */
Lucyjungz 14:4ba6147f067b 194 FILEMANAGER_GetXmlText(buf, m_StrDataInterval);
Lucyjungz 14:4ba6147f067b 195 m_DataInterval = atoi(m_StrDataInterval);
Lucyjungz 8:5af4e12c43b2 196 #if DEBUG
Lucyjungz 14:4ba6147f067b 197 printf("\r\n-found Data interval %s ", m_StrDataInterval);
Lucyjungz 8:5af4e12c43b2 198 #endif
Lucyjungz 8:5af4e12c43b2 199
Lucyjungz 8:5af4e12c43b2 200 /* Set state to indicate that we are finding */
nsrwsurasak 0:a27e0d3581d1 201 state = STATE_FINDING;
nsrwsurasak 0:a27e0d3581d1 202 }
nsrwsurasak 0:a27e0d3581d1 203 }
Lucyjungz 21:0c52dbc23f52 204 else if (strstr (buf,VAR_LIST_FILE_TAG))
Lucyjungz 21:0c52dbc23f52 205 {
Lucyjungz 21:0c52dbc23f52 206 /* Get XML text for Variable File Name */
Lucyjungz 21:0c52dbc23f52 207 char bufFileName[MAX_FILE_NAME_SIZE];
Lucyjungz 21:0c52dbc23f52 208 FILEMANAGER_GetXmlText(buf, bufFileName);
Lucyjungz 29:964610d82f8d 209
Lucyjungz 29:964610d82f8d 210 /* Get Copy to dedicated variable */
Lucyjungz 21:0c52dbc23f52 211 memcpy(m_varListFileName,PREFIX_FILE_NAME,strlen(PREFIX_FILE_NAME));
Lucyjungz 21:0c52dbc23f52 212 memcpy(&m_varListFileName[strlen(PREFIX_FILE_NAME)],bufFileName,strlen(bufFileName));
Lucyjungz 21:0c52dbc23f52 213
Lucyjungz 21:0c52dbc23f52 214 }
Lucyjungz 29:964610d82f8d 215 else if (strstr (buf,RMS_DEVICE_TAG))
Lucyjungz 29:964610d82f8d 216 {
Lucyjungz 29:964610d82f8d 217 /* Get XML text for RMS device TAG */
Lucyjungz 29:964610d82f8d 218 char bufDevice[XMLTEXT_SIZE];
Lucyjungz 29:964610d82f8d 219 FILEMANAGER_GetXmlText(buf, bufDevice);
Lucyjungz 29:964610d82f8d 220 m_TxID = atoi(bufDevice);
Lucyjungz 29:964610d82f8d 221
Lucyjungz 29:964610d82f8d 222 }
Lucyjungz 29:964610d82f8d 223 else if (strstr (buf,RMS_KEY_TAG))
Lucyjungz 29:964610d82f8d 224 {
Lucyjungz 29:964610d82f8d 225 /* Get XML text for RMS Key TAG */
Lucyjungz 29:964610d82f8d 226 char bufKey[XMLTEXT_SIZE];
Lucyjungz 29:964610d82f8d 227 FILEMANAGER_GetXmlText(buf, bufKey);
Lucyjungz 29:964610d82f8d 228 m_RxID = atoi(bufKey);
Lucyjungz 29:964610d82f8d 229
Lucyjungz 29:964610d82f8d 230 }
nsrwsurasak 0:a27e0d3581d1 231 }
Lucyjungz 3:6e08d0bba1bb 232 /* Ensure file is closed */
Lucyjungz 3:6e08d0bba1bb 233 fclose(fp);
nsrwsurasak 26:2f10fcabed0e 234
nsrwsurasak 26:2f10fcabed0e 235 #if defined(__MICROLIB) && defined(__ARMCC_VERSION) // with microlib and ARM compiler
nsrwsurasak 26:2f10fcabed0e 236 free(fp);
nsrwsurasak 26:2f10fcabed0e 237 #endif
nsrwsurasak 0:a27e0d3581d1 238 }
nsrwsurasak 0:a27e0d3581d1 239 }
Lucyjungz 3:6e08d0bba1bb 240 /**
Lucyjungz 3:6e08d0bba1bb 241 * @brief Function to log GPS Data
Lucyjungz 3:6e08d0bba1bb 242 * @note
Lucyjungz 3:6e08d0bba1bb 243 * @param timestamp - time structure to get Date
Lucyjungz 3:6e08d0bba1bb 244 * @param lat - char array for lattitude
Lucyjungz 3:6e08d0bba1bb 245 * @param longti - char array for longtitude
Lucyjungz 3:6e08d0bba1bb 246 * @retval None
Lucyjungz 3:6e08d0bba1bb 247 */
Lucyjungz 14:4ba6147f067b 248 void FILEMANAGER_LogGPSData(time_t timestamp ,char lat[], char longti[])
nsrwsurasak 0:a27e0d3581d1 249 {
nsrwsurasak 23:7ffd9724e105 250 if (!m_sdCardIsPresent)
nsrwsurasak 23:7ffd9724e105 251 {
nsrwsurasak 23:7ffd9724e105 252 /** Turn off LED */
nsrwsurasak 23:7ffd9724e105 253 FILEMANAGER_SetLedStatus(false);
nsrwsurasak 23:7ffd9724e105 254
nsrwsurasak 23:7ffd9724e105 255 printf("Error! SD Card is not connected \n");
nsrwsurasak 23:7ffd9724e105 256
nsrwsurasak 23:7ffd9724e105 257 /** Return as nothing to be done */
nsrwsurasak 23:7ffd9724e105 258 return;
nsrwsurasak 23:7ffd9724e105 259 }
Lucyjungz 3:6e08d0bba1bb 260 /* Get File name */
Lucyjungz 1:1f1f2b99756b 261 char file_name[] = GPS_LOG_FILE_NAME;
Lucyjungz 1:1f1f2b99756b 262
Lucyjungz 3:6e08d0bba1bb 263 /* Generate file name with time stamp */
Lucyjungz 14:4ba6147f067b 264 FILEMANAGER_GenerateFileNameWithTime(timestamp,file_name);
Lucyjungz 3:6e08d0bba1bb 265
Lucyjungz 3:6e08d0bba1bb 266 /* Open file with "APPEND" mode */
Lucyjungz 1:1f1f2b99756b 267 FILE *fp = fopen(file_name, "a");
Lucyjungz 1:1f1f2b99756b 268
Lucyjungz 3:6e08d0bba1bb 269 if (fp == NULL)
Lucyjungz 3:6e08d0bba1bb 270 {
Lucyjungz 3:6e08d0bba1bb 271 /* if it can't open the file then print error message */
Lucyjungz 1:1f1f2b99756b 272 printf("Error! Unable to open file %s!\n",file_name);
Lucyjungz 11:e21d4c5bfd1b 273
Lucyjungz 11:e21d4c5bfd1b 274 /* Indicate LED Status (OFF)*/
Lucyjungz 14:4ba6147f067b 275 FILEMANAGER_SetLedStatus(false);
Lucyjungz 3:6e08d0bba1bb 276 }
Lucyjungz 11:e21d4c5bfd1b 277 else
Lucyjungz 11:e21d4c5bfd1b 278 {
nsrwsurasak 28:12c88b5e46e5 279 #if CAPTURE_TIME
nsrwsurasak 28:12c88b5e46e5 280 t.reset();
nsrwsurasak 28:12c88b5e46e5 281 t.start();
nsrwsurasak 28:12c88b5e46e5 282 #endif
Lucyjungz 11:e21d4c5bfd1b 283 /* Indicate LED Status (ON)*/
Lucyjungz 14:4ba6147f067b 284 FILEMANAGER_SetLedStatus(true);
Lucyjungz 11:e21d4c5bfd1b 285
Lucyjungz 3:6e08d0bba1bb 286 /* Print some message for information */
Lucyjungz 1:1f1f2b99756b 287 printf("\r\n Writing to Gps Log File (%s)....",file_name);
Lucyjungz 1:1f1f2b99756b 288
Lucyjungz 3:6e08d0bba1bb 289 /* Write the line with lattitude and longtitude */
Lucyjungz 3:6e08d0bba1bb 290 fprintf(fp, "%d,%s,%s\n",timestamp,lat,longti);
Lucyjungz 1:1f1f2b99756b 291
Lucyjungz 1:1f1f2b99756b 292 printf("Done");
Lucyjungz 3:6e08d0bba1bb 293 /* Close file once it done */
Lucyjungz 3:6e08d0bba1bb 294 fclose(fp);
nsrwsurasak 26:2f10fcabed0e 295
nsrwsurasak 26:2f10fcabed0e 296 #if defined(__MICROLIB) && defined(__ARMCC_VERSION) // with microlib and ARM compiler
nsrwsurasak 26:2f10fcabed0e 297 free(fp);
nsrwsurasak 26:2f10fcabed0e 298 #endif
nsrwsurasak 28:12c88b5e46e5 299
nsrwsurasak 28:12c88b5e46e5 300 #if CAPTURE_TIME
nsrwsurasak 28:12c88b5e46e5 301 t.stop();
nsrwsurasak 28:12c88b5e46e5 302 printf("\r\nThe GPS Write Log time taken was %d millsecond\n", t.read_ms());
nsrwsurasak 28:12c88b5e46e5 303 #endif
Lucyjungz 1:1f1f2b99756b 304 }
Lucyjungz 1:1f1f2b99756b 305 }
Lucyjungz 3:6e08d0bba1bb 306 /**
Lucyjungz 3:6e08d0bba1bb 307 * @brief Function to log RMS Data
Lucyjungz 3:6e08d0bba1bb 308 * @note sequence must be in the same order with variableList
Lucyjungz 3:6e08d0bba1bb 309 * @param timestamp - time structure to get Date
Lucyjungz 3:6e08d0bba1bb 310 * @param var - float array to be log in the file
Lucyjungz 3:6e08d0bba1bb 311 * @param size - size of the float array
nsrwsurasak 25:7f3420c04178 312 * @param p_headerRequired - pointer to flag for log header
nsrwsurasak 25:7f3420c04178 313 * @param msec - time in millisecond
Lucyjungz 3:6e08d0bba1bb 314 * @retval None
Lucyjungz 3:6e08d0bba1bb 315 */
nsrwsurasak 25:7f3420c04178 316 void FILEMANAGER_LogRMSData(time_t timestamp ,float * var, int size, bool * p_headerRequired, uint32_t msec)
Lucyjungz 1:1f1f2b99756b 317 {
nsrwsurasak 23:7ffd9724e105 318 if (!m_sdCardIsPresent)
nsrwsurasak 23:7ffd9724e105 319 {
nsrwsurasak 23:7ffd9724e105 320 /** Turn off LED */
nsrwsurasak 23:7ffd9724e105 321 FILEMANAGER_SetLedStatus(false);
nsrwsurasak 23:7ffd9724e105 322
nsrwsurasak 23:7ffd9724e105 323 printf("Error! SD Card is not connected \n");
nsrwsurasak 23:7ffd9724e105 324
nsrwsurasak 23:7ffd9724e105 325 /** Return as nothing to be done */
nsrwsurasak 23:7ffd9724e105 326 return;
nsrwsurasak 23:7ffd9724e105 327 }
nsrwsurasak 31:983c30829d24 328
Lucyjungz 3:6e08d0bba1bb 329 /* Get File name */
Lucyjungz 1:1f1f2b99756b 330 char file_name[] = RTL_LOG_FILE_NAME;
Lucyjungz 1:1f1f2b99756b 331
Lucyjungz 3:6e08d0bba1bb 332 /* Generate File name with timestamp */
nsrwsurasak 25:7f3420c04178 333
Lucyjungz 14:4ba6147f067b 334 FILEMANAGER_GenerateFileNameWithTime(timestamp,file_name);
nsrwsurasak 25:7f3420c04178 335 #if DEBUG
nsrwsurasak 25:7f3420c04178 336 printf("\r\n File name is generated \n");
nsrwsurasak 25:7f3420c04178 337 #endif
Lucyjungz 21:0c52dbc23f52 338 if (*p_headerRequired || !FILEMANAGER_IsFileExist(file_name))
Lucyjungz 1:1f1f2b99756b 339 {
Lucyjungz 3:6e08d0bba1bb 340 /* If file is not exist, log the header */
Lucyjungz 14:4ba6147f067b 341 FILEMANAGER_LogRMSHeader(timestamp);
Lucyjungz 21:0c52dbc23f52 342
Lucyjungz 21:0c52dbc23f52 343 /* Clear flag once header has been written */
Lucyjungz 21:0c52dbc23f52 344 *p_headerRequired = false;
Lucyjungz 1:1f1f2b99756b 345 }
nsrwsurasak 25:7f3420c04178 346 #if DEBUG
nsrwsurasak 25:7f3420c04178 347 printf("\r\n Going to Open RMS File For write \n");
nsrwsurasak 25:7f3420c04178 348 #endif
Lucyjungz 3:6e08d0bba1bb 349 /* Open file with "APPEND" mode */
Lucyjungz 1:1f1f2b99756b 350 FILE *fp = fopen(file_name, "a");
nsrwsurasak 0:a27e0d3581d1 351
Lucyjungz 3:6e08d0bba1bb 352 if (fp == NULL)
Lucyjungz 3:6e08d0bba1bb 353 {
Lucyjungz 3:6e08d0bba1bb 354 /* In case of error, print the error message */
Lucyjungz 1:1f1f2b99756b 355 printf("Error! Unable to open file %s!\n",file_name);
Lucyjungz 11:e21d4c5bfd1b 356
Lucyjungz 11:e21d4c5bfd1b 357 /* Indicate LED Status (OFF)*/
Lucyjungz 14:4ba6147f067b 358 FILEMANAGER_SetLedStatus(false);
Lucyjungz 3:6e08d0bba1bb 359 }
Lucyjungz 3:6e08d0bba1bb 360 else
Lucyjungz 3:6e08d0bba1bb 361 {
Lucyjungz 11:e21d4c5bfd1b 362 /* Indicate LED Status (ON)*/
Lucyjungz 14:4ba6147f067b 363 FILEMANAGER_SetLedStatus(true);
Lucyjungz 11:e21d4c5bfd1b 364
nsrwsurasak 28:12c88b5e46e5 365 #if CAPTURE_TIME
nsrwsurasak 28:12c88b5e46e5 366 t.reset();
nsrwsurasak 28:12c88b5e46e5 367 t.start();
nsrwsurasak 28:12c88b5e46e5 368 #endif
Lucyjungz 3:6e08d0bba1bb 369 /* Print some message for information */
Lucyjungz 1:1f1f2b99756b 370 printf("\r\n Writing to Log File (%s)....",file_name);
Lucyjungz 1:1f1f2b99756b 371
Lucyjungz 3:6e08d0bba1bb 372 /* Write timestamp */
nsrwsurasak 25:7f3420c04178 373 fprintf(fp, "%d,%d",timestamp,msec);
Lucyjungz 1:1f1f2b99756b 374
Lucyjungz 3:6e08d0bba1bb 375 /* Write variable data */
Lucyjungz 1:1f1f2b99756b 376 for(int i = 0; i < size; i++)
Lucyjungz 1:1f1f2b99756b 377 {
nsrwsurasak 19:5af5c60e7a9f 378 fprintf(fp, ",%g",var[i]);
Lucyjungz 1:1f1f2b99756b 379 }
Lucyjungz 3:6e08d0bba1bb 380 /* Write new line as we done */
Lucyjungz 3:6e08d0bba1bb 381 fprintf(fp, "\n");
nsrwsurasak 0:a27e0d3581d1 382
Lucyjungz 1:1f1f2b99756b 383 printf("Done");
Lucyjungz 3:6e08d0bba1bb 384 /* Close the file */
Lucyjungz 3:6e08d0bba1bb 385 fclose(fp);
nsrwsurasak 26:2f10fcabed0e 386 #if defined(__MICROLIB) && defined(__ARMCC_VERSION) // with microlib and ARM compiler
nsrwsurasak 26:2f10fcabed0e 387 free(fp);
nsrwsurasak 26:2f10fcabed0e 388 #endif
nsrwsurasak 28:12c88b5e46e5 389 #if CAPTURE_TIME
nsrwsurasak 28:12c88b5e46e5 390 t.stop();
nsrwsurasak 28:12c88b5e46e5 391 printf("\r\n The RMS Write log time taken was %d milliseconds\n", t.read_ms());
nsrwsurasak 28:12c88b5e46e5 392 #endif
Lucyjungz 3:6e08d0bba1bb 393 }
Lucyjungz 3:6e08d0bba1bb 394 }
Lucyjungz 3:6e08d0bba1bb 395 /**
Lucyjungz 3:6e08d0bba1bb 396 * @brief Function to log RMS Header
Lucyjungz 3:6e08d0bba1bb 397 * @note sequence must be in the same order with variableList
Lucyjungz 3:6e08d0bba1bb 398 * @param timestamp - time structure to get Date
Lucyjungz 3:6e08d0bba1bb 399 * @retval None
Lucyjungz 3:6e08d0bba1bb 400 */
Lucyjungz 14:4ba6147f067b 401 void FILEMANAGER_LogRMSHeader(time_t timestamp)
nsrwsurasak 23:7ffd9724e105 402 {
nsrwsurasak 23:7ffd9724e105 403
Lucyjungz 3:6e08d0bba1bb 404 /* Get File name */
Lucyjungz 3:6e08d0bba1bb 405 char file_name[] = RTL_LOG_FILE_NAME;
Lucyjungz 3:6e08d0bba1bb 406
Lucyjungz 3:6e08d0bba1bb 407 /* Generate file name with time */
Lucyjungz 14:4ba6147f067b 408 FILEMANAGER_GenerateFileNameWithTime(timestamp,file_name);
Lucyjungz 3:6e08d0bba1bb 409
Lucyjungz 3:6e08d0bba1bb 410 /* Open file in append mode */
Lucyjungz 3:6e08d0bba1bb 411 FILE *fp = fopen(file_name, "a");
Lucyjungz 3:6e08d0bba1bb 412
Lucyjungz 3:6e08d0bba1bb 413 if (fp == NULL)
Lucyjungz 3:6e08d0bba1bb 414 {
Lucyjungz 3:6e08d0bba1bb 415 /* In case of error, print the error message */
Lucyjungz 3:6e08d0bba1bb 416 printf("Error! Unable to open file %s!\n",file_name);
Lucyjungz 11:e21d4c5bfd1b 417
Lucyjungz 11:e21d4c5bfd1b 418 /* Indicate LED Status (OFF)*/
Lucyjungz 14:4ba6147f067b 419 FILEMANAGER_SetLedStatus(false);
Lucyjungz 3:6e08d0bba1bb 420 }
Lucyjungz 3:6e08d0bba1bb 421 else
Lucyjungz 3:6e08d0bba1bb 422 {
Lucyjungz 11:e21d4c5bfd1b 423 /* Indicate LED Status (ON)*/
Lucyjungz 14:4ba6147f067b 424 FILEMANAGER_SetLedStatus(true);
Lucyjungz 11:e21d4c5bfd1b 425
Lucyjungz 3:6e08d0bba1bb 426 /* opened file so can write */
Lucyjungz 3:6e08d0bba1bb 427 printf("\r\n Writing to Log File (%s)....",file_name);
Lucyjungz 3:6e08d0bba1bb 428
Lucyjungz 3:6e08d0bba1bb 429 /* Write the header to the file */
Lucyjungz 14:4ba6147f067b 430 fprintf(fp, "%s,%s",RMS_HEADER_TIME,RMS_HEADER_MSECOND);
Lucyjungz 7:ab015947e368 431
Lucyjungz 14:4ba6147f067b 432 for(int i = 0; i < m_amountVarList; i++)
Lucyjungz 14:4ba6147f067b 433 {
Lucyjungz 14:4ba6147f067b 434 fprintf(fp, ",%s",m_varList[i].varName);
Lucyjungz 14:4ba6147f067b 435 }
Lucyjungz 14:4ba6147f067b 436 /* Write new line as done */
Lucyjungz 14:4ba6147f067b 437 fprintf(fp, "\n");
Lucyjungz 14:4ba6147f067b 438
Lucyjungz 14:4ba6147f067b 439 /* Write the timestamp unit to the file */
Lucyjungz 14:4ba6147f067b 440 fprintf(fp, "-,-");
Lucyjungz 14:4ba6147f067b 441
Lucyjungz 15:b63a539c3754 442
Lucyjungz 15:b63a539c3754 443 /* Write the unit of variables to the file */
Lucyjungz 3:6e08d0bba1bb 444 for(int i = 0; i < m_amountVarList; i++)
Lucyjungz 3:6e08d0bba1bb 445 {
Lucyjungz 15:b63a539c3754 446 fprintf(fp, ",%s",m_varList[i].varUnit);
Lucyjungz 3:6e08d0bba1bb 447 }
Lucyjungz 15:b63a539c3754 448
Lucyjungz 3:6e08d0bba1bb 449 /* Write new line as done */
Lucyjungz 3:6e08d0bba1bb 450 fprintf(fp, "\n");
Lucyjungz 3:6e08d0bba1bb 451
Lucyjungz 3:6e08d0bba1bb 452 printf("Done");
Lucyjungz 3:6e08d0bba1bb 453
Lucyjungz 3:6e08d0bba1bb 454 /* Close the file */
Lucyjungz 3:6e08d0bba1bb 455 fclose(fp);
nsrwsurasak 26:2f10fcabed0e 456
nsrwsurasak 26:2f10fcabed0e 457 #if defined(__MICROLIB) && defined(__ARMCC_VERSION) // with microlib and ARM compiler
nsrwsurasak 26:2f10fcabed0e 458 free(fp);
nsrwsurasak 26:2f10fcabed0e 459 #endif
Lucyjungz 3:6e08d0bba1bb 460 }
Lucyjungz 3:6e08d0bba1bb 461 }
Lucyjungz 3:6e08d0bba1bb 462 /**
Lucyjungz 3:6e08d0bba1bb 463 * @brief Function to log Mini RMS System Data
Lucyjungz 3:6e08d0bba1bb 464 * @note this file is only for debug
nsrwsurasak 27:b0eb0f36110e 465 * @param gps_interval - gps interval
nsrwsurasak 27:b0eb0f36110e 466 * @param rms_interval - rms interval
nsrwsurasak 27:b0eb0f36110e 467 * @param isResetCausedByWdg - flag to indicate system restart by watchdog
Lucyjungz 3:6e08d0bba1bb 468 * @retval None
Lucyjungz 3:6e08d0bba1bb 469 */
nsrwsurasak 27:b0eb0f36110e 470 void FILEMANAGER_LogSystemData(float gps_interval,float rms_interval, bool isResetCausedByWdg)
Lucyjungz 3:6e08d0bba1bb 471 {
Lucyjungz 3:6e08d0bba1bb 472 /* Open the file in append mode */
Lucyjungz 3:6e08d0bba1bb 473 FILE *fp = fopen(MINIRMS_LOG_FILE_NAME, "a");
Lucyjungz 3:6e08d0bba1bb 474
Lucyjungz 3:6e08d0bba1bb 475 if (fp == NULL) {
Lucyjungz 3:6e08d0bba1bb 476 /* In case of error, print the msg */
Lucyjungz 3:6e08d0bba1bb 477 printf("Error! Unable to open file!\n");
Lucyjungz 11:e21d4c5bfd1b 478
Lucyjungz 11:e21d4c5bfd1b 479 /* Indicate LED Status (OFF)*/
Lucyjungz 14:4ba6147f067b 480 FILEMANAGER_SetLedStatus(false);
Lucyjungz 3:6e08d0bba1bb 481 }
Lucyjungz 3:6e08d0bba1bb 482 else
Lucyjungz 3:6e08d0bba1bb 483 {
Lucyjungz 11:e21d4c5bfd1b 484 /* Indicate LED Status (ON)*/
Lucyjungz 14:4ba6147f067b 485 FILEMANAGER_SetLedStatus(true);
nsrwsurasak 25:7f3420c04178 486 /* get Timestamp */
nsrwsurasak 25:7f3420c04178 487 time_t seconds = time(NULL);
nsrwsurasak 25:7f3420c04178 488
Lucyjungz 3:6e08d0bba1bb 489 /* Write some message, which is TBD */
nsrwsurasak 25:7f3420c04178 490 fprintf(fp, "\nStart Mini-RMS System with At UTC Time %s , Gps Interval = %f second, RMS Interval = %f second, Variable = %d variable(s) ",ctime(&seconds),gps_interval,rms_interval,m_amountVarList);
nsrwsurasak 27:b0eb0f36110e 491 if (isResetCausedByWdg)
nsrwsurasak 27:b0eb0f36110e 492 {
nsrwsurasak 27:b0eb0f36110e 493 fprintf(fp, " **** Restart by Watchdog");
nsrwsurasak 27:b0eb0f36110e 494 }
nsrwsurasak 27:b0eb0f36110e 495
Lucyjungz 1:1f1f2b99756b 496 fclose(fp); // ensure you close the file after writing
nsrwsurasak 26:2f10fcabed0e 497
nsrwsurasak 26:2f10fcabed0e 498 #if defined(__MICROLIB) && defined(__ARMCC_VERSION) // with microlib and ARM compiler
nsrwsurasak 26:2f10fcabed0e 499 free(fp);
nsrwsurasak 26:2f10fcabed0e 500 #endif
Lucyjungz 1:1f1f2b99756b 501 }
Lucyjungz 1:1f1f2b99756b 502 }
Lucyjungz 3:6e08d0bba1bb 503 /**
Lucyjungz 3:6e08d0bba1bb 504 * @brief Utility to delete file
Lucyjungz 3:6e08d0bba1bb 505 * @note
Lucyjungz 3:6e08d0bba1bb 506 * @param filename - file name to be deleted
Lucyjungz 3:6e08d0bba1bb 507 * @retval None
Lucyjungz 3:6e08d0bba1bb 508 */
Lucyjungz 14:4ba6147f067b 509 void FILEMANAGER_Deletefile(char filename[])
nsrwsurasak 0:a27e0d3581d1 510 {
Lucyjungz 32:69b7c870d69c 511 FILEMANAGER_AddPrefixFilename(filename, m_bufFileName);
nsrwsurasak 0:a27e0d3581d1 512 printf("Deleting file '%s'...",filename);
Lucyjungz 32:69b7c870d69c 513 FILE *fp = fopen(m_bufFileName, "r"); // try and open file
nsrwsurasak 0:a27e0d3581d1 514 if (fp != NULL) { // if it does open...
nsrwsurasak 0:a27e0d3581d1 515 fclose(fp); // close it
nsrwsurasak 26:2f10fcabed0e 516
nsrwsurasak 26:2f10fcabed0e 517 #if defined(__MICROLIB) && defined(__ARMCC_VERSION) // with microlib and ARM compiler
nsrwsurasak 26:2f10fcabed0e 518 free(fp);
nsrwsurasak 26:2f10fcabed0e 519 #endif
nsrwsurasak 26:2f10fcabed0e 520
Lucyjungz 32:69b7c870d69c 521 remove(m_bufFileName); // and then delete
nsrwsurasak 0:a27e0d3581d1 522 printf("Done!\n");
nsrwsurasak 0:a27e0d3581d1 523 }
nsrwsurasak 0:a27e0d3581d1 524 // if we can't open it, it doesn't exist and so we can't delete it
nsrwsurasak 0:a27e0d3581d1 525 }
Lucyjungz 3:6e08d0bba1bb 526 /**
Lucyjungz 3:6e08d0bba1bb 527 * @brief Utility to check file available
Lucyjungz 3:6e08d0bba1bb 528 * @note
Lucyjungz 3:6e08d0bba1bb 529 * @param filename - file name to be checked
Lucyjungz 3:6e08d0bba1bb 530 * @retval true - file is exist , false - file is not exist
Lucyjungz 3:6e08d0bba1bb 531 */
Lucyjungz 14:4ba6147f067b 532 bool FILEMANAGER_IsFileExist(char filename[])
Lucyjungz 1:1f1f2b99756b 533 {
Lucyjungz 1:1f1f2b99756b 534 bool exist = false;
Lucyjungz 1:1f1f2b99756b 535 FILE *fp = fopen(filename, "r"); // try and open file
Lucyjungz 1:1f1f2b99756b 536 if (fp != NULL) { // if it does open...
Lucyjungz 1:1f1f2b99756b 537 fclose(fp); // close it
nsrwsurasak 26:2f10fcabed0e 538
nsrwsurasak 26:2f10fcabed0e 539 #if defined(__MICROLIB) && defined(__ARMCC_VERSION) // with microlib and ARM compiler
nsrwsurasak 26:2f10fcabed0e 540 free(fp);
nsrwsurasak 26:2f10fcabed0e 541 #endif
nsrwsurasak 26:2f10fcabed0e 542
Lucyjungz 1:1f1f2b99756b 543 exist = true;
Lucyjungz 1:1f1f2b99756b 544 }
Lucyjungz 1:1f1f2b99756b 545
Lucyjungz 1:1f1f2b99756b 546 return exist;
Lucyjungz 1:1f1f2b99756b 547 }
Lucyjungz 3:6e08d0bba1bb 548 /**
Lucyjungz 3:6e08d0bba1bb 549 * @brief Utility to get GPS Interval
Lucyjungz 14:4ba6147f067b 550 * @note must be called after read the setup file
Lucyjungz 3:6e08d0bba1bb 551 * @param None
Lucyjungz 3:6e08d0bba1bb 552 * @retval GPS interval
Lucyjungz 3:6e08d0bba1bb 553 */
Lucyjungz 14:4ba6147f067b 554 int FILEMANAGER_GPSInterval()
nsrwsurasak 0:a27e0d3581d1 555 {
Lucyjungz 3:6e08d0bba1bb 556 /* Return interval in int */
Lucyjungz 14:4ba6147f067b 557 return ( m_GpsInterval );
nsrwsurasak 0:a27e0d3581d1 558 }
Lucyjungz 3:6e08d0bba1bb 559 /**
Lucyjungz 3:6e08d0bba1bb 560 * @brief Utility to get Data Interval
Lucyjungz 14:4ba6147f067b 561 * @note must be called after read the setup file
Lucyjungz 3:6e08d0bba1bb 562 * @param None
Lucyjungz 3:6e08d0bba1bb 563 * @retval Data interval
Lucyjungz 3:6e08d0bba1bb 564 */
Lucyjungz 14:4ba6147f067b 565 int FILEMANAGER_DataInterval()
nsrwsurasak 0:a27e0d3581d1 566 {
Lucyjungz 3:6e08d0bba1bb 567 /* Return interval in int */
Lucyjungz 14:4ba6147f067b 568 return ( m_DataInterval );
nsrwsurasak 0:a27e0d3581d1 569 }
Lucyjungz 3:6e08d0bba1bb 570 /**
Lucyjungz 3:6e08d0bba1bb 571 * @brief Function to read the variable list file
Lucyjungz 3:6e08d0bba1bb 572 * @note Recommended to call this function at initilization phase
Lucyjungz 3:6e08d0bba1bb 573 * @param None
Lucyjungz 3:6e08d0bba1bb 574 * @retval pointer to variable list
Lucyjungz 3:6e08d0bba1bb 575 */
Lucyjungz 14:4ba6147f067b 576 Variable_Data_TypeDef * FILEMANAGER_ReadVarFile()
nsrwsurasak 0:a27e0d3581d1 577 {
Lucyjungz 3:6e08d0bba1bb 578 /* Open the file with reading mode */
Lucyjungz 21:0c52dbc23f52 579 FILE *fp = fopen(m_varListFileName, "r");
nsrwsurasak 0:a27e0d3581d1 580
Lucyjungz 3:6e08d0bba1bb 581 if (fp == NULL)
Lucyjungz 3:6e08d0bba1bb 582 {
Lucyjungz 3:6e08d0bba1bb 583 /* if it can't open the file then print error message */
Lucyjungz 21:0c52dbc23f52 584 printf("\nError! Unable to open file! %s \n", m_varListFileName);
Lucyjungz 11:e21d4c5bfd1b 585 /* Indicate LED Status (OFF)*/
Lucyjungz 14:4ba6147f067b 586 FILEMANAGER_SetLedStatus(false);
Lucyjungz 11:e21d4c5bfd1b 587
nsrwsurasak 0:a27e0d3581d1 588 return NULL;
Lucyjungz 3:6e08d0bba1bb 589 }
Lucyjungz 3:6e08d0bba1bb 590 else
Lucyjungz 3:6e08d0bba1bb 591 {
Lucyjungz 11:e21d4c5bfd1b 592 /* Indicate LED Status (ON)*/
Lucyjungz 14:4ba6147f067b 593 FILEMANAGER_SetLedStatus(true);
nsrwsurasak 0:a27e0d3581d1 594
Lucyjungz 3:6e08d0bba1bb 595 /* Allocate buffer for reading */
Lucyjungz 14:4ba6147f067b 596 char buf[READ_FILE_BUFFER_SIZE];
nsrwsurasak 0:a27e0d3581d1 597 int index = 0;
Lucyjungz 3:6e08d0bba1bb 598
Lucyjungz 3:6e08d0bba1bb 599 /* Initialize return value */
nsrwsurasak 0:a27e0d3581d1 600 memset(m_varList, ' ', sizeof(m_varList));
Lucyjungz 3:6e08d0bba1bb 601
Lucyjungz 3:6e08d0bba1bb 602 /* Read line from the file */
Lucyjungz 3:6e08d0bba1bb 603 while (fgets(buf, sizeof(buf), fp) != NULL)
Lucyjungz 3:6e08d0bba1bb 604 {
Lucyjungz 3:6e08d0bba1bb 605 /* Check the TAG */
Lucyjungz 3:6e08d0bba1bb 606 if (strstr (buf,VAR_NAME_TAG))
Lucyjungz 3:6e08d0bba1bb 607 {
Lucyjungz 3:6e08d0bba1bb 608 /* Found variable TAG, populate it*/
Lucyjungz 14:4ba6147f067b 609 FILEMANAGER_GetXmlText(buf , m_varList[index].varName);
Lucyjungz 3:6e08d0bba1bb 610 }
Lucyjungz 3:6e08d0bba1bb 611 else if (strstr (buf,VAR_ADDR_TAG))
Lucyjungz 3:6e08d0bba1bb 612 {
Lucyjungz 3:6e08d0bba1bb 613 /* Found variable address TAG, populate it*/
Lucyjungz 14:4ba6147f067b 614 FILEMANAGER_GetXmlText(buf , m_varList[index].varAddress);
Lucyjungz 5:7c513eee7b2b 615 }
Lucyjungz 10:a8003d357cf2 616 else if (strstr (buf,VAR_TYPE_TAG))
Lucyjungz 10:a8003d357cf2 617 {
Lucyjungz 10:a8003d357cf2 618 /* Found variable type TAG, populate it*/
Lucyjungz 14:4ba6147f067b 619 FILEMANAGER_GetXmlText(buf , m_varList[index].varType);
Lucyjungz 10:a8003d357cf2 620 }
Lucyjungz 6:5bd75c0f607c 621 else if (strstr (buf,VAR_LSB1_TAG))
Lucyjungz 6:5bd75c0f607c 622 {
Lucyjungz 6:5bd75c0f607c 623 /* Found variable LSB1 TAG, populate it*/
Lucyjungz 14:4ba6147f067b 624 FILEMANAGER_GetXmlText(buf , m_varList[index].varLSB1);
Lucyjungz 6:5bd75c0f607c 625 }
Lucyjungz 6:5bd75c0f607c 626 else if (strstr (buf,VAR_LSB2_TAG))
Lucyjungz 5:7c513eee7b2b 627 {
Lucyjungz 6:5bd75c0f607c 628 /* Found variable LSB2 TAG, populate it*/
Lucyjungz 14:4ba6147f067b 629 FILEMANAGER_GetXmlText(buf , m_varList[index].varLSB2);
Lucyjungz 6:5bd75c0f607c 630 }
Lucyjungz 6:5bd75c0f607c 631 else if (strstr (buf,VAR_BITMASK_TAG))
Lucyjungz 6:5bd75c0f607c 632 {
Lucyjungz 6:5bd75c0f607c 633 /* Found variable BitMask TAG, populate it*/
Lucyjungz 14:4ba6147f067b 634 FILEMANAGER_GetXmlText(buf , m_varList[index].varBitMask);
Lucyjungz 5:7c513eee7b2b 635 }
Lucyjungz 5:7c513eee7b2b 636 else if (strstr (buf,VAR_UNIT_TAG))
Lucyjungz 5:7c513eee7b2b 637 {
Lucyjungz 5:7c513eee7b2b 638 /* Found variable unit TAG, populate it*/
Lucyjungz 14:4ba6147f067b 639 FILEMANAGER_GetXmlText(buf , m_varList[index].varUnit);
nsrwsurasak 0:a27e0d3581d1 640 index++;
nsrwsurasak 0:a27e0d3581d1 641 }
nsrwsurasak 0:a27e0d3581d1 642
nsrwsurasak 0:a27e0d3581d1 643 }
Lucyjungz 3:6e08d0bba1bb 644 /* Close File */
Lucyjungz 3:6e08d0bba1bb 645 fclose(fp);
nsrwsurasak 26:2f10fcabed0e 646 #if defined(__MICROLIB) && defined(__ARMCC_VERSION) // with microlib and ARM compiler
nsrwsurasak 26:2f10fcabed0e 647 free(fp);
nsrwsurasak 26:2f10fcabed0e 648 #endif
Lucyjungz 3:6e08d0bba1bb 649
Lucyjungz 3:6e08d0bba1bb 650 /* Populate amount */
nsrwsurasak 0:a27e0d3581d1 651 m_amountVarList = index;
Lucyjungz 3:6e08d0bba1bb 652
Lucyjungz 3:6e08d0bba1bb 653 /* Return variable list */
nsrwsurasak 0:a27e0d3581d1 654 return m_varList;
nsrwsurasak 0:a27e0d3581d1 655 }
nsrwsurasak 0:a27e0d3581d1 656 }
Lucyjungz 3:6e08d0bba1bb 657 /**
Lucyjungz 3:6e08d0bba1bb 658 * @brief Function to amount of variable for data logging
Lucyjungz 3:6e08d0bba1bb 659 * @note Must be called after readVarFile
Lucyjungz 3:6e08d0bba1bb 660 * @param None
Lucyjungz 3:6e08d0bba1bb 661 * @retval amount of variable list
Lucyjungz 3:6e08d0bba1bb 662 */
Lucyjungz 14:4ba6147f067b 663 int FILEMANAGER_GetAmountVarList()
nsrwsurasak 0:a27e0d3581d1 664 {
nsrwsurasak 0:a27e0d3581d1 665 return m_amountVarList;
nsrwsurasak 0:a27e0d3581d1 666 }
Lucyjungz 3:6e08d0bba1bb 667 /**
Lucyjungz 3:6e08d0bba1bb 668 * @brief Function to get variable list
Lucyjungz 3:6e08d0bba1bb 669 * @note Must be called after readVarFile
Lucyjungz 3:6e08d0bba1bb 670 * @param None
Lucyjungz 3:6e08d0bba1bb 671 * @retval pointer to variable list
Lucyjungz 3:6e08d0bba1bb 672 */
Lucyjungz 14:4ba6147f067b 673 Variable_Data_TypeDef * FILEMANAGER_GetVarList()
nsrwsurasak 0:a27e0d3581d1 674 {
nsrwsurasak 0:a27e0d3581d1 675 return m_varList;
nsrwsurasak 0:a27e0d3581d1 676 }
Lucyjungz 29:964610d82f8d 677 /**
Lucyjungz 29:964610d82f8d 678 * @brief Function to get Receieve ID
Lucyjungz 29:964610d82f8d 679 * @note Must be called after readSetupFile
Lucyjungz 29:964610d82f8d 680 * @param None
Lucyjungz 29:964610d82f8d 681 * @retval Receieve ID
Lucyjungz 29:964610d82f8d 682 */
Lucyjungz 29:964610d82f8d 683 uint32_t FILEMANAGER_GetRxID()
Lucyjungz 29:964610d82f8d 684 {
Lucyjungz 29:964610d82f8d 685 return m_RxID;
Lucyjungz 29:964610d82f8d 686 }
Lucyjungz 29:964610d82f8d 687 /**
Lucyjungz 29:964610d82f8d 688 * @brief Function to get Transmit ID
Lucyjungz 29:964610d82f8d 689 * @note Must be called after readSetupFile
Lucyjungz 29:964610d82f8d 690 * @param None
Lucyjungz 29:964610d82f8d 691 * @retval Transmit ID
Lucyjungz 29:964610d82f8d 692 */
Lucyjungz 29:964610d82f8d 693 uint32_t FILEMANAGER_GetTxID()
Lucyjungz 29:964610d82f8d 694 {
Lucyjungz 29:964610d82f8d 695 return m_TxID;
Lucyjungz 29:964610d82f8d 696 }
Lucyjungz 11:e21d4c5bfd1b 697 /**
Lucyjungz 11:e21d4c5bfd1b 698 * @brief Utility to play with LED status
Lucyjungz 11:e21d4c5bfd1b 699 * @note Libary user need to assign proper PinName to LED_SDCARD
Lucyjungz 11:e21d4c5bfd1b 700 * @param on - True for turning LED ON, otherwise LED off
Lucyjungz 11:e21d4c5bfd1b 701 * @retval None
Lucyjungz 11:e21d4c5bfd1b 702 */
Lucyjungz 14:4ba6147f067b 703 static void FILEMANAGER_SetLedStatus(bool on)
Lucyjungz 11:e21d4c5bfd1b 704 {
Lucyjungz 12:0045fca3c160 705 /* Check LED Connection */
nsrwsurasak 22:2fc64ad66e35 706 if (ledStatus.is_connected())
Lucyjungz 11:e21d4c5bfd1b 707 {
Lucyjungz 21:0c52dbc23f52 708 /* Set LED regarding to given argment */
nsrwsurasak 22:2fc64ad66e35 709 if (m_sdCardIsPresent)
nsrwsurasak 22:2fc64ad66e35 710 {
nsrwsurasak 22:2fc64ad66e35 711 ledStatus = on;
nsrwsurasak 22:2fc64ad66e35 712 }
nsrwsurasak 22:2fc64ad66e35 713 else
nsrwsurasak 22:2fc64ad66e35 714 {
nsrwsurasak 22:2fc64ad66e35 715 ledStatus = false;
nsrwsurasak 22:2fc64ad66e35 716 }
nsrwsurasak 22:2fc64ad66e35 717
Lucyjungz 12:0045fca3c160 718 }
Lucyjungz 12:0045fca3c160 719 #if DEBUG
Lucyjungz 12:0045fca3c160 720 else
Lucyjungz 12:0045fca3c160 721 {
Lucyjungz 14:4ba6147f067b 722 printf("\r\nSDCard LED is not connected !!");
Lucyjungz 12:0045fca3c160 723 }
Lucyjungz 12:0045fca3c160 724 #endif
Lucyjungz 11:e21d4c5bfd1b 725 }
Lucyjungz 32:69b7c870d69c 726
Lucyjungz 32:69b7c870d69c 727 #if UART_FILE_ACCESS
Lucyjungz 32:69b7c870d69c 728 /**
Lucyjungz 32:69b7c870d69c 729 * @brief Utility to show list of file in SD Card
Lucyjungz 32:69b7c870d69c 730 * @param serial - pointer to serial to be display as output
Lucyjungz 32:69b7c870d69c 731 * @retval None
Lucyjungz 32:69b7c870d69c 732 */
Lucyjungz 32:69b7c870d69c 733 void FILEMANAGER_listDir(Serial* serial)
Lucyjungz 30:184d2ef7dff7 734 {
Lucyjungz 15:b63a539c3754 735
Lucyjungz 30:184d2ef7dff7 736 DIR *d;
Lucyjungz 30:184d2ef7dff7 737 struct dirent *p;
Lucyjungz 30:184d2ef7dff7 738
Lucyjungz 30:184d2ef7dff7 739 d = opendir("/sd");
Lucyjungz 30:184d2ef7dff7 740 if (d != NULL) {
Lucyjungz 32:69b7c870d69c 741 while ((p = readdir(d)) != NULL)
Lucyjungz 32:69b7c870d69c 742 {
Lucyjungz 32:69b7c870d69c 743 if (serial != NULL)
Lucyjungz 32:69b7c870d69c 744 {
Lucyjungz 32:69b7c870d69c 745 serial->printf(" - %s\n\r", p->d_name);
Lucyjungz 32:69b7c870d69c 746 }
Lucyjungz 32:69b7c870d69c 747 else
Lucyjungz 32:69b7c870d69c 748 {
Lucyjungz 32:69b7c870d69c 749 printf(" - %s\n\r", p->d_name);
Lucyjungz 32:69b7c870d69c 750 }
Lucyjungz 32:69b7c870d69c 751
Lucyjungz 30:184d2ef7dff7 752 }
Lucyjungz 30:184d2ef7dff7 753 } else {
Lucyjungz 32:69b7c870d69c 754
Lucyjungz 30:184d2ef7dff7 755 printf("Could not open directory!\n");
Lucyjungz 30:184d2ef7dff7 756 }
Lucyjungz 30:184d2ef7dff7 757 closedir(d);
Lucyjungz 30:184d2ef7dff7 758 }
Lucyjungz 32:69b7c870d69c 759 /**
Lucyjungz 32:69b7c870d69c 760 * @brief Utility to display content of specific file
Lucyjungz 32:69b7c870d69c 761 * @param serial - pointer to serial to be display as output
Lucyjungz 32:69b7c870d69c 762 * @param filename - filename to be read
Lucyjungz 32:69b7c870d69c 763 * @retval None
Lucyjungz 32:69b7c870d69c 764 */
Lucyjungz 32:69b7c870d69c 765 void FILEMANAGER_DisplayFile(Serial* serial, char* filename)
Lucyjungz 32:69b7c870d69c 766 {
Lucyjungz 30:184d2ef7dff7 767
Lucyjungz 32:69b7c870d69c 768 FILEMANAGER_AddPrefixFilename(filename, m_bufFileName);
Lucyjungz 32:69b7c870d69c 769 /* Open the file with reading mode */
Lucyjungz 32:69b7c870d69c 770 FILE *fp = fopen(m_bufFileName, "r");
Lucyjungz 32:69b7c870d69c 771
Lucyjungz 32:69b7c870d69c 772 if (fp == NULL)
Lucyjungz 32:69b7c870d69c 773 {
Lucyjungz 32:69b7c870d69c 774 /* if it can't open the file then print error message */
Lucyjungz 32:69b7c870d69c 775 if (serial != NULL)
Lucyjungz 32:69b7c870d69c 776 {
Lucyjungz 32:69b7c870d69c 777 serial->printf("\nError! Unable to open file! %s \n", filename);
Lucyjungz 32:69b7c870d69c 778 }
Lucyjungz 32:69b7c870d69c 779 else
Lucyjungz 32:69b7c870d69c 780 {
Lucyjungz 32:69b7c870d69c 781 printf("\nError! Unable to open file! %s \n", filename);
Lucyjungz 32:69b7c870d69c 782 }
Lucyjungz 32:69b7c870d69c 783 }
Lucyjungz 32:69b7c870d69c 784 else
Lucyjungz 32:69b7c870d69c 785 {
Lucyjungz 32:69b7c870d69c 786 /* Allocate buffer for reading file */
Lucyjungz 32:69b7c870d69c 787 char buf[READ_FILE_BUFFER_SIZE];
Lucyjungz 32:69b7c870d69c 788 /* Read line from the file */
Lucyjungz 32:69b7c870d69c 789 while (fgets(buf, sizeof(buf), fp) != NULL)
Lucyjungz 32:69b7c870d69c 790 {
Lucyjungz 32:69b7c870d69c 791 if (serial != NULL)
Lucyjungz 32:69b7c870d69c 792 {
Lucyjungz 32:69b7c870d69c 793 serial->printf("\n%s", buf);
Lucyjungz 32:69b7c870d69c 794 }
Lucyjungz 32:69b7c870d69c 795 else
Lucyjungz 32:69b7c870d69c 796 {
Lucyjungz 32:69b7c870d69c 797 printf("\n%s", buf);
Lucyjungz 32:69b7c870d69c 798 }
Lucyjungz 32:69b7c870d69c 799 }
Lucyjungz 32:69b7c870d69c 800 /* Close File */
Lucyjungz 32:69b7c870d69c 801 fclose(fp);
Lucyjungz 32:69b7c870d69c 802 #if defined(__MICROLIB) && defined(__ARMCC_VERSION) // with microlib and ARM compiler
Lucyjungz 32:69b7c870d69c 803 free(fp);
Lucyjungz 32:69b7c870d69c 804 #endif
Lucyjungz 32:69b7c870d69c 805
Lucyjungz 32:69b7c870d69c 806 }
Lucyjungz 32:69b7c870d69c 807 }
Lucyjungz 32:69b7c870d69c 808 /**
Lucyjungz 32:69b7c870d69c 809 * @brief Utility to replace content of specific file
Lucyjungz 32:69b7c870d69c 810 * @param serial - pointer to serial to be display as output
Lucyjungz 32:69b7c870d69c 811 * @param filename - filename to be replace
Lucyjungz 32:69b7c870d69c 812 * @retval None
Lucyjungz 32:69b7c870d69c 813 */
Lucyjungz 32:69b7c870d69c 814 void FILEMANAGER_WriteFile(Serial* serial, char* filename, char* input)
Lucyjungz 32:69b7c870d69c 815 {
Lucyjungz 32:69b7c870d69c 816 FILEMANAGER_AddPrefixFilename(filename, m_bufFileName);
Lucyjungz 32:69b7c870d69c 817 FILE *fp = fopen(m_bufFileName, "a");
Lucyjungz 32:69b7c870d69c 818
Lucyjungz 32:69b7c870d69c 819 if (fp == NULL)
Lucyjungz 32:69b7c870d69c 820 {
Lucyjungz 32:69b7c870d69c 821 /* if it can't open the file then print error message */
Lucyjungz 32:69b7c870d69c 822 if (serial != NULL)
Lucyjungz 32:69b7c870d69c 823 {
Lucyjungz 32:69b7c870d69c 824 serial->printf("\nError! Unable to open file! %s \n", filename);
Lucyjungz 32:69b7c870d69c 825 }
Lucyjungz 32:69b7c870d69c 826 else
Lucyjungz 32:69b7c870d69c 827 {
Lucyjungz 32:69b7c870d69c 828 printf("\nError! Unable to open file! %s \n", filename);
Lucyjungz 32:69b7c870d69c 829 }
Lucyjungz 32:69b7c870d69c 830
Lucyjungz 32:69b7c870d69c 831 }
Lucyjungz 32:69b7c870d69c 832 else
Lucyjungz 32:69b7c870d69c 833 {
Lucyjungz 32:69b7c870d69c 834 fprintf(fp, "%s",input);
Lucyjungz 32:69b7c870d69c 835
Lucyjungz 32:69b7c870d69c 836 fclose(fp);
Lucyjungz 32:69b7c870d69c 837 #if defined(__MICROLIB) && defined(__ARMCC_VERSION) // with microlib and ARM compiler
Lucyjungz 32:69b7c870d69c 838 free(fp);
Lucyjungz 32:69b7c870d69c 839 #endif
Lucyjungz 32:69b7c870d69c 840 }
Lucyjungz 32:69b7c870d69c 841 }
Lucyjungz 32:69b7c870d69c 842 static void FILEMANAGER_AddPrefixFilename(char* in, char* out)
Lucyjungz 32:69b7c870d69c 843 {
Lucyjungz 32:69b7c870d69c 844 sprintf(out,"%s%s", PREFIX_FILE_NAME, in);
Lucyjungz 32:69b7c870d69c 845 }
Lucyjungz 32:69b7c870d69c 846 #endif