Subversion Repositories f9daq

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
336 f9daq 1
/*
2
 * server.c
3
 *
4
 *  Created on: 18. dec. 2018
5
 *      Author: Domen
6
 */
7
 
8
#include "server.h"
9
//#include "data_handling.h" je ze definiran v server.h
10
 
11
 
12
 
13
 
14
 
15
 
16
 
17
 
18
/*-------------------------------PUBLIC FUNCTIONS----------------------------------*/
19
 
20
int Server_init(int *server_info)
21
{
22
 
23
        int sock_server;
24
        int sock_client;
25
        int enable = 1;
26
        struct sockaddr_in addr;
27
 
28
 
29
 
30
 
31
 
32
        if ( (sock_server = socket(AF_INET, SOCK_STREAM, 0)) < 0)  //create an endpoint for communication
33
        {
34
                perror ("ERROR:socket");
35
                return EXIT_FAILURE;
36
        }
37
 
38
 
39
        setsockopt(sock_server, SOL_SOCKET, SO_REUSEADDR, (void *)&enable, sizeof(enable)); //set the socket options
40
 
41
        //setup listening address (setup server address v flowchartu)
42
        memset(&addr, 0, sizeof(addr)); //copy 0 to the first sizeof(addr) characters of the string, pointet by the &addr
43
        addr.sin_family = AF_INET;
44
        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
45
        addr.sin_port = htons(1001); //Red pitaya port = 1001; //htons je isto kot htonl, samo da za short integerje
46
 
47
 
48
        if (bind(sock_server, (struct sockaddr *)&addr, sizeof(addr)) < 0)  //bind poveze ustvarjen socket (sock_server) z naslovom (stvar pod komentarjem setup listening address)
49
        {
50
                perror("ERROR:bind");
51
                return EXIT_FAILURE;
52
        }
53
 
54
        listen(sock_server,1024); //poskusa vzpostaviti povezavo, to naredi 1024x.
55
        printf("Listening on port 1001 ... \n");
56
 
57
 
58
 
59
        if( (sock_client = accept(sock_server, NULL, NULL)) < 0)
60
        {
61
                perror("ERROR: accept");
62
                return EXIT_FAILURE;
63
        }
64
 
65
        else
66
        {
67
 
68
                server_info[0] = sock_server; //zlozim podatke o serverju v tabelo
69
                server_info[1] = sock_client;
70
                printf("Server_init funcija pride do konca. Vrne sock client:%d",sock_client);
71
 
72
                return 0;
73
 
74
 
75
        }
76
 
77
 
78
}
79
 
80
 
81
 
82
 
83
void Server_comm(int* server_info, HW_system *strct, int status)
84
{
85
        int bytes_rec;
86
        uint32_t data_from_PC;
87
        uint32_t data_for_PC;
88
 
89
        int Server_ID = server_info[0];
90
        int Client_ID = server_info[1];
91
 
92
        int sent = 0; //za test
93
        static int do_it_once = 0;
94
 
95
 
96
        //PREJEMANJE PODATKOV OD PC-JA:
97
        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
98
 
99
        if (bytes_rec == 4) //vpis prejetih podatkov v strukturo
100
        {
101
 
102
                (strct -> start) = data_from_PC & START_MASK;
103
                (strct -> reset) = (data_from_PC & RESET_MASK) >> 1;
104
                (strct -> HW_config_flag) = (data_from_PC & HW_CONFIG_MASK) >> 2;
105
                (strct -> send_data_flag) = (data_from_PC & SEND_DATA_MASK) >> 3;
106
                (strct -> disconnect_flag) = (data_from_PC & DISCONNECT_MASK) >> 4;
107
                (strct -> threshold) = (data_from_PC & THRESHOLD_MASK) >> 8;
108
                (strct -> acq_window) = (data_from_PC & ACQ_WINDOW_MASK) >> 19;
109
                (strct -> sw_trigger) = (data_from_PC & SW_TRIGGER_MASK) >> 27;
110
 
111
                //za test
112
                //printf("Podatki, prejeti od PC-ja: 0x%08x\n",data_from_PC);
113
        }
114
 
115
        else
116
        {
117
                //printf("Ni blo 4 bajtov, prislo je %d bajtov.\n", bytes_rec);
118
        }
119
 
120
 
121
        //------------------------------NOTIFICATIONS ZA TEST-------------------------------------------------
122
        if((strct -> reset)==1) printf("Sistem RESET\n");
123
 
124
        //-----------------------------------------------------------------------------------------------------
125
 
126
        //POSILJANJE PODATKOV V PC:
127
        if ( ((strct -> send_data_flag) == 1) && (status == 2) )
128
        {
129
                data_for_PC = ( (strct -> hist_data) << 8 ) | ( (strct -> UC_rd_en_current) << 1 ) |  (strct -> UC_rd_en_final);
130
                sent = send(Client_ID,&data_for_PC, 4, MSG_NOSIGNAL); //&data_for_PC
131
                printf("Poslano %d bajtov.\n",sent);
132
 
133
                //ko pride final histogram, ugasni prejemanje, da ga potem PC spet zazene in se ne prehiteva
134
                if ( (strct -> UC_rd_en_final) == 1 && do_it_once == 0)
135
                {
136
                        (strct -> send_data_flag) = 0;
137
                        //printf("Poslan prvi podatek koncnega histograma. Ugasam posiljanje!");
138
                        do_it_once = 1;
139
                }
140
 
141
                else if ((strct ->  UC_rd_en_current == 1))
142
                {
143
                        do_it_once = 0;
144
                }
145
        }
146
 
147
        //TODO: zapiranje serverja in clienta
148
        if ((strct -> disconnect_flag) == 1)
149
        {
150
                close(Client_ID);
151
                close(Server_ID);
152
        }
153
 
154
 
155
}
156
 
157