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) 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"
|
2015-02-06 14:35:48 +00:00
|
|
|
#include MICROPY_HAL_H
|
2015-02-21 18:58:43 +00:00
|
|
|
#include "py/misc.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-02-06 14:35:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
DECLARE PRIVATE DEFINITIONS
|
|
|
|
******************************************************************************/
|
|
|
|
|
2015-06-03 16:28:03 +01:00
|
|
|
#define SERVERS_DEF_USER "micro"
|
|
|
|
#define SERVERS_DEF_PASS "python"
|
|
|
|
#define SERVERS_DEF_TIMEOUT_MS 300000 // 5 minutes
|
|
|
|
#define SERVERS_MIN_TIMEOUT_MS 5000 // 5 seconds
|
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;
|
|
|
|
} servers_data_t;
|
2015-02-06 14:35:48 +00:00
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
DECLARE PRIVATE DATA
|
|
|
|
******************************************************************************/
|
2015-06-03 16:28:03 +01:00
|
|
|
static servers_data_t servers_data = {.timeout = SERVERS_DEF_TIMEOUT_MS, .enabled = false, .do_disable = false,
|
|
|
|
.do_enable = false, .do_reset = false};
|
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
|
|
|
|
******************************************************************************/
|
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();
|
|
|
|
|
|
|
|
for ( ;; ) {
|
|
|
|
|
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;
|
2015-04-27 20:11:03 +01:00
|
|
|
}
|
2015-04-29 13:25:44 +01: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;
|
|
|
|
}
|
2015-06-03 16:28:03 +01:00
|
|
|
else if (servers_data.do_reset && servers_data.enabled) {
|
2015-05-23 18:56:22 +01:00
|
|
|
telnet_reset();
|
|
|
|
ftp_reset();
|
|
|
|
servers_data.do_reset = false;
|
|
|
|
// resetting the servers is needed to preven half-open sockets
|
|
|
|
// and we should also close all user sockets
|
|
|
|
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();
|
|
|
|
}
|
2015-04-27 20:11:03 +01: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) {
|
|
|
|
sleep_sockets = false;
|
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);
|
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;
|
|
|
|
HAL_Delay(SERVERS_CYCLE_TIME_MS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-04-27 20:11:03 +01:00
|
|
|
HAL_Delay (SERVERS_CYCLE_TIME_MS * 5);
|
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 {
|
|
|
|
HAL_Delay (SERVERS_CYCLE_TIME_MS);
|
|
|
|
} while (servers_are_enabled());
|
2015-04-27 20:11:03 +01:00
|
|
|
HAL_Delay (SERVERS_CYCLE_TIME_MS * 5);
|
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;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
HAL_Delay (SERVERS_CYCLE_TIME_MS + 1);
|
|
|
|
}
|
|
|
|
|
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-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-06-03 16:28:03 +01:00
|
|
|
bool servers_set_timeout (uint32_t timeout) {
|
|
|
|
if (timeout < SERVERS_MIN_TIMEOUT_MS) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
servers_data.timeout = timeout;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t servers_get_timeout (void) {
|
|
|
|
return servers_data.timeout;
|
|
|
|
}
|
|
|
|
|
2015-02-06 14:35:48 +00:00
|
|
|
/******************************************************************************
|
|
|
|
DEFINE PRIVATE FUNCTIONS
|
|
|
|
******************************************************************************/
|