Skip to content
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
4 changes: 3 additions & 1 deletion components/jetkvm_screen/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ The menu reads Set up Wi-Fi, Regulatory and Hotspot Mode (all three on the Wi-Fi

On the Wi-Fi board, Menu > Hotspot Mode turns access point mode on and off (`jetkvm_screen_access_point.c`); the display and the web UI call the mode hotspot mode. The entry is on the menu in every state of the device and reads Hotspot Mode: Off or Hotspot Mode: On. A hold on it asks the handler of `jetkvm_screen_set_wifi_access_point_handler()` for the other state, and when the mode turns on the menu gives way to the home screen of the state. Until the access point runs, the no-network screen reads Hotspot Mode and Starting. Then the home screen shows the access point as it shows a joined network: `192.168.4.1` as the address and the network name in the Wi-Fi row, whose icon is green while a device has joined. The header reads Hotspot in the place of the cloud state and the session count, and a row with a padlock below the Wi-Fi row shows the password. The Wi-Fi driver (`jetkvm_c5_wifi`) owns the mode.

While the access point runs, the strip of the home screen reads QR in the place of the dot beside the Down button, and only the other button opens the menu. The Down button puts a QR code in the place of the rows below the header: a white tile with the Wi-Fi network text `WIFI:T:WPA;S:<name>;P:<password>;;`, which a phone's camera reads to join the access point. The symbol has 33 modules of 4 px (LVGL's QR code library, `CONFIG_LV_USE_QRCODE` in `sdkconfig.wifi.defaults`) and a light margin of 16 px, or 11 px with the strip at the top or the bottom, where the tile has less room. On the code the strip shows Back beside the Back button, which puts the rows back; the Down button does nothing. The code also closes when a device joins, because the user needs the address next, and when the access point stops.

While "Web interface through Tailscale only" is on, the Advanced menu starts with the entry Tailscale Only: On (`jetkvm_screen_tailscale_only.c`). A hold on it turns the setting off, with the RPC that the web UI sends, and the entry goes with the next state. The device does not restart. This is the way back for an owner whose Tailscale path stopped working. The entry is not on the page while the setting is off.

The button strip takes 22 px of the home screen's lines when it is on the left or right, so the IPv4 address has 194 px there and 216 px otherwise. `jetkvm_screen_home_address.c` draws the address in the layout's font (Inter Bold at 24 px) when it fits its line, and in Inter Bold at 21 px when it does not. Every address fits 194 px in the smaller font. On the Ethernet board, an IPv6 address (the `ipv6_mode` setting) is shown below it. An address that does not fit one line breaks after the colon that makes the two lines most equal in width, so an address of eight groups has four groups on each line, and four groups fit 194 px. A `::` stays whole on one line. The MAC address line has that room only without an IPv6 address, so the two are not shown together. The Wi-Fi board shows the network name in that place and no IPv6 address.
Expand All @@ -56,4 +58,4 @@ Firmware boots in H.264-only mode. SW3 (active low; GPIO17 on the Wi-Fi board, G

`components/jetkvm_screen/tests/run_host_tests.sh` runs the host tests. `components/jetkvm_screen/tests/render_ui_screens.sh <dir> wifi|ethernet` renders all twelve generated pages and, for `wifi`, the Regulatory page and the screens of access point mode to PPM (and PNG with ImageMagick) using the LVGL software renderer, with no hardware. The pages are laid out by the production button code, so each one has the button strip, and the home screen is also rendered with the widest IPv4 address and the strip at each edge.

`components/jetkvm_screen/tests/run_home_address_host_test.sh` lays out the home screen in the same way and fails when an IPv4 address crosses the end of its line or an IPv6 address does not fit its two lines or breaks at another colon, without the strip and with the strip at each edge. The same script runs `test_access_point_screen.c` on both boards: Menu > Hotspot Mode is the third entry of the Wi-Fi board's menu, a hold on it asks for the other state, and the name of the mode in the header and the password row of the home screen fit beside the strip at each edge with the widest name and password. Both scripts need cmake, ninja and `managed_components/lvgl__lvgl`.
`components/jetkvm_screen/tests/run_home_address_host_test.sh` lays out the home screen in the same way and fails when an IPv4 address crosses the end of its line or an IPv6 address does not fit its two lines or breaks at another colon, without the strip and with the strip at each edge. The same script runs `test_access_point_screen.c` on both boards: Menu > Hotspot Mode is the third entry of the Wi-Fi board's menu, a hold on it asks for the other state, and the name of the mode in the header and the password row of the home screen fit beside the strip at each edge with the widest name and password. While the access point runs, the Down button opens the QR code and the Back button closes it; the code is the symbol of the name and the password at 4 px a module, fits below the header and beside the strip at each edge, and closes when a device joins and when the access point stops. Both scripts need cmake, ninja and `managed_components/lvgl__lvgl`.
5 changes: 4 additions & 1 deletion components/jetkvm_screen/jetkvm_screen.c
Original file line number Diff line number Diff line change
Expand Up @@ -1111,9 +1111,12 @@ static void apply_state(const jetkvm_screen_state_t *state)
jetkvm_screen_buttons_set_wifi_setup(state->wifi_setup_menu || s_wifi_setup_requested,
state->wifi_setup_stoppable);
if (jetkvm_screen_access_point_apply(state->wifi_ap,
access_point ? state->wifi_ap_password : NULL)) {
access_point ? state->wifi_ap_ssid : NULL,
access_point ? state->wifi_ap_password : NULL,
state->wifi_ap_stations)) {
s_wifi_access_point_turned_on = true;
}
jetkvm_screen_buttons_set_access_point(jetkvm_screen_access_point_runs());

