Files
desklock/firmware/main/gw_client.c
T
jpmschweitzerandClaude Fable 5 5c6a6eba36
Test, Build and Push / test-gateway (push) Successful in 10s
Test, Build and Push / release (push) Skipped
Test, Build and Push / build-gateway (push) Skipped
Fix touch crash + make rain a reachability signal + calm the alarm churn
TOUCH CRASH (the blue screen): app_on_touch ran a blocking WebSocket
send (up to 5s) directly in the LVGL touch callback, stalling the
MIPI-DSI flush into a garbage/blue frame + task-watchdog reboot on
every tap. Now touch_cb only gives a semaphore; a dedicated app_task
does the blocking sends, audio, and face changes off the render
thread. WS send timeout cut 5s to 1.5s as belt-and-braces.

RAIN = REACHABILITY (user request): rain now falls only while the
gateway WebSocket is live (gated on gw_connected in rain_tick). It
drains gracefully on disconnect, resumes on reconnect, a genuine
glanceable reachable signal. Idle density bumped 2 to 4 so
connected-idle reads distinctly from disconnected-black.

CALM THE WEDGE CHURN (user request): transient wifi/WS drops no longer
slam to the x_x error face or a CONNECTING banner. Boot goes straight
to the calm idle face (dry until connected). Only a sustained 30s+
outage escalates to x_x (clock_cb); the ~15s wedge-recovery just shows
a brief rain pause.

Two fixes from adversarial concurrency review before flashing:
- persistent single capture task (was xTaskCreate per utterance; a
  rapid re-tap or WS-stop-vs-app-start race could put two readers on
  one mic/I2S handle and corrupt the codec)
- reset s_talking on disconnect (app_on_disconnect) so the first tap
  after a mid-utterance drop starts fresh, not the stop branch

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-15 09:04:57 +02:00

136 lines
4.1 KiB
C

/* WebSocket link to the DeskLock gateway.
* Protocol: docs/architecture.md "WebSocket protocol" — keep in sync.
*/
#include <string.h>
#include "cJSON.h"
#include "esp_app_desc.h"
#include "esp_log.h"
#include "esp_websocket_client.h"
#include "desklock.h"
#include "secrets.h"
static const char *TAG = "gw";
static esp_websocket_client_handle_t s_ws;
static volatile bool s_connected;
static void handle_text(const char *data, int len)
{
cJSON *root = cJSON_ParseWithLength(data, len);
if (root == NULL) {
return;
}
const cJSON *type = cJSON_GetObjectItem(root, "type");
if (!cJSON_IsString(type)) {
cJSON_Delete(root);
return;
}
face_activity();
if (strcmp(type->valuestring, "state") == 0) {
const cJSON *value = cJSON_GetObjectItem(root, "value");
if (cJSON_IsString(value)) {
if (strcmp(value->valuestring, "thinking") == 0) {
face_set(FACE_EFFORT);
} else if (strcmp(value->valuestring, "idle") == 0 && !audio_is_playing()) {
face_set(FACE_IDLE);
}
}
} else if (strcmp(type->valuestring, "transcript") == 0) {
const cJSON *text = cJSON_GetObjectItem(root, "text");
ESP_LOGI(TAG, "transcript: %s", cJSON_IsString(text) ? text->valuestring : "?");
} else if (strcmp(type->valuestring, "reply_text") == 0) {
const cJSON *text = cJSON_GetObjectItem(root, "text");
ESP_LOGI(TAG, "tatlock: %s", cJSON_IsString(text) ? text->valuestring : "?");
} else if (strcmp(type->valuestring, "audio_start") == 0) {
audio_playback_begin();
face_set(FACE_SPEAKING);
} else if (strcmp(type->valuestring, "audio_end") == 0) {
audio_playback_end();
} else if (strcmp(type->valuestring, "error") == 0) {
face_set(FACE_RAGE);
}
cJSON_Delete(root);
}
static void ws_event(void *arg, esp_event_base_t base, int32_t id, void *data)
{
(void)arg;
(void)base;
esp_websocket_event_data_t *ev = (esp_websocket_event_data_t *)data;
switch (id) {
case WEBSOCKET_EVENT_CONNECTED: {
ESP_LOGI(TAG, "gateway connected");
s_connected = true;
char hello[128];
snprintf(hello, sizeof(hello),
"{\"type\":\"hello\",\"device\":\"desklock-p4\",\"fw\":\"%s\"}",
esp_app_get_description()->version);
gw_send_text(hello);
face_set(FACE_IDLE);
break;
}
case WEBSOCKET_EVENT_DISCONNECTED:
case WEBSOCKET_EVENT_ERROR:
if (s_connected) {
ESP_LOGW(TAG, "gateway lost");
}
s_connected = false;
audio_capture_stop();
app_on_disconnect();
/* Stay calm: the rain draining away signals "unreachable". A sustained
* outage escalates to the x_x error face in face.c (clock_cb). */
face_set(FACE_IDLE);
break;
case WEBSOCKET_EVENT_DATA:
if (ev->op_code == 0x01) { /* text */
handle_text(ev->data_ptr, ev->data_len);
} else if (ev->op_code == 0x02 && ev->data_len > 0) { /* binary PCM */
audio_playback_feed((const uint8_t *)ev->data_ptr, ev->data_len);
}
break;
default:
break;
}
}
void gw_start(void)
{
if (s_ws != NULL) {
return;
}
esp_websocket_client_config_t cfg = {
.uri = GATEWAY_WS_URI,
.buffer_size = 8192,
.reconnect_timeout_ms = 5000,
.network_timeout_ms = 10000,
.ping_interval_sec = 8, /* keep the link visibly alive through long Tatlock turns */
};
s_ws = esp_websocket_client_init(&cfg);
esp_websocket_register_events(s_ws, WEBSOCKET_EVENT_ANY, ws_event, NULL);
esp_websocket_client_start(s_ws);
ESP_LOGI(TAG, "connecting to %s", GATEWAY_WS_URI);
}
bool gw_connected(void)
{
return s_connected;
}
void gw_send_text(const char *json)
{
if (s_connected) {
esp_websocket_client_send_text(s_ws, json, strlen(json), pdMS_TO_TICKS(1500));
}
}
void gw_send_bin(const uint8_t *data, size_t len)
{
if (s_connected) {
esp_websocket_client_send_bin(s_ws, (const char *)data, len, pdMS_TO_TICKS(1500));
}
}