|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2016 Paul Sokolovsky |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include <stdio.h> |
| 28 | +#include <stdint.h> |
| 29 | +#include <string.h> |
| 30 | +#include <errno.h> |
| 31 | + |
| 32 | +#include "py/nlr.h" |
| 33 | +#include "py/obj.h" |
| 34 | +#include "py/runtime.h" |
| 35 | +#include "py/stream.h" |
| 36 | +#include "py/builtin.h" |
| 37 | +#include "extmod/modwebsocket.h" |
| 38 | + |
| 39 | +#if MICROPY_PY_WEBREPL |
| 40 | + |
| 41 | +#if 1 // print debugging info |
| 42 | +#define DEBUG_printf DEBUG_printf |
| 43 | +#else // don't print debugging info |
| 44 | +#define DEBUG_printf(...) (void)0 |
| 45 | +#endif |
| 46 | + |
| 47 | +struct webrepl_file { |
| 48 | + char sig[2]; |
| 49 | + char type; |
| 50 | + char flags; |
| 51 | + uint64_t offset; |
| 52 | + uint32_t size; |
| 53 | + uint16_t fname_len; |
| 54 | + char fname[64]; |
| 55 | +} __attribute__((packed)); |
| 56 | + |
| 57 | +typedef struct _mp_obj_webrepl_t { |
| 58 | + mp_obj_base_t base; |
| 59 | + mp_obj_t sock; |
| 60 | + byte state; |
| 61 | + byte hdr_to_recv; |
| 62 | + uint32_t data_to_recv; |
| 63 | + struct webrepl_file hdr; |
| 64 | + mp_obj_t cur_file; |
| 65 | +} mp_obj_webrepl_t; |
| 66 | + |
| 67 | + |
| 68 | +static inline void close_meth(mp_obj_t stream) { |
| 69 | + mp_obj_t dest[2]; |
| 70 | + mp_load_method(stream, MP_QSTR_close, dest); |
| 71 | + mp_call_method_n_kw(0, 0, dest); |
| 72 | +} |
| 73 | + |
| 74 | +STATIC void write_webrepl(mp_obj_t websock, const void *buf, size_t len) { |
| 75 | + const mp_stream_p_t *sock_stream = mp_get_stream_raise(websock, MP_STREAM_OP_WRITE | MP_STREAM_OP_IOCTL); |
| 76 | + int err; |
| 77 | + int old_opts = sock_stream->ioctl(websock, MP_STREAM_SET_DATA_OPTS, FRAME_BIN, &err); |
| 78 | + sock_stream->write(websock, buf, len, &err); |
| 79 | + sock_stream->ioctl(websock, MP_STREAM_SET_DATA_OPTS, old_opts, &err); |
| 80 | +} |
| 81 | + |
| 82 | +STATIC void write_webrepl_resp(mp_obj_t websock, uint16_t code) { |
| 83 | + char buf[4] = {'W', 'B', code & 0xff, code >> 8}; |
| 84 | + write_webrepl(websock, buf, sizeof(buf)); |
| 85 | +} |
| 86 | + |
| 87 | +STATIC mp_obj_t webrepl_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { |
| 88 | + mp_arg_check_num(n_args, n_kw, 1, 2, false); |
| 89 | + mp_get_stream_raise(args[0], MP_STREAM_OP_READ | MP_STREAM_OP_WRITE | MP_STREAM_OP_IOCTL); |
| 90 | + DEBUG_printf("sizeof(struct webrepl_file) = %lu\n", sizeof(struct webrepl_file)); |
| 91 | + mp_obj_webrepl_t *o = m_new_obj(mp_obj_webrepl_t); |
| 92 | + o->base.type = type; |
| 93 | + o->sock = args[0]; |
| 94 | + o->hdr_to_recv = sizeof(struct webrepl_file); |
| 95 | + o->data_to_recv = 0; |
| 96 | + return o; |
| 97 | +} |
| 98 | + |
| 99 | +STATIC mp_uint_t webrepl_read(mp_obj_t self_in, void *buf, mp_uint_t size, int *errcode) { |
| 100 | + // We know that os.dupterm always calls with size = 1 |
| 101 | + assert(size == 1); |
| 102 | + mp_obj_webrepl_t *self = self_in; |
| 103 | + const mp_stream_p_t *sock_stream = mp_get_stream_raise(self->sock, MP_STREAM_OP_READ); |
| 104 | + mp_uint_t out_sz = sock_stream->read(self->sock, buf, size, errcode); |
| 105 | + if (out_sz == 0 || out_sz == MP_STREAM_ERROR) { |
| 106 | + return out_sz; |
| 107 | + } |
| 108 | + // If last read data belonged to text record (== REPL) |
| 109 | + int err; |
| 110 | + if (sock_stream->ioctl(self->sock, MP_STREAM_GET_DATA_OPTS, 0, &err) == 1) { |
| 111 | + return out_sz; |
| 112 | + } |
| 113 | + |
| 114 | + DEBUG_printf("webrepl: received bin data, hdr_to_recv: %d, data_to_recv=%d\n", self->hdr_to_recv, self->data_to_recv); |
| 115 | + |
| 116 | + if (self->hdr_to_recv != 0) { |
| 117 | + char *p = (char*)&self->hdr + sizeof(self->hdr) - self->hdr_to_recv; |
| 118 | + *p++ = *(char*)buf; |
| 119 | + if (--self->hdr_to_recv != 0) { |
| 120 | + mp_uint_t hdr_sz = sock_stream->read(self->sock, p, self->hdr_to_recv, errcode); |
| 121 | + if (hdr_sz == MP_STREAM_ERROR) { |
| 122 | + return hdr_sz; |
| 123 | + } |
| 124 | + self->hdr_to_recv -= hdr_sz; |
| 125 | + if (self->hdr_to_recv != 0) { |
| 126 | + *errcode = EAGAIN; |
| 127 | + return MP_STREAM_ERROR; |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + DEBUG_printf("webrepl: op: %d, file: %s, chunk @%x, sz=%d\n", self->hdr.type, self->hdr.fname, (uint32_t)self->hdr.offset, self->hdr.size); |
| 132 | + |
| 133 | + // Process header |
| 134 | + mp_obj_t open_args[2] = { |
| 135 | + mp_obj_new_str(self->hdr.fname, strlen(self->hdr.fname), false), |
| 136 | + MP_OBJ_NEW_QSTR(MP_QSTR_wb) |
| 137 | + }; |
| 138 | + |
| 139 | + self->cur_file = mp_builtin_open(2, open_args, (mp_map_t*)&mp_const_empty_map); |
| 140 | + const mp_stream_p_t *file_stream = mp_get_stream_raise(self->cur_file, MP_STREAM_OP_WRITE | MP_STREAM_OP_IOCTL); |
| 141 | + struct mp_stream_seek_t seek = { .offset = self->hdr.offset, .whence = 0 }; |
| 142 | + int err; |
| 143 | + mp_uint_t res = file_stream->ioctl(self->cur_file, MP_STREAM_SEEK, (uintptr_t)&seek, &err); |
| 144 | + assert(res != MP_STREAM_ERROR); |
| 145 | + |
| 146 | + write_webrepl_resp(self->sock, 0); |
| 147 | + self->data_to_recv = self->hdr.size; |
| 148 | + |
| 149 | + *errcode = EAGAIN; |
| 150 | + return MP_STREAM_ERROR; |
| 151 | + } |
| 152 | + |
| 153 | + if (self->data_to_recv != 0) { |
| 154 | + static byte filebuf[256]; |
| 155 | + filebuf[0] = *(byte*)buf; |
| 156 | + mp_uint_t buf_sz = 1; |
| 157 | + if (--self->data_to_recv != 0) { |
| 158 | + size_t to_read = MIN(sizeof(filebuf) - 1, self->data_to_recv); |
| 159 | + mp_uint_t sz = sock_stream->read(self->sock, filebuf + 1, to_read, errcode); |
| 160 | + if (sz == MP_STREAM_ERROR) { |
| 161 | + return sz; |
| 162 | + } |
| 163 | + self->data_to_recv -= sz; |
| 164 | + buf_sz += sz; |
| 165 | + } |
| 166 | + |
| 167 | + DEBUG_printf("webrepl: Writing %lu bytes to file\n", buf_sz); |
| 168 | + int err; |
| 169 | + mp_uint_t res = mp_stream_writeall(self->cur_file, filebuf, buf_sz, &err); |
| 170 | + if(res == MP_STREAM_ERROR) { |
| 171 | + assert(0); |
| 172 | + } |
| 173 | + |
| 174 | + if (self->data_to_recv == 0) { |
| 175 | + close_meth(self->cur_file); |
| 176 | + self->hdr_to_recv = sizeof(struct webrepl_file); |
| 177 | + DEBUG_printf("webrepl: Finished writing file\n"); |
| 178 | + write_webrepl_resp(self->sock, 0); |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + *errcode = EAGAIN; |
| 183 | + return MP_STREAM_ERROR; |
| 184 | +} |
| 185 | + |
| 186 | +STATIC mp_uint_t webrepl_write(mp_obj_t self_in, const void *buf, mp_uint_t size, int *errcode) { |
| 187 | + mp_obj_webrepl_t *self = self_in; |
| 188 | + const mp_stream_p_t *stream_p = mp_get_stream_raise(self->sock, MP_STREAM_OP_WRITE); |
| 189 | + return stream_p->write(self->sock, buf, size, errcode); |
| 190 | +} |
| 191 | + |
| 192 | +STATIC const mp_map_elem_t webrepl_locals_dict_table[] = { |
| 193 | + { MP_OBJ_NEW_QSTR(MP_QSTR_read), (mp_obj_t)&mp_stream_read_obj }, |
| 194 | + { MP_OBJ_NEW_QSTR(MP_QSTR_write), (mp_obj_t)&mp_stream_write_obj }, |
| 195 | +}; |
| 196 | +STATIC MP_DEFINE_CONST_DICT(webrepl_locals_dict, webrepl_locals_dict_table); |
| 197 | + |
| 198 | +STATIC const mp_stream_p_t webrepl_stream_p = { |
| 199 | + .read = webrepl_read, |
| 200 | + .write = webrepl_write, |
| 201 | +}; |
| 202 | + |
| 203 | +STATIC const mp_obj_type_t webrepl_type = { |
| 204 | + { &mp_type_type }, |
| 205 | + .name = MP_QSTR__webrepl, |
| 206 | + .make_new = webrepl_make_new, |
| 207 | + .stream_p = &webrepl_stream_p, |
| 208 | + .locals_dict = (mp_obj_t)&webrepl_locals_dict, |
| 209 | +}; |
| 210 | + |
| 211 | +STATIC const mp_map_elem_t webrepl_module_globals_table[] = { |
| 212 | + { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_websocket) }, |
| 213 | + { MP_OBJ_NEW_QSTR(MP_QSTR__webrepl), (mp_obj_t)&webrepl_type }, |
| 214 | +}; |
| 215 | + |
| 216 | +STATIC MP_DEFINE_CONST_DICT(webrepl_module_globals, webrepl_module_globals_table); |
| 217 | + |
| 218 | +const mp_obj_module_t mp_module_webrepl = { |
| 219 | + .base = { &mp_type_module }, |
| 220 | + .name = MP_QSTR__webrepl, |
| 221 | + .globals = (mp_obj_dict_t*)&webrepl_module_globals, |
| 222 | +}; |
| 223 | + |
| 224 | +#endif // MICROPY_PY_WEBREPL |
0 commit comments