X-Git-Url: http://git.zerfleddert.de/cgi-bin/gitweb.cgi/rigol/blobdiff_plain/659f6954a480994415f72580df61635fba3d877f..7df76f49adf30ac260c99649678b084c9b6c282b:/rigold.c diff --git a/rigold.c b/rigold.c index b32c474..6e383b7 100644 --- a/rigold.c +++ b/rigold.c @@ -1,12 +1,222 @@ -#include +#include +#include +#include +#include +#include +#include +#include +#include +#include -#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 *buf) +{ + return send_binary(s, buf, strlen(buf)); +} + +static void serve_index(int s, struct scope *sc) +{ + send_text(s, "HTTP/1.0 200 OK\n"); + send_text(s, "Content-type: text/html\n\n"); + send_text(s, ""); + send_text(s, scope_idn(sc)); + send_text(s, "\n"); + send_text(s, "\n"); + send_text(s, "\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); +} + +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; + + 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); + + if (strcmp("/", file) == 0) { + serve_index(s, sc); + } else if (strcmp("/lcd.png", file) == 0) { + serve_lcd(s, sc); + } +} + +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; }