Skip to content

Commit f522849

Browse files
author
Daniel Campora
committed
cc3200: Add socket.timeout and socket.error exceptions.
1 parent 077812b commit f522849

5 files changed

Lines changed: 19 additions & 0 deletions

File tree

cc3200/mods/modnetwork.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ typedef struct _mod_network_socket_obj_t {
5252
int16_t sd;
5353
};
5454
bool closed;
55+
bool has_timeout;
5556
} mod_network_socket_obj_t;
5657

5758
/******************************************************************************

cc3200/mods/modusocket.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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_
153153
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(_errno)));
154154
}
155155

156+
s->has_timeout = false;
156157
modusocket_socket_add(s->sd, true);
157158
return s;
158159
}
@@ -261,6 +262,9 @@ STATIC mp_obj_t socket_recv(mp_obj_t self_in, mp_obj_t len_in) {
261262
int _errno;
262263
mp_uint_t ret = wlan_socket_recv(self, (byte*)vstr.buf, len, &_errno);
263264
if (ret == -1) {
265+
if (_errno == EAGAIN && self->has_timeout) {
266+
nlr_raise(mp_obj_new_exception_msg(&mp_type_TimeoutError, "timed out"));
267+
}
264268
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(_errno)));
265269
}
266270
if (ret == 0) {
@@ -304,6 +308,9 @@ STATIC mp_obj_t socket_recvfrom(mp_obj_t self_in, mp_obj_t len_in) {
304308
int _errno;
305309
mp_int_t ret = wlan_socket_recvfrom(self, (byte*)vstr.buf, vstr.len, ip, &port, &_errno);
306310
if (ret == -1) {
311+
if (_errno == EAGAIN && self->has_timeout) {
312+
nlr_raise(mp_obj_new_exception_msg(&mp_type_TimeoutError, "timed out"));
313+
}
307314
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(_errno)));
308315
}
309316
mp_obj_t tuple[2];
@@ -446,6 +453,10 @@ STATIC const mp_map_elem_t mp_module_usocket_globals_table[] = {
446453
{ MP_OBJ_NEW_QSTR(MP_QSTR_socket), (mp_obj_t)&socket_type },
447454
{ MP_OBJ_NEW_QSTR(MP_QSTR_getaddrinfo), (mp_obj_t)&mod_usocket_getaddrinfo_obj },
448455

456+
// class exceptions
457+
{ MP_OBJ_NEW_QSTR(MP_QSTR_error), (mp_obj_t)&mp_type_OSError },
458+
{ MP_OBJ_NEW_QSTR(MP_QSTR_timeout), (mp_obj_t)&mp_type_TimeoutError },
459+
449460
// class constants
450461
{ MP_OBJ_NEW_QSTR(MP_QSTR_AF_INET), MP_OBJ_NEW_SMALL_INT(AF_INET) },
451462
{ MP_OBJ_NEW_QSTR(MP_QSTR_AF_INET6), MP_OBJ_NEW_SMALL_INT(AF_INET6) },

cc3200/mods/modwlan.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,6 +1281,7 @@ int wlan_socket_setsockopt(mod_network_socket_obj_t *socket, mp_uint_t level, mp
12811281

12821282
int wlan_socket_settimeout(mod_network_socket_obj_t *s, mp_uint_t timeout_s, int *_errno) {
12831283
int ret;
1284+
bool has_timeout;
12841285
if (timeout_s == 0 || timeout_s == -1) {
12851286
SlSockNonblocking_t option;
12861287
if (timeout_s == 0) {
@@ -1291,19 +1292,22 @@ int wlan_socket_settimeout(mod_network_socket_obj_t *s, mp_uint_t timeout_s, int
12911292
option.NonblockingEnabled = 0;
12921293
}
12931294
ret = sl_SetSockOpt(s->sd, SOL_SOCKET, SO_NONBLOCKING, &option, sizeof(option));
1295+
has_timeout = false;
12941296
} else {
12951297
// set timeout
12961298
struct SlTimeval_t timeVal;
12971299
timeVal.tv_sec = timeout_s; // seconds
12981300
timeVal.tv_usec = 0; // microseconds. 10000 microseconds resolution
12991301
ret = sl_SetSockOpt(s->sd, SOL_SOCKET, SO_RCVTIMEO, &timeVal, sizeof(timeVal));
1302+
has_timeout = true;
13001303
}
13011304

13021305
if (ret != 0) {
13031306
*_errno = ret;
13041307
return -1;
13051308
}
13061309

1310+
s->has_timeout = has_timeout;
13071311
return 0;
13081312
}
13091313

cc3200/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
#define MICROPY_PY_BUILTINS_MEMORYVIEW (1)
6767
#define MICROPY_PY_BUILTINS_FROZENSET (1)
6868
#define MICROPY_PY_BUILTINS_EXECFILE (1)
69+
#define MICROPY_PY_BUILTINS_TIMEOUTERROR (1)
6970
#define MICROPY_PY_MICROPYTHON_MEM_INFO (0)
7071
#define MICROPY_PY_ARRAY_SLICE_ASSIGN (1)
7172
#define MICROPY_PY_COLLECTIONS_ORDEREDDICT (1)

cc3200/qstrdefsport.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,8 @@ Q(setblocking)
225225
Q(setsockopt)
226226
Q(close)
227227
Q(protocol)
228+
Q(error)
229+
Q(timeout)
228230
Q(AF_INET)
229231
Q(AF_INET6)
230232
Q(SOCK_STREAM)

0 commit comments

Comments
 (0)