Explorar o código

add modusocket moduselect

SummerGift %!s(int64=8) %!d(string=hai) anos
pai
achega
0e7d1855b0
Modificáronse 6 ficheiros con 1015 adicións e 2 borrados
  1. 2 2
      Quick Start Guide.md
  2. 13 0
      port/genhdr/qstrdefs.generated.h
  3. 93 0
      port/modnetwork.c
  4. 83 0
      port/modnetwork.h
  5. 354 0
      port/moduselect.c
  6. 470 0
      port/modusocket.c

+ 2 - 2
Quick Start Guide.md

@@ -130,9 +130,9 @@ GC:
 | -------------------------------- | ---------------------------------------- | -------- |
 | array                            | arrays of numeric data                   | yes      |
 | cmath                            | mathematical functions for complex numbers | yes      |
-| gc                               | control the garbage collector            | no       |
+| gc                               | control the garbage collector            | yes      |
 | math                             | mathematical functions                   | yes      |
-| sys                              | system specific functions                | no       |
+| sys                              | system specific functions                | yes      |
 | time                             | time related functions                   | yes      |
 | ubinascii                        | binary/ASCII conversions                 | yes      |
 | ucollections                     | collection and container types           | yes      |

+ 13 - 0
port/genhdr/qstrdefs.generated.h

@@ -470,6 +470,19 @@ QDEF(MP_QSTR_exit, (const byte*)"\x85\xbe\x04" "exit")
 QDEF(MP_QSTR_byteorder, (const byte*)"\x61\x99\x09" "byteorder")
 QDEF(MP_QSTR_version_info, (const byte*)"\x6e\x0a\x0c" "version_info")
 QDEF(MP_QSTR_sys, (const byte*)"\xbc\x8e\x03" "sys")
+QDEF(MP_QSTR_fileno, (const byte*)"\x82\x76\x06" "fileno")
+QDEF(MP_QSTR_makefile, (const byte*)"\xc1\xd5\x08" "makefile")
+QDEF(MP_QSTR_usocket, (const byte*)"\x75\x00\x07" "usocket")
+QDEF(MP_QSTR_getaddrinfo, (const byte*)"\x6e\x18\x0b" "getaddrinfo")
+QDEF(MP_QSTR_inet_pton, (const byte*)"\xc9\x94\x09" "inet_pton")
+QDEF(MP_QSTR_inet_ntop, (const byte*)"\x89\x1d\x09" "inet_ntop")
+QDEF(MP_QSTR_sockaddr, (const byte*)"\x62\x8c\x08" "sockaddr")
+QDEF(MP_QSTR_MSG_DONTROUTE, (const byte*)"\xeb\x5d\x0d" "MSG_DONTROUTE")
+QDEF(MP_QSTR_MSG_DONTWAIT, (const byte*)"\x99\xc8\x0c" "MSG_DONTWAIT")
+QDEF(MP_QSTR_SO_BROADCAST, (const byte*)"\xf9\xc6\x0c" "SO_BROADCAST")
+QDEF(MP_QSTR_AF_UNIX, (const byte*)"\xf7\x01\x07" "AF_UNIX")
+QDEF(MP_QSTR_SO_ERROR, (const byte*)"\x3e\x2a\x08" "SO_ERROR")
+
 
 
 // This file was automatically generated by makeqstrdata.py

+ 93 - 0
port/modnetwork.c

@@ -0,0 +1,93 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2014 Damien P. George
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include <stdio.h>
+#include <stdint.h>
+#include <string.h>
+
+#include "py/objlist.h"
+#include "py/runtime.h"
+#include "modnetwork.h"
+
+#if MICROPY_PY_NETWORK
+
+/// \module network - network configuration
+///
+/// This module provides network drivers and routing configuration.
+
+void mod_network_init(void) {
+    mp_obj_list_init(&MP_STATE_PORT(mod_network_nic_list), 0);
+}
+
+void mod_network_register_nic(mp_obj_t nic) {
+    for (mp_uint_t i = 0; i < MP_STATE_PORT(mod_network_nic_list).len; i++) {
+        if (MP_STATE_PORT(mod_network_nic_list).items[i] == nic) {
+            // nic already registered
+            return;
+        }
+    }
+    // nic not registered so add to list
+    mp_obj_list_append(&MP_STATE_PORT(mod_network_nic_list), nic);
+}
+
+mp_obj_t mod_network_find_nic(const uint8_t *ip) {
+    // find a NIC that is suited to given IP address
+    for (mp_uint_t i = 0; i < MP_STATE_PORT(mod_network_nic_list).len; i++) {
+        mp_obj_t nic = MP_STATE_PORT(mod_network_nic_list).items[i];
+        // TODO check IP suitability here
+        //mod_network_nic_type_t *nic_type = (mod_network_nic_type_t*)mp_obj_get_type(nic);
+        return nic;
+    }
+
+    nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "no available NIC"));
+}
+
+STATIC mp_obj_t network_route(void) {
+    return &MP_STATE_PORT(mod_network_nic_list);
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_0(network_route_obj, network_route);
+
+STATIC const mp_rom_map_elem_t mp_module_network_globals_table[] = {
+    { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_network) },
+
+    #if MICROPY_PY_WIZNET5K
+    { MP_ROM_QSTR(MP_QSTR_WIZNET5K), MP_ROM_PTR(&mod_network_nic_type_wiznet5k) },
+    #endif
+    #if MICROPY_PY_CC3K
+    { MP_ROM_QSTR(MP_QSTR_CC3K), MP_ROM_PTR(&mod_network_nic_type_cc3k) },
+    #endif
+
+    { MP_ROM_QSTR(MP_QSTR_route), MP_ROM_PTR(&network_route_obj) },
+};
+
+STATIC MP_DEFINE_CONST_DICT(mp_module_network_globals, mp_module_network_globals_table);
+
+const mp_obj_module_t mp_module_network = {
+    .base = { &mp_type_module },
+    .globals = (mp_obj_dict_t*)&mp_module_network_globals,
+};
+
+#endif  // MICROPY_PY_NETWORK

