2015-02-06 14:35:48 +00:00
|
|
|
/*
|
2017-06-30 08:22:17 +01:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2015-02-06 14:35:48 +00:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
2015-04-26 13:34:01 +01:00
|
|
|
#include <string.h>
|
2015-02-06 14:35:48 +00:00
|
|
|
|
2015-02-21 18:58:43 +00:00
|
|
|
#include "py/mpconfig.h"
|
|
|
|
#include "py/misc.h"
|
2016-10-17 23:53:43 +01:00
|
|
|
#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 "serverstask.h"
|
2015-04-26 13:34:01 +01:00
|
|
|
#include "simplelink.h"
|
2015-02-06 14:35:48 +00:00
|
|
|
#include "debug.h"
|
|
|
|
#include "telnet.h"
|
|
|
|
#include "ftp.h"
|
2015-02-25 10:08:51 +00:00
|
|
|
#include "pybwdt.h"
|
2015-05-22 18:53:33 +01:00
|
|
|
#include "modusocket.h"
|
2015-10-19 10:41:29 +01:00
|
|
|
#include "mpexception.h"
|
2016-02-21 20:28:19 +00:00
|
|
|
#include "modnetwork.h"
|
|
|
|
#include "modwlan.h"
|
2015-02-06 14:35:48 +00:00
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
DEFINE PRIVATE TYPES
|
|
|
|
******************************************************************************/
|
|
|
|
typedef struct {
|
2015-06-03 16:28:03 +01:00
|
|
|
uint32_t timeout;
|
|
|
|
bool enabled;
|
|
|
|
bool do_disable;
|
|
|
|
bool do_enable;
|
|
|
|
bool do_reset;
|
2016-02-21 20:28:19 +00:00
|
|
|
bool do_wlan_cycle_power;
|
2015-06-03 16:28:03 +01:00
|
|
|
} servers_data_t;
|
2015-02-06 14:35:48 +00:00
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
DECLARE PRIVATE DATA
|
|
|
|
******************************************************************************/
|
2016-02-21 20:28:19 +00:00
|
|
|
static servers_data_t servers_data = {.timeout = SERVERS_DEF_TIMEOUT_MS};
|
2015-05-22 18:53:33 +01:00
|
|
|
static volatile bool sleep_sockets = false;
|
2015-02-06 14:35:48 +00:00
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
DECLARE PRIVATE FUNCTIONS
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
DECLARE PUBLIC DATA
|
|
|
|
******************************************************************************/
|
2016-06-05 13:01:16 +01:00
|
|
|
|
|
|
|
// This is the static memory (TCB and stack) for the servers task
|
|
|
|
StaticTask_t svTaskTCB __attribute__ ((section (".rtos_heap")));
|
|
|
|
StackType_t svTaskStack[SERVERS_STACK_LEN] __attribute__ ((section (".rtos_heap"))) __attribute__((aligned (8)));
|
|
|
|
|
2015-04-26 13:34:01 +01:00
|
|
|
char servers_user[SERVERS_USER_PASS_LEN_MAX + 1];
|
|
|
|
char servers_pass[SERVERS_USER_PASS_LEN_MAX + 1];
|
2015-02-06 14:35:48 +00:00
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
DECLARE PUBLIC FUNCTIONS
|
|
|
|
******************************************************************************/
|
|
|
|
void TASK_Servers (void *pvParameters) {
|
|
|
|
|
|
|
|
bool cycle = false;
|
|
|
|
|
|
|
|
strcpy (servers_user, SERVERS_DEF_USER);
|
|
|
|
strcpy (servers_pass, SERVERS_DEF_PASS);
|
|
|
|
|
|
|
|
telnet_init();
|
|
|
|
ftp_init();
|
|
|
|
|
2020-02-27 04:36:53 +00:00
|
|
|
for ( ;;) {
|
2015-02-06 14:35:48 +00:00
|
|
|
|
2015-04-27 20:11:03 +01:00
|
|
|
if (servers_data.do_enable) {
|
2015-03-20 12:52:33 +00:00
|
|
|
// enable network services
|
2015-02-06 14:35:48 +00:00
|
|
|
telnet_enable();
|
|
|
|
ftp_enable();
|
2015-03-20 12:52:33 +00:00
|
|
|
// now set/clear the flags
|
2015-02-06 14:35:48 +00:00
|
|
|
servers_data.enabled = true;
|
2015-03-20 12:52:33 +00:00
|
|
|
servers_data.do_enable = false;
|
2020-02-27 04:36:53 +00:00
|
|
|
} else if (servers_data.do_disable) {
|
2015-04-27 20:11:03 +01:00
|
|
|
// disable network services
|
|
|
|
telnet_disable();
|
|
|
|
ftp_disable();
|
|
|
|
// now clear the flags
|
|
|
|
servers_data.do_disable = false;
|
|
|
|
servers_data.enabled = false;
|
2020-02-27 04:36:53 +00:00
|
|
|
} else if (servers_data.do_reset) {
|
2015-07-10 10:32:53 +01:00
|
|
|
// resetting the servers is needed to prevent half-open sockets
|
2015-05-23 18:56:22 +01:00
|
|
|
servers_data.do_reset = false;
|
2015-07-10 10:32:53 +01:00
|
|
|
if (servers_data.enabled) {
|
|
|
|
telnet_reset();
|
|
|
|
ftp_reset();
|
|
|
|
}
|
|
|
|
// and we should also close all user sockets. We do it here
|
|
|
|
// for convinience and to save on code size.
|
2015-05-23 18:56:22 +01:00
|
|
|
modusocket_close_all_user_sockets();
|
2015-04-27 20:11:03 +01:00
|
|
|
}
|
2015-06-03 16:28:03 +01:00
|
|
|
|
|
|
|
if (cycle) {
|
|
|
|
telnet_run();
|
2020-02-27 04:36:53 +00:00
|
|
|
} else {
|
2015-06-03 16:28:03 +01:00
|
|
|
ftp_run();
|
2015-02-06 14:35:48 +00:00
|
|
|
}
|
|
|
|
|
2015-05-22 18:53:33 +01:00
|
|
|
if (sleep_sockets) {
|
2015-05-25 17:54:43 +01:00
|
|
|
pybwdt_srv_sleeping(true);
|
2015-05-22 18:53:33 +01:00
|
|
|
modusocket_enter_sleep();
|
2015-05-25 17:54:43 +01:00
|
|
|
pybwdt_srv_sleeping(false);
|
2016-02-21 20:28:19 +00:00
|
|
|
mp_hal_delay_ms(SERVERS_CYCLE_TIME_MS * 2);
|
|
|
|
if (servers_data.do_wlan_cycle_power) {
|
|
|
|
servers_data.do_wlan_cycle_power = false;
|
|
|
|
wlan_off_on();
|
|
|
|
}
|
|
|
|
sleep_sockets = false;
|
|
|
|
|
2015-05-22 18:53:33 +01:00
|
|
|
}
|
|
|
|
|
2015-05-25 17:54:43 +01:00
|
|
|
// set the alive flag for the wdt
|
|
|
|
pybwdt_srv_alive();
|
|
|
|
|
2015-03-20 12:52:33 +00:00
|
|
|
// move to the next cycle
|
2015-02-06 14:35:48 +00:00
|
|
|
cycle = cycle ? false : true;
|
2015-10-29 17:38:44 +00:00
|
|
|
mp_hal_delay_ms(SERVERS_CYCLE_TIME_MS);
|
2015-02-06 14:35:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-19 16:06:20 +00:00
|
|
|
void servers_start (void) {
|
2015-02-06 14:35:48 +00:00
|
|
|
servers_data.do_enable = true;
|
2015-10-29 17:38:44 +00:00
|
|
|
mp_hal_delay_ms(SERVERS_CYCLE_TIME_MS * 3);
|
2015-03-20 12:52:33 +00:00
|
|
|
}
|
|
|
|
|
2015-03-19 16:06:20 +00:00
|
|
|
void servers_stop (void) {
|
2015-02-06 14:35:48 +00:00
|
|
|
servers_data.do_disable = true;
|
2015-03-19 16:06:20 +00:00
|
|
|
do {
|
2015-10-29 17:38:44 +00:00
|
|
|
mp_hal_delay_ms(SERVERS_CYCLE_TIME_MS);
|
2015-03-19 16:06:20 +00:00
|
|
|
} while (servers_are_enabled());
|
2015-10-29 17:38:44 +00:00
|
|
|
mp_hal_delay_ms(SERVERS_CYCLE_TIME_MS * 3);
|
2015-02-06 14:35:48 +00:00
|
|
|
}
|
|
|
|
|
2015-05-23 18:56:22 +01:00
|
|
|
void servers_reset (void) {
|
|
|
|
servers_data.do_reset = true;
|
|
|
|
}
|
|
|
|
|
2016-02-21 20:28:19 +00:00
|
|
|
void servers_wlan_cycle_power (void) {
|
|
|
|
servers_data.do_wlan_cycle_power = true;
|
|
|
|
}
|
|
|
|
|
2015-02-06 14:35:48 +00:00
|
|
|
bool servers_are_enabled (void) {
|
|
|
|
return servers_data.enabled;
|
|
|
|
}
|
|
|
|
|
2015-05-22 18:53:33 +01:00
|
|
|
void server_sleep_sockets (void) {
|
|
|
|
sleep_sockets = true;
|
2015-10-29 17:38:44 +00:00
|
|
|
mp_hal_delay_ms(SERVERS_CYCLE_TIME_MS + 1);
|
2015-05-22 18:53:33 +01:00
|
|
|
}
|
|
|
|
|
2015-03-19 16:06:20 +00:00
|
|
|
void servers_close_socket (int16_t *sd) {
|
2015-02-06 14:35:48 +00:00
|
|
|
if (*sd > 0) {
|
2015-05-22 18:53:33 +01:00
|
|
|
modusocket_socket_delete(*sd);
|
2015-02-06 14:35:48 +00:00
|
|
|
sl_Close(*sd);
|
|
|
|
*sd = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-19 16:06:20 +00:00
|
|
|
void servers_set_login (char *user, char *pass) {
|
2015-10-19 10:41:29 +01:00
|
|
|
if (strlen(user) > SERVERS_USER_PASS_LEN_MAX || strlen(pass) > SERVERS_USER_PASS_LEN_MAX) {
|
2016-10-17 23:53:43 +01:00
|
|
|
mp_raise_ValueError(mpexception_value_invalid_arguments);
|
2015-10-19 10:41:29 +01:00
|
|
|
}
|
2015-04-26 13:34:01 +01:00
|
|
|
memcpy(servers_user, user, SERVERS_USER_PASS_LEN_MAX);
|
|
|
|
memcpy(servers_pass, pass, SERVERS_USER_PASS_LEN_MAX);
|
2015-02-06 14:35:48 +00:00
|
|
|
}
|
|
|
|
|
2015-10-19 10:41:29 +01:00
|
|
|
void servers_set_timeout (uint32_t timeout) {
|
2015-06-03 16:28:03 +01:00
|
|
|
if (timeout < SERVERS_MIN_TIMEOUT_MS) {
|
2015-10-19 10:41:29 +01:00
|
|
|
// timeout is too low
|
2016-10-17 23:53:43 +01:00
|
|
|
mp_raise_ValueError(mpexception_value_invalid_arguments);
|
2015-06-03 16:28:03 +01:00
|
|
|
}
|
|
|
|
servers_data.timeout = timeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t servers_get_timeout (void) {
|
|
|
|
return servers_data.timeout;
|
|
|
|
}
|
|
|
|
|
2015-02-06 14:35:48 +00:00
|
|
|
/******************************************************************************
|
|
|
|
DEFINE PRIVATE FUNCTIONS
|
|
|
|
******************************************************************************/
|