Skip to content
This repository was archived by the owner on Apr 28, 2026. It is now read-only.

Commit 860fd33

Browse files
committed
fix(udp): start client connection only once
Trying to start a client connection on a sock already used failed and prevent to write other data. endPacket() allows to close a client connection properly if needed. Fixes #14 Signed-off-by: Frederic Pillon <frederic.pillon@st.com>
1 parent 5fbff53 commit 860fd33

10 files changed

Lines changed: 153 additions & 149 deletions

File tree

README.md

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,36 @@ List of the adaptations:
1010

1111
### WiFiST
1212
* Class constructor changed, 3 new constructors depend on the communication driver:
13-
**WiFiClass**(SPIClass \*SPIx, uint8_t cs, uint8_t spiIRQ, uint8_t reset, uint8_t wakeup);
14-
**WiFiClass**(HardwareSerial \*UARTx, uint8_t reset, uint8_t wakeup);
15-
**WiFiClass**(uint8_t tx, uint8_t rx, uint8_t reset, uint8_t wakeup);
16-
* **status()**: less verbose; only WL_CONNECTED, WL_NO_SHIELD, WL_IDLE_STATUS, WL_NO_SSID_AVAIL, WL_SCAN_COMPLETED, WL_CONNECT_FAILED implemented.
17-
* **setMac()**: function added to set MAC address of the Wifi module.
13+
`WiFiClass(SPIClass \*SPIx, uint8_t cs, uint8_t spiIRQ, uint8_t reset, uint8_t wakeup);`
14+
`WiFiClass(HardwareSerial \*UARTx, uint8_t reset, uint8_t wakeup);`
15+
`WiFiClass(uint8_t tx, uint8_t rx, uint8_t reset, uint8_t wakeup);`
16+
* `status()`: less verbose; only `WL_CONNECTED`, `WL_NO_SHIELD`, `WL_IDLE_STATUS`, `WL_NO_SSID_AVAIL`, `WL_SCAN_COMPLETED`, `WL_CONNECT_FAILED` implemented.
17+
* `setMac()`: function added to set MAC address of the Wifi module.
1818

1919
### WiFiClientST :
20-
* **available()**: not supported. Always returns 1.
21-
* **peek()**: not supported. Always returns 0.
22-
* **flush()**: empty function. Do nothing (already empty in Arduino Wifi API).
20+
* `available()`: not supported. Always returns 1.
21+
* `peek()`: not supported. Always returns 0.
22+
* `flush()`: empty function. Do nothing (already empty in Arduino Wifi API).
2323

2424
### WiFiUdpST:
25-
* **endPacket()**: not supported. Always returns 1. The data are sent when you call write().
26-
* **available()**: not supported. Always returns 0.
27-
* **parsePAcket()**: not supported. Always returns 0.
28-
* **peek()**: not supported. Always returns 0.
29-
* **flush()**: Do nothing (already empty in Arduino Wifi API).
25+
* `endPacket()`:The data are sent when you call `write()`. By default, do nothing and always return 1. If `true` is passed as argument the client connection started by `beginPacket()` is closed.
26+
* `available()`: not supported. Always returns 0.
27+
* `parsePAcket()`: not supported. Always returns 0.
28+
* `peek()`: not supported. Always returns 0.
29+
* `flush()`: Do nothing (already empty in Arduino Wifi API).
3030

3131
### WiFiServerST:
32-
* **status()**: Do nothing (always returns 1).
32+
* `status()`: Do nothing (always returns 1).
3333

3434
## Version
3535

