Blame |
Last modification |
View Log
| RSS feed
/*
* server.c
*
* Created on: 18. dec. 2018
* Author: Domen
*/
#include "server.h"
//#include "data_handling.h" je ze definiran v server.h
/*-------------------------------PUBLIC FUNCTIONS----------------------------------*/
int Server_init
(int *server_info
)
{
int sock_server
;
int sock_client
;
int enable
= 1;
struct sockaddr_in addr
;
if ( (sock_server
= socket
(AF_INET
, SOCK_STREAM
, 0)) < 0) //create an endpoint for communication
{
perror ("ERROR:socket");
return EXIT_FAILURE
;
}
setsockopt
(sock_server
, SOL_SOCKET
, SO_REUSEADDR
, (void *)&enable
, sizeof(enable
)); //set the socket options
//setup listening address (setup server address v flowchartu)
memset(&addr
, 0, sizeof(addr
)); //copy 0 to the first sizeof(addr) characters of the string, pointet by the &addr
addr.
sin_family = AF_INET
;
addr.
sin_addr.
s_addr = htonl
(INADDR_ANY
); //poslusaj na vseh IP naslovih //htonl converts the unsinged int hostlong from host byte order to netword byte order
addr.
sin_port = htons
(1001); //Red pitaya port = 1001; //htons je isto kot htonl, samo da za short integerje
if (bind
(sock_server
, (struct sockaddr
*)&addr
, sizeof(addr
)) < 0) //bind poveze ustvarjen socket (sock_server) z naslovom (stvar pod komentarjem setup listening address)
{
perror("ERROR:bind");
return EXIT_FAILURE
;
}
listen
(sock_server
,1024); //poskusa vzpostaviti povezavo, to naredi 1024x.
printf("Listening on port 1001 ... \n");
if( (sock_client
= accept
(sock_server
, NULL
, NULL
)) < 0)
{
perror("ERROR: accept");
return EXIT_FAILURE
;
}
else
{
server_info
[0] = sock_server
; //zlozim podatke o serverju v tabelo
server_info
[1] = sock_client
;
printf("Server_init funcija pride do konca. Vrne sock client:%d",sock_client
);
return 0;
}
}
void Server_comm
(int* server_info
, HW_system
*strct
, int status
)
{
int bytes_rec
;
uint32_t data_from_PC
;
uint32_t data_for_PC
;
int Server_ID
= server_info
[0];
int Client_ID
= server_info
[1];
int sent
= 0; //za test
static int do_it_once
= 0;
//PREJEMANJE PODATKOV OD PC-JA:
bytes_rec
= recv
(Client_ID
, &data_from_PC
, 4, MSG_DONTWAIT
); // vpise prejete podatke v data_from_PC in vrne, kolk bajtov je prejel v bytes_rec
if (bytes_rec
== 4) //vpis prejetih podatkov v strukturo
{
(strct
-> start
) = data_from_PC
& START_MASK
;
(strct
-> reset
) = (data_from_PC
& RESET_MASK
) >> 1;
(strct
-> HW_config_flag
) = (data_from_PC
& HW_CONFIG_MASK
) >> 2;
(strct
-> send_data_flag
) = (data_from_PC
& SEND_DATA_MASK
) >> 3;
(strct
-> disconnect_flag
) = (data_from_PC
& DISCONNECT_MASK
) >> 4;
(strct
-> threshold
) = (data_from_PC
& THRESHOLD_MASK
) >> 8;
(strct
-> acq_window
) = (data_from_PC
& ACQ_WINDOW_MASK
) >> 19;
(strct
-> sw_trigger
) = (data_from_PC
& SW_TRIGGER_MASK
) >> 27;
//za test
//printf("Podatki, prejeti od PC-ja: 0x%08x\n",data_from_PC);
}
else
{
//printf("Ni blo 4 bajtov, prislo je %d bajtov.\n", bytes_rec);
}
//------------------------------NOTIFICATIONS ZA TEST-------------------------------------------------
if((strct
-> reset
)==1) printf("Sistem RESET\n");
//-----------------------------------------------------------------------------------------------------
//POSILJANJE PODATKOV V PC:
if ( ((strct
-> send_data_flag
) == 1) && (status
== 2) )
{
data_for_PC
= ( (strct
-> hist_data
) << 8 ) | ( (strct
-> UC_rd_en_current
) << 1 ) | (strct
-> UC_rd_en_final
);
sent
= send
(Client_ID
,&data_for_PC
, 4, MSG_NOSIGNAL
); //&data_for_PC
printf("Poslano %d bajtov.\n",sent
);
//ko pride final histogram, ugasni prejemanje, da ga potem PC spet zazene in se ne prehiteva
if ( (strct
-> UC_rd_en_final
) == 1 && do_it_once
== 0)
{
(strct
-> send_data_flag
) = 0;
//printf("Poslan prvi podatek koncnega histograma. Ugasam posiljanje!");
do_it_once
= 1;
}
else if ((strct
-> UC_rd_en_current
== 1))
{
do_it_once
= 0;
}
}
//TODO: zapiranje serverja in clienta
if ((strct
-> disconnect_flag
) == 1)
{
close
(Client_ID
);
close
(Server_ID
);
}
}