-
Notifications
You must be signed in to change notification settings - Fork 0
/
gbcd-client.c
406 lines (364 loc) · 8.43 KB
/
gbcd-client.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#include <stdio.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <sys/select.h>
#include <sys/time.h>
#include <unistd.h>
#include <termios.h>
#define NET_BUFFER_LEN 1024
/*
*TODO: max-scan
*/
typedef enum /*_OPERATION_MODE*/ {
INFO,
SCAN
} MODE;
struct {
int verbosity;
bool force_acquire;
bool keep;
MODE mode;
char* server;
uint16_t port;
char* name;
unsigned int max_scans;
unsigned int scans;
} CONF;
int usage(char* fn){
printf("garfield-barcoded shell client\n\n");
printf("Known parameters:\n");
printf("\t--verbosity <n>\t\tSet verbosity (Default: 1, 0 supresses output)\n");
printf("\t--force\t\t\tForcibly acquire barcode scanner\n");
printf("\t--port <n>\t\tSet barcoded port\n");
printf("\t--max-scan <n>\t\tScan n barcodes and exit\n");
printf("\t--keep\t\t\tRegrab the scanner should someone else grab it\n");
printf("\tinfo\t\t\tGet Barcodescanner information\n");
printf("\tscan\t\t\tScan barcodes if scanner available\n");
printf("\t<host>\t\t\tBarcoded host\n");
printf("\t<username>\t\tUser name to acquire with\n");
return 1;
}
int sock_connect(char* host, uint16_t port){
int sockfd=-1, error;
char port_str[20];
struct addrinfo hints;
struct addrinfo* head;
struct addrinfo* iter;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
snprintf(port_str, sizeof(port_str), "%d", port);
error=getaddrinfo(host, port_str, &hints, &head);
if(error){
fprintf(stderr, "getaddrinfo: %s\r\n", gai_strerror(error));
return -1;
}
for(iter=head;iter;iter=iter->ai_next){
sockfd=socket(iter->ai_family, iter->ai_socktype, iter->ai_protocol);
if(sockfd<0){
continue;
}
error=connect(sockfd, iter->ai_addr, iter->ai_addrlen);
if(error!=0){
close(sockfd);
continue;
}
break;
}
freeaddrinfo(head);
iter=NULL;
if(sockfd<0){
perror("socket");
return -1;
}
if(error!=0){
perror("connect");
return -1;
}
return sockfd;
}
int handle_input(int fd, char* data){
unsigned i;
char send_buf[NET_BUFFER_LEN+1];
send_buf[0]=0;
if(CONF.verbosity>1){
fprintf(stderr, "Received data: %s\r\n", data);
}
if(!strncmp(data, "CONNECT", 7)){
//if info, print barcode scanner name
if(CONF.mode==INFO){
printf("%s", data+strlen("CONNECT 1 "));
}
//connect, respond with connect
snprintf(send_buf, sizeof(send_buf)-1, "CONNECT 1 %s\nQUERY\n", CONF.name);
}
else if(!strncmp(data, "OK", 2)){
//logged in or grab success
//running QUERY here interferes with --force --keep
//snprintf(send_buf, sizeof(send_buf)-1, "QUERY\n");
}
else if(!strncmp(data, "OWNER", 5)){
if(CONF.mode==INFO){
data+=strlen("OWNER ");
for(i=0;data[i]!=' ';i++){
}
data[i]=0;
printf(" - %s\r\n", data);
snprintf(send_buf, sizeof(send_buf)-1, "QUIT\n");
}
else{
//still owned
if(CONF.force_acquire){
snprintf(send_buf, sizeof(send_buf)-1, "GRAB\n");
}
else if (CONF.verbosity>0){
fprintf(stderr, "Scanner is currently busy, (g)rab or (q)uit?\r\n");
}
}
}
else if(!strncmp(data, "RELEASED", 8)){
if(CONF.mode==INFO){
printf(" - Free\r\n");
snprintf(send_buf, sizeof(send_buf)-1, "QUIT\n");
}
else{
//free to grab, acquire if nice
snprintf(send_buf, sizeof(send_buf)-1, "ACQUIRE\n");
}
}
else if(!strncmp(data, "REVOKED", 7)){
//regrab if keep
if(CONF.mode==SCAN){
if(CONF.keep){
snprintf(send_buf, sizeof(send_buf)-1, "GRAB\n");
}
else if(CONF.verbosity>0){
fprintf(stderr, "Scanner was grabbed, re-(g)rab or (q)uit?\r\n");
}
}
}
else if(!strncmp(data, "BARCODE", 7)){
//barcode input
CONF.scans++;
printf("%s\r\n", data+strlen("BARCODE "));
if(CONF.max_scans>0&&CONF.scans>=CONF.max_scans){
snprintf(send_buf, sizeof(send_buf)-1, "QUIT\n");
}
if(CONF.verbosity>1){
fprintf(stderr, "Scanned %d barcodes\r\n", CONF.scans);
}
}
else if(!strncmp(data, "ERROR", 5)){
if(CONF.verbosity>0){
fprintf(stderr, "Barcode daemon reported an error: %s\r\n", data+strlen("ERROR "));
}
return 1;
}
else{
if(CONF.verbosity>0){
fprintf(stderr, "Incoming data not recognized: %s\r\n", data);
}
return -1;
}
if(send_buf[0]){
if(CONF.verbosity>1){
fprintf(stderr, "Sent data: %s\r", send_buf);
}
send(fd, send_buf, strlen(send_buf), 0);
}
return 0;
}
int loop_select(int fd){
fd_set readfds;
unsigned i,c;
struct timeval tv;
int error, offset=0, result;
char stdin_buf[10];
char sock_buf[NET_BUFFER_LEN+1];
memset(sock_buf, 0, sizeof(sock_buf));
while(true){
FD_ZERO(&readfds);
FD_SET(fileno(stdin), &readfds);
FD_SET(fd, &readfds);
tv.tv_sec=10;
tv.tv_usec=0;
error=select(fd+1, &readfds, NULL, NULL, &tv);
if(error<0){
perror("select");
return 1;
}
if(error==0&&CONF.mode==INFO){
if(CONF.verbosity>0){
fprintf(stderr, "Timeout\r\n");
}
return 2;
}
if(FD_ISSET(fd, &readfds)){
//read data
error=recv(fd, sock_buf, sizeof(sock_buf)-1-offset, 0);
if(error<0){
perror("recv");
return -1;
}
else if(error>0){
//scan for newline
for(i=0;i<error;i++){
if(sock_buf[offset+i]=='\n'){
sock_buf[offset+i]=0;
result=handle_input(fd, sock_buf);
if(result<0){
//error
}
else if(result>0){
//quit
break;
}
//copy back
for(c=0;c+i+1<error;c++){
sock_buf[c]=sock_buf[offset+c+i+1];
}
offset=0;
error=c;
i=-1;
}
}
offset=i;
if(result!=0){
break;
}
}
else{
//FIXME
//server disconnected (or buffer overrun)
break;
}
}
if(FD_ISSET(fileno(stdin), &readfds)){
error=read(fileno(stdin), stdin_buf, sizeof(stdin_buf));
if(error<0){
perror("read/stdin");
return 1;
}
else if(error>0){
switch(stdin_buf[0]){
case 'g':
send(fd, "GRAB\n", 5, 0);
break;
case 'q':
send(fd, "QUIT\n", 5, 0);
break;
}
}
else{
break;
}
}
}
return 0;
}
int main(int argc, char** argv){
unsigned i=0;
int sockfd=-1;
struct termios term_normal, term_raw;
CONF.verbosity=1;
CONF.force_acquire=false;
CONF.mode=INFO;
CONF.server=NULL;
CONF.port=3974;
CONF.name=NULL;
CONF.max_scans=0;
CONF.scans=0;
CONF.keep=false;
//parse commandline options
for(i=1;i<argc;i++){
if(!strcmp(argv[i], "--verbosity")){
if(!argv[i+1]){
fprintf(stderr,"Missing parameter for verbosity\n\n");
exit(usage(argv[0]));
}
CONF.verbosity=strtoul(argv[++i], NULL, 10);
}
else if(!strcmp(argv[i], "--force")){
CONF.force_acquire=true;
}
else if(!strcmp(argv[i], "--keep")){
CONF.keep=true;
}
else if(!strcmp(argv[i], "--port")){
if(!argv[i+1]){
fprintf(stderr,"Missing parameter for port\n\n");
exit(usage(argv[0]));
}
CONF.port=strtoul(argv[++i], NULL, 10);
}
else if(!strcmp(argv[i], "--max-scan")){
if(!argv[i+1]){
fprintf(stderr,"Missing parameter for max scans\n\n");
exit(usage(argv[0]));
}
CONF.max_scans=strtoul(argv[++i], NULL, 10);
}
else if(!strcmp(argv[i], "info")){
CONF.mode=INFO;
}
else if(!strcmp(argv[i], "scan")){
CONF.mode=SCAN;
}
else{
if(!CONF.server){
CONF.server=argv[i];
}
else{
CONF.name=argv[i];
}
}
}
if(!CONF.server||!CONF.name){
fprintf(stderr, "Missing vital connection information.\n\n");
exit(usage(argv[0]));
}
if(CONF.verbosity>1){
fprintf(stderr, "Configuration Summary\n");
fprintf(stderr, "Verbosity level %d\n", CONF.verbosity);
fprintf(stderr, "Operation mode: %s\n", (CONF.mode==INFO)?"INFO":"SCAN");
fprintf(stderr, "Host: %s:%d\n", CONF.server, CONF.port);
fprintf(stderr, "Acquirer name: %s\n", CONF.name);
fprintf(stderr, "Forcibly acquire scanner: %s\n", (CONF.force_acquire)?"yes":"no");
fprintf(stderr, "Limit to %d scans\n", CONF.max_scans);
}
//connect to server
sockfd=sock_connect(CONF.server, CONF.port);
if(sockfd<0){
exit(2);
}
if(CONF.mode!=INFO&&CONF.verbosity>0){
fprintf(stderr, "Connected to barcode scanner\nHit q to disconnect\n");
}
if(CONF.mode!=INFO){
//set up terminal
if(tcgetattr(0, &term_normal)<0){
perror("tcgetattr");
}
memcpy(&term_raw, &term_normal, sizeof(term_raw));
cfmakeraw(&term_raw);
if(tcsetattr(0, TCSANOW, &term_raw)<0){
perror("tcsetattr");
}
}
if(loop_select(sockfd)<0){
//error'd
}
//cleanup
if(CONF.mode!=INFO){
if(tcsetattr(0, TCSANOW, &term_normal)<0){
perror("tcsetattr");
}
}
close(sockfd);
return 0;
}