Skip to content

WiFi udp Multicast #119

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
26 changes: 26 additions & 0 deletions libraries/WiFi/src/SecSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,32 @@ bool Socket::connect(const char *host, uint16_t port) {
return connect(&address);
}


bool Socket::joinMulticastGroup(IPAddress multicastIP, IPAddress localIP) {
cy_socket_ip_mreq_t mreq;
mreq.multi_addr.version = CY_SOCKET_IP_VER_V4;
mreq.multi_addr.ip.v4 = (multicastIP[3] << 24) | (multicastIP[2] << 16) |
(multicastIP[1] << 8) | multicastIP[0];
mreq.if_addr.version = CY_SOCKET_IP_VER_V4;
mreq.if_addr.ip.v4 = (localIP[3] << 24) | (localIP[2] << 16) |
(localIP[1] << 8) | localIP[0];

cy_rslt_t result = cy_socket_setsockopt(
socket,
CY_SOCKET_SOL_IP,
CY_SOCKET_SO_JOIN_MULTICAST_GROUP,
&mreq,
sizeof(mreq)
);

if (result != CY_RSLT_SUCCESS) {
_status = SOCKET_STATUS_ERROR;
_last_error = result;
return false;
}
return true;
}

void Socket::listen(int max_connections) {
_last_error = cy_socket_listen(socket, max_connections);
socket_assert(_last_error);
Expand Down
1 change: 1 addition & 0 deletions libraries/WiFi/src/SecSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class Socket {
void bind(uint16_t port);
bool connect(IPAddress ip, uint16_t port);
bool connect(const char *host, uint16_t port);
bool joinMulticastGroup(IPAddress multicastIP, IPAddress localIP);

void listen(int max_connections);
bool accept(Socket & client_socket);
Expand Down
23 changes: 18 additions & 5 deletions libraries/WiFi/src/WiFiUdp.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#include "WiFiUdp.h"
#include <Arduino.h>


#define udp_assert(cy_ret) if (cy_ret != CY_RSLT_SUCCESS) { \
_status = SOCKET_STATUS_ERROR; \
Expand All @@ -18,24 +16,39 @@ WiFiUDP::WiFiUDP() :
_port(0) {
}

uint8_t WiFiUDP::begin(uint16_t port) {
// Initialize the socket for UDP
uint8_t WiFiUDP::beginInternal(uint16_t port, IPAddress multicastIP) {

socket.begin(SOCKET_PROTOCOL_UDP);
if (socket.status() != SOCKET_STATUS_CREATED) {
return 0;
}

socket.setReceiveOptCallback(receiveCallback, this);

// Bind the socket to the specified port
socket.bind(port);
if (socket.status() != SOCKET_STATUS_BOUND) {
return 0;
}

if (multicastIP != IPAddress(0, 0, 0, 0)) {
IPAddress local_ip = WiFi.localIP(); // Get the local IP address

// Bind the socket to the specified multicast address and port
cy_rslt_t result = socket.joinMulticastGroup(multicastIP, local_ip);
udp_assert_raise(result);
}

return socket.status(); // Return the socket status
}

uint8_t WiFiUDP::begin(uint16_t port) {
// Start the UDP socket on the specified port
return beginInternal(port, IPAddress(0, 0, 0, 0));
}

uint8_t WiFiUDP::beginMulticast(IPAddress ip, uint16_t port) {
return beginInternal(port, ip);
}

void WiFiUDP::stop() {
socket.end();
Expand Down
2 changes: 2 additions & 0 deletions libraries/WiFi/src/WiFiUdp.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class WiFiUDP: public arduino::UDP {

WiFiUDP();
uint8_t begin(uint16_t);
uint8_t beginMulticast(IPAddress, uint16_t);
void stop();
int beginPacket(IPAddress ip, uint16_t port);
int beginPacket(const char *host, uint16_t port);
Expand All @@ -38,6 +39,7 @@ class WiFiUDP: public arduino::UDP {
private:
Socket socket;

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

typedef struct {
Expand Down
Loading