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