|
| 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_RP2_MODNETWORK_H |
| 27 | +#define MICROPY_INCLUDED_RP2_MODNETWORK_H |
| 28 | + |
| 29 | +#define MOD_NETWORK_IPADDR_BUF_SIZE (4) |
| 30 | + |
| 31 | +#define MOD_NETWORK_AF_INET (2) |
| 32 | +#define MOD_NETWORK_AF_INET6 (10) |
| 33 | + |
| 34 | +#define MOD_NETWORK_SOCK_STREAM (1) |
| 35 | +#define MOD_NETWORK_SOCK_DGRAM (2) |
| 36 | +#define MOD_NETWORK_SOCK_RAW (3) |
| 37 | + |
| 38 | +#define MOD_NETWORK_STA_IF (0) |
| 39 | +#define MOD_NETWORK_AP_IF (1) |
| 40 | + |
| 41 | +struct _mod_network_socket_obj_t; |
| 42 | + |
| 43 | +typedef struct _mod_network_nic_type_t { |
| 44 | + mp_obj_type_t base; |
| 45 | + |
| 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 | + |
| 49 | + // API for socket operations; return -1 on error |
| 50 | + int (*socket)(struct _mod_network_socket_obj_t *socket, int *_errno); |
| 51 | + void (*close)(struct _mod_network_socket_obj_t *socket); |
| 52 | + int (*bind)(struct _mod_network_socket_obj_t *socket, byte *ip, mp_uint_t port, int *_errno); |
| 53 | + int (*listen)(struct _mod_network_socket_obj_t *socket, mp_int_t backlog, int *_errno); |
| 54 | + int (*accept)(struct _mod_network_socket_obj_t *socket, struct _mod_network_socket_obj_t *socket2, byte *ip, mp_uint_t *port, int *_errno); |
| 55 | + int (*connect)(struct _mod_network_socket_obj_t *socket, byte *ip, mp_uint_t port, int *_errno); |
| 56 | + mp_uint_t (*send)(struct _mod_network_socket_obj_t *socket, const byte *buf, mp_uint_t len, int *_errno); |
| 57 | + mp_uint_t (*recv)(struct _mod_network_socket_obj_t *socket, byte *buf, mp_uint_t len, int *_errno); |
| 58 | + 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); |
| 59 | + 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); |
| 60 | + 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); |
| 61 | + int (*settimeout)(struct _mod_network_socket_obj_t *socket, mp_uint_t timeout_ms, int *_errno); |
| 62 | + int (*ioctl)(struct _mod_network_socket_obj_t *socket, mp_uint_t request, mp_uint_t arg, int *_errno); |
| 63 | +} mod_network_nic_type_t; |
| 64 | + |
| 65 | +typedef struct _mod_network_socket_obj_t { |
| 66 | + mp_obj_base_t base; |
| 67 | + mp_obj_t nic; |
| 68 | + mod_network_nic_type_t *nic_type; |
| 69 | + int16_t fd; |
| 70 | + uint16_t timeout; |
| 71 | + uint8_t domain; |
| 72 | + uint8_t type; |
| 73 | + bool bound; |
| 74 | +} mod_network_socket_obj_t; |
| 75 | + |
| 76 | +void mod_network_init(
179B
void); |
| 77 | +void mod_network_deinit(void); |
| 78 | +void mod_network_register_nic(mp_obj_t nic); |
| 79 | +mp_obj_t mod_network_find_nic(const uint8_t *ip); |
| 80 | + |
| 81 | +#endif // MICROPY_INCLUDED_RP2_MODNETWORK_H |
0 commit comments