2015-02-06 09:35:48 -05:00
|
|
|
/*
|
|
|
|
* This file is part of the Micro Python project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013, 2014 Damien P. George
|
|
|
|
* Copyright (c) 2015 Daniel Campora
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2015-05-22 13:53:33 -04:00
|
|
|
#include <std.h>
|
2015-02-06 09:35:48 -05:00
|
|
|
|
2015-02-21 13:58:43 -05:00
|
|
|
#include "py/mpstate.h"
|
2015-02-06 09:35:48 -05:00
|
|
|
#include MICROPY_HAL_H
|
2015-10-19 05:41:29 -04:00
|
|
|
#include "py/obj.h"
|
|
|
|
#include "py/nlr.h"
|
|
|
|
#include "py/runtime.h"
|
2015-02-06 09:35:48 -05:00
|
|
|
#include "modnetwork.h"
|
|
|
|
#include "mpexception.h"
|
2015-03-19 12:06:20 -04:00
|
|
|
#include "serverstask.h"
|
2015-05-22 13:53:33 -04:00
|
|
|
#include "simplelink.h"
|
|
|
|
|
2015-02-06 09:35:48 -05:00
|
|
|
|
2015-10-19 05:41:29 -04:00
|
|
|
/******************************************************************************
|
|
|
|
DEFINE TYPES
|
|
|
|
******************************************************************************/
|
|
|
|
typedef struct {
|
|
|
|
mp_obj_base_t base;
|
|
|
|
} network_server_obj_t;
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
DECLARE PRIVATE DATA
|
|
|
|
******************************************************************************/
|
|
|
|
STATIC network_server_obj_t network_server_obj;
|
|
|
|
STATIC const mp_obj_type_t network_server_type;
|
|
|
|
|
2015-02-06 09:35:48 -05:00
|
|
|
/// \module network - network configuration
|
|
|
|
///
|
2015-06-03 11:28:03 -04:00
|
|
|
/// This module provides network drivers and server configuration.
|
2015-02-06 09:35:48 -05:00
|
|
|
|
2015-02-27 10:50:06 -05:00
|
|
|
void mod_network_init0(void) {
|
2015-02-06 09:35:48 -05:00
|
|
|
}
|
|
|
|
|
2015-03-19 12:06:20 -04:00
|
|
|
#if (MICROPY_PORT_HAS_TELNET || MICROPY_PORT_HAS_FTP)
|
2015-10-19 05:41:29 -04:00
|
|
|
STATIC mp_obj_t network_server_init_helper(mp_obj_t self, const mp_arg_val_t *args) {
|
|
|
|
const char *user = SERVERS_DEF_USER;
|
|
|
|
const char *pass = SERVERS_DEF_PASS;
|
|
|
|
if (args[0].u_obj != MP_OBJ_NULL) {
|
|
|
|
mp_obj_t *login;
|
|
|
|
mp_obj_get_array_fixed_n(args[0].u_obj, 2, &login);
|
|
|
|
user = mp_obj_str_get_str(login[0]);
|
|
|
|
pass = mp_obj_str_get_str(login[1]);
|
2015-06-03 11:28:03 -04:00
|
|
|
}
|
2015-03-19 12:06:20 -04:00
|
|
|
|
2015-10-19 05:41:29 -04:00
|
|
|
uint32_t timeout = SERVERS_DEF_TIMEOUT_MS / 1000;
|
|
|
|
if (args[1].u_obj != MP_OBJ_NULL) {
|
|
|
|
timeout = mp_obj_get_int(args[1].u_obj);
|
2015-06-03 11:28:03 -04:00
|
|
|
}
|
2015-10-19 05:41:29 -04:00
|
|
|
|
|
|
|
// configure the new login
|
|
|
|
servers_set_login ((char *)user, (char *)pass);
|
|
|
|
|
|
|
|
// configure the timeout
|
|
|
|
servers_set_timeout(timeout * 1000);
|
|
|
|
|
|
|
|
// start the servers
|
|
|
|
servers_start();
|
|
|
|
|
2015-03-19 12:06:20 -04:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
2015-10-19 05:41:29 -04:00
|
|
|
|
|
|
|
STATIC const mp_arg_t network_server_args[] = {
|
|
|
|
{ MP_QSTR_id, MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
|
|
|
|
{ MP_QSTR_login, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
|
|
|
|
{ MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
|
|
|
|
};
|
|
|
|
STATIC mp_obj_t network_server_new (mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
|
|
|
// parse args
|
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(network_server_args)];
|
|
|
|
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(args), network_server_args, args);
|
|
|
|
|
|
|
|
// check the server id
|
|
|
|
if (args[0].u_obj != MP_OBJ_NULL) {
|
|
|
|
if (mp_obj_get_int(args[0].u_obj) != 0) {
|
|
|
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_resource_not_avaliable));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// setup the object and initialize it
|
|
|
|
network_server_obj_t *self = &network_server_obj;
|
|
|
|
self->base.type = &network_server_type;
|
|
|
|
network_server_init_helper(self, &args[1]);
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(network_server_new_obj, 0, network_server_new);
|
|
|
|
|
|
|
|
STATIC mp_obj_t network_server_init(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
|
|
|
// parse args
|
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(network_server_args) - 1];
|
|
|
|
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(args), &network_server_args[1], args);
|
|
|
|
return network_server_init_helper(pos_args[0], args);
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(network_server_init_obj, 1, network_server_init);
|
2015-06-03 11:28:03 -04:00
|
|
|
|
|
|
|
// timeout value given in seconds
|
|
|
|
STATIC mp_obj_t network_server_timeout(mp_uint_t n_args, const mp_obj_t *args) {
|
2015-10-19 05:41:29 -04:00
|
|
|
if (n_args > 1) {
|
|
|
|
uint32_t timeout = mp_obj_get_int(args[1]);
|
|
|
|
servers_set_timeout(timeout * 1000);
|
2015-06-03 11:28:03 -04:00
|
|
|
return mp_const_none;
|
|
|
|
} else {
|
|
|
|
// get
|
|
|
|
return mp_obj_new_int(servers_get_timeout() / 1000);
|
|
|
|
}
|
|
|
|
}
|
2015-10-19 05:41:29 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(network_server_timeout_obj, 1, 2, network_server_timeout);
|
|
|
|
|
|
|
|
STATIC mp_obj_t network_server_running(mp_obj_t self_in) {
|
|
|
|
// get
|
|
|
|
return mp_obj_new_bool(servers_are_enabled());
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(network_server_running_obj, network_server_running);
|
|
|
|
|
|
|
|
STATIC mp_obj_t network_server_deinit(mp_obj_t self_in) {
|
|
|
|
// simply stop the servers
|
|
|
|
servers_stop();
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(network_server_deinit_obj, network_server_deinit);
|
2015-03-19 12:06:20 -04:00
|
|
|
#endif
|
|
|
|
|
2015-02-06 09:35:48 -05:00
|
|
|
STATIC const mp_map_elem_t mp_module_network_globals_table[] = {
|
2015-03-19 12:06:20 -04:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_network) },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_WLAN), (mp_obj_t)&mod_network_nic_type_wlan },
|
|
|
|
|
|
|
|
#if (MICROPY_PORT_HAS_TELNET || MICROPY_PORT_HAS_FTP)
|
2015-10-19 05:41:29 -04:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_server), (mp_obj_t)&network_server_new_obj },
|
2015-03-19 12:06:20 -04:00
|
|
|
#endif
|
2015-02-06 09:35:48 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(mp_module_network_globals, mp_module_network_globals_table);
|
|
|
|
|
|
|
|
const mp_obj_module_t mp_module_network = {
|
|
|
|
.base = { &mp_type_module },
|
|
|
|
.name = MP_QSTR_network,
|
|
|
|
.globals = (mp_obj_dict_t*)&mp_module_network_globals,
|
|
|
|
};
|
2015-10-19 05:41:29 -04:00
|
|
|
|
|
|
|
#if (MICROPY_PORT_HAS_TELNET || MICROPY_PORT_HAS_FTP)
|
|
|
|
STATIC const mp_map_elem_t network_server_locals_dict_table[] = {
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_init), (mp_obj_t)&network_server_init_obj },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_deinit), (mp_obj_t)&network_server_deinit_obj },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_timeout), (mp_obj_t)&network_server_timeout_obj },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_running), (mp_obj_t)&network_server_running_obj },
|
|
|
|
};
|
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(network_server_locals_dict, network_server_locals_dict_table);
|
|
|
|
|
|
|
|
STATIC const mp_obj_type_t network_server_type = {
|
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_server,
|
|
|
|
.locals_dict = (mp_obj_t)&network_server_locals_dict,
|
|
|
|
};
|
|
|
|
#endif
|