add small webserver "rigold", currently only serving the contents of the LCD
authorMichael Gernoth <michael@gernoth.net>
Sun, 6 Jun 2010 15:35:00 +0000 (17:35 +0200)
committerMichael Gernoth <michael@gernoth.net>
Sun, 6 Jun 2010 15:35:00 +0000 (17:35 +0200)
commands.c
commands.h
rigol.c
rigold.c
usbtmc.c
usbtmc.h

index 75e76d03d65f2a4a2b4095de2ca49233f42811da..83df9c46ae811a835dcb874558ca46fb0a716c69 100644 (file)
@@ -106,15 +106,17 @@ void do_get_buf (struct usb_dev_handle *sc)
        usbtmc_sendscpi (sc, ":TIM:OFFSET 0", NULL, 0);
 }
 
-static unsigned char* get_lcd(struct usb_dev_handle *sc, int *imglen)
+unsigned char* get_lcd(struct usb_dev_handle *sc, int *imglen, int keylock)
 {
        unsigned char screen[320*234];
        unsigned char *png;
        int l;
 
-       /* Hide "RMT" from screen */
-       l = usbtmc_sendscpi(sc, ":KEY:LOCK DISABLE", NULL, 0); 
-       usleep(20000);
+       if (keylock) {
+               /* Hide "RMT" from screen */
+               l = usbtmc_sendscpi(sc, ":KEY:LOCK DISABLE", NULL, 0); 
+               usleep(30000);
+       }
 
        l = usbtmc_sendscpi(sc, ":LCD:DATA?", screen, sizeof(screen));
 
@@ -137,7 +139,7 @@ void do_get_screen(struct usb_dev_handle *sc)
        int fd;
        pid_t display;
 
-       png = get_lcd(sc, &imglen);
+       png = get_lcd(sc, &imglen, 1);
        if (png == NULL) {
                perror("get_lcd");
                return;
@@ -186,7 +188,7 @@ void do_display_screen(struct usb_dev_handle *sc)
        int pipefd[2];
        pid_t display;
 
-       png = get_lcd(sc, &imglen);
+       png = get_lcd(sc, &imglen, 1);
        if (png == NULL) {
                perror("get_lcd");
                return;
index 9d20fe8320bce96de8a0d1eb7f0dad0e71114338..ab013dc27f08c430da893947441cc64ddb01abb3 100644 (file)
@@ -2,3 +2,4 @@ void do_plot (struct usb_dev_handle *sc);
 void do_get_buf (struct usb_dev_handle *sc);
 void do_get_screen(struct usb_dev_handle *sc);
 void do_display_screen(struct usb_dev_handle *sc);
+unsigned char* get_lcd(struct usb_dev_handle *sc, int *imglen, int keylock);
diff --git a/rigol.c b/rigol.c
index 6ae9d73dac9a3eb3f0d73b74f965e5ef7923a9dc..5dfec41cea9e586491cd021a272983d625331a7e 100644 (file)
--- a/rigol.c
+++ b/rigol.c
@@ -104,6 +104,7 @@ int main(int argc, char **argv)
 
        //Initialize scope
        sc = usbtmc_initscope();
+       usbtmc_claim(sc);
        buff = malloc (1024*1024); 
        if (buff == NULL) {
                perror("malloc");
@@ -165,6 +166,7 @@ int main(int argc, char **argv)
        //Disable keylock, so the user doesn't have to press the 'force'-button
        l=usbtmc_sendscpi(sc, ":KEY:LOCK DISABLE",NULL,0); 
 
+       usbtmc_release(sc);
        usbtmc_close(sc);
        return 0;
 }
index b32c474454a859b4be6a2188492bf9b6c436f9f9..4d4d0b337e3374c1fcfa2450dca46e952d81bc34 100644 (file)
--- a/rigold.c
+++ b/rigold.c
+#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 <usb.h>
 
 #include "usbtmc.h"
+#include "commands.h"
+
+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 *buf)
+{
+       return send_binary(s, buf, strlen(buf));
+}
+
+static void serve_index(int s)
+{
+       send_text(s, "HTTP/1.0 200 OK");
+       send_text(s, "Content-type: text/html\n\n");
+       send_text(s, "<html><head><title>Rigol DS1000</title></head><body bgcolor=\"#ffffff\" text=\"#000000\">\n");
+       send_text(s, "<img src=\"/lcd.png\" height=\"234\" width=\"320\">\n");
+       send_text(s, "</body></html>\n");
+}
+
+static void serve_lcd(int s, struct usb_dev_handle *sc)
+{
+       char buf[256];
+       int imglen;
+       unsigned char *png;
+
+       usbtmc_claim(sc);
+       png = get_lcd(sc, &imglen, 0);
+       usbtmc_release(sc);
+
+       if (png == NULL)
+               return;
+
+
+       send_text(s, "HTTP/1.0 200 OK");
+       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 parse_request(int s, struct usb_dev_handle *sc)
+{
+       int ret;
+       char buf[1024];
+       char file[1024];
+       const char delim[] = " \t\x0d\x0a";
+       const char crlf[] = "\x0d\x0a";
+       char *saveptr;
+       char *token;
+
+       ret=read(s, buf, sizeof(buf)-1);
+       if (ret == -1) {
+               perror("read");
+               return;
+       }
+       buf[ret] = 0;
+
+       token = strtok_r(buf, delim, &saveptr);
+       /* TODO: Only GET... */
+       token = strtok_r(NULL, delim, &saveptr);
+       bzero(&file, sizeof(file));
+       strncpy(file, token, sizeof(file)-1);
+
+       do {
+               token = strtok_r(NULL, crlf, &saveptr);
+               /* TODO: FIXME */
+               #if 0
+               if (token == NULL) {
+                       ret=read(s, buf, sizeof(buf)-1);
+                       if (ret == -1) {
+                               perror("read");
+                               return;
+                       }
+                       buf[ret] = 0;
+                       token = strtok_r(buf, crlf, &saveptr);
+               }
+               #endif
+       } while(token != NULL);
+
+       if (strcmp("/", file) == 0) {
+               serve_index(s);
+       } else if (strcmp("/lcd.png", file) == 0) {
+               serve_lcd(s, sc);
+       }
+}
 
 int main(int argc, char **argv) 
 {
+       int sock, csock;
+       int opt;
+       socklen_t slen;
        struct usb_dev_handle *sc;
+       struct sockaddr_in sin, clientsin;
+       unsigned short port = 8888;
 
        sc = usbtmc_initscope();
+
+       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);
+       }
+
        usbtmc_close(sc);
        return 0;
 }
index d652494ffe39ffa60d71f75a29e4142daa11b3e6..8d05b099ab2fdd91c6ce7f377e4f9ad43de60497 100644 (file)
--- a/usbtmc.c
+++ b/usbtmc.c
@@ -103,6 +103,16 @@ int usbtmc_sendscpi(usb_dev_handle *dev, char* cmd,
        return 0;
 }
 
+void usbtmc_claim(usb_dev_handle *sc)
+{
+       usb_claim_interface(sc, 0);
+}
+
+void usbtmc_release(usb_dev_handle *sc)
+{
+       usb_release_interface(sc, 0);
+}
+
 //Initialize the scope.
 usb_dev_handle* usbtmc_initscope(void) {
        int r;
@@ -119,10 +129,11 @@ usb_dev_handle* usbtmc_initscope(void) {
        } else {
                printf("Scope found.\n");
        }
-       usb_claim_interface(dev,0);
+       usbtmc_claim(dev);
        //The following code isn't really necessary, the program works
        //OK without it too.
        r=usb_control_msg(dev, 0xC8, 9, 0, 0, (char*)buff, 4, 1000);
+       usbtmc_release(dev);
        if (r < 0) {
                fprintf (stderr, "Error %d sending init message: %s\n", 
                                r, strerror (-r));
@@ -138,6 +149,5 @@ usb_dev_handle* usbtmc_initscope(void) {
 void usbtmc_close(usb_dev_handle *sc)
 {
        //Free up and exit
-       usb_release_interface(sc,0);
        usb_close(sc);
 }
index 175511e8926fa69247ca13540c42bcc614a8dc98..a5ee8c80fb75a70b0733ae73fb643d74774f5ff3 100644 (file)
--- a/usbtmc.h
+++ b/usbtmc.h
@@ -1,3 +1,5 @@
 int usbtmc_sendscpi(usb_dev_handle *dev, char* cmd, unsigned char *resp, int resplen);
 usb_dev_handle* usbtmc_initscope(void);
 void usbtmc_close(usb_dev_handle *sc);
+void usbtmc_claim(usb_dev_handle *sc);
+void usbtmc_release(usb_dev_handle *sc);
Impressum, Datenschutz