Commit 3e3e41c4 authored by Christian Kellner's avatar Christian Kellner
Browse files

enun: BoltPolicy is an str/enum on bus/obj

Convert BoltPolicy usage to be an enum for object properties (manager
and device) and a string on the DBus (properties, and Enroll arg).
parent 3dc7b75d
......@@ -205,7 +205,7 @@ bolt_device_get_property (GObject *object,
break;
case PROP_POLICY:
g_value_set_uint (value, dev->policy);
g_value_set_enum (value, dev->policy);
break;
case PROP_HAVE_KEY:
......@@ -297,7 +297,7 @@ bolt_device_set_property (GObject *object,
break;
case PROP_POLICY:
dev->policy = g_value_get_uint (value);
dev->policy = g_value_get_enum (value);
break;
case PROP_HAVE_KEY:
......@@ -424,10 +424,9 @@ bolt_device_class_init (BoltDeviceClass *klass)
G_PARAM_STATIC_STRINGS);
props[PROP_POLICY] =
g_param_spec_uint ("policy",
g_param_spec_enum ("policy",
"Policy", NULL,
0,
BOLT_POLICY_LAST,
BOLT_TYPE_POLICY,
BOLT_POLICY_DEFAULT,
G_PARAM_READWRITE |
G_PARAM_STATIC_STRINGS);
......
......@@ -246,7 +246,7 @@ bolt_manager_get_property (GObject *object,
break;
case PROP_POLICY:
g_value_set_uint (value, mgr->policy);
g_value_set_enum (value, mgr->policy);
break;
case PROP_SECURITY:
......@@ -297,8 +297,9 @@ bolt_manager_class_init (BoltManagerClass *klass)
G_PARAM_STATIC_STRINGS);
props[PROP_POLICY] =
g_param_spec_uint ("default-policy", "DefaultPolicy", "DefaultPolicy",
0, G_MAXUINT32, 0,
g_param_spec_enum ("default-policy", "DefaultPolicy", "DefaultPolicy",
BOLT_TYPE_POLICY,
BOLT_POLICY_AUTO,
G_PARAM_READABLE |
G_PARAM_STATIC_STRINGS);
......@@ -1446,14 +1447,14 @@ enroll_device_done (GObject *device,
if (ok)
{
GVariant *params;
guint32 p;
const char *str;
BoltPolicy policy;
guint64 now;
params = g_dbus_method_invocation_get_parameters (inv);
g_variant_get_child (params, 1, "u", &p);
g_variant_get_child (params, 1, "&s", &str);
policy = p;
policy = bolt_enum_from_string (BOLT_TYPE_POLICY, str, NULL);
if (policy == BOLT_POLICY_DEFAULT)
policy = mgr->policy;
......@@ -1489,12 +1490,12 @@ handle_enroll_device (BoltExported *obj,
BoltManager *mgr;
const char *uid;
BoltSecurity level;
guint32 policy;
const char *policy;
mgr = BOLT_MANAGER (obj);
g_variant_get_child (params, 0, "&s", &uid);
g_variant_get_child (params, 1, "u", &policy);
g_variant_get_child (params, 1, "&s", &policy);
dev = manager_find_device_by_uid (mgr, uid, &error);
if (dev == NULL)
......@@ -1503,10 +1504,10 @@ handle_enroll_device (BoltExported *obj,
return TRUE;
}
if (!bolt_policy_validate ((BoltPolicy) policy))
if (bolt_enum_from_string (BOLT_TYPE_POLICY, policy, &error) == -1)
{
g_dbus_method_invocation_return_error (inv, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
"invalid policy: %" G_GUINT32_FORMAT, policy);
"invalid policy: %s", policy);
return TRUE;
}
......
......@@ -332,7 +332,7 @@ bolt_store_put_device (BoltStore *store,
type = bolt_device_get_device_type (device);
g_key_file_set_string (kf, DEVICE_GROUP, "type", bolt_device_type_to_string (type));
if (policy != BOLT_POLICY_INVALID)
if (policy != BOLT_POLICY_DEFAULT)
{
const char *str = bolt_policy_to_string (policy);
g_key_file_set_string (kf, USER_GROUP, "policy", str);
......
......@@ -406,10 +406,15 @@ bolt_client_enroll_device (BoltClient *client,
GDBusConnection *bus = NULL;
GVariant *params = NULL;
const char *opath = NULL;
const char *pstr;
g_return_val_if_fail (BOLT_IS_CLIENT (client), NULL);
params = g_variant_new ("(suu)", uid, (guint32) policy, (guint32) flags);
pstr = bolt_enum_to_string (BOLT_TYPE_POLICY, policy, error);
if (pstr == NULL)
return NULL;
params = g_variant_new ("(ssu)", uid, pstr, (guint32) flags);
val = g_dbus_proxy_call_sync (G_DBUS_PROXY (client),
"EnrollDevice",
params,
......
......@@ -268,8 +268,8 @@ enroll (BoltClient *client, int argc, char **argv)
const char *uid;
BoltPolicy policy = BOLT_POLICY_DEFAULT;
BoltAuthFlags flags = BOLT_AUTH_NONE;
static gchar *policy_arg;
static GOptionEntry options[] = {
const char *policy_arg = "default";
GOptionEntry options[] = {
{ "policy", 0, 0, G_OPTION_ARG_STRING, &policy_arg, "Policy for the device; one of {auto, manual, *default}", "POLICY" },
{ NULL }
};
......
......@@ -178,40 +178,19 @@ bolt_security_validate (BoltSecurity security)
BoltPolicy
bolt_policy_from_string (const char *str)
{
g_autoptr(GEnumClass) klass = NULL;
GEnumValue *value;
if (str == NULL)
return BOLT_POLICY_AUTO;
klass = g_type_class_ref (BOLT_TYPE_POLICY);
value = g_enum_get_value_by_nick (klass, str);
if (value == NULL)
return BOLT_POLICY_INVALID;
return value->value;
return bolt_enum_from_string (BOLT_TYPE_POLICY, str, NULL);
}
const char *
bolt_policy_to_string (BoltPolicy policy)
{
g_autoptr(GEnumClass) klass = NULL;
GEnumValue *value;
if (!bolt_policy_validate (policy))
return NULL;
klass = g_type_class_ref (BOLT_TYPE_POLICY);
value = g_enum_get_value (klass, policy);
return value->value_nick;
return bolt_enum_to_string (BOLT_TYPE_POLICY, policy, NULL);
}
gboolean
bolt_policy_validate (BoltPolicy policy)
{
return policy < BOLT_POLICY_INVALID && policy >= 0;
return bolt_enum_validate (BOLT_TYPE_POLICY, policy, NULL);
}
BoltDeviceType
......
......@@ -132,9 +132,6 @@ typedef enum {
BOLT_POLICY_MANUAL = 1,
BOLT_POLICY_AUTO = 2,
BOLT_POLICY_LAST,
BOLT_POLICY_INVALID = BOLT_POLICY_LAST
} BoltPolicy;
......
......@@ -28,7 +28,7 @@
</doc:para></doc:description></doc:doc>
</property>
<property name="DefaultPolicy" type="u" access="read">
<property name="DefaultPolicy" type="s" access="read">
<doc:doc><doc:description><doc:para>
The policy to use during enrollment when "default" was
sepcified.
......@@ -80,7 +80,7 @@
<doc:doc><doc:summary>The unique id of the device.</doc:summary>
</doc:doc>
</arg>
<arg type='u' name='policy' direction='in'>
<arg type='s' name='policy' direction='in'>
<doc:doc><doc:summary>Policy to use for the device.</doc:summary>
</doc:doc>
</arg>
......@@ -212,7 +212,7 @@
</doc:para></doc:description></doc:doc>
</property>
<property name="Policy" type="u" access="read">
<property name="Policy" type="s" access="read">
<doc:doc><doc:description><doc:para>
The authorization policy of the device.
</doc:para></doc:description></doc:doc>
......
......@@ -405,9 +405,9 @@ class BoltDevice(ProxyWrapper):
class BoltClient(ProxyWrapper):
signals = ['device_added', 'device_removed']
POLICY_DEFAULT = 0
POLICY_MANUAL = 1
POLICY_AUTO = 2
POLICY_DEFAULT = 'default'
POLICY_MANUAL = 'manual'
POLICY_AUTO = 'auto'
def __init__(self, bus):
super(BoltClient, self).__init__(bus,
......@@ -440,7 +440,7 @@ class BoltClient(ProxyWrapper):
return BoltDevice(bus, object_path)
def enroll(self, uid, policy=POLICY_DEFAULT, flags=0):
object_path = self.EnrollDevice("(suu)", uid, policy, flags)
object_path = self.EnrollDevice("(ssu)", uid, policy, flags)
if object_path is None:
return None
bus = self._proxy.get_connection()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment