This repository has been archived by the owner on Sep 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bcl: add InterfaceStateManager and its dependencies
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
Showing
20 changed files
with
1,628 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
139
common/beerocks/bcl/include/bcl/network/file_descriptor_impl.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
38
common/beerocks/bcl/include/bcl/network/interface_flags_reader.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ */ |
32 changes: 32 additions & 0 deletions
32
common/beerocks/bcl/include/bcl/network/interface_flags_reader_impl.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
28
common/beerocks/bcl/include/bcl/network/interface_state_manager.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ */ |
Oops, something went wrong.