]>
git.zerfleddert.de Git - rigol/blob - rigold.c
c399dc55526bd5b6f5cffd745c39c0d7f0fde881
2 #include <sys/socket.h>
17 static int send_binary(int s
, char *buf
, int len
)
22 ret
= write(s
, buf
, len
);
34 static int send_text(int s
, char *fmt
, ...)
41 cnt
= vsnprintf(buf
, sizeof(buf
), fmt
, argp
);
47 return send_binary(s
, buf
, cnt
);
50 static int send_command_output(int s
, struct scope
*sc
, char *cmd
)
52 unsigned char buf
[1024*1024];
55 res
= sendscpi(sc
, cmd
, buf
, sizeof(buf
));
56 send_binary(s
, (char*)buf
, res
);
61 static void serve_index(int s
, struct scope
*sc
, char *param
)
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");
70 update_scope_status(sc
);
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
);
77 send_text(s
, "Keyboard: Key Lock: %d<br><br>\n",
78 sc
->status
.keyboard
.key_lock
);
80 send_text(s
, "Measure: Source: %s, Total: %d<br>\n",
81 sc
->status
.measure
.source
,
82 sc
->status
.measure
.total
);
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
);
95 send_text(s
, "Overshoot: %lg, Preshoot: %lg<br>\n",
96 sc
->status
.measure
.ch1
.overshoot
,
97 sc
->status
.measure
.ch1
.preshoot
);
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
);
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
);
111 send_text(s
, "Pdelay: %lg, Ndelay: %lg<br>\n",
112 sc
->status
.measure
.ch1
.pdelay
,
113 sc
->status
.measure
.ch1
.ndelay
);
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
);
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
);
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
);
138 send_text(s
, "Overshoot: %lg, Preshoot: %lg<br>\n",
139 sc
->status
.measure
.ch2
.overshoot
,
140 sc
->status
.measure
.ch2
.preshoot
);
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
);
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
);
154 send_text(s
, "Pdelay: %lg, Ndelay: %lg<br>\n",
155 sc
->status
.measure
.ch2
.pdelay
,
156 sc
->status
.measure
.ch2
.ndelay
);
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
);
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
);
170 send_text(s
, "Display: Type: %s, Grid: %s, Persist: %d, MNUDisplay: %s, MNUStatus: %d, Screen: %s, Brightness: %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
);
180 send_text(s
, "Channel 1: ");
181 send_command_output(s
, sc
, ":CHAN1:DISP?");
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?");
188 send_command_output(s
, sc
, ":CHAN2:MEMD?");
189 send_text(s
, " sample depth, %.10lg samples/s<br>\n", sc
->status
.acquire
.srate_ch2
);
193 send_text(s
, sc
->status
.system
.lang
);
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=\"");
199 if (strncmp(param
, "cmd=", 4) == 0)
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> ");
212 if (strchr (param
, '?')) {
213 send_text(s
, "<pre>< ");
215 send_text(s
, "\n> ");
216 send_command_output(s
, sc
, param
);
217 send_text(s
, "</pre>\n");
219 sendscpi(sc
, param
, NULL
, 0);
223 send_text(s
, "</body></html>\n");
226 static void serve_lcd(int s
, struct scope
*sc
)
233 png
= get_lcd(sc
, &imglen
, 0);
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
);
244 send_binary(s
, (char*)png
, imglen
);
248 static void serve_404(int s
)
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");
258 int is_eor(char *buf
)
262 /* This does not completely work, when the request is split
263 in multiple packets... */
264 if (strstr(buf
, "\x0d\x0a\x0d\x0a")) {
266 } else if (strstr(buf
, "\x0a\x0a")) {
268 } else if (strcmp(buf
, "\x0d\x0a") == 0) {
270 } else if (strcmp(buf
, "\x0a") == 0) {
278 static void parse_request(int s
, struct scope
*sc
)
284 const char delim
[] = " \t\x0d\x0a";
290 ret
=read(s
, buf
, sizeof(buf
)-1);
299 token
= strtok_r(buf
, delim
, &saveptr
);
302 /* TODO: Only GET... */
303 token
= strtok_r(NULL
, delim
, &saveptr
);
306 bzero(&file
, sizeof(file
));
307 strncpy(file
, token
, sizeof(file
)-1);
311 ret
=read(s
, buf
, sizeof(buf
)-1);
321 param
= strchr(file
, '?');
326 while ((token
= strchr(param
, '%')) != NULL
) {
329 if (strlen(token
+1) < 2)
332 strncpy(buf
, token
+1, 3);
335 *token
= (char)strtoul(buf
, NULL
, 16);
336 memmove(token
+1, token
+3, strlen(token
)-2);
339 while ((token
= strchr(param
, '+')) != NULL
) {
344 if (strcmp("/", file
) == 0) {
345 serve_index(s
, sc
, param
);
346 } else if (strcmp("/cgi-bin/lcd", file
) == 0) {
353 void sighandler(int sig
)
360 printf("Signal %d received\n", sig
);
365 int main(int argc
, char **argv
)
367 struct sigaction act
;
372 struct sockaddr_in sin
, clientsin
;
373 unsigned short port
= 8088;
377 printf("Scope not found!\n");
381 bzero(&act
, sizeof(act
));
382 act
.sa_handler
= sighandler
;
383 act
.sa_flags
= SA_RESTART
;
384 if (sigaction(SIGPIPE
, &act
, NULL
) == -1) {
389 bzero(&act
, sizeof(act
));
390 act
.sa_handler
= sighandler
;
391 if (sigaction(SIGALRM
, &act
, NULL
) == -1) {
396 if ((sock
= socket(AF_INET
, SOCK_STREAM
, 0)) == -1) {
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
);
411 setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
, &opt
,sizeof(opt
));
413 if (bind(sock
, (struct sockaddr
*)&sin
, sizeof(sin
)) == -1) {
419 printf("Listening on Port %u\n", port
);
422 bzero(&clientsin
, sizeof(clientsin
));
423 slen
= sizeof(clientsin
);
424 if ((csock
= accept(sock
, (struct sockaddr
*)&clientsin
, &slen
)) == -1) {
428 parse_request(csock
, sc
);