diff options
Diffstat (limited to 'cc3200/mods/modusocket.c')
-rw-r--r-- | cc3200/mods/modusocket.c | 37 |
1 files changed, 15 insertions, 22 deletions
diff --git a/cc3200/mods/modusocket.c b/cc3200/mods/modusocket.c index 929238c5da..d3e596b03b 100644 --- a/cc3200/mods/modusocket.c +++ b/cc3200/mods/modusocket.c @@ -40,6 +40,18 @@ STATIC const mp_obj_type_t socket_type; +STATIC void socket_select_nic(mod_network_socket_obj_t *self) { + // select a nic + self->nic = mod_network_find_nic(); + 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) { + nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(_errno))); + } +} + // constructor socket(family=AF_INET, type=SOCK_STREAM, proto=IPPROTO_TCP, fileno=None) STATIC mp_obj_t socket_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) { mp_arg_check_num(n_args, n_kw, 0, 4, false); @@ -65,22 +77,12 @@ STATIC mp_obj_t socket_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_ } } } + + socket_select_nic(s); + 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) { - nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(_errno))); - } - } -} // method socket.close() STATIC mp_obj_t socket_close(mp_obj_t self_in) { mod_network_socket_obj_t *self = self_in; @@ -99,9 +101,6 @@ STATIC mp_obj_t socket_bind(mp_obj_t self_in, mp_obj_t addr_in) { uint8_t ip[MOD_NETWORK_IPV4ADDR_BUF_SIZE]; mp_uint_t port = mod_network_parse_inet_addr(addr_in, ip); - // 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) { @@ -170,9 +169,6 @@ STATIC mp_obj_t socket_connect(mp_obj_t self_in, mp_obj_t addr_in) { uint8_t ip[MOD_NETWORK_IPV4ADDR_BUF_SIZE]; mp_uint_t port = mod_network_parse_inet_addr(addr_in, ip); - // 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) { @@ -237,9 +233,6 @@ STATIC mp_obj_t socket_sendto(mp_obj_t self_in, mp_obj_t data_in, mp_obj_t addr_ uint8_t ip[MOD_NETWORK_IPV4ADDR_BUF_SIZE]; mp_uint_t port = mod_network_parse_inet_addr(addr_in, ip); - // 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); |