modnetwork.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2014 Damien P. George
  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 <stdio.h>
  27. #include <stdint.h>
  28. #include <string.h>
  29. #include "py/objlist.h"
  30. #include "py/runtime.h"
  31. #include "py/mphal.h"
  32. #include "lib/netutils/netutils.h"
  33. #include "modnetwork.h"
  34. #if MICROPY_PY_NETWORK
  35. #if MICROPY_PY_LWIP
  36. #include "lwip/netif.h"
  37. #include "lwip/timeouts.h"
  38. #include "lwip/dns.h"
  39. #include "lwip/dhcp.h"
  40. u32_t sys_now(void) {
  41. return mp_hal_ticks_ms();
  42. }
  43. void pyb_lwip_poll(void) {
  44. // Poll all the NICs for incoming data
  45. for (struct netif *netif = netif_list; netif != NULL; netif = netif->next) {
  46. if (netif->flags & NETIF_FLAG_LINK_UP) {
  47. mod_network_nic_type_t *nic = netif->state;
  48. nic->poll_callback(nic, netif);
  49. }
  50. }
  51. // Run the lwIP internal updates
  52. sys_check_timeouts();
  53. }
  54. #endif
  55. /// \module network - network configuration
  56. ///
  57. /// This module provides network drivers and routing configuration.
  58. void mod_network_init(void) {
  59. mp_obj_list_init(&MP_STATE_PORT(mod_network_nic_list), 0);
  60. }
  61. void mod_network_deinit(void) {
  62. }
  63. void mod_network_register_nic(mp_obj_t nic) {
  64. for (mp_uint_t i = 0; i < MP_STATE_PORT(mod_network_nic_list).len; i++) {
  65. if (MP_STATE_PORT(mod_network_nic_list).items[i] == nic) {
  66. // nic already registered
  67. return;
  68. }
  69. }
  70. // nic not registered so add to list
  71. mp_obj_list_append(MP_OBJ_FROM_PTR(&MP_STATE_PORT(mod_network_nic_list)), nic);
  72. }
  73. mp_obj_t mod_network_find_nic(const uint8_t *ip) {
  74. // find a NIC that is suited to given IP address
  75. for (mp_uint_t i = 0; i < MP_STATE_PORT(mod_network_nic_list).len; i++) {
  76. mp_obj_t nic = MP_STATE_PORT(mod_network_nic_list).items[i];
  77. // TODO check IP suitability here
  78. //mod_network_nic_type_t *nic_type = (mod_network_nic_type_t*)mp_obj_get_type(nic);
  79. return nic;
  80. }
  81. nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "no available NIC"));
  82. }
  83. STATIC mp_obj_t network_route(void) {
  84. return MP_OBJ_FROM_PTR(&MP_STATE_PORT(mod_network_nic_list));
  85. }
  86. STATIC MP_DEFINE_CONST_FUN_OBJ_0(network_route_obj, network_route);
  87. STATIC const mp_rom_map_elem_t mp_module_network_globals_table[] = {
  88. { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_network) },
  89. #if MICROPY_PY_WIZNET5K
  90. { MP_ROM_QSTR(MP_QSTR_WIZNET5K), MP_ROM_PTR(&mod_network_nic_type_wiznet5k) },
  91. #endif
  92. #if MICROPY_PY_CC3K
  93. { MP_ROM_QSTR(MP_QSTR_CC3K), MP_ROM_PTR(&mod_network_nic_type_cc3k) },
  94. #endif
  95. { MP_ROM_QSTR(MP_QSTR_route), MP_ROM_PTR(&network_route_obj) },
  96. };
  97. STATIC MP_DEFINE_CONST_DICT(mp_module_network_globals, mp_module_network_globals_table);
  98. const mp_obj_module_t mp_module_network = {
  99. .base = { &mp_type_module },
  100. .globals = (mp_obj_dict_t*)&mp_module_network_globals,
  101. };
  102. /*******************************************************************************/
  103. // Implementations of network methods that can be used by any interface
  104. #if MICROPY_PY_LWIP
  105. mp_obj_t mod_network_nic_ifconfig(struct netif *netif, size_t n_args, const mp_obj_t *args) {
  106. if (n_args == 0) {
  107. // Get IP addresses
  108. const ip_addr_t *dns = dns_getserver(0);
  109. mp_obj_t tuple[4] = {
  110. netutils_format_ipv4_addr((uint8_t*)&netif->ip_addr, NETUTILS_BIG),
  111. netutils_format_ipv4_addr((uint8_t*)&netif->netmask, NETUTILS_BIG),
  112. netutils_format_ipv4_addr((uint8_t*)&netif->gw, NETUTILS_BIG),
  113. netutils_format_ipv4_addr((uint8_t*)dns, NETUTILS_BIG),
  114. };
  115. return mp_obj_new_tuple(4, tuple);
  116. } else if (args[0] == MP_OBJ_NEW_QSTR(MP_QSTR_dhcp)) {
  117. // Start the DHCP client
  118. if (dhcp_supplied_address(netif)) {
  119. dhcp_renew(netif);
  120. } else {
  121. dhcp_stop(netif);
  122. dhcp_start(netif);
  123. }
  124. // Wait for DHCP to get IP address
  125. uint32_t start = mp_hal_ticks_ms();
  126. while (!dhcp_supplied_address(netif)) {
  127. if (mp_hal_ticks_ms() - start > 10000) {
  128. mp_raise_msg(&mp_type_OSError, "timeout waiting for DHCP to get IP address");
  129. }
  130. mp_hal_delay_ms(100);
  131. }
  132. return mp_const_none;
  133. } else {
  134. // Release and stop any existing DHCP
  135. dhcp_release(netif);
  136. dhcp_stop(netif);
  137. // Set static IP addresses
  138. mp_obj_t *items;
  139. mp_obj_get_array_fixed_n(args[0], 4, &items);
  140. netutils_parse_ipv4_addr(items[0], (uint8_t*)&netif->ip_addr, NETUTILS_BIG);
  141. netutils_parse_ipv4_addr(items[1], (uint8_t*)&netif->netmask, NETUTILS_BIG);
  142. netutils_parse_ipv4_addr(items[2], (uint8_t*)&netif->gw, NETUTILS_BIG);
  143. ip_addr_t dns;
  144. netutils_parse_ipv4_addr(items[3], (uint8_t*)&dns, NETUTILS_BIG);
  145. dns_setserver(0, &dns);
  146. return mp_const_none;
  147. }
  148. }
  149. #endif
  150. #endif // MICROPY_PY_NETWORK