modnetwork.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013, 2014 Damien P. George
  7. * Copyright (c) 2015 Daniel Campora
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. */
  27. #include "py/runtime.h"
  28. #include "py/mperrno.h"
  29. #include "py/mphal.h"
  30. #include "modnetwork.h"
  31. #include "mpexception.h"
  32. #include "serverstask.h"
  33. #include "simplelink.h"
  34. /******************************************************************************
  35. DEFINE TYPES
  36. ******************************************************************************/
  37. typedef struct {
  38. mp_obj_base_t base;
  39. } network_server_obj_t;
  40. /******************************************************************************
  41. DECLARE PRIVATE DATA
  42. ******************************************************************************/
  43. STATIC network_server_obj_t network_server_obj;
  44. STATIC const mp_obj_type_t network_server_type;
  45. /// \module network - network configuration
  46. ///
  47. /// This module provides network drivers and server configuration.
  48. void mod_network_init0(void) {
  49. }
  50. #if (MICROPY_PORT_HAS_TELNET || MICROPY_PORT_HAS_FTP)
  51. STATIC mp_obj_t network_server_init_helper(mp_obj_t self, const mp_arg_val_t *args) {
  52. const char *user = SERVERS_DEF_USER;
  53. const char *pass = SERVERS_DEF_PASS;
  54. if (args[0].u_obj != MP_OBJ_NULL) {
  55. mp_obj_t *login;
  56. mp_obj_get_array_fixed_n(args[0].u_obj, 2, &login);
  57. user = mp_obj_str_get_str(login[0]);
  58. pass = mp_obj_str_get_str(login[1]);
  59. }
  60. uint32_t timeout = SERVERS_DEF_TIMEOUT_MS / 1000;
  61. if (args[1].u_obj != MP_OBJ_NULL) {
  62. timeout = mp_obj_get_int(args[1].u_obj);
  63. }
  64. // configure the new login
  65. servers_set_login ((char *)user, (char *)pass);
  66. // configure the timeout
  67. servers_set_timeout(timeout * 1000);
  68. // start the servers
  69. servers_start();
  70. return mp_const_none;
  71. }
  72. STATIC const mp_arg_t network_server_args[] = {
  73. { MP_QSTR_id, MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
  74. { MP_QSTR_login, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
  75. { MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
  76. };
  77. STATIC mp_obj_t network_server_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
  78. // parse args
  79. mp_map_t kw_args;
  80. mp_map_init_fixed_table(&kw_args, n_kw, all_args + n_args);
  81. mp_arg_val_t args[MP_ARRAY_SIZE(network_server_args)];
  82. mp_arg_parse_all(n_args, all_args, &kw_args, MP_ARRAY_SIZE(args), network_server_args, args);
  83. // check the server id
  84. if (args[0].u_obj != MP_OBJ_NULL) {
  85. if (mp_obj_get_int(args[0].u_obj) != 0) {
  86. mp_raise_OSError(MP_ENODEV);
  87. }
  88. }
  89. // setup the object and initialize it
  90. network_server_obj_t *self = &network_server_obj;
  91. self->base.type = &network_server_type;
  92. network_server_init_helper(self, &args[1]);
  93. return (mp_obj_t)self;
  94. }
  95. STATIC mp_obj_t network_server_init(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
  96. // parse args
  97. mp_arg_val_t args[MP_ARRAY_SIZE(network_server_args) - 1];
  98. mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(args), &network_server_args[1], args);
  99. return network_server_init_helper(pos_args[0], args);
  100. }
  101. STATIC MP_DEFINE_CONST_FUN_OBJ_KW(network_server_init_obj, 1, network_server_init);
  102. // timeout value given in seconds
  103. STATIC mp_obj_t network_server_timeout(size_t n_args, const mp_obj_t *args) {
  104. if (n_args > 1) {
  105. uint32_t timeout = mp_obj_get_int(args[1]);
  106. servers_set_timeout(timeout * 1000);
  107. return mp_const_none;
  108. } else {
  109. // get
  110. return mp_obj_new_int(servers_get_timeout() / 1000);
  111. }
  112. }
  113. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(network_server_timeout_obj, 1, 2, network_server_timeout);
  114. STATIC mp_obj_t network_server_running(mp_obj_t self_in) {
  115. // get
  116. return mp_obj_new_bool(servers_are_enabled());
  117. }
  118. STATIC MP_DEFINE_CONST_FUN_OBJ_1(network_server_running_obj, network_server_running);
  119. STATIC mp_obj_t network_server_deinit(mp_obj_t self_in) {
  120. // simply stop the servers
  121. servers_stop();
  122. return mp_const_none;
  123. }
  124. STATIC MP_DEFINE_CONST_FUN_OBJ_1(network_server_deinit_obj, network_server_deinit);
  125. #endif
  126. STATIC const mp_rom_map_elem_t mp_module_network_globals_table[] = {
  127. { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_network) },
  128. { MP_ROM_QSTR(MP_QSTR_WLAN), MP_ROM_PTR(&mod_network_nic_type_wlan) },
  129. #if (MICROPY_PORT_HAS_TELNET || MICROPY_PORT_HAS_FTP)
  130. { MP_ROM_QSTR(MP_QSTR_Server), MP_ROM_PTR(&network_server_type) },
  131. #endif
  132. };
  133. STATIC MP_DEFINE_CONST_DICT(mp_module_network_globals, mp_module_network_globals_table);
  134. const mp_obj_module_t mp_module_network = {
  135. .base = { &mp_type_module },
  136. .globals = (mp_obj_dict_t*)&mp_module_network_globals,
  137. };
  138. #if (MICROPY_PORT_HAS_TELNET || MICROPY_PORT_HAS_FTP)
  139. STATIC const mp_rom_map_elem_t network_server_locals_dict_table[] = {
  140. { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&network_server_init_obj) },
  141. { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&network_server_deinit_obj) },
  142. { MP_ROM_QSTR(MP_QSTR_timeout), MP_ROM_PTR(&network_server_timeout_obj) },
  143. { MP_ROM_QSTR(MP_QSTR_isrunning), MP_ROM_PTR(&network_server_running_obj) },
  144. };
  145. STATIC MP_DEFINE_CONST_DICT(network_server_locals_dict, network_server_locals_dict_table);
  146. STATIC const mp_obj_type_t network_server_type = {
  147. { &mp_type_type },
  148. .name = MP_QSTR_Server,
  149. .make_new = network_server_make_new,
  150. .locals_dict = (mp_obj_t)&network_server_locals_dict,
  151. };
  152. #endif