modnetwork.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. *
  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. #ifndef MICROPY_INCLUDED_STM32_MODNETWORK_H
  27. #define MICROPY_INCLUDED_STM32_MODNETWORK_H
  28. #define MOD_NETWORK_IPADDR_BUF_SIZE (4)
  29. #define MOD_NETWORK_AF_INET (2)
  30. #define MOD_NETWORK_AF_INET6 (10)
  31. #define MOD_NETWORK_SOCK_STREAM (1)
  32. #define MOD_NETWORK_SOCK_DGRAM (2)
  33. #define MOD_NETWORK_SOCK_RAW (3)
  34. #if MICROPY_PY_LWIP
  35. struct netif;
  36. typedef struct _mod_network_nic_type_t {
  37. mp_obj_base_t base;
  38. void (*poll_callback)(void *data, struct netif *netif);
  39. } mod_network_nic_type_t;
  40. extern const mp_obj_type_t mod_network_nic_type_wiznet5k;
  41. mp_obj_t mod_network_nic_ifconfig(struct netif *netif, size_t n_args, const mp_obj_t *args);
  42. #else
  43. struct _mod_network_socket_obj_t;
  44. typedef struct _mod_network_nic_type_t {
  45. mp_obj_type_t base;
  46. // API for non-socket operations
  47. int (*gethostbyname)(mp_obj_t nic, const char *name, mp_uint_t len, uint8_t *ip_out);
  48. // API for socket operations; return -1 on error
  49. int (*socket)(struct _mod_network_socket_obj_t *socket, int *_errno);
  50. void (*close)(struct _mod_network_socket_obj_t *socket);
  51. int (*bind)(struct _mod_network_socket_obj_t *socket, byte *ip, mp_uint_t port, int *_errno);
  52. int (*listen)(struct _mod_network_socket_obj_t *socket, mp_int_t backlog, int *_errno);
  53. int (*accept)(struct _mod_network_socket_obj_t *socket, struct _mod_network_socket_obj_t *socket2, byte *ip, mp_uint_t *port, int *_errno);
  54. int (*connect)(struct _mod_network_socket_obj_t *socket, byte *ip, mp_uint_t port, int *_errno);
  55. mp_uint_t (*send)(struct _mod_network_socket_obj_t *socket, const byte *buf, mp_uint_t len, int *_errno);
  56. mp_uint_t (*recv)(struct _mod_network_socket_obj_t *socket, byte *buf, mp_uint_t len, int *_errno);
  57. mp_uint_t (*sendto)(struct _mod_network_socket_obj_t *socket, const byte *buf, mp_uint_t len, byte *ip, mp_uint_t port, int *_errno);
  58. mp_uint_t (*recvfrom)(struct _mod_network_socket_obj_t *socket, byte *buf, mp_uint_t len, byte *ip, mp_uint_t *port, int *_errno);
  59. int (*setsockopt)(struct _mod_network_socket_obj_t *socket, mp_uint_t level, mp_uint_t opt, const void *optval, mp_uint_t optlen, int *_errno);
  60. int (*settimeout)(struct _mod_network_socket_obj_t *socket, mp_uint_t timeout_ms, int *_errno);
  61. int (*ioctl)(struct _mod_network_socket_obj_t *socket, mp_uint_t request, mp_uint_t arg, int *_errno);
  62. } mod_network_nic_type_t;
  63. typedef struct _mod_network_socket_obj_t {
  64. mp_obj_base_t base;
  65. mp_obj_t nic;
  66. mod_network_nic_type_t *nic_type;
  67. union {
  68. struct {
  69. uint8_t domain;
  70. uint8_t type;
  71. int8_t fileno;
  72. } u_param;
  73. mp_uint_t u_state;
  74. };
  75. } mod_network_socket_obj_t;
  76. extern const mod_network_nic_type_t mod_network_nic_type_wiznet5k;
  77. extern const mod_network_nic_type_t mod_network_nic_type_cc3k;
  78. #endif
  79. void mod_network_init(void);
  80. void mod_network_deinit(void);
  81. void mod_network_register_nic(mp_obj_t nic);
  82. mp_obj_t mod_network_find_nic(const uint8_t *ip);
  83. #endif // MICROPY_INCLUDED_STM32_MODNETWORK_H