Skip to content

Commit 8cfdfb9

Browse files
committed
Remove extra logging, auth /cp/serial and add doc
1 parent 557e354 commit 8cfdfb9

File tree

4 files changed

+50
-44
lines changed

4 files changed

+50
-44
lines changed

docs/workflows.md

Lines changed: 47 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,44 @@ not protected by basic auth in case the device is someone elses.
286286

287287
Only `GET` requests are supported and will return `405 Method Not Allowed` otherwise.
288288

289+
#### `/cp/devices.json`
290+
291+
Returns information about other devices found on the network using MDNS.
292+
293+
* `total`: Total MDNS response count. May be more than in `devices` if internal limits were hit.
294+
* `devices`: List of discovered devices.
295+
* `hostname`: MDNS hostname
296+
* `instance_name`: MDNS instance name. Defaults to human readable board name.
297+
* `port`: Port of CircuitPython Web API
298+
* `ip`: IP address
299+
300+
Example:
301+
```sh
302+
curl -v -L http://circuitpython.local/cp/devices.json
303+
```
304+
305+
```json
306+
{
307+
"total": 1,
308+
"devices": [
309+
{
310+
"hostname": "cpy-951032",
311+
"instance_name": "Adafruit Feather ESP32-S2 TFT",
312+
"port": 80,
313+
"ip": "192.168.1.235"
314+
}
315+
]
316+
}
317+
```
318+
319+
#### `/cp/serial/`
320+
321+
322+
Serves a basic serial terminal program when a `GET` request is received without the
323+
`Upgrade: websocket` header. Otherwise the socket is upgraded to a WebSocket. See WebSockets below for more detail.
324+
325+
This is an authenticated endpoint in both modes.
326+
289327
#### `/cp/version.json`
290328

