26
26
27
27
#include "supervisor/shared/web_workflow/websocket.h"
28
28
29
+ #include "py/ringbuf.h"
30
+ #include "py/runtime.h"
31
+ #include "shared/runtime/interrupt_char.h"
29
32
#include "supervisor/shared/title_bar.h"
30
33
31
34
// TODO: Remove ESP specific stuff. For now, it is useful as we refine the server.
@@ -43,13 +46,20 @@ typedef struct {
43
46
size_t payload_remaining ;
44
47
} _websocket ;
45
48
49
+ // Buffer the incoming serial data in the background so that we can look for the
50
+ // interrupt character.
51
+ STATIC ringbuf_t _incoming_ringbuf ;
52
+ STATIC uint8_t _buf [16 ];
53
+
46
54
static _websocket cp_serial ;
47
55
48
56
static const char * TAG = "CP websocket" ;
49
57
50
58
void websocket_init (void ) {
51
59
cp_serial .socket .num = -1 ;
52
60
cp_serial .socket .connected = false;
61
+
62
+ ringbuf_init (& _incoming_ringbuf , _buf , sizeof (_buf ) - 1 );
53
63
}
54
64
55
65
void websocket_handoff (socketpool_socket_obj_t * socket ) {
@@ -193,16 +203,16 @@ bool websocket_available(void) {
193
203
if (!websocket_connected ()) {
194
204
return false;
195
205
}
196
- _read_next_frame_header ();
197
- return cp_serial . payload_remaining > 0 && cp_serial . frame_index >= cp_serial . frame_len ;
206
+ websocket_background ();
207
+ return ringbuf_num_filled ( & _incoming_ringbuf ) > 0 ;
198
208
}
199
209
200
210
char websocket_read_char (void ) {
201
- uint8_t c ;
202
- if (! _read_next_payload_byte ( & c ) ) {
203
- c = -1 ;
211
+ websocket_background () ;
212
+ if (ringbuf_num_filled ( & _incoming_ringbuf ) > 0 ) {
213
+ return ringbuf_get ( & _incoming_ringbuf ) ;
204
214
}
205
- return c ;
215
+ return -1 ;
206
216
}
207
217
208
218
static void _websocket_send (_websocket * ws , const char * text , size_t len ) {
@@ -246,3 +256,15 @@ static void _websocket_send(_websocket *ws, const char *text, size_t len) {
246
256
void websocket_write (const char * text , size_t len ) {
247
257
_websocket_send (& cp_serial , text , len );
248
258
}
259
+
260
+ void websocket_background (void ) {
261
+ uint8_t c ;
262
+ while (ringbuf_num_empty (& _incoming_ringbuf ) > 0 &&
263
+ _read_next_payload_byte (& c )) {
264
+ if (c == mp_interrupt_char ) {
265
+ mp_sched_keyboard_interrupt ();
266
+ continue ;
267
+ }
268
+ ringbuf_put (& _incoming_ringbuf , c );
269
+ }
270
+ }
0 commit comments