Skip to content

Add WebSocket at /cp/serial/ #6584

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jul 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/library/hashlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

.. module:: hashlib
:synopsis: hashing algorithms
:noindex:

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

Expand Down
78 changes: 47 additions & 31 deletions docs/workflows.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,44 @@ not protected by basic auth in case the device is someone elses.

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

#### `/cp/devices.json`

Returns information about other devices found on the network using MDNS.

* `total`: Total MDNS response count. May be more than in `devices` if internal limits were hit.
* `devices`: List of discovered devices.
* `hostname`: MDNS hostname
* `instance_name`: MDNS instance name. Defaults to human readable board name.
* `port`: Port of CircuitPython Web API
* `ip`: IP address

Example:
```sh
curl -v -L http://circuitpython.local/cp/devices.json
```

```json
{
"total": 1,
"devices": [
{
"hostname": "cpy-951032",
"instance_name": "Adafruit Feather ESP32-S2 TFT",
"port": 80,
"ip": "192.168.1.235"
}
]
}
```

#### `/cp/serial/`


Serves a basic serial terminal program when a `GET` request is received without the
`Upgrade: websocket` header. Otherwise the socket is upgraded to a WebSocket. See WebSockets below for more detail.

This is an authenticated endpoint in both modes.

#### `/cp/version.json`