/* Match JetKVM display.go: the unfinished Access menu stays hidden. The
* Network entry is Set up Wi-Fi on the Wi-Fi board. */
Expand Down
130 changes: 127 additions & 3 deletions components/jetkvm_screen/jetkvm_screen_access_point.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,20 @@
#include "ui/styles.h"
#include "ui/ui.h"

static lv_obj_t *s_entry, *s_row;
static lv_obj_t *s_entry, *s_row, *s_qr_area;

#if JETKVM_BOARD_WIFI
static lv_obj_t *s_entry_label, *s_password;
static lv_obj_t *s_entry_label, *s_password, *s_qr_tile, *s_qr;
static bool s_enabled;
static uint8_t s_stations;
static char s_qr_text[JETKVM_SCREEN_ACCESS_POINT_QR_TEXT_CAPACITY];
#define COLOR_ICON 0x808080
/* The text for the device's own name and password is 45 bytes, which the QR
* code library puts in a version 4 symbol of 33 modules. At 4 px a module is
* about 0.5 mm on the panel. */
#define QR_SIZE (33 * 4)
/* The light margin that a reader needs around the symbol: 4 modules. */
#define QR_MARGIN (4 * 4)
/* The blue of the no-network screen's second line. */
#define COLOR_MODE 0x4f8cff

Expand Down Expand Up @@ -88,20 +96,96 @@ static void row_init(void)
lv_obj_set_style_text_color(s_password, lv_color_hex(0xffffff), LV_PART_MAIN);
lv_label_set_text(s_password, "");
}

/* The tile is the largest square that the area holds, up to the symbol with
* its whole margin: the area is lower when the button strip is at the top or
* the bottom. */
static void qr_area_resized(lv_event_t *event)
{
(void)event;
const int32_t side = LV_MIN(LV_MIN(lv_obj_get_content_width(s_qr_area),
lv_obj_get_content_height(s_qr_area)),
QR_SIZE + 2 * QR_MARGIN);
lv_obj_set_size(s_qr_tile, side, side);
}

