Skip to content
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
1 change: 1 addition & 0 deletions .github/workflows/format-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ jobs:
- name: "Check C++ Formatting"
env:
CHECKED_DIRS: >-
Drv
Fw/Buffer
Fw/Cmd
Fw/Com
Expand Down
29 changes: 15 additions & 14 deletions Drv/Ip/IpSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,48 +9,49 @@
// acknowledged.
//
// ======================================================================
#include <cstring>
#include <sys/time.h>
#include <Drv/Ip/IpSocket.hpp>
#include <Fw/Types/Assert.hpp>
#include <Fw/FPrimeBasicTypes.hpp>
#include <Fw/Types/Assert.hpp>
#include <Fw/Types/StringUtils.hpp>
#include <sys/time.h>
#include <cstring>

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

#ifdef TGT_OS_TYPE_VXWORKS
#include <socket.h>
#include <inetLib.h>
#include <errnoLib.h>
#include <fioLib.h>
#include <hostLib.h>
#include <inetLib.h>
#include <ioLib.h>
#include <vxWorks.h>
#include <sockLib.h>
#include <fioLib.h>
#include <taskLib.h>
#include <socket.h>
#include <sysLib.h>
#include <errnoLib.h>
#include <taskLib.h>
#include <vxWorks.h>
#include <cstring>
#elif defined TGT_OS_TYPE_LINUX || TGT_OS_TYPE_DARWIN
#include <arpa/inet.h>
#include <sys/socket.h>
#include <unistd.h>
#include <cerrno>
#include <arpa/inet.h>
#else
#error OS not supported for IP Socket Communications
#endif


