11/*
22 * This file is part of the MicroPython project, http://micropython.org/
33 *
4- * Development of the code in this file was sponsored by Microbric Pty Ltd
5- * and Mnemote Pty Ltd
6- *
74 * The MIT License (MIT)
85 *
9- * Copyright (c) 2016, 2017 Nick Moore @mnemote
6+ * Copyright (c) 2022-2024 Infineon Technologies AG
107 *
11- * Based on extmod/modlwip.c
12- * Copyright (c) 2013, 2014 Damien P. George
13- * Copyright (c) 2015 Galen Hazelwood
8+ * Based on ports/esp32/modsocket.c
9+ * Copyright (c) 2016, 2017 Nick Moore @mnemote
1410 *
1511 * Permission is hereby granted, free of charge, to any person obtaining a copy
1612 * of this software and associated documentation files (the "Software"), to deal
@@ -83,7 +79,6 @@ typedef struct _socket_obj_t {
8379 #endif
8480} socket_obj_t ;
8581
86- // static const char *TAG = "modsocket";
8782
8883void _socket_settimeout (socket_obj_t * sock , uint64_t timeout_ms );
8984
@@ -156,62 +151,6 @@ static inline void check_for_exceptions(void) {
156151 mp_handle_pending (true);
157152}
158153
159- #if MICROPY_HW_ENABLE_MDNS_QUERIES
160- // This function mimics lwip_getaddrinfo, but makes an mDNS query
161- static int mdns_getaddrinfo (const char * host_str , const char * port_str ,
162- const struct addrinfo * hints , struct addrinfo * * res ) {
163- int host_len = strlen (host_str );
164- const int local_len = sizeof (MDNS_LOCAL_SUFFIX ) - 1 ;
165- if (host_len <= local_len ||
166- strcasecmp (host_str + host_len - local_len , MDNS_LOCAL_SUFFIX ) != 0 ) {
167- return 0 ;
168- }
169-
170- // mDNS query
171- char host_no_local [host_len - local_len + 1 ];
172- memcpy (host_no_local , host_str , host_len - local_len );
173- host_no_local [host_len - local_len ] = '\0' ;
174-
175- esp_ip4_addr_t addr = {0 };
176-
177- esp_err_t err = mdns_query_a (host_no_local , MDNS_QUERY_TIMEOUT_MS , & addr );
178- if (err != ESP_OK ) {
179- if (err == ESP_ERR_NOT_FOUND ) {
180- * res = NULL ;
181- return 0 ;
182- }
183- * res = NULL ;
184- return err ;
185- }
186-
187- struct addrinfo * ai = memp_malloc (MEMP_NETDB );
188- if (ai == NULL ) {
189- * res = NULL ;
190- return EAI_MEMORY ;
191- }
192- memset (ai , 0 , sizeof (struct addrinfo ) + sizeof (struct sockaddr_storage ));
193-
194- struct sockaddr_in * sa = (struct sockaddr_in * )((uint8_t * )ai + sizeof (struct addrinfo ));
195- inet_addr_from_ip4addr (& sa -> sin_addr , & addr );
196- sa -> sin_family = AF_INET ;
197- sa -> sin_len = sizeof (struct sockaddr_in );
198- sa -> sin_port = lwip_htons ((u16_t )atoi (port_str ));
199- ai -> ai_family = AF_INET ;
200- ai -> ai_canonname = ((char * )sa + sizeof (struct sockaddr_storage ));
201- memcpy (ai -> ai_canonname , host_str , host_len + 1 );
202- ai -> ai_addrlen = sizeof (struct sockaddr_storage );
203- ai -> ai_addr = (struct sockaddr * )sa ;
204- ai -> ai_socktype = SOCK_STREAM ;
205- if (hints ) {
206- ai -> ai_socktype = hints -> ai_socktype ;
207- ai -> ai_protocol = hints -> ai_protocol ;
208- }
209-
210- * res = ai ;
211- return 0 ;
212- }
213- #endif // MICROPY_HW_ENABLE_MDNS_QUERIES
214-
215154static void _getaddrinfo_inner (const mp_obj_t host , const mp_obj_t portx ,
216155 const struct addrinfo * hints , struct addrinfo * * res ) {
217156 int retval = 0 ;
@@ -235,10 +174,6 @@ static void _getaddrinfo_inner(const mp_obj_t host, const mp_obj_t portx,
235174
236175 MP_THREAD_GIL_EXIT ();
237176
238- #if MICROPY_HW_ENABLE_MDNS_QUERIES
239- retval = mdns_getaddrinfo (host_str , port_str , hints , res );
240- #endif
241-
242177 if (retval == 0 && * res == NULL ) {
243178 // Normal query
244179 retval = lwip_getaddrinfo (host_str , port_str , hints , res );
@@ -288,7 +223,7 @@ static mp_obj_t socket_make_new(const mp_obj_type_t *type_in, size_t n_args, siz
288223
289224 sock -> fd = lwip_socket (sock -> domain , sock -> type , sock -> proto );
290225 if (sock -> fd < 0 && errno == ENFILE ) {
291- // ESP32 LWIP has a hard socket limit, ENFILE is returned when this is
226+ // LWIP has a hard socket limit, ENFILE is returned when this is
292227 // reached. Similar to the logic elsewhere for MemoryError, try running
293228 // GC before failing outright.
294229 gc_collect ();
@@ -408,7 +343,7 @@ static mp_obj_t socket_connect(const mp_obj_t arg0, const mp_obj_t arg1) {
408343 // - Allows emulating a connect timeout, which is not supported by LWIP or
409344 // required by POSIX but is normal behaviour for CPython.
410345 if (fcntl (self -> fd , F_SETFL , flags | O_NONBLOCK ) < 0 ) {
411- // ESP_LOGE(TAG, "fcntl set failed %d", errno); // Unexpected internal failure
346+ mp_raise_msg_varg ( & mp_type_ValueError , MP_ERROR_TEXT ( "fcntl set failed %d" ) , errno );
412347 raise_err = errno ;
413348 }
414349 }
@@ -425,7 +360,7 @@ static mp_obj_t socket_connect(const mp_obj_t arg0, const mp_obj_t arg1) {
425360 // Set the socket back to blocking. We can still pass it to select() in this state.
426361 int r = fcntl (self -> fd , F_SETFL , flags );
427362 if (r != 0 && (raise_err == 0 || raise_err == EINPROGRESS )) {
428- // ESP_LOGE(TAG, "fcntl restore failed %d", errno); // Unexpected internal failure
363+ mp_raise_msg_varg ( & mp_type_ValueError , MP_ERROR_TEXT ( "fcntl restore failed %d" ) , errno );
429364 raise_err = errno ;
430365 }
431366 }
@@ -978,7 +913,6 @@ static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(psoc6_socket_getaddrinfo_obj, 2, 6, p
978913static mp_obj_t psoc6_socket_initialize () {
979914 static int initialized = 0 ;
980915 if (!initialized ) {
981- // ESP_LOGI(TAG, "Initializing");
982916 cy_network_init ();
983917
984918 initialized = 1 ;
0 commit comments