/* A reader needs dark modules on a light ground, so the code is on a white
* tile. */
static void qr_init(void)
{
s_qr_area = plain(lv_obj_get_parent(objects.home_info_container), 0, 0, 0, 0);
lv_obj_set_width(s_qr_area, LV_PCT(100));
lv_obj_set_flex_grow(s_qr_area, 1);
lv_obj_add_flag(s_qr_area, LV_OBJ_FLAG_HIDDEN);
lv_obj_add_event_cb(s_qr_area, qr_area_resized, LV_EVENT_SIZE_CHANGED, NULL);

s_qr_tile = plain(s_qr_area, 0, 0, QR_SIZE, QR_SIZE);
lv_obj_center(s_qr_tile);
lv_obj_set_style_radius(s_qr_tile, 6, LV_PART_MAIN);
lv_obj_set_style_bg_color(s_qr_tile, lv_color_white(), LV_PART_MAIN);
lv_obj_set_style_bg_opa(s_qr_tile, LV_OPA_COVER, LV_PART_MAIN);

s_qr = lv_qrcode_create(s_qr_tile);
lv_qrcode_set_size(s_qr, QR_SIZE);
lv_obj_center(s_qr);
}

static char *qr_append(char *at, const char *end, const char *text, bool escape)
{
for (; *text != '\0' && at + 2 < end; ++text) {
if (escape && strchr("\\;,:\"", *text) != NULL) {
*at++ = '\\';
}
*at++ = *text;
}
*at = '\0';
return at;
}

static void qr_update(const char *ssid, const char *password)
{
char text[sizeof(s_qr_text)];
jetkvm_screen_access_point_qr_text(ssid, password, text);
if (strcmp(text, s_qr_text) == 0) {
return;
}
if (lv_qrcode_update(s_qr, text, (uint32_t)strlen(text)) == LV_RESULT_OK) {
memcpy(s_qr_text, text, sizeof(s_qr_text));
}
}
#endif /* JETKVM_BOARD_WIFI */

void jetkvm_screen_access_point_qr_text(const char *ssid, const char *password,
char text[JETKVM_SCREEN_ACCESS_POINT_QR_TEXT_CAPACITY])
{
text[0] = '\0';
#if JETKVM_BOARD_WIFI
const char *end = text + JETKVM_SCREEN_ACCESS_POINT_QR_TEXT_CAPACITY;
char *at = qr_append(text, end, "WIFI:T:WPA;S:", false);
at = qr_append(at, end, ssid, true);
at = qr_append(at, end, ";P:", false);
at = qr_append(at, end, password, true);
(void)qr_append(at, end, ";;", false);
#else
(void)ssid;
(void)password;
#endif
}

void jetkvm_screen_access_point_init(void)
{
#if JETKVM_BOARD_WIFI
if (s_entry != NULL) {
return;
}
row_init();
qr_init();
entry_init();
#endif
}