Returns information about the device.
Expand Down Expand Up @@ -323,36 +361,6 @@ curl -v -L http://circuitpython.local/cp/version.json
}
```

#### `/cp/devices.json`

Returns information about other devices found on the network using MDNS.

* `total`: Total MDNS response count. May be more than in `devices` if internal limits were hit.
* `devices`: List of discovered devices.
* `hostname`: MDNS hostname
* `instance_name`: MDNS instance name. Defaults to human readable board name.
* `port`: Port of CircuitPython Web API
* `ip`: IP address

Example:
```sh
curl -v -L http://circuitpython.local/cp/devices.json
```

```json
{
"total": 1,
"devices": [
{
"hostname": "cpy-951032",
"instance_name": "Adafruit Feather ESP32-S2 TFT",
"port": 80,
"ip": "192.168.1.235"
}
]
}
```

### Static files

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

### WebSocket

Coming soon!
The CircuitPython serial interactions are available over a WebSocket. A WebSocket begins as a
special HTTP request that gets upgraded to a WebSocket. Authentication happens before upgrading.

WebSockets are *not* bare sockets once upgraded. Instead they have their own framing format for data.
CircuitPython can handle PING and CLOSE opcodes. All others are treated as TEXT. Data to
CircuitPython is expected to be masked UTF-8, as the spec requires. Data from CircuitPython to the
client is unmasked. It is also unbuffered so the client will get a variety of frame sizes.

Only one WebSocket at a time is supported.
4 changes: 4 additions & 0 deletions locale/circuitpython.pot
Original file line number Diff line number Diff line change
Expand Up @@ -2199,6 +2199,10 @@ msgstr ""
msgid "Unsupported format"
msgstr ""

#: shared-bindings/hashlib/__init__.c
msgid "Unsupported hash algorithm"
msgstr ""

#: ports/espressif/common-hal/dualbank/__init__.c
msgid "Update Failed"
msgstr ""
Expand Down
57 changes: 57 additions & 0 deletions ports/espressif/common-hal/hashlib/Hash.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2022 Scott Shawcroft for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#include "shared-bindings/hashlib/Hash.h"

#include "components/mbedtls/mbedtls/include/mbedtls/ssl.h"

void common_hal_hashlib_hash_update(hashlib_hash_obj_t *self, const uint8_t *data, size_t datalen) {
if (self->hash_type == MBEDTLS_SSL_HASH_SHA1) {
mbedtls_sha1_update_ret(&self->sha1, data, datalen);
return;
}
}

void common_hal_hashlib_hash_digest(hashlib_hash_obj_t *self, uint8_t *data, size_t datalen) {
if (datalen < common_hal_hashlib_hash_get_digest_size(self)) {
return;
}
if (self->hash_type == MBEDTLS_SSL_HASH_SHA1) {
// We copy the sha1 state so we can continue to update if needed or get
// the digest a second time.
mbedtls_sha1_context copy;
mbedtls_sha1_clone(&copy, &self->sha1);
mbedtls_sha1_finish_ret(&self->sha1, data);
mbedtls_sha1_clone(&self->sha1, &copy);
}
}

size_t common_hal_hashlib_hash_get_digest_size(hashlib_hash_obj_t *self) {
if (self->hash_type == MBEDTLS_SSL_HASH_SHA1) {
return 20;
}
return 0;
}
41 changes: 41 additions & 0 deletions ports/espressif/common-hal/hashlib/Hash.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2022 Scott Shawcroft for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#ifndef MICROPY_INCLUDED_ESPRESSIF_COMMON_HAL_HASHLIB_HASH_H
#define MICROPY_INCLUDED_ESPRESSIF_COMMON_HAL_HASHLIB_HASH_H

#include "components/mbedtls/mbedtls/include/mbedtls/sha1.h"

typedef struct {
mp_obj_base_t base;
union {
mbedtls_sha1_context sha1;
};
// Of MBEDTLS_SSL_HASH_*
uint8_t hash_type;
} hashlib_hash_obj_t;

#endif // MICROPY_INCLUDED_ESPRESSIF_COMMON_HAL_HASHLIB_HASH_H
40 changes: 40 additions & 0 deletions ports/espressif/common-hal/hashlib/__init__.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2022 Scott Shawcroft for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#include "shared-bindings/hashlib/__init__.h"

#include "components/mbedtls/mbedtls/include/mbedtls/ssl.h"


bool common_hal_hashlib_new(hashlib_hash_obj_t *self, const char *algorithm) {
if (strcmp(algorithm, "sha1") == 0) {
self->hash_type = MBEDTLS_SSL_HASH_SHA1;
mbedtls_sha1_init(&self->sha1);
mbedtls_sha1_starts_ret(&self->sha1);
return true;
}
return false;
}
1 change: 1 addition & 0 deletions ports/espressif/mpconfigport.mk
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ CIRCUITPY_COUNTIO ?= 1
CIRCUITPY_DUALBANK ?= 1
CIRCUITPY_FRAMEBUFFERIO ?= 1
CIRCUITPY_FREQUENCYIO ?= 1
CIRCUITPY_HASHLIB ?= 1
CIRCUITPY_IMAGECAPTURE ?= 1
CIRCUITPY_I2CPERIPHERAL ?= 1
CIRCUITPY_RGBMATRIX ?= 1
Expand Down
5 changes: 5 additions & 0 deletions py/circuitpy_defns.mk
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@ endif
ifeq ($(CIRCUITPY_GNSS),1)
SRC_PATTERNS += gnss/%
endif
ifeq ($(CIRCUITPY_HASHLIB),1)
SRC_PATTERNS += hashlib/%
endif
ifeq ($(CIRCUITPY_I2CPERIPHERAL),1)
SRC_PATTERNS += i2cperipheral/%
endif
Expand Down Expand Up @@ -419,6 +422,8 @@ SRC_COMMON_HAL_ALL = \
gnss/GNSS.c \
gnss/PositionFix.c \
gnss/SatelliteSystem.c \
hashlib/__init__.c \
hashlib/Hash.c \
i2cperipheral/I2CPeripheral.c \
i2cperipheral/__init__.c \
microcontroller/Pin.c \
Expand Down
5 changes: 4 additions & 1 deletion py/circuitpy_mpconfig.mk
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ CIRCUITPY_GETPASS ?= $(CIRCUITPY_FULL_BUILD)
CFLAGS += -DCIRCUITPY_GETPASS=$(CIRCUITPY_GETPASS)

ifeq ($(CIRCUITPY_DISPLAYIO),1)
CIRCUITPY_GIFIO ?= $(CIRCUITPY_FULL_BUILD)
CIRCUITPY_GIFIO ?= $(CIRCUITPY_CAMERA)
else
CIRCUITPY_GIFIO ?= 0
endif
Expand All @@ -242,6 +242,9 @@ CFLAGS += -DCIRCUITPY_GIFIO=$(CIRCUITPY_GIFIO)
CIRCUITPY_GNSS ?= 0
CFLAGS += -DCIRCUITPY_GNSS=$(CIRCUITPY_GNSS)

CIRCUITPY_HASHLIB ?= $(CIRCUITPY_WEB_WORKFLOW)
CFLAGS += -DCIRCUITPY_HASHLIB=$(CIRCUITPY_HASHLIB)

CIRCUITPY_I2CPERIPHERAL ?= $(CIRCUITPY_FULL_BUILD)
CFLAGS += -DCIRCUITPY_I2CPERIPHERAL=$(CIRCUITPY_I2CPERIPHERAL)

Expand Down
4 changes: 4 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@ pyelftools

# for stubs and annotations
adafruit-circuitpython-typing

# for web workflow minify
minify_html
jsmin
Loading