+ 83 - 0
port/modnetwork.h

@@ -0,0 +1,83 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2013, 2014 Damien P. George
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+#ifndef MICROPY_INCLUDED_STMHAL_MODNETWORK_H
+#define MICROPY_INCLUDED_STMHAL_MODNETWORK_H
+
+#define MOD_NETWORK_IPADDR_BUF_SIZE (4)
+
+#define MOD_NETWORK_AF_INET (2)
+#define MOD_NETWORK_AF_INET6 (10)
+
+#define MOD_NETWORK_SOCK_STREAM (1)
+#define MOD_NETWORK_SOCK_DGRAM (2)
+#define MOD_NETWORK_SOCK_RAW (3)
+
+struct _mod_network_socket_obj_t;
+
+typedef struct _mod_network_nic_type_t {
+    mp_obj_type_t base;
+
+    // API for non-socket operations
+    int (*gethostbyname)(mp_obj_t nic, const char *name, mp_uint_t len, uint8_t *ip_out);
+
+    // API for socket operations; return -1 on error
+    int (*socket)(struct _mod_network_socket_obj_t *socket, int *_errno);
+    void (*close)(struct _mod_network_socket_obj_t *socket);
+    int (*bind)(struct _mod_network_socket_obj_t *socket, byte *ip, mp_uint_t port, int *_errno);
+    int (*listen)(struct _mod_network_socket_obj_t *socket, mp_int_t backlog, int *_errno);
+    int (*accept)(struct _mod_network_socket_obj_t *socket, struct _mod_network_socket_obj_t *socket2, byte *ip, mp_uint_t *port, int *_errno);
+    int (*connect)(struct _mod_network_socket_obj_t *socket, byte *ip, mp_uint_t port, int *_errno);
+    mp_uint_t (*send)(struct _mod_network_socket_obj_t *socket, const byte *buf, mp_uint_t len, int *_errno);
+    mp_uint_t (*recv)(struct _mod_network_socket_obj_t *socket, byte *buf, mp_uint_t len, int *_errno);
+    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);
+    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);
+    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);
+    int (*settimeout)(struct _mod_network_socket_obj_t *socket, mp_uint_t timeout_ms, int *_errno);
+    int (*ioctl)(struct _mod_network_socket_obj_t *socket, mp_uint_t request, mp_uint_t arg, int *_errno);
+} mod_network_nic_type_t;
+
+typedef struct _mod_network_socket_obj_t {
+    mp_obj_base_t base;
+    mp_obj_t nic;
+    mod_network_nic_type_t *nic_type;
+    union {
+        struct {
+            uint8_t domain;
+            uint8_t type;
+            int8_t fileno;
+        } u_param;
+        mp_uint_t u_state;
+    };
+} mod_network_socket_obj_t;
+
+extern const mod_network_nic_type_t mod_network_nic_type_wiznet5k;
+extern const mod_network_nic_type_t mod_network_nic_type_cc3k;
+
+void mod_network_init(void);
+void mod_network_register_nic(mp_obj_t nic);
+mp_obj_t mod_network_find_nic(const uint8_t *ip);
+
+#endif // MICROPY_INCLUDED_STMHAL_MODNETWORK_H

+ 354 - 0
port/moduselect.c

