37
37
#include "components/lwip/lwip/src/include/lwip/sys.h"
38
38
#include "components/lwip/lwip/src/include/lwip/netdb.h"
39
39
40
+ #include "esp_log.h"
41
+
42
+ static const char * TAG = "socket" ;
43
+
40
44
STATIC socketpool_socket_obj_t * open_socket_handles [CONFIG_LWIP_MAX_SOCKETS ];
41
45
42
46
void socket_user_reset (void ) {
@@ -62,8 +66,7 @@ bool register_open_socket(socketpool_socket_obj_t *self) {
62
66
return false;
63
67
}
64
68
65
- socketpool_socket_obj_t * common_hal_socketpool_socket_accept (socketpool_socket_obj_t * self ,
66
- uint8_t * ip , uint32_t * port ) {
69
+ int socketpool_socket_accept (socketpool_socket_obj_t * self , uint8_t * ip , uint32_t * port ) {
67
70
struct sockaddr_in accept_addr ;
68
71
socklen_t socklen = sizeof (accept_addr );
69
72
int newsoc = -1 ;
@@ -81,7 +84,10 @@ socketpool_socket_obj_t *common_hal_socketpool_socket_accept(socketpool_socket_o
81
84
newsoc = lwip_accept (self -> num , (struct sockaddr * )& accept_addr , & socklen );
82
85
// In non-blocking mode, fail instead of timing out
83
86
if (newsoc == -1 && self -> timeout_ms == 0 ) {
84
- mp_raise_OSError (MP_EAGAIN );
87
+ if (errno != EAGAIN ) {
88
+ ESP_LOGE (TAG , "accept failed %d" , errno );
89
+ }
90
+ return - MP_EAGAIN ;
85
91
}
86
92
}
87
93
@@ -90,8 +96,17 @@ socketpool_socket_obj_t *common_hal_socketpool_socket_accept(socketpool_socket_o
90
96
memcpy ((void * )ip , (void * )& accept_addr .sin_addr .s_addr , sizeof (accept_addr .sin_addr .s_addr ));
91
97
* port = accept_addr .sin_port ;
92
98
} else {
93
- mp_raise_OSError ( ETIMEDOUT ) ;
99
+ return - ETIMEDOUT ;
94
100
}
101
+ if (newsoc < 0 ) {
102
+ return - MP_EBADF ;
103
+ }
104
+ return newsoc ;
105
+ }
106
+
107
+ socketpool_socket_obj_t * common_hal_socketpool_socket_accept (socketpool_socket_obj_t * self ,
108
+ uint8_t * ip , uint32_t * port ) {
109
+ int newsoc = socketpool_socket_accept (self , ip , port );
95
110
96
111
if (newsoc > 0 ) {
97
112
// Create the socket
@@ -108,7 +123,7 @@ socketpool_socket_obj_t *common_hal_socketpool_socket_accept(socketpool_socket_o
108
123
lwip_fcntl (newsoc , F_SETFL , O_NONBLOCK );
109
124
return sock ;
110
125
} else {
111
- mp_raise_OSError (MP_EBADF );
126
+ mp_raise_OSError (- newsoc );
112
127
return NULL ;
113
128
}
114
129
}
@@ -125,17 +140,22 @@ bool common_hal_socketpool_socket_bind(socketpool_socket_obj_t *self,
125
140
if (err != 0 ) {
126
141
mp_raise_RuntimeError (translate ("Cannot set socket options" ));
127
142
}
128
- int result = lwip_bind (self -> num , (struct sockaddr * )& bind_addr , sizeof (bind_addr )) == 0 ;
129
- return result ;
143
+ int result = lwip_bind (self -> num , (struct sockaddr * )& bind_addr , sizeof (bind_addr ));
144
+ ESP_LOGE (TAG , "bind result %d" , result );
145
+ return result == 0 ;
130
146
}
131
147
132
- void common_hal_socketpool_socket_close (socketpool_socket_obj_t * self ) {
148
+ void socketpool_socket_close (socketpool_socket_obj_t * self ) {
133
149
self -> connected = false;
134
150
if (self -> num >= 0 ) {
135
- lwip_shutdown (self -> num , 0 );
151
+ lwip_shutdown (self -> num , SHUT_RDWR );
136
152
lwip_close (self -> num );
137
153
self -> num = -1 ;
138
154
}
155
+ }
156
+
157
+ void common_hal_socketpool_socket_close (socketpool_socket_obj_t * self ) {
158
+ socketpool_socket_close (self );
139
159
// Remove socket record
140
160
for (size_t i = 0 ; i < MP_ARRAY_SIZE (open_socket_handles ); i ++ ) {
141
161
if (open_socket_handles [i ] == self ) {
@@ -199,7 +219,9 @@ bool common_hal_socketpool_socket_get_connected(socketpool_socket_obj_t *self) {
199
219
}
200
220
201
221
bool common_hal_socketpool_socket_listen (socketpool_socket_obj_t * self , int backlog ) {
202
- return lwip_listen (self -> num , backlog ) == 0 ;
222
+ int result = lwip_listen (self -> num , backlog );
223
+ ESP_LOGE (TAG , "listen result %d" , result );
224
+ return result == 0 ;
203
225
}
204
226
205
227
mp_uint_t common_hal_socketpool_socket_recvfrom_into (socketpool_socket_obj_t * self ,
@@ -242,7 +264,8 @@ mp_uint_t common_hal_socketpool_socket_recvfrom_into(socketpool_socket_obj_t *se
242
264
return received ;
243
265
}
244
266
245
- mp_uint_t common_hal_socketpool_socket_recv_into (socketpool_socket_obj_t * self , const uint8_t * buf , uint32_t len ) {
267
+ int socketpool_socket_recv_into (socketpool_socket_obj_t * self ,
268
+ const uint8_t * buf , uint32_t len ) {
246
269
int received = 0 ;
247
270
bool timed_out = false;
248
271
@@ -261,31 +284,52 @@ mp_uint_t common_hal_socketpool_socket_recv_into(socketpool_socket_obj_t *self,
261
284
262
285
// In non-blocking mode, fail instead of looping
263
286
if (received == -1 && self -> timeout_ms == 0 ) {
264
- mp_raise_OSError (MP_EAGAIN );
287
+ if (errno != EAGAIN ) {
288
+ ESP_LOGE (TAG , "recv %d" , errno );
289
+ }
290
+ return - MP_EAGAIN ;
265
291
}
266
292
}
267
293
} else {
268
- mp_raise_OSError ( MP_EBADF ) ;
294
+ return - MP_EBADF ;
269
295
}
270
296
271
297
if (timed_out ) {
272
- mp_raise_OSError ( ETIMEDOUT ) ;
298
+ return - ETIMEDOUT ;
273
299
}
274
300
return received ;
275
301
}
276
302
277
- mp_uint_t common_hal_socketpool_socket_send (socketpool_socket_obj_t * self , const uint8_t * buf , uint32_t len ) {
303
+ mp_uint_t common_hal_socketpool_socket_recv_into (socketpool_socket_obj_t * self , const uint8_t * buf , uint32_t len ) {
304
+ int received = socketpool_socket_recv_into (self , buf , len );
305
+ if (received < 0 ) {
306
+ mp_raise_OSError (received );
307
+ }
308
+ return received ;
309
+ }
310
+
311
+ int socketpool_socket_send (socketpool_socket_obj_t * self , const uint8_t * buf , uint32_t len ) {
278
312
int sent = -1 ;
279
313
if (self -> num != -1 ) {
280
314
// LWIP Socket
281
315
// TODO: deal with potential failure/add timeout?
282
316
sent = lwip_send (self -> num , buf , len , 0 );
283
317
} else {
284
- mp_raise_OSError ( MP_EBADF ) ;
318
+ sent = - MP_EBADF ;
285
319
}
286
320
287
321
if (sent < 0 ) {
288
- mp_raise_OSError (errno );
322
+ return - errno ;
323
+ }
324
+
325
+ return sent ;
326
+ }
327
+
328
+ mp_uint_t common_hal_socketpool_socket_send (socketpool_socket_obj_t * self , const uint8_t * buf , uint32_t len ) {
329
+ int sent = socketpool_socket_send (self , buf , len );
330
+
331
+ if (sent < 0 ) {
332
+ mp_raise_OSError (- sent );
289
333
}
290
334
return sent ;
291
335
}
0 commit comments