Skip to content

Commit 553bd7e

Browse files
jaenrig-ifxIFX-Anusha
authored andcommitted
libraries/WiFi: Added protocol member via Socket.begin().
Signed-off-by: jaenrig-ifx <[email protected]>
1 parent 5f0487b commit 553bd7e

File tree

5 files changed

+31
-16
lines changed

5 files changed

+31
-16
lines changed

libraries/WiFi/src/SecSocket.cpp

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,32 @@ Socket::Socket():
1414
_status(SOCKET_STATUS_UNINITED),
1515
_last_error(CY_RSLT_SUCCESS),
1616
remote_ip(0, 0, 0, 0),
17-
_port(0) {
17+
_port(0),
18+
_protocol(0) {
1819

1920
}
2021

21-
void Socket::begin(bool is_UDP) {
22+
void Socket::begin(socket_protocol_t protocol) {
2223
_last_error = Socket::global_sockets_init();
2324
socket_assert(_last_error);
24-
25-
if (is_UDP) {
26-
_last_error = cy_socket_create(CY_SOCKET_DOMAIN_AF_INET, CY_SOCKET_TYPE_DGRAM,
27-
CY_SOCKET_IPPROTO_UDP, &socket);
28-
socket_assert(_last_error);
29-
} else {
30-
_last_error = cy_socket_create(CY_SOCKET_DOMAIN_AF_INET, CY_SOCKET_TYPE_STREAM,
31-
CY_SOCKET_IPPROTO_TCP, &socket);
32-
socket_assert(_last_error);
25+
int socket_type = 0, socket_proto = 0;
26+
27+
_protocol = protocol;
28+
switch (_protocol) {
29+
case SOCKET_PROTOCOL_TCP:
30+
socket_type = CY_SOCKET_TYPE_STREAM;
31+
socket_proto = CY_SOCKET_IPPROTO_TCP;
32+
break;
33+
case SOCKET_PROTOCOL_UDP:
34+
socket_type = CY_SOCKET_TYPE_DGRAM;
35+
socket_proto = CY_SOCKET_IPPROTO_UDP;
36+
break;
3337
}
38+
39+
_last_error = cy_socket_create(CY_SOCKET_DOMAIN_AF_INET, socket_type,
40+
socket_proto, &socket);
41+
socket_assert(_last_error);
42+
3443
_status = SOCKET_STATUS_CREATED;
3544
}
3645

libraries/WiFi/src/SecSocket.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,18 @@ typedef enum {
1616
SOCKET_STATUS_ERROR = -1
1717
} socket_status_t;
1818

19+
typedef enum {
20+
SOCKET_PROTOCOL_TCP = 0,
21+
SOCKET_PROTOCOL_UDP,
22+
} socket_protocol_t;
23+
1924
class Socket {
2025

2126
public:
2227

2328
Socket();
2429

25-
void begin(bool is_UDP);
30+
void begin(socket_protocol_t protocol);
2631
void end();
2732

2833
void setTimeout(uint32_t timeout);
@@ -58,6 +63,7 @@ class Socket {
5863

5964
IPAddress remote_ip;
6065
uint16_t _port;
66+
socket_protocol_t _protocol;
6167

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

libraries/WiFi/src/WiFiClient.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ WiFiClient::WiFiClient() :
1414
}
1515

1616
int WiFiClient::connect(IPAddress ip, uint16_t port) {
17-
socket->begin(false); // false = TCP
17+
socket->begin(SOCKET_PROTOCOL_TCP);
1818

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

2525
int WiFiClient::connect(const char *host, uint16_t port) {
26-
socket->begin(false); // false = TCP
26+
socket->begin(SOCKET_PROTOCOL_TCP);
2727

2828
socket->setReceiveOptCallback(receiveCallback, this);
2929
socket->setDisconnectOptCallback(disconnectionCallback, this);

libraries/WiFi/src/WiFiServer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ WiFiServer::WiFiServer() {
99
}
1010

1111
void WiFiServer::begin(uint16_t port) {
12-
socket.begin(false); // false = TCP
12+
socket.begin(SOCKET_PROTOCOL_TCP);
1313

1414
socket.setTimeout(SERVER_RECV_TIMEOUT_MS);
1515
socket.setConnectOptCallback(connectionCallback, this);

libraries/WiFi/src/WiFiUdp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ uint8_t WiFiUDP::begin(uint16_t port) {
2222
_port = port;
2323

2424
// Initialize the socket for UDP
25-
socket.begin(true); // true = UDP
25+
socket.begin(SOCKET_PROTOCOL_UDP);
2626
if (socket.status() != SOCKET_STATUS_CREATED) {
2727
return 0; // Return 0 if socket creation fails
2828
}

0 commit comments

Comments
 (0)