Browse Source

cc3200: Add socket.timeout and socket.error exceptions.

pull/1357/merge
Daniel Campora 9 years ago
parent
commit
f522849a4d
  1. 1
      cc3200/mods/modnetwork.h
  2. 11
      cc3200/mods/modusocket.c
  3. 4
      cc3200/mods/modwlan.c
  4. 1
      cc3200/mpconfigport.h
  5. 2
      cc3200/qstrdefsport.h

1
cc3200/mods/modnetwork.h

@ -52,6 +52,7 @@ typedef struct _mod_network_socket_obj_t {
int16_t sd;
};
bool closed;
bool has_timeout;
} mod_network_socket_obj_t;
/******************************************************************************

11
cc3200/mods/modusocket.c

@ -153,6 +153,7 @@ STATIC mp_obj_t socket_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(_errno)));
}
s->has_timeout = false;
modusocket_socket_add(s->sd, true);
return s;
}
@ -261,6 +262,9 @@ STATIC mp_obj_t socket_recv(mp_obj_t self_in, mp_obj_t len_in) {
int _errno;
mp_uint_t ret = wlan_socket_recv(self, (byte*)vstr.buf, len, &_errno);
if (ret == -1) {
if (_errno == EAGAIN && self->has_timeout) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_TimeoutError, "timed out"));
}
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(_errno)));
}
if (ret == 0) {
@ -304,6 +308,9 @@ STATIC mp_obj_t socket_recvfrom(mp_obj_t self_in, mp_obj_t len_in) {
int _errno;
mp_int_t ret = wlan_socket_recvfrom(self, (byte*)vstr.buf, vstr.len, ip, &port, &_errno);
if (ret == -1) {
if (_errno == EAGAIN && self->has_timeout) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_TimeoutError, "timed out"));
}
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(_errno)));
}
mp_obj_t tuple[2];
@ -446,6 +453,10 @@ STATIC const mp_map_elem_t mp_module_usocket_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_socket), (mp_obj_t)&socket_type },
{ MP_OBJ_NEW_QSTR(MP_QSTR_getaddrinfo), (mp_obj_t)&mod_usocket_getaddrinfo_obj },
// class exceptions
{ MP_OBJ_NEW_QSTR(MP_QSTR_error), (mp_obj_t)&mp_type_OSError },
{ MP_OBJ_NEW_QSTR(MP_QSTR_timeout), (mp_obj_t)&mp_type_TimeoutError },
// class constants
{ MP_OBJ_NEW_QSTR(MP_QSTR_AF_INET), MP_OBJ_NEW_SMALL_INT(AF_INET) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_AF_INET6), MP_OBJ_NEW_SMALL_INT(AF_INET6) },

4
cc3200/mods/modwlan.c

@ -1281,6 +1281,7 @@ int wlan_socket_setsockopt(mod_network_socket_obj_t *socket, mp_uint_t level, mp
int wlan_socket_settimeout(mod_network_socket_obj_t *s, mp_uint_t timeout_s, int *_errno) {
int ret;
bool has_timeout;
if (timeout_s == 0 || timeout_s == -1) {
SlSockNonblocking_t option;
if (timeout_s == 0) {
@ -1291,12 +1292,14 @@ int wlan_socket_settimeout(mod_network_socket_obj_t *s, mp_uint_t timeout_s, int
option.NonblockingEnabled = 0;
}
ret = sl_SetSockOpt(s->sd, SOL_SOCKET, SO_NONBLOCKING, &option, sizeof(option));
has_timeout = false;
} else {
// set timeout
struct SlTimeval_t timeVal;
timeVal.tv_sec = timeout_s; // seconds
timeVal.tv_usec = 0; // microseconds. 10000 microseconds resolution
ret = sl_SetSockOpt(s->sd, SOL_SOCKET, SO_RCVTIMEO, &timeVal, sizeof(timeVal));
has_timeout = true;
}
if (ret != 0) {
@ -1304,6 +1307,7 @@ int wlan_socket_settimeout(mod_network_socket_obj_t *s, mp_uint_t timeout_s, int
return -1;
}
s->has_timeout = has_timeout;
return 0;
}

1
cc3200/mpconfigport.h

@ -66,6 +66,7 @@
#define MICROPY_PY_BUILTINS_MEMORYVIEW (1)
#define MICROPY_PY_BUILTINS_FROZENSET (1)
#define MICROPY_PY_BUILTINS_EXECFILE (1)
#define MICROPY_PY_BUILTINS_TIMEOUTERROR (1)
#define MICROPY_PY_MICROPYTHON_MEM_INFO (0)
#define MICROPY_PY_ARRAY_SLICE_ASSIGN (1)
#define MICROPY_PY_COLLECTIONS_ORDEREDDICT (1)

2
cc3200/qstrdefsport.h

@ -225,6 +225,8 @@ Q(setblocking)
Q(setsockopt)
Q(close)
Q(protocol)
Q(error)
Q(timeout)
Q(AF_INET)
Q(AF_INET6)
Q(SOCK_STREAM)

Loading…
Cancel
Save