core: refactor functions to use NM_UTILS_LOOKUP*()

Also use NM_UTILS_LOOKUP_STR() macro, which can transform
unknown values to their numerical representation using
alloca().
This commit is contained in:
Thomas Haller
2016-01-24 11:30:36 +01:00
parent 6c6ab10dea
commit ba187e054d
3 changed files with 62 additions and 116 deletions

View File

@@ -181,39 +181,25 @@ nm_platform_try_get (void)
/******************************************************************/ /******************************************************************/
/** /**
* nm_platform_error_to_string: * _nm_platform_error_to_string:
* @error_code: the error code to stringify. * @error_code: the error code to stringify.
* *
* Returns: A string representation of the error. * Returns: A string representation of the error.
* For negative numbers, this function interprets * For negative numbers, this function interprets
* the code as -errno. * the code as -errno.
* For invalid (positive) numbers it returns NULL.
*/ */
const char * NM_UTILS_LOOKUP_STR_DEFINE (_nm_platform_error_to_string, NMPlatformError,
nm_platform_error_to_string (NMPlatformError error) ( val < 0 ? g_strerror (- ((int) val)) : NULL ),
{ NM_UTILS_LOOKUP_STR_ITEM (NM_PLATFORM_ERROR_SUCCESS, "success"),
switch (error) { NM_UTILS_LOOKUP_STR_ITEM (NM_PLATFORM_ERROR_BUG, "bug"),
case NM_PLATFORM_ERROR_SUCCESS: NM_UTILS_LOOKUP_STR_ITEM (NM_PLATFORM_ERROR_UNSPECIFIED, "unspecified"),
return "success"; NM_UTILS_LOOKUP_STR_ITEM (NM_PLATFORM_ERROR_NOT_FOUND, "not-found"),
case NM_PLATFORM_ERROR_BUG: NM_UTILS_LOOKUP_STR_ITEM (NM_PLATFORM_ERROR_EXISTS, "exists"),
return "bug"; NM_UTILS_LOOKUP_STR_ITEM (NM_PLATFORM_ERROR_WRONG_TYPE, "wrong-type"),
case NM_PLATFORM_ERROR_UNSPECIFIED: NM_UTILS_LOOKUP_STR_ITEM (NM_PLATFORM_ERROR_NOT_SLAVE, "not-slave"),
return "unspecified"; NM_UTILS_LOOKUP_STR_ITEM (NM_PLATFORM_ERROR_NO_FIRMWARE, "no-firmware"),
case NM_PLATFORM_ERROR_NOT_FOUND: );
return "not-found";
case NM_PLATFORM_ERROR_EXISTS:
return "exists";
case NM_PLATFORM_ERROR_WRONG_TYPE:
return "wrong-type";
case NM_PLATFORM_ERROR_NOT_SLAVE:
return "not-slave";
case NM_PLATFORM_ERROR_NO_FIRMWARE:
return "no-firmware";
default:
if (error < 0)
return g_strerror (- ((int) error));
return "unknown";
}
}
/******************************************************************/ /******************************************************************/

View File

@@ -666,7 +666,8 @@ _nm_platform_uint8_inv (guint8 scope)
const char *nm_link_type_to_string (NMLinkType link_type); const char *nm_link_type_to_string (NMLinkType link_type);
const char *nm_platform_error_to_string (NMPlatformError error); const char *_nm_platform_error_to_string (NMPlatformError error);
#define nm_platform_error_to_string(error) NM_UTILS_LOOKUP_STR (_nm_platform_error_to_string, error)
gboolean nm_platform_sysctl_set (NMPlatform *self, const char *path, const char *value); gboolean nm_platform_sysctl_set (NMPlatform *self, const char *path, const char *value);
char *nm_platform_sysctl_get (NMPlatform *self, const char *path); char *nm_platform_sysctl_get (NMPlatform *self, const char *path);

View File

