forked from jbentham/picowi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
web_server.c
333 lines (303 loc) · 9.83 KB
/
web_server.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
// PicoWi Web server, see https://iosoft.blog/picowi
//
// Copyright (c) 2022, Jeremy P Bentham
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// v0.38 JPB 1/5/23 Adapted for iosoft.blog post
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include "lib/picowi_defs.h"
#include "lib/picowi_pico.h"
#include "lib/picowi_wifi.h"
#include "lib/picowi_init.h"
#include "lib/picowi_ioctl.h"
#include "lib/picowi_event.h"
#include "lib/picowi_join.h"
#include "lib/picowi_ip.h"
#include "lib/picowi_dhcp.h"
#include "lib/picowi_net.h"
#include "lib/picowi_tcp.h"
#include "lib/picowi_web.h"
// The hard-coded password is for test purposes only!!!
#define SSID "testnet"
#define PASSWD "testpass"
#define EVENT_POLL_USEC 100000
#define PING_RESP_USEC 200000
#define PING_DATA_SIZE 32
#define TEST_BLOCK_COUNT 100
const char hexchar[] = "0123456789ABCDEF";
const char b64chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
extern IPADDR my_ip, router_ip;
extern int dhcp_complete;
extern NET_SOCKET net_sockets[NUM_NET_SOCKETS];
BYTE testdata[256 * 3];
char testblock[256 * 5 + 3];
char temps[2000];
typedef struct {
char name[16];
int val;
} SERVER_ARG;
#define NUM_STATES 6
typedef enum {
STATE_IDLE,
STATE_READY,
STATE_PRELOAD,
STATE_PRETRIG,
STATE_POSTTRIG,
STATE_UPLOAD
} STATE_VALS;
SERVER_ARG server_args[] = {
{ "state", STATE_IDLE },
{ "nsamp", 0 },
{ "xsamp", 10000 },
{ "xrate", 100000 },
{ "" }
};
int web_status_handler(int sock, char *req, int oset);
int web_json_status(char *buff, int maxlen);
int web_root_handler(int sock, char *req, int oset);
int web_data_handler(int sock, char *req, int oset);
int web_bin_data_handler(int sock, char *req, int oset);
int web_favicon_handler(int sock, char *req, int oset);
void make_testdata(BYTE *data, int len);
void make_testblock(char *s, int len);
int base64_encode(char *out, unsigned char *in, int ilen);
int main()
{
uint32_t led_ticks;
bool ledon = false;
int server_sock;
struct sockaddr_in server_addr;
//set_display_mode(DISP_INFO | DISP_JOIN | DISP_TCP);
set_display_mode(DISP_INFO | DISP_JOIN | DISP_TCP_STATE);
io_init();
usdelay(1000);
make_testdata(testdata, sizeof(testdata));
make_testblock(testblock, sizeof(testblock));
if (net_init() && net_join(SSID, PASSWD))
{
server_sock = socket(AF_INET, SOCK_STREAM, 0);
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(HTTPORT);
if (server_sock < 0 ||
bind(server_sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0 ||
listen(server_sock, 3) < 0)
{
perror("socket/bind/listen failed");
return (1);
}
printf("Web server on port %u\n", HTTPORT);
web_page_handler("GET /favicon.ico", web_favicon_handler);
web_page_handler("GET /status.txt", web_status_handler);
web_page_handler("GET /data.txt", web_data_handler);
web_page_handler("GET /data.bin", web_bin_data_handler);
web_page_handler("GET /", web_root_handler);
ustimeout(&led_ticks, 0);
while (1)
{
// Get any events, poll the network-join state machine
net_event_poll();
net_state_poll();
tcp_socks_poll();
// Toggle LED at 0.5 Hz if joined, 5 Hz if not
if (ustimeout(&led_ticks, link_check() > 0 ? 1000000 : 100000))
wifi_set_led(ledon = !ledon);
}
}
}
// Handler for root Web page
int web_root_handler(int sock, char *req, int oset)
{
static int count = 1;
int n = 0;
if (req)
{
printf("TCP socket %d Rx %s\n", sock, req);
sprintf(temps, "<html><pre>Test %u\n</pre></html>", count++);
n = web_resp_add_str(sock,
HTTP_200_OK HTTP_SERVER HTTP_NOCACHE HTTP_CONNECTION_CLOSE
HTTP_CONTENT_HTML HTTP_HEADER_END) +
web_resp_add_str(sock, temps);
tcp_sock_close(sock);
}
return (n);
}
// Handler Web status page
int web_status_handler(int sock, char *req, int oset)
{
int n;
web_json_status(temps, sizeof(temps) - 1);
n = web_resp_add_str(sock,
HTTP_200_OK HTTP_SERVER HTTP_NOCACHE HTTP_ORIGIN_ANY
HTTP_CONTENT_HTML HTTP_CONNECTION_CLOSE HTTP_HEADER_END) +
web_resp_add_str(sock, temps);
tcp_sock_close(sock);
return (n);
}
// Return server status as json string
int web_json_status(char *buff, int maxlen)
{
SERVER_ARG *arg = server_args;
int n = sprintf(buff, "{");
while (arg->name[0] && n < maxlen - 20)
{
n += sprintf(&buff[n], "%s\"%s\":%d", n > 2 ? "," : "", arg->name, arg->val);
arg++;
}
return (n += sprintf(&buff[n], "}"));
}
// Handler for test data Web page
int web_data_handler(int sock, char *req, int oset)
{
int n = 0, diff;
static int dlen = 0, count = 0, last_oset = 0, startime;
NET_SOCKET *ts = &net_sockets[sock];
if (req)
{
printf("\nTCP socket %d Rx %s\n", sock, strtok(req, "\n"));
dlen = base64_encode(temps, testdata, sizeof(testdata));
n = web_resp_add_str(sock,
HTTP_200_OK HTTP_SERVER HTTP_NOCACHE HTTP_ORIGIN_ANY
HTTP_CONTENT_TEXT HTTP_CONNECTION_CLOSE HTTP_HEADER_END);
n += web_resp_add_data(sock, (BYTE *)temps, dlen);
count = last_oset = 0;
startime = ustime();
}
else
{
n = web_resp_add_data(sock, (BYTE *)temps, dlen);
if (oset - last_oset > 0)
{
last_oset = oset;
count++;
}
if (count >= TEST_BLOCK_COUNT)
{
tcp_sock_close(sock);
diff = ustime() - startime;
printf("%u bytes in %u usec, %u kbyte/s, %u errors\n",
count*sizeof(testblock),
diff,
(count*sizeof(testblock) * 1000)/diff,
ts->errors);
}
}
return (n);
}
// Handler for binary test data Web page
int web_bin_data_handler(int sock, char *req, int oset)
{
int n = 0, diff;
static int count = 0, last_oset = 0, startime;
NET_SOCKET *ts = &net_sockets[sock];
if (req)
{
printf("\nTCP socket %d Rx %s\n", sock, strtok(req, "\n"));
n = web_resp_add_str(sock,
HTTP_200_OK HTTP_SERVER HTTP_NOCACHE HTTP_ORIGIN_ANY
HTTP_CONTENT_BINARY);
n += web_resp_add_content_len(sock, TEST_BLOCK_COUNT * sizeof(testdata));
n += web_resp_add_str(sock, HTTP_CONNECTION_CLOSE HTTP_HEADER_END);
n += web_resp_add_data(sock, testdata, sizeof(testdata));
count = last_oset = 0;
startime = ustime();
}
else
{
n = web_resp_add_data(sock, testdata, sizeof(testdata));
if (oset - last_oset > 0)
{
last_oset = oset;
count++;
}
if (count >= TEST_BLOCK_COUNT)
{
tcp_sock_close(sock);
diff = ustime() - startime;
printf("%u bytes in %u usec, %u kbyte/s, %u errors\n",
count*sizeof(testdata),
diff,
(count*sizeof(testdata) * 1000)/diff,
ts->errors);
}
}
return (n);
}
// Handler for favicon
int web_favicon_handler(int sock, char *req, int oset)
{
int n = 0;
if (req)
{
printf("\nTCP socket %d Rx %s\n", sock, strtok(req, "\n"));
n = web_resp_add_str(sock, HTTP_404_FAIL HTTP_SERVER HTTP_CONTENT_HTML HTTP_HEADER_END);
tcp_sock_close(sock);
}
return (n);
}
// Make data block
void make_testdata(BYTE *data, int len)
{
int n;
for (n = 0; n < len; n++)
data[n] = n & 1 ? (BYTE)(~n >> 1) : (BYTE)(n >> 1);
}
// Make test block
void make_testblock(char *s, int len)
{
int n;
for (n = 0; n < (len - 1) / 3; n++)
{
*s++ = hexchar[(n >> 4) & 15];
*s++ = hexchar[n & 15];
*s++ = ' ';
}
*s++ = '\n';
*s++ = 0;
}
// Encode binry block as base64 string
int base64_encode(char *out, unsigned char *in, int ilen)
{
int olen, i, j, v;
olen = ilen % 3 ? ilen + 3 - ilen % 3 : ilen;
olen = (olen / 3) * 4;
out[olen] = '\0';
for (i = 0, j = 0; i < ilen; i += 3, j += 4)
{
v = in[i];
v = i + 1 < ilen ? v << 8 | in[i + 1] : v << 8;
v = i + 2 < ilen ? v << 8 | in[i + 2] : v << 8;
out[j] = b64chars[(v >> 18) & 0x3F];
out[j + 1] = b64chars[(v >> 12) & 0x3F];
if (i + 1 < ilen)
out[j + 2] = b64chars[(v >> 6) & 0x3F];
else
out[j + 2] = '=';
if (i + 2 < ilen)
out[j + 3] = b64chars[v & 0x3F];
else
out[j + 3] = '=';
}
return (olen);
}
// EOF