Start of web server

This commit is contained in:
Scott Shawcroft 2022-06-09 16:59:24 -07:00
parent a055113ed5
commit 301f3e0456
No known key found for this signature in database
GPG Key ID: 0DFD512649C052DA
3 changed files with 62 additions and 15 deletions

View File

@ -40,6 +40,26 @@ void common_hal_socketpool_socketpool_construct(socketpool_socketpool_obj_t *sel
}
}
void socketpool_socket(socketpool_socketpool_obj_t *self,
socketpool_socketpool_addressfamily_t family, socketpool_socketpool_sock_t type
socketpool_socket_obj_t *sock) {
sock->type = socket_type;
sock->family = addr_family;
sock->ipproto = ipproto;
sock->pool = self;
sock->timeout_ms = (uint)-1;
// Create LWIP socket
int socknum = -1;
socknum = lwip_socket(sock->family, sock->type, sock->ipproto);
if (socknum < 0 || !register_open_socket(sock)) {
mp_raise_RuntimeError(translate("Out of sockets"));
}
sock->num = socknum;
// Sockets should be nonblocking in most cases
lwip_fcntl(socknum, F_SETFL, O_NONBLOCK);
}
socketpool_socket_obj_t *common_hal_socketpool_socket(socketpool_socketpool_obj_t *self,
socketpool_socketpool_addressfamily_t family, socketpool_socketpool_sock_t type) {
@ -68,21 +88,8 @@ socketpool_socket_obj_t *common_hal_socketpool_socket(socketpool_socketpool_obj_
socketpool_socket_obj_t *sock = m_new_obj_with_finaliser(socketpool_socket_obj_t);
sock->base.type = &socketpool_socket_type;
sock->type = socket_type;
sock->family = addr_family;
sock->ipproto = ipproto;
sock->pool = self;
sock->timeout_ms = (uint)-1;
// Create LWIP socket
int socknum = -1;
socknum = lwip_socket(sock->family, sock->type, sock->ipproto);
if (socknum < 0 || !register_open_socket(sock)) {
mp_raise_RuntimeError(translate("Out of sockets"));
}
sock->num = socknum;
// Sockets should be nonblocking in most cases
lwip_fcntl(socknum, F_SETFL, O_NONBLOCK);
socketpool_socket(self, family, type, sock);
return sock;
}

View File

@ -155,7 +155,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(socketpool_socket_connect_obj, socketpool_socke
//| def listen(self, backlog: int) -> None:
//| """Set socket to listen for incoming connections
//|
//| :param ~int backlog: length of backlog queue for waiting connetions"""
//| :param ~int backlog: length of backlog queue for waiting connections"""
//| ...
//|
STATIC mp_obj_t socketpool_socket_listen(mp_obj_t self_in, mp_obj_t backlog_in) {

View File

@ -40,6 +40,9 @@
static wifi_radio_error_t wifi_status = WIFI_RADIO_ERROR_NONE;
static socketpool_socketpool_obj_t pool;
static socketpool_socket_obj_t listening;
void supervisor_web_workflow_status(void) {
serial_write_compressed(translate("Wi-Fi: "));
if (common_hal_wifi_radio_get_enabled(&common_hal_wifi_radio_obj)) {
@ -91,7 +94,44 @@ void supervisor_start_web_workflow(void) {
if (wifi_status != WIFI_RADIO_ERROR_NONE) {
common_hal_wifi_radio_set_enabled(&common_hal_wifi_radio_obj, false);
return;
}
pool.base.type = &socketpool_socketpool_type;
common_hal_socketpool_socketpool_construct(&pool, &common_hal_wifi_radio_obj);
listening.base.type = &socketpool_socket_type;
socketpool_socket(&pool, SOCKETPOOL_AF_INET, SOCKETPOOL_SOCK_STREAM, &listening);
// Bind to any ip.
// TODO: Make this port .env configurable.
common_hal_socketpool_socket_bind(&listening, "", 0, 80);
common_hal_socketpool_socket_listen(&listening, 1);
// Accept a connection and start parsing:
// * HTTP method
// * HTTP path
// * Headers we care about:
// * Authentication
// * Must match CIRCUITPY_WEB_AUTH
// * Host
// * IP - ok
// * cpy-mac.local - ok
// * circuitpython.local - redirect
// * Content-Length
//
// PUT /fs/<filename>
// GET /fs/<filename>
// - File contents
// - JSON directory representation
// GET /cp/devices.json
// - JSON list of MDNS results
// GET /cp/version.json
// - JSON version info
// GET /
// - Super basic editor
// GET /ws/circuitpython
// GET /ws/user
// - WebSockets
#endif
}