Skip to content
This repository has been archived by the owner on Sep 7, 2020. It is now read-only.

Commit

Permalink
bcl: add InterfaceStateManager and its dependencies
Browse files Browse the repository at this point in the history
https://jira.prplfoundation.org/browse/PPM-18

Create InterfaceStateManager class and all its dependencies. Follow-up
commits will use this class in the ieee1905_transport process as a new
mechanism to detect changes in the state of network interfaces, based
on the new sockets API.

Signed-off-by: Mario Maz <[email protected]>
  • Loading branch information
mariomaz committed Aug 13, 2020
1 parent 56cf500 commit de2a052
Show file tree
Hide file tree
Showing 20 changed files with 1,628 additions and 0 deletions.
1 change: 1 addition & 0 deletions common/beerocks/bcl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ if (BUILD_TESTS)
${bcl_sources}
${MODULE_PATH}/unit_tests/network_utils_test.cpp
${MODULE_PATH}/unit_tests/event_loop_impl_test.cpp
${MODULE_PATH}/unit_tests/interface_state_manager_impl_test.cpp
${MODULE_PATH}/unit_tests/wireless_utils_test.cpp
)
add_executable(${TEST_PROJECT_NAME}
Expand Down
37 changes: 37 additions & 0 deletions common/beerocks/bcl/include/bcl/network/file_descriptor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* SPDX-License-Identifier: BSD-2-Clause-Patent
*
* SPDX-FileCopyrightText: 2020 the prplMesh contributors (see AUTHORS.md)
*
* This code is subject to the terms of the BSD+Patent license.
* See LICENSE file for more details.
*/

#ifndef BCL_NETWORK_FILE_DESCRIPTOR_H_
#define BCL_NETWORK_FILE_DESCRIPTOR_H_

namespace beerocks {
namespace net {

/**
* This interface models OS resources implementing the file descriptor interface.
*/
class FileDescriptor {
public:
static constexpr int invalid_descriptor = -1;

virtual ~FileDescriptor() = default;

/**
* @brief Returns the file descriptor.
*
* A file descriptor is a number that uniquely identifies an open file in the OS.
*
* @return File descriptor.
*/
virtual int fd() = 0;
};

} // namespace net
} // namespace beerocks

#endif /* BCL_NETWORK_FILE_DESCRIPTOR_H_ */
139 changes: 139 additions & 0 deletions common/beerocks/bcl/include/bcl/network/file_descriptor_impl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/* SPDX-License-Identifier: BSD-2-Clause-Patent
*
* SPDX-FileCopyrightText: 2020 the prplMesh contributors (see AUTHORS.md)
*
* This code is subject to the terms of the BSD+Patent license.
* See LICENSE file for more details.
*/

#ifndef BCL_NETWORK_FILE_DESCRIPTOR_IMPL_H_
#define BCL_NETWORK_FILE_DESCRIPTOR_IMPL_H_

#include "file_descriptor.h"

#include <unistd.h>

#include <easylogging++.h>

namespace beerocks {
namespace net {

/**
* File descriptor implementation.
* This class is basically a wrapper around an `int fd` that automatically closes the descriptor
* on destructor and prevents from having multiple copies of the file descriptor to avoid
* programming errors.
*
* This class will be aggregated by all classes modeling OS resources implementing the file
* descriptor interface.
*/
class FileDescriptorImpl : public FileDescriptor {
public:
/**
* @brief Class constructor.
*
* @param fd File descriptor value
*/
explicit FileDescriptorImpl(int fd) : m_fd(fd)
{
if (invalid_descriptor == fd) {
LOG(ERROR) << "Invalid file descriptor: " << strerror(errno);
}
}

/**
* @brief Copy constructor
*
* Delete copy constructor to avoid having multiple copies of the file descriptor
*/
FileDescriptorImpl(const FileDescriptorImpl &) = delete;

/**
* @brief Move constructor.
*
* A move constructor allows the resources owned by an rvalue object to be moved into an
* lvalue without creating its copy.
*/
FileDescriptorImpl(FileDescriptorImpl &&obj)
{
this->m_fd = obj.m_fd;
obj.m_fd = FileDescriptor::invalid_descriptor;
}

/**
* @brief Assignment operator
*
* Delete assignment operator to avoid having multiple copies of the file descriptor
*/
FileDescriptorImpl &operator=(const FileDescriptorImpl &) = delete;

/**
* @brief Move assignment operator
*
* The move assignment operator is used to transfer ownership of a file descriptor
*/
FileDescriptorImpl &operator=(FileDescriptorImpl &&obj)
{
// Self-assignment detection
if (&obj == this) {
return *this;
}

// Release any resource we're holding
close();

// Transfer ownership of obj.m_fd to m_fd
this->m_fd = obj.m_fd;

return *this;
}

/**
* @brief Class destructor.
*
* Close socket on destructor (if it is still open)
*/
~FileDescriptorImpl() override { close(); }

/**
* @brief Gets file descriptor.
*
* File descriptor can be used for example in:
* - select(), poll() or epoll() to wait for events on this descriptor.
* - send() family of functions to send data through the socket connection.
* - recv() family of functions to receive data from the socket connection.
*
* @return File descriptor value.
*/
int fd() override { return m_fd; }

private:
/**
* File descriptor value
*/
int m_fd;

/**
* @brief Closes file descriptor.
*
* If valid, closes file descriptor and then invalidates it.
*
* @return True on success and false otherwise (for example, if it was already closed)
*/
bool close()
{
if (FileDescriptor::invalid_descriptor == m_fd) {
return false;
}

int rc = ::close(m_fd);
m_fd = FileDescriptor::invalid_descriptor;

return (0 == rc);
}
};

} // namespace net
} // namespace beerocks

#endif /* BCL_NETWORK_FILE_DESCRIPTOR_IMPL_H_ */
38 changes: 38 additions & 0 deletions common/beerocks/bcl/include/bcl/network/interface_flags_reader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* SPDX-License-Identifier: BSD-2-Clause-Patent
*
* SPDX-FileCopyrightText: 2020 the prplMesh contributors (see AUTHORS.md)
*
* This code is subject to the terms of the BSD+Patent license.
* See LICENSE file for more details.
*/

#ifndef BCL_NETWORK_INTERFACE_FLAGS_READER_H_
#define BCL_NETWORK_INTERFACE_FLAGS_READER_H_

#include <stdint.h>

#include <string>

namespace beerocks {
namespace net {

class InterfaceFlagsReader {
public:
virtual ~InterfaceFlagsReader() = default;

/**
* @brief Reads interface flags.
*
* Reads the active flag word of the network interface with given index.
*
* @param[in] iface_name Interface name.
* @param[out] iface_flags Device flags (bitmask)
* @return True on success and false otherwise.
*/
virtual bool read_flags(const std::string &iface_name, uint16_t &iface_flags) = 0;
};

} // namespace net
} // namespace beerocks

#endif /* BCL_NETWORK_INTERFACE_FLAGS_READER_H_ */
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* SPDX-License-Identifier: BSD-2-Clause-Patent
*
* SPDX-FileCopyrightText: 2020 the prplMesh contributors (see AUTHORS.md)
*
* This code is subject to the terms of the BSD+Patent license.
* See LICENSE file for more details.
*/

#ifndef BCL_NETWORK_INTERFACE_FLAGS_READER_IMPL_H_
#define BCL_NETWORK_INTERFACE_FLAGS_READER_IMPL_H_

#include "interface_flags_reader.h"

namespace beerocks {
namespace net {

class InterfaceFlagsReaderImpl : public InterfaceFlagsReader {
public:
/**
* @brief Reads interface flags.
*
* @see InterfaceFlagsReader::read_flags
*
* This implementation uses ioctl() with SIOCGIFFLAGS to read device flags.
*/
bool read_flags(const std::string &iface_name, uint16_t &iface_flags) override;
};

} // namespace net
} // namespace beerocks

#endif /* BCL_NETWORK_INTERFACE_FLAGS_READER_IMPL_H_ */
28 changes: 28 additions & 0 deletions common/beerocks/bcl/include/bcl/network/interface_state_manager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* SPDX-License-Identifier: BSD-2-Clause-Patent
*
* SPDX-FileCopyrightText: 2020 the prplMesh contributors (see AUTHORS.md)
*
* This code is subject to the terms of the BSD+Patent license.
* See LICENSE file for more details.
*/

#ifndef BCL_NETWORK_INTERFACE_STATE_MANAGER_H_
#define BCL_NETWORK_INTERFACE_STATE_MANAGER_H_

#include "interface_state_monitor.h"
#include "interface_state_reader.h"

namespace beerocks {
namespace net {

/**
* The InterfaceStateManager is a facade interface for both the InterfaceStateMonitor and
* InterfaceStateReader interfaces together.
*/
class InterfaceStateManager : public InterfaceStateMonitor, public InterfaceStateReader {
};

} // namespace net
} // namespace beerocks

#endif /* BCL_NETWORK_INTERFACE_STATE_MANAGER_H_ */
Loading

0 comments on commit de2a052

Please sign in to comment.