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