@@ -0,0 +1,354 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2014 Damien P. George
+ * Copyright (c) 2015-2017 Paul Sokolovsky
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "py/mpconfig.h"
+
+#if MICROPY_PY_USELECT_POSIX
+
+#include <stdio.h>
+#include <errno.h>
+#include <poll.h>
+
+#include "py/runtime.h"
+#include "py/obj.h"
+#include "py/objlist.h"
+#include "py/objtuple.h"
+#include "py/mphal.h"
+#include "fdfile.h"
+
+#define DEBUG 0
+
+#if MICROPY_PY_SOCKET
+extern const mp_obj_type_t mp_type_socket;
+#endif
+
+// Flags for poll()
+#define FLAG_ONESHOT (1)
+
+/// \class Poll - poll class
+
+typedef struct _mp_obj_poll_t {
+    mp_obj_base_t base;
+    unsigned short alloc;
+    unsigned short len;
+    struct pollfd *entries;
+    mp_obj_t *obj_map;
+    short iter_cnt;
+    short iter_idx;
+    int flags;
+    // callee-owned tuple
+    mp_obj_t ret_tuple;
+} mp_obj_poll_t;
+
+STATIC int get_fd(mp_obj_t fdlike) {
+    int fd;
+    // Shortcut for fdfile compatible types
+    if (MP_OBJ_IS_TYPE(fdlike, &mp_type_fileio)
+        #if MICROPY_PY_SOCKET
+        || MP_OBJ_IS_TYPE(fdlike, &mp_type_socket)
+        #endif
+        ) {
+        mp_obj_fdfile_t *fdfile = MP_OBJ_TO_PTR(fdlike);
+        fd = fdfile->fd;
+    } else {
+        fd = mp_obj_get_int(fdlike);
+    }
+    return fd;
+}
+
+/// \method register(obj[, eventmask])
+STATIC mp_obj_t poll_register(size_t n_args, const mp_obj_t *args) {
+    mp_obj_poll_t *self = MP_OBJ_TO_PTR(args[0]);
+    bool is_fd = MP_OBJ_IS_INT(args[1]);
+    int fd = get_fd(args[1]);
+
+    mp_uint_t flags;
+    if (n_args == 3) {
+        flags = mp_obj_get_int(args[2]);
+    } else {
+        flags = POLLIN | POLLOUT;
+    }
+
+    struct pollfd *free_slot = NULL;
+
+    struct pollfd *entry = self->entries;
+    for (int i = 0; i < self->len; i++, entry++) {
+        int entry_fd = entry->fd;
+        if (entry_fd == fd) {
+            entry->events = flags;
+            return mp_const_false;
+        }
+        if (entry_fd == -1) {
+            free_slot = entry;
+        }
+    }
+
+    if (free_slot == NULL) {
+        if (self->len >= self->alloc) {
+            self->entries = m_renew(struct pollfd, self->entries, self->alloc, self->alloc + 4);
+            if (self->obj_map) {
+                self->obj_map = m_renew(mp_obj_t, self->obj_map, self->alloc, self->alloc + 4);
+            }
+            self->alloc += 4;
+        }
+        free_slot = &self->entries[self->len++];
+    }
+
+    if (!is_fd) {
+        if (self->obj_map == NULL) {
+            self->obj_map = m_new0(mp_obj_t, self->alloc);
+        }
+        self->obj_map[free_slot - self->entries] = args[1];
+    }
+
+    free_slot->fd = fd;
+    free_slot->events = flags;
+    free_slot->revents = 0;
+    return mp_const_true;
+}
+MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(poll_register_obj, 2, 3, poll_register);
+
+/// \method unregister(obj)
+STATIC mp_obj_t poll_unregister(mp_obj_t self_in, mp_obj_t obj_in) {
+    mp_obj_poll_t *self = MP_OBJ_TO_PTR(self_in);
+    struct pollfd *entries = self->entries;
+    int fd = get_fd(obj_in);
+    for (int i = self->len - 1; i >= 0; i--) {
+        if (entries->fd == fd) {
+            entries->fd = -1;
+            if (self->obj_map) {
+                self->obj_map[entries - self->entries] = MP_OBJ_NULL;
+            }
+            break;
+        }
+        entries++;
+    }
+
+    // TODO raise KeyError if obj didn't exist in map
+    return mp_const_none;
+}
+MP_DEFINE_CONST_FUN_OBJ_2(poll_unregister_obj, poll_unregister);
+
+/// \method modify(obj, eventmask)
+STATIC mp_obj_t poll_modify(mp_obj_t self_in, mp_obj_t obj_in, mp_obj_t eventmask_in) {
+    mp_obj_poll_t *self = MP_OBJ_TO_PTR(self_in);
+    struct pollfd *entries = self->entries;
+    int fd = get_fd(obj_in);
+    for (int i = self->len - 1; i >= 0; i--) {
+        if (entries->fd == fd) {
+            entries->events = mp_obj_get_int(eventmask_in);
+            break;
+        }
+        entries++;
+    }
+
+    // TODO raise KeyError if obj didn't exist in map
+    return mp_const_none;
+}
+MP_DEFINE_CONST_FUN_OBJ_3(poll_modify_obj, poll_modify);
+
+STATIC int poll_poll_internal(size_t n_args, const mp_obj_t *args) {
+    mp_obj_poll_t *self = MP_OBJ_TO_PTR(args[0]);
+
+    // work out timeout (it's given already in ms)
+    int timeout = -1;
+    int flags = 0;
+    if (n_args >= 2) {
+        if (args[1] != mp_const_none) {
+            mp_int_t timeout_i = mp_obj_get_int(args[1]);
+            if (timeout_i >= 0) {
+                timeout = timeout_i;
+            }
+        }
+        if (n_args >= 3) {
+            flags = mp_obj_get_int(args[2]);
+        }
+    }
+
+    self->flags = flags;
+
+    int n_ready = poll(self->entries, self->len, timeout);
+    RAISE_ERRNO(n_ready, errno);
+    return n_ready;
+}
+
+/// \method poll([timeout])
+/// Timeout is in milliseconds.
+STATIC mp_obj_t poll_poll(size_t n_args, const mp_obj_t *args) {
+    int n_ready = poll_poll_internal(n_args, args);
+
+    if (n_ready == 0) {
+        return mp_const_empty_tuple;
+    }
+
+    mp_obj_poll_t *self = MP_OBJ_TO_PTR(args[0]);
+
+    mp_obj_list_t *ret_list = MP_OBJ_TO_PTR(mp_obj_new_list(n_ready, NULL));
+    int ret_i = 0;
+    struct pollfd *entries = self->entries;
+    for (int i = 0; i < self->len; i++, entries++) {
+        if (entries->revents != 0) {
+            mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(2, NULL));
+            // If there's an object stored, return it, otherwise raw fd
+            if (self->obj_map && self->obj_map[i] != MP_OBJ_NULL) {
+                t->items[0] = self->obj_map[i];
+            } else {
+                t->items[0] = MP_OBJ_NEW_SMALL_INT(entries->fd);
+            }
+            t->items[1] = MP_OBJ_NEW_SMALL_INT(entries->revents);
+            ret_list->items[ret_i++] = MP_OBJ_FROM_PTR(t);
+            if (self->flags & FLAG_ONESHOT) {
+                entries->events = 0;
+            }
+        }
+    }
+
+    return MP_OBJ_FROM_PTR(ret_list);
+}
+MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(poll_poll_obj, 1, 3, poll_poll);
+
+STATIC mp_obj_t poll_ipoll(size_t n_args, const mp_obj_t *args) {
+    mp_obj_poll_t *self = MP_OBJ_TO_PTR(args[0]);
+
+    if (self->ret_tuple == MP_OBJ_NULL) {
+        self->ret_tuple = mp_obj_new_tuple(2, NULL);
+    }
+
+    int n_ready = poll_poll_internal(n_args, args);
+    self->iter_cnt = n_ready;
+    self->iter_idx = 0;
+
+    return args[0];
+}
+MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(poll_ipoll_obj, 1, 3, poll_ipoll);
+
+STATIC mp_obj_t poll_iternext(mp_obj_t self_in) {
+    mp_obj_poll_t *self = MP_OBJ_TO_PTR(self_in);
+
+    if (self->iter_cnt == 0) {
+        return MP_OBJ_STOP_ITERATION;
+    }
+
+    self->iter_cnt--;
+
+    struct pollfd *entries = self->entries + self->iter_idx;
+    for (int i = self->iter_idx; i < self->len; i++, entries++) {
+        self->iter_idx++;
+        if (entries->revents != 0) {
+            mp_obj_tuple_t *t = MP_OBJ_TO_PTR(self->ret_tuple);
+            // If there's an object stored, return it, otherwise raw fd
+            if (self->obj_map && self->obj_map[i] != MP_OBJ_NULL) {
+                t->items[0] = self->obj_map[i];
+            } else {
+                t->items[0] = MP_OBJ_NEW_SMALL_INT(entries->fd);
+            }
+            t->items[1] = MP_OBJ_NEW_SMALL_INT(entries->revents);
+            if (self->flags & FLAG_ONESHOT) {
+                entries->events = 0;
+            }
+            return MP_OBJ_FROM_PTR(t);
+        }
+    }
+
+    assert(!"inconsistent number of poll active entries");
+    self->iter_cnt = 0;
+    return MP_OBJ_STOP_ITERATION;
+}
+
+#if DEBUG
+STATIC mp_obj_t poll_dump(mp_obj_t self_in) {
+    mp_obj_poll_t *self = MP_OBJ_TO_PTR(self_in);
+
+    struct pollfd *entries = self->entries;
+    for (int i = self->len - 1; i >= 0; i--) {
+        printf("fd: %d ev: %x rev: %x", entries->fd, entries->events, entries->revents);
+        if (self->obj_map) {
+            printf(" obj: %p", self->obj_map[entries - self->entries]);
+        }
+        printf("\n");
+        entries++;
+    }
+
+    return mp_const_none;
+}
+MP_DEFINE_CONST_FUN_OBJ_1(poll_dump_obj, poll_dump);
+#endif
+
+STATIC const mp_rom_map_elem_t poll_locals_dict_table[] = {
+    { MP_ROM_QSTR(MP_QSTR_register), MP_ROM_PTR(&poll_register_obj) },
+    { MP_ROM_QSTR(MP_QSTR_unregister), MP_ROM_PTR(&poll_unregister_obj) },
+    { MP_ROM_QSTR(MP_QSTR_modify), MP_ROM_PTR(&poll_modify_obj) },
+    { MP_ROM_QSTR(MP_QSTR_poll), MP_ROM_PTR(&poll_poll_obj) },
+    { MP_ROM_QSTR(MP_QSTR_ipoll), MP_ROM_PTR(&poll_ipoll_obj) },
+    #if DEBUG
+    { MP_ROM_QSTR(MP_QSTR_dump), MP_ROM_PTR(&poll_dump_obj) },
+    #endif
+};
+STATIC MP_DEFINE_CONST_DICT(poll_locals_dict, poll_locals_dict_table);
+
+STATIC const mp_obj_type_t mp_type_poll = {
+    { &mp_type_type },
+    .name = MP_QSTR_poll,
+    .getiter = mp_identity_getiter,
+    .iternext = poll_iternext,
+    .locals_dict = (void*)&poll_locals_dict,
+};
+
+STATIC mp_obj_t select_poll(size_t n_args, const mp_obj_t *args) {
+    int alloc = 4;
+    if (n_args > 0) {
+        alloc = mp_obj_get_int(args[0]);
+    }
+    mp_obj_poll_t *poll = m_new_obj(mp_obj_poll_t);
+    poll->base.type = &mp_type_poll;
+    poll->entries = m_new(struct pollfd, alloc);
+    poll->alloc = alloc;
+    poll->len = 0;
+    poll->obj_map = NULL;
+    poll->iter_cnt = 0;
+    poll->ret_tuple = MP_OBJ_NULL;
+    return MP_OBJ_FROM_PTR(poll);
+}
+MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_select_poll_obj, 0, 1, select_poll);
+
+STATIC const mp_rom_map_elem_t mp_module_select_globals_table[] = {
+    { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_uselect) },
+    { MP_ROM_QSTR(MP_QSTR_poll), MP_ROM_PTR(&mp_select_poll_obj) },
+    { MP_ROM_QSTR(MP_QSTR_POLLIN), MP_ROM_INT(POLLIN) },
+    { MP_ROM_QSTR(MP_QSTR_POLLOUT), MP_ROM_INT(POLLOUT) },
+    { MP_ROM_QSTR(MP_QSTR_POLLERR), MP_ROM_INT(POLLERR) },
+    { MP_ROM_QSTR(MP_QSTR_POLLHUP), MP_ROM_INT(POLLHUP) },
+};
+
+STATIC MP_DEFINE_CONST_DICT(mp_module_select_globals, mp_module_select_globals_table);
+
+const mp_obj_module_t mp_module_uselect = {
+    .base = { &mp_type_module },
+    .globals = (mp_obj_dict_t*)&mp_module_select_globals,
+};
+
+#endif // MICROPY_PY_USELECT_POSIX