namespace Drv {

IpSocket::IpSocket() : m_timeoutSeconds(0), m_timeoutMicroseconds(0), m_port(0) {
::memset(m_hostname, 0, sizeof(m_hostname));
}

SocketIpStatus IpSocket::configure(const char* const hostname, const U16 port, const U32 timeout_seconds, const U32 timeout_microseconds) {
SocketIpStatus IpSocket::configure(const char* const hostname,

Check notice

Code scanning / CodeQL

Use of basic integral type Note

hostname uses the basic integral type char rather than a typedef with size and signedness.

Check notice

Code scanning / CodeQL

Long function without assertion Note

All functions of more than 10 lines should have at least one assertion.
const U16 port,
const U32 timeout_seconds,
const U32 timeout_microseconds) {
FW_ASSERT(timeout_microseconds < 1000000, static_cast<FwAssertArgType>(timeout_microseconds));
FW_ASSERT(this->isValidPort(port), static_cast<FwAssertArgType>(port));
FW_ASSERT(hostname != nullptr);
Expand Down Expand Up @@ -165,11 +166,11 @@
}

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


I32 bytes_received_or_status; // Stores the return value from recvProtocol

// Loop primarily for EINTR. Other conditions should lead to an earlier exit.
Expand Down
6 changes: 4 additions & 2 deletions Drv/Ip/IpSocket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
#define DRV_IP_IPHELPER_HPP_

#include <Fw/FPrimeBasicTypes.hpp>
#include <config/IpCfg.hpp>
#include <Os/Mutex.hpp>
#include <config/IpCfg.hpp>

namespace Drv {

struct SocketDescriptor final {
int fd = -1; //!< Used for all sockets to track the communication file descriptor

Check notice

Code scanning / CodeQL

Use of basic integral type Note

fd uses the basic integral type int rather than a typedef with size and signedness.
int serverFd = -1; //!< Used for server sockets to track the listening file descriptor

Check notice

Code scanning / CodeQL

Use of basic integral type Note

serverFd uses the basic integral type int rather than a typedef with size and signedness.
};

/**
Expand Down Expand Up @@ -57,7 +57,7 @@
class IpSocket {
public:
IpSocket();
virtual ~IpSocket() {};

Check notice

Code scanning / CodeQL

More than one statement per line Note

This line contains 2 statements; only one is allowed.
/**
* \brief configure the ip socket with host and transmission timeouts
*
Expand All @@ -76,7 +76,9 @@
* \param send_timeout_microseconds: send timeout microseconds portion. Must be less than 1000000
* \return status of configure
*/
virtual SocketIpStatus configure(const char* hostname, const U16 port, const U32 send_timeout_seconds,
virtual SocketIpStatus configure(const char* hostname,
const U16 port,
const U32 send_timeout_seconds,
const U32 send_timeout_microseconds);

/**
Expand Down
11 changes: 5 additions & 6 deletions Drv/Ip/SocketComponentHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@

SocketComponentHelper::~SocketComponentHelper() {}

void SocketComponentHelper::start(const Fw::StringBase& name,

Check notice

Code scanning / CodeQL

Long function without assertion Note

All functions of more than 10 lines should have at least one assertion.
const FwTaskPriorityType priority,
const Os::Task::ParamType stack,
const Os::Task::ParamType cpuAffinity) {
FW_ASSERT(m_task.getState() == Os::Task::State::NOT_STARTED); // It is a coding error to start this task multiple times
FW_ASSERT(m_task.getState() ==
Os::Task::State::NOT_STARTED); // It is a coding error to start this task multiple times
this->m_stop = false;
// Note: the first step is for the IP socket to open the port
Os::Task::Arguments arguments(name, SocketComponentHelper::readTask, this, priority, stack, cpuAffinity);
Expand All @@ -45,7 +46,6 @@
} else {
local_open = OpenState::SKIP;
}

}
if (local_open == OpenState::OPENING) {
FW_ASSERT(this->m_descriptor.fd == -1); // Ensure we are not opening an opened socket
Expand Down Expand Up @@ -199,10 +199,9 @@
U32 size = static_cast<U32>(buffer.getSize());
// recv blocks, so it may have been a while since its done an isOpened check
status = this->recv(data, size);
if ((status != SOCK_SUCCESS) && (status != SOCK_INTERRUPTED_TRY_AGAIN) && (status != SOCK_NO_DATA_AVAILABLE)) {
Fw::Logger::log("[WARNING] Failed to recv from port with status %d and errno %d\n",
status,
errno);
if ((status != SOCK_SUCCESS) && (status != SOCK_INTERRUPTED_TRY_AGAIN) &&
(status != SOCK_NO_DATA_AVAILABLE)) {
Fw::Logger::log("[WARNING] Failed to recv from port with status %d and errno %d\n", status, errno);
this->close();
buffer.setSize(0);
} else {
Expand Down
28 changes: 12 additions & 16 deletions Drv/Ip/SocketComponentHelper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
#ifndef DRV_SocketComponentHelper_HPP
#define DRV_SocketComponentHelper_HPP

#include <Fw/Buffer/Buffer.hpp>
#include <Drv/Ip/IpSocket.hpp>
#include <Os/Task.hpp>
#include <Fw/Buffer/Buffer.hpp>
#include <Os/Mutex.hpp>
#include <Os/Task.hpp>

namespace Drv {
/**
Expand All @@ -27,12 +27,7 @@ namespace Drv {
*/
class SocketComponentHelper {
public:
enum OpenState{
NOT_OPEN,
OPENING,
OPEN,
SKIP
};
enum OpenState { NOT_OPEN, OPENING, OPEN, SKIP };
/**
* \brief constructs the socket read task
*/
Expand All @@ -52,7 +47,8 @@ class SocketComponentHelper {
* default behavior is to automatically open connections.
*
* \param name: name of the task
* \param priority: priority of the started task. See: Os::Task::start. Default: TASK_PRIORITY_DEFAULT, not prioritized
* \param priority: priority of the started task. See: Os::Task::start. Default: TASK_PRIORITY_DEFAULT, not
* prioritized
* \param stack: stack size provided to the task. See: Os::Task::start. Default: TASK_DEFAULT, posix threads default
* \param cpuAffinity: cpu affinity provided to task. See: Os::Task::start. Default: TASK_DEFAULT, don't care
*/
Expand Down Expand Up @@ -87,8 +83,9 @@ class SocketComponentHelper {
/**
* \brief set socket to automatically open connections when true, or not when false
*
* When passed `true`, this instructs the socket to automatically open a socket and reopen socket failed connections. When passed `false`
* the user must explicitly call the `open` method to open the socket initially and when a socket fails.
* When passed `true`, this instructs the socket to automatically open a socket and reopen socket failed
* connections. When passed `false` the user must explicitly call the `open` method to open the socket initially and
* when a socket fails.
*
* \param auto_open: true to automatically open and reopen sockets, false otherwise
*/
Expand Down Expand Up @@ -202,7 +199,6 @@ class SocketComponentHelper {
*/
virtual void connected() = 0;


/**
* \brief a task designed to read from the socket and output incoming data
*
Expand All @@ -214,9 +210,9 @@ class SocketComponentHelper {
/**
* \brief Re-open port if it has been disconnected
*
* This function is a helper to handle the situations where this code needs to safely reopen a socket. User code should
* connect using the `open` call. This is for opening/reopening in situations where automatic open is performed
* within this socket helper.
* This function is a helper to handle the situations where this code needs to safely reopen a socket. User code
* should connect using the `open` call. This is for opening/reopening in situations where automatic open is
* performed within this socket helper.
*
* \return status of reconnect, SOCK_SUCCESS for success, something else on error
*/
Expand All @@ -230,5 +226,5 @@ class SocketComponentHelper {
bool m_stop = true; //!< Stops the task when set to true
OpenState m_open = OpenState::NOT_OPEN; //!< Have we successfully opened
};
}
} // namespace Drv
#endif // DRV_SocketComponentHelper_HPP
15 changes: 7 additions & 8 deletions Drv/Ip/TcpClientSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,26 @@
// ======================================================================

#include <Drv/Ip/TcpClientSocket.hpp>
#include <Fw/FPrimeBasicTypes.hpp>
#include <Fw/Logger/Logger.hpp>
#include <Fw/Types/Assert.hpp>
#include <Fw/FPrimeBasicTypes.hpp>

#ifdef TGT_OS_TYPE_VXWORKS
#include <socket.h>
#include <inetLib.h>
#include <errnoLib.h>
#include <fioLib.h>
#include <hostLib.h>
#include <inetLib.h>
#include <ioLib.h>
#include <vxWorks.h>
#include <sockLib.h>
#include <taskLib.h>
#include <socket.h>
#include <sysLib.h>
#include <errnoLib.h>
#include <taskLib.h>
#include <vxWorks.h>
#include <cstring>
#elif defined TGT_OS_TYPE_LINUX || TGT_OS_TYPE_DARWIN
#include <arpa/inet.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#else
#error OS not supported for IP Socket Communications
#endif
Expand All @@ -46,7 +46,6 @@ bool TcpClientSocket::isValidPort(U16 port) {
return port != 0;
}


SocketIpStatus TcpClientSocket::openProtocol(SocketDescriptor& socketDescriptor) {
int socketFd = -1;
struct sockaddr_in address;
Expand Down
3 changes: 2 additions & 1 deletion Drv/Ip/TcpClientSocket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
#ifndef DRV_TCPCLIENT_TCPHELPER_HPP_
#define DRV_TCPCLIENT_TCPHELPER_HPP_

#include <Fw/FPrimeBasicTypes.hpp>
#include <Drv/Ip/IpSocket.hpp>
#include <Fw/FPrimeBasicTypes.hpp>
#include <config/IpCfg.hpp>

namespace Drv {
Expand All @@ -29,6 +29,7 @@ class TcpClientSocket : public IpSocket {
* \brief Constructor for client socket tcp implementation
*/
TcpClientSocket();

protected:
/**
* \brief Check if the given port is valid for the socket
Expand Down
17 changes: 9 additions & 8 deletions Drv/Ip/TcpServerSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,26 @@
//
// ======================================================================
#include <Drv/Ip/TcpServerSocket.hpp>
#include <Fw/FPrimeBasicTypes.hpp>
#include <Fw/Logger/Logger.hpp>
#include <Fw/Types/Assert.hpp>
#include <Fw/FPrimeBasicTypes.hpp>

#ifdef TGT_OS_TYPE_VXWORKS
#include <socket.h>
#include <inetLib.h>
#include <errnoLib.h>
#include <fioLib.h>
#include <hostLib.h>
#include <inetLib.h>
#include <ioLib.h>
#include <vxWorks.h>
#include <sockLib.h>
#include <taskLib.h>
#include <socket.h>
#include <sysLib.h>
#include <errnoLib.h>
#include <taskLib.h>
#include <vxWorks.h>
#include <cstring>
#elif defined TGT_OS_TYPE_LINUX || TGT_OS_TYPE_DARWIN
#include <arpa/inet.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#else
#error OS not supported for IP Socket Communications
#endif
Expand Down Expand Up @@ -73,11 +73,12 @@
}

socklen_t size = sizeof(address);
if (::getsockname(serverFd, reinterpret_cast<struct sockaddr*>(&address), &size) == -1) {

Check warning

Code scanning / CodeQL

Side effect in a Boolean expression Warning

This Boolean expression is not side-effect free.
::close(serverFd);
return SOCK_FAILED_TO_READ_BACK_PORT;
}
// TCP requires listening on the socket. Since we only expect a single client, set the TCP backlog (second argument) to 1 to prevent queuing of multiple clients.
// TCP requires listening on the socket. Since we only expect a single client, set the TCP backlog (second argument)
// to 1 to prevent queuing of multiple clients.
if (::listen(serverFd, 1) < 0) {
::close(serverFd);
return SOCK_FAILED_TO_LISTEN; // What we have here is a failure to communicate
Expand Down
5 changes: 1 addition & 4 deletions Drv/Ip/TcpServerSocket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
#ifndef DRV_TCPSERVER_TCPHELPER_HPP_
#define DRV_TCPSERVER_TCPHELPER_HPP_

#include <Fw/FPrimeBasicTypes.hpp>
#include <Drv/Ip/IpSocket.hpp>
#include <Fw/FPrimeBasicTypes.hpp>
#include <config/IpCfg.hpp>

namespace Drv {
Expand Down Expand Up @@ -85,9 +85,6 @@ class TcpServerSocket : public IpSocket {
* \return: size of data received, or -1 on error.
*/
I32 recvProtocol(const SocketDescriptor& socketDescriptor, U8* const data, const U32 size) override;



};
} // namespace Drv

Expand Down
Loading
Loading