Trung Nguyen
/
FINAL_PROJECT_4180
asdasdasd
Fork of FINAL_PROJECT_4180 by
Embed:
(wiki syntax)
Show/hide line numbers
lib.cpp
00001 #include "mbed.h" 00002 #include "lib.h" 00003 #include <cstring> 00004 00005 #define CMD_ENROLL 0x01 00006 #define CMD_AUTH 0x02 00007 #define FACE_FID 0x01 00008 #define SPEECH_FID 0x02 00009 #define VOICE_FID 0x03 00010 #define SUCCESS 0x55 00011 #define FAILURE 0x8A 00012 00013 int hashcode(const char *data, int size) 00014 { 00015 int hash = 17; 00016 for (int i = 0; i < size; i++) { 00017 hash += 31*hash + data[i]; 00018 } 00019 return hash; 00020 } 00021 void read_mag_card(Serial *device, char *dest, int *size) 00022 { 00023 int pos = 0; 00024 bool done = false; 00025 int markercount = 0; 00026 while (!done) { 00027 while (!device->readable()); 00028 dest[pos] = device->getc(); 00029 if (dest[pos] == '?') markercount++; 00030 if (markercount >= 2) done = true; 00031 pos++; 00032 } 00033 *size = pos - 1; 00034 } 00035 00036 bool fp_enroll(FPScanner *fp) 00037 { 00038 bool status = true; 00039 fp->SetLED(true); 00040 while(!fp->IsPressFinger()) wait(0.030); 00041 fp->CaptureFinger(true); 00042 if (fp->Identify1_N() == 200) { 00043 int id = fp->GetEnrollCount(); 00044 if (fp->EnrollStart(id) == 0) { 00045 fp->CaptureFinger(true); 00046 fp->Enroll1(); 00047 fp->CaptureFinger(true); 00048 fp->Enroll2(); 00049 fp->CaptureFinger(true); 00050 fp->Enroll3(); 00051 fp->SetLED(false); 00052 } else status = false; 00053 } else status = false; 00054 00055 return status; 00056 } 00057 00058 bool fp_auth(FPScanner *fp) 00059 { 00060 bool status = false; 00061 fp->SetLED(true); 00062 while(!fp->IsPressFinger()) wait(0.030); 00063 fp->CaptureFinger(true); 00064 if (fp->Identify1_N() != 200) status = true; 00065 fp->SetLED(false); 00066 return status; 00067 } 00068 00069 bool face_enroll(char *user_id, Serial *android) 00070 { 00071 char CMD[] = { 00072 0x0A, 00073 CMD_ENROLL, 00074 0x02, 00075 0x04, 00076 0x01, 00077 user_id[0], 00078 user_id[1], 00079 user_id[2], 00080 user_id[3], 00081 FACE_FID, 00082 '\0' 00083 }; 00084 00085 for (int i = 0; i < sizeof(CMD)/ sizeof(char); i++) { 00086 while(!android->writeable()); 00087 android->putc(CMD[i]); 00088 } 00089 00090 while (!android->readable()); 00091 char temp = android->getc(); 00092 if (temp == SUCCESS) return true; 00093 else return false; 00094 } 00095 00096 bool face_auth(char *user_id, Serial *android) 00097 { 00098 char CMD[] = { 00099 0x0A, 00100 CMD_AUTH, 00101 0x02, 00102 0x04, 00103 0x01, 00104 user_id[0], 00105 user_id[1], 00106 user_id[2], 00107 user_id[3], 00108 FACE_FID, 00109 '\0' 00110 }; 00111 00112 for (int i = 0; i < sizeof(CMD)/ sizeof(char); i++) { 00113 while(!android->writeable()); 00114 android->putc(CMD[i]); 00115 } 00116 00117 while (!android->readable()); 00118 char temp = android->getc(); 00119 if (temp == SUCCESS) return true; 00120 else return false; 00121 } 00122 00123 bool speech_enroll(char *user_id, Serial *android) 00124 { 00125 char CMD[] = { 00126 0x0A, 00127 CMD_ENROLL, 00128 0x02, 00129 0x04, 00130 0x01, 00131 user_id[0], 00132 user_id[1], 00133 user_id[2], 00134 user_id[3], 00135 SPEECH_FID, 00136 '\0' 00137 }; 00138 00139 for (int i = 0; i < sizeof(CMD)/ sizeof(char); i++) { 00140 while(!android->writeable()); 00141 android->putc(CMD[i]); 00142 } 00143 00144 while (!android->readable()); 00145 char temp = android->getc(); 00146 if (temp == SUCCESS) return true; 00147 else return false; 00148 } 00149 00150 bool speech_auth(char *user_id, Serial *android) 00151 { 00152 char CMD[] = { 00153 0x0A, 00154 CMD_AUTH, 00155 0x02, 00156 0x04, 00157 0x01, 00158 user_id[0], 00159 user_id[1], 00160 user_id[2], 00161 user_id[3], 00162 SPEECH_FID, 00163 '\0' 00164 }; 00165 00166 for (int i = 0; i < sizeof(CMD)/ sizeof(char); i++) { 00167 while(!android->writeable()); 00168 android->putc(CMD[i]); 00169 } 00170 00171 while (!android->readable()); 00172 char temp = android->getc(); 00173 if (temp == SUCCESS) return true; 00174 else return false; 00175 } 00176 00177 bool voice_enroll(char *user_id, Serial *android) 00178 { 00179 char CMD[] = { 00180 0x0A, 00181 CMD_ENROLL, 00182 0x02, 00183 0x04, 00184 0x01, 00185 user_id[0], 00186 user_id[1], 00187 user_id[2], 00188 user_id[3], 00189 VOICE_FID, 00190 '\0' 00191 }; 00192 00193 for (int i = 0; i < sizeof(CMD)/ sizeof(char); i++) { 00194 while(!android->writeable()); 00195 android->putc(CMD[i]); 00196 } 00197 00198 while (!android->readable()); 00199 char temp = android->getc(); 00200 if (temp == SUCCESS) return true; 00201 else return false; 00202 } 00203 00204 bool voice_auth(char *user_id, Serial *android) 00205 { 00206 char CMD[] = { 00207 0x0A, 00208 CMD_AUTH, 00209 0x02, 00210 0x04, 00211 0x01, 00212 user_id[0], 00213 user_id[1], 00214 user_id[2], 00215 user_id[3], 00216 VOICE_FID, 00217 '\0' 00218 }; 00219 00220 for (int i = 0; i < sizeof(CMD)/ sizeof(char); i++) { 00221 while(!android->writeable()); 00222 android->putc(CMD[i]); 00223 } 00224 00225 while (!android->readable()); 00226 char temp = android->getc(); 00227 if (temp == SUCCESS) return true; 00228 else return false; 00229 } 00230 00231 void buzzer(PwmOut *speaker, int seconds) 00232 { 00233 speaker->period(1.0/500.0); 00234 *speaker = 0.5; 00235 wait(seconds); 00236 *speaker = 0.0; 00237 } 00238 00239 void display_msg(Cryst_LCD *lcd, char *text) 00240 { 00241 lcd->cls(); 00242 lcd->locate(1, 0); 00243 lcd->printf("%s", text); 00244 } 00245 00246 void greeting(Cryst_LCD *lcd) 00247 { 00248 lcd->cls(); 00249 lcd->locate(0, 7); 00250 lcd->printf("WELCOME"); 00251 lcd->locate(3, 0); 00252 lcd->printf("*:AUTH"); 00253 lcd->locate(3, 12); 00254 lcd->printf("#:ENROLL"); 00255 } 00256 00257 bool prompt(const char* msg, Cryst_LCD *lcd, Keypad<4, 3>* pad) 00258 { 00259 bool res = false; 00260 char temp; 00261 lcd->cls(); 00262 lcd->locate(0,0); 00263 lcd->printf("%s", msg); 00264 lcd->locate(3, 0); 00265 lcd->printf("*:NO"); 00266 lcd->locate(3, 15); 00267 lcd->printf("#:YES"); 00268 while ((temp = pad->getKeyChar()) != '#' && temp != '*'); 00269 if (temp == '#') res = true; 00270 return res; 00271 } 00272 00273 bool is_valid_user(char *user_id) 00274 { 00275 char full_path[30]; 00276 std::strcpy(full_path, "/local/"); 00277 char filename[10]; 00278 std::memcpy(filename, user_id, 4); 00279 filename[4] = '\0'; 00280 std::strcat(filename, ".TXT"); 00281 std::strcat(full_path, filename); 00282 FILE *fp = fopen(full_path, "r"); 00283 if (fp == NULL)return false; 00284 else fclose(fp); 00285 return true; 00286 } 00287 00288 bool write_hash_to_file(char *user_id, int hash) 00289 { 00290 char full_path[30]; 00291 std::strcpy(full_path, "/local/"); 00292 char filename[10]; 00293 std::memcpy(filename, user_id, 4); 00294 filename[4] = '\0'; 00295 std::strcat(filename, ".TXT"); 00296 std::strcat(full_path, filename); 00297 FILE *fp = fopen(full_path, "w"); 00298 if (fp == NULL) return false; 00299 fprintf(fp, "%d", hash); 00300 fclose(fp); 00301 return true; 00302 } 00303 00304 bool read_hash_from_file(char *user_id, int *hash) 00305 { 00306 bool success_read = false; 00307 char full_path[30]; 00308 std::strcpy(full_path, "/local/"); 00309 char filename[10]; 00310 std::memcpy(filename, user_id, 4); 00311 filename[4] = '\0'; 00312 std::strcat(filename, ".TXT"); 00313 std::strcat(full_path, filename); 00314 FILE *fp = fopen(full_path, "r"); 00315 if (fp) { 00316 int t = fscanf(fp, "%d", hash); 00317 if (t != 0 && t != EOF) success_read = true; 00318 fclose(fp); 00319 } 00320 return success_read; 00321 } 00322 00323 bool read_pin(char *buffer, int length, Cryst_LCD* lcd, Keypad<4, 3>* pad) 00324 { 00325 bool res = false; 00326 int cur = 0; 00327 lcd->cls(); 00328 lcd->locate(3,0); 00329 lcd->printf("*:CLEAR"); 00330 lcd->locate(3, 13); 00331 lcd->printf("#:ENTER"); 00332 lcd->locate(0,0); 00333 lcd->printf("Enter 4-Digit PIN:\n"); 00334 lcd->cursor_on(); 00335 lcd->cursor_blink(); 00336 bool done = false; 00337 char temp_char; 00338 while (!done) { 00339 temp_char = pad->getKeyChar(); 00340 if (temp_char == '#') done = true; 00341 else if (temp_char == '*') { 00342 lcd->clear_line(); 00343 cur = 0; 00344 } else { 00345 if (cur < 19) lcd->printf("%C", '*'); 00346 if (cur < length) buffer[cur] = temp_char; 00347 cur++; 00348 } 00349 } 00350 if (cur == length) res = true; 00351 lcd->cursor_off(); 00352 lcd->cursor_no_blink(); 00353 return res; 00354 } 00355 00356 00357 bool fp_clearAll(FPScanner *fp) 00358 { 00359 bool status; 00360 fp->SetLED(true); 00361 status = fp->DeleteAll(); 00362 fp->SetLED(false); 00363 return status; 00364 } 00365 00366 char clear_serial_buffer(Serial *dev) 00367 { 00368 char temp; 00369 while (dev->readable()) { 00370 temp = dev->getc(); 00371 } 00372 return temp; 00373 }
Generated on Tue Jul 12 2022 17:17:35 by 1.7.2