36-
The WiFi library is based on FW "Inventek eS-WiFi ISM43362-M3G-L44-SPI C3.5.2.5.STM".
36+
> [!IMPORTANT]
37+
> The WiFi library is based on FW "Inventek eS-WiFi ISM43362-M3G-L44-SPI C3.5.2.5.STM".
38+
39+
> [!CAUTION]
40+
> * WiFiServerST is not stable due to issue of the current WiFi firmware version: C3.5.2.5.STM
41+
> * WEP-128 is not functional. Issue probably due to the current WiFi firmware version: C3.5.2.5.STM
42+
> * UDP server is not functional with the current WiFi firmware version: C3.5.2.5.STM while it was with FW version C3.5.2.3.BETA9 (#12)
3743
3844
To update the Inventek ISM-43362 Wi-Fi module firmware, please read the readme file for instructions
3945
include in this archive:
@@ -44,10 +50,6 @@ https://www.st.com/resource/en/utilities/inventek_fw_updater.zip
4450
* WPA-PSK (TKIP)
4551
* WPA2-PSK
4652

47-
## Restriction
48-
* WiFiServerST is not stable due to issue of the current WiFi firmware version: C3.5.2.5.STM
49-
* WEP-128 is not functional. Issue probably due to the current WiFi firmware version: C3.5.2.5.STM
50-
5153
## Examples
5254

5355
The examples are close of the Arduino WiFi library but with some adaptations to work
@@ -59,4 +61,4 @@ You can find the source files at
5961
https://github.com/stm32duino/WiFi-ISM43362-M3G-L44
6062

6163
The ISM43362-M3G-L44 datasheet is available at
62-
http://www.inventeksys.com/products-page/wifi-modules/serial-wifi/ism4336-m3g-l44-e-embedded-serial-to-wifi-module/
64+
https://www.inventeksys.com/ism4336-m3g-l44-e-embedded-serial-to-wifi-module/

examples/WiFiUdpNtpClient/WiFiUdpNtpClient.ino

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ const int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes
4848

4949
byte packetBuffer[ NTP_PACKET_SIZE]; // buffer to hold incoming and outgoing packets
5050

51-
bool ConnectionCreate = false;
52-
5351
// An UDP instance to let us send and receive packets over UDP
5452
WiFiUDP Udp;
5553

@@ -162,13 +160,12 @@ void sendNTPpacket(IPAddress& address) {
162160
packetBuffer[15] = 52;
163161

164162
// all NTP fields have been given values, now
165-
// you can send a packet requesting a timestamp:
166-
if (!ConnectionCreate) {
167-
// NTP requests are sent to port 123
168-
Udp.beginPacket(address, 123);
169-
ConnectionCreate = true;
163+
// a packet requesting a timestamp can be sent:
164+
// NTP requests are sent to port 123
165+
if (Udp.beginPacket(address, 123) == 1) {
166+
Udp.write(packetBuffer, NTP_PACKET_SIZE);
167+
Udp.endPacket();
170168
}
171-
Udp.write(packetBuffer, NTP_PACKET_SIZE);
172169
}
173170

174171
void printWifiStatus() {

src/ISM43362_M3G_L44_driver.cpp

Lines changed: 64 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
#include "ISM43362_M3G_L44_driver.h"
4949

5050
#if (ES_WIFI_PAYLOAD_SIZE + AT_ERROR_STRING_LEN) > ES_WIFI_DATA_SIZE
51-
#warning "ES_WIFI_PAYLOAD_SIZE is higer than ES_WIFI_DATA_SIZE this could cause overflow!"
51+
#warning "ES_WIFI_PAYLOAD_SIZE is higer than ES_WIFI_DATA_SIZE this could cause overflow!"
5252
#endif
5353

5454
_Static_assert((ES_WIFI_DATA_SIZE & 1) == 0, "ES_WIFI_DATA_SIZE have to be even!");
@@ -1347,12 +1347,11 @@ void IsmDrvClass::ES_WIFI_DNS_LookUp(const char *url, IPAddress *ipaddress)
13471347
/**
13481348
* @brief Configure and Start a Client connection.
13491349
* @param index : index of structure connection
1350-
* @retval None.
1350+
* @retval boolean, true if connection is established.
13511351
*/
1352-
void IsmDrvClass::ES_WIFI_StartClientConnection(uint8_t index)
1352+
bool IsmDrvClass::ES_WIFI_StartClientConnection(uint8_t index)
13531353
{
13541354
ES_WIFI_Status_t ret;
1355-
13561355
currentSock = index;
13571356
sockState[index] = SOCKET_BUSY;
13581357
sprintf((char *)EsWifiObj.CmdData, "%s=%d%s",
@@ -1392,14 +1391,15 @@ void IsmDrvClass::ES_WIFI_StartClientConnection(uint8_t index)
13921391
}
13931392
}
13941393
}
1394+
return (ret == ES_WIFI_STATUS_OK);
13951395
}
13961396

13971397
/**
13981398
* @brief Stop Client connection.
13991399
* @param index : index of structure connection
1400-
* @retval None.
1400+
* @retval boolean, true if success, false otherwise
14011401
*/
1402-
void IsmDrvClass::ES_WIFI_StopClientConnection(uint8_t index)
1402+
bool IsmDrvClass::ES_WIFI_StopClientConnection(uint8_t index)
14031403
{
14041404
ES_WIFI_Status_t ret;
14051405

@@ -1414,6 +1414,7 @@ void IsmDrvClass::ES_WIFI_StopClientConnection(uint8_t index)
14141414
AT_TR_CLIENT, SUFFIX_CMD);
14151415
ret = AT_ExecuteCommand();
14161416
}
1417+
return (ret == ES_WIFI_STATUS_OK);
14171418
}
14181419

