2015-02-06 14:35:48 +00: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 18:53:33 +01:00
|
|
|
#include <std.h>
|
2015-02-06 14:35:48 +00:00
|
|
|
|
2015-02-21 18:58:43 +00:00
|
|
|
#include "py/mpstate.h"
|
2015-10-19 10:41:29 +01:00
|
|
|
#include "py/obj.h"
|
|
|
|
#include "py/nlr.h"
|
|
|
|
#include "py/runtime.h"
|
2015-10-30 23:03:58 +00:00
|
|
|
#include "py/mphal.h"
|
2015-02-06 14:35:48 +00:00
|
|
|
#include "modnetwork.h"
|
|
|
|
#include "mpexception.h"
|
2015-03-19 16:06:20 +00:00
|
|
|
#include "serverstask.h"
|
2015-05-22 18:53:33 +01:00
|
|
|
#include "simplelink.h"
|
|
|
|
|
2015-02-06 14:35:48 +00:00
|
|
|
|
2015-10-19 10:41:29 +01: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 14:35:48 +00:00
|
|
|
/// \module network - network configuration
|
|
|
|
///
|
2015-06-03 16:28:03 +01:00
|
|
|
/// This module provides network drivers and server configuration.
|
2015-02-06 14:35:48 +00:00
|
|
|
|
2015-02-27 15:50:06 +00:00
|
|
|
void mod_network_init0(void) {
|
2015-02-06 14:35:48 +00:00
|
|
|
}
|
|
|
|
|
2015-03-19 16:06:20 +00:00
|
|
|
#if (MICROPY_PORT_HAS_TELNET || MICROPY_PORT_HAS_FTP)
|
2015-10-19 10:41:29 +01: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 16:28:03 +01:00
|
|
|
}
|
2015-03-19 16:06:20 +00:00
|
|
|
|
2015-10-19 10:41:29 +01: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 16:28:03 +01:00
|
|
|
}
|
2015-10-19 10:41:29 +01: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 16:06:20 +00:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
2015-10-19 10:41:29 +01: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} },
|
|
|
|
};
|
2016-01-03 15:55:55 +00:00
|
|
|
STATIC mp_obj_t network_server_make_new(const mp_obj_type_t *type, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *all_args) {
|
2015-10-19 10:41:29 +01:00
|
|
|
// parse args
|
2015-10-19 14:19:34 +01:00
|
|
|
mp_map_t kw_args;
|
|
|
|
mp_map_init_fixed_table(&kw_args, n_kw, all_args + n_args);
|
2015-10-19 10:41:29 +01:00
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(network_server_args)];
|
2015-10-19 14:19:34 +01:00
|
|
|
mp_arg_parse_all(n_args, all_args, &kw_args, MP_ARRAY_SIZE(args), network_server_args, args);
|
2015-10-19 10:41:29 +01:00
|
|
|
|
|
|
|
// check the server id
|
|
|
|
if (args[0].u_obj != MP_OBJ_NULL) {
|
|
|
|
if (mp_obj_get_int(args[0].u_obj) != 0) {
|
2016-10-17 23:53:43 +01:00
|
|
|
mp_raise_msg(&mp_type_OSError, mpexception_os_resource_not_avaliable);
|
2015-10-19 10:41:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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]);
|
|
|
|
|
2015-10-19 14:19:34 +01:00
|
|
|
return (mp_obj_t)self;
|
2015-10-19 10:41:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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 16:28:03 +01: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 10:41:29 +01:00
|
|
|
if (n_args > 1) {
|
|
|
|
uint32_t timeout = mp_obj_get_int(args[1]);
|
|
|
|
servers_set_timeout(timeout * 1000);
|
2015-06-03 16:28:03 +01:00
|
|
|
return mp_const_none;
|
|
|
|
} else {
|
|
|
|
// get
|
|
|
|
return mp_obj_new_int(servers_get_timeout() / 1000);
|
|
|
|
}
|
|
|
|
}
|
2015-10-19 10:41:29 +01: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 16:06:20 +00:00
|
|
|
#endif
|
|
|
|
|
2015-02-06 14:35:48 +00:00
|
|
|
STATIC const mp_map_elem_t mp_module_network_globals_table[] = {
|
2015-03-19 16:06:20 +00: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)
|
2016-02-22 21:51:22 +00:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_Server), (mp_obj_t)&network_server_type },
|
2015-03-19 16:06:20 +00:00
|
|
|
#endif
|
2015-02-06 14:35:48 +00: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 },
|
|
|
|
.globals = (mp_obj_dict_t*)&mp_module_network_globals,
|
|
|
|
};
|
2015-10-19 10:41:29 +01: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 },
|
2015-10-19 14:19:34 +01:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_isrunning), (mp_obj_t)&network_server_running_obj },
|
2015-10-19 10:41:29 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
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 },
|
2016-02-22 21:51:22 +00:00
|
|
|
.name = MP_QSTR_Server,
|
2015-10-19 14:19:34 +01:00
|
|
|
.make_new = network_server_make_new,
|
2015-10-19 10:41:29 +01:00
|
|
|
.locals_dict = (mp_obj_t)&network_server_locals_dict,
|
|
|
|
};
|
|
|
|
#endif
|