]> git.zerfleddert.de Git - rigol/blob - rigold.c
93a5eb05a78c3c71d1d82fb4720cd616ee08be55
[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 int i;
64 struct channel_s *ch;
65
66 send_text(s, "HTTP/1.0 200 OK\n");
67 send_text(s, "Content-type: text/html\n\n");
68 send_text(s, "<html><head><title>%s</title></head><body bgcolor=\"#ffffff\" text=\"#000000\">\n", scope_idn(sc));
69 send_text(s, "<img src=\"/cgi-bin/lcd\" height=\"234\" width=\"320\">\n");
70 send_text(s, "<br>\n");
71
72 claimscope(sc);
73 update_scope_status(sc);
74 releasescope(sc);
75
76 send_text(s, "System: Language: %s, Counter: %d, Beep: %d<br><br>\n",
77 sc->status.system.lang,
78 sc->status.system.counter_enabled,
79 sc->status.system.beep_enabled);
80
81 send_text(s, "Keyboard: Key Lock: %d<br><br>\n",
82 sc->status.keyboard.key_lock);
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);
118
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
150 send_text(s, "<br>Acquire: Type: %s, Mode: %s, Averages: %d, CH1: %.10lg samples/s, CH2: %.10lg samples/s<br>\n",
151 sc->status.acquire.type,
152 sc->status.acquire.mode,
153 sc->status.acquire.averages,
154 sc->status.acquire.srate_ch1,
155 sc->status.acquire.srate_ch2);
156
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",
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);
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 }
187
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);
194
195 send_text(s, "<br>Math: Math Displayed: %d, FFT Displayed: %d<br>\n",
196 sc->status.math.displayed,
197 sc->status.fft.displayed);
198
199 send_text(s, "<br>\n");
200 send_text(s, "<form method=\"get\" action=\"\">\n");
201 send_text(s, "<input type=\"text\" name=\"cmd\" value=\"");
202 if (param) {
203 if (strncmp(param, "cmd=", 4) == 0)
204 param += 4;
205
206 send_text(s, param);
207 }
208 send_text(s, "\">\n");
209 send_text(s, "<input type=\"submit\">\n");
210 send_text(s, "</form>\n");
211 send_text(s, "<a href=\"?cmd=:RUN\">RUN</a> ");
212 send_text(s, "<a href=\"?cmd=:STOP\">STOP</a> ");
213 send_text(s, "<a href=\"?cmd=:FORC\">FORCE</a> ");
214 if (param) {
215 claimscope(sc);
216 if (strchr (param, '?')) {
217 send_text(s, "<pre>< ");
218 send_text(s, param);
219 send_text(s, "\n> ");
220 send_command_output(s, sc, param);
221 send_text(s, "</pre>\n");
222 } else {
223 sendscpi(sc, param, NULL, 0);
224 }
225 releasescope(sc);
226 }
227 send_text(s, "</body></html>\n");
228 }
229
230 static void serve_lcd(int s, struct scope *sc)
231 {
232 char buf[256];
233 int imglen;
234 unsigned char *png;
235
236 claimscope(sc);
237 png = get_lcd(sc, &imglen, 0);
238 releasescope(sc);
239
240 if (png == NULL)
241 return;
242
243
244 send_text(s, "HTTP/1.0 200 OK\n");
245 send_text(s, "Content-type: image/png\n");
246 snprintf(buf, sizeof(buf), "Content-length: %u\n\n", imglen);
247 send_text(s, buf);
248 send_binary(s, (char*)png, imglen);
249 free(png);
250 }
251
252 static void serve_404(int s)
253 {
254 send_text(s, "HTTP/1.0 404 Not found\n");
255 send_text(s, "Content-type: text/html\n\n");
256 send_text(s, "<html><head><title>404</title></head>");
257 send_text(s, "<body bgcolor=\"#ffffff\" text=\"#000000\">\n");
258 send_text(s, "<h1>404</h1>");
259 send_text(s, "</body></html>\n");
260 }
261
262 int is_eor(char *buf)
263 {
264 int eor = 0;
265
266 /* This does not completely work, when the request is split
267 in multiple packets... */
268 if (strstr(buf, "\x0d\x0a\x0d\x0a")) {
269 eor = 1;
270 } else if (strstr(buf, "\x0a\x0a")) {
271 eor = 1;
272 } else if (strcmp(buf, "\x0d\x0a") == 0) {
273 eor = 1;
274 } else if (strcmp(buf, "\x0a") == 0) {
275 eor = 1;
276 }
277
278 return eor;
279 }
280
281
282 static void parse_request(int s, struct scope *sc)
283 {
284 int ret;
285 int eor;
286 char buf[4096];
287 char file[4096];
288 const char delim[] = " \t\x0d\x0a";
289 char *saveptr;
290 char *token;
291 char *param = NULL;
292
293 alarm(TIMEOUT);
294 ret=read(s, buf, sizeof(buf)-1);
295 if (ret == -1) {
296 perror("read");
297 return;
298 }
299 buf[ret] = 0;
300
301 eor = is_eor(buf);
302
303 token = strtok_r(buf, delim, &saveptr);
304 if (token == NULL)
305 return;
306 /* TODO: Only GET... */
307 token = strtok_r(NULL, delim, &saveptr);
308 if (token == NULL)
309 return;
310 bzero(&file, sizeof(file));
311 strncpy(file, token, sizeof(file)-1);
312
313 while (!eor) {
314 alarm(TIMEOUT);
315 ret=read(s, buf, sizeof(buf)-1);
316 if (ret == -1) {
317 perror("read");
318 return;
319 }
320 buf[ret] = 0;
321 eor = is_eor(buf);
322 }
323 alarm(0);
324
325 param = strchr(file, '?');
326 if (param) {
327 *param = 0;
328 param++;
329
330 while ((token = strchr(param, '%')) != NULL) {
331 char buf[3];
332
333 if (strlen(token+1) < 2)
334 break;
335
336 strncpy(buf, token+1, 3);
337 buf[2] = 0;
338
339 *token = (char)strtoul(buf, NULL, 16);
340 memmove(token+1, token+3, strlen(token)-2);
341
342 }
343 while ((token = strchr(param, '+')) != NULL) {
344 *token = ' ';
345 }
346 }
347
348 if (strcmp("/", file) == 0) {
349 serve_index(s, sc, param);
350 } else if (strcmp("/cgi-bin/lcd", file) == 0) {
351 serve_lcd(s, sc);
352 } else {
353 serve_404(s);
354 }
355 }
356
357 void sighandler(int sig)
358 {
359 switch(sig) {
360 case SIGPIPE:
361 break;
362 case SIGALRM:
363 default:
364 printf("Signal %d received\n", sig);
365 break;
366 }
367 }
368
369 int main(int argc, char **argv)
370 {
371 struct sigaction act;
372 int sock, csock;
373 int opt;
374 socklen_t slen;
375 struct scope *sc;
376 struct sockaddr_in sin, clientsin;
377 unsigned short port = 8088;
378
379 sc = initscope();
380 if (sc == NULL) {
381 printf("Scope not found!\n");
382 exit(EXIT_FAILURE);
383 }
384
385 bzero(&act, sizeof(act));
386 act.sa_handler = sighandler;
387 act.sa_flags = SA_RESTART;
388 if (sigaction(SIGPIPE, &act, NULL) == -1) {
389 perror("sigaction");
390 exit(EXIT_FAILURE);
391 }
392
393 bzero(&act, sizeof(act));
394 act.sa_handler = sighandler;
395 if (sigaction(SIGALRM, &act, NULL) == -1) {
396 perror("sigaction");
397 exit(EXIT_FAILURE);
398 }
399
400 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
401 perror("socket");
402 exit(EXIT_FAILURE);
403 }
404
405 if (argc == 2) {
406 port=atoi(argv[1]);
407 }
408
409 bzero(&sin, sizeof(sin));
410 sin.sin_addr.s_addr=htonl(INADDR_ANY);
411 sin.sin_family=AF_INET;
412 sin.sin_port=htons(port);
413
414 opt = 1;
415 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt,sizeof(opt));
416
417 if (bind(sock, (struct sockaddr*)&sin, sizeof(sin)) == -1) {
418 perror("bind");
419 exit(EXIT_FAILURE);
420 }
421
422 listen(sock, 32);
423 printf("Listening on Port %u\n", port);
424
425 while(1) {
426 bzero(&clientsin, sizeof(clientsin));
427 slen = sizeof(clientsin);
428 if ((csock = accept(sock, (struct sockaddr*)&clientsin, &slen)) == -1) {
429 perror("accept");
430 }
431
432 parse_request(csock, sc);
433
434 close(csock);
435 }
436
437 closescope(sc);
438 return 0;
439 }
Impressum, Datenschutz