blob: 79d71218624ec06409c7fb35be84d3c987d6bb94 [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 Sokolovsky32eb4b92015-06-12 17:26:10 +030042void error_check(bool status, const char *msg);
Paul Sokolovskyee3fec32015-06-12 17:16:52 +030043extern const mp_obj_module_t network_module;
44
45STATIC mp_obj_t get_module() {
46 return (mp_obj_t)&network_module;
47}
48STATIC MP_DEFINE_CONST_FUN_OBJ_0(get_module_obj, get_module);
49
Paul Sokolovsky32eb4b92015-06-12 17:26:10 +030050STATIC mp_obj_t esp_connect(mp_uint_t n_args, const mp_obj_t *args) {
51 struct station_config config = {{0}};
52 mp_uint_t len;
53 const char *p;
54
55 p = mp_obj_str_get_data(args[0], &len);
56 memcpy(config.ssid, p, len);
57 p = mp_obj_str_get_data(args[1], &len);
58 memcpy(config.password, p, len);
59
60 error_check(wifi_station_set_config(&config), "Cannot set STA config");
61 error_check(wifi_station_connect(), "Cannot connect to AP");
62
63 return mp_const_none;
64}
65STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp_connect_obj, 2, 6, esp_connect);
66
67STATIC mp_obj_t esp_disconnect() {
68 error_check(wifi_station_disconnect(), "Cannot disconnect from AP");
69 return mp_const_none;
70}
71STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp_disconnect_obj, esp_disconnect);
72
Bill Owens60ccb412015-06-18 05:54:29 -070073#define MODNETWORK_INCLUDE_CONSTANTS (1)
74
75STATIC mp_obj_t esp_status() {
76 return MP_OBJ_NEW_SMALL_INT(wifi_station_get_connect_status());
77}
78STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp_status_obj, esp_status);
79
Bill Owens686516f2015-06-16 10:27:12 -070080STATIC void esp_scan_cb(scaninfo *si, STATUS status) {
81 struct bss_info *bs;
82 if (si->pbss) {
83 STAILQ_FOREACH(bs, si->pbss, next) {
84 mp_obj_tuple_t *t = mp_obj_new_tuple(6, NULL);
85 t->items[0] = mp_obj_new_bytes(bs->ssid, strlen((char*)bs->ssid));
86 t->items[1] = mp_obj_new_bytes(bs->bssid, sizeof(bs->bssid));
87 t->items[2] = MP_OBJ_NEW_SMALL_INT(bs->channel);
88 t->items[3] = MP_OBJ_NEW_SMALL_INT(bs->rssi);
89 t->items[4] = MP_OBJ_NEW_SMALL_INT(bs->authmode);
90 t->items[5] = MP_OBJ_NEW_SMALL_INT(bs->is_hidden);
91 call_function_1_protected(MP_STATE_PORT(scan_cb_obj), t);
92 }
93 }
94}
95
96STATIC mp_obj_t esp_scan(mp_obj_t cb_in) {
97 MP_STATE_PORT(scan_cb_obj) = cb_in;
98 if (wifi_get_opmode() == SOFTAP_MODE) {
99 nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError,
100 "Scan not supported in AP mode"));
101 }
102 wifi_station_scan(NULL, (scan_done_cb_t)esp_scan_cb);
103 return mp_const_none;
104}
105STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp_scan_obj, esp_scan);
106
Bill Owense2bfa472015-06-22 13:22:17 -0700107/// \method isconnected()
108/// Return True if connected to an AP and an IP address has been assigned,
109/// false otherwise.
110STATIC mp_obj_t esp_isconnected() {
111 if (wifi_station_get_connect_status() == STATION_GOT_IP) {
112 return mp_const_true;
113 }
114 return mp_const_false;
115}
116
117STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp_isconnected_obj, esp_isconnected);
118
Paul Sokolovskyfce00362015-12-27 10:03:32 +0200119STATIC mp_obj_t esp_mac(mp_uint_t n_args, const mp_obj_t *args) {
120 uint8_t mac[6];
121 if (n_args == 0) {
122 wifi_get_macaddr(STATION_IF, mac);
123 return mp_obj_new_bytes(mac, sizeof(mac));
124 } else {
125 mp_buffer_info_t bufinfo;
126 mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_READ);
127
128 if (bufinfo.len != 6) {
129 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
130 "invalid buffer length"));
131 }
132
133 wifi_set_macaddr(STATION_IF, bufinfo.buf);
134 return mp_const_none;
135 }
136}
137STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp_mac_obj, 0, 1, esp_mac);
138
Damien George02ea74d2015-12-22 14:28:53 +0000139STATIC mp_obj_t esp_ifconfig(void) {
140 struct ip_info info;
141 wifi_get_ip_info(STATION_IF, &info);
142 mp_obj_t ifconfig[4] = {
143 netutils_format_ipv4_addr((uint8_t*)&info.ip, NETUTILS_BIG),
144 netutils_format_ipv4_addr((uint8_t*)&info.netmask, NETUTILS_BIG),
145 netutils_format_ipv4_addr((uint8_t*)&info.gw, NETUTILS_BIG),
146 MP_OBJ_NEW_QSTR(MP_QSTR_), // no DNS server
147 };
148 return mp_obj_new_tuple(4, ifconfig);
149}
150STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp_ifconfig_obj, esp_ifconfig);
151
Paul Sokolovskyee3fec32015-06-12 17:16:52 +0300152STATIC const mp_map_elem_t mp_module_network_globals_table[] = {
153 { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_network) },
154 // MicroPython "network" module interface requires it to contains classes
155 // to instantiate. But as we have just a static network interace,
156 // use module as a "class", and just make all methods module-global
157 // functions.
158 { MP_OBJ_NEW_QSTR(MP_QSTR_WLAN), (mp_obj_t)&get_module_obj },
Paul Sokolovsky32eb4b92015-06-12 17:26:10 +0300159 { MP_OBJ_NEW_QSTR(MP_QSTR_connect), (mp_obj_t)&esp_connect_obj },
160 { MP_OBJ_NEW_QSTR(MP_QSTR_disconnect), (mp_obj_t)&esp_disconnect_obj },
Bill Owens60ccb412015-06-18 05:54:29 -0700161 { MP_OBJ_NEW_QSTR(MP_QSTR_status), (mp_obj_t)&esp_status_obj },
Bill Owens686516f2015-06-16 10:27:12 -0700162 { MP_OBJ_NEW_QSTR(MP_QSTR_scan), (mp_obj_t)&esp_scan_obj },
Bill Owense2bfa472015-06-22 13:22:17 -0700163 { MP_OBJ_NEW_QSTR(MP_QSTR_isconnected), (mp_obj_t)&esp_isconnected_obj },
Paul Sokolovskyfce00362015-12-27 10:03:32 +0200164 { MP_OBJ_NEW_QSTR(MP_QSTR_mac), (mp_obj_t)&esp_mac_obj },
Damien George02ea74d2015-12-22 14:28:53 +0000165 { MP_OBJ_NEW_QSTR(MP_QSTR_ifconfig), (mp_obj_t)&esp_ifconfig_obj },
Bill Owens60ccb412015-06-18 05:54:29 -0700166
167#if MODNETWORK_INCLUDE_CONSTANTS
168 { MP_OBJ_NEW_QSTR(MP_QSTR_STAT_IDLE),
169 MP_OBJ_NEW_SMALL_INT(STATION_IDLE)},
170 { MP_OBJ_NEW_QSTR(MP_QSTR_STAT_CONNECTING),
171 MP_OBJ_NEW_SMALL_INT(STATION_CONNECTING)},
172 { MP_OBJ_NEW_QSTR(MP_QSTR_STAT_WRONG_PASSWORD),
173 MP_OBJ_NEW_SMALL_INT(STATION_WRONG_PASSWORD)},
174 { MP_OBJ_NEW_QSTR(MP_QSTR_STAT_NO_AP_FOUND),
175 MP_OBJ_NEW_SMALL_INT(STATION_NO_AP_FOUND)},
176 { MP_OBJ_NEW_QSTR(MP_QSTR_STAT_CONNECT_FAIL),
177 MP_OBJ_NEW_SMALL_INT(STATION_CONNECT_FAIL)},
178 { MP_OBJ_NEW_QSTR(MP_QSTR_STAT_GOT_IP),
179 MP_OBJ_NEW_SMALL_INT(STATION_GOT_IP)},
180#endif
Paul Sokolovskyee3fec32015-06-12 17:16:52 +0300181};
182
183STATIC MP_DEFINE_CONST_DICT(mp_module_network_globals, mp_module_network_globals_table);
184
185const mp_obj_module_t network_module = {
186 .base = { &mp_type_module },
187 .name = MP_QSTR_network,
188 .globals = (mp_obj_dict_t*)&mp_module_network_globals,
189};