Skip to content

Commit a4035aa

Browse files
committed
websocket serial kinda works
1 parent 07b2697 commit a4035aa

File tree

3 files changed

+32
-15
lines changed

3 files changed

+32
-15
lines changed

supervisor/shared/web_workflow/static/serial.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ function onSubmit() {
55
var input = document.getElementById("input");
66
// You can send message to the Web Socket using ws.send.
77
ws.send(input.value);
8-
output("send: " + input.value);
8+
// output("send: " + input.value);
99
input.value = "";
1010
input.focus();
1111
}
@@ -25,19 +25,19 @@ ws = new WebSocket("ws://cpy-f57ce8.local/cp/serial/");
2525

2626
// Set event handlers.
2727
ws.onopen = function() {
28-
output("onopen");
28+
console.log("onopen");
2929
};
3030

3131
ws.onmessage = function(e) {
3232
// e.data contains received string.
33-
output("onmessage: " + e.data);
33+
output(e.data);
3434
};
3535

3636
ws.onclose = function() {
37-
output("onclose");
37+
console.log("onclose");
3838
};
3939

4040
ws.onerror = function(e) {
41-
output("onerror");
41+
// output("onerror");
4242
console.log(e)
4343
};

supervisor/shared/web_workflow/web_workflow.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1059,6 +1059,7 @@ static bool _reply(socketpool_socket_obj_t *socket, _request *request) {
10591059
}
10601060

10611061
static void _reset_request(_request *request) {
1062+
ESP_LOGI(TAG, "reset request");
10621063
request->state = STATE_METHOD;
10631064
request->origin[0] = '\0';
10641065
request->content_length = 0;
@@ -1244,7 +1245,7 @@ void supervisor_web_workflow_background(void) {
12441245

12451246
// If we have a request in progress, continue working on it.
12461247
if (common_hal_socketpool_socket_get_connected(&active)) {
1247-
ESP_LOGI(TAG, "active connected");
1248+
// ESP_LOGI(TAG, "active connected %d", active_request.in_progress);
12481249
_process_request(&active, &active_request);
12491250
}
12501251
}

supervisor/shared/web_workflow/websocket.c

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ typedef struct {
3232
uint8_t frame_len;
3333
uint8_t payload_len_size;
3434
bool masked;
35-
uint32_t mask;
36-
size_t frame_index;
35+
uint8_t mask[4];
36+
int frame_index;
3737
size_t payload_remaining;
3838
} _websocket;
3939

@@ -96,27 +96,38 @@ static void _read_next_frame_header(void) {
9696

9797
ESP_LOGI(TAG, "mask %d length %x", cp_serial.masked, len);
9898
}
99-
while (cp_serial.frame_index > 1 &&
99+
while (cp_serial.frame_index >= 2 &&
100100
cp_serial.frame_index < (cp_serial.payload_len_size + 2) &&
101101
_read_byte(&h)) {
102102
cp_serial.frame_index++;
103-
cp_serial.payload_remaining = cp_serial.payload_remaining << 8 | c;
103+
cp_serial.payload_remaining = cp_serial.payload_remaining << 8 | h;
104104
}
105-
while (cp_serial.frame_index > (cp_serial.payload_len_size + 2) &&
105+
int mask_start = cp_serial.payload_len_size + 2;
106+
while (cp_serial.frame_index >= mask_start &&
106107
cp_serial.frame_index < cp_serial.frame_len &&
107108
_read_byte(&h)) {
109+
size_t mask_offset = cp_serial.frame_index - mask_start;
110+
cp_serial.mask[mask_offset] = h;
108111
cp_serial.frame_index++;
109-
cp_serial.mask = cp_serial.mask << 8 | c;
112+
ESP_LOGI(TAG, "mask %08x", (uint32_t)*cp_serial.mask);
110113
}
111114
}
112115

113116
static bool _read_next_payload_byte(uint8_t *c) {
114117
_read_next_frame_header();
115-
if (cp_serial.frame_index > cp_serial.frame_len &&
118+
if (cp_serial.frame_index >= cp_serial.frame_len &&
116119
cp_serial.payload_remaining > 0) {
117120
if (_read_byte(c)) {
121+
uint8_t mask_offset = (cp_serial.frame_index - cp_serial.frame_len) % 4;
122+
ESP_LOGI(TAG, "payload byte read %02x mask offset %d", *c, mask_offset);
123+
*c ^= cp_serial.mask[mask_offset];
124+
ESP_LOGI(TAG, "byte unmasked %02x", *c);
118125
cp_serial.frame_index++;
119126
cp_serial.payload_remaining--;
127+
ESP_LOGI(TAG, "payload remaining %d", cp_serial.payload_remaining);
128+
if (cp_serial.payload_remaining == 0) {
129+
cp_serial.frame_index = 0;
130+
}
120131
return true;
121132
}
122133
}
@@ -132,7 +143,9 @@ bool websocket_available(void) {
132143
}
133144

134145
char websocket_read_char(void) {
135-
return _read_next_payload_byte();
146+
uint8_t c;
147+
_read_next_payload_byte(&c);
148+
return c;
136149
}
137150

138151
static void _send_raw(socketpool_socket_obj_t *socket, const uint8_t *buf, int len) {
@@ -171,7 +184,10 @@ static void _websocket_send(_websocket *ws, const char *text, size_t len) {
171184
_send_raw(&ws->socket, (const uint8_t *)&len, 4);
172185
}
173186
_send_raw(&ws->socket, (const uint8_t *)text, len);
174-
ESP_LOGI(TAG, "sent over websocket: %s", text);
187+
char copy[len];
188+
memcpy(copy, text, len);
189+
copy[len] = '\0';
190+
ESP_LOGI(TAG, "sent over websocket: %s", copy);
175191
}
176192

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

0 commit comments

Comments
 (0)