Rewrite ActiveConnectionStateReason as a struct with an enum Value

This commit is contained in:
Vibhav Pant
2023-09-13 17:43:25 +05:30
parent 42be2b343d
commit f4c487188a
4 changed files with 87 additions and 69 deletions
@@ -24,50 +24,54 @@
namespace nearby {
namespace linux {
namespace networkmanager {
std::ostream &operator<<(
std::ostream &stream,
const ActiveConnection::ActiveConnectionStateReason &reason) {
switch (reason) {
case ActiveConnection::kStateReasonUnknown:
return stream << "The reason for the active connection state change is "
"unknown.";
case ActiveConnection::kStateReasonNone:
return stream
<< "No reason was given for the active connection state change.";
case ActiveConnection::kStateReasonUserDisconnected:
return stream << "The active connection changed state because the user "
"disconnected it.";
case ActiveConnection::kStateReasonDeviceDisconnected:
return stream << "The active connection changed state because the "
"device it was "
"using was disconnected.";
case ActiveConnection::kStateReasonServiceStopped:
return stream << "The service providing the VPN connection was stopped.";
case ActiveConnection::kStateReasonIPConfigInvalid:
return stream << "The IP config of the active connection was invalid.";
case ActiveConnection::kStateReasonConnectTimeout:
return stream << "The connection attempt to the VPN service timed out.";
case ActiveConnection::kStateReasonServiceStartTimeout:
return stream
<< "A timeout occurred while starting the service providing the "
"VPN connection.";
case ActiveConnection::kStateReasonServiceStartFailed:
return stream
<< "Starting the service providing the VPN connection failed.";
case ActiveConnection::kStateReasonNoSecrets:
return stream
<< "Necessary secrets for the connection were not provided.";
case ActiveConnection::kStateReasonLoginFailed:
return stream << "Authentication to the server failed.";
case ActiveConnection::kStateReasonConnectionRemoved:
return stream << "The connection was deleted from settings.";
case ActiveConnection::kStateReasonDependencyFailed:
return stream
<< "Master connection of this connection failed to activate.";
case ActiveConnection::kStateReasonDeviceRealizeFailed:
return stream << "Could not create the software device link.";
case ActiveConnection::kStateReasonDeviceRemoved:
return stream << "The device this connection depended on disappeared.";
std::string ActiveConnection::ActiveConnectionStateReason::ToString() const {
switch (value) {
case ActiveConnection::ActiveConnectionStateReason::kStateReasonUnknown:
return "The reason for the active connection state change is "
"unknown.";
case ActiveConnection::ActiveConnectionStateReason::kStateReasonNone:
return "No reason was given for the active connection state change.";
case ActiveConnection::ActiveConnectionStateReason::
kStateReasonUserDisconnected:
return "The active connection changed state because the user "
"disconnected it.";
case ActiveConnection::ActiveConnectionStateReason::
kStateReasonDeviceDisconnected:
return "The active connection changed state because the "
"device it was "
"using was disconnected.";
case ActiveConnection::ActiveConnectionStateReason::
kStateReasonServiceStopped:
return "The service providing the VPN connection was stopped.";
case ActiveConnection::ActiveConnectionStateReason::
kStateReasonIPConfigInvalid:
return "The IP config of the active connection was invalid.";
case ActiveConnection::ActiveConnectionStateReason::
kStateReasonConnectTimeout:
return "The connection attempt to the VPN service timed out.";
case ActiveConnection::ActiveConnectionStateReason::
kStateReasonServiceStartTimeout:
return "A timeout occurred while starting the service providing the "
"VPN connection.";
case ActiveConnection::ActiveConnectionStateReason::
kStateReasonServiceStartFailed:
return "Starting the service providing the VPN connection failed.";
case ActiveConnection::ActiveConnectionStateReason::kStateReasonNoSecrets:
return "Necessary secrets for the connection were not provided.";
case ActiveConnection::ActiveConnectionStateReason::kStateReasonLoginFailed:
return "Authentication to the server failed.";
case ActiveConnection::ActiveConnectionStateReason::
kStateReasonConnectionRemoved:
return "The connection was deleted from settings.";
case ActiveConnection::ActiveConnectionStateReason::
kStateReasonDependencyFailed:
return "Master connection of this connection failed to activate.";
case ActiveConnection::ActiveConnectionStateReason::
kStateReasonDeviceRealizeFailed:
return "Could not create the software device link.";
case ActiveConnection::ActiveConnectionStateReason::
kStateReasonDeviceRemoved:
return "The device this connection depended on disappeared.";
}
}
@@ -38,22 +38,27 @@ class ActiveConnection
kStateDeactivating = 3,
kStateDeactivated = 4
};
enum ActiveConnectionStateReason {
kStateReasonUnknown = 0,
kStateReasonNone = 1,
kStateReasonUserDisconnected = 2,
kStateReasonDeviceDisconnected = 3,
kStateReasonServiceStopped = 4,
kStateReasonIPConfigInvalid = 5,
kStateReasonConnectTimeout = 6,
kStateReasonServiceStartTimeout = 7,
kStateReasonServiceStartFailed = 8,
kStateReasonNoSecrets = 9,
kStateReasonLoginFailed = 10,
kStateReasonConnectionRemoved = 11,
kStateReasonDependencyFailed = 12,
kStateReasonDeviceRealizeFailed = 13,
kStateReasonDeviceRemoved = 14,
struct ActiveConnectionStateReason {
enum Value {
kStateReasonUnknown = 0,
kStateReasonNone = 1,
kStateReasonUserDisconnected = 2,
kStateReasonDeviceDisconnected = 3,
kStateReasonServiceStopped = 4,
kStateReasonIPConfigInvalid = 5,
kStateReasonConnectTimeout = 6,
kStateReasonServiceStartTimeout = 7,
kStateReasonServiceStartFailed = 8,
kStateReasonNoSecrets = 9,
kStateReasonLoginFailed = 10,
kStateReasonConnectionRemoved = 11,
kStateReasonDependencyFailed = 12,
kStateReasonDeviceRealizeFailed = 13,
kStateReasonDeviceRemoved = 14,
};
Value value{kStateReasonUnknown};
std::string ToString() const;
};
ActiveConnection(const ActiveConnection &) = delete;
@@ -66,7 +71,8 @@ class ActiveConnection
std::move(active_connection_path)),
system_bus_(std::move(system_bus)),
state_(kStateUnknown),
reason_(kStateReasonUnknown) {
reason_{ActiveConnection::ActiveConnectionStateReason::
kStateReasonUnknown} {
registerProxy();
try {
auto state = State();
@@ -86,8 +92,12 @@ class ActiveConnection
if (state >= kStateUnknown && state <= kStateDeactivated) {
state_ = static_cast<ActiveConnectionState>(state);
}
if (reason >= kStateReasonUnknown && reason <= kStateReasonDeviceRemoved) {
reason_ = static_cast<ActiveConnectionStateReason>(reason);
if (reason >= ActiveConnection::ActiveConnectionStateReason::
kStateReasonUnknown &&
reason <= ActiveConnection::ActiveConnectionStateReason::
kStateReasonDeviceRemoved) {
reason_ = ActiveConnectionStateReason{
static_cast<ActiveConnectionStateReason::Value>(reason)};
}
}
@@ -218,7 +218,7 @@ bool NetworkManagerWifiHotspotMedium::StartWifiHotspot(
<< ": timed out while waiting for connection "
<< active_conn->getObjectPath()
<< " to be activated, last NMActiveConnectionStateReason: "
<< reason.value();
<< reason->ToString();
DisconnectWifiHotspot();
return false;
}
@@ -238,7 +238,7 @@ api::WifiConnectionStatus NetworkManagerWifiMedium::ConnectToNetwork(
if (auto ret = sd_id128_randomize(&id); ret < 0) {
NEARBY_LOGS(ERROR) << __func__
<< ": could not generation a connection UUID";
<< ": could not generate a connection UUID";
return api::WifiConnectionStatus::kUnknown;
}
@@ -297,7 +297,7 @@ api::WifiConnectionStatus NetworkManagerWifiMedium::ConnectToNetwork(
<< __func__ << ": " << getObjectPath()
<< ": timed out while waiting for connection " << active_conn_path
<< " to be activated, last NMActiveConnectionStateReason: "
<< reason.value();
<< reason->ToString();
return api::WifiConnectionStatus::kUnknown;
}
@@ -305,9 +305,13 @@ api::WifiConnectionStatus NetworkManagerWifiMedium::ConnectToNetwork(
NEARBY_LOGS(ERROR) << __func__ << ": " << getObjectPath() << ": connection "
<< active_conn_path
<< " failed to activate, NMActiveConnectionStateReason:"
<< *reason;
if (*reason == networkmanager::ActiveConnection::kStateReasonNoSecrets ||
*reason == networkmanager::ActiveConnection::kStateReasonLoginFailed)
<< reason->ToString();
if (reason->value ==
networkmanager::ActiveConnection::ActiveConnectionStateReason::
kStateReasonNoSecrets ||
reason->value ==
networkmanager::ActiveConnection::ActiveConnectionStateReason::
kStateReasonLoginFailed)
return api::WifiConnectionStatus::kAuthFailure;
}