+ 470 - 0
port/modusocket.c

@@ -0,0 +1,470 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2014 Damien P. George
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include <stdio.h>
+#include <string.h>
+
+#include "py/objtuple.h"
+#include "py/objlist.h"
+#include "py/runtime.h"
+#include "py/mperrno.h"
+#include "lib/netutils/netutils.h"
+#include "modnetwork.h"
+
+#if MICROPY_PY_USOCKET
+
+/******************************************************************************/
+// socket class
+
+STATIC const mp_obj_type_t socket_type;
+
+// constructor socket(family=AF_INET, type=SOCK_STREAM, proto=0, fileno=None)
+STATIC mp_obj_t socket_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
+    mp_arg_check_num(n_args, n_kw, 0, 4, false);
+
+    // create socket object (not bound to any NIC yet)
+    mod_network_socket_obj_t *s = m_new_obj_with_finaliser(mod_network_socket_obj_t);
+    s->base.type = (mp_obj_t)&socket_type;
+    s->nic = MP_OBJ_NULL;
+    s->nic_type = NULL;
+    s->u_param.domain = MOD_NETWORK_AF_INET;
+    s->u_param.type = MOD_NETWORK_SOCK_STREAM;
+    s->u_param.fileno = -1;
+    if (n_args >= 1) {
+        s->u_param.domain = mp_obj_get_int(args[0]);
+        if (n_args >= 2) {
+            s->u_param.type = mp_obj_get_int(args[1]);
+            if (n_args >= 4) {
+                s->u_param.fileno = mp_obj_get_int(args[3]);
+            }
+        }
+    }
+
+    return s;
+}
+
+STATIC void socket_select_nic(mod_network_socket_obj_t *self, const byte *ip) {
+    if (self->nic == MP_OBJ_NULL) {
+        // select NIC based on IP
+        self->nic = mod_network_find_nic(ip);
+        self->nic_type = (mod_network_nic_type_t*)mp_obj_get_type(self->nic);
+
+        // call the NIC to open the socket
+        int _errno;
+        if (self->nic_type->socket(self, &_errno) != 0) {
+            mp_raise_OSError(_errno);
+        }
+    }
+}
+// method socket.close()
+STATIC mp_obj_t socket_close(mp_obj_t self_in) {
+    mod_network_socket_obj_t *self = self_in;
+    if (self->nic != MP_OBJ_NULL) {
+        self->nic_type->close(self);
+        self->nic = MP_OBJ_NULL;
+    }
+    return mp_const_none;
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(socket_close_obj, socket_close);
+
+// method socket.bind(address)
+STATIC mp_obj_t socket_bind(mp_obj_t self_in, mp_obj_t addr_in) {
+    mod_network_socket_obj_t *self = self_in;
+
+    // get address
+    uint8_t ip[MOD_NETWORK_IPADDR_BUF_SIZE];
+    mp_uint_t port = netutils_parse_inet_addr(addr_in, ip, NETUTILS_BIG);
+
+    // check if we need to select a NIC
+    socket_select_nic(self, ip);
+
+    // call the NIC to bind the socket
+    int _errno;
+    if (self->nic_type->bind(self, ip, port, &_errno) != 0) {
+        mp_raise_OSError(_errno);
+    }
+
+    return mp_const_none;
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_bind_obj, socket_bind);
+
+// method socket.listen(backlog)
+STATIC mp_obj_t socket_listen(mp_obj_t self_in, mp_obj_t backlog) {
+    mod_network_socket_obj_t *self = self_in;
+
+    if (self->nic == MP_OBJ_NULL) {
+        // not connected
+        // TODO I think we can listen even if not bound...
+        mp_raise_OSError(MP_ENOTCONN);
+    }
+
+    int _errno;
+    if (self->nic_type->listen(self, mp_obj_get_int(backlog), &_errno) != 0) {
+        mp_raise_OSError(_errno);
+    }
+
+    return mp_const_none;
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_listen_obj, socket_listen);
+
+// method socket.accept()
+STATIC mp_obj_t socket_accept(mp_obj_t self_in) {
+    mod_network_socket_obj_t *self = self_in;
+
+    // create new socket object
+    // starts with empty NIC so that finaliser doesn't run close() method if accept() fails
+    mod_network_socket_obj_t *socket2 = m_new_obj_with_finaliser(mod_network_socket_obj_t);
+    socket2->base.type = (mp_obj_t)&socket_type;
+    socket2->nic = MP_OBJ_NULL;
+    socket2->nic_type = NULL;
+
+    // accept incoming connection
+    uint8_t ip[MOD_NETWORK_IPADDR_BUF_SIZE];
+    mp_uint_t port;
+    int _errno;
+    if (self->nic_type->accept(self, socket2, ip, &port, &_errno) != 0) {
+        mp_raise_OSError(_errno);
+    }
+
+    // new socket has valid state, so set the NIC to the same as parent
+    socket2->nic = self->nic;
+    socket2->nic_type = self->nic_type;
+
+    // make the return value
+    mp_obj_tuple_t *client = mp_obj_new_tuple(2, NULL);
+    client->items[0] = socket2;
+    client->items[1] = netutils_format_inet_addr(ip, port, NETUTILS_BIG);
+
+    return client;
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(socket_accept_obj, socket_accept);
+
+// method socket.connect(address)
+STATIC mp_obj_t socket_connect(mp_obj_t self_in, mp_obj_t addr_in) {
+    mod_network_socket_obj_t *self = self_in;
+
+    // get address
+    uint8_t ip[MOD_NETWORK_IPADDR_BUF_SIZE];
+    mp_uint_t port = netutils_parse_inet_addr(addr_in, ip, NETUTILS_BIG);
+
+    // check if we need to select a NIC
+    socket_select_nic(self, ip);
+
+    // call the NIC to connect the socket
+    int _errno;
+    if (self->nic_type->connect(self, ip, port, &_errno) != 0) {
+        mp_raise_OSError(_errno);
+    }
+
+    return mp_const_none;
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_connect_obj, socket_connect);
+
+// method socket.send(bytes)
+STATIC mp_obj_t socket_send(mp_obj_t self_in, mp_obj_t buf_in) {
+    mod_network_socket_obj_t *self = self_in;
+    if (self->nic == MP_OBJ_NULL) {
+        // not connected
+        mp_raise_OSError(MP_EPIPE);
+    }
+    mp_buffer_info_t bufinfo;
+    mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_READ);
+    int _errno;
+    mp_uint_t ret = self->nic_type->send(self, bufinfo.buf, bufinfo.len, &_errno);
+    if (ret == -1) {
+        mp_raise_OSError(_errno);
+    }
+    return mp_obj_new_int_from_uint(ret);
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_send_obj, socket_send);
+
+// method socket.recv(bufsize)
+STATIC mp_obj_t socket_recv(mp_obj_t self_in, mp_obj_t len_in) {
+    mod_network_socket_obj_t *self = self_in;
+    if (self->nic == MP_OBJ_NULL) {
+        // not connected
+        mp_raise_OSError(MP_ENOTCONN);
+    }
+    mp_int_t len = mp_obj_get_int(len_in);
+    vstr_t vstr;
+    vstr_init_len(&vstr, len);
+    int _errno;
+    mp_uint_t ret = self->nic_type->recv(self, (byte*)vstr.buf, len, &_errno);
+    if (ret == -1) {
+        mp_raise_OSError(_errno);
+    }
+    if (ret == 0) {
+        return mp_const_empty_bytes;
+    }
+    vstr.len = ret;
+    return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_recv_obj, socket_recv);
+
+// method socket.sendto(bytes, address)
+STATIC mp_obj_t socket_sendto(mp_obj_t self_in, mp_obj_t data_in, mp_obj_t addr_in) {
+    mod_network_socket_obj_t *self = self_in;
+
+    // get the data
+    mp_buffer_info_t bufinfo;
+    mp_get_buffer_raise(data_in, &bufinfo, MP_BUFFER_READ);
+
+    // get address
+    uint8_t ip[MOD_NETWORK_IPADDR_BUF_SIZE];
+    mp_uint_t port = netutils_parse_inet_addr(addr_in, ip, NETUTILS_BIG);
+
+    // check if we need to select a NIC
+    socket_select_nic(self, ip);
+
+    // call the NIC to sendto
+    int _errno;
+    mp_int_t ret = self->nic_type->sendto(self, bufinfo.buf, bufinfo.len, ip, port, &_errno);
+    if (ret == -1) {
+        mp_raise_OSError(_errno);
+    }
+
+    return mp_obj_new_int(ret);
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_3(socket_sendto_obj, socket_sendto);
+
+// method socket.recvfrom(bufsize)
+STATIC mp_obj_t socket_recvfrom(mp_obj_t self_in, mp_obj_t len_in) {
+    mod_network_socket_obj_t *self = self_in;
+    if (self->nic == MP_OBJ_NULL) {
+        // not connected
+        mp_raise_OSError(MP_ENOTCONN);
+    }
+    vstr_t vstr;
+    vstr_init_len(&vstr, mp_obj_get_int(len_in));
+    byte ip[4];
+    mp_uint_t port;
+    int _errno;
+    mp_int_t ret = self->nic_type->recvfrom(self, (byte*)vstr.buf, vstr.len, ip, &port, &_errno);
+    if (ret == -1) {
+        mp_raise_OSError(_errno);
+    }
+    mp_obj_t tuple[2];
+    if (ret == 0) {
+        tuple[0] = mp_const_empty_bytes;
+    } else {
+        vstr.len = ret;
+        tuple[0] = mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
+    }
+    tuple[1] = netutils_format_inet_addr(ip, port, NETUTILS_BIG);
+    return mp_obj_new_tuple(2, tuple);
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_recvfrom_obj, socket_recvfrom);
+
+// method socket.setsockopt(level, optname, value)
+STATIC mp_obj_t socket_setsockopt(size_t n_args, const mp_obj_t *args) {
+    mod_network_socket_obj_t *self = args[0];
+
+    mp_int_t level = mp_obj_get_int(args[1]);
+    mp_int_t opt = mp_obj_get_int(args[2]);
+
+    const void *optval;
+    mp_uint_t optlen;
+    mp_int_t val;
+    if (mp_obj_is_integer(args[3])) {
+        val = mp_obj_get_int_truncated(args[3]);
+        optval = &val;
+        optlen = sizeof(val);
+    } else {
+        mp_buffer_info_t bufinfo;
+        mp_get_buffer_raise(args[3], &bufinfo, MP_BUFFER_READ);
+        optval = bufinfo.buf;
+        optlen = bufinfo.len;
+    }
+
+    int _errno;
+    if (self->nic_type->setsockopt(self, level, opt, optval, optlen, &_errno) != 0) {
+        mp_raise_OSError(_errno);
+    }
+
+    return mp_const_none;
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(socket_setsockopt_obj, 4, 4, socket_setsockopt);
+
+// method socket.settimeout(value)
+// timeout=0 means non-blocking
+// timeout=None means blocking
+// otherwise, timeout is in seconds
+STATIC mp_obj_t socket_settimeout(mp_obj_t self_in, mp_obj_t timeout_in) {
+    mod_network_socket_obj_t *self = self_in;
+    if (self->nic == MP_OBJ_NULL) {
+        // not connected
+        mp_raise_OSError(MP_ENOTCONN);
+    }
+    mp_uint_t timeout;
+    if (timeout_in == mp_const_none) {
+        timeout = -1;
+    } else {
+        #if MICROPY_PY_BUILTINS_FLOAT
+        timeout = 1000 * mp_obj_get_float(timeout_in);
+        #else
+        timeout = 1000 * mp_obj_get_int(timeout_in);
+        #endif
+    }
+    int _errno;
+    if (self->nic_type->settimeout(self, timeout, &_errno) != 0) {
+        mp_raise_OSError(_errno);
+    }
+    return mp_const_none;
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_settimeout_obj, socket_settimeout);
+
+// method socket.setblocking(flag)
+STATIC mp_obj_t socket_setblocking(mp_obj_t self_in, mp_obj_t blocking) {
+    if (mp_obj_is_true(blocking)) {
+        return socket_settimeout(self_in, mp_const_none);
+    } else {
+        return socket_settimeout(self_in, MP_OBJ_NEW_SMALL_INT(0));
+    }
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_setblocking_obj, socket_setblocking);
+
+STATIC const mp_rom_map_elem_t socket_locals_dict_table[] = {
+    { MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&socket_close_obj) },
+    { MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&socket_close_obj) },
+    { MP_ROM_QSTR(MP_QSTR_bind), MP_ROM_PTR(&socket_bind_obj) },
+    { MP_ROM_QSTR(MP_QSTR_listen), MP_ROM_PTR(&socket_listen_obj) },
+    { MP_ROM_QSTR(MP_QSTR_accept), MP_ROM_PTR(&socket_accept_obj) },
+    { MP_ROM_QSTR(MP_QSTR_connect), MP_ROM_PTR(&socket_connect_obj) },
+    { MP_ROM_QSTR(MP_QSTR_send), MP_ROM_PTR(&socket_send_obj) },
+    { MP_ROM_QSTR(MP_QSTR_recv), MP_ROM_PTR(&socket_recv_obj) },
+    { MP_ROM_QSTR(MP_QSTR_sendto), MP_ROM_PTR(&socket_sendto_obj) },
+    { MP_ROM_QSTR(MP_QSTR_recvfrom), MP_ROM_PTR(&socket_recvfrom_obj) },
+    { MP_ROM_QSTR(MP_QSTR_setsockopt), MP_ROM_PTR(&socket_setsockopt_obj) },
+    { MP_ROM_QSTR(MP_QSTR_settimeout), MP_ROM_PTR(&socket_settimeout_obj) },
+    { MP_ROM_QSTR(MP_QSTR_setblocking), MP_ROM_PTR(&socket_setblocking_obj) },
+};
+
+STATIC MP_DEFINE_CONST_DICT(socket_locals_dict, socket_locals_dict_table);
+
+mp_uint_t socket_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint_t arg, int *errcode) {
+    mod_network_socket_obj_t *self = self_in;
+    return self->nic_type->ioctl(self, request, arg, errcode);
+}
+
+STATIC const mp_stream_p_t socket_stream_p = {
+    .ioctl = socket_ioctl,
+    .is_text = false,
+};
+
+STATIC const mp_obj_type_t socket_type = {
+    { &mp_type_type },
+    .name = MP_QSTR_socket,
+    .make_new = socket_make_new,
+    .protocol = &socket_stream_p,
+    .locals_dict = (mp_obj_dict_t*)&socket_locals_dict,
+};
+
+/******************************************************************************/
+// usocket module
+
+// function usocket.getaddrinfo(host, port)
+STATIC mp_obj_t mod_usocket_getaddrinfo(mp_obj_t host_in, mp_obj_t port_in) {
+    size_t hlen;
+    const char *host = mp_obj_str_get_data(host_in, &hlen);
+    mp_int_t port = mp_obj_get_int(port_in);
+    uint8_t out_ip[MOD_NETWORK_IPADDR_BUF_SIZE];
+    bool have_ip = false;
+
+    if (hlen > 0) {
+        // check if host is already in IP form
+        nlr_buf_t nlr;
+        if (nlr_push(&nlr) == 0) {
+            netutils_parse_ipv4_addr(host_in, out_ip, NETUTILS_BIG);
+            have_ip = true;
+            nlr_pop();
+        } else {
+            // swallow exception: host was not in IP form so need to do DNS lookup
+        }
+    }
+
+    if (!have_ip) {
+        // find a NIC that can do a name lookup
+        for (mp_uint_t i = 0; i < MP_STATE_PORT(mod_network_nic_list).len; i++) {
+            mp_obj_t nic = MP_STATE_PORT(mod_network_nic_list).items[i];
+            mod_network_nic_type_t *nic_type = (mod_network_nic_type_t*)mp_obj_get_type(nic);
+            if (nic_type->gethostbyname != NULL) {
+                int ret = nic_type->gethostbyname(nic, host, hlen, out_ip);
+                if (ret != 0) {
+                    mp_raise_OSError(ret);
+                }
+                have_ip = true;
+                break;
+            }
+        }
+    }
+
+    if (!have_ip) {
+        nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "no available NIC"));
+    }
+
+    mp_obj_tuple_t *tuple = mp_obj_new_tuple(5, NULL);
+    tuple->items[0] = MP_OBJ_NEW_SMALL_INT(MOD_NETWORK_AF_INET);
+    tuple->items[1] = MP_OBJ_NEW_SMALL_INT(MOD_NETWORK_SOCK_STREAM);
+    tuple->items[2] = MP_OBJ_NEW_SMALL_INT(0);
+    tuple->items[3] = MP_OBJ_NEW_QSTR(MP_QSTR_);
+    tuple->items[4] = netutils_format_inet_addr(out_ip, port, NETUTILS_BIG);
+    return mp_obj_new_list(1, (mp_obj_t*)&tuple);
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_usocket_getaddrinfo_obj, mod_usocket_getaddrinfo);
+
+STATIC const mp_rom_map_elem_t mp_module_usocket_globals_table[] = {
+    { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_usocket) },
+
+    { MP_ROM_QSTR(MP_QSTR_socket), MP_ROM_PTR(&socket_type) },
+    { MP_ROM_QSTR(MP_QSTR_getaddrinfo), MP_ROM_PTR(&mod_usocket_getaddrinfo_obj) },
+
+    // class constants
+    { MP_ROM_QSTR(MP_QSTR_AF_INET), MP_ROM_INT(MOD_NETWORK_AF_INET) },
+    { MP_ROM_QSTR(MP_QSTR_AF_INET6), MP_ROM_INT(MOD_NETWORK_AF_INET6) },
+
+    { MP_ROM_QSTR(MP_QSTR_SOCK_STREAM), MP_ROM_INT(MOD_NETWORK_SOCK_STREAM) },
+    { MP_ROM_QSTR(MP_QSTR_SOCK_DGRAM), MP_ROM_INT(MOD_NETWORK_SOCK_DGRAM) },
+    { MP_ROM_QSTR(MP_QSTR_SOCK_RAW), MP_ROM_INT(MOD_NETWORK_SOCK_RAW) },
+
+    /*
+    { MP_ROM_QSTR(MP_QSTR_IPPROTO_IP), MP_ROM_INT(MOD_NETWORK_IPPROTO_IP) },
+    { MP_ROM_QSTR(MP_QSTR_IPPROTO_ICMP), MP_ROM_INT(MOD_NETWORK_IPPROTO_ICMP) },
+    { MP_ROM_QSTR(MP_QSTR_IPPROTO_IPV4), MP_ROM_INT(MOD_NETWORK_IPPROTO_IPV4) },
+    { MP_ROM_QSTR(MP_QSTR_IPPROTO_TCP), MP_ROM_INT(MOD_NETWORK_IPPROTO_TCP) },
+    { MP_ROM_QSTR(MP_QSTR_IPPROTO_UDP), MP_ROM_INT(MOD_NETWORK_IPPROTO_UDP) },
+    { MP_ROM_QSTR(MP_QSTR_IPPROTO_IPV6), MP_ROM_INT(MOD_NETWORK_IPPROTO_IPV6) },
+    { MP_ROM_QSTR(MP_QSTR_IPPROTO_RAW), MP_ROM_INT(MOD_NETWORK_IPPROTO_RAW) },
+    */
+};
+
+STATIC MP_DEFINE_CONST_DICT(mp_module_usocket_globals, mp_module_usocket_globals_table);
+
+const mp_obj_module_t mp_module_usocket = {
+    .base = { &mp_type_module },
+    .globals = (mp_obj_dict_t*)&mp_module_usocket_globals,
+};
+
+#endif  // MICROPY_PY_USOCKET