Skip to content

Commit 24342dd

Browse files
committed
extmod/modwebsocket: Start module for WebSocket helper functions.
Currently, only write support is implemented (of limited buffer size).
1 parent d4c8e62 commit 24342dd

6 files changed

Lines changed: 115 additions & 0 deletions

File tree

extmod/modwebsocket.c

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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+
30+
#include "py/nlr.h"
31+
#include "py/obj.h"
32+
#include "py/stream.h"
33+
34+
#if MICROPY_PY_WEBSOCKET
35+
36+
enum { FRAME_HEADER, FRAME_OPT, PAYLOAD };
37+
38+
typedef struct _mp_obj_websocket_t {
39+
mp_obj_base_t base;
40+
mp_obj_t sock;
41+
uint32_t mask;
42+
byte state;
43+
byte to_recv;
44+
byte mask_pos;
45+
byte buf[4];
46+
} mp_obj_websocket_t;
47+
48+
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) {
49+
assert(n_args == 1);
50+
mp_obj_websocket_t *o = m_new_obj(mp_obj_websocket_t);
51+
o->base.type = type;
52+
o->sock = args[0];
53+
o->state = FRAME_HEADER;
54+
o->to_recv = 2;
55+
o->mask_pos = 0;
56+
return o;
57+
}
58+
59+
STATIC mp_uint_t websocket_write(mp_obj_t self_in, const void *buf, mp_uint_t size, int *errcode) {
60+
mp_obj_websocket_t *self = self_in;
61+
assert(size < 126);
62+
byte header[] = {0x81, size};
63+
64+
mp_uint_t out_sz = mp_stream_writeall(self->sock, header, sizeof(header), errcode);
65+
if (out_sz == MP_STREAM_ERROR) {
66+
return MP_STREAM_ERROR;
67+
}
68+
return mp_stream_writeall(self->sock, buf, size, errcode);
69+
}
70+
71+
STATIC const mp_map_elem_t websocket_locals_dict_table[] = {
72+
{ MP_OBJ_NEW_QSTR(MP_QSTR_write), (mp_obj_t)&mp_stream_write_obj },
73+
};
74+
STATIC MP_DEFINE_CONST_DICT(websocket_locals_dict, websocket_locals_dict_table);
75+
76+
STATIC const mp_stream_p_t websocket_stream_p = {
77+
// .read = websocket_read,
78+
.write = websocket_write,
79+
};
80+
81+
STATIC const mp_obj_type_t websocket_type = {
82+
{ &mp_type_type },
83+
.name = MP_QSTR_websocket,
84+
.make_new = websocket_make_new,
85+
.stream_p = &websocket_stream_p,
86+
.locals_dict = (mp_obj_t)&websocket_locals_dict,
87+
};
88+
89+
STATIC const mp_map_elem_t websocket_module_globals_table[] = {
90+
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_websocket) },
91+
{ MP_OBJ_NEW_QSTR(MP_QSTR_websocket), (mp_obj_t)&websocket_type },
92+
};
93+
94+
STATIC MP_DEFINE_CONST_DICT(websocket_module_globals, websocket_module_globals_table);
95+
96+
const mp_obj_module_t mp_module_websocket = {
97+
.base = { &mp_type_module },
98+
.name = MP_QSTR_websocket,
99+
.globals = (mp_obj_dict_t*)&websocket_module_globals,
100+
};
101+
102+
#endif // MICROPY_PY_WEBSOCKET

py/builtin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ extern const mp_obj_module_t mp_module_urandom;
105105
extern const mp_obj_module_t mp_module_ussl;
106106
extern const mp_obj_module_t mp_module_machine;
107107
extern const mp_obj_module_t mp_module_lwip;
108+
extern const mp_obj_module_t mp_module_websocket;
108109

109110
// extmod functions
110111
MP_DECLARE_CONST_FUN_OBJ(pyb_mount_obj);

py/mpconfig.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,10 @@ typedef double mp_float_t;
836836
#define MICROPY_PY_USSL (0)
837837
#endif
838838

839+
#ifndef MICROPY_PY_WEBSOCKET
840+
#define MICROPY_PY_WEBSOCKET (0)
841+
#endif
842+
839843
/*****************************************************************************/
840844
/* Hooks for a port to add builtins */
841845

py/objmodule.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,9 @@ STATIC const mp_rom_map_elem_t mp_builtin_module_table[] = {
193193
#if MICROPY_PY_LWIP
194194
{ MP_ROM_QSTR(MP_QSTR_lwip), MP_ROM_PTR(&mp_module_lwip) },
195195
#endif
196+
#if MICROPY_PY_WEBSOCKET
197+
{ MP_ROM_QSTR(MP_QSTR_websocket), MP_ROM_PTR(&mp_module_websocket) },
198+
#endif
196199

197200
// extra builtin modules as defined by a port
198201
MICROPY_PORT_BUILTIN_MODULES

py/py.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ PY_O_BASENAME = \
169169
../extmod/machine_mem.o \
170170
../extmod/modussl.o \
171171
../extmod/modurandom.o \
172+
../extmod/modwebsocket.o \
172173
../extmod/fsusermount.o \
173174
../extmod/vfs_fat.o \
174175
../extmod/vfs_fat_ffconf.o \

py/qstrdefs.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,3 +752,7 @@ Q(uniform)
752752
Q(VfsFat)
753753
Q(flush)
754754
#endif
755+
756+
#if MICROPY_PY_WEBSOCKET
757+
Q(websocket)
758+
#endif

0 commit comments

Comments
 (0)