]> git.zerfleddert.de Git - rigol/blame - rigold.c
unlock keyboard on release
[rigol] / rigold.c
CommitLineData
ad9fbc05
MG
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>
659f6954
MG
8#include <usb.h>
9
10#include "usbtmc.h"
ad9fbc05
MG
11#include "commands.h"
12
13static 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
30static int send_text(int s, char *buf)
31{
32 return send_binary(s, buf, strlen(buf));
33}
34
35static 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
44static 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
66static 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);
77385d56
MG
84 if (token == NULL)
85 return;
ad9fbc05
MG
86 /* TODO: Only GET... */
87 token = strtok_r(NULL, delim, &saveptr);
77385d56
MG
88 if (token == NULL)
89 return;
ad9fbc05
MG
90 bzero(&file, sizeof(file));
91 strncpy(file, token, sizeof(file)-1);
92
93 do {
94 token = strtok_r(NULL, crlf, &saveptr);
95 /* TODO: FIXME */
96 #if 0
97 if (token == NULL) {
98 ret=read(s, buf, sizeof(buf)-1);
99 if (ret == -1) {
100 perror("read");
101 return;
102 }
103 buf[ret] = 0;
104 token = strtok_r(buf, crlf, &saveptr);
105 }
106 #endif
107 } while(token != NULL);
108
109 if (strcmp("/", file) == 0) {
110 serve_index(s);
111 } else if (strcmp("/lcd.png", file) == 0) {
112 serve_lcd(s, sc);
113 }
114}
659f6954
MG
115
116int main(int argc, char **argv)
117{
ad9fbc05
MG
118 int sock, csock;
119 int opt;
120 socklen_t slen;
659f6954 121 struct usb_dev_handle *sc;
ad9fbc05 122 struct sockaddr_in sin, clientsin;
77385d56 123 unsigned short port = 8088;
659f6954
MG
124
125 sc = usbtmc_initscope();
ad9fbc05
MG
126
127 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
128 perror("socket");
129 exit(EXIT_FAILURE);
130 }
131
132 if (argc == 2) {
133 port=atoi(argv[1]);
134 }
135
136 bzero(&sin, sizeof(sin));
137 sin.sin_addr.s_addr=htonl(INADDR_ANY);
138 sin.sin_family=AF_INET;
139 sin.sin_port=htons(port);
140
141 opt = 1;
142 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt,sizeof(opt));
143
144 if (bind(sock, (struct sockaddr*)&sin, sizeof(sin)) == -1) {
145 perror("bind");
146 exit(EXIT_FAILURE);
147 }
148
149 listen(sock, 32);
150 printf("Listening on Port %u\n", port);
151
152 while(1) {
153 bzero(&clientsin, sizeof(clientsin));
154 slen = sizeof(clientsin);
155 if ((csock = accept(sock, (struct sockaddr*)&clientsin, &slen)) == -1) {
156 perror("accept");
157 }
158
159 parse_request(csock, sc);
160
161 close(csock);
162 }
163
659f6954
MG
164 usbtmc_close(sc);
165 return 0;
166}
Impressum, Datenschutz