Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(udp): start client connection only once #18

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 22 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,36 @@ List of the adaptations:

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

### WiFiClientST :
* **available()**: not supported. Always returns 1.
* **peek()**: not supported. Always returns 0.
* **flush()**: empty function. Do nothing (already empty in Arduino Wifi API).
* `available()`: not supported. Always returns 1.
* `peek()`: not supported. Always returns 0.
* `flush()`: empty function. Do nothing (already empty in Arduino Wifi API).

### WiFiUdpST:
* **endPacket()**: not supported. Always returns 1. The data are sent when you call write().
* **available()**: not supported. Always returns 0.
* **parsePAcket()**: not supported. Always returns 0.
* **peek()**: not supported. Always returns 0.
* **flush()**: Do nothing (already empty in Arduino Wifi API).
* `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.
* `available()`: not supported. Always returns 0.
* `parsePAcket()`: not supported. Always returns 0.
* `peek()`: not supported. Always returns 0.
* `flush()`: Do nothing (already empty in Arduino Wifi API).

### WiFiServerST:
* **status()**: Do nothing (always returns 1).
* `status()`: Do nothing (always returns 1).

## Version

The WiFi library is based on FW "Inventek eS-WiFi ISM43362-M3G-L44-SPI C3.5.2.5.STM".
> [!IMPORTANT]
> The WiFi library is based on FW "Inventek eS-WiFi ISM43362-M3G-L44-SPI C3.5.2.5.STM".

> [!CAUTION]
> * WiFiServerST is not stable due to issue of the current WiFi firmware version: C3.5.2.5.STM
> * WEP-128 is not functional. Issue probably due to the current WiFi firmware version: C3.5.2.5.STM
> * 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)

To update the Inventek ISM-43362 Wi-Fi module firmware, please read the readme file for instructions
include in this archive:
Expand All @@ -44,10 +50,6 @@ https://www.st.com/resource/en/utilities/inventek_fw_updater.zip
* WPA-PSK (TKIP)
* WPA2-PSK

## Restriction
* WiFiServerST is not stable due to issue of the current WiFi firmware version: C3.5.2.5.STM
* WEP-128 is not functional. Issue probably due to the current WiFi firmware version: C3.5.2.5.STM

## Examples

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

The ISM43362-M3G-L44 datasheet is available at
http://www.inventeksys.com/products-page/wifi-modules/serial-wifi/ism4336-m3g-l44-e-embedded-serial-to-wifi-module/
https://www.inventeksys.com/ism4336-m3g-l44-e-embedded-serial-to-wifi-module/
13 changes: 5 additions & 8 deletions examples/WiFiUdpNtpClient/WiFiUdpNtpClient.ino
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ const int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes

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

bool ConnectionCreate = false;

// An UDP instance to let us send and receive packets over UDP
WiFiUDP Udp;

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

// all NTP fields have been given values, now
// you can send a packet requesting a timestamp:
if (!ConnectionCreate) {
// NTP requests are sent to port 123
Udp.beginPacket(address, 123);
ConnectionCreate = true;
// a packet requesting a timestamp can be sent:
// NTP requests are sent to port 123
if (Udp.beginPacket(address, 123) == 1) {
Udp.write(packetBuffer, NTP_PACKET_SIZE);
Udp.endPacket();
}
Udp.write(packetBuffer, NTP_PACKET_SIZE);
}

void printWifiStatus() {
Expand Down
140 changes: 64 additions & 76 deletions src/ISM43362_M3G_L44_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
#include "ISM43362_M3G_L44_driver.h"

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

_Static_assert((ES_WIFI_DATA_SIZE & 1) == 0, "ES_WIFI_DATA_SIZE have to be even!");
Expand Down Expand Up @@ -1347,12 +1347,11 @@ void IsmDrvClass::ES_WIFI_DNS_LookUp(const char *url, IPAddress *ipaddress)
/**
* @brief Configure and Start a Client connection.
* @param index : index of structure connection
* @retval None.
* @retval boolean, true if connection is established.
*/
void IsmDrvClass::ES_WIFI_StartClientConnection(uint8_t index)
bool IsmDrvClass::ES_WIFI_StartClientConnection(uint8_t index)
{
ES_WIFI_Status_t ret;

currentSock = index;
sockState[index] = SOCKET_BUSY;
sprintf((char *)EsWifiObj.CmdData, "%s=%d%s",
Expand Down Expand Up @@ -1392,14 +1391,15 @@ void IsmDrvClass::ES_WIFI_StartClientConnection(uint8_t index)
}
}
}
return (ret == ES_WIFI_STATUS_OK);
}

/**
* @brief Stop Client connection.
* @param index : index of structure connection
* @retval None.
* @retval boolean, true if success, false otherwise
*/
void IsmDrvClass::ES_WIFI_StopClientConnection(uint8_t index)
bool IsmDrvClass::ES_WIFI_StopClientConnection(uint8_t index)
{
ES_WIFI_Status_t ret;

Expand All @@ -1414,6 +1414,7 @@ void IsmDrvClass::ES_WIFI_StopClientConnection(uint8_t index)
AT_TR_CLIENT, SUFFIX_CMD);
ret = AT_ExecuteCommand();
}
return (ret == ES_WIFI_STATUS_OK);
}

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

if (index > MAX_SOCK_NUM) {
return;
}

sprintf((char *)EsWifiObj.CmdData, "%s=1,3000%s",
AT_TR_TCP_KEEP_ALIVE, SUFFIX_CMD);
ret = AT_ExecuteCommand();
if (ret == ES_WIFI_STATUS_OK) {
currentSock = index;
sockState[currentSock] = SOCKET_BUSY;
sprintf((char *)EsWifiObj.CmdData, "%s=%d%s",
AT_TR_SET_SOCKET, ESWifiConnTab[index].Number, SUFFIX_CMD);
if (index <= MAX_SOCK_NUM) {
sprintf((char *)EsWifiObj.CmdData, "%s=1,3000%s",
AT_TR_TCP_KEEP_ALIVE, SUFFIX_CMD);
ret = AT_ExecuteCommand();
if (ret == ES_WIFI_STATUS_OK) {
currentSock = index;
sockState[currentSock] = SOCKET_BUSY;
sprintf((char *)EsWifiObj.CmdData, "%s=%d%s",
AT_TR_SET_PROTOCOL, ESWifiConnTab[index].Type, SUFFIX_CMD);
AT_TR_SET_SOCKET, ESWifiConnTab[index].Number, SUFFIX_CMD);
ret = AT_ExecuteCommand();
if (ret == ES_WIFI_STATUS_OK) {
sprintf((char *)EsWifiObj.CmdData, "%s=%d%s",
AT_TR_SET_LOCAL_PORT_NUMBER, ESWifiConnTab[index].LocalPort, SUFFIX_CMD);
AT_TR_SET_PROTOCOL, ESWifiConnTab[index].Type, SUFFIX_CMD);
ret = AT_ExecuteCommand();
if (ret == ES_WIFI_STATUS_OK) {
sprintf((char *)EsWifiObj.CmdData, "%s=1%s",
AT_TR_SERVER, SUFFIX_CMD);
sprintf((char *)EsWifiObj.CmdData, "%s=%d%s",
AT_TR_SET_LOCAL_PORT_NUMBER, ESWifiConnTab[index].LocalPort, SUFFIX_CMD);
ret = AT_ExecuteCommand();

if (ret == ES_WIFI_STATUS_OK) {
if (mode == COMM_UART) {
if (Drv->IO_Receive(EsWifiObj.CmdData, 0, EsWifiObj.Timeout) > 0) {
if (strstr((char *)EsWifiObj.CmdData, "Accepted")) {
ptr = strtok((char *)EsWifiObj.CmdData + 2, " ");
ptr = strtok(NULL, " ");
ptr = strtok(NULL, " ");
ptr = strtok(NULL, ":");
ParseIP((char *)ptr, ESWifiConnTab[index].RemoteIP);
ret = ES_WIFI_STATUS_OK;
sprintf((char *)EsWifiObj.CmdData, "%s=1%s",
AT_TR_SERVER, SUFFIX_CMD);
ret = AT_ExecuteCommand();

if (ret == ES_WIFI_STATUS_OK) {
if (mode == COMM_UART) {
if (Drv->IO_Receive(EsWifiObj.CmdData, 0, EsWifiObj.Timeout) > 0) {
if (strstr((char *)EsWifiObj.CmdData, "Accepted")) {
ptr = strtok((char *)EsWifiObj.CmdData + 2, " ");
ptr = strtok(NULL, " ");
ptr = strtok(NULL, " ");
ptr = strtok(NULL, ":");
ParseIP((char *)ptr, ESWifiConnTab[index].RemoteIP);
ret = ES_WIFI_STATUS_OK;
}
}
}
} else if (mode == COMM_SPI) {
do {
strcpy((char *)EsWifiObj.CmdData, AT_MESSAGE_READ);
strcat((char *)EsWifiObj.CmdData, SUFFIX_CMD);
ret = AT_ExecuteCommand();
if (ret == ES_WIFI_STATUS_OK) {
if ((strstr((char *)EsWifiObj.CmdData, "[SOMA]")) && (strstr((char *)EsWifiObj.CmdData, "[EOMA]"))) {
if (strstr((char *)EsWifiObj.CmdData, "Accepted")) {
ptr = strtok((char *)EsWifiObj.CmdData + 2, " ");
ptr = strtok(NULL, " ");
ptr = strtok(NULL, " ");
ptr = strtok(NULL, ":");
ParseIP((char *)ptr, ESWifiConnTab[index].RemoteIP);
ret = ES_WIFI_STATUS_OK;
break;
} else if (mode == COMM_SPI) {
do {
strcpy((char *)EsWifiObj.CmdData, AT_MESSAGE_READ);
strcat((char *)EsWifiObj.CmdData, SUFFIX_CMD);
ret = AT_ExecuteCommand();
if (ret == ES_WIFI_STATUS_OK) {
if ((strstr((char *)EsWifiObj.CmdData, "[SOMA]")) && (strstr((char *)EsWifiObj.CmdData, "[EOMA]"))) {
if (strstr((char *)EsWifiObj.CmdData, "Accepted")) {
ptr = strtok((char *)EsWifiObj.CmdData + 2, " ");
ptr = strtok(NULL, " ");
ptr = strtok(NULL, " ");
ptr = strtok(NULL, ":");
ParseIP((char *)ptr, ESWifiConnTab[index].RemoteIP);
ret = ES_WIFI_STATUS_OK;
break;
}
}
} else {
ret = ES_WIFI_STATUS_ERROR;
break;
}
} else {
ret = ES_WIFI_STATUS_ERROR;
break;
}
Drv->IO_Delay(1000);
} while (1);
} else {
ret = ES_WIFI_STATUS_ERROR;
Drv->IO_Delay(1000);
} while (1);
} else {
ret = ES_WIFI_STATUS_ERROR;
}
}
}
}
Expand Down Expand Up @@ -1743,41 +1742,30 @@ void IsmDrvClass::ES_WIFI_ReceiveData(uint8_t Socket, uint8_t *pdata,
}
}

/**
* @brief Set connection parameter in the struct
* @param Number : socket number
* @param Type : Type of connection (UDP, TCP)
* @param LocalPort : local port
* @retval None.
*/
void IsmDrvClass::ES_WIFI_SetConnectionParam(uint8_t Number, ES_WIFI_ConnType_t Type, uint16_t LocalPort)
{
if (Number < MAX_SOCK_NUM) {
ESWifiConnTab[Number].Number = Number;
ESWifiConnTab[Number].Type = Type;
ESWifiConnTab[Number].LocalPort = LocalPort;
}
}

/**
* @brief Set connection parameter in the struct
* @param Number : socket number
* @param Type : Type of connection (UDP, TCP)
* @param LocalPort : local port
* @param Ip : Remote IP address
* @retval None.
* @retval boolean, true if success, false otherwise
*/
void IsmDrvClass::ES_WIFI_SetConnectionParam(uint8_t Number, ES_WIFI_ConnType_t Type, uint16_t LocalPort, IPAddress Ip)
bool IsmDrvClass::ES_WIFI_SetConnectionParam(uint8_t Number, ES_WIFI_ConnType_t Type, uint16_t LocalPort, IPAddress Ip)
{
bool ret = false;
if (Number < MAX_SOCK_NUM) {
ESWifiConnTab[Number].Number = Number;
ESWifiConnTab[Number].Type = Type;
ESWifiConnTab[Number].RemotePort = LocalPort;
ESWifiConnTab[Number].LocalPort = LocalPort;
for (int i = 0; i < 4; i++) {
ESWifiConnTab[Number].RemoteIP[i] = Ip[i];
if (Ip != INADDR_NONE) {
for (int i = 0; i < 4; i++) {
ESWifiConnTab[Number].RemoteIP[i] = Ip[i];
}
}
ret = true;
}
return ret;
}

/**
Expand Down
7 changes: 3 additions & 4 deletions src/ISM43362_M3G_L44_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -371,16 +371,15 @@ class IsmDrvClass : public WiFiDrvClass {
virtual void ES_WIFI_SetProductName(uint8_t *ProductName);
virtual void ES_WIFI_Ping(uint8_t *address, uint16_t count, uint16_t interval_ms);
virtual void ES_WIFI_DNS_LookUp(const char *url, IPAddress *ipaddress);
virtual void ES_WIFI_StartClientConnection(uint8_t index);
virtual void ES_WIFI_StopClientConnection(uint8_t index);
virtual bool ES_WIFI_StartClientConnection(uint8_t index);
virtual bool ES_WIFI_StopClientConnection(uint8_t index);
virtual void ES_WIFI_StartServerSingleConn(uint8_t index, comm_mode mode);
virtual void ES_WIFI_StopServerSingleConn(uint8_t index);
virtual void ES_WIFI_StartServerMultiConn(uint8_t socket, comm_mode mode);
virtual void ES_WIFI_StopServerMultiConn();
virtual void ES_WIFI_ReceiveData(uint8_t Socket, uint8_t *pdata, uint16_t Reqlen, uint16_t *Receivedlen, uint32_t Timeout);
virtual void ES_WIFI_getRemoteData(uint8_t sock, uint8_t *ip, uint16_t *port);
virtual void ES_WIFI_SetConnectionParam(uint8_t Number, ES_WIFI_ConnType_t Type, uint16_t LocalPort);
virtual void ES_WIFI_SetConnectionParam(uint8_t Number, ES_WIFI_ConnType_t Type, uint16_t LocalPort, IPAddress Ip);
virtual bool ES_WIFI_SetConnectionParam(uint8_t Number, ES_WIFI_ConnType_t Type, uint16_t LocalPort, IPAddress Ip = INADDR_NONE);
virtual void ES_WIFI_SendResp(uint8_t Socket, uint8_t *pdata, uint16_t Reqlen, uint16_t *SentLen, uint32_t Timeout);
virtual uint8_t getCurrentSocket(void);
virtual int8_t getFreeSocket(void);
Expand Down
11 changes: 7 additions & 4 deletions src/WiFiClientST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ WiFiClient::WiFiClient(uint8_t sock) : _sock(sock)
int WiFiClient::connect(IPAddress ip, uint16_t port)
{
int8_t sock;
int ret = 0;
if (_sock == NO_SOCKET_AVAIL) {
sock = DrvWiFi->getFreeSocket(); // get next free socket
if (sock != -1) {
Expand All @@ -70,11 +71,13 @@ int WiFiClient::connect(IPAddress ip, uint16_t port)
}
if (_sock != NO_SOCKET_AVAIL) {
// set connection parameter and start client
DrvWiFi->ES_WIFI_SetConnectionParam(_sock, ES_WIFI_TCP_CONNECTION, port, ip);
DrvWiFi->ES_WIFI_StartClientConnection(_sock);
return 1;
if (DrvWiFi->ES_WIFI_SetConnectionParam(_sock, ES_WIFI_TCP_CONNECTION, port, ip)) {
if (DrvWiFi->ES_WIFI_StartClientConnection(_sock)) {
ret = 1;
}
}
}
return 0;
return ret;
}

/**
Expand Down
12 changes: 7 additions & 5 deletions src/WiFiServerST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ void WiFiServer::begin()
int8_t sock;
sock = DrvWiFi->getFreeSocket();
if (sock != -1) {
DrvWiFi->ES_WIFI_SetConnectionParam(sock, ES_WIFI_TCP_CONNECTION, _port);
DrvWiFi->ES_WIFI_StartServerSingleConn(sock, COMM_SPI);
if (DrvWiFi->ES_WIFI_SetConnectionParam(sock, ES_WIFI_TCP_CONNECTION, _port)) {
DrvWiFi->ES_WIFI_StartServerSingleConn(sock, COMM_SPI);
}
}
}

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

//server not in listen state, restart it
if (cycle_server_down++ > TH_SERVER_DOWN) {
DrvWiFi->ES_WIFI_SetConnectionParam(sock, ES_WIFI_TCP_CONNECTION, _port);
DrvWiFi->ES_WIFI_StartServerSingleConn(sock, COMM_SPI);
cycle_server_down = 0;
if (DrvWiFi->ES_WIFI_SetConnectionParam(sock, ES_WIFI_TCP_CONNECTION, _port)) {
DrvWiFi->ES_WIFI_StartServerSingleConn(sock, COMM_SPI);
cycle_server_down = 0;
}
}

if (_status == SOCKET_BUSY) {
Expand Down
Loading