blob: 7631b9179d051f0b026ad9b89a8e9d56155cc5db [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"
39#include "ip_addr.h"
40#include "spi_flash.h"
41#include "utils.h"
Paul Sokolovskyee3fec32015-06-12 17:16:52 +030042
Paul Sokolovsky32eb4b92015-06-12 17:26:10 +030043void error_check(bool status, const char *msg);
Paul Sokolovskyee3fec32015-06-12 17:16:52 +030044extern const mp_obj_module_t network_module;
45
46STATIC mp_obj_t get_module() {
47 return (mp_obj_t)&network_module;
48}
49STATIC MP_DEFINE_CONST_FUN_OBJ_0(get_module_obj, get_module);
50
Paul Sokolovsky32eb4b92015-06-12 17:26:10 +030051STATIC mp_obj_t esp_connect(mp_uint_t n_args, const mp_obj_t *args) {
52 struct station_config config = {{0}};
53 mp_uint_t len;
54 const char *p;
55
56 p = mp_obj_str_get_data(args[0], &len);
57 memcpy(config.ssid, p, len);
58 p = mp_obj_str_get_data(args[1], &len);
59 memcpy(config.password, p, len);
60
61 error_check(wifi_station_set_config(&config), "Cannot set STA config");
62 error_check(wifi_station_connect(), "Cannot connect to AP");
63
64 return mp_const_none;
65}
66STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp_connect_obj, 2, 6, esp_connect);
67
68STATIC mp_obj_t esp_disconnect() {
69 error_check(wifi_station_disconnect(), "Cannot disconnect from AP");
70 return mp_const_none;
71}
72STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp_disconnect_obj, esp_disconnect);
73
Bill Owens60ccb412015-06-18 05:54:29 -070074#define MODNETWORK_INCLUDE_CONSTANTS (1)
75
76STATIC mp_obj_t esp_status() {
77 return MP_OBJ_NEW_SMALL_INT(wifi_station_get_connect_status());
78}
79STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp_status_obj, esp_status);
80
Bill Owens686516f2015-06-16 10:27:12 -070081STATIC void esp_scan_cb(scaninfo *si, STATUS status) {
82 struct bss_info *bs;
83 if (si->pbss) {
84 STAILQ_FOREACH(bs, si->pbss, next) {
85 mp_obj_tuple_t *t = mp_obj_new_tuple(6, NULL);
86 t->items[0] = mp_obj_new_bytes(bs->ssid, strlen((char*)bs->ssid));
87 t->items[1] = mp_obj_new_bytes(bs->bssid, sizeof(bs->bssid));
88 t->items[2] = MP_OBJ_NEW_SMALL_INT(bs->channel);
89 t->items[3] = MP_OBJ_NEW_SMALL_INT(bs->rssi);
90 t->items[4] = MP_OBJ_NEW_SMALL_INT(bs->authmode);
91 t->items[5] = MP_OBJ_NEW_SMALL_INT(bs->is_hidden);
92 call_function_1_protected(MP_STATE_PORT(scan_cb_obj), t);
93 }
94 }
95}
96
97STATIC mp_obj_t esp_scan(mp_obj_t cb_in) {
98 MP_STATE_PORT(scan_cb_obj) = cb_in;
99 if (wifi_get_opmode() == SOFTAP_MODE) {
100 nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError,
101 "Scan not supported in AP mode"));
102 }
103 wifi_station_scan(NULL, (scan_done_cb_t)esp_scan_cb);
104 return mp_const_none;
105}
106STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp_scan_obj, esp_scan);
107
Bill Owense2bfa472015-06-22 13:22:17 -0700108/// \method isconnected()
109/// Return True if connected to an AP and an IP address has been assigned,
110/// false otherwise.
111STATIC mp_obj_t esp_isconnected() {
112 if (wifi_station_get_connect_status() == STATION_GOT_IP) {
113 return mp_const_true;
114 }
115 return mp_const_false;
116}
117
118STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp_isconnected_obj, esp_isconnected);
119
Paul Sokolovskyee3fec32015-06-12 17:16:52 +0300120STATIC const mp_map_elem_t mp_module_network_globals_table[] = {
121 { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_network) },
122 // MicroPython "network" module interface requires it to contains classes
123 // to instantiate. But as we have just a static network interace,
124 // use module as a "class", and just make all methods module-global
125 // functions.
126 { MP_OBJ_NEW_QSTR(MP_QSTR_WLAN), (mp_obj_t)&get_module_obj },
Paul Sokolovsky32eb4b92015-06-12 17:26:10 +0300127 { MP_OBJ_NEW_QSTR(MP_QSTR_connect), (mp_obj_t)&esp_connect_obj },
128 { MP_OBJ_NEW_QSTR(MP_QSTR_disconnect), (mp_obj_t)&esp_disconnect_obj },
Bill Owens60ccb412015-06-18 05:54:29 -0700129 { MP_OBJ_NEW_QSTR(MP_QSTR_status), (mp_obj_t)&esp_status_obj },
Bill Owens686516f2015-06-16 10:27:12 -0700130 { MP_OBJ_NEW_QSTR(MP_QSTR_scan), (mp_obj_t)&esp_scan_obj },
Bill Owense2bfa472015-06-22 13:22:17 -0700131 { MP_OBJ_NEW_QSTR(MP_QSTR_isconnected), (mp_obj_t)&esp_isconnected_obj },
Bill Owens60ccb412015-06-18 05:54:29 -0700132
133#if MODNETWORK_INCLUDE_CONSTANTS
134 { MP_OBJ_NEW_QSTR(MP_QSTR_STAT_IDLE),
135 MP_OBJ_NEW_SMALL_INT(STATION_IDLE)},
136 { MP_OBJ_NEW_QSTR(MP_QSTR_STAT_CONNECTING),
137 MP_OBJ_NEW_SMALL_INT(STATION_CONNECTING)},
138 { MP_OBJ_NEW_QSTR(MP_QSTR_STAT_WRONG_PASSWORD),
139 MP_OBJ_NEW_SMALL_INT(STATION_WRONG_PASSWORD)},
140 { MP_OBJ_NEW_QSTR(MP_QSTR_STAT_NO_AP_FOUND),
141 MP_OBJ_NEW_SMALL_INT(STATION_NO_AP_FOUND)},
142 { MP_OBJ_NEW_QSTR(MP_QSTR_STAT_CONNECT_FAIL),
143 MP_OBJ_NEW_SMALL_INT(STATION_CONNECT_FAIL)},
144 { MP_OBJ_NEW_QSTR(MP_QSTR_STAT_GOT_IP),
145 MP_OBJ_NEW_SMALL_INT(STATION_GOT_IP)},
146#endif
Paul Sokolovskyee3fec32015-06-12 17:16:52 +0300147};
148
149STATIC MP_DEFINE_CONST_DICT(mp_module_network_globals, mp_module_network_globals_table);
150
151const mp_obj_module_t network_module = {
152 .base = { &mp_type_module },
153 .name = MP_QSTR_network,
154 .globals = (mp_obj_dict_t*)&mp_module_network_globals,
155};