Skip to content

Commit a1e91ea

Browse files
committed
libraries/WiFi: Refactor begin function.
Signed-off-by: IFX-Anusha <[email protected]>
1 parent 9280b7f commit a1e91ea

File tree

3 files changed

+16
-137
lines changed

3 files changed

+16
-137
lines changed

libraries/WiFi/examples/WiFiUdpSendReceiveString/WiFiUdpSendReceiveString.ino

Lines changed: 0 additions & 113 deletions
This file was deleted.

libraries/WiFi/src/WiFiUdp.cpp

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,47 +18,38 @@ WiFiUDP::WiFiUDP() :
1818
_port(0) {
1919
}
2020

21-
uint8_t WiFiUDP::begin(uint16_t port) {
22-
// Initialize the socket for UDP
21+
uint8_t WiFiUDP::beginInternal(uint16_t port, IPAddress multicastIP) {
22+
2323
socket.begin(SOCKET_PROTOCOL_UDP);
2424
if (socket.status() != SOCKET_STATUS_CREATED) {
2525
return 0;
2626
}
2727

2828
socket.setReceiveOptCallback(receiveCallback, this);
2929

30-
// Bind the socket to the specified port
3130
socket.bind(port);
3231
if (socket.status() != SOCKET_STATUS_BOUND) {
3332
return 0;
34-
35-
}
36-
return socket.status(); // Return the socket status
37-
}
38-
39-
uint8_t WiFiUDP::beginMulticast(IPAddress ip, uint16_t port) {
40-
// Initialize the socket for UDP
41-
socket.begin(SOCKET_PROTOCOL_UDP);
42-
if (socket.status() != SOCKET_STATUS_CREATED) {
43-
return 0;
4433
}
4534

46-
socket.setReceiveOptCallback(receiveCallback, this);
47-
48-
// Bind the socket to the specified port
49-
socket.bind(port);
50-
if (socket.status() != SOCKET_STATUS_BOUND) {
51-
return 0;
35+
if (multicastIP != IPAddress(0, 0, 0, 0)) {
36+
IPAddress local_ip = WiFi.localIP(); // Get the local IP address
5237

38+
// Bind the socket to the specified multicast address and port
39+
cy_rslt_t result = socket.joinMulticastGroup(multicastIP, local_ip);
40+
udp_assert_raise(result);
5341
}
5442

55-
IPAddress local_ip = WiFi.localIP(); // Get the local IP address
43+
return socket.status(); // Return the socket status
44+
}
5645

57-
// Bind the socket to the specified multicast address and port
58-
cy_rslt_t result = socket.joinMulticastGroup(ip, local_ip);
59-
udp_assert_raise(result);
46+
uint8_t WiFiUDP::begin(uint16_t port) {
47+
// Start the UDP socket on the specified port
48+
return beginInternal(port, IPAddress(0, 0, 0, 0));
49+
}
6050

61-
return socket.status(); // Return the socket status
51+
uint8_t WiFiUDP::beginMulticast(IPAddress ip, uint16_t port) {
52+
return beginInternal(port, ip);
6253
}
6354

6455
void WiFiUDP::stop() {

libraries/WiFi/src/WiFiUdp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class WiFiUDP: public arduino::UDP {
3939
private:
4040
Socket socket;
4141

42+
uint8_t beginInternal(uint16_t port, IPAddress multicastIP = IPAddress());
4243
static cy_rslt_t receiveCallback(cy_socket_t socket_handle, void *arg);
4344

4445
typedef struct {

0 commit comments

Comments
 (0)