File manager

Dependencies:   SDFileSystem

Dependents:   RwSDCard_Xml_GPS

Committer:
Lucyjungz
Date:
Wed Sep 14 08:32:24 2016 +0000
Revision:
34:097b1f6828b7
Parent:
33:dd67a980e9d8
Child:
35:b91169442c4a
Get time from Internal RTC instead of System time

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 33:dd67a980e9d8 286 #if DEBUG
Lucyjungz 3:6e08d0bba1bb 287 /* Print some message for information */
Lucyjungz 1:1f1f2b99756b 288 printf("\r\n Writing to Gps Log File (%s)....",file_name);
Lucyjungz 33:dd67a980e9d8 289 #endif
Lucyjungz 33:dd67a980e9d8 290
Lucyjungz 3:6e08d0bba1bb 291 /* Write the line with lattitude and longtitude */
Lucyjungz 3:6e08d0bba1bb 292 fprintf(fp, "%d,%s,%s\n",timestamp,lat,longti);
Lucyjungz 1:1f1f2b99756b 293
Lucyjungz 33:dd67a980e9d8 294 #if DEBUG
Lucyjungz 1:1f1f2b99756b 295 printf("Done");
Lucyjungz 33:dd67a980e9d8 296 #endif
Lucyjungz 33:dd67a980e9d8 297
Lucyjungz 3:6e08d0bba1bb 298 /* Close file once it done */
Lucyjungz 3:6e08d0bba1bb 299 fclose(fp);
nsrwsurasak 26:2f10fcabed0e 300
nsrwsurasak 26:2f10fcabed0e 301 #if defined(__MICROLIB) && defined(__ARMCC_VERSION) // with microlib and ARM compiler
nsrwsurasak 26:2f10fcabed0e 302 free(fp);
nsrwsurasak 26:2f10fcabed0e 303 #endif
nsrwsurasak 28:12c88b5e46e5 304
nsrwsurasak 28:12c88b5e46e5 305 #if CAPTURE_TIME
nsrwsurasak 28:12c88b5e46e5 306 t.stop();
nsrwsurasak 28:12c88b5e46e5 307 printf("\r\nThe GPS Write Log time taken was %d millsecond\n", t.read_ms());
nsrwsurasak 28:12c88b5e46e5 308 #endif
Lucyjungz 1:1f1f2b99756b 309 }
Lucyjungz 1:1f1f2b99756b 310 }
Lucyjungz 3:6e08d0bba1bb 311 /**
Lucyjungz 3:6e08d0bba1bb 312 * @brief Function to log RMS Data
Lucyjungz 3:6e08d0bba1bb 313 * @note sequence must be in the same order with variableList
Lucyjungz 3:6e08d0bba1bb 314 * @param timestamp - time structure to get Date
Lucyjungz 3:6e08d0bba1bb 315 * @param var - float array to be log in the file
Lucyjungz 3:6e08d0bba1bb 316 * @param size - size of the float array
nsrwsurasak 25:7f3420c04178 317 * @param p_headerRequired - pointer to flag for log header
nsrwsurasak 25:7f3420c04178 318 * @param msec - time in millisecond
Lucyjungz 3:6e08d0bba1bb 319 * @retval None
Lucyjungz 3:6e08d0bba1bb 320 */
nsrwsurasak 25:7f3420c04178 321 void FILEMANAGER_LogRMSData(time_t timestamp ,float * var, int size, bool * p_headerRequired, uint32_t msec)
Lucyjungz 1:1f1f2b99756b 322 {
nsrwsurasak 23:7ffd9724e105 323 if (!m_sdCardIsPresent)
nsrwsurasak 23:7ffd9724e105 324 {
nsrwsurasak 23:7ffd9724e105 325 /** Turn off LED */
nsrwsurasak 23:7ffd9724e105 326 FILEMANAGER_SetLedStatus(false);
nsrwsurasak 23:7ffd9724e105 327
nsrwsurasak 23:7ffd9724e105 328 printf("Error! SD Card is not connected \n");
nsrwsurasak 23:7ffd9724e105 329
nsrwsurasak 23:7ffd9724e105 330 /** Return as nothing to be done */
nsrwsurasak 23:7ffd9724e105 331 return;
nsrwsurasak 23:7ffd9724e105 332 }
nsrwsurasak 31:983c30829d24 333
Lucyjungz 3:6e08d0bba1bb 334 /* Get File name */
Lucyjungz 1:1f1f2b99756b 335 char file_name[] = RTL_LOG_FILE_NAME;
Lucyjungz 1:1f1f2b99756b 336
Lucyjungz 3:6e08d0bba1bb 337 /* Generate File name with timestamp */
nsrwsurasak 25:7f3420c04178 338
Lucyjungz 14:4ba6147f067b 339 FILEMANAGER_GenerateFileNameWithTime(timestamp,file_name);
nsrwsurasak 25:7f3420c04178 340 #if DEBUG
nsrwsurasak 25:7f3420c04178 341 printf("\r\n File name is generated \n");
nsrwsurasak 25:7f3420c04178 342 #endif
Lucyjungz 33:dd67a980e9d8 343
nsrwsurasak 25:7f3420c04178 344 #if DEBUG
nsrwsurasak 25:7f3420c04178 345 printf("\r\n Going to Open RMS File For write \n");
nsrwsurasak 25:7f3420c04178 346 #endif
Lucyjungz 3:6e08d0bba1bb 347 /* Open file with "APPEND" mode */
Lucyjungz 1:1f1f2b99756b 348 FILE *fp = fopen(file_name, "a");
Lucyjungz 33:dd67a980e9d8 349 if (*p_headerRequired || !FILEMANAGER_IsFileExist(file_name))
Lucyjungz 33:dd67a980e9d8 350 {
Lucyjungz 33:dd67a980e9d8 351 /* If file is not exist, log the header */
Lucyjungz 33:dd67a980e9d8 352 FILEMANAGER_LogRMSHeader(fp, timestamp, file_name);
Lucyjungz 33:dd67a980e9d8 353
Lucyjungz 33:dd67a980e9d8 354 /* Clear flag once header has been written */
Lucyjungz 33:dd67a980e9d8 355 *p_headerRequired = false;
Lucyjungz 33:dd67a980e9d8 356 }
Lucyjungz 3:6e08d0bba1bb 357 if (fp == NULL)
Lucyjungz 3:6e08d0bba1bb 358 {
Lucyjungz 3:6e08d0bba1bb 359 /* In case of error, print the error message */
Lucyjungz 1:1f1f2b99756b 360 printf("Error! Unable to open file %s!\n",file_name);
Lucyjungz 11:e21d4c5bfd1b 361
Lucyjungz 11:e21d4c5bfd1b 362 /* Indicate LED Status (OFF)*/
Lucyjungz 14:4ba6147f067b 363 FILEMANAGER_SetLedStatus(false);
Lucyjungz 3:6e08d0bba1bb 364 }
Lucyjungz 3:6e08d0bba1bb 365 else
Lucyjungz 3:6e08d0bba1bb 366 {
Lucyjungz 11:e21d4c5bfd1b 367 /* Indicate LED Status (ON)*/
Lucyjungz 14:4ba6147f067b 368 FILEMANAGER_SetLedStatus(true);
Lucyjungz 11:e21d4c5bfd1b 369
nsrwsurasak 28:12c88b5e46e5 370 #if CAPTURE_TIME
nsrwsurasak 28:12c88b5e46e5 371 t.reset();
nsrwsurasak 28:12c88b5e46e5 372 t.start();
nsrwsurasak 28:12c88b5e46e5 373 #endif
Lucyjungz 33:dd67a980e9d8 374
Lucyjungz 33:dd67a980e9d8 375 #if DEBUG
Lucyjungz 3:6e08d0bba1bb 376 /* Print some message for information */
Lucyjungz 1:1f1f2b99756b 377 printf("\r\n Writing to Log File (%s)....",file_name);
Lucyjungz 33:dd67a980e9d8 378 #endif
Lucyjungz 3:6e08d0bba1bb 379 /* Write timestamp */
nsrwsurasak 25:7f3420c04178 380 fprintf(fp, "%d,%d",timestamp,msec);
Lucyjungz 1:1f1f2b99756b 381
Lucyjungz 3:6e08d0bba1bb 382 /* Write variable data */
Lucyjungz 1:1f1f2b99756b 383 for(int i = 0; i < size; i++)
Lucyjungz 1:1f1f2b99756b 384 {
nsrwsurasak 19:5af5c60e7a9f 385 fprintf(fp, ",%g",var[i]);
Lucyjungz 1:1f1f2b99756b 386 }
Lucyjungz 3:6e08d0bba1bb 387 /* Write new line as we done */
Lucyjungz 3:6e08d0bba1bb 388 fprintf(fp, "\n");
nsrwsurasak 0:a27e0d3581d1 389
Lucyjungz 33:dd67a980e9d8 390 #if DEBUG
Lucyjungz 1:1f1f2b99756b 391 printf("Done");
Lucyjungz 33:dd67a980e9d8 392 #endif
Lucyjungz 3:6e08d0bba1bb 393 /* Close the file */
Lucyjungz 3:6e08d0bba1bb 394 fclose(fp);
nsrwsurasak 26:2f10fcabed0e 395 #if defined(__MICROLIB) && defined(__ARMCC_VERSION) // with microlib and ARM compiler
nsrwsurasak 26:2f10fcabed0e 396 free(fp);
nsrwsurasak 26:2f10fcabed0e 397 #endif
nsrwsurasak 28:12c88b5e46e5 398 #if CAPTURE_TIME
nsrwsurasak 28:12c88b5e46e5 399 t.stop();
nsrwsurasak 28:12c88b5e46e5 400 printf("\r\n The RMS Write log time taken was %d milliseconds\n", t.read_ms());
nsrwsurasak 28:12c88b5e46e5 401 #endif
Lucyjungz 3:6e08d0bba1bb 402 }
Lucyjungz 3:6e08d0bba1bb 403 }
Lucyjungz 3:6e08d0bba1bb 404 /**
Lucyjungz 3:6e08d0bba1bb 405 * @brief Function to log RMS Header
Lucyjungz 3:6e08d0bba1bb 406 * @note sequence must be in the same order with variableList
Lucyjungz 3:6e08d0bba1bb 407 * @param timestamp - time structure to get Date
Lucyjungz 3:6e08d0bba1bb 408 * @retval None
Lucyjungz 3:6e08d0bba1bb 409 */
Lucyjungz 33:dd67a980e9d8 410 void FILEMANAGER_LogRMSHeader(FILE *fp, time_t timestamp, char file_name[])
nsrwsurasak 23:7ffd9724e105 411 {
Lucyjungz 33:dd67a980e9d8 412 #if DEBUG
Lucyjungz 33:dd67a980e9d8 413 /* opened file so can write */
Lucyjungz 33:dd67a980e9d8 414 printf("\r\n Writing Header to Log File (%s)....",file_name);
Lucyjungz 33:dd67a980e9d8 415 #endif
Lucyjungz 33:dd67a980e9d8 416 fprintf(fp, "%s,%s",RMS_HEADER_TIME,RMS_HEADER_MSECOND);
Lucyjungz 3:6e08d0bba1bb 417
Lucyjungz 33:dd67a980e9d8 418 for(int i = 0; i < m_amountVarList; i++)
Lucyjungz 33:dd67a980e9d8 419 {
Lucyjungz 33:dd67a980e9d8 420 fprintf(fp, ",%s",m_varList[i].varName);
Lucyjungz 3:6e08d0bba1bb 421 }
Lucyjungz 33:dd67a980e9d8 422 /* Write new line as done */
Lucyjungz 33:dd67a980e9d8 423 fprintf(fp, "\n-,-");
Lucyjungz 33:dd67a980e9d8 424
Lucyjungz 33:dd67a980e9d8 425 /* Write the unit of variables to the file */
Lucyjungz 33:dd67a980e9d8 426 for(int i = 0; i < m_amountVarList; i++)
Lucyjungz 33:dd67a980e9d8 427 {
Lucyjungz 33:dd67a980e9d8 428 fprintf(fp, ",%s",m_varList[i].varUnit);
Lucyjungz 33:dd67a980e9d8 429 }
Lucyjungz 33:dd67a980e9d8 430
Lucyjungz 33:dd67a980e9d8 431 /* Write new line as done */
Lucyjungz 33:dd67a980e9d8 432 fprintf(fp, "\n");
Lucyjungz 33:dd67a980e9d8 433 #if DEBUG
Lucyjungz 33:dd67a980e9d8 434 printf("Done");
Lucyjungz 33:dd67a980e9d8 435 #endif
Lucyjungz 3:6e08d0bba1bb 436 }
Lucyjungz 3:6e08d0bba1bb 437 /**
Lucyjungz 3:6e08d0bba1bb 438 * @brief Function to log Mini RMS System Data
Lucyjungz 3:6e08d0bba1bb 439 * @note this file is only for debug
nsrwsurasak 27:b0eb0f36110e 440 * @param gps_interval - gps interval
nsrwsurasak 27:b0eb0f36110e 441 * @param rms_interval - rms interval
nsrwsurasak 27:b0eb0f36110e 442 * @param isResetCausedByWdg - flag to indicate system restart by watchdog
Lucyjungz 3:6e08d0bba1bb 443 * @retval None
Lucyjungz 3:6e08d0bba1bb 444 */
nsrwsurasak 27:b0eb0f36110e 445 void FILEMANAGER_LogSystemData(float gps_interval,float rms_interval, bool isResetCausedByWdg)
Lucyjungz 3:6e08d0bba1bb 446 {
Lucyjungz 3:6e08d0bba1bb 447 /* Open the file in append mode */
Lucyjungz 3:6e08d0bba1bb 448 FILE *fp = fopen(MINIRMS_LOG_FILE_NAME, "a");
Lucyjungz 3:6e08d0bba1bb 449
Lucyjungz 3:6e08d0bba1bb 450 if (fp == NULL) {
Lucyjungz 3:6e08d0bba1bb 451 /* In case of error, print the msg */
Lucyjungz 3:6e08d0bba1bb 452 printf("Error! Unable to open file!\n");
Lucyjungz 11:e21d4c5bfd1b 453
Lucyjungz 11:e21d4c5bfd1b 454 /* Indicate LED Status (OFF)*/
Lucyjungz 14:4ba6147f067b 455 FILEMANAGER_SetLedStatus(false);
Lucyjungz 3:6e08d0bba1bb 456 }
Lucyjungz 3:6e08d0bba1bb 457 else
Lucyjungz 3:6e08d0bba1bb 458 {
Lucyjungz 11:e21d4c5bfd1b 459 /* Indicate LED Status (ON)*/
Lucyjungz 14:4ba6147f067b 460 FILEMANAGER_SetLedStatus(true);
nsrwsurasak 25:7f3420c04178 461 /* get Timestamp */
Lucyjungz 34:097b1f6828b7 462 time_t seconds = INTERNALRTC_GetEpoch();
Lucyjungz 34:097b1f6828b7 463
Lucyjungz 3:6e08d0bba1bb 464 /* Write some message, which is TBD */
nsrwsurasak 25:7f3420c04178 465 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 466 if (isResetCausedByWdg)
nsrwsurasak 27:b0eb0f36110e 467 {
nsrwsurasak 27:b0eb0f36110e 468 fprintf(fp, " **** Restart by Watchdog");
nsrwsurasak 27:b0eb0f36110e 469 }
Lucyjungz 1:1f1f2b99756b 470 fclose(fp); // ensure you close the file after writing
nsrwsurasak 26:2f10fcabed0e 471 #if defined(__MICROLIB) && defined(__ARMCC_VERSION) // with microlib and ARM compiler
nsrwsurasak 26:2f10fcabed0e 472 free(fp);
nsrwsurasak 26:2f10fcabed0e 473 #endif
Lucyjungz 1:1f1f2b99756b 474 }
Lucyjungz 1:1f1f2b99756b 475 }
Lucyjungz 3:6e08d0bba1bb 476 /**
Lucyjungz 3:6e08d0bba1bb 477 * @brief Utility to delete file
Lucyjungz 3:6e08d0bba1bb 478 * @note
Lucyjungz 3:6e08d0bba1bb 479 * @param filename - file name to be deleted
Lucyjungz 3:6e08d0bba1bb 480 * @retval None
Lucyjungz 3:6e08d0bba1bb 481 */
Lucyjungz 14:4ba6147f067b 482 void FILEMANAGER_Deletefile(char filename[])
nsrwsurasak 0:a27e0d3581d1 483 {
Lucyjungz 33:dd67a980e9d8 484 #if UART_FILE_ACCESS
Lucyjungz 32:69b7c870d69c 485 FILEMANAGER_AddPrefixFilename(filename, m_bufFileName);
Lucyjungz 33:dd67a980e9d8 486 #endif
nsrwsurasak 0:a27e0d3581d1 487 printf("Deleting file '%s'...",filename);
Lucyjungz 33:dd67a980e9d8 488
Lucyjungz 33:dd67a980e9d8 489 #if UART_FILE_ACCESS
Lucyjungz 32:69b7c870d69c 490 FILE *fp = fopen(m_bufFileName, "r"); // try and open file
Lucyjungz 33:dd67a980e9d8 491 #else
Lucyjungz 33:dd67a980e9d8 492 FILE *fp = fopen(filename, "r"); // try and open file
Lucyjungz 33:dd67a980e9d8 493 #endif
nsrwsurasak 0:a27e0d3581d1 494 if (fp != NULL) { // if it does open...
nsrwsurasak 0:a27e0d3581d1 495 fclose(fp); // close it
nsrwsurasak 26:2f10fcabed0e 496
nsrwsurasak 26:2f10fcabed0e 497 #if defined(__MICROLIB) && defined(__ARMCC_VERSION) // with microlib and ARM compiler
nsrwsurasak 26:2f10fcabed0e 498 free(fp);
nsrwsurasak 26:2f10fcabed0e 499 #endif
nsrwsurasak 26:2f10fcabed0e 500
Lucyjungz 33:dd67a980e9d8 501 #if UART_FILE_ACCESS
Lucyjungz 32:69b7c870d69c 502 remove(m_bufFileName); // and then delete
Lucyjungz 33:dd67a980e9d8 503 #else
Lucyjungz 33:dd67a980e9d8 504 remove(filename); // and then delete
Lucyjungz 33:dd67a980e9d8 505 #endif
nsrwsurasak 0:a27e0d3581d1 506 printf("Done!\n");
nsrwsurasak 0:a27e0d3581d1 507 }
nsrwsurasak 0:a27e0d3581d1 508 // if we can't open it, it doesn't exist and so we can't delete it
nsrwsurasak 0:a27e0d3581d1 509 }
Lucyjungz 3:6e08d0bba1bb 510 /**
Lucyjungz 3:6e08d0bba1bb 511 * @brief Utility to check file available
Lucyjungz 3:6e08d0bba1bb 512 * @note
Lucyjungz 3:6e08d0bba1bb 513 * @param filename - file name to be checked
Lucyjungz 3:6e08d0bba1bb 514 * @retval true - file is exist , false - file is not exist
Lucyjungz 3:6e08d0bba1bb 515 */
Lucyjungz 14:4ba6147f067b 516 bool FILEMANAGER_IsFileExist(char filename[])
Lucyjungz 1:1f1f2b99756b 517 {
Lucyjungz 1:1f1f2b99756b 518 bool exist = false;
Lucyjungz 1:1f1f2b99756b 519 FILE *fp = fopen(filename, "r"); // try and open file
Lucyjungz 1:1f1f2b99756b 520 if (fp != NULL) { // if it does open...
Lucyjungz 1:1f1f2b99756b 521 fclose(fp); // close it
nsrwsurasak 26:2f10fcabed0e 522
nsrwsurasak 26:2f10fcabed0e 523 #if defined(__MICROLIB) && defined(__ARMCC_VERSION) // with microlib and ARM compiler
nsrwsurasak 26:2f10fcabed0e 524 free(fp);
nsrwsurasak 26:2f10fcabed0e 525 #endif
nsrwsurasak 26:2f10fcabed0e 526
Lucyjungz 1:1f1f2b99756b 527 exist = true;
Lucyjungz 1:1f1f2b99756b 528 }
Lucyjungz 1:1f1f2b99756b 529
Lucyjungz 1:1f1f2b99756b 530 return exist;
Lucyjungz 1:1f1f2b99756b 531 }
Lucyjungz 3:6e08d0bba1bb 532 /**
Lucyjungz 3:6e08d0bba1bb 533 * @brief Utility to get GPS Interval
Lucyjungz 14:4ba6147f067b 534 * @note must be called after read the setup file
Lucyjungz 3:6e08d0bba1bb 535 * @param None
Lucyjungz 3:6e08d0bba1bb 536 * @retval GPS interval
Lucyjungz 3:6e08d0bba1bb 537 */
Lucyjungz 14:4ba6147f067b 538 int FILEMANAGER_GPSInterval()
nsrwsurasak 0:a27e0d3581d1 539 {
Lucyjungz 3:6e08d0bba1bb 540 /* Return interval in int */
Lucyjungz 14:4ba6147f067b 541 return ( m_GpsInterval );
nsrwsurasak 0:a27e0d3581d1 542 }
Lucyjungz 3:6e08d0bba1bb 543 /**
Lucyjungz 3:6e08d0bba1bb 544 * @brief Utility to get Data Interval
Lucyjungz 14:4ba6147f067b 545 * @note must be called after read the setup file
Lucyjungz 3:6e08d0bba1bb 546 * @param None
Lucyjungz 3:6e08d0bba1bb 547 * @retval Data interval
Lucyjungz 3:6e08d0bba1bb 548 */
Lucyjungz 14:4ba6147f067b 549 int FILEMANAGER_DataInterval()
nsrwsurasak 0:a27e0d3581d1 550 {
Lucyjungz 3:6e08d0bba1bb 551 /* Return interval in int */
Lucyjungz 14:4ba6147f067b 552 return ( m_DataInterval );
nsrwsurasak 0:a27e0d3581d1 553 }
Lucyjungz 3:6e08d0bba1bb 554 /**
Lucyjungz 3:6e08d0bba1bb 555 * @brief Function to read the variable list file
Lucyjungz 3:6e08d0bba1bb 556 * @note Recommended to call this function at initilization phase
Lucyjungz 3:6e08d0bba1bb 557 * @param None
Lucyjungz 3:6e08d0bba1bb 558 * @retval pointer to variable list
Lucyjungz 3:6e08d0bba1bb 559 */
Lucyjungz 14:4ba6147f067b 560 Variable_Data_TypeDef * FILEMANAGER_ReadVarFile()
nsrwsurasak 0:a27e0d3581d1 561 {
Lucyjungz 3:6e08d0bba1bb 562 /* Open the file with reading mode */
Lucyjungz 21:0c52dbc23f52 563 FILE *fp = fopen(m_varListFileName, "r");
nsrwsurasak 0:a27e0d3581d1 564
Lucyjungz 3:6e08d0bba1bb 565 if (fp == NULL)
Lucyjungz 3:6e08d0bba1bb 566 {
Lucyjungz 3:6e08d0bba1bb 567 /* if it can't open the file then print error message */
Lucyjungz 21:0c52dbc23f52 568 printf("\nError! Unable to open file! %s \n", m_varListFileName);
Lucyjungz 11:e21d4c5bfd1b 569 /* Indicate LED Status (OFF)*/
Lucyjungz 14:4ba6147f067b 570 FILEMANAGER_SetLedStatus(false);
Lucyjungz 11:e21d4c5bfd1b 571
nsrwsurasak 0:a27e0d3581d1 572 return NULL;
Lucyjungz 3:6e08d0bba1bb 573 }
Lucyjungz 3:6e08d0bba1bb 574 else
Lucyjungz 3:6e08d0bba1bb 575 {
Lucyjungz 11:e21d4c5bfd1b 576 /* Indicate LED Status (ON)*/
Lucyjungz 14:4ba6147f067b 577 FILEMANAGER_SetLedStatus(true);
nsrwsurasak 0:a27e0d3581d1 578
Lucyjungz 3:6e08d0bba1bb 579 /* Allocate buffer for reading */
Lucyjungz 14:4ba6147f067b 580 char buf[READ_FILE_BUFFER_SIZE];
nsrwsurasak 0:a27e0d3581d1 581 int index = 0;
Lucyjungz 3:6e08d0bba1bb 582
Lucyjungz 3:6e08d0bba1bb 583 /* Initialize return value */
nsrwsurasak 0:a27e0d3581d1 584 memset(m_varList, ' ', sizeof(m_varList));
Lucyjungz 3:6e08d0bba1bb 585
Lucyjungz 3:6e08d0bba1bb 586 /* Read line from the file */
Lucyjungz 3:6e08d0bba1bb 587 while (fgets(buf, sizeof(buf), fp) != NULL)
Lucyjungz 3:6e08d0bba1bb 588 {
Lucyjungz 3:6e08d0bba1bb 589 /* Check the TAG */
Lucyjungz 3:6e08d0bba1bb 590 if (strstr (buf,VAR_NAME_TAG))
Lucyjungz 3:6e08d0bba1bb 591 {
Lucyjungz 3:6e08d0bba1bb 592 /* Found variable TAG, populate it*/
Lucyjungz 14:4ba6147f067b 593 FILEMANAGER_GetXmlText(buf , m_varList[index].varName);
Lucyjungz 3:6e08d0bba1bb 594 }
Lucyjungz 3:6e08d0bba1bb 595 else if (strstr (buf,VAR_ADDR_TAG))
Lucyjungz 3:6e08d0bba1bb 596 {
Lucyjungz 3:6e08d0bba1bb 597 /* Found variable address TAG, populate it*/
Lucyjungz 14:4ba6147f067b 598 FILEMANAGER_GetXmlText(buf , m_varList[index].varAddress);
Lucyjungz 5:7c513eee7b2b 599 }
Lucyjungz 10:a8003d357cf2 600 else if (strstr (buf,VAR_TYPE_TAG))
Lucyjungz 10:a8003d357cf2 601 {
Lucyjungz 10:a8003d357cf2 602 /* Found variable type TAG, populate it*/
Lucyjungz 14:4ba6147f067b 603 FILEMANAGER_GetXmlText(buf , m_varList[index].varType);
Lucyjungz 10:a8003d357cf2 604 }
Lucyjungz 6:5bd75c0f607c 605 else if (strstr (buf,VAR_LSB1_TAG))
Lucyjungz 6:5bd75c0f607c 606 {
Lucyjungz 6:5bd75c0f607c 607 /* Found variable LSB1 TAG, populate it*/
Lucyjungz 14:4ba6147f067b 608 FILEMANAGER_GetXmlText(buf , m_varList[index].varLSB1);
Lucyjungz 6:5bd75c0f607c 609 }
Lucyjungz 6:5bd75c0f607c 610 else if (strstr (buf,VAR_LSB2_TAG))
Lucyjungz 5:7c513eee7b2b 611 {
Lucyjungz 6:5bd75c0f607c 612 /* Found variable LSB2 TAG, populate it*/
Lucyjungz 14:4ba6147f067b 613 FILEMANAGER_GetXmlText(buf , m_varList[index].varLSB2);
Lucyjungz 6:5bd75c0f607c 614 }
Lucyjungz 6:5bd75c0f607c 615 else if (strstr (buf,VAR_BITMASK_TAG))
Lucyjungz 6:5bd75c0f607c 616 {
Lucyjungz 6:5bd75c0f607c 617 /* Found variable BitMask TAG, populate it*/
Lucyjungz 14:4ba6147f067b 618 FILEMANAGER_GetXmlText(buf , m_varList[index].varBitMask);
Lucyjungz 5:7c513eee7b2b 619 }
Lucyjungz 5:7c513eee7b2b 620 else if (strstr (buf,VAR_UNIT_TAG))
Lucyjungz 5:7c513eee7b2b 621 {
Lucyjungz 5:7c513eee7b2b 622 /* Found variable unit TAG, populate it*/
Lucyjungz 14:4ba6147f067b 623 FILEMANAGER_GetXmlText(buf , m_varList[index].varUnit);
nsrwsurasak 0:a27e0d3581d1 624 index++;
nsrwsurasak 0:a27e0d3581d1 625 }
nsrwsurasak 0:a27e0d3581d1 626
nsrwsurasak 0:a27e0d3581d1 627 }
Lucyjungz 3:6e08d0bba1bb 628 /* Close File */
Lucyjungz 3:6e08d0bba1bb 629 fclose(fp);
nsrwsurasak 26:2f10fcabed0e 630 #if defined(__MICROLIB) && defined(__ARMCC_VERSION) // with microlib and ARM compiler
nsrwsurasak 26:2f10fcabed0e 631 free(fp);
nsrwsurasak 26:2f10fcabed0e 632 #endif
Lucyjungz 3:6e08d0bba1bb 633
Lucyjungz 3:6e08d0bba1bb 634 /* Populate amount */
nsrwsurasak 0:a27e0d3581d1 635 m_amountVarList = index;
Lucyjungz 3:6e08d0bba1bb 636
Lucyjungz 3:6e08d0bba1bb 637 /* Return variable list */
nsrwsurasak 0:a27e0d3581d1 638 return m_varList;
nsrwsurasak 0:a27e0d3581d1 639 }
nsrwsurasak 0:a27e0d3581d1 640 }
Lucyjungz 3:6e08d0bba1bb 641 /**
Lucyjungz 3:6e08d0bba1bb 642 * @brief Function to amount of variable for data logging
Lucyjungz 3:6e08d0bba1bb 643 * @note Must be called after readVarFile
Lucyjungz 3:6e08d0bba1bb 644 * @param None
Lucyjungz 3:6e08d0bba1bb 645 * @retval amount of variable list
Lucyjungz 3:6e08d0bba1bb 646 */
Lucyjungz 14:4ba6147f067b 647 int FILEMANAGER_GetAmountVarList()
nsrwsurasak 0:a27e0d3581d1 648 {
nsrwsurasak 0:a27e0d3581d1 649 return m_amountVarList;
nsrwsurasak 0:a27e0d3581d1 650 }
Lucyjungz 3:6e08d0bba1bb 651 /**
Lucyjungz 3:6e08d0bba1bb 652 * @brief Function to get variable list
Lucyjungz 3:6e08d0bba1bb 653 * @note Must be called after readVarFile
Lucyjungz 3:6e08d0bba1bb 654 * @param None
Lucyjungz 3:6e08d0bba1bb 655 * @retval pointer to variable list
Lucyjungz 3:6e08d0bba1bb 656 */
Lucyjungz 14:4ba6147f067b 657 Variable_Data_TypeDef * FILEMANAGER_GetVarList()
nsrwsurasak 0:a27e0d3581d1 658 {
nsrwsurasak 0:a27e0d3581d1 659 return m_varList;
nsrwsurasak 0:a27e0d3581d1 660 }
Lucyjungz 29:964610d82f8d 661 /**
Lucyjungz 29:964610d82f8d 662 * @brief Function to get Receieve ID
Lucyjungz 29:964610d82f8d 663 * @note Must be called after readSetupFile
Lucyjungz 29:964610d82f8d 664 * @param None
Lucyjungz 29:964610d82f8d 665 * @retval Receieve ID
Lucyjungz 29:964610d82f8d 666 */
Lucyjungz 29:964610d82f8d 667 uint32_t FILEMANAGER_GetRxID()
Lucyjungz 29:964610d82f8d 668 {
Lucyjungz 29:964610d82f8d 669 return m_RxID;
Lucyjungz 29:964610d82f8d 670 }
Lucyjungz 29:964610d82f8d 671 /**
Lucyjungz 29:964610d82f8d 672 * @brief Function to get Transmit ID
Lucyjungz 29:964610d82f8d 673 * @note Must be called after readSetupFile
Lucyjungz 29:964610d82f8d 674 * @param None
Lucyjungz 29:964610d82f8d 675 * @retval Transmit ID
Lucyjungz 29:964610d82f8d 676 */
Lucyjungz 29:964610d82f8d 677 uint32_t FILEMANAGER_GetTxID()
Lucyjungz 29:964610d82f8d 678 {
Lucyjungz 29:964610d82f8d 679 return m_TxID;
Lucyjungz 29:964610d82f8d 680 }
Lucyjungz 11:e21d4c5bfd1b 681 /**
Lucyjungz 11:e21d4c5bfd1b 682 * @brief Utility to play with LED status
Lucyjungz 11:e21d4c5bfd1b 683 * @note Libary user need to assign proper PinName to LED_SDCARD
Lucyjungz 11:e21d4c5bfd1b 684 * @param on - True for turning LED ON, otherwise LED off
Lucyjungz 11:e21d4c5bfd1b 685 * @retval None
Lucyjungz 11:e21d4c5bfd1b 686 */
Lucyjungz 14:4ba6147f067b 687 static void FILEMANAGER_SetLedStatus(bool on)
Lucyjungz 11:e21d4c5bfd1b 688 {
Lucyjungz 12:0045fca3c160 689 /* Check LED Connection */
nsrwsurasak 22:2fc64ad66e35 690 if (ledStatus.is_connected())
Lucyjungz 11:e21d4c5bfd1b 691 {
Lucyjungz 21:0c52dbc23f52 692 /* Set LED regarding to given argment */
nsrwsurasak 22:2fc64ad66e35 693 if (m_sdCardIsPresent)
nsrwsurasak 22:2fc64ad66e35 694 {
nsrwsurasak 22:2fc64ad66e35 695 ledStatus = on;
nsrwsurasak 22:2fc64ad66e35 696 }
nsrwsurasak 22:2fc64ad66e35 697 else
nsrwsurasak 22:2fc64ad66e35 698 {
nsrwsurasak 22:2fc64ad66e35 699 ledStatus = false;
nsrwsurasak 22:2fc64ad66e35 700 }
nsrwsurasak 22:2fc64ad66e35 701
Lucyjungz 12:0045fca3c160 702 }
Lucyjungz 12:0045fca3c160 703 #if DEBUG
Lucyjungz 12:0045fca3c160 704 else
Lucyjungz 12:0045fca3c160 705 {
Lucyjungz 14:4ba6147f067b 706 printf("\r\nSDCard LED is not connected !!");
Lucyjungz 12:0045fca3c160 707 }
Lucyjungz 12:0045fca3c160 708 #endif
Lucyjungz 11:e21d4c5bfd1b 709 }
Lucyjungz 32:69b7c870d69c 710
Lucyjungz 32:69b7c870d69c 711 #if UART_FILE_ACCESS
Lucyjungz 32:69b7c870d69c 712 /**
Lucyjungz 32:69b7c870d69c 713 * @brief Utility to show list of file in SD Card
Lucyjungz 32:69b7c870d69c 714 * @param serial - pointer to serial to be display as output
Lucyjungz 32:69b7c870d69c 715 * @retval None
Lucyjungz 32:69b7c870d69c 716 */
Lucyjungz 32:69b7c870d69c 717 void FILEMANAGER_listDir(Serial* serial)
Lucyjungz 30:184d2ef7dff7 718 {
Lucyjungz 15:b63a539c3754 719
Lucyjungz 30:184d2ef7dff7 720 DIR *d;
Lucyjungz 30:184d2ef7dff7 721 struct dirent *p;
Lucyjungz 30:184d2ef7dff7 722
Lucyjungz 30:184d2ef7dff7 723 d = opendir("/sd");
Lucyjungz 30:184d2ef7dff7 724 if (d != NULL) {
Lucyjungz 32:69b7c870d69c 725 while ((p = readdir(d)) != NULL)
Lucyjungz 32:69b7c870d69c 726 {
Lucyjungz 32:69b7c870d69c 727 if (serial != NULL)
Lucyjungz 32:69b7c870d69c 728 {
Lucyjungz 32:69b7c870d69c 729 serial->printf(" - %s\n\r", p->d_name);
Lucyjungz 32:69b7c870d69c 730 }
Lucyjungz 32:69b7c870d69c 731 else
Lucyjungz 32:69b7c870d69c 732 {
Lucyjungz 32:69b7c870d69c 733 printf(" - %s\n\r", p->d_name);
Lucyjungz 32:69b7c870d69c 734 }
Lucyjungz 32:69b7c870d69c 735
Lucyjungz 30:184d2ef7dff7 736 }
Lucyjungz 30:184d2ef7dff7 737 } else {
Lucyjungz 32:69b7c870d69c 738
Lucyjungz 30:184d2ef7dff7 739 printf("Could not open directory!\n");
Lucyjungz 30:184d2ef7dff7 740 }
Lucyjungz 30:184d2ef7dff7 741 closedir(d);
Lucyjungz 30:184d2ef7dff7 742 }
Lucyjungz 32:69b7c870d69c 743 /**
Lucyjungz 32:69b7c870d69c 744 * @brief Utility to display content of specific file
Lucyjungz 32:69b7c870d69c 745 * @param serial - pointer to serial to be display as output
Lucyjungz 32:69b7c870d69c 746 * @param filename - filename to be read
Lucyjungz 32:69b7c870d69c 747 * @retval None
Lucyjungz 32:69b7c870d69c 748 */
Lucyjungz 32:69b7c870d69c 749 void FILEMANAGER_DisplayFile(Serial* serial, char* filename)
Lucyjungz 32:69b7c870d69c 750 {
Lucyjungz 30:184d2ef7dff7 751
Lucyjungz 32:69b7c870d69c 752 FILEMANAGER_AddPrefixFilename(filename, m_bufFileName);
Lucyjungz 32:69b7c870d69c 753 /* Open the file with reading mode */
Lucyjungz 32:69b7c870d69c 754 FILE *fp = fopen(m_bufFileName, "r");
Lucyjungz 32:69b7c870d69c 755
Lucyjungz 32:69b7c870d69c 756 if (fp == NULL)
Lucyjungz 32:69b7c870d69c 757 {
Lucyjungz 32:69b7c870d69c 758 /* if it can't open the file then print error message */
Lucyjungz 32:69b7c870d69c 759 if (serial != NULL)
Lucyjungz 32:69b7c870d69c 760 {
Lucyjungz 32:69b7c870d69c 761 serial->printf("\nError! Unable to open file! %s \n", filename);
Lucyjungz 32:69b7c870d69c 762 }
Lucyjungz 32:69b7c870d69c 763 else
Lucyjungz 32:69b7c870d69c 764 {
Lucyjungz 32:69b7c870d69c 765 printf("\nError! Unable to open file! %s \n", filename);
Lucyjungz 32:69b7c870d69c 766 }
Lucyjungz 32:69b7c870d69c 767 }
Lucyjungz 32:69b7c870d69c 768 else
Lucyjungz 32:69b7c870d69c 769 {
Lucyjungz 32:69b7c870d69c 770 /* Allocate buffer for reading file */
Lucyjungz 32:69b7c870d69c 771 char buf[READ_FILE_BUFFER_SIZE];
Lucyjungz 32:69b7c870d69c 772 /* Read line from the file */
Lucyjungz 32:69b7c870d69c 773 while (fgets(buf, sizeof(buf), fp) != NULL)
Lucyjungz 32:69b7c870d69c 774 {
Lucyjungz 32:69b7c870d69c 775 if (serial != NULL)
Lucyjungz 32:69b7c870d69c 776 {
Lucyjungz 32:69b7c870d69c 777 serial->printf("\n%s", buf);
Lucyjungz 32:69b7c870d69c 778 }
Lucyjungz 32:69b7c870d69c 779 else
Lucyjungz 32:69b7c870d69c 780 {
Lucyjungz 32:69b7c870d69c 781 printf("\n%s", buf);
Lucyjungz 32:69b7c870d69c 782 }
Lucyjungz 32:69b7c870d69c 783 }
Lucyjungz 32:69b7c870d69c 784 /* Close File */
Lucyjungz 32:69b7c870d69c 785 fclose(fp);
Lucyjungz 32:69b7c870d69c 786 #if defined(__MICROLIB) && defined(__ARMCC_VERSION) // with microlib and ARM compiler
Lucyjungz 32:69b7c870d69c 787 free(fp);
Lucyjungz 32:69b7c870d69c 788 #endif
Lucyjungz 32:69b7c870d69c 789
Lucyjungz 32:69b7c870d69c 790 }
Lucyjungz 32:69b7c870d69c 791 }
Lucyjungz 32:69b7c870d69c 792 /**
Lucyjungz 32:69b7c870d69c 793 * @brief Utility to replace content of specific file
Lucyjungz 32:69b7c870d69c 794 * @param serial - pointer to serial to be display as output
Lucyjungz 32:69b7c870d69c 795 * @param filename - filename to be replace
Lucyjungz 32:69b7c870d69c 796 * @retval None
Lucyjungz 32:69b7c870d69c 797 */
Lucyjungz 32:69b7c870d69c 798 void FILEMANAGER_WriteFile(Serial* serial, char* filename, char* input)
Lucyjungz 32:69b7c870d69c 799 {
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 }
Lucyjungz 32:69b7c870d69c 814
Lucyjungz 32:69b7c870d69c 815 }
Lucyjungz 32:69b7c870d69c 816 else
Lucyjungz 32:69b7c870d69c 817 {
Lucyjungz 32:69b7c870d69c 818 fprintf(fp, "%s",input);
Lucyjungz 32:69b7c870d69c 819
Lucyjungz 32:69b7c870d69c 820 fclose(fp);
Lucyjungz 32:69b7c870d69c 821 #if defined(__MICROLIB) && defined(__ARMCC_VERSION) // with microlib and ARM compiler
Lucyjungz 32:69b7c870d69c 822 free(fp);
Lucyjungz 32:69b7c870d69c 823 #endif
Lucyjungz 32:69b7c870d69c 824 }
Lucyjungz 32:69b7c870d69c 825 }
Lucyjungz 32:69b7c870d69c 826 static void FILEMANAGER_AddPrefixFilename(char* in, char* out)
Lucyjungz 32:69b7c870d69c 827 {
Lucyjungz 32:69b7c870d69c 828 sprintf(out,"%s%s", PREFIX_FILE_NAME, in);
Lucyjungz 32:69b7c870d69c 829 }
Lucyjungz 32:69b7c870d69c 830 #endif