-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
87 lines (70 loc) · 1.8 KB
/
Copy pathmain.cpp
File metadata and controls
87 lines (70 loc) · 1.8 KB
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
#include <string>
#include "mbed.h"
#include "SDFileSystem.h"
#include "EthernetInterface.h"
#include "htmlcontent.h"
#include "cmd.h"
#define RBFILE "/sd/test.rb"
#define ENABLE_DHCP
SDFileSystem sd(P8_5, P8_6, P8_3, P8_4, "sd");
Serial pc(USBTX, USBRX);
char i_buff[1024*3];
char o_buff[1024*3];
int mruby_argc = 2;
char *mruby_argv[] = {
"mruby",
RBFILE
};
int getRequestBody(char *request, char *buff, int *size)
{
string req(request);
int sp = req.find("Content-Length", 0);
int ep = req.find("\r\n", sp);
int len = 0;
sscanf(req.substr(sp, ep-sp).c_str(), "Content-Length: %d", &len);
sp = req.find("\r\n\r\n", 0) + 4;
string body = req.substr(sp, len);
*size = len;
sprintf(buff, body.c_str());
}
int writeScriptFile(char *buff, int size)
{
int i;
FILE *f = fopen(RBFILE, "w");
for (i=0; i < size; i++) {
fprintf(f, "%c", buff[i]);
}
fclose(f);
}
int main() {
EthernetInterface eth0;
printf("Init network interface...\n");
#ifdef ENABLE_DHCP
eth0.init();
#else
eth0.init("192.168.1.100", "255.255.255.0", "192.168.1.1");
#endif
eth0.connect();
printf("IP Address: %s\n", eth0.getIPAddress());
printf("done!\n\n");
TCPSocketServer server;
TCPSocketConnection conn;
server.bind(80);
server.listen();
while (true) {
server.accept(conn);
conn.receive(i_buff, sizeof(i_buff));
if (i_buff[0] == 'P') {
int size;
getRequestBody(i_buff, o_buff, &size);
writeScriptFile(o_buff, size);
cmd_mruby(mruby_argc, mruby_argv);
} else {
sprintf(o_buff, EDITPAGE);
}
conn.send(o_buff, sizeof(EDITPAGE));
conn.close();
}
server.close();
eth0.disconnect();
}