Skip to content

Commit e17d42a

Browse files
authored
Format Drv module (#3960)
* Format Drv module * Add Drv to format CI check * Fix double // in include path
1 parent 578e61f commit e17d42a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+1310
-1548
lines changed

.github/workflows/format-check.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ jobs:
3030
- name: "Check C++ Formatting"
3131
env:
3232
CHECKED_DIRS: >-
33+
Drv
3334
Fw/Buffer
3435
Fw/Cmd
3536
Fw/Com

Drv/Ip/IpSocket.cpp

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,55 +9,56 @@
99
// acknowledged.
1010
//
1111
// ======================================================================
12-
#include <cstring>
12+
#include <sys/time.h>
1313
#include <Drv/Ip/IpSocket.hpp>
14-
#include <Fw/Types/Assert.hpp>
1514
#include <Fw/FPrimeBasicTypes.hpp>
15+
#include <Fw/Types/Assert.hpp>
1616
#include <Fw/Types/StringUtils.hpp>
17-
#include <sys/time.h>
17+
#include <cstring>
1818

1919
// This implementation has primarily implemented to isolate
2020
// the socket interface from the F' Fw::Buffer class.
2121
// There is a macro in VxWorks (m_data) that collides with
2222
// the m_data member in Fw::Buffer.
2323

2424
#ifdef TGT_OS_TYPE_VXWORKS
25-
#include <socket.h>
26-
#include <inetLib.h>
25+
#include <errnoLib.h>
2726
#include <fioLib.h>
2827
#include <hostLib.h>
28+
#include <inetLib.h>
2929
#include <ioLib.h>
30-
#include <vxWorks.h>
3130
#include <sockLib.h>
32-
#include <fioLib.h>
33-
#include <taskLib.h>
31+
#include <socket.h>
3432
#include <sysLib.h>
35-
#include <errnoLib.h>
33+
#include <taskLib.h>
34+
#include <vxWorks.h>
3635
#include <cstring>
3736
#elif defined TGT_OS_TYPE_LINUX || TGT_OS_TYPE_DARWIN
37+
#include <arpa/inet.h>
3838
#include <sys/socket.h>
3939
#include <unistd.h>
4040
#include <cerrno>
41-
#include <arpa/inet.h>
4241
#else
4342
#error OS not supported for IP Socket Communications
4443
#endif
4544

46-
4745
namespace Drv {
4846

4947
IpSocket::IpSocket() : m_timeoutSeconds(0), m_timeoutMicroseconds(0), m_port(0) {
5048
::memset(m_hostname, 0, sizeof(m_hostname));
5149
}
5250

53-
SocketIpStatus IpSocket::configure(const char* const hostname, const U16 port, const U32 timeout_seconds, const U32 timeout_microseconds) {
51+
SocketIpStatus IpSocket::configure(const char* const hostname,
52+
const U16 port,
53+
const U32 timeout_seconds,
54+
const U32 timeout_microseconds) {
5455
FW_ASSERT(timeout_microseconds < 1000000, static_cast<FwAssertArgType>(timeout_microseconds));
5556
FW_ASSERT(this->isValidPort(port), static_cast<FwAssertArgType>(port));
5657
FW_ASSERT(hostname != nullptr);
5758
this->m_timeoutSeconds = timeout_seconds;
5859
this->m_timeoutMicroseconds = timeout_microseconds;
5960
this->m_port = port;
60-
(void) Fw::StringUtils::string_copy(this->m_hostname, hostname, static_cast<FwSizeType>(SOCKET_MAX_HOSTNAME_SIZE));
61+
(void)Fw::StringUtils::string_copy(this->m_hostname, hostname, static_cast<FwSizeType>(SOCKET_MAX_HOSTNAME_SIZE));
6162
return SOCK_SUCCESS;
6263
}
6364

@@ -75,7 +76,7 @@ SocketIpStatus IpSocket::setupTimeouts(int socketFd) {
7576
timeout.tv_sec = static_cast<time_t>(this->m_timeoutSeconds);
7677
timeout.tv_usec = static_cast<suseconds_t>(this->m_timeoutMicroseconds);
7778
// set socket write to timeout after 1 sec
78-
if (setsockopt(socketFd, SOL_SOCKET, SO_SNDTIMEO, reinterpret_cast<char *>(&timeout), sizeof(timeout)) < 0) {
79+
if (setsockopt(socketFd, SOL_SOCKET, SO_SNDTIMEO, reinterpret_cast<char*>(&timeout), sizeof(timeout)) < 0) {
7980
return SOCK_FAILED_TO_SET_SOCKET_OPTIONS;
8081
}
8182
#endif
@@ -132,9 +133,9 @@ SocketIpStatus IpSocket::send(const SocketDescriptor& socketDescriptor, const U8
132133
FW_ASSERT(socketDescriptor.fd != -1, static_cast<FwAssertArgType>(socketDescriptor.fd));
133134
FW_ASSERT(data != nullptr);
134135
FW_ASSERT(size > 0);
135-
136+
136137
U32 total = 0;
137-
I32 sent = 0;
138+
I32 sent = 0;
138139
// Attempt to send out data and retry as necessary
139140
for (U32 i = 0; (i < SOCKET_MAX_ITERATIONS) && (total < size); i++) {
140141
errno = 0;
@@ -165,12 +166,12 @@ SocketIpStatus IpSocket::send(const SocketDescriptor& socketDescriptor, const U8
165166
}
166167

167168
SocketIpStatus IpSocket::recv(const SocketDescriptor& socketDescriptor, U8* data, U32& req_read) {
168-
//TODO: Uncomment FW_ASSERT for socketDescriptor.fd once we fix TcpClientTester to not pass in uninitialized socketDescriptor
169-
// FW_ASSERT(socketDescriptor.fd != -1, static_cast<FwAssertArgType>(socketDescriptor.fd));
169+
// TODO: Uncomment FW_ASSERT for socketDescriptor.fd once we fix TcpClientTester to not pass in uninitialized
170+
// socketDescriptor
171+
// FW_ASSERT(socketDescriptor.fd != -1, static_cast<FwAssertArgType>(socketDescriptor.fd));
170172
FW_ASSERT(data != nullptr);
171173

172-
173-
I32 bytes_received_or_status; // Stores the return value from recvProtocol
174+
I32 bytes_received_or_status; // Stores the return value from recvProtocol
174175

175176
// Loop primarily for EINTR. Other conditions should lead to an earlier exit.
176177
for (U32 i = 0; i < SOCKET_MAX_ITERATIONS; i++) {
@@ -187,15 +188,15 @@ SocketIpStatus IpSocket::recv(const SocketDescriptor& socketDescriptor, U8* data
187188
// Handle zero return based on protocol-specific behavior
188189
req_read = 0;
189190
return this->handleZeroReturn();
190-
} else { // bytes_received_or_status == -1, an error occurred
191+
} else { // bytes_received_or_status == -1, an error occurred
191192
if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
192193
// Non-blocking socket would block, or SO_RCVTIMEO timeout occurred.
193194
req_read = 0;
194195
return SOCK_NO_DATA_AVAILABLE;
195196
} else if ((errno == ECONNRESET) || (errno == EBADF)) {
196197
// Connection reset or bad file descriptor.
197198
req_read = 0;
198-
return SOCK_DISCONNECTED; // Or a more specific error like SOCK_READ_ERROR
199+
return SOCK_DISCONNECTED; // Or a more specific error like SOCK_READ_ERROR
199200
} else {
200201
// Other socket read error.
201202
req_read = 0;

Drv/Ip/IpSocket.hpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
#define DRV_IP_IPHELPER_HPP_
1414

1515
#include <Fw/FPrimeBasicTypes.hpp>
16-
#include <config/IpCfg.hpp>
1716
#include <Os/Mutex.hpp>
17+
#include <config/IpCfg.hpp>
1818

1919
namespace Drv {
2020

2121
struct SocketDescriptor final {
22-
int fd = -1; //!< Used for all sockets to track the communication file descriptor
23-
int serverFd = -1; //!< Used for server sockets to track the listening file descriptor
22+
int fd = -1; //!< Used for all sockets to track the communication file descriptor
23+
int serverFd = -1; //!< Used for server sockets to track the listening file descriptor
2424
};
2525

2626
/**
@@ -57,7 +57,7 @@ enum SocketIpStatus {
5757
class IpSocket {
5858
public:
5959
IpSocket();
60-
virtual ~IpSocket(){};
60+
virtual ~IpSocket() {};
6161
/**
6262
* \brief configure the ip socket with host and transmission timeouts
6363
*
@@ -76,7 +76,9 @@ class IpSocket {
7676
* \param send_timeout_microseconds: send timeout microseconds portion. Must be less than 1000000
7777
* \return status of configure
7878
*/
79-
virtual SocketIpStatus configure(const char* hostname, const U16 port, const U32 send_timeout_seconds,
79+
virtual SocketIpStatus configure(const char* hostname,
80+
const U16 port,
81+
const U32 send_timeout_seconds,
8082
const U32 send_timeout_microseconds);
8183

8284
/**
@@ -173,7 +175,7 @@ class IpSocket {
173175
* \brief setup the socket timeout properties of the opened outgoing socket
174176
* \param socketDescriptor: socket descriptor to setup
175177
* \return status of timeout setup
176-
*/
178+
*/
177179
SocketIpStatus setupTimeouts(int socketFd);
178180

179181
/**
@@ -220,7 +222,7 @@ class IpSocket {
220222

221223
U32 m_timeoutSeconds;
222224
U32 m_timeoutMicroseconds;
223-
U16 m_port; //!< IP address port used
225+
U16 m_port; //!< IP address port used
224226
char m_hostname[SOCKET_MAX_HOSTNAME_SIZE]; //!< Hostname to supply
225227
};
226228
} // namespace Drv

Drv/Ip/SocketComponentHelper.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ SocketComponentHelper::SocketComponentHelper() {}
2121

2222
SocketComponentHelper::~SocketComponentHelper() {}
2323

24-
void SocketComponentHelper::start(const Fw::StringBase &name,
24+
void SocketComponentHelper::start(const Fw::StringBase& name,
2525
const FwTaskPriorityType priority,
2626
const Os::Task::ParamType stack,
2727
const Os::Task::ParamType cpuAffinity) {
28-
FW_ASSERT(m_task.getState() == Os::Task::State::NOT_STARTED); // It is a coding error to start this task multiple times
28+
FW_ASSERT(m_task.getState() ==
29+
Os::Task::State::NOT_STARTED); // It is a coding error to start this task multiple times
2930
this->m_stop = false;
3031
// Note: the first step is for the IP socket to open the port
3132
Os::Task::Arguments arguments(name, SocketComponentHelper::readTask, this, priority, stack, cpuAffinity);
@@ -45,7 +46,6 @@ SocketIpStatus SocketComponentHelper::open() {
4546
} else {
4647
local_open = OpenState::SKIP;
4748
}
48-
4949
}
5050
if (local_open == OpenState::OPENING) {
5151
FW_ASSERT(this->m_descriptor.fd == -1); // Ensure we are not opening an opened socket
@@ -111,7 +111,7 @@ SocketIpStatus SocketComponentHelper::send(const U8* const data, const U32 size)
111111
if (descriptor.fd == -1) {
112112
status = this->reopen();
113113
// if reopen wasn't successful, pass the that up to the caller
114-
if(status != SOCK_SUCCESS) {
114+
if (status != SOCK_SUCCESS) {
115115
return status;
116116
}
117117
// Refresh local copy after reopen
@@ -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, U32& size) {
161161
SocketIpStatus status = SOCK_SUCCESS;
162162
// Check for previously disconnected socket
163163
this->m_lock.lock();
@@ -199,10 +199,9 @@ void SocketComponentHelper::readLoop() {
199199
U32 size = static_cast<U32>(buffer.getSize());
200200
// recv blocks, so it may have been a while since its done an isOpened check
201201
status = this->recv(data, size);
202-
if ((status != SOCK_SUCCESS) && (status != SOCK_INTERRUPTED_TRY_AGAIN) && (status != SOCK_NO_DATA_AVAILABLE)) {
203-
Fw::Logger::log("[WARNING] Failed to recv from port with status %d and errno %d\n",
204-
status,
205-
errno);
202+
if ((status != SOCK_SUCCESS) && (status != SOCK_INTERRUPTED_TRY_AGAIN) &&
203+
(status != SOCK_NO_DATA_AVAILABLE)) {
204+
Fw::Logger::log("[WARNING] Failed to recv from port with status %d and errno %d\n", status, errno);
206205
this->close();
207206
buffer.setSize(0);
208207
} else {
@@ -215,7 +214,7 @@ void SocketComponentHelper::readLoop() {
215214
// This will loop until stopped. If auto-open is disabled, this will break when reopen returns disabled status
216215
while (this->running());
217216
// Close the socket
218-
this->close(); // Close the port entirely
217+
this->close(); // Close the port entirely
219218
}
220219

221220
void SocketComponentHelper::readTask(void* pointer) {

Drv/Ip/SocketComponentHelper.hpp

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
#ifndef DRV_SocketComponentHelper_HPP
1313
#define DRV_SocketComponentHelper_HPP
1414

15-
#include <Fw/Buffer/Buffer.hpp>
1615
#include <Drv/Ip/IpSocket.hpp>
17-
#include <Os/Task.hpp>
16+
#include <Fw/Buffer/Buffer.hpp>
1817
#include <Os/Mutex.hpp>
18+
#include <Os/Task.hpp>
1919

2020
namespace Drv {
2121
/**
@@ -27,12 +27,7 @@ namespace Drv {
2727
*/
2828
class SocketComponentHelper {
2929
public:
30-
enum OpenState{
31-
NOT_OPEN,
32-
OPENING,
33-
OPEN,
34-
SKIP
35-
};
30+
enum OpenState { NOT_OPEN, OPENING, OPEN, SKIP };
3631
/**
3732
* \brief constructs the socket read task
3833
*/
@@ -52,11 +47,12 @@ class SocketComponentHelper {
5247
* default behavior is to automatically open connections.
5348
*
5449
* \param name: name of the task
55-
* \param priority: priority of the started task. See: Os::Task::start. Default: TASK_PRIORITY_DEFAULT, not prioritized
50+
* \param priority: priority of the started task. See: Os::Task::start. Default: TASK_PRIORITY_DEFAULT, not
51+
* prioritized
5652
* \param stack: stack size provided to the task. See: Os::Task::start. Default: TASK_DEFAULT, posix threads default
5753
* \param cpuAffinity: cpu affinity provided to task. See: Os::Task::start. Default: TASK_DEFAULT, don't care
5854
*/
59-
void start(const Fw::StringBase &name,
55+
void start(const Fw::StringBase& name,
6056
const FwTaskPriorityType priority = Os::Task::TASK_PRIORITY_DEFAULT,
6157
const Os::Task::ParamType stack = Os::Task::TASK_DEFAULT,
6258
const Os::Task::ParamType cpuAffinity = Os::Task::TASK_DEFAULT);
@@ -73,7 +69,7 @@ class SocketComponentHelper {
7369
*/
7470
SocketIpStatus open();
7571

76-
/**
72+
/**
7773
* \brief check if IP socket has previously been opened
7874
*
7975
* Check if this IpSocket has previously been opened. In the case of Udp this will check for outgoing transmissions
@@ -87,8 +83,9 @@ class SocketComponentHelper {
8783
/**
8884
* \brief set socket to automatically open connections when true, or not when false
8985
*
90-
* When passed `true`, this instructs the socket to automatically open a socket and reopen socket failed connections. When passed `false`
91-
* the user must explicitly call the `open` method to open the socket initially and when a socket fails.
86+
* When passed `true`, this instructs the socket to automatically open a socket and reopen socket failed
87+
* connections. When passed `false` the user must explicitly call the `open` method to open the socket initially and
88+
* when a socket fails.
9289
*
9390
* \param auto_open: true to automatically open and reopen sockets, false otherwise
9491
*/
@@ -112,7 +109,7 @@ class SocketComponentHelper {
112109
* \param size: maximum size of data buffer to fill
113110
* \return status of the send, SOCK_DISCONNECTED to reopen, SOCK_SUCCESS on success, something else on error
114111
*/
115-
SocketIpStatus recv(U8* data, U32 &size);
112+
SocketIpStatus recv(U8* data, U32& size);
116113

117114
/**
118115
* \brief close the socket communications
@@ -202,7 +199,6 @@ class SocketComponentHelper {
202199
*/
203200
virtual void connected() = 0;
204201

205-
206202
/**
207203
* \brief a task designed to read from the socket and output incoming data
208204
*
@@ -214,9 +210,9 @@ class SocketComponentHelper {
214210
/**
215211
* \brief Re-open port if it has been disconnected
216212
*
217-
* This function is a helper to handle the situations where this code needs to safely reopen a socket. User code should
218-
* connect using the `open` call. This is for opening/reopening in situations where automatic open is performed
219-
* within this socket helper.
213+
* This function is a helper to handle the situations where this code needs to safely reopen a socket. User code
214+
* should connect using the `open` call. This is for opening/reopening in situations where automatic open is
215+
* performed within this socket helper.
220216
*
221217
* \return status of reconnect, SOCK_SUCCESS for success, something else on error
222218
*/
@@ -226,9 +222,9 @@ class SocketComponentHelper {
226222
Os::Task m_task;
227223
Os::Mutex m_lock;
228224
SocketDescriptor m_descriptor;
229-
bool m_reopen = true; //!< Force reopen on disconnect
230-
bool m_stop = true; //!< Stops the task when set to true
231-
OpenState m_open = OpenState::NOT_OPEN; //!< Have we successfully opened
225+
bool m_reopen = true; //!< Force reopen on disconnect
226+
bool m_stop = true; //!< Stops the task when set to true
227+
OpenState m_open = OpenState::NOT_OPEN; //!< Have we successfully opened
232228
};
233-
}
229+
} // namespace Drv
234230
#endif // DRV_SocketComponentHelper_HPP

0 commit comments

Comments
 (0)