serverstask.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * This file is part of the MicroPython 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. #include <stdint.h>
  27. #include <string.h>
  28. #include "py/mpconfig.h"
  29. #include "py/misc.h"
  30. #include "py/runtime.h"
  31. #include "py/mphal.h"
  32. #include "serverstask.h"
  33. #include "simplelink.h"
  34. #include "debug.h"
  35. #include "telnet.h"
  36. #include "ftp.h"
  37. #include "pybwdt.h"
  38. #include "modusocket.h"
  39. #include "mpexception.h"
  40. #include "modnetwork.h"
  41. #include "modwlan.h"
  42. /******************************************************************************
  43. DEFINE PRIVATE TYPES
  44. ******************************************************************************/
  45. typedef struct {
  46. uint32_t timeout;
  47. bool enabled;
  48. bool do_disable;
  49. bool do_enable;
  50. bool do_reset;
  51. bool do_wlan_cycle_power;
  52. } servers_data_t;
  53. /******************************************************************************
  54. DECLARE PRIVATE DATA
  55. ******************************************************************************/
  56. static servers_data_t servers_data = {.timeout = SERVERS_DEF_TIMEOUT_MS};
  57. static volatile bool sleep_sockets = false;
  58. /******************************************************************************
  59. DECLARE PRIVATE FUNCTIONS
  60. ******************************************************************************/
  61. /******************************************************************************
  62. DECLARE PUBLIC DATA
  63. ******************************************************************************/
  64. // This is the static memory (TCB and stack) for the servers task
  65. StaticTask_t svTaskTCB __attribute__ ((section (".rtos_heap")));
  66. StackType_t svTaskStack[SERVERS_STACK_LEN] __attribute__ ((section (".rtos_heap"))) __attribute__((aligned (8)));
  67. char servers_user[SERVERS_USER_PASS_LEN_MAX + 1];
  68. char servers_pass[SERVERS_USER_PASS_LEN_MAX + 1];
  69. /******************************************************************************
  70. DECLARE PUBLIC FUNCTIONS
  71. ******************************************************************************/
  72. void TASK_Servers (void *pvParameters) {
  73. bool cycle = false;
  74. strcpy (servers_user, SERVERS_DEF_USER);
  75. strcpy (servers_pass, SERVERS_DEF_PASS);
  76. telnet_init();
  77. ftp_init();
  78. for ( ;; ) {
  79. if (servers_data.do_enable) {
  80. // enable network services
  81. telnet_enable();
  82. ftp_enable();
  83. // now set/clear the flags
  84. servers_data.enabled = true;
  85. servers_data.do_enable = false;
  86. }
  87. else if (servers_data.do_disable) {
  88. // disable network services
  89. telnet_disable();
  90. ftp_disable();
  91. // now clear the flags
  92. servers_data.do_disable = false;
  93. servers_data.enabled = false;
  94. }
  95. else if (servers_data.do_reset) {
  96. // resetting the servers is needed to prevent half-open sockets
  97. servers_data.do_reset = false;
  98. if (servers_data.enabled) {
  99. telnet_reset();
  100. ftp_reset();
  101. }
  102. // and we should also close all user sockets. We do it here
  103. // for convinience and to save on code size.
  104. modusocket_close_all_user_sockets();
  105. }
  106. if (cycle) {
  107. telnet_run();
  108. }
  109. else {
  110. ftp_run();
  111. }
  112. if (sleep_sockets) {
  113. pybwdt_srv_sleeping(true);
  114. modusocket_enter_sleep();
  115. pybwdt_srv_sleeping(false);
  116. mp_hal_delay_ms(SERVERS_CYCLE_TIME_MS * 2);
  117. if (servers_data.do_wlan_cycle_power) {
  118. servers_data.do_wlan_cycle_power = false;
  119. wlan_off_on();
  120. }
  121. sleep_sockets = false;
  122. }
  123. // set the alive flag for the wdt
  124. pybwdt_srv_alive();
  125. // move to the next cycle
  126. cycle = cycle ? false : true;
  127. mp_hal_delay_ms(SERVERS_CYCLE_TIME_MS);
  128. }
  129. }
  130. void servers_start (void) {
  131. servers_data.do_enable = true;
  132. mp_hal_delay_ms(SERVERS_CYCLE_TIME_MS * 3);
  133. }
  134. void servers_stop (void) {
  135. servers_data.do_disable = true;
  136. do {
  137. mp_hal_delay_ms(SERVERS_CYCLE_TIME_MS);
  138. } while (servers_are_enabled());
  139. mp_hal_delay_ms(SERVERS_CYCLE_TIME_MS * 3);
  140. }
  141. void servers_reset (void) {
  142. servers_data.do_reset = true;
  143. }
  144. void servers_wlan_cycle_power (void) {
  145. servers_data.do_wlan_cycle_power = true;
  146. }
  147. bool servers_are_enabled (void) {
  148. return servers_data.enabled;
  149. }
  150. void server_sleep_sockets (void) {
  151. sleep_sockets = true;
  152. mp_hal_delay_ms(SERVERS_CYCLE_TIME_MS + 1);
  153. }
  154. void servers_close_socket (int16_t *sd) {
  155. if (*sd > 0) {
  156. modusocket_socket_delete(*sd);
  157. sl_Close(*sd);
  158. *sd = -1;
  159. }
  160. }
  161. void servers_set_login (char *user, char *pass) {
  162. if (strlen(user) > SERVERS_USER_PASS_LEN_MAX || strlen(pass) > SERVERS_USER_PASS_LEN_MAX) {
  163. mp_raise_ValueError(mpexception_value_invalid_arguments);
  164. }
  165. memcpy(servers_user, user, SERVERS_USER_PASS_LEN_MAX);
  166. memcpy(servers_pass, pass, SERVERS_USER_PASS_LEN_MAX);
  167. }
  168. void servers_set_timeout (uint32_t timeout) {
  169. if (timeout < SERVERS_MIN_TIMEOUT_MS) {
  170. // timeout is too low
  171. mp_raise_ValueError(mpexception_value_invalid_arguments);
  172. }
  173. servers_data.timeout = timeout;
  174. }
  175. uint32_t servers_get_timeout (void) {
  176. return servers_data.timeout;
  177. }
  178. /******************************************************************************
  179. DEFINE PRIVATE FUNCTIONS
  180. ******************************************************************************/