291329
Returns information about the device.
@@ -323,36 +361,6 @@ curl -v -L http://circuitpython.local/cp/version.json
323361
}
324362
```
325363

326-
#### `/cp/devices.json`
327-
328-
Returns information about other devices found on the network using MDNS.
329-
330-
* `total`: Total MDNS response count. May be more than in `devices` if internal limits were hit.
331-
* `devices`: List of discovered devices.
332-
* `hostname`: MDNS hostname
333-
* `instance_name`: MDNS instance name. Defaults to human readable board name.
334-
* `port`: Port of CircuitPython Web API
335-
* `ip`: IP address
336-
337-
Example:
338-
```sh
339-
curl -v -L http://circuitpython.local/cp/devices.json
340-
```
341-
342-
```json
343-
{
344-
"total": 1,
345-
"devices": [
346-
{
347-
"hostname": "cpy-951032",
348-
"instance_name": "Adafruit Feather ESP32-S2 TFT",
349-
"port": 80,
350-
"ip": "192.168.1.235"
351-
}
352-
]
353-
}
354-
```
355-
356364
### Static files
357365

358366
* `/favicon.ico` - Blinka
@@ -361,4 +369,12 @@ curl -v -L http://circuitpython.local/cp/devices.json
361369

362370
### WebSocket
363371

364-
Coming soon!
372+
The CircuitPython serial interactions are available over a WebSocket. A WebSocket begins as a
373+
special HTTP request that gets upgraded to a WebSocket. Authentication happens before upgrading.
374+
375+
WebSockets are *not* bare sockets once upgraded. Instead they have their own framing format for data.
376+
CircuitPython can handle PING and CLOSE opcodes. All others are treated as TEXT. Data to
377+
CircuitPython is expected to be masked UTF-8, as the spec requires. Data from CircuitPython to the
378+
client is unmasked. It is also unbuffered so the client will get a variety of frame sizes.
379+
380+
Only one WebSocket at a time is supported.

supervisor/shared/web_workflow/static/welcome.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
<head>
44
<title>CircuitPython</title>
55
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1">
67
<script src="/welcome.js" defer=true></script>
78
</head>
89
<body>
910
<h1><a href="/"><img src="/favicon.ico"/></a>&nbsp;Welcome!</h1>
10-
Welcome to CircuitPython's Web API. Go to the <a href="/fs/">file browser</a> to work with files in the CIRCUITPY drive. Make sure you've set <code>CIRCUITPY_WEB_API_PASSWORD='somepassword'</code> in <code>/.env</code>. Provide the password when the browser prompts for it. Leave the username blank.
11+
Welcome to CircuitPython's Web API. Go to the <a href="/fs/">file browser</a> to work with files in the CIRCUITPY drive. Go to the <a href="/cp/serial/">serial terminal</a> to see code output and interact with the REPL. Make sure you've set <code>CIRCUITPY_WEB_API_PASSWORD='somepassword'</code> in <code>/.env</code>. Provide the password when the browser prompts for it. Leave the username blank.
1112
<h2>Device Info</h2>
1213
Board: <a id="board"></a><br>
1314
Version: <span id="version"></span><br>

supervisor/shared/web_workflow/web_workflow.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,14 +1019,13 @@ static bool _reply(socketpool_socket_obj_t *socket, _request *request) {
10191019
} else if (strcmp(path, "/version.json") == 0) {
10201020
_reply_with_version_json(socket, request);
10211021
} else if (strcmp(path, "/serial/") == 0) {
1022-
if (false && !request->authenticated) {
1022+
if (!request->authenticated) {
10231023
if (_api_password[0] != '\0') {
10241024
_reply_unauthorized(socket, request);
10251025
} else {
10261026
_reply_forbidden(socket, request);
10271027
}
10281028
} else if (request->websocket) {
1029-
ESP_LOGI(TAG, "websocket!");
10301029
_reply_websocket_upgrade(socket, request);
10311030
} else {
10321031
_REPLY_STATIC(socket, request, serial_html);
@@ -1059,7 +1058,6 @@ static bool _reply(socketpool_socket_obj_t *socket, _request *request) {
10591058
}
10601059

10611060
static void _reset_request(_request *request) {
1062-
ESP_LOGI(TAG, "reset request");
10631061
request->state = STATE_METHOD;
10641062
request->origin[0] = '\0';
10651063
request->content_length = 0;

supervisor/shared/web_workflow/websocket.c

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ void websocket_init(void) {
4848
}
4949

5050
void websocket_handoff(socketpool_socket_obj_t *socket) {
51-
ESP_LOGI(TAG, "socket handed off");
5251
cp_serial.socket = *socket;
5352
cp_serial.closed = false;
5453
cp_serial.opcode = 0;
@@ -57,7 +56,6 @@ void websocket_handoff(socketpool_socket_obj_t *socket) {
5756
// Mark the original socket object as closed without telling the lower level.
5857
socket->connected = false;
5958
socket->num = -1;
60-
ESP_LOGI(TAG, "socket hand off done");
6159
}
6260

6361
bool websocket_connected(void) {
@@ -90,7 +88,6 @@ static void _read_next_frame_header(void) {
9088
if (cp_serial.frame_index == 0 && _read_byte(&h)) {
9189
cp_serial.frame_index++;
9290
cp_serial.opcode = h & 0xf;
93-
ESP_LOGI(TAG, "fin %d opcode %x", h >> 7, cp_serial.opcode);
9491
}
9592
if (cp_serial.frame_index == 1 && _read_byte(&h)) {
9693
cp_serial.frame_index++;
@@ -108,8 +105,6 @@ static void _read_next_frame_header(void) {
108105
if (cp_serial.masked) {
109106
cp_serial.frame_len += 4;
110107
}
111-
112-
ESP_LOGI(TAG, "mask %d length %x", cp_serial.masked, len);
113108
}
114109
while (cp_serial.frame_index >= 2 &&
115110
cp_serial.frame_index < (cp_serial.payload_len_size + 2) &&
@@ -133,7 +128,6 @@ static void _read_next_frame_header(void) {
133128
if (cp_serial.frame_index == cp_serial.frame_len) {
134129
uint8_t opcode = 0x8; // CLOSE
135130
if (cp_serial.opcode == 0x9) {
136-
ESP_LOGI(TAG, "websocket ping");
137131
opcode = 0xA; // PONG
138132
} else {
139133
// Set the TCP socket to send immediately so that we send the payload back before
@@ -160,7 +154,6 @@ static void _read_next_frame_header(void) {
160154
if (cp_serial.payload_remaining == 0) {
161155
cp_serial.frame_index = 0;
162156
if (cp_serial.opcode == 0x8) {
163-
ESP_LOGI(TAG, "websocket closed");
164157
cp_serial.closed = true;
165158

166159
common_hal_socketpool_socket_close(&cp_serial.socket);
@@ -199,7 +192,6 @@ bool websocket_available(void) {
199192
char websocket_read_char(void) {
200193
uint8_t c;
201194
_read_next_payload_byte(&c);
202-
ESP_LOGI(TAG, "read %c", c);
203195
return c;
204196
}
205197

@@ -232,7 +224,6 @@ static void _websocket_send(_websocket *ws, const char *text, size_t len) {
232224
char copy[len];
233225
memcpy(copy, text, len);
234226
copy[len] = '\0';
235-
ESP_LOGI(TAG, "sent over websocket: %s", copy);
236227
}
237228

238229
void websocket_write(const char *text, size_t len) {

0 commit comments

Comments
 (0)