Skip to content

Commit

Permalink
IAccessPointController::GetCapabilities return AccessPointOperationSt…
Browse files Browse the repository at this point in the history
…atus.
  • Loading branch information
abeltrano committed Feb 23, 2024
1 parent 6b11928 commit 61996fe
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 52 deletions.
38 changes: 20 additions & 18 deletions src/common/service/NetRemoteService.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -188,15 +188,17 @@ IAccessPointToNetRemoteAccessPointResultItem(IAccessPoint& accessPoint)
return MakeInvalidAccessPointResultItem();
}

Dot11AccessPointCapabilities dot11AccessPointCapabilities{};
try {
auto ieee80211AccessPointCapabilities = accessPointController->GetCapabilities();
dot11AccessPointCapabilities = ToDot11AccessPointCapabilities(ieee80211AccessPointCapabilities);
} catch (const AccessPointControllerException& apce) {
LOGE << std::format("Failed to get capabilities for access point {} ({})", interfaceName, apce.what());
// Obtain capabilities of the access point.
Ieee80211AccessPointCapabilities ieee80211AccessPointCapabilities{};
operationStatus = accessPointController->GetCapabilities(ieee80211AccessPointCapabilities);
if (!operationStatus) {
LOGE << std::format("Failed to get capabilities for access point {}", interfaceName);
return MakeInvalidAccessPointResultItem();
}

Dot11AccessPointCapabilities dot11AccessPointCapabilities{};
dot11AccessPointCapabilities = ToDot11AccessPointCapabilities(ieee80211AccessPointCapabilities);

const bool isEnabled{ operationalState == AccessPointOperationalState::Enabled };

// Populate the result item.
Expand Down Expand Up @@ -346,14 +348,15 @@ NetRemoteService::WifiAccessPointSetPhyType([[maybe_unused]] grpc::ServerContext
auto ieee80211Protocol = FromDot11PhyType(request->phytype());

// Check if Ieee80211 protocol is supported by AP.
try {
auto accessPointCapabilities = accessPointController->GetCapabilities();
const auto& supportedIeee80211Protocols = accessPointCapabilities.Protocols;
if (std::ranges::find(supportedIeee80211Protocols, ieee80211Protocol) == std::cend(supportedIeee80211Protocols)) {
return HandleFailure(request, result, WifiAccessPointOperationStatusCode::WifiAccessPointOperationStatusCodeOperationNotSupported, std::format("PHY type not supported by access point {}", request->accesspointid()));
}
} catch (const AccessPointControllerException& apce) {
return HandleFailure(request, result, WifiAccessPointOperationStatusCode::WifiAccessPointOperationStatusCodeInternalError, std::format("Failed to get capabilities for access point {} ({})", request->accesspointid(), apce.what()));
Ieee80211AccessPointCapabilities accessPointCapabilities{};
auto operationStatus = accessPointController->GetCapabilities(accessPointCapabilities);
if (!operationStatus) {
return HandleFailure(request, result, operationStatus.Code, std::format("Failed to get capabilities for access point {}", request->accesspointid()));
}

const auto& supportedIeee80211Protocols = accessPointCapabilities.Protocols;
if (std::ranges::find(supportedIeee80211Protocols, ieee80211Protocol) == std::cend(supportedIeee80211Protocols)) {
return HandleFailure(request, result, WifiAccessPointOperationStatusCode::WifiAccessPointOperationStatusCodeOperationNotSupported, std::format("PHY type not supported by access point {}", request->accesspointid()));
}

// Set the Ieee80211 protocol.
Expand Down Expand Up @@ -409,10 +412,9 @@ NetRemoteService::WifiAccessPointSetFrequencyBands([[maybe_unused]] grpc::Server

// Obtain capabilities of the access point.
Ieee80211AccessPointCapabilities accessPointCapabilities{};
try {
accessPointCapabilities = accessPointController->GetCapabilities();
} catch (const AccessPointControllerException& apce) {
return HandleFailure(request, result, WifiAccessPointOperationStatusCode::WifiAccessPointOperationStatusCodeInternalError, std::format("Failed to get capabilities for access point {} ({})", request->accesspointid(), apce.what()));
auto operationStatus = accessPointController->GetCapabilities(accessPointCapabilities);
if (!operationStatus) {
return HandleFailure(request, result, operationStatus.Code, std::format("Failed to get capabilities for access point {}", request->accesspointid()));
}

// Check if requested bands are supported by the AP.
Expand Down
3 changes: 3 additions & 0 deletions src/common/wifi/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ target_sources(wifi-core
AccessPointController.cxx
AccessPointControllerException.cxx
AccessPointOperationStatus.cxx
Ieee80211AccessPointCapabilities.cxx
PUBLIC
FILE_SET HEADERS
BASE_DIRS ${WIFI_CORE_PUBLIC_INCLUDE}
Expand All @@ -25,6 +26,8 @@ target_sources(wifi-core
)

target_link_libraries(wifi-core
PRIVATE
magic_enum::magic_enum
PUBLIC
notstd
)
Expand Down
44 changes: 44 additions & 0 deletions src/common/wifi/core/Ieee80211AccessPointCapabilities.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

#include <sstream>
#include <string>

#include <magic_enum.hpp>
#include <microsoft/net/wifi/Ieee80211AccessPointCapabilities.hxx>

using namespace Microsoft::Net::Wifi;

std::string
Ieee80211AccessPointCapabilities::ToString() const
{
std::ostringstream result{};

result << "Protocols: ";
for (const auto& protocol : Protocols) {
result << magic_enum::enum_name(protocol);
result << ' ';
}

result << '\n'
<< "Frequency Bands: ";
for (const auto& frequencyBand : FrequencyBands) {
result << magic_enum::enum_name(frequencyBand);
result << ' ';
}

result << '\n'
<< "Akm Suites: ";
for (const auto& akmSuite : AkmSuites) {
result << magic_enum::enum_name(akmSuite);
result << ' ';
}

result << '\n'
<< "Cipher Suites: ";
for (const auto& cipherSuite : CipherSuites) {
result << magic_enum::enum_name(cipherSuite);
result << ' ';
}
result << '\n';

return result.str();
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,11 @@ struct IAccessPointController
/**
* @brief Get the capabilities of the access point.
*
* @return Ieee80211AccessPointCapabilities
* @param ieee80211AccessPointCapabilities The value to store the capabilities.
* @return AccessPointOperationStatus
*/
virtual Ieee80211AccessPointCapabilities
GetCapabilities() = 0;
virtual AccessPointOperationStatus
GetCapabilities(Ieee80211AccessPointCapabilities& ieee80211AccessPointCapabilities) = 0;

/**
* @brief Set the operational state of the access point.
Expand All @@ -92,7 +93,7 @@ struct IAccessPointController
* @brief Set the Ieee80211 protocol of the access point.
*
* @param ieeeProtocol The Ieee80211 protocol to be set.
* @return AccessPointOperationStatus
* @return AccessPointOperationStatus
*/
virtual AccessPointOperationStatus
SetProtocol(Ieee80211Protocol ieeeProtocol) = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#ifndef IEEE_80211_ACCESS_POINT_CAPABILITIES_HXX
#define IEEE_80211_ACCESS_POINT_CAPABILITIES_HXX

#include <string>
#include <vector>

#include <microsoft/net/wifi/Ieee80211.hxx>
Expand All @@ -17,6 +18,14 @@ struct Ieee80211AccessPointCapabilities
std::vector<Ieee80211FrequencyBand> FrequencyBands;
std::vector<Ieee80211AkmSuite> AkmSuites;
std::vector<Ieee80211CipherSuite> CipherSuites;

/**
* @brief Get a string representation of the capabilities.
*
* @return std::string
*/
std::string
ToString() const;
};
} // namespace Microsoft::Net::Wifi

Expand Down
48 changes: 30 additions & 18 deletions src/linux/wifi/core/AccessPointControllerLinux.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -150,32 +150,44 @@ IeeeFrequencyBandToHostapdBand(Ieee80211FrequencyBand ieeeFrequencyBand)
}
} // namespace detail

Ieee80211AccessPointCapabilities
AccessPointControllerLinux::GetCapabilities()
AccessPointOperationStatus
AccessPointControllerLinux::GetCapabilities(Ieee80211AccessPointCapabilities& ieee80211AccessPointCapabilities)
{
auto wiphy = Nl80211Wiphy::FromInterfaceName(GetInterfaceName());
AccessPointOperationStatus status{};

const auto wiphy = Nl80211Wiphy::FromInterfaceName(GetInterfaceName());
if (!wiphy.has_value()) {
throw AccessPointControllerException(std::format("Failed to get wiphy for interface {}", GetInterfaceName()));
}
status.Code = AccessPointOperationStatusCode::AccessPointInvalid;
status.Message = std::format("Failed to get wiphy for interface {}", GetInterfaceName());
} else {
Ieee80211AccessPointCapabilities capabilities;

Ieee80211AccessPointCapabilities capabilities;
// Convert protocols.
capabilities.Protocols = detail::Nl80211WiphyToIeee80211Protocols(wiphy.value());

// Convert protocols.
capabilities.Protocols = detail::Nl80211WiphyToIeee80211Protocols(wiphy.value());
// Convert frequency bands.
capabilities.FrequencyBands = std::vector<Ieee80211FrequencyBand>(std::size(wiphy->Bands));
std::ranges::transform(std::views::keys(wiphy->Bands), std::begin(capabilities.FrequencyBands), detail::Nl80211BandToIeee80211FrequencyBand);

// Convert frequency bands.
capabilities.FrequencyBands = std::vector<Ieee80211FrequencyBand>(std::size(wiphy->Bands));
std::ranges::transform(std::views::keys(wiphy->Bands), std::begin(capabilities.FrequencyBands), detail::Nl80211BandToIeee80211FrequencyBand);
// Convert AKM suites.
capabilities.AkmSuites = std::vector<Ieee80211AkmSuite>(std::size(wiphy->AkmSuites));
std::ranges::transform(wiphy->AkmSuites, std::begin(capabilities.AkmSuites), detail::Nl80211AkmSuiteToIeee80211AkmSuite);

// Convert AKM suites.
capabilities.AkmSuites = std::vector<Ieee80211AkmSuite>(std::size(wiphy->AkmSuites));
std::ranges::transform(wiphy->AkmSuites, std::begin(capabilities.AkmSuites), detail::Nl80211AkmSuiteToIeee80211AkmSuite);
// Convert cipher suites.
capabilities.CipherSuites = std::vector<Ieee80211CipherSuite>(std::size(wiphy->CipherSuites));
std::ranges::transform(wiphy->CipherSuites, std::begin(capabilities.CipherSuites), detail::Nl80211CipherSuiteToIeee80211CipherSuite);

// Convert cipher suites.
capabilities.CipherSuites = std::vector<Ieee80211CipherSuite>(std::size(wiphy->CipherSuites));
std::ranges::transform(wiphy->CipherSuites, std::begin(capabilities.CipherSuites), detail::Nl80211CipherSuiteToIeee80211CipherSuite);
ieee80211AccessPointCapabilities = std::move(capabilities);
status = AccessPointOperationStatus::MakeSucceeded();
}

return capabilities;
if (status.Succeeded()) {
LOGD << std::format("Got capabilities for interface {}", GetInterfaceName());
} else {
LOGE << std::format("Failed to get capabilities for interface {} ({} - {})", GetInterfaceName(), magic_enum::enum_name(status.Code), status.Message);
}

return status;
}

AccessPointOperationStatus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,13 @@ struct AccessPointControllerLinux :
GetOperationalState(AccessPointOperationalState& operationalState) override;

/**
* @brief Get the Capabilities object
* @brief Get the capabilities of the access point.
*
* @return Ieee80211AccessPointCapabilities
* @param ieee80211AccessPointCapabilities The value to store the capabilities.
* @return AccessPointOperationStatus
*/
Ieee80211AccessPointCapabilities
GetCapabilities() override;
AccessPointOperationStatus
GetCapabilities(Ieee80211AccessPointCapabilities& ieee80211AccessPointCapabilities) override;

/**
* @brief Set the operational state of the access point.
Expand Down
7 changes: 4 additions & 3 deletions tests/unit/wifi/helpers/AccessPointControllerTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,15 @@ AccessPointControllerTest::GetOperationalState(AccessPointOperationalState &oper
return AccessPointOperationStatus::MakeSucceeded();
}

Ieee80211AccessPointCapabilities
AccessPointControllerTest::GetCapabilities()
AccessPointOperationStatus
AccessPointControllerTest::GetCapabilities(Ieee80211AccessPointCapabilities &ieee80211AccessPointCapabilities)
{
if (AccessPoint == nullptr) {
throw std::runtime_error("AccessPointControllerTest::GetCapabilities called with null AccessPoint");
}

return AccessPoint->Capabilities;
ieee80211AccessPointCapabilities = AccessPoint->Capabilities;
return AccessPointOperationStatus::MakeSucceeded();
}

AccessPointOperationStatus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,19 @@ struct AccessPointControllerTest final :
* @brief Get the access point operational state.
*
* @param operationalState The value to store the operational state.
* @return AccessPointOperationStatus
* @return AccessPointOperationStatus
*/
AccessPointOperationStatus
GetOperationalState(AccessPointOperationalState& operationalState) override;
GetOperationalState(AccessPointOperationalState &operationalState) override;

/**
* @brief Get the capabilities of the access point.
*
* @return Ieee80211AccessPointCapabilities
* @param ieee80211AccessPointCapabilities The value to store the capabilities.
* @return AccessPointOperationStatus
*/
Ieee80211AccessPointCapabilities
GetCapabilities() override;
AccessPointOperationStatus
GetCapabilities(Ieee80211AccessPointCapabilities &ieee80211AccessPointCapabilities) override;

/**
* @brief Set the operational state of the access point.
Expand Down

0 comments on commit 61996fe

Please sign in to comment.