Skip to content

Commit ee3fec3

Browse files
committed
esp8266: Add skeleton "network" module.
MicroPython "network" module interface requires it to contains classes to instantiate. But as we have a static network interace, make WLAN() "constructor" just return module itself, and just make all methods module-global functions.
1 parent 431603a commit ee3fec3

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed

esp8266/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ SRC_C = \
5959
modpybrtc.c \
6060
modpybadc.c \
6161
modesp.c \
62+
modnetwork.c \
6263
modutime.c \
6364
moduos.c \
6465
utils.c \

esp8266/modnetwork.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2015 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/objlist.h"
34+
#include "py/runtime.h"
35+
#include "modnetwork.h"
36+
37+
extern const mp_obj_module_t network_module;
38+
39+
STATIC mp_obj_t get_module() {
40+
return (mp_obj_t)&network_module;
41+
}
42+
STATIC MP_DEFINE_CONST_FUN_OBJ_0(get_module_obj, get_module);
43+
44+
STATIC const mp_map_elem_t mp_module_network_globals_table[] = {
45+
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_network) },
46+
// MicroPython "network" module interface requires it to contains classes
47+
// to instantiate. But as we have just a static network interace,
48+
// use module as a "class", and just make all methods module-global
49+
// functions.
50+
{ MP_OBJ_NEW_QSTR(MP_QSTR_WLAN), (mp_obj_t)&get_module_obj },
51+
};
52+
53+
STATIC MP_DEFINE_CONST_DICT(mp_module_network_globals, mp_module_network_globals_table);
54+
55+
const mp_obj_module_t network_module = {
56+
.base = { &mp_type_module },
57+
.name = MP_QSTR_network,
58+
.globals = (mp_obj_dict_t*)&mp_module_network_globals,
59+
};

esp8266/mpconfigport.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,14 @@ extern const struct _mp_obj_fun_builtin_t mp_builtin_open_obj;
6565
// extra built in modules to add to the list of known ones
6666
extern const struct _mp_obj_module_t pyb_module;
6767
extern const struct _mp_obj_module_t esp_module;
68+
extern const struct _mp_obj_module_t network_module;
6869
extern const struct _mp_obj_module_t utime_module;
6970
extern const struct _mp_obj_module_t uos_module;
7071

7172
#define MICROPY_PORT_BUILTIN_MODULES \
7273
{ MP_OBJ_NEW_QSTR(MP_QSTR_pyb), (mp_obj_t)&pyb_module }, \
7374
{ MP_OBJ_NEW_QSTR(MP_QSTR_esp), (mp_obj_t)&esp_module }, \
75+
{ MP_OBJ_NEW_QSTR(MP_QSTR_network), (mp_obj_t)&network_module }, \
7476
{ MP_OBJ_NEW_QSTR(MP_QSTR_utime), (mp_obj_t)&utime_module }, \
7577
{ MP_OBJ_NEW_QSTR(MP_QSTR_uos), (mp_obj_t)&uos_module }, \
7678

esp8266/qstrdefsport.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ Q(SLEEP_NONE)
9898
Q(SLEEP_LIGHT)
9999
Q(SLEEP_MODEM)
100100

101+
// network module
102+
Q(network)
103+
Q(WLAN)
104+
101105
// Pin class
102106
Q(Pin)
103107
Q(init)

0 commit comments

Comments
 (0)