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