Skip to content

Commit

Permalink
Update network functions to ESP32 SDK 3.0.x
Browse files Browse the repository at this point in the history
  • Loading branch information
pgrawehr authored and mchesser committed Nov 26, 2023
1 parent 0228c64 commit 0bc689d
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 31 deletions.
29 changes: 2 additions & 27 deletions src/EspNetworkFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <sys/poll.h>

#include "esp_wifi.h"
#include <WiFi.h>


tcpip_adapter_if_t tcpip_if[MAX_ACTIVE_INTERFACES] = { TCPIP_ADAPTER_IF_MAX };
Expand Down Expand Up @@ -137,34 +138,8 @@ network_result_t network_wait_for_connection(int32_t listeningSocket, int32_t* c
// error
return E_NETWORK_RESULT_FAILED;
}

if (ip_addr) {
// check on which network interface the client was connected and save the IP address
tcpip_adapter_ip_info_t ip_info = { 0 };
int n_if = network_get_active_interfaces();

if (n_if > 0) {
struct sockaddr_in clientAddr;
in_addrSize = sizeof(struct sockaddr_in);
getpeername(_sd, (struct sockaddr*)&clientAddr, (socklen_t*)&in_addrSize);
ESP_LOGI(NETWORK_TAG, "Client IP: %08x", clientAddr.sin_addr.s_addr);
*ip_addr = 0;
for (int i = 0; i < n_if; i++) {
tcpip_adapter_get_ip_info(tcpip_if[i], &ip_info);
ESP_LOGI(NETWORK_TAG, "Adapter: %08x, %08x", ip_info.ip.addr, ip_info.netmask.addr);
if ((ip_info.ip.addr & ip_info.netmask.addr) == (ip_info.netmask.addr & clientAddr.sin_addr.s_addr)) {
*ip_addr = ip_info.ip.addr;
ESP_LOGI(NETWORK_TAG, "Client connected on interface %d", tcpip_if[i]);
break;
}
}
if (*ip_addr == 0) {
ESP_LOGE(NETWORK_TAG, "No IP address detected (?!)");
}
}
else {
ESP_LOGE(NETWORK_TAG, "No active interface (?!)");
}
*ip_addr = WiFi.localIP();
}

// enable non-blocking mode if not data channel connection
Expand Down
2 changes: 1 addition & 1 deletion src/WifiCachingStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ void WifiCachingStream::flush()

size_t WifiCachingStream::readBytes(char* buffer, size_t length)
{
int received = 0;
size_t received = 0;
auto result = network_recv_non_blocking(_connection_sd, buffer, (int)length, &received);
if (received >= 1)
{
Expand Down
6 changes: 3 additions & 3 deletions src/WifiCachingStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ class WifiCachingStream : public Stream
{
private:
static constexpr int SendBufferSize = 500;
int _sd;
int _port;
int32_t _sd;
int32_t _port;

int _connection_sd;
int32_t _connection_sd;

byte _sendBuffer[SendBufferSize];
int _sendBufferIndex;
Expand Down

0 comments on commit 0bc689d

Please sign in to comment.