esp8266: Move connect/disconnect from "esp" module to network.

This commit is contained in:
Paul Sokolovsky 2015-06-12 17:26:10 +03:00
parent ee3fec3167
commit 32eb4b9055
2 changed files with 34 additions and 27 deletions

View File

@ -503,7 +503,7 @@ STATIC const mp_obj_type_t esp_socket_type = {
#define MODESP_INCLUDE_CONSTANTS (1)
static void error_check(bool status, const char *msg) {
void error_check(bool status, const char *msg) {
if (!status) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, msg));
}
@ -534,29 +534,6 @@ STATIC mp_obj_t esp_scan(mp_obj_t cb_in) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp_scan_obj, esp_scan);
STATIC mp_obj_t esp_connect(mp_uint_t n_args, const mp_obj_t *args) {
struct station_config config = {{0}};
mp_uint_t len;
const char *p;
p = mp_obj_str_get_data(args[0], &len);
memcpy(config.ssid, p, len);
p = mp_obj_str_get_data(args[1], &len);
memcpy(config.password, p, len);
error_check(wifi_station_set_config(&config), "Cannot set STA config");
error_check(wifi_station_connect(), "Cannot connect to AP");
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp_connect_obj, 2, 6, esp_connect);
STATIC mp_obj_t esp_disconnect() {
error_check(wifi_station_disconnect(), "Cannot disconnect from AP");
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp_disconnect_obj, esp_disconnect);
STATIC mp_obj_t esp_status() {
return MP_OBJ_NEW_SMALL_INT(wifi_station_get_connect_status());
}
@ -616,8 +593,6 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp_flash_id_obj, esp_flash_id);
STATIC const mp_map_elem_t esp_module_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_esp) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_connect), (mp_obj_t)&esp_connect_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_disconnect), (mp_obj_t)&esp_disconnect_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_scan), (mp_obj_t)&esp_scan_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_status), (mp_obj_t)&esp_status_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_mac), (mp_obj_t)&esp_mac_obj },

View File

@ -32,8 +32,15 @@
#include "py/nlr.h"
#include "py/objlist.h"
#include "py/runtime.h"
#include "modnetwork.h"
#include "netutils.h"
#include "queue.h"
#include "user_interface.h"
#include "espconn.h"
#include "ip_addr.h"
#include "spi_flash.h"
#include "utils.h"
void error_check(bool status, const char *msg);
extern const mp_obj_module_t network_module;
STATIC mp_obj_t get_module() {
@ -41,6 +48,29 @@ STATIC mp_obj_t get_module() {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(get_module_obj, get_module);
STATIC mp_obj_t esp_connect(mp_uint_t n_args, const mp_obj_t *args) {
struct station_config config = {{0}};
mp_uint_t len;
const char *p;
p = mp_obj_str_get_data(args[0], &len);
memcpy(config.ssid, p, len);
p = mp_obj_str_get_data(args[1], &len);
memcpy(config.password, p, len);
error_check(wifi_station_set_config(&config), "Cannot set STA config");
error_check(wifi_station_connect(), "Cannot connect to AP");
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp_connect_obj, 2, 6, esp_connect);
STATIC mp_obj_t esp_disconnect() {
error_check(wifi_station_disconnect(), "Cannot disconnect from AP");
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp_disconnect_obj, esp_disconnect);
STATIC const mp_map_elem_t mp_module_network_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_network) },
// MicroPython "network" module interface requires it to contains classes
@ -48,6 +78,8 @@ STATIC const mp_map_elem_t mp_module_network_globals_table[] = {
// use module as a "class", and just make all methods module-global
// functions.
{ MP_OBJ_NEW_QSTR(MP_QSTR_WLAN), (mp_obj_t)&get_module_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_connect), (mp_obj_t)&esp_connect_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_disconnect), (mp_obj_t)&esp_disconnect_obj },
};
STATIC MP_DEFINE_CONST_DICT(mp_module_network_globals, mp_module_network_globals_table);