Skip to content

Commit 301f3e0

Browse files
committed
Start of web server
1 parent a055113 commit 301f3e0

File tree

3 files changed

+62
-15
lines changed

3 files changed

+62
-15
lines changed

ports/espressif/common-hal/socketpool/SocketPool.c

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,26 @@ void common_hal_socketpool_socketpool_construct(socketpool_socketpool_obj_t *sel
4040
}
4141
}
4242

43+
void socketpool_socket(socketpool_socketpool_obj_t *self,
44+
socketpool_socketpool_addressfamily_t family, socketpool_socketpool_sock_t type
45+
socketpool_socket_obj_t *sock) {
46+
sock->type = socket_type;
47+
sock->family = addr_family;
48+
sock->ipproto = ipproto;
49+
sock->pool = self;
50+
sock->timeout_ms = (uint)-1;
51+
52+
// Create LWIP socket
53+
int socknum = -1;
54+
socknum = lwip_socket(sock->family, sock->type, sock->ipproto);
55+
if (socknum < 0 || !register_open_socket(sock)) {
56+
mp_raise_RuntimeError(translate("Out of sockets"));
57+
}
58+
sock->num = socknum;
59+
// Sockets should be nonblocking in most cases
60+
lwip_fcntl(socknum, F_SETFL, O_NONBLOCK);
61+
}
62+
4363
socketpool_socket_obj_t *common_hal_socketpool_socket(socketpool_socketpool_obj_t *self,
4464
socketpool_socketpool_addressfamily_t family, socketpool_socketpool_sock_t type) {
4565

@@ -68,21 +88,8 @@ socketpool_socket_obj_t *common_hal_socketpool_socket(socketpool_socketpool_obj_
6888

6989
socketpool_socket_obj_t *sock = m_new_obj_with_finaliser(socketpool_socket_obj_t);
7090
sock->base.type = &socketpool_socket_type;
71-
sock->type = socket_type;
72-
sock->family = addr_family;
73-
sock->ipproto = ipproto;
74-
sock->pool = self;
75-
sock->timeout_ms = (uint)-1;
7691

77-
// Create LWIP socket
78-
int socknum = -1;
79-
socknum = lwip_socket(sock->family, sock->type, sock->ipproto);
80-
if (socknum < 0 || !register_open_socket(sock)) {
81-
mp_raise_RuntimeError(translate("Out of sockets"));
82-
}
83-
sock->num = socknum;
84-
// Sockets should be nonblocking in most cases
85-
lwip_fcntl(socknum, F_SETFL, O_NONBLOCK);
92+
socketpool_socket(self, family, type, sock);
8693
return sock;
8794
}
8895

shared-bindings/socketpool/Socket.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(socketpool_socket_connect_obj, socketpool_socke
155155
//| def listen(self, backlog: int) -> None:
156156
//| """Set socket to listen for incoming connections
157157
//|
158-
//| :param ~int backlog: length of backlog queue for waiting connetions"""
158+
//| :param ~int backlog: length of backlog queue for waiting connections"""
159159
//| ...
160160
//|
161161
STATIC mp_obj_t socketpool_socket_listen(mp_obj_t self_in, mp_obj_t backlog_in) {

supervisor/shared/web_workflow/web_workflow.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040

4141
static wifi_radio_error_t wifi_status = WIFI_RADIO_ERROR_NONE;
4242

43+
static socketpool_socketpool_obj_t pool;
44+
static socketpool_socket_obj_t listening;
45+
4346
void supervisor_web_workflow_status(void) {
4447
serial_write_compressed(translate("Wi-Fi: "));
4548
if (common_hal_wifi_radio_get_enabled(&common_hal_wifi_radio_obj)) {
@@ -91,7 +94,44 @@ void supervisor_start_web_workflow(void) {
9194

9295
if (wifi_status != WIFI_RADIO_ERROR_NONE) {
9396
common_hal_wifi_radio_set_enabled(&common_hal_wifi_radio_obj, false);
97+
return;
9498
}
99+
100+
pool.base.type = &socketpool_socketpool_type;
101+
common_hal_socketpool_socketpool_construct(&pool, &common_hal_wifi_radio_obj);
102+
103+
listening.base.type = &socketpool_socket_type;
104+
socketpool_socket(&pool, SOCKETPOOL_AF_INET, SOCKETPOOL_SOCK_STREAM, &listening);
105+
// Bind to any ip.
106+
// TODO: Make this port .env configurable.
107+
common_hal_socketpool_socket_bind(&listening, "", 0, 80);
108+
common_hal_socketpool_socket_listen(&listening, 1);
109+
110+
// Accept a connection and start parsing:
111+
// * HTTP method
112+
// * HTTP path
113+
// * Headers we care about:
114+
// * Authentication
115+
// * Must match CIRCUITPY_WEB_AUTH
116+
// * Host
117+
// * IP - ok
118+
// * cpy-mac.local - ok
119+
// * circuitpython.local - redirect
120+
// * Content-Length
121+
//
122+
// PUT /fs/<filename>
123+
// GET /fs/<filename>
124+
// - File contents
125+
// - JSON directory representation
126+
// GET /cp/devices.json
127+
// - JSON list of MDNS results
128+
// GET /cp/version.json
129+
// - JSON version info
130+
// GET /
131+
// - Super basic editor
132+
// GET /ws/circuitpython
133+
// GET /ws/user
134+
// - WebSockets
95135
#endif
96136
}
97137

0 commit comments

Comments
 (0)