Skip to content

Commit d39d96b

Browse files
atxdpgeorge
authored andcommitted
esp8266: Add .onsent callback support
The function passed to socket.onsent() gets called after data is succesfully sent by the socket.
1 parent 2d56df6 commit d39d96b

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

esp8266/modesp.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ typedef struct _esp_socket_obj_t {
4747

4848
mp_obj_t cb_connect;
4949
mp_obj_t cb_recv;
50+
mp_obj_t cb_sent;
5051
mp_obj_t cb_disconnect;
5152

5253
uint8_t *recvbuf;
@@ -69,6 +70,7 @@ STATIC mp_obj_t esp_socket_make_new_base() {
6970
s->cb_connect = mp_const_none;
7071
s->cb_recv = mp_const_none;
7172
s->cb_disconnect = mp_const_none;
73+
s->cb_sent = mp_const_none;
7274
s->fromserver = false;
7375
s->connlist = NULL;
7476
return s;
@@ -168,6 +170,12 @@ STATIC void esp_socket_recv_callback(void *arg, char *pdata, unsigned short len)
168170
}
169171

170172
STATIC void esp_socket_sent_callback(void *arg) {
173+
struct espconn *conn = arg;
174+
esp_socket_obj_t *s = conn->reverse;
175+
176+
if (s->cb_sent != mp_const_none) {
177+
call_function_1_protected(s->cb_sent, s);
178+
}
171179
}
172180

173181
STATIC void esp_socket_disconnect_callback(void *arg) {
@@ -379,6 +387,13 @@ STATIC mp_obj_t esp_socket_onrecv(mp_obj_t self_in, mp_obj_t lambda_in) {
379387
}
380388
STATIC MP_DEFINE_CONST_FUN_OBJ_2(esp_socket_onrecv_obj, esp_socket_onrecv);
381389

390+
STATIC mp_obj_t esp_socket_onsent(mp_obj_t self_in, mp_obj_t lambda_in) {
391+
esp_socket_obj_t *s = self_in;
392+
s->cb_sent = lambda_in;
393+
return mp_const_none;
394+
}
395+
STATIC MP_DEFINE_CONST_FUN_OBJ_2(esp_socket_onsent_obj, esp_socket_onsent);
396+
382397
STATIC mp_obj_t esp_socket_ondisconnect(mp_obj_t self_in, mp_obj_t lambda_in) {
383398
esp_socket_obj_t *s = self_in;
384399
s->cb_disconnect = lambda_in;
@@ -454,6 +469,7 @@ STATIC const mp_map_elem_t esp_socket_locals_dict_table[] = {
454469
{ MP_OBJ_NEW_QSTR(MP_QSTR_recvfrom), (mp_obj_t)&esp_socket_recvfrom_obj },
455470
{ MP_OBJ_NEW_QSTR(MP_QSTR_onconnect), (mp_obj_t)&esp_socket_onconnect_obj },
456471
{ MP_OBJ_NEW_QSTR(MP_QSTR_onrecv), (mp_obj_t)&esp_socket_onrecv_obj },
472+
{ MP_OBJ_NEW_QSTR(MP_QSTR_onsent), (mp_obj_t)&esp_socket_onsent_obj },
457473
{ MP_OBJ_NEW_QSTR(MP_QSTR_ondisconnect), (mp_obj_t)&esp_socket_ondisconnect_obj },
458474
};
459475
STATIC MP_DEFINE_CONST_DICT(esp_socket_locals_dict, esp_socket_locals_dict_table);

esp8266/qstrdefsport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ Q(close)
6262
Q(protocol)
6363
Q(onconnect)
6464
Q(onrecv)
65+
Q(onsent)
6566
Q(ondisconnect)
6667
Q(STAT_IDLE)
6768
Q(STAT_CONNECTING)

0 commit comments

Comments
 (0)