bool jetkvm_screen_access_point_apply(bool enabled, const char *password)
bool jetkvm_screen_access_point_apply(bool enabled, const char *ssid, const char *password,
uint8_t stations)
{
#if JETKVM_BOARD_WIFI
if (s_entry == NULL) {
Expand All @@ -116,6 +200,15 @@ bool jetkvm_screen_access_point_apply(bool enabled, const char *password)
if (shown && strcmp(lv_label_get_text(s_password), password) != 0) {
lv_label_set_text(s_password, password);
}
if (shown) {
qr_update(ssid != NULL ? ssid : "", password);
}
/* The code has done its work when a device joins: the user needs the
* address next. */
if (!shown || stations > s_stations) {
jetkvm_screen_access_point_qr_show(false);
}
s_stations = shown ? stations : 0;
if (shown == lv_obj_has_flag(s_row, LV_OBJ_FLAG_HIDDEN)) {
/* The header's right side names the mode. The cloud is out of reach
* from the access point's network, so its icon goes; the caller sets
Expand All @@ -135,11 +228,37 @@ bool jetkvm_screen_access_point_apply(bool enabled, const char *password)
return turned_on;
#else
(void)enabled;
(void)ssid;
(void)password;
(void)stations;
return false;
#endif
}

bool jetkvm_screen_access_point_runs(void)
{
return s_row != NULL && !lv_obj_has_flag(s_row, LV_OBJ_FLAG_HIDDEN);
}

void jetkvm_screen_access_point_qr_show(bool shown)
{
if (shown == jetkvm_screen_access_point_qr_shown() ||
(shown && !jetkvm_screen_access_point_runs())) {
return;
}
lv_obj_t *rows[] = {objects.home_info_container, objects.divider,
objects.home_status_container};
for (size_t i = 0; i < sizeof(rows) / sizeof(rows[0]); i++) {
lv_obj_set_flag(rows[i], LV_OBJ_FLAG_HIDDEN, shown);
}
lv_obj_set_flag(s_qr_area, LV_OBJ_FLAG_HIDDEN, !shown);
}

bool jetkvm_screen_access_point_qr_shown(void)
{
return s_qr_area != NULL && !lv_obj_has_flag(s_qr_area, LV_OBJ_FLAG_HIDDEN);
}

lv_obj_t *jetkvm_screen_access_point_entry(void)
{
return s_entry;
Expand All @@ -149,3 +268,8 @@ lv_obj_t *jetkvm_screen_access_point_password_row(void)
{
return s_row;
}

lv_obj_t *jetkvm_screen_access_point_qr_area(void)
{
return s_qr_area;
}
45 changes: 37 additions & 8 deletions components/jetkvm_screen/jetkvm_screen_access_point.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <stdbool.h>
#include <stdint.h>

#include "lvgl.h"

Expand All @@ -18,22 +19,50 @@
* what a joined network does not have. The header names the mode: it reads
* "Hotspot" in the place of the cloud state and the session count, which the
* caller leaves alone meanwhile. A row below the Wi-Fi row shows the
* password. All calls run with LVGL owned by the caller.
* password.
*
* The home screen can also show a QR code that a phone's camera reads to
* join the access point. The code takes the place of the rows below the
* header, which stays. The button code opens and closes it
* (jetkvm_screen_buttons.c), and it closes when a device joins. All calls run
* with LVGL owned by the caller.
*/

/* Room for the text of the QR code with the longest name, with each of its
* 32 bytes escaped, and the password. */
#define JETKVM_SCREEN_ACCESS_POINT_QR_TEXT_CAPACITY 120U

/* Call once after ui_init(), after jetkvm_screen_regulatory_init() (which
* orders the menu) and after the home screen has its Wi-Fi row. */
void jetkvm_screen_access_point_init(void);

/* `enabled`: the mode is on. `password`: the password while the access point
* runs, else NULL; the home screen names the mode and shows the row only with
* a password. In the state after the last one with a password, the caller
* sets the header's cloud state and session count again.
/* `enabled`: the mode is on. `ssid` and `password`: the name and the password
* while the access point runs, else NULL; the home screen names the mode and
* shows the row only with a password. `stations`: the devices that have
* joined; the QR code closes when one more has. In the state after the last
* one with a password, the caller sets the header's cloud state and session
* count again, and the QR code closes.
* Returns true when the mode turned on with this call, so that the caller
* leaves the menu for the home screen. */
bool jetkvm_screen_access_point_apply(bool enabled, const char *password);
bool jetkvm_screen_access_point_apply(bool enabled, const char *ssid, const char *password,
uint8_t stations);

/* The home screen shows the access point: its QR code can open. */
bool jetkvm_screen_access_point_runs(void);

/* Puts the QR code in the place of the home screen's rows, or the rows back.
* The code opens only while the access point runs. */
void jetkvm_screen_access_point_qr_show(bool shown);
bool jetkvm_screen_access_point_qr_shown(void);

/* What the QR code holds: the Wi-Fi network text that phone cameras read,
* WIFI:T:WPA;S:<name>;P:<password>;; with a backslash before each of
* \ ; , : " in the name and the password. Empty on the Ethernet board. */
void jetkvm_screen_access_point_qr_text(const char *ssid, const char *password,
char text[JETKVM_SCREEN_ACCESS_POINT_QR_TEXT_CAPACITY]);

/* The menu entry and the password row of the home screen; NULL on the
* Ethernet board, which has no access point. */
/* The menu entry, the password row of the home screen and the area of the QR
* code; NULL on the Ethernet board, which has no access point. */
lv_obj_t *jetkvm_screen_access_point_entry(void);
lv_obj_t *jetkvm_screen_access_point_password_row(void);
lv_obj_t *jetkvm_screen_access_point_qr_area(void);
Loading
Loading