Skip to content

Commit c8c44a4

Browse files
committed
py: Add ioctl method to stream protocol; add initial modselect.
1 parent 8105736 commit c8c44a4

3 files changed

Lines changed: 80 additions & 1 deletion

File tree

extmod/modselect.c

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2014 Damien P. George
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 <assert.h>
28+
#include <string.h>
29+
#include <stdint.h>
30+
#include "mpconfig.h"
31+
#include "misc.h"
32+
#include "nlr.h"
33+
#include "qstr.h"
34+
#include "obj.h"
35+
#include "runtime.h"
36+
#include "objtuple.h"
37+
#include "binary.h"
38+
39+
#if MICROPY_PY_SELECT
40+
41+
/// \module select - Provides select function to wait for events on a stream
42+
///
43+
/// This module provides the select function.
44+
45+
/// \function select(rlist, wlist, xlist[, timeout])
46+
mp_obj_t select_select(uint n_args, const mp_obj_t *args) {
47+
return mp_obj_new_bytes((void*)mp_obj_int_get(ptr), mp_obj_int_get(size));
48+
}
49+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(select_select_obj, 3, 4, select_select);
50+
51+
STATIC const mp_map_elem_t mp_module_select_globals_table[] = {
52+
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_select) },
53+
{ MP_OBJ_NEW_QSTR(MP_QSTR_select), (mp_obj_t)&select_select_obj },
54+
};
55+
56+
STATIC const mp_obj_dict_t mp_module_select_globals = {
57+
.base = {&mp_type_dict},
58+
.map = {
59+
.all_keys_are_qstrs = 1,
60+
.table_is_fixed_array = 1,
61+
.used = MP_ARRAY_SIZE(mp_module_select_globals_table),
62+
.alloc = MP_ARRAY_SIZE(mp_module_select_globals_table),
63+
.table = (mp_map_elem_t*)mp_module_select_globals_table,
64+
},
65+
};
66+
67+
const mp_obj_module_t mp_module_select = {
68+
.base = { &mp_type_module },
69+
.name = MP_QSTR_select,
70+
.globals = (mp_obj_dict_t*)&mp_module_select_globals,
71+
};
72+
73+
#endif // MICROPY_PY_SELECT

py/mpconfig.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,10 @@ typedef double mp_float_t;
390390
#define MICROPY_PY_ZLIBD (0)
391391
#endif
392392

393+
#ifndef MICROPY_PY_SELECT
394+
#define MICROPY_PY_SELECT (0)
395+
#endif
396+
393397
/*****************************************************************************/
394398
/* Hooks for a port to add builtins */
395399

py/obj.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,12 +230,14 @@ void mp_get_buffer_raise(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flag
230230

231231
// Stream protocol
232232
#define MP_STREAM_ERROR (-1)
233+
#define MP_STREAM_FLUSH (1)
234+
#define MP_STREAM_POLL (2)
233235
typedef struct _mp_stream_p_t {
234236
// On error, functions should return MP_STREAM_ERROR and fill in *errcode (values
235237
// are implementation-dependent, but will be exposed to user, e.g. via exception).
236238
mp_uint_t (*read)(mp_obj_t obj, void *buf, mp_uint_t size, int *errcode);
237239
mp_uint_t (*write)(mp_obj_t obj, const void *buf, mp_uint_t size, int *errcode);
238-
// add seek() ?
240+
mp_uint_t (*ioctl(mp_obj_t obj, mp_uint_t request, int *errcode, ...);
239241
mp_uint_t is_text : 1; // default is bytes, set this for text stream
240242
} mp_stream_p_t;
241243

0 commit comments

Comments
 (0)