14191420
/**
@@ -1427,69 +1428,67 @@ void IsmDrvClass::ES_WIFI_StartServerSingleConn(uint8_t index, comm_mode mode)
14271428
ES_WIFI_Status_t ret = ES_WIFI_STATUS_ERROR;
14281429
char *ptr;
14291430

1430-
if (index > MAX_SOCK_NUM) {
1431-
return;
1432-
}
1433-
1434-
sprintf((char *)EsWifiObj.CmdData, "%s=1,3000%s",
1435-
AT_TR_TCP_KEEP_ALIVE, SUFFIX_CMD);
1436-
ret = AT_ExecuteCommand();
1437-
if (ret == ES_WIFI_STATUS_OK) {
1438-
currentSock = index;
1439-
sockState[currentSock] = SOCKET_BUSY;
1440-
sprintf((char *)EsWifiObj.CmdData, "%s=%d%s",
1441-
AT_TR_SET_SOCKET, ESWifiConnTab[index].Number, SUFFIX_CMD);
1431+
if (index <= MAX_SOCK_NUM) {
1432+
sprintf((char *)EsWifiObj.CmdData, "%s=1,3000%s",
1433+
AT_TR_TCP_KEEP_ALIVE, SUFFIX_CMD);
14421434
ret = AT_ExecuteCommand();
14431435
if (ret == ES_WIFI_STATUS_OK) {
1436+
currentSock = index;
1437+
sockState[currentSock] = SOCKET_BUSY;
14441438
sprintf((char *)EsWifiObj.CmdData, "%s=%d%s",
1445-
AT_TR_SET_PROTOCOL, ESWifiConnTab[index].Type, SUFFIX_CMD);
1439+
AT_TR_SET_SOCKET, ESWifiConnTab[index].Number, SUFFIX_CMD);
14461440
ret = AT_ExecuteCommand();
14471441
if (ret == ES_WIFI_STATUS_OK) {
14481442
sprintf((char *)EsWifiObj.CmdData, "%s=%d%s",
1449-
AT_TR_SET_LOCAL_PORT_NUMBER, ESWifiConnTab[index].LocalPort, SUFFIX_CMD);
1443+
AT_TR_SET_PROTOCOL, ESWifiConnTab[index].Type, SUFFIX_CMD);
14501444
ret = AT_ExecuteCommand();
14511445
if (ret == ES_WIFI_STATUS_OK) {
1452-
sprintf((char *)EsWifiObj.CmdData, "%s=1%s",
1453-
AT_TR_SERVER, SUFFIX_CMD);
1446+
sprintf((char *)EsWifiObj.CmdData, "%s=%d%s",
1447+
AT_TR_SET_LOCAL_PORT_NUMBER, ESWifiConnTab[index].LocalPort, SUFFIX_CMD);
14541448
ret = AT_ExecuteCommand();
1455-
14561449
if (ret == ES_WIFI_STATUS_OK) {
1457-
if (mode == COMM_UART) {
1458-
if (Drv->IO_Receive(EsWifiObj.CmdData, 0, EsWifiObj.Timeout) > 0) {
1459-
if (strstr((char *)EsWifiObj.CmdData, "Accepted")) {
1460-
ptr = strtok((char *)EsWifiObj.CmdData + 2, " ");
1461-
ptr = strtok(NULL, " ");
1462-
ptr = strtok(NULL, " ");
1463-
ptr = strtok(NULL, ":");
1464-
ParseIP((char *)ptr, ESWifiConnTab[index].RemoteIP);
1465-
ret = ES_WIFI_STATUS_OK;
1450+
sprintf((char *)EsWifiObj.CmdData, "%s=1%s",
1451+
AT_TR_SERVER, SUFFIX_CMD);
1452+
ret = AT_ExecuteCommand();
1453+
1454+
if (ret == ES_WIFI_STATUS_OK) {
1455+
if (mode == COMM_UART) {
1456+
if (Drv->IO_Receive(EsWifiObj.CmdData, 0, EsWifiObj.Timeout) > 0) {
1457+
if (strstr((char *)EsWifiObj.CmdData, "Accepted")) {
1458+
ptr = strtok((char *)EsWifiObj.CmdData + 2, " ");
1459+
ptr = strtok(NULL, " ");
1460+
ptr = strtok(NULL, " ");
1461+
ptr = strtok(NULL, ":");
1462+
ParseIP((char *)ptr, ESWifiConnTab[index].RemoteIP);
1463+
ret = ES_WIFI_STATUS_OK;
1464+
}
14661465
}
1467-
}
1468-
} else if (mode == COMM_SPI) {
1469-
do {
1470-
strcpy((char *)EsWifiObj.CmdData, AT_MESSAGE_READ);
1471-
strcat((char *)EsWifiObj.CmdData, SUFFIX_CMD);
1472-
ret = AT_ExecuteCommand();
1473-
if (ret == ES_WIFI_STATUS_OK) {
1474-
if ((strstr((char *)EsWifiObj.CmdData, "[SOMA]")) && (strstr((char *)EsWifiObj.CmdData, "[EOMA]"))) {
1475-
if (strstr((char *)EsWifiObj.CmdData, "Accepted")) {
1476-
ptr = strtok((char *)EsWifiObj.CmdData + 2, " ");
1477-
ptr = strtok(NULL, " ");
1478-
ptr = strtok(NULL, " ");
1479-
ptr = strtok(NULL, ":");
1480-
ParseIP((char *)ptr, ESWifiConnTab[index].RemoteIP);
1481-
ret = ES_WIFI_STATUS_OK;
1482-
break;
1466+
} else if (mode == COMM_SPI) {
1467+
do {
1468+
strcpy((char *)EsWifiObj.CmdData, AT_MESSAGE_READ);
1469+
strcat((char *)EsWifiObj.CmdData, SUFFIX_CMD);
1470+
ret = AT_ExecuteCommand();
1471+
if (ret == ES_WIFI_STATUS_OK) {
1472+
if ((strstr((char *)EsWifiObj.CmdData, "[SOMA]")) && (strstr((char *)EsWifiObj.CmdData, "[EOMA]"))) {
1473+
if (strstr((char *)EsWifiObj.CmdData, "Accepted")) {
1474+
ptr = strtok((char *)EsWifiObj.CmdData + 2, " ");
1475+
ptr = strtok(NULL, " ");
1476+
ptr = strtok(NULL, " ");
1477+
ptr = strtok(NULL, ":");
1478+
ParseIP((char *)ptr, ESWifiConnTab[index].RemoteIP);
1479+
ret = ES_WIFI_STATUS_OK;
1480+
break;
1481+
}
14831482
}
1483+
} else {
1484+
ret = ES_WIFI_STATUS_ERROR;
1485+
break;
14841486
}
1485-
} else {
1486-
ret = ES_WIFI_STATUS_ERROR;
1487-
break;
1488-
}
1489-
Drv->IO_Delay(1000);
1490-
} while (1);
1491-
} else {
1492-
ret = ES_WIFI_STATUS_ERROR;
1487+
Drv->IO_Delay(1000);
1488+
} while (1);
1489+
} else {
1490+
ret = ES_WIFI_STATUS_ERROR;
1491+
}
14931492
}
14941493
}
14951494
}
@@ -1743,41 +1742,30 @@ void IsmDrvClass::ES_WIFI_ReceiveData(uint8_t Socket, uint8_t *pdata,
17431742
}
17441743
}
17451744

1746-
/**
1747-
* @brief Set connection parameter in the struct
1748-
* @param Number : socket number
1749-
* @param Type : Type of connection (UDP, TCP)
1750-
* @param LocalPort : local port
1751-
* @retval None.
1752-
*/
1753-
void IsmDrvClass::ES_WIFI_SetConnectionParam(uint8_t Number, ES_WIFI_ConnType_t Type, uint16_t LocalPort)
1754-
{
1755-
if (Number < MAX_SOCK_NUM) {
1756-
ESWifiConnTab[Number].Number = Number;
1757-
ESWifiConnTab[Number].Type = Type;
1758-
ESWifiConnTab[Number].LocalPort = LocalPort;
1759-
}
1760-
}
1761-
17621745
/**
17631746
* @brief Set connection parameter in the struct
17641747
* @param Number : socket number
17651748
* @param Type : Type of connection (UDP, TCP)
17661749
* @param LocalPort : local port
17671750
* @param Ip : Remote IP address
1768-
* @retval None.
1751+
* @retval boolean, true if success, false otherwise
17691752
*/
1770-
void IsmDrvClass::ES_WIFI_SetConnectionParam(uint8_t Number, ES_WIFI_ConnType_t Type, uint16_t LocalPort, IPAddress Ip)
1753+
bool IsmDrvClass::ES_WIFI_SetConnectionParam(uint8_t Number, ES_WIFI_ConnType_t Type, uint16_t LocalPort, IPAddress Ip)
17711754
{
1755+
bool ret = false;
17721756
if (Number < MAX_SOCK_NUM) {
17731757
ESWifiConnTab[Number].Number = Number;
17741758
ESWifiConnTab[Number].Type = Type;
17751759
ESWifiConnTab[Number].RemotePort = LocalPort;
17761760
ESWifiConnTab[Number].LocalPort = LocalPort;
1777-
for (int i = 0; i < 4; i++) {
1778-
ESWifiConnTab[Number].RemoteIP[i] = Ip[i];
1761+
if (Ip != INADDR_NONE) {
1762+
for (int i = 0; i < 4; i++) {
1763+
ESWifiConnTab[Number].RemoteIP[i] = Ip[i];
1764+
}
17791765
}
1766+
ret = true;
17801767
}
1768+
return ret;
17811769
}
17821770

17831771
/**

src/ISM43362_M3G_L44_driver.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -371,16 +371,15 @@ class IsmDrvClass : public WiFiDrvClass {
371371
virtual void ES_WIFI_SetProductName(uint8_t *ProductName);
372372
virtual void ES_WIFI_Ping(uint8_t *address, uint16_t count, uint16_t interval_ms);
373373
virtual void ES_WIFI_DNS_LookUp(const char *url, IPAddress *ipaddress);
374-
virtual void ES_WIFI_StartClientConnection(uint8_t index);
375-
virtual void ES_WIFI_StopClientConnection(uint8_t index);
374+
virtual bool ES_WIFI_StartClientConnection(uint8_t index);
375+
virtual bool ES_WIFI_StopClientConnection(uint8_t index);
376376
virtual void ES_WIFI_StartServerSingleConn(uint8_t index, comm_mode mode);
377377
virtual void ES_WIFI_StopServerSingleConn(uint8_t index);
378378
virtual void ES_WIFI_StartServerMultiConn(uint8_t socket, comm_mode mode);
379379
virtual void ES_WIFI_StopServerMultiConn();
380380
virtual void ES_WIFI_ReceiveData(uint8_t Socket, uint8_t *pdata, uint16_t Reqlen, uint16_t *Receivedlen, uint32_t Timeout);
381381
virtual void ES_WIFI_getRemoteData(uint8_t sock, uint8_t *ip, uint16_t *port);
382-
virtual void ES_WIFI_SetConnectionParam(uint8_t Number, ES_WIFI_ConnType_t Type, uint16_t LocalPort);
383-
virtual void ES_WIFI_SetConnectionParam(uint8_t Number, ES_WIFI_ConnType_t Type, uint16_t LocalPort, IPAddress Ip);
382+
virtual bool ES_WIFI_SetConnectionParam(uint8_t Number, ES_WIFI_ConnType_t Type, uint16_t LocalPort, IPAddress Ip = INADDR_NONE);
384383
virtual void ES_WIFI_SendResp(uint8_t Socket, uint8_t *pdata, uint16_t Reqlen, uint16_t *SentLen, uint32_t Timeout);
385384
virtual uint8_t getCurrentSocket(void);
386385
virtual int8_t getFreeSocket(void);

src/WiFiClientST.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ WiFiClient::WiFiClient(uint8_t sock) : _sock(sock)
6262
int WiFiClient::connect(IPAddress ip, uint16_t port)
6363
{
6464
int8_t sock;
65+
int ret = 0;
6566
if (_sock == NO_SOCKET_AVAIL) {
6667
sock = DrvWiFi->getFreeSocket(); // get next free socket
6768
if (sock != -1) {
@@ -70,11 +71,13 @@ int WiFiClient::connect(IPAddress ip, uint16_t port)
7071
}
7172
if (_sock != NO_SOCKET_AVAIL) {
7273
// set connection parameter and start client
73-
DrvWiFi->ES_WIFI_SetConnectionParam(_sock, ES_WIFI_TCP_CONNECTION, port, ip);
74-
DrvWiFi->ES_WIFI_StartClientConnection(_sock);
75-
return 1;
74+
if (DrvWiFi->ES_WIFI_SetConnectionParam(_sock, ES_WIFI_TCP_CONNECTION, port, ip)) {
75+
if (DrvWiFi->ES_WIFI_StartClientConnection(_sock)) {
76+
ret = 1;
77+
}
78+
}
7679
}
77-
return 0;
80+
return ret;
7881
}
7982

8083
/**

src/WiFiServerST.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,9 @@ void WiFiServer::begin()
5959
int8_t sock;
6060
sock = DrvWiFi->getFreeSocket();
6161
if (sock != -1) {
62-
DrvWiFi->ES_WIFI_SetConnectionParam(sock, ES_WIFI_TCP_CONNECTION, _port);
63-
DrvWiFi->ES_WIFI_StartServerSingleConn(sock, COMM_SPI);
62+
if (DrvWiFi->ES_WIFI_SetConnectionParam(sock, ES_WIFI_TCP_CONNECTION, _port)) {
63+
DrvWiFi->ES_WIFI_StartServerSingleConn(sock, COMM_SPI);
64+
}
6465
}
6566
}
6667

@@ -79,9 +80,10 @@ WiFiClient WiFiServer::available(byte *status)
7980

8081
//server not in listen state, restart it
8182
if (cycle_server_down++ > TH_SERVER_DOWN) {
82-
DrvWiFi->ES_WIFI_SetConnectionParam(sock, ES_WIFI_TCP_CONNECTION, _port);
83-
DrvWiFi->ES_WIFI_StartServerSingleConn(sock, COMM_SPI);
84-
cycle_server_down = 0;
83+
if (DrvWiFi->ES_WIFI_SetConnectionParam(sock, ES_WIFI_TCP_CONNECTION, _port)) {
84+
DrvWiFi->ES_WIFI_StartServerSingleConn(sock, COMM_SPI);
85+
cycle_server_down = 0;
86+
}
8587
}
8688

8789
if (_status == SOCKET_BUSY) {

0 commit comments

Comments
 (0)