]>
git.zerfleddert.de Git - rigol/blob - rigold.c
29712e07b09db7230dc510be3032b9554c0d5a80
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>\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>\n",
78 sc
->status
.keyboard
.key_lock
);
80 send_text(s
, "Acquire: Type: %s, Mode: %s, Averages: %d<br>\n",
81 sc
->status
.acquire
.type
,
82 sc
->status
.acquire
.mode
,
83 sc
->status
.acquire
.averages
);
85 send_text(s
, "Horizontal: Mode: %s, Offset: %lg, Delayed Offset: %lg, Scale: %lg, Format: %s<br>\n",
86 sc
->status
.timebase
.mode
,
87 sc
->status
.timebase
.offset
,
88 sc
->status
.timebase
.delayed_offset
,
89 sc
->status
.timebase
.scale
,
90 sc
->status
.timebase
.format
);
92 send_text(s
, "Display: ");
93 send_command_output(s
, sc
, ":DISP:TYPE?");
95 send_command_output(s
, sc
, ":DISP:SCR?");
96 send_text(s
, ", Grid: ");
97 send_command_output(s
, sc
, ":DISP:GRID?");
98 send_text(s
, ", Persistence: ");
99 send_command_output(s
, sc
, ":DISP:PERS?");
100 send_text(s
, ", Menu: ");
101 send_command_output(s
, sc
, ":DISP:MNUS?");
102 send_text(s
, ", Brightness: ");
103 send_command_output(s
, sc
, ":DISP:BRIG?");
104 send_text(s
, ", Intensity: ");
105 send_command_output(s
, sc
, ":DISP:INT?");
106 send_text(s
, "<br>\n");
108 send_text(s
, "Channel 1: ");
109 send_command_output(s
, sc
, ":CHAN1:DISP?");
111 send_command_output(s
, sc
, ":CHAN1:MEMD?");
112 send_text(s
, " sample depth, %.10lg samples/s<br>\n", sc
->status
.acquire
.srate_chan1
);
113 send_text(s
, "Channel 2: ");
114 send_command_output(s
, sc
, ":CHAN2:DISP?");
116 send_command_output(s
, sc
, ":CHAN2:MEMD?");
117 send_text(s
, " sample depth, %.10lg samples/s<br>\n", sc
->status
.acquire
.srate_chan2
);
121 send_text(s
, sc
->status
.system
.lang
);
123 send_text(s
, "<br>\n");
124 send_text(s
, "<form method=\"get\" action=\"\">\n");
125 send_text(s
, "<input type=\"text\" name=\"cmd\" value=\"");
127 if (strncmp(param
, "cmd=", 4) == 0)
132 send_text(s
, "\">\n");
133 send_text(s
, "<input type=\"submit\">\n");
134 send_text(s
, "</form>\n");
135 send_text(s
, "<a href=\"?cmd=:RUN\">RUN</a> ");
136 send_text(s
, "<a href=\"?cmd=:STOP\">STOP</a> ");
137 send_text(s
, "<a href=\"?cmd=:FORC\">FORCE</a> ");
140 if (strchr (param
, '?')) {
141 send_text(s
, "<pre>< ");
143 send_text(s
, "\n> ");
144 send_command_output(s
, sc
, param
);
145 send_text(s
, "</pre>\n");
147 sendscpi(sc
, param
, NULL
, 0);
151 send_text(s
, "</body></html>\n");
154 static void serve_lcd(int s
, struct scope
*sc
)
161 png
= get_lcd(sc
, &imglen
, 0);
168 send_text(s
, "HTTP/1.0 200 OK\n");
169 send_text(s
, "Content-type: image/png\n");
170 snprintf(buf
, sizeof(buf
), "Content-length: %u\n\n", imglen
);
172 send_binary(s
, (char*)png
, imglen
);
176 static void serve_404(int s
)
178 send_text(s
, "HTTP/1.0 404 Not found\n");
179 send_text(s
, "Content-type: text/html\n\n");
180 send_text(s
, "<html><head><title>404</title></head>");
181 send_text(s
, "<body bgcolor=\"#ffffff\" text=\"#000000\">\n");
182 send_text(s
, "<h1>404</h1>");
183 send_text(s
, "</body></html>\n");
186 int is_eor(char *buf
)
190 /* This does not completely work, when the request is split
191 in multiple packets... */
192 if (strstr(buf
, "\x0d\x0a\x0d\x0a")) {
194 } else if (strstr(buf
, "\x0a\x0a")) {
196 } else if (strcmp(buf
, "\x0d\x0a") == 0) {
198 } else if (strcmp(buf
, "\x0a") == 0) {
206 static void parse_request(int s
, struct scope
*sc
)
212 const char delim
[] = " \t\x0d\x0a";
218 ret
=read(s
, buf
, sizeof(buf
)-1);
227 token
= strtok_r(buf
, delim
, &saveptr
);
230 /* TODO: Only GET... */
231 token
= strtok_r(NULL
, delim
, &saveptr
);
234 bzero(&file
, sizeof(file
));
235 strncpy(file
, token
, sizeof(file
)-1);
239 ret
=read(s
, buf
, sizeof(buf
)-1);
249 param
= strchr(file
, '?');
254 while ((token
= strchr(param
, '%')) != NULL
) {
257 if (strlen(token
+1) < 2)
260 strncpy(buf
, token
+1, 3);
263 *token
= (char)strtoul(buf
, NULL
, 16);
264 memmove(token
+1, token
+3, strlen(token
)-2);
267 while ((token
= strchr(param
, '+')) != NULL
) {
272 if (strcmp("/", file
) == 0) {
273 serve_index(s
, sc
, param
);
274 } else if (strcmp("/cgi-bin/lcd", file
) == 0) {
281 void sighandler(int sig
)
288 printf("Signal %d received\n", sig
);
293 int main(int argc
, char **argv
)
295 struct sigaction act
;
300 struct sockaddr_in sin
, clientsin
;
301 unsigned short port
= 8088;
305 printf("Scope not found!\n");
309 bzero(&act
, sizeof(act
));
310 act
.sa_handler
= sighandler
;
311 act
.sa_flags
= SA_RESTART
;
312 if (sigaction(SIGPIPE
, &act
, NULL
) == -1) {
317 bzero(&act
, sizeof(act
));
318 act
.sa_handler
= sighandler
;
319 if (sigaction(SIGALRM
, &act
, NULL
) == -1) {
324 if ((sock
= socket(AF_INET
, SOCK_STREAM
, 0)) == -1) {
333 bzero(&sin
, sizeof(sin
));
334 sin
.sin_addr
.s_addr
=htonl(INADDR_ANY
);
335 sin
.sin_family
=AF_INET
;
336 sin
.sin_port
=htons(port
);
339 setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
, &opt
,sizeof(opt
));
341 if (bind(sock
, (struct sockaddr
*)&sin
, sizeof(sin
)) == -1) {
347 printf("Listening on Port %u\n", port
);
350 bzero(&clientsin
, sizeof(clientsin
));
351 slen
= sizeof(clientsin
);
352 if ((csock
= accept(sock
, (struct sockaddr
*)&clientsin
, &slen
)) == -1) {
356 parse_request(csock
, sc
);