summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorPaul Sokolovsky <pfalcon@users.sourceforge.net>2016-03-24 19:14:12 +0200
committerPaul Sokolovsky <pfalcon@users.sourceforge.net>2016-03-24 19:16:00 +0200
commit24342dd65efc34d0f89c1a87b9a793d1299538e1 (patch)
tree08f52978e9569e2ec112411daaa2cde835e23a68
parentd4c8e626f280ab096c1341577931d8071972bd4c (diff)
downloadmicropython-24342dd65efc34d0f89c1a87b9a793d1299538e1.tar.gz
micropython-24342dd65efc34d0f89c1a87b9a793d1299538e1.zip
extmod/modwebsocket: Start module for WebSocket helper functions.
Currently, only write support is implemented (of limited buffer size).
-rw-r--r--extmod/modwebsocket.c102
-rw-r--r--py/builtin.h1
-rw-r--r--py/mpconfig.h4
-rw-r--r--py/objmodule.c3
-rw-r--r--py/py.mk1
-rw-r--r--py/qstrdefs.h4
6 files changed, 115 insertions, 0 deletions
diff --git a/extmod/modwebsocket.c b/extmod/modwebsocket.c
new file mode 100644
index 0000000000..7d2363a4c1
--- /dev/null
+++ b/extmod/modwebsocket.c
@@ -0,0 +1,102 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2016 Paul Sokolovsky
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include <stdio.h>
+#include <stdint.h>
+
+#include "py/nlr.h"
+#include "py/obj.h"
+#include "py/stream.h"
+
+#if MICROPY_PY_WEBSOCKET
+
+enum { FRAME_HEADER, FRAME_OPT, PAYLOAD };
+
+typedef struct _mp_obj_websocket_t {
+ mp_obj_base_t base;
+ mp_obj_t sock;
+ uint32_t mask;
+ byte state;
+ byte to_recv;
+ byte mask_pos;
+ byte buf[4];
+} mp_obj_websocket_t;
+
+STATIC mp_obj_t websocket_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
+ assert(n_args == 1);
+ mp_obj_websocket_t *o = m_new_obj(mp_obj_websocket_t);
+ o->base.type = type;
+ o->sock = args[0];
+ o->state = FRAME_HEADER;
+ o->to_recv = 2;
+ o->mask_pos = 0;
+ return o;
+}
+
+STATIC mp_uint_t websocket_write(mp_obj_t self_in, const void *buf, mp_uint_t size, int *errcode) {
+ mp_obj_websocket_t *self = self_in;
+ assert(size < 126);
+ byte header[] = {0x81, size};
+
+ mp_uint_t out_sz = mp_stream_writeall(self->sock, header, sizeof(header), errcode);
+ if (out_sz == MP_STREAM_ERROR) {
+ return MP_STREAM_ERROR;
+ }
+ return mp_stream_writeall(self->sock, buf, size, errcode);
+}
+
+STATIC const mp_map_elem_t websocket_locals_dict_table[] = {
+ { MP_OBJ_NEW_QSTR(MP_QSTR_write), (mp_obj_t)&mp_stream_write_obj },
+};
+STATIC MP_DEFINE_CONST_DICT(websocket_locals_dict, websocket_locals_dict_table);
+
+STATIC const mp_stream_p_t websocket_stream_p = {
+// .read = websocket_read,
+ .write = websocket_write,
+};
+
+STATIC const mp_obj_type_t websocket_type = {
+ { &mp_type_type },
+ .name = MP_QSTR_websocket,
+ .make_new = websocket_make_new,
+ .stream_p = &websocket_stream_p,
+ .locals_dict = (mp_obj_t)&websocket_locals_dict,
+};
+
+STATIC const mp_map_elem_t websocket_module_globals_table[] = {
+ { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_websocket) },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_websocket), (mp_obj_t)&websocket_type },
+};
+
+STATIC MP_DEFINE_CONST_DICT(websocket_module_globals, websocket_module_globals_table);
+
+const mp_obj_module_t mp_module_websocket = {
+ .base = { &mp_type_module },
+ .name = MP_QSTR_websocket,
+ .globals = (mp_obj_dict_t*)&websocket_module_globals,
+};
+
+#endif // MICROPY_PY_WEBSOCKET
diff --git a/py/builtin.h b/py/builtin.h
index 162835cd4f..e3e68e1519 100644
--- a/py/builtin.h
+++ b/py/builtin.h
@@ -105,6 +105,7 @@ extern const mp_obj_module_t mp_module_urandom;
extern const mp_obj_module_t mp_module_ussl;
extern const mp_obj_module_t mp_module_machine;
extern const mp_obj_module_t mp_module_lwip;
+extern const mp_obj_module_t mp_module_websocket;
// extmod functions
MP_DECLARE_CONST_FUN_OBJ(pyb_mount_obj);
diff --git a/py/mpconfig.h b/py/mpconfig.h
index 39070b712e..67bbba7e1f 100644
--- a/py/mpconfig.h
+++ b/py/mpconfig.h
@@ -836,6 +836,10 @@ typedef double mp_float_t;
#define MICROPY_PY_USSL (0)
#endif
+#ifndef MICROPY_PY_WEBSOCKET
+#define MICROPY_PY_WEBSOCKET (0)
+#endif
+
/*****************************************************************************/
/* Hooks for a port to add builtins */
diff --git a/py/objmodule.c b/py/objmodule.c
index 1034d00f60..5fd7b82c5b 100644
--- a/py/objmodule.c
+++ b/py/objmodule.c
@@ -193,6 +193,9 @@ STATIC const mp_rom_map_elem_t mp_builtin_module_table[] = {
#if MICROPY_PY_LWIP
{ MP_ROM_QSTR(MP_QSTR_lwip), MP_ROM_PTR(&mp_module_lwip) },
#endif
+#if MICROPY_PY_WEBSOCKET
+ { MP_ROM_QSTR(MP_QSTR_websocket), MP_ROM_PTR(&mp_module_websocket) },
+#endif
// extra builtin modules as defined by a port
MICROPY_PORT_BUILTIN_MODULES
diff --git a/py/py.mk b/py/py.mk
index 1c746c923f..899f333c62 100644
--- a/py/py.mk
+++ b/py/py.mk
@@ -169,6 +169,7 @@ PY_O_BASENAME = \
../extmod/machine_mem.o \
../extmod/modussl.o \
../extmod/modurandom.o \
+ ../extmod/modwebsocket.o \
../extmod/fsusermount.o \
../extmod/vfs_fat.o \
../extmod/vfs_fat_ffconf.o \
diff --git a/py/qstrdefs.h b/py/qstrdefs.h
index c8bef1ba39..734354b33e 100644
--- a/py/qstrdefs.h
+++ b/py/qstrdefs.h
@@ -752,3 +752,7 @@ Q(uniform)
Q(VfsFat)
Q(flush)
#endif
+
+#if MICROPY_PY_WEBSOCKET
+Q(websocket)
+#endif