forked from adafruit/circuitpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebsocket.c
More file actions
237 lines (214 loc) · 7.31 KB
/
Copy pathwebsocket.c
File metadata and controls
237 lines (214 loc) · 7.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// This file is part of the CircuitPython project: https://circuitpython.org
//
// SPDX-FileCopyrightText: Copyright (c) 2022 Scott Shawcroft for Adafruit Industries
//
// SPDX-License-Identifier: MIT
#include "supervisor/shared/web_workflow/websocket.h"
#include "py/ringbuf.h"
#include "py/runtime.h"
#include "shared/runtime/interrupt_char.h"
#include "shared-bindings/socketpool/SocketPool.h"
#include "supervisor/shared/web_workflow/web_workflow.h"
#if CIRCUITPY_STATUS_BAR
#include "supervisor/shared/status_bar.h"
#endif
typedef struct {
socketpool_socket_obj_t socket;
uint8_t opcode;
uint8_t frame_len;
uint8_t payload_len_size;
bool masked;
uint8_t mask[4];
int frame_index;
size_t payload_remaining;
} _websocket;
// Buffer the incoming serial data in the background so that we can look for the
// interrupt character.
static ringbuf_t _incoming_ringbuf;
static uint8_t _buf[16];
// make sure background is not called recursively
static bool in_web_background = false;
static _websocket cp_serial;
void websocket_init(void) {
socketpool_socket_reset(&cp_serial.socket);
ringbuf_init(&_incoming_ringbuf, _buf, sizeof(_buf));
}
void websocket_handoff(socketpool_socket_obj_t *socket) {
if (!common_hal_socketpool_socket_get_closed(&cp_serial.socket)) {
common_hal_socketpool_socket_close(&cp_serial.socket);
}
socketpool_socket_move(socket, &cp_serial.socket);
cp_serial.opcode = 0;
cp_serial.frame_index = 0;
cp_serial.frame_len = 2;
#if CIRCUITPY_STATUS_BAR
// Send the title bar for the new client.
supervisor_status_bar_request_update(true);
#endif
}
bool websocket_connected(void) {
return _incoming_ringbuf.size > 0 &&
!common_hal_socketpool_socket_get_closed(&cp_serial.socket) &&
common_hal_socketpool_socket_get_connected(&cp_serial.socket);
}
static bool _read_byte(uint8_t *c) {
int len = socketpool_socket_recv_into(&cp_serial.socket, c, 1);
if (len < 1) {
return false;
}
return true;
}
static void _read_next_frame_header(void) {
uint8_t h;
if (cp_serial.frame_index == 0 && _read_byte(&h)) {
cp_serial.frame_index++;
cp_serial.opcode = h & 0xf;
}
if (cp_serial.frame_index == 1 && _read_byte(&h)) {
cp_serial.frame_index++;
uint8_t len = h & 0x7f;
cp_serial.masked = (h >> 7) == 1;
if (len <= 125) {
cp_serial.payload_remaining = len;
cp_serial.payload_len_size = 0;
} else if (len == 126) { // 16 bit length
cp_serial.payload_len_size = 2;
} else if (len == 127) { // 64 bit length
cp_serial.payload_len_size = 8;
}
cp_serial.frame_len = 2 + cp_serial.payload_len_size;
if (cp_serial.masked) {
cp_serial.frame_len += 4;
}
}
while (cp_serial.frame_index >= 2 &&
cp_serial.frame_index < (cp_serial.payload_len_size + 2) &&
_read_byte(&h)) {
cp_serial.frame_index++;
cp_serial.payload_remaining = cp_serial.payload_remaining << 8 | h;
}
int mask_start = cp_serial.payload_len_size + 2;
while (cp_serial.frame_index >= mask_start &&
cp_serial.frame_index < cp_serial.frame_len &&
_read_byte(&h)) {
size_t mask_offset = cp_serial.frame_index - mask_start;
cp_serial.mask[mask_offset] = h;
cp_serial.frame_index++;
}
// Reply to PINGs and CLOSE.
while ((cp_serial.opcode == 0x8 ||
cp_serial.opcode == 0x9) &&
cp_serial.frame_index >= cp_serial.frame_len) {
if (cp_serial.frame_index == cp_serial.frame_len) {
uint8_t opcode = 0x8; // CLOSE
if (cp_serial.opcode == 0x9) {
opcode = 0xA; // PONG
}
uint8_t frame_header[2];
frame_header[0] = 1 << 7 | opcode;
frame_header[1] = cp_serial.payload_remaining;
web_workflow_send_raw(&cp_serial.socket, cp_serial.opcode != 0x9, (const uint8_t *)frame_header, 2);
}
if (cp_serial.payload_remaining > 0 && _read_byte(&h)) {
// Send the payload back to the client.
cp_serial.frame_index++;
cp_serial.payload_remaining--;
web_workflow_send_raw(&cp_serial.socket, false, &h, 1);
}
if (cp_serial.payload_remaining == 0) {
cp_serial.frame_index = 0;
if (cp_serial.opcode == 0x8) {
common_hal_socketpool_socket_close(&cp_serial.socket);
}
}
}
}
static bool _read_next_payload_byte(uint8_t *c) {
_read_next_frame_header();
if (cp_serial.opcode == 0x1 &&
cp_serial.frame_index >= cp_serial.frame_len &&
cp_serial.payload_remaining > 0) {
if (_read_byte(c)) {
uint8_t mask_offset = (cp_serial.frame_index - cp_serial.frame_len) % 4;
*c ^= cp_serial.mask[mask_offset];
cp_serial.frame_index++;
cp_serial.payload_remaining--;
if (cp_serial.payload_remaining == 0) {
cp_serial.frame_index = 0;
}
return true;
}
}
return false;
}
uint32_t websocket_available(void) {
if (!websocket_connected()) {
return 0;
}
websocket_background();
return ringbuf_num_filled(&_incoming_ringbuf);
}
char websocket_read_char(void) {
websocket_background();
if (ringbuf_num_filled(&_incoming_ringbuf) > 0) {
return ringbuf_get(&_incoming_ringbuf);
}
return -1;
}
static void _websocket_send(_websocket *ws, const char *text, size_t len) {
if (!websocket_connected()) {
return;
}
uint32_t opcode = 1;
uint8_t frame_header[2];
frame_header[0] = 1 << 7 | opcode;
uint8_t payload_len;
if (len <= 125) {
payload_len = len;
} else if (len < (1 << 16)) {
payload_len = 126;
} else {
payload_len = 127;
}
frame_header[1] = payload_len;
web_workflow_send_raw(&ws->socket, false, (const uint8_t *)frame_header, 2);
uint8_t extended_len[4];
if (payload_len == 126) {
extended_len[0] = (len >> 8) & 0xff;
extended_len[1] = len & 0xff;
web_workflow_send_raw(&ws->socket, false, extended_len, 2);
} else if (payload_len == 127) {
uint32_t zero = 0;
// 64 bits where top four bytes are zero.
web_workflow_send_raw(&ws->socket, false, (const uint8_t *)&zero, 4);
extended_len[0] = (len >> 24) & 0xff;
extended_len[1] = (len >> 16) & 0xff;
extended_len[2] = (len >> 8) & 0xff;
extended_len[3] = len & 0xff;
web_workflow_send_raw(&ws->socket, false, extended_len, 4);
}
web_workflow_send_raw(&ws->socket, false, (const uint8_t *)text, len);
}
void websocket_write(const char *text, size_t len) {
_websocket_send(&cp_serial, text, len);
}
void websocket_background(void) {
if (!websocket_connected()) {
return;
}
if (in_web_background) {
return;
}
in_web_background = true;
uint8_t c;
while (ringbuf_num_empty(&_incoming_ringbuf) > 0 &&
_read_next_payload_byte(&c)) {
if (c == mp_interrupt_char) {
ringbuf_clear(&_incoming_ringbuf);
mp_sched_keyboard_interrupt();
continue;
}
ringbuf_put(&_incoming_ringbuf, c);
}
in_web_background = false;
}