Skip to content

Commit 2f9de1c

Browse files
authored
Merge pull request #6584 from tannewt/websocket_serial
Add WebSocket at /cp/serial/
2 parents f869a86 + 031c124 commit 2f9de1c

23 files changed

+952
-41
lines changed

docs/library/hashlib.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
.. module:: hashlib
77
:synopsis: hashing algorithms
8+
:noindex:
89

910
|see_cpython_module| :mod:`cpython:hashlib`.
1011

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.

locale/circuitpython.pot

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2199,6 +2199,10 @@ msgstr ""
21992199
msgid "Unsupported format"
22002200
msgstr ""
22012201

2202+
#: shared-bindings/hashlib/__init__.c
2203+
msgid "Unsupported hash algorithm"
2204+
msgstr ""
2205+
22022206
#: ports/espressif/common-hal/dualbank/__init__.c
22032207
msgid "Update Failed"
22042208
msgstr ""
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2022 Scott Shawcroft for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "shared-bindings/hashlib/Hash.h"
28+
29+
#include "components/mbedtls/mbedtls/include/mbedtls/ssl.h"
30+
31+
void common_hal_hashlib_hash_update(hashlib_hash_obj_t *self, const uint8_t *data, size_t datalen) {
32+
if (self->hash_type == MBEDTLS_SSL_HASH_SHA1) {
33+
mbedtls_sha1_update_ret(&self->sha1, data, datalen);
34+
return;
35+
}
36+
}
37+
38+
void common_hal_hashlib_hash_digest(hashlib_hash_obj_t *self, uint8_t *data, size_t datalen) {
39+
if (datalen < common_hal_hashlib_hash_get_digest_size(self)) {
40+
return;
41+
}
42+
if (self->hash_type == MBEDTLS_SSL_HASH_SHA1) {
43+
// We copy the sha1 state so we can continue to update if needed or get
44+
// the digest a second time.
45+
mbedtls_sha1_context copy;
46+
mbedtls_sha1_clone(&copy, &self->sha1);
47+
mbedtls_sha1_finish_ret(&self->sha1, data);
48+
mbedtls_sha1_clone(&self->sha1, &copy);
49+
}
50+
}
51+
52+
size_t common_hal_hashlib_hash_get_digest_size(hashlib_hash_obj_t *self) {
53+
if (self->hash_type == MBEDTLS_SSL_HASH_SHA1) {
54+
return 20;
55+
}
56+
return 0;
57+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2022 Scott Shawcroft for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef MICROPY_INCLUDED_ESPRESSIF_COMMON_HAL_HASHLIB_HASH_H
28+
#define MICROPY_INCLUDED_ESPRESSIF_COMMON_HAL_HASHLIB_HASH_H
29+
30+
#include "components/mbedtls/mbedtls/include/mbedtls/sha1.h"
31+
32+
typedef struct {
33+
mp_obj_base_t base;
34+
union {
35+
mbedtls_sha1_context sha1;
36+
};
37+
// Of MBEDTLS_SSL_HASH_*
38+
uint8_t hash_type;
39+
} hashlib_hash_obj_t;
40+
41+
#endif // MICROPY_INCLUDED_ESPRESSIF_COMMON_HAL_HASHLIB_HASH_H
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2022 Scott Shawcroft for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "shared-bindings/hashlib/__init__.h"
28+
29+
#include "components/mbedtls/mbedtls/include/mbedtls/ssl.h"
30+
31+
32+
bool common_hal_hashlib_new(hashlib_hash_obj_t *self, const char *algorithm) {
33+
if (strcmp(algorithm, "sha1") == 0) {
34+
self->hash_type = MBEDTLS_SSL_HASH_SHA1;
35+
mbedtls_sha1_init(&self->sha1);
36+
mbedtls_sha1_starts_ret(&self->sha1);
37+
return true;
38+
}
39+
return false;
40+
}

ports/espressif/mpconfigport.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ CIRCUITPY_COUNTIO ?= 1
1919
CIRCUITPY_DUALBANK ?= 1
2020
CIRCUITPY_FRAMEBUFFERIO ?= 1
2121
CIRCUITPY_FREQUENCYIO ?= 1
22+
CIRCUITPY_HASHLIB ?= 1
2223
CIRCUITPY_IMAGECAPTURE ?= 1
2324
CIRCUITPY_I2CPERIPHERAL ?= 1
2425
CIRCUITPY_RGBMATRIX ?= 1

py/circuitpy_defns.mk

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,9 @@ endif
207207
ifeq ($(CIRCUITPY_GNSS),1)
208208
SRC_PATTERNS += gnss/%
209209
endif
210+
ifeq ($(CIRCUITPY_HASHLIB),1)
211+
SRC_PATTERNS += hashlib/%
212+
endif
210213
ifeq ($(CIRCUITPY_I2CPERIPHERAL),1)
211214
SRC_PATTERNS += i2cperipheral/%
212215
endif
@@ -419,6 +422,8 @@ SRC_COMMON_HAL_ALL = \
419422
gnss/GNSS.c \
420423
gnss/PositionFix.c \
421424
gnss/SatelliteSystem.c \
425+
hashlib/__init__.c \
426+
hashlib/Hash.c \
422427
i2cperipheral/I2CPeripheral.c \
423428
i2cperipheral/__init__.c \
424429
microcontroller/Pin.c \

py/circuitpy_mpconfig.mk

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ CIRCUITPY_GETPASS ?= $(CIRCUITPY_FULL_BUILD)
233233
CFLAGS += -DCIRCUITPY_GETPASS=$(CIRCUITPY_GETPASS)
234234

235235
ifeq ($(CIRCUITPY_DISPLAYIO),1)
236-
CIRCUITPY_GIFIO ?= $(CIRCUITPY_FULL_BUILD)
236+
CIRCUITPY_GIFIO ?= $(CIRCUITPY_CAMERA)
237237
else
238238
CIRCUITPY_GIFIO ?= 0
239239
endif
@@ -242,6 +242,9 @@ CFLAGS += -DCIRCUITPY_GIFIO=$(CIRCUITPY_GIFIO)
242242
CIRCUITPY_GNSS ?= 0
243243
CFLAGS += -DCIRCUITPY_GNSS=$(CIRCUITPY_GNSS)
244244

245+
CIRCUITPY_HASHLIB ?= $(CIRCUITPY_WEB_WORKFLOW)
246+
CFLAGS += -DCIRCUITPY_HASHLIB=$(CIRCUITPY_HASHLIB)
247+
245248
CIRCUITPY_I2CPERIPHERAL ?= $(CIRCUITPY_FULL_BUILD)
246249
CFLAGS += -DCIRCUITPY_I2CPERIPHERAL=$(CIRCUITPY_I2CPERIPHERAL)
247250

requirements-dev.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,7 @@ pyelftools
2626

2727
# for stubs and annotations
2828
adafruit-circuitpython-typing
29+
30+
# for web workflow minify
31+
minify_html
32+
jsmin

0 commit comments

Comments
 (0)