/** * @brief In the main function the connection to mySQL is created, the serial port is being initialized. * The data will be readed from serial port until a key was pressed. \n Every message will be parsed into array, * which will be used for creating of a SQL-statement. The SQL-command is gonna be executed. \n Finally the serial port * and the SQL-connection is getting closed. */ int main(int argc, CHAR* argv[]) { BOOL rc; CHAR msgTextBuf[1024]; DWORD bytesRead; // NumberOfBytesRead DWORD start, end, result; int i, j; DWORD dwRead; BOOL fWaitingOnRead = FALSE; OVERLAPPED osReader = {0}; char mce_table[] = "values_realtime"; short *shortMsg; rc = MySQLConnect(); // connect to mySQL if (rc == FALSE) { printf("Error with MySQLConnect(). Program exits. \n"); return -1; } rc = initSerial(COMPORT); // If the function succeeds, the return value is nonzero. if (rc == FALSE) { printf("Error while initSerial(). Program exits. \n"); return -1; } cleanBuffer(msgTextBuf); while (TRUE) { // reads data on port and store it on mySQL server. If succeeds rc is TRUE. rc = ReadFile(comPort, msgTextBuf, sizeof(msgTextBuf), &bytesRead, NULL); if (rc != FALSE && bytesRead != 0) { // parseMessage(msgTextBuf); shortMsg = parseMsgToShort(msgTextBuf, bytesRead); rc = updateMMData(mce_table, shortMsg); // rc = insertMMData(mce_table, shortMsg); if (rc == FALSE) { printf("Error while insertSQLData(). Program will continue. \n"); } else { printf("MM-data successfull updated. \n"); } // printf("%s \n", msgTextBuf); } } rc = CloseHandle(comPort); // If succeeds, rc is TRUE. if (rc == FALSE) { printf("Error while CloseHandle(). Program exits. \n"); return -1; } mysql_close(g_pMySQL); mysql_library_end(); printf("\n\nEnd of program.."); return 0; }
/** * ============================================================================ * Funktion: getUser * Input: char cNickname[], char cPassword[] * Output: struct ACCOUNT * * Beschreibung: Holt Informationen zu einem Nutzer aus der Datenbank. * ============================================================================ **/ ACCOUNT getUser(char cNickname[], char cPassword[]) { int arraysize=0; char cQuery[300]; MYSQL_ROW ROW; ACCOUNT user; MYSQL_RES *userEntity=NULL; MYSQL *Connection = NULL; Connection = MySQLConnect (); sprintf(cQuery, "SELECT id, first_name, last_name, username FROM accounts WHERE username ='******' AND password=MD5('%s');", cNickname, cPassword ); userEntity = QueryBuilder (Connection, cQuery); if(userEntity ) { ROW = mysql_fetch_row( userEntity ); if( ROW ) { arraysize = sizeof(ROW) / sizeof(char); if( arraysize == 4 ) { user.ID = atoi( ROW[0] ); strcpy(user.FirstName, ROW[1]); strcpy(user.LastName, ROW[2]); strcpy(user.UserName, ROW[3]); } else { DEBUG_Log("Database didnt select [4] rows."); } } } mysql_free_result(userEntity); MySQLClose (Connection); return user; }
/** * ============================================================================ * Funktion: authentificationStatus * Input: char cNickname[], char cPassword[] * Output: 0 (= Nutzer nicht gefunden) * 1 (= Passwort ungueltig) * 2 (= Erfolgreicher login) * * Beschreibung: Prueft Logindaten gegen die Datenbank und liefert einen * entsprechenden Status zurueck. * ============================================================================ **/ int authentificationStatus( char cNickname[], char cPassword[] ) { int num_fields=0,i=0; char cQuery[300]; MYSQL_RES *username=NULL, *passwordcheck=NULL; MYSQL *Connection = NULL; MYSQL_ROW ROW = NULL; Connection = MySQLConnect (); /* Query fuer Existenzpruefung */ sprintf(cQuery, "SELECT id FROM accounts WHERE username = '******';", cNickname ); username = QueryBuilder (Connection, cQuery); if( username ) /* Wenn Query erfolgreich */ { num_fields = mysql_num_rows(username); if(num_fields == 0) /* und Ergebnismenge 0 Zeilen lang */ { mysql_free_result(username); MySQLClose (Connection); /* dann wurde der Nutzer nicht gefunden; 0 zurueckgeben */ return 0; } num_fields = mysql_num_fields(username); /* Query zur Passwortpruefung vorbereiten */ sprintf( cQuery, "SELECT id FROM accounts " "WHERE username ='******' AND password=MD5('%s');", cNickname, cPassword ); num_fields = 0; /* num_fields des letzten Querys resetten */ passwordcheck = QueryBuilder(Connection, cQuery); if( passwordcheck ) /* Wenn Passwort-Query erfolgreich */ { num_fields = mysql_num_rows(passwordcheck); if(num_fields == 0) /* und Ergebnismenge 0 Zeilen lang */ { mysql_free_result(passwordcheck); MySQLClose (Connection); /* dann ist das Passwort nicht korrekt; 1 zurueckgeben */ return 1; } else { mysql_free_result(passwordcheck); MySQLClose (Connection); /* Nutzer und Passwort korrekt; 2 zurueckgeben */ return 2; } /* endif num_fields */ } /* endif passwortcheck */ } /* endif username */ MySQLClose (Connection); return 0; }
/* ============================================================================ * Funktion: toplist * Input: - * Output: - * * Beschreibung: Holt die Topliste aus der Datenbank und gibt sie aus. * ============================================================================= */ void toplist(ACCOUNT user) { int num_fields = 0, iStatus = 0, iTemp = 1, iLength = 0, i = 0, m_Exit = 0; int x = 0, xpos=0, ypos=0, page=1, j[3]; char *cMenu[SUDOKU_DIFFICULTIES] = { "Easy", "Medium", "Hard", "Exit" }; char userList[SUDOKU_DIFFICULTIES][100][3][100]; char highLight[2], cQuery[300], cString[200], coods[20], number[10]; MYSQL_ROW ROW; MYSQL_RES *topList[3] = {NULL, NULL, NULL}; MYSQL *Connection = NULL; sprintf(highLight, "%c", 16); system("cls"); createBlockView(WINDOW_WIDTH, 37); createBlockView(WINDOW_WIDTH, 10); printTo(((WINDOW_WIDTH-13)/2), 40, "T O P L I S T", 0); printTo(((WINDOW_WIDTH-13)/2), 41, "= = = = = = =", 0); for (i = 0; i < SUDOKU_DIFFICULTIES; i++) { printTo(((WINDOW_WIDTH-13)/2), 43+ i, cMenu[i], 0); } printTo(((WINDOW_WIDTH-13)/2)-5, 43, highLight, 0 ); Connection = MySQLConnect(); x=0; for(i=0; i<SUDOKU_DIFFICULTIES; i++) { x=0; sprintf(cQuery, "SELECT a.username, p.points, p.time FROM accounts a JOIN points p ON (a.id = p.userid) WHERE difficulty = %i ORDER BY p.points DESC, p.time ASC LIMIT 10", i+1 ); topList[i] = QueryBuilder(Connection, cQuery); if(topList[i]) { while (ROW = mysql_fetch_row(topList[i])) { strcpy(userList[i][x][0], ROW[0]); strcpy(userList[i][x][1], ROW[1]); strcpy(userList[i][x][2], ROW[2]); x++; } } j[i] = x; mysql_free_result(topList[i]); } MySQLClose(Connection); renderToplist(WINDOW_WIDTH); createTableHead(WINDOW_WIDTH); if(j[iTemp-1] > 10 ) { iLength = (page*10); } else { iLength = j[iTemp-1]; } for(i=0; i<iLength;i++) { _itoa(i+1,number,10); createTableRow(number, userList[iTemp-1][i][0], userList[iTemp-1][i][1], userList[iTemp-1][i][2], (i*2)+5+2, WINDOW_WIDTH); } closingRow((i*2)+6, WINDOW_WIDTH); gotoxy(WINDOW_WIDTH - 1, 0); do { do { iStatus = control(); printTo(((WINDOW_WIDTH-13)/2)-5, 42+ iTemp, " ", 0); if (iStatus == 2) { if (iTemp > 1) { iTemp--; } } else if (iStatus == 3) { if (iTemp < SUDOKU_DIFFICULTIES && iTemp > 0) { iTemp++; } } xpos = ((WINDOW_WIDTH - 13) / 2) - 5; ypos = ((WINDOW_HEIGHT / 3) * 2) + iTemp; sprintf(coods,""); printTo(((WINDOW_WIDTH-13)/2)-5, 42+ iTemp, highLight, 0); gotoxy(WINDOW_WIDTH - 1, 0); } while (iStatus != 6); switch (iTemp) { case 1: case 2: case 3: renderToplist(WINDOW_WIDTH); if(j[iTemp-1]>10) { iLength = (page*10); } else { iLength = j[iTemp-1]; } createTableHead(WINDOW_WIDTH); for(i=0; i<iLength; i++) { _itoa(i+1,number,10); createTableRow(number, userList[iTemp-1][i][0], userList[iTemp-1][i][1], userList[iTemp-1][i][2], (i*2)+5+2, WINDOW_WIDTH); } closingRow((i*2)+6, WINDOW_WIDTH); gotoxy(WINDOW_WIDTH - 1, 0); break; case 4: m_Exit = 1; break; } } while (!m_Exit); }
/* ============================================================================ * Funktion: registrierung * Input: - * Output: - * * Beschreibung: Logik fuer komplette Registrierung eines Users * ============================================================================= */ void registrierung(void) { MYSQL_RES *result = NULL, *userresult = NULL; char cVorname[25], cNachname[25], cNickname[20], cPasswort[28]; char cQuery[300], c; MYSQL *Connection = MySQLConnect (); int iSuccess=0, num_fields=0; /* Fenstergroesse anpassen */ resizeWindow(50, 27); system("cls"); /* Ausgabe fuer Registrierungsinfos */ printTo(18,2,"R E G I S T E R", 0); printTo(18,3,"= = = = = = = =", 0); printBox(7, "First name:"); printBox(12, "Last name:"); printBox(17, "Username:"******"Password:"******"%s", &cVorname); fflush(stdin); gotoxy(24, 13); scanf("%s", &cNachname); fflush(stdin); gotoxy(24, 18); scanf("%s", &cNickname); fflush(stdin); gotoxy(24, 23); GetPassword(cPasswort); sprintf( cQuery, "SELECT id FROM accounts WHERE username = '******';", cNickname ); /* SQL Anfrage schicken */ userresult = QueryBuilder (Connection, cQuery); if( userresult ) /* Wenn Query erfolgreich */ { num_fields = mysql_num_rows(userresult); if(num_fields == 0) /* und Ergebnismenge 0 Zeilen lang */ { iSuccess=1; } mysql_free_result(userresult); } if(iSuccess) { /* Query festlegen */ sprintf( cQuery, "INSERT INTO accounts VALUES (NULL, '%s', '%s', '%s', MD5('%s'), %i, %i)", cVorname, cNachname, cNickname, cPasswort, WINDOW_WIDTH, WINDOW_HEIGHT ); /* SQL Anfrage schicken */ result = QueryBuilder (Connection, cQuery); /* Speicher freigeben und Verbindung beenden */ mysql_free_result(result); /* Ausgabe bei Erfolg */ system("cls"); printf("\nThank you for your registration.\n"); printf("Your account has been created.\n\n\n\n\n\n\n"); printf("Please press a key to return."); } else { system("cls"); printTo(0,0,"Error! The username is already token.\nReturning to the main menue.", 12); } MySQLClose (Connection); getchar(); return; }