Skip to content

Commit

Permalink
Merge pull request #227 from microsoft/clienable
Browse files Browse the repository at this point in the history
Add commands to enable and disable APs from netremote-cli
  • Loading branch information
abeltrano authored Mar 16, 2024
2 parents 221dc50 + 9c0c261 commit 8e98c96
Show file tree
Hide file tree
Showing 13 changed files with 449 additions and 94 deletions.
2 changes: 2 additions & 0 deletions src/common/tools/cli/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ target_link_libraries(${PROJECT_NAME}-cli
plog::plog
PUBLIC
${PROJECT_NAME}-client
wifi-core
wifi-core-adapter-dot11
)

install(
Expand Down
40 changes: 40 additions & 0 deletions src/common/tools/cli/NetRemoteCli.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ NetRemoteCli::AddSubcommandWifi(CLI::App* parent)

// Sub-commands.
m_cliAppWifiEnumerateAccessPoints = AddSubcommandWifiEnumerateAccessPoints(wifiApp);
m_cliAppWifiAccessPointEnable = AddSubcommandWifiAccessPointEnable(wifiApp);
m_cliAppWifiAccessPointDisable = AddSubcommandWifiAccessPointDisable(wifiApp);

return wifiApp;
}
Expand All @@ -102,6 +104,32 @@ NetRemoteCli::AddSubcommandWifiEnumerateAccessPoints(CLI::App* parent)
return cliAppWifiEnumerateAccessPoints;
}

CLI::App*
NetRemoteCli::AddSubcommandWifiAccessPointEnable(CLI::App* parent)
{
auto* cliAppWifiAccessPointEnable = parent->add_subcommand("access-point-enable", "Enable a Wi-Fi access point");
cliAppWifiAccessPointEnable->alias("ap-enable");
cliAppWifiAccessPointEnable->callback([this] {
OnWifiAccessPointEnable();
});

return cliAppWifiAccessPointEnable;
}

CLI::App*
NetRemoteCli::AddSubcommandWifiAccessPointDisable(CLI::App* parent)
{
auto* cliAppWifiAccessPointDisable = parent->add_subcommand("access-point-disable", "Disable a Wi-Fi access point");
cliAppWifiAccessPointDisable->alias("ap-disable");
cliAppWifiAccessPointDisable->callback([this] {
OnWifiAccessPointEnable();
});

cliAppWifiAccessPointDisable->add_option("id", m_cliData->WifiAccessPointId, "The identifier of the access point to disable")->required();

return cliAppWifiAccessPointDisable;
}

void
NetRemoteCli::OnServerAddressChanged(const std::string& serverAddressArg)
{
Expand Down Expand Up @@ -129,3 +157,15 @@ NetRemoteCli::OnWifiEnumerateAccessPoints()
{
m_cliHandler->HandleCommandWifiEnumerateAccessPoints();
}

void
NetRemoteCli::OnWifiAccessPointEnable()
{
m_cliHandler->HandleCommandWifiAccessPointEnable(m_cliData->WifiAccessPointId);
}

void
NetRemoteCli::OnWifiAccessPointDisable()
{
m_cliHandler->HandleCommandWifiAccessPointDisable(m_cliData->WifiAccessPointId);
}
43 changes: 43 additions & 0 deletions src/common/tools/cli/NetRemoteCliHandler.cxx
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@

#include <memory>
#include <optional>
#include <string_view>
#include <utility>

#include <microsoft/net/remote/INetRemoteCliHandlerOperations.hxx>
#include <microsoft/net/remote/NetRemoteCli.hxx>
#include <microsoft/net/remote/NetRemoteCliHandler.hxx>
#include <microsoft/net/remote/NetRemoteServerConnection.hxx>
#include <microsoft/net/wifi/Ieee80211AccessPointConfiguration.hxx>
#include <plog/Log.h>

using namespace Microsoft::Net::Remote;
Expand Down Expand Up @@ -51,3 +54,43 @@ NetRemoteCliHandler::HandleCommandWifiEnumerateAccessPoints()
LOGD << "Executing command WifiEnumerateAccessPoints";
m_operations->WifiEnumerateAccessPoints();
}

using Microsoft::Net::Wifi::Ieee80211AccessPointConfiguration;

void
NetRemoteCliHandler::HandleCommandWifiAccessPointEnable(std::string_view accessPointId, const std::optional<Ieee80211AccessPointConfiguration>& ieee80211AccessPointConfiguration)
{
if (!m_operations) {
LOGE << "No operations instance available to handle command";
return;
}

auto parentStrong{ GetParentStrongRef() };
if (parentStrong == nullptr) {
LOGW << "Parent cli object is no longer valid, aborting command";
return;
}

LOGD << "Executing command WifiAccessPointEnable";

m_operations->WifiAccessPointEnable(accessPointId, ieee80211AccessPointConfiguration);
}

