![]() |
ATC
Advanced Trip Computer
|
00001 00006 #include <cstdio> 00007 #include <cstring> 00008 00009 #include "GPS.H" 00010 00011 extern "C" 00012 { 00013 #include "DES.H" 00014 } 00015 00016 #define SEC_PER_WEEK 3600*24*7 // secondi in una settimana 00017 #define PASSWORD_FILE "password.bin" 00018 #define POSITION_FILE "position.bin" 00019 00023 struct POSITION 00024 { 00025 unsigned int day; // giorno 00026 unsigned int month; // mese 00027 unsigned int year; // anno 00028 unsigned int hour; // ore 00029 unsigned int minute; // minuti 00030 unsigned int second; // secondi 00031 int lat_sign; // -1 => S, +1 => N 00032 unsigned int lat_degree; // latitudine: gradi 00033 unsigned int lat_minute; // latitudine: primi 00034 unsigned int lat_second; // latitudine: secondi 00035 int lon_sign; // -1 => W, +1 => E 00036 unsigned int lon_degree; // longitudine: gradi 00037 unsigned int lon_minute; // longitudine: primi 00038 unsigned int lon_second; // longitudine: secondi 00039 }; 00040 00044 class ATC : public GPSobserver 00045 { 00046 private: 00047 00050 bool trip; 00053 unsigned int positionCounter; 00056 double actualDirection; 00059 double actualSpeed; 00062 Position* positions; 00065 FILE* positionFile; 00066 00072 bool checkPassword(char password[]) 00073 { 00074 BYTE clear[8], coded[8], pswd[8]; 00075 int passwordLen = strlen(password); 00076 FILE* passwordFile; 00077 00078 // preparazione password (aggiustamento per lunghezza minore/maggiore 8 caratteri) 00079 if (passwordLen<=8) 00080 { 00081 for (int i=0; i<passwordLen; i++) 00082 clear[i] = password[i]; 00083 for (int i=passwordLen; i<8; i++) 00084 clear[i] = ' '; 00085 } 00086 else 00087 { 00088 for (int i=0; i<8; i++) 00089 clear[i] = password[i]; 00090 } 00091 // codifica password 00092 DES_encrypt(clear, coded, clear); 00093 // recupero file codificata da file 00094 passwordFile = fopen(PASSWORD_FILE,"rb"); 00095 fread(pswd, 8, 1, passwordFile); 00096 fclose(passwordFile); 00097 // confronto password 00098 for (int i=0; i<8; i++) 00099 if (coded[i] != pswd[i]) 00100 return false; 00101 return true; 00102 } 00109 double getTotalDistance(void) 00110 { 00111 double distance = 0; 00112 00113 for (unsigned int i=1; i<positionCounter; i++) 00114 distance += positions[i].distance(positions[i-1]); 00115 00116 return distance/1000.0; 00117 } 00123 double getElapsedTime(void) 00124 { 00125 int time; 00126 00127 if (positionCounter > 1) 00128 time = positions[0].elapsedTime(positions[positionCounter-1]); 00129 else 00130 time = 0; 00131 00132 return (double)time/3600.0; 00133 } 00139 int loadFromFile(const char filename[]) 00140 { 00141 POSITION pos; 00142 00143 positionFile = fopen(filename, "rb"); 00144 if (positionFile != NULL) 00145 { 00146 positionCounter = 0; 00147 while (!feof(positionFile)) 00148 { 00149 if (fread(&pos,sizeof(POSITION),1,positionFile) == 1) 00150 { 00151 Date date(pos.day, pos.month, pos.year); 00152 Time time(pos.hour, pos.minute, pos.second); 00153 Latitude lat(pos.lat_degree, pos.lat_minute, pos.lat_second, pos.lat_sign); 00154 Longitude lon(pos.lon_degree, pos.lon_minute, pos.lon_second, pos.lon_sign); 00155 Position position(date, time, lat, lon); 00156 positions[positionCounter] = position; 00157 positionCounter++; 00158 }; 00159 } 00160 } 00161 else 00162 return-1; 00163 fclose(positionFile); 00164 return positionCounter; 00165 } 00166 00167 00168 public: 00169 00175 ATC(void) 00176 { 00177 trip = false; 00178 positionCounter = 0; 00179 actualDirection = 0.0; 00180 actualSpeed = 0.0; 00181 positions = new Position[SEC_PER_WEEK]; 00182 if (loadFromFile(POSITION_FILE) >= 0) 00183 trip = true; 00184 } 00189 ~ATC(void) 00190 { 00191 delete positions; 00192 } 00198 bool isTrip(void) 00199 { 00200 return trip; 00201 } 00207 void beginJourney(char password[]) 00208 { 00209 BYTE clear[8], coded[8]; 00210 int passwordLen = strlen(password); 00211 FILE *passwordFile; 00212 00213 if (trip) // viaggio in corso 00214 return; 00215 00216 // preparazione password (aggiustamento per lunghezza minore/maggiore 8 caratteri) 00217 if (passwordLen<=8) 00218 { 00219 for (int i=0; i<passwordLen; i++) 00220 clear[i] = password[i]; 00221 for (int i=passwordLen; i<8; i++) 00222 clear[i] = ' '; 00223 } 00224 else 00225 { 00226 for (int i=0; i<8; i++) 00227 clear[i] = password[i]; 00228 } 00229 // codifica password 00230 DES_encrypt(clear, coded, clear); 00231 // scrittura password codificata su file 00232 passwordFile = fopen(PASSWORD_FILE,"wb"); 00233 fwrite(coded, 8, 1, passwordFile); 00234 fclose(passwordFile); 00235 // rimozione file delle posizioni 00236 remove(POSITION_FILE); 00237 positionCounter = 0; 00238 trip = true; 00239 } 00246 void endJourney(char password[]) 00247 { 00248 if (trip == false) 00249 return; 00250 if (checkPassword(password)) 00251 { 00252 remove(POSITION_FILE); 00253 remove(PASSWORD_FILE); 00254 positionCounter = 0; 00255 trip = false; 00256 } 00257 } 00265 void getJourneyInfo(double* totalDistance, double* elapsedTime, double* meanSpeed) 00266 { 00267 *totalDistance = getTotalDistance(); 00268 *elapsedTime = getElapsedTime(); 00269 if (*elapsedTime == 0.0) 00270 *meanSpeed = 0.0; 00271 else 00272 *meanSpeed = (*totalDistance)/(*elapsedTime); 00273 } 00283 Position getPastPosition(Date date, Time time, char password[]) 00284 { 00285 Latitude lat; 00286 Longitude lon; 00287 Position pos(date, time, lat, lon); // posizione fittizia con data/ora della richiesta 00288 00289 if (positionCounter == 0) // viaggio non in corso 00290 return pos; 00291 00292 if (checkPassword(password)) 00293 { 00294 if (positions[0].elapsedTime(pos)<0) // data/ora indicata precedente al momento della partenza 00295 return positions[0]; // prima posizione registrata 00296 for (unsigned int i=0; i<positionCounter; i++) // ricerca posizione immediatamente successiva alla data/ora richiesta 00297 if (positions[i].elapsedTime(pos)<=0) 00298 return positions[i]; 00299 return positions[positionCounter-1]; // ultima posizione registrata 00300 } 00301 else 00302 return pos; 00303 } 00312 Position getActualPosition(double* speed, double* direction) 00313 { 00314 Latitude lat; 00315 Longitude lon; 00316 Date date; 00317 Time time; 00318 Position pos(date, time, lat, lon); // posizione fittizia con data/ora fittizie 00319 00320 *speed = actualSpeed; 00321 *direction = actualDirection; 00322 if (positionCounter == 0) // viaggio non in corso 00323 return pos; 00324 00325 return positions[positionCounter-1]; 00326 } 00333 void saveToFile(char filename[], char password[]) 00334 { 00335 FILE* positionFile; 00336 char string[128], tmp[8]; 00337 unsigned char crc; 00338 00339 if (trip == false) 00340 return; 00341 if (!checkPassword(password)) 00342 return; 00343 00344 positionFile = fopen(filename,"wt"); 00345 for (unsigned int i=0; i<positionCounter; i++) 00346 { 00347 sprintf(string,"$GPRMC,%02i%02i%02i.000,A,%02i%02.4f,%c,%03i%02.4f,%c,0.00,000.00,%02i%02i%02i*", 00348 positions[i].getTime().getHour(), positions[i].getTime().getMinute(), positions[i].getTime().getSecond(), 00349 positions[i].getLatitude().getDegree(),(double)positions[i].getLatitude().getMinute()+(double)positions[i].getLatitude().getSecond()/60.0,(positions[i].getLatitude().getOrientation()>0 ? 'N' : 'S'), 00350 positions[i].getLongitude().getDegree(),(double)positions[i].getLongitude().getMinute()+(double)positions[i].getLongitude().getSecond()/60.0,(positions[i].getLongitude().getOrientation()>0 ? 'E' : 'W'), 00351 positions[i].getDate().getDay(), positions[i].getDate().getMonth(), positions[i].getDate().getYear()-2000); 00352 crc = 0x00; 00353 for (unsigned int i=1; string[i]!='*'; i++) 00354 crc ^= string[i]; 00355 sprintf(tmp,"%02X",crc); 00356 fprintf(positionFile,"%s%s\r\n",string,tmp); 00357 } 00358 fclose(positionFile); 00359 } 00367 void GPSnotify(Position position, double direction, double speed) 00368 { 00369 POSITION pos; 00370 00371 if (positionCounter < SEC_PER_WEEK) 00372 { 00373 // inserimento posizione nel vettore delle posizioni e aggiornamento del contatore 00374 positions[positionCounter] = position; 00375 positionCounter++; 00376 // scrittura posizione su file 00377 pos.day = position.getDate().getDay(); 00378 pos.month = position.getDate().getMonth(); 00379 pos.year = position.getDate().getYear(); 00380 pos.hour = position.getTime().getHour(); 00381 pos.minute = position.getTime().getMinute(); 00382 pos.second = position.getTime().getSecond(); 00383 pos.lat_sign = position.getLatitude().getOrientation(); 00384 pos.lat_degree = position.getLatitude().getDegree(); 00385 pos.lat_minute = position.getLatitude().getMinute(); 00386 pos.lat_second = position.getLatitude().getSecond(); 00387 pos.lon_sign = position.getLongitude().getOrientation(); 00388 pos.lon_degree = position.getLongitude().getDegree(); 00389 pos.lon_minute = position.getLongitude().getMinute(); 00390 pos.lon_second = position.getLongitude().getSecond(); 00391 positionFile = fopen(POSITION_FILE, "ab"); 00392 fwrite(&pos, sizeof(pos), 1, positionFile); 00393 fclose(positionFile); 00394 } 00395 actualDirection = direction; 00396 actualSpeed = speed; 00397 } 00398 };