Skip to content

Commit 6915c93

Browse files
authored
Update Drv IP stack to use FwSizeType instead of U32 (#4013)
* Update IP stack source to use SizeType instead of I/U32 * Update UTs reflecting U32/I32 change to SizeType * Static cast sizes per review * Fix commented out thing
1 parent 7972392 commit 6915c93

22 files changed

+103
-84
lines changed

Drv/Ip/IpSocket.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,14 @@ SocketIpStatus IpSocket::open(SocketDescriptor& socketDescriptor) {
129129
return status;
130130
}
131131

132-
SocketIpStatus IpSocket::send(const SocketDescriptor& socketDescriptor, const U8* const data, const U32 size) {
132+
SocketIpStatus IpSocket::send(const SocketDescriptor& socketDescriptor, const U8* const data, const FwSizeType size) {
133133
FW_ASSERT(data != nullptr);
134134
FW_ASSERT(size > 0);
135135

136-
U32 total = 0;
137-
I32 sent = 0;
136+
FwSizeType total = 0;
137+
FwSignedSizeType sent = 0;
138138
// Attempt to send out data and retry as necessary
139-
for (U32 i = 0; (i < SOCKET_MAX_ITERATIONS) && (total < size); i++) {
139+
for (FwSizeType i = 0; (i < SOCKET_MAX_ITERATIONS) && (total < size); i++) {
140140
errno = 0;
141141
// Send using my specific protocol
142142
sent = this->sendProtocol(socketDescriptor, data + total, size - total);
@@ -153,7 +153,7 @@ SocketIpStatus IpSocket::send(const SocketDescriptor& socketDescriptor, const U8
153153
return SOCK_SEND_ERROR;
154154
}
155155
FW_ASSERT(sent > 0, static_cast<FwAssertArgType>(sent));
156-
total += static_cast<U32>(sent);
156+
total += static_cast<FwSizeType>(sent);
157157
}
158158
// Failed to retry enough to send all data
159159
if (total < size) {
@@ -164,24 +164,24 @@ SocketIpStatus IpSocket::send(const SocketDescriptor& socketDescriptor, const U8
164164
return SOCK_SUCCESS;
165165
}
166166

167-
SocketIpStatus IpSocket::recv(const SocketDescriptor& socketDescriptor, U8* data, U32& req_read) {
167+
SocketIpStatus IpSocket::recv(const SocketDescriptor& socketDescriptor, U8* data, FwSizeType& req_read) {
168168
// TODO: Uncomment FW_ASSERT for socketDescriptor.fd once we fix TcpClientTester to not pass in uninitialized
169169
// socketDescriptor
170170
// FW_ASSERT(socketDescriptor.fd != -1, static_cast<FwAssertArgType>(socketDescriptor.fd));
171171
FW_ASSERT(data != nullptr);
172172

173-
I32 bytes_received_or_status; // Stores the return value from recvProtocol
173+
FwSignedSizeType bytes_received_or_status; // Stores the return value from recvProtocol
174174

175175
// Loop primarily for EINTR. Other conditions should lead to an earlier exit.
176-
for (U32 i = 0; i < SOCKET_MAX_ITERATIONS; i++) {
176+
for (FwSizeType i = 0; i < SOCKET_MAX_ITERATIONS; i++) {
177177
errno = 0;
178178
// Pass the current value of req_read (max buffer size) to recvProtocol.
179179
// recvProtocol returns bytes read or -1 on error.
180180
bytes_received_or_status = this->recvProtocol(socketDescriptor, data, req_read);
181181

182182
if (bytes_received_or_status > 0) {
183183
// Successfully read data
184-
req_read = static_cast<U32>(bytes_received_or_status);
184+
req_read = static_cast<FwSizeType>(bytes_received_or_status);
185185
return SOCK_SUCCESS;
186186
} else if (bytes_received_or_status == 0) {
187187
// Handle zero return based on protocol-specific behavior

Drv/Ip/IpSocket.hpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ class IpSocket {
116116
* \param size: size of data to send
117117
* \return status of the send, SOCK_DISCONNECTED to reopen, SOCK_SUCCESS on success, something else on error
118118
*/
119-
virtual SocketIpStatus send(const SocketDescriptor& socketDescriptor, const U8* const data, const U32 size);
119+
virtual SocketIpStatus send(const SocketDescriptor& socketDescriptor, const U8* const data, const FwSizeType size);
120120
/**
121121
* \brief receive data from the IP socket from the given buffer
122122
*
@@ -133,7 +133,7 @@ class IpSocket {
133133
* \param size: maximum size of data buffer to fill
134134
* \return status of the send, SOCK_DISCONNECTED to reopen, SOCK_SUCCESS on success, something else on error
135135
*/
136-
SocketIpStatus recv(const SocketDescriptor& fd, U8* const data, U32& size);
136+
SocketIpStatus recv(const SocketDescriptor& fd, U8* const data, FwSizeType& size);
137137

138138
/**
139139
* \brief closes the socket
@@ -198,7 +198,9 @@ class IpSocket {
198198
* \param size: size of data to send
199199
* \return: size of data sent, or -1 on error.
200200
*/
201-
virtual I32 sendProtocol(const SocketDescriptor& socketDescriptor, const U8* const data, const U32 size) = 0;
201+
virtual FwSignedSizeType sendProtocol(const SocketDescriptor& socketDescriptor,
202+
const U8* const data,
203+
const FwSizeType size) = 0;
202204

203205
/**
204206
* \brief Protocol specific implementation of recv. Called directly with error handling from recv.
@@ -207,7 +209,9 @@ class IpSocket {
207209
* \param size: size of data buffer
208210
* \return: size of data received, or -1 on error.
209211
*/
210-
virtual I32 recvProtocol(const SocketDescriptor& socketDescriptor, U8* const data, const U32 size) = 0;
212+
virtual FwSignedSizeType recvProtocol(const SocketDescriptor& socketDescriptor,
213+
U8* const data,
214+
const FwSizeType size) = 0;
211215

212216
/**
213217
* \brief Handle zero return from recvProtocol

Drv/Ip/SocketComponentHelper.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ SocketIpStatus SocketComponentHelper::reopen() {
102102
return status;
103103
}
104104

105-
SocketIpStatus SocketComponentHelper::send(const U8* const data, const U32 size) {
105+
SocketIpStatus SocketComponentHelper::send(const U8* const data, const FwSizeType size) {
106106
SocketIpStatus status = SOCK_SUCCESS;
107107
this->m_lock.lock();
108108
SocketDescriptor descriptor = this->m_descriptor;
@@ -157,7 +157,7 @@ bool SocketComponentHelper::running() {
157157
return running;
158158
}
159159

160-
SocketIpStatus SocketComponentHelper::recv(U8* data, U32& size) {
160+
SocketIpStatus SocketComponentHelper::recv(U8* data, FwSizeType& size) {
161161
SocketIpStatus status = SOCK_SUCCESS;
162162
// Check for previously disconnected socket
163163
this->m_lock.lock();
@@ -195,8 +195,7 @@ void SocketComponentHelper::readLoop() {
195195
Fw::Buffer buffer = this->getBuffer();
196196
U8* data = buffer.getData();
197197
FW_ASSERT(data);
198-
FW_ASSERT_NO_OVERFLOW(buffer.getSize(), U32);
199-
U32 size = static_cast<U32>(buffer.getSize());
198+
FwSizeType size = buffer.getSize();
200199
// recv blocks, so it may have been a while since its done an isOpened check
201200
status = this->recv(data, size);
202201
if ((status != SOCK_SUCCESS) && (status != SOCK_INTERRUPTED_TRY_AGAIN) &&

Drv/Ip/SocketComponentHelper.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class SocketComponentHelper {
9999
* \param size: size of data to send
100100
* \return status of send, SOCK_SUCCESS for success, something else on error
101101
*/
102-
SocketIpStatus send(const U8* const data, const U32 size);
102+
SocketIpStatus send(const U8* const data, const FwSizeType size);
103103

104104
/**
105105
* \brief receive data from the IP socket from the given buffer
@@ -109,7 +109,7 @@ class SocketComponentHelper {
109109
* \param size: maximum size of data buffer to fill
110110
* \return status of the send, SOCK_DISCONNECTED to reopen, SOCK_SUCCESS on success, something else on error
111111
*/
112-
SocketIpStatus recv(U8* data, U32& size);
112+
SocketIpStatus recv(U8* data, FwSizeType& size);
113113

114114
/**
115115
* \brief close the socket communications

Drv/Ip/TcpClientSocket.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,18 @@ SocketIpStatus TcpClientSocket::openProtocol(SocketDescriptor& socketDescriptor)
8585
return SOCK_SUCCESS;
8686
}
8787

88-
I32 TcpClientSocket::sendProtocol(const SocketDescriptor& socketDescriptor, const U8* const data, const U32 size) {
89-
return static_cast<I32>(::send(socketDescriptor.fd, data, size, SOCKET_IP_SEND_FLAGS));
88+
FwSignedSizeType TcpClientSocket::sendProtocol(const SocketDescriptor& socketDescriptor,
89+
const U8* const data,
90+
const FwSizeType size) {
91+
return static_cast<FwSignedSizeType>(
92+
::send(socketDescriptor.fd, data, static_cast<size_t>(size), SOCKET_IP_SEND_FLAGS));
9093
}
9194

92-
I32 TcpClientSocket::recvProtocol(const SocketDescriptor& socketDescriptor, U8* const data, const U32 size) {
93-
return static_cast<I32>(::recv(socketDescriptor.fd, data, size, SOCKET_IP_RECV_FLAGS));
95+
FwSignedSizeType TcpClientSocket::recvProtocol(const SocketDescriptor& socketDescriptor,
96+
U8* const data,
97+
const FwSizeType size) {
98+
return static_cast<FwSignedSizeType>(
99+
::recv(socketDescriptor.fd, data, static_cast<size_t>(size), SOCKET_IP_RECV_FLAGS));
94100
}
95101

96102
} // namespace Drv

Drv/Ip/TcpClientSocket.hpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,19 @@ class TcpClientSocket : public IpSocket {
5656
* \param size: size of data to send
5757
* \return: size of data sent, or -1 on error.
5858
*/
59-
I32 sendProtocol(const SocketDescriptor& socketDescriptor, const U8* const data, const U32 size) override;
59+
FwSignedSizeType sendProtocol(const SocketDescriptor& socketDescriptor,
60+
const U8* const data,
61+
const FwSizeType size) override;
6062
/**
6163
* \brief Protocol specific implementation of recv. Called directly with error handling from recv.
6264
* \param socketDescriptor: descriptor to recv from
6365
* \param data: data pointer to fill
6466
* \param size: size of data buffer
6567
* \return: size of data received, or -1 on error.
6668
*/
67-
I32 recvProtocol(const SocketDescriptor& socketDescriptor, U8* const data, const U32 size) override;
69+
FwSignedSizeType recvProtocol(const SocketDescriptor& socketDescriptor,
70+
U8* const data,
71+
const FwSizeType size) override;
6872
};
6973
} // namespace Drv
7074

Drv/Ip/TcpServerSocket.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,19 @@ SocketIpStatus TcpServerSocket::openProtocol(SocketDescriptor& socketDescriptor)
119119
return SOCK_SUCCESS;
120120
}
121121

122-
I32 TcpServerSocket::sendProtocol(const SocketDescriptor& socketDescriptor, const U8* const data, const U32 size) {
123-
return static_cast<I32>(::send(socketDescriptor.fd, data, size, SOCKET_IP_SEND_FLAGS));
122+
FwSignedSizeType TcpServerSocket::sendProtocol(const SocketDescriptor& socketDescriptor,
123+
const U8* const data,
124+
const FwSizeType size) {
125+
return static_cast<FwSignedSizeType>(
126+
::send(socketDescriptor.fd, data, static_cast<size_t>(size), SOCKET_IP_SEND_FLAGS));
124127
}
125128

126-
I32 TcpServerSocket::recvProtocol(const SocketDescriptor& socketDescriptor, U8* const data, const U32 size) {
127-
I32 size_buf;
129+
FwSignedSizeType TcpServerSocket::recvProtocol(const SocketDescriptor& socketDescriptor,
130+
U8* const data,
131+
const FwSizeType size) {
128132
// recv will return 0 if the client has done an orderly shutdown
129-
size_buf = static_cast<I32>(::recv(socketDescriptor.fd, data, size, SOCKET_IP_RECV_FLAGS));
130-
return size_buf;
133+
return static_cast<FwSignedSizeType>(
134+
::recv(socketDescriptor.fd, data, static_cast<size_t>(size), SOCKET_IP_RECV_FLAGS));
131135
}
132136

133137
} // namespace Drv

Drv/Ip/TcpServerSocket.hpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,19 @@ class TcpServerSocket : public IpSocket {
7676
* \param size: size of data to send
7777
* \return: size of data sent, or -1 on error.
7878
*/
79-
I32 sendProtocol(const SocketDescriptor& socketDescriptor, const U8* const data, const U32 size) override;
79+
FwSignedSizeType sendProtocol(const SocketDescriptor& socketDescriptor,
80+
const U8* const data,
81+
const FwSizeType size) override;
8082
/**
8183
* \brief Protocol specific implementation of recv. Called directly with error handling from recv.
8284
* \param socketDescriptor: descriptor to recv from
8385
* \param data: data pointer to fill
8486
* \param size: size of data buffer
8587
* \return: size of data received, or -1 on error.
8688
*/
87-
I32 recvProtocol(const SocketDescriptor& socketDescriptor, U8* const data, const U32 size) override;
89+
FwSignedSizeType recvProtocol(const SocketDescriptor& socketDescriptor,
90+
U8* const data,
91+
const FwSizeType size) override;
8892
};
8993
} // namespace Drv
9094

Drv/Ip/UdpSocket.cpp

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -192,17 +192,21 @@ SocketIpStatus UdpSocket::openProtocol(SocketDescriptor& socketDescriptor) {
192192
return status;
193193
}
194194

195-
I32 UdpSocket::sendProtocol(const SocketDescriptor& socketDescriptor, const U8* const data, const U32 size) {
195+
FwSignedSizeType UdpSocket::sendProtocol(const SocketDescriptor& socketDescriptor,
196+
const U8* const data,
197+
const FwSizeType size) {
196198
FW_ASSERT(this->m_addr_send.sin_family != 0); // Make sure the address was previously setup
197199
FW_ASSERT(socketDescriptor.fd >= 0); // File descriptor should be valid
198200
FW_ASSERT(data != nullptr); // Data pointer should not be null
199201

200-
return static_cast<I32>(::sendto(socketDescriptor.fd, data, size, SOCKET_IP_SEND_FLAGS,
201-
reinterpret_cast<struct sockaddr*>(&this->m_addr_send),
202-
sizeof(this->m_addr_send)));
202+
return static_cast<FwSignedSizeType>(
203+
::sendto(socketDescriptor.fd, data, static_cast<size_t>(size), SOCKET_IP_SEND_FLAGS,
204+
reinterpret_cast<struct sockaddr*>(&this->m_addr_send), sizeof(this->m_addr_send)));
203205
}
204206

205-
I32 UdpSocket::recvProtocol(const SocketDescriptor& socketDescriptor, U8* const data, const U32 size) {
207+
FwSignedSizeType UdpSocket::recvProtocol(const SocketDescriptor& socketDescriptor,
208+
U8* const data,
209+
const FwSizeType size) {
206210
FW_ASSERT(this->m_addr_recv.sin_family != 0); // Make sure the address was previously setup
207211
FW_ASSERT(socketDescriptor.fd >= 0); // File descriptor should be valid
208212
FW_ASSERT(data != nullptr); // Data pointer should not be null
@@ -212,8 +216,9 @@ I32 UdpSocket::recvProtocol(const SocketDescriptor& socketDescriptor, U8* const
212216
(void)::memset(&sender_addr, 0, sizeof(sender_addr));
213217

214218
socklen_t sender_addr_len = sizeof(sender_addr);
215-
I32 received = static_cast<I32>(::recvfrom(socketDescriptor.fd, data, size, SOCKET_IP_RECV_FLAGS,
216-
reinterpret_cast<struct sockaddr*>(&sender_addr), &sender_addr_len));
219+
FwSignedSizeType received = static_cast<FwSignedSizeType>(
220+
::recvfrom(socketDescriptor.fd, data, static_cast<size_t>(size), SOCKET_IP_RECV_FLAGS,
221+
reinterpret_cast<struct sockaddr*>(&sender_addr), &sender_addr_len));
217222
// If we have not configured a send port, set it to the source of the last received packet
218223
if (received >= 0 && this->m_addr_send.sin_port == 0) {
219224
this->m_addr_send = sender_addr;
@@ -223,14 +228,14 @@ I32 UdpSocket::recvProtocol(const SocketDescriptor& socketDescriptor, U8* const
223228
return received;
224229
}
225230

226-
SocketIpStatus UdpSocket::send(const SocketDescriptor& socketDescriptor, const U8* const data, const U32 size) {
231+
SocketIpStatus UdpSocket::send(const SocketDescriptor& socketDescriptor, const U8* const data, const FwSizeType size) {
227232
// Note: socketDescriptor.fd can be -1 in some test cases
228233
FW_ASSERT((size == 0) || (data != nullptr));
229234

230235
// Special case for zero-length datagrams in UDP
231236
if (size == 0) {
232237
errno = 0;
233-
I32 sent = this->sendProtocol(socketDescriptor, data, 0);
238+
FwSignedSizeType sent = this->sendProtocol(socketDescriptor, data, 0);
234239
if (sent == -1) {
235240
if (errno == EINTR) {
236241
// For zero-length datagrams, we'll just try once more if interrupted

Drv/Ip/UdpSocket.hpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class UdpSocket : public IpSocket {
109109
* \param size: size of data to send
110110
* \return: status of the send operation
111111
*/
112-
SocketIpStatus send(const SocketDescriptor& socketDescriptor, const U8* const data, const U32 size) override;
112+
SocketIpStatus send(const SocketDescriptor& socketDescriptor, const U8* const data, const FwSizeType size) override;
113113

114114
protected:
115115
/**
@@ -131,15 +131,19 @@ class UdpSocket : public IpSocket {
131131
* \param size: size of data to send
132132
* \return: size of data sent, or -1 on error.
133133
*/
134-
I32 sendProtocol(const SocketDescriptor& socketDescriptor, const U8* const data, const U32 size) override;
134+
FwSignedSizeType sendProtocol(const SocketDescriptor& socketDescriptor,
135+
const U8* const data,
136+
const FwSizeType size) override;
135137
/**
136138
* \brief Protocol specific implementation of recv. Called directly with error handling from recv.
137139
* \param socketDescriptor: descriptor to recv from
138140
* \param data: data pointer to fill
139141
* \param size: size of data buffer
140142
* \return: size of data received, or -1 on error.
141143
*/
142-
I32 recvProtocol(const SocketDescriptor& socketDescriptor, U8* const data, const U32 size) override;
144+
FwSignedSizeType recvProtocol(const SocketDescriptor& socketDescriptor,
145+
U8* const data,
146+
const FwSizeType size) override;
143147
/**
144148
* \brief Handle zero return from recvProtocol for UDP
145149
*

0 commit comments

Comments
 (0)