blob: 28d4989c0a67230f800ee8ff3dff9e494b9f4422 [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
Paul Sokolovskyee3fec32015-06-12 17:16:52 +030074STATIC const mp_map_elem_t mp_module_network_globals_table[] = {
75 { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_network) },
76 // MicroPython "network" module interface requires it to contains classes
77 // to instantiate. But as we have just a static network interace,
78 // use module as a "class", and just make all methods module-global
79 // functions.
80 { MP_OBJ_NEW_QSTR(MP_QSTR_WLAN), (mp_obj_t)&get_module_obj },
Paul Sokolovsky32eb4b92015-06-12 17:26:10 +030081 { MP_OBJ_NEW_QSTR(MP_QSTR_connect), (mp_obj_t)&esp_connect_obj },
82 { MP_OBJ_NEW_QSTR(MP_QSTR_disconnect), (mp_obj_t)&esp_disconnect_obj },
Paul Sokolovskyee3fec32015-06-12 17:16:52 +030083};
84
85STATIC MP_DEFINE_CONST_DICT(mp_module_network_globals, mp_module_network_globals_table);
86
87const mp_obj_module_t network_module = {
88 .base = { &mp_type_module },
89 .name = MP_QSTR_network,
90 .globals = (mp_obj_dict_t*)&mp_module_network_globals,
91};