blob: 6b5899e1860e95ad44e0bfab2c4d8430dc170000 [file] [log] [blame]
danicampora87856452015-02-06 15:35:48 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2015 Daniel Campora
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
27#include <stdint.h>
Matt Anderson04588332015-04-26 08:34:01 -040028#include <string.h>
danicampora87856452015-02-06 15:35:48 +010029
Damien George4a23a012015-02-21 18:58:43 +000030#include "py/mpconfig.h"
Damien George4a23a012015-02-21 18:58:43 +000031#include "py/misc.h"
Damien George50ddaaf2016-10-18 09:53:43 +110032#include "py/runtime.h"
Damien George731f3592015-10-30 23:03:58 +000033#include "py/mphal.h"
danicampora87856452015-02-06 15:35:48 +010034#include "serverstask.h"
Matt Anderson04588332015-04-26 08:34:01 -040035#include "simplelink.h"
danicampora87856452015-02-06 15:35:48 +010036#include "debug.h"
danicampora87856452015-02-06 15:35:48 +010037#include "telnet.h"
38#include "ftp.h"
danicampora11aa6ba2015-02-25 11:08:51 +010039#include "pybwdt.h"
Daniel Camporaed56b0b2015-05-22 19:53:33 +020040#include "modusocket.h"
danicampora2e0cd202015-10-19 11:41:29 +020041#include "mpexception.h"
danicampora495e7cf2016-02-21 21:28:19 +010042#include "modnetwork.h"
43#include "modwlan.h"
danicampora87856452015-02-06 15:35:48 +010044
45/******************************************************************************
danicampora87856452015-02-06 15:35:48 +010046 DEFINE PRIVATE TYPES
47 ******************************************************************************/
48typedef struct {
Daniel Camporacc204822015-06-03 17:28:03 +020049 uint32_t timeout;
50 bool enabled;
51 bool do_disable;
52 bool do_enable;
53 bool do_reset;
danicampora495e7cf2016-02-21 21:28:19 +010054 bool do_wlan_cycle_power;
Daniel Camporacc204822015-06-03 17:28:03 +020055} servers_data_t;
danicampora87856452015-02-06 15:35:48 +010056
57/******************************************************************************
58 DECLARE PRIVATE DATA
59 ******************************************************************************/
danicampora495e7cf2016-02-21 21:28:19 +010060static servers_data_t servers_data = {.timeout = SERVERS_DEF_TIMEOUT_MS};
Daniel Camporaed56b0b2015-05-22 19:53:33 +020061static volatile bool sleep_sockets = false;
danicampora87856452015-02-06 15:35:48 +010062
63/******************************************************************************
64 DECLARE PRIVATE FUNCTIONS
65 ******************************************************************************/
66
67/******************************************************************************
68 DECLARE PUBLIC DATA
69 ******************************************************************************/
Damien George469c6232016-06-05 13:01:16 +010070
71// This is the static memory (TCB and stack) for the servers task
72StaticTask_t svTaskTCB __attribute__ ((section (".rtos_heap")));
73StackType_t svTaskStack[SERVERS_STACK_LEN] __attribute__ ((section (".rtos_heap"))) __attribute__((aligned (8)));
74
Matt Anderson04588332015-04-26 08:34:01 -040075char servers_user[SERVERS_USER_PASS_LEN_MAX + 1];
76char servers_pass[SERVERS_USER_PASS_LEN_MAX + 1];
danicampora87856452015-02-06 15:35:48 +010077
78/******************************************************************************
79 DECLARE PUBLIC FUNCTIONS
80 ******************************************************************************/
danicampora87856452015-02-06 15:35:48 +010081void TASK_Servers (void *pvParameters) {
82
83 bool cycle = false;
84
danicampora87856452015-02-06 15:35:48 +010085 strcpy (servers_user, SERVERS_DEF_USER);
86 strcpy (servers_pass, SERVERS_DEF_PASS);
87
88 telnet_init();
89 ftp_init();
90
91 for ( ;; ) {
92
Daniel Campora9fbc2652015-04-27 21:11:03 +020093 if (servers_data.do_enable) {
danicampora77791b52015-03-20 13:52:33 +010094 // enable network services
danicampora87856452015-02-06 15:35:48 +010095 telnet_enable();
96 ftp_enable();
danicampora77791b52015-03-20 13:52:33 +010097 // now set/clear the flags
danicampora87856452015-02-06 15:35:48 +010098 servers_data.enabled = true;
danicampora77791b52015-03-20 13:52:33 +010099 servers_data.do_enable = false;
Daniel Campora9fbc2652015-04-27 21:11:03 +0200100 }
Daniel Camporabf4576d2015-04-29 14:25:44 +0200101 else if (servers_data.do_disable) {
Daniel Campora9fbc2652015-04-27 21:11:03 +0200102 // disable network services
103 telnet_disable();
104 ftp_disable();
105 // now clear the flags
106 servers_data.do_disable = false;
107 servers_data.enabled = false;
Daniel Campora9fbc2652015-04-27 21:11:03 +0200108 }
Daniel Camporacf814b22015-07-10 11:32:53 +0200109 else if (servers_data.do_reset) {
110 // resetting the servers is needed to prevent half-open sockets
Daniel Campora9f8c5452015-05-23 19:56:22 +0200111 servers_data.do_reset = false;
Daniel Camporacf814b22015-07-10 11:32:53 +0200112 if (servers_data.enabled) {
113 telnet_reset();
114 ftp_reset();
115 }
116 // and we should also close all user sockets. We do it here
117 // for convinience and to save on code size.
Daniel Campora9f8c5452015-05-23 19:56:22 +0200118 modusocket_close_all_user_sockets();
Daniel Campora9fbc2652015-04-27 21:11:03 +0200119 }
Daniel Camporacc204822015-06-03 17:28:03 +0200120
121 if (cycle) {
122 telnet_run();
123 }
Daniel Campora9fbc2652015-04-27 21:11:03 +0200124 else {
Daniel Camporacc204822015-06-03 17:28:03 +0200125 ftp_run();
danicampora87856452015-02-06 15:35:48 +0100126 }
127
Daniel Camporaed56b0b2015-05-22 19:53:33 +0200128 if (sleep_sockets) {
Daniel Campora65453362015-05-25 18:54:43 +0200129 pybwdt_srv_sleeping(true);
Daniel Camporaed56b0b2015-05-22 19:53:33 +0200130 modusocket_enter_sleep();
Daniel Campora65453362015-05-25 18:54:43 +0200131 pybwdt_srv_sleeping(false);
danicampora495e7cf2016-02-21 21:28:19 +0100132 mp_hal_delay_ms(SERVERS_CYCLE_TIME_MS * 2);
133 if (servers_data.do_wlan_cycle_power) {
134 servers_data.do_wlan_cycle_power = false;
135 wlan_off_on();
136 }
137 sleep_sockets = false;
138
Daniel Camporaed56b0b2015-05-22 19:53:33 +0200139 }
140
Daniel Campora65453362015-05-25 18:54:43 +0200141 // set the alive flag for the wdt
142 pybwdt_srv_alive();
143
danicampora77791b52015-03-20 13:52:33 +0100144 // move to the next cycle
danicampora87856452015-02-06 15:35:48 +0100145 cycle = cycle ? false : true;
Paul Sokolovskyf4decdc2015-10-29 20:38:44 +0300146 mp_hal_delay_ms(SERVERS_CYCLE_TIME_MS);
danicampora87856452015-02-06 15:35:48 +0100147 }
148}
149
danicamporaf8ee88b2015-03-19 17:06:20 +0100150void servers_start (void) {
danicampora87856452015-02-06 15:35:48 +0100151 servers_data.do_enable = true;
Paul Sokolovskyf4decdc2015-10-29 20:38:44 +0300152 mp_hal_delay_ms(SERVERS_CYCLE_TIME_MS * 3);
danicampora77791b52015-03-20 13:52:33 +0100153}
154
danicamporaf8ee88b2015-03-19 17:06:20 +0100155void servers_stop (void) {
danicampora87856452015-02-06 15:35:48 +0100156 servers_data.do_disable = true;
danicamporaf8ee88b2015-03-19 17:06:20 +0100157 do {
Paul Sokolovskyf4decdc2015-10-29 20:38:44 +0300158 mp_hal_delay_ms(SERVERS_CYCLE_TIME_MS);
danicamporaf8ee88b2015-03-19 17:06:20 +0100159 } while (servers_are_enabled());
Paul Sokolovskyf4decdc2015-10-29 20:38:44 +0300160 mp_hal_delay_ms(SERVERS_CYCLE_TIME_MS * 3);
danicampora87856452015-02-06 15:35:48 +0100161}
162
Daniel Campora9f8c5452015-05-23 19:56:22 +0200163void servers_reset (void) {
164 servers_data.do_reset = true;
165}
166
danicampora495e7cf2016-02-21 21:28:19 +0100167void servers_wlan_cycle_power (void) {
168 servers_data.do_wlan_cycle_power = true;
169}
170
danicampora87856452015-02-06 15:35:48 +0100171bool servers_are_enabled (void) {
172 return servers_data.enabled;
173}
174
Daniel Camporaed56b0b2015-05-22 19:53:33 +0200175void server_sleep_sockets (void) {
176 sleep_sockets = true;
Paul Sokolovskyf4decdc2015-10-29 20:38:44 +0300177 mp_hal_delay_ms(SERVERS_CYCLE_TIME_MS + 1);
Daniel Camporaed56b0b2015-05-22 19:53:33 +0200178}
179
danicamporaf8ee88b2015-03-19 17:06:20 +0100180void servers_close_socket (int16_t *sd) {
danicampora87856452015-02-06 15:35:48 +0100181 if (*sd > 0) {
Daniel Camporaed56b0b2015-05-22 19:53:33 +0200182 modusocket_socket_delete(*sd);
danicampora87856452015-02-06 15:35:48 +0100183 sl_Close(*sd);
184 *sd = -1;
185 }
186}
187
danicamporaf8ee88b2015-03-19 17:06:20 +0100188void servers_set_login (char *user, char *pass) {
danicampora2e0cd202015-10-19 11:41:29 +0200189 if (strlen(user) > SERVERS_USER_PASS_LEN_MAX || strlen(pass) > SERVERS_USER_PASS_LEN_MAX) {
Damien George50ddaaf2016-10-18 09:53:43 +1100190 mp_raise_ValueError(mpexception_value_invalid_arguments);
danicampora2e0cd202015-10-19 11:41:29 +0200191 }
Matt Anderson04588332015-04-26 08:34:01 -0400192 memcpy(servers_user, user, SERVERS_USER_PASS_LEN_MAX);
193 memcpy(servers_pass, pass, SERVERS_USER_PASS_LEN_MAX);
danicampora87856452015-02-06 15:35:48 +0100194}
195
danicampora2e0cd202015-10-19 11:41:29 +0200196void servers_set_timeout (uint32_t timeout) {
Daniel Camporacc204822015-06-03 17:28:03 +0200197 if (timeout < SERVERS_MIN_TIMEOUT_MS) {
danicampora2e0cd202015-10-19 11:41:29 +0200198 // timeout is too low
Damien George50ddaaf2016-10-18 09:53:43 +1100199 mp_raise_ValueError(mpexception_value_invalid_arguments);
Daniel Camporacc204822015-06-03 17:28:03 +0200200 }
201 servers_data.timeout = timeout;
Daniel Camporacc204822015-06-03 17:28:03 +0200202}
203
204uint32_t servers_get_timeout (void) {
205 return servers_data.timeout;
206}
207
danicampora87856452015-02-06 15:35:48 +0100208/******************************************************************************
209 DEFINE PRIVATE FUNCTIONS
210 ******************************************************************************/