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