]> git.zerfleddert.de Git - rigol/blob - rigold.c
e97db7c2fd7510b012c074293d858976d44be99d
[rigol] / rigold.c
1 #include <sys/types.h>
2 #include <sys/socket.h>
3 #include <arpa/inet.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <strings.h>
8 #include <unistd.h>
9 #include <signal.h>
10 #include <stdarg.h>
11
12 #include "scope.h"
13 #include "commands.h"
14
15 #define TIMEOUT 4
16
17 static int send_binary(int s, char *buf, int len)
18 {
19 int ret;
20
21 while(len > 0) {
22 ret = write(s, buf, len);
23 if (ret == -1) {
24 perror("write");
25 return ret;
26 }
27 buf += ret;
28 len -= ret;
29 }
30
31 return 0;
32 }
33
34 static int send_text(int s, char *fmt, ...)
35 {
36 va_list argp;
37 char buf[4096];
38 int cnt;
39
40 va_start(argp, fmt);
41 cnt = vsnprintf(buf, sizeof(buf), fmt, argp);
42 va_end(argp);
43
44 if (cnt < 0)
45 return cnt;
46
47 return send_binary(s, buf, cnt);
48 }
49
50 static int send_command_output(int s, struct scope *sc, char *cmd)
51 {
52 unsigned char buf[1024*1024];
53 int res;
54
55 res = sendscpi(sc, cmd, buf, sizeof(buf));
56 send_binary(s, (char*)buf, res);
57
58 return res;
59 }
60
61 static void serve_index(int s, struct scope *sc, char *param)
62 {
63 send_text(s, "HTTP/1.0 200 OK\n");
64 send_text(s, "Content-type: text/html\n\n");
65 send_text(s, "<html><head><title>%s</title></head><body bgcolor=\"#ffffff\" text=\"#000000\">\n", scope_idn(sc));
66 send_text(s, "<img src=\"/cgi-bin/lcd\" height=\"234\" width=\"320\">\n");
67 send_text(s, "<br>\n");
68
69 claimscope(sc);
70 update_scope_status(sc);
71
72 send_text(s, "System: Language: %s, Counter: %d, Beep: %d<br><br>\n",
73 sc->status.system.lang,
74 sc->status.system.counter_enabled,
75 sc->status.system.beep_enabled);
76
77 send_text(s, "Keyboard: Key Lock: %d<br><br>\n",
78 sc->status.keyboard.key_lock);
79
80 send_text(s, "Measure: Source: %s, Total: %d<br>\n",
81 sc->status.measure.source,
82 sc->status.measure.total);
83
84 send_text(s, "Measure CH1:<br>\n");
85 send_text(s, "VPP: %lg, VMAX: %lg, VMIN: %lg, VAMPLITUDE: %lg, VTOP: %lg, VBASE: %lg, VAVERAGE: %lg, VRMS: %lg<br>\n",
86 sc->status.measure.ch1.vpp,
87 sc->status.measure.ch1.vmax,
88 sc->status.measure.ch1.vmin,
89 sc->status.measure.ch1.vamplitude,
90 sc->status.measure.ch1.vtop,
91 sc->status.measure.ch1.vbase,
92 sc->status.measure.ch1.vaverage,
93 sc->status.measure.ch1.vrms);
94
95 send_text(s, "Overshoot: %lg, Preshoot: %lg<br>\n",
96 sc->status.measure.ch1.overshoot,
97 sc->status.measure.ch1.preshoot);
98
99 send_text(s, "Frequency: %lg, Risetime: %lg, Falltime: %lg<br>\n",
100 sc->status.measure.ch1.frequency,
101 sc->status.measure.ch1.risetime,
102 sc->status.measure.ch1.falltime);
103
104 send_text(s, "Period: %lg, Pwidth: %lg, Nwidth: %lg, Pdutycycle: %lg, Ndutycycle: %lg<br>\n",
105 sc->status.measure.ch1.period,
106 sc->status.measure.ch1.pwidth,
107 sc->status.measure.ch1.nwidth,
108 sc->status.measure.ch1.pdutycycle,
109 sc->status.measure.ch1.ndutycycle);
110
111 send_text(s, "Pdelay: %lg, Ndelay: %lg<br>\n",
112 sc->status.measure.ch1.pdelay,
113 sc->status.measure.ch1.ndelay);
114
115 send_text(s, "Acquire: Type: %s, Mode: %s, Averages: %d<br>\n",
116 sc->status.acquire.type,
117 sc->status.acquire.mode,
118 sc->status.acquire.averages);
119
120 send_text(s, "Horizontal: Mode: %s, Offset: %lg, Delayed Offset: %lg, Scale: %lg, Format: %s<br>\n",
121 sc->status.timebase.mode,
122 sc->status.timebase.offset,
123 sc->status.timebase.delayed_offset,
124 sc->status.timebase.scale,
125 sc->status.timebase.format);
126
127 send_text(s, "Measure CH2:<br>\n");
128 send_text(s, "VPP: %lg, VMAX: %lg, VMIN: %lg, VAMPLITUDE: %lg, VTOP: %lg, VBASE: %lg, VAVERAGE: %lg, VRMS: %lg<br>\n",
129 sc->status.measure.ch2.vpp,
130 sc->status.measure.ch2.vmax,
131 sc->status.measure.ch2.vmin,
132 sc->status.measure.ch2.vamplitude,
133 sc->status.measure.ch2.vtop,
134 sc->status.measure.ch2.vbase,
135 sc->status.measure.ch2.vaverage,
136 sc->status.measure.ch2.vrms);
137
138 send_text(s, "Overshoot: %lg, Preshoot: %lg<br>\n",
139 sc->status.measure.ch2.overshoot,
140 sc->status.measure.ch2.preshoot);
141
142 send_text(s, "Frequency: %lg, Risetime: %lg, Falltime: %lg<br>\n",
143 sc->status.measure.ch2.frequency,
144 sc->status.measure.ch2.risetime,
145 sc->status.measure.ch2.falltime);
146
147 send_text(s, "Period: %lg, Pwidth: %lg, Nwidth: %lg, Pdutycycle: %lg, Ndutycycle: %lg<br>\n",
148 sc->status.measure.ch2.period,
149 sc->status.measure.ch2.pwidth,
150 sc->status.measure.ch2.nwidth,
151 sc->status.measure.ch2.pdutycycle,
152 sc->status.measure.ch2.ndutycycle);
153
154 send_text(s, "Pdelay: %lg, Ndelay: %lg<br>\n",
155 sc->status.measure.ch2.pdelay,
156 sc->status.measure.ch2.ndelay);
157
158 send_text(s, "Acquire: Type: %s, Mode: %s, Averages: %d<br>\n",
159 sc->status.acquire.type,
160 sc->status.acquire.mode,
161 sc->status.acquire.averages);
162
163 send_text(s, "Horizontal: Mode: %s, Offset: %lg, Delayed Offset: %lg, Scale: %lg, Format: %s<br><br>\n",
164 sc->status.timebase.mode,
165 sc->status.timebase.offset,
166 sc->status.timebase.delayed_offset,
167 sc->status.timebase.scale,
168 sc->status.timebase.format);
169
170 send_text(s, "Display: Type: %s, Grid: %s, Persist: %d, MNUDisplay: %s, MNUStatus: %d, Screen: %s, Brightnbess: %d, Intensity: %d<br><br>\n",
171 sc->status.display.type,
172 sc->status.display.grid,
173 sc->status.display.persist,
174 sc->status.display.mnudisplay,
175 sc->status.display.mnustatus,
176 sc->status.display.screen,
177 sc->status.display.brightness,
178 sc->status.display.intensity);
179
180 send_text(s, "Channel 1: ");
181 send_command_output(s, sc, ":CHAN1:DISP?");
182 send_text(s, ", ");
183 send_command_output(s, sc, ":CHAN1:MEMD?");
184 send_text(s, " sample depth, %.10lg samples/s<br>\n", sc->status.acquire.srate_ch1);
185 send_text(s, "Channel 2: ");
186 send_command_output(s, sc, ":CHAN2:DISP?");
187 send_text(s, ", ");
188 send_command_output(s, sc, ":CHAN2:MEMD?");
189 send_text(s, " sample depth, %.10lg samples/s<br>\n", sc->status.acquire.srate_ch2);
190
191 releasescope(sc);
192
193 send_text(s, sc->status.system.lang);
194
195 send_text(s, "<br>\n");
196 send_text(s, "<form method=\"get\" action=\"\">\n");
197 send_text(s, "<input type=\"text\" name=\"cmd\" value=\"");
198 if (param) {
199 if (strncmp(param, "cmd=", 4) == 0)
200 param += 4;
201
202 send_text(s, param);
203 }
204 send_text(s, "\">\n");
205 send_text(s, "<input type=\"submit\">\n");
206 send_text(s, "</form>\n");
207 send_text(s, "<a href=\"?cmd=:RUN\">RUN</a> ");
208 send_text(s, "<a href=\"?cmd=:STOP\">STOP</a> ");
209 send_text(s, "<a href=\"?cmd=:FORC\">FORCE</a> ");
210 if (param) {
211 claimscope(sc);
212 if (strchr (param, '?')) {
213 send_text(s, "<pre>< ");
214 send_text(s, param);
215 send_text(s, "\n> ");
216 send_command_output(s, sc, param);
217 send_text(s, "</pre>\n");
218 } else {
219 sendscpi(sc, param, NULL, 0);
220 }
221 releasescope(sc);
222 }
223 send_text(s, "</body></html>\n");
224 }
225
226 static void serve_lcd(int s, struct scope *sc)
227 {
228 char buf[256];
229 int imglen;
230 unsigned char *png;
231
232 claimscope(sc);
233 png = get_lcd(sc, &imglen, 0);
234 releasescope(sc);
235
236 if (png == NULL)
237 return;
238
239
240 send_text(s, "HTTP/1.0 200 OK\n");
241 send_text(s, "Content-type: image/png\n");
242 snprintf(buf, sizeof(buf), "Content-length: %u\n\n", imglen);
243 send_text(s, buf);
244 send_binary(s, (char*)png, imglen);
245 free(png);
246 }
247
248 static void serve_404(int s)
249 {
250 send_text(s, "HTTP/1.0 404 Not found\n");
251 send_text(s, "Content-type: text/html\n\n");
252 send_text(s, "<html><head><title>404</title></head>");
253 send_text(s, "<body bgcolor=\"#ffffff\" text=\"#000000\">\n");
254 send_text(s, "<h1>404</h1>");
255 send_text(s, "</body></html>\n");
256 }
257
258 int is_eor(char *buf)
259 {
260 int eor = 0;
261
262 /* This does not completely work, when the request is split
263 in multiple packets... */
264 if (strstr(buf, "\x0d\x0a\x0d\x0a")) {
265 eor = 1;
266 } else if (strstr(buf, "\x0a\x0a")) {
267 eor = 1;
268 } else if (strcmp(buf, "\x0d\x0a") == 0) {
269 eor = 1;
270 } else if (strcmp(buf, "\x0a") == 0) {
271 eor = 1;
272 }
273
274 return eor;
275 }
276
277
278 static void parse_request(int s, struct scope *sc)
279 {
280 int ret;
281 int eor;
282 char buf[4096];
283 char file[4096];
284 const char delim[] = " \t\x0d\x0a";
285 char *saveptr;
286 char *token;
287 char *param = NULL;
288
289 alarm(TIMEOUT);
290 ret=read(s, buf, sizeof(buf)-1);
291 if (ret == -1) {
292 perror("read");
293 return;
294 }
295 buf[ret] = 0;
296
297 eor = is_eor(buf);
298
299 token = strtok_r(buf, delim, &saveptr);
300 if (token == NULL)
301 return;
302 /* TODO: Only GET... */
303 token = strtok_r(NULL, delim, &saveptr);
304 if (token == NULL)
305 return;
306 bzero(&file, sizeof(file));
307 strncpy(file, token, sizeof(file)-1);
308
309 while (!eor) {
310 alarm(TIMEOUT);
311 ret=read(s, buf, sizeof(buf)-1);
312 if (ret == -1) {
313 perror("read");
314 return;
315 }
316 buf[ret] = 0;
317 eor = is_eor(buf);
318 }
319 alarm(0);
320
321 param = strchr(file, '?');
322 if (param) {
323 *param = 0;
324 param++;
325
326 while ((token = strchr(param, '%')) != NULL) {
327 char buf[3];
328
329 if (strlen(token+1) < 2)
330 break;
331
332 strncpy(buf, token+1, 3);
333 buf[2] = 0;
334
335 *token = (char)strtoul(buf, NULL, 16);
336 memmove(token+1, token+3, strlen(token)-2);
337
338 }
339 while ((token = strchr(param, '+')) != NULL) {
340 *token = ' ';
341 }
342 }
343
344 if (strcmp("/", file) == 0) {
345 serve_index(s, sc, param);
346 } else if (strcmp("/cgi-bin/lcd", file) == 0) {
347 serve_lcd(s, sc);
348 } else {
349 serve_404(s);
350 }
351 }
352
353 void sighandler(int sig)
354 {
355 switch(sig) {
356 case SIGPIPE:
357 break;
358 case SIGALRM:
359 default:
360 printf("Signal %d received\n", sig);
361 break;
362 }
363 }
364
365 int main(int argc, char **argv)
366 {
367 struct sigaction act;
368 int sock, csock;
369 int opt;
370 socklen_t slen;
371 struct scope *sc;
372 struct sockaddr_in sin, clientsin;
373 unsigned short port = 8088;
374
375 sc = initscope();
376 if (sc == NULL) {
377 printf("Scope not found!\n");
378 exit(EXIT_FAILURE);
379 }
380
381 bzero(&act, sizeof(act));
382 act.sa_handler = sighandler;
383 act.sa_flags = SA_RESTART;
384 if (sigaction(SIGPIPE, &act, NULL) == -1) {
385 perror("sigaction");
386 exit(EXIT_FAILURE);
387 }
388
389 bzero(&act, sizeof(act));
390 act.sa_handler = sighandler;
391 if (sigaction(SIGALRM, &act, NULL) == -1) {
392 perror("sigaction");
393 exit(EXIT_FAILURE);
394 }
395
396 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
397 perror("socket");
398 exit(EXIT_FAILURE);
399 }
400
401 if (argc == 2) {
402 port=atoi(argv[1]);
403 }
404
405 bzero(&sin, sizeof(sin));
406 sin.sin_addr.s_addr=htonl(INADDR_ANY);
407 sin.sin_family=AF_INET;
408 sin.sin_port=htons(port);
409
410 opt = 1;
411 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt,sizeof(opt));
412
413 if (bind(sock, (struct sockaddr*)&sin, sizeof(sin)) == -1) {
414 perror("bind");
415 exit(EXIT_FAILURE);
416 }
417
418 listen(sock, 32);
419 printf("Listening on Port %u\n", port);
420
421 while(1) {
422 bzero(&clientsin, sizeof(clientsin));
423 slen = sizeof(clientsin);
424 if ((csock = accept(sock, (struct sockaddr*)&clientsin, &slen)) == -1) {
425 perror("accept");
426 }
427
428 parse_request(csock, sc);
429
430 close(csock);
431 }
432
433 closescope(sc);
434 return 0;
435 }
Impressum, Datenschutz