File manager
FileManager.cpp@1:1f1f2b99756b, 2016-05-10 (annotated)
- Committer:
- Lucyjungz
- Date:
- Tue May 10 09:22:27 2016 +0000
- Revision:
- 1:1f1f2b99756b
- Parent:
- 0:a27e0d3581d1
- Child:
- 2:18e004a47f52
Support file name with Date, RTL File
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 | |
nsrwsurasak | 0:a27e0d3581d1 | 5 | char m_GpsInterval[XMLTEXT_SIZE]; |
nsrwsurasak | 0:a27e0d3581d1 | 6 | char m_DataInterval[XMLTEXT_SIZE]; |
nsrwsurasak | 0:a27e0d3581d1 | 7 | Variable_Data_TypeDef m_varList[MAX_VAR]; |
Lucyjungz | 1:1f1f2b99756b | 8 | unsigned int m_amountVarList = 0; |
nsrwsurasak | 0:a27e0d3581d1 | 9 | |
nsrwsurasak | 0:a27e0d3581d1 | 10 | static void removeSpaces(char* s , int size); |
nsrwsurasak | 0:a27e0d3581d1 | 11 | static void getXmlText(char *str, char *ret); |
Lucyjungz | 1:1f1f2b99756b | 12 | static void generateFileNameWithTime(time_t timestamp, char * file_name); |
nsrwsurasak | 0:a27e0d3581d1 | 13 | |
nsrwsurasak | 0:a27e0d3581d1 | 14 | static void removeSpaces(char* s , int size) |
nsrwsurasak | 0:a27e0d3581d1 | 15 | { |
nsrwsurasak | 0:a27e0d3581d1 | 16 | char* cpy = s; // an alias to iterate through s without moving s |
nsrwsurasak | 0:a27e0d3581d1 | 17 | char* temp = s; |
nsrwsurasak | 0:a27e0d3581d1 | 18 | |
nsrwsurasak | 0:a27e0d3581d1 | 19 | for (int i = 0 ; i < size ; i++) { |
nsrwsurasak | 0:a27e0d3581d1 | 20 | if (*cpy != ' ') |
nsrwsurasak | 0:a27e0d3581d1 | 21 | *temp++ = *cpy; |
nsrwsurasak | 0:a27e0d3581d1 | 22 | cpy++; |
nsrwsurasak | 0:a27e0d3581d1 | 23 | } |
nsrwsurasak | 0:a27e0d3581d1 | 24 | *temp = 0; |
nsrwsurasak | 0:a27e0d3581d1 | 25 | return; |
nsrwsurasak | 0:a27e0d3581d1 | 26 | } |
nsrwsurasak | 0:a27e0d3581d1 | 27 | static void getXmlText(char *str, char *ret) |
nsrwsurasak | 0:a27e0d3581d1 | 28 | { |
nsrwsurasak | 0:a27e0d3581d1 | 29 | int size = strlen(str); |
nsrwsurasak | 0:a27e0d3581d1 | 30 | int i; |
nsrwsurasak | 0:a27e0d3581d1 | 31 | bool begin_text = false; |
nsrwsurasak | 0:a27e0d3581d1 | 32 | char * ret_addr = ret; |
nsrwsurasak | 0:a27e0d3581d1 | 33 | memset (ret,' ',XMLTEXT_SIZE); |
nsrwsurasak | 0:a27e0d3581d1 | 34 | |
nsrwsurasak | 0:a27e0d3581d1 | 35 | for(i = 0; i < size ; i++) { |
nsrwsurasak | 0:a27e0d3581d1 | 36 | |
nsrwsurasak | 0:a27e0d3581d1 | 37 | if (*str == '>') { |
nsrwsurasak | 0:a27e0d3581d1 | 38 | begin_text = true; |
nsrwsurasak | 0:a27e0d3581d1 | 39 | } else if (begin_text && *str == '<') { |
nsrwsurasak | 0:a27e0d3581d1 | 40 | begin_text = false; |
nsrwsurasak | 0:a27e0d3581d1 | 41 | break; |
nsrwsurasak | 0:a27e0d3581d1 | 42 | } else if (begin_text && *str != ' ') { |
nsrwsurasak | 0:a27e0d3581d1 | 43 | *ret = *str; |
nsrwsurasak | 0:a27e0d3581d1 | 44 | ret++; |
nsrwsurasak | 0:a27e0d3581d1 | 45 | } |
nsrwsurasak | 0:a27e0d3581d1 | 46 | |
nsrwsurasak | 0:a27e0d3581d1 | 47 | str++; |
nsrwsurasak | 0:a27e0d3581d1 | 48 | } |
nsrwsurasak | 0:a27e0d3581d1 | 49 | removeSpaces(ret_addr, XMLTEXT_SIZE); |
nsrwsurasak | 0:a27e0d3581d1 | 50 | } |
Lucyjungz | 1:1f1f2b99756b | 51 | static void generateFileNameWithTime(time_t timestamp, char * file_name) |
Lucyjungz | 1:1f1f2b99756b | 52 | { |
Lucyjungz | 1:1f1f2b99756b | 53 | char str[5]; |
Lucyjungz | 1:1f1f2b99756b | 54 | struct tm * ptm; |
Lucyjungz | 1:1f1f2b99756b | 55 | |
Lucyjungz | 1:1f1f2b99756b | 56 | ptm = localtime ( ×tamp ); |
Lucyjungz | 1:1f1f2b99756b | 57 | sprintf(str,"%04d", ptm->tm_year-100 + 2000); |
Lucyjungz | 1:1f1f2b99756b | 58 | memcpy(&file_name[4], str, 4); |
Lucyjungz | 1:1f1f2b99756b | 59 | |
Lucyjungz | 1:1f1f2b99756b | 60 | sprintf(str,"%02d", ptm->tm_mon+1); |
Lucyjungz | 1:1f1f2b99756b | 61 | memcpy(&file_name[9], str, 2); |
Lucyjungz | 1:1f1f2b99756b | 62 | |
Lucyjungz | 1:1f1f2b99756b | 63 | sprintf(str,"%02d", ptm->tm_mday); |
Lucyjungz | 1:1f1f2b99756b | 64 | memcpy(&file_name[12], str, 2); |
Lucyjungz | 1:1f1f2b99756b | 65 | } |
Lucyjungz | 1:1f1f2b99756b | 66 | |
nsrwsurasak | 0:a27e0d3581d1 | 67 | void readSetupFile() |
nsrwsurasak | 0:a27e0d3581d1 | 68 | { |
nsrwsurasak | 0:a27e0d3581d1 | 69 | // now open file for reading |
nsrwsurasak | 0:a27e0d3581d1 | 70 | FILE *fp = fopen(SETUP_FILE_NAME, "r"); |
nsrwsurasak | 0:a27e0d3581d1 | 71 | |
nsrwsurasak | 0:a27e0d3581d1 | 72 | if (fp == NULL) { // if it can't open the file then print error message |
nsrwsurasak | 0:a27e0d3581d1 | 73 | printf("\nError! Unable to open file! %s \n", SETUP_FILE_NAME); |
nsrwsurasak | 0:a27e0d3581d1 | 74 | } else { // opened file so can write |
nsrwsurasak | 0:a27e0d3581d1 | 75 | // fscanf(fp, "%d",&stored_top_score); // ensure data type matches - note address operator (&) |
nsrwsurasak | 0:a27e0d3581d1 | 76 | // serial.printf("Read %d from file.\n",stored_top_score); |
nsrwsurasak | 0:a27e0d3581d1 | 77 | |
nsrwsurasak | 0:a27e0d3581d1 | 78 | ReadingFileState state = STATE_FINDING; |
nsrwsurasak | 0:a27e0d3581d1 | 79 | char buf[1024]; |
nsrwsurasak | 0:a27e0d3581d1 | 80 | while (fgets(buf, sizeof(buf), fp) != NULL) { |
nsrwsurasak | 0:a27e0d3581d1 | 81 | if (strstr (buf,DATA_TAG)) { |
nsrwsurasak | 0:a27e0d3581d1 | 82 | state = STATE_FOUND_DATA; |
nsrwsurasak | 0:a27e0d3581d1 | 83 | } else if (strstr (buf,GPS_TAG)) { |
nsrwsurasak | 0:a27e0d3581d1 | 84 | state = STATE_FOUND_GPS; |
nsrwsurasak | 0:a27e0d3581d1 | 85 | } else if (strstr (buf,UPDATE_INTERVAL_TAG)) { |
nsrwsurasak | 0:a27e0d3581d1 | 86 | if (state == STATE_FOUND_GPS) { |
nsrwsurasak | 0:a27e0d3581d1 | 87 | getXmlText(buf, m_GpsInterval); |
nsrwsurasak | 0:a27e0d3581d1 | 88 | printf("\r\n-found GPS interval %s ", m_GpsInterval); |
nsrwsurasak | 0:a27e0d3581d1 | 89 | state = STATE_FINDING; |
nsrwsurasak | 0:a27e0d3581d1 | 90 | } else if(state == STATE_FOUND_DATA) { |
nsrwsurasak | 0:a27e0d3581d1 | 91 | getXmlText(buf, m_DataInterval); |
nsrwsurasak | 0:a27e0d3581d1 | 92 | printf("\r\n-found Data interval %s ", m_DataInterval); |
nsrwsurasak | 0:a27e0d3581d1 | 93 | state = STATE_FINDING; |
nsrwsurasak | 0:a27e0d3581d1 | 94 | } |
nsrwsurasak | 0:a27e0d3581d1 | 95 | } |
nsrwsurasak | 0:a27e0d3581d1 | 96 | } |
nsrwsurasak | 0:a27e0d3581d1 | 97 | fclose(fp); // ensure you close the file after reading |
nsrwsurasak | 0:a27e0d3581d1 | 98 | } |
nsrwsurasak | 0:a27e0d3581d1 | 99 | } |
Lucyjungz | 1:1f1f2b99756b | 100 | void logGPSData(time_t timestamp ,char lat[], char longti[]) |
nsrwsurasak | 0:a27e0d3581d1 | 101 | { |
Lucyjungz | 1:1f1f2b99756b | 102 | char file_name[] = GPS_LOG_FILE_NAME; |
Lucyjungz | 1:1f1f2b99756b | 103 | |
Lucyjungz | 1:1f1f2b99756b | 104 | generateFileNameWithTime(timestamp,file_name); |
Lucyjungz | 1:1f1f2b99756b | 105 | |
Lucyjungz | 1:1f1f2b99756b | 106 | FILE *fp = fopen(file_name, "a"); |
Lucyjungz | 1:1f1f2b99756b | 107 | |
Lucyjungz | 1:1f1f2b99756b | 108 | if (fp == NULL) { // if it can't open the file then print error message |
Lucyjungz | 1:1f1f2b99756b | 109 | printf("Error! Unable to open file %s!\n",file_name); |
Lucyjungz | 1:1f1f2b99756b | 110 | } else { // opened file so can write |
Lucyjungz | 1:1f1f2b99756b | 111 | printf("\r\n Writing to Gps Log File (%s)....",file_name); |
Lucyjungz | 1:1f1f2b99756b | 112 | |
Lucyjungz | 1:1f1f2b99756b | 113 | fprintf(fp, "%d,%s,%s\n",timestamp,lat,longti); // print formatted string to file (CSV) |
Lucyjungz | 1:1f1f2b99756b | 114 | |
Lucyjungz | 1:1f1f2b99756b | 115 | printf("Done"); |
Lucyjungz | 1:1f1f2b99756b | 116 | fclose(fp); // ensure you close the file after writing |
Lucyjungz | 1:1f1f2b99756b | 117 | } |
Lucyjungz | 1:1f1f2b99756b | 118 | } |
Lucyjungz | 1:1f1f2b99756b | 119 | void logRMSData(time_t timestamp ,float * var, int size) |
Lucyjungz | 1:1f1f2b99756b | 120 | { |
Lucyjungz | 1:1f1f2b99756b | 121 | char file_name[] = RTL_LOG_FILE_NAME; |
Lucyjungz | 1:1f1f2b99756b | 122 | |
Lucyjungz | 1:1f1f2b99756b | 123 | generateFileNameWithTime(timestamp,file_name); |
Lucyjungz | 1:1f1f2b99756b | 124 | if (!is_file_exist(file_name)) |
Lucyjungz | 1:1f1f2b99756b | 125 | { |
Lucyjungz | 1:1f1f2b99756b | 126 | logRMSHeader(timestamp); |
Lucyjungz | 1:1f1f2b99756b | 127 | } |
Lucyjungz | 1:1f1f2b99756b | 128 | FILE *fp = fopen(file_name, "a"); |
nsrwsurasak | 0:a27e0d3581d1 | 129 | |
nsrwsurasak | 0:a27e0d3581d1 | 130 | if (fp == NULL) { // if it can't open the file then print error message |
Lucyjungz | 1:1f1f2b99756b | 131 | printf("Error! Unable to open file %s!\n",file_name); |
nsrwsurasak | 0:a27e0d3581d1 | 132 | } else { // opened file so can write |
Lucyjungz | 1:1f1f2b99756b | 133 | printf("\r\n Writing to Log File (%s)....",file_name); |
Lucyjungz | 1:1f1f2b99756b | 134 | |
Lucyjungz | 1:1f1f2b99756b | 135 | fprintf(fp, "%d",timestamp); // print Timestamp |
Lucyjungz | 1:1f1f2b99756b | 136 | |
Lucyjungz | 1:1f1f2b99756b | 137 | for(int i = 0; i < size; i++) |
Lucyjungz | 1:1f1f2b99756b | 138 | { |
Lucyjungz | 1:1f1f2b99756b | 139 | fprintf(fp, ",%f",var[i]); // print variable list |
Lucyjungz | 1:1f1f2b99756b | 140 | } |
Lucyjungz | 1:1f1f2b99756b | 141 | fprintf(fp, "\n"); // print new line |
nsrwsurasak | 0:a27e0d3581d1 | 142 | |
Lucyjungz | 1:1f1f2b99756b | 143 | printf("Done"); |
Lucyjungz | 1:1f1f2b99756b | 144 | fclose(fp); // ensure you close the file after writing |
Lucyjungz | 1:1f1f2b99756b | 145 | } |
Lucyjungz | 1:1f1f2b99756b | 146 | } |
Lucyjungz | 1:1f1f2b99756b | 147 | void logRMSHeader(time_t timestamp) |
Lucyjungz | 1:1f1f2b99756b | 148 | { |
Lucyjungz | 1:1f1f2b99756b | 149 | char file_name[] = RTL_LOG_FILE_NAME; |
Lucyjungz | 1:1f1f2b99756b | 150 | |
Lucyjungz | 1:1f1f2b99756b | 151 | generateFileNameWithTime(timestamp,file_name); |
Lucyjungz | 1:1f1f2b99756b | 152 | |
Lucyjungz | 1:1f1f2b99756b | 153 | |
Lucyjungz | 1:1f1f2b99756b | 154 | FILE *fp = fopen(file_name, "a"); |
Lucyjungz | 1:1f1f2b99756b | 155 | |
Lucyjungz | 1:1f1f2b99756b | 156 | if (fp == NULL) { // if it can't open the file then print error message |
Lucyjungz | 1:1f1f2b99756b | 157 | printf("Error! Unable to open file %s!\n",file_name); |
Lucyjungz | 1:1f1f2b99756b | 158 | } else { // opened file so can write |
Lucyjungz | 1:1f1f2b99756b | 159 | printf("\r\n Writing to Log File (%s)....",file_name); |
Lucyjungz | 1:1f1f2b99756b | 160 | |
Lucyjungz | 1:1f1f2b99756b | 161 | fprintf(fp, "%s",RMS_HEADER_TIME); // print variable list |
Lucyjungz | 1:1f1f2b99756b | 162 | for(int i = 0; i < m_amountVarList; i++) |
Lucyjungz | 1:1f1f2b99756b | 163 | { |
Lucyjungz | 1:1f1f2b99756b | 164 | fprintf(fp, ",%s",m_varList[i].varName); // print variable list |
Lucyjungz | 1:1f1f2b99756b | 165 | } |
Lucyjungz | 1:1f1f2b99756b | 166 | fprintf(fp, "\n"); // print new line |
nsrwsurasak | 0:a27e0d3581d1 | 167 | |
nsrwsurasak | 0:a27e0d3581d1 | 168 | printf("Done"); |
nsrwsurasak | 0:a27e0d3581d1 | 169 | fclose(fp); // ensure you close the file after writing |
nsrwsurasak | 0:a27e0d3581d1 | 170 | } |
nsrwsurasak | 0:a27e0d3581d1 | 171 | } |
nsrwsurasak | 0:a27e0d3581d1 | 172 | void logSystemData(float gps_interval) |
nsrwsurasak | 0:a27e0d3581d1 | 173 | { |
nsrwsurasak | 0:a27e0d3581d1 | 174 | FILE *fp = fopen(MINIRMS_LOG_FILE_NAME, "a"); |
nsrwsurasak | 0:a27e0d3581d1 | 175 | |
nsrwsurasak | 0:a27e0d3581d1 | 176 | if (fp == NULL) { // if it can't open the file then print error message |
nsrwsurasak | 0:a27e0d3581d1 | 177 | printf("Error! Unable to open file!\n"); |
nsrwsurasak | 0:a27e0d3581d1 | 178 | } else { // opened file so can write |
Lucyjungz | 1:1f1f2b99756b | 179 | fprintf(fp, "\nStart Mini-RMS System with Gps Interval = %f",gps_interval); // ensure data type matches |
nsrwsurasak | 0:a27e0d3581d1 | 180 | fclose(fp); // ensure you close the file after writing |
nsrwsurasak | 0:a27e0d3581d1 | 181 | } |
nsrwsurasak | 0:a27e0d3581d1 | 182 | } |
nsrwsurasak | 0:a27e0d3581d1 | 183 | void delete_file(char filename[]) |
nsrwsurasak | 0:a27e0d3581d1 | 184 | { |
nsrwsurasak | 0:a27e0d3581d1 | 185 | printf("Deleting file '%s'...",filename); |
nsrwsurasak | 0:a27e0d3581d1 | 186 | FILE *fp = fopen(filename, "r"); // try and open file |
nsrwsurasak | 0:a27e0d3581d1 | 187 | if (fp != NULL) { // if it does open... |
nsrwsurasak | 0:a27e0d3581d1 | 188 | fclose(fp); // close it |
nsrwsurasak | 0:a27e0d3581d1 | 189 | remove(filename); // and then delete |
nsrwsurasak | 0:a27e0d3581d1 | 190 | printf("Done!\n"); |
nsrwsurasak | 0:a27e0d3581d1 | 191 | } |
nsrwsurasak | 0:a27e0d3581d1 | 192 | // if we can't open it, it doesn't exist and so we can't delete it |
nsrwsurasak | 0:a27e0d3581d1 | 193 | } |
Lucyjungz | 1:1f1f2b99756b | 194 | bool is_file_exist(char filename[]) |
Lucyjungz | 1:1f1f2b99756b | 195 | { |
Lucyjungz | 1:1f1f2b99756b | 196 | bool exist = false; |
Lucyjungz | 1:1f1f2b99756b | 197 | FILE *fp = fopen(filename, "r"); // try and open file |
Lucyjungz | 1:1f1f2b99756b | 198 | if (fp != NULL) { // if it does open... |
Lucyjungz | 1:1f1f2b99756b | 199 | fclose(fp); // close it |
Lucyjungz | 1:1f1f2b99756b | 200 | exist = true; |
Lucyjungz | 1:1f1f2b99756b | 201 | } |
Lucyjungz | 1:1f1f2b99756b | 202 | |
Lucyjungz | 1:1f1f2b99756b | 203 | return exist; |
Lucyjungz | 1:1f1f2b99756b | 204 | } |
nsrwsurasak | 0:a27e0d3581d1 | 205 | int GPSInterval() |
nsrwsurasak | 0:a27e0d3581d1 | 206 | { |
nsrwsurasak | 0:a27e0d3581d1 | 207 | //Return whether or not CRC is enabled |
nsrwsurasak | 0:a27e0d3581d1 | 208 | return atoi( m_GpsInterval ); |
nsrwsurasak | 0:a27e0d3581d1 | 209 | } |
nsrwsurasak | 0:a27e0d3581d1 | 210 | int DataInterval() |
nsrwsurasak | 0:a27e0d3581d1 | 211 | { |
nsrwsurasak | 0:a27e0d3581d1 | 212 | //Return whether or not CRC is enabled |
nsrwsurasak | 0:a27e0d3581d1 | 213 | return atoi( m_DataInterval ); |
nsrwsurasak | 0:a27e0d3581d1 | 214 | } |
nsrwsurasak | 0:a27e0d3581d1 | 215 | |
nsrwsurasak | 0:a27e0d3581d1 | 216 | Variable_Data_TypeDef * readVarFile() |
nsrwsurasak | 0:a27e0d3581d1 | 217 | { |
nsrwsurasak | 0:a27e0d3581d1 | 218 | // now open file for reading |
nsrwsurasak | 0:a27e0d3581d1 | 219 | FILE *fp = fopen(VARIABLE_FILE_NAME, "r"); |
nsrwsurasak | 0:a27e0d3581d1 | 220 | |
nsrwsurasak | 0:a27e0d3581d1 | 221 | if (fp == NULL) { // if it can't open the file then print error message |
nsrwsurasak | 0:a27e0d3581d1 | 222 | printf("\nError! Unable to open file! %s \n", VARIABLE_FILE_NAME); |
nsrwsurasak | 0:a27e0d3581d1 | 223 | return NULL; |
nsrwsurasak | 0:a27e0d3581d1 | 224 | } else { // opened file so can write |
nsrwsurasak | 0:a27e0d3581d1 | 225 | |
nsrwsurasak | 0:a27e0d3581d1 | 226 | char buf[1024]; |
nsrwsurasak | 0:a27e0d3581d1 | 227 | int index = 0; |
nsrwsurasak | 0:a27e0d3581d1 | 228 | memset(m_varList, ' ', sizeof(m_varList)); |
nsrwsurasak | 0:a27e0d3581d1 | 229 | while (fgets(buf, sizeof(buf), fp) != NULL) { |
nsrwsurasak | 0:a27e0d3581d1 | 230 | if (strstr (buf,VAR_NAME_TAG)) { |
nsrwsurasak | 0:a27e0d3581d1 | 231 | getXmlText(buf , m_varList[index].varName); |
nsrwsurasak | 0:a27e0d3581d1 | 232 | |
nsrwsurasak | 0:a27e0d3581d1 | 233 | } else if (strstr (buf,VAR_ADDR_TAG)) { |
nsrwsurasak | 0:a27e0d3581d1 | 234 | getXmlText(buf , m_varList[index].varAddress); |
nsrwsurasak | 0:a27e0d3581d1 | 235 | index++; |
nsrwsurasak | 0:a27e0d3581d1 | 236 | } |
nsrwsurasak | 0:a27e0d3581d1 | 237 | |
nsrwsurasak | 0:a27e0d3581d1 | 238 | } |
nsrwsurasak | 0:a27e0d3581d1 | 239 | fclose(fp); // ensure you close the file after reading |
nsrwsurasak | 0:a27e0d3581d1 | 240 | m_amountVarList = index; |
nsrwsurasak | 0:a27e0d3581d1 | 241 | return m_varList; |
nsrwsurasak | 0:a27e0d3581d1 | 242 | } |
nsrwsurasak | 0:a27e0d3581d1 | 243 | } |
nsrwsurasak | 0:a27e0d3581d1 | 244 | int getAmountVarList() |
nsrwsurasak | 0:a27e0d3581d1 | 245 | { |
nsrwsurasak | 0:a27e0d3581d1 | 246 | return m_amountVarList; |
nsrwsurasak | 0:a27e0d3581d1 | 247 | } |
nsrwsurasak | 0:a27e0d3581d1 | 248 | Variable_Data_TypeDef * getVarList() |
nsrwsurasak | 0:a27e0d3581d1 | 249 | { |
nsrwsurasak | 0:a27e0d3581d1 | 250 | return m_varList; |
nsrwsurasak | 0:a27e0d3581d1 | 251 | } |
nsrwsurasak | 0:a27e0d3581d1 | 252 | |
nsrwsurasak | 0:a27e0d3581d1 | 253 |