]> git.zerfleddert.de Git - rigol/blobdiff - rigold.c
typo
[rigol] / rigold.c
index b32c474454a859b4be6a2188492bf9b6c436f9f9..c399dc55526bd5b6f5cffd745c39c0d7f0fde881 100644 (file)
--- a/rigold.c
+++ b/rigold.c
-#include <usb.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <arpa/inet.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include <unistd.h>
+#include <signal.h>
+#include <stdarg.h>
 
-#include "usbtmc.h"
+#include "scope.h"
+#include "commands.h"
+
+#define TIMEOUT 4
+
+static int send_binary(int s, char *buf, int len)
+{
+       int ret;
+
+       while(len > 0) {
+               ret = write(s, buf, len);
+               if (ret == -1) {
+                       perror("write");
+                       return ret;
+               }
+               buf += ret;
+               len -= ret;
+       }
+
+       return 0;
+}
+
+static int send_text(int s, char *fmt, ...)
+{
+       va_list argp;
+       char buf[4096];
+       int cnt;
+
+       va_start(argp, fmt);
+       cnt = vsnprintf(buf, sizeof(buf), fmt, argp);
+       va_end(argp);
+
+       if (cnt < 0)
+               return cnt;
+
+       return send_binary(s, buf, cnt);
+}
+
+static int send_command_output(int s, struct scope *sc, char *cmd)
+{
+       unsigned char buf[1024*1024];
+       int res;
+
+       res = sendscpi(sc, cmd, buf, sizeof(buf));
+       send_binary(s, (char*)buf, res);
+
+       return res;
+}
+
+static void serve_index(int s, struct scope *sc, char *param)
+{
+       send_text(s, "HTTP/1.0 200 OK\n");
+       send_text(s, "Content-type: text/html\n\n");
+       send_text(s, "<html><head><title>%s</title></head><body bgcolor=\"#ffffff\" text=\"#000000\">\n", scope_idn(sc));
+       send_text(s, "<img src=\"/cgi-bin/lcd\" height=\"234\" width=\"320\">\n");
+       send_text(s, "<br>\n");
+
+       claimscope(sc);
+       update_scope_status(sc);
+
+       send_text(s, "System: Language: %s, Counter: %d, Beep: %d<br><br>\n",
+               sc->status.system.lang,
+               sc->status.system.counter_enabled,
+               sc->status.system.beep_enabled);
+       
+       send_text(s, "Keyboard: Key Lock: %d<br><br>\n",
+               sc->status.keyboard.key_lock);
+       
+       send_text(s, "Measure: Source: %s, Total: %d<br>\n",
+               sc->status.measure.source,
+               sc->status.measure.total);
+
+       send_text(s, "Measure CH1:<br>\n");
+       send_text(s, "VPP: %lg, VMAX: %lg, VMIN: %lg, VAMPLITUDE: %lg, VTOP: %lg, VBASE: %lg, VAVERAGE: %lg, VRMS: %lg<br>\n",
+               sc->status.measure.ch1.vpp,
+               sc->status.measure.ch1.vmax,
+               sc->status.measure.ch1.vmin,
+               sc->status.measure.ch1.vamplitude,
+               sc->status.measure.ch1.vtop,
+               sc->status.measure.ch1.vbase,
+               sc->status.measure.ch1.vaverage,
+               sc->status.measure.ch1.vrms);
+       
+       send_text(s, "Overshoot: %lg, Preshoot: %lg<br>\n",
+               sc->status.measure.ch1.overshoot,
+               sc->status.measure.ch1.preshoot);
+
+       send_text(s, "Frequency: %lg, Risetime: %lg, Falltime: %lg<br>\n",
+               sc->status.measure.ch1.frequency,
+               sc->status.measure.ch1.risetime,
+               sc->status.measure.ch1.falltime);
+
+       send_text(s, "Period: %lg, Pwidth: %lg, Nwidth: %lg, Pdutycycle: %lg, Ndutycycle: %lg<br>\n",
+               sc->status.measure.ch1.period,
+               sc->status.measure.ch1.pwidth,
+               sc->status.measure.ch1.nwidth,
+               sc->status.measure.ch1.pdutycycle,
+               sc->status.measure.ch1.ndutycycle);
+
+       send_text(s, "Pdelay: %lg, Ndelay: %lg<br>\n",
+               sc->status.measure.ch1.pdelay,
+               sc->status.measure.ch1.ndelay);
+
+       send_text(s, "Acquire: Type: %s, Mode: %s, Averages: %d<br>\n",
+               sc->status.acquire.type,
+               sc->status.acquire.mode,
+               sc->status.acquire.averages);
+
+       send_text(s, "Horizontal: Mode: %s, Offset: %lg, Delayed Offset: %lg, Scale: %lg, Format: %s<br>\n",
+               sc->status.timebase.mode,
+               sc->status.timebase.offset,
+               sc->status.timebase.delayed_offset,
+               sc->status.timebase.scale,
+               sc->status.timebase.format);
+
+       send_text(s, "Measure CH2:<br>\n");
+       send_text(s, "VPP: %lg, VMAX: %lg, VMIN: %lg, VAMPLITUDE: %lg, VTOP: %lg, VBASE: %lg, VAVERAGE: %lg, VRMS: %lg<br>\n",
+               sc->status.measure.ch2.vpp,
+               sc->status.measure.ch2.vmax,
+               sc->status.measure.ch2.vmin,
+               sc->status.measure.ch2.vamplitude,
+               sc->status.measure.ch2.vtop,
+               sc->status.measure.ch2.vbase,
+               sc->status.measure.ch2.vaverage,
+               sc->status.measure.ch2.vrms);
+       
+       send_text(s, "Overshoot: %lg, Preshoot: %lg<br>\n",
+               sc->status.measure.ch2.overshoot,
+               sc->status.measure.ch2.preshoot);
+
+       send_text(s, "Frequency: %lg, Risetime: %lg, Falltime: %lg<br>\n",
+               sc->status.measure.ch2.frequency,
+               sc->status.measure.ch2.risetime,
+               sc->status.measure.ch2.falltime);
+
+       send_text(s, "Period: %lg, Pwidth: %lg, Nwidth: %lg, Pdutycycle: %lg, Ndutycycle: %lg<br>\n",
+               sc->status.measure.ch2.period,
+               sc->status.measure.ch2.pwidth,
+               sc->status.measure.ch2.nwidth,
+               sc->status.measure.ch2.pdutycycle,
+               sc->status.measure.ch2.ndutycycle);
+
+       send_text(s, "Pdelay: %lg, Ndelay: %lg<br>\n",
+               sc->status.measure.ch2.pdelay,
+               sc->status.measure.ch2.ndelay);
+
+       send_text(s, "Acquire: Type: %s, Mode: %s, Averages: %d<br>\n",
+               sc->status.acquire.type,
+               sc->status.acquire.mode,
+               sc->status.acquire.averages);
+
+       send_text(s, "Horizontal: Mode: %s, Offset: %lg, Delayed Offset: %lg, Scale: %lg, Format: %s<br><br>\n",
+               sc->status.timebase.mode,
+               sc->status.timebase.offset,
+               sc->status.timebase.delayed_offset,
+               sc->status.timebase.scale,
+               sc->status.timebase.format);
+
+       send_text(s, "Display: Type: %s, Grid: %s, Persist: %d, MNUDisplay: %s, MNUStatus: %d, Screen: %s, Brightness: %d, Intensity: %d<br><br>\n",
+               sc->status.display.type,
+               sc->status.display.grid,
+               sc->status.display.persist,
+               sc->status.display.mnudisplay,
+               sc->status.display.mnustatus,
+               sc->status.display.screen,
+               sc->status.display.brightness,
+               sc->status.display.intensity);
+
+       send_text(s, "Channel 1: ");
+       send_command_output(s, sc, ":CHAN1:DISP?");
+       send_text(s, ", ");
+       send_command_output(s, sc, ":CHAN1:MEMD?");
+       send_text(s, " sample depth, %.10lg samples/s<br>\n", sc->status.acquire.srate_ch1);
+       send_text(s, "Channel 2: ");
+       send_command_output(s, sc, ":CHAN2:DISP?");
+       send_text(s, ", ");
+       send_command_output(s, sc, ":CHAN2:MEMD?");
+       send_text(s, " sample depth, %.10lg samples/s<br>\n", sc->status.acquire.srate_ch2);
+
+       releasescope(sc);
+
+       send_text(s, sc->status.system.lang);
+
+       send_text(s, "<br>\n");
+       send_text(s, "<form method=\"get\" action=\"\">\n");
+       send_text(s, "<input type=\"text\" name=\"cmd\" value=\"");
+       if (param) {
+               if (strncmp(param, "cmd=", 4) == 0)
+                       param += 4;
+
+               send_text(s, param);
+       }
+       send_text(s, "\">\n");
+       send_text(s, "<input type=\"submit\">\n");
+       send_text(s, "</form>\n");
+       send_text(s, "<a href=\"?cmd=:RUN\">RUN</a> ");
+       send_text(s, "<a href=\"?cmd=:STOP\">STOP</a> ");
+       send_text(s, "<a href=\"?cmd=:FORC\">FORCE</a> ");
+       if (param) {
+               claimscope(sc);
+               if (strchr (param, '?')) {
+                       send_text(s, "<pre>< ");
+                       send_text(s, param);
+                       send_text(s, "\n> ");
+                       send_command_output(s, sc, param);
+                       send_text(s, "</pre>\n");
+               } else {
+                       sendscpi(sc, param, NULL, 0);
+               }
+               releasescope(sc);
+       }
+       send_text(s, "</body></html>\n");
+}
+
+static void serve_lcd(int s, struct scope *sc)
+{
+       char buf[256];
+       int imglen;
+       unsigned char *png;
+
+       claimscope(sc);
+       png = get_lcd(sc, &imglen, 0);
+       releasescope(sc);
+
+       if (png == NULL)
+               return;
+
+
+       send_text(s, "HTTP/1.0 200 OK\n");
+       send_text(s, "Content-type: image/png\n");
+       snprintf(buf, sizeof(buf), "Content-length: %u\n\n", imglen);
+       send_text(s, buf);
+       send_binary(s, (char*)png, imglen);
+       free(png);
+}
+
+static void serve_404(int s)
+{
+       send_text(s, "HTTP/1.0 404 Not found\n");
+       send_text(s, "Content-type: text/html\n\n");
+       send_text(s, "<html><head><title>404</title></head>");
+       send_text(s, "<body bgcolor=\"#ffffff\" text=\"#000000\">\n");
+       send_text(s, "<h1>404</h1>");
+       send_text(s, "</body></html>\n");
+}
+
+int is_eor(char *buf)
+{
+       int eor = 0;
+
+       /* This does not completely work, when the request is split
+          in multiple packets... */
+       if (strstr(buf, "\x0d\x0a\x0d\x0a")) {
+               eor = 1;
+       } else if (strstr(buf, "\x0a\x0a")) {
+               eor = 1;
+       } else if (strcmp(buf, "\x0d\x0a") == 0) {
+               eor = 1;
+       } else if (strcmp(buf, "\x0a") == 0) {
+               eor = 1;
+       }
+
+       return eor;
+}
+
+
+static void parse_request(int s, struct scope *sc)
+{
+       int ret;
+       int eor;
+       char buf[4096];
+       char file[4096];
+       const char delim[] = " \t\x0d\x0a";
+       char *saveptr;
+       char *token;
+       char *param = NULL;
+
+       alarm(TIMEOUT);
+       ret=read(s, buf, sizeof(buf)-1);
+       if (ret == -1) {
+               perror("read");
+               return;
+       }
+       buf[ret] = 0;
+
+       eor = is_eor(buf);
+
+       token = strtok_r(buf, delim, &saveptr);
+       if (token == NULL)
+               return;
+       /* TODO: Only GET... */
+       token = strtok_r(NULL, delim, &saveptr);
+       if (token == NULL)
+               return;
+       bzero(&file, sizeof(file));
+       strncpy(file, token, sizeof(file)-1);
+
+       while (!eor) {
+               alarm(TIMEOUT);
+               ret=read(s, buf, sizeof(buf)-1);
+               if (ret == -1) {
+                       perror("read");
+                       return;
+               }
+               buf[ret] = 0;
+               eor = is_eor(buf);
+       }
+       alarm(0);
+
+       param = strchr(file, '?');
+       if (param) {
+               *param = 0;
+               param++;
+
+               while ((token = strchr(param, '%')) != NULL) {
+                       char buf[3];
+
+                       if (strlen(token+1) < 2)
+                               break;
+
+                       strncpy(buf, token+1, 3);
+                       buf[2] = 0;
+
+                       *token = (char)strtoul(buf, NULL, 16);
+                       memmove(token+1, token+3, strlen(token)-2);
+
+               }
+               while ((token = strchr(param, '+')) != NULL) {
+                       *token = ' ';
+               }
+       }
+
+       if (strcmp("/", file) == 0) {
+               serve_index(s, sc, param);
+       } else if (strcmp("/cgi-bin/lcd", file) == 0) {
+               serve_lcd(s, sc);
+       } else {
+               serve_404(s);
+       }
+}
+
+void sighandler(int sig)
+{
+       switch(sig) {
+               case SIGPIPE:
+                       break;
+               case SIGALRM:
+               default:
+                       printf("Signal %d received\n", sig);
+                       break;
+       }
+}
 
 int main(int argc, char **argv) 
 {
-       struct usb_dev_handle *sc;
+       struct sigaction act;
+       int sock, csock;
+       int opt;
+       socklen_t slen;
+       struct scope *sc;
+       struct sockaddr_in sin, clientsin;
+       unsigned short port = 8088;
+
+       sc = initscope();
+       if (sc == NULL) {
+               printf("Scope not found!\n");
+               exit(EXIT_FAILURE);
+       }
+
+       bzero(&act, sizeof(act));
+       act.sa_handler = sighandler;
+       act.sa_flags = SA_RESTART;
+       if (sigaction(SIGPIPE, &act, NULL) == -1) {
+               perror("sigaction");
+               exit(EXIT_FAILURE);
+       }
+
+       bzero(&act, sizeof(act));
+       act.sa_handler = sighandler;
+       if (sigaction(SIGALRM, &act, NULL) == -1) {
+               perror("sigaction");
+               exit(EXIT_FAILURE);
+       }
+
+       if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
+               perror("socket");
+               exit(EXIT_FAILURE);
+       }
+
+       if (argc == 2) {
+               port=atoi(argv[1]);
+       }
+
+       bzero(&sin, sizeof(sin));
+       sin.sin_addr.s_addr=htonl(INADDR_ANY);
+       sin.sin_family=AF_INET;
+       sin.sin_port=htons(port);
+
+       opt = 1;
+       setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt,sizeof(opt));
+
+       if (bind(sock, (struct sockaddr*)&sin, sizeof(sin)) == -1) {
+               perror("bind");
+               exit(EXIT_FAILURE);
+       }
+
+       listen(sock, 32);
+       printf("Listening on Port %u\n", port);
+
+       while(1) {
+               bzero(&clientsin, sizeof(clientsin));
+               slen = sizeof(clientsin);
+               if ((csock = accept(sock, (struct sockaddr*)&clientsin, &slen)) == -1) {
+                       perror("accept");
+               }
+
+               parse_request(csock, sc);
+
+               close(csock);
+       }
 
-       sc = usbtmc_initscope();
-       usbtmc_close(sc);
+       closescope(sc);
        return 0;
 }
Impressum, Datenschutz