File manager

Dependencies:   SDFileSystem

Dependents:   RwSDCard_Xml_GPS

Committer:
nsrwsurasak
Date:
Tue Sep 20 07:39:16 2016 +0000
Revision:
38:e2799f75940f
Parent:
36:09e662a69c50
Fix bug and delete print message.

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