modnetwork.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 "modnetwork.h"
  32. #if MICROPY_PY_NETWORK
  33. /// \module network - network configuration
  34. ///
  35. /// This module provides network drivers and routing configuration.
  36. void mod_network_init(void) {
  37. mp_obj_list_init(&MP_STATE_PORT(mod_network_nic_list), 0);
  38. }
  39. void mod_network_register_nic(mp_obj_t nic) {
  40. for (mp_uint_t i = 0; i < MP_STATE_PORT(mod_network_nic_list).len; i++) {
  41. if (MP_STATE_PORT(mod_network_nic_list).items[i] == nic) {
  42. // nic already registered
  43. return;
  44. }
  45. }
  46. // nic not registered so add to list
  47. mp_obj_list_append(&MP_STATE_PORT(mod_network_nic_list), nic);
  48. }
  49. mp_obj_t mod_network_find_nic(const uint8_t *ip) {
  50. // find a NIC that is suited to given IP address
  51. for (mp_uint_t i = 0; i < MP_STATE_PORT(mod_network_nic_list).len; i++) {
  52. mp_obj_t nic = MP_STATE_PORT(mod_network_nic_list).items[i];
  53. // TODO check IP suitability here
  54. //mod_network_nic_type_t *nic_type = (mod_network_nic_type_t*)mp_obj_get_type(nic);
  55. return nic;
  56. }
  57. nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "no available NIC"));
  58. }
  59. STATIC mp_obj_t network_route(void) {
  60. return &MP_STATE_PORT(mod_network_nic_list);
  61. }
  62. STATIC MP_DEFINE_CONST_FUN_OBJ_0(network_route_obj, network_route);
  63. STATIC const mp_rom_map_elem_t mp_module_network_globals_table[] = {
  64. { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_network) },
  65. #if MICROPY_PY_WIZNET5K
  66. { MP_ROM_QSTR(MP_QSTR_WIZNET5K), MP_ROM_PTR(&mod_network_nic_type_wiznet5k) },
  67. #endif
  68. #if MICROPY_PY_CC3K
  69. { MP_ROM_QSTR(MP_QSTR_CC3K), MP_ROM_PTR(&mod_network_nic_type_cc3k) },
  70. #endif
  71. { MP_ROM_QSTR(MP_QSTR_route), MP_ROM_PTR(&network_route_obj) },
  72. };
  73. STATIC MP_DEFINE_CONST_DICT(mp_module_network_globals, mp_module_network_globals_table);
  74. const mp_obj_module_t mp_module_network = {
  75. .base = { &mp_type_module },
  76. .globals = (mp_obj_dict_t*)&mp_module_network_globals,
  77. };
  78. #endif // MICROPY_PY_NETWORK