blob: 40bc4590848bd307560f03b379613f6c23b15242 [file] [log] [blame]
Paul Sokolovskyee3fec32015-06-12 17:16:52 +03001/*
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"
Paul Sokolovsky32eb4b92015-06-12 17:26:10 +030035#include "netutils.h"
36#include "queue.h"
37#include "user_interface.h"
38#include "espconn.h"
Paul Sokolovsky32eb4b92015-06-12 17:26:10 +030039#include "spi_flash.h"
40#include "utils.h"
Paul Sokolovskyee3fec32015-06-12 17:16:52 +030041
Paul Sokolovskyd5a12a62016-02-19 18:53:43 +020042#define MODNETWORK_INCLUDE_CONSTANTS (1)
43
Paul Sokolovsky9e8396a2016-02-13 19:26:17 +020044typedef struct _wlan_if_obj_t {
45 mp_obj_base_t base;
46 int if_id;
47} wlan_if_obj_t;
Paul Sokolovskyee3fec32015-06-12 17:16:52 +030048
Paul Sokolovsky9e8396a2016-02-13 19:26:17 +020049void error_check(bool status, const char *msg);
50const mp_obj_type_t wlan_if_type;
51
52STATIC const wlan_if_obj_t wlan_objs[] = {
53 {{&wlan_if_type}, STATION_IF},
54 {{&wlan_if_type}, SOFTAP_IF},
55};
56
57STATIC void require_if(mp_obj_t wlan_if, int if_no) {
58 wlan_if_obj_t *self = MP_OBJ_TO_PTR(wlan_if);
59 if (self->if_id != if_no) {
60 error_check(false, "STA required");
61 }
Paul Sokolovskyee3fec32015-06-12 17:16:52 +030062}
Paul Sokolovsky9e8396a2016-02-13 19:26:17 +020063
64STATIC mp_obj_t get_wlan(mp_uint_t n_args, const mp_obj_t *args) {
65 int idx = 0;
66 if (n_args > 0) {
67 idx = mp_obj_get_int(args[0]);
68 }
69 return MP_OBJ_FROM_PTR(&wlan_objs[idx]);
70}
71STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(get_wlan_obj, 0, 1, get_wlan);
Paul Sokolovskyee3fec32015-06-12 17:16:52 +030072
Paul Sokolovsky1c43a0f2016-02-19 18:44:13 +020073STATIC mp_obj_t esp_active(mp_uint_t n_args, const mp_obj_t *args) {
74 wlan_if_obj_t *self = MP_OBJ_TO_PTR(args[0]);
75 uint32_t mode = wifi_get_opmode();
76 if (n_args > 1) {
77 int mask = self->if_id == STATION_IF ? STATION_MODE : SOFTAP_MODE;
78 if (mp_obj_get_int(args[1]) != 0) {
79 mode |= mask;
80 } else {
81 mode &= ~mask;
82 }
83 error_check(wifi_set_opmode(mode), "Cannot update i/f status");
84 return mp_const_none;
85 }
86
87 // Get active status
88 if (self->if_id == STATION_IF) {
89 return mp_obj_new_bool(mode & STATION_MODE);
90 } else {
91 return mp_obj_new_bool(mode & SOFTAP_MODE);
92 }
93}
94STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp_active_obj, 1, 2, esp_active);
95
Paul Sokolovsky32eb4b92015-06-12 17:26:10 +030096STATIC mp_obj_t esp_connect(mp_uint_t n_args, const mp_obj_t *args) {
Paul Sokolovsky9e8396a2016-02-13 19:26:17 +020097 require_if(args[0], STATION_IF);
Paul Sokolovsky32eb4b92015-06-12 17:26:10 +030098 struct station_config config = {{0}};
99 mp_uint_t len;
100 const char *p;
101
Paul Sokolovsky32eb4b92015-06-12 17:26:10 +0300102 p = mp_obj_str_get_data(args[1], &len);
Paul Sokolovsky9e8396a2016-02-13 19:26:17 +0200103 memcpy(config.ssid, p, len);
104 p = mp_obj_str_get_data(args[2], &len);
Paul Sokolovsky32eb4b92015-06-12 17:26:10 +0300105 memcpy(config.password, p, len);
106
107 error_check(wifi_station_set_config(&config), "Cannot set STA config");
108 error_check(wifi_station_connect(), "Cannot connect to AP");
109
110 return mp_const_none;
111}
Paul Sokolovsky9e8396a2016-02-13 19:26:17 +0200112STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp_connect_obj, 3, 7, esp_connect);
Paul Sokolovsky32eb4b92015-06-12 17:26:10 +0300113
Paul Sokolovsky9e8396a2016-02-13 19:26:17 +0200114STATIC mp_obj_t esp_disconnect(mp_obj_t self_in) {
115 require_if(self_in, STATION_IF);
Paul Sokolovsky32eb4b92015-06-12 17:26:10 +0300116 error_check(wifi_station_disconnect(), "Cannot disconnect from AP");
117 return mp_const_none;
118}
Paul Sokolovsky9e8396a2016-02-13 19:26:17 +0200119STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp_disconnect_obj, esp_disconnect);
Paul Sokolovsky32eb4b92015-06-12 17:26:10 +0300120
Paul Sokolovsky9e8396a2016-02-13 19:26:17 +0200121STATIC mp_obj_t esp_status(mp_obj_t self_in) {
122 wlan_if_obj_t *self = MP_OBJ_TO_PTR(self_in);
123 if (self->if_id == STATION_IF) {
124 return MP_OBJ_NEW_SMALL_INT(wifi_station_get_connect_status());
125 }
126 return MP_OBJ_NEW_SMALL_INT(-1);
Bill Owens60ccb412015-06-18 05:54:29 -0700127}
Paul Sokolovsky9e8396a2016-02-13 19:26:17 +0200128STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp_status_obj, esp_status);
Bill Owens60ccb412015-06-18 05:54:29 -0700129
Bill Owens686516f2015-06-16 10:27:12 -0700130STATIC void esp_scan_cb(scaninfo *si, STATUS status) {
131 struct bss_info *bs;
132 if (si->pbss) {
133 STAILQ_FOREACH(bs, si->pbss, next) {
134 mp_obj_tuple_t *t = mp_obj_new_tuple(6, NULL);
135 t->items[0] = mp_obj_new_bytes(bs->ssid, strlen((char*)bs->ssid));
136 t->items[1] = mp_obj_new_bytes(bs->bssid, sizeof(bs->bssid));
137 t->items[2] = MP_OBJ_NEW_SMALL_INT(bs->channel);
138 t->items[3] = MP_OBJ_NEW_SMALL_INT(bs->rssi);
139 t->items[4] = MP_OBJ_NEW_SMALL_INT(bs->authmode);
140 t->items[5] = MP_OBJ_NEW_SMALL_INT(bs->is_hidden);
141 call_function_1_protected(MP_STATE_PORT(scan_cb_obj), t);
142 }
143 }
144}
145
Paul Sokolovsky9e8396a2016-02-13 19:26:17 +0200146STATIC mp_obj_t esp_scan(mp_obj_t self_in, mp_obj_t cb_in) {
Bill Owens686516f2015-06-16 10:27:12 -0700147 MP_STATE_PORT(scan_cb_obj) = cb_in;
148 if (wifi_get_opmode() == SOFTAP_MODE) {
149 nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError,
150 "Scan not supported in AP mode"));
151 }
152 wifi_station_scan(NULL, (scan_done_cb_t)esp_scan_cb);
153 return mp_const_none;
154}
Paul Sokolovsky9e8396a2016-02-13 19:26:17 +0200155STATIC MP_DEFINE_CONST_FUN_OBJ_2(esp_scan_obj, esp_scan);
Bill Owens686516f2015-06-16 10:27:12 -0700156
Bill Owense2bfa472015-06-22 13:22:17 -0700157/// \method isconnected()
158/// Return True if connected to an AP and an IP address has been assigned,
159/// false otherwise.
Paul Sokolovsky9e8396a2016-02-13 19:26:17 +0200160STATIC mp_obj_t esp_isconnected(mp_obj_t self_in) {
161 wlan_if_obj_t *self = MP_OBJ_TO_PTR(self_in);
162 if (self->if_id == STATION_IF) {
163 if (wifi_station_get_connect_status() == STATION_GOT_IP) {
164 return mp_const_true;
165 }
166 } else {
167 if (wifi_softap_get_station_num() > 0) {
168 return mp_const_true;
169 }
Bill Owense2bfa472015-06-22 13:22:17 -0700170 }
171 return mp_const_false;
172}
173
Paul Sokolovsky9e8396a2016-02-13 19:26:17 +0200174STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp_isconnected_obj, esp_isconnected);
Bill Owense2bfa472015-06-22 13:22:17 -0700175
Paul Sokolovskyfce00362015-12-27 10:03:32 +0200176STATIC mp_obj_t esp_mac(mp_uint_t n_args, const mp_obj_t *args) {
Paul Sokolovsky9e8396a2016-02-13 19:26:17 +0200177 wlan_if_obj_t *self = MP_OBJ_TO_PTR(args[0]);
Paul Sokolovskyfce00362015-12-27 10:03:32 +0200178 uint8_t mac[6];
Paul Sokolovsky9e8396a2016-02-13 19:26:17 +0200179 if (n_args == 1) {
180 wifi_get_macaddr(self->if_id, mac);
Paul Sokolovskyfce00362015-12-27 10:03:32 +0200181 return mp_obj_new_bytes(mac, sizeof(mac));
182 } else {
183 mp_buffer_info_t bufinfo;
Paul Sokolovsky9e8396a2016-02-13 19:26:17 +0200184 mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_READ);
Paul Sokolovskyfce00362015-12-27 10:03:32 +0200185
186 if (bufinfo.len != 6) {
187 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
188 "invalid buffer length"));
189 }
190
Paul Sokolovsky9e8396a2016-02-13 19:26:17 +0200191 wifi_set_macaddr(self->if_id, bufinfo.buf);
Paul Sokolovskyfce00362015-12-27 10:03:32 +0200192 return mp_const_none;
193 }
194}
Paul Sokolovsky9e8396a2016-02-13 19:26:17 +0200195STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp_mac_obj, 1, 2, esp_mac);
Paul Sokolovskyfce00362015-12-27 10:03:32 +0200196
Paul Sokolovsky9e8396a2016-02-13 19:26:17 +0200197STATIC mp_obj_t esp_ifconfig(mp_obj_t self_in) {
198 wlan_if_obj_t *self = MP_OBJ_TO_PTR(self_in);
Damien George02ea74d2015-12-22 14:28:53 +0000199 struct ip_info info;
Paul Sokolovsky9e8396a2016-02-13 19:26:17 +0200200 wifi_get_ip_info(self->if_id, &info);
Damien George02ea74d2015-12-22 14:28:53 +0000201 mp_obj_t ifconfig[4] = {
202 netutils_format_ipv4_addr((uint8_t*)&info.ip, NETUTILS_BIG),
203 netutils_format_ipv4_addr((uint8_t*)&info.netmask, NETUTILS_BIG),
204 netutils_format_ipv4_addr((uint8_t*)&info.gw, NETUTILS_BIG),
205 MP_OBJ_NEW_QSTR(MP_QSTR_), // no DNS server
206 };
207 return mp_obj_new_tuple(4, ifconfig);
208}
Paul Sokolovsky9e8396a2016-02-13 19:26:17 +0200209STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp_ifconfig_obj, esp_ifconfig);
Damien George02ea74d2015-12-22 14:28:53 +0000210
Paul Sokolovskya49c1602016-02-19 19:50:36 +0200211STATIC mp_obj_t esp_config(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
212 if (n_args != 1 && kwargs->used != 0) {
213 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
214 "either pos or kw args are allowed"));
215 }
216
217 wlan_if_obj_t *self = MP_OBJ_TO_PTR(args[0]);
218 union {
219 struct station_config sta;
220 struct softap_config ap;
221 } cfg;
222
223 if (self->if_id == STATION_IF) {
224 error_check(wifi_station_get_config(&cfg.sta), "can't get STA config");
225 } else {
226 error_check(wifi_softap_get_config(&cfg.ap), "can't get AP config");
227 }
228
229 if (kwargs->used != 0) {
230
231 for (mp_uint_t i = 0; i < kwargs->alloc; i++) {
232 if (MP_MAP_SLOT_IS_FILLED(kwargs, i)) {
233 #define QS(x) (uintptr_t)MP_OBJ_NEW_QSTR(x)
234 switch ((uintptr_t)kwargs->table[i].key) {
235 case QS(MP_QSTR_essid): {
236 mp_uint_t len;
237 const char *s = mp_obj_str_get_data(kwargs->table[i].value, &len);
238 len = MIN(len, sizeof(cfg.ap.ssid));
239 memcpy(cfg.ap.ssid, s, len);
240 cfg.ap.ssid_len = len;
241 break;
242 }
243 default:
244 goto unknown;
245 }
246 #undef QS
247 }
248 }
249
250 if (self->if_id == STATION_IF) {
251 error_check(wifi_station_set_config(&cfg.sta), "can't set STA config");
252 } else {
253 error_check(wifi_softap_set_config(&cfg.ap), "can't set AP config");
254 }
255
256 return mp_const_none;
257 }
258
259 // Get config
260
261 if (n_args != 2) {
262 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
263 "can query only one param"));
264 }
265
266 #define QS(x) (uintptr_t)MP_OBJ_NEW_QSTR(x)
267 switch ((uintptr_t)args[1]) {
268 case QS(MP_QSTR_essid):
269 return mp_obj_new_str((char*)cfg.ap.ssid, cfg.ap.ssid_len, false);
270 }
271 #undef QS
272
273unknown:
274 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
275 "unknown config param"));
276}
277STATIC MP_DEFINE_CONST_FUN_OBJ_KW(esp_config_obj, 1, esp_config);
278
Paul Sokolovsky9e8396a2016-02-13 19:26:17 +0200279STATIC const mp_map_elem_t wlan_if_locals_dict_table[] = {
Paul Sokolovsky1c43a0f2016-02-19 18:44:13 +0200280 { MP_OBJ_NEW_QSTR(MP_QSTR_active), (mp_obj_t)&esp_active_obj },
Paul Sokolovsky32eb4b92015-06-12 17:26:10 +0300281 { MP_OBJ_NEW_QSTR(MP_QSTR_connect), (mp_obj_t)&esp_connect_obj },
282 { MP_OBJ_NEW_QSTR(MP_QSTR_disconnect), (mp_obj_t)&esp_disconnect_obj },
Bill Owens60ccb412015-06-18 05:54:29 -0700283 { MP_OBJ_NEW_QSTR(MP_QSTR_status), (mp_obj_t)&esp_status_obj },
Bill Owens686516f2015-06-16 10:27:12 -0700284 { MP_OBJ_NEW_QSTR(MP_QSTR_scan), (mp_obj_t)&esp_scan_obj },
Bill Owense2bfa472015-06-22 13:22:17 -0700285 { MP_OBJ_NEW_QSTR(MP_QSTR_isconnected), (mp_obj_t)&esp_isconnected_obj },
Paul Sokolovskyfce00362015-12-27 10:03:32 +0200286 { MP_OBJ_NEW_QSTR(MP_QSTR_mac), (mp_obj_t)&esp_mac_obj },
Paul Sokolovskya49c1602016-02-19 19:50:36 +0200287 { MP_OBJ_NEW_QSTR(MP_QSTR_config), (mp_obj_t)&esp_config_obj },
Damien George02ea74d2015-12-22 14:28:53 +0000288 { MP_OBJ_NEW_QSTR(MP_QSTR_ifconfig), (mp_obj_t)&esp_ifconfig_obj },
Paul Sokolovsky9e8396a2016-02-13 19:26:17 +0200289};
290
291STATIC MP_DEFINE_CONST_DICT(wlan_if_locals_dict, wlan_if_locals_dict_table);
292
293const mp_obj_type_t wlan_if_type = {
294 { &mp_type_type },
295 .name = MP_QSTR_WLAN,
296 .locals_dict = (mp_obj_t)&wlan_if_locals_dict,
297};
298
Paul Sokolovsky7378c502016-02-19 12:53:45 +0200299STATIC mp_obj_t esp_wifi_mode(mp_uint_t n_args, const mp_obj_t *args) {
300 if (n_args == 0) {
301 return mp_obj_new_int(wifi_get_opmode());
302 } else {
303 wifi_set_opmode(mp_obj_get_int(args[0]));
304 return mp_const_none;
305 }
306}
307STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp_wifi_mode_obj, 0, 1, esp_wifi_mode);
308
309STATIC mp_obj_t esp_phy_mode(mp_uint_t n_args, const mp_obj_t *args) {
310 if (n_args == 0) {
311 return mp_obj_new_int(wifi_get_phy_mode());
312 } else {
313 wifi_set_phy_mode(mp_obj_get_int(args[0]));
314 return mp_const_none;
315 }
316}
317STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp_phy_mode_obj, 0, 1, esp_phy_mode);
318
Paul Sokolovsky9e8396a2016-02-13 19:26:17 +0200319STATIC const mp_map_elem_t mp_module_network_globals_table[] = {
320 { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_network) },
321 { MP_OBJ_NEW_QSTR(MP_QSTR_WLAN), (mp_obj_t)&get_wlan_obj },
Paul Sokolovsky7378c502016-02-19 12:53:45 +0200322 { MP_OBJ_NEW_QSTR(MP_QSTR_wifi_mode), (mp_obj_t)&esp_wifi_mode_obj },
323 { MP_OBJ_NEW_QSTR(MP_QSTR_phy_mode), (mp_obj_t)&esp_phy_mode_obj },
Bill Owens60ccb412015-06-18 05:54:29 -0700324
325#if MODNETWORK_INCLUDE_CONSTANTS
326 { MP_OBJ_NEW_QSTR(MP_QSTR_STAT_IDLE),
327 MP_OBJ_NEW_SMALL_INT(STATION_IDLE)},
328 { MP_OBJ_NEW_QSTR(MP_QSTR_STAT_CONNECTING),
329 MP_OBJ_NEW_SMALL_INT(STATION_CONNECTING)},
330 { MP_OBJ_NEW_QSTR(MP_QSTR_STAT_WRONG_PASSWORD),
331 MP_OBJ_NEW_SMALL_INT(STATION_WRONG_PASSWORD)},
332 { MP_OBJ_NEW_QSTR(MP_QSTR_STAT_NO_AP_FOUND),
333 MP_OBJ_NEW_SMALL_INT(STATION_NO_AP_FOUND)},
334 { MP_OBJ_NEW_QSTR(MP_QSTR_STAT_CONNECT_FAIL),
335 MP_OBJ_NEW_SMALL_INT(STATION_CONNECT_FAIL)},
336 { MP_OBJ_NEW_QSTR(MP_QSTR_STAT_GOT_IP),
337 MP_OBJ_NEW_SMALL_INT(STATION_GOT_IP)},
338#endif
Paul Sokolovskyee3fec32015-06-12 17:16:52 +0300339};
340
341STATIC MP_DEFINE_CONST_DICT(mp_module_network_globals, mp_module_network_globals_table);
342
343const mp_obj_module_t network_module = {
344 .base = { &mp_type_module },
345 .name = MP_QSTR_network,
346 .globals = (mp_obj_dict_t*)&mp_module_network_globals,
347};