bolt-device.c 6.74 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
 * Copyright © 2017 Red Hat, Inc
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *       Christian J. Kellner <christian@kellner.me>
 */

21
22
#include "config.h"

23
24
#include "bolt-dbus.h"
#include "bolt-device.h"
25
#include "bolt-enums.h"
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include "bolt-error.h"

#include <gio/gio.h>

struct _BoltDevice
{
  BoltProxy parent;
};

enum {
  PROP_0,

  /* D-Bus Props */
  PROP_UID,
  PROP_NAME,
  PROP_VENDOR,
42
  PROP_TYPE,
43
  PROP_STATUS,
44
  PROP_PARENT,
45
  PROP_SYSPATH,
46
  PROP_SECURITY,
47
  PROP_STORED,
48
49
  PROP_POLICY,
  PROP_KEY,
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73

  PROP_LAST
};

static GParamSpec *props[PROP_LAST] = {NULL, };

G_DEFINE_TYPE (BoltDevice,
               bolt_device,
               BOLT_TYPE_PROXY);

static void
bolt_device_get_property (GObject    *object,
                          guint       prop_id,
                          GValue     *value,
                          GParamSpec *pspec)
{
  if (bolt_proxy_get_dbus_property (object, prop_id, value))
    return;
}

static const BoltProxyProp *
bolt_device_get_dbus_props (guint *n)
{
  static BoltProxyProp dbus_props[] = {
74
75
76
    {"Uid",       "uid",       PROP_UID,        NULL},
    {"Name",      "name",      PROP_NAME,       NULL},
    {"Vendor",    "vendor",    PROP_VENDOR,     NULL},
77
    {"Type",      "type",      PROP_TYPE,       NULL},
78
    {"Status",    "status",    PROP_STATUS,     NULL},
79
    {"Parent",    "parent",    PROP_PARENT,     NULL},
80
81
    {"SysfsPath", "syspath",   PROP_SYSPATH,    NULL},
    {"Security",  "security",  PROP_SECURITY,   NULL},
82
    {"Stored",    "stored",    PROP_STORED,     NULL},
83
84
    {"Policy",    "policy",    PROP_POLICY,     NULL},
    {"Key",       "key",       PROP_KEY,        NULL}
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
  };

  *n = G_N_ELEMENTS (dbus_props);

  return dbus_props;
}


static void
bolt_device_class_init (BoltDeviceClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  BoltProxyClass *proxy_class = BOLT_PROXY_CLASS (klass);

  gobject_class->get_property = bolt_device_get_property;

  proxy_class->get_dbus_props = bolt_device_get_dbus_props;

  props[PROP_UID] =
    g_param_spec_string ("uid",
                         NULL, NULL,
                         "unknown",
                         G_PARAM_READABLE |
                         G_PARAM_STATIC_NICK);

  props[PROP_NAME] =
    g_param_spec_string ("name",
                         NULL, NULL,
                         "unknown",
                         G_PARAM_READABLE |
                         G_PARAM_STATIC_NICK);

  props[PROP_VENDOR] =
    g_param_spec_string ("vendor",
                         NULL, NULL,
                         "unknown",
                         G_PARAM_READABLE |
                         G_PARAM_STATIC_NICK);

124
125
126
127
128
129
130
131
  props[PROP_TYPE] =
    g_param_spec_enum ("type",
                       NULL, NULL,
                       BOLT_TYPE_DEVICE_TYPE,
                       BOLT_DEVICE_PERIPHERAL,
                       G_PARAM_READABLE |
                       G_PARAM_STATIC_NICK);

132
133
134
135
136
137
138
139
  props[PROP_STATUS] =
    g_param_spec_enum ("status",
                       NULL, NULL,
                       BOLT_TYPE_STATUS,
                       BOLT_STATUS_DISCONNECTED,
                       G_PARAM_READABLE |
                       G_PARAM_STATIC_NICK);

140
141
142
143
144
145
146
  props[PROP_PARENT] =
    g_param_spec_string ("parent",
                         NULL, NULL,
                         "unknown",
                         G_PARAM_READABLE |
                         G_PARAM_STATIC_NICK);

147
148
149
150
151
152
153
  props[PROP_SYSPATH] =
    g_param_spec_string ("syspath",
                         NULL, NULL,
                         "unknown",
                         G_PARAM_READABLE |
                         G_PARAM_STATIC_NICK);

154
155
156
157
158
159
160
  props[PROP_SECURITY] =
    g_param_spec_enum ("security",
                       NULL, NULL,
                       BOLT_TYPE_SECURITY,
                       BOLT_SECURITY_NONE,
                       G_PARAM_READABLE |
                       G_PARAM_STATIC_NICK);
161

162
163
164
165
166
167
  props[PROP_STORED] =
    g_param_spec_boolean ("stored",
                          NULL, NULL,
                          FALSE,
                          G_PARAM_READABLE |
                          G_PARAM_STATIC_NICK);
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184

  props[PROP_POLICY] =
    g_param_spec_enum ("policy",
                       NULL, NULL,
                       BOLT_TYPE_POLICY,
                       BOLT_POLICY_DEFAULT,
                       G_PARAM_READABLE |
                       G_PARAM_STATIC_NICK);

  props[PROP_KEY] =
    g_param_spec_enum ("key",
                       NULL, NULL,
                       BOLT_TYPE_KEY_STATE,
                       BOLT_KEY_MISSING,
                       G_PARAM_READABLE |
                       G_PARAM_STATIC_NICK);

185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
  g_object_class_install_properties (gobject_class,
                                     PROP_LAST,
                                     props);

}

static void
bolt_device_init (BoltDevice *mgr)
{
}

/* public methods */

BoltDevice *
bolt_device_new_for_object_path (GDBusConnection *bus,
                                 const char      *path,
                                 GError         **error)
{
  BoltDevice *dev;

  dev = g_initable_new (BOLT_TYPE_DEVICE,
                        NULL, error,
207
208
209
210
211
                        "g-flags", G_DBUS_PROXY_FLAGS_NONE,
                        "g-connection", bus,
                        "g-name", BOLT_DBUS_NAME,
                        "g-object-path", path,
                        "g-interface-name", BOLT_DBUS_DEVICE_INTERFACE,
212
213
214
215
216
217
                        NULL);

  return dev;
}

gboolean
218
219
220
bolt_device_authorize (BoltDevice   *dev,
                       BoltAuthFlags flags,
                       GError      **error)
221
222
223
224
225
{
  g_autoptr(GError) err = NULL;

  g_return_val_if_fail (BOLT_IS_DEVICE (dev), FALSE);

226
  g_dbus_proxy_call_sync (G_DBUS_PROXY (dev),
227
                          "Authorize",
228
                          g_variant_new ("(u)", (guint32) flags),
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
                          G_DBUS_CALL_FLAGS_NONE,
                          -1,
                          NULL,
                          &err);

  if (err != NULL)
    {
      if (g_dbus_error_is_remote_error (err))
        g_dbus_error_strip_remote_error (err);

      g_propagate_error (error, g_steal_pointer (&err));
      return FALSE;
    }

  return TRUE;
}