File manager
FileManager.cpp@8:5af4e12c43b2, 2016-05-18 (annotated)
- Committer:
- Lucyjungz
- Date:
- Wed May 18 07:38:25 2016 +0000
- Revision:
- 8:5af4e12c43b2
- Parent:
- 6:5bd75c0f607c
- Child:
- 9:b9115b3a77f9
Support Debug mode
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Lucyjungz | 1:1f1f2b99756b | 1 | #include "mbed.h" |
nsrwsurasak | 0:a27e0d3581d1 | 2 | #include "FileManager.h" |
nsrwsurasak | 0:a27e0d3581d1 | 3 | #include "SDFileSystem.h" |
nsrwsurasak | 0:a27e0d3581d1 | 4 | |
Lucyjungz | 3:6e08d0bba1bb | 5 | char m_GpsInterval[XMLTEXT_SIZE]; // GPS Interval |
Lucyjungz | 3:6e08d0bba1bb | 6 | char m_DataInterval[XMLTEXT_SIZE]; // Data Interval |
Lucyjungz | 3:6e08d0bba1bb | 7 | Variable_Data_TypeDef m_varList[MAX_VAR]; // Variable List |
Lucyjungz | 3:6e08d0bba1bb | 8 | unsigned int m_amountVarList = 0; // Amount of variable list |
Lucyjungz | 3:6e08d0bba1bb | 9 | |
Lucyjungz | 3:6e08d0bba1bb | 10 | /* ############### Static function prototype ################## */ |
nsrwsurasak | 0:a27e0d3581d1 | 11 | |
Lucyjungz | 4:ec62bf823914 | 12 | static void FileManager_RemoveSpaces(char* s , int size); |
Lucyjungz | 4:ec62bf823914 | 13 | static void FileManager_GetXmlText(char *str, char *ret); |
Lucyjungz | 4:ec62bf823914 | 14 | static void FileManager_GenerateFileNameWithTime(time_t timestamp, char * file_name); |
nsrwsurasak | 0:a27e0d3581d1 | 15 | |
Lucyjungz | 3:6e08d0bba1bb | 16 | |
Lucyjungz | 3:6e08d0bba1bb | 17 | /** |
Lucyjungz | 3:6e08d0bba1bb | 18 | * @brief Utility function to Remove space charector from given char array |
Lucyjungz | 3:6e08d0bba1bb | 19 | * @note |
Lucyjungz | 3:6e08d0bba1bb | 20 | * @param char array to process remove spaces |
Lucyjungz | 3:6e08d0bba1bb | 21 | * @param size of char array |
Lucyjungz | 3:6e08d0bba1bb | 22 | * @retval space removed char array |
Lucyjungz | 3:6e08d0bba1bb | 23 | */ |
Lucyjungz | 4:ec62bf823914 | 24 | static void FileManager_RemoveSpaces(char* s , int size) |
nsrwsurasak | 0:a27e0d3581d1 | 25 | { |
nsrwsurasak | 0:a27e0d3581d1 | 26 | char* cpy = s; // an alias to iterate through s without moving s |
nsrwsurasak | 0:a27e0d3581d1 | 27 | char* temp = s; |
nsrwsurasak | 0:a27e0d3581d1 | 28 | |
nsrwsurasak | 0:a27e0d3581d1 | 29 | for (int i = 0 ; i < size ; i++) { |
nsrwsurasak | 0:a27e0d3581d1 | 30 | if (*cpy != ' ') |
nsrwsurasak | 0:a27e0d3581d1 | 31 | *temp++ = *cpy; |
nsrwsurasak | 0:a27e0d3581d1 | 32 | cpy++; |
nsrwsurasak | 0:a27e0d3581d1 | 33 | } |
nsrwsurasak | 0:a27e0d3581d1 | 34 | *temp = 0; |
nsrwsurasak | 0:a27e0d3581d1 | 35 | return; |
nsrwsurasak | 0:a27e0d3581d1 | 36 | } |
Lucyjungz | 3:6e08d0bba1bb | 37 | /** |
Lucyjungz | 3:6e08d0bba1bb | 38 | * @brief Utility function to get XML tag |
Lucyjungz | 3:6e08d0bba1bb | 39 | * @note Only First tag will be returned |
Lucyjungz | 3:6e08d0bba1bb | 40 | * @param char array to get XML Text |
Lucyjungz | 3:6e08d0bba1bb | 41 | * @param char array to be populate XML text |
Lucyjungz | 3:6e08d0bba1bb | 42 | * @retval XML text |
Lucyjungz | 3:6e08d0bba1bb | 43 | */ |
Lucyjungz | 4:ec62bf823914 | 44 | static void FileManager_GetXmlText(char *str, char *ret) |
nsrwsurasak | 0:a27e0d3581d1 | 45 | { |
nsrwsurasak | 0:a27e0d3581d1 | 46 | int size = strlen(str); |
nsrwsurasak | 0:a27e0d3581d1 | 47 | int i; |
nsrwsurasak | 0:a27e0d3581d1 | 48 | bool begin_text = false; |
nsrwsurasak | 0:a27e0d3581d1 | 49 | char * ret_addr = ret; |
Lucyjungz | 3:6e08d0bba1bb | 50 | /* initialized our return value */ |
nsrwsurasak | 0:a27e0d3581d1 | 51 | memset (ret,' ',XMLTEXT_SIZE); |
nsrwsurasak | 0:a27e0d3581d1 | 52 | |
Lucyjungz | 3:6e08d0bba1bb | 53 | /* Loop to check XML tag symbols */ |
nsrwsurasak | 0:a27e0d3581d1 | 54 | for(i = 0; i < size ; i++) { |
nsrwsurasak | 0:a27e0d3581d1 | 55 | |
nsrwsurasak | 0:a27e0d3581d1 | 56 | if (*str == '>') { |
Lucyjungz | 3:6e08d0bba1bb | 57 | /* Found begining of the tag */ |
nsrwsurasak | 0:a27e0d3581d1 | 58 | begin_text = true; |
nsrwsurasak | 0:a27e0d3581d1 | 59 | } else if (begin_text && *str == '<') { |
Lucyjungz | 3:6e08d0bba1bb | 60 | /* Reach the end of text message */ |
nsrwsurasak | 0:a27e0d3581d1 | 61 | begin_text = false; |
nsrwsurasak | 0:a27e0d3581d1 | 62 | break; |
nsrwsurasak | 0:a27e0d3581d1 | 63 | } else if (begin_text && *str != ' ') { |
Lucyjungz | 3:6e08d0bba1bb | 64 | /* Populate the return value */ |
nsrwsurasak | 0:a27e0d3581d1 | 65 | *ret = *str; |
nsrwsurasak | 0:a27e0d3581d1 | 66 | ret++; |
nsrwsurasak | 0:a27e0d3581d1 | 67 | } |
Lucyjungz | 3:6e08d0bba1bb | 68 | /* Move to next char */ |
nsrwsurasak | 0:a27e0d3581d1 | 69 | str++; |
nsrwsurasak | 0:a27e0d3581d1 | 70 | } |
Lucyjungz | 3:6e08d0bba1bb | 71 | |
Lucyjungz | 3:6e08d0bba1bb | 72 | /* Remove space from return value */ |
Lucyjungz | 4:ec62bf823914 | 73 | FileManager_RemoveSpaces(ret_addr, XMLTEXT_SIZE); |
nsrwsurasak | 0:a27e0d3581d1 | 74 | } |
Lucyjungz | 3:6e08d0bba1bb | 75 | /** |
Lucyjungz | 3:6e08d0bba1bb | 76 | * @brief Utility function to get File Name with Date |
Lucyjungz | 3:6e08d0bba1bb | 77 | * @note Format file will be YYYY-MM-DD.filename |
Lucyjungz | 3:6e08d0bba1bb | 78 | * @param timestamp - time structure to get Date |
Lucyjungz | 3:6e08d0bba1bb | 79 | * @param file_name - char array to file name |
Lucyjungz | 3:6e08d0bba1bb | 80 | * @retval renamed file name |
Lucyjungz | 3:6e08d0bba1bb | 81 | */ |
Lucyjungz | 4:ec62bf823914 | 82 | static void FileManager_GenerateFileNameWithTime(time_t timestamp, char * file_name) |
Lucyjungz | 1:1f1f2b99756b | 83 | { |
Lucyjungz | 1:1f1f2b99756b | 84 | char str[5]; |
Lucyjungz | 1:1f1f2b99756b | 85 | struct tm * ptm; |
Lucyjungz | 1:1f1f2b99756b | 86 | |
Lucyjungz | 3:6e08d0bba1bb | 87 | /* Convert timestamp to readable format */ |
Lucyjungz | 3:6e08d0bba1bb | 88 | ptm = localtime ( ×tamp ); |
Lucyjungz | 3:6e08d0bba1bb | 89 | |
Lucyjungz | 3:6e08d0bba1bb | 90 | /* Replacing YYYY to the converted year */ |
Lucyjungz | 1:1f1f2b99756b | 91 | sprintf(str,"%04d", ptm->tm_year-100 + 2000); |
Lucyjungz | 1:1f1f2b99756b | 92 | memcpy(&file_name[4], str, 4); |
Lucyjungz | 1:1f1f2b99756b | 93 | |
Lucyjungz | 3:6e08d0bba1bb | 94 | /* Replacing MM to converted month */ |
Lucyjungz | 1:1f1f2b99756b | 95 | sprintf(str,"%02d", ptm->tm_mon+1); |
Lucyjungz | 1:1f1f2b99756b | 96 | memcpy(&file_name[9], str, 2); |
Lucyjungz | 1:1f1f2b99756b | 97 | |
Lucyjungz | 3:6e08d0bba1bb | 98 | /* Replacing DD to converted date */ |
Lucyjungz | 1:1f1f2b99756b | 99 | sprintf(str,"%02d", ptm->tm_mday); |
Lucyjungz | 1:1f1f2b99756b | 100 | memcpy(&file_name[12], str, 2); |
Lucyjungz | 1:1f1f2b99756b | 101 | } |
Lucyjungz | 3:6e08d0bba1bb | 102 | /** |
Lucyjungz | 3:6e08d0bba1bb | 103 | * @brief Function to perform read setup file |
Lucyjungz | 3:6e08d0bba1bb | 104 | * @note filename must be defined in FileManager.h, GPS/ Data interval will be stored in dedicated variable |
Lucyjungz | 3:6e08d0bba1bb | 105 | * @param None |
Lucyjungz | 3:6e08d0bba1bb | 106 | * @retval None |
Lucyjungz | 3:6e08d0bba1bb | 107 | */ |
Lucyjungz | 4:ec62bf823914 | 108 | void FileManager_ReadSetupFile() |
nsrwsurasak | 0:a27e0d3581d1 | 109 | { |
Lucyjungz | 3:6e08d0bba1bb | 110 | /* Open file in reading mode */ |
nsrwsurasak | 0:a27e0d3581d1 | 111 | FILE *fp = fopen(SETUP_FILE_NAME, "r"); |
nsrwsurasak | 0:a27e0d3581d1 | 112 | |
Lucyjungz | 3:6e08d0bba1bb | 113 | if (fp == NULL) { |
Lucyjungz | 3:6e08d0bba1bb | 114 | /* In case of error, print the message */ |
nsrwsurasak | 0:a27e0d3581d1 | 115 | printf("\nError! Unable to open file! %s \n", SETUP_FILE_NAME); |
Lucyjungz | 3:6e08d0bba1bb | 116 | } else { |
nsrwsurasak | 0:a27e0d3581d1 | 117 | |
Lucyjungz | 3:6e08d0bba1bb | 118 | /* Initialized state */ |
nsrwsurasak | 0:a27e0d3581d1 | 119 | ReadingFileState state = STATE_FINDING; |
Lucyjungz | 3:6e08d0bba1bb | 120 | |
Lucyjungz | 3:6e08d0bba1bb | 121 | /* Allocate buffer for reading file */ |
nsrwsurasak | 0:a27e0d3581d1 | 122 | char buf[1024]; |
Lucyjungz | 3:6e08d0bba1bb | 123 | |
Lucyjungz | 3:6e08d0bba1bb | 124 | /* Read line from the file */ |
nsrwsurasak | 0:a27e0d3581d1 | 125 | while (fgets(buf, sizeof(buf), fp) != NULL) { |
Lucyjungz | 3:6e08d0bba1bb | 126 | |
Lucyjungz | 3:6e08d0bba1bb | 127 | /* Check the TAG */ |
Lucyjungz | 3:6e08d0bba1bb | 128 | if (strstr (buf,DATA_TAG)) |
Lucyjungz | 3:6e08d0bba1bb | 129 | { |
Lucyjungz | 3:6e08d0bba1bb | 130 | /* Found the DATA TAG */ |
nsrwsurasak | 0:a27e0d3581d1 | 131 | state = STATE_FOUND_DATA; |
Lucyjungz | 3:6e08d0bba1bb | 132 | } else if (strstr (buf,GPS_TAG)) |
Lucyjungz | 3:6e08d0bba1bb | 133 | { |
Lucyjungz | 3:6e08d0bba1bb | 134 | /* Found GPS TAG */ |
nsrwsurasak | 0:a27e0d3581d1 | 135 | state = STATE_FOUND_GPS; |
Lucyjungz | 3:6e08d0bba1bb | 136 | } else if (strstr (buf,UPDATE_INTERVAL_TAG)) |
Lucyjungz | 3:6e08d0bba1bb | 137 | { |
Lucyjungz | 3:6e08d0bba1bb | 138 | /* Found Interval TAG */ |
Lucyjungz | 3:6e08d0bba1bb | 139 | if (state == STATE_FOUND_GPS) |
Lucyjungz | 3:6e08d0bba1bb | 140 | { |
Lucyjungz | 3:6e08d0bba1bb | 141 | /* Get XML text for GPS Interval */ |
Lucyjungz | 4:ec62bf823914 | 142 | FileManager_GetXmlText(buf, m_GpsInterval); |
Lucyjungz | 8:5af4e12c43b2 | 143 | #if DEBUG |
nsrwsurasak | 0:a27e0d3581d1 | 144 | printf("\r\n-found GPS interval %s ", m_GpsInterval); |
Lucyjungz | 8:5af4e12c43b2 | 145 | #endif |
Lucyjungz | 8:5af4e12c43b2 | 146 | |
Lucyjungz | 8:5af4e12c43b2 | 147 | /* Set state to indicate that we are finding */ |
nsrwsurasak | 0:a27e0d3581d1 | 148 | state = STATE_FINDING; |
Lucyjungz | 3:6e08d0bba1bb | 149 | } |
Lucyjungz | 3:6e08d0bba1bb | 150 | else if(state == STATE_FOUND_DATA) |
Lucyjungz | 3:6e08d0bba1bb | 151 | { |
Lucyjungz | 3:6e08d0bba1bb | 152 | /* Get XML text for Data Interval */ |
Lucyjungz | 4:ec62bf823914 | 153 | FileManager_GetXmlText(buf, m_DataInterval); |
Lucyjungz | 8:5af4e12c43b2 | 154 | |
Lucyjungz | 8:5af4e12c43b2 | 155 | #if DEBUG |
nsrwsurasak | 0:a27e0d3581d1 | 156 | printf("\r\n-found Data interval %s ", m_DataInterval); |
Lucyjungz | 8:5af4e12c43b2 | 157 | #endif |
Lucyjungz | 8:5af4e12c43b2 | 158 | |
Lucyjungz | 8:5af4e12c43b2 | 159 | /* Set state to indicate that we are finding */ |
nsrwsurasak | 0:a27e0d3581d1 | 160 | state = STATE_FINDING; |
nsrwsurasak | 0:a27e0d3581d1 | 161 | } |
nsrwsurasak | 0:a27e0d3581d1 | 162 | } |
nsrwsurasak | 0:a27e0d3581d1 | 163 | } |
Lucyjungz | 3:6e08d0bba1bb | 164 | /* Ensure file is closed */ |
Lucyjungz | 3:6e08d0bba1bb | 165 | fclose(fp); |
nsrwsurasak | 0:a27e0d3581d1 | 166 | } |
nsrwsurasak | 0:a27e0d3581d1 | 167 | } |
Lucyjungz | 3:6e08d0bba1bb | 168 | /** |
Lucyjungz | 3:6e08d0bba1bb | 169 | * @brief Function to log GPS Data |
Lucyjungz | 3:6e08d0bba1bb | 170 | * @note |
Lucyjungz | 3:6e08d0bba1bb | 171 | * @param timestamp - time structure to get Date |
Lucyjungz | 3:6e08d0bba1bb | 172 | * @param lat - char array for lattitude |
Lucyjungz | 3:6e08d0bba1bb | 173 | * @param longti - char array for longtitude |
Lucyjungz | 3:6e08d0bba1bb | 174 | * @retval None |
Lucyjungz | 3:6e08d0bba1bb | 175 | */ |
Lucyjungz | 4:ec62bf823914 | 176 | void FileManager_LogGPSData(time_t timestamp ,char lat[], char longti[]) |
nsrwsurasak | 0:a27e0d3581d1 | 177 | { |
Lucyjungz | 3:6e08d0bba1bb | 178 | /* Get File name */ |
Lucyjungz | 1:1f1f2b99756b | 179 | char file_name[] = GPS_LOG_FILE_NAME; |
Lucyjungz | 1:1f1f2b99756b | 180 | |
Lucyjungz | 3:6e08d0bba1bb | 181 | /* Generate file name with time stamp */ |
Lucyjungz | 4:ec62bf823914 | 182 | FileManager_GenerateFileNameWithTime(timestamp,file_name); |
Lucyjungz | 3:6e08d0bba1bb | 183 | |
Lucyjungz | 3:6e08d0bba1bb | 184 | /* Open file with "APPEND" mode */ |
Lucyjungz | 1:1f1f2b99756b | 185 | FILE *fp = fopen(file_name, "a"); |
Lucyjungz | 1:1f1f2b99756b | 186 | |
Lucyjungz | 3:6e08d0bba1bb | 187 | if (fp == NULL) |
Lucyjungz | 3:6e08d0bba1bb | 188 | { |
Lucyjungz | 3:6e08d0bba1bb | 189 | /* if it can't open the file then print error message */ |
Lucyjungz | 1:1f1f2b99756b | 190 | printf("Error! Unable to open file %s!\n",file_name); |
Lucyjungz | 3:6e08d0bba1bb | 191 | } |
Lucyjungz | 3:6e08d0bba1bb | 192 | else { |
Lucyjungz | 3:6e08d0bba1bb | 193 | /* Print some message for information */ |
Lucyjungz | 1:1f1f2b99756b | 194 | printf("\r\n Writing to Gps Log File (%s)....",file_name); |
Lucyjungz | 1:1f1f2b99756b | 195 | |
Lucyjungz | 3:6e08d0bba1bb | 196 | /* Write the line with lattitude and longtitude */ |
Lucyjungz | 3:6e08d0bba1bb | 197 | fprintf(fp, "%d,%s,%s\n",timestamp,lat,longti); |
Lucyjungz | 1:1f1f2b99756b | 198 | |
Lucyjungz | 1:1f1f2b99756b | 199 | printf("Done"); |
Lucyjungz | 3:6e08d0bba1bb | 200 | /* Close file once it done */ |
Lucyjungz | 3:6e08d0bba1bb | 201 | fclose(fp); |
Lucyjungz | 1:1f1f2b99756b | 202 | } |
Lucyjungz | 1:1f1f2b99756b | 203 | } |
Lucyjungz | 3:6e08d0bba1bb | 204 | /** |
Lucyjungz | 3:6e08d0bba1bb | 205 | * @brief Function to log RMS Data |
Lucyjungz | 3:6e08d0bba1bb | 206 | * @note sequence must be in the same order with variableList |
Lucyjungz | 3:6e08d0bba1bb | 207 | * @param timestamp - time structure to get Date |
Lucyjungz | 3:6e08d0bba1bb | 208 | * @param var - float array to be log in the file |
Lucyjungz | 3:6e08d0bba1bb | 209 | * @param size - size of the float array |
Lucyjungz | 3:6e08d0bba1bb | 210 | * @retval None |
Lucyjungz | 3:6e08d0bba1bb | 211 | */ |
Lucyjungz | 4:ec62bf823914 | 212 | void FileManager_LogRMSData(time_t timestamp ,float * var, int size) |
Lucyjungz | 1:1f1f2b99756b | 213 | { |
Lucyjungz | 3:6e08d0bba1bb | 214 | /* Get File name */ |
Lucyjungz | 1:1f1f2b99756b | 215 | char file_name[] = RTL_LOG_FILE_NAME; |
Lucyjungz | 1:1f1f2b99756b | 216 | |
Lucyjungz | 3:6e08d0bba1bb | 217 | /* Generate File name with timestamp */ |
Lucyjungz | 4:ec62bf823914 | 218 | FileManager_GenerateFileNameWithTime(timestamp,file_name); |
Lucyjungz | 4:ec62bf823914 | 219 | if (!FileManager_IsFileExist(file_name)) |
Lucyjungz | 1:1f1f2b99756b | 220 | { |
Lucyjungz | 3:6e08d0bba1bb | 221 | /* If file is not exist, log the header */ |
Lucyjungz | 4:ec62bf823914 | 222 | FileManager_LogRMSHeader(timestamp); |
Lucyjungz | 1:1f1f2b99756b | 223 | } |
Lucyjungz | 3:6e08d0bba1bb | 224 | /* Open file with "APPEND" mode */ |
Lucyjungz | 1:1f1f2b99756b | 225 | FILE *fp = fopen(file_name, "a"); |
nsrwsurasak | 0:a27e0d3581d1 | 226 | |
Lucyjungz | 3:6e08d0bba1bb | 227 | if (fp == NULL) |
Lucyjungz | 3:6e08d0bba1bb | 228 | { |
Lucyjungz | 3:6e08d0bba1bb | 229 | /* In case of error, print the error message */ |
Lucyjungz | 1:1f1f2b99756b | 230 | printf("Error! Unable to open file %s!\n",file_name); |
Lucyjungz | 3:6e08d0bba1bb | 231 | } |
Lucyjungz | 3:6e08d0bba1bb | 232 | else |
Lucyjungz | 3:6e08d0bba1bb | 233 | { |
Lucyjungz | 3:6e08d0bba1bb | 234 | /* Print some message for information */ |
Lucyjungz | 1:1f1f2b99756b | 235 | printf("\r\n Writing to Log File (%s)....",file_name); |
Lucyjungz | 1:1f1f2b99756b | 236 | |
Lucyjungz | 3:6e08d0bba1bb | 237 | /* Write timestamp */ |
Lucyjungz | 3:6e08d0bba1bb | 238 | fprintf(fp, "%d",timestamp); |
Lucyjungz | 1:1f1f2b99756b | 239 | |
Lucyjungz | 3:6e08d0bba1bb | 240 | /* Write variable data */ |
Lucyjungz | 1:1f1f2b99756b | 241 | for(int i = 0; i < size; i++) |
Lucyjungz | 1:1f1f2b99756b | 242 | { |
Lucyjungz | 3:6e08d0bba1bb | 243 | fprintf(fp, ",%f",var[i]); |
Lucyjungz | 1:1f1f2b99756b | 244 | } |
Lucyjungz | 3:6e08d0bba1bb | 245 | /* Write new line as we done */ |
Lucyjungz | 3:6e08d0bba1bb | 246 | fprintf(fp, "\n"); |
nsrwsurasak | 0:a27e0d3581d1 | 247 | |
Lucyjungz | 1:1f1f2b99756b | 248 | printf("Done"); |
Lucyjungz | 3:6e08d0bba1bb | 249 | /* Close the file */ |
Lucyjungz | 3:6e08d0bba1bb | 250 | fclose(fp); |
Lucyjungz | 3:6e08d0bba1bb | 251 | } |
Lucyjungz | 3:6e08d0bba1bb | 252 | } |
Lucyjungz | 3:6e08d0bba1bb | 253 | /** |
Lucyjungz | 3:6e08d0bba1bb | 254 | * @brief Function to log RMS Header |
Lucyjungz | 3:6e08d0bba1bb | 255 | * @note sequence must be in the same order with variableList |
Lucyjungz | 3:6e08d0bba1bb | 256 | * @param timestamp - time structure to get Date |
Lucyjungz | 3:6e08d0bba1bb | 257 | * @retval None |
Lucyjungz | 3:6e08d0bba1bb | 258 | */ |
Lucyjungz | 4:ec62bf823914 | 259 | void FileManager_LogRMSHeader(time_t timestamp) |
Lucyjungz | 3:6e08d0bba1bb | 260 | { |
Lucyjungz | 3:6e08d0bba1bb | 261 | /* Get File name */ |
Lucyjungz | 3:6e08d0bba1bb | 262 | char file_name[] = RTL_LOG_FILE_NAME; |
Lucyjungz | 3:6e08d0bba1bb | 263 | |
Lucyjungz | 3:6e08d0bba1bb | 264 | /* Generate file name with time */ |
Lucyjungz | 4:ec62bf823914 | 265 | FileManager_GenerateFileNameWithTime(timestamp,file_name); |
Lucyjungz | 3:6e08d0bba1bb | 266 | |
Lucyjungz | 3:6e08d0bba1bb | 267 | /* Open file in append mode */ |
Lucyjungz | 3:6e08d0bba1bb | 268 | FILE *fp = fopen(file_name, "a"); |
Lucyjungz | 3:6e08d0bba1bb | 269 | |
Lucyjungz | 3:6e08d0bba1bb | 270 | if (fp == NULL) |
Lucyjungz | 3:6e08d0bba1bb | 271 | { |
Lucyjungz | 3:6e08d0bba1bb | 272 | /* In case of error, print the error message */ |
Lucyjungz | 3:6e08d0bba1bb | 273 | printf("Error! Unable to open file %s!\n",file_name); |
Lucyjungz | 3:6e08d0bba1bb | 274 | } |
Lucyjungz | 3:6e08d0bba1bb | 275 | else |
Lucyjungz | 3:6e08d0bba1bb | 276 | { |
Lucyjungz | 3:6e08d0bba1bb | 277 | /* opened file so can write */ |
Lucyjungz | 3:6e08d0bba1bb | 278 | printf("\r\n Writing to Log File (%s)....",file_name); |
Lucyjungz | 3:6e08d0bba1bb | 279 | |
Lucyjungz | 3:6e08d0bba1bb | 280 | /* Write the header to the file */ |
Lucyjungz | 3:6e08d0bba1bb | 281 | fprintf(fp, "%s",RMS_HEADER_TIME); |
Lucyjungz | 3:6e08d0bba1bb | 282 | for(int i = 0; i < m_amountVarList; i++) |
Lucyjungz | 3:6e08d0bba1bb | 283 | { |
Lucyjungz | 3:6e08d0bba1bb | 284 | fprintf(fp, ",%s",m_varList[i].varName); |
Lucyjungz | 3:6e08d0bba1bb | 285 | } |
Lucyjungz | 3:6e08d0bba1bb | 286 | /* Write new line as done */ |
Lucyjungz | 3:6e08d0bba1bb | 287 | fprintf(fp, "\n"); |
Lucyjungz | 3:6e08d0bba1bb | 288 | |
Lucyjungz | 3:6e08d0bba1bb | 289 | printf("Done"); |
Lucyjungz | 3:6e08d0bba1bb | 290 | |
Lucyjungz | 3:6e08d0bba1bb | 291 | /* Close the file */ |
Lucyjungz | 3:6e08d0bba1bb | 292 | fclose(fp); |
Lucyjungz | 3:6e08d0bba1bb | 293 | } |
Lucyjungz | 3:6e08d0bba1bb | 294 | } |
Lucyjungz | 3:6e08d0bba1bb | 295 | /** |
Lucyjungz | 3:6e08d0bba1bb | 296 | * @brief Function to log Mini RMS System Data |
Lucyjungz | 3:6e08d0bba1bb | 297 | * @note this file is only for debug |
Lucyjungz | 3:6e08d0bba1bb | 298 | * @param gps_interval - |
Lucyjungz | 3:6e08d0bba1bb | 299 | * @retval None |
Lucyjungz | 3:6e08d0bba1bb | 300 | */ |
Lucyjungz | 4:ec62bf823914 | 301 | void FileManager_LogSystemData(float gps_interval) |
Lucyjungz | 3:6e08d0bba1bb | 302 | { |
Lucyjungz | 3:6e08d0bba1bb | 303 | /* Open the file in append mode */ |
Lucyjungz | 3:6e08d0bba1bb | 304 | FILE *fp = fopen(MINIRMS_LOG_FILE_NAME, "a"); |
Lucyjungz | 3:6e08d0bba1bb | 305 | |
Lucyjungz | 3:6e08d0bba1bb | 306 | if (fp == NULL) { |
Lucyjungz | 3:6e08d0bba1bb | 307 | /* In case of error, print the msg */ |
Lucyjungz | 3:6e08d0bba1bb | 308 | printf("Error! Unable to open file!\n"); |
Lucyjungz | 3:6e08d0bba1bb | 309 | } |
Lucyjungz | 3:6e08d0bba1bb | 310 | else |
Lucyjungz | 3:6e08d0bba1bb | 311 | { |
Lucyjungz | 3:6e08d0bba1bb | 312 | /* Write some message, which is TBD */ |
Lucyjungz | 3:6e08d0bba1bb | 313 | fprintf(fp, "\nStart Mini-RMS System with Gps Interval = %f",gps_interval); |
Lucyjungz | 1:1f1f2b99756b | 314 | fclose(fp); // ensure you close the file after writing |
Lucyjungz | 1:1f1f2b99756b | 315 | } |
Lucyjungz | 1:1f1f2b99756b | 316 | } |
Lucyjungz | 3:6e08d0bba1bb | 317 | /** |
Lucyjungz | 3:6e08d0bba1bb | 318 | * @brief Utility to delete file |
Lucyjungz | 3:6e08d0bba1bb | 319 | * @note |
Lucyjungz | 3:6e08d0bba1bb | 320 | * @param filename - file name to be deleted |
Lucyjungz | 3:6e08d0bba1bb | 321 | * @retval None |
Lucyjungz | 3:6e08d0bba1bb | 322 | */ |
Lucyjungz | 4:ec62bf823914 | 323 | void FileManager_Deletefile(char filename[]) |
nsrwsurasak | 0:a27e0d3581d1 | 324 | { |
nsrwsurasak | 0:a27e0d3581d1 | 325 | printf("Deleting file '%s'...",filename); |
nsrwsurasak | 0:a27e0d3581d1 | 326 | FILE *fp = fopen(filename, "r"); // try and open file |
nsrwsurasak | 0:a27e0d3581d1 | 327 | if (fp != NULL) { // if it does open... |
nsrwsurasak | 0:a27e0d3581d1 | 328 | fclose(fp); // close it |
nsrwsurasak | 0:a27e0d3581d1 | 329 | remove(filename); // and then delete |
nsrwsurasak | 0:a27e0d3581d1 | 330 | printf("Done!\n"); |
nsrwsurasak | 0:a27e0d3581d1 | 331 | } |
nsrwsurasak | 0:a27e0d3581d1 | 332 | // if we can't open it, it doesn't exist and so we can't delete it |
nsrwsurasak | 0:a27e0d3581d1 | 333 | } |
Lucyjungz | 3:6e08d0bba1bb | 334 | /** |
Lucyjungz | 3:6e08d0bba1bb | 335 | * @brief Utility to check file available |
Lucyjungz | 3:6e08d0bba1bb | 336 | * @note |
Lucyjungz | 3:6e08d0bba1bb | 337 | * @param filename - file name to be checked |
Lucyjungz | 3:6e08d0bba1bb | 338 | * @retval true - file is exist , false - file is not exist |
Lucyjungz | 3:6e08d0bba1bb | 339 | */ |
Lucyjungz | 4:ec62bf823914 | 340 | bool FileManager_IsFileExist(char filename[]) |
Lucyjungz | 1:1f1f2b99756b | 341 | { |
Lucyjungz | 1:1f1f2b99756b | 342 | bool exist = false; |
Lucyjungz | 1:1f1f2b99756b | 343 | FILE *fp = fopen(filename, "r"); // try and open file |
Lucyjungz | 1:1f1f2b99756b | 344 | if (fp != NULL) { // if it does open... |
Lucyjungz | 1:1f1f2b99756b | 345 | fclose(fp); // close it |
Lucyjungz | 1:1f1f2b99756b | 346 | exist = true; |
Lucyjungz | 1:1f1f2b99756b | 347 | } |
Lucyjungz | 1:1f1f2b99756b | 348 | |
Lucyjungz | 1:1f1f2b99756b | 349 | return exist; |
Lucyjungz | 1:1f1f2b99756b | 350 | } |
Lucyjungz | 3:6e08d0bba1bb | 351 | /** |
Lucyjungz | 3:6e08d0bba1bb | 352 | * @brief Utility to get GPS Interval |
Lucyjungz | 3:6e08d0bba1bb | 353 | * @note must be after read the setup file |
Lucyjungz | 3:6e08d0bba1bb | 354 | * @param None |
Lucyjungz | 3:6e08d0bba1bb | 355 | * @retval GPS interval |
Lucyjungz | 3:6e08d0bba1bb | 356 | */ |
Lucyjungz | 4:ec62bf823914 | 357 | int FileManager_GPSInterval() |
nsrwsurasak | 0:a27e0d3581d1 | 358 | { |
Lucyjungz | 3:6e08d0bba1bb | 359 | /* Return interval in int */ |
nsrwsurasak | 0:a27e0d3581d1 | 360 | return atoi( m_GpsInterval ); |
nsrwsurasak | 0:a27e0d3581d1 | 361 | } |
Lucyjungz | 3:6e08d0bba1bb | 362 | /** |
Lucyjungz | 3:6e08d0bba1bb | 363 | * @brief Utility to get Data Interval |
Lucyjungz | 3:6e08d0bba1bb | 364 | * @note must be after read the setup file |
Lucyjungz | 3:6e08d0bba1bb | 365 | * @param None |
Lucyjungz | 3:6e08d0bba1bb | 366 | * @retval Data interval |
Lucyjungz | 3:6e08d0bba1bb | 367 | */ |
Lucyjungz | 4:ec62bf823914 | 368 | int FileManager_DataInterval() |
nsrwsurasak | 0:a27e0d3581d1 | 369 | { |
Lucyjungz | 3:6e08d0bba1bb | 370 | /* Return interval in int */ |
nsrwsurasak | 0:a27e0d3581d1 | 371 | return atoi( m_DataInterval ); |
nsrwsurasak | 0:a27e0d3581d1 | 372 | } |
Lucyjungz | 3:6e08d0bba1bb | 373 | /** |
Lucyjungz | 3:6e08d0bba1bb | 374 | * @brief Function to read the variable list file |
Lucyjungz | 3:6e08d0bba1bb | 375 | * @note Recommended to call this function at initilization phase |
Lucyjungz | 3:6e08d0bba1bb | 376 | * @param None |
Lucyjungz | 3:6e08d0bba1bb | 377 | * @retval pointer to variable list |
Lucyjungz | 3:6e08d0bba1bb | 378 | */ |
Lucyjungz | 4:ec62bf823914 | 379 | Variable_Data_TypeDef * FileManager_ReadVarFile() |
nsrwsurasak | 0:a27e0d3581d1 | 380 | { |
Lucyjungz | 3:6e08d0bba1bb | 381 | /* Open the file with reading mode */ |
nsrwsurasak | 0:a27e0d3581d1 | 382 | FILE *fp = fopen(VARIABLE_FILE_NAME, "r"); |
nsrwsurasak | 0:a27e0d3581d1 | 383 | |
Lucyjungz | 3:6e08d0bba1bb | 384 | if (fp == NULL) |
Lucyjungz | 3:6e08d0bba1bb | 385 | { |
Lucyjungz | 3:6e08d0bba1bb | 386 | /* if it can't open the file then print error message */ |
nsrwsurasak | 0:a27e0d3581d1 | 387 | printf("\nError! Unable to open file! %s \n", VARIABLE_FILE_NAME); |
nsrwsurasak | 0:a27e0d3581d1 | 388 | return NULL; |
Lucyjungz | 3:6e08d0bba1bb | 389 | } |
Lucyjungz | 3:6e08d0bba1bb | 390 | else |
Lucyjungz | 3:6e08d0bba1bb | 391 | { |
Lucyjungz | 3:6e08d0bba1bb | 392 | /* opened file so can write */ |
nsrwsurasak | 0:a27e0d3581d1 | 393 | |
Lucyjungz | 3:6e08d0bba1bb | 394 | /* Allocate buffer for reading */ |
nsrwsurasak | 0:a27e0d3581d1 | 395 | char buf[1024]; |
nsrwsurasak | 0:a27e0d3581d1 | 396 | int index = 0; |
Lucyjungz | 3:6e08d0bba1bb | 397 | |
Lucyjungz | 3:6e08d0bba1bb | 398 | /* Initialize return value */ |
nsrwsurasak | 0:a27e0d3581d1 | 399 | memset(m_varList, ' ', sizeof(m_varList)); |
Lucyjungz | 3:6e08d0bba1bb | 400 | |
Lucyjungz | 3:6e08d0bba1bb | 401 | /* Read line from the file */ |
Lucyjungz | 3:6e08d0bba1bb | 402 | while (fgets(buf, sizeof(buf), fp) != NULL) |
Lucyjungz | 3:6e08d0bba1bb | 403 | { |
Lucyjungz | 3:6e08d0bba1bb | 404 | /* Check the TAG */ |
Lucyjungz | 3:6e08d0bba1bb | 405 | if (strstr (buf,VAR_NAME_TAG)) |
Lucyjungz | 3:6e08d0bba1bb | 406 | { |
Lucyjungz | 3:6e08d0bba1bb | 407 | /* Found variable TAG, populate it*/ |
Lucyjungz | 4:ec62bf823914 | 408 | FileManager_GetXmlText(buf , m_varList[index].varName); |
nsrwsurasak | 0:a27e0d3581d1 | 409 | |
Lucyjungz | 3:6e08d0bba1bb | 410 | } |
Lucyjungz | 3:6e08d0bba1bb | 411 | else if (strstr (buf,VAR_ADDR_TAG)) |
Lucyjungz | 3:6e08d0bba1bb | 412 | { |
Lucyjungz | 3:6e08d0bba1bb | 413 | /* Found variable address TAG, populate it*/ |
Lucyjungz | 4:ec62bf823914 | 414 | FileManager_GetXmlText(buf , m_varList[index].varAddress); |
Lucyjungz | 5:7c513eee7b2b | 415 | } |
Lucyjungz | 6:5bd75c0f607c | 416 | else if (strstr (buf,VAR_LSB1_TAG)) |
Lucyjungz | 6:5bd75c0f607c | 417 | { |
Lucyjungz | 6:5bd75c0f607c | 418 | /* Found variable LSB1 TAG, populate it*/ |
Lucyjungz | 6:5bd75c0f607c | 419 | FileManager_GetXmlText(buf , m_varList[index].varLSB1); |
Lucyjungz | 6:5bd75c0f607c | 420 | } |
Lucyjungz | 6:5bd75c0f607c | 421 | else if (strstr (buf,VAR_LSB2_TAG)) |
Lucyjungz | 5:7c513eee7b2b | 422 | { |
Lucyjungz | 6:5bd75c0f607c | 423 | /* Found variable LSB2 TAG, populate it*/ |
Lucyjungz | 6:5bd75c0f607c | 424 | FileManager_GetXmlText(buf , m_varList[index].varLSB2); |
Lucyjungz | 6:5bd75c0f607c | 425 | } |
Lucyjungz | 6:5bd75c0f607c | 426 | else if (strstr (buf,VAR_BITMASK_TAG)) |
Lucyjungz | 6:5bd75c0f607c | 427 | { |
Lucyjungz | 6:5bd75c0f607c | 428 | /* Found variable BitMask TAG, populate it*/ |
Lucyjungz | 6:5bd75c0f607c | 429 | FileManager_GetXmlText(buf , m_varList[index].varBitMask); |
Lucyjungz | 5:7c513eee7b2b | 430 | } |
Lucyjungz | 5:7c513eee7b2b | 431 | else if (strstr (buf,VAR_UNIT_TAG)) |
Lucyjungz | 5:7c513eee7b2b | 432 | { |
Lucyjungz | 5:7c513eee7b2b | 433 | /* Found variable unit TAG, populate it*/ |
Lucyjungz | 5:7c513eee7b2b | 434 | FileManager_GetXmlText(buf , m_varList[index].varUnit); |
nsrwsurasak | 0:a27e0d3581d1 | 435 | index++; |
nsrwsurasak | 0:a27e0d3581d1 | 436 | } |
nsrwsurasak | 0:a27e0d3581d1 | 437 | |
nsrwsurasak | 0:a27e0d3581d1 | 438 | } |
Lucyjungz | 3:6e08d0bba1bb | 439 | /* Close File */ |
Lucyjungz | 3:6e08d0bba1bb | 440 | fclose(fp); |
Lucyjungz | 3:6e08d0bba1bb | 441 | |
Lucyjungz | 3:6e08d0bba1bb | 442 | /* Populate amount */ |
nsrwsurasak | 0:a27e0d3581d1 | 443 | m_amountVarList = index; |
Lucyjungz | 3:6e08d0bba1bb | 444 | |
Lucyjungz | 3:6e08d0bba1bb | 445 | /* Return variable list */ |
nsrwsurasak | 0:a27e0d3581d1 | 446 | return m_varList; |
nsrwsurasak | 0:a27e0d3581d1 | 447 | } |
nsrwsurasak | 0:a27e0d3581d1 | 448 | } |
Lucyjungz | 3:6e08d0bba1bb | 449 | /** |
Lucyjungz | 3:6e08d0bba1bb | 450 | * @brief Function to amount of variable for data logging |
Lucyjungz | 3:6e08d0bba1bb | 451 | * @note Must be called after readVarFile |
Lucyjungz | 3:6e08d0bba1bb | 452 | * @param None |
Lucyjungz | 3:6e08d0bba1bb | 453 | * @retval amount of variable list |
Lucyjungz | 3:6e08d0bba1bb | 454 | */ |
Lucyjungz | 4:ec62bf823914 | 455 | int FileManager_GetAmountVarList() |
nsrwsurasak | 0:a27e0d3581d1 | 456 | { |
nsrwsurasak | 0:a27e0d3581d1 | 457 | return m_amountVarList; |
nsrwsurasak | 0:a27e0d3581d1 | 458 | } |
Lucyjungz | 3:6e08d0bba1bb | 459 | /** |
Lucyjungz | 3:6e08d0bba1bb | 460 | * @brief Function to get variable list |
Lucyjungz | 3:6e08d0bba1bb | 461 | * @note Must be called after readVarFile |
Lucyjungz | 3:6e08d0bba1bb | 462 | * @param None |
Lucyjungz | 3:6e08d0bba1bb | 463 | * @retval pointer to variable list |
Lucyjungz | 3:6e08d0bba1bb | 464 | */ |
Lucyjungz | 4:ec62bf823914 | 465 | Variable_Data_TypeDef * FileManager_GetVarList() |
nsrwsurasak | 0:a27e0d3581d1 | 466 | { |
nsrwsurasak | 0:a27e0d3581d1 | 467 | return m_varList; |
nsrwsurasak | 0:a27e0d3581d1 | 468 | } |
nsrwsurasak | 0:a27e0d3581d1 | 469 | |
nsrwsurasak | 0:a27e0d3581d1 | 470 |