2020-03-11 21:01:54 +00:00
|
|
|
//////////////////////////////////////////////////
|
|
|
|
// Bakalarska praca //
|
|
|
|
// Meno studenta: Tomas Lukac //
|
|
|
|
// Veduci BP: prof. Ing. Milos Drutarovsky CSc. //
|
|
|
|
// Skola: KEMT FEI TUKE //
|
2020-05-06 10:53:08 +00:00
|
|
|
// Datum poslednej upravy: 6.5.2020 //
|
2020-03-11 21:01:54 +00:00
|
|
|
//////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "komunikacia.h"
|
|
|
|
#include "kryptografia.h"
|
|
|
|
|
2020-04-10 17:04:14 +00:00
|
|
|
int poslat_subor(WOLFSSL* ssl, WOLFSSL_CTX* ctx, const char* cesta, nastavenia_aplikacie *nastavenia)
|
2020-03-11 21:01:54 +00:00
|
|
|
{
|
2020-03-12 16:22:08 +00:00
|
|
|
int uspech;
|
|
|
|
|
2020-04-16 09:25:05 +00:00
|
|
|
//odoslanie nazvu (resp. cesty) suboru
|
|
|
|
uspech = 0;
|
|
|
|
while(uspech < VELKOST_CESTY)
|
2020-03-12 16:22:08 +00:00
|
|
|
{
|
2020-04-16 09:25:05 +00:00
|
|
|
uspech = wolfSSL_write(ssl, cesta, VELKOST_CESTY);
|
|
|
|
if(uspech <= 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Nastala chyba pri posielani dat o subore.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2020-03-12 16:22:08 +00:00
|
|
|
}
|
2020-03-11 21:01:54 +00:00
|
|
|
|
2020-04-16 09:25:05 +00:00
|
|
|
//otvorenie suboru na citanie
|
|
|
|
FILE* subor = fopen(cesta, "rb");
|
2020-03-11 21:01:54 +00:00
|
|
|
if(subor == NULL)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Nebolo mozne najst pozadovany subor.\n");
|
|
|
|
ukoncit_spojenie(ssl, ctx);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("Posielam subor.\n");
|
|
|
|
|
|
|
|
//nastavi ukazovatel na koniec suboru
|
|
|
|
//a zisti velkost suboru
|
|
|
|
fseek(subor, 0, SEEK_END);
|
2020-04-16 09:25:05 +00:00
|
|
|
int velkost = (int)ftell(subor);
|
2020-03-12 16:22:08 +00:00
|
|
|
char velkost_suboru[VELKOST_SUBOR];
|
2020-03-11 21:01:54 +00:00
|
|
|
sprintf(velkost_suboru, "%ld", velkost);
|
|
|
|
printf("Velkost suboru: %s bajtov\n", velkost_suboru);
|
2020-04-16 09:25:05 +00:00
|
|
|
uspech = 0;
|
|
|
|
while(uspech < VELKOST_SUBOR)
|
|
|
|
{
|
|
|
|
uspech = wolfSSL_write(ssl, velkost_suboru, VELKOST_SUBOR);
|
|
|
|
if(uspech <= 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Nastala chyba pri posielani dat o velkosti suboru.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2020-03-11 21:01:54 +00:00
|
|
|
|
|
|
|
//nastavi ukazovatel na zaciatok suboru
|
|
|
|
//a nacita data zo suboru do pola
|
2020-04-16 09:25:05 +00:00
|
|
|
unsigned char* pole = calloc(velkost, sizeof(unsigned char));
|
2020-03-11 21:01:54 +00:00
|
|
|
fseek(subor, 0, SEEK_SET);
|
2020-04-16 09:25:05 +00:00
|
|
|
fread((unsigned char*)pole, 1, velkost, subor);
|
2020-03-11 21:01:54 +00:00
|
|
|
|
|
|
|
//posielanie jednotlivych bajtov
|
2020-04-16 09:25:05 +00:00
|
|
|
uspech = 0;
|
|
|
|
while(uspech < velkost)
|
2020-03-11 21:01:54 +00:00
|
|
|
{
|
2020-04-16 09:25:05 +00:00
|
|
|
uspech = wolfSSL_write(ssl, (unsigned char*)pole, velkost);
|
2020-03-12 16:22:08 +00:00
|
|
|
if(uspech <= 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Nastala chyba pri posielani suboru.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2020-03-11 21:01:54 +00:00
|
|
|
}
|
|
|
|
printf("Subor bol uspesne odoslany.\n");
|
2020-03-19 12:55:34 +00:00
|
|
|
|
|
|
|
//generovanie a poslanie kontrolneho suctu serveru pre kontrolu
|
|
|
|
byte* kontrolny_sucet;
|
|
|
|
int velkost_kontrolneho_suctu;
|
2020-04-16 09:25:05 +00:00
|
|
|
kontrolny_sucet = generovat_kontrolny_sucet_suboru(nastavenia, cesta, &velkost_kontrolneho_suctu);
|
|
|
|
|
|
|
|
uspech = 0;
|
|
|
|
while(uspech < velkost_kontrolneho_suctu)
|
2020-03-19 12:55:34 +00:00
|
|
|
{
|
2020-04-16 09:25:05 +00:00
|
|
|
uspech = wolfSSL_write(ssl, kontrolny_sucet, velkost_kontrolneho_suctu);
|
|
|
|
if(uspech <= 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Nastala chyba pri posielani kontrolneho suctu.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2020-03-19 12:55:34 +00:00
|
|
|
}
|
2020-04-16 09:25:05 +00:00
|
|
|
free(kontrolny_sucet);
|
|
|
|
fclose(subor);
|
|
|
|
free(pole);
|
2020-03-11 21:01:54 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-07 20:11:21 +00:00
|
|
|
int prijat_subor(WOLFSSL* ssl, WOLFSSL_CTX* ctx, nastavenia_aplikacie* nastavenia)
|
2020-03-11 21:01:54 +00:00
|
|
|
{
|
2020-03-12 16:22:08 +00:00
|
|
|
int uspech;
|
|
|
|
|
2020-04-16 09:25:05 +00:00
|
|
|
//prijem dat o subore
|
2020-03-12 16:22:08 +00:00
|
|
|
char* cesta = calloc(VELKOST_CESTY, sizeof(char));
|
2020-04-16 09:25:05 +00:00
|
|
|
char* velkost_suboru = calloc(VELKOST_SUBOR, sizeof(char));
|
|
|
|
|
|
|
|
uspech = 0;
|
|
|
|
while(uspech < VELKOST_CESTY)
|
2020-03-12 16:22:08 +00:00
|
|
|
{
|
2020-04-16 09:25:05 +00:00
|
|
|
uspech = wolfSSL_read(ssl, cesta, VELKOST_CESTY);
|
|
|
|
if(uspech <= 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Nastala chyba pri prijati dat o subore\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2020-03-12 16:22:08 +00:00
|
|
|
}
|
2020-03-11 21:01:54 +00:00
|
|
|
printf("Prebieha prijimanie suboru %s\n", cesta);
|
|
|
|
|
|
|
|
//ziskanie informacie od klienta o velkosti odoslaneho suboru
|
2020-04-16 09:25:05 +00:00
|
|
|
uspech = 0;
|
|
|
|
while(uspech < VELKOST_SUBOR)
|
2020-03-13 09:25:19 +00:00
|
|
|
{
|
2020-04-16 09:25:05 +00:00
|
|
|
uspech = wolfSSL_read(ssl, velkost_suboru, VELKOST_SUBOR);
|
|
|
|
if(uspech <= 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Nastala chyba pri prijati velkosti suboru\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2020-03-13 09:25:19 +00:00
|
|
|
}
|
2020-04-16 09:25:05 +00:00
|
|
|
|
|
|
|
int velkost = (int)atol(velkost_suboru);
|
2020-03-11 21:01:54 +00:00
|
|
|
if(velkost < 1)
|
|
|
|
{
|
|
|
|
printf("Nastala chyba pri prijati suboru\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("Velkost suboru: %s bajtov\n", velkost_suboru);
|
|
|
|
}
|
2020-03-12 16:22:08 +00:00
|
|
|
|
2020-03-11 21:01:54 +00:00
|
|
|
//prijem jednotlivych bajtov
|
2020-04-16 09:25:05 +00:00
|
|
|
unsigned char* pole = calloc(velkost, sizeof(unsigned char));
|
|
|
|
uspech = 0;
|
|
|
|
while(uspech < velkost)
|
2020-03-11 21:01:54 +00:00
|
|
|
{
|
2020-04-16 09:25:05 +00:00
|
|
|
uspech = wolfSSL_read(ssl, (unsigned char*)pole, velkost);
|
2020-03-12 16:22:08 +00:00
|
|
|
if(uspech <= 0)
|
|
|
|
{
|
2020-04-16 09:25:05 +00:00
|
|
|
fprintf(stderr, "Nastala chyba pri prijatii suboru.\n");
|
2020-03-12 16:22:08 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2020-03-11 21:01:54 +00:00
|
|
|
}
|
|
|
|
printf("Subor bol uspesne prijaty.\n");
|
2020-03-12 16:22:08 +00:00
|
|
|
|
2020-03-19 12:55:34 +00:00
|
|
|
//vypocet vlastneho kontrolneho suctu
|
|
|
|
byte* kontrolny_sucet;
|
|
|
|
int velkost_kontrolneho_suctu;
|
2020-04-16 09:25:05 +00:00
|
|
|
|
|
|
|
FILE *subor = fopen(cesta, "wb+");
|
|
|
|
int n = fwrite((unsigned char*)pole, 1, velkost, subor);
|
|
|
|
fclose(subor);
|
|
|
|
|
|
|
|
kontrolny_sucet = generovat_kontrolny_sucet_suboru(nastavenia, cesta, &velkost_kontrolneho_suctu);
|
2020-03-19 12:55:34 +00:00
|
|
|
|
|
|
|
//prijem hashu, ktory vypocital server
|
|
|
|
char* prijaty_kontrolny_sucet = calloc(velkost_kontrolneho_suctu, sizeof(char));
|
2020-04-16 09:25:05 +00:00
|
|
|
|
|
|
|
uspech = 0;
|
|
|
|
while(uspech != velkost_kontrolneho_suctu)
|
2020-03-19 12:55:34 +00:00
|
|
|
{
|
2020-04-16 09:25:05 +00:00
|
|
|
uspech = wolfSSL_read(ssl, (char*)prijaty_kontrolny_sucet, velkost_kontrolneho_suctu);
|
|
|
|
if(uspech <= 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Nastala chyba pri prijati kontrolneho suctu.\n");
|
|
|
|
free(prijaty_kontrolny_sucet);
|
|
|
|
return -1;
|
|
|
|
}
|
2020-03-19 12:55:34 +00:00
|
|
|
}
|
2020-04-16 09:25:05 +00:00
|
|
|
|
2020-03-12 16:22:08 +00:00
|
|
|
//kontrola ci sa prijaty a vypocitany kontrolny sucet suboru zhoduju
|
2020-04-16 09:25:05 +00:00
|
|
|
if(!strcmp((char*)prijaty_kontrolny_sucet, (char*)kontrolny_sucet))
|
2020-03-12 16:22:08 +00:00
|
|
|
{
|
|
|
|
printf("Subor prisiel v poriadku.\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-03-19 12:55:34 +00:00
|
|
|
fprintf(stderr, "Kontrolne sucty sa nezhoduju.\n");
|
|
|
|
printf("Subor neprisiel v poriadku alebo neboli pouzite rovnake funkcie.\n");
|
2020-03-12 16:22:08 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2020-04-16 09:25:05 +00:00
|
|
|
|
|
|
|
char*data = calloc(100, sizeof(char));
|
|
|
|
uspech = wolfSSL_read(ssl, data, 100);
|
|
|
|
free(data);
|
|
|
|
|
|
|
|
free(cesta);
|
|
|
|
free(velkost_suboru);
|
|
|
|
free(kontrolny_sucet);
|
|
|
|
free(prijaty_kontrolny_sucet);
|
2020-03-11 21:01:54 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int pripojit_na_server(char *ip_adresa, int cislo_portu, int pocet_sekund)
|
|
|
|
{
|
|
|
|
struct timeval casovy_interval;
|
|
|
|
int uspech;
|
|
|
|
int cislo_soketu;
|
|
|
|
struct hostent* hostitel;
|
|
|
|
struct sockaddr_in adresa;
|
|
|
|
fd_set sada_soketov;
|
|
|
|
socklen_t velkost_soketu;
|
|
|
|
int optval;
|
|
|
|
|
|
|
|
if((hostitel = gethostbyname(ip_adresa)) == NULL)
|
|
|
|
{
|
|
|
|
printf("Nastala chyba pri spracovani nazvu hostitela.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
printf("Vytvaranie soketu...\n");
|
|
|
|
cislo_soketu = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
memset(&adresa, 0, sizeof(adresa));
|
|
|
|
adresa.sin_family = AF_INET; //IPv4
|
|
|
|
adresa.sin_port = htons(cislo_portu);
|
|
|
|
adresa.sin_addr.s_addr = *(long*)(hostitel->h_addr);
|
|
|
|
|
|
|
|
//pokus o pripojenie s casovym intervalom
|
|
|
|
uspech = connect(cislo_soketu, (struct sockaddr*)&adresa, sizeof(adresa));
|
|
|
|
if(uspech < 0)
|
|
|
|
{
|
2020-04-07 20:11:21 +00:00
|
|
|
#if defined(_WIN32)
|
|
|
|
if(errno == WSAEINPROGRESS )
|
|
|
|
#else
|
|
|
|
if(errno == EINPROGRESS )
|
|
|
|
#endif
|
2020-03-11 21:01:54 +00:00
|
|
|
{
|
|
|
|
fprintf(stderr, "Nebolo mozne okamzite vytvorit spojenie\n");
|
|
|
|
do
|
|
|
|
{
|
|
|
|
casovy_interval.tv_sec = pocet_sekund;
|
|
|
|
casovy_interval.tv_usec = 0;
|
|
|
|
FD_ZERO(&sada_soketov);
|
|
|
|
FD_SET(cislo_soketu, &sada_soketov);
|
|
|
|
uspech = select(cislo_soketu + 1, NULL, &sada_soketov, NULL, &casovy_interval);
|
2020-04-07 20:11:21 +00:00
|
|
|
#if defined(_WIN32)
|
|
|
|
if (uspech < 0 && errno != WSAEINTR)
|
|
|
|
#else
|
2020-03-11 21:01:54 +00:00
|
|
|
if (uspech < 0 && errno != EINTR)
|
2020-04-07 20:11:21 +00:00
|
|
|
#endif
|
2020-03-11 21:01:54 +00:00
|
|
|
{
|
|
|
|
fprintf(stderr, "Nastala chyba pri pokuse o vytvorenie spojenia\nCislo chyby: %d\nPopis: %s\n", errno, strerror(errno));
|
|
|
|
ukoncit_soket(cislo_soketu);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
//bol zvoleny soket
|
|
|
|
else if(uspech > 0)
|
|
|
|
{
|
|
|
|
velkost_soketu = sizeof(int);
|
|
|
|
if (getsockopt(cislo_soketu, SOL_SOCKET, SO_ERROR, (void*)(&optval), &velkost_soketu) < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Nastala chyba v nastaveni soketu\nCislo chyby: %d\nPopis: %s\n", errno, strerror(errno));
|
|
|
|
ukoncit_soket(cislo_soketu);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
if(optval)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Nastala chyba v spojeni\nCislo chyby: %d\nPopis: %s\n", optval, strerror(optval));
|
|
|
|
ukoncit_soket(cislo_soketu);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Casovy interval vyprsal\n");
|
|
|
|
ukoncit_soket(cislo_soketu);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
} while(1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Spojenie zlyhalo.\nCislo chyby: %d\nPopis: %s\n", errno, strerror(errno));
|
|
|
|
ukoncit_soket(cislo_soketu);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cislo_soketu;
|
|
|
|
}
|
|
|
|
|
|
|
|
int cakat_na_komunikaciu(int cislo_portu)
|
|
|
|
{
|
|
|
|
int cislo_soketu;
|
|
|
|
struct sockaddr_in adresa;
|
|
|
|
|
|
|
|
printf("Vytvaranie socketu...\n");
|
|
|
|
cislo_soketu = socket(PF_INET, SOCK_STREAM, 0);
|
|
|
|
memset(&adresa, 0, sizeof(adresa));
|
|
|
|
adresa.sin_family = AF_INET; //IPv4
|
|
|
|
adresa.sin_port = htons(cislo_portu);
|
|
|
|
adresa.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
|
|
|
|
if(bind(cislo_soketu, (struct sockaddr*)&adresa, sizeof(adresa)) != 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Nebolo mozne priradit soket ku danemu portu.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2020-03-12 16:22:08 +00:00
|
|
|
|
|
|
|
//maximalna velkost do ktorej rada cakajucich spojeni pre soket moze rast
|
|
|
|
int velkost_radu = 10;
|
|
|
|
|
|
|
|
if(listen(cislo_soketu, velkost_radu) != 0)
|
2020-03-11 21:01:54 +00:00
|
|
|
{
|
|
|
|
fprintf(stderr, "Na danom porte nie je mozne cakat na komunikaciu.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("Server caka na komunikaciu na porte %d.\n", cislo_portu);
|
|
|
|
}
|
|
|
|
return cislo_soketu;
|
|
|
|
}
|
2020-03-30 01:32:44 +00:00
|
|
|
|
2020-05-06 10:46:52 +00:00
|
|
|
int rs232_otvorit_rozhranie(int cislo_rozhrania, int rychlost, char* rezim, int kontrola_toku_dat)
|
|
|
|
{
|
|
|
|
int uspech = 0;
|
|
|
|
uspech = RS232_OpenComport(cislo_rozhrania, rychlost, rezim, kontrola_toku_dat);
|
|
|
|
if(uspech == 1)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Nebolo mozne otvorit seriove rozhranie\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rs232_zatvorit_rozhranie(int cislo_rozhrania)
|
|
|
|
{
|
|
|
|
RS232_CloseComport(cislo_rozhrania);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rs232_odoslat_spravu(int cislo_rozhrania, char* sprava, int velkost_spravy)
|
|
|
|
{
|
|
|
|
int uspech = 0;
|
|
|
|
int odoslane_data = 0;
|
|
|
|
|
|
|
|
//kontrolne vzory, ktore jednoznacne identifikuju zaciatok a koniec spravy
|
|
|
|
unsigned char zaciatok_spravy[] = {0xAA, 0xAA, 0xAA, 0xAA};
|
|
|
|
unsigned char koniec_spravy[] = {0xBB, 0xBB, 0xBB, 0xBB};
|
|
|
|
|
|
|
|
//vytvorenie bajtov ktore budu niest informaciu o velkosti posielanej spravy
|
|
|
|
unsigned char* velkost_spravy_hex = calloc(4, sizeof(unsigned char));
|
|
|
|
velkost_spravy_hex[0] = (velkost_spravy >> 24) & 0xFF;
|
|
|
|
velkost_spravy_hex[1] = (velkost_spravy >> 16) & 0xFF;
|
|
|
|
velkost_spravy_hex[2] = (velkost_spravy >> 8) & 0xFF;
|
|
|
|
velkost_spravy_hex[3] = velkost_spravy & 0xFF;
|
|
|
|
|
|
|
|
//odoslanie kontrolnych vzorov, bajtov s velkostou spravy a samotnych dat na seriove rozhranie
|
|
|
|
uspech = RS232_SendBuf(cislo_rozhrania, (unsigned char*)zaciatok_spravy, sizeof(zaciatok_spravy));
|
|
|
|
if(uspech <= 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Nastala chyba pri odoslani kontrolneho vzoru\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
uspech = RS232_SendBuf(cislo_rozhrania, (unsigned char*)velkost_spravy_hex, 4);
|
|
|
|
if(uspech <= 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Nastala chyba pri odoslani kontrolneho vzoru\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
odoslane_data = (int)RS232_SendBuf(cislo_rozhrania, (unsigned char*)sprava, velkost_spravy);
|
|
|
|
RS232_SendBuf(cislo_rozhrania, (unsigned char*)koniec_spravy, sizeof(koniec_spravy));
|
|
|
|
if(uspech <= 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Nastala chyba pri odoslani kontrolneho vzoru\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(velkost_spravy_hex);
|
|
|
|
|
|
|
|
return odoslane_data;
|
|
|
|
}
|
|
|
|
int rs232_prijat_spravu(int cislo_rozhrania, const char* komunikacny_subor)
|
|
|
|
{
|
|
|
|
//otvorenie suboru do ktoreho sa zapisuju prijate binarne data
|
|
|
|
//ktore sa nasledne nacitaju do buffera (buf) pre desifrovanie
|
|
|
|
FILE* subor = fopen(komunikacny_subor, "ab+");
|
|
|
|
|
|
|
|
int uspech = 0;
|
|
|
|
int prebieha_nacitanie = 0;
|
|
|
|
int prebieha_nacitanie_velkosti_spravy = 0;
|
|
|
|
unsigned char znak;
|
|
|
|
|
|
|
|
//mnozstvo nacitanych dat
|
|
|
|
int nacitane_data = 0;
|
|
|
|
//velkost spravy, ktora sa bude prijmat
|
|
|
|
int velkost_spravy = 0;
|
|
|
|
//urcuje kolko bajtov z informacie o velkosti spravy
|
|
|
|
//uz bolo prijatych z celkoveho poctu (4)
|
|
|
|
int velkost_spravy_bajt = 0;
|
|
|
|
int velkost_spravy_pocet_bajtov = 4;
|
|
|
|
|
|
|
|
//kontrolne vzory, ktore jednoznacne identifikuju zaciatok a koniec spravy
|
|
|
|
unsigned char zaciatok_spravy[] = {0xAA, 0xAA, 0xAA, 0xAA};
|
|
|
|
int zaciatok_spravy_pozicia = 0; //aktualna pozicia v kontrolnom vzore
|
|
|
|
unsigned char koniec_spravy[] = {0xBB, 0xBB, 0xBB, 0xBB};
|
|
|
|
int koniec_spravy_pozicia = 0; //aktualna pozicia v kontrolnom vzore
|
|
|
|
int velkost_kontrolneho_vzoru = sizeof(zaciatok_spravy)/sizeof(zaciatok_spravy[0]);
|
|
|
|
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
uspech = RS232_PollComport(cislo_rozhrania, &znak, 1);
|
|
|
|
if(uspech > 0)
|
|
|
|
{
|
|
|
|
//ak nacitanie spravy este nezacalo, ale bol najdeny bajt, ktory je sucastou kontrolneho vzoru,
|
|
|
|
//ktory urcuje zaciatok spravy, posun sa v kontrolnom vzore o jednu poziciu a cakaj ci nebudu
|
|
|
|
//prichadzat dalsie bajty z tohto kontrolneho vzoru
|
|
|
|
if(!prebieha_nacitanie && (znak == zaciatok_spravy[zaciatok_spravy_pozicia]) &&
|
|
|
|
(zaciatok_spravy_pozicia < velkost_kontrolneho_vzoru - 1))
|
|
|
|
{
|
|
|
|
zaciatok_spravy_pozicia++;
|
|
|
|
}
|
|
|
|
//ak prebehlo nacitanie vsetkych bajtov, ktore su sucastou kontrolneho vzoru urcujuceho zaciatok spravy
|
|
|
|
//ocakavaj prichod bajtov, ktore budu niest informaciu o velkosti spravy
|
|
|
|
else if(!prebieha_nacitanie && (znak == zaciatok_spravy[zaciatok_spravy_pozicia]) &&
|
|
|
|
(zaciatok_spravy_pozicia == velkost_kontrolneho_vzoru - 1))
|
|
|
|
{
|
|
|
|
prebieha_nacitanie_velkosti_spravy = 1;
|
|
|
|
zaciatok_spravy_pozicia = 0;
|
|
|
|
}
|
|
|
|
//pokracuj v nacitavani bajtov urcujucich velkost spravy, dokym nie su vsetky nacitane
|
|
|
|
//a po nacitani vsetkych bajtov moze zacat nacitanie spravy
|
|
|
|
else if(!prebieha_nacitanie && prebieha_nacitanie_velkosti_spravy)
|
|
|
|
{
|
|
|
|
velkost_spravy_bajt++;
|
|
|
|
if(velkost_spravy_bajt == velkost_spravy_pocet_bajtov)
|
|
|
|
{
|
|
|
|
prebieha_nacitanie = 1;
|
|
|
|
prebieha_nacitanie_velkosti_spravy = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//ak prebieha nacitanie a bol najdeny bajt, ktory je sucastou kontrolneho vzoru, ktory urcuje koniec spravy,
|
|
|
|
//posun sa v kontrolnom vzore o jednu poziciu a cakaj ci nebudu prichadzat dalsie bajty z tohto kontrolneho vzoru
|
|
|
|
else if((prebieha_nacitanie && (znak == koniec_spravy[koniec_spravy_pozicia]) &&
|
|
|
|
(koniec_spravy_pozicia == 0)) || (!prebieha_nacitanie && (znak == koniec_spravy[koniec_spravy_pozicia]) &&
|
|
|
|
(koniec_spravy_pozicia < velkost_kontrolneho_vzoru - 1)))
|
|
|
|
{
|
|
|
|
koniec_spravy_pozicia++;
|
|
|
|
prebieha_nacitanie = 0;
|
|
|
|
}
|
|
|
|
//v pripade ak zacalo nacitanie kontrolneho vzoru, ktory urcuje koniec spravy, ale nebol najdeny posledny znak z tohto vzoru
|
|
|
|
//nacitaj do suboru docasne nezapisane znaky, ktore su sucastou kontrolneho vzoru a zaroven nacitaj aktualny znak
|
|
|
|
else if(!prebieha_nacitanie_velkosti_spravy && !prebieha_nacitanie && znak != koniec_spravy[koniec_spravy_pozicia])
|
|
|
|
{
|
|
|
|
for(int i = 0; i < koniec_spravy_pozicia; i++)
|
|
|
|
{
|
|
|
|
fwrite((char*)&koniec_spravy[i], 1, uspech, subor);
|
|
|
|
nacitane_data += uspech;
|
|
|
|
}
|
|
|
|
fwrite((char*)&znak, 1, uspech, subor);
|
|
|
|
nacitane_data += uspech;
|
|
|
|
|
|
|
|
koniec_spravy_pozicia = 0;
|
|
|
|
prebieha_nacitanie = 1;
|
|
|
|
}
|
|
|
|
//ak prebehlo nacitanie vsetkych bajtov, ktore su sucastou kontrolneho vzoru urcujuceho koniec spravy
|
|
|
|
//ukonci nacitanie spravy
|
|
|
|
else if(!prebieha_nacitanie_velkosti_spravy && !prebieha_nacitanie && (znak == koniec_spravy[0]) &&
|
|
|
|
(koniec_spravy_pozicia == 3))
|
|
|
|
{
|
|
|
|
prebieha_nacitanie = 0;
|
|
|
|
koniec_spravy_pozicia = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
//inak prebieha nacitanie, cize zapis aktualny znak (bajt) do suboru
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(prebieha_nacitanie && !prebieha_nacitanie_velkosti_spravy)
|
|
|
|
{
|
|
|
|
fwrite((char*)&znak, 1, uspech, subor);
|
|
|
|
nacitane_data += uspech;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(subor);
|
|
|
|
return nacitane_data;
|
|
|
|
}
|
|
|
|
|
2020-04-07 20:11:21 +00:00
|
|
|
|
2020-03-30 01:32:44 +00:00
|
|
|
|