-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpdesk.c
245 lines (188 loc) · 4.77 KB
/
helpdesk.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
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <signal.h>
#include <time.h>
#include "lcdout.h"
#include "restio.h"
#include "dodisplay.h"
#define CONFIGFILE "/etc/helpdeskbuttons.conf"
#define CONFIGSIZE 4095
static int do_exit;
void doexit(int status) {
do_exit = 1;
}
void wait_for_server(rest_handle *rh,int *hits,short hitct) {
while(rest_press_button(rh,0,PRESENCE,hits,hitct)) {
sleep(1);
if(do_exit == 1) {
return;
}
}
}
int main(int argc,char* argv[])
{
lcd_device *lcdfd;
rest_handle *rh;
unsigned short prevstate,prevpres;
json_object *jobj;
struct sigaction act;
int screen_dirty;
int hits[4];
char *URL;
int i,j;
int rest_ok;
int deskid;
FILE* config;
struct stat statconfig;
char rawconfig[CONFIGSIZE+1];
time_t tNow;
time_t tBefore;
time_t idlecheck;
// Get the LCD up and running.
lcdfd = lcd_init();
if(!lcdfd) {
exit(1);
}
if(!(config=fopen(CONFIGFILE,"r"))) {
perror("Can't open config file");
lcd_clear(lcdfd);
lcd_gotoxy(lcdfd,0,0);
lcd_puts(lcdfd,"Error opening config");
exit(1);
}
if(stat(CONFIGFILE,&statconfig)) {
perror("Can't stat config file.");
lcd_clear(lcdfd);
lcd_gotoxy(lcdfd,0,0);
lcd_puts(lcdfd,"Error checking config");
exit(1);
}
if(statconfig.st_size >= CONFIGSIZE) {
fprintf(stderr,"Config file implausibly large\n");
lcd_clear(lcdfd);
lcd_gotoxy(lcdfd,0,0);
lcd_puts(lcdfd,"Config file too big");
exit(1);
}
fread(rawconfig,1,statconfig.st_size,config);
rawconfig[CONFIGSIZE] = 0;
jobj = json_tokener_parse(rawconfig);
deskid = 0;
URL = 0;
idlecheck = 3600;
json_object_object_foreach(jobj,key,val) {
if(strcmp(key,"URL")==0) {
URL = strdup(json_object_get_string(val));
}
if(strcmp(key,"deskid")==0) {
deskid = json_object_get_int(val);
}
if(strcmp(key,"idlecheck")==0) {
idlecheck = json_object_get_int(val);
}
}
if(URL == 0 || deskid == 0) {
fprintf(stderr,"Missing deskid or URL\n");
lcd_clear(lcdfd);
lcd_gotoxy(lcdfd,0,0);
lcd_puts(lcdfd,"config missing URL or");
lcd_gotoxy(lcdfd,0,1);
lcd_puts(lcdfd,"deskid");
exit(1);
}
//printf("url = '%s'\n",URL);
json_object_put(jobj);
// Set up for intercepting ctrl-c and such
do_exit = 0;
act.sa_handler = doexit;
sigaction(SIGINT,&act,0l);
sigaction(SIGHUP,&act,0l);
sigaction(SIGQUIT,&act,0l);
sigaction(SIGTERM,&act,0l);
rh = rest_init(URL,deskid);
lcd_backlight(lcdfd,1);
do_init_display(lcdfd);
lcd_force_button_update(lcdfd);
// Check once per second until a connection.
//while(rest_press_button(rh,0,PRESENCE,hits,presencect(lcdfd))) {
// sleep(1);
//}
wait_for_server(rh,hits,lcd_presencect(lcdfd));
tBefore = time(0);
prevstate = lcd_buttons(lcdfd);
prevpres = lcd_presencect(lcdfd);
screen_dirty = 1;
while(1)
{
if(do_exit) {
lcd_backlight(lcdfd,0);
lcd_clear(lcdfd);
return(0);
}
tNow = time(0);
if(tNow > (tBefore + idlecheck)) {
do_network_activity(lcdfd);
if(rest_press_button(rh,0,PRESENCE,hits,lcd_presencect(lcdfd))) {
wait_for_server(rh,hits,lcd_presencect(lcdfd));
}
tBefore = tNow;
screen_dirty = 1;
}
if(screen_dirty) {
do_main_display(lcdfd,lcd_presencect(lcdfd),hits,deskid);
screen_dirty = 0;
}
usleep(100000);
i = lcd_check_button_event(lcdfd);
switch(i) {
case 0:
screen_dirty = 1;
do_network_activity(lcdfd);
// New packet from controller
if(prevstate != lcd_buttons(lcdfd)) {
// Button pressed or unpressed
for(j=0;j<8;j++) {
// Check if a particular button changed to pressed
if ( ((prevstate & (1<<j)) != (lcd_buttons(lcdfd) & (1<<j)))
&& (lcd_buttons(lcdfd) & (1<<j)) ) {
// Set up data to send to controller
if(j<4) {
rest_ok = rest_press_button(rh,j,INCREASE,hits,lcd_presencect(lcdfd));
} else {
rest_ok = rest_press_button(rh,j-4,DECREASE,hits,lcd_presencect(lcdfd));
}
if(rest_ok != 0) {
do_init_display(lcdfd);
wait_for_server(rh,hits,lcd_presencect(lcdfd));
}
}
}
prevstate = lcd_buttons(lcdfd);
}
if(prevpres != lcd_presencect(lcdfd)) {
prevpres = lcd_presencect(lcdfd);
rest_ok = rest_press_button(rh,0,PRESENCE,hits,lcd_presencect(lcdfd));
if(rest_ok != 0) {
do_init_display(lcdfd);
wait_for_server(rh,hits,lcd_presencect(lcdfd));
}
}
break;
// No button/presence changes
case 1:
break;
default:
perror("Read from USB");
screen_dirty = 1;
do_init_display(lcdfd);
wait_for_server(rh,hits,lcd_presencect(lcdfd));
break;
}
}
return 0;
}