blob: 4aa252cf563ecda707c065ba919997cb8fee8cd8 [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 Sokolovskyee3fec32015-06-12 17:16:52 +0300119STATIC const mp_map_elem_t mp_module_network_globals_table[] = {
120 { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_network) },
121 // MicroPython "network" module interface requires it to contains classes
122 // to instantiate. But as we have just a static network interace,
123 // use module as a "class", and just make all methods module-global
124 // functions.
125 { MP_OBJ_NEW_QSTR(MP_QSTR_WLAN), (mp_obj_t)&get_module_obj },
Paul Sokolovsky32eb4b92015-06-12 17:26:10 +0300126 { MP_OBJ_NEW_QSTR(MP_QSTR_connect), (mp_obj_t)&esp_connect_obj },
127 { MP_OBJ_NEW_QSTR(MP_QSTR_disconnect), (mp_obj_t)&esp_disconnect_obj },
Bill Owens60ccb412015-06-18 05:54:29 -0700128 { MP_OBJ_NEW_QSTR(MP_QSTR_status), (mp_obj_t)&esp_status_obj },
Bill Owens686516f2015-06-16 10:27:12 -0700129 { MP_OBJ_NEW_QSTR(MP_QSTR_scan), (mp_obj_t)&esp_scan_obj },
Bill Owense2bfa472015-06-22 13:22:17 -0700130 { MP_OBJ_NEW_QSTR(MP_QSTR_isconnected), (mp_obj_t)&esp_isconnected_obj },
Bill Owens60ccb412015-06-18 05:54:29 -0700131
132#if MODNETWORK_INCLUDE_CONSTANTS
133 { MP_OBJ_NEW_QSTR(MP_QSTR_STAT_IDLE),
134 MP_OBJ_NEW_SMALL_INT(STATION_IDLE)},
135 { MP_OBJ_NEW_QSTR(MP_QSTR_STAT_CONNECTING),
136 MP_OBJ_NEW_SMALL_INT(STATION_CONNECTING)},
137 { MP_OBJ_NEW_QSTR(MP_QSTR_STAT_WRONG_PASSWORD),
138 MP_OBJ_NEW_SMALL_INT(STATION_WRONG_PASSWORD)},
139 { MP_OBJ_NEW_QSTR(MP_QSTR_STAT_NO_AP_FOUND),
140 MP_OBJ_NEW_SMALL_INT(STATION_NO_AP_FOUND)},
141 { MP_OBJ_NEW_QSTR(MP_QSTR_STAT_CONNECT_FAIL),
142 MP_OBJ_NEW_SMALL_INT(STATION_CONNECT_FAIL)},
143 { MP_OBJ_NEW_QSTR(MP_QSTR_STAT_GOT_IP),
144 MP_OBJ_NEW_SMALL_INT(STATION_GOT_IP)},
145#endif
Paul Sokolovskyee3fec32015-06-12 17:16:52 +0300146};
147
148STATIC MP_DEFINE_CONST_DICT(mp_module_network_globals, mp_module_network_globals_table);
149
150const mp_obj_module_t network_module = {
151 .base = { &mp_type_module },
152 .name = MP_QSTR_network,
153 .globals = (mp_obj_dict_t*)&mp_module_network_globals,
154};