]> git.zerfleddert.de Git - rigol/blob - rigold.c
4d4d0b337e3374c1fcfa2450dca46e952d81bc34
[rigol] / rigold.c
1 #include <sys/types.h>
2 #include <sys/socket.h>
3 #include <arpa/inet.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <strings.h>
8 #include <usb.h>
9
10 #include "usbtmc.h"
11 #include "commands.h"
12
13 static int send_binary(int s, char *buf, int len)
14 {
15 int ret;
16
17 while(len > 0) {
18 ret = write(s, buf, len);
19 if (ret == -1) {
20 perror("write");
21 return ret;
22 }
23 buf += ret;
24 len -= ret;
25 }
26
27 return 0;
28 }
29
30 static int send_text(int s, char *buf)
31 {
32 return send_binary(s, buf, strlen(buf));
33 }
34
35 static void serve_index(int s)
36 {
37 send_text(s, "HTTP/1.0 200 OK");
38 send_text(s, "Content-type: text/html\n\n");
39 send_text(s, "<html><head><title>Rigol DS1000</title></head><body bgcolor=\"#ffffff\" text=\"#000000\">\n");
40 send_text(s, "<img src=\"/lcd.png\" height=\"234\" width=\"320\">\n");
41 send_text(s, "</body></html>\n");
42 }
43
44 static void serve_lcd(int s, struct usb_dev_handle *sc)
45 {
46 char buf[256];
47 int imglen;
48 unsigned char *png;
49
50 usbtmc_claim(sc);
51 png = get_lcd(sc, &imglen, 0);
52 usbtmc_release(sc);
53
54 if (png == NULL)
55 return;
56
57
58 send_text(s, "HTTP/1.0 200 OK");
59 send_text(s, "Content-type: image/png\n");
60 snprintf(buf, sizeof(buf), "Content-length: %u\n\n", imglen);
61 send_text(s, buf);
62 send_binary(s, (char*)png, imglen);
63 free(png);
64 }
65
66 static void parse_request(int s, struct usb_dev_handle *sc)
67 {
68 int ret;
69 char buf[1024];
70 char file[1024];
71 const char delim[] = " \t\x0d\x0a";
72 const char crlf[] = "\x0d\x0a";
73 char *saveptr;
74 char *token;
75
76 ret=read(s, buf, sizeof(buf)-1);
77 if (ret == -1) {
78 perror("read");
79 return;
80 }
81 buf[ret] = 0;
82
83 token = strtok_r(buf, delim, &saveptr);
84 /* TODO: Only GET... */
85 token = strtok_r(NULL, delim, &saveptr);
86 bzero(&file, sizeof(file));
87 strncpy(file, token, sizeof(file)-1);
88
89 do {
90 token = strtok_r(NULL, crlf, &saveptr);
91 /* TODO: FIXME */
92 #if 0
93 if (token == NULL) {
94 ret=read(s, buf, sizeof(buf)-1);
95 if (ret == -1) {
96 perror("read");
97 return;
98 }
99 buf[ret] = 0;
100 token = strtok_r(buf, crlf, &saveptr);
101 }
102 #endif
103 } while(token != NULL);
104
105 if (strcmp("/", file) == 0) {
106 serve_index(s);
107 } else if (strcmp("/lcd.png", file) == 0) {
108 serve_lcd(s, sc);
109 }
110 }
111
112 int main(int argc, char **argv)
113 {
114 int sock, csock;
115 int opt;
116 socklen_t slen;
117 struct usb_dev_handle *sc;
118 struct sockaddr_in sin, clientsin;
119 unsigned short port = 8888;
120
121 sc = usbtmc_initscope();
122
123 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
124 perror("socket");
125 exit(EXIT_FAILURE);
126 }
127
128 if (argc == 2) {
129 port=atoi(argv[1]);
130 }
131
132 bzero(&sin, sizeof(sin));
133 sin.sin_addr.s_addr=htonl(INADDR_ANY);
134 sin.sin_family=AF_INET;
135 sin.sin_port=htons(port);
136
137 opt = 1;
138 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt,sizeof(opt));
139
140 if (bind(sock, (struct sockaddr*)&sin, sizeof(sin)) == -1) {
141 perror("bind");
142 exit(EXIT_FAILURE);
143 }
144
145 listen(sock, 32);
146 printf("Listening on Port %u\n", port);
147
148 while(1) {
149 bzero(&clientsin, sizeof(clientsin));
150 slen = sizeof(clientsin);
151 if ((csock = accept(sock, (struct sockaddr*)&clientsin, &slen)) == -1) {
152 perror("accept");
153 }
154
155 parse_request(csock, sc);
156
157 close(csock);
158 }
159
160 usbtmc_close(sc);
161 return 0;
162 }
Impressum, Datenschutz