cli: split evaluation of activation state

The code used to determine the activation state is useful for other
clients as well, let's split it into a dedicated function that can be
shared.
This commit is contained in:
Beniamino Galvani 2017-10-12 13:57:10 +02:00
parent 802116db43
commit aec559cea5

View file

@ -2089,17 +2089,20 @@ active_connection_state_reason_to_string (NMActiveConnectionStateReason reason)
g_return_val_if_reached (_("Invalid reason"));
}
static void
check_activated (ActivateConnectionInfo *info)
static NMActiveConnectionState
get_effective_activation_state (NMActiveConnection *active,
NMDevice *device,
const char **reason)
{
NmCli *nmc = info->nmc;
NMDevice *device = info->device;
NMActiveConnection *active = info->active;
NMActiveConnectionState ac_state;
NMActiveConnectionStateReason ac_reason;
NMDeviceState dev_state = NM_DEVICE_STATE_UNKNOWN;
NMDeviceStateReason dev_reason = NM_DEVICE_STATE_REASON_UNKNOWN;
const char *reason;
g_return_val_if_fail (active, NM_ACTIVE_CONNECTION_STATE_UNKNOWN);
g_return_val_if_fail (reason, NM_ACTIVE_CONNECTION_STATE_UNKNOWN);
*reason = NULL;
ac_reason = nm_active_connection_get_state_reason (active);
if (device) {
@ -2107,18 +2110,9 @@ check_activated (ActivateConnectionInfo *info)
dev_reason = nm_device_get_state_reason (device);
}
switch (nm_active_connection_get_state (active)) {
case NM_ACTIVE_CONNECTION_STATE_ACTIVATED:
if (nmc->nmc_config.print_output == NMC_PRINT_PRETTY)
nmc_terminal_erase_line ();
g_print (_("Connection successfully activated (D-Bus active path: %s)\n"),
nm_object_get_path (NM_OBJECT (active)));
activate_connection_info_finish (info);
break;
ac_state = nm_active_connection_get_state (active);
switch (ac_state) {
case NM_ACTIVE_CONNECTION_STATE_DEACTIVATED:
if ( !device
|| ac_reason != NM_ACTIVE_CONNECTION_STATE_REASON_DEVICE_DISCONNECTED
|| nm_device_get_active_connection (device) != active) {
@ -2127,55 +2121,79 @@ check_activated (ActivateConnectionInfo *info)
* - or, @ac_reason is specific
* - or, @device no longer references the current @active
* >> we complete with @ac_reason. */
reason = active_connection_state_reason_to_string (ac_reason);
*reason = active_connection_state_reason_to_string (ac_reason);
} else if ( dev_state <= NM_DEVICE_STATE_DISCONNECTED
|| dev_state >= NM_DEVICE_STATE_FAILED) {
/* (2)
* - not (1)
* - and, the device is no longer in an activated state,
* >> we complete with @dev_reason. */
reason = nmc_device_reason_to_string (dev_reason);
*reason = nmc_device_reason_to_string (dev_reason);
} else {
/* (3)
* we wait for the device go disconnect. We will get a better
* failure reason from the device (2). */
reason = NULL;
}
if (reason) {
g_string_printf (nmc->return_text, _("Error: Connection activation failed: %s"),
reason);
nmc->return_value = NMC_RESULT_ERROR_CON_ACTIVATION;
activate_connection_info_finish (info);
return NM_ACTIVE_CONNECTION_STATE_UNKNOWN;
}
break;
case NM_ACTIVE_CONNECTION_STATE_ACTIVATING:
/* activating master connection does not automatically activate any slaves, so their
* active connection state will not progress beyond ACTIVATING state.
* Monitor the device instead. */
if (nmc->secret_agent) {
NMRemoteConnection *connection = nm_active_connection_get_connection (active);
nm_secret_agent_simple_enable (NM_SECRET_AGENT_SIMPLE (nmc->secret_agent),
nm_connection_get_path (NM_CONNECTION (connection)));
}
if ( device
&& ( NM_IS_DEVICE_BOND (device)
|| NM_IS_DEVICE_TEAM (device)
|| NM_IS_DEVICE_BRIDGE (device))
&& dev_state >= NM_DEVICE_STATE_IP_CONFIG
&& dev_state <= NM_DEVICE_STATE_ACTIVATED) {
if (nmc->nmc_config.print_output == NMC_PRINT_PRETTY)
nmc_terminal_erase_line ();
g_print (_("Connection successfully activated (master waiting for slaves) (D-Bus active path: %s)\n"),
nm_object_get_path (NM_OBJECT (active)));
activate_connection_info_finish (info);
*reason = "master waiting for slaves";
return NM_ACTIVE_CONNECTION_STATE_ACTIVATED;
}
break;
default:
break;
}
return ac_state;
}
static void
check_activated (ActivateConnectionInfo *info)
{
NMActiveConnectionState ac_state;
NmCli *nmc = info->nmc;
const char *reason = NULL;
ac_state = get_effective_activation_state (info->active, info->device, &reason);
switch (ac_state) {
case NM_ACTIVE_CONNECTION_STATE_ACTIVATED:
if (nmc->nmc_config.print_output == NMC_PRINT_PRETTY)
nmc_terminal_erase_line ();
if (reason) {
g_print (_("Connection successfully activated (%s) (D-Bus active path: %s)\n"),
reason,
nm_object_get_path (NM_OBJECT (info->active)));
} else {
g_print (_("Connection successfully activated (D-Bus active path: %s)\n"),
nm_object_get_path (NM_OBJECT (info->active)));
}
activate_connection_info_finish (info);
break;
case NM_ACTIVE_CONNECTION_STATE_DEACTIVATED:
nm_assert (reason);
g_string_printf (nmc->return_text, _("Error: Connection activation failed: %s"),
reason);
nmc->return_value = NMC_RESULT_ERROR_CON_ACTIVATION;
activate_connection_info_finish (info);
break;
case NM_ACTIVE_CONNECTION_STATE_ACTIVATING:
if (nmc->secret_agent) {
NMRemoteConnection *connection = nm_active_connection_get_connection (info->active);
nm_secret_agent_simple_enable (NM_SECRET_AGENT_SIMPLE (nmc->secret_agent),
nm_connection_get_path (NM_CONNECTION (connection)));
}
break;
default:
break;
}