modnetwork.h 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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_STMHAL_MODNETWORK_H
  27. #define MICROPY_INCLUDED_STMHAL_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. struct _mod_network_socket_obj_t;
  35. typedef struct _mod_network_nic_type_t {
  36. mp_obj_type_t base;
  37. // API for non-socket operations
  38. int (*gethostbyname)(mp_obj_t nic, const char *name, mp_uint_t len, uint8_t *ip_out);
  39. // API for socket operations; return -1 on error
  40. int (*socket)(struct _mod_network_socket_obj_t *socket, int *_errno);
  41. void (*close)(struct _mod_network_socket_obj_t *socket);
  42. int (*bind)(struct _mod_network_socket_obj_t *socket, byte *ip, mp_uint_t port, int *_errno);
  43. int (*listen)(struct _mod_network_socket_obj_t *socket, mp_int_t backlog, int *_errno);
  44. int (*accept)(struct _mod_network_socket_obj_t *socket, struct _mod_network_socket_obj_t *socket2, byte *ip, mp_uint_t *port, int *_errno);
  45. int (*connect)(struct _mod_network_socket_obj_t *socket, byte *ip, mp_uint_t port, int *_errno);
  46. mp_uint_t (*send)(struct _mod_network_socket_obj_t *socket, const byte *buf, mp_uint_t len, int *_errno);
  47. mp_uint_t (*recv)(struct _mod_network_socket_obj_t *socket, byte *buf, mp_uint_t len, int *_errno);
  48. 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);
  49. 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);
  50. 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);
  51. int (*settimeout)(struct _mod_network_socket_obj_t *socket, mp_uint_t timeout_ms, int *_errno);
  52. int (*ioctl)(struct _mod_network_socket_obj_t *socket, mp_uint_t request, mp_uint_t arg, int *_errno);
  53. } mod_network_nic_type_t;
  54. typedef struct _mod_network_socket_obj_t {
  55. mp_obj_base_t base;
  56. mp_obj_t nic;
  57. mod_network_nic_type_t *nic_type;
  58. union {
  59. struct {
  60. uint8_t domain;
  61. uint8_t type;
  62. int8_t fileno;
  63. } u_param;
  64. mp_uint_t u_state;
  65. };
  66. } mod_network_socket_obj_t;
  67. extern const mod_network_nic_type_t mod_network_nic_type_wiznet5k;
  68. extern const mod_network_nic_type_t mod_network_nic_type_cc3k;
  69. void mod_network_init(void);
  70. void mod_network_register_nic(mp_obj_t nic);
  71. mp_obj_t mod_network_find_nic(const uint8_t *ip);
  72. #endif // MICROPY_INCLUDED_STMHAL_MODNETWORK_H