@@ -767,21 +767,13 @@ nm_vpn_connection_get_service (NMVpnConnection *self)
return nm_setting_vpn_get_service_type (s_vpn); return nm_setting_vpn_get_service_type (s_vpn);
} }
static const char * NM_UTILS_LOOKUP_STR_DEFINE_STATIC (_vpn_plugin_failure_to_string, NMVpnPluginFailure,
vpn_plugin_failure_to_string (NMVpnPluginFailure failure) NULL,
{ NM_UTILS_LOOKUP_STR_ITEM (NM_VPN_PLUGIN_FAILURE_LOGIN_FAILED, "login-failed"),
switch (failure) { NM_UTILS_LOOKUP_STR_ITEM (NM_VPN_PLUGIN_FAILURE_CONNECT_FAILED, "connect-failed"),
case NM_VPN_PLUGIN_FAILURE_LOGIN_FAILED: NM_UTILS_LOOKUP_STR_ITEM (NM_VPN_PLUGIN_FAILURE_BAD_IP_CONFIG, "bad-ip-config"),
return "login-failed"; );
case NM_VPN_PLUGIN_FAILURE_CONNECT_FAILED: #define vpn_plugin_failure_to_string(failure) NM_UTILS_LOOKUP_STR (_vpn_plugin_failure_to_string, failure)
return "connect-failed";
case NM_VPN_PLUGIN_FAILURE_BAD_IP_CONFIG:
return "bad-ip-config";
default:
break;
}
return "unknown";
}
static void static void
plugin_failed (NMVpnConnection *self, guint reason) plugin_failed (NMVpnConnection *self, guint reason)
@@ -803,81 +795,48 @@ plugin_failed (NMVpnConnection *self, guint reason)
} }
} }
static const char * NM_UTILS_LOOKUP_STR_DEFINE_STATIC (_vpn_service_state_to_string, NMVpnServiceState,
vpn_service_state_to_string (NMVpnServiceState state) NULL,
{ NM_UTILS_LOOKUP_STR_ITEM (NM_VPN_SERVICE_STATE_INIT, "init"),
switch (state) { NM_UTILS_LOOKUP_STR_ITEM (NM_VPN_SERVICE_STATE_SHUTDOWN, "shutdown"),
case NM_VPN_SERVICE_STATE_INIT: NM_UTILS_LOOKUP_STR_ITEM (NM_VPN_SERVICE_STATE_STARTING, "starting"),
return "init"; NM_UTILS_LOOKUP_STR_ITEM (NM_VPN_SERVICE_STATE_STARTED, "started"),
case NM_VPN_SERVICE_STATE_SHUTDOWN: NM_UTILS_LOOKUP_STR_ITEM (NM_VPN_SERVICE_STATE_STOPPING, "stopping"),
return "shutdown"; NM_UTILS_LOOKUP_STR_ITEM (NM_VPN_SERVICE_STATE_STOPPED, "stopped"),
case NM_VPN_SERVICE_STATE_STARTING: );
return "starting"; #define vpn_service_state_to_string(state) NM_UTILS_LOOKUP_STR (_vpn_service_state_to_string, state)
case NM_VPN_SERVICE_STATE_STARTED:
return "started";
case NM_VPN_SERVICE_STATE_STOPPING:
return "stopping";
case NM_VPN_SERVICE_STATE_STOPPED:
return "stopped";
default:
break;
}
return "unknown";
}
static const char *state_table[] = { NM_UTILS_LOOKUP_STR_DEFINE_STATIC (_vpn_state_to_string, VpnState,
[STATE_UNKNOWN] = "unknown", NULL,
[STATE_WAITING] = "waiting", NM_UTILS_LOOKUP_STR_ITEM (STATE_UNKNOWN, "unknown"),
[STATE_PREPARE] = "prepare", NM_UTILS_LOOKUP_STR_ITEM (STATE_WAITING, "waiting"),
[STATE_NEED_AUTH] = "need-auth", NM_UTILS_LOOKUP_STR_ITEM (STATE_PREPARE, "prepare"),
[STATE_CONNECT] = "connect", NM_UTILS_LOOKUP_STR_ITEM (STATE_NEED_AUTH, "need-auth"),
[STATE_IP_CONFIG_GET] = "ip-config-get", NM_UTILS_LOOKUP_STR_ITEM (STATE_CONNECT, "connect"),
[STATE_PRE_UP] = "pre-up", NM_UTILS_LOOKUP_STR_ITEM (STATE_IP_CONFIG_GET, "ip-config-get"),
[STATE_ACTIVATED] = "activated", NM_UTILS_LOOKUP_STR_ITEM (STATE_PRE_UP, "pre-up"),
[STATE_DEACTIVATING] = "deactivating", NM_UTILS_LOOKUP_STR_ITEM (STATE_ACTIVATED, "activated"),
[STATE_DISCONNECTED] = "disconnected", NM_UTILS_LOOKUP_STR_ITEM (STATE_DEACTIVATING, "deactivating"),
[STATE_FAILED] = "failed", NM_UTILS_LOOKUP_STR_ITEM (STATE_DISCONNECTED, "disconnected"),
}; NM_UTILS_LOOKUP_STR_ITEM (STATE_FAILED, "failed"),
);
#define vpn_state_to_string(state) NM_UTILS_LOOKUP_STR (_vpn_state_to_string, state)
static const char * NM_UTILS_LOOKUP_STR_DEFINE_STATIC (_vpn_reason_to_string, NMVpnConnectionStateReason,
vpn_state_to_string (VpnState state) NULL,
{ NM_UTILS_LOOKUP_STR_ITEM (NM_VPN_CONNECTION_STATE_REASON_NONE, "none"),
if ((gsize) state < G_N_ELEMENTS (state_table)) NM_UTILS_LOOKUP_STR_ITEM (NM_VPN_CONNECTION_STATE_REASON_USER_DISCONNECTED, "user-disconnected"),
return state_table[state]; NM_UTILS_LOOKUP_STR_ITEM (NM_VPN_CONNECTION_STATE_REASON_DEVICE_DISCONNECTED, "device-disconnected"),
return "unknown"; NM_UTILS_LOOKUP_STR_ITEM (NM_VPN_CONNECTION_STATE_REASON_SERVICE_STOPPED, "service-stopped"),
} NM_UTILS_LOOKUP_STR_ITEM (NM_VPN_CONNECTION_STATE_REASON_IP_CONFIG_INVALID, "ip-config-invalid"),
NM_UTILS_LOOKUP_STR_ITEM (NM_VPN_CONNECTION_STATE_REASON_CONNECT_TIMEOUT, "connect-timeout"),
static const char * NM_UTILS_LOOKUP_STR_ITEM (NM_VPN_CONNECTION_STATE_REASON_SERVICE_START_TIMEOUT, "service-start-timeout"),
vpn_reason_to_string (NMVpnConnectionStateReason reason) NM_UTILS_LOOKUP_STR_ITEM (NM_VPN_CONNECTION_STATE_REASON_SERVICE_START_FAILED, "service-start-failed"),
{ NM_UTILS_LOOKUP_STR_ITEM (NM_VPN_CONNECTION_STATE_REASON_NO_SECRETS, "no-secrets"),
switch (reason) { NM_UTILS_LOOKUP_STR_ITEM (NM_VPN_CONNECTION_STATE_REASON_LOGIN_FAILED, "login-failed"),
case NM_VPN_CONNECTION_STATE_REASON_NONE: NM_UTILS_LOOKUP_STR_ITEM (NM_VPN_CONNECTION_STATE_REASON_CONNECTION_REMOVED, "connection-removed"),
return "none"; );
case NM_VPN_CONNECTION_STATE_REASON_USER_DISCONNECTED: #define vpn_reason_to_string(reason) NM_UTILS_LOOKUP_STR (_vpn_reason_to_string, reason)
return "user-disconnected";
case NM_VPN_CONNECTION_STATE_REASON_DEVICE_DISCONNECTED:
return "device-disconnected";
case NM_VPN_CONNECTION_STATE_REASON_SERVICE_STOPPED:
return "service-stopped";
case NM_VPN_CONNECTION_STATE_REASON_IP_CONFIG_INVALID:
return "ip-config-invalid";
case NM_VPN_CONNECTION_STATE_REASON_CONNECT_TIMEOUT:
return "connect-timeout";
case NM_VPN_CONNECTION_STATE_REASON_SERVICE_START_TIMEOUT:
return "service-start-timeout";
case NM_VPN_CONNECTION_STATE_REASON_SERVICE_START_FAILED:
return "service-start-failed";
case NM_VPN_CONNECTION_STATE_REASON_NO_SECRETS:
return "no-secrets";
case NM_VPN_CONNECTION_STATE_REASON_LOGIN_FAILED:
return "login-failed";
case NM_VPN_CONNECTION_STATE_REASON_CONNECTION_REMOVED:
return "connection-removed";
default:
break;
}
return "unknown";
}
static void static void
plugin_state_changed (NMVpnConnection *self, NMVpnServiceState new_service_state) plugin_state_changed (NMVpnConnection *self, NMVpnServiceState new_service_state)