-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader_curl.c
335 lines (293 loc) · 10 KB
/
reader_curl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
* Connector of SI data FIFO to printing to screen
*/
#include <error.h>
#include <errno.h>
#include <limits.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <signal.h>
#include <sys/wait.h>
#include "si_base.h"
#include "si_print.h"
#include <curl/curl.h>
#define FIFO_NAME "/tmp/si_data_fifo"
#define READER_TICK_TIMER 60 // seconds
#define URL1 "http://"
#define URL2 "/gorgon/www/reader/reader/"
void termination_handler(int signum){
f_term = 1;
}
#define stringparam(STRING, MAXSIZE) \
if(++i > argc) { \
error(EXIT_FAILURE, ERR_ARGS, "Unset parameter.\n"); \
} else if(strlen(argv[i]) >= (MAXSIZE)){ \
error(EXIT_FAILURE, ERR_ARGS, "Parameter too long.\n"); \
} else { \
strcpy((STRING), argv[i]); \
}
/* Usage */
int Usage(char *progname){
const char help[] = "\n"
"Pouziti:\n"
" %s [-h] [-tvq]\n"
"\n"
"Program cte SI cipy a odesila data na server do aplikace Gorgon\n"
"\n"
"Parametry:\n"
" -h ... help - tato napoveda\n"
" -v ... verbose - vypisovat vice informaci\n"
" -q ... quiet - vypisovat mene informaci\n"
" -s <server> ... Gorgon <server> [localhost]\n"
" -r <zavod> ... Gorgon <zavod> [test]\n"
" -l <log> ...logovat do souboru <log>\n"
"\n"
"Chyby:\n"
"\n";
printf(help, progname);
return 0;
}
/* End of usage */
/* Curl callbacks */
size_t debug_cb(char *ptr, size_t size, size_t nmemb, void *dummy){
size_t realsize = size * nmemb;
printf("Received from server:\n%s\n", ptr);
return realsize;
}
size_t status_cb(char *ptr, size_t size, size_t nmemb, void *server_status){
size_t realsize = size * nmemb;
int *st = (int *) server_status;
*st = si_getstatus_json(ptr);
if(*st == STAT_ERROR){
printf("Bad response from server:\n%s\n", ptr);
}
return realsize;
}
size_t display_cb(char *ptr, size_t size, size_t nmemb, char **displaystr){
size_t realsize = size * nmemb;
*displaystr = si_getstring_json(ptr, "display_out");
if(*displaystr == NULL){
printf("Bad response from server:\n%s\n", ptr);
}
return realsize;
}
/* Server listener test */
int ServerTest(char *url){
CURL *curl;
char *postdata;
struct curl_slist *headers;
int server_status = STAT_NOCONN;
curl = curl_easy_init();
headers = NULL;
headers = curl_slist_append(headers, "Content-Type: application/json");
headers = curl_slist_append(headers, "charsets: utf-8");
if((postdata = si_init_json())){
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postdata);
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *) &server_status);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, status_cb);
curl_easy_perform(curl);
free(postdata);
}
return server_status;
}
/* End of server listener test*/
#define MAX_PARAMS 256
#define MAXL_SERVER 51
#define MAXL_RACE 51
#define MAXL_LOGFILE 256
int main(int argc, char **argv){
int datafd;
int r;
pid_t pid;
struct s_sidata data;
struct s_dev *first_dev, *dev, **pp_dev;
char *postdata;
CURL *curl;
struct curl_slist *headers;
char *displaystr;
char cwd[MAXL_LOGFILE];
time_t timer;
FILE *fp_log;
/* Loop counters */
size_t i = 0;
size_t j = 0;
size_t p = 0;
/* Variables */
int verbose = 1;
char f_log = 0;
char server[MAXL_SERVER]; strcpy(server, "localhost");
char race[MAXL_RACE]; strcpy(race, "test");
char logfile[MAXL_LOGFILE]; logfile[0] = '\0';
char *url;
char *params[MAX_PARAMS];
/*
* Read through command-line arguments for options.
*/
for(i = 1; i < argc; i++) {
if(argv[i][0] == '-') {
for(j=0; argv[i][j] != '\0'; j++) {
switch(argv[i][j]) {
case 'v':
verbose++;
break;
case 'q':
verbose--;
break;
case 's':
stringparam(server, MAXL_SERVER);
goto nextparm;
case 'r':
stringparam(race, MAXL_RACE);
goto nextparm;
case 'l':
stringparam(logfile, MAXL_LOGFILE);
f_log = 1;
goto nextparm;
case 'h':
Usage(argv[0]);
return 0;
}
}
nextparm:
;
}else{
if(p >= MAX_PARAMS) continue;
params[p] = malloc(sizeof(*argv[i]));
strcpy(params[p], argv[i]);
p++;
}
}
/* End of getparam */
if(getcwd(cwd, MAXL_LOGFILE) == NULL){
error(EXIT_FAILURE, ERR_SIZE, "Too long path.");
}
si_verbose = verbose; // set si library verbose level
url = (char *) malloc(strlen(URL1) + strlen(URL2) + strlen(server) + strlen(race));
strcpy(url, URL1);
strcat(url, server);
strcat(url, URL2);
strcat(url, race);
if(verbose > 1) printf(">> URL: %s\n", url);
if(sizeof(data) > PIPE_BUF){
error(EXIT_FAILURE, ERR_SIZE, "Size of card data too big.");
}
if(access(FIFO_NAME, F_OK) == -1){
if(mkfifo(FIFO_NAME, 0777) != 0){
error(EXIT_FAILURE, errno, "Cannot create fifo %s", FIFO_NAME);
}
}
if(si_detect_devices(&first_dev) == 0){
error(EXIT_FAILURE, 0, "No SI devices detected.");
}
if((r = ServerTest(url)) != STAT_OK){
error(EXIT_FAILURE, 0, "Server error: %s", si_stat_errdesc(r));
}
chdir(cwd);
if(f_log){
if(logfile[0] != '/'){
if((strlen(cwd) + strlen(logfile) + 1) >= MAXL_LOGFILE){
error(EXIT_FAILURE, ERR_SIZE, "Too long path.");
}
strcat(cwd, "/");
strcat(cwd, logfile);
strcpy(logfile, cwd);
}
if((fp_log = fopen(logfile, "a")) == NULL){
error(EXIT_FAILURE, errno, "Cannot open log %s", logfile);
}
if(verbose > 1) printf(">> Logging into file: %s\n", logfile);
}
pp_dev = &first_dev;
while(*pp_dev != NULL){
if(((*pp_dev)->fd = si_initserial((*pp_dev)->devfile)) == -1){
error(EXIT_SUCCESS, errno, "Cannot open device %s", (*pp_dev)->devfile);
*pp_dev = (*pp_dev)->next; // Excluded from list, but NOT freed from memory
}else if(! IS_NEW(si_station_detect((*pp_dev)->fd))){
error(EXIT_SUCCESS, 0, "Old SI station (%s) is not supported.", (*pp_dev)->devfile);
*pp_dev = (*pp_dev)->next; // Excluded from list, but NOT freed from memory
}else if(((*pp_dev)->prot = si_station_setprot((*pp_dev)->fd)) == -1){
error(EXIT_SUCCESS, 0, "Cannot setup SI station (%s) protocol.", (*pp_dev)->devfile);
*pp_dev = (*pp_dev)->next; // Excluded from list, but NOT freed from memory
}else{
pp_dev = &((*pp_dev)->next);
}
}
if(first_dev == NULL){
error(EXIT_FAILURE, 0, "No devices can be opened.");
}
signal(SIGINT, termination_handler);
signal(SIGQUIT, termination_handler);
signal(SIGTERM, termination_handler);
signal(SIGHUP, termination_handler);
pid = fork();
switch(pid){
case -1:
error(EXIT_FAILURE, errno, "Cannot create new process.\n");
case 0: // SI reader
if((datafd = open(FIFO_NAME, O_WRONLY)) == -1){
error(EXIT_FAILURE, errno, "Cannot open fifo.\n");
}else{
si_reader_m(first_dev, datafd, READER_TICK_TIMER);
close(datafd);
}
break;
default:
if(verbose > 0) puts("Reading SI cards. Press Ctrl-C to break.");
if(f_log) {
timer = time(NULL);
fputs("# ", fp_log);
fputs(ctime(&timer), fp_log);
}
if((datafd = open(FIFO_NAME, O_RDONLY)) == -1){
error(EXIT_FAILURE, errno, "Cannot open fifo.\n");
}else{
curl = curl_easy_init();
headers = NULL;
headers = curl_slist_append(headers, "Content-Type: application/json");
headers = curl_slist_append(headers, "charsets: utf-8");
do{
r = read(datafd, &data, sizeof(data));
if(r > 0){
if((postdata = si_data_json(&data))){
if(f_log){
fputs(postdata, fp_log);
fputc('\n', fp_log);
}
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postdata);
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *) &displaystr);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, display_cb);
//curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, debug_cb);
if(verbose > 1) printf(">>> JSON DATA: %s\n", postdata);
curl_easy_perform(curl);
if(displaystr != NULL){
puts(displaystr);
free(displaystr);
}
free(postdata);
}
}
}while(r > 0);
curl_slist_free_all(headers);
}
wait(NULL);
}
// SI device reset to original state procedure
dev = first_dev;
while(dev != NULL){
if(si_station_resetprot(dev->fd, dev->prot) < 0){
error(EXIT_SUCCESS, 0, "Reset SI station setup failed for %s.", dev->devfile);
}
dev = dev->next;
}
if(f_log) fclose(fp_log);
return 0;
}