void
NetRemoteCliHandler::HandleCommandWifiAccessPointDisable(std::string_view accessPointId)
{
if (!m_operations) {
LOGE << "No operations instance available to handle command";
return;
}

auto parentStrong{ GetParentStrongRef() };
if (parentStrong == nullptr) {
LOGW << "Parent cli object is no longer valid, aborting command";
return;
}

LOGD << "Executing command WifiAccessPointDisable";

m_operations->WifiAccessPointDisable(accessPointId);
}
76 changes: 76 additions & 0 deletions src/common/tools/cli/NetRemoteCliHandlerOperations.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <microsoft/net/remote/protocol/NetRemoteService.grpc.pb.h>
#include <microsoft/net/remote/protocol/NetRemoteWifi.pb.h>
#include <microsoft/net/remote/protocol/WifiCore.pb.h>
#include <microsoft/net/wifi/Ieee80211Dot11Adapters.hxx>
#include <plog/Log.h>

using namespace Microsoft::Net::Remote;
Expand Down Expand Up @@ -136,6 +137,81 @@ NetRemoteCliHandlerOperations::WifiEnumerateAccessPoints()
}
}

void
NetRemoteCliHandlerOperations::WifiAccessPointEnable(std::string_view accessPointId, const std::optional<Microsoft::Net::Wifi::Ieee80211AccessPointConfiguration>& ieee80211AccessPointConfiguration)
{
WifiAccessPointEnableRequest request{};
WifiAccessPointEnableResult result{};
grpc::ClientContext clientContext{};

request.set_accesspointid(std::string(accessPointId));

// Populate access point configuration if present.
if (ieee80211AccessPointConfiguration.has_value()) {
auto& dot11AccessPointConfiguration = *request.mutable_configuration();

// Populate SSID if present.
if (ieee80211AccessPointConfiguration->Ssid.has_value()) {
dot11AccessPointConfiguration.mutable_ssid()->set_name(ieee80211AccessPointConfiguration->Ssid.value());
}

// Populate PHY type if present.
if (ieee80211AccessPointConfiguration->PhyType.has_value()) {
const auto dot11PhyType = ToDot11PhyType(ieee80211AccessPointConfiguration->PhyType.value());
dot11AccessPointConfiguration.set_phytype(dot11PhyType);
}

// Populate pairwise cipher suites if present.
if (!std::empty(ieee80211AccessPointConfiguration->PairwiseCipherSuites)) {
auto dot11PairwiseCipherSuites = ToDot11CipherSuiteConfigurations(ieee80211AccessPointConfiguration->PairwiseCipherSuites);
*dot11AccessPointConfiguration.mutable_pairwiseciphersuites() = {
std::make_move_iterator(std::begin(dot11PairwiseCipherSuites)),
std::make_move_iterator(std::end(dot11PairwiseCipherSuites))
};
}

// Populate authentication algorithms if present.
if (!std::empty(ieee80211AccessPointConfiguration->AuthenticationAlgorithms)) {
auto dot11AuthenticationAlgorithms = ToDot11AuthenticationAlgorithms(ieee80211AccessPointConfiguration->AuthenticationAlgorithms);
*dot11AccessPointConfiguration.mutable_authenticationalgorithms() = {
std::make_move_iterator(std::begin(dot11AuthenticationAlgorithms)),
std::make_move_iterator(std::end(dot11AuthenticationAlgorithms))
};
}

// Populate frequency bands if present.
if (!std::empty(ieee80211AccessPointConfiguration->FrequencyBands)) {
auto dot11FrequencyBands = ToDot11FrequencyBands(ieee80211AccessPointConfiguration->FrequencyBands);
*dot11AccessPointConfiguration.mutable_frequencybands() = {
std::make_move_iterator(std::begin(dot11FrequencyBands)),
std::make_move_iterator(std::end(dot11FrequencyBands))
};
}
}

auto status = m_connection->Client->WifiAccessPointEnable(&clientContext, request, &result);
if (!status.ok()) {
LOGE << std::format("Failed to enable WiFi access point, error={} details={} message={}", magic_enum::enum_name(status.error_code()), status.error_details(), status.error_message());
return;
}
}

void
NetRemoteCliHandlerOperations::WifiAccessPointDisable(std::string_view accessPointId)
{
WifiAccessPointDisableRequest request{};
WifiAccessPointDisableResult result{};
grpc::ClientContext clientContext{};

request.set_accesspointid(std::string(accessPointId));

auto status = m_connection->Client->WifiAccessPointDisable(&clientContext, request, &result);
if (!status.ok()) {
LOGE << std::format("Failed to disable WiFi access point, error={} details={} message={}", magic_enum::enum_name(status.error_code()), status.error_details(), status.error_message());
return;
}
}

std::unique_ptr<INetRemoteCliHandlerOperations>
NetRemoteCliHandlerOperationsFactory::Create(std::shared_ptr<NetRemoteServerConnection> connection)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
#define I_NET_REMOTE_CLI_HANDLER_OPERATIONS_HXX

#include <memory>
#include <optional>
#include <string_view>

#include <microsoft/net/remote/NetRemoteServerConnection.hxx>
#include <microsoft/net/wifi/Ieee80211AccessPointConfiguration.hxx>

