Hi Kolban,
The message is not from my application code. The code basically sets up a listening port and retrieves all the data using netconn and netbufs.
However - If I set up the ESP32 as an AP and connect my PC to the ESP32 AP it all works fine. If I connect the ESP32 as a STA to my work network and my PC to my work network and then transfer the file - then the problem occurs.
Attached is the relevant part of the code . Its just a Proof of concept .
Cant seem to attach code says invalid extension. So Ill paste here.
static void DowloadFile(struct netconn *conn) {
struct netbuf *inbuf;
char *buf;
uint16_t buflen;
uint32_t TotalLen = 0;
err_t err;
char Myreply[10];
size_t Address = MY_FLASH_START;
esp_err_t SpiErr = 0;
struct MD5Context WifiMD5;
MD5Init(&WifiMD5);
netconn_set_recvbufsize(conn,1024);
/* Read the data from the port, blocking if nothing yet there.
We assume the request (the part we care about) is in one netbuf */
TRACE_D("About to read from socket\n");
if( (SpiErr = spi_flash_erase_range(MY_FLASH_START,SPI_FLASH_SEC_SIZE * 200)) == ESP_OK) //819.200 k
{
TRACE_D("819 K of flash erased starting at %lu \n",(unsigned long)MY_FLASH_START);
}
else
{
TRACE_D("Fatal error with flash erase \n");
}
printf("Receiving File \n" );
/*
http://lwip.wikia.com/wiki/Receiving_data_with_LWIP
* True that data buf returned is of varying lengths so it makes
* it tricky to always write same amount to flash - have to handle internally
*/
err = netconn_recv(conn, &inbuf); //block until receive first netbuf
if(err == ERR_OK)
{
netbuf_data(inbuf, (void**) &buf, &buflen);
MD5Update(&WifiMD5 , (unsigned char const*)buf , buflen);
if(/*(buflen % 4) == 0*/1) //new spi driver should handled unalligned
{
WriteBinFileToFlash(Address, (uint32_t*)buf, buflen);
}
else
{
TRACE_D("BUFFER NOT WRITTEN TO FLASH - BUFLEN NOT MULTIPLE OF 4 \n");
}
TotalLen+=buflen;
TRACE_D("received : %d \n",buflen);
if(inbuf)
{
netbuf_delete(inbuf);
}
netconn_set_recvtimeout ( conn, 1000);
while(1)
{
err = netconn_recv(conn, &inbuf);
if(err == ERR_OK)
{
netbuf_data(inbuf, (void**) &buf, &buflen);
MD5Update(&WifiMD5 , (unsigned char const*)buf , buflen);
if(/*(buflen % 4) == 0*/1)
{
WriteBinFileToFlash(Address+TotalLen, (uint32_t*)buf, buflen);
}
else
{
TRACE_D("BUFFER NOT WRITTEN TO FLASH - BUFLEN NOT MULTIPLE OF 4 \n");
}
TotalLen+=buflen;
TRACE_D("received : %d , Total : %lu \n",buflen,(unsigned long)TotalLen);
if(inbuf)
{
netbuf_delete(inbuf);
}
}
else if(err != ERR_OK)
{
TRACE_D("netconn returned code %d \n",err);
break;
}
}
}
else
{
TRACE_D("File Downlaod recieve err %d : \n",err);
}
MD5Final(MyDigest , &WifiMD5);
TRACE_D("Total Data Received : %lu \n",(unsigned long)TotalLen );
TRACE_D (Myreply,"%lu",(unsigned long)TotalLen);
printf("Total Data Received : %lu \n",(unsigned long)TotalLen );
for(uint8_t i=0;i<16;i++)
{
printf("WIFI MD5 %x \n",MyDigest
);
}
/* Close the connection (server closes in HTTP) */
// netconn_set_recvtimeout ( conn, 0);
netconn_close(conn);
/* Delete the buffer (netconn_recv gives us ownership,
so we have to make sure to deallocate the buffer) */
if(inbuf)
{
netbuf_delete(inbuf);
}
//calculate crc from flash
VerifyBinFileFromFlash(MY_FLASH_START , TotalLen , GetFileHash());
}
/************************************************************
@Func:
@Inputs:
@Outputs:
@Comments: FIXME - only works on first connection - n subsequent connection client fails to connect
*************************************************************/
/** The main function, never returns! */
static void socket_netconn_thread(void *arg) {
struct netconn *conn, *newconn;
err_t err;
uint16_t PortNumber = *((uint16_t*)arg);
/* Create a new TCP connection handle */
conn = netconn_new(NETCONN_TCP);
LWIP_ERROR("invalid conn", (conn != NULL), return;);
/* Bind to specified port with default IP address */
netconn_bind(conn, NULL, PortNumber);
/* Put the connection into LISTEN state */
netconn_listen(conn);
//netconn_set_recvtimeout ( conn, 10000);
TRACE_D("listening on %d for 10 seconds \n", PortNumber); //FIXME - currently permanently listening -
do {
err = netconn_accept(conn, &newconn);
if (err == ERR_OK)
{
TRACE_D("Connection accepted\n");
DowloadFile(newconn);
netconn_delete(newconn);
}
else
{
TRACE_D("Connection err %d : \n",err);
}
} while (err == ERR_OK);
netconn_close(conn);
netconn_delete(conn);
TRACE_D("deleting firmware upload task \n");
vTaskDelete(NULL);
}
/************************************************************
@Func:
@Inputs:
@Outputs:
@Comments:
*************************************************************/
void syrp_download_bin(uint16_t ListeningPort)
{
FirmwareDownloadPort = ListeningPort;
//spawn a temporary thread to recieve bin file
xTaskCreate(&socket_netconn_thread, "socket_netconn_thread", 2048,
&FirmwareDownloadPort, 5, NULL);
}