Skip to content

Wifi UDP class and Begin function #106

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

Merged
merged 5 commits into from
May 11, 2025
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
22 changes: 18 additions & 4 deletions libraries/WiFi/src/SecSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,30 @@ Socket::Socket():
_status(SOCKET_STATUS_UNINITED),
_last_error(CY_RSLT_SUCCESS),
remote_ip(0, 0, 0, 0),
_port(0) {
_port(0),
_protocol(0) {

}

void Socket::begin() {
void Socket::begin(socket_protocol_t protocol) {
_last_error = Socket::global_sockets_init();
socket_assert(_last_error);
int socket_type = 0, socket_proto = 0;

_protocol = protocol;
switch (_protocol) {
case SOCKET_PROTOCOL_TCP:
socket_type = CY_SOCKET_TYPE_STREAM;
socket_proto = CY_SOCKET_IPPROTO_TCP;
break;
case SOCKET_PROTOCOL_UDP:
socket_type = CY_SOCKET_TYPE_DGRAM;
socket_proto = CY_SOCKET_IPPROTO_UDP;
break;
}

_last_error = cy_socket_create(CY_SOCKET_DOMAIN_AF_INET, CY_SOCKET_TYPE_STREAM,
CY_SOCKET_IPPROTO_TCP, &socket);
_last_error = cy_socket_create(CY_SOCKET_DOMAIN_AF_INET, socket_type,
socket_proto, &socket);
socket_assert(_last_error);

_status = SOCKET_STATUS_CREATED;
Expand Down
8 changes: 7 additions & 1 deletion libraries/WiFi/src/SecSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@ typedef enum {
SOCKET_STATUS_ERROR = -1
} socket_status_t;

typedef enum {
SOCKET_PROTOCOL_TCP = 0,
SOCKET_PROTOCOL_UDP,
} socket_protocol_t;

class Socket {

public:

Socket();

void begin();
void begin(socket_protocol_t protocol);
void end();

void setTimeout(uint32_t timeout);
Expand Down Expand Up @@ -58,6 +63,7 @@ class Socket {

IPAddress remote_ip;
uint16_t _port;
socket_protocol_t _protocol;

void setOptCallback(int optname, cy_socket_callback_t cback, void *arg);

Expand Down
4 changes: 2 additions & 2 deletions libraries/WiFi/src/WiFiClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ WiFiClient::WiFiClient() :
}

int WiFiClient::connect(IPAddress ip, uint16_t port) {
socket->begin();
socket->begin(SOCKET_PROTOCOL_TCP);

socket->setReceiveOptCallback(receiveCallback, this);
socket->setDisconnectOptCallback(disconnectionCallback, this);
Expand All @@ -23,7 +23,7 @@ int WiFiClient::connect(IPAddress ip, uint16_t port) {
}

int WiFiClient::connect(const char *host, uint16_t port) {
socket->begin();
socket->begin(SOCKET_PROTOCOL_TCP);

socket->setReceiveOptCallback(receiveCallback, this);
socket->setDisconnectOptCallback(disconnectionCallback, this);
Expand Down
2 changes: 1 addition & 1 deletion libraries/WiFi/src/WiFiServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ WiFiServer::WiFiServer() {
}

void WiFiServer::begin(uint16_t port) {
socket.begin();
socket.begin(SOCKET_PROTOCOL_TCP);

socket.setTimeout(SERVER_RECV_TIMEOUT_MS);
socket.setConnectOptCallback(connectionCallback, this);
Expand Down
97 changes: 97 additions & 0 deletions libraries/WiFi/src/WiFiUdp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#include "WiFiUdp.h"
#include <Arduino.h>

#define udp_assert(cy_ret) if (cy_ret != CY_RSLT_SUCCESS) { \
_status = SOCKET_STATUS_ERROR; \
return; \
}

#define udp_assert_raise(cy_ret) if (cy_ret != CY_RSLT_SUCCESS) { \
return cy_ret; \
}

WiFiUDP::WiFiUDP() :
client_handle(nullptr),
_status(SOCKET_STATUS_UNINITED),
_last_error(CY_RSLT_SUCCESS),
remote_ip(0, 0, 0, 0),
_port(0) {
}

uint8_t WiFiUDP::begin(uint16_t port) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Socket class is already there. Shouldn´t we extend it for UPD?
When UDP we use recvFrom() and sendTo() instead of recv() and send(). The create() gets different arguments but many other things would be reusable.
And other functions are simply not used for UDP.
https://github.com/Infineon/mtb-example-wifi-udp-client/blob/master/source/udp_client.c
https://github.com/Infineon/mtb-example-threadx-wifi-udp-server/blob/master/udp_server.c

_port = port;

// Initialize the socket for UDP
socket.begin(SOCKET_PROTOCOL_UDP);
if (socket.status() != SOCKET_STATUS_CREATED) {
return 0; // Return 0 if socket creation fails
}
// Bind the socket to the specified port
socket.bind(port);
if (socket.status() != SOCKET_STATUS_BOUND) {
return 0; // Return 0 if binding fail

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


void WiFiUDP::stop() {

}

int WiFiUDP::beginPacket(IPAddress ip, uint16_t port) {
return 0;
}

int WiFiUDP::beginPacket(const char *host, uint16_t port) {
return 0;
}

int WiFiUDP::endPacket() {
return 0;
}

size_t WiFiUDP::write(uint8_t) {
return 0;
}

size_t WiFiUDP::write(const uint8_t *buffer, size_t size) {
return 0;
}

int WiFiUDP::parsePacket() {
return 0;
}

int WiFiUDP::available() {
return 0;
}

int WiFiUDP::read() {
return 0;
}

int WiFiUDP::read(unsigned char *buffer, size_t len) {
return 0;
}

int WiFiUDP::read(char *buffer, size_t len) {
return 0;
}

int WiFiUDP::peek() {
return 0;
}

void WiFiUDP::flush() {
return;
}

IPAddress WiFiUDP::remoteIP() {
return IPAddress();
}

uint16_t WiFiUDP::remotePort() {
return 0;
}
49 changes: 49 additions & 0 deletions libraries/WiFi/src/WiFiUdp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#ifndef WIFI_UDP_H
#define WIFI_UDP_H

#include <stdint.h>
#include <string.h>
#include <api/Udp.h>
#include "WiFiClient.h"
#include "WiFi.h"
#include "WiFiServer.h"
#include "SecSocket.h"

class WiFiUDP: public arduino::UDP {
public:
WiFiUDP();
uint8_t begin(uint16_t);

// yet to implement these functions
void stop();
int beginPacket(IPAddress ip, uint16_t port);
int beginPacket(const char *host, uint16_t port);
int endPacket();
size_t write(uint8_t);
size_t write(const uint8_t *buffer, size_t size);

using Print::write;
int parsePacket();
int available();
int read();
int read(unsigned char *buffer, size_t len);
int read(char *buffer, size_t len);
int peek();
void flush();
IPAddress remoteIP();
uint16_t remotePort();



private:
Socket socket;

cy_socket_t client_handle;
socket_status_t _status;
cy_rslt_t _last_error;
IPAddress remote_ip;
uint16_t _port;

};

#endif /* WIFI_UDP_H */
Loading