namespace Microsoft::Net::Remote
{
Expand All @@ -21,15 +24,37 @@ struct INetRemoteCliHandlerOperations
* Prevent copying and moving of INetRemoteCliHandlerOperations objects.
*/
INetRemoteCliHandlerOperations(const INetRemoteCliHandlerOperations&) = delete;
INetRemoteCliHandlerOperations& operator=(const INetRemoteCliHandlerOperations&) = delete;

INetRemoteCliHandlerOperations(INetRemoteCliHandlerOperations&&) = delete;
INetRemoteCliHandlerOperations& operator=(INetRemoteCliHandlerOperations&&) = delete;

INetRemoteCliHandlerOperations&
operator=(const INetRemoteCliHandlerOperations&) = delete;

INetRemoteCliHandlerOperations&
operator=(INetRemoteCliHandlerOperations&&) = delete;

/**
* @brief Enumerate available WiFi access points.
*/
virtual void
WifiEnumerateAccessPoints() = 0;

/**
* @brief Enable the specified WiFi access point.
*
* @param accessPointId The identifier of the access point to enable.
* @param ieee80211AccessPointConfiguration The optional configuration to apply to the access point.
*/
virtual void
WifiAccessPointEnable(std::string_view accessPointId, const std::optional<Microsoft::Net::Wifi::Ieee80211AccessPointConfiguration>& ieee80211AccessPointConfiguration) = 0;

/**
* @brief Disable the specified WiFi access point.
*
* @param accessPointId The identifier of the access point to disable.
*/
virtual void
WifiAccessPointDisable(std::string_view accessPointId) = 0;
};

/**
Expand All @@ -42,12 +67,17 @@ struct INetRemoteCliHandlerOperationsFactory
virtual ~INetRemoteCliHandlerOperationsFactory() = default;

/**
* Prevent copying and moving of INetRemoteCliHandlerOperationsFactory objects.
* Prevent copying and moving of INetRemoteCliHandlerOperationsFactory objects.
*/
INetRemoteCliHandlerOperationsFactory(const INetRemoteCliHandlerOperationsFactory&) = delete;
INetRemoteCliHandlerOperationsFactory& operator=(const INetRemoteCliHandlerOperationsFactory&) = delete;

INetRemoteCliHandlerOperationsFactory(INetRemoteCliHandlerOperationsFactory&&) = delete;
INetRemoteCliHandlerOperationsFactory& operator=(INetRemoteCliHandlerOperationsFactory&&) = delete;

INetRemoteCliHandlerOperationsFactory&
operator=(const INetRemoteCliHandlerOperationsFactory&) = delete;

INetRemoteCliHandlerOperationsFactory&
operator=(INetRemoteCliHandlerOperationsFactory&&) = delete;

/**
* @brief Create a new INetRemoteCliHandlerOperationsFactory instance with the specified server connection.
Expand Down
32 changes: 32 additions & 0 deletions src/common/tools/cli/include/microsoft/net/remote/NetRemoteCli.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,24 @@ private:
CLI::App*
AddSubcommandWifiEnumerateAccessPoints(CLI::App* parent);

/**
* @brief Add the 'wifi ap-enable' sub-command.
*
* @param parent The parent app to add the sub-command to.
* @return CLI::App*
*/
CLI::App*
AddSubcommandWifiAccessPointEnable(CLI::App* parent);

/**
* @brief Add the 'wifi ap-disable' sub-command.
*
* @param parent The parent app to add the sub-command to.
* @return CLI::App*
*/
CLI::App*
AddSubcommandWifiAccessPointDisable(CLI::App* parent);

/**
* @brief Handle the 'server' option.
*
Expand All @@ -108,6 +126,18 @@ private:
void
OnWifiEnumerateAccessPoints();

/**
* @brief Handle the 'wifi ap-enable' command.
*/
void
OnWifiAccessPointEnable();

/**
* @brief Handle the 'wifi ap-disable' command.
*/
void
OnWifiAccessPointDisable();

private:
std::shared_ptr<NetRemoteCliData> m_cliData;
std::shared_ptr<NetRemoteCliHandler> m_cliHandler;
Expand All @@ -118,6 +148,8 @@ private:
CLI::Option* m_cliAppServerAddress{ nullptr };
CLI::App* m_cliAppWifi{ nullptr };
CLI::App* m_cliAppWifiEnumerateAccessPoints{ nullptr };
CLI::App* m_cliAppWifiAccessPointEnable{ nullptr };
CLI::App* m_cliAppWifiAccessPointDisable{ nullptr };
};
} // namespace Microsoft::Net::Remote

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ struct NetRemoteCliData
{
std::string ServerAddress{ Protocol::NetRemoteProtocol::AddressDefault };
NetRemoteCommandId Command{ NetRemoteCommandId::None };

std::string WifiAccessPointId{};
};
} // namespace Microsoft::Net::Remote

Expand Down
Loading

0 comments on commit 8e98c96

Please sign in to comment.