tokens can be NULL
[rigol] / rigold.c
index b32c474454a859b4be6a2188492bf9b6c436f9f9..0be124f1147c877e81321983c9517eedc6530c2e 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);
+       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);
+
+       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 = 8088;
 
        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;
 }
Impressum, Datenschutz